MCObjectSymbolizer: Switch from IntervalMap to sorted vector, following r182625.

This removes the need for the missing SectionRef operator< workaround, and fixes
an IntervalMap assert about alignment on MSVC.

llvm-svn: 182949
This commit is contained in:
Ahmed Bougacha 2013-05-30 18:18:36 +00:00
parent 278d9de314
commit 75633ba1e7
2 changed files with 54 additions and 17 deletions

View File

@ -15,10 +15,10 @@
#ifndef LLVM_MC_MCOBJECTSYMBOLIZER_H #ifndef LLVM_MC_MCOBJECTSYMBOLIZER_H
#define LLVM_MC_MCOBJECTSYMBOLIZER_H #define LLVM_MC_MCOBJECTSYMBOLIZER_H
#include "llvm/ADT/IntervalMap.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/MC/MCSymbolizer.h" #include "llvm/MC/MCSymbolizer.h"
#include "llvm/Object/ObjectFile.h" #include "llvm/Object/ObjectFile.h"
#include <vector>
namespace llvm { namespace llvm {
@ -33,12 +33,8 @@ protected:
const object::ObjectFile *Obj; const object::ObjectFile *Obj;
typedef DenseMap<uint64_t, object::RelocationRef> AddrToRelocMap; typedef DenseMap<uint64_t, object::RelocationRef> AddrToRelocMap;
// FIXME: Working around a missing SectionRef operator!= by storing typedef std::vector<object::SectionRef> SortedSectionList;
// DataRefImpl.p instead of SectionRef. Feel free to improve! SortedSectionList SortedSections;
typedef IntervalMap<uint64_t, uintptr_t> AddrToSectionMap;
AddrToSectionMap::Allocator AddrToSectionAllocator;
AddrToSectionMap AddrToSection;
// Map a load address to the first relocation that applies there. As far as I // Map a load address to the first relocation that applies there. As far as I
// know, if there are several relocations at the exact same address, they are // know, if there are several relocations at the exact same address, they are
@ -48,6 +44,11 @@ protected:
// relocation (referencing the subtrahend symbol). // relocation (referencing the subtrahend symbol).
AddrToRelocMap AddrToReloc; AddrToRelocMap AddrToReloc;
// Helpers around SortedSections.
SortedSectionList::const_iterator findSectionContaining(uint64_t Addr) const;
void insertSection(object::SectionRef Section);
MCObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo, MCObjectSymbolizer(MCContext &Ctx, OwningPtr<MCRelocationInfo> &RelInfo,
const object::ObjectFile *Obj); const object::ObjectFile *Obj);

View File

@ -17,6 +17,7 @@
#include "llvm/Object/MachO.h" #include "llvm/Object/MachO.h"
#include "llvm/Object/ELF.h" #include "llvm/Object/ELF.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include <algorithm>
using namespace llvm; using namespace llvm;
using namespace object; using namespace object;
@ -40,15 +41,16 @@ public:
return; return;
} }
uint64_t Addr = Value; uint64_t Addr = Value;
AddrToSectionMap::const_iterator SI = AddrToSection.find(Addr); SortedSectionList::const_iterator SI = findSectionContaining(Addr);
if (SI.valid()) { errs() << " looking for sec " << Addr << "\n";
DataRefImpl DRI; DRI.p = *SI; if (SI != SortedSections.end()) {
SectionRef S(DRI, Obj); const SectionRef &S = *SI;
StringRef Name; S.getName(Name); StringRef Name; S.getName(Name);
uint64_t SAddr; S.getAddress(SAddr);
if (Name == "__cstring") { if (Name == "__cstring") {
StringRef Contents; StringRef Contents;
S.getContents(Contents); S.getContents(Contents);
Contents = Contents.substr(Addr - SI.start()); Contents = Contents.substr(Addr - SAddr);
cStream << " ## literal pool for: " cStream << " ## literal pool for: "
<< Contents.substr(0, Contents.find_first_of(0)); << Contents.substr(0, Contents.find_first_of(0));
} }
@ -62,9 +64,7 @@ public:
MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx, MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx,
OwningPtr<MCRelocationInfo> &RelInfo, OwningPtr<MCRelocationInfo> &RelInfo,
const ObjectFile *Obj) const ObjectFile *Obj)
: MCSymbolizer(Ctx, RelInfo), Obj(Obj), : MCSymbolizer(Ctx, RelInfo), Obj(Obj), SortedSections(), AddrToReloc() {
AddrToSectionAllocator(), AddrToSection(AddrToSectionAllocator),
AddrToReloc() {
error_code ec; error_code ec;
for (section_iterator SI = Obj->begin_sections(), for (section_iterator SI = Obj->begin_sections(),
SE = Obj->end_sections(); SE = Obj->end_sections();
@ -81,8 +81,7 @@ MCObjectSymbolizer::MCObjectSymbolizer(MCContext &Ctx,
bool RequiredForExec; RelSecI->isRequiredForExecution(RequiredForExec); bool RequiredForExec; RelSecI->isRequiredForExecution(RequiredForExec);
if (RequiredForExec == false || Size == 0) if (RequiredForExec == false || Size == 0)
continue; continue;
AddrToSection.insert(StartAddr, StartAddr + Size - 1, insertSection(*SI);
SI->getRawDataRefImpl().p);
for (relocation_iterator RI = SI->begin_relocations(), for (relocation_iterator RI = SI->begin_relocations(),
RE = SI->end_relocations(); RE = SI->end_relocations();
RI != RE; RI != RE;
@ -177,3 +176,40 @@ MCObjectSymbolizer::createObjectSymbolizer(MCContext &Ctx,
} }
return new MCObjectSymbolizer(Ctx, RelInfo, Obj); return new MCObjectSymbolizer(Ctx, RelInfo, Obj);
} }
// SortedSections implementation.
static bool SectionStartsBefore(const SectionRef &S, uint64_t Addr) {
uint64_t SAddr; S.getAddress(SAddr);
return SAddr < Addr;
}
MCObjectSymbolizer::SortedSectionList::const_iterator
MCObjectSymbolizer::findSectionContaining(uint64_t Addr) const {
SortedSectionList::const_iterator
EndIt = SortedSections.end(),
It = std::lower_bound(SortedSections.begin(), EndIt,
Addr, SectionStartsBefore);
if (It == EndIt)
return It;
uint64_t SAddr; It->getAddress(SAddr);
uint64_t SSize; It->getSize(SSize);
if (Addr >= SAddr + SSize)
return EndIt;
return It;
}
void MCObjectSymbolizer::insertSection(SectionRef Sec) {
uint64_t SAddr; Sec.getAddress(SAddr);
uint64_t SSize; Sec.getSize(SSize);
SortedSectionList::iterator It = std::lower_bound(SortedSections.begin(),
SortedSections.end(),
SAddr,
SectionStartsBefore);
if (It != SortedSections.end()) {
uint64_t FoundSAddr; It->getAddress(FoundSAddr);
if (FoundSAddr < SAddr + SSize)
llvm_unreachable("Inserting overlapping sections");
}
SortedSections.insert(It, Sec);
}