1 //===- ObjectFile.cpp - File format independent object file -----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines a file format independent ObjectFile class. 11 // 12 //===----------------------------------------------------------------------===// 13 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" 20 21 using namespace llvm; 22 using namespace object; 23 24 void ObjectFile::anchor() { } 25 26 ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *Source, 27 bool BufferOwned) 28 : Binary(Type, Source, BufferOwned) {} 29 30 error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI, 31 uint32_t &Result) const { 32 Result = 0; 33 return object_error::success; 34 } 35 36 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { 37 return section_iterator(SectionRef(Sec, this)); 38 } 39 40 ErrorOr<ObjectFile *> ObjectFile::createObjectFile(MemoryBuffer *Object, 41 bool BufferOwned, 42 sys::fs::file_magic Type) { 43 if (Type == sys::fs::file_magic::unknown) 44 Type = sys::fs::identify_magic(Object->getBuffer()); 45 46 switch (Type) { 47 case sys::fs::file_magic::unknown: 48 case sys::fs::file_magic::bitcode: 49 case sys::fs::file_magic::archive: 50 case sys::fs::file_magic::macho_universal_binary: 51 case sys::fs::file_magic::windows_resource: 52 if (BufferOwned) 53 delete Object; 54 return object_error::invalid_file_type; 55 case sys::fs::file_magic::elf_relocatable: 56 case sys::fs::file_magic::elf_executable: 57 case sys::fs::file_magic::elf_shared_object: 58 case sys::fs::file_magic::elf_core: 59 return createELFObjectFile(Object, BufferOwned); 60 case sys::fs::file_magic::macho_object: 61 case sys::fs::file_magic::macho_executable: 62 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 63 case sys::fs::file_magic::macho_core: 64 case sys::fs::file_magic::macho_preload_executable: 65 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 66 case sys::fs::file_magic::macho_dynamic_linker: 67 case sys::fs::file_magic::macho_bundle: 68 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 69 case sys::fs::file_magic::macho_dsym_companion: 70 return createMachOObjectFile(Object, BufferOwned); 71 case sys::fs::file_magic::coff_object: 72 case sys::fs::file_magic::coff_import_library: 73 case sys::fs::file_magic::pecoff_executable: 74 return createCOFFObjectFile(Object, BufferOwned); 75 } 76 llvm_unreachable("Unexpected Object File Type"); 77 } 78 79 ErrorOr<ObjectFile *> ObjectFile::createObjectFile(StringRef ObjectPath) { 80 OwningPtr<MemoryBuffer> File; 81 if (error_code EC = MemoryBuffer::getFile(ObjectPath, File)) 82 return EC; 83 return createObjectFile(File.take()); 84 } 85