summaryrefslogtreecommitdiff
path: root/gold/binary.cc
blob: 54c4939a9a5d549056757ceab02e49de57a6d90e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// binary.cc -- binary input files for gold

// Copyright (C) 2008-2017 Free Software Foundation, Inc.
// Written by Ian Lance Taylor <iant@google.com>.

// This file is part of gold.

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.

#include "gold.h"

#include <cerrno>
#include <cstring>

#include "elfcpp.h"
#include "stringpool.h"
#include "fileread.h"
#include "output.h"
#include "binary.h"

// safe-ctype.h interferes with macros defined by the system <ctype.h>.
// Some C++ system headers might include <ctype.h> and rely on its macro
// definitions being intact.  So make sure that safe-ctype.h is included
// only after any C++ system headers, whether directly here (above) or via
// other local header files (e.g. #include <string> in "binary.h").
#include "safe-ctype.h"

// Support for reading binary files as input.  These become blobs in
// the final output.  These files are treated as though they have a
// single .data section and define three symbols:
// _binary_FILENAME_start, _binary_FILENAME_end, _binary_FILENAME_size.
// The FILENAME is the name of the input file, with any
// non-alphanumeric character changed to an underscore.

// We implement this by creating an ELF file in memory.

namespace gold
{

// class Binary_to_elf.

Binary_to_elf::Binary_to_elf(elfcpp::EM machine, int size, bool big_endian,
			     const std::string& filename)
  : elf_machine_(machine), size_(size), big_endian_(big_endian),
    filename_(filename), data_(NULL), filesize_(0)
{
}

Binary_to_elf::~Binary_to_elf()
{
  if (this->data_ != NULL)
    delete[] this->data_;
}

// Given FILENAME, create a buffer which looks like an ELF file with
// the contents of FILENAME as the contents of the only section.  The
// TASK parameters is mainly for debugging, and records who holds
// locks.

bool
Binary_to_elf::convert(const Task* task)
{
  if (this->size_ == 32)
    {
      if (!this->big_endian_)
	{
#ifdef HAVE_TARGET_32_LITTLE
	  return this->sized_convert<32, false>(task);
#else
	  gold_unreachable();
#endif
	}
      else
	{
#ifdef HAVE_TARGET_32_BIG
	  return this->sized_convert<32, true>(task);
#else
	  gold_unreachable();
#endif
	}
    }
  else if (this->size_ == 64)
    {
      if (!this->big_endian_)
	{
#ifdef HAVE_TARGET_64_LITTLE
	  return this->sized_convert<64, false>(task);
#else
	  gold_unreachable();
#endif
	}
      else
	{
#ifdef HAVE_TARGET_64_BIG
	  return this->sized_convert<64, true>(task);
#else
	  gold_unreachable();
#endif
	}
    }
  else
    gold_unreachable();
}

// We are going to create:
// * The ELF file header.
// * Five sections: null section, .data, .symtab, .strtab, .shstrtab
// * The contents of the file.
// * Four symbols: null, begin, end, size.
// * Three symbol names.
// * Four section names.

template<int size, bool big_endian>
bool
Binary_to_elf::sized_convert(const Task* task)
{
  // Read the input file.

  File_read f;
  if (!f.open(task, this->filename_))
    {
      gold_error(_("cannot open %s: %s:"), this->filename_.c_str(),
		 strerror(errno));
      return false;
    }

  section_size_type filesize = convert_to_section_size_type(f.filesize());
  const unsigned char* fileview;
  if (filesize == 0)
    fileview = NULL;
  else
    fileview = f.get_view(0, 0, filesize, false, false);

  unsigned int align;
  if (size == 32)
    align = 4;
  else if (size == 64)
    align = 8;
  else
    gold_unreachable();
  section_size_type aligned_filesize = align_address(filesize, align);

  // Build the stringpool for the symbol table.

  std::string mangled_name = this->filename_;
  for (std::string::iterator p = mangled_name.begin();
       p != mangled_name.end();
       ++p)
    if (!ISALNUM(*p))
      *p = '_';
  mangled_name = "_binary_" + mangled_name;
  std::string start_symbol_name = mangled_name + "_start";
  std::string end_symbol_name = mangled_name + "_end";
  std::string size_symbol_name = mangled_name + "_size";

  Stringpool strtab;
  strtab.add(start_symbol_name.c_str(), false, NULL);
  strtab.add(end_symbol_name.c_str(), false, NULL);
  strtab.add(size_symbol_name.c_str(), false, NULL);
  strtab.set_string_offsets();

  // Build the stringpool for the section name table.

  Stringpool shstrtab;
  shstrtab.add(".data", false, NULL);
  shstrtab.add(".symtab", false, NULL);
  shstrtab.add(".strtab", false, NULL);
  shstrtab.add(".shstrtab", false, NULL);
  shstrtab.set_string_offsets();

  // Work out the size of the generated file, and the offsets of the
  // various sections, and allocate a buffer.

  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;

  size_t output_size = (elfcpp::Elf_sizes<size>::ehdr_size
			+ 5 * elfcpp::Elf_sizes<size>::shdr_size);
  size_t data_offset = output_size;
  output_size += aligned_filesize;
  size_t symtab_offset = output_size;
  output_size += 4 * sym_size;
  size_t strtab_offset = output_size;
  output_size += strtab.get_strtab_size();
  size_t shstrtab_offset = output_size;
  output_size += shstrtab.get_strtab_size();

  unsigned char* buffer = new unsigned char[output_size];

  // Write out the data.

  unsigned char* pout = buffer;

  this->write_file_header<size, big_endian>(&pout);

  this->write_section_header<size, big_endian>("", &shstrtab, elfcpp::SHT_NULL,
					       0, 0, 0, 0, 0,
					       0, 0, &pout);
  // Having the section be named ".data", having it be writable, and
  // giving it an alignment of 1 is because the GNU linker does it
  // that way, and existing linker script expect it.
  this->write_section_header<size, big_endian>(".data", &shstrtab,
					       elfcpp::SHT_PROGBITS,
					       (elfcpp::SHF_ALLOC
						| elfcpp::SHF_WRITE),
					       data_offset,
					       filesize, 0, 0,
					       1, 0, &pout);
  this->write_section_header<size, big_endian>(".symtab", &shstrtab,
					       elfcpp::SHT_SYMTAB,
					       0, symtab_offset, 4 * sym_size,
					       3, 1, align, sym_size, &pout);
  this->write_section_header<size, big_endian>(".strtab", &shstrtab,
					       elfcpp::SHT_STRTAB,
					       0, strtab_offset,
					       strtab.get_strtab_size(),
					       0, 0, 1, 0, &pout);
  this->write_section_header<size, big_endian>(".shstrtab", &shstrtab,
					       elfcpp::SHT_STRTAB,
					       0, shstrtab_offset,
					       shstrtab.get_strtab_size(),
					       0, 0, 1, 0, &pout);

  if (filesize > 0)
    {
      memcpy(pout, fileview, filesize);
      pout += filesize;
      memset(pout, 0, aligned_filesize - filesize);
      pout += aligned_filesize - filesize;
    }

  this->write_symbol<size, big_endian>("", &strtab, 0, 0, 0, &pout);
  this->write_symbol<size, big_endian>(start_symbol_name, &strtab, 0, filesize,
				       1, &pout);
  this->write_symbol<size, big_endian>(end_symbol_name, &strtab, filesize, 0,
				       1, &pout);
  this->write_symbol<size, big_endian>(size_symbol_name, &strtab, filesize, 0,
				       elfcpp::SHN_ABS, &pout);

  strtab.write_to_buffer(pout, strtab.get_strtab_size());
  pout += strtab.get_strtab_size();

  shstrtab.write_to_buffer(pout, shstrtab.get_strtab_size());
  pout += shstrtab.get_strtab_size();

  gold_assert(static_cast<size_t>(pout - buffer) == output_size);

  this->data_ = buffer;
  this->filesize_ = output_size;

  f.unlock(task);

  return true;
}

// Write out the file header.

template<int size, bool big_endian>
void
Binary_to_elf::write_file_header(unsigned char** ppout)
{
  elfcpp::Ehdr_write<size, big_endian> oehdr(*ppout);

  unsigned char e_ident[elfcpp::EI_NIDENT];
  memset(e_ident, 0, elfcpp::EI_NIDENT);
  e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
  e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
  e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
  e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
  if (size == 32)
    e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
  else if (size == 64)
    e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
  else
    gold_unreachable();
  e_ident[elfcpp::EI_DATA] = (big_endian
			      ? elfcpp::ELFDATA2MSB
			      : elfcpp::ELFDATA2LSB);
  e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
  oehdr.put_e_ident(e_ident);

  oehdr.put_e_type(elfcpp::ET_REL);
  oehdr.put_e_machine(this->elf_machine_);
  oehdr.put_e_version(elfcpp::EV_CURRENT);
  oehdr.put_e_entry(0);
  oehdr.put_e_phoff(0);
  oehdr.put_e_shoff(elfcpp::Elf_sizes<size>::ehdr_size);
  oehdr.put_e_flags(0);
  oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
  oehdr.put_e_phentsize(0);
  oehdr.put_e_phnum(0);
  oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
  oehdr.put_e_shnum(5);
  oehdr.put_e_shstrndx(4);

  *ppout += elfcpp::Elf_sizes<size>::ehdr_size;
}

// Write out a section header.

template<int size, bool big_endian>
void
Binary_to_elf::write_section_header(
    const char* name,
    const Stringpool* shstrtab,
    elfcpp::SHT type,
    unsigned int flags,
    section_size_type offset,
    section_size_type section_size,
    unsigned int link,
    unsigned int info,
    unsigned int addralign,
    unsigned int entsize,
    unsigned char** ppout)
{
  elfcpp::Shdr_write<size, big_endian> oshdr(*ppout);

  oshdr.put_sh_name(*name == '\0' ? 0 : shstrtab->get_offset(name));
  oshdr.put_sh_type(type);
  oshdr.put_sh_flags(flags);
  oshdr.put_sh_addr(0);
  oshdr.put_sh_offset(offset);
  oshdr.put_sh_size(section_size);
  oshdr.put_sh_link(link);
  oshdr.put_sh_info(info);
  oshdr.put_sh_addralign(addralign);
  oshdr.put_sh_entsize(entsize);

  *ppout += elfcpp::Elf_sizes<size>::shdr_size;
}

// Write out a symbol.

template<int size, bool big_endian>
void
Binary_to_elf::write_symbol(
    const std::string& name,
    const Stringpool* strtab,
    section_size_type value,
    typename elfcpp::Elf_types<32>::Elf_WXword st_size,
    unsigned int shndx,
    unsigned char** ppout)
{
  unsigned char* pout = *ppout;

  elfcpp::Sym_write<size, big_endian> osym(pout);
  osym.put_st_name(name.empty() ? 0 : strtab->get_offset(name.c_str()));
  osym.put_st_value(value);
  osym.put_st_size(st_size);
  osym.put_st_info(name.empty() ? elfcpp::STB_LOCAL : elfcpp::STB_GLOBAL,
		   elfcpp::STT_NOTYPE);
  osym.put_st_other(elfcpp::STV_DEFAULT, 0);
  osym.put_st_shndx(shndx);

  *ppout += elfcpp::Elf_sizes<size>::sym_size;
}

} // End namespace gold.