* config/tc-avr.c (mcu_types): Add atmega32m1.
[binutils.git] / gold / output.cc
blobd4fc2a7a9a376e8ec9004fc2f9b14c0d6a83561f
1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008 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.
23 #include "gold.h"
25 #include <cstdlib>
26 #include <cstring>
27 #include <cerrno>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <algorithm>
33 #include "libiberty.h" // for unlink_if_ordinary()
35 #include "parameters.h"
36 #include "object.h"
37 #include "symtab.h"
38 #include "reloc.h"
39 #include "merge.h"
40 #include "output.h"
42 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
43 #ifndef MAP_ANONYMOUS
44 # define MAP_ANONYMOUS MAP_ANON
45 #endif
47 namespace gold
50 // Output_data variables.
52 bool Output_data::allocated_sizes_are_fixed;
54 // Output_data methods.
56 Output_data::~Output_data()
60 // Return the default alignment for the target size.
62 uint64_t
63 Output_data::default_alignment()
65 return Output_data::default_alignment_for_size(
66 parameters->target().get_size());
69 // Return the default alignment for a size--32 or 64.
71 uint64_t
72 Output_data::default_alignment_for_size(int size)
74 if (size == 32)
75 return 4;
76 else if (size == 64)
77 return 8;
78 else
79 gold_unreachable();
82 // Output_section_header methods. This currently assumes that the
83 // segment and section lists are complete at construction time.
85 Output_section_headers::Output_section_headers(
86 const Layout* layout,
87 const Layout::Segment_list* segment_list,
88 const Layout::Section_list* section_list,
89 const Layout::Section_list* unattached_section_list,
90 const Stringpool* secnamepool)
91 : layout_(layout),
92 segment_list_(segment_list),
93 section_list_(section_list),
94 unattached_section_list_(unattached_section_list),
95 secnamepool_(secnamepool)
97 // Count all the sections. Start with 1 for the null section.
98 off_t count = 1;
99 if (!parameters->options().relocatable())
101 for (Layout::Segment_list::const_iterator p = segment_list->begin();
102 p != segment_list->end();
103 ++p)
104 if ((*p)->type() == elfcpp::PT_LOAD)
105 count += (*p)->output_section_count();
107 else
109 for (Layout::Section_list::const_iterator p = section_list->begin();
110 p != section_list->end();
111 ++p)
112 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
113 ++count;
115 count += unattached_section_list->size();
117 const int size = parameters->target().get_size();
118 int shdr_size;
119 if (size == 32)
120 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
121 else if (size == 64)
122 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
123 else
124 gold_unreachable();
126 this->set_data_size(count * shdr_size);
129 // Write out the section headers.
131 void
132 Output_section_headers::do_write(Output_file* of)
134 switch (parameters->size_and_endianness())
136 #ifdef HAVE_TARGET_32_LITTLE
137 case Parameters::TARGET_32_LITTLE:
138 this->do_sized_write<32, false>(of);
139 break;
140 #endif
141 #ifdef HAVE_TARGET_32_BIG
142 case Parameters::TARGET_32_BIG:
143 this->do_sized_write<32, true>(of);
144 break;
145 #endif
146 #ifdef HAVE_TARGET_64_LITTLE
147 case Parameters::TARGET_64_LITTLE:
148 this->do_sized_write<64, false>(of);
149 break;
150 #endif
151 #ifdef HAVE_TARGET_64_BIG
152 case Parameters::TARGET_64_BIG:
153 this->do_sized_write<64, true>(of);
154 break;
155 #endif
156 default:
157 gold_unreachable();
161 template<int size, bool big_endian>
162 void
163 Output_section_headers::do_sized_write(Output_file* of)
165 off_t all_shdrs_size = this->data_size();
166 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
168 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
169 unsigned char* v = view;
172 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
173 oshdr.put_sh_name(0);
174 oshdr.put_sh_type(elfcpp::SHT_NULL);
175 oshdr.put_sh_flags(0);
176 oshdr.put_sh_addr(0);
177 oshdr.put_sh_offset(0);
178 oshdr.put_sh_size(0);
179 oshdr.put_sh_link(0);
180 oshdr.put_sh_info(0);
181 oshdr.put_sh_addralign(0);
182 oshdr.put_sh_entsize(0);
185 v += shdr_size;
187 unsigned int shndx = 1;
188 if (!parameters->options().relocatable())
190 for (Layout::Segment_list::const_iterator p =
191 this->segment_list_->begin();
192 p != this->segment_list_->end();
193 ++p)
194 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
195 this->secnamepool_,
197 &shndx);
199 else
201 for (Layout::Section_list::const_iterator p =
202 this->section_list_->begin();
203 p != this->section_list_->end();
204 ++p)
206 // We do unallocated sections below, except that group
207 // sections have to come first.
208 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
209 && (*p)->type() != elfcpp::SHT_GROUP)
210 continue;
211 gold_assert(shndx == (*p)->out_shndx());
212 elfcpp::Shdr_write<size, big_endian> oshdr(v);
213 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
214 v += shdr_size;
215 ++shndx;
219 for (Layout::Section_list::const_iterator p =
220 this->unattached_section_list_->begin();
221 p != this->unattached_section_list_->end();
222 ++p)
224 // For a relocatable link, we did unallocated group sections
225 // above, since they have to come first.
226 if ((*p)->type() == elfcpp::SHT_GROUP
227 && parameters->options().relocatable())
228 continue;
229 gold_assert(shndx == (*p)->out_shndx());
230 elfcpp::Shdr_write<size, big_endian> oshdr(v);
231 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
232 v += shdr_size;
233 ++shndx;
236 of->write_output_view(this->offset(), all_shdrs_size, view);
239 // Output_segment_header methods.
241 Output_segment_headers::Output_segment_headers(
242 const Layout::Segment_list& segment_list)
243 : segment_list_(segment_list)
245 const int size = parameters->target().get_size();
246 int phdr_size;
247 if (size == 32)
248 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
249 else if (size == 64)
250 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
251 else
252 gold_unreachable();
254 this->set_data_size(segment_list.size() * phdr_size);
257 void
258 Output_segment_headers::do_write(Output_file* of)
260 switch (parameters->size_and_endianness())
262 #ifdef HAVE_TARGET_32_LITTLE
263 case Parameters::TARGET_32_LITTLE:
264 this->do_sized_write<32, false>(of);
265 break;
266 #endif
267 #ifdef HAVE_TARGET_32_BIG
268 case Parameters::TARGET_32_BIG:
269 this->do_sized_write<32, true>(of);
270 break;
271 #endif
272 #ifdef HAVE_TARGET_64_LITTLE
273 case Parameters::TARGET_64_LITTLE:
274 this->do_sized_write<64, false>(of);
275 break;
276 #endif
277 #ifdef HAVE_TARGET_64_BIG
278 case Parameters::TARGET_64_BIG:
279 this->do_sized_write<64, true>(of);
280 break;
281 #endif
282 default:
283 gold_unreachable();
287 template<int size, bool big_endian>
288 void
289 Output_segment_headers::do_sized_write(Output_file* of)
291 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
292 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
293 gold_assert(all_phdrs_size == this->data_size());
294 unsigned char* view = of->get_output_view(this->offset(),
295 all_phdrs_size);
296 unsigned char* v = view;
297 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
298 p != this->segment_list_.end();
299 ++p)
301 elfcpp::Phdr_write<size, big_endian> ophdr(v);
302 (*p)->write_header(&ophdr);
303 v += phdr_size;
306 gold_assert(v - view == all_phdrs_size);
308 of->write_output_view(this->offset(), all_phdrs_size, view);
311 // Output_file_header methods.
313 Output_file_header::Output_file_header(const Target* target,
314 const Symbol_table* symtab,
315 const Output_segment_headers* osh,
316 const char* entry)
317 : target_(target),
318 symtab_(symtab),
319 segment_header_(osh),
320 section_header_(NULL),
321 shstrtab_(NULL),
322 entry_(entry)
324 const int size = parameters->target().get_size();
325 int ehdr_size;
326 if (size == 32)
327 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
328 else if (size == 64)
329 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
330 else
331 gold_unreachable();
333 this->set_data_size(ehdr_size);
336 // Set the section table information for a file header.
338 void
339 Output_file_header::set_section_info(const Output_section_headers* shdrs,
340 const Output_section* shstrtab)
342 this->section_header_ = shdrs;
343 this->shstrtab_ = shstrtab;
346 // Write out the file header.
348 void
349 Output_file_header::do_write(Output_file* of)
351 gold_assert(this->offset() == 0);
353 switch (parameters->size_and_endianness())
355 #ifdef HAVE_TARGET_32_LITTLE
356 case Parameters::TARGET_32_LITTLE:
357 this->do_sized_write<32, false>(of);
358 break;
359 #endif
360 #ifdef HAVE_TARGET_32_BIG
361 case Parameters::TARGET_32_BIG:
362 this->do_sized_write<32, true>(of);
363 break;
364 #endif
365 #ifdef HAVE_TARGET_64_LITTLE
366 case Parameters::TARGET_64_LITTLE:
367 this->do_sized_write<64, false>(of);
368 break;
369 #endif
370 #ifdef HAVE_TARGET_64_BIG
371 case Parameters::TARGET_64_BIG:
372 this->do_sized_write<64, true>(of);
373 break;
374 #endif
375 default:
376 gold_unreachable();
380 // Write out the file header with appropriate size and endianess.
382 template<int size, bool big_endian>
383 void
384 Output_file_header::do_sized_write(Output_file* of)
386 gold_assert(this->offset() == 0);
388 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
389 unsigned char* view = of->get_output_view(0, ehdr_size);
390 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
392 unsigned char e_ident[elfcpp::EI_NIDENT];
393 memset(e_ident, 0, elfcpp::EI_NIDENT);
394 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
395 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
396 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
397 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
398 if (size == 32)
399 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
400 else if (size == 64)
401 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
402 else
403 gold_unreachable();
404 e_ident[elfcpp::EI_DATA] = (big_endian
405 ? elfcpp::ELFDATA2MSB
406 : elfcpp::ELFDATA2LSB);
407 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
408 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
409 oehdr.put_e_ident(e_ident);
411 elfcpp::ET e_type;
412 if (parameters->options().relocatable())
413 e_type = elfcpp::ET_REL;
414 else if (parameters->options().shared())
415 e_type = elfcpp::ET_DYN;
416 else
417 e_type = elfcpp::ET_EXEC;
418 oehdr.put_e_type(e_type);
420 oehdr.put_e_machine(this->target_->machine_code());
421 oehdr.put_e_version(elfcpp::EV_CURRENT);
423 oehdr.put_e_entry(this->entry<size>());
425 if (this->segment_header_ == NULL)
426 oehdr.put_e_phoff(0);
427 else
428 oehdr.put_e_phoff(this->segment_header_->offset());
430 oehdr.put_e_shoff(this->section_header_->offset());
432 // FIXME: The target needs to set the flags.
433 oehdr.put_e_flags(0);
435 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
437 if (this->segment_header_ == NULL)
439 oehdr.put_e_phentsize(0);
440 oehdr.put_e_phnum(0);
442 else
444 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
445 oehdr.put_e_phnum(this->segment_header_->data_size()
446 / elfcpp::Elf_sizes<size>::phdr_size);
449 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
450 oehdr.put_e_shnum(this->section_header_->data_size()
451 / elfcpp::Elf_sizes<size>::shdr_size);
452 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
454 of->write_output_view(0, ehdr_size, view);
457 // Return the value to use for the entry address. THIS->ENTRY_ is the
458 // symbol specified on the command line, if any.
460 template<int size>
461 typename elfcpp::Elf_types<size>::Elf_Addr
462 Output_file_header::entry()
464 const bool should_issue_warning = (this->entry_ != NULL
465 && !parameters->options().relocatable()
466 && !parameters->options().shared());
468 // FIXME: Need to support target specific entry symbol.
469 const char* entry = this->entry_;
470 if (entry == NULL)
471 entry = "_start";
473 Symbol* sym = this->symtab_->lookup(entry);
475 typename Sized_symbol<size>::Value_type v;
476 if (sym != NULL)
478 Sized_symbol<size>* ssym;
479 ssym = this->symtab_->get_sized_symbol<size>(sym);
480 if (!ssym->is_defined() && should_issue_warning)
481 gold_warning("entry symbol '%s' exists but is not defined", entry);
482 v = ssym->value();
484 else
486 // We couldn't find the entry symbol. See if we can parse it as
487 // a number. This supports, e.g., -e 0x1000.
488 char* endptr;
489 v = strtoull(entry, &endptr, 0);
490 if (*endptr != '\0')
492 if (should_issue_warning)
493 gold_warning("cannot find entry symbol '%s'", entry);
494 v = 0;
498 return v;
501 // Output_data_const methods.
503 void
504 Output_data_const::do_write(Output_file* of)
506 of->write(this->offset(), this->data_.data(), this->data_.size());
509 // Output_data_const_buffer methods.
511 void
512 Output_data_const_buffer::do_write(Output_file* of)
514 of->write(this->offset(), this->p_, this->data_size());
517 // Output_section_data methods.
519 // Record the output section, and set the entry size and such.
521 void
522 Output_section_data::set_output_section(Output_section* os)
524 gold_assert(this->output_section_ == NULL);
525 this->output_section_ = os;
526 this->do_adjust_output_section(os);
529 // Return the section index of the output section.
531 unsigned int
532 Output_section_data::do_out_shndx() const
534 gold_assert(this->output_section_ != NULL);
535 return this->output_section_->out_shndx();
538 // Output_data_strtab methods.
540 // Set the final data size.
542 void
543 Output_data_strtab::set_final_data_size()
545 this->strtab_->set_string_offsets();
546 this->set_data_size(this->strtab_->get_strtab_size());
549 // Write out a string table.
551 void
552 Output_data_strtab::do_write(Output_file* of)
554 this->strtab_->write(of, this->offset());
557 // Output_reloc methods.
559 // A reloc against a global symbol.
561 template<bool dynamic, int size, bool big_endian>
562 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
563 Symbol* gsym,
564 unsigned int type,
565 Output_data* od,
566 Address address,
567 bool is_relative)
568 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
569 is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
571 // this->type_ is a bitfield; make sure TYPE fits.
572 gold_assert(this->type_ == type);
573 this->u1_.gsym = gsym;
574 this->u2_.od = od;
575 if (dynamic)
576 this->set_needs_dynsym_index();
579 template<bool dynamic, int size, bool big_endian>
580 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
581 Symbol* gsym,
582 unsigned int type,
583 Relobj* relobj,
584 unsigned int shndx,
585 Address address,
586 bool is_relative)
587 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
588 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
590 gold_assert(shndx != INVALID_CODE);
591 // this->type_ is a bitfield; make sure TYPE fits.
592 gold_assert(this->type_ == type);
593 this->u1_.gsym = gsym;
594 this->u2_.relobj = relobj;
595 if (dynamic)
596 this->set_needs_dynsym_index();
599 // A reloc against a local symbol.
601 template<bool dynamic, int size, bool big_endian>
602 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
603 Sized_relobj<size, big_endian>* relobj,
604 unsigned int local_sym_index,
605 unsigned int type,
606 Output_data* od,
607 Address address,
608 bool is_relative,
609 bool is_section_symbol)
610 : address_(address), local_sym_index_(local_sym_index), type_(type),
611 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
612 shndx_(INVALID_CODE)
614 gold_assert(local_sym_index != GSYM_CODE
615 && local_sym_index != INVALID_CODE);
616 // this->type_ is a bitfield; make sure TYPE fits.
617 gold_assert(this->type_ == type);
618 this->u1_.relobj = relobj;
619 this->u2_.od = od;
620 if (dynamic)
621 this->set_needs_dynsym_index();
624 template<bool dynamic, int size, bool big_endian>
625 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
626 Sized_relobj<size, big_endian>* relobj,
627 unsigned int local_sym_index,
628 unsigned int type,
629 unsigned int shndx,
630 Address address,
631 bool is_relative,
632 bool is_section_symbol)
633 : address_(address), local_sym_index_(local_sym_index), type_(type),
634 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
635 shndx_(shndx)
637 gold_assert(local_sym_index != GSYM_CODE
638 && local_sym_index != INVALID_CODE);
639 gold_assert(shndx != INVALID_CODE);
640 // this->type_ is a bitfield; make sure TYPE fits.
641 gold_assert(this->type_ == type);
642 this->u1_.relobj = relobj;
643 this->u2_.relobj = relobj;
644 if (dynamic)
645 this->set_needs_dynsym_index();
648 // A reloc against the STT_SECTION symbol of an output section.
650 template<bool dynamic, int size, bool big_endian>
651 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
652 Output_section* os,
653 unsigned int type,
654 Output_data* od,
655 Address address)
656 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
657 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
659 // this->type_ is a bitfield; make sure TYPE fits.
660 gold_assert(this->type_ == type);
661 this->u1_.os = os;
662 this->u2_.od = od;
663 if (dynamic)
664 this->set_needs_dynsym_index();
665 else
666 os->set_needs_symtab_index();
669 template<bool dynamic, int size, bool big_endian>
670 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
671 Output_section* os,
672 unsigned int type,
673 Relobj* relobj,
674 unsigned int shndx,
675 Address address)
676 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
677 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
679 gold_assert(shndx != INVALID_CODE);
680 // this->type_ is a bitfield; make sure TYPE fits.
681 gold_assert(this->type_ == type);
682 this->u1_.os = os;
683 this->u2_.relobj = relobj;
684 if (dynamic)
685 this->set_needs_dynsym_index();
686 else
687 os->set_needs_symtab_index();
690 // Record that we need a dynamic symbol index for this relocation.
692 template<bool dynamic, int size, bool big_endian>
693 void
694 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
695 set_needs_dynsym_index()
697 if (this->is_relative_)
698 return;
699 switch (this->local_sym_index_)
701 case INVALID_CODE:
702 gold_unreachable();
704 case GSYM_CODE:
705 this->u1_.gsym->set_needs_dynsym_entry();
706 break;
708 case SECTION_CODE:
709 this->u1_.os->set_needs_dynsym_index();
710 break;
712 case 0:
713 break;
715 default:
717 const unsigned int lsi = this->local_sym_index_;
718 if (!this->is_section_symbol_)
719 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
720 else
722 section_offset_type dummy;
723 Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
724 gold_assert(os != NULL);
725 os->set_needs_dynsym_index();
728 break;
732 // Get the symbol index of a relocation.
734 template<bool dynamic, int size, bool big_endian>
735 unsigned int
736 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
737 const
739 unsigned int index;
740 switch (this->local_sym_index_)
742 case INVALID_CODE:
743 gold_unreachable();
745 case GSYM_CODE:
746 if (this->u1_.gsym == NULL)
747 index = 0;
748 else if (dynamic)
749 index = this->u1_.gsym->dynsym_index();
750 else
751 index = this->u1_.gsym->symtab_index();
752 break;
754 case SECTION_CODE:
755 if (dynamic)
756 index = this->u1_.os->dynsym_index();
757 else
758 index = this->u1_.os->symtab_index();
759 break;
761 case 0:
762 // Relocations without symbols use a symbol index of 0.
763 index = 0;
764 break;
766 default:
768 const unsigned int lsi = this->local_sym_index_;
769 if (!this->is_section_symbol_)
771 if (dynamic)
772 index = this->u1_.relobj->dynsym_index(lsi);
773 else
774 index = this->u1_.relobj->symtab_index(lsi);
776 else
778 section_offset_type dummy;
779 Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
780 gold_assert(os != NULL);
781 if (dynamic)
782 index = os->dynsym_index();
783 else
784 index = os->symtab_index();
787 break;
789 gold_assert(index != -1U);
790 return index;
793 // For a local section symbol, get the section offset of the input
794 // section within the output section.
796 template<bool dynamic, int size, bool big_endian>
797 section_offset_type
798 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
799 local_section_offset() const
801 const unsigned int lsi = this->local_sym_index_;
802 section_offset_type offset;
803 Output_section* os = this->u1_.relobj->output_section(lsi, &offset);
804 gold_assert(os != NULL && offset != -1);
805 return offset;
808 // Write out the offset and info fields of a Rel or Rela relocation
809 // entry.
811 template<bool dynamic, int size, bool big_endian>
812 template<typename Write_rel>
813 void
814 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
815 Write_rel* wr) const
817 Address address = this->address_;
818 if (this->shndx_ != INVALID_CODE)
820 section_offset_type off;
821 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
822 &off);
823 gold_assert(os != NULL);
824 if (off != -1)
825 address += os->address() + off;
826 else
828 address = os->output_address(this->u2_.relobj, this->shndx_,
829 address);
830 gold_assert(address != -1U);
833 else if (this->u2_.od != NULL)
834 address += this->u2_.od->address();
835 wr->put_r_offset(address);
836 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
837 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
840 // Write out a Rel relocation.
842 template<bool dynamic, int size, bool big_endian>
843 void
844 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
845 unsigned char* pov) const
847 elfcpp::Rel_write<size, big_endian> orel(pov);
848 this->write_rel(&orel);
851 // Get the value of the symbol referred to by a Rel relocation.
853 template<bool dynamic, int size, bool big_endian>
854 typename elfcpp::Elf_types<size>::Elf_Addr
855 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
856 Address addend) const
858 if (this->local_sym_index_ == GSYM_CODE)
860 const Sized_symbol<size>* sym;
861 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
862 return sym->value() + addend;
864 gold_assert(this->local_sym_index_ != SECTION_CODE
865 && this->local_sym_index_ != INVALID_CODE
866 && !this->is_section_symbol_);
867 const unsigned int lsi = this->local_sym_index_;
868 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
869 return symval->value(this->u1_.relobj, addend);
872 // Write out a Rela relocation.
874 template<bool dynamic, int size, bool big_endian>
875 void
876 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
877 unsigned char* pov) const
879 elfcpp::Rela_write<size, big_endian> orel(pov);
880 this->rel_.write_rel(&orel);
881 Addend addend = this->addend_;
882 if (this->rel_.is_relative())
883 addend = this->rel_.symbol_value(addend);
884 else if (this->rel_.is_local_section_symbol())
885 addend += this->rel_.local_section_offset();
886 orel.put_r_addend(addend);
889 // Output_data_reloc_base methods.
891 // Adjust the output section.
893 template<int sh_type, bool dynamic, int size, bool big_endian>
894 void
895 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
896 ::do_adjust_output_section(Output_section* os)
898 if (sh_type == elfcpp::SHT_REL)
899 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
900 else if (sh_type == elfcpp::SHT_RELA)
901 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
902 else
903 gold_unreachable();
904 if (dynamic)
905 os->set_should_link_to_dynsym();
906 else
907 os->set_should_link_to_symtab();
910 // Write out relocation data.
912 template<int sh_type, bool dynamic, int size, bool big_endian>
913 void
914 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
915 Output_file* of)
917 const off_t off = this->offset();
918 const off_t oview_size = this->data_size();
919 unsigned char* const oview = of->get_output_view(off, oview_size);
921 unsigned char* pov = oview;
922 for (typename Relocs::const_iterator p = this->relocs_.begin();
923 p != this->relocs_.end();
924 ++p)
926 p->write(pov);
927 pov += reloc_size;
930 gold_assert(pov - oview == oview_size);
932 of->write_output_view(off, oview_size, oview);
934 // We no longer need the relocation entries.
935 this->relocs_.clear();
938 // Class Output_relocatable_relocs.
940 template<int sh_type, int size, bool big_endian>
941 void
942 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
944 this->set_data_size(this->rr_->output_reloc_count()
945 * Reloc_types<sh_type, size, big_endian>::reloc_size);
948 // class Output_data_group.
950 template<int size, bool big_endian>
951 Output_data_group<size, big_endian>::Output_data_group(
952 Sized_relobj<size, big_endian>* relobj,
953 section_size_type entry_count,
954 const elfcpp::Elf_Word* contents)
955 : Output_section_data(entry_count * 4, 4),
956 relobj_(relobj)
958 this->flags_ = elfcpp::Swap<32, big_endian>::readval(contents);
959 for (section_size_type i = 1; i < entry_count; ++i)
961 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
962 this->input_sections_.push_back(shndx);
966 // Write out the section group, which means translating the section
967 // indexes to apply to the output file.
969 template<int size, bool big_endian>
970 void
971 Output_data_group<size, big_endian>::do_write(Output_file* of)
973 const off_t off = this->offset();
974 const section_size_type oview_size =
975 convert_to_section_size_type(this->data_size());
976 unsigned char* const oview = of->get_output_view(off, oview_size);
978 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
979 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
980 ++contents;
982 for (std::vector<unsigned int>::const_iterator p =
983 this->input_sections_.begin();
984 p != this->input_sections_.end();
985 ++p, ++contents)
987 section_offset_type dummy;
988 Output_section* os = this->relobj_->output_section(*p, &dummy);
990 unsigned int output_shndx;
991 if (os != NULL)
992 output_shndx = os->out_shndx();
993 else
995 this->relobj_->error(_("section group retained but "
996 "group element discarded"));
997 output_shndx = 0;
1000 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1003 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1004 gold_assert(wrote == oview_size);
1006 of->write_output_view(off, oview_size, oview);
1008 // We no longer need this information.
1009 this->input_sections_.clear();
1012 // Output_data_got::Got_entry methods.
1014 // Write out the entry.
1016 template<int size, bool big_endian>
1017 void
1018 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1020 Valtype val = 0;
1022 switch (this->local_sym_index_)
1024 case GSYM_CODE:
1026 // If the symbol is resolved locally, we need to write out the
1027 // link-time value, which will be relocated dynamically by a
1028 // RELATIVE relocation.
1029 Symbol* gsym = this->u_.gsym;
1030 Sized_symbol<size>* sgsym;
1031 // This cast is a bit ugly. We don't want to put a
1032 // virtual method in Symbol, because we want Symbol to be
1033 // as small as possible.
1034 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1035 val = sgsym->value();
1037 break;
1039 case CONSTANT_CODE:
1040 val = this->u_.constant;
1041 break;
1043 default:
1045 const unsigned int lsi = this->local_sym_index_;
1046 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1047 val = symval->value(this->u_.object, 0);
1049 break;
1052 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1055 // Output_data_got methods.
1057 // Add an entry for a global symbol to the GOT. This returns true if
1058 // this is a new GOT entry, false if the symbol already had a GOT
1059 // entry.
1061 template<int size, bool big_endian>
1062 bool
1063 Output_data_got<size, big_endian>::add_global(
1064 Symbol* gsym,
1065 unsigned int got_type)
1067 if (gsym->has_got_offset(got_type))
1068 return false;
1070 this->entries_.push_back(Got_entry(gsym));
1071 this->set_got_size();
1072 gsym->set_got_offset(got_type, this->last_got_offset());
1073 return true;
1076 // Add an entry for a global symbol to the GOT, and add a dynamic
1077 // relocation of type R_TYPE for the GOT entry.
1078 template<int size, bool big_endian>
1079 void
1080 Output_data_got<size, big_endian>::add_global_with_rel(
1081 Symbol* gsym,
1082 unsigned int got_type,
1083 Rel_dyn* rel_dyn,
1084 unsigned int r_type)
1086 if (gsym->has_got_offset(got_type))
1087 return;
1089 this->entries_.push_back(Got_entry());
1090 this->set_got_size();
1091 unsigned int got_offset = this->last_got_offset();
1092 gsym->set_got_offset(got_type, got_offset);
1093 rel_dyn->add_global(gsym, r_type, this, got_offset);
1096 template<int size, bool big_endian>
1097 void
1098 Output_data_got<size, big_endian>::add_global_with_rela(
1099 Symbol* gsym,
1100 unsigned int got_type,
1101 Rela_dyn* rela_dyn,
1102 unsigned int r_type)
1104 if (gsym->has_got_offset(got_type))
1105 return;
1107 this->entries_.push_back(Got_entry());
1108 this->set_got_size();
1109 unsigned int got_offset = this->last_got_offset();
1110 gsym->set_got_offset(got_type, got_offset);
1111 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1114 // Add a pair of entries for a global symbol to the GOT, and add
1115 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1116 // If R_TYPE_2 == 0, add the second entry with no relocation.
1117 template<int size, bool big_endian>
1118 void
1119 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1120 Symbol* gsym,
1121 unsigned int got_type,
1122 Rel_dyn* rel_dyn,
1123 unsigned int r_type_1,
1124 unsigned int r_type_2)
1126 if (gsym->has_got_offset(got_type))
1127 return;
1129 this->entries_.push_back(Got_entry());
1130 unsigned int got_offset = this->last_got_offset();
1131 gsym->set_got_offset(got_type, got_offset);
1132 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1134 this->entries_.push_back(Got_entry());
1135 if (r_type_2 != 0)
1137 got_offset = this->last_got_offset();
1138 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1141 this->set_got_size();
1144 template<int size, bool big_endian>
1145 void
1146 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1147 Symbol* gsym,
1148 unsigned int got_type,
1149 Rela_dyn* rela_dyn,
1150 unsigned int r_type_1,
1151 unsigned int r_type_2)
1153 if (gsym->has_got_offset(got_type))
1154 return;
1156 this->entries_.push_back(Got_entry());
1157 unsigned int got_offset = this->last_got_offset();
1158 gsym->set_got_offset(got_type, got_offset);
1159 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1161 this->entries_.push_back(Got_entry());
1162 if (r_type_2 != 0)
1164 got_offset = this->last_got_offset();
1165 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1168 this->set_got_size();
1171 // Add an entry for a local symbol to the GOT. This returns true if
1172 // this is a new GOT entry, false if the symbol already has a GOT
1173 // entry.
1175 template<int size, bool big_endian>
1176 bool
1177 Output_data_got<size, big_endian>::add_local(
1178 Sized_relobj<size, big_endian>* object,
1179 unsigned int symndx,
1180 unsigned int got_type)
1182 if (object->local_has_got_offset(symndx, got_type))
1183 return false;
1185 this->entries_.push_back(Got_entry(object, symndx));
1186 this->set_got_size();
1187 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1188 return true;
1191 // Add an entry for a local symbol to the GOT, and add a dynamic
1192 // relocation of type R_TYPE for the GOT entry.
1193 template<int size, bool big_endian>
1194 void
1195 Output_data_got<size, big_endian>::add_local_with_rel(
1196 Sized_relobj<size, big_endian>* object,
1197 unsigned int symndx,
1198 unsigned int got_type,
1199 Rel_dyn* rel_dyn,
1200 unsigned int r_type)
1202 if (object->local_has_got_offset(symndx, got_type))
1203 return;
1205 this->entries_.push_back(Got_entry());
1206 this->set_got_size();
1207 unsigned int got_offset = this->last_got_offset();
1208 object->set_local_got_offset(symndx, got_type, got_offset);
1209 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1212 template<int size, bool big_endian>
1213 void
1214 Output_data_got<size, big_endian>::add_local_with_rela(
1215 Sized_relobj<size, big_endian>* object,
1216 unsigned int symndx,
1217 unsigned int got_type,
1218 Rela_dyn* rela_dyn,
1219 unsigned int r_type)
1221 if (object->local_has_got_offset(symndx, got_type))
1222 return;
1224 this->entries_.push_back(Got_entry());
1225 this->set_got_size();
1226 unsigned int got_offset = this->last_got_offset();
1227 object->set_local_got_offset(symndx, got_type, got_offset);
1228 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1231 // Add a pair of entries for a local symbol to the GOT, and add
1232 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1233 // If R_TYPE_2 == 0, add the second entry with no relocation.
1234 template<int size, bool big_endian>
1235 void
1236 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1237 Sized_relobj<size, big_endian>* object,
1238 unsigned int symndx,
1239 unsigned int shndx,
1240 unsigned int got_type,
1241 Rel_dyn* rel_dyn,
1242 unsigned int r_type_1,
1243 unsigned int r_type_2)
1245 if (object->local_has_got_offset(symndx, got_type))
1246 return;
1248 this->entries_.push_back(Got_entry());
1249 unsigned int got_offset = this->last_got_offset();
1250 object->set_local_got_offset(symndx, got_type, got_offset);
1251 section_offset_type off;
1252 Output_section* os = object->output_section(shndx, &off);
1253 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1255 this->entries_.push_back(Got_entry(object, symndx));
1256 if (r_type_2 != 0)
1258 got_offset = this->last_got_offset();
1259 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1262 this->set_got_size();
1265 template<int size, bool big_endian>
1266 void
1267 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1268 Sized_relobj<size, big_endian>* object,
1269 unsigned int symndx,
1270 unsigned int shndx,
1271 unsigned int got_type,
1272 Rela_dyn* rela_dyn,
1273 unsigned int r_type_1,
1274 unsigned int r_type_2)
1276 if (object->local_has_got_offset(symndx, got_type))
1277 return;
1279 this->entries_.push_back(Got_entry());
1280 unsigned int got_offset = this->last_got_offset();
1281 object->set_local_got_offset(symndx, got_type, got_offset);
1282 section_offset_type off;
1283 Output_section* os = object->output_section(shndx, &off);
1284 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1286 this->entries_.push_back(Got_entry(object, symndx));
1287 if (r_type_2 != 0)
1289 got_offset = this->last_got_offset();
1290 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1293 this->set_got_size();
1296 // Write out the GOT.
1298 template<int size, bool big_endian>
1299 void
1300 Output_data_got<size, big_endian>::do_write(Output_file* of)
1302 const int add = size / 8;
1304 const off_t off = this->offset();
1305 const off_t oview_size = this->data_size();
1306 unsigned char* const oview = of->get_output_view(off, oview_size);
1308 unsigned char* pov = oview;
1309 for (typename Got_entries::const_iterator p = this->entries_.begin();
1310 p != this->entries_.end();
1311 ++p)
1313 p->write(pov);
1314 pov += add;
1317 gold_assert(pov - oview == oview_size);
1319 of->write_output_view(off, oview_size, oview);
1321 // We no longer need the GOT entries.
1322 this->entries_.clear();
1325 // Output_data_dynamic::Dynamic_entry methods.
1327 // Write out the entry.
1329 template<int size, bool big_endian>
1330 void
1331 Output_data_dynamic::Dynamic_entry::write(
1332 unsigned char* pov,
1333 const Stringpool* pool) const
1335 typename elfcpp::Elf_types<size>::Elf_WXword val;
1336 switch (this->classification_)
1338 case DYNAMIC_NUMBER:
1339 val = this->u_.val;
1340 break;
1342 case DYNAMIC_SECTION_ADDRESS:
1343 val = this->u_.od->address();
1344 break;
1346 case DYNAMIC_SECTION_SIZE:
1347 val = this->u_.od->data_size();
1348 break;
1350 case DYNAMIC_SYMBOL:
1352 const Sized_symbol<size>* s =
1353 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1354 val = s->value();
1356 break;
1358 case DYNAMIC_STRING:
1359 val = pool->get_offset(this->u_.str);
1360 break;
1362 default:
1363 gold_unreachable();
1366 elfcpp::Dyn_write<size, big_endian> dw(pov);
1367 dw.put_d_tag(this->tag_);
1368 dw.put_d_val(val);
1371 // Output_data_dynamic methods.
1373 // Adjust the output section to set the entry size.
1375 void
1376 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1378 if (parameters->target().get_size() == 32)
1379 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1380 else if (parameters->target().get_size() == 64)
1381 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1382 else
1383 gold_unreachable();
1386 // Set the final data size.
1388 void
1389 Output_data_dynamic::set_final_data_size()
1391 // Add the terminating entry.
1392 this->add_constant(elfcpp::DT_NULL, 0);
1394 int dyn_size;
1395 if (parameters->target().get_size() == 32)
1396 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1397 else if (parameters->target().get_size() == 64)
1398 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1399 else
1400 gold_unreachable();
1401 this->set_data_size(this->entries_.size() * dyn_size);
1404 // Write out the dynamic entries.
1406 void
1407 Output_data_dynamic::do_write(Output_file* of)
1409 switch (parameters->size_and_endianness())
1411 #ifdef HAVE_TARGET_32_LITTLE
1412 case Parameters::TARGET_32_LITTLE:
1413 this->sized_write<32, false>(of);
1414 break;
1415 #endif
1416 #ifdef HAVE_TARGET_32_BIG
1417 case Parameters::TARGET_32_BIG:
1418 this->sized_write<32, true>(of);
1419 break;
1420 #endif
1421 #ifdef HAVE_TARGET_64_LITTLE
1422 case Parameters::TARGET_64_LITTLE:
1423 this->sized_write<64, false>(of);
1424 break;
1425 #endif
1426 #ifdef HAVE_TARGET_64_BIG
1427 case Parameters::TARGET_64_BIG:
1428 this->sized_write<64, true>(of);
1429 break;
1430 #endif
1431 default:
1432 gold_unreachable();
1436 template<int size, bool big_endian>
1437 void
1438 Output_data_dynamic::sized_write(Output_file* of)
1440 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1442 const off_t offset = this->offset();
1443 const off_t oview_size = this->data_size();
1444 unsigned char* const oview = of->get_output_view(offset, oview_size);
1446 unsigned char* pov = oview;
1447 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1448 p != this->entries_.end();
1449 ++p)
1451 p->write<size, big_endian>(pov, this->pool_);
1452 pov += dyn_size;
1455 gold_assert(pov - oview == oview_size);
1457 of->write_output_view(offset, oview_size, oview);
1459 // We no longer need the dynamic entries.
1460 this->entries_.clear();
1463 // Output_section::Input_section methods.
1465 // Return the data size. For an input section we store the size here.
1466 // For an Output_section_data, we have to ask it for the size.
1468 off_t
1469 Output_section::Input_section::data_size() const
1471 if (this->is_input_section())
1472 return this->u1_.data_size;
1473 else
1474 return this->u2_.posd->data_size();
1477 // Set the address and file offset.
1479 void
1480 Output_section::Input_section::set_address_and_file_offset(
1481 uint64_t address,
1482 off_t file_offset,
1483 off_t section_file_offset)
1485 if (this->is_input_section())
1486 this->u2_.object->set_section_offset(this->shndx_,
1487 file_offset - section_file_offset);
1488 else
1489 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1492 // Reset the address and file offset.
1494 void
1495 Output_section::Input_section::reset_address_and_file_offset()
1497 if (!this->is_input_section())
1498 this->u2_.posd->reset_address_and_file_offset();
1501 // Finalize the data size.
1503 void
1504 Output_section::Input_section::finalize_data_size()
1506 if (!this->is_input_section())
1507 this->u2_.posd->finalize_data_size();
1510 // Try to turn an input offset into an output offset. We want to
1511 // return the output offset relative to the start of this
1512 // Input_section in the output section.
1514 inline bool
1515 Output_section::Input_section::output_offset(
1516 const Relobj* object,
1517 unsigned int shndx,
1518 section_offset_type offset,
1519 section_offset_type *poutput) const
1521 if (!this->is_input_section())
1522 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1523 else
1525 if (this->shndx_ != shndx || this->u2_.object != object)
1526 return false;
1527 *poutput = offset;
1528 return true;
1532 // Return whether this is the merge section for the input section
1533 // SHNDX in OBJECT.
1535 inline bool
1536 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1537 unsigned int shndx) const
1539 if (this->is_input_section())
1540 return false;
1541 return this->u2_.posd->is_merge_section_for(object, shndx);
1544 // Write out the data. We don't have to do anything for an input
1545 // section--they are handled via Object::relocate--but this is where
1546 // we write out the data for an Output_section_data.
1548 void
1549 Output_section::Input_section::write(Output_file* of)
1551 if (!this->is_input_section())
1552 this->u2_.posd->write(of);
1555 // Write the data to a buffer. As for write(), we don't have to do
1556 // anything for an input section.
1558 void
1559 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1561 if (!this->is_input_section())
1562 this->u2_.posd->write_to_buffer(buffer);
1565 // Output_section methods.
1567 // Construct an Output_section. NAME will point into a Stringpool.
1569 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1570 elfcpp::Elf_Xword flags)
1571 : name_(name),
1572 addralign_(0),
1573 entsize_(0),
1574 load_address_(0),
1575 link_section_(NULL),
1576 link_(0),
1577 info_section_(NULL),
1578 info_symndx_(NULL),
1579 info_(0),
1580 type_(type),
1581 flags_(flags),
1582 out_shndx_(-1U),
1583 symtab_index_(0),
1584 dynsym_index_(0),
1585 input_sections_(),
1586 first_input_offset_(0),
1587 fills_(),
1588 postprocessing_buffer_(NULL),
1589 needs_symtab_index_(false),
1590 needs_dynsym_index_(false),
1591 should_link_to_symtab_(false),
1592 should_link_to_dynsym_(false),
1593 after_input_sections_(false),
1594 requires_postprocessing_(false),
1595 found_in_sections_clause_(false),
1596 has_load_address_(false),
1597 info_uses_section_index_(false),
1598 tls_offset_(0)
1600 // An unallocated section has no address. Forcing this means that
1601 // we don't need special treatment for symbols defined in debug
1602 // sections.
1603 if ((flags & elfcpp::SHF_ALLOC) == 0)
1604 this->set_address(0);
1607 Output_section::~Output_section()
1611 // Set the entry size.
1613 void
1614 Output_section::set_entsize(uint64_t v)
1616 if (this->entsize_ == 0)
1617 this->entsize_ = v;
1618 else
1619 gold_assert(this->entsize_ == v);
1622 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1623 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1624 // relocation section which applies to this section, or 0 if none, or
1625 // -1U if more than one. Return the offset of the input section
1626 // within the output section. Return -1 if the input section will
1627 // receive special handling. In the normal case we don't always keep
1628 // track of input sections for an Output_section. Instead, each
1629 // Object keeps track of the Output_section for each of its input
1630 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1631 // track of input sections here; this is used when SECTIONS appears in
1632 // a linker script.
1634 template<int size, bool big_endian>
1635 off_t
1636 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1637 unsigned int shndx,
1638 const char* secname,
1639 const elfcpp::Shdr<size, big_endian>& shdr,
1640 unsigned int reloc_shndx,
1641 bool have_sections_script)
1643 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1644 if ((addralign & (addralign - 1)) != 0)
1646 object->error(_("invalid alignment %lu for section \"%s\""),
1647 static_cast<unsigned long>(addralign), secname);
1648 addralign = 1;
1651 if (addralign > this->addralign_)
1652 this->addralign_ = addralign;
1654 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1655 this->flags_ |= (sh_flags
1656 & (elfcpp::SHF_WRITE
1657 | elfcpp::SHF_ALLOC
1658 | elfcpp::SHF_EXECINSTR));
1660 uint64_t entsize = shdr.get_sh_entsize();
1662 // .debug_str is a mergeable string section, but is not always so
1663 // marked by compilers. Mark manually here so we can optimize.
1664 if (strcmp(secname, ".debug_str") == 0)
1666 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1667 entsize = 1;
1670 // If this is a SHF_MERGE section, we pass all the input sections to
1671 // a Output_data_merge. We don't try to handle relocations for such
1672 // a section.
1673 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1674 && reloc_shndx == 0)
1676 if (this->add_merge_input_section(object, shndx, sh_flags,
1677 entsize, addralign))
1679 // Tell the relocation routines that they need to call the
1680 // output_offset method to determine the final address.
1681 return -1;
1685 off_t offset_in_section = this->current_data_size_for_child();
1686 off_t aligned_offset_in_section = align_address(offset_in_section,
1687 addralign);
1689 if (aligned_offset_in_section > offset_in_section
1690 && !have_sections_script
1691 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1692 && object->target()->has_code_fill())
1694 // We need to add some fill data. Using fill_list_ when
1695 // possible is an optimization, since we will often have fill
1696 // sections without input sections.
1697 off_t fill_len = aligned_offset_in_section - offset_in_section;
1698 if (this->input_sections_.empty())
1699 this->fills_.push_back(Fill(offset_in_section, fill_len));
1700 else
1702 // FIXME: When relaxing, the size needs to adjust to
1703 // maintain a constant alignment.
1704 std::string fill_data(object->target()->code_fill(fill_len));
1705 Output_data_const* odc = new Output_data_const(fill_data, 1);
1706 this->input_sections_.push_back(Input_section(odc));
1710 this->set_current_data_size_for_child(aligned_offset_in_section
1711 + shdr.get_sh_size());
1713 // We need to keep track of this section if we are already keeping
1714 // track of sections, or if we are relaxing. FIXME: Add test for
1715 // relaxing.
1716 if (have_sections_script || !this->input_sections_.empty())
1717 this->input_sections_.push_back(Input_section(object, shndx,
1718 shdr.get_sh_size(),
1719 addralign));
1721 return aligned_offset_in_section;
1724 // Add arbitrary data to an output section.
1726 void
1727 Output_section::add_output_section_data(Output_section_data* posd)
1729 Input_section inp(posd);
1730 this->add_output_section_data(&inp);
1732 if (posd->is_data_size_valid())
1734 off_t offset_in_section = this->current_data_size_for_child();
1735 off_t aligned_offset_in_section = align_address(offset_in_section,
1736 posd->addralign());
1737 this->set_current_data_size_for_child(aligned_offset_in_section
1738 + posd->data_size());
1742 // Add arbitrary data to an output section by Input_section.
1744 void
1745 Output_section::add_output_section_data(Input_section* inp)
1747 if (this->input_sections_.empty())
1748 this->first_input_offset_ = this->current_data_size_for_child();
1750 this->input_sections_.push_back(*inp);
1752 uint64_t addralign = inp->addralign();
1753 if (addralign > this->addralign_)
1754 this->addralign_ = addralign;
1756 inp->set_output_section(this);
1759 // Add a merge section to an output section.
1761 void
1762 Output_section::add_output_merge_section(Output_section_data* posd,
1763 bool is_string, uint64_t entsize)
1765 Input_section inp(posd, is_string, entsize);
1766 this->add_output_section_data(&inp);
1769 // Add an input section to a SHF_MERGE section.
1771 bool
1772 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1773 uint64_t flags, uint64_t entsize,
1774 uint64_t addralign)
1776 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1778 // We only merge strings if the alignment is not more than the
1779 // character size. This could be handled, but it's unusual.
1780 if (is_string && addralign > entsize)
1781 return false;
1783 Input_section_list::iterator p;
1784 for (p = this->input_sections_.begin();
1785 p != this->input_sections_.end();
1786 ++p)
1787 if (p->is_merge_section(is_string, entsize, addralign))
1789 p->add_input_section(object, shndx);
1790 return true;
1793 // We handle the actual constant merging in Output_merge_data or
1794 // Output_merge_string_data.
1795 Output_section_data* posd;
1796 if (!is_string)
1797 posd = new Output_merge_data(entsize, addralign);
1798 else
1800 switch (entsize)
1802 case 1:
1803 posd = new Output_merge_string<char>(addralign);
1804 break;
1805 case 2:
1806 posd = new Output_merge_string<uint16_t>(addralign);
1807 break;
1808 case 4:
1809 posd = new Output_merge_string<uint32_t>(addralign);
1810 break;
1811 default:
1812 return false;
1816 this->add_output_merge_section(posd, is_string, entsize);
1817 posd->add_input_section(object, shndx);
1819 return true;
1822 // Given an address OFFSET relative to the start of input section
1823 // SHNDX in OBJECT, return whether this address is being included in
1824 // the final link. This should only be called if SHNDX in OBJECT has
1825 // a special mapping.
1827 bool
1828 Output_section::is_input_address_mapped(const Relobj* object,
1829 unsigned int shndx,
1830 off_t offset) const
1832 gold_assert(object->is_section_specially_mapped(shndx));
1834 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1835 p != this->input_sections_.end();
1836 ++p)
1838 section_offset_type output_offset;
1839 if (p->output_offset(object, shndx, offset, &output_offset))
1840 return output_offset != -1;
1843 // By default we assume that the address is mapped. This should
1844 // only be called after we have passed all sections to Layout. At
1845 // that point we should know what we are discarding.
1846 return true;
1849 // Given an address OFFSET relative to the start of input section
1850 // SHNDX in object OBJECT, return the output offset relative to the
1851 // start of the input section in the output section. This should only
1852 // be called if SHNDX in OBJECT has a special mapping.
1854 section_offset_type
1855 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1856 section_offset_type offset) const
1858 gold_assert(object->is_section_specially_mapped(shndx));
1859 // This can only be called meaningfully when layout is complete.
1860 gold_assert(Output_data::is_layout_complete());
1862 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1863 p != this->input_sections_.end();
1864 ++p)
1866 section_offset_type output_offset;
1867 if (p->output_offset(object, shndx, offset, &output_offset))
1868 return output_offset;
1870 gold_unreachable();
1873 // Return the output virtual address of OFFSET relative to the start
1874 // of input section SHNDX in object OBJECT.
1876 uint64_t
1877 Output_section::output_address(const Relobj* object, unsigned int shndx,
1878 off_t offset) const
1880 gold_assert(object->is_section_specially_mapped(shndx));
1882 uint64_t addr = this->address() + this->first_input_offset_;
1883 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1884 p != this->input_sections_.end();
1885 ++p)
1887 addr = align_address(addr, p->addralign());
1888 section_offset_type output_offset;
1889 if (p->output_offset(object, shndx, offset, &output_offset))
1891 if (output_offset == -1)
1892 return -1U;
1893 return addr + output_offset;
1895 addr += p->data_size();
1898 // If we get here, it means that we don't know the mapping for this
1899 // input section. This might happen in principle if
1900 // add_input_section were called before add_output_section_data.
1901 // But it should never actually happen.
1903 gold_unreachable();
1906 // Return the output address of the start of the merged section for
1907 // input section SHNDX in object OBJECT.
1909 uint64_t
1910 Output_section::starting_output_address(const Relobj* object,
1911 unsigned int shndx) const
1913 gold_assert(object->is_section_specially_mapped(shndx));
1915 uint64_t addr = this->address() + this->first_input_offset_;
1916 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1917 p != this->input_sections_.end();
1918 ++p)
1920 addr = align_address(addr, p->addralign());
1922 // It would be nice if we could use the existing output_offset
1923 // method to get the output offset of input offset 0.
1924 // Unfortunately we don't know for sure that input offset 0 is
1925 // mapped at all.
1926 if (p->is_merge_section_for(object, shndx))
1927 return addr;
1929 addr += p->data_size();
1931 gold_unreachable();
1934 // Set the data size of an Output_section. This is where we handle
1935 // setting the addresses of any Output_section_data objects.
1937 void
1938 Output_section::set_final_data_size()
1940 if (this->input_sections_.empty())
1942 this->set_data_size(this->current_data_size_for_child());
1943 return;
1946 uint64_t address = this->address();
1947 off_t startoff = this->offset();
1948 off_t off = startoff + this->first_input_offset_;
1949 for (Input_section_list::iterator p = this->input_sections_.begin();
1950 p != this->input_sections_.end();
1951 ++p)
1953 off = align_address(off, p->addralign());
1954 p->set_address_and_file_offset(address + (off - startoff), off,
1955 startoff);
1956 off += p->data_size();
1959 this->set_data_size(off - startoff);
1962 // Reset the address and file offset.
1964 void
1965 Output_section::do_reset_address_and_file_offset()
1967 for (Input_section_list::iterator p = this->input_sections_.begin();
1968 p != this->input_sections_.end();
1969 ++p)
1970 p->reset_address_and_file_offset();
1973 // Set the TLS offset. Called only for SHT_TLS sections.
1975 void
1976 Output_section::do_set_tls_offset(uint64_t tls_base)
1978 this->tls_offset_ = this->address() - tls_base;
1981 // Write the section header to *OSHDR.
1983 template<int size, bool big_endian>
1984 void
1985 Output_section::write_header(const Layout* layout,
1986 const Stringpool* secnamepool,
1987 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1989 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1990 oshdr->put_sh_type(this->type_);
1992 elfcpp::Elf_Xword flags = this->flags_;
1993 if (this->info_section_ != NULL && this->info_uses_section_index_)
1994 flags |= elfcpp::SHF_INFO_LINK;
1995 oshdr->put_sh_flags(flags);
1997 oshdr->put_sh_addr(this->address());
1998 oshdr->put_sh_offset(this->offset());
1999 oshdr->put_sh_size(this->data_size());
2000 if (this->link_section_ != NULL)
2001 oshdr->put_sh_link(this->link_section_->out_shndx());
2002 else if (this->should_link_to_symtab_)
2003 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2004 else if (this->should_link_to_dynsym_)
2005 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2006 else
2007 oshdr->put_sh_link(this->link_);
2009 elfcpp::Elf_Word info;
2010 if (this->info_section_ != NULL)
2012 if (this->info_uses_section_index_)
2013 info = this->info_section_->out_shndx();
2014 else
2015 info = this->info_section_->symtab_index();
2017 else if (this->info_symndx_ != NULL)
2018 info = this->info_symndx_->symtab_index();
2019 else
2020 info = this->info_;
2021 oshdr->put_sh_info(info);
2023 oshdr->put_sh_addralign(this->addralign_);
2024 oshdr->put_sh_entsize(this->entsize_);
2027 // Write out the data. For input sections the data is written out by
2028 // Object::relocate, but we have to handle Output_section_data objects
2029 // here.
2031 void
2032 Output_section::do_write(Output_file* of)
2034 gold_assert(!this->requires_postprocessing());
2036 off_t output_section_file_offset = this->offset();
2037 for (Fill_list::iterator p = this->fills_.begin();
2038 p != this->fills_.end();
2039 ++p)
2041 std::string fill_data(parameters->target().code_fill(p->length()));
2042 of->write(output_section_file_offset + p->section_offset(),
2043 fill_data.data(), fill_data.size());
2046 for (Input_section_list::iterator p = this->input_sections_.begin();
2047 p != this->input_sections_.end();
2048 ++p)
2049 p->write(of);
2052 // If a section requires postprocessing, create the buffer to use.
2054 void
2055 Output_section::create_postprocessing_buffer()
2057 gold_assert(this->requires_postprocessing());
2059 if (this->postprocessing_buffer_ != NULL)
2060 return;
2062 if (!this->input_sections_.empty())
2064 off_t off = this->first_input_offset_;
2065 for (Input_section_list::iterator p = this->input_sections_.begin();
2066 p != this->input_sections_.end();
2067 ++p)
2069 off = align_address(off, p->addralign());
2070 p->finalize_data_size();
2071 off += p->data_size();
2073 this->set_current_data_size_for_child(off);
2076 off_t buffer_size = this->current_data_size_for_child();
2077 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2080 // Write all the data of an Output_section into the postprocessing
2081 // buffer. This is used for sections which require postprocessing,
2082 // such as compression. Input sections are handled by
2083 // Object::Relocate.
2085 void
2086 Output_section::write_to_postprocessing_buffer()
2088 gold_assert(this->requires_postprocessing());
2090 unsigned char* buffer = this->postprocessing_buffer();
2091 for (Fill_list::iterator p = this->fills_.begin();
2092 p != this->fills_.end();
2093 ++p)
2095 std::string fill_data(parameters->target().code_fill(p->length()));
2096 memcpy(buffer + p->section_offset(), fill_data.data(),
2097 fill_data.size());
2100 off_t off = this->first_input_offset_;
2101 for (Input_section_list::iterator p = this->input_sections_.begin();
2102 p != this->input_sections_.end();
2103 ++p)
2105 off = align_address(off, p->addralign());
2106 p->write_to_buffer(buffer + off);
2107 off += p->data_size();
2111 // Get the input sections for linker script processing. We leave
2112 // behind the Output_section_data entries. Note that this may be
2113 // slightly incorrect for merge sections. We will leave them behind,
2114 // but it is possible that the script says that they should follow
2115 // some other input sections, as in:
2116 // .rodata { *(.rodata) *(.rodata.cst*) }
2117 // For that matter, we don't handle this correctly:
2118 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2119 // With luck this will never matter.
2121 uint64_t
2122 Output_section::get_input_sections(
2123 uint64_t address,
2124 const std::string& fill,
2125 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2127 uint64_t orig_address = address;
2129 address = align_address(address, this->addralign());
2131 Input_section_list remaining;
2132 for (Input_section_list::iterator p = this->input_sections_.begin();
2133 p != this->input_sections_.end();
2134 ++p)
2136 if (p->is_input_section())
2137 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2138 else
2140 uint64_t aligned_address = align_address(address, p->addralign());
2141 if (aligned_address != address && !fill.empty())
2143 section_size_type length =
2144 convert_to_section_size_type(aligned_address - address);
2145 std::string this_fill;
2146 this_fill.reserve(length);
2147 while (this_fill.length() + fill.length() <= length)
2148 this_fill += fill;
2149 if (this_fill.length() < length)
2150 this_fill.append(fill, 0, length - this_fill.length());
2152 Output_section_data* posd = new Output_data_const(this_fill, 0);
2153 remaining.push_back(Input_section(posd));
2155 address = aligned_address;
2157 remaining.push_back(*p);
2159 p->finalize_data_size();
2160 address += p->data_size();
2164 this->input_sections_.swap(remaining);
2165 this->first_input_offset_ = 0;
2167 uint64_t data_size = address - orig_address;
2168 this->set_current_data_size_for_child(data_size);
2169 return data_size;
2172 // Add an input section from a script.
2174 void
2175 Output_section::add_input_section_for_script(Relobj* object,
2176 unsigned int shndx,
2177 off_t data_size,
2178 uint64_t addralign)
2180 if (addralign > this->addralign_)
2181 this->addralign_ = addralign;
2183 off_t offset_in_section = this->current_data_size_for_child();
2184 off_t aligned_offset_in_section = align_address(offset_in_section,
2185 addralign);
2187 this->set_current_data_size_for_child(aligned_offset_in_section
2188 + data_size);
2190 this->input_sections_.push_back(Input_section(object, shndx,
2191 data_size, addralign));
2194 // Print stats for merge sections to stderr.
2196 void
2197 Output_section::print_merge_stats()
2199 Input_section_list::iterator p;
2200 for (p = this->input_sections_.begin();
2201 p != this->input_sections_.end();
2202 ++p)
2203 p->print_merge_stats(this->name_);
2206 // Output segment methods.
2208 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2209 : output_data_(),
2210 output_bss_(),
2211 vaddr_(0),
2212 paddr_(0),
2213 memsz_(0),
2214 max_align_(0),
2215 min_p_align_(0),
2216 offset_(0),
2217 filesz_(0),
2218 type_(type),
2219 flags_(flags),
2220 is_max_align_known_(false),
2221 are_addresses_set_(false)
2225 // Add an Output_section to an Output_segment.
2227 void
2228 Output_segment::add_output_section(Output_section* os,
2229 elfcpp::Elf_Word seg_flags,
2230 bool front)
2232 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
2233 gold_assert(!this->is_max_align_known_);
2235 // Update the segment flags.
2236 this->flags_ |= seg_flags;
2238 Output_segment::Output_data_list* pdl;
2239 if (os->type() == elfcpp::SHT_NOBITS)
2240 pdl = &this->output_bss_;
2241 else
2242 pdl = &this->output_data_;
2244 // So that PT_NOTE segments will work correctly, we need to ensure
2245 // that all SHT_NOTE sections are adjacent. This will normally
2246 // happen automatically, because all the SHT_NOTE input sections
2247 // will wind up in the same output section. However, it is possible
2248 // for multiple SHT_NOTE input sections to have different section
2249 // flags, and thus be in different output sections, but for the
2250 // different section flags to map into the same segment flags and
2251 // thus the same output segment.
2253 // Note that while there may be many input sections in an output
2254 // section, there are normally only a few output sections in an
2255 // output segment. This loop is expected to be fast.
2257 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
2259 Output_segment::Output_data_list::iterator p = pdl->end();
2262 --p;
2263 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2265 // We don't worry about the FRONT parameter.
2266 ++p;
2267 pdl->insert(p, os);
2268 return;
2271 while (p != pdl->begin());
2274 // Similarly, so that PT_TLS segments will work, we need to group
2275 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2276 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2277 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2278 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2279 // and the PT_TLS segment -- we do this grouping only for the
2280 // PT_LOAD segment.
2281 if (this->type_ != elfcpp::PT_TLS
2282 && (os->flags() & elfcpp::SHF_TLS) != 0
2283 && !this->output_data_.empty())
2285 pdl = &this->output_data_;
2286 bool nobits = os->type() == elfcpp::SHT_NOBITS;
2287 bool sawtls = false;
2288 Output_segment::Output_data_list::iterator p = pdl->end();
2291 --p;
2292 bool insert;
2293 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2295 sawtls = true;
2296 // Put a NOBITS section after the first TLS section.
2297 // But a PROGBITS section after the first TLS/PROGBITS
2298 // section.
2299 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2301 else
2303 // If we've gone past the TLS sections, but we've seen a
2304 // TLS section, then we need to insert this section now.
2305 insert = sawtls;
2308 if (insert)
2310 // We don't worry about the FRONT parameter.
2311 ++p;
2312 pdl->insert(p, os);
2313 return;
2316 while (p != pdl->begin());
2318 // There are no TLS sections yet; put this one at the requested
2319 // location in the section list.
2322 if (front)
2323 pdl->push_front(os);
2324 else
2325 pdl->push_back(os);
2328 // Remove an Output_section from this segment. It is an error if it
2329 // is not present.
2331 void
2332 Output_segment::remove_output_section(Output_section* os)
2334 // We only need this for SHT_PROGBITS.
2335 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2336 for (Output_data_list::iterator p = this->output_data_.begin();
2337 p != this->output_data_.end();
2338 ++p)
2340 if (*p == os)
2342 this->output_data_.erase(p);
2343 return;
2346 gold_unreachable();
2349 // Add an Output_data (which is not an Output_section) to the start of
2350 // a segment.
2352 void
2353 Output_segment::add_initial_output_data(Output_data* od)
2355 gold_assert(!this->is_max_align_known_);
2356 this->output_data_.push_front(od);
2359 // Return the maximum alignment of the Output_data in Output_segment.
2361 uint64_t
2362 Output_segment::maximum_alignment()
2364 if (!this->is_max_align_known_)
2366 uint64_t addralign;
2368 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2369 if (addralign > this->max_align_)
2370 this->max_align_ = addralign;
2372 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2373 if (addralign > this->max_align_)
2374 this->max_align_ = addralign;
2376 this->is_max_align_known_ = true;
2379 return this->max_align_;
2382 // Return the maximum alignment of a list of Output_data.
2384 uint64_t
2385 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
2387 uint64_t ret = 0;
2388 for (Output_data_list::const_iterator p = pdl->begin();
2389 p != pdl->end();
2390 ++p)
2392 uint64_t addralign = (*p)->addralign();
2393 if (addralign > ret)
2394 ret = addralign;
2396 return ret;
2399 // Return the number of dynamic relocs applied to this segment.
2401 unsigned int
2402 Output_segment::dynamic_reloc_count() const
2404 return (this->dynamic_reloc_count_list(&this->output_data_)
2405 + this->dynamic_reloc_count_list(&this->output_bss_));
2408 // Return the number of dynamic relocs applied to an Output_data_list.
2410 unsigned int
2411 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2413 unsigned int count = 0;
2414 for (Output_data_list::const_iterator p = pdl->begin();
2415 p != pdl->end();
2416 ++p)
2417 count += (*p)->dynamic_reloc_count();
2418 return count;
2421 // Set the section addresses for an Output_segment. If RESET is true,
2422 // reset the addresses first. ADDR is the address and *POFF is the
2423 // file offset. Set the section indexes starting with *PSHNDX.
2424 // Return the address of the immediately following segment. Update
2425 // *POFF and *PSHNDX.
2427 uint64_t
2428 Output_segment::set_section_addresses(const Layout* layout, bool reset,
2429 uint64_t addr, off_t* poff,
2430 unsigned int* pshndx)
2432 gold_assert(this->type_ == elfcpp::PT_LOAD);
2434 if (!reset && this->are_addresses_set_)
2436 gold_assert(this->paddr_ == addr);
2437 addr = this->vaddr_;
2439 else
2441 this->vaddr_ = addr;
2442 this->paddr_ = addr;
2443 this->are_addresses_set_ = true;
2446 bool in_tls = false;
2448 off_t orig_off = *poff;
2449 this->offset_ = orig_off;
2451 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
2452 addr, poff, pshndx, &in_tls);
2453 this->filesz_ = *poff - orig_off;
2455 off_t off = *poff;
2457 uint64_t ret = this->set_section_list_addresses(layout, reset,
2458 &this->output_bss_,
2459 addr, poff, pshndx,
2460 &in_tls);
2462 // If the last section was a TLS section, align upward to the
2463 // alignment of the TLS segment, so that the overall size of the TLS
2464 // segment is aligned.
2465 if (in_tls)
2467 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2468 *poff = align_address(*poff, segment_align);
2471 this->memsz_ = *poff - orig_off;
2473 // Ignore the file offset adjustments made by the BSS Output_data
2474 // objects.
2475 *poff = off;
2477 return ret;
2480 // Set the addresses and file offsets in a list of Output_data
2481 // structures.
2483 uint64_t
2484 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
2485 Output_data_list* pdl,
2486 uint64_t addr, off_t* poff,
2487 unsigned int* pshndx,
2488 bool* in_tls)
2490 off_t startoff = *poff;
2492 off_t off = startoff;
2493 for (Output_data_list::iterator p = pdl->begin();
2494 p != pdl->end();
2495 ++p)
2497 if (reset)
2498 (*p)->reset_address_and_file_offset();
2500 // When using a linker script the section will most likely
2501 // already have an address.
2502 if (!(*p)->is_address_valid())
2504 uint64_t align = (*p)->addralign();
2506 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2508 // Give the first TLS section the alignment of the
2509 // entire TLS segment. Otherwise the TLS segment as a
2510 // whole may be misaligned.
2511 if (!*in_tls)
2513 Output_segment* tls_segment = layout->tls_segment();
2514 gold_assert(tls_segment != NULL);
2515 uint64_t segment_align = tls_segment->maximum_alignment();
2516 gold_assert(segment_align >= align);
2517 align = segment_align;
2519 *in_tls = true;
2522 else
2524 // If this is the first section after the TLS segment,
2525 // align it to at least the alignment of the TLS
2526 // segment, so that the size of the overall TLS segment
2527 // is aligned.
2528 if (*in_tls)
2530 uint64_t segment_align =
2531 layout->tls_segment()->maximum_alignment();
2532 if (segment_align > align)
2533 align = segment_align;
2535 *in_tls = false;
2539 off = align_address(off, align);
2540 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2542 else
2544 // The script may have inserted a skip forward, but it
2545 // better not have moved backward.
2546 gold_assert((*p)->address() >= addr + (off - startoff));
2547 off += (*p)->address() - (addr + (off - startoff));
2548 (*p)->set_file_offset(off);
2549 (*p)->finalize_data_size();
2552 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2553 // section. Such a section does not affect the size of a
2554 // PT_LOAD segment.
2555 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2556 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2557 off += (*p)->data_size();
2559 if ((*p)->is_section())
2561 (*p)->set_out_shndx(*pshndx);
2562 ++*pshndx;
2566 *poff = off;
2567 return addr + (off - startoff);
2570 // For a non-PT_LOAD segment, set the offset from the sections, if
2571 // any.
2573 void
2574 Output_segment::set_offset()
2576 gold_assert(this->type_ != elfcpp::PT_LOAD);
2578 gold_assert(!this->are_addresses_set_);
2580 if (this->output_data_.empty() && this->output_bss_.empty())
2582 this->vaddr_ = 0;
2583 this->paddr_ = 0;
2584 this->are_addresses_set_ = true;
2585 this->memsz_ = 0;
2586 this->min_p_align_ = 0;
2587 this->offset_ = 0;
2588 this->filesz_ = 0;
2589 return;
2592 const Output_data* first;
2593 if (this->output_data_.empty())
2594 first = this->output_bss_.front();
2595 else
2596 first = this->output_data_.front();
2597 this->vaddr_ = first->address();
2598 this->paddr_ = (first->has_load_address()
2599 ? first->load_address()
2600 : this->vaddr_);
2601 this->are_addresses_set_ = true;
2602 this->offset_ = first->offset();
2604 if (this->output_data_.empty())
2605 this->filesz_ = 0;
2606 else
2608 const Output_data* last_data = this->output_data_.back();
2609 this->filesz_ = (last_data->address()
2610 + last_data->data_size()
2611 - this->vaddr_);
2614 const Output_data* last;
2615 if (this->output_bss_.empty())
2616 last = this->output_data_.back();
2617 else
2618 last = this->output_bss_.back();
2619 this->memsz_ = (last->address()
2620 + last->data_size()
2621 - this->vaddr_);
2623 // If this is a TLS segment, align the memory size. The code in
2624 // set_section_list ensures that the section after the TLS segment
2625 // is aligned to give us room.
2626 if (this->type_ == elfcpp::PT_TLS)
2628 uint64_t segment_align = this->maximum_alignment();
2629 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
2630 this->memsz_ = align_address(this->memsz_, segment_align);
2634 // Set the TLS offsets of the sections in the PT_TLS segment.
2636 void
2637 Output_segment::set_tls_offsets()
2639 gold_assert(this->type_ == elfcpp::PT_TLS);
2641 for (Output_data_list::iterator p = this->output_data_.begin();
2642 p != this->output_data_.end();
2643 ++p)
2644 (*p)->set_tls_offset(this->vaddr_);
2646 for (Output_data_list::iterator p = this->output_bss_.begin();
2647 p != this->output_bss_.end();
2648 ++p)
2649 (*p)->set_tls_offset(this->vaddr_);
2652 // Return the address of the first section.
2654 uint64_t
2655 Output_segment::first_section_load_address() const
2657 for (Output_data_list::const_iterator p = this->output_data_.begin();
2658 p != this->output_data_.end();
2659 ++p)
2660 if ((*p)->is_section())
2661 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2663 for (Output_data_list::const_iterator p = this->output_bss_.begin();
2664 p != this->output_bss_.end();
2665 ++p)
2666 if ((*p)->is_section())
2667 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2669 gold_unreachable();
2672 // Return the number of Output_sections in an Output_segment.
2674 unsigned int
2675 Output_segment::output_section_count() const
2677 return (this->output_section_count_list(&this->output_data_)
2678 + this->output_section_count_list(&this->output_bss_));
2681 // Return the number of Output_sections in an Output_data_list.
2683 unsigned int
2684 Output_segment::output_section_count_list(const Output_data_list* pdl) const
2686 unsigned int count = 0;
2687 for (Output_data_list::const_iterator p = pdl->begin();
2688 p != pdl->end();
2689 ++p)
2691 if ((*p)->is_section())
2692 ++count;
2694 return count;
2697 // Return the section attached to the list segment with the lowest
2698 // load address. This is used when handling a PHDRS clause in a
2699 // linker script.
2701 Output_section*
2702 Output_segment::section_with_lowest_load_address() const
2704 Output_section* found = NULL;
2705 uint64_t found_lma = 0;
2706 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
2708 Output_section* found_data = found;
2709 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
2710 if (found != found_data && found_data != NULL)
2712 gold_error(_("nobits section %s may not precede progbits section %s "
2713 "in same segment"),
2714 found->name(), found_data->name());
2715 return NULL;
2718 return found;
2721 // Look through a list for a section with a lower load address.
2723 void
2724 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
2725 Output_section** found,
2726 uint64_t* found_lma) const
2728 for (Output_data_list::const_iterator p = pdl->begin();
2729 p != pdl->end();
2730 ++p)
2732 if (!(*p)->is_section())
2733 continue;
2734 Output_section* os = static_cast<Output_section*>(*p);
2735 uint64_t lma = (os->has_load_address()
2736 ? os->load_address()
2737 : os->address());
2738 if (*found == NULL || lma < *found_lma)
2740 *found = os;
2741 *found_lma = lma;
2746 // Write the segment data into *OPHDR.
2748 template<int size, bool big_endian>
2749 void
2750 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
2752 ophdr->put_p_type(this->type_);
2753 ophdr->put_p_offset(this->offset_);
2754 ophdr->put_p_vaddr(this->vaddr_);
2755 ophdr->put_p_paddr(this->paddr_);
2756 ophdr->put_p_filesz(this->filesz_);
2757 ophdr->put_p_memsz(this->memsz_);
2758 ophdr->put_p_flags(this->flags_);
2759 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
2762 // Write the section headers into V.
2764 template<int size, bool big_endian>
2765 unsigned char*
2766 Output_segment::write_section_headers(const Layout* layout,
2767 const Stringpool* secnamepool,
2768 unsigned char* v,
2769 unsigned int *pshndx) const
2771 // Every section that is attached to a segment must be attached to a
2772 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2773 // segments.
2774 if (this->type_ != elfcpp::PT_LOAD)
2775 return v;
2777 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
2778 &this->output_data_,
2779 v, pshndx);
2780 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
2781 &this->output_bss_,
2782 v, pshndx);
2783 return v;
2786 template<int size, bool big_endian>
2787 unsigned char*
2788 Output_segment::write_section_headers_list(const Layout* layout,
2789 const Stringpool* secnamepool,
2790 const Output_data_list* pdl,
2791 unsigned char* v,
2792 unsigned int* pshndx) const
2794 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2795 for (Output_data_list::const_iterator p = pdl->begin();
2796 p != pdl->end();
2797 ++p)
2799 if ((*p)->is_section())
2801 const Output_section* ps = static_cast<const Output_section*>(*p);
2802 gold_assert(*pshndx == ps->out_shndx());
2803 elfcpp::Shdr_write<size, big_endian> oshdr(v);
2804 ps->write_header(layout, secnamepool, &oshdr);
2805 v += shdr_size;
2806 ++*pshndx;
2809 return v;
2812 // Output_file methods.
2814 Output_file::Output_file(const char* name)
2815 : name_(name),
2816 o_(-1),
2817 file_size_(0),
2818 base_(NULL),
2819 map_is_anonymous_(false),
2820 is_temporary_(false)
2824 // Open the output file.
2826 void
2827 Output_file::open(off_t file_size)
2829 this->file_size_ = file_size;
2831 // Unlink the file first; otherwise the open() may fail if the file
2832 // is busy (e.g. it's an executable that's currently being executed).
2834 // However, the linker may be part of a system where a zero-length
2835 // file is created for it to write to, with tight permissions (gcc
2836 // 2.95 did something like this). Unlinking the file would work
2837 // around those permission controls, so we only unlink if the file
2838 // has a non-zero size. We also unlink only regular files to avoid
2839 // trouble with directories/etc.
2841 // If we fail, continue; this command is merely a best-effort attempt
2842 // to improve the odds for open().
2844 // We let the name "-" mean "stdout"
2845 if (!this->is_temporary_)
2847 if (strcmp(this->name_, "-") == 0)
2848 this->o_ = STDOUT_FILENO;
2849 else
2851 struct stat s;
2852 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
2853 unlink_if_ordinary(this->name_);
2855 int mode = parameters->options().relocatable() ? 0666 : 0777;
2856 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
2857 if (o < 0)
2858 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
2859 this->o_ = o;
2863 this->map();
2866 // Resize the output file.
2868 void
2869 Output_file::resize(off_t file_size)
2871 // If the mmap is mapping an anonymous memory buffer, this is easy:
2872 // just mremap to the new size. If it's mapping to a file, we want
2873 // to unmap to flush to the file, then remap after growing the file.
2874 if (this->map_is_anonymous_)
2876 void* base = ::mremap(this->base_, this->file_size_, file_size,
2877 MREMAP_MAYMOVE);
2878 if (base == MAP_FAILED)
2879 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
2880 this->base_ = static_cast<unsigned char*>(base);
2881 this->file_size_ = file_size;
2883 else
2885 this->unmap();
2886 this->file_size_ = file_size;
2887 this->map();
2891 // Map the file into memory.
2893 void
2894 Output_file::map()
2896 const int o = this->o_;
2898 // If the output file is not a regular file, don't try to mmap it;
2899 // instead, we'll mmap a block of memory (an anonymous buffer), and
2900 // then later write the buffer to the file.
2901 void* base;
2902 struct stat statbuf;
2903 if (o == STDOUT_FILENO || o == STDERR_FILENO
2904 || ::fstat(o, &statbuf) != 0
2905 || !S_ISREG(statbuf.st_mode)
2906 || this->is_temporary_)
2908 this->map_is_anonymous_ = true;
2909 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2910 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2912 else
2914 // Write out one byte to make the file the right size.
2915 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
2916 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
2917 char b = 0;
2918 if (::write(o, &b, 1) != 1)
2919 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
2921 // Map the file into memory.
2922 this->map_is_anonymous_ = false;
2923 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2924 MAP_SHARED, o, 0);
2926 if (base == MAP_FAILED)
2927 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
2928 this->base_ = static_cast<unsigned char*>(base);
2931 // Unmap the file from memory.
2933 void
2934 Output_file::unmap()
2936 if (::munmap(this->base_, this->file_size_) < 0)
2937 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
2938 this->base_ = NULL;
2941 // Close the output file.
2943 void
2944 Output_file::close()
2946 // If the map isn't file-backed, we need to write it now.
2947 if (this->map_is_anonymous_ && !this->is_temporary_)
2949 size_t bytes_to_write = this->file_size_;
2950 while (bytes_to_write > 0)
2952 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
2953 if (bytes_written == 0)
2954 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
2955 else if (bytes_written < 0)
2956 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
2957 else
2958 bytes_to_write -= bytes_written;
2961 this->unmap();
2963 // We don't close stdout or stderr
2964 if (this->o_ != STDOUT_FILENO
2965 && this->o_ != STDERR_FILENO
2966 && !this->is_temporary_)
2967 if (::close(this->o_) < 0)
2968 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
2969 this->o_ = -1;
2972 // Instantiate the templates we need. We could use the configure
2973 // script to restrict this to only the ones for implemented targets.
2975 #ifdef HAVE_TARGET_32_LITTLE
2976 template
2977 off_t
2978 Output_section::add_input_section<32, false>(
2979 Sized_relobj<32, false>* object,
2980 unsigned int shndx,
2981 const char* secname,
2982 const elfcpp::Shdr<32, false>& shdr,
2983 unsigned int reloc_shndx,
2984 bool have_sections_script);
2985 #endif
2987 #ifdef HAVE_TARGET_32_BIG
2988 template
2989 off_t
2990 Output_section::add_input_section<32, true>(
2991 Sized_relobj<32, true>* object,
2992 unsigned int shndx,
2993 const char* secname,
2994 const elfcpp::Shdr<32, true>& shdr,
2995 unsigned int reloc_shndx,
2996 bool have_sections_script);
2997 #endif
2999 #ifdef HAVE_TARGET_64_LITTLE
3000 template
3001 off_t
3002 Output_section::add_input_section<64, false>(
3003 Sized_relobj<64, false>* object,
3004 unsigned int shndx,
3005 const char* secname,
3006 const elfcpp::Shdr<64, false>& shdr,
3007 unsigned int reloc_shndx,
3008 bool have_sections_script);
3009 #endif
3011 #ifdef HAVE_TARGET_64_BIG
3012 template
3013 off_t
3014 Output_section::add_input_section<64, true>(
3015 Sized_relobj<64, true>* object,
3016 unsigned int shndx,
3017 const char* secname,
3018 const elfcpp::Shdr<64, true>& shdr,
3019 unsigned int reloc_shndx,
3020 bool have_sections_script);
3021 #endif
3023 #ifdef HAVE_TARGET_32_LITTLE
3024 template
3025 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
3026 #endif
3028 #ifdef HAVE_TARGET_32_BIG
3029 template
3030 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
3031 #endif
3033 #ifdef HAVE_TARGET_64_LITTLE
3034 template
3035 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
3036 #endif
3038 #ifdef HAVE_TARGET_64_BIG
3039 template
3040 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
3041 #endif
3043 #ifdef HAVE_TARGET_32_LITTLE
3044 template
3045 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
3046 #endif
3048 #ifdef HAVE_TARGET_32_BIG
3049 template
3050 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
3051 #endif
3053 #ifdef HAVE_TARGET_64_LITTLE
3054 template
3055 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
3056 #endif
3058 #ifdef HAVE_TARGET_64_BIG
3059 template
3060 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
3061 #endif
3063 #ifdef HAVE_TARGET_32_LITTLE
3064 template
3065 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
3066 #endif
3068 #ifdef HAVE_TARGET_32_BIG
3069 template
3070 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
3071 #endif
3073 #ifdef HAVE_TARGET_64_LITTLE
3074 template
3075 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
3076 #endif
3078 #ifdef HAVE_TARGET_64_BIG
3079 template
3080 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
3081 #endif
3083 #ifdef HAVE_TARGET_32_LITTLE
3084 template
3085 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
3086 #endif
3088 #ifdef HAVE_TARGET_32_BIG
3089 template
3090 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
3091 #endif
3093 #ifdef HAVE_TARGET_64_LITTLE
3094 template
3095 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
3096 #endif
3098 #ifdef HAVE_TARGET_64_BIG
3099 template
3100 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
3101 #endif
3103 #ifdef HAVE_TARGET_32_LITTLE
3104 template
3105 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3106 #endif
3108 #ifdef HAVE_TARGET_32_BIG
3109 template
3110 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3111 #endif
3113 #ifdef HAVE_TARGET_64_LITTLE
3114 template
3115 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3116 #endif
3118 #ifdef HAVE_TARGET_64_BIG
3119 template
3120 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3121 #endif
3123 #ifdef HAVE_TARGET_32_LITTLE
3124 template
3125 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3126 #endif
3128 #ifdef HAVE_TARGET_32_BIG
3129 template
3130 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3131 #endif
3133 #ifdef HAVE_TARGET_64_LITTLE
3134 template
3135 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3136 #endif
3138 #ifdef HAVE_TARGET_64_BIG
3139 template
3140 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3141 #endif
3143 #ifdef HAVE_TARGET_32_LITTLE
3144 template
3145 class Output_data_group<32, false>;
3146 #endif
3148 #ifdef HAVE_TARGET_32_BIG
3149 template
3150 class Output_data_group<32, true>;
3151 #endif
3153 #ifdef HAVE_TARGET_64_LITTLE
3154 template
3155 class Output_data_group<64, false>;
3156 #endif
3158 #ifdef HAVE_TARGET_64_BIG
3159 template
3160 class Output_data_group<64, true>;
3161 #endif
3163 #ifdef HAVE_TARGET_32_LITTLE
3164 template
3165 class Output_data_got<32, false>;
3166 #endif
3168 #ifdef HAVE_TARGET_32_BIG
3169 template
3170 class Output_data_got<32, true>;
3171 #endif
3173 #ifdef HAVE_TARGET_64_LITTLE
3174 template
3175 class Output_data_got<64, false>;
3176 #endif
3178 #ifdef HAVE_TARGET_64_BIG
3179 template
3180 class Output_data_got<64, true>;
3181 #endif
3183 } // End namespace gold.