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