2010-01-14 Doug Kwan <dougkwan@google.com>
[deliverable/binutils-gdb.git] / gold / output.cc
1 // output.cc -- manage the output file for gold
2
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstdlib>
26 #include <cstring>
27 #include <cerrno>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <algorithm>
33 #include "libiberty.h"
34
35 #include "parameters.h"
36 #include "object.h"
37 #include "symtab.h"
38 #include "reloc.h"
39 #include "merge.h"
40 #include "descriptors.h"
41 #include "output.h"
42
43 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
44 #ifndef MAP_ANONYMOUS
45 # define MAP_ANONYMOUS MAP_ANON
46 #endif
47
48 #ifndef HAVE_POSIX_FALLOCATE
49 // A dummy, non general, version of posix_fallocate. Here we just set
50 // the file size and hope that there is enough disk space. FIXME: We
51 // could allocate disk space by walking block by block and writing a
52 // zero byte into each block.
53 static int
54 posix_fallocate(int o, off_t offset, off_t len)
55 {
56 return ftruncate(o, offset + len);
57 }
58 #endif // !defined(HAVE_POSIX_FALLOCATE)
59
60 namespace gold
61 {
62
63 // Output_data variables.
64
65 bool Output_data::allocated_sizes_are_fixed;
66
67 // Output_data methods.
68
69 Output_data::~Output_data()
70 {
71 }
72
73 // Return the default alignment for the target size.
74
75 uint64_t
76 Output_data::default_alignment()
77 {
78 return Output_data::default_alignment_for_size(
79 parameters->target().get_size());
80 }
81
82 // Return the default alignment for a size--32 or 64.
83
84 uint64_t
85 Output_data::default_alignment_for_size(int size)
86 {
87 if (size == 32)
88 return 4;
89 else if (size == 64)
90 return 8;
91 else
92 gold_unreachable();
93 }
94
95 // Output_section_header methods. This currently assumes that the
96 // segment and section lists are complete at construction time.
97
98 Output_section_headers::Output_section_headers(
99 const Layout* layout,
100 const Layout::Segment_list* segment_list,
101 const Layout::Section_list* section_list,
102 const Layout::Section_list* unattached_section_list,
103 const Stringpool* secnamepool,
104 const Output_section* shstrtab_section)
105 : layout_(layout),
106 segment_list_(segment_list),
107 section_list_(section_list),
108 unattached_section_list_(unattached_section_list),
109 secnamepool_(secnamepool),
110 shstrtab_section_(shstrtab_section)
111 {
112 }
113
114 // Compute the current data size.
115
116 off_t
117 Output_section_headers::do_size() const
118 {
119 // Count all the sections. Start with 1 for the null section.
120 off_t count = 1;
121 if (!parameters->options().relocatable())
122 {
123 for (Layout::Segment_list::const_iterator p =
124 this->segment_list_->begin();
125 p != this->segment_list_->end();
126 ++p)
127 if ((*p)->type() == elfcpp::PT_LOAD)
128 count += (*p)->output_section_count();
129 }
130 else
131 {
132 for (Layout::Section_list::const_iterator p =
133 this->section_list_->begin();
134 p != this->section_list_->end();
135 ++p)
136 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
137 ++count;
138 }
139 count += this->unattached_section_list_->size();
140
141 const int size = parameters->target().get_size();
142 int shdr_size;
143 if (size == 32)
144 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
145 else if (size == 64)
146 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
147 else
148 gold_unreachable();
149
150 return count * shdr_size;
151 }
152
153 // Write out the section headers.
154
155 void
156 Output_section_headers::do_write(Output_file* of)
157 {
158 switch (parameters->size_and_endianness())
159 {
160 #ifdef HAVE_TARGET_32_LITTLE
161 case Parameters::TARGET_32_LITTLE:
162 this->do_sized_write<32, false>(of);
163 break;
164 #endif
165 #ifdef HAVE_TARGET_32_BIG
166 case Parameters::TARGET_32_BIG:
167 this->do_sized_write<32, true>(of);
168 break;
169 #endif
170 #ifdef HAVE_TARGET_64_LITTLE
171 case Parameters::TARGET_64_LITTLE:
172 this->do_sized_write<64, false>(of);
173 break;
174 #endif
175 #ifdef HAVE_TARGET_64_BIG
176 case Parameters::TARGET_64_BIG:
177 this->do_sized_write<64, true>(of);
178 break;
179 #endif
180 default:
181 gold_unreachable();
182 }
183 }
184
185 template<int size, bool big_endian>
186 void
187 Output_section_headers::do_sized_write(Output_file* of)
188 {
189 off_t all_shdrs_size = this->data_size();
190 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
191
192 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
193 unsigned char* v = view;
194
195 {
196 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
197 oshdr.put_sh_name(0);
198 oshdr.put_sh_type(elfcpp::SHT_NULL);
199 oshdr.put_sh_flags(0);
200 oshdr.put_sh_addr(0);
201 oshdr.put_sh_offset(0);
202
203 size_t section_count = (this->data_size()
204 / elfcpp::Elf_sizes<size>::shdr_size);
205 if (section_count < elfcpp::SHN_LORESERVE)
206 oshdr.put_sh_size(0);
207 else
208 oshdr.put_sh_size(section_count);
209
210 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
211 if (shstrndx < elfcpp::SHN_LORESERVE)
212 oshdr.put_sh_link(0);
213 else
214 oshdr.put_sh_link(shstrndx);
215
216 oshdr.put_sh_info(0);
217 oshdr.put_sh_addralign(0);
218 oshdr.put_sh_entsize(0);
219 }
220
221 v += shdr_size;
222
223 unsigned int shndx = 1;
224 if (!parameters->options().relocatable())
225 {
226 for (Layout::Segment_list::const_iterator p =
227 this->segment_list_->begin();
228 p != this->segment_list_->end();
229 ++p)
230 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
231 this->secnamepool_,
232 v,
233 &shndx);
234 }
235 else
236 {
237 for (Layout::Section_list::const_iterator p =
238 this->section_list_->begin();
239 p != this->section_list_->end();
240 ++p)
241 {
242 // We do unallocated sections below, except that group
243 // sections have to come first.
244 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
245 && (*p)->type() != elfcpp::SHT_GROUP)
246 continue;
247 gold_assert(shndx == (*p)->out_shndx());
248 elfcpp::Shdr_write<size, big_endian> oshdr(v);
249 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
250 v += shdr_size;
251 ++shndx;
252 }
253 }
254
255 for (Layout::Section_list::const_iterator p =
256 this->unattached_section_list_->begin();
257 p != this->unattached_section_list_->end();
258 ++p)
259 {
260 // For a relocatable link, we did unallocated group sections
261 // above, since they have to come first.
262 if ((*p)->type() == elfcpp::SHT_GROUP
263 && parameters->options().relocatable())
264 continue;
265 gold_assert(shndx == (*p)->out_shndx());
266 elfcpp::Shdr_write<size, big_endian> oshdr(v);
267 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
268 v += shdr_size;
269 ++shndx;
270 }
271
272 of->write_output_view(this->offset(), all_shdrs_size, view);
273 }
274
275 // Output_segment_header methods.
276
277 Output_segment_headers::Output_segment_headers(
278 const Layout::Segment_list& segment_list)
279 : segment_list_(segment_list)
280 {
281 }
282
283 void
284 Output_segment_headers::do_write(Output_file* of)
285 {
286 switch (parameters->size_and_endianness())
287 {
288 #ifdef HAVE_TARGET_32_LITTLE
289 case Parameters::TARGET_32_LITTLE:
290 this->do_sized_write<32, false>(of);
291 break;
292 #endif
293 #ifdef HAVE_TARGET_32_BIG
294 case Parameters::TARGET_32_BIG:
295 this->do_sized_write<32, true>(of);
296 break;
297 #endif
298 #ifdef HAVE_TARGET_64_LITTLE
299 case Parameters::TARGET_64_LITTLE:
300 this->do_sized_write<64, false>(of);
301 break;
302 #endif
303 #ifdef HAVE_TARGET_64_BIG
304 case Parameters::TARGET_64_BIG:
305 this->do_sized_write<64, true>(of);
306 break;
307 #endif
308 default:
309 gold_unreachable();
310 }
311 }
312
313 template<int size, bool big_endian>
314 void
315 Output_segment_headers::do_sized_write(Output_file* of)
316 {
317 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
318 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
319 gold_assert(all_phdrs_size == this->data_size());
320 unsigned char* view = of->get_output_view(this->offset(),
321 all_phdrs_size);
322 unsigned char* v = view;
323 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
324 p != this->segment_list_.end();
325 ++p)
326 {
327 elfcpp::Phdr_write<size, big_endian> ophdr(v);
328 (*p)->write_header(&ophdr);
329 v += phdr_size;
330 }
331
332 gold_assert(v - view == all_phdrs_size);
333
334 of->write_output_view(this->offset(), all_phdrs_size, view);
335 }
336
337 off_t
338 Output_segment_headers::do_size() const
339 {
340 const int size = parameters->target().get_size();
341 int phdr_size;
342 if (size == 32)
343 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
344 else if (size == 64)
345 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
346 else
347 gold_unreachable();
348
349 return this->segment_list_.size() * phdr_size;
350 }
351
352 // Output_file_header methods.
353
354 Output_file_header::Output_file_header(const Target* target,
355 const Symbol_table* symtab,
356 const Output_segment_headers* osh,
357 const char* entry)
358 : target_(target),
359 symtab_(symtab),
360 segment_header_(osh),
361 section_header_(NULL),
362 shstrtab_(NULL),
363 entry_(entry)
364 {
365 this->set_data_size(this->do_size());
366 }
367
368 // Set the section table information for a file header.
369
370 void
371 Output_file_header::set_section_info(const Output_section_headers* shdrs,
372 const Output_section* shstrtab)
373 {
374 this->section_header_ = shdrs;
375 this->shstrtab_ = shstrtab;
376 }
377
378 // Write out the file header.
379
380 void
381 Output_file_header::do_write(Output_file* of)
382 {
383 gold_assert(this->offset() == 0);
384
385 switch (parameters->size_and_endianness())
386 {
387 #ifdef HAVE_TARGET_32_LITTLE
388 case Parameters::TARGET_32_LITTLE:
389 this->do_sized_write<32, false>(of);
390 break;
391 #endif
392 #ifdef HAVE_TARGET_32_BIG
393 case Parameters::TARGET_32_BIG:
394 this->do_sized_write<32, true>(of);
395 break;
396 #endif
397 #ifdef HAVE_TARGET_64_LITTLE
398 case Parameters::TARGET_64_LITTLE:
399 this->do_sized_write<64, false>(of);
400 break;
401 #endif
402 #ifdef HAVE_TARGET_64_BIG
403 case Parameters::TARGET_64_BIG:
404 this->do_sized_write<64, true>(of);
405 break;
406 #endif
407 default:
408 gold_unreachable();
409 }
410 }
411
412 // Write out the file header with appropriate size and endianess.
413
414 template<int size, bool big_endian>
415 void
416 Output_file_header::do_sized_write(Output_file* of)
417 {
418 gold_assert(this->offset() == 0);
419
420 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
421 unsigned char* view = of->get_output_view(0, ehdr_size);
422 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
423
424 unsigned char e_ident[elfcpp::EI_NIDENT];
425 memset(e_ident, 0, elfcpp::EI_NIDENT);
426 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
427 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
428 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
429 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
430 if (size == 32)
431 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
432 else if (size == 64)
433 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
434 else
435 gold_unreachable();
436 e_ident[elfcpp::EI_DATA] = (big_endian
437 ? elfcpp::ELFDATA2MSB
438 : elfcpp::ELFDATA2LSB);
439 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
440 oehdr.put_e_ident(e_ident);
441
442 elfcpp::ET e_type;
443 if (parameters->options().relocatable())
444 e_type = elfcpp::ET_REL;
445 else if (parameters->options().output_is_position_independent())
446 e_type = elfcpp::ET_DYN;
447 else
448 e_type = elfcpp::ET_EXEC;
449 oehdr.put_e_type(e_type);
450
451 oehdr.put_e_machine(this->target_->machine_code());
452 oehdr.put_e_version(elfcpp::EV_CURRENT);
453
454 oehdr.put_e_entry(this->entry<size>());
455
456 if (this->segment_header_ == NULL)
457 oehdr.put_e_phoff(0);
458 else
459 oehdr.put_e_phoff(this->segment_header_->offset());
460
461 oehdr.put_e_shoff(this->section_header_->offset());
462 oehdr.put_e_flags(this->target_->processor_specific_flags());
463 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
464
465 if (this->segment_header_ == NULL)
466 {
467 oehdr.put_e_phentsize(0);
468 oehdr.put_e_phnum(0);
469 }
470 else
471 {
472 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
473 oehdr.put_e_phnum(this->segment_header_->data_size()
474 / elfcpp::Elf_sizes<size>::phdr_size);
475 }
476
477 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
478 size_t section_count = (this->section_header_->data_size()
479 / elfcpp::Elf_sizes<size>::shdr_size);
480
481 if (section_count < elfcpp::SHN_LORESERVE)
482 oehdr.put_e_shnum(this->section_header_->data_size()
483 / elfcpp::Elf_sizes<size>::shdr_size);
484 else
485 oehdr.put_e_shnum(0);
486
487 unsigned int shstrndx = this->shstrtab_->out_shndx();
488 if (shstrndx < elfcpp::SHN_LORESERVE)
489 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
490 else
491 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
492
493 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
494 // the e_ident field.
495 parameters->target().adjust_elf_header(view, ehdr_size);
496
497 of->write_output_view(0, ehdr_size, view);
498 }
499
500 // Return the value to use for the entry address. THIS->ENTRY_ is the
501 // symbol specified on the command line, if any.
502
503 template<int size>
504 typename elfcpp::Elf_types<size>::Elf_Addr
505 Output_file_header::entry()
506 {
507 const bool should_issue_warning = (this->entry_ != NULL
508 && !parameters->options().relocatable()
509 && !parameters->options().shared());
510
511 // FIXME: Need to support target specific entry symbol.
512 const char* entry = this->entry_;
513 if (entry == NULL)
514 entry = "_start";
515
516 Symbol* sym = this->symtab_->lookup(entry);
517
518 typename Sized_symbol<size>::Value_type v;
519 if (sym != NULL)
520 {
521 Sized_symbol<size>* ssym;
522 ssym = this->symtab_->get_sized_symbol<size>(sym);
523 if (!ssym->is_defined() && should_issue_warning)
524 gold_warning("entry symbol '%s' exists but is not defined", entry);
525 v = ssym->value();
526 }
527 else
528 {
529 // We couldn't find the entry symbol. See if we can parse it as
530 // a number. This supports, e.g., -e 0x1000.
531 char* endptr;
532 v = strtoull(entry, &endptr, 0);
533 if (*endptr != '\0')
534 {
535 if (should_issue_warning)
536 gold_warning("cannot find entry symbol '%s'", entry);
537 v = 0;
538 }
539 }
540
541 return v;
542 }
543
544 // Compute the current data size.
545
546 off_t
547 Output_file_header::do_size() const
548 {
549 const int size = parameters->target().get_size();
550 if (size == 32)
551 return elfcpp::Elf_sizes<32>::ehdr_size;
552 else if (size == 64)
553 return elfcpp::Elf_sizes<64>::ehdr_size;
554 else
555 gold_unreachable();
556 }
557
558 // Output_data_const methods.
559
560 void
561 Output_data_const::do_write(Output_file* of)
562 {
563 of->write(this->offset(), this->data_.data(), this->data_.size());
564 }
565
566 // Output_data_const_buffer methods.
567
568 void
569 Output_data_const_buffer::do_write(Output_file* of)
570 {
571 of->write(this->offset(), this->p_, this->data_size());
572 }
573
574 // Output_section_data methods.
575
576 // Record the output section, and set the entry size and such.
577
578 void
579 Output_section_data::set_output_section(Output_section* os)
580 {
581 gold_assert(this->output_section_ == NULL);
582 this->output_section_ = os;
583 this->do_adjust_output_section(os);
584 }
585
586 // Return the section index of the output section.
587
588 unsigned int
589 Output_section_data::do_out_shndx() const
590 {
591 gold_assert(this->output_section_ != NULL);
592 return this->output_section_->out_shndx();
593 }
594
595 // Set the alignment, which means we may need to update the alignment
596 // of the output section.
597
598 void
599 Output_section_data::set_addralign(uint64_t addralign)
600 {
601 this->addralign_ = addralign;
602 if (this->output_section_ != NULL
603 && this->output_section_->addralign() < addralign)
604 this->output_section_->set_addralign(addralign);
605 }
606
607 // Output_data_strtab methods.
608
609 // Set the final data size.
610
611 void
612 Output_data_strtab::set_final_data_size()
613 {
614 this->strtab_->set_string_offsets();
615 this->set_data_size(this->strtab_->get_strtab_size());
616 }
617
618 // Write out a string table.
619
620 void
621 Output_data_strtab::do_write(Output_file* of)
622 {
623 this->strtab_->write(of, this->offset());
624 }
625
626 // Output_reloc methods.
627
628 // A reloc against a global symbol.
629
630 template<bool dynamic, int size, bool big_endian>
631 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
632 Symbol* gsym,
633 unsigned int type,
634 Output_data* od,
635 Address address,
636 bool is_relative)
637 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
638 is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
639 {
640 // this->type_ is a bitfield; make sure TYPE fits.
641 gold_assert(this->type_ == type);
642 this->u1_.gsym = gsym;
643 this->u2_.od = od;
644 if (dynamic)
645 this->set_needs_dynsym_index();
646 }
647
648 template<bool dynamic, int size, bool big_endian>
649 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
650 Symbol* gsym,
651 unsigned int type,
652 Sized_relobj<size, big_endian>* relobj,
653 unsigned int shndx,
654 Address address,
655 bool is_relative)
656 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
657 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
658 {
659 gold_assert(shndx != INVALID_CODE);
660 // this->type_ is a bitfield; make sure TYPE fits.
661 gold_assert(this->type_ == type);
662 this->u1_.gsym = gsym;
663 this->u2_.relobj = relobj;
664 if (dynamic)
665 this->set_needs_dynsym_index();
666 }
667
668 // A reloc against a local symbol.
669
670 template<bool dynamic, int size, bool big_endian>
671 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
672 Sized_relobj<size, big_endian>* relobj,
673 unsigned int local_sym_index,
674 unsigned int type,
675 Output_data* od,
676 Address address,
677 bool is_relative,
678 bool is_section_symbol)
679 : address_(address), local_sym_index_(local_sym_index), type_(type),
680 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
681 shndx_(INVALID_CODE)
682 {
683 gold_assert(local_sym_index != GSYM_CODE
684 && local_sym_index != INVALID_CODE);
685 // this->type_ is a bitfield; make sure TYPE fits.
686 gold_assert(this->type_ == type);
687 this->u1_.relobj = relobj;
688 this->u2_.od = od;
689 if (dynamic)
690 this->set_needs_dynsym_index();
691 }
692
693 template<bool dynamic, int size, bool big_endian>
694 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
695 Sized_relobj<size, big_endian>* relobj,
696 unsigned int local_sym_index,
697 unsigned int type,
698 unsigned int shndx,
699 Address address,
700 bool is_relative,
701 bool is_section_symbol)
702 : address_(address), local_sym_index_(local_sym_index), type_(type),
703 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
704 shndx_(shndx)
705 {
706 gold_assert(local_sym_index != GSYM_CODE
707 && local_sym_index != INVALID_CODE);
708 gold_assert(shndx != INVALID_CODE);
709 // this->type_ is a bitfield; make sure TYPE fits.
710 gold_assert(this->type_ == type);
711 this->u1_.relobj = relobj;
712 this->u2_.relobj = relobj;
713 if (dynamic)
714 this->set_needs_dynsym_index();
715 }
716
717 // A reloc against the STT_SECTION symbol of an output section.
718
719 template<bool dynamic, int size, bool big_endian>
720 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
721 Output_section* os,
722 unsigned int type,
723 Output_data* od,
724 Address address)
725 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
726 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
727 {
728 // this->type_ is a bitfield; make sure TYPE fits.
729 gold_assert(this->type_ == type);
730 this->u1_.os = os;
731 this->u2_.od = od;
732 if (dynamic)
733 this->set_needs_dynsym_index();
734 else
735 os->set_needs_symtab_index();
736 }
737
738 template<bool dynamic, int size, bool big_endian>
739 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
740 Output_section* os,
741 unsigned int type,
742 Sized_relobj<size, big_endian>* relobj,
743 unsigned int shndx,
744 Address address)
745 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
746 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
747 {
748 gold_assert(shndx != INVALID_CODE);
749 // this->type_ is a bitfield; make sure TYPE fits.
750 gold_assert(this->type_ == type);
751 this->u1_.os = os;
752 this->u2_.relobj = relobj;
753 if (dynamic)
754 this->set_needs_dynsym_index();
755 else
756 os->set_needs_symtab_index();
757 }
758
759 // An absolute relocation.
760
761 template<bool dynamic, int size, bool big_endian>
762 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
763 unsigned int type,
764 Output_data* od,
765 Address address)
766 : address_(address), local_sym_index_(0), type_(type),
767 is_relative_(false), is_section_symbol_(false), shndx_(INVALID_CODE)
768 {
769 // this->type_ is a bitfield; make sure TYPE fits.
770 gold_assert(this->type_ == type);
771 this->u1_.relobj = NULL;
772 this->u2_.od = od;
773 }
774
775 template<bool dynamic, int size, bool big_endian>
776 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
777 unsigned int type,
778 Sized_relobj<size, big_endian>* relobj,
779 unsigned int shndx,
780 Address address)
781 : address_(address), local_sym_index_(0), type_(type),
782 is_relative_(false), is_section_symbol_(false), shndx_(shndx)
783 {
784 gold_assert(shndx != INVALID_CODE);
785 // this->type_ is a bitfield; make sure TYPE fits.
786 gold_assert(this->type_ == type);
787 this->u1_.relobj = NULL;
788 this->u2_.relobj = relobj;
789 }
790
791 // A target specific relocation.
792
793 template<bool dynamic, int size, bool big_endian>
794 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
795 unsigned int type,
796 void* arg,
797 Output_data* od,
798 Address address)
799 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
800 is_relative_(false), is_section_symbol_(false), shndx_(INVALID_CODE)
801 {
802 // this->type_ is a bitfield; make sure TYPE fits.
803 gold_assert(this->type_ == type);
804 this->u1_.arg = arg;
805 this->u2_.od = od;
806 }
807
808 template<bool dynamic, int size, bool big_endian>
809 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
810 unsigned int type,
811 void* arg,
812 Sized_relobj<size, big_endian>* relobj,
813 unsigned int shndx,
814 Address address)
815 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
816 is_relative_(false), is_section_symbol_(false), shndx_(shndx)
817 {
818 gold_assert(shndx != INVALID_CODE);
819 // this->type_ is a bitfield; make sure TYPE fits.
820 gold_assert(this->type_ == type);
821 this->u1_.arg = arg;
822 this->u2_.relobj = relobj;
823 }
824
825 // Record that we need a dynamic symbol index for this relocation.
826
827 template<bool dynamic, int size, bool big_endian>
828 void
829 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
830 set_needs_dynsym_index()
831 {
832 if (this->is_relative_)
833 return;
834 switch (this->local_sym_index_)
835 {
836 case INVALID_CODE:
837 gold_unreachable();
838
839 case GSYM_CODE:
840 this->u1_.gsym->set_needs_dynsym_entry();
841 break;
842
843 case SECTION_CODE:
844 this->u1_.os->set_needs_dynsym_index();
845 break;
846
847 case TARGET_CODE:
848 // The target must take care of this if necessary.
849 break;
850
851 case 0:
852 break;
853
854 default:
855 {
856 const unsigned int lsi = this->local_sym_index_;
857 if (!this->is_section_symbol_)
858 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
859 else
860 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
861 }
862 break;
863 }
864 }
865
866 // Get the symbol index of a relocation.
867
868 template<bool dynamic, int size, bool big_endian>
869 unsigned int
870 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
871 const
872 {
873 unsigned int index;
874 switch (this->local_sym_index_)
875 {
876 case INVALID_CODE:
877 gold_unreachable();
878
879 case GSYM_CODE:
880 if (this->u1_.gsym == NULL)
881 index = 0;
882 else if (dynamic)
883 index = this->u1_.gsym->dynsym_index();
884 else
885 index = this->u1_.gsym->symtab_index();
886 break;
887
888 case SECTION_CODE:
889 if (dynamic)
890 index = this->u1_.os->dynsym_index();
891 else
892 index = this->u1_.os->symtab_index();
893 break;
894
895 case TARGET_CODE:
896 index = parameters->target().reloc_symbol_index(this->u1_.arg,
897 this->type_);
898 break;
899
900 case 0:
901 // Relocations without symbols use a symbol index of 0.
902 index = 0;
903 break;
904
905 default:
906 {
907 const unsigned int lsi = this->local_sym_index_;
908 if (!this->is_section_symbol_)
909 {
910 if (dynamic)
911 index = this->u1_.relobj->dynsym_index(lsi);
912 else
913 index = this->u1_.relobj->symtab_index(lsi);
914 }
915 else
916 {
917 Output_section* os = this->u1_.relobj->output_section(lsi);
918 gold_assert(os != NULL);
919 if (dynamic)
920 index = os->dynsym_index();
921 else
922 index = os->symtab_index();
923 }
924 }
925 break;
926 }
927 gold_assert(index != -1U);
928 return index;
929 }
930
931 // For a local section symbol, get the address of the offset ADDEND
932 // within the input section.
933
934 template<bool dynamic, int size, bool big_endian>
935 typename elfcpp::Elf_types<size>::Elf_Addr
936 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
937 local_section_offset(Addend addend) const
938 {
939 gold_assert(this->local_sym_index_ != GSYM_CODE
940 && this->local_sym_index_ != SECTION_CODE
941 && this->local_sym_index_ != TARGET_CODE
942 && this->local_sym_index_ != INVALID_CODE
943 && this->local_sym_index_ != 0
944 && this->is_section_symbol_);
945 const unsigned int lsi = this->local_sym_index_;
946 Output_section* os = this->u1_.relobj->output_section(lsi);
947 gold_assert(os != NULL);
948 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
949 if (offset != invalid_address)
950 return offset + addend;
951 // This is a merge section.
952 offset = os->output_address(this->u1_.relobj, lsi, addend);
953 gold_assert(offset != invalid_address);
954 return offset;
955 }
956
957 // Get the output address of a relocation.
958
959 template<bool dynamic, int size, bool big_endian>
960 typename elfcpp::Elf_types<size>::Elf_Addr
961 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
962 {
963 Address address = this->address_;
964 if (this->shndx_ != INVALID_CODE)
965 {
966 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
967 gold_assert(os != NULL);
968 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
969 if (off != invalid_address)
970 address += os->address() + off;
971 else
972 {
973 address = os->output_address(this->u2_.relobj, this->shndx_,
974 address);
975 gold_assert(address != invalid_address);
976 }
977 }
978 else if (this->u2_.od != NULL)
979 address += this->u2_.od->address();
980 return address;
981 }
982
983 // Write out the offset and info fields of a Rel or Rela relocation
984 // entry.
985
986 template<bool dynamic, int size, bool big_endian>
987 template<typename Write_rel>
988 void
989 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
990 Write_rel* wr) const
991 {
992 wr->put_r_offset(this->get_address());
993 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
994 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
995 }
996
997 // Write out a Rel relocation.
998
999 template<bool dynamic, int size, bool big_endian>
1000 void
1001 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
1002 unsigned char* pov) const
1003 {
1004 elfcpp::Rel_write<size, big_endian> orel(pov);
1005 this->write_rel(&orel);
1006 }
1007
1008 // Get the value of the symbol referred to by a Rel relocation.
1009
1010 template<bool dynamic, int size, bool big_endian>
1011 typename elfcpp::Elf_types<size>::Elf_Addr
1012 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
1013 Addend addend) const
1014 {
1015 if (this->local_sym_index_ == GSYM_CODE)
1016 {
1017 const Sized_symbol<size>* sym;
1018 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
1019 return sym->value() + addend;
1020 }
1021 gold_assert(this->local_sym_index_ != SECTION_CODE
1022 && this->local_sym_index_ != TARGET_CODE
1023 && this->local_sym_index_ != INVALID_CODE
1024 && this->local_sym_index_ != 0
1025 && !this->is_section_symbol_);
1026 const unsigned int lsi = this->local_sym_index_;
1027 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
1028 return symval->value(this->u1_.relobj, addend);
1029 }
1030
1031 // Reloc comparison. This function sorts the dynamic relocs for the
1032 // benefit of the dynamic linker. First we sort all relative relocs
1033 // to the front. Among relative relocs, we sort by output address.
1034 // Among non-relative relocs, we sort by symbol index, then by output
1035 // address.
1036
1037 template<bool dynamic, int size, bool big_endian>
1038 int
1039 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1040 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1041 const
1042 {
1043 if (this->is_relative_)
1044 {
1045 if (!r2.is_relative_)
1046 return -1;
1047 // Otherwise sort by reloc address below.
1048 }
1049 else if (r2.is_relative_)
1050 return 1;
1051 else
1052 {
1053 unsigned int sym1 = this->get_symbol_index();
1054 unsigned int sym2 = r2.get_symbol_index();
1055 if (sym1 < sym2)
1056 return -1;
1057 else if (sym1 > sym2)
1058 return 1;
1059 // Otherwise sort by reloc address.
1060 }
1061
1062 section_offset_type addr1 = this->get_address();
1063 section_offset_type addr2 = r2.get_address();
1064 if (addr1 < addr2)
1065 return -1;
1066 else if (addr1 > addr2)
1067 return 1;
1068
1069 // Final tie breaker, in order to generate the same output on any
1070 // host: reloc type.
1071 unsigned int type1 = this->type_;
1072 unsigned int type2 = r2.type_;
1073 if (type1 < type2)
1074 return -1;
1075 else if (type1 > type2)
1076 return 1;
1077
1078 // These relocs appear to be exactly the same.
1079 return 0;
1080 }
1081
1082 // Write out a Rela relocation.
1083
1084 template<bool dynamic, int size, bool big_endian>
1085 void
1086 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1087 unsigned char* pov) const
1088 {
1089 elfcpp::Rela_write<size, big_endian> orel(pov);
1090 this->rel_.write_rel(&orel);
1091 Addend addend = this->addend_;
1092 if (this->rel_.is_target_specific())
1093 addend = parameters->target().reloc_addend(this->rel_.target_arg(),
1094 this->rel_.type(), addend);
1095 else if (this->rel_.is_relative())
1096 addend = this->rel_.symbol_value(addend);
1097 else if (this->rel_.is_local_section_symbol())
1098 addend = this->rel_.local_section_offset(addend);
1099 orel.put_r_addend(addend);
1100 }
1101
1102 // Output_data_reloc_base methods.
1103
1104 // Adjust the output section.
1105
1106 template<int sh_type, bool dynamic, int size, bool big_endian>
1107 void
1108 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1109 ::do_adjust_output_section(Output_section* os)
1110 {
1111 if (sh_type == elfcpp::SHT_REL)
1112 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1113 else if (sh_type == elfcpp::SHT_RELA)
1114 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1115 else
1116 gold_unreachable();
1117 if (dynamic)
1118 os->set_should_link_to_dynsym();
1119 else
1120 os->set_should_link_to_symtab();
1121 }
1122
1123 // Write out relocation data.
1124
1125 template<int sh_type, bool dynamic, int size, bool big_endian>
1126 void
1127 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1128 Output_file* of)
1129 {
1130 const off_t off = this->offset();
1131 const off_t oview_size = this->data_size();
1132 unsigned char* const oview = of->get_output_view(off, oview_size);
1133
1134 if (this->sort_relocs())
1135 {
1136 gold_assert(dynamic);
1137 std::sort(this->relocs_.begin(), this->relocs_.end(),
1138 Sort_relocs_comparison());
1139 }
1140
1141 unsigned char* pov = oview;
1142 for (typename Relocs::const_iterator p = this->relocs_.begin();
1143 p != this->relocs_.end();
1144 ++p)
1145 {
1146 p->write(pov);
1147 pov += reloc_size;
1148 }
1149
1150 gold_assert(pov - oview == oview_size);
1151
1152 of->write_output_view(off, oview_size, oview);
1153
1154 // We no longer need the relocation entries.
1155 this->relocs_.clear();
1156 }
1157
1158 // Class Output_relocatable_relocs.
1159
1160 template<int sh_type, int size, bool big_endian>
1161 void
1162 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1163 {
1164 this->set_data_size(this->rr_->output_reloc_count()
1165 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1166 }
1167
1168 // class Output_data_group.
1169
1170 template<int size, bool big_endian>
1171 Output_data_group<size, big_endian>::Output_data_group(
1172 Sized_relobj<size, big_endian>* relobj,
1173 section_size_type entry_count,
1174 elfcpp::Elf_Word flags,
1175 std::vector<unsigned int>* input_shndxes)
1176 : Output_section_data(entry_count * 4, 4, false),
1177 relobj_(relobj),
1178 flags_(flags)
1179 {
1180 this->input_shndxes_.swap(*input_shndxes);
1181 }
1182
1183 // Write out the section group, which means translating the section
1184 // indexes to apply to the output file.
1185
1186 template<int size, bool big_endian>
1187 void
1188 Output_data_group<size, big_endian>::do_write(Output_file* of)
1189 {
1190 const off_t off = this->offset();
1191 const section_size_type oview_size =
1192 convert_to_section_size_type(this->data_size());
1193 unsigned char* const oview = of->get_output_view(off, oview_size);
1194
1195 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1196 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1197 ++contents;
1198
1199 for (std::vector<unsigned int>::const_iterator p =
1200 this->input_shndxes_.begin();
1201 p != this->input_shndxes_.end();
1202 ++p, ++contents)
1203 {
1204 Output_section* os = this->relobj_->output_section(*p);
1205
1206 unsigned int output_shndx;
1207 if (os != NULL)
1208 output_shndx = os->out_shndx();
1209 else
1210 {
1211 this->relobj_->error(_("section group retained but "
1212 "group element discarded"));
1213 output_shndx = 0;
1214 }
1215
1216 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1217 }
1218
1219 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1220 gold_assert(wrote == oview_size);
1221
1222 of->write_output_view(off, oview_size, oview);
1223
1224 // We no longer need this information.
1225 this->input_shndxes_.clear();
1226 }
1227
1228 // Output_data_got::Got_entry methods.
1229
1230 // Write out the entry.
1231
1232 template<int size, bool big_endian>
1233 void
1234 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1235 {
1236 Valtype val = 0;
1237
1238 switch (this->local_sym_index_)
1239 {
1240 case GSYM_CODE:
1241 {
1242 // If the symbol is resolved locally, we need to write out the
1243 // link-time value, which will be relocated dynamically by a
1244 // RELATIVE relocation.
1245 Symbol* gsym = this->u_.gsym;
1246 Sized_symbol<size>* sgsym;
1247 // This cast is a bit ugly. We don't want to put a
1248 // virtual method in Symbol, because we want Symbol to be
1249 // as small as possible.
1250 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1251 val = sgsym->value();
1252 }
1253 break;
1254
1255 case CONSTANT_CODE:
1256 val = this->u_.constant;
1257 break;
1258
1259 default:
1260 {
1261 const unsigned int lsi = this->local_sym_index_;
1262 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1263 val = symval->value(this->u_.object, 0);
1264 }
1265 break;
1266 }
1267
1268 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1269 }
1270
1271 // Output_data_got methods.
1272
1273 // Add an entry for a global symbol to the GOT. This returns true if
1274 // this is a new GOT entry, false if the symbol already had a GOT
1275 // entry.
1276
1277 template<int size, bool big_endian>
1278 bool
1279 Output_data_got<size, big_endian>::add_global(
1280 Symbol* gsym,
1281 unsigned int got_type)
1282 {
1283 if (gsym->has_got_offset(got_type))
1284 return false;
1285
1286 this->entries_.push_back(Got_entry(gsym));
1287 this->set_got_size();
1288 gsym->set_got_offset(got_type, this->last_got_offset());
1289 return true;
1290 }
1291
1292 // Add an entry for a global symbol to the GOT, and add a dynamic
1293 // relocation of type R_TYPE for the GOT entry.
1294 template<int size, bool big_endian>
1295 void
1296 Output_data_got<size, big_endian>::add_global_with_rel(
1297 Symbol* gsym,
1298 unsigned int got_type,
1299 Rel_dyn* rel_dyn,
1300 unsigned int r_type)
1301 {
1302 if (gsym->has_got_offset(got_type))
1303 return;
1304
1305 this->entries_.push_back(Got_entry());
1306 this->set_got_size();
1307 unsigned int got_offset = this->last_got_offset();
1308 gsym->set_got_offset(got_type, got_offset);
1309 rel_dyn->add_global(gsym, r_type, this, got_offset);
1310 }
1311
1312 template<int size, bool big_endian>
1313 void
1314 Output_data_got<size, big_endian>::add_global_with_rela(
1315 Symbol* gsym,
1316 unsigned int got_type,
1317 Rela_dyn* rela_dyn,
1318 unsigned int r_type)
1319 {
1320 if (gsym->has_got_offset(got_type))
1321 return;
1322
1323 this->entries_.push_back(Got_entry());
1324 this->set_got_size();
1325 unsigned int got_offset = this->last_got_offset();
1326 gsym->set_got_offset(got_type, got_offset);
1327 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1328 }
1329
1330 // Add a pair of entries for a global symbol to the GOT, and add
1331 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1332 // If R_TYPE_2 == 0, add the second entry with no relocation.
1333 template<int size, bool big_endian>
1334 void
1335 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1336 Symbol* gsym,
1337 unsigned int got_type,
1338 Rel_dyn* rel_dyn,
1339 unsigned int r_type_1,
1340 unsigned int r_type_2)
1341 {
1342 if (gsym->has_got_offset(got_type))
1343 return;
1344
1345 this->entries_.push_back(Got_entry());
1346 unsigned int got_offset = this->last_got_offset();
1347 gsym->set_got_offset(got_type, got_offset);
1348 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1349
1350 this->entries_.push_back(Got_entry());
1351 if (r_type_2 != 0)
1352 {
1353 got_offset = this->last_got_offset();
1354 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1355 }
1356
1357 this->set_got_size();
1358 }
1359
1360 template<int size, bool big_endian>
1361 void
1362 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1363 Symbol* gsym,
1364 unsigned int got_type,
1365 Rela_dyn* rela_dyn,
1366 unsigned int r_type_1,
1367 unsigned int r_type_2)
1368 {
1369 if (gsym->has_got_offset(got_type))
1370 return;
1371
1372 this->entries_.push_back(Got_entry());
1373 unsigned int got_offset = this->last_got_offset();
1374 gsym->set_got_offset(got_type, got_offset);
1375 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1376
1377 this->entries_.push_back(Got_entry());
1378 if (r_type_2 != 0)
1379 {
1380 got_offset = this->last_got_offset();
1381 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1382 }
1383
1384 this->set_got_size();
1385 }
1386
1387 // Add an entry for a local symbol to the GOT. This returns true if
1388 // this is a new GOT entry, false if the symbol already has a GOT
1389 // entry.
1390
1391 template<int size, bool big_endian>
1392 bool
1393 Output_data_got<size, big_endian>::add_local(
1394 Sized_relobj<size, big_endian>* object,
1395 unsigned int symndx,
1396 unsigned int got_type)
1397 {
1398 if (object->local_has_got_offset(symndx, got_type))
1399 return false;
1400
1401 this->entries_.push_back(Got_entry(object, symndx));
1402 this->set_got_size();
1403 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1404 return true;
1405 }
1406
1407 // Add an entry for a local symbol to the GOT, and add a dynamic
1408 // relocation of type R_TYPE for the GOT entry.
1409 template<int size, bool big_endian>
1410 void
1411 Output_data_got<size, big_endian>::add_local_with_rel(
1412 Sized_relobj<size, big_endian>* object,
1413 unsigned int symndx,
1414 unsigned int got_type,
1415 Rel_dyn* rel_dyn,
1416 unsigned int r_type)
1417 {
1418 if (object->local_has_got_offset(symndx, got_type))
1419 return;
1420
1421 this->entries_.push_back(Got_entry());
1422 this->set_got_size();
1423 unsigned int got_offset = this->last_got_offset();
1424 object->set_local_got_offset(symndx, got_type, got_offset);
1425 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1426 }
1427
1428 template<int size, bool big_endian>
1429 void
1430 Output_data_got<size, big_endian>::add_local_with_rela(
1431 Sized_relobj<size, big_endian>* object,
1432 unsigned int symndx,
1433 unsigned int got_type,
1434 Rela_dyn* rela_dyn,
1435 unsigned int r_type)
1436 {
1437 if (object->local_has_got_offset(symndx, got_type))
1438 return;
1439
1440 this->entries_.push_back(Got_entry());
1441 this->set_got_size();
1442 unsigned int got_offset = this->last_got_offset();
1443 object->set_local_got_offset(symndx, got_type, got_offset);
1444 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1445 }
1446
1447 // Add a pair of entries for a local symbol to the GOT, and add
1448 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1449 // If R_TYPE_2 == 0, add the second entry with no relocation.
1450 template<int size, bool big_endian>
1451 void
1452 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1453 Sized_relobj<size, big_endian>* object,
1454 unsigned int symndx,
1455 unsigned int shndx,
1456 unsigned int got_type,
1457 Rel_dyn* rel_dyn,
1458 unsigned int r_type_1,
1459 unsigned int r_type_2)
1460 {
1461 if (object->local_has_got_offset(symndx, got_type))
1462 return;
1463
1464 this->entries_.push_back(Got_entry());
1465 unsigned int got_offset = this->last_got_offset();
1466 object->set_local_got_offset(symndx, got_type, got_offset);
1467 Output_section* os = object->output_section(shndx);
1468 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1469
1470 this->entries_.push_back(Got_entry(object, symndx));
1471 if (r_type_2 != 0)
1472 {
1473 got_offset = this->last_got_offset();
1474 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1475 }
1476
1477 this->set_got_size();
1478 }
1479
1480 template<int size, bool big_endian>
1481 void
1482 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1483 Sized_relobj<size, big_endian>* object,
1484 unsigned int symndx,
1485 unsigned int shndx,
1486 unsigned int got_type,
1487 Rela_dyn* rela_dyn,
1488 unsigned int r_type_1,
1489 unsigned int r_type_2)
1490 {
1491 if (object->local_has_got_offset(symndx, got_type))
1492 return;
1493
1494 this->entries_.push_back(Got_entry());
1495 unsigned int got_offset = this->last_got_offset();
1496 object->set_local_got_offset(symndx, got_type, got_offset);
1497 Output_section* os = object->output_section(shndx);
1498 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1499
1500 this->entries_.push_back(Got_entry(object, symndx));
1501 if (r_type_2 != 0)
1502 {
1503 got_offset = this->last_got_offset();
1504 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1505 }
1506
1507 this->set_got_size();
1508 }
1509
1510 // Write out the GOT.
1511
1512 template<int size, bool big_endian>
1513 void
1514 Output_data_got<size, big_endian>::do_write(Output_file* of)
1515 {
1516 const int add = size / 8;
1517
1518 const off_t off = this->offset();
1519 const off_t oview_size = this->data_size();
1520 unsigned char* const oview = of->get_output_view(off, oview_size);
1521
1522 unsigned char* pov = oview;
1523 for (typename Got_entries::const_iterator p = this->entries_.begin();
1524 p != this->entries_.end();
1525 ++p)
1526 {
1527 p->write(pov);
1528 pov += add;
1529 }
1530
1531 gold_assert(pov - oview == oview_size);
1532
1533 of->write_output_view(off, oview_size, oview);
1534
1535 // We no longer need the GOT entries.
1536 this->entries_.clear();
1537 }
1538
1539 // Output_data_dynamic::Dynamic_entry methods.
1540
1541 // Write out the entry.
1542
1543 template<int size, bool big_endian>
1544 void
1545 Output_data_dynamic::Dynamic_entry::write(
1546 unsigned char* pov,
1547 const Stringpool* pool) const
1548 {
1549 typename elfcpp::Elf_types<size>::Elf_WXword val;
1550 switch (this->offset_)
1551 {
1552 case DYNAMIC_NUMBER:
1553 val = this->u_.val;
1554 break;
1555
1556 case DYNAMIC_SECTION_SIZE:
1557 val = this->u_.od->data_size();
1558 break;
1559
1560 case DYNAMIC_SYMBOL:
1561 {
1562 const Sized_symbol<size>* s =
1563 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1564 val = s->value();
1565 }
1566 break;
1567
1568 case DYNAMIC_STRING:
1569 val = pool->get_offset(this->u_.str);
1570 break;
1571
1572 default:
1573 val = this->u_.od->address() + this->offset_;
1574 break;
1575 }
1576
1577 elfcpp::Dyn_write<size, big_endian> dw(pov);
1578 dw.put_d_tag(this->tag_);
1579 dw.put_d_val(val);
1580 }
1581
1582 // Output_data_dynamic methods.
1583
1584 // Adjust the output section to set the entry size.
1585
1586 void
1587 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1588 {
1589 if (parameters->target().get_size() == 32)
1590 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1591 else if (parameters->target().get_size() == 64)
1592 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1593 else
1594 gold_unreachable();
1595 }
1596
1597 // Set the final data size.
1598
1599 void
1600 Output_data_dynamic::set_final_data_size()
1601 {
1602 // Add the terminating entry if it hasn't been added.
1603 // Because of relaxation, we can run this multiple times.
1604 if (this->entries_.empty()
1605 || this->entries_.rbegin()->tag() != elfcpp::DT_NULL)
1606 this->add_constant(elfcpp::DT_NULL, 0);
1607
1608 int dyn_size;
1609 if (parameters->target().get_size() == 32)
1610 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1611 else if (parameters->target().get_size() == 64)
1612 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1613 else
1614 gold_unreachable();
1615 this->set_data_size(this->entries_.size() * dyn_size);
1616 }
1617
1618 // Write out the dynamic entries.
1619
1620 void
1621 Output_data_dynamic::do_write(Output_file* of)
1622 {
1623 switch (parameters->size_and_endianness())
1624 {
1625 #ifdef HAVE_TARGET_32_LITTLE
1626 case Parameters::TARGET_32_LITTLE:
1627 this->sized_write<32, false>(of);
1628 break;
1629 #endif
1630 #ifdef HAVE_TARGET_32_BIG
1631 case Parameters::TARGET_32_BIG:
1632 this->sized_write<32, true>(of);
1633 break;
1634 #endif
1635 #ifdef HAVE_TARGET_64_LITTLE
1636 case Parameters::TARGET_64_LITTLE:
1637 this->sized_write<64, false>(of);
1638 break;
1639 #endif
1640 #ifdef HAVE_TARGET_64_BIG
1641 case Parameters::TARGET_64_BIG:
1642 this->sized_write<64, true>(of);
1643 break;
1644 #endif
1645 default:
1646 gold_unreachable();
1647 }
1648 }
1649
1650 template<int size, bool big_endian>
1651 void
1652 Output_data_dynamic::sized_write(Output_file* of)
1653 {
1654 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1655
1656 const off_t offset = this->offset();
1657 const off_t oview_size = this->data_size();
1658 unsigned char* const oview = of->get_output_view(offset, oview_size);
1659
1660 unsigned char* pov = oview;
1661 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1662 p != this->entries_.end();
1663 ++p)
1664 {
1665 p->write<size, big_endian>(pov, this->pool_);
1666 pov += dyn_size;
1667 }
1668
1669 gold_assert(pov - oview == oview_size);
1670
1671 of->write_output_view(offset, oview_size, oview);
1672
1673 // We no longer need the dynamic entries.
1674 this->entries_.clear();
1675 }
1676
1677 // Class Output_symtab_xindex.
1678
1679 void
1680 Output_symtab_xindex::do_write(Output_file* of)
1681 {
1682 const off_t offset = this->offset();
1683 const off_t oview_size = this->data_size();
1684 unsigned char* const oview = of->get_output_view(offset, oview_size);
1685
1686 memset(oview, 0, oview_size);
1687
1688 if (parameters->target().is_big_endian())
1689 this->endian_do_write<true>(oview);
1690 else
1691 this->endian_do_write<false>(oview);
1692
1693 of->write_output_view(offset, oview_size, oview);
1694
1695 // We no longer need the data.
1696 this->entries_.clear();
1697 }
1698
1699 template<bool big_endian>
1700 void
1701 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1702 {
1703 for (Xindex_entries::const_iterator p = this->entries_.begin();
1704 p != this->entries_.end();
1705 ++p)
1706 {
1707 unsigned int symndx = p->first;
1708 gold_assert(symndx * 4 < this->data_size());
1709 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1710 }
1711 }
1712
1713 // Output_section::Input_section methods.
1714
1715 // Return the data size. For an input section we store the size here.
1716 // For an Output_section_data, we have to ask it for the size.
1717
1718 off_t
1719 Output_section::Input_section::data_size() const
1720 {
1721 if (this->is_input_section())
1722 return this->u1_.data_size;
1723 else
1724 return this->u2_.posd->data_size();
1725 }
1726
1727 // Set the address and file offset.
1728
1729 void
1730 Output_section::Input_section::set_address_and_file_offset(
1731 uint64_t address,
1732 off_t file_offset,
1733 off_t section_file_offset)
1734 {
1735 if (this->is_input_section())
1736 this->u2_.object->set_section_offset(this->shndx_,
1737 file_offset - section_file_offset);
1738 else
1739 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1740 }
1741
1742 // Reset the address and file offset.
1743
1744 void
1745 Output_section::Input_section::reset_address_and_file_offset()
1746 {
1747 if (!this->is_input_section())
1748 this->u2_.posd->reset_address_and_file_offset();
1749 }
1750
1751 // Finalize the data size.
1752
1753 void
1754 Output_section::Input_section::finalize_data_size()
1755 {
1756 if (!this->is_input_section())
1757 this->u2_.posd->finalize_data_size();
1758 }
1759
1760 // Try to turn an input offset into an output offset. We want to
1761 // return the output offset relative to the start of this
1762 // Input_section in the output section.
1763
1764 inline bool
1765 Output_section::Input_section::output_offset(
1766 const Relobj* object,
1767 unsigned int shndx,
1768 section_offset_type offset,
1769 section_offset_type *poutput) const
1770 {
1771 if (!this->is_input_section())
1772 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1773 else
1774 {
1775 if (this->shndx_ != shndx || this->u2_.object != object)
1776 return false;
1777 *poutput = offset;
1778 return true;
1779 }
1780 }
1781
1782 // Return whether this is the merge section for the input section
1783 // SHNDX in OBJECT.
1784
1785 inline bool
1786 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1787 unsigned int shndx) const
1788 {
1789 if (this->is_input_section())
1790 return false;
1791 return this->u2_.posd->is_merge_section_for(object, shndx);
1792 }
1793
1794 // Write out the data. We don't have to do anything for an input
1795 // section--they are handled via Object::relocate--but this is where
1796 // we write out the data for an Output_section_data.
1797
1798 void
1799 Output_section::Input_section::write(Output_file* of)
1800 {
1801 if (!this->is_input_section())
1802 this->u2_.posd->write(of);
1803 }
1804
1805 // Write the data to a buffer. As for write(), we don't have to do
1806 // anything for an input section.
1807
1808 void
1809 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1810 {
1811 if (!this->is_input_section())
1812 this->u2_.posd->write_to_buffer(buffer);
1813 }
1814
1815 // Print to a map file.
1816
1817 void
1818 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1819 {
1820 switch (this->shndx_)
1821 {
1822 case OUTPUT_SECTION_CODE:
1823 case MERGE_DATA_SECTION_CODE:
1824 case MERGE_STRING_SECTION_CODE:
1825 this->u2_.posd->print_to_mapfile(mapfile);
1826 break;
1827
1828 case RELAXED_INPUT_SECTION_CODE:
1829 {
1830 Output_relaxed_input_section* relaxed_section =
1831 this->relaxed_input_section();
1832 mapfile->print_input_section(relaxed_section->relobj(),
1833 relaxed_section->shndx());
1834 }
1835 break;
1836 default:
1837 mapfile->print_input_section(this->u2_.object, this->shndx_);
1838 break;
1839 }
1840 }
1841
1842 // Output_section methods.
1843
1844 // Construct an Output_section. NAME will point into a Stringpool.
1845
1846 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1847 elfcpp::Elf_Xword flags)
1848 : name_(name),
1849 addralign_(0),
1850 entsize_(0),
1851 load_address_(0),
1852 link_section_(NULL),
1853 link_(0),
1854 info_section_(NULL),
1855 info_symndx_(NULL),
1856 info_(0),
1857 type_(type),
1858 flags_(flags),
1859 out_shndx_(-1U),
1860 symtab_index_(0),
1861 dynsym_index_(0),
1862 input_sections_(),
1863 first_input_offset_(0),
1864 fills_(),
1865 postprocessing_buffer_(NULL),
1866 needs_symtab_index_(false),
1867 needs_dynsym_index_(false),
1868 should_link_to_symtab_(false),
1869 should_link_to_dynsym_(false),
1870 after_input_sections_(false),
1871 requires_postprocessing_(false),
1872 found_in_sections_clause_(false),
1873 has_load_address_(false),
1874 info_uses_section_index_(false),
1875 may_sort_attached_input_sections_(false),
1876 must_sort_attached_input_sections_(false),
1877 attached_input_sections_are_sorted_(false),
1878 is_relro_(false),
1879 is_relro_local_(false),
1880 is_last_relro_(false),
1881 is_first_non_relro_(false),
1882 is_small_section_(false),
1883 is_large_section_(false),
1884 is_interp_(false),
1885 is_dynamic_linker_section_(false),
1886 generate_code_fills_at_write_(false),
1887 is_entsize_zero_(false),
1888 tls_offset_(0),
1889 checkpoint_(NULL),
1890 merge_section_map_(),
1891 merge_section_by_properties_map_(),
1892 relaxed_input_section_map_(),
1893 is_relaxed_input_section_map_valid_(true)
1894 {
1895 // An unallocated section has no address. Forcing this means that
1896 // we don't need special treatment for symbols defined in debug
1897 // sections.
1898 if ((flags & elfcpp::SHF_ALLOC) == 0)
1899 this->set_address(0);
1900 }
1901
1902 Output_section::~Output_section()
1903 {
1904 delete this->checkpoint_;
1905 }
1906
1907 // Set the entry size.
1908
1909 void
1910 Output_section::set_entsize(uint64_t v)
1911 {
1912 if (this->is_entsize_zero_)
1913 ;
1914 else if (this->entsize_ == 0)
1915 this->entsize_ = v;
1916 else if (this->entsize_ != v)
1917 {
1918 this->entsize_ = 0;
1919 this->is_entsize_zero_ = 1;
1920 }
1921 }
1922
1923 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1924 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1925 // relocation section which applies to this section, or 0 if none, or
1926 // -1U if more than one. Return the offset of the input section
1927 // within the output section. Return -1 if the input section will
1928 // receive special handling. In the normal case we don't always keep
1929 // track of input sections for an Output_section. Instead, each
1930 // Object keeps track of the Output_section for each of its input
1931 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1932 // track of input sections here; this is used when SECTIONS appears in
1933 // a linker script.
1934
1935 template<int size, bool big_endian>
1936 off_t
1937 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1938 unsigned int shndx,
1939 const char* secname,
1940 const elfcpp::Shdr<size, big_endian>& shdr,
1941 unsigned int reloc_shndx,
1942 bool have_sections_script)
1943 {
1944 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1945 if ((addralign & (addralign - 1)) != 0)
1946 {
1947 object->error(_("invalid alignment %lu for section \"%s\""),
1948 static_cast<unsigned long>(addralign), secname);
1949 addralign = 1;
1950 }
1951
1952 if (addralign > this->addralign_)
1953 this->addralign_ = addralign;
1954
1955 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1956 uint64_t entsize = shdr.get_sh_entsize();
1957
1958 // .debug_str is a mergeable string section, but is not always so
1959 // marked by compilers. Mark manually here so we can optimize.
1960 if (strcmp(secname, ".debug_str") == 0)
1961 {
1962 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1963 entsize = 1;
1964 }
1965
1966 this->update_flags_for_input_section(sh_flags);
1967 this->set_entsize(entsize);
1968
1969 // If this is a SHF_MERGE section, we pass all the input sections to
1970 // a Output_data_merge. We don't try to handle relocations for such
1971 // a section. We don't try to handle empty merge sections--they
1972 // mess up the mappings, and are useless anyhow.
1973 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1974 && reloc_shndx == 0
1975 && shdr.get_sh_size() > 0)
1976 {
1977 if (this->add_merge_input_section(object, shndx, sh_flags,
1978 entsize, addralign))
1979 {
1980 // Tell the relocation routines that they need to call the
1981 // output_offset method to determine the final address.
1982 return -1;
1983 }
1984 }
1985
1986 off_t offset_in_section = this->current_data_size_for_child();
1987 off_t aligned_offset_in_section = align_address(offset_in_section,
1988 addralign);
1989
1990 // Determine if we want to delay code-fill generation until the output
1991 // section is written. When the target is relaxing, we want to delay fill
1992 // generating to avoid adjusting them during relaxation.
1993 if (!this->generate_code_fills_at_write_
1994 && !have_sections_script
1995 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1996 && parameters->target().has_code_fill()
1997 && parameters->target().may_relax())
1998 {
1999 gold_assert(this->fills_.empty());
2000 this->generate_code_fills_at_write_ = true;
2001 }
2002
2003 if (aligned_offset_in_section > offset_in_section
2004 && !this->generate_code_fills_at_write_
2005 && !have_sections_script
2006 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2007 && parameters->target().has_code_fill())
2008 {
2009 // We need to add some fill data. Using fill_list_ when
2010 // possible is an optimization, since we will often have fill
2011 // sections without input sections.
2012 off_t fill_len = aligned_offset_in_section - offset_in_section;
2013 if (this->input_sections_.empty())
2014 this->fills_.push_back(Fill(offset_in_section, fill_len));
2015 else
2016 {
2017 std::string fill_data(parameters->target().code_fill(fill_len));
2018 Output_data_const* odc = new Output_data_const(fill_data, 1);
2019 this->input_sections_.push_back(Input_section(odc));
2020 }
2021 }
2022
2023 this->set_current_data_size_for_child(aligned_offset_in_section
2024 + shdr.get_sh_size());
2025
2026 // We need to keep track of this section if we are already keeping
2027 // track of sections, or if we are relaxing. Also, if this is a
2028 // section which requires sorting, or which may require sorting in
2029 // the future, we keep track of the sections.
2030 if (have_sections_script
2031 || !this->input_sections_.empty()
2032 || this->may_sort_attached_input_sections()
2033 || this->must_sort_attached_input_sections()
2034 || parameters->options().user_set_Map()
2035 || parameters->target().may_relax())
2036 this->input_sections_.push_back(Input_section(object, shndx,
2037 shdr.get_sh_size(),
2038 addralign));
2039
2040 return aligned_offset_in_section;
2041 }
2042
2043 // Add arbitrary data to an output section.
2044
2045 void
2046 Output_section::add_output_section_data(Output_section_data* posd)
2047 {
2048 Input_section inp(posd);
2049 this->add_output_section_data(&inp);
2050
2051 if (posd->is_data_size_valid())
2052 {
2053 off_t offset_in_section = this->current_data_size_for_child();
2054 off_t aligned_offset_in_section = align_address(offset_in_section,
2055 posd->addralign());
2056 this->set_current_data_size_for_child(aligned_offset_in_section
2057 + posd->data_size());
2058 }
2059 }
2060
2061 // Add a relaxed input section.
2062
2063 void
2064 Output_section::add_relaxed_input_section(Output_relaxed_input_section* poris)
2065 {
2066 Input_section inp(poris);
2067 this->add_output_section_data(&inp);
2068 if (this->is_relaxed_input_section_map_valid_)
2069 {
2070 Input_section_specifier iss(poris->relobj(), poris->shndx());
2071 this->relaxed_input_section_map_[iss] = poris;
2072 }
2073
2074 // For a relaxed section, we use the current data size. Linker scripts
2075 // get all the input sections, including relaxed one from an output
2076 // section and add them back to them same output section to compute the
2077 // output section size. If we do not account for sizes of relaxed input
2078 // sections, an output section would be incorrectly sized.
2079 off_t offset_in_section = this->current_data_size_for_child();
2080 off_t aligned_offset_in_section = align_address(offset_in_section,
2081 poris->addralign());
2082 this->set_current_data_size_for_child(aligned_offset_in_section
2083 + poris->current_data_size());
2084 }
2085
2086 // Add arbitrary data to an output section by Input_section.
2087
2088 void
2089 Output_section::add_output_section_data(Input_section* inp)
2090 {
2091 if (this->input_sections_.empty())
2092 this->first_input_offset_ = this->current_data_size_for_child();
2093
2094 this->input_sections_.push_back(*inp);
2095
2096 uint64_t addralign = inp->addralign();
2097 if (addralign > this->addralign_)
2098 this->addralign_ = addralign;
2099
2100 inp->set_output_section(this);
2101 }
2102
2103 // Add a merge section to an output section.
2104
2105 void
2106 Output_section::add_output_merge_section(Output_section_data* posd,
2107 bool is_string, uint64_t entsize)
2108 {
2109 Input_section inp(posd, is_string, entsize);
2110 this->add_output_section_data(&inp);
2111 }
2112
2113 // Add an input section to a SHF_MERGE section.
2114
2115 bool
2116 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2117 uint64_t flags, uint64_t entsize,
2118 uint64_t addralign)
2119 {
2120 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2121
2122 // We only merge strings if the alignment is not more than the
2123 // character size. This could be handled, but it's unusual.
2124 if (is_string && addralign > entsize)
2125 return false;
2126
2127 // We cannot restore merged input section states.
2128 gold_assert(this->checkpoint_ == NULL);
2129
2130 // Look up merge sections by required properties.
2131 Merge_section_properties msp(is_string, entsize, addralign);
2132 Merge_section_by_properties_map::const_iterator p =
2133 this->merge_section_by_properties_map_.find(msp);
2134 if (p != this->merge_section_by_properties_map_.end())
2135 {
2136 Output_merge_base* merge_section = p->second;
2137 merge_section->add_input_section(object, shndx);
2138 gold_assert(merge_section->is_string() == is_string
2139 && merge_section->entsize() == entsize
2140 && merge_section->addralign() == addralign);
2141
2142 // Link input section to found merge section.
2143 Input_section_specifier iss(object, shndx);
2144 this->merge_section_map_[iss] = merge_section;
2145 return true;
2146 }
2147
2148 // We handle the actual constant merging in Output_merge_data or
2149 // Output_merge_string_data.
2150 Output_merge_base* pomb;
2151 if (!is_string)
2152 pomb = new Output_merge_data(entsize, addralign);
2153 else
2154 {
2155 switch (entsize)
2156 {
2157 case 1:
2158 pomb = new Output_merge_string<char>(addralign);
2159 break;
2160 case 2:
2161 pomb = new Output_merge_string<uint16_t>(addralign);
2162 break;
2163 case 4:
2164 pomb = new Output_merge_string<uint32_t>(addralign);
2165 break;
2166 default:
2167 return false;
2168 }
2169 }
2170
2171 // Add new merge section to this output section and link merge section
2172 // properties to new merge section in map.
2173 this->add_output_merge_section(pomb, is_string, entsize);
2174 this->merge_section_by_properties_map_[msp] = pomb;
2175
2176 // Add input section to new merge section and link input section to new
2177 // merge section in map.
2178 pomb->add_input_section(object, shndx);
2179 Input_section_specifier iss(object, shndx);
2180 this->merge_section_map_[iss] = pomb;
2181
2182 return true;
2183 }
2184
2185 // Build a relaxation map to speed up relaxation of existing input sections.
2186 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2187
2188 void
2189 Output_section::build_relaxation_map(
2190 const Input_section_list& input_sections,
2191 size_t limit,
2192 Relaxation_map* relaxation_map) const
2193 {
2194 for (size_t i = 0; i < limit; ++i)
2195 {
2196 const Input_section& is(input_sections[i]);
2197 if (is.is_input_section() || is.is_relaxed_input_section())
2198 {
2199 Input_section_specifier iss(is.relobj(), is.shndx());
2200 (*relaxation_map)[iss] = i;
2201 }
2202 }
2203 }
2204
2205 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2206 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from input section
2207 // specifier to indices of INPUT_SECTIONS.
2208
2209 void
2210 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2211 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2212 const Relaxation_map& map,
2213 Input_section_list* input_sections)
2214 {
2215 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2216 {
2217 Output_relaxed_input_section* poris = relaxed_sections[i];
2218 Input_section_specifier iss(poris->relobj(), poris->shndx());
2219 Relaxation_map::const_iterator p = map.find(iss);
2220 gold_assert(p != map.end());
2221 gold_assert((*input_sections)[p->second].is_input_section());
2222 (*input_sections)[p->second] = Input_section(poris);
2223 }
2224 }
2225
2226 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2227 // is a vector of pointers to Output_relaxed_input_section or its derived
2228 // classes. The relaxed sections must correspond to existing input sections.
2229
2230 void
2231 Output_section::convert_input_sections_to_relaxed_sections(
2232 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2233 {
2234 gold_assert(parameters->target().may_relax());
2235
2236 // We want to make sure that restore_states does not undo the effect of
2237 // this. If there is no checkpoint active, just search the current
2238 // input section list and replace the sections there. If there is
2239 // a checkpoint, also replace the sections there.
2240
2241 // By default, we look at the whole list.
2242 size_t limit = this->input_sections_.size();
2243
2244 if (this->checkpoint_ != NULL)
2245 {
2246 // Replace input sections with relaxed input section in the saved
2247 // copy of the input section list.
2248 if (this->checkpoint_->input_sections_saved())
2249 {
2250 Relaxation_map map;
2251 this->build_relaxation_map(
2252 *(this->checkpoint_->input_sections()),
2253 this->checkpoint_->input_sections()->size(),
2254 &map);
2255 this->convert_input_sections_in_list_to_relaxed_sections(
2256 relaxed_sections,
2257 map,
2258 this->checkpoint_->input_sections());
2259 }
2260 else
2261 {
2262 // We have not copied the input section list yet. Instead, just
2263 // look at the portion that would be saved.
2264 limit = this->checkpoint_->input_sections_size();
2265 }
2266 }
2267
2268 // Convert input sections in input_section_list.
2269 Relaxation_map map;
2270 this->build_relaxation_map(this->input_sections_, limit, &map);
2271 this->convert_input_sections_in_list_to_relaxed_sections(
2272 relaxed_sections,
2273 map,
2274 &this->input_sections_);
2275
2276 // Update fast look-up map.
2277 if (this->is_relaxed_input_section_map_valid_)
2278 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2279 {
2280 Output_relaxed_input_section* poris = relaxed_sections[i];
2281 Input_section_specifier iss(poris->relobj(), poris->shndx());
2282 this->relaxed_input_section_map_[iss] = poris;
2283 }
2284 }
2285
2286 // Update the output section flags based on input section flags.
2287
2288 void
2289 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2290 {
2291 // If we created the section with SHF_ALLOC clear, we set the
2292 // address. If we are now setting the SHF_ALLOC flag, we need to
2293 // undo that.
2294 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2295 && (flags & elfcpp::SHF_ALLOC) != 0)
2296 this->mark_address_invalid();
2297
2298 this->flags_ |= (flags
2299 & (elfcpp::SHF_WRITE
2300 | elfcpp::SHF_ALLOC
2301 | elfcpp::SHF_EXECINSTR));
2302
2303 if ((flags & elfcpp::SHF_MERGE) == 0)
2304 this->flags_ &=~ elfcpp::SHF_MERGE;
2305 else
2306 {
2307 if (this->current_data_size_for_child() == 0)
2308 this->flags_ |= elfcpp::SHF_MERGE;
2309 }
2310
2311 if ((flags & elfcpp::SHF_STRINGS) == 0)
2312 this->flags_ &=~ elfcpp::SHF_STRINGS;
2313 else
2314 {
2315 if (this->current_data_size_for_child() == 0)
2316 this->flags_ |= elfcpp::SHF_STRINGS;
2317 }
2318 }
2319
2320 // Find the merge section into which an input section with index SHNDX in
2321 // OBJECT has been added. Return NULL if none found.
2322
2323 Output_section_data*
2324 Output_section::find_merge_section(const Relobj* object,
2325 unsigned int shndx) const
2326 {
2327 Input_section_specifier iss(object, shndx);
2328 Output_section_data_by_input_section_map::const_iterator p =
2329 this->merge_section_map_.find(iss);
2330 if (p != this->merge_section_map_.end())
2331 {
2332 Output_section_data* posd = p->second;
2333 gold_assert(posd->is_merge_section_for(object, shndx));
2334 return posd;
2335 }
2336 else
2337 return NULL;
2338 }
2339
2340 // Find an relaxed input section corresponding to an input section
2341 // in OBJECT with index SHNDX.
2342
2343 const Output_relaxed_input_section*
2344 Output_section::find_relaxed_input_section(const Relobj* object,
2345 unsigned int shndx) const
2346 {
2347 // Be careful that the map may not be valid due to input section export
2348 // to scripts or a check-point restore.
2349 if (!this->is_relaxed_input_section_map_valid_)
2350 {
2351 // Rebuild the map as needed.
2352 this->relaxed_input_section_map_.clear();
2353 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2354 p != this->input_sections_.end();
2355 ++p)
2356 if (p->is_relaxed_input_section())
2357 {
2358 Input_section_specifier iss(p->relobj(), p->shndx());
2359 this->relaxed_input_section_map_[iss] =
2360 p->relaxed_input_section();
2361 }
2362 this->is_relaxed_input_section_map_valid_ = true;
2363 }
2364
2365 Input_section_specifier iss(object, shndx);
2366 Output_relaxed_input_section_by_input_section_map::const_iterator p =
2367 this->relaxed_input_section_map_.find(iss);
2368 if (p != this->relaxed_input_section_map_.end())
2369 return p->second;
2370 else
2371 return NULL;
2372 }
2373
2374 // Given an address OFFSET relative to the start of input section
2375 // SHNDX in OBJECT, return whether this address is being included in
2376 // the final link. This should only be called if SHNDX in OBJECT has
2377 // a special mapping.
2378
2379 bool
2380 Output_section::is_input_address_mapped(const Relobj* object,
2381 unsigned int shndx,
2382 off_t offset) const
2383 {
2384 // Look at the Output_section_data_maps first.
2385 const Output_section_data* posd = this->find_merge_section(object, shndx);
2386 if (posd == NULL)
2387 posd = this->find_relaxed_input_section(object, shndx);
2388
2389 if (posd != NULL)
2390 {
2391 section_offset_type output_offset;
2392 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2393 gold_assert(found);
2394 return output_offset != -1;
2395 }
2396
2397 // Fall back to the slow look-up.
2398 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2399 p != this->input_sections_.end();
2400 ++p)
2401 {
2402 section_offset_type output_offset;
2403 if (p->output_offset(object, shndx, offset, &output_offset))
2404 return output_offset != -1;
2405 }
2406
2407 // By default we assume that the address is mapped. This should
2408 // only be called after we have passed all sections to Layout. At
2409 // that point we should know what we are discarding.
2410 return true;
2411 }
2412
2413 // Given an address OFFSET relative to the start of input section
2414 // SHNDX in object OBJECT, return the output offset relative to the
2415 // start of the input section in the output section. This should only
2416 // be called if SHNDX in OBJECT has a special mapping.
2417
2418 section_offset_type
2419 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2420 section_offset_type offset) const
2421 {
2422 // This can only be called meaningfully when we know the data size
2423 // of this.
2424 gold_assert(this->is_data_size_valid());
2425
2426 // Look at the Output_section_data_maps first.
2427 const Output_section_data* posd = this->find_merge_section(object, shndx);
2428 if (posd == NULL)
2429 posd = this->find_relaxed_input_section(object, shndx);
2430 if (posd != NULL)
2431 {
2432 section_offset_type output_offset;
2433 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2434 gold_assert(found);
2435 return output_offset;
2436 }
2437
2438 // Fall back to the slow look-up.
2439 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2440 p != this->input_sections_.end();
2441 ++p)
2442 {
2443 section_offset_type output_offset;
2444 if (p->output_offset(object, shndx, offset, &output_offset))
2445 return output_offset;
2446 }
2447 gold_unreachable();
2448 }
2449
2450 // Return the output virtual address of OFFSET relative to the start
2451 // of input section SHNDX in object OBJECT.
2452
2453 uint64_t
2454 Output_section::output_address(const Relobj* object, unsigned int shndx,
2455 off_t offset) const
2456 {
2457 uint64_t addr = this->address() + this->first_input_offset_;
2458
2459 // Look at the Output_section_data_maps first.
2460 const Output_section_data* posd = this->find_merge_section(object, shndx);
2461 if (posd == NULL)
2462 posd = this->find_relaxed_input_section(object, shndx);
2463 if (posd != NULL && posd->is_address_valid())
2464 {
2465 section_offset_type output_offset;
2466 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2467 gold_assert(found);
2468 return posd->address() + output_offset;
2469 }
2470
2471 // Fall back to the slow look-up.
2472 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2473 p != this->input_sections_.end();
2474 ++p)
2475 {
2476 addr = align_address(addr, p->addralign());
2477 section_offset_type output_offset;
2478 if (p->output_offset(object, shndx, offset, &output_offset))
2479 {
2480 if (output_offset == -1)
2481 return -1ULL;
2482 return addr + output_offset;
2483 }
2484 addr += p->data_size();
2485 }
2486
2487 // If we get here, it means that we don't know the mapping for this
2488 // input section. This might happen in principle if
2489 // add_input_section were called before add_output_section_data.
2490 // But it should never actually happen.
2491
2492 gold_unreachable();
2493 }
2494
2495 // Find the output address of the start of the merged section for
2496 // input section SHNDX in object OBJECT.
2497
2498 bool
2499 Output_section::find_starting_output_address(const Relobj* object,
2500 unsigned int shndx,
2501 uint64_t* paddr) const
2502 {
2503 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2504 // Looking up the merge section map does not always work as we sometimes
2505 // find a merge section without its address set.
2506 uint64_t addr = this->address() + this->first_input_offset_;
2507 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2508 p != this->input_sections_.end();
2509 ++p)
2510 {
2511 addr = align_address(addr, p->addralign());
2512
2513 // It would be nice if we could use the existing output_offset
2514 // method to get the output offset of input offset 0.
2515 // Unfortunately we don't know for sure that input offset 0 is
2516 // mapped at all.
2517 if (p->is_merge_section_for(object, shndx))
2518 {
2519 *paddr = addr;
2520 return true;
2521 }
2522
2523 addr += p->data_size();
2524 }
2525
2526 // We couldn't find a merge output section for this input section.
2527 return false;
2528 }
2529
2530 // Set the data size of an Output_section. This is where we handle
2531 // setting the addresses of any Output_section_data objects.
2532
2533 void
2534 Output_section::set_final_data_size()
2535 {
2536 if (this->input_sections_.empty())
2537 {
2538 this->set_data_size(this->current_data_size_for_child());
2539 return;
2540 }
2541
2542 if (this->must_sort_attached_input_sections())
2543 this->sort_attached_input_sections();
2544
2545 uint64_t address = this->address();
2546 off_t startoff = this->offset();
2547 off_t off = startoff + this->first_input_offset_;
2548 for (Input_section_list::iterator p = this->input_sections_.begin();
2549 p != this->input_sections_.end();
2550 ++p)
2551 {
2552 off = align_address(off, p->addralign());
2553 p->set_address_and_file_offset(address + (off - startoff), off,
2554 startoff);
2555 off += p->data_size();
2556 }
2557
2558 this->set_data_size(off - startoff);
2559 }
2560
2561 // Reset the address and file offset.
2562
2563 void
2564 Output_section::do_reset_address_and_file_offset()
2565 {
2566 // An unallocated section has no address. Forcing this means that
2567 // we don't need special treatment for symbols defined in debug
2568 // sections. We do the same in the constructor.
2569 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2570 this->set_address(0);
2571
2572 for (Input_section_list::iterator p = this->input_sections_.begin();
2573 p != this->input_sections_.end();
2574 ++p)
2575 p->reset_address_and_file_offset();
2576 }
2577
2578 // Return true if address and file offset have the values after reset.
2579
2580 bool
2581 Output_section::do_address_and_file_offset_have_reset_values() const
2582 {
2583 if (this->is_offset_valid())
2584 return false;
2585
2586 // An unallocated section has address 0 after its construction or a reset.
2587 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2588 return this->is_address_valid() && this->address() == 0;
2589 else
2590 return !this->is_address_valid();
2591 }
2592
2593 // Set the TLS offset. Called only for SHT_TLS sections.
2594
2595 void
2596 Output_section::do_set_tls_offset(uint64_t tls_base)
2597 {
2598 this->tls_offset_ = this->address() - tls_base;
2599 }
2600
2601 // In a few cases we need to sort the input sections attached to an
2602 // output section. This is used to implement the type of constructor
2603 // priority ordering implemented by the GNU linker, in which the
2604 // priority becomes part of the section name and the sections are
2605 // sorted by name. We only do this for an output section if we see an
2606 // attached input section matching ".ctor.*", ".dtor.*",
2607 // ".init_array.*" or ".fini_array.*".
2608
2609 class Output_section::Input_section_sort_entry
2610 {
2611 public:
2612 Input_section_sort_entry()
2613 : input_section_(), index_(-1U), section_has_name_(false),
2614 section_name_()
2615 { }
2616
2617 Input_section_sort_entry(const Input_section& input_section,
2618 unsigned int index)
2619 : input_section_(input_section), index_(index),
2620 section_has_name_(input_section.is_input_section()
2621 || input_section.is_relaxed_input_section())
2622 {
2623 if (this->section_has_name_)
2624 {
2625 // This is only called single-threaded from Layout::finalize,
2626 // so it is OK to lock. Unfortunately we have no way to pass
2627 // in a Task token.
2628 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2629 Object* obj = (input_section.is_input_section()
2630 ? input_section.relobj()
2631 : input_section.relaxed_input_section()->relobj());
2632 Task_lock_obj<Object> tl(dummy_task, obj);
2633
2634 // This is a slow operation, which should be cached in
2635 // Layout::layout if this becomes a speed problem.
2636 this->section_name_ = obj->section_name(input_section.shndx());
2637 }
2638 }
2639
2640 // Return the Input_section.
2641 const Input_section&
2642 input_section() const
2643 {
2644 gold_assert(this->index_ != -1U);
2645 return this->input_section_;
2646 }
2647
2648 // The index of this entry in the original list. This is used to
2649 // make the sort stable.
2650 unsigned int
2651 index() const
2652 {
2653 gold_assert(this->index_ != -1U);
2654 return this->index_;
2655 }
2656
2657 // Whether there is a section name.
2658 bool
2659 section_has_name() const
2660 { return this->section_has_name_; }
2661
2662 // The section name.
2663 const std::string&
2664 section_name() const
2665 {
2666 gold_assert(this->section_has_name_);
2667 return this->section_name_;
2668 }
2669
2670 // Return true if the section name has a priority. This is assumed
2671 // to be true if it has a dot after the initial dot.
2672 bool
2673 has_priority() const
2674 {
2675 gold_assert(this->section_has_name_);
2676 return this->section_name_.find('.', 1);
2677 }
2678
2679 // Return true if this an input file whose base name matches
2680 // FILE_NAME. The base name must have an extension of ".o", and
2681 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2682 // This is to match crtbegin.o as well as crtbeginS.o without
2683 // getting confused by other possibilities. Overall matching the
2684 // file name this way is a dreadful hack, but the GNU linker does it
2685 // in order to better support gcc, and we need to be compatible.
2686 bool
2687 match_file_name(const char* match_file_name) const
2688 {
2689 const std::string& file_name(this->input_section_.relobj()->name());
2690 const char* base_name = lbasename(file_name.c_str());
2691 size_t match_len = strlen(match_file_name);
2692 if (strncmp(base_name, match_file_name, match_len) != 0)
2693 return false;
2694 size_t base_len = strlen(base_name);
2695 if (base_len != match_len + 2 && base_len != match_len + 3)
2696 return false;
2697 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2698 }
2699
2700 private:
2701 // The Input_section we are sorting.
2702 Input_section input_section_;
2703 // The index of this Input_section in the original list.
2704 unsigned int index_;
2705 // Whether this Input_section has a section name--it won't if this
2706 // is some random Output_section_data.
2707 bool section_has_name_;
2708 // The section name if there is one.
2709 std::string section_name_;
2710 };
2711
2712 // Return true if S1 should come before S2 in the output section.
2713
2714 bool
2715 Output_section::Input_section_sort_compare::operator()(
2716 const Output_section::Input_section_sort_entry& s1,
2717 const Output_section::Input_section_sort_entry& s2) const
2718 {
2719 // crtbegin.o must come first.
2720 bool s1_begin = s1.match_file_name("crtbegin");
2721 bool s2_begin = s2.match_file_name("crtbegin");
2722 if (s1_begin || s2_begin)
2723 {
2724 if (!s1_begin)
2725 return false;
2726 if (!s2_begin)
2727 return true;
2728 return s1.index() < s2.index();
2729 }
2730
2731 // crtend.o must come last.
2732 bool s1_end = s1.match_file_name("crtend");
2733 bool s2_end = s2.match_file_name("crtend");
2734 if (s1_end || s2_end)
2735 {
2736 if (!s1_end)
2737 return true;
2738 if (!s2_end)
2739 return false;
2740 return s1.index() < s2.index();
2741 }
2742
2743 // We sort all the sections with no names to the end.
2744 if (!s1.section_has_name() || !s2.section_has_name())
2745 {
2746 if (s1.section_has_name())
2747 return true;
2748 if (s2.section_has_name())
2749 return false;
2750 return s1.index() < s2.index();
2751 }
2752
2753 // A section with a priority follows a section without a priority.
2754 // The GNU linker does this for all but .init_array sections; until
2755 // further notice we'll assume that that is an mistake.
2756 bool s1_has_priority = s1.has_priority();
2757 bool s2_has_priority = s2.has_priority();
2758 if (s1_has_priority && !s2_has_priority)
2759 return false;
2760 if (!s1_has_priority && s2_has_priority)
2761 return true;
2762
2763 // Otherwise we sort by name.
2764 int compare = s1.section_name().compare(s2.section_name());
2765 if (compare != 0)
2766 return compare < 0;
2767
2768 // Otherwise we keep the input order.
2769 return s1.index() < s2.index();
2770 }
2771
2772 // Sort the input sections attached to an output section.
2773
2774 void
2775 Output_section::sort_attached_input_sections()
2776 {
2777 if (this->attached_input_sections_are_sorted_)
2778 return;
2779
2780 if (this->checkpoint_ != NULL
2781 && !this->checkpoint_->input_sections_saved())
2782 this->checkpoint_->save_input_sections();
2783
2784 // The only thing we know about an input section is the object and
2785 // the section index. We need the section name. Recomputing this
2786 // is slow but this is an unusual case. If this becomes a speed
2787 // problem we can cache the names as required in Layout::layout.
2788
2789 // We start by building a larger vector holding a copy of each
2790 // Input_section, plus its current index in the list and its name.
2791 std::vector<Input_section_sort_entry> sort_list;
2792
2793 unsigned int i = 0;
2794 for (Input_section_list::iterator p = this->input_sections_.begin();
2795 p != this->input_sections_.end();
2796 ++p, ++i)
2797 sort_list.push_back(Input_section_sort_entry(*p, i));
2798
2799 // Sort the input sections.
2800 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2801
2802 // Copy the sorted input sections back to our list.
2803 this->input_sections_.clear();
2804 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2805 p != sort_list.end();
2806 ++p)
2807 this->input_sections_.push_back(p->input_section());
2808
2809 // Remember that we sorted the input sections, since we might get
2810 // called again.
2811 this->attached_input_sections_are_sorted_ = true;
2812 }
2813
2814 // Write the section header to *OSHDR.
2815
2816 template<int size, bool big_endian>
2817 void
2818 Output_section::write_header(const Layout* layout,
2819 const Stringpool* secnamepool,
2820 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2821 {
2822 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2823 oshdr->put_sh_type(this->type_);
2824
2825 elfcpp::Elf_Xword flags = this->flags_;
2826 if (this->info_section_ != NULL && this->info_uses_section_index_)
2827 flags |= elfcpp::SHF_INFO_LINK;
2828 oshdr->put_sh_flags(flags);
2829
2830 oshdr->put_sh_addr(this->address());
2831 oshdr->put_sh_offset(this->offset());
2832 oshdr->put_sh_size(this->data_size());
2833 if (this->link_section_ != NULL)
2834 oshdr->put_sh_link(this->link_section_->out_shndx());
2835 else if (this->should_link_to_symtab_)
2836 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2837 else if (this->should_link_to_dynsym_)
2838 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2839 else
2840 oshdr->put_sh_link(this->link_);
2841
2842 elfcpp::Elf_Word info;
2843 if (this->info_section_ != NULL)
2844 {
2845 if (this->info_uses_section_index_)
2846 info = this->info_section_->out_shndx();
2847 else
2848 info = this->info_section_->symtab_index();
2849 }
2850 else if (this->info_symndx_ != NULL)
2851 info = this->info_symndx_->symtab_index();
2852 else
2853 info = this->info_;
2854 oshdr->put_sh_info(info);
2855
2856 oshdr->put_sh_addralign(this->addralign_);
2857 oshdr->put_sh_entsize(this->entsize_);
2858 }
2859
2860 // Write out the data. For input sections the data is written out by
2861 // Object::relocate, but we have to handle Output_section_data objects
2862 // here.
2863
2864 void
2865 Output_section::do_write(Output_file* of)
2866 {
2867 gold_assert(!this->requires_postprocessing());
2868
2869 // If the target performs relaxation, we delay filler generation until now.
2870 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
2871
2872 off_t output_section_file_offset = this->offset();
2873 for (Fill_list::iterator p = this->fills_.begin();
2874 p != this->fills_.end();
2875 ++p)
2876 {
2877 std::string fill_data(parameters->target().code_fill(p->length()));
2878 of->write(output_section_file_offset + p->section_offset(),
2879 fill_data.data(), fill_data.size());
2880 }
2881
2882 off_t off = this->offset() + this->first_input_offset_;
2883 for (Input_section_list::iterator p = this->input_sections_.begin();
2884 p != this->input_sections_.end();
2885 ++p)
2886 {
2887 off_t aligned_off = align_address(off, p->addralign());
2888 if (this->generate_code_fills_at_write_ && (off != aligned_off))
2889 {
2890 size_t fill_len = aligned_off - off;
2891 std::string fill_data(parameters->target().code_fill(fill_len));
2892 of->write(off, fill_data.data(), fill_data.size());
2893 }
2894
2895 p->write(of);
2896 off = aligned_off + p->data_size();
2897 }
2898 }
2899
2900 // If a section requires postprocessing, create the buffer to use.
2901
2902 void
2903 Output_section::create_postprocessing_buffer()
2904 {
2905 gold_assert(this->requires_postprocessing());
2906
2907 if (this->postprocessing_buffer_ != NULL)
2908 return;
2909
2910 if (!this->input_sections_.empty())
2911 {
2912 off_t off = this->first_input_offset_;
2913 for (Input_section_list::iterator p = this->input_sections_.begin();
2914 p != this->input_sections_.end();
2915 ++p)
2916 {
2917 off = align_address(off, p->addralign());
2918 p->finalize_data_size();
2919 off += p->data_size();
2920 }
2921 this->set_current_data_size_for_child(off);
2922 }
2923
2924 off_t buffer_size = this->current_data_size_for_child();
2925 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2926 }
2927
2928 // Write all the data of an Output_section into the postprocessing
2929 // buffer. This is used for sections which require postprocessing,
2930 // such as compression. Input sections are handled by
2931 // Object::Relocate.
2932
2933 void
2934 Output_section::write_to_postprocessing_buffer()
2935 {
2936 gold_assert(this->requires_postprocessing());
2937
2938 // If the target performs relaxation, we delay filler generation until now.
2939 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
2940
2941 unsigned char* buffer = this->postprocessing_buffer();
2942 for (Fill_list::iterator p = this->fills_.begin();
2943 p != this->fills_.end();
2944 ++p)
2945 {
2946 std::string fill_data(parameters->target().code_fill(p->length()));
2947 memcpy(buffer + p->section_offset(), fill_data.data(),
2948 fill_data.size());
2949 }
2950
2951 off_t off = this->first_input_offset_;
2952 for (Input_section_list::iterator p = this->input_sections_.begin();
2953 p != this->input_sections_.end();
2954 ++p)
2955 {
2956 off_t aligned_off = align_address(off, p->addralign());
2957 if (this->generate_code_fills_at_write_ && (off != aligned_off))
2958 {
2959 size_t fill_len = aligned_off - off;
2960 std::string fill_data(parameters->target().code_fill(fill_len));
2961 memcpy(buffer + off, fill_data.data(), fill_data.size());
2962 }
2963
2964 p->write_to_buffer(buffer + aligned_off);
2965 off = aligned_off + p->data_size();
2966 }
2967 }
2968
2969 // Get the input sections for linker script processing. We leave
2970 // behind the Output_section_data entries. Note that this may be
2971 // slightly incorrect for merge sections. We will leave them behind,
2972 // but it is possible that the script says that they should follow
2973 // some other input sections, as in:
2974 // .rodata { *(.rodata) *(.rodata.cst*) }
2975 // For that matter, we don't handle this correctly:
2976 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2977 // With luck this will never matter.
2978
2979 uint64_t
2980 Output_section::get_input_sections(
2981 uint64_t address,
2982 const std::string& fill,
2983 std::list<Simple_input_section>* input_sections)
2984 {
2985 if (this->checkpoint_ != NULL
2986 && !this->checkpoint_->input_sections_saved())
2987 this->checkpoint_->save_input_sections();
2988
2989 // Invalidate the relaxed input section map.
2990 this->is_relaxed_input_section_map_valid_ = false;
2991
2992 uint64_t orig_address = address;
2993
2994 address = align_address(address, this->addralign());
2995
2996 Input_section_list remaining;
2997 for (Input_section_list::iterator p = this->input_sections_.begin();
2998 p != this->input_sections_.end();
2999 ++p)
3000 {
3001 if (p->is_input_section())
3002 input_sections->push_back(Simple_input_section(p->relobj(),
3003 p->shndx()));
3004 else if (p->is_relaxed_input_section())
3005 input_sections->push_back(
3006 Simple_input_section(p->relaxed_input_section()));
3007 else
3008 {
3009 uint64_t aligned_address = align_address(address, p->addralign());
3010 if (aligned_address != address && !fill.empty())
3011 {
3012 section_size_type length =
3013 convert_to_section_size_type(aligned_address - address);
3014 std::string this_fill;
3015 this_fill.reserve(length);
3016 while (this_fill.length() + fill.length() <= length)
3017 this_fill += fill;
3018 if (this_fill.length() < length)
3019 this_fill.append(fill, 0, length - this_fill.length());
3020
3021 Output_section_data* posd = new Output_data_const(this_fill, 0);
3022 remaining.push_back(Input_section(posd));
3023 }
3024 address = aligned_address;
3025
3026 remaining.push_back(*p);
3027
3028 p->finalize_data_size();
3029 address += p->data_size();
3030 }
3031 }
3032
3033 this->input_sections_.swap(remaining);
3034 this->first_input_offset_ = 0;
3035
3036 uint64_t data_size = address - orig_address;
3037 this->set_current_data_size_for_child(data_size);
3038 return data_size;
3039 }
3040
3041 // Add an input section from a script.
3042
3043 void
3044 Output_section::add_input_section_for_script(const Simple_input_section& sis,
3045 off_t data_size,
3046 uint64_t addralign)
3047 {
3048 if (addralign > this->addralign_)
3049 this->addralign_ = addralign;
3050
3051 off_t offset_in_section = this->current_data_size_for_child();
3052 off_t aligned_offset_in_section = align_address(offset_in_section,
3053 addralign);
3054
3055 this->set_current_data_size_for_child(aligned_offset_in_section
3056 + data_size);
3057
3058 Input_section is =
3059 (sis.is_relaxed_input_section()
3060 ? Input_section(sis.relaxed_input_section())
3061 : Input_section(sis.relobj(), sis.shndx(), data_size, addralign));
3062 this->input_sections_.push_back(is);
3063 }
3064
3065 //
3066
3067 void
3068 Output_section::save_states()
3069 {
3070 gold_assert(this->checkpoint_ == NULL);
3071 Checkpoint_output_section* checkpoint =
3072 new Checkpoint_output_section(this->addralign_, this->flags_,
3073 this->input_sections_,
3074 this->first_input_offset_,
3075 this->attached_input_sections_are_sorted_);
3076 this->checkpoint_ = checkpoint;
3077 gold_assert(this->fills_.empty());
3078 }
3079
3080 void
3081 Output_section::restore_states()
3082 {
3083 gold_assert(this->checkpoint_ != NULL);
3084 Checkpoint_output_section* checkpoint = this->checkpoint_;
3085
3086 this->addralign_ = checkpoint->addralign();
3087 this->flags_ = checkpoint->flags();
3088 this->first_input_offset_ = checkpoint->first_input_offset();
3089
3090 if (!checkpoint->input_sections_saved())
3091 {
3092 // If we have not copied the input sections, just resize it.
3093 size_t old_size = checkpoint->input_sections_size();
3094 gold_assert(this->input_sections_.size() >= old_size);
3095 this->input_sections_.resize(old_size);
3096 }
3097 else
3098 {
3099 // We need to copy the whole list. This is not efficient for
3100 // extremely large output with hundreads of thousands of input
3101 // objects. We may need to re-think how we should pass sections
3102 // to scripts.
3103 this->input_sections_ = *checkpoint->input_sections();
3104 }
3105
3106 this->attached_input_sections_are_sorted_ =
3107 checkpoint->attached_input_sections_are_sorted();
3108
3109 // Simply invalidate the relaxed input section map since we do not keep
3110 // track of it.
3111 this->is_relaxed_input_section_map_valid_ = false;
3112 }
3113
3114 // Print to the map file.
3115
3116 void
3117 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3118 {
3119 mapfile->print_output_section(this);
3120
3121 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3122 p != this->input_sections_.end();
3123 ++p)
3124 p->print_to_mapfile(mapfile);
3125 }
3126
3127 // Print stats for merge sections to stderr.
3128
3129 void
3130 Output_section::print_merge_stats()
3131 {
3132 Input_section_list::iterator p;
3133 for (p = this->input_sections_.begin();
3134 p != this->input_sections_.end();
3135 ++p)
3136 p->print_merge_stats(this->name_);
3137 }
3138
3139 // Output segment methods.
3140
3141 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3142 : output_data_(),
3143 output_bss_(),
3144 vaddr_(0),
3145 paddr_(0),
3146 memsz_(0),
3147 max_align_(0),
3148 min_p_align_(0),
3149 offset_(0),
3150 filesz_(0),
3151 type_(type),
3152 flags_(flags),
3153 is_max_align_known_(false),
3154 are_addresses_set_(false),
3155 is_large_data_segment_(false)
3156 {
3157 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3158 // the flags.
3159 if (type == elfcpp::PT_TLS)
3160 this->flags_ = elfcpp::PF_R;
3161 }
3162
3163 // Add an Output_section to an Output_segment.
3164
3165 void
3166 Output_segment::add_output_section(Output_section* os,
3167 elfcpp::Elf_Word seg_flags,
3168 bool do_sort)
3169 {
3170 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3171 gold_assert(!this->is_max_align_known_);
3172 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3173 gold_assert(this->type() == elfcpp::PT_LOAD || !do_sort);
3174
3175 this->update_flags_for_output_section(seg_flags);
3176
3177 Output_segment::Output_data_list* pdl;
3178 if (os->type() == elfcpp::SHT_NOBITS)
3179 pdl = &this->output_bss_;
3180 else
3181 pdl = &this->output_data_;
3182
3183 // Note that while there may be many input sections in an output
3184 // section, there are normally only a few output sections in an
3185 // output segment. The loops below are expected to be fast.
3186
3187 // So that PT_NOTE segments will work correctly, we need to ensure
3188 // that all SHT_NOTE sections are adjacent.
3189 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
3190 {
3191 Output_segment::Output_data_list::iterator p = pdl->end();
3192 do
3193 {
3194 --p;
3195 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
3196 {
3197 ++p;
3198 pdl->insert(p, os);
3199 return;
3200 }
3201 }
3202 while (p != pdl->begin());
3203 }
3204
3205 // Similarly, so that PT_TLS segments will work, we need to group
3206 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
3207 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
3208 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
3209 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
3210 // and the PT_TLS segment; we do this grouping only for the PT_LOAD
3211 // segment.
3212 if (this->type_ != elfcpp::PT_TLS
3213 && (os->flags() & elfcpp::SHF_TLS) != 0)
3214 {
3215 pdl = &this->output_data_;
3216 if (!pdl->empty())
3217 {
3218 bool nobits = os->type() == elfcpp::SHT_NOBITS;
3219 bool sawtls = false;
3220 Output_segment::Output_data_list::iterator p = pdl->end();
3221 gold_assert(p != pdl->begin());
3222 do
3223 {
3224 --p;
3225 bool insert;
3226 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3227 {
3228 sawtls = true;
3229 // Put a NOBITS section after the first TLS section.
3230 // Put a PROGBITS section after the first
3231 // TLS/PROGBITS section.
3232 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
3233 }
3234 else
3235 {
3236 // If we've gone past the TLS sections, but we've
3237 // seen a TLS section, then we need to insert this
3238 // section now.
3239 insert = sawtls;
3240 }
3241
3242 if (insert)
3243 {
3244 ++p;
3245 pdl->insert(p, os);
3246 return;
3247 }
3248 }
3249 while (p != pdl->begin());
3250 }
3251
3252 // There are no TLS sections yet; put this one at the requested
3253 // location in the section list.
3254 }
3255
3256 if (do_sort)
3257 {
3258 // For the PT_GNU_RELRO segment, we need to group relro
3259 // sections, and we need to put them before any non-relro
3260 // sections. Any relro local sections go before relro non-local
3261 // sections. One section may be marked as the last relro
3262 // section.
3263 if (os->is_relro())
3264 {
3265 gold_assert(pdl == &this->output_data_);
3266 Output_segment::Output_data_list::iterator p;
3267 for (p = pdl->begin(); p != pdl->end(); ++p)
3268 {
3269 if (!(*p)->is_section())
3270 break;
3271
3272 Output_section* pos = (*p)->output_section();
3273 if (!pos->is_relro()
3274 || (os->is_relro_local() && !pos->is_relro_local())
3275 || (!os->is_last_relro() && pos->is_last_relro()))
3276 break;
3277 }
3278
3279 pdl->insert(p, os);
3280 return;
3281 }
3282
3283 // One section may be marked as the first section which follows
3284 // the relro sections.
3285 if (os->is_first_non_relro())
3286 {
3287 gold_assert(pdl == &this->output_data_);
3288 Output_segment::Output_data_list::iterator p;
3289 for (p = pdl->begin(); p != pdl->end(); ++p)
3290 {
3291 if (!(*p)->is_section())
3292 break;
3293
3294 Output_section* pos = (*p)->output_section();
3295 if (!pos->is_relro())
3296 break;
3297 }
3298
3299 pdl->insert(p, os);
3300 return;
3301 }
3302 }
3303
3304 // Small data sections go at the end of the list of data sections.
3305 // If OS is not small, and there are small sections, we have to
3306 // insert it before the first small section.
3307 if (os->type() != elfcpp::SHT_NOBITS
3308 && !os->is_small_section()
3309 && !pdl->empty()
3310 && pdl->back()->is_section()
3311 && pdl->back()->output_section()->is_small_section())
3312 {
3313 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3314 p != pdl->end();
3315 ++p)
3316 {
3317 if ((*p)->is_section()
3318 && (*p)->output_section()->is_small_section())
3319 {
3320 pdl->insert(p, os);
3321 return;
3322 }
3323 }
3324 gold_unreachable();
3325 }
3326
3327 // A small BSS section goes at the start of the BSS sections, after
3328 // other small BSS sections.
3329 if (os->type() == elfcpp::SHT_NOBITS && os->is_small_section())
3330 {
3331 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3332 p != pdl->end();
3333 ++p)
3334 {
3335 if (!(*p)->is_section()
3336 || !(*p)->output_section()->is_small_section())
3337 {
3338 pdl->insert(p, os);
3339 return;
3340 }
3341 }
3342 }
3343
3344 // A large BSS section goes at the end of the BSS sections, which
3345 // means that one that is not large must come before the first large
3346 // one.
3347 if (os->type() == elfcpp::SHT_NOBITS
3348 && !os->is_large_section()
3349 && !pdl->empty()
3350 && pdl->back()->is_section()
3351 && pdl->back()->output_section()->is_large_section())
3352 {
3353 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3354 p != pdl->end();
3355 ++p)
3356 {
3357 if ((*p)->is_section()
3358 && (*p)->output_section()->is_large_section())
3359 {
3360 pdl->insert(p, os);
3361 return;
3362 }
3363 }
3364 gold_unreachable();
3365 }
3366
3367 // We do some further output section sorting in order to make the
3368 // generated program run more efficiently. We should only do this
3369 // when not using a linker script, so it is controled by the DO_SORT
3370 // parameter.
3371 if (do_sort)
3372 {
3373 // FreeBSD requires the .interp section to be in the first page
3374 // of the executable. That is a more efficient location anyhow
3375 // for any OS, since it means that the kernel will have the data
3376 // handy after it reads the program headers.
3377 if (os->is_interp() && !pdl->empty())
3378 {
3379 pdl->insert(pdl->begin(), os);
3380 return;
3381 }
3382
3383 // Put loadable non-writable notes immediately after the .interp
3384 // sections, so that the PT_NOTE segment is on the first page of
3385 // the executable.
3386 if (os->type() == elfcpp::SHT_NOTE
3387 && (os->flags() & elfcpp::SHF_WRITE) == 0
3388 && !pdl->empty())
3389 {
3390 Output_segment::Output_data_list::iterator p = pdl->begin();
3391 if ((*p)->is_section() && (*p)->output_section()->is_interp())
3392 ++p;
3393 pdl->insert(p, os);
3394 return;
3395 }
3396
3397 // If this section is used by the dynamic linker, and it is not
3398 // writable, then put it first, after the .interp section and
3399 // any loadable notes. This makes it more likely that the
3400 // dynamic linker will have to read less data from the disk.
3401 if (os->is_dynamic_linker_section()
3402 && !pdl->empty()
3403 && (os->flags() & elfcpp::SHF_WRITE) == 0)
3404 {
3405 bool is_reloc = (os->type() == elfcpp::SHT_REL
3406 || os->type() == elfcpp::SHT_RELA);
3407 Output_segment::Output_data_list::iterator p = pdl->begin();
3408 while (p != pdl->end()
3409 && (*p)->is_section()
3410 && ((*p)->output_section()->is_dynamic_linker_section()
3411 || (*p)->output_section()->type() == elfcpp::SHT_NOTE))
3412 {
3413 // Put reloc sections after the other ones. Putting the
3414 // dynamic reloc sections first confuses BFD, notably
3415 // objcopy and strip.
3416 if (!is_reloc
3417 && ((*p)->output_section()->type() == elfcpp::SHT_REL
3418 || (*p)->output_section()->type() == elfcpp::SHT_RELA))
3419 break;
3420 ++p;
3421 }
3422 pdl->insert(p, os);
3423 return;
3424 }
3425 }
3426
3427 // If there were no constraints on the output section, just add it
3428 // to the end of the list.
3429 pdl->push_back(os);
3430 }
3431
3432 // Remove an Output_section from this segment. It is an error if it
3433 // is not present.
3434
3435 void
3436 Output_segment::remove_output_section(Output_section* os)
3437 {
3438 // We only need this for SHT_PROGBITS.
3439 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
3440 for (Output_data_list::iterator p = this->output_data_.begin();
3441 p != this->output_data_.end();
3442 ++p)
3443 {
3444 if (*p == os)
3445 {
3446 this->output_data_.erase(p);
3447 return;
3448 }
3449 }
3450 gold_unreachable();
3451 }
3452
3453 // Add an Output_data (which need not be an Output_section) to the
3454 // start of a segment.
3455
3456 void
3457 Output_segment::add_initial_output_data(Output_data* od)
3458 {
3459 gold_assert(!this->is_max_align_known_);
3460 this->output_data_.push_front(od);
3461 }
3462
3463 // Return whether the first data section is a relro section.
3464
3465 bool
3466 Output_segment::is_first_section_relro() const
3467 {
3468 return (!this->output_data_.empty()
3469 && this->output_data_.front()->is_section()
3470 && this->output_data_.front()->output_section()->is_relro());
3471 }
3472
3473 // Return the maximum alignment of the Output_data in Output_segment.
3474
3475 uint64_t
3476 Output_segment::maximum_alignment()
3477 {
3478 if (!this->is_max_align_known_)
3479 {
3480 uint64_t addralign;
3481
3482 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
3483 if (addralign > this->max_align_)
3484 this->max_align_ = addralign;
3485
3486 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
3487 if (addralign > this->max_align_)
3488 this->max_align_ = addralign;
3489
3490 this->is_max_align_known_ = true;
3491 }
3492
3493 return this->max_align_;
3494 }
3495
3496 // Return the maximum alignment of a list of Output_data.
3497
3498 uint64_t
3499 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
3500 {
3501 uint64_t ret = 0;
3502 for (Output_data_list::const_iterator p = pdl->begin();
3503 p != pdl->end();
3504 ++p)
3505 {
3506 uint64_t addralign = (*p)->addralign();
3507 if (addralign > ret)
3508 ret = addralign;
3509 }
3510 return ret;
3511 }
3512
3513 // Return the number of dynamic relocs applied to this segment.
3514
3515 unsigned int
3516 Output_segment::dynamic_reloc_count() const
3517 {
3518 return (this->dynamic_reloc_count_list(&this->output_data_)
3519 + this->dynamic_reloc_count_list(&this->output_bss_));
3520 }
3521
3522 // Return the number of dynamic relocs applied to an Output_data_list.
3523
3524 unsigned int
3525 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
3526 {
3527 unsigned int count = 0;
3528 for (Output_data_list::const_iterator p = pdl->begin();
3529 p != pdl->end();
3530 ++p)
3531 count += (*p)->dynamic_reloc_count();
3532 return count;
3533 }
3534
3535 // Set the section addresses for an Output_segment. If RESET is true,
3536 // reset the addresses first. ADDR is the address and *POFF is the
3537 // file offset. Set the section indexes starting with *PSHNDX.
3538 // Return the address of the immediately following segment. Update
3539 // *POFF and *PSHNDX.
3540
3541 uint64_t
3542 Output_segment::set_section_addresses(const Layout* layout, bool reset,
3543 uint64_t addr,
3544 unsigned int increase_relro,
3545 off_t* poff,
3546 unsigned int* pshndx)
3547 {
3548 gold_assert(this->type_ == elfcpp::PT_LOAD);
3549
3550 off_t orig_off = *poff;
3551
3552 // If we have relro sections, we need to pad forward now so that the
3553 // relro sections plus INCREASE_RELRO end on a common page boundary.
3554 if (parameters->options().relro()
3555 && this->is_first_section_relro()
3556 && (!this->are_addresses_set_ || reset))
3557 {
3558 uint64_t relro_size = 0;
3559 off_t off = *poff;
3560 for (Output_data_list::iterator p = this->output_data_.begin();
3561 p != this->output_data_.end();
3562 ++p)
3563 {
3564 if (!(*p)->is_section())
3565 break;
3566 Output_section* pos = (*p)->output_section();
3567 if (!pos->is_relro())
3568 break;
3569 gold_assert(!(*p)->is_section_flag_set(elfcpp::SHF_TLS));
3570 if ((*p)->is_address_valid())
3571 relro_size += (*p)->data_size();
3572 else
3573 {
3574 // FIXME: This could be faster.
3575 (*p)->set_address_and_file_offset(addr + relro_size,
3576 off + relro_size);
3577 relro_size += (*p)->data_size();
3578 (*p)->reset_address_and_file_offset();
3579 }
3580 }
3581 relro_size += increase_relro;
3582
3583 uint64_t page_align = parameters->target().common_pagesize();
3584
3585 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
3586 uint64_t desired_align = page_align - (relro_size % page_align);
3587 if (desired_align < *poff % page_align)
3588 *poff += page_align - *poff % page_align;
3589 *poff += desired_align - *poff % page_align;
3590 addr += *poff - orig_off;
3591 orig_off = *poff;
3592 }
3593
3594 if (!reset && this->are_addresses_set_)
3595 {
3596 gold_assert(this->paddr_ == addr);
3597 addr = this->vaddr_;
3598 }
3599 else
3600 {
3601 this->vaddr_ = addr;
3602 this->paddr_ = addr;
3603 this->are_addresses_set_ = true;
3604 }
3605
3606 bool in_tls = false;
3607
3608 this->offset_ = orig_off;
3609
3610 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
3611 addr, poff, pshndx, &in_tls);
3612 this->filesz_ = *poff - orig_off;
3613
3614 off_t off = *poff;
3615
3616 uint64_t ret = this->set_section_list_addresses(layout, reset,
3617 &this->output_bss_,
3618 addr, poff, pshndx,
3619 &in_tls);
3620
3621 // If the last section was a TLS section, align upward to the
3622 // alignment of the TLS segment, so that the overall size of the TLS
3623 // segment is aligned.
3624 if (in_tls)
3625 {
3626 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
3627 *poff = align_address(*poff, segment_align);
3628 }
3629
3630 this->memsz_ = *poff - orig_off;
3631
3632 // Ignore the file offset adjustments made by the BSS Output_data
3633 // objects.
3634 *poff = off;
3635
3636 return ret;
3637 }
3638
3639 // Set the addresses and file offsets in a list of Output_data
3640 // structures.
3641
3642 uint64_t
3643 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
3644 Output_data_list* pdl,
3645 uint64_t addr, off_t* poff,
3646 unsigned int* pshndx,
3647 bool* in_tls)
3648 {
3649 off_t startoff = *poff;
3650
3651 off_t off = startoff;
3652 for (Output_data_list::iterator p = pdl->begin();
3653 p != pdl->end();
3654 ++p)
3655 {
3656 if (reset)
3657 (*p)->reset_address_and_file_offset();
3658
3659 // When using a linker script the section will most likely
3660 // already have an address.
3661 if (!(*p)->is_address_valid())
3662 {
3663 uint64_t align = (*p)->addralign();
3664
3665 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3666 {
3667 // Give the first TLS section the alignment of the
3668 // entire TLS segment. Otherwise the TLS segment as a
3669 // whole may be misaligned.
3670 if (!*in_tls)
3671 {
3672 Output_segment* tls_segment = layout->tls_segment();
3673 gold_assert(tls_segment != NULL);
3674 uint64_t segment_align = tls_segment->maximum_alignment();
3675 gold_assert(segment_align >= align);
3676 align = segment_align;
3677
3678 *in_tls = true;
3679 }
3680 }
3681 else
3682 {
3683 // If this is the first section after the TLS segment,
3684 // align it to at least the alignment of the TLS
3685 // segment, so that the size of the overall TLS segment
3686 // is aligned.
3687 if (*in_tls)
3688 {
3689 uint64_t segment_align =
3690 layout->tls_segment()->maximum_alignment();
3691 if (segment_align > align)
3692 align = segment_align;
3693
3694 *in_tls = false;
3695 }
3696 }
3697
3698 off = align_address(off, align);
3699 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
3700 }
3701 else
3702 {
3703 // The script may have inserted a skip forward, but it
3704 // better not have moved backward.
3705 if ((*p)->address() >= addr + (off - startoff))
3706 off += (*p)->address() - (addr + (off - startoff));
3707 else
3708 {
3709 if (!layout->script_options()->saw_sections_clause())
3710 gold_unreachable();
3711 else
3712 {
3713 Output_section* os = (*p)->output_section();
3714
3715 // Cast to unsigned long long to avoid format warnings.
3716 unsigned long long previous_dot =
3717 static_cast<unsigned long long>(addr + (off - startoff));
3718 unsigned long long dot =
3719 static_cast<unsigned long long>((*p)->address());
3720
3721 if (os == NULL)
3722 gold_error(_("dot moves backward in linker script "
3723 "from 0x%llx to 0x%llx"), previous_dot, dot);
3724 else
3725 gold_error(_("address of section '%s' moves backward "
3726 "from 0x%llx to 0x%llx"),
3727 os->name(), previous_dot, dot);
3728 }
3729 }
3730 (*p)->set_file_offset(off);
3731 (*p)->finalize_data_size();
3732 }
3733
3734 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
3735 // section. Such a section does not affect the size of a
3736 // PT_LOAD segment.
3737 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
3738 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
3739 off += (*p)->data_size();
3740
3741 if ((*p)->is_section())
3742 {
3743 (*p)->set_out_shndx(*pshndx);
3744 ++*pshndx;
3745 }
3746 }
3747
3748 *poff = off;
3749 return addr + (off - startoff);
3750 }
3751
3752 // For a non-PT_LOAD segment, set the offset from the sections, if
3753 // any. Add INCREASE to the file size and the memory size.
3754
3755 void
3756 Output_segment::set_offset(unsigned int increase)
3757 {
3758 gold_assert(this->type_ != elfcpp::PT_LOAD);
3759
3760 gold_assert(!this->are_addresses_set_);
3761
3762 if (this->output_data_.empty() && this->output_bss_.empty())
3763 {
3764 gold_assert(increase == 0);
3765 this->vaddr_ = 0;
3766 this->paddr_ = 0;
3767 this->are_addresses_set_ = true;
3768 this->memsz_ = 0;
3769 this->min_p_align_ = 0;
3770 this->offset_ = 0;
3771 this->filesz_ = 0;
3772 return;
3773 }
3774
3775 const Output_data* first;
3776 if (this->output_data_.empty())
3777 first = this->output_bss_.front();
3778 else
3779 first = this->output_data_.front();
3780 this->vaddr_ = first->address();
3781 this->paddr_ = (first->has_load_address()
3782 ? first->load_address()
3783 : this->vaddr_);
3784 this->are_addresses_set_ = true;
3785 this->offset_ = first->offset();
3786
3787 if (this->output_data_.empty())
3788 this->filesz_ = 0;
3789 else
3790 {
3791 const Output_data* last_data = this->output_data_.back();
3792 this->filesz_ = (last_data->address()
3793 + last_data->data_size()
3794 - this->vaddr_);
3795 }
3796
3797 const Output_data* last;
3798 if (this->output_bss_.empty())
3799 last = this->output_data_.back();
3800 else
3801 last = this->output_bss_.back();
3802 this->memsz_ = (last->address()
3803 + last->data_size()
3804 - this->vaddr_);
3805
3806 this->filesz_ += increase;
3807 this->memsz_ += increase;
3808
3809 // If this is a TLS segment, align the memory size. The code in
3810 // set_section_list ensures that the section after the TLS segment
3811 // is aligned to give us room.
3812 if (this->type_ == elfcpp::PT_TLS)
3813 {
3814 uint64_t segment_align = this->maximum_alignment();
3815 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3816 this->memsz_ = align_address(this->memsz_, segment_align);
3817 }
3818 }
3819
3820 // Set the TLS offsets of the sections in the PT_TLS segment.
3821
3822 void
3823 Output_segment::set_tls_offsets()
3824 {
3825 gold_assert(this->type_ == elfcpp::PT_TLS);
3826
3827 for (Output_data_list::iterator p = this->output_data_.begin();
3828 p != this->output_data_.end();
3829 ++p)
3830 (*p)->set_tls_offset(this->vaddr_);
3831
3832 for (Output_data_list::iterator p = this->output_bss_.begin();
3833 p != this->output_bss_.end();
3834 ++p)
3835 (*p)->set_tls_offset(this->vaddr_);
3836 }
3837
3838 // Return the address of the first section.
3839
3840 uint64_t
3841 Output_segment::first_section_load_address() const
3842 {
3843 for (Output_data_list::const_iterator p = this->output_data_.begin();
3844 p != this->output_data_.end();
3845 ++p)
3846 if ((*p)->is_section())
3847 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3848
3849 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3850 p != this->output_bss_.end();
3851 ++p)
3852 if ((*p)->is_section())
3853 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3854
3855 gold_unreachable();
3856 }
3857
3858 // Return the number of Output_sections in an Output_segment.
3859
3860 unsigned int
3861 Output_segment::output_section_count() const
3862 {
3863 return (this->output_section_count_list(&this->output_data_)
3864 + this->output_section_count_list(&this->output_bss_));
3865 }
3866
3867 // Return the number of Output_sections in an Output_data_list.
3868
3869 unsigned int
3870 Output_segment::output_section_count_list(const Output_data_list* pdl) const
3871 {
3872 unsigned int count = 0;
3873 for (Output_data_list::const_iterator p = pdl->begin();
3874 p != pdl->end();
3875 ++p)
3876 {
3877 if ((*p)->is_section())
3878 ++count;
3879 }
3880 return count;
3881 }
3882
3883 // Return the section attached to the list segment with the lowest
3884 // load address. This is used when handling a PHDRS clause in a
3885 // linker script.
3886
3887 Output_section*
3888 Output_segment::section_with_lowest_load_address() const
3889 {
3890 Output_section* found = NULL;
3891 uint64_t found_lma = 0;
3892 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3893
3894 Output_section* found_data = found;
3895 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3896 if (found != found_data && found_data != NULL)
3897 {
3898 gold_error(_("nobits section %s may not precede progbits section %s "
3899 "in same segment"),
3900 found->name(), found_data->name());
3901 return NULL;
3902 }
3903
3904 return found;
3905 }
3906
3907 // Look through a list for a section with a lower load address.
3908
3909 void
3910 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3911 Output_section** found,
3912 uint64_t* found_lma) const
3913 {
3914 for (Output_data_list::const_iterator p = pdl->begin();
3915 p != pdl->end();
3916 ++p)
3917 {
3918 if (!(*p)->is_section())
3919 continue;
3920 Output_section* os = static_cast<Output_section*>(*p);
3921 uint64_t lma = (os->has_load_address()
3922 ? os->load_address()
3923 : os->address());
3924 if (*found == NULL || lma < *found_lma)
3925 {
3926 *found = os;
3927 *found_lma = lma;
3928 }
3929 }
3930 }
3931
3932 // Write the segment data into *OPHDR.
3933
3934 template<int size, bool big_endian>
3935 void
3936 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
3937 {
3938 ophdr->put_p_type(this->type_);
3939 ophdr->put_p_offset(this->offset_);
3940 ophdr->put_p_vaddr(this->vaddr_);
3941 ophdr->put_p_paddr(this->paddr_);
3942 ophdr->put_p_filesz(this->filesz_);
3943 ophdr->put_p_memsz(this->memsz_);
3944 ophdr->put_p_flags(this->flags_);
3945 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
3946 }
3947
3948 // Write the section headers into V.
3949
3950 template<int size, bool big_endian>
3951 unsigned char*
3952 Output_segment::write_section_headers(const Layout* layout,
3953 const Stringpool* secnamepool,
3954 unsigned char* v,
3955 unsigned int *pshndx) const
3956 {
3957 // Every section that is attached to a segment must be attached to a
3958 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3959 // segments.
3960 if (this->type_ != elfcpp::PT_LOAD)
3961 return v;
3962
3963 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3964 &this->output_data_,
3965 v, pshndx);
3966 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3967 &this->output_bss_,
3968 v, pshndx);
3969 return v;
3970 }
3971
3972 template<int size, bool big_endian>
3973 unsigned char*
3974 Output_segment::write_section_headers_list(const Layout* layout,
3975 const Stringpool* secnamepool,
3976 const Output_data_list* pdl,
3977 unsigned char* v,
3978 unsigned int* pshndx) const
3979 {
3980 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3981 for (Output_data_list::const_iterator p = pdl->begin();
3982 p != pdl->end();
3983 ++p)
3984 {
3985 if ((*p)->is_section())
3986 {
3987 const Output_section* ps = static_cast<const Output_section*>(*p);
3988 gold_assert(*pshndx == ps->out_shndx());
3989 elfcpp::Shdr_write<size, big_endian> oshdr(v);
3990 ps->write_header(layout, secnamepool, &oshdr);
3991 v += shdr_size;
3992 ++*pshndx;
3993 }
3994 }
3995 return v;
3996 }
3997
3998 // Print the output sections to the map file.
3999
4000 void
4001 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4002 {
4003 if (this->type() != elfcpp::PT_LOAD)
4004 return;
4005 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
4006 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
4007 }
4008
4009 // Print an output section list to the map file.
4010
4011 void
4012 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4013 const Output_data_list* pdl) const
4014 {
4015 for (Output_data_list::const_iterator p = pdl->begin();
4016 p != pdl->end();
4017 ++p)
4018 (*p)->print_to_mapfile(mapfile);
4019 }
4020
4021 // Output_file methods.
4022
4023 Output_file::Output_file(const char* name)
4024 : name_(name),
4025 o_(-1),
4026 file_size_(0),
4027 base_(NULL),
4028 map_is_anonymous_(false),
4029 is_temporary_(false)
4030 {
4031 }
4032
4033 // Try to open an existing file. Returns false if the file doesn't
4034 // exist, has a size of 0 or can't be mmapped.
4035
4036 bool
4037 Output_file::open_for_modification()
4038 {
4039 // The name "-" means "stdout".
4040 if (strcmp(this->name_, "-") == 0)
4041 return false;
4042
4043 // Don't bother opening files with a size of zero.
4044 struct stat s;
4045 if (::stat(this->name_, &s) != 0 || s.st_size == 0)
4046 return false;
4047
4048 int o = open_descriptor(-1, this->name_, O_RDWR, 0);
4049 if (o < 0)
4050 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4051 this->o_ = o;
4052 this->file_size_ = s.st_size;
4053
4054 // If the file can't be mmapped, copying the content to an anonymous
4055 // map will probably negate the performance benefits of incremental
4056 // linking. This could be helped by using views and loading only
4057 // the necessary parts, but this is not supported as of now.
4058 if (!this->map_no_anonymous())
4059 {
4060 release_descriptor(o, true);
4061 this->o_ = -1;
4062 this->file_size_ = 0;
4063 return false;
4064 }
4065
4066 return true;
4067 }
4068
4069 // Open the output file.
4070
4071 void
4072 Output_file::open(off_t file_size)
4073 {
4074 this->file_size_ = file_size;
4075
4076 // Unlink the file first; otherwise the open() may fail if the file
4077 // is busy (e.g. it's an executable that's currently being executed).
4078 //
4079 // However, the linker may be part of a system where a zero-length
4080 // file is created for it to write to, with tight permissions (gcc
4081 // 2.95 did something like this). Unlinking the file would work
4082 // around those permission controls, so we only unlink if the file
4083 // has a non-zero size. We also unlink only regular files to avoid
4084 // trouble with directories/etc.
4085 //
4086 // If we fail, continue; this command is merely a best-effort attempt
4087 // to improve the odds for open().
4088
4089 // We let the name "-" mean "stdout"
4090 if (!this->is_temporary_)
4091 {
4092 if (strcmp(this->name_, "-") == 0)
4093 this->o_ = STDOUT_FILENO;
4094 else
4095 {
4096 struct stat s;
4097 if (::stat(this->name_, &s) == 0
4098 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4099 {
4100 if (s.st_size != 0)
4101 ::unlink(this->name_);
4102 else if (!parameters->options().relocatable())
4103 {
4104 // If we don't unlink the existing file, add execute
4105 // permission where read permissions already exist
4106 // and where the umask permits.
4107 int mask = ::umask(0);
4108 ::umask(mask);
4109 s.st_mode |= (s.st_mode & 0444) >> 2;
4110 ::chmod(this->name_, s.st_mode & ~mask);
4111 }
4112 }
4113
4114 int mode = parameters->options().relocatable() ? 0666 : 0777;
4115 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4116 mode);
4117 if (o < 0)
4118 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4119 this->o_ = o;
4120 }
4121 }
4122
4123 this->map();
4124 }
4125
4126 // Resize the output file.
4127
4128 void
4129 Output_file::resize(off_t file_size)
4130 {
4131 // If the mmap is mapping an anonymous memory buffer, this is easy:
4132 // just mremap to the new size. If it's mapping to a file, we want
4133 // to unmap to flush to the file, then remap after growing the file.
4134 if (this->map_is_anonymous_)
4135 {
4136 void* base = ::mremap(this->base_, this->file_size_, file_size,
4137 MREMAP_MAYMOVE);
4138 if (base == MAP_FAILED)
4139 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
4140 this->base_ = static_cast<unsigned char*>(base);
4141 this->file_size_ = file_size;
4142 }
4143 else
4144 {
4145 this->unmap();
4146 this->file_size_ = file_size;
4147 if (!this->map_no_anonymous())
4148 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4149 }
4150 }
4151
4152 // Map an anonymous block of memory which will later be written to the
4153 // file. Return whether the map succeeded.
4154
4155 bool
4156 Output_file::map_anonymous()
4157 {
4158 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4159 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4160 if (base != MAP_FAILED)
4161 {
4162 this->map_is_anonymous_ = true;
4163 this->base_ = static_cast<unsigned char*>(base);
4164 return true;
4165 }
4166 return false;
4167 }
4168
4169 // Map the file into memory. Return whether the mapping succeeded.
4170
4171 bool
4172 Output_file::map_no_anonymous()
4173 {
4174 const int o = this->o_;
4175
4176 // If the output file is not a regular file, don't try to mmap it;
4177 // instead, we'll mmap a block of memory (an anonymous buffer), and
4178 // then later write the buffer to the file.
4179 void* base;
4180 struct stat statbuf;
4181 if (o == STDOUT_FILENO || o == STDERR_FILENO
4182 || ::fstat(o, &statbuf) != 0
4183 || !S_ISREG(statbuf.st_mode)
4184 || this->is_temporary_)
4185 return false;
4186
4187 // Ensure that we have disk space available for the file. If we
4188 // don't do this, it is possible that we will call munmap, close,
4189 // and exit with dirty buffers still in the cache with no assigned
4190 // disk blocks. If the disk is out of space at that point, the
4191 // output file will wind up incomplete, but we will have already
4192 // exited. The alternative to fallocate would be to use fdatasync,
4193 // but that would be a more significant performance hit.
4194 if (::posix_fallocate(o, 0, this->file_size_) < 0)
4195 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
4196
4197 // Map the file into memory.
4198 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4199 MAP_SHARED, o, 0);
4200
4201 // The mmap call might fail because of file system issues: the file
4202 // system might not support mmap at all, or it might not support
4203 // mmap with PROT_WRITE.
4204 if (base == MAP_FAILED)
4205 return false;
4206
4207 this->map_is_anonymous_ = false;
4208 this->base_ = static_cast<unsigned char*>(base);
4209 return true;
4210 }
4211
4212 // Map the file into memory.
4213
4214 void
4215 Output_file::map()
4216 {
4217 if (this->map_no_anonymous())
4218 return;
4219
4220 // The mmap call might fail because of file system issues: the file
4221 // system might not support mmap at all, or it might not support
4222 // mmap with PROT_WRITE. I'm not sure which errno values we will
4223 // see in all cases, so if the mmap fails for any reason and we
4224 // don't care about file contents, try for an anonymous map.
4225 if (this->map_anonymous())
4226 return;
4227
4228 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4229 this->name_, static_cast<unsigned long>(this->file_size_),
4230 strerror(errno));
4231 }
4232
4233 // Unmap the file from memory.
4234
4235 void
4236 Output_file::unmap()
4237 {
4238 if (::munmap(this->base_, this->file_size_) < 0)
4239 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
4240 this->base_ = NULL;
4241 }
4242
4243 // Close the output file.
4244
4245 void
4246 Output_file::close()
4247 {
4248 // If the map isn't file-backed, we need to write it now.
4249 if (this->map_is_anonymous_ && !this->is_temporary_)
4250 {
4251 size_t bytes_to_write = this->file_size_;
4252 size_t offset = 0;
4253 while (bytes_to_write > 0)
4254 {
4255 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
4256 bytes_to_write);
4257 if (bytes_written == 0)
4258 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
4259 else if (bytes_written < 0)
4260 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
4261 else
4262 {
4263 bytes_to_write -= bytes_written;
4264 offset += bytes_written;
4265 }
4266 }
4267 }
4268 this->unmap();
4269
4270 // We don't close stdout or stderr
4271 if (this->o_ != STDOUT_FILENO
4272 && this->o_ != STDERR_FILENO
4273 && !this->is_temporary_)
4274 if (::close(this->o_) < 0)
4275 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
4276 this->o_ = -1;
4277 }
4278
4279 // Instantiate the templates we need. We could use the configure
4280 // script to restrict this to only the ones for implemented targets.
4281
4282 #ifdef HAVE_TARGET_32_LITTLE
4283 template
4284 off_t
4285 Output_section::add_input_section<32, false>(
4286 Sized_relobj<32, false>* object,
4287 unsigned int shndx,
4288 const char* secname,
4289 const elfcpp::Shdr<32, false>& shdr,
4290 unsigned int reloc_shndx,
4291 bool have_sections_script);
4292 #endif
4293
4294 #ifdef HAVE_TARGET_32_BIG
4295 template
4296 off_t
4297 Output_section::add_input_section<32, true>(
4298 Sized_relobj<32, true>* object,
4299 unsigned int shndx,
4300 const char* secname,
4301 const elfcpp::Shdr<32, true>& shdr,
4302 unsigned int reloc_shndx,
4303 bool have_sections_script);
4304 #endif
4305
4306 #ifdef HAVE_TARGET_64_LITTLE
4307 template
4308 off_t
4309 Output_section::add_input_section<64, false>(
4310 Sized_relobj<64, false>* object,
4311 unsigned int shndx,
4312 const char* secname,
4313 const elfcpp::Shdr<64, false>& shdr,
4314 unsigned int reloc_shndx,
4315 bool have_sections_script);
4316 #endif
4317
4318 #ifdef HAVE_TARGET_64_BIG
4319 template
4320 off_t
4321 Output_section::add_input_section<64, true>(
4322 Sized_relobj<64, true>* object,
4323 unsigned int shndx,
4324 const char* secname,
4325 const elfcpp::Shdr<64, true>& shdr,
4326 unsigned int reloc_shndx,
4327 bool have_sections_script);
4328 #endif
4329
4330 #ifdef HAVE_TARGET_32_LITTLE
4331 template
4332 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
4333 #endif
4334
4335 #ifdef HAVE_TARGET_32_BIG
4336 template
4337 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
4338 #endif
4339
4340 #ifdef HAVE_TARGET_64_LITTLE
4341 template
4342 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
4343 #endif
4344
4345 #ifdef HAVE_TARGET_64_BIG
4346 template
4347 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
4348 #endif
4349
4350 #ifdef HAVE_TARGET_32_LITTLE
4351 template
4352 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
4353 #endif
4354
4355 #ifdef HAVE_TARGET_32_BIG
4356 template
4357 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
4358 #endif
4359
4360 #ifdef HAVE_TARGET_64_LITTLE
4361 template
4362 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
4363 #endif
4364
4365 #ifdef HAVE_TARGET_64_BIG
4366 template
4367 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
4368 #endif
4369
4370 #ifdef HAVE_TARGET_32_LITTLE
4371 template
4372 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
4373 #endif
4374
4375 #ifdef HAVE_TARGET_32_BIG
4376 template
4377 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
4378 #endif
4379
4380 #ifdef HAVE_TARGET_64_LITTLE
4381 template
4382 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
4383 #endif
4384
4385 #ifdef HAVE_TARGET_64_BIG
4386 template
4387 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
4388 #endif
4389
4390 #ifdef HAVE_TARGET_32_LITTLE
4391 template
4392 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
4393 #endif
4394
4395 #ifdef HAVE_TARGET_32_BIG
4396 template
4397 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
4398 #endif
4399
4400 #ifdef HAVE_TARGET_64_LITTLE
4401 template
4402 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
4403 #endif
4404
4405 #ifdef HAVE_TARGET_64_BIG
4406 template
4407 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
4408 #endif
4409
4410 #ifdef HAVE_TARGET_32_LITTLE
4411 template
4412 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
4413 #endif
4414
4415 #ifdef HAVE_TARGET_32_BIG
4416 template
4417 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
4418 #endif
4419
4420 #ifdef HAVE_TARGET_64_LITTLE
4421 template
4422 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
4423 #endif
4424
4425 #ifdef HAVE_TARGET_64_BIG
4426 template
4427 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
4428 #endif
4429
4430 #ifdef HAVE_TARGET_32_LITTLE
4431 template
4432 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
4433 #endif
4434
4435 #ifdef HAVE_TARGET_32_BIG
4436 template
4437 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
4438 #endif
4439
4440 #ifdef HAVE_TARGET_64_LITTLE
4441 template
4442 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
4443 #endif
4444
4445 #ifdef HAVE_TARGET_64_BIG
4446 template
4447 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
4448 #endif
4449
4450 #ifdef HAVE_TARGET_32_LITTLE
4451 template
4452 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
4453 #endif
4454
4455 #ifdef HAVE_TARGET_32_BIG
4456 template
4457 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
4458 #endif
4459
4460 #ifdef HAVE_TARGET_64_LITTLE
4461 template
4462 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
4463 #endif
4464
4465 #ifdef HAVE_TARGET_64_BIG
4466 template
4467 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
4468 #endif
4469
4470 #ifdef HAVE_TARGET_32_LITTLE
4471 template
4472 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
4473 #endif
4474
4475 #ifdef HAVE_TARGET_32_BIG
4476 template
4477 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
4478 #endif
4479
4480 #ifdef HAVE_TARGET_64_LITTLE
4481 template
4482 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
4483 #endif
4484
4485 #ifdef HAVE_TARGET_64_BIG
4486 template
4487 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
4488 #endif
4489
4490 #ifdef HAVE_TARGET_32_LITTLE
4491 template
4492 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
4493 #endif
4494
4495 #ifdef HAVE_TARGET_32_BIG
4496 template
4497 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
4498 #endif
4499
4500 #ifdef HAVE_TARGET_64_LITTLE
4501 template
4502 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
4503 #endif
4504
4505 #ifdef HAVE_TARGET_64_BIG
4506 template
4507 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
4508 #endif
4509
4510 #ifdef HAVE_TARGET_32_LITTLE
4511 template
4512 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
4513 #endif
4514
4515 #ifdef HAVE_TARGET_32_BIG
4516 template
4517 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
4518 #endif
4519
4520 #ifdef HAVE_TARGET_64_LITTLE
4521 template
4522 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
4523 #endif
4524
4525 #ifdef HAVE_TARGET_64_BIG
4526 template
4527 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
4528 #endif
4529
4530 #ifdef HAVE_TARGET_32_LITTLE
4531 template
4532 class Output_data_group<32, false>;
4533 #endif
4534
4535 #ifdef HAVE_TARGET_32_BIG
4536 template
4537 class Output_data_group<32, true>;
4538 #endif
4539
4540 #ifdef HAVE_TARGET_64_LITTLE
4541 template
4542 class Output_data_group<64, false>;
4543 #endif
4544
4545 #ifdef HAVE_TARGET_64_BIG
4546 template
4547 class Output_data_group<64, true>;
4548 #endif
4549
4550 #ifdef HAVE_TARGET_32_LITTLE
4551 template
4552 class Output_data_got<32, false>;
4553 #endif
4554
4555 #ifdef HAVE_TARGET_32_BIG
4556 template
4557 class Output_data_got<32, true>;
4558 #endif
4559
4560 #ifdef HAVE_TARGET_64_LITTLE
4561 template
4562 class Output_data_got<64, false>;
4563 #endif
4564
4565 #ifdef HAVE_TARGET_64_BIG
4566 template
4567 class Output_data_got<64, true>;
4568 #endif
4569
4570 } // End namespace gold.
This page took 0.172378 seconds and 5 git commands to generate.