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