1 //===- ObjectFile.cpp - File format independent object file ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines a file format independent ObjectFile class.
11 //===----------------------------------------------------------------------===//
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"
30 #include <system_error>
33 using namespace object
;
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
;
42 void ObjectFile::anchor() {}
44 ObjectFile::ObjectFile(unsigned int Type
, MemoryBufferRef Source
)
45 : SymbolicFile(Type
, Source
) {}
47 bool SectionRef::containsSymbol(SymbolRef S
) const {
48 Expected
<section_iterator
> SymSec
= S
.getSection();
50 // TODO: Actually report errors helpfully.
51 consumeError(SymSec
.takeError());
54 return *this == **SymSec
;
57 Expected
<uint64_t> ObjectFile::getSymbolValue(DataRefImpl Ref
) const {
58 if (Expected
<uint32_t> FlagsOrErr
= getSymbolFlags(Ref
)) {
59 if (*FlagsOrErr
& SymbolRef::SF_Undefined
)
61 if (*FlagsOrErr
& SymbolRef::SF_Common
)
62 return getCommonSymbolSize(Ref
);
64 // TODO: Test this error.
65 return FlagsOrErr
.takeError();
66 return getSymbolValueImpl(Ref
);
69 Error
ObjectFile::printSymbolName(raw_ostream
&OS
, DataRefImpl Symb
) const {
70 Expected
<StringRef
> Name
= getSymbolName(Symb
);
72 return Name
.takeError();
74 return Error::success();
77 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI
) const { return 0; }
79 bool ObjectFile::isSectionBitcode(DataRefImpl Sec
) const {
80 Expected
<StringRef
> NameOrErr
= getSectionName(Sec
);
82 return *NameOrErr
== ".llvmbc";
83 consumeError(NameOrErr
.takeError());
87 bool ObjectFile::isSectionStripped(DataRefImpl Sec
) const { return false; }
89 bool ObjectFile::isBerkeleyText(DataRefImpl Sec
) const {
90 return isSectionText(Sec
);
93 bool ObjectFile::isBerkeleyData(DataRefImpl Sec
) const {
94 return isSectionData(Sec
);
97 bool ObjectFile::isDebugSection(DataRefImpl Sec
) const { return false; }
99 Expected
<section_iterator
>
100 ObjectFile::getRelocatedSection(DataRefImpl Sec
) const {
101 return section_iterator(SectionRef(Sec
, this));
104 Triple
ObjectFile::makeTriple() const {
106 auto Arch
= getArch();
107 TheTriple
.setArch(Triple::ArchType(Arch
));
109 // For ARM targets, try to use the build attributes to build determine
110 // the build target. Target features are also added, but later during
112 if (Arch
== Triple::arm
|| Arch
== Triple::armeb
)
113 setARMSubArch(TheTriple
);
115 // TheTriple defaults to ELF, and COFF doesn't have an environment:
116 // something we can do here is indicate that it is mach-o.
118 TheTriple
.setObjectFormat(Triple::MachO
);
119 } else if (isCOFF()) {
120 const auto COFFObj
= cast
<COFFObjectFile
>(this);
121 if (COFFObj
->getArch() == Triple::thumb
)
122 TheTriple
.setTriple("thumbv7-windows");
123 } else if (isXCOFF()) {
124 // XCOFF implies AIX.
125 TheTriple
.setOS(Triple::AIX
);
126 TheTriple
.setObjectFormat(Triple::XCOFF
);
132 Expected
<std::unique_ptr
<ObjectFile
>>
133 ObjectFile::createObjectFile(MemoryBufferRef Object
, file_magic Type
,
135 StringRef Data
= Object
.getBuffer();
136 if (Type
== file_magic::unknown
)
137 Type
= identify_magic(Data
);
140 case file_magic::unknown
:
141 case file_magic::bitcode
:
142 case file_magic::coff_cl_gl_object
:
143 case file_magic::archive
:
144 case file_magic::macho_universal_binary
:
145 case file_magic::windows_resource
:
146 case file_magic::pdb
:
147 case file_magic::minidump
:
148 case file_magic::goff_object
:
149 return errorCodeToError(object_error::invalid_file_type
);
150 case file_magic::tapi_file
:
151 return errorCodeToError(object_error::invalid_file_type
);
152 case file_magic::elf
:
153 case file_magic::elf_relocatable
:
154 case file_magic::elf_executable
:
155 case file_magic::elf_shared_object
:
156 case file_magic::elf_core
:
157 return createELFObjectFile(Object
, InitContent
);
158 case file_magic::macho_object
:
159 case file_magic::macho_executable
:
160 case file_magic::macho_fixed_virtual_memory_shared_lib
:
161 case file_magic::macho_core
:
162 case file_magic::macho_preload_executable
:
163 case file_magic::macho_dynamically_linked_shared_lib
:
164 case file_magic::macho_dynamic_linker
:
165 case file_magic::macho_bundle
:
166 case file_magic::macho_dynamically_linked_shared_lib_stub
:
167 case file_magic::macho_dsym_companion
:
168 case file_magic::macho_kext_bundle
:
169 return createMachOObjectFile(Object
);
170 case file_magic::coff_object
:
171 case file_magic::coff_import_library
:
172 case file_magic::pecoff_executable
:
173 return createCOFFObjectFile(Object
);
174 case file_magic::xcoff_object_32
:
175 return createXCOFFObjectFile(Object
, Binary::ID_XCOFF32
);
176 case file_magic::xcoff_object_64
:
177 return createXCOFFObjectFile(Object
, Binary::ID_XCOFF64
);
178 case file_magic::wasm_object
:
179 return createWasmObjectFile(Object
);
181 llvm_unreachable("Unexpected Object File Type");
184 Expected
<OwningBinary
<ObjectFile
>>
185 ObjectFile::createObjectFile(StringRef ObjectPath
) {
186 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
187 MemoryBuffer::getFile(ObjectPath
);
188 if (std::error_code EC
= FileOrErr
.getError())
189 return errorCodeToError(EC
);
190 std::unique_ptr
<MemoryBuffer
> Buffer
= std::move(FileOrErr
.get());
192 Expected
<std::unique_ptr
<ObjectFile
>> ObjOrErr
=
193 createObjectFile(Buffer
->getMemBufferRef());
194 if (Error Err
= ObjOrErr
.takeError())
195 return std::move(Err
);
196 std::unique_ptr
<ObjectFile
> Obj
= std::move(ObjOrErr
.get());
198 return OwningBinary
<ObjectFile
>(std::move(Obj
), std::move(Buffer
));