[gdb/testsuite] Fix Wreturn-mismatch in gdb.base/list-dot-nodebug.exp
[binutils-gdb.git] / gold / output.cc
blobead67f20363597a6beab46791f7dd4ab9fed4c5c
1 // output.cc -- manage the output file for gold
3 // Copyright (C) 2006-2024 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/stat.h>
31 #include <algorithm>
32 #include <uchar.h>
34 #ifdef HAVE_SYS_MMAN_H
35 #include <sys/mman.h>
36 #endif
38 #include "libiberty.h"
40 #include "dwarf.h"
41 #include "parameters.h"
42 #include "object.h"
43 #include "symtab.h"
44 #include "reloc.h"
45 #include "merge.h"
46 #include "descriptors.h"
47 #include "layout.h"
48 #include "output.h"
50 // For systems without mmap support.
51 #ifndef HAVE_MMAP
52 # define mmap gold_mmap
53 # define munmap gold_munmap
54 # define mremap gold_mremap
55 # ifndef MAP_FAILED
56 # define MAP_FAILED (reinterpret_cast<void*>(-1))
57 # endif
58 # ifndef PROT_READ
59 # define PROT_READ 0
60 # endif
61 # ifndef PROT_WRITE
62 # define PROT_WRITE 0
63 # endif
64 # ifndef MAP_PRIVATE
65 # define MAP_PRIVATE 0
66 # endif
67 # ifndef MAP_ANONYMOUS
68 # define MAP_ANONYMOUS 0
69 # endif
70 # ifndef MAP_SHARED
71 # define MAP_SHARED 0
72 # endif
74 # ifndef ENOSYS
75 # define ENOSYS EINVAL
76 # endif
78 static void *
79 gold_mmap(void *, size_t, int, int, int, off_t)
81 errno = ENOSYS;
82 return MAP_FAILED;
85 static int
86 gold_munmap(void *, size_t)
88 errno = ENOSYS;
89 return -1;
92 static void *
93 gold_mremap(void *, size_t, size_t, int)
95 errno = ENOSYS;
96 return MAP_FAILED;
99 #endif
101 #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
102 # define mremap gold_mremap
103 extern "C" void *gold_mremap(void *, size_t, size_t, int);
104 #endif
106 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
107 #ifndef MAP_ANONYMOUS
108 # define MAP_ANONYMOUS MAP_ANON
109 #endif
111 #ifndef MREMAP_MAYMOVE
112 # define MREMAP_MAYMOVE 1
113 #endif
115 // Mingw does not have S_ISLNK.
116 #ifndef S_ISLNK
117 # define S_ISLNK(mode) 0
118 #endif
120 namespace gold
123 // A wrapper around posix_fallocate. If we don't have posix_fallocate,
124 // or the --no-posix-fallocate option is set, we try the fallocate
125 // system call directly. If that fails, we use ftruncate to set
126 // the file size and hope that there is enough disk space.
128 static int
129 gold_fallocate(int o, off_t offset, off_t len)
131 if (len <= 0)
132 return 0;
134 #ifdef HAVE_POSIX_FALLOCATE
135 if (parameters->options().posix_fallocate())
137 int err = ::posix_fallocate(o, offset, len);
138 if (err != EINVAL && err != ENOSYS && err != EOPNOTSUPP)
139 return err;
141 #endif // defined(HAVE_POSIX_FALLOCATE)
143 #ifdef HAVE_FALLOCATE
145 errno = 0;
146 int err = ::fallocate(o, 0, offset, len);
147 if (err < 0 && errno != EINVAL && errno != ENOSYS && errno != EOPNOTSUPP)
148 return errno;
150 #endif // defined(HAVE_FALLOCATE)
152 errno = 0;
153 if (::ftruncate(o, offset + len) < 0)
154 return errno;
155 return 0;
158 // Output_data variables.
160 bool Output_data::allocated_sizes_are_fixed;
162 // Output_data methods.
164 Output_data::~Output_data()
168 // Return the default alignment for the target size.
170 uint64_t
171 Output_data::default_alignment()
173 return Output_data::default_alignment_for_size(
174 parameters->target().get_size());
177 // Return the default alignment for a size--32 or 64.
179 uint64_t
180 Output_data::default_alignment_for_size(int size)
182 if (size == 32)
183 return 4;
184 else if (size == 64)
185 return 8;
186 else
187 gold_unreachable();
190 // Output_section_header methods. This currently assumes that the
191 // segment and section lists are complete at construction time.
193 Output_section_headers::Output_section_headers(
194 const Layout* layout,
195 const Layout::Segment_list* segment_list,
196 const Layout::Section_list* section_list,
197 const Layout::Section_list* unattached_section_list,
198 const Stringpool* secnamepool,
199 const Output_section* shstrtab_section)
200 : layout_(layout),
201 segment_list_(segment_list),
202 section_list_(section_list),
203 unattached_section_list_(unattached_section_list),
204 secnamepool_(secnamepool),
205 shstrtab_section_(shstrtab_section)
209 // Compute the current data size.
211 off_t
212 Output_section_headers::do_size() const
214 // Count all the sections. Start with 1 for the null section.
215 off_t count = 1;
216 if (!parameters->options().relocatable())
218 for (Layout::Segment_list::const_iterator p =
219 this->segment_list_->begin();
220 p != this->segment_list_->end();
221 ++p)
222 if ((*p)->type() == elfcpp::PT_LOAD)
223 count += (*p)->output_section_count();
225 else
227 for (Layout::Section_list::const_iterator p =
228 this->section_list_->begin();
229 p != this->section_list_->end();
230 ++p)
231 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
232 ++count;
234 count += this->unattached_section_list_->size();
236 const int size = parameters->target().get_size();
237 int shdr_size;
238 if (size == 32)
239 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
240 else if (size == 64)
241 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
242 else
243 gold_unreachable();
245 return count * shdr_size;
248 // Write out the section headers.
250 void
251 Output_section_headers::do_write(Output_file* of)
253 switch (parameters->size_and_endianness())
255 #ifdef HAVE_TARGET_32_LITTLE
256 case Parameters::TARGET_32_LITTLE:
257 this->do_sized_write<32, false>(of);
258 break;
259 #endif
260 #ifdef HAVE_TARGET_32_BIG
261 case Parameters::TARGET_32_BIG:
262 this->do_sized_write<32, true>(of);
263 break;
264 #endif
265 #ifdef HAVE_TARGET_64_LITTLE
266 case Parameters::TARGET_64_LITTLE:
267 this->do_sized_write<64, false>(of);
268 break;
269 #endif
270 #ifdef HAVE_TARGET_64_BIG
271 case Parameters::TARGET_64_BIG:
272 this->do_sized_write<64, true>(of);
273 break;
274 #endif
275 default:
276 gold_unreachable();
280 template<int size, bool big_endian>
281 void
282 Output_section_headers::do_sized_write(Output_file* of)
284 off_t all_shdrs_size = this->data_size();
285 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
287 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
288 unsigned char* v = view;
291 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
292 oshdr.put_sh_name(0);
293 oshdr.put_sh_type(elfcpp::SHT_NULL);
294 oshdr.put_sh_flags(0);
295 oshdr.put_sh_addr(0);
296 oshdr.put_sh_offset(0);
298 size_t section_count = (this->data_size()
299 / elfcpp::Elf_sizes<size>::shdr_size);
300 if (section_count < elfcpp::SHN_LORESERVE)
301 oshdr.put_sh_size(0);
302 else
303 oshdr.put_sh_size(section_count);
305 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
306 if (shstrndx < elfcpp::SHN_LORESERVE)
307 oshdr.put_sh_link(0);
308 else
309 oshdr.put_sh_link(shstrndx);
311 size_t segment_count = this->segment_list_->size();
312 oshdr.put_sh_info(segment_count >= elfcpp::PN_XNUM ? segment_count : 0);
314 oshdr.put_sh_addralign(0);
315 oshdr.put_sh_entsize(0);
318 v += shdr_size;
320 unsigned int shndx = 1;
321 if (!parameters->options().relocatable())
323 for (Layout::Segment_list::const_iterator p =
324 this->segment_list_->begin();
325 p != this->segment_list_->end();
326 ++p)
327 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
328 this->secnamepool_,
330 &shndx);
332 else
334 for (Layout::Section_list::const_iterator p =
335 this->section_list_->begin();
336 p != this->section_list_->end();
337 ++p)
339 // We do unallocated sections below, except that group
340 // sections have to come first.
341 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
342 && (*p)->type() != elfcpp::SHT_GROUP)
343 continue;
344 gold_assert(shndx == (*p)->out_shndx());
345 elfcpp::Shdr_write<size, big_endian> oshdr(v);
346 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
347 v += shdr_size;
348 ++shndx;
352 for (Layout::Section_list::const_iterator p =
353 this->unattached_section_list_->begin();
354 p != this->unattached_section_list_->end();
355 ++p)
357 // For a relocatable link, we did unallocated group sections
358 // above, since they have to come first.
359 if ((*p)->type() == elfcpp::SHT_GROUP
360 && parameters->options().relocatable())
361 continue;
362 gold_assert(shndx == (*p)->out_shndx());
363 elfcpp::Shdr_write<size, big_endian> oshdr(v);
364 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
365 v += shdr_size;
366 ++shndx;
369 of->write_output_view(this->offset(), all_shdrs_size, view);
372 // Output_segment_header methods.
374 Output_segment_headers::Output_segment_headers(
375 const Layout::Segment_list& segment_list)
376 : segment_list_(segment_list)
378 this->set_current_data_size_for_child(this->do_size());
381 void
382 Output_segment_headers::do_write(Output_file* of)
384 switch (parameters->size_and_endianness())
386 #ifdef HAVE_TARGET_32_LITTLE
387 case Parameters::TARGET_32_LITTLE:
388 this->do_sized_write<32, false>(of);
389 break;
390 #endif
391 #ifdef HAVE_TARGET_32_BIG
392 case Parameters::TARGET_32_BIG:
393 this->do_sized_write<32, true>(of);
394 break;
395 #endif
396 #ifdef HAVE_TARGET_64_LITTLE
397 case Parameters::TARGET_64_LITTLE:
398 this->do_sized_write<64, false>(of);
399 break;
400 #endif
401 #ifdef HAVE_TARGET_64_BIG
402 case Parameters::TARGET_64_BIG:
403 this->do_sized_write<64, true>(of);
404 break;
405 #endif
406 default:
407 gold_unreachable();
411 template<int size, bool big_endian>
412 void
413 Output_segment_headers::do_sized_write(Output_file* of)
415 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
416 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
417 gold_assert(all_phdrs_size == this->data_size());
418 unsigned char* view = of->get_output_view(this->offset(),
419 all_phdrs_size);
420 unsigned char* v = view;
421 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
422 p != this->segment_list_.end();
423 ++p)
425 elfcpp::Phdr_write<size, big_endian> ophdr(v);
426 (*p)->write_header(&ophdr);
427 v += phdr_size;
430 gold_assert(v - view == all_phdrs_size);
432 of->write_output_view(this->offset(), all_phdrs_size, view);
435 off_t
436 Output_segment_headers::do_size() const
438 const int size = parameters->target().get_size();
439 int phdr_size;
440 if (size == 32)
441 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
442 else if (size == 64)
443 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
444 else
445 gold_unreachable();
447 return this->segment_list_.size() * phdr_size;
450 // Output_file_header methods.
452 Output_file_header::Output_file_header(Target* target,
453 const Symbol_table* symtab,
454 const Output_segment_headers* osh)
455 : target_(target),
456 symtab_(symtab),
457 segment_header_(osh),
458 section_header_(NULL),
459 shstrtab_(NULL)
461 this->set_data_size(this->do_size());
464 // Set the section table information for a file header.
466 void
467 Output_file_header::set_section_info(const Output_section_headers* shdrs,
468 const Output_section* shstrtab)
470 this->section_header_ = shdrs;
471 this->shstrtab_ = shstrtab;
474 // Write out the file header.
476 void
477 Output_file_header::do_write(Output_file* of)
479 gold_assert(this->offset() == 0);
481 switch (parameters->size_and_endianness())
483 #ifdef HAVE_TARGET_32_LITTLE
484 case Parameters::TARGET_32_LITTLE:
485 this->do_sized_write<32, false>(of);
486 break;
487 #endif
488 #ifdef HAVE_TARGET_32_BIG
489 case Parameters::TARGET_32_BIG:
490 this->do_sized_write<32, true>(of);
491 break;
492 #endif
493 #ifdef HAVE_TARGET_64_LITTLE
494 case Parameters::TARGET_64_LITTLE:
495 this->do_sized_write<64, false>(of);
496 break;
497 #endif
498 #ifdef HAVE_TARGET_64_BIG
499 case Parameters::TARGET_64_BIG:
500 this->do_sized_write<64, true>(of);
501 break;
502 #endif
503 default:
504 gold_unreachable();
508 // Write out the file header with appropriate size and endianness.
510 template<int size, bool big_endian>
511 void
512 Output_file_header::do_sized_write(Output_file* of)
514 gold_assert(this->offset() == 0);
516 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
517 unsigned char* view = of->get_output_view(0, ehdr_size);
518 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
520 unsigned char e_ident[elfcpp::EI_NIDENT];
521 memset(e_ident, 0, elfcpp::EI_NIDENT);
522 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
523 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
524 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
525 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
526 if (size == 32)
527 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
528 else if (size == 64)
529 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
530 else
531 gold_unreachable();
532 e_ident[elfcpp::EI_DATA] = (big_endian
533 ? elfcpp::ELFDATA2MSB
534 : elfcpp::ELFDATA2LSB);
535 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
536 oehdr.put_e_ident(e_ident);
538 elfcpp::ET e_type;
539 if (parameters->options().relocatable())
540 e_type = elfcpp::ET_REL;
541 else if (parameters->options().output_is_position_independent())
542 e_type = elfcpp::ET_DYN;
543 else
544 e_type = elfcpp::ET_EXEC;
545 oehdr.put_e_type(e_type);
547 oehdr.put_e_machine(this->target_->machine_code());
548 oehdr.put_e_version(elfcpp::EV_CURRENT);
550 oehdr.put_e_entry(this->entry<size>());
552 if (this->segment_header_ == NULL)
553 oehdr.put_e_phoff(0);
554 else
555 oehdr.put_e_phoff(this->segment_header_->offset());
557 oehdr.put_e_shoff(this->section_header_->offset());
558 oehdr.put_e_flags(this->target_->processor_specific_flags());
559 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
561 if (this->segment_header_ == NULL)
563 oehdr.put_e_phentsize(0);
564 oehdr.put_e_phnum(0);
566 else
568 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
569 size_t phnum = (this->segment_header_->data_size()
570 / elfcpp::Elf_sizes<size>::phdr_size);
571 if (phnum > elfcpp::PN_XNUM)
572 phnum = elfcpp::PN_XNUM;
573 oehdr.put_e_phnum(phnum);
576 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
577 size_t section_count = (this->section_header_->data_size()
578 / elfcpp::Elf_sizes<size>::shdr_size);
580 if (section_count < elfcpp::SHN_LORESERVE)
581 oehdr.put_e_shnum(this->section_header_->data_size()
582 / elfcpp::Elf_sizes<size>::shdr_size);
583 else
584 oehdr.put_e_shnum(0);
586 unsigned int shstrndx = this->shstrtab_->out_shndx();
587 if (shstrndx < elfcpp::SHN_LORESERVE)
588 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
589 else
590 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
592 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
593 // the e_ident field.
594 this->target_->adjust_elf_header(view, ehdr_size);
596 of->write_output_view(0, ehdr_size, view);
599 // Return the value to use for the entry address.
601 template<int size>
602 typename elfcpp::Elf_types<size>::Elf_Addr
603 Output_file_header::entry()
605 const bool should_issue_warning = (parameters->options().entry() != NULL
606 && !parameters->options().relocatable()
607 && !parameters->options().shared());
608 const char* entry = parameters->entry();
609 Symbol* sym = this->symtab_->lookup(entry);
611 typename Sized_symbol<size>::Value_type v;
612 if (sym != NULL)
614 Sized_symbol<size>* ssym;
615 ssym = this->symtab_->get_sized_symbol<size>(sym);
616 if (!ssym->is_defined() && should_issue_warning)
617 gold_warning("entry symbol '%s' exists but is not defined", entry);
618 v = ssym->value();
620 else
622 // We couldn't find the entry symbol. See if we can parse it as
623 // a number. This supports, e.g., -e 0x1000.
624 char* endptr;
625 v = strtoull(entry, &endptr, 0);
626 if (*endptr != '\0')
628 if (should_issue_warning)
629 gold_warning("cannot find entry symbol '%s'", entry);
630 v = 0;
634 return v;
637 // Compute the current data size.
639 off_t
640 Output_file_header::do_size() const
642 const int size = parameters->target().get_size();
643 if (size == 32)
644 return elfcpp::Elf_sizes<32>::ehdr_size;
645 else if (size == 64)
646 return elfcpp::Elf_sizes<64>::ehdr_size;
647 else
648 gold_unreachable();
651 // Output_data_const methods.
653 void
654 Output_data_const::do_write(Output_file* of)
656 of->write(this->offset(), this->data_.data(), this->data_.size());
659 // Output_data_const_buffer methods.
661 void
662 Output_data_const_buffer::do_write(Output_file* of)
664 of->write(this->offset(), this->p_, this->data_size());
667 // Output_section_data methods.
669 // Record the output section, and set the entry size and such.
671 void
672 Output_section_data::set_output_section(Output_section* os)
674 gold_assert(this->output_section_ == NULL);
675 this->output_section_ = os;
676 this->do_adjust_output_section(os);
679 // Return the section index of the output section.
681 unsigned int
682 Output_section_data::do_out_shndx() const
684 gold_assert(this->output_section_ != NULL);
685 return this->output_section_->out_shndx();
688 // Set the alignment, which means we may need to update the alignment
689 // of the output section.
691 void
692 Output_section_data::set_addralign(uint64_t addralign)
694 this->addralign_ = addralign;
695 if (this->output_section_ != NULL
696 && this->output_section_->addralign() < addralign)
697 this->output_section_->set_addralign(addralign);
700 // Output_data_strtab methods.
702 // Set the final data size.
704 void
705 Output_data_strtab::set_final_data_size()
707 this->strtab_->set_string_offsets();
708 this->set_data_size(this->strtab_->get_strtab_size());
711 // Write out a string table.
713 void
714 Output_data_strtab::do_write(Output_file* of)
716 this->strtab_->write(of, this->offset());
719 // Output_reloc methods.
721 // A reloc against a global symbol.
723 template<bool dynamic, int size, bool big_endian>
724 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
725 Symbol* gsym,
726 unsigned int type,
727 Output_data* od,
728 Address address,
729 bool is_relative,
730 bool is_symbolless,
731 bool use_plt_offset)
732 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
733 is_relative_(is_relative), is_symbolless_(is_symbolless),
734 is_section_symbol_(false), use_plt_offset_(use_plt_offset), shndx_(INVALID_CODE)
736 // this->type_ is a bitfield; make sure TYPE fits.
737 gold_assert(this->type_ == type);
738 this->u1_.gsym = gsym;
739 this->u2_.od = od;
740 if (dynamic)
741 this->set_needs_dynsym_index();
744 template<bool dynamic, int size, bool big_endian>
745 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
746 Symbol* gsym,
747 unsigned int type,
748 Sized_relobj<size, big_endian>* relobj,
749 unsigned int shndx,
750 Address address,
751 bool is_relative,
752 bool is_symbolless,
753 bool use_plt_offset)
754 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
755 is_relative_(is_relative), is_symbolless_(is_symbolless),
756 is_section_symbol_(false), use_plt_offset_(use_plt_offset), shndx_(shndx)
758 gold_assert(shndx != INVALID_CODE);
759 // this->type_ is a bitfield; make sure TYPE fits.
760 gold_assert(this->type_ == type);
761 this->u1_.gsym = gsym;
762 this->u2_.relobj = relobj;
763 if (dynamic)
764 this->set_needs_dynsym_index();
767 // A reloc against a local symbol.
769 template<bool dynamic, int size, bool big_endian>
770 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
771 Sized_relobj<size, big_endian>* relobj,
772 unsigned int local_sym_index,
773 unsigned int type,
774 Output_data* od,
775 Address address,
776 bool is_relative,
777 bool is_symbolless,
778 bool is_section_symbol,
779 bool use_plt_offset)
780 : address_(address), local_sym_index_(local_sym_index), type_(type),
781 is_relative_(is_relative), is_symbolless_(is_symbolless),
782 is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
783 shndx_(INVALID_CODE)
785 gold_assert(local_sym_index != GSYM_CODE
786 && local_sym_index != INVALID_CODE);
787 // this->type_ is a bitfield; make sure TYPE fits.
788 gold_assert(this->type_ == type);
789 this->u1_.relobj = relobj;
790 this->u2_.od = od;
791 if (dynamic)
792 this->set_needs_dynsym_index();
795 template<bool dynamic, int size, bool big_endian>
796 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
797 Sized_relobj<size, big_endian>* relobj,
798 unsigned int local_sym_index,
799 unsigned int type,
800 unsigned int shndx,
801 Address address,
802 bool is_relative,
803 bool is_symbolless,
804 bool is_section_symbol,
805 bool use_plt_offset)
806 : address_(address), local_sym_index_(local_sym_index), type_(type),
807 is_relative_(is_relative), is_symbolless_(is_symbolless),
808 is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
809 shndx_(shndx)
811 gold_assert(local_sym_index != GSYM_CODE
812 && local_sym_index != INVALID_CODE);
813 gold_assert(shndx != INVALID_CODE);
814 // this->type_ is a bitfield; make sure TYPE fits.
815 gold_assert(this->type_ == type);
816 this->u1_.relobj = relobj;
817 this->u2_.relobj = relobj;
818 if (dynamic)
819 this->set_needs_dynsym_index();
822 // A reloc against the STT_SECTION symbol of an output section.
824 template<bool dynamic, int size, bool big_endian>
825 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
826 Output_section* os,
827 unsigned int type,
828 Output_data* od,
829 Address address,
830 bool is_relative)
831 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
832 is_relative_(is_relative), is_symbolless_(is_relative),
833 is_section_symbol_(true), use_plt_offset_(false), shndx_(INVALID_CODE)
835 // this->type_ is a bitfield; make sure TYPE fits.
836 gold_assert(this->type_ == type);
837 this->u1_.os = os;
838 this->u2_.od = od;
839 if (dynamic)
840 this->set_needs_dynsym_index();
841 else
842 os->set_needs_symtab_index();
845 template<bool dynamic, int size, bool big_endian>
846 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
847 Output_section* os,
848 unsigned int type,
849 Sized_relobj<size, big_endian>* relobj,
850 unsigned int shndx,
851 Address address,
852 bool is_relative)
853 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
854 is_relative_(is_relative), is_symbolless_(is_relative),
855 is_section_symbol_(true), use_plt_offset_(false), shndx_(shndx)
857 gold_assert(shndx != INVALID_CODE);
858 // this->type_ is a bitfield; make sure TYPE fits.
859 gold_assert(this->type_ == type);
860 this->u1_.os = os;
861 this->u2_.relobj = relobj;
862 if (dynamic)
863 this->set_needs_dynsym_index();
864 else
865 os->set_needs_symtab_index();
868 // An absolute or relative relocation.
870 template<bool dynamic, int size, bool big_endian>
871 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
872 unsigned int type,
873 Output_data* od,
874 Address address,
875 bool is_relative)
876 : address_(address), local_sym_index_(0), type_(type),
877 is_relative_(is_relative), is_symbolless_(false),
878 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
880 // this->type_ is a bitfield; make sure TYPE fits.
881 gold_assert(this->type_ == type);
882 this->u1_.relobj = NULL;
883 this->u2_.od = od;
886 template<bool dynamic, int size, bool big_endian>
887 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
888 unsigned int type,
889 Sized_relobj<size, big_endian>* relobj,
890 unsigned int shndx,
891 Address address,
892 bool is_relative)
893 : address_(address), local_sym_index_(0), type_(type),
894 is_relative_(is_relative), is_symbolless_(false),
895 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
897 gold_assert(shndx != INVALID_CODE);
898 // this->type_ is a bitfield; make sure TYPE fits.
899 gold_assert(this->type_ == type);
900 this->u1_.relobj = NULL;
901 this->u2_.relobj = relobj;
904 // A target specific relocation.
906 template<bool dynamic, int size, bool big_endian>
907 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
908 unsigned int type,
909 void* arg,
910 Output_data* od,
911 Address address)
912 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
913 is_relative_(false), is_symbolless_(false),
914 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
916 // this->type_ is a bitfield; make sure TYPE fits.
917 gold_assert(this->type_ == type);
918 this->u1_.arg = arg;
919 this->u2_.od = od;
922 template<bool dynamic, int size, bool big_endian>
923 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
924 unsigned int type,
925 void* arg,
926 Sized_relobj<size, big_endian>* relobj,
927 unsigned int shndx,
928 Address address)
929 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
930 is_relative_(false), is_symbolless_(false),
931 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
933 gold_assert(shndx != INVALID_CODE);
934 // this->type_ is a bitfield; make sure TYPE fits.
935 gold_assert(this->type_ == type);
936 this->u1_.arg = arg;
937 this->u2_.relobj = relobj;
940 // Record that we need a dynamic symbol index for this relocation.
942 template<bool dynamic, int size, bool big_endian>
943 void
944 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
945 set_needs_dynsym_index()
947 if (this->is_symbolless_)
948 return;
949 switch (this->local_sym_index_)
951 case INVALID_CODE:
952 gold_unreachable();
954 case GSYM_CODE:
955 this->u1_.gsym->set_needs_dynsym_entry();
956 break;
958 case SECTION_CODE:
959 this->u1_.os->set_needs_dynsym_index();
960 break;
962 case TARGET_CODE:
963 // The target must take care of this if necessary.
964 break;
966 case 0:
967 break;
969 default:
971 const unsigned int lsi = this->local_sym_index_;
972 Sized_relobj_file<size, big_endian>* relobj =
973 this->u1_.relobj->sized_relobj();
974 gold_assert(relobj != NULL);
975 if (!this->is_section_symbol_)
976 relobj->set_needs_output_dynsym_entry(lsi);
977 else
978 relobj->output_section(lsi)->set_needs_dynsym_index();
980 break;
984 // Get the symbol index of a relocation.
986 template<bool dynamic, int size, bool big_endian>
987 unsigned int
988 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
989 const
991 unsigned int index;
992 if (this->is_symbolless_)
993 return 0;
994 switch (this->local_sym_index_)
996 case INVALID_CODE:
997 gold_unreachable();
999 case GSYM_CODE:
1000 if (this->u1_.gsym == NULL)
1001 index = 0;
1002 else if (dynamic)
1003 index = this->u1_.gsym->dynsym_index();
1004 else
1005 index = this->u1_.gsym->symtab_index();
1006 break;
1008 case SECTION_CODE:
1009 if (dynamic)
1010 index = this->u1_.os->dynsym_index();
1011 else
1012 index = this->u1_.os->symtab_index();
1013 break;
1015 case TARGET_CODE:
1016 index = parameters->target().reloc_symbol_index(this->u1_.arg,
1017 this->type_);
1018 break;
1020 case 0:
1021 // Relocations without symbols use a symbol index of 0.
1022 index = 0;
1023 break;
1025 default:
1027 const unsigned int lsi = this->local_sym_index_;
1028 Sized_relobj_file<size, big_endian>* relobj =
1029 this->u1_.relobj->sized_relobj();
1030 gold_assert(relobj != NULL);
1031 if (!this->is_section_symbol_)
1033 if (dynamic)
1034 index = relobj->dynsym_index(lsi);
1035 else
1036 index = relobj->symtab_index(lsi);
1038 else
1040 Output_section* os = relobj->output_section(lsi);
1041 gold_assert(os != NULL);
1042 if (dynamic)
1043 index = os->dynsym_index();
1044 else
1045 index = os->symtab_index();
1048 break;
1050 gold_assert(index != -1U);
1051 return index;
1054 // For a local section symbol, get the address of the offset ADDEND
1055 // within the input section.
1057 template<bool dynamic, int size, bool big_endian>
1058 typename elfcpp::Elf_types<size>::Elf_Addr
1059 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1060 local_section_offset(Addend addend) const
1062 gold_assert(this->local_sym_index_ != GSYM_CODE
1063 && this->local_sym_index_ != SECTION_CODE
1064 && this->local_sym_index_ != TARGET_CODE
1065 && this->local_sym_index_ != INVALID_CODE
1066 && this->local_sym_index_ != 0
1067 && this->is_section_symbol_);
1068 const unsigned int lsi = this->local_sym_index_;
1069 Output_section* os = this->u1_.relobj->output_section(lsi);
1070 gold_assert(os != NULL);
1071 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
1072 if (offset != invalid_address)
1073 return offset + addend;
1074 // This is a merge section.
1075 Sized_relobj_file<size, big_endian>* relobj =
1076 this->u1_.relobj->sized_relobj();
1077 gold_assert(relobj != NULL);
1078 offset = os->output_address(relobj, lsi, addend);
1079 gold_assert(offset != invalid_address);
1080 return offset;
1083 // Get the output address of a relocation.
1085 template<bool dynamic, int size, bool big_endian>
1086 typename elfcpp::Elf_types<size>::Elf_Addr
1087 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
1089 Address address = this->address_;
1090 if (this->shndx_ != INVALID_CODE)
1092 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
1093 gold_assert(os != NULL);
1094 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
1095 if (off != invalid_address)
1096 address += os->address() + off;
1097 else
1099 Sized_relobj_file<size, big_endian>* relobj =
1100 this->u2_.relobj->sized_relobj();
1101 gold_assert(relobj != NULL);
1102 address = os->output_address(relobj, this->shndx_, address);
1103 gold_assert(address != invalid_address);
1106 else if (this->u2_.od != NULL)
1107 address += this->u2_.od->address();
1108 return address;
1111 // Write out the offset and info fields of a Rel or Rela relocation
1112 // entry.
1114 template<bool dynamic, int size, bool big_endian>
1115 template<typename Write_rel>
1116 void
1117 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
1118 Write_rel* wr) const
1120 wr->put_r_offset(this->get_address());
1121 unsigned int sym_index = this->get_symbol_index();
1122 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
1125 // Write out a Rel relocation.
1127 template<bool dynamic, int size, bool big_endian>
1128 void
1129 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
1130 unsigned char* pov) const
1132 elfcpp::Rel_write<size, big_endian> orel(pov);
1133 this->write_rel(&orel);
1136 // Get the value of the symbol referred to by a Rel relocation.
1138 template<bool dynamic, int size, bool big_endian>
1139 typename elfcpp::Elf_types<size>::Elf_Addr
1140 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
1141 Addend addend) const
1143 if (this->local_sym_index_ == GSYM_CODE)
1145 const Sized_symbol<size>* sym;
1146 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
1147 if (this->use_plt_offset_ && sym->has_plt_offset())
1148 return parameters->target().plt_address_for_global(sym);
1149 else
1150 return sym->value() + addend;
1152 if (this->local_sym_index_ == SECTION_CODE)
1154 gold_assert(!this->use_plt_offset_);
1155 return this->u1_.os->address() + addend;
1157 gold_assert(this->local_sym_index_ != TARGET_CODE
1158 && this->local_sym_index_ != INVALID_CODE
1159 && this->local_sym_index_ != 0
1160 && !this->is_section_symbol_);
1161 const unsigned int lsi = this->local_sym_index_;
1162 Sized_relobj_file<size, big_endian>* relobj =
1163 this->u1_.relobj->sized_relobj();
1164 gold_assert(relobj != NULL);
1165 if (this->use_plt_offset_)
1166 return parameters->target().plt_address_for_local(relobj, lsi);
1167 const Symbol_value<size>* symval = relobj->local_symbol(lsi);
1168 return symval->value(relobj, addend);
1171 // Reloc comparison. This function sorts the dynamic relocs for the
1172 // benefit of the dynamic linker. First we sort all relative relocs
1173 // to the front. Among relative relocs, we sort by output address.
1174 // Among non-relative relocs, we sort by symbol index, then by output
1175 // address.
1177 template<bool dynamic, int size, bool big_endian>
1179 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1180 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1181 const
1183 if (this->is_relative_)
1185 if (!r2.is_relative_)
1186 return -1;
1187 // Otherwise sort by reloc address below.
1189 else if (r2.is_relative_)
1190 return 1;
1191 else
1193 unsigned int sym1 = this->get_symbol_index();
1194 unsigned int sym2 = r2.get_symbol_index();
1195 if (sym1 < sym2)
1196 return -1;
1197 else if (sym1 > sym2)
1198 return 1;
1199 // Otherwise sort by reloc address.
1202 section_offset_type addr1 = this->get_address();
1203 section_offset_type addr2 = r2.get_address();
1204 if (addr1 < addr2)
1205 return -1;
1206 else if (addr1 > addr2)
1207 return 1;
1209 // Final tie breaker, in order to generate the same output on any
1210 // host: reloc type.
1211 unsigned int type1 = this->type_;
1212 unsigned int type2 = r2.type_;
1213 if (type1 < type2)
1214 return -1;
1215 else if (type1 > type2)
1216 return 1;
1218 // These relocs appear to be exactly the same.
1219 return 0;
1222 // Write out a Rela relocation.
1224 template<bool dynamic, int size, bool big_endian>
1225 void
1226 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1227 unsigned char* pov) const
1229 elfcpp::Rela_write<size, big_endian> orel(pov);
1230 this->rel_.write_rel(&orel);
1231 Addend addend = this->addend_;
1232 if (this->rel_.is_target_specific())
1233 addend = parameters->target().reloc_addend(this->rel_.target_arg(),
1234 this->rel_.type(), addend);
1235 else if (this->rel_.is_symbolless())
1236 addend = this->rel_.symbol_value(addend);
1237 else if (this->rel_.is_local_section_symbol())
1238 addend = this->rel_.local_section_offset(addend);
1239 orel.put_r_addend(addend);
1242 // Output_data_reloc_base methods.
1244 // Adjust the output section.
1246 template<int sh_type, bool dynamic, int size, bool big_endian>
1247 void
1248 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1249 ::do_adjust_output_section(Output_section* os)
1251 if (sh_type == elfcpp::SHT_REL)
1252 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1253 else if (sh_type == elfcpp::SHT_RELA)
1254 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1255 else
1256 gold_unreachable();
1258 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1259 // static link. The backends will generate a dynamic reloc section
1260 // to hold this. In that case we don't want to link to the dynsym
1261 // section, because there isn't one.
1262 if (!dynamic)
1263 os->set_should_link_to_symtab();
1264 else if (parameters->doing_static_link())
1266 else
1267 os->set_should_link_to_dynsym();
1270 // Standard relocation writer, which just calls Output_reloc::write().
1272 template<int sh_type, bool dynamic, int size, bool big_endian>
1273 struct Output_reloc_writer
1275 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1276 typedef std::vector<Output_reloc_type> Relocs;
1278 static void
1279 write(typename Relocs::const_iterator p, unsigned char* pov)
1280 { p->write(pov); }
1283 // Write out relocation data.
1285 template<int sh_type, bool dynamic, int size, bool big_endian>
1286 void
1287 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1288 Output_file* of)
1290 typedef Output_reloc_writer<sh_type, dynamic, size, big_endian> Writer;
1291 this->do_write_generic<Writer>(of);
1294 // Class Output_relocatable_relocs.
1296 template<int sh_type, int size, bool big_endian>
1297 void
1298 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1300 this->set_data_size(this->rr_->output_reloc_count()
1301 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1304 // class Output_data_group.
1306 template<int size, bool big_endian>
1307 Output_data_group<size, big_endian>::Output_data_group(
1308 Sized_relobj_file<size, big_endian>* relobj,
1309 section_size_type entry_count,
1310 elfcpp::Elf_Word flags,
1311 std::vector<unsigned int>* input_shndxes)
1312 : Output_section_data(entry_count * 4, 4, false),
1313 relobj_(relobj),
1314 flags_(flags)
1316 this->input_shndxes_.swap(*input_shndxes);
1319 // Write out the section group, which means translating the section
1320 // indexes to apply to the output file.
1322 template<int size, bool big_endian>
1323 void
1324 Output_data_group<size, big_endian>::do_write(Output_file* of)
1326 const off_t off = this->offset();
1327 const section_size_type oview_size =
1328 convert_to_section_size_type(this->data_size());
1329 unsigned char* const oview = of->get_output_view(off, oview_size);
1331 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1332 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1333 ++contents;
1335 for (std::vector<unsigned int>::const_iterator p =
1336 this->input_shndxes_.begin();
1337 p != this->input_shndxes_.end();
1338 ++p, ++contents)
1340 Output_section* os = this->relobj_->output_section(*p);
1342 unsigned int output_shndx;
1343 if (os != NULL)
1344 output_shndx = os->out_shndx();
1345 else
1347 this->relobj_->error(_("section group retained but "
1348 "group element discarded"));
1349 output_shndx = 0;
1352 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1355 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1356 gold_assert(wrote == oview_size);
1358 of->write_output_view(off, oview_size, oview);
1360 // We no longer need this information.
1361 this->input_shndxes_.clear();
1364 // Output_data_got::Got_entry methods.
1366 // Write out the entry.
1368 template<int got_size, bool big_endian>
1369 void
1370 Output_data_got<got_size, big_endian>::Got_entry::write(
1371 Output_data_got_base* got,
1372 unsigned int got_indx,
1373 unsigned char* pov) const
1375 Valtype val = 0;
1377 switch (this->local_sym_index_)
1379 case GSYM_CODE:
1381 // If the symbol is resolved locally, we need to write out the
1382 // link-time value, which will be relocated dynamically by a
1383 // RELATIVE relocation.
1384 Symbol* gsym = this->u_.gsym;
1385 if (this->use_plt_or_tls_offset_ && gsym->has_plt_offset())
1386 val = parameters->target().plt_address_for_global(gsym);
1387 else
1389 switch (parameters->size_and_endianness())
1391 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1392 case Parameters::TARGET_32_LITTLE:
1393 case Parameters::TARGET_32_BIG:
1395 // This cast is ugly. We don't want to put a
1396 // virtual method in Symbol, because we want Symbol
1397 // to be as small as possible.
1398 Sized_symbol<32>::Value_type v;
1399 v = static_cast<Sized_symbol<32>*>(gsym)->value();
1400 val = convert_types<Valtype, Sized_symbol<32>::Value_type>(v);
1402 break;
1403 #endif
1404 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1405 case Parameters::TARGET_64_LITTLE:
1406 case Parameters::TARGET_64_BIG:
1408 Sized_symbol<64>::Value_type v;
1409 v = static_cast<Sized_symbol<64>*>(gsym)->value();
1410 val = convert_types<Valtype, Sized_symbol<64>::Value_type>(v);
1412 break;
1413 #endif
1414 default:
1415 gold_unreachable();
1417 // If this is a GOT entry for a known value global symbol,
1418 // then the value should include the addend. If the value
1419 // is not known leave the value as zero; The GOT entry
1420 // will be set by a dynamic relocation.
1421 if (this->addend_ && gsym->final_value_is_known())
1422 val += this->addend_;
1423 if (this->use_plt_or_tls_offset_
1424 && gsym->type() == elfcpp::STT_TLS)
1425 val += parameters->target().tls_offset_for_global(gsym,
1426 got, got_indx,
1427 this->addend_);
1430 break;
1432 case CONSTANT_CODE:
1433 val = this->u_.constant;
1434 break;
1436 case RESERVED_CODE:
1437 // If we're doing an incremental update, don't touch this GOT entry.
1438 if (parameters->incremental_update())
1439 return;
1440 val = this->u_.constant;
1441 break;
1443 default:
1445 const Relobj* object = this->u_.object;
1446 const unsigned int lsi = this->local_sym_index_;
1447 bool is_tls = object->local_is_tls(lsi);
1448 if (this->use_plt_or_tls_offset_ && !is_tls)
1449 val = parameters->target().plt_address_for_local(object, lsi);
1450 else
1452 uint64_t lval = object->local_symbol_value(lsi, this->addend_);
1453 val = convert_types<Valtype, uint64_t>(lval);
1454 if (this->use_plt_or_tls_offset_ && is_tls)
1455 val += parameters->target().tls_offset_for_local(object, lsi,
1456 got, got_indx,
1457 this->addend_);
1460 break;
1463 elfcpp::Swap<got_size, big_endian>::writeval(pov, val);
1466 // Output_data_got methods.
1468 // Add an entry for a global symbol to the GOT. This returns true if
1469 // this is a new GOT entry, false if the symbol already had a GOT
1470 // entry.
1472 template<int got_size, bool big_endian>
1473 bool
1474 Output_data_got<got_size, big_endian>::add_global(Symbol* gsym,
1475 unsigned int got_type,
1476 uint64_t addend)
1478 if (gsym->has_got_offset(got_type, addend))
1479 return false;
1481 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false, addend));
1482 gsym->set_got_offset(got_type, got_offset, addend);
1483 return true;
1486 // Like add_global, but use the PLT offset.
1488 template<int got_size, bool big_endian>
1489 bool
1490 Output_data_got<got_size, big_endian>::add_global_plt(Symbol* gsym,
1491 unsigned int got_type,
1492 uint64_t addend)
1494 if (gsym->has_got_offset(got_type, addend))
1495 return false;
1497 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true, addend));
1498 gsym->set_got_offset(got_type, got_offset, addend);
1499 return true;
1502 // Add an entry for a global symbol to the GOT, and add a dynamic
1503 // relocation of type R_TYPE for the GOT entry.
1505 template<int got_size, bool big_endian>
1506 void
1507 Output_data_got<got_size, big_endian>::add_global_with_rel(
1508 Symbol* gsym,
1509 unsigned int got_type,
1510 Output_data_reloc_generic* rel_dyn,
1511 unsigned int r_type,
1512 uint64_t addend)
1514 if (gsym->has_got_offset(got_type, addend))
1515 return;
1517 unsigned int got_offset = this->add_got_entry(Got_entry());
1518 gsym->set_got_offset(got_type, got_offset, addend);
1519 rel_dyn->add_global_generic(gsym, r_type, this, got_offset, addend);
1522 // Add a pair of entries for a global symbol to the GOT, and add
1523 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1524 // If R_TYPE_2 == 0, add the second entry with no relocation.
1525 template<int got_size, bool big_endian>
1526 void
1527 Output_data_got<got_size, big_endian>::add_global_pair_with_rel(
1528 Symbol* gsym,
1529 unsigned int got_type,
1530 Output_data_reloc_generic* rel_dyn,
1531 unsigned int r_type_1,
1532 unsigned int r_type_2,
1533 uint64_t addend)
1535 if (gsym->has_got_offset(got_type, addend))
1536 return;
1538 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1539 gsym->set_got_offset(got_type, got_offset, addend);
1540 rel_dyn->add_global_generic(gsym, r_type_1, this, got_offset, addend);
1542 if (r_type_2 != 0)
1543 rel_dyn->add_global_generic(gsym, r_type_2, this,
1544 got_offset + got_size / 8, addend);
1547 // Add an entry for a local symbol plus ADDEND to the GOT. This returns
1548 // true if this is a new GOT entry, false if the symbol already has a GOT
1549 // entry.
1551 template<int got_size, bool big_endian>
1552 bool
1553 Output_data_got<got_size, big_endian>::add_local(
1554 Relobj* object,
1555 unsigned int symndx,
1556 unsigned int got_type,
1557 uint64_t addend)
1559 if (object->local_has_got_offset(symndx, got_type, addend))
1560 return false;
1562 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1563 false, addend));
1564 object->set_local_got_offset(symndx, got_type, got_offset, addend);
1565 return true;
1568 // Like add_local, but use the PLT offset.
1570 template<int got_size, bool big_endian>
1571 bool
1572 Output_data_got<got_size, big_endian>::add_local_plt(
1573 Relobj* object,
1574 unsigned int symndx,
1575 unsigned int got_type,
1576 uint64_t addend)
1578 if (object->local_has_got_offset(symndx, got_type, addend))
1579 return false;
1581 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1582 true, addend));
1583 object->set_local_got_offset(symndx, got_type, got_offset, addend);
1584 return true;
1587 // Add an entry for a local symbol plus ADDEND to the GOT, and add a dynamic
1588 // relocation of type R_TYPE for the GOT entry.
1590 template<int got_size, bool big_endian>
1591 void
1592 Output_data_got<got_size, big_endian>::add_local_with_rel(
1593 Relobj* object,
1594 unsigned int symndx,
1595 unsigned int got_type,
1596 Output_data_reloc_generic* rel_dyn,
1597 unsigned int r_type,
1598 uint64_t addend)
1600 if (object->local_has_got_offset(symndx, got_type, addend))
1601 return;
1603 unsigned int got_offset = this->add_got_entry(Got_entry());
1604 object->set_local_got_offset(symndx, got_type, got_offset, addend);
1605 rel_dyn->add_local_generic(object, symndx, r_type, this, got_offset,
1606 addend);
1609 // Add a pair of entries for a local symbol plus ADDEND to the GOT, and add
1610 // a dynamic relocation of type R_TYPE using the section symbol of
1611 // the output section to which input section SHNDX maps, on the first.
1612 // The first got entry will have a value of zero, the second the
1613 // value of the local symbol.
1614 template<int got_size, bool big_endian>
1615 void
1616 Output_data_got<got_size, big_endian>::add_local_pair_with_rel(
1617 Relobj* object,
1618 unsigned int symndx,
1619 unsigned int shndx,
1620 unsigned int got_type,
1621 Output_data_reloc_generic* rel_dyn,
1622 unsigned int r_type,
1623 uint64_t addend)
1625 if (object->local_has_got_offset(symndx, got_type, addend))
1626 return;
1628 unsigned int got_offset =
1629 this->add_got_entry_pair(Got_entry(),
1630 Got_entry(object, symndx, false, addend));
1631 object->set_local_got_offset(symndx, got_type, got_offset, addend);
1632 Output_section* os = object->output_section(shndx);
1633 rel_dyn->add_output_section_generic(os, r_type, this, got_offset, addend);
1636 // Add a pair of entries for a local symbol to the GOT, and add
1637 // a dynamic relocation of type R_TYPE using STN_UNDEF on the first.
1638 // The first got entry will have a value of zero, the second the
1639 // value of the local symbol offset by Target::tls_offset_for_local.
1640 template<int got_size, bool big_endian>
1641 void
1642 Output_data_got<got_size, big_endian>::add_local_tls_pair(
1643 Relobj* object,
1644 unsigned int symndx,
1645 unsigned int got_type,
1646 Output_data_reloc_generic* rel_dyn,
1647 unsigned int r_type,
1648 uint64_t addend)
1650 if (object->local_has_got_offset(symndx, got_type, addend))
1651 return;
1653 unsigned int got_offset
1654 = this->add_got_entry_pair(Got_entry(),
1655 Got_entry(object, symndx, true, addend));
1656 object->set_local_got_offset(symndx, got_type, got_offset, addend);
1657 rel_dyn->add_local_generic(object, 0, r_type, this, got_offset, addend);
1660 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1662 template<int got_size, bool big_endian>
1663 void
1664 Output_data_got<got_size, big_endian>::reserve_local(
1665 unsigned int i,
1666 Relobj* object,
1667 unsigned int sym_index,
1668 unsigned int got_type,
1669 uint64_t addend)
1671 this->do_reserve_slot(i);
1672 object->set_local_got_offset(sym_index, got_type, this->got_offset(i), addend);
1675 // Reserve a slot in the GOT for a global symbol.
1677 template<int got_size, bool big_endian>
1678 void
1679 Output_data_got<got_size, big_endian>::reserve_global(
1680 unsigned int i,
1681 Symbol* gsym,
1682 unsigned int got_type,
1683 uint64_t addend)
1685 this->do_reserve_slot(i);
1686 gsym->set_got_offset(got_type, this->got_offset(i), addend);
1689 // Write out the GOT.
1691 template<int got_size, bool big_endian>
1692 void
1693 Output_data_got<got_size, big_endian>::do_write(Output_file* of)
1695 const int add = got_size / 8;
1697 const off_t off = this->offset();
1698 const off_t oview_size = this->data_size();
1699 unsigned char* const oview = of->get_output_view(off, oview_size);
1701 unsigned char* pov = oview;
1702 for (unsigned int i = 0; i < this->entries_.size(); ++i)
1704 this->entries_[i].write(this, i, pov);
1705 pov += add;
1708 gold_assert(pov - oview == oview_size);
1710 of->write_output_view(off, oview_size, oview);
1712 // We no longer need the GOT entries.
1713 this->entries_.clear();
1716 // Create a new GOT entry and return its offset.
1718 template<int got_size, bool big_endian>
1719 unsigned int
1720 Output_data_got<got_size, big_endian>::add_got_entry(Got_entry got_entry)
1722 if (!this->is_data_size_valid())
1724 this->entries_.push_back(got_entry);
1725 this->set_got_size();
1726 return this->last_got_offset();
1728 else
1730 // For an incremental update, find an available slot.
1731 off_t got_offset = this->free_list_.allocate(got_size / 8,
1732 got_size / 8, 0);
1733 if (got_offset == -1)
1734 gold_fallback(_("out of patch space (GOT);"
1735 " relink with --incremental-full"));
1736 unsigned int got_index = got_offset / (got_size / 8);
1737 gold_assert(got_index < this->entries_.size());
1738 this->entries_[got_index] = got_entry;
1739 return static_cast<unsigned int>(got_offset);
1743 // Create a pair of new GOT entries and return the offset of the first.
1745 template<int got_size, bool big_endian>
1746 unsigned int
1747 Output_data_got<got_size, big_endian>::add_got_entry_pair(
1748 Got_entry got_entry_1,
1749 Got_entry got_entry_2)
1751 if (!this->is_data_size_valid())
1753 unsigned int got_offset;
1754 this->entries_.push_back(got_entry_1);
1755 got_offset = this->last_got_offset();
1756 this->entries_.push_back(got_entry_2);
1757 this->set_got_size();
1758 return got_offset;
1760 else
1762 // For an incremental update, find an available pair of slots.
1763 off_t got_offset = this->free_list_.allocate(2 * got_size / 8,
1764 got_size / 8, 0);
1765 if (got_offset == -1)
1766 gold_fallback(_("out of patch space (GOT);"
1767 " relink with --incremental-full"));
1768 unsigned int got_index = got_offset / (got_size / 8);
1769 gold_assert(got_index < this->entries_.size());
1770 this->entries_[got_index] = got_entry_1;
1771 this->entries_[got_index + 1] = got_entry_2;
1772 return static_cast<unsigned int>(got_offset);
1776 // Replace GOT entry I with a new value.
1778 template<int got_size, bool big_endian>
1779 void
1780 Output_data_got<got_size, big_endian>::replace_got_entry(
1781 unsigned int i,
1782 Got_entry got_entry)
1784 gold_assert(i < this->entries_.size());
1785 this->entries_[i] = got_entry;
1788 // Output_data_dynamic::Dynamic_entry methods.
1790 // Write out the entry.
1792 template<int size, bool big_endian>
1793 void
1794 Output_data_dynamic::Dynamic_entry::write(
1795 unsigned char* pov,
1796 const Stringpool* pool) const
1798 typename elfcpp::Elf_types<size>::Elf_WXword val;
1799 switch (this->offset_)
1801 case DYNAMIC_NUMBER:
1802 val = this->u_.val;
1803 break;
1805 case DYNAMIC_SECTION_SIZE:
1806 val = this->u_.od->data_size();
1807 if (this->od2 != NULL)
1808 val += this->od2->data_size();
1809 break;
1811 case DYNAMIC_SYMBOL:
1813 const Sized_symbol<size>* s =
1814 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1815 val = s->value();
1817 break;
1819 case DYNAMIC_STRING:
1820 val = pool->get_offset(this->u_.str);
1821 break;
1823 case DYNAMIC_CUSTOM:
1824 val = parameters->target().dynamic_tag_custom_value(this->tag_);
1825 break;
1827 default:
1828 val = this->u_.od->address() + this->offset_;
1829 break;
1832 elfcpp::Dyn_write<size, big_endian> dw(pov);
1833 dw.put_d_tag(this->tag_);
1834 dw.put_d_val(val);
1837 // Output_data_dynamic methods.
1839 // Adjust the output section to set the entry size.
1841 void
1842 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1844 if (parameters->target().get_size() == 32)
1845 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1846 else if (parameters->target().get_size() == 64)
1847 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1848 else
1849 gold_unreachable();
1852 // Get a dynamic entry offset.
1854 unsigned int
1855 Output_data_dynamic::get_entry_offset(elfcpp::DT tag) const
1857 int dyn_size;
1859 if (parameters->target().get_size() == 32)
1860 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1861 else if (parameters->target().get_size() == 64)
1862 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1863 else
1864 gold_unreachable();
1866 for (size_t i = 0; i < entries_.size(); ++i)
1867 if (entries_[i].tag() == tag)
1868 return i * dyn_size;
1870 return -1U;
1873 // Set the final data size.
1875 void
1876 Output_data_dynamic::set_final_data_size()
1878 // Add the terminating entry if it hasn't been added.
1879 // Because of relaxation, we can run this multiple times.
1880 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1882 int extra = parameters->options().spare_dynamic_tags();
1883 for (int i = 0; i < extra; ++i)
1884 this->add_constant(elfcpp::DT_NULL, 0);
1885 this->add_constant(elfcpp::DT_NULL, 0);
1888 int dyn_size;
1889 if (parameters->target().get_size() == 32)
1890 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1891 else if (parameters->target().get_size() == 64)
1892 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1893 else
1894 gold_unreachable();
1895 this->set_data_size(this->entries_.size() * dyn_size);
1898 // Write out the dynamic entries.
1900 void
1901 Output_data_dynamic::do_write(Output_file* of)
1903 switch (parameters->size_and_endianness())
1905 #ifdef HAVE_TARGET_32_LITTLE
1906 case Parameters::TARGET_32_LITTLE:
1907 this->sized_write<32, false>(of);
1908 break;
1909 #endif
1910 #ifdef HAVE_TARGET_32_BIG
1911 case Parameters::TARGET_32_BIG:
1912 this->sized_write<32, true>(of);
1913 break;
1914 #endif
1915 #ifdef HAVE_TARGET_64_LITTLE
1916 case Parameters::TARGET_64_LITTLE:
1917 this->sized_write<64, false>(of);
1918 break;
1919 #endif
1920 #ifdef HAVE_TARGET_64_BIG
1921 case Parameters::TARGET_64_BIG:
1922 this->sized_write<64, true>(of);
1923 break;
1924 #endif
1925 default:
1926 gold_unreachable();
1930 template<int size, bool big_endian>
1931 void
1932 Output_data_dynamic::sized_write(Output_file* of)
1934 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1936 const off_t offset = this->offset();
1937 const off_t oview_size = this->data_size();
1938 unsigned char* const oview = of->get_output_view(offset, oview_size);
1940 unsigned char* pov = oview;
1941 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1942 p != this->entries_.end();
1943 ++p)
1945 p->write<size, big_endian>(pov, this->pool_);
1946 pov += dyn_size;
1949 gold_assert(pov - oview == oview_size);
1951 of->write_output_view(offset, oview_size, oview);
1953 // We no longer need the dynamic entries.
1954 this->entries_.clear();
1957 // Class Output_symtab_xindex.
1959 void
1960 Output_symtab_xindex::do_write(Output_file* of)
1962 const off_t offset = this->offset();
1963 const off_t oview_size = this->data_size();
1964 unsigned char* const oview = of->get_output_view(offset, oview_size);
1966 memset(oview, 0, oview_size);
1968 if (parameters->target().is_big_endian())
1969 this->endian_do_write<true>(oview);
1970 else
1971 this->endian_do_write<false>(oview);
1973 of->write_output_view(offset, oview_size, oview);
1975 // We no longer need the data.
1976 this->entries_.clear();
1979 template<bool big_endian>
1980 void
1981 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1983 for (Xindex_entries::const_iterator p = this->entries_.begin();
1984 p != this->entries_.end();
1985 ++p)
1987 unsigned int symndx = p->first;
1988 gold_assert(static_cast<off_t>(symndx) * 4 < this->data_size());
1989 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1993 // Output_fill_debug_info methods.
1995 // Return the minimum size needed for a dummy compilation unit header.
1997 size_t
1998 Output_fill_debug_info::do_minimum_hole_size() const
2000 // Compile unit header fields: unit_length, version, debug_abbrev_offset,
2001 // address_size.
2002 const size_t len = 4 + 2 + 4 + 1;
2003 // For type units, add type_signature, type_offset.
2004 if (this->is_debug_types_)
2005 return len + 8 + 4;
2006 return len;
2009 // Write a dummy compilation unit header to fill a hole in the
2010 // .debug_info or .debug_types section.
2012 void
2013 Output_fill_debug_info::do_write(Output_file* of, off_t off, size_t len) const
2015 gold_debug(DEBUG_INCREMENTAL, "fill_debug_info(%08lx, %08lx)",
2016 static_cast<long>(off), static_cast<long>(len));
2018 gold_assert(len >= this->do_minimum_hole_size());
2020 unsigned char* const oview = of->get_output_view(off, len);
2021 unsigned char* pov = oview;
2023 // Write header fields: unit_length, version, debug_abbrev_offset,
2024 // address_size.
2025 if (this->is_big_endian())
2027 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
2028 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
2029 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, 0);
2031 else
2033 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
2034 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
2035 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, 0);
2037 pov += 4 + 2 + 4;
2038 *pov++ = 4;
2040 // For type units, the additional header fields -- type_signature,
2041 // type_offset -- can be filled with zeroes.
2043 // Fill the remainder of the free space with zeroes. The first
2044 // zero should tell the consumer there are no DIEs to read in this
2045 // compilation unit.
2046 if (pov < oview + len)
2047 memset(pov, 0, oview + len - pov);
2049 of->write_output_view(off, len, oview);
2052 // Output_fill_debug_line methods.
2054 // Return the minimum size needed for a dummy line number program header.
2056 size_t
2057 Output_fill_debug_line::do_minimum_hole_size() const
2059 // Line number program header fields: unit_length, version, header_length,
2060 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2061 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2062 const size_t len = 4 + 2 + 4 + this->header_length;
2063 return len;
2066 // Write a dummy line number program header to fill a hole in the
2067 // .debug_line section.
2069 void
2070 Output_fill_debug_line::do_write(Output_file* of, off_t off, size_t len) const
2072 gold_debug(DEBUG_INCREMENTAL, "fill_debug_line(%08lx, %08lx)",
2073 static_cast<long>(off), static_cast<long>(len));
2075 gold_assert(len >= this->do_minimum_hole_size());
2077 unsigned char* const oview = of->get_output_view(off, len);
2078 unsigned char* pov = oview;
2080 // Write header fields: unit_length, version, header_length,
2081 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2082 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2083 // We set the header_length field to cover the entire hole, so the
2084 // line number program is empty.
2085 if (this->is_big_endian())
2087 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
2088 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
2089 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, len - (4 + 2 + 4));
2091 else
2093 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
2094 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
2095 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, len - (4 + 2 + 4));
2097 pov += 4 + 2 + 4;
2098 *pov++ = 1; // minimum_instruction_length
2099 *pov++ = 0; // default_is_stmt
2100 *pov++ = 0; // line_base
2101 *pov++ = 5; // line_range
2102 *pov++ = 13; // opcode_base
2103 *pov++ = 0; // standard_opcode_lengths[1]
2104 *pov++ = 1; // standard_opcode_lengths[2]
2105 *pov++ = 1; // standard_opcode_lengths[3]
2106 *pov++ = 1; // standard_opcode_lengths[4]
2107 *pov++ = 1; // standard_opcode_lengths[5]
2108 *pov++ = 0; // standard_opcode_lengths[6]
2109 *pov++ = 0; // standard_opcode_lengths[7]
2110 *pov++ = 0; // standard_opcode_lengths[8]
2111 *pov++ = 1; // standard_opcode_lengths[9]
2112 *pov++ = 0; // standard_opcode_lengths[10]
2113 *pov++ = 0; // standard_opcode_lengths[11]
2114 *pov++ = 1; // standard_opcode_lengths[12]
2115 *pov++ = 0; // include_directories (empty)
2116 *pov++ = 0; // filenames (empty)
2118 // Some consumers don't check the header_length field, and simply
2119 // start reading the line number program immediately following the
2120 // header. For those consumers, we fill the remainder of the free
2121 // space with DW_LNS_set_basic_block opcodes. These are effectively
2122 // no-ops: the resulting line table program will not create any rows.
2123 if (pov < oview + len)
2124 memset(pov, elfcpp::DW_LNS_set_basic_block, oview + len - pov);
2126 of->write_output_view(off, len, oview);
2129 // Output_section::Input_section methods.
2131 // Return the current data size. For an input section we store the size here.
2132 // For an Output_section_data, we have to ask it for the size.
2134 off_t
2135 Output_section::Input_section::current_data_size() const
2137 if (this->is_input_section())
2138 return this->u1_.data_size;
2139 else
2141 this->u2_.posd->pre_finalize_data_size();
2142 return this->u2_.posd->current_data_size();
2146 // Return the data size. For an input section we store the size here.
2147 // For an Output_section_data, we have to ask it for the size.
2149 off_t
2150 Output_section::Input_section::data_size() const
2152 if (this->is_input_section())
2153 return this->u1_.data_size;
2154 else
2155 return this->u2_.posd->data_size();
2158 // Return the object for an input section.
2160 Relobj*
2161 Output_section::Input_section::relobj() const
2163 if (this->is_input_section())
2164 return this->u2_.object;
2165 else if (this->is_merge_section())
2167 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2168 return this->u2_.pomb->first_relobj();
2170 else if (this->is_relaxed_input_section())
2171 return this->u2_.poris->relobj();
2172 else
2173 gold_unreachable();
2176 // Return the input section index for an input section.
2178 unsigned int
2179 Output_section::Input_section::shndx() const
2181 if (this->is_input_section())
2182 return this->shndx_;
2183 else if (this->is_merge_section())
2185 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2186 return this->u2_.pomb->first_shndx();
2188 else if (this->is_relaxed_input_section())
2189 return this->u2_.poris->shndx();
2190 else
2191 gold_unreachable();
2194 // Set the address and file offset.
2196 void
2197 Output_section::Input_section::set_address_and_file_offset(
2198 uint64_t address,
2199 off_t file_offset,
2200 off_t section_file_offset)
2202 if (this->is_input_section())
2203 this->u2_.object->set_section_offset(this->shndx_,
2204 file_offset - section_file_offset);
2205 else
2206 this->u2_.posd->set_address_and_file_offset(address, file_offset);
2209 // Reset the address and file offset.
2211 void
2212 Output_section::Input_section::reset_address_and_file_offset()
2214 if (!this->is_input_section())
2215 this->u2_.posd->reset_address_and_file_offset();
2218 // Finalize the data size.
2220 void
2221 Output_section::Input_section::finalize_data_size()
2223 if (!this->is_input_section())
2224 this->u2_.posd->finalize_data_size();
2227 // Try to turn an input offset into an output offset. We want to
2228 // return the output offset relative to the start of this
2229 // Input_section in the output section.
2231 inline bool
2232 Output_section::Input_section::output_offset(
2233 const Relobj* object,
2234 unsigned int shndx,
2235 section_offset_type offset,
2236 section_offset_type* poutput) const
2238 if (!this->is_input_section())
2239 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
2240 else
2242 if (this->shndx_ != shndx || this->u2_.object != object)
2243 return false;
2244 *poutput = offset;
2245 return true;
2249 // Write out the data. We don't have to do anything for an input
2250 // section--they are handled via Object::relocate--but this is where
2251 // we write out the data for an Output_section_data.
2253 void
2254 Output_section::Input_section::write(Output_file* of)
2256 if (!this->is_input_section())
2257 this->u2_.posd->write(of);
2260 // Write the data to a buffer. As for write(), we don't have to do
2261 // anything for an input section.
2263 void
2264 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2266 if (!this->is_input_section())
2267 this->u2_.posd->write_to_buffer(buffer);
2270 // Print to a map file.
2272 void
2273 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2275 switch (this->shndx_)
2277 case OUTPUT_SECTION_CODE:
2278 case MERGE_DATA_SECTION_CODE:
2279 case MERGE_STRING_SECTION_CODE:
2280 this->u2_.posd->print_to_mapfile(mapfile);
2281 break;
2283 case RELAXED_INPUT_SECTION_CODE:
2285 Output_relaxed_input_section* relaxed_section =
2286 this->relaxed_input_section();
2287 mapfile->print_input_section(relaxed_section->relobj(),
2288 relaxed_section->shndx());
2290 break;
2291 default:
2292 mapfile->print_input_section(this->u2_.object, this->shndx_);
2293 break;
2297 // Output_section methods.
2299 // Construct an Output_section. NAME will point into a Stringpool.
2301 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2302 elfcpp::Elf_Xword flags)
2303 : name_(name),
2304 addralign_(0),
2305 entsize_(0),
2306 load_address_(0),
2307 link_section_(NULL),
2308 link_(0),
2309 info_section_(NULL),
2310 info_symndx_(NULL),
2311 info_(0),
2312 type_(type),
2313 flags_(flags),
2314 order_(ORDER_INVALID),
2315 out_shndx_(-1U),
2316 symtab_index_(0),
2317 dynsym_index_(0),
2318 input_sections_(),
2319 first_input_offset_(0),
2320 fills_(),
2321 postprocessing_buffer_(NULL),
2322 needs_symtab_index_(false),
2323 needs_dynsym_index_(false),
2324 should_link_to_symtab_(false),
2325 should_link_to_dynsym_(false),
2326 after_input_sections_(false),
2327 requires_postprocessing_(false),
2328 found_in_sections_clause_(false),
2329 has_load_address_(false),
2330 info_uses_section_index_(false),
2331 input_section_order_specified_(false),
2332 may_sort_attached_input_sections_(false),
2333 must_sort_attached_input_sections_(false),
2334 attached_input_sections_are_sorted_(false),
2335 is_relro_(false),
2336 is_small_section_(false),
2337 is_large_section_(false),
2338 generate_code_fills_at_write_(false),
2339 is_entsize_zero_(false),
2340 section_offsets_need_adjustment_(false),
2341 is_noload_(false),
2342 always_keeps_input_sections_(false),
2343 has_fixed_layout_(false),
2344 is_patch_space_allowed_(false),
2345 is_unique_segment_(false),
2346 tls_offset_(0),
2347 extra_segment_flags_(0),
2348 segment_alignment_(0),
2349 checkpoint_(NULL),
2350 lookup_maps_(new Output_section_lookup_maps),
2351 free_list_(),
2352 free_space_fill_(NULL),
2353 patch_space_(0),
2354 reloc_section_(NULL)
2356 // An unallocated section has no address. Forcing this means that
2357 // we don't need special treatment for symbols defined in debug
2358 // sections.
2359 if ((flags & elfcpp::SHF_ALLOC) == 0)
2360 this->set_address(0);
2363 Output_section::~Output_section()
2365 delete this->checkpoint_;
2368 // Set the entry size.
2370 void
2371 Output_section::set_entsize(uint64_t v)
2373 if (this->is_entsize_zero_)
2375 else if (this->entsize_ == 0)
2376 this->entsize_ = v;
2377 else if (this->entsize_ != v)
2379 this->entsize_ = 0;
2380 this->is_entsize_zero_ = 1;
2384 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2385 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2386 // relocation section which applies to this section, or 0 if none, or
2387 // -1U if more than one. Return the offset of the input section
2388 // within the output section. Return -1 if the input section will
2389 // receive special handling. In the normal case we don't always keep
2390 // track of input sections for an Output_section. Instead, each
2391 // Object keeps track of the Output_section for each of its input
2392 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2393 // track of input sections here; this is used when SECTIONS appears in
2394 // a linker script.
2396 template<int size, bool big_endian>
2397 off_t
2398 Output_section::add_input_section(Layout* layout,
2399 Sized_relobj_file<size, big_endian>* object,
2400 unsigned int shndx,
2401 const char* secname,
2402 const elfcpp::Shdr<size, big_endian>& shdr,
2403 unsigned int reloc_shndx,
2404 bool have_sections_script)
2406 section_size_type input_section_size = shdr.get_sh_size();
2407 section_size_type uncompressed_size;
2408 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2409 if (object->section_is_compressed(shndx, &uncompressed_size,
2410 &addralign))
2411 input_section_size = uncompressed_size;
2413 if ((addralign & (addralign - 1)) != 0)
2415 object->error(_("invalid alignment %lu for section \"%s\""),
2416 static_cast<unsigned long>(addralign), secname);
2417 addralign = 1;
2420 if (addralign > this->addralign_)
2421 this->addralign_ = addralign;
2423 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2424 uint64_t entsize = shdr.get_sh_entsize();
2426 // .debug_str is a mergeable string section, but is not always so
2427 // marked by compilers. Mark manually here so we can optimize.
2428 if (strcmp(secname, ".debug_str") == 0)
2430 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2431 entsize = 1;
2434 this->update_flags_for_input_section(sh_flags);
2435 this->set_entsize(entsize);
2437 // If this is a SHF_MERGE section, we pass all the input sections to
2438 // a Output_data_merge. We don't try to handle relocations for such
2439 // a section. We don't try to handle empty merge sections--they
2440 // mess up the mappings, and are useless anyhow.
2441 // FIXME: Need to handle merge sections during incremental update.
2442 if ((sh_flags & elfcpp::SHF_MERGE) != 0
2443 && reloc_shndx == 0
2444 && shdr.get_sh_size() > 0
2445 && !parameters->incremental())
2447 // Keep information about merged input sections for rebuilding fast
2448 // lookup maps if we have sections-script or we do relaxation.
2449 bool keeps_input_sections = (this->always_keeps_input_sections_
2450 || have_sections_script
2451 || parameters->target().may_relax());
2453 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2454 addralign, keeps_input_sections))
2456 // Tell the relocation routines that they need to call the
2457 // output_offset method to determine the final address.
2458 return -1;
2462 off_t offset_in_section;
2464 if (this->has_fixed_layout())
2466 // For incremental updates, find a chunk of unused space in the section.
2467 offset_in_section = this->free_list_.allocate(input_section_size,
2468 addralign, 0);
2469 if (offset_in_section == -1)
2470 gold_fallback(_("out of patch space in section %s; "
2471 "relink with --incremental-full"),
2472 this->name());
2473 return offset_in_section;
2476 offset_in_section = this->current_data_size_for_child();
2477 off_t aligned_offset_in_section = align_address(offset_in_section,
2478 addralign);
2479 this->set_current_data_size_for_child(aligned_offset_in_section
2480 + input_section_size);
2482 // Determine if we want to delay code-fill generation until the output
2483 // section is written. When the target is relaxing, we want to delay fill
2484 // generating to avoid adjusting them during relaxation. Also, if we are
2485 // sorting input sections we must delay fill generation.
2486 if (!this->generate_code_fills_at_write_
2487 && !have_sections_script
2488 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2489 && parameters->target().has_code_fill()
2490 && (parameters->target().may_relax()
2491 || layout->is_section_ordering_specified()))
2493 gold_assert(this->fills_.empty());
2494 this->generate_code_fills_at_write_ = true;
2497 if (aligned_offset_in_section > offset_in_section
2498 && !this->generate_code_fills_at_write_
2499 && !have_sections_script
2500 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2501 && parameters->target().has_code_fill())
2503 // We need to add some fill data. Using fill_list_ when
2504 // possible is an optimization, since we will often have fill
2505 // sections without input sections.
2506 off_t fill_len = aligned_offset_in_section - offset_in_section;
2507 if (this->input_sections_.empty())
2508 this->fills_.push_back(Fill(offset_in_section, fill_len));
2509 else
2511 std::string fill_data(parameters->target().code_fill(fill_len));
2512 Output_data_const* odc = new Output_data_const(fill_data, 1);
2513 this->input_sections_.push_back(Input_section(odc));
2517 // We need to keep track of this section if we are already keeping
2518 // track of sections, or if we are relaxing. Also, if this is a
2519 // section which requires sorting, or which may require sorting in
2520 // the future, we keep track of the sections. If the
2521 // --section-ordering-file option is used to specify the order of
2522 // sections, we need to keep track of sections.
2523 if (this->always_keeps_input_sections_
2524 || have_sections_script
2525 || !this->input_sections_.empty()
2526 || this->may_sort_attached_input_sections()
2527 || this->must_sort_attached_input_sections()
2528 || parameters->options().user_set_Map()
2529 || parameters->target().may_relax()
2530 || layout->is_section_ordering_specified())
2532 Input_section isecn(object, shndx, input_section_size, addralign);
2533 /* If section ordering is requested by specifying a ordering file,
2534 using --section-ordering-file, match the section name with
2535 a pattern. */
2536 if (parameters->options().section_ordering_file())
2538 unsigned int section_order_index =
2539 layout->find_section_order_index(std::string(secname));
2540 if (section_order_index != 0)
2542 isecn.set_section_order_index(section_order_index);
2543 this->set_input_section_order_specified();
2546 this->input_sections_.push_back(isecn);
2549 return aligned_offset_in_section;
2552 // Add arbitrary data to an output section.
2554 void
2555 Output_section::add_output_section_data(Output_section_data* posd)
2557 Input_section inp(posd);
2558 this->add_output_section_data(&inp);
2560 if (posd->is_data_size_valid())
2562 off_t offset_in_section;
2563 if (this->has_fixed_layout())
2565 // For incremental updates, find a chunk of unused space.
2566 offset_in_section = this->free_list_.allocate(posd->data_size(),
2567 posd->addralign(), 0);
2568 if (offset_in_section == -1)
2569 gold_fallback(_("out of patch space in section %s; "
2570 "relink with --incremental-full"),
2571 this->name());
2572 // Finalize the address and offset now.
2573 uint64_t addr = this->address();
2574 off_t offset = this->offset();
2575 posd->set_address_and_file_offset(addr + offset_in_section,
2576 offset + offset_in_section);
2578 else
2580 offset_in_section = this->current_data_size_for_child();
2581 off_t aligned_offset_in_section = align_address(offset_in_section,
2582 posd->addralign());
2583 this->set_current_data_size_for_child(aligned_offset_in_section
2584 + posd->data_size());
2587 else if (this->has_fixed_layout())
2589 // For incremental updates, arrange for the data to have a fixed layout.
2590 // This will mean that additions to the data must be allocated from
2591 // free space within the containing output section.
2592 uint64_t addr = this->address();
2593 posd->set_address(addr);
2594 posd->set_file_offset(0);
2595 // FIXME: This should eventually be unreachable.
2596 // gold_unreachable();
2600 // Add a relaxed input section.
2602 void
2603 Output_section::add_relaxed_input_section(Layout* layout,
2604 Output_relaxed_input_section* poris,
2605 const std::string& name)
2607 Input_section inp(poris);
2609 // If the --section-ordering-file option is used to specify the order of
2610 // sections, we need to keep track of sections.
2611 if (layout->is_section_ordering_specified())
2613 unsigned int section_order_index =
2614 layout->find_section_order_index(name);
2615 if (section_order_index != 0)
2617 inp.set_section_order_index(section_order_index);
2618 this->set_input_section_order_specified();
2622 this->add_output_section_data(&inp);
2623 if (this->lookup_maps_->is_valid())
2624 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2625 poris->shndx(), poris);
2627 // For a relaxed section, we use the current data size. Linker scripts
2628 // get all the input sections, including relaxed one from an output
2629 // section and add them back to the same output section to compute the
2630 // output section size. If we do not account for sizes of relaxed input
2631 // sections, an output section would be incorrectly sized.
2632 off_t offset_in_section = this->current_data_size_for_child();
2633 off_t aligned_offset_in_section = align_address(offset_in_section,
2634 poris->addralign());
2635 this->set_current_data_size_for_child(aligned_offset_in_section
2636 + poris->current_data_size());
2639 // Add arbitrary data to an output section by Input_section.
2641 void
2642 Output_section::add_output_section_data(Input_section* inp)
2644 if (this->input_sections_.empty())
2645 this->first_input_offset_ = this->current_data_size_for_child();
2647 this->input_sections_.push_back(*inp);
2649 uint64_t addralign = inp->addralign();
2650 if (addralign > this->addralign_)
2651 this->addralign_ = addralign;
2653 inp->set_output_section(this);
2656 // Add a merge section to an output section.
2658 void
2659 Output_section::add_output_merge_section(Output_section_data* posd,
2660 bool is_string, uint64_t entsize)
2662 Input_section inp(posd, is_string, entsize);
2663 this->add_output_section_data(&inp);
2666 // Add an input section to a SHF_MERGE section.
2668 bool
2669 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2670 uint64_t flags, uint64_t entsize,
2671 uint64_t addralign,
2672 bool keeps_input_sections)
2674 // We cannot merge sections with entsize == 0.
2675 if (entsize == 0)
2676 return false;
2678 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2680 // We cannot restore merged input section states.
2681 gold_assert(this->checkpoint_ == NULL);
2683 // Look up merge sections by required properties.
2684 // Currently, we only invalidate the lookup maps in script processing
2685 // and relaxation. We should not have done either when we reach here.
2686 // So we assume that the lookup maps are valid to simply code.
2687 gold_assert(this->lookup_maps_->is_valid());
2688 Merge_section_properties msp(is_string, entsize, addralign);
2689 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2690 bool is_new = false;
2691 if (pomb != NULL)
2693 gold_assert(pomb->is_string() == is_string
2694 && pomb->entsize() == entsize
2695 && pomb->addralign() == addralign);
2697 else
2699 // Create a new Output_merge_data or Output_merge_string_data.
2700 if (!is_string)
2701 pomb = new Output_merge_data(entsize, addralign);
2702 else
2704 switch (entsize)
2706 case 1:
2707 pomb = new Output_merge_string<char>(addralign);
2708 break;
2709 case 2:
2710 pomb = new Output_merge_string<char16_t>(addralign);
2711 break;
2712 case 4:
2713 pomb = new Output_merge_string<char32_t>(addralign);
2714 break;
2715 default:
2716 return false;
2719 // If we need to do script processing or relaxation, we need to keep
2720 // the original input sections to rebuild the fast lookup maps.
2721 if (keeps_input_sections)
2722 pomb->set_keeps_input_sections();
2723 is_new = true;
2726 if (pomb->add_input_section(object, shndx))
2728 // Add new merge section to this output section and link merge
2729 // section properties to new merge section in map.
2730 if (is_new)
2732 this->add_output_merge_section(pomb, is_string, entsize);
2733 this->lookup_maps_->add_merge_section(msp, pomb);
2736 return true;
2738 else
2740 // If add_input_section failed, delete new merge section to avoid
2741 // exporting empty merge sections in Output_section::get_input_section.
2742 if (is_new)
2743 delete pomb;
2744 return false;
2748 // Build a relaxation map to speed up relaxation of existing input sections.
2749 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2751 void
2752 Output_section::build_relaxation_map(
2753 const Input_section_list& input_sections,
2754 size_t limit,
2755 Relaxation_map* relaxation_map) const
2757 for (size_t i = 0; i < limit; ++i)
2759 const Input_section& is(input_sections[i]);
2760 if (is.is_input_section() || is.is_relaxed_input_section())
2762 Section_id sid(is.relobj(), is.shndx());
2763 (*relaxation_map)[sid] = i;
2768 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2769 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2770 // indices of INPUT_SECTIONS.
2772 void
2773 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2774 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2775 const Relaxation_map& map,
2776 Input_section_list* input_sections)
2778 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2780 Output_relaxed_input_section* poris = relaxed_sections[i];
2781 Section_id sid(poris->relobj(), poris->shndx());
2782 Relaxation_map::const_iterator p = map.find(sid);
2783 gold_assert(p != map.end());
2784 gold_assert((*input_sections)[p->second].is_input_section());
2786 // Remember section order index of original input section
2787 // if it is set. Copy it to the relaxed input section.
2788 unsigned int soi =
2789 (*input_sections)[p->second].section_order_index();
2790 (*input_sections)[p->second] = Input_section(poris);
2791 (*input_sections)[p->second].set_section_order_index(soi);
2795 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2796 // is a vector of pointers to Output_relaxed_input_section or its derived
2797 // classes. The relaxed sections must correspond to existing input sections.
2799 void
2800 Output_section::convert_input_sections_to_relaxed_sections(
2801 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2803 gold_assert(parameters->target().may_relax());
2805 // We want to make sure that restore_states does not undo the effect of
2806 // this. If there is no checkpoint active, just search the current
2807 // input section list and replace the sections there. If there is
2808 // a checkpoint, also replace the sections there.
2810 // By default, we look at the whole list.
2811 size_t limit = this->input_sections_.size();
2813 if (this->checkpoint_ != NULL)
2815 // Replace input sections with relaxed input section in the saved
2816 // copy of the input section list.
2817 if (this->checkpoint_->input_sections_saved())
2819 Relaxation_map map;
2820 this->build_relaxation_map(
2821 *(this->checkpoint_->input_sections()),
2822 this->checkpoint_->input_sections()->size(),
2823 &map);
2824 this->convert_input_sections_in_list_to_relaxed_sections(
2825 relaxed_sections,
2826 map,
2827 this->checkpoint_->input_sections());
2829 else
2831 // We have not copied the input section list yet. Instead, just
2832 // look at the portion that would be saved.
2833 limit = this->checkpoint_->input_sections_size();
2837 // Convert input sections in input_section_list.
2838 Relaxation_map map;
2839 this->build_relaxation_map(this->input_sections_, limit, &map);
2840 this->convert_input_sections_in_list_to_relaxed_sections(
2841 relaxed_sections,
2842 map,
2843 &this->input_sections_);
2845 // Update fast look-up map.
2846 if (this->lookup_maps_->is_valid())
2847 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2849 Output_relaxed_input_section* poris = relaxed_sections[i];
2850 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2851 poris->shndx(), poris);
2855 // Update the output section flags based on input section flags.
2857 void
2858 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2860 // If we created the section with SHF_ALLOC clear, we set the
2861 // address. If we are now setting the SHF_ALLOC flag, we need to
2862 // undo that.
2863 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2864 && (flags & elfcpp::SHF_ALLOC) != 0)
2865 this->mark_address_invalid();
2867 this->flags_ |= (flags
2868 & (elfcpp::SHF_WRITE
2869 | elfcpp::SHF_ALLOC
2870 | elfcpp::SHF_EXECINSTR));
2872 if ((flags & elfcpp::SHF_MERGE) == 0)
2873 this->flags_ &=~ elfcpp::SHF_MERGE;
2874 else
2876 if (this->current_data_size_for_child() == 0)
2877 this->flags_ |= elfcpp::SHF_MERGE;
2880 if ((flags & elfcpp::SHF_STRINGS) == 0)
2881 this->flags_ &=~ elfcpp::SHF_STRINGS;
2882 else
2884 if (this->current_data_size_for_child() == 0)
2885 this->flags_ |= elfcpp::SHF_STRINGS;
2889 // Find the merge section into which an input section with index SHNDX in
2890 // OBJECT has been added. Return NULL if none found.
2892 const Output_section_data*
2893 Output_section::find_merge_section(const Relobj* object,
2894 unsigned int shndx) const
2896 return object->find_merge_section(shndx);
2899 // Build the lookup maps for relaxed sections. This needs
2900 // to be declared as a const method so that it is callable with a const
2901 // Output_section pointer. The method only updates states of the maps.
2903 void
2904 Output_section::build_lookup_maps() const
2906 this->lookup_maps_->clear();
2907 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2908 p != this->input_sections_.end();
2909 ++p)
2911 if (p->is_relaxed_input_section())
2913 Output_relaxed_input_section* poris = p->relaxed_input_section();
2914 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2915 poris->shndx(), poris);
2920 // Find an relaxed input section corresponding to an input section
2921 // in OBJECT with index SHNDX.
2923 const Output_relaxed_input_section*
2924 Output_section::find_relaxed_input_section(const Relobj* object,
2925 unsigned int shndx) const
2927 if (!this->lookup_maps_->is_valid())
2928 this->build_lookup_maps();
2929 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2932 // Given an address OFFSET relative to the start of input section
2933 // SHNDX in OBJECT, return whether this address is being included in
2934 // the final link. This should only be called if SHNDX in OBJECT has
2935 // a special mapping.
2937 bool
2938 Output_section::is_input_address_mapped(const Relobj* object,
2939 unsigned int shndx,
2940 off_t offset) const
2942 // Look at the Output_section_data_maps first.
2943 const Output_section_data* posd = this->find_merge_section(object, shndx);
2944 if (posd == NULL)
2945 posd = this->find_relaxed_input_section(object, shndx);
2947 if (posd != NULL)
2949 section_offset_type output_offset;
2950 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2951 // By default we assume that the address is mapped. See comment at the
2952 // end.
2953 if (!found)
2954 return true;
2955 return output_offset != -1;
2958 // Fall back to the slow look-up.
2959 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2960 p != this->input_sections_.end();
2961 ++p)
2963 section_offset_type output_offset;
2964 if (p->output_offset(object, shndx, offset, &output_offset))
2965 return output_offset != -1;
2968 // By default we assume that the address is mapped. This should
2969 // only be called after we have passed all sections to Layout. At
2970 // that point we should know what we are discarding.
2971 return true;
2974 // Given an address OFFSET relative to the start of input section
2975 // SHNDX in object OBJECT, return the output offset relative to the
2976 // start of the input section in the output section. This should only
2977 // be called if SHNDX in OBJECT has a special mapping.
2979 section_offset_type
2980 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2981 section_offset_type offset) const
2983 // This can only be called meaningfully when we know the data size
2984 // of this.
2985 gold_assert(this->is_data_size_valid());
2987 // Look at the Output_section_data_maps first.
2988 const Output_section_data* posd = this->find_merge_section(object, shndx);
2989 if (posd == NULL)
2990 posd = this->find_relaxed_input_section(object, shndx);
2991 if (posd != NULL)
2993 section_offset_type output_offset;
2994 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2995 gold_assert(found);
2996 return output_offset;
2999 // Fall back to the slow look-up.
3000 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3001 p != this->input_sections_.end();
3002 ++p)
3004 section_offset_type output_offset;
3005 if (p->output_offset(object, shndx, offset, &output_offset))
3006 return output_offset;
3008 gold_unreachable();
3011 // Return the output virtual address of OFFSET relative to the start
3012 // of input section SHNDX in object OBJECT.
3014 uint64_t
3015 Output_section::output_address(const Relobj* object, unsigned int shndx,
3016 off_t offset) const
3018 uint64_t addr = this->address() + this->first_input_offset_;
3020 // Look at the Output_section_data_maps first.
3021 const Output_section_data* posd = this->find_merge_section(object, shndx);
3022 if (posd == NULL)
3023 posd = this->find_relaxed_input_section(object, shndx);
3024 if (posd != NULL && posd->is_address_valid())
3026 section_offset_type output_offset;
3027 bool found = posd->output_offset(object, shndx, offset, &output_offset);
3028 gold_assert(found);
3029 return posd->address() + output_offset;
3032 // Fall back to the slow look-up.
3033 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3034 p != this->input_sections_.end();
3035 ++p)
3037 addr = align_address(addr, p->addralign());
3038 section_offset_type output_offset;
3039 if (p->output_offset(object, shndx, offset, &output_offset))
3041 if (output_offset == -1)
3042 return -1ULL;
3043 return addr + output_offset;
3045 addr += p->data_size();
3048 // If we get here, it means that we don't know the mapping for this
3049 // input section. This might happen in principle if
3050 // add_input_section were called before add_output_section_data.
3051 // But it should never actually happen.
3053 gold_unreachable();
3056 // Find the output address of the start of the merged section for
3057 // input section SHNDX in object OBJECT.
3059 bool
3060 Output_section::find_starting_output_address(const Relobj* object,
3061 unsigned int shndx,
3062 uint64_t* paddr) const
3064 const Output_section_data* data = this->find_merge_section(object, shndx);
3065 if (data == NULL)
3066 return false;
3068 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
3069 // Looking up the merge section map does not always work as we sometimes
3070 // find a merge section without its address set.
3071 uint64_t addr = this->address() + this->first_input_offset_;
3072 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3073 p != this->input_sections_.end();
3074 ++p)
3076 addr = align_address(addr, p->addralign());
3078 // It would be nice if we could use the existing output_offset
3079 // method to get the output offset of input offset 0.
3080 // Unfortunately we don't know for sure that input offset 0 is
3081 // mapped at all.
3082 if (!p->is_input_section() && p->output_section_data() == data)
3084 *paddr = addr;
3085 return true;
3088 addr += p->data_size();
3091 // We couldn't find a merge output section for this input section.
3092 return false;
3095 // Update the data size of an Output_section.
3097 void
3098 Output_section::update_data_size()
3100 if (this->input_sections_.empty())
3101 return;
3103 if (this->must_sort_attached_input_sections()
3104 || this->input_section_order_specified())
3105 this->sort_attached_input_sections();
3107 off_t off = this->first_input_offset_;
3108 for (Input_section_list::iterator p = this->input_sections_.begin();
3109 p != this->input_sections_.end();
3110 ++p)
3112 off = align_address(off, p->addralign());
3113 off += p->current_data_size();
3116 this->set_current_data_size_for_child(off);
3119 // Set the data size of an Output_section. This is where we handle
3120 // setting the addresses of any Output_section_data objects.
3122 void
3123 Output_section::set_final_data_size()
3125 off_t data_size;
3127 if (this->input_sections_.empty())
3128 data_size = this->current_data_size_for_child();
3129 else
3131 if (this->must_sort_attached_input_sections()
3132 || this->input_section_order_specified())
3133 this->sort_attached_input_sections();
3135 uint64_t address = this->address();
3136 off_t startoff = this->offset();
3137 off_t off = this->first_input_offset_;
3138 for (Input_section_list::iterator p = this->input_sections_.begin();
3139 p != this->input_sections_.end();
3140 ++p)
3142 off = align_address(off, p->addralign());
3143 p->set_address_and_file_offset(address + off, startoff + off,
3144 startoff);
3145 off += p->data_size();
3147 data_size = off;
3150 // For full incremental links, we want to allocate some patch space
3151 // in most sections for subsequent incremental updates.
3152 if (this->is_patch_space_allowed_ && parameters->incremental_full())
3154 double pct = parameters->options().incremental_patch();
3155 size_t extra = static_cast<size_t>(data_size * pct);
3156 if (this->free_space_fill_ != NULL
3157 && this->free_space_fill_->minimum_hole_size() > extra)
3158 extra = this->free_space_fill_->minimum_hole_size();
3159 off_t new_size = align_address(data_size + extra, this->addralign());
3160 this->patch_space_ = new_size - data_size;
3161 gold_debug(DEBUG_INCREMENTAL,
3162 "set_final_data_size: %08lx + %08lx: section %s",
3163 static_cast<long>(data_size),
3164 static_cast<long>(this->patch_space_),
3165 this->name());
3166 data_size = new_size;
3169 this->set_data_size(data_size);
3172 // Reset the address and file offset.
3174 void
3175 Output_section::do_reset_address_and_file_offset()
3177 // An unallocated section has no address. Forcing this means that
3178 // we don't need special treatment for symbols defined in debug
3179 // sections. We do the same in the constructor. This does not
3180 // apply to NOLOAD sections though.
3181 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
3182 this->set_address(0);
3184 for (Input_section_list::iterator p = this->input_sections_.begin();
3185 p != this->input_sections_.end();
3186 ++p)
3187 p->reset_address_and_file_offset();
3189 // Remove any patch space that was added in set_final_data_size.
3190 if (this->patch_space_ > 0)
3192 this->set_current_data_size_for_child(this->current_data_size_for_child()
3193 - this->patch_space_);
3194 this->patch_space_ = 0;
3198 // Return true if address and file offset have the values after reset.
3200 bool
3201 Output_section::do_address_and_file_offset_have_reset_values() const
3203 if (this->is_offset_valid())
3204 return false;
3206 // An unallocated section has address 0 after its construction or a reset.
3207 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
3208 return this->is_address_valid() && this->address() == 0;
3209 else
3210 return !this->is_address_valid();
3213 // Set the TLS offset. Called only for SHT_TLS sections.
3215 void
3216 Output_section::do_set_tls_offset(uint64_t tls_base)
3218 this->tls_offset_ = this->address() - tls_base;
3221 // In a few cases we need to sort the input sections attached to an
3222 // output section. This is used to implement the type of constructor
3223 // priority ordering implemented by the GNU linker, in which the
3224 // priority becomes part of the section name and the sections are
3225 // sorted by name. We only do this for an output section if we see an
3226 // attached input section matching ".ctors.*", ".dtors.*",
3227 // ".init_array.*" or ".fini_array.*".
3229 class Output_section::Input_section_sort_entry
3231 public:
3232 Input_section_sort_entry()
3233 : input_section_(), index_(-1U), section_name_()
3236 Input_section_sort_entry(const Input_section& input_section,
3237 unsigned int index,
3238 bool must_sort_attached_input_sections,
3239 const char* output_section_name)
3240 : input_section_(input_section), index_(index), section_name_()
3242 if ((input_section.is_input_section()
3243 || input_section.is_relaxed_input_section())
3244 && must_sort_attached_input_sections)
3246 // This is only called single-threaded from Layout::finalize,
3247 // so it is OK to lock. Unfortunately we have no way to pass
3248 // in a Task token.
3249 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
3250 Object* obj = (input_section.is_input_section()
3251 ? input_section.relobj()
3252 : input_section.relaxed_input_section()->relobj());
3253 Task_lock_obj<Object> tl(dummy_task, obj);
3255 // This is a slow operation, which should be cached in
3256 // Layout::layout if this becomes a speed problem.
3257 this->section_name_ = obj->section_name(input_section.shndx());
3259 else if (input_section.is_output_section_data()
3260 && must_sort_attached_input_sections)
3262 // For linker-generated sections, use the output section name.
3263 this->section_name_.assign(output_section_name);
3267 // Return the Input_section.
3268 const Input_section&
3269 input_section() const
3271 gold_assert(this->index_ != -1U);
3272 return this->input_section_;
3275 // The index of this entry in the original list. This is used to
3276 // make the sort stable.
3277 unsigned int
3278 index() const
3280 gold_assert(this->index_ != -1U);
3281 return this->index_;
3284 // The section name.
3285 const std::string&
3286 section_name() const
3288 return this->section_name_;
3291 // Return true if the section name has a priority. This is assumed
3292 // to be true if it has a dot after the initial dot.
3293 bool
3294 has_priority() const
3296 return this->section_name_.find('.', 1) != std::string::npos;
3299 // Return the priority. Believe it or not, gcc encodes the priority
3300 // differently for .ctors/.dtors and .init_array/.fini_array
3301 // sections.
3302 unsigned int
3303 get_priority() const
3305 bool is_ctors;
3306 if (is_prefix_of(".ctors.", this->section_name_.c_str())
3307 || is_prefix_of(".dtors.", this->section_name_.c_str()))
3308 is_ctors = true;
3309 else if (is_prefix_of(".init_array.", this->section_name_.c_str())
3310 || is_prefix_of(".fini_array.", this->section_name_.c_str()))
3311 is_ctors = false;
3312 else
3313 return 0;
3314 char* end;
3315 unsigned long prio = strtoul((this->section_name_.c_str()
3316 + (is_ctors ? 7 : 12)),
3317 &end, 10);
3318 if (*end != '\0')
3319 return 0;
3320 else if (is_ctors)
3321 return 65535 - prio;
3322 else
3323 return prio;
3326 // Return true if this an input file whose base name matches
3327 // FILE_NAME. The base name must have an extension of ".o", and
3328 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3329 // This is to match crtbegin.o as well as crtbeginS.o without
3330 // getting confused by other possibilities. Overall matching the
3331 // file name this way is a dreadful hack, but the GNU linker does it
3332 // in order to better support gcc, and we need to be compatible.
3333 bool
3334 match_file_name(const char* file_name) const
3336 if (this->input_section_.is_output_section_data())
3337 return false;
3338 return Layout::match_file_name(this->input_section_.relobj(), file_name);
3341 // Returns 1 if THIS should appear before S in section order, -1 if S
3342 // appears before THIS and 0 if they are not comparable.
3344 compare_section_ordering(const Input_section_sort_entry& s) const
3346 unsigned int this_secn_index = this->input_section_.section_order_index();
3347 unsigned int s_secn_index = s.input_section().section_order_index();
3348 if (this_secn_index > 0 && s_secn_index > 0)
3350 if (this_secn_index < s_secn_index)
3351 return 1;
3352 else if (this_secn_index > s_secn_index)
3353 return -1;
3355 return 0;
3358 private:
3359 // The Input_section we are sorting.
3360 Input_section input_section_;
3361 // The index of this Input_section in the original list.
3362 unsigned int index_;
3363 // The section name if there is one.
3364 std::string section_name_;
3367 // Return true if S1 should come before S2 in the output section.
3369 bool
3370 Output_section::Input_section_sort_compare::operator()(
3371 const Output_section::Input_section_sort_entry& s1,
3372 const Output_section::Input_section_sort_entry& s2) const
3374 // crtbegin.o must come first.
3375 bool s1_begin = s1.match_file_name("crtbegin");
3376 bool s2_begin = s2.match_file_name("crtbegin");
3377 if (s1_begin || s2_begin)
3379 if (!s1_begin)
3380 return false;
3381 if (!s2_begin)
3382 return true;
3383 return s1.index() < s2.index();
3386 // crtend.o must come last.
3387 bool s1_end = s1.match_file_name("crtend");
3388 bool s2_end = s2.match_file_name("crtend");
3389 if (s1_end || s2_end)
3391 if (!s1_end)
3392 return true;
3393 if (!s2_end)
3394 return false;
3395 return s1.index() < s2.index();
3398 // A section with a priority follows a section without a priority.
3399 bool s1_has_priority = s1.has_priority();
3400 bool s2_has_priority = s2.has_priority();
3401 if (s1_has_priority && !s2_has_priority)
3402 return false;
3403 if (!s1_has_priority && s2_has_priority)
3404 return true;
3406 // Check if a section order exists for these sections through a section
3407 // ordering file. If sequence_num is 0, an order does not exist.
3408 int sequence_num = s1.compare_section_ordering(s2);
3409 if (sequence_num != 0)
3410 return sequence_num == 1;
3412 // Otherwise we sort by name.
3413 int compare = s1.section_name().compare(s2.section_name());
3414 if (compare != 0)
3415 return compare < 0;
3417 // Otherwise we keep the input order.
3418 return s1.index() < s2.index();
3421 // Return true if S1 should come before S2 in an .init_array or .fini_array
3422 // output section.
3424 bool
3425 Output_section::Input_section_sort_init_fini_compare::operator()(
3426 const Output_section::Input_section_sort_entry& s1,
3427 const Output_section::Input_section_sort_entry& s2) const
3429 // A section without a priority follows a section with a priority.
3430 // This is the reverse of .ctors and .dtors sections.
3431 bool s1_has_priority = s1.has_priority();
3432 bool s2_has_priority = s2.has_priority();
3433 if (s1_has_priority && !s2_has_priority)
3434 return true;
3435 if (!s1_has_priority && s2_has_priority)
3436 return false;
3438 // .ctors and .dtors sections without priority come after
3439 // .init_array and .fini_array sections without priority.
3440 if (!s1_has_priority
3441 && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors")
3442 && s1.section_name() != s2.section_name())
3443 return false;
3444 if (!s2_has_priority
3445 && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors")
3446 && s2.section_name() != s1.section_name())
3447 return true;
3449 // Sort by priority if we can.
3450 if (s1_has_priority)
3452 unsigned int s1_prio = s1.get_priority();
3453 unsigned int s2_prio = s2.get_priority();
3454 if (s1_prio < s2_prio)
3455 return true;
3456 else if (s1_prio > s2_prio)
3457 return false;
3460 // Check if a section order exists for these sections through a section
3461 // ordering file. If sequence_num is 0, an order does not exist.
3462 int sequence_num = s1.compare_section_ordering(s2);
3463 if (sequence_num != 0)
3464 return sequence_num == 1;
3466 // Otherwise we sort by name.
3467 int compare = s1.section_name().compare(s2.section_name());
3468 if (compare != 0)
3469 return compare < 0;
3471 // Otherwise we keep the input order.
3472 return s1.index() < s2.index();
3475 // Return true if S1 should come before S2. Sections that do not match
3476 // any pattern in the section ordering file are placed ahead of the sections
3477 // that match some pattern.
3479 bool
3480 Output_section::Input_section_sort_section_order_index_compare::operator()(
3481 const Output_section::Input_section_sort_entry& s1,
3482 const Output_section::Input_section_sort_entry& s2) const
3484 unsigned int s1_secn_index = s1.input_section().section_order_index();
3485 unsigned int s2_secn_index = s2.input_section().section_order_index();
3487 // Keep input order if section ordering cannot determine order.
3488 if (s1_secn_index == s2_secn_index)
3489 return s1.index() < s2.index();
3491 return s1_secn_index < s2_secn_index;
3494 // Return true if S1 should come before S2. This is the sort comparison
3495 // function for .text to sort sections with prefixes
3496 // .text.{unlikely,exit,startup,hot} before other sections.
3498 bool
3499 Output_section::Input_section_sort_section_prefix_special_ordering_compare
3500 ::operator()(
3501 const Output_section::Input_section_sort_entry& s1,
3502 const Output_section::Input_section_sort_entry& s2) const
3504 // Some input section names have special ordering requirements.
3505 const char *s1_section_name = s1.section_name().c_str();
3506 const char *s2_section_name = s2.section_name().c_str();
3507 int o1 = Layout::special_ordering_of_input_section(s1_section_name);
3508 int o2 = Layout::special_ordering_of_input_section(s2_section_name);
3509 if (o1 != o2)
3511 if (o1 < 0)
3512 return false;
3513 else if (o2 < 0)
3514 return true;
3515 else
3516 return o1 < o2;
3518 else if (is_prefix_of(".text.sorted", s1_section_name))
3519 return strcmp(s1_section_name, s2_section_name) <= 0;
3521 // Keep input order otherwise.
3522 return s1.index() < s2.index();
3525 // Return true if S1 should come before S2. This is the sort comparison
3526 // function for sections to sort them by name.
3528 bool
3529 Output_section::Input_section_sort_section_name_compare
3530 ::operator()(
3531 const Output_section::Input_section_sort_entry& s1,
3532 const Output_section::Input_section_sort_entry& s2) const
3534 // We sort by name.
3535 int compare = s1.section_name().compare(s2.section_name());
3536 if (compare != 0)
3537 return compare < 0;
3539 // Keep input order otherwise.
3540 return s1.index() < s2.index();
3543 // This updates the section order index of input sections according to the
3544 // the order specified in the mapping from Section id to order index.
3546 void
3547 Output_section::update_section_layout(
3548 const Section_layout_order* order_map)
3550 for (Input_section_list::iterator p = this->input_sections_.begin();
3551 p != this->input_sections_.end();
3552 ++p)
3554 if (p->is_input_section()
3555 || p->is_relaxed_input_section())
3557 Relobj* obj = (p->is_input_section()
3558 ? p->relobj()
3559 : p->relaxed_input_section()->relobj());
3560 unsigned int shndx = p->shndx();
3561 Section_layout_order::const_iterator it
3562 = order_map->find(Section_id(obj, shndx));
3563 if (it == order_map->end())
3564 continue;
3565 unsigned int section_order_index = it->second;
3566 if (section_order_index != 0)
3568 p->set_section_order_index(section_order_index);
3569 this->set_input_section_order_specified();
3575 // Sort the input sections attached to an output section.
3577 void
3578 Output_section::sort_attached_input_sections()
3580 if (this->attached_input_sections_are_sorted_)
3581 return;
3583 if (this->checkpoint_ != NULL
3584 && !this->checkpoint_->input_sections_saved())
3585 this->checkpoint_->save_input_sections();
3587 // The only thing we know about an input section is the object and
3588 // the section index. We need the section name. Recomputing this
3589 // is slow but this is an unusual case. If this becomes a speed
3590 // problem we can cache the names as required in Layout::layout.
3592 // We start by building a larger vector holding a copy of each
3593 // Input_section, plus its current index in the list and its name.
3594 std::vector<Input_section_sort_entry> sort_list;
3596 unsigned int i = 0;
3597 for (Input_section_list::iterator p = this->input_sections_.begin();
3598 p != this->input_sections_.end();
3599 ++p, ++i)
3600 sort_list.push_back(Input_section_sort_entry(*p, i,
3601 this->must_sort_attached_input_sections(),
3602 this->name()));
3604 // Sort the input sections.
3605 if (this->must_sort_attached_input_sections())
3607 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3608 || this->type() == elfcpp::SHT_INIT_ARRAY
3609 || this->type() == elfcpp::SHT_FINI_ARRAY)
3610 std::sort(sort_list.begin(), sort_list.end(),
3611 Input_section_sort_init_fini_compare());
3612 else if (strcmp(parameters->options().sort_section(), "name") == 0)
3613 std::sort(sort_list.begin(), sort_list.end(),
3614 Input_section_sort_section_name_compare());
3615 else if (strcmp(this->name(), ".text") == 0)
3616 std::sort(sort_list.begin(), sort_list.end(),
3617 Input_section_sort_section_prefix_special_ordering_compare());
3618 else
3619 std::sort(sort_list.begin(), sort_list.end(),
3620 Input_section_sort_compare());
3622 else
3624 gold_assert(this->input_section_order_specified());
3625 std::sort(sort_list.begin(), sort_list.end(),
3626 Input_section_sort_section_order_index_compare());
3629 // Copy the sorted input sections back to our list.
3630 this->input_sections_.clear();
3631 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3632 p != sort_list.end();
3633 ++p)
3634 this->input_sections_.push_back(p->input_section());
3635 sort_list.clear();
3637 // Remember that we sorted the input sections, since we might get
3638 // called again.
3639 this->attached_input_sections_are_sorted_ = true;
3642 // Write the section header to *OSHDR.
3644 template<int size, bool big_endian>
3645 void
3646 Output_section::write_header(const Layout* layout,
3647 const Stringpool* secnamepool,
3648 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3650 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3651 oshdr->put_sh_type(this->type_);
3653 elfcpp::Elf_Xword flags = this->flags_;
3654 if (this->info_section_ != NULL && this->info_uses_section_index_)
3655 flags |= elfcpp::SHF_INFO_LINK;
3656 oshdr->put_sh_flags(flags);
3658 oshdr->put_sh_addr(this->address());
3659 oshdr->put_sh_offset(this->offset());
3660 oshdr->put_sh_size(this->data_size());
3661 if (this->link_section_ != NULL)
3662 oshdr->put_sh_link(this->link_section_->out_shndx());
3663 else if (this->should_link_to_symtab_)
3664 oshdr->put_sh_link(layout->symtab_section_shndx());
3665 else if (this->should_link_to_dynsym_)
3666 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3667 else
3668 oshdr->put_sh_link(this->link_);
3670 elfcpp::Elf_Word info;
3671 if (this->info_section_ != NULL)
3673 if (this->info_uses_section_index_)
3674 info = this->info_section_->out_shndx();
3675 else
3676 info = this->info_section_->symtab_index();
3678 else if (this->info_symndx_ != NULL)
3679 info = this->info_symndx_->symtab_index();
3680 else
3681 info = this->info_;
3682 oshdr->put_sh_info(info);
3684 oshdr->put_sh_addralign(this->addralign_);
3685 oshdr->put_sh_entsize(this->entsize_);
3688 // Write out the data. For input sections the data is written out by
3689 // Object::relocate, but we have to handle Output_section_data objects
3690 // here.
3692 void
3693 Output_section::do_write(Output_file* of)
3695 gold_assert(!this->requires_postprocessing());
3697 // If the target performs relaxation, we delay filler generation until now.
3698 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3700 off_t output_section_file_offset = this->offset();
3701 for (Fill_list::iterator p = this->fills_.begin();
3702 p != this->fills_.end();
3703 ++p)
3705 std::string fill_data(parameters->target().code_fill(p->length()));
3706 of->write(output_section_file_offset + p->section_offset(),
3707 fill_data.data(), fill_data.size());
3710 off_t off = this->offset() + this->first_input_offset_;
3711 for (Input_section_list::iterator p = this->input_sections_.begin();
3712 p != this->input_sections_.end();
3713 ++p)
3715 off_t aligned_off = align_address(off, p->addralign());
3716 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3718 size_t fill_len = aligned_off - off;
3719 std::string fill_data(parameters->target().code_fill(fill_len));
3720 of->write(off, fill_data.data(), fill_data.size());
3723 p->write(of);
3724 off = aligned_off + p->data_size();
3727 // For incremental links, fill in unused chunks in debug sections
3728 // with dummy compilation unit headers.
3729 if (this->free_space_fill_ != NULL)
3731 for (Free_list::Const_iterator p = this->free_list_.begin();
3732 p != this->free_list_.end();
3733 ++p)
3735 off_t off = p->start_;
3736 size_t len = p->end_ - off;
3737 this->free_space_fill_->write(of, this->offset() + off, len);
3739 if (this->patch_space_ > 0)
3741 off_t off = this->current_data_size_for_child() - this->patch_space_;
3742 this->free_space_fill_->write(of, this->offset() + off,
3743 this->patch_space_);
3748 // If a section requires postprocessing, create the buffer to use.
3750 void
3751 Output_section::create_postprocessing_buffer()
3753 gold_assert(this->requires_postprocessing());
3755 if (this->postprocessing_buffer_ != NULL)
3756 return;
3758 if (!this->input_sections_.empty())
3760 off_t off = this->first_input_offset_;
3761 for (Input_section_list::iterator p = this->input_sections_.begin();
3762 p != this->input_sections_.end();
3763 ++p)
3765 off = align_address(off, p->addralign());
3766 p->finalize_data_size();
3767 off += p->data_size();
3769 this->set_current_data_size_for_child(off);
3772 off_t buffer_size = this->current_data_size_for_child();
3773 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3776 // Write all the data of an Output_section into the postprocessing
3777 // buffer. This is used for sections which require postprocessing,
3778 // such as compression. Input sections are handled by
3779 // Object::Relocate.
3781 void
3782 Output_section::write_to_postprocessing_buffer()
3784 gold_assert(this->requires_postprocessing());
3786 // If the target performs relaxation, we delay filler generation until now.
3787 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3789 unsigned char* buffer = this->postprocessing_buffer();
3790 for (Fill_list::iterator p = this->fills_.begin();
3791 p != this->fills_.end();
3792 ++p)
3794 std::string fill_data(parameters->target().code_fill(p->length()));
3795 memcpy(buffer + p->section_offset(), fill_data.data(),
3796 fill_data.size());
3799 off_t off = this->first_input_offset_;
3800 for (Input_section_list::iterator p = this->input_sections_.begin();
3801 p != this->input_sections_.end();
3802 ++p)
3804 off_t aligned_off = align_address(off, p->addralign());
3805 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3807 size_t fill_len = aligned_off - off;
3808 std::string fill_data(parameters->target().code_fill(fill_len));
3809 memcpy(buffer + off, fill_data.data(), fill_data.size());
3812 p->write_to_buffer(buffer + aligned_off);
3813 off = aligned_off + p->data_size();
3817 // Get the input sections for linker script processing. We leave
3818 // behind the Output_section_data entries. Note that this may be
3819 // slightly incorrect for merge sections. We will leave them behind,
3820 // but it is possible that the script says that they should follow
3821 // some other input sections, as in:
3822 // .rodata { *(.rodata) *(.rodata.cst*) }
3823 // For that matter, we don't handle this correctly:
3824 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3825 // With luck this will never matter.
3827 uint64_t
3828 Output_section::get_input_sections(
3829 uint64_t address,
3830 const std::string& fill,
3831 std::list<Input_section>* input_sections)
3833 if (this->checkpoint_ != NULL
3834 && !this->checkpoint_->input_sections_saved())
3835 this->checkpoint_->save_input_sections();
3837 // Invalidate fast look-up maps.
3838 this->lookup_maps_->invalidate();
3840 uint64_t orig_address = address;
3842 address = align_address(address, this->addralign());
3844 Input_section_list remaining;
3845 for (Input_section_list::iterator p = this->input_sections_.begin();
3846 p != this->input_sections_.end();
3847 ++p)
3849 if (p->is_input_section()
3850 || p->is_relaxed_input_section()
3851 || p->is_merge_section())
3852 input_sections->push_back(*p);
3853 else
3855 uint64_t aligned_address = align_address(address, p->addralign());
3856 if (aligned_address != address && !fill.empty())
3858 section_size_type length =
3859 convert_to_section_size_type(aligned_address - address);
3860 std::string this_fill;
3861 this_fill.reserve(length);
3862 while (this_fill.length() + fill.length() <= length)
3863 this_fill += fill;
3864 if (this_fill.length() < length)
3865 this_fill.append(fill, 0, length - this_fill.length());
3867 Output_section_data* posd = new Output_data_const(this_fill, 0);
3868 remaining.push_back(Input_section(posd));
3870 address = aligned_address;
3872 remaining.push_back(*p);
3874 p->finalize_data_size();
3875 address += p->data_size();
3879 this->input_sections_.swap(remaining);
3880 this->first_input_offset_ = 0;
3882 uint64_t data_size = address - orig_address;
3883 this->set_current_data_size_for_child(data_size);
3884 return data_size;
3887 // Add a script input section. SIS is an Output_section::Input_section,
3888 // which can be either a plain input section or a special input section like
3889 // a relaxed input section. For a special input section, its size must be
3890 // finalized.
3892 void
3893 Output_section::add_script_input_section(const Input_section& sis)
3895 uint64_t data_size = sis.data_size();
3896 uint64_t addralign = sis.addralign();
3897 if (addralign > this->addralign_)
3898 this->addralign_ = addralign;
3900 off_t offset_in_section = this->current_data_size_for_child();
3901 off_t aligned_offset_in_section = align_address(offset_in_section,
3902 addralign);
3904 this->set_current_data_size_for_child(aligned_offset_in_section
3905 + data_size);
3907 this->input_sections_.push_back(sis);
3909 // Update fast lookup maps if necessary.
3910 if (this->lookup_maps_->is_valid())
3912 if (sis.is_relaxed_input_section())
3914 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3915 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3916 poris->shndx(), poris);
3921 // Save states for relaxation.
3923 void
3924 Output_section::save_states()
3926 gold_assert(this->checkpoint_ == NULL);
3927 Checkpoint_output_section* checkpoint =
3928 new Checkpoint_output_section(this->addralign_, this->flags_,
3929 this->input_sections_,
3930 this->first_input_offset_,
3931 this->attached_input_sections_are_sorted_);
3932 this->checkpoint_ = checkpoint;
3933 gold_assert(this->fills_.empty());
3936 void
3937 Output_section::discard_states()
3939 gold_assert(this->checkpoint_ != NULL);
3940 delete this->checkpoint_;
3941 this->checkpoint_ = NULL;
3942 gold_assert(this->fills_.empty());
3944 // Simply invalidate the fast lookup maps since we do not keep
3945 // track of them.
3946 this->lookup_maps_->invalidate();
3949 void
3950 Output_section::restore_states()
3952 gold_assert(this->checkpoint_ != NULL);
3953 Checkpoint_output_section* checkpoint = this->checkpoint_;
3955 this->addralign_ = checkpoint->addralign();
3956 this->flags_ = checkpoint->flags();
3957 this->first_input_offset_ = checkpoint->first_input_offset();
3959 if (!checkpoint->input_sections_saved())
3961 // If we have not copied the input sections, just resize it.
3962 size_t old_size = checkpoint->input_sections_size();
3963 gold_assert(this->input_sections_.size() >= old_size);
3964 this->input_sections_.resize(old_size);
3966 else
3968 // We need to copy the whole list. This is not efficient for
3969 // extremely large output with hundreads of thousands of input
3970 // objects. We may need to re-think how we should pass sections
3971 // to scripts.
3972 this->input_sections_ = *checkpoint->input_sections();
3975 this->attached_input_sections_are_sorted_ =
3976 checkpoint->attached_input_sections_are_sorted();
3978 // Simply invalidate the fast lookup maps since we do not keep
3979 // track of them.
3980 this->lookup_maps_->invalidate();
3983 // Update the section offsets of input sections in this. This is required if
3984 // relaxation causes some input sections to change sizes.
3986 void
3987 Output_section::adjust_section_offsets()
3989 if (!this->section_offsets_need_adjustment_)
3990 return;
3992 off_t off = 0;
3993 for (Input_section_list::iterator p = this->input_sections_.begin();
3994 p != this->input_sections_.end();
3995 ++p)
3997 off = align_address(off, p->addralign());
3998 if (p->is_input_section())
3999 p->relobj()->set_section_offset(p->shndx(), off);
4000 off += p->data_size();
4003 this->section_offsets_need_adjustment_ = false;
4006 // Print to the map file.
4008 void
4009 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
4011 mapfile->print_output_section(this);
4013 for (Input_section_list::const_iterator p = this->input_sections_.begin();
4014 p != this->input_sections_.end();
4015 ++p)
4016 p->print_to_mapfile(mapfile);
4019 // Print stats for merge sections to stderr.
4021 void
4022 Output_section::print_merge_stats()
4024 Input_section_list::iterator p;
4025 for (p = this->input_sections_.begin();
4026 p != this->input_sections_.end();
4027 ++p)
4028 p->print_merge_stats(this->name_);
4031 // Set a fixed layout for the section. Used for incremental update links.
4033 void
4034 Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
4035 off_t sh_size, uint64_t sh_addralign)
4037 this->addralign_ = sh_addralign;
4038 this->set_current_data_size(sh_size);
4039 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
4040 this->set_address(sh_addr);
4041 this->set_file_offset(sh_offset);
4042 this->finalize_data_size();
4043 this->free_list_.init(sh_size, false);
4044 this->has_fixed_layout_ = true;
4047 // Reserve space within the fixed layout for the section. Used for
4048 // incremental update links.
4050 void
4051 Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
4053 this->free_list_.remove(sh_offset, sh_offset + sh_size);
4056 // Allocate space from the free list for the section. Used for
4057 // incremental update links.
4059 off_t
4060 Output_section::allocate(off_t len, uint64_t addralign)
4062 return this->free_list_.allocate(len, addralign, 0);
4065 // Output segment methods.
4067 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
4068 : vaddr_(0),
4069 paddr_(0),
4070 memsz_(0),
4071 align_(0),
4072 max_align_(0),
4073 min_p_align_(0),
4074 offset_(0),
4075 filesz_(0),
4076 type_(type),
4077 flags_(flags),
4078 is_max_align_known_(false),
4079 are_addresses_set_(false),
4080 is_large_data_segment_(false),
4081 is_unique_segment_(false)
4083 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
4084 // the flags.
4085 if (type == elfcpp::PT_TLS)
4086 this->flags_ = elfcpp::PF_R;
4089 // Add an Output_section to a PT_LOAD Output_segment.
4091 void
4092 Output_segment::add_output_section_to_load(Layout* layout,
4093 Output_section* os,
4094 elfcpp::Elf_Word seg_flags)
4096 gold_assert(this->type() == elfcpp::PT_LOAD);
4097 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4098 gold_assert(!this->is_max_align_known_);
4099 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
4101 this->update_flags_for_output_section(seg_flags);
4103 // We don't want to change the ordering if we have a linker script
4104 // with a SECTIONS clause.
4105 Output_section_order order = os->order();
4106 if (layout->script_options()->saw_sections_clause())
4107 order = static_cast<Output_section_order>(0);
4108 else
4109 gold_assert(order != ORDER_INVALID);
4111 this->output_lists_[order].push_back(os);
4114 // Add an Output_section to a non-PT_LOAD Output_segment.
4116 void
4117 Output_segment::add_output_section_to_nonload(Output_section* os,
4118 elfcpp::Elf_Word seg_flags)
4120 gold_assert(this->type() != elfcpp::PT_LOAD);
4121 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4122 gold_assert(!this->is_max_align_known_);
4124 this->update_flags_for_output_section(seg_flags);
4126 this->output_lists_[0].push_back(os);
4129 // Remove an Output_section from this segment. It is an error if it
4130 // is not present.
4132 void
4133 Output_segment::remove_output_section(Output_section* os)
4135 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4137 Output_data_list* pdl = &this->output_lists_[i];
4138 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
4140 if (*p == os)
4142 pdl->erase(p);
4143 return;
4147 gold_unreachable();
4150 // Add an Output_data (which need not be an Output_section) to the
4151 // start of a segment.
4153 void
4154 Output_segment::add_initial_output_data(Output_data* od)
4156 gold_assert(!this->is_max_align_known_);
4157 Output_data_list::iterator p = this->output_lists_[0].begin();
4158 this->output_lists_[0].insert(p, od);
4161 // Return true if this segment has any sections which hold actual
4162 // data, rather than being a BSS section.
4164 bool
4165 Output_segment::has_any_data_sections() const
4167 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4169 const Output_data_list* pdl = &this->output_lists_[i];
4170 for (Output_data_list::const_iterator p = pdl->begin();
4171 p != pdl->end();
4172 ++p)
4174 if (!(*p)->is_section())
4175 return true;
4176 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
4177 return true;
4180 return false;
4183 // Return whether the first data section (not counting TLS sections)
4184 // is a relro section.
4186 bool
4187 Output_segment::is_first_section_relro() const
4189 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4191 if (i == static_cast<int>(ORDER_TLS_BSS))
4192 continue;
4193 const Output_data_list* pdl = &this->output_lists_[i];
4194 if (!pdl->empty())
4196 Output_data* p = pdl->front();
4197 return p->is_section() && p->output_section()->is_relro();
4200 return false;
4203 // Return the maximum alignment of the Output_data in Output_segment.
4205 uint64_t
4206 Output_segment::maximum_alignment()
4208 if (!this->is_max_align_known_)
4210 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4212 const Output_data_list* pdl = &this->output_lists_[i];
4213 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
4214 if (addralign > this->max_align_)
4215 this->max_align_ = addralign;
4217 this->is_max_align_known_ = true;
4220 return this->max_align_;
4223 // Return the maximum alignment of a list of Output_data.
4225 uint64_t
4226 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
4228 uint64_t ret = 0;
4229 for (Output_data_list::const_iterator p = pdl->begin();
4230 p != pdl->end();
4231 ++p)
4233 uint64_t addralign = (*p)->addralign();
4234 if (addralign > ret)
4235 ret = addralign;
4237 return ret;
4240 // Return whether this segment has any dynamic relocs.
4242 bool
4243 Output_segment::has_dynamic_reloc() const
4245 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4246 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
4247 return true;
4248 return false;
4251 // Return whether this Output_data_list has any dynamic relocs.
4253 bool
4254 Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
4256 for (Output_data_list::const_iterator p = pdl->begin();
4257 p != pdl->end();
4258 ++p)
4259 if ((*p)->has_dynamic_reloc())
4260 return true;
4261 return false;
4264 // Set the section addresses for an Output_segment. If RESET is true,
4265 // reset the addresses first. ADDR is the address and *POFF is the
4266 // file offset. Set the section indexes starting with *PSHNDX.
4267 // INCREASE_RELRO is the size of the portion of the first non-relro
4268 // section that should be included in the PT_GNU_RELRO segment.
4269 // If this segment has relro sections, and has been aligned for
4270 // that purpose, set *HAS_RELRO to TRUE. Return the address of
4271 // the immediately following segment. Update *HAS_RELRO, *POFF,
4272 // and *PSHNDX.
4274 uint64_t
4275 Output_segment::set_section_addresses(const Target* target,
4276 Layout* layout, bool reset,
4277 uint64_t addr,
4278 unsigned int* increase_relro,
4279 bool* has_relro,
4280 off_t* poff,
4281 unsigned int* pshndx)
4283 gold_assert(this->type_ == elfcpp::PT_LOAD);
4285 uint64_t last_relro_pad = 0;
4286 off_t orig_off = *poff;
4288 bool in_tls = false;
4290 // If we have relro sections, we need to pad forward now so that the
4291 // relro sections plus INCREASE_RELRO end on an abi page boundary.
4292 if (parameters->options().relro()
4293 && this->is_first_section_relro()
4294 && (!this->are_addresses_set_ || reset))
4296 uint64_t relro_size = 0;
4297 off_t off = *poff;
4298 uint64_t max_align = 0;
4299 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
4301 Output_data_list* pdl = &this->output_lists_[i];
4302 Output_data_list::iterator p;
4303 for (p = pdl->begin(); p != pdl->end(); ++p)
4305 if (!(*p)->is_section())
4306 break;
4307 uint64_t align = (*p)->addralign();
4308 if (align > max_align)
4309 max_align = align;
4310 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4311 in_tls = true;
4312 else if (in_tls)
4314 // Align the first non-TLS section to the alignment
4315 // of the TLS segment.
4316 align = max_align;
4317 in_tls = false;
4319 // Ignore the size of the .tbss section.
4320 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
4321 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
4322 continue;
4323 relro_size = align_address(relro_size, align);
4324 if ((*p)->is_address_valid())
4325 relro_size += (*p)->data_size();
4326 else
4328 // FIXME: This could be faster.
4329 (*p)->set_address_and_file_offset(relro_size,
4330 relro_size);
4331 relro_size += (*p)->data_size();
4332 (*p)->reset_address_and_file_offset();
4335 if (p != pdl->end())
4336 break;
4338 relro_size += *increase_relro;
4339 // Pad the total relro size to a multiple of the maximum
4340 // section alignment seen.
4341 uint64_t aligned_size = align_address(relro_size, max_align);
4342 // Note the amount of padding added after the last relro section.
4343 last_relro_pad = aligned_size - relro_size;
4344 *has_relro = true;
4346 uint64_t page_align = parameters->target().abi_pagesize();
4348 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4349 uint64_t desired_align = page_align - (aligned_size % page_align);
4350 if (desired_align < off % page_align)
4351 off += page_align;
4352 off += desired_align - off % page_align;
4353 addr += off - orig_off;
4354 orig_off = off;
4355 *poff = off;
4358 if (!reset && this->are_addresses_set_)
4360 gold_assert(this->paddr_ == addr);
4361 addr = this->vaddr_;
4363 else
4365 this->vaddr_ = addr;
4366 this->paddr_ = addr;
4367 this->are_addresses_set_ = true;
4370 in_tls = false;
4372 this->offset_ = orig_off;
4374 off_t off = 0;
4375 off_t foff = *poff;
4376 uint64_t ret = 0;
4377 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4379 if (i == static_cast<int>(ORDER_RELRO_LAST))
4381 *poff += last_relro_pad;
4382 foff += last_relro_pad;
4383 addr += last_relro_pad;
4384 if (this->output_lists_[i].empty())
4386 // If there is nothing in the ORDER_RELRO_LAST list,
4387 // the padding will occur at the end of the relro
4388 // segment, and we need to add it to *INCREASE_RELRO.
4389 *increase_relro += last_relro_pad;
4392 addr = this->set_section_list_addresses(layout, reset,
4393 &this->output_lists_[i],
4394 addr, poff, &foff, pshndx,
4395 &in_tls);
4397 // FOFF tracks the last offset used for the file image,
4398 // and *POFF tracks the last offset used for the memory image.
4399 // When not using a linker script, bss sections should all
4400 // be processed in the ORDER_SMALL_BSS and later buckets.
4401 gold_assert(*poff == foff
4402 || i == static_cast<int>(ORDER_TLS_BSS)
4403 || i >= static_cast<int>(ORDER_SMALL_BSS)
4404 || layout->script_options()->saw_sections_clause());
4406 this->filesz_ = foff - orig_off;
4407 off = foff;
4409 ret = addr;
4412 // If the last section was a TLS section, align upward to the
4413 // alignment of the TLS segment, so that the overall size of the TLS
4414 // segment is aligned.
4415 if (in_tls)
4417 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4418 *poff = align_address(*poff, segment_align);
4421 this->memsz_ = *poff - orig_off;
4423 // Ignore the file offset adjustments made by the BSS Output_data
4424 // objects.
4425 *poff = off;
4427 // If code segments must contain only code, and this code segment is
4428 // page-aligned in the file, then fill it out to a whole page with
4429 // code fill (the tail of the segment will not be within any section).
4430 // Thus the entire code segment can be mapped from the file as whole
4431 // pages and that mapping will contain only valid instructions.
4432 if (target->isolate_execinstr() && (this->flags() & elfcpp::PF_X) != 0)
4434 uint64_t abi_pagesize = target->abi_pagesize();
4435 if (orig_off % abi_pagesize == 0 && off % abi_pagesize != 0)
4437 size_t fill_size = abi_pagesize - (off % abi_pagesize);
4439 std::string fill_data;
4440 if (target->has_code_fill())
4441 fill_data = target->code_fill(fill_size);
4442 else
4443 fill_data.resize(fill_size); // Zero fill.
4445 Output_data_const* fill = new Output_data_const(fill_data, 0);
4446 fill->set_address(this->vaddr_ + this->memsz_);
4447 fill->set_file_offset(off);
4448 layout->add_relax_output(fill);
4450 off += fill_size;
4451 gold_assert(off % abi_pagesize == 0);
4452 ret += fill_size;
4453 gold_assert(ret % abi_pagesize == 0);
4455 gold_assert((uint64_t) this->filesz_ == this->memsz_);
4456 this->memsz_ = this->filesz_ += fill_size;
4458 *poff = off;
4462 return ret;
4465 // Set the addresses and file offsets in a list of Output_data
4466 // structures.
4468 uint64_t
4469 Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4470 Output_data_list* pdl,
4471 uint64_t addr, off_t* poff,
4472 off_t* pfoff,
4473 unsigned int* pshndx,
4474 bool* in_tls)
4476 off_t startoff = *poff;
4477 // For incremental updates, we may allocate non-fixed sections from
4478 // free space in the file. This keeps track of the high-water mark.
4479 off_t maxoff = startoff;
4481 off_t off = startoff;
4482 off_t foff = *pfoff;
4483 for (Output_data_list::iterator p = pdl->begin();
4484 p != pdl->end();
4485 ++p)
4487 bool is_bss = (*p)->is_section_type(elfcpp::SHT_NOBITS);
4488 bool is_tls = (*p)->is_section_flag_set(elfcpp::SHF_TLS);
4490 if (reset)
4491 (*p)->reset_address_and_file_offset();
4493 // When doing an incremental update or when using a linker script,
4494 // the section will most likely already have an address.
4495 if (!(*p)->is_address_valid())
4497 uint64_t align = (*p)->addralign();
4499 if (is_tls)
4501 // Give the first TLS section the alignment of the
4502 // entire TLS segment. Otherwise the TLS segment as a
4503 // whole may be misaligned.
4504 if (!*in_tls)
4506 Output_segment* tls_segment = layout->tls_segment();
4507 gold_assert(tls_segment != NULL);
4508 uint64_t segment_align = tls_segment->maximum_alignment();
4509 gold_assert(segment_align >= align);
4510 align = segment_align;
4512 *in_tls = true;
4515 else
4517 // If this is the first section after the TLS segment,
4518 // align it to at least the alignment of the TLS
4519 // segment, so that the size of the overall TLS segment
4520 // is aligned.
4521 if (*in_tls)
4523 uint64_t segment_align =
4524 layout->tls_segment()->maximum_alignment();
4525 if (segment_align > align)
4526 align = segment_align;
4528 *in_tls = false;
4532 if (!parameters->incremental_update())
4534 gold_assert(off == foff || is_bss);
4535 off = align_address(off, align);
4536 if (is_tls || !is_bss)
4537 foff = off;
4538 (*p)->set_address_and_file_offset(addr + (off - startoff), foff);
4540 else
4542 // Incremental update: allocate file space from free list.
4543 (*p)->pre_finalize_data_size();
4544 off_t current_size = (*p)->current_data_size();
4545 off = layout->allocate(current_size, align, startoff);
4546 foff = off;
4547 if (off == -1)
4549 gold_assert((*p)->output_section() != NULL);
4550 gold_fallback(_("out of patch space for section %s; "
4551 "relink with --incremental-full"),
4552 (*p)->output_section()->name());
4554 (*p)->set_address_and_file_offset(addr + (off - startoff), foff);
4555 if ((*p)->data_size() > current_size)
4557 gold_assert((*p)->output_section() != NULL);
4558 gold_fallback(_("%s: section changed size; "
4559 "relink with --incremental-full"),
4560 (*p)->output_section()->name());
4564 else if (parameters->incremental_update())
4566 // For incremental updates, use the fixed offset for the
4567 // high-water mark computation.
4568 off = (*p)->offset();
4569 foff = off;
4571 else
4573 // The script may have inserted a skip forward, but it
4574 // better not have moved backward.
4575 if ((*p)->address() >= addr + (off - startoff))
4577 if (!is_bss && off > foff)
4578 gold_warning(_("script places BSS section in the middle "
4579 "of a LOAD segment; space will be allocated "
4580 "in the file"));
4581 off += (*p)->address() - (addr + (off - startoff));
4582 if (is_tls || !is_bss)
4583 foff = off;
4585 else
4587 if (!layout->script_options()->saw_sections_clause())
4588 gold_unreachable();
4589 else
4591 Output_section* os = (*p)->output_section();
4593 // Cast to unsigned long long to avoid format warnings.
4594 unsigned long long previous_dot =
4595 static_cast<unsigned long long>(addr + (off - startoff));
4596 unsigned long long dot =
4597 static_cast<unsigned long long>((*p)->address());
4599 if (os == NULL)
4600 gold_error(_("dot moves backward in linker script "
4601 "from 0x%llx to 0x%llx"), previous_dot, dot);
4602 else
4603 gold_error(_("address of section '%s' moves backward "
4604 "from 0x%llx to 0x%llx"),
4605 os->name(), previous_dot, dot);
4608 (*p)->set_file_offset(foff);
4609 (*p)->finalize_data_size();
4612 if (parameters->incremental_update())
4613 gold_debug(DEBUG_INCREMENTAL,
4614 "set_section_list_addresses: %08lx %08lx %s",
4615 static_cast<long>(off),
4616 static_cast<long>((*p)->data_size()),
4617 ((*p)->output_section() != NULL
4618 ? (*p)->output_section()->name() : "(special)"));
4620 // We want to ignore the size of a SHF_TLS SHT_NOBITS
4621 // section. Such a section does not affect the size of a
4622 // PT_LOAD segment.
4623 if (!is_tls || !is_bss)
4624 off += (*p)->data_size();
4626 // We don't allocate space in the file for SHT_NOBITS sections,
4627 // unless a script has force-placed one in the middle of a segment.
4628 if (!is_bss)
4629 foff = off;
4631 if (off > maxoff)
4632 maxoff = off;
4634 if ((*p)->is_section())
4636 (*p)->set_out_shndx(*pshndx);
4637 ++*pshndx;
4641 *poff = maxoff;
4642 *pfoff = foff;
4643 return addr + (maxoff - startoff);
4646 // For a non-PT_LOAD segment, set the offset from the sections, if
4647 // any. Add INCREASE to the file size and the memory size.
4649 void
4650 Output_segment::set_offset(unsigned int increase)
4652 gold_assert(this->type_ != elfcpp::PT_LOAD);
4654 gold_assert(!this->are_addresses_set_);
4656 // A non-load section only uses output_lists_[0].
4658 Output_data_list* pdl = &this->output_lists_[0];
4660 if (pdl->empty())
4662 gold_assert(increase == 0);
4663 this->vaddr_ = 0;
4664 this->paddr_ = 0;
4665 this->are_addresses_set_ = true;
4666 this->memsz_ = 0;
4667 this->min_p_align_ = 0;
4668 this->offset_ = 0;
4669 this->filesz_ = 0;
4670 return;
4673 // Find the first and last section by address.
4674 const Output_data* first = NULL;
4675 const Output_data* last_data = NULL;
4676 const Output_data* last_bss = NULL;
4677 for (Output_data_list::const_iterator p = pdl->begin();
4678 p != pdl->end();
4679 ++p)
4681 if (first == NULL
4682 || (*p)->address() < first->address()
4683 || ((*p)->address() == first->address()
4684 && (*p)->data_size() < first->data_size()))
4685 first = *p;
4686 const Output_data** plast;
4687 if ((*p)->is_section()
4688 && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4689 plast = &last_bss;
4690 else
4691 plast = &last_data;
4692 if (*plast == NULL
4693 || (*p)->address() > (*plast)->address()
4694 || ((*p)->address() == (*plast)->address()
4695 && (*p)->data_size() > (*plast)->data_size()))
4696 *plast = *p;
4699 this->vaddr_ = first->address();
4700 this->paddr_ = (first->has_load_address()
4701 ? first->load_address()
4702 : this->vaddr_);
4703 this->are_addresses_set_ = true;
4704 this->offset_ = first->offset();
4706 if (last_data == NULL)
4707 this->filesz_ = 0;
4708 else
4709 this->filesz_ = (last_data->address()
4710 + last_data->data_size()
4711 - this->vaddr_);
4713 const Output_data* last = last_bss != NULL ? last_bss : last_data;
4714 this->memsz_ = (last->address()
4715 + last->data_size()
4716 - this->vaddr_);
4718 this->filesz_ += increase;
4719 this->memsz_ += increase;
4721 // If this is a RELRO segment, verify that the segment ends at a
4722 // page boundary.
4723 if (this->type_ == elfcpp::PT_GNU_RELRO)
4725 uint64_t page_align = parameters->target().abi_pagesize();
4726 uint64_t segment_end = this->vaddr_ + this->memsz_;
4727 if (parameters->incremental_update())
4729 // The INCREASE_RELRO calculation is bypassed for an incremental
4730 // update, so we need to adjust the segment size manually here.
4731 segment_end = align_address(segment_end, page_align);
4732 this->memsz_ = segment_end - this->vaddr_;
4734 else
4735 gold_assert(segment_end == align_address(segment_end, page_align));
4738 // If this is a TLS segment, align the memory size. The code in
4739 // set_section_list ensures that the section after the TLS segment
4740 // is aligned to give us room.
4741 if (this->type_ == elfcpp::PT_TLS)
4743 uint64_t segment_align = this->maximum_alignment();
4744 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4745 this->memsz_ = align_address(this->memsz_, segment_align);
4749 // Set the TLS offsets of the sections in the PT_TLS segment.
4751 void
4752 Output_segment::set_tls_offsets()
4754 gold_assert(this->type_ == elfcpp::PT_TLS);
4756 for (Output_data_list::iterator p = this->output_lists_[0].begin();
4757 p != this->output_lists_[0].end();
4758 ++p)
4759 (*p)->set_tls_offset(this->vaddr_);
4762 // Return the first section.
4764 Output_section*
4765 Output_segment::first_section() const
4767 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4769 const Output_data_list* pdl = &this->output_lists_[i];
4770 for (Output_data_list::const_iterator p = pdl->begin();
4771 p != pdl->end();
4772 ++p)
4774 if ((*p)->is_section())
4775 return (*p)->output_section();
4778 return NULL;
4781 // Return the number of Output_sections in an Output_segment.
4783 unsigned int
4784 Output_segment::output_section_count() const
4786 unsigned int ret = 0;
4787 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4788 ret += this->output_section_count_list(&this->output_lists_[i]);
4789 return ret;
4792 // Return the number of Output_sections in an Output_data_list.
4794 unsigned int
4795 Output_segment::output_section_count_list(const Output_data_list* pdl) const
4797 unsigned int count = 0;
4798 for (Output_data_list::const_iterator p = pdl->begin();
4799 p != pdl->end();
4800 ++p)
4802 if ((*p)->is_section())
4803 ++count;
4805 return count;
4808 // Return the section attached to the list segment with the lowest
4809 // load address. This is used when handling a PHDRS clause in a
4810 // linker script.
4812 Output_section*
4813 Output_segment::section_with_lowest_load_address() const
4815 Output_section* found = NULL;
4816 uint64_t found_lma = 0;
4817 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4818 this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4819 &found_lma);
4820 return found;
4823 // Look through a list for a section with a lower load address.
4825 void
4826 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4827 Output_section** found,
4828 uint64_t* found_lma) const
4830 for (Output_data_list::const_iterator p = pdl->begin();
4831 p != pdl->end();
4832 ++p)
4834 if (!(*p)->is_section())
4835 continue;
4836 Output_section* os = static_cast<Output_section*>(*p);
4837 uint64_t lma = (os->has_load_address()
4838 ? os->load_address()
4839 : os->address());
4840 if (*found == NULL || lma < *found_lma)
4842 *found = os;
4843 *found_lma = lma;
4848 // Write the segment data into *OPHDR.
4850 template<int size, bool big_endian>
4851 void
4852 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4854 ophdr->put_p_type(this->type_);
4855 ophdr->put_p_offset(this->offset_);
4856 ophdr->put_p_vaddr(this->vaddr_);
4857 ophdr->put_p_paddr(this->paddr_);
4858 ophdr->put_p_filesz(this->filesz_);
4859 ophdr->put_p_memsz(this->memsz_);
4860 ophdr->put_p_flags(this->flags_);
4861 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4864 // Write the section headers into V.
4866 template<int size, bool big_endian>
4867 unsigned char*
4868 Output_segment::write_section_headers(const Layout* layout,
4869 const Stringpool* secnamepool,
4870 unsigned char* v,
4871 unsigned int* pshndx) const
4873 // Every section that is attached to a segment must be attached to a
4874 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4875 // segments.
4876 if (this->type_ != elfcpp::PT_LOAD)
4877 return v;
4879 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4881 const Output_data_list* pdl = &this->output_lists_[i];
4882 v = this->write_section_headers_list<size, big_endian>(layout,
4883 secnamepool,
4884 pdl,
4885 v, pshndx);
4888 return v;
4891 template<int size, bool big_endian>
4892 unsigned char*
4893 Output_segment::write_section_headers_list(const Layout* layout,
4894 const Stringpool* secnamepool,
4895 const Output_data_list* pdl,
4896 unsigned char* v,
4897 unsigned int* pshndx) const
4899 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4900 for (Output_data_list::const_iterator p = pdl->begin();
4901 p != pdl->end();
4902 ++p)
4904 if ((*p)->is_section())
4906 const Output_section* ps = static_cast<const Output_section*>(*p);
4907 gold_assert(*pshndx == ps->out_shndx());
4908 elfcpp::Shdr_write<size, big_endian> oshdr(v);
4909 ps->write_header(layout, secnamepool, &oshdr);
4910 v += shdr_size;
4911 ++*pshndx;
4914 return v;
4917 // Print the output sections to the map file.
4919 void
4920 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4922 if (this->type() != elfcpp::PT_LOAD)
4923 return;
4924 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4925 this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4928 // Print an output section list to the map file.
4930 void
4931 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4932 const Output_data_list* pdl) const
4934 for (Output_data_list::const_iterator p = pdl->begin();
4935 p != pdl->end();
4936 ++p)
4937 (*p)->print_to_mapfile(mapfile);
4940 // Output_file methods.
4942 Output_file::Output_file(const char* name)
4943 : name_(name),
4944 o_(-1),
4945 file_size_(0),
4946 base_(NULL),
4947 map_is_anonymous_(false),
4948 map_is_allocated_(false),
4949 is_temporary_(false)
4953 // Try to open an existing file. Returns false if the file doesn't
4954 // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
4955 // NULL, open that file as the base for incremental linking, and
4956 // copy its contents to the new output file. This routine can
4957 // be called for incremental updates, in which case WRITABLE should
4958 // be true, or by the incremental-dump utility, in which case
4959 // WRITABLE should be false.
4961 bool
4962 Output_file::open_base_file(const char* base_name, bool writable)
4964 // The name "-" means "stdout".
4965 if (strcmp(this->name_, "-") == 0)
4966 return false;
4968 bool use_base_file = base_name != NULL;
4969 if (!use_base_file)
4970 base_name = this->name_;
4971 else if (strcmp(base_name, this->name_) == 0)
4972 gold_fatal(_("%s: incremental base and output file name are the same"),
4973 base_name);
4975 // Don't bother opening files with a size of zero.
4976 struct stat s;
4977 if (::stat(base_name, &s) != 0)
4979 gold_info(_("%s: stat: %s"), base_name, strerror(errno));
4980 return false;
4982 if (s.st_size == 0)
4984 gold_info(_("%s: incremental base file is empty"), base_name);
4985 return false;
4988 // If we're using a base file, we want to open it read-only.
4989 if (use_base_file)
4990 writable = false;
4992 int oflags = writable ? O_RDWR : O_RDONLY;
4993 int o = open_descriptor(-1, base_name, oflags, 0);
4994 if (o < 0)
4996 gold_info(_("%s: open: %s"), base_name, strerror(errno));
4997 return false;
5000 // If the base file and the output file are different, open a
5001 // new output file and read the contents from the base file into
5002 // the newly-mapped region.
5003 if (use_base_file)
5005 this->open(s.st_size);
5006 ssize_t bytes_to_read = s.st_size;
5007 unsigned char* p = this->base_;
5008 while (bytes_to_read > 0)
5010 ssize_t len = ::read(o, p, bytes_to_read);
5011 if (len < 0)
5013 gold_info(_("%s: read failed: %s"), base_name, strerror(errno));
5014 return false;
5016 if (len == 0)
5018 gold_info(_("%s: file too short: read only %lld of %lld bytes"),
5019 base_name,
5020 static_cast<long long>(s.st_size - bytes_to_read),
5021 static_cast<long long>(s.st_size));
5022 return false;
5024 p += len;
5025 bytes_to_read -= len;
5027 ::close(o);
5028 return true;
5031 this->o_ = o;
5032 this->file_size_ = s.st_size;
5034 if (!this->map_no_anonymous(writable))
5036 release_descriptor(o, true);
5037 this->o_ = -1;
5038 this->file_size_ = 0;
5039 return false;
5042 return true;
5045 // Open the output file.
5047 void
5048 Output_file::open(off_t file_size)
5050 this->file_size_ = file_size;
5052 // Unlink the file first; otherwise the open() may fail if the file
5053 // is busy (e.g. it's an executable that's currently being executed).
5055 // However, the linker may be part of a system where a zero-length
5056 // file is created for it to write to, with tight permissions (gcc
5057 // 2.95 did something like this). Unlinking the file would work
5058 // around those permission controls, so we only unlink if the file
5059 // has a non-zero size. We also unlink only regular files to avoid
5060 // trouble with directories/etc.
5062 // If we fail, continue; this command is merely a best-effort attempt
5063 // to improve the odds for open().
5065 // We let the name "-" mean "stdout"
5066 if (!this->is_temporary_)
5068 if (strcmp(this->name_, "-") == 0)
5069 this->o_ = STDOUT_FILENO;
5070 else
5072 struct stat s;
5073 if (::stat(this->name_, &s) == 0
5074 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
5076 if (s.st_size != 0)
5077 ::unlink(this->name_);
5078 else if (!parameters->options().relocatable())
5080 // If we don't unlink the existing file, add execute
5081 // permission where read permissions already exist
5082 // and where the umask permits.
5083 int mask = ::umask(0);
5084 ::umask(mask);
5085 s.st_mode |= (s.st_mode & 0444) >> 2;
5086 ::chmod(this->name_, s.st_mode & ~mask);
5090 int mode = parameters->options().relocatable() ? 0666 : 0777;
5091 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
5092 mode);
5093 if (o < 0)
5094 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
5095 this->o_ = o;
5099 this->map();
5102 // Resize the output file.
5104 void
5105 Output_file::resize(off_t file_size)
5107 // If the mmap is mapping an anonymous memory buffer, this is easy:
5108 // just mremap to the new size. If it's mapping to a file, we want
5109 // to unmap to flush to the file, then remap after growing the file.
5110 if (this->map_is_anonymous_)
5112 void* base;
5113 if (!this->map_is_allocated_)
5115 base = ::mremap(this->base_, this->file_size_, file_size,
5116 MREMAP_MAYMOVE);
5117 if (base == MAP_FAILED)
5118 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
5120 else
5122 base = realloc(this->base_, file_size);
5123 if (base == NULL)
5124 gold_nomem();
5125 if (file_size > this->file_size_)
5126 memset(static_cast<char*>(base) + this->file_size_, 0,
5127 file_size - this->file_size_);
5129 this->base_ = static_cast<unsigned char*>(base);
5130 this->file_size_ = file_size;
5132 else
5134 this->unmap();
5135 this->file_size_ = file_size;
5136 if (!this->map_no_anonymous(true))
5137 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
5141 // Map an anonymous block of memory which will later be written to the
5142 // file. Return whether the map succeeded.
5144 bool
5145 Output_file::map_anonymous()
5147 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
5148 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
5149 if (base == MAP_FAILED)
5151 base = malloc(this->file_size_);
5152 if (base == NULL)
5153 return false;
5154 memset(base, 0, this->file_size_);
5155 this->map_is_allocated_ = true;
5157 this->base_ = static_cast<unsigned char*>(base);
5158 this->map_is_anonymous_ = true;
5159 return true;
5162 // Map the file into memory. Return whether the mapping succeeded.
5163 // If WRITABLE is true, map with write access.
5165 bool
5166 Output_file::map_no_anonymous(bool writable)
5168 const int o = this->o_;
5170 // If the output file is not a regular file, don't try to mmap it;
5171 // instead, we'll mmap a block of memory (an anonymous buffer), and
5172 // then later write the buffer to the file.
5173 void* base;
5174 struct stat statbuf;
5175 if (o == STDOUT_FILENO || o == STDERR_FILENO
5176 || ::fstat(o, &statbuf) != 0
5177 || !S_ISREG(statbuf.st_mode)
5178 || this->is_temporary_)
5179 return false;
5181 // Ensure that we have disk space available for the file. If we
5182 // don't do this, it is possible that we will call munmap, close,
5183 // and exit with dirty buffers still in the cache with no assigned
5184 // disk blocks. If the disk is out of space at that point, the
5185 // output file will wind up incomplete, but we will have already
5186 // exited. The alternative to fallocate would be to use fdatasync,
5187 // but that would be a more significant performance hit.
5188 if (writable)
5190 int err = gold_fallocate(o, 0, this->file_size_);
5191 if (err != 0)
5192 gold_fatal(_("%s: %s"), this->name_, strerror(err));
5195 // Map the file into memory.
5196 int prot = PROT_READ;
5197 if (writable)
5198 prot |= PROT_WRITE;
5199 base = ::mmap(NULL, this->file_size_, prot, MAP_SHARED, o, 0);
5201 // The mmap call might fail because of file system issues: the file
5202 // system might not support mmap at all, or it might not support
5203 // mmap with PROT_WRITE.
5204 if (base == MAP_FAILED)
5205 return false;
5207 this->map_is_anonymous_ = false;
5208 this->base_ = static_cast<unsigned char*>(base);
5209 return true;
5212 // Map the file into memory.
5214 void
5215 Output_file::map()
5217 if (parameters->options().mmap_output_file()
5218 && this->map_no_anonymous(true))
5219 return;
5221 // The mmap call might fail because of file system issues: the file
5222 // system might not support mmap at all, or it might not support
5223 // mmap with PROT_WRITE. I'm not sure which errno values we will
5224 // see in all cases, so if the mmap fails for any reason and we
5225 // don't care about file contents, try for an anonymous map.
5226 if (this->map_anonymous())
5227 return;
5229 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
5230 this->name_, static_cast<unsigned long>(this->file_size_),
5231 strerror(errno));
5234 // Unmap the file from memory.
5236 void
5237 Output_file::unmap()
5239 if (this->map_is_anonymous_)
5241 // We've already written out the data, so there is no reason to
5242 // waste time unmapping or freeing the memory.
5244 else
5246 if (::munmap(this->base_, this->file_size_) < 0)
5247 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
5249 this->base_ = NULL;
5252 // Close the output file.
5254 void
5255 Output_file::close()
5257 // If the map isn't file-backed, we need to write it now.
5258 if (this->map_is_anonymous_ && !this->is_temporary_)
5260 size_t bytes_to_write = this->file_size_;
5261 size_t offset = 0;
5262 while (bytes_to_write > 0)
5264 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
5265 bytes_to_write);
5266 if (bytes_written == 0)
5267 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
5268 else if (bytes_written < 0)
5269 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
5270 else
5272 bytes_to_write -= bytes_written;
5273 offset += bytes_written;
5277 this->unmap();
5279 // We don't close stdout or stderr
5280 if (this->o_ != STDOUT_FILENO
5281 && this->o_ != STDERR_FILENO
5282 && !this->is_temporary_)
5283 if (::close(this->o_) < 0)
5284 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
5285 this->o_ = -1;
5288 // Instantiate the templates we need. We could use the configure
5289 // script to restrict this to only the ones for implemented targets.
5291 #ifdef HAVE_TARGET_32_LITTLE
5292 template
5293 off_t
5294 Output_section::add_input_section<32, false>(
5295 Layout* layout,
5296 Sized_relobj_file<32, false>* object,
5297 unsigned int shndx,
5298 const char* secname,
5299 const elfcpp::Shdr<32, false>& shdr,
5300 unsigned int reloc_shndx,
5301 bool have_sections_script);
5302 #endif
5304 #ifdef HAVE_TARGET_32_BIG
5305 template
5306 off_t
5307 Output_section::add_input_section<32, true>(
5308 Layout* layout,
5309 Sized_relobj_file<32, true>* object,
5310 unsigned int shndx,
5311 const char* secname,
5312 const elfcpp::Shdr<32, true>& shdr,
5313 unsigned int reloc_shndx,
5314 bool have_sections_script);
5315 #endif
5317 #ifdef HAVE_TARGET_64_LITTLE
5318 template
5319 off_t
5320 Output_section::add_input_section<64, false>(
5321 Layout* layout,
5322 Sized_relobj_file<64, false>* object,
5323 unsigned int shndx,
5324 const char* secname,
5325 const elfcpp::Shdr<64, false>& shdr,
5326 unsigned int reloc_shndx,
5327 bool have_sections_script);
5328 #endif
5330 #ifdef HAVE_TARGET_64_BIG
5331 template
5332 off_t
5333 Output_section::add_input_section<64, true>(
5334 Layout* layout,
5335 Sized_relobj_file<64, true>* object,
5336 unsigned int shndx,
5337 const char* secname,
5338 const elfcpp::Shdr<64, true>& shdr,
5339 unsigned int reloc_shndx,
5340 bool have_sections_script);
5341 #endif
5343 #ifdef HAVE_TARGET_32_LITTLE
5344 template
5345 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
5346 #endif
5348 #ifdef HAVE_TARGET_32_BIG
5349 template
5350 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
5351 #endif
5353 #ifdef HAVE_TARGET_64_LITTLE
5354 template
5355 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
5356 #endif
5358 #ifdef HAVE_TARGET_64_BIG
5359 template
5360 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
5361 #endif
5363 #ifdef HAVE_TARGET_32_LITTLE
5364 template
5365 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
5366 #endif
5368 #ifdef HAVE_TARGET_32_BIG
5369 template
5370 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
5371 #endif
5373 #ifdef HAVE_TARGET_64_LITTLE
5374 template
5375 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
5376 #endif
5378 #ifdef HAVE_TARGET_64_BIG
5379 template
5380 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
5381 #endif
5383 #ifdef HAVE_TARGET_32_LITTLE
5384 template
5385 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
5386 #endif
5388 #ifdef HAVE_TARGET_32_BIG
5389 template
5390 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
5391 #endif
5393 #ifdef HAVE_TARGET_64_LITTLE
5394 template
5395 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
5396 #endif
5398 #ifdef HAVE_TARGET_64_BIG
5399 template
5400 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
5401 #endif
5403 #ifdef HAVE_TARGET_32_LITTLE
5404 template
5405 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
5406 #endif
5408 #ifdef HAVE_TARGET_32_BIG
5409 template
5410 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
5411 #endif
5413 #ifdef HAVE_TARGET_64_LITTLE
5414 template
5415 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
5416 #endif
5418 #ifdef HAVE_TARGET_64_BIG
5419 template
5420 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
5421 #endif
5423 #ifdef HAVE_TARGET_32_LITTLE
5424 template
5425 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
5426 #endif
5428 #ifdef HAVE_TARGET_32_BIG
5429 template
5430 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
5431 #endif
5433 #ifdef HAVE_TARGET_64_LITTLE
5434 template
5435 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
5436 #endif
5438 #ifdef HAVE_TARGET_64_BIG
5439 template
5440 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
5441 #endif
5443 #ifdef HAVE_TARGET_32_LITTLE
5444 template
5445 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
5446 #endif
5448 #ifdef HAVE_TARGET_32_BIG
5449 template
5450 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
5451 #endif
5453 #ifdef HAVE_TARGET_64_LITTLE
5454 template
5455 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
5456 #endif
5458 #ifdef HAVE_TARGET_64_BIG
5459 template
5460 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
5461 #endif
5463 #ifdef HAVE_TARGET_32_LITTLE
5464 template
5465 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
5466 #endif
5468 #ifdef HAVE_TARGET_32_BIG
5469 template
5470 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
5471 #endif
5473 #ifdef HAVE_TARGET_64_LITTLE
5474 template
5475 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
5476 #endif
5478 #ifdef HAVE_TARGET_64_BIG
5479 template
5480 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
5481 #endif
5483 #ifdef HAVE_TARGET_32_LITTLE
5484 template
5485 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
5486 #endif
5488 #ifdef HAVE_TARGET_32_BIG
5489 template
5490 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
5491 #endif
5493 #ifdef HAVE_TARGET_64_LITTLE
5494 template
5495 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
5496 #endif
5498 #ifdef HAVE_TARGET_64_BIG
5499 template
5500 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
5501 #endif
5503 #ifdef HAVE_TARGET_32_LITTLE
5504 template
5505 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
5506 #endif
5508 #ifdef HAVE_TARGET_32_BIG
5509 template
5510 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
5511 #endif
5513 #ifdef HAVE_TARGET_64_LITTLE
5514 template
5515 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
5516 #endif
5518 #ifdef HAVE_TARGET_64_BIG
5519 template
5520 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
5521 #endif
5523 #ifdef HAVE_TARGET_32_LITTLE
5524 template
5525 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5526 #endif
5528 #ifdef HAVE_TARGET_32_BIG
5529 template
5530 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5531 #endif
5533 #ifdef HAVE_TARGET_64_LITTLE
5534 template
5535 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5536 #endif
5538 #ifdef HAVE_TARGET_64_BIG
5539 template
5540 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5541 #endif
5543 #ifdef HAVE_TARGET_32_LITTLE
5544 template
5545 class Output_data_group<32, false>;
5546 #endif
5548 #ifdef HAVE_TARGET_32_BIG
5549 template
5550 class Output_data_group<32, true>;
5551 #endif
5553 #ifdef HAVE_TARGET_64_LITTLE
5554 template
5555 class Output_data_group<64, false>;
5556 #endif
5558 #ifdef HAVE_TARGET_64_BIG
5559 template
5560 class Output_data_group<64, true>;
5561 #endif
5563 template
5564 class Output_data_got<32, false>;
5566 template
5567 class Output_data_got<32, true>;
5569 template
5570 class Output_data_got<64, false>;
5572 template
5573 class Output_data_got<64, true>;
5575 } // End namespace gold.