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