binary.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // binary.h -- binary input files for gold -*- C++ -*-
  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. // Support binary input files by making them look like an ELF file.
  18. #ifndef GOLD_BINARY_H
  19. #define GOLD_BINARY_H
  20. #include <string>
  21. #include "elfcpp.h"
  22. namespace gold
  23. {
  24. class Task;
  25. template<typename Stringpool_char>
  26. class Stringpool_template;
  27. // This class takes a file name and creates a buffer which looks like
  28. // an ELF file read into memory.
  29. class Binary_to_elf
  30. {
  31. public:
  32. Binary_to_elf(elfcpp::EM machine, int size, bool big_endian,
  33. const std::string& filename);
  34. ~Binary_to_elf();
  35. // Read contents and create an ELF buffer. Return true if this
  36. // succeeds, false otherwise.
  37. bool
  38. convert(const Task*);
  39. // Return a pointer to the contents of the ELF file.
  40. const unsigned char*
  41. converted_data() const
  42. { return this->data_; }
  43. // Return a pointer to the contents of the ELF file and let the
  44. // caller take charge of it. It was allocated using new[].
  45. unsigned char*
  46. converted_data_leak()
  47. {
  48. unsigned char* ret = this->data_;
  49. this->data_ = NULL;
  50. return ret;
  51. }
  52. // Return the size of the ELF file.
  53. size_t
  54. converted_size() const
  55. { return this->filesize_; }
  56. private:
  57. Binary_to_elf(const Binary_to_elf&);
  58. Binary_to_elf& operator=(const Binary_to_elf&);
  59. template<int size, bool big_endian>
  60. bool
  61. sized_convert(const Task*);
  62. template<int size, bool big_endian>
  63. void
  64. write_file_header(unsigned char**);
  65. template<int size, bool big_endian>
  66. void
  67. write_section_header(const char*, const Stringpool_template<char>*,
  68. elfcpp::SHT, unsigned int, section_size_type,
  69. section_size_type, unsigned int, unsigned int,
  70. unsigned int, unsigned int, unsigned char**);
  71. template<int size, bool big_endian>
  72. void
  73. write_symbol(const std::string&, const Stringpool_template<char>*,
  74. section_size_type, typename elfcpp::Elf_types<32>::Elf_WXword,
  75. unsigned int, unsigned char**);
  76. // The ELF machine code of the file to create.
  77. elfcpp::EM elf_machine_;
  78. // The size of the file to create, 32 or 64.
  79. int size_;
  80. // Whether to create a big endian file.
  81. bool big_endian_;
  82. // The name of the file to read.
  83. std::string filename_;
  84. // The ELF file data, allocated by new [].
  85. unsigned char* data_;
  86. // The ELF file size.
  87. section_size_type filesize_;
  88. };
  89. } // End namespace gold.
  90. #endif // !defined(GOLD_BINARY_H)