Remove extraneous newlines.
[platform/upstream/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
33 #include "parameters.h"
34 #include "object.h"
35 #include "symtab.h"
36 #include "reloc.h"
37 #include "merge.h"
38 #include "output.h"
39
40 namespace gold
41 {
42
43 // Output_data variables.
44
45 bool Output_data::sizes_are_fixed;
46
47 // Output_data methods.
48
49 Output_data::~Output_data()
50 {
51 }
52
53 // Set the address and offset.
54
55 void
56 Output_data::set_address(uint64_t addr, off_t off)
57 {
58   this->address_ = addr;
59   this->offset_ = off;
60
61   // Let the child class know.
62   this->do_set_address(addr, off);
63 }
64
65 // Return the default alignment for a size--32 or 64.
66
67 uint64_t
68 Output_data::default_alignment(int size)
69 {
70   if (size == 32)
71     return 4;
72   else if (size == 64)
73     return 8;
74   else
75     gold_unreachable();
76 }
77
78 // Output_section_header methods.  This currently assumes that the
79 // segment and section lists are complete at construction time.
80
81 Output_section_headers::Output_section_headers(
82     const Layout* layout,
83     const Layout::Segment_list* segment_list,
84     const Layout::Section_list* unattached_section_list,
85     const Stringpool* secnamepool)
86   : layout_(layout),
87     segment_list_(segment_list),
88     unattached_section_list_(unattached_section_list),
89     secnamepool_(secnamepool)
90 {
91   // Count all the sections.  Start with 1 for the null section.
92   off_t count = 1;
93   for (Layout::Segment_list::const_iterator p = segment_list->begin();
94        p != segment_list->end();
95        ++p)
96     if ((*p)->type() == elfcpp::PT_LOAD)
97       count += (*p)->output_section_count();
98   count += unattached_section_list->size();
99
100   const int size = parameters->get_size();
101   int shdr_size;
102   if (size == 32)
103     shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
104   else if (size == 64)
105     shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
106   else
107     gold_unreachable();
108
109   this->set_data_size(count * shdr_size);
110 }
111
112 // Write out the section headers.
113
114 void
115 Output_section_headers::do_write(Output_file* of)
116 {
117   if (parameters->get_size() == 32)
118     {
119       if (parameters->is_big_endian())
120         {
121 #ifdef HAVE_TARGET_32_BIG
122           this->do_sized_write<32, true>(of);
123 #else
124           gold_unreachable();
125 #endif
126         }
127       else
128         {
129 #ifdef HAVE_TARGET_32_LITTLE
130           this->do_sized_write<32, false>(of);
131 #else
132           gold_unreachable();
133 #endif
134         }
135     }
136   else if (parameters->get_size() == 64)
137     {
138       if (parameters->is_big_endian())
139         {
140 #ifdef HAVE_TARGET_64_BIG
141           this->do_sized_write<64, true>(of);
142 #else
143           gold_unreachable();
144 #endif
145         }
146       else
147         {
148 #ifdef HAVE_TARGET_64_LITTLE
149           this->do_sized_write<64, false>(of);
150 #else
151           gold_unreachable();
152 #endif
153         }
154     }
155   else
156     gold_unreachable();
157 }
158
159 template<int size, bool big_endian>
160 void
161 Output_section_headers::do_sized_write(Output_file* of)
162 {
163   off_t all_shdrs_size = this->data_size();
164   unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
165
166   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
167   unsigned char* v = view;
168
169   {
170     typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
171     oshdr.put_sh_name(0);
172     oshdr.put_sh_type(elfcpp::SHT_NULL);
173     oshdr.put_sh_flags(0);
174     oshdr.put_sh_addr(0);
175     oshdr.put_sh_offset(0);
176     oshdr.put_sh_size(0);
177     oshdr.put_sh_link(0);
178     oshdr.put_sh_info(0);
179     oshdr.put_sh_addralign(0);
180     oshdr.put_sh_entsize(0);
181   }
182
183   v += shdr_size;
184
185   unsigned shndx = 1;
186   for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
187        p != this->segment_list_->end();
188        ++p)
189     v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
190             this->layout_, this->secnamepool_, v, &shndx
191             SELECT_SIZE_ENDIAN(size, big_endian));
192   for (Layout::Section_list::const_iterator p =
193          this->unattached_section_list_->begin();
194        p != this->unattached_section_list_->end();
195        ++p)
196     {
197       gold_assert(shndx == (*p)->out_shndx());
198       elfcpp::Shdr_write<size, big_endian> oshdr(v);
199       (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
200       v += shdr_size;
201       ++shndx;
202     }
203
204   of->write_output_view(this->offset(), all_shdrs_size, view);
205 }
206
207 // Output_segment_header methods.
208
209 Output_segment_headers::Output_segment_headers(
210     const Layout::Segment_list& segment_list)
211   : segment_list_(segment_list)
212 {
213   const int size = parameters->get_size();
214   int phdr_size;
215   if (size == 32)
216     phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
217   else if (size == 64)
218     phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
219   else
220     gold_unreachable();
221
222   this->set_data_size(segment_list.size() * phdr_size);
223 }
224
225 void
226 Output_segment_headers::do_write(Output_file* of)
227 {
228   if (parameters->get_size() == 32)
229     {
230       if (parameters->is_big_endian())
231         {
232 #ifdef HAVE_TARGET_32_BIG
233           this->do_sized_write<32, true>(of);
234 #else
235           gold_unreachable();
236 #endif
237         }
238       else
239         {
240 #ifdef HAVE_TARGET_32_LITTLE
241         this->do_sized_write<32, false>(of);
242 #else
243         gold_unreachable();
244 #endif
245         }
246     }
247   else if (parameters->get_size() == 64)
248     {
249       if (parameters->is_big_endian())
250         {
251 #ifdef HAVE_TARGET_64_BIG
252           this->do_sized_write<64, true>(of);
253 #else
254           gold_unreachable();
255 #endif
256         }
257       else
258         {
259 #ifdef HAVE_TARGET_64_LITTLE
260           this->do_sized_write<64, false>(of);
261 #else
262           gold_unreachable();
263 #endif
264         }
265     }
266   else
267     gold_unreachable();
268 }
269
270 template<int size, bool big_endian>
271 void
272 Output_segment_headers::do_sized_write(Output_file* of)
273 {
274   const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
275   off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
276   unsigned char* view = of->get_output_view(this->offset(),
277                                             all_phdrs_size);
278   unsigned char* v = view;
279   for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
280        p != this->segment_list_.end();
281        ++p)
282     {
283       elfcpp::Phdr_write<size, big_endian> ophdr(v);
284       (*p)->write_header(&ophdr);
285       v += phdr_size;
286     }
287
288   of->write_output_view(this->offset(), all_phdrs_size, view);
289 }
290
291 // Output_file_header methods.
292
293 Output_file_header::Output_file_header(const Target* target,
294                                        const Symbol_table* symtab,
295                                        const Output_segment_headers* osh)
296   : target_(target),
297     symtab_(symtab),
298     segment_header_(osh),
299     section_header_(NULL),
300     shstrtab_(NULL)
301 {
302   const int size = parameters->get_size();
303   int ehdr_size;
304   if (size == 32)
305     ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
306   else if (size == 64)
307     ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
308   else
309     gold_unreachable();
310
311   this->set_data_size(ehdr_size);
312 }
313
314 // Set the section table information for a file header.
315
316 void
317 Output_file_header::set_section_info(const Output_section_headers* shdrs,
318                                      const Output_section* shstrtab)
319 {
320   this->section_header_ = shdrs;
321   this->shstrtab_ = shstrtab;
322 }
323
324 // Write out the file header.
325
326 void
327 Output_file_header::do_write(Output_file* of)
328 {
329   if (parameters->get_size() == 32)
330     {
331       if (parameters->is_big_endian())
332         {
333 #ifdef HAVE_TARGET_32_BIG
334           this->do_sized_write<32, true>(of);
335 #else
336           gold_unreachable();
337 #endif
338         }
339       else
340         {
341 #ifdef HAVE_TARGET_32_LITTLE
342           this->do_sized_write<32, false>(of);
343 #else
344           gold_unreachable();
345 #endif
346         }
347     }
348   else if (parameters->get_size() == 64)
349     {
350       if (parameters->is_big_endian())
351         {
352 #ifdef HAVE_TARGET_64_BIG
353           this->do_sized_write<64, true>(of);
354 #else
355           gold_unreachable();
356 #endif
357         }
358       else
359         {
360 #ifdef HAVE_TARGET_64_LITTLE
361           this->do_sized_write<64, false>(of);
362 #else
363           gold_unreachable();
364 #endif
365         }
366     }
367   else
368     gold_unreachable();
369 }
370
371 // Write out the file header with appropriate size and endianess.
372
373 template<int size, bool big_endian>
374 void
375 Output_file_header::do_sized_write(Output_file* of)
376 {
377   gold_assert(this->offset() == 0);
378
379   int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
380   unsigned char* view = of->get_output_view(0, ehdr_size);
381   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
382
383   unsigned char e_ident[elfcpp::EI_NIDENT];
384   memset(e_ident, 0, elfcpp::EI_NIDENT);
385   e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
386   e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
387   e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
388   e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
389   if (size == 32)
390     e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
391   else if (size == 64)
392     e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
393   else
394     gold_unreachable();
395   e_ident[elfcpp::EI_DATA] = (big_endian
396                               ? elfcpp::ELFDATA2MSB
397                               : elfcpp::ELFDATA2LSB);
398   e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
399   // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
400   oehdr.put_e_ident(e_ident);
401
402   elfcpp::ET e_type;
403   // FIXME: ET_DYN.
404   if (parameters->output_is_object())
405     e_type = elfcpp::ET_REL;
406   else
407     e_type = elfcpp::ET_EXEC;
408   oehdr.put_e_type(e_type);
409
410   oehdr.put_e_machine(this->target_->machine_code());
411   oehdr.put_e_version(elfcpp::EV_CURRENT);
412
413   // FIXME: Need to support -e, and target specific entry symbol.
414   Symbol* sym = this->symtab_->lookup("_start");
415   typename Sized_symbol<size>::Value_type v;
416   if (sym == NULL)
417     v = 0;
418   else
419     {
420       Sized_symbol<size>* ssym;
421       ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
422         sym SELECT_SIZE(size));
423       v = ssym->value();
424     }
425   oehdr.put_e_entry(v);
426
427   oehdr.put_e_phoff(this->segment_header_->offset());
428   oehdr.put_e_shoff(this->section_header_->offset());
429
430   // FIXME: The target needs to set the flags.
431   oehdr.put_e_flags(0);
432
433   oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
434   oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
435   oehdr.put_e_phnum(this->segment_header_->data_size()
436                      / elfcpp::Elf_sizes<size>::phdr_size);
437   oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
438   oehdr.put_e_shnum(this->section_header_->data_size()
439                      / elfcpp::Elf_sizes<size>::shdr_size);
440   oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
441
442   of->write_output_view(0, ehdr_size, view);
443 }
444
445 // Output_data_const methods.
446
447 void
448 Output_data_const::do_write(Output_file* of)
449 {
450   of->write(this->offset(), this->data_.data(), this->data_.size());
451 }
452
453 // Output_data_const_buffer methods.
454
455 void
456 Output_data_const_buffer::do_write(Output_file* of)
457 {
458   of->write(this->offset(), this->p_, this->data_size());
459 }
460
461 // Output_section_data methods.
462
463 // Record the output section, and set the entry size and such.
464
465 void
466 Output_section_data::set_output_section(Output_section* os)
467 {
468   gold_assert(this->output_section_ == NULL);
469   this->output_section_ = os;
470   this->do_adjust_output_section(os);
471 }
472
473 // Return the section index of the output section.
474
475 unsigned int
476 Output_section_data::do_out_shndx() const
477 {
478   gold_assert(this->output_section_ != NULL);
479   return this->output_section_->out_shndx();
480 }
481
482 // Output_data_strtab methods.
483
484 // Set the address.  We don't actually care about the address, but we
485 // do set our final size.
486
487 void
488 Output_data_strtab::do_set_address(uint64_t, off_t)
489 {
490   this->strtab_->set_string_offsets();
491   this->set_data_size(this->strtab_->get_strtab_size());
492 }
493
494 // Write out a string table.
495
496 void
497 Output_data_strtab::do_write(Output_file* of)
498 {
499   this->strtab_->write(of, this->offset());
500 }
501
502 // Output_reloc methods.
503
504 // Get the symbol index of a relocation.
505
506 template<bool dynamic, int size, bool big_endian>
507 unsigned int
508 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
509   const
510 {
511   unsigned int index;
512   switch (this->local_sym_index_)
513     {
514     case INVALID_CODE:
515       gold_unreachable();
516
517     case GSYM_CODE:
518       if (this->u1_.gsym == NULL)
519         index = 0;
520       else if (dynamic)
521         index = this->u1_.gsym->dynsym_index();
522       else
523         index = this->u1_.gsym->symtab_index();
524       break;
525
526     case SECTION_CODE:
527       if (dynamic)
528         index = this->u1_.os->dynsym_index();
529       else
530         index = this->u1_.os->symtab_index();
531       break;
532
533     default:
534       if (dynamic)
535         {
536           // FIXME: It seems that some targets may need to generate
537           // dynamic relocations against local symbols for some
538           // reasons.  This will have to be addressed at some point.
539           gold_unreachable();
540         }
541       else
542         index = this->u1_.relobj->symtab_index(this->local_sym_index_);
543       break;
544     }
545   gold_assert(index != -1U);
546   return index;
547 }
548
549 // Write out the offset and info fields of a Rel or Rela relocation
550 // entry.
551
552 template<bool dynamic, int size, bool big_endian>
553 template<typename Write_rel>
554 void
555 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
556     Write_rel* wr) const
557 {
558   Address address = this->address_;
559   if (this->shndx_ != INVALID_CODE)
560     {
561       off_t off;
562       Output_section* os = this->u2_.relobj->output_section(this->shndx_,
563                                                             &off);
564       gold_assert(os != NULL);
565       address += os->address() + off;
566     }
567   else if (this->u2_.od != NULL)
568     address += this->u2_.od->address();
569   wr->put_r_offset(address);
570   wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(),
571                                           this->type_));
572 }
573
574 // Write out a Rel relocation.
575
576 template<bool dynamic, int size, bool big_endian>
577 void
578 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
579     unsigned char* pov) const
580 {
581   elfcpp::Rel_write<size, big_endian> orel(pov);
582   this->write_rel(&orel);
583 }
584
585 // Write out a Rela relocation.
586
587 template<bool dynamic, int size, bool big_endian>
588 void
589 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
590     unsigned char* pov) const
591 {
592   elfcpp::Rela_write<size, big_endian> orel(pov);
593   this->rel_.write_rel(&orel);
594   orel.put_r_addend(this->addend_);
595 }
596
597 // Output_data_reloc_base methods.
598
599 // Adjust the output section.
600
601 template<int sh_type, bool dynamic, int size, bool big_endian>
602 void
603 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
604     ::do_adjust_output_section(Output_section* os)
605 {
606   if (sh_type == elfcpp::SHT_REL)
607     os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
608   else if (sh_type == elfcpp::SHT_RELA)
609     os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
610   else
611     gold_unreachable();
612   if (dynamic)
613     os->set_should_link_to_dynsym();
614   else
615     os->set_should_link_to_symtab();
616 }
617
618 // Write out relocation data.
619
620 template<int sh_type, bool dynamic, int size, bool big_endian>
621 void
622 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
623     Output_file* of)
624 {
625   const off_t off = this->offset();
626   const off_t oview_size = this->data_size();
627   unsigned char* const oview = of->get_output_view(off, oview_size);
628
629   unsigned char* pov = oview;
630   for (typename Relocs::const_iterator p = this->relocs_.begin();
631        p != this->relocs_.end();
632        ++p)
633     {
634       p->write(pov);
635       pov += reloc_size;
636     }
637
638   gold_assert(pov - oview == oview_size);
639
640   of->write_output_view(off, oview_size, oview);
641
642   // We no longer need the relocation entries.
643   this->relocs_.clear();
644 }
645
646 // Output_data_got::Got_entry methods.
647
648 // Write out the entry.
649
650 template<int size, bool big_endian>
651 void
652 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
653 {
654   Valtype val = 0;
655
656   switch (this->local_sym_index_)
657     {
658     case GSYM_CODE:
659       {
660         Symbol* gsym = this->u_.gsym;
661
662         // If the symbol is resolved locally, we need to write out its
663         // value.  Otherwise we just write zero.  The target code is
664         // responsible for creating a relocation entry to fill in the
665         // value at runtime.
666         if (gsym->final_value_is_known())
667           {
668             Sized_symbol<size>* sgsym;
669             // This cast is a bit ugly.  We don't want to put a
670             // virtual method in Symbol, because we want Symbol to be
671             // as small as possible.
672             sgsym = static_cast<Sized_symbol<size>*>(gsym);
673             val = sgsym->value();
674           }
675       }
676       break;
677
678     case CONSTANT_CODE:
679       val = this->u_.constant;
680       break;
681
682     default:
683       val = this->u_.object->local_symbol_value(this->local_sym_index_);
684       break;
685     }
686
687   elfcpp::Swap<size, big_endian>::writeval(pov, val);
688 }
689
690 // Output_data_got methods.
691
692 // Add an entry for a global symbol to the GOT.  This returns true if
693 // this is a new GOT entry, false if the symbol already had a GOT
694 // entry.
695
696 template<int size, bool big_endian>
697 bool
698 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
699 {
700   if (gsym->has_got_offset())
701     return false;
702
703   this->entries_.push_back(Got_entry(gsym));
704   this->set_got_size();
705   gsym->set_got_offset(this->last_got_offset());
706   return true;
707 }
708
709 // Add an entry for a local symbol to the GOT.  This returns true if
710 // this is a new GOT entry, false if the symbol already has a GOT
711 // entry.
712
713 template<int size, bool big_endian>
714 bool
715 Output_data_got<size, big_endian>::add_local(
716     Sized_relobj<size, big_endian>* object,
717     unsigned int symndx)
718 {
719   if (object->local_has_got_offset(symndx))
720     return false;
721   this->entries_.push_back(Got_entry(object, symndx));
722   this->set_got_size();
723   object->set_local_got_offset(symndx, this->last_got_offset());
724   return true;
725 }
726
727 // Write out the GOT.
728
729 template<int size, bool big_endian>
730 void
731 Output_data_got<size, big_endian>::do_write(Output_file* of)
732 {
733   const int add = size / 8;
734
735   const off_t off = this->offset();
736   const off_t oview_size = this->data_size();
737   unsigned char* const oview = of->get_output_view(off, oview_size);
738
739   unsigned char* pov = oview;
740   for (typename Got_entries::const_iterator p = this->entries_.begin();
741        p != this->entries_.end();
742        ++p)
743     {
744       p->write(pov);
745       pov += add;
746     }
747
748   gold_assert(pov - oview == oview_size);
749
750   of->write_output_view(off, oview_size, oview);
751
752   // We no longer need the GOT entries.
753   this->entries_.clear();
754 }
755
756 // Output_data_dynamic::Dynamic_entry methods.
757
758 // Write out the entry.
759
760 template<int size, bool big_endian>
761 void
762 Output_data_dynamic::Dynamic_entry::write(
763     unsigned char* pov,
764     const Stringpool* pool
765     ACCEPT_SIZE_ENDIAN) const
766 {
767   typename elfcpp::Elf_types<size>::Elf_WXword val;
768   switch (this->classification_)
769     {
770     case DYNAMIC_NUMBER:
771       val = this->u_.val;
772       break;
773
774     case DYNAMIC_SECTION_ADDRESS:
775       val = this->u_.od->address();
776       break;
777
778     case DYNAMIC_SECTION_SIZE:
779       val = this->u_.od->data_size();
780       break;
781
782     case DYNAMIC_SYMBOL:
783       {
784         const Sized_symbol<size>* s =
785           static_cast<const Sized_symbol<size>*>(this->u_.sym);
786         val = s->value();
787       }
788       break;
789
790     case DYNAMIC_STRING:
791       val = pool->get_offset(this->u_.str);
792       break;
793
794     default:
795       gold_unreachable();
796     }
797
798   elfcpp::Dyn_write<size, big_endian> dw(pov);
799   dw.put_d_tag(this->tag_);
800   dw.put_d_val(val);
801 }
802
803 // Output_data_dynamic methods.
804
805 // Adjust the output section to set the entry size.
806
807 void
808 Output_data_dynamic::do_adjust_output_section(Output_section* os)
809 {
810   if (parameters->get_size() == 32)
811     os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
812   else if (parameters->get_size() == 64)
813     os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
814   else
815     gold_unreachable();
816 }
817
818 // Set the final data size.
819
820 void
821 Output_data_dynamic::do_set_address(uint64_t, off_t)
822 {
823   // Add the terminating entry.
824   this->add_constant(elfcpp::DT_NULL, 0);
825
826   int dyn_size;
827   if (parameters->get_size() == 32)
828     dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
829   else if (parameters->get_size() == 64)
830     dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
831   else
832     gold_unreachable();
833   this->set_data_size(this->entries_.size() * dyn_size);
834 }
835
836 // Write out the dynamic entries.
837
838 void
839 Output_data_dynamic::do_write(Output_file* of)
840 {
841   if (parameters->get_size() == 32)
842     {
843       if (parameters->is_big_endian())
844         {
845 #ifdef HAVE_TARGET_32_BIG
846           this->sized_write<32, true>(of);
847 #else
848           gold_unreachable();
849 #endif
850         }
851       else
852         {
853 #ifdef HAVE_TARGET_32_LITTLE
854           this->sized_write<32, false>(of);
855 #else
856           gold_unreachable();
857 #endif
858         }
859     }
860   else if (parameters->get_size() == 64)
861     {
862       if (parameters->is_big_endian())
863         {
864 #ifdef HAVE_TARGET_64_BIG
865           this->sized_write<64, true>(of);
866 #else
867           gold_unreachable();
868 #endif
869         }
870       else
871         {
872 #ifdef HAVE_TARGET_64_LITTLE
873           this->sized_write<64, false>(of);
874 #else
875           gold_unreachable();
876 #endif
877         }
878     }
879   else
880     gold_unreachable();
881 }
882
883 template<int size, bool big_endian>
884 void
885 Output_data_dynamic::sized_write(Output_file* of)
886 {
887   const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
888
889   const off_t offset = this->offset();
890   const off_t oview_size = this->data_size();
891   unsigned char* const oview = of->get_output_view(offset, oview_size);
892
893   unsigned char* pov = oview;
894   for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
895        p != this->entries_.end();
896        ++p)
897     {
898       p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
899           pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
900       pov += dyn_size;
901     }
902
903   gold_assert(pov - oview == oview_size);
904
905   of->write_output_view(offset, oview_size, oview);
906
907   // We no longer need the dynamic entries.
908   this->entries_.clear();
909 }
910
911 // Output_section::Input_section methods.
912
913 // Return the data size.  For an input section we store the size here.
914 // For an Output_section_data, we have to ask it for the size.
915
916 off_t
917 Output_section::Input_section::data_size() const
918 {
919   if (this->is_input_section())
920     return this->u1_.data_size;
921   else
922     return this->u2_.posd->data_size();
923 }
924
925 // Set the address and file offset.
926
927 void
928 Output_section::Input_section::set_address(uint64_t addr, off_t off,
929                                            off_t secoff)
930 {
931   if (this->is_input_section())
932     this->u2_.object->set_section_offset(this->shndx_, off - secoff);
933   else
934     this->u2_.posd->set_address(addr, off);
935 }
936
937 // Try to turn an input address into an output address.
938
939 bool
940 Output_section::Input_section::output_address(const Relobj* object,
941                                               unsigned int shndx,
942                                               off_t offset,
943                                               uint64_t output_section_address,
944                                               uint64_t *poutput) const
945 {
946   if (!this->is_input_section())
947     return this->u2_.posd->output_address(object, shndx, offset,
948                                           output_section_address, poutput);
949   else
950     {
951       if (this->shndx_ != shndx
952           || this->u2_.object != object)
953         return false;
954       off_t output_offset;
955       Output_section* os = object->output_section(shndx, &output_offset);
956       gold_assert(os != NULL);
957       *poutput = output_section_address + output_offset + offset;
958       return true;
959     }
960 }
961
962 // Write out the data.  We don't have to do anything for an input
963 // section--they are handled via Object::relocate--but this is where
964 // we write out the data for an Output_section_data.
965
966 void
967 Output_section::Input_section::write(Output_file* of)
968 {
969   if (!this->is_input_section())
970     this->u2_.posd->write(of);
971 }
972
973 // Output_section methods.
974
975 // Construct an Output_section.  NAME will point into a Stringpool.
976
977 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
978                                elfcpp::Elf_Xword flags)
979   : name_(name),
980     addralign_(0),
981     entsize_(0),
982     link_section_(NULL),
983     link_(0),
984     info_section_(NULL),
985     info_(0),
986     type_(type),
987     flags_(flags),
988     out_shndx_(0),
989     symtab_index_(0),
990     dynsym_index_(0),
991     input_sections_(),
992     first_input_offset_(0),
993     fills_(),
994     needs_symtab_index_(false),
995     needs_dynsym_index_(false),
996     should_link_to_symtab_(false),
997     should_link_to_dynsym_(false)
998 {
999 }
1000
1001 Output_section::~Output_section()
1002 {
1003 }
1004
1005 // Set the entry size.
1006
1007 void
1008 Output_section::set_entsize(uint64_t v)
1009 {
1010   if (this->entsize_ == 0)
1011     this->entsize_ = v;
1012   else
1013     gold_assert(this->entsize_ == v);
1014 }
1015
1016 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1017 // OBJECT, to the Output_section.  Return the offset of the input
1018 // section within the output section.  We don't always keep track of
1019 // input sections for an Output_section.  Instead, each Object keeps
1020 // track of the Output_section for each of its input sections.
1021
1022 template<int size, bool big_endian>
1023 off_t
1024 Output_section::add_input_section(Relobj* object, unsigned int shndx,
1025                                   const char* secname,
1026                                   const elfcpp::Shdr<size, big_endian>& shdr)
1027 {
1028   elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1029   if ((addralign & (addralign - 1)) != 0)
1030     {
1031       object->error(_("invalid alignment %lu for section \"%s\""),
1032                     static_cast<unsigned long>(addralign), secname);
1033       addralign = 1;
1034     }
1035
1036   if (addralign > this->addralign_)
1037     this->addralign_ = addralign;
1038
1039   // If this is a SHF_MERGE section, we pass all the input sections to
1040   // a Output_data_merge.
1041   if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0)
1042     {
1043       if (this->add_merge_input_section(object, shndx, shdr.get_sh_flags(),
1044                                         shdr.get_sh_entsize(),
1045                                         addralign))
1046         {
1047           // Tell the relocation routines that they need to call the
1048           // output_address method to determine the final address.
1049           return -1;
1050         }
1051     }
1052
1053   off_t offset_in_section = this->data_size();
1054   off_t aligned_offset_in_section = align_address(offset_in_section,
1055                                                   addralign);
1056
1057   if (aligned_offset_in_section > offset_in_section
1058       && (shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0
1059       && object->target()->has_code_fill())
1060     {
1061       // We need to add some fill data.  Using fill_list_ when
1062       // possible is an optimization, since we will often have fill
1063       // sections without input sections.
1064       off_t fill_len = aligned_offset_in_section - offset_in_section;
1065       if (this->input_sections_.empty())
1066         this->fills_.push_back(Fill(offset_in_section, fill_len));
1067       else
1068         {
1069           // FIXME: When relaxing, the size needs to adjust to
1070           // maintain a constant alignment.
1071           std::string fill_data(object->target()->code_fill(fill_len));
1072           Output_data_const* odc = new Output_data_const(fill_data, 1);
1073           this->input_sections_.push_back(Input_section(odc));
1074         }
1075     }
1076
1077   this->set_data_size(aligned_offset_in_section + shdr.get_sh_size());
1078
1079   // We need to keep track of this section if we are already keeping
1080   // track of sections, or if we are relaxing.  FIXME: Add test for
1081   // relaxing.
1082   if (!this->input_sections_.empty())
1083     this->input_sections_.push_back(Input_section(object, shndx,
1084                                                   shdr.get_sh_size(),
1085                                                   addralign));
1086
1087   return aligned_offset_in_section;
1088 }
1089
1090 // Add arbitrary data to an output section.
1091
1092 void
1093 Output_section::add_output_section_data(Output_section_data* posd)
1094 {
1095   Input_section inp(posd);
1096   this->add_output_section_data(&inp);
1097 }
1098
1099 // Add arbitrary data to an output section by Input_section.
1100
1101 void
1102 Output_section::add_output_section_data(Input_section* inp)
1103 {
1104   if (this->input_sections_.empty())
1105     this->first_input_offset_ = this->data_size();
1106
1107   this->input_sections_.push_back(*inp);
1108
1109   uint64_t addralign = inp->addralign();
1110   if (addralign > this->addralign_)
1111     this->addralign_ = addralign;
1112
1113   inp->set_output_section(this);
1114 }
1115
1116 // Add a merge section to an output section.
1117
1118 void
1119 Output_section::add_output_merge_section(Output_section_data* posd,
1120                                          bool is_string, uint64_t entsize)
1121 {
1122   Input_section inp(posd, is_string, entsize);
1123   this->add_output_section_data(&inp);
1124 }
1125
1126 // Add an input section to a SHF_MERGE section.
1127
1128 bool
1129 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1130                                         uint64_t flags, uint64_t entsize,
1131                                         uint64_t addralign)
1132 {
1133   // We only merge constants if the alignment is not more than the
1134   // entry size.  This could be handled, but it's unusual.
1135   if (addralign > entsize)
1136     return false;
1137
1138   bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1139   Input_section_list::iterator p;
1140   for (p = this->input_sections_.begin();
1141        p != this->input_sections_.end();
1142        ++p)
1143     if (p->is_merge_section(is_string, entsize))
1144       break;
1145
1146   // We handle the actual constant merging in Output_merge_data or
1147   // Output_merge_string_data.
1148   if (p != this->input_sections_.end())
1149     p->add_input_section(object, shndx);
1150   else
1151     {
1152       Output_section_data* posd;
1153       if (!is_string)
1154         posd = new Output_merge_data(entsize);
1155       else if (entsize == 1)
1156         posd = new Output_merge_string<char>();
1157       else if (entsize == 2)
1158         posd = new Output_merge_string<uint16_t>();
1159       else if (entsize == 4)
1160         posd = new Output_merge_string<uint32_t>();
1161       else
1162         return false;
1163
1164       this->add_output_merge_section(posd, is_string, entsize);
1165       posd->add_input_section(object, shndx);
1166     }
1167
1168   return true;
1169 }
1170
1171 // Return the output virtual address of OFFSET relative to the start
1172 // of input section SHNDX in object OBJECT.
1173
1174 uint64_t
1175 Output_section::output_address(const Relobj* object, unsigned int shndx,
1176                                off_t offset) const
1177 {
1178   uint64_t addr = this->address() + this->first_input_offset_;
1179   for (Input_section_list::const_iterator p = this->input_sections_.begin();
1180        p != this->input_sections_.end();
1181        ++p)
1182     {
1183       addr = align_address(addr, p->addralign());
1184       uint64_t output;
1185       if (p->output_address(object, shndx, offset, addr, &output))
1186         return output;
1187       addr += p->data_size();
1188     }
1189
1190   // If we get here, it means that we don't know the mapping for this
1191   // input section.  This might happen in principle if
1192   // add_input_section were called before add_output_section_data.
1193   // But it should never actually happen.
1194
1195   gold_unreachable();
1196 }
1197
1198 // Set the address of an Output_section.  This is where we handle
1199 // setting the addresses of any Output_section_data objects.
1200
1201 void
1202 Output_section::do_set_address(uint64_t address, off_t startoff)
1203 {
1204   if (this->input_sections_.empty())
1205     return;
1206
1207   off_t off = startoff + this->first_input_offset_;
1208   for (Input_section_list::iterator p = this->input_sections_.begin();
1209        p != this->input_sections_.end();
1210        ++p)
1211     {
1212       off = align_address(off, p->addralign());
1213       p->set_address(address + (off - startoff), off, startoff);
1214       off += p->data_size();
1215     }
1216
1217   this->set_data_size(off - startoff);
1218 }
1219
1220 // Write the section header to *OSHDR.
1221
1222 template<int size, bool big_endian>
1223 void
1224 Output_section::write_header(const Layout* layout,
1225                              const Stringpool* secnamepool,
1226                              elfcpp::Shdr_write<size, big_endian>* oshdr) const
1227 {
1228   oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1229   oshdr->put_sh_type(this->type_);
1230   oshdr->put_sh_flags(this->flags_);
1231   oshdr->put_sh_addr(this->address());
1232   oshdr->put_sh_offset(this->offset());
1233   oshdr->put_sh_size(this->data_size());
1234   if (this->link_section_ != NULL)
1235     oshdr->put_sh_link(this->link_section_->out_shndx());
1236   else if (this->should_link_to_symtab_)
1237     oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1238   else if (this->should_link_to_dynsym_)
1239     oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1240   else
1241     oshdr->put_sh_link(this->link_);
1242   if (this->info_section_ != NULL)
1243     oshdr->put_sh_info(this->info_section_->out_shndx());
1244   else
1245     oshdr->put_sh_info(this->info_);
1246   oshdr->put_sh_addralign(this->addralign_);
1247   oshdr->put_sh_entsize(this->entsize_);
1248 }
1249
1250 // Write out the data.  For input sections the data is written out by
1251 // Object::relocate, but we have to handle Output_section_data objects
1252 // here.
1253
1254 void
1255 Output_section::do_write(Output_file* of)
1256 {
1257   off_t output_section_file_offset = this->offset();
1258   for (Fill_list::iterator p = this->fills_.begin();
1259        p != this->fills_.end();
1260        ++p)
1261     {
1262       std::string fill_data(of->target()->code_fill(p->length()));
1263       of->write(output_section_file_offset + p->section_offset(),
1264                 fill_data.data(), fill_data.size());
1265     }
1266
1267   for (Input_section_list::iterator p = this->input_sections_.begin();
1268        p != this->input_sections_.end();
1269        ++p)
1270     p->write(of);
1271 }
1272
1273 // Output segment methods.
1274
1275 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1276   : output_data_(),
1277     output_bss_(),
1278     vaddr_(0),
1279     paddr_(0),
1280     memsz_(0),
1281     align_(0),
1282     offset_(0),
1283     filesz_(0),
1284     type_(type),
1285     flags_(flags),
1286     is_align_known_(false)
1287 {
1288 }
1289
1290 // Add an Output_section to an Output_segment.
1291
1292 void
1293 Output_segment::add_output_section(Output_section* os,
1294                                    elfcpp::Elf_Word seg_flags,
1295                                    bool front)
1296 {
1297   gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1298   gold_assert(!this->is_align_known_);
1299
1300   // Update the segment flags.
1301   this->flags_ |= seg_flags;
1302
1303   Output_segment::Output_data_list* pdl;
1304   if (os->type() == elfcpp::SHT_NOBITS)
1305     pdl = &this->output_bss_;
1306   else
1307     pdl = &this->output_data_;
1308
1309   // So that PT_NOTE segments will work correctly, we need to ensure
1310   // that all SHT_NOTE sections are adjacent.  This will normally
1311   // happen automatically, because all the SHT_NOTE input sections
1312   // will wind up in the same output section.  However, it is possible
1313   // for multiple SHT_NOTE input sections to have different section
1314   // flags, and thus be in different output sections, but for the
1315   // different section flags to map into the same segment flags and
1316   // thus the same output segment.
1317
1318   // Note that while there may be many input sections in an output
1319   // section, there are normally only a few output sections in an
1320   // output segment.  This loop is expected to be fast.
1321
1322   if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1323     {
1324       Output_segment::Output_data_list::iterator p = pdl->end();
1325       do
1326         {
1327           --p;
1328           if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1329             {
1330               // We don't worry about the FRONT parameter.
1331               ++p;
1332               pdl->insert(p, os);
1333               return;
1334             }
1335         }
1336       while (p != pdl->begin());
1337     }
1338
1339   // Similarly, so that PT_TLS segments will work, we need to group
1340   // SHF_TLS sections.  An SHF_TLS/SHT_NOBITS section is a special
1341   // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1342   // SHF_TLS/SHT_PROGBITS sections.  This lets us set up PT_TLS
1343   // correctly.
1344   if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
1345     {
1346       pdl = &this->output_data_;
1347       bool nobits = os->type() == elfcpp::SHT_NOBITS;
1348       bool sawtls = false;
1349       Output_segment::Output_data_list::iterator p = pdl->end();
1350       do
1351         {
1352           --p;
1353           bool insert;
1354           if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1355             {
1356               sawtls = true;
1357               // Put a NOBITS section after the first TLS section.
1358               // But a PROGBITS section after the first TLS/PROGBITS
1359               // section.
1360               insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1361             }
1362           else
1363             {
1364               // If we've gone past the TLS sections, but we've seen a
1365               // TLS section, then we need to insert this section now.
1366               insert = sawtls;
1367             }
1368
1369           if (insert)
1370             {
1371               // We don't worry about the FRONT parameter.
1372               ++p;
1373               pdl->insert(p, os);
1374               return;
1375             }
1376         }
1377       while (p != pdl->begin());
1378
1379       // There are no TLS sections yet; put this one at the requested
1380       // location in the section list.
1381     }
1382
1383   if (front)
1384     pdl->push_front(os);
1385   else
1386     pdl->push_back(os);
1387 }
1388
1389 // Add an Output_data (which is not an Output_section) to the start of
1390 // a segment.
1391
1392 void
1393 Output_segment::add_initial_output_data(Output_data* od)
1394 {
1395   gold_assert(!this->is_align_known_);
1396   this->output_data_.push_front(od);
1397 }
1398
1399 // Return the maximum alignment of the Output_data in Output_segment.
1400 // Once we compute this, we prohibit new sections from being added.
1401
1402 uint64_t
1403 Output_segment::addralign()
1404 {
1405   if (!this->is_align_known_)
1406     {
1407       uint64_t addralign;
1408
1409       addralign = Output_segment::maximum_alignment(&this->output_data_);
1410       if (addralign > this->align_)
1411         this->align_ = addralign;
1412
1413       addralign = Output_segment::maximum_alignment(&this->output_bss_);
1414       if (addralign > this->align_)
1415         this->align_ = addralign;
1416
1417       this->is_align_known_ = true;
1418     }
1419
1420   return this->align_;
1421 }
1422
1423 // Return the maximum alignment of a list of Output_data.
1424
1425 uint64_t
1426 Output_segment::maximum_alignment(const Output_data_list* pdl)
1427 {
1428   uint64_t ret = 0;
1429   for (Output_data_list::const_iterator p = pdl->begin();
1430        p != pdl->end();
1431        ++p)
1432     {
1433       uint64_t addralign = (*p)->addralign();
1434       if (addralign > ret)
1435         ret = addralign;
1436     }
1437   return ret;
1438 }
1439
1440 // Set the section addresses for an Output_segment.  ADDR is the
1441 // address and *POFF is the file offset.  Set the section indexes
1442 // starting with *PSHNDX.  Return the address of the immediately
1443 // following segment.  Update *POFF and *PSHNDX.
1444
1445 uint64_t
1446 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
1447                                       unsigned int* pshndx)
1448 {
1449   gold_assert(this->type_ == elfcpp::PT_LOAD);
1450
1451   this->vaddr_ = addr;
1452   this->paddr_ = addr;
1453
1454   off_t orig_off = *poff;
1455   this->offset_ = orig_off;
1456
1457   *poff = align_address(*poff, this->addralign());
1458
1459   addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
1460                                           pshndx);
1461   this->filesz_ = *poff - orig_off;
1462
1463   off_t off = *poff;
1464
1465   uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
1466                                                   poff, pshndx);
1467   this->memsz_ = *poff - orig_off;
1468
1469   // Ignore the file offset adjustments made by the BSS Output_data
1470   // objects.
1471   *poff = off;
1472
1473   return ret;
1474 }
1475
1476 // Set the addresses and file offsets in a list of Output_data
1477 // structures.
1478
1479 uint64_t
1480 Output_segment::set_section_list_addresses(Output_data_list* pdl,
1481                                            uint64_t addr, off_t* poff,
1482                                            unsigned int* pshndx)
1483 {
1484   off_t startoff = *poff;
1485
1486   off_t off = startoff;
1487   for (Output_data_list::iterator p = pdl->begin();
1488        p != pdl->end();
1489        ++p)
1490     {
1491       off = align_address(off, (*p)->addralign());
1492       (*p)->set_address(addr + (off - startoff), off);
1493
1494       // Unless this is a PT_TLS segment, we want to ignore the size
1495       // of a SHF_TLS/SHT_NOBITS section.  Such a section does not
1496       // affect the size of a PT_LOAD segment.
1497       if (this->type_ == elfcpp::PT_TLS
1498           || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
1499           || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
1500         off += (*p)->data_size();
1501
1502       if ((*p)->is_section())
1503         {
1504           (*p)->set_out_shndx(*pshndx);
1505           ++*pshndx;
1506         }
1507     }
1508
1509   *poff = off;
1510   return addr + (off - startoff);
1511 }
1512
1513 // For a non-PT_LOAD segment, set the offset from the sections, if
1514 // any.
1515
1516 void
1517 Output_segment::set_offset()
1518 {
1519   gold_assert(this->type_ != elfcpp::PT_LOAD);
1520
1521   if (this->output_data_.empty() && this->output_bss_.empty())
1522     {
1523       this->vaddr_ = 0;
1524       this->paddr_ = 0;
1525       this->memsz_ = 0;
1526       this->align_ = 0;
1527       this->offset_ = 0;
1528       this->filesz_ = 0;
1529       return;
1530     }
1531
1532   const Output_data* first;
1533   if (this->output_data_.empty())
1534     first = this->output_bss_.front();
1535   else
1536     first = this->output_data_.front();
1537   this->vaddr_ = first->address();
1538   this->paddr_ = this->vaddr_;
1539   this->offset_ = first->offset();
1540
1541   if (this->output_data_.empty())
1542     this->filesz_ = 0;
1543   else
1544     {
1545       const Output_data* last_data = this->output_data_.back();
1546       this->filesz_ = (last_data->address()
1547                        + last_data->data_size()
1548                        - this->vaddr_);
1549     }
1550
1551   const Output_data* last;
1552   if (this->output_bss_.empty())
1553     last = this->output_data_.back();
1554   else
1555     last = this->output_bss_.back();
1556   this->memsz_ = (last->address()
1557                   + last->data_size()
1558                   - this->vaddr_);
1559 }
1560
1561 // Return the number of Output_sections in an Output_segment.
1562
1563 unsigned int
1564 Output_segment::output_section_count() const
1565 {
1566   return (this->output_section_count_list(&this->output_data_)
1567           + this->output_section_count_list(&this->output_bss_));
1568 }
1569
1570 // Return the number of Output_sections in an Output_data_list.
1571
1572 unsigned int
1573 Output_segment::output_section_count_list(const Output_data_list* pdl) const
1574 {
1575   unsigned int count = 0;
1576   for (Output_data_list::const_iterator p = pdl->begin();
1577        p != pdl->end();
1578        ++p)
1579     {
1580       if ((*p)->is_section())
1581         ++count;
1582     }
1583   return count;
1584 }
1585
1586 // Write the segment data into *OPHDR.
1587
1588 template<int size, bool big_endian>
1589 void
1590 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
1591 {
1592   ophdr->put_p_type(this->type_);
1593   ophdr->put_p_offset(this->offset_);
1594   ophdr->put_p_vaddr(this->vaddr_);
1595   ophdr->put_p_paddr(this->paddr_);
1596   ophdr->put_p_filesz(this->filesz_);
1597   ophdr->put_p_memsz(this->memsz_);
1598   ophdr->put_p_flags(this->flags_);
1599   ophdr->put_p_align(this->addralign());
1600 }
1601
1602 // Write the section headers into V.
1603
1604 template<int size, bool big_endian>
1605 unsigned char*
1606 Output_segment::write_section_headers(const Layout* layout,
1607                                       const Stringpool* secnamepool,
1608                                       unsigned char* v,
1609                                       unsigned int *pshndx
1610                                       ACCEPT_SIZE_ENDIAN) const
1611 {
1612   // Every section that is attached to a segment must be attached to a
1613   // PT_LOAD segment, so we only write out section headers for PT_LOAD
1614   // segments.
1615   if (this->type_ != elfcpp::PT_LOAD)
1616     return v;
1617
1618   v = this->write_section_headers_list
1619       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1620           layout, secnamepool, &this->output_data_, v, pshndx
1621           SELECT_SIZE_ENDIAN(size, big_endian));
1622   v = this->write_section_headers_list
1623       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1624           layout, secnamepool, &this->output_bss_, v, pshndx
1625           SELECT_SIZE_ENDIAN(size, big_endian));
1626   return v;
1627 }
1628
1629 template<int size, bool big_endian>
1630 unsigned char*
1631 Output_segment::write_section_headers_list(const Layout* layout,
1632                                            const Stringpool* secnamepool,
1633                                            const Output_data_list* pdl,
1634                                            unsigned char* v,
1635                                            unsigned int* pshndx
1636                                            ACCEPT_SIZE_ENDIAN) const
1637 {
1638   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1639   for (Output_data_list::const_iterator p = pdl->begin();
1640        p != pdl->end();
1641        ++p)
1642     {
1643       if ((*p)->is_section())
1644         {
1645           const Output_section* ps = static_cast<const Output_section*>(*p);
1646           gold_assert(*pshndx == ps->out_shndx());
1647           elfcpp::Shdr_write<size, big_endian> oshdr(v);
1648           ps->write_header(layout, secnamepool, &oshdr);
1649           v += shdr_size;
1650           ++*pshndx;
1651         }
1652     }
1653   return v;
1654 }
1655
1656 // Output_file methods.
1657
1658 Output_file::Output_file(const General_options& options, Target* target)
1659   : options_(options),
1660     target_(target),
1661     name_(options.output_file_name()),
1662     o_(-1),
1663     file_size_(0),
1664     base_(NULL)
1665 {
1666 }
1667
1668 // Open the output file.
1669
1670 void
1671 Output_file::open(off_t file_size)
1672 {
1673   this->file_size_ = file_size;
1674
1675   // Unlink the file first; otherwise the open() may fail if the file
1676   // is busy (e.g. it's an executable that's currently being executed).
1677   //
1678   // However, the linker may be part of a system where a zero-length
1679   // file is created for it to write to, with tight permissions (gcc
1680   // 2.95 did something like this).  Unlinking the file would work
1681   // around those permission controls, so we only unlink if the file
1682   // has a non-zero size.  We also unlink only regular files to avoid
1683   // trouble with directories/etc.
1684   //
1685   // If we fail, continue; this command is merely a best-effort attempt
1686   // to improve the odds for open().
1687
1688   // FIXME: unlink the file if it's a symlink, even a symlink to a dir.
1689   //        Or do we want to follow the symlink and unlink its target? 
1690   struct stat s;
1691   if (::stat(this->name_, &s) == 0 && s.st_size != 0 && S_ISREG(s.st_mode))
1692     ::unlink(this->name_);
1693
1694   int mode = parameters->output_is_object() ? 0666 : 0777;
1695   int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1696   if (o < 0)
1697     gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
1698   this->o_ = o;
1699
1700   // Write out one byte to make the file the right size.
1701   if (::lseek(o, file_size - 1, SEEK_SET) < 0)
1702     gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
1703   char b = 0;
1704   if (::write(o, &b, 1) != 1)
1705     gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
1706
1707   // Map the file into memory.
1708   void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
1709                       MAP_SHARED, o, 0);
1710   if (base == MAP_FAILED)
1711     gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
1712   this->base_ = static_cast<unsigned char*>(base);
1713 }
1714
1715 // Close the output file.
1716
1717 void
1718 Output_file::close()
1719 {
1720   if (::munmap(this->base_, this->file_size_) < 0)
1721     gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
1722   this->base_ = NULL;
1723
1724   if (::close(this->o_) < 0)
1725     gold_error(_("%s: close: %s"), this->name_, strerror(errno));
1726   this->o_ = -1;
1727 }
1728
1729 // Instantiate the templates we need.  We could use the configure
1730 // script to restrict this to only the ones for implemented targets.
1731
1732 #ifdef HAVE_TARGET_32_LITTLE
1733 template
1734 off_t
1735 Output_section::add_input_section<32, false>(
1736     Relobj* object,
1737     unsigned int shndx,
1738     const char* secname,
1739     const elfcpp::Shdr<32, false>& shdr);
1740 #endif
1741
1742 #ifdef HAVE_TARGET_32_BIG
1743 template
1744 off_t
1745 Output_section::add_input_section<32, true>(
1746     Relobj* object,
1747     unsigned int shndx,
1748     const char* secname,
1749     const elfcpp::Shdr<32, true>& shdr);
1750 #endif
1751
1752 #ifdef HAVE_TARGET_64_LITTLE
1753 template
1754 off_t
1755 Output_section::add_input_section<64, false>(
1756     Relobj* object,
1757     unsigned int shndx,
1758     const char* secname,
1759     const elfcpp::Shdr<64, false>& shdr);
1760 #endif
1761
1762 #ifdef HAVE_TARGET_64_BIG
1763 template
1764 off_t
1765 Output_section::add_input_section<64, true>(
1766     Relobj* object,
1767     unsigned int shndx,
1768     const char* secname,
1769     const elfcpp::Shdr<64, true>& shdr);
1770 #endif
1771
1772 #ifdef HAVE_TARGET_32_LITTLE
1773 template
1774 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
1775 #endif
1776
1777 #ifdef HAVE_TARGET_32_BIG
1778 template
1779 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
1780 #endif
1781
1782 #ifdef HAVE_TARGET_64_LITTLE
1783 template
1784 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
1785 #endif
1786
1787 #ifdef HAVE_TARGET_64_BIG
1788 template
1789 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
1790 #endif
1791
1792 #ifdef HAVE_TARGET_32_LITTLE
1793 template
1794 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
1795 #endif
1796
1797 #ifdef HAVE_TARGET_32_BIG
1798 template
1799 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
1800 #endif
1801
1802 #ifdef HAVE_TARGET_64_LITTLE
1803 template
1804 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
1805 #endif
1806
1807 #ifdef HAVE_TARGET_64_BIG
1808 template
1809 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
1810 #endif
1811
1812 #ifdef HAVE_TARGET_32_LITTLE
1813 template
1814 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
1815 #endif
1816
1817 #ifdef HAVE_TARGET_32_BIG
1818 template
1819 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
1820 #endif
1821
1822 #ifdef HAVE_TARGET_64_LITTLE
1823 template
1824 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
1825 #endif
1826
1827 #ifdef HAVE_TARGET_64_BIG
1828 template
1829 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
1830 #endif
1831
1832 #ifdef HAVE_TARGET_32_LITTLE
1833 template
1834 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
1835 #endif
1836
1837 #ifdef HAVE_TARGET_32_BIG
1838 template
1839 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
1840 #endif
1841
1842 #ifdef HAVE_TARGET_64_LITTLE
1843 template
1844 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
1845 #endif
1846
1847 #ifdef HAVE_TARGET_64_BIG
1848 template
1849 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
1850 #endif
1851
1852 #ifdef HAVE_TARGET_32_LITTLE
1853 template
1854 class Output_data_got<32, false>;
1855 #endif
1856
1857 #ifdef HAVE_TARGET_32_BIG
1858 template
1859 class Output_data_got<32, true>;
1860 #endif
1861
1862 #ifdef HAVE_TARGET_64_LITTLE
1863 template
1864 class Output_data_got<64, false>;
1865 #endif
1866
1867 #ifdef HAVE_TARGET_64_BIG
1868 template
1869 class Output_data_got<64, true>;
1870 #endif
1871
1872 } // End namespace gold.