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