readelf: Save and dump original section header values
Preserve and display original section header values in readelf to aid debugging of malformed object files.
This patch for readelf introduces functionality to save and display the original, potentially garbage, section header values before they are cleaned up or validated. This is intended to aid debugging by making inconsistent or incorrect header data visible when dumping section headers, helping to diagnose issues in malformed object files. The changes involve adding a new field to store these original values and a function to save them, with a modification to clear this storage on demand.
- proposer
Proposes to save and dump original section header values in readelf to make garbage values visible for debugging malformed object files, modifying the approach to save original values on demand.
“validate_section_info clears the garbage values in the section header to avoid crash later. Save and dump the original section header values to make the garbage values in the section header visible when dumping section headers.”
- reviewer
Suggests an alternative approach where a separate structure holds "sane" section headers, allocating entries only when corrections are needed, rather than always storing original values.
“I think this would be better done the other way around. ie. have a Elf_Internal_Shdr **sane_section_headers that is initialised to point at entries in section_headers, with sane_section_headers[i] allocated as necessary when needing to correct a bogus header.”
- proposer
Rejects the alternative approach due to complexity, opting instead to modify the `orig_section_headers` to be allocated on demand.
“I have thought about something similar and decided against it since filedata->section_headers is used in many places. However, I can change filedata->orig_section_headers to on demand.”
Technical Tradeoffs
- Adds memory overhead to store original section header data, but this is managed by on-demand allocation.
- Improves debuggability of malformed object files at the cost of slightly increased complexity in `readelf`.
In Details
This series of patches for readelf aims to improve debugging of malformed object files by preserving and displaying original section header data. The validate_section_info function currently cleans up bad data, obscuring the original issue. The proposal introduces orig_section_headers to store the initial values, allowing them to be shown when readelf dumps section header information. A discussion occurred regarding the best way to manage this data, with a preference for on-demand allocation over a separate "sane" structure.
- readelf
- A utility program that displays information about ELF (Executable and Linkable Format) files, including sections, symbols, and relocation entries.
- section header
- A structure within an ELF file that describes a section, such as its name, type, load address, and size.
- malformed object files
- Object files (like ELF or PE/COFF) that do not strictly adhere to their defined format, often containing incorrect or corrupt data.
- ELF
- Executable and Linkable Format, a standard file format for executables, object code, shared libraries, and core dumps used in most Unix-like systems.
- validate_section_info
- A function within binutils' readelf that checks and potentially corrects or sanitizes section header information.