Generate a complete exception frame header. Discard duplicate
[platform/upstream/binutils.git] / gold / output.cc
1 // output.cc -- manage the output file for gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstdlib>
26 #include <cerrno>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <algorithm>
32 #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.
688         if (gsym->final_value_is_known())
689           {
690             Sized_symbol<size>* sgsym;
691             // This cast is a bit ugly.  We don't want to put a
692             // virtual method in Symbol, because we want Symbol to be
693             // as small as possible.
694             sgsym = static_cast<Sized_symbol<size>*>(gsym);
695             val = sgsym->value();
696           }
697       }
698       break;
699
700     case CONSTANT_CODE:
701       val = this->u_.constant;
702       break;
703
704     default:
705       val = this->u_.object->local_symbol_value(this->local_sym_index_);
706       break;
707     }
708
709   elfcpp::Swap<size, big_endian>::writeval(pov, val);
710 }
711
712 // Output_data_got methods.
713
714 // Add an entry for a global symbol to the GOT.  This returns true if
715 // this is a new GOT entry, false if the symbol already had a GOT
716 // entry.
717
718 template<int size, bool big_endian>
719 bool
720 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
721 {
722   if (gsym->has_got_offset())
723     return false;
724
725   this->entries_.push_back(Got_entry(gsym));
726   this->set_got_size();
727   gsym->set_got_offset(this->last_got_offset());
728   return true;
729 }
730
731 // Add an entry for a local symbol to the GOT.  This returns true if
732 // this is a new GOT entry, false if the symbol already has a GOT
733 // entry.
734
735 template<int size, bool big_endian>
736 bool
737 Output_data_got<size, big_endian>::add_local(
738     Sized_relobj<size, big_endian>* object,
739     unsigned int symndx)
740 {
741   if (object->local_has_got_offset(symndx))
742     return false;
743   this->entries_.push_back(Got_entry(object, symndx));
744   this->set_got_size();
745   object->set_local_got_offset(symndx, this->last_got_offset());
746   return true;
747 }
748
749 // Write out the GOT.
750
751 template<int size, bool big_endian>
752 void
753 Output_data_got<size, big_endian>::do_write(Output_file* of)
754 {
755   const int add = size / 8;
756
757   const off_t off = this->offset();
758   const off_t oview_size = this->data_size();
759   unsigned char* const oview = of->get_output_view(off, oview_size);
760
761   unsigned char* pov = oview;
762   for (typename Got_entries::const_iterator p = this->entries_.begin();
763        p != this->entries_.end();
764        ++p)
765     {
766       p->write(pov);
767       pov += add;
768     }
769
770   gold_assert(pov - oview == oview_size);
771
772   of->write_output_view(off, oview_size, oview);
773
774   // We no longer need the GOT entries.
775   this->entries_.clear();
776 }
777
778 // Output_data_dynamic::Dynamic_entry methods.
779
780 // Write out the entry.
781
782 template<int size, bool big_endian>
783 void
784 Output_data_dynamic::Dynamic_entry::write(
785     unsigned char* pov,
786     const Stringpool* pool
787     ACCEPT_SIZE_ENDIAN) const
788 {
789   typename elfcpp::Elf_types<size>::Elf_WXword val;
790   switch (this->classification_)
791     {
792     case DYNAMIC_NUMBER:
793       val = this->u_.val;
794       break;
795
796     case DYNAMIC_SECTION_ADDRESS:
797       val = this->u_.od->address();
798       break;
799
800     case DYNAMIC_SECTION_SIZE:
801       val = this->u_.od->data_size();
802       break;
803
804     case DYNAMIC_SYMBOL:
805       {
806         const Sized_symbol<size>* s =
807           static_cast<const Sized_symbol<size>*>(this->u_.sym);
808         val = s->value();
809       }
810       break;
811
812     case DYNAMIC_STRING:
813       val = pool->get_offset(this->u_.str);
814       break;
815
816     default:
817       gold_unreachable();
818     }
819
820   elfcpp::Dyn_write<size, big_endian> dw(pov);
821   dw.put_d_tag(this->tag_);
822   dw.put_d_val(val);
823 }
824
825 // Output_data_dynamic methods.
826
827 // Adjust the output section to set the entry size.
828
829 void
830 Output_data_dynamic::do_adjust_output_section(Output_section* os)
831 {
832   if (parameters->get_size() == 32)
833     os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
834   else if (parameters->get_size() == 64)
835     os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
836   else
837     gold_unreachable();
838 }
839
840 // Set the final data size.
841
842 void
843 Output_data_dynamic::do_set_address(uint64_t, off_t)
844 {
845   // Add the terminating entry.
846   this->add_constant(elfcpp::DT_NULL, 0);
847
848   int dyn_size;
849   if (parameters->get_size() == 32)
850     dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
851   else if (parameters->get_size() == 64)
852     dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
853   else
854     gold_unreachable();
855   this->set_data_size(this->entries_.size() * dyn_size);
856 }
857
858 // Write out the dynamic entries.
859
860 void
861 Output_data_dynamic::do_write(Output_file* of)
862 {
863   if (parameters->get_size() == 32)
864     {
865       if (parameters->is_big_endian())
866         {
867 #ifdef HAVE_TARGET_32_BIG
868           this->sized_write<32, true>(of);
869 #else
870           gold_unreachable();
871 #endif
872         }
873       else
874         {
875 #ifdef HAVE_TARGET_32_LITTLE
876           this->sized_write<32, false>(of);
877 #else
878           gold_unreachable();
879 #endif
880         }
881     }
882   else if (parameters->get_size() == 64)
883     {
884       if (parameters->is_big_endian())
885         {
886 #ifdef HAVE_TARGET_64_BIG
887           this->sized_write<64, true>(of);
888 #else
889           gold_unreachable();
890 #endif
891         }
892       else
893         {
894 #ifdef HAVE_TARGET_64_LITTLE
895           this->sized_write<64, false>(of);
896 #else
897           gold_unreachable();
898 #endif
899         }
900     }
901   else
902     gold_unreachable();
903 }
904
905 template<int size, bool big_endian>
906 void
907 Output_data_dynamic::sized_write(Output_file* of)
908 {
909   const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
910
911   const off_t offset = this->offset();
912   const off_t oview_size = this->data_size();
913   unsigned char* const oview = of->get_output_view(offset, oview_size);
914
915   unsigned char* pov = oview;
916   for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
917        p != this->entries_.end();
918        ++p)
919     {
920       p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
921           pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
922       pov += dyn_size;
923     }
924
925   gold_assert(pov - oview == oview_size);
926
927   of->write_output_view(offset, oview_size, oview);
928
929   // We no longer need the dynamic entries.
930   this->entries_.clear();
931 }
932
933 // Output_section::Input_section methods.
934
935 // Return the data size.  For an input section we store the size here.
936 // For an Output_section_data, we have to ask it for the size.
937
938 off_t
939 Output_section::Input_section::data_size() const
940 {
941   if (this->is_input_section())
942     return this->u1_.data_size;
943   else
944     return this->u2_.posd->data_size();
945 }
946
947 // Set the address and file offset.
948
949 void
950 Output_section::Input_section::set_address(uint64_t addr, off_t off,
951                                            off_t secoff)
952 {
953   if (this->is_input_section())
954     this->u2_.object->set_section_offset(this->shndx_, off - secoff);
955   else
956     this->u2_.posd->set_address(addr, off);
957 }
958
959 // Try to turn an input offset into an output offset.
960
961 bool
962 Output_section::Input_section::output_offset(const Relobj* object,
963                                              unsigned int shndx,
964                                              off_t offset,
965                                              off_t *poutput) const
966 {
967   if (!this->is_input_section())
968     return this->u2_.posd->output_offset(object, shndx, offset, poutput);
969   else
970     {
971       if (this->shndx_ != shndx || this->u2_.object != object)
972         return false;
973       off_t output_offset;
974       Output_section* os = object->output_section(shndx, &output_offset);
975       gold_assert(os != NULL);
976       gold_assert(output_offset != -1);
977       *poutput = output_offset + offset;
978       return true;
979     }
980 }
981
982 // Write out the data.  We don't have to do anything for an input
983 // section--they are handled via Object::relocate--but this is where
984 // we write out the data for an Output_section_data.
985
986 void
987 Output_section::Input_section::write(Output_file* of)
988 {
989   if (!this->is_input_section())
990     this->u2_.posd->write(of);
991 }
992
993 // Output_section methods.
994
995 // Construct an Output_section.  NAME will point into a Stringpool.
996
997 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
998                                elfcpp::Elf_Xword flags)
999   : name_(name),
1000     addralign_(0),
1001     entsize_(0),
1002     link_section_(NULL),
1003     link_(0),
1004     info_section_(NULL),
1005     info_(0),
1006     type_(type),
1007     flags_(flags),
1008     out_shndx_(-1U),
1009     symtab_index_(0),
1010     dynsym_index_(0),
1011     input_sections_(),
1012     first_input_offset_(0),
1013     fills_(),
1014     needs_symtab_index_(false),
1015     needs_dynsym_index_(false),
1016     should_link_to_symtab_(false),
1017     should_link_to_dynsym_(false),
1018     after_input_sections_(false)
1019 {
1020 }
1021
1022 Output_section::~Output_section()
1023 {
1024 }
1025
1026 // Set the entry size.
1027
1028 void
1029 Output_section::set_entsize(uint64_t v)
1030 {
1031   if (this->entsize_ == 0)
1032     this->entsize_ = v;
1033   else
1034     gold_assert(this->entsize_ == v);
1035 }
1036
1037 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1038 // OBJECT, to the Output_section.  RELOC_SHNDX is the index of a
1039 // relocation section which applies to this section, or 0 if none, or
1040 // -1U if more than one.  Return the offset of the input section
1041 // within the output section.  Return -1 if the input section will
1042 // receive special handling.  In the normal case we don't always keep
1043 // track of input sections for an Output_section.  Instead, each
1044 // Object keeps track of the Output_section for each of its input
1045 // sections.
1046
1047 template<int size, bool big_endian>
1048 off_t
1049 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1050                                   unsigned int shndx,
1051                                   const char* secname,
1052                                   const elfcpp::Shdr<size, big_endian>& shdr,
1053                                   unsigned int reloc_shndx)
1054 {
1055   elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1056   if ((addralign & (addralign - 1)) != 0)
1057     {
1058       object->error(_("invalid alignment %lu for section \"%s\""),
1059                     static_cast<unsigned long>(addralign), secname);
1060       addralign = 1;
1061     }
1062
1063   if (addralign > this->addralign_)
1064     this->addralign_ = addralign;
1065
1066   // If this is a SHF_MERGE section, we pass all the input sections to
1067   // a Output_data_merge.  We don't try to handle relocations for such
1068   // a section.
1069   if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0
1070       && reloc_shndx == 0)
1071     {
1072       if (this->add_merge_input_section(object, shndx, shdr.get_sh_flags(),
1073                                         shdr.get_sh_entsize(),
1074                                         addralign))
1075         {
1076           // Tell the relocation routines that they need to call the
1077           // output_offset method to determine the final address.
1078           return -1;
1079         }
1080     }
1081
1082   off_t offset_in_section = this->data_size();
1083   off_t aligned_offset_in_section = align_address(offset_in_section,
1084                                                   addralign);
1085
1086   if (aligned_offset_in_section > offset_in_section
1087       && (shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0
1088       && object->target()->has_code_fill())
1089     {
1090       // We need to add some fill data.  Using fill_list_ when
1091       // possible is an optimization, since we will often have fill
1092       // sections without input sections.
1093       off_t fill_len = aligned_offset_in_section - offset_in_section;
1094       if (this->input_sections_.empty())
1095         this->fills_.push_back(Fill(offset_in_section, fill_len));
1096       else
1097         {
1098           // FIXME: When relaxing, the size needs to adjust to
1099           // maintain a constant alignment.
1100           std::string fill_data(object->target()->code_fill(fill_len));
1101           Output_data_const* odc = new Output_data_const(fill_data, 1);
1102           this->input_sections_.push_back(Input_section(odc));
1103         }
1104     }
1105
1106   this->set_data_size(aligned_offset_in_section + shdr.get_sh_size());
1107
1108   // We need to keep track of this section if we are already keeping
1109   // track of sections, or if we are relaxing.  FIXME: Add test for
1110   // relaxing.
1111   if (!this->input_sections_.empty())
1112     this->input_sections_.push_back(Input_section(object, shndx,
1113                                                   shdr.get_sh_size(),
1114                                                   addralign));
1115
1116   return aligned_offset_in_section;
1117 }
1118
1119 // Add arbitrary data to an output section.
1120
1121 void
1122 Output_section::add_output_section_data(Output_section_data* posd)
1123 {
1124   Input_section inp(posd);
1125   this->add_output_section_data(&inp);
1126 }
1127
1128 // Add arbitrary data to an output section by Input_section.
1129
1130 void
1131 Output_section::add_output_section_data(Input_section* inp)
1132 {
1133   if (this->input_sections_.empty())
1134     this->first_input_offset_ = this->data_size();
1135
1136   this->input_sections_.push_back(*inp);
1137
1138   uint64_t addralign = inp->addralign();
1139   if (addralign > this->addralign_)
1140     this->addralign_ = addralign;
1141
1142   inp->set_output_section(this);
1143 }
1144
1145 // Add a merge section to an output section.
1146
1147 void
1148 Output_section::add_output_merge_section(Output_section_data* posd,
1149                                          bool is_string, uint64_t entsize)
1150 {
1151   Input_section inp(posd, is_string, entsize);
1152   this->add_output_section_data(&inp);
1153 }
1154
1155 // Add an input section to a SHF_MERGE section.
1156
1157 bool
1158 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1159                                         uint64_t flags, uint64_t entsize,
1160                                         uint64_t addralign)
1161 {
1162   bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1163
1164   // We only merge strings if the alignment is not more than the
1165   // character size.  This could be handled, but it's unusual.
1166   if (is_string && addralign > entsize)
1167     return false;
1168
1169   Input_section_list::iterator p;
1170   for (p = this->input_sections_.begin();
1171        p != this->input_sections_.end();
1172        ++p)
1173     if (p->is_merge_section(is_string, entsize, addralign))
1174       break;
1175
1176   // We handle the actual constant merging in Output_merge_data or
1177   // Output_merge_string_data.
1178   if (p != this->input_sections_.end())
1179     p->add_input_section(object, shndx);
1180   else
1181     {
1182       Output_section_data* posd;
1183       if (!is_string)
1184         posd = new Output_merge_data(entsize, addralign);
1185       else if (entsize == 1)
1186         posd = new Output_merge_string<char>(addralign);
1187       else if (entsize == 2)
1188         posd = new Output_merge_string<uint16_t>(addralign);
1189       else if (entsize == 4)
1190         posd = new Output_merge_string<uint32_t>(addralign);
1191       else
1192         return false;
1193
1194       this->add_output_merge_section(posd, is_string, entsize);
1195       posd->add_input_section(object, shndx);
1196     }
1197
1198   return true;
1199 }
1200
1201 // Given an address OFFSET relative to the start of input section
1202 // SHNDX in OBJECT, return whether this address is being included in
1203 // the final link.  This should only be called if SHNDX in OBJECT has
1204 // a special mapping.
1205
1206 bool
1207 Output_section::is_input_address_mapped(const Relobj* object,
1208                                         unsigned int shndx,
1209                                         off_t offset) const
1210 {
1211   gold_assert(object->is_section_specially_mapped(shndx));
1212
1213   for (Input_section_list::const_iterator p = this->input_sections_.begin();
1214        p != this->input_sections_.end();
1215        ++p)
1216     {
1217       off_t output_offset;
1218       if (p->output_offset(object, shndx, offset, &output_offset))
1219         return output_offset != -1;
1220     }
1221
1222   // By default we assume that the address is mapped.  This should
1223   // only be called after we have passed all sections to Layout.  At
1224   // that point we should know what we are discarding.
1225   return true;
1226 }
1227
1228 // Given an address OFFSET relative to the start of input section
1229 // SHNDX in object OBJECT, return the output offset relative to the
1230 // start of the section.  This should only be called if SHNDX in
1231 // OBJECT has a special mapping.
1232
1233 off_t
1234 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1235                               off_t offset) const
1236 {
1237   gold_assert(object->is_section_specially_mapped(shndx));
1238   // This can only be called meaningfully when layout is complete.
1239   gold_assert(Output_data::is_layout_complete());
1240
1241   for (Input_section_list::const_iterator p = this->input_sections_.begin();
1242        p != this->input_sections_.end();
1243        ++p)
1244     {
1245       off_t output_offset;
1246       if (p->output_offset(object, shndx, offset, &output_offset))
1247         return output_offset;
1248     }
1249   gold_unreachable();
1250 }
1251
1252 // Return the output virtual address of OFFSET relative to the start
1253 // of input section SHNDX in object OBJECT.
1254
1255 uint64_t
1256 Output_section::output_address(const Relobj* object, unsigned int shndx,
1257                                off_t offset) const
1258 {
1259   gold_assert(object->is_section_specially_mapped(shndx));
1260   // This can only be called meaningfully when layout is complete.
1261   gold_assert(Output_data::is_layout_complete());
1262
1263   uint64_t addr = this->address() + this->first_input_offset_;
1264   for (Input_section_list::const_iterator p = this->input_sections_.begin();
1265        p != this->input_sections_.end();
1266        ++p)
1267     {
1268       addr = align_address(addr, p->addralign());
1269       off_t output_offset;
1270       if (p->output_offset(object, shndx, offset, &output_offset))
1271         {
1272           if (output_offset == -1)
1273             return -1U;
1274           return addr + output_offset;
1275         }
1276       addr += p->data_size();
1277     }
1278
1279   // If we get here, it means that we don't know the mapping for this
1280   // input section.  This might happen in principle if
1281   // add_input_section were called before add_output_section_data.
1282   // But it should never actually happen.
1283
1284   gold_unreachable();
1285 }
1286
1287 // Set the address of an Output_section.  This is where we handle
1288 // setting the addresses of any Output_section_data objects.
1289
1290 void
1291 Output_section::do_set_address(uint64_t address, off_t startoff)
1292 {
1293   if (this->input_sections_.empty())
1294     return;
1295
1296   off_t off = startoff + this->first_input_offset_;
1297   for (Input_section_list::iterator p = this->input_sections_.begin();
1298        p != this->input_sections_.end();
1299        ++p)
1300     {
1301       off = align_address(off, p->addralign());
1302       p->set_address(address + (off - startoff), off, startoff);
1303       off += p->data_size();
1304     }
1305
1306   this->set_data_size(off - startoff);
1307 }
1308
1309 // Write the section header to *OSHDR.
1310
1311 template<int size, bool big_endian>
1312 void
1313 Output_section::write_header(const Layout* layout,
1314                              const Stringpool* secnamepool,
1315                              elfcpp::Shdr_write<size, big_endian>* oshdr) const
1316 {
1317   oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1318   oshdr->put_sh_type(this->type_);
1319   oshdr->put_sh_flags(this->flags_);
1320   oshdr->put_sh_addr(this->address());
1321   oshdr->put_sh_offset(this->offset());
1322   oshdr->put_sh_size(this->data_size());
1323   if (this->link_section_ != NULL)
1324     oshdr->put_sh_link(this->link_section_->out_shndx());
1325   else if (this->should_link_to_symtab_)
1326     oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1327   else if (this->should_link_to_dynsym_)
1328     oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1329   else
1330     oshdr->put_sh_link(this->link_);
1331   if (this->info_section_ != NULL)
1332     oshdr->put_sh_info(this->info_section_->out_shndx());
1333   else
1334     oshdr->put_sh_info(this->info_);
1335   oshdr->put_sh_addralign(this->addralign_);
1336   oshdr->put_sh_entsize(this->entsize_);
1337 }
1338
1339 // Write out the data.  For input sections the data is written out by
1340 // Object::relocate, but we have to handle Output_section_data objects
1341 // here.
1342
1343 void
1344 Output_section::do_write(Output_file* of)
1345 {
1346   off_t output_section_file_offset = this->offset();
1347   for (Fill_list::iterator p = this->fills_.begin();
1348        p != this->fills_.end();
1349        ++p)
1350     {
1351       std::string fill_data(of->target()->code_fill(p->length()));
1352       of->write(output_section_file_offset + p->section_offset(),
1353                 fill_data.data(), fill_data.size());
1354     }
1355
1356   for (Input_section_list::iterator p = this->input_sections_.begin();
1357        p != this->input_sections_.end();
1358        ++p)
1359     p->write(of);
1360 }
1361
1362 // Output segment methods.
1363
1364 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1365   : output_data_(),
1366     output_bss_(),
1367     vaddr_(0),
1368     paddr_(0),
1369     memsz_(0),
1370     align_(0),
1371     offset_(0),
1372     filesz_(0),
1373     type_(type),
1374     flags_(flags),
1375     is_align_known_(false)
1376 {
1377 }
1378
1379 // Add an Output_section to an Output_segment.
1380
1381 void
1382 Output_segment::add_output_section(Output_section* os,
1383                                    elfcpp::Elf_Word seg_flags,
1384                                    bool front)
1385 {
1386   gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1387   gold_assert(!this->is_align_known_);
1388
1389   // Update the segment flags.
1390   this->flags_ |= seg_flags;
1391
1392   Output_segment::Output_data_list* pdl;
1393   if (os->type() == elfcpp::SHT_NOBITS)
1394     pdl = &this->output_bss_;
1395   else
1396     pdl = &this->output_data_;
1397
1398   // So that PT_NOTE segments will work correctly, we need to ensure
1399   // that all SHT_NOTE sections are adjacent.  This will normally
1400   // happen automatically, because all the SHT_NOTE input sections
1401   // will wind up in the same output section.  However, it is possible
1402   // for multiple SHT_NOTE input sections to have different section
1403   // flags, and thus be in different output sections, but for the
1404   // different section flags to map into the same segment flags and
1405   // thus the same output segment.
1406
1407   // Note that while there may be many input sections in an output
1408   // section, there are normally only a few output sections in an
1409   // output segment.  This loop is expected to be fast.
1410
1411   if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1412     {
1413       Output_segment::Output_data_list::iterator p = pdl->end();
1414       do
1415         {
1416           --p;
1417           if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1418             {
1419               // We don't worry about the FRONT parameter.
1420               ++p;
1421               pdl->insert(p, os);
1422               return;
1423             }
1424         }
1425       while (p != pdl->begin());
1426     }
1427
1428   // Similarly, so that PT_TLS segments will work, we need to group
1429   // SHF_TLS sections.  An SHF_TLS/SHT_NOBITS section is a special
1430   // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1431   // SHF_TLS/SHT_PROGBITS sections.  This lets us set up PT_TLS
1432   // correctly.
1433   if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
1434     {
1435       pdl = &this->output_data_;
1436       bool nobits = os->type() == elfcpp::SHT_NOBITS;
1437       bool sawtls = false;
1438       Output_segment::Output_data_list::iterator p = pdl->end();
1439       do
1440         {
1441           --p;
1442           bool insert;
1443           if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1444             {
1445               sawtls = true;
1446               // Put a NOBITS section after the first TLS section.
1447               // But a PROGBITS section after the first TLS/PROGBITS
1448               // section.
1449               insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1450             }
1451           else
1452             {
1453               // If we've gone past the TLS sections, but we've seen a
1454               // TLS section, then we need to insert this section now.
1455               insert = sawtls;
1456             }
1457
1458           if (insert)
1459             {
1460               // We don't worry about the FRONT parameter.
1461               ++p;
1462               pdl->insert(p, os);
1463               return;
1464             }
1465         }
1466       while (p != pdl->begin());
1467
1468       // There are no TLS sections yet; put this one at the requested
1469       // location in the section list.
1470     }
1471
1472   if (front)
1473     pdl->push_front(os);
1474   else
1475     pdl->push_back(os);
1476 }
1477
1478 // Add an Output_data (which is not an Output_section) to the start of
1479 // a segment.
1480
1481 void
1482 Output_segment::add_initial_output_data(Output_data* od)
1483 {
1484   gold_assert(!this->is_align_known_);
1485   this->output_data_.push_front(od);
1486 }
1487
1488 // Return the maximum alignment of the Output_data in Output_segment.
1489 // Once we compute this, we prohibit new sections from being added.
1490
1491 uint64_t
1492 Output_segment::addralign()
1493 {
1494   if (!this->is_align_known_)
1495     {
1496       uint64_t addralign;
1497
1498       addralign = Output_segment::maximum_alignment(&this->output_data_);
1499       if (addralign > this->align_)
1500         this->align_ = addralign;
1501
1502       addralign = Output_segment::maximum_alignment(&this->output_bss_);
1503       if (addralign > this->align_)
1504         this->align_ = addralign;
1505
1506       this->is_align_known_ = true;
1507     }
1508
1509   return this->align_;
1510 }
1511
1512 // Return the maximum alignment of a list of Output_data.
1513
1514 uint64_t
1515 Output_segment::maximum_alignment(const Output_data_list* pdl)
1516 {
1517   uint64_t ret = 0;
1518   for (Output_data_list::const_iterator p = pdl->begin();
1519        p != pdl->end();
1520        ++p)
1521     {
1522       uint64_t addralign = (*p)->addralign();
1523       if (addralign > ret)
1524         ret = addralign;
1525     }
1526   return ret;
1527 }
1528
1529 // Set the section addresses for an Output_segment.  ADDR is the
1530 // address and *POFF is the file offset.  Set the section indexes
1531 // starting with *PSHNDX.  Return the address of the immediately
1532 // following segment.  Update *POFF and *PSHNDX.
1533
1534 uint64_t
1535 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
1536                                       unsigned int* pshndx)
1537 {
1538   gold_assert(this->type_ == elfcpp::PT_LOAD);
1539
1540   this->vaddr_ = addr;
1541   this->paddr_ = addr;
1542
1543   off_t orig_off = *poff;
1544   this->offset_ = orig_off;
1545
1546   *poff = align_address(*poff, this->addralign());
1547
1548   addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
1549                                           pshndx);
1550   this->filesz_ = *poff - orig_off;
1551
1552   off_t off = *poff;
1553
1554   uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
1555                                                   poff, pshndx);
1556   this->memsz_ = *poff - orig_off;
1557
1558   // Ignore the file offset adjustments made by the BSS Output_data
1559   // objects.
1560   *poff = off;
1561
1562   return ret;
1563 }
1564
1565 // Set the addresses and file offsets in a list of Output_data
1566 // structures.
1567
1568 uint64_t
1569 Output_segment::set_section_list_addresses(Output_data_list* pdl,
1570                                            uint64_t addr, off_t* poff,
1571                                            unsigned int* pshndx)
1572 {
1573   off_t startoff = *poff;
1574
1575   off_t off = startoff;
1576   for (Output_data_list::iterator p = pdl->begin();
1577        p != pdl->end();
1578        ++p)
1579     {
1580       off = align_address(off, (*p)->addralign());
1581       (*p)->set_address(addr + (off - startoff), off);
1582
1583       // Unless this is a PT_TLS segment, we want to ignore the size
1584       // of a SHF_TLS/SHT_NOBITS section.  Such a section does not
1585       // affect the size of a PT_LOAD segment.
1586       if (this->type_ == elfcpp::PT_TLS
1587           || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
1588           || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
1589         off += (*p)->data_size();
1590
1591       if ((*p)->is_section())
1592         {
1593           (*p)->set_out_shndx(*pshndx);
1594           ++*pshndx;
1595         }
1596     }
1597
1598   *poff = off;
1599   return addr + (off - startoff);
1600 }
1601
1602 // For a non-PT_LOAD segment, set the offset from the sections, if
1603 // any.
1604
1605 void
1606 Output_segment::set_offset()
1607 {
1608   gold_assert(this->type_ != elfcpp::PT_LOAD);
1609
1610   if (this->output_data_.empty() && this->output_bss_.empty())
1611     {
1612       this->vaddr_ = 0;
1613       this->paddr_ = 0;
1614       this->memsz_ = 0;
1615       this->align_ = 0;
1616       this->offset_ = 0;
1617       this->filesz_ = 0;
1618       return;
1619     }
1620
1621   const Output_data* first;
1622   if (this->output_data_.empty())
1623     first = this->output_bss_.front();
1624   else
1625     first = this->output_data_.front();
1626   this->vaddr_ = first->address();
1627   this->paddr_ = this->vaddr_;
1628   this->offset_ = first->offset();
1629
1630   if (this->output_data_.empty())
1631     this->filesz_ = 0;
1632   else
1633     {
1634       const Output_data* last_data = this->output_data_.back();
1635       this->filesz_ = (last_data->address()
1636                        + last_data->data_size()
1637                        - this->vaddr_);
1638     }
1639
1640   const Output_data* last;
1641   if (this->output_bss_.empty())
1642     last = this->output_data_.back();
1643   else
1644     last = this->output_bss_.back();
1645   this->memsz_ = (last->address()
1646                   + last->data_size()
1647                   - this->vaddr_);
1648 }
1649
1650 // Return the number of Output_sections in an Output_segment.
1651
1652 unsigned int
1653 Output_segment::output_section_count() const
1654 {
1655   return (this->output_section_count_list(&this->output_data_)
1656           + this->output_section_count_list(&this->output_bss_));
1657 }
1658
1659 // Return the number of Output_sections in an Output_data_list.
1660
1661 unsigned int
1662 Output_segment::output_section_count_list(const Output_data_list* pdl) const
1663 {
1664   unsigned int count = 0;
1665   for (Output_data_list::const_iterator p = pdl->begin();
1666        p != pdl->end();
1667        ++p)
1668     {
1669       if ((*p)->is_section())
1670         ++count;
1671     }
1672   return count;
1673 }
1674
1675 // Write the segment data into *OPHDR.
1676
1677 template<int size, bool big_endian>
1678 void
1679 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
1680 {
1681   ophdr->put_p_type(this->type_);
1682   ophdr->put_p_offset(this->offset_);
1683   ophdr->put_p_vaddr(this->vaddr_);
1684   ophdr->put_p_paddr(this->paddr_);
1685   ophdr->put_p_filesz(this->filesz_);
1686   ophdr->put_p_memsz(this->memsz_);
1687   ophdr->put_p_flags(this->flags_);
1688   ophdr->put_p_align(this->addralign());
1689 }
1690
1691 // Write the section headers into V.
1692
1693 template<int size, bool big_endian>
1694 unsigned char*
1695 Output_segment::write_section_headers(const Layout* layout,
1696                                       const Stringpool* secnamepool,
1697                                       unsigned char* v,
1698                                       unsigned int *pshndx
1699                                       ACCEPT_SIZE_ENDIAN) const
1700 {
1701   // Every section that is attached to a segment must be attached to a
1702   // PT_LOAD segment, so we only write out section headers for PT_LOAD
1703   // segments.
1704   if (this->type_ != elfcpp::PT_LOAD)
1705     return v;
1706
1707   v = this->write_section_headers_list
1708       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1709           layout, secnamepool, &this->output_data_, v, pshndx
1710           SELECT_SIZE_ENDIAN(size, big_endian));
1711   v = this->write_section_headers_list
1712       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1713           layout, secnamepool, &this->output_bss_, v, pshndx
1714           SELECT_SIZE_ENDIAN(size, big_endian));
1715   return v;
1716 }
1717
1718 template<int size, bool big_endian>
1719 unsigned char*
1720 Output_segment::write_section_headers_list(const Layout* layout,
1721                                            const Stringpool* secnamepool,
1722                                            const Output_data_list* pdl,
1723                                            unsigned char* v,
1724                                            unsigned int* pshndx
1725                                            ACCEPT_SIZE_ENDIAN) const
1726 {
1727   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1728   for (Output_data_list::const_iterator p = pdl->begin();
1729        p != pdl->end();
1730        ++p)
1731     {
1732       if ((*p)->is_section())
1733         {
1734           const Output_section* ps = static_cast<const Output_section*>(*p);
1735           gold_assert(*pshndx == ps->out_shndx());
1736           elfcpp::Shdr_write<size, big_endian> oshdr(v);
1737           ps->write_header(layout, secnamepool, &oshdr);
1738           v += shdr_size;
1739           ++*pshndx;
1740         }
1741     }
1742   return v;
1743 }
1744
1745 // Output_file methods.
1746
1747 Output_file::Output_file(const General_options& options, Target* target)
1748   : options_(options),
1749     target_(target),
1750     name_(options.output_file_name()),
1751     o_(-1),
1752     file_size_(0),
1753     base_(NULL)
1754 {
1755 }
1756
1757 // Open the output file.
1758
1759 void
1760 Output_file::open(off_t file_size)
1761 {
1762   this->file_size_ = file_size;
1763
1764   // Unlink the file first; otherwise the open() may fail if the file
1765   // is busy (e.g. it's an executable that's currently being executed).
1766   //
1767   // However, the linker may be part of a system where a zero-length
1768   // file is created for it to write to, with tight permissions (gcc
1769   // 2.95 did something like this).  Unlinking the file would work
1770   // around those permission controls, so we only unlink if the file
1771   // has a non-zero size.  We also unlink only regular files to avoid
1772   // trouble with directories/etc.
1773   //
1774   // If we fail, continue; this command is merely a best-effort attempt
1775   // to improve the odds for open().
1776
1777   struct stat s;
1778   if (::stat(this->name_, &s) == 0 && s.st_size != 0)
1779     unlink_if_ordinary(this->name_);
1780
1781   int mode = parameters->output_is_object() ? 0666 : 0777;
1782   int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1783   if (o < 0)
1784     gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
1785   this->o_ = o;
1786
1787   // Write out one byte to make the file the right size.
1788   if (::lseek(o, file_size - 1, SEEK_SET) < 0)
1789     gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
1790   char b = 0;
1791   if (::write(o, &b, 1) != 1)
1792     gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
1793
1794   // Map the file into memory.
1795   void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
1796                       MAP_SHARED, o, 0);
1797   if (base == MAP_FAILED)
1798     gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
1799   this->base_ = static_cast<unsigned char*>(base);
1800 }
1801
1802 // Close the output file.
1803
1804 void
1805 Output_file::close()
1806 {
1807   if (::munmap(this->base_, this->file_size_) < 0)
1808     gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
1809   this->base_ = NULL;
1810
1811   if (::close(this->o_) < 0)
1812     gold_error(_("%s: close: %s"), this->name_, strerror(errno));
1813   this->o_ = -1;
1814 }
1815
1816 // Instantiate the templates we need.  We could use the configure
1817 // script to restrict this to only the ones for implemented targets.
1818
1819 #ifdef HAVE_TARGET_32_LITTLE
1820 template
1821 off_t
1822 Output_section::add_input_section<32, false>(
1823     Sized_relobj<32, false>* object,
1824     unsigned int shndx,
1825     const char* secname,
1826     const elfcpp::Shdr<32, false>& shdr,
1827     unsigned int reloc_shndx);
1828 #endif
1829
1830 #ifdef HAVE_TARGET_32_BIG
1831 template
1832 off_t
1833 Output_section::add_input_section<32, true>(
1834     Sized_relobj<32, true>* object,
1835     unsigned int shndx,
1836     const char* secname,
1837     const elfcpp::Shdr<32, true>& shdr,
1838     unsigned int reloc_shndx);
1839 #endif
1840
1841 #ifdef HAVE_TARGET_64_LITTLE
1842 template
1843 off_t
1844 Output_section::add_input_section<64, false>(
1845     Sized_relobj<64, false>* object,
1846     unsigned int shndx,
1847     const char* secname,
1848     const elfcpp::Shdr<64, false>& shdr,
1849     unsigned int reloc_shndx);
1850 #endif
1851
1852 #ifdef HAVE_TARGET_64_BIG
1853 template
1854 off_t
1855 Output_section::add_input_section<64, true>(
1856     Sized_relobj<64, true>* object,
1857     unsigned int shndx,
1858     const char* secname,
1859     const elfcpp::Shdr<64, true>& shdr,
1860     unsigned int reloc_shndx);
1861 #endif
1862
1863 #ifdef HAVE_TARGET_32_LITTLE
1864 template
1865 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
1866 #endif
1867
1868 #ifdef HAVE_TARGET_32_BIG
1869 template
1870 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
1871 #endif
1872
1873 #ifdef HAVE_TARGET_64_LITTLE
1874 template
1875 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
1876 #endif
1877
1878 #ifdef HAVE_TARGET_64_BIG
1879 template
1880 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
1881 #endif
1882
1883 #ifdef HAVE_TARGET_32_LITTLE
1884 template
1885 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
1886 #endif
1887
1888 #ifdef HAVE_TARGET_32_BIG
1889 template
1890 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
1891 #endif
1892
1893 #ifdef HAVE_TARGET_64_LITTLE
1894 template
1895 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
1896 #endif
1897
1898 #ifdef HAVE_TARGET_64_BIG
1899 template
1900 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
1901 #endif
1902
1903 #ifdef HAVE_TARGET_32_LITTLE
1904 template
1905 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
1906 #endif
1907
1908 #ifdef HAVE_TARGET_32_BIG
1909 template
1910 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
1911 #endif
1912
1913 #ifdef HAVE_TARGET_64_LITTLE
1914 template
1915 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
1916 #endif
1917
1918 #ifdef HAVE_TARGET_64_BIG
1919 template
1920 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
1921 #endif
1922
1923 #ifdef HAVE_TARGET_32_LITTLE
1924 template
1925 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
1926 #endif
1927
1928 #ifdef HAVE_TARGET_32_BIG
1929 template
1930 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
1931 #endif
1932
1933 #ifdef HAVE_TARGET_64_LITTLE
1934 template
1935 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
1936 #endif
1937
1938 #ifdef HAVE_TARGET_64_BIG
1939 template
1940 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
1941 #endif
1942
1943 #ifdef HAVE_TARGET_32_LITTLE
1944 template
1945 class Output_data_got<32, false>;
1946 #endif
1947
1948 #ifdef HAVE_TARGET_32_BIG
1949 template
1950 class Output_data_got<32, true>;
1951 #endif
1952
1953 #ifdef HAVE_TARGET_64_LITTLE
1954 template
1955 class Output_data_got<64, false>;
1956 #endif
1957
1958 #ifdef HAVE_TARGET_64_BIG
1959 template
1960 class Output_data_got<64, true>;
1961 #endif
1962
1963 } // End namespace gold.