Movatterモバイル変換


[0]ホーム

URL:


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
9#include "llvm/ExecutionEngine/Orc/LoadLinkableFile.h"
10
11#include "llvm/ADT/ScopeExit.h"
12#include "llvm/BinaryFormat/Magic.h"
13#include "llvm/ExecutionEngine/Orc/MachO.h"
14#include "llvm/Support/FileSystem.h"
15
16#define DEBUG_TYPE "orc"
17
18namespacellvm {
19namespaceorc {
20
21static Expected<std::unique_ptr<MemoryBuffer>>
22checkCOFFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,
23constTriple &TT) {
24// TODO: Actually check the architecture of the file.
25return std::move(Obj);
26}
27
28staticExpected<std::unique_ptr<MemoryBuffer>>
29checkELFRelocatableObject(std::unique_ptr<MemoryBuffer> Obj,constTriple &TT) {
30// TODO: Actually check the architecture of the file.
31return std::move(Obj);
32}
33
34Expected<std::pair<std::unique_ptr<MemoryBuffer>,LinkableFileKind>>
35loadLinkableFile(StringRef Path,constTriple &TT,LoadArchives LA,
36 std::optional<StringRef> IdentifierOverride) {
37if (!IdentifierOverride)
38 IdentifierOverride = Path;
39
40Expected<sys::fs::file_t> FDOrErr =
41sys::fs::openNativeFileForRead(Path,sys::fs::OF_None);
42if (!FDOrErr)
43returncreateFileError(Path, FDOrErr.takeError());
44sys::fs::file_t FD = *FDOrErr;
45auto CloseFile =make_scope_exit([&]() {sys::fs::closeFile(FD); });
46
47auto Buf =
48MemoryBuffer::getOpenFile(FD, *IdentifierOverride,/*FileSize=*/-1);
49if (!Buf)
50return make_error<StringError>(
51StringRef("Could not load object at path ") + Path, Buf.getError());
52
53 std::optional<Triple::ObjectFormatType> RequireFormat;
54if (TT.getObjectFormat() !=Triple::UnknownObjectFormat)
55 RequireFormat = TT.getObjectFormat();
56
57switch (identify_magic((*Buf)->getBuffer())) {
58casefile_magic::archive:
59if (LA !=LoadArchives::Never)
60return std::make_pair(std::move(*Buf),LinkableFileKind::Archive);
61return make_error<StringError>(
62 Path +" does not contain a relocatable object file",
63inconvertibleErrorCode());
64casefile_magic::coff_object:
65if (LA ==LoadArchives::Required)
66return make_error<StringError>(Path +" does not contain an archive",
67inconvertibleErrorCode());
68
69if (!RequireFormat || *RequireFormat ==Triple::COFF) {
70auto CheckedBuf =checkCOFFRelocatableObject(std::move(*Buf), TT);
71if (!CheckedBuf)
72return CheckedBuf.takeError();
73return std::make_pair(std::move(*CheckedBuf),
74LinkableFileKind::RelocatableObject);
75 }
76break;
77casefile_magic::elf_relocatable:
78if (LA ==LoadArchives::Required)
79return make_error<StringError>(Path +" does not contain an archive",
80inconvertibleErrorCode());
81
82if (!RequireFormat || *RequireFormat ==Triple::ELF) {
83auto CheckedBuf =checkELFRelocatableObject(std::move(*Buf), TT);
84if (!CheckedBuf)
85return CheckedBuf.takeError();
86return std::make_pair(std::move(*CheckedBuf),
87LinkableFileKind::RelocatableObject);
88 }
89break;
90casefile_magic::macho_object:
91if (LA ==LoadArchives::Required)
92return make_error<StringError>(Path +" does not contain an archive",
93inconvertibleErrorCode());
94
95if (!RequireFormat || *RequireFormat ==Triple::MachO) {
96auto CheckedBuf =checkMachORelocatableObject(std::move(*Buf), TT,false);
97if (!CheckedBuf)
98return CheckedBuf.takeError();
99return std::make_pair(std::move(*CheckedBuf),
100LinkableFileKind::RelocatableObject);
101 }
102break;
103casefile_magic::macho_universal_binary:
104if (!RequireFormat || *RequireFormat ==Triple::MachO)
105returnloadLinkableSliceFromMachOUniversalBinary(
106 FD, std::move(*Buf), TT, LA, Path, *IdentifierOverride);
107break;
108default:
109break;
110 }
111
112return make_error<StringError>(
113 Path +
114" does not contain a relocatable object file or archive compatible "
115"with " +
116 TT.str(),
117inconvertibleErrorCode());
118}
119
120}// End namespace orc.
121}// End namespace llvm.
MachO.h
FileSystem.h
LoadLinkableFile.h
Magic.h
ScopeExit.h
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
llvm::Expected
Tagged union holding either a T or a Error.
Definition:Error.h:481
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition:Error.h:608
llvm::MemoryBuffer::getOpenFile
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.
Definition:MemoryBuffer.cpp:550
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition:Triple.h:44
llvm::Triple::MachO
@ MachO
Definition:Triple.h:314
llvm::Triple::ELF
@ ELF
Definition:Triple.h:312
llvm::Triple::COFF
@ COFF
Definition:Triple.h:310
llvm::Triple::UnknownObjectFormat
@ UnknownObjectFormat
Definition:Triple.h:308
llvm::orc::loadLinkableFile
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.
Definition:LoadLinkableFile.cpp:35
llvm::orc::loadLinkableSliceFromMachOUniversalBinary
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
llvm::orc::LinkableFileKind
LinkableFileKind
Definition:LoadLinkableFile.h:28
llvm::orc::LinkableFileKind::RelocatableObject
@ RelocatableObject
llvm::orc::LinkableFileKind::Archive
@ Archive
llvm::orc::LoadArchives
LoadArchives
Definition:LoadLinkableFile.h:30
llvm::orc::Never
@ Never
Definition:LoadLinkableFile.h:31
llvm::orc::Required
@ Required
Definition:LoadLinkableFile.h:33
llvm::orc::checkCOFFRelocatableObject
static Expected< std::unique_ptr< MemoryBuffer > > checkCOFFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
Definition:LoadLinkableFile.cpp:22
llvm::orc::checkMachORelocatableObject
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
llvm::orc::checkELFRelocatableObject
static Expected< std::unique_ptr< MemoryBuffer > > checkELFRelocatableObject(std::unique_ptr< MemoryBuffer > Obj, const Triple &TT)
Definition:LoadLinkableFile.cpp:29
llvm::sys::fs::file_t
int file_t
Definition:FileSystem.h:55
llvm::sys::fs::closeFile
std::error_code closeFile(file_t &F)
Close the file object.
llvm::sys::fs::OF_None
@ OF_None
Definition:FileSystem.h:750
llvm::sys::fs::openNativeFileForRead
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.
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::identify_magic
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition:Magic.cpp:33
llvm::createFileError
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition:Error.h:1385
llvm::make_scope_exit
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition:ScopeExit.h:59
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition:Error.cpp:98
llvm::file_magic::elf_relocatable
@ elf_relocatable
ELF Relocatable object file.
Definition:Magic.h:27
llvm::file_magic::archive
@ archive
ar style archive file
Definition:Magic.h:25
llvm::file_magic::macho_universal_binary
@ macho_universal_binary
Mach-O universal binary.
Definition:Magic.h:43
llvm::file_magic::macho_object
@ macho_object
Mach-O Object file.
Definition:Magic.h:32
llvm::file_magic::coff_object
@ coff_object
COFF object file.
Definition:Magic.h:47

Generated on Fri Jul 18 2025 11:27:31 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp