LLVM 22.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
29checkXCOFFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,
30 const Triple &TT) {
31 // TODO: Actually check the architecture of the file.
32 return std::move(Obj);
33}
34
36checkELFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT) {
37 // TODO: Actually check the architecture of the file.
38 return std::move(Obj);
39}
40
43 std::optional<StringRef> IdentifierOverride) {
44 if (!IdentifierOverride)
45 IdentifierOverride = Path;
46
49 if (!FDOrErr)
50 return createFileError(Path, FDOrErr.takeError());
51 sys::fs::file_t FD = *FDOrErr;
52 auto CloseFile = make_scope_exit([&]() { sys::fs::closeFile(FD); });
53
54 auto Buf =
55 MemoryBuffer::getOpenFile(FD, *IdentifierOverride, /*FileSize=*/-1);
56 if (!Buf)
57 return make_error<StringError>(
58 StringRef("Could not load object at path ") + Path, Buf.getError());
59
60 std::optional<Triple::ObjectFormatType> RequireFormat;
61 if (TT.getObjectFormat() != Triple::UnknownObjectFormat)
62 RequireFormat = TT.getObjectFormat();
63
64 switch (identify_magic((*Buf)->getBuffer())) {
66 if (LA != LoadArchives::Never)
67 return std::make_pair(std::move(*Buf), LinkableFileKind::Archive);
68 return make_error<StringError>(
69 Path + " does not contain a relocatable object file",
72 if (LA == LoadArchives::Required)
73 return make_error<StringError>(Path + " does not contain an archive",
75
76 if (!RequireFormat || *RequireFormat == Triple::COFF) {
77 auto CheckedBuf = checkCOFFRelocatableObject(std::move(*Buf), TT);
78 if (!CheckedBuf)
79 return CheckedBuf.takeError();
80 return std::make_pair(std::move(*CheckedBuf),
82 }
83 break;
85 if (LA == LoadArchives::Required)
86 return make_error<StringError>(Path + " does not contain an archive",
88
89 if (!RequireFormat || *RequireFormat == Triple::ELF) {
90 auto CheckedBuf = checkELFRelocatableObject(std::move(*Buf), TT);
91 if (!CheckedBuf)
92 return CheckedBuf.takeError();
93 return std::make_pair(std::move(*CheckedBuf),
95 }
96 break;
98 if (LA == LoadArchives::Required)
99 return make_error<StringError>(Path + " does not contain an archive",
101
102 if (!RequireFormat || *RequireFormat == Triple::MachO) {
103 auto CheckedBuf = checkMachORelocatableObject(std::move(*Buf), TT, false);
104 if (!CheckedBuf)
105 return CheckedBuf.takeError();
106 return std::make_pair(std::move(*CheckedBuf),
108 }
109 break;
111 if (!RequireFormat || *RequireFormat == Triple::MachO)
113 FD, std::move(*Buf), TT, LA, Path, *IdentifierOverride);
114 break;
116 if (!RequireFormat || *RequireFormat == Triple::XCOFF) {
117 auto CheckedBuf = checkXCOFFRelocatableObject(std::move(*Buf), TT);
118 if (!CheckedBuf)
119 return CheckedBuf.takeError();
120 return std::make_pair(std::move(*CheckedBuf),
122 }
123 break;
124 default:
125 break;
126 }
127
128 return make_error<StringError>(
129 Path +
130 " does not contain a relocatable object file or archive compatible "
131 "with " +
132 TT.str(),
134}
135
136} // End namespace orc.
137} // 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:485
Error takeError()
Take ownership of the stored error.
Definition: Error.h:612
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:55
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
@ UnknownObjectFormat
Definition: Triple.h:315
LLVM_ABI 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.
LLVM_ABI 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:143
static Expected< std::unique_ptr< MemoryBuffer > > checkCOFFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
LLVM_ABI 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:59
static Expected< std::unique_ptr< MemoryBuffer > > checkELFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
static Expected< std::unique_ptr< MemoryBuffer > > checkXCOFFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI 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
LLVM_ABI 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:1399
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
LLVM_ABI 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:28
@ archive
ar style archive file
Definition: Magic.h:26
@ xcoff_object_64
64-bit XCOFF object file
Definition: Magic.h:53
@ macho_universal_binary
Mach-O universal binary.
Definition: Magic.h:44
@ macho_object
Mach-O Object file.
Definition: Magic.h:33
@ coff_object
COFF object file.
Definition: Magic.h:48