]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/elftoolchain/libelf/elf.3
Fix kernel memory disclosure with nested jails.
[FreeBSD/FreeBSD.git] / contrib / elftoolchain / libelf / elf.3
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
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 .Xr ( elf32_xlatetof 3 ,
115 .Xr elf32_xlatetom 3 ,
116 .Xr elf64_xlatetof 3
117 and
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
124 manner.
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
129 .It
130 The ELF version that the application was compiled against.
131 This version determines the ABI expected by the application.
132 .It
133 The ELF version of the ELF object being manipulated by the
134 application through the ELF library.
135 .It
136 The ELF version (or set of versions) supported by the ELF library itself.
137 .El
138 .Pp
139 In order to facilitate working with ELF objects of differing versions,
140 the ELF library requires the application to call the
141 .Fn elf_version
142 function before invoking many of its operations, in order to inform
143 the library of the application's desired working version.
144 .Pp
145 In the current implementation, all three versions have to be
146 .Dv EV_CURRENT .
147 .Ss Namespace use
148 The ELF library uses the following prefixes:
149 .Bl -tag -width "ELF_F_*"
150 .It Dv elf_
151 Used for class-independent functions.
152 .It Dv elf32_
153 Used for functions working with 32 bit ELF objects.
154 .It Dv elf64_
155 Used for functions working with 64 bit ELF objects.
156 .It Dv Elf_
157 Used for class-independent data types.
158 .It Dv ELF_C_
159 Used for command values used in a few functions.
160 These symbols are defined as members of the
161 .Vt Elf_Cmd
162 enumeration.
163 .It Dv ELF_E_
164 Used for error numbers.
165 .It Dv ELF_F_
166 Used for flags.
167 .It Dv ELF_K_
168 These constants define the kind of file associated with an ELF
169 descriptor.
170 See
171 .Xr elf_kind 3 .
172 The symbols are defined by the
173 .Vt Elf_Kind
174 enumeration.
175 .It Dv ELF_T_
176 These values are defined by the
177 .Vt Elf_Type
178 enumeration, and denote the types of ELF data structures
179 that can be present in an ELF object.
180 .El
181 .Pp
182 In addition, the library uses symbols with prefixes
183 .Dv _ELF
184 and
185 .Dv _libelf
186 for its internal use.
187 .Ss Descriptors
188 Applications communicate with the library using descriptors.
189 These are:
190 .Bl -tag -width ".Vt Elf_Data"
191 .It Vt Elf
192 An
193 .Vt Elf
194 descriptor represents an ELF object or an
195 .Xr ar 1
196 archive.
197 It is allocated using one of the
198 .Fn elf_begin
199 or
200 .Fn elf_memory
201 functions.
202 An
203 .Vt Elf
204 descriptor can be used to read and write data to an ELF file.
205 An
206 .Vt Elf
207 descriptor can be associated with zero or more
208 .Vt Elf_Scn
209 section descriptors.
210 .Pp
211 Given an ELF descriptor, the application may retrieve the ELF
212 object's class-dependent
213 .Dq "Executable Header"
214 structures using the
215 .Fn elf32_getehdr
216 or
217 .Fn elf64_getehdr
218 functions.
219 A new Ehdr structure may be allocated using the
220 .Fn elf64_newehdr
221 or
222 .Fn elf64_newehdr
223 functions.
224 .Pp
225 The
226 .Dq "Program Header Table"
227 associated with an ELF descriptor may be allocated using the
228 .Fn elf32_getphdr
229 or
230 .Fn elf64_getphdr
231 functions.
232 A new program header table may be allocated or an existing table
233 resized using the
234 .Fn elf32_newphdr
235 or
236 .Fn elf64_newphdr
237 functions.
238 .Pp
239 The
240 .Vt Elf
241 structure is opaque and has no members visible to the
242 application.
243 .It Vt Elf_Data
244 An
245 .Vt Elf_Data
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.
253 .It Vt "void *d_buf"
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
257 would be placed.
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.
266 .El
267 .Pp
268 .Vt Elf_Data
269 descriptors are usually associated with
270 .Vt Elf_Scn
271 descriptors.
272 Existing data descriptors associated with an ELF section may be
273 structures are retrieved using the
274 .Fn elf_getdata
275 and
276 .Fn elf_rawdata
277 functions.
278 The
279 .Fn elf_newdata
280 function may be used to attach new data descriptors to an ELF section.
281 .It Vt Elf_Scn
282 .Vt Elf_Scn
283 descriptors represent a section in an ELF object.
284 .Pp
285 They are retrieved using the
286 .Fn elf_getscn
287 function.
288 An application may iterate through the existing sections of an ELF
289 object using the
290 .Fn elf_nextscn
291 function.
292 New sections may be allocated using the
293 .Fn elf_newscn
294 function.
295 .Pp
296 The
297 .Vt Elf_Scn
298 descriptor is opaque and contains no application modifiable fields.
299 .El
300 .Ss Supported Elf Types
301 The 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
305 Machine addresses.
306 .It Dv ELF_T_BYTE
307 Byte data.
308 The library will not attempt to translate byte data.
309 .It Dv ELF_T_CAP
310 Software and hardware capability records.
311 .It Dv ELF_T_DYN
312 Records used in a section of type
313 .Dv SHT_DYNAMIC .
314 .It Dv ELF_T_EHDR
315 ELF executable header.
316 .It Dv ELF_T_GNUHASH
317 GNU-style hash tables.
318 .It Dv ELF_T_HALF
319 16-bit unsigned words.
320 .It Dv ELF_T_LWORD
321 64 bit unsigned words.
322 .It Dv ELF_T_MOVE
323 ELF Move records.
324 .\".It Dv ELF_T_MOVEP
325 .\" As yet unsupported.
326 .It Dv ELF_T_NOTE
327 ELF Note structures.
328 .It Dv ELF_T_OFF
329 File offsets.
330 .It Dv ELF_T_PHDR
331 ELF program header table entries.
332 .It Dv ELF_T_REL
333 ELF relocation entries.
334 .It Dv ELF_T_RELA
335 ELF relocation entries with addends.
336 .It Dv ELF_T_SHDR
337 ELF section header entries.
338 .It Dv ELF_T_SWORD
339 Signed 32-bit words.
340 .It Dv ELF_T_SXWORD
341 Signed 64-bit words.
342 .It Dv ELF_T_SYMINFO
343 ELF symbol information.
344 .It Dv ELF_T_SYM
345 ELF symbol table entries.
346 .It Dv ELF_T_VDEF
347 Symbol version definition records.
348 .It Dv ELF_T_VNEED
349 Symbol version requirement records.
350 .It Dv ELF_T_WORD
351 Unsigned 32-bit words.
352 .It Dv ELF_T_XWORD
353 Unsigned 64-bit words.
354 .El
355 .Pp
356 The symbol
357 .Dv ELF_T_NUM
358 denotes the number of Elf types known to the library.
359 .Pp
360 The following table shows the mapping between ELF section types
361 defined in
362 .Xr elf 5
363 and 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
368 section 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
381 Empty sections.
382 See
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
394 Used 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
400 Same as
401 .Dv SHT_GNU_verdef .
402 .Xc
403 .It Dv SHT_SUNW_verneed Ta Dv ELF_T_VNEED Ta Xo
404 Same as
405 .Dv SHT_GNU_verneed .
406 .Xc
407 .It Dv SHT_SUNW_versym Ta Dv ELF_T_HALF Ta Xo
408 Same as
409 .Dv SHT_GNU_versym .
410 .Xc
411 .El
412 .Pp
413 Section types in the range
414 .Dv [ SHT_LOOS ,
415 .Dv SHT_HIUSER ]
416 are otherwise considered to be of type
417 .Dv ELF_T_BYTE .
418 .Ss Functional Grouping
419 This section contains a brief overview of the available functionality
420 in the ELF library.
421 Each 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
426 Retrieve the archive symbol table.
427 .It Fn elf_getarhdr
428 Retrieve the archive header for an object.
429 .It Fn elf_getbase
430 Retrieve the offset of a member inside an archive.
431 .It Fn elf_next
432 Iterate through an
433 .Xr ar 1
434 archive.
435 .It Fn elf_rand
436 Random access inside an
437 .Xr ar 1
438 archive.
439 .El
440 .It "Data Structures"
441 .Bl -tag -compact -width indent
442 .It Fn elf_getdata
443 Retrieve translated data for an ELF section.
444 .It Fn elf_getscn
445 Retrieve the section descriptor for a named section.
446 .It Fn elf_ndxscn
447 Retrieve the index for a section.
448 .It Fn elf_newdata
449 Add a new
450 .Vt Elf_Data
451 descriptor to an ELF section.
452 .It Fn elf_newscn
453 Add a new section descriptor to an ELF descriptor.
454 .It Fn elf_nextscn
455 Iterate through the sections in an ELF object.
456 .It Fn elf_rawdata
457 Retrieve untranslated data for an ELF section.
458 .It Fn elf_rawfile
459 Return a pointer to the untranslated file contents for an ELF object.
460 .It Fn elf32_getehdr , Fn elf64_getehdr
461 Retrieve the Executable Header in an ELF object.
462 .It Fn elf32_getphdr , Fn elf64_getphdr
463 Retrieve the Program Header Table in an ELF object.
464 .It Fn elf32_getshdr , Fn elf64_getshdr
465 Retrieve the ELF section header associated with an
466 .Vt Elf_Scn
467 descriptor.
468 .It Fn elf32_newehdr , Fn elf64_newehdr
469 Allocate an Executable Header in an ELF object.
470 .It Fn elf32_newphdr , Fn elf64_newphdr
471 Allocate 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
476 Translate an ELF data structure from its native representation to its
477 file representation.
478 .It Fn elf32_xlatetom , Fn elf64_xlatetom
479 Translate an ELF data structure from its file representation to a
480 native representation.
481 .El
482 .It "Error Reporting"
483 .Bl -tag -compact -width indent
484 .It Fn elf_errno
485 Retrieve the current error.
486 .It Fn elf_errmsg
487 Retrieve a human readable description of the current error.
488 .El
489 .It "Initialization"
490 .Bl -tag -compact -width indent
491 .It Fn elf_begin
492 Opens an
493 .Xr ar 1
494 archive or ELF object given a file descriptor.
495 .It Fn elf_end
496 Close an ELF descriptor and release all its resources.
497 .It Fn elf_memory
498 Opens an
499 .Xr ar 1
500 archive or ELF object present in a memory arena.
501 .It Fn elf_version
502 Sets the operating version.
503 .El
504 .It "IO Control"
505 .Bl -tag -width ".Fn elf_setshstrndx" -compact
506 .It Fn elf_cntl
507 Manage the association between and ELF descriptor and its underlying file.
508 .It Fn elf_flagdata
509 Mark an
510 .Vt Elf_Data
511 descriptor as dirty.
512 .It Fn elf_flagehdr
513 Mark the ELF Executable Header in an ELF descriptor as dirty.
514 .It Fn elf_flagphdr
515 Mark the ELF Program Header Table in an ELF descriptor as dirty.
516 .It Fn elf_flagscn
517 Mark an
518 .Vt Elf_Scn
519 descriptor as dirty.
520 .It Fn elf_flagshdr
521 Mark an ELF Section Header as dirty.
522 .It Fn elf_setshstrndx
523 Set the index of the section name string table for the ELF object.
524 .It Fn elf_update
525 Recompute ELF object layout and optionally write the modified object
526 back 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
531 Compute checksum of an ELF object.
532 .It Fn elf_getident
533 Retrieve the identification bytes for an ELF object.
534 .It Fn elf_getphdrnum
535 Retrieve the number of program headers in an ELF object.
536 .It Fn elf_getshdrnum
537 Retrieve the number of sections in an ELF object.
538 .It Fn elf_getshdrstrndx
539 Retrieve the section index of the section name string table in
540 an ELF object.
541 .It Fn elf_hash
542 Compute the ELF hash value of a string.
543 .It Fn elf_kind
544 Query the kind of object associated with an ELF descriptor.
545 .It Fn elf32_fsize , Fn elf64_fsize
546 Return the size of the file representation of an ELF type.
547 .El
548 .El
549 .Ss Controlling ELF Object Layout
550 In the usual mode of operation, library will compute section
551 offsets and alignments based on the contents of an ELF descriptor's
552 sections without need for further intervention by the
553 application.
554 .Pp
555 However, if the application wishes to take complete charge of the
556 layout of the ELF file, it may set the
557 .Dv ELF_F_LAYOUT
558 flag on an ELF descriptor using
559 .Xr elf_flagelf 3 ,
560 following which the library will use the data offsets and alignments
561 specified by the application when laying out the file.
562 Application control of file layout is described further in the
563 .Xr elf_update 3
564 manual page.
565 .Pp
566 Gaps in between sections will be filled with the fill character
567 set by function
568 .Fn elf_fill .
569 .Ss Error Handling
570 In case an error is encountered, these library functions set an
571 internal error number and signal the presence of the error by
572 returning an special return value.
573 The application can check the
574 current error number by calling
575 .Xr elf_errno 3 .
576 A human readable description of the recorded error is available by
577 calling
578 .Xr elf_errmsg 3 .
579 .Ss Memory Management Rules
580 The library keeps track of all
581 .Vt Elf_Scn
582 and
583 .Vt Elf_Data
584 descriptors associated with an ELF descriptor and recovers them
585 when the descriptor is closed using
586 .Xr elf_end 3 .
587 Thus the application must not call
588 .Xr free 3
589 on data structures allocated by the ELF library.
590 .Pp
591 Conversely the library will not
592 free data that it has not allocated.
593 As an example, an application may call
594 .Xr elf_newdata 3
595 to allocate a new
596 .Vt Elf_Data
597 descriptor and can set the
598 .Va d_off
599 member of the descriptor to point to a region of memory allocated
600 using
601 .Xr malloc 3 .
602 It is the applications responsibility to free this arena, though the
603 library will reclaim the space used by the
604 .Vt Elf_Data
605 descriptor itself.
606 .Sh SEE ALSO
607 .Xr gelf 3 ,
608 .Xr ar 5 ,
609 .Xr elf 5
610 .Sh HISTORY
611 The original ELF(3) API was developed for Unix System V.
612 The current implementation of the ELF(3) API appeared in
613 .Fx 7.0 .
614 .Sh AUTHORS
615 The ELF library was written by
616 .An Joseph Koshy Aq Mt jkoshy@FreeBSD.org .