xref: /freebsd/contrib/llvm-project/llvm/lib/Object/Binary.cpp (revision 2b8331622f0b212cf3bb4fc4914a501e5321d506)
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 
31 using namespace llvm;
32 using namespace object;
33 
34 Binary::~Binary() = default;
35 
36 Binary::Binary(unsigned int Type, MemoryBufferRef Source)
37     : TypeID(Type), Data(Source) {}
38 
39 StringRef Binary::getData() const { return Data.getBuffer(); }
40 
41 StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
42 
43 MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
44 
45 Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
46                                                        LLVMContext *Context,
47                                                        bool InitContent) {
48   file_magic Type = identify_magic(Buffer.getBuffer());
49 
50   switch (Type) {
51   case file_magic::archive:
52     return Archive::create(Buffer);
53   case file_magic::elf:
54   case file_magic::elf_relocatable:
55   case file_magic::elf_executable:
56   case file_magic::elf_shared_object:
57   case file_magic::elf_core:
58   case file_magic::goff_object:
59   case file_magic::macho_object:
60   case file_magic::macho_executable:
61   case file_magic::macho_fixed_virtual_memory_shared_lib:
62   case file_magic::macho_core:
63   case file_magic::macho_preload_executable:
64   case file_magic::macho_dynamically_linked_shared_lib:
65   case file_magic::macho_dynamic_linker:
66   case file_magic::macho_bundle:
67   case file_magic::macho_dynamically_linked_shared_lib_stub:
68   case file_magic::macho_dsym_companion:
69   case file_magic::macho_kext_bundle:
70   case file_magic::coff_object:
71   case file_magic::coff_import_library:
72   case file_magic::pecoff_executable:
73   case file_magic::bitcode:
74   case file_magic::xcoff_object_32:
75   case file_magic::xcoff_object_64:
76   case file_magic::wasm_object:
77     return ObjectFile::createSymbolicFile(Buffer, Type, Context, InitContent);
78   case file_magic::macho_universal_binary:
79     return MachOUniversalBinary::create(Buffer);
80   case file_magic::windows_resource:
81     return WindowsResource::createWindowsResource(Buffer);
82   case file_magic::pdb:
83     // PDB does not support the Binary interface.
84     return errorCodeToError(object_error::invalid_file_type);
85   case file_magic::unknown:
86   case file_magic::cuda_fatbinary:
87   case file_magic::coff_cl_gl_object:
88   case file_magic::dxcontainer_object:
89     // Unrecognized object file format.
90     return errorCodeToError(object_error::invalid_file_type);
91   case file_magic::offload_binary:
92     return OffloadBinary::create(Buffer);
93   case file_magic::minidump:
94     return MinidumpFile::create(Buffer);
95   case file_magic::tapi_file:
96     return TapiUniversal::create(Buffer);
97   }
98   llvm_unreachable("Unexpected Binary File Type");
99 }
100 
101 Expected<OwningBinary<Binary>>
102 object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) {
103   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
104       MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false,
105                                    /*RequiresNullTerminator=*/false);
106   if (std::error_code EC = FileOrErr.getError())
107     return errorCodeToError(EC);
108   std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
109 
110   Expected<std::unique_ptr<Binary>> BinOrErr =
111       createBinary(Buffer->getMemBufferRef(), Context, InitContent);
112   if (!BinOrErr)
113     return BinOrErr.takeError();
114   std::unique_ptr<Binary> &Bin = BinOrErr.get();
115 
116   return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
117 }
118