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