target.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // target.cc -- target support for gold.
  2. // Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. // Written by Doug Kwan <dougkwan@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 "elfcpp.h"
  19. #include "dynobj.h"
  20. #include "symtab.h"
  21. #include "output.h"
  22. #include "target.h"
  23. namespace gold
  24. {
  25. // Return whether NAME is a local label name. This is used to implement the
  26. // --discard-locals options and can be overridden by child classes to
  27. // implement system-specific behaviour. The logic here is the same as that
  28. // in _bfd_elf_is_local_label_name().
  29. bool
  30. Target::do_is_local_label_name(const char* name) const
  31. {
  32. // Normal local symbols start with ``.L''.
  33. if (name[0] == '.' && name[1] == 'L')
  34. return true;
  35. // At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
  36. // DWARF debugging symbols starting with ``..''.
  37. if (name[0] == '.' && name[1] == '.')
  38. return true;
  39. // gcc will sometimes generate symbols beginning with ``_.L_'' when
  40. // emitting DWARF debugging output. I suspect this is actually a
  41. // small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
  42. // ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
  43. // underscore to be emitted on some ELF targets). For ease of use,
  44. // we treat such symbols as local.
  45. if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
  46. return true;
  47. return false;
  48. }
  49. // Implementations of methods Target::do_make_elf_object are almost identical
  50. // except for the address sizes and endianities. So we extract this
  51. // into a template.
  52. template<int size, bool big_endian>
  53. inline Object*
  54. Target::do_make_elf_object_implementation(
  55. const std::string& name,
  56. Input_file* input_file,
  57. off_t offset,
  58. const elfcpp::Ehdr<size, big_endian>& ehdr)
  59. {
  60. int et = ehdr.get_e_type();
  61. // ET_EXEC files are valid input for --just-symbols/-R,
  62. // and we treat them as relocatable objects.
  63. if (et == elfcpp::ET_REL
  64. || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
  65. {
  66. Sized_relobj_file<size, big_endian>* obj =
  67. new Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr);
  68. obj->setup();
  69. return obj;
  70. }
  71. else if (et == elfcpp::ET_DYN)
  72. {
  73. Sized_dynobj<size, big_endian>* obj =
  74. new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
  75. obj->setup();
  76. return obj;
  77. }
  78. else
  79. {
  80. gold_error(_("%s: unsupported ELF file type %d"),
  81. name.c_str(), et);
  82. return NULL;
  83. }
  84. }
  85. // Make an ELF object called NAME by reading INPUT_FILE at OFFSET. EHDR
  86. // is the ELF header of the object. There are four versions of this
  87. // for different address sizes and endianities.
  88. #ifdef HAVE_TARGET_32_LITTLE
  89. Object*
  90. Target::do_make_elf_object(const std::string& name, Input_file* input_file,
  91. off_t offset, const elfcpp::Ehdr<32, false>& ehdr)
  92. {
  93. return this->do_make_elf_object_implementation<32, false>(name, input_file,
  94. offset, ehdr);
  95. }
  96. #endif
  97. #ifdef HAVE_TARGET_32_BIG
  98. Object*
  99. Target::do_make_elf_object(const std::string& name, Input_file* input_file,
  100. off_t offset, const elfcpp::Ehdr<32, true>& ehdr)
  101. {
  102. return this->do_make_elf_object_implementation<32, true>(name, input_file,
  103. offset, ehdr);
  104. }
  105. #endif
  106. #ifdef HAVE_TARGET_64_LITTLE
  107. Object*
  108. Target::do_make_elf_object(const std::string& name, Input_file* input_file,
  109. off_t offset, const elfcpp::Ehdr<64, false>& ehdr)
  110. {
  111. return this->do_make_elf_object_implementation<64, false>(name, input_file,
  112. offset, ehdr);
  113. }
  114. #endif
  115. #ifdef HAVE_TARGET_64_BIG
  116. Object*
  117. Target::do_make_elf_object(const std::string& name, Input_file* input_file,
  118. off_t offset, const elfcpp::Ehdr<64, true>& ehdr)
  119. {
  120. return this->do_make_elf_object_implementation<64, true>(name, input_file,
  121. offset, ehdr);
  122. }
  123. #endif
  124. Output_section*
  125. Target::do_make_output_section(const char* name, elfcpp::Elf_Word type,
  126. elfcpp::Elf_Xword flags)
  127. {
  128. return new Output_section(name, type, flags);
  129. }
  130. // Default for whether a reloc is a call to a non-split function is
  131. // whether the symbol is a function.
  132. bool
  133. Target::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
  134. {
  135. return sym->type() == elfcpp::STT_FUNC;
  136. }
  137. // Default conversion for -fsplit-stack is to give an error.
  138. void
  139. Target::do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
  140. section_size_type, unsigned char*, section_size_type,
  141. std::string*, std::string*) const
  142. {
  143. static bool warned;
  144. if (!warned)
  145. {
  146. gold_error(_("linker does not include stack split support "
  147. "required by %s"),
  148. object->name().c_str());
  149. warned = true;
  150. }
  151. }
  152. // Return whether BYTES/LEN matches VIEW/VIEW_SIZE at OFFSET.
  153. bool
  154. Target::match_view(const unsigned char* view, section_size_type view_size,
  155. section_offset_type offset, const char* bytes,
  156. size_t len) const
  157. {
  158. if (offset + len > view_size)
  159. return false;
  160. return memcmp(view + offset, bytes, len) == 0;
  161. }
  162. // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
  163. // for LEN bytes.
  164. void
  165. Target::set_view_to_nop(unsigned char* view, section_size_type view_size,
  166. section_offset_type offset, size_t len) const
  167. {
  168. gold_assert(offset >= 0 && offset + len <= view_size);
  169. if (!this->has_code_fill())
  170. memset(view + offset, 0, len);
  171. else
  172. {
  173. std::string fill = this->code_fill(len);
  174. memcpy(view + offset, fill.data(), len);
  175. }
  176. }
  177. // Return address and size to plug into eh_frame FDEs associated with a PLT.
  178. void
  179. Target::do_plt_fde_location(const Output_data* plt, unsigned char*,
  180. uint64_t* address, off_t* len) const
  181. {
  182. *address = plt->address();
  183. *len = plt->data_size();
  184. }
  185. // Class Sized_target.
  186. // Set the EI_OSABI field of the ELF header if requested.
  187. template<int size, bool big_endian>
  188. void
  189. Sized_target<size, big_endian>::do_adjust_elf_header(unsigned char* view,
  190. int len)
  191. {
  192. elfcpp::ELFOSABI osabi = this->osabi();
  193. if (osabi != elfcpp::ELFOSABI_NONE)
  194. {
  195. gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
  196. elfcpp::Ehdr<size, big_endian> ehdr(view);
  197. unsigned char e_ident[elfcpp::EI_NIDENT];
  198. memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
  199. e_ident[elfcpp::EI_OSABI] = osabi;
  200. elfcpp::Ehdr_write<size, big_endian> oehdr(view);
  201. oehdr.put_e_ident(e_ident);
  202. }
  203. }
  204. #ifdef HAVE_TARGET_32_LITTLE
  205. template
  206. class Sized_target<32, false>;
  207. #endif
  208. #ifdef HAVE_TARGET_32_BIG
  209. template
  210. class Sized_target<32, true>;
  211. #endif
  212. #ifdef HAVE_TARGET_64_LITTLE
  213. template
  214. class Sized_target<64, false>;
  215. #endif
  216. #ifdef HAVE_TARGET_64_BIG
  217. template
  218. class Sized_target<64, true>;
  219. #endif
  220. } // End namespace gold.