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