binutils Newspaper
JULY 29, 2026
readelf Proposed

readelf: Consolidate get_[32|64]bit_program_headers

Consolidates 32-bit and 64-bit program header reading functions into one in readelf.

This patch consolidates the get_32bit_program_headers and get_64bit_program_headers functions in readelf.c into a single get_program_headers function. The new function efficiently handles both 32-bit and 64-bit ELF program headers, using the BYTE_GET_SIZE macro for reading external ELF program header fields. This consolidation aims to eliminate code duplication and enhance the maintainability of the readelf utility, addressing PR binutils/34356.

In Details

This patch continues the refactoring of readelf's ELF parsing by merging the separate implementations for reading 32-bit and 64-bit program headers (get_32bit_program_headers and get_64bit_program_headers) into a single, unified get_program_headers function. This generalized function leverages the BYTE_GET_SIZE macro for reading fields from the external ELF file, thereby reducing code redundancy and simplifying the parsing logic for program headers. This change is part of the ongoing effort to address PR binutils/34356.

For Context
readelf
A utility that displays information about ELF files, including program header details.
program headers
Data structures within an ELF file that describe segments of the file, such as code, data, and stack, and how they should be mapped into memory.
ELF
Executable and Linkable Format, a standard file format for executables, object code, shared libraries, and core dumps on many Unix-like systems.
consolidation
The process of merging multiple functions or code blocks into a single, more efficient one.
BYTE_GET_SIZE
A macro likely used to read a value of a specific size from a byte buffer and advance the buffer pointer.
Filed Under: readelfelfprogram headersrefactoringcode consolidation