1 // binary.cc -- binary input files for gold
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
27 #include "safe-ctype.h"
30 #include "stringpool.h"
35 // Support for reading binary files as input. These become blobs in
36 // the final output. These files are treated as though they have a
37 // single .data section and define three symbols:
38 // _binary_FILENAME_start, _binary_FILENAME_end, _binary_FILENAME_end.
39 // The FILENAME is the name of the input file, with any
40 // non-alphanumeric character changed to an underscore.
42 // We implement this by creating an ELF file in memory.
47 // class Binary_to_elf.
49 Binary_to_elf::Binary_to_elf(elfcpp::EM machine, int size, bool big_endian,
50 const std::string& filename)
51 : elf_machine_(machine), size_(size), big_endian_(big_endian),
52 filename_(filename), data_(NULL), filesize_(0)
56 Binary_to_elf::~Binary_to_elf()
58 if (this->data_ != NULL)
62 // Given FILENAME, create a buffer which looks like an ELF file with
63 // the contents of FILENAME as the contents of the only section. The
64 // TASK parameters is mainly for debugging, and records who holds
68 Binary_to_elf::convert(const Task* task)
70 if (this->size_ == 32)
72 if (!this->big_endian_)
74 #ifdef HAVE_TARGET_32_LITTLE
75 return this->sized_convert<32, false>(task);
82 #ifdef HAVE_TARGET_32_BIG
83 return this->sized_convert<32, true>(task);
89 else if (this->size_ == 64)
91 if (!this->big_endian_)
93 #ifdef HAVE_TARGET_64_LITTLE
94 return this->sized_convert<64, false>(task);
101 #ifdef HAVE_TARGET_64_BIG
102 return this->sized_convert<64, true>(task);
112 // We are going to create:
113 // * The ELF file header.
114 // * Five sections: null section, .data, .symtab, .strtab, .shstrtab
115 // * The contents of the file.
116 // * Four symbols: null, begin, end, size.
117 // * Three symbol names.
118 // * Four section names.
120 template<int size, bool big_endian>
122 Binary_to_elf::sized_convert(const Task* task)
124 // Read the input file.
127 if (!f.open(task, this->filename_))
129 gold_error(_("cannot open %s: %s:"), this->filename_.c_str(),
134 section_size_type filesize = convert_to_section_size_type(f.filesize());
135 const unsigned char* fileview = f.get_view(0, 0, filesize, false, false);
144 section_size_type aligned_filesize = align_address(filesize, align);
146 // Build the stringpool for the symbol table.
148 std::string mangled_name = this->filename_;
149 for (std::string::iterator p = mangled_name.begin();
150 p != mangled_name.end();
154 mangled_name = "_binary_" + mangled_name;
155 std::string start_symbol_name = mangled_name + "_start";
156 std::string end_symbol_name = mangled_name + "_end";
157 std::string size_symbol_name = mangled_name + "_size";
160 strtab.add(start_symbol_name.c_str(), false, NULL);
161 strtab.add(end_symbol_name.c_str(), false, NULL);
162 strtab.add(size_symbol_name.c_str(), false, NULL);
163 strtab.set_string_offsets();
165 // Build the stringpool for the section name table.
168 shstrtab.add(".data", false, NULL);
169 shstrtab.add(".symtab", false, NULL);
170 shstrtab.add(".strtab", false, NULL);
171 shstrtab.add(".shstrtab", false, NULL);
172 shstrtab.set_string_offsets();
174 // Work out the size of the generated file, and the offsets of the
175 // various sections, and allocate a buffer.
177 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
179 size_t output_size = (elfcpp::Elf_sizes<size>::ehdr_size
180 + 5 * elfcpp::Elf_sizes<size>::shdr_size);
181 size_t data_offset = output_size;
182 output_size += aligned_filesize;
183 size_t symtab_offset = output_size;
184 output_size += 4 * sym_size;
185 size_t strtab_offset = output_size;
186 output_size += strtab.get_strtab_size();
187 size_t shstrtab_offset = output_size;
188 output_size += shstrtab.get_strtab_size();
190 unsigned char* buffer = new unsigned char[output_size];
192 // Write out the data.
194 unsigned char* pout = buffer;
196 this->write_file_header<size, big_endian>(&pout);
198 this->write_section_header<size, big_endian>("", &shstrtab, elfcpp::SHT_NULL,
201 // Having the section be named ".data", having it be writable, and
202 // giving it an alignment of 1 is because the GNU linker does it
203 // that way, and existing linker script expect it.
204 this->write_section_header<size, big_endian>(".data", &shstrtab,
205 elfcpp::SHT_PROGBITS,
207 | elfcpp::SHF_WRITE),
211 this->write_section_header<size, big_endian>(".symtab", &shstrtab,
213 0, symtab_offset, 4 * sym_size,
214 3, 1, align, sym_size, &pout);
215 this->write_section_header<size, big_endian>(".strtab", &shstrtab,
218 strtab.get_strtab_size(),
220 this->write_section_header<size, big_endian>(".shstrtab", &shstrtab,
223 shstrtab.get_strtab_size(),
226 memcpy(pout, fileview, filesize);
228 memset(pout, 0, aligned_filesize - filesize);
229 pout += aligned_filesize - filesize;
231 this->write_symbol<size, big_endian>("", &strtab, 0, 0, &pout);
232 this->write_symbol<size, big_endian>(start_symbol_name, &strtab, 0, 1,
234 this->write_symbol<size, big_endian>(end_symbol_name, &strtab, filesize, 1,
236 this->write_symbol<size, big_endian>(size_symbol_name, &strtab, filesize,
237 elfcpp::SHN_ABS, &pout);
239 strtab.write_to_buffer(pout, strtab.get_strtab_size());
240 pout += strtab.get_strtab_size();
242 shstrtab.write_to_buffer(pout, shstrtab.get_strtab_size());
243 pout += shstrtab.get_strtab_size();
245 gold_assert(static_cast<size_t>(pout - buffer) == output_size);
247 this->data_ = buffer;
248 this->filesize_ = output_size;
255 // Write out the file header.
257 template<int size, bool big_endian>
259 Binary_to_elf::write_file_header(unsigned char** ppout)
261 elfcpp::Ehdr_write<size, big_endian> oehdr(*ppout);
263 unsigned char e_ident[elfcpp::EI_NIDENT];
264 memset(e_ident, 0, elfcpp::EI_NIDENT);
265 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
266 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
267 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
268 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
270 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
272 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
275 e_ident[elfcpp::EI_DATA] = (big_endian
276 ? elfcpp::ELFDATA2MSB
277 : elfcpp::ELFDATA2LSB);
278 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
279 oehdr.put_e_ident(e_ident);
281 oehdr.put_e_type(elfcpp::ET_REL);
282 oehdr.put_e_machine(this->elf_machine_);
283 oehdr.put_e_version(elfcpp::EV_CURRENT);
284 oehdr.put_e_entry(0);
285 oehdr.put_e_phoff(0);
286 oehdr.put_e_shoff(elfcpp::Elf_sizes<size>::ehdr_size);
287 oehdr.put_e_flags(0);
288 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
289 oehdr.put_e_phentsize(0);
290 oehdr.put_e_phnum(0);
291 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
292 oehdr.put_e_shnum(5);
293 oehdr.put_e_shstrndx(4);
295 *ppout += elfcpp::Elf_sizes<size>::ehdr_size;
298 // Write out a section header.
300 template<int size, bool big_endian>
302 Binary_to_elf::write_section_header(
304 const Stringpool* shstrtab,
307 section_size_type offset,
308 section_size_type section_size,
311 unsigned int addralign,
312 unsigned int entsize,
313 unsigned char** ppout)
315 elfcpp::Shdr_write<size, big_endian> oshdr(*ppout);
317 oshdr.put_sh_name(*name == '\0' ? 0 : shstrtab->get_offset(name));
318 oshdr.put_sh_type(type);
319 oshdr.put_sh_flags(flags);
320 oshdr.put_sh_addr(0);
321 oshdr.put_sh_offset(offset);
322 oshdr.put_sh_size(section_size);
323 oshdr.put_sh_link(link);
324 oshdr.put_sh_info(info);
325 oshdr.put_sh_addralign(addralign);
326 oshdr.put_sh_entsize(entsize);
328 *ppout += elfcpp::Elf_sizes<size>::shdr_size;
331 // Write out a symbol.
333 template<int size, bool big_endian>
335 Binary_to_elf::write_symbol(
336 const std::string& name,
337 const Stringpool* strtab,
338 section_size_type value,
340 unsigned char** ppout)
342 unsigned char* pout = *ppout;
344 elfcpp::Sym_write<size, big_endian> osym(pout);
345 osym.put_st_name(name.empty() ? 0 : strtab->get_offset(name.c_str()));
346 osym.put_st_value(value);
348 osym.put_st_info(name.empty() ? elfcpp::STB_LOCAL : elfcpp::STB_GLOBAL,
350 osym.put_st_other(elfcpp::STV_DEFAULT, 0);
351 osym.put_st_shndx(shndx);
353 *ppout += elfcpp::Elf_sizes<size>::sym_size;
356 } // End namespace gold.