binutils Newspaper
JULY 29, 2026
readelf Proposed

readelf: move code around

Refactors `readelf.c` to reduce `#ifdef` directives and improve code organization.

This patch is the fifth in a series, aiming to refactor the readelf.c source file by rearranging code to minimize conditional compilation (#ifdef) blocks. The goal is to improve code cleanliness and maintainability. While the author notes that a more significant refactoring (e.g., using function pointers) might offer greater benefits, this patch focuses on a more modest code movement to limit the presence ofifdef-heavy sections.

In the Thread 1 participant
  1. Jan Beulich <jbeulich@suse.com> proposer

    Submits the final patch (5/5) in a series that rearranges code within `readelf.c` to reduce `#ifdef` directives and improve code organization.

    “Try to limit recently introduced #ifdef-ary at least a little. --- I was hoping for the overall effect to be better, yet I also didn't want to go too far with the re-arrangement. One aspect I noticed: Instead of the trivial get_elf_symbols() wrapper we could use a function pointer instead. That, however, already wouldn't quite extend to get_section_headers() and get_dynamic_section(), let alone g…”

In Details

This patch is part of a series aimed at refactoring readelf.c. It specifically addresses the code duplication and conditional compilation required to handle both 32-bit and 64-bit ELF file formats. The author moves sections of code, including the definitions for get_program_headers and get_dynamic_section, and utilizes C preprocessor macros (ElfXX) to include the same source file (readelf.c) twice with different ElfXX definitions, thereby reducing explicit #ifdef blocks for 32-bit vs. 64-bit handling. The intent is to simplify the source file and reduce maintenance burden.

For Context
readelf
A utility in GNU Binutils that displays information about ELF (Executable and Linkable Format) files.
ELF
Executable and Linkable Format, a standard file format for object files, executables, and shared libraries on Unix-like systems.
#ifdef
A C preprocessor directive used for conditional compilation. Code blocks enclosed in #ifdef/#endif are only included in the compilation if a specified macro is defined.
Function pointer
A variable that stores the memory address of a function, allowing functions to be called indirectly.
Filed Under: readelfrefactoringELFcode-qualitymaintainability