diff --git a/include/llvm/MC/MCSection.h b/include/llvm/MC/MCSection.h index b64a44cdd0a..1c01b2f8f3c 100644 --- a/include/llvm/MC/MCSection.h +++ b/include/llvm/MC/MCSection.h @@ -52,6 +52,14 @@ namespace llvm { virtual void PrintSwitchToSection(const MCAsmInfo &MAI, raw_ostream &OS) const = 0; + /// isBaseAddressKnownZero - Return true if we know that this section will + /// get a base address of zero. In cases where we know that this is true we + /// can emit section offsets as direct references to avoid a subtraction + /// from the base of the section, saving a relocation. + virtual bool isBaseAddressKnownZero() const { + return false; + } + // UseCodeAlign - Return true if a .align directive should use // "optimized nops" to fill instead of 0s. virtual bool UseCodeAlign() const = 0; diff --git a/include/llvm/MC/MCSectionELF.h b/include/llvm/MC/MCSectionELF.h index 26951725e49..c82de712820 100644 --- a/include/llvm/MC/MCSectionELF.h +++ b/include/llvm/MC/MCSectionELF.h @@ -66,6 +66,12 @@ public: virtual bool UseCodeAlign() const; virtual bool isVirtualSection() const; + /// isBaseAddressKnownZero - We know that non-allocatable sections (like + /// debug info) have a base of zero. + virtual bool isBaseAddressKnownZero() const { + return (getFlags() & ELF::SHF_ALLOC) == 0; + } + static bool classof(const MCSection *S) { return S->getVariant() == SV_ELF; } diff --git a/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp b/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp index 68e80e3983a..98a1bf2f1ce 100644 --- a/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp +++ b/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp @@ -191,6 +191,13 @@ void AsmPrinter::EmitSectionOffset(const MCSymbol *Label, assert((!Label->isInSection() || &Label->getSection() == &Section) && "Section offset using wrong section base for label"); + // If the section in question will end up with an address of 0 anyway, we can + // just emit an absolute reference to save a relocation. + if (Section.isBaseAddressKnownZero()) { + OutStreamer.EmitSymbolValue(Label, 4, 0/*AddrSpace*/); + return; + } + // Otherwise, emit it as a label difference from the start of the section. EmitLabelDifference(Label, SectionLabel, 4); }