1 //===- ObjectFile.cpp - File format independent object file -----*- C++ -*-===//
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/COFF.h"
15 #include "llvm/Object/MachO.h"
16 #include "llvm/Object/ObjectFile.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <system_error>
24 using namespace object;
26 void ObjectFile::anchor() { }
28 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
29 : SymbolicFile(Type, Source) {}
31 bool SectionRef::containsSymbol(SymbolRef S) const {
32 ErrorOr<section_iterator> SymSec = S.getSection();
35 return *this == **SymSec;
38 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
39 uint32_t Flags = getSymbolFlags(Ref);
40 if (Flags & SymbolRef::SF_Undefined)
42 if (Flags & SymbolRef::SF_Common)
43 return getCommonSymbolSize(Ref);
44 return getSymbolValueImpl(Ref);
47 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
48 DataRefImpl Symb) const {
49 ErrorOr<StringRef> Name = getSymbolName(Symb);
50 if (std::error_code EC = Name.getError())
53 return std::error_code();
56 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
58 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
59 return section_iterator(SectionRef(Sec, this));
62 ErrorOr<std::unique_ptr<ObjectFile>>
63 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
64 StringRef Data = Object.getBuffer();
65 if (Type == sys::fs::file_magic::unknown)
66 Type = sys::fs::identify_magic(Data);
69 case sys::fs::file_magic::unknown:
70 case sys::fs::file_magic::bitcode:
71 case sys::fs::file_magic::archive:
72 case sys::fs::file_magic::macho_universal_binary:
73 case sys::fs::file_magic::windows_resource:
74 return object_error::invalid_file_type;
75 case sys::fs::file_magic::elf:
76 case sys::fs::file_magic::elf_relocatable:
77 case sys::fs::file_magic::elf_executable:
78 case sys::fs::file_magic::elf_shared_object:
79 case sys::fs::file_magic::elf_core:
80 return createELFObjectFile(Object);
81 case sys::fs::file_magic::macho_object:
82 case sys::fs::file_magic::macho_executable:
83 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
84 case sys::fs::file_magic::macho_core:
85 case sys::fs::file_magic::macho_preload_executable:
86 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
87 case sys::fs::file_magic::macho_dynamic_linker:
88 case sys::fs::file_magic::macho_bundle:
89 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
90 case sys::fs::file_magic::macho_dsym_companion:
91 case sys::fs::file_magic::macho_kext_bundle:
92 return createMachOObjectFile(Object);
93 case sys::fs::file_magic::coff_object:
94 case sys::fs::file_magic::coff_import_library:
95 case sys::fs::file_magic::pecoff_executable:
96 return createCOFFObjectFile(Object);
98 llvm_unreachable("Unexpected Object File Type");
101 ErrorOr<OwningBinary<ObjectFile>>
102 ObjectFile::createObjectFile(StringRef ObjectPath) {
103 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
104 MemoryBuffer::getFile(ObjectPath);
105 if (std::error_code EC = FileOrErr.getError())
107 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
109 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
110 createObjectFile(Buffer->getMemBufferRef());
111 if (std::error_code EC = ObjOrErr.getError())
113 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
115 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));