1 //===- bolt/Rewrite/ExecutableFileMemoryManager.cpp -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "bolt/Rewrite/ExecutableFileMemoryManager.h"
10 #include "bolt/Rewrite/RewriteInstance.h"
11
12 #undef DEBUG_TYPE
13 #define DEBUG_TYPE "efmm"
14
15 using namespace llvm;
16 using namespace object;
17 using namespace bolt;
18
19 namespace llvm {
20
21 namespace bolt {
22
allocateSection(intptr_t Size,unsigned Alignment,unsigned SectionID,StringRef SectionName,bool IsCode,bool IsReadOnly)23 uint8_t *ExecutableFileMemoryManager::allocateSection(intptr_t Size,
24 unsigned Alignment,
25 unsigned SectionID,
26 StringRef SectionName,
27 bool IsCode,
28 bool IsReadOnly) {
29 // Register a debug section as a note section.
30 if (!ObjectsLoaded && RewriteInstance::isDebugSection(SectionName)) {
31 uint8_t *DataCopy = new uint8_t[Size];
32 BinarySection &Section =
33 BC.registerOrUpdateNoteSection(SectionName, DataCopy, Size, Alignment);
34 Section.setSectionID(SectionID);
35 assert(!Section.isAllocatable() && "note sections cannot be allocatable");
36 return DataCopy;
37 }
38
39 if (!IsCode && (SectionName == ".strtab" || SectionName == ".symtab" ||
40 SectionName == "" || SectionName.startswith(".rela.")))
41 return SectionMemoryManager::allocateDataSection(Size, Alignment, SectionID,
42 SectionName, IsReadOnly);
43
44 uint8_t *Ret;
45 if (IsCode)
46 Ret = SectionMemoryManager::allocateCodeSection(Size, Alignment, SectionID,
47 SectionName);
48 else
49 Ret = SectionMemoryManager::allocateDataSection(Size, Alignment, SectionID,
50 SectionName, IsReadOnly);
51
52 SmallVector<char, 256> Buf;
53 if (ObjectsLoaded > 0) {
54 if (BC.isELF()) {
55 SectionName = (Twine(SectionName) + ".bolt.extra." + Twine(ObjectsLoaded))
56 .toStringRef(Buf);
57 } else if (BC.isMachO()) {
58 assert((SectionName == "__text" || SectionName == "__data" ||
59 SectionName == "__fini" || SectionName == "__setup" ||
60 SectionName == "__cstring" || SectionName == "__literal16") &&
61 "Unexpected section in the instrumentation library");
62 // Sections coming from the instrumentation runtime are prefixed with "I".
63 SectionName = ("I" + Twine(SectionName)).toStringRef(Buf);
64 }
65 }
66
67 BinarySection &Section = BC.registerOrUpdateSection(
68 SectionName, ELF::SHT_PROGBITS,
69 BinarySection::getFlags(IsReadOnly, IsCode, true), Ret, Size, Alignment);
70 Section.setSectionID(SectionID);
71 assert(Section.isAllocatable() &&
72 "verify that allocatable is marked as allocatable");
73
74 LLVM_DEBUG(
75 dbgs() << "BOLT: allocating "
76 << (IsCode ? "code" : (IsReadOnly ? "read-only data" : "data"))
77 << " section : " << SectionName << " with size " << Size
78 << ", alignment " << Alignment << " at 0x" << Ret
79 << ", ID = " << SectionID << "\n");
80 return Ret;
81 }
82
finalizeMemory(std::string * ErrMsg)83 bool ExecutableFileMemoryManager::finalizeMemory(std::string *ErrMsg) {
84 LLVM_DEBUG(dbgs() << "BOLT: finalizeMemory()\n");
85 ++ObjectsLoaded;
86 return SectionMemoryManager::finalizeMemory(ErrMsg);
87 }
88
~ExecutableFileMemoryManager()89 ExecutableFileMemoryManager::~ExecutableFileMemoryManager() {}
90
91 } // namespace bolt
92
93 } // namespace llvm
94