1 //===- ObjectFile.cpp - File format independent object file ---------------===// 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/StringRef.h" 16 #include "llvm/BinaryFormat/Magic.h" 17 #include "llvm/Object/Binary.h" 18 #include "llvm/Object/COFF.h" 19 #include "llvm/Object/Error.h" 20 #include "llvm/Object/MachO.h" 21 #include "llvm/Object/Wasm.h" 22 #include "llvm/Support/Error.h" 23 #include "llvm/Support/ErrorHandling.h" 24 #include "llvm/Support/ErrorOr.h" 25 #include "llvm/Support/FileSystem.h" 26 #include "llvm/Support/MemoryBuffer.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include <algorithm> 29 #include <cstdint> 30 #include <memory> 31 #include <system_error> 32 33 using namespace llvm; 34 using namespace object; 35 36 void ObjectFile::anchor() {} 37 38 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source) 39 : SymbolicFile(Type, Source) {} 40 41 bool SectionRef::containsSymbol(SymbolRef S) const { 42 Expected<section_iterator> SymSec = S.getSection(); 43 if (!SymSec) { 44 // TODO: Actually report errors helpfully. 45 consumeError(SymSec.takeError()); 46 return false; 47 } 48 return *this == **SymSec; 49 } 50 51 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const { 52 uint32_t Flags = getSymbolFlags(Ref); 53 if (Flags & SymbolRef::SF_Undefined) 54 return 0; 55 if (Flags & SymbolRef::SF_Common) 56 return getCommonSymbolSize(Ref); 57 return getSymbolValueImpl(Ref); 58 } 59 60 std::error_code ObjectFile::printSymbolName(raw_ostream &OS, 61 DataRefImpl Symb) const { 62 Expected<StringRef> Name = getSymbolName(Symb); 63 if (!Name) 64 return errorToErrorCode(Name.takeError()); 65 OS << *Name; 66 return std::error_code(); 67 } 68 69 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; } 70 71 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const { 72 StringRef SectName; 73 if (!getSectionName(Sec, SectName)) 74 return SectName == ".llvmbc"; 75 return false; 76 } 77 78 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { 79 return section_iterator(SectionRef(Sec, this)); 80 } 81 82 Triple ObjectFile::makeTriple() const { 83 Triple TheTriple; 84 auto Arch = getArch(); 85 TheTriple.setArch(Triple::ArchType(Arch)); 86 87 // For ARM targets, try to use the build attributes to build determine 88 // the build target. Target features are also added, but later during 89 // disassembly. 90 if (Arch == Triple::arm || Arch == Triple::armeb) 91 setARMSubArch(TheTriple); 92 93 // TheTriple defaults to ELF, and COFF doesn't have an environment: 94 // the best we can do here is indicate that it is mach-o. 95 if (isMachO()) 96 TheTriple.setObjectFormat(Triple::MachO); 97 98 if (isCOFF()) { 99 const auto COFFObj = dyn_cast<COFFObjectFile>(this); 100 if (COFFObj->getArch() == Triple::thumb) 101 TheTriple.setTriple("thumbv7-windows"); 102 } 103 104 return TheTriple; 105 } 106 107 Expected<std::unique_ptr<ObjectFile>> 108 ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) { 109 StringRef Data = Object.getBuffer(); 110 if (Type == file_magic::unknown) 111 Type = identify_magic(Data); 112 113 switch (Type) { 114 case file_magic::unknown: 115 case file_magic::bitcode: 116 case file_magic::coff_cl_gl_object: 117 case file_magic::archive: 118 case file_magic::macho_universal_binary: 119 case file_magic::windows_resource: 120 return errorCodeToError(object_error::invalid_file_type); 121 case file_magic::elf: 122 case file_magic::elf_relocatable: 123 case file_magic::elf_executable: 124 case file_magic::elf_shared_object: 125 case file_magic::elf_core: 126 return errorOrToExpected(createELFObjectFile(Object)); 127 case file_magic::macho_object: 128 case file_magic::macho_executable: 129 case file_magic::macho_fixed_virtual_memory_shared_lib: 130 case file_magic::macho_core: 131 case file_magic::macho_preload_executable: 132 case file_magic::macho_dynamically_linked_shared_lib: 133 case file_magic::macho_dynamic_linker: 134 case file_magic::macho_bundle: 135 case file_magic::macho_dynamically_linked_shared_lib_stub: 136 case file_magic::macho_dsym_companion: 137 case file_magic::macho_kext_bundle: 138 return createMachOObjectFile(Object); 139 case file_magic::coff_object: 140 case file_magic::coff_import_library: 141 case file_magic::pecoff_executable: 142 return errorOrToExpected(createCOFFObjectFile(Object)); 143 case file_magic::wasm_object: 144 return createWasmObjectFile(Object); 145 } 146 llvm_unreachable("Unexpected Object File Type"); 147 } 148 149 Expected<OwningBinary<ObjectFile>> 150 ObjectFile::createObjectFile(StringRef ObjectPath) { 151 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 152 MemoryBuffer::getFile(ObjectPath); 153 if (std::error_code EC = FileOrErr.getError()) 154 return errorCodeToError(EC); 155 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get()); 156 157 Expected<std::unique_ptr<ObjectFile>> ObjOrErr = 158 createObjectFile(Buffer->getMemBufferRef()); 159 if (Error Err = ObjOrErr.takeError()) 160 return std::move(Err); 161 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get()); 162 163 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer)); 164 } 165