summaryrefslogtreecommitdiff
path: root/minix/commands/swifi/read_nlist.c
blob: 9c0335dc3883f77502d65215e2007e7e156bf2c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* ELF functions-only symbol table extractor by David van Moolenbroek.
 * Replaces generic a.out version by Philip Homburg.
 * Temporary solution until libc's nlist(3) becomes usable.
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <libelf.h>
#include <nlist.h>

static int get_syms(Elf *elf, Elf_Scn *scn, Elf32_Shdr *shdr,
	struct nlist **nlistp)
{
	Elf_Data *data;
	Elf32_Sym *sym;
	unsigned long nsyms;
	struct nlist *nlp;
	char *name;
	size_t size;
	int i;

	if ((data = elf_getdata(scn, NULL)) == NULL) {
		errno = ENOEXEC;
		return -1;
	}

	nsyms = data->d_size / sizeof(Elf32_Sym);

	size = sizeof(struct nlist) * nsyms;
	if ((*nlistp = nlp = malloc(size)) == NULL)
		return -1;
	memset(nlp, 0, size);

	sym = (Elf32_Sym *) data->d_buf;
	for (i = 0; i < nsyms; i++, sym++, nlp++) {
		nlp->n_value = sym->st_value;

		/* The caller only cares about N_TEXT. Properly setting other
		 * types would involve crosschecking with section types.
		 */
		switch (ELF32_ST_TYPE(sym->st_info)) {
		case STT_FUNC:   nlp->n_type = N_TEXT; break;
		default:         nlp->n_type = N_UNDF; break;
		}

		name = elf_strptr(elf, shdr->sh_link, (size_t) sym->st_name);
		if ((nlp->n_name = strdup(name ? name : "")) == NULL) {
			free(*nlistp);
			return -1;
		}
	}

	return nsyms;
}

int read_nlist(const char *filename, struct nlist **nlist_table)
{
	/* Read in the symbol table of 'filename'. Return the resulting symbols
	 * as an array, with 'nlist_table' set to point to it, and the number
	 * of elements as the return value. The caller has no way to free the
	 * results. Return 0 if the executable contains no symbols. Upon error,
	 * return -1 and set errno to an appropriate value.
	 */
	Elf *elf;
	Elf_Scn *scn;
	Elf32_Shdr *shdr;
	int res, fd;

	if (elf_version(EV_CURRENT) == EV_NONE) {
		errno = EINVAL;
		return -1;
	}

	if ((fd = open(filename, O_RDONLY)) < 0)
		return -1;

	elf = elf_begin(fd, ELF_C_READ, NULL);
	if (elf == NULL) {
		errno = ENOEXEC;
		close(fd);
		return -1;
	}

	scn = NULL;
	res = 0;
	while ((scn = elf_nextscn(elf, scn)) != NULL) {
		if ((shdr = elf32_getshdr(scn)) == NULL)
			continue;

		if (shdr->sh_type != SHT_SYMTAB)
			continue;

		res = get_syms(elf, scn, shdr, nlist_table);
		break;
	}

	elf_end(elf);
	close(fd);

	return res;
}