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