bfd/
[binutils.git] / gold / output.cc
blob64fcb37187b5cbc2a004e803d6a721f00e8afe8d
1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009 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 "descriptors.h"
41 #include "output.h"
43 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
44 #ifndef MAP_ANONYMOUS
45 # define MAP_ANONYMOUS MAP_ANON
46 #endif
48 #ifndef HAVE_POSIX_FALLOCATE
49 // A dummy, non general, version of posix_fallocate. Here we just set
50 // the file size and hope that there is enough disk space. FIXME: We
51 // could allocate disk space by walking block by block and writing a
52 // zero byte into each block.
53 static int
54 posix_fallocate(int o, off_t offset, off_t len)
56 return ftruncate(o, offset + len);
58 #endif // !defined(HAVE_POSIX_FALLOCATE)
60 namespace gold
63 // Output_data variables.
65 bool Output_data::allocated_sizes_are_fixed;
67 // Output_data methods.
69 Output_data::~Output_data()
73 // Return the default alignment for the target size.
75 uint64_t
76 Output_data::default_alignment()
78 return Output_data::default_alignment_for_size(
79 parameters->target().get_size());
82 // Return the default alignment for a size--32 or 64.
84 uint64_t
85 Output_data::default_alignment_for_size(int size)
87 if (size == 32)
88 return 4;
89 else if (size == 64)
90 return 8;
91 else
92 gold_unreachable();
95 // Output_section_header methods. This currently assumes that the
96 // segment and section lists are complete at construction time.
98 Output_section_headers::Output_section_headers(
99 const Layout* layout,
100 const Layout::Segment_list* segment_list,
101 const Layout::Section_list* section_list,
102 const Layout::Section_list* unattached_section_list,
103 const Stringpool* secnamepool,
104 const Output_section* shstrtab_section)
105 : layout_(layout),
106 segment_list_(segment_list),
107 section_list_(section_list),
108 unattached_section_list_(unattached_section_list),
109 secnamepool_(secnamepool),
110 shstrtab_section_(shstrtab_section)
112 // Count all the sections. Start with 1 for the null section.
113 off_t count = 1;
114 if (!parameters->options().relocatable())
116 for (Layout::Segment_list::const_iterator p = segment_list->begin();
117 p != segment_list->end();
118 ++p)
119 if ((*p)->type() == elfcpp::PT_LOAD)
120 count += (*p)->output_section_count();
122 else
124 for (Layout::Section_list::const_iterator p = section_list->begin();
125 p != section_list->end();
126 ++p)
127 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
128 ++count;
130 count += unattached_section_list->size();
132 const int size = parameters->target().get_size();
133 int shdr_size;
134 if (size == 32)
135 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
136 else if (size == 64)
137 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
138 else
139 gold_unreachable();
141 this->set_data_size(count * shdr_size);
144 // Write out the section headers.
146 void
147 Output_section_headers::do_write(Output_file* of)
149 switch (parameters->size_and_endianness())
151 #ifdef HAVE_TARGET_32_LITTLE
152 case Parameters::TARGET_32_LITTLE:
153 this->do_sized_write<32, false>(of);
154 break;
155 #endif
156 #ifdef HAVE_TARGET_32_BIG
157 case Parameters::TARGET_32_BIG:
158 this->do_sized_write<32, true>(of);
159 break;
160 #endif
161 #ifdef HAVE_TARGET_64_LITTLE
162 case Parameters::TARGET_64_LITTLE:
163 this->do_sized_write<64, false>(of);
164 break;
165 #endif
166 #ifdef HAVE_TARGET_64_BIG
167 case Parameters::TARGET_64_BIG:
168 this->do_sized_write<64, true>(of);
169 break;
170 #endif
171 default:
172 gold_unreachable();
176 template<int size, bool big_endian>
177 void
178 Output_section_headers::do_sized_write(Output_file* of)
180 off_t all_shdrs_size = this->data_size();
181 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
183 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
184 unsigned char* v = view;
187 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
188 oshdr.put_sh_name(0);
189 oshdr.put_sh_type(elfcpp::SHT_NULL);
190 oshdr.put_sh_flags(0);
191 oshdr.put_sh_addr(0);
192 oshdr.put_sh_offset(0);
194 size_t section_count = (this->data_size()
195 / elfcpp::Elf_sizes<size>::shdr_size);
196 if (section_count < elfcpp::SHN_LORESERVE)
197 oshdr.put_sh_size(0);
198 else
199 oshdr.put_sh_size(section_count);
201 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
202 if (shstrndx < elfcpp::SHN_LORESERVE)
203 oshdr.put_sh_link(0);
204 else
205 oshdr.put_sh_link(shstrndx);
207 oshdr.put_sh_info(0);
208 oshdr.put_sh_addralign(0);
209 oshdr.put_sh_entsize(0);
212 v += shdr_size;
214 unsigned int shndx = 1;
215 if (!parameters->options().relocatable())
217 for (Layout::Segment_list::const_iterator p =
218 this->segment_list_->begin();
219 p != this->segment_list_->end();
220 ++p)
221 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
222 this->secnamepool_,
224 &shndx);
226 else
228 for (Layout::Section_list::const_iterator p =
229 this->section_list_->begin();
230 p != this->section_list_->end();
231 ++p)
233 // We do unallocated sections below, except that group
234 // sections have to come first.
235 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
236 && (*p)->type() != elfcpp::SHT_GROUP)
237 continue;
238 gold_assert(shndx == (*p)->out_shndx());
239 elfcpp::Shdr_write<size, big_endian> oshdr(v);
240 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
241 v += shdr_size;
242 ++shndx;
246 for (Layout::Section_list::const_iterator p =
247 this->unattached_section_list_->begin();
248 p != this->unattached_section_list_->end();
249 ++p)
251 // For a relocatable link, we did unallocated group sections
252 // above, since they have to come first.
253 if ((*p)->type() == elfcpp::SHT_GROUP
254 && parameters->options().relocatable())
255 continue;
256 gold_assert(shndx == (*p)->out_shndx());
257 elfcpp::Shdr_write<size, big_endian> oshdr(v);
258 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
259 v += shdr_size;
260 ++shndx;
263 of->write_output_view(this->offset(), all_shdrs_size, view);
266 // Output_segment_header methods.
268 Output_segment_headers::Output_segment_headers(
269 const Layout::Segment_list& segment_list)
270 : segment_list_(segment_list)
272 const int size = parameters->target().get_size();
273 int phdr_size;
274 if (size == 32)
275 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
276 else if (size == 64)
277 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
278 else
279 gold_unreachable();
281 this->set_data_size(segment_list.size() * phdr_size);
284 void
285 Output_segment_headers::do_write(Output_file* of)
287 switch (parameters->size_and_endianness())
289 #ifdef HAVE_TARGET_32_LITTLE
290 case Parameters::TARGET_32_LITTLE:
291 this->do_sized_write<32, false>(of);
292 break;
293 #endif
294 #ifdef HAVE_TARGET_32_BIG
295 case Parameters::TARGET_32_BIG:
296 this->do_sized_write<32, true>(of);
297 break;
298 #endif
299 #ifdef HAVE_TARGET_64_LITTLE
300 case Parameters::TARGET_64_LITTLE:
301 this->do_sized_write<64, false>(of);
302 break;
303 #endif
304 #ifdef HAVE_TARGET_64_BIG
305 case Parameters::TARGET_64_BIG:
306 this->do_sized_write<64, true>(of);
307 break;
308 #endif
309 default:
310 gold_unreachable();
314 template<int size, bool big_endian>
315 void
316 Output_segment_headers::do_sized_write(Output_file* of)
318 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
319 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
320 gold_assert(all_phdrs_size == this->data_size());
321 unsigned char* view = of->get_output_view(this->offset(),
322 all_phdrs_size);
323 unsigned char* v = view;
324 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
325 p != this->segment_list_.end();
326 ++p)
328 elfcpp::Phdr_write<size, big_endian> ophdr(v);
329 (*p)->write_header(&ophdr);
330 v += phdr_size;
333 gold_assert(v - view == all_phdrs_size);
335 of->write_output_view(this->offset(), all_phdrs_size, view);
338 // Output_file_header methods.
340 Output_file_header::Output_file_header(const Target* target,
341 const Symbol_table* symtab,
342 const Output_segment_headers* osh,
343 const char* entry)
344 : target_(target),
345 symtab_(symtab),
346 segment_header_(osh),
347 section_header_(NULL),
348 shstrtab_(NULL),
349 entry_(entry)
351 const int size = parameters->target().get_size();
352 int ehdr_size;
353 if (size == 32)
354 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
355 else if (size == 64)
356 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
357 else
358 gold_unreachable();
360 this->set_data_size(ehdr_size);
363 // Set the section table information for a file header.
365 void
366 Output_file_header::set_section_info(const Output_section_headers* shdrs,
367 const Output_section* shstrtab)
369 this->section_header_ = shdrs;
370 this->shstrtab_ = shstrtab;
373 // Write out the file header.
375 void
376 Output_file_header::do_write(Output_file* of)
378 gold_assert(this->offset() == 0);
380 switch (parameters->size_and_endianness())
382 #ifdef HAVE_TARGET_32_LITTLE
383 case Parameters::TARGET_32_LITTLE:
384 this->do_sized_write<32, false>(of);
385 break;
386 #endif
387 #ifdef HAVE_TARGET_32_BIG
388 case Parameters::TARGET_32_BIG:
389 this->do_sized_write<32, true>(of);
390 break;
391 #endif
392 #ifdef HAVE_TARGET_64_LITTLE
393 case Parameters::TARGET_64_LITTLE:
394 this->do_sized_write<64, false>(of);
395 break;
396 #endif
397 #ifdef HAVE_TARGET_64_BIG
398 case Parameters::TARGET_64_BIG:
399 this->do_sized_write<64, true>(of);
400 break;
401 #endif
402 default:
403 gold_unreachable();
407 // Write out the file header with appropriate size and endianess.
409 template<int size, bool big_endian>
410 void
411 Output_file_header::do_sized_write(Output_file* of)
413 gold_assert(this->offset() == 0);
415 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
416 unsigned char* view = of->get_output_view(0, ehdr_size);
417 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
419 unsigned char e_ident[elfcpp::EI_NIDENT];
420 memset(e_ident, 0, elfcpp::EI_NIDENT);
421 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
422 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
423 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
424 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
425 if (size == 32)
426 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
427 else if (size == 64)
428 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
429 else
430 gold_unreachable();
431 e_ident[elfcpp::EI_DATA] = (big_endian
432 ? elfcpp::ELFDATA2MSB
433 : elfcpp::ELFDATA2LSB);
434 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
435 oehdr.put_e_ident(e_ident);
437 elfcpp::ET e_type;
438 if (parameters->options().relocatable())
439 e_type = elfcpp::ET_REL;
440 else if (parameters->options().shared())
441 e_type = elfcpp::ET_DYN;
442 else
443 e_type = elfcpp::ET_EXEC;
444 oehdr.put_e_type(e_type);
446 oehdr.put_e_machine(this->target_->machine_code());
447 oehdr.put_e_version(elfcpp::EV_CURRENT);
449 oehdr.put_e_entry(this->entry<size>());
451 if (this->segment_header_ == NULL)
452 oehdr.put_e_phoff(0);
453 else
454 oehdr.put_e_phoff(this->segment_header_->offset());
456 oehdr.put_e_shoff(this->section_header_->offset());
458 // FIXME: The target needs to set the flags.
459 oehdr.put_e_flags(0);
461 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
463 if (this->segment_header_ == NULL)
465 oehdr.put_e_phentsize(0);
466 oehdr.put_e_phnum(0);
468 else
470 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
471 oehdr.put_e_phnum(this->segment_header_->data_size()
472 / elfcpp::Elf_sizes<size>::phdr_size);
475 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
476 size_t section_count = (this->section_header_->data_size()
477 / elfcpp::Elf_sizes<size>::shdr_size);
479 if (section_count < elfcpp::SHN_LORESERVE)
480 oehdr.put_e_shnum(this->section_header_->data_size()
481 / elfcpp::Elf_sizes<size>::shdr_size);
482 else
483 oehdr.put_e_shnum(0);
485 unsigned int shstrndx = this->shstrtab_->out_shndx();
486 if (shstrndx < elfcpp::SHN_LORESERVE)
487 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
488 else
489 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
491 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
492 // the e_ident field.
493 parameters->target().adjust_elf_header(view, ehdr_size);
495 of->write_output_view(0, ehdr_size, view);
498 // Return the value to use for the entry address. THIS->ENTRY_ is the
499 // symbol specified on the command line, if any.
501 template<int size>
502 typename elfcpp::Elf_types<size>::Elf_Addr
503 Output_file_header::entry()
505 const bool should_issue_warning = (this->entry_ != NULL
506 && !parameters->options().relocatable()
507 && !parameters->options().shared());
509 // FIXME: Need to support target specific entry symbol.
510 const char* entry = this->entry_;
511 if (entry == NULL)
512 entry = "_start";
514 Symbol* sym = this->symtab_->lookup(entry);
516 typename Sized_symbol<size>::Value_type v;
517 if (sym != NULL)
519 Sized_symbol<size>* ssym;
520 ssym = this->symtab_->get_sized_symbol<size>(sym);
521 if (!ssym->is_defined() && should_issue_warning)
522 gold_warning("entry symbol '%s' exists but is not defined", entry);
523 v = ssym->value();
525 else
527 // We couldn't find the entry symbol. See if we can parse it as
528 // a number. This supports, e.g., -e 0x1000.
529 char* endptr;
530 v = strtoull(entry, &endptr, 0);
531 if (*endptr != '\0')
533 if (should_issue_warning)
534 gold_warning("cannot find entry symbol '%s'", entry);
535 v = 0;
539 return v;
542 // Output_data_const methods.
544 void
545 Output_data_const::do_write(Output_file* of)
547 of->write(this->offset(), this->data_.data(), this->data_.size());
550 // Output_data_const_buffer methods.
552 void
553 Output_data_const_buffer::do_write(Output_file* of)
555 of->write(this->offset(), this->p_, this->data_size());
558 // Output_section_data methods.
560 // Record the output section, and set the entry size and such.
562 void
563 Output_section_data::set_output_section(Output_section* os)
565 gold_assert(this->output_section_ == NULL);
566 this->output_section_ = os;
567 this->do_adjust_output_section(os);
570 // Return the section index of the output section.
572 unsigned int
573 Output_section_data::do_out_shndx() const
575 gold_assert(this->output_section_ != NULL);
576 return this->output_section_->out_shndx();
579 // Set the alignment, which means we may need to update the alignment
580 // of the output section.
582 void
583 Output_section_data::set_addralign(uint64_t addralign)
585 this->addralign_ = addralign;
586 if (this->output_section_ != NULL
587 && this->output_section_->addralign() < addralign)
588 this->output_section_->set_addralign(addralign);
591 // Output_data_strtab methods.
593 // Set the final data size.
595 void
596 Output_data_strtab::set_final_data_size()
598 this->strtab_->set_string_offsets();
599 this->set_data_size(this->strtab_->get_strtab_size());
602 // Write out a string table.
604 void
605 Output_data_strtab::do_write(Output_file* of)
607 this->strtab_->write(of, this->offset());
610 // Output_reloc methods.
612 // A reloc against a global symbol.
614 template<bool dynamic, int size, bool big_endian>
615 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
616 Symbol* gsym,
617 unsigned int type,
618 Output_data* od,
619 Address address,
620 bool is_relative)
621 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
622 is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
624 // this->type_ is a bitfield; make sure TYPE fits.
625 gold_assert(this->type_ == type);
626 this->u1_.gsym = gsym;
627 this->u2_.od = od;
628 if (dynamic)
629 this->set_needs_dynsym_index();
632 template<bool dynamic, int size, bool big_endian>
633 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
634 Symbol* gsym,
635 unsigned int type,
636 Sized_relobj<size, big_endian>* relobj,
637 unsigned int shndx,
638 Address address,
639 bool is_relative)
640 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
641 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
643 gold_assert(shndx != INVALID_CODE);
644 // this->type_ is a bitfield; make sure TYPE fits.
645 gold_assert(this->type_ == type);
646 this->u1_.gsym = gsym;
647 this->u2_.relobj = relobj;
648 if (dynamic)
649 this->set_needs_dynsym_index();
652 // A reloc against a local symbol.
654 template<bool dynamic, int size, bool big_endian>
655 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
656 Sized_relobj<size, big_endian>* relobj,
657 unsigned int local_sym_index,
658 unsigned int type,
659 Output_data* od,
660 Address address,
661 bool is_relative,
662 bool is_section_symbol)
663 : address_(address), local_sym_index_(local_sym_index), type_(type),
664 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
665 shndx_(INVALID_CODE)
667 gold_assert(local_sym_index != GSYM_CODE
668 && local_sym_index != INVALID_CODE);
669 // this->type_ is a bitfield; make sure TYPE fits.
670 gold_assert(this->type_ == type);
671 this->u1_.relobj = relobj;
672 this->u2_.od = od;
673 if (dynamic)
674 this->set_needs_dynsym_index();
677 template<bool dynamic, int size, bool big_endian>
678 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
679 Sized_relobj<size, big_endian>* relobj,
680 unsigned int local_sym_index,
681 unsigned int type,
682 unsigned int shndx,
683 Address address,
684 bool is_relative,
685 bool is_section_symbol)
686 : address_(address), local_sym_index_(local_sym_index), type_(type),
687 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
688 shndx_(shndx)
690 gold_assert(local_sym_index != GSYM_CODE
691 && local_sym_index != INVALID_CODE);
692 gold_assert(shndx != INVALID_CODE);
693 // this->type_ is a bitfield; make sure TYPE fits.
694 gold_assert(this->type_ == type);
695 this->u1_.relobj = relobj;
696 this->u2_.relobj = relobj;
697 if (dynamic)
698 this->set_needs_dynsym_index();
701 // A reloc against the STT_SECTION symbol of an output section.
703 template<bool dynamic, int size, bool big_endian>
704 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
705 Output_section* os,
706 unsigned int type,
707 Output_data* od,
708 Address address)
709 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
710 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
712 // this->type_ is a bitfield; make sure TYPE fits.
713 gold_assert(this->type_ == type);
714 this->u1_.os = os;
715 this->u2_.od = od;
716 if (dynamic)
717 this->set_needs_dynsym_index();
718 else
719 os->set_needs_symtab_index();
722 template<bool dynamic, int size, bool big_endian>
723 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
724 Output_section* os,
725 unsigned int type,
726 Sized_relobj<size, big_endian>* relobj,
727 unsigned int shndx,
728 Address address)
729 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
730 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
732 gold_assert(shndx != INVALID_CODE);
733 // this->type_ is a bitfield; make sure TYPE fits.
734 gold_assert(this->type_ == type);
735 this->u1_.os = os;
736 this->u2_.relobj = relobj;
737 if (dynamic)
738 this->set_needs_dynsym_index();
739 else
740 os->set_needs_symtab_index();
743 // Record that we need a dynamic symbol index for this relocation.
745 template<bool dynamic, int size, bool big_endian>
746 void
747 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
748 set_needs_dynsym_index()
750 if (this->is_relative_)
751 return;
752 switch (this->local_sym_index_)
754 case INVALID_CODE:
755 gold_unreachable();
757 case GSYM_CODE:
758 this->u1_.gsym->set_needs_dynsym_entry();
759 break;
761 case SECTION_CODE:
762 this->u1_.os->set_needs_dynsym_index();
763 break;
765 case 0:
766 break;
768 default:
770 const unsigned int lsi = this->local_sym_index_;
771 if (!this->is_section_symbol_)
772 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
773 else
774 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
776 break;
780 // Get the symbol index of a relocation.
782 template<bool dynamic, int size, bool big_endian>
783 unsigned int
784 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
785 const
787 unsigned int index;
788 switch (this->local_sym_index_)
790 case INVALID_CODE:
791 gold_unreachable();
793 case GSYM_CODE:
794 if (this->u1_.gsym == NULL)
795 index = 0;
796 else if (dynamic)
797 index = this->u1_.gsym->dynsym_index();
798 else
799 index = this->u1_.gsym->symtab_index();
800 break;
802 case SECTION_CODE:
803 if (dynamic)
804 index = this->u1_.os->dynsym_index();
805 else
806 index = this->u1_.os->symtab_index();
807 break;
809 case 0:
810 // Relocations without symbols use a symbol index of 0.
811 index = 0;
812 break;
814 default:
816 const unsigned int lsi = this->local_sym_index_;
817 if (!this->is_section_symbol_)
819 if (dynamic)
820 index = this->u1_.relobj->dynsym_index(lsi);
821 else
822 index = this->u1_.relobj->symtab_index(lsi);
824 else
826 Output_section* os = this->u1_.relobj->output_section(lsi);
827 gold_assert(os != NULL);
828 if (dynamic)
829 index = os->dynsym_index();
830 else
831 index = os->symtab_index();
834 break;
836 gold_assert(index != -1U);
837 return index;
840 // For a local section symbol, get the address of the offset ADDEND
841 // within the input section.
843 template<bool dynamic, int size, bool big_endian>
844 typename elfcpp::Elf_types<size>::Elf_Addr
845 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
846 local_section_offset(Addend addend) const
848 gold_assert(this->local_sym_index_ != GSYM_CODE
849 && this->local_sym_index_ != SECTION_CODE
850 && this->local_sym_index_ != INVALID_CODE
851 && this->is_section_symbol_);
852 const unsigned int lsi = this->local_sym_index_;
853 Output_section* os = this->u1_.relobj->output_section(lsi);
854 gold_assert(os != NULL);
855 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
856 if (offset != invalid_address)
857 return offset + addend;
858 // This is a merge section.
859 offset = os->output_address(this->u1_.relobj, lsi, addend);
860 gold_assert(offset != invalid_address);
861 return offset;
864 // Get the output address of a relocation.
866 template<bool dynamic, int size, bool big_endian>
867 typename elfcpp::Elf_types<size>::Elf_Addr
868 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
870 Address address = this->address_;
871 if (this->shndx_ != INVALID_CODE)
873 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
874 gold_assert(os != NULL);
875 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
876 if (off != invalid_address)
877 address += os->address() + off;
878 else
880 address = os->output_address(this->u2_.relobj, this->shndx_,
881 address);
882 gold_assert(address != invalid_address);
885 else if (this->u2_.od != NULL)
886 address += this->u2_.od->address();
887 return address;
890 // Write out the offset and info fields of a Rel or Rela relocation
891 // entry.
893 template<bool dynamic, int size, bool big_endian>
894 template<typename Write_rel>
895 void
896 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
897 Write_rel* wr) const
899 wr->put_r_offset(this->get_address());
900 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
901 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
904 // Write out a Rel relocation.
906 template<bool dynamic, int size, bool big_endian>
907 void
908 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
909 unsigned char* pov) const
911 elfcpp::Rel_write<size, big_endian> orel(pov);
912 this->write_rel(&orel);
915 // Get the value of the symbol referred to by a Rel relocation.
917 template<bool dynamic, int size, bool big_endian>
918 typename elfcpp::Elf_types<size>::Elf_Addr
919 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
920 Addend addend) const
922 if (this->local_sym_index_ == GSYM_CODE)
924 const Sized_symbol<size>* sym;
925 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
926 return sym->value() + addend;
928 gold_assert(this->local_sym_index_ != SECTION_CODE
929 && this->local_sym_index_ != INVALID_CODE
930 && !this->is_section_symbol_);
931 const unsigned int lsi = this->local_sym_index_;
932 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
933 return symval->value(this->u1_.relobj, addend);
936 // Reloc comparison. This function sorts the dynamic relocs for the
937 // benefit of the dynamic linker. First we sort all relative relocs
938 // to the front. Among relative relocs, we sort by output address.
939 // Among non-relative relocs, we sort by symbol index, then by output
940 // address.
942 template<bool dynamic, int size, bool big_endian>
944 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
945 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
946 const
948 if (this->is_relative_)
950 if (!r2.is_relative_)
951 return -1;
952 // Otherwise sort by reloc address below.
954 else if (r2.is_relative_)
955 return 1;
956 else
958 unsigned int sym1 = this->get_symbol_index();
959 unsigned int sym2 = r2.get_symbol_index();
960 if (sym1 < sym2)
961 return -1;
962 else if (sym1 > sym2)
963 return 1;
964 // Otherwise sort by reloc address.
967 section_offset_type addr1 = this->get_address();
968 section_offset_type addr2 = r2.get_address();
969 if (addr1 < addr2)
970 return -1;
971 else if (addr1 > addr2)
972 return 1;
974 // Final tie breaker, in order to generate the same output on any
975 // host: reloc type.
976 unsigned int type1 = this->type_;
977 unsigned int type2 = r2.type_;
978 if (type1 < type2)
979 return -1;
980 else if (type1 > type2)
981 return 1;
983 // These relocs appear to be exactly the same.
984 return 0;
987 // Write out a Rela relocation.
989 template<bool dynamic, int size, bool big_endian>
990 void
991 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
992 unsigned char* pov) const
994 elfcpp::Rela_write<size, big_endian> orel(pov);
995 this->rel_.write_rel(&orel);
996 Addend addend = this->addend_;
997 if (this->rel_.is_relative())
998 addend = this->rel_.symbol_value(addend);
999 else if (this->rel_.is_local_section_symbol())
1000 addend = this->rel_.local_section_offset(addend);
1001 orel.put_r_addend(addend);
1004 // Output_data_reloc_base methods.
1006 // Adjust the output section.
1008 template<int sh_type, bool dynamic, int size, bool big_endian>
1009 void
1010 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1011 ::do_adjust_output_section(Output_section* os)
1013 if (sh_type == elfcpp::SHT_REL)
1014 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1015 else if (sh_type == elfcpp::SHT_RELA)
1016 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1017 else
1018 gold_unreachable();
1019 if (dynamic)
1020 os->set_should_link_to_dynsym();
1021 else
1022 os->set_should_link_to_symtab();
1025 // Write out relocation data.
1027 template<int sh_type, bool dynamic, int size, bool big_endian>
1028 void
1029 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1030 Output_file* of)
1032 const off_t off = this->offset();
1033 const off_t oview_size = this->data_size();
1034 unsigned char* const oview = of->get_output_view(off, oview_size);
1036 if (this->sort_relocs_)
1038 gold_assert(dynamic);
1039 std::sort(this->relocs_.begin(), this->relocs_.end(),
1040 Sort_relocs_comparison());
1043 unsigned char* pov = oview;
1044 for (typename Relocs::const_iterator p = this->relocs_.begin();
1045 p != this->relocs_.end();
1046 ++p)
1048 p->write(pov);
1049 pov += reloc_size;
1052 gold_assert(pov - oview == oview_size);
1054 of->write_output_view(off, oview_size, oview);
1056 // We no longer need the relocation entries.
1057 this->relocs_.clear();
1060 // Class Output_relocatable_relocs.
1062 template<int sh_type, int size, bool big_endian>
1063 void
1064 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1066 this->set_data_size(this->rr_->output_reloc_count()
1067 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1070 // class Output_data_group.
1072 template<int size, bool big_endian>
1073 Output_data_group<size, big_endian>::Output_data_group(
1074 Sized_relobj<size, big_endian>* relobj,
1075 section_size_type entry_count,
1076 elfcpp::Elf_Word flags,
1077 std::vector<unsigned int>* input_shndxes)
1078 : Output_section_data(entry_count * 4, 4),
1079 relobj_(relobj),
1080 flags_(flags)
1082 this->input_shndxes_.swap(*input_shndxes);
1085 // Write out the section group, which means translating the section
1086 // indexes to apply to the output file.
1088 template<int size, bool big_endian>
1089 void
1090 Output_data_group<size, big_endian>::do_write(Output_file* of)
1092 const off_t off = this->offset();
1093 const section_size_type oview_size =
1094 convert_to_section_size_type(this->data_size());
1095 unsigned char* const oview = of->get_output_view(off, oview_size);
1097 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1098 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1099 ++contents;
1101 for (std::vector<unsigned int>::const_iterator p =
1102 this->input_shndxes_.begin();
1103 p != this->input_shndxes_.end();
1104 ++p, ++contents)
1106 Output_section* os = this->relobj_->output_section(*p);
1108 unsigned int output_shndx;
1109 if (os != NULL)
1110 output_shndx = os->out_shndx();
1111 else
1113 this->relobj_->error(_("section group retained but "
1114 "group element discarded"));
1115 output_shndx = 0;
1118 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1121 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1122 gold_assert(wrote == oview_size);
1124 of->write_output_view(off, oview_size, oview);
1126 // We no longer need this information.
1127 this->input_shndxes_.clear();
1130 // Output_data_got::Got_entry methods.
1132 // Write out the entry.
1134 template<int size, bool big_endian>
1135 void
1136 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1138 Valtype val = 0;
1140 switch (this->local_sym_index_)
1142 case GSYM_CODE:
1144 // If the symbol is resolved locally, we need to write out the
1145 // link-time value, which will be relocated dynamically by a
1146 // RELATIVE relocation.
1147 Symbol* gsym = this->u_.gsym;
1148 Sized_symbol<size>* sgsym;
1149 // This cast is a bit ugly. We don't want to put a
1150 // virtual method in Symbol, because we want Symbol to be
1151 // as small as possible.
1152 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1153 val = sgsym->value();
1155 break;
1157 case CONSTANT_CODE:
1158 val = this->u_.constant;
1159 break;
1161 default:
1163 const unsigned int lsi = this->local_sym_index_;
1164 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1165 val = symval->value(this->u_.object, 0);
1167 break;
1170 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1173 // Output_data_got methods.
1175 // Add an entry for a global symbol to the GOT. This returns true if
1176 // this is a new GOT entry, false if the symbol already had a GOT
1177 // entry.
1179 template<int size, bool big_endian>
1180 bool
1181 Output_data_got<size, big_endian>::add_global(
1182 Symbol* gsym,
1183 unsigned int got_type)
1185 if (gsym->has_got_offset(got_type))
1186 return false;
1188 this->entries_.push_back(Got_entry(gsym));
1189 this->set_got_size();
1190 gsym->set_got_offset(got_type, this->last_got_offset());
1191 return true;
1194 // Add an entry for a global symbol to the GOT, and add a dynamic
1195 // relocation of type R_TYPE for the GOT entry.
1196 template<int size, bool big_endian>
1197 void
1198 Output_data_got<size, big_endian>::add_global_with_rel(
1199 Symbol* gsym,
1200 unsigned int got_type,
1201 Rel_dyn* rel_dyn,
1202 unsigned int r_type)
1204 if (gsym->has_got_offset(got_type))
1205 return;
1207 this->entries_.push_back(Got_entry());
1208 this->set_got_size();
1209 unsigned int got_offset = this->last_got_offset();
1210 gsym->set_got_offset(got_type, got_offset);
1211 rel_dyn->add_global(gsym, r_type, this, got_offset);
1214 template<int size, bool big_endian>
1215 void
1216 Output_data_got<size, big_endian>::add_global_with_rela(
1217 Symbol* gsym,
1218 unsigned int got_type,
1219 Rela_dyn* rela_dyn,
1220 unsigned int r_type)
1222 if (gsym->has_got_offset(got_type))
1223 return;
1225 this->entries_.push_back(Got_entry());
1226 this->set_got_size();
1227 unsigned int got_offset = this->last_got_offset();
1228 gsym->set_got_offset(got_type, got_offset);
1229 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1232 // Add a pair of entries for a global symbol to the GOT, and add
1233 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1234 // If R_TYPE_2 == 0, add the second entry with no relocation.
1235 template<int size, bool big_endian>
1236 void
1237 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1238 Symbol* gsym,
1239 unsigned int got_type,
1240 Rel_dyn* rel_dyn,
1241 unsigned int r_type_1,
1242 unsigned int r_type_2)
1244 if (gsym->has_got_offset(got_type))
1245 return;
1247 this->entries_.push_back(Got_entry());
1248 unsigned int got_offset = this->last_got_offset();
1249 gsym->set_got_offset(got_type, got_offset);
1250 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1252 this->entries_.push_back(Got_entry());
1253 if (r_type_2 != 0)
1255 got_offset = this->last_got_offset();
1256 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1259 this->set_got_size();
1262 template<int size, bool big_endian>
1263 void
1264 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1265 Symbol* gsym,
1266 unsigned int got_type,
1267 Rela_dyn* rela_dyn,
1268 unsigned int r_type_1,
1269 unsigned int r_type_2)
1271 if (gsym->has_got_offset(got_type))
1272 return;
1274 this->entries_.push_back(Got_entry());
1275 unsigned int got_offset = this->last_got_offset();
1276 gsym->set_got_offset(got_type, got_offset);
1277 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1279 this->entries_.push_back(Got_entry());
1280 if (r_type_2 != 0)
1282 got_offset = this->last_got_offset();
1283 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1286 this->set_got_size();
1289 // Add an entry for a local symbol to the GOT. This returns true if
1290 // this is a new GOT entry, false if the symbol already has a GOT
1291 // entry.
1293 template<int size, bool big_endian>
1294 bool
1295 Output_data_got<size, big_endian>::add_local(
1296 Sized_relobj<size, big_endian>* object,
1297 unsigned int symndx,
1298 unsigned int got_type)
1300 if (object->local_has_got_offset(symndx, got_type))
1301 return false;
1303 this->entries_.push_back(Got_entry(object, symndx));
1304 this->set_got_size();
1305 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1306 return true;
1309 // Add an entry for a local symbol to the GOT, and add a dynamic
1310 // relocation of type R_TYPE for the GOT entry.
1311 template<int size, bool big_endian>
1312 void
1313 Output_data_got<size, big_endian>::add_local_with_rel(
1314 Sized_relobj<size, big_endian>* object,
1315 unsigned int symndx,
1316 unsigned int got_type,
1317 Rel_dyn* rel_dyn,
1318 unsigned int r_type)
1320 if (object->local_has_got_offset(symndx, got_type))
1321 return;
1323 this->entries_.push_back(Got_entry());
1324 this->set_got_size();
1325 unsigned int got_offset = this->last_got_offset();
1326 object->set_local_got_offset(symndx, got_type, got_offset);
1327 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1330 template<int size, bool big_endian>
1331 void
1332 Output_data_got<size, big_endian>::add_local_with_rela(
1333 Sized_relobj<size, big_endian>* object,
1334 unsigned int symndx,
1335 unsigned int got_type,
1336 Rela_dyn* rela_dyn,
1337 unsigned int r_type)
1339 if (object->local_has_got_offset(symndx, got_type))
1340 return;
1342 this->entries_.push_back(Got_entry());
1343 this->set_got_size();
1344 unsigned int got_offset = this->last_got_offset();
1345 object->set_local_got_offset(symndx, got_type, got_offset);
1346 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1349 // Add a pair of entries for a local symbol to the GOT, and add
1350 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1351 // If R_TYPE_2 == 0, add the second entry with no relocation.
1352 template<int size, bool big_endian>
1353 void
1354 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1355 Sized_relobj<size, big_endian>* object,
1356 unsigned int symndx,
1357 unsigned int shndx,
1358 unsigned int got_type,
1359 Rel_dyn* rel_dyn,
1360 unsigned int r_type_1,
1361 unsigned int r_type_2)
1363 if (object->local_has_got_offset(symndx, got_type))
1364 return;
1366 this->entries_.push_back(Got_entry());
1367 unsigned int got_offset = this->last_got_offset();
1368 object->set_local_got_offset(symndx, got_type, got_offset);
1369 Output_section* os = object->output_section(shndx);
1370 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1372 this->entries_.push_back(Got_entry(object, symndx));
1373 if (r_type_2 != 0)
1375 got_offset = this->last_got_offset();
1376 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1379 this->set_got_size();
1382 template<int size, bool big_endian>
1383 void
1384 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1385 Sized_relobj<size, big_endian>* object,
1386 unsigned int symndx,
1387 unsigned int shndx,
1388 unsigned int got_type,
1389 Rela_dyn* rela_dyn,
1390 unsigned int r_type_1,
1391 unsigned int r_type_2)
1393 if (object->local_has_got_offset(symndx, got_type))
1394 return;
1396 this->entries_.push_back(Got_entry());
1397 unsigned int got_offset = this->last_got_offset();
1398 object->set_local_got_offset(symndx, got_type, got_offset);
1399 Output_section* os = object->output_section(shndx);
1400 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1402 this->entries_.push_back(Got_entry(object, symndx));
1403 if (r_type_2 != 0)
1405 got_offset = this->last_got_offset();
1406 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1409 this->set_got_size();
1412 // Write out the GOT.
1414 template<int size, bool big_endian>
1415 void
1416 Output_data_got<size, big_endian>::do_write(Output_file* of)
1418 const int add = size / 8;
1420 const off_t off = this->offset();
1421 const off_t oview_size = this->data_size();
1422 unsigned char* const oview = of->get_output_view(off, oview_size);
1424 unsigned char* pov = oview;
1425 for (typename Got_entries::const_iterator p = this->entries_.begin();
1426 p != this->entries_.end();
1427 ++p)
1429 p->write(pov);
1430 pov += add;
1433 gold_assert(pov - oview == oview_size);
1435 of->write_output_view(off, oview_size, oview);
1437 // We no longer need the GOT entries.
1438 this->entries_.clear();
1441 // Output_data_dynamic::Dynamic_entry methods.
1443 // Write out the entry.
1445 template<int size, bool big_endian>
1446 void
1447 Output_data_dynamic::Dynamic_entry::write(
1448 unsigned char* pov,
1449 const Stringpool* pool) const
1451 typename elfcpp::Elf_types<size>::Elf_WXword val;
1452 switch (this->offset_)
1454 case DYNAMIC_NUMBER:
1455 val = this->u_.val;
1456 break;
1458 case DYNAMIC_SECTION_SIZE:
1459 val = this->u_.od->data_size();
1460 break;
1462 case DYNAMIC_SYMBOL:
1464 const Sized_symbol<size>* s =
1465 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1466 val = s->value();
1468 break;
1470 case DYNAMIC_STRING:
1471 val = pool->get_offset(this->u_.str);
1472 break;
1474 default:
1475 val = this->u_.od->address() + this->offset_;
1476 break;
1479 elfcpp::Dyn_write<size, big_endian> dw(pov);
1480 dw.put_d_tag(this->tag_);
1481 dw.put_d_val(val);
1484 // Output_data_dynamic methods.
1486 // Adjust the output section to set the entry size.
1488 void
1489 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1491 if (parameters->target().get_size() == 32)
1492 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1493 else if (parameters->target().get_size() == 64)
1494 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1495 else
1496 gold_unreachable();
1499 // Set the final data size.
1501 void
1502 Output_data_dynamic::set_final_data_size()
1504 // Add the terminating entry.
1505 this->add_constant(elfcpp::DT_NULL, 0);
1507 int dyn_size;
1508 if (parameters->target().get_size() == 32)
1509 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1510 else if (parameters->target().get_size() == 64)
1511 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1512 else
1513 gold_unreachable();
1514 this->set_data_size(this->entries_.size() * dyn_size);
1517 // Write out the dynamic entries.
1519 void
1520 Output_data_dynamic::do_write(Output_file* of)
1522 switch (parameters->size_and_endianness())
1524 #ifdef HAVE_TARGET_32_LITTLE
1525 case Parameters::TARGET_32_LITTLE:
1526 this->sized_write<32, false>(of);
1527 break;
1528 #endif
1529 #ifdef HAVE_TARGET_32_BIG
1530 case Parameters::TARGET_32_BIG:
1531 this->sized_write<32, true>(of);
1532 break;
1533 #endif
1534 #ifdef HAVE_TARGET_64_LITTLE
1535 case Parameters::TARGET_64_LITTLE:
1536 this->sized_write<64, false>(of);
1537 break;
1538 #endif
1539 #ifdef HAVE_TARGET_64_BIG
1540 case Parameters::TARGET_64_BIG:
1541 this->sized_write<64, true>(of);
1542 break;
1543 #endif
1544 default:
1545 gold_unreachable();
1549 template<int size, bool big_endian>
1550 void
1551 Output_data_dynamic::sized_write(Output_file* of)
1553 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1555 const off_t offset = this->offset();
1556 const off_t oview_size = this->data_size();
1557 unsigned char* const oview = of->get_output_view(offset, oview_size);
1559 unsigned char* pov = oview;
1560 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1561 p != this->entries_.end();
1562 ++p)
1564 p->write<size, big_endian>(pov, this->pool_);
1565 pov += dyn_size;
1568 gold_assert(pov - oview == oview_size);
1570 of->write_output_view(offset, oview_size, oview);
1572 // We no longer need the dynamic entries.
1573 this->entries_.clear();
1576 // Class Output_symtab_xindex.
1578 void
1579 Output_symtab_xindex::do_write(Output_file* of)
1581 const off_t offset = this->offset();
1582 const off_t oview_size = this->data_size();
1583 unsigned char* const oview = of->get_output_view(offset, oview_size);
1585 memset(oview, 0, oview_size);
1587 if (parameters->target().is_big_endian())
1588 this->endian_do_write<true>(oview);
1589 else
1590 this->endian_do_write<false>(oview);
1592 of->write_output_view(offset, oview_size, oview);
1594 // We no longer need the data.
1595 this->entries_.clear();
1598 template<bool big_endian>
1599 void
1600 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1602 for (Xindex_entries::const_iterator p = this->entries_.begin();
1603 p != this->entries_.end();
1604 ++p)
1605 elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
1608 // Output_section::Input_section methods.
1610 // Return the data size. For an input section we store the size here.
1611 // For an Output_section_data, we have to ask it for the size.
1613 off_t
1614 Output_section::Input_section::data_size() const
1616 if (this->is_input_section())
1617 return this->u1_.data_size;
1618 else
1619 return this->u2_.posd->data_size();
1622 // Set the address and file offset.
1624 void
1625 Output_section::Input_section::set_address_and_file_offset(
1626 uint64_t address,
1627 off_t file_offset,
1628 off_t section_file_offset)
1630 if (this->is_input_section())
1631 this->u2_.object->set_section_offset(this->shndx_,
1632 file_offset - section_file_offset);
1633 else
1634 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1637 // Reset the address and file offset.
1639 void
1640 Output_section::Input_section::reset_address_and_file_offset()
1642 if (!this->is_input_section())
1643 this->u2_.posd->reset_address_and_file_offset();
1646 // Finalize the data size.
1648 void
1649 Output_section::Input_section::finalize_data_size()
1651 if (!this->is_input_section())
1652 this->u2_.posd->finalize_data_size();
1655 // Try to turn an input offset into an output offset. We want to
1656 // return the output offset relative to the start of this
1657 // Input_section in the output section.
1659 inline bool
1660 Output_section::Input_section::output_offset(
1661 const Relobj* object,
1662 unsigned int shndx,
1663 section_offset_type offset,
1664 section_offset_type *poutput) const
1666 if (!this->is_input_section())
1667 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1668 else
1670 if (this->shndx_ != shndx || this->u2_.object != object)
1671 return false;
1672 *poutput = offset;
1673 return true;
1677 // Return whether this is the merge section for the input section
1678 // SHNDX in OBJECT.
1680 inline bool
1681 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1682 unsigned int shndx) const
1684 if (this->is_input_section())
1685 return false;
1686 return this->u2_.posd->is_merge_section_for(object, shndx);
1689 // Write out the data. We don't have to do anything for an input
1690 // section--they are handled via Object::relocate--but this is where
1691 // we write out the data for an Output_section_data.
1693 void
1694 Output_section::Input_section::write(Output_file* of)
1696 if (!this->is_input_section())
1697 this->u2_.posd->write(of);
1700 // Write the data to a buffer. As for write(), we don't have to do
1701 // anything for an input section.
1703 void
1704 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1706 if (!this->is_input_section())
1707 this->u2_.posd->write_to_buffer(buffer);
1710 // Print to a map file.
1712 void
1713 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1715 switch (this->shndx_)
1717 case OUTPUT_SECTION_CODE:
1718 case MERGE_DATA_SECTION_CODE:
1719 case MERGE_STRING_SECTION_CODE:
1720 this->u2_.posd->print_to_mapfile(mapfile);
1721 break;
1723 default:
1724 mapfile->print_input_section(this->u2_.object, this->shndx_);
1725 break;
1729 // Output_section methods.
1731 // Construct an Output_section. NAME will point into a Stringpool.
1733 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1734 elfcpp::Elf_Xword flags)
1735 : name_(name),
1736 addralign_(0),
1737 entsize_(0),
1738 load_address_(0),
1739 link_section_(NULL),
1740 link_(0),
1741 info_section_(NULL),
1742 info_symndx_(NULL),
1743 info_(0),
1744 type_(type),
1745 flags_(flags),
1746 out_shndx_(-1U),
1747 symtab_index_(0),
1748 dynsym_index_(0),
1749 input_sections_(),
1750 first_input_offset_(0),
1751 fills_(),
1752 postprocessing_buffer_(NULL),
1753 needs_symtab_index_(false),
1754 needs_dynsym_index_(false),
1755 should_link_to_symtab_(false),
1756 should_link_to_dynsym_(false),
1757 after_input_sections_(false),
1758 requires_postprocessing_(false),
1759 found_in_sections_clause_(false),
1760 has_load_address_(false),
1761 info_uses_section_index_(false),
1762 may_sort_attached_input_sections_(false),
1763 must_sort_attached_input_sections_(false),
1764 attached_input_sections_are_sorted_(false),
1765 is_relro_(false),
1766 is_relro_local_(false),
1767 is_small_section_(false),
1768 is_large_section_(false),
1769 tls_offset_(0)
1771 // An unallocated section has no address. Forcing this means that
1772 // we don't need special treatment for symbols defined in debug
1773 // sections.
1774 if ((flags & elfcpp::SHF_ALLOC) == 0)
1775 this->set_address(0);
1778 Output_section::~Output_section()
1782 // Set the entry size.
1784 void
1785 Output_section::set_entsize(uint64_t v)
1787 if (this->entsize_ == 0)
1788 this->entsize_ = v;
1789 else
1790 gold_assert(this->entsize_ == v);
1793 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1794 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1795 // relocation section which applies to this section, or 0 if none, or
1796 // -1U if more than one. Return the offset of the input section
1797 // within the output section. Return -1 if the input section will
1798 // receive special handling. In the normal case we don't always keep
1799 // track of input sections for an Output_section. Instead, each
1800 // Object keeps track of the Output_section for each of its input
1801 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1802 // track of input sections here; this is used when SECTIONS appears in
1803 // a linker script.
1805 template<int size, bool big_endian>
1806 off_t
1807 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1808 unsigned int shndx,
1809 const char* secname,
1810 const elfcpp::Shdr<size, big_endian>& shdr,
1811 unsigned int reloc_shndx,
1812 bool have_sections_script)
1814 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1815 if ((addralign & (addralign - 1)) != 0)
1817 object->error(_("invalid alignment %lu for section \"%s\""),
1818 static_cast<unsigned long>(addralign), secname);
1819 addralign = 1;
1822 if (addralign > this->addralign_)
1823 this->addralign_ = addralign;
1825 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1826 this->update_flags_for_input_section(sh_flags);
1828 uint64_t entsize = shdr.get_sh_entsize();
1830 // .debug_str is a mergeable string section, but is not always so
1831 // marked by compilers. Mark manually here so we can optimize.
1832 if (strcmp(secname, ".debug_str") == 0)
1834 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1835 entsize = 1;
1838 // If this is a SHF_MERGE section, we pass all the input sections to
1839 // a Output_data_merge. We don't try to handle relocations for such
1840 // a section. We don't try to handle empty merge sections--they
1841 // mess up the mappings, and are useless anyhow.
1842 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1843 && reloc_shndx == 0
1844 && shdr.get_sh_size() > 0)
1846 if (this->add_merge_input_section(object, shndx, sh_flags,
1847 entsize, addralign))
1849 // Tell the relocation routines that they need to call the
1850 // output_offset method to determine the final address.
1851 return -1;
1855 off_t offset_in_section = this->current_data_size_for_child();
1856 off_t aligned_offset_in_section = align_address(offset_in_section,
1857 addralign);
1859 if (aligned_offset_in_section > offset_in_section
1860 && !have_sections_script
1861 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1862 && object->target()->has_code_fill())
1864 // We need to add some fill data. Using fill_list_ when
1865 // possible is an optimization, since we will often have fill
1866 // sections without input sections.
1867 off_t fill_len = aligned_offset_in_section - offset_in_section;
1868 if (this->input_sections_.empty())
1869 this->fills_.push_back(Fill(offset_in_section, fill_len));
1870 else
1872 // FIXME: When relaxing, the size needs to adjust to
1873 // maintain a constant alignment.
1874 std::string fill_data(object->target()->code_fill(fill_len));
1875 Output_data_const* odc = new Output_data_const(fill_data, 1);
1876 this->input_sections_.push_back(Input_section(odc));
1880 this->set_current_data_size_for_child(aligned_offset_in_section
1881 + shdr.get_sh_size());
1883 // We need to keep track of this section if we are already keeping
1884 // track of sections, or if we are relaxing. Also, if this is a
1885 // section which requires sorting, or which may require sorting in
1886 // the future, we keep track of the sections. FIXME: Add test for
1887 // relaxing.
1888 if (have_sections_script
1889 || !this->input_sections_.empty()
1890 || this->may_sort_attached_input_sections()
1891 || this->must_sort_attached_input_sections()
1892 || parameters->options().user_set_Map())
1893 this->input_sections_.push_back(Input_section(object, shndx,
1894 shdr.get_sh_size(),
1895 addralign));
1897 return aligned_offset_in_section;
1900 // Add arbitrary data to an output section.
1902 void
1903 Output_section::add_output_section_data(Output_section_data* posd)
1905 Input_section inp(posd);
1906 this->add_output_section_data(&inp);
1908 if (posd->is_data_size_valid())
1910 off_t offset_in_section = this->current_data_size_for_child();
1911 off_t aligned_offset_in_section = align_address(offset_in_section,
1912 posd->addralign());
1913 this->set_current_data_size_for_child(aligned_offset_in_section
1914 + posd->data_size());
1918 // Add arbitrary data to an output section by Input_section.
1920 void
1921 Output_section::add_output_section_data(Input_section* inp)
1923 if (this->input_sections_.empty())
1924 this->first_input_offset_ = this->current_data_size_for_child();
1926 this->input_sections_.push_back(*inp);
1928 uint64_t addralign = inp->addralign();
1929 if (addralign > this->addralign_)
1930 this->addralign_ = addralign;
1932 inp->set_output_section(this);
1935 // Add a merge section to an output section.
1937 void
1938 Output_section::add_output_merge_section(Output_section_data* posd,
1939 bool is_string, uint64_t entsize)
1941 Input_section inp(posd, is_string, entsize);
1942 this->add_output_section_data(&inp);
1945 // Add an input section to a SHF_MERGE section.
1947 bool
1948 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1949 uint64_t flags, uint64_t entsize,
1950 uint64_t addralign)
1952 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1954 // We only merge strings if the alignment is not more than the
1955 // character size. This could be handled, but it's unusual.
1956 if (is_string && addralign > entsize)
1957 return false;
1959 Input_section_list::iterator p;
1960 for (p = this->input_sections_.begin();
1961 p != this->input_sections_.end();
1962 ++p)
1963 if (p->is_merge_section(is_string, entsize, addralign))
1965 p->add_input_section(object, shndx);
1966 return true;
1969 // We handle the actual constant merging in Output_merge_data or
1970 // Output_merge_string_data.
1971 Output_section_data* posd;
1972 if (!is_string)
1973 posd = new Output_merge_data(entsize, addralign);
1974 else
1976 switch (entsize)
1978 case 1:
1979 posd = new Output_merge_string<char>(addralign);
1980 break;
1981 case 2:
1982 posd = new Output_merge_string<uint16_t>(addralign);
1983 break;
1984 case 4:
1985 posd = new Output_merge_string<uint32_t>(addralign);
1986 break;
1987 default:
1988 return false;
1992 this->add_output_merge_section(posd, is_string, entsize);
1993 posd->add_input_section(object, shndx);
1995 return true;
1998 // Update the output section flags based on input section flags.
2000 void
2001 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2003 // If we created the section with SHF_ALLOC clear, we set the
2004 // address. If we are now setting the SHF_ALLOC flag, we need to
2005 // undo that.
2006 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2007 && (flags & elfcpp::SHF_ALLOC) != 0)
2008 this->mark_address_invalid();
2010 this->flags_ |= (flags
2011 & (elfcpp::SHF_WRITE
2012 | elfcpp::SHF_ALLOC
2013 | elfcpp::SHF_EXECINSTR));
2016 // Given an address OFFSET relative to the start of input section
2017 // SHNDX in OBJECT, return whether this address is being included in
2018 // the final link. This should only be called if SHNDX in OBJECT has
2019 // a special mapping.
2021 bool
2022 Output_section::is_input_address_mapped(const Relobj* object,
2023 unsigned int shndx,
2024 off_t offset) const
2026 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2027 p != this->input_sections_.end();
2028 ++p)
2030 section_offset_type output_offset;
2031 if (p->output_offset(object, shndx, offset, &output_offset))
2032 return output_offset != -1;
2035 // By default we assume that the address is mapped. This should
2036 // only be called after we have passed all sections to Layout. At
2037 // that point we should know what we are discarding.
2038 return true;
2041 // Given an address OFFSET relative to the start of input section
2042 // SHNDX in object OBJECT, return the output offset relative to the
2043 // start of the input section in the output section. This should only
2044 // be called if SHNDX in OBJECT has a special mapping.
2046 section_offset_type
2047 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2048 section_offset_type offset) const
2050 // This can only be called meaningfully when layout is complete.
2051 gold_assert(Output_data::is_layout_complete());
2053 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2054 p != this->input_sections_.end();
2055 ++p)
2057 section_offset_type output_offset;
2058 if (p->output_offset(object, shndx, offset, &output_offset))
2059 return output_offset;
2061 gold_unreachable();
2064 // Return the output virtual address of OFFSET relative to the start
2065 // of input section SHNDX in object OBJECT.
2067 uint64_t
2068 Output_section::output_address(const Relobj* object, unsigned int shndx,
2069 off_t offset) const
2071 uint64_t addr = this->address() + this->first_input_offset_;
2072 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2073 p != this->input_sections_.end();
2074 ++p)
2076 addr = align_address(addr, p->addralign());
2077 section_offset_type output_offset;
2078 if (p->output_offset(object, shndx, offset, &output_offset))
2080 if (output_offset == -1)
2081 return -1ULL;
2082 return addr + output_offset;
2084 addr += p->data_size();
2087 // If we get here, it means that we don't know the mapping for this
2088 // input section. This might happen in principle if
2089 // add_input_section were called before add_output_section_data.
2090 // But it should never actually happen.
2092 gold_unreachable();
2095 // Find the output address of the start of the merged section for
2096 // input section SHNDX in object OBJECT.
2098 bool
2099 Output_section::find_starting_output_address(const Relobj* object,
2100 unsigned int shndx,
2101 uint64_t* paddr) const
2103 uint64_t addr = this->address() + this->first_input_offset_;
2104 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2105 p != this->input_sections_.end();
2106 ++p)
2108 addr = align_address(addr, p->addralign());
2110 // It would be nice if we could use the existing output_offset
2111 // method to get the output offset of input offset 0.
2112 // Unfortunately we don't know for sure that input offset 0 is
2113 // mapped at all.
2114 if (p->is_merge_section_for(object, shndx))
2116 *paddr = addr;
2117 return true;
2120 addr += p->data_size();
2123 // We couldn't find a merge output section for this input section.
2124 return false;
2127 // Set the data size of an Output_section. This is where we handle
2128 // setting the addresses of any Output_section_data objects.
2130 void
2131 Output_section::set_final_data_size()
2133 if (this->input_sections_.empty())
2135 this->set_data_size(this->current_data_size_for_child());
2136 return;
2139 if (this->must_sort_attached_input_sections())
2140 this->sort_attached_input_sections();
2142 uint64_t address = this->address();
2143 off_t startoff = this->offset();
2144 off_t off = startoff + this->first_input_offset_;
2145 for (Input_section_list::iterator p = this->input_sections_.begin();
2146 p != this->input_sections_.end();
2147 ++p)
2149 off = align_address(off, p->addralign());
2150 p->set_address_and_file_offset(address + (off - startoff), off,
2151 startoff);
2152 off += p->data_size();
2155 this->set_data_size(off - startoff);
2158 // Reset the address and file offset.
2160 void
2161 Output_section::do_reset_address_and_file_offset()
2163 for (Input_section_list::iterator p = this->input_sections_.begin();
2164 p != this->input_sections_.end();
2165 ++p)
2166 p->reset_address_and_file_offset();
2169 // Set the TLS offset. Called only for SHT_TLS sections.
2171 void
2172 Output_section::do_set_tls_offset(uint64_t tls_base)
2174 this->tls_offset_ = this->address() - tls_base;
2177 // In a few cases we need to sort the input sections attached to an
2178 // output section. This is used to implement the type of constructor
2179 // priority ordering implemented by the GNU linker, in which the
2180 // priority becomes part of the section name and the sections are
2181 // sorted by name. We only do this for an output section if we see an
2182 // attached input section matching ".ctor.*", ".dtor.*",
2183 // ".init_array.*" or ".fini_array.*".
2185 class Output_section::Input_section_sort_entry
2187 public:
2188 Input_section_sort_entry()
2189 : input_section_(), index_(-1U), section_has_name_(false),
2190 section_name_()
2193 Input_section_sort_entry(const Input_section& input_section,
2194 unsigned int index)
2195 : input_section_(input_section), index_(index),
2196 section_has_name_(input_section.is_input_section())
2198 if (this->section_has_name_)
2200 // This is only called single-threaded from Layout::finalize,
2201 // so it is OK to lock. Unfortunately we have no way to pass
2202 // in a Task token.
2203 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2204 Object* obj = input_section.relobj();
2205 Task_lock_obj<Object> tl(dummy_task, obj);
2207 // This is a slow operation, which should be cached in
2208 // Layout::layout if this becomes a speed problem.
2209 this->section_name_ = obj->section_name(input_section.shndx());
2213 // Return the Input_section.
2214 const Input_section&
2215 input_section() const
2217 gold_assert(this->index_ != -1U);
2218 return this->input_section_;
2221 // The index of this entry in the original list. This is used to
2222 // make the sort stable.
2223 unsigned int
2224 index() const
2226 gold_assert(this->index_ != -1U);
2227 return this->index_;
2230 // Whether there is a section name.
2231 bool
2232 section_has_name() const
2233 { return this->section_has_name_; }
2235 // The section name.
2236 const std::string&
2237 section_name() const
2239 gold_assert(this->section_has_name_);
2240 return this->section_name_;
2243 // Return true if the section name has a priority. This is assumed
2244 // to be true if it has a dot after the initial dot.
2245 bool
2246 has_priority() const
2248 gold_assert(this->section_has_name_);
2249 return this->section_name_.find('.', 1);
2252 // Return true if this an input file whose base name matches
2253 // FILE_NAME. The base name must have an extension of ".o", and
2254 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2255 // This is to match crtbegin.o as well as crtbeginS.o without
2256 // getting confused by other possibilities. Overall matching the
2257 // file name this way is a dreadful hack, but the GNU linker does it
2258 // in order to better support gcc, and we need to be compatible.
2259 bool
2260 match_file_name(const char* match_file_name) const
2262 const std::string& file_name(this->input_section_.relobj()->name());
2263 const char* base_name = lbasename(file_name.c_str());
2264 size_t match_len = strlen(match_file_name);
2265 if (strncmp(base_name, match_file_name, match_len) != 0)
2266 return false;
2267 size_t base_len = strlen(base_name);
2268 if (base_len != match_len + 2 && base_len != match_len + 3)
2269 return false;
2270 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2273 private:
2274 // The Input_section we are sorting.
2275 Input_section input_section_;
2276 // The index of this Input_section in the original list.
2277 unsigned int index_;
2278 // Whether this Input_section has a section name--it won't if this
2279 // is some random Output_section_data.
2280 bool section_has_name_;
2281 // The section name if there is one.
2282 std::string section_name_;
2285 // Return true if S1 should come before S2 in the output section.
2287 bool
2288 Output_section::Input_section_sort_compare::operator()(
2289 const Output_section::Input_section_sort_entry& s1,
2290 const Output_section::Input_section_sort_entry& s2) const
2292 // crtbegin.o must come first.
2293 bool s1_begin = s1.match_file_name("crtbegin");
2294 bool s2_begin = s2.match_file_name("crtbegin");
2295 if (s1_begin || s2_begin)
2297 if (!s1_begin)
2298 return false;
2299 if (!s2_begin)
2300 return true;
2301 return s1.index() < s2.index();
2304 // crtend.o must come last.
2305 bool s1_end = s1.match_file_name("crtend");
2306 bool s2_end = s2.match_file_name("crtend");
2307 if (s1_end || s2_end)
2309 if (!s1_end)
2310 return true;
2311 if (!s2_end)
2312 return false;
2313 return s1.index() < s2.index();
2316 // We sort all the sections with no names to the end.
2317 if (!s1.section_has_name() || !s2.section_has_name())
2319 if (s1.section_has_name())
2320 return true;
2321 if (s2.section_has_name())
2322 return false;
2323 return s1.index() < s2.index();
2326 // A section with a priority follows a section without a priority.
2327 // The GNU linker does this for all but .init_array sections; until
2328 // further notice we'll assume that that is an mistake.
2329 bool s1_has_priority = s1.has_priority();
2330 bool s2_has_priority = s2.has_priority();
2331 if (s1_has_priority && !s2_has_priority)
2332 return false;
2333 if (!s1_has_priority && s2_has_priority)
2334 return true;
2336 // Otherwise we sort by name.
2337 int compare = s1.section_name().compare(s2.section_name());
2338 if (compare != 0)
2339 return compare < 0;
2341 // Otherwise we keep the input order.
2342 return s1.index() < s2.index();
2345 // Sort the input sections attached to an output section.
2347 void
2348 Output_section::sort_attached_input_sections()
2350 if (this->attached_input_sections_are_sorted_)
2351 return;
2353 // The only thing we know about an input section is the object and
2354 // the section index. We need the section name. Recomputing this
2355 // is slow but this is an unusual case. If this becomes a speed
2356 // problem we can cache the names as required in Layout::layout.
2358 // We start by building a larger vector holding a copy of each
2359 // Input_section, plus its current index in the list and its name.
2360 std::vector<Input_section_sort_entry> sort_list;
2362 unsigned int i = 0;
2363 for (Input_section_list::iterator p = this->input_sections_.begin();
2364 p != this->input_sections_.end();
2365 ++p, ++i)
2366 sort_list.push_back(Input_section_sort_entry(*p, i));
2368 // Sort the input sections.
2369 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2371 // Copy the sorted input sections back to our list.
2372 this->input_sections_.clear();
2373 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2374 p != sort_list.end();
2375 ++p)
2376 this->input_sections_.push_back(p->input_section());
2378 // Remember that we sorted the input sections, since we might get
2379 // called again.
2380 this->attached_input_sections_are_sorted_ = true;
2383 // Write the section header to *OSHDR.
2385 template<int size, bool big_endian>
2386 void
2387 Output_section::write_header(const Layout* layout,
2388 const Stringpool* secnamepool,
2389 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2391 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2392 oshdr->put_sh_type(this->type_);
2394 elfcpp::Elf_Xword flags = this->flags_;
2395 if (this->info_section_ != NULL && this->info_uses_section_index_)
2396 flags |= elfcpp::SHF_INFO_LINK;
2397 oshdr->put_sh_flags(flags);
2399 oshdr->put_sh_addr(this->address());
2400 oshdr->put_sh_offset(this->offset());
2401 oshdr->put_sh_size(this->data_size());
2402 if (this->link_section_ != NULL)
2403 oshdr->put_sh_link(this->link_section_->out_shndx());
2404 else if (this->should_link_to_symtab_)
2405 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2406 else if (this->should_link_to_dynsym_)
2407 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2408 else
2409 oshdr->put_sh_link(this->link_);
2411 elfcpp::Elf_Word info;
2412 if (this->info_section_ != NULL)
2414 if (this->info_uses_section_index_)
2415 info = this->info_section_->out_shndx();
2416 else
2417 info = this->info_section_->symtab_index();
2419 else if (this->info_symndx_ != NULL)
2420 info = this->info_symndx_->symtab_index();
2421 else
2422 info = this->info_;
2423 oshdr->put_sh_info(info);
2425 oshdr->put_sh_addralign(this->addralign_);
2426 oshdr->put_sh_entsize(this->entsize_);
2429 // Write out the data. For input sections the data is written out by
2430 // Object::relocate, but we have to handle Output_section_data objects
2431 // here.
2433 void
2434 Output_section::do_write(Output_file* of)
2436 gold_assert(!this->requires_postprocessing());
2438 off_t output_section_file_offset = this->offset();
2439 for (Fill_list::iterator p = this->fills_.begin();
2440 p != this->fills_.end();
2441 ++p)
2443 std::string fill_data(parameters->target().code_fill(p->length()));
2444 of->write(output_section_file_offset + p->section_offset(),
2445 fill_data.data(), fill_data.size());
2448 for (Input_section_list::iterator p = this->input_sections_.begin();
2449 p != this->input_sections_.end();
2450 ++p)
2451 p->write(of);
2454 // If a section requires postprocessing, create the buffer to use.
2456 void
2457 Output_section::create_postprocessing_buffer()
2459 gold_assert(this->requires_postprocessing());
2461 if (this->postprocessing_buffer_ != NULL)
2462 return;
2464 if (!this->input_sections_.empty())
2466 off_t off = this->first_input_offset_;
2467 for (Input_section_list::iterator p = this->input_sections_.begin();
2468 p != this->input_sections_.end();
2469 ++p)
2471 off = align_address(off, p->addralign());
2472 p->finalize_data_size();
2473 off += p->data_size();
2475 this->set_current_data_size_for_child(off);
2478 off_t buffer_size = this->current_data_size_for_child();
2479 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2482 // Write all the data of an Output_section into the postprocessing
2483 // buffer. This is used for sections which require postprocessing,
2484 // such as compression. Input sections are handled by
2485 // Object::Relocate.
2487 void
2488 Output_section::write_to_postprocessing_buffer()
2490 gold_assert(this->requires_postprocessing());
2492 unsigned char* buffer = this->postprocessing_buffer();
2493 for (Fill_list::iterator p = this->fills_.begin();
2494 p != this->fills_.end();
2495 ++p)
2497 std::string fill_data(parameters->target().code_fill(p->length()));
2498 memcpy(buffer + p->section_offset(), fill_data.data(),
2499 fill_data.size());
2502 off_t off = this->first_input_offset_;
2503 for (Input_section_list::iterator p = this->input_sections_.begin();
2504 p != this->input_sections_.end();
2505 ++p)
2507 off = align_address(off, p->addralign());
2508 p->write_to_buffer(buffer + off);
2509 off += p->data_size();
2513 // Get the input sections for linker script processing. We leave
2514 // behind the Output_section_data entries. Note that this may be
2515 // slightly incorrect for merge sections. We will leave them behind,
2516 // but it is possible that the script says that they should follow
2517 // some other input sections, as in:
2518 // .rodata { *(.rodata) *(.rodata.cst*) }
2519 // For that matter, we don't handle this correctly:
2520 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2521 // With luck this will never matter.
2523 uint64_t
2524 Output_section::get_input_sections(
2525 uint64_t address,
2526 const std::string& fill,
2527 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2529 uint64_t orig_address = address;
2531 address = align_address(address, this->addralign());
2533 Input_section_list remaining;
2534 for (Input_section_list::iterator p = this->input_sections_.begin();
2535 p != this->input_sections_.end();
2536 ++p)
2538 if (p->is_input_section())
2539 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2540 else
2542 uint64_t aligned_address = align_address(address, p->addralign());
2543 if (aligned_address != address && !fill.empty())
2545 section_size_type length =
2546 convert_to_section_size_type(aligned_address - address);
2547 std::string this_fill;
2548 this_fill.reserve(length);
2549 while (this_fill.length() + fill.length() <= length)
2550 this_fill += fill;
2551 if (this_fill.length() < length)
2552 this_fill.append(fill, 0, length - this_fill.length());
2554 Output_section_data* posd = new Output_data_const(this_fill, 0);
2555 remaining.push_back(Input_section(posd));
2557 address = aligned_address;
2559 remaining.push_back(*p);
2561 p->finalize_data_size();
2562 address += p->data_size();
2566 this->input_sections_.swap(remaining);
2567 this->first_input_offset_ = 0;
2569 uint64_t data_size = address - orig_address;
2570 this->set_current_data_size_for_child(data_size);
2571 return data_size;
2574 // Add an input section from a script.
2576 void
2577 Output_section::add_input_section_for_script(Relobj* object,
2578 unsigned int shndx,
2579 off_t data_size,
2580 uint64_t addralign)
2582 if (addralign > this->addralign_)
2583 this->addralign_ = addralign;
2585 off_t offset_in_section = this->current_data_size_for_child();
2586 off_t aligned_offset_in_section = align_address(offset_in_section,
2587 addralign);
2589 this->set_current_data_size_for_child(aligned_offset_in_section
2590 + data_size);
2592 this->input_sections_.push_back(Input_section(object, shndx,
2593 data_size, addralign));
2596 // Print to the map file.
2598 void
2599 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
2601 mapfile->print_output_section(this);
2603 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2604 p != this->input_sections_.end();
2605 ++p)
2606 p->print_to_mapfile(mapfile);
2609 // Print stats for merge sections to stderr.
2611 void
2612 Output_section::print_merge_stats()
2614 Input_section_list::iterator p;
2615 for (p = this->input_sections_.begin();
2616 p != this->input_sections_.end();
2617 ++p)
2618 p->print_merge_stats(this->name_);
2621 // Output segment methods.
2623 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2624 : output_data_(),
2625 output_bss_(),
2626 vaddr_(0),
2627 paddr_(0),
2628 memsz_(0),
2629 max_align_(0),
2630 min_p_align_(0),
2631 offset_(0),
2632 filesz_(0),
2633 type_(type),
2634 flags_(flags),
2635 is_max_align_known_(false),
2636 are_addresses_set_(false),
2637 is_large_data_segment_(false)
2641 // Add an Output_section to an Output_segment.
2643 void
2644 Output_segment::add_output_section(Output_section* os,
2645 elfcpp::Elf_Word seg_flags)
2647 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
2648 gold_assert(!this->is_max_align_known_);
2649 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
2651 // Update the segment flags.
2652 this->flags_ |= seg_flags;
2654 Output_segment::Output_data_list* pdl;
2655 if (os->type() == elfcpp::SHT_NOBITS)
2656 pdl = &this->output_bss_;
2657 else
2658 pdl = &this->output_data_;
2660 // So that PT_NOTE segments will work correctly, we need to ensure
2661 // that all SHT_NOTE sections are adjacent. This will normally
2662 // happen automatically, because all the SHT_NOTE input sections
2663 // will wind up in the same output section. However, it is possible
2664 // for multiple SHT_NOTE input sections to have different section
2665 // flags, and thus be in different output sections, but for the
2666 // different section flags to map into the same segment flags and
2667 // thus the same output segment.
2669 // Note that while there may be many input sections in an output
2670 // section, there are normally only a few output sections in an
2671 // output segment. This loop is expected to be fast.
2673 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
2675 Output_segment::Output_data_list::iterator p = pdl->end();
2678 --p;
2679 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2681 ++p;
2682 pdl->insert(p, os);
2683 return;
2686 while (p != pdl->begin());
2689 // Similarly, so that PT_TLS segments will work, we need to group
2690 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2691 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2692 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2693 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2694 // and the PT_TLS segment -- we do this grouping only for the
2695 // PT_LOAD segment.
2696 if (this->type_ != elfcpp::PT_TLS
2697 && (os->flags() & elfcpp::SHF_TLS) != 0)
2699 pdl = &this->output_data_;
2700 bool nobits = os->type() == elfcpp::SHT_NOBITS;
2701 bool sawtls = false;
2702 Output_segment::Output_data_list::iterator p = pdl->end();
2705 --p;
2706 bool insert;
2707 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2709 sawtls = true;
2710 // Put a NOBITS section after the first TLS section.
2711 // Put a PROGBITS section after the first TLS/PROGBITS
2712 // section.
2713 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2715 else
2717 // If we've gone past the TLS sections, but we've seen a
2718 // TLS section, then we need to insert this section now.
2719 insert = sawtls;
2722 if (insert)
2724 ++p;
2725 pdl->insert(p, os);
2726 return;
2729 while (p != pdl->begin());
2731 // There are no TLS sections yet; put this one at the requested
2732 // location in the section list.
2735 // For the PT_GNU_RELRO segment, we need to group relro sections,
2736 // and we need to put them before any non-relro sections. Also,
2737 // relro local sections go before relro non-local sections.
2738 if (parameters->options().relro() && os->is_relro())
2740 gold_assert(pdl == &this->output_data_);
2741 Output_segment::Output_data_list::iterator p;
2742 for (p = pdl->begin(); p != pdl->end(); ++p)
2744 if (!(*p)->is_section())
2745 break;
2747 Output_section* pos = (*p)->output_section();
2748 if (!pos->is_relro()
2749 || (os->is_relro_local() && !pos->is_relro_local()))
2750 break;
2753 pdl->insert(p, os);
2754 return;
2757 // Small data sections go at the end of the list of data sections.
2758 // If OS is not small, and there are small sections, we have to
2759 // insert it before the first small section.
2760 if (os->type() != elfcpp::SHT_NOBITS
2761 && !os->is_small_section()
2762 && !pdl->empty()
2763 && pdl->back()->is_section()
2764 && pdl->back()->output_section()->is_small_section())
2766 for (Output_segment::Output_data_list::iterator p = pdl->begin();
2767 p != pdl->end();
2768 ++p)
2770 if ((*p)->is_section()
2771 && (*p)->output_section()->is_small_section())
2773 pdl->insert(p, os);
2774 return;
2777 gold_unreachable();
2780 // A small BSS section goes at the start of the BSS sections, after
2781 // other small BSS sections.
2782 if (os->type() == elfcpp::SHT_NOBITS && os->is_small_section())
2784 for (Output_segment::Output_data_list::iterator p = pdl->begin();
2785 p != pdl->end();
2786 ++p)
2788 if (!(*p)->is_section()
2789 || !(*p)->output_section()->is_small_section())
2791 pdl->insert(p, os);
2792 return;
2797 // A large BSS section goes at the end of the BSS sections, which
2798 // means that one that is not large must come before the first large
2799 // one.
2800 if (os->type() == elfcpp::SHT_NOBITS
2801 && !os->is_large_section()
2802 && !pdl->empty()
2803 && pdl->back()->is_section()
2804 && pdl->back()->output_section()->is_large_section())
2806 for (Output_segment::Output_data_list::iterator p = pdl->begin();
2807 p != pdl->end();
2808 ++p)
2810 if ((*p)->is_section()
2811 && (*p)->output_section()->is_large_section())
2813 pdl->insert(p, os);
2814 return;
2817 gold_unreachable();
2820 pdl->push_back(os);
2823 // Remove an Output_section from this segment. It is an error if it
2824 // is not present.
2826 void
2827 Output_segment::remove_output_section(Output_section* os)
2829 // We only need this for SHT_PROGBITS.
2830 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2831 for (Output_data_list::iterator p = this->output_data_.begin();
2832 p != this->output_data_.end();
2833 ++p)
2835 if (*p == os)
2837 this->output_data_.erase(p);
2838 return;
2841 gold_unreachable();
2844 // Add an Output_data (which is not an Output_section) to the start of
2845 // a segment.
2847 void
2848 Output_segment::add_initial_output_data(Output_data* od)
2850 gold_assert(!this->is_max_align_known_);
2851 this->output_data_.push_front(od);
2854 // Return whether the first data section is a relro section.
2856 bool
2857 Output_segment::is_first_section_relro() const
2859 return (!this->output_data_.empty()
2860 && this->output_data_.front()->is_section()
2861 && this->output_data_.front()->output_section()->is_relro());
2864 // Return the maximum alignment of the Output_data in Output_segment.
2866 uint64_t
2867 Output_segment::maximum_alignment()
2869 if (!this->is_max_align_known_)
2871 uint64_t addralign;
2873 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2874 if (addralign > this->max_align_)
2875 this->max_align_ = addralign;
2877 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2878 if (addralign > this->max_align_)
2879 this->max_align_ = addralign;
2881 // If -z relro is in effect, and the first section in this
2882 // segment is a relro section, then the segment must be aligned
2883 // to at least the common page size. This ensures that the
2884 // PT_GNU_RELRO segment will start at a page boundary.
2885 if (this->type_ == elfcpp::PT_LOAD
2886 && parameters->options().relro()
2887 && this->is_first_section_relro())
2889 addralign = parameters->target().common_pagesize();
2890 if (addralign > this->max_align_)
2891 this->max_align_ = addralign;
2894 this->is_max_align_known_ = true;
2897 return this->max_align_;
2900 // Return the maximum alignment of a list of Output_data.
2902 uint64_t
2903 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
2905 uint64_t ret = 0;
2906 for (Output_data_list::const_iterator p = pdl->begin();
2907 p != pdl->end();
2908 ++p)
2910 uint64_t addralign = (*p)->addralign();
2911 if (addralign > ret)
2912 ret = addralign;
2914 return ret;
2917 // Return the number of dynamic relocs applied to this segment.
2919 unsigned int
2920 Output_segment::dynamic_reloc_count() const
2922 return (this->dynamic_reloc_count_list(&this->output_data_)
2923 + this->dynamic_reloc_count_list(&this->output_bss_));
2926 // Return the number of dynamic relocs applied to an Output_data_list.
2928 unsigned int
2929 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2931 unsigned int count = 0;
2932 for (Output_data_list::const_iterator p = pdl->begin();
2933 p != pdl->end();
2934 ++p)
2935 count += (*p)->dynamic_reloc_count();
2936 return count;
2939 // Set the section addresses for an Output_segment. If RESET is true,
2940 // reset the addresses first. ADDR is the address and *POFF is the
2941 // file offset. Set the section indexes starting with *PSHNDX.
2942 // Return the address of the immediately following segment. Update
2943 // *POFF and *PSHNDX.
2945 uint64_t
2946 Output_segment::set_section_addresses(const Layout* layout, bool reset,
2947 uint64_t addr, off_t* poff,
2948 unsigned int* pshndx)
2950 gold_assert(this->type_ == elfcpp::PT_LOAD);
2952 if (!reset && this->are_addresses_set_)
2954 gold_assert(this->paddr_ == addr);
2955 addr = this->vaddr_;
2957 else
2959 this->vaddr_ = addr;
2960 this->paddr_ = addr;
2961 this->are_addresses_set_ = true;
2964 bool in_tls = false;
2966 bool in_relro = (parameters->options().relro()
2967 && this->is_first_section_relro());
2969 off_t orig_off = *poff;
2970 this->offset_ = orig_off;
2972 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
2973 addr, poff, pshndx, &in_tls,
2974 &in_relro);
2975 this->filesz_ = *poff - orig_off;
2977 off_t off = *poff;
2979 uint64_t ret = this->set_section_list_addresses(layout, reset,
2980 &this->output_bss_,
2981 addr, poff, pshndx,
2982 &in_tls, &in_relro);
2984 // If the last section was a TLS section, align upward to the
2985 // alignment of the TLS segment, so that the overall size of the TLS
2986 // segment is aligned.
2987 if (in_tls)
2989 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2990 *poff = align_address(*poff, segment_align);
2993 // If all the sections were relro sections, align upward to the
2994 // common page size.
2995 if (in_relro)
2997 uint64_t page_align = parameters->target().common_pagesize();
2998 *poff = align_address(*poff, page_align);
3001 this->memsz_ = *poff - orig_off;
3003 // Ignore the file offset adjustments made by the BSS Output_data
3004 // objects.
3005 *poff = off;
3007 return ret;
3010 // Set the addresses and file offsets in a list of Output_data
3011 // structures.
3013 uint64_t
3014 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
3015 Output_data_list* pdl,
3016 uint64_t addr, off_t* poff,
3017 unsigned int* pshndx,
3018 bool* in_tls, bool* in_relro)
3020 off_t startoff = *poff;
3022 off_t off = startoff;
3023 for (Output_data_list::iterator p = pdl->begin();
3024 p != pdl->end();
3025 ++p)
3027 if (reset)
3028 (*p)->reset_address_and_file_offset();
3030 // When using a linker script the section will most likely
3031 // already have an address.
3032 if (!(*p)->is_address_valid())
3034 uint64_t align = (*p)->addralign();
3036 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3038 // Give the first TLS section the alignment of the
3039 // entire TLS segment. Otherwise the TLS segment as a
3040 // whole may be misaligned.
3041 if (!*in_tls)
3043 Output_segment* tls_segment = layout->tls_segment();
3044 gold_assert(tls_segment != NULL);
3045 uint64_t segment_align = tls_segment->maximum_alignment();
3046 gold_assert(segment_align >= align);
3047 align = segment_align;
3049 *in_tls = true;
3052 else
3054 // If this is the first section after the TLS segment,
3055 // align it to at least the alignment of the TLS
3056 // segment, so that the size of the overall TLS segment
3057 // is aligned.
3058 if (*in_tls)
3060 uint64_t segment_align =
3061 layout->tls_segment()->maximum_alignment();
3062 if (segment_align > align)
3063 align = segment_align;
3065 *in_tls = false;
3069 // If this is a non-relro section after a relro section,
3070 // align it to a common page boundary so that the dynamic
3071 // linker has a page to mark as read-only.
3072 if (*in_relro
3073 && (!(*p)->is_section()
3074 || !(*p)->output_section()->is_relro()))
3076 uint64_t page_align = parameters->target().common_pagesize();
3077 if (page_align > align)
3078 align = page_align;
3079 *in_relro = false;
3082 off = align_address(off, align);
3083 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
3085 else
3087 // The script may have inserted a skip forward, but it
3088 // better not have moved backward.
3089 gold_assert((*p)->address() >= addr + (off - startoff));
3090 off += (*p)->address() - (addr + (off - startoff));
3091 (*p)->set_file_offset(off);
3092 (*p)->finalize_data_size();
3095 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
3096 // section. Such a section does not affect the size of a
3097 // PT_LOAD segment.
3098 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
3099 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
3100 off += (*p)->data_size();
3102 if ((*p)->is_section())
3104 (*p)->set_out_shndx(*pshndx);
3105 ++*pshndx;
3109 *poff = off;
3110 return addr + (off - startoff);
3113 // For a non-PT_LOAD segment, set the offset from the sections, if
3114 // any.
3116 void
3117 Output_segment::set_offset()
3119 gold_assert(this->type_ != elfcpp::PT_LOAD);
3121 gold_assert(!this->are_addresses_set_);
3123 if (this->output_data_.empty() && this->output_bss_.empty())
3125 this->vaddr_ = 0;
3126 this->paddr_ = 0;
3127 this->are_addresses_set_ = true;
3128 this->memsz_ = 0;
3129 this->min_p_align_ = 0;
3130 this->offset_ = 0;
3131 this->filesz_ = 0;
3132 return;
3135 const Output_data* first;
3136 if (this->output_data_.empty())
3137 first = this->output_bss_.front();
3138 else
3139 first = this->output_data_.front();
3140 this->vaddr_ = first->address();
3141 this->paddr_ = (first->has_load_address()
3142 ? first->load_address()
3143 : this->vaddr_);
3144 this->are_addresses_set_ = true;
3145 this->offset_ = first->offset();
3147 if (this->output_data_.empty())
3148 this->filesz_ = 0;
3149 else
3151 const Output_data* last_data = this->output_data_.back();
3152 this->filesz_ = (last_data->address()
3153 + last_data->data_size()
3154 - this->vaddr_);
3157 const Output_data* last;
3158 if (this->output_bss_.empty())
3159 last = this->output_data_.back();
3160 else
3161 last = this->output_bss_.back();
3162 this->memsz_ = (last->address()
3163 + last->data_size()
3164 - this->vaddr_);
3166 // If this is a TLS segment, align the memory size. The code in
3167 // set_section_list ensures that the section after the TLS segment
3168 // is aligned to give us room.
3169 if (this->type_ == elfcpp::PT_TLS)
3171 uint64_t segment_align = this->maximum_alignment();
3172 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3173 this->memsz_ = align_address(this->memsz_, segment_align);
3176 // If this is a RELRO segment, align the memory size. The code in
3177 // set_section_list ensures that the section after the RELRO segment
3178 // is aligned to give us room.
3179 if (this->type_ == elfcpp::PT_GNU_RELRO)
3181 uint64_t page_align = parameters->target().common_pagesize();
3182 gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3183 this->memsz_ = align_address(this->memsz_, page_align);
3187 // Set the TLS offsets of the sections in the PT_TLS segment.
3189 void
3190 Output_segment::set_tls_offsets()
3192 gold_assert(this->type_ == elfcpp::PT_TLS);
3194 for (Output_data_list::iterator p = this->output_data_.begin();
3195 p != this->output_data_.end();
3196 ++p)
3197 (*p)->set_tls_offset(this->vaddr_);
3199 for (Output_data_list::iterator p = this->output_bss_.begin();
3200 p != this->output_bss_.end();
3201 ++p)
3202 (*p)->set_tls_offset(this->vaddr_);
3205 // Return the address of the first section.
3207 uint64_t
3208 Output_segment::first_section_load_address() const
3210 for (Output_data_list::const_iterator p = this->output_data_.begin();
3211 p != this->output_data_.end();
3212 ++p)
3213 if ((*p)->is_section())
3214 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3216 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3217 p != this->output_bss_.end();
3218 ++p)
3219 if ((*p)->is_section())
3220 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3222 gold_unreachable();
3225 // Return the number of Output_sections in an Output_segment.
3227 unsigned int
3228 Output_segment::output_section_count() const
3230 return (this->output_section_count_list(&this->output_data_)
3231 + this->output_section_count_list(&this->output_bss_));
3234 // Return the number of Output_sections in an Output_data_list.
3236 unsigned int
3237 Output_segment::output_section_count_list(const Output_data_list* pdl) const
3239 unsigned int count = 0;
3240 for (Output_data_list::const_iterator p = pdl->begin();
3241 p != pdl->end();
3242 ++p)
3244 if ((*p)->is_section())
3245 ++count;
3247 return count;
3250 // Return the section attached to the list segment with the lowest
3251 // load address. This is used when handling a PHDRS clause in a
3252 // linker script.
3254 Output_section*
3255 Output_segment::section_with_lowest_load_address() const
3257 Output_section* found = NULL;
3258 uint64_t found_lma = 0;
3259 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3261 Output_section* found_data = found;
3262 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3263 if (found != found_data && found_data != NULL)
3265 gold_error(_("nobits section %s may not precede progbits section %s "
3266 "in same segment"),
3267 found->name(), found_data->name());
3268 return NULL;
3271 return found;
3274 // Look through a list for a section with a lower load address.
3276 void
3277 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3278 Output_section** found,
3279 uint64_t* found_lma) const
3281 for (Output_data_list::const_iterator p = pdl->begin();
3282 p != pdl->end();
3283 ++p)
3285 if (!(*p)->is_section())
3286 continue;
3287 Output_section* os = static_cast<Output_section*>(*p);
3288 uint64_t lma = (os->has_load_address()
3289 ? os->load_address()
3290 : os->address());
3291 if (*found == NULL || lma < *found_lma)
3293 *found = os;
3294 *found_lma = lma;
3299 // Write the segment data into *OPHDR.
3301 template<int size, bool big_endian>
3302 void
3303 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
3305 ophdr->put_p_type(this->type_);
3306 ophdr->put_p_offset(this->offset_);
3307 ophdr->put_p_vaddr(this->vaddr_);
3308 ophdr->put_p_paddr(this->paddr_);
3309 ophdr->put_p_filesz(this->filesz_);
3310 ophdr->put_p_memsz(this->memsz_);
3311 ophdr->put_p_flags(this->flags_);
3312 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
3315 // Write the section headers into V.
3317 template<int size, bool big_endian>
3318 unsigned char*
3319 Output_segment::write_section_headers(const Layout* layout,
3320 const Stringpool* secnamepool,
3321 unsigned char* v,
3322 unsigned int *pshndx) const
3324 // Every section that is attached to a segment must be attached to a
3325 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3326 // segments.
3327 if (this->type_ != elfcpp::PT_LOAD)
3328 return v;
3330 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3331 &this->output_data_,
3332 v, pshndx);
3333 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3334 &this->output_bss_,
3335 v, pshndx);
3336 return v;
3339 template<int size, bool big_endian>
3340 unsigned char*
3341 Output_segment::write_section_headers_list(const Layout* layout,
3342 const Stringpool* secnamepool,
3343 const Output_data_list* pdl,
3344 unsigned char* v,
3345 unsigned int* pshndx) const
3347 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3348 for (Output_data_list::const_iterator p = pdl->begin();
3349 p != pdl->end();
3350 ++p)
3352 if ((*p)->is_section())
3354 const Output_section* ps = static_cast<const Output_section*>(*p);
3355 gold_assert(*pshndx == ps->out_shndx());
3356 elfcpp::Shdr_write<size, big_endian> oshdr(v);
3357 ps->write_header(layout, secnamepool, &oshdr);
3358 v += shdr_size;
3359 ++*pshndx;
3362 return v;
3365 // Print the output sections to the map file.
3367 void
3368 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3370 if (this->type() != elfcpp::PT_LOAD)
3371 return;
3372 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3373 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3376 // Print an output section list to the map file.
3378 void
3379 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3380 const Output_data_list* pdl) const
3382 for (Output_data_list::const_iterator p = pdl->begin();
3383 p != pdl->end();
3384 ++p)
3385 (*p)->print_to_mapfile(mapfile);
3388 // Output_file methods.
3390 Output_file::Output_file(const char* name)
3391 : name_(name),
3392 o_(-1),
3393 file_size_(0),
3394 base_(NULL),
3395 map_is_anonymous_(false),
3396 is_temporary_(false)
3400 // Open the output file.
3402 void
3403 Output_file::open(off_t file_size)
3405 this->file_size_ = file_size;
3407 // Unlink the file first; otherwise the open() may fail if the file
3408 // is busy (e.g. it's an executable that's currently being executed).
3410 // However, the linker may be part of a system where a zero-length
3411 // file is created for it to write to, with tight permissions (gcc
3412 // 2.95 did something like this). Unlinking the file would work
3413 // around those permission controls, so we only unlink if the file
3414 // has a non-zero size. We also unlink only regular files to avoid
3415 // trouble with directories/etc.
3417 // If we fail, continue; this command is merely a best-effort attempt
3418 // to improve the odds for open().
3420 // We let the name "-" mean "stdout"
3421 if (!this->is_temporary_)
3423 if (strcmp(this->name_, "-") == 0)
3424 this->o_ = STDOUT_FILENO;
3425 else
3427 struct stat s;
3428 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
3429 unlink_if_ordinary(this->name_);
3431 int mode = parameters->options().relocatable() ? 0666 : 0777;
3432 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
3433 mode);
3434 if (o < 0)
3435 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3436 this->o_ = o;
3440 this->map();
3443 // Resize the output file.
3445 void
3446 Output_file::resize(off_t file_size)
3448 // If the mmap is mapping an anonymous memory buffer, this is easy:
3449 // just mremap to the new size. If it's mapping to a file, we want
3450 // to unmap to flush to the file, then remap after growing the file.
3451 if (this->map_is_anonymous_)
3453 void* base = ::mremap(this->base_, this->file_size_, file_size,
3454 MREMAP_MAYMOVE);
3455 if (base == MAP_FAILED)
3456 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3457 this->base_ = static_cast<unsigned char*>(base);
3458 this->file_size_ = file_size;
3460 else
3462 this->unmap();
3463 this->file_size_ = file_size;
3464 this->map();
3468 // Map a block of memory which will later be written to the file.
3469 // Return a pointer to the memory.
3471 void*
3472 Output_file::map_anonymous()
3474 this->map_is_anonymous_ = true;
3475 return ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3476 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3479 // Map the file into memory.
3481 void
3482 Output_file::map()
3484 const int o = this->o_;
3486 // If the output file is not a regular file, don't try to mmap it;
3487 // instead, we'll mmap a block of memory (an anonymous buffer), and
3488 // then later write the buffer to the file.
3489 void* base;
3490 struct stat statbuf;
3491 if (o == STDOUT_FILENO || o == STDERR_FILENO
3492 || ::fstat(o, &statbuf) != 0
3493 || !S_ISREG(statbuf.st_mode)
3494 || this->is_temporary_)
3495 base = this->map_anonymous();
3496 else
3498 // Ensure that we have disk space available for the file. If we
3499 // don't do this, it is possible that we will call munmap,
3500 // close, and exit with dirty buffers still in the cache with no
3501 // assigned disk blocks. If the disk is out of space at that
3502 // point, the output file will wind up incomplete, but we will
3503 // have already exited. The alternative to fallocate would be
3504 // to use fdatasync, but that would be a more significant
3505 // performance hit.
3506 if (::posix_fallocate(o, 0, this->file_size_) < 0)
3507 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
3509 // Map the file into memory.
3510 this->map_is_anonymous_ = false;
3511 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3512 MAP_SHARED, o, 0);
3514 // The mmap call might fail because of file system issues: the
3515 // file system might not support mmap at all, or it might not
3516 // support mmap with PROT_WRITE. I'm not sure which errno
3517 // values we will see in all cases, so if the mmap fails for any
3518 // reason try for an anonymous map.
3519 if (base == MAP_FAILED)
3520 base = this->map_anonymous();
3522 if (base == MAP_FAILED)
3523 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
3524 this->name_, static_cast<unsigned long>(this->file_size_),
3525 strerror(errno));
3526 this->base_ = static_cast<unsigned char*>(base);
3529 // Unmap the file from memory.
3531 void
3532 Output_file::unmap()
3534 if (::munmap(this->base_, this->file_size_) < 0)
3535 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
3536 this->base_ = NULL;
3539 // Close the output file.
3541 void
3542 Output_file::close()
3544 // If the map isn't file-backed, we need to write it now.
3545 if (this->map_is_anonymous_ && !this->is_temporary_)
3547 size_t bytes_to_write = this->file_size_;
3548 size_t offset = 0;
3549 while (bytes_to_write > 0)
3551 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
3552 bytes_to_write);
3553 if (bytes_written == 0)
3554 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
3555 else if (bytes_written < 0)
3556 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
3557 else
3559 bytes_to_write -= bytes_written;
3560 offset += bytes_written;
3564 this->unmap();
3566 // We don't close stdout or stderr
3567 if (this->o_ != STDOUT_FILENO
3568 && this->o_ != STDERR_FILENO
3569 && !this->is_temporary_)
3570 if (::close(this->o_) < 0)
3571 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
3572 this->o_ = -1;
3575 // Instantiate the templates we need. We could use the configure
3576 // script to restrict this to only the ones for implemented targets.
3578 #ifdef HAVE_TARGET_32_LITTLE
3579 template
3580 off_t
3581 Output_section::add_input_section<32, false>(
3582 Sized_relobj<32, false>* object,
3583 unsigned int shndx,
3584 const char* secname,
3585 const elfcpp::Shdr<32, false>& shdr,
3586 unsigned int reloc_shndx,
3587 bool have_sections_script);
3588 #endif
3590 #ifdef HAVE_TARGET_32_BIG
3591 template
3592 off_t
3593 Output_section::add_input_section<32, true>(
3594 Sized_relobj<32, true>* object,
3595 unsigned int shndx,
3596 const char* secname,
3597 const elfcpp::Shdr<32, true>& shdr,
3598 unsigned int reloc_shndx,
3599 bool have_sections_script);
3600 #endif
3602 #ifdef HAVE_TARGET_64_LITTLE
3603 template
3604 off_t
3605 Output_section::add_input_section<64, false>(
3606 Sized_relobj<64, false>* object,
3607 unsigned int shndx,
3608 const char* secname,
3609 const elfcpp::Shdr<64, false>& shdr,
3610 unsigned int reloc_shndx,
3611 bool have_sections_script);
3612 #endif
3614 #ifdef HAVE_TARGET_64_BIG
3615 template
3616 off_t
3617 Output_section::add_input_section<64, true>(
3618 Sized_relobj<64, true>* object,
3619 unsigned int shndx,
3620 const char* secname,
3621 const elfcpp::Shdr<64, true>& shdr,
3622 unsigned int reloc_shndx,
3623 bool have_sections_script);
3624 #endif
3626 #ifdef HAVE_TARGET_32_LITTLE
3627 template
3628 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
3629 #endif
3631 #ifdef HAVE_TARGET_32_BIG
3632 template
3633 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
3634 #endif
3636 #ifdef HAVE_TARGET_64_LITTLE
3637 template
3638 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
3639 #endif
3641 #ifdef HAVE_TARGET_64_BIG
3642 template
3643 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
3644 #endif
3646 #ifdef HAVE_TARGET_32_LITTLE
3647 template
3648 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
3649 #endif
3651 #ifdef HAVE_TARGET_32_BIG
3652 template
3653 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
3654 #endif
3656 #ifdef HAVE_TARGET_64_LITTLE
3657 template
3658 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
3659 #endif
3661 #ifdef HAVE_TARGET_64_BIG
3662 template
3663 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
3664 #endif
3666 #ifdef HAVE_TARGET_32_LITTLE
3667 template
3668 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
3669 #endif
3671 #ifdef HAVE_TARGET_32_BIG
3672 template
3673 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
3674 #endif
3676 #ifdef HAVE_TARGET_64_LITTLE
3677 template
3678 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
3679 #endif
3681 #ifdef HAVE_TARGET_64_BIG
3682 template
3683 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
3684 #endif
3686 #ifdef HAVE_TARGET_32_LITTLE
3687 template
3688 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
3689 #endif
3691 #ifdef HAVE_TARGET_32_BIG
3692 template
3693 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
3694 #endif
3696 #ifdef HAVE_TARGET_64_LITTLE
3697 template
3698 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
3699 #endif
3701 #ifdef HAVE_TARGET_64_BIG
3702 template
3703 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
3704 #endif
3706 #ifdef HAVE_TARGET_32_LITTLE
3707 template
3708 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
3709 #endif
3711 #ifdef HAVE_TARGET_32_BIG
3712 template
3713 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
3714 #endif
3716 #ifdef HAVE_TARGET_64_LITTLE
3717 template
3718 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
3719 #endif
3721 #ifdef HAVE_TARGET_64_BIG
3722 template
3723 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
3724 #endif
3726 #ifdef HAVE_TARGET_32_LITTLE
3727 template
3728 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
3729 #endif
3731 #ifdef HAVE_TARGET_32_BIG
3732 template
3733 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
3734 #endif
3736 #ifdef HAVE_TARGET_64_LITTLE
3737 template
3738 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
3739 #endif
3741 #ifdef HAVE_TARGET_64_BIG
3742 template
3743 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
3744 #endif
3746 #ifdef HAVE_TARGET_32_LITTLE
3747 template
3748 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
3749 #endif
3751 #ifdef HAVE_TARGET_32_BIG
3752 template
3753 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
3754 #endif
3756 #ifdef HAVE_TARGET_64_LITTLE
3757 template
3758 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
3759 #endif
3761 #ifdef HAVE_TARGET_64_BIG
3762 template
3763 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
3764 #endif
3766 #ifdef HAVE_TARGET_32_LITTLE
3767 template
3768 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
3769 #endif
3771 #ifdef HAVE_TARGET_32_BIG
3772 template
3773 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
3774 #endif
3776 #ifdef HAVE_TARGET_64_LITTLE
3777 template
3778 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
3779 #endif
3781 #ifdef HAVE_TARGET_64_BIG
3782 template
3783 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
3784 #endif
3786 #ifdef HAVE_TARGET_32_LITTLE
3787 template
3788 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3789 #endif
3791 #ifdef HAVE_TARGET_32_BIG
3792 template
3793 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3794 #endif
3796 #ifdef HAVE_TARGET_64_LITTLE
3797 template
3798 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3799 #endif
3801 #ifdef HAVE_TARGET_64_BIG
3802 template
3803 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3804 #endif
3806 #ifdef HAVE_TARGET_32_LITTLE
3807 template
3808 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3809 #endif
3811 #ifdef HAVE_TARGET_32_BIG
3812 template
3813 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3814 #endif
3816 #ifdef HAVE_TARGET_64_LITTLE
3817 template
3818 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3819 #endif
3821 #ifdef HAVE_TARGET_64_BIG
3822 template
3823 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3824 #endif
3826 #ifdef HAVE_TARGET_32_LITTLE
3827 template
3828 class Output_data_group<32, false>;
3829 #endif
3831 #ifdef HAVE_TARGET_32_BIG
3832 template
3833 class Output_data_group<32, true>;
3834 #endif
3836 #ifdef HAVE_TARGET_64_LITTLE
3837 template
3838 class Output_data_group<64, false>;
3839 #endif
3841 #ifdef HAVE_TARGET_64_BIG
3842 template
3843 class Output_data_group<64, true>;
3844 #endif
3846 #ifdef HAVE_TARGET_32_LITTLE
3847 template
3848 class Output_data_got<32, false>;
3849 #endif
3851 #ifdef HAVE_TARGET_32_BIG
3852 template
3853 class Output_data_got<32, true>;
3854 #endif
3856 #ifdef HAVE_TARGET_64_LITTLE
3857 template
3858 class Output_data_got<64, false>;
3859 #endif
3861 #ifdef HAVE_TARGET_64_BIG
3862 template
3863 class Output_data_got<64, true>;
3864 #endif
3866 } // End namespace gold.