[llvm] [objcopy] Fix uninitialized ShndxType value in ELF symbol (PR #197739)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Tue May 19 23:57:34 PDT 2026


jh7370 wrote:

> For training purpose, I'm writing a small tool similar to readelf or objcopy. Instead of re-writing the whole code, I use the objcopy static library ELF format parsing functions to load ELF files, as this is probably a stable and complete ELF file parser.
> 
> Once ELF data loaded in memory, I can easily process it, but I was getting strange values for the ShndxType struct member. After some investigation, it was due to it being uninitalized.
> 
> I thought this was a mistake and maybe a potential bug, easy to fix, so I made this pull request. As I'm not familiar with the LLVM code base, I could have overthinking.

Yeah, it's not a mistake: it's very much by design that the value is uninitialized if it isn't used. To initialize it would imply that there is some significance to the value, when in fact there isn't. In ELF terms, it's to do with the symtab shndx section, i.e. the section that is used when there are too many symbols in the symbol table to be indexable via the normal logic (see https://gabi.xinuos.com/elf/05-symtab.html#section-index, specifically the bit about SHN_XINDEX). You can see how it is used in the `Symbol::getShndx` method and initialized in `SymbolTableSection::addSymbol`. These are the only places the value is referenced

I think this PR can be closed. Feel free to reopen if you think there is still a problem.

https://github.com/llvm/llvm-project/pull/197739


More information about the llvm-commits mailing list