1// binary.h -- binary input files for gold   -*- C++ -*-
2
3// Copyright (C) 2008-2017 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
23// Support binary input files by making them look like an ELF file.
24
25#ifndef GOLD_BINARY_H
26#define GOLD_BINARY_H
27
28#include <string>
29
30#include "elfcpp.h"
31
32namespace gold
33{
34
35class Task;
36
37template<typename Stringpool_char>
38class Stringpool_template;
39
40// This class takes a file name and creates a buffer which looks like
41// an ELF file read into memory.
42
43class Binary_to_elf
44{
45 public:
46  Binary_to_elf(elfcpp::EM machine, int size, bool big_endian,
47		const std::string& filename);
48
49  ~Binary_to_elf();
50
51  // Read contents and create an ELF buffer.  Return true if this
52  // succeeds, false otherwise.
53  bool
54  convert(const Task*);
55
56  // Return a pointer to the contents of the ELF file.
57  const unsigned char*
58  converted_data() const
59  { return this->data_; }
60
61  // Return a pointer to the contents of the ELF file and let the
62  // caller take charge of it.  It was allocated using new[].
63  unsigned char*
64  converted_data_leak()
65  {
66    unsigned char* ret = this->data_;
67    this->data_ = NULL;
68    return ret;
69  }
70
71  // Return the size of the ELF file.
72  size_t
73  converted_size() const
74  { return this->filesize_; }
75
76 private:
77  Binary_to_elf(const Binary_to_elf&);
78  Binary_to_elf& operator=(const Binary_to_elf&);
79
80  template<int size, bool big_endian>
81  bool
82  sized_convert(const Task*);
83
84  template<int size, bool big_endian>
85  void
86  write_file_header(unsigned char**);
87
88  template<int size, bool big_endian>
89  void
90  write_section_header(const char*, const Stringpool_template<char>*,
91		       elfcpp::SHT, unsigned int, section_size_type,
92		       section_size_type, unsigned int, unsigned int,
93		       unsigned int, unsigned int, unsigned char**);
94
95  template<int size, bool big_endian>
96  void
97  write_symbol(const std::string&, const Stringpool_template<char>*,
98	       section_size_type, typename elfcpp::Elf_types<32>::Elf_WXword,
99	       unsigned int, unsigned char**);
100
101  // The ELF machine code of the file to create.
102  elfcpp::EM elf_machine_;
103  // The size of the file to create, 32 or 64.
104  int size_;
105  // Whether to create a big endian file.
106  bool big_endian_;
107  // The name of the file to read.
108  std::string filename_;
109  // The ELF file data, allocated by new [].
110  unsigned char* data_;
111  // The ELF file size.
112  section_size_type filesize_;
113};
114
115} // End namespace gold.
116
117#endif // !defined(GOLD_BINARY_H)
118