1 // output.cc -- manage the output file for gold
18 // Output_data methods.
20 Output_data::~Output_data()
24 // Set the address and offset.
27 Output_data::set_address(uint64_t addr, off_t off)
29 this->address_ = addr;
32 // Let the child class know.
33 this->do_set_address(addr, off);
36 // Return the default alignment for a size--32 or 64.
39 Output_data::default_alignment(int size)
49 // Output_data_const methods.
52 Output_data_const::do_write(Output_file* output)
54 output->write(this->offset(), data_.data(), data_.size());
57 // Output_section_header methods. This currently assumes that the
58 // segment and section lists are complete at construction time.
60 Output_section_headers::Output_section_headers(
63 const Layout::Segment_list& segment_list,
64 const Layout::Section_list& section_list,
65 const Stringpool* secnamepool)
67 big_endian_(big_endian),
68 segment_list_(segment_list),
69 section_list_(section_list),
70 secnamepool_(secnamepool)
72 // Count all the sections. Start with 1 for the null section.
74 for (Layout::Segment_list::const_iterator p = segment_list.begin();
75 p != segment_list.end();
77 count += (*p)->output_section_count();
78 count += section_list.size();
82 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
84 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
88 this->set_data_size(count * shdr_size);
91 // Write out the section headers.
94 Output_section_headers::do_write(Output_file* of)
96 if (this->size_ == 32)
98 if (this->big_endian_)
99 this->do_sized_write<32, true>(of);
101 this->do_sized_write<32, false>(of);
103 else if (this->size_ == 64)
105 if (this->big_endian_)
106 this->do_sized_write<64, true>(of);
108 this->do_sized_write<64, false>(of);
114 template<int size, bool big_endian>
116 Output_section_headers::do_sized_write(Output_file* of)
118 off_t all_shdrs_size = this->data_size();
119 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
121 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
122 unsigned char* v = view;
125 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
126 oshdr.put_sh_name(0);
127 oshdr.put_sh_type(elfcpp::SHT_NULL);
128 oshdr.put_sh_flags(0);
129 oshdr.put_sh_addr(0);
130 oshdr.put_sh_offset(0);
131 oshdr.put_sh_size(0);
132 oshdr.put_sh_link(0);
133 oshdr.put_sh_info(0);
134 oshdr.put_sh_addralign(0);
135 oshdr.put_sh_entsize(0);
140 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
141 p != this->segment_list_.end();
143 v = (*p)->write_section_headers<size, big_endian>(this->secnamepool_, v);
144 for (Layout::Section_list::const_iterator p = this->section_list_.begin();
145 p != this->section_list_.end();
148 elfcpp::Shdr_write<size, big_endian> oshdr(v);
149 (*p)->write_header(this->secnamepool_, &oshdr);
153 of->write_output_view(this->offset(), all_shdrs_size, view);
156 // Output_segment_header methods.
158 Output_segment_headers::Output_segment_headers(
161 const Layout::Segment_list& segment_list)
162 : size_(size), big_endian_(big_endian), segment_list_(segment_list)
166 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
168 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
172 this->set_data_size(segment_list.size() * phdr_size);
176 Output_segment_headers::do_write(Output_file* of)
178 if (this->size_ == 32)
180 if (this->big_endian_)
181 this->do_sized_write<32, true>(of);
183 this->do_sized_write<32, false>(of);
185 else if (this->size_ == 64)
187 if (this->big_endian_)
188 this->do_sized_write<64, true>(of);
190 this->do_sized_write<64, false>(of);
196 template<int size, bool big_endian>
198 Output_segment_headers::do_sized_write(Output_file* of)
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(),
204 unsigned char* v = view;
205 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
206 p != this->segment_list_.end();
209 elfcpp::Phdr_write<size, big_endian> ophdr(v);
210 (*p)->write_header(&ophdr);
214 of->write_output_view(this->offset(), all_phdrs_size, view);
217 // Output_file_header methods.
219 Output_file_header::Output_file_header(int size,
221 const General_options& options,
222 const Target* target,
223 const Symbol_table* symtab,
224 const Output_segment_headers* osh)
226 big_endian_(big_endian),
230 segment_header_(osh),
231 section_header_(NULL),
236 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
238 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
242 this->set_data_size(ehdr_size);
245 // Set the section table information for a file header.
248 Output_file_header::set_section_info(const Output_section_headers* shdrs,
249 const Output_section* shstrtab)
251 this->section_header_ = shdrs;
252 this->shstrtab_ = shstrtab;
255 // Write out the file header.
258 Output_file_header::do_write(Output_file* of)
260 if (this->size_ == 32)
262 if (this->big_endian_)
263 this->do_sized_write<32, true>(of);
265 this->do_sized_write<32, false>(of);
267 else if (this->size_ == 64)
269 if (this->big_endian_)
270 this->do_sized_write<64, true>(of);
272 this->do_sized_write<64, false>(of);
278 // Write out the file header with appropriate size and endianess.
280 template<int size, bool big_endian>
282 Output_file_header::do_sized_write(Output_file* of)
284 assert(this->offset() == 0);
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);
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;
297 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
299 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
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);
311 if (this->options_.is_relocatable())
312 e_type = elfcpp::ET_REL;
314 e_type = elfcpp::ET_EXEC;
315 oehdr.put_e_type(e_type);
317 oehdr.put_e_machine(this->target_->machine_code());
318 oehdr.put_e_version(elfcpp::EV_CURRENT);
320 Symbol* sym = this->symtab_->lookup("_start");
321 typename Sized_symbol<size>::Value_type v;
326 Sized_symbol<size>* ssym;
327 ssym = this->symtab_->get_sized_symbol<size>(sym);
330 oehdr.put_e_entry(v);
332 oehdr.put_e_phoff(this->segment_header_->offset());
333 oehdr.put_e_shoff(this->section_header_->offset());
335 // FIXME: The target needs to set the flags.
336 oehdr.put_e_flags(0);
338 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
339 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
340 oehdr.put_e_phnum(this->segment_header_->data_size()
341 / elfcpp::Elf_sizes<size>::phdr_size);
342 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
343 oehdr.put_e_shnum(this->section_header_->data_size()
344 / elfcpp::Elf_sizes<size>::shdr_size);
345 oehdr.put_e_shstrndx(this->shstrtab_->shndx());
347 of->write_output_view(0, ehdr_size, view);
350 // Output_section methods.
352 // Construct an Output_section. NAME will point into a Stringpool.
354 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
355 elfcpp::Elf_Xword flags, unsigned int shndx)
367 Output_section::~Output_section()
371 // Add an input section to an Output_section. We don't keep track of
372 // input sections for an Output_section. Instead, each Object keeps
373 // track of the Output_section for each of its input sections.
375 template<int size, bool big_endian>
377 Output_section::add_input_section(Object* object, const char* secname,
378 const elfcpp::Shdr<size, big_endian>& shdr)
380 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
381 if ((addralign & (addralign - 1)) != 0)
383 fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
384 program_name, object->name().c_str(),
385 static_cast<unsigned long>(addralign), secname);
389 if (addralign > this->addralign_)
390 this->addralign_ = addralign;
392 off_t ssize = this->data_size();
393 ssize = (ssize + addralign - 1) &~ (addralign - 1);
395 // SHF_TLS/SHT_NOBITS sections are handled specially: they are
396 // treated as having no size and taking up no space. We only use
397 // the real size when setting the pt_memsz field of the PT_TLS
399 if ((this->flags_ & elfcpp::SHF_TLS) == 0
400 || this->type_ != elfcpp::SHT_NOBITS)
401 this->set_data_size(ssize + shdr.get_sh_size());
406 // Write the section header to *OSHDR.
408 template<int size, bool big_endian>
410 Output_section::write_header(const Stringpool* secnamepool,
411 elfcpp::Shdr_write<size, big_endian>* oshdr) const
413 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
414 oshdr->put_sh_type(this->type_);
415 oshdr->put_sh_flags(this->flags_);
416 oshdr->put_sh_addr(this->address());
417 oshdr->put_sh_offset(this->offset());
418 oshdr->put_sh_size(this->data_size());
419 oshdr->put_sh_link(this->link_);
420 oshdr->put_sh_info(this->info_);
421 oshdr->put_sh_addralign(this->addralign_);
422 oshdr->put_sh_entsize(this->entsize_);
425 // Output_section_symtab methods.
427 Output_section_symtab::Output_section_symtab(const char* name, off_t size,
429 : Output_section(name, elfcpp::SHT_SYMTAB, 0, shndx)
431 this->set_data_size(size);
434 // Output_section_strtab methods.
436 Output_section_strtab::Output_section_strtab(const char* name,
437 Stringpool* contents,
439 : Output_section(name, elfcpp::SHT_STRTAB, 0, shndx),
442 this->set_data_size(contents->get_strtab_size());
446 Output_section_strtab::do_write(Output_file* of)
448 this->contents_->write(of, this->offset());
451 // Output segment methods.
453 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
467 // Add an Output_section to an Output_segment.
470 Output_segment::add_output_section(Output_section* os,
471 elfcpp::Elf_Word seg_flags)
473 assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
475 // Update the segment flags and alignment.
476 this->flags_ |= seg_flags;
477 uint64_t addralign = os->addralign();
478 if (addralign > this->align_)
479 this->align_ = addralign;
481 Output_segment::Output_data_list* pdl;
482 if (os->type() == elfcpp::SHT_NOBITS)
483 pdl = &this->output_bss_;
485 pdl = &this->output_data_;
487 // So that PT_NOTE segments will work correctly, we need to ensure
488 // that all SHT_NOTE sections are adjacent. This will normally
489 // happen automatically, because all the SHT_NOTE input sections
490 // will wind up in the same output section. However, it is possible
491 // for multiple SHT_NOTE input sections to have different section
492 // flags, and thus be in different output sections, but for the
493 // different section flags to map into the same segment flags and
494 // thus the same output segment.
496 // Note that while there may be many input sections in an output
497 // section, there are normally only a few output sections in an
498 // output segment. This loop is expected to be fast.
500 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
502 Layout::Data_list::iterator p = pdl->end();
506 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
513 while (p != pdl->begin());
516 // Similarly, so that PT_TLS segments will work, we need to group
517 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
518 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
519 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
521 if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
523 pdl = &this->output_data_;
524 bool nobits = os->type() == elfcpp::SHT_NOBITS;
525 Layout::Data_list::iterator p = pdl->end();
529 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
530 && (nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS)))
537 while (p != pdl->begin());
543 // Add an Output_data (which is not an Output_section) to the start of
547 Output_segment::add_initial_output_data(Output_data* od)
549 uint64_t addralign = od->addralign();
550 if (addralign > this->align_)
551 this->align_ = addralign;
553 this->output_data_.push_front(od);
556 // Return the maximum alignment of the Output_data in Output_segment.
557 // We keep this up to date as we add Output_sections and Output_data.
560 Output_segment::max_data_align() const
565 // Set the section addresses for an Output_segment. ADDR is the
566 // address and *POFF is the file offset. Return the address of the
567 // immediately following segment. Update *POFF.
570 Output_segment::set_section_addresses(uint64_t addr, off_t* poff)
572 assert(this->type_ == elfcpp::PT_LOAD);
577 off_t orig_off = *poff;
578 this->offset_ = orig_off;
580 addr = this->set_section_list_addresses(&this->output_data_, addr, poff);
581 this->filesz_ = *poff - orig_off;
585 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
587 this->memsz_ = *poff - orig_off;
589 // Ignore the file offset adjustments made by the BSS Output_data
596 // Set the addresses in a list of Output_data structures.
599 Output_segment::set_section_list_addresses(Output_data_list* pdl,
600 uint64_t addr, off_t* poff)
604 for (Output_data_list::iterator p = pdl->begin();
608 uint64_t addralign = (*p)->addralign();
609 addr = (addr + addralign - 1) & ~ (addralign - 1);
610 off = (off + addralign - 1) & ~ (addralign - 1);
611 (*p)->set_address(addr, off);
613 uint64_t size = (*p)->data_size();
622 // For a non-PT_LOAD segment, set the offset from the sections, if
626 Output_segment::set_offset()
628 assert(this->type_ != elfcpp::PT_LOAD);
630 if (this->output_data_.empty() && this->output_bss_.empty())
641 const Output_data* first;
642 if (this->output_data_.empty())
643 first = this->output_bss_.front();
645 first = this->output_data_.front();
646 this->vaddr_ = first->address();
647 this->paddr_ = this->vaddr_;
648 this->offset_ = first->offset();
650 if (this->output_data_.empty())
654 const Output_data* last_data = this->output_data_.back();
655 this->filesz_ = (last_data->address()
656 + last_data->data_size()
660 const Output_data* last;
661 if (this->output_bss_.empty())
662 last = this->output_data_.back();
664 last = this->output_bss_.back();
665 this->memsz_ = (last->address()
669 // this->align_ was set as we added items.
672 // Return the number of Output_sections in an Output_segment.
675 Output_segment::output_section_count() const
677 return (this->output_section_count_list(&this->output_data_)
678 + this->output_section_count_list(&this->output_bss_));
681 // Return the number of Output_sections in an Output_data_list.
684 Output_segment::output_section_count_list(const Output_data_list* pdl) const
686 unsigned int count = 0;
687 for (Output_data_list::const_iterator p = pdl->begin();
691 if ((*p)->is_section())
697 // Write the segment data into *OPHDR.
699 template<int size, bool big_endian>
701 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr) const
703 ophdr->put_p_type(this->type_);
704 ophdr->put_p_offset(this->offset_);
705 ophdr->put_p_vaddr(this->vaddr_);
706 ophdr->put_p_paddr(this->paddr_);
707 ophdr->put_p_filesz(this->filesz_);
708 ophdr->put_p_memsz(this->memsz_);
709 ophdr->put_p_flags(this->flags_);
710 ophdr->put_p_align(this->align_);
713 // Write the section headers into V.
715 template<int size, bool big_endian>
717 Output_segment::write_section_headers(const Stringpool* secnamepool,
718 unsigned char* v) const
720 v = this->write_section_headers_list<size, big_endian>(secnamepool,
723 v = this->write_section_headers_list<size, big_endian>(secnamepool,
729 template<int size, bool big_endian>
731 Output_segment::write_section_headers_list(const Stringpool* secnamepool,
732 const Output_data_list* pdl,
733 unsigned char* v) const
735 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
736 for (Output_data_list::const_iterator p = pdl->begin();
740 if ((*p)->is_section())
742 Output_section* ps = static_cast<const Output_section*>(*p);
743 elfcpp::Shdr_write<size, big_endian> oshdr(v);
744 ps->write_header(secnamepool, &oshdr);
751 // Output_file methods.
753 Output_file::Output_file(const General_options& options)
755 name_(options.output_file_name()),
762 // Open the output file.
765 Output_file::open(off_t file_size)
767 this->file_size_ = file_size;
769 int mode = this->options_.is_relocatable() ? 0666 : 0777;
770 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
773 fprintf(stderr, _("%s: %s: open: %s\n"),
774 program_name, this->name_, strerror(errno));
779 // Write out one byte to make the file the right size.
780 if (::lseek(o, file_size - 1, SEEK_SET) < 0)
782 fprintf(stderr, _("%s: %s: lseek: %s\n"),
783 program_name, this->name_, strerror(errno));
787 if (::write(o, &b, 1) != 1)
789 fprintf(stderr, _("%s: %s: write: %s\n"),
790 program_name, this->name_, strerror(errno));
794 // Map the file into memory.
795 void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
797 if (base == MAP_FAILED)
799 fprintf(stderr, _("%s: %s: mmap: %s\n"),
800 program_name, this->name_, strerror(errno));
803 this->base_ = static_cast<unsigned char*>(base);
806 // Close the output file.
811 if (::munmap(this->base_, this->file_size_) < 0)
813 fprintf(stderr, _("%s: %s: munmap: %s\n"),
814 program_name, this->name_, strerror(errno));
819 if (::close(this->o_) < 0)
821 fprintf(stderr, _("%s: %s: close: %s\n"),
822 program_name, this->name_, strerror(errno));
828 // Instantiate the templates we need. We could use the configure
829 // script to restrict this to only the ones for implemented targets.
833 Output_section::add_input_section<32, false>(
836 const elfcpp::Shdr<32, false>& shdr);
840 Output_section::add_input_section<32, true>(
843 const elfcpp::Shdr<32, true>& shdr);
847 Output_section::add_input_section<64, false>(
850 const elfcpp::Shdr<64, false>& shdr);
854 Output_section::add_input_section<64, true>(
857 const elfcpp::Shdr<64, true>& shdr);
859 } // End namespace gold.