Automatic date update in version.in
[deliverable/binutils-gdb.git] / gold / copy-relocs.h
CommitLineData
12c0daef
ILT
1// copy-relocs.h -- handle COPY relocations for gold -*- C++ -*-
2
6f2750fe 3// Copyright (C) 2006-2016 Free Software Foundation, Inc.
12c0daef
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
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.
12
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.
17
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.
22
23#ifndef GOLD_COPY_RELOCS_H
24#define GOLD_COPY_RELOCS_H
25
26#include "elfcpp.h"
27#include "reloc-types.h"
28#include "output.h"
29
30namespace gold
31{
32
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.
45
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.
48
49template<int sh_type, int size, bool big_endian>
50class Copy_relocs
51{
52 private:
53 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc;
54
55 public:
56 Copy_relocs(unsigned int copy_reloc_type)
91f43acd 57 : entries_(), copy_reloc_type_(copy_reloc_type), dynbss_(NULL)
12c0daef
ILT
58 { }
59
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
859d7987
CC
68 copy_reloc(Symbol_table*,
69 Layout*,
70 Sized_symbol<size>* sym,
6fa2a40b 71 Sized_relobj_file<size, big_endian>* object,
859d7987
CC
72 unsigned int shndx,
73 Output_section* output_section,
74 unsigned int r_type,
75 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
76 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
12c0daef
ILT
77 Output_data_reloc<sh_type, true, size, big_endian>*);
78
79 // Return whether there are any saved relocations.
80 bool
81 any_saved_relocs() const
82 { return !this->entries_.empty(); }
83
84 // Emit any saved relocations which turn out to be needed. This is
85 // called after all the relocs have been scanned.
86 void
87 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
88
26d3c67d
CC
89 // Emit a COPY reloc.
90 void
91 emit_copy_reloc(Symbol_table*, Sized_symbol<size>*,
92 Output_data*, off_t,
93 Output_data_reloc<sh_type, true, size, big_endian>*);
94
91f43acd 95 protected:
12c0daef
ILT
96 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
97 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
98
99 // This POD class holds the relocations we are saving. We will emit
100 // these relocations if it turns out that the symbol does not
101 // require a COPY relocation.
91f43acd 102 struct Copy_reloc_entry
12c0daef 103 {
12c0daef 104 Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
6fa2a40b 105 Sized_relobj_file<size, big_endian>* relobj,
ef9beddf 106 unsigned int shndx,
12c0daef
ILT
107 Output_section* output_section,
108 Address address, Addend addend)
109 : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
110 shndx_(shndx), output_section_(output_section),
111 address_(address), addend_(addend)
112 { }
113
12c0daef
ILT
114 Symbol* sym_;
115 unsigned int reloc_type_;
6fa2a40b 116 Sized_relobj_file<size, big_endian>* relobj_;
12c0daef
ILT
117 unsigned int shndx_;
118 Output_section* output_section_;
119 Address address_;
120 Addend addend_;
121 };
122
91f43acd
CC
123 // Make a new COPY reloc and emit it.
124 void
125 make_copy_reloc(Symbol_table*, Layout*, Sized_symbol<size>*,
6eeb0170 126 Sized_relobj_file<size, big_endian>* object,
91f43acd
CC
127 Output_data_reloc<sh_type, true, size, big_endian>*);
128
12c0daef
ILT
129 // A list of relocs to be saved.
130 typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
131
91f43acd
CC
132 // The list of relocs we are saving.
133 Copy_reloc_entries entries_;
134
135 private:
12c0daef
ILT
136 // Return whether we need a COPY reloc.
137 bool
ef9beddf 138 need_copy_reloc(Sized_symbol<size>* gsym,
6fa2a40b 139 Sized_relobj_file<size, big_endian>* object,
12c0daef
ILT
140 unsigned int shndx) const;
141
12c0daef
ILT
142 // Save a reloc against SYM for possible emission later.
143 void
859d7987
CC
144 save(Symbol*,
145 Sized_relobj_file<size, big_endian>*,
146 unsigned int shndx,
147 Output_section*,
148 unsigned int r_type,
149 typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
150 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend);
12c0daef
ILT
151
152 // The target specific relocation type of the COPY relocation.
153 const unsigned int copy_reloc_type_;
154 // The dynamic BSS data which goes into the .bss section. This is
155 // where variables which require COPY relocations are placed.
156 Output_data_space* dynbss_;
12c0daef
ILT
157};
158
159} // End namespace gold.
160
161#endif // !defined(GOLD_COPY_RELOCS_H)
This page took 0.445606 seconds and 4 git commands to generate.