2 .\" Copyright 1989 AT&T All Rights Reserved Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH ELF 3ELF "Jul 23, 2001"
8 elf \- object file access library
12 cc [ \fIflag\fR ... ] \fIfile\fR ... \fB-lelf\fR [ \fIlibrary\fR ... ]
13 \fB#include <libelf.h>\fR
19 Functions in the \fBELF\fR access library let a program manipulate \fBELF\fR
20 (Executable and Linking Format) object files, archive files, and archive
21 members. The header provides type and function declarations for all library
25 Programs communicate with many of the higher-level routines using an \fIELF
26 descriptor\fR. That is, when the program starts working with a file,
27 \fBelf_begin\fR(3ELF) creates an \fBELF\fR descriptor through which the program
28 manipulates the structures and information in the file. These \fBELF\fR
29 descriptors can be used both to read and to write files. After the program
30 establishes an \fBELF\fR descriptor for a file, it may then obtain
31 \fIsection\fR \fIdescriptors\fR to manipulate the sections of the file (see
32 \fBelf_getscn\fR(3ELF)). Sections hold the bulk of an object file's real
33 information, such as text, data, the symbol table, and so on. A section
34 descriptor ``belongs'' to a particular \fBELF\fR descriptor, just as a section
35 belongs to a file. Finally, \fIdata\fR \fIdescriptors\fR are available through
36 section descriptors, allowing the program to manipulate the information
37 associated with a section. A data descriptor ``belongs'' to a section
41 Descriptors provide private handles to a file and its pieces. In other words, a
42 data descriptor is associated with one section descriptor, which is associated
43 with one \fBELF\fR descriptor, which is associated with one file. Although
44 descriptors are private, they give access to data that may be shared. Consider
45 programs that combine input files, using incoming data to create or update
46 another file. Such a program might get data descriptors for an input and an
47 output section. It then could update the output descriptor to reuse the input
48 descriptor's data. That is, the descriptors are distinct, but they could share
49 the associated data bytes. This sharing avoids the space overhead for duplicate
50 buffers and the performance overhead for copying data unnecessarily.
54 \fBELF\fR provides a framework in which to define a family of object files,
55 supporting multiple processors and architectures. An important distinction
56 among object files is the \fIclass\fR, or capacity, of the file. The 32-bit
57 class supports architectures in which a 32-bit object can represent addresses,
58 file sizes, and so on, as in the following:
68 \fBElf32_Addr\fR Unsigned address
70 \fBElf32_Half\fR Unsigned medium integer
72 \fBElf32_Off\fR Unsigned file offset
74 \fBElf32_Sword\fR Signed large integer
76 \fBElf32_Word\fR Unsigned large integer
78 \fBunsigned char\fR Unsigned small integer
83 The 64\(mibit class works the same as the 32\(mibit class, substituting 64 for
84 32 as necessary. Other classes will be defined as necessary, to support larger
85 (or smaller) machines. Some library services deal only with data objects for a
86 specific class, while others are class-independent. To make this distinction
87 clear, library function names reflect their status, as described below.
88 .SS "Data Representation"
91 Conceptually, two parallel sets of objects support cross compilation
92 environments. One set corresponds to file contents, while the other set
93 corresponds to the native memory image of the program manipulating the file.
94 Type definitions supplied by the headers work on the native machine, which may
95 have different data encodings (size, byte order, and so on) than the target
96 machine. Although native memory objects should be at least as big as the file
97 objects (to avoid information loss), they may be bigger if that is more natural
101 Translation facilities exist to convert between file and memory
102 representations. Some library routines convert data automatically, while others
103 leave conversion as the program's responsibility. Either way, programs that
104 create object files must write file-typed objects to those files; programs that
105 read object files must take a similar view. See \fBelf32_xlatetof\fR(3ELF) and
106 \fBelf32_fsize\fR(3ELF) for more information.
109 Programs may translate data explicitly, taking full control over the object
110 file layout and semantics. If the program prefers not to have and exercise
111 complete control, the library provides a higher-level interface that hides many
112 object file details. \fBelf_begin()\fR and related functions let a program deal
113 with the native memory types, converting between memory objects and their file
114 equivalents automatically when reading or writing an object file.
118 Object file versions allow \fBELF\fR to adapt to new requirements. \fIThree\fR
119 \fIindependent\fR \fIversions\fR can be important to a program. First, an
120 application program knows about a particular version by virtue of being
121 compiled with certain headers. Second, the access library similarly is compiled
122 with header files that control what versions it understands. Third, an
123 \fBELF\fR object file holds a value identifying its version, determined by the
124 \fBELF\fR version known by the file's creator. Ideally, all three versions
125 would be the same, but they may differ.
128 If a program's version is newer than the access library, the program might use
129 information unknown to the library. Translation routines might not work
130 properly, leading to undefined behavior. This condition merits installing a new
134 The library's version might be newer than the program's and the file's. The
135 library understands old versions, thus avoiding compatibility problems in this
139 Finally, a file's version might be newer than either the program or the library
140 understands. The program might or might not be able to process the file
141 properly, depending on whether the file has extra information and whether that
142 information can be safely ignored. Again, the safe alternative is to install a
143 new library that understands the file's version.
146 To accommodate these differences, a program must use \fBelf_version\fR(3ELF) to
147 pass its version to the library, thus establishing the \fIworking version\fR
148 for the process. Using this, the library accepts data from and presents data to
149 the program in the proper representations. When the library reads object files,
150 it uses each file's version to interpret the data. When writing files or
151 converting memory types to the file equivalents, the library uses the program's
152 working version for the file data.
153 .SS "System Services"
156 As mentioned above, \fBelf_begin()\fR and related routines provide a
157 higher-level interface to \fBELF\fR files, performing input and output on
158 behalf of the application program. These routines assume a program can hold
159 entire files in memory, without explicitly using temporary files. When reading
160 a file, the library routines bring the data into memory and perform subsequent
161 operations on the memory copy. Programs that wish to read or write large object
162 files with this model must execute on a machine with a large process virtual
163 address space. If the underlying operating system limits the number of open
164 files, a program can use \fBelf_cntl\fR(3ELF) to retrieve all necessary data
165 from the file, allowing the program to close the file descriptor and reuse it.
168 Although the \fBelf_begin()\fR interfaces are convenient and efficient for many
169 programs, they might be inappropriate for some. In those cases, an application
170 may invoke the \fBelf32_xlatetom\fR(3ELF) or \fBelf32_xlatetof\fR(3ELF) data
171 translation routines directly. These routines perform no input or output,
172 leaving that as the application's responsibility. By assuming a larger share of
173 the job, an application controls its input and output model.
177 Names associated with the library take several forms.
181 \fB\fBelf_\fR\fIname\fR\fR
184 These class-independent names perform some service, \fIname\fR, for the
191 \fB\fBelf32_\fR\fIname\fR\fR
194 Service names with an embedded class, \fB32\fR here, indicate they work only
195 for the designated class of files.
201 \fB\fBElf_\fR\fIType\fR\fR
204 Data types can be class-independent as well, distinguished by \fIType\fR.
210 \fB\fBElf32_\fR\fIType\fR\fR
213 Class-dependent data types have an embedded class name, \fB32\fR here.
219 \fB\fBELF_C_\fR\fICMD\fR\fR
222 Several functions take commands that control their actions. These values are
223 members of the \fBElf_Cmd\fR enumeration; they range from zero through
224 \fBELF_C_NUM\fR\(mi1\fB\&.\fR
230 \fB\fBELF_F_\fR\fIFLAG\fR\fR
233 Several functions take flags that control library status and/or actions. Flags
234 are bits that may be combined.
240 \fB\fBELF32_FSZ_\fR\fITYPE\fR\fR
243 These constants give the file sizes in bytes of the basic \fBELF\fR types for
244 the 32-bit class of files. See \fBelf32_fsize()\fR for more information.
250 \fB\fBELF_K_\fR\fIKIND\fR\fR
253 The function \fBelf_kind()\fR identifies the \fIKIND\fR of file associated with
254 an \fBELF\fR descriptor. These values are members of the \fBElf_Kind\fR
255 enumeration; they range from zero through \fBELF_K_NUM\fR\(mi1\fB\&.\fR
261 \fB\fBELF_T_\fR\fITYPE\fR\fR
264 When a service function, such as \fBelf32_xlatetom()\fR or
265 \fBelf32_xlatetof()\fR, deals with multiple types, names of this form specify
266 the desired \fITYPE\fR. Thus, for example, \fBELF_T_EHDR\fR is directly related
267 to \fBElf32_Ehdr\fR. These values are members of the \fBElf_Type\fR
268 enumeration; they range from zero through \fBELF_T_NUM\fR\(mi1\fB\&.\fR
273 \fBExample 1 \fRAn interpretation of elf file.
276 The basic interpretation of an ELF file consists of:
282 opening an ELF object file
288 obtaining an ELF descriptor
294 analyzing the file using the descriptor.
298 The following example opens the file, obtains the ELF descriptor, and prints
299 out the names of each section in the file.
309 static void failure(void);
311 main(int argc, char ** argv)
321 /* Open the input file */
322 if ((fd = open(argv[1], O_RDONLY)) == -1)
325 /* Obtain the ELF descriptor */
326 (void) elf_version(EV_CURRENT);
327 if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL)
330 /* Obtain the .shstrtab data buffer */
331 if (((ehdr = elf32_getehdr(elf)) == NULL) ||
332 ((scn = elf_getscn(elf, ehdr->e_shstrndx)) == NULL) ||
333 ((data = elf_getdata(scn, NULL)) == NULL))
336 /* Traverse input filename, printing each section */
337 for (cnt = 1, scn = NULL; scn = elf_nextscn(elf, scn); cnt++) {
338 if ((shdr = elf32_getshdr(scn)) == NULL)
340 (void) printf("[%d] %s\en", cnt,
341 (char *)data->d_buf + shdr->sh_name);
348 (void) fprintf(stderr, "%s\en", elf_errmsg(elf_errno()));
357 See \fBattributes\fR(5) for descriptions of the following attributes:
365 ATTRIBUTE TYPE ATTRIBUTE VALUE
367 Interface Stability Stable
375 \fBar.h\fR(3HEAD), \fBelf32_checksum\fR(3ELF), \fBelf32_fsize\fR(3ELF),
376 \fBelf32_getshdr\fR(3ELF), \fBelf32_xlatetof\fR(3ELF), \fBelf_begin\fR(3ELF),
377 \fBelf_cntl\fR(3ELF), \fBelf_errmsg\fR(3ELF), \fBelf_fill\fR(3ELF),
378 \fBelf_getarhdr\fR(3ELF), \fBelf_getarsym\fR(3ELF), \fBelf_getbase\fR(3ELF),
379 \fBelf_getdata\fR(3ELF), \fBelf_getident\fR(3ELF), \fBelf_getscn\fR(3ELF),
380 \fBelf_hash\fR(3ELF), \fBelf_kind\fR(3ELF), \fBelf_memory\fR(3ELF),
381 \fBelf_rawfile\fR(3ELF), \fBelf_strptr\fR(3ELF), \fBelf_update\fR(3ELF),
382 \fBelf_version\fR(3ELF), \fBgelf\fR(3ELF), \fBlibelf\fR(3LIB),
383 \fBattributes\fR(5), \fBlfcompile\fR(5)
386 \fIANSI C Programmer's Guide\fR
394 Information in the \fBELF\fR headers is separated into common parts and
395 processor-specific parts. A program can make a processor's information
396 available by including the appropriate header:
397 \fB<sys/elf_\fR\fINAME\fR\fB\&.h>\fR where \fINAME\fR matches the processor
398 name as used in the \fBELF\fR file header.
408 \fBM32\fR AT&T WE 32100
412 \fB386\fR Intel 80386, 80486, Pentium
417 Other processors will be added to the table as necessary.
420 To illustrate, a program could use the following code to ``see'' the
421 processor-specific information for the SPARC based system.
425 \fB#include <libelf.h>
426 #include <sys/elf_SPARC.h>\fR
432 Without the \fB<sys/elf_SPARC.h>\fR definition, only the common \fBELF\fR
433 information would be visible.
436 A program could use the following code to ``see'' the processor-specific
437 information for the Intel 80386:
441 \fB#include <libelf.h>
442 #include <sys/elf_386.h>\fR
448 Without the \fB<sys/elf_386.h>\fR definition, only the common \fBELF\fR
449 information would be visible.
452 Although reading the objects is rather straightforward, writing/updating them
453 can corrupt the shared offsets among sections. Upon creation, relationships are
454 established among the sections that must be maintained even if the object's