From 660daac2d0489045f2bb142a9f7eb5b7737f3594 Mon Sep 17 00:00:00 2001 From: Rafael Auler Date: Mon, 11 Dec 2017 17:07:56 -0800 Subject: [PATCH] [BOLT] Fix -simplify-rodata-loads wrt data chunks with relocs Summary: The pass was previously copying data that would change after layout because it had a relocation at the copied address. (cherry picked from FBD6541334) --- bolt/BinaryContext.cpp | 16 ++++++++++++++++ bolt/BinaryContext.h | 4 ++++ bolt/Passes/BinaryPasses.cpp | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/bolt/BinaryContext.cpp b/bolt/BinaryContext.cpp index d1f38138bb09..02821658b973 100644 --- a/bolt/BinaryContext.cpp +++ b/bolt/BinaryContext.cpp @@ -525,6 +525,22 @@ void BinaryContext::removeRelocationAt(uint64_t Address) { Relocations.erase(RelocI); } +const Relocation *BinaryContext::getRelocationAt(uint64_t Address) { + auto ContainingSection = getSectionForAddress(Address); + assert(ContainingSection && "cannot find section for address"); + auto RI = SectionRelocations.find(*ContainingSection); + if (RI == SectionRelocations.end()) + return nullptr; + + auto &Relocations = RI->second; + auto RelocI = Relocations.find( + Relocation{Address - ContainingSection->getAddress(), 0, 0, 0, 0}); + if (RelocI == Relocations.end()) + return nullptr; + + return &*RelocI; +} + size_t Relocation::getSizeForType(uint64_t Type) { switch (Type) { default: diff --git a/bolt/BinaryContext.h b/bolt/BinaryContext.h index ad4909e9f013..69b26b1d5c89 100644 --- a/bolt/BinaryContext.h +++ b/bolt/BinaryContext.h @@ -301,6 +301,10 @@ public: /// Remove registered relocation at a given \p Address. void removeRelocationAt(uint64_t Address); + /// Return a relocation registered at a given \p Address, or nullptr if there + /// is no relocation at such address. + const Relocation *getRelocationAt(uint64_t Address); + const BinaryFunction *getFunctionForSymbol(const MCSymbol *Symbol) const { auto BFI = SymbolToFunctionMap.find(Symbol); return BFI == SymbolToFunctionMap.end() ? nullptr : BFI->second; diff --git a/bolt/Passes/BinaryPasses.cpp b/bolt/Passes/BinaryPasses.cpp index ddcd87974dac..6f78bb856f2c 100644 --- a/bolt/Passes/BinaryPasses.cpp +++ b/bolt/Passes/BinaryPasses.cpp @@ -1146,6 +1146,10 @@ bool SimplifyRODataLoads::simplifyRODataLoads( SectionRef DataSection = DataSectionOrErr.get(); if (!DataSection.isReadOnly()) continue; + + if (BC.getRelocationAt(TargetAddress)) + continue; + uint32_t Offset = TargetAddress - DataSection.getAddress(); StringRef ConstantData; if (std::error_code EC = DataSection.getContents(ConstantData)) {