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