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