target.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // target.cc -- target support for gold.
  2. // Copyright (C) 2009-2022 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, const unsigned char*,
  134. const unsigned char*, section_size_type) const
  135. {
  136. return sym->type() == elfcpp::STT_FUNC;
  137. }
  138. // Default conversion for -fsplit-stack is to give an error.
  139. void
  140. Target::do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
  141. section_size_type, const unsigned char*, size_t,
  142. unsigned char*, section_size_type,
  143. std::string*, std::string*) const
  144. {
  145. static bool warned;
  146. if (!warned)
  147. {
  148. gold_error(_("linker does not include stack split support "
  149. "required by %s"),
  150. object->name().c_str());
  151. warned = true;
  152. }
  153. }
  154. // Return whether BYTES/LEN matches VIEW/VIEW_SIZE at OFFSET.
  155. bool
  156. Target::match_view(const unsigned char* view, section_size_type view_size,
  157. section_offset_type offset, const char* bytes,
  158. size_t len) const
  159. {
  160. if (offset + len > view_size)
  161. return false;
  162. return memcmp(view + offset, bytes, len) == 0;
  163. }
  164. // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
  165. // for LEN bytes.
  166. void
  167. Target::set_view_to_nop(unsigned char* view, section_size_type view_size,
  168. section_offset_type offset, size_t len) const
  169. {
  170. gold_assert(offset >= 0 && offset + len <= view_size);
  171. if (!this->has_code_fill())
  172. memset(view + offset, 0, len);
  173. else
  174. {
  175. std::string fill = this->code_fill(len);
  176. memcpy(view + offset, fill.data(), len);
  177. }
  178. }
  179. // Return address and size to plug into eh_frame FDEs associated with a PLT.
  180. void
  181. Target::do_plt_fde_location(const Output_data* plt, unsigned char*,
  182. uint64_t* address, off_t* len) const
  183. {
  184. *address = plt->address();
  185. *len = plt->data_size();
  186. }
  187. // Class Sized_target.
  188. // Set the EI_OSABI field of the ELF header if requested.
  189. template<int size, bool big_endian>
  190. void
  191. Sized_target<size, big_endian>::do_adjust_elf_header(unsigned char* view,
  192. int len)
  193. {
  194. elfcpp::ELFOSABI osabi = this->osabi();
  195. if (osabi != elfcpp::ELFOSABI_NONE)
  196. {
  197. gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
  198. elfcpp::Ehdr<size, big_endian> ehdr(view);
  199. unsigned char e_ident[elfcpp::EI_NIDENT];
  200. memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
  201. e_ident[elfcpp::EI_OSABI] = osabi;
  202. elfcpp::Ehdr_write<size, big_endian> oehdr(view);
  203. oehdr.put_e_ident(e_ident);
  204. }
  205. }
  206. #ifdef HAVE_TARGET_32_LITTLE
  207. template
  208. class Sized_target<32, false>;
  209. #endif
  210. #ifdef HAVE_TARGET_32_BIG
  211. template
  212. class Sized_target<32, true>;
  213. #endif
  214. #ifdef HAVE_TARGET_64_LITTLE
  215. template
  216. class Sized_target<64, false>;
  217. #endif
  218. #ifdef HAVE_TARGET_64_BIG
  219. template
  220. class Sized_target<64, true>;
  221. #endif
  222. } // End namespace gold.