1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "parameters.h"
42 // Output_data variables.
44 bool Output_data::sizes_are_fixed;
46 // Output_data methods.
48 Output_data::~Output_data()
52 // Set the address and offset.
55 Output_data::set_address(uint64_t addr, off_t off)
57 this->address_ = addr;
60 // Let the child class know.
61 this->do_set_address(addr, off);
64 // Return the default alignment for a size--32 or 64.
67 Output_data::default_alignment(int size)
77 // Output_section_header methods. This currently assumes that the
78 // segment and section lists are complete at construction time.
80 Output_section_headers::Output_section_headers(
84 const Layout::Segment_list* segment_list,
85 const Layout::Section_list* unattached_section_list,
86 const Stringpool* secnamepool)
88 big_endian_(big_endian),
90 segment_list_(segment_list),
91 unattached_section_list_(unattached_section_list),
92 secnamepool_(secnamepool)
94 // Count all the sections. Start with 1 for the null section.
96 for (Layout::Segment_list::const_iterator p = segment_list->begin();
97 p != segment_list->end();
99 if ((*p)->type() == elfcpp::PT_LOAD)
100 count += (*p)->output_section_count();
101 count += unattached_section_list->size();
105 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
107 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
111 this->set_data_size(count * shdr_size);
114 // Write out the section headers.
117 Output_section_headers::do_write(Output_file* of)
119 if (this->size_ == 32)
121 if (this->big_endian_)
122 this->do_sized_write<32, true>(of);
124 this->do_sized_write<32, false>(of);
126 else if (this->size_ == 64)
128 if (this->big_endian_)
129 this->do_sized_write<64, true>(of);
131 this->do_sized_write<64, false>(of);
137 template<int size, bool big_endian>
139 Output_section_headers::do_sized_write(Output_file* of)
141 off_t all_shdrs_size = this->data_size();
142 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
144 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
145 unsigned char* v = view;
148 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
149 oshdr.put_sh_name(0);
150 oshdr.put_sh_type(elfcpp::SHT_NULL);
151 oshdr.put_sh_flags(0);
152 oshdr.put_sh_addr(0);
153 oshdr.put_sh_offset(0);
154 oshdr.put_sh_size(0);
155 oshdr.put_sh_link(0);
156 oshdr.put_sh_info(0);
157 oshdr.put_sh_addralign(0);
158 oshdr.put_sh_entsize(0);
164 for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
165 p != this->segment_list_->end();
167 v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
168 this->layout_, this->secnamepool_, v, &shndx
169 SELECT_SIZE_ENDIAN(size, big_endian));
170 for (Layout::Section_list::const_iterator p =
171 this->unattached_section_list_->begin();
172 p != this->unattached_section_list_->end();
175 gold_assert(shndx == (*p)->out_shndx());
176 elfcpp::Shdr_write<size, big_endian> oshdr(v);
177 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
182 of->write_output_view(this->offset(), all_shdrs_size, view);
185 // Output_segment_header methods.
187 Output_segment_headers::Output_segment_headers(
190 const Layout::Segment_list& segment_list)
191 : size_(size), big_endian_(big_endian), segment_list_(segment_list)
195 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
197 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
201 this->set_data_size(segment_list.size() * phdr_size);
205 Output_segment_headers::do_write(Output_file* of)
207 if (this->size_ == 32)
209 if (this->big_endian_)
210 this->do_sized_write<32, true>(of);
212 this->do_sized_write<32, false>(of);
214 else if (this->size_ == 64)
216 if (this->big_endian_)
217 this->do_sized_write<64, true>(of);
219 this->do_sized_write<64, false>(of);
225 template<int size, bool big_endian>
227 Output_segment_headers::do_sized_write(Output_file* of)
229 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
230 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
231 unsigned char* view = of->get_output_view(this->offset(),
233 unsigned char* v = view;
234 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
235 p != this->segment_list_.end();
238 elfcpp::Phdr_write<size, big_endian> ophdr(v);
239 (*p)->write_header(&ophdr);
243 of->write_output_view(this->offset(), all_phdrs_size, view);
246 // Output_file_header methods.
248 Output_file_header::Output_file_header(int size,
250 const Target* target,
251 const Symbol_table* symtab,
252 const Output_segment_headers* osh)
254 big_endian_(big_endian),
257 segment_header_(osh),
258 section_header_(NULL),
263 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
265 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
269 this->set_data_size(ehdr_size);
272 // Set the section table information for a file header.
275 Output_file_header::set_section_info(const Output_section_headers* shdrs,
276 const Output_section* shstrtab)
278 this->section_header_ = shdrs;
279 this->shstrtab_ = shstrtab;
282 // Write out the file header.
285 Output_file_header::do_write(Output_file* of)
287 if (this->size_ == 32)
289 if (this->big_endian_)
290 this->do_sized_write<32, true>(of);
292 this->do_sized_write<32, false>(of);
294 else if (this->size_ == 64)
296 if (this->big_endian_)
297 this->do_sized_write<64, true>(of);
299 this->do_sized_write<64, false>(of);
305 // Write out the file header with appropriate size and endianess.
307 template<int size, bool big_endian>
309 Output_file_header::do_sized_write(Output_file* of)
311 gold_assert(this->offset() == 0);
313 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
314 unsigned char* view = of->get_output_view(0, ehdr_size);
315 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
317 unsigned char e_ident[elfcpp::EI_NIDENT];
318 memset(e_ident, 0, elfcpp::EI_NIDENT);
319 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
320 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
321 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
322 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
324 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
326 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
329 e_ident[elfcpp::EI_DATA] = (big_endian
330 ? elfcpp::ELFDATA2MSB
331 : elfcpp::ELFDATA2LSB);
332 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
333 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
334 oehdr.put_e_ident(e_ident);
338 if (parameters->output_is_object())
339 e_type = elfcpp::ET_REL;
341 e_type = elfcpp::ET_EXEC;
342 oehdr.put_e_type(e_type);
344 oehdr.put_e_machine(this->target_->machine_code());
345 oehdr.put_e_version(elfcpp::EV_CURRENT);
347 // FIXME: Need to support -e, and target specific entry symbol.
348 Symbol* sym = this->symtab_->lookup("_start");
349 typename Sized_symbol<size>::Value_type v;
354 Sized_symbol<size>* ssym;
355 ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
356 sym SELECT_SIZE(size));
359 oehdr.put_e_entry(v);
361 oehdr.put_e_phoff(this->segment_header_->offset());
362 oehdr.put_e_shoff(this->section_header_->offset());
364 // FIXME: The target needs to set the flags.
365 oehdr.put_e_flags(0);
367 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
368 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
369 oehdr.put_e_phnum(this->segment_header_->data_size()
370 / elfcpp::Elf_sizes<size>::phdr_size);
371 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
372 oehdr.put_e_shnum(this->section_header_->data_size()
373 / elfcpp::Elf_sizes<size>::shdr_size);
374 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
376 of->write_output_view(0, ehdr_size, view);
379 // Output_data_const methods.
382 Output_data_const::do_write(Output_file* of)
384 of->write(this->offset(), this->data_.data(), this->data_.size());
387 // Output_data_const_buffer methods.
390 Output_data_const_buffer::do_write(Output_file* of)
392 of->write(this->offset(), this->p_, this->data_size());
395 // Output_section_data methods.
397 // Record the output section, and set the entry size and such.
400 Output_section_data::set_output_section(Output_section* os)
402 gold_assert(this->output_section_ == NULL);
403 this->output_section_ = os;
404 this->do_adjust_output_section(os);
407 // Return the section index of the output section.
410 Output_section_data::do_out_shndx() const
412 gold_assert(this->output_section_ != NULL);
413 return this->output_section_->out_shndx();
416 // Output_data_strtab methods.
418 // Set the address. We don't actually care about the address, but we
419 // do set our final size.
422 Output_data_strtab::do_set_address(uint64_t, off_t)
424 this->strtab_->set_string_offsets();
425 this->set_data_size(this->strtab_->get_strtab_size());
428 // Write out a string table.
431 Output_data_strtab::do_write(Output_file* of)
433 this->strtab_->write(of, this->offset());
436 // Output_reloc methods.
438 // Get the symbol index of a relocation.
440 template<bool dynamic, int size, bool big_endian>
442 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
446 switch (this->local_sym_index_)
452 if (this->u1_.gsym == NULL)
455 index = this->u1_.gsym->dynsym_index();
457 index = this->u1_.gsym->symtab_index();
462 index = this->u1_.os->dynsym_index();
464 index = this->u1_.os->symtab_index();
470 // FIXME: It seems that some targets may need to generate
471 // dynamic relocations against local symbols for some
472 // reasons. This will have to be addressed at some point.
476 index = this->u1_.relobj->symtab_index(this->local_sym_index_);
479 gold_assert(index != -1U);
483 // Write out the offset and info fields of a Rel or Rela relocation
486 template<bool dynamic, int size, bool big_endian>
487 template<typename Write_rel>
489 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
492 Address address = this->address_;
493 if (this->shndx_ != INVALID_CODE)
496 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
498 gold_assert(os != NULL);
499 address += os->address() + off;
501 else if (this->u2_.od != NULL)
502 address += this->u2_.od->address();
503 wr->put_r_offset(address);
504 wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(),
508 // Write out a Rel relocation.
510 template<bool dynamic, int size, bool big_endian>
512 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
513 unsigned char* pov) const
515 elfcpp::Rel_write<size, big_endian> orel(pov);
516 this->write_rel(&orel);
519 // Write out a Rela relocation.
521 template<bool dynamic, int size, bool big_endian>
523 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
524 unsigned char* pov) const
526 elfcpp::Rela_write<size, big_endian> orel(pov);
527 this->rel_.write_rel(&orel);
528 orel.put_r_addend(this->addend_);
531 // Output_data_reloc_base methods.
533 // Adjust the output section.
535 template<int sh_type, bool dynamic, int size, bool big_endian>
537 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
538 ::do_adjust_output_section(Output_section* os)
540 if (sh_type == elfcpp::SHT_REL)
541 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
542 else if (sh_type == elfcpp::SHT_RELA)
543 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
547 os->set_should_link_to_dynsym();
549 os->set_should_link_to_symtab();
552 // Write out relocation data.
554 template<int sh_type, bool dynamic, int size, bool big_endian>
556 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
559 const off_t off = this->offset();
560 const off_t oview_size = this->data_size();
561 unsigned char* const oview = of->get_output_view(off, oview_size);
563 unsigned char* pov = oview;
564 for (typename Relocs::const_iterator p = this->relocs_.begin();
565 p != this->relocs_.end();
572 gold_assert(pov - oview == oview_size);
574 of->write_output_view(off, oview_size, oview);
576 // We no longer need the relocation entries.
577 this->relocs_.clear();
580 // Output_data_got::Got_entry methods.
582 // Write out the entry.
584 template<int size, bool big_endian>
586 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
590 switch (this->local_sym_index_)
594 Symbol* gsym = this->u_.gsym;
596 // If the symbol is resolved locally, we need to write out its
597 // value. Otherwise we just write zero. The target code is
598 // responsible for creating a relocation entry to fill in the
600 if (gsym->final_value_is_known())
602 Sized_symbol<size>* sgsym;
603 // This cast is a bit ugly. We don't want to put a
604 // virtual method in Symbol, because we want Symbol to be
605 // as small as possible.
606 sgsym = static_cast<Sized_symbol<size>*>(gsym);
607 val = sgsym->value();
613 val = this->u_.constant;
620 elfcpp::Swap<size, big_endian>::writeval(pov, val);
623 // Output_data_got methods.
625 // Add an entry for a global symbol to the GOT. This returns true if
626 // this is a new GOT entry, false if the symbol already had a GOT
629 template<int size, bool big_endian>
631 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
633 if (gsym->has_got_offset())
636 this->entries_.push_back(Got_entry(gsym));
637 this->set_got_size();
638 gsym->set_got_offset(this->last_got_offset());
642 // Write out the GOT.
644 template<int size, bool big_endian>
646 Output_data_got<size, big_endian>::do_write(Output_file* of)
648 const int add = size / 8;
650 const off_t off = this->offset();
651 const off_t oview_size = this->data_size();
652 unsigned char* const oview = of->get_output_view(off, oview_size);
654 unsigned char* pov = oview;
655 for (typename Got_entries::const_iterator p = this->entries_.begin();
656 p != this->entries_.end();
663 gold_assert(pov - oview == oview_size);
665 of->write_output_view(off, oview_size, oview);
667 // We no longer need the GOT entries.
668 this->entries_.clear();
671 // Output_data_dynamic::Dynamic_entry methods.
673 // Write out the entry.
675 template<int size, bool big_endian>
677 Output_data_dynamic::Dynamic_entry::write(
679 const Stringpool* pool
680 ACCEPT_SIZE_ENDIAN) const
682 typename elfcpp::Elf_types<size>::Elf_WXword val;
683 switch (this->classification_)
689 case DYNAMIC_SECTION_ADDRESS:
690 val = this->u_.od->address();
693 case DYNAMIC_SECTION_SIZE:
694 val = this->u_.od->data_size();
699 const Sized_symbol<size>* s =
700 static_cast<const Sized_symbol<size>*>(this->u_.sym);
706 val = pool->get_offset(this->u_.str);
713 elfcpp::Dyn_write<size, big_endian> dw(pov);
714 dw.put_d_tag(this->tag_);
718 // Output_data_dynamic methods.
720 // Adjust the output section to set the entry size.
723 Output_data_dynamic::do_adjust_output_section(Output_section* os)
725 if (this->target_->get_size() == 32)
726 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
727 else if (this->target_->get_size() == 64)
728 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
733 // Set the final data size.
736 Output_data_dynamic::do_set_address(uint64_t, off_t)
738 // Add the terminating entry.
739 this->add_constant(elfcpp::DT_NULL, 0);
742 if (this->target_->get_size() == 32)
743 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
744 else if (this->target_->get_size() == 64)
745 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
748 this->set_data_size(this->entries_.size() * dyn_size);
751 // Write out the dynamic entries.
754 Output_data_dynamic::do_write(Output_file* of)
756 if (this->target_->get_size() == 32)
758 if (this->target_->is_big_endian())
759 this->sized_write<32, true>(of);
761 this->sized_write<32, false>(of);
763 else if (this->target_->get_size() == 64)
765 if (this->target_->is_big_endian())
766 this->sized_write<64, true>(of);
768 this->sized_write<64, false>(of);
774 template<int size, bool big_endian>
776 Output_data_dynamic::sized_write(Output_file* of)
778 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
780 const off_t offset = this->offset();
781 const off_t oview_size = this->data_size();
782 unsigned char* const oview = of->get_output_view(offset, oview_size);
784 unsigned char* pov = oview;
785 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
786 p != this->entries_.end();
789 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
790 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
794 gold_assert(pov - oview == oview_size);
796 of->write_output_view(offset, oview_size, oview);
798 // We no longer need the dynamic entries.
799 this->entries_.clear();
802 // Output_section::Input_section methods.
804 // Return the data size. For an input section we store the size here.
805 // For an Output_section_data, we have to ask it for the size.
808 Output_section::Input_section::data_size() const
810 if (this->is_input_section())
811 return this->u1_.data_size;
813 return this->u2_.posd->data_size();
816 // Set the address and file offset.
819 Output_section::Input_section::set_address(uint64_t addr, off_t off,
822 if (this->is_input_section())
823 this->u2_.object->set_section_offset(this->shndx_, off - secoff);
825 this->u2_.posd->set_address(addr, off);
828 // Try to turn an input address into an output address.
831 Output_section::Input_section::output_address(const Relobj* object,
834 uint64_t output_section_address,
835 uint64_t *poutput) const
837 if (!this->is_input_section())
838 return this->u2_.posd->output_address(object, shndx, offset,
839 output_section_address, poutput);
842 if (this->u2_.object != object)
845 Output_section* os = object->output_section(shndx, &output_offset);
846 gold_assert(os != NULL);
847 *poutput = output_section_address + output_offset + offset;
852 // Write out the data. We don't have to do anything for an input
853 // section--they are handled via Object::relocate--but this is where
854 // we write out the data for an Output_section_data.
857 Output_section::Input_section::write(Output_file* of)
859 if (!this->is_input_section())
860 this->u2_.posd->write(of);
863 // Output_section methods.
865 // Construct an Output_section. NAME will point into a Stringpool.
867 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
868 elfcpp::Elf_Xword flags)
882 first_input_offset_(0),
884 needs_symtab_index_(false),
885 needs_dynsym_index_(false),
886 should_link_to_symtab_(false),
887 should_link_to_dynsym_(false)
891 Output_section::~Output_section()
895 // Set the entry size.
898 Output_section::set_entsize(uint64_t v)
900 if (this->entsize_ == 0)
903 gold_assert(this->entsize_ == v);
906 // Add the input section SHNDX, with header SHDR, named SECNAME, in
907 // OBJECT, to the Output_section. Return the offset of the input
908 // section within the output section. We don't always keep track of
909 // input sections for an Output_section. Instead, each Object keeps
910 // track of the Output_section for each of its input sections.
912 template<int size, bool big_endian>
914 Output_section::add_input_section(Relobj* object, unsigned int shndx,
916 const elfcpp::Shdr<size, big_endian>& shdr)
918 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
919 if ((addralign & (addralign - 1)) != 0)
921 fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
922 program_name, object->name().c_str(),
923 static_cast<unsigned long>(addralign), secname);
927 if (addralign > this->addralign_)
928 this->addralign_ = addralign;
930 // If this is a SHF_MERGE section, we pass all the input sections to
931 // a Output_data_merge.
932 if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0)
934 if (this->add_merge_input_section(object, shndx, shdr.get_sh_flags(),
935 shdr.get_sh_entsize(),
938 // Tell the relocation routines that they need to call the
939 // output_address method to determine the final address.
944 off_t offset_in_section = this->data_size();
945 off_t aligned_offset_in_section = align_address(offset_in_section,
948 if (aligned_offset_in_section > offset_in_section
949 && (shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0
950 && object->target()->has_code_fill())
952 // We need to add some fill data. Using fill_list_ when
953 // possible is an optimization, since we will often have fill
954 // sections without input sections.
955 off_t fill_len = aligned_offset_in_section - offset_in_section;
956 if (this->input_sections_.empty())
957 this->fills_.push_back(Fill(offset_in_section, fill_len));
960 // FIXME: When relaxing, the size needs to adjust to
961 // maintain a constant alignment.
962 std::string fill_data(object->target()->code_fill(fill_len));
963 Output_data_const* odc = new Output_data_const(fill_data, 1);
964 this->input_sections_.push_back(Input_section(odc));
968 this->set_data_size(aligned_offset_in_section + shdr.get_sh_size());
970 // We need to keep track of this section if we are already keeping
971 // track of sections, or if we are relaxing. FIXME: Add test for
973 if (!this->input_sections_.empty())
974 this->input_sections_.push_back(Input_section(object, shndx,
978 return aligned_offset_in_section;
981 // Add arbitrary data to an output section.
984 Output_section::add_output_section_data(Output_section_data* posd)
986 Input_section inp(posd);
987 this->add_output_section_data(&inp);
990 // Add arbitrary data to an output section by Input_section.
993 Output_section::add_output_section_data(Input_section* inp)
995 if (this->input_sections_.empty())
996 this->first_input_offset_ = this->data_size();
998 this->input_sections_.push_back(*inp);
1000 uint64_t addralign = inp->addralign();
1001 if (addralign > this->addralign_)
1002 this->addralign_ = addralign;
1004 inp->set_output_section(this);
1007 // Add a merge section to an output section.
1010 Output_section::add_output_merge_section(Output_section_data* posd,
1011 bool is_string, uint64_t entsize)
1013 Input_section inp(posd, is_string, entsize);
1014 this->add_output_section_data(&inp);
1017 // Add an input section to a SHF_MERGE section.
1020 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1021 uint64_t flags, uint64_t entsize,
1024 // We only merge constants if the alignment is not more than the
1025 // entry size. This could be handled, but it's unusual.
1026 if (addralign > entsize)
1029 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1030 Input_section_list::iterator p;
1031 for (p = this->input_sections_.begin();
1032 p != this->input_sections_.end();
1034 if (p->is_merge_section(is_string, entsize))
1037 // We handle the actual constant merging in Output_merge_data or
1038 // Output_merge_string_data.
1039 if (p != this->input_sections_.end())
1040 p->add_input_section(object, shndx);
1043 Output_section_data* posd;
1045 posd = new Output_merge_data(entsize);
1046 else if (entsize == 1)
1047 posd = new Output_merge_string<char>();
1048 else if (entsize == 2)
1049 posd = new Output_merge_string<uint16_t>();
1050 else if (entsize == 4)
1051 posd = new Output_merge_string<uint32_t>();
1055 this->add_output_merge_section(posd, is_string, entsize);
1056 posd->add_input_section(object, shndx);
1062 // Return the output virtual address of OFFSET relative to the start
1063 // of input section SHNDX in object OBJECT.
1066 Output_section::output_address(const Relobj* object, unsigned int shndx,
1069 uint64_t addr = this->address() + this->first_input_offset_;
1070 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1071 p != this->input_sections_.end();
1074 addr = align_address(addr, p->addralign());
1076 if (p->output_address(object, shndx, offset, addr, &output))
1078 addr += p->data_size();
1081 // If we get here, it means that we don't know the mapping for this
1082 // input section. This might happen in principle if
1083 // add_input_section were called before add_output_section_data.
1084 // But it should never actually happen.
1089 // Set the address of an Output_section. This is where we handle
1090 // setting the addresses of any Output_section_data objects.
1093 Output_section::do_set_address(uint64_t address, off_t startoff)
1095 if (this->input_sections_.empty())
1098 off_t off = startoff + this->first_input_offset_;
1099 for (Input_section_list::iterator p = this->input_sections_.begin();
1100 p != this->input_sections_.end();
1103 off = align_address(off, p->addralign());
1104 p->set_address(address + (off - startoff), off, startoff);
1105 off += p->data_size();
1108 this->set_data_size(off - startoff);
1111 // Write the section header to *OSHDR.
1113 template<int size, bool big_endian>
1115 Output_section::write_header(const Layout* layout,
1116 const Stringpool* secnamepool,
1117 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1119 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1120 oshdr->put_sh_type(this->type_);
1121 oshdr->put_sh_flags(this->flags_);
1122 oshdr->put_sh_addr(this->address());
1123 oshdr->put_sh_offset(this->offset());
1124 oshdr->put_sh_size(this->data_size());
1125 if (this->link_section_ != NULL)
1126 oshdr->put_sh_link(this->link_section_->out_shndx());
1127 else if (this->should_link_to_symtab_)
1128 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1129 else if (this->should_link_to_dynsym_)
1130 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1132 oshdr->put_sh_link(this->link_);
1133 if (this->info_section_ != NULL)
1134 oshdr->put_sh_info(this->info_section_->out_shndx());
1136 oshdr->put_sh_info(this->info_);
1137 oshdr->put_sh_addralign(this->addralign_);
1138 oshdr->put_sh_entsize(this->entsize_);
1141 // Write out the data. For input sections the data is written out by
1142 // Object::relocate, but we have to handle Output_section_data objects
1146 Output_section::do_write(Output_file* of)
1148 off_t output_section_file_offset = this->offset();
1149 for (Fill_list::iterator p = this->fills_.begin();
1150 p != this->fills_.end();
1153 std::string fill_data(of->target()->code_fill(p->length()));
1154 of->write(output_section_file_offset + p->section_offset(),
1155 fill_data.data(), fill_data.size());
1158 for (Input_section_list::iterator p = this->input_sections_.begin();
1159 p != this->input_sections_.end();
1164 // Output segment methods.
1166 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1177 is_align_known_(false)
1181 // Add an Output_section to an Output_segment.
1184 Output_segment::add_output_section(Output_section* os,
1185 elfcpp::Elf_Word seg_flags,
1188 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1189 gold_assert(!this->is_align_known_);
1191 // Update the segment flags.
1192 this->flags_ |= seg_flags;
1194 Output_segment::Output_data_list* pdl;
1195 if (os->type() == elfcpp::SHT_NOBITS)
1196 pdl = &this->output_bss_;
1198 pdl = &this->output_data_;
1200 // So that PT_NOTE segments will work correctly, we need to ensure
1201 // that all SHT_NOTE sections are adjacent. This will normally
1202 // happen automatically, because all the SHT_NOTE input sections
1203 // will wind up in the same output section. However, it is possible
1204 // for multiple SHT_NOTE input sections to have different section
1205 // flags, and thus be in different output sections, but for the
1206 // different section flags to map into the same segment flags and
1207 // thus the same output segment.
1209 // Note that while there may be many input sections in an output
1210 // section, there are normally only a few output sections in an
1211 // output segment. This loop is expected to be fast.
1213 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1215 Output_segment::Output_data_list::iterator p = pdl->end();
1219 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1221 // We don't worry about the FRONT parameter.
1227 while (p != pdl->begin());
1230 // Similarly, so that PT_TLS segments will work, we need to group
1231 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1232 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1233 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
1235 if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
1237 pdl = &this->output_data_;
1238 bool nobits = os->type() == elfcpp::SHT_NOBITS;
1239 bool sawtls = false;
1240 Output_segment::Output_data_list::iterator p = pdl->end();
1245 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1248 // Put a NOBITS section after the first TLS section.
1249 // But a PROGBITS section after the first TLS/PROGBITS
1251 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1255 // If we've gone past the TLS sections, but we've seen a
1256 // TLS section, then we need to insert this section now.
1262 // We don't worry about the FRONT parameter.
1268 while (p != pdl->begin());
1270 // There are no TLS sections yet; put this one at the requested
1271 // location in the section list.
1275 pdl->push_front(os);
1280 // Add an Output_data (which is not an Output_section) to the start of
1284 Output_segment::add_initial_output_data(Output_data* od)
1286 gold_assert(!this->is_align_known_);
1287 this->output_data_.push_front(od);
1290 // Return the maximum alignment of the Output_data in Output_segment.
1291 // Once we compute this, we prohibit new sections from being added.
1294 Output_segment::addralign()
1296 if (!this->is_align_known_)
1300 addralign = Output_segment::maximum_alignment(&this->output_data_);
1301 if (addralign > this->align_)
1302 this->align_ = addralign;
1304 addralign = Output_segment::maximum_alignment(&this->output_bss_);
1305 if (addralign > this->align_)
1306 this->align_ = addralign;
1308 this->is_align_known_ = true;
1311 return this->align_;
1314 // Return the maximum alignment of a list of Output_data.
1317 Output_segment::maximum_alignment(const Output_data_list* pdl)
1320 for (Output_data_list::const_iterator p = pdl->begin();
1324 uint64_t addralign = (*p)->addralign();
1325 if (addralign > ret)
1331 // Set the section addresses for an Output_segment. ADDR is the
1332 // address and *POFF is the file offset. Set the section indexes
1333 // starting with *PSHNDX. Return the address of the immediately
1334 // following segment. Update *POFF and *PSHNDX.
1337 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
1338 unsigned int* pshndx)
1340 gold_assert(this->type_ == elfcpp::PT_LOAD);
1342 this->vaddr_ = addr;
1343 this->paddr_ = addr;
1345 off_t orig_off = *poff;
1346 this->offset_ = orig_off;
1348 *poff = align_address(*poff, this->addralign());
1350 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
1352 this->filesz_ = *poff - orig_off;
1356 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
1358 this->memsz_ = *poff - orig_off;
1360 // Ignore the file offset adjustments made by the BSS Output_data
1367 // Set the addresses and file offsets in a list of Output_data
1371 Output_segment::set_section_list_addresses(Output_data_list* pdl,
1372 uint64_t addr, off_t* poff,
1373 unsigned int* pshndx)
1375 off_t startoff = *poff;
1377 off_t off = startoff;
1378 for (Output_data_list::iterator p = pdl->begin();
1382 off = align_address(off, (*p)->addralign());
1383 (*p)->set_address(addr + (off - startoff), off);
1385 // Unless this is a PT_TLS segment, we want to ignore the size
1386 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
1387 // affect the size of a PT_LOAD segment.
1388 if (this->type_ == elfcpp::PT_TLS
1389 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
1390 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
1391 off += (*p)->data_size();
1393 if ((*p)->is_section())
1395 (*p)->set_out_shndx(*pshndx);
1401 return addr + (off - startoff);
1404 // For a non-PT_LOAD segment, set the offset from the sections, if
1408 Output_segment::set_offset()
1410 gold_assert(this->type_ != elfcpp::PT_LOAD);
1412 if (this->output_data_.empty() && this->output_bss_.empty())
1423 const Output_data* first;
1424 if (this->output_data_.empty())
1425 first = this->output_bss_.front();
1427 first = this->output_data_.front();
1428 this->vaddr_ = first->address();
1429 this->paddr_ = this->vaddr_;
1430 this->offset_ = first->offset();
1432 if (this->output_data_.empty())
1436 const Output_data* last_data = this->output_data_.back();
1437 this->filesz_ = (last_data->address()
1438 + last_data->data_size()
1442 const Output_data* last;
1443 if (this->output_bss_.empty())
1444 last = this->output_data_.back();
1446 last = this->output_bss_.back();
1447 this->memsz_ = (last->address()
1452 // Return the number of Output_sections in an Output_segment.
1455 Output_segment::output_section_count() const
1457 return (this->output_section_count_list(&this->output_data_)
1458 + this->output_section_count_list(&this->output_bss_));
1461 // Return the number of Output_sections in an Output_data_list.
1464 Output_segment::output_section_count_list(const Output_data_list* pdl) const
1466 unsigned int count = 0;
1467 for (Output_data_list::const_iterator p = pdl->begin();
1471 if ((*p)->is_section())
1477 // Write the segment data into *OPHDR.
1479 template<int size, bool big_endian>
1481 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
1483 ophdr->put_p_type(this->type_);
1484 ophdr->put_p_offset(this->offset_);
1485 ophdr->put_p_vaddr(this->vaddr_);
1486 ophdr->put_p_paddr(this->paddr_);
1487 ophdr->put_p_filesz(this->filesz_);
1488 ophdr->put_p_memsz(this->memsz_);
1489 ophdr->put_p_flags(this->flags_);
1490 ophdr->put_p_align(this->addralign());
1493 // Write the section headers into V.
1495 template<int size, bool big_endian>
1497 Output_segment::write_section_headers(const Layout* layout,
1498 const Stringpool* secnamepool,
1500 unsigned int *pshndx
1501 ACCEPT_SIZE_ENDIAN) const
1503 // Every section that is attached to a segment must be attached to a
1504 // PT_LOAD segment, so we only write out section headers for PT_LOAD
1506 if (this->type_ != elfcpp::PT_LOAD)
1509 v = this->write_section_headers_list
1510 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1511 layout, secnamepool, &this->output_data_, v, pshndx
1512 SELECT_SIZE_ENDIAN(size, big_endian));
1513 v = this->write_section_headers_list
1514 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1515 layout, secnamepool, &this->output_bss_, v, pshndx
1516 SELECT_SIZE_ENDIAN(size, big_endian));
1520 template<int size, bool big_endian>
1522 Output_segment::write_section_headers_list(const Layout* layout,
1523 const Stringpool* secnamepool,
1524 const Output_data_list* pdl,
1526 unsigned int* pshndx
1527 ACCEPT_SIZE_ENDIAN) const
1529 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1530 for (Output_data_list::const_iterator p = pdl->begin();
1534 if ((*p)->is_section())
1536 const Output_section* ps = static_cast<const Output_section*>(*p);
1537 gold_assert(*pshndx == ps->out_shndx());
1538 elfcpp::Shdr_write<size, big_endian> oshdr(v);
1539 ps->write_header(layout, secnamepool, &oshdr);
1547 // Output_file methods.
1549 Output_file::Output_file(const General_options& options, Target* target)
1550 : options_(options),
1552 name_(options.output_file_name()),
1559 // Open the output file.
1562 Output_file::open(off_t file_size)
1564 this->file_size_ = file_size;
1566 int mode = parameters->output_is_object() ? 0666 : 0777;
1567 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1570 fprintf(stderr, _("%s: %s: open: %s\n"),
1571 program_name, this->name_, strerror(errno));
1576 // Write out one byte to make the file the right size.
1577 if (::lseek(o, file_size - 1, SEEK_SET) < 0)
1579 fprintf(stderr, _("%s: %s: lseek: %s\n"),
1580 program_name, this->name_, strerror(errno));
1584 if (::write(o, &b, 1) != 1)
1586 fprintf(stderr, _("%s: %s: write: %s\n"),
1587 program_name, this->name_, strerror(errno));
1591 // Map the file into memory.
1592 void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
1594 if (base == MAP_FAILED)
1596 fprintf(stderr, _("%s: %s: mmap: %s\n"),
1597 program_name, this->name_, strerror(errno));
1600 this->base_ = static_cast<unsigned char*>(base);
1603 // Close the output file.
1606 Output_file::close()
1608 if (::munmap(this->base_, this->file_size_) < 0)
1610 fprintf(stderr, _("%s: %s: munmap: %s\n"),
1611 program_name, this->name_, strerror(errno));
1616 if (::close(this->o_) < 0)
1618 fprintf(stderr, _("%s: %s: close: %s\n"),
1619 program_name, this->name_, strerror(errno));
1625 // Instantiate the templates we need. We could use the configure
1626 // script to restrict this to only the ones for implemented targets.
1628 #ifdef HAVE_TARGET_32_LITTLE
1631 Output_section::add_input_section<32, false>(
1634 const char* secname,
1635 const elfcpp::Shdr<32, false>& shdr);
1638 #ifdef HAVE_TARGET_32_BIG
1641 Output_section::add_input_section<32, true>(
1644 const char* secname,
1645 const elfcpp::Shdr<32, true>& shdr);
1648 #ifdef HAVE_TARGET_64_LITTLE
1651 Output_section::add_input_section<64, false>(
1654 const char* secname,
1655 const elfcpp::Shdr<64, false>& shdr);
1658 #ifdef HAVE_TARGET_64_BIG
1661 Output_section::add_input_section<64, true>(
1664 const char* secname,
1665 const elfcpp::Shdr<64, true>& shdr);
1668 #ifdef HAVE_TARGET_32_LITTLE
1670 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
1673 #ifdef HAVE_TARGET_32_BIG
1675 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
1678 #ifdef HAVE_TARGET_64_LITTLE
1680 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
1683 #ifdef HAVE_TARGET_64_BIG
1685 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
1688 #ifdef HAVE_TARGET_32_LITTLE
1690 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
1693 #ifdef HAVE_TARGET_32_BIG
1695 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
1698 #ifdef HAVE_TARGET_64_LITTLE
1700 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
1703 #ifdef HAVE_TARGET_64_BIG
1705 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
1708 #ifdef HAVE_TARGET_32_LITTLE
1710 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
1713 #ifdef HAVE_TARGET_32_BIG
1715 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
1718 #ifdef HAVE_TARGET_64_LITTLE
1720 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
1723 #ifdef HAVE_TARGET_64_BIG
1725 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
1728 #ifdef HAVE_TARGET_32_LITTLE
1730 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
1733 #ifdef HAVE_TARGET_32_BIG
1735 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
1738 #ifdef HAVE_TARGET_64_LITTLE
1740 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
1743 #ifdef HAVE_TARGET_64_BIG
1745 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
1748 #ifdef HAVE_TARGET_32_LITTLE
1750 class Output_data_got<32, false>;
1753 #ifdef HAVE_TARGET_32_BIG
1755 class Output_data_got<32, true>;
1758 #ifdef HAVE_TARGET_64_LITTLE
1760 class Output_data_got<64, false>;
1763 #ifdef HAVE_TARGET_64_BIG
1765 class Output_data_got<64, true>;
1768 } // End namespace gold.