cc [ flag ... ] file ... -lelf [ library ... ]
#include <libelf.h>
Unsafe
Functions in the ELF access library let a program manipulate ELF (Executable and Linking Format) object files, archive files, and archive members. The header provides type and function declarations for all library services.
Programs communicate with many of the higher-level routines using an ELF descriptor. That is, when the program starts working with a file, elf_begin(3E) creates an ELF descriptor through which the program manipulates the structures and information in the file. These ELF descriptors can be used both to read and to write files. After the program establishes an ELF descriptor for a file, it may then obtain section descriptors to manipulate the sections of the file (see elf_getscn(3E) ). Sections hold the bulk of an object file’s real information, such as text, data, the symbol table, and so on. A section descriptor ‘‘belongs’’ to a particular ELF descriptor, just as a section belongs to a file. Finally, data descriptors are available through section descriptors, allowing the program to manipulate the information associated with a section. A data descriptor ‘‘belongs’’ to a section descriptor.
Descriptors provide private handles to a file and its pieces. In other words, a data descriptor is associated with one section descriptor, which is associated with one ELF descriptor, which is associated with one file. Although descriptors are private, they give access to data that may be shared. Consider programs that combine input files, using incoming data to create or update another file. Such a program might get data descriptors for an input and an output section. It then could update the output descriptor to reuse the input descriptor’s data. That is, the descriptors are distinct, but they could share the associated data bytes. This sharing avoids the space overhead for duplicate buffers and the performance overhead for copying data unnecessarily.
Name Purpose Elf32_Addr Unsigned address Elf32_Half Unsigned medium integer Elf32_Off Unsigned file offset Elf32_Sword Signed large integer Elf32_Word Unsigned large integer unsigned char Unsigned small integer
Other classes will be defined as necessary, to support larger (or smaller) machines. Some library services deal only with data objects for a specific class, while others are class-independent. To make this distinction clear, library function names reflect their status, as described below.
Translation facilities exist to convert between file and memory representations. Some library routines convert data automatically, while others leave conversion as the program’s responsibility. Either way, programs that create object files must write file-typed objects to those files; programs that read object files must take a similar view. See elf32_xlatetof(3E) and elf32_fsize(3E) for more information.
Programs may translate data explicitly, taking full control over the object file layout and semantics. If the program prefers not to have and exercise complete control, the library provides a higher-level interface that hides many object file details. elf_begin() and related functions let a program deal with the native memory types, converting between memory objects and their file equivalents automatically when reading or writing an object file.
To accommodate these differences, a program must use elf_version(3E) to pass its version to the library, thus establishing the working version for the process. Using this, the library accepts data from and presents data to the program in the proper representations. When the library reads object files, it uses each file’s version to interpret the data. When writing files or converting memory types to the file equivalents, the library uses the program’s working version for the file data.
Although the elf_begin() interfaces are convenient and efficient for many programs, they might be inappropriate for some. In those cases, an application may invoke the elf32_xlatetom(3E) or elf32_xlatetof(3E) data translation routines directly. These routines perform no input or output, leaving that as the application’s responsibility. By assuming a larger share of the job, an application controls its input and output model.
Names associated with the library take several forms.
· opening an ELF object fileThe following example opens the file, obtains the ELF descriptor, and prints out the names of each section in the file.· obtaining an ELF descriptor
· analyzing the file using the descriptor.
#include <fcntl.h> #include <stdio.h> #include <libelf.h> #include <stdlib.h> #include <string.h> static void failure(void); void main(int argc, char ** argv) { Elf32_Shdr * shdr; Elf32_Ehdr * ehdr; Elf * elf; Elf_Scn * scn; Elf_Data * data; int fd; unsigned int cnt; /* Open the input file */ if ((fd = open(argv[1], O_RDONLY)) == -1) exit(1); /* Obtain the ELF descriptor */ (void) elf_version(EV_CURRENT); if ((elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) failure(); /* Obtain the .shstrtab data buffer */ if (((ehdr = elf32_getehdr(elf)) == NULL) || ((scn = elf_getscn(elf, ehdr->e_shstrndx)) == NULL) || ((data = elf_getdata(scn, NULL)) == NULL)) failure(); /* Traverse input filename, printing each section */ for (cnt = 1, scn = NULL; scn = elf_nextscn(elf, scn); cnt++) { if ((shdr = elf32_getshdr(scn)) == NULL) failure(); (void) printf("[%d]t%s0, cnt, (char *)data->d_buf + shdr->sh_name); } } /* end main */ static void failure() { (void) fprintf(stderr, "%s0, elf_errmsg(elf_errno())); exit(1); }Below is sample output from compiling and executing the above code, which prints the names of the sections using itself as the input file
% cc -o elfprint example.c -lelf % elfprint elfprint [1] .interp [2] .hash [3] .dynsym [4] .dynstr [5] .rela.ex_shared [6] .rela.bss [7] .rela.plt [8] .text [9] .init [10] .fini [11] .exception_ranges ...
ANSI C Programmer’s Guide
Name Processor M32 AT&T WE 32100 SPARC SPARC 386 Intel 80386 486 Intel 80486 860 Intel 80860 68K Motorola 68000 88K Motorola 88000
Other processors will be added to the table as necessary.
To illustrate,
a program could use the following code to ‘‘see’’ the processor-specific information
for the SPARC.
A program could use the
following code to ‘‘see’’ the processor-specific information for the Intel 80386:
Although reading the objects is rather straightforward, writing/updating them can corrupt the shared offsets among sections. Upon creation, relationships are established among the sections that must be maintained even if the object’s size is changed.