1*0a6a1f1dSLionel Sambuc.\" $NetBSD: elf_getdata.3,v 1.2 2014/03/09 16:58:04 christos Exp $ 2*0a6a1f1dSLionel Sambuc.\" 3*0a6a1f1dSLionel Sambuc.\" Copyright (c) 2006,2008,2010-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_getdata.3 1766 2011-08-22 06:01:03Z jkoshy 27*0a6a1f1dSLionel Sambuc.\" 28*0a6a1f1dSLionel Sambuc.Dd January 26, 2011 29*0a6a1f1dSLionel Sambuc.Os 30*0a6a1f1dSLionel Sambuc.Dt ELF_GETDATA 3 31*0a6a1f1dSLionel Sambuc.Sh NAME 32*0a6a1f1dSLionel Sambuc.Nm elf_getdata , 33*0a6a1f1dSLionel Sambuc.Nm elf_newdata , 34*0a6a1f1dSLionel Sambuc.Nm elf_rawdata 35*0a6a1f1dSLionel Sambuc.Nd iterate through or allocate section data 36*0a6a1f1dSLionel Sambuc.Sh LIBRARY 37*0a6a1f1dSLionel Sambuc.Lb libelf 38*0a6a1f1dSLionel Sambuc.Sh SYNOPSIS 39*0a6a1f1dSLionel Sambuc.In libelf.h 40*0a6a1f1dSLionel Sambuc.Ft "Elf_Data *" 41*0a6a1f1dSLionel Sambuc.Fn elf_getdata "Elf_Scn *scn" "Elf_Data *data" 42*0a6a1f1dSLionel Sambuc.Ft "Elf_Data *" 43*0a6a1f1dSLionel Sambuc.Fn elf_newdata "Elf_Scn *scn" 44*0a6a1f1dSLionel Sambuc.Ft "Elf_Data *" 45*0a6a1f1dSLionel Sambuc.Fn elf_rawdata "Elf_Scn *scn" "Elf_Data *data" 46*0a6a1f1dSLionel Sambuc.Sh DESCRIPTION 47*0a6a1f1dSLionel SambucThese functions are used to access and manipulate data descriptors 48*0a6a1f1dSLionel Sambucassociated with section descriptors. 49*0a6a1f1dSLionel SambucData descriptors used by the ELF library are described in 50*0a6a1f1dSLionel Sambuc.Xr elf 3 . 51*0a6a1f1dSLionel Sambuc.Pp 52*0a6a1f1dSLionel SambucFunction 53*0a6a1f1dSLionel Sambuc.Fn elf_getdata 54*0a6a1f1dSLionel Sambucwill return the next data descriptor associated with section descriptor 55*0a6a1f1dSLionel Sambuc.Ar scn . 56*0a6a1f1dSLionel SambucThe returned data descriptor will be setup to contain translated data. 57*0a6a1f1dSLionel SambucArgument 58*0a6a1f1dSLionel Sambuc.Ar data 59*0a6a1f1dSLionel Sambucmay be 60*0a6a1f1dSLionel Sambuc.Dv NULL , 61*0a6a1f1dSLionel Sambucin which case the function returns the first data descriptor 62*0a6a1f1dSLionel Sambucassociated with section 63*0a6a1f1dSLionel Sambuc.Ar scn . 64*0a6a1f1dSLionel SambucIf argument 65*0a6a1f1dSLionel Sambuc.Ar data 66*0a6a1f1dSLionel Sambucis not 67*0a6a1f1dSLionel Sambuc.Dv NULL , 68*0a6a1f1dSLionel Sambucit must be a pointer to a data descriptor associated with 69*0a6a1f1dSLionel Sambucsection descriptor 70*0a6a1f1dSLionel Sambuc.Ar scn , 71*0a6a1f1dSLionel Sambucand function 72*0a6a1f1dSLionel Sambuc.Fn elf_getdata 73*0a6a1f1dSLionel Sambucwill return a pointer to the next data descriptor for the section, 74*0a6a1f1dSLionel Sambucor 75*0a6a1f1dSLionel Sambuc.Dv NULL 76*0a6a1f1dSLionel Sambucwhen the end of the section's descriptor list is reached. 77*0a6a1f1dSLionel Sambuc.Pp 78*0a6a1f1dSLionel SambucFunction 79*0a6a1f1dSLionel Sambuc.Fn elf_newdata 80*0a6a1f1dSLionel Sambucwill allocate a new data descriptor and append it to the list of data 81*0a6a1f1dSLionel Sambucdescriptors associated with section descriptor 82*0a6a1f1dSLionel Sambuc.Ar scn . 83*0a6a1f1dSLionel SambucThe new data descriptor will be initialized as follows: 84*0a6a1f1dSLionel Sambuc.Bl -tag -width "d_version" -compact -offset indent 85*0a6a1f1dSLionel Sambuc.It Va d_align 86*0a6a1f1dSLionel SambucSet to 1. 87*0a6a1f1dSLionel Sambuc.It Va d_buf 88*0a6a1f1dSLionel SambucInitialized to. 89*0a6a1f1dSLionel Sambuc.Dv NULL . 90*0a6a1f1dSLionel Sambuc.It Va d_off 91*0a6a1f1dSLionel SambucSet to (off_t) -1. 92*0a6a1f1dSLionel SambucThis field is under application control if the 93*0a6a1f1dSLionel Sambuc.Dv ELF_F_LAYOUT 94*0a6a1f1dSLionel Sambucflag was set on the ELF descriptor. 95*0a6a1f1dSLionel Sambuc.It Va d_size 96*0a6a1f1dSLionel SambucSet to zero. 97*0a6a1f1dSLionel Sambuc.It Va d_type 98*0a6a1f1dSLionel SambucInitialized to 99*0a6a1f1dSLionel Sambuc.Dv ELF_T_BYTE . 100*0a6a1f1dSLionel Sambuc.It Va d_version 101*0a6a1f1dSLionel SambucSet to the current working version of the library, as set by 102*0a6a1f1dSLionel Sambuc.Xr elf_version 3 . 103*0a6a1f1dSLionel Sambuc.El 104*0a6a1f1dSLionel SambucThe application must set these values as appropriate before 105*0a6a1f1dSLionel Sambuccalling 106*0a6a1f1dSLionel Sambuc.Xr elf_update 3 . 107*0a6a1f1dSLionel SambucSection 108*0a6a1f1dSLionel Sambuc.Ar scn 109*0a6a1f1dSLionel Sambucmust be associated with an ELF file opened for writing. 110*0a6a1f1dSLionel SambucIf the application has not requested full control of layout by 111*0a6a1f1dSLionel Sambucsetting the 112*0a6a1f1dSLionel Sambuc.Dv ELF_F_LAYOUT 113*0a6a1f1dSLionel Sambucflag on descriptor 114*0a6a1f1dSLionel Sambuc.Ar elf , 115*0a6a1f1dSLionel Sambucthen the data referenced by the returned descriptor will be positioned 116*0a6a1f1dSLionel Sambucafter the existing content of the section, honoring the file alignment 117*0a6a1f1dSLionel Sambucspecified in member 118*0a6a1f1dSLionel Sambuc.Va d_align . 119*0a6a1f1dSLionel SambucOn successful completion of a call to 120*0a6a1f1dSLionel Sambuc.Fn elf_newdata , 121*0a6a1f1dSLionel Sambucthe ELF library will mark the section 122*0a6a1f1dSLionel Sambuc.Ar scn 123*0a6a1f1dSLionel Sambucas 124*0a6a1f1dSLionel Sambuc.Dq dirty . 125*0a6a1f1dSLionel Sambuc.Pp 126*0a6a1f1dSLionel SambucFunction 127*0a6a1f1dSLionel Sambuc.Fn elf_rawdata 128*0a6a1f1dSLionel Sambucis used to step through the data descriptors associated with 129*0a6a1f1dSLionel Sambucsection 130*0a6a1f1dSLionel Sambuc.Ar scn . 131*0a6a1f1dSLionel SambucIn contrast to function 132*0a6a1f1dSLionel Sambuc.Fn elf_getdata , 133*0a6a1f1dSLionel Sambucthis function returns untranslated data. 134*0a6a1f1dSLionel SambucIf argument 135*0a6a1f1dSLionel Sambuc.Ar data 136*0a6a1f1dSLionel Sambucis 137*0a6a1f1dSLionel Sambuc.Dv NULL , 138*0a6a1f1dSLionel Sambucthe first data descriptor associated with section 139*0a6a1f1dSLionel Sambuc.Ar scn 140*0a6a1f1dSLionel Sambucis returned. 141*0a6a1f1dSLionel SambucIf argument 142*0a6a1f1dSLionel Sambuc.Ar data 143*0a6a1f1dSLionel Sambucis not 144*0a6a1f1dSLionel Sambuc.Dv NULL , 145*0a6a1f1dSLionel Sambucis must be a data descriptor associated with 146*0a6a1f1dSLionel Sambucsection 147*0a6a1f1dSLionel Sambuc.Ar scn , 148*0a6a1f1dSLionel Sambucand function 149*0a6a1f1dSLionel Sambuc.Fn elf_rawdata 150*0a6a1f1dSLionel Sambucwill return the next data descriptor in the list, or 151*0a6a1f1dSLionel Sambuc.Dv NULL 152*0a6a1f1dSLionel Sambucif no further descriptors are present. 153*0a6a1f1dSLionel SambucFunction 154*0a6a1f1dSLionel Sambuc.Fn elf_rawdata 155*0a6a1f1dSLionel Sambucalways returns 156*0a6a1f1dSLionel Sambuc.Vt Elf_Data 157*0a6a1f1dSLionel Sambucstructures of type 158*0a6a1f1dSLionel Sambuc.Dv ELF_T_BYTE . 159*0a6a1f1dSLionel Sambuc.Ss Special handling of zero-sized and SHT_NOBITS sections 160*0a6a1f1dSLionel SambucFor sections of type 161*0a6a1f1dSLionel Sambuc.Dv SHT_NOBITS, 162*0a6a1f1dSLionel Sambucand for zero-sized sections, 163*0a6a1f1dSLionel Sambucthe functions 164*0a6a1f1dSLionel Sambuc.Fn elf_getdata 165*0a6a1f1dSLionel Sambucand 166*0a6a1f1dSLionel Sambuc.Fn elf_rawdata 167*0a6a1f1dSLionel Sambucreturn a pointer to a valid 168*0a6a1f1dSLionel Sambuc.Vt Elf_Data 169*0a6a1f1dSLionel Sambucstructure that has its 170*0a6a1f1dSLionel Sambuc.Va d_buf 171*0a6a1f1dSLionel Sambucmember set to 172*0a6a1f1dSLionel Sambuc.Dv NULL 173*0a6a1f1dSLionel Sambucand its 174*0a6a1f1dSLionel Sambuc.Va d_size 175*0a6a1f1dSLionel Sambucmember set to the size of the section. 176*0a6a1f1dSLionel Sambuc.Pp 177*0a6a1f1dSLionel SambucIf an application wishes to create a section of type 178*0a6a1f1dSLionel Sambuc.Dv SHT_NOBITS , 179*0a6a1f1dSLionel Sambucit should add a data buffer to the section using function 180*0a6a1f1dSLionel Sambuc.Fn elf_newdata . 181*0a6a1f1dSLionel SambucIt should then set the 182*0a6a1f1dSLionel Sambuc.Va d_buf 183*0a6a1f1dSLionel Sambucand 184*0a6a1f1dSLionel Sambuc.Va d_size 185*0a6a1f1dSLionel Sambucmembers of the returned 186*0a6a1f1dSLionel Sambuc.Vt Elf_Data 187*0a6a1f1dSLionel Sambucstructure to 188*0a6a1f1dSLionel Sambuc.Dv NULL 189*0a6a1f1dSLionel Sambucand the desired size of the section respectively. 190*0a6a1f1dSLionel Sambuc.Sh RETURN VALUES 191*0a6a1f1dSLionel SambucThese functions return a valid pointer to a data descriptor if successful, or 192*0a6a1f1dSLionel Sambuc.Dv NULL 193*0a6a1f1dSLionel Sambucif an error occurs. 194*0a6a1f1dSLionel Sambuc.Sh ERRORS 195*0a6a1f1dSLionel SambucThese functions may fail with the following errors: 196*0a6a1f1dSLionel Sambuc.Bl -tag -width "[ELF_E_RESOURCE]" 197*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 198*0a6a1f1dSLionel SambucEither of the arguments 199*0a6a1f1dSLionel Sambuc.Ar scn 200*0a6a1f1dSLionel Sambucor 201*0a6a1f1dSLionel Sambuc.Ar data 202*0a6a1f1dSLionel Sambucwas 203*0a6a1f1dSLionel Sambuc.Dv NULL . 204*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 205*0a6a1f1dSLionel SambucThe data descriptor referenced by argument 206*0a6a1f1dSLionel Sambuc.Ar data 207*0a6a1f1dSLionel Sambucis not associated with section descriptor 208*0a6a1f1dSLionel Sambuc.Ar scn . 209*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_ARGUMENT 210*0a6a1f1dSLionel SambucThe section denoted by argument 211*0a6a1f1dSLionel Sambuc.Ar scn 212*0a6a1f1dSLionel Sambuchad no data associated with it. 213*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_DATA 214*0a6a1f1dSLionel SambucRetrieval of data from the underlying object failed. 215*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_RESOURCE 216*0a6a1f1dSLionel SambucAn out of memory condition was detected. 217*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_SECTION 218*0a6a1f1dSLionel SambucSection 219*0a6a1f1dSLionel Sambuc.Ar scn 220*0a6a1f1dSLionel Sambuchad type 221*0a6a1f1dSLionel Sambuc.Dv SHT_NULL . 222*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_SECTION 223*0a6a1f1dSLionel SambucThe type of the section 224*0a6a1f1dSLionel Sambuc.Ar scn 225*0a6a1f1dSLionel Sambucwas not recognized by the library. 226*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_SECTION 227*0a6a1f1dSLionel SambucThe size of the section 228*0a6a1f1dSLionel Sambuc.Ar scn 229*0a6a1f1dSLionel Sambucis not a multiple of the file size for its section type. 230*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_SECTION 231*0a6a1f1dSLionel SambucThe file offset for section 232*0a6a1f1dSLionel Sambuc.Ar scn 233*0a6a1f1dSLionel Sambucis incorrect. 234*0a6a1f1dSLionel Sambuc.It Bq Er ELF_E_UNIMPL 235*0a6a1f1dSLionel SambucThe section type associated with section 236*0a6a1f1dSLionel Sambuc.Ar scn 237*0a6a1f1dSLionel Sambucis currently unsupported by the library. 238*0a6a1f1dSLionel Sambuc.El 239*0a6a1f1dSLionel Sambuc.Sh SEE ALSO 240*0a6a1f1dSLionel Sambuc.Xr elf 3 , 241*0a6a1f1dSLionel Sambuc.Xr elf_flagdata 3 , 242*0a6a1f1dSLionel Sambuc.Xr elf_flagscn 3 , 243*0a6a1f1dSLionel Sambuc.Xr elf_getscn 3 , 244*0a6a1f1dSLionel Sambuc.Xr elf_getshdr 3 , 245*0a6a1f1dSLionel Sambuc.Xr elf_newscn 3 , 246*0a6a1f1dSLionel Sambuc.Xr elf_rawfile 3 , 247*0a6a1f1dSLionel Sambuc.Xr elf_update 3 , 248*0a6a1f1dSLionel Sambuc.Xr elf_version 3 , 249*0a6a1f1dSLionel Sambuc.Xr gelf 3 250