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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/* $OpenBSD: elf.c,v 1.2 2004/01/13 17:32:32 mickey Exp $ */
/*
* Copyright (c) 2003 Michael Shalayeff
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#if ELF_TARG_CLASS == ELFCLASS32
#define swap_addr swap32
#define swap_off swap32
#define swap_sword swap32
#define swap_word swap32
#define swap_sxword swap32
#define swap_xword swap32
#define swap_half swap16
#define swap_quarter swap16
#elif ELF_TARG_CLASS == ELFCLASS64
#define swap_addr swap64
#define swap_off swap64
#ifdef __alpha__
#define swap_sword swap64
#define swap_word swap64
#else
#define swap_sword swap32
#define swap_word swap32
#endif
#define swap_sxword swap64
#define swap_xword swap64
#define swap_half swap64
#define swap_quarter swap16
#else
#error "Unsupported ELF class"
#endif
int
elf_fix_header(Elf_Ehdr *eh)
{
/* nothing to do */
if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
return (0);
eh->e_type = swap16(eh->e_type);
eh->e_machine = swap16(eh->e_machine);
eh->e_version = swap32(eh->e_version);
eh->e_entry = swap_addr(eh->e_entry);
eh->e_phoff = swap_off(eh->e_phoff);
eh->e_shoff = swap_off(eh->e_shoff);
eh->e_flags = swap32(eh->e_flags);
eh->e_ehsize = swap16(eh->e_ehsize);
eh->e_phentsize = swap16(eh->e_phentsize);
eh->e_phnum = swap16(eh->e_phnum);
eh->e_shentsize = swap16(eh->e_shentsize);
eh->e_shnum = swap16(eh->e_shnum);
eh->e_shstrndx = swap16(eh->e_shstrndx);
return (1);
}
int
elf_fix_shdrs(Elf_Ehdr *eh, Elf_Shdr *shdr)
{
int i;
/* nothing to do */
if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
return (0);
for (i = eh->e_shnum; i--; shdr++) {
shdr->sh_name = swap32(shdr->sh_name);
shdr->sh_type = swap32(shdr->sh_type);
shdr->sh_flags = swap_xword(shdr->sh_flags);
shdr->sh_addr = swap_addr(shdr->sh_addr);
shdr->sh_offset = swap_off(shdr->sh_offset);
shdr->sh_size = swap_xword(shdr->sh_size);
shdr->sh_link = swap32(shdr->sh_link);
shdr->sh_info = swap32(shdr->sh_info);
shdr->sh_addralign = swap_xword(shdr->sh_addralign);
shdr->sh_entsize = swap_xword(shdr->sh_entsize);
}
return (1);
}
int
elf_fix_phdrs(Elf_Ehdr *eh, Elf_Phdr *phdr)
{
int i;
/* nothing to do */
if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
return (0);
for (i = eh->e_phnum; i--; phdr++) {
phdr->p_type = swap32(phdr->p_type);
phdr->p_flags = swap32(phdr->p_flags);
phdr->p_offset = swap_off(phdr->p_offset);
phdr->p_vaddr = swap_addr(phdr->p_vaddr);
phdr->p_paddr = swap_addr(phdr->p_paddr);
phdr->p_filesz = swap_xword(phdr->p_filesz);
phdr->p_memsz = swap_xword(phdr->p_memsz);
phdr->p_align = swap_xword(phdr->p_align);
}
return (1);
}
int
elf_fix_sym(Elf_Ehdr *eh, Elf_Sym *sym)
{
/* nothing to do */
if (eh->e_ident[EI_DATA] == ELF_TARG_DATA)
return (0);
sym->st_name = swap32(sym->st_name);
sym->st_shndx = swap16(sym->st_shndx);
sym->st_value = swap_addr(sym->st_value);
sym->st_size = swap_xword(sym->st_size);
return (1);
}
/*
* Devise nlist's type from Elf_Sym.
* XXX this task is done as well in libc and kvm_mkdb.
*/
int
elf2nlist(Elf_Sym *sym, Elf_Ehdr *eh, Elf_Shdr *shdr, char *shstr, struct nlist *np)
{
/* extern char *stab; */
const char *sn;
if (sym->st_shndx < eh->e_shnum)
sn = shstr + shdr[sym->st_shndx].sh_name;
else
sn = "";
/* printf("%s 0x%x %s(0x%x)\n", stab + sym->st_name, sym->st_info, sn, sym->st_shndx); */
switch(ELF_ST_TYPE(sym->st_info)) {
case STT_NOTYPE:
switch (sym->st_shndx) {
case SHN_UNDEF:
np->n_type = N_UNDF | N_EXT;
break;
case SHN_ABS:
np->n_type = N_ABS;
break;
case SHN_COMMON:
np->n_type = N_COMM;
break;
default:
if (sym->st_shndx >= eh->e_shnum)
np->n_type = N_COMM | N_EXT;
else if (!strcmp(sn, ELF_TEXT))
np->n_type = N_TEXT;
else if (!strcmp(sn, ELF_RODATA)) {
np->n_type = N_DATA;
np->n_other = 'r';
} else if (!strcmp(sn, ELF_DATA))
np->n_type = N_DATA;
else if (!strcmp(sn, ELF_BSS))
np->n_type = N_BSS;
else
np->n_other = '?';
break;
}
break;
case STT_FUNC:
np->n_type = N_TEXT;
if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
np->n_type = N_INDR;
np->n_other = 'W';
} else if (sym->st_shndx == SHN_UNDEF)
np->n_type = N_UNDF | N_EXT;
else if (strcmp(sn, ELF_TEXT)) /* XXX GNU compat */
np->n_other = '?';
break;
case STT_OBJECT:
np->n_type = N_DATA;
if (sym->st_shndx == SHN_ABS)
np->n_type = N_ABS;
if (sym->st_shndx == SHN_COMMON)
np->n_type = N_COMM;
else if (sym->st_shndx >= eh->e_shnum)
break;
else if (!strcmp(sn, ELF_BSS))
np->n_type = N_BSS;
else if (!strcmp(sn, ELF_RODATA))
np->n_other = 'r';
break;
case STT_FILE:
np->n_type = N_FN | N_EXT;
break;
/* XXX how about cross-nm then ? */
#ifdef STT_PARISC_MILLI
case STT_PARISC_MILLI:
np->n_type = N_TEXT;
break;
#endif
default:
np->n_other = '?';
break;
}
if (np->n_type != N_UNDF && ELF_ST_BIND(sym->st_info) != STB_LOCAL) {
np->n_type |= N_EXT;
if (np->n_other)
np->n_other = toupper(np->n_other);
}
return (0);
}
|