1.\" $NetBSD: elf.3,v 1.6 2024/03/03 17:37:33 christos Exp $ 2.\" 3.\" Copyright (c) 2006-2008,2011,2019 Joseph Koshy. 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 Joseph Koshy ``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 Joseph Koshy 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.\" Id: elf.3 3927 2021-03-07 17:22:22Z jkoshy 27.\" 28.Dd March 7, 2021 29.Dt ELF 3 30.Os 31.Sh NAME 32.Nm elf 33.Nd API for manipulating ELF objects 34.Sh LIBRARY 35.Lb libelf 36.Sh SYNOPSIS 37.In libelf.h 38.Sh DESCRIPTION 39The 40.Lb libelf 41provides functions that allow an application to read and manipulate 42ELF object files, and to read 43.Xr ar 1 44archives. 45The library allows the manipulation of ELF objects in a byte ordering 46and word-size independent way, allowing an application to read and 47create ELF objects for 32 and 64 bit architectures and for little- 48and big-endian machines. 49The library is capable of processing ELF objects that use extended 50section numbering. 51.Pp 52This manual page serves to provide an overview of the functionality in 53the ELF library. 54Further information may found in the manual pages for individual 55.Xr ELF 3 56functions that comprise the library. 57.Ss ELF Concepts 58As described in 59.Xr elf 5 , 60ELF files contain several data structures that are laid out in a 61specific way. 62ELF files begin with an 63.Dq Executable Header , 64and may contain an optional 65.Dq Program Header Table , 66and optional data in the form of ELF 67.Dq sections . 68A 69.Dq Section Header Table 70describes the content of the data in these sections. 71.Pp 72ELF objects have an associated 73.Dq "ELF class" 74which denotes the natural machine word size for the architecture 75the object is associated with. 76Objects for 32 bit architectures have an ELF class of 77.Dv ELFCLASS32 . 78Objects for 64 bit architectures have an ELF class of 79.Dv ELFCLASS64 . 80.Pp 81ELF objects also have an associated 82.Dq endianness 83which denotes the endianness of the machine architecture associated 84with the object. 85This may be 86.Dv ELFDATA2LSB 87for little-endian architectures and 88.Dv ELFDATA2MSB 89for big-endian architectures. 90.Pp 91ELF objects are also associated with an API version number. 92This version number determines the layout of the individual components 93of an ELF file and the semantics associated with these. 94.Ss Data Representation And Translation 95The 96.Xr ELF 3 97library distinguishes between 98.Dq native 99representations of ELF data structures and their 100.Dq file 101representations. 102.Pp 103An application would work with ELF data in its 104.Dq native 105representation, i.e., using the native byteorder and alignment mandated 106by the processor the application is running on. 107The 108.Dq file 109representation of the same data could use a different byte ordering 110and follow different constraints on object alignment than these native 111constraints. 112.Pp 113Accordingly, the 114.Xr ELF 3 115library offers translation facilities 116.Xr ( elf32_xlatetof 3 , 117.Xr elf32_xlatetom 3 , 118.Xr elf64_xlatetof 3 119and 120.Xr elf64_xlatetom 3 ) 121to and from these representations. 122It also provides higher-level APIs 123.Xr ( gelf_xlatetof 3 , 124.Xr gelf_xlatetom 3 ) 125that retrieve and store data from the ELF object in a class-agnostic 126manner. 127.Ss Library Working Version 128Conceptually, there are three version numbers associated with an 129application using the ELF library to manipulate ELF objects: 130.Bl -bullet -compact -offset indent 131.It 132The ELF version that the application was compiled against. 133This version determines the ABI expected by the application. 134.It 135The ELF version of the ELF object being manipulated by the 136application through the ELF library. 137.It 138The ELF version (or set of versions) supported by the ELF library itself. 139.El 140.Pp 141In order to facilitate working with ELF objects of differing versions, 142the ELF library requires the application to call the 143.Fn elf_version 144function before invoking many of its operations, in order to inform 145the library of the application's desired working version. 146.Pp 147In the current implementation, all three versions have to be 148.Dv EV_CURRENT . 149.Ss Namespace use 150The ELF library uses the following prefixes: 151.Bl -tag -width "ELF_F_*" 152.It Dv elf_ 153Used for class-independent functions. 154.It Dv elf32_ 155Used for functions working with 32 bit ELF objects. 156.It Dv elf64_ 157Used for functions working with 64 bit ELF objects. 158.It Dv Elf_ 159Used for class-independent data types. 160.It Dv ELF_C_ 161Used for command values used in a few functions. 162These symbols are defined as members of the 163.Vt Elf_Cmd 164enumeration. 165.It Dv ELF_E_ 166Used for error numbers. 167.It Dv ELF_F_ 168Used for flags. 169.It Dv ELF_K_ 170These constants define the kind of file associated with an ELF 171descriptor. 172See 173.Xr elf_kind 3 . 174The symbols are defined by the 175.Vt Elf_Kind 176enumeration. 177.It Dv ELF_T_ 178These values are defined by the 179.Vt Elf_Type 180enumeration, and denote the types of ELF data structures 181that can be present in an ELF object. 182.El 183.Pp 184In addition, the library uses symbols with prefixes 185.Dv _ELF 186and 187.Dv _libelf 188for its internal use. 189.Ss Descriptors 190Applications communicate with the library using descriptors. 191These are: 192.Bl -tag -width ".Vt Elf_Data" 193.It Vt Elf 194An 195.Vt Elf 196descriptor represents an ELF object or an 197.Xr ar 1 198archive. 199It is allocated using one of the 200.Fn elf_begin 201or 202.Fn elf_memory 203functions. 204An 205.Vt Elf 206descriptor can be used to read and write data to an ELF file. 207An 208.Vt Elf 209descriptor can be associated with zero or more 210.Vt Elf_Scn 211section descriptors. 212.Pp 213Given an ELF descriptor, the application may retrieve the ELF 214object's class-dependent 215.Dq "Executable Header" 216structures using the 217.Fn elf32_getehdr 218or 219.Fn elf64_getehdr 220functions. 221A new Ehdr structure may be allocated using the 222.Fn elf64_newehdr 223or 224.Fn elf64_newehdr 225functions. 226.Pp 227The 228.Dq "Program Header Table" 229associated with an ELF descriptor may be allocated using the 230.Fn elf32_getphdr 231or 232.Fn elf64_getphdr 233functions. 234A new program header table may be allocated or an existing table 235resized using the 236.Fn elf32_newphdr 237or 238.Fn elf64_newphdr 239functions. 240.Pp 241The 242.Vt Elf 243structure is opaque and has no members visible to the 244application. 245.It Vt Elf_Data 246An 247.Vt Elf_Data 248data structure describes an individual chunk of a ELF file as 249represented in memory. 250It has the following application-visible members: 251.Bl -tag -width ".Vt unsigned int d_version" -compact 252.It Vt "uint64_t d_align" 253The in-file alignment of the data buffer within its containing ELF section. 254This value must be non-zero and a power of two. 255.It Vt "void *d_buf" 256A pointer to data in memory. 257.It Vt "uint64_t d_off" 258The offset within the containing section where this descriptor's data 259would be placed. 260This field will be computed by the library unless the application 261requests full control of the ELF object's layout. 262.It Vt "uint64_t d_size" 263The number of bytes of data in this descriptor. 264.It Vt "Elf_Type d_type" 265The ELF type (see below) of the data in this descriptor. 266.It Vt "unsigned int d_version" 267The operating version for the data in this buffer. 268.El 269.Pp 270.Vt Elf_Data 271descriptors are usually used in conjunction with 272.Vt Elf_Scn 273descriptors. 274.It Vt Elf_Scn 275.Vt Elf_Scn 276descriptors represent sections in an ELF object. 277These descriptors are opaque and contain no application modifiable 278fields. 279.Pp 280The 281.Vt Elf_Scn 282descriptor for a specific section in an ELF object can be 283retrieved using the 284.Fn elf_getscn 285function. 286The sections contained in an ELF object can be traversed using the 287.Fn elf_nextscn 288function. 289New sections are allocated using the 290.Fn elf_newscn 291function. 292.Pp 293The 294.Vt Elf_Data 295descriptors associated with a given section can be retrieved 296using the 297.Fn elf_getdata 298function. 299New data descriptors can be added to a section 300descriptor using the 301.Fn elf_newdata 302function. 303The untranslated 304.Dq file 305representation of data in a section can be retrieved using the 306.Fn elf_rawdata 307function. 308.El 309.Ss Supported Elf Types 310The following ELF datatypes are supported by the library. 311.Pp 312.Bl -tag -width ".Dv ELF_T_SYMINFO" -compact 313.It Dv ELF_T_ADDR 314Machine addresses. 315.It Dv ELF_T_BYTE 316Byte data. 317The library will not attempt to translate byte data. 318.It Dv ELF_T_CAP 319Software and hardware capability records. 320.It Dv ELF_T_DYN 321Records used in a section of type 322.Dv SHT_DYNAMIC . 323.It Dv ELF_T_EHDR 324ELF executable header. 325.It Dv ELF_T_GNUHASH 326GNU-style hash tables. 327.It Dv ELF_T_HALF 32816-bit unsigned words. 329.It Dv ELF_T_LWORD 33064 bit unsigned words. 331.It Dv ELF_T_MOVE 332ELF Move records. 333.\".It Dv ELF_T_MOVEP 334.\" As yet unsupported. 335.It Dv ELF_T_NOTE 336ELF Note structures. 337.It Dv ELF_T_OFF 338File offsets. 339.It Dv ELF_T_PHDR 340ELF program header table entries. 341.It Dv ELF_T_REL 342ELF relocation entries. 343.It Dv ELF_T_RELA 344ELF relocation entries with addends. 345.It Dv ELF_T_SHDR 346ELF section header entries. 347.It Dv ELF_T_SWORD 348Signed 32-bit words. 349.It Dv ELF_T_SXWORD 350Signed 64-bit words. 351.It Dv ELF_T_SYMINFO 352ELF symbol information. 353.It Dv ELF_T_SYM 354ELF symbol table entries. 355.It Dv ELF_T_VDEF 356Symbol version definition records. 357.It Dv ELF_T_VNEED 358Symbol version requirement records. 359.It Dv ELF_T_WORD 360Unsigned 32-bit words. 361.It Dv ELF_T_XWORD 362Unsigned 64-bit words. 363.El 364.Pp 365The symbol 366.Dv ELF_T_NUM 367denotes the number of Elf types known to the library. 368.Pp 369The following table shows the mapping between ELF section types 370defined in 371.Xr elf 5 372and the types supported by the library. 373.Bl -column ".Dv SHT_PREINIT_ARRAY" ".Dv ELF_T_SYMINFO" 374.It Em Section Type Ta Em "Library Type" Ta Em Description 375.It Dv SHT_DYNAMIC Ta Dv ELF_T_DYN Ta Xo 376.Sq .dynamic 377section entries. 378.Xc 379.It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking. 380.It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers. 381.It Dv SHT_GNU_HASH Ta Dv ELF_T_GNUHASH Ta GNU hash sections. 382.It Dv SHT_GNU_LIBLIST Ta Dv ELF_T_WORD Ta List of libraries to be pre-linked. 383.It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions. 384.It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements. 385.It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols. 386.It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker. 387.It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes. 388.It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers. 389.It Dv SHT_NOBITS Ta Dv ELF_T_BYTE Ta Xo 390Empty sections. 391See 392.Xr elf 5 . 393.Xc 394.It Dv SHT_NOTE Ta Dv ELF_T_NOTE Ta ELF note records. 395.It Dv SHT_PREINIT_ARRAY Ta Dv ELF_T_ADDR Ta Pre-initialization function pointers. 396.It Dv SHT_PROGBITS Ta Dv ELF_T_BYTE Ta Machine code. 397.It Dv SHT_REL Ta Dv ELF_T_REL Ta ELF relocation records. 398.It Dv SHT_RELA Ta Dv ELF_T_RELA Ta Relocation records with addends. 399.It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables. 400.It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables. 401.It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering. 402.It Dv SHT_SUNW_dof Ta Dv ELF_T_BYTE Ta Xo 403Used by 404.Xr dtrace 1 . 405.Xc 406.It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records. 407.It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags. 408.It Dv SHT_SUNW_verdef Ta Dv ELF_T_VDEF Ta Xo 409Same as 410.Dv SHT_GNU_verdef . 411.Xc 412.It Dv SHT_SUNW_verneed Ta Dv ELF_T_VNEED Ta Xo 413Same as 414.Dv SHT_GNU_verneed . 415.Xc 416.It Dv SHT_SUNW_versym Ta Dv ELF_T_HALF Ta Xo 417Same as 418.Dv SHT_GNU_versym . 419.Xc 420.El 421.Pp 422Section types in the range 423.Dv [ SHT_LOOS , 424.Dv SHT_HIUSER ] 425are otherwise considered to be of type 426.Dv ELF_T_BYTE . 427.Ss Functional Grouping 428This section contains a brief overview of the available functionality 429in the ELF library. 430Each function listed here is described further in its own manual page. 431.Bl -tag -width indent 432.It "Archive Access" 433.Bl -tag -compact -width indent 434.It Fn elf_getarsym 435Retrieve the archive symbol table. 436.It Fn elf_getarhdr 437Retrieve the archive header for an object. 438.It Fn elf_getbase 439Retrieve the offset of a member inside an archive. 440.It Fn elf_next 441Iterate through an 442.Xr ar 1 443archive. 444.It Fn elf_rand 445Random access inside an 446.Xr ar 1 447archive. 448.El 449.It "Data Structures" 450.Bl -tag -compact -width indent 451.It Fn elf_getdata 452Retrieve translated data for an ELF section. 453.It Fn elf_getscn 454Retrieve the section descriptor for a named section. 455.It Fn elf_ndxscn 456Retrieve the index for a section. 457.It Fn elf_newdata 458Add a new 459.Vt Elf_Data 460descriptor to an ELF section. 461.It Fn elf_newscn 462Add a new section descriptor to an ELF descriptor. 463.It Fn elf_nextscn 464Iterate through the sections in an ELF object. 465.It Fn elf_rawdata 466Retrieve untranslated data for an ELF section. 467.It Fn elf_rawfile 468Return a pointer to the untranslated file contents for an ELF object. 469.It Fn elf32_getehdr , Fn elf64_getehdr 470Retrieve the Executable Header in an ELF object. 471.It Fn elf32_getphdr , Fn elf64_getphdr 472Retrieve the Program Header Table in an ELF object. 473.It Fn elf32_getshdr , Fn elf64_getshdr 474Retrieve the ELF section header associated with an 475.Vt Elf_Scn 476descriptor. 477.It Fn elf32_newehdr , Fn elf64_newehdr 478Allocate an Executable Header in an ELF object. 479.It Fn elf32_newphdr , Fn elf64_newphdr 480Allocate or resize the Program Header Table in an ELF object. 481.El 482.It "Data Translation" 483.Bl -tag -compact -width indent 484.It Fn elf32_xlatetof , Fn elf64_xlatetof 485Translate an ELF data structure from its native representation to its 486file representation. 487.It Fn elf32_xlatetom , Fn elf64_xlatetom 488Translate an ELF data structure from its file representation to a 489native representation. 490.El 491.It "Error Reporting" 492.Bl -tag -compact -width indent 493.It Fn elf_errno 494Retrieve the current error. 495.It Fn elf_errmsg 496Retrieve a human readable description of the current error. 497.El 498.It "Initialization" 499.Bl -tag -compact -width indent 500.It Fn elf_begin 501Opens an 502.Xr ar 1 503archive or ELF object given a file descriptor. 504.It Fn elf_end 505Close an ELF descriptor and release all its resources. 506.It Fn elf_memory 507Opens an 508.Xr ar 1 509archive or ELF object present in a memory arena. 510.It Fn elf_version 511Sets the operating version. 512.El 513.It "IO Control" 514.Bl -tag -width ".Fn elf_setshstrndx" -compact 515.It Fn elf_cntl 516Manage the association between and ELF descriptor and its underlying file. 517.It Fn elf_flagdata 518Mark an 519.Vt Elf_Data 520descriptor as dirty. 521.It Fn elf_flagehdr 522Mark the ELF Executable Header in an ELF descriptor as dirty. 523.It Fn elf_flagphdr 524Mark the ELF Program Header Table in an ELF descriptor as dirty. 525.It Fn elf_flagscn 526Mark an 527.Vt Elf_Scn 528descriptor as dirty. 529.It Fn elf_flagshdr 530Mark an ELF Section Header as dirty. 531.It Fn elf_setshstrndx 532Set the index of the section name string table for the ELF object. 533.It Fn elf_update 534Recompute ELF object layout and optionally write the modified object 535back to the underlying file. 536.El 537.It "Queries" 538.Bl -tag -width ".Fn elf_getshstrndx" -compact 539.It Fn elf32_checksum , Fn elf64_checkum 540Compute checksum of an ELF object. 541.It Fn elf_getident 542Retrieve the identification bytes for an ELF object. 543.It Fn elf_getphdrnum 544Retrieve the number of program headers in an ELF object. 545.It Fn elf_getshdrnum 546Retrieve the number of sections in an ELF object. 547.It Fn elf_getshdrstrndx 548Retrieve the section index of the section name string table in 549an ELF object. 550.It Fn elf_hash 551Compute the ELF hash value of a string. 552.It Fn elf_kind 553Query the kind of object associated with an ELF descriptor. 554.It Fn elf32_fsize , Fn elf64_fsize 555Return the size of the file representation of an ELF type. 556.El 557.El 558.Ss Controlling ELF Object Layout 559In the usual mode of operation, library will compute section 560offsets and alignments based on the contents of an ELF descriptor's 561sections without need for further intervention by the 562application. 563.Pp 564However, if the application wishes to take complete charge of the 565layout of the ELF file, it may set the 566.Dv ELF_F_LAYOUT 567flag on an ELF descriptor using 568.Xr elf_flagelf 3 , 569following which the library will use the data offsets and alignments 570specified by the application when laying out the file. 571Application control of file layout is described further in the 572.Xr elf_update 3 573manual page. 574.Pp 575Gaps in between sections will be filled with the fill character 576set by function 577.Fn elf_fill . 578.Ss Error Handling 579In case an error is encountered, these library functions set an 580internal error number and signal the presence of the error by 581returning a special return value. 582The application can check the 583current error number by calling 584.Xr elf_errno 3 . 585A human readable description of the recorded error is available by 586calling 587.Xr elf_errmsg 3 . 588.Ss Memory Management Rules 589The library keeps track of all 590.Vt Elf_Scn 591and 592.Vt Elf_Data 593descriptors associated with an ELF descriptor and recovers them 594when the descriptor is closed using 595.Xr elf_end 3 . 596Thus the application must not call 597.Xr free 3 598on data structures allocated by the ELF library. 599.Pp 600Conversely the library will not 601free data that it has not allocated. 602As an example, an application may call 603.Xr elf_newdata 3 604to allocate a new 605.Vt Elf_Data 606descriptor and can set the 607.Va d_off 608member of the descriptor to point to a region of memory allocated 609using 610.Xr malloc 3 . 611It is the applications responsibility to free this arena, though the 612library will reclaim the space used by the 613.Vt Elf_Data 614descriptor itself. 615.Sh SEE ALSO 616.Xr gelf 3 , 617.Xr ar 5 , 618.Xr elf 5 619.Sh HISTORY 620The original 621.Nm 622API was developed for 623.At V . 624The current implementation of the API appeared in 625.Fx 7.0 626and 627.Nx 6.0 . 628.Sh AUTHORS 629The ELF library was written by 630.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org . 631