llvm-project
122 строки · 4.5 Кб
1//===- Binary.cpp - A generic binary file ---------------------------------===//
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// This file defines the Binary class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Object/Binary.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/BinaryFormat/Magic.h"
16#include "llvm/Object/Archive.h"
17#include "llvm/Object/Error.h"
18#include "llvm/Object/MachOUniversal.h"
19#include "llvm/Object/Minidump.h"
20#include "llvm/Object/ObjectFile.h"
21#include "llvm/Object/OffloadBinary.h"
22#include "llvm/Object/TapiUniversal.h"
23#include "llvm/Object/WindowsResource.h"
24#include "llvm/Support/Error.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/ErrorOr.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include <memory>
29#include <system_error>
30
31using namespace llvm;
32using namespace object;
33
34Binary::~Binary() = default;
35
36Binary::Binary(unsigned int Type, MemoryBufferRef Source)
37: TypeID(Type), Data(Source) {}
38
39StringRef Binary::getData() const { return Data.getBuffer(); }
40
41StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
42
43MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
44
45Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
46LLVMContext *Context,
47bool InitContent) {
48file_magic Type = identify_magic(Buffer.getBuffer());
49
50switch (Type) {
51case file_magic::archive:
52return Archive::create(Buffer);
53case file_magic::elf:
54case file_magic::elf_relocatable:
55case file_magic::elf_executable:
56case file_magic::elf_shared_object:
57case file_magic::elf_core:
58case file_magic::goff_object:
59case file_magic::macho_object:
60case file_magic::macho_executable:
61case file_magic::macho_fixed_virtual_memory_shared_lib:
62case file_magic::macho_core:
63case file_magic::macho_preload_executable:
64case file_magic::macho_dynamically_linked_shared_lib:
65case file_magic::macho_dynamic_linker:
66case file_magic::macho_bundle:
67case file_magic::macho_dynamically_linked_shared_lib_stub:
68case file_magic::macho_dsym_companion:
69case file_magic::macho_kext_bundle:
70case file_magic::macho_file_set:
71case file_magic::coff_object:
72case file_magic::coff_import_library:
73case file_magic::pecoff_executable:
74case file_magic::bitcode:
75case file_magic::xcoff_object_32:
76case file_magic::xcoff_object_64:
77case file_magic::wasm_object:
78return ObjectFile::createSymbolicFile(Buffer, Type, Context, InitContent);
79case file_magic::macho_universal_binary:
80return MachOUniversalBinary::create(Buffer);
81case file_magic::windows_resource:
82return WindowsResource::createWindowsResource(Buffer);
83case file_magic::pdb:
84// PDB does not support the Binary interface.
85return errorCodeToError(object_error::invalid_file_type);
86case file_magic::unknown:
87case file_magic::clang_ast:
88case file_magic::cuda_fatbinary:
89case file_magic::coff_cl_gl_object:
90case file_magic::dxcontainer_object:
91case file_magic::offload_bundle:
92case file_magic::offload_bundle_compressed:
93case file_magic::spirv_object:
94// Unrecognized object file format.
95return errorCodeToError(object_error::invalid_file_type);
96case file_magic::offload_binary:
97return OffloadBinary::create(Buffer);
98case file_magic::minidump:
99return MinidumpFile::create(Buffer);
100case file_magic::tapi_file:
101return TapiUniversal::create(Buffer);
102}
103llvm_unreachable("Unexpected Binary File Type");
104}
105
106Expected<OwningBinary<Binary>>
107object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) {
108ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
109MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false,
110/*RequiresNullTerminator=*/false);
111if (std::error_code EC = FileOrErr.getError())
112return errorCodeToError(EC);
113std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
114
115Expected<std::unique_ptr<Binary>> BinOrErr =
116createBinary(Buffer->getMemBufferRef(), Context, InitContent);
117if (!BinOrErr)
118return BinOrErr.takeError();
119std::unique_ptr<Binary> &Bin = BinOrErr.get();
120
121return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
122}
123