Libelf by Example
Libelf by Example


Journalists and bloggers: please do not publish anything about this until the embargo is lifted!

Please contact the author for more information about when this is.

Libelf by Example

About the Book

This tutorial introduces libelf, a library implementing the ELF(3)/GELF(3) API 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 code in this tutorial is 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 'source' for this tutorial is itself published under a liberal license; please see the Elftoolchain project's site.

  • Share this book

  • Feedback

    You must own a copy of this Book to access the forums

    Email the Author(s)

About the Author

Joseph Koshy
Joseph Koshy

Hello there!

I am a software developer. I started programming on an IBM 370/155, using FORTRAN and punched cards.

I have spent much of my professional life working on kernels, systems software, performance analysis and tackling problems of scale.

I’m happiest when writing open-source code. In my free time I work on the Elftoolchain project. 

Hope you find my work useful!

Table of Contents

  • Preface
  • 1. Introduction
    • 1.1 Tutorial Overview
    • 1.2 Tutorial Structure
  • 2. Getting Started
    • 2.1 Example: Getting started with libelf
  • 3. Peering Inside an ELF Object
    • 3.1 The Layout of an ELF file
    • 3.2 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 ELF section handling with libelf
    • 5.2 Example: Listing section names
  • 6. Creating new ELF objects
    • 6.1 Example: Creating an ELF object
    • 6.2 The finer points in creating ELF objects
  • 7. Processing ar(1) archives
    • 7.1 Archive structure
    • 7.2 Example: Stepping through an ar(1) archive
  • 8. Conclusion
    • 8.1 Further Reading
    • 8.2 Getting Further Help

Causes Supported

Electronic Frontier Foundation

Defending your civil liberties in a digital world.

Based in San Francisco, EFF is a donor-supported membership organization working to protect fundamental rights regardless of technology.

From the Internet to the iPod, technologies are transforming our society and empowering us as speakers, citizens, creators, and consumers. When our freedoms in the networked world come under attack, the Electronic Frontier Foundation (EFF) is the first line of defense. EFF broke new ground when it was founded in 1990—well before the Internet was on most people's radar—and continues to confront cutting-edge issues defending free speech, privacy, innovation, and consumer rights today. From the beginning, EFF has championed the public interest in every critical battle affecting digital rights.

Authors have earned$8,411,992writing, publishing and selling on Leanpub,
earning 80% royalties while saving up to 25 million pounds of CO2 and up to 46,000 trees.

Learn more about writing on Leanpub

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.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses! Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks. Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. It really is that easy.

Learn more about writing on Leanpub