1 //===- Binary.cpp - A generic binary 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 the Binary class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Object/Binary.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/Path.h"
19 
20 // Include headers for createBinary.
21 #include "llvm/Object/Archive.h"
22 #include "llvm/Object/MachOUniversal.h"
23 #include "llvm/Object/ObjectFile.h"
24 
25 using namespace llvm;
26 using namespace object;
27 
~Binary()28 Binary::~Binary() {}
29 
Binary(unsigned int Type,MemoryBufferRef Source)30 Binary::Binary(unsigned int Type, MemoryBufferRef Source)
31     : TypeID(Type), Data(Source) {}
32 
getData() const33 StringRef Binary::getData() const { return Data.getBuffer(); }
34 
getFileName() const35 StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
36 
getMemoryBufferRef() const37 MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
38 
createBinary(MemoryBufferRef Buffer,LLVMContext * Context)39 Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
40                                                       LLVMContext *Context) {
41   sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer());
42 
43   switch (Type) {
44     case sys::fs::file_magic::archive:
45       return Archive::create(Buffer);
46     case sys::fs::file_magic::elf:
47     case sys::fs::file_magic::elf_relocatable:
48     case sys::fs::file_magic::elf_executable:
49     case sys::fs::file_magic::elf_shared_object:
50     case sys::fs::file_magic::elf_core:
51     case sys::fs::file_magic::macho_object:
52     case sys::fs::file_magic::macho_executable:
53     case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
54     case sys::fs::file_magic::macho_core:
55     case sys::fs::file_magic::macho_preload_executable:
56     case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
57     case sys::fs::file_magic::macho_dynamic_linker:
58     case sys::fs::file_magic::macho_bundle:
59     case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
60     case sys::fs::file_magic::macho_dsym_companion:
61     case sys::fs::file_magic::macho_kext_bundle:
62     case sys::fs::file_magic::coff_object:
63     case sys::fs::file_magic::coff_import_library:
64     case sys::fs::file_magic::pecoff_executable:
65     case sys::fs::file_magic::bitcode:
66       return ObjectFile::createSymbolicFile(Buffer, Type, Context);
67     case sys::fs::file_magic::macho_universal_binary:
68       return MachOUniversalBinary::create(Buffer);
69     case sys::fs::file_magic::unknown:
70     case sys::fs::file_magic::windows_resource:
71       // Unrecognized object file format.
72       return errorCodeToError(object_error::invalid_file_type);
73   }
74   llvm_unreachable("Unexpected Binary File Type");
75 }
76 
createBinary(StringRef Path)77 Expected<OwningBinary<Binary>> object::createBinary(StringRef Path) {
78   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
79       MemoryBuffer::getFileOrSTDIN(Path);
80   if (std::error_code EC = FileOrErr.getError())
81     return errorCodeToError(EC);
82   std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
83 
84   Expected<std::unique_ptr<Binary>> BinOrErr =
85       createBinary(Buffer->getMemBufferRef());
86   if (!BinOrErr)
87     return BinOrErr.takeError();
88   std::unique_ptr<Binary> &Bin = BinOrErr.get();
89 
90   return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
91 }
92