gas/
[binutils.git] / gold / copy-relocs.h
blobd1e2323b25c6baffc4970621f5f9d926111043e4
1 // copy-relocs.h -- handle COPY relocations for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #ifndef GOLD_COPY_RELOCS_H
24 #define GOLD_COPY_RELOCS_H
26 #include "elfcpp.h"
27 #include "reloc-types.h"
28 #include "output.h"
30 namespace gold
33 // This class is used to manage COPY relocations. We try to avoid
34 // them when possible. A COPY relocation may be required when an
35 // executable refers to a variable defined in a shared library. COPY
36 // relocations are problematic because they tie the executable to the
37 // exact size of the variable in the shared library. We can avoid
38 // them if all the references to the variable are in a writeable
39 // section. In that case we can simply use dynamic relocations.
40 // However, when scanning relocs, we don't know when we see the
41 // relocation whether we will be forced to use a COPY relocation or
42 // not. So we have to save the relocation during the reloc scanning,
43 // and then emit it as a dynamic relocation if necessary. This class
44 // implements that. It is used by the target specific code.
46 // The template parameter SH_TYPE is the type of the reloc section to
47 // be used for COPY relocs: elfcpp::SHT_REL or elfcpp::SHT_RELA.
49 template<int sh_type, int size, bool big_endian>
50 class Copy_relocs
52 private:
53 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc;
55 public:
56 Copy_relocs(unsigned int copy_reloc_type)
57 : copy_reloc_type_(copy_reloc_type), dynbss_(NULL), entries_()
58 { }
60 // This is called while scanning relocs if we see a relocation
61 // against a symbol which may force us to generate a COPY reloc.
62 // SYM is the symbol. OBJECT is the object whose relocs we are
63 // scanning. The relocation is being applied to section SHNDX in
64 // OBJECT. OUTPUT_SECTION is the output section where section SHNDX
65 // will wind up. REL is the reloc itself. The Output_data_reloc
66 // section is where the dynamic relocs are put.
67 void
68 copy_reloc(Symbol_table*, Layout*, Sized_symbol<size>* sym,
69 Sized_relobj_file<size, big_endian>* object,
70 unsigned int shndx, Output_section* output_section,
71 const Reloc& rel,
72 Output_data_reloc<sh_type, true, size, big_endian>*);
74 // Return whether there are any saved relocations.
75 bool
76 any_saved_relocs() const
77 { return !this->entries_.empty(); }
79 // Emit any saved relocations which turn out to be needed. This is
80 // called after all the relocs have been scanned.
81 void
82 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
84 // Emit a COPY reloc.
85 void
86 emit_copy_reloc(Symbol_table*, Sized_symbol<size>*,
87 Output_data*, off_t,
88 Output_data_reloc<sh_type, true, size, big_endian>*);
90 private:
91 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
92 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
94 // This POD class holds the relocations we are saving. We will emit
95 // these relocations if it turns out that the symbol does not
96 // require a COPY relocation.
97 class Copy_reloc_entry
99 public:
100 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
101 Sized_relobj_file<size, big_endian>* relobj,
102 unsigned int shndx,
103 Output_section* output_section,
104 Address address, Addend addend)
105 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
106 shndx_(shndx), output_section_(output_section),
107 address_(address), addend_(addend)
110 // Emit this reloc if appropriate. This is called after we have
111 // scanned all the relocations, so we know whether we emitted a
112 // COPY relocation for SYM_.
113 void
114 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
116 private:
117 Symbol* sym_;
118 unsigned int reloc_type_;
119 Sized_relobj_file<size, big_endian>* relobj_;
120 unsigned int shndx_;
121 Output_section* output_section_;
122 Address address_;
123 Addend addend_;
126 // A list of relocs to be saved.
127 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
129 // Return whether we need a COPY reloc.
130 bool
131 need_copy_reloc(Sized_symbol<size>* gsym,
132 Sized_relobj_file<size, big_endian>* object,
133 unsigned int shndx) const;
135 // Make a new COPY reloc and emit it.
136 void
137 make_copy_reloc(Symbol_table*, Layout*, Sized_symbol<size>*,
138 Output_data_reloc<sh_type, true, size, big_endian>*);
140 // Save a reloc against SYM for possible emission later.
141 void
142 save(Symbol*, Sized_relobj_file<size, big_endian>*, unsigned int shndx,
143 Output_section*, const Reloc& rel);
145 // The target specific relocation type of the COPY relocation.
146 const unsigned int copy_reloc_type_;
147 // The dynamic BSS data which goes into the .bss section. This is
148 // where variables which require COPY relocations are placed.
149 Output_data_space* dynbss_;
150 // The list of relocs we are saving.
151 Copy_reloc_entries entries_;
154 } // End namespace gold.
156 #endif // !defined(GOLD_COPY_RELOCS_H)