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/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> 22 23 using namespace llvm; 24 using namespace object; 25 26 void ObjectFile::anchor() { } 27 28 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source) 29 : SymbolicFile(Type, Source) {} 30 31 bool SectionRef::containsSymbol(SymbolRef S) const { 32 section_iterator SymSec = getObject()->section_end(); 33 if (S.getSection(SymSec)) 34 return false; 35 return *this == *SymSec; 36 } 37 38 std::error_code ObjectFile::printSymbolName(raw_ostream &OS, 39 DataRefImpl Symb) const { 40 ErrorOr<StringRef> Name = getSymbolName(Symb); 41 if (std::error_code EC = Name.getError()) 42 return EC; 43 OS << *Name; 44 return std::error_code(); 45 } 46 47 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; } 48 49 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { 50 return section_iterator(SectionRef(Sec, this)); 51 } 52 53 ErrorOr<std::unique_ptr<ObjectFile>> 54 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) { 55 StringRef Data = Object.getBuffer(); 56 if (Type == sys::fs::file_magic::unknown) 57 Type = sys::fs::identify_magic(Data); 58 59 switch (Type) { 60 case sys::fs::file_magic::unknown: 61 case sys::fs::file_magic::bitcode: 62 case sys::fs::file_magic::archive: 63 case sys::fs::file_magic::macho_universal_binary: 64 case sys::fs::file_magic::windows_resource: 65 return object_error::invalid_file_type; 66 case sys::fs::file_magic::elf: 67 case sys::fs::file_magic::elf_relocatable: 68 case sys::fs::file_magic::elf_executable: 69 case sys::fs::file_magic::elf_shared_object: 70 case sys::fs::file_magic::elf_core: 71 return createELFObjectFile(Object); 72 case sys::fs::file_magic::macho_object: 73 case sys::fs::file_magic::macho_executable: 74 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 75 case sys::fs::file_magic::macho_core: 76 case sys::fs::file_magic::macho_preload_executable: 77 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 78 case sys::fs::file_magic::macho_dynamic_linker: 79 case sys::fs::file_magic::macho_bundle: 80 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 81 case sys::fs::file_magic::macho_dsym_companion: 82 case sys::fs::file_magic::macho_kext_bundle: 83 return createMachOObjectFile(Object); 84 case sys::fs::file_magic::coff_object: 85 case sys::fs::file_magic::coff_import_library: 86 case sys::fs::file_magic::pecoff_executable: 87 return createCOFFObjectFile(Object); 88 } 89 llvm_unreachable("Unexpected Object File Type"); 90 } 91 92 ErrorOr<OwningBinary<ObjectFile>> 93 ObjectFile::createObjectFile(StringRef ObjectPath) { 94 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 95 MemoryBuffer::getFile(ObjectPath); 96 if (std::error_code EC = FileOrErr.getError()) 97 return EC; 98 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get()); 99 100 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = 101 createObjectFile(Buffer->getMemBufferRef()); 102 if (std::error_code EC = ObjOrErr.getError()) 103 return EC; 104 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get()); 105 106 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer)); 107 } 108