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