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