1 .\" Copyright (c) 2006-2008 Joseph Koshy. All rights reserved.
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions
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.
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
31 .Nd API for manipulating ELF objects
39 provides functions that allow an application to read and manipulate
40 ELF object files, and to read
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
50 This manual page serves to provide an overview of the functionality in
52 Further information may found in the manual pages for individual
54 functions that comprise the library.
58 ELF files contain several data structures that are laid out in a
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
67 .Dq Section Header Table
68 describes the content of the data in these sections.
70 ELF objects have an associated
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
76 Objects for 64 bit architectures have an ELF class of
79 ELF objects also have an associated
81 which denotes the endianness of the machine architecture associated
85 for little-endian architectures and
87 for big-endian architectures.
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
95 library distinguishes between
97 representations of ELF data structures and their
101 An application would work with ELF data in its
103 representation, i.e., using the native byteorder and alignment mandated
104 by the processor the application is running on.
107 representation of the same data could use a different byte ordering
108 and follow different constraints on object alignment than these native
113 library offers translation facilities
114 .Xr ( elf32_xlatetof 3 ,
115 .Xr elf32_xlatetom 3 ,
118 .Xr elf64_xlatetom 3 )
120 representations and also provides higher-level APIs that retrieve and store
121 data from the ELF object in a transparent manner.
122 .Ss Library Working Version
123 Conceptually, there are three version numbers associated with an
124 application using the ELF library to manipulate ELF objects:
125 .Bl -bullet -compact -offset indent
127 The ELF version that the application was compiled against.
128 This version determines the ABI expected by the application.
130 The ELF version of the ELF object being manipulated by the
131 application through the ELF library.
133 The ELF version (or set of versions) supported by the ELF library itself.
136 In order to facilitate working with ELF objects of differing versions,
137 the ELF library requires the application to call the
139 function before invoking many of its operations, in order to inform
140 the library of the application's desired working version.
142 In the current implementation, all three versions have to be
145 The ELF library uses the following prefixes:
146 .Bl -tag -width "ELF_F_*"
148 Used for class-independent functions.
150 Used for functions working with 32 bit ELF objects.
152 Used for functions working with 64 bit ELF objects.
154 Used for class-independent data types.
156 Used for command values used in a few functions.
157 These symbols are defined as members of the
161 Used for error numbers.
165 These constants define the kind of file associated with an ELF
169 The symbols are defined by the
173 These values are defined by the
175 enumeration, and denote the types of ELF data structures
176 that can be present in an ELF object.
179 In addition, the library uses symbols with prefixes
183 for its internal use.
185 Applications communicate with the library using descriptors.
187 .Bl -tag -width ".Vt Elf_Data"
191 descriptor represents an ELF object or an
194 It is allocated using one of the
201 descriptor can be used to read and write data to an ELF file.
204 descriptor can be associated with zero or more
208 Given an ELF descriptor, the application may retrieve the ELF
209 object's class-dependent
210 .Dq "Executable Header"
216 A new Ehdr structure may be allocated using the
223 .Dq "Program Header Table"
224 associated with an ELF descriptor may be allocated using the
229 A new program header table may be allocated or an existing table
238 structure is opaque and has no members visible to the
240 .\" TODO describe the Elf_Arhdr and Elf_Arsym structures.
244 data structure describes an individual chunk of a ELF file as
245 represented in memory.
246 It has the following application visible members:
247 .Bl -tag -width ".Vt unsigned int d_version" -compact
248 .It Vt "uint64_t d_align"
249 The in-file alignment of the data buffer within its containing ELF section.
250 This value must be a power of two.
251 .It Vt "uint64_t d_off"
252 The offset with the containing section where this descriptors data
254 This field will be computed by the library unless the application
255 requests full control of the ELF object's layout.
256 .It Vt "uint64_t d_size"
257 The number of bytes of data in this descriptor.
259 A pointer to data in memory.
260 .It Vt "Elf_Type d_type"
261 The ELF type (see below) of the data in this descriptor.
262 .It Vt "unsigned int d_version"
263 The operating version for the data in this buffer.
267 descriptors are usually associated with
270 Existing data descriptors associated with an ELF section may be
271 structures are retrieved using the
276 function may be used to attach new data descriptors to an ELF section.
279 descriptors represent a section in an ELF object.
281 They are retrieved using the
284 An application may iterate through the existing sections of an ELF
288 New sections may be allocated using the
294 descriptor is opaque and contains no application modifiable fields.
296 .Ss Supported Elf Types
297 The following ELF datatypes are supported by the library.
299 .Bl -tag -width ".Dv ELF_T_SYMINFO" -compact
304 The library will not attempt to translate byte data.
306 Software and hardware capability records.
308 Records used in a section of type
311 ELF executable header.
313 16-bit unsigned words.
315 64 bit unsigned words.
318 .\".It Dv ELF_T_MOVEP
319 .\" As yet unsupported.
325 ELF program header table entries.
327 ELF relocation entries.
329 ELF relocation entries with addends.
331 ELF section header entries.
337 ELF symbol information.
339 ELF symbol table entries.
341 Symbol version definition records.
343 Symbol version requirement records.
345 Unsigned 32-bit words.
347 Unsigned 64-bit words.
352 denotes the number of Elf types known to the library.
354 The following table shows the mapping between ELF section types
357 and the types supported by the library.
358 .Bl -column ".Dv SHT_PREINIT_ARRAY" ".Dv ELF_T_SYMINFO"
359 .It Em Section Type Ta Em "Library Type" Ta Em Description
360 .It Dv SHT_DYNAMIC Ta Dv ELF_T_DYN Ta Xo
364 .It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking.
365 .It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers.
366 .It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker.
367 .It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes.
368 .It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers.
369 .It Dv SHT_NOBITS Ta Dv ELF_T_BYTE Ta Xo
374 .It Dv SHT_NOTE Ta Dv ELF_T_NOTE Ta ELF note records.
375 .It Dv SHT_PREINIT_ARRAY Ta Dv ELF_T_ADDR Ta Pre-initialization function pointers.
376 .It Dv SHT_PROGBITS Ta Dv ELF_T_BYTE Ta Machine code.
377 .It Dv SHT_REL Ta Dv ELF_T_REL Ta ELF relocation records.
378 .It Dv SHT_RELA Ta Dv ELF_T_RELA Ta Relocation records with addends.
379 .It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables.
380 .It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables.
381 .It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering.
382 .It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions.
383 .It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements.
384 .It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols.
385 .It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records.
386 .It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags.
389 .Ss Functional Grouping
390 This section contains a brief overview of the available functionality
392 Each function listed here is described further in its own manual page.
393 .Bl -tag -width indent
397 Retrieve the archive symbol table.
399 Retrieve the archive header for an object.
401 Retrieve the offset of a member inside an archive.
407 Random access inside an
411 .It "Data Structures"
414 Retrieve translated data for an ELF section.
416 Retrieve the section descriptor for a named section.
418 Retrieve the index for a section.
422 descriptor to an ELF section.
424 Add a new section descriptor to an ELF descriptor.
426 Iterate through the sections in an ELF object.
428 Retrieve untranslated data for an ELF sectino.
430 Return a pointer to the untranslated file contents for an ELF object.
431 .It Fn elf32_getehdr , Fn elf64_getehdr
432 Retrieve the Executable Header in an ELF object.
433 .It Fn elf32_getphdr , Fn elf64_getphdr
434 Retrieve the Program Header Table in an ELF object.
435 .It Fn elf32_getshdr , Fn elf64_getshdr
436 Retrieve the ELF section header associated with an
439 .It Fn elf32_newehdr , Fn elf64_newehdr
440 Allocate an Executable Header in an ELF object.
441 .It Fn elf32_newphdr , Fn elf64_newphdr
442 Allocate or resize the Program Header Table in an ELF object.
444 .It "Data Translation"
446 .It Fn elf32_xlatetof , Fn elf64_xlatetof
447 Translate an ELF data structure from its native representation to its
449 .It Fn elf32_xlatetom , Fn elf64_xlatetom
450 Translate an ELF data structure from its file representation to a
451 native representation.
453 .It "Error Reporting"
456 Retrieve the current error.
458 Retrieve a human readable description of the current error.
465 archive or ELF object given a file descriptor.
467 Close an ELF descriptor and release all its resources.
471 archive or ELF object present in a memory arena.
473 Sets the operating version.
476 .Bl -tag -width ".Fn elf_setshstrndx" -compact
478 Manage the association between and ELF descriptor and its underlying file.
484 Mark the ELF Executable Header in an ELF descriptor as dirty.
486 Mark the ELF Program Header Table in an ELF descriptor as dirty.
492 Mark an ELF Section Header as dirty.
493 .It Fn elf_setshstrndx
494 Set the index of the section name string table for the ELF object.
496 Recompute ELF object layout and optionally write the modified object
497 back to the underlying file.
500 .Bl -tag -width ".Fn elf_getshstrndx" -compact
501 .It Fn elf32_checksum , Fn elf64_checkum
502 Compute checksum of an ELF object.
504 Retrieve the identification bytes for an ELF object.
506 Retrieve the number of sections in an ELF object.
507 .It Fn elf_getshstrndx
508 Retrieve the section index of the section name string table in
511 Compute the ELF hash value of a string.
513 Query the kind of object associated with an ELF descriptor.
514 .It Fn elf32_fsize , Fn elf64_fsize
515 Return the size of the file representation of an ELF type.
518 .Ss Controlling ELF Object Layout
519 In the usual mode of operation, library will compute section
520 offsets and alignments based on the contents of an ELF descriptor's
521 sections without need for further intervention by the
524 However, if the application wishes to take complete charge of the
525 layout of the ELF file, it may set the
527 flag on an ELF descriptor using
529 following which the library will use the data offsets and alignments
530 specified by the application when laying out the file.
531 Application control of file layout is described further in the
535 Gaps in between sections will be filled with the fill character
539 In case an error is encountered, these library functions set an
540 internal error number and signal the presence of the error by
541 returning an special return value.
542 The application can check the
543 current error number by calling
545 A human readable description of the recorded error is available by
548 .Ss Memory Management Rules
549 The library keeps track of all
553 descriptors associated with an ELF descriptor and recovers them
554 when the descriptor is closed using
556 Thus the application must not call
558 on data structures allocated by the ELF library.
560 Conversely the library will not
561 free data that it has not allocated.
562 As an example, an application may call
566 descriptor and can set the
568 member of the descriptor to point to a region of memory allocated
571 It is the applications responsibility to free this arena, though the
572 library will reclaim the space used by the
579 The original ELF(3) API was developed for Unix System V.
580 The current implementation of the ELF(3) API appeared in
583 The ELF library was written by
585 .Aq jkoshy@FreeBSD.org .