blob: 89e9d732ce9dcc9a95c410f33ed5c3cc4036a536 [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 Espindola2e60ca92014-06-24 13:56:32 +000030Binary::Binary(unsigned int Type, std::unique_ptr<MemoryBuffer> Source)
31 : TypeID(Type), Data(std::move(Source)) {}
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000032
33StringRef Binary::getData() const {
34 return Data->getBuffer();
35}
36
37StringRef Binary::getFileName() const {
38 return Data->getBufferIdentifier();
39}
40
Rafael Espindola437b0d52014-07-31 03:12:45 +000041ErrorOr<std::unique_ptr<Binary>>
42object::createBinary(std::unique_ptr<MemoryBuffer> Buffer,
43 LLVMContext *Context) {
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000044 sys::fs::file_magic Type = sys::fs::identify_magic(Buffer->getBuffer());
Rafael Espindolaec46f312014-01-22 16:04:52 +000045
46 switch (Type) {
Rafael Espindola692410e2014-01-21 23:06:54 +000047 case sys::fs::file_magic::archive:
Rafael Espindola2e60ca92014-06-24 13:56:32 +000048 return Archive::create(std::move(Buffer));
Rafael Espindola110801a2013-06-11 14:39:59 +000049 case sys::fs::file_magic::elf_relocatable:
50 case sys::fs::file_magic::elf_executable:
51 case sys::fs::file_magic::elf_shared_object:
Rafael Espindola692410e2014-01-21 23:06:54 +000052 case sys::fs::file_magic::elf_core:
Rafael Espindola110801a2013-06-11 14:39:59 +000053 case sys::fs::file_magic::macho_object:
54 case sys::fs::file_magic::macho_executable:
55 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
56 case sys::fs::file_magic::macho_core:
57 case sys::fs::file_magic::macho_preload_executable:
58 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
59 case sys::fs::file_magic::macho_dynamic_linker:
60 case sys::fs::file_magic::macho_bundle:
Alexey Samsonov64188f92013-06-28 09:44:05 +000061 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
Rafael Espindola692410e2014-01-21 23:06:54 +000062 case sys::fs::file_magic::macho_dsym_companion:
Rafael Espindola110801a2013-06-11 14:39:59 +000063 case sys::fs::file_magic::coff_object:
Rui Ueyamae448f9e2013-11-15 21:22:02 +000064 case sys::fs::file_magic::coff_import_library:
Rafael Espindola692410e2014-01-21 23:06:54 +000065 case sys::fs::file_magic::pecoff_executable:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000066 case sys::fs::file_magic::bitcode:
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000067 return ObjectFile::createSymbolicFile(Buffer, Type, Context);
Rafael Espindolaec46f312014-01-22 16:04:52 +000068 case sys::fs::file_magic::macho_universal_binary:
Rafael Espindola2e60ca92014-06-24 13:56:32 +000069 return MachOUniversalBinary::create(std::move(Buffer));
Alexey Samsonov64188f92013-06-28 09:44:05 +000070 case sys::fs::file_magic::unknown:
Rafael Espindolaec46f312014-01-22 16:04:52 +000071 case sys::fs::file_magic::windows_resource:
Alexey Samsonov64188f92013-06-28 09:44:05 +000072 // Unrecognized object file format.
Michael J. Spencerec29b122011-06-25 17:54:50 +000073 return object_error::invalid_file_type;
74 }
Alexey Samsonov64188f92013-06-28 09:44:05 +000075 llvm_unreachable("Unexpected Binary File Type");
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000076}
77
Rafael Espindola437b0d52014-07-31 03:12:45 +000078ErrorOr<std::unique_ptr<Binary>> object::createBinary(StringRef Path) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000079 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
80 MemoryBuffer::getFileOrSTDIN(Path);
81 if (std::error_code EC = FileOrErr.getError())
Rafael Espindola63da2952014-01-15 19:37:43 +000082 return EC;
David Blaikie4b9ae522014-07-21 16:26:24 +000083 return createBinary(std::move(*FileOrErr));
Michael J. Spencerb02c95d2011-06-25 17:54:29 +000084}