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