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 ErrorOr<section_iterator> SymSec = S.getSection(); 33 if (!SymSec) 34 return false; 35 return *this == **SymSec; 36 } 37 38 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const { 39 uint32_t Flags = getSymbolFlags(Ref); 40 if (Flags & SymbolRef::SF_Undefined) 41 return 0; 42 if (Flags & SymbolRef::SF_Common) 43 return getCommonSymbolSize(Ref); 44 return getSymbolValueImpl(Ref); 45 } 46 47 std::error_code ObjectFile::printSymbolName(raw_ostream &OS, 48 DataRefImpl Symb) const { 49 Expected<StringRef> Name = getSymbolName(Symb); 50 if (!Name) 51 return errorToErrorCode(Name.takeError()); 52 OS << *Name; 53 return std::error_code(); 54 } 55 56 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; } 57 58 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const { 59 StringRef SectName; 60 if (!getSectionName(Sec, SectName)) 61 return SectName == ".llvmbc"; 62 return false; 63 } 64 65 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { 66 return section_iterator(SectionRef(Sec, this)); 67 } 68 69 Expected<std::unique_ptr<ObjectFile>> 70 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) { 71 StringRef Data = Object.getBuffer(); 72 if (Type == sys::fs::file_magic::unknown) 73 Type = sys::fs::identify_magic(Data); 74 75 switch (Type) { 76 case sys::fs::file_magic::unknown: 77 case sys::fs::file_magic::bitcode: 78 case sys::fs::file_magic::archive: 79 case sys::fs::file_magic::macho_universal_binary: 80 case sys::fs::file_magic::windows_resource: 81 return errorCodeToError(object_error::invalid_file_type); 82 case sys::fs::file_magic::elf: 83 case sys::fs::file_magic::elf_relocatable: 84 case sys::fs::file_magic::elf_executable: 85 case sys::fs::file_magic::elf_shared_object: 86 case sys::fs::file_magic::elf_core: 87 return errorOrToExpected(createELFObjectFile(Object)); 88 case sys::fs::file_magic::macho_object: 89 case sys::fs::file_magic::macho_executable: 90 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 91 case sys::fs::file_magic::macho_core: 92 case sys::fs::file_magic::macho_preload_executable: 93 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 94 case sys::fs::file_magic::macho_dynamic_linker: 95 case sys::fs::file_magic::macho_bundle: 96 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 97 case sys::fs::file_magic::macho_dsym_companion: 98 case sys::fs::file_magic::macho_kext_bundle: 99 return createMachOObjectFile(Object); 100 case sys::fs::file_magic::coff_object: 101 case sys::fs::file_magic::coff_import_library: 102 case sys::fs::file_magic::pecoff_executable: 103 return errorOrToExpected(createCOFFObjectFile(Object)); 104 } 105 llvm_unreachable("Unexpected Object File Type"); 106 } 107 108 Expected<OwningBinary<ObjectFile>> 109 ObjectFile::createObjectFile(StringRef ObjectPath) { 110 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 111 MemoryBuffer::getFile(ObjectPath); 112 if (std::error_code EC = FileOrErr.getError()) 113 return errorCodeToError(EC); 114 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get()); 115 116 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = 117 createObjectFile(Buffer->getMemBufferRef()); 118 if (!ObjOrErr) 119 ObjOrErr.takeError(); 120 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get()); 121 122 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer)); 123 } 124