================ @@ -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) { + struct DT_GNU_HASH_HEADER { + uint32_t nbuckets = 0; + uint32_t symoffset = 0; + uint32_t bloom_size = 0; + uint32_t bloom_shift = 0; + }; + if (auto data = ReadDataFromDynamic(gnu_hash, sizeof(DT_GNU_HASH_HEADER))) { ---------------- labath wrote:
This could use a short description (or a link to one) of the format of the section. Nothing too involved, just a couple of sentences to let the reader know what to expect.. 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