copy-relocs.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // copy-relocs.cc -- handle COPY relocations for gold.
  2. // Copyright (C) 2006-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 "symtab.h"
  19. #include "copy-relocs.h"
  20. namespace gold
  21. {
  22. // Copy_relocs methods.
  23. // Handle a relocation against a symbol which may force us to generate
  24. // a COPY reloc.
  25. template<int sh_type, int size, bool big_endian>
  26. void
  27. Copy_relocs<sh_type, size, big_endian>::copy_reloc(
  28. Symbol_table* symtab,
  29. Layout* layout,
  30. Sized_symbol<size>* sym,
  31. Sized_relobj_file<size, big_endian>* object,
  32. unsigned int shndx,
  33. Output_section* output_section,
  34. unsigned int r_type,
  35. typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
  36. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
  37. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
  38. {
  39. if (this->need_copy_reloc(sym, object, shndx))
  40. this->make_copy_reloc(symtab, layout, sym, object, reloc_section);
  41. else
  42. {
  43. // We may not need a COPY relocation. Save this relocation to
  44. // possibly be emitted later.
  45. this->save(sym, object, shndx, output_section,
  46. r_type, r_offset, r_addend);
  47. }
  48. }
  49. // Return whether we need a COPY reloc for a relocation against SYM.
  50. // The relocation is begin applied to section SHNDX in OBJECT.
  51. template<int sh_type, int size, bool big_endian>
  52. bool
  53. Copy_relocs<sh_type, size, big_endian>::need_copy_reloc(
  54. Sized_symbol<size>* sym,
  55. Sized_relobj_file<size, big_endian>* object,
  56. unsigned int shndx) const
  57. {
  58. if (!parameters->options().copyreloc())
  59. return false;
  60. if (sym->symsize() == 0)
  61. return false;
  62. // If this is a readonly section, then we need a COPY reloc.
  63. // Otherwise we can use a dynamic reloc. Note that calling
  64. // section_flags here can be slow, as the information is not cached;
  65. // fortunately we shouldn't see too many potential COPY relocs.
  66. if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
  67. return true;
  68. return false;
  69. }
  70. // Emit a COPY relocation for SYM.
  71. template<int sh_type, int size, bool big_endian>
  72. void
  73. Copy_relocs<sh_type, size, big_endian>::emit_copy_reloc(
  74. Symbol_table* symtab,
  75. Sized_symbol<size>* sym,
  76. Output_data* posd,
  77. off_t offset,
  78. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
  79. {
  80. // Define the symbol as being copied.
  81. symtab->define_with_copy_reloc(sym, posd, offset);
  82. // Add the COPY relocation to the dynamic reloc section.
  83. reloc_section->add_global_generic(sym, this->copy_reloc_type_, posd,
  84. offset, 0);
  85. }
  86. // Make a COPY relocation for SYM and emit it.
  87. template<int sh_type, int size, bool big_endian>
  88. void
  89. Copy_relocs<sh_type, size, big_endian>::make_copy_reloc(
  90. Symbol_table* symtab,
  91. Layout* layout,
  92. Sized_symbol<size>* sym,
  93. Sized_relobj_file<size, big_endian>* object,
  94. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
  95. {
  96. // We should not be here if -z nocopyreloc is given.
  97. gold_assert(parameters->options().copyreloc());
  98. gold_assert(sym->is_from_dynobj());
  99. // The symbol must not have protected visibility.
  100. if (sym->is_protected())
  101. {
  102. gold_error(_("%s: cannot make copy relocation for "
  103. "protected symbol '%s', defined in %s"),
  104. object->name().c_str(),
  105. sym->name(),
  106. sym->object()->name().c_str());
  107. }
  108. typename elfcpp::Elf_types<size>::Elf_WXword symsize = sym->symsize();
  109. // There is no defined way to determine the required alignment of
  110. // the symbol. We know that the symbol is defined in a dynamic
  111. // object. We start with the alignment of the section in which it
  112. // is defined; presumably we do not require an alignment larger than
  113. // that. Then we reduce that alignment if the symbol is not aligned
  114. // within the section.
  115. bool is_ordinary;
  116. unsigned int shndx = sym->shndx(&is_ordinary);
  117. gold_assert(is_ordinary);
  118. typename elfcpp::Elf_types<size>::Elf_WXword addralign;
  119. bool is_readonly = false;
  120. {
  121. // Lock the object so we can read from it. This is only called
  122. // single-threaded from scan_relocs, so it is OK to lock.
  123. // Unfortunately we have no way to pass in a Task token.
  124. const Task* dummy_task = reinterpret_cast<const Task*>(-1);
  125. Object* obj = sym->object();
  126. Task_lock_obj<Object> tl(dummy_task, obj);
  127. addralign = obj->section_addralign(shndx);
  128. if (parameters->options().relro())
  129. {
  130. if ((obj->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
  131. is_readonly = true;
  132. else
  133. {
  134. // Symbols in .data.rel.ro should also be treated as read-only.
  135. if (obj->section_name(shndx) == ".data.rel.ro")
  136. is_readonly = true;
  137. }
  138. }
  139. }
  140. typename Sized_symbol<size>::Value_type value = sym->value();
  141. while ((value & (addralign - 1)) != 0)
  142. addralign >>= 1;
  143. // Mark the dynamic object as needed for the --as-needed option.
  144. sym->object()->set_is_needed();
  145. Output_data_space* dynbss;
  146. if (is_readonly)
  147. {
  148. if (this->dynrelro_ == NULL)
  149. {
  150. this->dynrelro_ = new Output_data_space(addralign, "** dynrelro");
  151. layout->add_output_section_data(".data.rel.ro",
  152. elfcpp::SHT_PROGBITS,
  153. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  154. this->dynrelro_, ORDER_RELRO, false);
  155. }
  156. dynbss = this->dynrelro_;
  157. }
  158. else
  159. {
  160. if (this->dynbss_ == NULL)
  161. {
  162. this->dynbss_ = new Output_data_space(addralign, "** dynbss");
  163. layout->add_output_section_data(".bss",
  164. elfcpp::SHT_NOBITS,
  165. elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
  166. this->dynbss_, ORDER_BSS, false);
  167. }
  168. dynbss = this->dynbss_;
  169. }
  170. if (addralign > dynbss->addralign())
  171. dynbss->set_space_alignment(addralign);
  172. section_size_type dynbss_size =
  173. convert_to_section_size_type(dynbss->current_data_size());
  174. dynbss_size = align_address(dynbss_size, addralign);
  175. section_size_type offset = dynbss_size;
  176. dynbss->set_current_data_size(dynbss_size + symsize);
  177. this->emit_copy_reloc(symtab, sym, dynbss, offset, reloc_section);
  178. }
  179. // Save a relocation to possibly be emitted later.
  180. template<int sh_type, int size, bool big_endian>
  181. void
  182. Copy_relocs<sh_type, size, big_endian>::save(
  183. Symbol* sym,
  184. Sized_relobj_file<size, big_endian>* object,
  185. unsigned int shndx,
  186. Output_section* output_section,
  187. unsigned int r_type,
  188. typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
  189. typename elfcpp::Elf_types<size>::Elf_Swxword r_addend)
  190. {
  191. this->entries_.push_back(Copy_reloc_entry(sym, r_type, object, shndx,
  192. output_section, r_offset,
  193. r_addend));
  194. }
  195. // Emit any saved relocs.
  196. template<int sh_type, int size, bool big_endian>
  197. void
  198. Copy_relocs<sh_type, size, big_endian>::emit(
  199. Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
  200. {
  201. for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
  202. p != this->entries_.end();
  203. ++p)
  204. {
  205. Copy_reloc_entry& entry = *p;
  206. // If the symbol is no longer defined in a dynamic object, then we
  207. // emitted a COPY relocation, and we do not want to emit this
  208. // dynamic relocation.
  209. if (entry.sym_->is_from_dynobj())
  210. reloc_section->add_global_generic(entry.sym_, entry.reloc_type_,
  211. entry.output_section_, entry.relobj_,
  212. entry.shndx_, entry.address_,
  213. entry.addend_);
  214. }
  215. // We no longer need the saved information.
  216. this->entries_.clear();
  217. }
  218. // Instantiate the templates we need.
  219. #ifdef HAVE_TARGET_32_LITTLE
  220. template
  221. class Copy_relocs<elfcpp::SHT_REL, 32, false>;
  222. template
  223. class Copy_relocs<elfcpp::SHT_RELA, 32, false>;
  224. #endif
  225. #ifdef HAVE_TARGET_32_BIG
  226. template
  227. class Copy_relocs<elfcpp::SHT_REL, 32, true>;
  228. template
  229. class Copy_relocs<elfcpp::SHT_RELA, 32, true>;
  230. #endif
  231. #ifdef HAVE_TARGET_64_LITTLE
  232. template
  233. class Copy_relocs<elfcpp::SHT_REL, 64, false>;
  234. template
  235. class Copy_relocs<elfcpp::SHT_RELA, 64, false>;
  236. #endif
  237. #ifdef HAVE_TARGET_64_BIG
  238. template
  239. class Copy_relocs<elfcpp::SHT_REL, 64, true>;
  240. template
  241. class Copy_relocs<elfcpp::SHT_RELA, 64, true>;
  242. #endif
  243. } // End namespace gold.