summaryrefslogtreecommitdiff
path: root/libelf/nlist.c
blob: f94432fb4af4cfa2e63c4830f1400e7837947ee9 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
nlist.c - implementation of the nlist(3) function.
Copyright (C) 1995 Michael Riepe <riepe@ifwsn4.ifw.uni-hannover.de>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <private.h>
#include <nlist.h>
#include <fcntl.h>

struct hash {
    const char* name;
    unsigned long   hash;
    Elf32_Sym*	sym;
};

static int
_elf_nlist(Elf *elf, struct nlist *nl) {
    Elf_Scn *symtab = NULL;
    Elf_Scn *strtab = NULL;
    Elf_Data *symdata;
    Elf_Data *strdata;
    Elf32_Ehdr *ehdr;
    Elf32_Shdr *shdr;
    Elf_Scn *scn;
    Elf32_Sym *symbols;
    const char *strings;
    unsigned nsymbols;
    unsigned nstrings;
    unsigned i;
    const char *name;
    struct hash *table;
    unsigned nhash;
    unsigned long hash;
    unsigned long j;

    if (!(ehdr = elf32_getehdr(elf))) {
	return -1;
    }
    scn = NULL;
    while ((scn = elf_nextscn(elf, scn))) {
	if (!(shdr = elf32_getshdr(scn))) {
	    return -1;
	}
	if (shdr->sh_type == SHT_SYMTAB) {
	    strtab = elf_getscn(elf, shdr->sh_link);
	    symtab = scn;
	    break;
	}
	if (shdr->sh_type == SHT_DYNSYM) {
	    strtab = elf_getscn(elf, shdr->sh_link);
	    symtab = scn;
	}
    }
    if (elf_errno()) {
	return -1;
    }
    symdata = elf_getdata(symtab, NULL);
    strdata = elf_getdata(strtab, NULL);
    if (!symdata || !strdata) {
	return -1;
    }
    symbols = (Elf32_Sym*)symdata->d_buf;
    strings = (const char*)strdata->d_buf;
    nsymbols = symdata->d_size / sizeof(Elf32_Sym);
    nstrings = strdata->d_size;
    if (!symbols || !strings || !nsymbols || !nstrings) {
	return -1;
    }

    /*
     * build a simple hash table
     */
    nhash = 3 * nsymbols - 4;
    if (!(table = (struct hash*)malloc(nhash * sizeof(*table)))) {
	return -1;
    }
    for (i = 0; i < nhash; i++) {
	table[i].name = NULL;
    }
    for (i = 1; i < nsymbols; i++) {
	if (symbols[i].st_name < 0 && symbols[i].st_name >= nstrings) {
	    free(table);
	    return -1;
	}
	if (symbols[i].st_name == 0) {
	    continue;
	}
	name = strings + symbols[i].st_name;
	hash = elf_hash(name);
	for (j = hash; table[j %= nhash].name; j += 3) {
	    if (table[j].hash != hash) {
		continue;
	    }
	    if (table[j].name == name || !strcmp(table[j].name, name)) {
		break;
	    }
	}
	table[j].hash = hash;
	table[j].name = name;
	table[j].sym = &symbols[i];
    }

    /*
     * symbol lookup
     */
    for (i = 0; (name = nl[i].n_name) && *name; i++) {
	hash = elf_hash(name);
	for (j = hash; table[j %= nhash].name; j += 3) {
	    if (table[j].hash == hash && !strcmp(table[j].name, name)) {
		break;
	    }
	}
	if (table[j].name) {
	    nl[i].n_value = table[j].sym->st_value;
	    nl[i].n_scnum = table[j].sym->st_shndx;
	}
	else {
	    nl[i].n_value = 0;
	    nl[i].n_scnum = 0;
	}
	/*
	 * this needs more work
	 */
	nl[i].n_type = 0;
	nl[i].n_sclass = 0;
	nl[i].n_numaux = 0;
    }
    free(table);
    return 0;
}

int
nlist(const char *filename, struct nlist *nl) {
    int result = -1;
    unsigned oldver;
    Elf *elf;
    int fd;

    if ((oldver = elf_version(EV_CURRENT)) != EV_NONE) {
	if ((fd = open(filename, O_RDONLY)) != -1) {
	    if ((elf = elf_begin(fd, ELF_C_READ, NULL))) {
		result = _elf_nlist(elf, nl);
		elf_end(elf);
	    }
	    close(fd);
	}
	elf_version(oldver);
    }
    if (result) {
	while (nl->n_name && *nl->n_name) {
	    nl->n_value = 0;
	    nl++;
	}
    }
    return result;
}