1.\"Copyright (c) 1999 Jeroen Ruigrok van der Werven 2.\"All rights reserved. 3.\" 4.\"Redistribution and use in source and binary forms, with or without 5.\"modification, are permitted provided that the following conditions 6.\"are met: 7.\"1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\"2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 13.\"THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14.\"ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.\"IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.\"ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17.\"FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18.\"DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19.\"OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20.\"HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21.\"LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22.\"OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23.\"SUCH DAMAGE. 24.\" 25.\" $FreeBSD$ 26.\" 27.Dd July 31, 1999 28.Dt ELF 5 29.Os FreeBSD 30.Sh NAME 31.Nm elf 32.Nd format of ELF executable binary files 33.Sh SYNOPSIS 34.Fd #include <elf.h> 35.Sh DESCRIPTION 36The header file 37.Aq Pa elf.h 38defines the format of ELF executable binary files. 39Amongst these files are 40normal executable files, relocatable object files, core files and shared 41libraries. 42.Pp 43An executable file using the ELF file format consists of an ELF header, 44followed by a program header table or a section header table, or both. 45The ELF header is always at offset zero of the file. 46The program header 47table and the section header table's offset in the file are defined in the 48ELF header. 49The two tables describe the rest of the particularities of 50the file. 51.Pp 52Applications which wish to process ELF binary files for their native 53architecture only should include 54.Pa elf.h 55in their source code. 56These applications should need to refer to 57all the types and structures by their generic names 58.Dq Elf_xxx 59and to the macros by 60.Dq ELF_xxx . 61Applications written this way can be compiled on any architecture, 62regardless whether the host is 32-bit or 64-bit. 63.Pp 64Should an application need to process ELF files of an unknown 65architecture then the application needs to include both 66.Pa sys/elf32.h 67and 68.Pa sys/elf64.h 69instead of 70.Pa elf.h . 71Furthermore, all types and structures need to be identified by either 72.Dq Elf32_xxx 73or 74.Dq Elf64_xxx . 75The macros need to be identified by 76.Dq ELF32_xxx 77or 78.Dq ELF64_xxx . 79.Pp 80Whatever the system's architecture is, it will always include 81.Pa sys/elf_common.h 82as well as 83.Pa sys/elf_generic.h . 84.Pp 85These header files describe the above mentioned headers as C structures 86and also include structures for dynamic sections, relocation sections and 87symbol tables. 88.Pp 89The following types are being used for 32-bit architectures: 90.Bd -literal -offset indent 91Elf32_Addr Unsigned program address 92Elf32_Half Unsigned halfword field 93Elf32_Off Unsigned file offset 94Elf32_Sword Signed large integer 95Elf32_Word Field or unsigned large integer 96Elf32_Size Unsigned object size 97.Ed 98.Pp 99For 64-bit architectures we have the following types: 100.Bd -literal -offset indent 101Elf64_Addr Unsigned program address 102Elf64_Half Unsigned halfword field 103Elf64_Off Unsigned file offset 104Elf64_Sword Signed large integer 105Elf64_Word Field or unsigned large integer 106Elf64_Size Unsigned object size 107Elf64_Quarter Unsigned quarterword field 108.Ed 109.Pp 110All data structures that the file format defines follow the 111.Dq natural 112size and alignment guidelines for the relevant class. 113If necessary, 114data structures contain explicit padding to ensure 4-byte alignment 115for 4-byte objects, to force structure sizes to a multiple of 4, etc. 116.Pp 117The ELF header is described by the type Elf32_Ehdr or Elf64_Ehdr: 118.Bd -literal -offset indent 119typedef struct { 120 unsigned char e_ident[EI_NIDENT]; 121 Elf32_Half e_type; 122 Elf32_Half e_machine; 123 Elf32_Word e_version; 124 Elf32_Addr e_entry; 125 Elf32_Off e_phoff; 126 Elf32_Off e_shoff; 127 Elf32_Word e_flags; 128 Elf32_Half e_ehsize; 129 Elf32_Half e_phentsize; 130 Elf32_Half e_phnum; 131 Elf32_Half e_shentsize; 132 Elf32_Half e_shnum; 133 Elf32_Half e_shstrndx; 134} Elf32_Ehdr; 135.Ed 136.Pp 137.Bd -literal -offset indent 138typedef struct { 139 unsigned char e_ident[EI_NIDENT]; 140 Elf64_Quarter e_type; 141 Elf64_Quarter e_machine; 142 Elf64_Half e_version; 143 Elf64_Addr e_entry; 144 Elf64_Off e_phoff; 145 Elf64_Off e_shoff; 146 Elf64_Half e_flags; 147 Elf64_Quarter e_ehsize; 148 Elf64_Quarter e_phentsize; 149 Elf64_Quarter e_phnum; 150 Elf64_Quarter e_shentsize; 151 Elf64_Quarter e_shnum; 152 Elf64_Quarter e_shstrndx; 153} Elf64_Ehdr; 154.Ed 155.Pp 156The fields have the following meanings: 157.Pp 158.Bl -tag -width "e_phentsize" -compact -offset indent 159.It Dv e_ident 160This array of bytes specifies to interpret the file, 161independent of the processor or the file's remaining contents. 162Within this array everything is named by macros, which start with 163the prefix 164.Sy EI_ 165and may contain values which start with the prefix 166.Sy ELF . 167The following macros are defined: 168.Pp 169.Bl -tag -width "EI_ABIVERSION" -compact 170.It Dv EI_MAG0 171The first byte of the magic number. 172It must be filled with 173.Sy ELFMAG0 . 174.It Dv EI_MAG1 175The second byte of the magic number. 176It must be filled with 177.Sy ELFMAG1 . 178.It Dv EI_MAG2 179The third byte of the magic number. 180It must be filled with 181.Sy ELFMAG2 . 182.It Dv EI_MAG3 183The fourth byte of the magic number. 184It must be filled with 185.Sy ELFMAG3 . 186.It Dv EI_CLASS 187The fifth byte identifies the architecture for this binary: 188.Pp 189.Bl -tag -width "ELFCLASSNONE" -compact 190.It Dv ELFCLASSNONE 191This class is invalid. 192.It Dv ELFCLASS32 193This defines the 32-bit architecture. 194It supports machines with files 195and virtual address spaces up to 4 Gigabytes. 196.It Dv ELFCLASS64 197This defines the 64-bit architecture. 198.El 199.It Dv EI_DATA 200The sixth byte specifies the data encoding of the processor-specific 201data in the file. 202Currently these encodings are supported: 203.Pp 204.Bl -tag -width "ELFDATA2LSB" -compact 205.It Dv ELFDATANONE 206Unknown data format. 207.It Dv ELFDATA2LSB 208Two's complement, little-endian. 209.It Dv ELFDATA2MSB 210Two's complement, big-endian. 211.El 212.It Dv EI_VERSION 213The version number of the ELF specification: 214.Pp 215.Bl -tag -width "EV_CURRENT" -compact 216.It Dv EV_NONE 217Invalid version. 218.It Dv EV_CURRENT 219Current version. 220.El 221.It Dv EI_OSABI 222This byte identifies the operating system 223and ABI to which the object is targeted. 224Some fields in other ELF structures have flags 225and values that have platform specific meanings; 226the interpretation of those fields is determined by the value of this byte. 227The following values are currently defined: 228.Pp 229.Bl -tag -width "ELFOSABI_STANDALONE" -compact 230.It Dv ELFOSABI_SYSV 231UNIX System V ABI. 232.It Dv ELFOSABI_HPUX 233HP-UX operating system ABI. 234.It Dv ELFOSABI_NETBSD 235NetBSD operating system ABI. 236.It Dv ELFOSABI_LINUX 237GNU/Linux operating system ABI. 238.It Dv ELFOSABI_HURD 239GNU/Hurd operating system ABI. 240.It Dv ELFOSABI_86OPEN 24186Open Common IA32 ABI. 242.It Dv ELFOSABI_SOLARIS 243Solaris operating system ABI. 244.It Dv ELFOSABI_MONTEREY 245Monterey project ABI. 246.It Dv ELFOSABI_IRIX 247IRIX operating system ABI. 248.It Dv ELFOSABI_FREEBSD 249FreeBSD operating system ABI. 250.It Dv ELFOSABI_TRU64 251TRU64 UNIX operating system ABI. 252.It Dv ELFOSABI_ARM 253ARM architecture ABI. 254.It Dv ELFOSABI_STANDALONE 255Standalone (embedded) ABI. 256.El 257.It Dv EI_ABIVERSION 258This byte identifies the version of the ABI 259to which the object is targeted. 260This field is used to distinguish among incompatible versions of an ABI. 261The interpretation of this version number 262is dependent on the ABI identified by the EI_OSABI field. 263Applications conforming to this specification use the value 0. 264.It Dv EI_PAD 265Start of padding. 266These bytes are reserved and set to zero. 267Programs 268which read them should ignore them. 269The value for EI_PAD will change in 270the future if currently unused bytes are given meanings. 271.It Dv EI_BRAND 272Start of architecture identification. 273.It Dv EI_NIDENT 274The size of the e_ident array. 275.El 276.Pp 277.It Dv e_type 278This member of the structure identifies the object file type: 279.Pp 280.Bl -tag -width "ET_NONE" -compact 281.It Dv ET_NONE 282An unknown type. 283.It Dv ET_REL 284A relocatable file. 285.It Dv ET_EXEC 286An executable file. 287.It Dv ET_DYN 288A shared object. 289.It Dv ET_CORE 290A core file. 291.El 292.Pp 293.It Dv e_machine 294This member specifies the required architecture for an individual file: 295.Pp 296.Bl -tag -width "EM_MIPS_RS4_BE" -compact 297.It Dv EM_NONE 298An unknown machine. 299.It Dv EM_M32 300AT&T WE 32100. 301.It Dv EM_SPARC 302Sun Microsystems SPARC. 303.It Dv EM_386 304Intel 80386. 305.It Dv EM_68K 306Motorola 68000. 307.It Dv EM_88K 308Motorola 88000. 309.It Dv EM_486 310Intel 80486. 311.It Dv EM_860 312Intel 80860. 313.It Dv EM_MIPS 314MIPS RS3000 (big-endian only). 315.It Dv EM_MIPS_RS4_BE 316MIPS RS4000 (big-endian only). 317.It Dv EM_SPARC64 318SPARC v9 64-bit unofficial. 319.It Dv EM_PARISC 320HPPA. 321.It Dv EM_PPC 322PowerPC. 323.It Dv EM_ALPHA 324Compaq [DEC] Alpha. 325.El 326.Pp 327.It Dv e_version 328This member identifies the file version: 329.Pp 330.Bl -tag -width "EV_CURRENT" -compact 331.It Dv EV_NONE 332Invalid version 333.It Dv EV_CURRENT 334Current version 335.El 336.It Dv e_entry 337This member gives the virtual address to which the system first transfers 338control, thus starting the process. 339If the file has no associated entry 340point, this member holds zero. 341.It Dv e_phoff 342This member holds the program header table's file offset in bytes. 343If 344the file has no program header table, this member holds zero. 345.It Dv e_shoff 346This member holds the section header table's file offset in bytes. 347If the 348file has no section header table this member holds zero. 349.It Dv e_flags 350This member holds processor-specific flags associated with the file. 351Flag 352names take the form EF_`machine_flag'. Currently no flags have been defined. 353.It Dv e_ehsize 354This member holds the ELF header's size in bytes. 355.It Dv e_phentsize 356This member holds the size in bytes of one entry in the file's program header 357table; all entries are the same size. 358.It Dv e_phnum 359This member holds the number of entries in the program header 360table. 361Thus the product of 362.Sy e_phentsize 363and 364.Sy e_phnum 365gives the table's size 366in bytes. 367If a file has no program header, 368.Sy e_phnum 369holds the value zero. 370.It Dv e_shentsize 371This member holds a sections header's size in bytes. 372A section header is one 373entry in the section header table; all entries are the same size. 374.It Dv e_shnum 375This member holds the number of entries in the section header table. 376Thus 377the product of 378.Sy e_shentsize 379and 380.Sy e_shnum 381gives the section header table's size in bytes. 382If a file has no section 383header table, 384.Sy e_shnum 385holds the value of zero. 386.It Dv e_shstrndx 387This member holds the section header table index of the entry associated 388with the section name string table. 389If the file has no section name string 390table, this member holds the value 391.Sy SHN_UNDEF . 392.Pp 393.Bl -tag -width "SHN_LORESERVE" -compact 394.It Dv SHN_UNDEF 395This value marks an undefined, missing, irrelevant, or otherwise meaningless 396section reference. 397For example, a symbol 398.Dq defined 399relative to section number 400.Sy SHN_UNDEF 401is an undefined symbol. 402.It Dv SHN_LORESERVE 403This value specifies the lower bound of the range of reserved indexes. 404.It Dv SHN_LOPROC 405This value up to and including 406.Sy SHN_HIPROC 407are reserved for processor-specific semantics. 408.It Dv SHN_HIPROC 409This value down to and including 410.Sy SHN_LOPROC 411are reserved for processor-specific semantics. 412.It Dv SHN_ABS 413This value specifies absolute values for the corresponding reference. 414For 415example, symbols defined relative to section number 416.Sy SHN_ABS 417have absolute values and are not affected by relocation. 418.It Dv SHN_COMMON 419Symbols defined relative to this section are common symbols, such as Fortran 420COMMON or unallocated C external variables. 421.It Dv SHN_HIRESERVE 422This value specifies the upper bound of the range of the range of reserved 423indices between 424.Sy SHN_LORESERVE 425and 426.Sy SHN_HIRESERVE , 427inclusive; the values do 428not reference the section header table. 429That is, the section header table 430does 431.Em not 432contain entries for the reserved indices. 433.El 434.El 435.Pp 436An executable or shared object file's program header table is an array of 437structures, each describing a segment or other information the system needs 438to prepare the program for execution. 439An object file 440.Em segment 441contains one or more 442.Em sections . 443Program headers are meaningful only for executable and shared object files. 444A file specifies its own program header size with the ELF header's 445.Sy e_phentsize 446and 447.Sy e_phnum 448members. 449As with the Elf executable header, the program header 450also has different versions depending on the architecture: 451.Pp 452.Bd -literal -offset indent 453typedef struct { 454 Elf32_Word p_type; 455 Elf32_Off p_offset; 456 Elf32_Addr p_vaddr; 457 Elf32_Addr p_paddr; 458 Elf32_Size p_filesz; 459 Elf32_Size p_memsz; 460 Elf32_Word p_flags; 461 Elf32_Size p_align; 462} Elf32_Phdr; 463.Ed 464.Pp 465.Bd -literal -offset indent 466typedef struct { 467 Elf64_Half p_type; 468 Elf64_Half p_flags; 469 Elf64_Off p_offset; 470 Elf64_Addr p_vaddr; 471 Elf64_Addr p_paddr; 472 Elf64_Size p_filesz; 473 Elf64_Size p_memsz; 474 Elf64_Size p_align; 475} Elf64_Phdr; 476.Ed 477.Pp 478The main difference between the 32-bit and the 64-bit program header lies 479only in the location of a 480.Sy p_flags 481member in the total struct. 482.Pp 483.Bl -tag -width "p_offset" -compact -offset indent 484.It Dv p_type 485This member of the Phdr struct tells what kind of segment this array 486element describes or how to interpret the array element's information. 487.Bl -tag -width "PT_DYNAMIC" -compact 488.Pp 489.It Dv PT_NULL 490The array element is unused and the other members' values are undefined. 491This lets the program header have ignored entries. 492.It Dv PT_LOAD 493The array element specifies a loadable segment, described by 494.Sy p_filesz 495and 496.Sy p_memsz . 497The bytes from the file are mapped to the beginning of the memory 498segment. 499If the segment's memory size 500.Pq Sy p_memsz 501is larger than the file size 502.Pq Sy p_filesz , 503the 504.Dq extra 505bytes are defined to hold the value 0 and to follow the segment's 506initialized area. 507The file size may not be larger than the memory size. 508Loadable segment entries in the program header table appear in ascending 509order, sorted on the 510.Sy p_vaddr 511member. 512.It Dv PT_DYNAMIC 513The array element specifies dynamic linking information. 514.It Dv PT_INTERP 515The array element specifies the location and size of a null-terminated 516path name to invoke as an interpreter. 517This segment type is meaningful 518only for executable files (though it may occur for shared objects). However 519it may not occur more than once in a file. 520If it is present it must precede 521any loadable segment entry. 522.It Dv PT_NOTE 523The array element specifies the location and size for auxiliary information. 524.It Dv PT_SHLIB 525This segment type is reserved but has unspecified semantics. 526Programs that 527contain an array element of this type do not conform to the ABI. 528.It Dv PT_PHDR 529The array element, if present, specifies the location and size of the program 530header table itself, both in the file and in the memory image of the program. 531This segment type may not occur more than once in a file. 532Moreover, it may 533only occur if the program header table is part of the memory image of the 534program. 535If it is present it must precede any loadable segment entry. 536.It Dv PT_LOPROC 537This value up to and including 538.Sy PT_HIPROC 539are reserved for processor-specific semantics. 540.It Dv PT_HIPROC 541This value down to and including 542.Sy PT_LOPROC 543are reserved for processor-specific semantics. 544.El 545.Pp 546.It Dv p_offset 547This member holds the offset from the beginning of the file at which 548the first byte of the of the segment resides. 549.It Dv p_vaddr 550This member holds the virtual address at which the first byte of the 551segment resides in memory. 552.It Dv p_paddr 553On systems for which physical addressing is relevant, this member is 554reserved for the segment's physical address. 555Under BSD this member is 556not used and must be zero. 557.It Dv p_filesz 558This member holds the number of bytes in the file image of the segment. 559It may be zero. 560.It Dv p_memsz 561This member holds the number of bytes in the memory image of the segment. 562It may be zero. 563.It Dv p_flags 564This member holds flags relevant to the segment: 565.Pp 566.Bl -tag -width "PF_X" -compact 567.It Dv PF_X 568An executable segment. 569.It Dv PF_W 570A writable segment. 571.It Dv PF_R 572A readable segment. 573.El 574.Pp 575A text segment commonly has the flags 576.Sy PF_X 577and 578.Sy PF_R . 579A data segment commonly has 580.Sy PF_X , 581.Sy PF_W 582and 583.Sy PF_R . 584.It Dv p_align 585This member holds the value to which the segments are aligned in memory 586and in the file. 587Loadable process segments must have congruent values for 588.Sy p_vaddr 589and 590.Sy p_offset , 591modulo the page size. 592Values of zero and one mean no alignment is required. 593Otherwise, 594.Sy p_align 595should be a positive, integral power of two, and 596.Sy p_vaddr 597should equal 598.Sy p_offset , 599modulo 600.Sy p_align . 601.El 602.Pp 603An file's section header table lets one locate all the file's sections. 604The 605section header table is an array of Elf32_Shdr or Elf64_Shdr structures. 606The 607ELF header's 608.Sy e_shoff 609member gives the byte offset from the beginning of the file to the section 610header table. 611.Sy e_shnum 612holds the number of entries the section header table contains. 613.Sy e_shentsize 614holds the size in bytes of each entry. 615.Pp 616A section header table index is a subscript into this array. 617Some section 618header table indices are reserved. 619An object file does not have sections for 620these special indices: 621.Pp 622.Bl -tag -width "SHN_LORESERVE" -compact 623.It Dv SHN_UNDEF 624This value marks an undefined, missing, irrelevant or otherwise meaningless 625section reference. 626.It Dv SHN_LORESERVE 627This value specifies the lower bound of the range of reserved indices. 628.It Dv SHN_LOPROC 629This value up to and including 630.Sy SHN_HIPROC 631are reserved for processor-specific semantics. 632.It Dv SHN_HIPROC 633This value down to and including 634.Sy SHN_LOPROC 635are reserved for processor-specific semantics. 636.It Dv SHN_ABS 637This value specifies absolute values for the corresponding reference. 638For 639example, symbols defined relative to section number 640.Sy SHN_ABS 641have absolute values and are not affected by relocation. 642.It Dv SHN_COMMON 643Symbols defined relative to this section are common symbols, such as FORTRAN 644COMMON or unallocated C external variables. 645.It Dv SHN_HIRESERVE 646This value specifies the upper bound of the range of reserved indices. 647The 648system reserves indices between 649.Sy SHN_LORESERVE 650and 651.Sy SHN_HIRESERVE , 652inclusive. 653The section header table does not contain entries for the 654reserved indices. 655.El 656.Pp 657The section header has the following structure: 658.Bd -literal -offset indent 659typedef struct { 660 Elf32_Word sh_name; 661 Elf32_Word sh_type; 662 Elf32_Word sh_flags; 663 Elf32_Addr sh_addr; 664 Elf32_Off sh_offset; 665 Elf32_Size sh_size; 666 Elf32_Word sh_link; 667 Elf32_Word sh_info; 668 Elf32_Size sh_addralign; 669 Elf32_Size sh_entsize; 670} Elf32_Shdr; 671.Ed 672.Pp 673.Bd -literal -offset indent 674typedef struct { 675 Elf64_Half sh_name; 676 Elf64_Half sh_type; 677 Elf64_Size sh_flags; 678 Elf64_Addr sh_addr; 679 Elf64_Off sh_offset; 680 Elf64_Size sh_size; 681 Elf64_Half sh_link; 682 Elf64_Half sh_info; 683 Elf64_Size sh_addralign; 684 Elf64_Size sh_entsize; 685} Elf64_Shdr; 686.Ed 687.Pp 688.Bl -tag -width "sh_addralign" -compact 689.It Dv sh_name 690This member specifies the name of the section. 691Its value is an index 692into the section header string table section, giving the location of 693a null-terminated string. 694.It Dv sh_type 695This member categorizes the section's contents and semantics. 696.Pp 697.Bl -tag -width "SHT_PROGBITS" -compact 698.It Dv SHT_NULL 699This value marks the section header as inactive. 700It does not 701have an associated section. 702Other members of the section header 703have undefined values. 704.It Dv SHT_PROGBITS 705The section holds information defined by the program, whose 706format and meaning are determined solely by the program. 707.It Dv SHT_SYMTAB 708This section holds a symbol table. 709Typically, 710.Sy SHT_SYMTAB 711provides symbols for link editing, though it may also be used 712for dynamic linking. 713As a complete symbol table, it may contain 714many symbols unnecessary for dynamic linking. 715An object file can 716also contain a 717.Sy SHN_DYNSYM 718section. 719.It Dv SHT_STRTAB 720This section holds a string table. 721An object file may have multiple 722string table sections. 723.It Dv SHT_RELA 724This section holds relocation entries with explicit addends, such 725as type 726.Sy Elf32_Rela 727for the 32-bit class of object files. 728An object may have multiple 729relocation sections. 730.It Dv SHT_HASH 731This section holds a symbol hash table. 732All object participating in 733dynamic linking must contain a symbol hash table. 734An object file may 735have only one hash table. 736.It Dv SHT_DYNAMIC 737This section holds information for dynamic linking. 738An object file may 739have only one dynamic section. 740.It Dv SHT_NOTE 741This section holds information that marks the file in some way. 742.It Dv SHT_NOBITS 743A section of this type occupies no space in the file but otherwise 744resembles 745.Sy SHN_PROGBITS . 746Although this section contains no bytes, the 747.Sy sh_offset 748member contains the conceptual file offset. 749.It Dv SHT_REL 750This section holds relocation offsets without explicit addends, such 751as type 752.Sy Elf32_Rel 753for the 32-bit class of object files. 754An object file may have multiple 755relocation sections. 756.It Dv SHT_SHLIB 757This section is reserved but has unspecified semantics. 758.It Dv SHT_DYNSYM 759This section holds a minimal set of dynamic linking symbols. 760An 761object file can also contain a 762.Sy SHN_SYMTAB 763section. 764.It Dv SHT_LOPROC 765This value up to and including 766.Sy SHT_HIPROC 767are reserved for processor-specific semantics. 768.It Dv SHT_HIPROC 769This value down to and including 770.Sy SHT_LOPROC 771are reserved for processor-specific semantics. 772.It Dv SHT_LOUSER 773This value specifies the lower bound of the range of indices reserved for 774application programs. 775.It Dv SHT_HIUSER 776This value specifies the upper bound of the range of indices reserved for 777application programs. 778Section types between 779.Sy SHT_LOUSER 780and 781.Sy SHT_HIUSER 782may be used by the application, without conflicting with current or future 783system-defined section types. 784.El 785.Pp 786.It Dv sh_flags 787Sections support one-bit flags that describe miscellaneous attributes. 788If a flag bit is set in 789.Sy sh_flags , 790the attribute is 791.Dq on 792for the section. 793Otherwise, the attribute is 794.Dq off 795or does not apply. 796Undefined attributes are set to zero. 797.Pp 798.Bl -tag -width "SHF_EXECINSTR" -compact 799.It Dv SHF_WRITE 800This section contains data that should be writable during process 801execution. 802.It Dv SHF_ALLOC 803The section occupies memory during process execution. 804Some control 805sections do not reside in the memory image of an object file. 806This 807attribute is off for those sections. 808.It Dv SHF_EXECINSTR 809The section contains executable machine instructions. 810.It Dv SHF_MASKPROC 811All bits included in this mask are reserved for processor-specific 812semantics. 813.El 814.Pp 815.It Dv sh_addr 816If the section will appear in the memory image of a process, this member 817holds the address at which the section's first byte should reside. 818Otherwise, the member contains zero. 819.It Dv sh_offset 820This member's value holds the byte offset from the beginning of the file 821to the first byte in the section. 822One section type, 823.Sy SHT_NOBITS , 824occupies no space in the file, and its 825.Sy sh_offset 826member locates the conceptual placement in the file. 827.It Dv sh_size 828This member holds the section's size in bytes. 829Unless the section type 830is 831.Sy SHT_NOBITS , 832the section occupies 833.Sy sh_size 834bytes in the file. 835A section of type 836.Sy SHT_NOBITS 837may have a non-zero size, but it occupies no space in the file. 838.It Dv sh_link 839This member holds a section header table index link, whose interpretation 840depends on the section type. 841.It Dv sh_info 842This member holds extra information, whose interpretation depends on the 843section type. 844.It Dv sh_addralign 845Some sections have address alignment constraints. 846If a section holds a 847doubleword, the system must ensure doubleword alignment for the entire 848section. 849That is, the value of 850.Sy sh_addr 851must be congruent to zero, modulo the value of 852.Sy sh_addralign . 853Only zero and positive integral powers of two are allowed. 854Values of zero 855or one mean the section has no alignment constraints. 856.It Dv sh_entsize 857Some sections hold a table of fixed-sized entries, such as a symbol table. 858For such a section, this member gives the size in bytes for each entry. 859This member contains zero if the section does not hold a table of 860fixed-size entries. 861.El 862.Pp 863Various sections hold program and control information: 864.Bl -tag -width ".shstrtab" -compact 865.It .bss 866This section holds uninitialized data that contributes to the program's 867memory image. 868By definition, the system initializes the data with zeros 869when the program begins to run. 870This section is of type 871.Sy SHT_NOBITS . 872The attributes types are 873.Sy SHF_ALLOC 874and 875.Sy SHF_WRITE . 876.It .comment 877This section holds version control information. 878This section is of type 879.Sy SHT_PROGBITS . 880No attribute types are used. 881.It .data 882This section holds initialized data that contribute to the program's 883memory image. 884This section is of type 885.Sy SHT_PROGBITS . 886The attribute types are 887.Sy SHF_ALLOC 888and 889.Sy SHF_WRITE . 890.It .data1 891This section holds initialized data that contribute to the program's 892memory image. 893This section is of type 894.Sy SHT_PROGBITS . 895The attribute types are 896.Sy SHF_ALLOC 897and 898.Sy SHF_WRITE . 899.It .debug 900This section holds information for symbolic debugging. 901The contents 902are unspecified. 903This section is of type 904.Sy SHT_PROGBITS . 905No attribute types are used. 906.It .dynamic 907This section holds dynamic linking information. 908The section's attributes 909will include the 910.Sy SHF_ALLOC 911bit. 912Whether the 913.Sy SHF_WRITE 914bit is set is processor-specific. 915This section is of type 916.Sy SHT_DYNAMIC . 917See the attributes above. 918.It .dynstr 919This section holds strings needed for dynamic linking, most commonly 920the strings that represent the names associated with symbol table entries. 921This section is of type 922.Sy SHT_STRTAB . 923The attribute type used is 924.Sy SHF_ALLOC . 925.It .dynsym 926This section holds the dynamic linking symbol table. 927This section is of type 928.Sy SHT_DYNSYM . 929The attribute used is 930.Sy SHF_ALLOC . 931.It .fini 932This section holds executable instructions that contribute to the process 933termination code. 934When a program exits normally the system arranges to 935execute the code in this section. 936This section is of type 937.Sy SHT_PROGBITS . 938The attributes used are 939.Sy SHF_ALLOC 940and 941.Sy SHF_EXECINSTR . 942.It .got 943This section holds the global offset table. 944This section is of type 945.Sy SHT_PROGBITS . 946The attributes are processor-specific. 947.It .hash 948This section holds a symbol hash table. 949This section is of type 950.Sy SHT_HASH . 951The attribute used is 952.Sy SHF_ALLOC . 953.It .init 954This section holds executable instructions that contribute to the process 955initialization code. 956When a program starts to run the system arranges to 957execute the code in this section before calling the main program entry point. 958This section is of type 959.Sy SHT_PROGBITS . 960The attributes used are 961.Sy SHF_ALLOC 962and 963.Sy SHF_EXECINSTR . 964.It .interp 965This section holds the pathname of a program interpreter. 966If the file has 967a loadable segment that includes the section, the section's attributes will 968include the 969.Sy SHF_ALLOC 970bit. 971Otherwise, that bit will be off. 972This section is of type 973.Sy SHT_PROGBITS . 974.It .line 975This section holds line number information for symbolic debugging, which 976describes the correspondence between the program source and the machine code. 977The contents are unspecified. 978This section is of type 979.Sy SHT_PROGBITS . 980No attribute types are used. 981.It .note 982This section holds information in the 983.Dq Note Section 984format described below. 985This section is of type 986.Sy SHT_NOTE . 987No attribute types are used. 988.It .plt 989This section holds the procedure linkage table. 990This section is of type 991.Sy SHT_PROGBITS . 992The attributes are processor-specific. 993.It .relNAME 994This section holds relocation information as described below. 995If the file 996has a loadable segment that includes relocation, the section's attributes 997will include the 998.Sy SHF_ALLOC 999bit. 1000Otherwise the bit will be off. 1001By convention, 1002.Dq NAME 1003is supplied by the section to which the relocations apply. 1004Thus a relocation 1005section for 1006.Sy .text 1007normally would have the name 1008.Sy .rel.text . 1009This section is of type 1010.Sy SHT_REL . 1011.It .relaNAME 1012This section holds relocation information as described below. 1013If the file 1014has a loadable segment that includes relocation, the section's attributes 1015will include the 1016.Sy SHF_ALLOC 1017bit. 1018Otherwise the bit will be off. 1019By convention, 1020.Dq NAME 1021is supplied by the section to which the relocations apply. 1022Thus a relocation 1023section for 1024.Sy .text 1025normally would have the name 1026.Sy .rela.text . 1027This section is of type 1028.Sy SHT_RELA . 1029.It .rodata 1030This section holds read-only data that typically contributes to a 1031non-writable segment in the process image. 1032This section is of type 1033.Sy SHT_PROGBITS . 1034The attribute used is 1035.Sy SHF_ALLOC . 1036.It .rodata1 1037This section hold read-only data that typically contributes to a 1038non-writable segment in the process image. 1039This section is of type 1040.Sy SHT_PROGBITS . 1041The attribute used is 1042.Sy SHF_ALLOC . 1043.It .shstrtab 1044This section holds section names. 1045This section is of type 1046.Sy SHT_STRTAB . 1047No attribute types are used. 1048.It .strtab 1049This section holds strings, most commonly the strings that represent the 1050names associated with symbol table entries. 1051If the file has a loadable 1052segment that includes the symbol string table, the section's attributes 1053will include the 1054.Sy SHF_ALLOC 1055bit. 1056Otherwise the bit will be off. 1057This section is of type 1058.Sy SHT_STRTAB . 1059.It .symtab 1060This section holds a symbol table. 1061If the file has a loadable segment 1062that includes the symbol table, the section's attributes will include 1063the 1064.Sy SHF_ALLOC 1065bit. 1066Otherwise the bit will be off. 1067This section is of type 1068.Sy SHT_SYMTAB . 1069.It .text 1070This section holds the 1071.Dq text , 1072or executable instructions, of a program. 1073This section is of type 1074.Sy SHT_PROGBITS . 1075The attributes used are 1076.Sy SHF_ALLOC 1077and 1078.Sy SHF_EXECINSTR . 1079.El 1080.Pp 1081String table sections hold null-terminated character sequences, commonly 1082called strings. 1083The object file uses these strings to represent symbol 1084and section names. 1085One references a string as an index into the string 1086table section. 1087The first byte, which is index zero, is defined to hold 1088a null character. 1089Similarly, a string table's last byte is defined to 1090hold a null character, ensuring null termination for all strings. 1091.Pp 1092An object file's symbol table holds information needed to locate and 1093relocate a program's symbolic definitions and references. 1094A symbol table 1095index is a subscript into this array. 1096.Pp 1097.Bd -literal -offset indent 1098typedef struct { 1099 Elf32_Word st_name; 1100 Elf32_Addr st_value; 1101 Elf32_Size st_size; 1102 unsigned char st_info; 1103 unsigned char st_other; 1104 Elf32_Half st_shndx; 1105} Elf32_Sym; 1106.Ed 1107.Pp 1108.Bd -literal -offset indent 1109typedef struct { 1110 Elf64_Half st_name; 1111 unsigned char st_info; 1112 unsigned char st_other; 1113 Elf64_Quarter st_shndx; 1114 Elf64_Addr st_value; 1115 Elf64_Size st_size; 1116} Elf64_Sym; 1117.Ed 1118.Pp 1119.Bl -tag -width "st_value" -compact 1120.It Dv st_name 1121This member holds an index into the object file's symbol string table, 1122which holds character representations of the symbol names. 1123If the value 1124is non-zero, it represents a string table index that gives the symbol 1125name. 1126Otherwise, the symbol table has no name. 1127.It Dv st_value 1128This member gives the value of the associated symbol. 1129.It Dv st_size 1130Many symbols have associated sizes. 1131This member holds zero if the symbol 1132has no size or an unknown size. 1133.It Dv st_info 1134This member specifies the symbol's type and binding attributes: 1135.Pp 1136.Bl -tag -width "STT_SECTION" -compact 1137.It Dv STT_NOTYPE 1138The symbol's type is not defined. 1139.It Dv STT_OBJECT 1140The symbol is associated with a data object. 1141.It Dv STT_FUNC 1142The symbol is associated with a function or other executable code. 1143.It Dv STT_SECTION 1144The symbol is associated with a section. 1145Symbol table entries of 1146this type exist primarily for relocation and normally have 1147.Sy STB_LOCAL 1148bindings. 1149.It Dv STT_FILE 1150By convention the symbol's name gives the name of the source file 1151associated with the object file. 1152A file symbol has 1153.Sy STB_LOCAL 1154bindings, its section index is 1155.Sy SHN_ABS , 1156and it precedes the other 1157.Sy STB_LOCAL 1158symbols of the file, if it is present. 1159.It Dv STT_LOPROC 1160This value up to and including 1161.Sy STT_HIPROC 1162are reserved for processor-specific semantics. 1163.It Dv STT_HIPROC 1164This value down to and including 1165.Sy STT_LOPROC 1166are reserved for processor-specific semantics. 1167.El 1168.Pp 1169.Bl -tag -width "STB_GLOBAL" -compact 1170.It Dv STB_LOCAL 1171Local symbols are not visible outside the object file containing their 1172definition. 1173Local symbols of the same name may exist in multiple file 1174without interfering with each other. 1175.It Dv STB_GLOBAL 1176Global symbols are visible to all object files being combined. 1177One file's 1178definition of a global symbol will satisfy another file's undefined 1179reference to the same symbol. 1180.It Dv STB_WEAK 1181Weak symbols resemble global symbols, but their definitions have lower 1182precedence. 1183.It Dv STB_LOPROC 1184This value up to and including 1185.Sy STB_HIPROC 1186are reserved for processor-specific semantics. 1187.It Dv STB_HIPROC 1188This value down to and including 1189.Sy STB_LOPROC 1190are reserved for processor-specific semantics. 1191.Pp 1192There are macros for packing and unpacking the binding and type fields: 1193.Pp 1194.Bl -tag -width "ELF32_ST_INFO(bind, type)" -compact 1195.It Xo 1196.Fn ELF32_ST_BIND info 1197.Xc 1198or 1199.Fn ELF64_ST_BIND info 1200extract a binding from an st_info value. 1201.It Xo 1202.Fn ELF64_ST_TYPE info 1203.Xc 1204or 1205.Fn ELF32_ST_TYPE info 1206extract a type from an st_info value. 1207.It Xo 1208.Fn ELF32_ST_INFO bind type 1209.Xc 1210or 1211.Fn ELF64_ST_INFO bind type 1212convert a binding and a type into an st_info value. 1213.El 1214.El 1215.Pp 1216.It Dv st_other 1217This member currently holds zero and has no defined meaning. 1218.It Dv st_shndx 1219Every symbol table entry is 1220.Dq defined 1221in relation to some action. 1222This member holds the relevant section 1223header table index. 1224.El 1225.Pp 1226Relocation is the process of connecting symbolic references with 1227symbolic definitions. 1228Relocatable files must have information that 1229describes how to modify their section contents, thus allowing executable 1230and shared object files to hold the right information for a process' 1231program image. 1232Relocation entries are these data. 1233.Pp 1234Relocation structures that do not need an addend: 1235.Pp 1236.Bd -literal -offset indent 1237typedef struct { 1238 Elf32_Addr r_offset; 1239 Elf32_Word r_info; 1240} Elf32_Rel; 1241.Ed 1242.Bd -literal -offset indent 1243typedef struct { 1244 Elf64_Addr r_offset; 1245 Elf64_Size r_info; 1246} Elf64_Rel; 1247.Ed 1248.Pp 1249Relocation structures that need an addend: 1250.Pp 1251.Bd -literal -offset indent 1252typedef struct { 1253 Elf32_Addr r_offset; 1254 Elf32_Word r_info; 1255 Elf32_Sword r_addend; 1256} Elf32_Rela; 1257.Ed 1258.Bd -literal -offset indent 1259typedef struct { 1260 Elf64_Addr r_offset; 1261 Elf64_Size r_info; 1262 Elf64_Off r_addend; 1263} Elf64_Rela; 1264.Ed 1265.Pp 1266.Bl -tag -width "r_offset" -compact 1267.It Dv r_offset 1268This member gives the location at which to apply the relocation action. 1269For a relocatable file, the value is the byte offset from the beginning 1270of the section to the storage unit affected by the relocation. 1271For an 1272executable file or shared object, the value is the virtual address of 1273the storage unit affected by the relocation. 1274.It Dv r_info 1275This member gives both the symbol table index with respect to which the 1276relocation must be made and the type of relocation to apply. 1277Relocation 1278types are processor-specific. 1279When the text refers to a relocation 1280entry's relocation type or symbol table index, it means the result of 1281applying 1282.Sy ELF_[32|64]_R_TYPE 1283or 1284.Sy ELF[32|64]_R_SYM , 1285respectively to the entry's 1286.Sy r_info 1287member. 1288.It Dv r_addend 1289This member specifies a constant addend used to compute the value to be 1290stored into the relocatable field. 1291.El 1292.Sh SEE ALSO 1293.Xr as 1 , 1294.Xr gdb 1 , 1295.Xr ld 1 , 1296.Xr objdump 1 , 1297.Xr execve 2 , 1298.Xr core 5 1299.Rs 1300.%A Hewlett Packard 1301.%B Elf-64 Object File Format 1302.Re 1303.Rs 1304.%A Santa Cruz Operation 1305.%B System V Application Binary Interface 1306.Re 1307.Rs 1308.%A Unix System Laboratories 1309.%T Object Files 1310.%B "Executable and Linking Format (ELF)" 1311.Re 1312.Sh HISTORY 1313The ELF header files made their appearance in 1314.Fx 2.2.6 . 1315ELF in itself first appeared in 1316.At V . 1317The ELF format is an adopted standard. 1318.Sh AUTHORS 1319This manual page was written by 1320.An Jeroen Ruigrok van der Werven 1321.Aq asmodai@FreeBSD.org 1322with inspiration from BSDi's BSD/OS 1323.Xr elf 5 1324manpage. 1325