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