OSDN Git Service

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