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 3.3 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_VERSION" -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_PAD 222Start of padding. 223These bytes are reserved and set to zero. 224Programs 225which read them should ignore them. 226The value for EI_PAD will change in 227the future if currently unused bytes are given meanings. 228.It Dv EI_BRAND 229Start of architecture identification. 230.It Dv EI_NIDENT 231The size of the e_ident array. 232.El 233.Pp 234.It Dv e_type 235This member of the structure identifies the object file type: 236.Pp 237.Bl -tag -width "ET_NONE" -compact 238.It Dv ET_NONE 239An unknown type. 240.It Dv ET_REL 241A relocatable file. 242.It Dv ET_EXEC 243An executable file. 244.It Dv ET_DYN 245A shared object. 246.It Dv ET_CORE 247A core file. 248.El 249.Pp 250.It Dv e_machine 251This member specifies the required architecture for an individual file: 252.Pp 253.Bl -tag -width "EM_MIPS_RS4_BE" -compact 254.It Dv EM_NONE 255An unknown machine. 256.It Dv EM_M32 257AT&T WE 32100. 258.It Dv EM_SPARC 259Sun Microsystems SPARC. 260.It Dv EM_386 261Intel 80386. 262.It Dv EM_68K 263Motorola 68000. 264.It Dv EM_88K 265Motorola 88000. 266.It Dv EM_486 267Intel 80486. 268.It Dv EM_860 269Intel 80860. 270.It Dv EM_MIPS 271MIPS RS3000 (big-endian only). 272.It Dv EM_MIPS_RS4_BE 273MIPS RS4000 (big-endian only). 274.It Dv EM_SPARC64 275SPARC v9 64-bit unofficial. 276.It Dv EM_PARISC 277HPPA. 278.It Dv EM_PPC 279PowerPC. 280.It Dv EM_ALPHA 281Compaq [DEC] Alpha. 282.El 283.Pp 284.It Dv e_version 285This member identifies the file version: 286.Pp 287.Bl -tag -width "EV_CURRENT" -compact 288.It Dv EV_NONE 289Invalid version 290.It Dv EV_CURRENT 291Current version 292.El 293.It Dv e_entry 294This member gives the virtual address to which the system first transfers 295control, thus starting the process. 296If the file has no associated entry 297point, this member holds zero. 298.It Dv e_phoff 299This member holds the program header table's file offset in bytes. 300If 301the file has no program header table, this member holds zero. 302.It Dv e_shoff 303This member holds the section header table's file offset in bytes. 304If the 305file has no section header table this member holds zero. 306.It Dv e_flags 307This member holds processor-specific flags associated with the file. 308Flag 309names take the form EF_`machine_flag'. Currently no flags have been defined. 310.It Dv e_ehsize 311This member holds the ELF header's size in bytes. 312.It Dv e_phentsize 313This member holds the size in bytes of one entry in the file's program header 314table; all entries are the same size. 315.It Dv e_phnum 316This member holds the number of entries in the program header 317table. 318Thus the product of 319.Sy e_phentsize 320and 321.Sy e_phnum 322gives the table's size 323in bytes. 324If a file has no program header, 325.Sy e_phnum 326holds the value zero. 327.It Dv e_shentsize 328This member holds a sections header's size in bytes. 329A section header is one 330entry in the section header table; all entries are the same size. 331.It Dv e_shnum 332This member holds the number of entries in the section header table. 333Thus 334the product of 335.Sy e_shentsize 336and 337.Sy e_shnum 338gives the section header table's size in bytes. 339If a file has no section 340header table, 341.Sy e_shnum 342holds the value of zero. 343.It Dv e_shstrndx 344This member holds the section header table index of the entry associated 345with the section name string table. 346If the file has no section name string 347table, this member holds the value 348.Sy SHN_UNDEF . 349.Pp 350.Bl -tag -width "SHN_LORESERVE" -compact 351.It Dv SHN_UNDEF 352This value marks an undefined, missing, irrelevant, or otherwise meaningless 353section reference. 354For example, a symbol 355.Dq defined 356relative to section number 357.Sy SHN_UNDEF 358is an undefined symbol. 359.It Dv SHN_LORESERVE 360This value specifies the lower bound of the range of reserved indexes. 361.It Dv SHN_LOPROC 362This value up to and including 363.Sy SHN_HIPROC 364are reserved for processor-specific semantics. 365.It Dv SHN_HIPROC 366This value down to and including 367.Sy SHN_LOPROC 368are reserved for processor-specific semantics. 369.It Dv SHN_ABS 370This value specifies absolute values for the corresponding reference. 371For 372example, symbols defined relative to section number 373.Sy SHN_ABS 374have absolute values and are not affected by relocation. 375.It Dv SHN_COMMON 376Symbols defined relative to this section are common symbols, such as Fortran 377COMMON or unallocated C external variables. 378.It Dv SHN_HIRESERVE 379This value specifies the upper bound of the range of the range of reserved 380indices between 381.Sy SHN_LORESERVE 382and 383.Sy SHN_HIRESERVE , 384inclusive; the values do 385not reference the section header table. 386That is, the section header table 387does 388.Em not 389contain entries for the reserved indices. 390.El 391.El 392.Pp 393An executable or shared object file's program header table is an array of 394structures, each describing a segment or other information the system needs 395to prepare the program for execution. 396An object file 397.Em segment 398contains one or more 399.Em sections . 400Program headers are meaningful only for executable and shared object files. 401A file specifies its own program header size with the ELF header's 402.Sy e_phentsize 403and 404.Sy e_phnum 405members. 406As with the Elf executable header, the program header 407also has different versions depending on the architecture: 408.Pp 409.Bd -literal -offset indent 410typedef struct { 411 Elf32_Word p_type; 412 Elf32_Off p_offset; 413 Elf32_Addr p_vaddr; 414 Elf32_Addr p_paddr; 415 Elf32_Size p_filesz; 416 Elf32_Size p_memsz; 417 Elf32_Word p_flags; 418 Elf32_Size p_align; 419} Elf32_Phdr; 420.Ed 421.Pp 422.Bd -literal -offset indent 423typedef struct { 424 Elf64_Half p_type; 425 Elf64_Half p_flags; 426 Elf64_Off p_offset; 427 Elf64_Addr p_vaddr; 428 Elf64_Addr p_paddr; 429 Elf64_Size p_filesz; 430 Elf64_Size p_memsz; 431 Elf64_Size p_align; 432} Elf64_Phdr; 433.Ed 434.Pp 435The main difference between the 32-bit and the 64-bit program header lies 436only in the location of a 437.Sy p_flags 438member in the total struct. 439.Pp 440.Bl -tag -width "p_offset" -compact -offset indent 441.It Dv p_type 442This member of the Phdr struct tells what kind of segment this array 443element describes or how to interpret the array element's information. 444.Bl -tag -width "PT_DYNAMIC" -compact 445.Pp 446.It Dv PT_NULL 447The array element is unused and the other members' values are undefined. 448This lets the program header have ignored entries. 449.It Dv PT_LOAD 450The array element specifies a loadable segment, described by 451.Sy p_filesz 452and 453.Sy p_memsz . 454The bytes from the file are mapped to the beginning of the memory 455segment. 456If the segment's memory size ( 457.Sy p_memsz 458) is larger than the file 459size ( 460.Sy p_filesz 461), the 462.Dq extra 463bytes are defined to hold the value 0 and to follow the segment's 464initialized area. 465The file size may not be larger than the memory size. 466Loadable segment entries in the program header table appear in ascending 467order, sorted on the 468.Sy p_vaddr 469member. 470.It Dv PT_DYNAMIC 471The array element specifies dynamic linking information. 472.It Dv PT_INTERP 473The array element specifies the location and size of a null-terminated 474path name to invoke as an interpreter. 475This segment type is meaningful 476only for executable files (though it may occur for shared objects). However 477it may not occur more than once in a file. 478If it is present it must precede 479any loadable segment entry. 480.It Dv PT_NOTE 481The array element specifies the location and size for auxiliary information. 482.It Dv PT_SHLIB 483This segment type is reserved but has unspecified semantics. 484Programs that 485contain an array element of this type do not conform to the ABI. 486.It Dv PT_PHDR 487The array element, if present, specifies the location and size of the program 488header table itself, both in the file and in the memory image of the program. 489This segment type may not occur more than once in a file. 490Moreover, it may 491only occur if the program header table is part of the memory image of the 492program. 493If it is present it must precede any loadable segment entry. 494.It Dv PT_LOPROC 495This value up to and including 496.Sy PT_HIPROC 497are reserved for processor-specific semantics. 498.It Dv PT_HIPROC 499This value down to and including 500.Sy PT_LOPROC 501are reserved for processor-specific semantics. 502.El 503.Pp 504.It Dv p_offset 505This member holds the offset from the beginning of the file at which 506the first byte of the of the segment resides. 507.It Dv p_vaddr 508This member holds the virtual address at which the first byte of the 509segment resides in memory. 510.It Dv p_paddr 511On systems for which physical addressing is relevant, this member is 512reserved for the segment's physical address. 513Under BSD this member is 514not used and must be zero. 515.It Dv p_filesz 516This member holds the number of bytes in the file image of the segment. 517It may be zero. 518.It Dv p_memsz 519This member holds the number of bytes in the memory image of the segment. 520It may be zero. 521.It Dv p_flags 522This member holds flags relevant to the segment: 523.Pp 524.Bl -tag -width "PF_X" -compact 525.It Dv PF_X 526An executable segment. 527.It Dv PF_W 528A writable segment. 529.It Dv PF_R 530A readable segment. 531.El 532.Pp 533A text segment commonly has the flags 534.Sy PF_X 535and 536.Sy PF_R . 537A data segment commonly has 538.Sy PF_X , 539.Sy PF_W 540and 541.Sy PF_R . 542.It Dv p_align 543This member holds the value to which the segments are aligned in memory 544and in the file. 545Loadable process segments must have congruent values for 546.Sy p_vaddr 547and 548.Sy p_offset , 549modulo the page size. 550Values of zero and one mean no alignment is required. 551Otherwise, 552.Sy p_align 553should be a positive, integral power of two, and 554.Sy p_vaddr 555should equal 556.Sy p_offset , 557modulo 558.Sy p_align . 559.El 560.Pp 561An file's section header table lets one locate all the file's sections. 562The 563section header table is an array of Elf32_Shdr or Elf64_Shdr structures. 564The 565ELF header's 566.Sy e_shoff 567member gives the byte offset from the beginning of the file to the section 568header table. 569.Sy e_shnum 570holds the number of entries the section header table contains. 571.Sy e_shentsize 572holds the size in bytes of each entry. 573.Pp 574A section header table index is a subscript into this array. 575Some section 576header table indices are reserved. 577An object file does not have sections for 578these special indices: 579.Pp 580.Bl -tag -width "SHN_LORESERVE" -compact 581.It Dv SHN_UNDEF 582This value marks an undefined, missing, irrelevant or otherwise meaningless 583section reference. 584.It Dv SHN_LORESERVE 585This value specifies the lower bound of the range of reserved indices. 586.It Dv SHN_LOPROC 587This value up to and including 588.Sy SHN_HIPROC 589are reserved for processor-specific semantics. 590.It Dv SHN_HIPROC 591This value down to and including 592.Sy SHN_LOPROC 593are reserved for processor-specific semantics. 594.It Dv SHN_ABS 595This value specifies absolute values for the corresponding reference. 596For 597example, symbols defined relative to section number 598.Sy SHN_ABS 599have absolute values and are not affected by relocation. 600.It Dv SHN_COMMON 601Symbols defined relative to this section are common symbols, such as FORTRAN 602COMMON or unallocated C external variables. 603.It Dv SHN_HIRESERVE 604This value specifies the upper bound of the range of reserved indices. 605The 606system reserves indices between 607.Sy SHN_LORESERVE 608and 609.Sy SHN_HIRESERVE, 610inclusive. 611The section header table does not contain entries for the 612reserved indices. 613.El 614.Pp 615The section header has the following structure: 616.Bd -literal -offset indent 617typedef struct { 618 Elf32_Word sh_name; 619 Elf32_Word sh_type; 620 Elf32_Word sh_flags; 621 Elf32_Addr sh_addr; 622 Elf32_Off sh_offset; 623 Elf32_Size sh_size; 624 Elf32_Word sh_link; 625 Elf32_Word sh_info; 626 Elf32_Size sh_addralign; 627 Elf32_Size sh_entsize; 628} Elf32_Shdr; 629.Ed 630.Pp 631.Bd -literal -offset indent 632typedef struct { 633 Elf64_Half sh_name; 634 Elf64_Half sh_type; 635 Elf64_Size sh_flags; 636 Elf64_Addr sh_addr; 637 Elf64_Off sh_offset; 638 Elf64_Size sh_size; 639 Elf64_Half sh_link; 640 Elf64_Half sh_info; 641 Elf64_Size sh_addralign; 642 Elf64_Size sh_entsize; 643} Elf64_Shdr; 644.Ed 645.Pp 646.Bl -tag -width "sh_addralign" -compact 647.It Dv sh_name 648This member specifies the name of the section. 649Its value is an index 650into the section header string table section, giving the location of 651a null-terminated string. 652.It Dv sh_type 653This member categorizes the section's contents and semantics. 654.Pp 655.Bl -tag -width "SHT_PROGBITS" -compact 656.It Dv SHT_NULL 657This value marks the section header as inactive. 658It does not 659have an associated section. 660Other members of the section header 661have undefined values. 662.It Dv SHT_PROGBITS 663The section holds information defined by the program, whose 664format and meaning are determined solely by the program. 665.It Dv SHT_SYMTAB 666This section holds a symbol table. 667Typically, 668.Sy SHT_SYMTAB 669provides symbols for link editing, though it may also be used 670for dynamic linking. 671As a complete symbol table, it may contain 672many symbols unnecessary for dynamic linking. 673An object file can 674also contain a 675.Sy SHN_DYNSYM 676section. 677.It Dv SHT_STRTAB 678This section holds a string table. 679An object file may have multiple 680string table sections. 681.It Dv SHT_RELA 682This section holds relocation entries with explicit addends, such 683as type 684.Sy Elf32_Rela 685for the 32-bit class of object files. 686An object may have multiple 687relocation sections. 688.It Dv SHT_HASH 689This section holds a symbol hash table. 690All object participating in 691dynamic linking must contain a symbol hash table. 692An object file may 693have only one hash table. 694.It Dv SHT_DYNAMIC 695This section holds information for dynamic linking. 696An object file may 697have only one dynamic section. 698.It Dv SHT_NOTE 699This section holds information that marks the file in some way. 700.It Dv SHT_NOBITS 701A section of this type occupies no space in the file but otherwise 702resembles 703.Sy SHN_PROGBITS . 704Although this section contains no bytes, the 705.Sy sh_offset 706member contains the conceptual file offset. 707.It Dv SHT_REL 708This section holds relocation offsets without explicit addends, such 709as type 710.Sy Elf32_Rel 711for the 32-bit class of object files. 712An object file may have multiple 713relocation sections. 714.It Dv SHT_SHLIB 715This section is reserved but has unspecified semantics. 716.It Dv SHT_DYNSYM 717This section holds a minimal set of dynamic linking symbols. 718An 719object file can also contain a 720.Sy SHN_SYMTAB 721section. 722.It Dv SHT_LOPROC 723This value up to and including 724.Sy SHT_HIPROC 725are reserved for processor-specific semantics. 726.It Dv SHT_HIPROC 727This value down to and including 728.Sy SHT_LOPROC 729are reserved for processor-specific semantics. 730.It Dv SHT_LOUSER 731This value specifies the lower bound of the range of indices reserved for 732application programs. 733.It Dv SHT_HIUSER 734This value specifies the upper bound of the range of indices reserved for 735application programs. 736Section types between 737.Sy SHT_LOUSER 738and 739.Sy SHT_HIUSER 740may be used by the application, without conflicting with current or future 741system-defined section types. 742.El 743.Pp 744.It Dv sh_flags 745Sections support one-bit flags that describe miscellaneous attributes. 746If a flag bit is set in 747.Sy sh_flags , 748the attribute is 749.Dq on 750for the section. 751Otherwise, the attribute is 752.Dq off 753or does not apply. 754Undefined attributes are set to zero. 755.Pp 756.Bl -tag -width "SHF_EXECINSTR" -compact 757.It Dv SHF_WRITE 758This section contains data that should be writable during process 759execution. 760.It Dv SHF_ALLOC 761The section occupies memory during process execution. 762Some control 763sections do not reside in the memory image of an object file. 764This 765attribute is off for those sections. 766.It Dv SHF_EXECINSTR 767The section contains executable machine instructions. 768.It Dv SHF_MASKPROC 769All bits included in this mask are reserved for processor-specific 770semantics. 771.El 772.Pp 773.It Dv sh_addr 774If the section will appear in the memory image of a process, this member 775holds the address at which the section's first byte should reside. 776Otherwise, the member contains zero. 777.It Dv sh_offset 778This member's value holds the byte offset from the beginning of the file 779to the first byte in the section. 780One section type, 781.Sy SHT_NOBITS , 782occupies no space in the file, and its 783.Sy sh_offset 784member locates the conceptual placement in the file. 785.It Dv sh_size 786This member holds the section's size in bytes. 787Unless the section type 788is 789.Sy SHT_NOBITS , 790the section occupies 791.Sy sh_size 792bytes in the file. 793A section of type 794.Sy SHT_NOBITS 795may have a non-zero size, but it occupies no space in the file. 796.It Dv sh_link 797This member holds a section header table index link, whose interpretation 798depends on the section type. 799.It Dv sh_info 800This member holds extra information, whose interpretation depends on the 801section type. 802.It Dv sh_addralign 803Some sections have address alignment constraints. 804If a section holds a 805doubleword, the system must ensure doubleword alignment for the entire 806section. 807That is, the value of 808.Sy sh_addr 809must be congruent to zero, modulo the value of 810.Sy sh_addralign . 811Only zero and positive integral powers of two are allowed. 812Values of zero 813or one mean the section has no alignment constraints. 814.It Dv sh_entsize 815Some sections hold a table of fixed-sized entries, such as a symbol table. 816For such a section, this member gives the size in bytes for each entry. 817This member contains zero if the section does not hold a table of 818fixed-size entries. 819.El 820.Pp 821Various sections hold program and control information: 822.Bl -tag -width ".shstrtab" -compact 823.It .bss 824This section holds uninitialized data that contributes to the program's 825memory image. 826By definition, the system initializes the data with zeros 827when the program begins to run. 828This section is of type 829.Sy SHT_NOBITS . 830The attributes types are 831.Sy SHF_ALLOC 832and 833.Sy SHF_WRITE . 834.It .comment 835This section holds version control information. 836This section is of type 837.Sy SHT_PROGBITS . 838No attribute types are used. 839.It .data 840This section holds initialized data that contribute to the program's 841memory image. 842This section is of type 843.Sy SHT_PROGBITS . 844The attribute types are 845.Sy SHF_ALLOC 846and 847.Sy SHF_WRITE . 848.It .data1 849This section holds initialized data that contribute to the program's 850memory image. 851This section is of type 852.Sy SHT_PROGBITS . 853The attribute types are 854.Sy SHF_ALLOC 855and 856.Sy SHF_WRITE . 857.It .debug 858This section holds information for symbolic debugging. 859The contents 860are unspecified. 861This section is of type 862.Sy SHT_PROGBITS . 863No attribute types are used. 864.It .dynamic 865This section holds dynamic linking information. 866The section's attributes 867will include the 868.Sy SHF_ALLOC 869bit. 870Whether the 871.Sy SHF_WRITE 872bit is set is processor-specific. 873This section is of type 874.Sy SHT_DYNAMIC . 875See the attributes above. 876.It .dynstr 877This section holds strings needed for dynamic linking, most commonly 878the strings that represent the names associated with symbol table entries. 879This section is of type 880.Sy SHT_STRTAB . 881The attribute type used is 882.Sy SHF_ALLOC . 883.It .dynsym 884This section holds the dynamic linking symbol table. 885This section is of type 886.Sy SHT_DYNSYM . 887The attribute used is 888.Sy SHF_ALLOC . 889.It .fini 890This section holds executable instructions that contribute to the process 891termination code. 892When a program exits normally the system arranges to 893execute the code in this section. 894This section is of type 895.Sy SHT_PROGBITS . 896The attributes used are 897.Sy SHF_ALLOC 898and 899.Sy SHF_EXECINSTR . 900.It .got 901This section holds the global offset table. 902This section is of type 903.Sy SHT_PROGBITS . 904The attributes are processor-specific. 905.It .hash 906This section holds a symbol hash table. 907This section is of type 908.Sy SHT_HASH . 909The attribute used is 910.Sy SHF_ALLOC . 911.It .init 912This section holds executable instructions that contribute to the process 913initialization code. 914When a program starts to run the system arranges to 915execute the code in this section before calling the main program entry point. 916This section is of type 917.Sy SHT_PROGBITS . 918The attributes used are 919.Sy SHF_ALLOC 920and 921.Sy SHF_EXECINSTR . 922.It .interp 923This section holds the pathname of a program interpreter. 924If the file has 925a loadable segment that includes the section, the section's attributes will 926include the 927.Sy SHF_ALLOC 928bit. 929Otherwise, that bit will be off. 930This section is of type 931.Sy SHT_PROGBITS . 932.It .line 933This section holds line number information for symbolic debugging, which 934describes the correspondence between the program source and the machine code. 935The contents are unspecified. 936This section is of type 937.Sy SHT_PROGBITS . 938No attribute types are used. 939.It .note 940This section holds information in the 941.Dq Note Section 942format described below. 943This section is of type 944.Sy SHT_NOTE . 945No attribute types are used. 946.It .plt 947This section holds the procedure linkage table. 948This section is of type 949.Sy SHT_PROGBITS . 950The attributes are processor-specific. 951.It .relNAME 952This section holds relocation information as described below. 953If the file 954has a loadable segment that includes relocation, the section's attributes 955will include the 956.Sy SHF_ALLOC 957bit. 958Otherwise the bit will be off. 959By convention, 960.Dq NAME 961is supplied by the section to which the relocations apply. 962Thus a relocation 963section for 964.Sy .text 965normally would have the name 966.Sy .rel.text . 967This section is of type 968.Sy SHT_REL . 969.It .relaNAME 970This section holds relocation information as described below. 971If the file 972has a loadable segment that includes relocation, the section's attributes 973will include the 974.Sy SHF_ALLOC 975bit. 976Otherwise the bit will be off. 977By convention, 978.Dq NAME 979is supplied by the section to which the relocations apply. 980Thus a relocation 981section for 982.Sy .text 983normally would have the name 984.Sy .rela.text . 985This section is of type 986.Sy SHT_RELA . 987.It .rodata 988This section holds read-only data that typically contributes to a 989non-writable segment in the process image. 990This section is of type 991.Sy SHT_PROGBITS . 992The attribute used is 993.Sy SHF_ALLOC . 994.It .rodata1 995This section hold read-only data that typically contributes to a 996non-writable segment in the process image. 997This section is of type 998.Sy SHT_PROGBITS . 999The attribute used is 1000.Sy SHF_ALLOC . 1001.It .shstrtab 1002This section holds section names. 1003This section is of type 1004.Sy SHT_STRTAB . 1005No attribute types are used. 1006.It .strtab 1007This section holds strings, most commonly the strings that represent the 1008names associated with symbol table entries. 1009If the file has a loadable 1010segment that includes the symbol string table, the section's attributes 1011will include the 1012.Sy SHF_ALLOC 1013bit. 1014Otherwise the bit will be off. 1015This section is of type 1016.Sy SHT_STRTAB . 1017.It .symtab 1018This section holds a symbol table. 1019If the file has a loadable segment 1020that includes the symbol table, the section's attributes will include 1021the 1022.Sy SHF_ALLOC 1023bit. 1024Otherwise the bit will be off. 1025This section is of type 1026.Sy SHT_SYMTAB . 1027.It .text 1028This section holds the 1029.Dq text , 1030or executable instructions, of a program. 1031This section is of type 1032.Sy SHT_PROGBITS . 1033The attributes used are 1034.Sy SHF_ALLOC 1035and 1036.Sy SHF_EXECINSTR . 1037.El 1038.Pp 1039String table sections hold null-terminated character sequences, commonly 1040called strings. 1041The object file uses these strings to represent symbol 1042and section names. 1043One references a string as an index into the string 1044table section. 1045The first byte, which is index zero, is defined to hold 1046a null character. 1047Similarly, a string table's last byte is defined to 1048hold a null character, ensuring null termination for all strings. 1049.Pp 1050An object file's symbol table holds information needed to locate and 1051relocate a program's symbolic definitions and references. 1052A symbol table 1053index is a subscript into this array. 1054.Pp 1055.Bd -literal -offset indent 1056typedef struct { 1057 Elf32_Word st_name; 1058 Elf32_Addr st_value; 1059 Elf32_Size st_size; 1060 unsigned char st_info; 1061 unsigned char st_other; 1062 Elf32_Half st_shndx; 1063} Elf32_Sym; 1064.Ed 1065.Pp 1066.Bd -literal -offset indent 1067typedef struct { 1068 Elf64_Half st_name; 1069 unsigned char st_info; 1070 unsigned char st_other; 1071 Elf64_Quarter st_shndx; 1072 Elf64_Addr st_value; 1073 Elf64_Size st_size; 1074} Elf64_Sym; 1075.Ed 1076.Pp 1077.Bl -tag -width "st_value" -compact 1078.It Dv st_name 1079This member holds an index into the object file's symbol string table, 1080which holds character representations of the symbol names. 1081If the value 1082is non-zero, it represents a string table index that gives the symbol 1083name. 1084Otherwise, the symbol table has no name. 1085.It Dv st_value 1086This member gives the value of the associated symbol. 1087.It Dv st_size 1088Many symbols have associated sizes. 1089This member holds zero if the symbol 1090has no size or an unknown size. 1091.It Dv st_info 1092This member specifies the symbol's type and binding attributes: 1093.Pp 1094.Bl -tag -width "STT_SECTION" -compact 1095.It Dv STT_NOTYPE 1096The symbol's type is not defined. 1097.It Dv STT_OBJECT 1098The symbol is associated with a data object. 1099.It Dv STT_FUNC 1100The symbol is associated with a function or other executable code. 1101.It Dv STT_SECTION 1102The symbol is associated with a section. 1103Symbol table entries of 1104this type exist primarily for relocation and normally have 1105.Sy STB_LOCAL 1106bindings. 1107.It Dv STT_FILE 1108By convention the symbol's name gives the name of the source file 1109associated with the object file. 1110A file symbol has 1111.Sy STB_LOCAL 1112bindings, its section index is 1113.Sy SHN_ABS , 1114and it precedes the other 1115.Sy STB_LOCAL 1116symbols of the file, if it is present. 1117.It Dv STT_LOPROC 1118This value up to and including 1119.Sy STT_HIPROC 1120are reserved for processor-specific semantics. 1121.It Dv STT_HIPROC 1122This value down to and including 1123.Sy STT_LOPROC 1124are reserved for processor-specific semantics. 1125.El 1126.Pp 1127.Bl -tag -width "STB_GLOBAL" -compact 1128.It Dv STB_LOCAL 1129Local symbols are not visible outside the object file containing their 1130definition. 1131Local symbols of the same name may exist in multiple file 1132without interfering with each other. 1133.It Dv STB_GLOBAL 1134Global symbols are visible to all object files being combined. 1135One file's 1136definition of a global symbol will satisfy another file's undefined 1137reference to the same symbol. 1138.It Dv STB_WEAK 1139Weak symbols resemble global symbols, but their definitions have lower 1140precedence. 1141.It Dv STB_LOPROC 1142This value up to and including 1143.Sy STB_HIPROC 1144are reserved for processor-specific semantics. 1145.It Dv STB_HIPROC 1146This value down to and including 1147.Sy STB_LOPROC 1148are reserved for processor-specific semantics. 1149.Pp 1150There are macros for packing and unpacking the binding and type fields: 1151.Bl -tag -width "ELF32_ST_INFO(bind, type)" -compact 1152.It Dv ELF32_ST_BIND(info) 1153or 1154.Sy ELF64_ST_BIND(info) 1155extract a binding from an st_info value. 1156.It Dv ELF64_ST_TYPE(info) 1157or 1158.Sy ELF32_ST_TYPE(info) 1159extract a type from an st_info value. 1160.It Dv ELF32_ST_INFO(bind, type) 1161or 1162.Sy ELF64_ST_INFO(bind, type) 1163convert a binding and a type into an st_info value. 1164.El 1165.El 1166.Pp 1167.It Dv st_other 1168This member currently holds zero and has no defined meaning. 1169.It Dv st_shndx 1170Every symbol table entry is 1171.Dq defined 1172in relation to some action. 1173This member holds the relevant section 1174header table index. 1175.El 1176.Pp 1177Relocation is the process of connecting symbolic references with 1178symbolic definitions. 1179Relocatable files must have information that 1180describes how to modify their section contents, thus allowing executable 1181and shared object files to hold the right information for a process' 1182program image. 1183Relocation entries are these data. 1184.Pp 1185Relocation structures that do not need an addend: 1186.Pp 1187.Bd -literal -offset indent 1188typedef struct { 1189 Elf32_Addr r_offset; 1190 Elf32_Word r_info; 1191} Elf32_Rel; 1192.Ed 1193.Bd -literal -offset indent 1194typedef struct { 1195 Elf64_Addr r_offset; 1196 Elf64_Size r_info; 1197} Elf64_Rel; 1198.Ed 1199.Pp 1200Relocation structures that need an addend: 1201.Pp 1202.Bd -literal -offset indent 1203typedef struct { 1204 Elf32_Addr r_offset; 1205 Elf32_Word r_info; 1206 Elf32_Sword r_addend; 1207} Elf32_Rela; 1208.Ed 1209.Bd -literal -offset indent 1210typedef struct { 1211 Elf64_Addr r_offset; 1212 Elf64_Size r_info; 1213 Elf64_Off r_addend; 1214} Elf64_Rela; 1215.Ed 1216.Pp 1217.Bl -tag -width "r_offset" -compact 1218.It Dv r_offset 1219This member gives the location at which to apply the relocation action. 1220For a relocatable file, the value is the byte offset from the beginning 1221of the section to the storage unit affected by the relocation. 1222For an 1223executable file or shared object, the value is the virtual address of 1224the storage unit affected by the relocation. 1225.It Dv r_info 1226This member gives both the symbol table index with respect to which the 1227relocation must be made and the type of relocation to apply. 1228Relocation 1229types are processor-specific. 1230When the text refers to a relocation 1231entry's relocation type or symbol table index, it means the result of 1232applying 1233.Sy ELF_[32|64]_R_TYPE 1234or 1235.Sy ELF[32|64]_R_SYM , 1236respectively to the entry's 1237.Sy r_info 1238member. 1239.It Dv r_addend 1240This member specifies a constant addend used to compute the value to be 1241stored into the relocatable field. 1242.El 1243.Sh SEE ALSO 1244.Xr as 1 , 1245.Xr gdb 1 , 1246.Xr ld 1 , 1247.Xr objdump 1 , 1248.Xr execve 2 , 1249.Xr core 5 1250.Rs 1251.%A Hewlett Packard 1252.%B Elf-64 Object File Format 1253.Re 1254.Rs 1255.%A Santa Cruz Operation 1256.%B System V Application Binary Interface 1257.Re 1258.Rs 1259.%A Unix System Laboratories 1260.%T Object Files 1261.%B "Executable and Linking Format (ELF)" 1262.Re 1263.Sh HISTORY 1264The ELF header files made their appearance in 1265.Fx 2.2.6 . 1266ELF in itself first appeared in 1267.At V . 1268The ELF format is an adopted standard. 1269.Sh AUTHORS 1270This manual page was written by 1271.An Jeroen Ruigrok van der Werven 1272.Aq asmodai@FreeBSD.org 1273with inspiration from BSDi's BSD/OS 1274.Xr elf 5 1275manpage. 1276