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 embedded 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,
166 foreach_elf_file_class ELF_N_FILE_CLASS,
171 foreach_elf_data_encoding ELF_N_DATA_ENCODING,
172 } elf_data_encoding_t;
176 #define _(f,i) ELF_##f = i,
185 foreach_elf_file_type
192 #define _(f,i) ELF_ARCH_##f = i,
193 foreach_elf_architecture
195 } elf_architecture_t;
202 elf_file_class_t file_class:8;
203 elf_data_encoding_t data_encoding:8;
204 u8 file_version_ident;
210 elf_file_type_t file_type:16;
211 elf_architecture_t architecture:16;
214 } elf_first_header_t;
216 /* 32/64 bit file header following basic file header. */
217 #define foreach_elf32_file_header \
218 _ (u32, entry_point) \
219 _ (u32, segment_header_file_offset) \
220 _ (u32, section_header_file_offset) \
222 _ (u16, n_bytes_this_header) \
223 _ (u16, segment_header_size) \
224 _ (u16, segment_header_count) \
225 _ (u16, section_header_size) \
226 _ (u16, section_header_count) \
227 _ (u16, section_header_string_table_index)
229 #define foreach_elf64_file_header \
230 _ (u64, entry_point) \
231 _ (u64, segment_header_file_offset) \
232 _ (u64, section_header_file_offset) \
234 _ (u16, n_bytes_this_header) \
235 _ (u16, segment_header_size) \
236 _ (u16, segment_header_count) \
237 _ (u16, section_header_size) \
238 _ (u16, section_header_count) \
239 _ (u16, section_header_string_table_index)
241 /* Section header. */
242 #define foreach_elf32_section_header \
246 _ (u32, exec_address) \
247 _ (u32, file_offset) \
250 _ (u32, additional_info) \
254 #define foreach_elf64_section_header \
258 _ (u64, exec_address) \
259 _ (u64, file_offset) \
262 _ (u32, additional_info) \
266 /* Program segment header. */
267 #define foreach_elf32_segment_header \
269 _ (u32, file_offset) \
270 _ (u32, virtual_address) \
271 _ (u32, physical_address) \
273 _ (u32, memory_size) \
277 #define foreach_elf64_segment_header \
280 _ (u64, file_offset) \
281 _ (u64, virtual_address) \
282 _ (u64, physical_address) \
284 _ (u64, memory_size) \
288 #define foreach_elf32_symbol_header \
292 /* binding upper 4 bits; type lower 4 bits */ \
293 _ (u8, binding_and_type) \
295 _ (u16, section_index)
297 #define foreach_elf64_symbol_header \
299 _ (u8, binding_and_type) \
301 _ (u16, section_index) \
309 foreach_elf32_file_header} elf32_file_header_t;
313 foreach_elf64_file_header} elf64_file_header_t;
317 foreach_elf32_section_header} elf32_section_header_t;
321 foreach_elf64_section_header} elf64_section_header_t;
325 foreach_elf32_segment_header} elf32_segment_header_t;
329 foreach_elf64_segment_header} elf64_segment_header_t;
333 foreach_elf32_symbol_header} elf32_symbol_t;
337 foreach_elf64_symbol_header} elf64_symbol_t;
340 /* Special section names. */
341 #define foreach_elf_symbol_reserved_section_index \
342 _ (ABSOLUTE, 0xfff1) /* Associated symbol is absolute */ \
343 _ (COMMON, 0xfff2) /* Associated symbol is common */ \
344 _ (XINDEX, 0xffff) /* Index is in extra table. */
346 #define ELF_SYMBOL_SECTION_RESERVED_LO 0xff00
347 #define ELF_SYMBOL_SECTION_RESERVED_HI 0xffff
348 #define ELF_SYMBOL_SECTION_ARCH_SPECIFIC_LO 0xff00
349 #define ELF_SYMBOL_SECTION_ARCH_SPECIFIC_HI 0xff1f
350 #define ELF_SYMBOL_SECTION_OS_SPECIFIC_LO 0xff20
351 #define ELF_SYMBOL_SECTION_OS_SPECIFIC_HI 0xff3f
354 #define foreach_elf_section_type \
356 _ (PROGRAM_DATA, 1) \
357 _ (SYMBOL_TABLE, 2) \
358 _ (STRING_TABLE, 3) \
359 _ (RELOCATION_ADD, 4) \
360 _ (SYMBOL_TABLE_HASH, 5) \
361 _ (DYNAMIC, 6) /* Dynamic linking information */ \
362 _ (NOTE, 7) /* Notes */ \
363 _ (NO_BITS, 8) /* Program space with no data (bss) */ \
364 _ (RELOCATION, 9) /* Relocation entries, no addends */ \
365 _ (DYNAMIC_SYMBOL_TABLE, 11) /* Dynamic linker symbol table */ \
366 _ (INIT_ARRAY, 14) /* Array of constructors */ \
367 _ (FINI_ARRAY, 15) /* Array of destructors */ \
368 _ (PREINIT_ARRAY, 16) /* Array of pre-constructors */ \
369 _ (GROUP, 17) /* Section group */ \
370 _ (SYMTAB_SHNDX, 18) /* Extended section indices */ \
371 _ (OS_SPECIFIC_LO, 0x60000000) /* Start OS-specific */ \
372 _ (GNU_LIBLIST, 0x6ffffff7) /* Prelink library list */ \
373 _ (CHECKSUM, 0x6ffffff8) /* Checksum for DSO content. */ \
374 _ (SUNW_MOVE, 0x6ffffffa) \
375 _ (SUNW_COMDAT, 0x6ffffffb) \
376 _ (SUNW_SYMINFO, 0x6ffffffc) \
377 _ (GNU_VERDEF, 0x6ffffffd) /* Version definition section. */ \
378 _ (GNU_VERNEED, 0x6ffffffe) /* Version needs section. */ \
379 _ (GNU_VERSYM, 0x6fffffff) /* Version symbol table. */ \
380 _ (ARCH_SPECIFIC_LO, 0x70000000) /* Start of processor-specific */ \
381 _ (ARCH_SPECIFIC_HI, 0x7fffffff) /* End of processor-specific */ \
382 _ (APP_SPECIFIC_LO, 0x80000000) /* Start of application-specific */ \
383 _ (APP_SPECIFIC_HI, 0x8fffffff) /* End of application-specific */
386 #define foreach_elf_section_flag \
391 _ (STRING_TABLE, 5) \
393 _ (PRESERVE_LINK_ORDER, 7) \
394 _ (OS_NON_CONFORMING, 8) \
397 _ (OS_SPECIFIC_LO, 20) \
398 _ (OS_SPECIFIC_HI, 27) \
399 _ (ARCH_SPECIFIC_LO, 28) \
400 _ (ARCH_SPECIFIC_HI, 31)
404 #define _(f,i) ELF_SECTION_##f = i,
405 foreach_elf_section_type
407 ELF_SECTION_OS_SPECIFIC_HI = 0x6fffffff,
408 } elf_section_type_t;
412 #define _(f,i) ELF_SECTION_FLAG_BIT_##f = i,
413 foreach_elf_section_flag
415 } elf_section_flag_bit_t;
419 #define _(f,i) ELF_SECTION_FLAG_##f = 1 << ELF_SECTION_FLAG_BIT_##f,
420 foreach_elf_section_flag
422 } elf_section_flag_t;
424 /* Symbol bindings (upper 4 bits of binding_and_type). */
425 #define foreach_elf_symbol_binding \
426 _ (LOCAL, 0) /* Local symbol */ \
427 _ (GLOBAL, 1) /* Global symbol */ \
428 _ (WEAK, 2) /* Weak symbol */ \
429 _ (OS_SPECIFIC_LO, 10) /* Start of OS-specific */ \
430 _ (OS_SPECIFIC_HI, 12) /* End of OS-specific */ \
431 _ (ARCH_SPECIFIC_LO, 13) /* Start of processor-specific */ \
432 _ (ARCH_SPECIFIC_HI, 15) /* End of processor-specific */
434 /* Symbol types (lower 4 bits of binding_and_type). */
435 #define foreach_elf_symbol_type \
437 _ (DATA, 1) /* Symbol is a data object */ \
438 _ (CODE, 2) /* Symbol is a code object */ \
439 _ (SECTION, 3) /* Symbol associated with a section */ \
440 _ (FILE, 4) /* Symbol's name is file name */ \
441 _ (COMMON, 5) /* Symbol is a common data object */ \
442 _ (TLS, 6) /* Symbol is thread-local data */ \
443 _ (OS_SPECIFIC_LO, 10) /* Start of OS-specific */ \
444 _ (OS_SPECIFIC_HI, 12) /* End of OS-specific */ \
445 _ (ARCH_SPECIFIC_LO, 13) /* Start of processor-specific */ \
446 _ (ARCH_SPECIFIC_HI, 15) /* End of processor-specific */
448 /* Symbol visibility. */
449 #define foreach_elf_symbol_visibility \
450 _ (DEFAULT, 0) /* Default symbol visibility rules */ \
451 _ (INTERNAL, 1) /* Processor specific hidden class */ \
452 _ (HIDDEN, 2) /* Unavailable in other modules */ \
453 _ (PROTECTED, 3) /* Not preemptible, not exported */
455 /* The syminfo section if available contains additional
456 information about every dynamic symbol. */
463 /* Possible values for bound_to. */
464 #define foreach_elf_symbol_info_bound_to \
465 _ (SELF, 0xffff) /* Symbol bound to self */ \
466 _ (PARENT, 0xfffe) /* Symbol bound to parent */ \
467 _ (RESERVED_LO, 0xff00) \
468 _ (RESERVED_HI, 0xffff)
470 /* Symbol info flags. */
471 #define foreach_elf_symbol_info_flags \
472 _ (DIRECT) /* Direct bound symbol */ \
473 _ (PASS_THRU) /* Pass-thru symbol for translator */ \
474 _ (COPY) /* Symbol is a copy-reloc */ \
475 _ (LAZY_LOAD) /* Symbol bound to object to be lazy loaded */
477 /* Relocation table entry with/without addend. */
481 u32 symbol_and_type; /* high 24 symbol, low 8 type. */
483 } elf32_relocation_t;
488 u64 symbol_and_type; /* high 32 symbol, low 32 type. */
490 } elf64_relocation_t;
497 } elf_relocation_with_addend_t;
499 #define elf_relocation_next(r,type) \
500 ((void *) ((r) + 1) \
501 + ((type) == ELF_SECTION_RELOCATION_ADD ? sizeof ((r)->addend[0]) : 0))
504 #define foreach_elf_segment_type \
506 _ (LOAD, 1) /* Loadable program segment */ \
507 _ (DYNAMIC, 2) /* Dynamic linking information */ \
508 _ (INTERP, 3) /* Program interpreter */ \
509 _ (NOTE, 4) /* Auxiliary information */ \
510 _ (SEGMENT_TABLE, 6) /* Entry for header table itself */ \
511 _ (TLS, 7) /* Thread-local storage segment */ \
512 _ (OS_SPECIFIC_LO, 0x60000000) /* Start of OS-specific */ \
513 _ (GNU_EH_FRAME, 0x6474e550) /* GCC .eh_frame_hdr segment */ \
514 _ (GNU_STACK, 0x6474e551) /* Indicates stack executability */ \
515 _ (GNU_RELRO, 0x6474e552) /* Read-only after relocation */ \
516 _ (SUNW_BSS, 0x6ffffffa) /* Sun specific BSS */ \
517 _ (SUNW_STACK, 0x6ffffffb) /* Sun specific stack */ \
518 _ (OS_SPECIFIC_HI, 0x6fffffff) /* End of OS-specific */ \
519 _ (ARCH_SPECIFIC_LO, 0x70000000) /* Start of processor-specific */ \
520 _ (ARCH_SPECIFIC_HI, 0x7fffffff) /* End of processor-specific */
523 #define foreach_elf_segment_flag \
527 _ (OS_SPECIFIC_LO, 20) \
528 _ (OS_SPECIFIC_HI, 27) \
529 _ (ARCH_SPECIFIC_LO, 28) \
530 _ (ARCH_SPECIFIC_HI, 31)
534 #define _(f,i) ELF_SEGMENT_##f = i,
535 foreach_elf_segment_type
537 } elf_segment_type_t;
541 #define _(f,i) ELF_SEGMENT_FLAG_BIT_##f = i,
542 foreach_elf_segment_flag
544 } elf_segment_flag_bit_t;
548 #define _(f,i) ELF_SEGMENT_FLAG_##f = 1 << ELF_SEGMENT_FLAG_BIT_##f,
549 foreach_elf_segment_flag
551 } elf_segment_flag_t;
553 #define foreach_elf32_dynamic_entry_header \
557 #define foreach_elf64_dynamic_entry_header \
565 foreach_elf32_dynamic_entry_header} elf32_dynamic_entry_t;
569 foreach_elf64_dynamic_entry_header} elf64_dynamic_entry_t;
573 #define foreach_elf_dynamic_entry_type \
574 _ (END, 0) /* Marks end of dynamic section */ \
575 _ (NEEDED_LIBRARY, 1) /* Name of needed library */ \
576 _ (PLT_RELOCATION_SIZE, 2) /* Size in bytes of PLT relocs */ \
577 _ (PLT_GOT, 3) /* Processor defined value */ \
578 _ (SYMBOL_HASH, 4) /* Address of symbol hash table */ \
579 _ (STRING_TABLE, 5) /* Address of string table */ \
580 _ (SYMBOL_TABLE, 6) /* Address of symbol table */ \
581 _ (RELA_ADDRESS, 7) /* Address of Rela relocs */ \
582 _ (RELA_SIZE, 8) /* Total size of Rela relocs */ \
583 _ (RELA_ENTRY_SIZE, 9) /* Size of one Rela reloc */ \
584 _ (STRING_TABLE_SIZE, 10) /* Size of string table */ \
585 _ (SYMBOL_TABLE_ENTRY_SIZE, 11) /* Size of one symbol table entry */ \
586 _ (INIT_FUNCTION, 12) /* Address of init function */ \
587 _ (FINI_FUNCTION, 13) /* Address of termination function */ \
588 _ (SONAME, 14) /* Name of shared object */ \
589 _ (RPATH, 15) /* Library search path (deprecated) */ \
590 _ (SYMBOLIC, 16) /* Start symbol search here */ \
591 _ (REL, 17) /* Address of Rel relocs */ \
592 _ (RELSZ, 18) /* Total size of Rel relocs */ \
593 _ (RELENT, 19) /* Size of one Rel reloc */ \
594 _ (PLT_RELOCATION_TYPE, 20) /* Type of reloc in PLT */ \
595 _ (DEBUG, 21) /* For debugging; unspecified */ \
596 _ (TEXTREL, 22) /* Reloc might modify .text */ \
597 _ (PLT_RELOCATION_ADDRESS, 23) /* Address of PLT relocs */ \
598 _ (BIND_NOW, 24) /* Process relocations of object */ \
599 _ (INIT_ARRAY, 25) /* Array with addresses of init fct */ \
600 _ (FINI_ARRAY, 26) /* Array with addresses of fini fct */ \
601 _ (INIT_ARRAYSZ, 27) /* Size in bytes of DT_INIT_ARRAY */ \
602 _ (FINI_ARRAYSZ, 28) /* Size in bytes of DT_FINI_ARRAY */ \
603 _ (RUN_PATH, 29) /* Library search path */ \
604 _ (FLAGS, 30) /* Flags for object being loaded */ \
605 _ (ENCODING, 31) /* Start of encoded range */ \
606 _ (PREINIT_ARRAY, 32) /* Array with addresses of fns */ \
607 _ (PREINIT_ARRAY_SIZE, 33) /* Size of PREINIT_ARRAY in bytes. */ \
608 _ (GNU_PRELINKED, 0x6ffffdf5) /* Prelinking timestamp */ \
609 _ (GNU_CONFLICTSZ, 0x6ffffdf6) /* Size of conflict section */ \
610 _ (GNU_LIBLISTSZ, 0x6ffffdf7) /* Size of library list */ \
611 _ (CHECKSUM, 0x6ffffdf8) \
612 _ (PLTPADSZ, 0x6ffffdf9) \
613 _ (MOVEENT, 0x6ffffdfa) \
614 _ (MOVESZ, 0x6ffffdfb) \
615 _ (FEATURE_1, 0x6ffffdfc) /* Feature selection (DTF_*). */ \
616 _ (POSFLAG_1, 0x6ffffdfd) /* Flags for following entries. */ \
617 _ (SYMINSZ, 0x6ffffdfe) /* Size of syminfo table (in bytes) */ \
618 _ (SYMINENT, 0x6ffffdff) /* Entry size of syminfo */ \
619 _ (GNU_HASH, 0x6ffffef5) \
620 _ (GNU_CONFLICT, 0x6ffffef8) /* Start of conflict section */ \
621 _ (GNU_LIBLIST, 0x6ffffef9) /* Library list */ \
622 _ (CONFIG, 0x6ffffefa) /* Configuration information. */ \
623 _ (DEPAUDIT, 0x6ffffefb) /* Dependency auditing. */ \
624 _ (AUDIT, 0x6ffffefc) /* Object auditing. */ \
625 _ (PLTPAD, 0x6ffffefd) /* PLT padding. */ \
626 _ (MOVETAB, 0x6ffffefe) /* Move table. */ \
627 _ (SYMINFO, 0x6ffffeff) /* Syminfo table. */ \
628 _ (VERSYM, 0x6ffffff0) \
629 _ (RELACOUNT, 0x6ffffff9) \
630 _ (RELCOUNT, 0x6ffffffa) \
631 _ (FLAGS_1, 0x6ffffffb) /* State flags, see DF_1_* below. */ \
632 _ (VERSION_DEF, 0x6ffffffc) /* Address of version definition table */ \
633 _ (VERSION_DEF_COUNT, 0x6ffffffd) /* Number of version definitions */ \
634 _ (VERSION_NEED, 0x6ffffffe) /* Address of table with needed versions */ \
635 _ (VERSION_NEED_COUNT, 0x6fffffff) /* Number of needed versions */ \
636 _ (AUXILIARY, 0x7ffffffd) /* Shared object to load before self */ \
637 _ (FILTER, 0x7fffffff) /* Shared object to get values from */
641 #define _(f,n) ELF_DYNAMIC_ENTRY_##f = (n),
642 foreach_elf_dynamic_entry_type
644 } elf_dynamic_entry_type_t;
646 /* Values of `d_un.d_val' in the DT_FLAGS entry. */
647 #define ELF_DYNAMIC_FLAGS_ORIGIN (1 << 0) /* Object may use DF_ORIGIN */
648 #define ELF_DYNAMIC_FLAGS_SYMBOLIC (1 << 1) /* Symbol resolutions starts here */
649 #define ELF_DYNAMIC_FLAGS_TEXT_RELOCATIONS (1 << 2) /* Object contains text relocations */
650 #define ELF_DYNAMIC_FLAGS_BIND_NOW (1 << 3) /* No lazy binding for this object */
651 #define ELF_DYNAMIC_FLAGS_STATIC_TLS (1 << 4) /* Module uses the static TLS model */
653 /* State flags selectable in the `d_un.d_val' element of the DT_FLAGS_1
654 entry in the dynamic section. */
655 #define DF_1_NOW 0x00000001 /* Set RTLD_NOW for this object. */
656 #define DF_1_GLOBAL 0x00000002 /* Set RTLD_GLOBAL for this object. */
657 #define DF_1_GROUP 0x00000004 /* Set RTLD_GROUP for this object. */
658 #define DF_1_NODELETE 0x00000008 /* Set RTLD_NODELETE for this object. */
659 #define DF_1_LOADFLTR 0x00000010 /* Trigger filtee loading at runtime. */
660 #define DF_1_INITFIRST 0x00000020 /* Set RTLD_INITFIRST for this object */
661 #define DF_1_NOOPEN 0x00000040 /* Set RTLD_NOOPEN for this object. */
662 #define DF_1_ORIGIN 0x00000080 /* $ORIGIN must be handled. */
663 #define DF_1_DIRECT 0x00000100 /* Direct binding enabled. */
664 #define DF_1_TRANS 0x00000200
665 #define DF_1_INTERPOSE 0x00000400 /* Object is used to interpose. */
666 #define DF_1_NODEFLIB 0x00000800 /* Ignore default lib search path. */
667 #define DF_1_NODUMP 0x00001000 /* Object can't be dldump'ed. */
668 #define DF_1_CONFALT 0x00002000 /* Configuration alternative created. */
669 #define DF_1_ENDFILTEE 0x00004000 /* Filtee terminates filters search. */
670 #define DF_1_DISPRELDNE 0x00008000 /* Disp reloc applied at build time. */
671 #define DF_1_DISPRELPND 0x00010000 /* Disp reloc applied at run-time. */
673 /* Flags for the feature selection in DT_FEATURE_1. */
674 #define DTF_1_PARINIT 0x00000001
675 #define DTF_1_CONFEXP 0x00000002
677 /* Flags in the DT_POSFLAG_1 entry effecting only the next DT_* entry. */
678 #define DF_P1_LAZYLOAD 0x00000001 /* Lazyload following object. */
679 #define DF_P1_GROUPPERM 0x00000002 /* Symbols from next object are not
680 generally available. */
682 /* Version definition sections. */
691 u32 byte_offset_next_version_definition;
692 } elf_dynamic_version_definition_t;
697 u32 next_offset; /* byte offset of ver def aux next entry */
698 } elf_dynamic_version_definition_aux_t;
700 /* Version definition flags. */
701 #define ELF_DYNAMIC_VERSION_FILE (1 << 0) /* Version definition of file itself */
702 #define ELF_DYNAMIC_VERSION_WEAK (1 << 1) /* Weak version identifier */
704 /* Version symbol index. */
705 #define ELF_DYNAMIC_VERSYM_LOCAL 0 /* Symbol is local. */
706 #define ELF_DYNAMIC_VERSYM_GLOBAL 1 /* Symbol is global. */
707 #define ELF_DYNAMIC_VERSYM_RESERVED_LO 0xff00 /* Beginning of reserved entries. */
708 #define ELF_DYNAMIC_VERSYM_ELIMINATE 0xff01 /* Symbol is to be eliminated. */
710 /* Version dependency section. */
711 #define foreach_elf_dynamic_version_need_field \
714 _ (u32, file_name_offset) \
715 _ (u32, first_aux_offset) \
718 #define foreach_elf_dynamic_version_need_aux_field \
721 _ (u16, versym_index) \
728 foreach_elf_dynamic_version_need_field
730 } elf_dynamic_version_need_t;
735 foreach_elf_dynamic_version_need_aux_field
737 } elf_dynamic_version_need_aux_t;
741 elf_dynamic_version_need_t need;
742 elf_dynamic_version_need_aux_t aux;
743 } elf_dynamic_version_need_union_t;
745 /* Note section contents. Each entry in the note section begins with
746 a header of a fixed form. */
755 /* Known names of notes. */
757 /* Solaris entries in the note section have this name. */
758 #define ELF_NOTE_SOLARIS "SUNW Solaris"
760 /* Note entries for GNU systems have this name. */
761 #define ELF_NOTE_GNU "GNU"
764 /* Defined types of notes for Solaris. */
766 /* Value of descriptor (one word) is desired pagesize for the binary. */
767 #define ELF_NOTE_PAGESIZE_HINT 1
770 /* Defined note types for GNU systems. */
772 /* ABI information. The descriptor consists of words:
773 word 0: OS descriptor
774 word 1: major version of the ABI
775 word 2: minor version of the ABI
776 word 3: subminor version of the ABI
779 #define ELF_NOTE_ABI 1
782 /* Known OSes. These value can appear in word 0 of an ELF_NOTE_ABI
783 note section entry. */
784 #define ELF_NOTE_OS_LINUX 0
785 #define ELF_NOTE_OS_GNU 1
786 #define ELF_NOTE_OS_SOLARIS2 2
787 #define ELF_NOTE_OS_FREEBSD 3
789 /* AMD x86-64 relocations. */
790 #define foreach_elf_x86_64_relocation_type \
791 _ (NONE, 0) /* No reloc */ \
792 _ (DIRECT_64, 1) /* Direct 64 bit */ \
793 _ (PC_REL_I32, 2) /* PC relative 32 bit signed */ \
794 _ (GOT_REL_32, 3) /* 32 bit GOT entry */ \
795 _ (PLT_REL_32, 4) /* 32 bit PLT address */ \
796 _ (COPY, 5) /* Copy symbol at runtime */ \
797 _ (CREATE_GOT, 6) /* Create GOT entry */ \
798 _ (CREATE_PLT, 7) /* Create PLT entry */ \
799 _ (RELATIVE, 8) /* Adjust by program base */ \
800 _ (PC_REL_I32_GOT, 9) /* 32 bit PC relative offset to GOT */ \
801 _ (DIRECT_U32, 10) /* Direct 32 bit zero extended */ \
802 _ (DIRECT_I32, 11) /* Direct 32 bit sign extended */ \
803 _ (DIRECT_U16, 12) /* Direct 16 bit zero extended */ \
804 _ (PC_REL_I16, 13) /* 16 bit sign extended pc relative */ \
805 _ (DIRECT_I8, 14) /* Direct 8 bit sign extended */ \
806 _ (PC_REL_I8, 15) /* 8 bit sign extended pc relative */ \
807 _ (DTPMOD64, 16) /* ID of module containing symbol */ \
808 _ (DTPOFF64, 17) /* Offset in module's TLS block */ \
809 _ (TPOFF64, 18) /* Offset in initial TLS block */ \
810 _ (TLSGD, 19) /* 32 bit signed PC relative offset to two GOT entries for GD symbol */ \
811 _ (TLSLD, 20) /* 32 bit signed PC relative offset to two GOT entries for LD symbol */ \
812 _ (DTPOFF32, 21) /* Offset in TLS block */ \
813 _ (GOTTPOFF, 22) /* 32 bit signed PC relative offset to GOT entry for IE symbol */ \
814 _ (TPOFF32, 23) /* Offset in initial TLS, block) */
818 elf64_symbol_t *symbols;
824 uword *symbol_by_name;
825 } elf_symbol_table_t;
828 elf_symbol_table_free (elf_symbol_table_t * s)
830 vec_free (s->symbols);
831 hash_free (s->symbol_by_name);
835 elf_symbol_name (elf_symbol_table_t * t, elf64_symbol_t * sym)
837 return vec_elt_at_index (t->string_table, sym->name);
842 elf_relocation_with_addend_t *relocations;
845 } elf_relocation_table_t;
848 elf_relocation_table_free (elf_relocation_table_t * r)
850 vec_free (r->relocations);
855 elf64_section_header_t header;
859 /* Index of segments containing this section. */
860 uword *segment_index_bitmap;
862 /* Aligned size (included padding not included in
863 header.file_size). */
866 i64 exec_address_change;
873 elf64_segment_header_t header;
875 /* Sections contained in this segment. */
876 uword *section_index_bitmap;
891 elf_first_header_t first_header;
893 elf64_file_header_t file_header;
895 elf_segment_t *segments;
897 elf_section_t *sections;
899 uword *section_by_name;
900 uword *section_by_start_address;
902 elf_symbol_table_t *symbol_tables;
903 elf_relocation_table_t *relocation_tables;
907 elf64_dynamic_entry_t *dynamic_entries;
908 u8 *dynamic_string_table;
909 u32 dynamic_string_table_section_index;
910 u32 dynamic_symbol_table_section_index;
911 u32 dynamic_symbol_table_index;
912 u32 dynamic_section_index;
914 u32 versym_section_index;
915 elf_dynamic_version_need_union_t *verneed;
916 u32 verneed_section_index;
920 elf_main_init (elf_main_t * em)
922 memset (em, 0, sizeof (em[0]));
926 elf_main_free (elf_main_t * em)
930 for (i = 0; i < vec_len (em->segments); i++)
931 vec_free (em->segments[i].contents);
932 vec_free (em->segments);
934 for (i = 0; i < vec_len (em->sections); i++)
935 vec_free (em->sections[i].contents);
936 vec_free (em->sections);
938 hash_free (em->section_by_name);
939 for (i = 0; i < vec_len (em->symbol_tables); i++)
940 elf_symbol_table_free (em->symbol_tables + i);
941 for (i = 0; i < vec_len (em->relocation_tables); i++)
942 elf_relocation_table_free (em->relocation_tables + i);
944 vec_free (em->dynamic_entries);
945 vec_free (em->interpreter);
949 elf_get_segment_contents (elf_main_t * em, void *data, uword segment_index)
951 elf_segment_t *g = vec_elt_at_index (em->segments, segment_index);
953 vec_add (g->contents, data + g->header.file_offset,
954 g->header.memory_size);
958 elf_get_section_contents (elf_main_t * em,
959 uword section_index, uword elt_size)
964 s = vec_elt_at_index (em->sections, section_index);
967 if (vec_len (s->contents) > 0)
969 /* Make vector copy of contents with given element size. */
970 result = _vec_resize (result,
971 vec_len (s->contents) / elt_size,
972 vec_len (s->contents),
973 /* header_bytes */ 0,
975 clib_memcpy (result, s->contents, vec_len (s->contents));
982 elf_set_section_contents (elf_main_t * em,
984 void *new_contents, uword n_content_bytes)
988 s = vec_elt_at_index (em->sections, section_index);
989 vec_free (s->contents);
990 vec_add (s->contents, new_contents, n_content_bytes);
994 elf_section_name (elf_main_t * em, elf_section_t * s)
996 elf_section_t *es = vec_elt_at_index (em->sections,
998 file_header.section_header_string_table_index);
999 return vec_elt_at_index (es->contents, s->header.name);
1003 elf_swap_u8 (elf_main_t * em, u8 x)
1009 elf_swap_u16 (elf_main_t * em, u16 x)
1011 return em->need_byte_swap ? clib_byte_swap_u16 (x) : x;
1015 elf_swap_u32 (elf_main_t * em, u32 x)
1017 return em->need_byte_swap ? clib_byte_swap_u32 (x) : x;
1021 elf_swap_u64 (elf_main_t * em, u64 x)
1023 return em->need_byte_swap ? clib_byte_swap_u64 (x) : x;
1026 #define FORMAT_ELF_MAIN_SYMBOLS (1 << 0)
1027 #define FORMAT_ELF_MAIN_RELOCATIONS (1 << 1)
1028 #define FORMAT_ELF_MAIN_DYNAMIC (1 << 2)
1030 format_function_t format_elf_main;
1031 format_function_t format_elf_symbol;
1033 clib_error_t *elf_read_file (elf_main_t * em, char *file_name);
1034 clib_error_t *elf_write_file (elf_main_t * em, char *file_name);
1035 clib_error_t *elf_delete_named_section (elf_main_t * em, char *section_name);
1036 clib_error_t *elf_parse (elf_main_t * em, void *data, uword data_bytes);
1037 void elf_parse_symbols (elf_main_t * em);
1039 clib_error_t *elf_get_section_by_name (elf_main_t * em, char *section_name,
1040 elf_section_t ** result);
1041 clib_error_t *elf_get_section_by_start_address (elf_main_t * em,
1042 uword start_address,
1043 elf_section_t ** result);
1046 elf_create_section_with_contents (elf_main_t * em,
1048 elf64_section_header_t * header,
1049 void *contents, uword n_content_bytes);
1050 uword elf_delete_segment_with_type (elf_main_t * em,
1051 elf_segment_type_t segment_type);
1052 void elf_set_dynamic_entries (elf_main_t * em);
1054 #endif /* included_clib_elf_h */
1057 * fd.io coding-style-patch-verification: ON
1060 * eval: (c-set-style "gnu")