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 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
32 DataRefImpl Symb) const {
34 if (std::error_code EC = getSymbolName(Symb, Name))
37 return object_error::success;
40 std::error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
41 uint32_t &Result) const {
43 return object_error::success;
46 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
47 return section_iterator(SectionRef(Sec, this));
50 ErrorOr<std::unique_ptr<ObjectFile>>
51 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
52 StringRef Data = Object.getBuffer();
53 if (Type == sys::fs::file_magic::unknown)
54 Type = sys::fs::identify_magic(Data);
57 case sys::fs::file_magic::unknown:
58 case sys::fs::file_magic::bitcode:
59 case sys::fs::file_magic::archive:
60 case sys::fs::file_magic::macho_universal_binary:
61 case sys::fs::file_magic::windows_resource:
62 return object_error::invalid_file_type;
63 case sys::fs::file_magic::elf_relocatable:
64 case sys::fs::file_magic::elf_executable:
65 case sys::fs::file_magic::elf_shared_object:
66 case sys::fs::file_magic::elf_core:
67 return createELFObjectFile(Object);
68 case sys::fs::file_magic::macho_object:
69 case sys::fs::file_magic::macho_executable:
70 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
71 case sys::fs::file_magic::macho_core:
72 case sys::fs::file_magic::macho_preload_executable:
73 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
74 case sys::fs::file_magic::macho_dynamic_linker:
75 case sys::fs::file_magic::macho_bundle:
76 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
77 case sys::fs::file_magic::macho_dsym_companion:
78 return createMachOObjectFile(Object);
79 case sys::fs::file_magic::coff_object:
80 case sys::fs::file_magic::coff_import_library:
81 case sys::fs::file_magic::pecoff_executable:
82 return createCOFFObjectFile(Object);
84 llvm_unreachable("Unexpected Object File Type");
87 ErrorOr<OwningBinary<ObjectFile>>
88 ObjectFile::createObjectFile(StringRef ObjectPath) {
89 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
90 MemoryBuffer::getFile(ObjectPath);
91 if (std::error_code EC = FileOrErr.getError())
93 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
95 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
96 createObjectFile(Buffer->getMemBufferRef());
97 if (std::error_code EC = ObjOrErr.getError())
99 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
101 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));