38 #ifndef included_clib_elf_h 39 #define included_clib_elf_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) \ 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) 85 #define foreach_elf_architecture \ 162 #define _(f) ELF_##f, 176 #define _(f,i) ELF_##f = i, 189 #define _(f,i) ELF_ARCH_##f = i, 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) 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) \ 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 \ 289 _ (u8, binding_and_type) \ 291 _ (u16, section_index) 293 #define foreach_elf64_symbol_header \ 295 _ (u8, binding_and_type) \ 297 _ (u16, section_index) \ 337 #define foreach_elf_symbol_reserved_section_index \ 338 _ (ABSOLUTE, 0xfff1) \ 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) \ 361 _ (DYNAMIC_SYMBOL_TABLE, 11) \ 364 _ (PREINIT_ARRAY, 16) \ 366 _ (SYMTAB_SHNDX, 18) \ 367 _ (OS_SPECIFIC_LO, 0x60000000) \ 368 _ (GNU_LIBLIST, 0x6ffffff7) \ 369 _ (CHECKSUM, 0x6ffffff8) \ 370 _ (SUNW_MOVE, 0x6ffffffa) \ 371 _ (SUNW_COMDAT, 0x6ffffffb) \ 372 _ (SUNW_SYMINFO, 0x6ffffffc) \ 373 _ (GNU_VERDEF, 0x6ffffffd) \ 374 _ (GNU_VERNEED, 0x6ffffffe) \ 375 _ (GNU_VERSYM, 0x6fffffff) \ 376 _ (ARCH_SPECIFIC_LO, 0x70000000) \ 377 _ (ARCH_SPECIFIC_HI, 0x7fffffff) \ 378 _ (APP_SPECIFIC_LO, 0x80000000) \ 379 _ (APP_SPECIFIC_HI, 0x8fffffff) 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, 406 #define _(f,i) ELF_SECTION_FLAG_BIT_##f = i, 412 #define _(f,i) ELF_SECTION_FLAG_##f = 1 << ELF_SECTION_FLAG_BIT_##f, 418 #define foreach_elf_symbol_binding \ 422 _ (OS_SPECIFIC_LO, 10) \ 423 _ (OS_SPECIFIC_HI, 12) \ 424 _ (ARCH_SPECIFIC_LO, 13) \ 425 _ (ARCH_SPECIFIC_HI, 15) 428 #define foreach_elf_symbol_type \ 436 _ (OS_SPECIFIC_LO, 10) \ 437 _ (OS_SPECIFIC_HI, 12) \ 438 _ (ARCH_SPECIFIC_LO, 13) \ 439 _ (ARCH_SPECIFIC_HI, 15) 442 #define foreach_elf_symbol_visibility \ 456 #define foreach_elf_symbol_info_bound_to \ 459 _ (RESERVED_LO, 0xff00) \ 460 _ (RESERVED_HI, 0xffff) 463 #define foreach_elf_symbol_info_flags \ 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 \ 499 _ (SEGMENT_TABLE, 6) \ 501 _ (OS_SPECIFIC_LO, 0x60000000) \ 502 _ (GNU_EH_FRAME, 0x6474e550) \ 503 _ (GNU_STACK, 0x6474e551) \ 504 _ (GNU_RELRO, 0x6474e552) \ 505 _ (SUNW_BSS, 0x6ffffffa) \ 506 _ (SUNW_STACK, 0x6ffffffb) \ 507 _ (OS_SPECIFIC_HI, 0x6fffffff) \ 508 _ (ARCH_SPECIFIC_LO, 0x70000000) \ 509 _ (ARCH_SPECIFIC_HI, 0x7fffffff) 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, 528 #define _(f,i) ELF_SEGMENT_FLAG_BIT_##f = i, 534 #define _(f,i) ELF_SEGMENT_FLAG_##f = 1 << ELF_SEGMENT_FLAG_BIT_##f, 539 #define foreach_elf32_dynamic_entry_header \ 543 #define foreach_elf64_dynamic_entry_header \ 559 #define foreach_elf_dynamic_entry_type \ 561 _ (NEEDED_LIBRARY, 1) \ 562 _ (PLT_RELOCATION_SIZE, 2) \ 565 _ (STRING_TABLE, 5) \ 566 _ (SYMBOL_TABLE, 6) \ 567 _ (RELA_ADDRESS, 7) \ 569 _ (RELA_ENTRY_SIZE, 9) \ 570 _ (STRING_TABLE_SIZE, 10) \ 571 _ (SYMBOL_TABLE_ENTRY_SIZE, 11) \ 572 _ (INIT_FUNCTION, 12) \ 573 _ (FINI_FUNCTION, 13) \ 580 _ (PLT_RELOCATION_TYPE, 20) \ 583 _ (PLT_RELOCATION_ADDRESS, 23) \ 587 _ (INIT_ARRAYSZ, 27) \ 588 _ (FINI_ARRAYSZ, 28) \ 592 _ (PREINIT_ARRAY, 32) \ 593 _ (PREINIT_ARRAY_SIZE, 33) \ 594 _ (GNU_PRELINKED, 0x6ffffdf5) \ 595 _ (GNU_CONFLICTSZ, 0x6ffffdf6) \ 596 _ (GNU_LIBLISTSZ, 0x6ffffdf7) \ 597 _ (CHECKSUM, 0x6ffffdf8) \ 598 _ (PLTPADSZ, 0x6ffffdf9) \ 599 _ (MOVEENT, 0x6ffffdfa) \ 600 _ (MOVESZ, 0x6ffffdfb) \ 601 _ (FEATURE_1, 0x6ffffdfc) \ 602 _ (POSFLAG_1, 0x6ffffdfd) \ 603 _ (SYMINSZ, 0x6ffffdfe) \ 604 _ (SYMINENT, 0x6ffffdff) \ 605 _ (GNU_HASH, 0x6ffffef5) \ 606 _ (GNU_CONFLICT, 0x6ffffef8) \ 607 _ (GNU_LIBLIST, 0x6ffffef9) \ 608 _ (CONFIG, 0x6ffffefa) \ 609 _ (DEPAUDIT, 0x6ffffefb) \ 610 _ (AUDIT, 0x6ffffefc) \ 611 _ (PLTPAD, 0x6ffffefd) \ 612 _ (MOVETAB, 0x6ffffefe) \ 613 _ (SYMINFO, 0x6ffffeff) \ 614 _ (VERSYM, 0x6ffffff0) \ 615 _ (RELACOUNT, 0x6ffffff9) \ 616 _ (RELCOUNT, 0x6ffffffa) \ 617 _ (FLAGS_1, 0x6ffffffb) \ 618 _ (VERSION_DEF, 0x6ffffffc) \ 619 _ (VERSION_DEF_COUNT, 0x6ffffffd) \ 620 _ (VERSION_NEED, 0x6ffffffe) \ 621 _ (VERSION_NEED_COUNT, 0x6fffffff) \ 622 _ (AUXILIARY, 0x7ffffffd) \ 623 _ (FILTER, 0x7fffffff) 626 #define _(f,n) ELF_DYNAMIC_ENTRY_##f = (n), 632 #define ELF_DYNAMIC_FLAGS_ORIGIN (1 << 0) 633 #define ELF_DYNAMIC_FLAGS_SYMBOLIC (1 << 1) 634 #define ELF_DYNAMIC_FLAGS_TEXT_RELOCATIONS (1 << 2) 635 #define ELF_DYNAMIC_FLAGS_BIND_NOW (1 << 3) 636 #define ELF_DYNAMIC_FLAGS_STATIC_TLS (1 << 4) 640 #define DF_1_NOW 0x00000001 641 #define DF_1_GLOBAL 0x00000002 642 #define DF_1_GROUP 0x00000004 643 #define DF_1_NODELETE 0x00000008 644 #define DF_1_LOADFLTR 0x00000010 645 #define DF_1_INITFIRST 0x00000020 646 #define DF_1_NOOPEN 0x00000040 647 #define DF_1_ORIGIN 0x00000080 648 #define DF_1_DIRECT 0x00000100 649 #define DF_1_TRANS 0x00000200 650 #define DF_1_INTERPOSE 0x00000400 651 #define DF_1_NODEFLIB 0x00000800 652 #define DF_1_NODUMP 0x00001000 653 #define DF_1_CONFALT 0x00002000 654 #define DF_1_ENDFILTEE 0x00004000 655 #define DF_1_DISPRELDNE 0x00008000 656 #define DF_1_DISPRELPND 0x00010000 659 #define DTF_1_PARINIT 0x00000001 660 #define DTF_1_CONFEXP 0x00000002 663 #define DF_P1_LAZYLOAD 0x00000001 664 #define DF_P1_GROUPPERM 0x00000002 685 #define ELF_DYNAMIC_VERSION_FILE (1 << 0) 686 #define ELF_DYNAMIC_VERSION_WEAK (1 << 1) 689 #define ELF_DYNAMIC_VERSYM_LOCAL 0 690 #define ELF_DYNAMIC_VERSYM_GLOBAL 1 691 #define ELF_DYNAMIC_VERSYM_RESERVED_LO 0xff00 692 #define ELF_DYNAMIC_VERSYM_ELIMINATE 0xff01 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) \ 742 #define ELF_NOTE_SOLARIS "SUNW Solaris" 745 #define ELF_NOTE_GNU "GNU" 751 #define ELF_NOTE_PAGESIZE_HINT 1 763 #define ELF_NOTE_ABI 1 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 774 #define foreach_elf_x86_64_relocation_type \ 784 _ (PC_REL_I32_GOT, 9) \ 899 memset (em, 0,
sizeof (em[0]));
949 result = _vec_resize (result,
964 uword n_content_bytes)
996 #define FORMAT_ELF_MAIN_SYMBOLS (1 << 0) 997 #define FORMAT_ELF_MAIN_RELOCATIONS (1 << 1) 998 #define FORMAT_ELF_MAIN_DYNAMIC (1 << 2) 1016 char * section_name,
1019 uword n_content_bytes);
elf64_segment_header_t header
#define foreach_elf_segment_flag
sll srl srl sll sra u16x4 i
u32 verneed_section_index
#define foreach_elf_dynamic_entry_type
#define foreach_elf32_section_header
#define foreach_elf64_section_header
#define foreach_elf32_dynamic_entry_header
clib_error_t * elf_read_file(elf_main_t *em, char *file_name)
always_inline u32 clib_byte_swap_u32(u32 x)
#define foreach_elf_section_type
#define foreach_elf_segment_type
always_inline void elf_set_section_contents(elf_main_t *em, uword section_index, void *new_contents, uword n_content_bytes)
u32 dynamic_section_index
always_inline u8 * elf_section_name(elf_main_t *em, elf_section_t *s)
clib_error_t * elf_delete_named_section(elf_main_t *em, char *section_name)
#define foreach_elf64_file_header
#define vec_add(V, E, N)
Add N elements to end of vector V (no header, unspecified alignment)
u32 byte_offset_next_version_definition
#define foreach_elf_dynamic_version_need_aux_field
#define foreach_elf64_symbol_header
uword * section_index_bitmap
always_inline u8 elf_swap_u8(elf_main_t *em, u8 x)
always_inline u64 elf_swap_u64(elf_main_t *em, u64 x)
always_inline void elf_get_segment_contents(elf_main_t *em, void *data, uword segment_index)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define foreach_elf32_segment_header
uword elf_delete_segment_with_type(elf_main_t *em, elf_segment_type_t segment_type)
#define foreach_elf_file_class
elf_symbol_table_t * symbol_tables
#define foreach_elf_dynamic_version_need_field
clib_error_t * elf_get_section_by_start_address(elf_main_t *em, uword start_address, elf_section_t **result)
elf_dynamic_version_need_aux_t aux
#define foreach_elf_file_type
u8 * dynamic_string_table
#define foreach_elf_data_encoding
always_inline void elf_relocation_table_free(elf_relocation_table_t *r)
#define foreach_elf64_segment_header
void elf_create_section_with_contents(elf_main_t *em, char *section_name, elf64_section_header_t *header, void *contents, uword n_content_bytes)
always_inline void elf_main_init(elf_main_t *em)
clib_error_t * elf_get_section_by_name(elf_main_t *em, char *section_name, elf_section_t **result)
#define foreach_elf32_file_header
always_inline u16 elf_swap_u16(elf_main_t *em, u16 x)
elf_first_header_t first_header
clib_error_t * elf_parse(elf_main_t *em, void *data, uword data_bytes)
#define vec_free(V)
Free vector's memory (no header).
always_inline void elf_main_free(elf_main_t *em)
#define foreach_elf32_symbol_header
elf_relocation_with_addend_t * relocations
#define clib_memcpy(a, b, c)
always_inline u8 * elf_symbol_name(elf_symbol_table_t *t, elf64_symbol_t *sym)
u32 dynamic_symbol_table_section_index
always_inline void elf_symbol_table_free(elf_symbol_table_t *s)
elf64_dynamic_entry_t * dynamic_entries
elf_relocation_table_t * relocation_tables
elf_dynamic_version_need_t need
format_function_t format_elf_main
#define foreach_elf64_dynamic_entry_header
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
void elf_parse_symbols(elf_main_t *em)
clib_error_t * elf_write_file(elf_main_t *em, char *file_name)
uword * segment_index_bitmap
elf64_file_header_t file_header
#define foreach_elf_architecture
always_inline void * elf_get_section_contents(elf_main_t *em, uword section_index, uword elt_size)
format_function_t format_elf_symbol
always_inline u64 clib_byte_swap_u64(u64 x)
u32 dynamic_symbol_table_index
elf64_section_header_t header
elf_dynamic_version_need_union_t * verneed
void elf_set_dynamic_entries(elf_main_t *em)
always_inline u16 clib_byte_swap_u16(u16 x)
#define foreach_elf_section_flag
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
uword * section_by_start_address
always_inline u32 elf_swap_u32(elf_main_t *em, u32 x)
u32 dynamic_string_table_section_index