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, 192 #define _(f,i) ELF_ARCH_##f = i, 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) 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) \ 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 \ 293 _ (u8, binding_and_type) \ 295 _ (u16, section_index) 297 #define foreach_elf64_symbol_header \ 299 _ (u8, binding_and_type) \ 301 _ (u16, section_index) \ 341 #define foreach_elf_symbol_reserved_section_index \ 342 _ (ABSOLUTE, 0xfff1) \ 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) \ 365 _ (DYNAMIC_SYMBOL_TABLE, 11) \ 368 _ (PREINIT_ARRAY, 16) \ 370 _ (SYMTAB_SHNDX, 18) \ 371 _ (OS_SPECIFIC_LO, 0x60000000) \ 372 _ (GNU_LIBLIST, 0x6ffffff7) \ 373 _ (CHECKSUM, 0x6ffffff8) \ 374 _ (SUNW_MOVE, 0x6ffffffa) \ 375 _ (SUNW_COMDAT, 0x6ffffffb) \ 376 _ (SUNW_SYMINFO, 0x6ffffffc) \ 377 _ (GNU_VERDEF, 0x6ffffffd) \ 378 _ (GNU_VERNEED, 0x6ffffffe) \ 379 _ (GNU_VERSYM, 0x6fffffff) \ 380 _ (ARCH_SPECIFIC_LO, 0x70000000) \ 381 _ (ARCH_SPECIFIC_HI, 0x7fffffff) \ 382 _ (APP_SPECIFIC_LO, 0x80000000) \ 383 _ (APP_SPECIFIC_HI, 0x8fffffff) 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, 412 #define _(f,i) ELF_SECTION_FLAG_BIT_##f = i, 419 #define _(f,i) ELF_SECTION_FLAG_##f = 1 << ELF_SECTION_FLAG_BIT_##f, 425 #define foreach_elf_symbol_binding \ 429 _ (OS_SPECIFIC_LO, 10) \ 430 _ (OS_SPECIFIC_HI, 12) \ 431 _ (ARCH_SPECIFIC_LO, 13) \ 432 _ (ARCH_SPECIFIC_HI, 15) 435 #define foreach_elf_symbol_type \ 443 _ (OS_SPECIFIC_LO, 10) \ 444 _ (OS_SPECIFIC_HI, 12) \ 445 _ (ARCH_SPECIFIC_LO, 13) \ 446 _ (ARCH_SPECIFIC_HI, 15) 449 #define foreach_elf_symbol_visibility \ 464 #define foreach_elf_symbol_info_bound_to \ 467 _ (RESERVED_LO, 0xff00) \ 468 _ (RESERVED_HI, 0xffff) 471 #define foreach_elf_symbol_info_flags \ 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 \ 510 _ (SEGMENT_TABLE, 6) \ 512 _ (OS_SPECIFIC_LO, 0x60000000) \ 513 _ (GNU_EH_FRAME, 0x6474e550) \ 514 _ (GNU_STACK, 0x6474e551) \ 515 _ (GNU_RELRO, 0x6474e552) \ 516 _ (SUNW_BSS, 0x6ffffffa) \ 517 _ (SUNW_STACK, 0x6ffffffb) \ 518 _ (OS_SPECIFIC_HI, 0x6fffffff) \ 519 _ (ARCH_SPECIFIC_LO, 0x70000000) \ 520 _ (ARCH_SPECIFIC_HI, 0x7fffffff) 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, 541 #define _(f,i) ELF_SEGMENT_FLAG_BIT_##f = i, 548 #define _(f,i) ELF_SEGMENT_FLAG_##f = 1 << ELF_SEGMENT_FLAG_BIT_##f, 553 #define foreach_elf32_dynamic_entry_header \ 557 #define foreach_elf64_dynamic_entry_header \ 573 #define foreach_elf_dynamic_entry_type \ 575 _ (NEEDED_LIBRARY, 1) \ 576 _ (PLT_RELOCATION_SIZE, 2) \ 579 _ (STRING_TABLE, 5) \ 580 _ (SYMBOL_TABLE, 6) \ 581 _ (RELA_ADDRESS, 7) \ 583 _ (RELA_ENTRY_SIZE, 9) \ 584 _ (STRING_TABLE_SIZE, 10) \ 585 _ (SYMBOL_TABLE_ENTRY_SIZE, 11) \ 586 _ (INIT_FUNCTION, 12) \ 587 _ (FINI_FUNCTION, 13) \ 594 _ (PLT_RELOCATION_TYPE, 20) \ 597 _ (PLT_RELOCATION_ADDRESS, 23) \ 601 _ (INIT_ARRAYSZ, 27) \ 602 _ (FINI_ARRAYSZ, 28) \ 606 _ (PREINIT_ARRAY, 32) \ 607 _ (PREINIT_ARRAY_SIZE, 33) \ 608 _ (GNU_PRELINKED, 0x6ffffdf5) \ 609 _ (GNU_CONFLICTSZ, 0x6ffffdf6) \ 610 _ (GNU_LIBLISTSZ, 0x6ffffdf7) \ 611 _ (CHECKSUM, 0x6ffffdf8) \ 612 _ (PLTPADSZ, 0x6ffffdf9) \ 613 _ (MOVEENT, 0x6ffffdfa) \ 614 _ (MOVESZ, 0x6ffffdfb) \ 615 _ (FEATURE_1, 0x6ffffdfc) \ 616 _ (POSFLAG_1, 0x6ffffdfd) \ 617 _ (SYMINSZ, 0x6ffffdfe) \ 618 _ (SYMINENT, 0x6ffffdff) \ 619 _ (GNU_HASH, 0x6ffffef5) \ 620 _ (GNU_CONFLICT, 0x6ffffef8) \ 621 _ (GNU_LIBLIST, 0x6ffffef9) \ 622 _ (CONFIG, 0x6ffffefa) \ 623 _ (DEPAUDIT, 0x6ffffefb) \ 624 _ (AUDIT, 0x6ffffefc) \ 625 _ (PLTPAD, 0x6ffffefd) \ 626 _ (MOVETAB, 0x6ffffefe) \ 627 _ (SYMINFO, 0x6ffffeff) \ 628 _ (VERSYM, 0x6ffffff0) \ 629 _ (RELACOUNT, 0x6ffffff9) \ 630 _ (RELCOUNT, 0x6ffffffa) \ 631 _ (FLAGS_1, 0x6ffffffb) \ 632 _ (VERSION_DEF, 0x6ffffffc) \ 633 _ (VERSION_DEF_COUNT, 0x6ffffffd) \ 634 _ (VERSION_NEED, 0x6ffffffe) \ 635 _ (VERSION_NEED_COUNT, 0x6fffffff) \ 636 _ (AUXILIARY, 0x7ffffffd) \ 637 _ (FILTER, 0x7fffffff) 641 #define _(f,n) ELF_DYNAMIC_ENTRY_##f = (n), 647 #define ELF_DYNAMIC_FLAGS_ORIGIN (1 << 0) 648 #define ELF_DYNAMIC_FLAGS_SYMBOLIC (1 << 1) 649 #define ELF_DYNAMIC_FLAGS_TEXT_RELOCATIONS (1 << 2) 650 #define ELF_DYNAMIC_FLAGS_BIND_NOW (1 << 3) 651 #define ELF_DYNAMIC_FLAGS_STATIC_TLS (1 << 4) 655 #define DF_1_NOW 0x00000001 656 #define DF_1_GLOBAL 0x00000002 657 #define DF_1_GROUP 0x00000004 658 #define DF_1_NODELETE 0x00000008 659 #define DF_1_LOADFLTR 0x00000010 660 #define DF_1_INITFIRST 0x00000020 661 #define DF_1_NOOPEN 0x00000040 662 #define DF_1_ORIGIN 0x00000080 663 #define DF_1_DIRECT 0x00000100 664 #define DF_1_TRANS 0x00000200 665 #define DF_1_INTERPOSE 0x00000400 666 #define DF_1_NODEFLIB 0x00000800 667 #define DF_1_NODUMP 0x00001000 668 #define DF_1_CONFALT 0x00002000 669 #define DF_1_ENDFILTEE 0x00004000 670 #define DF_1_DISPRELDNE 0x00008000 671 #define DF_1_DISPRELPND 0x00010000 674 #define DTF_1_PARINIT 0x00000001 675 #define DTF_1_CONFEXP 0x00000002 678 #define DF_P1_LAZYLOAD 0x00000001 679 #define DF_P1_GROUPPERM 0x00000002 701 #define ELF_DYNAMIC_VERSION_FILE (1 << 0) 702 #define ELF_DYNAMIC_VERSION_WEAK (1 << 1) 705 #define ELF_DYNAMIC_VERSYM_LOCAL 0 706 #define ELF_DYNAMIC_VERSYM_GLOBAL 1 707 #define ELF_DYNAMIC_VERSYM_RESERVED_LO 0xff00 708 #define ELF_DYNAMIC_VERSYM_ELIMINATE 0xff01 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) \ 758 #define ELF_NOTE_SOLARIS "SUNW Solaris" 761 #define ELF_NOTE_GNU "GNU" 767 #define ELF_NOTE_PAGESIZE_HINT 1 779 #define ELF_NOTE_ABI 1 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 790 #define foreach_elf_x86_64_relocation_type \ 800 _ (PC_REL_I32_GOT, 9) \ 922 memset (em, 0,
sizeof (em[0]));
970 result = _vec_resize (result,
984 void *new_contents,
uword n_content_bytes)
998 file_header.section_header_string_table_index);
1026 #define FORMAT_ELF_MAIN_SYMBOLS (1 << 0) 1027 #define FORMAT_ELF_MAIN_RELOCATIONS (1 << 1) 1028 #define FORMAT_ELF_MAIN_DYNAMIC (1 << 2) 1042 uword start_address,
1049 void *contents,
uword n_content_bytes);
static void elf_main_free(elf_main_t *em)
static u32 elf_swap_u32(elf_main_t *em, u32 x)
elf64_segment_header_t header
#define foreach_elf_segment_flag
u8 pad[3]
log2 (size of the packing page block)
static u8 * elf_section_name(elf_main_t *em, elf_section_t *s)
u32 verneed_section_index
#define foreach_elf_dynamic_entry_type
#define foreach_elf32_section_header
#define foreach_elf64_section_header
static void elf_symbol_table_free(elf_symbol_table_t *s)
static u32 clib_byte_swap_u32(u32 x)
#define foreach_elf32_dynamic_entry_header
clib_error_t * elf_read_file(elf_main_t *em, char *file_name)
#define foreach_elf_section_type
#define foreach_elf_segment_type
u32 dynamic_section_index
static u64 elf_swap_u64(elf_main_t *em, u64 x)
static void elf_relocation_table_free(elf_relocation_table_t *r)
static u8 elf_swap_u8(elf_main_t *em, u8 x)
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
memset(h->entries, 0, sizeof(h->entries[0])*entries)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
static u64 clib_byte_swap_u64(u64 x)
#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
#define foreach_elf64_segment_header
static void elf_get_segment_contents(elf_main_t *em, void *data, uword segment_index)
void elf_create_section_with_contents(elf_main_t *em, char *section_name, elf64_section_header_t *header, void *contents, uword n_content_bytes)
clib_error_t * elf_get_section_by_name(elf_main_t *em, char *section_name, elf_section_t **result)
#define foreach_elf32_file_header
static void elf_set_section_contents(elf_main_t *em, uword section_index, void *new_contents, uword n_content_bytes)
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).
#define foreach_elf32_symbol_header
elf_relocation_with_addend_t * relocations
#define clib_memcpy(a, b, c)
static void elf_main_init(elf_main_t *em)
u32 dynamic_symbol_table_section_index
static void * elf_get_section_contents(elf_main_t *em, uword section_index, uword elt_size)
elf64_dynamic_entry_t * dynamic_entries
elf_relocation_table_t * relocation_tables
elf_dynamic_version_need_t need
format_function_t format_elf_main
static u8 * elf_symbol_name(elf_symbol_table_t *t, elf64_symbol_t *sym)
#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
format_function_t format_elf_symbol
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)
static u16 elf_swap_u16(elf_main_t *em, u16 x)
static 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
u32 dynamic_string_table_section_index