Add -Wshadow to the gcc command line options used when compiling the binutils.
[binutils.git] / gold / output.cc
blob2f9ed2330f2d2286ce3531a419e34e01ca7e5749
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"
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)
114 // Compute the current data size.
116 off_t
117 Output_section_headers::do_size() const
119 // Count all the sections. Start with 1 for the null section.
120 off_t count = 1;
121 if (!parameters->options().relocatable())
123 for (Layout::Segment_list::const_iterator p =
124 this->segment_list_->begin();
125 p != this->segment_list_->end();
126 ++p)
127 if ((*p)->type() == elfcpp::PT_LOAD)
128 count += (*p)->output_section_count();
130 else
132 for (Layout::Section_list::const_iterator p =
133 this->section_list_->begin();
134 p != this->section_list_->end();
135 ++p)
136 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
137 ++count;
139 count += this->unattached_section_list_->size();
141 const int size = parameters->target().get_size();
142 int shdr_size;
143 if (size == 32)
144 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
145 else if (size == 64)
146 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
147 else
148 gold_unreachable();
150 return count * shdr_size;
153 // Write out the section headers.
155 void
156 Output_section_headers::do_write(Output_file* of)
158 switch (parameters->size_and_endianness())
160 #ifdef HAVE_TARGET_32_LITTLE
161 case Parameters::TARGET_32_LITTLE:
162 this->do_sized_write<32, false>(of);
163 break;
164 #endif
165 #ifdef HAVE_TARGET_32_BIG
166 case Parameters::TARGET_32_BIG:
167 this->do_sized_write<32, true>(of);
168 break;
169 #endif
170 #ifdef HAVE_TARGET_64_LITTLE
171 case Parameters::TARGET_64_LITTLE:
172 this->do_sized_write<64, false>(of);
173 break;
174 #endif
175 #ifdef HAVE_TARGET_64_BIG
176 case Parameters::TARGET_64_BIG:
177 this->do_sized_write<64, true>(of);
178 break;
179 #endif
180 default:
181 gold_unreachable();
185 template<int size, bool big_endian>
186 void
187 Output_section_headers::do_sized_write(Output_file* of)
189 off_t all_shdrs_size = this->data_size();
190 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
192 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
193 unsigned char* v = view;
196 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
197 oshdr.put_sh_name(0);
198 oshdr.put_sh_type(elfcpp::SHT_NULL);
199 oshdr.put_sh_flags(0);
200 oshdr.put_sh_addr(0);
201 oshdr.put_sh_offset(0);
203 size_t section_count = (this->data_size()
204 / elfcpp::Elf_sizes<size>::shdr_size);
205 if (section_count < elfcpp::SHN_LORESERVE)
206 oshdr.put_sh_size(0);
207 else
208 oshdr.put_sh_size(section_count);
210 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
211 if (shstrndx < elfcpp::SHN_LORESERVE)
212 oshdr.put_sh_link(0);
213 else
214 oshdr.put_sh_link(shstrndx);
216 oshdr.put_sh_info(0);
217 oshdr.put_sh_addralign(0);
218 oshdr.put_sh_entsize(0);
221 v += shdr_size;
223 unsigned int shndx = 1;
224 if (!parameters->options().relocatable())
226 for (Layout::Segment_list::const_iterator p =
227 this->segment_list_->begin();
228 p != this->segment_list_->end();
229 ++p)
230 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
231 this->secnamepool_,
233 &shndx);
235 else
237 for (Layout::Section_list::const_iterator p =
238 this->section_list_->begin();
239 p != this->section_list_->end();
240 ++p)
242 // We do unallocated sections below, except that group
243 // sections have to come first.
244 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
245 && (*p)->type() != elfcpp::SHT_GROUP)
246 continue;
247 gold_assert(shndx == (*p)->out_shndx());
248 elfcpp::Shdr_write<size, big_endian> oshdr(v);
249 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
250 v += shdr_size;
251 ++shndx;
255 for (Layout::Section_list::const_iterator p =
256 this->unattached_section_list_->begin();
257 p != this->unattached_section_list_->end();
258 ++p)
260 // For a relocatable link, we did unallocated group sections
261 // above, since they have to come first.
262 if ((*p)->type() == elfcpp::SHT_GROUP
263 && parameters->options().relocatable())
264 continue;
265 gold_assert(shndx == (*p)->out_shndx());
266 elfcpp::Shdr_write<size, big_endian> oshdr(v);
267 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
268 v += shdr_size;
269 ++shndx;
272 of->write_output_view(this->offset(), all_shdrs_size, view);
275 // Output_segment_header methods.
277 Output_segment_headers::Output_segment_headers(
278 const Layout::Segment_list& segment_list)
279 : segment_list_(segment_list)
283 void
284 Output_segment_headers::do_write(Output_file* of)
286 switch (parameters->size_and_endianness())
288 #ifdef HAVE_TARGET_32_LITTLE
289 case Parameters::TARGET_32_LITTLE:
290 this->do_sized_write<32, false>(of);
291 break;
292 #endif
293 #ifdef HAVE_TARGET_32_BIG
294 case Parameters::TARGET_32_BIG:
295 this->do_sized_write<32, true>(of);
296 break;
297 #endif
298 #ifdef HAVE_TARGET_64_LITTLE
299 case Parameters::TARGET_64_LITTLE:
300 this->do_sized_write<64, false>(of);
301 break;
302 #endif
303 #ifdef HAVE_TARGET_64_BIG
304 case Parameters::TARGET_64_BIG:
305 this->do_sized_write<64, true>(of);
306 break;
307 #endif
308 default:
309 gold_unreachable();
313 template<int size, bool big_endian>
314 void
315 Output_segment_headers::do_sized_write(Output_file* of)
317 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
318 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
319 gold_assert(all_phdrs_size == this->data_size());
320 unsigned char* view = of->get_output_view(this->offset(),
321 all_phdrs_size);
322 unsigned char* v = view;
323 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
324 p != this->segment_list_.end();
325 ++p)
327 elfcpp::Phdr_write<size, big_endian> ophdr(v);
328 (*p)->write_header(&ophdr);
329 v += phdr_size;
332 gold_assert(v - view == all_phdrs_size);
334 of->write_output_view(this->offset(), all_phdrs_size, view);
337 off_t
338 Output_segment_headers::do_size() const
340 const int size = parameters->target().get_size();
341 int phdr_size;
342 if (size == 32)
343 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
344 else if (size == 64)
345 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
346 else
347 gold_unreachable();
349 return this->segment_list_.size() * phdr_size;
352 // Output_file_header methods.
354 Output_file_header::Output_file_header(const Target* target,
355 const Symbol_table* symtab,
356 const Output_segment_headers* osh,
357 const char* aentry)
358 : target_(target),
359 symtab_(symtab),
360 segment_header_(osh),
361 section_header_(NULL),
362 shstrtab_(NULL),
363 entry_(aentry)
365 this->set_data_size(this->do_size());
368 // Set the section table information for a file header.
370 void
371 Output_file_header::set_section_info(const Output_section_headers* shdrs,
372 const Output_section* shstrtab)
374 this->section_header_ = shdrs;
375 this->shstrtab_ = shstrtab;
378 // Write out the file header.
380 void
381 Output_file_header::do_write(Output_file* of)
383 gold_assert(this->offset() == 0);
385 switch (parameters->size_and_endianness())
387 #ifdef HAVE_TARGET_32_LITTLE
388 case Parameters::TARGET_32_LITTLE:
389 this->do_sized_write<32, false>(of);
390 break;
391 #endif
392 #ifdef HAVE_TARGET_32_BIG
393 case Parameters::TARGET_32_BIG:
394 this->do_sized_write<32, true>(of);
395 break;
396 #endif
397 #ifdef HAVE_TARGET_64_LITTLE
398 case Parameters::TARGET_64_LITTLE:
399 this->do_sized_write<64, false>(of);
400 break;
401 #endif
402 #ifdef HAVE_TARGET_64_BIG
403 case Parameters::TARGET_64_BIG:
404 this->do_sized_write<64, true>(of);
405 break;
406 #endif
407 default:
408 gold_unreachable();
412 // Write out the file header with appropriate size and endianess.
414 template<int size, bool big_endian>
415 void
416 Output_file_header::do_sized_write(Output_file* of)
418 gold_assert(this->offset() == 0);
420 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
421 unsigned char* view = of->get_output_view(0, ehdr_size);
422 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
424 unsigned char e_ident[elfcpp::EI_NIDENT];
425 memset(e_ident, 0, elfcpp::EI_NIDENT);
426 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
427 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
428 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
429 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
430 if (size == 32)
431 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
432 else if (size == 64)
433 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
434 else
435 gold_unreachable();
436 e_ident[elfcpp::EI_DATA] = (big_endian
437 ? elfcpp::ELFDATA2MSB
438 : elfcpp::ELFDATA2LSB);
439 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
440 oehdr.put_e_ident(e_ident);
442 elfcpp::ET e_type;
443 if (parameters->options().relocatable())
444 e_type = elfcpp::ET_REL;
445 else if (parameters->options().output_is_position_independent())
446 e_type = elfcpp::ET_DYN;
447 else
448 e_type = elfcpp::ET_EXEC;
449 oehdr.put_e_type(e_type);
451 oehdr.put_e_machine(this->target_->machine_code());
452 oehdr.put_e_version(elfcpp::EV_CURRENT);
454 oehdr.put_e_entry(this->entry<size>());
456 if (this->segment_header_ == NULL)
457 oehdr.put_e_phoff(0);
458 else
459 oehdr.put_e_phoff(this->segment_header_->offset());
461 oehdr.put_e_shoff(this->section_header_->offset());
462 oehdr.put_e_flags(this->target_->processor_specific_flags());
463 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
465 if (this->segment_header_ == NULL)
467 oehdr.put_e_phentsize(0);
468 oehdr.put_e_phnum(0);
470 else
472 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
473 oehdr.put_e_phnum(this->segment_header_->data_size()
474 / elfcpp::Elf_sizes<size>::phdr_size);
477 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
478 size_t section_count = (this->section_header_->data_size()
479 / elfcpp::Elf_sizes<size>::shdr_size);
481 if (section_count < elfcpp::SHN_LORESERVE)
482 oehdr.put_e_shnum(this->section_header_->data_size()
483 / elfcpp::Elf_sizes<size>::shdr_size);
484 else
485 oehdr.put_e_shnum(0);
487 unsigned int shstrndx = this->shstrtab_->out_shndx();
488 if (shstrndx < elfcpp::SHN_LORESERVE)
489 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
490 else
491 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
493 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
494 // the e_ident field.
495 parameters->target().adjust_elf_header(view, ehdr_size);
497 of->write_output_view(0, ehdr_size, view);
500 // Return the value to use for the entry address. THIS->ENTRY_ is the
501 // symbol specified on the command line, if any.
503 template<int size>
504 typename elfcpp::Elf_types<size>::Elf_Addr
505 Output_file_header::entry()
507 const bool should_issue_warning = (this->entry_ != NULL
508 && !parameters->options().relocatable()
509 && !parameters->options().shared());
511 // FIXME: Need to support target specific entry symbol.
512 const char* entry_sym = this->entry_;
513 if (entry_sym == NULL)
514 entry_sym = "_start";
516 Symbol* sym = this->symtab_->lookup(entry_sym);
518 typename Sized_symbol<size>::Value_type v;
519 if (sym != NULL)
521 Sized_symbol<size>* ssym;
522 ssym = this->symtab_->get_sized_symbol<size>(sym);
523 if (!ssym->is_defined() && should_issue_warning)
524 gold_warning("entry symbol '%s' exists but is not defined", entry_sym);
525 v = ssym->value();
527 else
529 // We couldn't find the entry symbol. See if we can parse it as
530 // a number. This supports, e.g., -e 0x1000.
531 char* endptr;
532 v = strtoull(entry_sym, &endptr, 0);
533 if (*endptr != '\0')
535 if (should_issue_warning)
536 gold_warning("cannot find entry symbol '%s'", entry_sym);
537 v = 0;
541 return v;
544 // Compute the current data size.
546 off_t
547 Output_file_header::do_size() const
549 const int size = parameters->target().get_size();
550 if (size == 32)
551 return elfcpp::Elf_sizes<32>::ehdr_size;
552 else if (size == 64)
553 return elfcpp::Elf_sizes<64>::ehdr_size;
554 else
555 gold_unreachable();
558 // Output_data_const methods.
560 void
561 Output_data_const::do_write(Output_file* of)
563 of->write(this->offset(), this->data_.data(), this->data_.size());
566 // Output_data_const_buffer methods.
568 void
569 Output_data_const_buffer::do_write(Output_file* of)
571 of->write(this->offset(), this->p_, this->data_size());
574 // Output_section_data methods.
576 // Record the output section, and set the entry size and such.
578 void
579 Output_section_data::set_output_section(Output_section* os)
581 gold_assert(this->output_section_ == NULL);
582 this->output_section_ = os;
583 this->do_adjust_output_section(os);
586 // Return the section index of the output section.
588 unsigned int
589 Output_section_data::do_out_shndx() const
591 gold_assert(this->output_section_ != NULL);
592 return this->output_section_->out_shndx();
595 // Set the alignment, which means we may need to update the alignment
596 // of the output section.
598 void
599 Output_section_data::set_addralign(uint64_t addr_align)
601 this->addralign_ = addr_align;
602 if (this->output_section_ != NULL
603 && this->output_section_->addralign() < addr_align)
604 this->output_section_->set_addralign(addr_align);
607 // Output_data_strtab methods.
609 // Set the final data size.
611 void
612 Output_data_strtab::set_final_data_size()
614 this->strtab_->set_string_offsets();
615 this->set_data_size(this->strtab_->get_strtab_size());
618 // Write out a string table.
620 void
621 Output_data_strtab::do_write(Output_file* of)
623 this->strtab_->write(of, this->offset());
626 // Output_reloc methods.
628 // A reloc against a global symbol.
630 template<bool dynamic, int size, bool big_endian>
631 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
632 Symbol* gsym,
633 unsigned int type,
634 Output_data* od,
635 Address address,
636 bool is_rel)
637 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
638 is_relative_(is_rel), is_section_symbol_(false), shndx_(INVALID_CODE)
640 // this->type_ is a bitfield; make sure TYPE fits.
641 gold_assert(this->type_ == type);
642 this->u1_.gsym = gsym;
643 this->u2_.od = od;
644 if (dynamic)
645 this->set_needs_dynsym_index();
648 template<bool dynamic, int size, bool big_endian>
649 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
650 Symbol* gsym,
651 unsigned int type,
652 Sized_relobj<size, big_endian>* relobj,
653 unsigned int shndx,
654 Address address,
655 bool is_rel)
656 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
657 is_relative_(is_rel), is_section_symbol_(false), shndx_(shndx)
659 gold_assert(shndx != INVALID_CODE);
660 // this->type_ is a bitfield; make sure TYPE fits.
661 gold_assert(this->type_ == type);
662 this->u1_.gsym = gsym;
663 this->u2_.relobj = relobj;
664 if (dynamic)
665 this->set_needs_dynsym_index();
668 // A reloc against a local symbol.
670 template<bool dynamic, int size, bool big_endian>
671 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
672 Sized_relobj<size, big_endian>* relobj,
673 unsigned int local_sym_index,
674 unsigned int type,
675 Output_data* od,
676 Address address,
677 bool is_rel,
678 bool is_section_symbol)
679 : address_(address), local_sym_index_(local_sym_index), type_(type),
680 is_relative_(is_rel), is_section_symbol_(is_section_symbol),
681 shndx_(INVALID_CODE)
683 gold_assert(local_sym_index != GSYM_CODE
684 && local_sym_index != INVALID_CODE);
685 // this->type_ is a bitfield; make sure TYPE fits.
686 gold_assert(this->type_ == type);
687 this->u1_.relobj = relobj;
688 this->u2_.od = od;
689 if (dynamic)
690 this->set_needs_dynsym_index();
693 template<bool dynamic, int size, bool big_endian>
694 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
695 Sized_relobj<size, big_endian>* relobj,
696 unsigned int local_sym_index,
697 unsigned int type,
698 unsigned int shndx,
699 Address address,
700 bool is_rel,
701 bool is_section_symbol)
702 : address_(address), local_sym_index_(local_sym_index), type_(type),
703 is_relative_(is_rel), is_section_symbol_(is_section_symbol),
704 shndx_(shndx)
706 gold_assert(local_sym_index != GSYM_CODE
707 && local_sym_index != INVALID_CODE);
708 gold_assert(shndx != INVALID_CODE);
709 // this->type_ is a bitfield; make sure TYPE fits.
710 gold_assert(this->type_ == type);
711 this->u1_.relobj = relobj;
712 this->u2_.relobj = relobj;
713 if (dynamic)
714 this->set_needs_dynsym_index();
717 // A reloc against the STT_SECTION symbol of an output section.
719 template<bool dynamic, int size, bool big_endian>
720 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
721 Output_section* os,
722 unsigned int type,
723 Output_data* od,
724 Address address)
725 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
726 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
728 // this->type_ is a bitfield; make sure TYPE fits.
729 gold_assert(this->type_ == type);
730 this->u1_.os = os;
731 this->u2_.od = od;
732 if (dynamic)
733 this->set_needs_dynsym_index();
734 else
735 os->set_needs_symtab_index();
738 template<bool dynamic, int size, bool big_endian>
739 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
740 Output_section* os,
741 unsigned int type,
742 Sized_relobj<size, big_endian>* relobj,
743 unsigned int shndx,
744 Address address)
745 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
746 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
748 gold_assert(shndx != INVALID_CODE);
749 // this->type_ is a bitfield; make sure TYPE fits.
750 gold_assert(this->type_ == type);
751 this->u1_.os = os;
752 this->u2_.relobj = relobj;
753 if (dynamic)
754 this->set_needs_dynsym_index();
755 else
756 os->set_needs_symtab_index();
759 // Record that we need a dynamic symbol index for this relocation.
761 template<bool dynamic, int size, bool big_endian>
762 void
763 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
764 set_needs_dynsym_index()
766 if (this->is_relative_)
767 return;
768 switch (this->local_sym_index_)
770 case INVALID_CODE:
771 gold_unreachable();
773 case GSYM_CODE:
774 this->u1_.gsym->set_needs_dynsym_entry();
775 break;
777 case SECTION_CODE:
778 this->u1_.os->set_needs_dynsym_index();
779 break;
781 case 0:
782 break;
784 default:
786 const unsigned int lsi = this->local_sym_index_;
787 if (!this->is_section_symbol_)
788 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
789 else
790 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
792 break;
796 // Get the symbol index of a relocation.
798 template<bool dynamic, int size, bool big_endian>
799 unsigned int
800 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
801 const
803 unsigned int index;
804 switch (this->local_sym_index_)
806 case INVALID_CODE:
807 gold_unreachable();
809 case GSYM_CODE:
810 if (this->u1_.gsym == NULL)
811 index = 0;
812 else if (dynamic)
813 index = this->u1_.gsym->dynsym_index();
814 else
815 index = this->u1_.gsym->symtab_index();
816 break;
818 case SECTION_CODE:
819 if (dynamic)
820 index = this->u1_.os->dynsym_index();
821 else
822 index = this->u1_.os->symtab_index();
823 break;
825 case 0:
826 // Relocations without symbols use a symbol index of 0.
827 index = 0;
828 break;
830 default:
832 const unsigned int lsi = this->local_sym_index_;
833 if (!this->is_section_symbol_)
835 if (dynamic)
836 index = this->u1_.relobj->dynsym_index(lsi);
837 else
838 index = this->u1_.relobj->symtab_index(lsi);
840 else
842 Output_section* os = this->u1_.relobj->output_section(lsi);
843 gold_assert(os != NULL);
844 if (dynamic)
845 index = os->dynsym_index();
846 else
847 index = os->symtab_index();
850 break;
852 gold_assert(index != -1U);
853 return index;
856 // For a local section symbol, get the address of the offset ADDEND
857 // within the input section.
859 template<bool dynamic, int size, bool big_endian>
860 typename elfcpp::Elf_types<size>::Elf_Addr
861 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
862 local_section_offset(Addend addend) const
864 gold_assert(this->local_sym_index_ != GSYM_CODE
865 && this->local_sym_index_ != SECTION_CODE
866 && this->local_sym_index_ != INVALID_CODE
867 && this->is_section_symbol_);
868 const unsigned int lsi = this->local_sym_index_;
869 Output_section* os = this->u1_.relobj->output_section(lsi);
870 gold_assert(os != NULL);
871 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
872 if (offset != invalid_address)
873 return offset + addend;
874 // This is a merge section.
875 offset = os->output_address(this->u1_.relobj, lsi, addend);
876 gold_assert(offset != invalid_address);
877 return offset;
880 // Get the output address of a relocation.
882 template<bool dynamic, int size, bool big_endian>
883 typename elfcpp::Elf_types<size>::Elf_Addr
884 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
886 Address address = this->address_;
887 if (this->shndx_ != INVALID_CODE)
889 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
890 gold_assert(os != NULL);
891 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
892 if (off != invalid_address)
893 address += os->address() + off;
894 else
896 address = os->output_address(this->u2_.relobj, this->shndx_,
897 address);
898 gold_assert(address != invalid_address);
901 else if (this->u2_.od != NULL)
902 address += this->u2_.od->address();
903 return address;
906 // Write out the offset and info fields of a Rel or Rela relocation
907 // entry.
909 template<bool dynamic, int size, bool big_endian>
910 template<typename Write_rel>
911 void
912 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
913 Write_rel* wr) const
915 wr->put_r_offset(this->get_address());
916 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
917 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
920 // Write out a Rel relocation.
922 template<bool dynamic, int size, bool big_endian>
923 void
924 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
925 unsigned char* pov) const
927 elfcpp::Rel_write<size, big_endian> orel(pov);
928 this->write_rel(&orel);
931 // Get the value of the symbol referred to by a Rel relocation.
933 template<bool dynamic, int size, bool big_endian>
934 typename elfcpp::Elf_types<size>::Elf_Addr
935 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
936 Addend addend) const
938 if (this->local_sym_index_ == GSYM_CODE)
940 const Sized_symbol<size>* sym;
941 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
942 return sym->value() + addend;
944 gold_assert(this->local_sym_index_ != SECTION_CODE
945 && this->local_sym_index_ != INVALID_CODE
946 && !this->is_section_symbol_);
947 const unsigned int lsi = this->local_sym_index_;
948 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
949 return symval->value(this->u1_.relobj, addend);
952 // Reloc comparison. This function sorts the dynamic relocs for the
953 // benefit of the dynamic linker. First we sort all relative relocs
954 // to the front. Among relative relocs, we sort by output address.
955 // Among non-relative relocs, we sort by symbol index, then by output
956 // address.
958 template<bool dynamic, int size, bool big_endian>
960 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
961 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
962 const
964 if (this->is_relative_)
966 if (!r2.is_relative_)
967 return -1;
968 // Otherwise sort by reloc address below.
970 else if (r2.is_relative_)
971 return 1;
972 else
974 unsigned int sym1 = this->get_symbol_index();
975 unsigned int sym2 = r2.get_symbol_index();
976 if (sym1 < sym2)
977 return -1;
978 else if (sym1 > sym2)
979 return 1;
980 // Otherwise sort by reloc address.
983 section_offset_type addr1 = this->get_address();
984 section_offset_type addr2 = r2.get_address();
985 if (addr1 < addr2)
986 return -1;
987 else if (addr1 > addr2)
988 return 1;
990 // Final tie breaker, in order to generate the same output on any
991 // host: reloc type.
992 unsigned int type1 = this->type_;
993 unsigned int type2 = r2.type_;
994 if (type1 < type2)
995 return -1;
996 else if (type1 > type2)
997 return 1;
999 // These relocs appear to be exactly the same.
1000 return 0;
1003 // Write out a Rela relocation.
1005 template<bool dynamic, int size, bool big_endian>
1006 void
1007 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1008 unsigned char* pov) const
1010 elfcpp::Rela_write<size, big_endian> orel(pov);
1011 this->rel_.write_rel(&orel);
1012 Addend addend = this->addend_;
1013 if (this->rel_.is_relative())
1014 addend = this->rel_.symbol_value(addend);
1015 else if (this->rel_.is_local_section_symbol())
1016 addend = this->rel_.local_section_offset(addend);
1017 orel.put_r_addend(addend);
1020 // Output_data_reloc_base methods.
1022 // Adjust the output section.
1024 template<int sh_type, bool dynamic, int size, bool big_endian>
1025 void
1026 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1027 ::do_adjust_output_section(Output_section* os)
1029 if (sh_type == elfcpp::SHT_REL)
1030 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1031 else if (sh_type == elfcpp::SHT_RELA)
1032 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1033 else
1034 gold_unreachable();
1035 if (dynamic)
1036 os->set_should_link_to_dynsym();
1037 else
1038 os->set_should_link_to_symtab();
1041 // Write out relocation data.
1043 template<int sh_type, bool dynamic, int size, bool big_endian>
1044 void
1045 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1046 Output_file* of)
1048 const off_t off = this->offset();
1049 const off_t oview_size = this->data_size();
1050 unsigned char* const oview = of->get_output_view(off, oview_size);
1052 if (this->sort_relocs_)
1054 gold_assert(dynamic);
1055 std::sort(this->relocs_.begin(), this->relocs_.end(),
1056 Sort_relocs_comparison());
1059 unsigned char* pov = oview;
1060 for (typename Relocs::const_iterator p = this->relocs_.begin();
1061 p != this->relocs_.end();
1062 ++p)
1064 p->write(pov);
1065 pov += reloc_size;
1068 gold_assert(pov - oview == oview_size);
1070 of->write_output_view(off, oview_size, oview);
1072 // We no longer need the relocation entries.
1073 this->relocs_.clear();
1076 // Class Output_relocatable_relocs.
1078 template<int sh_type, int size, bool big_endian>
1079 void
1080 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1082 this->set_data_size(this->rr_->output_reloc_count()
1083 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1086 // class Output_data_group.
1088 template<int size, bool big_endian>
1089 Output_data_group<size, big_endian>::Output_data_group(
1090 Sized_relobj<size, big_endian>* relobj,
1091 section_size_type entry_count,
1092 elfcpp::Elf_Word flags,
1093 std::vector<unsigned int>* input_shndxes)
1094 : Output_section_data(entry_count * 4, 4, false),
1095 relobj_(relobj),
1096 flags_(flags)
1098 this->input_shndxes_.swap(*input_shndxes);
1101 // Write out the section group, which means translating the section
1102 // indexes to apply to the output file.
1104 template<int size, bool big_endian>
1105 void
1106 Output_data_group<size, big_endian>::do_write(Output_file* of)
1108 const off_t off = this->offset();
1109 const section_size_type oview_size =
1110 convert_to_section_size_type(this->data_size());
1111 unsigned char* const oview = of->get_output_view(off, oview_size);
1113 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1114 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1115 ++contents;
1117 for (std::vector<unsigned int>::const_iterator p =
1118 this->input_shndxes_.begin();
1119 p != this->input_shndxes_.end();
1120 ++p, ++contents)
1122 Output_section* os = this->relobj_->output_section(*p);
1124 unsigned int output_shndx;
1125 if (os != NULL)
1126 output_shndx = os->out_shndx();
1127 else
1129 this->relobj_->error(_("section group retained but "
1130 "group element discarded"));
1131 output_shndx = 0;
1134 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1137 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1138 gold_assert(wrote == oview_size);
1140 of->write_output_view(off, oview_size, oview);
1142 // We no longer need this information.
1143 this->input_shndxes_.clear();
1146 // Output_data_got::Got_entry methods.
1148 // Write out the entry.
1150 template<int size, bool big_endian>
1151 void
1152 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1154 Valtype val = 0;
1156 switch (this->local_sym_index_)
1158 case GSYM_CODE:
1160 // If the symbol is resolved locally, we need to write out the
1161 // link-time value, which will be relocated dynamically by a
1162 // RELATIVE relocation.
1163 Symbol* gsym = this->u_.gsym;
1164 Sized_symbol<size>* sgsym;
1165 // This cast is a bit ugly. We don't want to put a
1166 // virtual method in Symbol, because we want Symbol to be
1167 // as small as possible.
1168 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1169 val = sgsym->value();
1171 break;
1173 case CONSTANT_CODE:
1174 val = this->u_.constant;
1175 break;
1177 default:
1179 const unsigned int lsi = this->local_sym_index_;
1180 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1181 val = symval->value(this->u_.object, 0);
1183 break;
1186 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1189 // Output_data_got methods.
1191 // Add an entry for a global symbol to the GOT. This returns true if
1192 // this is a new GOT entry, false if the symbol already had a GOT
1193 // entry.
1195 template<int size, bool big_endian>
1196 bool
1197 Output_data_got<size, big_endian>::add_global(
1198 Symbol* gsym,
1199 unsigned int got_type)
1201 if (gsym->has_got_offset(got_type))
1202 return false;
1204 this->entries_.push_back(Got_entry(gsym));
1205 this->set_got_size();
1206 gsym->set_got_offset(got_type, this->last_got_offset());
1207 return true;
1210 // Add an entry for a global symbol to the GOT, and add a dynamic
1211 // relocation of type R_TYPE for the GOT entry.
1212 template<int size, bool big_endian>
1213 void
1214 Output_data_got<size, big_endian>::add_global_with_rel(
1215 Symbol* gsym,
1216 unsigned int got_type,
1217 Rel_dyn* rel_dyn,
1218 unsigned int r_type)
1220 if (gsym->has_got_offset(got_type))
1221 return;
1223 this->entries_.push_back(Got_entry());
1224 this->set_got_size();
1225 unsigned int g_offset = this->last_got_offset();
1226 gsym->set_got_offset(got_type, g_offset);
1227 rel_dyn->add_global(gsym, r_type, this, g_offset);
1230 template<int size, bool big_endian>
1231 void
1232 Output_data_got<size, big_endian>::add_global_with_rela(
1233 Symbol* gsym,
1234 unsigned int got_type,
1235 Rela_dyn* rela_dyn,
1236 unsigned int r_type)
1238 if (gsym->has_got_offset(got_type))
1239 return;
1241 this->entries_.push_back(Got_entry());
1242 this->set_got_size();
1243 unsigned int g_offset = this->last_got_offset();
1244 gsym->set_got_offset(got_type, g_offset);
1245 rela_dyn->add_global(gsym, r_type, this, g_offset, 0);
1248 // Add a pair of entries for a global symbol to the GOT, and add
1249 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1250 // If R_TYPE_2 == 0, add the second entry with no relocation.
1251 template<int size, bool big_endian>
1252 void
1253 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1254 Symbol* gsym,
1255 unsigned int got_type,
1256 Rel_dyn* rel_dyn,
1257 unsigned int r_type_1,
1258 unsigned int r_type_2)
1260 if (gsym->has_got_offset(got_type))
1261 return;
1263 this->entries_.push_back(Got_entry());
1264 unsigned int g_offset = this->last_got_offset();
1265 gsym->set_got_offset(got_type, g_offset);
1266 rel_dyn->add_global(gsym, r_type_1, this, g_offset);
1268 this->entries_.push_back(Got_entry());
1269 if (r_type_2 != 0)
1271 g_offset = this->last_got_offset();
1272 rel_dyn->add_global(gsym, r_type_2, this, g_offset);
1275 this->set_got_size();
1278 template<int size, bool big_endian>
1279 void
1280 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1281 Symbol* gsym,
1282 unsigned int got_type,
1283 Rela_dyn* rela_dyn,
1284 unsigned int r_type_1,
1285 unsigned int r_type_2)
1287 if (gsym->has_got_offset(got_type))
1288 return;
1290 this->entries_.push_back(Got_entry());
1291 unsigned int g_offset = this->last_got_offset();
1292 gsym->set_got_offset(got_type, g_offset);
1293 rela_dyn->add_global(gsym, r_type_1, this, g_offset, 0);
1295 this->entries_.push_back(Got_entry());
1296 if (r_type_2 != 0)
1298 g_offset = this->last_got_offset();
1299 rela_dyn->add_global(gsym, r_type_2, this, g_offset, 0);
1302 this->set_got_size();
1305 // Add an entry for a local symbol to the GOT. This returns true if
1306 // this is a new GOT entry, false if the symbol already has a GOT
1307 // entry.
1309 template<int size, bool big_endian>
1310 bool
1311 Output_data_got<size, big_endian>::add_local(
1312 Sized_relobj<size, big_endian>* object,
1313 unsigned int symndx,
1314 unsigned int got_type)
1316 if (object->local_has_got_offset(symndx, got_type))
1317 return false;
1319 this->entries_.push_back(Got_entry(object, symndx));
1320 this->set_got_size();
1321 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1322 return true;
1325 // Add an entry for a local symbol to the GOT, and add a dynamic
1326 // relocation of type R_TYPE for the GOT entry.
1327 template<int size, bool big_endian>
1328 void
1329 Output_data_got<size, big_endian>::add_local_with_rel(
1330 Sized_relobj<size, big_endian>* object,
1331 unsigned int symndx,
1332 unsigned int got_type,
1333 Rel_dyn* rel_dyn,
1334 unsigned int r_type)
1336 if (object->local_has_got_offset(symndx, got_type))
1337 return;
1339 this->entries_.push_back(Got_entry());
1340 this->set_got_size();
1341 unsigned int g_offset = this->last_got_offset();
1342 object->set_local_got_offset(symndx, got_type, g_offset);
1343 rel_dyn->add_local(object, symndx, r_type, this, g_offset);
1346 template<int size, bool big_endian>
1347 void
1348 Output_data_got<size, big_endian>::add_local_with_rela(
1349 Sized_relobj<size, big_endian>* object,
1350 unsigned int symndx,
1351 unsigned int got_type,
1352 Rela_dyn* rela_dyn,
1353 unsigned int r_type)
1355 if (object->local_has_got_offset(symndx, got_type))
1356 return;
1358 this->entries_.push_back(Got_entry());
1359 this->set_got_size();
1360 unsigned int g_offset = this->last_got_offset();
1361 object->set_local_got_offset(symndx, got_type, g_offset);
1362 rela_dyn->add_local(object, symndx, r_type, this, g_offset, 0);
1365 // Add a pair of entries for a local symbol to the GOT, and add
1366 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1367 // If R_TYPE_2 == 0, add the second entry with no relocation.
1368 template<int size, bool big_endian>
1369 void
1370 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1371 Sized_relobj<size, big_endian>* object,
1372 unsigned int symndx,
1373 unsigned int shndx,
1374 unsigned int got_type,
1375 Rel_dyn* rel_dyn,
1376 unsigned int r_type_1,
1377 unsigned int r_type_2)
1379 if (object->local_has_got_offset(symndx, got_type))
1380 return;
1382 this->entries_.push_back(Got_entry());
1383 unsigned int g_offset = this->last_got_offset();
1384 object->set_local_got_offset(symndx, got_type, g_offset);
1385 Output_section* os = object->output_section(shndx);
1386 rel_dyn->add_output_section(os, r_type_1, this, g_offset);
1388 this->entries_.push_back(Got_entry(object, symndx));
1389 if (r_type_2 != 0)
1391 g_offset = this->last_got_offset();
1392 rel_dyn->add_output_section(os, r_type_2, this, g_offset);
1395 this->set_got_size();
1398 template<int size, bool big_endian>
1399 void
1400 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1401 Sized_relobj<size, big_endian>* object,
1402 unsigned int symndx,
1403 unsigned int shndx,
1404 unsigned int got_type,
1405 Rela_dyn* rela_dyn,
1406 unsigned int r_type_1,
1407 unsigned int r_type_2)
1409 if (object->local_has_got_offset(symndx, got_type))
1410 return;
1412 this->entries_.push_back(Got_entry());
1413 unsigned int g_offset = this->last_got_offset();
1414 object->set_local_got_offset(symndx, got_type, g_offset);
1415 Output_section* os = object->output_section(shndx);
1416 rela_dyn->add_output_section(os, r_type_1, this, g_offset, 0);
1418 this->entries_.push_back(Got_entry(object, symndx));
1419 if (r_type_2 != 0)
1421 g_offset = this->last_got_offset();
1422 rela_dyn->add_output_section(os, r_type_2, this, g_offset, 0);
1425 this->set_got_size();
1428 // Write out the GOT.
1430 template<int size, bool big_endian>
1431 void
1432 Output_data_got<size, big_endian>::do_write(Output_file* of)
1434 const int add = size / 8;
1436 const off_t off = this->offset();
1437 const off_t oview_size = this->data_size();
1438 unsigned char* const oview = of->get_output_view(off, oview_size);
1440 unsigned char* pov = oview;
1441 for (typename Got_entries::const_iterator p = this->entries_.begin();
1442 p != this->entries_.end();
1443 ++p)
1445 p->write(pov);
1446 pov += add;
1449 gold_assert(pov - oview == oview_size);
1451 of->write_output_view(off, oview_size, oview);
1453 // We no longer need the GOT entries.
1454 this->entries_.clear();
1457 // Output_data_dynamic::Dynamic_entry methods.
1459 // Write out the entry.
1461 template<int size, bool big_endian>
1462 void
1463 Output_data_dynamic::Dynamic_entry::write(
1464 unsigned char* pov,
1465 const Stringpool* pool) const
1467 typename elfcpp::Elf_types<size>::Elf_WXword val;
1468 switch (this->offset_)
1470 case DYNAMIC_NUMBER:
1471 val = this->u_.val;
1472 break;
1474 case DYNAMIC_SECTION_SIZE:
1475 val = this->u_.od->data_size();
1476 break;
1478 case DYNAMIC_SYMBOL:
1480 const Sized_symbol<size>* s =
1481 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1482 val = s->value();
1484 break;
1486 case DYNAMIC_STRING:
1487 val = pool->get_offset(this->u_.str);
1488 break;
1490 default:
1491 val = this->u_.od->address() + this->offset_;
1492 break;
1495 elfcpp::Dyn_write<size, big_endian> dw(pov);
1496 dw.put_d_tag(this->tag_);
1497 dw.put_d_val(val);
1500 // Output_data_dynamic methods.
1502 // Adjust the output section to set the entry size.
1504 void
1505 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1507 if (parameters->target().get_size() == 32)
1508 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1509 else if (parameters->target().get_size() == 64)
1510 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1511 else
1512 gold_unreachable();
1515 // Set the final data size.
1517 void
1518 Output_data_dynamic::set_final_data_size()
1520 // Add the terminating entry if it hasn't been added.
1521 // Because of relaxation, we can run this multiple times.
1522 if (this->entries_.empty()
1523 || this->entries_.rbegin()->tag() != elfcpp::DT_NULL)
1524 this->add_constant(elfcpp::DT_NULL, 0);
1526 int dyn_size;
1527 if (parameters->target().get_size() == 32)
1528 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1529 else if (parameters->target().get_size() == 64)
1530 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1531 else
1532 gold_unreachable();
1533 this->set_data_size(this->entries_.size() * dyn_size);
1536 // Write out the dynamic entries.
1538 void
1539 Output_data_dynamic::do_write(Output_file* of)
1541 switch (parameters->size_and_endianness())
1543 #ifdef HAVE_TARGET_32_LITTLE
1544 case Parameters::TARGET_32_LITTLE:
1545 this->sized_write<32, false>(of);
1546 break;
1547 #endif
1548 #ifdef HAVE_TARGET_32_BIG
1549 case Parameters::TARGET_32_BIG:
1550 this->sized_write<32, true>(of);
1551 break;
1552 #endif
1553 #ifdef HAVE_TARGET_64_LITTLE
1554 case Parameters::TARGET_64_LITTLE:
1555 this->sized_write<64, false>(of);
1556 break;
1557 #endif
1558 #ifdef HAVE_TARGET_64_BIG
1559 case Parameters::TARGET_64_BIG:
1560 this->sized_write<64, true>(of);
1561 break;
1562 #endif
1563 default:
1564 gold_unreachable();
1568 template<int size, bool big_endian>
1569 void
1570 Output_data_dynamic::sized_write(Output_file* of)
1572 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1574 const off_t off = this->offset();
1575 const off_t oview_size = this->data_size();
1576 unsigned char* const oview = of->get_output_view(off, oview_size);
1578 unsigned char* pov = oview;
1579 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1580 p != this->entries_.end();
1581 ++p)
1583 p->write<size, big_endian>(pov, this->pool_);
1584 pov += dyn_size;
1587 gold_assert(pov - oview == oview_size);
1589 of->write_output_view(off, oview_size, oview);
1591 // We no longer need the dynamic entries.
1592 this->entries_.clear();
1595 // Class Output_symtab_xindex.
1597 void
1598 Output_symtab_xindex::do_write(Output_file* of)
1600 const off_t off = this->offset();
1601 const off_t oview_size = this->data_size();
1602 unsigned char* const oview = of->get_output_view(off, oview_size);
1604 memset(oview, 0, oview_size);
1606 if (parameters->target().is_big_endian())
1607 this->endian_do_write<true>(oview);
1608 else
1609 this->endian_do_write<false>(oview);
1611 of->write_output_view(off, oview_size, oview);
1613 // We no longer need the data.
1614 this->entries_.clear();
1617 template<bool big_endian>
1618 void
1619 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1621 for (Xindex_entries::const_iterator p = this->entries_.begin();
1622 p != this->entries_.end();
1623 ++p)
1625 unsigned int symndx = p->first;
1626 gold_assert(symndx * 4 < this->data_size());
1627 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1631 // Output_section::Input_section methods.
1633 // Return the data size. For an input section we store the size here.
1634 // For an Output_section_data, we have to ask it for the size.
1636 off_t
1637 Output_section::Input_section::data_size() const
1639 if (this->is_input_section())
1640 return this->u1_.data_size;
1641 else
1642 return this->u2_.posd->data_size();
1645 // Set the address and file offset.
1647 void
1648 Output_section::Input_section::set_address_and_file_offset(
1649 uint64_t address,
1650 off_t file_offset,
1651 off_t section_file_offset)
1653 if (this->is_input_section())
1654 this->u2_.object->set_section_offset(this->shndx_,
1655 file_offset - section_file_offset);
1656 else
1657 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1660 // Reset the address and file offset.
1662 void
1663 Output_section::Input_section::reset_address_and_file_offset()
1665 if (!this->is_input_section())
1666 this->u2_.posd->reset_address_and_file_offset();
1669 // Finalize the data size.
1671 void
1672 Output_section::Input_section::finalize_data_size()
1674 if (!this->is_input_section())
1675 this->u2_.posd->finalize_data_size();
1678 // Try to turn an input offset into an output offset. We want to
1679 // return the output offset relative to the start of this
1680 // Input_section in the output section.
1682 inline bool
1683 Output_section::Input_section::output_offset(
1684 const Relobj* object,
1685 unsigned int sec_shndx,
1686 section_offset_type off,
1687 section_offset_type *poutput) const
1689 if (!this->is_input_section())
1690 return this->u2_.posd->output_offset(object, sec_shndx, off, poutput);
1691 else
1693 if (this->shndx_ != sec_shndx || this->u2_.object != object)
1694 return false;
1695 *poutput = off;
1696 return true;
1700 // Return whether this is the merge section for the input section
1701 // SHNDX in OBJECT.
1703 inline bool
1704 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1705 unsigned int sec_shndx) const
1707 if (this->is_input_section())
1708 return false;
1709 return this->u2_.posd->is_merge_section_for(object, sec_shndx);
1712 // Write out the data. We don't have to do anything for an input
1713 // section--they are handled via Object::relocate--but this is where
1714 // we write out the data for an Output_section_data.
1716 void
1717 Output_section::Input_section::write(Output_file* of)
1719 if (!this->is_input_section())
1720 this->u2_.posd->write(of);
1723 // Write the data to a buffer. As for write(), we don't have to do
1724 // anything for an input section.
1726 void
1727 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1729 if (!this->is_input_section())
1730 this->u2_.posd->write_to_buffer(buffer);
1733 // Print to a map file.
1735 void
1736 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1738 switch (this->shndx_)
1740 case OUTPUT_SECTION_CODE:
1741 case MERGE_DATA_SECTION_CODE:
1742 case MERGE_STRING_SECTION_CODE:
1743 this->u2_.posd->print_to_mapfile(mapfile);
1744 break;
1746 case RELAXED_INPUT_SECTION_CODE:
1748 Output_relaxed_input_section* relaxed_section =
1749 this->relaxed_input_section();
1750 mapfile->print_input_section(relaxed_section->relobj(),
1751 relaxed_section->shndx());
1753 break;
1754 default:
1755 mapfile->print_input_section(this->u2_.object, this->shndx_);
1756 break;
1760 // Output_section methods.
1762 // Construct an Output_section. NAME will point into a Stringpool.
1764 Output_section::Output_section(const char* aname, elfcpp::Elf_Word atype,
1765 elfcpp::Elf_Xword aflags)
1766 : name_(aname),
1767 addralign_(0),
1768 entsize_(0),
1769 load_address_(0),
1770 link_section_(NULL),
1771 link_(0),
1772 info_section_(NULL),
1773 info_symndx_(NULL),
1774 info_(0),
1775 type_(atype),
1776 flags_(aflags),
1777 out_shndx_(-1U),
1778 symtab_index_(0),
1779 dynsym_index_(0),
1780 input_sections_(),
1781 first_input_offset_(0),
1782 fills_(),
1783 postprocessing_buffer_(NULL),
1784 needs_symtab_index_(false),
1785 needs_dynsym_index_(false),
1786 should_link_to_symtab_(false),
1787 should_link_to_dynsym_(false),
1788 after_input_sections_(false),
1789 requires_postprocessing_(false),
1790 found_in_sections_clause_(false),
1791 has_load_address_(false),
1792 info_uses_section_index_(false),
1793 may_sort_attached_input_sections_(false),
1794 must_sort_attached_input_sections_(false),
1795 attached_input_sections_are_sorted_(false),
1796 is_relro_(false),
1797 is_relro_local_(false),
1798 is_small_section_(false),
1799 is_large_section_(false),
1800 is_interp_(false),
1801 is_dynamic_linker_section_(false),
1802 generate_code_fills_at_write_(false),
1803 tls_offset_(0),
1804 checkpoint_(NULL),
1805 merge_section_map_(),
1806 merge_section_by_properties_map_(),
1807 relaxed_input_section_map_(),
1808 is_relaxed_input_section_map_valid_(true)
1810 // An unallocated section has no address. Forcing this means that
1811 // we don't need special treatment for symbols defined in debug
1812 // sections.
1813 if ((aflags & elfcpp::SHF_ALLOC) == 0)
1814 this->set_address(0);
1817 Output_section::~Output_section()
1819 delete this->checkpoint_;
1822 // Set the entry size.
1824 void
1825 Output_section::set_entsize(uint64_t v)
1827 if (this->entsize_ == 0)
1828 this->entsize_ = v;
1829 else
1830 gold_assert(this->entsize_ == v);
1833 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1834 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1835 // relocation section which applies to this section, or 0 if none, or
1836 // -1U if more than one. Return the offset of the input section
1837 // within the output section. Return -1 if the input section will
1838 // receive special handling. In the normal case we don't always keep
1839 // track of input sections for an Output_section. Instead, each
1840 // Object keeps track of the Output_section for each of its input
1841 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1842 // track of input sections here; this is used when SECTIONS appears in
1843 // a linker script.
1845 template<int size, bool big_endian>
1846 off_t
1847 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1848 unsigned int sec_shndx,
1849 const char* secname,
1850 const elfcpp::Shdr<size, big_endian>& shdr,
1851 unsigned int reloc_shndx,
1852 bool have_sections_script)
1854 elfcpp::Elf_Xword addr_align = shdr.get_sh_addralign();
1855 if ((addr_align & (addr_align - 1)) != 0)
1857 object->error(_("invalid alignment %lu for section \"%s\""),
1858 static_cast<unsigned long>(addr_align), secname);
1859 addr_align = 1;
1862 if (addr_align > this->addralign_)
1863 this->addralign_ = addr_align;
1865 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1866 this->update_flags_for_input_section(sh_flags);
1868 uint64_t ent_size = shdr.get_sh_entsize();
1870 // .debug_str is a mergeable string section, but is not always so
1871 // marked by compilers. Mark manually here so we can optimize.
1872 if (strcmp(secname, ".debug_str") == 0)
1874 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1875 ent_size = 1;
1878 // If this is a SHF_MERGE section, we pass all the input sections to
1879 // a Output_data_merge. We don't try to handle relocations for such
1880 // a section. We don't try to handle empty merge sections--they
1881 // mess up the mappings, and are useless anyhow.
1882 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1883 && reloc_shndx == 0
1884 && shdr.get_sh_size() > 0)
1886 if (this->add_merge_input_section(object, sec_shndx, sh_flags,
1887 ent_size, addr_align))
1889 // Tell the relocation routines that they need to call the
1890 // output_offset method to determine the final address.
1891 return -1;
1895 off_t offset_in_section = this->current_data_size_for_child();
1896 off_t aligned_offset_in_section = align_address(offset_in_section,
1897 addr_align);
1899 // Determine if we want to delay code-fill generation until the output
1900 // section is written. When the target is relaxing, we want to delay fill
1901 // generating to avoid adjusting them during relaxation.
1902 if (!this->generate_code_fills_at_write_
1903 && !have_sections_script
1904 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1905 && parameters->target().has_code_fill()
1906 && parameters->target().may_relax())
1908 gold_assert(this->fills_.empty());
1909 this->generate_code_fills_at_write_ = true;
1912 if (aligned_offset_in_section > offset_in_section
1913 && !this->generate_code_fills_at_write_
1914 && !have_sections_script
1915 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1916 && parameters->target().has_code_fill())
1918 // We need to add some fill data. Using fill_list_ when
1919 // possible is an optimization, since we will often have fill
1920 // sections without input sections.
1921 off_t fill_len = aligned_offset_in_section - offset_in_section;
1922 if (this->input_sections_.empty())
1923 this->fills_.push_back(Fill(offset_in_section, fill_len));
1924 else
1926 std::string fill_data(parameters->target().code_fill(fill_len));
1927 Output_data_const* odc = new Output_data_const(fill_data, 1);
1928 this->input_sections_.push_back(Input_section(odc));
1932 this->set_current_data_size_for_child(aligned_offset_in_section
1933 + shdr.get_sh_size());
1935 // We need to keep track of this section if we are already keeping
1936 // track of sections, or if we are relaxing. Also, if this is a
1937 // section which requires sorting, or which may require sorting in
1938 // the future, we keep track of the sections.
1939 if (have_sections_script
1940 || !this->input_sections_.empty()
1941 || this->may_sort_attached_input_sections()
1942 || this->must_sort_attached_input_sections()
1943 || parameters->options().user_set_Map()
1944 || parameters->target().may_relax())
1945 this->input_sections_.push_back(Input_section(object, sec_shndx,
1946 shdr.get_sh_size(),
1947 addr_align));
1949 return aligned_offset_in_section;
1952 // Add arbitrary data to an output section.
1954 void
1955 Output_section::add_output_section_data(Output_section_data* posd)
1957 Input_section inp(posd);
1958 this->add_output_section_data(&inp);
1960 if (posd->is_data_size_valid())
1962 off_t offset_in_section = this->current_data_size_for_child();
1963 off_t aligned_offset_in_section = align_address(offset_in_section,
1964 posd->addralign());
1965 this->set_current_data_size_for_child(aligned_offset_in_section
1966 + posd->data_size());
1970 // Add a relaxed input section.
1972 void
1973 Output_section::add_relaxed_input_section(Output_relaxed_input_section* poris)
1975 Input_section inp(poris);
1976 this->add_output_section_data(&inp);
1977 if (this->is_relaxed_input_section_map_valid_)
1979 Input_section_specifier iss(poris->relobj(), poris->shndx());
1980 this->relaxed_input_section_map_[iss] = poris;
1983 // For a relaxed section, we use the current data size. Linker scripts
1984 // get all the input sections, including relaxed one from an output
1985 // section and add them back to them same output section to compute the
1986 // output section size. If we do not account for sizes of relaxed input
1987 // sections, an output section would be incorrectly sized.
1988 off_t offset_in_section = this->current_data_size_for_child();
1989 off_t aligned_offset_in_section = align_address(offset_in_section,
1990 poris->addralign());
1991 this->set_current_data_size_for_child(aligned_offset_in_section
1992 + poris->current_data_size());
1995 // Add arbitrary data to an output section by Input_section.
1997 void
1998 Output_section::add_output_section_data(Input_section* inp)
2000 if (this->input_sections_.empty())
2001 this->first_input_offset_ = this->current_data_size_for_child();
2003 this->input_sections_.push_back(*inp);
2005 uint64_t addr_align = inp->addralign();
2006 if (addr_align > this->addralign_)
2007 this->addralign_ = addr_align;
2009 inp->set_output_section(this);
2012 // Add a merge section to an output section.
2014 void
2015 Output_section::add_output_merge_section(Output_section_data* posd,
2016 bool is_string, uint64_t ent_size)
2018 Input_section inp(posd, is_string, ent_size);
2019 this->add_output_section_data(&inp);
2022 // Add an input section to a SHF_MERGE section.
2024 bool
2025 Output_section::add_merge_input_section(Relobj* object, unsigned int sec_shndx,
2026 uint64_t aflags, uint64_t ent_size,
2027 uint64_t addr_align)
2029 bool is_string = (aflags & elfcpp::SHF_STRINGS) != 0;
2031 // We only merge strings if the alignment is not more than the
2032 // character size. This could be handled, but it's unusual.
2033 if (is_string && addr_align > ent_size)
2034 return false;
2036 // We cannot restore merged input section states.
2037 gold_assert(this->checkpoint_ == NULL);
2039 // Look up merge sections by required properties.
2040 Merge_section_properties msp(is_string, ent_size, addr_align);
2041 Merge_section_by_properties_map::const_iterator p =
2042 this->merge_section_by_properties_map_.find(msp);
2043 if (p != this->merge_section_by_properties_map_.end())
2045 Output_merge_base* merge_section = p->second;
2046 merge_section->add_input_section(object, sec_shndx);
2047 gold_assert(merge_section->is_string() == is_string
2048 && merge_section->entsize() == ent_size
2049 && merge_section->addralign() == addr_align);
2051 // Link input section to found merge section.
2052 Input_section_specifier iss(object, sec_shndx);
2053 this->merge_section_map_[iss] = merge_section;
2054 return true;
2057 // We handle the actual constant merging in Output_merge_data or
2058 // Output_merge_string_data.
2059 Output_merge_base* pomb;
2060 if (!is_string)
2061 pomb = new Output_merge_data(ent_size, addr_align);
2062 else
2064 switch (ent_size)
2066 case 1:
2067 pomb = new Output_merge_string<char>(addr_align);
2068 break;
2069 case 2:
2070 pomb = new Output_merge_string<uint16_t>(addr_align);
2071 break;
2072 case 4:
2073 pomb = new Output_merge_string<uint32_t>(addr_align);
2074 break;
2075 default:
2076 return false;
2080 // Add new merge section to this output section and link merge section
2081 // properties to new merge section in map.
2082 this->add_output_merge_section(pomb, is_string, ent_size);
2083 this->merge_section_by_properties_map_[msp] = pomb;
2085 // Add input section to new merge section and link input section to new
2086 // merge section in map.
2087 pomb->add_input_section(object, sec_shndx);
2088 Input_section_specifier iss(object, sec_shndx);
2089 this->merge_section_map_[iss] = pomb;
2091 return true;
2094 // Build a relaxation map to speed up relaxation of existing input sections.
2095 // Look up to the first LIMIT elements in INPUTSECTIONS.
2097 void
2098 Output_section::build_relaxation_map(
2099 const Input_section_list& inputsections,
2100 size_t limit,
2101 Relaxation_map* relaxation_map) const
2103 for (size_t i = 0; i < limit; ++i)
2105 const Input_section& is(inputsections[i]);
2106 if (is.is_input_section() || is.is_relaxed_input_section())
2108 Input_section_specifier iss(is.relobj(), is.shndx());
2109 (*relaxation_map)[iss] = i;
2114 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2115 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from input section
2116 // specifier to indices of INPUTSECTIONS.
2118 void
2119 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2120 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2121 const Relaxation_map& map,
2122 Input_section_list* inputsections)
2124 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2126 Output_relaxed_input_section* poris = relaxed_sections[i];
2127 Input_section_specifier iss(poris->relobj(), poris->shndx());
2128 Relaxation_map::const_iterator p = map.find(iss);
2129 gold_assert(p != map.end());
2130 gold_assert((*inputsections)[p->second].is_input_section());
2131 (*inputsections)[p->second] = Input_section(poris);
2135 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2136 // is a vector of pointers to Output_relaxed_input_section or its derived
2137 // classes. The relaxed sections must correspond to existing input sections.
2139 void
2140 Output_section::convert_input_sections_to_relaxed_sections(
2141 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2143 gold_assert(parameters->target().may_relax());
2145 // We want to make sure that restore_states does not undo the effect of
2146 // this. If there is no checkpoint active, just search the current
2147 // input section list and replace the sections there. If there is
2148 // a checkpoint, also replace the sections there.
2150 // By default, we look at the whole list.
2151 size_t limit = this->input_sections_.size();
2153 if (this->checkpoint_ != NULL)
2155 // Replace input sections with relaxed input section in the saved
2156 // copy of the input section list.
2157 if (this->checkpoint_->input_sections_saved())
2159 Relaxation_map map;
2160 this->build_relaxation_map(
2161 *(this->checkpoint_->input_sections()),
2162 this->checkpoint_->input_sections()->size(),
2163 &map);
2164 this->convert_input_sections_in_list_to_relaxed_sections(
2165 relaxed_sections,
2166 map,
2167 this->checkpoint_->input_sections());
2169 else
2171 // We have not copied the input section list yet. Instead, just
2172 // look at the portion that would be saved.
2173 limit = this->checkpoint_->input_sections_size();
2177 // Convert input sections in input_section_list.
2178 Relaxation_map map;
2179 this->build_relaxation_map(this->input_sections_, limit, &map);
2180 this->convert_input_sections_in_list_to_relaxed_sections(
2181 relaxed_sections,
2182 map,
2183 &this->input_sections_);
2186 // Update the output section flags based on input section flags.
2188 void
2189 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword aflags)
2191 // If we created the section with SHF_ALLOC clear, we set the
2192 // address. If we are now setting the SHF_ALLOC flag, we need to
2193 // undo that.
2194 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2195 && (aflags & elfcpp::SHF_ALLOC) != 0)
2196 this->mark_address_invalid();
2198 this->flags_ |= (aflags
2199 & (elfcpp::SHF_WRITE
2200 | elfcpp::SHF_ALLOC
2201 | elfcpp::SHF_EXECINSTR));
2204 // Find the merge section into which an input section with index SEC_SHNDX in
2205 // OBJECT has been added. Return NULL if none found.
2207 Output_section_data*
2208 Output_section::find_merge_section(const Relobj* object,
2209 unsigned int sec_shndx) const
2211 Input_section_specifier iss(object, sec_shndx);
2212 Output_section_data_by_input_section_map::const_iterator p =
2213 this->merge_section_map_.find(iss);
2214 if (p != this->merge_section_map_.end())
2216 Output_section_data* posd = p->second;
2217 gold_assert(posd->is_merge_section_for(object, sec_shndx));
2218 return posd;
2220 else
2221 return NULL;
2224 // Find an relaxed input section corresponding to an input section
2225 // in OBJECT with index SEC_SHNDX.
2227 const Output_section_data*
2228 Output_section::find_relaxed_input_section(const Relobj* object,
2229 unsigned int sec_shndx) const
2231 // Be careful that the map may not be valid due to input section export
2232 // to scripts or a check-point restore.
2233 if (!this->is_relaxed_input_section_map_valid_)
2235 // Rebuild the map as needed.
2236 this->relaxed_input_section_map_.clear();
2237 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2238 p != this->input_sections_.end();
2239 ++p)
2240 if (p->is_relaxed_input_section())
2242 Input_section_specifier iss(p->relobj(), p->shndx());
2243 this->relaxed_input_section_map_[iss] =
2244 p->relaxed_input_section();
2246 this->is_relaxed_input_section_map_valid_ = true;
2249 Input_section_specifier iss(object, sec_shndx);
2250 Output_section_data_by_input_section_map::const_iterator p =
2251 this->relaxed_input_section_map_.find(iss);
2252 if (p != this->relaxed_input_section_map_.end())
2253 return p->second;
2254 else
2255 return NULL;
2258 // Given an address OFF relative to the start of input section
2259 // SEC_SHNDX in OBJECT, return whether this address is being included in
2260 // the final link. This should only be called if SEC_SHNDX in OBJECT has
2261 // a special mapping.
2263 bool
2264 Output_section::is_input_address_mapped(const Relobj* object,
2265 unsigned int sec_shndx,
2266 off_t off) const
2268 // Look at the Output_section_data_maps first.
2269 const Output_section_data* posd = this->find_merge_section(object, sec_shndx);
2270 if (posd == NULL)
2271 posd = this->find_relaxed_input_section(object, sec_shndx);
2273 if (posd != NULL)
2275 section_offset_type outputoffset;
2276 bool found = posd->output_offset(object, sec_shndx, off, &outputoffset);
2277 gold_assert(found);
2278 return outputoffset != -1;
2281 // Fall back to the slow look-up.
2282 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2283 p != this->input_sections_.end();
2284 ++p)
2286 section_offset_type outputoffset;
2287 if (p->output_offset(object, sec_shndx, off, &outputoffset))
2288 return outputoffset != -1;
2291 // By default we assume that the address is mapped. This should
2292 // only be called after we have passed all sections to Layout. At
2293 // that point we should know what we are discarding.
2294 return true;
2297 // Given an address OFF relative to the start of input section
2298 // SEC_SHNDX in object OBJECT, return the output offset relative to the
2299 // start of the input section in the output section. This should only
2300 // be called if SEC_SHNDX in OBJECT has a special mapping.
2302 section_offset_type
2303 Output_section::output_offset(const Relobj* object, unsigned int sec_shndx,
2304 section_offset_type off) const
2306 // This can only be called meaningfully when we know the data size
2307 // of this.
2308 gold_assert(this->is_data_size_valid());
2310 // Look at the Output_section_data_maps first.
2311 const Output_section_data* posd = this->find_merge_section(object, sec_shndx);
2312 if (posd == NULL)
2313 posd = this->find_relaxed_input_section(object, sec_shndx);
2314 if (posd != NULL)
2316 section_offset_type outputoffset;
2317 bool found = posd->output_offset(object, sec_shndx, off, &outputoffset);
2318 gold_assert(found);
2319 return outputoffset;
2322 // Fall back to the slow look-up.
2323 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2324 p != this->input_sections_.end();
2325 ++p)
2327 section_offset_type outputoffset;
2328 if (p->output_offset(object, sec_shndx, off, &outputoffset))
2329 return outputoffset;
2331 gold_unreachable();
2334 // Return the output virtual address of OFF relative to the start
2335 // of input section SEC_SHNDX in object OBJECT.
2337 uint64_t
2338 Output_section::output_address(const Relobj* object, unsigned int sec_shndx,
2339 off_t off) const
2341 uint64_t addr = this->address() + this->first_input_offset_;
2343 // Look at the Output_section_data_maps first.
2344 const Output_section_data* posd = this->find_merge_section(object, sec_shndx);
2345 if (posd == NULL)
2346 posd = this->find_relaxed_input_section(object, sec_shndx);
2347 if (posd != NULL && posd->is_address_valid())
2349 section_offset_type outputoffset;
2350 bool found = posd->output_offset(object, sec_shndx, off, &outputoffset);
2351 gold_assert(found);
2352 return posd->address() + outputoffset;
2355 // Fall back to the slow look-up.
2356 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2357 p != this->input_sections_.end();
2358 ++p)
2360 addr = align_address(addr, p->addralign());
2361 section_offset_type outputoffset;
2362 if (p->output_offset(object, sec_shndx, off, &outputoffset))
2364 if (outputoffset == -1)
2365 return -1ULL;
2366 return addr + outputoffset;
2368 addr += p->data_size();
2371 // If we get here, it means that we don't know the mapping for this
2372 // input section. This might happen in principle if
2373 // add_input_section were called before add_output_section_data.
2374 // But it should never actually happen.
2376 gold_unreachable();
2379 // Find the output address of the start of the merged section for
2380 // input section SEC_SHNDX in object OBJECT.
2382 bool
2383 Output_section::find_starting_output_address(const Relobj* object,
2384 unsigned int sec_shndx,
2385 uint64_t* paddr) const
2387 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2388 // Looking up the merge section map does not always work as we sometimes
2389 // find a merge section without its address set.
2390 uint64_t addr = this->address() + this->first_input_offset_;
2391 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2392 p != this->input_sections_.end();
2393 ++p)
2395 addr = align_address(addr, p->addralign());
2397 // It would be nice if we could use the existing output_offset
2398 // method to get the output offset of input offset 0.
2399 // Unfortunately we don't know for sure that input offset 0 is
2400 // mapped at all.
2401 if (p->is_merge_section_for(object, sec_shndx))
2403 *paddr = addr;
2404 return true;
2407 addr += p->data_size();
2410 // We couldn't find a merge output section for this input section.
2411 return false;
2414 // Set the data size of an Output_section. This is where we handle
2415 // setting the addresses of any Output_section_data objects.
2417 void
2418 Output_section::set_final_data_size()
2420 if (this->input_sections_.empty())
2422 this->set_data_size(this->current_data_size_for_child());
2423 return;
2426 if (this->must_sort_attached_input_sections())
2427 this->sort_attached_input_sections();
2429 uint64_t addr = this->address();
2430 off_t startoff = this->offset();
2431 off_t off = startoff + this->first_input_offset_;
2432 for (Input_section_list::iterator p = this->input_sections_.begin();
2433 p != this->input_sections_.end();
2434 ++p)
2436 off = align_address(off, p->addralign());
2437 p->set_address_and_file_offset(addr + (off - startoff), off,
2438 startoff);
2439 off += p->data_size();
2442 this->set_data_size(off - startoff);
2445 // Reset the address and file offset.
2447 void
2448 Output_section::do_reset_address_and_file_offset()
2450 // An unallocated section has no address. Forcing this means that
2451 // we don't need special treatment for symbols defined in debug
2452 // sections. We do the same in the constructor.
2453 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2454 this->set_address(0);
2456 for (Input_section_list::iterator p = this->input_sections_.begin();
2457 p != this->input_sections_.end();
2458 ++p)
2459 p->reset_address_and_file_offset();
2462 // Return true if address and file offset have the values after reset.
2464 bool
2465 Output_section::do_address_and_file_offset_have_reset_values() const
2467 if (this->is_offset_valid())
2468 return false;
2470 // An unallocated section has address 0 after its construction or a reset.
2471 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2472 return this->is_address_valid() && this->address() == 0;
2473 else
2474 return !this->is_address_valid();
2477 // Set the TLS offset. Called only for SHT_TLS sections.
2479 void
2480 Output_section::do_set_tls_offset(uint64_t tls_base)
2482 this->tls_offset_ = this->address() - tls_base;
2485 // In a few cases we need to sort the input sections attached to an
2486 // output section. This is used to implement the type of constructor
2487 // priority ordering implemented by the GNU linker, in which the
2488 // priority becomes part of the section name and the sections are
2489 // sorted by name. We only do this for an output section if we see an
2490 // attached input section matching ".ctor.*", ".dtor.*",
2491 // ".init_array.*" or ".fini_array.*".
2493 class Output_section::Input_section_sort_entry
2495 public:
2496 Input_section_sort_entry()
2497 : input_section_(), index_(-1U), section_has_name_(false),
2498 section_name_()
2501 Input_section_sort_entry(const Input_section& inputsection,
2502 unsigned int indx)
2503 : input_section_(inputsection), index_(indx),
2504 section_has_name_(inputsection.is_input_section()
2505 || inputsection.is_relaxed_input_section())
2507 if (this->section_has_name_)
2509 // This is only called single-threaded from Layout::finalize,
2510 // so it is OK to lock. Unfortunately we have no way to pass
2511 // in a Task token.
2512 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2513 Object* obj = (inputsection.is_input_section()
2514 ? inputsection.relobj()
2515 : inputsection.relaxed_input_section()->relobj());
2516 Task_lock_obj<Object> tl(dummy_task, obj);
2518 // This is a slow operation, which should be cached in
2519 // Layout::layout if this becomes a speed problem.
2520 this->section_name_ = obj->section_name(inputsection.shndx());
2524 // Return the Input_section.
2525 const Input_section&
2526 input_section() const
2528 gold_assert(this->index_ != -1U);
2529 return this->input_section_;
2532 // The index of this entry in the original list. This is used to
2533 // make the sort stable.
2534 unsigned int
2535 index() const
2537 gold_assert(this->index_ != -1U);
2538 return this->index_;
2541 // Whether there is a section name.
2542 bool
2543 section_has_name() const
2544 { return this->section_has_name_; }
2546 // The section name.
2547 const std::string&
2548 section_name() const
2550 gold_assert(this->section_has_name_);
2551 return this->section_name_;
2554 // Return true if the section name has a priority. This is assumed
2555 // to be true if it has a dot after the initial dot.
2556 bool
2557 has_priority() const
2559 gold_assert(this->section_has_name_);
2560 return this->section_name_.find('.', 1);
2563 // Return true if this an input file whose base name matches
2564 // FILE_NAME. The base name must have an extension of ".o", and
2565 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2566 // This is to match crtbegin.o as well as crtbeginS.o without
2567 // getting confused by other possibilities. Overall matching the
2568 // file name this way is a dreadful hack, but the GNU linker does it
2569 // in order to better support gcc, and we need to be compatible.
2570 bool
2571 match_file_name(const char* match_filename) const
2573 const std::string& file_name(this->input_section_.relobj()->name());
2574 const char* base_name = lbasename(file_name.c_str());
2575 size_t match_len = strlen(match_filename);
2576 if (strncmp(base_name, match_filename, match_len) != 0)
2577 return false;
2578 size_t base_len = strlen(base_name);
2579 if (base_len != match_len + 2 && base_len != match_len + 3)
2580 return false;
2581 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2584 private:
2585 // The Input_section we are sorting.
2586 Input_section input_section_;
2587 // The index of this Input_section in the original list.
2588 unsigned int index_;
2589 // Whether this Input_section has a section name--it won't if this
2590 // is some random Output_section_data.
2591 bool section_has_name_;
2592 // The section name if there is one.
2593 std::string section_name_;
2596 // Return true if S1 should come before S2 in the output section.
2598 bool
2599 Output_section::Input_section_sort_compare::operator()(
2600 const Output_section::Input_section_sort_entry& s1,
2601 const Output_section::Input_section_sort_entry& s2) const
2603 // crtbegin.o must come first.
2604 bool s1_begin = s1.match_file_name("crtbegin");
2605 bool s2_begin = s2.match_file_name("crtbegin");
2606 if (s1_begin || s2_begin)
2608 if (!s1_begin)
2609 return false;
2610 if (!s2_begin)
2611 return true;
2612 return s1.index() < s2.index();
2615 // crtend.o must come last.
2616 bool s1_end = s1.match_file_name("crtend");
2617 bool s2_end = s2.match_file_name("crtend");
2618 if (s1_end || s2_end)
2620 if (!s1_end)
2621 return true;
2622 if (!s2_end)
2623 return false;
2624 return s1.index() < s2.index();
2627 // We sort all the sections with no names to the end.
2628 if (!s1.section_has_name() || !s2.section_has_name())
2630 if (s1.section_has_name())
2631 return true;
2632 if (s2.section_has_name())
2633 return false;
2634 return s1.index() < s2.index();
2637 // A section with a priority follows a section without a priority.
2638 // The GNU linker does this for all but .init_array sections; until
2639 // further notice we'll assume that that is an mistake.
2640 bool s1_has_priority = s1.has_priority();
2641 bool s2_has_priority = s2.has_priority();
2642 if (s1_has_priority && !s2_has_priority)
2643 return false;
2644 if (!s1_has_priority && s2_has_priority)
2645 return true;
2647 // Otherwise we sort by name.
2648 int compare = s1.section_name().compare(s2.section_name());
2649 if (compare != 0)
2650 return compare < 0;
2652 // Otherwise we keep the input order.
2653 return s1.index() < s2.index();
2656 // Sort the input sections attached to an output section.
2658 void
2659 Output_section::sort_attached_input_sections()
2661 if (this->attached_input_sections_are_sorted_)
2662 return;
2664 if (this->checkpoint_ != NULL
2665 && !this->checkpoint_->input_sections_saved())
2666 this->checkpoint_->save_input_sections();
2668 // The only thing we know about an input section is the object and
2669 // the section index. We need the section name. Recomputing this
2670 // is slow but this is an unusual case. If this becomes a speed
2671 // problem we can cache the names as required in Layout::layout.
2673 // We start by building a larger vector holding a copy of each
2674 // Input_section, plus its current index in the list and its name.
2675 std::vector<Input_section_sort_entry> sort_list;
2677 unsigned int i = 0;
2678 for (Input_section_list::iterator p = this->input_sections_.begin();
2679 p != this->input_sections_.end();
2680 ++p, ++i)
2681 sort_list.push_back(Input_section_sort_entry(*p, i));
2683 // Sort the input sections.
2684 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2686 // Copy the sorted input sections back to our list.
2687 this->input_sections_.clear();
2688 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2689 p != sort_list.end();
2690 ++p)
2691 this->input_sections_.push_back(p->input_section());
2693 // Remember that we sorted the input sections, since we might get
2694 // called again.
2695 this->attached_input_sections_are_sorted_ = true;
2698 // Write the section header to *OSHDR.
2700 template<int size, bool big_endian>
2701 void
2702 Output_section::write_header(const Layout* layout,
2703 const Stringpool* secnamepool,
2704 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2706 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2707 oshdr->put_sh_type(this->type_);
2709 elfcpp::Elf_Xword xflags = this->flags_;
2710 if (this->info_section_ != NULL && this->info_uses_section_index_)
2711 xflags |= elfcpp::SHF_INFO_LINK;
2712 oshdr->put_sh_flags(xflags);
2714 oshdr->put_sh_addr(this->address());
2715 oshdr->put_sh_offset(this->offset());
2716 oshdr->put_sh_size(this->data_size());
2717 if (this->link_section_ != NULL)
2718 oshdr->put_sh_link(this->link_section_->out_shndx());
2719 else if (this->should_link_to_symtab_)
2720 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2721 else if (this->should_link_to_dynsym_)
2722 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2723 else
2724 oshdr->put_sh_link(this->link_);
2726 elfcpp::Elf_Word inf;
2727 if (this->info_section_ != NULL)
2729 if (this->info_uses_section_index_)
2730 inf = this->info_section_->out_shndx();
2731 else
2732 inf = this->info_section_->symtab_index();
2734 else if (this->info_symndx_ != NULL)
2735 inf = this->info_symndx_->symtab_index();
2736 else
2737 inf = this->info_;
2738 oshdr->put_sh_info(inf);
2740 oshdr->put_sh_addralign(this->addralign_);
2741 oshdr->put_sh_entsize(this->entsize_);
2744 // Write out the data. For input sections the data is written out by
2745 // Object::relocate, but we have to handle Output_section_data objects
2746 // here.
2748 void
2749 Output_section::do_write(Output_file* of)
2751 gold_assert(!this->requires_postprocessing());
2753 // If the target performs relaxation, we delay filler generation until now.
2754 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
2756 off_t output_section_file_offset = this->offset();
2757 for (Fill_list::iterator p = this->fills_.begin();
2758 p != this->fills_.end();
2759 ++p)
2761 std::string fill_data(parameters->target().code_fill(p->length()));
2762 of->write(output_section_file_offset + p->section_offset(),
2763 fill_data.data(), fill_data.size());
2766 off_t off = this->offset() + this->first_input_offset_;
2767 for (Input_section_list::iterator p = this->input_sections_.begin();
2768 p != this->input_sections_.end();
2769 ++p)
2771 off_t aligned_off = align_address(off, p->addralign());
2772 if (this->generate_code_fills_at_write_ && (off != aligned_off))
2774 size_t fill_len = aligned_off - off;
2775 std::string fill_data(parameters->target().code_fill(fill_len));
2776 of->write(off, fill_data.data(), fill_data.size());
2779 p->write(of);
2780 off = aligned_off + p->data_size();
2784 // If a section requires postprocessing, create the buffer to use.
2786 void
2787 Output_section::create_postprocessing_buffer()
2789 gold_assert(this->requires_postprocessing());
2791 if (this->postprocessing_buffer_ != NULL)
2792 return;
2794 if (!this->input_sections_.empty())
2796 off_t off = this->first_input_offset_;
2797 for (Input_section_list::iterator p = this->input_sections_.begin();
2798 p != this->input_sections_.end();
2799 ++p)
2801 off = align_address(off, p->addralign());
2802 p->finalize_data_size();
2803 off += p->data_size();
2805 this->set_current_data_size_for_child(off);
2808 off_t buffer_size = this->current_data_size_for_child();
2809 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2812 // Write all the data of an Output_section into the postprocessing
2813 // buffer. This is used for sections which require postprocessing,
2814 // such as compression. Input sections are handled by
2815 // Object::Relocate.
2817 void
2818 Output_section::write_to_postprocessing_buffer()
2820 gold_assert(this->requires_postprocessing());
2822 // If the target performs relaxation, we delay filler generation until now.
2823 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
2825 unsigned char* buffer = this->postprocessing_buffer();
2826 for (Fill_list::iterator p = this->fills_.begin();
2827 p != this->fills_.end();
2828 ++p)
2830 std::string fill_data(parameters->target().code_fill(p->length()));
2831 memcpy(buffer + p->section_offset(), fill_data.data(),
2832 fill_data.size());
2835 off_t off = this->first_input_offset_;
2836 for (Input_section_list::iterator p = this->input_sections_.begin();
2837 p != this->input_sections_.end();
2838 ++p)
2840 off_t aligned_off = align_address(off, p->addralign());
2841 if (this->generate_code_fills_at_write_ && (off != aligned_off))
2843 size_t fill_len = aligned_off - off;
2844 std::string fill_data(parameters->target().code_fill(fill_len));
2845 memcpy(buffer + off, fill_data.data(), fill_data.size());
2848 p->write_to_buffer(buffer + aligned_off);
2849 off = aligned_off + p->data_size();
2853 // Get the input sections for linker script processing. We leave
2854 // behind the Output_section_data entries. Note that this may be
2855 // slightly incorrect for merge sections. We will leave them behind,
2856 // but it is possible that the script says that they should follow
2857 // some other input sections, as in:
2858 // .rodata { *(.rodata) *(.rodata.cst*) }
2859 // For that matter, we don't handle this correctly:
2860 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2861 // With luck this will never matter.
2863 uint64_t
2864 Output_section::get_input_sections(
2865 uint64_t addr,
2866 const std::string& fill,
2867 std::list<Simple_input_section>* inputsections)
2869 if (this->checkpoint_ != NULL
2870 && !this->checkpoint_->input_sections_saved())
2871 this->checkpoint_->save_input_sections();
2873 // Invalidate the relaxed input section map.
2874 this->is_relaxed_input_section_map_valid_ = false;
2876 uint64_t orig_address = addr;
2878 addr = align_address(addr, this->addralign());
2880 Input_section_list remaining;
2881 for (Input_section_list::iterator p = this->input_sections_.begin();
2882 p != this->input_sections_.end();
2883 ++p)
2885 if (p->is_input_section())
2886 inputsections->push_back(Simple_input_section(p->relobj(),
2887 p->shndx()));
2888 else if (p->is_relaxed_input_section())
2889 inputsections->push_back(
2890 Simple_input_section(p->relaxed_input_section()));
2891 else
2893 uint64_t aligned_address = align_address(addr, p->addralign());
2894 if (aligned_address != addr && !fill.empty())
2896 section_size_type length =
2897 convert_to_section_size_type(aligned_address - addr);
2898 std::string this_fill;
2899 this_fill.reserve(length);
2900 while (this_fill.length() + fill.length() <= length)
2901 this_fill += fill;
2902 if (this_fill.length() < length)
2903 this_fill.append(fill, 0, length - this_fill.length());
2905 Output_section_data* posd = new Output_data_const(this_fill, 0);
2906 remaining.push_back(Input_section(posd));
2908 addr = aligned_address;
2910 remaining.push_back(*p);
2912 p->finalize_data_size();
2913 addr += p->data_size();
2917 this->input_sections_.swap(remaining);
2918 this->first_input_offset_ = 0;
2920 uint64_t datasize = addr - orig_address;
2921 this->set_current_data_size_for_child(datasize);
2922 return datasize;
2925 // Add an input section from a script.
2927 void
2928 Output_section::add_input_section_for_script(const Simple_input_section& sis,
2929 off_t datasize,
2930 uint64_t addr_align)
2932 if (addr_align > this->addralign_)
2933 this->addralign_ = addr_align;
2935 off_t offset_in_section = this->current_data_size_for_child();
2936 off_t aligned_offset_in_section = align_address(offset_in_section,
2937 addr_align);
2939 this->set_current_data_size_for_child(aligned_offset_in_section
2940 + datasize);
2942 Input_section is =
2943 (sis.is_relaxed_input_section()
2944 ? Input_section(sis.relaxed_input_section())
2945 : Input_section(sis.relobj(), sis.shndx(), datasize, addr_align));
2946 this->input_sections_.push_back(is);
2951 void
2952 Output_section::save_states()
2954 gold_assert(this->checkpoint_ == NULL);
2955 Checkpoint_output_section* checkpoint =
2956 new Checkpoint_output_section(this->addralign_, this->flags_,
2957 this->input_sections_,
2958 this->first_input_offset_,
2959 this->attached_input_sections_are_sorted_);
2960 this->checkpoint_ = checkpoint;
2961 gold_assert(this->fills_.empty());
2964 void
2965 Output_section::restore_states()
2967 gold_assert(this->checkpoint_ != NULL);
2968 Checkpoint_output_section* checkpoint = this->checkpoint_;
2970 this->addralign_ = checkpoint->addralign();
2971 this->flags_ = checkpoint->flags();
2972 this->first_input_offset_ = checkpoint->first_input_offset();
2974 if (!checkpoint->input_sections_saved())
2976 // If we have not copied the input sections, just resize it.
2977 size_t old_size = checkpoint->input_sections_size();
2978 gold_assert(this->input_sections_.size() >= old_size);
2979 this->input_sections_.resize(old_size);
2981 else
2983 // We need to copy the whole list. This is not efficient for
2984 // extremely large output with hundreads of thousands of input
2985 // objects. We may need to re-think how we should pass sections
2986 // to scripts.
2987 this->input_sections_ = *checkpoint->input_sections();
2990 this->attached_input_sections_are_sorted_ =
2991 checkpoint->attached_input_sections_are_sorted();
2993 // Simply invalidate the relaxed input section map since we do not keep
2994 // track of it.
2995 this->is_relaxed_input_section_map_valid_ = false;
2998 // Print to the map file.
3000 void
3001 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3003 mapfile->print_output_section(this);
3005 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3006 p != this->input_sections_.end();
3007 ++p)
3008 p->print_to_mapfile(mapfile);
3011 // Print stats for merge sections to stderr.
3013 void
3014 Output_section::print_merge_stats()
3016 Input_section_list::iterator p;
3017 for (p = this->input_sections_.begin();
3018 p != this->input_sections_.end();
3019 ++p)
3020 p->print_merge_stats(this->name_);
3023 // Output segment methods.
3025 Output_segment::Output_segment(elfcpp::Elf_Word atype, elfcpp::Elf_Word aflags)
3026 : output_data_(),
3027 output_bss_(),
3028 vaddr_(0),
3029 paddr_(0),
3030 memsz_(0),
3031 max_align_(0),
3032 min_p_align_(0),
3033 offset_(0),
3034 filesz_(0),
3035 type_(atype),
3036 flags_(aflags),
3037 is_max_align_known_(false),
3038 are_addresses_set_(false),
3039 is_large_data_segment_(false)
3043 // Add an Output_section to an Output_segment.
3045 void
3046 Output_segment::add_output_section(Output_section* os,
3047 elfcpp::Elf_Word seg_flags,
3048 bool do_sort)
3050 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3051 gold_assert(!this->is_max_align_known_);
3052 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3053 gold_assert(this->type() == elfcpp::PT_LOAD || !do_sort);
3055 // Update the segment flags.
3056 this->flags_ |= seg_flags;
3058 Output_segment::Output_data_list* pdl;
3059 if (os->type() == elfcpp::SHT_NOBITS)
3060 pdl = &this->output_bss_;
3061 else
3062 pdl = &this->output_data_;
3064 // Note that while there may be many input sections in an output
3065 // section, there are normally only a few output sections in an
3066 // output segment. The loops below are expected to be fast.
3068 // So that PT_NOTE segments will work correctly, we need to ensure
3069 // that all SHT_NOTE sections are adjacent.
3070 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
3072 Output_segment::Output_data_list::iterator p = pdl->end();
3075 --p;
3076 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
3078 ++p;
3079 pdl->insert(p, os);
3080 return;
3083 while (p != pdl->begin());
3086 // Similarly, so that PT_TLS segments will work, we need to group
3087 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
3088 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
3089 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
3090 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
3091 // and the PT_TLS segment; we do this grouping only for the PT_LOAD
3092 // segment.
3093 if (this->type_ != elfcpp::PT_TLS
3094 && (os->flags() & elfcpp::SHF_TLS) != 0)
3096 pdl = &this->output_data_;
3097 if (!pdl->empty())
3099 bool nobits = os->type() == elfcpp::SHT_NOBITS;
3100 bool sawtls = false;
3101 Output_segment::Output_data_list::iterator p = pdl->end();
3102 gold_assert(p != pdl->begin());
3105 --p;
3106 bool insert;
3107 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3109 sawtls = true;
3110 // Put a NOBITS section after the first TLS section.
3111 // Put a PROGBITS section after the first
3112 // TLS/PROGBITS section.
3113 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
3115 else
3117 // If we've gone past the TLS sections, but we've
3118 // seen a TLS section, then we need to insert this
3119 // section now.
3120 insert = sawtls;
3123 if (insert)
3125 ++p;
3126 pdl->insert(p, os);
3127 return;
3130 while (p != pdl->begin());
3133 // There are no TLS sections yet; put this one at the requested
3134 // location in the section list.
3137 // For the PT_GNU_RELRO segment, we need to group relro sections,
3138 // and we need to put them before any non-relro sections. Also,
3139 // relro local sections go before relro non-local sections.
3140 if (parameters->options().relro() && os->is_relro())
3142 gold_assert(pdl == &this->output_data_);
3143 Output_segment::Output_data_list::iterator p;
3144 for (p = pdl->begin(); p != pdl->end(); ++p)
3146 if (!(*p)->is_section())
3147 break;
3149 Output_section* pos = (*p)->output_section();
3150 if (!pos->is_relro()
3151 || (os->is_relro_local() && !pos->is_relro_local()))
3152 break;
3155 pdl->insert(p, os);
3156 return;
3159 // Small data sections go at the end of the list of data sections.
3160 // If OS is not small, and there are small sections, we have to
3161 // insert it before the first small section.
3162 if (os->type() != elfcpp::SHT_NOBITS
3163 && !os->is_small_section()
3164 && !pdl->empty()
3165 && pdl->back()->is_section()
3166 && pdl->back()->output_section()->is_small_section())
3168 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3169 p != pdl->end();
3170 ++p)
3172 if ((*p)->is_section()
3173 && (*p)->output_section()->is_small_section())
3175 pdl->insert(p, os);
3176 return;
3179 gold_unreachable();
3182 // A small BSS section goes at the start of the BSS sections, after
3183 // other small BSS sections.
3184 if (os->type() == elfcpp::SHT_NOBITS && os->is_small_section())
3186 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3187 p != pdl->end();
3188 ++p)
3190 if (!(*p)->is_section()
3191 || !(*p)->output_section()->is_small_section())
3193 pdl->insert(p, os);
3194 return;
3199 // A large BSS section goes at the end of the BSS sections, which
3200 // means that one that is not large must come before the first large
3201 // one.
3202 if (os->type() == elfcpp::SHT_NOBITS
3203 && !os->is_large_section()
3204 && !pdl->empty()
3205 && pdl->back()->is_section()
3206 && pdl->back()->output_section()->is_large_section())
3208 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3209 p != pdl->end();
3210 ++p)
3212 if ((*p)->is_section()
3213 && (*p)->output_section()->is_large_section())
3215 pdl->insert(p, os);
3216 return;
3219 gold_unreachable();
3222 // We do some further output section sorting in order to make the
3223 // generated program run more efficiently. We should only do this
3224 // when not using a linker script, so it is controled by the DO_SORT
3225 // parameter.
3226 if (do_sort)
3228 // FreeBSD requires the .interp section to be in the first page
3229 // of the executable. That is a more efficient location anyhow
3230 // for any OS, since it means that the kernel will have the data
3231 // handy after it reads the program headers.
3232 if (os->is_interp() && !pdl->empty())
3234 pdl->insert(pdl->begin(), os);
3235 return;
3238 // Put loadable non-writable notes immediately after the .interp
3239 // sections, so that the PT_NOTE segment is on the first page of
3240 // the executable.
3241 if (os->type() == elfcpp::SHT_NOTE
3242 && (os->flags() & elfcpp::SHF_WRITE) == 0
3243 && !pdl->empty())
3245 Output_segment::Output_data_list::iterator p = pdl->begin();
3246 if ((*p)->is_section() && (*p)->output_section()->is_interp())
3247 ++p;
3248 pdl->insert(p, os);
3249 return;
3252 // If this section is used by the dynamic linker, and it is not
3253 // writable, then put it first, after the .interp section and
3254 // any loadable notes. This makes it more likely that the
3255 // dynamic linker will have to read less data from the disk.
3256 if (os->is_dynamic_linker_section()
3257 && !pdl->empty()
3258 && (os->flags() & elfcpp::SHF_WRITE) == 0)
3260 bool is_reloc = (os->type() == elfcpp::SHT_REL
3261 || os->type() == elfcpp::SHT_RELA);
3262 Output_segment::Output_data_list::iterator p = pdl->begin();
3263 while (p != pdl->end()
3264 && (*p)->is_section()
3265 && ((*p)->output_section()->is_dynamic_linker_section()
3266 || (*p)->output_section()->type() == elfcpp::SHT_NOTE))
3268 // Put reloc sections after the other ones. Putting the
3269 // dynamic reloc sections first confuses BFD, notably
3270 // objcopy and strip.
3271 if (!is_reloc
3272 && ((*p)->output_section()->type() == elfcpp::SHT_REL
3273 || (*p)->output_section()->type() == elfcpp::SHT_RELA))
3274 break;
3275 ++p;
3277 pdl->insert(p, os);
3278 return;
3282 // If there were no constraints on the output section, just add it
3283 // to the end of the list.
3284 pdl->push_back(os);
3287 // Remove an Output_section from this segment. It is an error if it
3288 // is not present.
3290 void
3291 Output_segment::remove_output_section(Output_section* os)
3293 // We only need this for SHT_PROGBITS.
3294 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
3295 for (Output_data_list::iterator p = this->output_data_.begin();
3296 p != this->output_data_.end();
3297 ++p)
3299 if (*p == os)
3301 this->output_data_.erase(p);
3302 return;
3305 gold_unreachable();
3308 // Add an Output_data (which is not an Output_section) to the start of
3309 // a segment.
3311 void
3312 Output_segment::add_initial_output_data(Output_data* od)
3314 gold_assert(!this->is_max_align_known_);
3315 this->output_data_.push_front(od);
3318 // Return whether the first data section is a relro section.
3320 bool
3321 Output_segment::is_first_section_relro() const
3323 return (!this->output_data_.empty()
3324 && this->output_data_.front()->is_section()
3325 && this->output_data_.front()->output_section()->is_relro());
3328 // Return the maximum alignment of the Output_data in Output_segment.
3330 uint64_t
3331 Output_segment::maximum_alignment()
3333 if (!this->is_max_align_known_)
3335 uint64_t addr_align;
3337 addr_align = Output_segment::maximum_alignment_list(&this->output_data_);
3338 if (addr_align > this->max_align_)
3339 this->max_align_ = addr_align;
3341 addr_align = Output_segment::maximum_alignment_list(&this->output_bss_);
3342 if (addr_align > this->max_align_)
3343 this->max_align_ = addr_align;
3345 // If -z relro is in effect, and the first section in this
3346 // segment is a relro section, then the segment must be aligned
3347 // to at least the common page size. This ensures that the
3348 // PT_GNU_RELRO segment will start at a page boundary.
3349 if (this->type_ == elfcpp::PT_LOAD
3350 && parameters->options().relro()
3351 && this->is_first_section_relro())
3353 addr_align = parameters->target().common_pagesize();
3354 if (addr_align > this->max_align_)
3355 this->max_align_ = addr_align;
3358 this->is_max_align_known_ = true;
3361 return this->max_align_;
3364 // Return the maximum alignment of a list of Output_data.
3366 uint64_t
3367 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
3369 uint64_t ret = 0;
3370 for (Output_data_list::const_iterator p = pdl->begin();
3371 p != pdl->end();
3372 ++p)
3374 uint64_t addr_align = (*p)->addralign();
3375 if (addr_align > ret)
3376 ret = addr_align;
3378 return ret;
3381 // Return the number of dynamic relocs applied to this segment.
3383 unsigned int
3384 Output_segment::dynamic_reloc_count() const
3386 return (this->dynamic_reloc_count_list(&this->output_data_)
3387 + this->dynamic_reloc_count_list(&this->output_bss_));
3390 // Return the number of dynamic relocs applied to an Output_data_list.
3392 unsigned int
3393 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
3395 unsigned int count = 0;
3396 for (Output_data_list::const_iterator p = pdl->begin();
3397 p != pdl->end();
3398 ++p)
3399 count += (*p)->dynamic_reloc_count();
3400 return count;
3403 // Set the section addresses for an Output_segment. If RESET is true,
3404 // reset the addresses first. ADDR is the address and *POFF is the
3405 // file offset. Set the section indexes starting with *PSHNDX.
3406 // Return the address of the immediately following segment. Update
3407 // *POFF and *PSHNDX.
3409 uint64_t
3410 Output_segment::set_section_addresses(const Layout* layout, bool reset,
3411 uint64_t addr, off_t* poff,
3412 unsigned int* pshndx)
3414 gold_assert(this->type_ == elfcpp::PT_LOAD);
3416 if (!reset && this->are_addresses_set_)
3418 gold_assert(this->paddr_ == addr);
3419 addr = this->vaddr_;
3421 else
3423 this->vaddr_ = addr;
3424 this->paddr_ = addr;
3425 this->are_addresses_set_ = true;
3428 bool in_tls = false;
3430 bool in_relro = (parameters->options().relro()
3431 && this->is_first_section_relro());
3433 off_t orig_off = *poff;
3434 this->offset_ = orig_off;
3436 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
3437 addr, poff, pshndx, &in_tls,
3438 &in_relro);
3439 this->filesz_ = *poff - orig_off;
3441 off_t off = *poff;
3443 uint64_t ret = this->set_section_list_addresses(layout, reset,
3444 &this->output_bss_,
3445 addr, poff, pshndx,
3446 &in_tls, &in_relro);
3448 // If the last section was a TLS section, align upward to the
3449 // alignment of the TLS segment, so that the overall size of the TLS
3450 // segment is aligned.
3451 if (in_tls)
3453 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
3454 *poff = align_address(*poff, segment_align);
3457 // If all the sections were relro sections, align upward to the
3458 // common page size.
3459 if (in_relro)
3461 uint64_t page_align = parameters->target().common_pagesize();
3462 *poff = align_address(*poff, page_align);
3465 this->memsz_ = *poff - orig_off;
3467 // Ignore the file offset adjustments made by the BSS Output_data
3468 // objects.
3469 *poff = off;
3471 return ret;
3474 // Set the addresses and file offsets in a list of Output_data
3475 // structures.
3477 uint64_t
3478 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
3479 Output_data_list* pdl,
3480 uint64_t addr, off_t* poff,
3481 unsigned int* pshndx,
3482 bool* in_tls, bool* in_relro)
3484 off_t startoff = *poff;
3486 off_t off = startoff;
3487 for (Output_data_list::iterator p = pdl->begin();
3488 p != pdl->end();
3489 ++p)
3491 if (reset)
3492 (*p)->reset_address_and_file_offset();
3494 // When using a linker script the section will most likely
3495 // already have an address.
3496 if (!(*p)->is_address_valid())
3498 uint64_t align = (*p)->addralign();
3500 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3502 // Give the first TLS section the alignment of the
3503 // entire TLS segment. Otherwise the TLS segment as a
3504 // whole may be misaligned.
3505 if (!*in_tls)
3507 Output_segment* tls_segment = layout->tls_segment();
3508 gold_assert(tls_segment != NULL);
3509 uint64_t segment_align = tls_segment->maximum_alignment();
3510 gold_assert(segment_align >= align);
3511 align = segment_align;
3513 *in_tls = true;
3516 else
3518 // If this is the first section after the TLS segment,
3519 // align it to at least the alignment of the TLS
3520 // segment, so that the size of the overall TLS segment
3521 // is aligned.
3522 if (*in_tls)
3524 uint64_t segment_align =
3525 layout->tls_segment()->maximum_alignment();
3526 if (segment_align > align)
3527 align = segment_align;
3529 *in_tls = false;
3533 // If this is a non-relro section after a relro section,
3534 // align it to a common page boundary so that the dynamic
3535 // linker has a page to mark as read-only.
3536 if (*in_relro
3537 && (!(*p)->is_section()
3538 || !(*p)->output_section()->is_relro()))
3540 uint64_t page_align = parameters->target().common_pagesize();
3541 if (page_align > align)
3542 align = page_align;
3543 *in_relro = false;
3546 off = align_address(off, align);
3547 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
3549 else
3551 // The script may have inserted a skip forward, but it
3552 // better not have moved backward.
3553 if ((*p)->address() >= addr + (off - startoff))
3554 off += (*p)->address() - (addr + (off - startoff));
3555 else
3557 if (!layout->script_options()->saw_sections_clause())
3558 gold_unreachable();
3559 else
3561 Output_section* os = (*p)->output_section();
3563 // Cast to unsigned long long to avoid format warnings.
3564 unsigned long long previous_dot =
3565 static_cast<unsigned long long>(addr + (off - startoff));
3566 unsigned long long dot =
3567 static_cast<unsigned long long>((*p)->address());
3569 if (os == NULL)
3570 gold_error(_("dot moves backward in linker script "
3571 "from 0x%llx to 0x%llx"), previous_dot, dot);
3572 else
3573 gold_error(_("address of section '%s' moves backward "
3574 "from 0x%llx to 0x%llx"),
3575 os->name(), previous_dot, dot);
3578 (*p)->set_file_offset(off);
3579 (*p)->finalize_data_size();
3582 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
3583 // section. Such a section does not affect the size of a
3584 // PT_LOAD segment.
3585 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
3586 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
3587 off += (*p)->data_size();
3589 if ((*p)->is_section())
3591 (*p)->set_out_shndx(*pshndx);
3592 ++*pshndx;
3596 *poff = off;
3597 return addr + (off - startoff);
3600 // For a non-PT_LOAD segment, set the offset from the sections, if
3601 // any.
3603 void
3604 Output_segment::set_offset()
3606 gold_assert(this->type_ != elfcpp::PT_LOAD);
3608 gold_assert(!this->are_addresses_set_);
3610 if (this->output_data_.empty() && this->output_bss_.empty())
3612 this->vaddr_ = 0;
3613 this->paddr_ = 0;
3614 this->are_addresses_set_ = true;
3615 this->memsz_ = 0;
3616 this->min_p_align_ = 0;
3617 this->offset_ = 0;
3618 this->filesz_ = 0;
3619 return;
3622 const Output_data* first;
3623 if (this->output_data_.empty())
3624 first = this->output_bss_.front();
3625 else
3626 first = this->output_data_.front();
3627 this->vaddr_ = first->address();
3628 this->paddr_ = (first->has_load_address()
3629 ? first->load_address()
3630 : this->vaddr_);
3631 this->are_addresses_set_ = true;
3632 this->offset_ = first->offset();
3634 if (this->output_data_.empty())
3635 this->filesz_ = 0;
3636 else
3638 const Output_data* last_data = this->output_data_.back();
3639 this->filesz_ = (last_data->address()
3640 + last_data->data_size()
3641 - this->vaddr_);
3644 const Output_data* last;
3645 if (this->output_bss_.empty())
3646 last = this->output_data_.back();
3647 else
3648 last = this->output_bss_.back();
3649 this->memsz_ = (last->address()
3650 + last->data_size()
3651 - this->vaddr_);
3653 // If this is a TLS segment, align the memory size. The code in
3654 // set_section_list ensures that the section after the TLS segment
3655 // is aligned to give us room.
3656 if (this->type_ == elfcpp::PT_TLS)
3658 uint64_t segment_align = this->maximum_alignment();
3659 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3660 this->memsz_ = align_address(this->memsz_, segment_align);
3663 // If this is a RELRO segment, align the memory size. The code in
3664 // set_section_list ensures that the section after the RELRO segment
3665 // is aligned to give us room.
3666 if (this->type_ == elfcpp::PT_GNU_RELRO)
3668 uint64_t page_align = parameters->target().common_pagesize();
3669 gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3670 this->memsz_ = align_address(this->memsz_, page_align);
3674 // Set the TLS offsets of the sections in the PT_TLS segment.
3676 void
3677 Output_segment::set_tls_offsets()
3679 gold_assert(this->type_ == elfcpp::PT_TLS);
3681 for (Output_data_list::iterator p = this->output_data_.begin();
3682 p != this->output_data_.end();
3683 ++p)
3684 (*p)->set_tls_offset(this->vaddr_);
3686 for (Output_data_list::iterator p = this->output_bss_.begin();
3687 p != this->output_bss_.end();
3688 ++p)
3689 (*p)->set_tls_offset(this->vaddr_);
3692 // Return the address of the first section.
3694 uint64_t
3695 Output_segment::first_section_load_address() const
3697 for (Output_data_list::const_iterator p = this->output_data_.begin();
3698 p != this->output_data_.end();
3699 ++p)
3700 if ((*p)->is_section())
3701 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3703 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3704 p != this->output_bss_.end();
3705 ++p)
3706 if ((*p)->is_section())
3707 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3709 gold_unreachable();
3712 // Return the number of Output_sections in an Output_segment.
3714 unsigned int
3715 Output_segment::output_section_count() const
3717 return (this->output_section_count_list(&this->output_data_)
3718 + this->output_section_count_list(&this->output_bss_));
3721 // Return the number of Output_sections in an Output_data_list.
3723 unsigned int
3724 Output_segment::output_section_count_list(const Output_data_list* pdl) const
3726 unsigned int count = 0;
3727 for (Output_data_list::const_iterator p = pdl->begin();
3728 p != pdl->end();
3729 ++p)
3731 if ((*p)->is_section())
3732 ++count;
3734 return count;
3737 // Return the section attached to the list segment with the lowest
3738 // load address. This is used when handling a PHDRS clause in a
3739 // linker script.
3741 Output_section*
3742 Output_segment::section_with_lowest_load_address() const
3744 Output_section* found = NULL;
3745 uint64_t found_lma = 0;
3746 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3748 Output_section* found_data = found;
3749 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3750 if (found != found_data && found_data != NULL)
3752 gold_error(_("nobits section %s may not precede progbits section %s "
3753 "in same segment"),
3754 found->name(), found_data->name());
3755 return NULL;
3758 return found;
3761 // Look through a list for a section with a lower load address.
3763 void
3764 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3765 Output_section** found,
3766 uint64_t* found_lma) const
3768 for (Output_data_list::const_iterator p = pdl->begin();
3769 p != pdl->end();
3770 ++p)
3772 if (!(*p)->is_section())
3773 continue;
3774 Output_section* os = static_cast<Output_section*>(*p);
3775 uint64_t lma = (os->has_load_address()
3776 ? os->load_address()
3777 : os->address());
3778 if (*found == NULL || lma < *found_lma)
3780 *found = os;
3781 *found_lma = lma;
3786 // Write the segment data into *OPHDR.
3788 template<int size, bool big_endian>
3789 void
3790 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
3792 ophdr->put_p_type(this->type_);
3793 ophdr->put_p_offset(this->offset_);
3794 ophdr->put_p_vaddr(this->vaddr_);
3795 ophdr->put_p_paddr(this->paddr_);
3796 ophdr->put_p_filesz(this->filesz_);
3797 ophdr->put_p_memsz(this->memsz_);
3798 ophdr->put_p_flags(this->flags_);
3799 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
3802 // Write the section headers into V.
3804 template<int size, bool big_endian>
3805 unsigned char*
3806 Output_segment::write_section_headers(const Layout* layout,
3807 const Stringpool* secnamepool,
3808 unsigned char* v,
3809 unsigned int *pshndx) const
3811 // Every section that is attached to a segment must be attached to a
3812 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3813 // segments.
3814 if (this->type_ != elfcpp::PT_LOAD)
3815 return v;
3817 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3818 &this->output_data_,
3819 v, pshndx);
3820 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3821 &this->output_bss_,
3822 v, pshndx);
3823 return v;
3826 template<int size, bool big_endian>
3827 unsigned char*
3828 Output_segment::write_section_headers_list(const Layout* layout,
3829 const Stringpool* secnamepool,
3830 const Output_data_list* pdl,
3831 unsigned char* v,
3832 unsigned int* pshndx) const
3834 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3835 for (Output_data_list::const_iterator p = pdl->begin();
3836 p != pdl->end();
3837 ++p)
3839 if ((*p)->is_section())
3841 const Output_section* ps = static_cast<const Output_section*>(*p);
3842 gold_assert(*pshndx == ps->out_shndx());
3843 elfcpp::Shdr_write<size, big_endian> oshdr(v);
3844 ps->write_header(layout, secnamepool, &oshdr);
3845 v += shdr_size;
3846 ++*pshndx;
3849 return v;
3852 // Print the output sections to the map file.
3854 void
3855 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3857 if (this->type() != elfcpp::PT_LOAD)
3858 return;
3859 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3860 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3863 // Print an output section list to the map file.
3865 void
3866 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3867 const Output_data_list* pdl) const
3869 for (Output_data_list::const_iterator p = pdl->begin();
3870 p != pdl->end();
3871 ++p)
3872 (*p)->print_to_mapfile(mapfile);
3875 // Output_file methods.
3877 Output_file::Output_file(const char* name)
3878 : name_(name),
3879 o_(-1),
3880 file_size_(0),
3881 base_(NULL),
3882 map_is_anonymous_(false),
3883 is_temporary_(false)
3887 // Try to open an existing file. Returns false if the file doesn't
3888 // exist, has a size of 0 or can't be mmapped.
3890 bool
3891 Output_file::open_for_modification()
3893 // The name "-" means "stdout".
3894 if (strcmp(this->name_, "-") == 0)
3895 return false;
3897 // Don't bother opening files with a size of zero.
3898 struct stat s;
3899 if (::stat(this->name_, &s) != 0 || s.st_size == 0)
3900 return false;
3902 int o = open_descriptor(-1, this->name_, O_RDWR, 0);
3903 if (o < 0)
3904 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3905 this->o_ = o;
3906 this->file_size_ = s.st_size;
3908 // If the file can't be mmapped, copying the content to an anonymous
3909 // map will probably negate the performance benefits of incremental
3910 // linking. This could be helped by using views and loading only
3911 // the necessary parts, but this is not supported as of now.
3912 if (!this->map_no_anonymous())
3914 release_descriptor(o, true);
3915 this->o_ = -1;
3916 this->file_size_ = 0;
3917 return false;
3920 return true;
3923 // Open the output file.
3925 void
3926 Output_file::open(off_t file_size)
3928 this->file_size_ = file_size;
3930 // Unlink the file first; otherwise the open() may fail if the file
3931 // is busy (e.g. it's an executable that's currently being executed).
3933 // However, the linker may be part of a system where a zero-length
3934 // file is created for it to write to, with tight permissions (gcc
3935 // 2.95 did something like this). Unlinking the file would work
3936 // around those permission controls, so we only unlink if the file
3937 // has a non-zero size. We also unlink only regular files to avoid
3938 // trouble with directories/etc.
3940 // If we fail, continue; this command is merely a best-effort attempt
3941 // to improve the odds for open().
3943 // We let the name "-" mean "stdout"
3944 if (!this->is_temporary_)
3946 if (strcmp(this->name_, "-") == 0)
3947 this->o_ = STDOUT_FILENO;
3948 else
3950 struct stat s;
3951 if (::stat(this->name_, &s) == 0
3952 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
3954 if (s.st_size != 0)
3955 ::unlink(this->name_);
3956 else if (!parameters->options().relocatable())
3958 // If we don't unlink the existing file, add execute
3959 // permission where read permissions already exist
3960 // and where the umask permits.
3961 int mask = ::umask(0);
3962 ::umask(mask);
3963 s.st_mode |= (s.st_mode & 0444) >> 2;
3964 ::chmod(this->name_, s.st_mode & ~mask);
3968 int mode = parameters->options().relocatable() ? 0666 : 0777;
3969 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
3970 mode);
3971 if (o < 0)
3972 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3973 this->o_ = o;
3977 this->map();
3980 // Resize the output file.
3982 void
3983 Output_file::resize(off_t file_size)
3985 // If the mmap is mapping an anonymous memory buffer, this is easy:
3986 // just mremap to the new size. If it's mapping to a file, we want
3987 // to unmap to flush to the file, then remap after growing the file.
3988 if (this->map_is_anonymous_)
3990 void* base = ::mremap(this->base_, this->file_size_, file_size,
3991 MREMAP_MAYMOVE);
3992 if (base == MAP_FAILED)
3993 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3994 this->base_ = static_cast<unsigned char*>(base);
3995 this->file_size_ = file_size;
3997 else
3999 this->unmap();
4000 this->file_size_ = file_size;
4001 if (!this->map_no_anonymous())
4002 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4006 // Map an anonymous block of memory which will later be written to the
4007 // file. Return whether the map succeeded.
4009 bool
4010 Output_file::map_anonymous()
4012 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4013 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4014 if (base != MAP_FAILED)
4016 this->map_is_anonymous_ = true;
4017 this->base_ = static_cast<unsigned char*>(base);
4018 return true;
4020 return false;
4023 // Map the file into memory. Return whether the mapping succeeded.
4025 bool
4026 Output_file::map_no_anonymous()
4028 const int o = this->o_;
4030 // If the output file is not a regular file, don't try to mmap it;
4031 // instead, we'll mmap a block of memory (an anonymous buffer), and
4032 // then later write the buffer to the file.
4033 void* base;
4034 struct stat statbuf;
4035 if (o == STDOUT_FILENO || o == STDERR_FILENO
4036 || ::fstat(o, &statbuf) != 0
4037 || !S_ISREG(statbuf.st_mode)
4038 || this->is_temporary_)
4039 return false;
4041 // Ensure that we have disk space available for the file. If we
4042 // don't do this, it is possible that we will call munmap, close,
4043 // and exit with dirty buffers still in the cache with no assigned
4044 // disk blocks. If the disk is out of space at that point, the
4045 // output file will wind up incomplete, but we will have already
4046 // exited. The alternative to fallocate would be to use fdatasync,
4047 // but that would be a more significant performance hit.
4048 if (::posix_fallocate(o, 0, this->file_size_) < 0)
4049 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
4051 // Map the file into memory.
4052 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4053 MAP_SHARED, o, 0);
4055 // The mmap call might fail because of file system issues: the file
4056 // system might not support mmap at all, or it might not support
4057 // mmap with PROT_WRITE.
4058 if (base == MAP_FAILED)
4059 return false;
4061 this->map_is_anonymous_ = false;
4062 this->base_ = static_cast<unsigned char*>(base);
4063 return true;
4066 // Map the file into memory.
4068 void
4069 Output_file::map()
4071 if (this->map_no_anonymous())
4072 return;
4074 // The mmap call might fail because of file system issues: the file
4075 // system might not support mmap at all, or it might not support
4076 // mmap with PROT_WRITE. I'm not sure which errno values we will
4077 // see in all cases, so if the mmap fails for any reason and we
4078 // don't care about file contents, try for an anonymous map.
4079 if (this->map_anonymous())
4080 return;
4082 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4083 this->name_, static_cast<unsigned long>(this->file_size_),
4084 strerror(errno));
4087 // Unmap the file from memory.
4089 void
4090 Output_file::unmap()
4092 if (::munmap(this->base_, this->file_size_) < 0)
4093 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
4094 this->base_ = NULL;
4097 // Close the output file.
4099 void
4100 Output_file::close()
4102 // If the map isn't file-backed, we need to write it now.
4103 if (this->map_is_anonymous_ && !this->is_temporary_)
4105 size_t bytes_to_write = this->file_size_;
4106 size_t offset = 0;
4107 while (bytes_to_write > 0)
4109 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
4110 bytes_to_write);
4111 if (bytes_written == 0)
4112 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
4113 else if (bytes_written < 0)
4114 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
4115 else
4117 bytes_to_write -= bytes_written;
4118 offset += bytes_written;
4122 this->unmap();
4124 // We don't close stdout or stderr
4125 if (this->o_ != STDOUT_FILENO
4126 && this->o_ != STDERR_FILENO
4127 && !this->is_temporary_)
4128 if (::close(this->o_) < 0)
4129 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
4130 this->o_ = -1;
4133 // Instantiate the templates we need. We could use the configure
4134 // script to restrict this to only the ones for implemented targets.
4136 #ifdef HAVE_TARGET_32_LITTLE
4137 template
4138 off_t
4139 Output_section::add_input_section<32, false>(
4140 Sized_relobj<32, false>* object,
4141 unsigned int sec_shndx,
4142 const char* secname,
4143 const elfcpp::Shdr<32, false>& shdr,
4144 unsigned int reloc_shndx,
4145 bool have_sections_script);
4146 #endif
4148 #ifdef HAVE_TARGET_32_BIG
4149 template
4150 off_t
4151 Output_section::add_input_section<32, true>(
4152 Sized_relobj<32, true>* object,
4153 unsigned int sec_shndx,
4154 const char* secname,
4155 const elfcpp::Shdr<32, true>& shdr,
4156 unsigned int reloc_shndx,
4157 bool have_sections_script);
4158 #endif
4160 #ifdef HAVE_TARGET_64_LITTLE
4161 template
4162 off_t
4163 Output_section::add_input_section<64, false>(
4164 Sized_relobj<64, false>* object,
4165 unsigned int sec_shndx,
4166 const char* secname,
4167 const elfcpp::Shdr<64, false>& shdr,
4168 unsigned int reloc_shndx,
4169 bool have_sections_script);
4170 #endif
4172 #ifdef HAVE_TARGET_64_BIG
4173 template
4174 off_t
4175 Output_section::add_input_section<64, true>(
4176 Sized_relobj<64, true>* object,
4177 unsigned int sec_shndx,
4178 const char* secname,
4179 const elfcpp::Shdr<64, true>& shdr,
4180 unsigned int reloc_shndx,
4181 bool have_sections_script);
4182 #endif
4184 #ifdef HAVE_TARGET_32_LITTLE
4185 template
4186 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
4187 #endif
4189 #ifdef HAVE_TARGET_32_BIG
4190 template
4191 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
4192 #endif
4194 #ifdef HAVE_TARGET_64_LITTLE
4195 template
4196 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
4197 #endif
4199 #ifdef HAVE_TARGET_64_BIG
4200 template
4201 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
4202 #endif
4204 #ifdef HAVE_TARGET_32_LITTLE
4205 template
4206 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
4207 #endif
4209 #ifdef HAVE_TARGET_32_BIG
4210 template
4211 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
4212 #endif
4214 #ifdef HAVE_TARGET_64_LITTLE
4215 template
4216 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
4217 #endif
4219 #ifdef HAVE_TARGET_64_BIG
4220 template
4221 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
4222 #endif
4224 #ifdef HAVE_TARGET_32_LITTLE
4225 template
4226 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
4227 #endif
4229 #ifdef HAVE_TARGET_32_BIG
4230 template
4231 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
4232 #endif
4234 #ifdef HAVE_TARGET_64_LITTLE
4235 template
4236 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
4237 #endif
4239 #ifdef HAVE_TARGET_64_BIG
4240 template
4241 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
4242 #endif
4244 #ifdef HAVE_TARGET_32_LITTLE
4245 template
4246 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
4247 #endif
4249 #ifdef HAVE_TARGET_32_BIG
4250 template
4251 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
4252 #endif
4254 #ifdef HAVE_TARGET_64_LITTLE
4255 template
4256 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
4257 #endif
4259 #ifdef HAVE_TARGET_64_BIG
4260 template
4261 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
4262 #endif
4264 #ifdef HAVE_TARGET_32_LITTLE
4265 template
4266 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
4267 #endif
4269 #ifdef HAVE_TARGET_32_BIG
4270 template
4271 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
4272 #endif
4274 #ifdef HAVE_TARGET_64_LITTLE
4275 template
4276 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
4277 #endif
4279 #ifdef HAVE_TARGET_64_BIG
4280 template
4281 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
4282 #endif
4284 #ifdef HAVE_TARGET_32_LITTLE
4285 template
4286 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
4287 #endif
4289 #ifdef HAVE_TARGET_32_BIG
4290 template
4291 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
4292 #endif
4294 #ifdef HAVE_TARGET_64_LITTLE
4295 template
4296 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
4297 #endif
4299 #ifdef HAVE_TARGET_64_BIG
4300 template
4301 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
4302 #endif
4304 #ifdef HAVE_TARGET_32_LITTLE
4305 template
4306 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
4307 #endif
4309 #ifdef HAVE_TARGET_32_BIG
4310 template
4311 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
4312 #endif
4314 #ifdef HAVE_TARGET_64_LITTLE
4315 template
4316 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
4317 #endif
4319 #ifdef HAVE_TARGET_64_BIG
4320 template
4321 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
4322 #endif
4324 #ifdef HAVE_TARGET_32_LITTLE
4325 template
4326 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
4327 #endif
4329 #ifdef HAVE_TARGET_32_BIG
4330 template
4331 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
4332 #endif
4334 #ifdef HAVE_TARGET_64_LITTLE
4335 template
4336 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
4337 #endif
4339 #ifdef HAVE_TARGET_64_BIG
4340 template
4341 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
4342 #endif
4344 #ifdef HAVE_TARGET_32_LITTLE
4345 template
4346 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
4347 #endif
4349 #ifdef HAVE_TARGET_32_BIG
4350 template
4351 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
4352 #endif
4354 #ifdef HAVE_TARGET_64_LITTLE
4355 template
4356 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
4357 #endif
4359 #ifdef HAVE_TARGET_64_BIG
4360 template
4361 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
4362 #endif
4364 #ifdef HAVE_TARGET_32_LITTLE
4365 template
4366 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
4367 #endif
4369 #ifdef HAVE_TARGET_32_BIG
4370 template
4371 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
4372 #endif
4374 #ifdef HAVE_TARGET_64_LITTLE
4375 template
4376 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
4377 #endif
4379 #ifdef HAVE_TARGET_64_BIG
4380 template
4381 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
4382 #endif
4384 #ifdef HAVE_TARGET_32_LITTLE
4385 template
4386 class Output_data_group<32, false>;
4387 #endif
4389 #ifdef HAVE_TARGET_32_BIG
4390 template
4391 class Output_data_group<32, true>;
4392 #endif
4394 #ifdef HAVE_TARGET_64_LITTLE
4395 template
4396 class Output_data_group<64, false>;
4397 #endif
4399 #ifdef HAVE_TARGET_64_BIG
4400 template
4401 class Output_data_group<64, true>;
4402 #endif
4404 #ifdef HAVE_TARGET_32_LITTLE
4405 template
4406 class Output_data_got<32, false>;
4407 #endif
4409 #ifdef HAVE_TARGET_32_BIG
4410 template
4411 class Output_data_got<32, true>;
4412 #endif
4414 #ifdef HAVE_TARGET_64_LITTLE
4415 template
4416 class Output_data_got<64, false>;
4417 #endif
4419 #ifdef HAVE_TARGET_64_BIG
4420 template
4421 class Output_data_got<64, true>;
4422 #endif
4424 } // End namespace gold.