1 .\" $NetBSD: elf.3,v 1.2 2009/12/21 18:27:48 thorpej Exp $
3 .\" Copyright (c) 2006,2007 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.3,v 1.5.2.1.2.1 2009/10/25 01:10:29 kensmith Exp $
33 .Nd API for manipulating ELF objects
41 library provides functions that allow an application to read and
42 manipulate ELF object files, and to read
45 The library allows the manipulation of ELF objects in a byte ordering
46 and word-size independent way, allowing an application to read and
47 create ELF objects for 32 and 64 bit architectures and for little-
48 and big-endian machines.
49 The library is capable of processing ELF objects that use extended
52 This manual page serves to provide an overview of the functionality in
54 Further information may found in the manual pages for individual
56 functions that comprise the library.
60 ELF files contain several data structures that are laid out in a
62 ELF files begin with an
63 .Dq Executable Header ,
64 and may contain an optional
65 .Dq Program Header Table ,
66 and optional data in the form of ELF
69 .Dq Section Header Table
70 describes the content of the data in these sections.
72 ELF objects have an associated
74 which denotes the natural machine word size for the architecture
75 the object is associated with.
76 Objects for 32 bit architectures have an ELF class of
78 Objects for 64 bit architectures have an ELF class of
81 ELF objects also have an associated
83 which denotes the endianness of the machine architecture associated
87 for little-endian architectures and
89 for big-endian architectures.
91 ELF objects are also associated with an API version number.
92 This version number determines the layout of the individual components
93 of an ELF file and the semantics associated with these.
94 .Ss Data Representation And Translation
97 library distinguishes between
99 representations of ELF data structures and their
103 An application would work with ELF data in its
105 representation, i.e., using the native byteorder and alignment mandated
106 by the processor the application is running on.
109 representation of the same data could use a different byte ordering
110 and follow different constraints on object alignment than these native
115 library offers translation facilities
116 .Xr ( elf32_xlatetof 3 ,
117 .Xr elf32_xlatetom 3 ,
120 .Xr elf64_xlatetom 3 )
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
129 The ELF version that the application was compiled against.
130 This version determines the ABI expected by the application.
132 The ELF version of the ELF object being manipulated by the
133 application through the ELF library.
135 The ELF version (or set of versions) supported by the ELF library itself.
138 In order to facilitate working with ELF objects of differing versions,
139 the ELF library requires the application to call the
141 function before invoking many of its operations, in order to inform
142 the library of the application's desired working version.
144 In the current implementation, all three versions have to be
147 The ELF library uses the following prefixes:
148 .Bl -tag -width "ELF_F_*"
150 Used for class-independent functions.
152 Used for functions working with 32 bit ELF objects.
154 Used for functions working with 64 bit ELF objects.
156 Used for class-independent data types.
158 Used for command values used in a few functions.
159 These symbols are defined as members of the
163 Used for error numbers.
167 These constants define the kind of file associated with an ELF
171 The symbols are defined by the
175 These values are defined by the
177 enumeration, and denote the types of ELF data structures
178 that can be present in an ELF object.
181 Applications communicate with the library using descriptors.
183 .Bl -tag -width ".Vt Elf_Data"
187 descriptor represents an ELF object or an
190 It is allocated using one of the
197 descriptor can be used to read and write data to an ELF file.
200 descriptor can be associated with zero or more
204 Given an ELF descriptor, the application may retrieve the ELF
205 object's class-dependent
206 .Dq "Executable Header"
212 A new Ehdr structure may be allocated using the
219 .Dq "Program Header Table"
220 associated with an ELF descriptor may be allocated using the
225 A new program header table may be allocated or an existing table
234 structure is opaque and has no members visible to the
236 .\" TODO describe the Elf_Arhdr and Elf_Arsym structures.
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
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.
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.
263 descriptors are usually associated with
266 Existing data descriptors associated with an ELF section may be
267 structures are retrieved using the
272 function may be used to attach new data descriptors to an ELF section.
275 descriptors represent a section in an ELF object.
277 They are retrieved using the
280 An application may iterate through the existing sections of an ELF
284 New sections may be allocated using the
290 descriptor is opaque and contains no application modifiable fields.
292 .Ss Supported Elf Types
293 The following ELF datatypes are supported by the library.
295 .Bl -tag -width ".Dv ELF_T_SYMINFO" -compact
300 The library will not attempt to translate byte data.
302 Software and hardware capability records.
304 Records used in a section of type
307 ELF executable header.
309 16-bit unsigned words.
311 64 bit unsigned words.
314 .\".It Dv ELF_T_MOVEP
315 .\" As yet unsupported.
321 ELF program header table entries.
323 ELF relocation entries.
325 ELF relocation entries with addends.
327 ELF section header entries.
333 ELF symbol information.
335 ELF symbol table entries.
337 Symbol version definition records.
339 Symbol version requirement records.
341 Unsigned 32-bit words.
343 Unsigned 64-bit words.
348 denotes the number of Elf types known to the library.
350 The following table shows the mapping between ELF section types
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 So .dynamic Sc section entries.
357 .It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking.
358 .It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers.
359 .It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker.
360 .It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes.
361 .It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers.
362 .It Dv SHT_NOBITS Ta Dv ELF_T_BYTE Ta Empty sections. See Xr elf 5 .
363 .It Dv SHT_NOTE Ta Dv ELF_T_NOTE Ta ELF note records.
364 .It Dv SHT_PREINIT_ARRAY Ta Dv ELF_T_ADDR Ta Pre-initialization function pointers.
365 .It Dv SHT_PROGBITS Ta Dv ELF_T_BYTE Ta Machine code.
366 .It Dv SHT_REL Ta Dv ELF_T_REL Ta ELF relocation records.
367 .It Dv SHT_RELA Ta Dv ELF_T_RELA Ta Relocation records with addends.
368 .It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables.
369 .It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables.
370 .It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering.
371 .It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions.
372 .It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements.
373 .It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols.
374 .It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records.
375 .It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags.
377 .Ss Functional Grouping
378 This section contains a brief overview of the available functionality
380 Each function listed here is described further in its own manual page.
381 .Bl -tag -width indent
385 Retrieve the archive symbol table.
387 Retrieve the archive header for an object.
389 Retrieve the offset of a member inside an archive.
395 Random access inside an
399 .It "Data Structures"
402 Retrieve translated data for an ELF section.
404 Retrieve the section descriptor for a named section.
406 Retrieve the index for a section.
410 descriptor to an ELF section.
412 Add a new section descriptor to an ELF descriptor.
414 Iterate through the sections in an ELF object.
416 Retrieve untranslated data for an ELF sectino.
418 Return a pointer to the untranslated file contents for an ELF object.
419 .It Fn elf32_getehdr , Fn elf64_getehdr
420 Retrieve the Executable Header in an ELF object.
421 .It Fn elf32_getphdr , Fn elf64_getphdr
422 Retrieve the Program Header Table in an ELF object.
423 .It Fn elf32_getshdr , Fn elf64_getshdr
424 Retrieve the ELF section header associated with an
427 .It Fn elf32_newehdr , Fn elf64_newehdr
428 Allocate an Executable Header in an ELF object.
429 .It Fn elf32_newphdr , Fn elf64_newphdr
430 Allocate or resize the Program Header Table in an ELF object.
432 .It "Data Translation"
434 .It Fn elf32_xlatetof , Fn elf64_xlatetof
435 Translate an ELF data structure from its native representation to its
437 .It Fn elf32_xlatetom , Fn elf64_xlatetom
438 Translate an ELF data structure from its file representation to a
439 native representation.
441 .It "Error Reporting"
444 Retrieve the current error.
446 Retrieve a human readable description of the current error.
453 archive or ELF object given a file descriptor.
455 Close an ELF descriptor and release all its resources.
459 archive or ELF object present in a memory arena.
461 Sets the operating version.
464 .Bl -tag -width ".Fn elf_setshstrndx" -compact
466 Manage the association between and ELF descriptor and its underlying file.
472 Mark the ELF Executable Header in an ELF descriptor as dirty.
474 Mark the ELF Program Header Table in an ELF descriptor as dirty.
480 Mark an ELF Section Header as dirty.
481 .It Fn elf_setshstrndx
482 Set the index of the section name string table for the ELF object.
484 Recompute ELF object layout and optionally write the modified object
485 back to the underlying file.
488 .Bl -tag -width ".Fn elf_getshstrndx" -compact
489 .It Fn elf32_checksum , Fn elf64_checkum
490 Compute checksum of an ELF object.
492 Retrieve the identification bytes for an ELF object.
494 Retrieve the number of sections in an ELF object.
495 .It Fn elf_getshstrndx
496 Retrieve the section index of the section name string table in
499 Compute the ELF hash value of a string.
501 Query the kind of object associated with an ELF descriptor.
502 .It Fn elf32_fsize , Fn elf64_fsize
503 Return the size of the file representation of an ELF type.
506 .Ss Controlling ELF Object Layout
507 In the usual mode of operation, library will compute section
508 offsets and alignments based on the contents of an ELF descriptor's
509 sections without need for further intervention by the
512 However, if the application wishes to take complete charge of the
513 layout of the ELF file, it may set the
515 flag on an ELF descriptor using
517 following which the library will use the data offsets and alignments
518 specified by the application when laying out the file.
519 Application control of file layout is described further in the
523 Gaps in between sections will be filled with the fill character
527 In case an error is encountered, these library functions set an
528 internal error number and signal the presence of the error by
529 returning an special return value.
530 The application can check the
531 current error number by calling
533 A human readable description of the recorded error is available by
536 .Ss Memory Management Rules
537 The library keeps track of all
541 descriptors associated with an ELF descriptor and recovers them
542 when the descriptor is closed using
544 Thus the application must not call
546 on data structures allocated by the ELF library.
548 Conversely the library will not
549 free data that it has not allocated.
550 As an example, an application may call
554 descriptor and can set the
556 member of the descriptor to point to a region of memory allocated
559 It is the applications responsibility to free this arena, though the
560 library will reclaim the space used by the
567 The original ELF(3) API was developed for Unix System V.
568 This implementation first appeared in
573 The ELF library was written by
575 .Aq jkoshy@FreeBSD.org .