daily update
[binutils.git] / gold / output.cc
bloba3dab399ad56c4ddca15dabe9f801074adfb4ac8
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 Relobj* 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 Relobj* 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
759 section_offset_type dummy;
760 Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
761 gold_assert(os != NULL);
762 os->set_needs_dynsym_index();
765 break;
769 // Get the symbol index of a relocation.
771 template<bool dynamic, int size, bool big_endian>
772 unsigned int
773 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
774 const
776 unsigned int index;
777 switch (this->local_sym_index_)
779 case INVALID_CODE:
780 gold_unreachable();
782 case GSYM_CODE:
783 if (this->u1_.gsym == NULL)
784 index = 0;
785 else if (dynamic)
786 index = this->u1_.gsym->dynsym_index();
787 else
788 index = this->u1_.gsym->symtab_index();
789 break;
791 case SECTION_CODE:
792 if (dynamic)
793 index = this->u1_.os->dynsym_index();
794 else
795 index = this->u1_.os->symtab_index();
796 break;
798 case 0:
799 // Relocations without symbols use a symbol index of 0.
800 index = 0;
801 break;
803 default:
805 const unsigned int lsi = this->local_sym_index_;
806 if (!this->is_section_symbol_)
808 if (dynamic)
809 index = this->u1_.relobj->dynsym_index(lsi);
810 else
811 index = this->u1_.relobj->symtab_index(lsi);
813 else
815 section_offset_type dummy;
816 Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
817 gold_assert(os != NULL);
818 if (dynamic)
819 index = os->dynsym_index();
820 else
821 index = os->symtab_index();
824 break;
826 gold_assert(index != -1U);
827 return index;
830 // For a local section symbol, get the address of the offset ADDEND
831 // within the input section.
833 template<bool dynamic, int size, bool big_endian>
834 section_offset_type
835 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
836 local_section_offset(Addend addend) const
838 gold_assert(this->local_sym_index_ != GSYM_CODE
839 && this->local_sym_index_ != SECTION_CODE
840 && this->local_sym_index_ != INVALID_CODE
841 && this->is_section_symbol_);
842 const unsigned int lsi = this->local_sym_index_;
843 section_offset_type offset;
844 Output_section* os = this->u1_.relobj->output_section(lsi, &offset);
845 gold_assert(os != NULL);
846 if (offset != -1)
847 return offset + addend;
848 // This is a merge section.
849 offset = os->output_address(this->u1_.relobj, lsi, addend);
850 gold_assert(offset != -1);
851 return offset;
854 // Get the output address of a relocation.
856 template<bool dynamic, int size, bool big_endian>
857 typename elfcpp::Elf_types<size>::Elf_Addr
858 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
860 Address address = this->address_;
861 if (this->shndx_ != INVALID_CODE)
863 section_offset_type off;
864 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
865 &off);
866 gold_assert(os != NULL);
867 if (off != -1)
868 address += os->address() + off;
869 else
871 address = os->output_address(this->u2_.relobj, this->shndx_,
872 address);
873 gold_assert(address != -1U);
876 else if (this->u2_.od != NULL)
877 address += this->u2_.od->address();
878 return address;
881 // Write out the offset and info fields of a Rel or Rela relocation
882 // entry.
884 template<bool dynamic, int size, bool big_endian>
885 template<typename Write_rel>
886 void
887 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
888 Write_rel* wr) const
890 wr->put_r_offset(this->get_address());
891 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
892 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
895 // Write out a Rel relocation.
897 template<bool dynamic, int size, bool big_endian>
898 void
899 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
900 unsigned char* pov) const
902 elfcpp::Rel_write<size, big_endian> orel(pov);
903 this->write_rel(&orel);
906 // Get the value of the symbol referred to by a Rel relocation.
908 template<bool dynamic, int size, bool big_endian>
909 typename elfcpp::Elf_types<size>::Elf_Addr
910 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
911 Addend addend) const
913 if (this->local_sym_index_ == GSYM_CODE)
915 const Sized_symbol<size>* sym;
916 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
917 return sym->value() + addend;
919 gold_assert(this->local_sym_index_ != SECTION_CODE
920 && this->local_sym_index_ != INVALID_CODE
921 && !this->is_section_symbol_);
922 const unsigned int lsi = this->local_sym_index_;
923 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
924 return symval->value(this->u1_.relobj, addend);
927 // Reloc comparison. This function sorts the dynamic relocs for the
928 // benefit of the dynamic linker. First we sort all relative relocs
929 // to the front. Among relative relocs, we sort by output address.
930 // Among non-relative relocs, we sort by symbol index, then by output
931 // address.
933 template<bool dynamic, int size, bool big_endian>
935 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
936 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
937 const
939 if (this->is_relative_)
941 if (!r2.is_relative_)
942 return -1;
943 // Otherwise sort by reloc address below.
945 else if (r2.is_relative_)
946 return 1;
947 else
949 unsigned int sym1 = this->get_symbol_index();
950 unsigned int sym2 = r2.get_symbol_index();
951 if (sym1 < sym2)
952 return -1;
953 else if (sym1 > sym2)
954 return 1;
955 // Otherwise sort by reloc address.
958 section_offset_type addr1 = this->get_address();
959 section_offset_type addr2 = r2.get_address();
960 if (addr1 < addr2)
961 return -1;
962 else if (addr1 > addr2)
963 return 1;
965 // Final tie breaker, in order to generate the same output on any
966 // host: reloc type.
967 unsigned int type1 = this->type_;
968 unsigned int type2 = r2.type_;
969 if (type1 < type2)
970 return -1;
971 else if (type1 > type2)
972 return 1;
974 // These relocs appear to be exactly the same.
975 return 0;
978 // Write out a Rela relocation.
980 template<bool dynamic, int size, bool big_endian>
981 void
982 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
983 unsigned char* pov) const
985 elfcpp::Rela_write<size, big_endian> orel(pov);
986 this->rel_.write_rel(&orel);
987 Addend addend = this->addend_;
988 if (this->rel_.is_relative())
989 addend = this->rel_.symbol_value(addend);
990 else if (this->rel_.is_local_section_symbol())
991 addend = this->rel_.local_section_offset(addend);
992 orel.put_r_addend(addend);
995 // Output_data_reloc_base methods.
997 // Adjust the output section.
999 template<int sh_type, bool dynamic, int size, bool big_endian>
1000 void
1001 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1002 ::do_adjust_output_section(Output_section* os)
1004 if (sh_type == elfcpp::SHT_REL)
1005 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1006 else if (sh_type == elfcpp::SHT_RELA)
1007 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1008 else
1009 gold_unreachable();
1010 if (dynamic)
1011 os->set_should_link_to_dynsym();
1012 else
1013 os->set_should_link_to_symtab();
1016 // Write out relocation data.
1018 template<int sh_type, bool dynamic, int size, bool big_endian>
1019 void
1020 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1021 Output_file* of)
1023 const off_t off = this->offset();
1024 const off_t oview_size = this->data_size();
1025 unsigned char* const oview = of->get_output_view(off, oview_size);
1027 if (this->sort_relocs_)
1029 gold_assert(dynamic);
1030 std::sort(this->relocs_.begin(), this->relocs_.end(),
1031 Sort_relocs_comparison());
1034 unsigned char* pov = oview;
1035 for (typename Relocs::const_iterator p = this->relocs_.begin();
1036 p != this->relocs_.end();
1037 ++p)
1039 p->write(pov);
1040 pov += reloc_size;
1043 gold_assert(pov - oview == oview_size);
1045 of->write_output_view(off, oview_size, oview);
1047 // We no longer need the relocation entries.
1048 this->relocs_.clear();
1051 // Class Output_relocatable_relocs.
1053 template<int sh_type, int size, bool big_endian>
1054 void
1055 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1057 this->set_data_size(this->rr_->output_reloc_count()
1058 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1061 // class Output_data_group.
1063 template<int size, bool big_endian>
1064 Output_data_group<size, big_endian>::Output_data_group(
1065 Sized_relobj<size, big_endian>* relobj,
1066 section_size_type entry_count,
1067 elfcpp::Elf_Word flags,
1068 std::vector<unsigned int>* input_shndxes)
1069 : Output_section_data(entry_count * 4, 4),
1070 relobj_(relobj),
1071 flags_(flags)
1073 this->input_shndxes_.swap(*input_shndxes);
1076 // Write out the section group, which means translating the section
1077 // indexes to apply to the output file.
1079 template<int size, bool big_endian>
1080 void
1081 Output_data_group<size, big_endian>::do_write(Output_file* of)
1083 const off_t off = this->offset();
1084 const section_size_type oview_size =
1085 convert_to_section_size_type(this->data_size());
1086 unsigned char* const oview = of->get_output_view(off, oview_size);
1088 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1089 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1090 ++contents;
1092 for (std::vector<unsigned int>::const_iterator p =
1093 this->input_shndxes_.begin();
1094 p != this->input_shndxes_.end();
1095 ++p, ++contents)
1097 section_offset_type dummy;
1098 Output_section* os = this->relobj_->output_section(*p, &dummy);
1100 unsigned int output_shndx;
1101 if (os != NULL)
1102 output_shndx = os->out_shndx();
1103 else
1105 this->relobj_->error(_("section group retained but "
1106 "group element discarded"));
1107 output_shndx = 0;
1110 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1113 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1114 gold_assert(wrote == oview_size);
1116 of->write_output_view(off, oview_size, oview);
1118 // We no longer need this information.
1119 this->input_shndxes_.clear();
1122 // Output_data_got::Got_entry methods.
1124 // Write out the entry.
1126 template<int size, bool big_endian>
1127 void
1128 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1130 Valtype val = 0;
1132 switch (this->local_sym_index_)
1134 case GSYM_CODE:
1136 // If the symbol is resolved locally, we need to write out the
1137 // link-time value, which will be relocated dynamically by a
1138 // RELATIVE relocation.
1139 Symbol* gsym = this->u_.gsym;
1140 Sized_symbol<size>* sgsym;
1141 // This cast is a bit ugly. We don't want to put a
1142 // virtual method in Symbol, because we want Symbol to be
1143 // as small as possible.
1144 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1145 val = sgsym->value();
1147 break;
1149 case CONSTANT_CODE:
1150 val = this->u_.constant;
1151 break;
1153 default:
1155 const unsigned int lsi = this->local_sym_index_;
1156 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1157 val = symval->value(this->u_.object, 0);
1159 break;
1162 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1165 // Output_data_got methods.
1167 // Add an entry for a global symbol to the GOT. This returns true if
1168 // this is a new GOT entry, false if the symbol already had a GOT
1169 // entry.
1171 template<int size, bool big_endian>
1172 bool
1173 Output_data_got<size, big_endian>::add_global(
1174 Symbol* gsym,
1175 unsigned int got_type)
1177 if (gsym->has_got_offset(got_type))
1178 return false;
1180 this->entries_.push_back(Got_entry(gsym));
1181 this->set_got_size();
1182 gsym->set_got_offset(got_type, this->last_got_offset());
1183 return true;
1186 // Add an entry for a global symbol to the GOT, and add a dynamic
1187 // relocation of type R_TYPE for the GOT entry.
1188 template<int size, bool big_endian>
1189 void
1190 Output_data_got<size, big_endian>::add_global_with_rel(
1191 Symbol* gsym,
1192 unsigned int got_type,
1193 Rel_dyn* rel_dyn,
1194 unsigned int r_type)
1196 if (gsym->has_got_offset(got_type))
1197 return;
1199 this->entries_.push_back(Got_entry());
1200 this->set_got_size();
1201 unsigned int got_offset = this->last_got_offset();
1202 gsym->set_got_offset(got_type, got_offset);
1203 rel_dyn->add_global(gsym, r_type, this, got_offset);
1206 template<int size, bool big_endian>
1207 void
1208 Output_data_got<size, big_endian>::add_global_with_rela(
1209 Symbol* gsym,
1210 unsigned int got_type,
1211 Rela_dyn* rela_dyn,
1212 unsigned int r_type)
1214 if (gsym->has_got_offset(got_type))
1215 return;
1217 this->entries_.push_back(Got_entry());
1218 this->set_got_size();
1219 unsigned int got_offset = this->last_got_offset();
1220 gsym->set_got_offset(got_type, got_offset);
1221 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1224 // Add a pair of entries for a global symbol to the GOT, and add
1225 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1226 // If R_TYPE_2 == 0, add the second entry with no relocation.
1227 template<int size, bool big_endian>
1228 void
1229 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1230 Symbol* gsym,
1231 unsigned int got_type,
1232 Rel_dyn* rel_dyn,
1233 unsigned int r_type_1,
1234 unsigned int r_type_2)
1236 if (gsym->has_got_offset(got_type))
1237 return;
1239 this->entries_.push_back(Got_entry());
1240 unsigned int got_offset = this->last_got_offset();
1241 gsym->set_got_offset(got_type, got_offset);
1242 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1244 this->entries_.push_back(Got_entry());
1245 if (r_type_2 != 0)
1247 got_offset = this->last_got_offset();
1248 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1251 this->set_got_size();
1254 template<int size, bool big_endian>
1255 void
1256 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1257 Symbol* gsym,
1258 unsigned int got_type,
1259 Rela_dyn* rela_dyn,
1260 unsigned int r_type_1,
1261 unsigned int r_type_2)
1263 if (gsym->has_got_offset(got_type))
1264 return;
1266 this->entries_.push_back(Got_entry());
1267 unsigned int got_offset = this->last_got_offset();
1268 gsym->set_got_offset(got_type, got_offset);
1269 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1271 this->entries_.push_back(Got_entry());
1272 if (r_type_2 != 0)
1274 got_offset = this->last_got_offset();
1275 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1278 this->set_got_size();
1281 // Add an entry for a local symbol to the GOT. This returns true if
1282 // this is a new GOT entry, false if the symbol already has a GOT
1283 // entry.
1285 template<int size, bool big_endian>
1286 bool
1287 Output_data_got<size, big_endian>::add_local(
1288 Sized_relobj<size, big_endian>* object,
1289 unsigned int symndx,
1290 unsigned int got_type)
1292 if (object->local_has_got_offset(symndx, got_type))
1293 return false;
1295 this->entries_.push_back(Got_entry(object, symndx));
1296 this->set_got_size();
1297 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1298 return true;
1301 // Add an entry for a local symbol to the GOT, and add a dynamic
1302 // relocation of type R_TYPE for the GOT entry.
1303 template<int size, bool big_endian>
1304 void
1305 Output_data_got<size, big_endian>::add_local_with_rel(
1306 Sized_relobj<size, big_endian>* object,
1307 unsigned int symndx,
1308 unsigned int got_type,
1309 Rel_dyn* rel_dyn,
1310 unsigned int r_type)
1312 if (object->local_has_got_offset(symndx, got_type))
1313 return;
1315 this->entries_.push_back(Got_entry());
1316 this->set_got_size();
1317 unsigned int got_offset = this->last_got_offset();
1318 object->set_local_got_offset(symndx, got_type, got_offset);
1319 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1322 template<int size, bool big_endian>
1323 void
1324 Output_data_got<size, big_endian>::add_local_with_rela(
1325 Sized_relobj<size, big_endian>* object,
1326 unsigned int symndx,
1327 unsigned int got_type,
1328 Rela_dyn* rela_dyn,
1329 unsigned int r_type)
1331 if (object->local_has_got_offset(symndx, got_type))
1332 return;
1334 this->entries_.push_back(Got_entry());
1335 this->set_got_size();
1336 unsigned int got_offset = this->last_got_offset();
1337 object->set_local_got_offset(symndx, got_type, got_offset);
1338 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1341 // Add a pair of entries for a local symbol to the GOT, and add
1342 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1343 // If R_TYPE_2 == 0, add the second entry with no relocation.
1344 template<int size, bool big_endian>
1345 void
1346 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1347 Sized_relobj<size, big_endian>* object,
1348 unsigned int symndx,
1349 unsigned int shndx,
1350 unsigned int got_type,
1351 Rel_dyn* rel_dyn,
1352 unsigned int r_type_1,
1353 unsigned int r_type_2)
1355 if (object->local_has_got_offset(symndx, got_type))
1356 return;
1358 this->entries_.push_back(Got_entry());
1359 unsigned int got_offset = this->last_got_offset();
1360 object->set_local_got_offset(symndx, got_type, got_offset);
1361 section_offset_type off;
1362 Output_section* os = object->output_section(shndx, &off);
1363 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1365 this->entries_.push_back(Got_entry(object, symndx));
1366 if (r_type_2 != 0)
1368 got_offset = this->last_got_offset();
1369 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1372 this->set_got_size();
1375 template<int size, bool big_endian>
1376 void
1377 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1378 Sized_relobj<size, big_endian>* object,
1379 unsigned int symndx,
1380 unsigned int shndx,
1381 unsigned int got_type,
1382 Rela_dyn* rela_dyn,
1383 unsigned int r_type_1,
1384 unsigned int r_type_2)
1386 if (object->local_has_got_offset(symndx, got_type))
1387 return;
1389 this->entries_.push_back(Got_entry());
1390 unsigned int got_offset = this->last_got_offset();
1391 object->set_local_got_offset(symndx, got_type, got_offset);
1392 section_offset_type off;
1393 Output_section* os = object->output_section(shndx, &off);
1394 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1396 this->entries_.push_back(Got_entry(object, symndx));
1397 if (r_type_2 != 0)
1399 got_offset = this->last_got_offset();
1400 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1403 this->set_got_size();
1406 // Write out the GOT.
1408 template<int size, bool big_endian>
1409 void
1410 Output_data_got<size, big_endian>::do_write(Output_file* of)
1412 const int add = size / 8;
1414 const off_t off = this->offset();
1415 const off_t oview_size = this->data_size();
1416 unsigned char* const oview = of->get_output_view(off, oview_size);
1418 unsigned char* pov = oview;
1419 for (typename Got_entries::const_iterator p = this->entries_.begin();
1420 p != this->entries_.end();
1421 ++p)
1423 p->write(pov);
1424 pov += add;
1427 gold_assert(pov - oview == oview_size);
1429 of->write_output_view(off, oview_size, oview);
1431 // We no longer need the GOT entries.
1432 this->entries_.clear();
1435 // Output_data_dynamic::Dynamic_entry methods.
1437 // Write out the entry.
1439 template<int size, bool big_endian>
1440 void
1441 Output_data_dynamic::Dynamic_entry::write(
1442 unsigned char* pov,
1443 const Stringpool* pool) const
1445 typename elfcpp::Elf_types<size>::Elf_WXword val;
1446 switch (this->offset_)
1448 case DYNAMIC_NUMBER:
1449 val = this->u_.val;
1450 break;
1452 case DYNAMIC_SECTION_SIZE:
1453 val = this->u_.od->data_size();
1454 break;
1456 case DYNAMIC_SYMBOL:
1458 const Sized_symbol<size>* s =
1459 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1460 val = s->value();
1462 break;
1464 case DYNAMIC_STRING:
1465 val = pool->get_offset(this->u_.str);
1466 break;
1468 default:
1469 val = this->u_.od->address() + this->offset_;
1470 break;
1473 elfcpp::Dyn_write<size, big_endian> dw(pov);
1474 dw.put_d_tag(this->tag_);
1475 dw.put_d_val(val);
1478 // Output_data_dynamic methods.
1480 // Adjust the output section to set the entry size.
1482 void
1483 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1485 if (parameters->target().get_size() == 32)
1486 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1487 else if (parameters->target().get_size() == 64)
1488 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1489 else
1490 gold_unreachable();
1493 // Set the final data size.
1495 void
1496 Output_data_dynamic::set_final_data_size()
1498 // Add the terminating entry.
1499 this->add_constant(elfcpp::DT_NULL, 0);
1501 int dyn_size;
1502 if (parameters->target().get_size() == 32)
1503 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1504 else if (parameters->target().get_size() == 64)
1505 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1506 else
1507 gold_unreachable();
1508 this->set_data_size(this->entries_.size() * dyn_size);
1511 // Write out the dynamic entries.
1513 void
1514 Output_data_dynamic::do_write(Output_file* of)
1516 switch (parameters->size_and_endianness())
1518 #ifdef HAVE_TARGET_32_LITTLE
1519 case Parameters::TARGET_32_LITTLE:
1520 this->sized_write<32, false>(of);
1521 break;
1522 #endif
1523 #ifdef HAVE_TARGET_32_BIG
1524 case Parameters::TARGET_32_BIG:
1525 this->sized_write<32, true>(of);
1526 break;
1527 #endif
1528 #ifdef HAVE_TARGET_64_LITTLE
1529 case Parameters::TARGET_64_LITTLE:
1530 this->sized_write<64, false>(of);
1531 break;
1532 #endif
1533 #ifdef HAVE_TARGET_64_BIG
1534 case Parameters::TARGET_64_BIG:
1535 this->sized_write<64, true>(of);
1536 break;
1537 #endif
1538 default:
1539 gold_unreachable();
1543 template<int size, bool big_endian>
1544 void
1545 Output_data_dynamic::sized_write(Output_file* of)
1547 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1549 const off_t offset = this->offset();
1550 const off_t oview_size = this->data_size();
1551 unsigned char* const oview = of->get_output_view(offset, oview_size);
1553 unsigned char* pov = oview;
1554 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1555 p != this->entries_.end();
1556 ++p)
1558 p->write<size, big_endian>(pov, this->pool_);
1559 pov += dyn_size;
1562 gold_assert(pov - oview == oview_size);
1564 of->write_output_view(offset, oview_size, oview);
1566 // We no longer need the dynamic entries.
1567 this->entries_.clear();
1570 // Class Output_symtab_xindex.
1572 void
1573 Output_symtab_xindex::do_write(Output_file* of)
1575 const off_t offset = this->offset();
1576 const off_t oview_size = this->data_size();
1577 unsigned char* const oview = of->get_output_view(offset, oview_size);
1579 memset(oview, 0, oview_size);
1581 if (parameters->target().is_big_endian())
1582 this->endian_do_write<true>(oview);
1583 else
1584 this->endian_do_write<false>(oview);
1586 of->write_output_view(offset, oview_size, oview);
1588 // We no longer need the data.
1589 this->entries_.clear();
1592 template<bool big_endian>
1593 void
1594 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1596 for (Xindex_entries::const_iterator p = this->entries_.begin();
1597 p != this->entries_.end();
1598 ++p)
1599 elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
1602 // Output_section::Input_section methods.
1604 // Return the data size. For an input section we store the size here.
1605 // For an Output_section_data, we have to ask it for the size.
1607 off_t
1608 Output_section::Input_section::data_size() const
1610 if (this->is_input_section())
1611 return this->u1_.data_size;
1612 else
1613 return this->u2_.posd->data_size();
1616 // Set the address and file offset.
1618 void
1619 Output_section::Input_section::set_address_and_file_offset(
1620 uint64_t address,
1621 off_t file_offset,
1622 off_t section_file_offset)
1624 if (this->is_input_section())
1625 this->u2_.object->set_section_offset(this->shndx_,
1626 file_offset - section_file_offset);
1627 else
1628 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1631 // Reset the address and file offset.
1633 void
1634 Output_section::Input_section::reset_address_and_file_offset()
1636 if (!this->is_input_section())
1637 this->u2_.posd->reset_address_and_file_offset();
1640 // Finalize the data size.
1642 void
1643 Output_section::Input_section::finalize_data_size()
1645 if (!this->is_input_section())
1646 this->u2_.posd->finalize_data_size();
1649 // Try to turn an input offset into an output offset. We want to
1650 // return the output offset relative to the start of this
1651 // Input_section in the output section.
1653 inline bool
1654 Output_section::Input_section::output_offset(
1655 const Relobj* object,
1656 unsigned int shndx,
1657 section_offset_type offset,
1658 section_offset_type *poutput) const
1660 if (!this->is_input_section())
1661 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1662 else
1664 if (this->shndx_ != shndx || this->u2_.object != object)
1665 return false;
1666 *poutput = offset;
1667 return true;
1671 // Return whether this is the merge section for the input section
1672 // SHNDX in OBJECT.
1674 inline bool
1675 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1676 unsigned int shndx) const
1678 if (this->is_input_section())
1679 return false;
1680 return this->u2_.posd->is_merge_section_for(object, shndx);
1683 // Write out the data. We don't have to do anything for an input
1684 // section--they are handled via Object::relocate--but this is where
1685 // we write out the data for an Output_section_data.
1687 void
1688 Output_section::Input_section::write(Output_file* of)
1690 if (!this->is_input_section())
1691 this->u2_.posd->write(of);
1694 // Write the data to a buffer. As for write(), we don't have to do
1695 // anything for an input section.
1697 void
1698 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1700 if (!this->is_input_section())
1701 this->u2_.posd->write_to_buffer(buffer);
1704 // Print to a map file.
1706 void
1707 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1709 switch (this->shndx_)
1711 case OUTPUT_SECTION_CODE:
1712 case MERGE_DATA_SECTION_CODE:
1713 case MERGE_STRING_SECTION_CODE:
1714 this->u2_.posd->print_to_mapfile(mapfile);
1715 break;
1717 default:
1718 mapfile->print_input_section(this->u2_.object, this->shndx_);
1719 break;
1723 // Output_section methods.
1725 // Construct an Output_section. NAME will point into a Stringpool.
1727 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1728 elfcpp::Elf_Xword flags)
1729 : name_(name),
1730 addralign_(0),
1731 entsize_(0),
1732 load_address_(0),
1733 link_section_(NULL),
1734 link_(0),
1735 info_section_(NULL),
1736 info_symndx_(NULL),
1737 info_(0),
1738 type_(type),
1739 flags_(flags),
1740 out_shndx_(-1U),
1741 symtab_index_(0),
1742 dynsym_index_(0),
1743 input_sections_(),
1744 first_input_offset_(0),
1745 fills_(),
1746 postprocessing_buffer_(NULL),
1747 needs_symtab_index_(false),
1748 needs_dynsym_index_(false),
1749 should_link_to_symtab_(false),
1750 should_link_to_dynsym_(false),
1751 after_input_sections_(false),
1752 requires_postprocessing_(false),
1753 found_in_sections_clause_(false),
1754 has_load_address_(false),
1755 info_uses_section_index_(false),
1756 may_sort_attached_input_sections_(false),
1757 must_sort_attached_input_sections_(false),
1758 attached_input_sections_are_sorted_(false),
1759 is_relro_(false),
1760 is_relro_local_(false),
1761 tls_offset_(0)
1763 // An unallocated section has no address. Forcing this means that
1764 // we don't need special treatment for symbols defined in debug
1765 // sections.
1766 if ((flags & elfcpp::SHF_ALLOC) == 0)
1767 this->set_address(0);
1770 Output_section::~Output_section()
1774 // Set the entry size.
1776 void
1777 Output_section::set_entsize(uint64_t v)
1779 if (this->entsize_ == 0)
1780 this->entsize_ = v;
1781 else
1782 gold_assert(this->entsize_ == v);
1785 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1786 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1787 // relocation section which applies to this section, or 0 if none, or
1788 // -1U if more than one. Return the offset of the input section
1789 // within the output section. Return -1 if the input section will
1790 // receive special handling. In the normal case we don't always keep
1791 // track of input sections for an Output_section. Instead, each
1792 // Object keeps track of the Output_section for each of its input
1793 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1794 // track of input sections here; this is used when SECTIONS appears in
1795 // a linker script.
1797 template<int size, bool big_endian>
1798 off_t
1799 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1800 unsigned int shndx,
1801 const char* secname,
1802 const elfcpp::Shdr<size, big_endian>& shdr,
1803 unsigned int reloc_shndx,
1804 bool have_sections_script)
1806 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1807 if ((addralign & (addralign - 1)) != 0)
1809 object->error(_("invalid alignment %lu for section \"%s\""),
1810 static_cast<unsigned long>(addralign), secname);
1811 addralign = 1;
1814 if (addralign > this->addralign_)
1815 this->addralign_ = addralign;
1817 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1818 this->update_flags_for_input_section(sh_flags);
1820 uint64_t entsize = shdr.get_sh_entsize();
1822 // .debug_str is a mergeable string section, but is not always so
1823 // marked by compilers. Mark manually here so we can optimize.
1824 if (strcmp(secname, ".debug_str") == 0)
1826 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1827 entsize = 1;
1830 // If this is a SHF_MERGE section, we pass all the input sections to
1831 // a Output_data_merge. We don't try to handle relocations for such
1832 // a section.
1833 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1834 && reloc_shndx == 0)
1836 if (this->add_merge_input_section(object, shndx, sh_flags,
1837 entsize, addralign))
1839 // Tell the relocation routines that they need to call the
1840 // output_offset method to determine the final address.
1841 return -1;
1845 off_t offset_in_section = this->current_data_size_for_child();
1846 off_t aligned_offset_in_section = align_address(offset_in_section,
1847 addralign);
1849 if (aligned_offset_in_section > offset_in_section
1850 && !have_sections_script
1851 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1852 && object->target()->has_code_fill())
1854 // We need to add some fill data. Using fill_list_ when
1855 // possible is an optimization, since we will often have fill
1856 // sections without input sections.
1857 off_t fill_len = aligned_offset_in_section - offset_in_section;
1858 if (this->input_sections_.empty())
1859 this->fills_.push_back(Fill(offset_in_section, fill_len));
1860 else
1862 // FIXME: When relaxing, the size needs to adjust to
1863 // maintain a constant alignment.
1864 std::string fill_data(object->target()->code_fill(fill_len));
1865 Output_data_const* odc = new Output_data_const(fill_data, 1);
1866 this->input_sections_.push_back(Input_section(odc));
1870 this->set_current_data_size_for_child(aligned_offset_in_section
1871 + shdr.get_sh_size());
1873 // We need to keep track of this section if we are already keeping
1874 // track of sections, or if we are relaxing. Also, if this is a
1875 // section which requires sorting, or which may require sorting in
1876 // the future, we keep track of the sections. FIXME: Add test for
1877 // relaxing.
1878 if (have_sections_script
1879 || !this->input_sections_.empty()
1880 || this->may_sort_attached_input_sections()
1881 || this->must_sort_attached_input_sections()
1882 || parameters->options().user_set_Map())
1883 this->input_sections_.push_back(Input_section(object, shndx,
1884 shdr.get_sh_size(),
1885 addralign));
1887 return aligned_offset_in_section;
1890 // Add arbitrary data to an output section.
1892 void
1893 Output_section::add_output_section_data(Output_section_data* posd)
1895 Input_section inp(posd);
1896 this->add_output_section_data(&inp);
1898 if (posd->is_data_size_valid())
1900 off_t offset_in_section = this->current_data_size_for_child();
1901 off_t aligned_offset_in_section = align_address(offset_in_section,
1902 posd->addralign());
1903 this->set_current_data_size_for_child(aligned_offset_in_section
1904 + posd->data_size());
1908 // Add arbitrary data to an output section by Input_section.
1910 void
1911 Output_section::add_output_section_data(Input_section* inp)
1913 if (this->input_sections_.empty())
1914 this->first_input_offset_ = this->current_data_size_for_child();
1916 this->input_sections_.push_back(*inp);
1918 uint64_t addralign = inp->addralign();
1919 if (addralign > this->addralign_)
1920 this->addralign_ = addralign;
1922 inp->set_output_section(this);
1925 // Add a merge section to an output section.
1927 void
1928 Output_section::add_output_merge_section(Output_section_data* posd,
1929 bool is_string, uint64_t entsize)
1931 Input_section inp(posd, is_string, entsize);
1932 this->add_output_section_data(&inp);
1935 // Add an input section to a SHF_MERGE section.
1937 bool
1938 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1939 uint64_t flags, uint64_t entsize,
1940 uint64_t addralign)
1942 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1944 // We only merge strings if the alignment is not more than the
1945 // character size. This could be handled, but it's unusual.
1946 if (is_string && addralign > entsize)
1947 return false;
1949 Input_section_list::iterator p;
1950 for (p = this->input_sections_.begin();
1951 p != this->input_sections_.end();
1952 ++p)
1953 if (p->is_merge_section(is_string, entsize, addralign))
1955 p->add_input_section(object, shndx);
1956 return true;
1959 // We handle the actual constant merging in Output_merge_data or
1960 // Output_merge_string_data.
1961 Output_section_data* posd;
1962 if (!is_string)
1963 posd = new Output_merge_data(entsize, addralign);
1964 else
1966 switch (entsize)
1968 case 1:
1969 posd = new Output_merge_string<char>(addralign);
1970 break;
1971 case 2:
1972 posd = new Output_merge_string<uint16_t>(addralign);
1973 break;
1974 case 4:
1975 posd = new Output_merge_string<uint32_t>(addralign);
1976 break;
1977 default:
1978 return false;
1982 this->add_output_merge_section(posd, is_string, entsize);
1983 posd->add_input_section(object, shndx);
1985 return true;
1988 // Given an address OFFSET relative to the start of input section
1989 // SHNDX in OBJECT, return whether this address is being included in
1990 // the final link. This should only be called if SHNDX in OBJECT has
1991 // a special mapping.
1993 bool
1994 Output_section::is_input_address_mapped(const Relobj* object,
1995 unsigned int shndx,
1996 off_t offset) const
1998 gold_assert(object->is_section_specially_mapped(shndx));
2000 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2001 p != this->input_sections_.end();
2002 ++p)
2004 section_offset_type output_offset;
2005 if (p->output_offset(object, shndx, offset, &output_offset))
2006 return output_offset != -1;
2009 // By default we assume that the address is mapped. This should
2010 // only be called after we have passed all sections to Layout. At
2011 // that point we should know what we are discarding.
2012 return true;
2015 // Given an address OFFSET relative to the start of input section
2016 // SHNDX in object OBJECT, return the output offset relative to the
2017 // start of the input section in the output section. This should only
2018 // be called if SHNDX in OBJECT has a special mapping.
2020 section_offset_type
2021 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2022 section_offset_type offset) const
2024 gold_assert(object->is_section_specially_mapped(shndx));
2025 // This can only be called meaningfully when layout is complete.
2026 gold_assert(Output_data::is_layout_complete());
2028 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2029 p != this->input_sections_.end();
2030 ++p)
2032 section_offset_type output_offset;
2033 if (p->output_offset(object, shndx, offset, &output_offset))
2034 return output_offset;
2036 gold_unreachable();
2039 // Return the output virtual address of OFFSET relative to the start
2040 // of input section SHNDX in object OBJECT.
2042 uint64_t
2043 Output_section::output_address(const Relobj* object, unsigned int shndx,
2044 off_t offset) const
2046 gold_assert(object->is_section_specially_mapped(shndx));
2048 uint64_t addr = this->address() + this->first_input_offset_;
2049 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2050 p != this->input_sections_.end();
2051 ++p)
2053 addr = align_address(addr, p->addralign());
2054 section_offset_type output_offset;
2055 if (p->output_offset(object, shndx, offset, &output_offset))
2057 if (output_offset == -1)
2058 return -1U;
2059 return addr + output_offset;
2061 addr += p->data_size();
2064 // If we get here, it means that we don't know the mapping for this
2065 // input section. This might happen in principle if
2066 // add_input_section were called before add_output_section_data.
2067 // But it should never actually happen.
2069 gold_unreachable();
2072 // Return the output address of the start of the merged section for
2073 // input section SHNDX in object OBJECT.
2075 uint64_t
2076 Output_section::starting_output_address(const Relobj* object,
2077 unsigned int shndx) const
2079 gold_assert(object->is_section_specially_mapped(shndx));
2081 uint64_t addr = this->address() + this->first_input_offset_;
2082 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2083 p != this->input_sections_.end();
2084 ++p)
2086 addr = align_address(addr, p->addralign());
2088 // It would be nice if we could use the existing output_offset
2089 // method to get the output offset of input offset 0.
2090 // Unfortunately we don't know for sure that input offset 0 is
2091 // mapped at all.
2092 if (p->is_merge_section_for(object, shndx))
2093 return addr;
2095 addr += p->data_size();
2097 gold_unreachable();
2100 // Set the data size of an Output_section. This is where we handle
2101 // setting the addresses of any Output_section_data objects.
2103 void
2104 Output_section::set_final_data_size()
2106 if (this->input_sections_.empty())
2108 this->set_data_size(this->current_data_size_for_child());
2109 return;
2112 if (this->must_sort_attached_input_sections())
2113 this->sort_attached_input_sections();
2115 uint64_t address = this->address();
2116 off_t startoff = this->offset();
2117 off_t off = startoff + this->first_input_offset_;
2118 for (Input_section_list::iterator p = this->input_sections_.begin();
2119 p != this->input_sections_.end();
2120 ++p)
2122 off = align_address(off, p->addralign());
2123 p->set_address_and_file_offset(address + (off - startoff), off,
2124 startoff);
2125 off += p->data_size();
2128 this->set_data_size(off - startoff);
2131 // Reset the address and file offset.
2133 void
2134 Output_section::do_reset_address_and_file_offset()
2136 for (Input_section_list::iterator p = this->input_sections_.begin();
2137 p != this->input_sections_.end();
2138 ++p)
2139 p->reset_address_and_file_offset();
2142 // Set the TLS offset. Called only for SHT_TLS sections.
2144 void
2145 Output_section::do_set_tls_offset(uint64_t tls_base)
2147 this->tls_offset_ = this->address() - tls_base;
2150 // In a few cases we need to sort the input sections attached to an
2151 // output section. This is used to implement the type of constructor
2152 // priority ordering implemented by the GNU linker, in which the
2153 // priority becomes part of the section name and the sections are
2154 // sorted by name. We only do this for an output section if we see an
2155 // attached input section matching ".ctor.*", ".dtor.*",
2156 // ".init_array.*" or ".fini_array.*".
2158 class Output_section::Input_section_sort_entry
2160 public:
2161 Input_section_sort_entry()
2162 : input_section_(), index_(-1U), section_has_name_(false),
2163 section_name_()
2166 Input_section_sort_entry(const Input_section& input_section,
2167 unsigned int index)
2168 : input_section_(input_section), index_(index),
2169 section_has_name_(input_section.is_input_section())
2171 if (this->section_has_name_)
2173 // This is only called single-threaded from Layout::finalize,
2174 // so it is OK to lock. Unfortunately we have no way to pass
2175 // in a Task token.
2176 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2177 Object* obj = input_section.relobj();
2178 Task_lock_obj<Object> tl(dummy_task, obj);
2180 // This is a slow operation, which should be cached in
2181 // Layout::layout if this becomes a speed problem.
2182 this->section_name_ = obj->section_name(input_section.shndx());
2186 // Return the Input_section.
2187 const Input_section&
2188 input_section() const
2190 gold_assert(this->index_ != -1U);
2191 return this->input_section_;
2194 // The index of this entry in the original list. This is used to
2195 // make the sort stable.
2196 unsigned int
2197 index() const
2199 gold_assert(this->index_ != -1U);
2200 return this->index_;
2203 // Whether there is a section name.
2204 bool
2205 section_has_name() const
2206 { return this->section_has_name_; }
2208 // The section name.
2209 const std::string&
2210 section_name() const
2212 gold_assert(this->section_has_name_);
2213 return this->section_name_;
2216 // Return true if the section name has a priority. This is assumed
2217 // to be true if it has a dot after the initial dot.
2218 bool
2219 has_priority() const
2221 gold_assert(this->section_has_name_);
2222 return this->section_name_.find('.', 1);
2225 // Return true if this an input file whose base name matches
2226 // FILE_NAME. The base name must have an extension of ".o", and
2227 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2228 // This is to match crtbegin.o as well as crtbeginS.o without
2229 // getting confused by other possibilities. Overall matching the
2230 // file name this way is a dreadful hack, but the GNU linker does it
2231 // in order to better support gcc, and we need to be compatible.
2232 bool
2233 match_file_name(const char* match_file_name) const
2235 const std::string& file_name(this->input_section_.relobj()->name());
2236 const char* base_name = lbasename(file_name.c_str());
2237 size_t match_len = strlen(match_file_name);
2238 if (strncmp(base_name, match_file_name, match_len) != 0)
2239 return false;
2240 size_t base_len = strlen(base_name);
2241 if (base_len != match_len + 2 && base_len != match_len + 3)
2242 return false;
2243 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2246 private:
2247 // The Input_section we are sorting.
2248 Input_section input_section_;
2249 // The index of this Input_section in the original list.
2250 unsigned int index_;
2251 // Whether this Input_section has a section name--it won't if this
2252 // is some random Output_section_data.
2253 bool section_has_name_;
2254 // The section name if there is one.
2255 std::string section_name_;
2258 // Return true if S1 should come before S2 in the output section.
2260 bool
2261 Output_section::Input_section_sort_compare::operator()(
2262 const Output_section::Input_section_sort_entry& s1,
2263 const Output_section::Input_section_sort_entry& s2) const
2265 // crtbegin.o must come first.
2266 bool s1_begin = s1.match_file_name("crtbegin");
2267 bool s2_begin = s2.match_file_name("crtbegin");
2268 if (s1_begin || s2_begin)
2270 if (!s1_begin)
2271 return false;
2272 if (!s2_begin)
2273 return true;
2274 return s1.index() < s2.index();
2277 // crtend.o must come last.
2278 bool s1_end = s1.match_file_name("crtend");
2279 bool s2_end = s2.match_file_name("crtend");
2280 if (s1_end || s2_end)
2282 if (!s1_end)
2283 return true;
2284 if (!s2_end)
2285 return false;
2286 return s1.index() < s2.index();
2289 // We sort all the sections with no names to the end.
2290 if (!s1.section_has_name() || !s2.section_has_name())
2292 if (s1.section_has_name())
2293 return true;
2294 if (s2.section_has_name())
2295 return false;
2296 return s1.index() < s2.index();
2299 // A section with a priority follows a section without a priority.
2300 // The GNU linker does this for all but .init_array sections; until
2301 // further notice we'll assume that that is an mistake.
2302 bool s1_has_priority = s1.has_priority();
2303 bool s2_has_priority = s2.has_priority();
2304 if (s1_has_priority && !s2_has_priority)
2305 return false;
2306 if (!s1_has_priority && s2_has_priority)
2307 return true;
2309 // Otherwise we sort by name.
2310 int compare = s1.section_name().compare(s2.section_name());
2311 if (compare != 0)
2312 return compare < 0;
2314 // Otherwise we keep the input order.
2315 return s1.index() < s2.index();
2318 // Sort the input sections attached to an output section.
2320 void
2321 Output_section::sort_attached_input_sections()
2323 if (this->attached_input_sections_are_sorted_)
2324 return;
2326 // The only thing we know about an input section is the object and
2327 // the section index. We need the section name. Recomputing this
2328 // is slow but this is an unusual case. If this becomes a speed
2329 // problem we can cache the names as required in Layout::layout.
2331 // We start by building a larger vector holding a copy of each
2332 // Input_section, plus its current index in the list and its name.
2333 std::vector<Input_section_sort_entry> sort_list;
2335 unsigned int i = 0;
2336 for (Input_section_list::iterator p = this->input_sections_.begin();
2337 p != this->input_sections_.end();
2338 ++p, ++i)
2339 sort_list.push_back(Input_section_sort_entry(*p, i));
2341 // Sort the input sections.
2342 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2344 // Copy the sorted input sections back to our list.
2345 this->input_sections_.clear();
2346 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2347 p != sort_list.end();
2348 ++p)
2349 this->input_sections_.push_back(p->input_section());
2351 // Remember that we sorted the input sections, since we might get
2352 // called again.
2353 this->attached_input_sections_are_sorted_ = true;
2356 // Write the section header to *OSHDR.
2358 template<int size, bool big_endian>
2359 void
2360 Output_section::write_header(const Layout* layout,
2361 const Stringpool* secnamepool,
2362 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2364 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2365 oshdr->put_sh_type(this->type_);
2367 elfcpp::Elf_Xword flags = this->flags_;
2368 if (this->info_section_ != NULL && this->info_uses_section_index_)
2369 flags |= elfcpp::SHF_INFO_LINK;
2370 oshdr->put_sh_flags(flags);
2372 oshdr->put_sh_addr(this->address());
2373 oshdr->put_sh_offset(this->offset());
2374 oshdr->put_sh_size(this->data_size());
2375 if (this->link_section_ != NULL)
2376 oshdr->put_sh_link(this->link_section_->out_shndx());
2377 else if (this->should_link_to_symtab_)
2378 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2379 else if (this->should_link_to_dynsym_)
2380 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2381 else
2382 oshdr->put_sh_link(this->link_);
2384 elfcpp::Elf_Word info;
2385 if (this->info_section_ != NULL)
2387 if (this->info_uses_section_index_)
2388 info = this->info_section_->out_shndx();
2389 else
2390 info = this->info_section_->symtab_index();
2392 else if (this->info_symndx_ != NULL)
2393 info = this->info_symndx_->symtab_index();
2394 else
2395 info = this->info_;
2396 oshdr->put_sh_info(info);
2398 oshdr->put_sh_addralign(this->addralign_);
2399 oshdr->put_sh_entsize(this->entsize_);
2402 // Write out the data. For input sections the data is written out by
2403 // Object::relocate, but we have to handle Output_section_data objects
2404 // here.
2406 void
2407 Output_section::do_write(Output_file* of)
2409 gold_assert(!this->requires_postprocessing());
2411 off_t output_section_file_offset = this->offset();
2412 for (Fill_list::iterator p = this->fills_.begin();
2413 p != this->fills_.end();
2414 ++p)
2416 std::string fill_data(parameters->target().code_fill(p->length()));
2417 of->write(output_section_file_offset + p->section_offset(),
2418 fill_data.data(), fill_data.size());
2421 for (Input_section_list::iterator p = this->input_sections_.begin();
2422 p != this->input_sections_.end();
2423 ++p)
2424 p->write(of);
2427 // If a section requires postprocessing, create the buffer to use.
2429 void
2430 Output_section::create_postprocessing_buffer()
2432 gold_assert(this->requires_postprocessing());
2434 if (this->postprocessing_buffer_ != NULL)
2435 return;
2437 if (!this->input_sections_.empty())
2439 off_t off = this->first_input_offset_;
2440 for (Input_section_list::iterator p = this->input_sections_.begin();
2441 p != this->input_sections_.end();
2442 ++p)
2444 off = align_address(off, p->addralign());
2445 p->finalize_data_size();
2446 off += p->data_size();
2448 this->set_current_data_size_for_child(off);
2451 off_t buffer_size = this->current_data_size_for_child();
2452 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2455 // Write all the data of an Output_section into the postprocessing
2456 // buffer. This is used for sections which require postprocessing,
2457 // such as compression. Input sections are handled by
2458 // Object::Relocate.
2460 void
2461 Output_section::write_to_postprocessing_buffer()
2463 gold_assert(this->requires_postprocessing());
2465 unsigned char* buffer = this->postprocessing_buffer();
2466 for (Fill_list::iterator p = this->fills_.begin();
2467 p != this->fills_.end();
2468 ++p)
2470 std::string fill_data(parameters->target().code_fill(p->length()));
2471 memcpy(buffer + p->section_offset(), fill_data.data(),
2472 fill_data.size());
2475 off_t off = this->first_input_offset_;
2476 for (Input_section_list::iterator p = this->input_sections_.begin();
2477 p != this->input_sections_.end();
2478 ++p)
2480 off = align_address(off, p->addralign());
2481 p->write_to_buffer(buffer + off);
2482 off += p->data_size();
2486 // Get the input sections for linker script processing. We leave
2487 // behind the Output_section_data entries. Note that this may be
2488 // slightly incorrect for merge sections. We will leave them behind,
2489 // but it is possible that the script says that they should follow
2490 // some other input sections, as in:
2491 // .rodata { *(.rodata) *(.rodata.cst*) }
2492 // For that matter, we don't handle this correctly:
2493 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2494 // With luck this will never matter.
2496 uint64_t
2497 Output_section::get_input_sections(
2498 uint64_t address,
2499 const std::string& fill,
2500 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2502 uint64_t orig_address = address;
2504 address = align_address(address, this->addralign());
2506 Input_section_list remaining;
2507 for (Input_section_list::iterator p = this->input_sections_.begin();
2508 p != this->input_sections_.end();
2509 ++p)
2511 if (p->is_input_section())
2512 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2513 else
2515 uint64_t aligned_address = align_address(address, p->addralign());
2516 if (aligned_address != address && !fill.empty())
2518 section_size_type length =
2519 convert_to_section_size_type(aligned_address - address);
2520 std::string this_fill;
2521 this_fill.reserve(length);
2522 while (this_fill.length() + fill.length() <= length)
2523 this_fill += fill;
2524 if (this_fill.length() < length)
2525 this_fill.append(fill, 0, length - this_fill.length());
2527 Output_section_data* posd = new Output_data_const(this_fill, 0);
2528 remaining.push_back(Input_section(posd));
2530 address = aligned_address;
2532 remaining.push_back(*p);
2534 p->finalize_data_size();
2535 address += p->data_size();
2539 this->input_sections_.swap(remaining);
2540 this->first_input_offset_ = 0;
2542 uint64_t data_size = address - orig_address;
2543 this->set_current_data_size_for_child(data_size);
2544 return data_size;
2547 // Add an input section from a script.
2549 void
2550 Output_section::add_input_section_for_script(Relobj* object,
2551 unsigned int shndx,
2552 off_t data_size,
2553 uint64_t addralign)
2555 if (addralign > this->addralign_)
2556 this->addralign_ = addralign;
2558 off_t offset_in_section = this->current_data_size_for_child();
2559 off_t aligned_offset_in_section = align_address(offset_in_section,
2560 addralign);
2562 this->set_current_data_size_for_child(aligned_offset_in_section
2563 + data_size);
2565 this->input_sections_.push_back(Input_section(object, shndx,
2566 data_size, addralign));
2569 // Print to the map file.
2571 void
2572 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
2574 mapfile->print_output_section(this);
2576 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2577 p != this->input_sections_.end();
2578 ++p)
2579 p->print_to_mapfile(mapfile);
2582 // Print stats for merge sections to stderr.
2584 void
2585 Output_section::print_merge_stats()
2587 Input_section_list::iterator p;
2588 for (p = this->input_sections_.begin();
2589 p != this->input_sections_.end();
2590 ++p)
2591 p->print_merge_stats(this->name_);
2594 // Output segment methods.
2596 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2597 : output_data_(),
2598 output_bss_(),
2599 vaddr_(0),
2600 paddr_(0),
2601 memsz_(0),
2602 max_align_(0),
2603 min_p_align_(0),
2604 offset_(0),
2605 filesz_(0),
2606 type_(type),
2607 flags_(flags),
2608 is_max_align_known_(false),
2609 are_addresses_set_(false)
2613 // Add an Output_section to an Output_segment.
2615 void
2616 Output_segment::add_output_section(Output_section* os,
2617 elfcpp::Elf_Word seg_flags)
2619 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
2620 gold_assert(!this->is_max_align_known_);
2622 // Update the segment flags.
2623 this->flags_ |= seg_flags;
2625 Output_segment::Output_data_list* pdl;
2626 if (os->type() == elfcpp::SHT_NOBITS)
2627 pdl = &this->output_bss_;
2628 else
2629 pdl = &this->output_data_;
2631 // So that PT_NOTE segments will work correctly, we need to ensure
2632 // that all SHT_NOTE sections are adjacent. This will normally
2633 // happen automatically, because all the SHT_NOTE input sections
2634 // will wind up in the same output section. However, it is possible
2635 // for multiple SHT_NOTE input sections to have different section
2636 // flags, and thus be in different output sections, but for the
2637 // different section flags to map into the same segment flags and
2638 // thus the same output segment.
2640 // Note that while there may be many input sections in an output
2641 // section, there are normally only a few output sections in an
2642 // output segment. This loop is expected to be fast.
2644 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
2646 Output_segment::Output_data_list::iterator p = pdl->end();
2649 --p;
2650 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2652 ++p;
2653 pdl->insert(p, os);
2654 return;
2657 while (p != pdl->begin());
2660 // Similarly, so that PT_TLS segments will work, we need to group
2661 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2662 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2663 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2664 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2665 // and the PT_TLS segment -- we do this grouping only for the
2666 // PT_LOAD segment.
2667 if (this->type_ != elfcpp::PT_TLS
2668 && (os->flags() & elfcpp::SHF_TLS) != 0
2669 && !this->output_data_.empty())
2671 pdl = &this->output_data_;
2672 bool nobits = os->type() == elfcpp::SHT_NOBITS;
2673 bool sawtls = false;
2674 Output_segment::Output_data_list::iterator p = pdl->end();
2677 --p;
2678 bool insert;
2679 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2681 sawtls = true;
2682 // Put a NOBITS section after the first TLS section.
2683 // Put a PROGBITS section after the first TLS/PROGBITS
2684 // section.
2685 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2687 else
2689 // If we've gone past the TLS sections, but we've seen a
2690 // TLS section, then we need to insert this section now.
2691 insert = sawtls;
2694 if (insert)
2696 ++p;
2697 pdl->insert(p, os);
2698 return;
2701 while (p != pdl->begin());
2703 // There are no TLS sections yet; put this one at the requested
2704 // location in the section list.
2707 // For the PT_GNU_RELRO segment, we need to group relro sections,
2708 // and we need to put them before any non-relro sections. Also,
2709 // relro local sections go before relro non-local sections.
2710 if (parameters->options().relro() && os->is_relro())
2712 gold_assert(pdl == &this->output_data_);
2713 Output_segment::Output_data_list::iterator p;
2714 for (p = pdl->begin(); p != pdl->end(); ++p)
2716 if (!(*p)->is_section())
2717 break;
2719 Output_section* pos = (*p)->output_section();
2720 if (!pos->is_relro()
2721 || (os->is_relro_local() && !pos->is_relro_local()))
2722 break;
2725 pdl->insert(p, os);
2726 return;
2729 pdl->push_back(os);
2732 // Remove an Output_section from this segment. It is an error if it
2733 // is not present.
2735 void
2736 Output_segment::remove_output_section(Output_section* os)
2738 // We only need this for SHT_PROGBITS.
2739 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2740 for (Output_data_list::iterator p = this->output_data_.begin();
2741 p != this->output_data_.end();
2742 ++p)
2744 if (*p == os)
2746 this->output_data_.erase(p);
2747 return;
2750 gold_unreachable();
2753 // Add an Output_data (which is not an Output_section) to the start of
2754 // a segment.
2756 void
2757 Output_segment::add_initial_output_data(Output_data* od)
2759 gold_assert(!this->is_max_align_known_);
2760 this->output_data_.push_front(od);
2763 // Return whether the first data section is a relro section.
2765 bool
2766 Output_segment::is_first_section_relro() const
2768 return (!this->output_data_.empty()
2769 && this->output_data_.front()->is_section()
2770 && this->output_data_.front()->output_section()->is_relro());
2773 // Return the maximum alignment of the Output_data in Output_segment.
2775 uint64_t
2776 Output_segment::maximum_alignment()
2778 if (!this->is_max_align_known_)
2780 uint64_t addralign;
2782 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2783 if (addralign > this->max_align_)
2784 this->max_align_ = addralign;
2786 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2787 if (addralign > this->max_align_)
2788 this->max_align_ = addralign;
2790 // If -z relro is in effect, and the first section in this
2791 // segment is a relro section, then the segment must be aligned
2792 // to at least the common page size. This ensures that the
2793 // PT_GNU_RELRO segment will start at a page boundary.
2794 if (parameters->options().relro() && this->is_first_section_relro())
2796 addralign = parameters->target().common_pagesize();
2797 if (addralign > this->max_align_)
2798 this->max_align_ = addralign;
2801 this->is_max_align_known_ = true;
2804 return this->max_align_;
2807 // Return the maximum alignment of a list of Output_data.
2809 uint64_t
2810 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
2812 uint64_t ret = 0;
2813 for (Output_data_list::const_iterator p = pdl->begin();
2814 p != pdl->end();
2815 ++p)
2817 uint64_t addralign = (*p)->addralign();
2818 if (addralign > ret)
2819 ret = addralign;
2821 return ret;
2824 // Return the number of dynamic relocs applied to this segment.
2826 unsigned int
2827 Output_segment::dynamic_reloc_count() const
2829 return (this->dynamic_reloc_count_list(&this->output_data_)
2830 + this->dynamic_reloc_count_list(&this->output_bss_));
2833 // Return the number of dynamic relocs applied to an Output_data_list.
2835 unsigned int
2836 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2838 unsigned int count = 0;
2839 for (Output_data_list::const_iterator p = pdl->begin();
2840 p != pdl->end();
2841 ++p)
2842 count += (*p)->dynamic_reloc_count();
2843 return count;
2846 // Set the section addresses for an Output_segment. If RESET is true,
2847 // reset the addresses first. ADDR is the address and *POFF is the
2848 // file offset. Set the section indexes starting with *PSHNDX.
2849 // Return the address of the immediately following segment. Update
2850 // *POFF and *PSHNDX.
2852 uint64_t
2853 Output_segment::set_section_addresses(const Layout* layout, bool reset,
2854 uint64_t addr, off_t* poff,
2855 unsigned int* pshndx)
2857 gold_assert(this->type_ == elfcpp::PT_LOAD);
2859 if (!reset && this->are_addresses_set_)
2861 gold_assert(this->paddr_ == addr);
2862 addr = this->vaddr_;
2864 else
2866 this->vaddr_ = addr;
2867 this->paddr_ = addr;
2868 this->are_addresses_set_ = true;
2871 bool in_tls = false;
2873 bool in_relro = (parameters->options().relro()
2874 && this->is_first_section_relro());
2876 off_t orig_off = *poff;
2877 this->offset_ = orig_off;
2879 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
2880 addr, poff, pshndx, &in_tls,
2881 &in_relro);
2882 this->filesz_ = *poff - orig_off;
2884 off_t off = *poff;
2886 uint64_t ret = this->set_section_list_addresses(layout, reset,
2887 &this->output_bss_,
2888 addr, poff, pshndx,
2889 &in_tls, &in_relro);
2891 // If the last section was a TLS section, align upward to the
2892 // alignment of the TLS segment, so that the overall size of the TLS
2893 // segment is aligned.
2894 if (in_tls)
2896 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2897 *poff = align_address(*poff, segment_align);
2900 // If all the sections were relro sections, align upward to the
2901 // common page size.
2902 if (in_relro)
2904 uint64_t page_align = parameters->target().common_pagesize();
2905 *poff = align_address(*poff, page_align);
2908 this->memsz_ = *poff - orig_off;
2910 // Ignore the file offset adjustments made by the BSS Output_data
2911 // objects.
2912 *poff = off;
2914 return ret;
2917 // Set the addresses and file offsets in a list of Output_data
2918 // structures.
2920 uint64_t
2921 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
2922 Output_data_list* pdl,
2923 uint64_t addr, off_t* poff,
2924 unsigned int* pshndx,
2925 bool* in_tls, bool* in_relro)
2927 off_t startoff = *poff;
2929 off_t off = startoff;
2930 for (Output_data_list::iterator p = pdl->begin();
2931 p != pdl->end();
2932 ++p)
2934 if (reset)
2935 (*p)->reset_address_and_file_offset();
2937 // When using a linker script the section will most likely
2938 // already have an address.
2939 if (!(*p)->is_address_valid())
2941 uint64_t align = (*p)->addralign();
2943 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2945 // Give the first TLS section the alignment of the
2946 // entire TLS segment. Otherwise the TLS segment as a
2947 // whole may be misaligned.
2948 if (!*in_tls)
2950 Output_segment* tls_segment = layout->tls_segment();
2951 gold_assert(tls_segment != NULL);
2952 uint64_t segment_align = tls_segment->maximum_alignment();
2953 gold_assert(segment_align >= align);
2954 align = segment_align;
2956 *in_tls = true;
2959 else
2961 // If this is the first section after the TLS segment,
2962 // align it to at least the alignment of the TLS
2963 // segment, so that the size of the overall TLS segment
2964 // is aligned.
2965 if (*in_tls)
2967 uint64_t segment_align =
2968 layout->tls_segment()->maximum_alignment();
2969 if (segment_align > align)
2970 align = segment_align;
2972 *in_tls = false;
2976 // If this is a non-relro section after a relro section,
2977 // align it to a common page boundary so that the dynamic
2978 // linker has a page to mark as read-only.
2979 if (*in_relro
2980 && (!(*p)->is_section()
2981 || !(*p)->output_section()->is_relro()))
2983 uint64_t page_align = parameters->target().common_pagesize();
2984 if (page_align > align)
2985 align = page_align;
2986 *in_relro = false;
2989 off = align_address(off, align);
2990 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2992 else
2994 // The script may have inserted a skip forward, but it
2995 // better not have moved backward.
2996 gold_assert((*p)->address() >= addr + (off - startoff));
2997 off += (*p)->address() - (addr + (off - startoff));
2998 (*p)->set_file_offset(off);
2999 (*p)->finalize_data_size();
3002 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
3003 // section. Such a section does not affect the size of a
3004 // PT_LOAD segment.
3005 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
3006 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
3007 off += (*p)->data_size();
3009 if ((*p)->is_section())
3011 (*p)->set_out_shndx(*pshndx);
3012 ++*pshndx;
3016 *poff = off;
3017 return addr + (off - startoff);
3020 // For a non-PT_LOAD segment, set the offset from the sections, if
3021 // any.
3023 void
3024 Output_segment::set_offset()
3026 gold_assert(this->type_ != elfcpp::PT_LOAD);
3028 gold_assert(!this->are_addresses_set_);
3030 if (this->output_data_.empty() && this->output_bss_.empty())
3032 this->vaddr_ = 0;
3033 this->paddr_ = 0;
3034 this->are_addresses_set_ = true;
3035 this->memsz_ = 0;
3036 this->min_p_align_ = 0;
3037 this->offset_ = 0;
3038 this->filesz_ = 0;
3039 return;
3042 const Output_data* first;
3043 if (this->output_data_.empty())
3044 first = this->output_bss_.front();
3045 else
3046 first = this->output_data_.front();
3047 this->vaddr_ = first->address();
3048 this->paddr_ = (first->has_load_address()
3049 ? first->load_address()
3050 : this->vaddr_);
3051 this->are_addresses_set_ = true;
3052 this->offset_ = first->offset();
3054 if (this->output_data_.empty())
3055 this->filesz_ = 0;
3056 else
3058 const Output_data* last_data = this->output_data_.back();
3059 this->filesz_ = (last_data->address()
3060 + last_data->data_size()
3061 - this->vaddr_);
3064 const Output_data* last;
3065 if (this->output_bss_.empty())
3066 last = this->output_data_.back();
3067 else
3068 last = this->output_bss_.back();
3069 this->memsz_ = (last->address()
3070 + last->data_size()
3071 - this->vaddr_);
3073 // If this is a TLS segment, align the memory size. The code in
3074 // set_section_list ensures that the section after the TLS segment
3075 // is aligned to give us room.
3076 if (this->type_ == elfcpp::PT_TLS)
3078 uint64_t segment_align = this->maximum_alignment();
3079 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3080 this->memsz_ = align_address(this->memsz_, segment_align);
3083 // If this is a RELRO segment, align the memory size. The code in
3084 // set_section_list ensures that the section after the RELRO segment
3085 // is aligned to give us room.
3086 if (this->type_ == elfcpp::PT_GNU_RELRO)
3088 uint64_t page_align = parameters->target().common_pagesize();
3089 gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3090 this->memsz_ = align_address(this->memsz_, page_align);
3094 // Set the TLS offsets of the sections in the PT_TLS segment.
3096 void
3097 Output_segment::set_tls_offsets()
3099 gold_assert(this->type_ == elfcpp::PT_TLS);
3101 for (Output_data_list::iterator p = this->output_data_.begin();
3102 p != this->output_data_.end();
3103 ++p)
3104 (*p)->set_tls_offset(this->vaddr_);
3106 for (Output_data_list::iterator p = this->output_bss_.begin();
3107 p != this->output_bss_.end();
3108 ++p)
3109 (*p)->set_tls_offset(this->vaddr_);
3112 // Return the address of the first section.
3114 uint64_t
3115 Output_segment::first_section_load_address() const
3117 for (Output_data_list::const_iterator p = this->output_data_.begin();
3118 p != this->output_data_.end();
3119 ++p)
3120 if ((*p)->is_section())
3121 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3123 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3124 p != this->output_bss_.end();
3125 ++p)
3126 if ((*p)->is_section())
3127 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3129 gold_unreachable();
3132 // Return the number of Output_sections in an Output_segment.
3134 unsigned int
3135 Output_segment::output_section_count() const
3137 return (this->output_section_count_list(&this->output_data_)
3138 + this->output_section_count_list(&this->output_bss_));
3141 // Return the number of Output_sections in an Output_data_list.
3143 unsigned int
3144 Output_segment::output_section_count_list(const Output_data_list* pdl) const
3146 unsigned int count = 0;
3147 for (Output_data_list::const_iterator p = pdl->begin();
3148 p != pdl->end();
3149 ++p)
3151 if ((*p)->is_section())
3152 ++count;
3154 return count;
3157 // Return the section attached to the list segment with the lowest
3158 // load address. This is used when handling a PHDRS clause in a
3159 // linker script.
3161 Output_section*
3162 Output_segment::section_with_lowest_load_address() const
3164 Output_section* found = NULL;
3165 uint64_t found_lma = 0;
3166 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3168 Output_section* found_data = found;
3169 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3170 if (found != found_data && found_data != NULL)
3172 gold_error(_("nobits section %s may not precede progbits section %s "
3173 "in same segment"),
3174 found->name(), found_data->name());
3175 return NULL;
3178 return found;
3181 // Look through a list for a section with a lower load address.
3183 void
3184 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3185 Output_section** found,
3186 uint64_t* found_lma) const
3188 for (Output_data_list::const_iterator p = pdl->begin();
3189 p != pdl->end();
3190 ++p)
3192 if (!(*p)->is_section())
3193 continue;
3194 Output_section* os = static_cast<Output_section*>(*p);
3195 uint64_t lma = (os->has_load_address()
3196 ? os->load_address()
3197 : os->address());
3198 if (*found == NULL || lma < *found_lma)
3200 *found = os;
3201 *found_lma = lma;
3206 // Write the segment data into *OPHDR.
3208 template<int size, bool big_endian>
3209 void
3210 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
3212 ophdr->put_p_type(this->type_);
3213 ophdr->put_p_offset(this->offset_);
3214 ophdr->put_p_vaddr(this->vaddr_);
3215 ophdr->put_p_paddr(this->paddr_);
3216 ophdr->put_p_filesz(this->filesz_);
3217 ophdr->put_p_memsz(this->memsz_);
3218 ophdr->put_p_flags(this->flags_);
3219 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
3222 // Write the section headers into V.
3224 template<int size, bool big_endian>
3225 unsigned char*
3226 Output_segment::write_section_headers(const Layout* layout,
3227 const Stringpool* secnamepool,
3228 unsigned char* v,
3229 unsigned int *pshndx) const
3231 // Every section that is attached to a segment must be attached to a
3232 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3233 // segments.
3234 if (this->type_ != elfcpp::PT_LOAD)
3235 return v;
3237 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3238 &this->output_data_,
3239 v, pshndx);
3240 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3241 &this->output_bss_,
3242 v, pshndx);
3243 return v;
3246 template<int size, bool big_endian>
3247 unsigned char*
3248 Output_segment::write_section_headers_list(const Layout* layout,
3249 const Stringpool* secnamepool,
3250 const Output_data_list* pdl,
3251 unsigned char* v,
3252 unsigned int* pshndx) const
3254 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3255 for (Output_data_list::const_iterator p = pdl->begin();
3256 p != pdl->end();
3257 ++p)
3259 if ((*p)->is_section())
3261 const Output_section* ps = static_cast<const Output_section*>(*p);
3262 gold_assert(*pshndx == ps->out_shndx());
3263 elfcpp::Shdr_write<size, big_endian> oshdr(v);
3264 ps->write_header(layout, secnamepool, &oshdr);
3265 v += shdr_size;
3266 ++*pshndx;
3269 return v;
3272 // Print the output sections to the map file.
3274 void
3275 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3277 if (this->type() != elfcpp::PT_LOAD)
3278 return;
3279 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3280 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3283 // Print an output section list to the map file.
3285 void
3286 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3287 const Output_data_list* pdl) const
3289 for (Output_data_list::const_iterator p = pdl->begin();
3290 p != pdl->end();
3291 ++p)
3292 (*p)->print_to_mapfile(mapfile);
3295 // Output_file methods.
3297 Output_file::Output_file(const char* name)
3298 : name_(name),
3299 o_(-1),
3300 file_size_(0),
3301 base_(NULL),
3302 map_is_anonymous_(false),
3303 is_temporary_(false)
3307 // Open the output file.
3309 void
3310 Output_file::open(off_t file_size)
3312 this->file_size_ = file_size;
3314 // Unlink the file first; otherwise the open() may fail if the file
3315 // is busy (e.g. it's an executable that's currently being executed).
3317 // However, the linker may be part of a system where a zero-length
3318 // file is created for it to write to, with tight permissions (gcc
3319 // 2.95 did something like this). Unlinking the file would work
3320 // around those permission controls, so we only unlink if the file
3321 // has a non-zero size. We also unlink only regular files to avoid
3322 // trouble with directories/etc.
3324 // If we fail, continue; this command is merely a best-effort attempt
3325 // to improve the odds for open().
3327 // We let the name "-" mean "stdout"
3328 if (!this->is_temporary_)
3330 if (strcmp(this->name_, "-") == 0)
3331 this->o_ = STDOUT_FILENO;
3332 else
3334 struct stat s;
3335 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
3336 unlink_if_ordinary(this->name_);
3338 int mode = parameters->options().relocatable() ? 0666 : 0777;
3339 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
3340 if (o < 0)
3341 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3342 this->o_ = o;
3346 this->map();
3349 // Resize the output file.
3351 void
3352 Output_file::resize(off_t file_size)
3354 // If the mmap is mapping an anonymous memory buffer, this is easy:
3355 // just mremap to the new size. If it's mapping to a file, we want
3356 // to unmap to flush to the file, then remap after growing the file.
3357 if (this->map_is_anonymous_)
3359 void* base = ::mremap(this->base_, this->file_size_, file_size,
3360 MREMAP_MAYMOVE);
3361 if (base == MAP_FAILED)
3362 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3363 this->base_ = static_cast<unsigned char*>(base);
3364 this->file_size_ = file_size;
3366 else
3368 this->unmap();
3369 this->file_size_ = file_size;
3370 this->map();
3374 // Map the file into memory.
3376 void
3377 Output_file::map()
3379 const int o = this->o_;
3381 // If the output file is not a regular file, don't try to mmap it;
3382 // instead, we'll mmap a block of memory (an anonymous buffer), and
3383 // then later write the buffer to the file.
3384 void* base;
3385 struct stat statbuf;
3386 if (o == STDOUT_FILENO || o == STDERR_FILENO
3387 || ::fstat(o, &statbuf) != 0
3388 || !S_ISREG(statbuf.st_mode)
3389 || this->is_temporary_)
3391 this->map_is_anonymous_ = true;
3392 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3393 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3395 else
3397 // Write out one byte to make the file the right size.
3398 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
3399 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
3400 char b = 0;
3401 if (::write(o, &b, 1) != 1)
3402 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
3404 // Map the file into memory.
3405 this->map_is_anonymous_ = false;
3406 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3407 MAP_SHARED, o, 0);
3409 if (base == MAP_FAILED)
3410 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
3411 this->base_ = static_cast<unsigned char*>(base);
3414 // Unmap the file from memory.
3416 void
3417 Output_file::unmap()
3419 if (::munmap(this->base_, this->file_size_) < 0)
3420 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
3421 this->base_ = NULL;
3424 // Close the output file.
3426 void
3427 Output_file::close()
3429 // If the map isn't file-backed, we need to write it now.
3430 if (this->map_is_anonymous_ && !this->is_temporary_)
3432 size_t bytes_to_write = this->file_size_;
3433 while (bytes_to_write > 0)
3435 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
3436 if (bytes_written == 0)
3437 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
3438 else if (bytes_written < 0)
3439 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
3440 else
3441 bytes_to_write -= bytes_written;
3444 this->unmap();
3446 // We don't close stdout or stderr
3447 if (this->o_ != STDOUT_FILENO
3448 && this->o_ != STDERR_FILENO
3449 && !this->is_temporary_)
3450 if (::close(this->o_) < 0)
3451 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
3452 this->o_ = -1;
3455 // Instantiate the templates we need. We could use the configure
3456 // script to restrict this to only the ones for implemented targets.
3458 #ifdef HAVE_TARGET_32_LITTLE
3459 template
3460 off_t
3461 Output_section::add_input_section<32, false>(
3462 Sized_relobj<32, false>* object,
3463 unsigned int shndx,
3464 const char* secname,
3465 const elfcpp::Shdr<32, false>& shdr,
3466 unsigned int reloc_shndx,
3467 bool have_sections_script);
3468 #endif
3470 #ifdef HAVE_TARGET_32_BIG
3471 template
3472 off_t
3473 Output_section::add_input_section<32, true>(
3474 Sized_relobj<32, true>* object,
3475 unsigned int shndx,
3476 const char* secname,
3477 const elfcpp::Shdr<32, true>& shdr,
3478 unsigned int reloc_shndx,
3479 bool have_sections_script);
3480 #endif
3482 #ifdef HAVE_TARGET_64_LITTLE
3483 template
3484 off_t
3485 Output_section::add_input_section<64, false>(
3486 Sized_relobj<64, false>* object,
3487 unsigned int shndx,
3488 const char* secname,
3489 const elfcpp::Shdr<64, false>& shdr,
3490 unsigned int reloc_shndx,
3491 bool have_sections_script);
3492 #endif
3494 #ifdef HAVE_TARGET_64_BIG
3495 template
3496 off_t
3497 Output_section::add_input_section<64, true>(
3498 Sized_relobj<64, true>* object,
3499 unsigned int shndx,
3500 const char* secname,
3501 const elfcpp::Shdr<64, true>& shdr,
3502 unsigned int reloc_shndx,
3503 bool have_sections_script);
3504 #endif
3506 #ifdef HAVE_TARGET_32_LITTLE
3507 template
3508 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
3509 #endif
3511 #ifdef HAVE_TARGET_32_BIG
3512 template
3513 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
3514 #endif
3516 #ifdef HAVE_TARGET_64_LITTLE
3517 template
3518 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
3519 #endif
3521 #ifdef HAVE_TARGET_64_BIG
3522 template
3523 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
3524 #endif
3526 #ifdef HAVE_TARGET_32_LITTLE
3527 template
3528 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
3529 #endif
3531 #ifdef HAVE_TARGET_32_BIG
3532 template
3533 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
3534 #endif
3536 #ifdef HAVE_TARGET_64_LITTLE
3537 template
3538 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
3539 #endif
3541 #ifdef HAVE_TARGET_64_BIG
3542 template
3543 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
3544 #endif
3546 #ifdef HAVE_TARGET_32_LITTLE
3547 template
3548 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
3549 #endif
3551 #ifdef HAVE_TARGET_32_BIG
3552 template
3553 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
3554 #endif
3556 #ifdef HAVE_TARGET_64_LITTLE
3557 template
3558 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
3559 #endif
3561 #ifdef HAVE_TARGET_64_BIG
3562 template
3563 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
3564 #endif
3566 #ifdef HAVE_TARGET_32_LITTLE
3567 template
3568 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
3569 #endif
3571 #ifdef HAVE_TARGET_32_BIG
3572 template
3573 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
3574 #endif
3576 #ifdef HAVE_TARGET_64_LITTLE
3577 template
3578 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
3579 #endif
3581 #ifdef HAVE_TARGET_64_BIG
3582 template
3583 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
3584 #endif
3586 #ifdef HAVE_TARGET_32_LITTLE
3587 template
3588 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3589 #endif
3591 #ifdef HAVE_TARGET_32_BIG
3592 template
3593 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3594 #endif
3596 #ifdef HAVE_TARGET_64_LITTLE
3597 template
3598 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3599 #endif
3601 #ifdef HAVE_TARGET_64_BIG
3602 template
3603 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3604 #endif
3606 #ifdef HAVE_TARGET_32_LITTLE
3607 template
3608 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3609 #endif
3611 #ifdef HAVE_TARGET_32_BIG
3612 template
3613 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3614 #endif
3616 #ifdef HAVE_TARGET_64_LITTLE
3617 template
3618 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3619 #endif
3621 #ifdef HAVE_TARGET_64_BIG
3622 template
3623 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3624 #endif
3626 #ifdef HAVE_TARGET_32_LITTLE
3627 template
3628 class Output_data_group<32, false>;
3629 #endif
3631 #ifdef HAVE_TARGET_32_BIG
3632 template
3633 class Output_data_group<32, true>;
3634 #endif
3636 #ifdef HAVE_TARGET_64_LITTLE
3637 template
3638 class Output_data_group<64, false>;
3639 #endif
3641 #ifdef HAVE_TARGET_64_BIG
3642 template
3643 class Output_data_group<64, true>;
3644 #endif
3646 #ifdef HAVE_TARGET_32_LITTLE
3647 template
3648 class Output_data_got<32, false>;
3649 #endif
3651 #ifdef HAVE_TARGET_32_BIG
3652 template
3653 class Output_data_got<32, true>;
3654 #endif
3656 #ifdef HAVE_TARGET_64_LITTLE
3657 template
3658 class Output_data_got<64, false>;
3659 #endif
3661 #ifdef HAVE_TARGET_64_BIG
3662 template
3663 class Output_data_got<64, true>;
3664 #endif
3666 } // End namespace gold.