daily update
[binutils.git] / gold / output.cc
blob7633c730807353cdc507840217c6dee68d39ef24
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 Sized_relobj_file<size, big_endian>* object = this->u_.object;
1391 const unsigned int lsi = this->local_sym_index_;
1392 const Symbol_value<size>* symval = object->local_symbol(lsi);
1393 if (!this->use_plt_offset_)
1394 val = symval->value(this->u_.object, 0);
1395 else
1397 uint64_t plt_address =
1398 parameters->target().plt_address_for_local(object, lsi);
1399 val = plt_address + object->local_plt_offset(lsi);
1402 break;
1405 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1408 // Output_data_got methods.
1410 // Add an entry for a global symbol to the GOT. This returns true if
1411 // this is a new GOT entry, false if the symbol already had a GOT
1412 // entry.
1414 template<int size, bool big_endian>
1415 bool
1416 Output_data_got<size, big_endian>::add_global(
1417 Symbol* gsym,
1418 unsigned int got_type)
1420 if (gsym->has_got_offset(got_type))
1421 return false;
1423 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false));
1424 gsym->set_got_offset(got_type, got_offset);
1425 return true;
1428 // Like add_global, but use the PLT offset.
1430 template<int size, bool big_endian>
1431 bool
1432 Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1433 unsigned int got_type)
1435 if (gsym->has_got_offset(got_type))
1436 return false;
1438 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true));
1439 gsym->set_got_offset(got_type, got_offset);
1440 return true;
1443 // Add an entry for a global symbol to the GOT, and add a dynamic
1444 // relocation of type R_TYPE for the GOT entry.
1446 template<int size, bool big_endian>
1447 void
1448 Output_data_got<size, big_endian>::add_global_with_rel(
1449 Symbol* gsym,
1450 unsigned int got_type,
1451 Rel_dyn* rel_dyn,
1452 unsigned int r_type)
1454 if (gsym->has_got_offset(got_type))
1455 return;
1457 unsigned int got_offset = this->add_got_entry(Got_entry());
1458 gsym->set_got_offset(got_type, got_offset);
1459 rel_dyn->add_global(gsym, r_type, this, got_offset);
1462 template<int size, bool big_endian>
1463 void
1464 Output_data_got<size, big_endian>::add_global_with_rela(
1465 Symbol* gsym,
1466 unsigned int got_type,
1467 Rela_dyn* rela_dyn,
1468 unsigned int r_type)
1470 if (gsym->has_got_offset(got_type))
1471 return;
1473 unsigned int got_offset = this->add_got_entry(Got_entry());
1474 gsym->set_got_offset(got_type, got_offset);
1475 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1478 // Add a pair of entries for a global symbol to the GOT, and add
1479 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1480 // If R_TYPE_2 == 0, add the second entry with no relocation.
1481 template<int size, bool big_endian>
1482 void
1483 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1484 Symbol* gsym,
1485 unsigned int got_type,
1486 Rel_dyn* rel_dyn,
1487 unsigned int r_type_1,
1488 unsigned int r_type_2)
1490 if (gsym->has_got_offset(got_type))
1491 return;
1493 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1494 gsym->set_got_offset(got_type, got_offset);
1495 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1497 if (r_type_2 != 0)
1498 rel_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8);
1501 template<int size, bool big_endian>
1502 void
1503 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1504 Symbol* gsym,
1505 unsigned int got_type,
1506 Rela_dyn* rela_dyn,
1507 unsigned int r_type_1,
1508 unsigned int r_type_2)
1510 if (gsym->has_got_offset(got_type))
1511 return;
1513 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1514 gsym->set_got_offset(got_type, got_offset);
1515 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1517 if (r_type_2 != 0)
1518 rela_dyn->add_global(gsym, r_type_2, this, got_offset + size / 8, 0);
1521 // Add an entry for a local symbol to the GOT. This returns true if
1522 // this is a new GOT entry, false if the symbol already has a GOT
1523 // entry.
1525 template<int size, bool big_endian>
1526 bool
1527 Output_data_got<size, big_endian>::add_local(
1528 Sized_relobj_file<size, big_endian>* object,
1529 unsigned int symndx,
1530 unsigned int got_type)
1532 if (object->local_has_got_offset(symndx, got_type))
1533 return false;
1535 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1536 false));
1537 object->set_local_got_offset(symndx, got_type, got_offset);
1538 return true;
1541 // Like add_local, but use the PLT offset.
1543 template<int size, bool big_endian>
1544 bool
1545 Output_data_got<size, big_endian>::add_local_plt(
1546 Sized_relobj_file<size, big_endian>* object,
1547 unsigned int symndx,
1548 unsigned int got_type)
1550 if (object->local_has_got_offset(symndx, got_type))
1551 return false;
1553 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1554 true));
1555 object->set_local_got_offset(symndx, got_type, got_offset);
1556 return true;
1559 // Add an entry for a local symbol to the GOT, and add a dynamic
1560 // relocation of type R_TYPE for the GOT entry.
1562 template<int size, bool big_endian>
1563 void
1564 Output_data_got<size, big_endian>::add_local_with_rel(
1565 Sized_relobj_file<size, big_endian>* object,
1566 unsigned int symndx,
1567 unsigned int got_type,
1568 Rel_dyn* rel_dyn,
1569 unsigned int r_type)
1571 if (object->local_has_got_offset(symndx, got_type))
1572 return;
1574 unsigned int got_offset = this->add_got_entry(Got_entry());
1575 object->set_local_got_offset(symndx, got_type, got_offset);
1576 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1579 template<int size, bool big_endian>
1580 void
1581 Output_data_got<size, big_endian>::add_local_with_rela(
1582 Sized_relobj_file<size, big_endian>* object,
1583 unsigned int symndx,
1584 unsigned int got_type,
1585 Rela_dyn* rela_dyn,
1586 unsigned int r_type)
1588 if (object->local_has_got_offset(symndx, got_type))
1589 return;
1591 unsigned int got_offset = this->add_got_entry(Got_entry());
1592 object->set_local_got_offset(symndx, got_type, got_offset);
1593 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1596 // Add a pair of entries for a local symbol to the GOT, and add
1597 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1598 // If R_TYPE_2 == 0, add the second entry with no relocation.
1599 template<int size, bool big_endian>
1600 void
1601 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1602 Sized_relobj_file<size, big_endian>* object,
1603 unsigned int symndx,
1604 unsigned int shndx,
1605 unsigned int got_type,
1606 Rel_dyn* rel_dyn,
1607 unsigned int r_type_1,
1608 unsigned int r_type_2)
1610 if (object->local_has_got_offset(symndx, got_type))
1611 return;
1613 unsigned int got_offset =
1614 this->add_got_entry_pair(Got_entry(),
1615 Got_entry(object, symndx, false));
1616 object->set_local_got_offset(symndx, got_type, got_offset);
1617 Output_section* os = object->output_section(shndx);
1618 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1620 if (r_type_2 != 0)
1621 rel_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8);
1624 template<int size, bool big_endian>
1625 void
1626 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1627 Sized_relobj_file<size, big_endian>* object,
1628 unsigned int symndx,
1629 unsigned int shndx,
1630 unsigned int got_type,
1631 Rela_dyn* rela_dyn,
1632 unsigned int r_type_1,
1633 unsigned int r_type_2)
1635 if (object->local_has_got_offset(symndx, got_type))
1636 return;
1638 unsigned int got_offset =
1639 this->add_got_entry_pair(Got_entry(),
1640 Got_entry(object, symndx, false));
1641 object->set_local_got_offset(symndx, got_type, got_offset);
1642 Output_section* os = object->output_section(shndx);
1643 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1645 if (r_type_2 != 0)
1646 rela_dyn->add_output_section(os, r_type_2, this, got_offset + size / 8, 0);
1649 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1651 template<int size, bool big_endian>
1652 void
1653 Output_data_got<size, big_endian>::reserve_local(
1654 unsigned int i,
1655 Sized_relobj<size, big_endian>* object,
1656 unsigned int sym_index,
1657 unsigned int got_type)
1659 this->reserve_slot(i);
1660 object->set_local_got_offset(sym_index, got_type, this->got_offset(i));
1663 // Reserve a slot in the GOT for a global symbol.
1665 template<int size, bool big_endian>
1666 void
1667 Output_data_got<size, big_endian>::reserve_global(
1668 unsigned int i,
1669 Symbol* gsym,
1670 unsigned int got_type)
1672 this->reserve_slot(i);
1673 gsym->set_got_offset(got_type, this->got_offset(i));
1676 // Write out the GOT.
1678 template<int size, bool big_endian>
1679 void
1680 Output_data_got<size, big_endian>::do_write(Output_file* of)
1682 const int add = size / 8;
1684 const off_t off = this->offset();
1685 const off_t oview_size = this->data_size();
1686 unsigned char* const oview = of->get_output_view(off, oview_size);
1688 unsigned char* pov = oview;
1689 for (typename Got_entries::const_iterator p = this->entries_.begin();
1690 p != this->entries_.end();
1691 ++p)
1693 p->write(pov);
1694 pov += add;
1697 gold_assert(pov - oview == oview_size);
1699 of->write_output_view(off, oview_size, oview);
1701 // We no longer need the GOT entries.
1702 this->entries_.clear();
1705 // Create a new GOT entry and return its offset.
1707 template<int size, bool big_endian>
1708 unsigned int
1709 Output_data_got<size, big_endian>::add_got_entry(Got_entry got_entry)
1711 if (!this->is_data_size_valid())
1713 this->entries_.push_back(got_entry);
1714 this->set_got_size();
1715 return this->last_got_offset();
1717 else
1719 // For an incremental update, find an available slot.
1720 off_t got_offset = this->free_list_.allocate(size / 8, size / 8, 0);
1721 if (got_offset == -1)
1722 gold_fallback(_("out of patch space (GOT);"
1723 " relink with --incremental-full"));
1724 unsigned int got_index = got_offset / (size / 8);
1725 gold_assert(got_index < this->entries_.size());
1726 this->entries_[got_index] = got_entry;
1727 return static_cast<unsigned int>(got_offset);
1731 // Create a pair of new GOT entries and return the offset of the first.
1733 template<int size, bool big_endian>
1734 unsigned int
1735 Output_data_got<size, big_endian>::add_got_entry_pair(Got_entry got_entry_1,
1736 Got_entry got_entry_2)
1738 if (!this->is_data_size_valid())
1740 unsigned int got_offset;
1741 this->entries_.push_back(got_entry_1);
1742 got_offset = this->last_got_offset();
1743 this->entries_.push_back(got_entry_2);
1744 this->set_got_size();
1745 return got_offset;
1747 else
1749 // For an incremental update, find an available pair of slots.
1750 off_t got_offset = this->free_list_.allocate(2 * size / 8, size / 8, 0);
1751 if (got_offset == -1)
1752 gold_fallback(_("out of patch space (GOT);"
1753 " relink with --incremental-full"));
1754 unsigned int got_index = got_offset / (size / 8);
1755 gold_assert(got_index < this->entries_.size());
1756 this->entries_[got_index] = got_entry_1;
1757 this->entries_[got_index + 1] = got_entry_2;
1758 return static_cast<unsigned int>(got_offset);
1762 // Output_data_dynamic::Dynamic_entry methods.
1764 // Write out the entry.
1766 template<int size, bool big_endian>
1767 void
1768 Output_data_dynamic::Dynamic_entry::write(
1769 unsigned char* pov,
1770 const Stringpool* pool) const
1772 typename elfcpp::Elf_types<size>::Elf_WXword val;
1773 switch (this->offset_)
1775 case DYNAMIC_NUMBER:
1776 val = this->u_.val;
1777 break;
1779 case DYNAMIC_SECTION_SIZE:
1780 val = this->u_.od->data_size();
1781 if (this->od2 != NULL)
1782 val += this->od2->data_size();
1783 break;
1785 case DYNAMIC_SYMBOL:
1787 const Sized_symbol<size>* s =
1788 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1789 val = s->value();
1791 break;
1793 case DYNAMIC_STRING:
1794 val = pool->get_offset(this->u_.str);
1795 break;
1797 default:
1798 val = this->u_.od->address() + this->offset_;
1799 break;
1802 elfcpp::Dyn_write<size, big_endian> dw(pov);
1803 dw.put_d_tag(this->tag_);
1804 dw.put_d_val(val);
1807 // Output_data_dynamic methods.
1809 // Adjust the output section to set the entry size.
1811 void
1812 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1814 if (parameters->target().get_size() == 32)
1815 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1816 else if (parameters->target().get_size() == 64)
1817 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1818 else
1819 gold_unreachable();
1822 // Set the final data size.
1824 void
1825 Output_data_dynamic::set_final_data_size()
1827 // Add the terminating entry if it hasn't been added.
1828 // Because of relaxation, we can run this multiple times.
1829 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1831 int extra = parameters->options().spare_dynamic_tags();
1832 for (int i = 0; i < extra; ++i)
1833 this->add_constant(elfcpp::DT_NULL, 0);
1834 this->add_constant(elfcpp::DT_NULL, 0);
1837 int dyn_size;
1838 if (parameters->target().get_size() == 32)
1839 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1840 else if (parameters->target().get_size() == 64)
1841 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1842 else
1843 gold_unreachable();
1844 this->set_data_size(this->entries_.size() * dyn_size);
1847 // Write out the dynamic entries.
1849 void
1850 Output_data_dynamic::do_write(Output_file* of)
1852 switch (parameters->size_and_endianness())
1854 #ifdef HAVE_TARGET_32_LITTLE
1855 case Parameters::TARGET_32_LITTLE:
1856 this->sized_write<32, false>(of);
1857 break;
1858 #endif
1859 #ifdef HAVE_TARGET_32_BIG
1860 case Parameters::TARGET_32_BIG:
1861 this->sized_write<32, true>(of);
1862 break;
1863 #endif
1864 #ifdef HAVE_TARGET_64_LITTLE
1865 case Parameters::TARGET_64_LITTLE:
1866 this->sized_write<64, false>(of);
1867 break;
1868 #endif
1869 #ifdef HAVE_TARGET_64_BIG
1870 case Parameters::TARGET_64_BIG:
1871 this->sized_write<64, true>(of);
1872 break;
1873 #endif
1874 default:
1875 gold_unreachable();
1879 template<int size, bool big_endian>
1880 void
1881 Output_data_dynamic::sized_write(Output_file* of)
1883 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1885 const off_t offset = this->offset();
1886 const off_t oview_size = this->data_size();
1887 unsigned char* const oview = of->get_output_view(offset, oview_size);
1889 unsigned char* pov = oview;
1890 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1891 p != this->entries_.end();
1892 ++p)
1894 p->write<size, big_endian>(pov, this->pool_);
1895 pov += dyn_size;
1898 gold_assert(pov - oview == oview_size);
1900 of->write_output_view(offset, oview_size, oview);
1902 // We no longer need the dynamic entries.
1903 this->entries_.clear();
1906 // Class Output_symtab_xindex.
1908 void
1909 Output_symtab_xindex::do_write(Output_file* of)
1911 const off_t offset = this->offset();
1912 const off_t oview_size = this->data_size();
1913 unsigned char* const oview = of->get_output_view(offset, oview_size);
1915 memset(oview, 0, oview_size);
1917 if (parameters->target().is_big_endian())
1918 this->endian_do_write<true>(oview);
1919 else
1920 this->endian_do_write<false>(oview);
1922 of->write_output_view(offset, oview_size, oview);
1924 // We no longer need the data.
1925 this->entries_.clear();
1928 template<bool big_endian>
1929 void
1930 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1932 for (Xindex_entries::const_iterator p = this->entries_.begin();
1933 p != this->entries_.end();
1934 ++p)
1936 unsigned int symndx = p->first;
1937 gold_assert(symndx * 4 < this->data_size());
1938 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1942 // Output_fill_debug_info methods.
1944 // Return the minimum size needed for a dummy compilation unit header.
1946 size_t
1947 Output_fill_debug_info::do_minimum_hole_size() const
1949 // Compile unit header fields: unit_length, version, debug_abbrev_offset,
1950 // address_size.
1951 const size_t len = 4 + 2 + 4 + 1;
1952 // For type units, add type_signature, type_offset.
1953 if (this->is_debug_types_)
1954 return len + 8 + 4;
1955 return len;
1958 // Write a dummy compilation unit header to fill a hole in the
1959 // .debug_info or .debug_types section.
1961 void
1962 Output_fill_debug_info::do_write(Output_file* of, off_t off, size_t len) const
1964 gold_debug(DEBUG_INCREMENTAL, "fill_debug_info(%08lx, %08lx)",
1965 static_cast<long>(off), static_cast<long>(len));
1967 gold_assert(len >= this->do_minimum_hole_size());
1969 unsigned char* const oview = of->get_output_view(off, len);
1970 unsigned char* pov = oview;
1972 // Write header fields: unit_length, version, debug_abbrev_offset,
1973 // address_size.
1974 if (this->is_big_endian())
1976 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
1977 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
1978 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, 0);
1980 else
1982 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
1983 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
1984 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, 0);
1986 pov += 4 + 2 + 4;
1987 *pov++ = 4;
1989 // For type units, the additional header fields -- type_signature,
1990 // type_offset -- can be filled with zeroes.
1992 // Fill the remainder of the free space with zeroes. The first
1993 // zero should tell the consumer there are no DIEs to read in this
1994 // compilation unit.
1995 if (pov < oview + len)
1996 memset(pov, 0, oview + len - pov);
1998 of->write_output_view(off, len, oview);
2001 // Output_fill_debug_line methods.
2003 // Return the minimum size needed for a dummy line number program header.
2005 size_t
2006 Output_fill_debug_line::do_minimum_hole_size() const
2008 // Line number program header fields: unit_length, version, header_length,
2009 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2010 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2011 const size_t len = 4 + 2 + 4 + this->header_length;
2012 return len;
2015 // Write a dummy line number program header to fill a hole in the
2016 // .debug_line section.
2018 void
2019 Output_fill_debug_line::do_write(Output_file* of, off_t off, size_t len) const
2021 gold_debug(DEBUG_INCREMENTAL, "fill_debug_line(%08lx, %08lx)",
2022 static_cast<long>(off), static_cast<long>(len));
2024 gold_assert(len >= this->do_minimum_hole_size());
2026 unsigned char* const oview = of->get_output_view(off, len);
2027 unsigned char* pov = oview;
2029 // Write header fields: unit_length, version, header_length,
2030 // minimum_instruction_length, default_is_stmt, line_base, line_range,
2031 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2032 // We set the header_length field to cover the entire hole, so the
2033 // line number program is empty.
2034 if (this->is_big_endian())
2036 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
2037 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
2038 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, len - (4 + 2 + 4));
2040 else
2042 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
2043 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
2044 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, len - (4 + 2 + 4));
2046 pov += 4 + 2 + 4;
2047 *pov++ = 1; // minimum_instruction_length
2048 *pov++ = 0; // default_is_stmt
2049 *pov++ = 0; // line_base
2050 *pov++ = 5; // line_range
2051 *pov++ = 13; // opcode_base
2052 *pov++ = 0; // standard_opcode_lengths[1]
2053 *pov++ = 1; // standard_opcode_lengths[2]
2054 *pov++ = 1; // standard_opcode_lengths[3]
2055 *pov++ = 1; // standard_opcode_lengths[4]
2056 *pov++ = 1; // standard_opcode_lengths[5]
2057 *pov++ = 0; // standard_opcode_lengths[6]
2058 *pov++ = 0; // standard_opcode_lengths[7]
2059 *pov++ = 0; // standard_opcode_lengths[8]
2060 *pov++ = 1; // standard_opcode_lengths[9]
2061 *pov++ = 0; // standard_opcode_lengths[10]
2062 *pov++ = 0; // standard_opcode_lengths[11]
2063 *pov++ = 1; // standard_opcode_lengths[12]
2064 *pov++ = 0; // include_directories (empty)
2065 *pov++ = 0; // filenames (empty)
2067 // Some consumers don't check the header_length field, and simply
2068 // start reading the line number program immediately following the
2069 // header. For those consumers, we fill the remainder of the free
2070 // space with DW_LNS_set_basic_block opcodes. These are effectively
2071 // no-ops: the resulting line table program will not create any rows.
2072 if (pov < oview + len)
2073 memset(pov, elfcpp::DW_LNS_set_basic_block, oview + len - pov);
2075 of->write_output_view(off, len, oview);
2078 // Output_section::Input_section methods.
2080 // Return the current data size. For an input section we store the size here.
2081 // For an Output_section_data, we have to ask it for the size.
2083 off_t
2084 Output_section::Input_section::current_data_size() const
2086 if (this->is_input_section())
2087 return this->u1_.data_size;
2088 else
2090 this->u2_.posd->pre_finalize_data_size();
2091 return this->u2_.posd->current_data_size();
2095 // Return the data size. For an input section we store the size here.
2096 // For an Output_section_data, we have to ask it for the size.
2098 off_t
2099 Output_section::Input_section::data_size() const
2101 if (this->is_input_section())
2102 return this->u1_.data_size;
2103 else
2104 return this->u2_.posd->data_size();
2107 // Return the object for an input section.
2109 Relobj*
2110 Output_section::Input_section::relobj() const
2112 if (this->is_input_section())
2113 return this->u2_.object;
2114 else if (this->is_merge_section())
2116 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2117 return this->u2_.pomb->first_relobj();
2119 else if (this->is_relaxed_input_section())
2120 return this->u2_.poris->relobj();
2121 else
2122 gold_unreachable();
2125 // Return the input section index for an input section.
2127 unsigned int
2128 Output_section::Input_section::shndx() const
2130 if (this->is_input_section())
2131 return this->shndx_;
2132 else if (this->is_merge_section())
2134 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2135 return this->u2_.pomb->first_shndx();
2137 else if (this->is_relaxed_input_section())
2138 return this->u2_.poris->shndx();
2139 else
2140 gold_unreachable();
2143 // Set the address and file offset.
2145 void
2146 Output_section::Input_section::set_address_and_file_offset(
2147 uint64_t address,
2148 off_t file_offset,
2149 off_t section_file_offset)
2151 if (this->is_input_section())
2152 this->u2_.object->set_section_offset(this->shndx_,
2153 file_offset - section_file_offset);
2154 else
2155 this->u2_.posd->set_address_and_file_offset(address, file_offset);
2158 // Reset the address and file offset.
2160 void
2161 Output_section::Input_section::reset_address_and_file_offset()
2163 if (!this->is_input_section())
2164 this->u2_.posd->reset_address_and_file_offset();
2167 // Finalize the data size.
2169 void
2170 Output_section::Input_section::finalize_data_size()
2172 if (!this->is_input_section())
2173 this->u2_.posd->finalize_data_size();
2176 // Try to turn an input offset into an output offset. We want to
2177 // return the output offset relative to the start of this
2178 // Input_section in the output section.
2180 inline bool
2181 Output_section::Input_section::output_offset(
2182 const Relobj* object,
2183 unsigned int shndx,
2184 section_offset_type offset,
2185 section_offset_type* poutput) const
2187 if (!this->is_input_section())
2188 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
2189 else
2191 if (this->shndx_ != shndx || this->u2_.object != object)
2192 return false;
2193 *poutput = offset;
2194 return true;
2198 // Return whether this is the merge section for the input section
2199 // SHNDX in OBJECT.
2201 inline bool
2202 Output_section::Input_section::is_merge_section_for(const Relobj* object,
2203 unsigned int shndx) const
2205 if (this->is_input_section())
2206 return false;
2207 return this->u2_.posd->is_merge_section_for(object, shndx);
2210 // Write out the data. We don't have to do anything for an input
2211 // section--they are handled via Object::relocate--but this is where
2212 // we write out the data for an Output_section_data.
2214 void
2215 Output_section::Input_section::write(Output_file* of)
2217 if (!this->is_input_section())
2218 this->u2_.posd->write(of);
2221 // Write the data to a buffer. As for write(), we don't have to do
2222 // anything for an input section.
2224 void
2225 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2227 if (!this->is_input_section())
2228 this->u2_.posd->write_to_buffer(buffer);
2231 // Print to a map file.
2233 void
2234 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2236 switch (this->shndx_)
2238 case OUTPUT_SECTION_CODE:
2239 case MERGE_DATA_SECTION_CODE:
2240 case MERGE_STRING_SECTION_CODE:
2241 this->u2_.posd->print_to_mapfile(mapfile);
2242 break;
2244 case RELAXED_INPUT_SECTION_CODE:
2246 Output_relaxed_input_section* relaxed_section =
2247 this->relaxed_input_section();
2248 mapfile->print_input_section(relaxed_section->relobj(),
2249 relaxed_section->shndx());
2251 break;
2252 default:
2253 mapfile->print_input_section(this->u2_.object, this->shndx_);
2254 break;
2258 // Output_section methods.
2260 // Construct an Output_section. NAME will point into a Stringpool.
2262 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2263 elfcpp::Elf_Xword flags)
2264 : name_(name),
2265 addralign_(0),
2266 entsize_(0),
2267 load_address_(0),
2268 link_section_(NULL),
2269 link_(0),
2270 info_section_(NULL),
2271 info_symndx_(NULL),
2272 info_(0),
2273 type_(type),
2274 flags_(flags),
2275 order_(ORDER_INVALID),
2276 out_shndx_(-1U),
2277 symtab_index_(0),
2278 dynsym_index_(0),
2279 input_sections_(),
2280 first_input_offset_(0),
2281 fills_(),
2282 postprocessing_buffer_(NULL),
2283 needs_symtab_index_(false),
2284 needs_dynsym_index_(false),
2285 should_link_to_symtab_(false),
2286 should_link_to_dynsym_(false),
2287 after_input_sections_(false),
2288 requires_postprocessing_(false),
2289 found_in_sections_clause_(false),
2290 has_load_address_(false),
2291 info_uses_section_index_(false),
2292 input_section_order_specified_(false),
2293 may_sort_attached_input_sections_(false),
2294 must_sort_attached_input_sections_(false),
2295 attached_input_sections_are_sorted_(false),
2296 is_relro_(false),
2297 is_small_section_(false),
2298 is_large_section_(false),
2299 generate_code_fills_at_write_(false),
2300 is_entsize_zero_(false),
2301 section_offsets_need_adjustment_(false),
2302 is_noload_(false),
2303 always_keeps_input_sections_(false),
2304 has_fixed_layout_(false),
2305 is_patch_space_allowed_(false),
2306 tls_offset_(0),
2307 checkpoint_(NULL),
2308 lookup_maps_(new Output_section_lookup_maps),
2309 free_list_(),
2310 free_space_fill_(NULL),
2311 patch_space_(0)
2313 // An unallocated section has no address. Forcing this means that
2314 // we don't need special treatment for symbols defined in debug
2315 // sections.
2316 if ((flags & elfcpp::SHF_ALLOC) == 0)
2317 this->set_address(0);
2320 Output_section::~Output_section()
2322 delete this->checkpoint_;
2325 // Set the entry size.
2327 void
2328 Output_section::set_entsize(uint64_t v)
2330 if (this->is_entsize_zero_)
2332 else if (this->entsize_ == 0)
2333 this->entsize_ = v;
2334 else if (this->entsize_ != v)
2336 this->entsize_ = 0;
2337 this->is_entsize_zero_ = 1;
2341 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2342 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2343 // relocation section which applies to this section, or 0 if none, or
2344 // -1U if more than one. Return the offset of the input section
2345 // within the output section. Return -1 if the input section will
2346 // receive special handling. In the normal case we don't always keep
2347 // track of input sections for an Output_section. Instead, each
2348 // Object keeps track of the Output_section for each of its input
2349 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2350 // track of input sections here; this is used when SECTIONS appears in
2351 // a linker script.
2353 template<int size, bool big_endian>
2354 off_t
2355 Output_section::add_input_section(Layout* layout,
2356 Sized_relobj_file<size, big_endian>* object,
2357 unsigned int shndx,
2358 const char* secname,
2359 const elfcpp::Shdr<size, big_endian>& shdr,
2360 unsigned int reloc_shndx,
2361 bool have_sections_script)
2363 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2364 if ((addralign & (addralign - 1)) != 0)
2366 object->error(_("invalid alignment %lu for section \"%s\""),
2367 static_cast<unsigned long>(addralign), secname);
2368 addralign = 1;
2371 if (addralign > this->addralign_)
2372 this->addralign_ = addralign;
2374 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2375 uint64_t entsize = shdr.get_sh_entsize();
2377 // .debug_str is a mergeable string section, but is not always so
2378 // marked by compilers. Mark manually here so we can optimize.
2379 if (strcmp(secname, ".debug_str") == 0)
2381 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2382 entsize = 1;
2385 this->update_flags_for_input_section(sh_flags);
2386 this->set_entsize(entsize);
2388 // If this is a SHF_MERGE section, we pass all the input sections to
2389 // a Output_data_merge. We don't try to handle relocations for such
2390 // a section. We don't try to handle empty merge sections--they
2391 // mess up the mappings, and are useless anyhow.
2392 // FIXME: Need to handle merge sections during incremental update.
2393 if ((sh_flags & elfcpp::SHF_MERGE) != 0
2394 && reloc_shndx == 0
2395 && shdr.get_sh_size() > 0
2396 && !parameters->incremental())
2398 // Keep information about merged input sections for rebuilding fast
2399 // lookup maps if we have sections-script or we do relaxation.
2400 bool keeps_input_sections = (this->always_keeps_input_sections_
2401 || have_sections_script
2402 || parameters->target().may_relax());
2404 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2405 addralign, keeps_input_sections))
2407 // Tell the relocation routines that they need to call the
2408 // output_offset method to determine the final address.
2409 return -1;
2413 section_size_type input_section_size = shdr.get_sh_size();
2414 section_size_type uncompressed_size;
2415 if (object->section_is_compressed(shndx, &uncompressed_size))
2416 input_section_size = uncompressed_size;
2418 off_t offset_in_section;
2419 off_t aligned_offset_in_section;
2420 if (this->has_fixed_layout())
2422 // For incremental updates, find a chunk of unused space in the section.
2423 offset_in_section = this->free_list_.allocate(input_section_size,
2424 addralign, 0);
2425 if (offset_in_section == -1)
2426 gold_fallback(_("out of patch space in section %s; "
2427 "relink with --incremental-full"),
2428 this->name());
2429 aligned_offset_in_section = offset_in_section;
2431 else
2433 offset_in_section = this->current_data_size_for_child();
2434 aligned_offset_in_section = align_address(offset_in_section,
2435 addralign);
2436 this->set_current_data_size_for_child(aligned_offset_in_section
2437 + input_section_size);
2440 // Determine if we want to delay code-fill generation until the output
2441 // section is written. When the target is relaxing, we want to delay fill
2442 // generating to avoid adjusting them during relaxation. Also, if we are
2443 // sorting input sections we must delay fill generation.
2444 if (!this->generate_code_fills_at_write_
2445 && !have_sections_script
2446 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2447 && parameters->target().has_code_fill()
2448 && (parameters->target().may_relax()
2449 || layout->is_section_ordering_specified()))
2451 gold_assert(this->fills_.empty());
2452 this->generate_code_fills_at_write_ = true;
2455 if (aligned_offset_in_section > offset_in_section
2456 && !this->generate_code_fills_at_write_
2457 && !have_sections_script
2458 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2459 && parameters->target().has_code_fill())
2461 // We need to add some fill data. Using fill_list_ when
2462 // possible is an optimization, since we will often have fill
2463 // sections without input sections.
2464 off_t fill_len = aligned_offset_in_section - offset_in_section;
2465 if (this->input_sections_.empty())
2466 this->fills_.push_back(Fill(offset_in_section, fill_len));
2467 else
2469 std::string fill_data(parameters->target().code_fill(fill_len));
2470 Output_data_const* odc = new Output_data_const(fill_data, 1);
2471 this->input_sections_.push_back(Input_section(odc));
2475 // We need to keep track of this section if we are already keeping
2476 // track of sections, or if we are relaxing. Also, if this is a
2477 // section which requires sorting, or which may require sorting in
2478 // the future, we keep track of the sections. If the
2479 // --section-ordering-file option is used to specify the order of
2480 // sections, we need to keep track of sections.
2481 if (this->always_keeps_input_sections_
2482 || have_sections_script
2483 || !this->input_sections_.empty()
2484 || this->may_sort_attached_input_sections()
2485 || this->must_sort_attached_input_sections()
2486 || parameters->options().user_set_Map()
2487 || parameters->target().may_relax()
2488 || layout->is_section_ordering_specified())
2490 Input_section isecn(object, shndx, input_section_size, addralign);
2491 /* If section ordering is requested by specifying a ordering file,
2492 using --section-ordering-file, match the section name with
2493 a pattern. */
2494 if (parameters->options().section_ordering_file())
2496 unsigned int section_order_index =
2497 layout->find_section_order_index(std::string(secname));
2498 if (section_order_index != 0)
2500 isecn.set_section_order_index(section_order_index);
2501 this->set_input_section_order_specified();
2504 if (this->has_fixed_layout())
2506 // For incremental updates, finalize the address and offset now.
2507 uint64_t addr = this->address();
2508 isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2509 aligned_offset_in_section,
2510 this->offset());
2512 this->input_sections_.push_back(isecn);
2515 return aligned_offset_in_section;
2518 // Add arbitrary data to an output section.
2520 void
2521 Output_section::add_output_section_data(Output_section_data* posd)
2523 Input_section inp(posd);
2524 this->add_output_section_data(&inp);
2526 if (posd->is_data_size_valid())
2528 off_t offset_in_section;
2529 if (this->has_fixed_layout())
2531 // For incremental updates, find a chunk of unused space.
2532 offset_in_section = this->free_list_.allocate(posd->data_size(),
2533 posd->addralign(), 0);
2534 if (offset_in_section == -1)
2535 gold_fallback(_("out of patch space in section %s; "
2536 "relink with --incremental-full"),
2537 this->name());
2538 // Finalize the address and offset now.
2539 uint64_t addr = this->address();
2540 off_t offset = this->offset();
2541 posd->set_address_and_file_offset(addr + offset_in_section,
2542 offset + offset_in_section);
2544 else
2546 offset_in_section = this->current_data_size_for_child();
2547 off_t aligned_offset_in_section = align_address(offset_in_section,
2548 posd->addralign());
2549 this->set_current_data_size_for_child(aligned_offset_in_section
2550 + posd->data_size());
2553 else if (this->has_fixed_layout())
2555 // For incremental updates, arrange for the data to have a fixed layout.
2556 // This will mean that additions to the data must be allocated from
2557 // free space within the containing output section.
2558 uint64_t addr = this->address();
2559 posd->set_address(addr);
2560 posd->set_file_offset(0);
2561 // FIXME: This should eventually be unreachable.
2562 // gold_unreachable();
2566 // Add a relaxed input section.
2568 void
2569 Output_section::add_relaxed_input_section(Layout* layout,
2570 Output_relaxed_input_section* poris,
2571 const std::string& name)
2573 Input_section inp(poris);
2575 // If the --section-ordering-file option is used to specify the order of
2576 // sections, we need to keep track of sections.
2577 if (layout->is_section_ordering_specified())
2579 unsigned int section_order_index =
2580 layout->find_section_order_index(name);
2581 if (section_order_index != 0)
2583 inp.set_section_order_index(section_order_index);
2584 this->set_input_section_order_specified();
2588 this->add_output_section_data(&inp);
2589 if (this->lookup_maps_->is_valid())
2590 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2591 poris->shndx(), poris);
2593 // For a relaxed section, we use the current data size. Linker scripts
2594 // get all the input sections, including relaxed one from an output
2595 // section and add them back to them same output section to compute the
2596 // output section size. If we do not account for sizes of relaxed input
2597 // sections, an output section would be incorrectly sized.
2598 off_t offset_in_section = this->current_data_size_for_child();
2599 off_t aligned_offset_in_section = align_address(offset_in_section,
2600 poris->addralign());
2601 this->set_current_data_size_for_child(aligned_offset_in_section
2602 + poris->current_data_size());
2605 // Add arbitrary data to an output section by Input_section.
2607 void
2608 Output_section::add_output_section_data(Input_section* inp)
2610 if (this->input_sections_.empty())
2611 this->first_input_offset_ = this->current_data_size_for_child();
2613 this->input_sections_.push_back(*inp);
2615 uint64_t addralign = inp->addralign();
2616 if (addralign > this->addralign_)
2617 this->addralign_ = addralign;
2619 inp->set_output_section(this);
2622 // Add a merge section to an output section.
2624 void
2625 Output_section::add_output_merge_section(Output_section_data* posd,
2626 bool is_string, uint64_t entsize)
2628 Input_section inp(posd, is_string, entsize);
2629 this->add_output_section_data(&inp);
2632 // Add an input section to a SHF_MERGE section.
2634 bool
2635 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2636 uint64_t flags, uint64_t entsize,
2637 uint64_t addralign,
2638 bool keeps_input_sections)
2640 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2642 // We only merge strings if the alignment is not more than the
2643 // character size. This could be handled, but it's unusual.
2644 if (is_string && addralign > entsize)
2645 return false;
2647 // We cannot restore merged input section states.
2648 gold_assert(this->checkpoint_ == NULL);
2650 // Look up merge sections by required properties.
2651 // Currently, we only invalidate the lookup maps in script processing
2652 // and relaxation. We should not have done either when we reach here.
2653 // So we assume that the lookup maps are valid to simply code.
2654 gold_assert(this->lookup_maps_->is_valid());
2655 Merge_section_properties msp(is_string, entsize, addralign);
2656 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2657 bool is_new = false;
2658 if (pomb != NULL)
2660 gold_assert(pomb->is_string() == is_string
2661 && pomb->entsize() == entsize
2662 && pomb->addralign() == addralign);
2664 else
2666 // Create a new Output_merge_data or Output_merge_string_data.
2667 if (!is_string)
2668 pomb = new Output_merge_data(entsize, addralign);
2669 else
2671 switch (entsize)
2673 case 1:
2674 pomb = new Output_merge_string<char>(addralign);
2675 break;
2676 case 2:
2677 pomb = new Output_merge_string<uint16_t>(addralign);
2678 break;
2679 case 4:
2680 pomb = new Output_merge_string<uint32_t>(addralign);
2681 break;
2682 default:
2683 return false;
2686 // If we need to do script processing or relaxation, we need to keep
2687 // the original input sections to rebuild the fast lookup maps.
2688 if (keeps_input_sections)
2689 pomb->set_keeps_input_sections();
2690 is_new = true;
2693 if (pomb->add_input_section(object, shndx))
2695 // Add new merge section to this output section and link merge
2696 // section properties to new merge section in map.
2697 if (is_new)
2699 this->add_output_merge_section(pomb, is_string, entsize);
2700 this->lookup_maps_->add_merge_section(msp, pomb);
2703 // Add input section to new merge section and link input section to new
2704 // merge section in map.
2705 this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
2706 return true;
2708 else
2710 // If add_input_section failed, delete new merge section to avoid
2711 // exporting empty merge sections in Output_section::get_input_section.
2712 if (is_new)
2713 delete pomb;
2714 return false;
2718 // Build a relaxation map to speed up relaxation of existing input sections.
2719 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2721 void
2722 Output_section::build_relaxation_map(
2723 const Input_section_list& input_sections,
2724 size_t limit,
2725 Relaxation_map* relaxation_map) const
2727 for (size_t i = 0; i < limit; ++i)
2729 const Input_section& is(input_sections[i]);
2730 if (is.is_input_section() || is.is_relaxed_input_section())
2732 Section_id sid(is.relobj(), is.shndx());
2733 (*relaxation_map)[sid] = i;
2738 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2739 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2740 // indices of INPUT_SECTIONS.
2742 void
2743 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2744 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2745 const Relaxation_map& map,
2746 Input_section_list* input_sections)
2748 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2750 Output_relaxed_input_section* poris = relaxed_sections[i];
2751 Section_id sid(poris->relobj(), poris->shndx());
2752 Relaxation_map::const_iterator p = map.find(sid);
2753 gold_assert(p != map.end());
2754 gold_assert((*input_sections)[p->second].is_input_section());
2756 // Remember section order index of original input section
2757 // if it is set. Copy it to the relaxed input section.
2758 unsigned int soi =
2759 (*input_sections)[p->second].section_order_index();
2760 (*input_sections)[p->second] = Input_section(poris);
2761 (*input_sections)[p->second].set_section_order_index(soi);
2765 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2766 // is a vector of pointers to Output_relaxed_input_section or its derived
2767 // classes. The relaxed sections must correspond to existing input sections.
2769 void
2770 Output_section::convert_input_sections_to_relaxed_sections(
2771 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2773 gold_assert(parameters->target().may_relax());
2775 // We want to make sure that restore_states does not undo the effect of
2776 // this. If there is no checkpoint active, just search the current
2777 // input section list and replace the sections there. If there is
2778 // a checkpoint, also replace the sections there.
2780 // By default, we look at the whole list.
2781 size_t limit = this->input_sections_.size();
2783 if (this->checkpoint_ != NULL)
2785 // Replace input sections with relaxed input section in the saved
2786 // copy of the input section list.
2787 if (this->checkpoint_->input_sections_saved())
2789 Relaxation_map map;
2790 this->build_relaxation_map(
2791 *(this->checkpoint_->input_sections()),
2792 this->checkpoint_->input_sections()->size(),
2793 &map);
2794 this->convert_input_sections_in_list_to_relaxed_sections(
2795 relaxed_sections,
2796 map,
2797 this->checkpoint_->input_sections());
2799 else
2801 // We have not copied the input section list yet. Instead, just
2802 // look at the portion that would be saved.
2803 limit = this->checkpoint_->input_sections_size();
2807 // Convert input sections in input_section_list.
2808 Relaxation_map map;
2809 this->build_relaxation_map(this->input_sections_, limit, &map);
2810 this->convert_input_sections_in_list_to_relaxed_sections(
2811 relaxed_sections,
2812 map,
2813 &this->input_sections_);
2815 // Update fast look-up map.
2816 if (this->lookup_maps_->is_valid())
2817 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2819 Output_relaxed_input_section* poris = relaxed_sections[i];
2820 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2821 poris->shndx(), poris);
2825 // Update the output section flags based on input section flags.
2827 void
2828 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2830 // If we created the section with SHF_ALLOC clear, we set the
2831 // address. If we are now setting the SHF_ALLOC flag, we need to
2832 // undo that.
2833 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2834 && (flags & elfcpp::SHF_ALLOC) != 0)
2835 this->mark_address_invalid();
2837 this->flags_ |= (flags
2838 & (elfcpp::SHF_WRITE
2839 | elfcpp::SHF_ALLOC
2840 | elfcpp::SHF_EXECINSTR));
2842 if ((flags & elfcpp::SHF_MERGE) == 0)
2843 this->flags_ &=~ elfcpp::SHF_MERGE;
2844 else
2846 if (this->current_data_size_for_child() == 0)
2847 this->flags_ |= elfcpp::SHF_MERGE;
2850 if ((flags & elfcpp::SHF_STRINGS) == 0)
2851 this->flags_ &=~ elfcpp::SHF_STRINGS;
2852 else
2854 if (this->current_data_size_for_child() == 0)
2855 this->flags_ |= elfcpp::SHF_STRINGS;
2859 // Find the merge section into which an input section with index SHNDX in
2860 // OBJECT has been added. Return NULL if none found.
2862 Output_section_data*
2863 Output_section::find_merge_section(const Relobj* object,
2864 unsigned int shndx) const
2866 if (!this->lookup_maps_->is_valid())
2867 this->build_lookup_maps();
2868 return this->lookup_maps_->find_merge_section(object, shndx);
2871 // Build the lookup maps for merge and relaxed sections. This is needs
2872 // to be declared as a const methods so that it is callable with a const
2873 // Output_section pointer. The method only updates states of the maps.
2875 void
2876 Output_section::build_lookup_maps() const
2878 this->lookup_maps_->clear();
2879 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2880 p != this->input_sections_.end();
2881 ++p)
2883 if (p->is_merge_section())
2885 Output_merge_base* pomb = p->output_merge_base();
2886 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2887 pomb->addralign());
2888 this->lookup_maps_->add_merge_section(msp, pomb);
2889 for (Output_merge_base::Input_sections::const_iterator is =
2890 pomb->input_sections_begin();
2891 is != pomb->input_sections_end();
2892 ++is)
2894 const Const_section_id& csid = *is;
2895 this->lookup_maps_->add_merge_input_section(csid.first,
2896 csid.second, pomb);
2900 else if (p->is_relaxed_input_section())
2902 Output_relaxed_input_section* poris = p->relaxed_input_section();
2903 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2904 poris->shndx(), poris);
2909 // Find an relaxed input section corresponding to an input section
2910 // in OBJECT with index SHNDX.
2912 const Output_relaxed_input_section*
2913 Output_section::find_relaxed_input_section(const Relobj* object,
2914 unsigned int shndx) const
2916 if (!this->lookup_maps_->is_valid())
2917 this->build_lookup_maps();
2918 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2921 // Given an address OFFSET relative to the start of input section
2922 // SHNDX in OBJECT, return whether this address is being included in
2923 // the final link. This should only be called if SHNDX in OBJECT has
2924 // a special mapping.
2926 bool
2927 Output_section::is_input_address_mapped(const Relobj* object,
2928 unsigned int shndx,
2929 off_t offset) const
2931 // Look at the Output_section_data_maps first.
2932 const Output_section_data* posd = this->find_merge_section(object, shndx);
2933 if (posd == NULL)
2934 posd = this->find_relaxed_input_section(object, shndx);
2936 if (posd != NULL)
2938 section_offset_type output_offset;
2939 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2940 gold_assert(found);
2941 return output_offset != -1;
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 section_offset_type output_offset;
2950 if (p->output_offset(object, shndx, offset, &output_offset))
2951 return output_offset != -1;
2954 // By default we assume that the address is mapped. This should
2955 // only be called after we have passed all sections to Layout. At
2956 // that point we should know what we are discarding.
2957 return true;
2960 // Given an address OFFSET relative to the start of input section
2961 // SHNDX in object OBJECT, return the output offset relative to the
2962 // start of the input section in the output section. This should only
2963 // be called if SHNDX in OBJECT has a special mapping.
2965 section_offset_type
2966 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2967 section_offset_type offset) const
2969 // This can only be called meaningfully when we know the data size
2970 // of this.
2971 gold_assert(this->is_data_size_valid());
2973 // Look at the Output_section_data_maps first.
2974 const Output_section_data* posd = this->find_merge_section(object, shndx);
2975 if (posd == NULL)
2976 posd = this->find_relaxed_input_section(object, shndx);
2977 if (posd != NULL)
2979 section_offset_type output_offset;
2980 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2981 gold_assert(found);
2982 return output_offset;
2985 // Fall back to the slow look-up.
2986 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2987 p != this->input_sections_.end();
2988 ++p)
2990 section_offset_type output_offset;
2991 if (p->output_offset(object, shndx, offset, &output_offset))
2992 return output_offset;
2994 gold_unreachable();
2997 // Return the output virtual address of OFFSET relative to the start
2998 // of input section SHNDX in object OBJECT.
3000 uint64_t
3001 Output_section::output_address(const Relobj* object, unsigned int shndx,
3002 off_t offset) const
3004 uint64_t addr = this->address() + this->first_input_offset_;
3006 // Look at the Output_section_data_maps first.
3007 const Output_section_data* posd = this->find_merge_section(object, shndx);
3008 if (posd == NULL)
3009 posd = this->find_relaxed_input_section(object, shndx);
3010 if (posd != NULL && posd->is_address_valid())
3012 section_offset_type output_offset;
3013 bool found = posd->output_offset(object, shndx, offset, &output_offset);
3014 gold_assert(found);
3015 return posd->address() + output_offset;
3018 // Fall back to the slow look-up.
3019 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3020 p != this->input_sections_.end();
3021 ++p)
3023 addr = align_address(addr, p->addralign());
3024 section_offset_type output_offset;
3025 if (p->output_offset(object, shndx, offset, &output_offset))
3027 if (output_offset == -1)
3028 return -1ULL;
3029 return addr + output_offset;
3031 addr += p->data_size();
3034 // If we get here, it means that we don't know the mapping for this
3035 // input section. This might happen in principle if
3036 // add_input_section were called before add_output_section_data.
3037 // But it should never actually happen.
3039 gold_unreachable();
3042 // Find the output address of the start of the merged section for
3043 // input section SHNDX in object OBJECT.
3045 bool
3046 Output_section::find_starting_output_address(const Relobj* object,
3047 unsigned int shndx,
3048 uint64_t* paddr) const
3050 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
3051 // Looking up the merge section map does not always work as we sometimes
3052 // find a merge section without its address set.
3053 uint64_t addr = this->address() + this->first_input_offset_;
3054 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3055 p != this->input_sections_.end();
3056 ++p)
3058 addr = align_address(addr, p->addralign());
3060 // It would be nice if we could use the existing output_offset
3061 // method to get the output offset of input offset 0.
3062 // Unfortunately we don't know for sure that input offset 0 is
3063 // mapped at all.
3064 if (p->is_merge_section_for(object, shndx))
3066 *paddr = addr;
3067 return true;
3070 addr += p->data_size();
3073 // We couldn't find a merge output section for this input section.
3074 return false;
3077 // Update the data size of an Output_section.
3079 void
3080 Output_section::update_data_size()
3082 if (this->input_sections_.empty())
3083 return;
3085 if (this->must_sort_attached_input_sections()
3086 || this->input_section_order_specified())
3087 this->sort_attached_input_sections();
3089 off_t off = this->first_input_offset_;
3090 for (Input_section_list::iterator p = this->input_sections_.begin();
3091 p != this->input_sections_.end();
3092 ++p)
3094 off = align_address(off, p->addralign());
3095 off += p->current_data_size();
3098 this->set_current_data_size_for_child(off);
3101 // Set the data size of an Output_section. This is where we handle
3102 // setting the addresses of any Output_section_data objects.
3104 void
3105 Output_section::set_final_data_size()
3107 off_t data_size;
3109 if (this->input_sections_.empty())
3110 data_size = this->current_data_size_for_child();
3111 else
3113 if (this->must_sort_attached_input_sections()
3114 || this->input_section_order_specified())
3115 this->sort_attached_input_sections();
3117 uint64_t address = this->address();
3118 off_t startoff = this->offset();
3119 off_t off = startoff + this->first_input_offset_;
3120 for (Input_section_list::iterator p = this->input_sections_.begin();
3121 p != this->input_sections_.end();
3122 ++p)
3124 off = align_address(off, p->addralign());
3125 p->set_address_and_file_offset(address + (off - startoff), off,
3126 startoff);
3127 off += p->data_size();
3129 data_size = off - startoff;
3132 // For full incremental links, we want to allocate some patch space
3133 // in most sections for subsequent incremental updates.
3134 if (this->is_patch_space_allowed_ && parameters->incremental_full())
3136 double pct = parameters->options().incremental_patch();
3137 size_t extra = static_cast<size_t>(data_size * pct);
3138 if (this->free_space_fill_ != NULL
3139 && this->free_space_fill_->minimum_hole_size() > extra)
3140 extra = this->free_space_fill_->minimum_hole_size();
3141 off_t new_size = align_address(data_size + extra, this->addralign());
3142 this->patch_space_ = new_size - data_size;
3143 gold_debug(DEBUG_INCREMENTAL,
3144 "set_final_data_size: %08lx + %08lx: section %s",
3145 static_cast<long>(data_size),
3146 static_cast<long>(this->patch_space_),
3147 this->name());
3148 data_size = new_size;
3151 this->set_data_size(data_size);
3154 // Reset the address and file offset.
3156 void
3157 Output_section::do_reset_address_and_file_offset()
3159 // An unallocated section has no address. Forcing this means that
3160 // we don't need special treatment for symbols defined in debug
3161 // sections. We do the same in the constructor. This does not
3162 // apply to NOLOAD sections though.
3163 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
3164 this->set_address(0);
3166 for (Input_section_list::iterator p = this->input_sections_.begin();
3167 p != this->input_sections_.end();
3168 ++p)
3169 p->reset_address_and_file_offset();
3171 // Remove any patch space that was added in set_final_data_size.
3172 if (this->patch_space_ > 0)
3174 this->set_current_data_size_for_child(this->current_data_size_for_child()
3175 - this->patch_space_);
3176 this->patch_space_ = 0;
3180 // Return true if address and file offset have the values after reset.
3182 bool
3183 Output_section::do_address_and_file_offset_have_reset_values() const
3185 if (this->is_offset_valid())
3186 return false;
3188 // An unallocated section has address 0 after its construction or a reset.
3189 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
3190 return this->is_address_valid() && this->address() == 0;
3191 else
3192 return !this->is_address_valid();
3195 // Set the TLS offset. Called only for SHT_TLS sections.
3197 void
3198 Output_section::do_set_tls_offset(uint64_t tls_base)
3200 this->tls_offset_ = this->address() - tls_base;
3203 // In a few cases we need to sort the input sections attached to an
3204 // output section. This is used to implement the type of constructor
3205 // priority ordering implemented by the GNU linker, in which the
3206 // priority becomes part of the section name and the sections are
3207 // sorted by name. We only do this for an output section if we see an
3208 // attached input section matching ".ctors.*", ".dtors.*",
3209 // ".init_array.*" or ".fini_array.*".
3211 class Output_section::Input_section_sort_entry
3213 public:
3214 Input_section_sort_entry()
3215 : input_section_(), index_(-1U), section_has_name_(false),
3216 section_name_()
3219 Input_section_sort_entry(const Input_section& input_section,
3220 unsigned int index,
3221 bool must_sort_attached_input_sections)
3222 : input_section_(input_section), index_(index),
3223 section_has_name_(input_section.is_input_section()
3224 || input_section.is_relaxed_input_section())
3226 if (this->section_has_name_
3227 && must_sort_attached_input_sections)
3229 // This is only called single-threaded from Layout::finalize,
3230 // so it is OK to lock. Unfortunately we have no way to pass
3231 // in a Task token.
3232 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
3233 Object* obj = (input_section.is_input_section()
3234 ? input_section.relobj()
3235 : input_section.relaxed_input_section()->relobj());
3236 Task_lock_obj<Object> tl(dummy_task, obj);
3238 // This is a slow operation, which should be cached in
3239 // Layout::layout if this becomes a speed problem.
3240 this->section_name_ = obj->section_name(input_section.shndx());
3244 // Return the Input_section.
3245 const Input_section&
3246 input_section() const
3248 gold_assert(this->index_ != -1U);
3249 return this->input_section_;
3252 // The index of this entry in the original list. This is used to
3253 // make the sort stable.
3254 unsigned int
3255 index() const
3257 gold_assert(this->index_ != -1U);
3258 return this->index_;
3261 // Whether there is a section name.
3262 bool
3263 section_has_name() const
3264 { return this->section_has_name_; }
3266 // The section name.
3267 const std::string&
3268 section_name() const
3270 gold_assert(this->section_has_name_);
3271 return this->section_name_;
3274 // Return true if the section name has a priority. This is assumed
3275 // to be true if it has a dot after the initial dot.
3276 bool
3277 has_priority() const
3279 gold_assert(this->section_has_name_);
3280 return this->section_name_.find('.', 1) != std::string::npos;
3283 // Return the priority. Believe it or not, gcc encodes the priority
3284 // differently for .ctors/.dtors and .init_array/.fini_array
3285 // sections.
3286 unsigned int
3287 get_priority() const
3289 gold_assert(this->section_has_name_);
3290 bool is_ctors;
3291 if (is_prefix_of(".ctors.", this->section_name_.c_str())
3292 || is_prefix_of(".dtors.", this->section_name_.c_str()))
3293 is_ctors = true;
3294 else if (is_prefix_of(".init_array.", this->section_name_.c_str())
3295 || is_prefix_of(".fini_array.", this->section_name_.c_str()))
3296 is_ctors = false;
3297 else
3298 return 0;
3299 char* end;
3300 unsigned long prio = strtoul((this->section_name_.c_str()
3301 + (is_ctors ? 7 : 12)),
3302 &end, 10);
3303 if (*end != '\0')
3304 return 0;
3305 else if (is_ctors)
3306 return 65535 - prio;
3307 else
3308 return prio;
3311 // Return true if this an input file whose base name matches
3312 // FILE_NAME. The base name must have an extension of ".o", and
3313 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3314 // This is to match crtbegin.o as well as crtbeginS.o without
3315 // getting confused by other possibilities. Overall matching the
3316 // file name this way is a dreadful hack, but the GNU linker does it
3317 // in order to better support gcc, and we need to be compatible.
3318 bool
3319 match_file_name(const char* file_name) const
3320 { return Layout::match_file_name(this->input_section_.relobj(), file_name); }
3322 // Returns 1 if THIS should appear before S in section order, -1 if S
3323 // appears before THIS and 0 if they are not comparable.
3325 compare_section_ordering(const Input_section_sort_entry& s) const
3327 unsigned int this_secn_index = this->input_section_.section_order_index();
3328 unsigned int s_secn_index = s.input_section().section_order_index();
3329 if (this_secn_index > 0 && s_secn_index > 0)
3331 if (this_secn_index < s_secn_index)
3332 return 1;
3333 else if (this_secn_index > s_secn_index)
3334 return -1;
3336 return 0;
3339 private:
3340 // The Input_section we are sorting.
3341 Input_section input_section_;
3342 // The index of this Input_section in the original list.
3343 unsigned int index_;
3344 // Whether this Input_section has a section name--it won't if this
3345 // is some random Output_section_data.
3346 bool section_has_name_;
3347 // The section name if there is one.
3348 std::string section_name_;
3351 // Return true if S1 should come before S2 in the output section.
3353 bool
3354 Output_section::Input_section_sort_compare::operator()(
3355 const Output_section::Input_section_sort_entry& s1,
3356 const Output_section::Input_section_sort_entry& s2) const
3358 // crtbegin.o must come first.
3359 bool s1_begin = s1.match_file_name("crtbegin");
3360 bool s2_begin = s2.match_file_name("crtbegin");
3361 if (s1_begin || s2_begin)
3363 if (!s1_begin)
3364 return false;
3365 if (!s2_begin)
3366 return true;
3367 return s1.index() < s2.index();
3370 // crtend.o must come last.
3371 bool s1_end = s1.match_file_name("crtend");
3372 bool s2_end = s2.match_file_name("crtend");
3373 if (s1_end || s2_end)
3375 if (!s1_end)
3376 return true;
3377 if (!s2_end)
3378 return false;
3379 return s1.index() < s2.index();
3382 // We sort all the sections with no names to the end.
3383 if (!s1.section_has_name() || !s2.section_has_name())
3385 if (s1.section_has_name())
3386 return true;
3387 if (s2.section_has_name())
3388 return false;
3389 return s1.index() < s2.index();
3392 // A section with a priority follows a section without a priority.
3393 bool s1_has_priority = s1.has_priority();
3394 bool s2_has_priority = s2.has_priority();
3395 if (s1_has_priority && !s2_has_priority)
3396 return false;
3397 if (!s1_has_priority && s2_has_priority)
3398 return true;
3400 // Check if a section order exists for these sections through a section
3401 // ordering file. If sequence_num is 0, an order does not exist.
3402 int sequence_num = s1.compare_section_ordering(s2);
3403 if (sequence_num != 0)
3404 return sequence_num == 1;
3406 // Otherwise we sort by name.
3407 int compare = s1.section_name().compare(s2.section_name());
3408 if (compare != 0)
3409 return compare < 0;
3411 // Otherwise we keep the input order.
3412 return s1.index() < s2.index();
3415 // Return true if S1 should come before S2 in an .init_array or .fini_array
3416 // output section.
3418 bool
3419 Output_section::Input_section_sort_init_fini_compare::operator()(
3420 const Output_section::Input_section_sort_entry& s1,
3421 const Output_section::Input_section_sort_entry& s2) const
3423 // We sort all the sections with no names to the end.
3424 if (!s1.section_has_name() || !s2.section_has_name())
3426 if (s1.section_has_name())
3427 return true;
3428 if (s2.section_has_name())
3429 return false;
3430 return s1.index() < s2.index();
3433 // A section without a priority follows a section with a priority.
3434 // This is the reverse of .ctors and .dtors sections.
3435 bool s1_has_priority = s1.has_priority();
3436 bool s2_has_priority = s2.has_priority();
3437 if (s1_has_priority && !s2_has_priority)
3438 return true;
3439 if (!s1_has_priority && s2_has_priority)
3440 return false;
3442 // .ctors and .dtors sections without priority come after
3443 // .init_array and .fini_array sections without priority.
3444 if (!s1_has_priority
3445 && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors")
3446 && s1.section_name() != s2.section_name())
3447 return false;
3448 if (!s2_has_priority
3449 && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors")
3450 && s2.section_name() != s1.section_name())
3451 return true;
3453 // Sort by priority if we can.
3454 if (s1_has_priority)
3456 unsigned int s1_prio = s1.get_priority();
3457 unsigned int s2_prio = s2.get_priority();
3458 if (s1_prio < s2_prio)
3459 return true;
3460 else if (s1_prio > s2_prio)
3461 return false;
3464 // Check if a section order exists for these sections through a section
3465 // ordering file. If sequence_num is 0, an order does not exist.
3466 int sequence_num = s1.compare_section_ordering(s2);
3467 if (sequence_num != 0)
3468 return sequence_num == 1;
3470 // Otherwise we sort by name.
3471 int compare = s1.section_name().compare(s2.section_name());
3472 if (compare != 0)
3473 return compare < 0;
3475 // Otherwise we keep the input order.
3476 return s1.index() < s2.index();
3479 // Return true if S1 should come before S2. Sections that do not match
3480 // any pattern in the section ordering file are placed ahead of the sections
3481 // that match some pattern.
3483 bool
3484 Output_section::Input_section_sort_section_order_index_compare::operator()(
3485 const Output_section::Input_section_sort_entry& s1,
3486 const Output_section::Input_section_sort_entry& s2) const
3488 unsigned int s1_secn_index = s1.input_section().section_order_index();
3489 unsigned int s2_secn_index = s2.input_section().section_order_index();
3491 // Keep input order if section ordering cannot determine order.
3492 if (s1_secn_index == s2_secn_index)
3493 return s1.index() < s2.index();
3495 return s1_secn_index < s2_secn_index;
3498 // This updates the section order index of input sections according to the
3499 // the order specified in the mapping from Section id to order index.
3501 void
3502 Output_section::update_section_layout(
3503 const Section_layout_order* order_map)
3505 for (Input_section_list::iterator p = this->input_sections_.begin();
3506 p != this->input_sections_.end();
3507 ++p)
3509 if (p->is_input_section()
3510 || p->is_relaxed_input_section())
3512 Object* obj = (p->is_input_section()
3513 ? p->relobj()
3514 : p->relaxed_input_section()->relobj());
3515 unsigned int shndx = p->shndx();
3516 Section_layout_order::const_iterator it
3517 = order_map->find(Section_id(obj, shndx));
3518 if (it == order_map->end())
3519 continue;
3520 unsigned int section_order_index = it->second;
3521 if (section_order_index != 0)
3523 p->set_section_order_index(section_order_index);
3524 this->set_input_section_order_specified();
3530 // Sort the input sections attached to an output section.
3532 void
3533 Output_section::sort_attached_input_sections()
3535 if (this->attached_input_sections_are_sorted_)
3536 return;
3538 if (this->checkpoint_ != NULL
3539 && !this->checkpoint_->input_sections_saved())
3540 this->checkpoint_->save_input_sections();
3542 // The only thing we know about an input section is the object and
3543 // the section index. We need the section name. Recomputing this
3544 // is slow but this is an unusual case. If this becomes a speed
3545 // problem we can cache the names as required in Layout::layout.
3547 // We start by building a larger vector holding a copy of each
3548 // Input_section, plus its current index in the list and its name.
3549 std::vector<Input_section_sort_entry> sort_list;
3551 unsigned int i = 0;
3552 for (Input_section_list::iterator p = this->input_sections_.begin();
3553 p != this->input_sections_.end();
3554 ++p, ++i)
3555 sort_list.push_back(Input_section_sort_entry(*p, i,
3556 this->must_sort_attached_input_sections()));
3558 // Sort the input sections.
3559 if (this->must_sort_attached_input_sections())
3561 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3562 || this->type() == elfcpp::SHT_INIT_ARRAY
3563 || this->type() == elfcpp::SHT_FINI_ARRAY)
3564 std::sort(sort_list.begin(), sort_list.end(),
3565 Input_section_sort_init_fini_compare());
3566 else
3567 std::sort(sort_list.begin(), sort_list.end(),
3568 Input_section_sort_compare());
3570 else
3572 gold_assert(this->input_section_order_specified());
3573 std::sort(sort_list.begin(), sort_list.end(),
3574 Input_section_sort_section_order_index_compare());
3577 // Copy the sorted input sections back to our list.
3578 this->input_sections_.clear();
3579 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3580 p != sort_list.end();
3581 ++p)
3582 this->input_sections_.push_back(p->input_section());
3583 sort_list.clear();
3585 // Remember that we sorted the input sections, since we might get
3586 // called again.
3587 this->attached_input_sections_are_sorted_ = true;
3590 // Write the section header to *OSHDR.
3592 template<int size, bool big_endian>
3593 void
3594 Output_section::write_header(const Layout* layout,
3595 const Stringpool* secnamepool,
3596 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3598 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3599 oshdr->put_sh_type(this->type_);
3601 elfcpp::Elf_Xword flags = this->flags_;
3602 if (this->info_section_ != NULL && this->info_uses_section_index_)
3603 flags |= elfcpp::SHF_INFO_LINK;
3604 oshdr->put_sh_flags(flags);
3606 oshdr->put_sh_addr(this->address());
3607 oshdr->put_sh_offset(this->offset());
3608 oshdr->put_sh_size(this->data_size());
3609 if (this->link_section_ != NULL)
3610 oshdr->put_sh_link(this->link_section_->out_shndx());
3611 else if (this->should_link_to_symtab_)
3612 oshdr->put_sh_link(layout->symtab_section_shndx());
3613 else if (this->should_link_to_dynsym_)
3614 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3615 else
3616 oshdr->put_sh_link(this->link_);
3618 elfcpp::Elf_Word info;
3619 if (this->info_section_ != NULL)
3621 if (this->info_uses_section_index_)
3622 info = this->info_section_->out_shndx();
3623 else
3624 info = this->info_section_->symtab_index();
3626 else if (this->info_symndx_ != NULL)
3627 info = this->info_symndx_->symtab_index();
3628 else
3629 info = this->info_;
3630 oshdr->put_sh_info(info);
3632 oshdr->put_sh_addralign(this->addralign_);
3633 oshdr->put_sh_entsize(this->entsize_);
3636 // Write out the data. For input sections the data is written out by
3637 // Object::relocate, but we have to handle Output_section_data objects
3638 // here.
3640 void
3641 Output_section::do_write(Output_file* of)
3643 gold_assert(!this->requires_postprocessing());
3645 // If the target performs relaxation, we delay filler generation until now.
3646 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3648 off_t output_section_file_offset = this->offset();
3649 for (Fill_list::iterator p = this->fills_.begin();
3650 p != this->fills_.end();
3651 ++p)
3653 std::string fill_data(parameters->target().code_fill(p->length()));
3654 of->write(output_section_file_offset + p->section_offset(),
3655 fill_data.data(), fill_data.size());
3658 off_t off = this->offset() + this->first_input_offset_;
3659 for (Input_section_list::iterator p = this->input_sections_.begin();
3660 p != this->input_sections_.end();
3661 ++p)
3663 off_t aligned_off = align_address(off, p->addralign());
3664 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3666 size_t fill_len = aligned_off - off;
3667 std::string fill_data(parameters->target().code_fill(fill_len));
3668 of->write(off, fill_data.data(), fill_data.size());
3671 p->write(of);
3672 off = aligned_off + p->data_size();
3675 // For incremental links, fill in unused chunks in debug sections
3676 // with dummy compilation unit headers.
3677 if (this->free_space_fill_ != NULL)
3679 for (Free_list::Const_iterator p = this->free_list_.begin();
3680 p != this->free_list_.end();
3681 ++p)
3683 off_t off = p->start_;
3684 size_t len = p->end_ - off;
3685 this->free_space_fill_->write(of, this->offset() + off, len);
3687 if (this->patch_space_ > 0)
3689 off_t off = this->current_data_size_for_child() - this->patch_space_;
3690 this->free_space_fill_->write(of, this->offset() + off,
3691 this->patch_space_);
3696 // If a section requires postprocessing, create the buffer to use.
3698 void
3699 Output_section::create_postprocessing_buffer()
3701 gold_assert(this->requires_postprocessing());
3703 if (this->postprocessing_buffer_ != NULL)
3704 return;
3706 if (!this->input_sections_.empty())
3708 off_t off = this->first_input_offset_;
3709 for (Input_section_list::iterator p = this->input_sections_.begin();
3710 p != this->input_sections_.end();
3711 ++p)
3713 off = align_address(off, p->addralign());
3714 p->finalize_data_size();
3715 off += p->data_size();
3717 this->set_current_data_size_for_child(off);
3720 off_t buffer_size = this->current_data_size_for_child();
3721 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3724 // Write all the data of an Output_section into the postprocessing
3725 // buffer. This is used for sections which require postprocessing,
3726 // such as compression. Input sections are handled by
3727 // Object::Relocate.
3729 void
3730 Output_section::write_to_postprocessing_buffer()
3732 gold_assert(this->requires_postprocessing());
3734 // If the target performs relaxation, we delay filler generation until now.
3735 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3737 unsigned char* buffer = this->postprocessing_buffer();
3738 for (Fill_list::iterator p = this->fills_.begin();
3739 p != this->fills_.end();
3740 ++p)
3742 std::string fill_data(parameters->target().code_fill(p->length()));
3743 memcpy(buffer + p->section_offset(), fill_data.data(),
3744 fill_data.size());
3747 off_t off = this->first_input_offset_;
3748 for (Input_section_list::iterator p = this->input_sections_.begin();
3749 p != this->input_sections_.end();
3750 ++p)
3752 off_t aligned_off = align_address(off, p->addralign());
3753 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3755 size_t fill_len = aligned_off - off;
3756 std::string fill_data(parameters->target().code_fill(fill_len));
3757 memcpy(buffer + off, fill_data.data(), fill_data.size());
3760 p->write_to_buffer(buffer + aligned_off);
3761 off = aligned_off + p->data_size();
3765 // Get the input sections for linker script processing. We leave
3766 // behind the Output_section_data entries. Note that this may be
3767 // slightly incorrect for merge sections. We will leave them behind,
3768 // but it is possible that the script says that they should follow
3769 // some other input sections, as in:
3770 // .rodata { *(.rodata) *(.rodata.cst*) }
3771 // For that matter, we don't handle this correctly:
3772 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3773 // With luck this will never matter.
3775 uint64_t
3776 Output_section::get_input_sections(
3777 uint64_t address,
3778 const std::string& fill,
3779 std::list<Input_section>* input_sections)
3781 if (this->checkpoint_ != NULL
3782 && !this->checkpoint_->input_sections_saved())
3783 this->checkpoint_->save_input_sections();
3785 // Invalidate fast look-up maps.
3786 this->lookup_maps_->invalidate();
3788 uint64_t orig_address = address;
3790 address = align_address(address, this->addralign());
3792 Input_section_list remaining;
3793 for (Input_section_list::iterator p = this->input_sections_.begin();
3794 p != this->input_sections_.end();
3795 ++p)
3797 if (p->is_input_section()
3798 || p->is_relaxed_input_section()
3799 || p->is_merge_section())
3800 input_sections->push_back(*p);
3801 else
3803 uint64_t aligned_address = align_address(address, p->addralign());
3804 if (aligned_address != address && !fill.empty())
3806 section_size_type length =
3807 convert_to_section_size_type(aligned_address - address);
3808 std::string this_fill;
3809 this_fill.reserve(length);
3810 while (this_fill.length() + fill.length() <= length)
3811 this_fill += fill;
3812 if (this_fill.length() < length)
3813 this_fill.append(fill, 0, length - this_fill.length());
3815 Output_section_data* posd = new Output_data_const(this_fill, 0);
3816 remaining.push_back(Input_section(posd));
3818 address = aligned_address;
3820 remaining.push_back(*p);
3822 p->finalize_data_size();
3823 address += p->data_size();
3827 this->input_sections_.swap(remaining);
3828 this->first_input_offset_ = 0;
3830 uint64_t data_size = address - orig_address;
3831 this->set_current_data_size_for_child(data_size);
3832 return data_size;
3835 // Add a script input section. SIS is an Output_section::Input_section,
3836 // which can be either a plain input section or a special input section like
3837 // a relaxed input section. For a special input section, its size must be
3838 // finalized.
3840 void
3841 Output_section::add_script_input_section(const Input_section& sis)
3843 uint64_t data_size = sis.data_size();
3844 uint64_t addralign = sis.addralign();
3845 if (addralign > this->addralign_)
3846 this->addralign_ = addralign;
3848 off_t offset_in_section = this->current_data_size_for_child();
3849 off_t aligned_offset_in_section = align_address(offset_in_section,
3850 addralign);
3852 this->set_current_data_size_for_child(aligned_offset_in_section
3853 + data_size);
3855 this->input_sections_.push_back(sis);
3857 // Update fast lookup maps if necessary.
3858 if (this->lookup_maps_->is_valid())
3860 if (sis.is_merge_section())
3862 Output_merge_base* pomb = sis.output_merge_base();
3863 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3864 pomb->addralign());
3865 this->lookup_maps_->add_merge_section(msp, pomb);
3866 for (Output_merge_base::Input_sections::const_iterator p =
3867 pomb->input_sections_begin();
3868 p != pomb->input_sections_end();
3869 ++p)
3870 this->lookup_maps_->add_merge_input_section(p->first, p->second,
3871 pomb);
3873 else if (sis.is_relaxed_input_section())
3875 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3876 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3877 poris->shndx(), poris);
3882 // Save states for relaxation.
3884 void
3885 Output_section::save_states()
3887 gold_assert(this->checkpoint_ == NULL);
3888 Checkpoint_output_section* checkpoint =
3889 new Checkpoint_output_section(this->addralign_, this->flags_,
3890 this->input_sections_,
3891 this->first_input_offset_,
3892 this->attached_input_sections_are_sorted_);
3893 this->checkpoint_ = checkpoint;
3894 gold_assert(this->fills_.empty());
3897 void
3898 Output_section::discard_states()
3900 gold_assert(this->checkpoint_ != NULL);
3901 delete this->checkpoint_;
3902 this->checkpoint_ = NULL;
3903 gold_assert(this->fills_.empty());
3905 // Simply invalidate the fast lookup maps since we do not keep
3906 // track of them.
3907 this->lookup_maps_->invalidate();
3910 void
3911 Output_section::restore_states()
3913 gold_assert(this->checkpoint_ != NULL);
3914 Checkpoint_output_section* checkpoint = this->checkpoint_;
3916 this->addralign_ = checkpoint->addralign();
3917 this->flags_ = checkpoint->flags();
3918 this->first_input_offset_ = checkpoint->first_input_offset();
3920 if (!checkpoint->input_sections_saved())
3922 // If we have not copied the input sections, just resize it.
3923 size_t old_size = checkpoint->input_sections_size();
3924 gold_assert(this->input_sections_.size() >= old_size);
3925 this->input_sections_.resize(old_size);
3927 else
3929 // We need to copy the whole list. This is not efficient for
3930 // extremely large output with hundreads of thousands of input
3931 // objects. We may need to re-think how we should pass sections
3932 // to scripts.
3933 this->input_sections_ = *checkpoint->input_sections();
3936 this->attached_input_sections_are_sorted_ =
3937 checkpoint->attached_input_sections_are_sorted();
3939 // Simply invalidate the fast lookup maps since we do not keep
3940 // track of them.
3941 this->lookup_maps_->invalidate();
3944 // Update the section offsets of input sections in this. This is required if
3945 // relaxation causes some input sections to change sizes.
3947 void
3948 Output_section::adjust_section_offsets()
3950 if (!this->section_offsets_need_adjustment_)
3951 return;
3953 off_t off = 0;
3954 for (Input_section_list::iterator p = this->input_sections_.begin();
3955 p != this->input_sections_.end();
3956 ++p)
3958 off = align_address(off, p->addralign());
3959 if (p->is_input_section())
3960 p->relobj()->set_section_offset(p->shndx(), off);
3961 off += p->data_size();
3964 this->section_offsets_need_adjustment_ = false;
3967 // Print to the map file.
3969 void
3970 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3972 mapfile->print_output_section(this);
3974 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3975 p != this->input_sections_.end();
3976 ++p)
3977 p->print_to_mapfile(mapfile);
3980 // Print stats for merge sections to stderr.
3982 void
3983 Output_section::print_merge_stats()
3985 Input_section_list::iterator p;
3986 for (p = this->input_sections_.begin();
3987 p != this->input_sections_.end();
3988 ++p)
3989 p->print_merge_stats(this->name_);
3992 // Set a fixed layout for the section. Used for incremental update links.
3994 void
3995 Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3996 off_t sh_size, uint64_t sh_addralign)
3998 this->addralign_ = sh_addralign;
3999 this->set_current_data_size(sh_size);
4000 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
4001 this->set_address(sh_addr);
4002 this->set_file_offset(sh_offset);
4003 this->finalize_data_size();
4004 this->free_list_.init(sh_size, false);
4005 this->has_fixed_layout_ = true;
4008 // Reserve space within the fixed layout for the section. Used for
4009 // incremental update links.
4011 void
4012 Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
4014 this->free_list_.remove(sh_offset, sh_offset + sh_size);
4017 // Allocate space from the free list for the section. Used for
4018 // incremental update links.
4020 off_t
4021 Output_section::allocate(off_t len, uint64_t addralign)
4023 return this->free_list_.allocate(len, addralign, 0);
4026 // Output segment methods.
4028 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
4029 : vaddr_(0),
4030 paddr_(0),
4031 memsz_(0),
4032 max_align_(0),
4033 min_p_align_(0),
4034 offset_(0),
4035 filesz_(0),
4036 type_(type),
4037 flags_(flags),
4038 is_max_align_known_(false),
4039 are_addresses_set_(false),
4040 is_large_data_segment_(false)
4042 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
4043 // the flags.
4044 if (type == elfcpp::PT_TLS)
4045 this->flags_ = elfcpp::PF_R;
4048 // Add an Output_section to a PT_LOAD Output_segment.
4050 void
4051 Output_segment::add_output_section_to_load(Layout* layout,
4052 Output_section* os,
4053 elfcpp::Elf_Word seg_flags)
4055 gold_assert(this->type() == elfcpp::PT_LOAD);
4056 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4057 gold_assert(!this->is_max_align_known_);
4058 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
4060 this->update_flags_for_output_section(seg_flags);
4062 // We don't want to change the ordering if we have a linker script
4063 // with a SECTIONS clause.
4064 Output_section_order order = os->order();
4065 if (layout->script_options()->saw_sections_clause())
4066 order = static_cast<Output_section_order>(0);
4067 else
4068 gold_assert(order != ORDER_INVALID);
4070 this->output_lists_[order].push_back(os);
4073 // Add an Output_section to a non-PT_LOAD Output_segment.
4075 void
4076 Output_segment::add_output_section_to_nonload(Output_section* os,
4077 elfcpp::Elf_Word seg_flags)
4079 gold_assert(this->type() != elfcpp::PT_LOAD);
4080 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4081 gold_assert(!this->is_max_align_known_);
4083 this->update_flags_for_output_section(seg_flags);
4085 this->output_lists_[0].push_back(os);
4088 // Remove an Output_section from this segment. It is an error if it
4089 // is not present.
4091 void
4092 Output_segment::remove_output_section(Output_section* os)
4094 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4096 Output_data_list* pdl = &this->output_lists_[i];
4097 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
4099 if (*p == os)
4101 pdl->erase(p);
4102 return;
4106 gold_unreachable();
4109 // Add an Output_data (which need not be an Output_section) to the
4110 // start of a segment.
4112 void
4113 Output_segment::add_initial_output_data(Output_data* od)
4115 gold_assert(!this->is_max_align_known_);
4116 Output_data_list::iterator p = this->output_lists_[0].begin();
4117 this->output_lists_[0].insert(p, od);
4120 // Return true if this segment has any sections which hold actual
4121 // data, rather than being a BSS section.
4123 bool
4124 Output_segment::has_any_data_sections() const
4126 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4128 const Output_data_list* pdl = &this->output_lists_[i];
4129 for (Output_data_list::const_iterator p = pdl->begin();
4130 p != pdl->end();
4131 ++p)
4133 if (!(*p)->is_section())
4134 return true;
4135 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
4136 return true;
4139 return false;
4142 // Return whether the first data section (not counting TLS sections)
4143 // is a relro section.
4145 bool
4146 Output_segment::is_first_section_relro() const
4148 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4150 if (i == static_cast<int>(ORDER_TLS_DATA)
4151 || i == static_cast<int>(ORDER_TLS_BSS))
4152 continue;
4153 const Output_data_list* pdl = &this->output_lists_[i];
4154 if (!pdl->empty())
4156 Output_data* p = pdl->front();
4157 return p->is_section() && p->output_section()->is_relro();
4160 return false;
4163 // Return the maximum alignment of the Output_data in Output_segment.
4165 uint64_t
4166 Output_segment::maximum_alignment()
4168 if (!this->is_max_align_known_)
4170 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4172 const Output_data_list* pdl = &this->output_lists_[i];
4173 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
4174 if (addralign > this->max_align_)
4175 this->max_align_ = addralign;
4177 this->is_max_align_known_ = true;
4180 return this->max_align_;
4183 // Return the maximum alignment of a list of Output_data.
4185 uint64_t
4186 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
4188 uint64_t ret = 0;
4189 for (Output_data_list::const_iterator p = pdl->begin();
4190 p != pdl->end();
4191 ++p)
4193 uint64_t addralign = (*p)->addralign();
4194 if (addralign > ret)
4195 ret = addralign;
4197 return ret;
4200 // Return whether this segment has any dynamic relocs.
4202 bool
4203 Output_segment::has_dynamic_reloc() const
4205 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4206 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
4207 return true;
4208 return false;
4211 // Return whether this Output_data_list has any dynamic relocs.
4213 bool
4214 Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
4216 for (Output_data_list::const_iterator p = pdl->begin();
4217 p != pdl->end();
4218 ++p)
4219 if ((*p)->has_dynamic_reloc())
4220 return true;
4221 return false;
4224 // Set the section addresses for an Output_segment. If RESET is true,
4225 // reset the addresses first. ADDR is the address and *POFF is the
4226 // file offset. Set the section indexes starting with *PSHNDX.
4227 // INCREASE_RELRO is the size of the portion of the first non-relro
4228 // section that should be included in the PT_GNU_RELRO segment.
4229 // If this segment has relro sections, and has been aligned for
4230 // that purpose, set *HAS_RELRO to TRUE. Return the address of
4231 // the immediately following segment. Update *HAS_RELRO, *POFF,
4232 // and *PSHNDX.
4234 uint64_t
4235 Output_segment::set_section_addresses(Layout* layout, bool reset,
4236 uint64_t addr,
4237 unsigned int* increase_relro,
4238 bool* has_relro,
4239 off_t* poff,
4240 unsigned int* pshndx)
4242 gold_assert(this->type_ == elfcpp::PT_LOAD);
4244 uint64_t last_relro_pad = 0;
4245 off_t orig_off = *poff;
4247 bool in_tls = false;
4249 // If we have relro sections, we need to pad forward now so that the
4250 // relro sections plus INCREASE_RELRO end on a common page boundary.
4251 if (parameters->options().relro()
4252 && this->is_first_section_relro()
4253 && (!this->are_addresses_set_ || reset))
4255 uint64_t relro_size = 0;
4256 off_t off = *poff;
4257 uint64_t max_align = 0;
4258 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
4260 Output_data_list* pdl = &this->output_lists_[i];
4261 Output_data_list::iterator p;
4262 for (p = pdl->begin(); p != pdl->end(); ++p)
4264 if (!(*p)->is_section())
4265 break;
4266 uint64_t align = (*p)->addralign();
4267 if (align > max_align)
4268 max_align = align;
4269 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4270 in_tls = true;
4271 else if (in_tls)
4273 // Align the first non-TLS section to the alignment
4274 // of the TLS segment.
4275 align = max_align;
4276 in_tls = false;
4278 relro_size = align_address(relro_size, align);
4279 // Ignore the size of the .tbss section.
4280 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
4281 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
4282 continue;
4283 if ((*p)->is_address_valid())
4284 relro_size += (*p)->data_size();
4285 else
4287 // FIXME: This could be faster.
4288 (*p)->set_address_and_file_offset(addr + relro_size,
4289 off + relro_size);
4290 relro_size += (*p)->data_size();
4291 (*p)->reset_address_and_file_offset();
4294 if (p != pdl->end())
4295 break;
4297 relro_size += *increase_relro;
4298 // Pad the total relro size to a multiple of the maximum
4299 // section alignment seen.
4300 uint64_t aligned_size = align_address(relro_size, max_align);
4301 // Note the amount of padding added after the last relro section.
4302 last_relro_pad = aligned_size - relro_size;
4303 *has_relro = true;
4305 uint64_t page_align = parameters->target().common_pagesize();
4307 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4308 uint64_t desired_align = page_align - (aligned_size % page_align);
4309 if (desired_align < *poff % page_align)
4310 *poff += page_align - *poff % page_align;
4311 *poff += desired_align - *poff % page_align;
4312 addr += *poff - orig_off;
4313 orig_off = *poff;
4316 if (!reset && this->are_addresses_set_)
4318 gold_assert(this->paddr_ == addr);
4319 addr = this->vaddr_;
4321 else
4323 this->vaddr_ = addr;
4324 this->paddr_ = addr;
4325 this->are_addresses_set_ = true;
4328 in_tls = false;
4330 this->offset_ = orig_off;
4332 off_t off = 0;
4333 uint64_t ret;
4334 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4336 if (i == static_cast<int>(ORDER_RELRO_LAST))
4338 *poff += last_relro_pad;
4339 addr += last_relro_pad;
4340 if (this->output_lists_[i].empty())
4342 // If there is nothing in the ORDER_RELRO_LAST list,
4343 // the padding will occur at the end of the relro
4344 // segment, and we need to add it to *INCREASE_RELRO.
4345 *increase_relro += last_relro_pad;
4348 addr = this->set_section_list_addresses(layout, reset,
4349 &this->output_lists_[i],
4350 addr, poff, pshndx, &in_tls);
4351 if (i < static_cast<int>(ORDER_SMALL_BSS))
4353 this->filesz_ = *poff - orig_off;
4354 off = *poff;
4357 ret = addr;
4360 // If the last section was a TLS section, align upward to the
4361 // alignment of the TLS segment, so that the overall size of the TLS
4362 // segment is aligned.
4363 if (in_tls)
4365 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4366 *poff = align_address(*poff, segment_align);
4369 this->memsz_ = *poff - orig_off;
4371 // Ignore the file offset adjustments made by the BSS Output_data
4372 // objects.
4373 *poff = off;
4375 return ret;
4378 // Set the addresses and file offsets in a list of Output_data
4379 // structures.
4381 uint64_t
4382 Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4383 Output_data_list* pdl,
4384 uint64_t addr, off_t* poff,
4385 unsigned int* pshndx,
4386 bool* in_tls)
4388 off_t startoff = *poff;
4389 // For incremental updates, we may allocate non-fixed sections from
4390 // free space in the file. This keeps track of the high-water mark.
4391 off_t maxoff = startoff;
4393 off_t off = startoff;
4394 for (Output_data_list::iterator p = pdl->begin();
4395 p != pdl->end();
4396 ++p)
4398 if (reset)
4399 (*p)->reset_address_and_file_offset();
4401 // When doing an incremental update or when using a linker script,
4402 // the section will most likely already have an address.
4403 if (!(*p)->is_address_valid())
4405 uint64_t align = (*p)->addralign();
4407 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4409 // Give the first TLS section the alignment of the
4410 // entire TLS segment. Otherwise the TLS segment as a
4411 // whole may be misaligned.
4412 if (!*in_tls)
4414 Output_segment* tls_segment = layout->tls_segment();
4415 gold_assert(tls_segment != NULL);
4416 uint64_t segment_align = tls_segment->maximum_alignment();
4417 gold_assert(segment_align >= align);
4418 align = segment_align;
4420 *in_tls = true;
4423 else
4425 // If this is the first section after the TLS segment,
4426 // align it to at least the alignment of the TLS
4427 // segment, so that the size of the overall TLS segment
4428 // is aligned.
4429 if (*in_tls)
4431 uint64_t segment_align =
4432 layout->tls_segment()->maximum_alignment();
4433 if (segment_align > align)
4434 align = segment_align;
4436 *in_tls = false;
4440 if (!parameters->incremental_update())
4442 off = align_address(off, align);
4443 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4445 else
4447 // Incremental update: allocate file space from free list.
4448 (*p)->pre_finalize_data_size();
4449 off_t current_size = (*p)->current_data_size();
4450 off = layout->allocate(current_size, align, startoff);
4451 if (off == -1)
4453 gold_assert((*p)->output_section() != NULL);
4454 gold_fallback(_("out of patch space for section %s; "
4455 "relink with --incremental-full"),
4456 (*p)->output_section()->name());
4458 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4459 if ((*p)->data_size() > current_size)
4461 gold_assert((*p)->output_section() != NULL);
4462 gold_fallback(_("%s: section changed size; "
4463 "relink with --incremental-full"),
4464 (*p)->output_section()->name());
4468 else if (parameters->incremental_update())
4470 // For incremental updates, use the fixed offset for the
4471 // high-water mark computation.
4472 off = (*p)->offset();
4474 else
4476 // The script may have inserted a skip forward, but it
4477 // better not have moved backward.
4478 if ((*p)->address() >= addr + (off - startoff))
4479 off += (*p)->address() - (addr + (off - startoff));
4480 else
4482 if (!layout->script_options()->saw_sections_clause())
4483 gold_unreachable();
4484 else
4486 Output_section* os = (*p)->output_section();
4488 // Cast to unsigned long long to avoid format warnings.
4489 unsigned long long previous_dot =
4490 static_cast<unsigned long long>(addr + (off - startoff));
4491 unsigned long long dot =
4492 static_cast<unsigned long long>((*p)->address());
4494 if (os == NULL)
4495 gold_error(_("dot moves backward in linker script "
4496 "from 0x%llx to 0x%llx"), previous_dot, dot);
4497 else
4498 gold_error(_("address of section '%s' moves backward "
4499 "from 0x%llx to 0x%llx"),
4500 os->name(), previous_dot, dot);
4503 (*p)->set_file_offset(off);
4504 (*p)->finalize_data_size();
4507 if (parameters->incremental_update())
4508 gold_debug(DEBUG_INCREMENTAL,
4509 "set_section_list_addresses: %08lx %08lx %s",
4510 static_cast<long>(off),
4511 static_cast<long>((*p)->data_size()),
4512 ((*p)->output_section() != NULL
4513 ? (*p)->output_section()->name() : "(special)"));
4515 // We want to ignore the size of a SHF_TLS SHT_NOBITS
4516 // section. Such a section does not affect the size of a
4517 // PT_LOAD segment.
4518 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
4519 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
4520 off += (*p)->data_size();
4522 if (off > maxoff)
4523 maxoff = off;
4525 if ((*p)->is_section())
4527 (*p)->set_out_shndx(*pshndx);
4528 ++*pshndx;
4532 *poff = maxoff;
4533 return addr + (maxoff - startoff);
4536 // For a non-PT_LOAD segment, set the offset from the sections, if
4537 // any. Add INCREASE to the file size and the memory size.
4539 void
4540 Output_segment::set_offset(unsigned int increase)
4542 gold_assert(this->type_ != elfcpp::PT_LOAD);
4544 gold_assert(!this->are_addresses_set_);
4546 // A non-load section only uses output_lists_[0].
4548 Output_data_list* pdl = &this->output_lists_[0];
4550 if (pdl->empty())
4552 gold_assert(increase == 0);
4553 this->vaddr_ = 0;
4554 this->paddr_ = 0;
4555 this->are_addresses_set_ = true;
4556 this->memsz_ = 0;
4557 this->min_p_align_ = 0;
4558 this->offset_ = 0;
4559 this->filesz_ = 0;
4560 return;
4563 // Find the first and last section by address.
4564 const Output_data* first = NULL;
4565 const Output_data* last_data = NULL;
4566 const Output_data* last_bss = NULL;
4567 for (Output_data_list::const_iterator p = pdl->begin();
4568 p != pdl->end();
4569 ++p)
4571 if (first == NULL
4572 || (*p)->address() < first->address()
4573 || ((*p)->address() == first->address()
4574 && (*p)->data_size() < first->data_size()))
4575 first = *p;
4576 const Output_data** plast;
4577 if ((*p)->is_section()
4578 && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4579 plast = &last_bss;
4580 else
4581 plast = &last_data;
4582 if (*plast == NULL
4583 || (*p)->address() > (*plast)->address()
4584 || ((*p)->address() == (*plast)->address()
4585 && (*p)->data_size() > (*plast)->data_size()))
4586 *plast = *p;
4589 this->vaddr_ = first->address();
4590 this->paddr_ = (first->has_load_address()
4591 ? first->load_address()
4592 : this->vaddr_);
4593 this->are_addresses_set_ = true;
4594 this->offset_ = first->offset();
4596 if (last_data == NULL)
4597 this->filesz_ = 0;
4598 else
4599 this->filesz_ = (last_data->address()
4600 + last_data->data_size()
4601 - this->vaddr_);
4603 const Output_data* last = last_bss != NULL ? last_bss : last_data;
4604 this->memsz_ = (last->address()
4605 + last->data_size()
4606 - this->vaddr_);
4608 this->filesz_ += increase;
4609 this->memsz_ += increase;
4611 // If this is a RELRO segment, verify that the segment ends at a
4612 // page boundary.
4613 if (this->type_ == elfcpp::PT_GNU_RELRO)
4615 uint64_t page_align = parameters->target().common_pagesize();
4616 uint64_t segment_end = this->vaddr_ + this->memsz_;
4617 if (parameters->incremental_update())
4619 // The INCREASE_RELRO calculation is bypassed for an incremental
4620 // update, so we need to adjust the segment size manually here.
4621 segment_end = align_address(segment_end, page_align);
4622 this->memsz_ = segment_end - this->vaddr_;
4624 else
4625 gold_assert(segment_end == align_address(segment_end, page_align));
4628 // If this is a TLS segment, align the memory size. The code in
4629 // set_section_list ensures that the section after the TLS segment
4630 // is aligned to give us room.
4631 if (this->type_ == elfcpp::PT_TLS)
4633 uint64_t segment_align = this->maximum_alignment();
4634 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4635 this->memsz_ = align_address(this->memsz_, segment_align);
4639 // Set the TLS offsets of the sections in the PT_TLS segment.
4641 void
4642 Output_segment::set_tls_offsets()
4644 gold_assert(this->type_ == elfcpp::PT_TLS);
4646 for (Output_data_list::iterator p = this->output_lists_[0].begin();
4647 p != this->output_lists_[0].end();
4648 ++p)
4649 (*p)->set_tls_offset(this->vaddr_);
4652 // Return the load address of the first section.
4654 uint64_t
4655 Output_segment::first_section_load_address() const
4657 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4659 const Output_data_list* pdl = &this->output_lists_[i];
4660 for (Output_data_list::const_iterator p = pdl->begin();
4661 p != pdl->end();
4662 ++p)
4664 if ((*p)->is_section())
4665 return ((*p)->has_load_address()
4666 ? (*p)->load_address()
4667 : (*p)->address());
4670 gold_unreachable();
4673 // Return the number of Output_sections in an Output_segment.
4675 unsigned int
4676 Output_segment::output_section_count() const
4678 unsigned int ret = 0;
4679 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4680 ret += this->output_section_count_list(&this->output_lists_[i]);
4681 return ret;
4684 // Return the number of Output_sections in an Output_data_list.
4686 unsigned int
4687 Output_segment::output_section_count_list(const Output_data_list* pdl) const
4689 unsigned int count = 0;
4690 for (Output_data_list::const_iterator p = pdl->begin();
4691 p != pdl->end();
4692 ++p)
4694 if ((*p)->is_section())
4695 ++count;
4697 return count;
4700 // Return the section attached to the list segment with the lowest
4701 // load address. This is used when handling a PHDRS clause in a
4702 // linker script.
4704 Output_section*
4705 Output_segment::section_with_lowest_load_address() const
4707 Output_section* found = NULL;
4708 uint64_t found_lma = 0;
4709 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4710 this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4711 &found_lma);
4712 return found;
4715 // Look through a list for a section with a lower load address.
4717 void
4718 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4719 Output_section** found,
4720 uint64_t* found_lma) const
4722 for (Output_data_list::const_iterator p = pdl->begin();
4723 p != pdl->end();
4724 ++p)
4726 if (!(*p)->is_section())
4727 continue;
4728 Output_section* os = static_cast<Output_section*>(*p);
4729 uint64_t lma = (os->has_load_address()
4730 ? os->load_address()
4731 : os->address());
4732 if (*found == NULL || lma < *found_lma)
4734 *found = os;
4735 *found_lma = lma;
4740 // Write the segment data into *OPHDR.
4742 template<int size, bool big_endian>
4743 void
4744 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4746 ophdr->put_p_type(this->type_);
4747 ophdr->put_p_offset(this->offset_);
4748 ophdr->put_p_vaddr(this->vaddr_);
4749 ophdr->put_p_paddr(this->paddr_);
4750 ophdr->put_p_filesz(this->filesz_);
4751 ophdr->put_p_memsz(this->memsz_);
4752 ophdr->put_p_flags(this->flags_);
4753 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4756 // Write the section headers into V.
4758 template<int size, bool big_endian>
4759 unsigned char*
4760 Output_segment::write_section_headers(const Layout* layout,
4761 const Stringpool* secnamepool,
4762 unsigned char* v,
4763 unsigned int* pshndx) const
4765 // Every section that is attached to a segment must be attached to a
4766 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4767 // segments.
4768 if (this->type_ != elfcpp::PT_LOAD)
4769 return v;
4771 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4773 const Output_data_list* pdl = &this->output_lists_[i];
4774 v = this->write_section_headers_list<size, big_endian>(layout,
4775 secnamepool,
4776 pdl,
4777 v, pshndx);
4780 return v;
4783 template<int size, bool big_endian>
4784 unsigned char*
4785 Output_segment::write_section_headers_list(const Layout* layout,
4786 const Stringpool* secnamepool,
4787 const Output_data_list* pdl,
4788 unsigned char* v,
4789 unsigned int* pshndx) const
4791 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4792 for (Output_data_list::const_iterator p = pdl->begin();
4793 p != pdl->end();
4794 ++p)
4796 if ((*p)->is_section())
4798 const Output_section* ps = static_cast<const Output_section*>(*p);
4799 gold_assert(*pshndx == ps->out_shndx());
4800 elfcpp::Shdr_write<size, big_endian> oshdr(v);
4801 ps->write_header(layout, secnamepool, &oshdr);
4802 v += shdr_size;
4803 ++*pshndx;
4806 return v;
4809 // Print the output sections to the map file.
4811 void
4812 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4814 if (this->type() != elfcpp::PT_LOAD)
4815 return;
4816 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4817 this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4820 // Print an output section list to the map file.
4822 void
4823 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4824 const Output_data_list* pdl) const
4826 for (Output_data_list::const_iterator p = pdl->begin();
4827 p != pdl->end();
4828 ++p)
4829 (*p)->print_to_mapfile(mapfile);
4832 // Output_file methods.
4834 Output_file::Output_file(const char* name)
4835 : name_(name),
4836 o_(-1),
4837 file_size_(0),
4838 base_(NULL),
4839 map_is_anonymous_(false),
4840 map_is_allocated_(false),
4841 is_temporary_(false)
4845 // Try to open an existing file. Returns false if the file doesn't
4846 // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
4847 // NULL, open that file as the base for incremental linking, and
4848 // copy its contents to the new output file. This routine can
4849 // be called for incremental updates, in which case WRITABLE should
4850 // be true, or by the incremental-dump utility, in which case
4851 // WRITABLE should be false.
4853 bool
4854 Output_file::open_base_file(const char* base_name, bool writable)
4856 // The name "-" means "stdout".
4857 if (strcmp(this->name_, "-") == 0)
4858 return false;
4860 bool use_base_file = base_name != NULL;
4861 if (!use_base_file)
4862 base_name = this->name_;
4863 else if (strcmp(base_name, this->name_) == 0)
4864 gold_fatal(_("%s: incremental base and output file name are the same"),
4865 base_name);
4867 // Don't bother opening files with a size of zero.
4868 struct stat s;
4869 if (::stat(base_name, &s) != 0)
4871 gold_info(_("%s: stat: %s"), base_name, strerror(errno));
4872 return false;
4874 if (s.st_size == 0)
4876 gold_info(_("%s: incremental base file is empty"), base_name);
4877 return false;
4880 // If we're using a base file, we want to open it read-only.
4881 if (use_base_file)
4882 writable = false;
4884 int oflags = writable ? O_RDWR : O_RDONLY;
4885 int o = open_descriptor(-1, base_name, oflags, 0);
4886 if (o < 0)
4888 gold_info(_("%s: open: %s"), base_name, strerror(errno));
4889 return false;
4892 // If the base file and the output file are different, open a
4893 // new output file and read the contents from the base file into
4894 // the newly-mapped region.
4895 if (use_base_file)
4897 this->open(s.st_size);
4898 ssize_t bytes_to_read = s.st_size;
4899 unsigned char* p = this->base_;
4900 while (bytes_to_read > 0)
4902 ssize_t len = ::read(o, p, bytes_to_read);
4903 if (len < 0)
4905 gold_info(_("%s: read failed: %s"), base_name, strerror(errno));
4906 return false;
4908 if (len == 0)
4910 gold_info(_("%s: file too short: read only %lld of %lld bytes"),
4911 base_name,
4912 static_cast<long long>(s.st_size - bytes_to_read),
4913 static_cast<long long>(s.st_size));
4914 return false;
4916 p += len;
4917 bytes_to_read -= len;
4919 ::close(o);
4920 return true;
4923 this->o_ = o;
4924 this->file_size_ = s.st_size;
4926 if (!this->map_no_anonymous(writable))
4928 release_descriptor(o, true);
4929 this->o_ = -1;
4930 this->file_size_ = 0;
4931 return false;
4934 return true;
4937 // Open the output file.
4939 void
4940 Output_file::open(off_t file_size)
4942 this->file_size_ = file_size;
4944 // Unlink the file first; otherwise the open() may fail if the file
4945 // is busy (e.g. it's an executable that's currently being executed).
4947 // However, the linker may be part of a system where a zero-length
4948 // file is created for it to write to, with tight permissions (gcc
4949 // 2.95 did something like this). Unlinking the file would work
4950 // around those permission controls, so we only unlink if the file
4951 // has a non-zero size. We also unlink only regular files to avoid
4952 // trouble with directories/etc.
4954 // If we fail, continue; this command is merely a best-effort attempt
4955 // to improve the odds for open().
4957 // We let the name "-" mean "stdout"
4958 if (!this->is_temporary_)
4960 if (strcmp(this->name_, "-") == 0)
4961 this->o_ = STDOUT_FILENO;
4962 else
4964 struct stat s;
4965 if (::stat(this->name_, &s) == 0
4966 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4968 if (s.st_size != 0)
4969 ::unlink(this->name_);
4970 else if (!parameters->options().relocatable())
4972 // If we don't unlink the existing file, add execute
4973 // permission where read permissions already exist
4974 // and where the umask permits.
4975 int mask = ::umask(0);
4976 ::umask(mask);
4977 s.st_mode |= (s.st_mode & 0444) >> 2;
4978 ::chmod(this->name_, s.st_mode & ~mask);
4982 int mode = parameters->options().relocatable() ? 0666 : 0777;
4983 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4984 mode);
4985 if (o < 0)
4986 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4987 this->o_ = o;
4991 this->map();
4994 // Resize the output file.
4996 void
4997 Output_file::resize(off_t file_size)
4999 // If the mmap is mapping an anonymous memory buffer, this is easy:
5000 // just mremap to the new size. If it's mapping to a file, we want
5001 // to unmap to flush to the file, then remap after growing the file.
5002 if (this->map_is_anonymous_)
5004 void* base;
5005 if (!this->map_is_allocated_)
5007 base = ::mremap(this->base_, this->file_size_, file_size,
5008 MREMAP_MAYMOVE);
5009 if (base == MAP_FAILED)
5010 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
5012 else
5014 base = realloc(this->base_, file_size);
5015 if (base == NULL)
5016 gold_nomem();
5017 if (file_size > this->file_size_)
5018 memset(static_cast<char*>(base) + this->file_size_, 0,
5019 file_size - this->file_size_);
5021 this->base_ = static_cast<unsigned char*>(base);
5022 this->file_size_ = file_size;
5024 else
5026 this->unmap();
5027 this->file_size_ = file_size;
5028 if (!this->map_no_anonymous(true))
5029 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
5033 // Map an anonymous block of memory which will later be written to the
5034 // file. Return whether the map succeeded.
5036 bool
5037 Output_file::map_anonymous()
5039 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
5040 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
5041 if (base == MAP_FAILED)
5043 base = malloc(this->file_size_);
5044 if (base == NULL)
5045 return false;
5046 memset(base, 0, this->file_size_);
5047 this->map_is_allocated_ = true;
5049 this->base_ = static_cast<unsigned char*>(base);
5050 this->map_is_anonymous_ = true;
5051 return true;
5054 // Map the file into memory. Return whether the mapping succeeded.
5055 // If WRITABLE is true, map with write access.
5057 bool
5058 Output_file::map_no_anonymous(bool writable)
5060 const int o = this->o_;
5062 // If the output file is not a regular file, don't try to mmap it;
5063 // instead, we'll mmap a block of memory (an anonymous buffer), and
5064 // then later write the buffer to the file.
5065 void* base;
5066 struct stat statbuf;
5067 if (o == STDOUT_FILENO || o == STDERR_FILENO
5068 || ::fstat(o, &statbuf) != 0
5069 || !S_ISREG(statbuf.st_mode)
5070 || this->is_temporary_)
5071 return false;
5073 // Ensure that we have disk space available for the file. If we
5074 // don't do this, it is possible that we will call munmap, close,
5075 // and exit with dirty buffers still in the cache with no assigned
5076 // disk blocks. If the disk is out of space at that point, the
5077 // output file will wind up incomplete, but we will have already
5078 // exited. The alternative to fallocate would be to use fdatasync,
5079 // but that would be a more significant performance hit.
5080 if (writable)
5082 int err = ::posix_fallocate(o, 0, this->file_size_);
5083 if (err != 0)
5084 gold_fatal(_("%s: %s"), this->name_, strerror(err));
5087 // Map the file into memory.
5088 int prot = PROT_READ;
5089 if (writable)
5090 prot |= PROT_WRITE;
5091 base = ::mmap(NULL, this->file_size_, prot, MAP_SHARED, o, 0);
5093 // The mmap call might fail because of file system issues: the file
5094 // system might not support mmap at all, or it might not support
5095 // mmap with PROT_WRITE.
5096 if (base == MAP_FAILED)
5097 return false;
5099 this->map_is_anonymous_ = false;
5100 this->base_ = static_cast<unsigned char*>(base);
5101 return true;
5104 // Map the file into memory.
5106 void
5107 Output_file::map()
5109 if (this->map_no_anonymous(true))
5110 return;
5112 // The mmap call might fail because of file system issues: the file
5113 // system might not support mmap at all, or it might not support
5114 // mmap with PROT_WRITE. I'm not sure which errno values we will
5115 // see in all cases, so if the mmap fails for any reason and we
5116 // don't care about file contents, try for an anonymous map.
5117 if (this->map_anonymous())
5118 return;
5120 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
5121 this->name_, static_cast<unsigned long>(this->file_size_),
5122 strerror(errno));
5125 // Unmap the file from memory.
5127 void
5128 Output_file::unmap()
5130 if (this->map_is_anonymous_)
5132 // We've already written out the data, so there is no reason to
5133 // waste time unmapping or freeing the memory.
5135 else
5137 if (::munmap(this->base_, this->file_size_) < 0)
5138 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
5140 this->base_ = NULL;
5143 // Close the output file.
5145 void
5146 Output_file::close()
5148 // If the map isn't file-backed, we need to write it now.
5149 if (this->map_is_anonymous_ && !this->is_temporary_)
5151 size_t bytes_to_write = this->file_size_;
5152 size_t offset = 0;
5153 while (bytes_to_write > 0)
5155 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
5156 bytes_to_write);
5157 if (bytes_written == 0)
5158 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
5159 else if (bytes_written < 0)
5160 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
5161 else
5163 bytes_to_write -= bytes_written;
5164 offset += bytes_written;
5168 this->unmap();
5170 // We don't close stdout or stderr
5171 if (this->o_ != STDOUT_FILENO
5172 && this->o_ != STDERR_FILENO
5173 && !this->is_temporary_)
5174 if (::close(this->o_) < 0)
5175 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
5176 this->o_ = -1;
5179 // Instantiate the templates we need. We could use the configure
5180 // script to restrict this to only the ones for implemented targets.
5182 #ifdef HAVE_TARGET_32_LITTLE
5183 template
5184 off_t
5185 Output_section::add_input_section<32, false>(
5186 Layout* layout,
5187 Sized_relobj_file<32, false>* object,
5188 unsigned int shndx,
5189 const char* secname,
5190 const elfcpp::Shdr<32, false>& shdr,
5191 unsigned int reloc_shndx,
5192 bool have_sections_script);
5193 #endif
5195 #ifdef HAVE_TARGET_32_BIG
5196 template
5197 off_t
5198 Output_section::add_input_section<32, true>(
5199 Layout* layout,
5200 Sized_relobj_file<32, true>* object,
5201 unsigned int shndx,
5202 const char* secname,
5203 const elfcpp::Shdr<32, true>& shdr,
5204 unsigned int reloc_shndx,
5205 bool have_sections_script);
5206 #endif
5208 #ifdef HAVE_TARGET_64_LITTLE
5209 template
5210 off_t
5211 Output_section::add_input_section<64, false>(
5212 Layout* layout,
5213 Sized_relobj_file<64, false>* object,
5214 unsigned int shndx,
5215 const char* secname,
5216 const elfcpp::Shdr<64, false>& shdr,
5217 unsigned int reloc_shndx,
5218 bool have_sections_script);
5219 #endif
5221 #ifdef HAVE_TARGET_64_BIG
5222 template
5223 off_t
5224 Output_section::add_input_section<64, true>(
5225 Layout* layout,
5226 Sized_relobj_file<64, true>* object,
5227 unsigned int shndx,
5228 const char* secname,
5229 const elfcpp::Shdr<64, true>& shdr,
5230 unsigned int reloc_shndx,
5231 bool have_sections_script);
5232 #endif
5234 #ifdef HAVE_TARGET_32_LITTLE
5235 template
5236 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
5237 #endif
5239 #ifdef HAVE_TARGET_32_BIG
5240 template
5241 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
5242 #endif
5244 #ifdef HAVE_TARGET_64_LITTLE
5245 template
5246 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
5247 #endif
5249 #ifdef HAVE_TARGET_64_BIG
5250 template
5251 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
5252 #endif
5254 #ifdef HAVE_TARGET_32_LITTLE
5255 template
5256 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
5257 #endif
5259 #ifdef HAVE_TARGET_32_BIG
5260 template
5261 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
5262 #endif
5264 #ifdef HAVE_TARGET_64_LITTLE
5265 template
5266 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
5267 #endif
5269 #ifdef HAVE_TARGET_64_BIG
5270 template
5271 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
5272 #endif
5274 #ifdef HAVE_TARGET_32_LITTLE
5275 template
5276 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
5277 #endif
5279 #ifdef HAVE_TARGET_32_BIG
5280 template
5281 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
5282 #endif
5284 #ifdef HAVE_TARGET_64_LITTLE
5285 template
5286 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
5287 #endif
5289 #ifdef HAVE_TARGET_64_BIG
5290 template
5291 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
5292 #endif
5294 #ifdef HAVE_TARGET_32_LITTLE
5295 template
5296 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
5297 #endif
5299 #ifdef HAVE_TARGET_32_BIG
5300 template
5301 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
5302 #endif
5304 #ifdef HAVE_TARGET_64_LITTLE
5305 template
5306 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
5307 #endif
5309 #ifdef HAVE_TARGET_64_BIG
5310 template
5311 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
5312 #endif
5314 #ifdef HAVE_TARGET_32_LITTLE
5315 template
5316 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
5317 #endif
5319 #ifdef HAVE_TARGET_32_BIG
5320 template
5321 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
5322 #endif
5324 #ifdef HAVE_TARGET_64_LITTLE
5325 template
5326 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
5327 #endif
5329 #ifdef HAVE_TARGET_64_BIG
5330 template
5331 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
5332 #endif
5334 #ifdef HAVE_TARGET_32_LITTLE
5335 template
5336 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
5337 #endif
5339 #ifdef HAVE_TARGET_32_BIG
5340 template
5341 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
5342 #endif
5344 #ifdef HAVE_TARGET_64_LITTLE
5345 template
5346 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
5347 #endif
5349 #ifdef HAVE_TARGET_64_BIG
5350 template
5351 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
5352 #endif
5354 #ifdef HAVE_TARGET_32_LITTLE
5355 template
5356 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
5357 #endif
5359 #ifdef HAVE_TARGET_32_BIG
5360 template
5361 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
5362 #endif
5364 #ifdef HAVE_TARGET_64_LITTLE
5365 template
5366 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
5367 #endif
5369 #ifdef HAVE_TARGET_64_BIG
5370 template
5371 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
5372 #endif
5374 #ifdef HAVE_TARGET_32_LITTLE
5375 template
5376 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
5377 #endif
5379 #ifdef HAVE_TARGET_32_BIG
5380 template
5381 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
5382 #endif
5384 #ifdef HAVE_TARGET_64_LITTLE
5385 template
5386 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
5387 #endif
5389 #ifdef HAVE_TARGET_64_BIG
5390 template
5391 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
5392 #endif
5394 #ifdef HAVE_TARGET_32_LITTLE
5395 template
5396 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
5397 #endif
5399 #ifdef HAVE_TARGET_32_BIG
5400 template
5401 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
5402 #endif
5404 #ifdef HAVE_TARGET_64_LITTLE
5405 template
5406 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
5407 #endif
5409 #ifdef HAVE_TARGET_64_BIG
5410 template
5411 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
5412 #endif
5414 #ifdef HAVE_TARGET_32_LITTLE
5415 template
5416 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5417 #endif
5419 #ifdef HAVE_TARGET_32_BIG
5420 template
5421 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5422 #endif
5424 #ifdef HAVE_TARGET_64_LITTLE
5425 template
5426 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5427 #endif
5429 #ifdef HAVE_TARGET_64_BIG
5430 template
5431 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5432 #endif
5434 #ifdef HAVE_TARGET_32_LITTLE
5435 template
5436 class Output_data_group<32, false>;
5437 #endif
5439 #ifdef HAVE_TARGET_32_BIG
5440 template
5441 class Output_data_group<32, true>;
5442 #endif
5444 #ifdef HAVE_TARGET_64_LITTLE
5445 template
5446 class Output_data_group<64, false>;
5447 #endif
5449 #ifdef HAVE_TARGET_64_BIG
5450 template
5451 class Output_data_group<64, true>;
5452 #endif
5454 #ifdef HAVE_TARGET_32_LITTLE
5455 template
5456 class Output_data_got<32, false>;
5457 #endif
5459 #ifdef HAVE_TARGET_32_BIG
5460 template
5461 class Output_data_got<32, true>;
5462 #endif
5464 #ifdef HAVE_TARGET_64_LITTLE
5465 template
5466 class Output_data_got<64, false>;
5467 #endif
5469 #ifdef HAVE_TARGET_64_BIG
5470 template
5471 class Output_data_got<64, true>;
5472 #endif
5474 } // End namespace gold.