blob: c56eeb1ea8b8d95968726a32f504dda6f1bb0108 [file] [log] [blame]
Michael J. Spencerb02c95d2011-06-25 17:54:29 +00001//===- 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"
Rafael Espindola110801a2013-06-11 14:39:59 +000016#include "llvm/Support/FileSystem.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000017#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000018#include "llvm/Support/Path.h"
19
Michael J. Spencerec29b122011-06-25 17:54:50 +000020// Include headers for createBinary.
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000021#include "llvm/Object/Archive.h"
Alexey Samsonove6388e62013-06-18 15:03:28 +000022#include "llvm/Object/MachOUniversal.h"
Michael J. Spencerd3b7b122011-09-27 19:36:55 +000023#include "llvm/Object/ObjectFile.h"
Michael J. Spencerec29b122011-06-25 17:54:50 +000024
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000025using namespace llvm;
26using namespace object;
27
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000028Binary::~Binary() {}
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000029
Rafael Espindola48af1c22014-08-19 18:44:46 +000030Binary::Binary(unsigned int Type, MemoryBufferRef Source)
31 : TypeID(Type), Data(Source) {}
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000032
Rafael Espindola48af1c22014-08-19 18:44:46 +000033StringRef Binary::getData() const { return Data.getBuffer(); }
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000034
Rafael Espindola48af1c22014-08-19 18:44:46 +000035StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000036
Rafael Espindola48af1c22014-08-19 18:44:46 +000037MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
38
39ErrorOr<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
40 LLVMContext *Context) {
41 sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer());
Rafael Espindolaec46f312014-01-22 16:04:52 +000042
43 switch (Type) {
Rafael Espindola692410e2014-01-21 23:06:54 +000044 case sys::fs::file_magic::archive:
Rafael Espindola48af1c22014-08-19 18:44:46 +000045 return Archive::create(Buffer);
Michael J. Spencerbbd875b2014-11-18 01:14:25 +000046 case sys::fs::file_magic::elf:
Rafael Espindola110801a2013-06-11 14:39:59 +000047 case sys::fs::file_magic::elf_relocatable:
48 case sys::fs::file_magic::elf_executable:
49 case sys::fs::file_magic::elf_shared_object:
Rafael Espindola692410e2014-01-21 23:06:54 +000050 case sys::fs::file_magic::elf_core:
Rafael Espindola110801a2013-06-11 14:39:59 +000051 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:
Alexey Samsonov64188f92013-06-28 09:44:05 +000059 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
Rafael Espindola692410e2014-01-21 23:06:54 +000060 case sys::fs::file_magic::macho_dsym_companion:
Rafael Espindola110801a2013-06-11 14:39:59 +000061 case sys::fs::file_magic::coff_object:
Rui Ueyamae448f9e2013-11-15 21:22:02 +000062 case sys::fs::file_magic::coff_import_library:
Rafael Espindola692410e2014-01-21 23:06:54 +000063 case sys::fs::file_magic::pecoff_executable:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000064 case sys::fs::file_magic::bitcode:
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000065 return ObjectFile::createSymbolicFile(Buffer, Type, Context);
Rafael Espindolaec46f312014-01-22 16:04:52 +000066 case sys::fs::file_magic::macho_universal_binary:
David Blaikie1f76e522014-09-03 17:41:05 +000067 return MachOUniversalBinary::create(Buffer);
Alexey Samsonov64188f92013-06-28 09:44:05 +000068 case sys::fs::file_magic::unknown:
Rafael Espindolaec46f312014-01-22 16:04:52 +000069 case sys::fs::file_magic::windows_resource:
Alexey Samsonov64188f92013-06-28 09:44:05 +000070 // Unrecognized object file format.
Michael J. Spencerec29b122011-06-25 17:54:50 +000071 return object_error::invalid_file_type;
72 }
Alexey Samsonov64188f92013-06-28 09:44:05 +000073 llvm_unreachable("Unexpected Binary File Type");
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000074}
75
Rafael Espindola48af1c22014-08-19 18:44:46 +000076ErrorOr<OwningBinary<Binary>> object::createBinary(StringRef Path) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000077 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
78 MemoryBuffer::getFileOrSTDIN(Path);
79 if (std::error_code EC = FileOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +000080 return EC;
Rafael Espindola48af1c22014-08-19 18:44:46 +000081 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
82
83 ErrorOr<std::unique_ptr<Binary>> BinOrErr =
84 createBinary(Buffer->getMemBufferRef());
85 if (std::error_code EC = BinOrErr.getError())
86 return EC;
87 std::unique_ptr<Binary> &Bin = BinOrErr.get();
88
89 return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000090}