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 && (SectionName == ".strtab" || SectionName == ".symtab" || 42 SectionName == "" || SectionName.startswith(".rela."))) { 43 return SectionMemoryManager::allocateDataSection(Size, Alignment, SectionID, 44 SectionName, IsReadOnly); 45 } 46 47 uint8_t *Ret; 48 if (IsCode) { 49 Ret = SectionMemoryManager::allocateCodeSection(Size, Alignment, SectionID, 50 SectionName); 51 } else { 52 Ret = SectionMemoryManager::allocateDataSection(Size, Alignment, SectionID, 53 SectionName, IsReadOnly); 54 } 55 56 SmallVector<char, 256> Buf; 57 if (ObjectsLoaded > 0) { 58 if (BC.isELF()) { 59 SectionName = (Twine(SectionName) + ".bolt.extra." + Twine(ObjectsLoaded)) 60 .toStringRef(Buf); 61 } else if (BC.isMachO()) { 62 assert((SectionName == "__text" || SectionName == "__data" || 63 SectionName == "__fini" || SectionName == "__setup" || 64 SectionName == "__cstring" || SectionName == "__literal16") && 65 "Unexpected section in the instrumentation library"); 66 // Sections coming from the instrumentation runtime are prefixed with "I". 67 SectionName = ("I" + Twine(SectionName)).toStringRef(Buf); 68 } 69 } 70 71 BinarySection &Section = BC.registerOrUpdateSection( 72 SectionName, ELF::SHT_PROGBITS, 73 BinarySection::getFlags(IsReadOnly, IsCode, true), Ret, Size, Alignment); 74 Section.setSectionID(SectionID); 75 assert(Section.isAllocatable() && 76 "verify that allocatable is marked as allocatable"); 77 78 LLVM_DEBUG( 79 dbgs() << "BOLT: allocating " 80 << (IsCode ? "code" : (IsReadOnly ? "read-only data" : "data")) 81 << " section : " << SectionName << " with size " << Size 82 << ", alignment " << Alignment << " at 0x" << Ret 83 << ", ID = " << SectionID << "\n"); 84 return Ret; 85 } 86 87 bool ExecutableFileMemoryManager::finalizeMemory(std::string *ErrMsg) { 88 LLVM_DEBUG(dbgs() << "BOLT: finalizeMemory()\n"); 89 ++ObjectsLoaded; 90 return SectionMemoryManager::finalizeMemory(ErrMsg); 91 } 92 93 ExecutableFileMemoryManager::~ExecutableFileMemoryManager() {} 94 95 } // namespace bolt 96 97 } // namespace llvm 98