1.\" $OpenBSD: elf.5,v 1.34 2017/10/27 08:36:42 mpi 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: October 27 2017 $ 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 program address 83Elf32_Off Unsigned file offset 84Elf32_Sword Signed large integer 85Elf32_Word Unsigned large integer 86Elf32_Half Unsigned medium integer 87.Ed 88.Pp 89And the following types are used for 64-bit architectures: 90.Bd -literal -offset indent 91Elf64_Addr Unsigned program address 92Elf64_Off Unsigned file offset 93Elf64_Shalf Signed halfword field 94Elf64_Sword Signed large integer 95Elf64_Word Field or unsigned large integer 96Elf64_Sxword Signed object size or alignment 97Elf64_Xword Unsigned object size or alignment 98Elf64_Half Unsigned halfword field 99Elf64_Quarter Unsigned quarterword field 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_Quarter e_type; 132 Elf64_Quarter e_machine; 133 Elf64_Half e_version; 134 Elf64_Addr e_entry; 135 Elf64_Off e_phoff; 136 Elf64_Off e_shoff; 137 Elf64_Half e_flags; 138 Elf64_Quarter e_ehsize; 139 Elf64_Quarter e_phentsize; 140 Elf64_Quarter e_phnum; 141 Elf64_Quarter e_shentsize; 142 Elf64_Quarter e_shnum; 143 Elf64_Quarter 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_ARM 315Advanced RISC Machines ARM. 316.It Dv EM_ALPHA 317Compaq [DEC] Alpha. 318.It Dv EM_SH 319Hitachi/Renesas Super-H. 320.It Dv EM_SPARCV9 321SPARC v9 64-bit. 322.It Dv EM_IA_64 323Intel IA-64. 324.It Dv EM_AMD64 325AMD64. 326.It Dv EM_VAX 327DEC Vax. 328.It Dv EM_ALPHA_EXP 329Compaq [DEC] Alpha with enhanced instruction set. 330.El 331.It Dv e_version 332This member identifies the file version: 333.Pp 334.Bl -tag -width "EV_CURRENT" -compact 335.It Dv EV_NONE 336Invalid version. 337.It Dv EV_CURRENT 338Current version. 339.El 340.It Dv e_entry 341This member gives the virtual address to which the system first transfers 342control, thus starting the process. 343If the file has no associated entry 344point, this member holds zero. 345.It Dv e_phoff 346This member holds the program header table's file offset in bytes. 347If 348the file has no program header table, this member holds zero. 349.It Dv e_shoff 350This member holds the section header table's file offset in bytes. 351If the 352file has no section header table this member holds zero. 353.It Dv e_flags 354This member holds processor-specific flags associated with the file. 355Flag names take the form EF_`machine_flag'. 356Currently no flags have been defined. 357.It Dv e_ehsize 358This member holds the ELF header's size in bytes. 359.It Dv e_phentsize 360This member holds the size in bytes of one entry in the file's program header 361table; all entries are the same size. 362.It Dv e_phnum 363This member holds the number of entries in the program header 364table. 365Thus the product of 366.Sy e_phentsize 367and 368.Sy e_phnum 369gives the table's size 370in bytes. 371If a file has no program header, 372.Sy e_phnum 373holds the value zero. 374.It Dv e_shentsize 375This member holds a sections header's size in bytes. 376A section header is one 377entry in the section header table; all entries are the same size. 378.It Dv e_shnum 379This member holds the number of entries in the section header table. 380Thus 381the product of 382.Sy e_shentsize 383and 384.Sy e_shnum 385gives the section header table's size in bytes. 386If a file has no section 387header table, 388.Sy e_shnum 389holds the value of zero. 390.It Dv e_shstrndx 391This member holds the section header table index of the entry associated 392with the section name string table. 393If the file has no section name string 394table, this member holds the value 395.Dv SHN_UNDEF . 396.El 397.Pp 398An executable or shared object file's program header table is an array of 399structures, each describing a segment or other information the system needs 400to prepare the program for execution. 401An object file 402.Em segment 403contains one or more 404.Em sections . 405Program headers are meaningful only for executable and shared object files. 406A file specifies its own program header size with the ELF header's 407.Sy e_phentsize 408and 409.Sy e_phnum 410members. 411As with the ELF executable header, the program header 412also has different versions depending on the architecture: 413.Bd -literal -offset indent 414typedef struct { 415 Elf32_Word p_type; 416 Elf32_Off p_offset; 417 Elf32_Addr p_vaddr; 418 Elf32_Addr p_paddr; 419 Elf32_Word p_filesz; 420 Elf32_Word p_memsz; 421 Elf32_Word p_flags; 422 Elf32_Word p_align; 423} Elf32_Phdr; 424.Ed 425.Bd -literal -offset indent 426typedef struct { 427 Elf64_Half p_type; 428 Elf64_Half p_flags; 429 Elf64_Off p_offset; 430 Elf64_Addr p_vaddr; 431 Elf64_Addr p_paddr; 432 Elf64_Xword p_filesz; 433 Elf64_Xword p_memsz; 434 Elf64_Xword p_align; 435} Elf64_Phdr; 436.Ed 437.Pp 438The main difference between the 32-bit and the 64-bit program header lies 439only in the location of a 440.Sy p_flags 441member in the total struct. 442.Bl -tag -width "p_offset" -offset indent 443.It Dv p_type 444This member of the Phdr struct tells what kind of segment this array 445element describes or how to interpret the array element's information. 446.Bl -tag -width "PT_DYNAMIC" 447.It Dv PT_NULL 448The array element is unused and the other members' values are undefined. 449This lets the program header have ignored entries. 450.It Dv PT_LOAD 451The array element specifies a loadable segment, described by 452.Sy p_filesz 453and 454.Sy p_memsz . 455The bytes from the file are mapped to the beginning of the memory 456segment. 457If the segment's memory size 458.Pq Sy p_memsz 459is larger than the file size 460.Pq Sy p_filesz , 461the 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 the location and size of the dynamic section, 472both in the file and in the memory image of the program. 473This segment type may not occur more than once in a file and may 474only occur if the dynamic section is part of the memory image of 475the program. 476.It Dv PT_INTERP 477The array element specifies the location and size of a null-terminated 478path name to invoke as an interpreter. 479This segment type is meaningful 480only for executable files (though it may occur for shared objects). 481However it may not occur more than once in a file. 482If it is present, it must precede any loadable segment entry. 483.It Dv PT_NOTE 484The array element specifies the location and size for auxiliary information. 485.It Dv PT_SHLIB 486This segment type is reserved but has unspecified semantics. 487Programs that 488contain an array element of this type do not conform to the ABI. 489.It Dv PT_PHDR 490The array element specifies the location and size of the program 491header table itself, both in the file and in the memory image of the program. 492This segment type may not occur more than once in a file and may 493only occur if the program header table is part of the memory image 494of the program. 495If it is present, it must precede any loadable segment entry. 496.It Dv PT_TLS 497The array element specifies the location and size of the 498thread-local storage for this file. 499Each thread in a process loading this file will have the segment's 500memory size 501.Pq Sy p_memsz 502allocated for it, where the bytes up to the segment's file size 503.Pq Sy p_filesz 504will be initialized with the data in this segment and the remaining 505.Dq extra 506bytes will be set to zero. 507This segment type may not occur more than once in a file and may 508only occur if the thread-local storage is part of the memory image 509of the program. 510.It Dv PT_GNU_EH_FRAME 511The array element specifies the location and size of the GNU exception 512frame header, both in the file and in the memory image of the program. 513This segment type may not occur more than once in a file and may 514only occur if the GNU exception frame header is part of the memory 515image of the program. 516.It Dv PT_GNU_RELRO 517The array element specifies the location and size of a part of the 518memory image of the program that should be made read-only once all 519immediate relocation processing for the file has been performed. 520This segment type may not occur more than once in a file. 521.It Dv PT_OPENBSD_RANDOMIZE 522The array element specifies the location and size of a part of the 523memory image of the program that must be filled with random data 524before any code in the object is executed. 525The memory region specified by a segment of this type may overlap 526the region specified by a 527.Dv PT_GNU_RELRO 528segment, in which case the intersection will be filled with random 529data before being marked read-only. 530This segment type may occur more than once in a file, but a limit 531on the total number of bytes in the segments for an object of no 532less than 65536 bytes may be imposed. 533.It Dv PT_OPENBSD_WXNEEDED 534The array element specifies that a process executing this file may 535need to be able to map or protect memory regions as simultaneously 536executable and writable. 537If the system is unable or unwilling to permit that for this executable 538then it may fail immediately. 539This segment type is meaningful only for executable files and is 540ignored in other objects. 541.It Dv PT_LOOS 542This value up to and including 543.Dv PT_HIOS 544is reserved for operating system-specific semantics. 545.It Dv PT_HIOS 546This value down to and including 547.Dv PT_LOOS 548is reserved for operating system-specific semantics. 549.It Dv PT_LOPROC 550This value up to and including 551.Dv PT_HIPROC 552is reserved for processor-specific semantics. 553.It Dv PT_HIPROC 554This value down to and including 555.Dv PT_LOPROC 556is reserved for processor-specific semantics. 557.El 558.It Dv p_offset 559This member holds the offset from the beginning of the file at which 560the first byte of the segment resides. 561.It Dv p_vaddr 562This member holds the virtual address at which the first byte of the 563segment resides in memory. 564.It Dv p_paddr 565On systems for which physical addressing is relevant, this member is 566reserved for the segment's physical address. 567Under 568.Bx 569this member is 570not used and must be zero. 571.It Dv p_filesz 572This member holds the number of bytes in the file image of the segment. 573It may be zero. 574.It Dv p_memsz 575This member holds the number of bytes in the memory image of the segment. 576It may be zero. 577.It Dv p_flags 578This member holds flags relevant to the segment: 579.Pp 580.Bl -tag -width "PF_X" -compact 581.It Dv PF_X 582An executable segment. 583.It Dv PF_W 584A writable segment. 585.It Dv PF_R 586A readable segment. 587.El 588.Pp 589A text segment commonly has the flags 590.Dv PF_X 591and 592.Dv PF_R . 593A data segment commonly has 594.Dv PF_X , 595.Dv PF_W 596and 597.Dv PF_R . 598.It Dv p_align 599This member holds the value to which the segments are aligned in memory 600and in the file. 601Loadable process segments must have congruent values for 602.Sy p_vaddr 603and 604.Sy p_offset , 605modulo the page size. 606Values of zero and one mean no alignment is required. 607Otherwise, 608.Sy p_align 609should be a positive, integral power of two, and 610.Sy p_vaddr 611should equal 612.Sy p_offset , 613modulo 614.Sy p_align . 615.El 616.Pp 617A file's section header table lets one locate all the file's sections. 618The 619section header table is an array of Elf32_Shdr or Elf64_Shdr structures. 620The 621ELF header's 622.Sy e_shoff 623member gives the byte offset from the beginning of the file to the section 624header table. 625.Sy e_shnum 626holds the number of entries the section header table contains. 627.Sy e_shentsize 628holds the size in bytes of each entry. 629.Pp 630A section header table index is a subscript into this array. 631Some section 632header table indices are reserved. 633An object file does not have sections for 634these special indices: 635.Bl -tag -width "SHN_LORESERVE" 636.It Dv SHN_UNDEF 637This value marks an undefined, missing, irrelevant or otherwise meaningless 638section reference. 639For example, a symbol 640.Dq defined 641relative to section number 642.Dv SHN_UNDEF 643is an undefined symbol. 644.It Dv SHN_LORESERVE 645This value specifies the lower bound of the range of reserved indices. 646.It Dv SHN_LOPROC 647This value up to and including 648.Dv SHN_HIPROC 649is reserved for processor-specific semantics. 650.It Dv SHN_HIPROC 651This value down to and including 652.Dv SHN_LOPROC 653is reserved for processor-specific semantics. 654.It Dv SHN_ABS 655This value specifies the absolute value for the corresponding reference. 656For 657example, a symbol defined relative to section number 658.Dv SHN_ABS 659has an absolute value and is not affected by relocation. 660.It Dv SHN_COMMON 661Symbols defined relative to this section are common symbols, such as FORTRAN 662COMMON or unallocated C external variables. 663.It Dv SHN_HIRESERVE 664This value specifies the upper bound of the range of reserved indices. 665The 666system reserves indices between 667.Dv SHN_LORESERVE 668and 669.Dv SHN_HIRESERVE , 670inclusive. 671The section header table does not contain entries for the 672reserved indices. 673.El 674.Pp 675The section header has the following structure: 676.Bd -literal -offset indent 677typedef struct { 678 Elf32_Word sh_name; 679 Elf32_Word sh_type; 680 Elf32_Word sh_flags; 681 Elf32_Addr sh_addr; 682 Elf32_Off sh_offset; 683 Elf32_Word sh_size; 684 Elf32_Word sh_link; 685 Elf32_Word sh_info; 686 Elf32_Word sh_addralign; 687 Elf32_Word sh_entsize; 688} Elf32_Shdr; 689.Ed 690.Bd -literal -offset indent 691typedef struct { 692 Elf64_Half sh_name; 693 Elf64_Half sh_type; 694 Elf64_Xword sh_flags; 695 Elf64_Addr sh_addr; 696 Elf64_Off sh_offset; 697 Elf64_Xword sh_size; 698 Elf64_Half sh_link; 699 Elf64_Half sh_info; 700 Elf64_Xword sh_addralign; 701 Elf64_Xword sh_entsize; 702} Elf64_Shdr; 703.Ed 704.Bl -tag -width "sh_addralign" 705.It Dv sh_name 706This member specifies the name of the section. 707Its value is an index 708into the section header string table section, giving the location of 709a null-terminated string. 710.It Dv sh_type 711This member categorizes the section's contents and semantics. 712.Bl -tag -width "SHT_PROGBITS" 713.It Dv SHT_NULL 714This value marks the section header as inactive. 715It does not 716have an associated section. 717Other members of the section header 718have undefined values. 719.It Dv SHT_PROGBITS 720This section holds information defined by the program, whose 721format and meaning are determined solely by the program. 722.It Dv SHT_SYMTAB 723This section holds a symbol table. 724Typically, 725.Dv SHT_SYMTAB 726provides symbols for link editing, though it may also be used 727for dynamic linking. 728As a complete symbol table, it may contain 729many symbols unnecessary for dynamic linking. 730An object file can 731also contain a 732.Dv SHT_DYNSYM 733section. 734.It Dv SHT_STRTAB 735This section holds a string table. 736An object file may have multiple 737string table sections. 738.It Dv SHT_RELA 739This section holds relocation entries with explicit addends, such 740as type 741.Sy Elf32_Rela 742for the 32-bit class of object files. 743An object may have multiple 744relocation sections. 745.It Dv SHT_HASH 746This section holds a symbol hash table. 747An object participating in 748dynamic linking must contain a symbol hash table. 749An object file may 750have only one hash table. 751.It Dv SHT_DYNAMIC 752This section holds information for dynamic linking. 753An object file may 754have only one dynamic section. 755.It Dv SHT_NOTE 756This section holds information that marks the file in some way. 757.It Dv SHT_NOBITS 758A section of this type occupies no space in the file but otherwise 759resembles 760.Dv SHT_PROGBITS . 761Although this section contains no bytes, the 762.Sy sh_offset 763member contains the conceptual file offset. 764.It Dv SHT_REL 765This section holds relocation offsets without explicit addends, such 766as type 767.Sy Elf32_Rel 768for the 32-bit class of object files. 769An object file may have multiple 770relocation sections. 771.It Dv SHT_SHLIB 772This section is reserved but has unspecified semantics. 773.It Dv SHT_DYNSYM 774This section holds a minimal set of dynamic linking symbols. 775An 776object file can also contain a 777.Dv SHT_SYMTAB 778section. 779.It Dv SHT_LOPROC 780This value up to and including 781.Dv SHT_HIPROC 782is reserved for processor-specific semantics. 783.It Dv SHT_HIPROC 784This value down to and including 785.Dv SHT_LOPROC 786is reserved for processor-specific semantics. 787.It Dv SHT_LOUSER 788This value specifies the lower bound of the range of indices reserved for 789application programs. 790.It Dv SHT_HIUSER 791This value specifies the upper bound of the range of indices reserved for 792application programs. 793Section types between 794.Dv SHT_LOUSER 795and 796.Dv SHT_HIUSER 797may be used by the application, without conflicting with current or future 798system-defined section types. 799.El 800.It Dv sh_flags 801Sections support one-bit flags that describe miscellaneous attributes. 802If a flag bit is set in 803.Sy sh_flags , 804the attribute is 805.Dq on 806for the section. 807Otherwise, the attribute is 808.Dq off 809or does not apply. 810Undefined attributes are set to zero. 811.Pp 812.Bl -tag -width "SHF_EXECINSTR" -compact 813.It Dv SHF_WRITE 814This section contains data that should be writable during process 815execution. 816.It Dv SHF_ALLOC 817This section occupies memory during process execution. 818Some control 819sections do not reside in the memory image of an object file. 820This 821attribute is off for those sections. 822.It Dv SHF_EXECINSTR 823This section contains executable machine instructions. 824.It Dv SHF_TLS 825This section is for thread-local storage. 826.It Dv SHF_MASKPROC 827All bits included in this mask are reserved for processor-specific 828semantics. 829.El 830.It Dv sh_addr 831If this section appears in the memory image of a process, this member 832holds the address at which the section's first byte should reside. 833Otherwise, the member contains zero. 834.It Dv sh_offset 835This member's value holds the byte offset from the beginning of the file 836to the first byte in the section. 837One section type, 838.Dv SHT_NOBITS , 839occupies no space in the file, and its 840.Sy sh_offset 841member locates the conceptual placement in the file. 842.It Dv sh_size 843This member holds the section's size in bytes. 844Unless the section type 845is 846.Dv SHT_NOBITS , 847the section occupies 848.Sy sh_size 849bytes in the file. 850A section of type 851.Dv SHT_NOBITS 852may have a non-zero size, but it occupies no space in the file. 853.It Dv sh_link 854This member holds a section header table index link, whose interpretation 855depends on the section type. 856.It Dv sh_info 857This member holds extra information, whose interpretation depends on the 858section type. 859.It Dv sh_addralign 860Some sections have address alignment constraints. 861If a section holds a 862doubleword, the system must ensure doubleword alignment for the entire 863section. 864That is, the value of 865.Sy sh_addr 866must be congruent to zero, modulo the value of 867.Sy sh_addralign . 868Only zero and positive integral powers of two are allowed. 869Values of zero 870or one mean the section has no alignment constraints. 871.It Dv sh_entsize 872Some sections hold a table of fixed-sized entries, such as a symbol table. 873For such a section, this member gives the size in bytes for each entry. 874This member contains zero if the section does not hold a table of 875fixed-size entries. 876.El 877.Pp 878Various sections hold program and control information: 879.Bl -tag -width ".shstrtab" 880.It .SUNW_ctf 881This section contains the (un)compressed Compact C-Type Format data 882describing the object's types and symbols. 883This section is of type 884.Dv SHT_PROGBITS . 885.It .bss 886This section holds uninitialized data that contribute to the program's 887memory image. 888By definition, the system initializes the data with zeros 889when the program begins to run. 890This section is of type 891.Dv SHT_NOBITS . 892The attribute types are 893.Dv SHF_ALLOC 894and 895.Dv SHF_WRITE . 896.It .comment 897This section holds version control information. 898This section is of type 899.Dv SHT_PROGBITS . 900No attribute types are used. 901.It .ctors 902This section holds initialized pointers to the C++ constructor functions. 903This section is of type 904.Dv SHT_PROGBITS . 905The attribute types are 906.Dv SHF_ALLOC 907and 908.Dv SHF_WRITE . 909.It .data 910This section holds initialized data that contribute to the program's 911memory image. 912This section is of type 913.Dv SHT_PROGBITS . 914The attribute types are 915.Dv SHF_ALLOC 916and 917.Dv SHF_WRITE . 918.It .data1 919This section holds initialized data that contribute to the program's 920memory image. 921This section is of type 922.Dv SHT_PROGBITS . 923The attribute types are 924.Dv SHF_ALLOC 925and 926.Dv SHF_WRITE . 927.It .debug 928This section holds information for symbolic debugging. 929The contents 930are unspecified. 931This section is of type 932.Dv SHT_PROGBITS . 933No attribute types are used. 934.It .dtors 935This section holds initialized pointers to the C++ destructor functions. 936This section is of type 937.Dv SHT_PROGBITS . 938The attribute types are 939.Dv SHF_ALLOC 940and 941.Dv SHF_WRITE . 942.It .dynamic 943This section holds dynamic linking information. 944The section's attributes 945will include the 946.Dv SHF_ALLOC 947bit. 948Whether the 949.Dv SHF_WRITE 950bit is set is processor-specific. 951This section is of type 952.Dv SHT_DYNAMIC . 953See the attributes above. 954.It .dynstr 955This section holds strings needed for dynamic linking, most commonly 956the strings that represent the names associated with symbol table entries. 957This section is of type 958.Dv SHT_STRTAB . 959The attribute type used is 960.Dv SHF_ALLOC . 961.It .dynsym 962This section holds the dynamic linking symbol table. 963This section is of type 964.Dv SHT_DYNSYM . 965The attribute used is 966.Dv SHF_ALLOC . 967.It .fini 968This section holds executable instructions that contribute to the process 969termination code. 970When a program exits normally the system arranges to 971execute the code in this section. 972This section is of type 973.Dv SHT_PROGBITS . 974The attributes used are 975.Dv SHF_ALLOC 976and 977.Dv SHF_EXECINSTR . 978.It .got 979This section holds the global offset table. 980This section is of type 981.Dv SHT_PROGBITS . 982The attributes are processor-specific. 983.It .hash 984This section holds a symbol hash table. 985This section is of type 986.Dv SHT_HASH . 987The attribute used is 988.Dv SHF_ALLOC . 989.It .init 990This section holds executable instructions that contribute to the process 991initialization code. 992When a program starts to run the system arranges to 993execute the code in this section before calling the main program entry point. 994This section is of type 995.Dv SHT_PROGBITS . 996The attributes used are 997.Dv SHF_ALLOC 998and 999.Dv SHF_EXECINSTR . 1000.It .interp 1001This section holds the pathname of a program interpreter. 1002If the file has 1003a loadable segment that includes the section, the section's attributes will 1004include the 1005.Dv SHF_ALLOC 1006bit. 1007Otherwise, that bit will be off. 1008This section is of type 1009.Dv SHT_PROGBITS . 1010.It .line 1011This section holds line number information for symbolic debugging, which 1012describes the correspondence between the program source and the machine code. 1013The contents are unspecified. 1014This section is of type 1015.Dv SHT_PROGBITS . 1016No attribute types are used. 1017.It .note 1018This section holds information in the 1019note section 1020format described below. 1021This section is of type 1022.Dv SHT_NOTE . 1023No attribute types are used. 1024.Ox 1025native executables usually contain a 1026.Sy .note.openbsd.ident 1027section to identify themselves, for the kernel to bypass any compatibility 1028ELF binary emulation tests when loading the file. 1029.It .plt 1030This section holds the procedure linkage table. 1031This section is of type 1032.Dv SHT_PROGBITS . 1033The attributes are processor-specific. 1034.It .relNAME 1035This section holds relocation information as described below. 1036If the file 1037has a loadable segment that includes relocation, the section's attributes 1038will include the 1039.Dv SHF_ALLOC 1040bit. 1041Otherwise the bit will be off. 1042By convention, 1043.Dq NAME 1044is supplied by the section to which the relocations apply. 1045Thus a relocation 1046section for 1047.Sy .text 1048normally would have the name 1049.Sy .rel.text . 1050This section is of type 1051.Dv SHT_REL . 1052.It .relaNAME 1053This section holds relocation information as described below. 1054If the file 1055has a loadable segment that includes relocation, the section's attributes 1056will include the 1057.Dv SHF_ALLOC 1058bit. 1059Otherwise the bit will be off. 1060By convention, 1061.Dq NAME 1062is supplied by the section to which the relocations apply. 1063Thus a relocation 1064section for 1065.Sy .text 1066normally would have the name 1067.Sy .rela.text . 1068This section is of type 1069.Dv SHT_RELA . 1070.It .rodata 1071This section holds read-only data that typically contribute to a 1072non-writable segment in the process image. 1073This section is of type 1074.Dv SHT_PROGBITS . 1075The attribute used is 1076.Dv SHF_ALLOC . 1077.It .rodata1 1078This section holds read-only data that typically contribute to a 1079non-writable segment in the process image. 1080This section is of type 1081.Dv SHT_PROGBITS . 1082The attribute used is 1083.Dv SHF_ALLOC . 1084.It .shstrtab 1085This section holds section names. 1086This section is of type 1087.Dv SHT_STRTAB . 1088No attribute types are used. 1089.It .strtab 1090This section holds strings, most commonly the strings that represent the 1091names associated with symbol table entries. 1092If the file has a loadable 1093segment that includes the symbol string table, the section's attributes 1094will include the 1095.Dv SHF_ALLOC 1096bit. 1097Otherwise the bit will be off. 1098This section is of type 1099.Dv SHT_STRTAB . 1100.It .symtab 1101This section holds a symbol table. 1102If the file has a loadable segment 1103that includes the symbol table, the section's attributes will include 1104the 1105.Dv SHF_ALLOC 1106bit. 1107Otherwise the bit will be off. 1108This section is of type 1109.Dv SHT_SYMTAB . 1110.It .tbss 1111This section is the thread-local storage version of 1112.Sy .bss , 1113holding uninitialized data that contribute to the program's memory 1114image on a per-thread basis. 1115By definition, the system allocates and initializes the data with 1116zeros for each thread before it first accesses it. 1117This section is of type 1118.Dv SHT_NOBITS . 1119The attribute types are 1120.Dv SHF_ALLOC , 1121.Dv SHF_WRITE , 1122and 1123.Dv SHF_TLS . 1124.It .tdata 1125This section is the thread-local storage version of 1126.Sy .data , 1127holding initialized data that contribute to the program's memory 1128image on a per-thread basis. 1129The system allocates and initializes the data for each thread before 1130it first accesses it. 1131This section is of type 1132.Dv SHT_PROGBITS . 1133The attribute types are 1134.Dv SHF_ALLOC , 1135.Dv SHF_WRITE , 1136and 1137.Dv SHF_TLS . 1138.It .text 1139This section holds the 1140.Dq text , 1141or executable instructions, of a program. 1142This section is of type 1143.Dv SHT_PROGBITS . 1144The attributes used are 1145.Dv SHF_ALLOC 1146and 1147.Dv SHF_EXECINSTR . 1148.El 1149.Pp 1150String table sections hold null-terminated character sequences, commonly 1151called strings. 1152The object file uses these strings to represent symbol 1153and section names. 1154One references a string as an index into the string 1155table section. 1156The first byte, which is index zero, is defined to hold 1157a null character. 1158Similarly, a string table's last byte is defined to 1159hold a null character, ensuring null termination for all strings. 1160.Pp 1161An object file's symbol table holds information needed to locate and 1162relocate a program's symbolic definitions and references. 1163A symbol table 1164index is a subscript into this array. 1165.Bd -literal -offset indent 1166typedef struct { 1167 Elf32_Word st_name; 1168 Elf32_Addr st_value; 1169 Elf32_Word st_size; 1170 unsigned char st_info; 1171 unsigned char st_other; 1172 Elf32_Half st_shndx; 1173} Elf32_Sym; 1174.Ed 1175.Bd -literal -offset indent 1176typedef struct { 1177 Elf64_Half st_name; 1178 Elf_Byte st_info; 1179 Elf_Byte st_other; 1180 Elf64_Quarter st_shndx; 1181 Elf64_Xword st_value; 1182 Elf64_Xword st_size; 1183} Elf64_Sym; 1184.Ed 1185.Bl -tag -width "st_value" 1186.It Dv st_name 1187This member holds an index into the object file's symbol string table, 1188which holds character representations of the symbol names. 1189If the value 1190is non-zero, it represents a string table index that gives the symbol 1191name. 1192Otherwise, the symbol table has no name. 1193.It Dv st_value 1194This member gives the value of the associated symbol. 1195.It Dv st_size 1196Many symbols have associated sizes. 1197This member holds zero if the symbol 1198has no size or an unknown size. 1199.It Dv st_info 1200This member specifies the symbol's type and binding attributes: 1201.Bl -tag -width "STT_SECTION" 1202.It Dv STT_NOTYPE 1203The symbol's type is not defined. 1204.It Dv STT_OBJECT 1205The symbol is associated with a data object. 1206.It Dv STT_FUNC 1207The symbol is associated with a function or other executable code. 1208.It Dv STT_SECTION 1209The symbol is associated with a section. 1210Symbol table entries of 1211this type exist primarily for relocation and normally have 1212.Dv STB_LOCAL 1213bindings. 1214.It Dv STT_FILE 1215By convention, the symbol's name gives the name of the source file 1216associated with the object file. 1217A file symbol has 1218.Dv STB_LOCAL 1219bindings, its section index is 1220.Dv SHN_ABS , 1221and it precedes the other 1222.Dv STB_LOCAL 1223symbols of the file, if it is present. 1224.It Dv STT_TLS 1225The symbol is associated with an object in thread-local storage. 1226The symbol's value is its offset in the TLS storage for this file. 1227.It Dv STT_LOPROC 1228This value up to and including 1229.Dv STT_HIPROC 1230is reserved for processor-specific semantics. 1231.It Dv STT_HIPROC 1232This value down to and including 1233.Dv STT_LOPROC 1234is reserved for processor-specific semantics. 1235.El 1236.Bl -tag -width "STB_GLOBAL" 1237.It Dv STB_LOCAL 1238Local symbols are not visible outside the object file containing their 1239definition. 1240Local symbols of the same name may exist in multiple files 1241without interfering with each other. 1242.It Dv STB_GLOBAL 1243Global symbols are visible to all object files being combined. 1244One file's 1245definition of a global symbol will satisfy another file's undefined 1246reference to the same symbol. 1247.It Dv STB_WEAK 1248Weak symbols resemble global symbols, but their definitions have lower 1249precedence. 1250.It Dv STB_LOPROC 1251This value up to and including 1252.Dv STB_HIPROC 1253is reserved for processor-specific semantics. 1254.It Dv STB_HIPROC 1255This value down to and including 1256.Dv STB_LOPROC 1257is reserved for processor-specific semantics. 1258.Pp 1259There are macros for packing and unpacking the binding and type fields: 1260.Pp 1261.Bl -tag -width "ELF32_ST_INFO(bind, type)" -compact 1262.It Xo 1263.Fn ELF32_ST_BIND info 1264.Xc 1265or 1266.Fn ELF64_ST_BIND info 1267extract a binding from an st_info value. 1268.It Xo 1269.Fn ELF64_ST_TYPE info 1270.Xc 1271or 1272.Fn ELF32_ST_TYPE info 1273extract a type from an st_info value. 1274.It Xo 1275.Fn ELF32_ST_INFO bind type 1276.Xc 1277or 1278.Fn ELF64_ST_INFO bind type 1279convert a binding and a type into an st_info value. 1280.El 1281.El 1282.It Dv st_other 1283This member currently holds zero and has no defined meaning. 1284.It Dv st_shndx 1285Every symbol table entry is 1286.Dq defined 1287in relation to some section. 1288This member holds the relevant section 1289header table index. 1290.El 1291.Pp 1292Relocation is the process of connecting symbolic references with 1293symbolic definitions. 1294Relocatable files must have information that 1295describes how to modify their section contents, thus allowing executable 1296and shared object files to hold the right information for a process' 1297program image. 1298Relocation entries are these data. 1299.Pp 1300Relocation structures that do not need an addend: 1301.Bd -literal -offset indent 1302typedef struct { 1303 Elf32_Addr r_offset; 1304 Elf32_Word r_info; 1305} Elf32_Rel; 1306.Ed 1307.Bd -literal -offset indent 1308typedef struct { 1309 Elf64_Xword r_offset; 1310 Elf64_Xword r_info; 1311} Elf64_Rel; 1312.Ed 1313.Pp 1314Relocation structures that need an addend: 1315.Bd -literal -offset indent 1316typedef struct { 1317 Elf32_Addr r_offset; 1318 Elf32_Word r_info; 1319 Elf32_Sword r_addend; 1320} Elf32_Rela; 1321.Ed 1322.Bd -literal -offset indent 1323typedef struct { 1324 Elf64_Xword r_offset; 1325 Elf64_Xword r_info; 1326 Elf64_Sxword r_addend; 1327} Elf64_Rela; 1328.Ed 1329.Bl -tag -width "r_offset" 1330.It Dv r_offset 1331This member gives the location at which to apply the relocation action. 1332For a relocatable file, the value is the byte offset from the beginning 1333of the section to the storage unit affected by the relocation. 1334For an 1335executable file or shared object, the value is the virtual address of 1336the storage unit affected by the relocation. 1337.It Dv r_info 1338This member gives both the symbol table index with respect to which the 1339relocation must be made and the type of relocation to apply. 1340Relocation 1341types are processor-specific. 1342When the text refers to a relocation 1343entry's relocation type or symbol table index, it means the result of 1344applying 1345.Dv ELF[32|64]_R_TYPE 1346or 1347.Dv ELF[32|64]_R_SYM , 1348respectively, to the entry's 1349.Sy r_info 1350member. 1351.It Dv r_addend 1352This member specifies a constant addend used to compute the value to be 1353stored into the relocatable field. 1354.El 1355.Pp 1356The note section is used to hold vendor-specific information that 1357may be used to help identify a binary's ABI. 1358It should start with an Elf_Note struct, 1359followed by the section name and the section description. 1360The actual note contents follow thereafter. 1361.Bd -literal -offset indent 1362typedef struct { 1363Elf32_Word namesz; 1364Elf32_Word descsz; 1365Elf32_Word type; 1366} Elf32_Note; 1367 1368typedef struct { 1369Elf64_Half namesz; 1370Elf64_Half descsz; 1371Elf64_Half type; 1372} Elf64_Note; 1373.Ed 1374.Bl -tag -width "r_offset" 1375.It Dv namesz 1376Length of the note name, rounded up to a 4-byte boundary. 1377.It Dv descsz 1378Length of the note description, rounded up to a 4-byte boundary. 1379.It Dv type 1380A vendor-specific note type. 1381.El 1382.Pp 1383The name and description strings follow the note structure. 1384Each string is aligned on a 4-byte boundary. 1385.Sh SEE ALSO 1386.Xr as 1 , 1387.Xr gdb 1 , 1388.Xr ld 1 , 1389.Xr objdump 1 , 1390.Xr execve 2 , 1391.Xr core 5 1392.Rs 1393.%A Hewlett-Packard 1394.%B Elf-64 Object File Format 1395.Re 1396.Rs 1397.%A Santa Cruz Operation 1398.%B System V Application Binary Interface 1399.Re 1400.Rs 1401.%A Unix System Laboratories 1402.%T Object Files 1403.%B "Executable and Linking Format (ELF)" 1404.Re 1405.Sh HISTORY 1406.Ox 1407ELF support first appeared in 1408.Ox 1.2 . 1409Starting with 1410.Ox 5.4 , 1411all supported platforms use it as the native binary file format. 1412ELF in itself first appeared in 1413.At V . 1414The ELF format is an adopted standard. 1415.Sh AUTHORS 1416This manual page was written by 1417.An Jeroen Ruigrok van der Werven Aq Mt asmodai@FreeBSD.org 1418with inspiration from BSDi's 1419.Bsx 1420.Nm elf 1421manpage. 1422