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