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