* gold.cc (queue_middle_tasks): Process existing GOT/PLT entries.
[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     case RESERVED_CODE:
1362       // If we're doing an incremental update, don't touch this GOT entry.
1363       if (parameters->incremental_update())
1364         return;
1365       val = this->u_.constant;
1366       break;
1367
1368     default:
1369       {
1370         const Sized_relobj<size, big_endian>* object = this->u_.object;
1371         const unsigned int lsi = this->local_sym_index_;
1372         const Symbol_value<size>* symval = object->local_symbol(lsi);
1373         if (!this->use_plt_offset_)
1374           val = symval->value(this->u_.object, 0);
1375         else
1376           {
1377             const Output_data* plt =
1378               parameters->target().plt_section_for_local(object, lsi);
1379             val = plt->address() + object->local_plt_offset(lsi);
1380           }
1381       }
1382       break;
1383     }
1384
1385   elfcpp::Swap<size, big_endian>::writeval(pov, val);
1386 }
1387
1388 // Output_data_got methods.
1389
1390 // Add an entry for a global symbol to the GOT.  This returns true if
1391 // this is a new GOT entry, false if the symbol already had a GOT
1392 // entry.
1393
1394 template<int size, bool big_endian>
1395 bool
1396 Output_data_got<size, big_endian>::add_global(
1397     Symbol* gsym,
1398     unsigned int got_type)
1399 {
1400   if (gsym->has_got_offset(got_type))
1401     return false;
1402
1403   unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false));
1404   gsym->set_got_offset(got_type, got_offset);
1405   return true;
1406 }
1407
1408 // Like add_global, but use the PLT offset.
1409
1410 template<int size, bool big_endian>
1411 bool
1412 Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1413                                                   unsigned int got_type)
1414 {
1415   if (gsym->has_got_offset(got_type))
1416     return false;
1417
1418   unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true));
1419   gsym->set_got_offset(got_type, got_offset);
1420   return true;
1421 }
1422
1423 // Add an entry for a global symbol to the GOT, and add a dynamic
1424 // relocation of type R_TYPE for the GOT entry.
1425
1426 template<int size, bool big_endian>
1427 void
1428 Output_data_got<size, big_endian>::add_global_with_rel(
1429     Symbol* gsym,
1430     unsigned int got_type,
1431     Rel_dyn* rel_dyn,
1432     unsigned int r_type)
1433 {
1434   if (gsym->has_got_offset(got_type))
1435     return;
1436
1437   unsigned int got_offset = this->add_got_entry(Got_entry());
1438   gsym->set_got_offset(got_type, got_offset);
1439   rel_dyn->add_global(gsym, r_type, this, got_offset);
1440 }
1441
1442 template<int size, bool big_endian>
1443 void
1444 Output_data_got<size, big_endian>::add_global_with_rela(
1445     Symbol* gsym,
1446     unsigned int got_type,
1447     Rela_dyn* rela_dyn,
1448     unsigned int r_type)
1449 {
1450   if (gsym->has_got_offset(got_type))
1451     return;
1452
1453   unsigned int got_offset = this->add_got_entry(Got_entry());
1454   gsym->set_got_offset(got_type, got_offset);
1455   rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1456 }
1457
1458 // Add a pair of entries for a global symbol to the GOT, and add
1459 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1460 // If R_TYPE_2 == 0, add the second entry with no relocation.
1461 template<int size, bool big_endian>
1462 void
1463 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1464     Symbol* gsym,
1465     unsigned int got_type,
1466     Rel_dyn* rel_dyn,
1467     unsigned int r_type_1,
1468     unsigned int r_type_2)
1469 {
1470   if (gsym->has_got_offset(got_type))
1471     return;
1472
1473   unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1474   gsym->set_got_offset(got_type, got_offset);
1475   rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1476
1477   if (r_type_2 != 0)
1478     rel_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8);
1479 }
1480
1481 template<int size, bool big_endian>
1482 void
1483 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1484     Symbol* gsym,
1485     unsigned int got_type,
1486     Rela_dyn* rela_dyn,
1487     unsigned int r_type_1,
1488     unsigned int r_type_2)
1489 {
1490   if (gsym->has_got_offset(got_type))
1491     return;
1492
1493   unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1494   gsym->set_got_offset(got_type, got_offset);
1495   rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1496
1497   if (r_type_2 != 0)
1498     rela_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8, 0);
1499 }
1500
1501 // Add an entry for a local symbol to the GOT.  This returns true if
1502 // this is a new GOT entry, false if the symbol already has a GOT
1503 // entry.
1504
1505 template<int size, bool big_endian>
1506 bool
1507 Output_data_got<size, big_endian>::add_local(
1508     Sized_relobj<size, big_endian>* object,
1509     unsigned int symndx,
1510     unsigned int got_type)
1511 {
1512   if (object->local_has_got_offset(symndx, got_type))
1513     return false;
1514
1515   unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1516                                                           false));
1517   object->set_local_got_offset(symndx, got_type, got_offset);
1518   return true;
1519 }
1520
1521 // Like add_local, but use the PLT offset.
1522
1523 template<int size, bool big_endian>
1524 bool
1525 Output_data_got<size, big_endian>::add_local_plt(
1526     Sized_relobj<size, big_endian>* object,
1527     unsigned int symndx,
1528     unsigned int got_type)
1529 {
1530   if (object->local_has_got_offset(symndx, got_type))
1531     return false;
1532
1533   unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1534                                                           true));
1535   object->set_local_got_offset(symndx, got_type, got_offset);
1536   return true;
1537 }
1538
1539 // Add an entry for a local symbol to the GOT, and add a dynamic
1540 // relocation of type R_TYPE for the GOT entry.
1541
1542 template<int size, bool big_endian>
1543 void
1544 Output_data_got<size, big_endian>::add_local_with_rel(
1545     Sized_relobj<size, big_endian>* object,
1546     unsigned int symndx,
1547     unsigned int got_type,
1548     Rel_dyn* rel_dyn,
1549     unsigned int r_type)
1550 {
1551   if (object->local_has_got_offset(symndx, got_type))
1552     return;
1553
1554   unsigned int got_offset = this->add_got_entry(Got_entry());
1555   object->set_local_got_offset(symndx, got_type, got_offset);
1556   rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1557 }
1558
1559 template<int size, bool big_endian>
1560 void
1561 Output_data_got<size, big_endian>::add_local_with_rela(
1562     Sized_relobj<size, big_endian>* object,
1563     unsigned int symndx,
1564     unsigned int got_type,
1565     Rela_dyn* rela_dyn,
1566     unsigned int r_type)
1567 {
1568   if (object->local_has_got_offset(symndx, got_type))
1569     return;
1570
1571   unsigned int got_offset = this->add_got_entry(Got_entry());
1572   object->set_local_got_offset(symndx, got_type, got_offset);
1573   rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1574 }
1575
1576 // Add a pair of entries for a local symbol to the GOT, and add
1577 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1578 // If R_TYPE_2 == 0, add the second entry with no relocation.
1579 template<int size, bool big_endian>
1580 void
1581 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1582     Sized_relobj<size, big_endian>* object,
1583     unsigned int symndx,
1584     unsigned int shndx,
1585     unsigned int got_type,
1586     Rel_dyn* rel_dyn,
1587     unsigned int r_type_1,
1588     unsigned int r_type_2)
1589 {
1590   if (object->local_has_got_offset(symndx, got_type))
1591     return;
1592
1593   unsigned int got_offset =
1594       this->add_got_entry_pair(Got_entry(),
1595                                Got_entry(object, symndx, false));
1596   object->set_local_got_offset(symndx, got_type, got_offset);
1597   Output_section* os = object->output_section(shndx);
1598   rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1599
1600   if (r_type_2 != 0)
1601     rel_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8);
1602 }
1603
1604 template<int size, bool big_endian>
1605 void
1606 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1607     Sized_relobj<size, big_endian>* object,
1608     unsigned int symndx,
1609     unsigned int shndx,
1610     unsigned int got_type,
1611     Rela_dyn* rela_dyn,
1612     unsigned int r_type_1,
1613     unsigned int r_type_2)
1614 {
1615   if (object->local_has_got_offset(symndx, got_type))
1616     return;
1617
1618   unsigned int got_offset =
1619       this->add_got_entry_pair(Got_entry(),
1620                                Got_entry(object, symndx, false));
1621   object->set_local_got_offset(symndx, got_type, got_offset);
1622   Output_section* os = object->output_section(shndx);
1623   rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1624
1625   if (r_type_2 != 0)
1626     rela_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8, 0);
1627 }
1628
1629 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1630
1631 template<int size, bool big_endian>
1632 void
1633 Output_data_got<size, big_endian>::reserve_slot(unsigned int i)
1634 {
1635   this->free_list_.remove(i * size / 8, (i + 1) * size / 8);
1636 }
1637
1638 // Reserve a slot in the GOT for a global symbol.
1639
1640 template<int size, bool big_endian>
1641 void
1642 Output_data_got<size, big_endian>::reserve_slot_for_global(
1643     unsigned int i,
1644     Symbol* gsym,
1645     unsigned int got_type)
1646 {
1647   this->free_list_.remove(i * size / 8, (i + 1) * size / 8);
1648   gsym->set_got_offset(got_type, this->got_offset(i));
1649 }
1650
1651 // Write out the GOT.
1652
1653 template<int size, bool big_endian>
1654 void
1655 Output_data_got<size, big_endian>::do_write(Output_file* of)
1656 {
1657   const int add = size / 8;
1658
1659   const off_t off = this->offset();
1660   const off_t oview_size = this->data_size();
1661   unsigned char* const oview = of->get_output_view(off, oview_size);
1662
1663   unsigned char* pov = oview;
1664   for (typename Got_entries::const_iterator p = this->entries_.begin();
1665        p != this->entries_.end();
1666        ++p)
1667     {
1668       p->write(pov);
1669       pov += add;
1670     }
1671
1672   gold_assert(pov - oview == oview_size);
1673
1674   of->write_output_view(off, oview_size, oview);
1675
1676   // We no longer need the GOT entries.
1677   this->entries_.clear();
1678 }
1679
1680 // Create a new GOT entry and return its offset.
1681
1682 template<int size, bool big_endian>
1683 unsigned int
1684 Output_data_got<size, big_endian>::add_got_entry(Got_entry got_entry)
1685 {
1686   if (!this->is_data_size_valid())
1687     {
1688       this->entries_.push_back(got_entry);
1689       this->set_got_size();
1690       return this->last_got_offset();
1691     }
1692   else
1693     {
1694       // For an incremental update, find an available slot.
1695       off_t got_offset = this->free_list_.allocate(size / 8, size / 8, 0);
1696       if (got_offset == -1)
1697         gold_fatal(_("out of patch space (GOT);"
1698                      " relink with --incremental-full"));
1699       unsigned int got_index = got_offset / (size / 8);
1700       gold_assert(got_index < this->entries_.size());
1701       this->entries_[got_index] = got_entry;
1702       return static_cast<unsigned int>(got_offset);
1703     }
1704 }
1705
1706 // Create a pair of new GOT entries and return the offset of the first.
1707
1708 template<int size, bool big_endian>
1709 unsigned int
1710 Output_data_got<size, big_endian>::add_got_entry_pair(Got_entry got_entry_1,
1711                                                       Got_entry got_entry_2)
1712 {
1713   if (!this->is_data_size_valid())
1714     {
1715       unsigned int got_offset;
1716       this->entries_.push_back(got_entry_1);
1717       got_offset = this->last_got_offset();
1718       this->entries_.push_back(got_entry_2);
1719       this->set_got_size();
1720       return got_offset;
1721     }
1722   else
1723     {
1724       // For an incremental update, find an available pair of slots.
1725       off_t got_offset = this->free_list_.allocate(2 * size / 8, size / 8, 0);
1726       if (got_offset == -1)
1727         gold_fatal(_("out of patch space (GOT);"
1728                      " relink with --incremental-full"));
1729       unsigned int got_index = got_offset / (size / 8);
1730       gold_assert(got_index < this->entries_.size());
1731       this->entries_[got_index] = got_entry_1;
1732       this->entries_[got_index + 1] = got_entry_2;
1733       return static_cast<unsigned int>(got_offset);
1734     }
1735 }
1736
1737 // Output_data_dynamic::Dynamic_entry methods.
1738
1739 // Write out the entry.
1740
1741 template<int size, bool big_endian>
1742 void
1743 Output_data_dynamic::Dynamic_entry::write(
1744     unsigned char* pov,
1745     const Stringpool* pool) const
1746 {
1747   typename elfcpp::Elf_types<size>::Elf_WXword val;
1748   switch (this->offset_)
1749     {
1750     case DYNAMIC_NUMBER:
1751       val = this->u_.val;
1752       break;
1753
1754     case DYNAMIC_SECTION_SIZE:
1755       val = this->u_.od->data_size();
1756       if (this->od2 != NULL)
1757         val += this->od2->data_size();
1758       break;
1759
1760     case DYNAMIC_SYMBOL:
1761       {
1762         const Sized_symbol<size>* s =
1763           static_cast<const Sized_symbol<size>*>(this->u_.sym);
1764         val = s->value();
1765       }
1766       break;
1767
1768     case DYNAMIC_STRING:
1769       val = pool->get_offset(this->u_.str);
1770       break;
1771
1772     default:
1773       val = this->u_.od->address() + this->offset_;
1774       break;
1775     }
1776
1777   elfcpp::Dyn_write<size, big_endian> dw(pov);
1778   dw.put_d_tag(this->tag_);
1779   dw.put_d_val(val);
1780 }
1781
1782 // Output_data_dynamic methods.
1783
1784 // Adjust the output section to set the entry size.
1785
1786 void
1787 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1788 {
1789   if (parameters->target().get_size() == 32)
1790     os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1791   else if (parameters->target().get_size() == 64)
1792     os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1793   else
1794     gold_unreachable();
1795 }
1796
1797 // Set the final data size.
1798
1799 void
1800 Output_data_dynamic::set_final_data_size()
1801 {
1802   // Add the terminating entry if it hasn't been added.
1803   // Because of relaxation, we can run this multiple times.
1804   if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1805     {
1806       int extra = parameters->options().spare_dynamic_tags();
1807       for (int i = 0; i < extra; ++i)
1808         this->add_constant(elfcpp::DT_NULL, 0);
1809       this->add_constant(elfcpp::DT_NULL, 0);
1810     }
1811
1812   int dyn_size;
1813   if (parameters->target().get_size() == 32)
1814     dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1815   else if (parameters->target().get_size() == 64)
1816     dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1817   else
1818     gold_unreachable();
1819   this->set_data_size(this->entries_.size() * dyn_size);
1820 }
1821
1822 // Write out the dynamic entries.
1823
1824 void
1825 Output_data_dynamic::do_write(Output_file* of)
1826 {
1827   switch (parameters->size_and_endianness())
1828     {
1829 #ifdef HAVE_TARGET_32_LITTLE
1830     case Parameters::TARGET_32_LITTLE:
1831       this->sized_write<32, false>(of);
1832       break;
1833 #endif
1834 #ifdef HAVE_TARGET_32_BIG
1835     case Parameters::TARGET_32_BIG:
1836       this->sized_write<32, true>(of);
1837       break;
1838 #endif
1839 #ifdef HAVE_TARGET_64_LITTLE
1840     case Parameters::TARGET_64_LITTLE:
1841       this->sized_write<64, false>(of);
1842       break;
1843 #endif
1844 #ifdef HAVE_TARGET_64_BIG
1845     case Parameters::TARGET_64_BIG:
1846       this->sized_write<64, true>(of);
1847       break;
1848 #endif
1849     default:
1850       gold_unreachable();
1851     }
1852 }
1853
1854 template<int size, bool big_endian>
1855 void
1856 Output_data_dynamic::sized_write(Output_file* of)
1857 {
1858   const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1859
1860   const off_t offset = this->offset();
1861   const off_t oview_size = this->data_size();
1862   unsigned char* const oview = of->get_output_view(offset, oview_size);
1863
1864   unsigned char* pov = oview;
1865   for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1866        p != this->entries_.end();
1867        ++p)
1868     {
1869       p->write<size, big_endian>(pov, this->pool_);
1870       pov += dyn_size;
1871     }
1872
1873   gold_assert(pov - oview == oview_size);
1874
1875   of->write_output_view(offset, oview_size, oview);
1876
1877   // We no longer need the dynamic entries.
1878   this->entries_.clear();
1879 }
1880
1881 // Class Output_symtab_xindex.
1882
1883 void
1884 Output_symtab_xindex::do_write(Output_file* of)
1885 {
1886   const off_t offset = this->offset();
1887   const off_t oview_size = this->data_size();
1888   unsigned char* const oview = of->get_output_view(offset, oview_size);
1889
1890   memset(oview, 0, oview_size);
1891
1892   if (parameters->target().is_big_endian())
1893     this->endian_do_write<true>(oview);
1894   else
1895     this->endian_do_write<false>(oview);
1896
1897   of->write_output_view(offset, oview_size, oview);
1898
1899   // We no longer need the data.
1900   this->entries_.clear();
1901 }
1902
1903 template<bool big_endian>
1904 void
1905 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1906 {
1907   for (Xindex_entries::const_iterator p = this->entries_.begin();
1908        p != this->entries_.end();
1909        ++p)
1910     {
1911       unsigned int symndx = p->first;
1912       gold_assert(symndx * 4 < this->data_size());
1913       elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1914     }
1915 }
1916
1917 // Output_section::Input_section methods.
1918
1919 // Return the current data size.  For an input section we store the size here.
1920 // For an Output_section_data, we have to ask it for the size.
1921
1922 off_t
1923 Output_section::Input_section::current_data_size() const
1924 {
1925   if (this->is_input_section())
1926     return this->u1_.data_size;
1927   else
1928     {
1929       this->u2_.posd->pre_finalize_data_size();
1930       return this->u2_.posd->current_data_size();
1931     }
1932 }
1933
1934 // Return the data size.  For an input section we store the size here.
1935 // For an Output_section_data, we have to ask it for the size.
1936
1937 off_t
1938 Output_section::Input_section::data_size() const
1939 {
1940   if (this->is_input_section())
1941     return this->u1_.data_size;
1942   else
1943     return this->u2_.posd->data_size();
1944 }
1945
1946 // Return the object for an input section.
1947
1948 Relobj*
1949 Output_section::Input_section::relobj() const
1950 {
1951   if (this->is_input_section())
1952     return this->u2_.object;
1953   else if (this->is_merge_section())
1954     {
1955       gold_assert(this->u2_.pomb->first_relobj() != NULL);
1956       return this->u2_.pomb->first_relobj();
1957     }
1958   else if (this->is_relaxed_input_section())
1959     return this->u2_.poris->relobj();
1960   else
1961     gold_unreachable();
1962 }
1963
1964 // Return the input section index for an input section.
1965
1966 unsigned int
1967 Output_section::Input_section::shndx() const
1968 {
1969   if (this->is_input_section())
1970     return this->shndx_;
1971   else if (this->is_merge_section())
1972     {
1973       gold_assert(this->u2_.pomb->first_relobj() != NULL);
1974       return this->u2_.pomb->first_shndx();
1975     }
1976   else if (this->is_relaxed_input_section())
1977     return this->u2_.poris->shndx();
1978   else
1979     gold_unreachable();
1980 }
1981
1982 // Set the address and file offset.
1983
1984 void
1985 Output_section::Input_section::set_address_and_file_offset(
1986     uint64_t address,
1987     off_t file_offset,
1988     off_t section_file_offset)
1989 {
1990   if (this->is_input_section())
1991     this->u2_.object->set_section_offset(this->shndx_,
1992                                          file_offset - section_file_offset);
1993   else
1994     this->u2_.posd->set_address_and_file_offset(address, file_offset);
1995 }
1996
1997 // Reset the address and file offset.
1998
1999 void
2000 Output_section::Input_section::reset_address_and_file_offset()
2001 {
2002   if (!this->is_input_section())
2003     this->u2_.posd->reset_address_and_file_offset();
2004 }
2005
2006 // Finalize the data size.
2007
2008 void
2009 Output_section::Input_section::finalize_data_size()
2010 {
2011   if (!this->is_input_section())
2012     this->u2_.posd->finalize_data_size();
2013 }
2014
2015 // Try to turn an input offset into an output offset.  We want to
2016 // return the output offset relative to the start of this
2017 // Input_section in the output section.
2018
2019 inline bool
2020 Output_section::Input_section::output_offset(
2021     const Relobj* object,
2022     unsigned int shndx,
2023     section_offset_type offset,
2024     section_offset_type* poutput) const
2025 {
2026   if (!this->is_input_section())
2027     return this->u2_.posd->output_offset(object, shndx, offset, poutput);
2028   else
2029     {
2030       if (this->shndx_ != shndx || this->u2_.object != object)
2031         return false;
2032       *poutput = offset;
2033       return true;
2034     }
2035 }
2036
2037 // Return whether this is the merge section for the input section
2038 // SHNDX in OBJECT.
2039
2040 inline bool
2041 Output_section::Input_section::is_merge_section_for(const Relobj* object,
2042                                                     unsigned int shndx) const
2043 {
2044   if (this->is_input_section())
2045     return false;
2046   return this->u2_.posd->is_merge_section_for(object, shndx);
2047 }
2048
2049 // Write out the data.  We don't have to do anything for an input
2050 // section--they are handled via Object::relocate--but this is where
2051 // we write out the data for an Output_section_data.
2052
2053 void
2054 Output_section::Input_section::write(Output_file* of)
2055 {
2056   if (!this->is_input_section())
2057     this->u2_.posd->write(of);
2058 }
2059
2060 // Write the data to a buffer.  As for write(), we don't have to do
2061 // anything for an input section.
2062
2063 void
2064 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2065 {
2066   if (!this->is_input_section())
2067     this->u2_.posd->write_to_buffer(buffer);
2068 }
2069
2070 // Print to a map file.
2071
2072 void
2073 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2074 {
2075   switch (this->shndx_)
2076     {
2077     case OUTPUT_SECTION_CODE:
2078     case MERGE_DATA_SECTION_CODE:
2079     case MERGE_STRING_SECTION_CODE:
2080       this->u2_.posd->print_to_mapfile(mapfile);
2081       break;
2082
2083     case RELAXED_INPUT_SECTION_CODE:
2084       {
2085         Output_relaxed_input_section* relaxed_section =
2086           this->relaxed_input_section();
2087         mapfile->print_input_section(relaxed_section->relobj(),
2088                                      relaxed_section->shndx());
2089       }
2090       break;
2091     default:
2092       mapfile->print_input_section(this->u2_.object, this->shndx_);
2093       break;
2094     }
2095 }
2096
2097 // Output_section methods.
2098
2099 // Construct an Output_section.  NAME will point into a Stringpool.
2100
2101 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2102                                elfcpp::Elf_Xword flags)
2103   : name_(name),
2104     addralign_(0),
2105     entsize_(0),
2106     load_address_(0),
2107     link_section_(NULL),
2108     link_(0),
2109     info_section_(NULL),
2110     info_symndx_(NULL),
2111     info_(0),
2112     type_(type),
2113     flags_(flags),
2114     order_(ORDER_INVALID),
2115     out_shndx_(-1U),
2116     symtab_index_(0),
2117     dynsym_index_(0),
2118     input_sections_(),
2119     first_input_offset_(0),
2120     fills_(),
2121     postprocessing_buffer_(NULL),
2122     needs_symtab_index_(false),
2123     needs_dynsym_index_(false),
2124     should_link_to_symtab_(false),
2125     should_link_to_dynsym_(false),
2126     after_input_sections_(false),
2127     requires_postprocessing_(false),
2128     found_in_sections_clause_(false),
2129     has_load_address_(false),
2130     info_uses_section_index_(false),
2131     input_section_order_specified_(false),
2132     may_sort_attached_input_sections_(false),
2133     must_sort_attached_input_sections_(false),
2134     attached_input_sections_are_sorted_(false),
2135     is_relro_(false),
2136     is_small_section_(false),
2137     is_large_section_(false),
2138     generate_code_fills_at_write_(false),
2139     is_entsize_zero_(false),
2140     section_offsets_need_adjustment_(false),
2141     is_noload_(false),
2142     always_keeps_input_sections_(false),
2143     has_fixed_layout_(false),
2144     tls_offset_(0),
2145     checkpoint_(NULL),
2146     lookup_maps_(new Output_section_lookup_maps),
2147     free_list_()
2148 {
2149   // An unallocated section has no address.  Forcing this means that
2150   // we don't need special treatment for symbols defined in debug
2151   // sections.
2152   if ((flags & elfcpp::SHF_ALLOC) == 0)
2153     this->set_address(0);
2154 }
2155
2156 Output_section::~Output_section()
2157 {
2158   delete this->checkpoint_;
2159 }
2160
2161 // Set the entry size.
2162
2163 void
2164 Output_section::set_entsize(uint64_t v)
2165 {
2166   if (this->is_entsize_zero_)
2167     ;
2168   else if (this->entsize_ == 0)
2169     this->entsize_ = v;
2170   else if (this->entsize_ != v)
2171     {
2172       this->entsize_ = 0;
2173       this->is_entsize_zero_ = 1;
2174     }
2175 }
2176
2177 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2178 // OBJECT, to the Output_section.  RELOC_SHNDX is the index of a
2179 // relocation section which applies to this section, or 0 if none, or
2180 // -1U if more than one.  Return the offset of the input section
2181 // within the output section.  Return -1 if the input section will
2182 // receive special handling.  In the normal case we don't always keep
2183 // track of input sections for an Output_section.  Instead, each
2184 // Object keeps track of the Output_section for each of its input
2185 // sections.  However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2186 // track of input sections here; this is used when SECTIONS appears in
2187 // a linker script.
2188
2189 template<int size, bool big_endian>
2190 off_t
2191 Output_section::add_input_section(Layout* layout,
2192                                   Sized_relobj<size, big_endian>* object,
2193                                   unsigned int shndx,
2194                                   const char* secname,
2195                                   const elfcpp::Shdr<size, big_endian>& shdr,
2196                                   unsigned int reloc_shndx,
2197                                   bool have_sections_script)
2198 {
2199   elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2200   if ((addralign & (addralign - 1)) != 0)
2201     {
2202       object->error(_("invalid alignment %lu for section \"%s\""),
2203                     static_cast<unsigned long>(addralign), secname);
2204       addralign = 1;
2205     }
2206
2207   if (addralign > this->addralign_)
2208     this->addralign_ = addralign;
2209
2210   typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2211   uint64_t entsize = shdr.get_sh_entsize();
2212
2213   // .debug_str is a mergeable string section, but is not always so
2214   // marked by compilers.  Mark manually here so we can optimize.
2215   if (strcmp(secname, ".debug_str") == 0)
2216     {
2217       sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2218       entsize = 1;
2219     }
2220
2221   this->update_flags_for_input_section(sh_flags);
2222   this->set_entsize(entsize);
2223
2224   // If this is a SHF_MERGE section, we pass all the input sections to
2225   // a Output_data_merge.  We don't try to handle relocations for such
2226   // a section.  We don't try to handle empty merge sections--they
2227   // mess up the mappings, and are useless anyhow.
2228   // FIXME: Need to handle merge sections during incremental update.
2229   if ((sh_flags & elfcpp::SHF_MERGE) != 0
2230       && reloc_shndx == 0
2231       && shdr.get_sh_size() > 0
2232       && !parameters->incremental())
2233     {
2234       // Keep information about merged input sections for rebuilding fast
2235       // lookup maps if we have sections-script or we do relaxation.
2236       bool keeps_input_sections = (this->always_keeps_input_sections_
2237                                    || have_sections_script
2238                                    || parameters->target().may_relax());
2239
2240       if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2241                                         addralign, keeps_input_sections))
2242         {
2243           // Tell the relocation routines that they need to call the
2244           // output_offset method to determine the final address.
2245           return -1;
2246         }
2247     }
2248
2249   section_size_type input_section_size = shdr.get_sh_size();
2250   section_size_type uncompressed_size;
2251   if (object->section_is_compressed(shndx, &uncompressed_size))
2252     input_section_size = uncompressed_size;
2253
2254   off_t offset_in_section;
2255   off_t aligned_offset_in_section;
2256   if (this->has_fixed_layout())
2257     {
2258       // For incremental updates, find a chunk of unused space in the section.
2259       offset_in_section = this->free_list_.allocate(input_section_size,
2260                                                     addralign, 0);
2261       if (offset_in_section == -1)
2262         gold_fatal(_("out of patch space; relink with --incremental-full"));
2263       aligned_offset_in_section = offset_in_section;
2264     }
2265   else
2266     {
2267       offset_in_section = this->current_data_size_for_child();
2268       aligned_offset_in_section = align_address(offset_in_section,
2269                                                 addralign);
2270       this->set_current_data_size_for_child(aligned_offset_in_section
2271                                             + input_section_size);
2272     }
2273
2274   // Determine if we want to delay code-fill generation until the output
2275   // section is written.  When the target is relaxing, we want to delay fill
2276   // generating to avoid adjusting them during relaxation.  Also, if we are
2277   // sorting input sections we must delay fill generation.
2278   if (!this->generate_code_fills_at_write_
2279       && !have_sections_script
2280       && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2281       && parameters->target().has_code_fill()
2282       && (parameters->target().may_relax()
2283           || parameters->options().section_ordering_file()))
2284     {
2285       gold_assert(this->fills_.empty());
2286       this->generate_code_fills_at_write_ = true;
2287     }
2288
2289   if (aligned_offset_in_section > offset_in_section
2290       && !this->generate_code_fills_at_write_
2291       && !have_sections_script
2292       && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2293       && parameters->target().has_code_fill())
2294     {
2295       // We need to add some fill data.  Using fill_list_ when
2296       // possible is an optimization, since we will often have fill
2297       // sections without input sections.
2298       off_t fill_len = aligned_offset_in_section - offset_in_section;
2299       if (this->input_sections_.empty())
2300         this->fills_.push_back(Fill(offset_in_section, fill_len));
2301       else
2302         {
2303           std::string fill_data(parameters->target().code_fill(fill_len));
2304           Output_data_const* odc = new Output_data_const(fill_data, 1);
2305           this->input_sections_.push_back(Input_section(odc));
2306         }
2307     }
2308
2309   // We need to keep track of this section if we are already keeping
2310   // track of sections, or if we are relaxing.  Also, if this is a
2311   // section which requires sorting, or which may require sorting in
2312   // the future, we keep track of the sections.  If the
2313   // --section-ordering-file option is used to specify the order of
2314   // sections, we need to keep track of sections.
2315   if (this->always_keeps_input_sections_
2316       || have_sections_script
2317       || !this->input_sections_.empty()
2318       || this->may_sort_attached_input_sections()
2319       || this->must_sort_attached_input_sections()
2320       || parameters->options().user_set_Map()
2321       || parameters->target().may_relax()
2322       || parameters->options().section_ordering_file())
2323     {
2324       Input_section isecn(object, shndx, input_section_size, addralign);
2325       if (parameters->options().section_ordering_file())
2326         {
2327           unsigned int section_order_index =
2328             layout->find_section_order_index(std::string(secname));
2329           if (section_order_index != 0)
2330             {
2331               isecn.set_section_order_index(section_order_index);
2332               this->set_input_section_order_specified();
2333             }
2334         }
2335       if (this->has_fixed_layout())
2336         {
2337           // For incremental updates, finalize the address and offset now.
2338           uint64_t addr = this->address();
2339           isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2340                                             aligned_offset_in_section,
2341                                             this->offset());
2342         }
2343       this->input_sections_.push_back(isecn);
2344     }
2345
2346   return aligned_offset_in_section;
2347 }
2348
2349 // Add arbitrary data to an output section.
2350
2351 void
2352 Output_section::add_output_section_data(Output_section_data* posd)
2353 {
2354   Input_section inp(posd);
2355   this->add_output_section_data(&inp);
2356
2357   if (posd->is_data_size_valid())
2358     {
2359       off_t offset_in_section;
2360       if (this->has_fixed_layout())
2361         {
2362           // For incremental updates, find a chunk of unused space.
2363           offset_in_section = this->free_list_.allocate(posd->data_size(),
2364                                                         posd->addralign(), 0);
2365           if (offset_in_section == -1)
2366             gold_fatal(_("out of patch space; relink with --incremental-full"));
2367           // Finalize the address and offset now.
2368           uint64_t addr = this->address();
2369           off_t offset = this->offset();
2370           posd->set_address_and_file_offset(addr + offset_in_section,
2371                                             offset + offset_in_section);
2372         }
2373       else
2374         {
2375           offset_in_section = this->current_data_size_for_child();
2376           off_t aligned_offset_in_section = align_address(offset_in_section,
2377                                                           posd->addralign());
2378           this->set_current_data_size_for_child(aligned_offset_in_section
2379                                                 + posd->data_size());
2380         }
2381     }
2382   else if (this->has_fixed_layout())
2383     {
2384       // For incremental updates, arrange for the data to have a fixed layout.
2385       // This will mean that additions to the data must be allocated from
2386       // free space within the containing output section.
2387       uint64_t addr = this->address();
2388       posd->set_address(addr);
2389       posd->set_file_offset(0);
2390       // FIXME: This should eventually be unreachable.
2391       // gold_unreachable();
2392     }
2393 }
2394
2395 // Add a relaxed input section.
2396
2397 void
2398 Output_section::add_relaxed_input_section(Layout* layout,
2399                                           Output_relaxed_input_section* poris,
2400                                           const std::string& name)
2401 {
2402   Input_section inp(poris);
2403
2404   // If the --section-ordering-file option is used to specify the order of
2405   // sections, we need to keep track of sections.
2406   if (parameters->options().section_ordering_file())
2407     {
2408       unsigned int section_order_index =
2409         layout->find_section_order_index(name);
2410       if (section_order_index != 0)
2411         {
2412           inp.set_section_order_index(section_order_index);
2413           this->set_input_section_order_specified();
2414         }
2415     }
2416
2417   this->add_output_section_data(&inp);
2418   if (this->lookup_maps_->is_valid())
2419     this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2420                                                   poris->shndx(), poris);
2421
2422   // For a relaxed section, we use the current data size.  Linker scripts
2423   // get all the input sections, including relaxed one from an output
2424   // section and add them back to them same output section to compute the
2425   // output section size.  If we do not account for sizes of relaxed input
2426   // sections,  an output section would be incorrectly sized.
2427   off_t offset_in_section = this->current_data_size_for_child();
2428   off_t aligned_offset_in_section = align_address(offset_in_section,
2429                                                   poris->addralign());
2430   this->set_current_data_size_for_child(aligned_offset_in_section
2431                                         + poris->current_data_size());
2432 }
2433
2434 // Add arbitrary data to an output section by Input_section.
2435
2436 void
2437 Output_section::add_output_section_data(Input_section* inp)
2438 {
2439   if (this->input_sections_.empty())
2440     this->first_input_offset_ = this->current_data_size_for_child();
2441
2442   this->input_sections_.push_back(*inp);
2443
2444   uint64_t addralign = inp->addralign();
2445   if (addralign > this->addralign_)
2446     this->addralign_ = addralign;
2447
2448   inp->set_output_section(this);
2449 }
2450
2451 // Add a merge section to an output section.
2452
2453 void
2454 Output_section::add_output_merge_section(Output_section_data* posd,
2455                                          bool is_string, uint64_t entsize)
2456 {
2457   Input_section inp(posd, is_string, entsize);
2458   this->add_output_section_data(&inp);
2459 }
2460
2461 // Add an input section to a SHF_MERGE section.
2462
2463 bool
2464 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2465                                         uint64_t flags, uint64_t entsize,
2466                                         uint64_t addralign,
2467                                         bool keeps_input_sections)
2468 {
2469   bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2470
2471   // We only merge strings if the alignment is not more than the
2472   // character size.  This could be handled, but it's unusual.
2473   if (is_string && addralign > entsize)
2474     return false;
2475
2476   // We cannot restore merged input section states.
2477   gold_assert(this->checkpoint_ == NULL);
2478
2479   // Look up merge sections by required properties.
2480   // Currently, we only invalidate the lookup maps in script processing
2481   // and relaxation.  We should not have done either when we reach here.
2482   // So we assume that the lookup maps are valid to simply code.
2483   gold_assert(this->lookup_maps_->is_valid());
2484   Merge_section_properties msp(is_string, entsize, addralign);
2485   Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2486   bool is_new = false;
2487   if (pomb != NULL)
2488     {
2489       gold_assert(pomb->is_string() == is_string
2490                   && pomb->entsize() == entsize
2491                   && pomb->addralign() == addralign);
2492     }
2493   else
2494     {
2495       // Create a new Output_merge_data or Output_merge_string_data.
2496       if (!is_string)
2497         pomb = new Output_merge_data(entsize, addralign);
2498       else
2499         {
2500           switch (entsize)
2501             {
2502             case 1:
2503               pomb = new Output_merge_string<char>(addralign);
2504               break;
2505             case 2:
2506               pomb = new Output_merge_string<uint16_t>(addralign);
2507               break;
2508             case 4:
2509               pomb = new Output_merge_string<uint32_t>(addralign);
2510               break;
2511             default:
2512               return false;
2513             }
2514         }
2515       // If we need to do script processing or relaxation, we need to keep
2516       // the original input sections to rebuild the fast lookup maps.
2517       if (keeps_input_sections)
2518         pomb->set_keeps_input_sections();
2519       is_new = true;
2520     }
2521
2522   if (pomb->add_input_section(object, shndx))
2523     {
2524       // Add new merge section to this output section and link merge
2525       // section properties to new merge section in map.
2526       if (is_new)
2527         {
2528           this->add_output_merge_section(pomb, is_string, entsize);
2529           this->lookup_maps_->add_merge_section(msp, pomb);
2530         }
2531
2532       // Add input section to new merge section and link input section to new
2533       // merge section in map.
2534       this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
2535       return true;
2536     }
2537   else
2538     {
2539       // If add_input_section failed, delete new merge section to avoid
2540       // exporting empty merge sections in Output_section::get_input_section.
2541       if (is_new)
2542         delete pomb;
2543       return false;
2544     }
2545 }
2546
2547 // Build a relaxation map to speed up relaxation of existing input sections.
2548 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2549
2550 void
2551 Output_section::build_relaxation_map(
2552   const Input_section_list& input_sections,
2553   size_t limit,
2554   Relaxation_map* relaxation_map) const
2555 {
2556   for (size_t i = 0; i < limit; ++i)
2557     {
2558       const Input_section& is(input_sections[i]);
2559       if (is.is_input_section() || is.is_relaxed_input_section())
2560         {
2561           Section_id sid(is.relobj(), is.shndx());
2562           (*relaxation_map)[sid] = i;
2563         }
2564     }
2565 }
2566
2567 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2568 // sections in RELAXED_SECTIONS.  MAP is a prebuilt map from section id
2569 // indices of INPUT_SECTIONS.
2570
2571 void
2572 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2573   const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2574   const Relaxation_map& map,
2575   Input_section_list* input_sections)
2576 {
2577   for (size_t i = 0; i < relaxed_sections.size(); ++i)
2578     {
2579       Output_relaxed_input_section* poris = relaxed_sections[i];
2580       Section_id sid(poris->relobj(), poris->shndx());
2581       Relaxation_map::const_iterator p = map.find(sid);
2582       gold_assert(p != map.end());
2583       gold_assert((*input_sections)[p->second].is_input_section());
2584
2585       // Remember section order index of original input section
2586       // if it is set.  Copy it to the relaxed input section.
2587       unsigned int soi =
2588         (*input_sections)[p->second].section_order_index();
2589       (*input_sections)[p->second] = Input_section(poris);
2590       (*input_sections)[p->second].set_section_order_index(soi);
2591     }
2592 }
2593   
2594 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2595 // is a vector of pointers to Output_relaxed_input_section or its derived
2596 // classes.  The relaxed sections must correspond to existing input sections.
2597
2598 void
2599 Output_section::convert_input_sections_to_relaxed_sections(
2600   const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2601 {
2602   gold_assert(parameters->target().may_relax());
2603
2604   // We want to make sure that restore_states does not undo the effect of
2605   // this.  If there is no checkpoint active, just search the current
2606   // input section list and replace the sections there.  If there is
2607   // a checkpoint, also replace the sections there.
2608   
2609   // By default, we look at the whole list.
2610   size_t limit = this->input_sections_.size();
2611
2612   if (this->checkpoint_ != NULL)
2613     {
2614       // Replace input sections with relaxed input section in the saved
2615       // copy of the input section list.
2616       if (this->checkpoint_->input_sections_saved())
2617         {
2618           Relaxation_map map;
2619           this->build_relaxation_map(
2620                     *(this->checkpoint_->input_sections()),
2621                     this->checkpoint_->input_sections()->size(),
2622                     &map);
2623           this->convert_input_sections_in_list_to_relaxed_sections(
2624                     relaxed_sections,
2625                     map,
2626                     this->checkpoint_->input_sections());
2627         }
2628       else
2629         {
2630           // We have not copied the input section list yet.  Instead, just
2631           // look at the portion that would be saved.
2632           limit = this->checkpoint_->input_sections_size();
2633         }
2634     }
2635
2636   // Convert input sections in input_section_list.
2637   Relaxation_map map;
2638   this->build_relaxation_map(this->input_sections_, limit, &map);
2639   this->convert_input_sections_in_list_to_relaxed_sections(
2640             relaxed_sections,
2641             map,
2642             &this->input_sections_);
2643
2644   // Update fast look-up map.
2645   if (this->lookup_maps_->is_valid())
2646     for (size_t i = 0; i < relaxed_sections.size(); ++i)
2647       {
2648         Output_relaxed_input_section* poris = relaxed_sections[i];
2649         this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2650                                                       poris->shndx(), poris);
2651       }
2652 }
2653
2654 // Update the output section flags based on input section flags.
2655
2656 void
2657 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2658 {
2659   // If we created the section with SHF_ALLOC clear, we set the
2660   // address.  If we are now setting the SHF_ALLOC flag, we need to
2661   // undo that.
2662   if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2663       && (flags & elfcpp::SHF_ALLOC) != 0)
2664     this->mark_address_invalid();
2665
2666   this->flags_ |= (flags
2667                    & (elfcpp::SHF_WRITE
2668                       | elfcpp::SHF_ALLOC
2669                       | elfcpp::SHF_EXECINSTR));
2670
2671   if ((flags & elfcpp::SHF_MERGE) == 0)
2672     this->flags_ &=~ elfcpp::SHF_MERGE;
2673   else
2674     {
2675       if (this->current_data_size_for_child() == 0)
2676         this->flags_ |= elfcpp::SHF_MERGE;
2677     }
2678
2679   if ((flags & elfcpp::SHF_STRINGS) == 0)
2680     this->flags_ &=~ elfcpp::SHF_STRINGS;
2681   else
2682     {
2683       if (this->current_data_size_for_child() == 0)
2684         this->flags_ |= elfcpp::SHF_STRINGS;
2685     }
2686 }
2687
2688 // Find the merge section into which an input section with index SHNDX in
2689 // OBJECT has been added.  Return NULL if none found.
2690
2691 Output_section_data*
2692 Output_section::find_merge_section(const Relobj* object,
2693                                    unsigned int shndx) const
2694 {
2695   if (!this->lookup_maps_->is_valid())
2696     this->build_lookup_maps();
2697   return this->lookup_maps_->find_merge_section(object, shndx);
2698 }
2699
2700 // Build the lookup maps for merge and relaxed sections.  This is needs
2701 // to be declared as a const methods so that it is callable with a const
2702 // Output_section pointer.  The method only updates states of the maps.
2703
2704 void
2705 Output_section::build_lookup_maps() const
2706 {
2707   this->lookup_maps_->clear();
2708   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2709        p != this->input_sections_.end();
2710        ++p)
2711     {
2712       if (p->is_merge_section())
2713         {
2714           Output_merge_base* pomb = p->output_merge_base();
2715           Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2716                                        pomb->addralign());
2717           this->lookup_maps_->add_merge_section(msp, pomb);
2718           for (Output_merge_base::Input_sections::const_iterator is =
2719                  pomb->input_sections_begin();
2720                is != pomb->input_sections_end();
2721                ++is) 
2722             {
2723               const Const_section_id& csid = *is;
2724             this->lookup_maps_->add_merge_input_section(csid.first,
2725                                                         csid.second, pomb);
2726             }
2727             
2728         }
2729       else if (p->is_relaxed_input_section())
2730         {
2731           Output_relaxed_input_section* poris = p->relaxed_input_section();
2732           this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2733                                                         poris->shndx(), poris);
2734         }
2735     }
2736 }
2737
2738 // Find an relaxed input section corresponding to an input section
2739 // in OBJECT with index SHNDX.
2740
2741 const Output_relaxed_input_section*
2742 Output_section::find_relaxed_input_section(const Relobj* object,
2743                                            unsigned int shndx) const
2744 {
2745   if (!this->lookup_maps_->is_valid())
2746     this->build_lookup_maps();
2747   return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2748 }
2749
2750 // Given an address OFFSET relative to the start of input section
2751 // SHNDX in OBJECT, return whether this address is being included in
2752 // the final link.  This should only be called if SHNDX in OBJECT has
2753 // a special mapping.
2754
2755 bool
2756 Output_section::is_input_address_mapped(const Relobj* object,
2757                                         unsigned int shndx,
2758                                         off_t offset) const
2759 {
2760   // Look at the Output_section_data_maps first.
2761   const Output_section_data* posd = this->find_merge_section(object, shndx);
2762   if (posd == NULL)
2763     posd = this->find_relaxed_input_section(object, shndx);
2764
2765   if (posd != NULL)
2766     {
2767       section_offset_type output_offset;
2768       bool found = posd->output_offset(object, shndx, offset, &output_offset);
2769       gold_assert(found);   
2770       return output_offset != -1;
2771     }
2772
2773   // Fall back to the slow look-up.
2774   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2775        p != this->input_sections_.end();
2776        ++p)
2777     {
2778       section_offset_type output_offset;
2779       if (p->output_offset(object, shndx, offset, &output_offset))
2780         return output_offset != -1;
2781     }
2782
2783   // By default we assume that the address is mapped.  This should
2784   // only be called after we have passed all sections to Layout.  At
2785   // that point we should know what we are discarding.
2786   return true;
2787 }
2788
2789 // Given an address OFFSET relative to the start of input section
2790 // SHNDX in object OBJECT, return the output offset relative to the
2791 // start of the input section in the output section.  This should only
2792 // be called if SHNDX in OBJECT has a special mapping.
2793
2794 section_offset_type
2795 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2796                               section_offset_type offset) const
2797 {
2798   // This can only be called meaningfully when we know the data size
2799   // of this.
2800   gold_assert(this->is_data_size_valid());
2801
2802   // Look at the Output_section_data_maps first.
2803   const Output_section_data* posd = this->find_merge_section(object, shndx);
2804   if (posd == NULL) 
2805     posd = this->find_relaxed_input_section(object, shndx);
2806   if (posd != NULL)
2807     {
2808       section_offset_type output_offset;
2809       bool found = posd->output_offset(object, shndx, offset, &output_offset);
2810       gold_assert(found);   
2811       return output_offset;
2812     }
2813
2814   // Fall back to the slow look-up.
2815   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2816        p != this->input_sections_.end();
2817        ++p)
2818     {
2819       section_offset_type output_offset;
2820       if (p->output_offset(object, shndx, offset, &output_offset))
2821         return output_offset;
2822     }
2823   gold_unreachable();
2824 }
2825
2826 // Return the output virtual address of OFFSET relative to the start
2827 // of input section SHNDX in object OBJECT.
2828
2829 uint64_t
2830 Output_section::output_address(const Relobj* object, unsigned int shndx,
2831                                off_t offset) const
2832 {
2833   uint64_t addr = this->address() + this->first_input_offset_;
2834
2835   // Look at the Output_section_data_maps first.
2836   const Output_section_data* posd = this->find_merge_section(object, shndx);
2837   if (posd == NULL) 
2838     posd = this->find_relaxed_input_section(object, shndx);
2839   if (posd != NULL && posd->is_address_valid())
2840     {
2841       section_offset_type output_offset;
2842       bool found = posd->output_offset(object, shndx, offset, &output_offset);
2843       gold_assert(found);
2844       return posd->address() + output_offset;
2845     }
2846
2847   // Fall back to the slow look-up.
2848   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2849        p != this->input_sections_.end();
2850        ++p)
2851     {
2852       addr = align_address(addr, p->addralign());
2853       section_offset_type output_offset;
2854       if (p->output_offset(object, shndx, offset, &output_offset))
2855         {
2856           if (output_offset == -1)
2857             return -1ULL;
2858           return addr + output_offset;
2859         }
2860       addr += p->data_size();
2861     }
2862
2863   // If we get here, it means that we don't know the mapping for this
2864   // input section.  This might happen in principle if
2865   // add_input_section were called before add_output_section_data.
2866   // But it should never actually happen.
2867
2868   gold_unreachable();
2869 }
2870
2871 // Find the output address of the start of the merged section for
2872 // input section SHNDX in object OBJECT.
2873
2874 bool
2875 Output_section::find_starting_output_address(const Relobj* object,
2876                                              unsigned int shndx,
2877                                              uint64_t* paddr) const
2878 {
2879   // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2880   // Looking up the merge section map does not always work as we sometimes
2881   // find a merge section without its address set.
2882   uint64_t addr = this->address() + this->first_input_offset_;
2883   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2884        p != this->input_sections_.end();
2885        ++p)
2886     {
2887       addr = align_address(addr, p->addralign());
2888
2889       // It would be nice if we could use the existing output_offset
2890       // method to get the output offset of input offset 0.
2891       // Unfortunately we don't know for sure that input offset 0 is
2892       // mapped at all.
2893       if (p->is_merge_section_for(object, shndx))
2894         {
2895           *paddr = addr;
2896           return true;
2897         }
2898
2899       addr += p->data_size();
2900     }
2901
2902   // We couldn't find a merge output section for this input section.
2903   return false;
2904 }
2905
2906 // Update the data size of an Output_section.
2907
2908 void
2909 Output_section::update_data_size()
2910 {
2911   if (this->input_sections_.empty())
2912       return;
2913
2914   if (this->must_sort_attached_input_sections()
2915       || this->input_section_order_specified())
2916     this->sort_attached_input_sections();
2917
2918   off_t off = this->first_input_offset_;
2919   for (Input_section_list::iterator p = this->input_sections_.begin();
2920        p != this->input_sections_.end();
2921        ++p)
2922     {
2923       off = align_address(off, p->addralign());
2924       off += p->current_data_size();
2925     }
2926
2927   this->set_current_data_size_for_child(off);
2928 }
2929
2930 // Set the data size of an Output_section.  This is where we handle
2931 // setting the addresses of any Output_section_data objects.
2932
2933 void
2934 Output_section::set_final_data_size()
2935 {
2936   if (this->input_sections_.empty())
2937     {
2938       this->set_data_size(this->current_data_size_for_child());
2939       return;
2940     }
2941
2942   if (this->must_sort_attached_input_sections()
2943       || this->input_section_order_specified())
2944     this->sort_attached_input_sections();
2945
2946   uint64_t address = this->address();
2947   off_t startoff = this->offset();
2948   off_t off = startoff + this->first_input_offset_;
2949   for (Input_section_list::iterator p = this->input_sections_.begin();
2950        p != this->input_sections_.end();
2951        ++p)
2952     {
2953       off = align_address(off, p->addralign());
2954       p->set_address_and_file_offset(address + (off - startoff), off,
2955                                      startoff);
2956       off += p->data_size();
2957     }
2958
2959   this->set_data_size(off - startoff);
2960 }
2961
2962 // Reset the address and file offset.
2963
2964 void
2965 Output_section::do_reset_address_and_file_offset()
2966 {
2967   // An unallocated section has no address.  Forcing this means that
2968   // we don't need special treatment for symbols defined in debug
2969   // sections.  We do the same in the constructor.  This does not
2970   // apply to NOLOAD sections though.
2971   if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
2972      this->set_address(0);
2973
2974   for (Input_section_list::iterator p = this->input_sections_.begin();
2975        p != this->input_sections_.end();
2976        ++p)
2977     p->reset_address_and_file_offset();
2978 }
2979   
2980 // Return true if address and file offset have the values after reset.
2981
2982 bool
2983 Output_section::do_address_and_file_offset_have_reset_values() const
2984 {
2985   if (this->is_offset_valid())
2986     return false;
2987
2988   // An unallocated section has address 0 after its construction or a reset.
2989   if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2990     return this->is_address_valid() && this->address() == 0;
2991   else
2992     return !this->is_address_valid();
2993 }
2994
2995 // Set the TLS offset.  Called only for SHT_TLS sections.
2996
2997 void
2998 Output_section::do_set_tls_offset(uint64_t tls_base)
2999 {
3000   this->tls_offset_ = this->address() - tls_base;
3001 }
3002
3003 // In a few cases we need to sort the input sections attached to an
3004 // output section.  This is used to implement the type of constructor
3005 // priority ordering implemented by the GNU linker, in which the
3006 // priority becomes part of the section name and the sections are
3007 // sorted by name.  We only do this for an output section if we see an
3008 // attached input section matching ".ctor.*", ".dtor.*",
3009 // ".init_array.*" or ".fini_array.*".
3010
3011 class Output_section::Input_section_sort_entry
3012 {
3013  public:
3014   Input_section_sort_entry()
3015     : input_section_(), index_(-1U), section_has_name_(false),
3016       section_name_()
3017   { }
3018
3019   Input_section_sort_entry(const Input_section& input_section,
3020                            unsigned int index,
3021                            bool must_sort_attached_input_sections)
3022     : input_section_(input_section), index_(index),
3023       section_has_name_(input_section.is_input_section()
3024                         || input_section.is_relaxed_input_section())
3025   {
3026     if (this->section_has_name_
3027         && must_sort_attached_input_sections)
3028       {
3029         // This is only called single-threaded from Layout::finalize,
3030         // so it is OK to lock.  Unfortunately we have no way to pass
3031         // in a Task token.
3032         const Task* dummy_task = reinterpret_cast<const Task*>(-1);
3033         Object* obj = (input_section.is_input_section()
3034                        ? input_section.relobj()
3035                        : input_section.relaxed_input_section()->relobj());
3036         Task_lock_obj<Object> tl(dummy_task, obj);
3037
3038         // This is a slow operation, which should be cached in
3039         // Layout::layout if this becomes a speed problem.
3040         this->section_name_ = obj->section_name(input_section.shndx());
3041       }
3042   }
3043
3044   // Return the Input_section.
3045   const Input_section&
3046   input_section() const
3047   {
3048     gold_assert(this->index_ != -1U);
3049     return this->input_section_;
3050   }
3051
3052   // The index of this entry in the original list.  This is used to
3053   // make the sort stable.
3054   unsigned int
3055   index() const
3056   {
3057     gold_assert(this->index_ != -1U);
3058     return this->index_;
3059   }
3060
3061   // Whether there is a section name.
3062   bool
3063   section_has_name() const
3064   { return this->section_has_name_; }
3065
3066   // The section name.
3067   const std::string&
3068   section_name() const
3069   {
3070     gold_assert(this->section_has_name_);
3071     return this->section_name_;
3072   }
3073
3074   // Return true if the section name has a priority.  This is assumed
3075   // to be true if it has a dot after the initial dot.
3076   bool
3077   has_priority() const
3078   {
3079     gold_assert(this->section_has_name_);
3080     return this->section_name_.find('.', 1) != std::string::npos;
3081   }
3082
3083   // Return true if this an input file whose base name matches
3084   // FILE_NAME.  The base name must have an extension of ".o", and
3085   // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3086   // This is to match crtbegin.o as well as crtbeginS.o without
3087   // getting confused by other possibilities.  Overall matching the
3088   // file name this way is a dreadful hack, but the GNU linker does it
3089   // in order to better support gcc, and we need to be compatible.
3090   bool
3091   match_file_name(const char* match_file_name) const
3092   {
3093     const std::string& file_name(this->input_section_.relobj()->name());
3094     const char* base_name = lbasename(file_name.c_str());
3095     size_t match_len = strlen(match_file_name);
3096     if (strncmp(base_name, match_file_name, match_len) != 0)
3097       return false;
3098     size_t base_len = strlen(base_name);
3099     if (base_len != match_len + 2 && base_len != match_len + 3)
3100       return false;
3101     return memcmp(base_name + base_len - 2, ".o", 2) == 0;
3102   }
3103
3104   // Returns 1 if THIS should appear before S in section order, -1 if S
3105   // appears before THIS and 0 if they are not comparable.
3106   int
3107   compare_section_ordering(const Input_section_sort_entry& s) const
3108   {
3109     unsigned int this_secn_index = this->input_section_.section_order_index();
3110     unsigned int s_secn_index = s.input_section().section_order_index();
3111     if (this_secn_index > 0 && s_secn_index > 0)
3112       {
3113         if (this_secn_index < s_secn_index)
3114           return 1;
3115         else if (this_secn_index > s_secn_index)
3116           return -1;
3117       }
3118     return 0;
3119   }
3120
3121  private:
3122   // The Input_section we are sorting.
3123   Input_section input_section_;
3124   // The index of this Input_section in the original list.
3125   unsigned int index_;
3126   // Whether this Input_section has a section name--it won't if this
3127   // is some random Output_section_data.
3128   bool section_has_name_;
3129   // The section name if there is one.
3130   std::string section_name_;
3131 };
3132
3133 // Return true if S1 should come before S2 in the output section.
3134
3135 bool
3136 Output_section::Input_section_sort_compare::operator()(
3137     const Output_section::Input_section_sort_entry& s1,
3138     const Output_section::Input_section_sort_entry& s2) const
3139 {
3140   // crtbegin.o must come first.
3141   bool s1_begin = s1.match_file_name("crtbegin");
3142   bool s2_begin = s2.match_file_name("crtbegin");
3143   if (s1_begin || s2_begin)
3144     {
3145       if (!s1_begin)
3146         return false;
3147       if (!s2_begin)
3148         return true;
3149       return s1.index() < s2.index();
3150     }
3151
3152   // crtend.o must come last.
3153   bool s1_end = s1.match_file_name("crtend");
3154   bool s2_end = s2.match_file_name("crtend");
3155   if (s1_end || s2_end)
3156     {
3157       if (!s1_end)
3158         return true;
3159       if (!s2_end)
3160         return false;
3161       return s1.index() < s2.index();
3162     }
3163
3164   // We sort all the sections with no names to the end.
3165   if (!s1.section_has_name() || !s2.section_has_name())
3166     {
3167       if (s1.section_has_name())
3168         return true;
3169       if (s2.section_has_name())
3170         return false;
3171       return s1.index() < s2.index();
3172     }
3173
3174   // A section with a priority follows a section without a priority.
3175   bool s1_has_priority = s1.has_priority();
3176   bool s2_has_priority = s2.has_priority();
3177   if (s1_has_priority && !s2_has_priority)
3178     return false;
3179   if (!s1_has_priority && s2_has_priority)
3180     return true;
3181
3182   // Check if a section order exists for these sections through a section
3183   // ordering file.  If sequence_num is 0, an order does not exist.
3184   int sequence_num = s1.compare_section_ordering(s2);
3185   if (sequence_num != 0)
3186     return sequence_num == 1;
3187
3188   // Otherwise we sort by name.
3189   int compare = s1.section_name().compare(s2.section_name());
3190   if (compare != 0)
3191     return compare < 0;
3192
3193   // Otherwise we keep the input order.
3194   return s1.index() < s2.index();
3195 }
3196
3197 // Return true if S1 should come before S2 in an .init_array or .fini_array
3198 // output section.
3199
3200 bool
3201 Output_section::Input_section_sort_init_fini_compare::operator()(
3202     const Output_section::Input_section_sort_entry& s1,
3203     const Output_section::Input_section_sort_entry& s2) const
3204 {
3205   // We sort all the sections with no names to the end.
3206   if (!s1.section_has_name() || !s2.section_has_name())
3207     {
3208       if (s1.section_has_name())
3209         return true;
3210       if (s2.section_has_name())
3211         return false;
3212       return s1.index() < s2.index();
3213     }
3214
3215   // A section without a priority follows a section with a priority.
3216   // This is the reverse of .ctors and .dtors sections.
3217   bool s1_has_priority = s1.has_priority();
3218   bool s2_has_priority = s2.has_priority();
3219   if (s1_has_priority && !s2_has_priority)
3220     return true;
3221   if (!s1_has_priority && s2_has_priority)
3222     return false;
3223
3224   // Check if a section order exists for these sections through a section
3225   // ordering file.  If sequence_num is 0, an order does not exist.
3226   int sequence_num = s1.compare_section_ordering(s2);
3227   if (sequence_num != 0)
3228     return sequence_num == 1;
3229
3230   // Otherwise we sort by name.
3231   int compare = s1.section_name().compare(s2.section_name());
3232   if (compare != 0)
3233     return compare < 0;
3234
3235   // Otherwise we keep the input order.
3236   return s1.index() < s2.index();
3237 }
3238
3239 // Return true if S1 should come before S2.  Sections that do not match
3240 // any pattern in the section ordering file are placed ahead of the sections
3241 // that match some pattern.
3242
3243 bool
3244 Output_section::Input_section_sort_section_order_index_compare::operator()(
3245     const Output_section::Input_section_sort_entry& s1,
3246     const Output_section::Input_section_sort_entry& s2) const
3247 {
3248   unsigned int s1_secn_index = s1.input_section().section_order_index();
3249   unsigned int s2_secn_index = s2.input_section().section_order_index();
3250
3251   // Keep input order if section ordering cannot determine order.
3252   if (s1_secn_index == s2_secn_index)
3253     return s1.index() < s2.index();
3254   
3255   return s1_secn_index < s2_secn_index;
3256 }
3257
3258 // Sort the input sections attached to an output section.
3259
3260 void
3261 Output_section::sort_attached_input_sections()
3262 {
3263   if (this->attached_input_sections_are_sorted_)
3264     return;
3265
3266   if (this->checkpoint_ != NULL
3267       && !this->checkpoint_->input_sections_saved())
3268     this->checkpoint_->save_input_sections();
3269
3270   // The only thing we know about an input section is the object and
3271   // the section index.  We need the section name.  Recomputing this
3272   // is slow but this is an unusual case.  If this becomes a speed
3273   // problem we can cache the names as required in Layout::layout.
3274
3275   // We start by building a larger vector holding a copy of each
3276   // Input_section, plus its current index in the list and its name.
3277   std::vector<Input_section_sort_entry> sort_list;
3278
3279   unsigned int i = 0;
3280   for (Input_section_list::iterator p = this->input_sections_.begin();
3281        p != this->input_sections_.end();
3282        ++p, ++i)
3283       sort_list.push_back(Input_section_sort_entry(*p, i,
3284                             this->must_sort_attached_input_sections()));
3285
3286   // Sort the input sections.
3287   if (this->must_sort_attached_input_sections())
3288     {
3289       if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3290           || this->type() == elfcpp::SHT_INIT_ARRAY
3291           || this->type() == elfcpp::SHT_FINI_ARRAY)
3292         std::sort(sort_list.begin(), sort_list.end(),
3293                   Input_section_sort_init_fini_compare());
3294       else
3295         std::sort(sort_list.begin(), sort_list.end(),
3296                   Input_section_sort_compare());
3297     }
3298   else
3299     {
3300       gold_assert(parameters->options().section_ordering_file());
3301       std::sort(sort_list.begin(), sort_list.end(),
3302                 Input_section_sort_section_order_index_compare());
3303     }
3304
3305   // Copy the sorted input sections back to our list.
3306   this->input_sections_.clear();
3307   for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3308        p != sort_list.end();
3309        ++p)
3310     this->input_sections_.push_back(p->input_section());
3311   sort_list.clear();
3312
3313   // Remember that we sorted the input sections, since we might get
3314   // called again.
3315   this->attached_input_sections_are_sorted_ = true;
3316 }
3317
3318 // Write the section header to *OSHDR.
3319
3320 template<int size, bool big_endian>
3321 void
3322 Output_section::write_header(const Layout* layout,
3323                              const Stringpool* secnamepool,
3324                              elfcpp::Shdr_write<size, big_endian>* oshdr) const
3325 {
3326   oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3327   oshdr->put_sh_type(this->type_);
3328
3329   elfcpp::Elf_Xword flags = this->flags_;
3330   if (this->info_section_ != NULL && this->info_uses_section_index_)
3331     flags |= elfcpp::SHF_INFO_LINK;
3332   oshdr->put_sh_flags(flags);
3333
3334   oshdr->put_sh_addr(this->address());
3335   oshdr->put_sh_offset(this->offset());
3336   oshdr->put_sh_size(this->data_size());
3337   if (this->link_section_ != NULL)
3338     oshdr->put_sh_link(this->link_section_->out_shndx());
3339   else if (this->should_link_to_symtab_)
3340     oshdr->put_sh_link(layout->symtab_section()->out_shndx());
3341   else if (this->should_link_to_dynsym_)
3342     oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3343   else
3344     oshdr->put_sh_link(this->link_);
3345
3346   elfcpp::Elf_Word info;
3347   if (this->info_section_ != NULL)
3348     {
3349       if (this->info_uses_section_index_)
3350         info = this->info_section_->out_shndx();
3351       else
3352         info = this->info_section_->symtab_index();
3353     }
3354   else if (this->info_symndx_ != NULL)
3355     info = this->info_symndx_->symtab_index();
3356   else
3357     info = this->info_;
3358   oshdr->put_sh_info(info);
3359
3360   oshdr->put_sh_addralign(this->addralign_);
3361   oshdr->put_sh_entsize(this->entsize_);
3362 }
3363
3364 // Write out the data.  For input sections the data is written out by
3365 // Object::relocate, but we have to handle Output_section_data objects
3366 // here.
3367
3368 void
3369 Output_section::do_write(Output_file* of)
3370 {
3371   gold_assert(!this->requires_postprocessing());
3372
3373   // If the target performs relaxation, we delay filler generation until now.
3374   gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3375
3376   off_t output_section_file_offset = this->offset();
3377   for (Fill_list::iterator p = this->fills_.begin();
3378        p != this->fills_.end();
3379        ++p)
3380     {
3381       std::string fill_data(parameters->target().code_fill(p->length()));
3382       of->write(output_section_file_offset + p->section_offset(),
3383                 fill_data.data(), fill_data.size());
3384     }
3385
3386   off_t off = this->offset() + this->first_input_offset_;
3387   for (Input_section_list::iterator p = this->input_sections_.begin();
3388        p != this->input_sections_.end();
3389        ++p)
3390     {
3391       off_t aligned_off = align_address(off, p->addralign());
3392       if (this->generate_code_fills_at_write_ && (off != aligned_off))
3393         {
3394           size_t fill_len = aligned_off - off;
3395           std::string fill_data(parameters->target().code_fill(fill_len));
3396           of->write(off, fill_data.data(), fill_data.size());
3397         }
3398
3399       p->write(of);
3400       off = aligned_off + p->data_size();
3401     }
3402 }
3403
3404 // If a section requires postprocessing, create the buffer to use.
3405
3406 void
3407 Output_section::create_postprocessing_buffer()
3408 {
3409   gold_assert(this->requires_postprocessing());
3410
3411   if (this->postprocessing_buffer_ != NULL)
3412     return;
3413
3414   if (!this->input_sections_.empty())
3415     {
3416       off_t off = this->first_input_offset_;
3417       for (Input_section_list::iterator p = this->input_sections_.begin();
3418            p != this->input_sections_.end();
3419            ++p)
3420         {
3421           off = align_address(off, p->addralign());
3422           p->finalize_data_size();
3423           off += p->data_size();
3424         }
3425       this->set_current_data_size_for_child(off);
3426     }
3427
3428   off_t buffer_size = this->current_data_size_for_child();
3429   this->postprocessing_buffer_ = new unsigned char[buffer_size];
3430 }
3431
3432 // Write all the data of an Output_section into the postprocessing
3433 // buffer.  This is used for sections which require postprocessing,
3434 // such as compression.  Input sections are handled by
3435 // Object::Relocate.
3436
3437 void
3438 Output_section::write_to_postprocessing_buffer()
3439 {
3440   gold_assert(this->requires_postprocessing());
3441
3442   // If the target performs relaxation, we delay filler generation until now.
3443   gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3444
3445   unsigned char* buffer = this->postprocessing_buffer();
3446   for (Fill_list::iterator p = this->fills_.begin();
3447        p != this->fills_.end();
3448        ++p)
3449     {
3450       std::string fill_data(parameters->target().code_fill(p->length()));
3451       memcpy(buffer + p->section_offset(), fill_data.data(),
3452              fill_data.size());
3453     }
3454
3455   off_t off = this->first_input_offset_;
3456   for (Input_section_list::iterator p = this->input_sections_.begin();
3457        p != this->input_sections_.end();
3458        ++p)
3459     {
3460       off_t aligned_off = align_address(off, p->addralign());
3461       if (this->generate_code_fills_at_write_ && (off != aligned_off))
3462         {
3463           size_t fill_len = aligned_off - off;
3464           std::string fill_data(parameters->target().code_fill(fill_len));
3465           memcpy(buffer + off, fill_data.data(), fill_data.size());
3466         }
3467
3468       p->write_to_buffer(buffer + aligned_off);
3469       off = aligned_off + p->data_size();
3470     }
3471 }
3472
3473 // Get the input sections for linker script processing.  We leave
3474 // behind the Output_section_data entries.  Note that this may be
3475 // slightly incorrect for merge sections.  We will leave them behind,
3476 // but it is possible that the script says that they should follow
3477 // some other input sections, as in:
3478 //    .rodata { *(.rodata) *(.rodata.cst*) }
3479 // For that matter, we don't handle this correctly:
3480 //    .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3481 // With luck this will never matter.
3482
3483 uint64_t
3484 Output_section::get_input_sections(
3485     uint64_t address,
3486     const std::string& fill,
3487     std::list<Input_section>* input_sections)
3488 {
3489   if (this->checkpoint_ != NULL
3490       && !this->checkpoint_->input_sections_saved())
3491     this->checkpoint_->save_input_sections();
3492
3493   // Invalidate fast look-up maps.
3494   this->lookup_maps_->invalidate();
3495
3496   uint64_t orig_address = address;
3497
3498   address = align_address(address, this->addralign());
3499
3500   Input_section_list remaining;
3501   for (Input_section_list::iterator p = this->input_sections_.begin();
3502        p != this->input_sections_.end();
3503        ++p)
3504     {
3505       if (p->is_input_section()
3506           || p->is_relaxed_input_section()
3507           || p->is_merge_section())
3508         input_sections->push_back(*p);
3509       else
3510         {
3511           uint64_t aligned_address = align_address(address, p->addralign());
3512           if (aligned_address != address && !fill.empty())
3513             {
3514               section_size_type length =
3515                 convert_to_section_size_type(aligned_address - address);
3516               std::string this_fill;
3517               this_fill.reserve(length);
3518               while (this_fill.length() + fill.length() <= length)
3519                 this_fill += fill;
3520               if (this_fill.length() < length)
3521                 this_fill.append(fill, 0, length - this_fill.length());
3522
3523               Output_section_data* posd = new Output_data_const(this_fill, 0);
3524               remaining.push_back(Input_section(posd));
3525             }
3526           address = aligned_address;
3527
3528           remaining.push_back(*p);
3529
3530           p->finalize_data_size();
3531           address += p->data_size();
3532         }
3533     }
3534
3535   this->input_sections_.swap(remaining);
3536   this->first_input_offset_ = 0;
3537
3538   uint64_t data_size = address - orig_address;
3539   this->set_current_data_size_for_child(data_size);
3540   return data_size;
3541 }
3542
3543 // Add a script input section.  SIS is an Output_section::Input_section,
3544 // which can be either a plain input section or a special input section like
3545 // a relaxed input section.  For a special input section, its size must be
3546 // finalized.
3547
3548 void
3549 Output_section::add_script_input_section(const Input_section& sis)
3550 {
3551   uint64_t data_size = sis.data_size();
3552   uint64_t addralign = sis.addralign();
3553   if (addralign > this->addralign_)
3554     this->addralign_ = addralign;
3555
3556   off_t offset_in_section = this->current_data_size_for_child();
3557   off_t aligned_offset_in_section = align_address(offset_in_section,
3558                                                   addralign);
3559
3560   this->set_current_data_size_for_child(aligned_offset_in_section
3561                                         + data_size);
3562
3563   this->input_sections_.push_back(sis);
3564
3565   // Update fast lookup maps if necessary. 
3566   if (this->lookup_maps_->is_valid())
3567     {
3568       if (sis.is_merge_section())
3569         {
3570           Output_merge_base* pomb = sis.output_merge_base();
3571           Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3572                                        pomb->addralign());
3573           this->lookup_maps_->add_merge_section(msp, pomb);
3574           for (Output_merge_base::Input_sections::const_iterator p =
3575                  pomb->input_sections_begin();
3576                p != pomb->input_sections_end();
3577                ++p)
3578             this->lookup_maps_->add_merge_input_section(p->first, p->second,
3579                                                         pomb);
3580         }
3581       else if (sis.is_relaxed_input_section())
3582         {
3583           Output_relaxed_input_section* poris = sis.relaxed_input_section();
3584           this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3585                                                         poris->shndx(), poris);
3586         }
3587     }
3588 }
3589
3590 // Save states for relaxation.
3591
3592 void
3593 Output_section::save_states()
3594 {
3595   gold_assert(this->checkpoint_ == NULL);
3596   Checkpoint_output_section* checkpoint =
3597     new Checkpoint_output_section(this->addralign_, this->flags_,
3598                                   this->input_sections_,
3599                                   this->first_input_offset_,
3600                                   this->attached_input_sections_are_sorted_);
3601   this->checkpoint_ = checkpoint;
3602   gold_assert(this->fills_.empty());
3603 }
3604
3605 void
3606 Output_section::discard_states()
3607 {
3608   gold_assert(this->checkpoint_ != NULL);
3609   delete this->checkpoint_;
3610   this->checkpoint_ = NULL;
3611   gold_assert(this->fills_.empty());
3612
3613   // Simply invalidate the fast lookup maps since we do not keep
3614   // track of them.
3615   this->lookup_maps_->invalidate();
3616 }
3617
3618 void
3619 Output_section::restore_states()
3620 {
3621   gold_assert(this->checkpoint_ != NULL);
3622   Checkpoint_output_section* checkpoint = this->checkpoint_;
3623
3624   this->addralign_ = checkpoint->addralign();
3625   this->flags_ = checkpoint->flags();
3626   this->first_input_offset_ = checkpoint->first_input_offset();
3627
3628   if (!checkpoint->input_sections_saved())
3629     {
3630       // If we have not copied the input sections, just resize it.
3631       size_t old_size = checkpoint->input_sections_size();
3632       gold_assert(this->input_sections_.size() >= old_size);
3633       this->input_sections_.resize(old_size);
3634     }
3635   else
3636     {
3637       // We need to copy the whole list.  This is not efficient for
3638       // extremely large output with hundreads of thousands of input
3639       // objects.  We may need to re-think how we should pass sections
3640       // to scripts.
3641       this->input_sections_ = *checkpoint->input_sections();
3642     }
3643
3644   this->attached_input_sections_are_sorted_ =
3645     checkpoint->attached_input_sections_are_sorted();
3646
3647   // Simply invalidate the fast lookup maps since we do not keep
3648   // track of them.
3649   this->lookup_maps_->invalidate();
3650 }
3651
3652 // Update the section offsets of input sections in this.  This is required if
3653 // relaxation causes some input sections to change sizes.
3654
3655 void
3656 Output_section::adjust_section_offsets()
3657 {
3658   if (!this->section_offsets_need_adjustment_)
3659     return;
3660
3661   off_t off = 0;
3662   for (Input_section_list::iterator p = this->input_sections_.begin();
3663        p != this->input_sections_.end();
3664        ++p)
3665     {
3666       off = align_address(off, p->addralign());
3667       if (p->is_input_section())
3668         p->relobj()->set_section_offset(p->shndx(), off);
3669       off += p->data_size();
3670     }
3671
3672   this->section_offsets_need_adjustment_ = false;
3673 }
3674
3675 // Print to the map file.
3676
3677 void
3678 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3679 {
3680   mapfile->print_output_section(this);
3681
3682   for (Input_section_list::const_iterator p = this->input_sections_.begin();
3683        p != this->input_sections_.end();
3684        ++p)
3685     p->print_to_mapfile(mapfile);
3686 }
3687
3688 // Print stats for merge sections to stderr.
3689
3690 void
3691 Output_section::print_merge_stats()
3692 {
3693   Input_section_list::iterator p;
3694   for (p = this->input_sections_.begin();
3695        p != this->input_sections_.end();
3696        ++p)
3697     p->print_merge_stats(this->name_);
3698 }
3699
3700 // Set a fixed layout for the section.  Used for incremental update links.
3701
3702 void
3703 Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3704                                  off_t sh_size, uint64_t sh_addralign)
3705 {
3706   this->addralign_ = sh_addralign;
3707   this->set_current_data_size(sh_size);
3708   if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
3709     this->set_address(sh_addr);
3710   this->set_file_offset(sh_offset);
3711   this->finalize_data_size();
3712   this->free_list_.init(sh_size, false);
3713   this->has_fixed_layout_ = true;
3714 }
3715
3716 // Reserve space within the fixed layout for the section.  Used for
3717 // incremental update links.
3718 void
3719 Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
3720 {
3721   this->free_list_.remove(sh_offset, sh_offset + sh_size);
3722 }
3723
3724 // Output segment methods.
3725
3726 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3727   : vaddr_(0),
3728     paddr_(0),
3729     memsz_(0),
3730     max_align_(0),
3731     min_p_align_(0),
3732     offset_(0),
3733     filesz_(0),
3734     type_(type),
3735     flags_(flags),
3736     is_max_align_known_(false),
3737     are_addresses_set_(false),
3738     is_large_data_segment_(false)
3739 {
3740   // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3741   // the flags.
3742   if (type == elfcpp::PT_TLS)
3743     this->flags_ = elfcpp::PF_R;
3744 }
3745
3746 // Add an Output_section to a PT_LOAD Output_segment.
3747
3748 void
3749 Output_segment::add_output_section_to_load(Layout* layout,
3750                                            Output_section* os,
3751                                            elfcpp::Elf_Word seg_flags)
3752 {
3753   gold_assert(this->type() == elfcpp::PT_LOAD);
3754   gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3755   gold_assert(!this->is_max_align_known_);
3756   gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3757
3758   this->update_flags_for_output_section(seg_flags);
3759
3760   // We don't want to change the ordering if we have a linker script
3761   // with a SECTIONS clause.
3762   Output_section_order order = os->order();
3763   if (layout->script_options()->saw_sections_clause())
3764     order = static_cast<Output_section_order>(0);
3765   else
3766     gold_assert(order != ORDER_INVALID);
3767
3768   this->output_lists_[order].push_back(os);
3769 }
3770
3771 // Add an Output_section to a non-PT_LOAD Output_segment.
3772
3773 void
3774 Output_segment::add_output_section_to_nonload(Output_section* os,
3775                                               elfcpp::Elf_Word seg_flags)
3776 {
3777   gold_assert(this->type() != elfcpp::PT_LOAD);
3778   gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3779   gold_assert(!this->is_max_align_known_);
3780
3781   this->update_flags_for_output_section(seg_flags);
3782
3783   this->output_lists_[0].push_back(os);
3784 }
3785
3786 // Remove an Output_section from this segment.  It is an error if it
3787 // is not present.
3788
3789 void
3790 Output_segment::remove_output_section(Output_section* os)
3791 {
3792   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3793     {
3794       Output_data_list* pdl = &this->output_lists_[i];
3795       for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
3796         {
3797           if (*p == os)
3798             {
3799               pdl->erase(p);
3800               return;
3801             }
3802         }
3803     }
3804   gold_unreachable();
3805 }
3806
3807 // Add an Output_data (which need not be an Output_section) to the
3808 // start of a segment.
3809
3810 void
3811 Output_segment::add_initial_output_data(Output_data* od)
3812 {
3813   gold_assert(!this->is_max_align_known_);
3814   Output_data_list::iterator p = this->output_lists_[0].begin();
3815   this->output_lists_[0].insert(p, od);
3816 }
3817
3818 // Return true if this segment has any sections which hold actual
3819 // data, rather than being a BSS section.
3820
3821 bool
3822 Output_segment::has_any_data_sections() const
3823 {
3824   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3825     {
3826       const Output_data_list* pdl = &this->output_lists_[i];
3827       for (Output_data_list::const_iterator p = pdl->begin();
3828            p != pdl->end();
3829            ++p)
3830         {
3831           if (!(*p)->is_section())
3832             return true;
3833           if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
3834             return true;
3835         }
3836     }
3837   return false;
3838 }
3839
3840 // Return whether the first data section (not counting TLS sections)
3841 // is a relro section.
3842
3843 bool
3844 Output_segment::is_first_section_relro() const
3845 {
3846   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3847     {
3848       if (i == static_cast<int>(ORDER_TLS_DATA)
3849           || i == static_cast<int>(ORDER_TLS_BSS))
3850         continue;
3851       const Output_data_list* pdl = &this->output_lists_[i];
3852       if (!pdl->empty())
3853         {
3854           Output_data* p = pdl->front();
3855           return p->is_section() && p->output_section()->is_relro();
3856         }
3857     }
3858   return false;
3859 }
3860
3861 // Return the maximum alignment of the Output_data in Output_segment.
3862
3863 uint64_t
3864 Output_segment::maximum_alignment()
3865 {
3866   if (!this->is_max_align_known_)
3867     {
3868       for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3869         {       
3870           const Output_data_list* pdl = &this->output_lists_[i];
3871           uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
3872           if (addralign > this->max_align_)
3873             this->max_align_ = addralign;
3874         }
3875       this->is_max_align_known_ = true;
3876     }
3877
3878   return this->max_align_;
3879 }
3880
3881 // Return the maximum alignment of a list of Output_data.
3882
3883 uint64_t
3884 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
3885 {
3886   uint64_t ret = 0;
3887   for (Output_data_list::const_iterator p = pdl->begin();
3888        p != pdl->end();
3889        ++p)
3890     {
3891       uint64_t addralign = (*p)->addralign();
3892       if (addralign > ret)
3893         ret = addralign;
3894     }
3895   return ret;
3896 }
3897
3898 // Return whether this segment has any dynamic relocs.
3899
3900 bool
3901 Output_segment::has_dynamic_reloc() const
3902 {
3903   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
3904     if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
3905       return true;
3906   return false;
3907 }
3908
3909 // Return whether this Output_data_list has any dynamic relocs.
3910
3911 bool
3912 Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
3913 {
3914   for (Output_data_list::const_iterator p = pdl->begin();
3915        p != pdl->end();
3916        ++p)
3917     if ((*p)->has_dynamic_reloc())
3918       return true;
3919   return false;
3920 }
3921
3922 // Set the section addresses for an Output_segment.  If RESET is true,
3923 // reset the addresses first.  ADDR is the address and *POFF is the
3924 // file offset.  Set the section indexes starting with *PSHNDX.
3925 // INCREASE_RELRO is the size of the portion of the first non-relro
3926 // section that should be included in the PT_GNU_RELRO segment.
3927 // If this segment has relro sections, and has been aligned for
3928 // that purpose, set *HAS_RELRO to TRUE.  Return the address of
3929 // the immediately following segment.  Update *HAS_RELRO, *POFF,
3930 // and *PSHNDX.
3931
3932 uint64_t
3933 Output_segment::set_section_addresses(Layout* layout, bool reset,
3934                                       uint64_t addr,
3935                                       unsigned int* increase_relro,
3936                                       bool* has_relro,
3937                                       off_t* poff,
3938                                       unsigned int* pshndx)
3939 {
3940   gold_assert(this->type_ == elfcpp::PT_LOAD);
3941
3942   uint64_t last_relro_pad = 0;
3943   off_t orig_off = *poff;
3944
3945   bool in_tls = false;
3946
3947   // If we have relro sections, we need to pad forward now so that the
3948   // relro sections plus INCREASE_RELRO end on a common page boundary.
3949   if (parameters->options().relro()
3950       && this->is_first_section_relro()
3951       && (!this->are_addresses_set_ || reset))
3952     {
3953       uint64_t relro_size = 0;
3954       off_t off = *poff;
3955       uint64_t max_align = 0;
3956       for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
3957         {
3958           Output_data_list* pdl = &this->output_lists_[i];
3959           Output_data_list::iterator p;
3960           for (p = pdl->begin(); p != pdl->end(); ++p)
3961             {
3962               if (!(*p)->is_section())
3963                 break;
3964               uint64_t align = (*p)->addralign();
3965               if (align > max_align)
3966                 max_align = align;
3967               if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3968                 in_tls = true;
3969               else if (in_tls)
3970                 {
3971                   // Align the first non-TLS section to the alignment
3972                   // of the TLS segment.
3973                   align = max_align;
3974                   in_tls = false;
3975                 }
3976               relro_size = align_address(relro_size, align);
3977               // Ignore the size of the .tbss section.
3978               if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
3979                   && (*p)->is_section_type(elfcpp::SHT_NOBITS))
3980                 continue;
3981               if ((*p)->is_address_valid())
3982                 relro_size += (*p)->data_size();
3983               else
3984                 {
3985                   // FIXME: This could be faster.
3986                   (*p)->set_address_and_file_offset(addr + relro_size,
3987                                                     off + relro_size);
3988                   relro_size += (*p)->data_size();
3989                   (*p)->reset_address_and_file_offset();
3990                 }
3991             }
3992           if (p != pdl->end())
3993             break;
3994         }
3995       relro_size += *increase_relro;
3996       // Pad the total relro size to a multiple of the maximum
3997       // section alignment seen.
3998       uint64_t aligned_size = align_address(relro_size, max_align);
3999       // Note the amount of padding added after the last relro section.
4000       last_relro_pad = aligned_size - relro_size;
4001       *has_relro = true;
4002
4003       uint64_t page_align = parameters->target().common_pagesize();
4004
4005       // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4006       uint64_t desired_align = page_align - (aligned_size % page_align);
4007       if (desired_align < *poff % page_align)
4008         *poff += page_align - *poff % page_align;
4009       *poff += desired_align - *poff % page_align;
4010       addr += *poff - orig_off;
4011       orig_off = *poff;
4012     }
4013
4014   if (!reset && this->are_addresses_set_)
4015     {
4016       gold_assert(this->paddr_ == addr);
4017       addr = this->vaddr_;
4018     }
4019   else
4020     {
4021       this->vaddr_ = addr;
4022       this->paddr_ = addr;
4023       this->are_addresses_set_ = true;
4024     }
4025
4026   in_tls = false;
4027
4028   this->offset_ = orig_off;
4029
4030   off_t off = 0;
4031   uint64_t ret;
4032   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4033     {
4034       if (i == static_cast<int>(ORDER_RELRO_LAST))
4035         {
4036           *poff += last_relro_pad;
4037           addr += last_relro_pad;
4038           if (this->output_lists_[i].empty())
4039             {
4040               // If there is nothing in the ORDER_RELRO_LAST list,
4041               // the padding will occur at the end of the relro
4042               // segment, and we need to add it to *INCREASE_RELRO.
4043               *increase_relro += last_relro_pad;
4044             }
4045         }
4046       addr = this->set_section_list_addresses(layout, reset,
4047                                               &this->output_lists_[i],
4048                                               addr, poff, pshndx, &in_tls);
4049       if (i < static_cast<int>(ORDER_SMALL_BSS))
4050         {
4051           this->filesz_ = *poff - orig_off;
4052           off = *poff;
4053         }
4054
4055       ret = addr;
4056     }
4057
4058   // If the last section was a TLS section, align upward to the
4059   // alignment of the TLS segment, so that the overall size of the TLS
4060   // segment is aligned.
4061   if (in_tls)
4062     {
4063       uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4064       *poff = align_address(*poff, segment_align);
4065     }
4066
4067   this->memsz_ = *poff - orig_off;
4068
4069   // Ignore the file offset adjustments made by the BSS Output_data
4070   // objects.
4071   *poff = off;
4072
4073   return ret;
4074 }
4075
4076 // Set the addresses and file offsets in a list of Output_data
4077 // structures.
4078
4079 uint64_t
4080 Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4081                                            Output_data_list* pdl,
4082                                            uint64_t addr, off_t* poff,
4083                                            unsigned int* pshndx,
4084                                            bool* in_tls)
4085 {
4086   off_t startoff = *poff;
4087   // For incremental updates, we may allocate non-fixed sections from
4088   // free space in the file.  This keeps track of the high-water mark.
4089   off_t maxoff = startoff;
4090
4091   off_t off = startoff;
4092   for (Output_data_list::iterator p = pdl->begin();
4093        p != pdl->end();
4094        ++p)
4095     {
4096       if (reset)
4097         (*p)->reset_address_and_file_offset();
4098
4099       // When doing an incremental update or when using a linker script,
4100       // the section will most likely already have an address.
4101       if (!(*p)->is_address_valid())
4102         {
4103           uint64_t align = (*p)->addralign();
4104
4105           if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4106             {
4107               // Give the first TLS section the alignment of the
4108               // entire TLS segment.  Otherwise the TLS segment as a
4109               // whole may be misaligned.
4110               if (!*in_tls)
4111                 {
4112                   Output_segment* tls_segment = layout->tls_segment();
4113                   gold_assert(tls_segment != NULL);
4114                   uint64_t segment_align = tls_segment->maximum_alignment();
4115                   gold_assert(segment_align >= align);
4116                   align = segment_align;
4117
4118                   *in_tls = true;
4119                 }
4120             }
4121           else
4122             {
4123               // If this is the first section after the TLS segment,
4124               // align it to at least the alignment of the TLS
4125               // segment, so that the size of the overall TLS segment
4126               // is aligned.
4127               if (*in_tls)
4128                 {
4129                   uint64_t segment_align =
4130                       layout->tls_segment()->maximum_alignment();
4131                   if (segment_align > align)
4132                     align = segment_align;
4133
4134                   *in_tls = false;
4135                 }
4136             }
4137
4138           // FIXME: Need to handle TLS and .bss with incremental update.
4139           if (!parameters->incremental_update()
4140               || (*p)->is_section_flag_set(elfcpp::SHF_TLS)
4141               || (*p)->is_section_type(elfcpp::SHT_NOBITS))
4142             {
4143               off = align_address(off, align);
4144               (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4145             }
4146           else
4147             {
4148               // Incremental update: allocate file space from free list.
4149               (*p)->pre_finalize_data_size();
4150               off_t current_size = (*p)->current_data_size();
4151               off = layout->allocate(current_size, align, startoff);
4152               if (off == -1)
4153                 {
4154                   gold_assert((*p)->output_section() != NULL);
4155                   gold_fatal(_("out of patch space for section %s; "
4156                                "relink with --incremental-full"),
4157                              (*p)->output_section()->name());
4158                 }
4159               (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4160               if ((*p)->data_size() > current_size)
4161                 {
4162                   gold_assert((*p)->output_section() != NULL);
4163                   gold_fatal(_("%s: section changed size; "
4164                                "relink with --incremental-full"),
4165                              (*p)->output_section()->name());
4166                 }
4167             }
4168         }
4169       else if (parameters->incremental_update())
4170         {
4171           // For incremental updates, use the fixed offset for the
4172           // high-water mark computation.
4173           off = (*p)->offset();
4174         }
4175       else
4176         {
4177           // The script may have inserted a skip forward, but it
4178           // better not have moved backward.
4179           if ((*p)->address() >= addr + (off - startoff))
4180             off += (*p)->address() - (addr + (off - startoff));
4181           else
4182             {
4183               if (!layout->script_options()->saw_sections_clause())
4184                 gold_unreachable();
4185               else
4186                 {
4187                   Output_section* os = (*p)->output_section();
4188
4189                   // Cast to unsigned long long to avoid format warnings.
4190                   unsigned long long previous_dot =
4191                     static_cast<unsigned long long>(addr + (off - startoff));
4192                   unsigned long long dot =
4193                     static_cast<unsigned long long>((*p)->address());
4194
4195                   if (os == NULL)
4196                     gold_error(_("dot moves backward in linker script "
4197                                  "from 0x%llx to 0x%llx"), previous_dot, dot);
4198                   else
4199                     gold_error(_("address of section '%s' moves backward "
4200                                  "from 0x%llx to 0x%llx"),
4201                                os->name(), previous_dot, dot);
4202                 }
4203             }
4204           (*p)->set_file_offset(off);
4205           (*p)->finalize_data_size();
4206         }
4207
4208       gold_debug(DEBUG_INCREMENTAL,
4209                  "set_section_list_addresses: %08lx %08lx %s",
4210                  static_cast<long>(off),
4211                  static_cast<long>((*p)->data_size()),
4212                  ((*p)->output_section() != NULL
4213                   ? (*p)->output_section()->name() : "(special)"));
4214
4215       // We want to ignore the size of a SHF_TLS or SHT_NOBITS
4216       // section.  Such a section does not affect the size of a
4217       // PT_LOAD segment.
4218       if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
4219           || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
4220         off += (*p)->data_size();
4221
4222       if (off > maxoff)
4223         maxoff = off;
4224
4225       if ((*p)->is_section())
4226         {
4227           (*p)->set_out_shndx(*pshndx);
4228           ++*pshndx;
4229         }
4230     }
4231
4232   *poff = maxoff;
4233   return addr + (maxoff - startoff);
4234 }
4235
4236 // For a non-PT_LOAD segment, set the offset from the sections, if
4237 // any.  Add INCREASE to the file size and the memory size.
4238
4239 void
4240 Output_segment::set_offset(unsigned int increase)
4241 {
4242   gold_assert(this->type_ != elfcpp::PT_LOAD);
4243
4244   gold_assert(!this->are_addresses_set_);
4245
4246   // A non-load section only uses output_lists_[0].
4247
4248   Output_data_list* pdl = &this->output_lists_[0];
4249
4250   if (pdl->empty())
4251     {
4252       gold_assert(increase == 0);
4253       this->vaddr_ = 0;
4254       this->paddr_ = 0;
4255       this->are_addresses_set_ = true;
4256       this->memsz_ = 0;
4257       this->min_p_align_ = 0;
4258       this->offset_ = 0;
4259       this->filesz_ = 0;
4260       return;
4261     }
4262
4263   // Find the first and last section by address.
4264   const Output_data* first = NULL;
4265   const Output_data* last_data = NULL;
4266   const Output_data* last_bss = NULL;
4267   for (Output_data_list::const_iterator p = pdl->begin();
4268        p != pdl->end();
4269        ++p)
4270     {
4271       if (first == NULL
4272           || (*p)->address() < first->address()
4273           || ((*p)->address() == first->address()
4274               && (*p)->data_size() < first->data_size()))
4275         first = *p;
4276       const Output_data** plast;
4277       if ((*p)->is_section()
4278           && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4279         plast = &last_bss;
4280       else
4281         plast = &last_data;
4282       if (*plast == NULL
4283           || (*p)->address() > (*plast)->address()
4284           || ((*p)->address() == (*plast)->address()
4285               && (*p)->data_size() > (*plast)->data_size()))
4286         *plast = *p;
4287     }
4288
4289   this->vaddr_ = first->address();
4290   this->paddr_ = (first->has_load_address()
4291                   ? first->load_address()
4292                   : this->vaddr_);
4293   this->are_addresses_set_ = true;
4294   this->offset_ = first->offset();
4295
4296   if (last_data == NULL)
4297     this->filesz_ = 0;
4298   else
4299     this->filesz_ = (last_data->address()
4300                      + last_data->data_size()
4301                      - this->vaddr_);
4302
4303   const Output_data* last = last_bss != NULL ? last_bss : last_data;
4304   this->memsz_ = (last->address()
4305                   + last->data_size()
4306                   - this->vaddr_);
4307
4308   this->filesz_ += increase;
4309   this->memsz_ += increase;
4310
4311   // If this is a RELRO segment, verify that the segment ends at a
4312   // page boundary.
4313   if (this->type_ == elfcpp::PT_GNU_RELRO)
4314     {
4315       uint64_t page_align = parameters->target().common_pagesize();
4316       uint64_t segment_end = this->vaddr_ + this->memsz_;
4317       if (parameters->incremental_update())
4318         {
4319           // The INCREASE_RELRO calculation is bypassed for an incremental
4320           // update, so we need to adjust the segment size manually here.
4321           segment_end = align_address(segment_end, page_align);
4322           this->memsz_ = segment_end - this->vaddr_;
4323         }
4324       else
4325         gold_assert(segment_end == align_address(segment_end, page_align));
4326     }
4327
4328   // If this is a TLS segment, align the memory size.  The code in
4329   // set_section_list ensures that the section after the TLS segment
4330   // is aligned to give us room.
4331   if (this->type_ == elfcpp::PT_TLS)
4332     {
4333       uint64_t segment_align = this->maximum_alignment();
4334       gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4335       this->memsz_ = align_address(this->memsz_, segment_align);
4336     }
4337 }
4338
4339 // Set the TLS offsets of the sections in the PT_TLS segment.
4340
4341 void
4342 Output_segment::set_tls_offsets()
4343 {
4344   gold_assert(this->type_ == elfcpp::PT_TLS);
4345
4346   for (Output_data_list::iterator p = this->output_lists_[0].begin();
4347        p != this->output_lists_[0].end();
4348        ++p)
4349     (*p)->set_tls_offset(this->vaddr_);
4350 }
4351
4352 // Return the load address of the first section.
4353
4354 uint64_t
4355 Output_segment::first_section_load_address() const
4356 {
4357   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4358     {
4359       const Output_data_list* pdl = &this->output_lists_[i];
4360       for (Output_data_list::const_iterator p = pdl->begin();
4361            p != pdl->end();
4362            ++p)
4363         {
4364           if ((*p)->is_section())
4365             return ((*p)->has_load_address()
4366                     ? (*p)->load_address()
4367                     : (*p)->address());
4368         }
4369     }
4370   gold_unreachable();
4371 }
4372
4373 // Return the number of Output_sections in an Output_segment.
4374
4375 unsigned int
4376 Output_segment::output_section_count() const
4377 {
4378   unsigned int ret = 0;
4379   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4380     ret += this->output_section_count_list(&this->output_lists_[i]);
4381   return ret;
4382 }
4383
4384 // Return the number of Output_sections in an Output_data_list.
4385
4386 unsigned int
4387 Output_segment::output_section_count_list(const Output_data_list* pdl) const
4388 {
4389   unsigned int count = 0;
4390   for (Output_data_list::const_iterator p = pdl->begin();
4391        p != pdl->end();
4392        ++p)
4393     {
4394       if ((*p)->is_section())
4395         ++count;
4396     }
4397   return count;
4398 }
4399
4400 // Return the section attached to the list segment with the lowest
4401 // load address.  This is used when handling a PHDRS clause in a
4402 // linker script.
4403
4404 Output_section*
4405 Output_segment::section_with_lowest_load_address() const
4406 {
4407   Output_section* found = NULL;
4408   uint64_t found_lma = 0;
4409   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4410     this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4411                                       &found_lma);
4412   return found;
4413 }
4414
4415 // Look through a list for a section with a lower load address.
4416
4417 void
4418 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4419                                             Output_section** found,
4420                                             uint64_t* found_lma) const
4421 {
4422   for (Output_data_list::const_iterator p = pdl->begin();
4423        p != pdl->end();
4424        ++p)
4425     {
4426       if (!(*p)->is_section())
4427         continue;
4428       Output_section* os = static_cast<Output_section*>(*p);
4429       uint64_t lma = (os->has_load_address()
4430                       ? os->load_address()
4431                       : os->address());
4432       if (*found == NULL || lma < *found_lma)
4433         {
4434           *found = os;
4435           *found_lma = lma;
4436         }
4437     }
4438 }
4439
4440 // Write the segment data into *OPHDR.
4441
4442 template<int size, bool big_endian>
4443 void
4444 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4445 {
4446   ophdr->put_p_type(this->type_);
4447   ophdr->put_p_offset(this->offset_);
4448   ophdr->put_p_vaddr(this->vaddr_);
4449   ophdr->put_p_paddr(this->paddr_);
4450   ophdr->put_p_filesz(this->filesz_);
4451   ophdr->put_p_memsz(this->memsz_);
4452   ophdr->put_p_flags(this->flags_);
4453   ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4454 }
4455
4456 // Write the section headers into V.
4457
4458 template<int size, bool big_endian>
4459 unsigned char*
4460 Output_segment::write_section_headers(const Layout* layout,
4461                                       const Stringpool* secnamepool,
4462                                       unsigned char* v,
4463                                       unsigned int* pshndx) const
4464 {
4465   // Every section that is attached to a segment must be attached to a
4466   // PT_LOAD segment, so we only write out section headers for PT_LOAD
4467   // segments.
4468   if (this->type_ != elfcpp::PT_LOAD)
4469     return v;
4470
4471   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4472     {
4473       const Output_data_list* pdl = &this->output_lists_[i];
4474       v = this->write_section_headers_list<size, big_endian>(layout,
4475                                                              secnamepool,
4476                                                              pdl,
4477                                                              v, pshndx);
4478     }
4479
4480   return v;
4481 }
4482
4483 template<int size, bool big_endian>
4484 unsigned char*
4485 Output_segment::write_section_headers_list(const Layout* layout,
4486                                            const Stringpool* secnamepool,
4487                                            const Output_data_list* pdl,
4488                                            unsigned char* v,
4489                                            unsigned int* pshndx) const
4490 {
4491   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4492   for (Output_data_list::const_iterator p = pdl->begin();
4493        p != pdl->end();
4494        ++p)
4495     {
4496       if ((*p)->is_section())
4497         {
4498           const Output_section* ps = static_cast<const Output_section*>(*p);
4499           gold_assert(*pshndx == ps->out_shndx());
4500           elfcpp::Shdr_write<size, big_endian> oshdr(v);
4501           ps->write_header(layout, secnamepool, &oshdr);
4502           v += shdr_size;
4503           ++*pshndx;
4504         }
4505     }
4506   return v;
4507 }
4508
4509 // Print the output sections to the map file.
4510
4511 void
4512 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4513 {
4514   if (this->type() != elfcpp::PT_LOAD)
4515     return;
4516   for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4517     this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4518 }
4519
4520 // Print an output section list to the map file.
4521
4522 void
4523 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4524                                               const Output_data_list* pdl) const
4525 {
4526   for (Output_data_list::const_iterator p = pdl->begin();
4527        p != pdl->end();
4528        ++p)
4529     (*p)->print_to_mapfile(mapfile);
4530 }
4531
4532 // Output_file methods.
4533
4534 Output_file::Output_file(const char* name)
4535   : name_(name),
4536     o_(-1),
4537     file_size_(0),
4538     base_(NULL),
4539     map_is_anonymous_(false),
4540     map_is_allocated_(false),
4541     is_temporary_(false)
4542 {
4543 }
4544
4545 // Try to open an existing file.  Returns false if the file doesn't
4546 // exist, has a size of 0 or can't be mmapped.
4547
4548 bool
4549 Output_file::open_for_modification()
4550 {
4551   // The name "-" means "stdout".
4552   if (strcmp(this->name_, "-") == 0)
4553     return false;
4554
4555   // Don't bother opening files with a size of zero.
4556   struct stat s;
4557   if (::stat(this->name_, &s) != 0 || s.st_size == 0)
4558     return false;
4559
4560   int o = open_descriptor(-1, this->name_, O_RDWR, 0);
4561   if (o < 0)
4562     gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4563   this->o_ = o;
4564   this->file_size_ = s.st_size;
4565
4566   // If the file can't be mmapped, copying the content to an anonymous
4567   // map will probably negate the performance benefits of incremental
4568   // linking.  This could be helped by using views and loading only
4569   // the necessary parts, but this is not supported as of now.
4570   if (!this->map_no_anonymous())
4571     {
4572       release_descriptor(o, true);
4573       this->o_ = -1;
4574       this->file_size_ = 0;
4575       return false;
4576     }
4577
4578   return true;
4579 }
4580
4581 // Open the output file.
4582
4583 void
4584 Output_file::open(off_t file_size)
4585 {
4586   this->file_size_ = file_size;
4587
4588   // Unlink the file first; otherwise the open() may fail if the file
4589   // is busy (e.g. it's an executable that's currently being executed).
4590   //
4591   // However, the linker may be part of a system where a zero-length
4592   // file is created for it to write to, with tight permissions (gcc
4593   // 2.95 did something like this).  Unlinking the file would work
4594   // around those permission controls, so we only unlink if the file
4595   // has a non-zero size.  We also unlink only regular files to avoid
4596   // trouble with directories/etc.
4597   //
4598   // If we fail, continue; this command is merely a best-effort attempt
4599   // to improve the odds for open().
4600
4601   // We let the name "-" mean "stdout"
4602   if (!this->is_temporary_)
4603     {
4604       if (strcmp(this->name_, "-") == 0)
4605         this->o_ = STDOUT_FILENO;
4606       else
4607         {
4608           struct stat s;
4609           if (::stat(this->name_, &s) == 0
4610               && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4611             {
4612               if (s.st_size != 0)
4613                 ::unlink(this->name_);
4614               else if (!parameters->options().relocatable())
4615                 {
4616                   // If we don't unlink the existing file, add execute
4617                   // permission where read permissions already exist
4618                   // and where the umask permits.
4619                   int mask = ::umask(0);
4620                   ::umask(mask);
4621                   s.st_mode |= (s.st_mode & 0444) >> 2;
4622                   ::chmod(this->name_, s.st_mode & ~mask);
4623                 }
4624             }
4625
4626           int mode = parameters->options().relocatable() ? 0666 : 0777;
4627           int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4628                                   mode);
4629           if (o < 0)
4630             gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4631           this->o_ = o;
4632         }
4633     }
4634
4635   this->map();
4636 }
4637
4638 // Resize the output file.
4639
4640 void
4641 Output_file::resize(off_t file_size)
4642 {
4643   // If the mmap is mapping an anonymous memory buffer, this is easy:
4644   // just mremap to the new size.  If it's mapping to a file, we want
4645   // to unmap to flush to the file, then remap after growing the file.
4646   if (this->map_is_anonymous_)
4647     {
4648       void* base;
4649       if (!this->map_is_allocated_)
4650         {
4651           base = ::mremap(this->base_, this->file_size_, file_size,
4652                           MREMAP_MAYMOVE);
4653           if (base == MAP_FAILED)
4654             gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
4655         }
4656       else
4657         {
4658           base = realloc(this->base_, file_size);
4659           if (base == NULL)
4660             gold_nomem();
4661           if (file_size > this->file_size_)
4662             memset(static_cast<char*>(base) + this->file_size_, 0,
4663                    file_size - this->file_size_);
4664         }
4665       this->base_ = static_cast<unsigned char*>(base);
4666       this->file_size_ = file_size;
4667     }
4668   else
4669     {
4670       this->unmap();
4671       this->file_size_ = file_size;
4672       if (!this->map_no_anonymous())
4673         gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4674     }
4675 }
4676
4677 // Map an anonymous block of memory which will later be written to the
4678 // file.  Return whether the map succeeded.
4679
4680 bool
4681 Output_file::map_anonymous()
4682 {
4683   void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4684                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4685   if (base == MAP_FAILED)
4686     {
4687       base = malloc(this->file_size_);
4688       if (base == NULL)
4689         return false;
4690       memset(base, 0, this->file_size_);
4691       this->map_is_allocated_ = true;
4692     }
4693   this->base_ = static_cast<unsigned char*>(base);
4694   this->map_is_anonymous_ = true;
4695   return true;
4696 }
4697
4698 // Map the file into memory.  Return whether the mapping succeeded.
4699
4700 bool
4701 Output_file::map_no_anonymous()
4702 {
4703   const int o = this->o_;
4704
4705   // If the output file is not a regular file, don't try to mmap it;
4706   // instead, we'll mmap a block of memory (an anonymous buffer), and
4707   // then later write the buffer to the file.
4708   void* base;
4709   struct stat statbuf;
4710   if (o == STDOUT_FILENO || o == STDERR_FILENO
4711       || ::fstat(o, &statbuf) != 0
4712       || !S_ISREG(statbuf.st_mode)
4713       || this->is_temporary_)
4714     return false;
4715
4716   // Ensure that we have disk space available for the file.  If we
4717   // don't do this, it is possible that we will call munmap, close,
4718   // and exit with dirty buffers still in the cache with no assigned
4719   // disk blocks.  If the disk is out of space at that point, the
4720   // output file will wind up incomplete, but we will have already
4721   // exited.  The alternative to fallocate would be to use fdatasync,
4722   // but that would be a more significant performance hit.
4723   if (::posix_fallocate(o, 0, this->file_size_) < 0)
4724     gold_fatal(_("%s: %s"), this->name_, strerror(errno));
4725
4726   // Map the file into memory.
4727   base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4728                 MAP_SHARED, o, 0);
4729
4730   // The mmap call might fail because of file system issues: the file
4731   // system might not support mmap at all, or it might not support
4732   // mmap with PROT_WRITE.
4733   if (base == MAP_FAILED)
4734     return false;
4735
4736   this->map_is_anonymous_ = false;
4737   this->base_ = static_cast<unsigned char*>(base);
4738   return true;
4739 }
4740
4741 // Map the file into memory.
4742
4743 void
4744 Output_file::map()
4745 {
4746   if (this->map_no_anonymous())
4747     return;
4748
4749   // The mmap call might fail because of file system issues: the file
4750   // system might not support mmap at all, or it might not support
4751   // mmap with PROT_WRITE.  I'm not sure which errno values we will
4752   // see in all cases, so if the mmap fails for any reason and we
4753   // don't care about file contents, try for an anonymous map.
4754   if (this->map_anonymous())
4755     return;
4756
4757   gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4758              this->name_, static_cast<unsigned long>(this->file_size_),
4759              strerror(errno));
4760 }
4761
4762 // Unmap the file from memory.
4763
4764 void
4765 Output_file::unmap()
4766 {
4767   if (this->map_is_anonymous_)
4768     {
4769       // We've already written out the data, so there is no reason to
4770       // waste time unmapping or freeing the memory.
4771     }
4772   else
4773     {
4774       if (::munmap(this->base_, this->file_size_) < 0)
4775         gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
4776     }
4777   this->base_ = NULL;
4778 }
4779
4780 // Close the output file.
4781
4782 void
4783 Output_file::close()
4784 {
4785   // If the map isn't file-backed, we need to write it now.
4786   if (this->map_is_anonymous_ && !this->is_temporary_)
4787     {
4788       size_t bytes_to_write = this->file_size_;
4789       size_t offset = 0;
4790       while (bytes_to_write > 0)
4791         {
4792           ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
4793                                           bytes_to_write);
4794           if (bytes_written == 0)
4795             gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
4796           else if (bytes_written < 0)
4797             gold_error(_("%s: write: %s"), this->name_, strerror(errno));
4798           else
4799             {
4800               bytes_to_write -= bytes_written;
4801               offset += bytes_written;
4802             }
4803         }
4804     }
4805   this->unmap();
4806
4807   // We don't close stdout or stderr
4808   if (this->o_ != STDOUT_FILENO
4809       && this->o_ != STDERR_FILENO
4810       && !this->is_temporary_)
4811     if (::close(this->o_) < 0)
4812       gold_error(_("%s: close: %s"), this->name_, strerror(errno));
4813   this->o_ = -1;
4814 }
4815
4816 // Instantiate the templates we need.  We could use the configure
4817 // script to restrict this to only the ones for implemented targets.
4818
4819 #ifdef HAVE_TARGET_32_LITTLE
4820 template
4821 off_t
4822 Output_section::add_input_section<32, false>(
4823     Layout* layout,
4824     Sized_relobj<32, false>* object,
4825     unsigned int shndx,
4826     const char* secname,
4827     const elfcpp::Shdr<32, false>& shdr,
4828     unsigned int reloc_shndx,
4829     bool have_sections_script);
4830 #endif
4831
4832 #ifdef HAVE_TARGET_32_BIG
4833 template
4834 off_t
4835 Output_section::add_input_section<32, true>(
4836     Layout* layout,
4837     Sized_relobj<32, true>* object,
4838     unsigned int shndx,
4839     const char* secname,
4840     const elfcpp::Shdr<32, true>& shdr,
4841     unsigned int reloc_shndx,
4842     bool have_sections_script);
4843 #endif
4844
4845 #ifdef HAVE_TARGET_64_LITTLE
4846 template
4847 off_t
4848 Output_section::add_input_section<64, false>(
4849     Layout* layout,
4850     Sized_relobj<64, false>* object,
4851     unsigned int shndx,
4852     const char* secname,
4853     const elfcpp::Shdr<64, false>& shdr,
4854     unsigned int reloc_shndx,
4855     bool have_sections_script);
4856 #endif
4857
4858 #ifdef HAVE_TARGET_64_BIG
4859 template
4860 off_t
4861 Output_section::add_input_section<64, true>(
4862     Layout* layout,
4863     Sized_relobj<64, true>* object,
4864     unsigned int shndx,
4865     const char* secname,
4866     const elfcpp::Shdr<64, true>& shdr,
4867     unsigned int reloc_shndx,
4868     bool have_sections_script);
4869 #endif
4870
4871 #ifdef HAVE_TARGET_32_LITTLE
4872 template
4873 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
4874 #endif
4875
4876 #ifdef HAVE_TARGET_32_BIG
4877 template
4878 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
4879 #endif
4880
4881 #ifdef HAVE_TARGET_64_LITTLE
4882 template
4883 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
4884 #endif
4885
4886 #ifdef HAVE_TARGET_64_BIG
4887 template
4888 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
4889 #endif
4890
4891 #ifdef HAVE_TARGET_32_LITTLE
4892 template
4893 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
4894 #endif
4895
4896 #ifdef HAVE_TARGET_32_BIG
4897 template
4898 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
4899 #endif
4900
4901 #ifdef HAVE_TARGET_64_LITTLE
4902 template
4903 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
4904 #endif
4905
4906 #ifdef HAVE_TARGET_64_BIG
4907 template
4908 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
4909 #endif
4910
4911 #ifdef HAVE_TARGET_32_LITTLE
4912 template
4913 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
4914 #endif
4915
4916 #ifdef HAVE_TARGET_32_BIG
4917 template
4918 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
4919 #endif
4920
4921 #ifdef HAVE_TARGET_64_LITTLE
4922 template
4923 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
4924 #endif
4925
4926 #ifdef HAVE_TARGET_64_BIG
4927 template
4928 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
4929 #endif
4930
4931 #ifdef HAVE_TARGET_32_LITTLE
4932 template
4933 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
4934 #endif
4935
4936 #ifdef HAVE_TARGET_32_BIG
4937 template
4938 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
4939 #endif
4940
4941 #ifdef HAVE_TARGET_64_LITTLE
4942 template
4943 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
4944 #endif
4945
4946 #ifdef HAVE_TARGET_64_BIG
4947 template
4948 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
4949 #endif
4950
4951 #ifdef HAVE_TARGET_32_LITTLE
4952 template
4953 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
4954 #endif
4955
4956 #ifdef HAVE_TARGET_32_BIG
4957 template
4958 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
4959 #endif
4960
4961 #ifdef HAVE_TARGET_64_LITTLE
4962 template
4963 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
4964 #endif
4965
4966 #ifdef HAVE_TARGET_64_BIG
4967 template
4968 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
4969 #endif
4970
4971 #ifdef HAVE_TARGET_32_LITTLE
4972 template
4973 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
4974 #endif
4975
4976 #ifdef HAVE_TARGET_32_BIG
4977 template
4978 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
4979 #endif
4980
4981 #ifdef HAVE_TARGET_64_LITTLE
4982 template
4983 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
4984 #endif
4985
4986 #ifdef HAVE_TARGET_64_BIG
4987 template
4988 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
4989 #endif
4990
4991 #ifdef HAVE_TARGET_32_LITTLE
4992 template
4993 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
4994 #endif
4995
4996 #ifdef HAVE_TARGET_32_BIG
4997 template
4998 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
4999 #endif
5000
5001 #ifdef HAVE_TARGET_64_LITTLE
5002 template
5003 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
5004 #endif
5005
5006 #ifdef HAVE_TARGET_64_BIG
5007 template
5008 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
5009 #endif
5010
5011 #ifdef HAVE_TARGET_32_LITTLE
5012 template
5013 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
5014 #endif
5015
5016 #ifdef HAVE_TARGET_32_BIG
5017 template
5018 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
5019 #endif
5020
5021 #ifdef HAVE_TARGET_64_LITTLE
5022 template
5023 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
5024 #endif
5025
5026 #ifdef HAVE_TARGET_64_BIG
5027 template
5028 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
5029 #endif
5030
5031 #ifdef HAVE_TARGET_32_LITTLE
5032 template
5033 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
5034 #endif
5035
5036 #ifdef HAVE_TARGET_32_BIG
5037 template
5038 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
5039 #endif
5040
5041 #ifdef HAVE_TARGET_64_LITTLE
5042 template
5043 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
5044 #endif
5045
5046 #ifdef HAVE_TARGET_64_BIG
5047 template
5048 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
5049 #endif
5050
5051 #ifdef HAVE_TARGET_32_LITTLE
5052 template
5053 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5054 #endif
5055
5056 #ifdef HAVE_TARGET_32_BIG
5057 template
5058 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5059 #endif
5060
5061 #ifdef HAVE_TARGET_64_LITTLE
5062 template
5063 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5064 #endif
5065
5066 #ifdef HAVE_TARGET_64_BIG
5067 template
5068 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5069 #endif
5070
5071 #ifdef HAVE_TARGET_32_LITTLE
5072 template
5073 class Output_data_group<32, false>;
5074 #endif
5075
5076 #ifdef HAVE_TARGET_32_BIG
5077 template
5078 class Output_data_group<32, true>;
5079 #endif
5080
5081 #ifdef HAVE_TARGET_64_LITTLE
5082 template
5083 class Output_data_group<64, false>;
5084 #endif
5085
5086 #ifdef HAVE_TARGET_64_BIG
5087 template
5088 class Output_data_group<64, true>;
5089 #endif
5090
5091 #ifdef HAVE_TARGET_32_LITTLE
5092 template
5093 class Output_data_got<32, false>;
5094 #endif
5095
5096 #ifdef HAVE_TARGET_32_BIG
5097 template
5098 class Output_data_got<32, true>;
5099 #endif
5100
5101 #ifdef HAVE_TARGET_64_LITTLE
5102 template
5103 class Output_data_got<64, false>;
5104 #endif
5105
5106 #ifdef HAVE_TARGET_64_BIG
5107 template
5108 class Output_data_got<64, true>;
5109 #endif
5110
5111 } // End namespace gold.