copy-relocs.cc 7.6 KB

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