Update for line number changes.
[binutils.git] / gold / output.cc
blob220a4f6b9e5bc0f250dd937293e543ab65a25796
1 // output.cc -- manage the output file for gold
3 #include "gold.h"
5 #include <cstdlib>
6 #include <cerrno>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <sys/mman.h>
10 #include <algorithm>
12 #include "object.h"
13 #include "symtab.h"
14 #include "reloc.h"
15 #include "output.h"
17 namespace gold
20 // Output_data methods.
22 Output_data::~Output_data()
26 // Set the address and offset.
28 void
29 Output_data::set_address(uint64_t addr, off_t off)
31 this->address_ = addr;
32 this->offset_ = off;
34 // Let the child class know.
35 this->do_set_address(addr, off);
38 // Return the default alignment for a size--32 or 64.
40 uint64_t
41 Output_data::default_alignment(int size)
43 if (size == 32)
44 return 4;
45 else if (size == 64)
46 return 8;
47 else
48 abort();
51 // Output_data_const methods.
53 void
54 Output_data_const::do_write(Output_file* output)
56 output->write(this->offset(), data_.data(), data_.size());
59 // Output_section_header methods. This currently assumes that the
60 // segment and section lists are complete at construction time.
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)
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();
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();
91 this->set_data_size(count * shdr_size);
94 // Write out the section headers.
96 void
97 Output_section_headers::do_write(Output_file* of)
99 if (this->size_ == 32)
101 if (this->big_endian_)
102 this->do_sized_write<32, true>(of);
103 else
104 this->do_sized_write<32, false>(of);
106 else if (this->size_ == 64)
108 if (this->big_endian_)
109 this->do_sized_write<64, true>(of);
110 else
111 this->do_sized_write<64, false>(of);
113 else
114 abort();
117 template<int size, bool big_endian>
118 void
119 Output_section_headers::do_sized_write(Output_file* of)
121 off_t all_shdrs_size = this->data_size();
122 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
124 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
125 unsigned char* v = view;
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);
141 v += shdr_size;
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(size, big_endian) (
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)
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;
161 of->write_output_view(this->offset(), all_shdrs_size, view);
164 // Output_segment_header methods.
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)
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();
180 this->set_data_size(segment_list.size() * phdr_size);
183 void
184 Output_segment_headers::do_write(Output_file* of)
186 if (this->size_ == 32)
188 if (this->big_endian_)
189 this->do_sized_write<32, true>(of);
190 else
191 this->do_sized_write<32, false>(of);
193 else if (this->size_ == 64)
195 if (this->big_endian_)
196 this->do_sized_write<64, true>(of);
197 else
198 this->do_sized_write<64, false>(of);
200 else
201 abort();
204 template<int size, bool big_endian>
205 void
206 Output_segment_headers::do_sized_write(Output_file* of)
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)
217 elfcpp::Phdr_write<size, big_endian> ophdr(v);
218 (*p)->write_header(&ophdr);
219 v += phdr_size;
222 of->write_output_view(this->offset(), all_phdrs_size, view);
225 // Output_file_header methods.
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)
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();
250 this->set_data_size(ehdr_size);
253 // Set the section table information for a file header.
255 void
256 Output_file_header::set_section_info(const Output_section_headers* shdrs,
257 const Output_section* shstrtab)
259 this->section_header_ = shdrs;
260 this->shstrtab_ = shstrtab;
263 // Write out the file header.
265 void
266 Output_file_header::do_write(Output_file* of)
268 if (this->size_ == 32)
270 if (this->big_endian_)
271 this->do_sized_write<32, true>(of);
272 else
273 this->do_sized_write<32, false>(of);
275 else if (this->size_ == 64)
277 if (this->big_endian_)
278 this->do_sized_write<64, true>(of);
279 else
280 this->do_sized_write<64, false>(of);
282 else
283 abort();
286 // Write out the file header with appropriate size and endianess.
288 template<int size, bool big_endian>
289 void
290 Output_file_header::do_sized_write(Output_file* of)
292 assert(this->offset() == 0);
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);
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);
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);
325 oehdr.put_e_machine(this->target_->machine_code());
326 oehdr.put_e_version(elfcpp::EV_CURRENT);
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
335 Sized_symbol<size>* ssym;
336 ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
337 sym SELECT_SIZE(size));
338 v = ssym->value();
340 oehdr.put_e_entry(v);
342 oehdr.put_e_phoff(this->segment_header_->offset());
343 oehdr.put_e_shoff(this->section_header_->offset());
345 // FIXME: The target needs to set the flags.
346 oehdr.put_e_flags(0);
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());
357 of->write_output_view(0, ehdr_size, view);
360 // Output_section_got::Got_entry methods.
362 // Write out the entry.
364 template<int size, bool big_endian>
365 void
366 Output_section_got<size, big_endian>::Got_entry::write(unsigned char* pov)
367 const
369 Valtype val = 0;
371 switch (this->local_sym_index_)
373 case GSYM_CODE:
375 Symbol* gsym = this->u_.gsym;
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())
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();
391 break;
393 case CONSTANT_CODE:
394 val = this->u_.constant;
395 break;
397 default:
398 abort();
401 Valtype* povv = reinterpret_cast<Valtype*>(pov);
402 Swap<size, big_endian>::writeval(povv, val);
405 // Output_section_data methods.
407 unsigned int
408 Output_section_data::do_out_shndx() const
410 assert(this->output_section_ != NULL);
411 return this->output_section_->out_shndx();
414 // Output_section_got methods.
416 // Write out the GOT.
418 template<int size, bool big_endian>
419 void
420 Output_section_got<size, big_endian>::do_write(Output_file* of)
422 const int add = size / 8;
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);
428 unsigned char* pov = oview;
429 for (typename Got_entries::const_iterator p = this->entries_.begin();
430 p != this->entries_.end();
431 ++p)
433 p->write(pov);
434 pov += add;
437 of->write_output_view(off, oview_size, oview);
439 // We no longer need the GOT entries.
440 this->entries_.clear();
443 // Output_section::Input_section methods.
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.
448 off_t
449 Output_section::Input_section::data_size() const
451 if (this->is_input_section())
452 return this->data_size_;
453 else
454 return this->u_.posd->data_size();
457 // Set the address and file offset.
459 void
460 Output_section::Input_section::set_address(uint64_t addr, off_t off,
461 off_t secoff)
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);
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.
473 void
474 Output_section::Input_section::write(Output_file* of)
476 if (!this->is_input_section())
477 this->u_.posd->write(of);
480 // Output_section methods.
482 // Construct an Output_section. NAME will point into a Stringpool.
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)
500 Output_section::~Output_section()
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.
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)
516 assert(this->may_add_data_);
518 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
519 if ((addralign & (addralign - 1)) != 0)
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);
527 if (addralign > this->addralign_)
528 this->addralign_ = addralign;
530 off_t ssize = this->data_size();
531 ssize = align_address(ssize, addralign);
532 this->set_data_size(ssize + shdr.get_sh_size());
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));
542 return ssize;
545 // Add arbitrary data to an output section.
547 void
548 Output_section::add_output_section_data(Output_section_data* posd)
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);
559 // Set the address of an Output_section. This is where we handle
560 // setting the addresses of any Output_section_data objects.
562 void
563 Output_section::do_set_address(uint64_t address, off_t startoff)
565 if (this->input_sections_.empty())
566 return;
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)
573 off = align_address(off, p->addralign());
574 p->set_address(address + (off - startoff), off, startoff);
575 off += p->data_size();
578 this->set_data_size(off - startoff);
581 // Write the section header to *OSHDR.
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
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_);
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.
604 void
605 Output_section::do_write(Output_file* of)
607 for (Input_section_list::iterator p = this->input_sections_.begin();
608 p != this->input_sections_.end();
609 ++p)
610 p->write(of);
613 // Output_section_symtab methods.
615 Output_section_symtab::Output_section_symtab(const char* name, off_t size)
616 : Output_section(name, elfcpp::SHT_SYMTAB, 0, false)
618 this->set_data_size(size);
621 // Output_section_strtab methods.
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)
628 this->set_data_size(contents->get_strtab_size());
631 void
632 Output_section_strtab::do_write(Output_file* of)
634 this->contents_->write(of, this->offset());
637 // Output segment methods.
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)
654 // Add an Output_section to an Output_segment.
656 void
657 Output_segment::add_output_section(Output_section* os,
658 elfcpp::Elf_Word seg_flags)
660 assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
661 assert(!this->is_align_known_);
663 // Update the segment flags.
664 this->flags_ |= seg_flags;
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_;
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.
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.
685 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
687 Layout::Data_list::iterator p = pdl->end();
690 --p;
691 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
693 ++p;
694 pdl->insert(p, os);
695 return;
698 while (p != pdl->begin());
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())
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();
714 --p;
715 bool insert;
716 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
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);
724 else
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;
731 if (insert)
733 ++p;
734 pdl->insert(p, os);
735 return;
738 while (p != pdl->begin());
740 // There are no TLS sections yet; put this one at the end of the
741 // section list.
744 pdl->push_back(os);
747 // Add an Output_data (which is not an Output_section) to the start of
748 // a segment.
750 void
751 Output_segment::add_initial_output_data(Output_data* od)
753 assert(!this->is_align_known_);
754 this->output_data_.push_front(od);
757 // Return the maximum alignment of the Output_data in Output_segment.
758 // Once we compute this, we prohibit new sections from being added.
760 uint64_t
761 Output_segment::addralign()
763 if (!this->is_align_known_)
765 uint64_t addralign;
767 addralign = Output_segment::maximum_alignment(&this->output_data_);
768 if (addralign > this->align_)
769 this->align_ = addralign;
771 addralign = Output_segment::maximum_alignment(&this->output_bss_);
772 if (addralign > this->align_)
773 this->align_ = addralign;
775 this->is_align_known_ = true;
778 return this->align_;
781 // Return the maximum alignment of a list of Output_data.
783 uint64_t
784 Output_segment::maximum_alignment(const Output_data_list* pdl)
786 uint64_t ret = 0;
787 for (Output_data_list::const_iterator p = pdl->begin();
788 p != pdl->end();
789 ++p)
791 uint64_t addralign = (*p)->addralign();
792 if (addralign > ret)
793 ret = addralign;
795 return ret;
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.
803 uint64_t
804 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
805 unsigned int* pshndx)
807 assert(this->type_ == elfcpp::PT_LOAD);
809 this->vaddr_ = addr;
810 this->paddr_ = addr;
812 off_t orig_off = *poff;
813 this->offset_ = orig_off;
815 *poff = align_address(*poff, this->addralign());
817 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
818 pshndx);
819 this->filesz_ = *poff - orig_off;
821 off_t off = *poff;
823 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
824 poff, pshndx);
825 this->memsz_ = *poff - orig_off;
827 // Ignore the file offset adjustments made by the BSS Output_data
828 // objects.
829 *poff = off;
831 return ret;
834 // Set the addresses in a list of Output_data structures.
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)
841 off_t startoff = *poff;
843 off_t off = startoff;
844 for (Output_data_list::iterator p = pdl->begin();
845 p != pdl->end();
846 ++p)
848 off = align_address(off, (*p)->addralign());
849 (*p)->set_address(addr + (off - startoff), off);
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();
859 if ((*p)->is_section())
861 (*p)->set_out_shndx(*pshndx);
862 ++*pshndx;
866 *poff = off;
867 return addr + (off - startoff);
870 // For a non-PT_LOAD segment, set the offset from the sections, if
871 // any.
873 void
874 Output_segment::set_offset()
876 assert(this->type_ != elfcpp::PT_LOAD);
878 if (this->output_data_.empty() && this->output_bss_.empty())
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;
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();
898 if (this->output_data_.empty())
899 this->filesz_ = 0;
900 else
902 const Output_data* last_data = this->output_data_.back();
903 this->filesz_ = (last_data->address()
904 + last_data->data_size()
905 - this->vaddr_);
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_);
918 // Return the number of Output_sections in an Output_segment.
920 unsigned int
921 Output_segment::output_section_count() const
923 return (this->output_section_count_list(&this->output_data_)
924 + this->output_section_count_list(&this->output_bss_));
927 // Return the number of Output_sections in an Output_data_list.
929 unsigned int
930 Output_segment::output_section_count_list(const Output_data_list* pdl) const
932 unsigned int count = 0;
933 for (Output_data_list::const_iterator p = pdl->begin();
934 p != pdl->end();
935 ++p)
937 if ((*p)->is_section())
938 ++count;
940 return count;
943 // Write the segment data into *OPHDR.
945 template<int size, bool big_endian>
946 void
947 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
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());
959 // Write the section headers into V.
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
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;
974 v = this->write_section_headers_list
975 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
976 secnamepool, &this->output_data_, v, pshndx
977 SELECT_SIZE_ENDIAN(size, big_endian));
978 v = this->write_section_headers_list
979 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
980 secnamepool, &this->output_bss_, v, pshndx
981 SELECT_SIZE_ENDIAN(size, big_endian));
982 return v;
985 template<int size, bool big_endian>
986 unsigned char*
987 Output_segment::write_section_headers_list(const Stringpool* secnamepool,
988 const Output_data_list* pdl,
989 unsigned char* v,
990 unsigned int* pshndx
991 ACCEPT_SIZE_ENDIAN) const
993 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
994 for (Output_data_list::const_iterator p = pdl->begin();
995 p != pdl->end();
996 ++p)
998 if ((*p)->is_section())
1000 const Output_section* ps = static_cast<const Output_section*>(*p);
1001 assert(*pshndx == ps->out_shndx());
1002 elfcpp::Shdr_write<size, big_endian> oshdr(v);
1003 ps->write_header(secnamepool, &oshdr);
1004 v += shdr_size;
1005 ++*pshndx;
1008 return v;
1011 // Output_file methods.
1013 Output_file::Output_file(const General_options& options)
1014 : options_(options),
1015 name_(options.output_file_name()),
1016 o_(-1),
1017 file_size_(0),
1018 base_(NULL)
1022 // Open the output file.
1024 void
1025 Output_file::open(off_t file_size)
1027 this->file_size_ = file_size;
1029 int mode = this->options_.is_relocatable() ? 0666 : 0777;
1030 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1031 if (o < 0)
1033 fprintf(stderr, _("%s: %s: open: %s\n"),
1034 program_name, this->name_, strerror(errno));
1035 gold_exit(false);
1037 this->o_ = o;
1039 // Write out one byte to make the file the right size.
1040 if (::lseek(o, file_size - 1, SEEK_SET) < 0)
1042 fprintf(stderr, _("%s: %s: lseek: %s\n"),
1043 program_name, this->name_, strerror(errno));
1044 gold_exit(false);
1046 char b = 0;
1047 if (::write(o, &b, 1) != 1)
1049 fprintf(stderr, _("%s: %s: write: %s\n"),
1050 program_name, this->name_, strerror(errno));
1051 gold_exit(false);
1054 // Map the file into memory.
1055 void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
1056 MAP_SHARED, o, 0);
1057 if (base == MAP_FAILED)
1059 fprintf(stderr, _("%s: %s: mmap: %s\n"),
1060 program_name, this->name_, strerror(errno));
1061 gold_exit(false);
1063 this->base_ = static_cast<unsigned char*>(base);
1066 // Close the output file.
1068 void
1069 Output_file::close()
1071 if (::munmap(this->base_, this->file_size_) < 0)
1073 fprintf(stderr, _("%s: %s: munmap: %s\n"),
1074 program_name, this->name_, strerror(errno));
1075 gold_exit(false);
1077 this->base_ = NULL;
1079 if (::close(this->o_) < 0)
1081 fprintf(stderr, _("%s: %s: close: %s\n"),
1082 program_name, this->name_, strerror(errno));
1083 gold_exit(false);
1085 this->o_ = -1;
1088 // Instantiate the templates we need. We could use the configure
1089 // script to restrict this to only the ones for implemented targets.
1091 template
1092 off_t
1093 Output_section::add_input_section<32, false>(
1094 Object* object,
1095 unsigned int shndx,
1096 const char* secname,
1097 const elfcpp::Shdr<32, false>& shdr);
1099 template
1100 off_t
1101 Output_section::add_input_section<32, true>(
1102 Object* object,
1103 unsigned int shndx,
1104 const char* secname,
1105 const elfcpp::Shdr<32, true>& shdr);
1107 template
1108 off_t
1109 Output_section::add_input_section<64, false>(
1110 Object* object,
1111 unsigned int shndx,
1112 const char* secname,
1113 const elfcpp::Shdr<64, false>& shdr);
1115 template
1116 off_t
1117 Output_section::add_input_section<64, true>(
1118 Object* object,
1119 unsigned int shndx,
1120 const char* secname,
1121 const elfcpp::Shdr<64, true>& shdr);
1123 template
1124 void
1125 Output_section_got<32, false>::do_write(Output_file* of);
1127 template
1128 void
1129 Output_section_got<32, true>::do_write(Output_file* of);
1131 template
1132 void
1133 Output_section_got<64, false>::do_write(Output_file* of);
1135 template
1136 void
1137 Output_section_got<64, true>::do_write(Output_file* of);
1139 } // End namespace gold.