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