daily update
[binutils.git] / gold / output.cc
blobca190392d14f4b88d6a41ac46620988031460261
1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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>
33 #ifdef HAVE_SYS_MMAN_H
34 #include <sys/mman.h>
35 #endif
37 #include "libiberty.h"
39 #include "dwarf.h"
40 #include "parameters.h"
41 #include "object.h"
42 #include "symtab.h"
43 #include "reloc.h"
44 #include "merge.h"
45 #include "descriptors.h"
46 #include "layout.h"
47 #include "output.h"
49 // For systems without mmap support.
50 #ifndef HAVE_MMAP
51 # define mmap gold_mmap
52 # define munmap gold_munmap
53 # define mremap gold_mremap
54 # ifndef MAP_FAILED
55 # define MAP_FAILED (reinterpret_cast<void*>(-1))
56 # endif
57 # ifndef PROT_READ
58 # define PROT_READ 0
59 # endif
60 # ifndef PROT_WRITE
61 # define PROT_WRITE 0
62 # endif
63 # ifndef MAP_PRIVATE
64 # define MAP_PRIVATE 0
65 # endif
66 # ifndef MAP_ANONYMOUS
67 # define MAP_ANONYMOUS 0
68 # endif
69 # ifndef MAP_SHARED
70 # define MAP_SHARED 0
71 # endif
73 # ifndef ENOSYS
74 # define ENOSYS EINVAL
75 # endif
77 static void *
78 gold_mmap(void *, size_t, int, int, int, off_t)
80 errno = ENOSYS;
81 return MAP_FAILED;
84 static int
85 gold_munmap(void *, size_t)
87 errno = ENOSYS;
88 return -1;
91 static void *
92 gold_mremap(void *, size_t, size_t, int)
94 errno = ENOSYS;
95 return MAP_FAILED;
98 #endif
100 #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
101 # define mremap gold_mremap
102 extern "C" void *gold_mremap(void *, size_t, size_t, int);
103 #endif
105 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
106 #ifndef MAP_ANONYMOUS
107 # define MAP_ANONYMOUS MAP_ANON
108 #endif
110 #ifndef MREMAP_MAYMOVE
111 # define MREMAP_MAYMOVE 1
112 #endif
114 #ifndef HAVE_POSIX_FALLOCATE
115 // A dummy, non general, version of posix_fallocate. Here we just set
116 // the file size and hope that there is enough disk space. FIXME: We
117 // could allocate disk space by walking block by block and writing a
118 // zero byte into each block.
119 static int
120 posix_fallocate(int o, off_t offset, off_t len)
122 if (ftruncate(o, offset + len) < 0)
123 return errno;
124 return 0;
126 #endif // !defined(HAVE_POSIX_FALLOCATE)
128 // Mingw does not have S_ISLNK.
129 #ifndef S_ISLNK
130 # define S_ISLNK(mode) 0
131 #endif
133 namespace gold
136 // Output_data variables.
138 bool Output_data::allocated_sizes_are_fixed;
140 // Output_data methods.
142 Output_data::~Output_data()
146 // Return the default alignment for the target size.
148 uint64_t
149 Output_data::default_alignment()
151 return Output_data::default_alignment_for_size(
152 parameters->target().get_size());
155 // Return the default alignment for a size--32 or 64.
157 uint64_t
158 Output_data::default_alignment_for_size(int size)
160 if (size == 32)
161 return 4;
162 else if (size == 64)
163 return 8;
164 else
165 gold_unreachable();
168 // Output_section_header methods. This currently assumes that the
169 // segment and section lists are complete at construction time.
171 Output_section_headers::Output_section_headers(
172 const Layout* layout,
173 const Layout::Segment_list* segment_list,
174 const Layout::Section_list* section_list,
175 const Layout::Section_list* unattached_section_list,
176 const Stringpool* secnamepool,
177 const Output_section* shstrtab_section)
178 : layout_(layout),
179 segment_list_(segment_list),
180 section_list_(section_list),
181 unattached_section_list_(unattached_section_list),
182 secnamepool_(secnamepool),
183 shstrtab_section_(shstrtab_section)
187 // Compute the current data size.
189 off_t
190 Output_section_headers::do_size() const
192 // Count all the sections. Start with 1 for the null section.
193 off_t count = 1;
194 if (!parameters->options().relocatable())
196 for (Layout::Segment_list::const_iterator p =
197 this->segment_list_->begin();
198 p != this->segment_list_->end();
199 ++p)
200 if ((*p)->type() == elfcpp::PT_LOAD)
201 count += (*p)->output_section_count();
203 else
205 for (Layout::Section_list::const_iterator p =
206 this->section_list_->begin();
207 p != this->section_list_->end();
208 ++p)
209 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
210 ++count;
212 count += this->unattached_section_list_->size();
214 const int size = parameters->target().get_size();
215 int shdr_size;
216 if (size == 32)
217 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
218 else if (size == 64)
219 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
220 else
221 gold_unreachable();
223 return count * shdr_size;
226 // Write out the section headers.
228 void
229 Output_section_headers::do_write(Output_file* of)
231 switch (parameters->size_and_endianness())
233 #ifdef HAVE_TARGET_32_LITTLE
234 case Parameters::TARGET_32_LITTLE:
235 this->do_sized_write<32, false>(of);
236 break;
237 #endif
238 #ifdef HAVE_TARGET_32_BIG
239 case Parameters::TARGET_32_BIG:
240 this->do_sized_write<32, true>(of);
241 break;
242 #endif
243 #ifdef HAVE_TARGET_64_LITTLE
244 case Parameters::TARGET_64_LITTLE:
245 this->do_sized_write<64, false>(of);
246 break;
247 #endif
248 #ifdef HAVE_TARGET_64_BIG
249 case Parameters::TARGET_64_BIG:
250 this->do_sized_write<64, true>(of);
251 break;
252 #endif
253 default:
254 gold_unreachable();
258 template<int size, bool big_endian>
259 void
260 Output_section_headers::do_sized_write(Output_file* of)
262 off_t all_shdrs_size = this->data_size();
263 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
265 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
266 unsigned char* v = view;
269 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
270 oshdr.put_sh_name(0);
271 oshdr.put_sh_type(elfcpp::SHT_NULL);
272 oshdr.put_sh_flags(0);
273 oshdr.put_sh_addr(0);
274 oshdr.put_sh_offset(0);
276 size_t section_count = (this->data_size()
277 / elfcpp::Elf_sizes<size>::shdr_size);
278 if (section_count < elfcpp::SHN_LORESERVE)
279 oshdr.put_sh_size(0);
280 else
281 oshdr.put_sh_size(section_count);
283 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
284 if (shstrndx < elfcpp::SHN_LORESERVE)
285 oshdr.put_sh_link(0);
286 else
287 oshdr.put_sh_link(shstrndx);
289 size_t segment_count = this->segment_list_->size();
290 oshdr.put_sh_info(segment_count >= elfcpp::PN_XNUM ? segment_count : 0);
292 oshdr.put_sh_addralign(0);
293 oshdr.put_sh_entsize(0);
296 v += shdr_size;
298 unsigned int shndx = 1;
299 if (!parameters->options().relocatable())
301 for (Layout::Segment_list::const_iterator p =
302 this->segment_list_->begin();
303 p != this->segment_list_->end();
304 ++p)
305 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
306 this->secnamepool_,
308 &shndx);
310 else
312 for (Layout::Section_list::const_iterator p =
313 this->section_list_->begin();
314 p != this->section_list_->end();
315 ++p)
317 // We do unallocated sections below, except that group
318 // sections have to come first.
319 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
320 && (*p)->type() != elfcpp::SHT_GROUP)
321 continue;
322 gold_assert(shndx == (*p)->out_shndx());
323 elfcpp::Shdr_write<size, big_endian> oshdr(v);
324 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
325 v += shdr_size;
326 ++shndx;
330 for (Layout::Section_list::const_iterator p =
331 this->unattached_section_list_->begin();
332 p != this->unattached_section_list_->end();
333 ++p)
335 // For a relocatable link, we did unallocated group sections
336 // above, since they have to come first.
337 if ((*p)->type() == elfcpp::SHT_GROUP
338 && parameters->options().relocatable())
339 continue;
340 gold_assert(shndx == (*p)->out_shndx());
341 elfcpp::Shdr_write<size, big_endian> oshdr(v);
342 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
343 v += shdr_size;
344 ++shndx;
347 of->write_output_view(this->offset(), all_shdrs_size, view);
350 // Output_segment_header methods.
352 Output_segment_headers::Output_segment_headers(
353 const Layout::Segment_list& segment_list)
354 : segment_list_(segment_list)
356 this->set_current_data_size_for_child(this->do_size());
359 void
360 Output_segment_headers::do_write(Output_file* of)
362 switch (parameters->size_and_endianness())
364 #ifdef HAVE_TARGET_32_LITTLE
365 case Parameters::TARGET_32_LITTLE:
366 this->do_sized_write<32, false>(of);
367 break;
368 #endif
369 #ifdef HAVE_TARGET_32_BIG
370 case Parameters::TARGET_32_BIG:
371 this->do_sized_write<32, true>(of);
372 break;
373 #endif
374 #ifdef HAVE_TARGET_64_LITTLE
375 case Parameters::TARGET_64_LITTLE:
376 this->do_sized_write<64, false>(of);
377 break;
378 #endif
379 #ifdef HAVE_TARGET_64_BIG
380 case Parameters::TARGET_64_BIG:
381 this->do_sized_write<64, true>(of);
382 break;
383 #endif
384 default:
385 gold_unreachable();
389 template<int size, bool big_endian>
390 void
391 Output_segment_headers::do_sized_write(Output_file* of)
393 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
394 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
395 gold_assert(all_phdrs_size == this->data_size());
396 unsigned char* view = of->get_output_view(this->offset(),
397 all_phdrs_size);
398 unsigned char* v = view;
399 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
400 p != this->segment_list_.end();
401 ++p)
403 elfcpp::Phdr_write<size, big_endian> ophdr(v);
404 (*p)->write_header(&ophdr);
405 v += phdr_size;
408 gold_assert(v - view == all_phdrs_size);
410 of->write_output_view(this->offset(), all_phdrs_size, view);
413 off_t
414 Output_segment_headers::do_size() const
416 const int size = parameters->target().get_size();
417 int phdr_size;
418 if (size == 32)
419 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
420 else if (size == 64)
421 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
422 else
423 gold_unreachable();
425 return this->segment_list_.size() * phdr_size;
428 // Output_file_header methods.
430 Output_file_header::Output_file_header(const Target* target,
431 const Symbol_table* symtab,
432 const Output_segment_headers* osh)
433 : target_(target),
434 symtab_(symtab),
435 segment_header_(osh),
436 section_header_(NULL),
437 shstrtab_(NULL)
439 this->set_data_size(this->do_size());
442 // Set the section table information for a file header.
444 void
445 Output_file_header::set_section_info(const Output_section_headers* shdrs,
446 const Output_section* shstrtab)
448 this->section_header_ = shdrs;
449 this->shstrtab_ = shstrtab;
452 // Write out the file header.
454 void
455 Output_file_header::do_write(Output_file* of)
457 gold_assert(this->offset() == 0);
459 switch (parameters->size_and_endianness())
461 #ifdef HAVE_TARGET_32_LITTLE
462 case Parameters::TARGET_32_LITTLE:
463 this->do_sized_write<32, false>(of);
464 break;
465 #endif
466 #ifdef HAVE_TARGET_32_BIG
467 case Parameters::TARGET_32_BIG:
468 this->do_sized_write<32, true>(of);
469 break;
470 #endif
471 #ifdef HAVE_TARGET_64_LITTLE
472 case Parameters::TARGET_64_LITTLE:
473 this->do_sized_write<64, false>(of);
474 break;
475 #endif
476 #ifdef HAVE_TARGET_64_BIG
477 case Parameters::TARGET_64_BIG:
478 this->do_sized_write<64, true>(of);
479 break;
480 #endif
481 default:
482 gold_unreachable();
486 // Write out the file header with appropriate size and endianness.
488 template<int size, bool big_endian>
489 void
490 Output_file_header::do_sized_write(Output_file* of)
492 gold_assert(this->offset() == 0);
494 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
495 unsigned char* view = of->get_output_view(0, ehdr_size);
496 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
498 unsigned char e_ident[elfcpp::EI_NIDENT];
499 memset(e_ident, 0, elfcpp::EI_NIDENT);
500 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
501 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
502 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
503 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
504 if (size == 32)
505 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
506 else if (size == 64)
507 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
508 else
509 gold_unreachable();
510 e_ident[elfcpp::EI_DATA] = (big_endian
511 ? elfcpp::ELFDATA2MSB
512 : elfcpp::ELFDATA2LSB);
513 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
514 oehdr.put_e_ident(e_ident);
516 elfcpp::ET e_type;
517 if (parameters->options().relocatable())
518 e_type = elfcpp::ET_REL;
519 else if (parameters->options().output_is_position_independent())
520 e_type = elfcpp::ET_DYN;
521 else
522 e_type = elfcpp::ET_EXEC;
523 oehdr.put_e_type(e_type);
525 oehdr.put_e_machine(this->target_->machine_code());
526 oehdr.put_e_version(elfcpp::EV_CURRENT);
528 oehdr.put_e_entry(this->entry<size>());
530 if (this->segment_header_ == NULL)
531 oehdr.put_e_phoff(0);
532 else
533 oehdr.put_e_phoff(this->segment_header_->offset());
535 oehdr.put_e_shoff(this->section_header_->offset());
536 oehdr.put_e_flags(this->target_->processor_specific_flags());
537 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
539 if (this->segment_header_ == NULL)
541 oehdr.put_e_phentsize(0);
542 oehdr.put_e_phnum(0);
544 else
546 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
547 size_t phnum = (this->segment_header_->data_size()
548 / elfcpp::Elf_sizes<size>::phdr_size);
549 if (phnum > elfcpp::PN_XNUM)
550 phnum = elfcpp::PN_XNUM;
551 oehdr.put_e_phnum(phnum);
554 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
555 size_t section_count = (this->section_header_->data_size()
556 / elfcpp::Elf_sizes<size>::shdr_size);
558 if (section_count < elfcpp::SHN_LORESERVE)
559 oehdr.put_e_shnum(this->section_header_->data_size()
560 / elfcpp::Elf_sizes<size>::shdr_size);
561 else
562 oehdr.put_e_shnum(0);
564 unsigned int shstrndx = this->shstrtab_->out_shndx();
565 if (shstrndx < elfcpp::SHN_LORESERVE)
566 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
567 else
568 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
570 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
571 // the e_ident field.
572 parameters->target().adjust_elf_header(view, ehdr_size);
574 of->write_output_view(0, ehdr_size, view);
577 // Return the value to use for the entry address.
579 template<int size>
580 typename elfcpp::Elf_types<size>::Elf_Addr
581 Output_file_header::entry()
583 const bool should_issue_warning = (parameters->options().entry() != NULL
584 && !parameters->options().relocatable()
585 && !parameters->options().shared());
586 const char* entry = parameters->entry();
587 Symbol* sym = this->symtab_->lookup(entry);
589 typename Sized_symbol<size>::Value_type v;
590 if (sym != NULL)
592 Sized_symbol<size>* ssym;
593 ssym = this->symtab_->get_sized_symbol<size>(sym);
594 if (!ssym->is_defined() && should_issue_warning)
595 gold_warning("entry symbol '%s' exists but is not defined", entry);
596 v = ssym->value();
598 else
600 // We couldn't find the entry symbol. See if we can parse it as
601 // a number. This supports, e.g., -e 0x1000.
602 char* endptr;
603 v = strtoull(entry, &endptr, 0);
604 if (*endptr != '\0')
606 if (should_issue_warning)
607 gold_warning("cannot find entry symbol '%s'", entry);
608 v = 0;
612 return v;
615 // Compute the current data size.
617 off_t
618 Output_file_header::do_size() const
620 const int size = parameters->target().get_size();
621 if (size == 32)
622 return elfcpp::Elf_sizes<32>::ehdr_size;
623 else if (size == 64)
624 return elfcpp::Elf_sizes<64>::ehdr_size;
625 else
626 gold_unreachable();
629 // Output_data_const methods.
631 void
632 Output_data_const::do_write(Output_file* of)
634 of->write(this->offset(), this->data_.data(), this->data_.size());
637 // Output_data_const_buffer methods.
639 void
640 Output_data_const_buffer::do_write(Output_file* of)
642 of->write(this->offset(), this->p_, this->data_size());
645 // Output_section_data methods.
647 // Record the output section, and set the entry size and such.
649 void
650 Output_section_data::set_output_section(Output_section* os)
652 gold_assert(this->output_section_ == NULL);
653 this->output_section_ = os;
654 this->do_adjust_output_section(os);
657 // Return the section index of the output section.
659 unsigned int
660 Output_section_data::do_out_shndx() const
662 gold_assert(this->output_section_ != NULL);
663 return this->output_section_->out_shndx();
666 // Set the alignment, which means we may need to update the alignment
667 // of the output section.
669 void
670 Output_section_data::set_addralign(uint64_t addralign)
672 this->addralign_ = addralign;
673 if (this->output_section_ != NULL
674 && this->output_section_->addralign() < addralign)
675 this->output_section_->set_addralign(addralign);
678 // Output_data_strtab methods.
680 // Set the final data size.
682 void
683 Output_data_strtab::set_final_data_size()
685 this->strtab_->set_string_offsets();
686 this->set_data_size(this->strtab_->get_strtab_size());
689 // Write out a string table.
691 void
692 Output_data_strtab::do_write(Output_file* of)
694 this->strtab_->write(of, this->offset());
697 // Output_reloc methods.
699 // A reloc against a global symbol.
701 template<bool dynamic, int size, bool big_endian>
702 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
703 Symbol* gsym,
704 unsigned int type,
705 Output_data* od,
706 Address address,
707 bool is_relative,
708 bool is_symbolless)
709 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
710 is_relative_(is_relative), is_symbolless_(is_symbolless),
711 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
713 // this->type_ is a bitfield; make sure TYPE fits.
714 gold_assert(this->type_ == type);
715 this->u1_.gsym = gsym;
716 this->u2_.od = od;
717 if (dynamic)
718 this->set_needs_dynsym_index();
721 template<bool dynamic, int size, bool big_endian>
722 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
723 Symbol* gsym,
724 unsigned int type,
725 Sized_relobj<size, big_endian>* relobj,
726 unsigned int shndx,
727 Address address,
728 bool is_relative,
729 bool is_symbolless)
730 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
731 is_relative_(is_relative), is_symbolless_(is_symbolless),
732 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
734 gold_assert(shndx != INVALID_CODE);
735 // this->type_ is a bitfield; make sure TYPE fits.
736 gold_assert(this->type_ == type);
737 this->u1_.gsym = gsym;
738 this->u2_.relobj = relobj;
739 if (dynamic)
740 this->set_needs_dynsym_index();
743 // A reloc against a local symbol.
745 template<bool dynamic, int size, bool big_endian>
746 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
747 Sized_relobj<size, big_endian>* relobj,
748 unsigned int local_sym_index,
749 unsigned int type,
750 Output_data* od,
751 Address address,
752 bool is_relative,
753 bool is_symbolless,
754 bool is_section_symbol,
755 bool use_plt_offset)
756 : address_(address), local_sym_index_(local_sym_index), type_(type),
757 is_relative_(is_relative), is_symbolless_(is_symbolless),
758 is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
759 shndx_(INVALID_CODE)
761 gold_assert(local_sym_index != GSYM_CODE
762 && local_sym_index != INVALID_CODE);
763 // this->type_ is a bitfield; make sure TYPE fits.
764 gold_assert(this->type_ == type);
765 this->u1_.relobj = relobj;
766 this->u2_.od = od;
767 if (dynamic)
768 this->set_needs_dynsym_index();
771 template<bool dynamic, int size, bool big_endian>
772 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
773 Sized_relobj<size, big_endian>* relobj,
774 unsigned int local_sym_index,
775 unsigned int type,
776 unsigned int shndx,
777 Address address,
778 bool is_relative,
779 bool is_symbolless,
780 bool is_section_symbol,
781 bool use_plt_offset)
782 : address_(address), local_sym_index_(local_sym_index), type_(type),
783 is_relative_(is_relative), is_symbolless_(is_symbolless),
784 is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
785 shndx_(shndx)
787 gold_assert(local_sym_index != GSYM_CODE
788 && local_sym_index != INVALID_CODE);
789 gold_assert(shndx != INVALID_CODE);
790 // this->type_ is a bitfield; make sure TYPE fits.
791 gold_assert(this->type_ == type);
792 this->u1_.relobj = relobj;
793 this->u2_.relobj = relobj;
794 if (dynamic)
795 this->set_needs_dynsym_index();
798 // A reloc against the STT_SECTION symbol of an output section.
800 template<bool dynamic, int size, bool big_endian>
801 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
802 Output_section* os,
803 unsigned int type,
804 Output_data* od,
805 Address address)
806 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
807 is_relative_(false), is_symbolless_(false),
808 is_section_symbol_(true), use_plt_offset_(false), shndx_(INVALID_CODE)
810 // this->type_ is a bitfield; make sure TYPE fits.
811 gold_assert(this->type_ == type);
812 this->u1_.os = os;
813 this->u2_.od = od;
814 if (dynamic)
815 this->set_needs_dynsym_index();
816 else
817 os->set_needs_symtab_index();
820 template<bool dynamic, int size, bool big_endian>
821 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
822 Output_section* os,
823 unsigned int type,
824 Sized_relobj<size, big_endian>* relobj,
825 unsigned int shndx,
826 Address address)
827 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
828 is_relative_(false), is_symbolless_(false),
829 is_section_symbol_(true), use_plt_offset_(false), shndx_(shndx)
831 gold_assert(shndx != INVALID_CODE);
832 // this->type_ is a bitfield; make sure TYPE fits.
833 gold_assert(this->type_ == type);
834 this->u1_.os = os;
835 this->u2_.relobj = relobj;
836 if (dynamic)
837 this->set_needs_dynsym_index();
838 else
839 os->set_needs_symtab_index();
842 // An absolute relocation.
844 template<bool dynamic, int size, bool big_endian>
845 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
846 unsigned int type,
847 Output_data* od,
848 Address address)
849 : address_(address), local_sym_index_(0), type_(type),
850 is_relative_(false), is_symbolless_(false),
851 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
853 // this->type_ is a bitfield; make sure TYPE fits.
854 gold_assert(this->type_ == type);
855 this->u1_.relobj = NULL;
856 this->u2_.od = od;
859 template<bool dynamic, int size, bool big_endian>
860 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
861 unsigned int type,
862 Sized_relobj<size, big_endian>* relobj,
863 unsigned int shndx,
864 Address address)
865 : address_(address), local_sym_index_(0), type_(type),
866 is_relative_(false), is_symbolless_(false),
867 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
869 gold_assert(shndx != INVALID_CODE);
870 // this->type_ is a bitfield; make sure TYPE fits.
871 gold_assert(this->type_ == type);
872 this->u1_.relobj = NULL;
873 this->u2_.relobj = relobj;
876 // A target specific relocation.
878 template<bool dynamic, int size, bool big_endian>
879 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
880 unsigned int type,
881 void* arg,
882 Output_data* od,
883 Address address)
884 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
885 is_relative_(false), is_symbolless_(false),
886 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
888 // this->type_ is a bitfield; make sure TYPE fits.
889 gold_assert(this->type_ == type);
890 this->u1_.arg = arg;
891 this->u2_.od = od;
894 template<bool dynamic, int size, bool big_endian>
895 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
896 unsigned int type,
897 void* arg,
898 Sized_relobj<size, big_endian>* relobj,
899 unsigned int shndx,
900 Address address)
901 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
902 is_relative_(false), is_symbolless_(false),
903 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
905 gold_assert(shndx != INVALID_CODE);
906 // this->type_ is a bitfield; make sure TYPE fits.
907 gold_assert(this->type_ == type);
908 this->u1_.arg = arg;
909 this->u2_.relobj = relobj;
912 // Record that we need a dynamic symbol index for this relocation.
914 template<bool dynamic, int size, bool big_endian>
915 void
916 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
917 set_needs_dynsym_index()
919 if (this->is_symbolless_)
920 return;
921 switch (this->local_sym_index_)
923 case INVALID_CODE:
924 gold_unreachable();
926 case GSYM_CODE:
927 this->u1_.gsym->set_needs_dynsym_entry();
928 break;
930 case SECTION_CODE:
931 this->u1_.os->set_needs_dynsym_index();
932 break;
934 case TARGET_CODE:
935 // The target must take care of this if necessary.
936 break;
938 case 0:
939 break;
941 default:
943 const unsigned int lsi = this->local_sym_index_;
944 Sized_relobj_file<size, big_endian>* relobj =
945 this->u1_.relobj->sized_relobj();
946 gold_assert(relobj != NULL);
947 if (!this->is_section_symbol_)
948 relobj->set_needs_output_dynsym_entry(lsi);
949 else
950 relobj->output_section(lsi)->set_needs_dynsym_index();
952 break;
956 // Get the symbol index of a relocation.
958 template<bool dynamic, int size, bool big_endian>
959 unsigned int
960 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
961 const
963 unsigned int index;
964 if (this->is_symbolless_)
965 return 0;
966 switch (this->local_sym_index_)
968 case INVALID_CODE:
969 gold_unreachable();
971 case GSYM_CODE:
972 if (this->u1_.gsym == NULL)
973 index = 0;
974 else if (dynamic)
975 index = this->u1_.gsym->dynsym_index();
976 else
977 index = this->u1_.gsym->symtab_index();
978 break;
980 case SECTION_CODE:
981 if (dynamic)
982 index = this->u1_.os->dynsym_index();
983 else
984 index = this->u1_.os->symtab_index();
985 break;
987 case TARGET_CODE:
988 index = parameters->target().reloc_symbol_index(this->u1_.arg,
989 this->type_);
990 break;
992 case 0:
993 // Relocations without symbols use a symbol index of 0.
994 index = 0;
995 break;
997 default:
999 const unsigned int lsi = this->local_sym_index_;
1000 Sized_relobj_file<size, big_endian>* relobj =
1001 this->u1_.relobj->sized_relobj();
1002 gold_assert(relobj != NULL);
1003 if (!this->is_section_symbol_)
1005 if (dynamic)
1006 index = relobj->dynsym_index(lsi);
1007 else
1008 index = relobj->symtab_index(lsi);
1010 else
1012 Output_section* os = relobj->output_section(lsi);
1013 gold_assert(os != NULL);
1014 if (dynamic)
1015 index = os->dynsym_index();
1016 else
1017 index = os->symtab_index();
1020 break;
1022 gold_assert(index != -1U);
1023 return index;
1026 // For a local section symbol, get the address of the offset ADDEND
1027 // within the input section.
1029 template<bool dynamic, int size, bool big_endian>
1030 typename elfcpp::Elf_types<size>::Elf_Addr
1031 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1032 local_section_offset(Addend addend) const
1034 gold_assert(this->local_sym_index_ != GSYM_CODE
1035 && this->local_sym_index_ != SECTION_CODE
1036 && this->local_sym_index_ != TARGET_CODE
1037 && this->local_sym_index_ != INVALID_CODE
1038 && this->local_sym_index_ != 0
1039 && this->is_section_symbol_);
1040 const unsigned int lsi = this->local_sym_index_;
1041 Output_section* os = this->u1_.relobj->output_section(lsi);
1042 gold_assert(os != NULL);
1043 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
1044 if (offset != invalid_address)
1045 return offset + addend;
1046 // This is a merge section.
1047 Sized_relobj_file<size, big_endian>* relobj =
1048 this->u1_.relobj->sized_relobj();
1049 gold_assert(relobj != NULL);
1050 offset = os->output_address(relobj, lsi, addend);
1051 gold_assert(offset != invalid_address);
1052 return offset;
1055 // Get the output address of a relocation.
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>::get_address() const
1061 Address address = this->address_;
1062 if (this->shndx_ != INVALID_CODE)
1064 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
1065 gold_assert(os != NULL);
1066 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
1067 if (off != invalid_address)
1068 address += os->address() + off;
1069 else
1071 Sized_relobj_file<size, big_endian>* relobj =
1072 this->u2_.relobj->sized_relobj();
1073 gold_assert(relobj != NULL);
1074 address = os->output_address(relobj, this->shndx_, address);
1075 gold_assert(address != invalid_address);
1078 else if (this->u2_.od != NULL)
1079 address += this->u2_.od->address();
1080 return address;
1083 // Write out the offset and info fields of a Rel or Rela relocation
1084 // entry.
1086 template<bool dynamic, int size, bool big_endian>
1087 template<typename Write_rel>
1088 void
1089 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
1090 Write_rel* wr) const
1092 wr->put_r_offset(this->get_address());
1093 unsigned int sym_index = this->get_symbol_index();
1094 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
1097 // Write out a Rel relocation.
1099 template<bool dynamic, int size, bool big_endian>
1100 void
1101 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
1102 unsigned char* pov) const
1104 elfcpp::Rel_write<size, big_endian> orel(pov);
1105 this->write_rel(&orel);
1108 // Get the value of the symbol referred to by a Rel relocation.
1110 template<bool dynamic, int size, bool big_endian>
1111 typename elfcpp::Elf_types<size>::Elf_Addr
1112 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
1113 Addend addend) const
1115 if (this->local_sym_index_ == GSYM_CODE)
1117 const Sized_symbol<size>* sym;
1118 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
1119 return sym->value() + addend;
1121 gold_assert(this->local_sym_index_ != SECTION_CODE
1122 && this->local_sym_index_ != TARGET_CODE
1123 && this->local_sym_index_ != INVALID_CODE
1124 && this->local_sym_index_ != 0
1125 && !this->is_section_symbol_);
1126 const unsigned int lsi = this->local_sym_index_;
1127 Sized_relobj_file<size, big_endian>* relobj =
1128 this->u1_.relobj->sized_relobj();
1129 gold_assert(relobj != NULL);
1130 if (this->use_plt_offset_)
1132 uint64_t plt_address =
1133 parameters->target().plt_address_for_local(relobj, lsi);
1134 return plt_address + relobj->local_plt_offset(lsi);
1136 const Symbol_value<size>* symval = relobj->local_symbol(lsi);
1137 return symval->value(relobj, addend);
1140 // Reloc comparison. This function sorts the dynamic relocs for the
1141 // benefit of the dynamic linker. First we sort all relative relocs
1142 // to the front. Among relative relocs, we sort by output address.
1143 // Among non-relative relocs, we sort by symbol index, then by output
1144 // address.
1146 template<bool dynamic, int size, bool big_endian>
1148 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1149 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1150 const
1152 if (this->is_relative_)
1154 if (!r2.is_relative_)
1155 return -1;
1156 // Otherwise sort by reloc address below.
1158 else if (r2.is_relative_)
1159 return 1;
1160 else
1162 unsigned int sym1 = this->get_symbol_index();
1163 unsigned int sym2 = r2.get_symbol_index();
1164 if (sym1 < sym2)
1165 return -1;
1166 else if (sym1 > sym2)
1167 return 1;
1168 // Otherwise sort by reloc address.
1171 section_offset_type addr1 = this->get_address();
1172 section_offset_type addr2 = r2.get_address();
1173 if (addr1 < addr2)
1174 return -1;
1175 else if (addr1 > addr2)
1176 return 1;
1178 // Final tie breaker, in order to generate the same output on any
1179 // host: reloc type.
1180 unsigned int type1 = this->type_;
1181 unsigned int type2 = r2.type_;
1182 if (type1 < type2)
1183 return -1;
1184 else if (type1 > type2)
1185 return 1;
1187 // These relocs appear to be exactly the same.
1188 return 0;
1191 // Write out a Rela relocation.
1193 template<bool dynamic, int size, bool big_endian>
1194 void
1195 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1196 unsigned char* pov) const
1198 elfcpp::Rela_write<size, big_endian> orel(pov);
1199 this->rel_.write_rel(&orel);
1200 Addend addend = this->addend_;
1201 if (this->rel_.is_target_specific())
1202 addend = parameters->target().reloc_addend(this->rel_.target_arg(),
1203 this->rel_.type(), addend);
1204 else if (this->rel_.is_symbolless())
1205 addend = this->rel_.symbol_value(addend);
1206 else if (this->rel_.is_local_section_symbol())
1207 addend = this->rel_.local_section_offset(addend);
1208 orel.put_r_addend(addend);
1211 // Output_data_reloc_base methods.
1213 // Adjust the output section.
1215 template<int sh_type, bool dynamic, int size, bool big_endian>
1216 void
1217 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1218 ::do_adjust_output_section(Output_section* os)
1220 if (sh_type == elfcpp::SHT_REL)
1221 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1222 else if (sh_type == elfcpp::SHT_RELA)
1223 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1224 else
1225 gold_unreachable();
1227 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1228 // static link. The backends will generate a dynamic reloc section
1229 // to hold this. In that case we don't want to link to the dynsym
1230 // section, because there isn't one.
1231 if (!dynamic)
1232 os->set_should_link_to_symtab();
1233 else if (parameters->doing_static_link())
1235 else
1236 os->set_should_link_to_dynsym();
1239 // Write out relocation data.
1241 template<int sh_type, bool dynamic, int size, bool big_endian>
1242 void
1243 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1244 Output_file* of)
1246 const off_t off = this->offset();
1247 const off_t oview_size = this->data_size();
1248 unsigned char* const oview = of->get_output_view(off, oview_size);
1250 if (this->sort_relocs())
1252 gold_assert(dynamic);
1253 std::sort(this->relocs_.begin(), this->relocs_.end(),
1254 Sort_relocs_comparison());
1257 unsigned char* pov = oview;
1258 for (typename Relocs::const_iterator p = this->relocs_.begin();
1259 p != this->relocs_.end();
1260 ++p)
1262 p->write(pov);
1263 pov += reloc_size;
1266 gold_assert(pov - oview == oview_size);
1268 of->write_output_view(off, oview_size, oview);
1270 // We no longer need the relocation entries.
1271 this->relocs_.clear();
1274 // Class Output_relocatable_relocs.
1276 template<int sh_type, int size, bool big_endian>
1277 void
1278 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1280 this->set_data_size(this->rr_->output_reloc_count()
1281 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1284 // class Output_data_group.
1286 template<int size, bool big_endian>
1287 Output_data_group<size, big_endian>::Output_data_group(
1288 Sized_relobj_file<size, big_endian>* relobj,
1289 section_size_type entry_count,
1290 elfcpp::Elf_Word flags,
1291 std::vector<unsigned int>* input_shndxes)
1292 : Output_section_data(entry_count * 4, 4, false),
1293 relobj_(relobj),
1294 flags_(flags)
1296 this->input_shndxes_.swap(*input_shndxes);
1299 // Write out the section group, which means translating the section
1300 // indexes to apply to the output file.
1302 template<int size, bool big_endian>
1303 void
1304 Output_data_group<size, big_endian>::do_write(Output_file* of)
1306 const off_t off = this->offset();
1307 const section_size_type oview_size =
1308 convert_to_section_size_type(this->data_size());
1309 unsigned char* const oview = of->get_output_view(off, oview_size);
1311 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1312 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1313 ++contents;
1315 for (std::vector<unsigned int>::const_iterator p =
1316 this->input_shndxes_.begin();
1317 p != this->input_shndxes_.end();
1318 ++p, ++contents)
1320 Output_section* os = this->relobj_->output_section(*p);
1322 unsigned int output_shndx;
1323 if (os != NULL)
1324 output_shndx = os->out_shndx();
1325 else
1327 this->relobj_->error(_("section group retained but "
1328 "group element discarded"));
1329 output_shndx = 0;
1332 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1335 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1336 gold_assert(wrote == oview_size);
1338 of->write_output_view(off, oview_size, oview);
1340 // We no longer need this information.
1341 this->input_shndxes_.clear();
1344 // Output_data_got::Got_entry methods.
1346 // Write out the entry.
1348 template<int size, bool big_endian>
1349 void
1350 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1352 Valtype val = 0;
1354 switch (this->local_sym_index_)
1356 case GSYM_CODE:
1358 // If the symbol is resolved locally, we need to write out the
1359 // link-time value, which will be relocated dynamically by a
1360 // RELATIVE relocation.
1361 Symbol* gsym = this->u_.gsym;
1362 if (this->use_plt_offset_ && gsym->has_plt_offset())
1363 val = (parameters->target().plt_address_for_global(gsym)
1364 + gsym->plt_offset());
1365 else
1367 Sized_symbol<size>* sgsym;
1368 // This cast is a bit ugly. We don't want to put a
1369 // virtual method in Symbol, because we want Symbol to be
1370 // as small as possible.
1371 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1372 val = sgsym->value();
1375 break;
1377 case CONSTANT_CODE:
1378 val = this->u_.constant;
1379 break;
1381 case RESERVED_CODE:
1382 // If we're doing an incremental update, don't touch this GOT entry.
1383 if (parameters->incremental_update())
1384 return;
1385 val = this->u_.constant;
1386 break;
1388 default:
1390 const Relobj* object = this->u_.object;
1391 const unsigned int lsi = this->local_sym_index_;
1392 if (!this->use_plt_offset_)
1394 uint64_t lval = object->local_symbol_value(lsi, 0);
1395 val = convert_types<Valtype, uint64_t>(lval);
1397 else
1399 uint64_t plt_address =
1400 parameters->target().plt_address_for_local(object, lsi);
1401 val = plt_address + object->local_plt_offset(lsi);
1404 break;
1407 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1410 // Output_data_got methods.
1412 // Add an entry for a global symbol to the GOT. This returns true if
1413 // this is a new GOT entry, false if the symbol already had a GOT
1414 // entry.
1416 template<int size, bool big_endian>
1417 bool
1418 Output_data_got<size, big_endian>::add_global(
1419 Symbol* gsym,
1420 unsigned int got_type)
1422 if (gsym->has_got_offset(got_type))
1423 return false;
1425 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false));
1426 gsym->set_got_offset(got_type, got_offset);
1427 return true;
1430 // Like add_global, but use the PLT offset.
1432 template<int size, bool big_endian>
1433 bool
1434 Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1435 unsigned int got_type)
1437 if (gsym->has_got_offset(got_type))
1438 return false;
1440 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true));
1441 gsym->set_got_offset(got_type, got_offset);
1442 return true;
1445 // Add an entry for a global symbol to the GOT, and add a dynamic
1446 // relocation of type R_TYPE for the GOT entry.
1448 template<int size, bool big_endian>
1449 void
1450 Output_data_got<size, big_endian>::add_global_with_rel(
1451 Symbol* gsym,
1452 unsigned int got_type,
1453 Output_data_reloc_generic* rel_dyn,
1454 unsigned int r_type)
1456 if (gsym->has_got_offset(got_type))
1457 return;
1459 unsigned int got_offset = this->add_got_entry(Got_entry());
1460 gsym->set_got_offset(got_type, got_offset);
1461 rel_dyn->add_global_generic(gsym, r_type, this, got_offset, 0);
1464 // Add a pair of entries for a global symbol to the GOT, and add
1465 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1466 // If R_TYPE_2 == 0, add the second entry with no relocation.
1467 template<int size, bool big_endian>
1468 void
1469 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1470 Symbol* gsym,
1471 unsigned int got_type,
1472 Output_data_reloc_generic* rel_dyn,
1473 unsigned int r_type_1,
1474 unsigned int r_type_2)
1476 if (gsym->has_got_offset(got_type))
1477 return;
1479 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1480 gsym->set_got_offset(got_type, got_offset);
1481 rel_dyn->add_global_generic(gsym, r_type_1, this, got_offset, 0);
1483 if (r_type_2 != 0)
1484 rel_dyn->add_global_generic(gsym, r_type_2, this,
1485 got_offset + size / 8, 0);
1488 // Add an entry for a local symbol to the GOT. This returns true if
1489 // this is a new GOT entry, false if the symbol already has a GOT
1490 // entry.
1492 template<int size, bool big_endian>
1493 bool
1494 Output_data_got<size, big_endian>::add_local(
1495 Relobj* object,
1496 unsigned int symndx,
1497 unsigned int got_type)
1499 if (object->local_has_got_offset(symndx, got_type))
1500 return false;
1502 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1503 false));
1504 object->set_local_got_offset(symndx, got_type, got_offset);
1505 return true;
1508 // Like add_local, but use the PLT offset.
1510 template<int size, bool big_endian>
1511 bool
1512 Output_data_got<size, big_endian>::add_local_plt(
1513 Relobj* object,
1514 unsigned int symndx,
1515 unsigned int got_type)
1517 if (object->local_has_got_offset(symndx, got_type))
1518 return false;
1520 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1521 true));
1522 object->set_local_got_offset(symndx, got_type, got_offset);
1523 return true;
1526 // Add an entry for a local symbol to the GOT, and add a dynamic
1527 // relocation of type R_TYPE for the GOT entry.
1529 template<int size, bool big_endian>
1530 void
1531 Output_data_got<size, big_endian>::add_local_with_rel(
1532 Relobj* object,
1533 unsigned int symndx,
1534 unsigned int got_type,
1535 Output_data_reloc_generic* rel_dyn,
1536 unsigned int r_type)
1538 if (object->local_has_got_offset(symndx, got_type))
1539 return;
1541 unsigned int got_offset = this->add_got_entry(Got_entry());
1542 object->set_local_got_offset(symndx, got_type, got_offset);
1543 rel_dyn->add_local_generic(object, symndx, r_type, this, got_offset, 0);
1546 // Add a pair of entries for a local symbol to the GOT, and add
1547 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1548 // If R_TYPE_2 == 0, add the second entry with no relocation.
1549 template<int size, bool big_endian>
1550 void
1551 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1552 Relobj* object,
1553 unsigned int symndx,
1554 unsigned int shndx,
1555 unsigned int got_type,
1556 Output_data_reloc_generic* rel_dyn,
1557 unsigned int r_type_1,
1558 unsigned int r_type_2)
1560 if (object->local_has_got_offset(symndx, got_type))
1561 return;
1563 unsigned int got_offset =
1564 this->add_got_entry_pair(Got_entry(),
1565 Got_entry(object, symndx, false));
1566 object->set_local_got_offset(symndx, got_type, got_offset);
1567 Output_section* os = object->output_section(shndx);
1568 rel_dyn->add_output_section_generic(os, r_type_1, this, got_offset, 0);
1570 if (r_type_2 != 0)
1571 rel_dyn->add_output_section_generic(os, r_type_2, this,
1572 got_offset + size / 8, 0);
1575 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1577 template<int size, bool big_endian>
1578 void
1579 Output_data_got<size, big_endian>::reserve_local(
1580 unsigned int i,
1581 Relobj* object,
1582 unsigned int sym_index,
1583 unsigned int got_type)
1585 this->do_reserve_slot(i);
1586 object->set_local_got_offset(sym_index, got_type, this->got_offset(i));
1589 // Reserve a slot in the GOT for a global symbol.
1591 template<int size, bool big_endian>
1592 void
1593 Output_data_got<size, big_endian>::reserve_global(
1594 unsigned int i,
1595 Symbol* gsym,
1596 unsigned int got_type)
1598 this->do_reserve_slot(i);
1599 gsym->set_got_offset(got_type, this->got_offset(i));
1602 // Write out the GOT.
1604 template<int size, bool big_endian>
1605 void
1606 Output_data_got<size, big_endian>::do_write(Output_file* of)
1608 const int add = size / 8;
1610 const off_t off = this->offset();
1611 const off_t oview_size = this->data_size();
1612 unsigned char* const oview = of->get_output_view(off, oview_size);
1614 unsigned char* pov = oview;
1615 for (typename Got_entries::const_iterator p = this->entries_.begin();
1616 p != this->entries_.end();
1617 ++p)
1619 p->write(pov);
1620 pov += add;
1623 gold_assert(pov - oview == oview_size);
1625 of->write_output_view(off, oview_size, oview);
1627 // We no longer need the GOT entries.
1628 this->entries_.clear();
1631 // Create a new GOT entry and return its offset.
1633 template<int size, bool big_endian>
1634 unsigned int
1635 Output_data_got<size, big_endian>::add_got_entry(Got_entry got_entry)
1637 if (!this->is_data_size_valid())
1639 this->entries_.push_back(got_entry);
1640 this->set_got_size();
1641 return this->last_got_offset();
1643 else
1645 // For an incremental update, find an available slot.
1646 off_t got_offset = this->free_list_.allocate(size / 8, size / 8, 0);
1647 if (got_offset == -1)
1648 gold_fallback(_("out of patch space (GOT);"
1649 " relink with --incremental-full"));
1650 unsigned int got_index = got_offset / (size / 8);
1651 gold_assert(got_index < this->entries_.size());
1652 this->entries_[got_index] = got_entry;
1653 return static_cast<unsigned int>(got_offset);
1657 // Create a pair of new GOT entries and return the offset of the first.
1659 template<int size, bool big_endian>
1660 unsigned int
1661 Output_data_got<size, big_endian>::add_got_entry_pair(Got_entry got_entry_1,
1662 Got_entry got_entry_2)
1664 if (!this->is_data_size_valid())
1666 unsigned int got_offset;
1667 this->entries_.push_back(got_entry_1);
1668 got_offset = this->last_got_offset();
1669 this->entries_.push_back(got_entry_2);
1670 this->set_got_size();
1671 return got_offset;
1673 else
1675 // For an incremental update, find an available pair of slots.
1676 off_t got_offset = this->free_list_.allocate(2 * size / 8, size / 8, 0);
1677 if (got_offset == -1)
1678 gold_fallback(_("out of patch space (GOT);"
1679 " relink with --incremental-full"));
1680 unsigned int got_index = got_offset / (size / 8);
1681 gold_assert(got_index < this->entries_.size());
1682 this->entries_[got_index] = got_entry_1;
1683 this->entries_[got_index + 1] = got_entry_2;
1684 return static_cast<unsigned int>(got_offset);
1688 // Output_data_dynamic::Dynamic_entry methods.
1690 // Write out the entry.
1692 template<int size, bool big_endian>
1693 void
1694 Output_data_dynamic::Dynamic_entry::write(
1695 unsigned char* pov,
1696 const Stringpool* pool) const
1698 typename elfcpp::Elf_types<size>::Elf_WXword val;
1699 switch (this->offset_)
1701 case DYNAMIC_NUMBER:
1702 val = this->u_.val;
1703 break;
1705 case DYNAMIC_SECTION_SIZE:
1706 val = this->u_.od->data_size();
1707 if (this->od2 != NULL)
1708 val += this->od2->data_size();
1709 break;
1711 case DYNAMIC_SYMBOL:
1713 const Sized_symbol<size>* s =
1714 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1715 val = s->value();
1717 break;
1719 case DYNAMIC_STRING:
1720 val = pool->get_offset(this->u_.str);
1721 break;
1723 default:
1724 val = this->u_.od->address() + this->offset_;
1725 break;
1728 elfcpp::Dyn_write<size, big_endian> dw(pov);
1729 dw.put_d_tag(this->tag_);
1730 dw.put_d_val(val);
1733 // Output_data_dynamic methods.
1735 // Adjust the output section to set the entry size.
1737 void
1738 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1740 if (parameters->target().get_size() == 32)
1741 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1742 else if (parameters->target().get_size() == 64)
1743 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1744 else
1745 gold_unreachable();
1748 // Set the final data size.
1750 void
1751 Output_data_dynamic::set_final_data_size()
1753 // Add the terminating entry if it hasn't been added.
1754 // Because of relaxation, we can run this multiple times.
1755 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1757 int extra = parameters->options().spare_dynamic_tags();
1758 for (int i = 0; i < extra; ++i)
1759 this->add_constant(elfcpp::DT_NULL, 0);
1760 this->add_constant(elfcpp::DT_NULL, 0);
1763 int dyn_size;
1764 if (parameters->target().get_size() == 32)
1765 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1766 else if (parameters->target().get_size() == 64)
1767 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1768 else
1769 gold_unreachable();
1770 this->set_data_size(this->entries_.size() * dyn_size);
1773 // Write out the dynamic entries.
1775 void
1776 Output_data_dynamic::do_write(Output_file* of)
1778 switch (parameters->size_and_endianness())
1780 #ifdef HAVE_TARGET_32_LITTLE
1781 case Parameters::TARGET_32_LITTLE:
1782 this->sized_write<32, false>(of);
1783 break;
1784 #endif
1785 #ifdef HAVE_TARGET_32_BIG
1786 case Parameters::TARGET_32_BIG:
1787 this->sized_write<32, true>(of);
1788 break;
1789 #endif
1790 #ifdef HAVE_TARGET_64_LITTLE
1791 case Parameters::TARGET_64_LITTLE:
1792 this->sized_write<64, false>(of);
1793 break;
1794 #endif
1795 #ifdef HAVE_TARGET_64_BIG
1796 case Parameters::TARGET_64_BIG:
1797 this->sized_write<64, true>(of);
1798 break;
1799 #endif
1800 default:
1801 gold_unreachable();
1805 template<int size, bool big_endian>
1806 void
1807 Output_data_dynamic::sized_write(Output_file* of)
1809 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1811 const off_t offset = this->offset();
1812 const off_t oview_size = this->data_size();
1813 unsigned char* const oview = of->get_output_view(offset, oview_size);
1815 unsigned char* pov = oview;
1816 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1817 p != this->entries_.end();
1818 ++p)
1820 p->write<size, big_endian>(pov, this->pool_);
1821 pov += dyn_size;
1824 gold_assert(pov - oview == oview_size);
1826 of->write_output_view(offset, oview_size, oview);
1828 // We no longer need the dynamic entries.
1829 this->entries_.clear();
1832 // Class Output_symtab_xindex.
1834 void
1835 Output_symtab_xindex::do_write(Output_file* of)
1837 const off_t offset = this->offset();
1838 const off_t oview_size = this->data_size();
1839 unsigned char* const oview = of->get_output_view(offset, oview_size);
1841 memset(oview, 0, oview_size);
1843 if (parameters->target().is_big_endian())
1844 this->endian_do_write<true>(oview);
1845 else
1846 this->endian_do_write<false>(oview);
1848 of->write_output_view(offset, oview_size, oview);
1850 // We no longer need the data.
1851 this->entries_.clear();
1854 template<bool big_endian>
1855 void
1856 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1858 for (Xindex_entries::const_iterator p = this->entries_.begin();
1859 p != this->entries_.end();
1860 ++p)
1862 unsigned int symndx = p->first;
1863 gold_assert(symndx * 4 < this->data_size());
1864 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1868 // Output_fill_debug_info methods.
1870 // Return the minimum size needed for a dummy compilation unit header.
1872 size_t
1873 Output_fill_debug_info::do_minimum_hole_size() const
1875 // Compile unit header fields: unit_length, version, debug_abbrev_offset,
1876 // address_size.
1877 const size_t len = 4 + 2 + 4 + 1;
1878 // For type units, add type_signature, type_offset.
1879 if (this->is_debug_types_)
1880 return len + 8 + 4;
1881 return len;
1884 // Write a dummy compilation unit header to fill a hole in the
1885 // .debug_info or .debug_types section.
1887 void
1888 Output_fill_debug_info::do_write(Output_file* of, off_t off, size_t len) const
1890 gold_debug(DEBUG_INCREMENTAL, "fill_debug_info(%08lx, %08lx)",
1891 static_cast<long>(off), static_cast<long>(len));
1893 gold_assert(len >= this->do_minimum_hole_size());
1895 unsigned char* const oview = of->get_output_view(off, len);
1896 unsigned char* pov = oview;
1898 // Write header fields: unit_length, version, debug_abbrev_offset,
1899 // address_size.
1900 if (this->is_big_endian())
1902 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
1903 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
1904 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, 0);
1906 else
1908 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
1909 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
1910 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, 0);
1912 pov += 4 + 2 + 4;
1913 *pov++ = 4;
1915 // For type units, the additional header fields -- type_signature,
1916 // type_offset -- can be filled with zeroes.
1918 // Fill the remainder of the free space with zeroes. The first
1919 // zero should tell the consumer there are no DIEs to read in this
1920 // compilation unit.
1921 if (pov < oview + len)
1922 memset(pov, 0, oview + len - pov);
1924 of->write_output_view(off, len, oview);
1927 // Output_fill_debug_line methods.
1929 // Return the minimum size needed for a dummy line number program header.
1931 size_t
1932 Output_fill_debug_line::do_minimum_hole_size() const
1934 // Line number program header fields: unit_length, version, header_length,
1935 // minimum_instruction_length, default_is_stmt, line_base, line_range,
1936 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
1937 const size_t len = 4 + 2 + 4 + this->header_length;
1938 return len;
1941 // Write a dummy line number program header to fill a hole in the
1942 // .debug_line section.
1944 void
1945 Output_fill_debug_line::do_write(Output_file* of, off_t off, size_t len) const
1947 gold_debug(DEBUG_INCREMENTAL, "fill_debug_line(%08lx, %08lx)",
1948 static_cast<long>(off), static_cast<long>(len));
1950 gold_assert(len >= this->do_minimum_hole_size());
1952 unsigned char* const oview = of->get_output_view(off, len);
1953 unsigned char* pov = oview;
1955 // Write header fields: unit_length, version, header_length,
1956 // minimum_instruction_length, default_is_stmt, line_base, line_range,
1957 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
1958 // We set the header_length field to cover the entire hole, so the
1959 // line number program is empty.
1960 if (this->is_big_endian())
1962 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
1963 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
1964 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, len - (4 + 2 + 4));
1966 else
1968 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
1969 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
1970 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, len - (4 + 2 + 4));
1972 pov += 4 + 2 + 4;
1973 *pov++ = 1; // minimum_instruction_length
1974 *pov++ = 0; // default_is_stmt
1975 *pov++ = 0; // line_base
1976 *pov++ = 5; // line_range
1977 *pov++ = 13; // opcode_base
1978 *pov++ = 0; // standard_opcode_lengths[1]
1979 *pov++ = 1; // standard_opcode_lengths[2]
1980 *pov++ = 1; // standard_opcode_lengths[3]
1981 *pov++ = 1; // standard_opcode_lengths[4]
1982 *pov++ = 1; // standard_opcode_lengths[5]
1983 *pov++ = 0; // standard_opcode_lengths[6]
1984 *pov++ = 0; // standard_opcode_lengths[7]
1985 *pov++ = 0; // standard_opcode_lengths[8]
1986 *pov++ = 1; // standard_opcode_lengths[9]
1987 *pov++ = 0; // standard_opcode_lengths[10]
1988 *pov++ = 0; // standard_opcode_lengths[11]
1989 *pov++ = 1; // standard_opcode_lengths[12]
1990 *pov++ = 0; // include_directories (empty)
1991 *pov++ = 0; // filenames (empty)
1993 // Some consumers don't check the header_length field, and simply
1994 // start reading the line number program immediately following the
1995 // header. For those consumers, we fill the remainder of the free
1996 // space with DW_LNS_set_basic_block opcodes. These are effectively
1997 // no-ops: the resulting line table program will not create any rows.
1998 if (pov < oview + len)
1999 memset(pov, elfcpp::DW_LNS_set_basic_block, oview + len - pov);
2001 of->write_output_view(off, len, oview);
2004 // Output_section::Input_section methods.
2006 // Return the current data size. For an input section we store the size here.
2007 // For an Output_section_data, we have to ask it for the size.
2009 off_t
2010 Output_section::Input_section::current_data_size() const
2012 if (this->is_input_section())
2013 return this->u1_.data_size;
2014 else
2016 this->u2_.posd->pre_finalize_data_size();
2017 return this->u2_.posd->current_data_size();
2021 // Return the data size. For an input section we store the size here.
2022 // For an Output_section_data, we have to ask it for the size.
2024 off_t
2025 Output_section::Input_section::data_size() const
2027 if (this->is_input_section())
2028 return this->u1_.data_size;
2029 else
2030 return this->u2_.posd->data_size();
2033 // Return the object for an input section.
2035 Relobj*
2036 Output_section::Input_section::relobj() const
2038 if (this->is_input_section())
2039 return this->u2_.object;
2040 else if (this->is_merge_section())
2042 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2043 return this->u2_.pomb->first_relobj();
2045 else if (this->is_relaxed_input_section())
2046 return this->u2_.poris->relobj();
2047 else
2048 gold_unreachable();
2051 // Return the input section index for an input section.
2053 unsigned int
2054 Output_section::Input_section::shndx() const
2056 if (this->is_input_section())
2057 return this->shndx_;
2058 else if (this->is_merge_section())
2060 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2061 return this->u2_.pomb->first_shndx();
2063 else if (this->is_relaxed_input_section())
2064 return this->u2_.poris->shndx();
2065 else
2066 gold_unreachable();
2069 // Set the address and file offset.
2071 void
2072 Output_section::Input_section::set_address_and_file_offset(
2073 uint64_t address,
2074 off_t file_offset,
2075 off_t section_file_offset)
2077 if (this->is_input_section())
2078 this->u2_.object->set_section_offset(this->shndx_,
2079 file_offset - section_file_offset);
2080 else
2081 this->u2_.posd->set_address_and_file_offset(address, file_offset);
2084 // Reset the address and file offset.
2086 void
2087 Output_section::Input_section::reset_address_and_file_offset()
2089 if (!this->is_input_section())
2090 this->u2_.posd->reset_address_and_file_offset();
2093 // Finalize the data size.
2095 void
2096 Output_section::Input_section::finalize_data_size()
2098 if (!this->is_input_section())
2099 this->u2_.posd->finalize_data_size();
2102 // Try to turn an input offset into an output offset. We want to
2103 // return the output offset relative to the start of this
2104 // Input_section in the output section.
2106 inline bool
2107 Output_section::Input_section::output_offset(
2108 const Relobj* object,
2109 unsigned int shndx,
2110 section_offset_type offset,
2111 section_offset_type* poutput) const
2113 if (!this->is_input_section())
2114 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
2115 else
2117 if (this->shndx_ != shndx || this->u2_.object != object)
2118 return false;
2119 *poutput = offset;
2120 return true;
2124 // Return whether this is the merge section for the input section
2125 // SHNDX in OBJECT.
2127 inline bool
2128 Output_section::Input_section::is_merge_section_for(const Relobj* object,
2129 unsigned int shndx) const
2131 if (this->is_input_section())
2132 return false;
2133 return this->u2_.posd->is_merge_section_for(object, shndx);
2136 // Write out the data. We don't have to do anything for an input
2137 // section--they are handled via Object::relocate--but this is where
2138 // we write out the data for an Output_section_data.
2140 void
2141 Output_section::Input_section::write(Output_file* of)
2143 if (!this->is_input_section())
2144 this->u2_.posd->write(of);
2147 // Write the data to a buffer. As for write(), we don't have to do
2148 // anything for an input section.
2150 void
2151 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2153 if (!this->is_input_section())
2154 this->u2_.posd->write_to_buffer(buffer);
2157 // Print to a map file.
2159 void
2160 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2162 switch (this->shndx_)
2164 case OUTPUT_SECTION_CODE:
2165 case MERGE_DATA_SECTION_CODE:
2166 case MERGE_STRING_SECTION_CODE:
2167 this->u2_.posd->print_to_mapfile(mapfile);
2168 break;
2170 case RELAXED_INPUT_SECTION_CODE:
2172 Output_relaxed_input_section* relaxed_section =
2173 this->relaxed_input_section();
2174 mapfile->print_input_section(relaxed_section->relobj(),
2175 relaxed_section->shndx());
2177 break;
2178 default:
2179 mapfile->print_input_section(this->u2_.object, this->shndx_);
2180 break;
2184 // Output_section methods.
2186 // Construct an Output_section. NAME will point into a Stringpool.
2188 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2189 elfcpp::Elf_Xword flags)
2190 : name_(name),
2191 addralign_(0),
2192 entsize_(0),
2193 load_address_(0),
2194 link_section_(NULL),
2195 link_(0),
2196 info_section_(NULL),
2197 info_symndx_(NULL),
2198 info_(0),
2199 type_(type),
2200 flags_(flags),
2201 order_(ORDER_INVALID),
2202 out_shndx_(-1U),
2203 symtab_index_(0),
2204 dynsym_index_(0),
2205 input_sections_(),
2206 first_input_offset_(0),
2207 fills_(),
2208 postprocessing_buffer_(NULL),
2209 needs_symtab_index_(false),
2210 needs_dynsym_index_(false),
2211 should_link_to_symtab_(false),
2212 should_link_to_dynsym_(false),
2213 after_input_sections_(false),
2214 requires_postprocessing_(false),
2215 found_in_sections_clause_(false),
2216 has_load_address_(false),
2217 info_uses_section_index_(false),
2218 input_section_order_specified_(false),
2219 may_sort_attached_input_sections_(false),
2220 must_sort_attached_input_sections_(false),
2221 attached_input_sections_are_sorted_(false),
2222 is_relro_(false),
2223 is_small_section_(false),
2224 is_large_section_(false),
2225 generate_code_fills_at_write_(false),
2226 is_entsize_zero_(false),
2227 section_offsets_need_adjustment_(false),
2228 is_noload_(false),
2229 always_keeps_input_sections_(false),
2230 has_fixed_layout_(false),
2231 is_patch_space_allowed_(false),
2232 tls_offset_(0),
2233 checkpoint_(NULL),
2234 lookup_maps_(new Output_section_lookup_maps),
2235 free_list_(),
2236 free_space_fill_(NULL),
2237 patch_space_(0)
2239 // An unallocated section has no address. Forcing this means that
2240 // we don't need special treatment for symbols defined in debug
2241 // sections.
2242 if ((flags & elfcpp::SHF_ALLOC) == 0)
2243 this->set_address(0);
2246 Output_section::~Output_section()
2248 delete this->checkpoint_;
2251 // Set the entry size.
2253 void
2254 Output_section::set_entsize(uint64_t v)
2256 if (this->is_entsize_zero_)
2258 else if (this->entsize_ == 0)
2259 this->entsize_ = v;
2260 else if (this->entsize_ != v)
2262 this->entsize_ = 0;
2263 this->is_entsize_zero_ = 1;
2267 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2268 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2269 // relocation section which applies to this section, or 0 if none, or
2270 // -1U if more than one. Return the offset of the input section
2271 // within the output section. Return -1 if the input section will
2272 // receive special handling. In the normal case we don't always keep
2273 // track of input sections for an Output_section. Instead, each
2274 // Object keeps track of the Output_section for each of its input
2275 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2276 // track of input sections here; this is used when SECTIONS appears in
2277 // a linker script.
2279 template<int size, bool big_endian>
2280 off_t
2281 Output_section::add_input_section(Layout* layout,
2282 Sized_relobj_file<size, big_endian>* object,
2283 unsigned int shndx,
2284 const char* secname,
2285 const elfcpp::Shdr<size, big_endian>& shdr,
2286 unsigned int reloc_shndx,
2287 bool have_sections_script)
2289 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2290 if ((addralign & (addralign - 1)) != 0)
2292 object->error(_("invalid alignment %lu for section \"%s\""),
2293 static_cast<unsigned long>(addralign), secname);
2294 addralign = 1;
2297 if (addralign > this->addralign_)
2298 this->addralign_ = addralign;
2300 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2301 uint64_t entsize = shdr.get_sh_entsize();
2303 // .debug_str is a mergeable string section, but is not always so
2304 // marked by compilers. Mark manually here so we can optimize.
2305 if (strcmp(secname, ".debug_str") == 0)
2307 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2308 entsize = 1;
2311 this->update_flags_for_input_section(sh_flags);
2312 this->set_entsize(entsize);
2314 // If this is a SHF_MERGE section, we pass all the input sections to
2315 // a Output_data_merge. We don't try to handle relocations for such
2316 // a section. We don't try to handle empty merge sections--they
2317 // mess up the mappings, and are useless anyhow.
2318 // FIXME: Need to handle merge sections during incremental update.
2319 if ((sh_flags & elfcpp::SHF_MERGE) != 0
2320 && reloc_shndx == 0
2321 && shdr.get_sh_size() > 0
2322 && !parameters->incremental())
2324 // Keep information about merged input sections for rebuilding fast
2325 // lookup maps if we have sections-script or we do relaxation.
2326 bool keeps_input_sections = (this->always_keeps_input_sections_
2327 || have_sections_script
2328 || parameters->target().may_relax());
2330 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2331 addralign, keeps_input_sections))
2333 // Tell the relocation routines that they need to call the
2334 // output_offset method to determine the final address.
2335 return -1;
2339 section_size_type input_section_size = shdr.get_sh_size();
2340 section_size_type uncompressed_size;
2341 if (object->section_is_compressed(shndx, &uncompressed_size))
2342 input_section_size = uncompressed_size;
2344 off_t offset_in_section;
2345 off_t aligned_offset_in_section;
2346 if (this->has_fixed_layout())
2348 // For incremental updates, find a chunk of unused space in the section.
2349 offset_in_section = this->free_list_.allocate(input_section_size,
2350 addralign, 0);
2351 if (offset_in_section == -1)
2352 gold_fallback(_("out of patch space in section %s; "
2353 "relink with --incremental-full"),
2354 this->name());
2355 aligned_offset_in_section = offset_in_section;
2357 else
2359 offset_in_section = this->current_data_size_for_child();
2360 aligned_offset_in_section = align_address(offset_in_section,
2361 addralign);
2362 this->set_current_data_size_for_child(aligned_offset_in_section
2363 + input_section_size);
2366 // Determine if we want to delay code-fill generation until the output
2367 // section is written. When the target is relaxing, we want to delay fill
2368 // generating to avoid adjusting them during relaxation. Also, if we are
2369 // sorting input sections we must delay fill generation.
2370 if (!this->generate_code_fills_at_write_
2371 && !have_sections_script
2372 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2373 && parameters->target().has_code_fill()
2374 && (parameters->target().may_relax()
2375 || layout->is_section_ordering_specified()))
2377 gold_assert(this->fills_.empty());
2378 this->generate_code_fills_at_write_ = true;
2381 if (aligned_offset_in_section > offset_in_section
2382 && !this->generate_code_fills_at_write_
2383 && !have_sections_script
2384 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2385 && parameters->target().has_code_fill())
2387 // We need to add some fill data. Using fill_list_ when
2388 // possible is an optimization, since we will often have fill
2389 // sections without input sections.
2390 off_t fill_len = aligned_offset_in_section - offset_in_section;
2391 if (this->input_sections_.empty())
2392 this->fills_.push_back(Fill(offset_in_section, fill_len));
2393 else
2395 std::string fill_data(parameters->target().code_fill(fill_len));
2396 Output_data_const* odc = new Output_data_const(fill_data, 1);
2397 this->input_sections_.push_back(Input_section(odc));
2401 // We need to keep track of this section if we are already keeping
2402 // track of sections, or if we are relaxing. Also, if this is a
2403 // section which requires sorting, or which may require sorting in
2404 // the future, we keep track of the sections. If the
2405 // --section-ordering-file option is used to specify the order of
2406 // sections, we need to keep track of sections.
2407 if (this->always_keeps_input_sections_
2408 || have_sections_script
2409 || !this->input_sections_.empty()
2410 || this->may_sort_attached_input_sections()
2411 || this->must_sort_attached_input_sections()
2412 || parameters->options().user_set_Map()
2413 || parameters->target().may_relax()
2414 || layout->is_section_ordering_specified())
2416 Input_section isecn(object, shndx, input_section_size, addralign);
2417 /* If section ordering is requested by specifying a ordering file,
2418 using --section-ordering-file, match the section name with
2419 a pattern. */
2420 if (parameters->options().section_ordering_file())
2422 unsigned int section_order_index =
2423 layout->find_section_order_index(std::string(secname));
2424 if (section_order_index != 0)
2426 isecn.set_section_order_index(section_order_index);
2427 this->set_input_section_order_specified();
2430 if (this->has_fixed_layout())
2432 // For incremental updates, finalize the address and offset now.
2433 uint64_t addr = this->address();
2434 isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2435 aligned_offset_in_section,
2436 this->offset());
2438 this->input_sections_.push_back(isecn);
2441 return aligned_offset_in_section;
2444 // Add arbitrary data to an output section.
2446 void
2447 Output_section::add_output_section_data(Output_section_data* posd)
2449 Input_section inp(posd);
2450 this->add_output_section_data(&inp);
2452 if (posd->is_data_size_valid())
2454 off_t offset_in_section;
2455 if (this->has_fixed_layout())
2457 // For incremental updates, find a chunk of unused space.
2458 offset_in_section = this->free_list_.allocate(posd->data_size(),
2459 posd->addralign(), 0);
2460 if (offset_in_section == -1)
2461 gold_fallback(_("out of patch space in section %s; "
2462 "relink with --incremental-full"),
2463 this->name());
2464 // Finalize the address and offset now.
2465 uint64_t addr = this->address();
2466 off_t offset = this->offset();
2467 posd->set_address_and_file_offset(addr + offset_in_section,
2468 offset + offset_in_section);
2470 else
2472 offset_in_section = this->current_data_size_for_child();
2473 off_t aligned_offset_in_section = align_address(offset_in_section,
2474 posd->addralign());
2475 this->set_current_data_size_for_child(aligned_offset_in_section
2476 + posd->data_size());
2479 else if (this->has_fixed_layout())
2481 // For incremental updates, arrange for the data to have a fixed layout.
2482 // This will mean that additions to the data must be allocated from
2483 // free space within the containing output section.
2484 uint64_t addr = this->address();
2485 posd->set_address(addr);
2486 posd->set_file_offset(0);
2487 // FIXME: This should eventually be unreachable.
2488 // gold_unreachable();
2492 // Add a relaxed input section.
2494 void
2495 Output_section::add_relaxed_input_section(Layout* layout,
2496 Output_relaxed_input_section* poris,
2497 const std::string& name)
2499 Input_section inp(poris);
2501 // If the --section-ordering-file option is used to specify the order of
2502 // sections, we need to keep track of sections.
2503 if (layout->is_section_ordering_specified())
2505 unsigned int section_order_index =
2506 layout->find_section_order_index(name);
2507 if (section_order_index != 0)
2509 inp.set_section_order_index(section_order_index);
2510 this->set_input_section_order_specified();
2514 this->add_output_section_data(&inp);
2515 if (this->lookup_maps_->is_valid())
2516 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2517 poris->shndx(), poris);
2519 // For a relaxed section, we use the current data size. Linker scripts
2520 // get all the input sections, including relaxed one from an output
2521 // section and add them back to them same output section to compute the
2522 // output section size. If we do not account for sizes of relaxed input
2523 // sections, an output section would be incorrectly sized.
2524 off_t offset_in_section = this->current_data_size_for_child();
2525 off_t aligned_offset_in_section = align_address(offset_in_section,
2526 poris->addralign());
2527 this->set_current_data_size_for_child(aligned_offset_in_section
2528 + poris->current_data_size());
2531 // Add arbitrary data to an output section by Input_section.
2533 void
2534 Output_section::add_output_section_data(Input_section* inp)
2536 if (this->input_sections_.empty())
2537 this->first_input_offset_ = this->current_data_size_for_child();
2539 this->input_sections_.push_back(*inp);
2541 uint64_t addralign = inp->addralign();
2542 if (addralign > this->addralign_)
2543 this->addralign_ = addralign;
2545 inp->set_output_section(this);
2548 // Add a merge section to an output section.
2550 void
2551 Output_section::add_output_merge_section(Output_section_data* posd,
2552 bool is_string, uint64_t entsize)
2554 Input_section inp(posd, is_string, entsize);
2555 this->add_output_section_data(&inp);
2558 // Add an input section to a SHF_MERGE section.
2560 bool
2561 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2562 uint64_t flags, uint64_t entsize,
2563 uint64_t addralign,
2564 bool keeps_input_sections)
2566 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2568 // We only merge strings if the alignment is not more than the
2569 // character size. This could be handled, but it's unusual.
2570 if (is_string && addralign > entsize)
2571 return false;
2573 // We cannot restore merged input section states.
2574 gold_assert(this->checkpoint_ == NULL);
2576 // Look up merge sections by required properties.
2577 // Currently, we only invalidate the lookup maps in script processing
2578 // and relaxation. We should not have done either when we reach here.
2579 // So we assume that the lookup maps are valid to simply code.
2580 gold_assert(this->lookup_maps_->is_valid());
2581 Merge_section_properties msp(is_string, entsize, addralign);
2582 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2583 bool is_new = false;
2584 if (pomb != NULL)
2586 gold_assert(pomb->is_string() == is_string
2587 && pomb->entsize() == entsize
2588 && pomb->addralign() == addralign);
2590 else
2592 // Create a new Output_merge_data or Output_merge_string_data.
2593 if (!is_string)
2594 pomb = new Output_merge_data(entsize, addralign);
2595 else
2597 switch (entsize)
2599 case 1:
2600 pomb = new Output_merge_string<char>(addralign);
2601 break;
2602 case 2:
2603 pomb = new Output_merge_string<uint16_t>(addralign);
2604 break;
2605 case 4:
2606 pomb = new Output_merge_string<uint32_t>(addralign);
2607 break;
2608 default:
2609 return false;
2612 // If we need to do script processing or relaxation, we need to keep
2613 // the original input sections to rebuild the fast lookup maps.
2614 if (keeps_input_sections)
2615 pomb->set_keeps_input_sections();
2616 is_new = true;
2619 if (pomb->add_input_section(object, shndx))
2621 // Add new merge section to this output section and link merge
2622 // section properties to new merge section in map.
2623 if (is_new)
2625 this->add_output_merge_section(pomb, is_string, entsize);
2626 this->lookup_maps_->add_merge_section(msp, pomb);
2629 // Add input section to new merge section and link input section to new
2630 // merge section in map.
2631 this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
2632 return true;
2634 else
2636 // If add_input_section failed, delete new merge section to avoid
2637 // exporting empty merge sections in Output_section::get_input_section.
2638 if (is_new)
2639 delete pomb;
2640 return false;
2644 // Build a relaxation map to speed up relaxation of existing input sections.
2645 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2647 void
2648 Output_section::build_relaxation_map(
2649 const Input_section_list& input_sections,
2650 size_t limit,
2651 Relaxation_map* relaxation_map) const
2653 for (size_t i = 0; i < limit; ++i)
2655 const Input_section& is(input_sections[i]);
2656 if (is.is_input_section() || is.is_relaxed_input_section())
2658 Section_id sid(is.relobj(), is.shndx());
2659 (*relaxation_map)[sid] = i;
2664 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2665 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2666 // indices of INPUT_SECTIONS.
2668 void
2669 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2670 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2671 const Relaxation_map& map,
2672 Input_section_list* input_sections)
2674 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2676 Output_relaxed_input_section* poris = relaxed_sections[i];
2677 Section_id sid(poris->relobj(), poris->shndx());
2678 Relaxation_map::const_iterator p = map.find(sid);
2679 gold_assert(p != map.end());
2680 gold_assert((*input_sections)[p->second].is_input_section());
2682 // Remember section order index of original input section
2683 // if it is set. Copy it to the relaxed input section.
2684 unsigned int soi =
2685 (*input_sections)[p->second].section_order_index();
2686 (*input_sections)[p->second] = Input_section(poris);
2687 (*input_sections)[p->second].set_section_order_index(soi);
2691 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2692 // is a vector of pointers to Output_relaxed_input_section or its derived
2693 // classes. The relaxed sections must correspond to existing input sections.
2695 void
2696 Output_section::convert_input_sections_to_relaxed_sections(
2697 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2699 gold_assert(parameters->target().may_relax());
2701 // We want to make sure that restore_states does not undo the effect of
2702 // this. If there is no checkpoint active, just search the current
2703 // input section list and replace the sections there. If there is
2704 // a checkpoint, also replace the sections there.
2706 // By default, we look at the whole list.
2707 size_t limit = this->input_sections_.size();
2709 if (this->checkpoint_ != NULL)
2711 // Replace input sections with relaxed input section in the saved
2712 // copy of the input section list.
2713 if (this->checkpoint_->input_sections_saved())
2715 Relaxation_map map;
2716 this->build_relaxation_map(
2717 *(this->checkpoint_->input_sections()),
2718 this->checkpoint_->input_sections()->size(),
2719 &map);
2720 this->convert_input_sections_in_list_to_relaxed_sections(
2721 relaxed_sections,
2722 map,
2723 this->checkpoint_->input_sections());
2725 else
2727 // We have not copied the input section list yet. Instead, just
2728 // look at the portion that would be saved.
2729 limit = this->checkpoint_->input_sections_size();
2733 // Convert input sections in input_section_list.
2734 Relaxation_map map;
2735 this->build_relaxation_map(this->input_sections_, limit, &map);
2736 this->convert_input_sections_in_list_to_relaxed_sections(
2737 relaxed_sections,
2738 map,
2739 &this->input_sections_);
2741 // Update fast look-up map.
2742 if (this->lookup_maps_->is_valid())
2743 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2745 Output_relaxed_input_section* poris = relaxed_sections[i];
2746 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2747 poris->shndx(), poris);
2751 // Update the output section flags based on input section flags.
2753 void
2754 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2756 // If we created the section with SHF_ALLOC clear, we set the
2757 // address. If we are now setting the SHF_ALLOC flag, we need to
2758 // undo that.
2759 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2760 && (flags & elfcpp::SHF_ALLOC) != 0)
2761 this->mark_address_invalid();
2763 this->flags_ |= (flags
2764 & (elfcpp::SHF_WRITE
2765 | elfcpp::SHF_ALLOC
2766 | elfcpp::SHF_EXECINSTR));
2768 if ((flags & elfcpp::SHF_MERGE) == 0)
2769 this->flags_ &=~ elfcpp::SHF_MERGE;
2770 else
2772 if (this->current_data_size_for_child() == 0)
2773 this->flags_ |= elfcpp::SHF_MERGE;
2776 if ((flags & elfcpp::SHF_STRINGS) == 0)
2777 this->flags_ &=~ elfcpp::SHF_STRINGS;
2778 else
2780 if (this->current_data_size_for_child() == 0)
2781 this->flags_ |= elfcpp::SHF_STRINGS;
2785 // Find the merge section into which an input section with index SHNDX in
2786 // OBJECT has been added. Return NULL if none found.
2788 Output_section_data*
2789 Output_section::find_merge_section(const Relobj* object,
2790 unsigned int shndx) const
2792 if (!this->lookup_maps_->is_valid())
2793 this->build_lookup_maps();
2794 return this->lookup_maps_->find_merge_section(object, shndx);
2797 // Build the lookup maps for merge and relaxed sections. This is needs
2798 // to be declared as a const methods so that it is callable with a const
2799 // Output_section pointer. The method only updates states of the maps.
2801 void
2802 Output_section::build_lookup_maps() const
2804 this->lookup_maps_->clear();
2805 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2806 p != this->input_sections_.end();
2807 ++p)
2809 if (p->is_merge_section())
2811 Output_merge_base* pomb = p->output_merge_base();
2812 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2813 pomb->addralign());
2814 this->lookup_maps_->add_merge_section(msp, pomb);
2815 for (Output_merge_base::Input_sections::const_iterator is =
2816 pomb->input_sections_begin();
2817 is != pomb->input_sections_end();
2818 ++is)
2820 const Const_section_id& csid = *is;
2821 this->lookup_maps_->add_merge_input_section(csid.first,
2822 csid.second, pomb);
2826 else if (p->is_relaxed_input_section())
2828 Output_relaxed_input_section* poris = p->relaxed_input_section();
2829 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2830 poris->shndx(), poris);
2835 // Find an relaxed input section corresponding to an input section
2836 // in OBJECT with index SHNDX.
2838 const Output_relaxed_input_section*
2839 Output_section::find_relaxed_input_section(const Relobj* object,
2840 unsigned int shndx) const
2842 if (!this->lookup_maps_->is_valid())
2843 this->build_lookup_maps();
2844 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2847 // Given an address OFFSET relative to the start of input section
2848 // SHNDX in OBJECT, return whether this address is being included in
2849 // the final link. This should only be called if SHNDX in OBJECT has
2850 // a special mapping.
2852 bool
2853 Output_section::is_input_address_mapped(const Relobj* object,
2854 unsigned int shndx,
2855 off_t offset) const
2857 // Look at the Output_section_data_maps first.
2858 const Output_section_data* posd = this->find_merge_section(object, shndx);
2859 if (posd == NULL)
2860 posd = this->find_relaxed_input_section(object, shndx);
2862 if (posd != NULL)
2864 section_offset_type output_offset;
2865 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2866 gold_assert(found);
2867 return output_offset != -1;
2870 // Fall back to the slow look-up.
2871 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2872 p != this->input_sections_.end();
2873 ++p)
2875 section_offset_type output_offset;
2876 if (p->output_offset(object, shndx, offset, &output_offset))
2877 return output_offset != -1;
2880 // By default we assume that the address is mapped. This should
2881 // only be called after we have passed all sections to Layout. At
2882 // that point we should know what we are discarding.
2883 return true;
2886 // Given an address OFFSET relative to the start of input section
2887 // SHNDX in object OBJECT, return the output offset relative to the
2888 // start of the input section in the output section. This should only
2889 // be called if SHNDX in OBJECT has a special mapping.
2891 section_offset_type
2892 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2893 section_offset_type offset) const
2895 // This can only be called meaningfully when we know the data size
2896 // of this.
2897 gold_assert(this->is_data_size_valid());
2899 // Look at the Output_section_data_maps first.
2900 const Output_section_data* posd = this->find_merge_section(object, shndx);
2901 if (posd == NULL)
2902 posd = this->find_relaxed_input_section(object, shndx);
2903 if (posd != NULL)
2905 section_offset_type output_offset;
2906 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2907 gold_assert(found);
2908 return output_offset;
2911 // Fall back to the slow look-up.
2912 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2913 p != this->input_sections_.end();
2914 ++p)
2916 section_offset_type output_offset;
2917 if (p->output_offset(object, shndx, offset, &output_offset))
2918 return output_offset;
2920 gold_unreachable();
2923 // Return the output virtual address of OFFSET relative to the start
2924 // of input section SHNDX in object OBJECT.
2926 uint64_t
2927 Output_section::output_address(const Relobj* object, unsigned int shndx,
2928 off_t offset) const
2930 uint64_t addr = this->address() + this->first_input_offset_;
2932 // Look at the Output_section_data_maps first.
2933 const Output_section_data* posd = this->find_merge_section(object, shndx);
2934 if (posd == NULL)
2935 posd = this->find_relaxed_input_section(object, shndx);
2936 if (posd != NULL && posd->is_address_valid())
2938 section_offset_type output_offset;
2939 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2940 gold_assert(found);
2941 return posd->address() + output_offset;
2944 // Fall back to the slow look-up.
2945 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2946 p != this->input_sections_.end();
2947 ++p)
2949 addr = align_address(addr, p->addralign());
2950 section_offset_type output_offset;
2951 if (p->output_offset(object, shndx, offset, &output_offset))
2953 if (output_offset == -1)
2954 return -1ULL;
2955 return addr + output_offset;
2957 addr += p->data_size();
2960 // If we get here, it means that we don't know the mapping for this
2961 // input section. This might happen in principle if
2962 // add_input_section were called before add_output_section_data.
2963 // But it should never actually happen.
2965 gold_unreachable();
2968 // Find the output address of the start of the merged section for
2969 // input section SHNDX in object OBJECT.
2971 bool
2972 Output_section::find_starting_output_address(const Relobj* object,
2973 unsigned int shndx,
2974 uint64_t* paddr) const
2976 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2977 // Looking up the merge section map does not always work as we sometimes
2978 // find a merge section without its address set.
2979 uint64_t addr = this->address() + this->first_input_offset_;
2980 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2981 p != this->input_sections_.end();
2982 ++p)
2984 addr = align_address(addr, p->addralign());
2986 // It would be nice if we could use the existing output_offset
2987 // method to get the output offset of input offset 0.
2988 // Unfortunately we don't know for sure that input offset 0 is
2989 // mapped at all.
2990 if (p->is_merge_section_for(object, shndx))
2992 *paddr = addr;
2993 return true;
2996 addr += p->data_size();
2999 // We couldn't find a merge output section for this input section.
3000 return false;
3003 // Update the data size of an Output_section.
3005 void
3006 Output_section::update_data_size()
3008 if (this->input_sections_.empty())
3009 return;
3011 if (this->must_sort_attached_input_sections()
3012 || this->input_section_order_specified())
3013 this->sort_attached_input_sections();
3015 off_t off = this->first_input_offset_;
3016 for (Input_section_list::iterator p = this->input_sections_.begin();
3017 p != this->input_sections_.end();
3018 ++p)
3020 off = align_address(off, p->addralign());
3021 off += p->current_data_size();
3024 this->set_current_data_size_for_child(off);
3027 // Set the data size of an Output_section. This is where we handle
3028 // setting the addresses of any Output_section_data objects.
3030 void
3031 Output_section::set_final_data_size()
3033 off_t data_size;
3035 if (this->input_sections_.empty())
3036 data_size = this->current_data_size_for_child();
3037 else
3039 if (this->must_sort_attached_input_sections()
3040 || this->input_section_order_specified())
3041 this->sort_attached_input_sections();
3043 uint64_t address = this->address();
3044 off_t startoff = this->offset();
3045 off_t off = startoff + this->first_input_offset_;
3046 for (Input_section_list::iterator p = this->input_sections_.begin();
3047 p != this->input_sections_.end();
3048 ++p)
3050 off = align_address(off, p->addralign());
3051 p->set_address_and_file_offset(address + (off - startoff), off,
3052 startoff);
3053 off += p->data_size();
3055 data_size = off - startoff;
3058 // For full incremental links, we want to allocate some patch space
3059 // in most sections for subsequent incremental updates.
3060 if (this->is_patch_space_allowed_ && parameters->incremental_full())
3062 double pct = parameters->options().incremental_patch();
3063 size_t extra = static_cast<size_t>(data_size * pct);
3064 if (this->free_space_fill_ != NULL
3065 && this->free_space_fill_->minimum_hole_size() > extra)
3066 extra = this->free_space_fill_->minimum_hole_size();
3067 off_t new_size = align_address(data_size + extra, this->addralign());
3068 this->patch_space_ = new_size - data_size;
3069 gold_debug(DEBUG_INCREMENTAL,
3070 "set_final_data_size: %08lx + %08lx: section %s",
3071 static_cast<long>(data_size),
3072 static_cast<long>(this->patch_space_),
3073 this->name());
3074 data_size = new_size;
3077 this->set_data_size(data_size);
3080 // Reset the address and file offset.
3082 void
3083 Output_section::do_reset_address_and_file_offset()
3085 // An unallocated section has no address. Forcing this means that
3086 // we don't need special treatment for symbols defined in debug
3087 // sections. We do the same in the constructor. This does not
3088 // apply to NOLOAD sections though.
3089 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
3090 this->set_address(0);
3092 for (Input_section_list::iterator p = this->input_sections_.begin();
3093 p != this->input_sections_.end();
3094 ++p)
3095 p->reset_address_and_file_offset();
3097 // Remove any patch space that was added in set_final_data_size.
3098 if (this->patch_space_ > 0)
3100 this->set_current_data_size_for_child(this->current_data_size_for_child()
3101 - this->patch_space_);
3102 this->patch_space_ = 0;
3106 // Return true if address and file offset have the values after reset.
3108 bool
3109 Output_section::do_address_and_file_offset_have_reset_values() const
3111 if (this->is_offset_valid())
3112 return false;
3114 // An unallocated section has address 0 after its construction or a reset.
3115 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
3116 return this->is_address_valid() && this->address() == 0;
3117 else
3118 return !this->is_address_valid();
3121 // Set the TLS offset. Called only for SHT_TLS sections.
3123 void
3124 Output_section::do_set_tls_offset(uint64_t tls_base)
3126 this->tls_offset_ = this->address() - tls_base;
3129 // In a few cases we need to sort the input sections attached to an
3130 // output section. This is used to implement the type of constructor
3131 // priority ordering implemented by the GNU linker, in which the
3132 // priority becomes part of the section name and the sections are
3133 // sorted by name. We only do this for an output section if we see an
3134 // attached input section matching ".ctors.*", ".dtors.*",
3135 // ".init_array.*" or ".fini_array.*".
3137 class Output_section::Input_section_sort_entry
3139 public:
3140 Input_section_sort_entry()
3141 : input_section_(), index_(-1U), section_has_name_(false),
3142 section_name_()
3145 Input_section_sort_entry(const Input_section& input_section,
3146 unsigned int index,
3147 bool must_sort_attached_input_sections)
3148 : input_section_(input_section), index_(index),
3149 section_has_name_(input_section.is_input_section()
3150 || input_section.is_relaxed_input_section())
3152 if (this->section_has_name_
3153 && must_sort_attached_input_sections)
3155 // This is only called single-threaded from Layout::finalize,
3156 // so it is OK to lock. Unfortunately we have no way to pass
3157 // in a Task token.
3158 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
3159 Object* obj = (input_section.is_input_section()
3160 ? input_section.relobj()
3161 : input_section.relaxed_input_section()->relobj());
3162 Task_lock_obj<Object> tl(dummy_task, obj);
3164 // This is a slow operation, which should be cached in
3165 // Layout::layout if this becomes a speed problem.
3166 this->section_name_ = obj->section_name(input_section.shndx());
3170 // Return the Input_section.
3171 const Input_section&
3172 input_section() const
3174 gold_assert(this->index_ != -1U);
3175 return this->input_section_;
3178 // The index of this entry in the original list. This is used to
3179 // make the sort stable.
3180 unsigned int
3181 index() const
3183 gold_assert(this->index_ != -1U);
3184 return this->index_;
3187 // Whether there is a section name.
3188 bool
3189 section_has_name() const
3190 { return this->section_has_name_; }
3192 // The section name.
3193 const std::string&
3194 section_name() const
3196 gold_assert(this->section_has_name_);
3197 return this->section_name_;
3200 // Return true if the section name has a priority. This is assumed
3201 // to be true if it has a dot after the initial dot.
3202 bool
3203 has_priority() const
3205 gold_assert(this->section_has_name_);
3206 return this->section_name_.find('.', 1) != std::string::npos;
3209 // Return the priority. Believe it or not, gcc encodes the priority
3210 // differently for .ctors/.dtors and .init_array/.fini_array
3211 // sections.
3212 unsigned int
3213 get_priority() const
3215 gold_assert(this->section_has_name_);
3216 bool is_ctors;
3217 if (is_prefix_of(".ctors.", this->section_name_.c_str())
3218 || is_prefix_of(".dtors.", this->section_name_.c_str()))
3219 is_ctors = true;
3220 else if (is_prefix_of(".init_array.", this->section_name_.c_str())
3221 || is_prefix_of(".fini_array.", this->section_name_.c_str()))
3222 is_ctors = false;
3223 else
3224 return 0;
3225 char* end;
3226 unsigned long prio = strtoul((this->section_name_.c_str()
3227 + (is_ctors ? 7 : 12)),
3228 &end, 10);
3229 if (*end != '\0')
3230 return 0;
3231 else if (is_ctors)
3232 return 65535 - prio;
3233 else
3234 return prio;
3237 // Return true if this an input file whose base name matches
3238 // FILE_NAME. The base name must have an extension of ".o", and
3239 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3240 // This is to match crtbegin.o as well as crtbeginS.o without
3241 // getting confused by other possibilities. Overall matching the
3242 // file name this way is a dreadful hack, but the GNU linker does it
3243 // in order to better support gcc, and we need to be compatible.
3244 bool
3245 match_file_name(const char* file_name) const
3246 { return Layout::match_file_name(this->input_section_.relobj(), file_name); }
3248 // Returns 1 if THIS should appear before S in section order, -1 if S
3249 // appears before THIS and 0 if they are not comparable.
3251 compare_section_ordering(const Input_section_sort_entry& s) const
3253 unsigned int this_secn_index = this->input_section_.section_order_index();
3254 unsigned int s_secn_index = s.input_section().section_order_index();
3255 if (this_secn_index > 0 && s_secn_index > 0)
3257 if (this_secn_index < s_secn_index)
3258 return 1;
3259 else if (this_secn_index > s_secn_index)
3260 return -1;
3262 return 0;
3265 private:
3266 // The Input_section we are sorting.
3267 Input_section input_section_;
3268 // The index of this Input_section in the original list.
3269 unsigned int index_;
3270 // Whether this Input_section has a section name--it won't if this
3271 // is some random Output_section_data.
3272 bool section_has_name_;
3273 // The section name if there is one.
3274 std::string section_name_;
3277 // Return true if S1 should come before S2 in the output section.
3279 bool
3280 Output_section::Input_section_sort_compare::operator()(
3281 const Output_section::Input_section_sort_entry& s1,
3282 const Output_section::Input_section_sort_entry& s2) const
3284 // crtbegin.o must come first.
3285 bool s1_begin = s1.match_file_name("crtbegin");
3286 bool s2_begin = s2.match_file_name("crtbegin");
3287 if (s1_begin || s2_begin)
3289 if (!s1_begin)
3290 return false;
3291 if (!s2_begin)
3292 return true;
3293 return s1.index() < s2.index();
3296 // crtend.o must come last.
3297 bool s1_end = s1.match_file_name("crtend");
3298 bool s2_end = s2.match_file_name("crtend");
3299 if (s1_end || s2_end)
3301 if (!s1_end)
3302 return true;
3303 if (!s2_end)
3304 return false;
3305 return s1.index() < s2.index();
3308 // We sort all the sections with no names to the end.
3309 if (!s1.section_has_name() || !s2.section_has_name())
3311 if (s1.section_has_name())
3312 return true;
3313 if (s2.section_has_name())
3314 return false;
3315 return s1.index() < s2.index();
3318 // A section with a priority follows a section without a priority.
3319 bool s1_has_priority = s1.has_priority();
3320 bool s2_has_priority = s2.has_priority();
3321 if (s1_has_priority && !s2_has_priority)
3322 return false;
3323 if (!s1_has_priority && s2_has_priority)
3324 return true;
3326 // Check if a section order exists for these sections through a section
3327 // ordering file. If sequence_num is 0, an order does not exist.
3328 int sequence_num = s1.compare_section_ordering(s2);
3329 if (sequence_num != 0)
3330 return sequence_num == 1;
3332 // Otherwise we sort by name.
3333 int compare = s1.section_name().compare(s2.section_name());
3334 if (compare != 0)
3335 return compare < 0;
3337 // Otherwise we keep the input order.
3338 return s1.index() < s2.index();
3341 // Return true if S1 should come before S2 in an .init_array or .fini_array
3342 // output section.
3344 bool
3345 Output_section::Input_section_sort_init_fini_compare::operator()(
3346 const Output_section::Input_section_sort_entry& s1,
3347 const Output_section::Input_section_sort_entry& s2) const
3349 // We sort all the sections with no names to the end.
3350 if (!s1.section_has_name() || !s2.section_has_name())
3352 if (s1.section_has_name())
3353 return true;
3354 if (s2.section_has_name())
3355 return false;
3356 return s1.index() < s2.index();
3359 // A section without a priority follows a section with a priority.
3360 // This is the reverse of .ctors and .dtors sections.
3361 bool s1_has_priority = s1.has_priority();
3362 bool s2_has_priority = s2.has_priority();
3363 if (s1_has_priority && !s2_has_priority)
3364 return true;
3365 if (!s1_has_priority && s2_has_priority)
3366 return false;
3368 // .ctors and .dtors sections without priority come after
3369 // .init_array and .fini_array sections without priority.
3370 if (!s1_has_priority
3371 && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors")
3372 && s1.section_name() != s2.section_name())
3373 return false;
3374 if (!s2_has_priority
3375 && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors")
3376 && s2.section_name() != s1.section_name())
3377 return true;
3379 // Sort by priority if we can.
3380 if (s1_has_priority)
3382 unsigned int s1_prio = s1.get_priority();
3383 unsigned int s2_prio = s2.get_priority();
3384 if (s1_prio < s2_prio)
3385 return true;
3386 else if (s1_prio > s2_prio)
3387 return false;
3390 // Check if a section order exists for these sections through a section
3391 // ordering file. If sequence_num is 0, an order does not exist.
3392 int sequence_num = s1.compare_section_ordering(s2);
3393 if (sequence_num != 0)
3394 return sequence_num == 1;
3396 // Otherwise we sort by name.
3397 int compare = s1.section_name().compare(s2.section_name());
3398 if (compare != 0)
3399 return compare < 0;
3401 // Otherwise we keep the input order.
3402 return s1.index() < s2.index();
3405 // Return true if S1 should come before S2. Sections that do not match
3406 // any pattern in the section ordering file are placed ahead of the sections
3407 // that match some pattern.
3409 bool
3410 Output_section::Input_section_sort_section_order_index_compare::operator()(
3411 const Output_section::Input_section_sort_entry& s1,
3412 const Output_section::Input_section_sort_entry& s2) const
3414 unsigned int s1_secn_index = s1.input_section().section_order_index();
3415 unsigned int s2_secn_index = s2.input_section().section_order_index();
3417 // Keep input order if section ordering cannot determine order.
3418 if (s1_secn_index == s2_secn_index)
3419 return s1.index() < s2.index();
3421 return s1_secn_index < s2_secn_index;
3424 // This updates the section order index of input sections according to the
3425 // the order specified in the mapping from Section id to order index.
3427 void
3428 Output_section::update_section_layout(
3429 const Section_layout_order* order_map)
3431 for (Input_section_list::iterator p = this->input_sections_.begin();
3432 p != this->input_sections_.end();
3433 ++p)
3435 if (p->is_input_section()
3436 || p->is_relaxed_input_section())
3438 Object* obj = (p->is_input_section()
3439 ? p->relobj()
3440 : p->relaxed_input_section()->relobj());
3441 unsigned int shndx = p->shndx();
3442 Section_layout_order::const_iterator it
3443 = order_map->find(Section_id(obj, shndx));
3444 if (it == order_map->end())
3445 continue;
3446 unsigned int section_order_index = it->second;
3447 if (section_order_index != 0)
3449 p->set_section_order_index(section_order_index);
3450 this->set_input_section_order_specified();
3456 // Sort the input sections attached to an output section.
3458 void
3459 Output_section::sort_attached_input_sections()
3461 if (this->attached_input_sections_are_sorted_)
3462 return;
3464 if (this->checkpoint_ != NULL
3465 && !this->checkpoint_->input_sections_saved())
3466 this->checkpoint_->save_input_sections();
3468 // The only thing we know about an input section is the object and
3469 // the section index. We need the section name. Recomputing this
3470 // is slow but this is an unusual case. If this becomes a speed
3471 // problem we can cache the names as required in Layout::layout.
3473 // We start by building a larger vector holding a copy of each
3474 // Input_section, plus its current index in the list and its name.
3475 std::vector<Input_section_sort_entry> sort_list;
3477 unsigned int i = 0;
3478 for (Input_section_list::iterator p = this->input_sections_.begin();
3479 p != this->input_sections_.end();
3480 ++p, ++i)
3481 sort_list.push_back(Input_section_sort_entry(*p, i,
3482 this->must_sort_attached_input_sections()));
3484 // Sort the input sections.
3485 if (this->must_sort_attached_input_sections())
3487 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3488 || this->type() == elfcpp::SHT_INIT_ARRAY
3489 || this->type() == elfcpp::SHT_FINI_ARRAY)
3490 std::sort(sort_list.begin(), sort_list.end(),
3491 Input_section_sort_init_fini_compare());
3492 else
3493 std::sort(sort_list.begin(), sort_list.end(),
3494 Input_section_sort_compare());
3496 else
3498 gold_assert(this->input_section_order_specified());
3499 std::sort(sort_list.begin(), sort_list.end(),
3500 Input_section_sort_section_order_index_compare());
3503 // Copy the sorted input sections back to our list.
3504 this->input_sections_.clear();
3505 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3506 p != sort_list.end();
3507 ++p)
3508 this->input_sections_.push_back(p->input_section());
3509 sort_list.clear();
3511 // Remember that we sorted the input sections, since we might get
3512 // called again.
3513 this->attached_input_sections_are_sorted_ = true;
3516 // Write the section header to *OSHDR.
3518 template<int size, bool big_endian>
3519 void
3520 Output_section::write_header(const Layout* layout,
3521 const Stringpool* secnamepool,
3522 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3524 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3525 oshdr->put_sh_type(this->type_);
3527 elfcpp::Elf_Xword flags = this->flags_;
3528 if (this->info_section_ != NULL && this->info_uses_section_index_)
3529 flags |= elfcpp::SHF_INFO_LINK;
3530 oshdr->put_sh_flags(flags);
3532 oshdr->put_sh_addr(this->address());
3533 oshdr->put_sh_offset(this->offset());
3534 oshdr->put_sh_size(this->data_size());
3535 if (this->link_section_ != NULL)
3536 oshdr->put_sh_link(this->link_section_->out_shndx());
3537 else if (this->should_link_to_symtab_)
3538 oshdr->put_sh_link(layout->symtab_section_shndx());
3539 else if (this->should_link_to_dynsym_)
3540 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3541 else
3542 oshdr->put_sh_link(this->link_);
3544 elfcpp::Elf_Word info;
3545 if (this->info_section_ != NULL)
3547 if (this->info_uses_section_index_)
3548 info = this->info_section_->out_shndx();
3549 else
3550 info = this->info_section_->symtab_index();
3552 else if (this->info_symndx_ != NULL)
3553 info = this->info_symndx_->symtab_index();
3554 else
3555 info = this->info_;
3556 oshdr->put_sh_info(info);
3558 oshdr->put_sh_addralign(this->addralign_);
3559 oshdr->put_sh_entsize(this->entsize_);
3562 // Write out the data. For input sections the data is written out by
3563 // Object::relocate, but we have to handle Output_section_data objects
3564 // here.
3566 void
3567 Output_section::do_write(Output_file* of)
3569 gold_assert(!this->requires_postprocessing());
3571 // If the target performs relaxation, we delay filler generation until now.
3572 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3574 off_t output_section_file_offset = this->offset();
3575 for (Fill_list::iterator p = this->fills_.begin();
3576 p != this->fills_.end();
3577 ++p)
3579 std::string fill_data(parameters->target().code_fill(p->length()));
3580 of->write(output_section_file_offset + p->section_offset(),
3581 fill_data.data(), fill_data.size());
3584 off_t off = this->offset() + this->first_input_offset_;
3585 for (Input_section_list::iterator p = this->input_sections_.begin();
3586 p != this->input_sections_.end();
3587 ++p)
3589 off_t aligned_off = align_address(off, p->addralign());
3590 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3592 size_t fill_len = aligned_off - off;
3593 std::string fill_data(parameters->target().code_fill(fill_len));
3594 of->write(off, fill_data.data(), fill_data.size());
3597 p->write(of);
3598 off = aligned_off + p->data_size();
3601 // For incremental links, fill in unused chunks in debug sections
3602 // with dummy compilation unit headers.
3603 if (this->free_space_fill_ != NULL)
3605 for (Free_list::Const_iterator p = this->free_list_.begin();
3606 p != this->free_list_.end();
3607 ++p)
3609 off_t off = p->start_;
3610 size_t len = p->end_ - off;
3611 this->free_space_fill_->write(of, this->offset() + off, len);
3613 if (this->patch_space_ > 0)
3615 off_t off = this->current_data_size_for_child() - this->patch_space_;
3616 this->free_space_fill_->write(of, this->offset() + off,
3617 this->patch_space_);
3622 // If a section requires postprocessing, create the buffer to use.
3624 void
3625 Output_section::create_postprocessing_buffer()
3627 gold_assert(this->requires_postprocessing());
3629 if (this->postprocessing_buffer_ != NULL)
3630 return;
3632 if (!this->input_sections_.empty())
3634 off_t off = this->first_input_offset_;
3635 for (Input_section_list::iterator p = this->input_sections_.begin();
3636 p != this->input_sections_.end();
3637 ++p)
3639 off = align_address(off, p->addralign());
3640 p->finalize_data_size();
3641 off += p->data_size();
3643 this->set_current_data_size_for_child(off);
3646 off_t buffer_size = this->current_data_size_for_child();
3647 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3650 // Write all the data of an Output_section into the postprocessing
3651 // buffer. This is used for sections which require postprocessing,
3652 // such as compression. Input sections are handled by
3653 // Object::Relocate.
3655 void
3656 Output_section::write_to_postprocessing_buffer()
3658 gold_assert(this->requires_postprocessing());
3660 // If the target performs relaxation, we delay filler generation until now.
3661 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3663 unsigned char* buffer = this->postprocessing_buffer();
3664 for (Fill_list::iterator p = this->fills_.begin();
3665 p != this->fills_.end();
3666 ++p)
3668 std::string fill_data(parameters->target().code_fill(p->length()));
3669 memcpy(buffer + p->section_offset(), fill_data.data(),
3670 fill_data.size());
3673 off_t off = this->first_input_offset_;
3674 for (Input_section_list::iterator p = this->input_sections_.begin();
3675 p != this->input_sections_.end();
3676 ++p)
3678 off_t aligned_off = align_address(off, p->addralign());
3679 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3681 size_t fill_len = aligned_off - off;
3682 std::string fill_data(parameters->target().code_fill(fill_len));
3683 memcpy(buffer + off, fill_data.data(), fill_data.size());
3686 p->write_to_buffer(buffer + aligned_off);
3687 off = aligned_off + p->data_size();
3691 // Get the input sections for linker script processing. We leave
3692 // behind the Output_section_data entries. Note that this may be
3693 // slightly incorrect for merge sections. We will leave them behind,
3694 // but it is possible that the script says that they should follow
3695 // some other input sections, as in:
3696 // .rodata { *(.rodata) *(.rodata.cst*) }
3697 // For that matter, we don't handle this correctly:
3698 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3699 // With luck this will never matter.
3701 uint64_t
3702 Output_section::get_input_sections(
3703 uint64_t address,
3704 const std::string& fill,
3705 std::list<Input_section>* input_sections)
3707 if (this->checkpoint_ != NULL
3708 && !this->checkpoint_->input_sections_saved())
3709 this->checkpoint_->save_input_sections();
3711 // Invalidate fast look-up maps.
3712 this->lookup_maps_->invalidate();
3714 uint64_t orig_address = address;
3716 address = align_address(address, this->addralign());
3718 Input_section_list remaining;
3719 for (Input_section_list::iterator p = this->input_sections_.begin();
3720 p != this->input_sections_.end();
3721 ++p)
3723 if (p->is_input_section()
3724 || p->is_relaxed_input_section()
3725 || p->is_merge_section())
3726 input_sections->push_back(*p);
3727 else
3729 uint64_t aligned_address = align_address(address, p->addralign());
3730 if (aligned_address != address && !fill.empty())
3732 section_size_type length =
3733 convert_to_section_size_type(aligned_address - address);
3734 std::string this_fill;
3735 this_fill.reserve(length);
3736 while (this_fill.length() + fill.length() <= length)
3737 this_fill += fill;
3738 if (this_fill.length() < length)
3739 this_fill.append(fill, 0, length - this_fill.length());
3741 Output_section_data* posd = new Output_data_const(this_fill, 0);
3742 remaining.push_back(Input_section(posd));
3744 address = aligned_address;
3746 remaining.push_back(*p);
3748 p->finalize_data_size();
3749 address += p->data_size();
3753 this->input_sections_.swap(remaining);
3754 this->first_input_offset_ = 0;
3756 uint64_t data_size = address - orig_address;
3757 this->set_current_data_size_for_child(data_size);
3758 return data_size;
3761 // Add a script input section. SIS is an Output_section::Input_section,
3762 // which can be either a plain input section or a special input section like
3763 // a relaxed input section. For a special input section, its size must be
3764 // finalized.
3766 void
3767 Output_section::add_script_input_section(const Input_section& sis)
3769 uint64_t data_size = sis.data_size();
3770 uint64_t addralign = sis.addralign();
3771 if (addralign > this->addralign_)
3772 this->addralign_ = addralign;
3774 off_t offset_in_section = this->current_data_size_for_child();
3775 off_t aligned_offset_in_section = align_address(offset_in_section,
3776 addralign);
3778 this->set_current_data_size_for_child(aligned_offset_in_section
3779 + data_size);
3781 this->input_sections_.push_back(sis);
3783 // Update fast lookup maps if necessary.
3784 if (this->lookup_maps_->is_valid())
3786 if (sis.is_merge_section())
3788 Output_merge_base* pomb = sis.output_merge_base();
3789 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3790 pomb->addralign());
3791 this->lookup_maps_->add_merge_section(msp, pomb);
3792 for (Output_merge_base::Input_sections::const_iterator p =
3793 pomb->input_sections_begin();
3794 p != pomb->input_sections_end();
3795 ++p)
3796 this->lookup_maps_->add_merge_input_section(p->first, p->second,
3797 pomb);
3799 else if (sis.is_relaxed_input_section())
3801 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3802 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3803 poris->shndx(), poris);
3808 // Save states for relaxation.
3810 void
3811 Output_section::save_states()
3813 gold_assert(this->checkpoint_ == NULL);
3814 Checkpoint_output_section* checkpoint =
3815 new Checkpoint_output_section(this->addralign_, this->flags_,
3816 this->input_sections_,
3817 this->first_input_offset_,
3818 this->attached_input_sections_are_sorted_);
3819 this->checkpoint_ = checkpoint;
3820 gold_assert(this->fills_.empty());
3823 void
3824 Output_section::discard_states()
3826 gold_assert(this->checkpoint_ != NULL);
3827 delete this->checkpoint_;
3828 this->checkpoint_ = NULL;
3829 gold_assert(this->fills_.empty());
3831 // Simply invalidate the fast lookup maps since we do not keep
3832 // track of them.
3833 this->lookup_maps_->invalidate();
3836 void
3837 Output_section::restore_states()
3839 gold_assert(this->checkpoint_ != NULL);
3840 Checkpoint_output_section* checkpoint = this->checkpoint_;
3842 this->addralign_ = checkpoint->addralign();
3843 this->flags_ = checkpoint->flags();
3844 this->first_input_offset_ = checkpoint->first_input_offset();
3846 if (!checkpoint->input_sections_saved())
3848 // If we have not copied the input sections, just resize it.
3849 size_t old_size = checkpoint->input_sections_size();
3850 gold_assert(this->input_sections_.size() >= old_size);
3851 this->input_sections_.resize(old_size);
3853 else
3855 // We need to copy the whole list. This is not efficient for
3856 // extremely large output with hundreads of thousands of input
3857 // objects. We may need to re-think how we should pass sections
3858 // to scripts.
3859 this->input_sections_ = *checkpoint->input_sections();
3862 this->attached_input_sections_are_sorted_ =
3863 checkpoint->attached_input_sections_are_sorted();
3865 // Simply invalidate the fast lookup maps since we do not keep
3866 // track of them.
3867 this->lookup_maps_->invalidate();
3870 // Update the section offsets of input sections in this. This is required if
3871 // relaxation causes some input sections to change sizes.
3873 void
3874 Output_section::adjust_section_offsets()
3876 if (!this->section_offsets_need_adjustment_)
3877 return;
3879 off_t off = 0;
3880 for (Input_section_list::iterator p = this->input_sections_.begin();
3881 p != this->input_sections_.end();
3882 ++p)
3884 off = align_address(off, p->addralign());
3885 if (p->is_input_section())
3886 p->relobj()->set_section_offset(p->shndx(), off);
3887 off += p->data_size();
3890 this->section_offsets_need_adjustment_ = false;
3893 // Print to the map file.
3895 void
3896 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3898 mapfile->print_output_section(this);
3900 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3901 p != this->input_sections_.end();
3902 ++p)
3903 p->print_to_mapfile(mapfile);
3906 // Print stats for merge sections to stderr.
3908 void
3909 Output_section::print_merge_stats()
3911 Input_section_list::iterator p;
3912 for (p = this->input_sections_.begin();
3913 p != this->input_sections_.end();
3914 ++p)
3915 p->print_merge_stats(this->name_);
3918 // Set a fixed layout for the section. Used for incremental update links.
3920 void
3921 Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3922 off_t sh_size, uint64_t sh_addralign)
3924 this->addralign_ = sh_addralign;
3925 this->set_current_data_size(sh_size);
3926 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
3927 this->set_address(sh_addr);
3928 this->set_file_offset(sh_offset);
3929 this->finalize_data_size();
3930 this->free_list_.init(sh_size, false);
3931 this->has_fixed_layout_ = true;
3934 // Reserve space within the fixed layout for the section. Used for
3935 // incremental update links.
3937 void
3938 Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
3940 this->free_list_.remove(sh_offset, sh_offset + sh_size);
3943 // Allocate space from the free list for the section. Used for
3944 // incremental update links.
3946 off_t
3947 Output_section::allocate(off_t len, uint64_t addralign)
3949 return this->free_list_.allocate(len, addralign, 0);
3952 // Output segment methods.
3954 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3955 : vaddr_(0),
3956 paddr_(0),
3957 memsz_(0),
3958 max_align_(0),
3959 min_p_align_(0),
3960 offset_(0),
3961 filesz_(0),
3962 type_(type),
3963 flags_(flags),
3964 is_max_align_known_(false),
3965 are_addresses_set_(false),
3966 is_large_data_segment_(false)
3968 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3969 // the flags.
3970 if (type == elfcpp::PT_TLS)
3971 this->flags_ = elfcpp::PF_R;
3974 // Add an Output_section to a PT_LOAD Output_segment.
3976 void
3977 Output_segment::add_output_section_to_load(Layout* layout,
3978 Output_section* os,
3979 elfcpp::Elf_Word seg_flags)
3981 gold_assert(this->type() == elfcpp::PT_LOAD);
3982 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3983 gold_assert(!this->is_max_align_known_);
3984 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3986 this->update_flags_for_output_section(seg_flags);
3988 // We don't want to change the ordering if we have a linker script
3989 // with a SECTIONS clause.
3990 Output_section_order order = os->order();
3991 if (layout->script_options()->saw_sections_clause())
3992 order = static_cast<Output_section_order>(0);
3993 else
3994 gold_assert(order != ORDER_INVALID);
3996 this->output_lists_[order].push_back(os);
3999 // Add an Output_section to a non-PT_LOAD Output_segment.
4001 void
4002 Output_segment::add_output_section_to_nonload(Output_section* os,
4003 elfcpp::Elf_Word seg_flags)
4005 gold_assert(this->type() != elfcpp::PT_LOAD);
4006 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4007 gold_assert(!this->is_max_align_known_);
4009 this->update_flags_for_output_section(seg_flags);
4011 this->output_lists_[0].push_back(os);
4014 // Remove an Output_section from this segment. It is an error if it
4015 // is not present.
4017 void
4018 Output_segment::remove_output_section(Output_section* os)
4020 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4022 Output_data_list* pdl = &this->output_lists_[i];
4023 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
4025 if (*p == os)
4027 pdl->erase(p);
4028 return;
4032 gold_unreachable();
4035 // Add an Output_data (which need not be an Output_section) to the
4036 // start of a segment.
4038 void
4039 Output_segment::add_initial_output_data(Output_data* od)
4041 gold_assert(!this->is_max_align_known_);
4042 Output_data_list::iterator p = this->output_lists_[0].begin();
4043 this->output_lists_[0].insert(p, od);
4046 // Return true if this segment has any sections which hold actual
4047 // data, rather than being a BSS section.
4049 bool
4050 Output_segment::has_any_data_sections() const
4052 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4054 const Output_data_list* pdl = &this->output_lists_[i];
4055 for (Output_data_list::const_iterator p = pdl->begin();
4056 p != pdl->end();
4057 ++p)
4059 if (!(*p)->is_section())
4060 return true;
4061 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
4062 return true;
4065 return false;
4068 // Return whether the first data section (not counting TLS sections)
4069 // is a relro section.
4071 bool
4072 Output_segment::is_first_section_relro() const
4074 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4076 if (i == static_cast<int>(ORDER_TLS_DATA)
4077 || i == static_cast<int>(ORDER_TLS_BSS))
4078 continue;
4079 const Output_data_list* pdl = &this->output_lists_[i];
4080 if (!pdl->empty())
4082 Output_data* p = pdl->front();
4083 return p->is_section() && p->output_section()->is_relro();
4086 return false;
4089 // Return the maximum alignment of the Output_data in Output_segment.
4091 uint64_t
4092 Output_segment::maximum_alignment()
4094 if (!this->is_max_align_known_)
4096 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4098 const Output_data_list* pdl = &this->output_lists_[i];
4099 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
4100 if (addralign > this->max_align_)
4101 this->max_align_ = addralign;
4103 this->is_max_align_known_ = true;
4106 return this->max_align_;
4109 // Return the maximum alignment of a list of Output_data.
4111 uint64_t
4112 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
4114 uint64_t ret = 0;
4115 for (Output_data_list::const_iterator p = pdl->begin();
4116 p != pdl->end();
4117 ++p)
4119 uint64_t addralign = (*p)->addralign();
4120 if (addralign > ret)
4121 ret = addralign;
4123 return ret;
4126 // Return whether this segment has any dynamic relocs.
4128 bool
4129 Output_segment::has_dynamic_reloc() const
4131 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4132 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
4133 return true;
4134 return false;
4137 // Return whether this Output_data_list has any dynamic relocs.
4139 bool
4140 Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
4142 for (Output_data_list::const_iterator p = pdl->begin();
4143 p != pdl->end();
4144 ++p)
4145 if ((*p)->has_dynamic_reloc())
4146 return true;
4147 return false;
4150 // Set the section addresses for an Output_segment. If RESET is true,
4151 // reset the addresses first. ADDR is the address and *POFF is the
4152 // file offset. Set the section indexes starting with *PSHNDX.
4153 // INCREASE_RELRO is the size of the portion of the first non-relro
4154 // section that should be included in the PT_GNU_RELRO segment.
4155 // If this segment has relro sections, and has been aligned for
4156 // that purpose, set *HAS_RELRO to TRUE. Return the address of
4157 // the immediately following segment. Update *HAS_RELRO, *POFF,
4158 // and *PSHNDX.
4160 uint64_t
4161 Output_segment::set_section_addresses(Layout* layout, bool reset,
4162 uint64_t addr,
4163 unsigned int* increase_relro,
4164 bool* has_relro,
4165 off_t* poff,
4166 unsigned int* pshndx)
4168 gold_assert(this->type_ == elfcpp::PT_LOAD);
4170 uint64_t last_relro_pad = 0;
4171 off_t orig_off = *poff;
4173 bool in_tls = false;
4175 // If we have relro sections, we need to pad forward now so that the
4176 // relro sections plus INCREASE_RELRO end on a common page boundary.
4177 if (parameters->options().relro()
4178 && this->is_first_section_relro()
4179 && (!this->are_addresses_set_ || reset))
4181 uint64_t relro_size = 0;
4182 off_t off = *poff;
4183 uint64_t max_align = 0;
4184 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
4186 Output_data_list* pdl = &this->output_lists_[i];
4187 Output_data_list::iterator p;
4188 for (p = pdl->begin(); p != pdl->end(); ++p)
4190 if (!(*p)->is_section())
4191 break;
4192 uint64_t align = (*p)->addralign();
4193 if (align > max_align)
4194 max_align = align;
4195 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4196 in_tls = true;
4197 else if (in_tls)
4199 // Align the first non-TLS section to the alignment
4200 // of the TLS segment.
4201 align = max_align;
4202 in_tls = false;
4204 relro_size = align_address(relro_size, align);
4205 // Ignore the size of the .tbss section.
4206 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
4207 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
4208 continue;
4209 if ((*p)->is_address_valid())
4210 relro_size += (*p)->data_size();
4211 else
4213 // FIXME: This could be faster.
4214 (*p)->set_address_and_file_offset(addr + relro_size,
4215 off + relro_size);
4216 relro_size += (*p)->data_size();
4217 (*p)->reset_address_and_file_offset();
4220 if (p != pdl->end())
4221 break;
4223 relro_size += *increase_relro;
4224 // Pad the total relro size to a multiple of the maximum
4225 // section alignment seen.
4226 uint64_t aligned_size = align_address(relro_size, max_align);
4227 // Note the amount of padding added after the last relro section.
4228 last_relro_pad = aligned_size - relro_size;
4229 *has_relro = true;
4231 uint64_t page_align = parameters->target().common_pagesize();
4233 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4234 uint64_t desired_align = page_align - (aligned_size % page_align);
4235 if (desired_align < *poff % page_align)
4236 *poff += page_align - *poff % page_align;
4237 *poff += desired_align - *poff % page_align;
4238 addr += *poff - orig_off;
4239 orig_off = *poff;
4242 if (!reset && this->are_addresses_set_)
4244 gold_assert(this->paddr_ == addr);
4245 addr = this->vaddr_;
4247 else
4249 this->vaddr_ = addr;
4250 this->paddr_ = addr;
4251 this->are_addresses_set_ = true;
4254 in_tls = false;
4256 this->offset_ = orig_off;
4258 off_t off = 0;
4259 uint64_t ret;
4260 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4262 if (i == static_cast<int>(ORDER_RELRO_LAST))
4264 *poff += last_relro_pad;
4265 addr += last_relro_pad;
4266 if (this->output_lists_[i].empty())
4268 // If there is nothing in the ORDER_RELRO_LAST list,
4269 // the padding will occur at the end of the relro
4270 // segment, and we need to add it to *INCREASE_RELRO.
4271 *increase_relro += last_relro_pad;
4274 addr = this->set_section_list_addresses(layout, reset,
4275 &this->output_lists_[i],
4276 addr, poff, pshndx, &in_tls);
4277 if (i < static_cast<int>(ORDER_SMALL_BSS))
4279 this->filesz_ = *poff - orig_off;
4280 off = *poff;
4283 ret = addr;
4286 // If the last section was a TLS section, align upward to the
4287 // alignment of the TLS segment, so that the overall size of the TLS
4288 // segment is aligned.
4289 if (in_tls)
4291 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4292 *poff = align_address(*poff, segment_align);
4295 this->memsz_ = *poff - orig_off;
4297 // Ignore the file offset adjustments made by the BSS Output_data
4298 // objects.
4299 *poff = off;
4301 return ret;
4304 // Set the addresses and file offsets in a list of Output_data
4305 // structures.
4307 uint64_t
4308 Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4309 Output_data_list* pdl,
4310 uint64_t addr, off_t* poff,
4311 unsigned int* pshndx,
4312 bool* in_tls)
4314 off_t startoff = *poff;
4315 // For incremental updates, we may allocate non-fixed sections from
4316 // free space in the file. This keeps track of the high-water mark.
4317 off_t maxoff = startoff;
4319 off_t off = startoff;
4320 for (Output_data_list::iterator p = pdl->begin();
4321 p != pdl->end();
4322 ++p)
4324 if (reset)
4325 (*p)->reset_address_and_file_offset();
4327 // When doing an incremental update or when using a linker script,
4328 // the section will most likely already have an address.
4329 if (!(*p)->is_address_valid())
4331 uint64_t align = (*p)->addralign();
4333 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4335 // Give the first TLS section the alignment of the
4336 // entire TLS segment. Otherwise the TLS segment as a
4337 // whole may be misaligned.
4338 if (!*in_tls)
4340 Output_segment* tls_segment = layout->tls_segment();
4341 gold_assert(tls_segment != NULL);
4342 uint64_t segment_align = tls_segment->maximum_alignment();
4343 gold_assert(segment_align >= align);
4344 align = segment_align;
4346 *in_tls = true;
4349 else
4351 // If this is the first section after the TLS segment,
4352 // align it to at least the alignment of the TLS
4353 // segment, so that the size of the overall TLS segment
4354 // is aligned.
4355 if (*in_tls)
4357 uint64_t segment_align =
4358 layout->tls_segment()->maximum_alignment();
4359 if (segment_align > align)
4360 align = segment_align;
4362 *in_tls = false;
4366 if (!parameters->incremental_update())
4368 off = align_address(off, align);
4369 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4371 else
4373 // Incremental update: allocate file space from free list.
4374 (*p)->pre_finalize_data_size();
4375 off_t current_size = (*p)->current_data_size();
4376 off = layout->allocate(current_size, align, startoff);
4377 if (off == -1)
4379 gold_assert((*p)->output_section() != NULL);
4380 gold_fallback(_("out of patch space for section %s; "
4381 "relink with --incremental-full"),
4382 (*p)->output_section()->name());
4384 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4385 if ((*p)->data_size() > current_size)
4387 gold_assert((*p)->output_section() != NULL);
4388 gold_fallback(_("%s: section changed size; "
4389 "relink with --incremental-full"),
4390 (*p)->output_section()->name());
4394 else if (parameters->incremental_update())
4396 // For incremental updates, use the fixed offset for the
4397 // high-water mark computation.
4398 off = (*p)->offset();
4400 else
4402 // The script may have inserted a skip forward, but it
4403 // better not have moved backward.
4404 if ((*p)->address() >= addr + (off - startoff))
4405 off += (*p)->address() - (addr + (off - startoff));
4406 else
4408 if (!layout->script_options()->saw_sections_clause())
4409 gold_unreachable();
4410 else
4412 Output_section* os = (*p)->output_section();
4414 // Cast to unsigned long long to avoid format warnings.
4415 unsigned long long previous_dot =
4416 static_cast<unsigned long long>(addr + (off - startoff));
4417 unsigned long long dot =
4418 static_cast<unsigned long long>((*p)->address());
4420 if (os == NULL)
4421 gold_error(_("dot moves backward in linker script "
4422 "from 0x%llx to 0x%llx"), previous_dot, dot);
4423 else
4424 gold_error(_("address of section '%s' moves backward "
4425 "from 0x%llx to 0x%llx"),
4426 os->name(), previous_dot, dot);
4429 (*p)->set_file_offset(off);
4430 (*p)->finalize_data_size();
4433 if (parameters->incremental_update())
4434 gold_debug(DEBUG_INCREMENTAL,
4435 "set_section_list_addresses: %08lx %08lx %s",
4436 static_cast<long>(off),
4437 static_cast<long>((*p)->data_size()),
4438 ((*p)->output_section() != NULL
4439 ? (*p)->output_section()->name() : "(special)"));
4441 // We want to ignore the size of a SHF_TLS SHT_NOBITS
4442 // section. Such a section does not affect the size of a
4443 // PT_LOAD segment.
4444 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
4445 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
4446 off += (*p)->data_size();
4448 if (off > maxoff)
4449 maxoff = off;
4451 if ((*p)->is_section())
4453 (*p)->set_out_shndx(*pshndx);
4454 ++*pshndx;
4458 *poff = maxoff;
4459 return addr + (maxoff - startoff);
4462 // For a non-PT_LOAD segment, set the offset from the sections, if
4463 // any. Add INCREASE to the file size and the memory size.
4465 void
4466 Output_segment::set_offset(unsigned int increase)
4468 gold_assert(this->type_ != elfcpp::PT_LOAD);
4470 gold_assert(!this->are_addresses_set_);
4472 // A non-load section only uses output_lists_[0].
4474 Output_data_list* pdl = &this->output_lists_[0];
4476 if (pdl->empty())
4478 gold_assert(increase == 0);
4479 this->vaddr_ = 0;
4480 this->paddr_ = 0;
4481 this->are_addresses_set_ = true;
4482 this->memsz_ = 0;
4483 this->min_p_align_ = 0;
4484 this->offset_ = 0;
4485 this->filesz_ = 0;
4486 return;
4489 // Find the first and last section by address.
4490 const Output_data* first = NULL;
4491 const Output_data* last_data = NULL;
4492 const Output_data* last_bss = NULL;
4493 for (Output_data_list::const_iterator p = pdl->begin();
4494 p != pdl->end();
4495 ++p)
4497 if (first == NULL
4498 || (*p)->address() < first->address()
4499 || ((*p)->address() == first->address()
4500 && (*p)->data_size() < first->data_size()))
4501 first = *p;
4502 const Output_data** plast;
4503 if ((*p)->is_section()
4504 && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4505 plast = &last_bss;
4506 else
4507 plast = &last_data;
4508 if (*plast == NULL
4509 || (*p)->address() > (*plast)->address()
4510 || ((*p)->address() == (*plast)->address()
4511 && (*p)->data_size() > (*plast)->data_size()))
4512 *plast = *p;
4515 this->vaddr_ = first->address();
4516 this->paddr_ = (first->has_load_address()
4517 ? first->load_address()
4518 : this->vaddr_);
4519 this->are_addresses_set_ = true;
4520 this->offset_ = first->offset();
4522 if (last_data == NULL)
4523 this->filesz_ = 0;
4524 else
4525 this->filesz_ = (last_data->address()
4526 + last_data->data_size()
4527 - this->vaddr_);
4529 const Output_data* last = last_bss != NULL ? last_bss : last_data;
4530 this->memsz_ = (last->address()
4531 + last->data_size()
4532 - this->vaddr_);
4534 this->filesz_ += increase;
4535 this->memsz_ += increase;
4537 // If this is a RELRO segment, verify that the segment ends at a
4538 // page boundary.
4539 if (this->type_ == elfcpp::PT_GNU_RELRO)
4541 uint64_t page_align = parameters->target().common_pagesize();
4542 uint64_t segment_end = this->vaddr_ + this->memsz_;
4543 if (parameters->incremental_update())
4545 // The INCREASE_RELRO calculation is bypassed for an incremental
4546 // update, so we need to adjust the segment size manually here.
4547 segment_end = align_address(segment_end, page_align);
4548 this->memsz_ = segment_end - this->vaddr_;
4550 else
4551 gold_assert(segment_end == align_address(segment_end, page_align));
4554 // If this is a TLS segment, align the memory size. The code in
4555 // set_section_list ensures that the section after the TLS segment
4556 // is aligned to give us room.
4557 if (this->type_ == elfcpp::PT_TLS)
4559 uint64_t segment_align = this->maximum_alignment();
4560 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4561 this->memsz_ = align_address(this->memsz_, segment_align);
4565 // Set the TLS offsets of the sections in the PT_TLS segment.
4567 void
4568 Output_segment::set_tls_offsets()
4570 gold_assert(this->type_ == elfcpp::PT_TLS);
4572 for (Output_data_list::iterator p = this->output_lists_[0].begin();
4573 p != this->output_lists_[0].end();
4574 ++p)
4575 (*p)->set_tls_offset(this->vaddr_);
4578 // Return the load address of the first section.
4580 uint64_t
4581 Output_segment::first_section_load_address() const
4583 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4585 const Output_data_list* pdl = &this->output_lists_[i];
4586 for (Output_data_list::const_iterator p = pdl->begin();
4587 p != pdl->end();
4588 ++p)
4590 if ((*p)->is_section())
4591 return ((*p)->has_load_address()
4592 ? (*p)->load_address()
4593 : (*p)->address());
4596 gold_unreachable();
4599 // Return the number of Output_sections in an Output_segment.
4601 unsigned int
4602 Output_segment::output_section_count() const
4604 unsigned int ret = 0;
4605 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4606 ret += this->output_section_count_list(&this->output_lists_[i]);
4607 return ret;
4610 // Return the number of Output_sections in an Output_data_list.
4612 unsigned int
4613 Output_segment::output_section_count_list(const Output_data_list* pdl) const
4615 unsigned int count = 0;
4616 for (Output_data_list::const_iterator p = pdl->begin();
4617 p != pdl->end();
4618 ++p)
4620 if ((*p)->is_section())
4621 ++count;
4623 return count;
4626 // Return the section attached to the list segment with the lowest
4627 // load address. This is used when handling a PHDRS clause in a
4628 // linker script.
4630 Output_section*
4631 Output_segment::section_with_lowest_load_address() const
4633 Output_section* found = NULL;
4634 uint64_t found_lma = 0;
4635 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4636 this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4637 &found_lma);
4638 return found;
4641 // Look through a list for a section with a lower load address.
4643 void
4644 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4645 Output_section** found,
4646 uint64_t* found_lma) const
4648 for (Output_data_list::const_iterator p = pdl->begin();
4649 p != pdl->end();
4650 ++p)
4652 if (!(*p)->is_section())
4653 continue;
4654 Output_section* os = static_cast<Output_section*>(*p);
4655 uint64_t lma = (os->has_load_address()
4656 ? os->load_address()
4657 : os->address());
4658 if (*found == NULL || lma < *found_lma)
4660 *found = os;
4661 *found_lma = lma;
4666 // Write the segment data into *OPHDR.
4668 template<int size, bool big_endian>
4669 void
4670 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4672 ophdr->put_p_type(this->type_);
4673 ophdr->put_p_offset(this->offset_);
4674 ophdr->put_p_vaddr(this->vaddr_);
4675 ophdr->put_p_paddr(this->paddr_);
4676 ophdr->put_p_filesz(this->filesz_);
4677 ophdr->put_p_memsz(this->memsz_);
4678 ophdr->put_p_flags(this->flags_);
4679 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4682 // Write the section headers into V.
4684 template<int size, bool big_endian>
4685 unsigned char*
4686 Output_segment::write_section_headers(const Layout* layout,
4687 const Stringpool* secnamepool,
4688 unsigned char* v,
4689 unsigned int* pshndx) const
4691 // Every section that is attached to a segment must be attached to a
4692 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4693 // segments.
4694 if (this->type_ != elfcpp::PT_LOAD)
4695 return v;
4697 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4699 const Output_data_list* pdl = &this->output_lists_[i];
4700 v = this->write_section_headers_list<size, big_endian>(layout,
4701 secnamepool,
4702 pdl,
4703 v, pshndx);
4706 return v;
4709 template<int size, bool big_endian>
4710 unsigned char*
4711 Output_segment::write_section_headers_list(const Layout* layout,
4712 const Stringpool* secnamepool,
4713 const Output_data_list* pdl,
4714 unsigned char* v,
4715 unsigned int* pshndx) const
4717 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4718 for (Output_data_list::const_iterator p = pdl->begin();
4719 p != pdl->end();
4720 ++p)
4722 if ((*p)->is_section())
4724 const Output_section* ps = static_cast<const Output_section*>(*p);
4725 gold_assert(*pshndx == ps->out_shndx());
4726 elfcpp::Shdr_write<size, big_endian> oshdr(v);
4727 ps->write_header(layout, secnamepool, &oshdr);
4728 v += shdr_size;
4729 ++*pshndx;
4732 return v;
4735 // Print the output sections to the map file.
4737 void
4738 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4740 if (this->type() != elfcpp::PT_LOAD)
4741 return;
4742 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4743 this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4746 // Print an output section list to the map file.
4748 void
4749 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4750 const Output_data_list* pdl) const
4752 for (Output_data_list::const_iterator p = pdl->begin();
4753 p != pdl->end();
4754 ++p)
4755 (*p)->print_to_mapfile(mapfile);
4758 // Output_file methods.
4760 Output_file::Output_file(const char* name)
4761 : name_(name),
4762 o_(-1),
4763 file_size_(0),
4764 base_(NULL),
4765 map_is_anonymous_(false),
4766 map_is_allocated_(false),
4767 is_temporary_(false)
4771 // Try to open an existing file. Returns false if the file doesn't
4772 // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
4773 // NULL, open that file as the base for incremental linking, and
4774 // copy its contents to the new output file. This routine can
4775 // be called for incremental updates, in which case WRITABLE should
4776 // be true, or by the incremental-dump utility, in which case
4777 // WRITABLE should be false.
4779 bool
4780 Output_file::open_base_file(const char* base_name, bool writable)
4782 // The name "-" means "stdout".
4783 if (strcmp(this->name_, "-") == 0)
4784 return false;
4786 bool use_base_file = base_name != NULL;
4787 if (!use_base_file)
4788 base_name = this->name_;
4789 else if (strcmp(base_name, this->name_) == 0)
4790 gold_fatal(_("%s: incremental base and output file name are the same"),
4791 base_name);
4793 // Don't bother opening files with a size of zero.
4794 struct stat s;
4795 if (::stat(base_name, &s) != 0)
4797 gold_info(_("%s: stat: %s"), base_name, strerror(errno));
4798 return false;
4800 if (s.st_size == 0)
4802 gold_info(_("%s: incremental base file is empty"), base_name);
4803 return false;
4806 // If we're using a base file, we want to open it read-only.
4807 if (use_base_file)
4808 writable = false;
4810 int oflags = writable ? O_RDWR : O_RDONLY;
4811 int o = open_descriptor(-1, base_name, oflags, 0);
4812 if (o < 0)
4814 gold_info(_("%s: open: %s"), base_name, strerror(errno));
4815 return false;
4818 // If the base file and the output file are different, open a
4819 // new output file and read the contents from the base file into
4820 // the newly-mapped region.
4821 if (use_base_file)
4823 this->open(s.st_size);
4824 ssize_t bytes_to_read = s.st_size;
4825 unsigned char* p = this->base_;
4826 while (bytes_to_read > 0)
4828 ssize_t len = ::read(o, p, bytes_to_read);
4829 if (len < 0)
4831 gold_info(_("%s: read failed: %s"), base_name, strerror(errno));
4832 return false;
4834 if (len == 0)
4836 gold_info(_("%s: file too short: read only %lld of %lld bytes"),
4837 base_name,
4838 static_cast<long long>(s.st_size - bytes_to_read),
4839 static_cast<long long>(s.st_size));
4840 return false;
4842 p += len;
4843 bytes_to_read -= len;
4845 ::close(o);
4846 return true;
4849 this->o_ = o;
4850 this->file_size_ = s.st_size;
4852 if (!this->map_no_anonymous(writable))
4854 release_descriptor(o, true);
4855 this->o_ = -1;
4856 this->file_size_ = 0;
4857 return false;
4860 return true;
4863 // Open the output file.
4865 void
4866 Output_file::open(off_t file_size)
4868 this->file_size_ = file_size;
4870 // Unlink the file first; otherwise the open() may fail if the file
4871 // is busy (e.g. it's an executable that's currently being executed).
4873 // However, the linker may be part of a system where a zero-length
4874 // file is created for it to write to, with tight permissions (gcc
4875 // 2.95 did something like this). Unlinking the file would work
4876 // around those permission controls, so we only unlink if the file
4877 // has a non-zero size. We also unlink only regular files to avoid
4878 // trouble with directories/etc.
4880 // If we fail, continue; this command is merely a best-effort attempt
4881 // to improve the odds for open().
4883 // We let the name "-" mean "stdout"
4884 if (!this->is_temporary_)
4886 if (strcmp(this->name_, "-") == 0)
4887 this->o_ = STDOUT_FILENO;
4888 else
4890 struct stat s;
4891 if (::stat(this->name_, &s) == 0
4892 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4894 if (s.st_size != 0)
4895 ::unlink(this->name_);
4896 else if (!parameters->options().relocatable())
4898 // If we don't unlink the existing file, add execute
4899 // permission where read permissions already exist
4900 // and where the umask permits.
4901 int mask = ::umask(0);
4902 ::umask(mask);
4903 s.st_mode |= (s.st_mode & 0444) >> 2;
4904 ::chmod(this->name_, s.st_mode & ~mask);
4908 int mode = parameters->options().relocatable() ? 0666 : 0777;
4909 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4910 mode);
4911 if (o < 0)
4912 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4913 this->o_ = o;
4917 this->map();
4920 // Resize the output file.
4922 void
4923 Output_file::resize(off_t file_size)
4925 // If the mmap is mapping an anonymous memory buffer, this is easy:
4926 // just mremap to the new size. If it's mapping to a file, we want
4927 // to unmap to flush to the file, then remap after growing the file.
4928 if (this->map_is_anonymous_)
4930 void* base;
4931 if (!this->map_is_allocated_)
4933 base = ::mremap(this->base_, this->file_size_, file_size,
4934 MREMAP_MAYMOVE);
4935 if (base == MAP_FAILED)
4936 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
4938 else
4940 base = realloc(this->base_, file_size);
4941 if (base == NULL)
4942 gold_nomem();
4943 if (file_size > this->file_size_)
4944 memset(static_cast<char*>(base) + this->file_size_, 0,
4945 file_size - this->file_size_);
4947 this->base_ = static_cast<unsigned char*>(base);
4948 this->file_size_ = file_size;
4950 else
4952 this->unmap();
4953 this->file_size_ = file_size;
4954 if (!this->map_no_anonymous(true))
4955 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4959 // Map an anonymous block of memory which will later be written to the
4960 // file. Return whether the map succeeded.
4962 bool
4963 Output_file::map_anonymous()
4965 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4966 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4967 if (base == MAP_FAILED)
4969 base = malloc(this->file_size_);
4970 if (base == NULL)
4971 return false;
4972 memset(base, 0, this->file_size_);
4973 this->map_is_allocated_ = true;
4975 this->base_ = static_cast<unsigned char*>(base);
4976 this->map_is_anonymous_ = true;
4977 return true;
4980 // Map the file into memory. Return whether the mapping succeeded.
4981 // If WRITABLE is true, map with write access.
4983 bool
4984 Output_file::map_no_anonymous(bool writable)
4986 const int o = this->o_;
4988 // If the output file is not a regular file, don't try to mmap it;
4989 // instead, we'll mmap a block of memory (an anonymous buffer), and
4990 // then later write the buffer to the file.
4991 void* base;
4992 struct stat statbuf;
4993 if (o == STDOUT_FILENO || o == STDERR_FILENO
4994 || ::fstat(o, &statbuf) != 0
4995 || !S_ISREG(statbuf.st_mode)
4996 || this->is_temporary_)
4997 return false;
4999 // Ensure that we have disk space available for the file. If we
5000 // don't do this, it is possible that we will call munmap, close,
5001 // and exit with dirty buffers still in the cache with no assigned
5002 // disk blocks. If the disk is out of space at that point, the
5003 // output file will wind up incomplete, but we will have already
5004 // exited. The alternative to fallocate would be to use fdatasync,
5005 // but that would be a more significant performance hit.
5006 if (writable)
5008 int err = ::posix_fallocate(o, 0, this->file_size_);
5009 if (err != 0)
5010 gold_fatal(_("%s: %s"), this->name_, strerror(err));
5013 // Map the file into memory.
5014 int prot = PROT_READ;
5015 if (writable)
5016 prot |= PROT_WRITE;
5017 base = ::mmap(NULL, this->file_size_, prot, MAP_SHARED, o, 0);
5019 // The mmap call might fail because of file system issues: the file
5020 // system might not support mmap at all, or it might not support
5021 // mmap with PROT_WRITE.
5022 if (base == MAP_FAILED)
5023 return false;
5025 this->map_is_anonymous_ = false;
5026 this->base_ = static_cast<unsigned char*>(base);
5027 return true;
5030 // Map the file into memory.
5032 void
5033 Output_file::map()
5035 if (this->map_no_anonymous(true))
5036 return;
5038 // The mmap call might fail because of file system issues: the file
5039 // system might not support mmap at all, or it might not support
5040 // mmap with PROT_WRITE. I'm not sure which errno values we will
5041 // see in all cases, so if the mmap fails for any reason and we
5042 // don't care about file contents, try for an anonymous map.
5043 if (this->map_anonymous())
5044 return;
5046 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
5047 this->name_, static_cast<unsigned long>(this->file_size_),
5048 strerror(errno));
5051 // Unmap the file from memory.
5053 void
5054 Output_file::unmap()
5056 if (this->map_is_anonymous_)
5058 // We've already written out the data, so there is no reason to
5059 // waste time unmapping or freeing the memory.
5061 else
5063 if (::munmap(this->base_, this->file_size_) < 0)
5064 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
5066 this->base_ = NULL;
5069 // Close the output file.
5071 void
5072 Output_file::close()
5074 // If the map isn't file-backed, we need to write it now.
5075 if (this->map_is_anonymous_ && !this->is_temporary_)
5077 size_t bytes_to_write = this->file_size_;
5078 size_t offset = 0;
5079 while (bytes_to_write > 0)
5081 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
5082 bytes_to_write);
5083 if (bytes_written == 0)
5084 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
5085 else if (bytes_written < 0)
5086 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
5087 else
5089 bytes_to_write -= bytes_written;
5090 offset += bytes_written;
5094 this->unmap();
5096 // We don't close stdout or stderr
5097 if (this->o_ != STDOUT_FILENO
5098 && this->o_ != STDERR_FILENO
5099 && !this->is_temporary_)
5100 if (::close(this->o_) < 0)
5101 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
5102 this->o_ = -1;
5105 // Instantiate the templates we need. We could use the configure
5106 // script to restrict this to only the ones for implemented targets.
5108 #ifdef HAVE_TARGET_32_LITTLE
5109 template
5110 off_t
5111 Output_section::add_input_section<32, false>(
5112 Layout* layout,
5113 Sized_relobj_file<32, false>* object,
5114 unsigned int shndx,
5115 const char* secname,
5116 const elfcpp::Shdr<32, false>& shdr,
5117 unsigned int reloc_shndx,
5118 bool have_sections_script);
5119 #endif
5121 #ifdef HAVE_TARGET_32_BIG
5122 template
5123 off_t
5124 Output_section::add_input_section<32, true>(
5125 Layout* layout,
5126 Sized_relobj_file<32, true>* object,
5127 unsigned int shndx,
5128 const char* secname,
5129 const elfcpp::Shdr<32, true>& shdr,
5130 unsigned int reloc_shndx,
5131 bool have_sections_script);
5132 #endif
5134 #ifdef HAVE_TARGET_64_LITTLE
5135 template
5136 off_t
5137 Output_section::add_input_section<64, false>(
5138 Layout* layout,
5139 Sized_relobj_file<64, false>* object,
5140 unsigned int shndx,
5141 const char* secname,
5142 const elfcpp::Shdr<64, false>& shdr,
5143 unsigned int reloc_shndx,
5144 bool have_sections_script);
5145 #endif
5147 #ifdef HAVE_TARGET_64_BIG
5148 template
5149 off_t
5150 Output_section::add_input_section<64, true>(
5151 Layout* layout,
5152 Sized_relobj_file<64, true>* object,
5153 unsigned int shndx,
5154 const char* secname,
5155 const elfcpp::Shdr<64, true>& shdr,
5156 unsigned int reloc_shndx,
5157 bool have_sections_script);
5158 #endif
5160 #ifdef HAVE_TARGET_32_LITTLE
5161 template
5162 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
5163 #endif
5165 #ifdef HAVE_TARGET_32_BIG
5166 template
5167 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
5168 #endif
5170 #ifdef HAVE_TARGET_64_LITTLE
5171 template
5172 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
5173 #endif
5175 #ifdef HAVE_TARGET_64_BIG
5176 template
5177 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
5178 #endif
5180 #ifdef HAVE_TARGET_32_LITTLE
5181 template
5182 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
5183 #endif
5185 #ifdef HAVE_TARGET_32_BIG
5186 template
5187 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
5188 #endif
5190 #ifdef HAVE_TARGET_64_LITTLE
5191 template
5192 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
5193 #endif
5195 #ifdef HAVE_TARGET_64_BIG
5196 template
5197 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
5198 #endif
5200 #ifdef HAVE_TARGET_32_LITTLE
5201 template
5202 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
5203 #endif
5205 #ifdef HAVE_TARGET_32_BIG
5206 template
5207 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
5208 #endif
5210 #ifdef HAVE_TARGET_64_LITTLE
5211 template
5212 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
5213 #endif
5215 #ifdef HAVE_TARGET_64_BIG
5216 template
5217 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
5218 #endif
5220 #ifdef HAVE_TARGET_32_LITTLE
5221 template
5222 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
5223 #endif
5225 #ifdef HAVE_TARGET_32_BIG
5226 template
5227 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
5228 #endif
5230 #ifdef HAVE_TARGET_64_LITTLE
5231 template
5232 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
5233 #endif
5235 #ifdef HAVE_TARGET_64_BIG
5236 template
5237 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
5238 #endif
5240 #ifdef HAVE_TARGET_32_LITTLE
5241 template
5242 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
5243 #endif
5245 #ifdef HAVE_TARGET_32_BIG
5246 template
5247 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
5248 #endif
5250 #ifdef HAVE_TARGET_64_LITTLE
5251 template
5252 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
5253 #endif
5255 #ifdef HAVE_TARGET_64_BIG
5256 template
5257 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
5258 #endif
5260 #ifdef HAVE_TARGET_32_LITTLE
5261 template
5262 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
5263 #endif
5265 #ifdef HAVE_TARGET_32_BIG
5266 template
5267 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
5268 #endif
5270 #ifdef HAVE_TARGET_64_LITTLE
5271 template
5272 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
5273 #endif
5275 #ifdef HAVE_TARGET_64_BIG
5276 template
5277 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
5278 #endif
5280 #ifdef HAVE_TARGET_32_LITTLE
5281 template
5282 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
5283 #endif
5285 #ifdef HAVE_TARGET_32_BIG
5286 template
5287 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
5288 #endif
5290 #ifdef HAVE_TARGET_64_LITTLE
5291 template
5292 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
5293 #endif
5295 #ifdef HAVE_TARGET_64_BIG
5296 template
5297 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
5298 #endif
5300 #ifdef HAVE_TARGET_32_LITTLE
5301 template
5302 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
5303 #endif
5305 #ifdef HAVE_TARGET_32_BIG
5306 template
5307 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
5308 #endif
5310 #ifdef HAVE_TARGET_64_LITTLE
5311 template
5312 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
5313 #endif
5315 #ifdef HAVE_TARGET_64_BIG
5316 template
5317 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
5318 #endif
5320 #ifdef HAVE_TARGET_32_LITTLE
5321 template
5322 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
5323 #endif
5325 #ifdef HAVE_TARGET_32_BIG
5326 template
5327 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
5328 #endif
5330 #ifdef HAVE_TARGET_64_LITTLE
5331 template
5332 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
5333 #endif
5335 #ifdef HAVE_TARGET_64_BIG
5336 template
5337 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
5338 #endif
5340 #ifdef HAVE_TARGET_32_LITTLE
5341 template
5342 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5343 #endif
5345 #ifdef HAVE_TARGET_32_BIG
5346 template
5347 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5348 #endif
5350 #ifdef HAVE_TARGET_64_LITTLE
5351 template
5352 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5353 #endif
5355 #ifdef HAVE_TARGET_64_BIG
5356 template
5357 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5358 #endif
5360 #ifdef HAVE_TARGET_32_LITTLE
5361 template
5362 class Output_data_group<32, false>;
5363 #endif
5365 #ifdef HAVE_TARGET_32_BIG
5366 template
5367 class Output_data_group<32, true>;
5368 #endif
5370 #ifdef HAVE_TARGET_64_LITTLE
5371 template
5372 class Output_data_group<64, false>;
5373 #endif
5375 #ifdef HAVE_TARGET_64_BIG
5376 template
5377 class Output_data_group<64, true>;
5378 #endif
5380 #ifdef HAVE_TARGET_32_LITTLE
5381 template
5382 class Output_data_got<32, false>;
5383 #endif
5385 #ifdef HAVE_TARGET_32_BIG
5386 template
5387 class Output_data_got<32, true>;
5388 #endif
5390 #ifdef HAVE_TARGET_64_LITTLE
5391 template
5392 class Output_data_got<64, false>;
5393 #endif
5395 #ifdef HAVE_TARGET_64_BIG
5396 template
5397 class Output_data_got<64, true>;
5398 #endif
5400 } // End namespace gold.