More dynamic object support, initial scripting support.
[platform/upstream/binutils.git] / gold / output.cc
1 // output.cc -- manage the output file for gold
2
3 #include "gold.h"
4
5 #include <cstdlib>
6 #include <cerrno>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/mman.h>
10 #include <algorithm>
11
12 #include "object.h"
13 #include "symtab.h"
14 #include "reloc.h"
15 #include "output.h"
16
17 namespace gold
18 {
19
20 // Output_data methods.
21
22 Output_data::~Output_data()
23 {
24 }
25
26 // Set the address and offset.
27
28 void
29 Output_data::set_address(uint64_t addr, off_t off)
30 {
31   this->address_ = addr;
32   this->offset_ = off;
33
34   // Let the child class know.
35   this->do_set_address(addr, off);
36 }
37
38 // Return the default alignment for a size--32 or 64.
39
40 uint64_t
41 Output_data::default_alignment(int size)
42 {
43   if (size == 32)
44     return 4;
45   else if (size == 64)
46     return 8;
47   else
48     abort();
49 }
50
51 // Output_section_header methods.  This currently assumes that the
52 // segment and section lists are complete at construction time.
53
54 Output_section_headers::Output_section_headers(
55     int size,
56     bool big_endian,
57     const Layout::Segment_list& segment_list,
58     const Layout::Section_list& section_list,
59     const Stringpool* secnamepool)
60   : size_(size),
61     big_endian_(big_endian),
62     segment_list_(segment_list),
63     section_list_(section_list),
64     secnamepool_(secnamepool)
65 {
66   // Count all the sections.  Start with 1 for the null section.
67   off_t count = 1;
68   for (Layout::Segment_list::const_iterator p = segment_list.begin();
69        p != segment_list.end();
70        ++p)
71     if ((*p)->type() == elfcpp::PT_LOAD)
72       count += (*p)->output_section_count();
73   count += section_list.size();
74
75   int shdr_size;
76   if (size == 32)
77     shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
78   else if (size == 64)
79     shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
80   else
81     abort();
82
83   this->set_data_size(count * shdr_size);
84 }
85
86 // Write out the section headers.
87
88 void
89 Output_section_headers::do_write(Output_file* of)
90 {
91   if (this->size_ == 32)
92     {
93       if (this->big_endian_)
94         this->do_sized_write<32, true>(of);
95       else
96         this->do_sized_write<32, false>(of);
97     }
98   else if (this->size_ == 64)
99     {
100       if (this->big_endian_)
101         this->do_sized_write<64, true>(of);
102       else
103         this->do_sized_write<64, false>(of);
104     }
105   else
106     abort();
107 }
108
109 template<int size, bool big_endian>
110 void
111 Output_section_headers::do_sized_write(Output_file* of)
112 {
113   off_t all_shdrs_size = this->data_size();
114   unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
115
116   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
117   unsigned char* v = view;
118
119   {
120     typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
121     oshdr.put_sh_name(0);
122     oshdr.put_sh_type(elfcpp::SHT_NULL);
123     oshdr.put_sh_flags(0);
124     oshdr.put_sh_addr(0);
125     oshdr.put_sh_offset(0);
126     oshdr.put_sh_size(0);
127     oshdr.put_sh_link(0);
128     oshdr.put_sh_info(0);
129     oshdr.put_sh_addralign(0);
130     oshdr.put_sh_entsize(0);
131   }
132
133   v += shdr_size;
134
135   unsigned shndx = 1;
136   for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
137        p != this->segment_list_.end();
138        ++p)
139     v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
140             this->secnamepool_, v, &shndx
141             SELECT_SIZE_ENDIAN(size, big_endian));
142   for (Layout::Section_list::const_iterator p = this->section_list_.begin();
143        p != this->section_list_.end();
144        ++p)
145     {
146       assert(shndx == (*p)->out_shndx());
147       elfcpp::Shdr_write<size, big_endian> oshdr(v);
148       (*p)->write_header(this->secnamepool_, &oshdr);
149       v += shdr_size;
150       ++shndx;
151     }
152
153   of->write_output_view(this->offset(), all_shdrs_size, view);
154 }
155
156 // Output_segment_header methods.
157
158 Output_segment_headers::Output_segment_headers(
159     int size,
160     bool big_endian,
161     const Layout::Segment_list& segment_list)
162   : size_(size), big_endian_(big_endian), segment_list_(segment_list)
163 {
164   int phdr_size;
165   if (size == 32)
166     phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
167   else if (size == 64)
168     phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
169   else
170     abort();
171
172   this->set_data_size(segment_list.size() * phdr_size);
173 }
174
175 void
176 Output_segment_headers::do_write(Output_file* of)
177 {
178   if (this->size_ == 32)
179     {
180       if (this->big_endian_)
181         this->do_sized_write<32, true>(of);
182       else
183         this->do_sized_write<32, false>(of);
184     }
185   else if (this->size_ == 64)
186     {
187       if (this->big_endian_)
188         this->do_sized_write<64, true>(of);
189       else
190         this->do_sized_write<64, false>(of);
191     }
192   else
193     abort();
194 }
195
196 template<int size, bool big_endian>
197 void
198 Output_segment_headers::do_sized_write(Output_file* of)
199 {
200   const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
201   off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
202   unsigned char* view = of->get_output_view(this->offset(),
203                                             all_phdrs_size);
204   unsigned char* v = view;
205   for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
206        p != this->segment_list_.end();
207        ++p)
208     {
209       elfcpp::Phdr_write<size, big_endian> ophdr(v);
210       (*p)->write_header(&ophdr);
211       v += phdr_size;
212     }
213
214   of->write_output_view(this->offset(), all_phdrs_size, view);
215 }
216
217 // Output_file_header methods.
218
219 Output_file_header::Output_file_header(int size,
220                                        bool big_endian,
221                                        const General_options& options,
222                                        const Target* target,
223                                        const Symbol_table* symtab,
224                                        const Output_segment_headers* osh)
225   : size_(size),
226     big_endian_(big_endian),
227     options_(options),
228     target_(target),
229     symtab_(symtab),
230     segment_header_(osh),
231     section_header_(NULL),
232     shstrtab_(NULL)
233 {
234   int ehdr_size;
235   if (size == 32)
236     ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
237   else if (size == 64)
238     ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
239   else
240     abort();
241
242   this->set_data_size(ehdr_size);
243 }
244
245 // Set the section table information for a file header.
246
247 void
248 Output_file_header::set_section_info(const Output_section_headers* shdrs,
249                                      const Output_section* shstrtab)
250 {
251   this->section_header_ = shdrs;
252   this->shstrtab_ = shstrtab;
253 }
254
255 // Write out the file header.
256
257 void
258 Output_file_header::do_write(Output_file* of)
259 {
260   if (this->size_ == 32)
261     {
262       if (this->big_endian_)
263         this->do_sized_write<32, true>(of);
264       else
265         this->do_sized_write<32, false>(of);
266     }
267   else if (this->size_ == 64)
268     {
269       if (this->big_endian_)
270         this->do_sized_write<64, true>(of);
271       else
272         this->do_sized_write<64, false>(of);
273     }
274   else
275     abort();
276 }
277
278 // Write out the file header with appropriate size and endianess.
279
280 template<int size, bool big_endian>
281 void
282 Output_file_header::do_sized_write(Output_file* of)
283 {
284   assert(this->offset() == 0);
285
286   int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
287   unsigned char* view = of->get_output_view(0, ehdr_size);
288   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
289
290   unsigned char e_ident[elfcpp::EI_NIDENT];
291   memset(e_ident, 0, elfcpp::EI_NIDENT);
292   e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
293   e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
294   e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
295   e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
296   if (size == 32)
297     e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
298   else if (size == 64)
299     e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
300   else
301     abort();
302   e_ident[elfcpp::EI_DATA] = (big_endian
303                               ? elfcpp::ELFDATA2MSB
304                               : elfcpp::ELFDATA2LSB);
305   e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
306   // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
307   oehdr.put_e_ident(e_ident);
308
309   elfcpp::ET e_type;
310   // FIXME: ET_DYN.
311   if (this->options_.is_relocatable())
312     e_type = elfcpp::ET_REL;
313   else
314     e_type = elfcpp::ET_EXEC;
315   oehdr.put_e_type(e_type);
316
317   oehdr.put_e_machine(this->target_->machine_code());
318   oehdr.put_e_version(elfcpp::EV_CURRENT);
319
320   // FIXME: Need to support -e, and target specific entry symbol.
321   Symbol* sym = this->symtab_->lookup("_start");
322   typename Sized_symbol<size>::Value_type v;
323   if (sym == NULL)
324     v = 0;
325   else
326     {
327       Sized_symbol<size>* ssym;
328       ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
329         sym SELECT_SIZE(size));
330       v = ssym->value();
331     }
332   oehdr.put_e_entry(v);
333
334   oehdr.put_e_phoff(this->segment_header_->offset());
335   oehdr.put_e_shoff(this->section_header_->offset());
336
337   // FIXME: The target needs to set the flags.
338   oehdr.put_e_flags(0);
339
340   oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
341   oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
342   oehdr.put_e_phnum(this->segment_header_->data_size()
343                      / elfcpp::Elf_sizes<size>::phdr_size);
344   oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
345   oehdr.put_e_shnum(this->section_header_->data_size()
346                      / elfcpp::Elf_sizes<size>::shdr_size);
347   oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
348
349   of->write_output_view(0, ehdr_size, view);
350 }
351
352 // Output_data_const methods.
353
354 void
355 Output_data_const::do_write(Output_file* output)
356 {
357   output->write(this->offset(), data_.data(), data_.size());
358 }
359
360 // Output_section_data methods.
361
362 unsigned int
363 Output_section_data::do_out_shndx() const
364 {
365   assert(this->output_section_ != NULL);
366   return this->output_section_->out_shndx();
367 }
368
369 // Output_data_got::Got_entry methods.
370
371 // Write out the entry.
372
373 template<int size, bool big_endian>
374 void
375 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov)
376     const
377 {
378   Valtype val = 0;
379
380   switch (this->local_sym_index_)
381     {
382     case GSYM_CODE:
383       {
384         Symbol* gsym = this->u_.gsym;
385
386         // If the symbol is resolved locally, we need to write out its
387         // value.  Otherwise we just write zero.  The target code is
388         // responsible for creating a relocation entry to fill in the
389         // value at runtime.
390         if (gsym->is_resolved_locally())
391           {
392             Sized_symbol<size>* sgsym;
393             // This cast is a bit ugly.  We don't want to put a
394             // virtual method in Symbol, because we want Symbol to be
395             // as small as possible.
396             sgsym = static_cast<Sized_symbol<size>*>(gsym);
397             val = sgsym->value();
398           }
399       }
400       break;
401
402     case CONSTANT_CODE:
403       val = this->u_.constant;
404       break;
405
406     default:
407       abort();
408     }
409
410   Valtype* povv = reinterpret_cast<Valtype*>(pov);
411   elfcpp::Swap<size, big_endian>::writeval(povv, val);
412 }
413
414 // Output_data_got methods.
415
416 // Add an entry for a global symbol to the GOT.  This returns true if
417 // this is a new GOT entry, false if the symbol already had a GOT
418 // entry.
419
420 template<int size, bool big_endian>
421 bool
422 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
423 {
424   if (gsym->has_got_offset())
425     return false;
426
427   this->entries_.push_back(Got_entry(gsym));
428   this->set_got_size();
429   gsym->set_got_offset(this->last_got_offset());
430   return true;
431 }
432
433 // Write out the GOT.
434
435 template<int size, bool big_endian>
436 void
437 Output_data_got<size, big_endian>::do_write(Output_file* of)
438 {
439   const int add = size / 8;
440
441   const off_t off = this->offset();
442   const off_t oview_size = this->entries_.size() * add;
443   unsigned char* const oview = of->get_output_view(off, oview_size);
444
445   unsigned char* pov = oview;
446   for (typename Got_entries::const_iterator p = this->entries_.begin();
447        p != this->entries_.end();
448        ++p)
449     {
450       p->write(pov);
451       pov += add;
452     }
453
454   of->write_output_view(off, oview_size, oview);
455
456   // We no longer need the GOT entries.
457   this->entries_.clear();
458 }
459
460 // Output_section::Input_section methods.
461
462 // Return the data size.  For an input section we store the size here.
463 // For an Output_section_data, we have to ask it for the size.
464
465 off_t
466 Output_section::Input_section::data_size() const
467 {
468   if (this->is_input_section())
469     return this->data_size_;
470   else
471     return this->u_.posd->data_size();
472 }
473
474 // Set the address and file offset.
475
476 void
477 Output_section::Input_section::set_address(uint64_t addr, off_t off,
478                                            off_t secoff)
479 {
480   if (this->is_input_section())
481     this->u_.object->set_section_offset(this->shndx_, off - secoff);
482   else
483     this->u_.posd->set_address(addr, off);
484 }
485
486 // Write out the data.  We don't have to do anything for an input
487 // section--they are handled via Object::relocate--but this is where
488 // we write out the data for an Output_section_data.
489
490 void
491 Output_section::Input_section::write(Output_file* of)
492 {
493   if (!this->is_input_section())
494     this->u_.posd->write(of);
495 }
496
497 // Output_section methods.
498
499 // Construct an Output_section.  NAME will point into a Stringpool.
500
501 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
502                                elfcpp::Elf_Xword flags, bool may_add_data)
503   : name_(name),
504     addralign_(0),
505     entsize_(0),
506     link_(0),
507     info_(0),
508     type_(type),
509     flags_(flags),
510     out_shndx_(0),
511     input_sections_(),
512     first_input_offset_(0),
513     may_add_data_(may_add_data)
514 {
515 }
516
517 Output_section::~Output_section()
518 {
519 }
520
521 // Add the input section SHNDX, with header SHDR, named SECNAME, in
522 // OBJECT, to the Output_section.  Return the offset of the input
523 // section within the output section.  We don't always keep track of
524 // input sections for an Output_section.  Instead, each Object keeps
525 // track of the Output_section for each of its input sections.
526
527 template<int size, bool big_endian>
528 off_t
529 Output_section::add_input_section(Relobj* object, unsigned int shndx,
530                                   const char* secname,
531                                   const elfcpp::Shdr<size, big_endian>& shdr)
532 {
533   assert(this->may_add_data_);
534
535   elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
536   if ((addralign & (addralign - 1)) != 0)
537     {
538       fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
539               program_name, object->name().c_str(),
540               static_cast<unsigned long>(addralign), secname);
541       gold_exit(false);
542     }
543
544   if (addralign > this->addralign_)
545     this->addralign_ = addralign;
546
547   off_t ssize = this->data_size();
548   ssize = align_address(ssize, addralign);
549   this->set_data_size(ssize + shdr.get_sh_size());
550
551   // We need to keep track of this section if we are already keeping
552   // track of sections, or if we are relaxing.  FIXME: Add test for
553   // relaxing.
554   if (! this->input_sections_.empty())
555     this->input_sections_.push_back(Input_section(object, shndx,
556                                                   shdr.get_sh_size(),
557                                                   addralign));
558
559   return ssize;
560 }
561
562 // Add arbitrary data to an output section.
563
564 void
565 Output_section::add_output_section_data(Output_section_data* posd)
566 {
567   if (this->input_sections_.empty())
568     this->first_input_offset_ = this->data_size();
569   this->input_sections_.push_back(Input_section(posd));
570   uint64_t addralign = posd->addralign();
571   if (addralign > this->addralign_)
572     this->addralign_ = addralign;
573   posd->set_output_section(this);
574 }
575
576 // Set the address of an Output_section.  This is where we handle
577 // setting the addresses of any Output_section_data objects.
578
579 void
580 Output_section::do_set_address(uint64_t address, off_t startoff)
581 {
582   if (this->input_sections_.empty())
583     return;
584
585   off_t off = startoff + this->first_input_offset_;
586   for (Input_section_list::iterator p = this->input_sections_.begin();
587        p != this->input_sections_.end();
588        ++p)
589     {
590       off = align_address(off, p->addralign());
591       p->set_address(address + (off - startoff), off, startoff);
592       off += p->data_size();
593     }
594
595   this->set_data_size(off - startoff);
596 }
597
598 // Write the section header to *OSHDR.
599
600 template<int size, bool big_endian>
601 void
602 Output_section::write_header(const Stringpool* secnamepool,
603                              elfcpp::Shdr_write<size, big_endian>* oshdr) const
604 {
605   oshdr->put_sh_name(secnamepool->get_offset(this->name_));
606   oshdr->put_sh_type(this->type_);
607   oshdr->put_sh_flags(this->flags_);
608   oshdr->put_sh_addr(this->address());
609   oshdr->put_sh_offset(this->offset());
610   oshdr->put_sh_size(this->data_size());
611   oshdr->put_sh_link(this->link_);
612   oshdr->put_sh_info(this->info_);
613   oshdr->put_sh_addralign(this->addralign_);
614   oshdr->put_sh_entsize(this->entsize_);
615 }
616
617 // Write out the data.  For input sections the data is written out by
618 // Object::relocate, but we have to handle Output_section_data objects
619 // here.
620
621 void
622 Output_section::do_write(Output_file* of)
623 {
624   for (Input_section_list::iterator p = this->input_sections_.begin();
625        p != this->input_sections_.end();
626        ++p)
627     p->write(of);
628 }
629
630 // Output_section_symtab methods.
631
632 Output_section_symtab::Output_section_symtab(const char* name, off_t size)
633   : Output_section(name, elfcpp::SHT_SYMTAB, 0, false)
634 {
635   this->set_data_size(size);
636 }
637
638 // Output_section_strtab methods.
639
640 Output_section_strtab::Output_section_strtab(const char* name,
641                                              Stringpool* contents)
642   : Output_section(name, elfcpp::SHT_STRTAB, 0, false),
643     contents_(contents)
644 {
645   this->set_data_size(contents->get_strtab_size());
646 }
647
648 void
649 Output_section_strtab::do_write(Output_file* of)
650 {
651   this->contents_->write(of, this->offset());
652 }
653
654 // Output segment methods.
655
656 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
657   : output_data_(),
658     output_bss_(),
659     vaddr_(0),
660     paddr_(0),
661     memsz_(0),
662     align_(0),
663     offset_(0),
664     filesz_(0),
665     type_(type),
666     flags_(flags),
667     is_align_known_(false)
668 {
669 }
670
671 // Add an Output_section to an Output_segment.
672
673 void
674 Output_segment::add_output_section(Output_section* os,
675                                    elfcpp::Elf_Word seg_flags,
676                                    bool front)
677 {
678   assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
679   assert(!this->is_align_known_);
680
681   // Update the segment flags.
682   this->flags_ |= seg_flags;
683
684   Output_segment::Output_data_list* pdl;
685   if (os->type() == elfcpp::SHT_NOBITS)
686     pdl = &this->output_bss_;
687   else
688     pdl = &this->output_data_;
689
690   // So that PT_NOTE segments will work correctly, we need to ensure
691   // that all SHT_NOTE sections are adjacent.  This will normally
692   // happen automatically, because all the SHT_NOTE input sections
693   // will wind up in the same output section.  However, it is possible
694   // for multiple SHT_NOTE input sections to have different section
695   // flags, and thus be in different output sections, but for the
696   // different section flags to map into the same segment flags and
697   // thus the same output segment.
698
699   // Note that while there may be many input sections in an output
700   // section, there are normally only a few output sections in an
701   // output segment.  This loop is expected to be fast.
702
703   if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
704     {
705       Layout::Data_list::iterator p = pdl->end();
706       do
707         {
708           --p;
709           if ((*p)->is_section_type(elfcpp::SHT_NOTE))
710             {
711               // We don't worry about the FRONT parameter.
712               ++p;
713               pdl->insert(p, os);
714               return;
715             }
716         }
717       while (p != pdl->begin());
718     }
719
720   // Similarly, so that PT_TLS segments will work, we need to group
721   // SHF_TLS sections.  An SHF_TLS/SHT_NOBITS section is a special
722   // case: we group the SHF_TLS/SHT_NOBITS sections right after the
723   // SHF_TLS/SHT_PROGBITS sections.  This lets us set up PT_TLS
724   // correctly.
725   if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
726     {
727       pdl = &this->output_data_;
728       bool nobits = os->type() == elfcpp::SHT_NOBITS;
729       bool sawtls = false;
730       Layout::Data_list::iterator p = pdl->end();
731       do
732         {
733           --p;
734           bool insert;
735           if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
736             {
737               sawtls = true;
738               // Put a NOBITS section after the first TLS section.
739               // But a PROGBITS section after the first TLS/PROGBITS
740               // section.
741               insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
742             }
743           else
744             {
745               // If we've gone past the TLS sections, but we've seen a
746               // TLS section, then we need to insert this section now.
747               insert = sawtls;
748             }
749
750           if (insert)
751             {
752               // We don't worry about the FRONT parameter.
753               ++p;
754               pdl->insert(p, os);
755               return;
756             }
757         }
758       while (p != pdl->begin());
759
760       // There are no TLS sections yet; put this one at the requested
761       // location in the section list.
762     }
763
764   if (front)
765     pdl->push_front(os);
766   else
767     pdl->push_back(os);
768 }
769
770 // Add an Output_data (which is not an Output_section) to the start of
771 // a segment.
772
773 void
774 Output_segment::add_initial_output_data(Output_data* od)
775 {
776   assert(!this->is_align_known_);
777   this->output_data_.push_front(od);
778 }
779
780 // Return the maximum alignment of the Output_data in Output_segment.
781 // Once we compute this, we prohibit new sections from being added.
782
783 uint64_t
784 Output_segment::addralign()
785 {
786   if (!this->is_align_known_)
787     {
788       uint64_t addralign;
789
790       addralign = Output_segment::maximum_alignment(&this->output_data_);
791       if (addralign > this->align_)
792         this->align_ = addralign;
793
794       addralign = Output_segment::maximum_alignment(&this->output_bss_);
795       if (addralign > this->align_)
796         this->align_ = addralign;
797
798       this->is_align_known_ = true;
799     }
800
801   return this->align_;
802 }
803
804 // Return the maximum alignment of a list of Output_data.
805
806 uint64_t
807 Output_segment::maximum_alignment(const Output_data_list* pdl)
808 {
809   uint64_t ret = 0;
810   for (Output_data_list::const_iterator p = pdl->begin();
811        p != pdl->end();
812        ++p)
813     {
814       uint64_t addralign = (*p)->addralign();
815       if (addralign > ret)
816         ret = addralign;
817     }
818   return ret;
819 }
820
821 // Set the section addresses for an Output_segment.  ADDR is the
822 // address and *POFF is the file offset.  Set the section indexes
823 // starting with *PSHNDX.  Return the address of the immediately
824 // following segment.  Update *POFF and *PSHNDX.
825
826 uint64_t
827 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
828                                       unsigned int* pshndx)
829 {
830   assert(this->type_ == elfcpp::PT_LOAD);
831
832   this->vaddr_ = addr;
833   this->paddr_ = addr;
834
835   off_t orig_off = *poff;
836   this->offset_ = orig_off;
837
838   *poff = align_address(*poff, this->addralign());
839
840   addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
841                                           pshndx);
842   this->filesz_ = *poff - orig_off;
843
844   off_t off = *poff;
845
846   uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
847                                                   poff, pshndx);
848   this->memsz_ = *poff - orig_off;
849
850   // Ignore the file offset adjustments made by the BSS Output_data
851   // objects.
852   *poff = off;
853
854   return ret;
855 }
856
857 // Set the addresses in a list of Output_data structures.
858
859 uint64_t
860 Output_segment::set_section_list_addresses(Output_data_list* pdl,
861                                            uint64_t addr, off_t* poff,
862                                            unsigned int* pshndx)
863 {
864   off_t startoff = *poff;
865
866   off_t off = startoff;
867   for (Output_data_list::iterator p = pdl->begin();
868        p != pdl->end();
869        ++p)
870     {
871       off = align_address(off, (*p)->addralign());
872       (*p)->set_address(addr + (off - startoff), off);
873
874       // Unless this is a PT_TLS segment, we want to ignore the size
875       // of a SHF_TLS/SHT_NOBITS section.  Such a section does not
876       // affect the size of a PT_LOAD segment.
877       if (this->type_ == elfcpp::PT_TLS
878           || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
879           || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
880         off += (*p)->data_size();
881
882       if ((*p)->is_section())
883         {
884           (*p)->set_out_shndx(*pshndx);
885           ++*pshndx;
886         }
887     }
888
889   *poff = off;
890   return addr + (off - startoff);
891 }
892
893 // For a non-PT_LOAD segment, set the offset from the sections, if
894 // any.
895
896 void
897 Output_segment::set_offset()
898 {
899   assert(this->type_ != elfcpp::PT_LOAD);
900
901   if (this->output_data_.empty() && this->output_bss_.empty())
902     {
903       this->vaddr_ = 0;
904       this->paddr_ = 0;
905       this->memsz_ = 0;
906       this->align_ = 0;
907       this->offset_ = 0;
908       this->filesz_ = 0;
909       return;
910     }
911
912   const Output_data* first;
913   if (this->output_data_.empty())
914     first = this->output_bss_.front();
915   else
916     first = this->output_data_.front();
917   this->vaddr_ = first->address();
918   this->paddr_ = this->vaddr_;
919   this->offset_ = first->offset();
920
921   if (this->output_data_.empty())
922     this->filesz_ = 0;
923   else
924     {
925       const Output_data* last_data = this->output_data_.back();
926       this->filesz_ = (last_data->address()
927                        + last_data->data_size()
928                        - this->vaddr_);
929     }
930
931   const Output_data* last;
932   if (this->output_bss_.empty())
933     last = this->output_data_.back();
934   else
935     last = this->output_bss_.back();
936   this->memsz_ = (last->address()
937                   + last->data_size()
938                   - this->vaddr_);
939 }
940
941 // Return the number of Output_sections in an Output_segment.
942
943 unsigned int
944 Output_segment::output_section_count() const
945 {
946   return (this->output_section_count_list(&this->output_data_)
947           + this->output_section_count_list(&this->output_bss_));
948 }
949
950 // Return the number of Output_sections in an Output_data_list.
951
952 unsigned int
953 Output_segment::output_section_count_list(const Output_data_list* pdl) const
954 {
955   unsigned int count = 0;
956   for (Output_data_list::const_iterator p = pdl->begin();
957        p != pdl->end();
958        ++p)
959     {
960       if ((*p)->is_section())
961         ++count;
962     }
963   return count;
964 }
965
966 // Write the segment data into *OPHDR.
967
968 template<int size, bool big_endian>
969 void
970 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
971 {
972   ophdr->put_p_type(this->type_);
973   ophdr->put_p_offset(this->offset_);
974   ophdr->put_p_vaddr(this->vaddr_);
975   ophdr->put_p_paddr(this->paddr_);
976   ophdr->put_p_filesz(this->filesz_);
977   ophdr->put_p_memsz(this->memsz_);
978   ophdr->put_p_flags(this->flags_);
979   ophdr->put_p_align(this->addralign());
980 }
981
982 // Write the section headers into V.
983
984 template<int size, bool big_endian>
985 unsigned char*
986 Output_segment::write_section_headers(const Stringpool* secnamepool,
987                                       unsigned char* v,
988                                       unsigned int *pshndx
989                                       ACCEPT_SIZE_ENDIAN) const
990 {
991   // Every section that is attached to a segment must be attached to a
992   // PT_LOAD segment, so we only write out section headers for PT_LOAD
993   // segments.
994   if (this->type_ != elfcpp::PT_LOAD)
995     return v;
996
997   v = this->write_section_headers_list
998       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
999           secnamepool, &this->output_data_, v, pshndx
1000           SELECT_SIZE_ENDIAN(size, big_endian));
1001   v = this->write_section_headers_list
1002       SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1003           secnamepool, &this->output_bss_, v, pshndx
1004           SELECT_SIZE_ENDIAN(size, big_endian));
1005   return v;
1006 }
1007
1008 template<int size, bool big_endian>
1009 unsigned char*
1010 Output_segment::write_section_headers_list(const Stringpool* secnamepool,
1011                                            const Output_data_list* pdl,
1012                                            unsigned char* v,
1013                                            unsigned int* pshndx
1014                                            ACCEPT_SIZE_ENDIAN) const
1015 {
1016   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1017   for (Output_data_list::const_iterator p = pdl->begin();
1018        p != pdl->end();
1019        ++p)
1020     {
1021       if ((*p)->is_section())
1022         {
1023           const Output_section* ps = static_cast<const Output_section*>(*p);
1024           assert(*pshndx == ps->out_shndx());
1025           elfcpp::Shdr_write<size, big_endian> oshdr(v);
1026           ps->write_header(secnamepool, &oshdr);
1027           v += shdr_size;
1028           ++*pshndx;
1029         }
1030     }
1031   return v;
1032 }
1033
1034 // Output_file methods.
1035
1036 Output_file::Output_file(const General_options& options)
1037   : options_(options),
1038     name_(options.output_file_name()),
1039     o_(-1),
1040     file_size_(0),
1041     base_(NULL)
1042 {
1043 }
1044
1045 // Open the output file.
1046
1047 void
1048 Output_file::open(off_t file_size)
1049 {
1050   this->file_size_ = file_size;
1051
1052   int mode = this->options_.is_relocatable() ? 0666 : 0777;
1053   int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1054   if (o < 0)
1055     {
1056       fprintf(stderr, _("%s: %s: open: %s\n"),
1057               program_name, this->name_, strerror(errno));
1058       gold_exit(false);
1059     }
1060   this->o_ = o;
1061
1062   // Write out one byte to make the file the right size.
1063   if (::lseek(o, file_size - 1, SEEK_SET) < 0)
1064     {
1065       fprintf(stderr, _("%s: %s: lseek: %s\n"),
1066               program_name, this->name_, strerror(errno));
1067       gold_exit(false);
1068     }
1069   char b = 0;
1070   if (::write(o, &b, 1) != 1)
1071     {
1072       fprintf(stderr, _("%s: %s: write: %s\n"),
1073               program_name, this->name_, strerror(errno));
1074       gold_exit(false);
1075     }
1076
1077   // Map the file into memory.
1078   void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
1079                       MAP_SHARED, o, 0);
1080   if (base == MAP_FAILED)
1081     {
1082       fprintf(stderr, _("%s: %s: mmap: %s\n"),
1083               program_name, this->name_, strerror(errno));
1084       gold_exit(false);
1085     }
1086   this->base_ = static_cast<unsigned char*>(base);
1087 }
1088
1089 // Close the output file.
1090
1091 void
1092 Output_file::close()
1093 {
1094   if (::munmap(this->base_, this->file_size_) < 0)
1095     {
1096       fprintf(stderr, _("%s: %s: munmap: %s\n"),
1097               program_name, this->name_, strerror(errno));
1098       gold_exit(false);
1099     }
1100   this->base_ = NULL;
1101
1102   if (::close(this->o_) < 0)
1103     {
1104       fprintf(stderr, _("%s: %s: close: %s\n"),
1105               program_name, this->name_, strerror(errno));
1106       gold_exit(false);
1107     }
1108   this->o_ = -1;
1109 }
1110
1111 // Instantiate the templates we need.  We could use the configure
1112 // script to restrict this to only the ones for implemented targets.
1113
1114 template
1115 off_t
1116 Output_section::add_input_section<32, false>(
1117     Relobj* object,
1118     unsigned int shndx,
1119     const char* secname,
1120     const elfcpp::Shdr<32, false>& shdr);
1121
1122 template
1123 off_t
1124 Output_section::add_input_section<32, true>(
1125     Relobj* object,
1126     unsigned int shndx,
1127     const char* secname,
1128     const elfcpp::Shdr<32, true>& shdr);
1129
1130 template
1131 off_t
1132 Output_section::add_input_section<64, false>(
1133     Relobj* object,
1134     unsigned int shndx,
1135     const char* secname,
1136     const elfcpp::Shdr<64, false>& shdr);
1137
1138 template
1139 off_t
1140 Output_section::add_input_section<64, true>(
1141     Relobj* object,
1142     unsigned int shndx,
1143     const char* secname,
1144     const elfcpp::Shdr<64, true>& shdr);
1145
1146 template
1147 class Output_data_got<32, false>;
1148
1149 template
1150 class Output_data_got<32, true>;
1151
1152 template
1153 class Output_data_got<64, false>;
1154
1155 template
1156 class Output_data_got<64, true>;
1157
1158 } // End namespace gold.