1 .\" $NetBSD: elf_begin.3,v 1.2 2014/03/09 16:58:04 christos Exp $
3 .\" Copyright (c) 2006,2008-2011 Joseph Koshy. All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
14 .\" This software is provided by Joseph Koshy ``as is'' and
15 .\" any express or implied warranties, including, but not limited to, the
16 .\" implied warranties of merchantability and fitness for a particular purpose
17 .\" are disclaimed. in no event shall Joseph Koshy be liable
18 .\" for any direct, indirect, incidental, special, exemplary, or consequential
19 .\" damages (including, but not limited to, procurement of substitute goods
20 .\" or services; loss of use, data, or profits; or business interruption)
21 .\" however caused and on any theory of liability, whether in contract, strict
22 .\" liability, or tort (including negligence or otherwise) arising in any way
23 .\" out of the use of this software, even if advised of the possibility of
26 .\" Id: elf_begin.3 2313 2011-12-11 06:19:24Z jkoshy
33 .Nd open an ELF file or ar(1) archive
39 .Fn elf_begin "int fd" "Elf_Cmd cmd" "Elf *elf"
43 is used to open ELF files and
45 archives for further processing by other APIs in the
48 It is also used to access individual ELF members of an
50 archive in combination with the
58 is an open file descriptor returned from an
65 for reading or writing depending on the value of argument
69 is primarily used for iterating through archives.
73 can have the following values:
74 .Bl -tag -width "ELF_C_WRITE"
83 are ignored, and no additional error is signalled.
85 This value is to be when the application wishes to examine (but not
86 modify) the contents of the file specified by the arguments
90 It can be used for both
92 archives and for ELF objects.
96 is NULL, the library will allocate a new ELF descriptor for the file
100 should have been opened for reading.
104 is not NULL, and references a regular ELF file previously opened with
106 then the activation count for the descriptor referenced by argument
109 The value in argument
111 should match that used to open the descriptor argument
116 is not NULL, and references a descriptor for an
118 archive opened earlier with
120 a descriptor for an element in the archive is returned as
121 described in the section
122 .Sx "Processing ar(1) archives"
124 The value for argument
126 should match that used to open the archive earlier.
130 is not NULL, and references an
132 archive opened earlier with
134 then the value of the argument
138 This command is used to prepare an ELF file for reading and writing.
139 This command is not supported for
145 should have been opened for reading and writing.
148 is NULL, the library will allocate a new ELF descriptor for
149 the file being processed.
152 is non-null, it should point to a descriptor previously
155 with the same values for arguments
159 in this case the library will increment the activation count for descriptor
161 and return the same descriptor.
163 Changes to the in-memory image of the ELF file may be written back to
168 This command is used when the application wishes to create a new ELF
172 should have been opened for writing.
175 is ignored, and the previous contents of file referenced by argument
179 .Ss Processing ar(1) archives
182 archive may be opened in read mode (with argument
190 The returned ELF descriptor can be passed into to
193 to access individual members of the archive.
195 Random access within an opened archive is possible using
202 The symbol table of the archive may be retrieved
206 The function returns a pointer to a ELF descriptor if successful, or NULL
207 if an error occurred.
209 To iterate through the members of an
212 .Bd -literal -offset indent
217 if ((ar_e = elf_begin(fd, c, (Elf *) 0)) == 0) {
218 \&... handle error in opening the archive ...
220 while ((elf_e = elf_begin(fd, c, ar_e)) != 0) {
221 \&... process member referenced by elf_e here ...
227 To create a new ELF file, use:
228 .Bd -literal -offset indent
232 if ((fd = open("filename", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
233 \&... handle the error from open(2) ...
235 if ((e = elf_begin(fd, ELF_C_WRITE, (Elf *) 0)) == 0) {
236 \&... handle the error from elf_begin() ...
238 \&... create the ELF image using other elf(3) APIs ...
239 elf_update(e, ELF_C_WRITE);
245 can fail with the following errors:
246 .Bl -tag -width "[ELF_E_RESOURCE]"
247 .It Bq Er ELF_E_ARCHIVE
248 The archive denoted by argument
251 .It Bq Er ELF_E_ARGUMENT
252 The value in argument
255 .It Bq Er ELF_E_ARGUMENT
256 A non-null value for argument
262 .It Bq Er ELF_E_ARGUMENT
263 The value of argument
265 differs from the one the ELF descriptor
268 .It Bq Er ELF_E_ARGUMENT
271 differs from the value specified when ELF descriptor
274 .It Bq Er ELF_E_ARGUMENT
277 archive was opened with with
281 .It Bq Er ELF_E_ARGUMENT
282 The file referenced by argument
285 .It Bq Er ELF_E_ARGUMENT
286 The underlying file for argument
288 was of an unsupported type.
290 The file descriptor in argument
294 The file descriptor in argument
296 could not be read or written to.
297 .It Bq Er ELF_E_RESOURCE
298 An out of memory condition was encountered.
299 .It Bq Er ELF_E_SEQUENCE
302 was called before a working version was established with
304 .It Bq Er ELF_E_VERSION
305 The ELF object referenced by argument
307 was of an unsupported ELF version.