binary.cc 11 KB

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