Skip to content

Commit

Permalink
[BOLT] Relocation support for non-allocatable sections.
Browse files Browse the repository at this point in the history
Summary:
Relocations can be created for non-allocatable (aka Note) sections.

To start using this for debug info, the emission has to be moved
earlier in the pipeline for relocation processing to kick in.

(cherry picked from FBD4835204)
  • Loading branch information
maksfb committed Apr 5, 2017
1 parent a990053 commit 34c8a7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
5 changes: 2 additions & 3 deletions bolt/DWARFRewriter.cpp
Expand Up @@ -396,9 +396,8 @@ void RewriteInstance::updateFunctionRanges() {
}

void RewriteInstance::generateDebugRanges() {
using RangeType = enum { RANGES, ARANGES };
for (int IntRT = RANGES; IntRT <= ARANGES; ++IntRT) {
RangeType RT = static_cast<RangeType>(IntRT);
enum { RANGES, ARANGES };
for (auto RT = RANGES + 0; RT <= ARANGES; ++RT) {
const char *SectionName = (RT == RANGES) ? ".debug_ranges"
: ".debug_aranges";
SmallVector<char, 16> RangesBuffer;
Expand Down
55 changes: 26 additions & 29 deletions bolt/RewriteInstance.cpp
Expand Up @@ -455,27 +455,21 @@ uint8_t *ExecutableFileMemoryManager::recordNoteSection(
<< " with size " << Size << ", alignment " << Alignment
<< " at 0x"
<< Twine::utohexstr(reinterpret_cast<uint64_t>(Data)) << '\n');
if (SectionName == ".debug_line") {
// We need to make a copy of the section contents if we'll need it for
// a future reference.
uint8_t *DataCopy = new uint8_t[Size];
memcpy(DataCopy, Data, Size);
NoteSectionInfo[SectionName] =
SectionInfo(reinterpret_cast<uint64_t>(DataCopy),
Size,
Alignment,
/*IsCode=*/false,
/*IsReadOnly=*/true,
/*IsLocal=*/false,
0,
0,
SectionID);
return DataCopy;
} else {
DEBUG(dbgs() << "BOLT-DEBUG: ignoring section " << SectionName
<< " in recordNoteSection()\n");
return nullptr;
}
// a future reference. RuntimeDyld will not allocate the space forus.
uint8_t *DataCopy = new uint8_t[Size];
memcpy(DataCopy, Data, Size);
NoteSectionInfo[SectionName] =
SectionInfo(reinterpret_cast<uint64_t>(DataCopy),
Size,
Alignment,
/*IsCode=*/false,
/*IsReadOnly=*/true,
/*IsLocal=*/false,
0,
0,
SectionID);
return DataCopy;
}

bool ExecutableFileMemoryManager::finalizeMemory(std::string *ErrMsg) {
Expand Down Expand Up @@ -2105,14 +2099,11 @@ void RewriteInstance::emitFunctions() {
Streamer->EmitLabel(BC->Ctx->getOrCreateSymbol("__hot_end"));
}

if (opts::Relocs) {
emitDataSections(Streamer.get());
}


if (opts::UpdateDebugSections)
updateDebugLineInfoForNonSimpleFunctions();

emitDataSections(Streamer.get());

// Relocate .eh_frame to .eh_frame_old.
if (EHFrameSection.getObject() != nullptr) {
relocateEHFrameSection();
Expand Down Expand Up @@ -2401,16 +2392,22 @@ void RewriteInstance::emitDataSection(MCStreamer *Streamer, SectionRef Section,
SectionName = Name;
else
Section.getName(SectionName);

const auto SectionFlags = ELFSectionRef(Section).getFlags();
const auto SectionType = ELFSectionRef(Section).getType();
auto *ELFSection = BC->Ctx->getELFSection(SectionName,
ELF::SHT_PROGBITS,
ELF::SHF_WRITE | ELF::SHF_ALLOC);
SectionType,
SectionFlags);

StringRef SectionContents;
Section.getContents(SectionContents);

Streamer->SwitchSection(ELFSection);
Streamer->EmitValueToAlignment(Section.getAlignment());

DEBUG(dbgs() << "BOLT-DEBUG: emitting section " << SectionName << '\n');
DEBUG(dbgs() << "BOLT-DEBUG: emitting "
<< (SectionFlags & ELF::SHF_ALLOC ? "" : "non-")
<< "allocatable data section " << SectionName << '\n');

auto SRI = BC->SectionRelocations.find(Section);
if (SRI == BC->SectionRelocations.end()) {
Expand Down Expand Up @@ -2634,7 +2631,7 @@ void RewriteInstance::rewriteNoteSections() {
// Write section extension.
Address = SI.AllocAddress;
if (Address) {
DEBUG(dbgs() << "BOLT: " << (Size ? "appending" : "writing")
DEBUG(dbgs() << "BOLT-DEBUG: " << (Size ? "appending" : "writing")
<< " contents to section "
<< *SectionName << '\n');
OS.write(reinterpret_cast<const char *>(Address), SI.Size);
Expand Down
2 changes: 1 addition & 1 deletion bolt/RewriteInstance.h
Expand Up @@ -384,7 +384,7 @@ class RewriteInstance {
bool IsSimple);
private:

/// If we are updating debug info, these are the section we need to overwrite.
/// When updating debug info, these are the sections we overwrite.
static constexpr const char *DebugSectionsToOverwrite[] = {
".debug_aranges",
".debug_line",
Expand Down

0 comments on commit 34c8a7c

Please sign in to comment.