3 .\" Copyright (c) 2006 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 .\" $FreeBSD: src/lib/libelf/elf_begin.3,v 1.3.10.1.2.1 2009/10/25 01:10:29 kensmith Exp $
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 argument
88 It can be used for both
90 archives and for regular ELF files.
94 should have been opened for reading.
97 is NULL, the library will allocate a new ELF descriptor for
98 the file being processed.
101 is not NULL, and references a regular ELF file previously opened with
103 then the activation count for
108 is not NULL, and references a descriptor for an
110 archive opened earlier with
112 a descriptor for an element in the archive is returned as
113 described in the section
114 .Sx "Processing ar(1) archives"
117 The command is used to prepare an ELF file for reading and writing.
118 It is not supported for archives.
122 should have been opened for reading and writing.
125 is NULL, the library will allocate a new ELF descriptor for
126 the file being processed.
129 is non-null, it should point to a descriptor previously
132 with the same values for arguments
136 in this case the library will increment the activation count for descriptor
138 and return the same descriptor.
139 Changes to the in-memory image of the ELF file are written back to
144 This command is not valid for
148 This command is used when the application wishes to create a new ELF
152 should have been opened for writing.
155 is ignored, and the previous contents of file referenced by
160 .Ss Processing ar(1) archives
163 archive may be opened in read mode (with argument
169 The returned ELF descriptor can be passed into to
172 to access individual members of the archive.
174 Random access within an opened archive is possible using
181 The symbol table of the archive may be retrieved
185 The function returns a pointer to a ELF descriptor if successful, or NULL
186 if an error occurred.
188 To iterate through the members of an
191 .Bd -literal -offset indent
196 if ((ar_e = elf_begin(fd, c, (Elf *) 0)) == 0) {
197 \&... handle error in opening the archive ...
199 while ((elf_e = elf_begin(fd, c, ar_e)) != 0) {
200 \&... process member referenced by elf_e here ...
206 To create a new ELF file, use:
207 .Bd -literal -offset indent
211 if ((fd = open("filename", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
212 \&... handle the error from open(2) ...
214 if ((e = elf_begin(fd, ELF_C_WRITE, (Elf *) 0)) == 0) {
215 \&... handle the error from elf_begin() ...
217 \&... create the ELF image using other elf(3) APIs ...
218 elf_update(e, ELF_C_WRITE);
224 can fail with the following errors:
226 .Bl -tag -width "[ELF_E_RESOURCE]"
227 .It Bq Er ELF_E_ARGUMENT
228 An unrecognized value was specified in argument
230 .It Bq Er ELF_E_ARGUMENT
231 A non-null value for argument
237 .It Bq Er ELF_E_ARGUMENT
238 The value of argument
240 differs from the one the ELF descriptor
243 .It Bq Er ELF_E_ARGUMENT
246 differs from the value specified when ELF descriptor
249 .It Bq Er ELF_E_ARGUMENT
252 was not a descriptor for an
255 .It Bq Er ELF_E_ARGUMENT
258 archive was opened with with
265 was unable to truncate a file opened for writing using
267 .It Bq Er ELF_E_RESOURCE
268 An out of memory condition was encountered.
269 .It Bq Er ELF_E_SEQUENCE
272 was called before a working version was established with