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