Update comments about output offsets and merged input sections.
[external/binutils.git] / gold / output.cc
1 // output.cc -- manage the output file for gold
2
3 // Copyright 2006, 2007 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 <cerrno>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <algorithm>
32 #include "libiberty.h"   // for unlink_if_ordinary()
33
34 #include "parameters.h"
35 #include "object.h"
36 #include "symtab.h"
37 #include "reloc.h"
38 #include "merge.h"
39 #include "output.h"
40
41 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
42 #ifndef MAP_ANONYMOUS
43 # define MAP_ANONYMOUS  MAP_ANON
44 #endif
45
46 namespace gold
47 {
48
49 // Output_data variables.
50
51 bool Output_data::allocated_sizes_are_fixed;
52
53 // Output_data methods.
54
55 Output_data::~Output_data()
56 {
57 }
58
59 // Return the default alignment for the target size.
60
61 uint64_t
62 Output_data::default_alignment()
63 {
64   return Output_data::default_alignment_for_size(parameters->get_size());
65 }
66
67 // Return the default alignment for a size--32 or 64.
68
69 uint64_t
70 Output_data::default_alignment_for_size(int size)
71 {
72   if (size == 32)
73     return 4;
74   else if (size == 64)
75     return 8;
76   else
77     gold_unreachable();
78 }
79
80 // Output_section_header methods.  This currently assumes that the
81 // segment and section lists are complete at construction time.
82
83 Output_section_headers::Output_section_headers(
84     const Layout* layout,
85     const Layout::Segment_list* segment_list,
86     const Layout::Section_list* unattached_section_list,
87     const Stringpool* secnamepool)
88   : layout_(layout),
89     segment_list_(segment_list),
90     unattached_section_list_(unattached_section_list),
91     secnamepool_(secnamepool)
92 {
93   // Count all the sections.  Start with 1 for the null section.
94   off_t count = 1;
95   for (Layout::Segment_list::const_iterator p = segment_list->begin();
96        p != segment_list->end();
97        ++p)
98     if ((*p)->type() == elfcpp::PT_LOAD)
99       count += (*p)->output_section_count();
100   count += unattached_section_list->size();
101
102   const int size = parameters->get_size();
103   int shdr_size;
104   if (size == 32)
105     shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
106   else if (size == 64)
107     shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
108   else
109     gold_unreachable();
110
111   this->set_data_size(count * shdr_size);
112 }
113
114 // Write out the section headers.
115
116 void
117 Output_section_headers::do_write(Output_file* of)
118 {
119   if (parameters->get_size() == 32)
120     {
121       if (parameters->is_big_endian())
122         {
123 #ifdef HAVE_TARGET_32_BIG
124           this->do_sized_write<32, true>(of);
125 #else
126           gold_unreachable();
127 #endif
128         }
129       else
130         {
131 #ifdef HAVE_TARGET_32_LITTLE
132           this->do_sized_write<32, false>(of);
133 #else
134           gold_unreachable();
135 #endif
136         }
137     }
138   else if (parameters->get_size() == 64)
139     {
140       if (parameters->is_big_endian())
141         {
142 #ifdef HAVE_TARGET_64_BIG
143           this->do_sized_write<64, true>(of);
144 #else
145           gold_unreachable();
146 #endif
147         }
148       else
149         {
150 #ifdef HAVE_TARGET_64_LITTLE
151           this->do_sized_write<64, false>(of);
152 #else
153           gold_unreachable();
154 #endif
155         }
156     }
157   else
158     gold_unreachable();
159 }
160
161 template<int size, bool big_endian>
162 void
163 Output_section_headers::do_sized_write(Output_file* of)
164 {
165   off_t all_shdrs_size = this->data_size();
166   unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
167
168   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
169   unsigned char* v = view;
170
171   {
172     typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
173     oshdr.put_sh_name(0);
174     oshdr.put_sh_type(elfcpp::SHT_NULL);
175     oshdr.put_sh_flags(0);
176     oshdr.put_sh_addr(0);
177     oshdr.put_sh_offset(0);
178     oshdr.put_sh_size(0);
179     oshdr.put_sh_link(0);
180     oshdr.put_sh_info(0);
181     oshdr.put_sh_addralign(0);
182     oshdr.put_sh_entsize(0);
183   }
184
185   v += shdr_size;
186
187   unsigned shndx = 1;
188   for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
189        p != this->segment_list_->end();
190        ++p)
191     v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
192             this->layout_, this->secnamepool_, v, &shndx
193             SELECT_SIZE_ENDIAN(size, big_endian));
194   for (Layout::Section_list::const_iterator p =
195          this->unattached_section_list_->begin();
196        p != this->unattached_section_list_->end();
197        ++p)
198     {
199       gold_assert(shndx == (*p)->out_shndx());
200       elfcpp::Shdr_write<size, big_endian> oshdr(v);
201       (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
202       v += shdr_size;
203       ++shndx;
204     }
205
206   of->write_output_view(this->offset(), all_shdrs_size, view);
207 }
208
209 // Output_segment_header methods.
210
211 Output_segment_headers::Output_segment_headers(
212     const Layout::Segment_list& segment_list)
213   : segment_list_(segment_list)
214 {
215   const int size = parameters->get_size();
216   int phdr_size;
217   if (size == 32)
218     phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
219   else if (size == 64)
220     phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
221   else
222     gold_unreachable();
223
224   this->set_data_size(segment_list.size() * phdr_size);
225 }
226
227 void
228 Output_segment_headers::do_write(Output_file* of)
229 {
230   if (parameters->get_size() == 32)
231     {
232       if (parameters->is_big_endian())
233         {
234 #ifdef HAVE_TARGET_32_BIG
235           this->do_sized_write<32, true>(of);
236 #else
237           gold_unreachable();
238 #endif
239         }
240       else
241         {
242 #ifdef HAVE_TARGET_32_LITTLE
243         this->do_sized_write<32, false>(of);
244 #else
245         gold_unreachable();
246 #endif
247         }
248     }
249   else if (parameters->get_size() == 64)
250     {
251       if (parameters->is_big_endian())
252         {
253 #ifdef HAVE_TARGET_64_BIG
254           this->do_sized_write<64, true>(of);
255 #else
256           gold_unreachable();
257 #endif
258         }
259       else
260         {
261 #ifdef HAVE_TARGET_64_LITTLE
262           this->do_sized_write<64, false>(of);
263 #else
264           gold_unreachable();
265 #endif
266         }
267     }
268   else
269     gold_unreachable();
270 }
271
272 template<int size, bool big_endian>
273 void
274 Output_segment_headers::do_sized_write(Output_file* of)
275 {
276   const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
277   off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
278   unsigned char* view = of->get_output_view(this->offset(),
279                                             all_phdrs_size);
280   unsigned char* v = view;
281   for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
282        p != this->segment_list_.end();
283        ++p)
284     {
285       elfcpp::Phdr_write<size, big_endian> ophdr(v);
286       (*p)->write_header(&ophdr);
287       v += phdr_size;
288     }
289
290   of->write_output_view(this->offset(), all_phdrs_size, view);
291 }
292
293 // Output_file_header methods.
294
295 Output_file_header::Output_file_header(const Target* target,
296                                        const Symbol_table* symtab,
297                                        const Output_segment_headers* osh)
298   : target_(target),
299     symtab_(symtab),
300     segment_header_(osh),
301     section_header_(NULL),
302     shstrtab_(NULL)
303 {
304   const int size = parameters->get_size();
305   int ehdr_size;
306   if (size == 32)
307     ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
308   else if (size == 64)
309     ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
310   else
311     gold_unreachable();
312
313   this->set_data_size(ehdr_size);
314 }
315
316 // Set the section table information for a file header.
317
318 void
319 Output_file_header::set_section_info(const Output_section_headers* shdrs,
320                                      const Output_section* shstrtab)
321 {
322   this->section_header_ = shdrs;
323   this->shstrtab_ = shstrtab;
324 }
325
326 // Write out the file header.
327
328 void
329 Output_file_header::do_write(Output_file* of)
330 {
331   gold_assert(this->offset() == 0);
332
333   if (parameters->get_size() == 32)
334     {
335       if (parameters->is_big_endian())
336         {
337 #ifdef HAVE_TARGET_32_BIG
338           this->do_sized_write<32, true>(of);
339 #else
340           gold_unreachable();
341 #endif
342         }
343       else
344         {
345 #ifdef HAVE_TARGET_32_LITTLE
346           this->do_sized_write<32, false>(of);
347 #else
348           gold_unreachable();
349 #endif
350         }
351     }
352   else if (parameters->get_size() == 64)
353     {
354       if (parameters->is_big_endian())
355         {
356 #ifdef HAVE_TARGET_64_BIG
357           this->do_sized_write<64, true>(of);
358 #else
359           gold_unreachable();
360 #endif
361         }
362       else
363         {
364 #ifdef HAVE_TARGET_64_LITTLE
365           this->do_sized_write<64, false>(of);
366 #else
367           gold_unreachable();
368 #endif
369         }
370     }
371   else
372     gold_unreachable();
373 }
374
375 // Write out the file header with appropriate size and endianess.
376
377 template<int size, bool big_endian>
378 void
379 Output_file_header::do_sized_write(Output_file* of)
380 {
381   gold_assert(this->offset() == 0);
382
383   int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
384   unsigned char* view = of->get_output_view(0, ehdr_size);
385   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
386
387   unsigned char e_ident[elfcpp::EI_NIDENT];
388   memset(e_ident, 0, elfcpp::EI_NIDENT);
389   e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
390   e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
391   e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
392   e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
393   if (size == 32)
394     e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
395   else if (size == 64)
396     e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
397   else
398     gold_unreachable();
399   e_ident[elfcpp::EI_DATA] = (big_endian
400                               ? elfcpp::ELFDATA2MSB
401                               : elfcpp::ELFDATA2LSB);
402   e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
403   // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
404   oehdr.put_e_ident(e_ident);
405
406   elfcpp::ET e_type;
407   if (parameters->output_is_object())
408     e_type = elfcpp::ET_REL;
409   else if (parameters->output_is_shared())
410     e_type = elfcpp::ET_DYN;
411   else
412     e_type = elfcpp::ET_EXEC;
413   oehdr.put_e_type(e_type);
414
415   oehdr.put_e_machine(this->target_->machine_code());
416   oehdr.put_e_version(elfcpp::EV_CURRENT);
417
418   // FIXME: Need to support -e, and target specific entry symbol.
419   Symbol* sym = this->symtab_->lookup("_start");
420   typename Sized_symbol<size>::Value_type v;
421   if (sym == NULL)
422     v = 0;
423   else
424     {
425       Sized_symbol<size>* ssym;
426       ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
427         sym SELECT_SIZE(size));
428       v = ssym->value();
429     }
430   oehdr.put_e_entry(v);
431
432   oehdr.put_e_phoff(this->segment_header_->offset());
433   oehdr.put_e_shoff(this->section_header_->offset());
434
435   // FIXME: The target needs to set the flags.
436   oehdr.put_e_flags(0);
437
438   oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
439   oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
440   oehdr.put_e_phnum(this->segment_header_->data_size()
441                      / elfcpp::Elf_sizes<size>::phdr_size);
442   oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
443   oehdr.put_e_shnum(this->section_header_->data_size()
444                      / elfcpp::Elf_sizes<size>::shdr_size);
445   oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
446
447   of->write_output_view(0, ehdr_size, view);
448 }
449
450 // Output_data_const methods.
451
452 void
453 Output_data_const::do_write(Output_file* of)
454 {
455   of->write(this->offset(), this->data_.data(), this->data_.size());
456 }
457
458 // Output_data_const_buffer methods.
459
460 void
461 Output_data_const_buffer::do_write(Output_file* of)
462 {
463   of->write(this->offset(), this->p_, this->data_size());
464 }
465
466 // Output_section_data methods.
467
468 // Record the output section, and set the entry size and such.
469
470 void
471 Output_section_data::set_output_section(Output_section* os)
472 {
473   gold_assert(this->output_section_ == NULL);
474   this->output_section_ = os;
475   this->do_adjust_output_section(os);
476 }
477
478 // Return the section index of the output section.
479
480 unsigned int
481 Output_section_data::do_out_shndx() const
482 {
483   gold_assert(this->output_section_ != NULL);
484   return this->output_section_->out_shndx();
485 }
486
487 // Output_data_strtab methods.
488
489 // Set the final data size.
490
491 void
492 Output_data_strtab::set_final_data_size()
493 {
494   this->strtab_->set_string_offsets();
495   this->set_data_size(this->strtab_->get_strtab_size());
496 }
497
498 // Write out a string table.
499
500 void
501 Output_data_strtab::do_write(Output_file* of)
502 {
503   this->strtab_->write(of, this->offset());
504 }
505
506 // Output_reloc methods.
507
508 // A reloc against a global symbol.
509
510 template<bool dynamic, int size, bool big_endian>
511 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
512     Symbol* gsym,
513     unsigned int type,
514     Output_data* od,
515     Address address,
516     bool is_relative)
517   : address_(address), local_sym_index_(GSYM_CODE), type_(type),
518     is_relative_(is_relative), shndx_(INVALID_CODE)
519 {
520   this->u1_.gsym = gsym;
521   this->u2_.od = od;
522   if (dynamic && !is_relative)
523     gsym->set_needs_dynsym_entry();
524 }
525
526 template<bool dynamic, int size, bool big_endian>
527 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
528     Symbol* gsym,
529     unsigned int type,
530     Relobj* relobj,
531     unsigned int shndx,
532     Address address,
533     bool is_relative)
534   : address_(address), local_sym_index_(GSYM_CODE), type_(type),
535     is_relative_(is_relative), shndx_(shndx)
536 {
537   gold_assert(shndx != INVALID_CODE);
538   this->u1_.gsym = gsym;
539   this->u2_.relobj = relobj;
540   if (dynamic && !is_relative)
541     gsym->set_needs_dynsym_entry();
542 }
543
544 // A reloc against a local symbol.
545
546 template<bool dynamic, int size, bool big_endian>
547 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
548     Sized_relobj<size, big_endian>* relobj,
549     unsigned int local_sym_index,
550     unsigned int type,
551     Output_data* od,
552     Address address,
553     bool is_relative)
554   : address_(address), local_sym_index_(local_sym_index), type_(type),
555     is_relative_(is_relative), shndx_(INVALID_CODE)
556 {
557   gold_assert(local_sym_index != GSYM_CODE
558               && local_sym_index != INVALID_CODE);
559   this->u1_.relobj = relobj;
560   this->u2_.od = od;
561   if (dynamic && !is_relative)
562     relobj->set_needs_output_dynsym_entry(local_sym_index);
563 }
564
565 template<bool dynamic, int size, bool big_endian>
566 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
567     Sized_relobj<size, big_endian>* relobj,
568     unsigned int local_sym_index,
569     unsigned int type,
570     unsigned int shndx,
571     Address address,
572     bool is_relative)
573   : address_(address), local_sym_index_(local_sym_index), type_(type),
574     is_relative_(is_relative), shndx_(shndx)
575 {
576   gold_assert(local_sym_index != GSYM_CODE
577               && local_sym_index != INVALID_CODE);
578   gold_assert(shndx != INVALID_CODE);
579   this->u1_.relobj = relobj;
580   this->u2_.relobj = relobj;
581   if (dynamic && !is_relative)
582     relobj->set_needs_output_dynsym_entry(local_sym_index);
583 }
584
585 // A reloc against the STT_SECTION symbol of an output section.
586
587 template<bool dynamic, int size, bool big_endian>
588 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
589     Output_section* os,
590     unsigned int type,
591     Output_data* od,
592     Address address)
593   : address_(address), local_sym_index_(SECTION_CODE), type_(type),
594     is_relative_(false), shndx_(INVALID_CODE)
595 {
596   this->u1_.os = os;
597   this->u2_.od = od;
598   if (dynamic)
599     os->set_needs_dynsym_index();
600 }
601
602 template<bool dynamic, int size, bool big_endian>
603 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
604     Output_section* os,
605     unsigned int type,
606     Relobj* relobj,
607     unsigned int shndx,
608     Address address)
609   : address_(address), local_sym_index_(SECTION_CODE), type_(type),
610     is_relative_(false), shndx_(shndx)
611 {
612   gold_assert(shndx != INVALID_CODE);
613   this->u1_.os = os;
614   this->u2_.relobj = relobj;
615   if (dynamic)
616     os->set_needs_dynsym_index();
617 }
618
619 // Get the symbol index of a relocation.
620
621 template<bool dynamic, int size, bool big_endian>
622 unsigned int
623 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
624   const
625 {
626   unsigned int index;
627   switch (this->local_sym_index_)
628     {
629     case INVALID_CODE:
630       gold_unreachable();
631
632     case GSYM_CODE:
633       if (this->u1_.gsym == NULL)
634         index = 0;
635       else if (dynamic)
636         index = this->u1_.gsym->dynsym_index();
637       else
638         index = this->u1_.gsym->symtab_index();
639       break;
640
641     case SECTION_CODE:
642       if (dynamic)
643         index = this->u1_.os->dynsym_index();
644       else
645         index = this->u1_.os->symtab_index();
646       break;
647
648     case 0:
649       // Relocations without symbols use a symbol index of 0.
650       index = 0;
651       break;
652
653     default:
654       if (dynamic)
655         index = this->u1_.relobj->dynsym_index(this->local_sym_index_);
656       else
657         index = this->u1_.relobj->symtab_index(this->local_sym_index_);
658       break;
659     }
660   gold_assert(index != -1U);
661   return index;
662 }
663
664 // Write out the offset and info fields of a Rel or Rela relocation
665 // entry.
666
667 template<bool dynamic, int size, bool big_endian>
668 template<typename Write_rel>
669 void
670 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
671     Write_rel* wr) const
672 {
673   Address address = this->address_;
674   if (this->shndx_ != INVALID_CODE)
675     {
676       section_offset_type off;
677       Output_section* os = this->u2_.relobj->output_section(this->shndx_,
678                                                             &off);
679       gold_assert(os != NULL);
680       if (off != -1)
681         address += os->address() + off;
682       else
683         {
684           address = os->output_address(this->u2_.relobj, this->shndx_,
685                                        address);
686           gold_assert(address != -1U);
687         }
688     }
689   else if (this->u2_.od != NULL)
690     address += this->u2_.od->address();
691   wr->put_r_offset(address);
692   unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
693   wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
694 }
695
696 // Write out a Rel relocation.
697
698 template<bool dynamic, int size, bool big_endian>
699 void
700 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
701     unsigned char* pov) const
702 {
703   elfcpp::Rel_write<size, big_endian> orel(pov);
704   this->write_rel(&orel);
705 }
706
707 // Get the value of the symbol referred to by a Rel relocation.
708
709 template<bool dynamic, int size, bool big_endian>
710 typename elfcpp::Elf_types<size>::Elf_Addr
711 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value() const
712 {
713   if (this->local_sym_index_ == GSYM_CODE)
714     {
715       const Sized_symbol<size>* sym;
716       sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
717       return sym->value();
718     }
719   gold_assert(this->local_sym_index_ != SECTION_CODE
720               && this->local_sym_index_ != INVALID_CODE);
721   const Sized_relobj<size, big_endian>* relobj = this->u1_.relobj;
722   return relobj->local_symbol_value(this->local_sym_index_);
723 }
724
725 // Write out a Rela relocation.
726
727 template<bool dynamic, int size, bool big_endian>
728 void
729 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
730     unsigned char* pov) const
731 {
732   elfcpp::Rela_write<size, big_endian> orel(pov);
733   this->rel_.write_rel(&orel);
734   Addend addend = this->addend_;
735   if (rel_.is_relative())
736     addend += rel_.symbol_value();
737   orel.put_r_addend(addend);
738 }
739
740 // Output_data_reloc_base methods.
741
742 // Adjust the output section.
743
744 template<int sh_type, bool dynamic, int size, bool big_endian>
745 void
746 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
747     ::do_adjust_output_section(Output_section* os)
748 {
749   if (sh_type == elfcpp::SHT_REL)
750     os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
751   else if (sh_type == elfcpp::SHT_RELA)
752     os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
753   else
754     gold_unreachable();
755   if (dynamic)
756     os->set_should_link_to_dynsym();
757   else
758     os->set_should_link_to_symtab();
759 }
760
761 // Write out relocation data.
762
763 template<int sh_type, bool dynamic, int size, bool big_endian>
764 void
765 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
766     Output_file* of)
767 {
768   const off_t off = this->offset();
769   const off_t oview_size = this->data_size();
770   unsigned char* const oview = of->get_output_view(off, oview_size);
771
772   unsigned char* pov = oview;
773   for (typename Relocs::const_iterator p = this->relocs_.begin();
774        p != this->relocs_.end();
775        ++p)
776     {
777       p->write(pov);
778       pov += reloc_size;
779     }
780
781   gold_assert(pov - oview == oview_size);
782
783   of->write_output_view(off, oview_size, oview);
784
785   // We no longer need the relocation entries.
786   this->relocs_.clear();
787 }
788
789 // Output_data_got::Got_entry methods.
790
791 // Write out the entry.
792
793 template<int size, bool big_endian>
794 void
795 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
796 {
797   Valtype val = 0;
798
799   switch (this->local_sym_index_)
800     {
801     case GSYM_CODE:
802       {
803         // If the symbol is resolved locally, we need to write out the
804         // link-time value, which will be relocated dynamically by a
805         // RELATIVE relocation.
806         Symbol* gsym = this->u_.gsym;
807         Sized_symbol<size>* sgsym;
808         // This cast is a bit ugly.  We don't want to put a
809         // virtual method in Symbol, because we want Symbol to be
810         // as small as possible.
811         sgsym = static_cast<Sized_symbol<size>*>(gsym);
812         val = sgsym->value();
813       }
814       break;
815
816     case CONSTANT_CODE:
817       val = this->u_.constant;
818       break;
819
820     default:
821       val = this->u_.object->local_symbol_value(this->local_sym_index_);
822       break;
823     }
824
825   elfcpp::Swap<size, big_endian>::writeval(pov, val);
826 }
827
828 // Output_data_got methods.
829
830 // Add an entry for a global symbol to the GOT.  This returns true if
831 // this is a new GOT entry, false if the symbol already had a GOT
832 // entry.
833
834 template<int size, bool big_endian>
835 bool
836 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
837 {
838   if (gsym->has_got_offset())
839     return false;
840
841   this->entries_.push_back(Got_entry(gsym));
842   this->set_got_size();
843   gsym->set_got_offset(this->last_got_offset());
844   return true;
845 }
846
847 // Add an entry for a global symbol to the GOT, and add a dynamic
848 // relocation of type R_TYPE for the GOT entry.
849 template<int size, bool big_endian>
850 void
851 Output_data_got<size, big_endian>::add_global_with_rel(
852     Symbol* gsym,
853     Rel_dyn* rel_dyn,
854     unsigned int r_type)
855 {
856   if (gsym->has_got_offset())
857     return;
858
859   this->entries_.push_back(Got_entry());
860   this->set_got_size();
861   unsigned int got_offset = this->last_got_offset();
862   gsym->set_got_offset(got_offset);
863   rel_dyn->add_global(gsym, r_type, this, got_offset);
864 }
865
866 template<int size, bool big_endian>
867 void
868 Output_data_got<size, big_endian>::add_global_with_rela(
869     Symbol* gsym,
870     Rela_dyn* rela_dyn,
871     unsigned int r_type)
872 {
873   if (gsym->has_got_offset())
874     return;
875
876   this->entries_.push_back(Got_entry());
877   this->set_got_size();
878   unsigned int got_offset = this->last_got_offset();
879   gsym->set_got_offset(got_offset);
880   rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
881 }
882
883 // Add an entry for a local symbol to the GOT.  This returns true if
884 // this is a new GOT entry, false if the symbol already has a GOT
885 // entry.
886
887 template<int size, bool big_endian>
888 bool
889 Output_data_got<size, big_endian>::add_local(
890     Sized_relobj<size, big_endian>* object,
891     unsigned int symndx)
892 {
893   if (object->local_has_got_offset(symndx))
894     return false;
895
896   this->entries_.push_back(Got_entry(object, symndx));
897   this->set_got_size();
898   object->set_local_got_offset(symndx, this->last_got_offset());
899   return true;
900 }
901
902 // Add an entry for a local symbol to the GOT, and add a dynamic
903 // relocation of type R_TYPE for the GOT entry.
904 template<int size, bool big_endian>
905 void
906 Output_data_got<size, big_endian>::add_local_with_rel(
907     Sized_relobj<size, big_endian>* object,
908     unsigned int symndx,
909     Rel_dyn* rel_dyn,
910     unsigned int r_type)
911 {
912   if (object->local_has_got_offset(symndx))
913     return;
914
915   this->entries_.push_back(Got_entry());
916   this->set_got_size();
917   unsigned int got_offset = this->last_got_offset();
918   object->set_local_got_offset(symndx, got_offset);
919   rel_dyn->add_local(object, symndx, r_type, this, got_offset);
920 }
921
922 template<int size, bool big_endian>
923 void
924 Output_data_got<size, big_endian>::add_local_with_rela(
925     Sized_relobj<size, big_endian>* object,
926     unsigned int symndx,
927     Rela_dyn* rela_dyn,
928     unsigned int r_type)
929 {
930   if (object->local_has_got_offset(symndx))
931     return;
932
933   this->entries_.push_back(Got_entry());
934   this->set_got_size();
935   unsigned int got_offset = this->last_got_offset();
936   object->set_local_got_offset(symndx, got_offset);
937   rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
938 }
939
940 // Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
941 // In a pair of entries, the first value in the pair will be used for the
942 // module index, and the second value will be used for the dtv-relative
943 // offset. This returns true if this is a new GOT entry, false if the symbol
944 // already has a GOT entry.
945
946 template<int size, bool big_endian>
947 bool
948 Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym, bool need_pair)
949 {
950   if (gsym->has_tls_got_offset(need_pair))
951     return false;
952
953   this->entries_.push_back(Got_entry(gsym));
954   gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
955   if (need_pair)
956     this->entries_.push_back(Got_entry(gsym));
957   this->set_got_size();
958   return true;
959 }
960
961 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
962 // relocation of type R_TYPE.
963 template<int size, bool big_endian>
964 void
965 Output_data_got<size, big_endian>::add_global_tls_with_rel(
966     Symbol* gsym,
967     Rel_dyn* rel_dyn,
968     unsigned int r_type)
969 {
970   if (gsym->has_tls_got_offset(false))
971     return;
972
973   this->entries_.push_back(Got_entry());
974   this->set_got_size();
975   unsigned int got_offset = this->last_got_offset();
976   gsym->set_tls_got_offset(got_offset, false);
977   rel_dyn->add_global(gsym, r_type, this, got_offset);
978 }
979
980 template<int size, bool big_endian>
981 void
982 Output_data_got<size, big_endian>::add_global_tls_with_rela(
983     Symbol* gsym,
984     Rela_dyn* rela_dyn,
985     unsigned int r_type)
986 {
987   if (gsym->has_tls_got_offset(false))
988     return;
989
990   this->entries_.push_back(Got_entry());
991   this->set_got_size();
992   unsigned int got_offset = this->last_got_offset();
993   gsym->set_tls_got_offset(got_offset, false);
994   rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
995 }
996
997 // Add a pair of entries for a global TLS symbol to the GOT, and add
998 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
999 template<int size, bool big_endian>
1000 void
1001 Output_data_got<size, big_endian>::add_global_tls_with_rel(
1002     Symbol* gsym,
1003     Rel_dyn* rel_dyn,
1004     unsigned int mod_r_type,
1005     unsigned int dtv_r_type)
1006 {
1007   if (gsym->has_tls_got_offset(true))
1008     return;
1009
1010   this->entries_.push_back(Got_entry());
1011   unsigned int got_offset = this->last_got_offset();
1012   gsym->set_tls_got_offset(got_offset, true);
1013   rel_dyn->add_global(gsym, mod_r_type, this, got_offset);
1014
1015   this->entries_.push_back(Got_entry());
1016   this->set_got_size();
1017   got_offset = this->last_got_offset();
1018   rel_dyn->add_global(gsym, dtv_r_type, this, got_offset);
1019 }
1020
1021 template<int size, bool big_endian>
1022 void
1023 Output_data_got<size, big_endian>::add_global_tls_with_rela(
1024     Symbol* gsym,
1025     Rela_dyn* rela_dyn,
1026     unsigned int mod_r_type,
1027     unsigned int dtv_r_type)
1028 {
1029   if (gsym->has_tls_got_offset(true))
1030     return;
1031
1032   this->entries_.push_back(Got_entry());
1033   unsigned int got_offset = this->last_got_offset();
1034   gsym->set_tls_got_offset(got_offset, true);
1035   rela_dyn->add_global(gsym, mod_r_type, this, got_offset, 0);
1036
1037   this->entries_.push_back(Got_entry());
1038   this->set_got_size();
1039   got_offset = this->last_got_offset();
1040   rela_dyn->add_global(gsym, dtv_r_type, this, got_offset, 0);
1041 }
1042
1043 // Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1044 // In a pair of entries, the first value in the pair will be used for the
1045 // module index, and the second value will be used for the dtv-relative
1046 // offset. This returns true if this is a new GOT entry, false if the symbol
1047 // already has a GOT entry.
1048
1049 template<int size, bool big_endian>
1050 bool
1051 Output_data_got<size, big_endian>::add_local_tls(
1052     Sized_relobj<size, big_endian>* object,
1053     unsigned int symndx,
1054     bool need_pair)
1055 {
1056   if (object->local_has_tls_got_offset(symndx, need_pair))
1057     return false;
1058
1059   this->entries_.push_back(Got_entry(object, symndx));
1060   object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
1061   if (need_pair)
1062     this->entries_.push_back(Got_entry(object, symndx));
1063   this->set_got_size();
1064   return true;
1065 }
1066
1067 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1068 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1069 // Because this is a local symbol, the first GOT entry can be relocated
1070 // relative to a section symbol, and the second GOT entry will have an
1071 // dtv-relative value that can be computed at link time.
1072 template<int size, bool big_endian>
1073 void
1074 Output_data_got<size, big_endian>::add_local_tls_with_rel(
1075     Sized_relobj<size, big_endian>* object,
1076     unsigned int symndx,
1077     unsigned int shndx,
1078     bool need_pair,
1079     Rel_dyn* rel_dyn,
1080     unsigned int r_type)
1081 {
1082   if (object->local_has_tls_got_offset(symndx, need_pair))
1083     return;
1084
1085   this->entries_.push_back(Got_entry());
1086   unsigned int got_offset = this->last_got_offset();
1087   object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1088   section_offset_type off;
1089   Output_section* os = object->output_section(shndx, &off);
1090   rel_dyn->add_output_section(os, r_type, this, got_offset);
1091
1092   // The second entry of the pair will be statically initialized
1093   // with the TLS offset of the symbol.
1094   if (need_pair)
1095     this->entries_.push_back(Got_entry(object, symndx));
1096
1097   this->set_got_size();
1098 }
1099
1100 template<int size, bool big_endian>
1101 void
1102 Output_data_got<size, big_endian>::add_local_tls_with_rela(
1103     Sized_relobj<size, big_endian>* object,
1104     unsigned int symndx,
1105     unsigned int shndx,
1106     bool need_pair,
1107     Rela_dyn* rela_dyn,
1108     unsigned int r_type)
1109 {
1110   if (object->local_has_tls_got_offset(symndx, need_pair))
1111     return;
1112
1113   this->entries_.push_back(Got_entry());
1114   unsigned int got_offset = this->last_got_offset();
1115   object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1116   section_offset_type off;
1117   Output_section* os = object->output_section(shndx, &off);
1118   rela_dyn->add_output_section(os, r_type, this, got_offset, 0);
1119
1120   // The second entry of the pair will be statically initialized
1121   // with the TLS offset of the symbol.
1122   if (need_pair)
1123     this->entries_.push_back(Got_entry(object, symndx));
1124
1125   this->set_got_size();
1126 }
1127
1128 // Write out the GOT.
1129
1130 template<int size, bool big_endian>
1131 void
1132 Output_data_got<size, big_endian>::do_write(Output_file* of)
1133 {
1134   const int add = size / 8;
1135
1136   const off_t off = this->offset();
1137   const off_t oview_size = this->data_size();
1138   unsigned char* const oview = of->get_output_view(off, oview_size);
1139
1140   unsigned char* pov = oview;
1141   for (typename Got_entries::const_iterator p = this->entries_.begin();
1142        p != this->entries_.end();
1143        ++p)
1144     {
1145       p->write(pov);
1146       pov += add;
1147     }
1148
1149   gold_assert(pov - oview == oview_size);
1150
1151   of->write_output_view(off, oview_size, oview);
1152
1153   // We no longer need the GOT entries.
1154   this->entries_.clear();
1155 }
1156
1157 // Output_data_dynamic::Dynamic_entry methods.
1158
1159 // Write out the entry.
1160
1161 template<int size, bool big_endian>
1162 void
1163 Output_data_dynamic::Dynamic_entry::write(
1164     unsigned char* pov,
1165     const Stringpool* pool
1166     ACCEPT_SIZE_ENDIAN) const
1167 {
1168   typename elfcpp::Elf_types<size>::Elf_WXword val;
1169   switch (this->classification_)
1170     {
1171     case DYNAMIC_NUMBER:
1172       val = this->u_.val;
1173       break;
1174
1175     case DYNAMIC_SECTION_ADDRESS:
1176       val = this->u_.od->address();
1177       break;
1178
1179     case DYNAMIC_SECTION_SIZE:
1180       val = this->u_.od->data_size();
1181       break;
1182
1183     case DYNAMIC_SYMBOL:
1184       {
1185         const Sized_symbol<size>* s =
1186           static_cast<const Sized_symbol<size>*>(this->u_.sym);
1187         val = s->value();
1188       }
1189       break;
1190
1191     case DYNAMIC_STRING:
1192       val = pool->get_offset(this->u_.str);
1193       break;
1194
1195     default:
1196       gold_unreachable();
1197     }
1198
1199   elfcpp::Dyn_write<size, big_endian> dw(pov);
1200   dw.put_d_tag(this->tag_);
1201   dw.put_d_val(val);
1202 }
1203
1204 // Output_data_dynamic methods.
1205
1206 // Adjust the output section to set the entry size.
1207
1208 void
1209 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1210 {
1211   if (parameters->get_size() == 32)
1212     os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1213   else if (parameters->get_size() == 64)
1214     os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1215   else
1216     gold_unreachable();
1217 }
1218
1219 // Set the final data size.
1220
1221 void
1222 Output_data_dynamic::set_final_data_size()
1223 {
1224   // Add the terminating entry.
1225   this->add_constant(elfcpp::DT_NULL, 0);
1226
1227   int dyn_size;
1228   if (parameters->get_size() == 32)
1229     dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1230   else if (parameters->get_size() == 64)
1231     dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1232   else
1233     gold_unreachable();
1234   this->set_data_size(this->entries_.size() * dyn_size);
1235 }
1236
1237 // Write out the dynamic entries.
1238
1239 void
1240 Output_data_dynamic::do_write(Output_file* of)
1241 {
1242   if (parameters->get_size() == 32)
1243     {
1244       if (parameters->is_big_endian())
1245         {
1246 #ifdef HAVE_TARGET_32_BIG
1247           this->sized_write<32, true>(of);
1248 #else
1249           gold_unreachable();
1250 #endif
1251         }
1252       else
1253         {
1254 #ifdef HAVE_TARGET_32_LITTLE
1255           this->sized_write<32, false>(of);
1256 #else
1257           gold_unreachable();
1258 #endif
1259         }
1260     }
1261   else if (parameters->get_size() == 64)
1262     {
1263       if (parameters->is_big_endian())
1264         {
1265 #ifdef HAVE_TARGET_64_BIG
1266           this->sized_write<64, true>(of);
1267 #else
1268           gold_unreachable();
1269 #endif
1270         }
1271       else
1272         {
1273 #ifdef HAVE_TARGET_64_LITTLE
1274           this->sized_write<64, false>(of);
1275 #else
1276           gold_unreachable();
1277 #endif
1278         }
1279     }
1280   else
1281     gold_unreachable();
1282 }
1283
1284 template<int size, bool big_endian>
1285 void
1286 Output_data_dynamic::sized_write(Output_file* of)
1287 {
1288   const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1289
1290   const off_t offset = this->offset();
1291   const off_t oview_size = this->data_size();
1292   unsigned char* const oview = of->get_output_view(offset, oview_size);
1293
1294   unsigned char* pov = oview;
1295   for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1296        p != this->entries_.end();
1297        ++p)
1298     {
1299       p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1300           pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
1301       pov += dyn_size;
1302     }
1303
1304   gold_assert(pov - oview == oview_size);
1305
1306   of->write_output_view(offset, oview_size, oview);
1307
1308   // We no longer need the dynamic entries.
1309   this->entries_.clear();
1310 }
1311
1312 // Output_section::Input_section methods.
1313
1314 // Return the data size.  For an input section we store the size here.
1315 // For an Output_section_data, we have to ask it for the size.
1316
1317 off_t
1318 Output_section::Input_section::data_size() const
1319 {
1320   if (this->is_input_section())
1321     return this->u1_.data_size;
1322   else
1323     return this->u2_.posd->data_size();
1324 }
1325
1326 // Set the address and file offset.
1327
1328 void
1329 Output_section::Input_section::set_address_and_file_offset(
1330     uint64_t address,
1331     off_t file_offset,
1332     off_t section_file_offset)
1333 {
1334   if (this->is_input_section())
1335     this->u2_.object->set_section_offset(this->shndx_,
1336                                          file_offset - section_file_offset);
1337   else
1338     this->u2_.posd->set_address_and_file_offset(address, file_offset);
1339 }
1340
1341 // Finalize the data size.
1342
1343 void
1344 Output_section::Input_section::finalize_data_size()
1345 {
1346   if (!this->is_input_section())
1347     this->u2_.posd->finalize_data_size();
1348 }
1349
1350 // Try to turn an input offset into an output offset.  We want to
1351 // return the output offset relative to the start of this
1352 // Input_section in the output section.
1353
1354 inline bool
1355 Output_section::Input_section::output_offset(
1356     const Relobj* object,
1357     unsigned int shndx,
1358     section_offset_type offset,
1359     section_offset_type *poutput) const
1360 {
1361   if (!this->is_input_section())
1362     return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1363   else
1364     {
1365       if (this->shndx_ != shndx || this->u2_.object != object)
1366         return false;
1367       *poutput = offset;
1368       return true;
1369     }
1370 }
1371
1372 // Write out the data.  We don't have to do anything for an input
1373 // section--they are handled via Object::relocate--but this is where
1374 // we write out the data for an Output_section_data.
1375
1376 void
1377 Output_section::Input_section::write(Output_file* of)
1378 {
1379   if (!this->is_input_section())
1380     this->u2_.posd->write(of);
1381 }
1382
1383 // Write the data to a buffer.  As for write(), we don't have to do
1384 // anything for an input section.
1385
1386 void
1387 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1388 {
1389   if (!this->is_input_section())
1390     this->u2_.posd->write_to_buffer(buffer);
1391 }
1392
1393 // Output_section methods.
1394
1395 // Construct an Output_section.  NAME will point into a Stringpool.
1396
1397 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1398                                elfcpp::Elf_Xword flags)
1399   : name_(name),
1400     addralign_(0),
1401     entsize_(0),
1402     link_section_(NULL),
1403     link_(0),
1404     info_section_(NULL),
1405     info_(0),
1406     type_(type),
1407     flags_(flags),
1408     out_shndx_(-1U),
1409     symtab_index_(0),
1410     dynsym_index_(0),
1411     input_sections_(),
1412     first_input_offset_(0),
1413     fills_(),
1414     postprocessing_buffer_(NULL),
1415     needs_symtab_index_(false),
1416     needs_dynsym_index_(false),
1417     should_link_to_symtab_(false),
1418     should_link_to_dynsym_(false),
1419     after_input_sections_(false),
1420     requires_postprocessing_(false),
1421     tls_offset_(0)
1422 {
1423   // An unallocated section has no address.  Forcing this means that
1424   // we don't need special treatment for symbols defined in debug
1425   // sections.
1426   if ((flags & elfcpp::SHF_ALLOC) == 0)
1427     this->set_address(0);
1428 }
1429
1430 Output_section::~Output_section()
1431 {
1432 }
1433
1434 // Set the entry size.
1435
1436 void
1437 Output_section::set_entsize(uint64_t v)
1438 {
1439   if (this->entsize_ == 0)
1440     this->entsize_ = v;
1441   else
1442     gold_assert(this->entsize_ == v);
1443 }
1444
1445 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1446 // OBJECT, to the Output_section.  RELOC_SHNDX is the index of a
1447 // relocation section which applies to this section, or 0 if none, or
1448 // -1U if more than one.  Return the offset of the input section
1449 // within the output section.  Return -1 if the input section will
1450 // receive special handling.  In the normal case we don't always keep
1451 // track of input sections for an Output_section.  Instead, each
1452 // Object keeps track of the Output_section for each of its input
1453 // sections.
1454
1455 template<int size, bool big_endian>
1456 off_t
1457 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1458                                   unsigned int shndx,
1459                                   const char* secname,
1460                                   const elfcpp::Shdr<size, big_endian>& shdr,
1461                                   unsigned int reloc_shndx)
1462 {
1463   elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1464   if ((addralign & (addralign - 1)) != 0)
1465     {
1466       object->error(_("invalid alignment %lu for section \"%s\""),
1467                     static_cast<unsigned long>(addralign), secname);
1468       addralign = 1;
1469     }
1470
1471   if (addralign > this->addralign_)
1472     this->addralign_ = addralign;
1473
1474   typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1475   uint64_t entsize = shdr.get_sh_entsize();
1476
1477   // .debug_str is a mergeable string section, but is not always so
1478   // marked by compilers.  Mark manually here so we can optimize.
1479   if (strcmp(secname, ".debug_str") == 0)
1480     {
1481       sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1482       entsize = 1;
1483     }
1484
1485   // If this is a SHF_MERGE section, we pass all the input sections to
1486   // a Output_data_merge.  We don't try to handle relocations for such
1487   // a section.
1488   if ((sh_flags & elfcpp::SHF_MERGE) != 0
1489       && reloc_shndx == 0)
1490     {
1491       if (this->add_merge_input_section(object, shndx, sh_flags,
1492                                         entsize, addralign))
1493         {
1494           // Tell the relocation routines that they need to call the
1495           // output_offset method to determine the final address.
1496           return -1;
1497         }
1498     }
1499
1500   off_t offset_in_section = this->current_data_size_for_child();
1501   off_t aligned_offset_in_section = align_address(offset_in_section,
1502                                                   addralign);
1503
1504   if (aligned_offset_in_section > offset_in_section
1505       && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1506       && object->target()->has_code_fill())
1507     {
1508       // We need to add some fill data.  Using fill_list_ when
1509       // possible is an optimization, since we will often have fill
1510       // sections without input sections.
1511       off_t fill_len = aligned_offset_in_section - offset_in_section;
1512       if (this->input_sections_.empty())
1513         this->fills_.push_back(Fill(offset_in_section, fill_len));
1514       else
1515         {
1516           // FIXME: When relaxing, the size needs to adjust to
1517           // maintain a constant alignment.
1518           std::string fill_data(object->target()->code_fill(fill_len));
1519           Output_data_const* odc = new Output_data_const(fill_data, 1);
1520           this->input_sections_.push_back(Input_section(odc));
1521         }
1522     }
1523
1524   this->set_current_data_size_for_child(aligned_offset_in_section
1525                                         + shdr.get_sh_size());
1526
1527   // We need to keep track of this section if we are already keeping
1528   // track of sections, or if we are relaxing.  FIXME: Add test for
1529   // relaxing.
1530   if (!this->input_sections_.empty())
1531     this->input_sections_.push_back(Input_section(object, shndx,
1532                                                   shdr.get_sh_size(),
1533                                                   addralign));
1534
1535   return aligned_offset_in_section;
1536 }
1537
1538 // Add arbitrary data to an output section.
1539
1540 void
1541 Output_section::add_output_section_data(Output_section_data* posd)
1542 {
1543   Input_section inp(posd);
1544   this->add_output_section_data(&inp);
1545 }
1546
1547 // Add arbitrary data to an output section by Input_section.
1548
1549 void
1550 Output_section::add_output_section_data(Input_section* inp)
1551 {
1552   if (this->input_sections_.empty())
1553     this->first_input_offset_ = this->current_data_size_for_child();
1554
1555   this->input_sections_.push_back(*inp);
1556
1557   uint64_t addralign = inp->addralign();
1558   if (addralign > this->addralign_)
1559     this->addralign_ = addralign;
1560
1561   inp->set_output_section(this);
1562 }
1563
1564 // Add a merge section to an output section.
1565
1566 void
1567 Output_section::add_output_merge_section(Output_section_data* posd,
1568                                          bool is_string, uint64_t entsize)
1569 {
1570   Input_section inp(posd, is_string, entsize);
1571   this->add_output_section_data(&inp);
1572 }
1573
1574 // Add an input section to a SHF_MERGE section.
1575
1576 bool
1577 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1578                                         uint64_t flags, uint64_t entsize,
1579                                         uint64_t addralign)
1580 {
1581   bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1582
1583   // We only merge strings if the alignment is not more than the
1584   // character size.  This could be handled, but it's unusual.
1585   if (is_string && addralign > entsize)
1586     return false;
1587
1588   Input_section_list::iterator p;
1589   for (p = this->input_sections_.begin();
1590        p != this->input_sections_.end();
1591        ++p)
1592     if (p->is_merge_section(is_string, entsize, addralign))
1593       {
1594         p->add_input_section(object, shndx);
1595         return true;
1596       }
1597
1598   // We handle the actual constant merging in Output_merge_data or
1599   // Output_merge_string_data.
1600   Output_section_data* posd;
1601   if (!is_string)
1602     posd = new Output_merge_data(entsize, addralign);
1603   else
1604     {
1605       switch (entsize)
1606         {
1607         case 1:
1608           posd = new Output_merge_string<char>(addralign);
1609           break;
1610         case 2:
1611           posd = new Output_merge_string<uint16_t>(addralign);
1612           break;
1613         case 4:
1614           posd = new Output_merge_string<uint32_t>(addralign);
1615           break;
1616         default:
1617           return false;
1618         }
1619     }
1620
1621   this->add_output_merge_section(posd, is_string, entsize);
1622   posd->add_input_section(object, shndx);
1623
1624   return true;
1625 }
1626
1627 // Given an address OFFSET relative to the start of input section
1628 // SHNDX in OBJECT, return whether this address is being included in
1629 // the final link.  This should only be called if SHNDX in OBJECT has
1630 // a special mapping.
1631
1632 bool
1633 Output_section::is_input_address_mapped(const Relobj* object,
1634                                         unsigned int shndx,
1635                                         off_t offset) const
1636 {
1637   gold_assert(object->is_section_specially_mapped(shndx));
1638
1639   for (Input_section_list::const_iterator p = this->input_sections_.begin();
1640        p != this->input_sections_.end();
1641        ++p)
1642     {
1643       section_offset_type output_offset;
1644       if (p->output_offset(object, shndx, offset, &output_offset))
1645         return output_offset != -1;
1646     }
1647
1648   // By default we assume that the address is mapped.  This should
1649   // only be called after we have passed all sections to Layout.  At
1650   // that point we should know what we are discarding.
1651   return true;
1652 }
1653
1654 // Given an address OFFSET relative to the start of input section
1655 // SHNDX in object OBJECT, return the output offset relative to the
1656 // start of the input section in the output section.  This should only
1657 // be called if SHNDX in OBJECT has a special mapping.
1658
1659 section_offset_type
1660 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1661                               section_offset_type offset) const
1662 {
1663   gold_assert(object->is_section_specially_mapped(shndx));
1664   // This can only be called meaningfully when layout is complete.
1665   gold_assert(Output_data::is_layout_complete());
1666
1667   for (Input_section_list::const_iterator p = this->input_sections_.begin();
1668        p != this->input_sections_.end();
1669        ++p)
1670     {
1671       section_offset_type output_offset;
1672       if (p->output_offset(object, shndx, offset, &output_offset))
1673         return output_offset;
1674     }
1675   gold_unreachable();
1676 }
1677
1678 // Return the output virtual address of OFFSET relative to the start
1679 // of input section SHNDX in object OBJECT.
1680
1681 uint64_t
1682 Output_section::output_address(const Relobj* object, unsigned int shndx,
1683                                off_t offset) const
1684 {
1685   gold_assert(object->is_section_specially_mapped(shndx));
1686   // This can only be called meaningfully when layout is complete.
1687   gold_assert(Output_data::is_layout_complete());
1688
1689   uint64_t addr = this->address() + this->first_input_offset_;
1690   for (Input_section_list::const_iterator p = this->input_sections_.begin();
1691        p != this->input_sections_.end();
1692        ++p)
1693     {
1694       addr = align_address(addr, p->addralign());
1695       section_offset_type output_offset;
1696       if (p->output_offset(object, shndx, offset, &output_offset))
1697         {
1698           if (output_offset == -1)
1699             return -1U;
1700           return addr + output_offset;
1701         }
1702       addr += p->data_size();
1703     }
1704
1705   // If we get here, it means that we don't know the mapping for this
1706   // input section.  This might happen in principle if
1707   // add_input_section were called before add_output_section_data.
1708   // But it should never actually happen.
1709
1710   gold_unreachable();
1711 }
1712
1713 // Set the data size of an Output_section.  This is where we handle
1714 // setting the addresses of any Output_section_data objects.
1715
1716 void
1717 Output_section::set_final_data_size()
1718 {
1719   if (this->input_sections_.empty())
1720     {
1721       this->set_data_size(this->current_data_size_for_child());
1722       return;
1723     }
1724
1725   uint64_t address = this->address();
1726   off_t startoff = this->offset();
1727   off_t off = startoff + this->first_input_offset_;
1728   for (Input_section_list::iterator p = this->input_sections_.begin();
1729        p != this->input_sections_.end();
1730        ++p)
1731     {
1732       off = align_address(off, p->addralign());
1733       p->set_address_and_file_offset(address + (off - startoff), off,
1734                                      startoff);
1735       off += p->data_size();
1736     }
1737
1738   this->set_data_size(off - startoff);
1739 }
1740
1741 // Set the TLS offset.  Called only for SHT_TLS sections.
1742
1743 void
1744 Output_section::do_set_tls_offset(uint64_t tls_base)
1745 {
1746   this->tls_offset_ = this->address() - tls_base;
1747 }
1748
1749 // Write the section header to *OSHDR.
1750
1751 template<int size, bool big_endian>
1752 void
1753 Output_section::write_header(const Layout* layout,
1754                              const Stringpool* secnamepool,
1755                              elfcpp::Shdr_write<size, big_endian>* oshdr) const
1756 {
1757   oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1758   oshdr->put_sh_type(this->type_);
1759   oshdr->put_sh_flags(this->flags_);
1760   oshdr->put_sh_addr(this->address());
1761   oshdr->put_sh_offset(this->offset());
1762   oshdr->put_sh_size(this->data_size());
1763   if (this->link_section_ != NULL)
1764     oshdr->put_sh_link(this->link_section_->out_shndx());
1765   else if (this->should_link_to_symtab_)
1766     oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1767   else if (this->should_link_to_dynsym_)
1768     oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1769   else
1770     oshdr->put_sh_link(this->link_);
1771   if (this->info_section_ != NULL)
1772     oshdr->put_sh_info(this->info_section_->out_shndx());
1773   else
1774     oshdr->put_sh_info(this->info_);
1775   oshdr->put_sh_addralign(this->addralign_);
1776   oshdr->put_sh_entsize(this->entsize_);
1777 }
1778
1779 // Write out the data.  For input sections the data is written out by
1780 // Object::relocate, but we have to handle Output_section_data objects
1781 // here.
1782
1783 void
1784 Output_section::do_write(Output_file* of)
1785 {
1786   gold_assert(!this->requires_postprocessing());
1787
1788   off_t output_section_file_offset = this->offset();
1789   for (Fill_list::iterator p = this->fills_.begin();
1790        p != this->fills_.end();
1791        ++p)
1792     {
1793       std::string fill_data(of->target()->code_fill(p->length()));
1794       of->write(output_section_file_offset + p->section_offset(),
1795                 fill_data.data(), fill_data.size());
1796     }
1797
1798   for (Input_section_list::iterator p = this->input_sections_.begin();
1799        p != this->input_sections_.end();
1800        ++p)
1801     p->write(of);
1802 }
1803
1804 // If a section requires postprocessing, create the buffer to use.
1805
1806 void
1807 Output_section::create_postprocessing_buffer()
1808 {
1809   gold_assert(this->requires_postprocessing());
1810   gold_assert(this->postprocessing_buffer_ == NULL);
1811
1812   if (!this->input_sections_.empty())
1813     {
1814       off_t off = this->first_input_offset_;
1815       for (Input_section_list::iterator p = this->input_sections_.begin();
1816            p != this->input_sections_.end();
1817            ++p)
1818         {
1819           off = align_address(off, p->addralign());
1820           p->finalize_data_size();
1821           off += p->data_size();
1822         }
1823       this->set_current_data_size_for_child(off);
1824     }
1825
1826   off_t buffer_size = this->current_data_size_for_child();
1827   this->postprocessing_buffer_ = new unsigned char[buffer_size];
1828 }
1829
1830 // Write all the data of an Output_section into the postprocessing
1831 // buffer.  This is used for sections which require postprocessing,
1832 // such as compression.  Input sections are handled by
1833 // Object::Relocate.
1834
1835 void
1836 Output_section::write_to_postprocessing_buffer()
1837 {
1838   gold_assert(this->requires_postprocessing());
1839
1840   Target* target = parameters->target();
1841   unsigned char* buffer = this->postprocessing_buffer();
1842   for (Fill_list::iterator p = this->fills_.begin();
1843        p != this->fills_.end();
1844        ++p)
1845     {
1846       std::string fill_data(target->code_fill(p->length()));
1847       memcpy(buffer + p->section_offset(), fill_data.data(), fill_data.size());
1848     }
1849
1850   off_t off = this->first_input_offset_;
1851   for (Input_section_list::iterator p = this->input_sections_.begin();
1852        p != this->input_sections_.end();
1853        ++p)
1854     {
1855       off = align_address(off, p->addralign());
1856       p->write_to_buffer(buffer + off);
1857       off += p->data_size();
1858     }
1859 }
1860
1861 // Print stats for merge sections to stderr.
1862
1863 void
1864 Output_section::print_merge_stats()
1865 {
1866   Input_section_list::iterator p;
1867   for (p = this->input_sections_.begin();
1868        p != this->input_sections_.end();
1869        ++p)
1870     p->print_merge_stats(this->name_);
1871 }
1872
1873 // Output segment methods.
1874
1875 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1876   : output_data_(),
1877     output_bss_(),
1878     vaddr_(0),
1879     paddr_(0),
1880     memsz_(0),
1881     align_(0),
1882     offset_(0),
1883     filesz_(0),
1884     type_(type),
1885     flags_(flags),
1886     is_align_known_(false)
1887 {
1888 }
1889
1890 // Add an Output_section to an Output_segment.
1891
1892 void
1893 Output_segment::add_output_section(Output_section* os,
1894                                    elfcpp::Elf_Word seg_flags,
1895                                    bool front)
1896 {
1897   gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1898   gold_assert(!this->is_align_known_);
1899
1900   // Update the segment flags.
1901   this->flags_ |= seg_flags;
1902
1903   Output_segment::Output_data_list* pdl;
1904   if (os->type() == elfcpp::SHT_NOBITS)
1905     pdl = &this->output_bss_;
1906   else
1907     pdl = &this->output_data_;
1908
1909   // So that PT_NOTE segments will work correctly, we need to ensure
1910   // that all SHT_NOTE sections are adjacent.  This will normally
1911   // happen automatically, because all the SHT_NOTE input sections
1912   // will wind up in the same output section.  However, it is possible
1913   // for multiple SHT_NOTE input sections to have different section
1914   // flags, and thus be in different output sections, but for the
1915   // different section flags to map into the same segment flags and
1916   // thus the same output segment.
1917
1918   // Note that while there may be many input sections in an output
1919   // section, there are normally only a few output sections in an
1920   // output segment.  This loop is expected to be fast.
1921
1922   if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1923     {
1924       Output_segment::Output_data_list::iterator p = pdl->end();
1925       do
1926         {
1927           --p;
1928           if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1929             {
1930               // We don't worry about the FRONT parameter.
1931               ++p;
1932               pdl->insert(p, os);
1933               return;
1934             }
1935         }
1936       while (p != pdl->begin());
1937     }
1938
1939   // Similarly, so that PT_TLS segments will work, we need to group
1940   // SHF_TLS sections.  An SHF_TLS/SHT_NOBITS section is a special
1941   // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1942   // SHF_TLS/SHT_PROGBITS sections.  This lets us set up PT_TLS
1943   // correctly.  SHF_TLS sections get added to both a PT_LOAD segment
1944   // and the PT_TLS segment -- we do this grouping only for the
1945   // PT_LOAD segment.
1946   if (this->type_ != elfcpp::PT_TLS
1947       && (os->flags() & elfcpp::SHF_TLS) != 0
1948       && !this->output_data_.empty())
1949     {
1950       pdl = &this->output_data_;
1951       bool nobits = os->type() == elfcpp::SHT_NOBITS;
1952       bool sawtls = false;
1953       Output_segment::Output_data_list::iterator p = pdl->end();
1954       do
1955         {
1956           --p;
1957           bool insert;
1958           if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1959             {
1960               sawtls = true;
1961               // Put a NOBITS section after the first TLS section.
1962               // But a PROGBITS section after the first TLS/PROGBITS
1963               // section.
1964               insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1965             }
1966           else
1967             {
1968               // If we've gone past the TLS sections, but we've seen a
1969               // TLS section, then we need to insert this section now.
1970               insert = sawtls;
1971             }
1972
1973           if (insert)
1974             {
1975               // We don't worry about the FRONT parameter.
1976               ++p;
1977               pdl->insert(p, os);
1978               return;
1979             }
1980         }
1981       while (p != pdl->begin());
1982
1983       // There are no TLS sections yet; put this one at the requested
1984       // location in the section list.
1985     }
1986
1987   if (front)
1988     pdl->push_front(os);
1989   else
1990     pdl->push_back(os);
1991 }
1992
1993 // Add an Output_data (which is not an Output_section) to the start of
1994 // a segment.
1995
1996 void
1997 Output_segment::add_initial_output_data(Output_data* od)
1998 {
1999   gold_assert(!this->is_align_known_);
2000   this->output_data_.push_front(od);
2001 }
2002
2003 // Return the maximum alignment of the Output_data in Output_segment.
2004 // Once we compute this, we prohibit new sections from being added.
2005
2006 uint64_t
2007 Output_segment::addralign()
2008 {
2009   if (!this->is_align_known_)
2010     {
2011       uint64_t addralign;
2012
2013       addralign = Output_segment::maximum_alignment(&this->output_data_);
2014       if (addralign > this->align_)
2015         this->align_ = addralign;
2016
2017       addralign = Output_segment::maximum_alignment(&this->output_bss_);
2018       if (addralign > this->align_)
2019         this->align_ = addralign;
2020
2021       this->is_align_known_ = true;
2022     }
2023
2024   return this->align_;
2025 }
2026
2027 // Return the maximum alignment of a list of Output_data.
2028
2029 uint64_t
2030 Output_segment::maximum_alignment(const Output_data_list* pdl)
2031 {
2032   uint64_t ret = 0;
2033   for (Output_data_list::const_iterator p = pdl->begin();
2034        p != pdl->end();
2035        ++p)
2036     {
2037       uint64_t addralign = (*p)->addralign();
2038       if (addralign > ret)
2039         ret = addralign;
2040     }
2041   return ret;
2042 }
2043
2044 // Return the number of dynamic relocs applied to this segment.
2045
2046 unsigned int
2047 Output_segment::dynamic_reloc_count() const
2048 {
2049   return (this->dynamic_reloc_count_list(&this->output_data_)
2050           + this->dynamic_reloc_count_list(&this->output_bss_));
2051 }
2052
2053 // Return the number of dynamic relocs applied to an Output_data_list.
2054
2055 unsigned int
2056 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2057 {
2058   unsigned int count = 0;
2059   for (Output_data_list::const_iterator p = pdl->begin();
2060        p != pdl->end();
2061        ++p)
2062     count += (*p)->dynamic_reloc_count();
2063   return count;
2064 }
2065
2066 // Set the section addresses for an Output_segment.  ADDR is the
2067 // address and *POFF is the file offset.  Set the section indexes
2068 // starting with *PSHNDX.  Return the address of the immediately
2069 // following segment.  Update *POFF and *PSHNDX.
2070
2071 uint64_t
2072 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
2073                                       unsigned int* pshndx)
2074 {
2075   gold_assert(this->type_ == elfcpp::PT_LOAD);
2076
2077   this->vaddr_ = addr;
2078   this->paddr_ = addr;
2079
2080   off_t orig_off = *poff;
2081   this->offset_ = orig_off;
2082
2083   *poff = align_address(*poff, this->addralign());
2084
2085   addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
2086                                           pshndx);
2087   this->filesz_ = *poff - orig_off;
2088
2089   off_t off = *poff;
2090
2091   uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
2092                                                   poff, pshndx);
2093   this->memsz_ = *poff - orig_off;
2094
2095   // Ignore the file offset adjustments made by the BSS Output_data
2096   // objects.
2097   *poff = off;
2098
2099   return ret;
2100 }
2101
2102 // Set the addresses and file offsets in a list of Output_data
2103 // structures.
2104
2105 uint64_t
2106 Output_segment::set_section_list_addresses(Output_data_list* pdl,
2107                                            uint64_t addr, off_t* poff,
2108                                            unsigned int* pshndx)
2109 {
2110   off_t startoff = *poff;
2111
2112   off_t off = startoff;
2113   for (Output_data_list::iterator p = pdl->begin();
2114        p != pdl->end();
2115        ++p)
2116     {
2117       off = align_address(off, (*p)->addralign());
2118       (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2119
2120       // Unless this is a PT_TLS segment, we want to ignore the size
2121       // of a SHF_TLS/SHT_NOBITS section.  Such a section does not
2122       // affect the size of a PT_LOAD segment.
2123       if (this->type_ == elfcpp::PT_TLS
2124           || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2125           || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2126         off += (*p)->data_size();
2127
2128       if ((*p)->is_section())
2129         {
2130           (*p)->set_out_shndx(*pshndx);
2131           ++*pshndx;
2132         }
2133     }
2134
2135   *poff = off;
2136   return addr + (off - startoff);
2137 }
2138
2139 // For a non-PT_LOAD segment, set the offset from the sections, if
2140 // any.
2141
2142 void
2143 Output_segment::set_offset()
2144 {
2145   gold_assert(this->type_ != elfcpp::PT_LOAD);
2146
2147   if (this->output_data_.empty() && this->output_bss_.empty())
2148     {
2149       this->vaddr_ = 0;
2150       this->paddr_ = 0;
2151       this->memsz_ = 0;
2152       this->align_ = 0;
2153       this->offset_ = 0;
2154       this->filesz_ = 0;
2155       return;
2156     }
2157
2158   const Output_data* first;
2159   if (this->output_data_.empty())
2160     first = this->output_bss_.front();
2161   else
2162     first = this->output_data_.front();
2163   this->vaddr_ = first->address();
2164   this->paddr_ = this->vaddr_;
2165   this->offset_ = first->offset();
2166
2167   if (this->output_data_.empty())
2168     this->filesz_ = 0;
2169   else
2170     {
2171       const Output_data* last_data = this->output_data_.back();
2172       this->filesz_ = (last_data->address()
2173                        + last_data->data_size()
2174                        - this->vaddr_);
2175     }
2176
2177   const Output_data* last;
2178   if (this->output_bss_.empty())
2179     last = this->output_data_.back();
2180   else
2181     last = this->output_bss_.back();
2182   this->memsz_ = (last->address()
2183                   + last->data_size()
2184                   - this->vaddr_);
2185 }
2186
2187 // Set the TLS offsets of the sections in the PT_TLS segment.
2188
2189 void
2190 Output_segment::set_tls_offsets()
2191 {
2192   gold_assert(this->type_ == elfcpp::PT_TLS);
2193
2194   for (Output_data_list::iterator p = this->output_data_.begin();
2195        p != this->output_data_.end();
2196        ++p)
2197     (*p)->set_tls_offset(this->vaddr_);
2198
2199   for (Output_data_list::iterator p = this->output_bss_.begin();
2200        p != this->output_bss_.end();
2201        ++p)
2202     (*p)->set_tls_offset(this->vaddr_);
2203 }
2204
2205 // Return the number of Output_sections in an Output_segment.
2206
2207 unsigned int
2208 Output_segment::output_section_count() const
2209 {
2210   return (this->output_section_count_list(&this->output_data_)
2211           + this->output_section_count_list(&this->output_bss_));
2212 }
2213
2214 // Return the number of Output_sections in an Output_data_list.
2215
2216 unsigned int
2217 Output_segment::output_section_count_list(const Output_data_list* pdl) const
2218 {
2219   unsigned int count = 0;
2220   for (Output_data_list::const_iterator p = pdl->begin();
2221        p != pdl->end();
2222        ++p)
2223     {
2224       if ((*p)->is_section())
2225         ++count;
2226     }
2227   return count;
2228 }
2229
2230 // Write the segment data into *OPHDR.
2231
2232 template<int size, bool big_endian>
2233 void
2234 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
2235 {
2236   ophdr->put_p_type(this->type_);
2237   ophdr->put_p_offset(this->offset_);
2238   ophdr->put_p_vaddr(this->vaddr_);
2239   ophdr->put_p_paddr(this->paddr_);
2240   ophdr->put_p_filesz(this->filesz_);
2241   ophdr->put_p_memsz(this->memsz_);
2242   ophdr->put_p_flags(this->flags_);
2243   ophdr->put_p_align(this->addralign());
2244 }
2245
2246 // Write the section headers into V.
2247
2248 template<int size, bool big_endian>
2249 unsigned char*
2250 Output_segment::write_section_headers(const Layout* layout,
2251                                       const Stringpool* secnamepool,
2252                                       unsigned char* v,
2253                                       unsigned int *pshndx
2254                                       ACCEPT_SIZE_ENDIAN) const
2255 {
2256   // Every section that is attached to a segment must be attached to a
2257   // PT_LOAD segment, so we only write out section headers for PT_LOAD
2258   // segments.
2259   if (this->type_ != elfcpp::PT_LOAD)
2260     return v;
2261
2262   v = this->write_section_headers_list
2263       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
2264           layout, secnamepool, &this->output_data_, v, pshndx
2265           SELECT_SIZE_ENDIAN(size, big_endian));
2266   v = this->write_section_headers_list
2267       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
2268           layout, secnamepool, &this->output_bss_, v, pshndx
2269           SELECT_SIZE_ENDIAN(size, big_endian));
2270   return v;
2271 }
2272
2273 template<int size, bool big_endian>
2274 unsigned char*
2275 Output_segment::write_section_headers_list(const Layout* layout,
2276                                            const Stringpool* secnamepool,
2277                                            const Output_data_list* pdl,
2278                                            unsigned char* v,
2279                                            unsigned int* pshndx
2280                                            ACCEPT_SIZE_ENDIAN) const
2281 {
2282   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2283   for (Output_data_list::const_iterator p = pdl->begin();
2284        p != pdl->end();
2285        ++p)
2286     {
2287       if ((*p)->is_section())
2288         {
2289           const Output_section* ps = static_cast<const Output_section*>(*p);
2290           gold_assert(*pshndx == ps->out_shndx());
2291           elfcpp::Shdr_write<size, big_endian> oshdr(v);
2292           ps->write_header(layout, secnamepool, &oshdr);
2293           v += shdr_size;
2294           ++*pshndx;
2295         }
2296     }
2297   return v;
2298 }
2299
2300 // Output_file methods.
2301
2302 Output_file::Output_file(const General_options& options, Target* target)
2303   : options_(options),
2304     target_(target),
2305     name_(options.output_file_name()),
2306     o_(-1),
2307     file_size_(0),
2308     base_(NULL),
2309     map_is_anonymous_(false)
2310 {
2311 }
2312
2313 // Open the output file.
2314
2315 void
2316 Output_file::open(off_t file_size)
2317 {
2318   this->file_size_ = file_size;
2319
2320   // Unlink the file first; otherwise the open() may fail if the file
2321   // is busy (e.g. it's an executable that's currently being executed).
2322   //
2323   // However, the linker may be part of a system where a zero-length
2324   // file is created for it to write to, with tight permissions (gcc
2325   // 2.95 did something like this).  Unlinking the file would work
2326   // around those permission controls, so we only unlink if the file
2327   // has a non-zero size.  We also unlink only regular files to avoid
2328   // trouble with directories/etc.
2329   //
2330   // If we fail, continue; this command is merely a best-effort attempt
2331   // to improve the odds for open().
2332
2333   // We let the name "-" mean "stdout"
2334   if (strcmp(this->name_, "-") == 0)
2335     this->o_ = STDOUT_FILENO;
2336   else
2337     {
2338       struct stat s;
2339       if (::stat(this->name_, &s) == 0 && s.st_size != 0)
2340         unlink_if_ordinary(this->name_);
2341
2342       int mode = parameters->output_is_object() ? 0666 : 0777;
2343       int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
2344       if (o < 0)
2345         gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
2346       this->o_ = o;
2347     }
2348
2349   this->map();
2350 }
2351
2352 // Resize the output file.
2353
2354 void
2355 Output_file::resize(off_t file_size)
2356 {
2357   // If the mmap is mapping an anonymous memory buffer, this is easy:
2358   // just mremap to the new size.  If it's mapping to a file, we want
2359   // to unmap to flush to the file, then remap after growing the file.
2360   if (this->map_is_anonymous_)
2361     {
2362       void* base = ::mremap(this->base_, this->file_size_, file_size,
2363                             MREMAP_MAYMOVE);
2364       if (base == MAP_FAILED)
2365         gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
2366       this->base_ = static_cast<unsigned char*>(base);
2367       this->file_size_ = file_size;
2368     }
2369   else
2370     {
2371       this->unmap();
2372       this->file_size_ = file_size;
2373       this->map();
2374     }
2375 }
2376
2377 // Map the file into memory.
2378
2379 void
2380 Output_file::map()
2381 {
2382   const int o = this->o_;
2383
2384   // If the output file is not a regular file, don't try to mmap it;
2385   // instead, we'll mmap a block of memory (an anonymous buffer), and
2386   // then later write the buffer to the file.
2387   void* base;
2388   struct stat statbuf;
2389   if (o == STDOUT_FILENO || o == STDERR_FILENO
2390       || ::fstat(o, &statbuf) != 0
2391       || !S_ISREG(statbuf.st_mode))
2392     {
2393       this->map_is_anonymous_ = true;
2394       base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2395                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2396     }
2397   else
2398     {
2399       // Write out one byte to make the file the right size.
2400       if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
2401         gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
2402       char b = 0;
2403       if (::write(o, &b, 1) != 1)
2404         gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
2405
2406       // Map the file into memory.
2407       this->map_is_anonymous_ = false;
2408       base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2409                     MAP_SHARED, o, 0);
2410     }
2411   if (base == MAP_FAILED)
2412     gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
2413   this->base_ = static_cast<unsigned char*>(base);
2414 }
2415
2416 // Unmap the file from memory.
2417
2418 void
2419 Output_file::unmap()
2420 {
2421   if (::munmap(this->base_, this->file_size_) < 0)
2422     gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
2423   this->base_ = NULL;
2424 }
2425
2426 // Close the output file.
2427
2428 void
2429 Output_file::close()
2430 {
2431   // If the map isn't file-backed, we need to write it now.
2432   if (this->map_is_anonymous_)
2433     {
2434       size_t bytes_to_write = this->file_size_;
2435       while (bytes_to_write > 0)
2436         {
2437           ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
2438           if (bytes_written == 0)
2439             gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
2440           else if (bytes_written < 0)
2441             gold_error(_("%s: write: %s"), this->name_, strerror(errno));
2442           else
2443             bytes_to_write -= bytes_written;
2444         }
2445     }
2446   this->unmap();
2447
2448   // We don't close stdout or stderr
2449   if (this->o_ != STDOUT_FILENO && this->o_ != STDERR_FILENO)
2450     if (::close(this->o_) < 0)
2451       gold_error(_("%s: close: %s"), this->name_, strerror(errno));
2452   this->o_ = -1;
2453 }
2454
2455 // Instantiate the templates we need.  We could use the configure
2456 // script to restrict this to only the ones for implemented targets.
2457
2458 #ifdef HAVE_TARGET_32_LITTLE
2459 template
2460 off_t
2461 Output_section::add_input_section<32, false>(
2462     Sized_relobj<32, false>* object,
2463     unsigned int shndx,
2464     const char* secname,
2465     const elfcpp::Shdr<32, false>& shdr,
2466     unsigned int reloc_shndx);
2467 #endif
2468
2469 #ifdef HAVE_TARGET_32_BIG
2470 template
2471 off_t
2472 Output_section::add_input_section<32, true>(
2473     Sized_relobj<32, true>* object,
2474     unsigned int shndx,
2475     const char* secname,
2476     const elfcpp::Shdr<32, true>& shdr,
2477     unsigned int reloc_shndx);
2478 #endif
2479
2480 #ifdef HAVE_TARGET_64_LITTLE
2481 template
2482 off_t
2483 Output_section::add_input_section<64, false>(
2484     Sized_relobj<64, false>* object,
2485     unsigned int shndx,
2486     const char* secname,
2487     const elfcpp::Shdr<64, false>& shdr,
2488     unsigned int reloc_shndx);
2489 #endif
2490
2491 #ifdef HAVE_TARGET_64_BIG
2492 template
2493 off_t
2494 Output_section::add_input_section<64, true>(
2495     Sized_relobj<64, true>* object,
2496     unsigned int shndx,
2497     const char* secname,
2498     const elfcpp::Shdr<64, true>& shdr,
2499     unsigned int reloc_shndx);
2500 #endif
2501
2502 #ifdef HAVE_TARGET_32_LITTLE
2503 template
2504 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
2505 #endif
2506
2507 #ifdef HAVE_TARGET_32_BIG
2508 template
2509 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
2510 #endif
2511
2512 #ifdef HAVE_TARGET_64_LITTLE
2513 template
2514 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
2515 #endif
2516
2517 #ifdef HAVE_TARGET_64_BIG
2518 template
2519 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
2520 #endif
2521
2522 #ifdef HAVE_TARGET_32_LITTLE
2523 template
2524 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
2525 #endif
2526
2527 #ifdef HAVE_TARGET_32_BIG
2528 template
2529 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
2530 #endif
2531
2532 #ifdef HAVE_TARGET_64_LITTLE
2533 template
2534 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
2535 #endif
2536
2537 #ifdef HAVE_TARGET_64_BIG
2538 template
2539 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
2540 #endif
2541
2542 #ifdef HAVE_TARGET_32_LITTLE
2543 template
2544 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
2545 #endif
2546
2547 #ifdef HAVE_TARGET_32_BIG
2548 template
2549 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
2550 #endif
2551
2552 #ifdef HAVE_TARGET_64_LITTLE
2553 template
2554 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
2555 #endif
2556
2557 #ifdef HAVE_TARGET_64_BIG
2558 template
2559 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
2560 #endif
2561
2562 #ifdef HAVE_TARGET_32_LITTLE
2563 template
2564 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
2565 #endif
2566
2567 #ifdef HAVE_TARGET_32_BIG
2568 template
2569 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
2570 #endif
2571
2572 #ifdef HAVE_TARGET_64_LITTLE
2573 template
2574 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
2575 #endif
2576
2577 #ifdef HAVE_TARGET_64_BIG
2578 template
2579 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
2580 #endif
2581
2582 #ifdef HAVE_TARGET_32_LITTLE
2583 template
2584 class Output_data_got<32, false>;
2585 #endif
2586
2587 #ifdef HAVE_TARGET_32_BIG
2588 template
2589 class Output_data_got<32, true>;
2590 #endif
2591
2592 #ifdef HAVE_TARGET_64_LITTLE
2593 template
2594 class Output_data_got<64, false>;
2595 #endif
2596
2597 #ifdef HAVE_TARGET_64_BIG
2598 template
2599 class Output_data_got<64, true>;
2600 #endif
2601
2602 } // End namespace gold.