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