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