gold: Allow use_plt_offset to be specified for global relocations.
[deliverable/binutils-gdb.git] / gold / output.cc
1 // output.cc -- manage the output file for gold
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <cstdlib>
26 #include <cstring>
27 #include <cerrno>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <algorithm>
32
33 #ifdef HAVE_SYS_MMAN_H
34 #include <sys/mman.h>
35 #endif
36
37 #include "libiberty.h"
38
39 #include "dwarf.h"
40 #include "parameters.h"
41 #include "object.h"
42 #include "symtab.h"
43 #include "reloc.h"
44 #include "merge.h"
45 #include "descriptors.h"
46 #include "layout.h"
47 #include "output.h"
48
49 // For systems without mmap support.
50 #ifndef HAVE_MMAP
51 # define mmap gold_mmap
52 # define munmap gold_munmap
53 # define mremap gold_mremap
54 # ifndef MAP_FAILED
55 # define MAP_FAILED (reinterpret_cast<void*>(-1))
56 # endif
57 # ifndef PROT_READ
58 # define PROT_READ 0
59 # endif
60 # ifndef PROT_WRITE
61 # define PROT_WRITE 0
62 # endif
63 # ifndef MAP_PRIVATE
64 # define MAP_PRIVATE 0
65 # endif
66 # ifndef MAP_ANONYMOUS
67 # define MAP_ANONYMOUS 0
68 # endif
69 # ifndef MAP_SHARED
70 # define MAP_SHARED 0
71 # endif
72
73 # ifndef ENOSYS
74 # define ENOSYS EINVAL
75 # endif
76
77 static void *
78 gold_mmap(void *, size_t, int, int, int, off_t)
79 {
80 errno = ENOSYS;
81 return MAP_FAILED;
82 }
83
84 static int
85 gold_munmap(void *, size_t)
86 {
87 errno = ENOSYS;
88 return -1;
89 }
90
91 static void *
92 gold_mremap(void *, size_t, size_t, int)
93 {
94 errno = ENOSYS;
95 return MAP_FAILED;
96 }
97
98 #endif
99
100 #if defined(HAVE_MMAP) && !defined(HAVE_MREMAP)
101 # define mremap gold_mremap
102 extern "C" void *gold_mremap(void *, size_t, size_t, int);
103 #endif
104
105 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
106 #ifndef MAP_ANONYMOUS
107 # define MAP_ANONYMOUS MAP_ANON
108 #endif
109
110 #ifndef MREMAP_MAYMOVE
111 # define MREMAP_MAYMOVE 1
112 #endif
113
114 #ifndef HAVE_POSIX_FALLOCATE
115 // A dummy, non general, version of posix_fallocate. Here we just set
116 // the file size and hope that there is enough disk space. FIXME: We
117 // could allocate disk space by walking block by block and writing a
118 // zero byte into each block.
119 static int
120 posix_fallocate(int o, off_t offset, off_t len)
121 {
122 if (ftruncate(o, offset + len) < 0)
123 return errno;
124 return 0;
125 }
126 #endif // !defined(HAVE_POSIX_FALLOCATE)
127
128 // Mingw does not have S_ISLNK.
129 #ifndef S_ISLNK
130 # define S_ISLNK(mode) 0
131 #endif
132
133 namespace gold
134 {
135
136 // Output_data variables.
137
138 bool Output_data::allocated_sizes_are_fixed;
139
140 // Output_data methods.
141
142 Output_data::~Output_data()
143 {
144 }
145
146 // Return the default alignment for the target size.
147
148 uint64_t
149 Output_data::default_alignment()
150 {
151 return Output_data::default_alignment_for_size(
152 parameters->target().get_size());
153 }
154
155 // Return the default alignment for a size--32 or 64.
156
157 uint64_t
158 Output_data::default_alignment_for_size(int size)
159 {
160 if (size == 32)
161 return 4;
162 else if (size == 64)
163 return 8;
164 else
165 gold_unreachable();
166 }
167
168 // Output_section_header methods. This currently assumes that the
169 // segment and section lists are complete at construction time.
170
171 Output_section_headers::Output_section_headers(
172 const Layout* layout,
173 const Layout::Segment_list* segment_list,
174 const Layout::Section_list* section_list,
175 const Layout::Section_list* unattached_section_list,
176 const Stringpool* secnamepool,
177 const Output_section* shstrtab_section)
178 : layout_(layout),
179 segment_list_(segment_list),
180 section_list_(section_list),
181 unattached_section_list_(unattached_section_list),
182 secnamepool_(secnamepool),
183 shstrtab_section_(shstrtab_section)
184 {
185 }
186
187 // Compute the current data size.
188
189 off_t
190 Output_section_headers::do_size() const
191 {
192 // Count all the sections. Start with 1 for the null section.
193 off_t count = 1;
194 if (!parameters->options().relocatable())
195 {
196 for (Layout::Segment_list::const_iterator p =
197 this->segment_list_->begin();
198 p != this->segment_list_->end();
199 ++p)
200 if ((*p)->type() == elfcpp::PT_LOAD)
201 count += (*p)->output_section_count();
202 }
203 else
204 {
205 for (Layout::Section_list::const_iterator p =
206 this->section_list_->begin();
207 p != this->section_list_->end();
208 ++p)
209 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
210 ++count;
211 }
212 count += this->unattached_section_list_->size();
213
214 const int size = parameters->target().get_size();
215 int shdr_size;
216 if (size == 32)
217 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
218 else if (size == 64)
219 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
220 else
221 gold_unreachable();
222
223 return count * shdr_size;
224 }
225
226 // Write out the section headers.
227
228 void
229 Output_section_headers::do_write(Output_file* of)
230 {
231 switch (parameters->size_and_endianness())
232 {
233 #ifdef HAVE_TARGET_32_LITTLE
234 case Parameters::TARGET_32_LITTLE:
235 this->do_sized_write<32, false>(of);
236 break;
237 #endif
238 #ifdef HAVE_TARGET_32_BIG
239 case Parameters::TARGET_32_BIG:
240 this->do_sized_write<32, true>(of);
241 break;
242 #endif
243 #ifdef HAVE_TARGET_64_LITTLE
244 case Parameters::TARGET_64_LITTLE:
245 this->do_sized_write<64, false>(of);
246 break;
247 #endif
248 #ifdef HAVE_TARGET_64_BIG
249 case Parameters::TARGET_64_BIG:
250 this->do_sized_write<64, true>(of);
251 break;
252 #endif
253 default:
254 gold_unreachable();
255 }
256 }
257
258 template<int size, bool big_endian>
259 void
260 Output_section_headers::do_sized_write(Output_file* of)
261 {
262 off_t all_shdrs_size = this->data_size();
263 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
264
265 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
266 unsigned char* v = view;
267
268 {
269 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
270 oshdr.put_sh_name(0);
271 oshdr.put_sh_type(elfcpp::SHT_NULL);
272 oshdr.put_sh_flags(0);
273 oshdr.put_sh_addr(0);
274 oshdr.put_sh_offset(0);
275
276 size_t section_count = (this->data_size()
277 / elfcpp::Elf_sizes<size>::shdr_size);
278 if (section_count < elfcpp::SHN_LORESERVE)
279 oshdr.put_sh_size(0);
280 else
281 oshdr.put_sh_size(section_count);
282
283 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
284 if (shstrndx < elfcpp::SHN_LORESERVE)
285 oshdr.put_sh_link(0);
286 else
287 oshdr.put_sh_link(shstrndx);
288
289 size_t segment_count = this->segment_list_->size();
290 oshdr.put_sh_info(segment_count >= elfcpp::PN_XNUM ? segment_count : 0);
291
292 oshdr.put_sh_addralign(0);
293 oshdr.put_sh_entsize(0);
294 }
295
296 v += shdr_size;
297
298 unsigned int shndx = 1;
299 if (!parameters->options().relocatable())
300 {
301 for (Layout::Segment_list::const_iterator p =
302 this->segment_list_->begin();
303 p != this->segment_list_->end();
304 ++p)
305 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
306 this->secnamepool_,
307 v,
308 &shndx);
309 }
310 else
311 {
312 for (Layout::Section_list::const_iterator p =
313 this->section_list_->begin();
314 p != this->section_list_->end();
315 ++p)
316 {
317 // We do unallocated sections below, except that group
318 // sections have to come first.
319 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
320 && (*p)->type() != elfcpp::SHT_GROUP)
321 continue;
322 gold_assert(shndx == (*p)->out_shndx());
323 elfcpp::Shdr_write<size, big_endian> oshdr(v);
324 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
325 v += shdr_size;
326 ++shndx;
327 }
328 }
329
330 for (Layout::Section_list::const_iterator p =
331 this->unattached_section_list_->begin();
332 p != this->unattached_section_list_->end();
333 ++p)
334 {
335 // For a relocatable link, we did unallocated group sections
336 // above, since they have to come first.
337 if ((*p)->type() == elfcpp::SHT_GROUP
338 && parameters->options().relocatable())
339 continue;
340 gold_assert(shndx == (*p)->out_shndx());
341 elfcpp::Shdr_write<size, big_endian> oshdr(v);
342 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
343 v += shdr_size;
344 ++shndx;
345 }
346
347 of->write_output_view(this->offset(), all_shdrs_size, view);
348 }
349
350 // Output_segment_header methods.
351
352 Output_segment_headers::Output_segment_headers(
353 const Layout::Segment_list& segment_list)
354 : segment_list_(segment_list)
355 {
356 this->set_current_data_size_for_child(this->do_size());
357 }
358
359 void
360 Output_segment_headers::do_write(Output_file* of)
361 {
362 switch (parameters->size_and_endianness())
363 {
364 #ifdef HAVE_TARGET_32_LITTLE
365 case Parameters::TARGET_32_LITTLE:
366 this->do_sized_write<32, false>(of);
367 break;
368 #endif
369 #ifdef HAVE_TARGET_32_BIG
370 case Parameters::TARGET_32_BIG:
371 this->do_sized_write<32, true>(of);
372 break;
373 #endif
374 #ifdef HAVE_TARGET_64_LITTLE
375 case Parameters::TARGET_64_LITTLE:
376 this->do_sized_write<64, false>(of);
377 break;
378 #endif
379 #ifdef HAVE_TARGET_64_BIG
380 case Parameters::TARGET_64_BIG:
381 this->do_sized_write<64, true>(of);
382 break;
383 #endif
384 default:
385 gold_unreachable();
386 }
387 }
388
389 template<int size, bool big_endian>
390 void
391 Output_segment_headers::do_sized_write(Output_file* of)
392 {
393 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
394 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
395 gold_assert(all_phdrs_size == this->data_size());
396 unsigned char* view = of->get_output_view(this->offset(),
397 all_phdrs_size);
398 unsigned char* v = view;
399 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
400 p != this->segment_list_.end();
401 ++p)
402 {
403 elfcpp::Phdr_write<size, big_endian> ophdr(v);
404 (*p)->write_header(&ophdr);
405 v += phdr_size;
406 }
407
408 gold_assert(v - view == all_phdrs_size);
409
410 of->write_output_view(this->offset(), all_phdrs_size, view);
411 }
412
413 off_t
414 Output_segment_headers::do_size() const
415 {
416 const int size = parameters->target().get_size();
417 int phdr_size;
418 if (size == 32)
419 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
420 else if (size == 64)
421 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
422 else
423 gold_unreachable();
424
425 return this->segment_list_.size() * phdr_size;
426 }
427
428 // Output_file_header methods.
429
430 Output_file_header::Output_file_header(const Target* target,
431 const Symbol_table* symtab,
432 const Output_segment_headers* osh)
433 : target_(target),
434 symtab_(symtab),
435 segment_header_(osh),
436 section_header_(NULL),
437 shstrtab_(NULL)
438 {
439 this->set_data_size(this->do_size());
440 }
441
442 // Set the section table information for a file header.
443
444 void
445 Output_file_header::set_section_info(const Output_section_headers* shdrs,
446 const Output_section* shstrtab)
447 {
448 this->section_header_ = shdrs;
449 this->shstrtab_ = shstrtab;
450 }
451
452 // Write out the file header.
453
454 void
455 Output_file_header::do_write(Output_file* of)
456 {
457 gold_assert(this->offset() == 0);
458
459 switch (parameters->size_and_endianness())
460 {
461 #ifdef HAVE_TARGET_32_LITTLE
462 case Parameters::TARGET_32_LITTLE:
463 this->do_sized_write<32, false>(of);
464 break;
465 #endif
466 #ifdef HAVE_TARGET_32_BIG
467 case Parameters::TARGET_32_BIG:
468 this->do_sized_write<32, true>(of);
469 break;
470 #endif
471 #ifdef HAVE_TARGET_64_LITTLE
472 case Parameters::TARGET_64_LITTLE:
473 this->do_sized_write<64, false>(of);
474 break;
475 #endif
476 #ifdef HAVE_TARGET_64_BIG
477 case Parameters::TARGET_64_BIG:
478 this->do_sized_write<64, true>(of);
479 break;
480 #endif
481 default:
482 gold_unreachable();
483 }
484 }
485
486 // Write out the file header with appropriate size and endianness.
487
488 template<int size, bool big_endian>
489 void
490 Output_file_header::do_sized_write(Output_file* of)
491 {
492 gold_assert(this->offset() == 0);
493
494 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
495 unsigned char* view = of->get_output_view(0, ehdr_size);
496 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
497
498 unsigned char e_ident[elfcpp::EI_NIDENT];
499 memset(e_ident, 0, elfcpp::EI_NIDENT);
500 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
501 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
502 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
503 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
504 if (size == 32)
505 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
506 else if (size == 64)
507 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
508 else
509 gold_unreachable();
510 e_ident[elfcpp::EI_DATA] = (big_endian
511 ? elfcpp::ELFDATA2MSB
512 : elfcpp::ELFDATA2LSB);
513 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
514 oehdr.put_e_ident(e_ident);
515
516 elfcpp::ET e_type;
517 if (parameters->options().relocatable())
518 e_type = elfcpp::ET_REL;
519 else if (parameters->options().output_is_position_independent())
520 e_type = elfcpp::ET_DYN;
521 else
522 e_type = elfcpp::ET_EXEC;
523 oehdr.put_e_type(e_type);
524
525 oehdr.put_e_machine(this->target_->machine_code());
526 oehdr.put_e_version(elfcpp::EV_CURRENT);
527
528 oehdr.put_e_entry(this->entry<size>());
529
530 if (this->segment_header_ == NULL)
531 oehdr.put_e_phoff(0);
532 else
533 oehdr.put_e_phoff(this->segment_header_->offset());
534
535 oehdr.put_e_shoff(this->section_header_->offset());
536 oehdr.put_e_flags(this->target_->processor_specific_flags());
537 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
538
539 if (this->segment_header_ == NULL)
540 {
541 oehdr.put_e_phentsize(0);
542 oehdr.put_e_phnum(0);
543 }
544 else
545 {
546 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
547 size_t phnum = (this->segment_header_->data_size()
548 / elfcpp::Elf_sizes<size>::phdr_size);
549 if (phnum > elfcpp::PN_XNUM)
550 phnum = elfcpp::PN_XNUM;
551 oehdr.put_e_phnum(phnum);
552 }
553
554 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
555 size_t section_count = (this->section_header_->data_size()
556 / elfcpp::Elf_sizes<size>::shdr_size);
557
558 if (section_count < elfcpp::SHN_LORESERVE)
559 oehdr.put_e_shnum(this->section_header_->data_size()
560 / elfcpp::Elf_sizes<size>::shdr_size);
561 else
562 oehdr.put_e_shnum(0);
563
564 unsigned int shstrndx = this->shstrtab_->out_shndx();
565 if (shstrndx < elfcpp::SHN_LORESERVE)
566 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
567 else
568 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
569
570 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
571 // the e_ident field.
572 parameters->target().adjust_elf_header(view, ehdr_size);
573
574 of->write_output_view(0, ehdr_size, view);
575 }
576
577 // Return the value to use for the entry address.
578
579 template<int size>
580 typename elfcpp::Elf_types<size>::Elf_Addr
581 Output_file_header::entry()
582 {
583 const bool should_issue_warning = (parameters->options().entry() != NULL
584 && !parameters->options().relocatable()
585 && !parameters->options().shared());
586 const char* entry = parameters->entry();
587 Symbol* sym = this->symtab_->lookup(entry);
588
589 typename Sized_symbol<size>::Value_type v;
590 if (sym != NULL)
591 {
592 Sized_symbol<size>* ssym;
593 ssym = this->symtab_->get_sized_symbol<size>(sym);
594 if (!ssym->is_defined() && should_issue_warning)
595 gold_warning("entry symbol '%s' exists but is not defined", entry);
596 v = ssym->value();
597 }
598 else
599 {
600 // We couldn't find the entry symbol. See if we can parse it as
601 // a number. This supports, e.g., -e 0x1000.
602 char* endptr;
603 v = strtoull(entry, &endptr, 0);
604 if (*endptr != '\0')
605 {
606 if (should_issue_warning)
607 gold_warning("cannot find entry symbol '%s'", entry);
608 v = 0;
609 }
610 }
611
612 return v;
613 }
614
615 // Compute the current data size.
616
617 off_t
618 Output_file_header::do_size() const
619 {
620 const int size = parameters->target().get_size();
621 if (size == 32)
622 return elfcpp::Elf_sizes<32>::ehdr_size;
623 else if (size == 64)
624 return elfcpp::Elf_sizes<64>::ehdr_size;
625 else
626 gold_unreachable();
627 }
628
629 // Output_data_const methods.
630
631 void
632 Output_data_const::do_write(Output_file* of)
633 {
634 of->write(this->offset(), this->data_.data(), this->data_.size());
635 }
636
637 // Output_data_const_buffer methods.
638
639 void
640 Output_data_const_buffer::do_write(Output_file* of)
641 {
642 of->write(this->offset(), this->p_, this->data_size());
643 }
644
645 // Output_section_data methods.
646
647 // Record the output section, and set the entry size and such.
648
649 void
650 Output_section_data::set_output_section(Output_section* os)
651 {
652 gold_assert(this->output_section_ == NULL);
653 this->output_section_ = os;
654 this->do_adjust_output_section(os);
655 }
656
657 // Return the section index of the output section.
658
659 unsigned int
660 Output_section_data::do_out_shndx() const
661 {
662 gold_assert(this->output_section_ != NULL);
663 return this->output_section_->out_shndx();
664 }
665
666 // Set the alignment, which means we may need to update the alignment
667 // of the output section.
668
669 void
670 Output_section_data::set_addralign(uint64_t addralign)
671 {
672 this->addralign_ = addralign;
673 if (this->output_section_ != NULL
674 && this->output_section_->addralign() < addralign)
675 this->output_section_->set_addralign(addralign);
676 }
677
678 // Output_data_strtab methods.
679
680 // Set the final data size.
681
682 void
683 Output_data_strtab::set_final_data_size()
684 {
685 this->strtab_->set_string_offsets();
686 this->set_data_size(this->strtab_->get_strtab_size());
687 }
688
689 // Write out a string table.
690
691 void
692 Output_data_strtab::do_write(Output_file* of)
693 {
694 this->strtab_->write(of, this->offset());
695 }
696
697 // Output_reloc methods.
698
699 // A reloc against a global symbol.
700
701 template<bool dynamic, int size, bool big_endian>
702 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
703 Symbol* gsym,
704 unsigned int type,
705 Output_data* od,
706 Address address,
707 bool is_relative,
708 bool is_symbolless,
709 bool use_plt_offset)
710 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
711 is_relative_(is_relative), is_symbolless_(is_symbolless),
712 is_section_symbol_(false), use_plt_offset_(use_plt_offset), shndx_(INVALID_CODE)
713 {
714 // this->type_ is a bitfield; make sure TYPE fits.
715 gold_assert(this->type_ == type);
716 this->u1_.gsym = gsym;
717 this->u2_.od = od;
718 if (dynamic)
719 this->set_needs_dynsym_index();
720 }
721
722 template<bool dynamic, int size, bool big_endian>
723 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
724 Symbol* gsym,
725 unsigned int type,
726 Sized_relobj<size, big_endian>* relobj,
727 unsigned int shndx,
728 Address address,
729 bool is_relative,
730 bool is_symbolless,
731 bool use_plt_offset)
732 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
733 is_relative_(is_relative), is_symbolless_(is_symbolless),
734 is_section_symbol_(false), use_plt_offset_(use_plt_offset), shndx_(shndx)
735 {
736 gold_assert(shndx != INVALID_CODE);
737 // this->type_ is a bitfield; make sure TYPE fits.
738 gold_assert(this->type_ == type);
739 this->u1_.gsym = gsym;
740 this->u2_.relobj = relobj;
741 if (dynamic)
742 this->set_needs_dynsym_index();
743 }
744
745 // A reloc against a local symbol.
746
747 template<bool dynamic, int size, bool big_endian>
748 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
749 Sized_relobj<size, big_endian>* relobj,
750 unsigned int local_sym_index,
751 unsigned int type,
752 Output_data* od,
753 Address address,
754 bool is_relative,
755 bool is_symbolless,
756 bool is_section_symbol,
757 bool use_plt_offset)
758 : address_(address), local_sym_index_(local_sym_index), type_(type),
759 is_relative_(is_relative), is_symbolless_(is_symbolless),
760 is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
761 shndx_(INVALID_CODE)
762 {
763 gold_assert(local_sym_index != GSYM_CODE
764 && local_sym_index != INVALID_CODE);
765 // this->type_ is a bitfield; make sure TYPE fits.
766 gold_assert(this->type_ == type);
767 this->u1_.relobj = relobj;
768 this->u2_.od = od;
769 if (dynamic)
770 this->set_needs_dynsym_index();
771 }
772
773 template<bool dynamic, int size, bool big_endian>
774 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
775 Sized_relobj<size, big_endian>* relobj,
776 unsigned int local_sym_index,
777 unsigned int type,
778 unsigned int shndx,
779 Address address,
780 bool is_relative,
781 bool is_symbolless,
782 bool is_section_symbol,
783 bool use_plt_offset)
784 : address_(address), local_sym_index_(local_sym_index), type_(type),
785 is_relative_(is_relative), is_symbolless_(is_symbolless),
786 is_section_symbol_(is_section_symbol), use_plt_offset_(use_plt_offset),
787 shndx_(shndx)
788 {
789 gold_assert(local_sym_index != GSYM_CODE
790 && local_sym_index != INVALID_CODE);
791 gold_assert(shndx != INVALID_CODE);
792 // this->type_ is a bitfield; make sure TYPE fits.
793 gold_assert(this->type_ == type);
794 this->u1_.relobj = relobj;
795 this->u2_.relobj = relobj;
796 if (dynamic)
797 this->set_needs_dynsym_index();
798 }
799
800 // A reloc against the STT_SECTION symbol of an output section.
801
802 template<bool dynamic, int size, bool big_endian>
803 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
804 Output_section* os,
805 unsigned int type,
806 Output_data* od,
807 Address address)
808 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
809 is_relative_(false), is_symbolless_(false),
810 is_section_symbol_(true), use_plt_offset_(false), shndx_(INVALID_CODE)
811 {
812 // this->type_ is a bitfield; make sure TYPE fits.
813 gold_assert(this->type_ == type);
814 this->u1_.os = os;
815 this->u2_.od = od;
816 if (dynamic)
817 this->set_needs_dynsym_index();
818 else
819 os->set_needs_symtab_index();
820 }
821
822 template<bool dynamic, int size, bool big_endian>
823 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
824 Output_section* os,
825 unsigned int type,
826 Sized_relobj<size, big_endian>* relobj,
827 unsigned int shndx,
828 Address address)
829 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
830 is_relative_(false), is_symbolless_(false),
831 is_section_symbol_(true), use_plt_offset_(false), shndx_(shndx)
832 {
833 gold_assert(shndx != INVALID_CODE);
834 // this->type_ is a bitfield; make sure TYPE fits.
835 gold_assert(this->type_ == type);
836 this->u1_.os = os;
837 this->u2_.relobj = relobj;
838 if (dynamic)
839 this->set_needs_dynsym_index();
840 else
841 os->set_needs_symtab_index();
842 }
843
844 // An absolute relocation.
845
846 template<bool dynamic, int size, bool big_endian>
847 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
848 unsigned int type,
849 Output_data* od,
850 Address address)
851 : address_(address), local_sym_index_(0), type_(type),
852 is_relative_(false), is_symbolless_(false),
853 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
854 {
855 // this->type_ is a bitfield; make sure TYPE fits.
856 gold_assert(this->type_ == type);
857 this->u1_.relobj = NULL;
858 this->u2_.od = od;
859 }
860
861 template<bool dynamic, int size, bool big_endian>
862 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
863 unsigned int type,
864 Sized_relobj<size, big_endian>* relobj,
865 unsigned int shndx,
866 Address address)
867 : address_(address), local_sym_index_(0), type_(type),
868 is_relative_(false), is_symbolless_(false),
869 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
870 {
871 gold_assert(shndx != INVALID_CODE);
872 // this->type_ is a bitfield; make sure TYPE fits.
873 gold_assert(this->type_ == type);
874 this->u1_.relobj = NULL;
875 this->u2_.relobj = relobj;
876 }
877
878 // A target specific relocation.
879
880 template<bool dynamic, int size, bool big_endian>
881 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
882 unsigned int type,
883 void* arg,
884 Output_data* od,
885 Address address)
886 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
887 is_relative_(false), is_symbolless_(false),
888 is_section_symbol_(false), use_plt_offset_(false), shndx_(INVALID_CODE)
889 {
890 // this->type_ is a bitfield; make sure TYPE fits.
891 gold_assert(this->type_ == type);
892 this->u1_.arg = arg;
893 this->u2_.od = od;
894 }
895
896 template<bool dynamic, int size, bool big_endian>
897 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
898 unsigned int type,
899 void* arg,
900 Sized_relobj<size, big_endian>* relobj,
901 unsigned int shndx,
902 Address address)
903 : address_(address), local_sym_index_(TARGET_CODE), type_(type),
904 is_relative_(false), is_symbolless_(false),
905 is_section_symbol_(false), use_plt_offset_(false), shndx_(shndx)
906 {
907 gold_assert(shndx != INVALID_CODE);
908 // this->type_ is a bitfield; make sure TYPE fits.
909 gold_assert(this->type_ == type);
910 this->u1_.arg = arg;
911 this->u2_.relobj = relobj;
912 }
913
914 // Record that we need a dynamic symbol index for this relocation.
915
916 template<bool dynamic, int size, bool big_endian>
917 void
918 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
919 set_needs_dynsym_index()
920 {
921 if (this->is_symbolless_)
922 return;
923 switch (this->local_sym_index_)
924 {
925 case INVALID_CODE:
926 gold_unreachable();
927
928 case GSYM_CODE:
929 this->u1_.gsym->set_needs_dynsym_entry();
930 break;
931
932 case SECTION_CODE:
933 this->u1_.os->set_needs_dynsym_index();
934 break;
935
936 case TARGET_CODE:
937 // The target must take care of this if necessary.
938 break;
939
940 case 0:
941 break;
942
943 default:
944 {
945 const unsigned int lsi = this->local_sym_index_;
946 Sized_relobj_file<size, big_endian>* relobj =
947 this->u1_.relobj->sized_relobj();
948 gold_assert(relobj != NULL);
949 if (!this->is_section_symbol_)
950 relobj->set_needs_output_dynsym_entry(lsi);
951 else
952 relobj->output_section(lsi)->set_needs_dynsym_index();
953 }
954 break;
955 }
956 }
957
958 // Get the symbol index of a relocation.
959
960 template<bool dynamic, int size, bool big_endian>
961 unsigned int
962 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
963 const
964 {
965 unsigned int index;
966 if (this->is_symbolless_)
967 return 0;
968 switch (this->local_sym_index_)
969 {
970 case INVALID_CODE:
971 gold_unreachable();
972
973 case GSYM_CODE:
974 if (this->u1_.gsym == NULL)
975 index = 0;
976 else if (dynamic)
977 index = this->u1_.gsym->dynsym_index();
978 else
979 index = this->u1_.gsym->symtab_index();
980 break;
981
982 case SECTION_CODE:
983 if (dynamic)
984 index = this->u1_.os->dynsym_index();
985 else
986 index = this->u1_.os->symtab_index();
987 break;
988
989 case TARGET_CODE:
990 index = parameters->target().reloc_symbol_index(this->u1_.arg,
991 this->type_);
992 break;
993
994 case 0:
995 // Relocations without symbols use a symbol index of 0.
996 index = 0;
997 break;
998
999 default:
1000 {
1001 const unsigned int lsi = this->local_sym_index_;
1002 Sized_relobj_file<size, big_endian>* relobj =
1003 this->u1_.relobj->sized_relobj();
1004 gold_assert(relobj != NULL);
1005 if (!this->is_section_symbol_)
1006 {
1007 if (dynamic)
1008 index = relobj->dynsym_index(lsi);
1009 else
1010 index = relobj->symtab_index(lsi);
1011 }
1012 else
1013 {
1014 Output_section* os = relobj->output_section(lsi);
1015 gold_assert(os != NULL);
1016 if (dynamic)
1017 index = os->dynsym_index();
1018 else
1019 index = os->symtab_index();
1020 }
1021 }
1022 break;
1023 }
1024 gold_assert(index != -1U);
1025 return index;
1026 }
1027
1028 // For a local section symbol, get the address of the offset ADDEND
1029 // within the input section.
1030
1031 template<bool dynamic, int size, bool big_endian>
1032 typename elfcpp::Elf_types<size>::Elf_Addr
1033 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1034 local_section_offset(Addend addend) const
1035 {
1036 gold_assert(this->local_sym_index_ != GSYM_CODE
1037 && this->local_sym_index_ != SECTION_CODE
1038 && this->local_sym_index_ != TARGET_CODE
1039 && this->local_sym_index_ != INVALID_CODE
1040 && this->local_sym_index_ != 0
1041 && this->is_section_symbol_);
1042 const unsigned int lsi = this->local_sym_index_;
1043 Output_section* os = this->u1_.relobj->output_section(lsi);
1044 gold_assert(os != NULL);
1045 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
1046 if (offset != invalid_address)
1047 return offset + addend;
1048 // This is a merge section.
1049 Sized_relobj_file<size, big_endian>* relobj =
1050 this->u1_.relobj->sized_relobj();
1051 gold_assert(relobj != NULL);
1052 offset = os->output_address(relobj, lsi, addend);
1053 gold_assert(offset != invalid_address);
1054 return offset;
1055 }
1056
1057 // Get the output address of a relocation.
1058
1059 template<bool dynamic, int size, bool big_endian>
1060 typename elfcpp::Elf_types<size>::Elf_Addr
1061 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
1062 {
1063 Address address = this->address_;
1064 if (this->shndx_ != INVALID_CODE)
1065 {
1066 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
1067 gold_assert(os != NULL);
1068 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
1069 if (off != invalid_address)
1070 address += os->address() + off;
1071 else
1072 {
1073 Sized_relobj_file<size, big_endian>* relobj =
1074 this->u2_.relobj->sized_relobj();
1075 gold_assert(relobj != NULL);
1076 address = os->output_address(relobj, this->shndx_, address);
1077 gold_assert(address != invalid_address);
1078 }
1079 }
1080 else if (this->u2_.od != NULL)
1081 address += this->u2_.od->address();
1082 return address;
1083 }
1084
1085 // Write out the offset and info fields of a Rel or Rela relocation
1086 // entry.
1087
1088 template<bool dynamic, int size, bool big_endian>
1089 template<typename Write_rel>
1090 void
1091 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
1092 Write_rel* wr) const
1093 {
1094 wr->put_r_offset(this->get_address());
1095 unsigned int sym_index = this->get_symbol_index();
1096 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
1097 }
1098
1099 // Write out a Rel relocation.
1100
1101 template<bool dynamic, int size, bool big_endian>
1102 void
1103 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
1104 unsigned char* pov) const
1105 {
1106 elfcpp::Rel_write<size, big_endian> orel(pov);
1107 this->write_rel(&orel);
1108 }
1109
1110 // Get the value of the symbol referred to by a Rel relocation.
1111
1112 template<bool dynamic, int size, bool big_endian>
1113 typename elfcpp::Elf_types<size>::Elf_Addr
1114 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
1115 Addend addend) const
1116 {
1117 if (this->local_sym_index_ == GSYM_CODE)
1118 {
1119 const Sized_symbol<size>* sym;
1120 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
1121 if (this->use_plt_offset_ && sym->has_plt_offset())
1122 {
1123 uint64_t plt_address =
1124 parameters->target().plt_address_for_global(sym);
1125 return plt_address + sym->plt_offset();
1126 }
1127 else
1128 return sym->value() + addend;
1129 }
1130 gold_assert(this->local_sym_index_ != SECTION_CODE
1131 && this->local_sym_index_ != TARGET_CODE
1132 && this->local_sym_index_ != INVALID_CODE
1133 && this->local_sym_index_ != 0
1134 && !this->is_section_symbol_);
1135 const unsigned int lsi = this->local_sym_index_;
1136 Sized_relobj_file<size, big_endian>* relobj =
1137 this->u1_.relobj->sized_relobj();
1138 gold_assert(relobj != NULL);
1139 if (this->use_plt_offset_)
1140 {
1141 uint64_t plt_address =
1142 parameters->target().plt_address_for_local(relobj, lsi);
1143 return plt_address + relobj->local_plt_offset(lsi);
1144 }
1145 const Symbol_value<size>* symval = relobj->local_symbol(lsi);
1146 return symval->value(relobj, addend);
1147 }
1148
1149 // Reloc comparison. This function sorts the dynamic relocs for the
1150 // benefit of the dynamic linker. First we sort all relative relocs
1151 // to the front. Among relative relocs, we sort by output address.
1152 // Among non-relative relocs, we sort by symbol index, then by output
1153 // address.
1154
1155 template<bool dynamic, int size, bool big_endian>
1156 int
1157 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
1158 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1159 const
1160 {
1161 if (this->is_relative_)
1162 {
1163 if (!r2.is_relative_)
1164 return -1;
1165 // Otherwise sort by reloc address below.
1166 }
1167 else if (r2.is_relative_)
1168 return 1;
1169 else
1170 {
1171 unsigned int sym1 = this->get_symbol_index();
1172 unsigned int sym2 = r2.get_symbol_index();
1173 if (sym1 < sym2)
1174 return -1;
1175 else if (sym1 > sym2)
1176 return 1;
1177 // Otherwise sort by reloc address.
1178 }
1179
1180 section_offset_type addr1 = this->get_address();
1181 section_offset_type addr2 = r2.get_address();
1182 if (addr1 < addr2)
1183 return -1;
1184 else if (addr1 > addr2)
1185 return 1;
1186
1187 // Final tie breaker, in order to generate the same output on any
1188 // host: reloc type.
1189 unsigned int type1 = this->type_;
1190 unsigned int type2 = r2.type_;
1191 if (type1 < type2)
1192 return -1;
1193 else if (type1 > type2)
1194 return 1;
1195
1196 // These relocs appear to be exactly the same.
1197 return 0;
1198 }
1199
1200 // Write out a Rela relocation.
1201
1202 template<bool dynamic, int size, bool big_endian>
1203 void
1204 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1205 unsigned char* pov) const
1206 {
1207 elfcpp::Rela_write<size, big_endian> orel(pov);
1208 this->rel_.write_rel(&orel);
1209 Addend addend = this->addend_;
1210 if (this->rel_.is_target_specific())
1211 addend = parameters->target().reloc_addend(this->rel_.target_arg(),
1212 this->rel_.type(), addend);
1213 else if (this->rel_.is_symbolless())
1214 addend = this->rel_.symbol_value(addend);
1215 else if (this->rel_.is_local_section_symbol())
1216 addend = this->rel_.local_section_offset(addend);
1217 orel.put_r_addend(addend);
1218 }
1219
1220 // Output_data_reloc_base methods.
1221
1222 // Adjust the output section.
1223
1224 template<int sh_type, bool dynamic, int size, bool big_endian>
1225 void
1226 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1227 ::do_adjust_output_section(Output_section* os)
1228 {
1229 if (sh_type == elfcpp::SHT_REL)
1230 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1231 else if (sh_type == elfcpp::SHT_RELA)
1232 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1233 else
1234 gold_unreachable();
1235
1236 // A STT_GNU_IFUNC symbol may require a IRELATIVE reloc when doing a
1237 // static link. The backends will generate a dynamic reloc section
1238 // to hold this. In that case we don't want to link to the dynsym
1239 // section, because there isn't one.
1240 if (!dynamic)
1241 os->set_should_link_to_symtab();
1242 else if (parameters->doing_static_link())
1243 ;
1244 else
1245 os->set_should_link_to_dynsym();
1246 }
1247
1248 // Write out relocation data.
1249
1250 template<int sh_type, bool dynamic, int size, bool big_endian>
1251 void
1252 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1253 Output_file* of)
1254 {
1255 const off_t off = this->offset();
1256 const off_t oview_size = this->data_size();
1257 unsigned char* const oview = of->get_output_view(off, oview_size);
1258
1259 if (this->sort_relocs())
1260 {
1261 gold_assert(dynamic);
1262 std::sort(this->relocs_.begin(), this->relocs_.end(),
1263 Sort_relocs_comparison());
1264 }
1265
1266 unsigned char* pov = oview;
1267 for (typename Relocs::const_iterator p = this->relocs_.begin();
1268 p != this->relocs_.end();
1269 ++p)
1270 {
1271 p->write(pov);
1272 pov += reloc_size;
1273 }
1274
1275 gold_assert(pov - oview == oview_size);
1276
1277 of->write_output_view(off, oview_size, oview);
1278
1279 // We no longer need the relocation entries.
1280 this->relocs_.clear();
1281 }
1282
1283 // Class Output_relocatable_relocs.
1284
1285 template<int sh_type, int size, bool big_endian>
1286 void
1287 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1288 {
1289 this->set_data_size(this->rr_->output_reloc_count()
1290 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1291 }
1292
1293 // class Output_data_group.
1294
1295 template<int size, bool big_endian>
1296 Output_data_group<size, big_endian>::Output_data_group(
1297 Sized_relobj_file<size, big_endian>* relobj,
1298 section_size_type entry_count,
1299 elfcpp::Elf_Word flags,
1300 std::vector<unsigned int>* input_shndxes)
1301 : Output_section_data(entry_count * 4, 4, false),
1302 relobj_(relobj),
1303 flags_(flags)
1304 {
1305 this->input_shndxes_.swap(*input_shndxes);
1306 }
1307
1308 // Write out the section group, which means translating the section
1309 // indexes to apply to the output file.
1310
1311 template<int size, bool big_endian>
1312 void
1313 Output_data_group<size, big_endian>::do_write(Output_file* of)
1314 {
1315 const off_t off = this->offset();
1316 const section_size_type oview_size =
1317 convert_to_section_size_type(this->data_size());
1318 unsigned char* const oview = of->get_output_view(off, oview_size);
1319
1320 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1321 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1322 ++contents;
1323
1324 for (std::vector<unsigned int>::const_iterator p =
1325 this->input_shndxes_.begin();
1326 p != this->input_shndxes_.end();
1327 ++p, ++contents)
1328 {
1329 Output_section* os = this->relobj_->output_section(*p);
1330
1331 unsigned int output_shndx;
1332 if (os != NULL)
1333 output_shndx = os->out_shndx();
1334 else
1335 {
1336 this->relobj_->error(_("section group retained but "
1337 "group element discarded"));
1338 output_shndx = 0;
1339 }
1340
1341 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1342 }
1343
1344 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1345 gold_assert(wrote == oview_size);
1346
1347 of->write_output_view(off, oview_size, oview);
1348
1349 // We no longer need this information.
1350 this->input_shndxes_.clear();
1351 }
1352
1353 // Output_data_got::Got_entry methods.
1354
1355 // Write out the entry.
1356
1357 template<int size, bool big_endian>
1358 void
1359 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1360 {
1361 Valtype val = 0;
1362
1363 switch (this->local_sym_index_)
1364 {
1365 case GSYM_CODE:
1366 {
1367 // If the symbol is resolved locally, we need to write out the
1368 // link-time value, which will be relocated dynamically by a
1369 // RELATIVE relocation.
1370 Symbol* gsym = this->u_.gsym;
1371 if (this->use_plt_offset_ && gsym->has_plt_offset())
1372 val = (parameters->target().plt_address_for_global(gsym)
1373 + gsym->plt_offset());
1374 else
1375 {
1376 Sized_symbol<size>* sgsym;
1377 // This cast is a bit ugly. We don't want to put a
1378 // virtual method in Symbol, because we want Symbol to be
1379 // as small as possible.
1380 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1381 val = sgsym->value();
1382 }
1383 }
1384 break;
1385
1386 case CONSTANT_CODE:
1387 val = this->u_.constant;
1388 break;
1389
1390 case RESERVED_CODE:
1391 // If we're doing an incremental update, don't touch this GOT entry.
1392 if (parameters->incremental_update())
1393 return;
1394 val = this->u_.constant;
1395 break;
1396
1397 default:
1398 {
1399 const Relobj* object = this->u_.object;
1400 const unsigned int lsi = this->local_sym_index_;
1401 if (!this->use_plt_offset_)
1402 {
1403 uint64_t lval = object->local_symbol_value(lsi, 0);
1404 val = convert_types<Valtype, uint64_t>(lval);
1405 }
1406 else
1407 {
1408 uint64_t plt_address =
1409 parameters->target().plt_address_for_local(object, lsi);
1410 val = plt_address + object->local_plt_offset(lsi);
1411 }
1412 }
1413 break;
1414 }
1415
1416 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1417 }
1418
1419 // Output_data_got methods.
1420
1421 // Add an entry for a global symbol to the GOT. This returns true if
1422 // this is a new GOT entry, false if the symbol already had a GOT
1423 // entry.
1424
1425 template<int size, bool big_endian>
1426 bool
1427 Output_data_got<size, big_endian>::add_global(
1428 Symbol* gsym,
1429 unsigned int got_type)
1430 {
1431 if (gsym->has_got_offset(got_type))
1432 return false;
1433
1434 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, false));
1435 gsym->set_got_offset(got_type, got_offset);
1436 return true;
1437 }
1438
1439 // Like add_global, but use the PLT offset.
1440
1441 template<int size, bool big_endian>
1442 bool
1443 Output_data_got<size, big_endian>::add_global_plt(Symbol* gsym,
1444 unsigned int got_type)
1445 {
1446 if (gsym->has_got_offset(got_type))
1447 return false;
1448
1449 unsigned int got_offset = this->add_got_entry(Got_entry(gsym, true));
1450 gsym->set_got_offset(got_type, got_offset);
1451 return true;
1452 }
1453
1454 // Add an entry for a global symbol to the GOT, and add a dynamic
1455 // relocation of type R_TYPE for the GOT entry.
1456
1457 template<int size, bool big_endian>
1458 void
1459 Output_data_got<size, big_endian>::add_global_with_rel(
1460 Symbol* gsym,
1461 unsigned int got_type,
1462 Output_data_reloc_generic* rel_dyn,
1463 unsigned int r_type)
1464 {
1465 if (gsym->has_got_offset(got_type))
1466 return;
1467
1468 unsigned int got_offset = this->add_got_entry(Got_entry());
1469 gsym->set_got_offset(got_type, got_offset);
1470 rel_dyn->add_global_generic(gsym, r_type, this, got_offset, 0);
1471 }
1472
1473 // Add a pair of entries for a global symbol to the GOT, and add
1474 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1475 // If R_TYPE_2 == 0, add the second entry with no relocation.
1476 template<int size, bool big_endian>
1477 void
1478 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1479 Symbol* gsym,
1480 unsigned int got_type,
1481 Output_data_reloc_generic* rel_dyn,
1482 unsigned int r_type_1,
1483 unsigned int r_type_2)
1484 {
1485 if (gsym->has_got_offset(got_type))
1486 return;
1487
1488 unsigned int got_offset = this->add_got_entry_pair(Got_entry(), Got_entry());
1489 gsym->set_got_offset(got_type, got_offset);
1490 rel_dyn->add_global_generic(gsym, r_type_1, this, got_offset, 0);
1491
1492 if (r_type_2 != 0)
1493 rel_dyn->add_global_generic(gsym, r_type_2, this,
1494 got_offset + size / 8, 0);
1495 }
1496
1497 // Add an entry for a local symbol to the GOT. This returns true if
1498 // this is a new GOT entry, false if the symbol already has a GOT
1499 // entry.
1500
1501 template<int size, bool big_endian>
1502 bool
1503 Output_data_got<size, big_endian>::add_local(
1504 Relobj* object,
1505 unsigned int symndx,
1506 unsigned int got_type)
1507 {
1508 if (object->local_has_got_offset(symndx, got_type))
1509 return false;
1510
1511 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1512 false));
1513 object->set_local_got_offset(symndx, got_type, got_offset);
1514 return true;
1515 }
1516
1517 // Like add_local, but use the PLT offset.
1518
1519 template<int size, bool big_endian>
1520 bool
1521 Output_data_got<size, big_endian>::add_local_plt(
1522 Relobj* object,
1523 unsigned int symndx,
1524 unsigned int got_type)
1525 {
1526 if (object->local_has_got_offset(symndx, got_type))
1527 return false;
1528
1529 unsigned int got_offset = this->add_got_entry(Got_entry(object, symndx,
1530 true));
1531 object->set_local_got_offset(symndx, got_type, got_offset);
1532 return true;
1533 }
1534
1535 // Add an entry for a local symbol to the GOT, and add a dynamic
1536 // relocation of type R_TYPE for the GOT entry.
1537
1538 template<int size, bool big_endian>
1539 void
1540 Output_data_got<size, big_endian>::add_local_with_rel(
1541 Relobj* object,
1542 unsigned int symndx,
1543 unsigned int got_type,
1544 Output_data_reloc_generic* rel_dyn,
1545 unsigned int r_type)
1546 {
1547 if (object->local_has_got_offset(symndx, got_type))
1548 return;
1549
1550 unsigned int got_offset = this->add_got_entry(Got_entry());
1551 object->set_local_got_offset(symndx, got_type, got_offset);
1552 rel_dyn->add_local_generic(object, symndx, r_type, this, got_offset, 0);
1553 }
1554
1555 // Add a pair of entries for a local symbol to the GOT, and add
1556 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1557 // If R_TYPE_2 == 0, add the second entry with no relocation.
1558 template<int size, bool big_endian>
1559 void
1560 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1561 Relobj* object,
1562 unsigned int symndx,
1563 unsigned int shndx,
1564 unsigned int got_type,
1565 Output_data_reloc_generic* rel_dyn,
1566 unsigned int r_type_1,
1567 unsigned int r_type_2)
1568 {
1569 if (object->local_has_got_offset(symndx, got_type))
1570 return;
1571
1572 unsigned int got_offset =
1573 this->add_got_entry_pair(Got_entry(),
1574 Got_entry(object, symndx, false));
1575 object->set_local_got_offset(symndx, got_type, got_offset);
1576 Output_section* os = object->output_section(shndx);
1577 rel_dyn->add_output_section_generic(os, r_type_1, this, got_offset, 0);
1578
1579 if (r_type_2 != 0)
1580 rel_dyn->add_output_section_generic(os, r_type_2, this,
1581 got_offset + size / 8, 0);
1582 }
1583
1584 // Reserve a slot in the GOT for a local symbol or the second slot of a pair.
1585
1586 template<int size, bool big_endian>
1587 void
1588 Output_data_got<size, big_endian>::reserve_local(
1589 unsigned int i,
1590 Relobj* object,
1591 unsigned int sym_index,
1592 unsigned int got_type)
1593 {
1594 this->do_reserve_slot(i);
1595 object->set_local_got_offset(sym_index, got_type, this->got_offset(i));
1596 }
1597
1598 // Reserve a slot in the GOT for a global symbol.
1599
1600 template<int size, bool big_endian>
1601 void
1602 Output_data_got<size, big_endian>::reserve_global(
1603 unsigned int i,
1604 Symbol* gsym,
1605 unsigned int got_type)
1606 {
1607 this->do_reserve_slot(i);
1608 gsym->set_got_offset(got_type, this->got_offset(i));
1609 }
1610
1611 // Write out the GOT.
1612
1613 template<int size, bool big_endian>
1614 void
1615 Output_data_got<size, big_endian>::do_write(Output_file* of)
1616 {
1617 const int add = size / 8;
1618
1619 const off_t off = this->offset();
1620 const off_t oview_size = this->data_size();
1621 unsigned char* const oview = of->get_output_view(off, oview_size);
1622
1623 unsigned char* pov = oview;
1624 for (typename Got_entries::const_iterator p = this->entries_.begin();
1625 p != this->entries_.end();
1626 ++p)
1627 {
1628 p->write(pov);
1629 pov += add;
1630 }
1631
1632 gold_assert(pov - oview == oview_size);
1633
1634 of->write_output_view(off, oview_size, oview);
1635
1636 // We no longer need the GOT entries.
1637 this->entries_.clear();
1638 }
1639
1640 // Create a new GOT entry and return its offset.
1641
1642 template<int size, bool big_endian>
1643 unsigned int
1644 Output_data_got<size, big_endian>::add_got_entry(Got_entry got_entry)
1645 {
1646 if (!this->is_data_size_valid())
1647 {
1648 this->entries_.push_back(got_entry);
1649 this->set_got_size();
1650 return this->last_got_offset();
1651 }
1652 else
1653 {
1654 // For an incremental update, find an available slot.
1655 off_t got_offset = this->free_list_.allocate(size / 8, size / 8, 0);
1656 if (got_offset == -1)
1657 gold_fallback(_("out of patch space (GOT);"
1658 " relink with --incremental-full"));
1659 unsigned int got_index = got_offset / (size / 8);
1660 gold_assert(got_index < this->entries_.size());
1661 this->entries_[got_index] = got_entry;
1662 return static_cast<unsigned int>(got_offset);
1663 }
1664 }
1665
1666 // Create a pair of new GOT entries and return the offset of the first.
1667
1668 template<int size, bool big_endian>
1669 unsigned int
1670 Output_data_got<size, big_endian>::add_got_entry_pair(Got_entry got_entry_1,
1671 Got_entry got_entry_2)
1672 {
1673 if (!this->is_data_size_valid())
1674 {
1675 unsigned int got_offset;
1676 this->entries_.push_back(got_entry_1);
1677 got_offset = this->last_got_offset();
1678 this->entries_.push_back(got_entry_2);
1679 this->set_got_size();
1680 return got_offset;
1681 }
1682 else
1683 {
1684 // For an incremental update, find an available pair of slots.
1685 off_t got_offset = this->free_list_.allocate(2 * size / 8, size / 8, 0);
1686 if (got_offset == -1)
1687 gold_fallback(_("out of patch space (GOT);"
1688 " relink with --incremental-full"));
1689 unsigned int got_index = got_offset / (size / 8);
1690 gold_assert(got_index < this->entries_.size());
1691 this->entries_[got_index] = got_entry_1;
1692 this->entries_[got_index + 1] = got_entry_2;
1693 return static_cast<unsigned int>(got_offset);
1694 }
1695 }
1696
1697 // Output_data_dynamic::Dynamic_entry methods.
1698
1699 // Write out the entry.
1700
1701 template<int size, bool big_endian>
1702 void
1703 Output_data_dynamic::Dynamic_entry::write(
1704 unsigned char* pov,
1705 const Stringpool* pool) const
1706 {
1707 typename elfcpp::Elf_types<size>::Elf_WXword val;
1708 switch (this->offset_)
1709 {
1710 case DYNAMIC_NUMBER:
1711 val = this->u_.val;
1712 break;
1713
1714 case DYNAMIC_SECTION_SIZE:
1715 val = this->u_.od->data_size();
1716 if (this->od2 != NULL)
1717 val += this->od2->data_size();
1718 break;
1719
1720 case DYNAMIC_SYMBOL:
1721 {
1722 const Sized_symbol<size>* s =
1723 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1724 val = s->value();
1725 }
1726 break;
1727
1728 case DYNAMIC_STRING:
1729 val = pool->get_offset(this->u_.str);
1730 break;
1731
1732 default:
1733 val = this->u_.od->address() + this->offset_;
1734 break;
1735 }
1736
1737 elfcpp::Dyn_write<size, big_endian> dw(pov);
1738 dw.put_d_tag(this->tag_);
1739 dw.put_d_val(val);
1740 }
1741
1742 // Output_data_dynamic methods.
1743
1744 // Adjust the output section to set the entry size.
1745
1746 void
1747 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1748 {
1749 if (parameters->target().get_size() == 32)
1750 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1751 else if (parameters->target().get_size() == 64)
1752 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1753 else
1754 gold_unreachable();
1755 }
1756
1757 // Set the final data size.
1758
1759 void
1760 Output_data_dynamic::set_final_data_size()
1761 {
1762 // Add the terminating entry if it hasn't been added.
1763 // Because of relaxation, we can run this multiple times.
1764 if (this->entries_.empty() || this->entries_.back().tag() != elfcpp::DT_NULL)
1765 {
1766 int extra = parameters->options().spare_dynamic_tags();
1767 for (int i = 0; i < extra; ++i)
1768 this->add_constant(elfcpp::DT_NULL, 0);
1769 this->add_constant(elfcpp::DT_NULL, 0);
1770 }
1771
1772 int dyn_size;
1773 if (parameters->target().get_size() == 32)
1774 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1775 else if (parameters->target().get_size() == 64)
1776 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1777 else
1778 gold_unreachable();
1779 this->set_data_size(this->entries_.size() * dyn_size);
1780 }
1781
1782 // Write out the dynamic entries.
1783
1784 void
1785 Output_data_dynamic::do_write(Output_file* of)
1786 {
1787 switch (parameters->size_and_endianness())
1788 {
1789 #ifdef HAVE_TARGET_32_LITTLE
1790 case Parameters::TARGET_32_LITTLE:
1791 this->sized_write<32, false>(of);
1792 break;
1793 #endif
1794 #ifdef HAVE_TARGET_32_BIG
1795 case Parameters::TARGET_32_BIG:
1796 this->sized_write<32, true>(of);
1797 break;
1798 #endif
1799 #ifdef HAVE_TARGET_64_LITTLE
1800 case Parameters::TARGET_64_LITTLE:
1801 this->sized_write<64, false>(of);
1802 break;
1803 #endif
1804 #ifdef HAVE_TARGET_64_BIG
1805 case Parameters::TARGET_64_BIG:
1806 this->sized_write<64, true>(of);
1807 break;
1808 #endif
1809 default:
1810 gold_unreachable();
1811 }
1812 }
1813
1814 template<int size, bool big_endian>
1815 void
1816 Output_data_dynamic::sized_write(Output_file* of)
1817 {
1818 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1819
1820 const off_t offset = this->offset();
1821 const off_t oview_size = this->data_size();
1822 unsigned char* const oview = of->get_output_view(offset, oview_size);
1823
1824 unsigned char* pov = oview;
1825 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1826 p != this->entries_.end();
1827 ++p)
1828 {
1829 p->write<size, big_endian>(pov, this->pool_);
1830 pov += dyn_size;
1831 }
1832
1833 gold_assert(pov - oview == oview_size);
1834
1835 of->write_output_view(offset, oview_size, oview);
1836
1837 // We no longer need the dynamic entries.
1838 this->entries_.clear();
1839 }
1840
1841 // Class Output_symtab_xindex.
1842
1843 void
1844 Output_symtab_xindex::do_write(Output_file* of)
1845 {
1846 const off_t offset = this->offset();
1847 const off_t oview_size = this->data_size();
1848 unsigned char* const oview = of->get_output_view(offset, oview_size);
1849
1850 memset(oview, 0, oview_size);
1851
1852 if (parameters->target().is_big_endian())
1853 this->endian_do_write<true>(oview);
1854 else
1855 this->endian_do_write<false>(oview);
1856
1857 of->write_output_view(offset, oview_size, oview);
1858
1859 // We no longer need the data.
1860 this->entries_.clear();
1861 }
1862
1863 template<bool big_endian>
1864 void
1865 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1866 {
1867 for (Xindex_entries::const_iterator p = this->entries_.begin();
1868 p != this->entries_.end();
1869 ++p)
1870 {
1871 unsigned int symndx = p->first;
1872 gold_assert(symndx * 4 < this->data_size());
1873 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1874 }
1875 }
1876
1877 // Output_fill_debug_info methods.
1878
1879 // Return the minimum size needed for a dummy compilation unit header.
1880
1881 size_t
1882 Output_fill_debug_info::do_minimum_hole_size() const
1883 {
1884 // Compile unit header fields: unit_length, version, debug_abbrev_offset,
1885 // address_size.
1886 const size_t len = 4 + 2 + 4 + 1;
1887 // For type units, add type_signature, type_offset.
1888 if (this->is_debug_types_)
1889 return len + 8 + 4;
1890 return len;
1891 }
1892
1893 // Write a dummy compilation unit header to fill a hole in the
1894 // .debug_info or .debug_types section.
1895
1896 void
1897 Output_fill_debug_info::do_write(Output_file* of, off_t off, size_t len) const
1898 {
1899 gold_debug(DEBUG_INCREMENTAL, "fill_debug_info(%08lx, %08lx)",
1900 static_cast<long>(off), static_cast<long>(len));
1901
1902 gold_assert(len >= this->do_minimum_hole_size());
1903
1904 unsigned char* const oview = of->get_output_view(off, len);
1905 unsigned char* pov = oview;
1906
1907 // Write header fields: unit_length, version, debug_abbrev_offset,
1908 // address_size.
1909 if (this->is_big_endian())
1910 {
1911 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
1912 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
1913 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, 0);
1914 }
1915 else
1916 {
1917 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
1918 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
1919 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, 0);
1920 }
1921 pov += 4 + 2 + 4;
1922 *pov++ = 4;
1923
1924 // For type units, the additional header fields -- type_signature,
1925 // type_offset -- can be filled with zeroes.
1926
1927 // Fill the remainder of the free space with zeroes. The first
1928 // zero should tell the consumer there are no DIEs to read in this
1929 // compilation unit.
1930 if (pov < oview + len)
1931 memset(pov, 0, oview + len - pov);
1932
1933 of->write_output_view(off, len, oview);
1934 }
1935
1936 // Output_fill_debug_line methods.
1937
1938 // Return the minimum size needed for a dummy line number program header.
1939
1940 size_t
1941 Output_fill_debug_line::do_minimum_hole_size() const
1942 {
1943 // Line number program header fields: unit_length, version, header_length,
1944 // minimum_instruction_length, default_is_stmt, line_base, line_range,
1945 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
1946 const size_t len = 4 + 2 + 4 + this->header_length;
1947 return len;
1948 }
1949
1950 // Write a dummy line number program header to fill a hole in the
1951 // .debug_line section.
1952
1953 void
1954 Output_fill_debug_line::do_write(Output_file* of, off_t off, size_t len) const
1955 {
1956 gold_debug(DEBUG_INCREMENTAL, "fill_debug_line(%08lx, %08lx)",
1957 static_cast<long>(off), static_cast<long>(len));
1958
1959 gold_assert(len >= this->do_minimum_hole_size());
1960
1961 unsigned char* const oview = of->get_output_view(off, len);
1962 unsigned char* pov = oview;
1963
1964 // Write header fields: unit_length, version, header_length,
1965 // minimum_instruction_length, default_is_stmt, line_base, line_range,
1966 // opcode_base, standard_opcode_lengths[], include_directories, filenames.
1967 // We set the header_length field to cover the entire hole, so the
1968 // line number program is empty.
1969 if (this->is_big_endian())
1970 {
1971 elfcpp::Swap_unaligned<32, true>::writeval(pov, len - 4);
1972 elfcpp::Swap_unaligned<16, true>::writeval(pov + 4, this->version);
1973 elfcpp::Swap_unaligned<32, true>::writeval(pov + 6, len - (4 + 2 + 4));
1974 }
1975 else
1976 {
1977 elfcpp::Swap_unaligned<32, false>::writeval(pov, len - 4);
1978 elfcpp::Swap_unaligned<16, false>::writeval(pov + 4, this->version);
1979 elfcpp::Swap_unaligned<32, false>::writeval(pov + 6, len - (4 + 2 + 4));
1980 }
1981 pov += 4 + 2 + 4;
1982 *pov++ = 1; // minimum_instruction_length
1983 *pov++ = 0; // default_is_stmt
1984 *pov++ = 0; // line_base
1985 *pov++ = 5; // line_range
1986 *pov++ = 13; // opcode_base
1987 *pov++ = 0; // standard_opcode_lengths[1]
1988 *pov++ = 1; // standard_opcode_lengths[2]
1989 *pov++ = 1; // standard_opcode_lengths[3]
1990 *pov++ = 1; // standard_opcode_lengths[4]
1991 *pov++ = 1; // standard_opcode_lengths[5]
1992 *pov++ = 0; // standard_opcode_lengths[6]
1993 *pov++ = 0; // standard_opcode_lengths[7]
1994 *pov++ = 0; // standard_opcode_lengths[8]
1995 *pov++ = 1; // standard_opcode_lengths[9]
1996 *pov++ = 0; // standard_opcode_lengths[10]
1997 *pov++ = 0; // standard_opcode_lengths[11]
1998 *pov++ = 1; // standard_opcode_lengths[12]
1999 *pov++ = 0; // include_directories (empty)
2000 *pov++ = 0; // filenames (empty)
2001
2002 // Some consumers don't check the header_length field, and simply
2003 // start reading the line number program immediately following the
2004 // header. For those consumers, we fill the remainder of the free
2005 // space with DW_LNS_set_basic_block opcodes. These are effectively
2006 // no-ops: the resulting line table program will not create any rows.
2007 if (pov < oview + len)
2008 memset(pov, elfcpp::DW_LNS_set_basic_block, oview + len - pov);
2009
2010 of->write_output_view(off, len, oview);
2011 }
2012
2013 // Output_section::Input_section methods.
2014
2015 // Return the current data size. For an input section we store the size here.
2016 // For an Output_section_data, we have to ask it for the size.
2017
2018 off_t
2019 Output_section::Input_section::current_data_size() const
2020 {
2021 if (this->is_input_section())
2022 return this->u1_.data_size;
2023 else
2024 {
2025 this->u2_.posd->pre_finalize_data_size();
2026 return this->u2_.posd->current_data_size();
2027 }
2028 }
2029
2030 // Return the data size. For an input section we store the size here.
2031 // For an Output_section_data, we have to ask it for the size.
2032
2033 off_t
2034 Output_section::Input_section::data_size() const
2035 {
2036 if (this->is_input_section())
2037 return this->u1_.data_size;
2038 else
2039 return this->u2_.posd->data_size();
2040 }
2041
2042 // Return the object for an input section.
2043
2044 Relobj*
2045 Output_section::Input_section::relobj() const
2046 {
2047 if (this->is_input_section())
2048 return this->u2_.object;
2049 else if (this->is_merge_section())
2050 {
2051 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2052 return this->u2_.pomb->first_relobj();
2053 }
2054 else if (this->is_relaxed_input_section())
2055 return this->u2_.poris->relobj();
2056 else
2057 gold_unreachable();
2058 }
2059
2060 // Return the input section index for an input section.
2061
2062 unsigned int
2063 Output_section::Input_section::shndx() const
2064 {
2065 if (this->is_input_section())
2066 return this->shndx_;
2067 else if (this->is_merge_section())
2068 {
2069 gold_assert(this->u2_.pomb->first_relobj() != NULL);
2070 return this->u2_.pomb->first_shndx();
2071 }
2072 else if (this->is_relaxed_input_section())
2073 return this->u2_.poris->shndx();
2074 else
2075 gold_unreachable();
2076 }
2077
2078 // Set the address and file offset.
2079
2080 void
2081 Output_section::Input_section::set_address_and_file_offset(
2082 uint64_t address,
2083 off_t file_offset,
2084 off_t section_file_offset)
2085 {
2086 if (this->is_input_section())
2087 this->u2_.object->set_section_offset(this->shndx_,
2088 file_offset - section_file_offset);
2089 else
2090 this->u2_.posd->set_address_and_file_offset(address, file_offset);
2091 }
2092
2093 // Reset the address and file offset.
2094
2095 void
2096 Output_section::Input_section::reset_address_and_file_offset()
2097 {
2098 if (!this->is_input_section())
2099 this->u2_.posd->reset_address_and_file_offset();
2100 }
2101
2102 // Finalize the data size.
2103
2104 void
2105 Output_section::Input_section::finalize_data_size()
2106 {
2107 if (!this->is_input_section())
2108 this->u2_.posd->finalize_data_size();
2109 }
2110
2111 // Try to turn an input offset into an output offset. We want to
2112 // return the output offset relative to the start of this
2113 // Input_section in the output section.
2114
2115 inline bool
2116 Output_section::Input_section::output_offset(
2117 const Relobj* object,
2118 unsigned int shndx,
2119 section_offset_type offset,
2120 section_offset_type* poutput) const
2121 {
2122 if (!this->is_input_section())
2123 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
2124 else
2125 {
2126 if (this->shndx_ != shndx || this->u2_.object != object)
2127 return false;
2128 *poutput = offset;
2129 return true;
2130 }
2131 }
2132
2133 // Return whether this is the merge section for the input section
2134 // SHNDX in OBJECT.
2135
2136 inline bool
2137 Output_section::Input_section::is_merge_section_for(const Relobj* object,
2138 unsigned int shndx) const
2139 {
2140 if (this->is_input_section())
2141 return false;
2142 return this->u2_.posd->is_merge_section_for(object, shndx);
2143 }
2144
2145 // Write out the data. We don't have to do anything for an input
2146 // section--they are handled via Object::relocate--but this is where
2147 // we write out the data for an Output_section_data.
2148
2149 void
2150 Output_section::Input_section::write(Output_file* of)
2151 {
2152 if (!this->is_input_section())
2153 this->u2_.posd->write(of);
2154 }
2155
2156 // Write the data to a buffer. As for write(), we don't have to do
2157 // anything for an input section.
2158
2159 void
2160 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
2161 {
2162 if (!this->is_input_section())
2163 this->u2_.posd->write_to_buffer(buffer);
2164 }
2165
2166 // Print to a map file.
2167
2168 void
2169 Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
2170 {
2171 switch (this->shndx_)
2172 {
2173 case OUTPUT_SECTION_CODE:
2174 case MERGE_DATA_SECTION_CODE:
2175 case MERGE_STRING_SECTION_CODE:
2176 this->u2_.posd->print_to_mapfile(mapfile);
2177 break;
2178
2179 case RELAXED_INPUT_SECTION_CODE:
2180 {
2181 Output_relaxed_input_section* relaxed_section =
2182 this->relaxed_input_section();
2183 mapfile->print_input_section(relaxed_section->relobj(),
2184 relaxed_section->shndx());
2185 }
2186 break;
2187 default:
2188 mapfile->print_input_section(this->u2_.object, this->shndx_);
2189 break;
2190 }
2191 }
2192
2193 // Output_section methods.
2194
2195 // Construct an Output_section. NAME will point into a Stringpool.
2196
2197 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
2198 elfcpp::Elf_Xword flags)
2199 : name_(name),
2200 addralign_(0),
2201 entsize_(0),
2202 load_address_(0),
2203 link_section_(NULL),
2204 link_(0),
2205 info_section_(NULL),
2206 info_symndx_(NULL),
2207 info_(0),
2208 type_(type),
2209 flags_(flags),
2210 order_(ORDER_INVALID),
2211 out_shndx_(-1U),
2212 symtab_index_(0),
2213 dynsym_index_(0),
2214 input_sections_(),
2215 first_input_offset_(0),
2216 fills_(),
2217 postprocessing_buffer_(NULL),
2218 needs_symtab_index_(false),
2219 needs_dynsym_index_(false),
2220 should_link_to_symtab_(false),
2221 should_link_to_dynsym_(false),
2222 after_input_sections_(false),
2223 requires_postprocessing_(false),
2224 found_in_sections_clause_(false),
2225 has_load_address_(false),
2226 info_uses_section_index_(false),
2227 input_section_order_specified_(false),
2228 may_sort_attached_input_sections_(false),
2229 must_sort_attached_input_sections_(false),
2230 attached_input_sections_are_sorted_(false),
2231 is_relro_(false),
2232 is_small_section_(false),
2233 is_large_section_(false),
2234 generate_code_fills_at_write_(false),
2235 is_entsize_zero_(false),
2236 section_offsets_need_adjustment_(false),
2237 is_noload_(false),
2238 always_keeps_input_sections_(false),
2239 has_fixed_layout_(false),
2240 is_patch_space_allowed_(false),
2241 tls_offset_(0),
2242 checkpoint_(NULL),
2243 lookup_maps_(new Output_section_lookup_maps),
2244 free_list_(),
2245 free_space_fill_(NULL),
2246 patch_space_(0)
2247 {
2248 // An unallocated section has no address. Forcing this means that
2249 // we don't need special treatment for symbols defined in debug
2250 // sections.
2251 if ((flags & elfcpp::SHF_ALLOC) == 0)
2252 this->set_address(0);
2253 }
2254
2255 Output_section::~Output_section()
2256 {
2257 delete this->checkpoint_;
2258 }
2259
2260 // Set the entry size.
2261
2262 void
2263 Output_section::set_entsize(uint64_t v)
2264 {
2265 if (this->is_entsize_zero_)
2266 ;
2267 else if (this->entsize_ == 0)
2268 this->entsize_ = v;
2269 else if (this->entsize_ != v)
2270 {
2271 this->entsize_ = 0;
2272 this->is_entsize_zero_ = 1;
2273 }
2274 }
2275
2276 // Add the input section SHNDX, with header SHDR, named SECNAME, in
2277 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
2278 // relocation section which applies to this section, or 0 if none, or
2279 // -1U if more than one. Return the offset of the input section
2280 // within the output section. Return -1 if the input section will
2281 // receive special handling. In the normal case we don't always keep
2282 // track of input sections for an Output_section. Instead, each
2283 // Object keeps track of the Output_section for each of its input
2284 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
2285 // track of input sections here; this is used when SECTIONS appears in
2286 // a linker script.
2287
2288 template<int size, bool big_endian>
2289 off_t
2290 Output_section::add_input_section(Layout* layout,
2291 Sized_relobj_file<size, big_endian>* object,
2292 unsigned int shndx,
2293 const char* secname,
2294 const elfcpp::Shdr<size, big_endian>& shdr,
2295 unsigned int reloc_shndx,
2296 bool have_sections_script)
2297 {
2298 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
2299 if ((addralign & (addralign - 1)) != 0)
2300 {
2301 object->error(_("invalid alignment %lu for section \"%s\""),
2302 static_cast<unsigned long>(addralign), secname);
2303 addralign = 1;
2304 }
2305
2306 if (addralign > this->addralign_)
2307 this->addralign_ = addralign;
2308
2309 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
2310 uint64_t entsize = shdr.get_sh_entsize();
2311
2312 // .debug_str is a mergeable string section, but is not always so
2313 // marked by compilers. Mark manually here so we can optimize.
2314 if (strcmp(secname, ".debug_str") == 0)
2315 {
2316 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
2317 entsize = 1;
2318 }
2319
2320 this->update_flags_for_input_section(sh_flags);
2321 this->set_entsize(entsize);
2322
2323 // If this is a SHF_MERGE section, we pass all the input sections to
2324 // a Output_data_merge. We don't try to handle relocations for such
2325 // a section. We don't try to handle empty merge sections--they
2326 // mess up the mappings, and are useless anyhow.
2327 // FIXME: Need to handle merge sections during incremental update.
2328 if ((sh_flags & elfcpp::SHF_MERGE) != 0
2329 && reloc_shndx == 0
2330 && shdr.get_sh_size() > 0
2331 && !parameters->incremental())
2332 {
2333 // Keep information about merged input sections for rebuilding fast
2334 // lookup maps if we have sections-script or we do relaxation.
2335 bool keeps_input_sections = (this->always_keeps_input_sections_
2336 || have_sections_script
2337 || parameters->target().may_relax());
2338
2339 if (this->add_merge_input_section(object, shndx, sh_flags, entsize,
2340 addralign, keeps_input_sections))
2341 {
2342 // Tell the relocation routines that they need to call the
2343 // output_offset method to determine the final address.
2344 return -1;
2345 }
2346 }
2347
2348 section_size_type input_section_size = shdr.get_sh_size();
2349 section_size_type uncompressed_size;
2350 if (object->section_is_compressed(shndx, &uncompressed_size))
2351 input_section_size = uncompressed_size;
2352
2353 off_t offset_in_section;
2354 off_t aligned_offset_in_section;
2355 if (this->has_fixed_layout())
2356 {
2357 // For incremental updates, find a chunk of unused space in the section.
2358 offset_in_section = this->free_list_.allocate(input_section_size,
2359 addralign, 0);
2360 if (offset_in_section == -1)
2361 gold_fallback(_("out of patch space in section %s; "
2362 "relink with --incremental-full"),
2363 this->name());
2364 aligned_offset_in_section = offset_in_section;
2365 }
2366 else
2367 {
2368 offset_in_section = this->current_data_size_for_child();
2369 aligned_offset_in_section = align_address(offset_in_section,
2370 addralign);
2371 this->set_current_data_size_for_child(aligned_offset_in_section
2372 + input_section_size);
2373 }
2374
2375 // Determine if we want to delay code-fill generation until the output
2376 // section is written. When the target is relaxing, we want to delay fill
2377 // generating to avoid adjusting them during relaxation. Also, if we are
2378 // sorting input sections we must delay fill generation.
2379 if (!this->generate_code_fills_at_write_
2380 && !have_sections_script
2381 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2382 && parameters->target().has_code_fill()
2383 && (parameters->target().may_relax()
2384 || layout->is_section_ordering_specified()))
2385 {
2386 gold_assert(this->fills_.empty());
2387 this->generate_code_fills_at_write_ = true;
2388 }
2389
2390 if (aligned_offset_in_section > offset_in_section
2391 && !this->generate_code_fills_at_write_
2392 && !have_sections_script
2393 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
2394 && parameters->target().has_code_fill())
2395 {
2396 // We need to add some fill data. Using fill_list_ when
2397 // possible is an optimization, since we will often have fill
2398 // sections without input sections.
2399 off_t fill_len = aligned_offset_in_section - offset_in_section;
2400 if (this->input_sections_.empty())
2401 this->fills_.push_back(Fill(offset_in_section, fill_len));
2402 else
2403 {
2404 std::string fill_data(parameters->target().code_fill(fill_len));
2405 Output_data_const* odc = new Output_data_const(fill_data, 1);
2406 this->input_sections_.push_back(Input_section(odc));
2407 }
2408 }
2409
2410 // We need to keep track of this section if we are already keeping
2411 // track of sections, or if we are relaxing. Also, if this is a
2412 // section which requires sorting, or which may require sorting in
2413 // the future, we keep track of the sections. If the
2414 // --section-ordering-file option is used to specify the order of
2415 // sections, we need to keep track of sections.
2416 if (this->always_keeps_input_sections_
2417 || have_sections_script
2418 || !this->input_sections_.empty()
2419 || this->may_sort_attached_input_sections()
2420 || this->must_sort_attached_input_sections()
2421 || parameters->options().user_set_Map()
2422 || parameters->target().may_relax()
2423 || layout->is_section_ordering_specified())
2424 {
2425 Input_section isecn(object, shndx, input_section_size, addralign);
2426 /* If section ordering is requested by specifying a ordering file,
2427 using --section-ordering-file, match the section name with
2428 a pattern. */
2429 if (parameters->options().section_ordering_file())
2430 {
2431 unsigned int section_order_index =
2432 layout->find_section_order_index(std::string(secname));
2433 if (section_order_index != 0)
2434 {
2435 isecn.set_section_order_index(section_order_index);
2436 this->set_input_section_order_specified();
2437 }
2438 }
2439 if (this->has_fixed_layout())
2440 {
2441 // For incremental updates, finalize the address and offset now.
2442 uint64_t addr = this->address();
2443 isecn.set_address_and_file_offset(addr + aligned_offset_in_section,
2444 aligned_offset_in_section,
2445 this->offset());
2446 }
2447 this->input_sections_.push_back(isecn);
2448 }
2449
2450 return aligned_offset_in_section;
2451 }
2452
2453 // Add arbitrary data to an output section.
2454
2455 void
2456 Output_section::add_output_section_data(Output_section_data* posd)
2457 {
2458 Input_section inp(posd);
2459 this->add_output_section_data(&inp);
2460
2461 if (posd->is_data_size_valid())
2462 {
2463 off_t offset_in_section;
2464 if (this->has_fixed_layout())
2465 {
2466 // For incremental updates, find a chunk of unused space.
2467 offset_in_section = this->free_list_.allocate(posd->data_size(),
2468 posd->addralign(), 0);
2469 if (offset_in_section == -1)
2470 gold_fallback(_("out of patch space in section %s; "
2471 "relink with --incremental-full"),
2472 this->name());
2473 // Finalize the address and offset now.
2474 uint64_t addr = this->address();
2475 off_t offset = this->offset();
2476 posd->set_address_and_file_offset(addr + offset_in_section,
2477 offset + offset_in_section);
2478 }
2479 else
2480 {
2481 offset_in_section = this->current_data_size_for_child();
2482 off_t aligned_offset_in_section = align_address(offset_in_section,
2483 posd->addralign());
2484 this->set_current_data_size_for_child(aligned_offset_in_section
2485 + posd->data_size());
2486 }
2487 }
2488 else if (this->has_fixed_layout())
2489 {
2490 // For incremental updates, arrange for the data to have a fixed layout.
2491 // This will mean that additions to the data must be allocated from
2492 // free space within the containing output section.
2493 uint64_t addr = this->address();
2494 posd->set_address(addr);
2495 posd->set_file_offset(0);
2496 // FIXME: This should eventually be unreachable.
2497 // gold_unreachable();
2498 }
2499 }
2500
2501 // Add a relaxed input section.
2502
2503 void
2504 Output_section::add_relaxed_input_section(Layout* layout,
2505 Output_relaxed_input_section* poris,
2506 const std::string& name)
2507 {
2508 Input_section inp(poris);
2509
2510 // If the --section-ordering-file option is used to specify the order of
2511 // sections, we need to keep track of sections.
2512 if (layout->is_section_ordering_specified())
2513 {
2514 unsigned int section_order_index =
2515 layout->find_section_order_index(name);
2516 if (section_order_index != 0)
2517 {
2518 inp.set_section_order_index(section_order_index);
2519 this->set_input_section_order_specified();
2520 }
2521 }
2522
2523 this->add_output_section_data(&inp);
2524 if (this->lookup_maps_->is_valid())
2525 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2526 poris->shndx(), poris);
2527
2528 // For a relaxed section, we use the current data size. Linker scripts
2529 // get all the input sections, including relaxed one from an output
2530 // section and add them back to them same output section to compute the
2531 // output section size. If we do not account for sizes of relaxed input
2532 // sections, an output section would be incorrectly sized.
2533 off_t offset_in_section = this->current_data_size_for_child();
2534 off_t aligned_offset_in_section = align_address(offset_in_section,
2535 poris->addralign());
2536 this->set_current_data_size_for_child(aligned_offset_in_section
2537 + poris->current_data_size());
2538 }
2539
2540 // Add arbitrary data to an output section by Input_section.
2541
2542 void
2543 Output_section::add_output_section_data(Input_section* inp)
2544 {
2545 if (this->input_sections_.empty())
2546 this->first_input_offset_ = this->current_data_size_for_child();
2547
2548 this->input_sections_.push_back(*inp);
2549
2550 uint64_t addralign = inp->addralign();
2551 if (addralign > this->addralign_)
2552 this->addralign_ = addralign;
2553
2554 inp->set_output_section(this);
2555 }
2556
2557 // Add a merge section to an output section.
2558
2559 void
2560 Output_section::add_output_merge_section(Output_section_data* posd,
2561 bool is_string, uint64_t entsize)
2562 {
2563 Input_section inp(posd, is_string, entsize);
2564 this->add_output_section_data(&inp);
2565 }
2566
2567 // Add an input section to a SHF_MERGE section.
2568
2569 bool
2570 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2571 uint64_t flags, uint64_t entsize,
2572 uint64_t addralign,
2573 bool keeps_input_sections)
2574 {
2575 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2576
2577 // We only merge strings if the alignment is not more than the
2578 // character size. This could be handled, but it's unusual.
2579 if (is_string && addralign > entsize)
2580 return false;
2581
2582 // We cannot restore merged input section states.
2583 gold_assert(this->checkpoint_ == NULL);
2584
2585 // Look up merge sections by required properties.
2586 // Currently, we only invalidate the lookup maps in script processing
2587 // and relaxation. We should not have done either when we reach here.
2588 // So we assume that the lookup maps are valid to simply code.
2589 gold_assert(this->lookup_maps_->is_valid());
2590 Merge_section_properties msp(is_string, entsize, addralign);
2591 Output_merge_base* pomb = this->lookup_maps_->find_merge_section(msp);
2592 bool is_new = false;
2593 if (pomb != NULL)
2594 {
2595 gold_assert(pomb->is_string() == is_string
2596 && pomb->entsize() == entsize
2597 && pomb->addralign() == addralign);
2598 }
2599 else
2600 {
2601 // Create a new Output_merge_data or Output_merge_string_data.
2602 if (!is_string)
2603 pomb = new Output_merge_data(entsize, addralign);
2604 else
2605 {
2606 switch (entsize)
2607 {
2608 case 1:
2609 pomb = new Output_merge_string<char>(addralign);
2610 break;
2611 case 2:
2612 pomb = new Output_merge_string<uint16_t>(addralign);
2613 break;
2614 case 4:
2615 pomb = new Output_merge_string<uint32_t>(addralign);
2616 break;
2617 default:
2618 return false;
2619 }
2620 }
2621 // If we need to do script processing or relaxation, we need to keep
2622 // the original input sections to rebuild the fast lookup maps.
2623 if (keeps_input_sections)
2624 pomb->set_keeps_input_sections();
2625 is_new = true;
2626 }
2627
2628 if (pomb->add_input_section(object, shndx))
2629 {
2630 // Add new merge section to this output section and link merge
2631 // section properties to new merge section in map.
2632 if (is_new)
2633 {
2634 this->add_output_merge_section(pomb, is_string, entsize);
2635 this->lookup_maps_->add_merge_section(msp, pomb);
2636 }
2637
2638 // Add input section to new merge section and link input section to new
2639 // merge section in map.
2640 this->lookup_maps_->add_merge_input_section(object, shndx, pomb);
2641 return true;
2642 }
2643 else
2644 {
2645 // If add_input_section failed, delete new merge section to avoid
2646 // exporting empty merge sections in Output_section::get_input_section.
2647 if (is_new)
2648 delete pomb;
2649 return false;
2650 }
2651 }
2652
2653 // Build a relaxation map to speed up relaxation of existing input sections.
2654 // Look up to the first LIMIT elements in INPUT_SECTIONS.
2655
2656 void
2657 Output_section::build_relaxation_map(
2658 const Input_section_list& input_sections,
2659 size_t limit,
2660 Relaxation_map* relaxation_map) const
2661 {
2662 for (size_t i = 0; i < limit; ++i)
2663 {
2664 const Input_section& is(input_sections[i]);
2665 if (is.is_input_section() || is.is_relaxed_input_section())
2666 {
2667 Section_id sid(is.relobj(), is.shndx());
2668 (*relaxation_map)[sid] = i;
2669 }
2670 }
2671 }
2672
2673 // Convert regular input sections in INPUT_SECTIONS into relaxed input
2674 // sections in RELAXED_SECTIONS. MAP is a prebuilt map from section id
2675 // indices of INPUT_SECTIONS.
2676
2677 void
2678 Output_section::convert_input_sections_in_list_to_relaxed_sections(
2679 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2680 const Relaxation_map& map,
2681 Input_section_list* input_sections)
2682 {
2683 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2684 {
2685 Output_relaxed_input_section* poris = relaxed_sections[i];
2686 Section_id sid(poris->relobj(), poris->shndx());
2687 Relaxation_map::const_iterator p = map.find(sid);
2688 gold_assert(p != map.end());
2689 gold_assert((*input_sections)[p->second].is_input_section());
2690
2691 // Remember section order index of original input section
2692 // if it is set. Copy it to the relaxed input section.
2693 unsigned int soi =
2694 (*input_sections)[p->second].section_order_index();
2695 (*input_sections)[p->second] = Input_section(poris);
2696 (*input_sections)[p->second].set_section_order_index(soi);
2697 }
2698 }
2699
2700 // Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2701 // is a vector of pointers to Output_relaxed_input_section or its derived
2702 // classes. The relaxed sections must correspond to existing input sections.
2703
2704 void
2705 Output_section::convert_input_sections_to_relaxed_sections(
2706 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2707 {
2708 gold_assert(parameters->target().may_relax());
2709
2710 // We want to make sure that restore_states does not undo the effect of
2711 // this. If there is no checkpoint active, just search the current
2712 // input section list and replace the sections there. If there is
2713 // a checkpoint, also replace the sections there.
2714
2715 // By default, we look at the whole list.
2716 size_t limit = this->input_sections_.size();
2717
2718 if (this->checkpoint_ != NULL)
2719 {
2720 // Replace input sections with relaxed input section in the saved
2721 // copy of the input section list.
2722 if (this->checkpoint_->input_sections_saved())
2723 {
2724 Relaxation_map map;
2725 this->build_relaxation_map(
2726 *(this->checkpoint_->input_sections()),
2727 this->checkpoint_->input_sections()->size(),
2728 &map);
2729 this->convert_input_sections_in_list_to_relaxed_sections(
2730 relaxed_sections,
2731 map,
2732 this->checkpoint_->input_sections());
2733 }
2734 else
2735 {
2736 // We have not copied the input section list yet. Instead, just
2737 // look at the portion that would be saved.
2738 limit = this->checkpoint_->input_sections_size();
2739 }
2740 }
2741
2742 // Convert input sections in input_section_list.
2743 Relaxation_map map;
2744 this->build_relaxation_map(this->input_sections_, limit, &map);
2745 this->convert_input_sections_in_list_to_relaxed_sections(
2746 relaxed_sections,
2747 map,
2748 &this->input_sections_);
2749
2750 // Update fast look-up map.
2751 if (this->lookup_maps_->is_valid())
2752 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2753 {
2754 Output_relaxed_input_section* poris = relaxed_sections[i];
2755 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2756 poris->shndx(), poris);
2757 }
2758 }
2759
2760 // Update the output section flags based on input section flags.
2761
2762 void
2763 Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2764 {
2765 // If we created the section with SHF_ALLOC clear, we set the
2766 // address. If we are now setting the SHF_ALLOC flag, we need to
2767 // undo that.
2768 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2769 && (flags & elfcpp::SHF_ALLOC) != 0)
2770 this->mark_address_invalid();
2771
2772 this->flags_ |= (flags
2773 & (elfcpp::SHF_WRITE
2774 | elfcpp::SHF_ALLOC
2775 | elfcpp::SHF_EXECINSTR));
2776
2777 if ((flags & elfcpp::SHF_MERGE) == 0)
2778 this->flags_ &=~ elfcpp::SHF_MERGE;
2779 else
2780 {
2781 if (this->current_data_size_for_child() == 0)
2782 this->flags_ |= elfcpp::SHF_MERGE;
2783 }
2784
2785 if ((flags & elfcpp::SHF_STRINGS) == 0)
2786 this->flags_ &=~ elfcpp::SHF_STRINGS;
2787 else
2788 {
2789 if (this->current_data_size_for_child() == 0)
2790 this->flags_ |= elfcpp::SHF_STRINGS;
2791 }
2792 }
2793
2794 // Find the merge section into which an input section with index SHNDX in
2795 // OBJECT has been added. Return NULL if none found.
2796
2797 Output_section_data*
2798 Output_section::find_merge_section(const Relobj* object,
2799 unsigned int shndx) const
2800 {
2801 if (!this->lookup_maps_->is_valid())
2802 this->build_lookup_maps();
2803 return this->lookup_maps_->find_merge_section(object, shndx);
2804 }
2805
2806 // Build the lookup maps for merge and relaxed sections. This is needs
2807 // to be declared as a const methods so that it is callable with a const
2808 // Output_section pointer. The method only updates states of the maps.
2809
2810 void
2811 Output_section::build_lookup_maps() const
2812 {
2813 this->lookup_maps_->clear();
2814 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2815 p != this->input_sections_.end();
2816 ++p)
2817 {
2818 if (p->is_merge_section())
2819 {
2820 Output_merge_base* pomb = p->output_merge_base();
2821 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
2822 pomb->addralign());
2823 this->lookup_maps_->add_merge_section(msp, pomb);
2824 for (Output_merge_base::Input_sections::const_iterator is =
2825 pomb->input_sections_begin();
2826 is != pomb->input_sections_end();
2827 ++is)
2828 {
2829 const Const_section_id& csid = *is;
2830 this->lookup_maps_->add_merge_input_section(csid.first,
2831 csid.second, pomb);
2832 }
2833
2834 }
2835 else if (p->is_relaxed_input_section())
2836 {
2837 Output_relaxed_input_section* poris = p->relaxed_input_section();
2838 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
2839 poris->shndx(), poris);
2840 }
2841 }
2842 }
2843
2844 // Find an relaxed input section corresponding to an input section
2845 // in OBJECT with index SHNDX.
2846
2847 const Output_relaxed_input_section*
2848 Output_section::find_relaxed_input_section(const Relobj* object,
2849 unsigned int shndx) const
2850 {
2851 if (!this->lookup_maps_->is_valid())
2852 this->build_lookup_maps();
2853 return this->lookup_maps_->find_relaxed_input_section(object, shndx);
2854 }
2855
2856 // Given an address OFFSET relative to the start of input section
2857 // SHNDX in OBJECT, return whether this address is being included in
2858 // the final link. This should only be called if SHNDX in OBJECT has
2859 // a special mapping.
2860
2861 bool
2862 Output_section::is_input_address_mapped(const Relobj* object,
2863 unsigned int shndx,
2864 off_t offset) const
2865 {
2866 // Look at the Output_section_data_maps first.
2867 const Output_section_data* posd = this->find_merge_section(object, shndx);
2868 if (posd == NULL)
2869 posd = this->find_relaxed_input_section(object, shndx);
2870
2871 if (posd != NULL)
2872 {
2873 section_offset_type output_offset;
2874 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2875 gold_assert(found);
2876 return output_offset != -1;
2877 }
2878
2879 // Fall back to the slow look-up.
2880 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2881 p != this->input_sections_.end();
2882 ++p)
2883 {
2884 section_offset_type output_offset;
2885 if (p->output_offset(object, shndx, offset, &output_offset))
2886 return output_offset != -1;
2887 }
2888
2889 // By default we assume that the address is mapped. This should
2890 // only be called after we have passed all sections to Layout. At
2891 // that point we should know what we are discarding.
2892 return true;
2893 }
2894
2895 // Given an address OFFSET relative to the start of input section
2896 // SHNDX in object OBJECT, return the output offset relative to the
2897 // start of the input section in the output section. This should only
2898 // be called if SHNDX in OBJECT has a special mapping.
2899
2900 section_offset_type
2901 Output_section::output_offset(const Relobj* object, unsigned int shndx,
2902 section_offset_type offset) const
2903 {
2904 // This can only be called meaningfully when we know the data size
2905 // of this.
2906 gold_assert(this->is_data_size_valid());
2907
2908 // Look at the Output_section_data_maps first.
2909 const Output_section_data* posd = this->find_merge_section(object, shndx);
2910 if (posd == NULL)
2911 posd = this->find_relaxed_input_section(object, shndx);
2912 if (posd != NULL)
2913 {
2914 section_offset_type output_offset;
2915 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2916 gold_assert(found);
2917 return output_offset;
2918 }
2919
2920 // Fall back to the slow look-up.
2921 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2922 p != this->input_sections_.end();
2923 ++p)
2924 {
2925 section_offset_type output_offset;
2926 if (p->output_offset(object, shndx, offset, &output_offset))
2927 return output_offset;
2928 }
2929 gold_unreachable();
2930 }
2931
2932 // Return the output virtual address of OFFSET relative to the start
2933 // of input section SHNDX in object OBJECT.
2934
2935 uint64_t
2936 Output_section::output_address(const Relobj* object, unsigned int shndx,
2937 off_t offset) const
2938 {
2939 uint64_t addr = this->address() + this->first_input_offset_;
2940
2941 // Look at the Output_section_data_maps first.
2942 const Output_section_data* posd = this->find_merge_section(object, shndx);
2943 if (posd == NULL)
2944 posd = this->find_relaxed_input_section(object, shndx);
2945 if (posd != NULL && posd->is_address_valid())
2946 {
2947 section_offset_type output_offset;
2948 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2949 gold_assert(found);
2950 return posd->address() + output_offset;
2951 }
2952
2953 // Fall back to the slow look-up.
2954 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2955 p != this->input_sections_.end();
2956 ++p)
2957 {
2958 addr = align_address(addr, p->addralign());
2959 section_offset_type output_offset;
2960 if (p->output_offset(object, shndx, offset, &output_offset))
2961 {
2962 if (output_offset == -1)
2963 return -1ULL;
2964 return addr + output_offset;
2965 }
2966 addr += p->data_size();
2967 }
2968
2969 // If we get here, it means that we don't know the mapping for this
2970 // input section. This might happen in principle if
2971 // add_input_section were called before add_output_section_data.
2972 // But it should never actually happen.
2973
2974 gold_unreachable();
2975 }
2976
2977 // Find the output address of the start of the merged section for
2978 // input section SHNDX in object OBJECT.
2979
2980 bool
2981 Output_section::find_starting_output_address(const Relobj* object,
2982 unsigned int shndx,
2983 uint64_t* paddr) const
2984 {
2985 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2986 // Looking up the merge section map does not always work as we sometimes
2987 // find a merge section without its address set.
2988 uint64_t addr = this->address() + this->first_input_offset_;
2989 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2990 p != this->input_sections_.end();
2991 ++p)
2992 {
2993 addr = align_address(addr, p->addralign());
2994
2995 // It would be nice if we could use the existing output_offset
2996 // method to get the output offset of input offset 0.
2997 // Unfortunately we don't know for sure that input offset 0 is
2998 // mapped at all.
2999 if (p->is_merge_section_for(object, shndx))
3000 {
3001 *paddr = addr;
3002 return true;
3003 }
3004
3005 addr += p->data_size();
3006 }
3007
3008 // We couldn't find a merge output section for this input section.
3009 return false;
3010 }
3011
3012 // Update the data size of an Output_section.
3013
3014 void
3015 Output_section::update_data_size()
3016 {
3017 if (this->input_sections_.empty())
3018 return;
3019
3020 if (this->must_sort_attached_input_sections()
3021 || this->input_section_order_specified())
3022 this->sort_attached_input_sections();
3023
3024 off_t off = this->first_input_offset_;
3025 for (Input_section_list::iterator p = this->input_sections_.begin();
3026 p != this->input_sections_.end();
3027 ++p)
3028 {
3029 off = align_address(off, p->addralign());
3030 off += p->current_data_size();
3031 }
3032
3033 this->set_current_data_size_for_child(off);
3034 }
3035
3036 // Set the data size of an Output_section. This is where we handle
3037 // setting the addresses of any Output_section_data objects.
3038
3039 void
3040 Output_section::set_final_data_size()
3041 {
3042 off_t data_size;
3043
3044 if (this->input_sections_.empty())
3045 data_size = this->current_data_size_for_child();
3046 else
3047 {
3048 if (this->must_sort_attached_input_sections()
3049 || this->input_section_order_specified())
3050 this->sort_attached_input_sections();
3051
3052 uint64_t address = this->address();
3053 off_t startoff = this->offset();
3054 off_t off = startoff + this->first_input_offset_;
3055 for (Input_section_list::iterator p = this->input_sections_.begin();
3056 p != this->input_sections_.end();
3057 ++p)
3058 {
3059 off = align_address(off, p->addralign());
3060 p->set_address_and_file_offset(address + (off - startoff), off,
3061 startoff);
3062 off += p->data_size();
3063 }
3064 data_size = off - startoff;
3065 }
3066
3067 // For full incremental links, we want to allocate some patch space
3068 // in most sections for subsequent incremental updates.
3069 if (this->is_patch_space_allowed_ && parameters->incremental_full())
3070 {
3071 double pct = parameters->options().incremental_patch();
3072 size_t extra = static_cast<size_t>(data_size * pct);
3073 if (this->free_space_fill_ != NULL
3074 && this->free_space_fill_->minimum_hole_size() > extra)
3075 extra = this->free_space_fill_->minimum_hole_size();
3076 off_t new_size = align_address(data_size + extra, this->addralign());
3077 this->patch_space_ = new_size - data_size;
3078 gold_debug(DEBUG_INCREMENTAL,
3079 "set_final_data_size: %08lx + %08lx: section %s",
3080 static_cast<long>(data_size),
3081 static_cast<long>(this->patch_space_),
3082 this->name());
3083 data_size = new_size;
3084 }
3085
3086 this->set_data_size(data_size);
3087 }
3088
3089 // Reset the address and file offset.
3090
3091 void
3092 Output_section::do_reset_address_and_file_offset()
3093 {
3094 // An unallocated section has no address. Forcing this means that
3095 // we don't need special treatment for symbols defined in debug
3096 // sections. We do the same in the constructor. This does not
3097 // apply to NOLOAD sections though.
3098 if (((this->flags_ & elfcpp::SHF_ALLOC) == 0) && !this->is_noload_)
3099 this->set_address(0);
3100
3101 for (Input_section_list::iterator p = this->input_sections_.begin();
3102 p != this->input_sections_.end();
3103 ++p)
3104 p->reset_address_and_file_offset();
3105
3106 // Remove any patch space that was added in set_final_data_size.
3107 if (this->patch_space_ > 0)
3108 {
3109 this->set_current_data_size_for_child(this->current_data_size_for_child()
3110 - this->patch_space_);
3111 this->patch_space_ = 0;
3112 }
3113 }
3114
3115 // Return true if address and file offset have the values after reset.
3116
3117 bool
3118 Output_section::do_address_and_file_offset_have_reset_values() const
3119 {
3120 if (this->is_offset_valid())
3121 return false;
3122
3123 // An unallocated section has address 0 after its construction or a reset.
3124 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
3125 return this->is_address_valid() && this->address() == 0;
3126 else
3127 return !this->is_address_valid();
3128 }
3129
3130 // Set the TLS offset. Called only for SHT_TLS sections.
3131
3132 void
3133 Output_section::do_set_tls_offset(uint64_t tls_base)
3134 {
3135 this->tls_offset_ = this->address() - tls_base;
3136 }
3137
3138 // In a few cases we need to sort the input sections attached to an
3139 // output section. This is used to implement the type of constructor
3140 // priority ordering implemented by the GNU linker, in which the
3141 // priority becomes part of the section name and the sections are
3142 // sorted by name. We only do this for an output section if we see an
3143 // attached input section matching ".ctors.*", ".dtors.*",
3144 // ".init_array.*" or ".fini_array.*".
3145
3146 class Output_section::Input_section_sort_entry
3147 {
3148 public:
3149 Input_section_sort_entry()
3150 : input_section_(), index_(-1U), section_has_name_(false),
3151 section_name_()
3152 { }
3153
3154 Input_section_sort_entry(const Input_section& input_section,
3155 unsigned int index,
3156 bool must_sort_attached_input_sections)
3157 : input_section_(input_section), index_(index),
3158 section_has_name_(input_section.is_input_section()
3159 || input_section.is_relaxed_input_section())
3160 {
3161 if (this->section_has_name_
3162 && must_sort_attached_input_sections)
3163 {
3164 // This is only called single-threaded from Layout::finalize,
3165 // so it is OK to lock. Unfortunately we have no way to pass
3166 // in a Task token.
3167 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
3168 Object* obj = (input_section.is_input_section()
3169 ? input_section.relobj()
3170 : input_section.relaxed_input_section()->relobj());
3171 Task_lock_obj<Object> tl(dummy_task, obj);
3172
3173 // This is a slow operation, which should be cached in
3174 // Layout::layout if this becomes a speed problem.
3175 this->section_name_ = obj->section_name(input_section.shndx());
3176 }
3177 }
3178
3179 // Return the Input_section.
3180 const Input_section&
3181 input_section() const
3182 {
3183 gold_assert(this->index_ != -1U);
3184 return this->input_section_;
3185 }
3186
3187 // The index of this entry in the original list. This is used to
3188 // make the sort stable.
3189 unsigned int
3190 index() const
3191 {
3192 gold_assert(this->index_ != -1U);
3193 return this->index_;
3194 }
3195
3196 // Whether there is a section name.
3197 bool
3198 section_has_name() const
3199 { return this->section_has_name_; }
3200
3201 // The section name.
3202 const std::string&
3203 section_name() const
3204 {
3205 gold_assert(this->section_has_name_);
3206 return this->section_name_;
3207 }
3208
3209 // Return true if the section name has a priority. This is assumed
3210 // to be true if it has a dot after the initial dot.
3211 bool
3212 has_priority() const
3213 {
3214 gold_assert(this->section_has_name_);
3215 return this->section_name_.find('.', 1) != std::string::npos;
3216 }
3217
3218 // Return the priority. Believe it or not, gcc encodes the priority
3219 // differently for .ctors/.dtors and .init_array/.fini_array
3220 // sections.
3221 unsigned int
3222 get_priority() const
3223 {
3224 gold_assert(this->section_has_name_);
3225 bool is_ctors;
3226 if (is_prefix_of(".ctors.", this->section_name_.c_str())
3227 || is_prefix_of(".dtors.", this->section_name_.c_str()))
3228 is_ctors = true;
3229 else if (is_prefix_of(".init_array.", this->section_name_.c_str())
3230 || is_prefix_of(".fini_array.", this->section_name_.c_str()))
3231 is_ctors = false;
3232 else
3233 return 0;
3234 char* end;
3235 unsigned long prio = strtoul((this->section_name_.c_str()
3236 + (is_ctors ? 7 : 12)),
3237 &end, 10);
3238 if (*end != '\0')
3239 return 0;
3240 else if (is_ctors)
3241 return 65535 - prio;
3242 else
3243 return prio;
3244 }
3245
3246 // Return true if this an input file whose base name matches
3247 // FILE_NAME. The base name must have an extension of ".o", and
3248 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
3249 // This is to match crtbegin.o as well as crtbeginS.o without
3250 // getting confused by other possibilities. Overall matching the
3251 // file name this way is a dreadful hack, but the GNU linker does it
3252 // in order to better support gcc, and we need to be compatible.
3253 bool
3254 match_file_name(const char* file_name) const
3255 { return Layout::match_file_name(this->input_section_.relobj(), file_name); }
3256
3257 // Returns 1 if THIS should appear before S in section order, -1 if S
3258 // appears before THIS and 0 if they are not comparable.
3259 int
3260 compare_section_ordering(const Input_section_sort_entry& s) const
3261 {
3262 unsigned int this_secn_index = this->input_section_.section_order_index();
3263 unsigned int s_secn_index = s.input_section().section_order_index();
3264 if (this_secn_index > 0 && s_secn_index > 0)
3265 {
3266 if (this_secn_index < s_secn_index)
3267 return 1;
3268 else if (this_secn_index > s_secn_index)
3269 return -1;
3270 }
3271 return 0;
3272 }
3273
3274 private:
3275 // The Input_section we are sorting.
3276 Input_section input_section_;
3277 // The index of this Input_section in the original list.
3278 unsigned int index_;
3279 // Whether this Input_section has a section name--it won't if this
3280 // is some random Output_section_data.
3281 bool section_has_name_;
3282 // The section name if there is one.
3283 std::string section_name_;
3284 };
3285
3286 // Return true if S1 should come before S2 in the output section.
3287
3288 bool
3289 Output_section::Input_section_sort_compare::operator()(
3290 const Output_section::Input_section_sort_entry& s1,
3291 const Output_section::Input_section_sort_entry& s2) const
3292 {
3293 // crtbegin.o must come first.
3294 bool s1_begin = s1.match_file_name("crtbegin");
3295 bool s2_begin = s2.match_file_name("crtbegin");
3296 if (s1_begin || s2_begin)
3297 {
3298 if (!s1_begin)
3299 return false;
3300 if (!s2_begin)
3301 return true;
3302 return s1.index() < s2.index();
3303 }
3304
3305 // crtend.o must come last.
3306 bool s1_end = s1.match_file_name("crtend");
3307 bool s2_end = s2.match_file_name("crtend");
3308 if (s1_end || s2_end)
3309 {
3310 if (!s1_end)
3311 return true;
3312 if (!s2_end)
3313 return false;
3314 return s1.index() < s2.index();
3315 }
3316
3317 // We sort all the sections with no names to the end.
3318 if (!s1.section_has_name() || !s2.section_has_name())
3319 {
3320 if (s1.section_has_name())
3321 return true;
3322 if (s2.section_has_name())
3323 return false;
3324 return s1.index() < s2.index();
3325 }
3326
3327 // A section with a priority follows a section without a priority.
3328 bool s1_has_priority = s1.has_priority();
3329 bool s2_has_priority = s2.has_priority();
3330 if (s1_has_priority && !s2_has_priority)
3331 return false;
3332 if (!s1_has_priority && s2_has_priority)
3333 return true;
3334
3335 // Check if a section order exists for these sections through a section
3336 // ordering file. If sequence_num is 0, an order does not exist.
3337 int sequence_num = s1.compare_section_ordering(s2);
3338 if (sequence_num != 0)
3339 return sequence_num == 1;
3340
3341 // Otherwise we sort by name.
3342 int compare = s1.section_name().compare(s2.section_name());
3343 if (compare != 0)
3344 return compare < 0;
3345
3346 // Otherwise we keep the input order.
3347 return s1.index() < s2.index();
3348 }
3349
3350 // Return true if S1 should come before S2 in an .init_array or .fini_array
3351 // output section.
3352
3353 bool
3354 Output_section::Input_section_sort_init_fini_compare::operator()(
3355 const Output_section::Input_section_sort_entry& s1,
3356 const Output_section::Input_section_sort_entry& s2) const
3357 {
3358 // We sort all the sections with no names to the end.
3359 if (!s1.section_has_name() || !s2.section_has_name())
3360 {
3361 if (s1.section_has_name())
3362 return true;
3363 if (s2.section_has_name())
3364 return false;
3365 return s1.index() < s2.index();
3366 }
3367
3368 // A section without a priority follows a section with a priority.
3369 // This is the reverse of .ctors and .dtors sections.
3370 bool s1_has_priority = s1.has_priority();
3371 bool s2_has_priority = s2.has_priority();
3372 if (s1_has_priority && !s2_has_priority)
3373 return true;
3374 if (!s1_has_priority && s2_has_priority)
3375 return false;
3376
3377 // .ctors and .dtors sections without priority come after
3378 // .init_array and .fini_array sections without priority.
3379 if (!s1_has_priority
3380 && (s1.section_name() == ".ctors" || s1.section_name() == ".dtors")
3381 && s1.section_name() != s2.section_name())
3382 return false;
3383 if (!s2_has_priority
3384 && (s2.section_name() == ".ctors" || s2.section_name() == ".dtors")
3385 && s2.section_name() != s1.section_name())
3386 return true;
3387
3388 // Sort by priority if we can.
3389 if (s1_has_priority)
3390 {
3391 unsigned int s1_prio = s1.get_priority();
3392 unsigned int s2_prio = s2.get_priority();
3393 if (s1_prio < s2_prio)
3394 return true;
3395 else if (s1_prio > s2_prio)
3396 return false;
3397 }
3398
3399 // Check if a section order exists for these sections through a section
3400 // ordering file. If sequence_num is 0, an order does not exist.
3401 int sequence_num = s1.compare_section_ordering(s2);
3402 if (sequence_num != 0)
3403 return sequence_num == 1;
3404
3405 // Otherwise we sort by name.
3406 int compare = s1.section_name().compare(s2.section_name());
3407 if (compare != 0)
3408 return compare < 0;
3409
3410 // Otherwise we keep the input order.
3411 return s1.index() < s2.index();
3412 }
3413
3414 // Return true if S1 should come before S2. Sections that do not match
3415 // any pattern in the section ordering file are placed ahead of the sections
3416 // that match some pattern.
3417
3418 bool
3419 Output_section::Input_section_sort_section_order_index_compare::operator()(
3420 const Output_section::Input_section_sort_entry& s1,
3421 const Output_section::Input_section_sort_entry& s2) const
3422 {
3423 unsigned int s1_secn_index = s1.input_section().section_order_index();
3424 unsigned int s2_secn_index = s2.input_section().section_order_index();
3425
3426 // Keep input order if section ordering cannot determine order.
3427 if (s1_secn_index == s2_secn_index)
3428 return s1.index() < s2.index();
3429
3430 return s1_secn_index < s2_secn_index;
3431 }
3432
3433 // This updates the section order index of input sections according to the
3434 // the order specified in the mapping from Section id to order index.
3435
3436 void
3437 Output_section::update_section_layout(
3438 const Section_layout_order* order_map)
3439 {
3440 for (Input_section_list::iterator p = this->input_sections_.begin();
3441 p != this->input_sections_.end();
3442 ++p)
3443 {
3444 if (p->is_input_section()
3445 || p->is_relaxed_input_section())
3446 {
3447 Object* obj = (p->is_input_section()
3448 ? p->relobj()
3449 : p->relaxed_input_section()->relobj());
3450 unsigned int shndx = p->shndx();
3451 Section_layout_order::const_iterator it
3452 = order_map->find(Section_id(obj, shndx));
3453 if (it == order_map->end())
3454 continue;
3455 unsigned int section_order_index = it->second;
3456 if (section_order_index != 0)
3457 {
3458 p->set_section_order_index(section_order_index);
3459 this->set_input_section_order_specified();
3460 }
3461 }
3462 }
3463 }
3464
3465 // Sort the input sections attached to an output section.
3466
3467 void
3468 Output_section::sort_attached_input_sections()
3469 {
3470 if (this->attached_input_sections_are_sorted_)
3471 return;
3472
3473 if (this->checkpoint_ != NULL
3474 && !this->checkpoint_->input_sections_saved())
3475 this->checkpoint_->save_input_sections();
3476
3477 // The only thing we know about an input section is the object and
3478 // the section index. We need the section name. Recomputing this
3479 // is slow but this is an unusual case. If this becomes a speed
3480 // problem we can cache the names as required in Layout::layout.
3481
3482 // We start by building a larger vector holding a copy of each
3483 // Input_section, plus its current index in the list and its name.
3484 std::vector<Input_section_sort_entry> sort_list;
3485
3486 unsigned int i = 0;
3487 for (Input_section_list::iterator p = this->input_sections_.begin();
3488 p != this->input_sections_.end();
3489 ++p, ++i)
3490 sort_list.push_back(Input_section_sort_entry(*p, i,
3491 this->must_sort_attached_input_sections()));
3492
3493 // Sort the input sections.
3494 if (this->must_sort_attached_input_sections())
3495 {
3496 if (this->type() == elfcpp::SHT_PREINIT_ARRAY
3497 || this->type() == elfcpp::SHT_INIT_ARRAY
3498 || this->type() == elfcpp::SHT_FINI_ARRAY)
3499 std::sort(sort_list.begin(), sort_list.end(),
3500 Input_section_sort_init_fini_compare());
3501 else
3502 std::sort(sort_list.begin(), sort_list.end(),
3503 Input_section_sort_compare());
3504 }
3505 else
3506 {
3507 gold_assert(this->input_section_order_specified());
3508 std::sort(sort_list.begin(), sort_list.end(),
3509 Input_section_sort_section_order_index_compare());
3510 }
3511
3512 // Copy the sorted input sections back to our list.
3513 this->input_sections_.clear();
3514 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
3515 p != sort_list.end();
3516 ++p)
3517 this->input_sections_.push_back(p->input_section());
3518 sort_list.clear();
3519
3520 // Remember that we sorted the input sections, since we might get
3521 // called again.
3522 this->attached_input_sections_are_sorted_ = true;
3523 }
3524
3525 // Write the section header to *OSHDR.
3526
3527 template<int size, bool big_endian>
3528 void
3529 Output_section::write_header(const Layout* layout,
3530 const Stringpool* secnamepool,
3531 elfcpp::Shdr_write<size, big_endian>* oshdr) const
3532 {
3533 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
3534 oshdr->put_sh_type(this->type_);
3535
3536 elfcpp::Elf_Xword flags = this->flags_;
3537 if (this->info_section_ != NULL && this->info_uses_section_index_)
3538 flags |= elfcpp::SHF_INFO_LINK;
3539 oshdr->put_sh_flags(flags);
3540
3541 oshdr->put_sh_addr(this->address());
3542 oshdr->put_sh_offset(this->offset());
3543 oshdr->put_sh_size(this->data_size());
3544 if (this->link_section_ != NULL)
3545 oshdr->put_sh_link(this->link_section_->out_shndx());
3546 else if (this->should_link_to_symtab_)
3547 oshdr->put_sh_link(layout->symtab_section_shndx());
3548 else if (this->should_link_to_dynsym_)
3549 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
3550 else
3551 oshdr->put_sh_link(this->link_);
3552
3553 elfcpp::Elf_Word info;
3554 if (this->info_section_ != NULL)
3555 {
3556 if (this->info_uses_section_index_)
3557 info = this->info_section_->out_shndx();
3558 else
3559 info = this->info_section_->symtab_index();
3560 }
3561 else if (this->info_symndx_ != NULL)
3562 info = this->info_symndx_->symtab_index();
3563 else
3564 info = this->info_;
3565 oshdr->put_sh_info(info);
3566
3567 oshdr->put_sh_addralign(this->addralign_);
3568 oshdr->put_sh_entsize(this->entsize_);
3569 }
3570
3571 // Write out the data. For input sections the data is written out by
3572 // Object::relocate, but we have to handle Output_section_data objects
3573 // here.
3574
3575 void
3576 Output_section::do_write(Output_file* of)
3577 {
3578 gold_assert(!this->requires_postprocessing());
3579
3580 // If the target performs relaxation, we delay filler generation until now.
3581 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3582
3583 off_t output_section_file_offset = this->offset();
3584 for (Fill_list::iterator p = this->fills_.begin();
3585 p != this->fills_.end();
3586 ++p)
3587 {
3588 std::string fill_data(parameters->target().code_fill(p->length()));
3589 of->write(output_section_file_offset + p->section_offset(),
3590 fill_data.data(), fill_data.size());
3591 }
3592
3593 off_t off = this->offset() + this->first_input_offset_;
3594 for (Input_section_list::iterator p = this->input_sections_.begin();
3595 p != this->input_sections_.end();
3596 ++p)
3597 {
3598 off_t aligned_off = align_address(off, p->addralign());
3599 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3600 {
3601 size_t fill_len = aligned_off - off;
3602 std::string fill_data(parameters->target().code_fill(fill_len));
3603 of->write(off, fill_data.data(), fill_data.size());
3604 }
3605
3606 p->write(of);
3607 off = aligned_off + p->data_size();
3608 }
3609
3610 // For incremental links, fill in unused chunks in debug sections
3611 // with dummy compilation unit headers.
3612 if (this->free_space_fill_ != NULL)
3613 {
3614 for (Free_list::Const_iterator p = this->free_list_.begin();
3615 p != this->free_list_.end();
3616 ++p)
3617 {
3618 off_t off = p->start_;
3619 size_t len = p->end_ - off;
3620 this->free_space_fill_->write(of, this->offset() + off, len);
3621 }
3622 if (this->patch_space_ > 0)
3623 {
3624 off_t off = this->current_data_size_for_child() - this->patch_space_;
3625 this->free_space_fill_->write(of, this->offset() + off,
3626 this->patch_space_);
3627 }
3628 }
3629 }
3630
3631 // If a section requires postprocessing, create the buffer to use.
3632
3633 void
3634 Output_section::create_postprocessing_buffer()
3635 {
3636 gold_assert(this->requires_postprocessing());
3637
3638 if (this->postprocessing_buffer_ != NULL)
3639 return;
3640
3641 if (!this->input_sections_.empty())
3642 {
3643 off_t off = this->first_input_offset_;
3644 for (Input_section_list::iterator p = this->input_sections_.begin();
3645 p != this->input_sections_.end();
3646 ++p)
3647 {
3648 off = align_address(off, p->addralign());
3649 p->finalize_data_size();
3650 off += p->data_size();
3651 }
3652 this->set_current_data_size_for_child(off);
3653 }
3654
3655 off_t buffer_size = this->current_data_size_for_child();
3656 this->postprocessing_buffer_ = new unsigned char[buffer_size];
3657 }
3658
3659 // Write all the data of an Output_section into the postprocessing
3660 // buffer. This is used for sections which require postprocessing,
3661 // such as compression. Input sections are handled by
3662 // Object::Relocate.
3663
3664 void
3665 Output_section::write_to_postprocessing_buffer()
3666 {
3667 gold_assert(this->requires_postprocessing());
3668
3669 // If the target performs relaxation, we delay filler generation until now.
3670 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
3671
3672 unsigned char* buffer = this->postprocessing_buffer();
3673 for (Fill_list::iterator p = this->fills_.begin();
3674 p != this->fills_.end();
3675 ++p)
3676 {
3677 std::string fill_data(parameters->target().code_fill(p->length()));
3678 memcpy(buffer + p->section_offset(), fill_data.data(),
3679 fill_data.size());
3680 }
3681
3682 off_t off = this->first_input_offset_;
3683 for (Input_section_list::iterator p = this->input_sections_.begin();
3684 p != this->input_sections_.end();
3685 ++p)
3686 {
3687 off_t aligned_off = align_address(off, p->addralign());
3688 if (this->generate_code_fills_at_write_ && (off != aligned_off))
3689 {
3690 size_t fill_len = aligned_off - off;
3691 std::string fill_data(parameters->target().code_fill(fill_len));
3692 memcpy(buffer + off, fill_data.data(), fill_data.size());
3693 }
3694
3695 p->write_to_buffer(buffer + aligned_off);
3696 off = aligned_off + p->data_size();
3697 }
3698 }
3699
3700 // Get the input sections for linker script processing. We leave
3701 // behind the Output_section_data entries. Note that this may be
3702 // slightly incorrect for merge sections. We will leave them behind,
3703 // but it is possible that the script says that they should follow
3704 // some other input sections, as in:
3705 // .rodata { *(.rodata) *(.rodata.cst*) }
3706 // For that matter, we don't handle this correctly:
3707 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
3708 // With luck this will never matter.
3709
3710 uint64_t
3711 Output_section::get_input_sections(
3712 uint64_t address,
3713 const std::string& fill,
3714 std::list<Input_section>* input_sections)
3715 {
3716 if (this->checkpoint_ != NULL
3717 && !this->checkpoint_->input_sections_saved())
3718 this->checkpoint_->save_input_sections();
3719
3720 // Invalidate fast look-up maps.
3721 this->lookup_maps_->invalidate();
3722
3723 uint64_t orig_address = address;
3724
3725 address = align_address(address, this->addralign());
3726
3727 Input_section_list remaining;
3728 for (Input_section_list::iterator p = this->input_sections_.begin();
3729 p != this->input_sections_.end();
3730 ++p)
3731 {
3732 if (p->is_input_section()
3733 || p->is_relaxed_input_section()
3734 || p->is_merge_section())
3735 input_sections->push_back(*p);
3736 else
3737 {
3738 uint64_t aligned_address = align_address(address, p->addralign());
3739 if (aligned_address != address && !fill.empty())
3740 {
3741 section_size_type length =
3742 convert_to_section_size_type(aligned_address - address);
3743 std::string this_fill;
3744 this_fill.reserve(length);
3745 while (this_fill.length() + fill.length() <= length)
3746 this_fill += fill;
3747 if (this_fill.length() < length)
3748 this_fill.append(fill, 0, length - this_fill.length());
3749
3750 Output_section_data* posd = new Output_data_const(this_fill, 0);
3751 remaining.push_back(Input_section(posd));
3752 }
3753 address = aligned_address;
3754
3755 remaining.push_back(*p);
3756
3757 p->finalize_data_size();
3758 address += p->data_size();
3759 }
3760 }
3761
3762 this->input_sections_.swap(remaining);
3763 this->first_input_offset_ = 0;
3764
3765 uint64_t data_size = address - orig_address;
3766 this->set_current_data_size_for_child(data_size);
3767 return data_size;
3768 }
3769
3770 // Add a script input section. SIS is an Output_section::Input_section,
3771 // which can be either a plain input section or a special input section like
3772 // a relaxed input section. For a special input section, its size must be
3773 // finalized.
3774
3775 void
3776 Output_section::add_script_input_section(const Input_section& sis)
3777 {
3778 uint64_t data_size = sis.data_size();
3779 uint64_t addralign = sis.addralign();
3780 if (addralign > this->addralign_)
3781 this->addralign_ = addralign;
3782
3783 off_t offset_in_section = this->current_data_size_for_child();
3784 off_t aligned_offset_in_section = align_address(offset_in_section,
3785 addralign);
3786
3787 this->set_current_data_size_for_child(aligned_offset_in_section
3788 + data_size);
3789
3790 this->input_sections_.push_back(sis);
3791
3792 // Update fast lookup maps if necessary.
3793 if (this->lookup_maps_->is_valid())
3794 {
3795 if (sis.is_merge_section())
3796 {
3797 Output_merge_base* pomb = sis.output_merge_base();
3798 Merge_section_properties msp(pomb->is_string(), pomb->entsize(),
3799 pomb->addralign());
3800 this->lookup_maps_->add_merge_section(msp, pomb);
3801 for (Output_merge_base::Input_sections::const_iterator p =
3802 pomb->input_sections_begin();
3803 p != pomb->input_sections_end();
3804 ++p)
3805 this->lookup_maps_->add_merge_input_section(p->first, p->second,
3806 pomb);
3807 }
3808 else if (sis.is_relaxed_input_section())
3809 {
3810 Output_relaxed_input_section* poris = sis.relaxed_input_section();
3811 this->lookup_maps_->add_relaxed_input_section(poris->relobj(),
3812 poris->shndx(), poris);
3813 }
3814 }
3815 }
3816
3817 // Save states for relaxation.
3818
3819 void
3820 Output_section::save_states()
3821 {
3822 gold_assert(this->checkpoint_ == NULL);
3823 Checkpoint_output_section* checkpoint =
3824 new Checkpoint_output_section(this->addralign_, this->flags_,
3825 this->input_sections_,
3826 this->first_input_offset_,
3827 this->attached_input_sections_are_sorted_);
3828 this->checkpoint_ = checkpoint;
3829 gold_assert(this->fills_.empty());
3830 }
3831
3832 void
3833 Output_section::discard_states()
3834 {
3835 gold_assert(this->checkpoint_ != NULL);
3836 delete this->checkpoint_;
3837 this->checkpoint_ = NULL;
3838 gold_assert(this->fills_.empty());
3839
3840 // Simply invalidate the fast lookup maps since we do not keep
3841 // track of them.
3842 this->lookup_maps_->invalidate();
3843 }
3844
3845 void
3846 Output_section::restore_states()
3847 {
3848 gold_assert(this->checkpoint_ != NULL);
3849 Checkpoint_output_section* checkpoint = this->checkpoint_;
3850
3851 this->addralign_ = checkpoint->addralign();
3852 this->flags_ = checkpoint->flags();
3853 this->first_input_offset_ = checkpoint->first_input_offset();
3854
3855 if (!checkpoint->input_sections_saved())
3856 {
3857 // If we have not copied the input sections, just resize it.
3858 size_t old_size = checkpoint->input_sections_size();
3859 gold_assert(this->input_sections_.size() >= old_size);
3860 this->input_sections_.resize(old_size);
3861 }
3862 else
3863 {
3864 // We need to copy the whole list. This is not efficient for
3865 // extremely large output with hundreads of thousands of input
3866 // objects. We may need to re-think how we should pass sections
3867 // to scripts.
3868 this->input_sections_ = *checkpoint->input_sections();
3869 }
3870
3871 this->attached_input_sections_are_sorted_ =
3872 checkpoint->attached_input_sections_are_sorted();
3873
3874 // Simply invalidate the fast lookup maps since we do not keep
3875 // track of them.
3876 this->lookup_maps_->invalidate();
3877 }
3878
3879 // Update the section offsets of input sections in this. This is required if
3880 // relaxation causes some input sections to change sizes.
3881
3882 void
3883 Output_section::adjust_section_offsets()
3884 {
3885 if (!this->section_offsets_need_adjustment_)
3886 return;
3887
3888 off_t off = 0;
3889 for (Input_section_list::iterator p = this->input_sections_.begin();
3890 p != this->input_sections_.end();
3891 ++p)
3892 {
3893 off = align_address(off, p->addralign());
3894 if (p->is_input_section())
3895 p->relobj()->set_section_offset(p->shndx(), off);
3896 off += p->data_size();
3897 }
3898
3899 this->section_offsets_need_adjustment_ = false;
3900 }
3901
3902 // Print to the map file.
3903
3904 void
3905 Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3906 {
3907 mapfile->print_output_section(this);
3908
3909 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3910 p != this->input_sections_.end();
3911 ++p)
3912 p->print_to_mapfile(mapfile);
3913 }
3914
3915 // Print stats for merge sections to stderr.
3916
3917 void
3918 Output_section::print_merge_stats()
3919 {
3920 Input_section_list::iterator p;
3921 for (p = this->input_sections_.begin();
3922 p != this->input_sections_.end();
3923 ++p)
3924 p->print_merge_stats(this->name_);
3925 }
3926
3927 // Set a fixed layout for the section. Used for incremental update links.
3928
3929 void
3930 Output_section::set_fixed_layout(uint64_t sh_addr, off_t sh_offset,
3931 off_t sh_size, uint64_t sh_addralign)
3932 {
3933 this->addralign_ = sh_addralign;
3934 this->set_current_data_size(sh_size);
3935 if ((this->flags_ & elfcpp::SHF_ALLOC) != 0)
3936 this->set_address(sh_addr);
3937 this->set_file_offset(sh_offset);
3938 this->finalize_data_size();
3939 this->free_list_.init(sh_size, false);
3940 this->has_fixed_layout_ = true;
3941 }
3942
3943 // Reserve space within the fixed layout for the section. Used for
3944 // incremental update links.
3945
3946 void
3947 Output_section::reserve(uint64_t sh_offset, uint64_t sh_size)
3948 {
3949 this->free_list_.remove(sh_offset, sh_offset + sh_size);
3950 }
3951
3952 // Allocate space from the free list for the section. Used for
3953 // incremental update links.
3954
3955 off_t
3956 Output_section::allocate(off_t len, uint64_t addralign)
3957 {
3958 return this->free_list_.allocate(len, addralign, 0);
3959 }
3960
3961 // Output segment methods.
3962
3963 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3964 : vaddr_(0),
3965 paddr_(0),
3966 memsz_(0),
3967 max_align_(0),
3968 min_p_align_(0),
3969 offset_(0),
3970 filesz_(0),
3971 type_(type),
3972 flags_(flags),
3973 is_max_align_known_(false),
3974 are_addresses_set_(false),
3975 is_large_data_segment_(false)
3976 {
3977 // The ELF ABI specifies that a PT_TLS segment always has PF_R as
3978 // the flags.
3979 if (type == elfcpp::PT_TLS)
3980 this->flags_ = elfcpp::PF_R;
3981 }
3982
3983 // Add an Output_section to a PT_LOAD Output_segment.
3984
3985 void
3986 Output_segment::add_output_section_to_load(Layout* layout,
3987 Output_section* os,
3988 elfcpp::Elf_Word seg_flags)
3989 {
3990 gold_assert(this->type() == elfcpp::PT_LOAD);
3991 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
3992 gold_assert(!this->is_max_align_known_);
3993 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
3994
3995 this->update_flags_for_output_section(seg_flags);
3996
3997 // We don't want to change the ordering if we have a linker script
3998 // with a SECTIONS clause.
3999 Output_section_order order = os->order();
4000 if (layout->script_options()->saw_sections_clause())
4001 order = static_cast<Output_section_order>(0);
4002 else
4003 gold_assert(order != ORDER_INVALID);
4004
4005 this->output_lists_[order].push_back(os);
4006 }
4007
4008 // Add an Output_section to a non-PT_LOAD Output_segment.
4009
4010 void
4011 Output_segment::add_output_section_to_nonload(Output_section* os,
4012 elfcpp::Elf_Word seg_flags)
4013 {
4014 gold_assert(this->type() != elfcpp::PT_LOAD);
4015 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
4016 gold_assert(!this->is_max_align_known_);
4017
4018 this->update_flags_for_output_section(seg_flags);
4019
4020 this->output_lists_[0].push_back(os);
4021 }
4022
4023 // Remove an Output_section from this segment. It is an error if it
4024 // is not present.
4025
4026 void
4027 Output_segment::remove_output_section(Output_section* os)
4028 {
4029 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4030 {
4031 Output_data_list* pdl = &this->output_lists_[i];
4032 for (Output_data_list::iterator p = pdl->begin(); p != pdl->end(); ++p)
4033 {
4034 if (*p == os)
4035 {
4036 pdl->erase(p);
4037 return;
4038 }
4039 }
4040 }
4041 gold_unreachable();
4042 }
4043
4044 // Add an Output_data (which need not be an Output_section) to the
4045 // start of a segment.
4046
4047 void
4048 Output_segment::add_initial_output_data(Output_data* od)
4049 {
4050 gold_assert(!this->is_max_align_known_);
4051 Output_data_list::iterator p = this->output_lists_[0].begin();
4052 this->output_lists_[0].insert(p, od);
4053 }
4054
4055 // Return true if this segment has any sections which hold actual
4056 // data, rather than being a BSS section.
4057
4058 bool
4059 Output_segment::has_any_data_sections() const
4060 {
4061 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4062 {
4063 const Output_data_list* pdl = &this->output_lists_[i];
4064 for (Output_data_list::const_iterator p = pdl->begin();
4065 p != pdl->end();
4066 ++p)
4067 {
4068 if (!(*p)->is_section())
4069 return true;
4070 if ((*p)->output_section()->type() != elfcpp::SHT_NOBITS)
4071 return true;
4072 }
4073 }
4074 return false;
4075 }
4076
4077 // Return whether the first data section (not counting TLS sections)
4078 // is a relro section.
4079
4080 bool
4081 Output_segment::is_first_section_relro() const
4082 {
4083 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4084 {
4085 if (i == static_cast<int>(ORDER_TLS_DATA)
4086 || i == static_cast<int>(ORDER_TLS_BSS))
4087 continue;
4088 const Output_data_list* pdl = &this->output_lists_[i];
4089 if (!pdl->empty())
4090 {
4091 Output_data* p = pdl->front();
4092 return p->is_section() && p->output_section()->is_relro();
4093 }
4094 }
4095 return false;
4096 }
4097
4098 // Return the maximum alignment of the Output_data in Output_segment.
4099
4100 uint64_t
4101 Output_segment::maximum_alignment()
4102 {
4103 if (!this->is_max_align_known_)
4104 {
4105 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4106 {
4107 const Output_data_list* pdl = &this->output_lists_[i];
4108 uint64_t addralign = Output_segment::maximum_alignment_list(pdl);
4109 if (addralign > this->max_align_)
4110 this->max_align_ = addralign;
4111 }
4112 this->is_max_align_known_ = true;
4113 }
4114
4115 return this->max_align_;
4116 }
4117
4118 // Return the maximum alignment of a list of Output_data.
4119
4120 uint64_t
4121 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
4122 {
4123 uint64_t ret = 0;
4124 for (Output_data_list::const_iterator p = pdl->begin();
4125 p != pdl->end();
4126 ++p)
4127 {
4128 uint64_t addralign = (*p)->addralign();
4129 if (addralign > ret)
4130 ret = addralign;
4131 }
4132 return ret;
4133 }
4134
4135 // Return whether this segment has any dynamic relocs.
4136
4137 bool
4138 Output_segment::has_dynamic_reloc() const
4139 {
4140 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4141 if (this->has_dynamic_reloc_list(&this->output_lists_[i]))
4142 return true;
4143 return false;
4144 }
4145
4146 // Return whether this Output_data_list has any dynamic relocs.
4147
4148 bool
4149 Output_segment::has_dynamic_reloc_list(const Output_data_list* pdl) const
4150 {
4151 for (Output_data_list::const_iterator p = pdl->begin();
4152 p != pdl->end();
4153 ++p)
4154 if ((*p)->has_dynamic_reloc())
4155 return true;
4156 return false;
4157 }
4158
4159 // Set the section addresses for an Output_segment. If RESET is true,
4160 // reset the addresses first. ADDR is the address and *POFF is the
4161 // file offset. Set the section indexes starting with *PSHNDX.
4162 // INCREASE_RELRO is the size of the portion of the first non-relro
4163 // section that should be included in the PT_GNU_RELRO segment.
4164 // If this segment has relro sections, and has been aligned for
4165 // that purpose, set *HAS_RELRO to TRUE. Return the address of
4166 // the immediately following segment. Update *HAS_RELRO, *POFF,
4167 // and *PSHNDX.
4168
4169 uint64_t
4170 Output_segment::set_section_addresses(Layout* layout, bool reset,
4171 uint64_t addr,
4172 unsigned int* increase_relro,
4173 bool* has_relro,
4174 off_t* poff,
4175 unsigned int* pshndx)
4176 {
4177 gold_assert(this->type_ == elfcpp::PT_LOAD);
4178
4179 uint64_t last_relro_pad = 0;
4180 off_t orig_off = *poff;
4181
4182 bool in_tls = false;
4183
4184 // If we have relro sections, we need to pad forward now so that the
4185 // relro sections plus INCREASE_RELRO end on a common page boundary.
4186 if (parameters->options().relro()
4187 && this->is_first_section_relro()
4188 && (!this->are_addresses_set_ || reset))
4189 {
4190 uint64_t relro_size = 0;
4191 off_t off = *poff;
4192 uint64_t max_align = 0;
4193 for (int i = 0; i <= static_cast<int>(ORDER_RELRO_LAST); ++i)
4194 {
4195 Output_data_list* pdl = &this->output_lists_[i];
4196 Output_data_list::iterator p;
4197 for (p = pdl->begin(); p != pdl->end(); ++p)
4198 {
4199 if (!(*p)->is_section())
4200 break;
4201 uint64_t align = (*p)->addralign();
4202 if (align > max_align)
4203 max_align = align;
4204 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4205 in_tls = true;
4206 else if (in_tls)
4207 {
4208 // Align the first non-TLS section to the alignment
4209 // of the TLS segment.
4210 align = max_align;
4211 in_tls = false;
4212 }
4213 relro_size = align_address(relro_size, align);
4214 // Ignore the size of the .tbss section.
4215 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
4216 && (*p)->is_section_type(elfcpp::SHT_NOBITS))
4217 continue;
4218 if ((*p)->is_address_valid())
4219 relro_size += (*p)->data_size();
4220 else
4221 {
4222 // FIXME: This could be faster.
4223 (*p)->set_address_and_file_offset(addr + relro_size,
4224 off + relro_size);
4225 relro_size += (*p)->data_size();
4226 (*p)->reset_address_and_file_offset();
4227 }
4228 }
4229 if (p != pdl->end())
4230 break;
4231 }
4232 relro_size += *increase_relro;
4233 // Pad the total relro size to a multiple of the maximum
4234 // section alignment seen.
4235 uint64_t aligned_size = align_address(relro_size, max_align);
4236 // Note the amount of padding added after the last relro section.
4237 last_relro_pad = aligned_size - relro_size;
4238 *has_relro = true;
4239
4240 uint64_t page_align = parameters->target().common_pagesize();
4241
4242 // Align to offset N such that (N + RELRO_SIZE) % PAGE_ALIGN == 0.
4243 uint64_t desired_align = page_align - (aligned_size % page_align);
4244 if (desired_align < *poff % page_align)
4245 *poff += page_align - *poff % page_align;
4246 *poff += desired_align - *poff % page_align;
4247 addr += *poff - orig_off;
4248 orig_off = *poff;
4249 }
4250
4251 if (!reset && this->are_addresses_set_)
4252 {
4253 gold_assert(this->paddr_ == addr);
4254 addr = this->vaddr_;
4255 }
4256 else
4257 {
4258 this->vaddr_ = addr;
4259 this->paddr_ = addr;
4260 this->are_addresses_set_ = true;
4261 }
4262
4263 in_tls = false;
4264
4265 this->offset_ = orig_off;
4266
4267 off_t off = 0;
4268 uint64_t ret;
4269 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4270 {
4271 if (i == static_cast<int>(ORDER_RELRO_LAST))
4272 {
4273 *poff += last_relro_pad;
4274 addr += last_relro_pad;
4275 if (this->output_lists_[i].empty())
4276 {
4277 // If there is nothing in the ORDER_RELRO_LAST list,
4278 // the padding will occur at the end of the relro
4279 // segment, and we need to add it to *INCREASE_RELRO.
4280 *increase_relro += last_relro_pad;
4281 }
4282 }
4283 addr = this->set_section_list_addresses(layout, reset,
4284 &this->output_lists_[i],
4285 addr, poff, pshndx, &in_tls);
4286 if (i < static_cast<int>(ORDER_SMALL_BSS))
4287 {
4288 this->filesz_ = *poff - orig_off;
4289 off = *poff;
4290 }
4291
4292 ret = addr;
4293 }
4294
4295 // If the last section was a TLS section, align upward to the
4296 // alignment of the TLS segment, so that the overall size of the TLS
4297 // segment is aligned.
4298 if (in_tls)
4299 {
4300 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
4301 *poff = align_address(*poff, segment_align);
4302 }
4303
4304 this->memsz_ = *poff - orig_off;
4305
4306 // Ignore the file offset adjustments made by the BSS Output_data
4307 // objects.
4308 *poff = off;
4309
4310 return ret;
4311 }
4312
4313 // Set the addresses and file offsets in a list of Output_data
4314 // structures.
4315
4316 uint64_t
4317 Output_segment::set_section_list_addresses(Layout* layout, bool reset,
4318 Output_data_list* pdl,
4319 uint64_t addr, off_t* poff,
4320 unsigned int* pshndx,
4321 bool* in_tls)
4322 {
4323 off_t startoff = *poff;
4324 // For incremental updates, we may allocate non-fixed sections from
4325 // free space in the file. This keeps track of the high-water mark.
4326 off_t maxoff = startoff;
4327
4328 off_t off = startoff;
4329 for (Output_data_list::iterator p = pdl->begin();
4330 p != pdl->end();
4331 ++p)
4332 {
4333 if (reset)
4334 (*p)->reset_address_and_file_offset();
4335
4336 // When doing an incremental update or when using a linker script,
4337 // the section will most likely already have an address.
4338 if (!(*p)->is_address_valid())
4339 {
4340 uint64_t align = (*p)->addralign();
4341
4342 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
4343 {
4344 // Give the first TLS section the alignment of the
4345 // entire TLS segment. Otherwise the TLS segment as a
4346 // whole may be misaligned.
4347 if (!*in_tls)
4348 {
4349 Output_segment* tls_segment = layout->tls_segment();
4350 gold_assert(tls_segment != NULL);
4351 uint64_t segment_align = tls_segment->maximum_alignment();
4352 gold_assert(segment_align >= align);
4353 align = segment_align;
4354
4355 *in_tls = true;
4356 }
4357 }
4358 else
4359 {
4360 // If this is the first section after the TLS segment,
4361 // align it to at least the alignment of the TLS
4362 // segment, so that the size of the overall TLS segment
4363 // is aligned.
4364 if (*in_tls)
4365 {
4366 uint64_t segment_align =
4367 layout->tls_segment()->maximum_alignment();
4368 if (segment_align > align)
4369 align = segment_align;
4370
4371 *in_tls = false;
4372 }
4373 }
4374
4375 if (!parameters->incremental_update())
4376 {
4377 off = align_address(off, align);
4378 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4379 }
4380 else
4381 {
4382 // Incremental update: allocate file space from free list.
4383 (*p)->pre_finalize_data_size();
4384 off_t current_size = (*p)->current_data_size();
4385 off = layout->allocate(current_size, align, startoff);
4386 if (off == -1)
4387 {
4388 gold_assert((*p)->output_section() != NULL);
4389 gold_fallback(_("out of patch space for section %s; "
4390 "relink with --incremental-full"),
4391 (*p)->output_section()->name());
4392 }
4393 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
4394 if ((*p)->data_size() > current_size)
4395 {
4396 gold_assert((*p)->output_section() != NULL);
4397 gold_fallback(_("%s: section changed size; "
4398 "relink with --incremental-full"),
4399 (*p)->output_section()->name());
4400 }
4401 }
4402 }
4403 else if (parameters->incremental_update())
4404 {
4405 // For incremental updates, use the fixed offset for the
4406 // high-water mark computation.
4407 off = (*p)->offset();
4408 }
4409 else
4410 {
4411 // The script may have inserted a skip forward, but it
4412 // better not have moved backward.
4413 if ((*p)->address() >= addr + (off - startoff))
4414 off += (*p)->address() - (addr + (off - startoff));
4415 else
4416 {
4417 if (!layout->script_options()->saw_sections_clause())
4418 gold_unreachable();
4419 else
4420 {
4421 Output_section* os = (*p)->output_section();
4422
4423 // Cast to unsigned long long to avoid format warnings.
4424 unsigned long long previous_dot =
4425 static_cast<unsigned long long>(addr + (off - startoff));
4426 unsigned long long dot =
4427 static_cast<unsigned long long>((*p)->address());
4428
4429 if (os == NULL)
4430 gold_error(_("dot moves backward in linker script "
4431 "from 0x%llx to 0x%llx"), previous_dot, dot);
4432 else
4433 gold_error(_("address of section '%s' moves backward "
4434 "from 0x%llx to 0x%llx"),
4435 os->name(), previous_dot, dot);
4436 }
4437 }
4438 (*p)->set_file_offset(off);
4439 (*p)->finalize_data_size();
4440 }
4441
4442 if (parameters->incremental_update())
4443 gold_debug(DEBUG_INCREMENTAL,
4444 "set_section_list_addresses: %08lx %08lx %s",
4445 static_cast<long>(off),
4446 static_cast<long>((*p)->data_size()),
4447 ((*p)->output_section() != NULL
4448 ? (*p)->output_section()->name() : "(special)"));
4449
4450 // We want to ignore the size of a SHF_TLS SHT_NOBITS
4451 // section. Such a section does not affect the size of a
4452 // PT_LOAD segment.
4453 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
4454 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
4455 off += (*p)->data_size();
4456
4457 if (off > maxoff)
4458 maxoff = off;
4459
4460 if ((*p)->is_section())
4461 {
4462 (*p)->set_out_shndx(*pshndx);
4463 ++*pshndx;
4464 }
4465 }
4466
4467 *poff = maxoff;
4468 return addr + (maxoff - startoff);
4469 }
4470
4471 // For a non-PT_LOAD segment, set the offset from the sections, if
4472 // any. Add INCREASE to the file size and the memory size.
4473
4474 void
4475 Output_segment::set_offset(unsigned int increase)
4476 {
4477 gold_assert(this->type_ != elfcpp::PT_LOAD);
4478
4479 gold_assert(!this->are_addresses_set_);
4480
4481 // A non-load section only uses output_lists_[0].
4482
4483 Output_data_list* pdl = &this->output_lists_[0];
4484
4485 if (pdl->empty())
4486 {
4487 gold_assert(increase == 0);
4488 this->vaddr_ = 0;
4489 this->paddr_ = 0;
4490 this->are_addresses_set_ = true;
4491 this->memsz_ = 0;
4492 this->min_p_align_ = 0;
4493 this->offset_ = 0;
4494 this->filesz_ = 0;
4495 return;
4496 }
4497
4498 // Find the first and last section by address.
4499 const Output_data* first = NULL;
4500 const Output_data* last_data = NULL;
4501 const Output_data* last_bss = NULL;
4502 for (Output_data_list::const_iterator p = pdl->begin();
4503 p != pdl->end();
4504 ++p)
4505 {
4506 if (first == NULL
4507 || (*p)->address() < first->address()
4508 || ((*p)->address() == first->address()
4509 && (*p)->data_size() < first->data_size()))
4510 first = *p;
4511 const Output_data** plast;
4512 if ((*p)->is_section()
4513 && (*p)->output_section()->type() == elfcpp::SHT_NOBITS)
4514 plast = &last_bss;
4515 else
4516 plast = &last_data;
4517 if (*plast == NULL
4518 || (*p)->address() > (*plast)->address()
4519 || ((*p)->address() == (*plast)->address()
4520 && (*p)->data_size() > (*plast)->data_size()))
4521 *plast = *p;
4522 }
4523
4524 this->vaddr_ = first->address();
4525 this->paddr_ = (first->has_load_address()
4526 ? first->load_address()
4527 : this->vaddr_);
4528 this->are_addresses_set_ = true;
4529 this->offset_ = first->offset();
4530
4531 if (last_data == NULL)
4532 this->filesz_ = 0;
4533 else
4534 this->filesz_ = (last_data->address()
4535 + last_data->data_size()
4536 - this->vaddr_);
4537
4538 const Output_data* last = last_bss != NULL ? last_bss : last_data;
4539 this->memsz_ = (last->address()
4540 + last->data_size()
4541 - this->vaddr_);
4542
4543 this->filesz_ += increase;
4544 this->memsz_ += increase;
4545
4546 // If this is a RELRO segment, verify that the segment ends at a
4547 // page boundary.
4548 if (this->type_ == elfcpp::PT_GNU_RELRO)
4549 {
4550 uint64_t page_align = parameters->target().common_pagesize();
4551 uint64_t segment_end = this->vaddr_ + this->memsz_;
4552 if (parameters->incremental_update())
4553 {
4554 // The INCREASE_RELRO calculation is bypassed for an incremental
4555 // update, so we need to adjust the segment size manually here.
4556 segment_end = align_address(segment_end, page_align);
4557 this->memsz_ = segment_end - this->vaddr_;
4558 }
4559 else
4560 gold_assert(segment_end == align_address(segment_end, page_align));
4561 }
4562
4563 // If this is a TLS segment, align the memory size. The code in
4564 // set_section_list ensures that the section after the TLS segment
4565 // is aligned to give us room.
4566 if (this->type_ == elfcpp::PT_TLS)
4567 {
4568 uint64_t segment_align = this->maximum_alignment();
4569 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
4570 this->memsz_ = align_address(this->memsz_, segment_align);
4571 }
4572 }
4573
4574 // Set the TLS offsets of the sections in the PT_TLS segment.
4575
4576 void
4577 Output_segment::set_tls_offsets()
4578 {
4579 gold_assert(this->type_ == elfcpp::PT_TLS);
4580
4581 for (Output_data_list::iterator p = this->output_lists_[0].begin();
4582 p != this->output_lists_[0].end();
4583 ++p)
4584 (*p)->set_tls_offset(this->vaddr_);
4585 }
4586
4587 // Return the load address of the first section.
4588
4589 uint64_t
4590 Output_segment::first_section_load_address() const
4591 {
4592 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4593 {
4594 const Output_data_list* pdl = &this->output_lists_[i];
4595 for (Output_data_list::const_iterator p = pdl->begin();
4596 p != pdl->end();
4597 ++p)
4598 {
4599 if ((*p)->is_section())
4600 return ((*p)->has_load_address()
4601 ? (*p)->load_address()
4602 : (*p)->address());
4603 }
4604 }
4605 gold_unreachable();
4606 }
4607
4608 // Return the number of Output_sections in an Output_segment.
4609
4610 unsigned int
4611 Output_segment::output_section_count() const
4612 {
4613 unsigned int ret = 0;
4614 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4615 ret += this->output_section_count_list(&this->output_lists_[i]);
4616 return ret;
4617 }
4618
4619 // Return the number of Output_sections in an Output_data_list.
4620
4621 unsigned int
4622 Output_segment::output_section_count_list(const Output_data_list* pdl) const
4623 {
4624 unsigned int count = 0;
4625 for (Output_data_list::const_iterator p = pdl->begin();
4626 p != pdl->end();
4627 ++p)
4628 {
4629 if ((*p)->is_section())
4630 ++count;
4631 }
4632 return count;
4633 }
4634
4635 // Return the section attached to the list segment with the lowest
4636 // load address. This is used when handling a PHDRS clause in a
4637 // linker script.
4638
4639 Output_section*
4640 Output_segment::section_with_lowest_load_address() const
4641 {
4642 Output_section* found = NULL;
4643 uint64_t found_lma = 0;
4644 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4645 this->lowest_load_address_in_list(&this->output_lists_[i], &found,
4646 &found_lma);
4647 return found;
4648 }
4649
4650 // Look through a list for a section with a lower load address.
4651
4652 void
4653 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
4654 Output_section** found,
4655 uint64_t* found_lma) const
4656 {
4657 for (Output_data_list::const_iterator p = pdl->begin();
4658 p != pdl->end();
4659 ++p)
4660 {
4661 if (!(*p)->is_section())
4662 continue;
4663 Output_section* os = static_cast<Output_section*>(*p);
4664 uint64_t lma = (os->has_load_address()
4665 ? os->load_address()
4666 : os->address());
4667 if (*found == NULL || lma < *found_lma)
4668 {
4669 *found = os;
4670 *found_lma = lma;
4671 }
4672 }
4673 }
4674
4675 // Write the segment data into *OPHDR.
4676
4677 template<int size, bool big_endian>
4678 void
4679 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
4680 {
4681 ophdr->put_p_type(this->type_);
4682 ophdr->put_p_offset(this->offset_);
4683 ophdr->put_p_vaddr(this->vaddr_);
4684 ophdr->put_p_paddr(this->paddr_);
4685 ophdr->put_p_filesz(this->filesz_);
4686 ophdr->put_p_memsz(this->memsz_);
4687 ophdr->put_p_flags(this->flags_);
4688 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
4689 }
4690
4691 // Write the section headers into V.
4692
4693 template<int size, bool big_endian>
4694 unsigned char*
4695 Output_segment::write_section_headers(const Layout* layout,
4696 const Stringpool* secnamepool,
4697 unsigned char* v,
4698 unsigned int* pshndx) const
4699 {
4700 // Every section that is attached to a segment must be attached to a
4701 // PT_LOAD segment, so we only write out section headers for PT_LOAD
4702 // segments.
4703 if (this->type_ != elfcpp::PT_LOAD)
4704 return v;
4705
4706 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4707 {
4708 const Output_data_list* pdl = &this->output_lists_[i];
4709 v = this->write_section_headers_list<size, big_endian>(layout,
4710 secnamepool,
4711 pdl,
4712 v, pshndx);
4713 }
4714
4715 return v;
4716 }
4717
4718 template<int size, bool big_endian>
4719 unsigned char*
4720 Output_segment::write_section_headers_list(const Layout* layout,
4721 const Stringpool* secnamepool,
4722 const Output_data_list* pdl,
4723 unsigned char* v,
4724 unsigned int* pshndx) const
4725 {
4726 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
4727 for (Output_data_list::const_iterator p = pdl->begin();
4728 p != pdl->end();
4729 ++p)
4730 {
4731 if ((*p)->is_section())
4732 {
4733 const Output_section* ps = static_cast<const Output_section*>(*p);
4734 gold_assert(*pshndx == ps->out_shndx());
4735 elfcpp::Shdr_write<size, big_endian> oshdr(v);
4736 ps->write_header(layout, secnamepool, &oshdr);
4737 v += shdr_size;
4738 ++*pshndx;
4739 }
4740 }
4741 return v;
4742 }
4743
4744 // Print the output sections to the map file.
4745
4746 void
4747 Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
4748 {
4749 if (this->type() != elfcpp::PT_LOAD)
4750 return;
4751 for (int i = 0; i < static_cast<int>(ORDER_MAX); ++i)
4752 this->print_section_list_to_mapfile(mapfile, &this->output_lists_[i]);
4753 }
4754
4755 // Print an output section list to the map file.
4756
4757 void
4758 Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
4759 const Output_data_list* pdl) const
4760 {
4761 for (Output_data_list::const_iterator p = pdl->begin();
4762 p != pdl->end();
4763 ++p)
4764 (*p)->print_to_mapfile(mapfile);
4765 }
4766
4767 // Output_file methods.
4768
4769 Output_file::Output_file(const char* name)
4770 : name_(name),
4771 o_(-1),
4772 file_size_(0),
4773 base_(NULL),
4774 map_is_anonymous_(false),
4775 map_is_allocated_(false),
4776 is_temporary_(false)
4777 {
4778 }
4779
4780 // Try to open an existing file. Returns false if the file doesn't
4781 // exist, has a size of 0 or can't be mmapped. If BASE_NAME is not
4782 // NULL, open that file as the base for incremental linking, and
4783 // copy its contents to the new output file. This routine can
4784 // be called for incremental updates, in which case WRITABLE should
4785 // be true, or by the incremental-dump utility, in which case
4786 // WRITABLE should be false.
4787
4788 bool
4789 Output_file::open_base_file(const char* base_name, bool writable)
4790 {
4791 // The name "-" means "stdout".
4792 if (strcmp(this->name_, "-") == 0)
4793 return false;
4794
4795 bool use_base_file = base_name != NULL;
4796 if (!use_base_file)
4797 base_name = this->name_;
4798 else if (strcmp(base_name, this->name_) == 0)
4799 gold_fatal(_("%s: incremental base and output file name are the same"),
4800 base_name);
4801
4802 // Don't bother opening files with a size of zero.
4803 struct stat s;
4804 if (::stat(base_name, &s) != 0)
4805 {
4806 gold_info(_("%s: stat: %s"), base_name, strerror(errno));
4807 return false;
4808 }
4809 if (s.st_size == 0)
4810 {
4811 gold_info(_("%s: incremental base file is empty"), base_name);
4812 return false;
4813 }
4814
4815 // If we're using a base file, we want to open it read-only.
4816 if (use_base_file)
4817 writable = false;
4818
4819 int oflags = writable ? O_RDWR : O_RDONLY;
4820 int o = open_descriptor(-1, base_name, oflags, 0);
4821 if (o < 0)
4822 {
4823 gold_info(_("%s: open: %s"), base_name, strerror(errno));
4824 return false;
4825 }
4826
4827 // If the base file and the output file are different, open a
4828 // new output file and read the contents from the base file into
4829 // the newly-mapped region.
4830 if (use_base_file)
4831 {
4832 this->open(s.st_size);
4833 ssize_t bytes_to_read = s.st_size;
4834 unsigned char* p = this->base_;
4835 while (bytes_to_read > 0)
4836 {
4837 ssize_t len = ::read(o, p, bytes_to_read);
4838 if (len < 0)
4839 {
4840 gold_info(_("%s: read failed: %s"), base_name, strerror(errno));
4841 return false;
4842 }
4843 if (len == 0)
4844 {
4845 gold_info(_("%s: file too short: read only %lld of %lld bytes"),
4846 base_name,
4847 static_cast<long long>(s.st_size - bytes_to_read),
4848 static_cast<long long>(s.st_size));
4849 return false;
4850 }
4851 p += len;
4852 bytes_to_read -= len;
4853 }
4854 ::close(o);
4855 return true;
4856 }
4857
4858 this->o_ = o;
4859 this->file_size_ = s.st_size;
4860
4861 if (!this->map_no_anonymous(writable))
4862 {
4863 release_descriptor(o, true);
4864 this->o_ = -1;
4865 this->file_size_ = 0;
4866 return false;
4867 }
4868
4869 return true;
4870 }
4871
4872 // Open the output file.
4873
4874 void
4875 Output_file::open(off_t file_size)
4876 {
4877 this->file_size_ = file_size;
4878
4879 // Unlink the file first; otherwise the open() may fail if the file
4880 // is busy (e.g. it's an executable that's currently being executed).
4881 //
4882 // However, the linker may be part of a system where a zero-length
4883 // file is created for it to write to, with tight permissions (gcc
4884 // 2.95 did something like this). Unlinking the file would work
4885 // around those permission controls, so we only unlink if the file
4886 // has a non-zero size. We also unlink only regular files to avoid
4887 // trouble with directories/etc.
4888 //
4889 // If we fail, continue; this command is merely a best-effort attempt
4890 // to improve the odds for open().
4891
4892 // We let the name "-" mean "stdout"
4893 if (!this->is_temporary_)
4894 {
4895 if (strcmp(this->name_, "-") == 0)
4896 this->o_ = STDOUT_FILENO;
4897 else
4898 {
4899 struct stat s;
4900 if (::stat(this->name_, &s) == 0
4901 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
4902 {
4903 if (s.st_size != 0)
4904 ::unlink(this->name_);
4905 else if (!parameters->options().relocatable())
4906 {
4907 // If we don't unlink the existing file, add execute
4908 // permission where read permissions already exist
4909 // and where the umask permits.
4910 int mask = ::umask(0);
4911 ::umask(mask);
4912 s.st_mode |= (s.st_mode & 0444) >> 2;
4913 ::chmod(this->name_, s.st_mode & ~mask);
4914 }
4915 }
4916
4917 int mode = parameters->options().relocatable() ? 0666 : 0777;
4918 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
4919 mode);
4920 if (o < 0)
4921 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
4922 this->o_ = o;
4923 }
4924 }
4925
4926 this->map();
4927 }
4928
4929 // Resize the output file.
4930
4931 void
4932 Output_file::resize(off_t file_size)
4933 {
4934 // If the mmap is mapping an anonymous memory buffer, this is easy:
4935 // just mremap to the new size. If it's mapping to a file, we want
4936 // to unmap to flush to the file, then remap after growing the file.
4937 if (this->map_is_anonymous_)
4938 {
4939 void* base;
4940 if (!this->map_is_allocated_)
4941 {
4942 base = ::mremap(this->base_, this->file_size_, file_size,
4943 MREMAP_MAYMOVE);
4944 if (base == MAP_FAILED)
4945 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
4946 }
4947 else
4948 {
4949 base = realloc(this->base_, file_size);
4950 if (base == NULL)
4951 gold_nomem();
4952 if (file_size > this->file_size_)
4953 memset(static_cast<char*>(base) + this->file_size_, 0,
4954 file_size - this->file_size_);
4955 }
4956 this->base_ = static_cast<unsigned char*>(base);
4957 this->file_size_ = file_size;
4958 }
4959 else
4960 {
4961 this->unmap();
4962 this->file_size_ = file_size;
4963 if (!this->map_no_anonymous(true))
4964 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
4965 }
4966 }
4967
4968 // Map an anonymous block of memory which will later be written to the
4969 // file. Return whether the map succeeded.
4970
4971 bool
4972 Output_file::map_anonymous()
4973 {
4974 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
4975 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
4976 if (base == MAP_FAILED)
4977 {
4978 base = malloc(this->file_size_);
4979 if (base == NULL)
4980 return false;
4981 memset(base, 0, this->file_size_);
4982 this->map_is_allocated_ = true;
4983 }
4984 this->base_ = static_cast<unsigned char*>(base);
4985 this->map_is_anonymous_ = true;
4986 return true;
4987 }
4988
4989 // Map the file into memory. Return whether the mapping succeeded.
4990 // If WRITABLE is true, map with write access.
4991
4992 bool
4993 Output_file::map_no_anonymous(bool writable)
4994 {
4995 const int o = this->o_;
4996
4997 // If the output file is not a regular file, don't try to mmap it;
4998 // instead, we'll mmap a block of memory (an anonymous buffer), and
4999 // then later write the buffer to the file.
5000 void* base;
5001 struct stat statbuf;
5002 if (o == STDOUT_FILENO || o == STDERR_FILENO
5003 || ::fstat(o, &statbuf) != 0
5004 || !S_ISREG(statbuf.st_mode)
5005 || this->is_temporary_)
5006 return false;
5007
5008 // Ensure that we have disk space available for the file. If we
5009 // don't do this, it is possible that we will call munmap, close,
5010 // and exit with dirty buffers still in the cache with no assigned
5011 // disk blocks. If the disk is out of space at that point, the
5012 // output file will wind up incomplete, but we will have already
5013 // exited. The alternative to fallocate would be to use fdatasync,
5014 // but that would be a more significant performance hit.
5015 if (writable)
5016 {
5017 int err = ::posix_fallocate(o, 0, this->file_size_);
5018 if (err != 0)
5019 gold_fatal(_("%s: %s"), this->name_, strerror(err));
5020 }
5021
5022 // Map the file into memory.
5023 int prot = PROT_READ;
5024 if (writable)
5025 prot |= PROT_WRITE;
5026 base = ::mmap(NULL, this->file_size_, prot, MAP_SHARED, o, 0);
5027
5028 // The mmap call might fail because of file system issues: the file
5029 // system might not support mmap at all, or it might not support
5030 // mmap with PROT_WRITE.
5031 if (base == MAP_FAILED)
5032 return false;
5033
5034 this->map_is_anonymous_ = false;
5035 this->base_ = static_cast<unsigned char*>(base);
5036 return true;
5037 }
5038
5039 // Map the file into memory.
5040
5041 void
5042 Output_file::map()
5043 {
5044 if (this->map_no_anonymous(true))
5045 return;
5046
5047 // The mmap call might fail because of file system issues: the file
5048 // system might not support mmap at all, or it might not support
5049 // mmap with PROT_WRITE. I'm not sure which errno values we will
5050 // see in all cases, so if the mmap fails for any reason and we
5051 // don't care about file contents, try for an anonymous map.
5052 if (this->map_anonymous())
5053 return;
5054
5055 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
5056 this->name_, static_cast<unsigned long>(this->file_size_),
5057 strerror(errno));
5058 }
5059
5060 // Unmap the file from memory.
5061
5062 void
5063 Output_file::unmap()
5064 {
5065 if (this->map_is_anonymous_)
5066 {
5067 // We've already written out the data, so there is no reason to
5068 // waste time unmapping or freeing the memory.
5069 }
5070 else
5071 {
5072 if (::munmap(this->base_, this->file_size_) < 0)
5073 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
5074 }
5075 this->base_ = NULL;
5076 }
5077
5078 // Close the output file.
5079
5080 void
5081 Output_file::close()
5082 {
5083 // If the map isn't file-backed, we need to write it now.
5084 if (this->map_is_anonymous_ && !this->is_temporary_)
5085 {
5086 size_t bytes_to_write = this->file_size_;
5087 size_t offset = 0;
5088 while (bytes_to_write > 0)
5089 {
5090 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
5091 bytes_to_write);
5092 if (bytes_written == 0)
5093 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
5094 else if (bytes_written < 0)
5095 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
5096 else
5097 {
5098 bytes_to_write -= bytes_written;
5099 offset += bytes_written;
5100 }
5101 }
5102 }
5103 this->unmap();
5104
5105 // We don't close stdout or stderr
5106 if (this->o_ != STDOUT_FILENO
5107 && this->o_ != STDERR_FILENO
5108 && !this->is_temporary_)
5109 if (::close(this->o_) < 0)
5110 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
5111 this->o_ = -1;
5112 }
5113
5114 // Instantiate the templates we need. We could use the configure
5115 // script to restrict this to only the ones for implemented targets.
5116
5117 #ifdef HAVE_TARGET_32_LITTLE
5118 template
5119 off_t
5120 Output_section::add_input_section<32, false>(
5121 Layout* layout,
5122 Sized_relobj_file<32, false>* object,
5123 unsigned int shndx,
5124 const char* secname,
5125 const elfcpp::Shdr<32, false>& shdr,
5126 unsigned int reloc_shndx,
5127 bool have_sections_script);
5128 #endif
5129
5130 #ifdef HAVE_TARGET_32_BIG
5131 template
5132 off_t
5133 Output_section::add_input_section<32, true>(
5134 Layout* layout,
5135 Sized_relobj_file<32, true>* object,
5136 unsigned int shndx,
5137 const char* secname,
5138 const elfcpp::Shdr<32, true>& shdr,
5139 unsigned int reloc_shndx,
5140 bool have_sections_script);
5141 #endif
5142
5143 #ifdef HAVE_TARGET_64_LITTLE
5144 template
5145 off_t
5146 Output_section::add_input_section<64, false>(
5147 Layout* layout,
5148 Sized_relobj_file<64, false>* object,
5149 unsigned int shndx,
5150 const char* secname,
5151 const elfcpp::Shdr<64, false>& shdr,
5152 unsigned int reloc_shndx,
5153 bool have_sections_script);
5154 #endif
5155
5156 #ifdef HAVE_TARGET_64_BIG
5157 template
5158 off_t
5159 Output_section::add_input_section<64, true>(
5160 Layout* layout,
5161 Sized_relobj_file<64, true>* object,
5162 unsigned int shndx,
5163 const char* secname,
5164 const elfcpp::Shdr<64, true>& shdr,
5165 unsigned int reloc_shndx,
5166 bool have_sections_script);
5167 #endif
5168
5169 #ifdef HAVE_TARGET_32_LITTLE
5170 template
5171 class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
5172 #endif
5173
5174 #ifdef HAVE_TARGET_32_BIG
5175 template
5176 class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
5177 #endif
5178
5179 #ifdef HAVE_TARGET_64_LITTLE
5180 template
5181 class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
5182 #endif
5183
5184 #ifdef HAVE_TARGET_64_BIG
5185 template
5186 class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
5187 #endif
5188
5189 #ifdef HAVE_TARGET_32_LITTLE
5190 template
5191 class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
5192 #endif
5193
5194 #ifdef HAVE_TARGET_32_BIG
5195 template
5196 class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
5197 #endif
5198
5199 #ifdef HAVE_TARGET_64_LITTLE
5200 template
5201 class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
5202 #endif
5203
5204 #ifdef HAVE_TARGET_64_BIG
5205 template
5206 class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
5207 #endif
5208
5209 #ifdef HAVE_TARGET_32_LITTLE
5210 template
5211 class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
5212 #endif
5213
5214 #ifdef HAVE_TARGET_32_BIG
5215 template
5216 class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
5217 #endif
5218
5219 #ifdef HAVE_TARGET_64_LITTLE
5220 template
5221 class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
5222 #endif
5223
5224 #ifdef HAVE_TARGET_64_BIG
5225 template
5226 class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
5227 #endif
5228
5229 #ifdef HAVE_TARGET_32_LITTLE
5230 template
5231 class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
5232 #endif
5233
5234 #ifdef HAVE_TARGET_32_BIG
5235 template
5236 class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
5237 #endif
5238
5239 #ifdef HAVE_TARGET_64_LITTLE
5240 template
5241 class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
5242 #endif
5243
5244 #ifdef HAVE_TARGET_64_BIG
5245 template
5246 class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
5247 #endif
5248
5249 #ifdef HAVE_TARGET_32_LITTLE
5250 template
5251 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
5252 #endif
5253
5254 #ifdef HAVE_TARGET_32_BIG
5255 template
5256 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
5257 #endif
5258
5259 #ifdef HAVE_TARGET_64_LITTLE
5260 template
5261 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
5262 #endif
5263
5264 #ifdef HAVE_TARGET_64_BIG
5265 template
5266 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
5267 #endif
5268
5269 #ifdef HAVE_TARGET_32_LITTLE
5270 template
5271 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
5272 #endif
5273
5274 #ifdef HAVE_TARGET_32_BIG
5275 template
5276 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
5277 #endif
5278
5279 #ifdef HAVE_TARGET_64_LITTLE
5280 template
5281 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
5282 #endif
5283
5284 #ifdef HAVE_TARGET_64_BIG
5285 template
5286 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
5287 #endif
5288
5289 #ifdef HAVE_TARGET_32_LITTLE
5290 template
5291 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
5292 #endif
5293
5294 #ifdef HAVE_TARGET_32_BIG
5295 template
5296 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
5297 #endif
5298
5299 #ifdef HAVE_TARGET_64_LITTLE
5300 template
5301 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
5302 #endif
5303
5304 #ifdef HAVE_TARGET_64_BIG
5305 template
5306 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
5307 #endif
5308
5309 #ifdef HAVE_TARGET_32_LITTLE
5310 template
5311 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
5312 #endif
5313
5314 #ifdef HAVE_TARGET_32_BIG
5315 template
5316 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
5317 #endif
5318
5319 #ifdef HAVE_TARGET_64_LITTLE
5320 template
5321 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
5322 #endif
5323
5324 #ifdef HAVE_TARGET_64_BIG
5325 template
5326 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
5327 #endif
5328
5329 #ifdef HAVE_TARGET_32_LITTLE
5330 template
5331 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
5332 #endif
5333
5334 #ifdef HAVE_TARGET_32_BIG
5335 template
5336 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
5337 #endif
5338
5339 #ifdef HAVE_TARGET_64_LITTLE
5340 template
5341 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
5342 #endif
5343
5344 #ifdef HAVE_TARGET_64_BIG
5345 template
5346 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
5347 #endif
5348
5349 #ifdef HAVE_TARGET_32_LITTLE
5350 template
5351 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
5352 #endif
5353
5354 #ifdef HAVE_TARGET_32_BIG
5355 template
5356 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
5357 #endif
5358
5359 #ifdef HAVE_TARGET_64_LITTLE
5360 template
5361 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
5362 #endif
5363
5364 #ifdef HAVE_TARGET_64_BIG
5365 template
5366 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
5367 #endif
5368
5369 #ifdef HAVE_TARGET_32_LITTLE
5370 template
5371 class Output_data_group<32, false>;
5372 #endif
5373
5374 #ifdef HAVE_TARGET_32_BIG
5375 template
5376 class Output_data_group<32, true>;
5377 #endif
5378
5379 #ifdef HAVE_TARGET_64_LITTLE
5380 template
5381 class Output_data_group<64, false>;
5382 #endif
5383
5384 #ifdef HAVE_TARGET_64_BIG
5385 template
5386 class Output_data_group<64, true>;
5387 #endif
5388
5389 #ifdef HAVE_TARGET_32_LITTLE
5390 template
5391 class Output_data_got<32, false>;
5392 #endif
5393
5394 #ifdef HAVE_TARGET_32_BIG
5395 template
5396 class Output_data_got<32, true>;
5397 #endif
5398
5399 #ifdef HAVE_TARGET_64_LITTLE
5400 template
5401 class Output_data_got<64, false>;
5402 #endif
5403
5404 #ifdef HAVE_TARGET_64_BIG
5405 template
5406 class Output_data_got<64, true>;
5407 #endif
5408
5409 } // End namespace gold.
This page took 0.133231 seconds and 5 git commands to generate.