2019-03-14 18:49:40 -07:00
|
|
|
//===--- ExecutableFileMemoryManager.cpp ----------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "ExecutableFileMemoryManager.h"
|
|
|
|
|
#include "RewriteInstance.h"
|
|
|
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
2019-07-24 14:03:43 -07:00
|
|
|
#define DEBUG_TYPE "efmm"
|
2019-03-14 18:49:40 -07:00
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
using namespace object;
|
|
|
|
|
using namespace bolt;
|
|
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
|
|
namespace bolt {
|
|
|
|
|
|
|
|
|
|
uint8_t *ExecutableFileMemoryManager::allocateSection(intptr_t Size,
|
|
|
|
|
unsigned Alignment,
|
|
|
|
|
unsigned SectionID,
|
|
|
|
|
StringRef SectionName,
|
|
|
|
|
bool IsCode,
|
|
|
|
|
bool IsReadOnly) {
|
2019-04-26 15:30:12 -07:00
|
|
|
// Register a debug section as a note section.
|
2019-07-24 14:03:43 -07:00
|
|
|
if (!ObjectsLoaded && RewriteInstance::isDebugSection(SectionName)) {
|
2019-04-26 15:30:12 -07:00
|
|
|
uint8_t *DataCopy = new uint8_t[Size];
|
|
|
|
|
auto &Section = BC.registerOrUpdateNoteSection(SectionName,
|
|
|
|
|
DataCopy,
|
|
|
|
|
Size,
|
|
|
|
|
Alignment);
|
|
|
|
|
Section.setSectionID(SectionID);
|
|
|
|
|
assert(!Section.isAllocatable() && "note sections cannot be allocatable");
|
|
|
|
|
return DataCopy;
|
2019-03-14 18:49:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t *Ret;
|
|
|
|
|
if (IsCode) {
|
|
|
|
|
Ret = SectionMemoryManager::allocateCodeSection(Size, Alignment,
|
|
|
|
|
SectionID, SectionName);
|
|
|
|
|
} else {
|
2020-11-17 13:57:29 -08:00
|
|
|
Ret = SectionMemoryManager::allocateDataSection(Size, Alignment, SectionID,
|
|
|
|
|
SectionName, IsReadOnly);
|
2019-03-14 18:49:40 -07:00
|
|
|
}
|
|
|
|
|
|
2019-07-24 14:03:43 -07:00
|
|
|
SmallVector<char, 256> Buf;
|
2020-11-17 13:57:29 -08:00
|
|
|
if (ObjectsLoaded > 0) {
|
|
|
|
|
if (BC.isELF()) {
|
|
|
|
|
SectionName = (Twine(SectionName) + ".bolt.extra." + Twine(ObjectsLoaded))
|
|
|
|
|
.toStringRef(Buf);
|
|
|
|
|
} else if (BC.isMachO()) {
|
|
|
|
|
assert((SectionName == "__text" || SectionName == "__data" ||
|
|
|
|
|
SectionName == "__setup" || SectionName == "__cstring") &&
|
|
|
|
|
"Unexpected section in the instrumentation library");
|
|
|
|
|
SectionName = ("I" + Twine(SectionName)).toStringRef(Buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-24 14:03:43 -07:00
|
|
|
|
2020-11-17 13:57:29 -08:00
|
|
|
auto &Section = BC.registerOrUpdateSection(
|
|
|
|
|
SectionName, ELF::SHT_PROGBITS,
|
|
|
|
|
BinarySection::getFlags(IsReadOnly, IsCode, true), Ret, Size, Alignment);
|
2019-03-14 18:49:40 -07:00
|
|
|
Section.setSectionID(SectionID);
|
|
|
|
|
assert(Section.isAllocatable() &&
|
|
|
|
|
"verify that allocatable is marked as allocatable");
|
|
|
|
|
|
2020-02-18 09:20:17 -08:00
|
|
|
DEBUG(dbgs() << "BOLT: allocating "
|
2019-03-14 18:49:40 -07:00
|
|
|
<< (IsCode ? "code" : (IsReadOnly ? "read-only data" : "data"))
|
|
|
|
|
<< " section : " << SectionName
|
|
|
|
|
<< " with size " << Size << ", alignment " << Alignment
|
|
|
|
|
<< " at 0x" << Ret << ", ID = " << SectionID << "\n");
|
|
|
|
|
return Ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ExecutableFileMemoryManager::finalizeMemory(std::string *ErrMsg) {
|
|
|
|
|
DEBUG(dbgs() << "BOLT: finalizeMemory()\n");
|
2019-07-24 14:03:43 -07:00
|
|
|
++ObjectsLoaded;
|
2019-03-14 18:49:40 -07:00
|
|
|
return SectionMemoryManager::finalizeMemory(ErrMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExecutableFileMemoryManager::~ExecutableFileMemoryManager() { }
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|