binary.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // binary.cc -- binary input files for gold
  2. // Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cerrno>
  19. #include <cstring>
  20. #include "safe-ctype.h"
  21. #include "elfcpp.h"
  22. #include "stringpool.h"
  23. #include "fileread.h"
  24. #include "output.h"
  25. #include "binary.h"
  26. // Support for reading binary files as input. These become blobs in
  27. // the final output. These files are treated as though they have a
  28. // single .data section and define three symbols:
  29. // _binary_FILENAME_start, _binary_FILENAME_end, _binary_FILENAME_size.
  30. // The FILENAME is the name of the input file, with any
  31. // non-alphanumeric character changed to an underscore.
  32. // We implement this by creating an ELF file in memory.
  33. namespace gold
  34. {
  35. // class Binary_to_elf.
  36. Binary_to_elf::Binary_to_elf(elfcpp::EM machine, int size, bool big_endian,
  37. const std::string& filename)
  38. : elf_machine_(machine), size_(size), big_endian_(big_endian),
  39. filename_(filename), data_(NULL), filesize_(0)
  40. {
  41. }
  42. Binary_to_elf::~Binary_to_elf()
  43. {
  44. if (this->data_ != NULL)
  45. delete[] this->data_;
  46. }
  47. // Given FILENAME, create a buffer which looks like an ELF file with
  48. // the contents of FILENAME as the contents of the only section. The
  49. // TASK parameters is mainly for debugging, and records who holds
  50. // locks.
  51. bool
  52. Binary_to_elf::convert(const Task* task)
  53. {
  54. if (this->size_ == 32)
  55. {
  56. if (!this->big_endian_)
  57. {
  58. #ifdef HAVE_TARGET_32_LITTLE
  59. return this->sized_convert<32, false>(task);
  60. #else
  61. gold_unreachable();
  62. #endif
  63. }
  64. else
  65. {
  66. #ifdef HAVE_TARGET_32_BIG
  67. return this->sized_convert<32, true>(task);
  68. #else
  69. gold_unreachable();
  70. #endif
  71. }
  72. }
  73. else if (this->size_ == 64)
  74. {
  75. if (!this->big_endian_)
  76. {
  77. #ifdef HAVE_TARGET_64_LITTLE
  78. return this->sized_convert<64, false>(task);
  79. #else
  80. gold_unreachable();
  81. #endif
  82. }
  83. else
  84. {
  85. #ifdef HAVE_TARGET_64_BIG
  86. return this->sized_convert<64, true>(task);
  87. #else
  88. gold_unreachable();
  89. #endif
  90. }
  91. }
  92. else
  93. gold_unreachable();
  94. }
  95. // We are going to create:
  96. // * The ELF file header.
  97. // * Five sections: null section, .data, .symtab, .strtab, .shstrtab
  98. // * The contents of the file.
  99. // * Four symbols: null, begin, end, size.
  100. // * Three symbol names.
  101. // * Four section names.
  102. template<int size, bool big_endian>
  103. bool
  104. Binary_to_elf::sized_convert(const Task* task)
  105. {
  106. // Read the input file.
  107. File_read f;
  108. if (!f.open(task, this->filename_))
  109. {
  110. gold_error(_("cannot open %s: %s:"), this->filename_.c_str(),
  111. strerror(errno));
  112. return false;
  113. }
  114. section_size_type filesize = convert_to_section_size_type(f.filesize());
  115. const unsigned char* fileview;
  116. if (filesize == 0)
  117. fileview = NULL;
  118. else
  119. fileview = f.get_view(0, 0, filesize, false, false);
  120. unsigned int align;
  121. if (size == 32)
  122. align = 4;
  123. else if (size == 64)
  124. align = 8;
  125. else
  126. gold_unreachable();
  127. section_size_type aligned_filesize = align_address(filesize, align);
  128. // Build the stringpool for the symbol table.
  129. std::string mangled_name = this->filename_;
  130. for (std::string::iterator p = mangled_name.begin();
  131. p != mangled_name.end();
  132. ++p)
  133. if (!ISALNUM(*p))
  134. *p = '_';
  135. mangled_name = "_binary_" + mangled_name;
  136. std::string start_symbol_name = mangled_name + "_start";
  137. std::string end_symbol_name = mangled_name + "_end";
  138. std::string size_symbol_name = mangled_name + "_size";
  139. Stringpool strtab;
  140. strtab.add(start_symbol_name.c_str(), false, NULL);
  141. strtab.add(end_symbol_name.c_str(), false, NULL);
  142. strtab.add(size_symbol_name.c_str(), false, NULL);
  143. strtab.set_string_offsets();
  144. // Build the stringpool for the section name table.
  145. Stringpool shstrtab;
  146. shstrtab.add(".data", false, NULL);
  147. shstrtab.add(".symtab", false, NULL);
  148. shstrtab.add(".strtab", false, NULL);
  149. shstrtab.add(".shstrtab", false, NULL);
  150. shstrtab.set_string_offsets();
  151. // Work out the size of the generated file, and the offsets of the
  152. // various sections, and allocate a buffer.
  153. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  154. size_t output_size = (elfcpp::Elf_sizes<size>::ehdr_size
  155. + 5 * elfcpp::Elf_sizes<size>::shdr_size);
  156. size_t data_offset = output_size;
  157. output_size += aligned_filesize;
  158. size_t symtab_offset = output_size;
  159. output_size += 4 * sym_size;
  160. size_t strtab_offset = output_size;
  161. output_size += strtab.get_strtab_size();
  162. size_t shstrtab_offset = output_size;
  163. output_size += shstrtab.get_strtab_size();
  164. unsigned char* buffer = new unsigned char[output_size];
  165. // Write out the data.
  166. unsigned char* pout = buffer;
  167. this->write_file_header<size, big_endian>(&pout);
  168. this->write_section_header<size, big_endian>("", &shstrtab, elfcpp::SHT_NULL,
  169. 0, 0, 0, 0, 0,
  170. 0, 0, &pout);
  171. // Having the section be named ".data", having it be writable, and
  172. // giving it an alignment of 1 is because the GNU linker does it
  173. // that way, and existing linker script expect it.
  174. this->write_section_header<size, big_endian>(".data", &shstrtab,
  175. elfcpp::SHT_PROGBITS,
  176. (elfcpp::SHF_ALLOC
  177. | elfcpp::SHF_WRITE),
  178. data_offset,
  179. filesize, 0, 0,
  180. 1, 0, &pout);
  181. this->write_section_header<size, big_endian>(".symtab", &shstrtab,
  182. elfcpp::SHT_SYMTAB,
  183. 0, symtab_offset, 4 * sym_size,
  184. 3, 1, align, sym_size, &pout);
  185. this->write_section_header<size, big_endian>(".strtab", &shstrtab,
  186. elfcpp::SHT_STRTAB,
  187. 0, strtab_offset,
  188. strtab.get_strtab_size(),
  189. 0, 0, 1, 0, &pout);
  190. this->write_section_header<size, big_endian>(".shstrtab", &shstrtab,
  191. elfcpp::SHT_STRTAB,
  192. 0, shstrtab_offset,
  193. shstrtab.get_strtab_size(),
  194. 0, 0, 1, 0, &pout);
  195. if (filesize > 0)
  196. {
  197. memcpy(pout, fileview, filesize);
  198. pout += filesize;
  199. memset(pout, 0, aligned_filesize - filesize);
  200. pout += aligned_filesize - filesize;
  201. }
  202. this->write_symbol<size, big_endian>("", &strtab, 0, 0, 0, &pout);
  203. this->write_symbol<size, big_endian>(start_symbol_name, &strtab, 0, filesize,
  204. 1, &pout);
  205. this->write_symbol<size, big_endian>(end_symbol_name, &strtab, filesize, 0,
  206. 1, &pout);
  207. this->write_symbol<size, big_endian>(size_symbol_name, &strtab, filesize, 0,
  208. elfcpp::SHN_ABS, &pout);
  209. strtab.write_to_buffer(pout, strtab.get_strtab_size());
  210. pout += strtab.get_strtab_size();
  211. shstrtab.write_to_buffer(pout, shstrtab.get_strtab_size());
  212. pout += shstrtab.get_strtab_size();
  213. gold_assert(static_cast<size_t>(pout - buffer) == output_size);
  214. this->data_ = buffer;
  215. this->filesize_ = output_size;
  216. f.unlock(task);
  217. return true;
  218. }
  219. // Write out the file header.
  220. template<int size, bool big_endian>
  221. void
  222. Binary_to_elf::write_file_header(unsigned char** ppout)
  223. {
  224. elfcpp::Ehdr_write<size, big_endian> oehdr(*ppout);
  225. unsigned char e_ident[elfcpp::EI_NIDENT];
  226. memset(e_ident, 0, elfcpp::EI_NIDENT);
  227. e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
  228. e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
  229. e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
  230. e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
  231. if (size == 32)
  232. e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
  233. else if (size == 64)
  234. e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
  235. else
  236. gold_unreachable();
  237. e_ident[elfcpp::EI_DATA] = (big_endian
  238. ? elfcpp::ELFDATA2MSB
  239. : elfcpp::ELFDATA2LSB);
  240. e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
  241. oehdr.put_e_ident(e_ident);
  242. oehdr.put_e_type(elfcpp::ET_REL);
  243. oehdr.put_e_machine(this->elf_machine_);
  244. oehdr.put_e_version(elfcpp::EV_CURRENT);
  245. oehdr.put_e_entry(0);
  246. oehdr.put_e_phoff(0);
  247. oehdr.put_e_shoff(elfcpp::Elf_sizes<size>::ehdr_size);
  248. oehdr.put_e_flags(0);
  249. oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
  250. oehdr.put_e_phentsize(0);
  251. oehdr.put_e_phnum(0);
  252. oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
  253. oehdr.put_e_shnum(5);
  254. oehdr.put_e_shstrndx(4);
  255. *ppout += elfcpp::Elf_sizes<size>::ehdr_size;
  256. }
  257. // Write out a section header.
  258. template<int size, bool big_endian>
  259. void
  260. Binary_to_elf::write_section_header(
  261. const char* name,
  262. const Stringpool* shstrtab,
  263. elfcpp::SHT type,
  264. unsigned int flags,
  265. section_size_type offset,
  266. section_size_type section_size,
  267. unsigned int link,
  268. unsigned int info,
  269. unsigned int addralign,
  270. unsigned int entsize,
  271. unsigned char** ppout)
  272. {
  273. elfcpp::Shdr_write<size, big_endian> oshdr(*ppout);
  274. oshdr.put_sh_name(*name == '\0' ? 0 : shstrtab->get_offset(name));
  275. oshdr.put_sh_type(type);
  276. oshdr.put_sh_flags(flags);
  277. oshdr.put_sh_addr(0);
  278. oshdr.put_sh_offset(offset);
  279. oshdr.put_sh_size(section_size);
  280. oshdr.put_sh_link(link);
  281. oshdr.put_sh_info(info);
  282. oshdr.put_sh_addralign(addralign);
  283. oshdr.put_sh_entsize(entsize);
  284. *ppout += elfcpp::Elf_sizes<size>::shdr_size;
  285. }
  286. // Write out a symbol.
  287. template<int size, bool big_endian>
  288. void
  289. Binary_to_elf::write_symbol(
  290. const std::string& name,
  291. const Stringpool* strtab,
  292. section_size_type value,
  293. typename elfcpp::Elf_types<32>::Elf_WXword st_size,
  294. unsigned int shndx,
  295. unsigned char** ppout)
  296. {
  297. unsigned char* pout = *ppout;
  298. elfcpp::Sym_write<size, big_endian> osym(pout);
  299. osym.put_st_name(name.empty() ? 0 : strtab->get_offset(name.c_str()));
  300. osym.put_st_value(value);
  301. osym.put_st_size(st_size);
  302. osym.put_st_info(name.empty() ? elfcpp::STB_LOCAL : elfcpp::STB_GLOBAL,
  303. elfcpp::STT_NOTYPE);
  304. osym.put_st_other(elfcpp::STV_DEFAULT, 0);
  305. osym.put_st_shndx(shndx);
  306. *ppout += elfcpp::Elf_sizes<size>::sym_size;
  307. }
  308. } // End namespace gold.