Libelf by Example
Last updated on 2020-03-30
About the Book
This tutorial introduces libelf, a library for reading and writing object code in the Extensible Linking Format (ELF) file format.
The tutorial covers:
- Getting started with libelf: obtaining a handle to an ELF object, establishing a working ELF version, and handling errors reported by libelf.
- How ELF data structures are laid out in-memory and on disk, the notions of "file representation" and "memory representation", how to write applications that can handle non-native binaries.
- ELF Segments and the ELF Program Header Table, retrieving the program header table from an ELF executable and the meaning of the fields of a program header table entry.
- How data is stored inside ELF sections, the ELF Section Header Table, and how to traverse the sections in an ELF object.
- How to create new ELF objects: the rules for ordering individual API calls, the default object layout implemented by libelf, and how to specify custom layouts.
- ar archives: their structure, and how to read the contents of these archives using the APIs provided by libelf.
The example programs in this tutorial are written in the C programming language.
While written for the libelf implementation that is part of the Elftoolchain open-source project, this tutorial is expected to be compatible with other libelf implementations, such as that offered by the GNU project.
This tutorial is part of the Leanpub for Causes program; the royalties from its purchases go to an organisation that is developing a liberally licensed open source operating system. The 'source' for this tutorial is itself published under a liberal license; please see the Elftoolchain project's site.
If you would like to see any other ELF related topics covered in a future version of the tutorial, or if you have suggestions for improving it, then please do post at the Leanpub forum for the book.
- 1. Introduction
- 1.1 What to Expect From This Tutorial
- 2. Getting Started
- 3. Peering Inside an ELF Object
- 3.1 ELF Object Kinds
- 3.2 ELF File Layout
- 3.3 Extended Numbering
- 3.4 The Elf32, Elf64 and GElf APIs
- 3.5 File and Memory Representations
- 3.6 Example: Reading an ELF Executable Header
- 4. Examining the Program Header Table
- 4.1 The ELF Program Header Table
- 4.2 Example: Reading a Program Header Table
- 5. Looking at Sections
- 5.1 The Section Header Table
- 5.2 ELF Section Handling With libelf
- 5.3 ELF String Tables
- 5.4 Example: Listing Section Names
- 6. Creating New ELF Objects
- 6.1 Example: Creating an ELF Object
- 6.2 Controlling ELF Layout
- 6.3 Fill Characters
- 6.4 Memory Ownership
- 6.5 Data Structure Lifetimes
- 6.6 Modifying Existing ELF Objects
- 7. Processing ar archives
- 7.1 The Structure of ar Archives
- 7.2 Special Archive Members
- 7.3 Archive Flavors
- 7.4 Archive Symbol Tables
- 7.5 Random Archive Access Using elf_rand
- 7.6 Example: Stepping Through an ar Archive
- 8. Conclusion
- 8.1 Further Reading
- 8.2 Getting Further Help
The NetBSD Foundation, Inc.
Of course it runs NetBSDhttps://www.netbsd.org
NetBSD is a free, fast, secure, and highly portable Unix-like Open Source operating system. It is available for a wide range of platforms, from large-scale servers and powerful desktop systems to handheld and embedded devices.
The Leanpub 45-day 100% Happiness Guarantee
Within 45 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers), EPUB (for phones and tablets) and MOBI (for Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.