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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/* Macros to enable writing native and generic ELF access code.
Copyright (C) 2003 Red Hat, Inc.
Written by Ulrich Drepper <drepper@redhat.com>, 2003.
This program is Open Source software; you can redistribute it and/or
modify it under the terms of the Open Software License version 1.0 as
published by the Open Source Initiative.
You should have received a copy of the Open Software License along
with this program; if not, you may obtain a copy of the Open Software
License version 1.0 from http://www.opensource.org/licenses/osl.php or
by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
3001 King Ranch Road, Ukiah, CA 95482. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <libebl.h>
/* By default the linker is handling all architectures. But it can
be configured to be a native-only linker. */
#if NATIVE_ELF == 32
/* 32-bit only. */
# define XElf_Ehdr Elf32_Ehdr
# define XElf_Shdr Elf32_Shdr
# define XElf_Off Elf32_Off
# define XElf_Addr Elf32_Addr
# define XElf_Half Elf32_Half
# define XElf_Word Elf32_Word
# define XElf_Xword Elf32_Word
# define XElf_Sxword Elf32_Sword
# define XElf_Versym Elf32_Versym
# define XElf_Sym Elf32_Sym
# define XElf_Rel Elf32_Rel
# define XElf_Rela Elf32_Rela
# define XElf_Ehdr_vardef(name) Elf32_Ehdr *name
# define xelf_getehdr(elf, name) name = elf32_getehdr (elf)
# define xelf_getehdr_copy(elf, name, copy) \
(copy) = *(name = elf32_getehdr (elf))
# define xelf_newehdr(elf, klass) elf32_newehdr (elf)
# define xelf_update_ehdr(elf, ehdr) \
/* nothing */ ((void) (elf), (void) (ehdr), 1)
# define xelf_getclass(elf) ELFCLASS32
# define XElf_Phdr_vardef(name) Elf32_Phdr *name
# define xelf_newphdr(elf, n) elf32_newphdr (elf, n)
# define xelf_getphdr(elf, idx, name) name = elf32_getphdr (elf) + idx
# define xelf_getphdr_ptr(elf, idx, name) name = elf32_getphdr (elf) + idx
# define xelf_update_phdr(elf, idx, phdr) \
/* nothing */ ((void) (elf), (void) (idx), (void) (phdr), 1)
# define XElf_Shdr_vardef(name) Elf32_Shdr *name
# define xelf_getshdr(scn, name) name = elf32_getshdr (scn)
# define xelf_getshdr_copy(scn, name, copy) \
(copy) = *(name = elf32_getshdr (scn))
# define xelf_update_shdr(scn, shdr) \
/* nothing */ ((void) (scn), (void) (shdr), 1)
# define XElf_Sym_vardef(name) Elf32_Sym *name
# define xelf_getsym(data, idx, name) \
name = &((Elf32_Sym *) (data)->d_buf)[idx]
# define xelf_getsym_ptr(data, idx, name) \
name = &((Elf32_Sym *) (data)->d_buf)[idx]
# define xelf_getsymshndx(data, ndxdata, idx, name1, name2) \
(name1 = &((Elf32_Sym *) ((data)->d_buf))[idx]); \
name2 = (unlikely ((ndxdata) != NULL) \
? ((Elf32_Word *) ((ndxdata)->d_buf))[idx] : 0)
# define xelf_update_sym(data, idx, sym) \
/* nothing */ ((void) (data), (void) (idx), (void) (sym), 1)
# define xelf_update_symshndx(data, ndxdata, idx, name1, name2, datachanged) \
if (datachanged) \
((Elf32_Sym *) ((data)->d_buf))[idx] = *name1; \
if (unlikely (ndxdata != NULL)) \
((Elf32_Word *) ((ndxdata)->d_buf))[idx] = name2
# define XElf_Versym_vardef(name) Elf32_Versym name
# define xelf_getversym_copy(data, idx, name) \
(name = ((Elf32_Versym *) ((data)->d_buf))[idx], &name)
# define XElf_Dyn_vardef(name) Elf32_Dyn *name
# define xelf_getdyn(data, idx, name) \
name = &((Elf32_Dyn *) ((data)->d_buf))[idx]
# define xelf_getdyn_ptr(data, idx, name) \
name = &((Elf32_Dyn *) ((data)->d_buf))[idx]
# define xelf_update_dyn(data, idx, name) \
/* nothing */ ((void) (data), (void) (idx), (void) (name), 1)
# define XElf_Rel_vardef(name) Elf32_Rel *name
# define xelf_getrel(data, idx, name) \
name = &((Elf32_Rel *) ((data)->d_buf))[idx]
# define xelf_getrel_ptr(data, idx, name) \
name = &((Elf32_Rel *) ((data)->d_buf))[idx]
# define xelf_update_rel(data, idx, name) \
/* nothing */ ((void) (data), (void) (idx), (void) (name), 1)
# define XElf_Rela_vardef(name) Elf32_Rela *name
# define xelf_getrela(data, idx, name) \
name = &((Elf32_Rela *) ((data)->d_buf))[idx]
# define xelf_getrela_ptr(data, idx, name) \
name = &((Elf32_Rela *) ((data)->d_buf))[idx]
# define xelf_update_rela(data, idx, name) \
/* nothing */ ((void) (data), (void) (idx), (void) (name), 1)
# define XElf_Verdef_vardef(name) Elf32_Verdef *name
# define xelf_getverdef(data, offset, name) \
name = ((Elf32_Verdef *) ((char *) ((data)->d_buf) + (offset)))
# define XElf_Verdaux_vardef(name) Elf32_Verdaux *name
# define xelf_getverdaux(data, offset, name) \
name = ((Elf32_Verdaux *) ((char *) ((data)->d_buf) + (offset)))
# define XELF_ST_TYPE(info) ELF32_ST_TYPE (info)
# define XELF_ST_BIND(info) ELF32_ST_BIND (info)
# define XELF_ST_INFO(bind, type) ELF32_ST_INFO (bind, type)
# define XELF_ST_VISIBILITY(info) ELF32_ST_VISIBILITY (info)
# define XELF_R_SYM(info) ELF32_R_SYM (info)
# define XELF_R_TYPE(info) ELF32_R_TYPE (info)
# define XELF_R_INFO(sym, type) ELF32_R_INFO (sym, type)
# define xelf_fsize(elf, type, cnt) \
(__builtin_constant_p (type) \
? ({ size_t fsize; \
switch (type) \
{ \
case ELF_T_BYTE: fsize = 1; break; \
case ELF_T_ADDR: fsize = sizeof (Elf32_Addr); break; \
case ELF_T_DYN: fsize = sizeof (Elf32_Dyn); break; \
case ELF_T_EHDR: fsize = sizeof (Elf32_Ehdr); break; \
case ELF_T_HALF: fsize = sizeof (Elf32_Half); break; \
case ELF_T_OFF: fsize = sizeof (Elf32_Off); break; \
case ELF_T_PHDR: fsize = sizeof (Elf32_Phdr); break; \
case ELF_T_RELA: fsize = sizeof (Elf32_Rela); break; \
case ELF_T_REL: fsize = sizeof (Elf32_Rel); break; \
case ELF_T_SHDR: fsize = sizeof (Elf32_Shdr); break; \
case ELF_T_SWORD: fsize = sizeof (Elf32_Sword); break; \
case ELF_T_SYM: fsize = sizeof (Elf32_Sym); break; \
case ELF_T_WORD: fsize = sizeof (Elf32_Word); break; \
case ELF_T_XWORD: fsize = sizeof (Elf32_Xword); break; \
case ELF_T_SXWORD: fsize = sizeof (Elf32_Sxword); break; \
case ELF_T_VDEF: fsize = sizeof (Elf32_Verdef); break; \
case ELF_T_VDAUX: fsize = sizeof (Elf32_Verdaux); break; \
case ELF_T_VNEED: fsize = sizeof (Elf32_Verneed); break; \
case ELF_T_VNAUX: fsize = sizeof (Elf32_Vernaux); break; \
case ELF_T_NHDR: fsize = sizeof (Elf32_Nhdr); break; \
case ELF_T_SYMINFO: fsize = sizeof (Elf32_Syminfo); break; \
case ELF_T_MOVE: fsize = sizeof (Elf32_Move); break; \
default: fsize = 0; break; \
} \
fsize * (cnt); }) \
: gelf_fsize (elf, type, cnt, EV_CURRENT))
#elif NATIVE_ELF == 64
/* 64-bit only. */
# define XElf_Ehdr Elf64_Ehdr
# define XElf_Shdr Elf64_Shdr
# define XElf_Addr Elf64_Addr
# define XElf_Half Elf64_Half
# define XElf_Off Elf64_Off
# define XElf_Word Elf64_Word
# define XElf_Xword Elf64_Xword
# define XElf_Sxword Elf64_Sxword
# define XElf_Versym Elf64_Versym
# define XElf_Sym Elf64_Sym
# define XElf_Rel Elf64_Rel
# define XElf_Rela Elf64_Rela
# define XElf_Ehdr_vardef(name) Elf64_Ehdr *name
# define xelf_getehdr(elf, name) name = elf64_getehdr (elf)
# define xelf_getehdr_copy(elf, name, copy) \
(copy) = *(name = elf64_getehdr (elf))
# define xelf_newehdr(elf, klass) elf64_newehdr (elf)
# define xelf_update_ehdr(elf, ehdr) \
/* nothing */ ((void) (elf), (void) (ehdr), 1)
# define xelf_getclass(elf) ELFCLASS32
# define XElf_Phdr_vardef(name) Elf64_Phdr *name
# define xelf_newphdr(elf, n) elf64_newphdr (elf, n)
# define xelf_getphdr(elf, idx, name) name = elf64_getphdr (elf) + idx
# define xelf_getphdr_ptr(elf, idx, name) name = elf64_getphdr (elf) + idx
# define xelf_update_phdr(elf, idx, phdr) \
/* nothing */ ((void) (elf), (void) (idx), (void) (phdr), 1)
# define XElf_Shdr_vardef(name) Elf64_Shdr *name
# define xelf_getshdr(scn, name) name = elf64_getshdr (scn)
# define xelf_getshdr_copy(scn, name, copy) \
(copy) = *(name = elf64_getshdr (scn))
# define xelf_update_shdr(scn, shdr) \
/* nothing */ ((void) (scn), (void) (shdr), 1)
# define XElf_Sym_vardef(name) Elf64_Sym *name
# define xelf_getsym(data, idx, name) \
name = &((Elf64_Sym *) (data)->d_buf)[idx]
# define xelf_getsym_ptr(data, idx, name) \
name = &((Elf64_Sym *) (data)->d_buf)[idx]
# define xelf_getsymshndx(data, ndxdata, idx, name1, name2) \
(name1 = &((Elf64_Sym *) ((data)->d_buf))[idx]); \
name2 = (unlikely ((ndxdata) != NULL) \
? ((Elf32_Word *) ((ndxdata)->d_buf))[idx] : 0)
# define xelf_update_sym(data, idx, sym) \
/* nothing */ ((void) (data), (void) (idx), (void) (sym), 1)
# define xelf_update_symshndx(data, ndxdata, idx, name1, name2, datachanged) \
if (datachanged) \
((Elf64_Sym *) ((data)->d_buf))[idx] = *name1; \
if (ndxdata != NULL) \
(((Elf32_Word *) ((ndxdata)->d_buf))[idx] = name2)
# define XElf_Versym_vardef(name) Elf64_Versym name
# define xelf_getversym_copy(data, idx, name) \
(name = ((Elf64_Versym *) ((data)->d_buf))[idx], (&name))
# define XElf_Dyn_vardef(name) Elf64_Dyn *name
# define xelf_getdyn(data, idx, name) \
name = &((Elf64_Dyn *) ((data)->d_buf))[idx]
# define xelf_getdyn_ptr(data, idx, name) \
name = &((Elf64_Dyn *) ((data)->d_buf))[idx]
# define xelf_update_dyn(data, idx, name) \
/* nothing */ ((void) (data), (void) (idx), (void) (name), 1)
# define XElf_Rel_vardef(name) Elf64_Rel *name
# define xelf_getrel(data, idx, name) \
name = &((Elf64_Rel *) ((data)->d_buf))[idx]
# define xelf_getrel_ptr(data, idx, name) \
name = &((Elf64_Rel *) ((data)->d_buf))[idx]
# define xelf_update_rel(data, idx, name) \
/* nothing */ ((void) (data), (void) (idx), (void) (name), 1)
# define XElf_Rela_vardef(name) Elf64_Rela *name
# define xelf_getrela(data, idx, name) \
name = &((Elf64_Rela *) ((data)->d_buf))[idx]
# define xelf_getrela_ptr(data, idx, name) \
name = &((Elf64_Rela *) ((data)->d_buf))[idx]
# define xelf_update_rela(data, idx, name) \
/* nothing */ ((void) (data), (void) (idx), (void) (name), 1)
# define XElf_Verdef_vardef(name) Elf64_Verdef *name
# define xelf_getverdef(data, offset, name) \
name = ((Elf64_Verdef *) ((char *) ((data)->d_buf) + (offset)))
# define XElf_Verdaux_vardef(name) Elf64_Verdaux *name
# define xelf_getverdaux(data, offset, name) \
name = ((Elf64_Verdaux *) ((char *) ((data)->d_buf) + (offset)))
# define XELF_ST_TYPE(info) ELF64_ST_TYPE (info)
# define XELF_ST_BIND(info) ELF64_ST_BIND (info)
# define XELF_ST_INFO(bind, type) ELF64_ST_INFO (bind, type)
# define XELF_ST_VISIBILITY(info) ELF64_ST_VISIBILITY (info)
# define XELF_R_SYM(info) ELF64_R_SYM (info)
# define XELF_R_TYPE(info) ELF64_R_TYPE (info)
# define XELF_R_INFO(sym, type) ELF64_R_INFO (sym, type)
# define xelf_fsize(elf, type, cnt) \
(__builtin_constant_p (type) \
? ({ size_t fsize; \
switch (type) \
{ \
case ELF_T_BYTE: fsize = 1; break; \
case ELF_T_ADDR: fsize = sizeof (Elf64_Addr); break; \
case ELF_T_DYN: fsize = sizeof (Elf64_Dyn); break; \
case ELF_T_EHDR: fsize = sizeof (Elf64_Ehdr); break; \
case ELF_T_HALF: fsize = sizeof (Elf64_Half); break; \
case ELF_T_OFF: fsize = sizeof (Elf64_Off); break; \
case ELF_T_PHDR: fsize = sizeof (Elf64_Phdr); break; \
case ELF_T_RELA: fsize = sizeof (Elf64_Rela); break; \
case ELF_T_REL: fsize = sizeof (Elf64_Rel); break; \
case ELF_T_SHDR: fsize = sizeof (Elf64_Shdr); break; \
case ELF_T_SWORD: fsize = sizeof (Elf64_Sword); break; \
case ELF_T_SYM: fsize = sizeof (Elf64_Sym); break; \
case ELF_T_WORD: fsize = sizeof (Elf64_Word); break; \
case ELF_T_XWORD: fsize = sizeof (Elf64_Xword); break; \
case ELF_T_SXWORD: fsize = sizeof (Elf64_Sxword); break; \
case ELF_T_VDEF: fsize = sizeof (Elf64_Verdef); break; \
case ELF_T_VDAUX: fsize = sizeof (Elf64_Verdaux); break; \
case ELF_T_VNEED: fsize = sizeof (Elf64_Verneed); break; \
case ELF_T_VNAUX: fsize = sizeof (Elf64_Vernaux); break; \
case ELF_T_NHDR: fsize = sizeof (Elf64_Nhdr); break; \
case ELF_T_SYMINFO: fsize = sizeof (Elf64_Syminfo); break; \
case ELF_T_MOVE: fsize = sizeof (Elf64_Move); break; \
default: fsize = 0; break; \
} \
fsize * (cnt); }) \
: gelf_fsize (elf, type, cnt, EV_CURRENT))
#else
# include <gelf.h>
/* Generic linker. */
# define XElf_Ehdr GElf_Ehdr
# define XElf_Shdr GElf_Shdr
# define XElf_Addr GElf_Addr
# define XElf_Half GElf_Half
# define XElf_Off GElf_Off
# define XElf_Word GElf_Word
# define XElf_Xword GElf_Xword
# define XElf_Sxword GElf_Sxword
# define XElf_Versym GElf_Versym
# define XElf_Sym GElf_Sym
# define XElf_Rel GElf_Rel
# define XElf_Rela GElf_Rela
# define XElf_Ehdr_vardef(name) GElf_Ehdr name##_mem; GElf_Ehdr *name
# define xelf_getehdr(elf, name) name = gelf_getehdr (elf, &name##_mem)
# define xelf_getehdr_copy(elf, name, copy) \
name = gelf_getehdr (elf, &(copy))
# define xelf_newehdr(elf, klass) gelf_newehdr (elf, klass)
# define xelf_update_ehdr(elf, ehdr) gelf_update_ehdr (elf, ehdr)
# define xelf_getclass(elf) gelf_getclass (elf)
# define XElf_Phdr_vardef(name) GElf_Phdr name##_mem; GElf_Phdr *name
# define xelf_newphdr(elf, n) gelf_newphdr (elf, n)
# define xelf_getphdr(elf, idx, name) \
name = gelf_getphdr (elf, idx, &name##_mem)
# define xelf_getphdr_ptr(elf, idx, name) \
name = &name##_mem
# define xelf_update_phdr(elf, idx, phdr) \
gelf_update_phdr (elf, idx, phdr)
# define XElf_Shdr_vardef(name) GElf_Shdr name##_mem; GElf_Shdr *name
# define xelf_getshdr(scn, name) name = gelf_getshdr (scn, &name##_mem)
# define xelf_getshdr_copy(scn, name, copy) \
name = gelf_getshdr (scn, &(copy))
# define xelf_update_shdr(scn, shdr) gelf_update_shdr (scn, shdr)
# define XElf_Sym_vardef(name) GElf_Sym name##_mem; GElf_Sym *name
# define xelf_getsym(data, idx, name) \
name = gelf_getsym (data, idx, &name##_mem)
# define xelf_getsym_ptr(data, idx, name) \
name = &name##_mem
# define xelf_getsymshndx(data, ndxdata, idx, name1, name2) \
name1 = gelf_getsymshndx (data, ndxdata, idx, &name1##_mem, &(name2))
# define xelf_update_sym(data, idx, sym) gelf_update_sym (data, idx, sym)
# define xelf_update_symshndx(data, ndxdata, idx, name1, name2, datachanged) \
gelf_update_symshndx (data, ndxdata, idx, name1, name2)
# define XElf_Versym_vardef(name) GElf_Versym name
# define xelf_getversym_copy(data, idx, name) \
gelf_getversym (data, idx, &name)
# define XElf_Dyn_vardef(name) GElf_Dyn name##_mem; GElf_Dyn *name
# define xelf_getdyn(data, idx, name) \
name = gelf_getdyn (data, idx, &name##_mem)
# define xelf_getdyn_ptr(data, idx, name) \
name = &name##_mem
# define xelf_update_dyn(data, idx, name) \
gelf_update_dyn (data, idx, name)
# define XElf_Rel_vardef(name) GElf_Rel name##_mem; GElf_Rel *name
# define xelf_getrel(data, idx, name) \
name = gelf_getrel (data, idx, &name##_mem)
# define xelf_getrel_ptr(data, idx, name) \
name = &name##_mem
# define xelf_update_rel(data, idx, name) \
gelf_update_rel (data, idx, name)
# define XElf_Rela_vardef(name) GElf_Rela name##_mem; GElf_Rela *name
# define xelf_getrela(data, idx, name) \
name = gelf_getrela (data, idx, &name##_mem)
# define xelf_getrela_ptr(data, idx, name) \
name = &name##_mem
# define xelf_update_rela(data, idx, name) \
gelf_update_rela (data, idx, name)
# define XElf_Verdef_vardef(name) GElf_Verdef name##_mem; GElf_Verdef *name
# define xelf_getverdef(data, offset, name) \
name = gelf_getverdef (data, offset, &name##_mem)
# define XElf_Verdaux_vardef(name) GElf_Verdaux name##_mem; GElf_Verdaux *name
# define xelf_getverdaux(data, offset, name) \
name = gelf_getverdaux (data, offset, &name##_mem)
# define XELF_ST_TYPE(info) GELF_ST_TYPE (info)
# define XELF_ST_BIND(info) GELF_ST_BIND (info)
# define XELF_ST_INFO(bind, type) GELF_ST_INFO (bind, type)
# define XELF_ST_VISIBILITY(info) GELF_ST_VISIBILITY (info)
# define XELF_R_SYM(info) GELF_R_SYM (info)
# define XELF_R_TYPE(info) GELF_R_TYPE (info)
# define XELF_R_INFO(sym, type) GELF_R_INFO (sym, type)
# define xelf_fsize(elf, type, cnt) \
gelf_fsize (elf, type, cnt, EV_CURRENT)
#endif
|