1 .\" $NetBSD: elf_update.3,v 1.2 2014/03/09 16:58:04 christos Exp $
3 .\" Copyright (c) 2006-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_update.3 1729 2011-08-14 09:13:00Z jkoshy
33 .Nd update an ELF descriptor
39 .Fn elf_update "Elf *elf" "Elf_Cmd cmd"
43 causes the library to recalculate the structure of an ELF
44 object and optionally write out the image of the object
49 should reference a valid ELF descriptor.
53 can be one of the following values:
54 .Bl -tag -width "Dv ELF_C_WRITE"
56 The library will recalculate structural information flagging
57 modified structures with the
59 flag, but will not write data to the underlying file image.
61 The library will recalculate structural information and will
62 also write the new image to the underlying file.
63 The ELF descriptor referenced by argument
65 should permit the underlying ELF object to be written or updated
74 descriptors associated with descriptor
76 should be considered invalid after a call to
78 .Ss Specifying Object Layout
81 supports two layout modes.
82 .Bl -tag -width indent
86 flag is not set on the ELF descriptor, the ELF library will lay out
87 the ELF object according to the following scheme:
88 .Bl -tag -compact -width "Section Data"
90 The ELF executable header will be placed at the start of the object.
92 If the ELF descriptor contains a program header table, it will be
93 placed after the Executable Header.
95 ELF section data, if any, will be placed next, keeping each section's
96 alignment requirements in mind.
98 The ELF section header table, if any, will be placed last.
100 .It "Application Controlled Layout"
101 The application can take full control of the layout of the ELF object
104 flag on the ELF descriptor (see
105 .Xr elf_flagelf 3 ) .
106 In this case the library will lay out the ELF object using
107 application-supplied information as below:
109 .Bl -tag -compact -width "Section Data"
111 The ELF executable header will be placed at the start of the object.
113 The ELF program header table, if any, it will be placed at the offset
116 field of the ELF executable header.
118 The data for each ELF section will be placed at the offset specified
121 field of the section's header.
122 The size of the section will be taken from the
124 field of the section header.
126 The ELF section header table, if any, will be placed at the offset
129 field of the executable header.
133 Gaps in the coverage of the file's contents will be set to the fill value
136 .Ss Application Supplied Information
137 The application needs to set the following fields in the data
138 structures associated with the ELF descriptor prior to calling
140 .Bl -tag -width indent
141 .It "Executable Header"
142 The fields of the ELF executable header that need to be set by the
145 .Bl -tag -width "e_ident[EI_OSABI]" -compact
147 To be set to the desired entry address for executables.
149 To be set to the desired processor specific flags.
150 .It Va "e_ident[EI_DATA]"
151 Must be set to one of
155 .It Va "e_ident[EI_OSABI]"
156 To be set to the OS ABI desired.
159 executables, this field should be set to
160 .Dv ELFOSABI_FREEBSD .
162 To be set to the desired machine architecture, one of the
164 values in the header file
165 .In elfdefinitions.h .
167 If the application is managing the object's layout, it must
168 set this field to the file offset of the ELF program header table.
170 If the application is managing the object's layout, it must
171 set this field to the file offset of the ELF section header table.
173 To be set to the index of the string table containing
176 To be set to the type of the ELF object, one of the
178 values in the header file
179 .In elfdefinitions.h .
181 To be set to the desired version of the ELF object.
184 All fields of the entries in the program header table need to be
185 set by the application.
187 The fields of ELF section headers that need to be set by the
190 .Bl -tag -width "sh_addralign" -compact
192 To be set to the memory address where the section should reside.
194 If the application is managing the file layout, it must set this
195 field to the desired alignment for the section's contents.
196 This value must be a power of two and must be at least as large as the
197 largest alignment needed by any
199 descriptor associated with the section.
201 To be set to the size of each entry, for sections containing fixed size
202 elements, or set to zero for sections without fixed size elements.
203 If the application is not managing file layout, it may leave this
204 field as zero for those sections whose types are known to the library.
206 To be set to the desired section flags.
208 To be set as described in
211 To be set as described in
214 To be set to the index of the section's name in the string table
215 containing section names.
217 If the application is managing the file layout, it must set this
218 field to the file offset of the section's contents.
220 If the application is managing the file layout, it must set this
221 field to the file size of the section's contents.
223 To be set to the type of the section.
228 descriptors associated with each section specify its contents
230 .Xr elf_getdata 3 ) .
231 While all the fields in these descriptors are under application
232 control, the following fields influence object layout:
233 .Bl -tag -width "Va d_align" -compact
235 To be set to the desired alignment, within the containing section, of
236 the descriptor's data.
238 If the application is managing object layout, it must set this field
239 to the file offset, within the section, at which the descriptor's data
242 To be set to the size in bytes of the memory representation of the
249 returns the total size of the file image if successful, or -1 if an
252 This function may fail with the following errors:
253 .Bl -tag -width "[ELF_E_RESOURCE]"
254 .It Bq Er ELF_E_ARGUMENT
258 .It Bq Er ELF_E_ARGUMENT
262 .It Bq Er ELF_E_ARGUMENT
265 was not a descriptor for an ELF object.
266 .It Bq Er ELF_E_CLASS
268 .Va e_ident[EI_CLASS]
269 field of the executable header of argument
271 did not match the class of the file.
275 descriptor contained in argument
277 specified an unsupported type.
281 descriptor specified an alignment that was zero or was not a power of
283 .It Bq Er ELF_E_HEADER
284 The ELF header in argument
286 requested a different byte order from the byte order already
287 associated with the file.
289 An I/O error was encountered.
290 .It Bq Er ELF_E_LAYOUT
293 descriptor contained in argument
295 specified an alignment incompatible with its containing section.
296 .It Bq Er ELF_E_LAYOUT
299 contained section descriptors that overlapped in extent.
300 .It Bq Er ELF_E_LAYOUT
303 contained section descriptors that were incorrectly aligned or were
304 too small for their data.
305 .It Bq Er ELF_E_LAYOUT
308 was set on the Elf descriptor and the executable header overlapped
309 with the program header table.
310 .It Bq Er ELF_E_LAYOUT
313 was set on the Elf descriptor and the program header table was placed
314 at a misaligned file offset.
315 .It Bq Er ELF_E_LAYOUT
318 was set on the Elf descriptor and the section header table overlapped
319 an extent mapped by a section descriptor.
320 .It Bq Er ELF_E_LAYOUT
323 flag was set on the Elf descriptor, and the
327 descriptor contained a value that was not a multiple of the
328 descriptor's specified alignment.
332 operation was requested with an ELF descriptor that was not opened for
334 .It Bq Er ELF_E_SECTION
337 contained a section with an unrecognized type.
338 .It Bq Er ELF_E_SECTION
339 The section header at index
341 had an illegal section type.
342 .It Bq Er ELF_E_SEQUENCE
345 operation was requested after a prior call to
346 .Fn elf_cntl elf ELF_C_FDDONE
347 disassociated the ELF descriptor
349 from its underlying file.
350 .It Bq Er ELF_E_VERSION
353 had an unsupported version or contained an
355 descriptor with an unsupported version.
359 .Xr elf32_getehdr 3 ,
360 .Xr elf32_getphdr 3 ,
361 .Xr elf32_newehdr 3 ,
362 .Xr elf32_newphdr 3 ,
363 .Xr elf64_getehdr 3 ,
364 .Xr elf64_getphdr 3 ,
365 .Xr elf64_newehdr 3 ,
366 .Xr elf64_newphdr 3 ,