LLVM 20.0.0git
LoadRelocatableObject.cpp
Go to the documentation of this file.
1//===----- LoadRelocatableObject.cpp -- Load relocatable object files -----===//
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
12
13#define DEBUG_TYPE "orc"
14
15namespace llvm {
16namespace orc {
17
18static Expected<std::unique_ptr<MemoryBuffer>>
19checkCOFFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,
20 const Triple &TT) {
21 // TODO: Actually check the architecture of the file.
22 return std::move(Obj);
23}
24
26checkELFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj, const Triple &TT) {
27 // TODO: Actually check the architecture of the file.
28 return std::move(Obj);
29}
30
33 auto Buf = MemoryBuffer::getFile(Path);
34 if (!Buf)
35 return createFileError(Path, Buf.getError());
36
37 std::optional<Triple::ObjectFormatType> RequireFormat;
38 if (TT.getObjectFormat() != Triple::UnknownObjectFormat)
39 RequireFormat = TT.getObjectFormat();
40
41 switch (identify_magic((*Buf)->getBuffer())) {
43 if (!RequireFormat || *RequireFormat == Triple::COFF)
44 return checkCOFFRelocatableObject(std::move(*Buf), TT);
45 break;
47 if (!RequireFormat || *RequireFormat == Triple::ELF)
48 return checkELFRelocatableObject(std::move(*Buf), TT);
49 break;
51 if (!RequireFormat || *RequireFormat == Triple::MachO)
52 return checkMachORelocatableObject(std::move(*Buf), TT, false);
53 break;
55 if (!RequireFormat || *RequireFormat == Triple::MachO)
57 std::move(*Buf), TT);
58 break;
59 default:
60 break;
61 }
62 return make_error<StringError>(
63 Path + " does not contain a relocatable object file compatible with " +
64 TT.str(),
66}
67
68} // End namespace orc.
69} // End namespace llvm.
Tagged union holding either a T or a Error.
Definition: Error.h:481
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ UnknownObjectFormat
Definition: Triple.h:298
Expected< std::unique_ptr< MemoryBuffer > > checkMachORelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT, bool ObjIsSlice)
Check that the given buffer contains a MachO object file compatible with the given triple.
Definition: MachO.cpp:57
Expected< std::unique_ptr< MemoryBuffer > > loadRelocatableObject(StringRef Path, const Triple &TT)
static Expected< std::unique_ptr< MemoryBuffer > > checkCOFFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
Expected< std::unique_ptr< MemoryBuffer > > loadMachORelocatableObjectFromUniversalBinary(StringRef UBPath, std::unique_ptr< MemoryBuffer > UBBuf, const Triple &TT)
Load a compatible relocatable object (if available) from a MachO universal binary.
Definition: MachO.cpp:112
static Expected< std::unique_ptr< MemoryBuffer > > checkELFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
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:1380
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
@ 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