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