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