daily update
[binutils.git] / gold / output.cc
blob6ec4aef6601b02e02d32e6f38986b8daf8426a56
1 // output.cc -- manage the output file for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cstdlib>
26 #include <cstring>
27 #include <cerrno>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <sys/stat.h>
32 #include <algorithm>
33 #include "libiberty.h" // for unlink_if_ordinary()
35 #include "parameters.h"
36 #include "object.h"
37 #include "symtab.h"
38 #include "reloc.h"
39 #include "merge.h"
40 #include "output.h"
42 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
43 #ifndef MAP_ANONYMOUS
44 # define MAP_ANONYMOUS MAP_ANON
45 #endif
47 namespace gold
50 // Output_data variables.
52 bool Output_data::allocated_sizes_are_fixed;
54 // Output_data methods.
56 Output_data::~Output_data()
60 // Return the default alignment for the target size.
62 uint64_t
63 Output_data::default_alignment()
65 return Output_data::default_alignment_for_size(
66 parameters->target().get_size());
69 // Return the default alignment for a size--32 or 64.
71 uint64_t
72 Output_data::default_alignment_for_size(int size)
74 if (size == 32)
75 return 4;
76 else if (size == 64)
77 return 8;
78 else
79 gold_unreachable();
82 // Output_section_header methods. This currently assumes that the
83 // segment and section lists are complete at construction time.
85 Output_section_headers::Output_section_headers(
86 const Layout* layout,
87 const Layout::Segment_list* segment_list,
88 const Layout::Section_list* section_list,
89 const Layout::Section_list* unattached_section_list,
90 const Stringpool* secnamepool,
91 const Output_section* shstrtab_section)
92 : layout_(layout),
93 segment_list_(segment_list),
94 section_list_(section_list),
95 unattached_section_list_(unattached_section_list),
96 secnamepool_(secnamepool),
97 shstrtab_section_(shstrtab_section)
99 // Count all the sections. Start with 1 for the null section.
100 off_t count = 1;
101 if (!parameters->options().relocatable())
103 for (Layout::Segment_list::const_iterator p = segment_list->begin();
104 p != segment_list->end();
105 ++p)
106 if ((*p)->type() == elfcpp::PT_LOAD)
107 count += (*p)->output_section_count();
109 else
111 for (Layout::Section_list::const_iterator p = section_list->begin();
112 p != section_list->end();
113 ++p)
114 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
115 ++count;
117 count += unattached_section_list->size();
119 const int size = parameters->target().get_size();
120 int shdr_size;
121 if (size == 32)
122 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
123 else if (size == 64)
124 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
125 else
126 gold_unreachable();
128 this->set_data_size(count * shdr_size);
131 // Write out the section headers.
133 void
134 Output_section_headers::do_write(Output_file* of)
136 switch (parameters->size_and_endianness())
138 #ifdef HAVE_TARGET_32_LITTLE
139 case Parameters::TARGET_32_LITTLE:
140 this->do_sized_write<32, false>(of);
141 break;
142 #endif
143 #ifdef HAVE_TARGET_32_BIG
144 case Parameters::TARGET_32_BIG:
145 this->do_sized_write<32, true>(of);
146 break;
147 #endif
148 #ifdef HAVE_TARGET_64_LITTLE
149 case Parameters::TARGET_64_LITTLE:
150 this->do_sized_write<64, false>(of);
151 break;
152 #endif
153 #ifdef HAVE_TARGET_64_BIG
154 case Parameters::TARGET_64_BIG:
155 this->do_sized_write<64, true>(of);
156 break;
157 #endif
158 default:
159 gold_unreachable();
163 template<int size, bool big_endian>
164 void
165 Output_section_headers::do_sized_write(Output_file* of)
167 off_t all_shdrs_size = this->data_size();
168 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
170 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
171 unsigned char* v = view;
174 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
175 oshdr.put_sh_name(0);
176 oshdr.put_sh_type(elfcpp::SHT_NULL);
177 oshdr.put_sh_flags(0);
178 oshdr.put_sh_addr(0);
179 oshdr.put_sh_offset(0);
181 size_t section_count = (this->data_size()
182 / elfcpp::Elf_sizes<size>::shdr_size);
183 if (section_count < elfcpp::SHN_LORESERVE)
184 oshdr.put_sh_size(0);
185 else
186 oshdr.put_sh_size(section_count);
188 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
189 if (shstrndx < elfcpp::SHN_LORESERVE)
190 oshdr.put_sh_link(0);
191 else
192 oshdr.put_sh_link(shstrndx);
194 oshdr.put_sh_info(0);
195 oshdr.put_sh_addralign(0);
196 oshdr.put_sh_entsize(0);
199 v += shdr_size;
201 unsigned int shndx = 1;
202 if (!parameters->options().relocatable())
204 for (Layout::Segment_list::const_iterator p =
205 this->segment_list_->begin();
206 p != this->segment_list_->end();
207 ++p)
208 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
209 this->secnamepool_,
211 &shndx);
213 else
215 for (Layout::Section_list::const_iterator p =
216 this->section_list_->begin();
217 p != this->section_list_->end();
218 ++p)
220 // We do unallocated sections below, except that group
221 // sections have to come first.
222 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
223 && (*p)->type() != elfcpp::SHT_GROUP)
224 continue;
225 gold_assert(shndx == (*p)->out_shndx());
226 elfcpp::Shdr_write<size, big_endian> oshdr(v);
227 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
228 v += shdr_size;
229 ++shndx;
233 for (Layout::Section_list::const_iterator p =
234 this->unattached_section_list_->begin();
235 p != this->unattached_section_list_->end();
236 ++p)
238 // For a relocatable link, we did unallocated group sections
239 // above, since they have to come first.
240 if ((*p)->type() == elfcpp::SHT_GROUP
241 && parameters->options().relocatable())
242 continue;
243 gold_assert(shndx == (*p)->out_shndx());
244 elfcpp::Shdr_write<size, big_endian> oshdr(v);
245 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
246 v += shdr_size;
247 ++shndx;
250 of->write_output_view(this->offset(), all_shdrs_size, view);
253 // Output_segment_header methods.
255 Output_segment_headers::Output_segment_headers(
256 const Layout::Segment_list& segment_list)
257 : segment_list_(segment_list)
259 const int size = parameters->target().get_size();
260 int phdr_size;
261 if (size == 32)
262 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
263 else if (size == 64)
264 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
265 else
266 gold_unreachable();
268 this->set_data_size(segment_list.size() * phdr_size);
271 void
272 Output_segment_headers::do_write(Output_file* of)
274 switch (parameters->size_and_endianness())
276 #ifdef HAVE_TARGET_32_LITTLE
277 case Parameters::TARGET_32_LITTLE:
278 this->do_sized_write<32, false>(of);
279 break;
280 #endif
281 #ifdef HAVE_TARGET_32_BIG
282 case Parameters::TARGET_32_BIG:
283 this->do_sized_write<32, true>(of);
284 break;
285 #endif
286 #ifdef HAVE_TARGET_64_LITTLE
287 case Parameters::TARGET_64_LITTLE:
288 this->do_sized_write<64, false>(of);
289 break;
290 #endif
291 #ifdef HAVE_TARGET_64_BIG
292 case Parameters::TARGET_64_BIG:
293 this->do_sized_write<64, true>(of);
294 break;
295 #endif
296 default:
297 gold_unreachable();
301 template<int size, bool big_endian>
302 void
303 Output_segment_headers::do_sized_write(Output_file* of)
305 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
306 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
307 gold_assert(all_phdrs_size == this->data_size());
308 unsigned char* view = of->get_output_view(this->offset(),
309 all_phdrs_size);
310 unsigned char* v = view;
311 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
312 p != this->segment_list_.end();
313 ++p)
315 elfcpp::Phdr_write<size, big_endian> ophdr(v);
316 (*p)->write_header(&ophdr);
317 v += phdr_size;
320 gold_assert(v - view == all_phdrs_size);
322 of->write_output_view(this->offset(), all_phdrs_size, view);
325 // Output_file_header methods.
327 Output_file_header::Output_file_header(const Target* target,
328 const Symbol_table* symtab,
329 const Output_segment_headers* osh,
330 const char* entry)
331 : target_(target),
332 symtab_(symtab),
333 segment_header_(osh),
334 section_header_(NULL),
335 shstrtab_(NULL),
336 entry_(entry)
338 const int size = parameters->target().get_size();
339 int ehdr_size;
340 if (size == 32)
341 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
342 else if (size == 64)
343 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
344 else
345 gold_unreachable();
347 this->set_data_size(ehdr_size);
350 // Set the section table information for a file header.
352 void
353 Output_file_header::set_section_info(const Output_section_headers* shdrs,
354 const Output_section* shstrtab)
356 this->section_header_ = shdrs;
357 this->shstrtab_ = shstrtab;
360 // Write out the file header.
362 void
363 Output_file_header::do_write(Output_file* of)
365 gold_assert(this->offset() == 0);
367 switch (parameters->size_and_endianness())
369 #ifdef HAVE_TARGET_32_LITTLE
370 case Parameters::TARGET_32_LITTLE:
371 this->do_sized_write<32, false>(of);
372 break;
373 #endif
374 #ifdef HAVE_TARGET_32_BIG
375 case Parameters::TARGET_32_BIG:
376 this->do_sized_write<32, true>(of);
377 break;
378 #endif
379 #ifdef HAVE_TARGET_64_LITTLE
380 case Parameters::TARGET_64_LITTLE:
381 this->do_sized_write<64, false>(of);
382 break;
383 #endif
384 #ifdef HAVE_TARGET_64_BIG
385 case Parameters::TARGET_64_BIG:
386 this->do_sized_write<64, true>(of);
387 break;
388 #endif
389 default:
390 gold_unreachable();
394 // Write out the file header with appropriate size and endianess.
396 template<int size, bool big_endian>
397 void
398 Output_file_header::do_sized_write(Output_file* of)
400 gold_assert(this->offset() == 0);
402 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
403 unsigned char* view = of->get_output_view(0, ehdr_size);
404 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
406 unsigned char e_ident[elfcpp::EI_NIDENT];
407 memset(e_ident, 0, elfcpp::EI_NIDENT);
408 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
409 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
410 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
411 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
412 if (size == 32)
413 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
414 else if (size == 64)
415 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
416 else
417 gold_unreachable();
418 e_ident[elfcpp::EI_DATA] = (big_endian
419 ? elfcpp::ELFDATA2MSB
420 : elfcpp::ELFDATA2LSB);
421 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
422 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
423 oehdr.put_e_ident(e_ident);
425 elfcpp::ET e_type;
426 if (parameters->options().relocatable())
427 e_type = elfcpp::ET_REL;
428 else if (parameters->options().shared())
429 e_type = elfcpp::ET_DYN;
430 else
431 e_type = elfcpp::ET_EXEC;
432 oehdr.put_e_type(e_type);
434 oehdr.put_e_machine(this->target_->machine_code());
435 oehdr.put_e_version(elfcpp::EV_CURRENT);
437 oehdr.put_e_entry(this->entry<size>());
439 if (this->segment_header_ == NULL)
440 oehdr.put_e_phoff(0);
441 else
442 oehdr.put_e_phoff(this->segment_header_->offset());
444 oehdr.put_e_shoff(this->section_header_->offset());
446 // FIXME: The target needs to set the flags.
447 oehdr.put_e_flags(0);
449 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
451 if (this->segment_header_ == NULL)
453 oehdr.put_e_phentsize(0);
454 oehdr.put_e_phnum(0);
456 else
458 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
459 oehdr.put_e_phnum(this->segment_header_->data_size()
460 / elfcpp::Elf_sizes<size>::phdr_size);
463 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
464 size_t section_count = (this->section_header_->data_size()
465 / elfcpp::Elf_sizes<size>::shdr_size);
467 if (section_count < elfcpp::SHN_LORESERVE)
468 oehdr.put_e_shnum(this->section_header_->data_size()
469 / elfcpp::Elf_sizes<size>::shdr_size);
470 else
471 oehdr.put_e_shnum(0);
473 unsigned int shstrndx = this->shstrtab_->out_shndx();
474 if (shstrndx < elfcpp::SHN_LORESERVE)
475 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
476 else
477 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
479 of->write_output_view(0, ehdr_size, view);
482 // Return the value to use for the entry address. THIS->ENTRY_ is the
483 // symbol specified on the command line, if any.
485 template<int size>
486 typename elfcpp::Elf_types<size>::Elf_Addr
487 Output_file_header::entry()
489 const bool should_issue_warning = (this->entry_ != NULL
490 && !parameters->options().relocatable()
491 && !parameters->options().shared());
493 // FIXME: Need to support target specific entry symbol.
494 const char* entry = this->entry_;
495 if (entry == NULL)
496 entry = "_start";
498 Symbol* sym = this->symtab_->lookup(entry);
500 typename Sized_symbol<size>::Value_type v;
501 if (sym != NULL)
503 Sized_symbol<size>* ssym;
504 ssym = this->symtab_->get_sized_symbol<size>(sym);
505 if (!ssym->is_defined() && should_issue_warning)
506 gold_warning("entry symbol '%s' exists but is not defined", entry);
507 v = ssym->value();
509 else
511 // We couldn't find the entry symbol. See if we can parse it as
512 // a number. This supports, e.g., -e 0x1000.
513 char* endptr;
514 v = strtoull(entry, &endptr, 0);
515 if (*endptr != '\0')
517 if (should_issue_warning)
518 gold_warning("cannot find entry symbol '%s'", entry);
519 v = 0;
523 return v;
526 // Output_data_const methods.
528 void
529 Output_data_const::do_write(Output_file* of)
531 of->write(this->offset(), this->data_.data(), this->data_.size());
534 // Output_data_const_buffer methods.
536 void
537 Output_data_const_buffer::do_write(Output_file* of)
539 of->write(this->offset(), this->p_, this->data_size());
542 // Output_section_data methods.
544 // Record the output section, and set the entry size and such.
546 void
547 Output_section_data::set_output_section(Output_section* os)
549 gold_assert(this->output_section_ == NULL);
550 this->output_section_ = os;
551 this->do_adjust_output_section(os);
554 // Return the section index of the output section.
556 unsigned int
557 Output_section_data::do_out_shndx() const
559 gold_assert(this->output_section_ != NULL);
560 return this->output_section_->out_shndx();
563 // Set the alignment, which means we may need to update the alignment
564 // of the output section.
566 void
567 Output_section_data::set_addralign(uint64_t addralign)
569 this->addralign_ = addralign;
570 if (this->output_section_ != NULL
571 && this->output_section_->addralign() < addralign)
572 this->output_section_->set_addralign(addralign);
575 // Output_data_strtab methods.
577 // Set the final data size.
579 void
580 Output_data_strtab::set_final_data_size()
582 this->strtab_->set_string_offsets();
583 this->set_data_size(this->strtab_->get_strtab_size());
586 // Write out a string table.
588 void
589 Output_data_strtab::do_write(Output_file* of)
591 this->strtab_->write(of, this->offset());
594 // Output_reloc methods.
596 // A reloc against a global symbol.
598 template<bool dynamic, int size, bool big_endian>
599 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
600 Symbol* gsym,
601 unsigned int type,
602 Output_data* od,
603 Address address,
604 bool is_relative)
605 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
606 is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
608 // this->type_ is a bitfield; make sure TYPE fits.
609 gold_assert(this->type_ == type);
610 this->u1_.gsym = gsym;
611 this->u2_.od = od;
612 if (dynamic)
613 this->set_needs_dynsym_index();
616 template<bool dynamic, int size, bool big_endian>
617 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
618 Symbol* gsym,
619 unsigned int type,
620 Sized_relobj<size, big_endian>* relobj,
621 unsigned int shndx,
622 Address address,
623 bool is_relative)
624 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
625 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
627 gold_assert(shndx != INVALID_CODE);
628 // this->type_ is a bitfield; make sure TYPE fits.
629 gold_assert(this->type_ == type);
630 this->u1_.gsym = gsym;
631 this->u2_.relobj = relobj;
632 if (dynamic)
633 this->set_needs_dynsym_index();
636 // A reloc against a local symbol.
638 template<bool dynamic, int size, bool big_endian>
639 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
640 Sized_relobj<size, big_endian>* relobj,
641 unsigned int local_sym_index,
642 unsigned int type,
643 Output_data* od,
644 Address address,
645 bool is_relative,
646 bool is_section_symbol)
647 : address_(address), local_sym_index_(local_sym_index), type_(type),
648 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
649 shndx_(INVALID_CODE)
651 gold_assert(local_sym_index != GSYM_CODE
652 && local_sym_index != INVALID_CODE);
653 // this->type_ is a bitfield; make sure TYPE fits.
654 gold_assert(this->type_ == type);
655 this->u1_.relobj = relobj;
656 this->u2_.od = od;
657 if (dynamic)
658 this->set_needs_dynsym_index();
661 template<bool dynamic, int size, bool big_endian>
662 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
663 Sized_relobj<size, big_endian>* relobj,
664 unsigned int local_sym_index,
665 unsigned int type,
666 unsigned int shndx,
667 Address address,
668 bool is_relative,
669 bool is_section_symbol)
670 : address_(address), local_sym_index_(local_sym_index), type_(type),
671 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
672 shndx_(shndx)
674 gold_assert(local_sym_index != GSYM_CODE
675 && local_sym_index != INVALID_CODE);
676 gold_assert(shndx != INVALID_CODE);
677 // this->type_ is a bitfield; make sure TYPE fits.
678 gold_assert(this->type_ == type);
679 this->u1_.relobj = relobj;
680 this->u2_.relobj = relobj;
681 if (dynamic)
682 this->set_needs_dynsym_index();
685 // A reloc against the STT_SECTION symbol of an output section.
687 template<bool dynamic, int size, bool big_endian>
688 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
689 Output_section* os,
690 unsigned int type,
691 Output_data* od,
692 Address address)
693 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
694 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
696 // this->type_ is a bitfield; make sure TYPE fits.
697 gold_assert(this->type_ == type);
698 this->u1_.os = os;
699 this->u2_.od = od;
700 if (dynamic)
701 this->set_needs_dynsym_index();
702 else
703 os->set_needs_symtab_index();
706 template<bool dynamic, int size, bool big_endian>
707 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
708 Output_section* os,
709 unsigned int type,
710 Sized_relobj<size, big_endian>* relobj,
711 unsigned int shndx,
712 Address address)
713 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
714 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
716 gold_assert(shndx != INVALID_CODE);
717 // this->type_ is a bitfield; make sure TYPE fits.
718 gold_assert(this->type_ == type);
719 this->u1_.os = os;
720 this->u2_.relobj = relobj;
721 if (dynamic)
722 this->set_needs_dynsym_index();
723 else
724 os->set_needs_symtab_index();
727 // Record that we need a dynamic symbol index for this relocation.
729 template<bool dynamic, int size, bool big_endian>
730 void
731 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
732 set_needs_dynsym_index()
734 if (this->is_relative_)
735 return;
736 switch (this->local_sym_index_)
738 case INVALID_CODE:
739 gold_unreachable();
741 case GSYM_CODE:
742 this->u1_.gsym->set_needs_dynsym_entry();
743 break;
745 case SECTION_CODE:
746 this->u1_.os->set_needs_dynsym_index();
747 break;
749 case 0:
750 break;
752 default:
754 const unsigned int lsi = this->local_sym_index_;
755 if (!this->is_section_symbol_)
756 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
757 else
758 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
760 break;
764 // Get the symbol index of a relocation.
766 template<bool dynamic, int size, bool big_endian>
767 unsigned int
768 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
769 const
771 unsigned int index;
772 switch (this->local_sym_index_)
774 case INVALID_CODE:
775 gold_unreachable();
777 case GSYM_CODE:
778 if (this->u1_.gsym == NULL)
779 index = 0;
780 else if (dynamic)
781 index = this->u1_.gsym->dynsym_index();
782 else
783 index = this->u1_.gsym->symtab_index();
784 break;
786 case SECTION_CODE:
787 if (dynamic)
788 index = this->u1_.os->dynsym_index();
789 else
790 index = this->u1_.os->symtab_index();
791 break;
793 case 0:
794 // Relocations without symbols use a symbol index of 0.
795 index = 0;
796 break;
798 default:
800 const unsigned int lsi = this->local_sym_index_;
801 if (!this->is_section_symbol_)
803 if (dynamic)
804 index = this->u1_.relobj->dynsym_index(lsi);
805 else
806 index = this->u1_.relobj->symtab_index(lsi);
808 else
810 Output_section* os = this->u1_.relobj->output_section(lsi);
811 gold_assert(os != NULL);
812 if (dynamic)
813 index = os->dynsym_index();
814 else
815 index = os->symtab_index();
818 break;
820 gold_assert(index != -1U);
821 return index;
824 // For a local section symbol, get the address of the offset ADDEND
825 // within the input section.
827 template<bool dynamic, int size, bool big_endian>
828 typename elfcpp::Elf_types<size>::Elf_Addr
829 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
830 local_section_offset(Addend addend) const
832 gold_assert(this->local_sym_index_ != GSYM_CODE
833 && this->local_sym_index_ != SECTION_CODE
834 && this->local_sym_index_ != INVALID_CODE
835 && this->is_section_symbol_);
836 const unsigned int lsi = this->local_sym_index_;
837 Output_section* os = this->u1_.relobj->output_section(lsi);
838 gold_assert(os != NULL);
839 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
840 if (offset != -1U)
841 return offset + addend;
842 // This is a merge section.
843 offset = os->output_address(this->u1_.relobj, lsi, addend);
844 gold_assert(offset != -1U);
845 return offset;
848 // Get the output address of a relocation.
850 template<bool dynamic, int size, bool big_endian>
851 typename elfcpp::Elf_types<size>::Elf_Addr
852 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
854 Address address = this->address_;
855 if (this->shndx_ != INVALID_CODE)
857 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
858 gold_assert(os != NULL);
859 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
860 if (off != -1U)
861 address += os->address() + off;
862 else
864 address = os->output_address(this->u2_.relobj, this->shndx_,
865 address);
866 gold_assert(address != -1U);
869 else if (this->u2_.od != NULL)
870 address += this->u2_.od->address();
871 return address;
874 // Write out the offset and info fields of a Rel or Rela relocation
875 // entry.
877 template<bool dynamic, int size, bool big_endian>
878 template<typename Write_rel>
879 void
880 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
881 Write_rel* wr) const
883 wr->put_r_offset(this->get_address());
884 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
885 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
888 // Write out a Rel relocation.
890 template<bool dynamic, int size, bool big_endian>
891 void
892 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
893 unsigned char* pov) const
895 elfcpp::Rel_write<size, big_endian> orel(pov);
896 this->write_rel(&orel);
899 // Get the value of the symbol referred to by a Rel relocation.
901 template<bool dynamic, int size, bool big_endian>
902 typename elfcpp::Elf_types<size>::Elf_Addr
903 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
904 Addend addend) const
906 if (this->local_sym_index_ == GSYM_CODE)
908 const Sized_symbol<size>* sym;
909 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
910 return sym->value() + addend;
912 gold_assert(this->local_sym_index_ != SECTION_CODE
913 && this->local_sym_index_ != INVALID_CODE
914 && !this->is_section_symbol_);
915 const unsigned int lsi = this->local_sym_index_;
916 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
917 return symval->value(this->u1_.relobj, addend);
920 // Reloc comparison. This function sorts the dynamic relocs for the
921 // benefit of the dynamic linker. First we sort all relative relocs
922 // to the front. Among relative relocs, we sort by output address.
923 // Among non-relative relocs, we sort by symbol index, then by output
924 // address.
926 template<bool dynamic, int size, bool big_endian>
928 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
929 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
930 const
932 if (this->is_relative_)
934 if (!r2.is_relative_)
935 return -1;
936 // Otherwise sort by reloc address below.
938 else if (r2.is_relative_)
939 return 1;
940 else
942 unsigned int sym1 = this->get_symbol_index();
943 unsigned int sym2 = r2.get_symbol_index();
944 if (sym1 < sym2)
945 return -1;
946 else if (sym1 > sym2)
947 return 1;
948 // Otherwise sort by reloc address.
951 section_offset_type addr1 = this->get_address();
952 section_offset_type addr2 = r2.get_address();
953 if (addr1 < addr2)
954 return -1;
955 else if (addr1 > addr2)
956 return 1;
958 // Final tie breaker, in order to generate the same output on any
959 // host: reloc type.
960 unsigned int type1 = this->type_;
961 unsigned int type2 = r2.type_;
962 if (type1 < type2)
963 return -1;
964 else if (type1 > type2)
965 return 1;
967 // These relocs appear to be exactly the same.
968 return 0;
971 // Write out a Rela relocation.
973 template<bool dynamic, int size, bool big_endian>
974 void
975 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
976 unsigned char* pov) const
978 elfcpp::Rela_write<size, big_endian> orel(pov);
979 this->rel_.write_rel(&orel);
980 Addend addend = this->addend_;
981 if (this->rel_.is_relative())
982 addend = this->rel_.symbol_value(addend);
983 else if (this->rel_.is_local_section_symbol())
984 addend = this->rel_.local_section_offset(addend);
985 orel.put_r_addend(addend);
988 // Output_data_reloc_base methods.
990 // Adjust the output section.
992 template<int sh_type, bool dynamic, int size, bool big_endian>
993 void
994 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
995 ::do_adjust_output_section(Output_section* os)
997 if (sh_type == elfcpp::SHT_REL)
998 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
999 else if (sh_type == elfcpp::SHT_RELA)
1000 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1001 else
1002 gold_unreachable();
1003 if (dynamic)
1004 os->set_should_link_to_dynsym();
1005 else
1006 os->set_should_link_to_symtab();
1009 // Write out relocation data.
1011 template<int sh_type, bool dynamic, int size, bool big_endian>
1012 void
1013 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1014 Output_file* of)
1016 const off_t off = this->offset();
1017 const off_t oview_size = this->data_size();
1018 unsigned char* const oview = of->get_output_view(off, oview_size);
1020 if (this->sort_relocs_)
1022 gold_assert(dynamic);
1023 std::sort(this->relocs_.begin(), this->relocs_.end(),
1024 Sort_relocs_comparison());
1027 unsigned char* pov = oview;
1028 for (typename Relocs::const_iterator p = this->relocs_.begin();
1029 p != this->relocs_.end();
1030 ++p)
1032 p->write(pov);
1033 pov += reloc_size;
1036 gold_assert(pov - oview == oview_size);
1038 of->write_output_view(off, oview_size, oview);
1040 // We no longer need the relocation entries.
1041 this->relocs_.clear();
1044 // Class Output_relocatable_relocs.
1046 template<int sh_type, int size, bool big_endian>
1047 void
1048 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1050 this->set_data_size(this->rr_->output_reloc_count()
1051 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1054 // class Output_data_group.
1056 template<int size, bool big_endian>
1057 Output_data_group<size, big_endian>::Output_data_group(
1058 Sized_relobj<size, big_endian>* relobj,
1059 section_size_type entry_count,
1060 elfcpp::Elf_Word flags,
1061 std::vector<unsigned int>* input_shndxes)
1062 : Output_section_data(entry_count * 4, 4),
1063 relobj_(relobj),
1064 flags_(flags)
1066 this->input_shndxes_.swap(*input_shndxes);
1069 // Write out the section group, which means translating the section
1070 // indexes to apply to the output file.
1072 template<int size, bool big_endian>
1073 void
1074 Output_data_group<size, big_endian>::do_write(Output_file* of)
1076 const off_t off = this->offset();
1077 const section_size_type oview_size =
1078 convert_to_section_size_type(this->data_size());
1079 unsigned char* const oview = of->get_output_view(off, oview_size);
1081 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1082 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1083 ++contents;
1085 for (std::vector<unsigned int>::const_iterator p =
1086 this->input_shndxes_.begin();
1087 p != this->input_shndxes_.end();
1088 ++p, ++contents)
1090 Output_section* os = this->relobj_->output_section(*p);
1092 unsigned int output_shndx;
1093 if (os != NULL)
1094 output_shndx = os->out_shndx();
1095 else
1097 this->relobj_->error(_("section group retained but "
1098 "group element discarded"));
1099 output_shndx = 0;
1102 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1105 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1106 gold_assert(wrote == oview_size);
1108 of->write_output_view(off, oview_size, oview);
1110 // We no longer need this information.
1111 this->input_shndxes_.clear();
1114 // Output_data_got::Got_entry methods.
1116 // Write out the entry.
1118 template<int size, bool big_endian>
1119 void
1120 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1122 Valtype val = 0;
1124 switch (this->local_sym_index_)
1126 case GSYM_CODE:
1128 // If the symbol is resolved locally, we need to write out the
1129 // link-time value, which will be relocated dynamically by a
1130 // RELATIVE relocation.
1131 Symbol* gsym = this->u_.gsym;
1132 Sized_symbol<size>* sgsym;
1133 // This cast is a bit ugly. We don't want to put a
1134 // virtual method in Symbol, because we want Symbol to be
1135 // as small as possible.
1136 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1137 val = sgsym->value();
1139 break;
1141 case CONSTANT_CODE:
1142 val = this->u_.constant;
1143 break;
1145 default:
1147 const unsigned int lsi = this->local_sym_index_;
1148 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1149 val = symval->value(this->u_.object, 0);
1151 break;
1154 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1157 // Output_data_got methods.
1159 // Add an entry for a global symbol to the GOT. This returns true if
1160 // this is a new GOT entry, false if the symbol already had a GOT
1161 // entry.
1163 template<int size, bool big_endian>
1164 bool
1165 Output_data_got<size, big_endian>::add_global(
1166 Symbol* gsym,
1167 unsigned int got_type)
1169 if (gsym->has_got_offset(got_type))
1170 return false;
1172 this->entries_.push_back(Got_entry(gsym));
1173 this->set_got_size();
1174 gsym->set_got_offset(got_type, this->last_got_offset());
1175 return true;
1178 // Add an entry for a global symbol to the GOT, and add a dynamic
1179 // relocation of type R_TYPE for the GOT entry.
1180 template<int size, bool big_endian>
1181 void
1182 Output_data_got<size, big_endian>::add_global_with_rel(
1183 Symbol* gsym,
1184 unsigned int got_type,
1185 Rel_dyn* rel_dyn,
1186 unsigned int r_type)
1188 if (gsym->has_got_offset(got_type))
1189 return;
1191 this->entries_.push_back(Got_entry());
1192 this->set_got_size();
1193 unsigned int got_offset = this->last_got_offset();
1194 gsym->set_got_offset(got_type, got_offset);
1195 rel_dyn->add_global(gsym, r_type, this, got_offset);
1198 template<int size, bool big_endian>
1199 void
1200 Output_data_got<size, big_endian>::add_global_with_rela(
1201 Symbol* gsym,
1202 unsigned int got_type,
1203 Rela_dyn* rela_dyn,
1204 unsigned int r_type)
1206 if (gsym->has_got_offset(got_type))
1207 return;
1209 this->entries_.push_back(Got_entry());
1210 this->set_got_size();
1211 unsigned int got_offset = this->last_got_offset();
1212 gsym->set_got_offset(got_type, got_offset);
1213 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1216 // Add a pair of entries for a global symbol to the GOT, and add
1217 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1218 // If R_TYPE_2 == 0, add the second entry with no relocation.
1219 template<int size, bool big_endian>
1220 void
1221 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1222 Symbol* gsym,
1223 unsigned int got_type,
1224 Rel_dyn* rel_dyn,
1225 unsigned int r_type_1,
1226 unsigned int r_type_2)
1228 if (gsym->has_got_offset(got_type))
1229 return;
1231 this->entries_.push_back(Got_entry());
1232 unsigned int got_offset = this->last_got_offset();
1233 gsym->set_got_offset(got_type, got_offset);
1234 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1236 this->entries_.push_back(Got_entry());
1237 if (r_type_2 != 0)
1239 got_offset = this->last_got_offset();
1240 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1243 this->set_got_size();
1246 template<int size, bool big_endian>
1247 void
1248 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1249 Symbol* gsym,
1250 unsigned int got_type,
1251 Rela_dyn* rela_dyn,
1252 unsigned int r_type_1,
1253 unsigned int r_type_2)
1255 if (gsym->has_got_offset(got_type))
1256 return;
1258 this->entries_.push_back(Got_entry());
1259 unsigned int got_offset = this->last_got_offset();
1260 gsym->set_got_offset(got_type, got_offset);
1261 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1263 this->entries_.push_back(Got_entry());
1264 if (r_type_2 != 0)
1266 got_offset = this->last_got_offset();
1267 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1270 this->set_got_size();
1273 // Add an entry for a local symbol to the GOT. This returns true if
1274 // this is a new GOT entry, false if the symbol already has a GOT
1275 // entry.
1277 template<int size, bool big_endian>
1278 bool
1279 Output_data_got<size, big_endian>::add_local(
1280 Sized_relobj<size, big_endian>* object,
1281 unsigned int symndx,
1282 unsigned int got_type)
1284 if (object->local_has_got_offset(symndx, got_type))
1285 return false;
1287 this->entries_.push_back(Got_entry(object, symndx));
1288 this->set_got_size();
1289 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1290 return true;
1293 // Add an entry for a local symbol to the GOT, and add a dynamic
1294 // relocation of type R_TYPE for the GOT entry.
1295 template<int size, bool big_endian>
1296 void
1297 Output_data_got<size, big_endian>::add_local_with_rel(
1298 Sized_relobj<size, big_endian>* object,
1299 unsigned int symndx,
1300 unsigned int got_type,
1301 Rel_dyn* rel_dyn,
1302 unsigned int r_type)
1304 if (object->local_has_got_offset(symndx, got_type))
1305 return;
1307 this->entries_.push_back(Got_entry());
1308 this->set_got_size();
1309 unsigned int got_offset = this->last_got_offset();
1310 object->set_local_got_offset(symndx, got_type, got_offset);
1311 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1314 template<int size, bool big_endian>
1315 void
1316 Output_data_got<size, big_endian>::add_local_with_rela(
1317 Sized_relobj<size, big_endian>* object,
1318 unsigned int symndx,
1319 unsigned int got_type,
1320 Rela_dyn* rela_dyn,
1321 unsigned int r_type)
1323 if (object->local_has_got_offset(symndx, got_type))
1324 return;
1326 this->entries_.push_back(Got_entry());
1327 this->set_got_size();
1328 unsigned int got_offset = this->last_got_offset();
1329 object->set_local_got_offset(symndx, got_type, got_offset);
1330 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1333 // Add a pair of entries for a local symbol to the GOT, and add
1334 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1335 // If R_TYPE_2 == 0, add the second entry with no relocation.
1336 template<int size, bool big_endian>
1337 void
1338 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1339 Sized_relobj<size, big_endian>* object,
1340 unsigned int symndx,
1341 unsigned int shndx,
1342 unsigned int got_type,
1343 Rel_dyn* rel_dyn,
1344 unsigned int r_type_1,
1345 unsigned int r_type_2)
1347 if (object->local_has_got_offset(symndx, got_type))
1348 return;
1350 this->entries_.push_back(Got_entry());
1351 unsigned int got_offset = this->last_got_offset();
1352 object->set_local_got_offset(symndx, got_type, got_offset);
1353 Output_section* os = object->output_section(shndx);
1354 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1356 this->entries_.push_back(Got_entry(object, symndx));
1357 if (r_type_2 != 0)
1359 got_offset = this->last_got_offset();
1360 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1363 this->set_got_size();
1366 template<int size, bool big_endian>
1367 void
1368 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1369 Sized_relobj<size, big_endian>* object,
1370 unsigned int symndx,
1371 unsigned int shndx,
1372 unsigned int got_type,
1373 Rela_dyn* rela_dyn,
1374 unsigned int r_type_1,
1375 unsigned int r_type_2)
1377 if (object->local_has_got_offset(symndx, got_type))
1378 return;
1380 this->entries_.push_back(Got_entry());
1381 unsigned int got_offset = this->last_got_offset();
1382 object->set_local_got_offset(symndx, got_type, got_offset);
1383 Output_section* os = object->output_section(shndx);
1384 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1386 this->entries_.push_back(Got_entry(object, symndx));
1387 if (r_type_2 != 0)
1389 got_offset = this->last_got_offset();
1390 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1393 this->set_got_size();
1396 // Write out the GOT.
1398 template<int size, bool big_endian>
1399 void
1400 Output_data_got<size, big_endian>::do_write(Output_file* of)
1402 const int add = size / 8;
1404 const off_t off = this->offset();
1405 const off_t oview_size = this->data_size();
1406 unsigned char* const oview = of->get_output_view(off, oview_size);
1408 unsigned char* pov = oview;
1409 for (typename Got_entries::const_iterator p = this->entries_.begin();
1410 p != this->entries_.end();
1411 ++p)
1413 p->write(pov);
1414 pov += add;
1417 gold_assert(pov - oview == oview_size);
1419 of->write_output_view(off, oview_size, oview);
1421 // We no longer need the GOT entries.
1422 this->entries_.clear();
1425 // Output_data_dynamic::Dynamic_entry methods.
1427 // Write out the entry.
1429 template<int size, bool big_endian>
1430 void
1431 Output_data_dynamic::Dynamic_entry::write(
1432 unsigned char* pov,
1433 const Stringpool* pool) const
1435 typename elfcpp::Elf_types<size>::Elf_WXword val;
1436 switch (this->offset_)
1438 case DYNAMIC_NUMBER:
1439 val = this->u_.val;
1440 break;
1442 case DYNAMIC_SECTION_SIZE:
1443 val = this->u_.od->data_size();
1444 break;
1446 case DYNAMIC_SYMBOL:
1448 const Sized_symbol<size>* s =
1449 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1450 val = s->value();
1452 break;
1454 case DYNAMIC_STRING:
1455 val = pool->get_offset(this->u_.str);
1456 break;
1458 default:
1459 val = this->u_.od->address() + this->offset_;
1460 break;
1463 elfcpp::Dyn_write<size, big_endian> dw(pov);
1464 dw.put_d_tag(this->tag_);
1465 dw.put_d_val(val);
1468 // Output_data_dynamic methods.
1470 // Adjust the output section to set the entry size.
1472 void
1473 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1475 if (parameters->target().get_size() == 32)
1476 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1477 else if (parameters->target().get_size() == 64)
1478 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1479 else
1480 gold_unreachable();
1483 // Set the final data size.
1485 void
1486 Output_data_dynamic::set_final_data_size()
1488 // Add the terminating entry.
1489 this->add_constant(elfcpp::DT_NULL, 0);
1491 int dyn_size;
1492 if (parameters->target().get_size() == 32)
1493 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1494 else if (parameters->target().get_size() == 64)
1495 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1496 else
1497 gold_unreachable();
1498 this->set_data_size(this->entries_.size() * dyn_size);
1501 // Write out the dynamic entries.
1503 void
1504 Output_data_dynamic::do_write(Output_file* of)
1506 switch (parameters->size_and_endianness())
1508 #ifdef HAVE_TARGET_32_LITTLE
1509 case Parameters::TARGET_32_LITTLE:
1510 this->sized_write<32, false>(of);
1511 break;
1512 #endif
1513 #ifdef HAVE_TARGET_32_BIG
1514 case Parameters::TARGET_32_BIG:
1515 this->sized_write<32, true>(of);
1516 break;
1517 #endif
1518 #ifdef HAVE_TARGET_64_LITTLE
1519 case Parameters::TARGET_64_LITTLE:
1520 this->sized_write<64, false>(of);
1521 break;
1522 #endif
1523 #ifdef HAVE_TARGET_64_BIG
1524 case Parameters::TARGET_64_BIG:
1525 this->sized_write<64, true>(of);
1526 break;
1527 #endif
1528 default:
1529 gold_unreachable();
1533 template<int size, bool big_endian>
1534 void
1535 Output_data_dynamic::sized_write(Output_file* of)
1537 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1539 const off_t offset = this->offset();
1540 const off_t oview_size = this->data_size();
1541 unsigned char* const oview = of->get_output_view(offset, oview_size);
1543 unsigned char* pov = oview;
1544 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1545 p != this->entries_.end();
1546 ++p)
1548 p->write<size, big_endian>(pov, this->pool_);
1549 pov += dyn_size;
1552 gold_assert(pov - oview == oview_size);
1554 of->write_output_view(offset, oview_size, oview);
1556 // We no longer need the dynamic entries.
1557 this->entries_.clear();
1560 // Class Output_symtab_xindex.
1562 void
1563 Output_symtab_xindex::do_write(Output_file* of)
1565 const off_t offset = this->offset();
1566 const off_t oview_size = this->data_size();
1567 unsigned char* const oview = of->get_output_view(offset, oview_size);
1569 memset(oview, 0, oview_size);
1571 if (parameters->target().is_big_endian())
1572 this->endian_do_write<true>(oview);
1573 else
1574 this->endian_do_write<false>(oview);
1576 of->write_output_view(offset, oview_size, oview);
1578 // We no longer need the data.
1579 this->entries_.clear();
1582 template<bool big_endian>
1583 void
1584 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1586 for (Xindex_entries::const_iterator p = this->entries_.begin();
1587 p != this->entries_.end();
1588 ++p)
1589 elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
1592 // Output_section::Input_section methods.
1594 // Return the data size. For an input section we store the size here.
1595 // For an Output_section_data, we have to ask it for the size.
1597 off_t
1598 Output_section::Input_section::data_size() const
1600 if (this->is_input_section())
1601 return this->u1_.data_size;
1602 else
1603 return this->u2_.posd->data_size();
1606 // Set the address and file offset.
1608 void
1609 Output_section::Input_section::set_address_and_file_offset(
1610 uint64_t address,
1611 off_t file_offset,
1612 off_t section_file_offset)
1614 if (this->is_input_section())
1615 this->u2_.object->set_section_offset(this->shndx_,
1616 file_offset - section_file_offset);
1617 else
1618 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1621 // Reset the address and file offset.
1623 void
1624 Output_section::Input_section::reset_address_and_file_offset()
1626 if (!this->is_input_section())
1627 this->u2_.posd->reset_address_and_file_offset();
1630 // Finalize the data size.
1632 void
1633 Output_section::Input_section::finalize_data_size()
1635 if (!this->is_input_section())
1636 this->u2_.posd->finalize_data_size();
1639 // Try to turn an input offset into an output offset. We want to
1640 // return the output offset relative to the start of this
1641 // Input_section in the output section.
1643 inline bool
1644 Output_section::Input_section::output_offset(
1645 const Relobj* object,
1646 unsigned int shndx,
1647 section_offset_type offset,
1648 section_offset_type *poutput) const
1650 if (!this->is_input_section())
1651 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1652 else
1654 if (this->shndx_ != shndx || this->u2_.object != object)
1655 return false;
1656 *poutput = offset;
1657 return true;
1661 // Return whether this is the merge section for the input section
1662 // SHNDX in OBJECT.
1664 inline bool
1665 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1666 unsigned int shndx) const
1668 if (this->is_input_section())
1669 return false;
1670 return this->u2_.posd->is_merge_section_for(object, shndx);
1673 // Write out the data. We don't have to do anything for an input
1674 // section--they are handled via Object::relocate--but this is where
1675 // we write out the data for an Output_section_data.
1677 void
1678 Output_section::Input_section::write(Output_file* of)
1680 if (!this->is_input_section())
1681 this->u2_.posd->write(of);
1684 // Write the data to a buffer. As for write(), we don't have to do
1685 // anything for an input section.
1687 void
1688 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1690 if (!this->is_input_section())
1691 this->u2_.posd->write_to_buffer(buffer);
1694 // Print to a map file.
1696 void
1697 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1699 switch (this->shndx_)
1701 case OUTPUT_SECTION_CODE:
1702 case MERGE_DATA_SECTION_CODE:
1703 case MERGE_STRING_SECTION_CODE:
1704 this->u2_.posd->print_to_mapfile(mapfile);
1705 break;
1707 default:
1708 mapfile->print_input_section(this->u2_.object, this->shndx_);
1709 break;
1713 // Output_section methods.
1715 // Construct an Output_section. NAME will point into a Stringpool.
1717 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1718 elfcpp::Elf_Xword flags)
1719 : name_(name),
1720 addralign_(0),
1721 entsize_(0),
1722 load_address_(0),
1723 link_section_(NULL),
1724 link_(0),
1725 info_section_(NULL),
1726 info_symndx_(NULL),
1727 info_(0),
1728 type_(type),
1729 flags_(flags),
1730 out_shndx_(-1U),
1731 symtab_index_(0),
1732 dynsym_index_(0),
1733 input_sections_(),
1734 first_input_offset_(0),
1735 fills_(),
1736 postprocessing_buffer_(NULL),
1737 needs_symtab_index_(false),
1738 needs_dynsym_index_(false),
1739 should_link_to_symtab_(false),
1740 should_link_to_dynsym_(false),
1741 after_input_sections_(false),
1742 requires_postprocessing_(false),
1743 found_in_sections_clause_(false),
1744 has_load_address_(false),
1745 info_uses_section_index_(false),
1746 may_sort_attached_input_sections_(false),
1747 must_sort_attached_input_sections_(false),
1748 attached_input_sections_are_sorted_(false),
1749 is_relro_(false),
1750 is_relro_local_(false),
1751 tls_offset_(0)
1753 // An unallocated section has no address. Forcing this means that
1754 // we don't need special treatment for symbols defined in debug
1755 // sections.
1756 if ((flags & elfcpp::SHF_ALLOC) == 0)
1757 this->set_address(0);
1760 Output_section::~Output_section()
1764 // Set the entry size.
1766 void
1767 Output_section::set_entsize(uint64_t v)
1769 if (this->entsize_ == 0)
1770 this->entsize_ = v;
1771 else
1772 gold_assert(this->entsize_ == v);
1775 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1776 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1777 // relocation section which applies to this section, or 0 if none, or
1778 // -1U if more than one. Return the offset of the input section
1779 // within the output section. Return -1 if the input section will
1780 // receive special handling. In the normal case we don't always keep
1781 // track of input sections for an Output_section. Instead, each
1782 // Object keeps track of the Output_section for each of its input
1783 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1784 // track of input sections here; this is used when SECTIONS appears in
1785 // a linker script.
1787 template<int size, bool big_endian>
1788 off_t
1789 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1790 unsigned int shndx,
1791 const char* secname,
1792 const elfcpp::Shdr<size, big_endian>& shdr,
1793 unsigned int reloc_shndx,
1794 bool have_sections_script)
1796 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1797 if ((addralign & (addralign - 1)) != 0)
1799 object->error(_("invalid alignment %lu for section \"%s\""),
1800 static_cast<unsigned long>(addralign), secname);
1801 addralign = 1;
1804 if (addralign > this->addralign_)
1805 this->addralign_ = addralign;
1807 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1808 this->update_flags_for_input_section(sh_flags);
1810 uint64_t entsize = shdr.get_sh_entsize();
1812 // .debug_str is a mergeable string section, but is not always so
1813 // marked by compilers. Mark manually here so we can optimize.
1814 if (strcmp(secname, ".debug_str") == 0)
1816 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1817 entsize = 1;
1820 // If this is a SHF_MERGE section, we pass all the input sections to
1821 // a Output_data_merge. We don't try to handle relocations for such
1822 // a section.
1823 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1824 && reloc_shndx == 0)
1826 if (this->add_merge_input_section(object, shndx, sh_flags,
1827 entsize, addralign))
1829 // Tell the relocation routines that they need to call the
1830 // output_offset method to determine the final address.
1831 return -1;
1835 off_t offset_in_section = this->current_data_size_for_child();
1836 off_t aligned_offset_in_section = align_address(offset_in_section,
1837 addralign);
1839 if (aligned_offset_in_section > offset_in_section
1840 && !have_sections_script
1841 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1842 && object->target()->has_code_fill())
1844 // We need to add some fill data. Using fill_list_ when
1845 // possible is an optimization, since we will often have fill
1846 // sections without input sections.
1847 off_t fill_len = aligned_offset_in_section - offset_in_section;
1848 if (this->input_sections_.empty())
1849 this->fills_.push_back(Fill(offset_in_section, fill_len));
1850 else
1852 // FIXME: When relaxing, the size needs to adjust to
1853 // maintain a constant alignment.
1854 std::string fill_data(object->target()->code_fill(fill_len));
1855 Output_data_const* odc = new Output_data_const(fill_data, 1);
1856 this->input_sections_.push_back(Input_section(odc));
1860 this->set_current_data_size_for_child(aligned_offset_in_section
1861 + shdr.get_sh_size());
1863 // We need to keep track of this section if we are already keeping
1864 // track of sections, or if we are relaxing. Also, if this is a
1865 // section which requires sorting, or which may require sorting in
1866 // the future, we keep track of the sections. FIXME: Add test for
1867 // relaxing.
1868 if (have_sections_script
1869 || !this->input_sections_.empty()
1870 || this->may_sort_attached_input_sections()
1871 || this->must_sort_attached_input_sections()
1872 || parameters->options().user_set_Map())
1873 this->input_sections_.push_back(Input_section(object, shndx,
1874 shdr.get_sh_size(),
1875 addralign));
1877 return aligned_offset_in_section;
1880 // Add arbitrary data to an output section.
1882 void
1883 Output_section::add_output_section_data(Output_section_data* posd)
1885 Input_section inp(posd);
1886 this->add_output_section_data(&inp);
1888 if (posd->is_data_size_valid())
1890 off_t offset_in_section = this->current_data_size_for_child();
1891 off_t aligned_offset_in_section = align_address(offset_in_section,
1892 posd->addralign());
1893 this->set_current_data_size_for_child(aligned_offset_in_section
1894 + posd->data_size());
1898 // Add arbitrary data to an output section by Input_section.
1900 void
1901 Output_section::add_output_section_data(Input_section* inp)
1903 if (this->input_sections_.empty())
1904 this->first_input_offset_ = this->current_data_size_for_child();
1906 this->input_sections_.push_back(*inp);
1908 uint64_t addralign = inp->addralign();
1909 if (addralign > this->addralign_)
1910 this->addralign_ = addralign;
1912 inp->set_output_section(this);
1915 // Add a merge section to an output section.
1917 void
1918 Output_section::add_output_merge_section(Output_section_data* posd,
1919 bool is_string, uint64_t entsize)
1921 Input_section inp(posd, is_string, entsize);
1922 this->add_output_section_data(&inp);
1925 // Add an input section to a SHF_MERGE section.
1927 bool
1928 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1929 uint64_t flags, uint64_t entsize,
1930 uint64_t addralign)
1932 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1934 // We only merge strings if the alignment is not more than the
1935 // character size. This could be handled, but it's unusual.
1936 if (is_string && addralign > entsize)
1937 return false;
1939 Input_section_list::iterator p;
1940 for (p = this->input_sections_.begin();
1941 p != this->input_sections_.end();
1942 ++p)
1943 if (p->is_merge_section(is_string, entsize, addralign))
1945 p->add_input_section(object, shndx);
1946 return true;
1949 // We handle the actual constant merging in Output_merge_data or
1950 // Output_merge_string_data.
1951 Output_section_data* posd;
1952 if (!is_string)
1953 posd = new Output_merge_data(entsize, addralign);
1954 else
1956 switch (entsize)
1958 case 1:
1959 posd = new Output_merge_string<char>(addralign);
1960 break;
1961 case 2:
1962 posd = new Output_merge_string<uint16_t>(addralign);
1963 break;
1964 case 4:
1965 posd = new Output_merge_string<uint32_t>(addralign);
1966 break;
1967 default:
1968 return false;
1972 this->add_output_merge_section(posd, is_string, entsize);
1973 posd->add_input_section(object, shndx);
1975 return true;
1978 // Given an address OFFSET relative to the start of input section
1979 // SHNDX in OBJECT, return whether this address is being included in
1980 // the final link. This should only be called if SHNDX in OBJECT has
1981 // a special mapping.
1983 bool
1984 Output_section::is_input_address_mapped(const Relobj* object,
1985 unsigned int shndx,
1986 off_t offset) const
1988 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1989 p != this->input_sections_.end();
1990 ++p)
1992 section_offset_type output_offset;
1993 if (p->output_offset(object, shndx, offset, &output_offset))
1994 return output_offset != -1;
1997 // By default we assume that the address is mapped. This should
1998 // only be called after we have passed all sections to Layout. At
1999 // that point we should know what we are discarding.
2000 return true;
2003 // Given an address OFFSET relative to the start of input section
2004 // SHNDX in object OBJECT, return the output offset relative to the
2005 // start of the input section in the output section. This should only
2006 // be called if SHNDX in OBJECT has a special mapping.
2008 section_offset_type
2009 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2010 section_offset_type offset) const
2012 // This can only be called meaningfully when layout is complete.
2013 gold_assert(Output_data::is_layout_complete());
2015 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2016 p != this->input_sections_.end();
2017 ++p)
2019 section_offset_type output_offset;
2020 if (p->output_offset(object, shndx, offset, &output_offset))
2021 return output_offset;
2023 gold_unreachable();
2026 // Return the output virtual address of OFFSET relative to the start
2027 // of input section SHNDX in object OBJECT.
2029 uint64_t
2030 Output_section::output_address(const Relobj* object, unsigned int shndx,
2031 off_t offset) const
2033 uint64_t addr = this->address() + this->first_input_offset_;
2034 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2035 p != this->input_sections_.end();
2036 ++p)
2038 addr = align_address(addr, p->addralign());
2039 section_offset_type output_offset;
2040 if (p->output_offset(object, shndx, offset, &output_offset))
2042 if (output_offset == -1)
2043 return -1U;
2044 return addr + output_offset;
2046 addr += p->data_size();
2049 // If we get here, it means that we don't know the mapping for this
2050 // input section. This might happen in principle if
2051 // add_input_section were called before add_output_section_data.
2052 // But it should never actually happen.
2054 gold_unreachable();
2057 // Return the output address of the start of the merged section for
2058 // input section SHNDX in object OBJECT.
2060 uint64_t
2061 Output_section::starting_output_address(const Relobj* object,
2062 unsigned int shndx) const
2064 uint64_t addr = this->address() + this->first_input_offset_;
2065 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2066 p != this->input_sections_.end();
2067 ++p)
2069 addr = align_address(addr, p->addralign());
2071 // It would be nice if we could use the existing output_offset
2072 // method to get the output offset of input offset 0.
2073 // Unfortunately we don't know for sure that input offset 0 is
2074 // mapped at all.
2075 if (p->is_merge_section_for(object, shndx))
2076 return addr;
2078 addr += p->data_size();
2080 gold_unreachable();
2083 // Set the data size of an Output_section. This is where we handle
2084 // setting the addresses of any Output_section_data objects.
2086 void
2087 Output_section::set_final_data_size()
2089 if (this->input_sections_.empty())
2091 this->set_data_size(this->current_data_size_for_child());
2092 return;
2095 if (this->must_sort_attached_input_sections())
2096 this->sort_attached_input_sections();
2098 uint64_t address = this->address();
2099 off_t startoff = this->offset();
2100 off_t off = startoff + this->first_input_offset_;
2101 for (Input_section_list::iterator p = this->input_sections_.begin();
2102 p != this->input_sections_.end();
2103 ++p)
2105 off = align_address(off, p->addralign());
2106 p->set_address_and_file_offset(address + (off - startoff), off,
2107 startoff);
2108 off += p->data_size();
2111 this->set_data_size(off - startoff);
2114 // Reset the address and file offset.
2116 void
2117 Output_section::do_reset_address_and_file_offset()
2119 for (Input_section_list::iterator p = this->input_sections_.begin();
2120 p != this->input_sections_.end();
2121 ++p)
2122 p->reset_address_and_file_offset();
2125 // Set the TLS offset. Called only for SHT_TLS sections.
2127 void
2128 Output_section::do_set_tls_offset(uint64_t tls_base)
2130 this->tls_offset_ = this->address() - tls_base;
2133 // In a few cases we need to sort the input sections attached to an
2134 // output section. This is used to implement the type of constructor
2135 // priority ordering implemented by the GNU linker, in which the
2136 // priority becomes part of the section name and the sections are
2137 // sorted by name. We only do this for an output section if we see an
2138 // attached input section matching ".ctor.*", ".dtor.*",
2139 // ".init_array.*" or ".fini_array.*".
2141 class Output_section::Input_section_sort_entry
2143 public:
2144 Input_section_sort_entry()
2145 : input_section_(), index_(-1U), section_has_name_(false),
2146 section_name_()
2149 Input_section_sort_entry(const Input_section& input_section,
2150 unsigned int index)
2151 : input_section_(input_section), index_(index),
2152 section_has_name_(input_section.is_input_section())
2154 if (this->section_has_name_)
2156 // This is only called single-threaded from Layout::finalize,
2157 // so it is OK to lock. Unfortunately we have no way to pass
2158 // in a Task token.
2159 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2160 Object* obj = input_section.relobj();
2161 Task_lock_obj<Object> tl(dummy_task, obj);
2163 // This is a slow operation, which should be cached in
2164 // Layout::layout if this becomes a speed problem.
2165 this->section_name_ = obj->section_name(input_section.shndx());
2169 // Return the Input_section.
2170 const Input_section&
2171 input_section() const
2173 gold_assert(this->index_ != -1U);
2174 return this->input_section_;
2177 // The index of this entry in the original list. This is used to
2178 // make the sort stable.
2179 unsigned int
2180 index() const
2182 gold_assert(this->index_ != -1U);
2183 return this->index_;
2186 // Whether there is a section name.
2187 bool
2188 section_has_name() const
2189 { return this->section_has_name_; }
2191 // The section name.
2192 const std::string&
2193 section_name() const
2195 gold_assert(this->section_has_name_);
2196 return this->section_name_;
2199 // Return true if the section name has a priority. This is assumed
2200 // to be true if it has a dot after the initial dot.
2201 bool
2202 has_priority() const
2204 gold_assert(this->section_has_name_);
2205 return this->section_name_.find('.', 1);
2208 // Return true if this an input file whose base name matches
2209 // FILE_NAME. The base name must have an extension of ".o", and
2210 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2211 // This is to match crtbegin.o as well as crtbeginS.o without
2212 // getting confused by other possibilities. Overall matching the
2213 // file name this way is a dreadful hack, but the GNU linker does it
2214 // in order to better support gcc, and we need to be compatible.
2215 bool
2216 match_file_name(const char* match_file_name) const
2218 const std::string& file_name(this->input_section_.relobj()->name());
2219 const char* base_name = lbasename(file_name.c_str());
2220 size_t match_len = strlen(match_file_name);
2221 if (strncmp(base_name, match_file_name, match_len) != 0)
2222 return false;
2223 size_t base_len = strlen(base_name);
2224 if (base_len != match_len + 2 && base_len != match_len + 3)
2225 return false;
2226 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2229 private:
2230 // The Input_section we are sorting.
2231 Input_section input_section_;
2232 // The index of this Input_section in the original list.
2233 unsigned int index_;
2234 // Whether this Input_section has a section name--it won't if this
2235 // is some random Output_section_data.
2236 bool section_has_name_;
2237 // The section name if there is one.
2238 std::string section_name_;
2241 // Return true if S1 should come before S2 in the output section.
2243 bool
2244 Output_section::Input_section_sort_compare::operator()(
2245 const Output_section::Input_section_sort_entry& s1,
2246 const Output_section::Input_section_sort_entry& s2) const
2248 // crtbegin.o must come first.
2249 bool s1_begin = s1.match_file_name("crtbegin");
2250 bool s2_begin = s2.match_file_name("crtbegin");
2251 if (s1_begin || s2_begin)
2253 if (!s1_begin)
2254 return false;
2255 if (!s2_begin)
2256 return true;
2257 return s1.index() < s2.index();
2260 // crtend.o must come last.
2261 bool s1_end = s1.match_file_name("crtend");
2262 bool s2_end = s2.match_file_name("crtend");
2263 if (s1_end || s2_end)
2265 if (!s1_end)
2266 return true;
2267 if (!s2_end)
2268 return false;
2269 return s1.index() < s2.index();
2272 // We sort all the sections with no names to the end.
2273 if (!s1.section_has_name() || !s2.section_has_name())
2275 if (s1.section_has_name())
2276 return true;
2277 if (s2.section_has_name())
2278 return false;
2279 return s1.index() < s2.index();
2282 // A section with a priority follows a section without a priority.
2283 // The GNU linker does this for all but .init_array sections; until
2284 // further notice we'll assume that that is an mistake.
2285 bool s1_has_priority = s1.has_priority();
2286 bool s2_has_priority = s2.has_priority();
2287 if (s1_has_priority && !s2_has_priority)
2288 return false;
2289 if (!s1_has_priority && s2_has_priority)
2290 return true;
2292 // Otherwise we sort by name.
2293 int compare = s1.section_name().compare(s2.section_name());
2294 if (compare != 0)
2295 return compare < 0;
2297 // Otherwise we keep the input order.
2298 return s1.index() < s2.index();
2301 // Sort the input sections attached to an output section.
2303 void
2304 Output_section::sort_attached_input_sections()
2306 if (this->attached_input_sections_are_sorted_)
2307 return;
2309 // The only thing we know about an input section is the object and
2310 // the section index. We need the section name. Recomputing this
2311 // is slow but this is an unusual case. If this becomes a speed
2312 // problem we can cache the names as required in Layout::layout.
2314 // We start by building a larger vector holding a copy of each
2315 // Input_section, plus its current index in the list and its name.
2316 std::vector<Input_section_sort_entry> sort_list;
2318 unsigned int i = 0;
2319 for (Input_section_list::iterator p = this->input_sections_.begin();
2320 p != this->input_sections_.end();
2321 ++p, ++i)
2322 sort_list.push_back(Input_section_sort_entry(*p, i));
2324 // Sort the input sections.
2325 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2327 // Copy the sorted input sections back to our list.
2328 this->input_sections_.clear();
2329 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2330 p != sort_list.end();
2331 ++p)
2332 this->input_sections_.push_back(p->input_section());
2334 // Remember that we sorted the input sections, since we might get
2335 // called again.
2336 this->attached_input_sections_are_sorted_ = true;
2339 // Write the section header to *OSHDR.
2341 template<int size, bool big_endian>
2342 void
2343 Output_section::write_header(const Layout* layout,
2344 const Stringpool* secnamepool,
2345 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2347 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2348 oshdr->put_sh_type(this->type_);
2350 elfcpp::Elf_Xword flags = this->flags_;
2351 if (this->info_section_ != NULL && this->info_uses_section_index_)
2352 flags |= elfcpp::SHF_INFO_LINK;
2353 oshdr->put_sh_flags(flags);
2355 oshdr->put_sh_addr(this->address());
2356 oshdr->put_sh_offset(this->offset());
2357 oshdr->put_sh_size(this->data_size());
2358 if (this->link_section_ != NULL)
2359 oshdr->put_sh_link(this->link_section_->out_shndx());
2360 else if (this->should_link_to_symtab_)
2361 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2362 else if (this->should_link_to_dynsym_)
2363 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2364 else
2365 oshdr->put_sh_link(this->link_);
2367 elfcpp::Elf_Word info;
2368 if (this->info_section_ != NULL)
2370 if (this->info_uses_section_index_)
2371 info = this->info_section_->out_shndx();
2372 else
2373 info = this->info_section_->symtab_index();
2375 else if (this->info_symndx_ != NULL)
2376 info = this->info_symndx_->symtab_index();
2377 else
2378 info = this->info_;
2379 oshdr->put_sh_info(info);
2381 oshdr->put_sh_addralign(this->addralign_);
2382 oshdr->put_sh_entsize(this->entsize_);
2385 // Write out the data. For input sections the data is written out by
2386 // Object::relocate, but we have to handle Output_section_data objects
2387 // here.
2389 void
2390 Output_section::do_write(Output_file* of)
2392 gold_assert(!this->requires_postprocessing());
2394 off_t output_section_file_offset = this->offset();
2395 for (Fill_list::iterator p = this->fills_.begin();
2396 p != this->fills_.end();
2397 ++p)
2399 std::string fill_data(parameters->target().code_fill(p->length()));
2400 of->write(output_section_file_offset + p->section_offset(),
2401 fill_data.data(), fill_data.size());
2404 for (Input_section_list::iterator p = this->input_sections_.begin();
2405 p != this->input_sections_.end();
2406 ++p)
2407 p->write(of);
2410 // If a section requires postprocessing, create the buffer to use.
2412 void
2413 Output_section::create_postprocessing_buffer()
2415 gold_assert(this->requires_postprocessing());
2417 if (this->postprocessing_buffer_ != NULL)
2418 return;
2420 if (!this->input_sections_.empty())
2422 off_t off = this->first_input_offset_;
2423 for (Input_section_list::iterator p = this->input_sections_.begin();
2424 p != this->input_sections_.end();
2425 ++p)
2427 off = align_address(off, p->addralign());
2428 p->finalize_data_size();
2429 off += p->data_size();
2431 this->set_current_data_size_for_child(off);
2434 off_t buffer_size = this->current_data_size_for_child();
2435 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2438 // Write all the data of an Output_section into the postprocessing
2439 // buffer. This is used for sections which require postprocessing,
2440 // such as compression. Input sections are handled by
2441 // Object::Relocate.
2443 void
2444 Output_section::write_to_postprocessing_buffer()
2446 gold_assert(this->requires_postprocessing());
2448 unsigned char* buffer = this->postprocessing_buffer();
2449 for (Fill_list::iterator p = this->fills_.begin();
2450 p != this->fills_.end();
2451 ++p)
2453 std::string fill_data(parameters->target().code_fill(p->length()));
2454 memcpy(buffer + p->section_offset(), fill_data.data(),
2455 fill_data.size());
2458 off_t off = this->first_input_offset_;
2459 for (Input_section_list::iterator p = this->input_sections_.begin();
2460 p != this->input_sections_.end();
2461 ++p)
2463 off = align_address(off, p->addralign());
2464 p->write_to_buffer(buffer + off);
2465 off += p->data_size();
2469 // Get the input sections for linker script processing. We leave
2470 // behind the Output_section_data entries. Note that this may be
2471 // slightly incorrect for merge sections. We will leave them behind,
2472 // but it is possible that the script says that they should follow
2473 // some other input sections, as in:
2474 // .rodata { *(.rodata) *(.rodata.cst*) }
2475 // For that matter, we don't handle this correctly:
2476 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2477 // With luck this will never matter.
2479 uint64_t
2480 Output_section::get_input_sections(
2481 uint64_t address,
2482 const std::string& fill,
2483 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2485 uint64_t orig_address = address;
2487 address = align_address(address, this->addralign());
2489 Input_section_list remaining;
2490 for (Input_section_list::iterator p = this->input_sections_.begin();
2491 p != this->input_sections_.end();
2492 ++p)
2494 if (p->is_input_section())
2495 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2496 else
2498 uint64_t aligned_address = align_address(address, p->addralign());
2499 if (aligned_address != address && !fill.empty())
2501 section_size_type length =
2502 convert_to_section_size_type(aligned_address - address);
2503 std::string this_fill;
2504 this_fill.reserve(length);
2505 while (this_fill.length() + fill.length() <= length)
2506 this_fill += fill;
2507 if (this_fill.length() < length)
2508 this_fill.append(fill, 0, length - this_fill.length());
2510 Output_section_data* posd = new Output_data_const(this_fill, 0);
2511 remaining.push_back(Input_section(posd));
2513 address = aligned_address;
2515 remaining.push_back(*p);
2517 p->finalize_data_size();
2518 address += p->data_size();
2522 this->input_sections_.swap(remaining);
2523 this->first_input_offset_ = 0;
2525 uint64_t data_size = address - orig_address;
2526 this->set_current_data_size_for_child(data_size);
2527 return data_size;
2530 // Add an input section from a script.
2532 void
2533 Output_section::add_input_section_for_script(Relobj* object,
2534 unsigned int shndx,
2535 off_t data_size,
2536 uint64_t addralign)
2538 if (addralign > this->addralign_)
2539 this->addralign_ = addralign;
2541 off_t offset_in_section = this->current_data_size_for_child();
2542 off_t aligned_offset_in_section = align_address(offset_in_section,
2543 addralign);
2545 this->set_current_data_size_for_child(aligned_offset_in_section
2546 + data_size);
2548 this->input_sections_.push_back(Input_section(object, shndx,
2549 data_size, addralign));
2552 // Print to the map file.
2554 void
2555 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
2557 mapfile->print_output_section(this);
2559 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2560 p != this->input_sections_.end();
2561 ++p)
2562 p->print_to_mapfile(mapfile);
2565 // Print stats for merge sections to stderr.
2567 void
2568 Output_section::print_merge_stats()
2570 Input_section_list::iterator p;
2571 for (p = this->input_sections_.begin();
2572 p != this->input_sections_.end();
2573 ++p)
2574 p->print_merge_stats(this->name_);
2577 // Output segment methods.
2579 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2580 : output_data_(),
2581 output_bss_(),
2582 vaddr_(0),
2583 paddr_(0),
2584 memsz_(0),
2585 max_align_(0),
2586 min_p_align_(0),
2587 offset_(0),
2588 filesz_(0),
2589 type_(type),
2590 flags_(flags),
2591 is_max_align_known_(false),
2592 are_addresses_set_(false)
2596 // Add an Output_section to an Output_segment.
2598 void
2599 Output_segment::add_output_section(Output_section* os,
2600 elfcpp::Elf_Word seg_flags)
2602 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
2603 gold_assert(!this->is_max_align_known_);
2605 // Update the segment flags.
2606 this->flags_ |= seg_flags;
2608 Output_segment::Output_data_list* pdl;
2609 if (os->type() == elfcpp::SHT_NOBITS)
2610 pdl = &this->output_bss_;
2611 else
2612 pdl = &this->output_data_;
2614 // So that PT_NOTE segments will work correctly, we need to ensure
2615 // that all SHT_NOTE sections are adjacent. This will normally
2616 // happen automatically, because all the SHT_NOTE input sections
2617 // will wind up in the same output section. However, it is possible
2618 // for multiple SHT_NOTE input sections to have different section
2619 // flags, and thus be in different output sections, but for the
2620 // different section flags to map into the same segment flags and
2621 // thus the same output segment.
2623 // Note that while there may be many input sections in an output
2624 // section, there are normally only a few output sections in an
2625 // output segment. This loop is expected to be fast.
2627 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
2629 Output_segment::Output_data_list::iterator p = pdl->end();
2632 --p;
2633 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2635 ++p;
2636 pdl->insert(p, os);
2637 return;
2640 while (p != pdl->begin());
2643 // Similarly, so that PT_TLS segments will work, we need to group
2644 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2645 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2646 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2647 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2648 // and the PT_TLS segment -- we do this grouping only for the
2649 // PT_LOAD segment.
2650 if (this->type_ != elfcpp::PT_TLS
2651 && (os->flags() & elfcpp::SHF_TLS) != 0
2652 && !this->output_data_.empty())
2654 pdl = &this->output_data_;
2655 bool nobits = os->type() == elfcpp::SHT_NOBITS;
2656 bool sawtls = false;
2657 Output_segment::Output_data_list::iterator p = pdl->end();
2660 --p;
2661 bool insert;
2662 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2664 sawtls = true;
2665 // Put a NOBITS section after the first TLS section.
2666 // Put a PROGBITS section after the first TLS/PROGBITS
2667 // section.
2668 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2670 else
2672 // If we've gone past the TLS sections, but we've seen a
2673 // TLS section, then we need to insert this section now.
2674 insert = sawtls;
2677 if (insert)
2679 ++p;
2680 pdl->insert(p, os);
2681 return;
2684 while (p != pdl->begin());
2686 // There are no TLS sections yet; put this one at the requested
2687 // location in the section list.
2690 // For the PT_GNU_RELRO segment, we need to group relro sections,
2691 // and we need to put them before any non-relro sections. Also,
2692 // relro local sections go before relro non-local sections.
2693 if (parameters->options().relro() && os->is_relro())
2695 gold_assert(pdl == &this->output_data_);
2696 Output_segment::Output_data_list::iterator p;
2697 for (p = pdl->begin(); p != pdl->end(); ++p)
2699 if (!(*p)->is_section())
2700 break;
2702 Output_section* pos = (*p)->output_section();
2703 if (!pos->is_relro()
2704 || (os->is_relro_local() && !pos->is_relro_local()))
2705 break;
2708 pdl->insert(p, os);
2709 return;
2712 pdl->push_back(os);
2715 // Remove an Output_section from this segment. It is an error if it
2716 // is not present.
2718 void
2719 Output_segment::remove_output_section(Output_section* os)
2721 // We only need this for SHT_PROGBITS.
2722 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2723 for (Output_data_list::iterator p = this->output_data_.begin();
2724 p != this->output_data_.end();
2725 ++p)
2727 if (*p == os)
2729 this->output_data_.erase(p);
2730 return;
2733 gold_unreachable();
2736 // Add an Output_data (which is not an Output_section) to the start of
2737 // a segment.
2739 void
2740 Output_segment::add_initial_output_data(Output_data* od)
2742 gold_assert(!this->is_max_align_known_);
2743 this->output_data_.push_front(od);
2746 // Return whether the first data section is a relro section.
2748 bool
2749 Output_segment::is_first_section_relro() const
2751 return (!this->output_data_.empty()
2752 && this->output_data_.front()->is_section()
2753 && this->output_data_.front()->output_section()->is_relro());
2756 // Return the maximum alignment of the Output_data in Output_segment.
2758 uint64_t
2759 Output_segment::maximum_alignment()
2761 if (!this->is_max_align_known_)
2763 uint64_t addralign;
2765 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2766 if (addralign > this->max_align_)
2767 this->max_align_ = addralign;
2769 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2770 if (addralign > this->max_align_)
2771 this->max_align_ = addralign;
2773 // If -z relro is in effect, and the first section in this
2774 // segment is a relro section, then the segment must be aligned
2775 // to at least the common page size. This ensures that the
2776 // PT_GNU_RELRO segment will start at a page boundary.
2777 if (parameters->options().relro() && this->is_first_section_relro())
2779 addralign = parameters->target().common_pagesize();
2780 if (addralign > this->max_align_)
2781 this->max_align_ = addralign;
2784 this->is_max_align_known_ = true;
2787 return this->max_align_;
2790 // Return the maximum alignment of a list of Output_data.
2792 uint64_t
2793 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
2795 uint64_t ret = 0;
2796 for (Output_data_list::const_iterator p = pdl->begin();
2797 p != pdl->end();
2798 ++p)
2800 uint64_t addralign = (*p)->addralign();
2801 if (addralign > ret)
2802 ret = addralign;
2804 return ret;
2807 // Return the number of dynamic relocs applied to this segment.
2809 unsigned int
2810 Output_segment::dynamic_reloc_count() const
2812 return (this->dynamic_reloc_count_list(&this->output_data_)
2813 + this->dynamic_reloc_count_list(&this->output_bss_));
2816 // Return the number of dynamic relocs applied to an Output_data_list.
2818 unsigned int
2819 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2821 unsigned int count = 0;
2822 for (Output_data_list::const_iterator p = pdl->begin();
2823 p != pdl->end();
2824 ++p)
2825 count += (*p)->dynamic_reloc_count();
2826 return count;
2829 // Set the section addresses for an Output_segment. If RESET is true,
2830 // reset the addresses first. ADDR is the address and *POFF is the
2831 // file offset. Set the section indexes starting with *PSHNDX.
2832 // Return the address of the immediately following segment. Update
2833 // *POFF and *PSHNDX.
2835 uint64_t
2836 Output_segment::set_section_addresses(const Layout* layout, bool reset,
2837 uint64_t addr, off_t* poff,
2838 unsigned int* pshndx)
2840 gold_assert(this->type_ == elfcpp::PT_LOAD);
2842 if (!reset && this->are_addresses_set_)
2844 gold_assert(this->paddr_ == addr);
2845 addr = this->vaddr_;
2847 else
2849 this->vaddr_ = addr;
2850 this->paddr_ = addr;
2851 this->are_addresses_set_ = true;
2854 bool in_tls = false;
2856 bool in_relro = (parameters->options().relro()
2857 && this->is_first_section_relro());
2859 off_t orig_off = *poff;
2860 this->offset_ = orig_off;
2862 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
2863 addr, poff, pshndx, &in_tls,
2864 &in_relro);
2865 this->filesz_ = *poff - orig_off;
2867 off_t off = *poff;
2869 uint64_t ret = this->set_section_list_addresses(layout, reset,
2870 &this->output_bss_,
2871 addr, poff, pshndx,
2872 &in_tls, &in_relro);
2874 // If the last section was a TLS section, align upward to the
2875 // alignment of the TLS segment, so that the overall size of the TLS
2876 // segment is aligned.
2877 if (in_tls)
2879 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2880 *poff = align_address(*poff, segment_align);
2883 // If all the sections were relro sections, align upward to the
2884 // common page size.
2885 if (in_relro)
2887 uint64_t page_align = parameters->target().common_pagesize();
2888 *poff = align_address(*poff, page_align);
2891 this->memsz_ = *poff - orig_off;
2893 // Ignore the file offset adjustments made by the BSS Output_data
2894 // objects.
2895 *poff = off;
2897 return ret;
2900 // Set the addresses and file offsets in a list of Output_data
2901 // structures.
2903 uint64_t
2904 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
2905 Output_data_list* pdl,
2906 uint64_t addr, off_t* poff,
2907 unsigned int* pshndx,
2908 bool* in_tls, bool* in_relro)
2910 off_t startoff = *poff;
2912 off_t off = startoff;
2913 for (Output_data_list::iterator p = pdl->begin();
2914 p != pdl->end();
2915 ++p)
2917 if (reset)
2918 (*p)->reset_address_and_file_offset();
2920 // When using a linker script the section will most likely
2921 // already have an address.
2922 if (!(*p)->is_address_valid())
2924 uint64_t align = (*p)->addralign();
2926 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2928 // Give the first TLS section the alignment of the
2929 // entire TLS segment. Otherwise the TLS segment as a
2930 // whole may be misaligned.
2931 if (!*in_tls)
2933 Output_segment* tls_segment = layout->tls_segment();
2934 gold_assert(tls_segment != NULL);
2935 uint64_t segment_align = tls_segment->maximum_alignment();
2936 gold_assert(segment_align >= align);
2937 align = segment_align;
2939 *in_tls = true;
2942 else
2944 // If this is the first section after the TLS segment,
2945 // align it to at least the alignment of the TLS
2946 // segment, so that the size of the overall TLS segment
2947 // is aligned.
2948 if (*in_tls)
2950 uint64_t segment_align =
2951 layout->tls_segment()->maximum_alignment();
2952 if (segment_align > align)
2953 align = segment_align;
2955 *in_tls = false;
2959 // If this is a non-relro section after a relro section,
2960 // align it to a common page boundary so that the dynamic
2961 // linker has a page to mark as read-only.
2962 if (*in_relro
2963 && (!(*p)->is_section()
2964 || !(*p)->output_section()->is_relro()))
2966 uint64_t page_align = parameters->target().common_pagesize();
2967 if (page_align > align)
2968 align = page_align;
2969 *in_relro = false;
2972 off = align_address(off, align);
2973 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2975 else
2977 // The script may have inserted a skip forward, but it
2978 // better not have moved backward.
2979 gold_assert((*p)->address() >= addr + (off - startoff));
2980 off += (*p)->address() - (addr + (off - startoff));
2981 (*p)->set_file_offset(off);
2982 (*p)->finalize_data_size();
2985 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2986 // section. Such a section does not affect the size of a
2987 // PT_LOAD segment.
2988 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2989 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2990 off += (*p)->data_size();
2992 if ((*p)->is_section())
2994 (*p)->set_out_shndx(*pshndx);
2995 ++*pshndx;
2999 *poff = off;
3000 return addr + (off - startoff);
3003 // For a non-PT_LOAD segment, set the offset from the sections, if
3004 // any.
3006 void
3007 Output_segment::set_offset()
3009 gold_assert(this->type_ != elfcpp::PT_LOAD);
3011 gold_assert(!this->are_addresses_set_);
3013 if (this->output_data_.empty() && this->output_bss_.empty())
3015 this->vaddr_ = 0;
3016 this->paddr_ = 0;
3017 this->are_addresses_set_ = true;
3018 this->memsz_ = 0;
3019 this->min_p_align_ = 0;
3020 this->offset_ = 0;
3021 this->filesz_ = 0;
3022 return;
3025 const Output_data* first;
3026 if (this->output_data_.empty())
3027 first = this->output_bss_.front();
3028 else
3029 first = this->output_data_.front();
3030 this->vaddr_ = first->address();
3031 this->paddr_ = (first->has_load_address()
3032 ? first->load_address()
3033 : this->vaddr_);
3034 this->are_addresses_set_ = true;
3035 this->offset_ = first->offset();
3037 if (this->output_data_.empty())
3038 this->filesz_ = 0;
3039 else
3041 const Output_data* last_data = this->output_data_.back();
3042 this->filesz_ = (last_data->address()
3043 + last_data->data_size()
3044 - this->vaddr_);
3047 const Output_data* last;
3048 if (this->output_bss_.empty())
3049 last = this->output_data_.back();
3050 else
3051 last = this->output_bss_.back();
3052 this->memsz_ = (last->address()
3053 + last->data_size()
3054 - this->vaddr_);
3056 // If this is a TLS segment, align the memory size. The code in
3057 // set_section_list ensures that the section after the TLS segment
3058 // is aligned to give us room.
3059 if (this->type_ == elfcpp::PT_TLS)
3061 uint64_t segment_align = this->maximum_alignment();
3062 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3063 this->memsz_ = align_address(this->memsz_, segment_align);
3066 // If this is a RELRO segment, align the memory size. The code in
3067 // set_section_list ensures that the section after the RELRO segment
3068 // is aligned to give us room.
3069 if (this->type_ == elfcpp::PT_GNU_RELRO)
3071 uint64_t page_align = parameters->target().common_pagesize();
3072 gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3073 this->memsz_ = align_address(this->memsz_, page_align);
3077 // Set the TLS offsets of the sections in the PT_TLS segment.
3079 void
3080 Output_segment::set_tls_offsets()
3082 gold_assert(this->type_ == elfcpp::PT_TLS);
3084 for (Output_data_list::iterator p = this->output_data_.begin();
3085 p != this->output_data_.end();
3086 ++p)
3087 (*p)->set_tls_offset(this->vaddr_);
3089 for (Output_data_list::iterator p = this->output_bss_.begin();
3090 p != this->output_bss_.end();
3091 ++p)
3092 (*p)->set_tls_offset(this->vaddr_);
3095 // Return the address of the first section.
3097 uint64_t
3098 Output_segment::first_section_load_address() const
3100 for (Output_data_list::const_iterator p = this->output_data_.begin();
3101 p != this->output_data_.end();
3102 ++p)
3103 if ((*p)->is_section())
3104 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3106 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3107 p != this->output_bss_.end();
3108 ++p)
3109 if ((*p)->is_section())
3110 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3112 gold_unreachable();
3115 // Return the number of Output_sections in an Output_segment.
3117 unsigned int
3118 Output_segment::output_section_count() const
3120 return (this->output_section_count_list(&this->output_data_)
3121 + this->output_section_count_list(&this->output_bss_));
3124 // Return the number of Output_sections in an Output_data_list.
3126 unsigned int
3127 Output_segment::output_section_count_list(const Output_data_list* pdl) const
3129 unsigned int count = 0;
3130 for (Output_data_list::const_iterator p = pdl->begin();
3131 p != pdl->end();
3132 ++p)
3134 if ((*p)->is_section())
3135 ++count;
3137 return count;
3140 // Return the section attached to the list segment with the lowest
3141 // load address. This is used when handling a PHDRS clause in a
3142 // linker script.
3144 Output_section*
3145 Output_segment::section_with_lowest_load_address() const
3147 Output_section* found = NULL;
3148 uint64_t found_lma = 0;
3149 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3151 Output_section* found_data = found;
3152 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3153 if (found != found_data && found_data != NULL)
3155 gold_error(_("nobits section %s may not precede progbits section %s "
3156 "in same segment"),
3157 found->name(), found_data->name());
3158 return NULL;
3161 return found;
3164 // Look through a list for a section with a lower load address.
3166 void
3167 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3168 Output_section** found,
3169 uint64_t* found_lma) const
3171 for (Output_data_list::const_iterator p = pdl->begin();
3172 p != pdl->end();
3173 ++p)
3175 if (!(*p)->is_section())
3176 continue;
3177 Output_section* os = static_cast<Output_section*>(*p);
3178 uint64_t lma = (os->has_load_address()
3179 ? os->load_address()
3180 : os->address());
3181 if (*found == NULL || lma < *found_lma)
3183 *found = os;
3184 *found_lma = lma;
3189 // Write the segment data into *OPHDR.
3191 template<int size, bool big_endian>
3192 void
3193 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
3195 ophdr->put_p_type(this->type_);
3196 ophdr->put_p_offset(this->offset_);
3197 ophdr->put_p_vaddr(this->vaddr_);
3198 ophdr->put_p_paddr(this->paddr_);
3199 ophdr->put_p_filesz(this->filesz_);
3200 ophdr->put_p_memsz(this->memsz_);
3201 ophdr->put_p_flags(this->flags_);
3202 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
3205 // Write the section headers into V.
3207 template<int size, bool big_endian>
3208 unsigned char*
3209 Output_segment::write_section_headers(const Layout* layout,
3210 const Stringpool* secnamepool,
3211 unsigned char* v,
3212 unsigned int *pshndx) const
3214 // Every section that is attached to a segment must be attached to a
3215 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3216 // segments.
3217 if (this->type_ != elfcpp::PT_LOAD)
3218 return v;
3220 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3221 &this->output_data_,
3222 v, pshndx);
3223 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3224 &this->output_bss_,
3225 v, pshndx);
3226 return v;
3229 template<int size, bool big_endian>
3230 unsigned char*
3231 Output_segment::write_section_headers_list(const Layout* layout,
3232 const Stringpool* secnamepool,
3233 const Output_data_list* pdl,
3234 unsigned char* v,
3235 unsigned int* pshndx) const
3237 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3238 for (Output_data_list::const_iterator p = pdl->begin();
3239 p != pdl->end();
3240 ++p)
3242 if ((*p)->is_section())
3244 const Output_section* ps = static_cast<const Output_section*>(*p);
3245 gold_assert(*pshndx == ps->out_shndx());
3246 elfcpp::Shdr_write<size, big_endian> oshdr(v);
3247 ps->write_header(layout, secnamepool, &oshdr);
3248 v += shdr_size;
3249 ++*pshndx;
3252 return v;
3255 // Print the output sections to the map file.
3257 void
3258 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3260 if (this->type() != elfcpp::PT_LOAD)
3261 return;
3262 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3263 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3266 // Print an output section list to the map file.
3268 void
3269 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3270 const Output_data_list* pdl) const
3272 for (Output_data_list::const_iterator p = pdl->begin();
3273 p != pdl->end();
3274 ++p)
3275 (*p)->print_to_mapfile(mapfile);
3278 // Output_file methods.
3280 Output_file::Output_file(const char* name)
3281 : name_(name),
3282 o_(-1),
3283 file_size_(0),
3284 base_(NULL),
3285 map_is_anonymous_(false),
3286 is_temporary_(false)
3290 // Open the output file.
3292 void
3293 Output_file::open(off_t file_size)
3295 this->file_size_ = file_size;
3297 // Unlink the file first; otherwise the open() may fail if the file
3298 // is busy (e.g. it's an executable that's currently being executed).
3300 // However, the linker may be part of a system where a zero-length
3301 // file is created for it to write to, with tight permissions (gcc
3302 // 2.95 did something like this). Unlinking the file would work
3303 // around those permission controls, so we only unlink if the file
3304 // has a non-zero size. We also unlink only regular files to avoid
3305 // trouble with directories/etc.
3307 // If we fail, continue; this command is merely a best-effort attempt
3308 // to improve the odds for open().
3310 // We let the name "-" mean "stdout"
3311 if (!this->is_temporary_)
3313 if (strcmp(this->name_, "-") == 0)
3314 this->o_ = STDOUT_FILENO;
3315 else
3317 struct stat s;
3318 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
3319 unlink_if_ordinary(this->name_);
3321 int mode = parameters->options().relocatable() ? 0666 : 0777;
3322 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
3323 if (o < 0)
3324 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3325 this->o_ = o;
3329 this->map();
3332 // Resize the output file.
3334 void
3335 Output_file::resize(off_t file_size)
3337 // If the mmap is mapping an anonymous memory buffer, this is easy:
3338 // just mremap to the new size. If it's mapping to a file, we want
3339 // to unmap to flush to the file, then remap after growing the file.
3340 if (this->map_is_anonymous_)
3342 void* base = ::mremap(this->base_, this->file_size_, file_size,
3343 MREMAP_MAYMOVE);
3344 if (base == MAP_FAILED)
3345 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3346 this->base_ = static_cast<unsigned char*>(base);
3347 this->file_size_ = file_size;
3349 else
3351 this->unmap();
3352 this->file_size_ = file_size;
3353 this->map();
3357 // Map the file into memory.
3359 void
3360 Output_file::map()
3362 const int o = this->o_;
3364 // If the output file is not a regular file, don't try to mmap it;
3365 // instead, we'll mmap a block of memory (an anonymous buffer), and
3366 // then later write the buffer to the file.
3367 void* base;
3368 struct stat statbuf;
3369 if (o == STDOUT_FILENO || o == STDERR_FILENO
3370 || ::fstat(o, &statbuf) != 0
3371 || !S_ISREG(statbuf.st_mode)
3372 || this->is_temporary_)
3374 this->map_is_anonymous_ = true;
3375 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3376 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3378 else
3380 // Write out one byte to make the file the right size.
3381 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
3382 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
3383 char b = 0;
3384 if (::write(o, &b, 1) != 1)
3385 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
3387 // Map the file into memory.
3388 this->map_is_anonymous_ = false;
3389 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3390 MAP_SHARED, o, 0);
3392 if (base == MAP_FAILED)
3393 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
3394 this->base_ = static_cast<unsigned char*>(base);
3397 // Unmap the file from memory.
3399 void
3400 Output_file::unmap()
3402 if (::munmap(this->base_, this->file_size_) < 0)
3403 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
3404 this->base_ = NULL;
3407 // Close the output file.
3409 void
3410 Output_file::close()
3412 // If the map isn't file-backed, we need to write it now.
3413 if (this->map_is_anonymous_ && !this->is_temporary_)
3415 size_t bytes_to_write = this->file_size_;
3416 while (bytes_to_write > 0)
3418 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
3419 if (bytes_written == 0)
3420 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
3421 else if (bytes_written < 0)
3422 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
3423 else
3424 bytes_to_write -= bytes_written;
3427 this->unmap();
3429 // We don't close stdout or stderr
3430 if (this->o_ != STDOUT_FILENO
3431 && this->o_ != STDERR_FILENO
3432 && !this->is_temporary_)
3433 if (::close(this->o_) < 0)
3434 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
3435 this->o_ = -1;
3438 // Instantiate the templates we need. We could use the configure
3439 // script to restrict this to only the ones for implemented targets.
3441 #ifdef HAVE_TARGET_32_LITTLE
3442 template
3443 off_t
3444 Output_section::add_input_section<32, false>(
3445 Sized_relobj<32, false>* object,
3446 unsigned int shndx,
3447 const char* secname,
3448 const elfcpp::Shdr<32, false>& shdr,
3449 unsigned int reloc_shndx,
3450 bool have_sections_script);
3451 #endif
3453 #ifdef HAVE_TARGET_32_BIG
3454 template
3455 off_t
3456 Output_section::add_input_section<32, true>(
3457 Sized_relobj<32, true>* object,
3458 unsigned int shndx,
3459 const char* secname,
3460 const elfcpp::Shdr<32, true>& shdr,
3461 unsigned int reloc_shndx,
3462 bool have_sections_script);
3463 #endif
3465 #ifdef HAVE_TARGET_64_LITTLE
3466 template
3467 off_t
3468 Output_section::add_input_section<64, false>(
3469 Sized_relobj<64, false>* object,
3470 unsigned int shndx,
3471 const char* secname,
3472 const elfcpp::Shdr<64, false>& shdr,
3473 unsigned int reloc_shndx,
3474 bool have_sections_script);
3475 #endif
3477 #ifdef HAVE_TARGET_64_BIG
3478 template
3479 off_t
3480 Output_section::add_input_section<64, true>(
3481 Sized_relobj<64, true>* object,
3482 unsigned int shndx,
3483 const char* secname,
3484 const elfcpp::Shdr<64, true>& shdr,
3485 unsigned int reloc_shndx,
3486 bool have_sections_script);
3487 #endif
3489 #ifdef HAVE_TARGET_32_LITTLE
3490 template
3491 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
3492 #endif
3494 #ifdef HAVE_TARGET_32_BIG
3495 template
3496 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
3497 #endif
3499 #ifdef HAVE_TARGET_64_LITTLE
3500 template
3501 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
3502 #endif
3504 #ifdef HAVE_TARGET_64_BIG
3505 template
3506 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
3507 #endif
3509 #ifdef HAVE_TARGET_32_LITTLE
3510 template
3511 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
3512 #endif
3514 #ifdef HAVE_TARGET_32_BIG
3515 template
3516 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
3517 #endif
3519 #ifdef HAVE_TARGET_64_LITTLE
3520 template
3521 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
3522 #endif
3524 #ifdef HAVE_TARGET_64_BIG
3525 template
3526 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
3527 #endif
3529 #ifdef HAVE_TARGET_32_LITTLE
3530 template
3531 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
3532 #endif
3534 #ifdef HAVE_TARGET_32_BIG
3535 template
3536 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
3537 #endif
3539 #ifdef HAVE_TARGET_64_LITTLE
3540 template
3541 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
3542 #endif
3544 #ifdef HAVE_TARGET_64_BIG
3545 template
3546 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
3547 #endif
3549 #ifdef HAVE_TARGET_32_LITTLE
3550 template
3551 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
3552 #endif
3554 #ifdef HAVE_TARGET_32_BIG
3555 template
3556 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
3557 #endif
3559 #ifdef HAVE_TARGET_64_LITTLE
3560 template
3561 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
3562 #endif
3564 #ifdef HAVE_TARGET_64_BIG
3565 template
3566 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
3567 #endif
3569 #ifdef HAVE_TARGET_32_LITTLE
3570 template
3571 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3572 #endif
3574 #ifdef HAVE_TARGET_32_BIG
3575 template
3576 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3577 #endif
3579 #ifdef HAVE_TARGET_64_LITTLE
3580 template
3581 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3582 #endif
3584 #ifdef HAVE_TARGET_64_BIG
3585 template
3586 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3587 #endif
3589 #ifdef HAVE_TARGET_32_LITTLE
3590 template
3591 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3592 #endif
3594 #ifdef HAVE_TARGET_32_BIG
3595 template
3596 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3597 #endif
3599 #ifdef HAVE_TARGET_64_LITTLE
3600 template
3601 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3602 #endif
3604 #ifdef HAVE_TARGET_64_BIG
3605 template
3606 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3607 #endif
3609 #ifdef HAVE_TARGET_32_LITTLE
3610 template
3611 class Output_data_group<32, false>;
3612 #endif
3614 #ifdef HAVE_TARGET_32_BIG
3615 template
3616 class Output_data_group<32, true>;
3617 #endif
3619 #ifdef HAVE_TARGET_64_LITTLE
3620 template
3621 class Output_data_group<64, false>;
3622 #endif
3624 #ifdef HAVE_TARGET_64_BIG
3625 template
3626 class Output_data_group<64, true>;
3627 #endif
3629 #ifdef HAVE_TARGET_32_LITTLE
3630 template
3631 class Output_data_got<32, false>;
3632 #endif
3634 #ifdef HAVE_TARGET_32_BIG
3635 template
3636 class Output_data_got<32, true>;
3637 #endif
3639 #ifdef HAVE_TARGET_64_LITTLE
3640 template
3641 class Output_data_got<64, false>;
3642 #endif
3644 #ifdef HAVE_TARGET_64_BIG
3645 template
3646 class Output_data_got<64, true>;
3647 #endif
3649 } // End namespace gold.