* layout.cc (Layout::make_output_section): Call
[external/binutils.git] / gold / output.cc
1 // output.cc -- manage the output file for gold
2
3 // Copyright 2006, 2007, 2008, 2009 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 <cstring>
27 #include <cerrno>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <algorithm>
33 #include "libiberty.h"   // for unlink_if_ordinary()
34
35 #include "parameters.h"
36 #include "object.h"
37 #include "symtab.h"
38 #include "reloc.h"
39 #include "merge.h"
40 #include "descriptors.h"
41 #include "output.h"
42
43 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
44 #ifndef MAP_ANONYMOUS
45 # define MAP_ANONYMOUS  MAP_ANON
46 #endif
47
48 #ifndef HAVE_POSIX_FALLOCATE
49 // A dummy, non general, version of posix_fallocate.  Here we just set
50 // the file size and hope that there is enough disk space.  FIXME: We
51 // could allocate disk space by walking block by block and writing a
52 // zero byte into each block.
53 static int
54 posix_fallocate(int o, off_t offset, off_t len)
55 {
56   return ftruncate(o, offset + len);
57 }
58 #endif // !defined(HAVE_POSIX_FALLOCATE)
59
60 namespace gold
61 {
62
63 // Output_data variables.
64
65 bool Output_data::allocated_sizes_are_fixed;
66
67 // Output_data methods.
68
69 Output_data::~Output_data()
70 {
71 }
72
73 // Return the default alignment for the target size.
74
75 uint64_t
76 Output_data::default_alignment()
77 {
78   return Output_data::default_alignment_for_size(
79       parameters->target().get_size());
80 }
81
82 // Return the default alignment for a size--32 or 64.
83
84 uint64_t
85 Output_data::default_alignment_for_size(int size)
86 {
87   if (size == 32)
88     return 4;
89   else if (size == 64)
90     return 8;
91   else
92     gold_unreachable();
93 }
94
95 // Output_section_header methods.  This currently assumes that the
96 // segment and section lists are complete at construction time.
97
98 Output_section_headers::Output_section_headers(
99     const Layout* layout,
100     const Layout::Segment_list* segment_list,
101     const Layout::Section_list* section_list,
102     const Layout::Section_list* unattached_section_list,
103     const Stringpool* secnamepool,
104     const Output_section* shstrtab_section)
105   : layout_(layout),
106     segment_list_(segment_list),
107     section_list_(section_list),
108     unattached_section_list_(unattached_section_list),
109     secnamepool_(secnamepool),
110     shstrtab_section_(shstrtab_section)
111 {
112   // Count all the sections.  Start with 1 for the null section.
113   off_t count = 1;
114   if (!parameters->options().relocatable())
115     {
116       for (Layout::Segment_list::const_iterator p = segment_list->begin();
117            p != segment_list->end();
118            ++p)
119         if ((*p)->type() == elfcpp::PT_LOAD)
120           count += (*p)->output_section_count();
121     }
122   else
123     {
124       for (Layout::Section_list::const_iterator p = section_list->begin();
125            p != section_list->end();
126            ++p)
127         if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
128           ++count;
129     }
130   count += unattached_section_list->size();
131
132   const int size = parameters->target().get_size();
133   int shdr_size;
134   if (size == 32)
135     shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
136   else if (size == 64)
137     shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
138   else
139     gold_unreachable();
140
141   this->set_data_size(count * shdr_size);
142 }
143
144 // Write out the section headers.
145
146 void
147 Output_section_headers::do_write(Output_file* of)
148 {
149   switch (parameters->size_and_endianness())
150     {
151 #ifdef HAVE_TARGET_32_LITTLE
152     case Parameters::TARGET_32_LITTLE:
153       this->do_sized_write<32, false>(of);
154       break;
155 #endif
156 #ifdef HAVE_TARGET_32_BIG
157     case Parameters::TARGET_32_BIG:
158       this->do_sized_write<32, true>(of);
159       break;
160 #endif
161 #ifdef HAVE_TARGET_64_LITTLE
162     case Parameters::TARGET_64_LITTLE:
163       this->do_sized_write<64, false>(of);
164       break;
165 #endif
166 #ifdef HAVE_TARGET_64_BIG
167     case Parameters::TARGET_64_BIG:
168       this->do_sized_write<64, true>(of);
169       break;
170 #endif
171     default:
172       gold_unreachable();
173     }
174 }
175
176 template<int size, bool big_endian>
177 void
178 Output_section_headers::do_sized_write(Output_file* of)
179 {
180   off_t all_shdrs_size = this->data_size();
181   unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
182
183   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
184   unsigned char* v = view;
185
186   {
187     typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
188     oshdr.put_sh_name(0);
189     oshdr.put_sh_type(elfcpp::SHT_NULL);
190     oshdr.put_sh_flags(0);
191     oshdr.put_sh_addr(0);
192     oshdr.put_sh_offset(0);
193
194     size_t section_count = (this->data_size()
195                             / elfcpp::Elf_sizes<size>::shdr_size);
196     if (section_count < elfcpp::SHN_LORESERVE)
197       oshdr.put_sh_size(0);
198     else
199       oshdr.put_sh_size(section_count);
200
201     unsigned int shstrndx = this->shstrtab_section_->out_shndx();
202     if (shstrndx < elfcpp::SHN_LORESERVE)
203       oshdr.put_sh_link(0);
204     else
205       oshdr.put_sh_link(shstrndx);
206
207     oshdr.put_sh_info(0);
208     oshdr.put_sh_addralign(0);
209     oshdr.put_sh_entsize(0);
210   }
211
212   v += shdr_size;
213
214   unsigned int shndx = 1;
215   if (!parameters->options().relocatable())
216     {
217       for (Layout::Segment_list::const_iterator p =
218              this->segment_list_->begin();
219            p != this->segment_list_->end();
220            ++p)
221         v = (*p)->write_section_headers<size, big_endian>(this->layout_,
222                                                           this->secnamepool_,
223                                                           v,
224                                                           &shndx);
225     }
226   else
227     {
228       for (Layout::Section_list::const_iterator p =
229              this->section_list_->begin();
230            p != this->section_list_->end();
231            ++p)
232         {
233           // We do unallocated sections below, except that group
234           // sections have to come first.
235           if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
236               && (*p)->type() != elfcpp::SHT_GROUP)
237             continue;
238           gold_assert(shndx == (*p)->out_shndx());
239           elfcpp::Shdr_write<size, big_endian> oshdr(v);
240           (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
241           v += shdr_size;
242           ++shndx;
243         }
244     }
245
246   for (Layout::Section_list::const_iterator p =
247          this->unattached_section_list_->begin();
248        p != this->unattached_section_list_->end();
249        ++p)
250     {
251       // For a relocatable link, we did unallocated group sections
252       // above, since they have to come first.
253       if ((*p)->type() == elfcpp::SHT_GROUP
254           && parameters->options().relocatable())
255         continue;
256       gold_assert(shndx == (*p)->out_shndx());
257       elfcpp::Shdr_write<size, big_endian> oshdr(v);
258       (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
259       v += shdr_size;
260       ++shndx;
261     }
262
263   of->write_output_view(this->offset(), all_shdrs_size, view);
264 }
265
266 // Output_segment_header methods.
267
268 Output_segment_headers::Output_segment_headers(
269     const Layout::Segment_list& segment_list)
270   : segment_list_(segment_list)
271 {
272   const int size = parameters->target().get_size();
273   int phdr_size;
274   if (size == 32)
275     phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
276   else if (size == 64)
277     phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
278   else
279     gold_unreachable();
280
281   this->set_data_size(segment_list.size() * phdr_size);
282 }
283
284 void
285 Output_segment_headers::do_write(Output_file* of)
286 {
287   switch (parameters->size_and_endianness())
288     {
289 #ifdef HAVE_TARGET_32_LITTLE
290     case Parameters::TARGET_32_LITTLE:
291       this->do_sized_write<32, false>(of);
292       break;
293 #endif
294 #ifdef HAVE_TARGET_32_BIG
295     case Parameters::TARGET_32_BIG:
296       this->do_sized_write<32, true>(of);
297       break;
298 #endif
299 #ifdef HAVE_TARGET_64_LITTLE
300     case Parameters::TARGET_64_LITTLE:
301       this->do_sized_write<64, false>(of);
302       break;
303 #endif
304 #ifdef HAVE_TARGET_64_BIG
305     case Parameters::TARGET_64_BIG:
306       this->do_sized_write<64, true>(of);
307       break;
308 #endif
309     default:
310       gold_unreachable();
311     }
312 }
313
314 template<int size, bool big_endian>
315 void
316 Output_segment_headers::do_sized_write(Output_file* of)
317 {
318   const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
319   off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
320   gold_assert(all_phdrs_size == this->data_size());
321   unsigned char* view = of->get_output_view(this->offset(),
322                                             all_phdrs_size);
323   unsigned char* v = view;
324   for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
325        p != this->segment_list_.end();
326        ++p)
327     {
328       elfcpp::Phdr_write<size, big_endian> ophdr(v);
329       (*p)->write_header(&ophdr);
330       v += phdr_size;
331     }
332
333   gold_assert(v - view == all_phdrs_size);
334
335   of->write_output_view(this->offset(), all_phdrs_size, view);
336 }
337
338 // Output_file_header methods.
339
340 Output_file_header::Output_file_header(const Target* target,
341                                        const Symbol_table* symtab,
342                                        const Output_segment_headers* osh,
343                                        const char* entry)
344   : target_(target),
345     symtab_(symtab),
346     segment_header_(osh),
347     section_header_(NULL),
348     shstrtab_(NULL),
349     entry_(entry)
350 {
351   const int size = parameters->target().get_size();
352   int ehdr_size;
353   if (size == 32)
354     ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
355   else if (size == 64)
356     ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
357   else
358     gold_unreachable();
359
360   this->set_data_size(ehdr_size);
361 }
362
363 // Set the section table information for a file header.
364
365 void
366 Output_file_header::set_section_info(const Output_section_headers* shdrs,
367                                      const Output_section* shstrtab)
368 {
369   this->section_header_ = shdrs;
370   this->shstrtab_ = shstrtab;
371 }
372
373 // Write out the file header.
374
375 void
376 Output_file_header::do_write(Output_file* of)
377 {
378   gold_assert(this->offset() == 0);
379
380   switch (parameters->size_and_endianness())
381     {
382 #ifdef HAVE_TARGET_32_LITTLE
383     case Parameters::TARGET_32_LITTLE:
384       this->do_sized_write<32, false>(of);
385       break;
386 #endif
387 #ifdef HAVE_TARGET_32_BIG
388     case Parameters::TARGET_32_BIG:
389       this->do_sized_write<32, true>(of);
390       break;
391 #endif
392 #ifdef HAVE_TARGET_64_LITTLE
393     case Parameters::TARGET_64_LITTLE:
394       this->do_sized_write<64, false>(of);
395       break;
396 #endif
397 #ifdef HAVE_TARGET_64_BIG
398     case Parameters::TARGET_64_BIG:
399       this->do_sized_write<64, true>(of);
400       break;
401 #endif
402     default:
403       gold_unreachable();
404     }
405 }
406
407 // Write out the file header with appropriate size and endianess.
408
409 template<int size, bool big_endian>
410 void
411 Output_file_header::do_sized_write(Output_file* of)
412 {
413   gold_assert(this->offset() == 0);
414
415   int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
416   unsigned char* view = of->get_output_view(0, ehdr_size);
417   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
418
419   unsigned char e_ident[elfcpp::EI_NIDENT];
420   memset(e_ident, 0, elfcpp::EI_NIDENT);
421   e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
422   e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
423   e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
424   e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
425   if (size == 32)
426     e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
427   else if (size == 64)
428     e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
429   else
430     gold_unreachable();
431   e_ident[elfcpp::EI_DATA] = (big_endian
432                               ? elfcpp::ELFDATA2MSB
433                               : elfcpp::ELFDATA2LSB);
434   e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
435   oehdr.put_e_ident(e_ident);
436
437   elfcpp::ET e_type;
438   if (parameters->options().relocatable())
439     e_type = elfcpp::ET_REL;
440   else if (parameters->options().shared())
441     e_type = elfcpp::ET_DYN;
442   else
443     e_type = elfcpp::ET_EXEC;
444   oehdr.put_e_type(e_type);
445
446   oehdr.put_e_machine(this->target_->machine_code());
447   oehdr.put_e_version(elfcpp::EV_CURRENT);
448
449   oehdr.put_e_entry(this->entry<size>());
450
451   if (this->segment_header_ == NULL)
452     oehdr.put_e_phoff(0);
453   else
454     oehdr.put_e_phoff(this->segment_header_->offset());
455
456   oehdr.put_e_shoff(this->section_header_->offset());
457
458   // FIXME: The target needs to set the flags.
459   oehdr.put_e_flags(0);
460
461   oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
462
463   if (this->segment_header_ == NULL)
464     {
465       oehdr.put_e_phentsize(0);
466       oehdr.put_e_phnum(0);
467     }
468   else
469     {
470       oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
471       oehdr.put_e_phnum(this->segment_header_->data_size()
472                         / elfcpp::Elf_sizes<size>::phdr_size);
473     }
474
475   oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
476   size_t section_count = (this->section_header_->data_size()
477                           / elfcpp::Elf_sizes<size>::shdr_size);
478
479   if (section_count < elfcpp::SHN_LORESERVE)
480     oehdr.put_e_shnum(this->section_header_->data_size()
481                       / elfcpp::Elf_sizes<size>::shdr_size);
482   else
483     oehdr.put_e_shnum(0);
484
485   unsigned int shstrndx = this->shstrtab_->out_shndx();
486   if (shstrndx < elfcpp::SHN_LORESERVE)
487     oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
488   else
489     oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
490
491   // Let the target adjust the ELF header, e.g., to set EI_OSABI in
492   // the e_ident field.
493   parameters->target().adjust_elf_header(view, ehdr_size);
494
495   of->write_output_view(0, ehdr_size, view);
496 }
497
498 // Return the value to use for the entry address.  THIS->ENTRY_ is the
499 // symbol specified on the command line, if any.
500
501 template<int size>
502 typename elfcpp::Elf_types<size>::Elf_Addr
503 Output_file_header::entry()
504 {
505   const bool should_issue_warning = (this->entry_ != NULL
506                                      && !parameters->options().relocatable()
507                                      && !parameters->options().shared());
508
509   // FIXME: Need to support target specific entry symbol.
510   const char* entry = this->entry_;
511   if (entry == NULL)
512     entry = "_start";
513
514   Symbol* sym = this->symtab_->lookup(entry);
515
516   typename Sized_symbol<size>::Value_type v;
517   if (sym != NULL)
518     {
519       Sized_symbol<size>* ssym;
520       ssym = this->symtab_->get_sized_symbol<size>(sym);
521       if (!ssym->is_defined() && should_issue_warning)
522         gold_warning("entry symbol '%s' exists but is not defined", entry);
523       v = ssym->value();
524     }
525   else
526     {
527       // We couldn't find the entry symbol.  See if we can parse it as
528       // a number.  This supports, e.g., -e 0x1000.
529       char* endptr;
530       v = strtoull(entry, &endptr, 0);
531       if (*endptr != '\0')
532         {
533           if (should_issue_warning)
534             gold_warning("cannot find entry symbol '%s'", entry);
535           v = 0;
536         }
537     }
538
539   return v;
540 }
541
542 // Output_data_const methods.
543
544 void
545 Output_data_const::do_write(Output_file* of)
546 {
547   of->write(this->offset(), this->data_.data(), this->data_.size());
548 }
549
550 // Output_data_const_buffer methods.
551
552 void
553 Output_data_const_buffer::do_write(Output_file* of)
554 {
555   of->write(this->offset(), this->p_, this->data_size());
556 }
557
558 // Output_section_data methods.
559
560 // Record the output section, and set the entry size and such.
561
562 void
563 Output_section_data::set_output_section(Output_section* os)
564 {
565   gold_assert(this->output_section_ == NULL);
566   this->output_section_ = os;
567   this->do_adjust_output_section(os);
568 }
569
570 // Return the section index of the output section.
571
572 unsigned int
573 Output_section_data::do_out_shndx() const
574 {
575   gold_assert(this->output_section_ != NULL);
576   return this->output_section_->out_shndx();
577 }
578
579 // Set the alignment, which means we may need to update the alignment
580 // of the output section.
581
582 void
583 Output_section_data::set_addralign(uint64_t addralign)
584 {
585   this->addralign_ = addralign;
586   if (this->output_section_ != NULL
587       && this->output_section_->addralign() < addralign)
588     this->output_section_->set_addralign(addralign);
589 }
590
591 // Output_data_strtab methods.
592
593 // Set the final data size.
594
595 void
596 Output_data_strtab::set_final_data_size()
597 {
598   this->strtab_->set_string_offsets();
599   this->set_data_size(this->strtab_->get_strtab_size());
600 }
601
602 // Write out a string table.
603
604 void
605 Output_data_strtab::do_write(Output_file* of)
606 {
607   this->strtab_->write(of, this->offset());
608 }
609
610 // Output_reloc methods.
611
612 // A reloc against a global symbol.
613
614 template<bool dynamic, int size, bool big_endian>
615 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
616     Symbol* gsym,
617     unsigned int type,
618     Output_data* od,
619     Address address,
620     bool is_relative)
621   : address_(address), local_sym_index_(GSYM_CODE), type_(type),
622     is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
623 {
624   // this->type_ is a bitfield; make sure TYPE fits.
625   gold_assert(this->type_ == type);
626   this->u1_.gsym = gsym;
627   this->u2_.od = od;
628   if (dynamic)
629     this->set_needs_dynsym_index();
630 }
631
632 template<bool dynamic, int size, bool big_endian>
633 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
634     Symbol* gsym,
635     unsigned int type,
636     Sized_relobj<size, big_endian>* relobj,
637     unsigned int shndx,
638     Address address,
639     bool is_relative)
640   : address_(address), local_sym_index_(GSYM_CODE), type_(type),
641     is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
642 {
643   gold_assert(shndx != INVALID_CODE);
644   // this->type_ is a bitfield; make sure TYPE fits.
645   gold_assert(this->type_ == type);
646   this->u1_.gsym = gsym;
647   this->u2_.relobj = relobj;
648   if (dynamic)
649     this->set_needs_dynsym_index();
650 }
651
652 // A reloc against a local symbol.
653
654 template<bool dynamic, int size, bool big_endian>
655 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
656     Sized_relobj<size, big_endian>* relobj,
657     unsigned int local_sym_index,
658     unsigned int type,
659     Output_data* od,
660     Address address,
661     bool is_relative,
662     bool is_section_symbol)
663   : address_(address), local_sym_index_(local_sym_index), type_(type),
664     is_relative_(is_relative), is_section_symbol_(is_section_symbol),
665     shndx_(INVALID_CODE)
666 {
667   gold_assert(local_sym_index != GSYM_CODE
668               && local_sym_index != INVALID_CODE);
669   // this->type_ is a bitfield; make sure TYPE fits.
670   gold_assert(this->type_ == type);
671   this->u1_.relobj = relobj;
672   this->u2_.od = od;
673   if (dynamic)
674     this->set_needs_dynsym_index();
675 }
676
677 template<bool dynamic, int size, bool big_endian>
678 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
679     Sized_relobj<size, big_endian>* relobj,
680     unsigned int local_sym_index,
681     unsigned int type,
682     unsigned int shndx,
683     Address address,
684     bool is_relative,
685     bool is_section_symbol)
686   : address_(address), local_sym_index_(local_sym_index), type_(type),
687     is_relative_(is_relative), is_section_symbol_(is_section_symbol),
688     shndx_(shndx)
689 {
690   gold_assert(local_sym_index != GSYM_CODE
691               && local_sym_index != INVALID_CODE);
692   gold_assert(shndx != INVALID_CODE);
693   // this->type_ is a bitfield; make sure TYPE fits.
694   gold_assert(this->type_ == type);
695   this->u1_.relobj = relobj;
696   this->u2_.relobj = relobj;
697   if (dynamic)
698     this->set_needs_dynsym_index();
699 }
700
701 // A reloc against the STT_SECTION symbol of an output section.
702
703 template<bool dynamic, int size, bool big_endian>
704 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
705     Output_section* os,
706     unsigned int type,
707     Output_data* od,
708     Address address)
709   : address_(address), local_sym_index_(SECTION_CODE), type_(type),
710     is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
711 {
712   // this->type_ is a bitfield; make sure TYPE fits.
713   gold_assert(this->type_ == type);
714   this->u1_.os = os;
715   this->u2_.od = od;
716   if (dynamic)
717     this->set_needs_dynsym_index();
718   else
719     os->set_needs_symtab_index();
720 }
721
722 template<bool dynamic, int size, bool big_endian>
723 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
724     Output_section* os,
725     unsigned int type,
726     Sized_relobj<size, big_endian>* relobj,
727     unsigned int shndx,
728     Address address)
729   : address_(address), local_sym_index_(SECTION_CODE), type_(type),
730     is_relative_(false), is_section_symbol_(true), shndx_(shndx)
731 {
732   gold_assert(shndx != INVALID_CODE);
733   // this->type_ is a bitfield; make sure TYPE fits.
734   gold_assert(this->type_ == type);
735   this->u1_.os = os;
736   this->u2_.relobj = relobj;
737   if (dynamic)
738     this->set_needs_dynsym_index();
739   else
740     os->set_needs_symtab_index();
741 }
742
743 // Record that we need a dynamic symbol index for this relocation.
744
745 template<bool dynamic, int size, bool big_endian>
746 void
747 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
748 set_needs_dynsym_index()
749 {
750   if (this->is_relative_)
751     return;
752   switch (this->local_sym_index_)
753     {
754     case INVALID_CODE:
755       gold_unreachable();
756
757     case GSYM_CODE:
758       this->u1_.gsym->set_needs_dynsym_entry();
759       break;
760
761     case SECTION_CODE:
762       this->u1_.os->set_needs_dynsym_index();
763       break;
764
765     case 0:
766       break;
767
768     default:
769       {
770         const unsigned int lsi = this->local_sym_index_;
771         if (!this->is_section_symbol_)
772           this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
773         else
774           this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
775       }
776       break;
777     }
778 }
779
780 // Get the symbol index of a relocation.
781
782 template<bool dynamic, int size, bool big_endian>
783 unsigned int
784 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
785   const
786 {
787   unsigned int index;
788   switch (this->local_sym_index_)
789     {
790     case INVALID_CODE:
791       gold_unreachable();
792
793     case GSYM_CODE:
794       if (this->u1_.gsym == NULL)
795         index = 0;
796       else if (dynamic)
797         index = this->u1_.gsym->dynsym_index();
798       else
799         index = this->u1_.gsym->symtab_index();
800       break;
801
802     case SECTION_CODE:
803       if (dynamic)
804         index = this->u1_.os->dynsym_index();
805       else
806         index = this->u1_.os->symtab_index();
807       break;
808
809     case 0:
810       // Relocations without symbols use a symbol index of 0.
811       index = 0;
812       break;
813
814     default:
815       {
816         const unsigned int lsi = this->local_sym_index_;
817         if (!this->is_section_symbol_)
818           {
819             if (dynamic)
820               index = this->u1_.relobj->dynsym_index(lsi);
821             else
822               index = this->u1_.relobj->symtab_index(lsi);
823           }
824         else
825           {
826             Output_section* os = this->u1_.relobj->output_section(lsi);
827             gold_assert(os != NULL);
828             if (dynamic)
829               index = os->dynsym_index();
830             else
831               index = os->symtab_index();
832           }
833       }
834       break;
835     }
836   gold_assert(index != -1U);
837   return index;
838 }
839
840 // For a local section symbol, get the address of the offset ADDEND
841 // within the input section.
842
843 template<bool dynamic, int size, bool big_endian>
844 typename elfcpp::Elf_types<size>::Elf_Addr
845 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
846   local_section_offset(Addend addend) const
847 {
848   gold_assert(this->local_sym_index_ != GSYM_CODE
849               && this->local_sym_index_ != SECTION_CODE
850               && this->local_sym_index_ != INVALID_CODE
851               && this->is_section_symbol_);
852   const unsigned int lsi = this->local_sym_index_;
853   Output_section* os = this->u1_.relobj->output_section(lsi);
854   gold_assert(os != NULL);
855   Address offset = this->u1_.relobj->get_output_section_offset(lsi);
856   if (offset != invalid_address)
857     return offset + addend;
858   // This is a merge section.
859   offset = os->output_address(this->u1_.relobj, lsi, addend);
860   gold_assert(offset != invalid_address);
861   return offset;
862 }
863
864 // Get the output address of a relocation.
865
866 template<bool dynamic, int size, bool big_endian>
867 typename elfcpp::Elf_types<size>::Elf_Addr
868 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
869 {
870   Address address = this->address_;
871   if (this->shndx_ != INVALID_CODE)
872     {
873       Output_section* os = this->u2_.relobj->output_section(this->shndx_);
874       gold_assert(os != NULL);
875       Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
876       if (off != invalid_address)
877         address += os->address() + off;
878       else
879         {
880           address = os->output_address(this->u2_.relobj, this->shndx_,
881                                        address);
882           gold_assert(address != invalid_address);
883         }
884     }
885   else if (this->u2_.od != NULL)
886     address += this->u2_.od->address();
887   return address;
888 }
889
890 // Write out the offset and info fields of a Rel or Rela relocation
891 // entry.
892
893 template<bool dynamic, int size, bool big_endian>
894 template<typename Write_rel>
895 void
896 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
897     Write_rel* wr) const
898 {
899   wr->put_r_offset(this->get_address());
900   unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
901   wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
902 }
903
904 // Write out a Rel relocation.
905
906 template<bool dynamic, int size, bool big_endian>
907 void
908 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
909     unsigned char* pov) const
910 {
911   elfcpp::Rel_write<size, big_endian> orel(pov);
912   this->write_rel(&orel);
913 }
914
915 // Get the value of the symbol referred to by a Rel relocation.
916
917 template<bool dynamic, int size, bool big_endian>
918 typename elfcpp::Elf_types<size>::Elf_Addr
919 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
920     Addend addend) const
921 {
922   if (this->local_sym_index_ == GSYM_CODE)
923     {
924       const Sized_symbol<size>* sym;
925       sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
926       return sym->value() + addend;
927     }
928   gold_assert(this->local_sym_index_ != SECTION_CODE
929               && this->local_sym_index_ != INVALID_CODE
930               && !this->is_section_symbol_);
931   const unsigned int lsi = this->local_sym_index_;
932   const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
933   return symval->value(this->u1_.relobj, addend);
934 }
935
936 // Reloc comparison.  This function sorts the dynamic relocs for the
937 // benefit of the dynamic linker.  First we sort all relative relocs
938 // to the front.  Among relative relocs, we sort by output address.
939 // Among non-relative relocs, we sort by symbol index, then by output
940 // address.
941
942 template<bool dynamic, int size, bool big_endian>
943 int
944 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
945   compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
946     const
947 {
948   if (this->is_relative_)
949     {
950       if (!r2.is_relative_)
951         return -1;
952       // Otherwise sort by reloc address below.
953     }
954   else if (r2.is_relative_)
955     return 1;
956   else
957     {
958       unsigned int sym1 = this->get_symbol_index();
959       unsigned int sym2 = r2.get_symbol_index();
960       if (sym1 < sym2)
961         return -1;
962       else if (sym1 > sym2)
963         return 1;
964       // Otherwise sort by reloc address.
965     }
966
967   section_offset_type addr1 = this->get_address();
968   section_offset_type addr2 = r2.get_address();
969   if (addr1 < addr2)
970     return -1;
971   else if (addr1 > addr2)
972     return 1;
973
974   // Final tie breaker, in order to generate the same output on any
975   // host: reloc type.
976   unsigned int type1 = this->type_;
977   unsigned int type2 = r2.type_;
978   if (type1 < type2)
979     return -1;
980   else if (type1 > type2)
981     return 1;
982
983   // These relocs appear to be exactly the same.
984   return 0;
985 }
986
987 // Write out a Rela relocation.
988
989 template<bool dynamic, int size, bool big_endian>
990 void
991 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
992     unsigned char* pov) const
993 {
994   elfcpp::Rela_write<size, big_endian> orel(pov);
995   this->rel_.write_rel(&orel);
996   Addend addend = this->addend_;
997   if (this->rel_.is_relative())
998     addend = this->rel_.symbol_value(addend);
999   else if (this->rel_.is_local_section_symbol())
1000     addend = this->rel_.local_section_offset(addend);
1001   orel.put_r_addend(addend);
1002 }
1003
1004 // Output_data_reloc_base methods.
1005
1006 // Adjust the output section.
1007
1008 template<int sh_type, bool dynamic, int size, bool big_endian>
1009 void
1010 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1011     ::do_adjust_output_section(Output_section* os)
1012 {
1013   if (sh_type == elfcpp::SHT_REL)
1014     os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1015   else if (sh_type == elfcpp::SHT_RELA)
1016     os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1017   else
1018     gold_unreachable();
1019   if (dynamic)
1020     os->set_should_link_to_dynsym();
1021   else
1022     os->set_should_link_to_symtab();
1023 }
1024
1025 // Write out relocation data.
1026
1027 template<int sh_type, bool dynamic, int size, bool big_endian>
1028 void
1029 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1030     Output_file* of)
1031 {
1032   const off_t off = this->offset();
1033   const off_t oview_size = this->data_size();
1034   unsigned char* const oview = of->get_output_view(off, oview_size);
1035
1036   if (this->sort_relocs_)
1037     {
1038       gold_assert(dynamic);
1039       std::sort(this->relocs_.begin(), this->relocs_.end(),
1040                 Sort_relocs_comparison());
1041     }
1042
1043   unsigned char* pov = oview;
1044   for (typename Relocs::const_iterator p = this->relocs_.begin();
1045        p != this->relocs_.end();
1046        ++p)
1047     {
1048       p->write(pov);
1049       pov += reloc_size;
1050     }
1051
1052   gold_assert(pov - oview == oview_size);
1053
1054   of->write_output_view(off, oview_size, oview);
1055
1056   // We no longer need the relocation entries.
1057   this->relocs_.clear();
1058 }
1059
1060 // Class Output_relocatable_relocs.
1061
1062 template<int sh_type, int size, bool big_endian>
1063 void
1064 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1065 {
1066   this->set_data_size(this->rr_->output_reloc_count()
1067                       * Reloc_types<sh_type, size, big_endian>::reloc_size);
1068 }
1069
1070 // class Output_data_group.
1071
1072 template<int size, bool big_endian>
1073 Output_data_group<size, big_endian>::Output_data_group(
1074     Sized_relobj<size, big_endian>* relobj,
1075     section_size_type entry_count,
1076     elfcpp::Elf_Word flags,
1077     std::vector<unsigned int>* input_shndxes)
1078   : Output_section_data(entry_count * 4, 4),
1079     relobj_(relobj),
1080     flags_(flags)
1081 {
1082   this->input_shndxes_.swap(*input_shndxes);
1083 }
1084
1085 // Write out the section group, which means translating the section
1086 // indexes to apply to the output file.
1087
1088 template<int size, bool big_endian>
1089 void
1090 Output_data_group<size, big_endian>::do_write(Output_file* of)
1091 {
1092   const off_t off = this->offset();
1093   const section_size_type oview_size =
1094     convert_to_section_size_type(this->data_size());
1095   unsigned char* const oview = of->get_output_view(off, oview_size);
1096
1097   elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1098   elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1099   ++contents;
1100
1101   for (std::vector<unsigned int>::const_iterator p =
1102          this->input_shndxes_.begin();
1103        p != this->input_shndxes_.end();
1104        ++p, ++contents)
1105     {
1106       Output_section* os = this->relobj_->output_section(*p);
1107
1108       unsigned int output_shndx;
1109       if (os != NULL)
1110         output_shndx = os->out_shndx();
1111       else
1112         {
1113           this->relobj_->error(_("section group retained but "
1114                                  "group element discarded"));
1115           output_shndx = 0;
1116         }
1117
1118       elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1119     }
1120
1121   size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1122   gold_assert(wrote == oview_size);
1123
1124   of->write_output_view(off, oview_size, oview);
1125
1126   // We no longer need this information.
1127   this->input_shndxes_.clear();
1128 }
1129
1130 // Output_data_got::Got_entry methods.
1131
1132 // Write out the entry.
1133
1134 template<int size, bool big_endian>
1135 void
1136 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1137 {
1138   Valtype val = 0;
1139
1140   switch (this->local_sym_index_)
1141     {
1142     case GSYM_CODE:
1143       {
1144         // If the symbol is resolved locally, we need to write out the
1145         // link-time value, which will be relocated dynamically by a
1146         // RELATIVE relocation.
1147         Symbol* gsym = this->u_.gsym;
1148         Sized_symbol<size>* sgsym;
1149         // This cast is a bit ugly.  We don't want to put a
1150         // virtual method in Symbol, because we want Symbol to be
1151         // as small as possible.
1152         sgsym = static_cast<Sized_symbol<size>*>(gsym);
1153         val = sgsym->value();
1154       }
1155       break;
1156
1157     case CONSTANT_CODE:
1158       val = this->u_.constant;
1159       break;
1160
1161     default:
1162       {
1163         const unsigned int lsi = this->local_sym_index_;
1164         const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1165         val = symval->value(this->u_.object, 0);
1166       }
1167       break;
1168     }
1169
1170   elfcpp::Swap<size, big_endian>::writeval(pov, val);
1171 }
1172
1173 // Output_data_got methods.
1174
1175 // Add an entry for a global symbol to the GOT.  This returns true if
1176 // this is a new GOT entry, false if the symbol already had a GOT
1177 // entry.
1178
1179 template<int size, bool big_endian>
1180 bool
1181 Output_data_got<size, big_endian>::add_global(
1182     Symbol* gsym,
1183     unsigned int got_type)
1184 {
1185   if (gsym->has_got_offset(got_type))
1186     return false;
1187
1188   this->entries_.push_back(Got_entry(gsym));
1189   this->set_got_size();
1190   gsym->set_got_offset(got_type, this->last_got_offset());
1191   return true;
1192 }
1193
1194 // Add an entry for a global symbol to the GOT, and add a dynamic
1195 // relocation of type R_TYPE for the GOT entry.
1196 template<int size, bool big_endian>
1197 void
1198 Output_data_got<size, big_endian>::add_global_with_rel(
1199     Symbol* gsym,
1200     unsigned int got_type,
1201     Rel_dyn* rel_dyn,
1202     unsigned int r_type)
1203 {
1204   if (gsym->has_got_offset(got_type))
1205     return;
1206
1207   this->entries_.push_back(Got_entry());
1208   this->set_got_size();
1209   unsigned int got_offset = this->last_got_offset();
1210   gsym->set_got_offset(got_type, got_offset);
1211   rel_dyn->add_global(gsym, r_type, this, got_offset);
1212 }
1213
1214 template<int size, bool big_endian>
1215 void
1216 Output_data_got<size, big_endian>::add_global_with_rela(
1217     Symbol* gsym,
1218     unsigned int got_type,
1219     Rela_dyn* rela_dyn,
1220     unsigned int r_type)
1221 {
1222   if (gsym->has_got_offset(got_type))
1223     return;
1224
1225   this->entries_.push_back(Got_entry());
1226   this->set_got_size();
1227   unsigned int got_offset = this->last_got_offset();
1228   gsym->set_got_offset(got_type, got_offset);
1229   rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1230 }
1231
1232 // Add a pair of entries for a global symbol to the GOT, and add
1233 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1234 // If R_TYPE_2 == 0, add the second entry with no relocation.
1235 template<int size, bool big_endian>
1236 void
1237 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1238     Symbol* gsym,
1239     unsigned int got_type,
1240     Rel_dyn* rel_dyn,
1241     unsigned int r_type_1,
1242     unsigned int r_type_2)
1243 {
1244   if (gsym->has_got_offset(got_type))
1245     return;
1246
1247   this->entries_.push_back(Got_entry());
1248   unsigned int got_offset = this->last_got_offset();
1249   gsym->set_got_offset(got_type, got_offset);
1250   rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1251
1252   this->entries_.push_back(Got_entry());
1253   if (r_type_2 != 0)
1254     {
1255       got_offset = this->last_got_offset();
1256       rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1257     }
1258
1259   this->set_got_size();
1260 }
1261
1262 template<int size, bool big_endian>
1263 void
1264 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1265     Symbol* gsym,
1266     unsigned int got_type,
1267     Rela_dyn* rela_dyn,
1268     unsigned int r_type_1,
1269     unsigned int r_type_2)
1270 {
1271   if (gsym->has_got_offset(got_type))
1272     return;
1273
1274   this->entries_.push_back(Got_entry());
1275   unsigned int got_offset = this->last_got_offset();
1276   gsym->set_got_offset(got_type, got_offset);
1277   rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1278
1279   this->entries_.push_back(Got_entry());
1280   if (r_type_2 != 0)
1281     {
1282       got_offset = this->last_got_offset();
1283       rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1284     }
1285
1286   this->set_got_size();
1287 }
1288
1289 // Add an entry for a local symbol to the GOT.  This returns true if
1290 // this is a new GOT entry, false if the symbol already has a GOT
1291 // entry.
1292
1293 template<int size, bool big_endian>
1294 bool
1295 Output_data_got<size, big_endian>::add_local(
1296     Sized_relobj<size, big_endian>* object,
1297     unsigned int symndx,
1298     unsigned int got_type)
1299 {
1300   if (object->local_has_got_offset(symndx, got_type))
1301     return false;
1302
1303   this->entries_.push_back(Got_entry(object, symndx));
1304   this->set_got_size();
1305   object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1306   return true;
1307 }
1308
1309 // Add an entry for a local symbol to the GOT, and add a dynamic
1310 // relocation of type R_TYPE for the GOT entry.
1311 template<int size, bool big_endian>
1312 void
1313 Output_data_got<size, big_endian>::add_local_with_rel(
1314     Sized_relobj<size, big_endian>* object,
1315     unsigned int symndx,
1316     unsigned int got_type,
1317     Rel_dyn* rel_dyn,
1318     unsigned int r_type)
1319 {
1320   if (object->local_has_got_offset(symndx, got_type))
1321     return;
1322
1323   this->entries_.push_back(Got_entry());
1324   this->set_got_size();
1325   unsigned int got_offset = this->last_got_offset();
1326   object->set_local_got_offset(symndx, got_type, got_offset);
1327   rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1328 }
1329
1330 template<int size, bool big_endian>
1331 void
1332 Output_data_got<size, big_endian>::add_local_with_rela(
1333     Sized_relobj<size, big_endian>* object,
1334     unsigned int symndx,
1335     unsigned int got_type,
1336     Rela_dyn* rela_dyn,
1337     unsigned int r_type)
1338 {
1339   if (object->local_has_got_offset(symndx, got_type))
1340     return;
1341
1342   this->entries_.push_back(Got_entry());
1343   this->set_got_size();
1344   unsigned int got_offset = this->last_got_offset();
1345   object->set_local_got_offset(symndx, got_type, got_offset);
1346   rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1347 }
1348
1349 // Add a pair of entries for a local symbol to the GOT, and add
1350 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1351 // If R_TYPE_2 == 0, add the second entry with no relocation.
1352 template<int size, bool big_endian>
1353 void
1354 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1355     Sized_relobj<size, big_endian>* object,
1356     unsigned int symndx,
1357     unsigned int shndx,
1358     unsigned int got_type,
1359     Rel_dyn* rel_dyn,
1360     unsigned int r_type_1,
1361     unsigned int r_type_2)
1362 {
1363   if (object->local_has_got_offset(symndx, got_type))
1364     return;
1365
1366   this->entries_.push_back(Got_entry());
1367   unsigned int got_offset = this->last_got_offset();
1368   object->set_local_got_offset(symndx, got_type, got_offset);
1369   Output_section* os = object->output_section(shndx);
1370   rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1371
1372   this->entries_.push_back(Got_entry(object, symndx));
1373   if (r_type_2 != 0)
1374     {
1375       got_offset = this->last_got_offset();
1376       rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1377     }
1378
1379   this->set_got_size();
1380 }
1381
1382 template<int size, bool big_endian>
1383 void
1384 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1385     Sized_relobj<size, big_endian>* object,
1386     unsigned int symndx,
1387     unsigned int shndx,
1388     unsigned int got_type,
1389     Rela_dyn* rela_dyn,
1390     unsigned int r_type_1,
1391     unsigned int r_type_2)
1392 {
1393   if (object->local_has_got_offset(symndx, got_type))
1394     return;
1395
1396   this->entries_.push_back(Got_entry());
1397   unsigned int got_offset = this->last_got_offset();
1398   object->set_local_got_offset(symndx, got_type, got_offset);
1399   Output_section* os = object->output_section(shndx);
1400   rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1401
1402   this->entries_.push_back(Got_entry(object, symndx));
1403   if (r_type_2 != 0)
1404     {
1405       got_offset = this->last_got_offset();
1406       rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1407     }
1408
1409   this->set_got_size();
1410 }
1411
1412 // Write out the GOT.
1413
1414 template<int size, bool big_endian>
1415 void
1416 Output_data_got<size, big_endian>::do_write(Output_file* of)
1417 {
1418   const int add = size / 8;
1419
1420   const off_t off = this->offset();
1421   const off_t oview_size = this->data_size();
1422   unsigned char* const oview = of->get_output_view(off, oview_size);
1423
1424   unsigned char* pov = oview;
1425   for (typename Got_entries::const_iterator p = this->entries_.begin();
1426        p != this->entries_.end();
1427        ++p)
1428     {
1429       p->write(pov);
1430       pov += add;
1431     }
1432
1433   gold_assert(pov - oview == oview_size);
1434
1435   of->write_output_view(off, oview_size, oview);
1436
1437   // We no longer need the GOT entries.
1438   this->entries_.clear();
1439 }
1440
1441 // Output_data_dynamic::Dynamic_entry methods.
1442
1443 // Write out the entry.
1444
1445 template<int size, bool big_endian>
1446 void
1447 Output_data_dynamic::Dynamic_entry::write(
1448     unsigned char* pov,
1449     const Stringpool* pool) const
1450 {
1451   typename elfcpp::Elf_types<size>::Elf_WXword val;
1452   switch (this->offset_)
1453     {
1454     case DYNAMIC_NUMBER:
1455       val = this->u_.val;
1456       break;
1457
1458     case DYNAMIC_SECTION_SIZE:
1459       val = this->u_.od->data_size();
1460       break;
1461
1462     case DYNAMIC_SYMBOL:
1463       {
1464         const Sized_symbol<size>* s =
1465           static_cast<const Sized_symbol<size>*>(this->u_.sym);
1466         val = s->value();
1467       }
1468       break;
1469
1470     case DYNAMIC_STRING:
1471       val = pool->get_offset(this->u_.str);
1472       break;
1473
1474     default:
1475       val = this->u_.od->address() + this->offset_;
1476       break;
1477     }
1478
1479   elfcpp::Dyn_write<size, big_endian> dw(pov);
1480   dw.put_d_tag(this->tag_);
1481   dw.put_d_val(val);
1482 }
1483
1484 // Output_data_dynamic methods.
1485
1486 // Adjust the output section to set the entry size.
1487
1488 void
1489 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1490 {
1491   if (parameters->target().get_size() == 32)
1492     os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1493   else if (parameters->target().get_size() == 64)
1494     os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1495   else
1496     gold_unreachable();
1497 }
1498
1499 // Set the final data size.
1500
1501 void
1502 Output_data_dynamic::set_final_data_size()
1503 {
1504   // Add the terminating entry.
1505   this->add_constant(elfcpp::DT_NULL, 0);
1506
1507   int dyn_size;
1508   if (parameters->target().get_size() == 32)
1509     dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1510   else if (parameters->target().get_size() == 64)
1511     dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1512   else
1513     gold_unreachable();
1514   this->set_data_size(this->entries_.size() * dyn_size);
1515 }
1516
1517 // Write out the dynamic entries.
1518
1519 void
1520 Output_data_dynamic::do_write(Output_file* of)
1521 {
1522   switch (parameters->size_and_endianness())
1523     {
1524 #ifdef HAVE_TARGET_32_LITTLE
1525     case Parameters::TARGET_32_LITTLE:
1526       this->sized_write<32, false>(of);
1527       break;
1528 #endif
1529 #ifdef HAVE_TARGET_32_BIG
1530     case Parameters::TARGET_32_BIG:
1531       this->sized_write<32, true>(of);
1532       break;
1533 #endif
1534 #ifdef HAVE_TARGET_64_LITTLE
1535     case Parameters::TARGET_64_LITTLE:
1536       this->sized_write<64, false>(of);
1537       break;
1538 #endif
1539 #ifdef HAVE_TARGET_64_BIG
1540     case Parameters::TARGET_64_BIG:
1541       this->sized_write<64, true>(of);
1542       break;
1543 #endif
1544     default:
1545       gold_unreachable();
1546     }
1547 }
1548
1549 template<int size, bool big_endian>
1550 void
1551 Output_data_dynamic::sized_write(Output_file* of)
1552 {
1553   const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1554
1555   const off_t offset = this->offset();
1556   const off_t oview_size = this->data_size();
1557   unsigned char* const oview = of->get_output_view(offset, oview_size);
1558
1559   unsigned char* pov = oview;
1560   for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1561        p != this->entries_.end();
1562        ++p)
1563     {
1564       p->write<size, big_endian>(pov, this->pool_);
1565       pov += dyn_size;
1566     }
1567
1568   gold_assert(pov - oview == oview_size);
1569
1570   of->write_output_view(offset, oview_size, oview);
1571
1572   // We no longer need the dynamic entries.
1573   this->entries_.clear();
1574 }
1575
1576 // Class Output_symtab_xindex.
1577
1578 void
1579 Output_symtab_xindex::do_write(Output_file* of)
1580 {
1581   const off_t offset = this->offset();
1582   const off_t oview_size = this->data_size();
1583   unsigned char* const oview = of->get_output_view(offset, oview_size);
1584
1585   memset(oview, 0, oview_size);
1586
1587   if (parameters->target().is_big_endian())
1588     this->endian_do_write<true>(oview);
1589   else
1590     this->endian_do_write<false>(oview);
1591
1592   of->write_output_view(offset, oview_size, oview);
1593
1594   // We no longer need the data.
1595   this->entries_.clear();
1596 }
1597
1598 template<bool big_endian>
1599 void
1600 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1601 {
1602   for (Xindex_entries::const_iterator p = this->entries_.begin();
1603        p != this->entries_.end();
1604        ++p)
1605     elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
1606 }
1607
1608 // Output_section::Input_section methods.
1609
1610 // Return the data size.  For an input section we store the size here.
1611 // For an Output_section_data, we have to ask it for the size.
1612
1613 off_t
1614 Output_section::Input_section::data_size() const
1615 {
1616   if (this->is_input_section())
1617     return this->u1_.data_size;
1618   else
1619     return this->u2_.posd->data_size();
1620 }
1621
1622 // Set the address and file offset.
1623
1624 void
1625 Output_section::Input_section::set_address_and_file_offset(
1626     uint64_t address,
1627     off_t file_offset,
1628     off_t section_file_offset)
1629 {
1630   if (this->is_input_section())
1631     this->u2_.object->set_section_offset(this->shndx_,
1632                                          file_offset - section_file_offset);
1633   else
1634     this->u2_.posd->set_address_and_file_offset(address, file_offset);
1635 }
1636
1637 // Reset the address and file offset.
1638
1639 void
1640 Output_section::Input_section::reset_address_and_file_offset()
1641 {
1642   if (!this->is_input_section())
1643     this->u2_.posd->reset_address_and_file_offset();
1644 }
1645
1646 // Finalize the data size.
1647
1648 void
1649 Output_section::Input_section::finalize_data_size()
1650 {
1651   if (!this->is_input_section())
1652     this->u2_.posd->finalize_data_size();
1653 }
1654
1655 // Try to turn an input offset into an output offset.  We want to
1656 // return the output offset relative to the start of this
1657 // Input_section in the output section.
1658
1659 inline bool
1660 Output_section::Input_section::output_offset(
1661     const Relobj* object,
1662     unsigned int shndx,
1663     section_offset_type offset,
1664     section_offset_type *poutput) const
1665 {
1666   if (!this->is_input_section())
1667     return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1668   else
1669     {
1670       if (this->shndx_ != shndx || this->u2_.object != object)
1671         return false;
1672       *poutput = offset;
1673       return true;
1674     }
1675 }
1676
1677 // Return whether this is the merge section for the input section
1678 // SHNDX in OBJECT.
1679
1680 inline bool
1681 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1682                                                     unsigned int shndx) const
1683 {
1684   if (this->is_input_section())
1685     return false;
1686   return this->u2_.posd->is_merge_section_for(object, shndx);
1687 }
1688
1689 // Write out the data.  We don't have to do anything for an input
1690 // section--they are handled via Object::relocate--but this is where
1691 // we write out the data for an Output_section_data.
1692
1693 void
1694 Output_section::Input_section::write(Output_file* of)
1695 {
1696   if (!this->is_input_section())
1697     this->u2_.posd->write(of);
1698 }
1699
1700 // Write the data to a buffer.  As for write(), we don't have to do
1701 // anything for an input section.
1702
1703 void
1704 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1705 {
1706   if (!this->is_input_section())
1707     this->u2_.posd->write_to_buffer(buffer);
1708 }
1709
1710 // Print to a map file.
1711
1712 void
1713 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1714 {
1715   switch (this->shndx_)
1716     {
1717     case OUTPUT_SECTION_CODE:
1718     case MERGE_DATA_SECTION_CODE:
1719     case MERGE_STRING_SECTION_CODE:
1720       this->u2_.posd->print_to_mapfile(mapfile);
1721       break;
1722
1723     default:
1724       mapfile->print_input_section(this->u2_.object, this->shndx_);
1725       break;
1726     }
1727 }
1728
1729 // Output_section methods.
1730
1731 // Construct an Output_section.  NAME will point into a Stringpool.
1732
1733 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1734                                elfcpp::Elf_Xword flags)
1735   : name_(name),
1736     addralign_(0),
1737     entsize_(0),
1738     load_address_(0),
1739     link_section_(NULL),
1740     link_(0),
1741     info_section_(NULL),
1742     info_symndx_(NULL),
1743     info_(0),
1744     type_(type),
1745     flags_(flags),
1746     out_shndx_(-1U),
1747     symtab_index_(0),
1748     dynsym_index_(0),
1749     input_sections_(),
1750     first_input_offset_(0),
1751     fills_(),
1752     postprocessing_buffer_(NULL),
1753     needs_symtab_index_(false),
1754     needs_dynsym_index_(false),
1755     should_link_to_symtab_(false),
1756     should_link_to_dynsym_(false),
1757     after_input_sections_(false),
1758     requires_postprocessing_(false),
1759     found_in_sections_clause_(false),
1760     has_load_address_(false),
1761     info_uses_section_index_(false),
1762     may_sort_attached_input_sections_(false),
1763     must_sort_attached_input_sections_(false),
1764     attached_input_sections_are_sorted_(false),
1765     is_relro_(false),
1766     is_relro_local_(false),
1767     is_small_section_(false),
1768     is_large_section_(false),
1769     tls_offset_(0)
1770 {
1771   // An unallocated section has no address.  Forcing this means that
1772   // we don't need special treatment for symbols defined in debug
1773   // sections.
1774   if ((flags & elfcpp::SHF_ALLOC) == 0)
1775     this->set_address(0);
1776 }
1777
1778 Output_section::~Output_section()
1779 {
1780 }
1781
1782 // Set the entry size.
1783
1784 void
1785 Output_section::set_entsize(uint64_t v)
1786 {
1787   if (this->entsize_ == 0)
1788     this->entsize_ = v;
1789   else
1790     gold_assert(this->entsize_ == v);
1791 }
1792
1793 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1794 // OBJECT, to the Output_section.  RELOC_SHNDX is the index of a
1795 // relocation section which applies to this section, or 0 if none, or
1796 // -1U if more than one.  Return the offset of the input section
1797 // within the output section.  Return -1 if the input section will
1798 // receive special handling.  In the normal case we don't always keep
1799 // track of input sections for an Output_section.  Instead, each
1800 // Object keeps track of the Output_section for each of its input
1801 // sections.  However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1802 // track of input sections here; this is used when SECTIONS appears in
1803 // a linker script.
1804
1805 template<int size, bool big_endian>
1806 off_t
1807 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1808                                   unsigned int shndx,
1809                                   const char* secname,
1810                                   const elfcpp::Shdr<size, big_endian>& shdr,
1811                                   unsigned int reloc_shndx,
1812                                   bool have_sections_script)
1813 {
1814   elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1815   if ((addralign & (addralign - 1)) != 0)
1816     {
1817       object->error(_("invalid alignment %lu for section \"%s\""),
1818                     static_cast<unsigned long>(addralign), secname);
1819       addralign = 1;
1820     }
1821
1822   if (addralign > this->addralign_)
1823     this->addralign_ = addralign;
1824
1825   typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1826   this->update_flags_for_input_section(sh_flags);
1827
1828   uint64_t entsize = shdr.get_sh_entsize();
1829
1830   // .debug_str is a mergeable string section, but is not always so
1831   // marked by compilers.  Mark manually here so we can optimize.
1832   if (strcmp(secname, ".debug_str") == 0)
1833     {
1834       sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1835       entsize = 1;
1836     }
1837
1838   // If this is a SHF_MERGE section, we pass all the input sections to
1839   // a Output_data_merge.  We don't try to handle relocations for such
1840   // a section.  We don't try to handle empty merge sections--they
1841   // mess up the mappings, and are useless anyhow.
1842   if ((sh_flags & elfcpp::SHF_MERGE) != 0
1843       && reloc_shndx == 0
1844       && shdr.get_sh_size() > 0)
1845     {
1846       if (this->add_merge_input_section(object, shndx, sh_flags,
1847                                         entsize, addralign))
1848         {
1849           // Tell the relocation routines that they need to call the
1850           // output_offset method to determine the final address.
1851           return -1;
1852         }
1853     }
1854
1855   off_t offset_in_section = this->current_data_size_for_child();
1856   off_t aligned_offset_in_section = align_address(offset_in_section,
1857                                                   addralign);
1858
1859   if (aligned_offset_in_section > offset_in_section
1860       && !have_sections_script
1861       && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1862       && object->target()->has_code_fill())
1863     {
1864       // We need to add some fill data.  Using fill_list_ when
1865       // possible is an optimization, since we will often have fill
1866       // sections without input sections.
1867       off_t fill_len = aligned_offset_in_section - offset_in_section;
1868       if (this->input_sections_.empty())
1869         this->fills_.push_back(Fill(offset_in_section, fill_len));
1870       else
1871         {
1872           // FIXME: When relaxing, the size needs to adjust to
1873           // maintain a constant alignment.
1874           std::string fill_data(object->target()->code_fill(fill_len));
1875           Output_data_const* odc = new Output_data_const(fill_data, 1);
1876           this->input_sections_.push_back(Input_section(odc));
1877         }
1878     }
1879
1880   this->set_current_data_size_for_child(aligned_offset_in_section
1881                                         + shdr.get_sh_size());
1882
1883   // We need to keep track of this section if we are already keeping
1884   // track of sections, or if we are relaxing.  Also, if this is a
1885   // section which requires sorting, or which may require sorting in
1886   // the future, we keep track of the sections.  FIXME: Add test for
1887   // relaxing.
1888   if (have_sections_script
1889       || !this->input_sections_.empty()
1890       || this->may_sort_attached_input_sections()
1891       || this->must_sort_attached_input_sections()
1892       || parameters->options().user_set_Map())
1893     this->input_sections_.push_back(Input_section(object, shndx,
1894                                                   shdr.get_sh_size(),
1895                                                   addralign));
1896
1897   return aligned_offset_in_section;
1898 }
1899
1900 // Add arbitrary data to an output section.
1901
1902 void
1903 Output_section::add_output_section_data(Output_section_data* posd)
1904 {
1905   Input_section inp(posd);
1906   this->add_output_section_data(&inp);
1907
1908   if (posd->is_data_size_valid())
1909     {
1910       off_t offset_in_section = this->current_data_size_for_child();
1911       off_t aligned_offset_in_section = align_address(offset_in_section,
1912                                                       posd->addralign());
1913       this->set_current_data_size_for_child(aligned_offset_in_section
1914                                             + posd->data_size());
1915     }
1916 }
1917
1918 // Add arbitrary data to an output section by Input_section.
1919
1920 void
1921 Output_section::add_output_section_data(Input_section* inp)
1922 {
1923   if (this->input_sections_.empty())
1924     this->first_input_offset_ = this->current_data_size_for_child();
1925
1926   this->input_sections_.push_back(*inp);
1927
1928   uint64_t addralign = inp->addralign();
1929   if (addralign > this->addralign_)
1930     this->addralign_ = addralign;
1931
1932   inp->set_output_section(this);
1933 }
1934
1935 // Add a merge section to an output section.
1936
1937 void
1938 Output_section::add_output_merge_section(Output_section_data* posd,
1939                                          bool is_string, uint64_t entsize)
1940 {
1941   Input_section inp(posd, is_string, entsize);
1942   this->add_output_section_data(&inp);
1943 }
1944
1945 // Add an input section to a SHF_MERGE section.
1946
1947 bool
1948 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1949                                         uint64_t flags, uint64_t entsize,
1950                                         uint64_t addralign)
1951 {
1952   bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1953
1954   // We only merge strings if the alignment is not more than the
1955   // character size.  This could be handled, but it's unusual.
1956   if (is_string && addralign > entsize)
1957     return false;
1958
1959   Input_section_list::iterator p;
1960   for (p = this->input_sections_.begin();
1961        p != this->input_sections_.end();
1962        ++p)
1963     if (p->is_merge_section(is_string, entsize, addralign))
1964       {
1965         p->add_input_section(object, shndx);
1966         return true;
1967       }
1968
1969   // We handle the actual constant merging in Output_merge_data or
1970   // Output_merge_string_data.
1971   Output_section_data* posd;
1972   if (!is_string)
1973     posd = new Output_merge_data(entsize, addralign);
1974   else
1975     {
1976       switch (entsize)
1977         {
1978         case 1:
1979           posd = new Output_merge_string<char>(addralign);
1980           break;
1981         case 2:
1982           posd = new Output_merge_string<uint16_t>(addralign);
1983           break;
1984         case 4:
1985           posd = new Output_merge_string<uint32_t>(addralign);
1986           break;
1987         default:
1988           return false;
1989         }
1990     }
1991
1992   this->add_output_merge_section(posd, is_string, entsize);
1993   posd->add_input_section(object, shndx);
1994
1995   return true;
1996 }
1997
1998 // Given an address OFFSET relative to the start of input section
1999 // SHNDX in OBJECT, return whether this address is being included in
2000 // the final link.  This should only be called if SHNDX in OBJECT has
2001 // a special mapping.
2002
2003 bool
2004 Output_section::is_input_address_mapped(const Relobj* object,
2005                                         unsigned int shndx,
2006                                         off_t offset) const
2007 {
2008   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2009        p != this->input_sections_.end();
2010        ++p)
2011     {
2012       section_offset_type output_offset;
2013       if (p->output_offset(object, shndx, offset, &output_offset))
2014         return output_offset != -1;
2015     }
2016
2017   // By default we assume that the address is mapped.  This should
2018   // only be called after we have passed all sections to Layout.  At
2019   // that point we should know what we are discarding.
2020   return true;
2021 }
2022
2023 // Given an address OFFSET relative to the start of input section
2024 // SHNDX in object OBJECT, return the output offset relative to the
2025 // start of the input section in the output section.  This should only
2026 // be called if SHNDX in OBJECT has a special mapping.
2027
2028 section_offset_type
2029 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2030                               section_offset_type offset) const
2031 {
2032   // This can only be called meaningfully when layout is complete.
2033   gold_assert(Output_data::is_layout_complete());
2034
2035   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2036        p != this->input_sections_.end();
2037        ++p)
2038     {
2039       section_offset_type output_offset;
2040       if (p->output_offset(object, shndx, offset, &output_offset))
2041         return output_offset;
2042     }
2043   gold_unreachable();
2044 }
2045
2046 // Return the output virtual address of OFFSET relative to the start
2047 // of input section SHNDX in object OBJECT.
2048
2049 uint64_t
2050 Output_section::output_address(const Relobj* object, unsigned int shndx,
2051                                off_t offset) const
2052 {
2053   uint64_t addr = this->address() + this->first_input_offset_;
2054   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2055        p != this->input_sections_.end();
2056        ++p)
2057     {
2058       addr = align_address(addr, p->addralign());
2059       section_offset_type output_offset;
2060       if (p->output_offset(object, shndx, offset, &output_offset))
2061         {
2062           if (output_offset == -1)
2063             return -1ULL;
2064           return addr + output_offset;
2065         }
2066       addr += p->data_size();
2067     }
2068
2069   // If we get here, it means that we don't know the mapping for this
2070   // input section.  This might happen in principle if
2071   // add_input_section were called before add_output_section_data.
2072   // But it should never actually happen.
2073
2074   gold_unreachable();
2075 }
2076
2077 // Find the output address of the start of the merged section for
2078 // input section SHNDX in object OBJECT.
2079
2080 bool
2081 Output_section::find_starting_output_address(const Relobj* object,
2082                                              unsigned int shndx,
2083                                              uint64_t* paddr) const
2084 {
2085   uint64_t addr = this->address() + this->first_input_offset_;
2086   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2087        p != this->input_sections_.end();
2088        ++p)
2089     {
2090       addr = align_address(addr, p->addralign());
2091
2092       // It would be nice if we could use the existing output_offset
2093       // method to get the output offset of input offset 0.
2094       // Unfortunately we don't know for sure that input offset 0 is
2095       // mapped at all.
2096       if (p->is_merge_section_for(object, shndx))
2097         {
2098           *paddr = addr;
2099           return true;
2100         }
2101
2102       addr += p->data_size();
2103     }
2104
2105   // We couldn't find a merge output section for this input section.
2106   return false;
2107 }
2108
2109 // Set the data size of an Output_section.  This is where we handle
2110 // setting the addresses of any Output_section_data objects.
2111
2112 void
2113 Output_section::set_final_data_size()
2114 {
2115   if (this->input_sections_.empty())
2116     {
2117       this->set_data_size(this->current_data_size_for_child());
2118       return;
2119     }
2120
2121   if (this->must_sort_attached_input_sections())
2122     this->sort_attached_input_sections();
2123
2124   uint64_t address = this->address();
2125   off_t startoff = this->offset();
2126   off_t off = startoff + this->first_input_offset_;
2127   for (Input_section_list::iterator p = this->input_sections_.begin();
2128        p != this->input_sections_.end();
2129        ++p)
2130     {
2131       off = align_address(off, p->addralign());
2132       p->set_address_and_file_offset(address + (off - startoff), off,
2133                                      startoff);
2134       off += p->data_size();
2135     }
2136
2137   this->set_data_size(off - startoff);
2138 }
2139
2140 // Reset the address and file offset.
2141
2142 void
2143 Output_section::do_reset_address_and_file_offset()
2144 {
2145   for (Input_section_list::iterator p = this->input_sections_.begin();
2146        p != this->input_sections_.end();
2147        ++p)
2148     p->reset_address_and_file_offset();
2149 }
2150
2151 // Set the TLS offset.  Called only for SHT_TLS sections.
2152
2153 void
2154 Output_section::do_set_tls_offset(uint64_t tls_base)
2155 {
2156   this->tls_offset_ = this->address() - tls_base;
2157 }
2158
2159 // In a few cases we need to sort the input sections attached to an
2160 // output section.  This is used to implement the type of constructor
2161 // priority ordering implemented by the GNU linker, in which the
2162 // priority becomes part of the section name and the sections are
2163 // sorted by name.  We only do this for an output section if we see an
2164 // attached input section matching ".ctor.*", ".dtor.*",
2165 // ".init_array.*" or ".fini_array.*".
2166
2167 class Output_section::Input_section_sort_entry
2168 {
2169  public:
2170   Input_section_sort_entry()
2171     : input_section_(), index_(-1U), section_has_name_(false),
2172       section_name_()
2173   { }
2174
2175   Input_section_sort_entry(const Input_section& input_section,
2176                            unsigned int index)
2177     : input_section_(input_section), index_(index),
2178       section_has_name_(input_section.is_input_section())
2179   {
2180     if (this->section_has_name_)
2181       {
2182         // This is only called single-threaded from Layout::finalize,
2183         // so it is OK to lock.  Unfortunately we have no way to pass
2184         // in a Task token.
2185         const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2186         Object* obj = input_section.relobj();
2187         Task_lock_obj<Object> tl(dummy_task, obj);
2188
2189         // This is a slow operation, which should be cached in
2190         // Layout::layout if this becomes a speed problem.
2191         this->section_name_ = obj->section_name(input_section.shndx());
2192       }
2193   }
2194
2195   // Return the Input_section.
2196   const Input_section&
2197   input_section() const
2198   {
2199     gold_assert(this->index_ != -1U);
2200     return this->input_section_;
2201   }
2202
2203   // The index of this entry in the original list.  This is used to
2204   // make the sort stable.
2205   unsigned int
2206   index() const
2207   {
2208     gold_assert(this->index_ != -1U);
2209     return this->index_;
2210   }
2211
2212   // Whether there is a section name.
2213   bool
2214   section_has_name() const
2215   { return this->section_has_name_; }
2216
2217   // The section name.
2218   const std::string&
2219   section_name() const
2220   {
2221     gold_assert(this->section_has_name_);
2222     return this->section_name_;
2223   }
2224
2225   // Return true if the section name has a priority.  This is assumed
2226   // to be true if it has a dot after the initial dot.
2227   bool
2228   has_priority() const
2229   {
2230     gold_assert(this->section_has_name_);
2231     return this->section_name_.find('.', 1);
2232   }
2233
2234   // Return true if this an input file whose base name matches
2235   // FILE_NAME.  The base name must have an extension of ".o", and
2236   // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2237   // This is to match crtbegin.o as well as crtbeginS.o without
2238   // getting confused by other possibilities.  Overall matching the
2239   // file name this way is a dreadful hack, but the GNU linker does it
2240   // in order to better support gcc, and we need to be compatible.
2241   bool
2242   match_file_name(const char* match_file_name) const
2243   {
2244     const std::string& file_name(this->input_section_.relobj()->name());
2245     const char* base_name = lbasename(file_name.c_str());
2246     size_t match_len = strlen(match_file_name);
2247     if (strncmp(base_name, match_file_name, match_len) != 0)
2248       return false;
2249     size_t base_len = strlen(base_name);
2250     if (base_len != match_len + 2 && base_len != match_len + 3)
2251       return false;
2252     return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2253   }
2254
2255  private:
2256   // The Input_section we are sorting.
2257   Input_section input_section_;
2258   // The index of this Input_section in the original list.
2259   unsigned int index_;
2260   // Whether this Input_section has a section name--it won't if this
2261   // is some random Output_section_data.
2262   bool section_has_name_;
2263   // The section name if there is one.
2264   std::string section_name_;
2265 };
2266
2267 // Return true if S1 should come before S2 in the output section.
2268
2269 bool
2270 Output_section::Input_section_sort_compare::operator()(
2271     const Output_section::Input_section_sort_entry& s1,
2272     const Output_section::Input_section_sort_entry& s2) const
2273 {
2274   // crtbegin.o must come first.
2275   bool s1_begin = s1.match_file_name("crtbegin");
2276   bool s2_begin = s2.match_file_name("crtbegin");
2277   if (s1_begin || s2_begin)
2278     {
2279       if (!s1_begin)
2280         return false;
2281       if (!s2_begin)
2282         return true;
2283       return s1.index() < s2.index();
2284     }
2285
2286   // crtend.o must come last.
2287   bool s1_end = s1.match_file_name("crtend");
2288   bool s2_end = s2.match_file_name("crtend");
2289   if (s1_end || s2_end)
2290     {
2291       if (!s1_end)
2292         return true;
2293       if (!s2_end)
2294         return false;
2295       return s1.index() < s2.index();
2296     }
2297
2298   // We sort all the sections with no names to the end.
2299   if (!s1.section_has_name() || !s2.section_has_name())
2300     {
2301       if (s1.section_has_name())
2302         return true;
2303       if (s2.section_has_name())
2304         return false;
2305       return s1.index() < s2.index();
2306     }
2307
2308   // A section with a priority follows a section without a priority.
2309   // The GNU linker does this for all but .init_array sections; until
2310   // further notice we'll assume that that is an mistake.
2311   bool s1_has_priority = s1.has_priority();
2312   bool s2_has_priority = s2.has_priority();
2313   if (s1_has_priority && !s2_has_priority)
2314     return false;
2315   if (!s1_has_priority && s2_has_priority)
2316     return true;
2317
2318   // Otherwise we sort by name.
2319   int compare = s1.section_name().compare(s2.section_name());
2320   if (compare != 0)
2321     return compare < 0;
2322
2323   // Otherwise we keep the input order.
2324   return s1.index() < s2.index();
2325 }
2326
2327 // Sort the input sections attached to an output section.
2328
2329 void
2330 Output_section::sort_attached_input_sections()
2331 {
2332   if (this->attached_input_sections_are_sorted_)
2333     return;
2334
2335   // The only thing we know about an input section is the object and
2336   // the section index.  We need the section name.  Recomputing this
2337   // is slow but this is an unusual case.  If this becomes a speed
2338   // problem we can cache the names as required in Layout::layout.
2339
2340   // We start by building a larger vector holding a copy of each
2341   // Input_section, plus its current index in the list and its name.
2342   std::vector<Input_section_sort_entry> sort_list;
2343
2344   unsigned int i = 0;
2345   for (Input_section_list::iterator p = this->input_sections_.begin();
2346        p != this->input_sections_.end();
2347        ++p, ++i)
2348     sort_list.push_back(Input_section_sort_entry(*p, i));
2349
2350   // Sort the input sections.
2351   std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2352
2353   // Copy the sorted input sections back to our list.
2354   this->input_sections_.clear();
2355   for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2356        p != sort_list.end();
2357        ++p)
2358     this->input_sections_.push_back(p->input_section());
2359
2360   // Remember that we sorted the input sections, since we might get
2361   // called again.
2362   this->attached_input_sections_are_sorted_ = true;
2363 }
2364
2365 // Write the section header to *OSHDR.
2366
2367 template<int size, bool big_endian>
2368 void
2369 Output_section::write_header(const Layout* layout,
2370                              const Stringpool* secnamepool,
2371                              elfcpp::Shdr_write<size, big_endian>* oshdr) const
2372 {
2373   oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2374   oshdr->put_sh_type(this->type_);
2375
2376   elfcpp::Elf_Xword flags = this->flags_;
2377   if (this->info_section_ != NULL && this->info_uses_section_index_)
2378     flags |= elfcpp::SHF_INFO_LINK;
2379   oshdr->put_sh_flags(flags);
2380
2381   oshdr->put_sh_addr(this->address());
2382   oshdr->put_sh_offset(this->offset());
2383   oshdr->put_sh_size(this->data_size());
2384   if (this->link_section_ != NULL)
2385     oshdr->put_sh_link(this->link_section_->out_shndx());
2386   else if (this->should_link_to_symtab_)
2387     oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2388   else if (this->should_link_to_dynsym_)
2389     oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2390   else
2391     oshdr->put_sh_link(this->link_);
2392
2393   elfcpp::Elf_Word info;
2394   if (this->info_section_ != NULL)
2395     {
2396       if (this->info_uses_section_index_)
2397         info = this->info_section_->out_shndx();
2398       else
2399         info = this->info_section_->symtab_index();
2400     }
2401   else if (this->info_symndx_ != NULL)
2402     info = this->info_symndx_->symtab_index();
2403   else
2404     info = this->info_;
2405   oshdr->put_sh_info(info);
2406
2407   oshdr->put_sh_addralign(this->addralign_);
2408   oshdr->put_sh_entsize(this->entsize_);
2409 }
2410
2411 // Write out the data.  For input sections the data is written out by
2412 // Object::relocate, but we have to handle Output_section_data objects
2413 // here.
2414
2415 void
2416 Output_section::do_write(Output_file* of)
2417 {
2418   gold_assert(!this->requires_postprocessing());
2419
2420   off_t output_section_file_offset = this->offset();
2421   for (Fill_list::iterator p = this->fills_.begin();
2422        p != this->fills_.end();
2423        ++p)
2424     {
2425       std::string fill_data(parameters->target().code_fill(p->length()));
2426       of->write(output_section_file_offset + p->section_offset(),
2427                 fill_data.data(), fill_data.size());
2428     }
2429
2430   for (Input_section_list::iterator p = this->input_sections_.begin();
2431        p != this->input_sections_.end();
2432        ++p)
2433     p->write(of);
2434 }
2435
2436 // If a section requires postprocessing, create the buffer to use.
2437
2438 void
2439 Output_section::create_postprocessing_buffer()
2440 {
2441   gold_assert(this->requires_postprocessing());
2442
2443   if (this->postprocessing_buffer_ != NULL)
2444     return;
2445
2446   if (!this->input_sections_.empty())
2447     {
2448       off_t off = this->first_input_offset_;
2449       for (Input_section_list::iterator p = this->input_sections_.begin();
2450            p != this->input_sections_.end();
2451            ++p)
2452         {
2453           off = align_address(off, p->addralign());
2454           p->finalize_data_size();
2455           off += p->data_size();
2456         }
2457       this->set_current_data_size_for_child(off);
2458     }
2459
2460   off_t buffer_size = this->current_data_size_for_child();
2461   this->postprocessing_buffer_ = new unsigned char[buffer_size];
2462 }
2463
2464 // Write all the data of an Output_section into the postprocessing
2465 // buffer.  This is used for sections which require postprocessing,
2466 // such as compression.  Input sections are handled by
2467 // Object::Relocate.
2468
2469 void
2470 Output_section::write_to_postprocessing_buffer()
2471 {
2472   gold_assert(this->requires_postprocessing());
2473
2474   unsigned char* buffer = this->postprocessing_buffer();
2475   for (Fill_list::iterator p = this->fills_.begin();
2476        p != this->fills_.end();
2477        ++p)
2478     {
2479       std::string fill_data(parameters->target().code_fill(p->length()));
2480       memcpy(buffer + p->section_offset(), fill_data.data(),
2481              fill_data.size());
2482     }
2483
2484   off_t off = this->first_input_offset_;
2485   for (Input_section_list::iterator p = this->input_sections_.begin();
2486        p != this->input_sections_.end();
2487        ++p)
2488     {
2489       off = align_address(off, p->addralign());
2490       p->write_to_buffer(buffer + off);
2491       off += p->data_size();
2492     }
2493 }
2494
2495 // Get the input sections for linker script processing.  We leave
2496 // behind the Output_section_data entries.  Note that this may be
2497 // slightly incorrect for merge sections.  We will leave them behind,
2498 // but it is possible that the script says that they should follow
2499 // some other input sections, as in:
2500 //    .rodata { *(.rodata) *(.rodata.cst*) }
2501 // For that matter, we don't handle this correctly:
2502 //    .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2503 // With luck this will never matter.
2504
2505 uint64_t
2506 Output_section::get_input_sections(
2507     uint64_t address,
2508     const std::string& fill,
2509     std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2510 {
2511   uint64_t orig_address = address;
2512
2513   address = align_address(address, this->addralign());
2514
2515   Input_section_list remaining;
2516   for (Input_section_list::iterator p = this->input_sections_.begin();
2517        p != this->input_sections_.end();
2518        ++p)
2519     {
2520       if (p->is_input_section())
2521         input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2522       else
2523         {
2524           uint64_t aligned_address = align_address(address, p->addralign());
2525           if (aligned_address != address && !fill.empty())
2526             {
2527               section_size_type length =
2528                 convert_to_section_size_type(aligned_address - address);
2529               std::string this_fill;
2530               this_fill.reserve(length);
2531               while (this_fill.length() + fill.length() <= length)
2532                 this_fill += fill;
2533               if (this_fill.length() < length)
2534                 this_fill.append(fill, 0, length - this_fill.length());
2535
2536               Output_section_data* posd = new Output_data_const(this_fill, 0);
2537               remaining.push_back(Input_section(posd));
2538             }
2539           address = aligned_address;
2540
2541           remaining.push_back(*p);
2542
2543           p->finalize_data_size();
2544           address += p->data_size();
2545         }
2546     }
2547
2548   this->input_sections_.swap(remaining);
2549   this->first_input_offset_ = 0;
2550
2551   uint64_t data_size = address - orig_address;
2552   this->set_current_data_size_for_child(data_size);
2553   return data_size;
2554 }
2555
2556 // Add an input section from a script.
2557
2558 void
2559 Output_section::add_input_section_for_script(Relobj* object,
2560                                              unsigned int shndx,
2561                                              off_t data_size,
2562                                              uint64_t addralign)
2563 {
2564   if (addralign > this->addralign_)
2565     this->addralign_ = addralign;
2566
2567   off_t offset_in_section = this->current_data_size_for_child();
2568   off_t aligned_offset_in_section = align_address(offset_in_section,
2569                                                   addralign);
2570
2571   this->set_current_data_size_for_child(aligned_offset_in_section
2572                                         + data_size);
2573
2574   this->input_sections_.push_back(Input_section(object, shndx,
2575                                                 data_size, addralign));
2576 }
2577
2578 // Print to the map file.
2579
2580 void
2581 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
2582 {
2583   mapfile->print_output_section(this);
2584
2585   for (Input_section_list::const_iterator p = this->input_sections_.begin();
2586        p != this->input_sections_.end();
2587        ++p)
2588     p->print_to_mapfile(mapfile);
2589 }
2590
2591 // Print stats for merge sections to stderr.
2592
2593 void
2594 Output_section::print_merge_stats()
2595 {
2596   Input_section_list::iterator p;
2597   for (p = this->input_sections_.begin();
2598        p != this->input_sections_.end();
2599        ++p)
2600     p->print_merge_stats(this->name_);
2601 }
2602
2603 // Output segment methods.
2604
2605 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2606   : output_data_(),
2607     output_bss_(),
2608     vaddr_(0),
2609     paddr_(0),
2610     memsz_(0),
2611     max_align_(0),
2612     min_p_align_(0),
2613     offset_(0),
2614     filesz_(0),
2615     type_(type),
2616     flags_(flags),
2617     is_max_align_known_(false),
2618     are_addresses_set_(false),
2619     is_large_data_segment_(false)
2620 {
2621 }
2622
2623 // Add an Output_section to an Output_segment.
2624
2625 void
2626 Output_segment::add_output_section(Output_section* os,
2627                                    elfcpp::Elf_Word seg_flags)
2628 {
2629   gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
2630   gold_assert(!this->is_max_align_known_);
2631   gold_assert(os->is_large_data_section() == this->is_large_data_segment());
2632
2633   // Update the segment flags.
2634   this->flags_ |= seg_flags;
2635
2636   Output_segment::Output_data_list* pdl;
2637   if (os->type() == elfcpp::SHT_NOBITS)
2638     pdl = &this->output_bss_;
2639   else
2640     pdl = &this->output_data_;
2641
2642   // So that PT_NOTE segments will work correctly, we need to ensure
2643   // that all SHT_NOTE sections are adjacent.  This will normally
2644   // happen automatically, because all the SHT_NOTE input sections
2645   // will wind up in the same output section.  However, it is possible
2646   // for multiple SHT_NOTE input sections to have different section
2647   // flags, and thus be in different output sections, but for the
2648   // different section flags to map into the same segment flags and
2649   // thus the same output segment.
2650
2651   // Note that while there may be many input sections in an output
2652   // section, there are normally only a few output sections in an
2653   // output segment.  This loop is expected to be fast.
2654
2655   if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
2656     {
2657       Output_segment::Output_data_list::iterator p = pdl->end();
2658       do
2659         {
2660           --p;
2661           if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2662             {
2663               ++p;
2664               pdl->insert(p, os);
2665               return;
2666             }
2667         }
2668       while (p != pdl->begin());
2669     }
2670
2671   // Similarly, so that PT_TLS segments will work, we need to group
2672   // SHF_TLS sections.  An SHF_TLS/SHT_NOBITS section is a special
2673   // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2674   // SHF_TLS/SHT_PROGBITS sections.  This lets us set up PT_TLS
2675   // correctly.  SHF_TLS sections get added to both a PT_LOAD segment
2676   // and the PT_TLS segment -- we do this grouping only for the
2677   // PT_LOAD segment.
2678   if (this->type_ != elfcpp::PT_TLS
2679       && (os->flags() & elfcpp::SHF_TLS) != 0)
2680     {
2681       pdl = &this->output_data_;
2682       bool nobits = os->type() == elfcpp::SHT_NOBITS;
2683       bool sawtls = false;
2684       Output_segment::Output_data_list::iterator p = pdl->end();
2685       do
2686         {
2687           --p;
2688           bool insert;
2689           if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2690             {
2691               sawtls = true;
2692               // Put a NOBITS section after the first TLS section.
2693               // Put a PROGBITS section after the first TLS/PROGBITS
2694               // section.
2695               insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2696             }
2697           else
2698             {
2699               // If we've gone past the TLS sections, but we've seen a
2700               // TLS section, then we need to insert this section now.
2701               insert = sawtls;
2702             }
2703
2704           if (insert)
2705             {
2706               ++p;
2707               pdl->insert(p, os);
2708               return;
2709             }
2710         }
2711       while (p != pdl->begin());
2712
2713       // There are no TLS sections yet; put this one at the requested
2714       // location in the section list.
2715     }
2716
2717   // For the PT_GNU_RELRO segment, we need to group relro sections,
2718   // and we need to put them before any non-relro sections.  Also,
2719   // relro local sections go before relro non-local sections.
2720   if (parameters->options().relro() && os->is_relro())
2721     {
2722       gold_assert(pdl == &this->output_data_);
2723       Output_segment::Output_data_list::iterator p;
2724       for (p = pdl->begin(); p != pdl->end(); ++p)
2725         {
2726           if (!(*p)->is_section())
2727             break;
2728
2729           Output_section* pos = (*p)->output_section();
2730           if (!pos->is_relro()
2731               || (os->is_relro_local() && !pos->is_relro_local()))
2732             break;
2733         }
2734
2735       pdl->insert(p, os);
2736       return;
2737     }
2738
2739   // Small data sections go at the end of the list of data sections.
2740   // If OS is not small, and there are small sections, we have to
2741   // insert it before the first small section.
2742   if (os->type() != elfcpp::SHT_NOBITS
2743       && !os->is_small_section()
2744       && !pdl->empty()
2745       && pdl->back()->is_section()
2746       && pdl->back()->output_section()->is_small_section())
2747     {
2748       for (Output_segment::Output_data_list::iterator p = pdl->begin();
2749            p != pdl->end();
2750            ++p)
2751         {
2752           if ((*p)->is_section()
2753               && (*p)->output_section()->is_small_section())
2754             {
2755               pdl->insert(p, os);
2756               return;
2757             }
2758         }
2759       gold_unreachable();
2760     }
2761
2762   // A small BSS section goes at the start of the BSS sections, after
2763   // other small BSS sections.
2764   if (os->type() == elfcpp::SHT_NOBITS && os->is_small_section())
2765     {
2766       for (Output_segment::Output_data_list::iterator p = pdl->begin();
2767            p != pdl->end();
2768            ++p)
2769         {
2770           if (!(*p)->is_section()
2771               || !(*p)->output_section()->is_small_section())
2772             {
2773               pdl->insert(p, os);
2774               return;
2775             }
2776         }
2777     }
2778
2779   // A large BSS section goes at the end of the BSS sections, which
2780   // means that one that is not large must come before the first large
2781   // one.
2782   if (os->type() == elfcpp::SHT_NOBITS
2783       && !os->is_large_section()
2784       && !pdl->empty()
2785       && pdl->back()->is_section()
2786       && pdl->back()->output_section()->is_large_section())
2787     {
2788       for (Output_segment::Output_data_list::iterator p = pdl->begin();
2789            p != pdl->end();
2790            ++p)
2791         {
2792           if ((*p)->is_section()
2793               && (*p)->output_section()->is_large_section())
2794             {
2795               pdl->insert(p, os);
2796               return;
2797             }
2798         }
2799       gold_unreachable();
2800     }
2801
2802   pdl->push_back(os);
2803 }
2804
2805 // Remove an Output_section from this segment.  It is an error if it
2806 // is not present.
2807
2808 void
2809 Output_segment::remove_output_section(Output_section* os)
2810 {
2811   // We only need this for SHT_PROGBITS.
2812   gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2813   for (Output_data_list::iterator p = this->output_data_.begin();
2814        p != this->output_data_.end();
2815        ++p)
2816    {
2817      if (*p == os)
2818        {
2819          this->output_data_.erase(p);
2820          return;
2821        }
2822    }
2823   gold_unreachable();
2824 }
2825
2826 // Add an Output_data (which is not an Output_section) to the start of
2827 // a segment.
2828
2829 void
2830 Output_segment::add_initial_output_data(Output_data* od)
2831 {
2832   gold_assert(!this->is_max_align_known_);
2833   this->output_data_.push_front(od);
2834 }
2835
2836 // Return whether the first data section is a relro section.
2837
2838 bool
2839 Output_segment::is_first_section_relro() const
2840 {
2841   return (!this->output_data_.empty()
2842           && this->output_data_.front()->is_section()
2843           && this->output_data_.front()->output_section()->is_relro());
2844 }
2845
2846 // Return the maximum alignment of the Output_data in Output_segment.
2847
2848 uint64_t
2849 Output_segment::maximum_alignment()
2850 {
2851   if (!this->is_max_align_known_)
2852     {
2853       uint64_t addralign;
2854
2855       addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2856       if (addralign > this->max_align_)
2857         this->max_align_ = addralign;
2858
2859       addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2860       if (addralign > this->max_align_)
2861         this->max_align_ = addralign;
2862
2863       // If -z relro is in effect, and the first section in this
2864       // segment is a relro section, then the segment must be aligned
2865       // to at least the common page size.  This ensures that the
2866       // PT_GNU_RELRO segment will start at a page boundary.
2867       if (this->type_ == elfcpp::PT_LOAD
2868           && parameters->options().relro()
2869           && this->is_first_section_relro())
2870         {
2871           addralign = parameters->target().common_pagesize();
2872           if (addralign > this->max_align_)
2873             this->max_align_ = addralign;
2874         }
2875
2876       this->is_max_align_known_ = true;
2877     }
2878
2879   return this->max_align_;
2880 }
2881
2882 // Return the maximum alignment of a list of Output_data.
2883
2884 uint64_t
2885 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
2886 {
2887   uint64_t ret = 0;
2888   for (Output_data_list::const_iterator p = pdl->begin();
2889        p != pdl->end();
2890        ++p)
2891     {
2892       uint64_t addralign = (*p)->addralign();
2893       if (addralign > ret)
2894         ret = addralign;
2895     }
2896   return ret;
2897 }
2898
2899 // Return the number of dynamic relocs applied to this segment.
2900
2901 unsigned int
2902 Output_segment::dynamic_reloc_count() const
2903 {
2904   return (this->dynamic_reloc_count_list(&this->output_data_)
2905           + this->dynamic_reloc_count_list(&this->output_bss_));
2906 }
2907
2908 // Return the number of dynamic relocs applied to an Output_data_list.
2909
2910 unsigned int
2911 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2912 {
2913   unsigned int count = 0;
2914   for (Output_data_list::const_iterator p = pdl->begin();
2915        p != pdl->end();
2916        ++p)
2917     count += (*p)->dynamic_reloc_count();
2918   return count;
2919 }
2920
2921 // Set the section addresses for an Output_segment.  If RESET is true,
2922 // reset the addresses first.  ADDR is the address and *POFF is the
2923 // file offset.  Set the section indexes starting with *PSHNDX.
2924 // Return the address of the immediately following segment.  Update
2925 // *POFF and *PSHNDX.
2926
2927 uint64_t
2928 Output_segment::set_section_addresses(const Layout* layout, bool reset,
2929                                       uint64_t addr, off_t* poff,
2930                                       unsigned int* pshndx)
2931 {
2932   gold_assert(this->type_ == elfcpp::PT_LOAD);
2933
2934   if (!reset && this->are_addresses_set_)
2935     {
2936       gold_assert(this->paddr_ == addr);
2937       addr = this->vaddr_;
2938     }
2939   else
2940     {
2941       this->vaddr_ = addr;
2942       this->paddr_ = addr;
2943       this->are_addresses_set_ = true;
2944     }
2945
2946   bool in_tls = false;
2947
2948   bool in_relro = (parameters->options().relro()
2949                    && this->is_first_section_relro());
2950
2951   off_t orig_off = *poff;
2952   this->offset_ = orig_off;
2953
2954   addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
2955                                           addr, poff, pshndx, &in_tls,
2956                                           &in_relro);
2957   this->filesz_ = *poff - orig_off;
2958
2959   off_t off = *poff;
2960
2961   uint64_t ret = this->set_section_list_addresses(layout, reset,
2962                                                   &this->output_bss_,
2963                                                   addr, poff, pshndx,
2964                                                   &in_tls, &in_relro);
2965
2966   // If the last section was a TLS section, align upward to the
2967   // alignment of the TLS segment, so that the overall size of the TLS
2968   // segment is aligned.
2969   if (in_tls)
2970     {
2971       uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2972       *poff = align_address(*poff, segment_align);
2973     }
2974
2975   // If all the sections were relro sections, align upward to the
2976   // common page size.
2977   if (in_relro)
2978     {
2979       uint64_t page_align = parameters->target().common_pagesize();
2980       *poff = align_address(*poff, page_align);
2981     }
2982
2983   this->memsz_ = *poff - orig_off;
2984
2985   // Ignore the file offset adjustments made by the BSS Output_data
2986   // objects.
2987   *poff = off;
2988
2989   return ret;
2990 }
2991
2992 // Set the addresses and file offsets in a list of Output_data
2993 // structures.
2994
2995 uint64_t
2996 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
2997                                            Output_data_list* pdl,
2998                                            uint64_t addr, off_t* poff,
2999                                            unsigned int* pshndx,
3000                                            bool* in_tls, bool* in_relro)
3001 {
3002   off_t startoff = *poff;
3003
3004   off_t off = startoff;
3005   for (Output_data_list::iterator p = pdl->begin();
3006        p != pdl->end();
3007        ++p)
3008     {
3009       if (reset)
3010         (*p)->reset_address_and_file_offset();
3011
3012       // When using a linker script the section will most likely
3013       // already have an address.
3014       if (!(*p)->is_address_valid())
3015         {
3016           uint64_t align = (*p)->addralign();
3017
3018           if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3019             {
3020               // Give the first TLS section the alignment of the
3021               // entire TLS segment.  Otherwise the TLS segment as a
3022               // whole may be misaligned.
3023               if (!*in_tls)
3024                 {
3025                   Output_segment* tls_segment = layout->tls_segment();
3026                   gold_assert(tls_segment != NULL);
3027                   uint64_t segment_align = tls_segment->maximum_alignment();
3028                   gold_assert(segment_align >= align);
3029                   align = segment_align;
3030
3031                   *in_tls = true;
3032                 }
3033             }
3034           else
3035             {
3036               // If this is the first section after the TLS segment,
3037               // align it to at least the alignment of the TLS
3038               // segment, so that the size of the overall TLS segment
3039               // is aligned.
3040               if (*in_tls)
3041                 {
3042                   uint64_t segment_align =
3043                       layout->tls_segment()->maximum_alignment();
3044                   if (segment_align > align)
3045                     align = segment_align;
3046
3047                   *in_tls = false;
3048                 }
3049             }
3050
3051           // If this is a non-relro section after a relro section,
3052           // align it to a common page boundary so that the dynamic
3053           // linker has a page to mark as read-only.
3054           if (*in_relro
3055               && (!(*p)->is_section()
3056                   || !(*p)->output_section()->is_relro()))
3057             {
3058               uint64_t page_align = parameters->target().common_pagesize();
3059               if (page_align > align)
3060                 align = page_align;
3061               *in_relro = false;
3062             }
3063
3064           off = align_address(off, align);
3065           (*p)->set_address_and_file_offset(addr + (off - startoff), off);
3066         }
3067       else
3068         {
3069           // The script may have inserted a skip forward, but it
3070           // better not have moved backward.
3071           gold_assert((*p)->address() >= addr + (off - startoff));
3072           off += (*p)->address() - (addr + (off - startoff));
3073           (*p)->set_file_offset(off);
3074           (*p)->finalize_data_size();
3075         }
3076
3077       // We want to ignore the size of a SHF_TLS or SHT_NOBITS
3078       // section.  Such a section does not affect the size of a
3079       // PT_LOAD segment.
3080       if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
3081           || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
3082         off += (*p)->data_size();
3083
3084       if ((*p)->is_section())
3085         {
3086           (*p)->set_out_shndx(*pshndx);
3087           ++*pshndx;
3088         }
3089     }
3090
3091   *poff = off;
3092   return addr + (off - startoff);
3093 }
3094
3095 // For a non-PT_LOAD segment, set the offset from the sections, if
3096 // any.
3097
3098 void
3099 Output_segment::set_offset()
3100 {
3101   gold_assert(this->type_ != elfcpp::PT_LOAD);
3102
3103   gold_assert(!this->are_addresses_set_);
3104
3105   if (this->output_data_.empty() && this->output_bss_.empty())
3106     {
3107       this->vaddr_ = 0;
3108       this->paddr_ = 0;
3109       this->are_addresses_set_ = true;
3110       this->memsz_ = 0;
3111       this->min_p_align_ = 0;
3112       this->offset_ = 0;
3113       this->filesz_ = 0;
3114       return;
3115     }
3116
3117   const Output_data* first;
3118   if (this->output_data_.empty())
3119     first = this->output_bss_.front();
3120   else
3121     first = this->output_data_.front();
3122   this->vaddr_ = first->address();
3123   this->paddr_ = (first->has_load_address()
3124                   ? first->load_address()
3125                   : this->vaddr_);
3126   this->are_addresses_set_ = true;
3127   this->offset_ = first->offset();
3128
3129   if (this->output_data_.empty())
3130     this->filesz_ = 0;
3131   else
3132     {
3133       const Output_data* last_data = this->output_data_.back();
3134       this->filesz_ = (last_data->address()
3135                        + last_data->data_size()
3136                        - this->vaddr_);
3137     }
3138
3139   const Output_data* last;
3140   if (this->output_bss_.empty())
3141     last = this->output_data_.back();
3142   else
3143     last = this->output_bss_.back();
3144   this->memsz_ = (last->address()
3145                   + last->data_size()
3146                   - this->vaddr_);
3147
3148   // If this is a TLS segment, align the memory size.  The code in
3149   // set_section_list ensures that the section after the TLS segment
3150   // is aligned to give us room.
3151   if (this->type_ == elfcpp::PT_TLS)
3152     {
3153       uint64_t segment_align = this->maximum_alignment();
3154       gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3155       this->memsz_ = align_address(this->memsz_, segment_align);
3156     }
3157
3158   // If this is a RELRO segment, align the memory size.  The code in
3159   // set_section_list ensures that the section after the RELRO segment
3160   // is aligned to give us room.
3161   if (this->type_ == elfcpp::PT_GNU_RELRO)
3162     {
3163       uint64_t page_align = parameters->target().common_pagesize();
3164       gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3165       this->memsz_ = align_address(this->memsz_, page_align);
3166     }
3167 }
3168
3169 // Set the TLS offsets of the sections in the PT_TLS segment.
3170
3171 void
3172 Output_segment::set_tls_offsets()
3173 {
3174   gold_assert(this->type_ == elfcpp::PT_TLS);
3175
3176   for (Output_data_list::iterator p = this->output_data_.begin();
3177        p != this->output_data_.end();
3178        ++p)
3179     (*p)->set_tls_offset(this->vaddr_);
3180
3181   for (Output_data_list::iterator p = this->output_bss_.begin();
3182        p != this->output_bss_.end();
3183        ++p)
3184     (*p)->set_tls_offset(this->vaddr_);
3185 }
3186
3187 // Return the address of the first section.
3188
3189 uint64_t
3190 Output_segment::first_section_load_address() const
3191 {
3192   for (Output_data_list::const_iterator p = this->output_data_.begin();
3193        p != this->output_data_.end();
3194        ++p)
3195     if ((*p)->is_section())
3196       return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3197
3198   for (Output_data_list::const_iterator p = this->output_bss_.begin();
3199        p != this->output_bss_.end();
3200        ++p)
3201     if ((*p)->is_section())
3202       return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3203
3204   gold_unreachable();
3205 }
3206
3207 // Return the number of Output_sections in an Output_segment.
3208
3209 unsigned int
3210 Output_segment::output_section_count() const
3211 {
3212   return (this->output_section_count_list(&this->output_data_)
3213           + this->output_section_count_list(&this->output_bss_));
3214 }
3215
3216 // Return the number of Output_sections in an Output_data_list.
3217
3218 unsigned int
3219 Output_segment::output_section_count_list(const Output_data_list* pdl) const
3220 {
3221   unsigned int count = 0;
3222   for (Output_data_list::const_iterator p = pdl->begin();
3223        p != pdl->end();
3224        ++p)
3225     {
3226       if ((*p)->is_section())
3227         ++count;
3228     }
3229   return count;
3230 }
3231
3232 // Return the section attached to the list segment with the lowest
3233 // load address.  This is used when handling a PHDRS clause in a
3234 // linker script.
3235
3236 Output_section*
3237 Output_segment::section_with_lowest_load_address() const
3238 {
3239   Output_section* found = NULL;
3240   uint64_t found_lma = 0;
3241   this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3242
3243   Output_section* found_data = found;
3244   this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3245   if (found != found_data && found_data != NULL)
3246     {
3247       gold_error(_("nobits section %s may not precede progbits section %s "
3248                    "in same segment"),
3249                  found->name(), found_data->name());
3250       return NULL;
3251     }
3252
3253   return found;
3254 }
3255
3256 // Look through a list for a section with a lower load address.
3257
3258 void
3259 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3260                                             Output_section** found,
3261                                             uint64_t* found_lma) const
3262 {
3263   for (Output_data_list::const_iterator p = pdl->begin();
3264        p != pdl->end();
3265        ++p)
3266     {
3267       if (!(*p)->is_section())
3268         continue;
3269       Output_section* os = static_cast<Output_section*>(*p);
3270       uint64_t lma = (os->has_load_address()
3271                       ? os->load_address()
3272                       : os->address());
3273       if (*found == NULL || lma < *found_lma)
3274         {
3275           *found = os;
3276           *found_lma = lma;
3277         }
3278     }
3279 }
3280
3281 // Write the segment data into *OPHDR.
3282
3283 template<int size, bool big_endian>
3284 void
3285 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
3286 {
3287   ophdr->put_p_type(this->type_);
3288   ophdr->put_p_offset(this->offset_);
3289   ophdr->put_p_vaddr(this->vaddr_);
3290   ophdr->put_p_paddr(this->paddr_);
3291   ophdr->put_p_filesz(this->filesz_);
3292   ophdr->put_p_memsz(this->memsz_);
3293   ophdr->put_p_flags(this->flags_);
3294   ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
3295 }
3296
3297 // Write the section headers into V.
3298
3299 template<int size, bool big_endian>
3300 unsigned char*
3301 Output_segment::write_section_headers(const Layout* layout,
3302                                       const Stringpool* secnamepool,
3303                                       unsigned char* v,
3304                                       unsigned int *pshndx) const
3305 {
3306   // Every section that is attached to a segment must be attached to a
3307   // PT_LOAD segment, so we only write out section headers for PT_LOAD
3308   // segments.
3309   if (this->type_ != elfcpp::PT_LOAD)
3310     return v;
3311
3312   v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3313                                                          &this->output_data_,
3314                                                          v, pshndx);
3315   v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3316                                                          &this->output_bss_,
3317                                                          v, pshndx);
3318   return v;
3319 }
3320
3321 template<int size, bool big_endian>
3322 unsigned char*
3323 Output_segment::write_section_headers_list(const Layout* layout,
3324                                            const Stringpool* secnamepool,
3325                                            const Output_data_list* pdl,
3326                                            unsigned char* v,
3327                                            unsigned int* pshndx) const
3328 {
3329   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3330   for (Output_data_list::const_iterator p = pdl->begin();
3331        p != pdl->end();
3332        ++p)
3333     {
3334       if ((*p)->is_section())
3335         {
3336           const Output_section* ps = static_cast<const Output_section*>(*p);
3337           gold_assert(*pshndx == ps->out_shndx());
3338           elfcpp::Shdr_write<size, big_endian> oshdr(v);
3339           ps->write_header(layout, secnamepool, &oshdr);
3340           v += shdr_size;
3341           ++*pshndx;
3342         }
3343     }
3344   return v;
3345 }
3346
3347 // Print the output sections to the map file.
3348
3349 void
3350 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3351 {
3352   if (this->type() != elfcpp::PT_LOAD)
3353     return;
3354   this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3355   this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3356 }
3357
3358 // Print an output section list to the map file.
3359
3360 void
3361 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3362                                               const Output_data_list* pdl) const
3363 {
3364   for (Output_data_list::const_iterator p = pdl->begin();
3365        p != pdl->end();
3366        ++p)
3367     (*p)->print_to_mapfile(mapfile);
3368 }
3369
3370 // Output_file methods.
3371
3372 Output_file::Output_file(const char* name)
3373   : name_(name),
3374     o_(-1),
3375     file_size_(0),
3376     base_(NULL),
3377     map_is_anonymous_(false),
3378     is_temporary_(false)
3379 {
3380 }
3381
3382 // Open the output file.
3383
3384 void
3385 Output_file::open(off_t file_size)
3386 {
3387   this->file_size_ = file_size;
3388
3389   // Unlink the file first; otherwise the open() may fail if the file
3390   // is busy (e.g. it's an executable that's currently being executed).
3391   //
3392   // However, the linker may be part of a system where a zero-length
3393   // file is created for it to write to, with tight permissions (gcc
3394   // 2.95 did something like this).  Unlinking the file would work
3395   // around those permission controls, so we only unlink if the file
3396   // has a non-zero size.  We also unlink only regular files to avoid
3397   // trouble with directories/etc.
3398   //
3399   // If we fail, continue; this command is merely a best-effort attempt
3400   // to improve the odds for open().
3401
3402   // We let the name "-" mean "stdout"
3403   if (!this->is_temporary_)
3404     {
3405       if (strcmp(this->name_, "-") == 0)
3406         this->o_ = STDOUT_FILENO;
3407       else
3408         {
3409           struct stat s;
3410           if (::stat(this->name_, &s) == 0 && s.st_size != 0)
3411             unlink_if_ordinary(this->name_);
3412
3413           int mode = parameters->options().relocatable() ? 0666 : 0777;
3414           int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
3415                                   mode);
3416           if (o < 0)
3417             gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3418           this->o_ = o;
3419         }
3420     }
3421
3422   this->map();
3423 }
3424
3425 // Resize the output file.
3426
3427 void
3428 Output_file::resize(off_t file_size)
3429 {
3430   // If the mmap is mapping an anonymous memory buffer, this is easy:
3431   // just mremap to the new size.  If it's mapping to a file, we want
3432   // to unmap to flush to the file, then remap after growing the file.
3433   if (this->map_is_anonymous_)
3434     {
3435       void* base = ::mremap(this->base_, this->file_size_, file_size,
3436                             MREMAP_MAYMOVE);
3437       if (base == MAP_FAILED)
3438         gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3439       this->base_ = static_cast<unsigned char*>(base);
3440       this->file_size_ = file_size;
3441     }
3442   else
3443     {
3444       this->unmap();
3445       this->file_size_ = file_size;
3446       this->map();
3447     }
3448 }
3449
3450 // Map a block of memory which will later be written to the file.
3451 // Return a pointer to the memory.
3452
3453 void*
3454 Output_file::map_anonymous()
3455 {
3456   this->map_is_anonymous_ = true;
3457   return ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3458                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3459 }
3460
3461 // Map the file into memory.
3462
3463 void
3464 Output_file::map()
3465 {
3466   const int o = this->o_;
3467
3468   // If the output file is not a regular file, don't try to mmap it;
3469   // instead, we'll mmap a block of memory (an anonymous buffer), and
3470   // then later write the buffer to the file.
3471   void* base;
3472   struct stat statbuf;
3473   if (o == STDOUT_FILENO || o == STDERR_FILENO
3474       || ::fstat(o, &statbuf) != 0
3475       || !S_ISREG(statbuf.st_mode)
3476       || this->is_temporary_)
3477     base = this->map_anonymous();
3478   else
3479     {
3480       // Ensure that we have disk space available for the file.  If we
3481       // don't do this, it is possible that we will call munmap,
3482       // close, and exit with dirty buffers still in the cache with no
3483       // assigned disk blocks.  If the disk is out of space at that
3484       // point, the output file will wind up incomplete, but we will
3485       // have already exited.  The alternative to fallocate would be
3486       // to use fdatasync, but that would be a more significant
3487       // performance hit.
3488       if (::posix_fallocate(o, 0, this->file_size_) < 0)
3489         gold_fatal(_("%s: %s"), this->name_, strerror(errno));
3490
3491       // Map the file into memory.
3492       this->map_is_anonymous_ = false;
3493       base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3494                     MAP_SHARED, o, 0);
3495
3496       // The mmap call might fail because of file system issues: the
3497       // file system might not support mmap at all, or it might not
3498       // support mmap with PROT_WRITE.  I'm not sure which errno
3499       // values we will see in all cases, so if the mmap fails for any
3500       // reason try for an anonymous map.
3501       if (base == MAP_FAILED)
3502         base = this->map_anonymous();
3503     }
3504   if (base == MAP_FAILED)
3505     gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
3506                this->name_, static_cast<unsigned long>(this->file_size_),
3507                strerror(errno));
3508   this->base_ = static_cast<unsigned char*>(base);
3509 }
3510
3511 // Unmap the file from memory.
3512
3513 void
3514 Output_file::unmap()
3515 {
3516   if (::munmap(this->base_, this->file_size_) < 0)
3517     gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
3518   this->base_ = NULL;
3519 }
3520
3521 // Close the output file.
3522
3523 void
3524 Output_file::close()
3525 {
3526   // If the map isn't file-backed, we need to write it now.
3527   if (this->map_is_anonymous_ && !this->is_temporary_)
3528     {
3529       size_t bytes_to_write = this->file_size_;
3530       size_t offset = 0;
3531       while (bytes_to_write > 0)
3532         {
3533           ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
3534                                           bytes_to_write);
3535           if (bytes_written == 0)
3536             gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
3537           else if (bytes_written < 0)
3538             gold_error(_("%s: write: %s"), this->name_, strerror(errno));
3539           else
3540             {
3541               bytes_to_write -= bytes_written;
3542               offset += bytes_written;
3543             }
3544         }
3545     }
3546   this->unmap();
3547
3548   // We don't close stdout or stderr
3549   if (this->o_ != STDOUT_FILENO
3550       && this->o_ != STDERR_FILENO
3551       && !this->is_temporary_)
3552     if (::close(this->o_) < 0)
3553       gold_error(_("%s: close: %s"), this->name_, strerror(errno));
3554   this->o_ = -1;
3555 }
3556
3557 // Instantiate the templates we need.  We could use the configure
3558 // script to restrict this to only the ones for implemented targets.
3559
3560 #ifdef HAVE_TARGET_32_LITTLE
3561 template
3562 off_t
3563 Output_section::add_input_section<32, false>(
3564     Sized_relobj<32, false>* object,
3565     unsigned int shndx,
3566     const char* secname,
3567     const elfcpp::Shdr<32, false>& shdr,
3568     unsigned int reloc_shndx,
3569     bool have_sections_script);
3570 #endif
3571
3572 #ifdef HAVE_TARGET_32_BIG
3573 template
3574 off_t
3575 Output_section::add_input_section<32, true>(
3576     Sized_relobj<32, true>* object,
3577     unsigned int shndx,
3578     const char* secname,
3579     const elfcpp::Shdr<32, true>& shdr,
3580     unsigned int reloc_shndx,
3581     bool have_sections_script);
3582 #endif
3583
3584 #ifdef HAVE_TARGET_64_LITTLE
3585 template
3586 off_t
3587 Output_section::add_input_section<64, false>(
3588     Sized_relobj<64, false>* object,
3589     unsigned int shndx,
3590     const char* secname,
3591     const elfcpp::Shdr<64, false>& shdr,
3592     unsigned int reloc_shndx,
3593     bool have_sections_script);
3594 #endif
3595
3596 #ifdef HAVE_TARGET_64_BIG
3597 template
3598 off_t
3599 Output_section::add_input_section<64, true>(
3600     Sized_relobj<64, true>* object,
3601     unsigned int shndx,
3602     const char* secname,
3603     const elfcpp::Shdr<64, true>& shdr,
3604     unsigned int reloc_shndx,
3605     bool have_sections_script);
3606 #endif
3607
3608 #ifdef HAVE_TARGET_32_LITTLE
3609 template
3610 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
3611 #endif
3612
3613 #ifdef HAVE_TARGET_32_BIG
3614 template
3615 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
3616 #endif
3617
3618 #ifdef HAVE_TARGET_64_LITTLE
3619 template
3620 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
3621 #endif
3622
3623 #ifdef HAVE_TARGET_64_BIG
3624 template
3625 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
3626 #endif
3627
3628 #ifdef HAVE_TARGET_32_LITTLE
3629 template
3630 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
3631 #endif
3632
3633 #ifdef HAVE_TARGET_32_BIG
3634 template
3635 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
3636 #endif
3637
3638 #ifdef HAVE_TARGET_64_LITTLE
3639 template
3640 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
3641 #endif
3642
3643 #ifdef HAVE_TARGET_64_BIG
3644 template
3645 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
3646 #endif
3647
3648 #ifdef HAVE_TARGET_32_LITTLE
3649 template
3650 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
3651 #endif
3652
3653 #ifdef HAVE_TARGET_32_BIG
3654 template
3655 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
3656 #endif
3657
3658 #ifdef HAVE_TARGET_64_LITTLE
3659 template
3660 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
3661 #endif
3662
3663 #ifdef HAVE_TARGET_64_BIG
3664 template
3665 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
3666 #endif
3667
3668 #ifdef HAVE_TARGET_32_LITTLE
3669 template
3670 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
3671 #endif
3672
3673 #ifdef HAVE_TARGET_32_BIG
3674 template
3675 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
3676 #endif
3677
3678 #ifdef HAVE_TARGET_64_LITTLE
3679 template
3680 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
3681 #endif
3682
3683 #ifdef HAVE_TARGET_64_BIG
3684 template
3685 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
3686 #endif
3687
3688 #ifdef HAVE_TARGET_32_LITTLE
3689 template
3690 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
3691 #endif
3692
3693 #ifdef HAVE_TARGET_32_BIG
3694 template
3695 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
3696 #endif
3697
3698 #ifdef HAVE_TARGET_64_LITTLE
3699 template
3700 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
3701 #endif
3702
3703 #ifdef HAVE_TARGET_64_BIG
3704 template
3705 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
3706 #endif
3707
3708 #ifdef HAVE_TARGET_32_LITTLE
3709 template
3710 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
3711 #endif
3712
3713 #ifdef HAVE_TARGET_32_BIG
3714 template
3715 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
3716 #endif
3717
3718 #ifdef HAVE_TARGET_64_LITTLE
3719 template
3720 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
3721 #endif
3722
3723 #ifdef HAVE_TARGET_64_BIG
3724 template
3725 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
3726 #endif
3727
3728 #ifdef HAVE_TARGET_32_LITTLE
3729 template
3730 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
3731 #endif
3732
3733 #ifdef HAVE_TARGET_32_BIG
3734 template
3735 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
3736 #endif
3737
3738 #ifdef HAVE_TARGET_64_LITTLE
3739 template
3740 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
3741 #endif
3742
3743 #ifdef HAVE_TARGET_64_BIG
3744 template
3745 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
3746 #endif
3747
3748 #ifdef HAVE_TARGET_32_LITTLE
3749 template
3750 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
3751 #endif
3752
3753 #ifdef HAVE_TARGET_32_BIG
3754 template
3755 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
3756 #endif
3757
3758 #ifdef HAVE_TARGET_64_LITTLE
3759 template
3760 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
3761 #endif
3762
3763 #ifdef HAVE_TARGET_64_BIG
3764 template
3765 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
3766 #endif
3767
3768 #ifdef HAVE_TARGET_32_LITTLE
3769 template
3770 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3771 #endif
3772
3773 #ifdef HAVE_TARGET_32_BIG
3774 template
3775 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3776 #endif
3777
3778 #ifdef HAVE_TARGET_64_LITTLE
3779 template
3780 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3781 #endif
3782
3783 #ifdef HAVE_TARGET_64_BIG
3784 template
3785 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3786 #endif
3787
3788 #ifdef HAVE_TARGET_32_LITTLE
3789 template
3790 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3791 #endif
3792
3793 #ifdef HAVE_TARGET_32_BIG
3794 template
3795 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3796 #endif
3797
3798 #ifdef HAVE_TARGET_64_LITTLE
3799 template
3800 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3801 #endif
3802
3803 #ifdef HAVE_TARGET_64_BIG
3804 template
3805 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3806 #endif
3807
3808 #ifdef HAVE_TARGET_32_LITTLE
3809 template
3810 class Output_data_group<32, false>;
3811 #endif
3812
3813 #ifdef HAVE_TARGET_32_BIG
3814 template
3815 class Output_data_group<32, true>;
3816 #endif
3817
3818 #ifdef HAVE_TARGET_64_LITTLE
3819 template
3820 class Output_data_group<64, false>;
3821 #endif
3822
3823 #ifdef HAVE_TARGET_64_BIG
3824 template
3825 class Output_data_group<64, true>;
3826 #endif
3827
3828 #ifdef HAVE_TARGET_32_LITTLE
3829 template
3830 class Output_data_got<32, false>;
3831 #endif
3832
3833 #ifdef HAVE_TARGET_32_BIG
3834 template
3835 class Output_data_got<32, true>;
3836 #endif
3837
3838 #ifdef HAVE_TARGET_64_LITTLE
3839 template
3840 class Output_data_got<64, false>;
3841 #endif
3842
3843 #ifdef HAVE_TARGET_64_BIG
3844 template
3845 class Output_data_got<64, true>;
3846 #endif
3847
3848 } // End namespace gold.