Libelf by Example
Libelf by Example
Free!
Minimum price
$12.99
Suggested price
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.

  • 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'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
https://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 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”.

Authors have earned$8,700,029writing, 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