[BOLT] Use NameResolver class for local symbols

Summary: NameResolver class is used to assign unique names to local symbols.

(cherry picked from FBD18277131)
This commit is contained in:
Maksim Panchenko 2019-11-01 12:31:17 -07:00
parent 1ed3ac17ff
commit a1388308f0
3 changed files with 25 additions and 23 deletions

View File

@ -173,25 +173,11 @@ class BinaryContext {
/// with size won't overflow
uint32_t DuplicatedJumpTables{0x10000000};
/// Map used for disambiguation of local symbols.
StringMap<uint64_t> LocalSymbols;
public:
/// [name] -> [BinaryData*] map used for global symbol resolution.
using SymbolMapType = StringMap<BinaryData *>;
SymbolMapType GlobalSymbols;
/// Return unique version of a symbol name in the form "<name>/<number>".
std::string uniquifySymbolName(const std::string &Name) {
const auto ID = ++LocalSymbols[Name];
return Name + '/' + std::to_string(ID);
}
/// Release memory used for disambiguation of local symbols.
void freeLocalSymbols() {
clearList(LocalSymbols);
}
/// [address] -> [BinaryData], ...
/// Addresses never change.
/// Note: it is important that clients do not hold on to instances of

View File

@ -1093,11 +1093,13 @@ void RewriteInstance::run() {
void RewriteInstance::discoverFileObjects() {
NamedRegionTimer T("discoverFileObjects", "discover file objects",
TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
NameResolver NR;
FileSymRefs.clear();
BC->getBinaryFunctions().clear();
BC->clearBinaryData();
// For local symbols we want to keep track of associated FILE symbol name for
// disambiguation by combined name.
StringRef FileSymbolName;
@ -1291,9 +1293,9 @@ void RewriteInstance::discoverFileObjects() {
AltPrefix = Name + "/" + std::string(SFI->second);
}
UniqueName = BC->uniquifySymbolName(Name);
UniqueName = NR.uniquifySymbolName(Name);
if (!AltPrefix.empty())
AlternativeName = BC->uniquifySymbolName(AltPrefix);
AlternativeName = NR.uniquifySymbolName(AltPrefix);
}
uint64_t SymbolSize = ELFSymbolRef(Symbol).getSize();
@ -1558,9 +1560,7 @@ void RewriteInstance::discoverFileObjects() {
});
for (const auto &Section : RelocationSections)
readRelocations(Section);
BC->freeLocalSymbols();
readRelocations(Section, NR);
}
void RewriteInstance::disassemblePLT() {
@ -2120,7 +2120,8 @@ bool RewriteInstance::analyzeRelocation(const RelocationRef &Rel,
return true;
}
void RewriteInstance::readRelocations(const SectionRef &Section) {
void RewriteInstance::readRelocations(const SectionRef &Section,
NameResolver &NR) {
StringRef SectionName;
Section.getName(SectionName);
DEBUG(dbgs() << "BOLT-DEBUG: reading relocations for section "
@ -2519,9 +2520,9 @@ void RewriteInstance::readRelocations(const SectionRef &Section) {
} else {
if (StringRef(SymbolName).startswith(
BC->AsmInfo->getPrivateGlobalPrefix())) {
Name = BC->uniquifySymbolName("PG" + SymbolName);
Name = NR.uniquifySymbolName("PG" + SymbolName);
} else {
Name = BC->uniquifySymbolName(SymbolName);
Name = NR.uniquifySymbolName(SymbolName);
}
}
ReferencedSymbol = BC->registerNameAtAddress(Name,

View File

@ -45,6 +45,21 @@ class RewriteInstanceDiff;
/// optimizations) and rewriting. It also has the logic to coordinate such
/// events.
class RewriteInstance {
private:
/// Class used for assigning unique names to local symbols.
class NameResolver {
private:
/// Track the number of duplicate names.
StringMap<uint64_t> LocalSymbols;
public:
/// Return unique version of a symbol name in the form "<name>/<number>".
std::string uniquifySymbolName(const std::string &Name) {
const auto ID = ++LocalSymbols[Name];
return Name + '/' + std::to_string(ID);
}
};
public:
RewriteInstance(llvm::object::ELFObjectFileBase *File, DataReader &DR,
DataAggregator &DA, const int Argc, const char *const *Argv,
@ -70,7 +85,7 @@ public:
void adjustCommandLineOptions();
/// Read relocations from a given section.
void readRelocations(const object::SectionRef &Section);
void readRelocations(const object::SectionRef &Section, NameResolver &NR);
/// Read information from debug sections.
void readDebugInfo();