Libelf by Example
Libelf by Example
A tutorial introduction to the ELF(3) & GELF(3) APIs.
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.
Table of Contents
- Preface
- 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
- Index
Causes Supported

The NetBSD Foundation, Inc.
Of course it runs NetBSD
https://www.netbsd.orgNetBSD 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 60-day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
See full terms
80% Royalties. Earn $16 on a $20 book.
We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $12 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
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) and EPUB (for phones, tablets and 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.
Learn more about Leanpub's ebook formats and where to read them