================ @@ -3895,3 +3924,103 @@ std::optional<lldb_private::DataExtractor> ObjectFileELF::GetDynamicData() { } return std::nullopt; } + + +std::optional<DataExtractor> +ObjectFileELF::GetDynsymDataFromDynamic(uint32_t &num_symbols) { + // Every ELF file which represents an executable or shared library has + // mandatory .dynamic entries. The DT_SYMTAB value contains a pointer to the + // symbol table, and DT_SYMENT contains the size of a symbol table entry. + // We then can use either the DT_HASH or DT_GNU_HASH to find the number of + // symbols in the symbol table as the symbol count is not stored in the + // .dynamic section as a key/value pair. + // + // When loading and ELF file from memory, only the program headers end up + // being mapped into memory, and we can find these values in the PT_DYNAMIC + // segment. + num_symbols = 0; + // Get the process in case this is an in memory ELF file. + ProcessSP process_sp(m_process_wp.lock()); + const ELFDynamic *symtab = FindDynamicSymbol(DT_SYMTAB); + const ELFDynamic *syment = FindDynamicSymbol(DT_SYMENT); + const ELFDynamic *hash = FindDynamicSymbol(DT_HASH); + const ELFDynamic *gnu_hash = FindDynamicSymbol(DT_GNU_HASH); + // DT_SYMTAB and DT_SYMENT are mandatory. + if (symtab == nullptr || syment == nullptr) + return std::nullopt; + // We must have either a DT_HASH or a DT_GNU_HASH. + if (hash == nullptr && gnu_hash == nullptr) + return std::nullopt; + // The number of symbols in the symbol table is the number of entries in the + // symbol table divided by the size of each symbol table entry. + // We must figure out the number of symbols in the symbol table using the + // DT_HASH or the DT_GNU_HASH as the number of symbols isn't stored anywhere + // in the .dynamic section. + + lldb::offset_t offset; + if (hash) { + // The DT_HASH header contains the number of symbols in the "nchain" + // member. The header looks like this: + // struct DT_HASH_HEADER { + // uint32_t nbucket; + // uint32_t nchain; + // }; + if (auto data = ReadDataFromDynamic(hash, 8)) { + offset = 4; + num_symbols = data->GetU32(&offset); + } + } + if (num_symbols == 0 && gnu_hash) { ---------------- labath wrote:
This code is rather involved, and it would be better to have it in a helper function. Then it could also use early returns for some of the paths. I'd recommend something like: ``` if (std::optional<uint32_t> syms = GetNumSymbolsFromHash()) num_symbols = *syms; else if (std::optional<uint32_t> syms = GetNumSymbolsFromDtHash()) num_symbols = *syms; ... ``` https://github.com/llvm/llvm-project/pull/112596 _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits