123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- //===------ ObjectFileInterface.cpp - MU interface utils for objects ------===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- #include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
- #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
- #include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
- #include "llvm/Object/ELFObjectFile.h"
- #include "llvm/Object/MachO.h"
- #include "llvm/Object/ObjectFile.h"
- #include "llvm/Support/Debug.h"
- #define DEBUG_TYPE "orc"
- namespace llvm {
- namespace orc {
- void addInitSymbol(MaterializationUnit::Interface &I, ExecutionSession &ES,
- StringRef ObjFileName) {
- assert(!I.InitSymbol && "I already has an init symbol");
- size_t Counter = 0;
- do {
- std::string InitSymString;
- raw_string_ostream(InitSymString)
- << "$." << ObjFileName << ".__inits." << Counter++;
- I.InitSymbol = ES.intern(InitSymString);
- } while (I.SymbolFlags.count(I.InitSymbol));
- I.SymbolFlags[I.InitSymbol] = JITSymbolFlags::MaterializationSideEffectsOnly;
- }
- static Expected<MaterializationUnit::Interface>
- getMachOObjectFileSymbolInfo(ExecutionSession &ES,
- const object::MachOObjectFile &Obj) {
- MaterializationUnit::Interface I;
- for (auto &Sym : Obj.symbols()) {
- Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
- if (!SymFlagsOrErr)
- // TODO: Test this error.
- return SymFlagsOrErr.takeError();
- // Skip symbols not defined in this object file.
- if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
- continue;
- // Skip symbols that are not global.
- if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
- continue;
- // Skip symbols that have type SF_File.
- if (auto SymType = Sym.getType()) {
- if (*SymType == object::SymbolRef::ST_File)
- continue;
- } else
- return SymType.takeError();
- auto Name = Sym.getName();
- if (!Name)
- return Name.takeError();
- auto InternedName = ES.intern(*Name);
- auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
- if (!SymFlags)
- return SymFlags.takeError();
- // Strip the 'exported' flag from MachO linker-private symbols.
- if (Name->startswith("l"))
- *SymFlags &= ~JITSymbolFlags::Exported;
- I.SymbolFlags[InternedName] = std::move(*SymFlags);
- }
- for (auto &Sec : Obj.sections()) {
- auto SecType = Obj.getSectionType(Sec);
- if ((SecType & MachO::SECTION_TYPE) == MachO::S_MOD_INIT_FUNC_POINTERS) {
- addInitSymbol(I, ES, Obj.getFileName());
- break;
- }
- auto SegName = Obj.getSectionFinalSegmentName(Sec.getRawDataRefImpl());
- auto SecName = cantFail(Obj.getSectionName(Sec.getRawDataRefImpl()));
- if (MachOPlatform::isInitializerSection(SegName, SecName)) {
- addInitSymbol(I, ES, Obj.getFileName());
- break;
- }
- }
- return I;
- }
- static Expected<MaterializationUnit::Interface>
- getELFObjectFileSymbolInfo(ExecutionSession &ES,
- const object::ELFObjectFileBase &Obj) {
- MaterializationUnit::Interface I;
- for (auto &Sym : Obj.symbols()) {
- Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
- if (!SymFlagsOrErr)
- // TODO: Test this error.
- return SymFlagsOrErr.takeError();
- // Skip symbols not defined in this object file.
- if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
- continue;
- // Skip symbols that are not global.
- if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
- continue;
- // Skip symbols that have type SF_File.
- if (auto SymType = Sym.getType()) {
- if (*SymType == object::SymbolRef::ST_File)
- continue;
- } else
- return SymType.takeError();
- auto Name = Sym.getName();
- if (!Name)
- return Name.takeError();
- auto InternedName = ES.intern(*Name);
- auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
- if (!SymFlags)
- return SymFlags.takeError();
- // ELF STB_GNU_UNIQUE should map to Weak for ORC.
- if (Sym.getBinding() == ELF::STB_GNU_UNIQUE)
- *SymFlags |= JITSymbolFlags::Weak;
- I.SymbolFlags[InternedName] = std::move(*SymFlags);
- }
- SymbolStringPtr InitSymbol;
- for (auto &Sec : Obj.sections()) {
- if (auto SecName = Sec.getName()) {
- if (ELFNixPlatform::isInitializerSection(*SecName)) {
- addInitSymbol(I, ES, Obj.getFileName());
- break;
- }
- }
- }
- return I;
- }
- Expected<MaterializationUnit::Interface>
- getGenericObjectFileSymbolInfo(ExecutionSession &ES,
- const object::ObjectFile &Obj) {
- MaterializationUnit::Interface I;
- for (auto &Sym : Obj.symbols()) {
- Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
- if (!SymFlagsOrErr)
- // TODO: Test this error.
- return SymFlagsOrErr.takeError();
- // Skip symbols not defined in this object file.
- if (*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined)
- continue;
- // Skip symbols that are not global.
- if (!(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
- continue;
- // Skip symbols that have type SF_File.
- if (auto SymType = Sym.getType()) {
- if (*SymType == object::SymbolRef::ST_File)
- continue;
- } else
- return SymType.takeError();
- auto Name = Sym.getName();
- if (!Name)
- return Name.takeError();
- auto InternedName = ES.intern(*Name);
- auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
- if (!SymFlags)
- return SymFlags.takeError();
- I.SymbolFlags[InternedName] = std::move(*SymFlags);
- }
- return I;
- }
- Expected<MaterializationUnit::Interface>
- getObjectFileInterface(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
- auto Obj = object::ObjectFile::createObjectFile(ObjBuffer);
- if (!Obj)
- return Obj.takeError();
- if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Obj->get()))
- return getMachOObjectFileSymbolInfo(ES, *MachOObj);
- else if (auto *ELFObj = dyn_cast<object::ELFObjectFileBase>(Obj->get()))
- return getELFObjectFileSymbolInfo(ES, *ELFObj);
- return getGenericObjectFileSymbolInfo(ES, **Obj);
- }
- } // End namespace orc.
- } // End namespace llvm.
|