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