xref: /llvm-project/llvm/lib/Object/ObjectFile.cpp (revision 8be28cdc5281edbe83886168978303345ca4a78b)
1 //===- ObjectFile.cpp - File format independent object 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 a file format independent ObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/BinaryFormat/Magic.h"
16 #include "llvm/Object/Binary.h"
17 #include "llvm/Object/COFF.h"
18 #include "llvm/Object/Error.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Object/Wasm.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 "llvm/Support/raw_ostream.h"
27 #include <algorithm>
28 #include <cstdint>
29 #include <memory>
30 #include <system_error>
31 
32 using namespace llvm;
33 using namespace object;
34 
35 void ObjectFile::anchor() {}
36 
37 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
38     : SymbolicFile(Type, Source) {}
39 
40 bool SectionRef::containsSymbol(SymbolRef S) const {
41   Expected<section_iterator> SymSec = S.getSection();
42   if (!SymSec) {
43     // TODO: Actually report errors helpfully.
44     consumeError(SymSec.takeError());
45     return false;
46   }
47   return *this == **SymSec;
48 }
49 
50 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
51   uint32_t Flags = getSymbolFlags(Ref);
52   if (Flags & SymbolRef::SF_Undefined)
53     return 0;
54   if (Flags & SymbolRef::SF_Common)
55     return getCommonSymbolSize(Ref);
56   return getSymbolValueImpl(Ref);
57 }
58 
59 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
60                                             DataRefImpl Symb) const {
61   Expected<StringRef> Name = getSymbolName(Symb);
62   if (!Name)
63     return errorToErrorCode(Name.takeError());
64   OS << *Name;
65   return std::error_code();
66 }
67 
68 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
69 
70 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
71   if (Expected<StringRef> NameOrErr = getSectionName(Sec))
72     return *NameOrErr == ".llvmbc";
73   return false;
74 }
75 
76 bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
77 
78 bool ObjectFile::isBerkeleyText(DataRefImpl Sec) const {
79   return isSectionText(Sec);
80 }
81 
82 bool ObjectFile::isBerkeleyData(DataRefImpl Sec) const {
83   return isSectionData(Sec);
84 }
85 
86 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
87   return section_iterator(SectionRef(Sec, this));
88 }
89 
90 Triple ObjectFile::makeTriple() const {
91   Triple TheTriple;
92   auto Arch = getArch();
93   TheTriple.setArch(Triple::ArchType(Arch));
94 
95   // For ARM targets, try to use the build attributes to build determine
96   // the build target. Target features are also added, but later during
97   // disassembly.
98   if (Arch == Triple::arm || Arch == Triple::armeb)
99     setARMSubArch(TheTriple);
100 
101   // TheTriple defaults to ELF, and COFF doesn't have an environment:
102   // the best we can do here is indicate that it is mach-o.
103   if (isMachO())
104     TheTriple.setObjectFormat(Triple::MachO);
105 
106   if (isCOFF()) {
107     const auto COFFObj = dyn_cast<COFFObjectFile>(this);
108     if (COFFObj->getArch() == Triple::thumb)
109       TheTriple.setTriple("thumbv7-windows");
110   }
111 
112   return TheTriple;
113 }
114 
115 Expected<std::unique_ptr<ObjectFile>>
116 ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
117   StringRef Data = Object.getBuffer();
118   if (Type == file_magic::unknown)
119     Type = identify_magic(Data);
120 
121   switch (Type) {
122   case file_magic::unknown:
123   case file_magic::bitcode:
124   case file_magic::coff_cl_gl_object:
125   case file_magic::archive:
126   case file_magic::macho_universal_binary:
127   case file_magic::windows_resource:
128   case file_magic::pdb:
129   case file_magic::minidump:
130     return errorCodeToError(object_error::invalid_file_type);
131   case file_magic::elf:
132   case file_magic::elf_relocatable:
133   case file_magic::elf_executable:
134   case file_magic::elf_shared_object:
135   case file_magic::elf_core:
136     return createELFObjectFile(Object);
137   case file_magic::macho_object:
138   case file_magic::macho_executable:
139   case file_magic::macho_fixed_virtual_memory_shared_lib:
140   case file_magic::macho_core:
141   case file_magic::macho_preload_executable:
142   case file_magic::macho_dynamically_linked_shared_lib:
143   case file_magic::macho_dynamic_linker:
144   case file_magic::macho_bundle:
145   case file_magic::macho_dynamically_linked_shared_lib_stub:
146   case file_magic::macho_dsym_companion:
147   case file_magic::macho_kext_bundle:
148     return createMachOObjectFile(Object);
149   case file_magic::coff_object:
150   case file_magic::coff_import_library:
151   case file_magic::pecoff_executable:
152     return createCOFFObjectFile(Object);
153   case file_magic::xcoff_object_32:
154     return createXCOFFObjectFile(Object);
155   case file_magic::wasm_object:
156     return createWasmObjectFile(Object);
157   }
158   llvm_unreachable("Unexpected Object File Type");
159 }
160 
161 Expected<OwningBinary<ObjectFile>>
162 ObjectFile::createObjectFile(StringRef ObjectPath) {
163   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
164       MemoryBuffer::getFile(ObjectPath);
165   if (std::error_code EC = FileOrErr.getError())
166     return errorCodeToError(EC);
167   std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
168 
169   Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
170       createObjectFile(Buffer->getMemBufferRef());
171   if (Error Err = ObjOrErr.takeError())
172     return std::move(Err);
173   std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
174 
175   return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
176 }
177