LLVM 20.0.0git
LoadLinkableFile.cpp
Go to the documentation of this file.
1//===------- LoadLinkableFile.cpp -- Load relocatables and archives -------===//
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
10
11#include "llvm/ADT/ScopeExit.h"
15
16#define DEBUG_TYPE "orc"
17
18namespace llvm {
19namespace orc {
20
21static Expected<std::unique_ptr<MemoryBuffer>>
22checkCOFFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,
23 const Triple &TT) {
24 // TODO: Actually check the architecture of the file.
25 return std::move(Obj);
26}
27
29checkELFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT) {
30 // TODO: Actually check the architecture of the file.
31 return std::move(Obj);
32}
33
36 std::optional<StringRef> IdentifierOverride) {
37 if (!IdentifierOverride)
38 IdentifierOverride = Path;
39
42 if (!FDOrErr)
43 return createFileError(Path, FDOrErr.takeError());
44 sys::fs::file_t FD = *FDOrErr;
45 auto CloseFile = make_scope_exit([&]() { sys::fs::closeFile(FD); });
46
47 auto Buf =
48 MemoryBuffer::getOpenFile(FD, *IdentifierOverride, /*FileSize=*/-1);
49 if (!Buf)
50 return make_error<StringError>(
51 StringRef("Could not load object at path ") + Path, Buf.getError());
52
53 std::optional<Triple::ObjectFormatType> RequireFormat;
54 if (TT.getObjectFormat() != Triple::UnknownObjectFormat)
55 RequireFormat = TT.getObjectFormat();
56
57 switch (identify_magic((*Buf)->getBuffer())) {
59 if (LA != LoadArchives::Never)
60 return std::make_pair(std::move(*Buf), LinkableFileKind::Archive);
61 return make_error<StringError>(
62 Path + " does not contain a relocatable object file",
65 if (LA == LoadArchives::Required)
66 return make_error<StringError>(Path + " does not contain an archive",
68
69 if (!RequireFormat || *RequireFormat == Triple::COFF) {
70 auto CheckedBuf = checkCOFFRelocatableObject(std::move(*Buf), TT);
71 if (!CheckedBuf)
72 return CheckedBuf.takeError();
73 return std::make_pair(std::move(*CheckedBuf),
75 }
76 break;
78 if (LA == LoadArchives::Required)
79 return make_error<StringError>(Path + " does not contain an archive",
81
82 if (!RequireFormat || *RequireFormat == Triple::ELF) {
83 auto CheckedBuf = checkELFRelocatableObject(std::move(*Buf), TT);
84 if (!CheckedBuf)
85 return CheckedBuf.takeError();
86 return std::make_pair(std::move(*CheckedBuf),
88 }
89 break;
91 if (LA == LoadArchives::Required)
92 return make_error<StringError>(Path + " does not contain an archive",
94
95 if (!RequireFormat || *RequireFormat == Triple::MachO) {
96 auto CheckedBuf = checkMachORelocatableObject(std::move(*Buf), TT, false);
97 if (!CheckedBuf)
98 return CheckedBuf.takeError();
99 return std::make_pair(std::move(*CheckedBuf),
101 }
102 break;
104 if (!RequireFormat || *RequireFormat == Triple::MachO)
106 FD, std::move(*Buf), TT, LA, Path, *IdentifierOverride);
107 break;
108 default:
109 break;
110 }
111
112 return make_error<StringError>(
113 Path +
114 " does not contain a relocatable object file or archive compatible "
115 "with " +
116 TT.str(),
118}
119
120} // End namespace orc.
121} // End namespace llvm.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ UnknownObjectFormat
Definition: Triple.h:308
Expected< std::pair< std::unique_ptr< MemoryBuffer >, LinkableFileKind > > loadLinkableFile(StringRef Path, const Triple &TT, LoadArchives LA, std::optional< StringRef > IdentifierOverride=std::nullopt)
Create a MemoryBuffer covering the "linkable" part of the given path.
Expected< std::pair< std::unique_ptr< MemoryBuffer >, LinkableFileKind > > loadLinkableSliceFromMachOUniversalBinary(sys::fs::file_t FD, std::unique_ptr< MemoryBuffer > UBBuf, const Triple &TT, LoadArchives LA, StringRef UBPath, StringRef Identifier)
Load a compatible relocatable object (if available) from a MachO universal binary.
Definition: MachO.cpp:142
static Expected< std::unique_ptr< MemoryBuffer > > checkCOFFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
Error checkMachORelocatableObject(MemoryBufferRef Obj, const Triple &TT, bool ObjIsSlice)
Check that the given buffer contains a MachO object file compatible with the given triple.
Definition: MachO.cpp:58
static Expected< std::unique_ptr< MemoryBuffer > > checkELFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
std::error_code closeFile(file_t &F)
Close the file object.
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition: Error.h:1385
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
@ elf_relocatable
ELF Relocatable object file.
Definition: Magic.h:27
@ archive
ar style archive file
Definition: Magic.h:25
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:43
@ macho_object
Mach-O Object file.
Definition: Magic.h:32
@ coff_object
COFF object file.
Definition: Magic.h:47