* descriptors.cc (Descriptors::open): Report correct name in error
[deliverable/binutils-gdb.git] / gold / reloc.cc
CommitLineData
61ba1cf9
ILT
1// reloc.cc -- relocate input files for gold.
2
93ceb764 3// Copyright 2006, 2007, 2008, 2009, 2010 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 33#include "reloc.h"
032ce4e9 34#include "icf.h"
a2e47362 35#include "compressed_output.h"
61ba1cf9
ILT
36
37namespace gold
38{
39
92e059d8
ILT
40// Read_relocs methods.
41
42// These tasks just read the relocation information from the file.
43// After reading it, the start another task to process the
44// information. These tasks requires access to the file.
45
17a1d0a9
ILT
46Task_token*
47Read_relocs::is_runnable()
92e059d8 48{
17a1d0a9 49 return this->object_->is_locked() ? this->object_->token() : NULL;
92e059d8
ILT
50}
51
52// Lock the file.
53
17a1d0a9
ILT
54void
55Read_relocs::locks(Task_locker* tl)
92e059d8 56{
17a1d0a9 57 tl->add(this, this->object_->token());
92e059d8
ILT
58}
59
60// Read the relocations and then start a Scan_relocs_task.
61
62void
63Read_relocs::run(Workqueue* workqueue)
64{
65 Read_relocs_data *rd = new Read_relocs_data;
66 this->object_->read_relocs(rd);
6d03d481 67 this->object_->set_relocs_data(rd);
17a1d0a9
ILT
68 this->object_->release();
69
ef15dade
ST
70 // If garbage collection or identical comdat folding is desired, we
71 // process the relocs first before scanning them. Scanning of relocs is
72 // done only after garbage or identical sections is identified.
032ce4e9
ST
73 if (parameters->options().gc_sections()
74 || parameters->options().icf_enabled())
ef15dade 75 {
ad0f2072 76 workqueue->queue_next(new Gc_process_relocs(this->symtab_,
6d03d481
ST
77 this->layout_,
78 this->object_, rd,
93ceb764
ILT
79 this->this_blocker_,
80 this->next_blocker_));
6d03d481
ST
81 }
82 else
83 {
ad0f2072
ILT
84 workqueue->queue_next(new Scan_relocs(this->symtab_, this->layout_,
85 this->object_, rd,
93ceb764
ILT
86 this->this_blocker_,
87 this->next_blocker_));
6d03d481 88 }
92e059d8
ILT
89}
90
c7912668
ILT
91// Return a debugging name for the task.
92
93std::string
94Read_relocs::get_name() const
95{
96 return "Read_relocs " + this->object_->name();
97}
98
6d03d481
ST
99// Gc_process_relocs methods.
100
93ceb764
ILT
101Gc_process_relocs::~Gc_process_relocs()
102{
103 if (this->this_blocker_ != NULL)
104 delete this->this_blocker_;
105}
106
107// These tasks process the relocations read by Read_relocs and
6d03d481 108// determine which sections are referenced and which are garbage.
93ceb764
ILT
109// This task is done only when --gc-sections is used. This is blocked
110// by THIS_BLOCKER_. It unblocks NEXT_BLOCKER_.
6d03d481
ST
111
112Task_token*
113Gc_process_relocs::is_runnable()
114{
93ceb764
ILT
115 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
116 return this->this_blocker_;
6d03d481
ST
117 if (this->object_->is_locked())
118 return this->object_->token();
119 return NULL;
120}
121
122void
123Gc_process_relocs::locks(Task_locker* tl)
124{
125 tl->add(this, this->object_->token());
93ceb764 126 tl->add(this, this->next_blocker_);
6d03d481
ST
127}
128
129void
130Gc_process_relocs::run(Workqueue*)
131{
ad0f2072 132 this->object_->gc_process_relocs(this->symtab_, this->layout_, this->rd_);
6d03d481
ST
133 this->object_->release();
134}
135
136// Return a debugging name for the task.
137
138std::string
139Gc_process_relocs::get_name() const
140{
141 return "Gc_process_relocs " + this->object_->name();
142}
143
92e059d8
ILT
144// Scan_relocs methods.
145
93ceb764
ILT
146Scan_relocs::~Scan_relocs()
147{
148 if (this->this_blocker_ != NULL)
149 delete this->this_blocker_;
150}
151
92e059d8
ILT
152// These tasks scan the relocations read by Read_relocs and mark up
153// the symbol table to indicate which relocations are required. We
154// use a lock on the symbol table to keep them from interfering with
155// each other.
156
17a1d0a9
ILT
157Task_token*
158Scan_relocs::is_runnable()
92e059d8 159{
93ceb764
ILT
160 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
161 return this->this_blocker_;
17a1d0a9
ILT
162 if (this->object_->is_locked())
163 return this->object_->token();
164 return NULL;
92e059d8
ILT
165}
166
167// Return the locks we hold: one on the file, one on the symbol table
168// and one blocker.
169
17a1d0a9
ILT
170void
171Scan_relocs::locks(Task_locker* tl)
92e059d8 172{
17a1d0a9 173 tl->add(this, this->object_->token());
93ceb764 174 tl->add(this, this->next_blocker_);
92e059d8
ILT
175}
176
177// Scan the relocs.
178
179void
180Scan_relocs::run(Workqueue*)
181{
ad0f2072 182 this->object_->scan_relocs(this->symtab_, this->layout_, this->rd_);
92e059d8
ILT
183 delete this->rd_;
184 this->rd_ = NULL;
a2a5469e 185 this->object_->release();
92e059d8
ILT
186}
187
c7912668
ILT
188// Return a debugging name for the task.
189
190std::string
191Scan_relocs::get_name() const
192{
193 return "Scan_relocs " + this->object_->name();
194}
195
61ba1cf9
ILT
196// Relocate_task methods.
197
730cdc88 198// We may have to wait for the output sections to be written.
61ba1cf9 199
17a1d0a9
ILT
200Task_token*
201Relocate_task::is_runnable()
61ba1cf9 202{
730cdc88
ILT
203 if (this->object_->relocs_must_follow_section_writes()
204 && this->output_sections_blocker_->is_blocked())
17a1d0a9 205 return this->output_sections_blocker_;
730cdc88 206
c7912668 207 if (this->object_->is_locked())
17a1d0a9 208 return this->object_->token();
c7912668 209
17a1d0a9 210 return NULL;
61ba1cf9
ILT
211}
212
213// We want to lock the file while we run. We want to unblock
730cdc88 214// INPUT_SECTIONS_BLOCKER and FINAL_BLOCKER when we are done.
17a1d0a9 215// INPUT_SECTIONS_BLOCKER may be NULL.
61ba1cf9 216
17a1d0a9
ILT
217void
218Relocate_task::locks(Task_locker* tl)
61ba1cf9 219{
17a1d0a9
ILT
220 if (this->input_sections_blocker_ != NULL)
221 tl->add(this, this->input_sections_blocker_);
222 tl->add(this, this->final_blocker_);
223 tl->add(this, this->object_->token());
61ba1cf9
ILT
224}
225
226// Run the task.
227
228void
229Relocate_task::run(Workqueue*)
230{
ad0f2072 231 this->object_->relocate(this->symtab_, this->layout_, this->of_);
cb295612
ILT
232
233 // This is normally the last thing we will do with an object, so
234 // uncache all views.
235 this->object_->clear_view_cache_marks();
236
17a1d0a9 237 this->object_->release();
61ba1cf9
ILT
238}
239
c7912668
ILT
240// Return a debugging name for the task.
241
242std::string
243Relocate_task::get_name() const
244{
245 return "Relocate_task " + this->object_->name();
246}
247
92e059d8
ILT
248// Read the relocs and local symbols from the object file and store
249// the information in RD.
250
251template<int size, bool big_endian>
252void
f6ce93d6 253Sized_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
92e059d8
ILT
254{
255 rd->relocs.clear();
256
2ea97941
ILT
257 unsigned int shnum = this->shnum();
258 if (shnum == 0)
92e059d8
ILT
259 return;
260
2ea97941 261 rd->relocs.reserve(shnum / 2);
92e059d8 262
ef9beddf
ILT
263 const Output_sections& out_sections(this->output_sections());
264 const std::vector<Address>& out_offsets(this->section_offsets_);
730cdc88 265
645f8123 266 const unsigned char *pshdrs = this->get_view(this->elf_file_.shoff(),
2ea97941 267 shnum * This::shdr_size,
39d0cb0e 268 true, true);
92e059d8
ILT
269 // Skip the first, dummy, section.
270 const unsigned char *ps = pshdrs + This::shdr_size;
2ea97941 271 for (unsigned int i = 1; i < shnum; ++i, ps += This::shdr_size)
92e059d8
ILT
272 {
273 typename This::Shdr shdr(ps);
274
275 unsigned int sh_type = shdr.get_sh_type();
276 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
277 continue;
278
d491d34e 279 unsigned int shndx = this->adjust_shndx(shdr.get_sh_info());
2ea97941 280 if (shndx >= shnum)
92e059d8 281 {
75f2446e
ILT
282 this->error(_("relocation section %u has bad info %u"),
283 i, shndx);
284 continue;
92e059d8
ILT
285 }
286
ef9beddf 287 Output_section* os = out_sections[shndx];
730cdc88 288 if (os == NULL)
92e059d8
ILT
289 continue;
290
ead1e424
ILT
291 // We are scanning relocations in order to fill out the GOT and
292 // PLT sections. Relocations for sections which are not
293 // allocated (typically debugging sections) should not add new
6a74a719 294 // GOT and PLT entries. So we skip them unless this is a
031cdbed
ILT
295 // relocatable link or we need to emit relocations. FIXME: What
296 // should we do if a linker script maps a section with SHF_ALLOC
297 // clear to a section with SHF_ALLOC set?
7019cd25
ILT
298 typename This::Shdr secshdr(pshdrs + shndx * This::shdr_size);
299 bool is_section_allocated = ((secshdr.get_sh_flags() & elfcpp::SHF_ALLOC)
300 != 0);
301 if (!is_section_allocated
8851ecca
ILT
302 && !parameters->options().relocatable()
303 && !parameters->options().emit_relocs())
7019cd25 304 continue;
ead1e424 305
d491d34e 306 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
92e059d8 307 {
75f2446e
ILT
308 this->error(_("relocation section %u uses unexpected "
309 "symbol table %u"),
d491d34e 310 i, this->adjust_shndx(shdr.get_sh_link()));
75f2446e 311 continue;
92e059d8
ILT
312 }
313
314 off_t sh_size = shdr.get_sh_size();
315
316 unsigned int reloc_size;
317 if (sh_type == elfcpp::SHT_REL)
318 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
319 else
320 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
321 if (reloc_size != shdr.get_sh_entsize())
322 {
75f2446e
ILT
323 this->error(_("unexpected entsize for reloc section %u: %lu != %u"),
324 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
325 reloc_size);
326 continue;
92e059d8
ILT
327 }
328
329 size_t reloc_count = sh_size / reloc_size;
f5c3f225 330 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
92e059d8 331 {
75f2446e
ILT
332 this->error(_("reloc section %u size %lu uneven"),
333 i, static_cast<unsigned long>(sh_size));
334 continue;
92e059d8
ILT
335 }
336
337 rd->relocs.push_back(Section_relocs());
338 Section_relocs& sr(rd->relocs.back());
339 sr.reloc_shndx = i;
340 sr.data_shndx = shndx;
9eb9fa57 341 sr.contents = this->get_lasting_view(shdr.get_sh_offset(), sh_size,
39d0cb0e 342 true, true);
92e059d8
ILT
343 sr.sh_type = sh_type;
344 sr.reloc_count = reloc_count;
730cdc88 345 sr.output_section = os;
a45248e0 346 sr.needs_special_offset_handling = out_offsets[shndx] == invalid_address;
7019cd25 347 sr.is_data_section_allocated = is_section_allocated;
92e059d8
ILT
348 }
349
350 // Read the local symbols.
a3ad94ed 351 gold_assert(this->symtab_shndx_ != -1U);
645f8123 352 if (this->symtab_shndx_ == 0 || this->local_symbol_count_ == 0)
92e059d8
ILT
353 rd->local_symbols = NULL;
354 else
355 {
356 typename This::Shdr symtabshdr(pshdrs
645f8123 357 + this->symtab_shndx_ * This::shdr_size);
a3ad94ed 358 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
2ea97941 359 const int sym_size = This::sym_size;
92e059d8 360 const unsigned int loccount = this->local_symbol_count_;
a3ad94ed 361 gold_assert(loccount == symtabshdr.get_sh_info());
2ea97941 362 off_t locsize = loccount * sym_size;
92e059d8 363 rd->local_symbols = this->get_lasting_view(symtabshdr.get_sh_offset(),
39d0cb0e 364 locsize, true, true);
92e059d8
ILT
365 }
366}
367
6d03d481
ST
368// Process the relocs to generate mappings from source sections to referenced
369// sections. This is used during garbage colletion to determine garbage
370// sections.
371
372template<int size, bool big_endian>
373void
ad0f2072 374Sized_relobj<size, big_endian>::do_gc_process_relocs(Symbol_table* symtab,
2ea97941 375 Layout* layout,
ad0f2072 376 Read_relocs_data* rd)
6d03d481 377{
029ba973
ILT
378 Sized_target<size, big_endian>* target =
379 parameters->sized_target<size, big_endian>();
6d03d481
ST
380
381 const unsigned char* local_symbols;
382 if (rd->local_symbols == NULL)
383 local_symbols = NULL;
384 else
385 local_symbols = rd->local_symbols->data();
386
387 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
388 p != rd->relocs.end();
389 ++p)
390 {
391 if (!parameters->options().relocatable())
392 {
393 // As noted above, when not generating an object file, we
394 // only scan allocated sections. We may see a non-allocated
395 // section here if we are emitting relocs.
396 if (p->is_data_section_allocated)
2ea97941 397 target->gc_process_relocs(symtab, layout, this,
6d03d481
ST
398 p->data_shndx, p->sh_type,
399 p->contents->data(), p->reloc_count,
400 p->output_section,
401 p->needs_special_offset_handling,
402 this->local_symbol_count_,
403 local_symbols);
404 }
405 }
406}
407
408
92e059d8
ILT
409// Scan the relocs and adjust the symbol table. This looks for
410// relocations which require GOT/PLT/COPY relocations.
411
412template<int size, bool big_endian>
413void
ad0f2072 414Sized_relobj<size, big_endian>::do_scan_relocs(Symbol_table* symtab,
2ea97941 415 Layout* layout,
92e059d8
ILT
416 Read_relocs_data* rd)
417{
029ba973
ILT
418 Sized_target<size, big_endian>* target =
419 parameters->sized_target<size, big_endian>();
92e059d8
ILT
420
421 const unsigned char* local_symbols;
422 if (rd->local_symbols == NULL)
423 local_symbols = NULL;
424 else
425 local_symbols = rd->local_symbols->data();
426
427 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
428 p != rd->relocs.end();
429 ++p)
430 {
6d03d481
ST
431 // When garbage collection is on, unreferenced sections are not included
432 // in the link that would have been included normally. This is known only
433 // after Read_relocs hence this check has to be done again.
032ce4e9
ST
434 if (parameters->options().gc_sections()
435 || parameters->options().icf_enabled())
6d03d481
ST
436 {
437 if (p->output_section == NULL)
438 continue;
439 }
8851ecca 440 if (!parameters->options().relocatable())
7019cd25
ILT
441 {
442 // As noted above, when not generating an object file, we
443 // only scan allocated sections. We may see a non-allocated
444 // section here if we are emitting relocs.
445 if (p->is_data_section_allocated)
2ea97941 446 target->scan_relocs(symtab, layout, this, p->data_shndx,
7019cd25
ILT
447 p->sh_type, p->contents->data(),
448 p->reloc_count, p->output_section,
449 p->needs_special_offset_handling,
450 this->local_symbol_count_,
451 local_symbols);
8851ecca 452 if (parameters->options().emit_relocs())
2ea97941 453 this->emit_relocs_scan(symtab, layout, local_symbols, p);
7019cd25 454 }
6a74a719
ILT
455 else
456 {
457 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
458 gold_assert(rr != NULL);
459 rr->set_reloc_count(p->reloc_count);
2ea97941 460 target->scan_relocatable_relocs(symtab, layout, this,
6a74a719
ILT
461 p->data_shndx, p->sh_type,
462 p->contents->data(),
463 p->reloc_count,
464 p->output_section,
465 p->needs_special_offset_handling,
466 this->local_symbol_count_,
467 local_symbols,
468 rr);
469 }
470
92e059d8
ILT
471 delete p->contents;
472 p->contents = NULL;
473 }
474
475 if (rd->local_symbols != NULL)
476 {
477 delete rd->local_symbols;
478 rd->local_symbols = NULL;
479 }
480}
481
7019cd25
ILT
482// This is a strategy class we use when scanning for --emit-relocs.
483
484template<int sh_type>
485class Emit_relocs_strategy
486{
487 public:
488 // A local non-section symbol.
489 inline Relocatable_relocs::Reloc_strategy
68943102 490 local_non_section_strategy(unsigned int, Relobj*, unsigned int)
7019cd25
ILT
491 { return Relocatable_relocs::RELOC_COPY; }
492
493 // A local section symbol.
494 inline Relocatable_relocs::Reloc_strategy
495 local_section_strategy(unsigned int, Relobj*)
496 {
497 if (sh_type == elfcpp::SHT_RELA)
498 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
499 else
500 {
501 // The addend is stored in the section contents. Since this
502 // is not a relocatable link, we are going to apply the
503 // relocation contents to the section as usual. This means
504 // that we have no way to record the original addend. If the
505 // original addend is not zero, there is basically no way for
506 // the user to handle this correctly. Caveat emptor.
507 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
508 }
509 }
510
511 // A global symbol.
512 inline Relocatable_relocs::Reloc_strategy
513 global_strategy(unsigned int, Relobj*, unsigned int)
514 { return Relocatable_relocs::RELOC_COPY; }
515};
516
517// Scan the input relocations for --emit-relocs.
518
519template<int size, bool big_endian>
520void
521Sized_relobj<size, big_endian>::emit_relocs_scan(
7019cd25 522 Symbol_table* symtab,
2ea97941 523 Layout* layout,
7019cd25
ILT
524 const unsigned char* plocal_syms,
525 const Read_relocs_data::Relocs_list::iterator& p)
526{
527 Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
528 gold_assert(rr != NULL);
529 rr->set_reloc_count(p->reloc_count);
530
531 if (p->sh_type == elfcpp::SHT_REL)
2ea97941 532 this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(symtab, layout,
7019cd25
ILT
533 plocal_syms, p, rr);
534 else
535 {
536 gold_assert(p->sh_type == elfcpp::SHT_RELA);
2ea97941 537 this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(symtab, layout,
ad0f2072 538 plocal_syms, p, rr);
7019cd25
ILT
539 }
540}
541
542// Scan the input relocation for --emit-relocs, templatized on the
543// type of the relocation section.
544
545template<int size, bool big_endian>
546template<int sh_type>
547void
548Sized_relobj<size, big_endian>::emit_relocs_scan_reltype(
7019cd25 549 Symbol_table* symtab,
2ea97941 550 Layout* layout,
7019cd25
ILT
551 const unsigned char* plocal_syms,
552 const Read_relocs_data::Relocs_list::iterator& p,
553 Relocatable_relocs* rr)
554{
555 scan_relocatable_relocs<size, big_endian, sh_type,
556 Emit_relocs_strategy<sh_type> >(
7019cd25 557 symtab,
2ea97941 558 layout,
7019cd25
ILT
559 this,
560 p->data_shndx,
561 p->contents->data(),
562 p->reloc_count,
563 p->output_section,
564 p->needs_special_offset_handling,
565 this->local_symbol_count_,
566 plocal_syms,
567 rr);
568}
569
61ba1cf9
ILT
570// Relocate the input sections and write out the local symbols.
571
572template<int size, bool big_endian>
573void
ad0f2072 574Sized_relobj<size, big_endian>::do_relocate(const Symbol_table* symtab,
2ea97941 575 const Layout* layout,
61ba1cf9
ILT
576 Output_file* of)
577{
2ea97941 578 unsigned int shnum = this->shnum();
61ba1cf9
ILT
579
580 // Read the section headers.
645f8123 581 const unsigned char* pshdrs = this->get_view(this->elf_file_.shoff(),
2ea97941 582 shnum * This::shdr_size,
39d0cb0e 583 true, true);
61ba1cf9
ILT
584
585 Views views;
2ea97941 586 views.resize(shnum);
61ba1cf9
ILT
587
588 // Make two passes over the sections. The first one copies the
589 // section data to the output file. The second one applies
590 // relocations.
591
592 this->write_sections(pshdrs, of, &views);
593
a9a60db6
ILT
594 // To speed up relocations, we set up hash tables for fast lookup of
595 // input offsets to output addresses.
596 this->initialize_input_to_output_maps();
597
61ba1cf9
ILT
598 // Apply relocations.
599
2ea97941 600 this->relocate_sections(symtab, layout, pshdrs, &views);
61ba1cf9 601
a9a60db6
ILT
602 // After we've done the relocations, we release the hash tables,
603 // since we no longer need them.
604 this->free_input_to_output_maps();
605
61ba1cf9 606 // Write out the accumulated views.
2ea97941 607 for (unsigned int i = 1; i < shnum; ++i)
61ba1cf9
ILT
608 {
609 if (views[i].view != NULL)
730cdc88 610 {
96803768
ILT
611 if (!views[i].is_postprocessing_view)
612 {
613 if (views[i].is_input_output_view)
614 of->write_input_output_view(views[i].offset,
615 views[i].view_size,
616 views[i].view);
617 else
618 of->write_output_view(views[i].offset, views[i].view_size,
619 views[i].view);
620 }
730cdc88 621 }
61ba1cf9
ILT
622 }
623
624 // Write out the local symbols.
2ea97941
ILT
625 this->write_local_symbols(of, layout->sympool(), layout->dynpool(),
626 layout->symtab_xindex(), layout->dynsym_xindex());
cb295612
ILT
627
628 // We should no longer need the local symbol values.
629 this->clear_local_symbols();
61ba1cf9
ILT
630}
631
cb295612
ILT
632// Sort a Read_multiple vector by file offset.
633struct Read_multiple_compare
634{
635 inline bool
636 operator()(const File_read::Read_multiple_entry& rme1,
637 const File_read::Read_multiple_entry& rme2) const
638 { return rme1.file_offset < rme2.file_offset; }
639};
640
61ba1cf9
ILT
641// Write section data to the output file. PSHDRS points to the
642// section headers. Record the views in *PVIEWS for use when
643// relocating.
644
645template<int size, bool big_endian>
646void
f6ce93d6 647Sized_relobj<size, big_endian>::write_sections(const unsigned char* pshdrs,
61ba1cf9 648 Output_file* of,
cb295612 649 Views* pviews)
61ba1cf9 650{
2ea97941 651 unsigned int shnum = this->shnum();
ef9beddf
ILT
652 const Output_sections& out_sections(this->output_sections());
653 const std::vector<Address>& out_offsets(this->section_offsets_);
61ba1cf9 654
cb295612
ILT
655 File_read::Read_multiple rm;
656 bool is_sorted = true;
657
61ba1cf9 658 const unsigned char* p = pshdrs + This::shdr_size;
2ea97941 659 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
61ba1cf9
ILT
660 {
661 View_size* pvs = &(*pviews)[i];
662
663 pvs->view = NULL;
664
ef9beddf 665 const Output_section* os = out_sections[i];
61ba1cf9
ILT
666 if (os == NULL)
667 continue;
ef9beddf 668 Address output_offset = out_offsets[i];
61ba1cf9
ILT
669
670 typename This::Shdr shdr(p);
671
672 if (shdr.get_sh_type() == elfcpp::SHT_NOBITS)
673 continue;
674
8851ecca
ILT
675 if ((parameters->options().relocatable()
676 || parameters->options().emit_relocs())
6a74a719
ILT
677 && (shdr.get_sh_type() == elfcpp::SHT_REL
678 || shdr.get_sh_type() == elfcpp::SHT_RELA)
679 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
680 {
7019cd25
ILT
681 // This is a reloc section in a relocatable link or when
682 // emitting relocs. We don't need to read the input file.
683 // The size and file offset are stored in the
684 // Relocatable_relocs structure.
6a74a719
ILT
685 Relocatable_relocs* rr = this->relocatable_relocs(i);
686 gold_assert(rr != NULL);
687 Output_data* posd = rr->output_data();
688 gold_assert(posd != NULL);
689
690 pvs->offset = posd->offset();
691 pvs->view_size = posd->data_size();
692 pvs->view = of->get_output_view(pvs->offset, pvs->view_size);
693 pvs->address = posd->address();
694 pvs->is_input_output_view = false;
695 pvs->is_postprocessing_view = false;
696
697 continue;
698 }
699
96803768
ILT
700 // In the normal case, this input section is simply mapped to
701 // the output section at offset OUTPUT_OFFSET.
702
eff45813
CC
703 // However, if OUTPUT_OFFSET == INVALID_ADDRESS, then input data is
704 // handled specially--e.g., a .eh_frame section. The relocation
96803768
ILT
705 // routines need to check for each reloc where it should be
706 // applied. For this case, we need an input/output view for the
707 // entire contents of the section in the output file. We don't
708 // want to copy the contents of the input section to the output
709 // section; the output section contents were already written,
710 // and we waited for them in Relocate_task::is_runnable because
711 // relocs_must_follow_section_writes is set for the object.
712
713 // Regardless of which of the above cases is true, we have to
714 // check requires_postprocessing of the output section. If that
715 // is false, then we work with views of the output file
716 // directly. If it is true, then we work with a separate
717 // buffer, and the output section is responsible for writing the
718 // final data to the output file.
719
2ea97941 720 off_t output_section_offset;
ef9beddf 721 Address output_section_size;
96803768
ILT
722 if (!os->requires_postprocessing())
723 {
2ea97941 724 output_section_offset = os->offset();
ef9beddf 725 output_section_size = convert_types<Address, off_t>(os->data_size());
96803768
ILT
726 }
727 else
728 {
2ea97941 729 output_section_offset = 0;
ef9beddf
ILT
730 output_section_size =
731 convert_types<Address, off_t>(os->postprocessing_buffer_size());
96803768
ILT
732 }
733
730cdc88 734 off_t view_start;
fe8718a4 735 section_size_type view_size;
a2e47362 736 bool must_decompress = false;
eff45813 737 if (output_offset != invalid_address)
730cdc88 738 {
2ea97941 739 view_start = output_section_offset + output_offset;
fe8718a4 740 view_size = convert_to_section_size_type(shdr.get_sh_size());
a2e47362
CC
741 section_size_type uncompressed_size;
742 if (this->section_is_compressed(i, &uncompressed_size))
743 {
744 view_size = uncompressed_size;
745 must_decompress = true;
746 }
730cdc88
ILT
747 }
748 else
749 {
2ea97941 750 view_start = output_section_offset;
fe8718a4 751 view_size = convert_to_section_size_type(output_section_size);
730cdc88 752 }
61ba1cf9 753
730cdc88 754 if (view_size == 0)
ead1e424
ILT
755 continue;
756
eff45813 757 gold_assert(output_offset == invalid_address
ef9beddf 758 || output_offset + view_size <= output_section_size);
ead1e424 759
2ea97941 760 unsigned char* view;
96803768
ILT
761 if (os->requires_postprocessing())
762 {
763 unsigned char* buffer = os->postprocessing_buffer();
2ea97941 764 view = buffer + view_start;
a2e47362 765 if (output_offset != invalid_address && !must_decompress)
cb295612
ILT
766 {
767 off_t sh_offset = shdr.get_sh_offset();
768 if (!rm.empty() && rm.back().file_offset > sh_offset)
769 is_sorted = false;
770 rm.push_back(File_read::Read_multiple_entry(sh_offset,
2ea97941 771 view_size, view));
cb295612 772 }
96803768 773 }
730cdc88
ILT
774 else
775 {
eff45813 776 if (output_offset == invalid_address)
2ea97941 777 view = of->get_input_output_view(view_start, view_size);
96803768
ILT
778 else
779 {
2ea97941 780 view = of->get_output_view(view_start, view_size);
a2e47362
CC
781 if (!must_decompress)
782 {
783 off_t sh_offset = shdr.get_sh_offset();
784 if (!rm.empty() && rm.back().file_offset > sh_offset)
785 is_sorted = false;
786 rm.push_back(File_read::Read_multiple_entry(sh_offset,
787 view_size, view));
788 }
96803768 789 }
730cdc88 790 }
92e059d8 791
a2e47362
CC
792 if (must_decompress)
793 {
794 // Read and decompress the section.
795 section_size_type len;
796 const unsigned char* p = this->section_contents(i, &len, false);
797 if (!decompress_input_section(p, len, view, view_size))
798 this->error(_("could not decompress section %s"),
799 this->section_name(i).c_str());
800 }
801
2ea97941 802 pvs->view = view;
730cdc88 803 pvs->address = os->address();
eff45813 804 if (output_offset != invalid_address)
730cdc88
ILT
805 pvs->address += output_offset;
806 pvs->offset = view_start;
807 pvs->view_size = view_size;
eff45813 808 pvs->is_input_output_view = output_offset == invalid_address;
96803768 809 pvs->is_postprocessing_view = os->requires_postprocessing();
61ba1cf9 810 }
cb295612
ILT
811
812 // Actually read the data.
813 if (!rm.empty())
814 {
815 if (!is_sorted)
816 std::sort(rm.begin(), rm.end(), Read_multiple_compare());
817 this->read_multiple(rm);
818 }
61ba1cf9
ILT
819}
820
821// Relocate section data. VIEWS points to the section data as views
822// in the output file.
823
824template<int size, bool big_endian>
825void
72adc4fa 826Sized_relobj<size, big_endian>::do_relocate_sections(
92e059d8 827 const Symbol_table* symtab,
2ea97941 828 const Layout* layout,
92e059d8
ILT
829 const unsigned char* pshdrs,
830 Views* pviews)
61ba1cf9 831{
2ea97941 832 unsigned int shnum = this->shnum();
029ba973
ILT
833 Sized_target<size, big_endian>* target =
834 parameters->sized_target<size, big_endian>();
61ba1cf9 835
ef9beddf
ILT
836 const Output_sections& out_sections(this->output_sections());
837 const std::vector<Address>& out_offsets(this->section_offsets_);
730cdc88 838
92e059d8 839 Relocate_info<size, big_endian> relinfo;
92e059d8 840 relinfo.symtab = symtab;
2ea97941 841 relinfo.layout = layout;
92e059d8 842 relinfo.object = this;
92e059d8 843
61ba1cf9 844 const unsigned char* p = pshdrs + This::shdr_size;
2ea97941 845 for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
61ba1cf9
ILT
846 {
847 typename This::Shdr shdr(p);
848
849 unsigned int sh_type = shdr.get_sh_type();
850 if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
851 continue;
852
1307d6cd
ILT
853 off_t sh_size = shdr.get_sh_size();
854 if (sh_size == 0)
855 continue;
856
d491d34e 857 unsigned int index = this->adjust_shndx(shdr.get_sh_info());
61ba1cf9
ILT
858 if (index >= this->shnum())
859 {
75f2446e
ILT
860 this->error(_("relocation section %u has bad info %u"),
861 i, index);
862 continue;
61ba1cf9
ILT
863 }
864
ef9beddf 865 Output_section* os = out_sections[index];
730cdc88 866 if (os == NULL)
61ba1cf9
ILT
867 {
868 // This relocation section is against a section which we
869 // discarded.
870 continue;
871 }
ef9beddf 872 Address output_offset = out_offsets[index];
61ba1cf9 873
a3ad94ed 874 gold_assert((*pviews)[index].view != NULL);
8851ecca 875 if (parameters->options().relocatable())
6a74a719 876 gold_assert((*pviews)[i].view != NULL);
61ba1cf9 877
d491d34e 878 if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx_)
61ba1cf9 879 {
75f2446e
ILT
880 gold_error(_("relocation section %u uses unexpected "
881 "symbol table %u"),
d491d34e 882 i, this->adjust_shndx(shdr.get_sh_link()));
75f2446e 883 continue;
61ba1cf9
ILT
884 }
885
61ba1cf9 886 const unsigned char* prelocs = this->get_view(shdr.get_sh_offset(),
39d0cb0e 887 sh_size, true, false);
61ba1cf9
ILT
888
889 unsigned int reloc_size;
890 if (sh_type == elfcpp::SHT_REL)
891 reloc_size = elfcpp::Elf_sizes<size>::rel_size;
892 else
893 reloc_size = elfcpp::Elf_sizes<size>::rela_size;
894
895 if (reloc_size != shdr.get_sh_entsize())
896 {
75f2446e
ILT
897 gold_error(_("unexpected entsize for reloc section %u: %lu != %u"),
898 i, static_cast<unsigned long>(shdr.get_sh_entsize()),
730cdc88 899 reloc_size);
75f2446e 900 continue;
61ba1cf9
ILT
901 }
902
903 size_t reloc_count = sh_size / reloc_size;
f5c3f225 904 if (static_cast<off_t>(reloc_count * reloc_size) != sh_size)
61ba1cf9 905 {
75f2446e
ILT
906 gold_error(_("reloc section %u size %lu uneven"),
907 i, static_cast<unsigned long>(sh_size));
908 continue;
61ba1cf9
ILT
909 }
910
eff45813 911 gold_assert(output_offset != invalid_address
96803768
ILT
912 || this->relocs_must_follow_section_writes());
913
92e059d8 914 relinfo.reloc_shndx = i;
82bb573a 915 relinfo.reloc_shdr = p;
92e059d8 916 relinfo.data_shndx = index;
82bb573a 917 relinfo.data_shdr = pshdrs + index * This::shdr_size;
2ea97941 918 unsigned char* view = (*pviews)[index].view;
364c7fa5
ILT
919 Address address = (*pviews)[index].address;
920 section_size_type view_size = (*pviews)[index].view_size;
921
922 Reloc_symbol_changes* reloc_map = NULL;
923 if (this->uses_split_stack() && output_offset != invalid_address)
924 {
925 typename This::Shdr data_shdr(pshdrs + index * This::shdr_size);
926 if ((data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
927 this->split_stack_adjust(symtab, pshdrs, sh_type, index,
2ea97941 928 prelocs, reloc_count, view, view_size,
364c7fa5
ILT
929 &reloc_map);
930 }
931
8851ecca 932 if (!parameters->options().relocatable())
7019cd25 933 {
364c7fa5 934 target->relocate_section(&relinfo, sh_type, prelocs, reloc_count, os,
eff45813 935 output_offset == invalid_address,
2ea97941 936 view, address, view_size, reloc_map);
8851ecca 937 if (parameters->options().emit_relocs())
7019cd25 938 this->emit_relocs(&relinfo, i, sh_type, prelocs, reloc_count,
2ea97941 939 os, output_offset, view, address, view_size,
364c7fa5 940 (*pviews)[i].view, (*pviews)[i].view_size);
7019cd25 941 }
6a74a719
ILT
942 else
943 {
944 Relocatable_relocs* rr = this->relocatable_relocs(i);
364c7fa5
ILT
945 target->relocate_for_relocatable(&relinfo, sh_type, prelocs,
946 reloc_count, os, output_offset, rr,
2ea97941 947 view, address, view_size,
6a74a719
ILT
948 (*pviews)[i].view,
949 (*pviews)[i].view_size);
950 }
61ba1cf9
ILT
951 }
952}
953
7019cd25
ILT
954// Emit the relocs for --emit-relocs.
955
956template<int size, bool big_endian>
957void
958Sized_relobj<size, big_endian>::emit_relocs(
959 const Relocate_info<size, big_endian>* relinfo,
960 unsigned int i,
961 unsigned int sh_type,
962 const unsigned char* prelocs,
963 size_t reloc_count,
2ea97941 964 Output_section* output_section,
ef9beddf 965 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
2ea97941 966 unsigned char* view,
7019cd25
ILT
967 typename elfcpp::Elf_types<size>::Elf_Addr address,
968 section_size_type view_size,
969 unsigned char* reloc_view,
970 section_size_type reloc_view_size)
971{
972 if (sh_type == elfcpp::SHT_REL)
973 this->emit_relocs_reltype<elfcpp::SHT_REL>(relinfo, i, prelocs,
2ea97941 974 reloc_count, output_section,
7019cd25 975 offset_in_output_section,
2ea97941 976 view, address, view_size,
7019cd25
ILT
977 reloc_view, reloc_view_size);
978 else
979 {
980 gold_assert(sh_type == elfcpp::SHT_RELA);
981 this->emit_relocs_reltype<elfcpp::SHT_RELA>(relinfo, i, prelocs,
2ea97941 982 reloc_count, output_section,
7019cd25 983 offset_in_output_section,
2ea97941 984 view, address, view_size,
7019cd25
ILT
985 reloc_view, reloc_view_size);
986 }
987}
988
989// Emit the relocs for --emit-relocs, templatized on the type of the
990// relocation section.
991
992template<int size, bool big_endian>
993template<int sh_type>
994void
995Sized_relobj<size, big_endian>::emit_relocs_reltype(
996 const Relocate_info<size, big_endian>* relinfo,
997 unsigned int i,
998 const unsigned char* prelocs,
999 size_t reloc_count,
2ea97941 1000 Output_section* output_section,
ef9beddf 1001 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section,
2ea97941 1002 unsigned char* view,
7019cd25
ILT
1003 typename elfcpp::Elf_types<size>::Elf_Addr address,
1004 section_size_type view_size,
1005 unsigned char* reloc_view,
1006 section_size_type reloc_view_size)
1007{
1008 const Relocatable_relocs* rr = this->relocatable_relocs(i);
1009 relocate_for_relocatable<size, big_endian, sh_type>(
1010 relinfo,
1011 prelocs,
1012 reloc_count,
2ea97941 1013 output_section,
7019cd25
ILT
1014 offset_in_output_section,
1015 rr,
2ea97941 1016 view,
7019cd25
ILT
1017 address,
1018 view_size,
1019 reloc_view,
1020 reloc_view_size);
1021}
1022
a9a60db6
ILT
1023// Create merge hash tables for the local symbols. These are used to
1024// speed up relocations.
1025
1026template<int size, bool big_endian>
1027void
1028Sized_relobj<size, big_endian>::initialize_input_to_output_maps()
1029{
1030 const unsigned int loccount = this->local_symbol_count_;
1031 for (unsigned int i = 1; i < loccount; ++i)
1032 {
1033 Symbol_value<size>& lv(this->local_values_[i]);
1034 lv.initialize_input_to_output_map(this);
1035 }
1036}
1037
1038// Free merge hash tables for the local symbols.
1039
1040template<int size, bool big_endian>
1041void
1042Sized_relobj<size, big_endian>::free_input_to_output_maps()
1043{
1044 const unsigned int loccount = this->local_symbol_count_;
1045 for (unsigned int i = 1; i < loccount; ++i)
1046 {
1047 Symbol_value<size>& lv(this->local_values_[i]);
1048 lv.free_input_to_output_map();
1049 }
1050}
1051
364c7fa5
ILT
1052// If an object was compiled with -fsplit-stack, this is called to
1053// check whether any relocations refer to functions defined in objects
1054// which were not compiled with -fsplit-stack. If they were, then we
1055// need to apply some target-specific adjustments to request
1056// additional stack space.
1057
1058template<int size, bool big_endian>
1059void
1060Sized_relobj<size, big_endian>::split_stack_adjust(
1061 const Symbol_table* symtab,
1062 const unsigned char* pshdrs,
1063 unsigned int sh_type,
1064 unsigned int shndx,
1065 const unsigned char* prelocs,
1066 size_t reloc_count,
2ea97941 1067 unsigned char* view,
364c7fa5
ILT
1068 section_size_type view_size,
1069 Reloc_symbol_changes** reloc_map)
1070{
1071 if (sh_type == elfcpp::SHT_REL)
1072 this->split_stack_adjust_reltype<elfcpp::SHT_REL>(symtab, pshdrs, shndx,
1073 prelocs, reloc_count,
2ea97941 1074 view, view_size,
364c7fa5
ILT
1075 reloc_map);
1076 else
1077 {
1078 gold_assert(sh_type == elfcpp::SHT_RELA);
1079 this->split_stack_adjust_reltype<elfcpp::SHT_RELA>(symtab, pshdrs, shndx,
1080 prelocs, reloc_count,
2ea97941 1081 view, view_size,
364c7fa5
ILT
1082 reloc_map);
1083 }
1084}
1085
1086// Adjust for -fsplit-stack, templatized on the type of the relocation
1087// section.
1088
1089template<int size, bool big_endian>
1090template<int sh_type>
1091void
1092Sized_relobj<size, big_endian>::split_stack_adjust_reltype(
1093 const Symbol_table* symtab,
1094 const unsigned char* pshdrs,
1095 unsigned int shndx,
1096 const unsigned char* prelocs,
1097 size_t reloc_count,
2ea97941 1098 unsigned char* view,
364c7fa5
ILT
1099 section_size_type view_size,
1100 Reloc_symbol_changes** reloc_map)
1101{
1102 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
1103 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
1104
1105 size_t local_count = this->local_symbol_count();
1106
1107 std::vector<section_offset_type> non_split_refs;
1108
1109 const unsigned char* pr = prelocs;
1110 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1111 {
1112 Reltype reloc(pr);
1113
1114 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
1115 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1116 if (r_sym < local_count)
1117 continue;
1118
1119 const Symbol* gsym = this->global_symbol(r_sym);
1120 gold_assert(gsym != NULL);
1121 if (gsym->is_forwarder())
1122 gsym = symtab->resolve_forwards(gsym);
1123
1124 // See if this relocation refers to a function defined in an
1125 // object compiled without -fsplit-stack. Note that we don't
1126 // care about the type of relocation--this means that in some
1127 // cases we will ask for a large stack unnecessarily, but this
1128 // is not fatal. FIXME: Some targets have symbols which are
1129 // functions but are not type STT_FUNC, e.g., STT_ARM_TFUNC.
b6848d3c 1130 if (!gsym->is_undefined()
364c7fa5
ILT
1131 && gsym->source() == Symbol::FROM_OBJECT
1132 && !gsym->object()->uses_split_stack())
1133 {
b6848d3c
ILT
1134 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1135 if (parameters->target().is_call_to_non_split(gsym, r_type))
1136 {
1137 section_offset_type offset =
1138 convert_to_section_size_type(reloc.get_r_offset());
1139 non_split_refs.push_back(offset);
1140 }
364c7fa5
ILT
1141 }
1142 }
1143
1144 if (non_split_refs.empty())
1145 return;
1146
1147 // At this point, every entry in NON_SPLIT_REFS indicates a
1148 // relocation which refers to a function in an object compiled
1149 // without -fsplit-stack. We now have to convert that list into a
1150 // set of offsets to functions. First, we find all the functions.
1151
1152 Function_offsets function_offsets;
1153 this->find_functions(pshdrs, shndx, &function_offsets);
1154 if (function_offsets.empty())
1155 return;
1156
1157 // Now get a list of the function with references to non split-stack
1158 // code.
1159
1160 Function_offsets calls_non_split;
1161 for (std::vector<section_offset_type>::const_iterator p
1162 = non_split_refs.begin();
1163 p != non_split_refs.end();
1164 ++p)
1165 {
1166 Function_offsets::const_iterator low = function_offsets.lower_bound(*p);
1167 if (low == function_offsets.end())
1168 --low;
1169 else if (low->first == *p)
1170 ;
1171 else if (low == function_offsets.begin())
1172 continue;
1173 else
1174 --low;
1175
1176 calls_non_split.insert(*low);
1177 }
1178 if (calls_non_split.empty())
1179 return;
1180
1181 // Now we have a set of functions to adjust. The adjustments are
1182 // target specific. Besides changing the output section view
1183 // however, it likes, the target may request a relocation change
1184 // from one global symbol name to another.
1185
1186 for (Function_offsets::const_iterator p = calls_non_split.begin();
1187 p != calls_non_split.end();
1188 ++p)
1189 {
1190 std::string from;
1191 std::string to;
1192 parameters->target().calls_non_split(this, shndx, p->first, p->second,
2ea97941 1193 view, view_size, &from, &to);
364c7fa5
ILT
1194 if (!from.empty())
1195 {
1196 gold_assert(!to.empty());
1197 Symbol* tosym = NULL;
1198
1199 // Find relocations in the relevant function which are for
1200 // FROM.
1201 pr = prelocs;
1202 for (size_t i = 0; i < reloc_count; ++i, pr += reloc_size)
1203 {
1204 Reltype reloc(pr);
1205
1206 typename elfcpp::Elf_types<size>::Elf_WXword r_info =
1207 reloc.get_r_info();
1208 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1209 if (r_sym < local_count)
1210 continue;
1211
2ea97941 1212 section_offset_type offset =
364c7fa5 1213 convert_to_section_size_type(reloc.get_r_offset());
2ea97941
ILT
1214 if (offset < p->first
1215 || (offset
364c7fa5
ILT
1216 >= (p->first
1217 + static_cast<section_offset_type>(p->second))))
1218 continue;
1219
1220 const Symbol* gsym = this->global_symbol(r_sym);
1221 if (from == gsym->name())
1222 {
1223 if (tosym == NULL)
1224 {
1225 tosym = symtab->lookup(to.c_str());
1226 if (tosym == NULL)
1227 {
1228 this->error(_("could not convert call "
1229 "to '%s' to '%s'"),
1230 from.c_str(), to.c_str());
1231 break;
1232 }
1233 }
1234
1235 if (*reloc_map == NULL)
1236 *reloc_map = new Reloc_symbol_changes(reloc_count);
1237 (*reloc_map)->set(i, tosym);
1238 }
1239 }
1240 }
1241 }
1242}
1243
1244// Find all the function in this object defined in section SHNDX.
1245// Store their offsets in the section in FUNCTION_OFFSETS.
1246
1247template<int size, bool big_endian>
1248void
1249Sized_relobj<size, big_endian>::find_functions(
1250 const unsigned char* pshdrs,
1251 unsigned int shndx,
1252 Sized_relobj<size, big_endian>::Function_offsets* function_offsets)
1253{
1254 // We need to read the symbols to find the functions. If we wanted
1255 // to, we could cache reading the symbols across all sections in the
1256 // object.
2ea97941
ILT
1257 const unsigned int symtab_shndx = this->symtab_shndx_;
1258 typename This::Shdr symtabshdr(pshdrs + symtab_shndx * This::shdr_size);
364c7fa5
ILT
1259 gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1260
1261 typename elfcpp::Elf_types<size>::Elf_WXword sh_size =
1262 symtabshdr.get_sh_size();
1263 const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1264 sh_size, true, true);
1265
2ea97941
ILT
1266 const int sym_size = This::sym_size;
1267 const unsigned int symcount = sh_size / sym_size;
1268 for (unsigned int i = 0; i < symcount; ++i, psyms += sym_size)
364c7fa5
ILT
1269 {
1270 typename elfcpp::Sym<size, big_endian> isym(psyms);
1271
1272 // FIXME: Some targets can have functions which do not have type
1273 // STT_FUNC, e.g., STT_ARM_TFUNC.
1274 if (isym.get_st_type() != elfcpp::STT_FUNC
1275 || isym.get_st_size() == 0)
1276 continue;
1277
1278 bool is_ordinary;
1279 unsigned int sym_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1280 &is_ordinary);
1281 if (!is_ordinary || sym_shndx != shndx)
1282 continue;
1283
1284 section_offset_type value =
1285 convert_to_section_size_type(isym.get_st_value());
1286 section_size_type fnsize =
1287 convert_to_section_size_type(isym.get_st_size());
1288
1289 (*function_offsets)[value] = fnsize;
1290 }
1291}
1292
a9a60db6
ILT
1293// Class Merged_symbol_value.
1294
1295template<int size>
1296void
1297Merged_symbol_value<size>::initialize_input_to_output_map(
1298 const Relobj* object,
1299 unsigned int input_shndx)
1300{
1301 Object_merge_map* map = object->merge_map();
1302 map->initialize_input_to_output_map<size>(input_shndx,
1303 this->output_start_address_,
1304 &this->output_addresses_);
1305}
1306
1307// Get the output value corresponding to an input offset if we
1308// couldn't find it in the hash table.
1309
1310template<int size>
1311typename elfcpp::Elf_types<size>::Elf_Addr
1312Merged_symbol_value<size>::value_from_output_section(
1313 const Relobj* object,
1314 unsigned int input_shndx,
1315 typename elfcpp::Elf_types<size>::Elf_Addr input_offset) const
1316{
1317 section_offset_type output_offset;
1318 bool found = object->merge_map()->get_output_offset(NULL, input_shndx,
1319 input_offset,
1320 &output_offset);
1321
1322 // If this assertion fails, it means that some relocation was
1323 // against a portion of an input merge section which we didn't map
1324 // to the output file and we didn't explicitly discard. We should
1325 // always map all portions of input merge sections.
1326 gold_assert(found);
1327
1328 if (output_offset == -1)
1329 return 0;
1330 else
1331 return this->output_start_address_ + output_offset;
1332}
1333
730cdc88
ILT
1334// Track_relocs methods.
1335
1336// Initialize the class to track the relocs. This gets the object,
1337// the reloc section index, and the type of the relocs. This returns
1338// false if something goes wrong.
1339
1340template<int size, bool big_endian>
1341bool
1342Track_relocs<size, big_endian>::initialize(
b696e6d4 1343 Object* object,
730cdc88
ILT
1344 unsigned int reloc_shndx,
1345 unsigned int reloc_type)
1346{
730cdc88
ILT
1347 // If RELOC_SHNDX is -1U, it means there is more than one reloc
1348 // section for the .eh_frame section. We can't handle that case.
1349 if (reloc_shndx == -1U)
1350 return false;
1351
1352 // If RELOC_SHNDX is 0, there is no reloc section.
1353 if (reloc_shndx == 0)
1354 return true;
1355
1356 // Get the contents of the reloc section.
1357 this->prelocs_ = object->section_contents(reloc_shndx, &this->len_, false);
1358
1359 if (reloc_type == elfcpp::SHT_REL)
1360 this->reloc_size_ = elfcpp::Elf_sizes<size>::rel_size;
1361 else if (reloc_type == elfcpp::SHT_RELA)
1362 this->reloc_size_ = elfcpp::Elf_sizes<size>::rela_size;
1363 else
1364 gold_unreachable();
1365
1366 if (this->len_ % this->reloc_size_ != 0)
1367 {
1368 object->error(_("reloc section size %zu is not a multiple of "
1369 "reloc size %d\n"),
1370 static_cast<size_t>(this->len_),
1371 this->reloc_size_);
1372 return false;
1373 }
1374
1375 return true;
1376}
1377
1378// Return the offset of the next reloc, or -1 if there isn't one.
1379
1380template<int size, bool big_endian>
1381off_t
1382Track_relocs<size, big_endian>::next_offset() const
1383{
1384 if (this->pos_ >= this->len_)
1385 return -1;
1386
1387 // Rel and Rela start out the same, so we can always use Rel to find
1388 // the r_offset value.
1389 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1390 return rel.get_r_offset();
1391}
1392
1393// Return the index of the symbol referenced by the next reloc, or -1U
1394// if there aren't any more relocs.
1395
1396template<int size, bool big_endian>
1397unsigned int
1398Track_relocs<size, big_endian>::next_symndx() const
1399{
1400 if (this->pos_ >= this->len_)
1401 return -1U;
1402
1403 // Rel and Rela start out the same, so we can use Rel to find the
1404 // symbol index.
1405 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1406 return elfcpp::elf_r_sym<size>(rel.get_r_info());
1407}
1408
1409// Advance to the next reloc whose r_offset is greater than or equal
1410// to OFFSET. Return the number of relocs we skip.
1411
1412template<int size, bool big_endian>
1413int
1414Track_relocs<size, big_endian>::advance(off_t offset)
1415{
1416 int ret = 0;
1417 while (this->pos_ < this->len_)
1418 {
1419 // Rel and Rela start out the same, so we can always use Rel to
1420 // find the r_offset value.
1421 elfcpp::Rel<size, big_endian> rel(this->prelocs_ + this->pos_);
1422 if (static_cast<off_t>(rel.get_r_offset()) >= offset)
1423 break;
1424 ++ret;
1425 this->pos_ += this->reloc_size_;
1426 }
1427 return ret;
1428}
1429
12c0daef 1430// Instantiate the templates we need.
61ba1cf9 1431
193a53d9 1432#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1433template
1434void
f6ce93d6 1435Sized_relobj<32, false>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1436#endif
92e059d8 1437
193a53d9 1438#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1439template
1440void
f6ce93d6 1441Sized_relobj<32, true>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1442#endif
92e059d8 1443
193a53d9 1444#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1445template
1446void
f6ce93d6 1447Sized_relobj<64, false>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1448#endif
92e059d8 1449
193a53d9 1450#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1451template
1452void
f6ce93d6 1453Sized_relobj<64, true>::do_read_relocs(Read_relocs_data* rd);
193a53d9 1454#endif
92e059d8 1455
6d03d481
ST
1456#ifdef HAVE_TARGET_32_LITTLE
1457template
1458void
ad0f2072 1459Sized_relobj<32, false>::do_gc_process_relocs(Symbol_table* symtab,
2ea97941 1460 Layout* layout,
ad0f2072 1461 Read_relocs_data* rd);
6d03d481
ST
1462#endif
1463
1464#ifdef HAVE_TARGET_32_BIG
1465template
1466void
ad0f2072 1467Sized_relobj<32, true>::do_gc_process_relocs(Symbol_table* symtab,
2ea97941 1468 Layout* layout,
ad0f2072 1469 Read_relocs_data* rd);
6d03d481
ST
1470#endif
1471
1472#ifdef HAVE_TARGET_64_LITTLE
1473template
1474void
ad0f2072 1475Sized_relobj<64, false>::do_gc_process_relocs(Symbol_table* symtab,
2ea97941 1476 Layout* layout,
ad0f2072 1477 Read_relocs_data* rd);
6d03d481
ST
1478#endif
1479
1480#ifdef HAVE_TARGET_64_BIG
1481template
1482void
ad0f2072 1483Sized_relobj<64, true>::do_gc_process_relocs(Symbol_table* symtab,
2ea97941 1484 Layout* layout,
ad0f2072 1485 Read_relocs_data* rd);
6d03d481
ST
1486#endif
1487
193a53d9 1488#ifdef HAVE_TARGET_32_LITTLE
92e059d8
ILT
1489template
1490void
ad0f2072 1491Sized_relobj<32, false>::do_scan_relocs(Symbol_table* symtab,
2ea97941 1492 Layout* layout,
92e059d8 1493 Read_relocs_data* rd);
193a53d9 1494#endif
92e059d8 1495
193a53d9 1496#ifdef HAVE_TARGET_32_BIG
92e059d8
ILT
1497template
1498void
ad0f2072 1499Sized_relobj<32, true>::do_scan_relocs(Symbol_table* symtab,
2ea97941 1500 Layout* layout,
92e059d8 1501 Read_relocs_data* rd);
193a53d9 1502#endif
92e059d8 1503
193a53d9 1504#ifdef HAVE_TARGET_64_LITTLE
92e059d8
ILT
1505template
1506void
ad0f2072 1507Sized_relobj<64, false>::do_scan_relocs(Symbol_table* symtab,
2ea97941 1508 Layout* layout,
92e059d8 1509 Read_relocs_data* rd);
193a53d9 1510#endif
92e059d8 1511
193a53d9 1512#ifdef HAVE_TARGET_64_BIG
92e059d8
ILT
1513template
1514void
ad0f2072 1515Sized_relobj<64, true>::do_scan_relocs(Symbol_table* symtab,
2ea97941 1516 Layout* layout,
92e059d8 1517 Read_relocs_data* rd);
193a53d9 1518#endif
92e059d8 1519
193a53d9 1520#ifdef HAVE_TARGET_32_LITTLE
61ba1cf9
ILT
1521template
1522void
ad0f2072 1523Sized_relobj<32, false>::do_relocate(const Symbol_table* symtab,
2ea97941 1524 const Layout* layout,
61ba1cf9 1525 Output_file* of);
193a53d9 1526#endif
61ba1cf9 1527
193a53d9 1528#ifdef HAVE_TARGET_32_BIG
61ba1cf9
ILT
1529template
1530void
ad0f2072 1531Sized_relobj<32, true>::do_relocate(const Symbol_table* symtab,
2ea97941 1532 const Layout* layout,
61ba1cf9 1533 Output_file* of);
193a53d9 1534#endif
61ba1cf9 1535
193a53d9 1536#ifdef HAVE_TARGET_64_LITTLE
61ba1cf9
ILT
1537template
1538void
ad0f2072 1539Sized_relobj<64, false>::do_relocate(const Symbol_table* symtab,
2ea97941 1540 const Layout* layout,
61ba1cf9 1541 Output_file* of);
193a53d9 1542#endif
61ba1cf9 1543
193a53d9 1544#ifdef HAVE_TARGET_64_BIG
61ba1cf9
ILT
1545template
1546void
ad0f2072 1547Sized_relobj<64, true>::do_relocate(const Symbol_table* symtab,
2ea97941 1548 const Layout* layout,
61ba1cf9 1549 Output_file* of);
193a53d9 1550#endif
61ba1cf9 1551
72adc4fa
DK
1552#ifdef HAVE_TARGET_32_LITTLE
1553template
1554void
1555Sized_relobj<32, false>::do_relocate_sections(
72adc4fa 1556 const Symbol_table* symtab,
2ea97941 1557 const Layout* layout,
72adc4fa
DK
1558 const unsigned char* pshdrs,
1559 Views* pviews);
1560#endif
1561
1562#ifdef HAVE_TARGET_32_BIG
1563template
1564void
1565Sized_relobj<32, true>::do_relocate_sections(
72adc4fa 1566 const Symbol_table* symtab,
2ea97941 1567 const Layout* layout,
72adc4fa
DK
1568 const unsigned char* pshdrs,
1569 Views* pviews);
1570#endif
1571
1572#ifdef HAVE_TARGET_64_LITTLE
1573template
1574void
1575Sized_relobj<64, false>::do_relocate_sections(
72adc4fa 1576 const Symbol_table* symtab,
2ea97941 1577 const Layout* layout,
72adc4fa
DK
1578 const unsigned char* pshdrs,
1579 Views* pviews);
1580#endif
1581
1582#ifdef HAVE_TARGET_64_BIG
1583template
1584void
1585Sized_relobj<64, true>::do_relocate_sections(
72adc4fa
DK
1586 const Symbol_table* symtab,
1587 const Layout* layout,
1588 const unsigned char* pshdrs,
1589 Views* pviews);
1590#endif
1591
3e4afc80
ILT
1592#ifdef HAVE_TARGET_32_LITTLE
1593template
1594void
1595Sized_relobj<32, false>::initialize_input_to_output_maps();
1596
1597template
1598void
1599Sized_relobj<32, false>::free_input_to_output_maps();
1600#endif
1601
1602#ifdef HAVE_TARGET_32_BIG
1603template
1604void
1605Sized_relobj<32, true>::initialize_input_to_output_maps();
1606
1607template
1608void
1609Sized_relobj<32, true>::free_input_to_output_maps();
1610#endif
1611
1612#ifdef HAVE_TARGET_64_LITTLE
1613template
1614void
1615Sized_relobj<64, false>::initialize_input_to_output_maps();
1616
1617template
1618void
1619Sized_relobj<64, false>::free_input_to_output_maps();
1620#endif
1621
1622#ifdef HAVE_TARGET_64_BIG
1623template
1624void
1625Sized_relobj<64, true>::initialize_input_to_output_maps();
1626
1627template
1628void
1629Sized_relobj<64, true>::free_input_to_output_maps();
1630#endif
1631
a9a60db6
ILT
1632#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1633template
1634class Merged_symbol_value<32>;
1635#endif
1636
1637#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1638template
1639class Merged_symbol_value<64>;
1640#endif
1641
1642#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1643template
1644class Symbol_value<32>;
1645#endif
1646
1647#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1648template
1649class Symbol_value<64>;
1650#endif
1651
730cdc88
ILT
1652#ifdef HAVE_TARGET_32_LITTLE
1653template
1654class Track_relocs<32, false>;
1655#endif
1656
1657#ifdef HAVE_TARGET_32_BIG
1658template
1659class Track_relocs<32, true>;
1660#endif
1661
1662#ifdef HAVE_TARGET_64_LITTLE
1663template
1664class Track_relocs<64, false>;
1665#endif
1666
1667#ifdef HAVE_TARGET_64_BIG
1668template
1669class Track_relocs<64, true>;
1670#endif
1671
61ba1cf9 1672} // End namespace gold.
This page took 0.246214 seconds and 4 git commands to generate.