| 1 | //===----------- Mangling.cpp -- Name Mangling Utilities for ORC ----------===// |
| 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 "llvm/ExecutionEngine/Orc/Mangling.h" |
| 10 | #include "llvm/ADT/StringExtras.h" |
| 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/IR/Constants.h" |
| 13 | #include "llvm/IR/Mangler.h" |
| 14 | #include "llvm/Support/ErrorHandling.h" |
| 15 | |
| 16 | #define DEBUG_TYPE "orc" |
| 17 | |
| 18 | namespace llvm::orc { |
| 19 | |
| 20 | MangleAndInterner::MangleAndInterner(ExecutionSession &ES, StringRef ABIName) |
| 21 | : ES(ES), Mode(fromTriple(TT: ES.getTargetTriple(), ABIName)) {} |
| 22 | |
| 23 | MangleAndInterner::MangleAndInterner(ExecutionSession &ES, ManglingMode Mode) |
| 24 | : ES(ES), Mode(Mode) {} |
| 25 | |
| 26 | MangleAndInterner::MangleAndInterner(ExecutionSession &ES, const DataLayout &DL) |
| 27 | : ES(ES), Mode(fromDataLayout(DL)) {} |
| 28 | |
| 29 | // TODO: The prefixing rules below, and the mangling-mode derivation in |
| 30 | // fromDataLayoutStr, duplicate logic that already lives in llvm::Mangler |
| 31 | // (getNameWithPrefix) and DataLayout (ManglingModeT and its "m:" spec |
| 32 | // parsing). They are re-implemented here only because those APIs require a |
| 33 | // full DataLayout, which this class is meant to work without. We should |
| 34 | // refactor to have one copy of this code, probably best defined in |
| 35 | // TargetParser, shared between all users. |
| 36 | SymbolStringPtr MangleAndInterner::operator()(StringRef Name) { |
| 37 | if (Name.empty()) |
| 38 | return ES.intern(SymName: Name); |
| 39 | |
| 40 | if (Name.front() == '\1') |
| 41 | return ES.intern(SymName: Name.substr(Start: 1)); |
| 42 | |
| 43 | if (Name[0] == '?' && doNotMangleLeadingQuestionMark()) |
| 44 | return ES.intern(SymName: Name); |
| 45 | |
| 46 | if (Mode == ManglingMode::MachO || Mode == ManglingMode::WinCOFFX86) |
| 47 | return ES.intern(SymName: ("_" + Name).str()); |
| 48 | |
| 49 | return ES.intern(SymName: Name); |
| 50 | } |
| 51 | |
| 52 | MangleAndInterner::ManglingMode |
| 53 | MangleAndInterner::fromDataLayoutStr(StringRef DLStr) { |
| 54 | for (StringRef Spec : split(Str: DLStr, Separator: '-')) { |
| 55 | if (!Spec.starts_with(Prefix: "m:" )) |
| 56 | continue; |
| 57 | auto ModeStr = Spec.drop_front(N: 2); |
| 58 | assert(ModeStr.size() == 1 && |
| 59 | "invalid data layout string from Triple::computeDataLayout" ); |
| 60 | switch (ModeStr[0]) { |
| 61 | case 'e': |
| 62 | return ManglingMode::ELF; |
| 63 | case 'l': |
| 64 | return ManglingMode::GOFF; |
| 65 | case 'o': |
| 66 | return ManglingMode::MachO; |
| 67 | case 'm': |
| 68 | return ManglingMode::Mips; |
| 69 | case 'w': |
| 70 | return ManglingMode::WinCOFF; |
| 71 | case 'x': |
| 72 | return ManglingMode::WinCOFFX86; |
| 73 | case 'a': |
| 74 | return ManglingMode::XCOFF; |
| 75 | default: |
| 76 | llvm_unreachable("Invalid mangling mode from Triple::computeDataLayout" ); |
| 77 | } |
| 78 | } |
| 79 | return ManglingMode::None; |
| 80 | } |
| 81 | |
| 82 | MangleAndInterner::ManglingMode |
| 83 | MangleAndInterner::fromTriple(const Triple &TT, StringRef ABIName) { |
| 84 | return fromDataLayoutStr(DLStr: TT.computeDataLayout(ABIName)); |
| 85 | } |
| 86 | |
| 87 | MangleAndInterner::ManglingMode |
| 88 | MangleAndInterner::fromDataLayout(const DataLayout &DL) { |
| 89 | return fromDataLayoutStr(DLStr: DL.getStringRepresentation()); |
| 90 | } |
| 91 | |
| 92 | bool MangleAndInterner::doNotMangleLeadingQuestionMark() const { |
| 93 | return Mode == ManglingMode::WinCOFF || Mode == ManglingMode::WinCOFFX86; |
| 94 | } |
| 95 | |
| 96 | void IRSymbolMapper::add(ExecutionSession &ES, const ManglingOptions &MO, |
| 97 | ArrayRef<GlobalValue *> GVs, |
| 98 | SymbolFlagsMap &SymbolFlags, |
| 99 | SymbolNameToDefinitionMap *SymbolToDefinition) { |
| 100 | if (GVs.empty()) |
| 101 | return; |
| 102 | |
| 103 | MangleAndInterner Mangle(ES, GVs[0]->getDataLayout()); |
| 104 | for (auto *G : GVs) { |
| 105 | assert(G && "GVs cannot contain null elements" ); |
| 106 | if (!G->hasName() || G->isDeclaration() || G->hasLocalLinkage() || |
| 107 | G->hasAvailableExternallyLinkage() || G->hasAppendingLinkage()) |
| 108 | continue; |
| 109 | |
| 110 | if (G->isThreadLocal() && MO.EmulatedTLS) { |
| 111 | auto *GV = cast<GlobalVariable>(Val: G); |
| 112 | |
| 113 | auto Flags = JITSymbolFlags::fromGlobalValue(GV: *GV); |
| 114 | |
| 115 | auto EmuTLSV = Mangle(("__emutls_v." + GV->getName()).str()); |
| 116 | SymbolFlags[EmuTLSV] = Flags; |
| 117 | if (SymbolToDefinition) |
| 118 | (*SymbolToDefinition)[EmuTLSV] = GV; |
| 119 | |
| 120 | // If this GV has a non-zero initializer we'll need to emit an |
| 121 | // __emutls.t symbol too. |
| 122 | if (GV->hasInitializer()) { |
| 123 | const auto *InitVal = GV->getInitializer(); |
| 124 | |
| 125 | // Skip zero-initializers. |
| 126 | if (isa<ConstantAggregateZero>(Val: InitVal)) |
| 127 | continue; |
| 128 | const auto *InitIntValue = dyn_cast<ConstantInt>(Val: InitVal); |
| 129 | if (InitIntValue && InitIntValue->isZero()) |
| 130 | continue; |
| 131 | |
| 132 | auto EmuTLST = Mangle(("__emutls_t." + GV->getName()).str()); |
| 133 | SymbolFlags[EmuTLST] = Flags; |
| 134 | if (SymbolToDefinition) |
| 135 | (*SymbolToDefinition)[EmuTLST] = GV; |
| 136 | } |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | // Otherwise we just need a normal linker mangling. |
| 141 | auto MangledName = Mangle(G->getName()); |
| 142 | SymbolFlags[MangledName] = JITSymbolFlags::fromGlobalValue(GV: *G); |
| 143 | if (SymbolToDefinition) |
| 144 | (*SymbolToDefinition)[MangledName] = G; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } // namespace llvm::orc |
| 149 | |