Commit | Line | Data |
---|---|---|
61ba1cf9 ILT |
1 | // reloc.cc -- relocate input files for gold. |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f 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 | ||
61ba1cf9 ILT |
23 | #include "gold.h" |
24 | ||
cb295612 ILT |
25 | #include <algorithm> |
26 | ||
61ba1cf9 | 27 | #include "workqueue.h" |
5a6f7e2d | 28 | #include "symtab.h" |
61ba1cf9 | 29 | #include "output.h" |
a9a60db6 ILT |
30 | #include "merge.h" |
31 | #include "object.h" | |
7019cd25 | 32 | #include "target-reloc.h" |
61ba1cf9 ILT |
33 | #include "reloc.h" |
34 | ||
35 | namespace gold | |
36 | { | |
37 | ||
92e059d8 ILT |
38 | // Read_relocs methods. |
39 | ||
40 | // These tasks just read the relocation information from the file. | |
41 | // After reading it, the start another task to process the | |
42 | // information. These tasks requires access to the file. | |
43 | ||
17a1d0a9 ILT |
44 | Task_token* |
45 | Read_relocs::is_runnable() | |
92e059d8 | 46 | { |
17a1d0a9 | 47 | return this->object_->is_locked() ? this->object_->token() : NULL; |
92e059d8 ILT |
48 | } |
49 | ||
50 | // Lock the file. | |
51 | ||
17a1d0a9 ILT |
52 | void |
53 | Read_relocs::locks(Task_locker* tl) | |
92e059d8 | 54 | { |
17a1d0a9 | 55 | tl->add(this, this->object_->token()); |
92e059d8 ILT |
56 | } |
57 | ||
58 | // Read the relocations and then start a Scan_relocs_task. | |
59 | ||
60 | void | |
61 | Read_relocs::run(Workqueue* workqueue) | |
62 | { | |
63 | Read_relocs_data *rd = new Read_relocs_data; | |
64 | this->object_->read_relocs(rd); | |
17a1d0a9 ILT |
65 | this->object_->release(); |
66 | ||
da769d56 ILT |
67 | workqueue->queue_next(new Scan_relocs(this->options_, this->symtab_, |
68 | this->layout_, this->object_, rd, | |
69 | this->symtab_lock_, this->blocker_)); | |
92e059d8 ILT |
70 | } |
71 | ||
c7912668 ILT |
72 | // Return a debugging name for the task. |
73 | ||
74 | std::string | |
75 | Read_relocs::get_name() const | |
76 | { | |
77 | return "Read_relocs " + this->object_->name(); | |
78 | } | |
79 | ||
92e059d8 ILT |
80 | // Scan_relocs methods. |
81 | ||
82 | // These tasks scan the relocations read by Read_relocs and mark up | |
83 | // the symbol table to indicate which relocations are required. We | |
84 | // use a lock on the symbol table to keep them from interfering with | |
85 | // each other. | |
86 | ||
17a1d0a9 ILT |
87 | Task_token* |
88 | Scan_relocs::is_runnable() | |
92e059d8 | 89 | { |
17a1d0a9 ILT |
90 | if (!this->symtab_lock_->is_writable()) |
91 | return this->symtab_lock_; | |
92 | if (this->object_->is_locked()) | |
93 | return this->object_->token(); | |
94 | return NULL; | |
92e059d8 ILT |
95 | } |
96 | ||
97 | // Return the locks we hold: one on the file, one on the symbol table | |
98 | // and one blocker. | |
99 | ||
17a1d0a9 ILT |
100 | void |
101 | Scan_relocs::locks(Task_locker* tl) | |
92e059d8 | 102 | { |
17a1d0a9 ILT |
103 | tl->add(this, this->object_->token()); |
104 | tl->add(this, this->symtab_lock_); | |
105 | tl->add(this, this->blocker_); | |
92e059d8 ILT |
106 | } |
107 | ||
108 | // Scan the relocs. | |
109 | ||
110 | void | |
111 | Scan_relocs::run(Workqueue*) | |
112 | { | |
ead1e424 ILT |
113 | this->object_->scan_relocs(this->options_, this->symtab_, this->layout_, |
114 | this->rd_); | |
17a1d0a9 | 115 | this->object_->release(); |
92e059d8 ILT |
116 | delete this->rd_; |
117 | this->rd_ = NULL; | |
118 | } | |
119 | ||
c7912668 ILT |
120 | // Return a debugging name for the task. |
121 | ||
122 | std::string | |
123 | Scan_relocs::get_name() const | |
124 | { | |
125 | return "Scan_relocs " + this->object_->name(); | |
126 | } | |
127 | ||
61ba1cf9 ILT |
128 | // Relocate_task methods. |
129 | ||
730cdc88 | 130 | // We may have to wait for the output sections to be written. |
61ba1cf9 | 131 | |
17a1d0a9 ILT |
132 | Task_token* |
133 | Relocate_task::is_runnable() | |
61ba1cf9 | 134 | { |
730cdc88 ILT |
135 | if (this->object_->relocs_must_follow_section_writes() |
136 | && this->output_sections_blocker_->is_blocked()) | |
17a1d0a9 | 137 | return this->output_sections_blocker_; |
730cdc88 | 138 | |
c7912668 | 139 | if (this->object_->is_locked()) |
17a1d0a9 | 140 | return this->object_->token(); |
c7912668 | 141 | |
17a1d0a9 | 142 | return NULL; |
61ba1cf9 ILT |
143 | } |
144 | ||
145 | // We want to lock the file while we run. We want to unblock | |
730cdc88 | 146 | // INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done. |
17a1d0a9 | 147 | // INPUT_SECTIONS_BLOCKER may be NULL. |
61ba1cf9 | 148 | |
17a1d0a9 ILT |
149 | void |
150 | Relocate_task::locks(Task_locker* tl) | |
61ba1cf9 | 151 | { |
17a1d0a9 ILT |
152 | if (this->input_sections_blocker_ != NULL) |
153 | tl->add(this, this->input_sections_blocker_); | |
154 | tl->add(this, this->final_blocker_); | |
155 | tl->add(this, this->object_->token()); | |
61ba1cf9 ILT |
156 | } |
157 | ||
158 | // Run the task. | |
159 | ||
160 | void | |
161 | Relocate_task::run(Workqueue*) | |
162 | { | |
92e059d8 | 163 | this->object_->relocate(this->options_, this->symtab_, this->layout_, |
61ba1cf9 | 164 | this->of_); |
cb295612 ILT |
165 | |
166 | // This is normally the last thing we will do with an object, so | |
167 | // uncache all views. | |
168 | this->object_->clear_view_cache_marks(); | |
169 | ||
17a1d0a9 | 170 | this->object_->release(); |
61ba1cf9 ILT |
171 | } |
172 | ||
c7912668 ILT |
173 | // Return a debugging name for the task. |
174 | ||
175 | std::string | |
176 | Relocate_task::get_name() const | |
177 | { | |
178 | return "Relocate_task " + this->object_->name(); | |
179 | } | |
180 | ||
92e059d8 ILT |
181 | // Read the relocs and local symbols from the object file and store |
182 | // the information in RD. | |
183 | ||
184 | template<int size, bool big_endian> | |
185 | void | |
f6ce93d6 | 186 | Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd) |
92e059d8 ILT |
187 | { |
188 | rd->relocs.clear(); | |
189 | ||
190 | unsigned int shnum = this->shnum(); | |
191 | if (shnum == 0) | |
192 | return; | |
193 | ||
194 | rd->relocs.reserve(shnum / 2); | |
195 | ||
730cdc88 ILT |
196 | std::vector<Map_to_output>& map_sections(this->map_to_output()); |
197 | ||
645f8123 | 198 | const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(), |
9eb9fa57 | 199 | shnum * This::shdr_size, |
39d0cb0e | 200 | true, true); |
92e059d8 ILT |
201 | // Skip the first, dummy, section. |
202 | const unsigned char *ps = pshdrs + This::shdr_size; | |
203 | for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size) | |
204 | { | |
205 | typename This::Shdr shdr(ps); | |
206 | ||
207 | unsigned int sh_type = shdr.get_sh_type(); | |
208 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
209 | continue; | |
210 | ||
211 | unsigned int shndx = shdr.get_sh_info(); | |
212 | if (shndx >= shnum) | |
213 | { | |
75f2446e ILT |
214 | this->error(_("relocation section %u has bad info %u"), |
215 | i, shndx); | |
216 | continue; | |
92e059d8 ILT |
217 | } |
218 | ||
730cdc88 ILT |
219 | Output_section* os = map_sections[shndx].output_section; |
220 | if (os == NULL) | |
92e059d8 ILT |
221 | continue; |
222 | ||
ead1e424 ILT |
223 | // We are scanning relocations in order to fill out the GOT and |
224 | // PLT sections. Relocations for sections which are not | |
225 | // allocated (typically debugging sections) should not add new | |
6a74a719 | 226 | // GOT and PLT entries. So we skip them unless this is a |
7019cd25 ILT |
227 | // relocatable link or we need to emit relocations. |
228 | typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size); | |
229 | bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) | |
230 | != 0); | |
231 | if (!is_section_allocated | |
8851ecca ILT |
232 | && !parameters->options().relocatable() |
233 | && !parameters->options().emit_relocs()) | |
7019cd25 | 234 | continue; |
ead1e424 | 235 | |
645f8123 | 236 | if (shdr.get_sh_link() != this->symtab_shndx_) |
92e059d8 | 237 | { |
75f2446e ILT |
238 | this->error(_("relocation section %u uses unexpected " |
239 | "symbol table %u"), | |
240 | i, shdr.get_sh_link()); | |
241 | continue; | |
92e059d8 ILT |
242 | } |
243 | ||
244 | off_t sh_size = shdr.get_sh_size(); | |
245 | ||
246 | unsigned int reloc_size; | |
247 | if (sh_type == elfcpp::SHT_REL) | |
248 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
249 | else | |
250 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
251 | if (reloc_size != shdr.get_sh_entsize()) | |
252 | { | |
75f2446e ILT |
253 | this->error(_("unexpected entsize for reloc section %u: %lu != %u"), |
254 | i, static_cast<unsigned long>(shdr.get_sh_entsize()), | |
255 | reloc_size); | |
256 | continue; | |
92e059d8 ILT |
257 | } |
258 | ||
259 | size_t reloc_count = sh_size / reloc_size; | |
f5c3f225 | 260 | if (static_cast<off_t>(reloc_count * reloc_size) != sh_size) |
92e059d8 | 261 | { |
75f2446e ILT |
262 | this->error(_("reloc section %u size %lu uneven"), |
263 | i, static_cast<unsigned long>(sh_size)); | |
264 | continue; | |
92e059d8 ILT |
265 | } |
266 | ||
267 | rd->relocs.push_back(Section_relocs()); | |
268 | Section_relocs& sr(rd->relocs.back()); | |
269 | sr.reloc_shndx = i; | |
270 | sr.data_shndx = shndx; | |
9eb9fa57 | 271 | sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size, |
39d0cb0e | 272 | true, true); |
92e059d8 ILT |
273 | sr.sh_type = sh_type; |
274 | sr.reloc_count = reloc_count; | |
730cdc88 ILT |
275 | sr.output_section = os; |
276 | sr.needs_special_offset_handling = map_sections[shndx].offset == -1; | |
7019cd25 | 277 | sr.is_data_section_allocated = is_section_allocated; |
92e059d8 ILT |
278 | } |
279 | ||
280 | // Read the local symbols. | |
a3ad94ed | 281 | gold_assert(this->symtab_shndx_ != -1U); |
645f8123 | 282 | if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0) |
92e059d8 ILT |
283 | rd->local_symbols = NULL; |
284 | else | |
285 | { | |
286 | typename This::Shdr symtabshdr(pshdrs | |
645f8123 | 287 | + this->symtab_shndx_ * This::shdr_size); |
a3ad94ed | 288 | gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB); |
92e059d8 ILT |
289 | const int sym_size = This::sym_size; |
290 | const unsigned int loccount = this->local_symbol_count_; | |
a3ad94ed | 291 | gold_assert(loccount == symtabshdr.get_sh_info()); |
92e059d8 ILT |
292 | off_t locsize = loccount * sym_size; |
293 | rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(), | |
39d0cb0e | 294 | locsize, true, true); |
92e059d8 ILT |
295 | } |
296 | } | |
297 | ||
298 | // Scan the relocs and adjust the symbol table. This looks for | |
299 | // relocations which require GOT/PLT/COPY relocations. | |
300 | ||
301 | template<int size, bool big_endian> | |
302 | void | |
f6ce93d6 | 303 | Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options, |
92e059d8 | 304 | Symbol_table* symtab, |
ead1e424 | 305 | Layout* layout, |
92e059d8 ILT |
306 | Read_relocs_data* rd) |
307 | { | |
308 | Sized_target<size, big_endian>* target = this->sized_target(); | |
309 | ||
310 | const unsigned char* local_symbols; | |
311 | if (rd->local_symbols == NULL) | |
312 | local_symbols = NULL; | |
313 | else | |
314 | local_symbols = rd->local_symbols->data(); | |
315 | ||
316 | for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); | |
317 | p != rd->relocs.end(); | |
318 | ++p) | |
319 | { | |
8851ecca | 320 | if (!parameters->options().relocatable()) |
7019cd25 ILT |
321 | { |
322 | // As noted above, when not generating an object file, we | |
323 | // only scan allocated sections. We may see a non-allocated | |
324 | // section here if we are emitting relocs. | |
325 | if (p->is_data_section_allocated) | |
326 | target->scan_relocs(options, symtab, layout, this, p->data_shndx, | |
327 | p->sh_type, p->contents->data(), | |
328 | p->reloc_count, p->output_section, | |
329 | p->needs_special_offset_handling, | |
330 | this->local_symbol_count_, | |
331 | local_symbols); | |
8851ecca | 332 | if (parameters->options().emit_relocs()) |
7019cd25 ILT |
333 | this->emit_relocs_scan(options, symtab, layout, local_symbols, p); |
334 | } | |
6a74a719 ILT |
335 | else |
336 | { | |
337 | Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx); | |
338 | gold_assert(rr != NULL); | |
339 | rr->set_reloc_count(p->reloc_count); | |
340 | target->scan_relocatable_relocs(options, symtab, layout, this, | |
341 | p->data_shndx, p->sh_type, | |
342 | p->contents->data(), | |
343 | p->reloc_count, | |
344 | p->output_section, | |
345 | p->needs_special_offset_handling, | |
346 | this->local_symbol_count_, | |
347 | local_symbols, | |
348 | rr); | |
349 | } | |
350 | ||
92e059d8 ILT |
351 | delete p->contents; |
352 | p->contents = NULL; | |
353 | } | |
354 | ||
355 | if (rd->local_symbols != NULL) | |
356 | { | |
357 | delete rd->local_symbols; | |
358 | rd->local_symbols = NULL; | |
359 | } | |
360 | } | |
361 | ||
7019cd25 ILT |
362 | // This is a strategy class we use when scanning for --emit-relocs. |
363 | ||
364 | template<int sh_type> | |
365 | class Emit_relocs_strategy | |
366 | { | |
367 | public: | |
368 | // A local non-section symbol. | |
369 | inline Relocatable_relocs::Reloc_strategy | |
370 | local_non_section_strategy(unsigned int, Relobj*) | |
371 | { return Relocatable_relocs::RELOC_COPY; } | |
372 | ||
373 | // A local section symbol. | |
374 | inline Relocatable_relocs::Reloc_strategy | |
375 | local_section_strategy(unsigned int, Relobj*) | |
376 | { | |
377 | if (sh_type == elfcpp::SHT_RELA) | |
378 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; | |
379 | else | |
380 | { | |
381 | // The addend is stored in the section contents. Since this | |
382 | // is not a relocatable link, we are going to apply the | |
383 | // relocation contents to the section as usual. This means | |
384 | // that we have no way to record the original addend. If the | |
385 | // original addend is not zero, there is basically no way for | |
386 | // the user to handle this correctly. Caveat emptor. | |
387 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; | |
388 | } | |
389 | } | |
390 | ||
391 | // A global symbol. | |
392 | inline Relocatable_relocs::Reloc_strategy | |
393 | global_strategy(unsigned int, Relobj*, unsigned int) | |
394 | { return Relocatable_relocs::RELOC_COPY; } | |
395 | }; | |
396 | ||
397 | // Scan the input relocations for --emit-relocs. | |
398 | ||
399 | template<int size, bool big_endian> | |
400 | void | |
401 | Sized_relobj<size, big_endian>::emit_relocs_scan( | |
402 | const General_options& options, | |
403 | Symbol_table* symtab, | |
404 | Layout* layout, | |
405 | const unsigned char* plocal_syms, | |
406 | const Read_relocs_data::Relocs_list::iterator& p) | |
407 | { | |
408 | Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx); | |
409 | gold_assert(rr != NULL); | |
410 | rr->set_reloc_count(p->reloc_count); | |
411 | ||
412 | if (p->sh_type == elfcpp::SHT_REL) | |
413 | this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(options, symtab, layout, | |
414 | plocal_syms, p, rr); | |
415 | else | |
416 | { | |
417 | gold_assert(p->sh_type == elfcpp::SHT_RELA); | |
418 | this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(options, symtab, | |
419 | layout, plocal_syms, p, | |
420 | rr); | |
421 | } | |
422 | } | |
423 | ||
424 | // Scan the input relocation for --emit-relocs, templatized on the | |
425 | // type of the relocation section. | |
426 | ||
427 | template<int size, bool big_endian> | |
428 | template<int sh_type> | |
429 | void | |
430 | Sized_relobj<size, big_endian>::emit_relocs_scan_reltype( | |
431 | const General_options& options, | |
432 | Symbol_table* symtab, | |
433 | Layout* layout, | |
434 | const unsigned char* plocal_syms, | |
435 | const Read_relocs_data::Relocs_list::iterator& p, | |
436 | Relocatable_relocs* rr) | |
437 | { | |
438 | scan_relocatable_relocs<size, big_endian, sh_type, | |
439 | Emit_relocs_strategy<sh_type> >( | |
440 | options, | |
441 | symtab, | |
442 | layout, | |
443 | this, | |
444 | p->data_shndx, | |
445 | p->contents->data(), | |
446 | p->reloc_count, | |
447 | p->output_section, | |
448 | p->needs_special_offset_handling, | |
449 | this->local_symbol_count_, | |
450 | plocal_syms, | |
451 | rr); | |
452 | } | |
453 | ||
61ba1cf9 ILT |
454 | // Relocate the input sections and write out the local symbols. |
455 | ||
456 | template<int size, bool big_endian> | |
457 | void | |
f6ce93d6 | 458 | Sized_relobj<size, big_endian>::do_relocate(const General_options& options, |
61ba1cf9 | 459 | const Symbol_table* symtab, |
92e059d8 | 460 | const Layout* layout, |
61ba1cf9 ILT |
461 | Output_file* of) |
462 | { | |
463 | unsigned int shnum = this->shnum(); | |
464 | ||
465 | // Read the section headers. | |
645f8123 | 466 | const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(), |
9eb9fa57 | 467 | shnum * This::shdr_size, |
39d0cb0e | 468 | true, true); |
61ba1cf9 ILT |
469 | |
470 | Views views; | |
471 | views.resize(shnum); | |
472 | ||
473 | // Make two passes over the sections. The first one copies the | |
474 | // section data to the output file. The second one applies | |
475 | // relocations. | |
476 | ||
477 | this->write_sections(pshdrs, of, &views); | |
478 | ||
a9a60db6 ILT |
479 | // To speed up relocations, we set up hash tables for fast lookup of |
480 | // input offsets to output addresses. | |
481 | this->initialize_input_to_output_maps(); | |
482 | ||
61ba1cf9 ILT |
483 | // Apply relocations. |
484 | ||
92e059d8 | 485 | this->relocate_sections(options, symtab, layout, pshdrs, &views); |
61ba1cf9 | 486 | |
a9a60db6 ILT |
487 | // After we've done the relocations, we release the hash tables, |
488 | // since we no longer need them. | |
489 | this->free_input_to_output_maps(); | |
490 | ||
61ba1cf9 ILT |
491 | // Write out the accumulated views. |
492 | for (unsigned int i = 1; i < shnum; ++i) | |
493 | { | |
494 | if (views[i].view != NULL) | |
730cdc88 | 495 | { |
96803768 ILT |
496 | if (!views[i].is_postprocessing_view) |
497 | { | |
498 | if (views[i].is_input_output_view) | |
499 | of->write_input_output_view(views[i].offset, | |
500 | views[i].view_size, | |
501 | views[i].view); | |
502 | else | |
503 | of->write_output_view(views[i].offset, views[i].view_size, | |
504 | views[i].view); | |
505 | } | |
730cdc88 | 506 | } |
61ba1cf9 ILT |
507 | } |
508 | ||
509 | // Write out the local symbols. | |
7bf1f802 | 510 | this->write_local_symbols(of, layout->sympool(), layout->dynpool()); |
cb295612 ILT |
511 | |
512 | // We should no longer need the local symbol values. | |
513 | this->clear_local_symbols(); | |
61ba1cf9 ILT |
514 | } |
515 | ||
cb295612 ILT |
516 | // Sort a Read_multiple vector by file offset. |
517 | struct Read_multiple_compare | |
518 | { | |
519 | inline bool | |
520 | operator()(const File_read::Read_multiple_entry& rme1, | |
521 | const File_read::Read_multiple_entry& rme2) const | |
522 | { return rme1.file_offset < rme2.file_offset; } | |
523 | }; | |
524 | ||
61ba1cf9 ILT |
525 | // Write section data to the output file. PSHDRS points to the |
526 | // section headers. Record the views in *PVIEWS for use when | |
527 | // relocating. | |
528 | ||
529 | template<int size, bool big_endian> | |
530 | void | |
f6ce93d6 | 531 | Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs, |
61ba1cf9 | 532 | Output_file* of, |
cb295612 | 533 | Views* pviews) |
61ba1cf9 ILT |
534 | { |
535 | unsigned int shnum = this->shnum(); | |
17a1d0a9 | 536 | const std::vector<Map_to_output>& map_sections(this->map_to_output()); |
61ba1cf9 | 537 | |
cb295612 ILT |
538 | File_read::Read_multiple rm; |
539 | bool is_sorted = true; | |
540 | ||
61ba1cf9 ILT |
541 | const unsigned char* p = pshdrs + This::shdr_size; |
542 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) | |
543 | { | |
544 | View_size* pvs = &(*pviews)[i]; | |
545 | ||
546 | pvs->view = NULL; | |
547 | ||
548 | const Output_section* os = map_sections[i].output_section; | |
549 | if (os == NULL) | |
550 | continue; | |
730cdc88 | 551 | off_t output_offset = map_sections[i].offset; |
61ba1cf9 ILT |
552 | |
553 | typename This::Shdr shdr(p); | |
554 | ||
555 | if (shdr.get_sh_type() == elfcpp::SHT_NOBITS) | |
556 | continue; | |
557 | ||
8851ecca ILT |
558 | if ((parameters->options().relocatable() |
559 | || parameters->options().emit_relocs()) | |
6a74a719 ILT |
560 | && (shdr.get_sh_type() == elfcpp::SHT_REL |
561 | || shdr.get_sh_type() == elfcpp::SHT_RELA) | |
562 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
563 | { | |
7019cd25 ILT |
564 | // This is a reloc section in a relocatable link or when |
565 | // emitting relocs. We don't need to read the input file. | |
566 | // The size and file offset are stored in the | |
567 | // Relocatable_relocs structure. | |
6a74a719 ILT |
568 | Relocatable_relocs* rr = this->relocatable_relocs(i); |
569 | gold_assert(rr != NULL); | |
570 | Output_data* posd = rr->output_data(); | |
571 | gold_assert(posd != NULL); | |
572 | ||
573 | pvs->offset = posd->offset(); | |
574 | pvs->view_size = posd->data_size(); | |
575 | pvs->view = of->get_output_view(pvs->offset, pvs->view_size); | |
576 | pvs->address = posd->address(); | |
577 | pvs->is_input_output_view = false; | |
578 | pvs->is_postprocessing_view = false; | |
579 | ||
580 | continue; | |
581 | } | |
582 | ||
96803768 ILT |
583 | // In the normal case, this input section is simply mapped to |
584 | // the output section at offset OUTPUT_OFFSET. | |
585 | ||
586 | // However, if OUTPUT_OFFSET == -1, then input data is handled | |
587 | // specially--e.g., a .eh_frame section. The relocation | |
588 | // routines need to check for each reloc where it should be | |
589 | // applied. For this case, we need an input/output view for the | |
590 | // entire contents of the section in the output file. We don't | |
591 | // want to copy the contents of the input section to the output | |
592 | // section; the output section contents were already written, | |
593 | // and we waited for them in Relocate_task::is_runnable because | |
594 | // relocs_must_follow_section_writes is set for the object. | |
595 | ||
596 | // Regardless of which of the above cases is true, we have to | |
597 | // check requires_postprocessing of the output section. If that | |
598 | // is false, then we work with views of the output file | |
599 | // directly. If it is true, then we work with a separate | |
600 | // buffer, and the output section is responsible for writing the | |
601 | // final data to the output file. | |
602 | ||
603 | off_t output_section_offset; | |
604 | off_t output_section_size; | |
605 | if (!os->requires_postprocessing()) | |
606 | { | |
607 | output_section_offset = os->offset(); | |
608 | output_section_size = os->data_size(); | |
609 | } | |
610 | else | |
611 | { | |
612 | output_section_offset = 0; | |
613 | output_section_size = os->postprocessing_buffer_size(); | |
614 | } | |
615 | ||
730cdc88 | 616 | off_t view_start; |
fe8718a4 | 617 | section_size_type view_size; |
730cdc88 ILT |
618 | if (output_offset != -1) |
619 | { | |
96803768 | 620 | view_start = output_section_offset + output_offset; |
fe8718a4 | 621 | view_size = convert_to_section_size_type(shdr.get_sh_size()); |
730cdc88 ILT |
622 | } |
623 | else | |
624 | { | |
96803768 | 625 | view_start = output_section_offset; |
fe8718a4 | 626 | view_size = convert_to_section_size_type(output_section_size); |
730cdc88 | 627 | } |
61ba1cf9 | 628 | |
730cdc88 | 629 | if (view_size == 0) |
ead1e424 ILT |
630 | continue; |
631 | ||
730cdc88 ILT |
632 | gold_assert(output_offset == -1 |
633 | || (output_offset >= 0 | |
8d32f935 ILT |
634 | && (output_offset + static_cast<off_t>(view_size) |
635 | <= output_section_size))); | |
ead1e424 | 636 | |
730cdc88 | 637 | unsigned char* view; |
96803768 ILT |
638 | if (os->requires_postprocessing()) |
639 | { | |
640 | unsigned char* buffer = os->postprocessing_buffer(); | |
641 | view = buffer + view_start; | |
642 | if (output_offset != -1) | |
cb295612 ILT |
643 | { |
644 | off_t sh_offset = shdr.get_sh_offset(); | |
645 | if (!rm.empty() && rm.back().file_offset > sh_offset) | |
646 | is_sorted = false; | |
647 | rm.push_back(File_read::Read_multiple_entry(sh_offset, | |
648 | view_size, view)); | |
649 | } | |
96803768 | 650 | } |
730cdc88 ILT |
651 | else |
652 | { | |
96803768 ILT |
653 | if (output_offset == -1) |
654 | view = of->get_input_output_view(view_start, view_size); | |
655 | else | |
656 | { | |
657 | view = of->get_output_view(view_start, view_size); | |
cb295612 ILT |
658 | off_t sh_offset = shdr.get_sh_offset(); |
659 | if (!rm.empty() && rm.back().file_offset > sh_offset) | |
660 | is_sorted = false; | |
661 | rm.push_back(File_read::Read_multiple_entry(sh_offset, | |
662 | view_size, view)); | |
96803768 | 663 | } |
730cdc88 | 664 | } |
92e059d8 | 665 | |
61ba1cf9 | 666 | pvs->view = view; |
730cdc88 ILT |
667 | pvs->address = os->address(); |
668 | if (output_offset != -1) | |
669 | pvs->address += output_offset; | |
670 | pvs->offset = view_start; | |
671 | pvs->view_size = view_size; | |
672 | pvs->is_input_output_view = output_offset == -1; | |
96803768 | 673 | pvs->is_postprocessing_view = os->requires_postprocessing(); |
61ba1cf9 | 674 | } |
cb295612 ILT |
675 | |
676 | // Actually read the data. | |
677 | if (!rm.empty()) | |
678 | { | |
679 | if (!is_sorted) | |
680 | std::sort(rm.begin(), rm.end(), Read_multiple_compare()); | |
681 | this->read_multiple(rm); | |
682 | } | |
61ba1cf9 ILT |
683 | } |
684 | ||
685 | // Relocate section data. VIEWS points to the section data as views | |
686 | // in the output file. | |
687 | ||
688 | template<int size, bool big_endian> | |
689 | void | |
f6ce93d6 | 690 | Sized_relobj<size, big_endian>::relocate_sections( |
92e059d8 ILT |
691 | const General_options& options, |
692 | const Symbol_table* symtab, | |
693 | const Layout* layout, | |
694 | const unsigned char* pshdrs, | |
695 | Views* pviews) | |
61ba1cf9 ILT |
696 | { |
697 | unsigned int shnum = this->shnum(); | |
61ba1cf9 ILT |
698 | Sized_target<size, big_endian>* target = this->sized_target(); |
699 | ||
17a1d0a9 | 700 | const std::vector<Map_to_output>& map_sections(this->map_to_output()); |
730cdc88 | 701 | |
92e059d8 ILT |
702 | Relocate_info<size, big_endian> relinfo; |
703 | relinfo.options = &options; | |
704 | relinfo.symtab = symtab; | |
705 | relinfo.layout = layout; | |
706 | relinfo.object = this; | |
92e059d8 | 707 | |
61ba1cf9 ILT |
708 | const unsigned char* p = pshdrs + This::shdr_size; |
709 | for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size) | |
710 | { | |
711 | typename This::Shdr shdr(p); | |
712 | ||
713 | unsigned int sh_type = shdr.get_sh_type(); | |
714 | if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA) | |
715 | continue; | |
716 | ||
717 | unsigned int index = shdr.get_sh_info(); | |
718 | if (index >= this->shnum()) | |
719 | { | |
75f2446e ILT |
720 | this->error(_("relocation section %u has bad info %u"), |
721 | i, index); | |
722 | continue; | |
61ba1cf9 ILT |
723 | } |
724 | ||
730cdc88 ILT |
725 | Output_section* os = map_sections[index].output_section; |
726 | if (os == NULL) | |
61ba1cf9 ILT |
727 | { |
728 | // This relocation section is against a section which we | |
729 | // discarded. | |
730 | continue; | |
731 | } | |
730cdc88 | 732 | off_t output_offset = map_sections[index].offset; |
61ba1cf9 | 733 | |
a3ad94ed | 734 | gold_assert((*pviews)[index].view != NULL); |
8851ecca | 735 | if (parameters->options().relocatable()) |
6a74a719 | 736 | gold_assert((*pviews)[i].view != NULL); |
61ba1cf9 | 737 | |
645f8123 | 738 | if (shdr.get_sh_link() != this->symtab_shndx_) |
61ba1cf9 | 739 | { |
75f2446e ILT |
740 | gold_error(_("relocation section %u uses unexpected " |
741 | "symbol table %u"), | |
742 | i, shdr.get_sh_link()); | |
743 | continue; | |
61ba1cf9 ILT |
744 | } |
745 | ||
746 | off_t sh_size = shdr.get_sh_size(); | |
747 | const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(), | |
39d0cb0e | 748 | sh_size, true, false); |
61ba1cf9 ILT |
749 | |
750 | unsigned int reloc_size; | |
751 | if (sh_type == elfcpp::SHT_REL) | |
752 | reloc_size = elfcpp::Elf_sizes<size>::rel_size; | |
753 | else | |
754 | reloc_size = elfcpp::Elf_sizes<size>::rela_size; | |
755 | ||
756 | if (reloc_size != shdr.get_sh_entsize()) | |
757 | { | |
75f2446e ILT |
758 | gold_error(_("unexpected entsize for reloc section %u: %lu != %u"), |
759 | i, static_cast<unsigned long>(shdr.get_sh_entsize()), | |
730cdc88 | 760 | reloc_size); |
75f2446e | 761 | continue; |
61ba1cf9 ILT |
762 | } |
763 | ||
764 | size_t reloc_count = sh_size / reloc_size; | |
f5c3f225 | 765 | if (static_cast<off_t>(reloc_count * reloc_size) != sh_size) |
61ba1cf9 | 766 | { |
75f2446e ILT |
767 | gold_error(_("reloc section %u size %lu uneven"), |
768 | i, static_cast<unsigned long>(sh_size)); | |
769 | continue; | |
61ba1cf9 ILT |
770 | } |
771 | ||
96803768 ILT |
772 | gold_assert(output_offset != -1 |
773 | || this->relocs_must_follow_section_writes()); | |
774 | ||
92e059d8 ILT |
775 | relinfo.reloc_shndx = i; |
776 | relinfo.data_shndx = index; | |
8851ecca | 777 | if (!parameters->options().relocatable()) |
7019cd25 ILT |
778 | { |
779 | target->relocate_section(&relinfo, | |
780 | sh_type, | |
781 | prelocs, | |
782 | reloc_count, | |
783 | os, | |
784 | output_offset == -1, | |
785 | (*pviews)[index].view, | |
786 | (*pviews)[index].address, | |
787 | (*pviews)[index].view_size); | |
8851ecca | 788 | if (parameters->options().emit_relocs()) |
7019cd25 ILT |
789 | this->emit_relocs(&relinfo, i, sh_type, prelocs, reloc_count, |
790 | os, output_offset, | |
791 | (*pviews)[index].view, | |
792 | (*pviews)[index].address, | |
793 | (*pviews)[index].view_size, | |
794 | (*pviews)[i].view, | |
795 | (*pviews)[i].view_size); | |
796 | } | |
6a74a719 ILT |
797 | else |
798 | { | |
799 | Relocatable_relocs* rr = this->relocatable_relocs(i); | |
800 | target->relocate_for_relocatable(&relinfo, | |
801 | sh_type, | |
802 | prelocs, | |
803 | reloc_count, | |
804 | os, | |
805 | output_offset, | |
806 | rr, | |
807 | (*pviews)[index].view, | |
808 | (*pviews)[index].address, | |
809 | (*pviews)[index].view_size, | |
810 | (*pviews)[i].view, | |
811 | (*pviews)[i].view_size); | |
812 | } | |
61ba1cf9 ILT |
813 | } |
814 | } | |
815 | ||
7019cd25 ILT |
816 | // Emit the relocs for --emit-relocs. |
817 | ||
818 | template<int size, bool big_endian> | |
819 | void | |
820 | Sized_relobj<size, big_endian>::emit_relocs( | |
821 | const Relocate_info<size, big_endian>* relinfo, | |
822 | unsigned int i, | |
823 | unsigned int sh_type, | |
824 | const unsigned char* prelocs, | |
825 | size_t reloc_count, | |
826 | Output_section* output_section, | |
827 | off_t offset_in_output_section, | |
828 | unsigned char* view, | |
829 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
830 | section_size_type view_size, | |
831 | unsigned char* reloc_view, | |
832 | section_size_type reloc_view_size) | |
833 | { | |
834 | if (sh_type == elfcpp::SHT_REL) | |
835 | this->emit_relocs_reltype<elfcpp::SHT_REL>(relinfo, i, prelocs, | |
836 | reloc_count, output_section, | |
837 | offset_in_output_section, | |
838 | view, address, view_size, | |
839 | reloc_view, reloc_view_size); | |
840 | else | |
841 | { | |
842 | gold_assert(sh_type == elfcpp::SHT_RELA); | |
843 | this->emit_relocs_reltype<elfcpp::SHT_RELA>(relinfo, i, prelocs, | |
844 | reloc_count, output_section, | |
845 | offset_in_output_section, | |
846 | view, address, view_size, | |
847 | reloc_view, reloc_view_size); | |
848 | } | |
849 | } | |
850 | ||
851 | // Emit the relocs for --emit-relocs, templatized on the type of the | |
852 | // relocation section. | |
853 | ||
854 | template<int size, bool big_endian> | |
855 | template<int sh_type> | |
856 | void | |
857 | Sized_relobj<size, big_endian>::emit_relocs_reltype( | |
858 | const Relocate_info<size, big_endian>* relinfo, | |
859 | unsigned int i, | |
860 | const unsigned char* prelocs, | |
861 | size_t reloc_count, | |
862 | Output_section* output_section, | |
863 | off_t offset_in_output_section, | |
864 | unsigned char* view, | |
865 | typename elfcpp::Elf_types<size>::Elf_Addr address, | |
866 | section_size_type view_size, | |
867 | unsigned char* reloc_view, | |
868 | section_size_type reloc_view_size) | |
869 | { | |
870 | const Relocatable_relocs* rr = this->relocatable_relocs(i); | |
871 | relocate_for_relocatable<size, big_endian, sh_type>( | |
872 | relinfo, | |
873 | prelocs, | |
874 | reloc_count, | |
875 | output_section, | |
876 | offset_in_output_section, | |
877 | rr, | |
878 | view, | |
879 | address, | |
880 | view_size, | |
881 | reloc_view, | |
882 | reloc_view_size); | |
883 | } | |
884 | ||
a9a60db6 ILT |
885 | // Create merge hash tables for the local symbols. These are used to |
886 | // speed up relocations. | |
887 | ||
888 | template<int size, bool big_endian> | |
889 | void | |
890 | Sized_relobj<size, big_endian>::initialize_input_to_output_maps() | |
891 | { | |
892 | const unsigned int loccount = this->local_symbol_count_; | |
893 | for (unsigned int i = 1; i < loccount; ++i) | |
894 | { | |
895 | Symbol_value<size>& lv(this->local_values_[i]); | |
896 | lv.initialize_input_to_output_map(this); | |
897 | } | |
898 | } | |
899 | ||
900 | // Free merge hash tables for the local symbols. | |
901 | ||
902 | template<int size, bool big_endian> | |
903 | void | |
904 | Sized_relobj<size, big_endian>::free_input_to_output_maps() | |
905 | { | |
906 | const unsigned int loccount = this->local_symbol_count_; | |
907 | for (unsigned int i = 1; i < loccount; ++i) | |
908 | { | |
909 | Symbol_value<size>& lv(this->local_values_[i]); | |
910 | lv.free_input_to_output_map(); | |
911 | } | |
912 | } | |
913 | ||
914 | // Class Merged_symbol_value. | |
915 | ||
916 | template<int size> | |
917 | void | |
918 | Merged_symbol_value<size>::initialize_input_to_output_map( | |
919 | const Relobj* object, | |
920 | unsigned int input_shndx) | |
921 | { | |
922 | Object_merge_map* map = object->merge_map(); | |
923 | map->initialize_input_to_output_map<size>(input_shndx, | |
924 | this->output_start_address_, | |
925 | &this->output_addresses_); | |
926 | } | |
927 | ||
928 | // Get the output value corresponding to an input offset if we | |
929 | // couldn't find it in the hash table. | |
930 | ||
931 | template<int size> | |
932 | typename elfcpp::Elf_types<size>::Elf_Addr | |
933 | Merged_symbol_value<size>::value_from_output_section( | |
934 | const Relobj* object, | |
935 | unsigned int input_shndx, | |
936 | typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const | |
937 | { | |
938 | section_offset_type output_offset; | |
939 | bool found = object->merge_map()->get_output_offset(NULL, input_shndx, | |
940 | input_offset, | |
941 | &output_offset); | |
942 | ||
943 | // If this assertion fails, it means that some relocation was | |
944 | // against a portion of an input merge section which we didn't map | |
945 | // to the output file and we didn't explicitly discard. We should | |
946 | // always map all portions of input merge sections. | |
947 | gold_assert(found); | |
948 | ||
949 | if (output_offset == -1) | |
950 | return 0; | |
951 | else | |
952 | return this->output_start_address_ + output_offset; | |
953 | } | |
954 | ||
5a6f7e2d ILT |
955 | // Copy_relocs::Copy_reloc_entry methods. |
956 | ||
957 | // Return whether we should emit this reloc. We should emit it if the | |
958 | // symbol is still defined in a dynamic object. If we should not emit | |
959 | // it, we clear it, to save ourselves the test next time. | |
960 | ||
961 | template<int size, bool big_endian> | |
962 | bool | |
963 | Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit() | |
964 | { | |
965 | if (this->sym_ == NULL) | |
966 | return false; | |
14b31740 | 967 | if (this->sym_->is_from_dynobj()) |
5a6f7e2d ILT |
968 | return true; |
969 | this->sym_ = NULL; | |
970 | return false; | |
971 | } | |
972 | ||
973 | // Emit a reloc into a SHT_REL section. | |
974 | ||
975 | template<int size, bool big_endian> | |
976 | void | |
977 | Copy_relocs<size, big_endian>::Copy_reloc_entry::emit( | |
978 | Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data) | |
979 | { | |
16649710 | 980 | this->sym_->set_needs_dynsym_entry(); |
4f4c5f80 ILT |
981 | reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_, |
982 | this->relobj_, this->shndx_, this->address_); | |
5a6f7e2d ILT |
983 | } |
984 | ||
985 | // Emit a reloc into a SHT_RELA section. | |
986 | ||
987 | template<int size, bool big_endian> | |
988 | void | |
989 | Copy_relocs<size, big_endian>::Copy_reloc_entry::emit( | |
990 | Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data) | |
991 | { | |
16649710 | 992 | this->sym_->set_needs_dynsym_entry(); |
4f4c5f80 ILT |
993 | reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_, |
994 | this->relobj_, this->shndx_, this->address_, | |
995 | this->addend_); | |
5a6f7e2d ILT |
996 | } |
997 | ||
998 | // Copy_relocs methods. | |
a3ad94ed ILT |
999 | |
1000 | // Return whether we need a COPY reloc for a relocation against GSYM. | |
1001 | // The relocation is being applied to section SHNDX in OBJECT. | |
1002 | ||
1003 | template<int size, bool big_endian> | |
1004 | bool | |
5a6f7e2d | 1005 | Copy_relocs<size, big_endian>::need_copy_reloc( |
a3ad94ed ILT |
1006 | const General_options*, |
1007 | Relobj* object, | |
1008 | unsigned int shndx, | |
5a6f7e2d | 1009 | Sized_symbol<size>* sym) |
a3ad94ed ILT |
1010 | { |
1011 | // FIXME: Handle -z nocopyrelocs. | |
1012 | ||
5a6f7e2d ILT |
1013 | if (sym->symsize() == 0) |
1014 | return false; | |
1015 | ||
a3ad94ed ILT |
1016 | // If this is a readonly section, then we need a COPY reloc. |
1017 | // Otherwise we can use a dynamic reloc. | |
1018 | if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0) | |
1019 | return true; | |
1020 | ||
1021 | return false; | |
1022 | } | |
1023 | ||
5a6f7e2d ILT |
1024 | // Save a Rel reloc. |
1025 | ||
1026 | template<int size, bool big_endian> | |
1027 | void | |
1028 | Copy_relocs<size, big_endian>::save( | |
1029 | Symbol* sym, | |
1030 | Relobj* relobj, | |
1031 | unsigned int shndx, | |
4f4c5f80 | 1032 | Output_section* output_section, |
5a6f7e2d ILT |
1033 | const elfcpp::Rel<size, big_endian>& rel) |
1034 | { | |
1035 | unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info()); | |
1036 | this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx, | |
4f4c5f80 ILT |
1037 | output_section, |
1038 | rel.get_r_offset(), 0)); | |
5a6f7e2d ILT |
1039 | } |
1040 | ||
1041 | // Save a Rela reloc. | |
1042 | ||
1043 | template<int size, bool big_endian> | |
1044 | void | |
1045 | Copy_relocs<size, big_endian>::save( | |
1046 | Symbol* sym, | |
1047 | Relobj* relobj, | |
1048 | unsigned int shndx, | |
4f4c5f80 | 1049 | Output_section* output_section, |
5a6f7e2d ILT |
1050 | const elfcpp::Rela<size, big_endian>& rela) |
1051 | { | |
1052 | unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info()); | |
1053 | this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx, | |
4f4c5f80 | 1054 | output_section, |
5a6f7e2d ILT |
1055 | rela.get_r_offset(), |
1056 | rela.get_r_addend())); | |
1057 | } | |
1058 | ||
1059 | // Return whether there are any relocs to emit. We don't want to emit | |
1060 | // a reloc if the symbol is no longer defined in a dynamic object. | |
1061 | ||
1062 | template<int size, bool big_endian> | |
1063 | bool | |
1064 | Copy_relocs<size, big_endian>::any_to_emit() | |
1065 | { | |
1066 | for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); | |
1067 | p != this->entries_.end(); | |
1068 | ++p) | |
1069 | { | |
1070 | if (p->should_emit()) | |
1071 | return true; | |
1072 | } | |
1073 | return false; | |
1074 | } | |
1075 | ||
1076 | // Emit relocs. | |
1077 | ||
1078 | template<int size, bool big_endian> | |
1079 | template<int sh_type> | |
1080 | void | |
1081 | Copy_relocs<size, big_endian>::emit( | |
1082 | Output_data_reloc<sh_type, true, size, big_endian>* reloc_data) | |
1083 | { | |
1084 | for (typename Copy_reloc_entries::iterator p = this->entries_.begin(); | |
1085 | p != this->entries_.end(); | |
1086 | ++p) | |
1087 | { | |
1088 | if (p->should_emit()) | |
1089 | p->emit(reloc_data); | |
1090 | } | |
1091 | } | |
1092 | ||
730cdc88 ILT |
1093 | // Track_relocs methods. |
1094 | ||
1095 | // Initialize the class to track the relocs. This gets the object, | |
1096 | // the reloc section index, and the type of the relocs. This returns | |
1097 | // false if something goes wrong. | |
1098 | ||
1099 | template<int size, bool big_endian> | |
1100 | bool | |
1101 | Track_relocs<size, big_endian>::initialize( | |
b696e6d4 | 1102 | Object* object, |
730cdc88 ILT |
1103 | unsigned int reloc_shndx, |
1104 | unsigned int reloc_type) | |
1105 | { | |
730cdc88 ILT |
1106 | // If RELOC_SHNDX is -1U, it means there is more than one reloc |
1107 | // section for the .eh_frame section. We can't handle that case. | |
1108 | if (reloc_shndx == -1U) | |
1109 | return false; | |
1110 | ||
1111 | // If RELOC_SHNDX is 0, there is no reloc section. | |
1112 | if (reloc_shndx == 0) | |
1113 | return true; | |
1114 | ||
1115 | // Get the contents of the reloc section. | |
1116 | this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false); | |
1117 | ||
1118 | if (reloc_type == elfcpp::SHT_REL) | |
1119 | this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size; | |
1120 | else if (reloc_type == elfcpp::SHT_RELA) | |
1121 | this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size; | |
1122 | else | |
1123 | gold_unreachable(); | |
1124 | ||
1125 | if (this->len_ % this->reloc_size_ != 0) | |
1126 | { | |
1127 | object->error(_("reloc section size %zu is not a multiple of " | |
1128 | "reloc size %d\n"), | |
1129 | static_cast<size_t>(this->len_), | |
1130 | this->reloc_size_); | |
1131 | return false; | |
1132 | } | |
1133 | ||
1134 | return true; | |
1135 | } | |
1136 | ||
1137 | // Return the offset of the next reloc, or -1 if there isn't one. | |
1138 | ||
1139 | template<int size, bool big_endian> | |
1140 | off_t | |
1141 | Track_relocs<size, big_endian>::next_offset() const | |
1142 | { | |
1143 | if (this->pos_ >= this->len_) | |
1144 | return -1; | |
1145 | ||
1146 | // Rel and Rela start out the same, so we can always use Rel to find | |
1147 | // the r_offset value. | |
1148 | elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); | |
1149 | return rel.get_r_offset(); | |
1150 | } | |
1151 | ||
1152 | // Return the index of the symbol referenced by the next reloc, or -1U | |
1153 | // if there aren't any more relocs. | |
1154 | ||
1155 | template<int size, bool big_endian> | |
1156 | unsigned int | |
1157 | Track_relocs<size, big_endian>::next_symndx() const | |
1158 | { | |
1159 | if (this->pos_ >= this->len_) | |
1160 | return -1U; | |
1161 | ||
1162 | // Rel and Rela start out the same, so we can use Rel to find the | |
1163 | // symbol index. | |
1164 | elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); | |
1165 | return elfcpp::elf_r_sym<size>(rel.get_r_info()); | |
1166 | } | |
1167 | ||
1168 | // Advance to the next reloc whose r_offset is greater than or equal | |
1169 | // to OFFSET. Return the number of relocs we skip. | |
1170 | ||
1171 | template<int size, bool big_endian> | |
1172 | int | |
1173 | Track_relocs<size, big_endian>::advance(off_t offset) | |
1174 | { | |
1175 | int ret = 0; | |
1176 | while (this->pos_ < this->len_) | |
1177 | { | |
1178 | // Rel and Rela start out the same, so we can always use Rel to | |
1179 | // find the r_offset value. | |
1180 | elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_); | |
1181 | if (static_cast<off_t>(rel.get_r_offset()) >= offset) | |
1182 | break; | |
1183 | ++ret; | |
1184 | this->pos_ += this->reloc_size_; | |
1185 | } | |
1186 | return ret; | |
1187 | } | |
1188 | ||
61ba1cf9 ILT |
1189 | // Instantiate the templates we need. We could use the configure |
1190 | // script to restrict this to only the ones for implemented targets. | |
1191 | ||
193a53d9 | 1192 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
1193 | template |
1194 | void | |
f6ce93d6 | 1195 | Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1196 | #endif |
92e059d8 | 1197 | |
193a53d9 | 1198 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
1199 | template |
1200 | void | |
f6ce93d6 | 1201 | Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1202 | #endif |
92e059d8 | 1203 | |
193a53d9 | 1204 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
1205 | template |
1206 | void | |
f6ce93d6 | 1207 | Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1208 | #endif |
92e059d8 | 1209 | |
193a53d9 | 1210 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
1211 | template |
1212 | void | |
f6ce93d6 | 1213 | Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd); |
193a53d9 | 1214 | #endif |
92e059d8 | 1215 | |
193a53d9 | 1216 | #ifdef HAVE_TARGET_32_LITTLE |
92e059d8 ILT |
1217 | template |
1218 | void | |
f6ce93d6 | 1219 | Sized_relobj<32, false>::do_scan_relocs(const General_options& options, |
92e059d8 | 1220 | Symbol_table* symtab, |
ead1e424 | 1221 | Layout* layout, |
92e059d8 | 1222 | Read_relocs_data* rd); |
193a53d9 | 1223 | #endif |
92e059d8 | 1224 | |
193a53d9 | 1225 | #ifdef HAVE_TARGET_32_BIG |
92e059d8 ILT |
1226 | template |
1227 | void | |
f6ce93d6 | 1228 | Sized_relobj<32, true>::do_scan_relocs(const General_options& options, |
92e059d8 | 1229 | Symbol_table* symtab, |
ead1e424 | 1230 | Layout* layout, |
92e059d8 | 1231 | Read_relocs_data* rd); |
193a53d9 | 1232 | #endif |
92e059d8 | 1233 | |
193a53d9 | 1234 | #ifdef HAVE_TARGET_64_LITTLE |
92e059d8 ILT |
1235 | template |
1236 | void | |
f6ce93d6 | 1237 | Sized_relobj<64, false>::do_scan_relocs(const General_options& options, |
92e059d8 | 1238 | Symbol_table* symtab, |
ead1e424 | 1239 | Layout* layout, |
92e059d8 | 1240 | Read_relocs_data* rd); |
193a53d9 | 1241 | #endif |
92e059d8 | 1242 | |
193a53d9 | 1243 | #ifdef HAVE_TARGET_64_BIG |
92e059d8 ILT |
1244 | template |
1245 | void | |
f6ce93d6 | 1246 | Sized_relobj<64, true>::do_scan_relocs(const General_options& options, |
92e059d8 | 1247 | Symbol_table* symtab, |
ead1e424 | 1248 | Layout* layout, |
92e059d8 | 1249 | Read_relocs_data* rd); |
193a53d9 | 1250 | #endif |
92e059d8 | 1251 | |
193a53d9 | 1252 | #ifdef HAVE_TARGET_32_LITTLE |
61ba1cf9 ILT |
1253 | template |
1254 | void | |
f6ce93d6 | 1255 | Sized_relobj<32, false>::do_relocate(const General_options& options, |
61ba1cf9 | 1256 | const Symbol_table* symtab, |
92e059d8 | 1257 | const Layout* layout, |
61ba1cf9 | 1258 | Output_file* of); |
193a53d9 | 1259 | #endif |
61ba1cf9 | 1260 | |
193a53d9 | 1261 | #ifdef HAVE_TARGET_32_BIG |
61ba1cf9 ILT |
1262 | template |
1263 | void | |
f6ce93d6 | 1264 | Sized_relobj<32, true>::do_relocate(const General_options& options, |
61ba1cf9 | 1265 | const Symbol_table* symtab, |
92e059d8 | 1266 | const Layout* layout, |
61ba1cf9 | 1267 | Output_file* of); |
193a53d9 | 1268 | #endif |
61ba1cf9 | 1269 | |
193a53d9 | 1270 | #ifdef HAVE_TARGET_64_LITTLE |
61ba1cf9 ILT |
1271 | template |
1272 | void | |
f6ce93d6 | 1273 | Sized_relobj<64, false>::do_relocate(const General_options& options, |
61ba1cf9 | 1274 | const Symbol_table* symtab, |
92e059d8 | 1275 | const Layout* layout, |
61ba1cf9 | 1276 | Output_file* of); |
193a53d9 | 1277 | #endif |
61ba1cf9 | 1278 | |
193a53d9 | 1279 | #ifdef HAVE_TARGET_64_BIG |
61ba1cf9 ILT |
1280 | template |
1281 | void | |
f6ce93d6 | 1282 | Sized_relobj<64, true>::do_relocate(const General_options& options, |
61ba1cf9 | 1283 | const Symbol_table* symtab, |
92e059d8 | 1284 | const Layout* layout, |
61ba1cf9 | 1285 | Output_file* of); |
193a53d9 | 1286 | #endif |
61ba1cf9 | 1287 | |
a9a60db6 ILT |
1288 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) |
1289 | template | |
1290 | class Merged_symbol_value<32>; | |
1291 | #endif | |
1292 | ||
1293 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1294 | template | |
1295 | class Merged_symbol_value<64>; | |
1296 | #endif | |
1297 | ||
1298 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
1299 | template | |
1300 | class Symbol_value<32>; | |
1301 | #endif | |
1302 | ||
1303 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
1304 | template | |
1305 | class Symbol_value<64>; | |
1306 | #endif | |
1307 | ||
193a53d9 | 1308 | #ifdef HAVE_TARGET_32_LITTLE |
a3ad94ed | 1309 | template |
5a6f7e2d | 1310 | class Copy_relocs<32, false>; |
193a53d9 | 1311 | #endif |
a3ad94ed | 1312 | |
193a53d9 | 1313 | #ifdef HAVE_TARGET_32_BIG |
a3ad94ed | 1314 | template |
5a6f7e2d | 1315 | class Copy_relocs<32, true>; |
193a53d9 | 1316 | #endif |
a3ad94ed | 1317 | |
193a53d9 | 1318 | #ifdef HAVE_TARGET_64_LITTLE |
a3ad94ed | 1319 | template |
5a6f7e2d | 1320 | class Copy_relocs<64, false>; |
193a53d9 | 1321 | #endif |
a3ad94ed | 1322 | |
193a53d9 | 1323 | #ifdef HAVE_TARGET_64_BIG |
a3ad94ed | 1324 | template |
5a6f7e2d | 1325 | class Copy_relocs<64, true>; |
193a53d9 | 1326 | #endif |
5a6f7e2d | 1327 | |
193a53d9 | 1328 | #ifdef HAVE_TARGET_32_LITTLE |
5a6f7e2d ILT |
1329 | template |
1330 | void | |
1331 | Copy_relocs<32, false>::emit<elfcpp::SHT_REL>( | |
1332 | Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*); | |
193a53d9 | 1333 | #endif |
5a6f7e2d | 1334 | |
193a53d9 | 1335 | #ifdef HAVE_TARGET_32_BIG |
5a6f7e2d ILT |
1336 | template |
1337 | void | |
1338 | Copy_relocs<32, true>::emit<elfcpp::SHT_REL>( | |
1339 | Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*); | |
193a53d9 | 1340 | #endif |
5a6f7e2d | 1341 | |
193a53d9 | 1342 | #ifdef HAVE_TARGET_64_LITTLE |
5a6f7e2d ILT |
1343 | template |
1344 | void | |
1345 | Copy_relocs<64, false>::emit<elfcpp::SHT_REL>( | |
1346 | Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*); | |
193a53d9 | 1347 | #endif |
5a6f7e2d | 1348 | |
193a53d9 | 1349 | #ifdef HAVE_TARGET_64_BIG |
5a6f7e2d ILT |
1350 | template |
1351 | void | |
1352 | Copy_relocs<64, true>::emit<elfcpp::SHT_REL>( | |
1353 | Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*); | |
193a53d9 | 1354 | #endif |
5a6f7e2d | 1355 | |
193a53d9 | 1356 | #ifdef HAVE_TARGET_32_LITTLE |
5a6f7e2d ILT |
1357 | template |
1358 | void | |
1359 | Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>( | |
1360 | Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*); | |
193a53d9 | 1361 | #endif |
5a6f7e2d | 1362 | |
193a53d9 | 1363 | #ifdef HAVE_TARGET_32_BIG |
5a6f7e2d ILT |
1364 | template |
1365 | void | |
1366 | Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>( | |
1367 | Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*); | |
193a53d9 | 1368 | #endif |
5a6f7e2d | 1369 | |
193a53d9 | 1370 | #ifdef HAVE_TARGET_64_LITTLE |
5a6f7e2d ILT |
1371 | template |
1372 | void | |
1373 | Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>( | |
1374 | Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*); | |
193a53d9 | 1375 | #endif |
5a6f7e2d | 1376 | |
193a53d9 | 1377 | #ifdef HAVE_TARGET_64_BIG |
5a6f7e2d ILT |
1378 | template |
1379 | void | |
1380 | Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>( | |
1381 | Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*); | |
193a53d9 | 1382 | #endif |
61ba1cf9 | 1383 | |
730cdc88 ILT |
1384 | #ifdef HAVE_TARGET_32_LITTLE |
1385 | template | |
1386 | class Track_relocs<32, false>; | |
1387 | #endif | |
1388 | ||
1389 | #ifdef HAVE_TARGET_32_BIG | |
1390 | template | |
1391 | class Track_relocs<32, true>; | |
1392 | #endif | |
1393 | ||
1394 | #ifdef HAVE_TARGET_64_LITTLE | |
1395 | template | |
1396 | class Track_relocs<64, false>; | |
1397 | #endif | |
1398 | ||
1399 | #ifdef HAVE_TARGET_64_BIG | |
1400 | template | |
1401 | class Track_relocs<64, true>; | |
1402 | #endif | |
1403 | ||
61ba1cf9 | 1404 | } // End namespace gold. |