2021-12-22 02:21:41 +08:00
|
|
|
//===- bolt/Rewrite/ExecutableFileMemoryManager.cpp -----------------------===//
|
2019-03-15 09:49:40 +08:00
|
|
|
//
|
2021-03-16 09:04:18 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-03-15 09:49:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-10-09 02:47:10 +08:00
|
|
|
#include "bolt/Rewrite/ExecutableFileMemoryManager.h"
|
|
|
|
#include "bolt/Rewrite/RewriteInstance.h"
|
2019-03-15 09:49:40 +08:00
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
2019-07-25 05:03:43 +08:00
|
|
|
#define DEBUG_TYPE "efmm"
|
2019-03-15 09:49:40 +08: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-27 06:30:12 +08:00
|
|
|
// Register a debug section as a note section.
|
2019-07-25 05:03:43 +08:00
|
|
|
if (!ObjectsLoaded && RewriteInstance::isDebugSection(SectionName)) {
|
2019-04-27 06:30:12 +08:00
|
|
|
uint8_t *DataCopy = new uint8_t[Size];
|
2021-04-08 15:19:26 +08:00
|
|
|
BinarySection &Section =
|
|
|
|
BC.registerOrUpdateNoteSection(SectionName, DataCopy, Size, Alignment);
|
2019-04-27 06:30:12 +08:00
|
|
|
Section.setSectionID(SectionID);
|
|
|
|
assert(!Section.isAllocatable() && "note sections cannot be allocatable");
|
|
|
|
return DataCopy;
|
2019-03-15 09:49:40 +08:00
|
|
|
}
|
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
if (!IsCode && (SectionName == ".strtab" || SectionName == ".symtab" ||
|
2021-12-24 04:38:33 +08:00
|
|
|
SectionName == "" || SectionName.startswith(".rela.")))
|
2020-12-02 08:29:39 +08:00
|
|
|
return SectionMemoryManager::allocateDataSection(Size, Alignment, SectionID,
|
|
|
|
SectionName, IsReadOnly);
|
|
|
|
|
2019-03-15 09:49:40 +08:00
|
|
|
uint8_t *Ret;
|
2021-12-24 04:38:33 +08:00
|
|
|
if (IsCode)
|
2021-12-15 08:52:51 +08:00
|
|
|
Ret = SectionMemoryManager::allocateCodeSection(Size, Alignment, SectionID,
|
|
|
|
SectionName);
|
2021-12-24 04:38:33 +08:00
|
|
|
else
|
2020-11-18 05:57:29 +08:00
|
|
|
Ret = SectionMemoryManager::allocateDataSection(Size, Alignment, SectionID,
|
|
|
|
SectionName, IsReadOnly);
|
2019-03-15 09:49:40 +08:00
|
|
|
|
2019-07-25 05:03:43 +08:00
|
|
|
SmallVector<char, 256> Buf;
|
2020-11-18 05: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" ||
|
2020-11-20 10:18:28 +08:00
|
|
|
SectionName == "__fini" || SectionName == "__setup" ||
|
2021-01-29 04:04:46 +08:00
|
|
|
SectionName == "__cstring" || SectionName == "__literal16") &&
|
2020-11-18 05:57:29 +08:00
|
|
|
"Unexpected section in the instrumentation library");
|
2021-01-29 04:04:46 +08:00
|
|
|
// Sections coming from the instrumentation runtime are prefixed with "I".
|
2020-11-18 05:57:29 +08:00
|
|
|
SectionName = ("I" + Twine(SectionName)).toStringRef(Buf);
|
|
|
|
}
|
|
|
|
}
|
2019-07-25 05:03:43 +08:00
|
|
|
|
2021-04-08 15:19:26 +08:00
|
|
|
BinarySection &Section = BC.registerOrUpdateSection(
|
2020-11-18 05:57:29 +08:00
|
|
|
SectionName, ELF::SHT_PROGBITS,
|
|
|
|
BinarySection::getFlags(IsReadOnly, IsCode, true), Ret, Size, Alignment);
|
2019-03-15 09:49:40 +08:00
|
|
|
Section.setSectionID(SectionID);
|
|
|
|
assert(Section.isAllocatable() &&
|
|
|
|
"verify that allocatable is marked as allocatable");
|
|
|
|
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(
|
|
|
|
dbgs() << "BOLT: allocating "
|
|
|
|
<< (IsCode ? "code" : (IsReadOnly ? "read-only data" : "data"))
|
|
|
|
<< " section : " << SectionName << " with size " << Size
|
|
|
|
<< ", alignment " << Alignment << " at 0x" << Ret
|
|
|
|
<< ", ID = " << SectionID << "\n");
|
2019-03-15 09:49:40 +08:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ExecutableFileMemoryManager::finalizeMemory(std::string *ErrMsg) {
|
2020-12-02 08:29:39 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "BOLT: finalizeMemory()\n");
|
2019-07-25 05:03:43 +08:00
|
|
|
++ObjectsLoaded;
|
2019-03-15 09:49:40 +08:00
|
|
|
return SectionMemoryManager::finalizeMemory(ErrMsg);
|
|
|
|
}
|
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
ExecutableFileMemoryManager::~ExecutableFileMemoryManager() {}
|
2019-03-15 09:49:40 +08:00
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
} // namespace bolt
|
2019-03-15 09:49:40 +08:00
|
|
|
|
2021-12-15 08:52:51 +08:00
|
|
|
} // namespace llvm
|