Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/LoadLinkableFile.cpp
213799 views
//===------- LoadLinkableFile.cpp -- Load relocatables and archives -------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/ExecutionEngine/Orc/LoadLinkableFile.h"910#include "llvm/ADT/ScopeExit.h"11#include "llvm/BinaryFormat/Magic.h"12#include "llvm/ExecutionEngine/Orc/MachO.h"13#include "llvm/Support/FileSystem.h"1415#define DEBUG_TYPE "orc"1617namespace llvm {18namespace orc {1920static Expected<std::unique_ptr<MemoryBuffer>>21checkCOFFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,22const Triple &TT) {23// TODO: Actually check the architecture of the file.24return std::move(Obj);25}2627static Expected<std::unique_ptr<MemoryBuffer>>28checkXCOFFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,29const Triple &TT) {30// TODO: Actually check the architecture of the file.31return std::move(Obj);32}3334static Expected<std::unique_ptr<MemoryBuffer>>35checkELFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT) {36// TODO: Actually check the architecture of the file.37return std::move(Obj);38}3940Expected<std::pair<std::unique_ptr<MemoryBuffer>, LinkableFileKind>>41loadLinkableFile(StringRef Path, const Triple &TT, LoadArchives LA,42std::optional<StringRef> IdentifierOverride) {43if (!IdentifierOverride)44IdentifierOverride = Path;4546Expected<sys::fs::file_t> FDOrErr =47sys::fs::openNativeFileForRead(Path, sys::fs::OF_None);48if (!FDOrErr)49return createFileError(Path, FDOrErr.takeError());50sys::fs::file_t FD = *FDOrErr;51auto CloseFile = make_scope_exit([&]() { sys::fs::closeFile(FD); });5253auto Buf =54MemoryBuffer::getOpenFile(FD, *IdentifierOverride, /*FileSize=*/-1);55if (!Buf)56return make_error<StringError>(57StringRef("Could not load object at path ") + Path, Buf.getError());5859std::optional<Triple::ObjectFormatType> RequireFormat;60if (TT.getObjectFormat() != Triple::UnknownObjectFormat)61RequireFormat = TT.getObjectFormat();6263switch (identify_magic((*Buf)->getBuffer())) {64case file_magic::archive:65if (LA != LoadArchives::Never)66return std::make_pair(std::move(*Buf), LinkableFileKind::Archive);67return make_error<StringError>(68Path + " does not contain a relocatable object file",69inconvertibleErrorCode());70case file_magic::coff_object:71if (LA == LoadArchives::Required)72return make_error<StringError>(Path + " does not contain an archive",73inconvertibleErrorCode());7475if (!RequireFormat || *RequireFormat == Triple::COFF) {76auto CheckedBuf = checkCOFFRelocatableObject(std::move(*Buf), TT);77if (!CheckedBuf)78return CheckedBuf.takeError();79return std::make_pair(std::move(*CheckedBuf),80LinkableFileKind::RelocatableObject);81}82break;83case file_magic::elf_relocatable:84if (LA == LoadArchives::Required)85return make_error<StringError>(Path + " does not contain an archive",86inconvertibleErrorCode());8788if (!RequireFormat || *RequireFormat == Triple::ELF) {89auto CheckedBuf = checkELFRelocatableObject(std::move(*Buf), TT);90if (!CheckedBuf)91return CheckedBuf.takeError();92return std::make_pair(std::move(*CheckedBuf),93LinkableFileKind::RelocatableObject);94}95break;96case file_magic::macho_object:97if (LA == LoadArchives::Required)98return make_error<StringError>(Path + " does not contain an archive",99inconvertibleErrorCode());100101if (!RequireFormat || *RequireFormat == Triple::MachO) {102auto CheckedBuf = checkMachORelocatableObject(std::move(*Buf), TT, false);103if (!CheckedBuf)104return CheckedBuf.takeError();105return std::make_pair(std::move(*CheckedBuf),106LinkableFileKind::RelocatableObject);107}108break;109case file_magic::macho_universal_binary:110if (!RequireFormat || *RequireFormat == Triple::MachO)111return loadLinkableSliceFromMachOUniversalBinary(112FD, std::move(*Buf), TT, LA, Path, *IdentifierOverride);113break;114case file_magic::xcoff_object_64:115if (!RequireFormat || *RequireFormat == Triple::XCOFF) {116auto CheckedBuf = checkXCOFFRelocatableObject(std::move(*Buf), TT);117if (!CheckedBuf)118return CheckedBuf.takeError();119return std::make_pair(std::move(*CheckedBuf),120LinkableFileKind::RelocatableObject);121}122break;123default:124break;125}126127return make_error<StringError>(128Path +129" does not contain a relocatable object file or archive compatible "130"with " +131TT.str(),132inconvertibleErrorCode());133}134135} // End namespace orc.136} // End namespace llvm.137138139