* dwarf2.c (find_line): Don't trust debug information after an
[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
6a74a719
ILT
225 // GOT and PLT entries. So we skip them unless this is a
226 // relocatable link.
227 if (!parameters->output_is_object())
228 {
229 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
230 if ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
231 continue;
232 }
ead1e424 233
645f8123 234 if (shdr.get_sh_link() != this->symtab_shndx_)
92e059d8 235 {
75f2446e
ILT
236 this->error(_("relocation section %u uses unexpected "
237 "symbol table %u"),
238 i, shdr.get_sh_link());
239 continue;
92e059d8
ILT
240 }
241
242 off_t sh_size = shdr.get_sh_size();
243
244 unsigned int reloc_size;
245 if (sh_type == elfcpp::SHT_REL)
246 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
247 else
248 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
249 if (reloc_size != shdr.get_sh_entsize())
250 {
75f2446e
ILT
251 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
252 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
253 reloc_size);
254 continue;
92e059d8
ILT
255 }
256
257 size_t reloc_count = sh_size / reloc_size;
f5c3f225 258 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
92e059d8 259 {
75f2446e
ILT
260 this->error(_("reloc section %u size %lu uneven"),
261 i, static_cast<unsigned long>(sh_size));
262 continue;
92e059d8
ILT
263 }
264
265 rd->relocs.push_back(Section_relocs());
266 Section_relocs& sr(rd->relocs.back());
267 sr.reloc_shndx = i;
268 sr.data_shndx = shndx;
9eb9fa57
ILT
269 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
270 true);
92e059d8
ILT
271 sr.sh_type = sh_type;
272 sr.reloc_count = reloc_count;
730cdc88
ILT
273 sr.output_section = os;
274 sr.needs_special_offset_handling = map_sections[shndx].offset == -1;
92e059d8
ILT
275 }
276
277 // Read the local symbols.
a3ad94ed 278 gold_assert(this->symtab_shndx_ != -1U);
645f8123 279 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
92e059d8
ILT
280 rd->local_symbols = NULL;
281 else
282 {
283 typename This::Shdr symtabshdr(pshdrs
645f8123 284 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 285 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
92e059d8
ILT
286 const int sym_size = This::sym_size;
287 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 288 gold_assert(loccount == symtabshdr.get_sh_info());
92e059d8
ILT
289 off_t locsize = loccount * sym_size;
290 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
9eb9fa57 291 locsize, true);
92e059d8
ILT
292 }
293}
294
295// Scan the relocs and adjust the symbol table. This looks for
296// relocations which require GOT/PLT/COPY relocations.
297
298template<int size, bool big_endian>
299void
f6ce93d6 300Sized_relobj<size, big_endian>::do_scan_relocs(const General_options& options,
92e059d8 301 Symbol_table* symtab,
ead1e424 302 Layout* layout,
92e059d8
ILT
303 Read_relocs_data* rd)
304{
305 Sized_target<size, big_endian>* target = this->sized_target();
306
307 const unsigned char* local_symbols;
308 if (rd->local_symbols == NULL)
309 local_symbols = NULL;
310 else
311 local_symbols = rd->local_symbols->data();
312
313 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
314 p != rd->relocs.end();
315 ++p)
316 {
6a74a719
ILT
317 if (!parameters->output_is_object())
318 target->scan_relocs(options, symtab, layout, this, p->data_shndx,
319 p->sh_type, p->contents->data(), p->reloc_count,
320 p->output_section,
321 p->needs_special_offset_handling,
322 this->local_symbol_count_,
323 local_symbols);
324 else
325 {
326 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
327 gold_assert(rr != NULL);
328 rr->set_reloc_count(p->reloc_count);
329 target->scan_relocatable_relocs(options, symtab, layout, this,
330 p->data_shndx, p->sh_type,
331 p->contents->data(),
332 p->reloc_count,
333 p->output_section,
334 p->needs_special_offset_handling,
335 this->local_symbol_count_,
336 local_symbols,
337 rr);
338 }
339
92e059d8
ILT
340 delete p->contents;
341 p->contents = NULL;
342 }
343
344 if (rd->local_symbols != NULL)
345 {
346 delete rd->local_symbols;
347 rd->local_symbols = NULL;
348 }
349}
350
61ba1cf9
ILT
351// Relocate the input sections and write out the local symbols.
352
353template<int size, bool big_endian>
354void
f6ce93d6 355Sized_relobj<size, big_endian>::do_relocate(const General_options& options,
61ba1cf9 356 const Symbol_table* symtab,
92e059d8 357 const Layout* layout,
61ba1cf9
ILT
358 Output_file* of)
359{
360 unsigned int shnum = this->shnum();
361
362 // Read the section headers.
645f8123 363 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
9eb9fa57
ILT
364 shnum * This::shdr_size,
365 true);
61ba1cf9
ILT
366
367 Views views;
368 views.resize(shnum);
369
370 // Make two passes over the sections. The first one copies the
371 // section data to the output file. The second one applies
372 // relocations.
373
374 this->write_sections(pshdrs, of, &views);
375
a9a60db6
ILT
376 // To speed up relocations, we set up hash tables for fast lookup of
377 // input offsets to output addresses.
378 this->initialize_input_to_output_maps();
379
61ba1cf9
ILT
380 // Apply relocations.
381
92e059d8 382 this->relocate_sections(options, symtab, layout, pshdrs, &views);
61ba1cf9 383
a9a60db6
ILT
384 // After we've done the relocations, we release the hash tables,
385 // since we no longer need them.
386 this->free_input_to_output_maps();
387
61ba1cf9
ILT
388 // Write out the accumulated views.
389 for (unsigned int i = 1; i < shnum; ++i)
390 {
391 if (views[i].view != NULL)
730cdc88 392 {
96803768
ILT
393 if (!views[i].is_postprocessing_view)
394 {
395 if (views[i].is_input_output_view)
396 of->write_input_output_view(views[i].offset,
397 views[i].view_size,
398 views[i].view);
399 else
400 of->write_output_view(views[i].offset, views[i].view_size,
401 views[i].view);
402 }
730cdc88 403 }
61ba1cf9
ILT
404 }
405
406 // Write out the local symbols.
7bf1f802 407 this->write_local_symbols(of, layout->sympool(), layout->dynpool());
cb295612
ILT
408
409 // We should no longer need the local symbol values.
410 this->clear_local_symbols();
61ba1cf9
ILT
411}
412
cb295612
ILT
413// Sort a Read_multiple vector by file offset.
414struct Read_multiple_compare
415{
416 inline bool
417 operator()(const File_read::Read_multiple_entry& rme1,
418 const File_read::Read_multiple_entry& rme2) const
419 { return rme1.file_offset < rme2.file_offset; }
420};
421
61ba1cf9
ILT
422// Write section data to the output file. PSHDRS points to the
423// section headers. Record the views in *PVIEWS for use when
424// relocating.
425
426template<int size, bool big_endian>
427void
f6ce93d6 428Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
61ba1cf9 429 Output_file* of,
cb295612 430 Views* pviews)
61ba1cf9
ILT
431{
432 unsigned int shnum = this->shnum();
17a1d0a9 433 const std::vector<Map_to_output>& map_sections(this->map_to_output());
61ba1cf9 434
cb295612
ILT
435 File_read::Read_multiple rm;
436 bool is_sorted = true;
437
61ba1cf9
ILT
438 const unsigned char* p = pshdrs + This::shdr_size;
439 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
440 {
441 View_size* pvs = &(*pviews)[i];
442
443 pvs->view = NULL;
444
445 const Output_section* os = map_sections[i].output_section;
446 if (os == NULL)
447 continue;
730cdc88 448 off_t output_offset = map_sections[i].offset;
61ba1cf9
ILT
449
450 typename This::Shdr shdr(p);
451
452 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
453 continue;
454
6a74a719
ILT
455 if (parameters->output_is_object()
456 && (shdr.get_sh_type() == elfcpp::SHT_REL
457 || shdr.get_sh_type() == elfcpp::SHT_RELA)
458 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
459 {
460 // This is a reloc section in a relocatable link. We don't
461 // need to read the input file. The size and file offset
462 // are stored in the Relocatable_relocs structure.
463 Relocatable_relocs* rr = this->relocatable_relocs(i);
464 gold_assert(rr != NULL);
465 Output_data* posd = rr->output_data();
466 gold_assert(posd != NULL);
467
468 pvs->offset = posd->offset();
469 pvs->view_size = posd->data_size();
470 pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
471 pvs->address = posd->address();
472 pvs->is_input_output_view = false;
473 pvs->is_postprocessing_view = false;
474
475 continue;
476 }
477
96803768
ILT
478 // In the normal case, this input section is simply mapped to
479 // the output section at offset OUTPUT_OFFSET.
480
481 // However, if OUTPUT_OFFSET == -1, then input data is handled
482 // specially--e.g., a .eh_frame section. The relocation
483 // routines need to check for each reloc where it should be
484 // applied. For this case, we need an input/output view for the
485 // entire contents of the section in the output file. We don't
486 // want to copy the contents of the input section to the output
487 // section; the output section contents were already written,
488 // and we waited for them in Relocate_task::is_runnable because
489 // relocs_must_follow_section_writes is set for the object.
490
491 // Regardless of which of the above cases is true, we have to
492 // check requires_postprocessing of the output section. If that
493 // is false, then we work with views of the output file
494 // directly. If it is true, then we work with a separate
495 // buffer, and the output section is responsible for writing the
496 // final data to the output file.
497
498 off_t output_section_offset;
499 off_t output_section_size;
500 if (!os->requires_postprocessing())
501 {
502 output_section_offset = os->offset();
503 output_section_size = os->data_size();
504 }
505 else
506 {
507 output_section_offset = 0;
508 output_section_size = os->postprocessing_buffer_size();
509 }
510
730cdc88 511 off_t view_start;
fe8718a4 512 section_size_type view_size;
730cdc88
ILT
513 if (output_offset != -1)
514 {
96803768 515 view_start = output_section_offset + output_offset;
fe8718a4 516 view_size = convert_to_section_size_type(shdr.get_sh_size());
730cdc88
ILT
517 }
518 else
519 {
96803768 520 view_start = output_section_offset;
fe8718a4 521 view_size = convert_to_section_size_type(output_section_size);
730cdc88 522 }
61ba1cf9 523
730cdc88 524 if (view_size == 0)
ead1e424
ILT
525 continue;
526
730cdc88
ILT
527 gold_assert(output_offset == -1
528 || (output_offset >= 0
8d32f935
ILT
529 && (output_offset + static_cast<off_t>(view_size)
530 <= output_section_size)));
ead1e424 531
730cdc88 532 unsigned char* view;
96803768
ILT
533 if (os->requires_postprocessing())
534 {
535 unsigned char* buffer = os->postprocessing_buffer();
536 view = buffer + view_start;
537 if (output_offset != -1)
cb295612
ILT
538 {
539 off_t sh_offset = shdr.get_sh_offset();
540 if (!rm.empty() && rm.back().file_offset > sh_offset)
541 is_sorted = false;
542 rm.push_back(File_read::Read_multiple_entry(sh_offset,
543 view_size, view));
544 }
96803768 545 }
730cdc88
ILT
546 else
547 {
96803768
ILT
548 if (output_offset == -1)
549 view = of->get_input_output_view(view_start, view_size);
550 else
551 {
552 view = of->get_output_view(view_start, view_size);
cb295612
ILT
553 off_t sh_offset = shdr.get_sh_offset();
554 if (!rm.empty() && rm.back().file_offset > sh_offset)
555 is_sorted = false;
556 rm.push_back(File_read::Read_multiple_entry(sh_offset,
557 view_size, view));
96803768 558 }
730cdc88 559 }
92e059d8 560
61ba1cf9 561 pvs->view = view;
730cdc88
ILT
562 pvs->address = os->address();
563 if (output_offset != -1)
564 pvs->address += output_offset;
565 pvs->offset = view_start;
566 pvs->view_size = view_size;
567 pvs->is_input_output_view = output_offset == -1;
96803768 568 pvs->is_postprocessing_view = os->requires_postprocessing();
61ba1cf9 569 }
cb295612
ILT
570
571 // Actually read the data.
572 if (!rm.empty())
573 {
574 if (!is_sorted)
575 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
576 this->read_multiple(rm);
577 }
61ba1cf9
ILT
578}
579
580// Relocate section data. VIEWS points to the section data as views
581// in the output file.
582
583template<int size, bool big_endian>
584void
f6ce93d6 585Sized_relobj<size, big_endian>::relocate_sections(
92e059d8
ILT
586 const General_options& options,
587 const Symbol_table* symtab,
588 const Layout* layout,
589 const unsigned char* pshdrs,
590 Views* pviews)
61ba1cf9
ILT
591{
592 unsigned int shnum = this->shnum();
61ba1cf9
ILT
593 Sized_target<size, big_endian>* target = this->sized_target();
594
17a1d0a9 595 const std::vector<Map_to_output>& map_sections(this->map_to_output());
730cdc88 596
92e059d8
ILT
597 Relocate_info<size, big_endian> relinfo;
598 relinfo.options = &options;
599 relinfo.symtab = symtab;
600 relinfo.layout = layout;
601 relinfo.object = this;
92e059d8 602
61ba1cf9
ILT
603 const unsigned char* p = pshdrs + This::shdr_size;
604 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
605 {
606 typename This::Shdr shdr(p);
607
608 unsigned int sh_type = shdr.get_sh_type();
609 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
610 continue;
611
612 unsigned int index = shdr.get_sh_info();
613 if (index >= this->shnum())
614 {
75f2446e
ILT
615 this->error(_("relocation section %u has bad info %u"),
616 i, index);
617 continue;
61ba1cf9
ILT
618 }
619
730cdc88
ILT
620 Output_section* os = map_sections[index].output_section;
621 if (os == NULL)
61ba1cf9
ILT
622 {
623 // This relocation section is against a section which we
624 // discarded.
625 continue;
626 }
730cdc88 627 off_t output_offset = map_sections[index].offset;
61ba1cf9 628
a3ad94ed 629 gold_assert((*pviews)[index].view != NULL);
6a74a719
ILT
630 if (parameters->output_is_object())
631 gold_assert((*pviews)[i].view != NULL);
61ba1cf9 632
645f8123 633 if (shdr.get_sh_link() != this->symtab_shndx_)
61ba1cf9 634 {
75f2446e
ILT
635 gold_error(_("relocation section %u uses unexpected "
636 "symbol table %u"),
637 i, shdr.get_sh_link());
638 continue;
61ba1cf9
ILT
639 }
640
641 off_t sh_size = shdr.get_sh_size();
642 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
9eb9fa57 643 sh_size, false);
61ba1cf9
ILT
644
645 unsigned int reloc_size;
646 if (sh_type == elfcpp::SHT_REL)
647 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
648 else
649 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
650
651 if (reloc_size != shdr.get_sh_entsize())
652 {
75f2446e
ILT
653 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
654 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
730cdc88 655 reloc_size);
75f2446e 656 continue;
61ba1cf9
ILT
657 }
658
659 size_t reloc_count = sh_size / reloc_size;
f5c3f225 660 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
61ba1cf9 661 {
75f2446e
ILT
662 gold_error(_("reloc section %u size %lu uneven"),
663 i, static_cast<unsigned long>(sh_size));
664 continue;
61ba1cf9
ILT
665 }
666
96803768
ILT
667 gold_assert(output_offset != -1
668 || this->relocs_must_follow_section_writes());
669
92e059d8
ILT
670 relinfo.reloc_shndx = i;
671 relinfo.data_shndx = index;
6a74a719
ILT
672 if (!parameters->output_is_object())
673 target->relocate_section(&relinfo,
674 sh_type,
675 prelocs,
676 reloc_count,
677 os,
678 output_offset == -1,
679 (*pviews)[index].view,
680 (*pviews)[index].address,
681 (*pviews)[index].view_size);
682 else
683 {
684 Relocatable_relocs* rr = this->relocatable_relocs(i);
685 target->relocate_for_relocatable(&relinfo,
686 sh_type,
687 prelocs,
688 reloc_count,
689 os,
690 output_offset,
691 rr,
692 (*pviews)[index].view,
693 (*pviews)[index].address,
694 (*pviews)[index].view_size,
695 (*pviews)[i].view,
696 (*pviews)[i].view_size);
697 }
61ba1cf9
ILT
698 }
699}
700
a9a60db6
ILT
701// Create merge hash tables for the local symbols. These are used to
702// speed up relocations.
703
704template<int size, bool big_endian>
705void
706Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
707{
708 const unsigned int loccount = this->local_symbol_count_;
709 for (unsigned int i = 1; i < loccount; ++i)
710 {
711 Symbol_value<size>& lv(this->local_values_[i]);
712 lv.initialize_input_to_output_map(this);
713 }
714}
715
716// Free merge hash tables for the local symbols.
717
718template<int size, bool big_endian>
719void
720Sized_relobj<size, big_endian>::free_input_to_output_maps()
721{
722 const unsigned int loccount = this->local_symbol_count_;
723 for (unsigned int i = 1; i < loccount; ++i)
724 {
725 Symbol_value<size>& lv(this->local_values_[i]);
726 lv.free_input_to_output_map();
727 }
728}
729
730// Class Merged_symbol_value.
731
732template<int size>
733void
734Merged_symbol_value<size>::initialize_input_to_output_map(
735 const Relobj* object,
736 unsigned int input_shndx)
737{
738 Object_merge_map* map = object->merge_map();
739 map->initialize_input_to_output_map<size>(input_shndx,
740 this->output_start_address_,
741 &this->output_addresses_);
742}
743
744// Get the output value corresponding to an input offset if we
745// couldn't find it in the hash table.
746
747template<int size>
748typename elfcpp::Elf_types<size>::Elf_Addr
749Merged_symbol_value<size>::value_from_output_section(
750 const Relobj* object,
751 unsigned int input_shndx,
752 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
753{
754 section_offset_type output_offset;
755 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
756 input_offset,
757 &output_offset);
758
759 // If this assertion fails, it means that some relocation was
760 // against a portion of an input merge section which we didn't map
761 // to the output file and we didn't explicitly discard. We should
762 // always map all portions of input merge sections.
763 gold_assert(found);
764
765 if (output_offset == -1)
766 return 0;
767 else
768 return this->output_start_address_ + output_offset;
769}
770
5a6f7e2d
ILT
771// Copy_relocs::Copy_reloc_entry methods.
772
773// Return whether we should emit this reloc. We should emit it if the
774// symbol is still defined in a dynamic object. If we should not emit
775// it, we clear it, to save ourselves the test next time.
776
777template<int size, bool big_endian>
778bool
779Copy_relocs<size, big_endian>::Copy_reloc_entry::should_emit()
780{
781 if (this->sym_ == NULL)
782 return false;
14b31740 783 if (this->sym_->is_from_dynobj())
5a6f7e2d
ILT
784 return true;
785 this->sym_ = NULL;
786 return false;
787}
788
789// Emit a reloc into a SHT_REL section.
790
791template<int size, bool big_endian>
792void
793Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
794 Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>* reloc_data)
795{
16649710 796 this->sym_->set_needs_dynsym_entry();
4f4c5f80
ILT
797 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
798 this->relobj_, this->shndx_, this->address_);
5a6f7e2d
ILT
799}
800
801// Emit a reloc into a SHT_RELA section.
802
803template<int size, bool big_endian>
804void
805Copy_relocs<size, big_endian>::Copy_reloc_entry::emit(
806 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>* reloc_data)
807{
16649710 808 this->sym_->set_needs_dynsym_entry();
4f4c5f80
ILT
809 reloc_data->add_global(this->sym_, this->reloc_type_, this->output_section_,
810 this->relobj_, this->shndx_, this->address_,
811 this->addend_);
5a6f7e2d
ILT
812}
813
814// Copy_relocs methods.
a3ad94ed
ILT
815
816// Return whether we need a COPY reloc for a relocation against GSYM.
817// The relocation is being applied to section SHNDX in OBJECT.
818
819template<int size, bool big_endian>
820bool
5a6f7e2d 821Copy_relocs<size, big_endian>::need_copy_reloc(
a3ad94ed
ILT
822 const General_options*,
823 Relobj* object,
824 unsigned int shndx,
5a6f7e2d 825 Sized_symbol<size>* sym)
a3ad94ed
ILT
826{
827 // FIXME: Handle -z nocopyrelocs.
828
5a6f7e2d
ILT
829 if (sym->symsize() == 0)
830 return false;
831
a3ad94ed
ILT
832 // If this is a readonly section, then we need a COPY reloc.
833 // Otherwise we can use a dynamic reloc.
834 if ((object->section_flags(shndx) & elfcpp::SHF_WRITE) == 0)
835 return true;
836
837 return false;
838}
839
5a6f7e2d
ILT
840// Save a Rel reloc.
841
842template<int size, bool big_endian>
843void
844Copy_relocs<size, big_endian>::save(
845 Symbol* sym,
846 Relobj* relobj,
847 unsigned int shndx,
4f4c5f80 848 Output_section* output_section,
5a6f7e2d
ILT
849 const elfcpp::Rel<size, big_endian>& rel)
850{
851 unsigned int reloc_type = elfcpp::elf_r_type<size>(rel.get_r_info());
852 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
4f4c5f80
ILT
853 output_section,
854 rel.get_r_offset(), 0));
5a6f7e2d
ILT
855}
856
857// Save a Rela reloc.
858
859template<int size, bool big_endian>
860void
861Copy_relocs<size, big_endian>::save(
862 Symbol* sym,
863 Relobj* relobj,
864 unsigned int shndx,
4f4c5f80 865 Output_section* output_section,
5a6f7e2d
ILT
866 const elfcpp::Rela<size, big_endian>& rela)
867{
868 unsigned int reloc_type = elfcpp::elf_r_type<size>(rela.get_r_info());
869 this->entries_.push_back(Copy_reloc_entry(sym, reloc_type, relobj, shndx,
4f4c5f80 870 output_section,
5a6f7e2d
ILT
871 rela.get_r_offset(),
872 rela.get_r_addend()));
873}
874
875// Return whether there are any relocs to emit. We don't want to emit
876// a reloc if the symbol is no longer defined in a dynamic object.
877
878template<int size, bool big_endian>
879bool
880Copy_relocs<size, big_endian>::any_to_emit()
881{
882 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
883 p != this->entries_.end();
884 ++p)
885 {
886 if (p->should_emit())
887 return true;
888 }
889 return false;
890}
891
892// Emit relocs.
893
894template<int size, bool big_endian>
895template<int sh_type>
896void
897Copy_relocs<size, big_endian>::emit(
898 Output_data_reloc<sh_type, true, size, big_endian>* reloc_data)
899{
900 for (typename Copy_reloc_entries::iterator p = this->entries_.begin();
901 p != this->entries_.end();
902 ++p)
903 {
904 if (p->should_emit())
905 p->emit(reloc_data);
906 }
907}
908
730cdc88
ILT
909// Track_relocs methods.
910
911// Initialize the class to track the relocs. This gets the object,
912// the reloc section index, and the type of the relocs. This returns
913// false if something goes wrong.
914
915template<int size, bool big_endian>
916bool
917Track_relocs<size, big_endian>::initialize(
b696e6d4 918 Object* object,
730cdc88
ILT
919 unsigned int reloc_shndx,
920 unsigned int reloc_type)
921{
730cdc88
ILT
922 // If RELOC_SHNDX is -1U, it means there is more than one reloc
923 // section for the .eh_frame section. We can't handle that case.
924 if (reloc_shndx == -1U)
925 return false;
926
927 // If RELOC_SHNDX is 0, there is no reloc section.
928 if (reloc_shndx == 0)
929 return true;
930
931 // Get the contents of the reloc section.
932 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
933
934 if (reloc_type == elfcpp::SHT_REL)
935 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
936 else if (reloc_type == elfcpp::SHT_RELA)
937 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
938 else
939 gold_unreachable();
940
941 if (this->len_ % this->reloc_size_ != 0)
942 {
943 object->error(_("reloc section size %zu is not a multiple of "
944 "reloc size %d\n"),
945 static_cast<size_t>(this->len_),
946 this->reloc_size_);
947 return false;
948 }
949
950 return true;
951}
952
953// Return the offset of the next reloc, or -1 if there isn't one.
954
955template<int size, bool big_endian>
956off_t
957Track_relocs<size, big_endian>::next_offset() const
958{
959 if (this->pos_ >= this->len_)
960 return -1;
961
962 // Rel and Rela start out the same, so we can always use Rel to find
963 // the r_offset value.
964 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
965 return rel.get_r_offset();
966}
967
968// Return the index of the symbol referenced by the next reloc, or -1U
969// if there aren't any more relocs.
970
971template<int size, bool big_endian>
972unsigned int
973Track_relocs<size, big_endian>::next_symndx() const
974{
975 if (this->pos_ >= this->len_)
976 return -1U;
977
978 // Rel and Rela start out the same, so we can use Rel to find the
979 // symbol index.
980 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
981 return elfcpp::elf_r_sym<size>(rel.get_r_info());
982}
983
984// Advance to the next reloc whose r_offset is greater than or equal
985// to OFFSET. Return the number of relocs we skip.
986
987template<int size, bool big_endian>
988int
989Track_relocs<size, big_endian>::advance(off_t offset)
990{
991 int ret = 0;
992 while (this->pos_ < this->len_)
993 {
994 // Rel and Rela start out the same, so we can always use Rel to
995 // find the r_offset value.
996 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
997 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
998 break;
999 ++ret;
1000 this->pos_ += this->reloc_size_;
1001 }
1002 return ret;
1003}
1004
61ba1cf9
ILT
1005// Instantiate the templates we need. We could use the configure
1006// script to restrict this to only the ones for implemented targets.
1007
193a53d9 1008#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1009template
1010void
f6ce93d6 1011Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1012#endif
92e059d8 1013
193a53d9 1014#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1015template
1016void
f6ce93d6 1017Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1018#endif
92e059d8 1019
193a53d9 1020#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1021template
1022void
f6ce93d6 1023Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1024#endif
92e059d8 1025
193a53d9 1026#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1027template
1028void
f6ce93d6 1029Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1030#endif
92e059d8 1031
193a53d9 1032#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1033template
1034void
f6ce93d6 1035Sized_relobj<32, false>::do_scan_relocs(const General_options& options,
92e059d8 1036 Symbol_table* symtab,
ead1e424 1037 Layout* layout,
92e059d8 1038 Read_relocs_data* rd);
193a53d9 1039#endif
92e059d8 1040
193a53d9 1041#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1042template
1043void
f6ce93d6 1044Sized_relobj<32, true>::do_scan_relocs(const General_options& options,
92e059d8 1045 Symbol_table* symtab,
ead1e424 1046 Layout* layout,
92e059d8 1047 Read_relocs_data* rd);
193a53d9 1048#endif
92e059d8 1049
193a53d9 1050#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1051template
1052void
f6ce93d6 1053Sized_relobj<64, false>::do_scan_relocs(const General_options& options,
92e059d8 1054 Symbol_table* symtab,
ead1e424 1055 Layout* layout,
92e059d8 1056 Read_relocs_data* rd);
193a53d9 1057#endif
92e059d8 1058
193a53d9 1059#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1060template
1061void
f6ce93d6 1062Sized_relobj<64, true>::do_scan_relocs(const General_options& options,
92e059d8 1063 Symbol_table* symtab,
ead1e424 1064 Layout* layout,
92e059d8 1065 Read_relocs_data* rd);
193a53d9 1066#endif
92e059d8 1067
193a53d9 1068#ifdef HAVE_TARGET_32_LITTLE
61ba1cf9
ILT
1069template
1070void
f6ce93d6 1071Sized_relobj<32, false>::do_relocate(const General_options& options,
61ba1cf9 1072 const Symbol_table* symtab,
92e059d8 1073 const Layout* layout,
61ba1cf9 1074 Output_file* of);
193a53d9 1075#endif
61ba1cf9 1076
193a53d9 1077#ifdef HAVE_TARGET_32_BIG
61ba1cf9
ILT
1078template
1079void
f6ce93d6 1080Sized_relobj<32, true>::do_relocate(const General_options& options,
61ba1cf9 1081 const Symbol_table* symtab,
92e059d8 1082 const Layout* layout,
61ba1cf9 1083 Output_file* of);
193a53d9 1084#endif
61ba1cf9 1085
193a53d9 1086#ifdef HAVE_TARGET_64_LITTLE
61ba1cf9
ILT
1087template
1088void
f6ce93d6 1089Sized_relobj<64, false>::do_relocate(const General_options& options,
61ba1cf9 1090 const Symbol_table* symtab,
92e059d8 1091 const Layout* layout,
61ba1cf9 1092 Output_file* of);
193a53d9 1093#endif
61ba1cf9 1094
193a53d9 1095#ifdef HAVE_TARGET_64_BIG
61ba1cf9
ILT
1096template
1097void
f6ce93d6 1098Sized_relobj<64, true>::do_relocate(const General_options& options,
61ba1cf9 1099 const Symbol_table* symtab,
92e059d8 1100 const Layout* layout,
61ba1cf9 1101 Output_file* of);
193a53d9 1102#endif
61ba1cf9 1103
a9a60db6
ILT
1104#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1105template
1106class Merged_symbol_value<32>;
1107#endif
1108
1109#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1110template
1111class Merged_symbol_value<64>;
1112#endif
1113
1114#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1115template
1116class Symbol_value<32>;
1117#endif
1118
1119#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1120template
1121class Symbol_value<64>;
1122#endif
1123
193a53d9 1124#ifdef HAVE_TARGET_32_LITTLE
a3ad94ed 1125template
5a6f7e2d 1126class Copy_relocs<32, false>;
193a53d9 1127#endif
a3ad94ed 1128
193a53d9 1129#ifdef HAVE_TARGET_32_BIG
a3ad94ed 1130template
5a6f7e2d 1131class Copy_relocs<32, true>;
193a53d9 1132#endif
a3ad94ed 1133
193a53d9 1134#ifdef HAVE_TARGET_64_LITTLE
a3ad94ed 1135template
5a6f7e2d 1136class Copy_relocs<64, false>;
193a53d9 1137#endif
a3ad94ed 1138
193a53d9 1139#ifdef HAVE_TARGET_64_BIG
a3ad94ed 1140template
5a6f7e2d 1141class Copy_relocs<64, true>;
193a53d9 1142#endif
5a6f7e2d 1143
193a53d9 1144#ifdef HAVE_TARGET_32_LITTLE
5a6f7e2d
ILT
1145template
1146void
1147Copy_relocs<32, false>::emit<elfcpp::SHT_REL>(
1148 Output_data_reloc<elfcpp::SHT_REL, true, 32, false>*);
193a53d9 1149#endif
5a6f7e2d 1150
193a53d9 1151#ifdef HAVE_TARGET_32_BIG
5a6f7e2d
ILT
1152template
1153void
1154Copy_relocs<32, true>::emit<elfcpp::SHT_REL>(
1155 Output_data_reloc<elfcpp::SHT_REL, true, 32, true>*);
193a53d9 1156#endif
5a6f7e2d 1157
193a53d9 1158#ifdef HAVE_TARGET_64_LITTLE
5a6f7e2d
ILT
1159template
1160void
1161Copy_relocs<64, false>::emit<elfcpp::SHT_REL>(
1162 Output_data_reloc<elfcpp::SHT_REL, true, 64, false>*);
193a53d9 1163#endif
5a6f7e2d 1164
193a53d9 1165#ifdef HAVE_TARGET_64_BIG
5a6f7e2d
ILT
1166template
1167void
1168Copy_relocs<64, true>::emit<elfcpp::SHT_REL>(
1169 Output_data_reloc<elfcpp::SHT_REL, true, 64, true>*);
193a53d9 1170#endif
5a6f7e2d 1171
193a53d9 1172#ifdef HAVE_TARGET_32_LITTLE
5a6f7e2d
ILT
1173template
1174void
1175Copy_relocs<32, false>::emit<elfcpp::SHT_RELA>(
1176 Output_data_reloc<elfcpp::SHT_RELA , true, 32, false>*);
193a53d9 1177#endif
5a6f7e2d 1178
193a53d9 1179#ifdef HAVE_TARGET_32_BIG
5a6f7e2d
ILT
1180template
1181void
1182Copy_relocs<32, true>::emit<elfcpp::SHT_RELA>(
1183 Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>*);
193a53d9 1184#endif
5a6f7e2d 1185
193a53d9 1186#ifdef HAVE_TARGET_64_LITTLE
5a6f7e2d
ILT
1187template
1188void
1189Copy_relocs<64, false>::emit<elfcpp::SHT_RELA>(
1190 Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>*);
193a53d9 1191#endif
5a6f7e2d 1192
193a53d9 1193#ifdef HAVE_TARGET_64_BIG
5a6f7e2d
ILT
1194template
1195void
1196Copy_relocs<64, true>::emit<elfcpp::SHT_RELA>(
1197 Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>*);
193a53d9 1198#endif
61ba1cf9 1199
730cdc88
ILT
1200#ifdef HAVE_TARGET_32_LITTLE
1201template
1202class Track_relocs<32, false>;
1203#endif
1204
1205#ifdef HAVE_TARGET_32_BIG
1206template
1207class Track_relocs<32, true>;
1208#endif
1209
1210#ifdef HAVE_TARGET_64_LITTLE
1211template
1212class Track_relocs<64, false>;
1213#endif
1214
1215#ifdef HAVE_TARGET_64_BIG
1216template
1217class Track_relocs<64, true>;
1218#endif
1219
61ba1cf9 1220} // End namespace gold.
This page took 0.130767 seconds and 4 git commands to generate.