Skip to content

Commit 9187bbf

Browse files
committed
[lldb][ELF] Move address class map into the symbol table
Code in llvm#90622 exposed a situation where a symbol table may be loaded via a second copy of the same object. This resulted in the first copy having its address class map updated, but not the second. And it was that second one we used for symbol lookups, since it was found by a plugin and we assume those to be more specific. (the plugins returning the same file may itself be a bug) I tried fixing this by returning the address map changes from the symbol table parsing. Which works but it's awkward to make sure both object's maps get updated. Instead, this change moves the address class map into the symbol table, which is shared between the objects already. ObjectFileELF::GetAddressClass was already checking whether the symbol table existed anyway, so we are ok to use it.
1 parent 58a94b1 commit 9187bbf

File tree

4 files changed

+54
-34
lines changed

4 files changed

+54
-34
lines changed

lldb/include/lldb/Symbol/Symtab.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Symtab {
2323
public:
2424
typedef std::vector<uint32_t> IndexCollection;
2525
typedef UniqueCStringMap<uint32_t> NameToIndexMap;
26+
typedef std::map<lldb::addr_t, lldb_private::AddressClass>
27+
FileAddressToAddressClassMap;
2628

2729
enum Debug {
2830
eDebugNo, // Not a debug symbol
@@ -239,6 +241,16 @@ class Symtab {
239241
}
240242
/// \}
241243

244+
/// Set the address class for the given address.
245+
void SetAddressClass(lldb::addr_t addr,
246+
lldb_private::AddressClass addr_class);
247+
248+
/// Lookup the address class of the given address.
249+
///
250+
/// \return
251+
/// The address' class, if it is known, otherwise AddressClass::eCode.
252+
lldb_private::AddressClass GetAddressClass(lldb::addr_t addr);
253+
242254
protected:
243255
typedef std::vector<Symbol> collection;
244256
typedef collection::iterator iterator;
@@ -274,6 +286,9 @@ class Symtab {
274286
collection m_symbols;
275287
FileRangeToIndexMap m_file_addr_to_index;
276288

289+
/// The address class for each symbol in the elf file
290+
FileAddressToAddressClassMap m_address_class_map;
291+
277292
/// Maps function names to symbol indices (grouped by FunctionNameTypes)
278293
std::map<lldb::FunctionNameType, UniqueCStringMap<uint32_t>>
279294
m_name_to_symbol_indices;

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -769,17 +769,7 @@ AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) {
769769
if (res != AddressClass::eCode)
770770
return res;
771771

772-
auto ub = m_address_class_map.upper_bound(file_addr);
773-
if (ub == m_address_class_map.begin()) {
774-
// No entry in the address class map before the address. Return default
775-
// address class for an address in a code section.
776-
return AddressClass::eCode;
777-
}
778-
779-
// Move iterator to the address class entry preceding address
780-
--ub;
781-
782-
return ub->second;
772+
return symtab->GetAddressClass(file_addr);
783773
}
784774

785775
size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) {
@@ -2213,18 +2203,18 @@ unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
22132203
switch (mapping_symbol) {
22142204
case 'a':
22152205
// $a[.<any>]* - marks an ARM instruction sequence
2216-
m_address_class_map[symbol.st_value] = AddressClass::eCode;
2206+
symtab->SetAddressClass(symbol.st_value, AddressClass::eCode);
22172207
break;
22182208
case 'b':
22192209
case 't':
22202210
// $b[.<any>]* - marks a THUMB BL instruction sequence
22212211
// $t[.<any>]* - marks a THUMB instruction sequence
2222-
m_address_class_map[symbol.st_value] =
2223-
AddressClass::eCodeAlternateISA;
2212+
symtab->SetAddressClass(symbol.st_value,
2213+
AddressClass::eCodeAlternateISA);
22242214
break;
22252215
case 'd':
22262216
// $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2227-
m_address_class_map[symbol.st_value] = AddressClass::eData;
2217+
symtab->SetAddressClass(symbol.st_value, AddressClass::eData);
22282218
break;
22292219
}
22302220
}
@@ -2238,11 +2228,11 @@ unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
22382228
switch (mapping_symbol) {
22392229
case 'x':
22402230
// $x[.<any>]* - marks an A64 instruction sequence
2241-
m_address_class_map[symbol.st_value] = AddressClass::eCode;
2231+
symtab->SetAddressClass(symbol.st_value, AddressClass::eCode);
22422232
break;
22432233
case 'd':
22442234
// $d[.<any>]* - marks a data item sequence (e.g. lit pool)
2245-
m_address_class_map[symbol.st_value] = AddressClass::eData;
2235+
symtab->SetAddressClass(symbol.st_value, AddressClass::eData);
22462236
break;
22472237
}
22482238
}
@@ -2260,11 +2250,11 @@ unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
22602250
// conjunction with symbol.st_value to produce the final
22612251
// symbol_value that we store in the symtab.
22622252
symbol_value_offset = -1;
2263-
m_address_class_map[symbol.st_value ^ 1] =
2264-
AddressClass::eCodeAlternateISA;
2253+
symtab->SetAddressClass(symbol.st_value ^ 1,
2254+
AddressClass::eCodeAlternateISA);
22652255
} else {
22662256
// This address is ARM
2267-
m_address_class_map[symbol.st_value] = AddressClass::eCode;
2257+
symtab->SetAddressClass(symbol.st_value, AddressClass::eCode);
22682258
}
22692259
}
22702260
}
@@ -2285,17 +2275,19 @@ unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id,
22852275
*/
22862276
if (arch.IsMIPS()) {
22872277
if (IS_MICROMIPS(symbol.st_other))
2288-
m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
2278+
symtab->SetAddressClass(symbol.st_value,
2279+
AddressClass::eCodeAlternateISA);
22892280
else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) {
22902281
symbol.st_value = symbol.st_value & (~1ull);
2291-
m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA;
2282+
symtab->SetAddressClass(symbol.st_value,
2283+
AddressClass::eCodeAlternateISA);
22922284
} else {
22932285
if (symbol_type == eSymbolTypeCode)
2294-
m_address_class_map[symbol.st_value] = AddressClass::eCode;
2286+
symtab->SetAddressClass(symbol.st_value, AddressClass::eCode);
22952287
else if (symbol_type == eSymbolTypeData)
2296-
m_address_class_map[symbol.st_value] = AddressClass::eData;
2288+
symtab->SetAddressClass(symbol.st_value, AddressClass::eData);
22972289
else
2298-
m_address_class_map[symbol.st_value] = AddressClass::eUnknown;
2290+
symtab->SetAddressClass(symbol.st_value, AddressClass::eUnknown);
22992291
}
23002292
}
23012293
}
@@ -3060,10 +3052,10 @@ void ObjectFileELF::ParseSymtab(Symtab &lldb_symtab) {
30603052
if (arch.GetMachine() == llvm::Triple::arm &&
30613053
(entry_point_file_addr & 1)) {
30623054
symbol.GetAddressRef().SetOffset(entry_point_addr.GetOffset() ^ 1);
3063-
m_address_class_map[entry_point_file_addr ^ 1] =
3064-
AddressClass::eCodeAlternateISA;
3055+
lldb_symtab.SetAddressClass(entry_point_file_addr ^ 1,
3056+
AddressClass::eCodeAlternateISA);
30653057
} else {
3066-
m_address_class_map[entry_point_file_addr] = AddressClass::eCode;
3058+
lldb_symtab.SetAddressClass(entry_point_file_addr, AddressClass::eCode);
30673059
}
30683060
lldb_symtab.AddSymbol(symbol);
30693061
}

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ class ObjectFileELF : public lldb_private::ObjectFile {
187187
typedef DynamicSymbolColl::iterator DynamicSymbolCollIter;
188188
typedef DynamicSymbolColl::const_iterator DynamicSymbolCollConstIter;
189189

190-
typedef std::map<lldb::addr_t, lldb_private::AddressClass>
191-
FileAddressToAddressClassMap;
192-
193190
/// Version of this reader common to all plugins based on this class.
194191
static const uint32_t m_plugin_version = 1;
195192
static const uint32_t g_core_uuid_magic;
@@ -227,9 +224,6 @@ class ObjectFileELF : public lldb_private::ObjectFile {
227224
/// The architecture detected from parsing elf file contents.
228225
lldb_private::ArchSpec m_arch_spec;
229226

230-
/// The address class for each symbol in the elf file
231-
FileAddressToAddressClassMap m_address_class_map;
232-
233227
/// Returns the index of the given section header.
234228
size_t SectionIndex(const SectionHeaderCollIter &I);
235229

lldb/source/Symbol/Symtab.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,3 +1372,22 @@ bool Symtab::LoadFromCache() {
13721372
SetWasLoadedFromCache();
13731373
return result;
13741374
}
1375+
1376+
void Symtab::SetAddressClass(lldb::addr_t addr,
1377+
lldb_private::AddressClass addr_class) {
1378+
m_address_class_map.insert_or_assign(addr, addr_class);
1379+
}
1380+
1381+
lldb_private::AddressClass Symtab::GetAddressClass(lldb::addr_t addr) {
1382+
auto ub = m_address_class_map.upper_bound(addr);
1383+
if (ub == m_address_class_map.begin()) {
1384+
// No entry in the address class map before the address. Return default
1385+
// address class for an address in a code section.
1386+
return AddressClass::eCode;
1387+
}
1388+
1389+
// Move iterator to the address class entry preceding address
1390+
--ub;
1391+
1392+
return ub->second;
1393+
}

0 commit comments

Comments
 (0)