xref: /freebsd/contrib/llvm-project/llvm/lib/Object/SymbolicFile.cpp (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
1 //===- SymbolicFile.cpp - Interface that only provides symbols ------------===//
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 a file format independent SymbolicFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/SymbolicFile.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/BinaryFormat/Magic.h"
16 #include "llvm/Object/COFFImportFile.h"
17 #include "llvm/Object/Error.h"
18 #include "llvm/Object/IRObjectFile.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include <algorithm>
27 #include <memory>
28 
29 using namespace llvm;
30 using namespace object;
31 
32 SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
33     : Binary(Type, Source) {}
34 
35 SymbolicFile::~SymbolicFile() = default;
36 
37 Expected<std::unique_ptr<SymbolicFile>>
38 SymbolicFile::createSymbolicFile(MemoryBufferRef Object, file_magic Type,
39                                  LLVMContext *Context, bool InitContent) {
40   StringRef Data = Object.getBuffer();
41   if (Type == file_magic::unknown)
42     Type = identify_magic(Data);
43 
44   if (!isSymbolicFile(Type, Context))
45     return errorCodeToError(object_error::invalid_file_type);
46 
47   switch (Type) {
48   case file_magic::bitcode:
49     // Context is guaranteed to be non-null here, because bitcode magic only
50     // indicates a symbolic file when Context is non-null.
51     return IRObjectFile::create(Object, *Context);
52   case file_magic::elf:
53   case file_magic::elf_executable:
54   case file_magic::elf_shared_object:
55   case file_magic::elf_core:
56   case file_magic::macho_executable:
57   case file_magic::macho_fixed_virtual_memory_shared_lib:
58   case file_magic::macho_core:
59   case file_magic::macho_preload_executable:
60   case file_magic::macho_dynamically_linked_shared_lib:
61   case file_magic::macho_dynamic_linker:
62   case file_magic::macho_bundle:
63   case file_magic::macho_dynamically_linked_shared_lib_stub:
64   case file_magic::macho_dsym_companion:
65   case file_magic::macho_kext_bundle:
66   case file_magic::pecoff_executable:
67   case file_magic::xcoff_object_32:
68   case file_magic::xcoff_object_64:
69   case file_magic::wasm_object:
70     return ObjectFile::createObjectFile(Object, Type, InitContent);
71   case file_magic::coff_import_library:
72     return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
73   case file_magic::elf_relocatable:
74   case file_magic::macho_object:
75   case file_magic::coff_object: {
76     Expected<std::unique_ptr<ObjectFile>> Obj =
77         ObjectFile::createObjectFile(Object, Type, InitContent);
78     if (!Obj || !Context)
79       return std::move(Obj);
80 
81     Expected<MemoryBufferRef> BCData =
82         IRObjectFile::findBitcodeInObject(*Obj->get());
83     if (!BCData) {
84       consumeError(BCData.takeError());
85       return std::move(Obj);
86     }
87 
88     return IRObjectFile::create(
89         MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
90         *Context);
91   }
92   default:
93     llvm_unreachable("Unexpected Binary File Type");
94   }
95 }
96 
97 bool SymbolicFile::isSymbolicFile(file_magic Type, const LLVMContext *Context) {
98   switch (Type) {
99   case file_magic::bitcode:
100     return Context != nullptr;
101   case file_magic::elf:
102   case file_magic::elf_executable:
103   case file_magic::elf_shared_object:
104   case file_magic::elf_core:
105   case file_magic::macho_executable:
106   case file_magic::macho_fixed_virtual_memory_shared_lib:
107   case file_magic::macho_core:
108   case file_magic::macho_preload_executable:
109   case file_magic::macho_dynamically_linked_shared_lib:
110   case file_magic::macho_dynamic_linker:
111   case file_magic::macho_bundle:
112   case file_magic::macho_dynamically_linked_shared_lib_stub:
113   case file_magic::macho_dsym_companion:
114   case file_magic::macho_kext_bundle:
115   case file_magic::pecoff_executable:
116   case file_magic::xcoff_object_32:
117   case file_magic::xcoff_object_64:
118   case file_magic::wasm_object:
119   case file_magic::coff_import_library:
120   case file_magic::elf_relocatable:
121   case file_magic::macho_object:
122   case file_magic::coff_object:
123     return true;
124   default:
125     return false;
126   }
127 }
128