|
| 1 | +//===-------- GetDylibInterface.cpp - Get interface for real dylib --------===// |
| 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/GetDylibInterface.h" |
| 10 | + |
| 11 | +#include "llvm/BinaryFormat/Magic.h" |
| 12 | +#include "llvm/Object/MachO.h" |
| 13 | +#include "llvm/Object/MachOUniversal.h" |
| 14 | + |
| 15 | +#define DEBUG_TYPE "orc" |
| 16 | + |
| 17 | +namespace llvm::orc { |
| 18 | + |
| 19 | +Expected<SymbolNameSet> getDylibInterfaceFromDylib(ExecutionSession &ES, |
| 20 | + Twine Path) { |
| 21 | + auto CPUType = MachO::getCPUType(ES.getTargetTriple()); |
| 22 | + if (!CPUType) |
| 23 | + return CPUType.takeError(); |
| 24 | + |
| 25 | + auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple()); |
| 26 | + if (!CPUSubType) |
| 27 | + return CPUSubType.takeError(); |
| 28 | + |
| 29 | + auto Buf = MemoryBuffer::getFile(Path); |
| 30 | + if (!Buf) |
| 31 | + return createFileError(Path, Buf.getError()); |
| 32 | + |
| 33 | + auto SymFile = |
| 34 | + object::SymbolicFile::createSymbolicFile((*Buf)->getMemBufferRef()); |
| 35 | + if (!SymFile) |
| 36 | + return SymFile.takeError(); |
| 37 | + |
| 38 | + std::unique_ptr<object::MachOObjectFile> MachOFile; |
| 39 | + if (isa<object::MachOObjectFile>(**SymFile)) |
| 40 | + MachOFile.reset(dyn_cast<object::MachOObjectFile>(SymFile->release())); |
| 41 | + else if (auto *MachOUni = |
| 42 | + dyn_cast<object::MachOUniversalBinary>(SymFile->get())) { |
| 43 | + for (auto &O : MachOUni->objects()) { |
| 44 | + if (O.getCPUType() == *CPUType && O.getCPUSubType() == *CPUSubType) { |
| 45 | + if (auto Obj = O.getAsObjectFile()) |
| 46 | + MachOFile = std::move(*Obj); |
| 47 | + else |
| 48 | + return Obj.takeError(); |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + if (!MachOFile) |
| 53 | + return make_error<StringError>("MachO universal binary at " + Path + |
| 54 | + " does not contain a slice for " + |
| 55 | + ES.getTargetTriple().str(), |
| 56 | + inconvertibleErrorCode()); |
| 57 | + } else |
| 58 | + return make_error<StringError>("File at " + Path + " is not a MachO", |
| 59 | + inconvertibleErrorCode()); |
| 60 | + |
| 61 | + if (MachOFile->getHeader().filetype != MachO::MH_DYLIB) |
| 62 | + return make_error<StringError>("MachO at " + Path + " is not a dylib", |
| 63 | + inconvertibleErrorCode()); |
| 64 | + |
| 65 | + SymbolNameSet Symbols; |
| 66 | + for (auto &Sym : MachOFile->symbols()) { |
| 67 | + if (auto Name = Sym.getName()) |
| 68 | + Symbols.insert(ES.intern(*Name)); |
| 69 | + else |
| 70 | + return Name.takeError(); |
| 71 | + } |
| 72 | + |
| 73 | + return std::move(Symbols); |
| 74 | +} |
| 75 | + |
| 76 | +Expected<SymbolNameSet> getDylibInterfaceFromTapiFile(ExecutionSession &ES, |
| 77 | + Twine Path) { |
| 78 | + SymbolNameSet Symbols; |
| 79 | + |
| 80 | + auto TapiFileBuffer = MemoryBuffer::getFile(Path); |
| 81 | + if (!TapiFileBuffer) |
| 82 | + return createFileError(Path, TapiFileBuffer.getError()); |
| 83 | + |
| 84 | + auto Tapi = |
| 85 | + object::TapiUniversal::create((*TapiFileBuffer)->getMemBufferRef()); |
| 86 | + if (!Tapi) |
| 87 | + return Tapi.takeError(); |
| 88 | + |
| 89 | + auto CPUType = MachO::getCPUType(ES.getTargetTriple()); |
| 90 | + if (!CPUType) |
| 91 | + return CPUType.takeError(); |
| 92 | + |
| 93 | + auto CPUSubType = MachO::getCPUSubType(ES.getTargetTriple()); |
| 94 | + if (!CPUSubType) |
| 95 | + return CPUSubType.takeError(); |
| 96 | + |
| 97 | + auto &IF = (*Tapi)->getInterfaceFile(); |
| 98 | + auto Interface = |
| 99 | + IF.extract(MachO::getArchitectureFromCpuType(*CPUType, *CPUSubType)); |
| 100 | + if (!Interface) |
| 101 | + return Interface.takeError(); |
| 102 | + |
| 103 | + for (auto *Sym : (*Interface)->exports()) |
| 104 | + Symbols.insert(ES.intern(Sym->getName())); |
| 105 | + |
| 106 | + return Symbols; |
| 107 | +} |
| 108 | + |
| 109 | +Expected<SymbolNameSet> getDylibInterface(ExecutionSession &ES, Twine Path) { |
| 110 | + file_magic Magic; |
| 111 | + if (auto EC = identify_magic(Path, Magic)) |
| 112 | + return createFileError(Path, EC); |
| 113 | + |
| 114 | + SymbolNameSet Symbols; |
| 115 | + switch (Magic) { |
| 116 | + case file_magic::macho_dynamically_linked_shared_lib: |
| 117 | + return getDylibInterfaceFromDylib(ES, Path); |
| 118 | + case file_magic::tapi_file: |
| 119 | + return getDylibInterfaceFromTapiFile(ES, Path); |
| 120 | + default: |
| 121 | + return make_error<StringError>("Cannot get interface for " + Path + |
| 122 | + " unrecognized file type", |
| 123 | + inconvertibleErrorCode()); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +} // namespace llvm::orc |
0 commit comments