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/ObjectFile.h"
15 #include "llvm/ADT/OwningPtr.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/system_error.h"
22 using namespace object;
24 void ObjectFile::anchor() { }
26 ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *Source,
28 : SymbolicFile(Type, Source, BufferOwned) {}
30 error_code ObjectFile::printSymbolName(raw_ostream &OS,
31 DataRefImpl Symb) const {
33 if (error_code EC = getSymbolName(Symb, Name))
36 return object_error::success;
39 error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
40 uint32_t &Result) const {
42 return object_error::success;
45 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
46 return section_iterator(SectionRef(Sec, this));
49 ErrorOr<ObjectFile *> ObjectFile::createObjectFile(MemoryBuffer *Object,
51 sys::fs::file_magic Type) {
52 if (Type == sys::fs::file_magic::unknown)
53 Type = sys::fs::identify_magic(Object->getBuffer());
56 case sys::fs::file_magic::unknown:
57 case sys::fs::file_magic::bitcode:
58 case sys::fs::file_magic::archive:
59 case sys::fs::file_magic::macho_universal_binary:
60 case sys::fs::file_magic::windows_resource:
63 return object_error::invalid_file_type;
64 case sys::fs::file_magic::elf_relocatable:
65 case sys::fs::file_magic::elf_executable:
66 case sys::fs::file_magic::elf_shared_object:
67 case sys::fs::file_magic::elf_core:
68 return createELFObjectFile(Object, BufferOwned);
69 case sys::fs::file_magic::macho_object:
70 case sys::fs::file_magic::macho_executable:
71 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
72 case sys::fs::file_magic::macho_core:
73 case sys::fs::file_magic::macho_preload_executable:
74 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
75 case sys::fs::file_magic::macho_dynamic_linker:
76 case sys::fs::file_magic::macho_bundle:
77 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
78 case sys::fs::file_magic::macho_dsym_companion:
79 return createMachOObjectFile(Object, BufferOwned);
80 case sys::fs::file_magic::coff_object:
81 case sys::fs::file_magic::coff_import_library:
82 case sys::fs::file_magic::pecoff_executable:
83 return createCOFFObjectFile(Object, BufferOwned);
85 llvm_unreachable("Unexpected Object File Type");
88 ErrorOr<ObjectFile *> ObjectFile::createObjectFile(StringRef ObjectPath) {
89 OwningPtr<MemoryBuffer> File;
90 if (error_code EC = MemoryBuffer::getFile(ObjectPath, File))
92 return createObjectFile(File.take());