1 .\" Copyright (c) 2006-2008,2011,2019 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
24 .\" $Id: elf.3 3743 2019-06-12 19:36:30Z jkoshy $
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 )
119 to and from these representations.
120 It also provides higher-level APIs
121 .Xr ( gelf_xlatetof 3 ,
122 .Xr gelf_xlatetom 3 )
123 that retrieve and store data from the ELF object in a class-agnostic
125 .Ss Library Working Version
126 Conceptually, there are three version numbers associated with an
127 application using the ELF library to manipulate ELF objects:
128 .Bl -bullet -compact -offset indent
130 The ELF version that the application was compiled against.
131 This version determines the ABI expected by the application.
133 The ELF version of the ELF object being manipulated by the
134 application through the ELF library.
136 The ELF version (or set of versions) supported by the ELF library itself.
139 In order to facilitate working with ELF objects of differing versions,
140 the ELF library requires the application to call the
142 function before invoking many of its operations, in order to inform
143 the library of the application's desired working version.
145 In the current implementation, all three versions have to be
148 The ELF library uses the following prefixes:
149 .Bl -tag -width "ELF_F_*"
151 Used for class-independent functions.
153 Used for functions working with 32 bit ELF objects.
155 Used for functions working with 64 bit ELF objects.
157 Used for class-independent data types.
159 Used for command values used in a few functions.
160 These symbols are defined as members of the
164 Used for error numbers.
168 These constants define the kind of file associated with an ELF
172 The symbols are defined by the
176 These values are defined by the
178 enumeration, and denote the types of ELF data structures
179 that can be present in an ELF object.
182 In addition, the library uses symbols with prefixes
186 for its internal use.
188 Applications communicate with the library using descriptors.
190 .Bl -tag -width ".Vt Elf_Data"
194 descriptor represents an ELF object or an
197 It is allocated using one of the
204 descriptor can be used to read and write data to an ELF file.
207 descriptor can be associated with zero or more
211 Given an ELF descriptor, the application may retrieve the ELF
212 object's class-dependent
213 .Dq "Executable Header"
219 A new Ehdr structure may be allocated using the
226 .Dq "Program Header Table"
227 associated with an ELF descriptor may be allocated using the
232 A new program header table may be allocated or an existing table
241 structure is opaque and has no members visible to the
246 data structure describes an individual chunk of a ELF file as
247 represented in memory.
248 It has the following application-visible members:
249 .Bl -tag -width ".Vt unsigned int d_version" -compact
250 .It Vt "uint64_t d_align"
251 The in-file alignment of the data buffer within its containing ELF section.
252 This value must be non-zero and a power of two.
254 A pointer to data in memory.
255 .It Vt "uint64_t d_off"
256 The offset within the containing section where this descriptor's data
258 This field will be computed by the library unless the application
259 requests full control of the ELF object's layout.
260 .It Vt "uint64_t d_size"
261 The number of bytes of data in this descriptor.
262 .It Vt "Elf_Type d_type"
263 The ELF type (see below) of the data in this descriptor.
264 .It Vt "unsigned int d_version"
265 The operating version for the data in this buffer.
269 descriptors are usually used in conjunction with
274 descriptors represent sections in an ELF object.
275 These descriptors are opaque and contain no application modifiable
280 descriptor for a specific section in an ELF object can be
284 The sections contained in an ELF object can be traversed using the
287 New sections are allocated using the
293 descriptors associated with a given section can be retrieved
297 New data descriptors can be added to a section
303 representation of data in a section can be retrieved using the
307 .Ss Supported Elf Types
308 The following ELF datatypes are supported by the library.
310 .Bl -tag -width ".Dv ELF_T_SYMINFO" -compact
315 The library will not attempt to translate byte data.
317 Software and hardware capability records.
319 Records used in a section of type
322 ELF executable header.
324 GNU-style hash tables.
326 16-bit unsigned words.
328 64 bit unsigned words.
331 .\".It Dv ELF_T_MOVEP
332 .\" As yet unsupported.
338 ELF program header table entries.
340 ELF relocation entries.
342 ELF relocation entries with addends.
344 ELF section header entries.
350 ELF symbol information.
352 ELF symbol table entries.
354 Symbol version definition records.
356 Symbol version requirement records.
358 Unsigned 32-bit words.
360 Unsigned 64-bit words.
365 denotes the number of Elf types known to the library.
367 The following table shows the mapping between ELF section types
370 and the types supported by the library.
371 .Bl -column ".Dv SHT_PREINIT_ARRAY" ".Dv ELF_T_SYMINFO"
372 .It Em Section Type Ta Em "Library Type" Ta Em Description
373 .It Dv SHT_DYNAMIC Ta Dv ELF_T_DYN Ta Xo
377 .It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking.
378 .It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers.
379 .It Dv SHT_GNU_HASH Ta Dv ELF_T_GNUHASH Ta GNU hash sections.
380 .It Dv SHT_GNU_LIBLIST Ta Dv ELF_T_WORD Ta List of libraries to be pre-linked.
381 .It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions.
382 .It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements.
383 .It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols.
384 .It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker.
385 .It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes.
386 .It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers.
387 .It Dv SHT_NOBITS Ta Dv ELF_T_BYTE Ta Xo
392 .It Dv SHT_NOTE Ta Dv ELF_T_NOTE Ta ELF note records.
393 .It Dv SHT_PREINIT_ARRAY Ta Dv ELF_T_ADDR Ta Pre-initialization function pointers.
394 .It Dv SHT_PROGBITS Ta Dv ELF_T_BYTE Ta Machine code.
395 .It Dv SHT_REL Ta Dv ELF_T_REL Ta ELF relocation records.
396 .It Dv SHT_RELA Ta Dv ELF_T_RELA Ta Relocation records with addends.
397 .It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables.
398 .It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables.
399 .It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering.
400 .It Dv SHT_SUNW_dof Ta Dv ELF_T_BYTE Ta Xo
404 .It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records.
405 .It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags.
406 .It Dv SHT_SUNW_verdef Ta Dv ELF_T_VDEF Ta Xo
410 .It Dv SHT_SUNW_verneed Ta Dv ELF_T_VNEED Ta Xo
412 .Dv SHT_GNU_verneed .
414 .It Dv SHT_SUNW_versym Ta Dv ELF_T_HALF Ta Xo
420 Section types in the range
423 are otherwise considered to be of type
425 .Ss Functional Grouping
426 This section contains a brief overview of the available functionality
428 Each function listed here is described further in its own manual page.
429 .Bl -tag -width indent
431 .Bl -tag -compact -width indent
433 Retrieve the archive symbol table.
435 Retrieve the archive header for an object.
437 Retrieve the offset of a member inside an archive.
443 Random access inside an
447 .It "Data Structures"
448 .Bl -tag -compact -width indent
450 Retrieve translated data for an ELF section.
452 Retrieve the section descriptor for a named section.
454 Retrieve the index for a section.
458 descriptor to an ELF section.
460 Add a new section descriptor to an ELF descriptor.
462 Iterate through the sections in an ELF object.
464 Retrieve untranslated data for an ELF section.
466 Return a pointer to the untranslated file contents for an ELF object.
467 .It Fn elf32_getehdr , Fn elf64_getehdr
468 Retrieve the Executable Header in an ELF object.
469 .It Fn elf32_getphdr , Fn elf64_getphdr
470 Retrieve the Program Header Table in an ELF object.
471 .It Fn elf32_getshdr , Fn elf64_getshdr
472 Retrieve the ELF section header associated with an
475 .It Fn elf32_newehdr , Fn elf64_newehdr
476 Allocate an Executable Header in an ELF object.
477 .It Fn elf32_newphdr , Fn elf64_newphdr
478 Allocate or resize the Program Header Table in an ELF object.
480 .It "Data Translation"
481 .Bl -tag -compact -width indent
482 .It Fn elf32_xlatetof , Fn elf64_xlatetof
483 Translate an ELF data structure from its native representation to its
485 .It Fn elf32_xlatetom , Fn elf64_xlatetom
486 Translate an ELF data structure from its file representation to a
487 native representation.
489 .It "Error Reporting"
490 .Bl -tag -compact -width indent
492 Retrieve the current error.
494 Retrieve a human readable description of the current error.
497 .Bl -tag -compact -width indent
501 archive or ELF object given a file descriptor.
503 Close an ELF descriptor and release all its resources.
507 archive or ELF object present in a memory arena.
509 Sets the operating version.
512 .Bl -tag -width ".Fn elf_setshstrndx" -compact
514 Manage the association between and ELF descriptor and its underlying file.
520 Mark the ELF Executable Header in an ELF descriptor as dirty.
522 Mark the ELF Program Header Table in an ELF descriptor as dirty.
528 Mark an ELF Section Header as dirty.
529 .It Fn elf_setshstrndx
530 Set the index of the section name string table for the ELF object.
532 Recompute ELF object layout and optionally write the modified object
533 back to the underlying file.
536 .Bl -tag -width ".Fn elf_getshstrndx" -compact
537 .It Fn elf32_checksum , Fn elf64_checkum
538 Compute checksum of an ELF object.
540 Retrieve the identification bytes for an ELF object.
541 .It Fn elf_getphdrnum
542 Retrieve the number of program headers in an ELF object.
543 .It Fn elf_getshdrnum
544 Retrieve the number of sections in an ELF object.
545 .It Fn elf_getshdrstrndx
546 Retrieve the section index of the section name string table in
549 Compute the ELF hash value of a string.
551 Query the kind of object associated with an ELF descriptor.
552 .It Fn elf32_fsize , Fn elf64_fsize
553 Return the size of the file representation of an ELF type.
556 .Ss Controlling ELF Object Layout
557 In the usual mode of operation, library will compute section
558 offsets and alignments based on the contents of an ELF descriptor's
559 sections without need for further intervention by the
562 However, if the application wishes to take complete charge of the
563 layout of the ELF file, it may set the
565 flag on an ELF descriptor using
567 following which the library will use the data offsets and alignments
568 specified by the application when laying out the file.
569 Application control of file layout is described further in the
573 Gaps in between sections will be filled with the fill character
577 In case an error is encountered, these library functions set an
578 internal error number and signal the presence of the error by
579 returning an special return value.
580 The application can check the
581 current error number by calling
583 A human readable description of the recorded error is available by
586 .Ss Memory Management Rules
587 The library keeps track of all
591 descriptors associated with an ELF descriptor and recovers them
592 when the descriptor is closed using
594 Thus the application must not call
596 on data structures allocated by the ELF library.
598 Conversely the library will not
599 free data that it has not allocated.
600 As an example, an application may call
604 descriptor and can set the
606 member of the descriptor to point to a region of memory allocated
609 It is the applications responsibility to free this arena, though the
610 library will reclaim the space used by the
620 API was developed for
622 The current implementation of the API appeared in
625 The ELF library was written by
626 .An Joseph Koshy Aq Mt jkoshy@FreeBSD.org .