2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #ifndef included_clib_elf_h
39 #define included_clib_elf_h
41 #include <vppinfra/format.h>
42 #include <vppinfra/hash.h>
43 #include <vppinfra/vec.h>
44 #include <vppinfra/byte_order.h>
46 #define foreach_elf_file_class \
47 _ (CLASS_NONE) _ (32BIT) _ (64BIT)
49 #define foreach_elf_data_encoding \
51 _ (TWOS_COMPLEMENT_LITTLE_ENDIAN) \
52 _ (TWOS_COMPLEMENT_BIG_ENDIAN)
54 #define ELF_VERSION_NONE (0)
55 #define ELF_VERSION_CURRENT (1)
57 #define foreach_elf_abi \
66 _ (COMPAQ_TRU64, 10) \
72 /* Legal values for type (object file type). */
73 #define foreach_elf_file_type \
79 _ (OS_SPECIFIC_LO, 0xfe00) \
80 _ (OS_SPECIFIC_HI, 0xfeff) \
81 _ (ARCH_SPECIFIC_LO, 0xff00) \
82 _ (ARCH_SPECIFIC_HI, 0xffff)
84 /* Legal values for architecture. */
85 #define foreach_elf_architecture \
86 _ (NONE, 0) /* No machine */ \
87 _ (M32, 1) /* AT&T WE 32100 */ \
88 _ (SPARC, 2) /* SUN SPARC */ \
89 _ (386, 3) /* Intel 80386 */ \
90 _ (68K, 4) /* Motorola m68k family */ \
91 _ (88K, 5) /* Motorola m88k family */ \
92 _ (860, 7) /* Intel 80860 */ \
93 _ (MIPS, 8) /* MIPS R3000 big-endian */ \
94 _ (S370, 9) /* IBM System/370 */ \
95 _ (MIPS_RS3_LE, 10) /* MIPS R3000 little-endian */ \
96 _ (PARISC, 15) /* HPPA */ \
97 _ (VPP500, 17) /* Fujitsu VPP500 */ \
98 _ (SPARC32PLUS, 18) /* Sun's "v8plus" */ \
99 _ (960, 19) /* Intel 80960 */ \
100 _ (PPC, 20) /* PowerPC */ \
101 _ (PPC64, 21) /* PowerPC 64-bit */ \
102 _ (S390, 22) /* IBM S390 */ \
103 _ (V800, 36) /* NEC V800 series */ \
104 _ (FR20, 37) /* Fujitsu FR20 */ \
105 _ (RH32, 38) /* TRW RH-32 */ \
106 _ (RCE, 39) /* Motorola RCE */ \
107 _ (ARM, 40) /* ARM */ \
108 _ (FAKE_ALPHA, 41) /* Digital Alpha */ \
109 _ (SH, 42) /* Hitachi SH */ \
110 _ (SPARCV9, 43) /* SPARC v9 64-bit */ \
111 _ (TRICORE, 44) /* Siemens Tricore */ \
112 _ (ARC, 45) /* Argonaut RISC Core */ \
113 _ (H8_300, 46) /* Hitachi H8/300 */ \
114 _ (H8_300H, 47) /* Hitachi H8/300H */ \
115 _ (H8S, 48) /* Hitachi H8S */ \
116 _ (H8_500, 49) /* Hitachi H8/500 */ \
117 _ (IA_64, 50) /* Intel Merced */ \
118 _ (MIPS_X, 51) /* Stanford MIPS-X */ \
119 _ (COLDFIRE, 52) /* Motorola Coldfire */ \
120 _ (68HC12, 53) /* Motorola M68HC12 */ \
121 _ (MMA, 54) /* Fujitsu MMA Multimedia Accel. */ \
122 _ (PCP, 55) /* Siemens PCP */ \
123 _ (NCPU, 56) /* Sony nCPU embeeded RISC */ \
124 _ (NDR1, 57) /* Denso NDR1 microprocessor */ \
125 _ (STARCORE, 58) /* Motorola Start*Core processor */ \
126 _ (ME16, 59) /* Toyota ME16 processor */ \
127 _ (ST100, 60) /* STMicroelectronic ST100 */ \
128 _ (TINYJ, 61) /* Advanced Logic Corp. Tinyj */ \
129 _ (X86_64, 62) /* AMD x86-64 architecture */ \
130 _ (PDSP, 63) /* Sony DSP Processor */ \
131 _ (FX66, 66) /* Siemens FX66 microcontroller */ \
132 _ (ST9PLUS, 67) /* STMicroelectronics ST9+ 8/16 mc */ \
133 _ (ST7, 68) /* STmicroelectronics ST7 8 bit mc */ \
134 _ (68HC16, 69) /* Motorola MC68HC16 */ \
135 _ (68HC11, 70) /* Motorola MC68HC11 */ \
136 _ (68HC08, 71) /* Motorola MC68HC08 */ \
137 _ (68HC05, 72) /* Motorola MC68HC05 */ \
138 _ (SVX, 73) /* Silicon Graphics SVx */ \
139 _ (ST19, 74) /* STMicroelectronics ST19 8 bit mc */ \
140 _ (VAX, 75) /* Digital VAX */ \
141 _ (CRIS, 76) /* Axis 32-bit embedded proc. */ \
142 _ (JAVELIN, 77) /* Infineon 32-bit embedded proc. */ \
143 _ (FIREPATH, 78) /* Element 14 64-bit DSP Processor */ \
144 _ (ZSP, 79) /* LSI Logic 16-bit DSP Processor */ \
145 _ (MMIX, 80) /* Knuth's 64-bit processor */ \
146 _ (HUANY, 81) /* Harvard machine-independent */ \
147 _ (PRISM, 82) /* SiTera Prism */ \
148 _ (AVR, 83) /* Atmel AVR 8-bit microcontroller */ \
149 _ (FR30, 84) /* Fujitsu FR30 */ \
150 _ (D10V, 85) /* Mitsubishi D10V */ \
151 _ (D30V, 86) /* Mitsubishi D30V */ \
152 _ (V850, 87) /* NEC v850 */ \
153 _ (M32R, 88) /* Mitsubishi M32R */ \
154 _ (MN10300, 89) /* Matsushita MN10300 */ \
155 _ (MN10200, 90) /* Matsushita MN10200 */ \
156 _ (PJ, 91) /* picoJava */ \
157 _ (OPENRISC, 92) /* OpenRISC 32-bit processor */ \
158 _ (ARC_A5, 93) /* ARC Cores Tangent-A5 */ \
159 _ (XTENSA, 94) /* Tensilica Xtensa Architecture */ \
162 #define _(f) ELF_##f,
165 foreach_elf_file_class
170 foreach_elf_data_encoding
172 } elf_data_encoding_t;
176 #define _(f,i) ELF_##f = i,
183 foreach_elf_file_type
189 #define _(f,i) ELF_ARCH_##f = i,
190 foreach_elf_architecture
192 } elf_architecture_t;
198 elf_file_class_t file_class : 8;
199 elf_data_encoding_t data_encoding : 8;
200 u8 file_version_ident;
206 elf_file_type_t file_type : 16;
207 elf_architecture_t architecture : 16;
210 } elf_first_header_t;
212 /* 32/64 bit file header following basic file header. */
213 #define foreach_elf32_file_header \
214 _ (u32, entry_point) \
215 _ (u32, segment_header_file_offset) \
216 _ (u32, section_header_file_offset) \
218 _ (u16, n_bytes_this_header) \
219 _ (u16, segment_header_size) \
220 _ (u16, segment_header_count) \
221 _ (u16, section_header_size) \
222 _ (u16, section_header_count) \
223 _ (u16, section_header_string_table_index)
225 #define foreach_elf64_file_header \
226 _ (u64, entry_point) \
227 _ (u64, segment_header_file_offset) \
228 _ (u64, section_header_file_offset) \
230 _ (u16, n_bytes_this_header) \
231 _ (u16, segment_header_size) \
232 _ (u16, segment_header_count) \
233 _ (u16, section_header_size) \
234 _ (u16, section_header_count) \
235 _ (u16, section_header_string_table_index)
237 /* Section header. */
238 #define foreach_elf32_section_header \
242 _ (u32, exec_address) \
243 _ (u32, file_offset) \
246 _ (u32, additional_info) \
250 #define foreach_elf64_section_header \
254 _ (u64, exec_address) \
255 _ (u64, file_offset) \
258 _ (u32, additional_info) \
262 /* Program segment header. */
263 #define foreach_elf32_segment_header \
265 _ (u32, file_offset) \
266 _ (u32, virtual_address) \
267 _ (u32, physical_address) \
269 _ (u32, memory_size) \
273 #define foreach_elf64_segment_header \
276 _ (u64, file_offset) \
277 _ (u64, virtual_address) \
278 _ (u64, physical_address) \
280 _ (u64, memory_size) \
284 #define foreach_elf32_symbol_header \
288 /* binding upper 4 bits; type lower 4 bits */ \
289 _ (u8, binding_and_type) \
291 _ (u16, section_index)
293 #define foreach_elf64_symbol_header \
295 _ (u8, binding_and_type) \
297 _ (u16, section_index) \
304 foreach_elf32_file_header
305 } elf32_file_header_t;
308 foreach_elf64_file_header
309 } elf64_file_header_t;
312 foreach_elf32_section_header
313 } elf32_section_header_t;
316 foreach_elf64_section_header
317 } elf64_section_header_t;
320 foreach_elf32_segment_header
321 } elf32_segment_header_t;
324 foreach_elf64_segment_header
325 } elf64_segment_header_t;
328 foreach_elf32_symbol_header
332 foreach_elf64_symbol_header
336 /* Special section names. */
337 #define foreach_elf_symbol_reserved_section_index \
338 _ (ABSOLUTE, 0xfff1) /* Associated symbol is absolute */ \
339 _ (COMMON, 0xfff2) /* Associated symbol is common */ \
340 _ (XINDEX, 0xffff) /* Index is in extra table. */
342 #define ELF_SYMBOL_SECTION_RESERVED_LO 0xff00
343 #define ELF_SYMBOL_SECTION_RESERVED_HI 0xffff
344 #define ELF_SYMBOL_SECTION_ARCH_SPECIFIC_LO 0xff00
345 #define ELF_SYMBOL_SECTION_ARCH_SPECIFIC_HI 0xff1f
346 #define ELF_SYMBOL_SECTION_OS_SPECIFIC_LO 0xff20
347 #define ELF_SYMBOL_SECTION_OS_SPECIFIC_HI 0xff3f
350 #define foreach_elf_section_type \
352 _ (PROGRAM_DATA, 1) \
353 _ (SYMBOL_TABLE, 2) \
354 _ (STRING_TABLE, 3) \
355 _ (RELOCATION_ADD, 4) \
356 _ (SYMBOL_TABLE_HASH, 5) \
357 _ (DYNAMIC, 6) /* Dynamic linking information */ \
358 _ (NOTE, 7) /* Notes */ \
359 _ (NO_BITS, 8) /* Program space with no data (bss) */ \
360 _ (RELOCATION, 9) /* Relocation entries, no addends */ \
361 _ (DYNAMIC_SYMBOL_TABLE, 11) /* Dynamic linker symbol table */ \
362 _ (INIT_ARRAY, 14) /* Array of constructors */ \
363 _ (FINI_ARRAY, 15) /* Array of destructors */ \
364 _ (PREINIT_ARRAY, 16) /* Array of pre-constructors */ \
365 _ (GROUP, 17) /* Section group */ \
366 _ (SYMTAB_SHNDX, 18) /* Extended section indices */ \
367 _ (OS_SPECIFIC_LO, 0x60000000) /* Start OS-specific */ \
368 _ (GNU_LIBLIST, 0x6ffffff7) /* Prelink library list */ \
369 _ (CHECKSUM, 0x6ffffff8) /* Checksum for DSO content. */ \
370 _ (SUNW_MOVE, 0x6ffffffa) \
371 _ (SUNW_COMDAT, 0x6ffffffb) \
372 _ (SUNW_SYMINFO, 0x6ffffffc) \
373 _ (GNU_VERDEF, 0x6ffffffd) /* Version definition section. */ \
374 _ (GNU_VERNEED, 0x6ffffffe) /* Version needs section. */ \
375 _ (GNU_VERSYM, 0x6fffffff) /* Version symbol table. */ \
376 _ (ARCH_SPECIFIC_LO, 0x70000000) /* Start of processor-specific */ \
377 _ (ARCH_SPECIFIC_HI, 0x7fffffff) /* End of processor-specific */ \
378 _ (APP_SPECIFIC_LO, 0x80000000) /* Start of application-specific */ \
379 _ (APP_SPECIFIC_HI, 0x8fffffff) /* End of application-specific */
382 #define foreach_elf_section_flag \
387 _ (STRING_TABLE, 5) \
389 _ (PRESERVE_LINK_ORDER, 7) \
390 _ (OS_NON_CONFORMING, 8) \
393 _ (OS_SPECIFIC_LO, 20) \
394 _ (OS_SPECIFIC_HI, 27) \
395 _ (ARCH_SPECIFIC_LO, 28) \
396 _ (ARCH_SPECIFIC_HI, 31)
399 #define _(f,i) ELF_SECTION_##f = i,
400 foreach_elf_section_type
402 ELF_SECTION_OS_SPECIFIC_HI = 0x6fffffff,
403 } elf_section_type_t;
406 #define _(f,i) ELF_SECTION_FLAG_BIT_##f = i,
407 foreach_elf_section_flag
409 } elf_section_flag_bit_t;
412 #define _(f,i) ELF_SECTION_FLAG_##f = 1 << ELF_SECTION_FLAG_BIT_##f,
413 foreach_elf_section_flag
415 } elf_section_flag_t;
417 /* Symbol bindings (upper 4 bits of binding_and_type). */
418 #define foreach_elf_symbol_binding \
419 _ (LOCAL, 0) /* Local symbol */ \
420 _ (GLOBAL, 1) /* Global symbol */ \
421 _ (WEAK, 2) /* Weak symbol */ \
422 _ (OS_SPECIFIC_LO, 10) /* Start of OS-specific */ \
423 _ (OS_SPECIFIC_HI, 12) /* End of OS-specific */ \
424 _ (ARCH_SPECIFIC_LO, 13) /* Start of processor-specific */ \
425 _ (ARCH_SPECIFIC_HI, 15) /* End of processor-specific */
427 /* Symbol types (lower 4 bits of binding_and_type). */
428 #define foreach_elf_symbol_type \
430 _ (DATA, 1) /* Symbol is a data object */ \
431 _ (CODE, 2) /* Symbol is a code object */ \
432 _ (SECTION, 3) /* Symbol associated with a section */ \
433 _ (FILE, 4) /* Symbol's name is file name */ \
434 _ (COMMON, 5) /* Symbol is a common data object */ \
435 _ (TLS, 6) /* Symbol is thread-local data */ \
436 _ (OS_SPECIFIC_LO, 10) /* Start of OS-specific */ \
437 _ (OS_SPECIFIC_HI, 12) /* End of OS-specific */ \
438 _ (ARCH_SPECIFIC_LO, 13) /* Start of processor-specific */ \
439 _ (ARCH_SPECIFIC_HI, 15) /* End of processor-specific */
441 /* Symbol visibility. */
442 #define foreach_elf_symbol_visibility \
443 _ (DEFAULT, 0) /* Default symbol visibility rules */ \
444 _ (INTERNAL, 1) /* Processor specific hidden class */ \
445 _ (HIDDEN, 2) /* Unavailable in other modules */ \
446 _ (PROTECTED, 3) /* Not preemptible, not exported */
448 /* The syminfo section if available contains additional
449 information about every dynamic symbol. */
455 /* Possible values for bound_to. */
456 #define foreach_elf_symbol_info_bound_to \
457 _ (SELF, 0xffff) /* Symbol bound to self */ \
458 _ (PARENT, 0xfffe) /* Symbol bound to parent */ \
459 _ (RESERVED_LO, 0xff00) \
460 _ (RESERVED_HI, 0xffff)
462 /* Symbol info flags. */
463 #define foreach_elf_symbol_info_flags \
464 _ (DIRECT) /* Direct bound symbol */ \
465 _ (PASS_THRU) /* Pass-thru symbol for translator */ \
466 _ (COPY) /* Symbol is a copy-reloc */ \
467 _ (LAZY_LOAD) /* Symbol bound to object to be lazy loaded */
469 /* Relocation table entry with/without addend. */
472 u32 symbol_and_type; /* high 24 symbol, low 8 type. */
474 } elf32_relocation_t;
478 u64 symbol_and_type; /* high 32 symbol, low 32 type. */
480 } elf64_relocation_t;
486 } elf_relocation_with_addend_t;
488 #define elf_relocation_next(r,type) \
489 ((void *) ((r) + 1) \
490 + ((type) == ELF_SECTION_RELOCATION_ADD ? sizeof ((r)->addend[0]) : 0))
493 #define foreach_elf_segment_type \
495 _ (LOAD, 1) /* Loadable program segment */ \
496 _ (DYNAMIC, 2) /* Dynamic linking information */ \
497 _ (INTERP, 3) /* Program interpreter */ \
498 _ (NOTE, 4) /* Auxiliary information */ \
499 _ (SEGMENT_TABLE, 6) /* Entry for header table itself */ \
500 _ (TLS, 7) /* Thread-local storage segment */ \
501 _ (OS_SPECIFIC_LO, 0x60000000) /* Start of OS-specific */ \
502 _ (GNU_EH_FRAME, 0x6474e550) /* GCC .eh_frame_hdr segment */ \
503 _ (GNU_STACK, 0x6474e551) /* Indicates stack executability */ \
504 _ (GNU_RELRO, 0x6474e552) /* Read-only after relocation */ \
505 _ (SUNW_BSS, 0x6ffffffa) /* Sun specific BSS */ \
506 _ (SUNW_STACK, 0x6ffffffb) /* Sun specific stack */ \
507 _ (OS_SPECIFIC_HI, 0x6fffffff) /* End of OS-specific */ \
508 _ (ARCH_SPECIFIC_LO, 0x70000000) /* Start of processor-specific */ \
509 _ (ARCH_SPECIFIC_HI, 0x7fffffff) /* End of processor-specific */
512 #define foreach_elf_segment_flag \
516 _ (OS_SPECIFIC_LO, 20) \
517 _ (OS_SPECIFIC_HI, 27) \
518 _ (ARCH_SPECIFIC_LO, 28) \
519 _ (ARCH_SPECIFIC_HI, 31)
522 #define _(f,i) ELF_SEGMENT_##f = i,
523 foreach_elf_segment_type
525 } elf_segment_type_t;
528 #define _(f,i) ELF_SEGMENT_FLAG_BIT_##f = i,
529 foreach_elf_segment_flag
531 } elf_segment_flag_bit_t;
534 #define _(f,i) ELF_SEGMENT_FLAG_##f = 1 << ELF_SEGMENT_FLAG_BIT_##f,
535 foreach_elf_segment_flag
537 } elf_segment_flag_t;
539 #define foreach_elf32_dynamic_entry_header \
543 #define foreach_elf64_dynamic_entry_header \
550 foreach_elf32_dynamic_entry_header
551 } elf32_dynamic_entry_t;
554 foreach_elf64_dynamic_entry_header
555 } elf64_dynamic_entry_t;
559 #define foreach_elf_dynamic_entry_type \
560 _ (END, 0) /* Marks end of dynamic section */ \
561 _ (NEEDED_LIBRARY, 1) /* Name of needed library */ \
562 _ (PLT_RELOCATION_SIZE, 2) /* Size in bytes of PLT relocs */ \
563 _ (PLT_GOT, 3) /* Processor defined value */ \
564 _ (SYMBOL_HASH, 4) /* Address of symbol hash table */ \
565 _ (STRING_TABLE, 5) /* Address of string table */ \
566 _ (SYMBOL_TABLE, 6) /* Address of symbol table */ \
567 _ (RELA_ADDRESS, 7) /* Address of Rela relocs */ \
568 _ (RELA_SIZE, 8) /* Total size of Rela relocs */ \
569 _ (RELA_ENTRY_SIZE, 9) /* Size of one Rela reloc */ \
570 _ (STRING_TABLE_SIZE, 10) /* Size of string table */ \
571 _ (SYMBOL_TABLE_ENTRY_SIZE, 11) /* Size of one symbol table entry */ \
572 _ (INIT_FUNCTION, 12) /* Address of init function */ \
573 _ (FINI_FUNCTION, 13) /* Address of termination function */ \
574 _ (SONAME, 14) /* Name of shared object */ \
575 _ (RPATH, 15) /* Library search path (deprecated) */ \
576 _ (SYMBOLIC, 16) /* Start symbol search here */ \
577 _ (REL, 17) /* Address of Rel relocs */ \
578 _ (RELSZ, 18) /* Total size of Rel relocs */ \
579 _ (RELENT, 19) /* Size of one Rel reloc */ \
580 _ (PLT_RELOCATION_TYPE, 20) /* Type of reloc in PLT */ \
581 _ (DEBUG, 21) /* For debugging; unspecified */ \
582 _ (TEXTREL, 22) /* Reloc might modify .text */ \
583 _ (PLT_RELOCATION_ADDRESS, 23) /* Address of PLT relocs */ \
584 _ (BIND_NOW, 24) /* Process relocations of object */ \
585 _ (INIT_ARRAY, 25) /* Array with addresses of init fct */ \
586 _ (FINI_ARRAY, 26) /* Array with addresses of fini fct */ \
587 _ (INIT_ARRAYSZ, 27) /* Size in bytes of DT_INIT_ARRAY */ \
588 _ (FINI_ARRAYSZ, 28) /* Size in bytes of DT_FINI_ARRAY */ \
589 _ (RUN_PATH, 29) /* Library search path */ \
590 _ (FLAGS, 30) /* Flags for object being loaded */ \
591 _ (ENCODING, 31) /* Start of encoded range */ \
592 _ (PREINIT_ARRAY, 32) /* Array with addresses of fns */ \
593 _ (PREINIT_ARRAY_SIZE, 33) /* Size of PREINIT_ARRAY in bytes. */ \
594 _ (GNU_PRELINKED, 0x6ffffdf5) /* Prelinking timestamp */ \
595 _ (GNU_CONFLICTSZ, 0x6ffffdf6) /* Size of conflict section */ \
596 _ (GNU_LIBLISTSZ, 0x6ffffdf7) /* Size of library list */ \
597 _ (CHECKSUM, 0x6ffffdf8) \
598 _ (PLTPADSZ, 0x6ffffdf9) \
599 _ (MOVEENT, 0x6ffffdfa) \
600 _ (MOVESZ, 0x6ffffdfb) \
601 _ (FEATURE_1, 0x6ffffdfc) /* Feature selection (DTF_*). */ \
602 _ (POSFLAG_1, 0x6ffffdfd) /* Flags for following entries. */ \
603 _ (SYMINSZ, 0x6ffffdfe) /* Size of syminfo table (in bytes) */ \
604 _ (SYMINENT, 0x6ffffdff) /* Entry size of syminfo */ \
605 _ (GNU_HASH, 0x6ffffef5) \
606 _ (GNU_CONFLICT, 0x6ffffef8) /* Start of conflict section */ \
607 _ (GNU_LIBLIST, 0x6ffffef9) /* Library list */ \
608 _ (CONFIG, 0x6ffffefa) /* Configuration information. */ \
609 _ (DEPAUDIT, 0x6ffffefb) /* Dependency auditing. */ \
610 _ (AUDIT, 0x6ffffefc) /* Object auditing. */ \
611 _ (PLTPAD, 0x6ffffefd) /* PLT padding. */ \
612 _ (MOVETAB, 0x6ffffefe) /* Move table. */ \
613 _ (SYMINFO, 0x6ffffeff) /* Syminfo table. */ \
614 _ (VERSYM, 0x6ffffff0) \
615 _ (RELACOUNT, 0x6ffffff9) \
616 _ (RELCOUNT, 0x6ffffffa) \
617 _ (FLAGS_1, 0x6ffffffb) /* State flags, see DF_1_* below. */ \
618 _ (VERSION_DEF, 0x6ffffffc) /* Address of version definition table */ \
619 _ (VERSION_DEF_COUNT, 0x6ffffffd) /* Number of version definitions */ \
620 _ (VERSION_NEED, 0x6ffffffe) /* Address of table with needed versions */ \
621 _ (VERSION_NEED_COUNT, 0x6fffffff) /* Number of needed versions */ \
622 _ (AUXILIARY, 0x7ffffffd) /* Shared object to load before self */ \
623 _ (FILTER, 0x7fffffff) /* Shared object to get values from */
626 #define _(f,n) ELF_DYNAMIC_ENTRY_##f = (n),
627 foreach_elf_dynamic_entry_type
629 } elf_dynamic_entry_type_t;
631 /* Values of `d_un.d_val' in the DT_FLAGS entry. */
632 #define ELF_DYNAMIC_FLAGS_ORIGIN (1 << 0) /* Object may use DF_ORIGIN */
633 #define ELF_DYNAMIC_FLAGS_SYMBOLIC (1 << 1) /* Symbol resolutions starts here */
634 #define ELF_DYNAMIC_FLAGS_TEXT_RELOCATIONS (1 << 2) /* Object contains text relocations */
635 #define ELF_DYNAMIC_FLAGS_BIND_NOW (1 << 3) /* No lazy binding for this object */
636 #define ELF_DYNAMIC_FLAGS_STATIC_TLS (1 << 4) /* Module uses the static TLS model */
638 /* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1
639 entry in the dynamic section. */
640 #define DF_1_NOW 0x00000001 /* Set RTLD_NOW for this object. */
641 #define DF_1_GLOBAL 0x00000002 /* Set RTLD_GLOBAL for this object. */
642 #define DF_1_GROUP 0x00000004 /* Set RTLD_GROUP for this object. */
643 #define DF_1_NODELETE 0x00000008 /* Set RTLD_NODELETE for this object.*/
644 #define DF_1_LOADFLTR 0x00000010 /* Trigger filtee loading at runtime.*/
645 #define DF_1_INITFIRST 0x00000020 /* Set RTLD_INITFIRST for this object*/
646 #define DF_1_NOOPEN 0x00000040 /* Set RTLD_NOOPEN for this object. */
647 #define DF_1_ORIGIN 0x00000080 /* $ORIGIN must be handled. */
648 #define DF_1_DIRECT 0x00000100 /* Direct binding enabled. */
649 #define DF_1_TRANS 0x00000200
650 #define DF_1_INTERPOSE 0x00000400 /* Object is used to interpose. */
651 #define DF_1_NODEFLIB 0x00000800 /* Ignore default lib search path. */
652 #define DF_1_NODUMP 0x00001000 /* Object can't be dldump'ed. */
653 #define DF_1_CONFALT 0x00002000 /* Configuration alternative created.*/
654 #define DF_1_ENDFILTEE 0x00004000 /* Filtee terminates filters search. */
655 #define DF_1_DISPRELDNE 0x00008000 /* Disp reloc applied at build time. */
656 #define DF_1_DISPRELPND 0x00010000 /* Disp reloc applied at run-time. */
658 /* Flags for the feature selection in DT_FEATURE_1. */
659 #define DTF_1_PARINIT 0x00000001
660 #define DTF_1_CONFEXP 0x00000002
662 /* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry. */
663 #define DF_P1_LAZYLOAD 0x00000001 /* Lazyload following object. */
664 #define DF_P1_GROUPPERM 0x00000002 /* Symbols from next object are not
665 generally available. */
667 /* Version definition sections. */
676 u32 byte_offset_next_version_definition;
677 } elf_dynamic_version_definition_t;
681 u32 next_offset; /* byte offset of ver def aux next entry */
682 } elf_dynamic_version_definition_aux_t;
684 /* Version definition flags. */
685 #define ELF_DYNAMIC_VERSION_FILE (1 << 0) /* Version definition of file itself */
686 #define ELF_DYNAMIC_VERSION_WEAK (1 << 1) /* Weak version identifier */
688 /* Version symbol index. */
689 #define ELF_DYNAMIC_VERSYM_LOCAL 0 /* Symbol is local. */
690 #define ELF_DYNAMIC_VERSYM_GLOBAL 1 /* Symbol is global. */
691 #define ELF_DYNAMIC_VERSYM_RESERVED_LO 0xff00 /* Beginning of reserved entries. */
692 #define ELF_DYNAMIC_VERSYM_ELIMINATE 0xff01 /* Symbol is to be eliminated. */
694 /* Version dependency section. */
695 #define foreach_elf_dynamic_version_need_field \
698 _ (u32, file_name_offset) \
699 _ (u32, first_aux_offset) \
702 #define foreach_elf_dynamic_version_need_aux_field \
705 _ (u16, versym_index) \
712 foreach_elf_dynamic_version_need_field
714 } elf_dynamic_version_need_t;
719 foreach_elf_dynamic_version_need_aux_field
721 } elf_dynamic_version_need_aux_t;
725 elf_dynamic_version_need_t need;
726 elf_dynamic_version_need_aux_t aux;
727 } elf_dynamic_version_need_union_t;
729 /* Note section contents. Each entry in the note section begins with
730 a header of a fixed form. */
739 /* Known names of notes. */
741 /* Solaris entries in the note section have this name. */
742 #define ELF_NOTE_SOLARIS "SUNW Solaris"
744 /* Note entries for GNU systems have this name. */
745 #define ELF_NOTE_GNU "GNU"
748 /* Defined types of notes for Solaris. */
750 /* Value of descriptor (one word) is desired pagesize for the binary. */
751 #define ELF_NOTE_PAGESIZE_HINT 1
754 /* Defined note types for GNU systems. */
756 /* ABI information. The descriptor consists of words:
757 word 0: OS descriptor
758 word 1: major version of the ABI
759 word 2: minor version of the ABI
760 word 3: subminor version of the ABI
763 #define ELF_NOTE_ABI 1
766 /* Known OSes. These value can appear in word 0 of an ELF_NOTE_ABI
767 note section entry. */
768 #define ELF_NOTE_OS_LINUX 0
769 #define ELF_NOTE_OS_GNU 1
770 #define ELF_NOTE_OS_SOLARIS2 2
771 #define ELF_NOTE_OS_FREEBSD 3
773 /* AMD x86-64 relocations. */
774 #define foreach_elf_x86_64_relocation_type \
775 _ (NONE, 0) /* No reloc */ \
776 _ (DIRECT_64, 1) /* Direct 64 bit */ \
777 _ (PC_REL_I32, 2) /* PC relative 32 bit signed */ \
778 _ (GOT_REL_32, 3) /* 32 bit GOT entry */ \
779 _ (PLT_REL_32, 4) /* 32 bit PLT address */ \
780 _ (COPY, 5) /* Copy symbol at runtime */ \
781 _ (CREATE_GOT, 6) /* Create GOT entry */ \
782 _ (CREATE_PLT, 7) /* Create PLT entry */ \
783 _ (RELATIVE, 8) /* Adjust by program base */ \
784 _ (PC_REL_I32_GOT, 9) /* 32 bit PC relative offset to GOT */ \
785 _ (DIRECT_U32, 10) /* Direct 32 bit zero extended */ \
786 _ (DIRECT_I32, 11) /* Direct 32 bit sign extended */ \
787 _ (DIRECT_U16, 12) /* Direct 16 bit zero extended */ \
788 _ (PC_REL_I16, 13) /* 16 bit sign extended pc relative */ \
789 _ (DIRECT_I8, 14) /* Direct 8 bit sign extended */ \
790 _ (PC_REL_I8, 15) /* 8 bit sign extended pc relative */ \
791 _ (DTPMOD64, 16) /* ID of module containing symbol */ \
792 _ (DTPOFF64, 17) /* Offset in module's TLS block */ \
793 _ (TPOFF64, 18) /* Offset in initial TLS block */ \
794 _ (TLSGD, 19) /* 32 bit signed PC relative offset to two GOT entries for GD symbol */ \
795 _ (TLSLD, 20) /* 32 bit signed PC relative offset to two GOT entries for LD symbol */ \
796 _ (DTPOFF32, 21) /* Offset in TLS block */ \
797 _ (GOTTPOFF, 22) /* 32 bit signed PC relative offset to GOT entry for IE symbol */ \
798 _ (TPOFF32, 23) /* Offset in initial TLS, block) */
801 elf64_symbol_t * symbols;
807 uword * symbol_by_name;
808 } elf_symbol_table_t;
811 elf_symbol_table_free (elf_symbol_table_t * s)
813 vec_free (s->symbols);
814 hash_free (s->symbol_by_name);
818 elf_symbol_name (elf_symbol_table_t * t, elf64_symbol_t * sym)
819 { return vec_elt_at_index (t->string_table, sym->name); }
822 elf_relocation_with_addend_t * relocations;
825 } elf_relocation_table_t;
828 elf_relocation_table_free (elf_relocation_table_t * r)
830 vec_free (r->relocations);
834 elf64_section_header_t header;
838 /* Index of segments containing this section. */
839 uword * segment_index_bitmap;
841 /* Aligned size (included padding not included in
842 header.file_size). */
845 i64 exec_address_change;
851 elf64_segment_header_t header;
853 /* Sections contained in this segment. */
854 uword * section_index_bitmap;
868 elf_first_header_t first_header;
870 elf64_file_header_t file_header;
872 elf_segment_t * segments;
874 elf_section_t * sections;
876 uword * section_by_name;
877 uword * section_by_start_address;
879 elf_symbol_table_t * symbol_tables;
880 elf_relocation_table_t * relocation_tables;
884 elf64_dynamic_entry_t * dynamic_entries;
885 u8 * dynamic_string_table;
886 u32 dynamic_string_table_section_index;
887 u32 dynamic_symbol_table_section_index;
888 u32 dynamic_symbol_table_index;
889 u32 dynamic_section_index;
891 u32 versym_section_index;
892 elf_dynamic_version_need_union_t * verneed;
893 u32 verneed_section_index;
897 elf_main_init (elf_main_t * em)
899 memset (em, 0, sizeof (em[0]));
903 elf_main_free (elf_main_t * em)
907 for (i = 0; i < vec_len (em->segments); i++)
908 vec_free (em->segments[i].contents);
909 vec_free (em->segments);
911 for (i = 0; i < vec_len (em->sections); i++)
912 vec_free (em->sections[i].contents);
913 vec_free (em->sections);
915 hash_free (em->section_by_name);
916 for (i = 0; i < vec_len (em->symbol_tables); i++)
917 elf_symbol_table_free (em->symbol_tables + i);
918 for (i = 0; i < vec_len (em->relocation_tables); i++)
919 elf_relocation_table_free (em->relocation_tables + i);
921 vec_free (em->dynamic_entries);
922 vec_free (em->interpreter);
926 elf_get_segment_contents (elf_main_t * em,
930 elf_segment_t * g = vec_elt_at_index (em->segments, segment_index);
932 vec_add (g->contents, data + g->header.file_offset, g->header.memory_size);
936 elf_get_section_contents (elf_main_t * em,
943 s = vec_elt_at_index (em->sections, section_index);
946 if (vec_len (s->contents) > 0)
948 /* Make vector copy of contents with given element size. */
949 result = _vec_resize (result,
950 vec_len (s->contents) / elt_size,
951 vec_len (s->contents),
952 /* header_bytes */ 0,
954 clib_memcpy (result, s->contents, vec_len (s->contents));
961 elf_set_section_contents (elf_main_t * em,
964 uword n_content_bytes)
968 s = vec_elt_at_index (em->sections, section_index);
969 vec_free (s->contents);
970 vec_add (s->contents, new_contents, n_content_bytes);
974 elf_section_name (elf_main_t * em, elf_section_t * s)
976 elf_section_t * es = vec_elt_at_index (em->sections, em->file_header.section_header_string_table_index);
977 return vec_elt_at_index (es->contents, s->header.name);
981 elf_swap_u8 (elf_main_t * em, u8 x)
985 elf_swap_u16 (elf_main_t * em, u16 x)
986 { return em->need_byte_swap ? clib_byte_swap_u16 (x) : x; }
989 elf_swap_u32 (elf_main_t * em, u32 x)
990 { return em->need_byte_swap ? clib_byte_swap_u32 (x) : x; }
993 elf_swap_u64 (elf_main_t * em, u64 x)
994 { return em->need_byte_swap ? clib_byte_swap_u64 (x) : x; }
996 #define FORMAT_ELF_MAIN_SYMBOLS (1 << 0)
997 #define FORMAT_ELF_MAIN_RELOCATIONS (1 << 1)
998 #define FORMAT_ELF_MAIN_DYNAMIC (1 << 2)
1000 format_function_t format_elf_main;
1001 format_function_t format_elf_symbol;
1003 clib_error_t * elf_read_file (elf_main_t * em, char * file_name);
1004 clib_error_t * elf_write_file (elf_main_t * em, char * file_name);
1005 clib_error_t * elf_delete_named_section (elf_main_t * em, char * section_name);
1006 clib_error_t * elf_parse (elf_main_t * em, void * data, uword data_bytes);
1007 void elf_parse_symbols (elf_main_t * em);
1010 elf_get_section_by_name (elf_main_t * em, char * section_name, elf_section_t ** result);
1012 elf_get_section_by_start_address (elf_main_t * em, uword start_address, elf_section_t ** result);
1015 elf_create_section_with_contents (elf_main_t * em,
1016 char * section_name,
1017 elf64_section_header_t * header,
1019 uword n_content_bytes);
1020 uword elf_delete_segment_with_type (elf_main_t * em, elf_segment_type_t segment_type);
1021 void elf_set_dynamic_entries (elf_main_t * em);
1023 #endif /* included_clib_elf_h */