Libelf by Example
With Membership
Suggested price

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.

  • Share this book

  • Categories

    • Computers and Programming
    • Software
    • C and C++
  • 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'm also a computer scientist.

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. 

I hope you find my work useful!

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

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 NetBSD Project is an international collaborative effort of a large group of people, to produce a freely available and redistributable UNIX-like operating system, NetBSD. In addition to our own work, NetBSD contains a variety of other free software, including 4.4BSD Lite2 from the University of California, Berkeley. One of the primary focuses of the NetBSD project has been to make the base OS highly portable. This has resulted in NetBSD being ported to a large number of hardware platforms. NetBSD is also interoperable, implementing many standard APIs and network protocols, and emulating many other systems' ABIs. The NetBSD Foundation is incorporated in the United States as a tax-exempt corporation (under Section 501(c)(3) of the US Internal Revenue Code) that devotes itself to the traditional goals and spirit of the NetBSD Project and owns the trademark of the word “NetBSD”.

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.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

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 $13 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

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. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub