]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libelf/elf.3
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libelf / elf.3
1 .\" Copyright (c) 2006,2007 Joseph Koshy.  All rights reserved.
2 .\"
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions
5 .\" are met:
6 .\" 1. Redistributions of source code must retain the above copyright
7 .\"    notice, this list of conditions and the following disclaimer.
8 .\" 2. Redistributions in binary form must reproduce the above copyright
9 .\"    notice, this list of conditions and the following disclaimer in the
10 .\"    documentation and/or other materials provided with the distribution.
11 .\"
12 .\" This software is provided by Joseph Koshy ``as is'' and
13 .\" any express or implied warranties, including, but not limited to, the
14 .\" implied warranties of merchantability and fitness for a particular purpose
15 .\" are disclaimed.  in no event shall Joseph Koshy be liable
16 .\" for any direct, indirect, incidental, special, exemplary, or consequential
17 .\" damages (including, but not limited to, procurement of substitute goods
18 .\" or services; loss of use, data, or profits; or business interruption)
19 .\" however caused and on any theory of liability, whether in contract, strict
20 .\" liability, or tort (including negligence or otherwise) arising in any way
21 .\" out of the use of this software, even if advised of the possibility of
22 .\" such damage.
23 .\"
24 .\" $FreeBSD$
25 .\"
26 .Dd October 21, 2007
27 .Dt ELF 3
28 .Os
29 .Sh NAME
30 .Nm elf
31 .Nd API for manipulating ELF objects
32 .Sh LIBRARY
33 .Lb libelf
34 .Sh SYNOPSIS
35 .In libelf.h
36 .Sh DESCRIPTION
37 The
38 .Lb libelf
39 provides functions that allow an application to read and manipulate
40 ELF object files, and to read
41 .Xr ar 1
42 archives.
43 The library allows the manipulation of ELF objects in a byte ordering
44 and word-size independent way, allowing an application to read and
45 create ELF objects for 32 and 64 bit architectures and for little-
46 and big-endian machines.
47 The library is capable of processing ELF objects that use extended
48 section numbering.
49 .Pp
50 This manual page serves to provide an overview of the functionality in
51 the ELF library.
52 Further information may found in the manual pages for individual
53 .Xr ELF 3
54 functions that comprise the library.
55 .Ss ELF Concepts
56 As described in
57 .Xr elf 5 ,
58 ELF files contain several data structures that are laid out in a
59 specific way.
60 ELF files begin with an
61 .Dq Executable Header ,
62 and may contain an optional
63 .Dq Program Header Table ,
64 and optional data in the form of ELF
65 .Dq sections .
66 A
67 .Dq Section Header Table
68 describes the content of the data in these sections.
69 .Pp
70 ELF objects have an associated
71 .Dq "ELF class"
72 which denotes the natural machine word size for the architecture
73 the object is associated with.
74 Objects for 32 bit architectures have an ELF class of
75 .Dv ELFCLASS32 .
76 Objects for 64 bit architectures have an ELF class of
77 .Dv ELFCLASS64 .
78 .Pp
79 ELF objects also have an associated
80 .Dq endianness
81 which denotes the endianness of the machine architecture associated
82 with the object.
83 This may be
84 .Dv ELFDATA2LSB
85 for little-endian architectures and
86 .Dv ELFDATA2MSB
87 for big-endian architectures.
88 .Pp
89 ELF objects are also associated with an API version number.
90 This version number determines the layout of the individual components
91 of an ELF file and the semantics associated with these.
92 .Ss Data Representation And Translation
93 The
94 .Xr ELF 3
95 library distinguishes between
96 .Dq native
97 representations of ELF data structures and their
98 .Dq file
99 representations.
100 .Pp
101 An application would work with ELF data in its
102 .Dq native
103 representation, i.e., using the native byteorder and alignment mandated
104 by the processor the application is running on.
105 The
106 .Dq file
107 representation of the same data could use a different byte ordering
108 and follow different constraints on object alignment than these native
109 constraints.
110 .Pp
111 Accordingly, the
112 .Xr ELF 3
113 library offers translation facilities
114 .Po
115 .Xr elf32_xlatetof 3 ,
116 .Xr elf32_xlatetom 3 ,
117 .Xr elf64_xlatetof 3
118 and
119 .Xr elf64_xlatetom 3
120 .Pc
121 to and from these
122 representations and also provides higher-level APIs that retrieve and store
123 data from the ELF object in a transparent manner.
124 .Ss Library Working Version
125 Conceptually, there are three version numbers associated with an
126 application using the ELF library to manipulate ELF objects:
127 .Bl -bullet -compact -offset indent
128 .It
129 The ELF version that the application was compiled against.
130 This version determines the ABI expected by the application.
131 .It
132 The ELF version of the ELF object being manipulated by the
133 application through the ELF library.
134 .It
135 The ELF version (or set of versions) supported by the ELF library itself.
136 .El
137 .Pp
138 In order to facilitate working with ELF objects of differing versions,
139 the ELF library requires the application to call the
140 .Fn elf_version
141 function before invoking many of its operations, in order to inform
142 the library of the application's desired working version.
143 .Pp
144 In the current implementation, all three versions have to be
145 .Dv EV_CURRENT .
146 .Ss Namespace use
147 The ELF library uses the following prefixes:
148 .Bl -tag -width "ELF_F_*"
149 .It elf_*
150 Used for class-independent functions.
151 .It elf32_*
152 Used for functions working with 32 bit ELF objects.
153 .It elf64_*
154 Used for functions working with 64 bit ELF objects.
155 .It Elf_*
156 Used for class-independent data types.
157 .It ELF_C_*
158 Used for command values used in a few functions.
159 These symbols are defined as members of the
160 .Vt Elf_Cmd
161 enumeration.
162 .It ELF_E_*
163 Used for error numbers.
164 .It ELF_F_*
165 Used for flags.
166 .It ELF_K_*
167 These constants define the kind of file associated with an ELF
168 descriptor.
169 See
170 .Xr elf_kind 3 .
171 The symbols are defined by the
172 .Vt Elf_Kind
173 enumeration.
174 .It ELF_T_*
175 These values are defined by the
176 .Vt Elf_Type
177 enumeration, and denote the types of ELF data structures
178 that can be present in an ELF object.
179 .El
180 .Ss Descriptors
181 Applications communicate with the library using descriptors.
182 These are:
183 .Bl -tag -width ".Vt Elf_Data"
184 .It Vt Elf
185 An
186 .Vt Elf
187 descriptor represents an ELF object or an
188 .Xr ar 1
189 archive.
190 It is allocated using one of the
191 .Fn elf_begin
192 or
193 .Fn elf_memory
194 functions.
195 An
196 .Vt Elf
197 descriptor can be used to read and write data to an ELF file.
198 An
199 .Vt Elf
200 descriptor can be associated with zero or more
201 .Vt Elf_Scn
202 section descriptors.
203 .Pp
204 Given an ELF descriptor, the application may retrieve the ELF
205 object's class-dependent
206 .Dq "Executable Header"
207 structures using the
208 .Fn elf32_getehdr
209 or
210 .Fn elf64_getehdr
211 functions.
212 A new Ehdr structure may be allocated using the
213 .Fn elf64_newehdr
214 or
215 .Fn elf64_newehdr
216 functions.
217 .Pp
218 The
219 .Dq "Program Header Table"
220 associated with an ELF descriptor may be allocated using the
221 .Fn elf32_getphdr
222 or
223 .Fn elf64_getphdr
224 functions.
225 A new program header table may be allocated or an existing table
226 resized using the
227 .Fn elf32_newphdr
228 or
229 .Fn elf64_newphdr
230 functions.
231 .Pp
232 The
233 .Vt Elf
234 structure is opaque and has no members visible to the
235 application.
236 .\" TODO describe the Elf_Arhdr and Elf_Arsym structures.
237 .It Vt Elf_Data
238 An
239 .Vt Elf_Data
240 data structure describes an individual chunk of a ELF file as
241 represented in memory.
242 It has the following application visible members:
243 .Bl -tag -width ".Vt unsigned int d_version" -compact
244 .It Vt "uint64_t d_align"
245 The in-file alignment of the data buffer within its containing ELF section.
246 This value must be a power of two.
247 .It Vt "uint64_t d_off"
248 The offset with the containing section where this descriptors data
249 would be placed.
250 This field will be computed by the library unless the application
251 requests full control of the ELF object's layout.
252 .It Vt "uint64_t d_size"
253 The number of bytes of data in this descriptor.
254 .It Vt "void *d_buf"
255 A pointer to data in memory.
256 .It Vt "Elf_Type d_type"
257 The ELF type (see below) of the data in this descriptor.
258 .It Vt "unsigned int d_version"
259 The operating version for the data in this buffer.
260 .El
261 .Pp
262 .Vt Elf_Data
263 descriptors are usually associated with
264 .Vt Elf_Scn
265 descriptors.
266 Existing data descriptors associated with an ELF section may be
267 structures are retrieved using the
268 .Fn elf_getdata
269 function.
270 The
271 .Fn elf_newdata
272 function may be used to attach new data descriptors to an ELF section.
273 .It Vt Elf_Scn
274 .Vt Elf_Scn
275 descriptors represent a section in an ELF object.
276 .Pp
277 They are retrieved using the
278 .Fn elf_getscn
279 function.
280 An application may iterate through the existing sections of an ELF
281 object using the
282 .Fn elf_nextscn
283 function.
284 New sections may be allocated using the
285 .Fn elf_newscn
286 function.
287 .Pp
288 The
289 .Vt Elf_Scn
290 descriptor is opaque and contains no application modifiable fields.
291 .El
292 .Ss Supported Elf Types
293 The following ELF datatypes are supported by the library.
294 .Pp
295 .Bl -tag -width ".Dv ELF_T_SYMINFO" -compact
296 .It Dv ELF_T_ADDR
297 Machine addresses.
298 .It Dv ELF_T_BYTE
299 Byte data.
300 The library will not attempt to translate byte data.
301 .It Dv ELF_T_CAP
302 Software and hardware capability records.
303 .It Dv ELF_T_DYN
304 Records used in a section of type
305 .Dv SHT_DYNAMIC .
306 .It Dv ELF_T_EHDR
307 ELF executable header.
308 .It Dv ELF_T_HALF
309 16-bit unsigned words.
310 .It Dv ELF_T_LWORD
311 64 bit unsigned words.
312 .It Dv ELF_T_MOVE
313 ELF Move records.
314 .\".It Dv ELF_T_MOVEP
315 .\" As yet unsupported.
316 .It Dv ELF_T_NOTE
317 ELF Note structures.
318 .It Dv ELF_T_OFF
319 File offsets.
320 .It Dv ELF_T_PHDR
321 ELF program header table entries.
322 .It Dv ELF_T_REL
323 ELF relocation entries.
324 .It Dv ELF_T_RELA
325 ELF relocation entries with addends.
326 .It Dv ELF_T_SHDR
327 ELF section header entries.
328 .It Dv ELF_T_SWORD
329 Signed 32-bit words.
330 .It Dv ELF_T_SXWORD
331 Signed 64-bit words.
332 .It Dv ELF_T_SYMINFO
333 ELF symbol information.
334 .It Dv ELF_T_SYM
335 ELF symbol table entries.
336 .It Dv ELF_T_VDEF
337 Symbol version definition records.
338 .It Dv ELF_T_VNEED
339 Symbol version requirement records.
340 .It Dv ELF_T_WORD
341 Unsigned 32-bit words.
342 .It Dv ELF_T_XWORD
343 Unsigned 64-bit words.
344 .El
345 .Pp
346 The symbol
347 .Dv ELF_T_NUM
348 denotes the number of Elf types known to the library.
349 .Pp
350 The following table shows the mapping between ELF section types
351 defined in
352 .Xr elf 5
353 and the types supported by the library.
354 .Bl -column ".Dv SHT_PREINIT_ARRAY" ".Dv ELF_T_SYMINFO"
355 .It Em Section Type Ta Em "Library Type" Ta Em Description
356 .It Dv SHT_DYNAMIC Ta Dv ELF_T_DYN Ta Xo
357 .Sq .dynamic
358 section entries.
359 .Xc
360 .It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking.
361 .It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers.
362 .It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker.
363 .It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes.
364 .It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers.
365 .It Dv SHT_NOBITS Ta Dv ELF_T_BYTE Ta Xo
366 Empty sections.
367 See
368 .Xr elf 5 .
369 .Xc
370 .It Dv SHT_NOTE Ta Dv ELF_T_NOTE Ta ELF note records.
371 .It Dv SHT_PREINIT_ARRAY Ta Dv ELF_T_ADDR Ta Pre-initialization function pointers.
372 .It Dv SHT_PROGBITS Ta Dv ELF_T_BYTE Ta Machine code.
373 .It Dv SHT_REL Ta Dv ELF_T_REL Ta ELF relocation records.
374 .It Dv SHT_RELA Ta Dv ELF_T_RELA Ta Relocation records with addends.
375 .It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables.
376 .It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables.
377 .It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering.
378 .It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions.
379 .It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements.
380 .It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols.
381 .It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records.
382 .It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags.
383 .El
384 .Ss Functional Grouping
385 This section contains a brief overview of the available functionality
386 in the ELF library.
387 Each function listed here is described further in its own manual page.
388 .Bl -tag -width indent
389 .It "Archive Access"
390 .Bl -tag -compact
391 .It Fn elf_getarsym
392 Retrieve the archive symbol table.
393 .It Fn elf_getarhdr
394 Retrieve the archive header for an object.
395 .It Fn elf_getbase
396 Retrieve the offset of a member inside an archive.
397 .It Fn elf_next
398 Iterate through an
399 .Xr ar 1
400 archive.
401 .It Fn elf_rand
402 Random access inside an
403 .Xr ar 1
404 archive.
405 .El
406 .It "Data Structures"
407 .Bl -tag -compact
408 .It Fn elf_getdata
409 Retrieve translated data for an ELF section.
410 .It Fn elf_getscn
411 Retrieve the section descriptor for a named section.
412 .It Fn elf_ndxscn
413 Retrieve the index for a section.
414 .It Fn elf_newdata
415 Add a new
416 .Vt Elf_Data
417 descriptor to an ELF section.
418 .It Fn elf_newscn
419 Add a new section descriptor to an ELF descriptor.
420 .It Fn elf_nextscn
421 Iterate through the sections in an ELF object.
422 .It Fn elf_rawdata
423 Retrieve untranslated data for an ELF sectino.
424 .It Fn elf_rawfile
425 Return a pointer to the untranslated file contents for an ELF object.
426 .It Fn elf32_getehdr , Fn elf64_getehdr
427 Retrieve the Executable Header in an ELF object.
428 .It Fn elf32_getphdr , Fn elf64_getphdr
429 Retrieve the Program Header Table in an ELF object.
430 .It Fn elf32_getshdr , Fn elf64_getshdr
431 Retrieve the ELF section header associated with an
432 .Vt Elf_Scn
433 descriptor.
434 .It Fn elf32_newehdr , Fn elf64_newehdr
435 Allocate an Executable Header in an ELF object.
436 .It Fn elf32_newphdr , Fn elf64_newphdr
437 Allocate or resize the Program Header Table in an ELF object.
438 .El
439 .It "Data Translation"
440 .Bl -tag -compact
441 .It Fn elf32_xlatetof , Fn elf64_xlatetof
442 Translate an ELF data structure from its native representation to its
443 file representation.
444 .It Fn elf32_xlatetom , Fn elf64_xlatetom
445 Translate an ELF data structure from its file representation to a
446 native representation.
447 .El
448 .It "Error Reporting"
449 .Bl -tag -compact
450 .It Fn elf_errno
451 Retrieve the current error.
452 .It Fn elf_errmsg
453 Retrieve a human readable description of the current error.
454 .El
455 .It "Initialization"
456 .Bl -tag -compact
457 .It Fn elf_begin
458 Opens an
459 .Xr ar 1
460 archive or ELF object given a file descriptor.
461 .It Fn elf_end
462 Close an ELF descriptor and release all its resources.
463 .It Fn elf_memory
464 Opens an
465 .Xr ar 1
466 archive or ELF object present in a memory area.
467 .It Fn elf_version
468 Sets the operating version.
469 .El
470 .It "IO Control"
471 .Bl -tag -width ".Fn elf_setshstrndx" -compact
472 .It Fn elf_cntl
473 Manage the association between and ELF descriptor and its underlying file.
474 .It Fn elf_flagdata
475 Mark an
476 .Vt Elf_Data
477 descriptor as dirty.
478 .It Fn elf_flagehdr
479 Mark the ELF Executable Header in an ELF descriptor as dirty.
480 .It Fn elf_flagphdr
481 Mark the ELF Program Header Table in an ELF descriptor as dirty.
482 .It Fn elf_flagscn
483 Mark an
484 .Vt Elf_Scn
485 descriptor as dirty.
486 .It Fn elf_flagshdr
487 Mark an ELF Section Header as dirty.
488 .It Fn elf_setshstrndx
489 Set the index of the section name string table for the ELF object.
490 .It Fn elf_update
491 Recompute ELF object layout and optionally write the modified object
492 back to the underlying file.
493 .El
494 .It "Queries"
495 .Bl -tag -width ".Fn elf_getshstrndx" -compact
496 .It Fn elf32_checksum , Fn elf64_checkum
497 Compute checksum of an ELF object.
498 .It Fn elf_getident
499 Retrieve the identification bytes for an ELF object.
500 .It Fn elf_getshnum
501 Retrieve the number of sections in an ELF object.
502 .It Fn elf_getshstrndx
503 Retrieve the section index of the section name string table in
504 an ELF object.
505 .It Fn elf_hash
506 Compute the ELF hash value of a string.
507 .It Fn elf_kind
508 Query the kind of object associated with an ELF descriptor.
509 .It Fn elf32_fsize , Fn elf64_fsize
510 Return the size of the file representation of an ELF type.
511 .El
512 .El
513 .Ss Controlling ELF Object Layout
514 In the usual mode of operation, library will compute section
515 offsets and alignments based on the contents of an ELF descriptor's
516 sections without need for further intervention by the
517 application.
518 .Pp
519 However, if the application wishes to take complete charge of the
520 layout of the ELF file, it may set the
521 .Dv ELF_F_LAYOUT
522 flag on an ELF descriptor using
523 .Xr elf_flagelf 3 ,
524 following which the library will use the data offsets and alignments
525 specified by the application when laying out the file.
526 Application control of file layout is described further in the
527 .Xr elf_update 3
528 manual page.
529 .Pp
530 Gaps in between sections will be filled with the fill character
531 set by function
532 .Fn elf_fill .
533 .Ss Error Handling
534 In case an error is encountered, these library functions set an
535 internal error number and signal the presence of the error by
536 returning an special return value.
537 The application can check the
538 current error number by calling
539 .Xr elf_errno 3 .
540 A human readable description of the recorded error is available by
541 calling
542 .Xr elf_errmsg 3 .
543 .Ss Memory Management Rules
544 The library keeps track of all
545 .Vt Elf_Scn
546 and
547 .Vt Elf_Data
548 descriptors associated with an ELF descriptor and recovers them
549 when the descriptor is closed using
550 .Xr elf_end 3 .
551 Thus the application must not call
552 .Xr free 3
553 on data structures allocated by the ELF library.
554 .Pp
555 Conversely the library will not
556 free data that it has not allocated.
557 As an example, an application may call
558 .Xr elf_newdata 3
559 to allocate a new
560 .Vt Elf_Data
561 descriptor and can set the
562 .Va d_off
563 member of the descriptor to point to a region of memory allocated
564 using
565 .Xr malloc 3 .
566 It is the applications responsibility to free this area, though the
567 library will reclaim the space used by the
568 .Vt Elf_Data
569 descriptor itself.
570 .Sh SEE ALSO
571 .Xr gelf 3 ,
572 .Xr elf 5
573 .Sh HISTORY
574 The original ELF(3) API was developed for Unix System V.
575 The current implementation of the ELF(3) API appeared in
576 .Fx 7.0 .
577 .Sh AUTHORS
578 The ELF library was written by
579 .An "Joseph Koshy"
580 .Aq jkoshy@FreeBSD.org .