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