copy-relocs.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // copy-relocs.h -- handle COPY relocations for gold -*- C++ -*-
  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. #ifndef GOLD_COPY_RELOCS_H
  18. #define GOLD_COPY_RELOCS_H
  19. #include "elfcpp.h"
  20. #include "reloc-types.h"
  21. #include "output.h"
  22. namespace gold
  23. {
  24. // This class is used to manage COPY relocations. We try to avoid
  25. // them when possible. A COPY relocation may be required when an
  26. // executable refers to a variable defined in a shared library. COPY
  27. // relocations are problematic because they tie the executable to the
  28. // exact size of the variable in the shared library. We can avoid
  29. // them if all the references to the variable are in a writeable
  30. // section. In that case we can simply use dynamic relocations.
  31. // However, when scanning relocs, we don't know when we see the
  32. // relocation whether we will be forced to use a COPY relocation or
  33. // not. So we have to save the relocation during the reloc scanning,
  34. // and then emit it as a dynamic relocation if necessary. This class
  35. // implements that. It is used by the target specific code.
  36. // The template parameter SH_TYPE is the type of the reloc section to
  37. // be used for COPY relocs: elfcpp::SHT_REL or elfcpp::SHT_RELA.
  38. template<int sh_type, int size, bool big_endian>
  39. class Copy_relocs
  40. {
  41. private:
  42. typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc;
  43. public:
  44. Copy_relocs(unsigned int copy_reloc_type)
  45. : entries_(), copy_reloc_type_(copy_reloc_type), dynbss_(NULL)
  46. { }
  47. // This is called while scanning relocs if we see a relocation
  48. // against a symbol which may force us to generate a COPY reloc.
  49. // SYM is the symbol. OBJECT is the object whose relocs we are
  50. // scanning. The relocation is being applied to section SHNDX in
  51. // OBJECT. OUTPUT_SECTION is the output section where section SHNDX
  52. // will wind up. REL is the reloc itself. The Output_data_reloc
  53. // section is where the dynamic relocs are put.
  54. void
  55. copy_reloc(Symbol_table*, Layout*, Sized_symbol<size>* sym,
  56. Sized_relobj_file<size, big_endian>* object,
  57. unsigned int shndx, Output_section* output_section,
  58. const Reloc& rel,
  59. Output_data_reloc<sh_type, true, size, big_endian>*);
  60. // Return whether there are any saved relocations.
  61. bool
  62. any_saved_relocs() const
  63. { return !this->entries_.empty(); }
  64. // Emit any saved relocations which turn out to be needed. This is
  65. // called after all the relocs have been scanned.
  66. void
  67. emit(Output_data_reloc<sh_type, true, size, big_endian>*);
  68. // Emit a COPY reloc.
  69. void
  70. emit_copy_reloc(Symbol_table*, Sized_symbol<size>*,
  71. Output_data*, off_t,
  72. Output_data_reloc<sh_type, true, size, big_endian>*);
  73. protected:
  74. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  75. typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
  76. // This POD class holds the relocations we are saving. We will emit
  77. // these relocations if it turns out that the symbol does not
  78. // require a COPY relocation.
  79. struct Copy_reloc_entry
  80. {
  81. Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
  82. Sized_relobj_file<size, big_endian>* relobj,
  83. unsigned int shndx,
  84. Output_section* output_section,
  85. Address address, Addend addend)
  86. : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
  87. shndx_(shndx), output_section_(output_section),
  88. address_(address), addend_(addend)
  89. { }
  90. Symbol* sym_;
  91. unsigned int reloc_type_;
  92. Sized_relobj_file<size, big_endian>* relobj_;
  93. unsigned int shndx_;
  94. Output_section* output_section_;
  95. Address address_;
  96. Addend addend_;
  97. };
  98. // Make a new COPY reloc and emit it.
  99. void
  100. make_copy_reloc(Symbol_table*, Layout*, Sized_symbol<size>*,
  101. Output_data_reloc<sh_type, true, size, big_endian>*);
  102. // A list of relocs to be saved.
  103. typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
  104. // The list of relocs we are saving.
  105. Copy_reloc_entries entries_;
  106. private:
  107. // Return whether we need a COPY reloc.
  108. bool
  109. need_copy_reloc(Sized_symbol<size>* gsym,
  110. Sized_relobj_file<size, big_endian>* object,
  111. unsigned int shndx) const;
  112. // Save a reloc against SYM for possible emission later.
  113. void
  114. save(Symbol*, Sized_relobj_file<size, big_endian>*, unsigned int shndx,
  115. Output_section*, const Reloc& rel);
  116. // The target specific relocation type of the COPY relocation.
  117. const unsigned int copy_reloc_type_;
  118. // The dynamic BSS data which goes into the .bss section. This is
  119. // where variables which require COPY relocations are placed.
  120. Output_data_space* dynbss_;
  121. };
  122. } // End namespace gold.
  123. #endif // !defined(GOLD_COPY_RELOCS_H)