1*0a6a1f1dSLionel Sambuc.\" $NetBSD: elf_begin.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_begin.3 2313 2011-12-11 06:19:24Z jkoshy 27*0a6a1f1dSLionel Sambuc.\" 28*0a6a1f1dSLionel Sambuc.Dd December 11, 2011 29*0a6a1f1dSLionel Sambuc.Os 30*0a6a1f1dSLionel Sambuc.Dt ELF_BEGIN 3 31*0a6a1f1dSLionel Sambuc.Sh NAME 32*0a6a1f1dSLionel Sambuc.Nm elf_begin 33*0a6a1f1dSLionel Sambuc.Nd open an ELF file or ar(1) archive 34*0a6a1f1dSLionel Sambuc.Sh LIBRARY 35*0a6a1f1dSLionel Sambuc.Lb libelf 36*0a6a1f1dSLionel Sambuc.Sh SYNOPSIS 37*0a6a1f1dSLionel Sambuc.In libelf.h 38*0a6a1f1dSLionel Sambuc.Ft "Elf *" 39*0a6a1f1dSLionel Sambuc.Fn elf_begin "int fd" "Elf_Cmd cmd" "Elf *elf" 40*0a6a1f1dSLionel Sambuc.Sh DESCRIPTION 41*0a6a1f1dSLionel SambucFunction 42*0a6a1f1dSLionel Sambuc.Fn elf_begin 43*0a6a1f1dSLionel Sambucis used to open ELF files and 44*0a6a1f1dSLionel Sambuc.Xr ar 1 45*0a6a1f1dSLionel Sambucarchives for further processing by other APIs in the 46*0a6a1f1dSLionel Sambuc.Xr elf 3 47*0a6a1f1dSLionel Sambuclibrary. 48*0a6a1f1dSLionel SambucIt is also used to access individual ELF members of an 49*0a6a1f1dSLionel Sambuc.Xr ar 1 50*0a6a1f1dSLionel Sambucarchive in combination with the 51*0a6a1f1dSLionel Sambuc.Xr elf_next 3 52*0a6a1f1dSLionel Sambucand 53*0a6a1f1dSLionel Sambuc.Xr elf_rand 3 54*0a6a1f1dSLionel SambucAPIs. 55*0a6a1f1dSLionel Sambuc.Pp 56*0a6a1f1dSLionel SambucArgument 57*0a6a1f1dSLionel Sambuc.Ar fd 58*0a6a1f1dSLionel Sambucis an open file descriptor returned from an 59*0a6a1f1dSLionel Sambuc.Xr open 2 60*0a6a1f1dSLionel Sambucsystem call. 61*0a6a1f1dSLionel SambucFunction 62*0a6a1f1dSLionel Sambuc.Fn elf_begin 63*0a6a1f1dSLionel Sambucuses argument 64*0a6a1f1dSLionel Sambuc.Ar fd 65*0a6a1f1dSLionel Sambucfor reading or writing depending on the value of argument 66*0a6a1f1dSLionel Sambuc.Ar cmd . 67*0a6a1f1dSLionel SambucArgument 68*0a6a1f1dSLionel Sambuc.Ar elf 69*0a6a1f1dSLionel Sambucis primarily used for iterating through archives. 70*0a6a1f1dSLionel Sambuc.Pp 71*0a6a1f1dSLionel SambucThe argument 72*0a6a1f1dSLionel Sambuc.Ar cmd 73*0a6a1f1dSLionel Sambuccan have the following values: 74*0a6a1f1dSLionel Sambuc.Bl -tag -width "ELF_C_WRITE" 75*0a6a1f1dSLionel Sambuc.It ELF_C_NULL 76*0a6a1f1dSLionel SambucCauses 77*0a6a1f1dSLionel Sambuc.Fn elf_begin 78*0a6a1f1dSLionel Sambucto return NULL. 79*0a6a1f1dSLionel SambucArguments 80*0a6a1f1dSLionel Sambuc.Ar fd 81*0a6a1f1dSLionel Sambucand 82*0a6a1f1dSLionel Sambuc.Ar elf 83*0a6a1f1dSLionel Sambucare ignored, and no additional error is signalled. 84*0a6a1f1dSLionel Sambuc.It ELF_C_READ 85*0a6a1f1dSLionel SambucThis value is to be when the application wishes to examine (but not 86*0a6a1f1dSLionel Sambucmodify) the contents of the file specified by the arguments 87*0a6a1f1dSLionel Sambuc.Ar fd 88*0a6a1f1dSLionel Sambucand 89*0a6a1f1dSLionel Sambuc.Ar elf . 90*0a6a1f1dSLionel SambucIt can be used for both 91*0a6a1f1dSLionel Sambuc.Xr ar 1 92*0a6a1f1dSLionel Sambucarchives and for ELF objects. 93*0a6a1f1dSLionel Sambuc.Pp 94*0a6a1f1dSLionel SambucIf argument 95*0a6a1f1dSLionel Sambuc.Ar elf 96*0a6a1f1dSLionel Sambucis NULL, the library will allocate a new ELF descriptor for the file 97*0a6a1f1dSLionel Sambucbeing processed. 98*0a6a1f1dSLionel SambucThe argument 99*0a6a1f1dSLionel Sambuc.Ar fd 100*0a6a1f1dSLionel Sambucshould have been opened for reading. 101*0a6a1f1dSLionel Sambuc.Pp 102*0a6a1f1dSLionel SambucIf argument 103*0a6a1f1dSLionel Sambuc.Ar elf 104*0a6a1f1dSLionel Sambucis not NULL, and references a regular ELF file previously opened with 105*0a6a1f1dSLionel Sambuc.Fn elf_begin , 106*0a6a1f1dSLionel Sambucthen the activation count for the descriptor referenced by argument 107*0a6a1f1dSLionel Sambuc.Ar elf 108*0a6a1f1dSLionel Sambucis incremented. 109*0a6a1f1dSLionel SambucThe value in argument 110*0a6a1f1dSLionel Sambuc.Ar fd 111*0a6a1f1dSLionel Sambucshould match that used to open the descriptor argument 112*0a6a1f1dSLionel Sambuc.Ar elf . 113*0a6a1f1dSLionel Sambuc.Pp 114*0a6a1f1dSLionel SambucIf argument 115*0a6a1f1dSLionel Sambuc.Ar elf 116*0a6a1f1dSLionel Sambucis not NULL, and references a descriptor for an 117*0a6a1f1dSLionel Sambuc.Xr ar 1 118*0a6a1f1dSLionel Sambucarchive opened earlier with 119*0a6a1f1dSLionel Sambuc.Fn elf_begin , 120*0a6a1f1dSLionel Sambuca descriptor for an element in the archive is returned as 121*0a6a1f1dSLionel Sambucdescribed in the section 122*0a6a1f1dSLionel Sambuc.Sx "Processing ar(1) archives" 123*0a6a1f1dSLionel Sambucbelow. 124*0a6a1f1dSLionel SambucThe value for argument 125*0a6a1f1dSLionel Sambuc.Ar fd 126*0a6a1f1dSLionel Sambucshould match that used to open the archive earlier. 127*0a6a1f1dSLionel Sambuc.Pp 128*0a6a1f1dSLionel SambucIf argument 129*0a6a1f1dSLionel Sambuc.Ar elf 130*0a6a1f1dSLionel Sambucis not NULL, and references an 131*0a6a1f1dSLionel Sambuc.Xr ar 1 132*0a6a1f1dSLionel Sambucarchive opened earlier with 133*0a6a1f1dSLionel Sambuc.Fn elf_memory , 134*0a6a1f1dSLionel Sambucthen the value of the argument 135*0a6a1f1dSLionel Sambuc.Ar fd 136*0a6a1f1dSLionel Sambucis ignored. 137*0a6a1f1dSLionel Sambuc.It Dv ELF_C_RDWR 138*0a6a1f1dSLionel SambucThis command is used to prepare an ELF file for reading and writing. 139*0a6a1f1dSLionel SambucThis command is not supported for 140*0a6a1f1dSLionel Sambuc.Xr ar 1 141*0a6a1f1dSLionel Sambucarchives. 142*0a6a1f1dSLionel Sambuc.Pp 143*0a6a1f1dSLionel SambucArgument 144*0a6a1f1dSLionel Sambuc.Ar fd 145*0a6a1f1dSLionel Sambucshould have been opened for reading and writing. 146*0a6a1f1dSLionel SambucIf argument 147*0a6a1f1dSLionel Sambuc.Ar elf 148*0a6a1f1dSLionel Sambucis NULL, the library will allocate a new ELF descriptor for 149*0a6a1f1dSLionel Sambucthe file being processed. 150*0a6a1f1dSLionel SambucIf the argument 151*0a6a1f1dSLionel Sambuc.Ar elf 152*0a6a1f1dSLionel Sambucis non-null, it should point to a descriptor previously 153*0a6a1f1dSLionel Sambucallocated with 154*0a6a1f1dSLionel Sambuc.Fn elf_begin 155*0a6a1f1dSLionel Sambucwith the same values for arguments 156*0a6a1f1dSLionel Sambuc.Ar fd 157*0a6a1f1dSLionel Sambucand 158*0a6a1f1dSLionel Sambuc.Ar cmd ; 159*0a6a1f1dSLionel Sambucin this case the library will increment the activation count for descriptor 160*0a6a1f1dSLionel Sambuc.Ar elf 161*0a6a1f1dSLionel Sambucand return the same descriptor. 162*0a6a1f1dSLionel Sambuc.Pp 163*0a6a1f1dSLionel SambucChanges to the in-memory image of the ELF file may be written back to 164*0a6a1f1dSLionel Sambucdisk using the 165*0a6a1f1dSLionel Sambuc.Xr elf_update 3 166*0a6a1f1dSLionel Sambucfunction. 167*0a6a1f1dSLionel Sambuc.It Dv ELF_C_WRITE 168*0a6a1f1dSLionel SambucThis command is used when the application wishes to create a new ELF 169*0a6a1f1dSLionel Sambucfile. 170*0a6a1f1dSLionel SambucArgument 171*0a6a1f1dSLionel Sambuc.Ar fd 172*0a6a1f1dSLionel Sambucshould have been opened for writing. 173*0a6a1f1dSLionel SambucArgument 174*0a6a1f1dSLionel Sambuc.Ar elf 175*0a6a1f1dSLionel Sambucis ignored, and the previous contents of file referenced by argument 176*0a6a1f1dSLionel Sambuc.Ar fd 177*0a6a1f1dSLionel Sambucare overwritten. 178*0a6a1f1dSLionel Sambuc.El 179*0a6a1f1dSLionel Sambuc.Ss Processing ar(1) archives 180*0a6a1f1dSLionel SambucAn 181*0a6a1f1dSLionel Sambuc.Xr ar 1 182*0a6a1f1dSLionel Sambucarchive may be opened in read mode (with argument 183*0a6a1f1dSLionel Sambuc.Ar cmd 184*0a6a1f1dSLionel Sambucset to 185*0a6a1f1dSLionel Sambuc.Dv ELF_C_READ ) 186*0a6a1f1dSLionel Sambucusing 187*0a6a1f1dSLionel Sambuc.Fn elf_begin 188*0a6a1f1dSLionel Sambucor 189*0a6a1f1dSLionel Sambuc.Fn elf_memory . 190*0a6a1f1dSLionel SambucThe returned ELF descriptor can be passed into to 191*0a6a1f1dSLionel Sambucsubsequent calls to 192*0a6a1f1dSLionel Sambuc.Fn elf_begin 193*0a6a1f1dSLionel Sambucto access individual members of the archive. 194*0a6a1f1dSLionel Sambuc.Pp 195*0a6a1f1dSLionel SambucRandom access within an opened archive is possible using 196*0a6a1f1dSLionel Sambucthe 197*0a6a1f1dSLionel Sambuc.Xr elf_next 3 198*0a6a1f1dSLionel Sambucand 199*0a6a1f1dSLionel Sambuc.Xr elf_rand 3 200*0a6a1f1dSLionel Sambucfunctions. 201*0a6a1f1dSLionel Sambuc.Pp 202*0a6a1f1dSLionel SambucThe symbol table of the archive may be retrieved 203*0a6a1f1dSLionel Sambucusing 204*0a6a1f1dSLionel Sambuc.Xr elf_getarsym 3 . 205*0a6a1f1dSLionel Sambuc.Sh RETURN VALUES 206*0a6a1f1dSLionel SambucThe function returns a pointer to a ELF descriptor if successful, or NULL 207*0a6a1f1dSLionel Sambucif an error occurred. 208*0a6a1f1dSLionel Sambuc.Sh EXAMPLES 209*0a6a1f1dSLionel SambucTo iterate through the members of an 210*0a6a1f1dSLionel Sambuc.Xr ar 1 211*0a6a1f1dSLionel Sambucarchive, use: 212*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent 213*0a6a1f1dSLionel SambucElf_Cmd c; 214*0a6a1f1dSLionel SambucElf *ar_e, *elf_e; 215*0a6a1f1dSLionel Sambuc\&... 216*0a6a1f1dSLionel Sambucc = ELF_C_READ; 217*0a6a1f1dSLionel Sambucif ((ar_e = elf_begin(fd, c, (Elf *) 0)) == 0) { 218*0a6a1f1dSLionel Sambuc \&... handle error in opening the archive ... 219*0a6a1f1dSLionel Sambuc} 220*0a6a1f1dSLionel Sambucwhile ((elf_e = elf_begin(fd, c, ar_e)) != 0) { 221*0a6a1f1dSLionel Sambuc \&... process member referenced by elf_e here ... 222*0a6a1f1dSLionel Sambuc c = elf_next(elf_e); 223*0a6a1f1dSLionel Sambuc elf_end(elf_e); 224*0a6a1f1dSLionel Sambuc} 225*0a6a1f1dSLionel Sambuc.Ed 226*0a6a1f1dSLionel Sambuc.Pp 227*0a6a1f1dSLionel SambucTo create a new ELF file, use: 228*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent 229*0a6a1f1dSLionel Sambucint fd; 230*0a6a1f1dSLionel SambucElf *e; 231*0a6a1f1dSLionel Sambuc\&... 232*0a6a1f1dSLionel Sambucif ((fd = open("filename", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) { 233*0a6a1f1dSLionel Sambuc \&... handle the error from open(2) ... 234*0a6a1f1dSLionel Sambuc} 235*0a6a1f1dSLionel Sambucif ((e = elf_begin(fd, ELF_C_WRITE, (Elf *) 0)) == 0) { 236*0a6a1f1dSLionel Sambuc \&... handle the error from elf_begin() ... 237*0a6a1f1dSLionel Sambuc} 238*0a6a1f1dSLionel Sambuc\&... create the ELF image using other elf(3) APIs ... 239*0a6a1f1dSLionel Sambucelf_update(e, ELF_C_WRITE); 240*0a6a1f1dSLionel Sambucelf_end(e); 241*0a6a1f1dSLionel Sambuc.Ed 242*0a6a1f1dSLionel Sambuc.Sh ERRORS 243*0a6a1f1dSLionel SambucFunction 244*0a6a1f1dSLionel Sambuc.Fn elf_begin 245*0a6a1f1dSLionel Sambuccan fail with the following errors: 246*0a6a1f1dSLionel Sambuc.Bl -tag -width "[ELF_E_RESOURCE]" 247*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARCHIVE 248*0a6a1f1dSLionel SambucThe archive denoted by argument 249*0a6a1f1dSLionel Sambuc.Ar elf 250*0a6a1f1dSLionel Sambuccould not be parsed. 251*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 252*0a6a1f1dSLionel SambucThe value in argument 253*0a6a1f1dSLionel Sambuc.Ar cmd 254*0a6a1f1dSLionel Sambucwas unrecognized. 255*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 256*0a6a1f1dSLionel SambucA non-null value for argument 257*0a6a1f1dSLionel Sambuc.Ar elf 258*0a6a1f1dSLionel Sambucwas specified when 259*0a6a1f1dSLionel Sambuc.Ar cmd 260*0a6a1f1dSLionel Sambucwas set to 261*0a6a1f1dSLionel Sambuc.Dv ELF_C_RDWR . 262*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 263*0a6a1f1dSLionel SambucThe value of argument 264*0a6a1f1dSLionel Sambuc.Ar fd 265*0a6a1f1dSLionel Sambucdiffers from the one the ELF descriptor 266*0a6a1f1dSLionel Sambuc.Ar elf 267*0a6a1f1dSLionel Sambucwas created with. 268*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 269*0a6a1f1dSLionel SambucArgument 270*0a6a1f1dSLionel Sambuc.Ar cmd 271*0a6a1f1dSLionel Sambucdiffers from the value specified when ELF descriptor 272*0a6a1f1dSLionel Sambuc.Ar elf 273*0a6a1f1dSLionel Sambucwas created. 274*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 275*0a6a1f1dSLionel SambucAn 276*0a6a1f1dSLionel Sambuc.Xr ar 1 277*0a6a1f1dSLionel Sambucarchive was opened with with 278*0a6a1f1dSLionel Sambuc.Ar cmd 279*0a6a1f1dSLionel Sambucset to 280*0a6a1f1dSLionel Sambuc.Dv ELF_C_RDWR . 281*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 282*0a6a1f1dSLionel SambucThe file referenced by argument 283*0a6a1f1dSLionel Sambuc.Ar fd 284*0a6a1f1dSLionel Sambucwas empty. 285*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 286*0a6a1f1dSLionel SambucThe underlying file for argument 287*0a6a1f1dSLionel Sambuc.Ar fd 288*0a6a1f1dSLionel Sambucwas of an unsupported type. 289*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_IO 290*0a6a1f1dSLionel SambucThe file descriptor in argument 291*0a6a1f1dSLionel Sambuc.Ar fd 292*0a6a1f1dSLionel Sambucwas invalid. 293*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_IO 294*0a6a1f1dSLionel SambucThe file descriptor in argument 295*0a6a1f1dSLionel Sambuc.Ar fd 296*0a6a1f1dSLionel Sambuccould not be read or written to. 297*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_RESOURCE 298*0a6a1f1dSLionel SambucAn out of memory condition was encountered. 299*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_SEQUENCE 300*0a6a1f1dSLionel SambucFunction 301*0a6a1f1dSLionel Sambuc.Fn elf_begin 302*0a6a1f1dSLionel Sambucwas called before a working version was established with 303*0a6a1f1dSLionel Sambuc.Xr elf_version 3 . 304*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_VERSION 305*0a6a1f1dSLionel SambucThe ELF object referenced by argument 306*0a6a1f1dSLionel Sambuc.Ar fd 307*0a6a1f1dSLionel Sambucwas of an unsupported ELF version. 308*0a6a1f1dSLionel Sambuc.El 309*0a6a1f1dSLionel Sambuc.Sh SEE ALSO 310*0a6a1f1dSLionel Sambuc.Xr elf 3 , 311*0a6a1f1dSLionel Sambuc.Xr elf_end 3 , 312*0a6a1f1dSLionel Sambuc.Xr elf_errno 3 , 313*0a6a1f1dSLionel Sambuc.Xr elf_memory 3 , 314*0a6a1f1dSLionel Sambuc.Xr elf_next 3 , 315*0a6a1f1dSLionel Sambuc.Xr elf_rand 3 , 316*0a6a1f1dSLionel Sambuc.Xr elf_update 3 , 317*0a6a1f1dSLionel Sambuc.Xr gelf 3 318