1 //===- ObjectFile.cpp - File format independent object file ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a file format independent ObjectFile class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/ObjectFile.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/BinaryFormat/Magic.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Object/Wasm.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ErrorOr.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
31 #include <system_error>
34 using namespace object
;
36 void ObjectFile::anchor() {}
38 ObjectFile::ObjectFile(unsigned int Type
, MemoryBufferRef Source
)
39 : SymbolicFile(Type
, Source
) {}
41 bool SectionRef::containsSymbol(SymbolRef S
) const {
42 Expected
<section_iterator
> SymSec
= S
.getSection();
44 // TODO: Actually report errors helpfully.
45 consumeError(SymSec
.takeError());
48 return *this == **SymSec
;
51 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref
) const {
52 uint32_t Flags
= getSymbolFlags(Ref
);
53 if (Flags
& SymbolRef::SF_Undefined
)
55 if (Flags
& SymbolRef::SF_Common
)
56 return getCommonSymbolSize(Ref
);
57 return getSymbolValueImpl(Ref
);
60 std::error_code
ObjectFile::printSymbolName(raw_ostream
&OS
,
61 DataRefImpl Symb
) const {
62 Expected
<StringRef
> Name
= getSymbolName(Symb
);
64 return errorToErrorCode(Name
.takeError());
66 return std::error_code();
69 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI
) const { return 0; }
71 bool ObjectFile::isSectionBitcode(DataRefImpl Sec
) const {
73 if (!getSectionName(Sec
, SectName
))
74 return SectName
== ".llvmbc";
78 bool ObjectFile::isSectionStripped(DataRefImpl Sec
) const { return false; }
80 section_iterator
ObjectFile::getRelocatedSection(DataRefImpl Sec
) const {
81 return section_iterator(SectionRef(Sec
, this));
84 Triple
ObjectFile::makeTriple() const {
86 auto Arch
= getArch();
87 TheTriple
.setArch(Triple::ArchType(Arch
));
89 // For ARM targets, try to use the build attributes to build determine
90 // the build target. Target features are also added, but later during
92 if (Arch
== Triple::arm
|| Arch
== Triple::armeb
)
93 setARMSubArch(TheTriple
);
95 // TheTriple defaults to ELF, and COFF doesn't have an environment:
96 // the best we can do here is indicate that it is mach-o.
98 TheTriple
.setObjectFormat(Triple::MachO
);
101 const auto COFFObj
= dyn_cast
<COFFObjectFile
>(this);
102 if (COFFObj
->getArch() == Triple::thumb
)
103 TheTriple
.setTriple("thumbv7-windows");
109 Expected
<std::unique_ptr
<ObjectFile
>>
110 ObjectFile::createObjectFile(MemoryBufferRef Object
, file_magic Type
) {
111 StringRef Data
= Object
.getBuffer();
112 if (Type
== file_magic::unknown
)
113 Type
= identify_magic(Data
);
116 case file_magic::unknown
:
117 case file_magic::bitcode
:
118 case file_magic::coff_cl_gl_object
:
119 case file_magic::archive
:
120 case file_magic::macho_universal_binary
:
121 case file_magic::windows_resource
:
122 case file_magic::pdb
:
123 return errorCodeToError(object_error::invalid_file_type
);
124 case file_magic::elf
:
125 case file_magic::elf_relocatable
:
126 case file_magic::elf_executable
:
127 case file_magic::elf_shared_object
:
128 case file_magic::elf_core
:
129 return createELFObjectFile(Object
);
130 case file_magic::macho_object
:
131 case file_magic::macho_executable
:
132 case file_magic::macho_fixed_virtual_memory_shared_lib
:
133 case file_magic::macho_core
:
134 case file_magic::macho_preload_executable
:
135 case file_magic::macho_dynamically_linked_shared_lib
:
136 case file_magic::macho_dynamic_linker
:
137 case file_magic::macho_bundle
:
138 case file_magic::macho_dynamically_linked_shared_lib_stub
:
139 case file_magic::macho_dsym_companion
:
140 case file_magic::macho_kext_bundle
:
141 return createMachOObjectFile(Object
);
142 case file_magic::coff_object
:
143 case file_magic::coff_import_library
:
144 case file_magic::pecoff_executable
:
145 return createCOFFObjectFile(Object
);
146 case file_magic::wasm_object
:
147 return createWasmObjectFile(Object
);
149 llvm_unreachable("Unexpected Object File Type");
152 Expected
<OwningBinary
<ObjectFile
>>
153 ObjectFile::createObjectFile(StringRef ObjectPath
) {
154 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
155 MemoryBuffer::getFile(ObjectPath
);
156 if (std::error_code EC
= FileOrErr
.getError())
157 return errorCodeToError(EC
);
158 std::unique_ptr
<MemoryBuffer
> Buffer
= std::move(FileOrErr
.get());
160 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
161 createObjectFile(Buffer
->getMemBufferRef());
162 if (Error Err
= ObjOrErr
.takeError())
163 return std::move(Err
);
164 std::unique_ptr
<ObjectFile
> Obj
= std::move(ObjOrErr
.get());
166 return OwningBinary
<ObjectFile
>(std::move(Obj
), std::move(Buffer
));