From Craig Silverstein: Have Parameters point to General_options.
[deliverable/binutils-gdb.git] / gold / output.cc
1 // output.cc -- manage the output file for gold
2
3 // Copyright 2006, 2007 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 <cerrno>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <algorithm>
32 #include "libiberty.h" // for unlink_if_ordinary()
33
34 #include "parameters.h"
35 #include "object.h"
36 #include "symtab.h"
37 #include "reloc.h"
38 #include "merge.h"
39 #include "output.h"
40
41 // Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
42 #ifndef MAP_ANONYMOUS
43 # define MAP_ANONYMOUS MAP_ANON
44 #endif
45
46 namespace gold
47 {
48
49 // Output_data variables.
50
51 bool Output_data::allocated_sizes_are_fixed;
52
53 // Output_data methods.
54
55 Output_data::~Output_data()
56 {
57 }
58
59 // Return the default alignment for the target size.
60
61 uint64_t
62 Output_data::default_alignment()
63 {
64 return Output_data::default_alignment_for_size(
65 parameters->target().get_size());
66 }
67
68 // Return the default alignment for a size--32 or 64.
69
70 uint64_t
71 Output_data::default_alignment_for_size(int size)
72 {
73 if (size == 32)
74 return 4;
75 else if (size == 64)
76 return 8;
77 else
78 gold_unreachable();
79 }
80
81 // Output_section_header methods. This currently assumes that the
82 // segment and section lists are complete at construction time.
83
84 Output_section_headers::Output_section_headers(
85 const Layout* layout,
86 const Layout::Segment_list* segment_list,
87 const Layout::Section_list* section_list,
88 const Layout::Section_list* unattached_section_list,
89 const Stringpool* secnamepool)
90 : layout_(layout),
91 segment_list_(segment_list),
92 section_list_(section_list),
93 unattached_section_list_(unattached_section_list),
94 secnamepool_(secnamepool)
95 {
96 // Count all the sections. Start with 1 for the null section.
97 off_t count = 1;
98 if (!parameters->options().relocatable())
99 {
100 for (Layout::Segment_list::const_iterator p = segment_list->begin();
101 p != segment_list->end();
102 ++p)
103 if ((*p)->type() == elfcpp::PT_LOAD)
104 count += (*p)->output_section_count();
105 }
106 else
107 {
108 for (Layout::Section_list::const_iterator p = section_list->begin();
109 p != section_list->end();
110 ++p)
111 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
112 ++count;
113 }
114 count += unattached_section_list->size();
115
116 const int size = parameters->target().get_size();
117 int shdr_size;
118 if (size == 32)
119 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
120 else if (size == 64)
121 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
122 else
123 gold_unreachable();
124
125 this->set_data_size(count * shdr_size);
126 }
127
128 // Write out the section headers.
129
130 void
131 Output_section_headers::do_write(Output_file* of)
132 {
133 switch (parameters->size_and_endianness())
134 {
135 #ifdef HAVE_TARGET_32_LITTLE
136 case Parameters::TARGET_32_LITTLE:
137 this->do_sized_write<32, false>(of);
138 break;
139 #endif
140 #ifdef HAVE_TARGET_32_BIG
141 case Parameters::TARGET_32_BIG:
142 this->do_sized_write<32, true>(of);
143 break;
144 #endif
145 #ifdef HAVE_TARGET_64_LITTLE
146 case Parameters::TARGET_64_LITTLE:
147 this->do_sized_write<64, false>(of);
148 break;
149 #endif
150 #ifdef HAVE_TARGET_64_BIG
151 case Parameters::TARGET_64_BIG:
152 this->do_sized_write<64, true>(of);
153 break;
154 #endif
155 default:
156 gold_unreachable();
157 }
158 }
159
160 template<int size, bool big_endian>
161 void
162 Output_section_headers::do_sized_write(Output_file* of)
163 {
164 off_t all_shdrs_size = this->data_size();
165 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
166
167 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
168 unsigned char* v = view;
169
170 {
171 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
172 oshdr.put_sh_name(0);
173 oshdr.put_sh_type(elfcpp::SHT_NULL);
174 oshdr.put_sh_flags(0);
175 oshdr.put_sh_addr(0);
176 oshdr.put_sh_offset(0);
177 oshdr.put_sh_size(0);
178 oshdr.put_sh_link(0);
179 oshdr.put_sh_info(0);
180 oshdr.put_sh_addralign(0);
181 oshdr.put_sh_entsize(0);
182 }
183
184 v += shdr_size;
185
186 unsigned int shndx = 1;
187 if (!parameters->options().relocatable())
188 {
189 for (Layout::Segment_list::const_iterator p =
190 this->segment_list_->begin();
191 p != this->segment_list_->end();
192 ++p)
193 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
194 this->secnamepool_,
195 v,
196 &shndx);
197 }
198 else
199 {
200 for (Layout::Section_list::const_iterator p =
201 this->section_list_->begin();
202 p != this->section_list_->end();
203 ++p)
204 {
205 // We do unallocated sections below, except that group
206 // sections have to come first.
207 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
208 && (*p)->type() != elfcpp::SHT_GROUP)
209 continue;
210 gold_assert(shndx == (*p)->out_shndx());
211 elfcpp::Shdr_write<size, big_endian> oshdr(v);
212 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
213 v += shdr_size;
214 ++shndx;
215 }
216 }
217
218 for (Layout::Section_list::const_iterator p =
219 this->unattached_section_list_->begin();
220 p != this->unattached_section_list_->end();
221 ++p)
222 {
223 // For a relocatable link, we did unallocated group sections
224 // above, since they have to come first.
225 if ((*p)->type() == elfcpp::SHT_GROUP
226 && parameters->options().relocatable())
227 continue;
228 gold_assert(shndx == (*p)->out_shndx());
229 elfcpp::Shdr_write<size, big_endian> oshdr(v);
230 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
231 v += shdr_size;
232 ++shndx;
233 }
234
235 of->write_output_view(this->offset(), all_shdrs_size, view);
236 }
237
238 // Output_segment_header methods.
239
240 Output_segment_headers::Output_segment_headers(
241 const Layout::Segment_list& segment_list)
242 : segment_list_(segment_list)
243 {
244 const int size = parameters->target().get_size();
245 int phdr_size;
246 if (size == 32)
247 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
248 else if (size == 64)
249 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
250 else
251 gold_unreachable();
252
253 this->set_data_size(segment_list.size() * phdr_size);
254 }
255
256 void
257 Output_segment_headers::do_write(Output_file* of)
258 {
259 switch (parameters->size_and_endianness())
260 {
261 #ifdef HAVE_TARGET_32_LITTLE
262 case Parameters::TARGET_32_LITTLE:
263 this->do_sized_write<32, false>(of);
264 break;
265 #endif
266 #ifdef HAVE_TARGET_32_BIG
267 case Parameters::TARGET_32_BIG:
268 this->do_sized_write<32, true>(of);
269 break;
270 #endif
271 #ifdef HAVE_TARGET_64_LITTLE
272 case Parameters::TARGET_64_LITTLE:
273 this->do_sized_write<64, false>(of);
274 break;
275 #endif
276 #ifdef HAVE_TARGET_64_BIG
277 case Parameters::TARGET_64_BIG:
278 this->do_sized_write<64, true>(of);
279 break;
280 #endif
281 default:
282 gold_unreachable();
283 }
284 }
285
286 template<int size, bool big_endian>
287 void
288 Output_segment_headers::do_sized_write(Output_file* of)
289 {
290 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
291 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
292 gold_assert(all_phdrs_size == this->data_size());
293 unsigned char* view = of->get_output_view(this->offset(),
294 all_phdrs_size);
295 unsigned char* v = view;
296 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
297 p != this->segment_list_.end();
298 ++p)
299 {
300 elfcpp::Phdr_write<size, big_endian> ophdr(v);
301 (*p)->write_header(&ophdr);
302 v += phdr_size;
303 }
304
305 gold_assert(v - view == all_phdrs_size);
306
307 of->write_output_view(this->offset(), all_phdrs_size, view);
308 }
309
310 // Output_file_header methods.
311
312 Output_file_header::Output_file_header(const Target* target,
313 const Symbol_table* symtab,
314 const Output_segment_headers* osh,
315 const char* entry)
316 : target_(target),
317 symtab_(symtab),
318 segment_header_(osh),
319 section_header_(NULL),
320 shstrtab_(NULL),
321 entry_(entry)
322 {
323 const int size = parameters->target().get_size();
324 int ehdr_size;
325 if (size == 32)
326 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
327 else if (size == 64)
328 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
329 else
330 gold_unreachable();
331
332 this->set_data_size(ehdr_size);
333 }
334
335 // Set the section table information for a file header.
336
337 void
338 Output_file_header::set_section_info(const Output_section_headers* shdrs,
339 const Output_section* shstrtab)
340 {
341 this->section_header_ = shdrs;
342 this->shstrtab_ = shstrtab;
343 }
344
345 // Write out the file header.
346
347 void
348 Output_file_header::do_write(Output_file* of)
349 {
350 gold_assert(this->offset() == 0);
351
352 switch (parameters->size_and_endianness())
353 {
354 #ifdef HAVE_TARGET_32_LITTLE
355 case Parameters::TARGET_32_LITTLE:
356 this->do_sized_write<32, false>(of);
357 break;
358 #endif
359 #ifdef HAVE_TARGET_32_BIG
360 case Parameters::TARGET_32_BIG:
361 this->do_sized_write<32, true>(of);
362 break;
363 #endif
364 #ifdef HAVE_TARGET_64_LITTLE
365 case Parameters::TARGET_64_LITTLE:
366 this->do_sized_write<64, false>(of);
367 break;
368 #endif
369 #ifdef HAVE_TARGET_64_BIG
370 case Parameters::TARGET_64_BIG:
371 this->do_sized_write<64, true>(of);
372 break;
373 #endif
374 default:
375 gold_unreachable();
376 }
377 }
378
379 // Write out the file header with appropriate size and endianess.
380
381 template<int size, bool big_endian>
382 void
383 Output_file_header::do_sized_write(Output_file* of)
384 {
385 gold_assert(this->offset() == 0);
386
387 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
388 unsigned char* view = of->get_output_view(0, ehdr_size);
389 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
390
391 unsigned char e_ident[elfcpp::EI_NIDENT];
392 memset(e_ident, 0, elfcpp::EI_NIDENT);
393 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
394 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
395 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
396 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
397 if (size == 32)
398 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
399 else if (size == 64)
400 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
401 else
402 gold_unreachable();
403 e_ident[elfcpp::EI_DATA] = (big_endian
404 ? elfcpp::ELFDATA2MSB
405 : elfcpp::ELFDATA2LSB);
406 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
407 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
408 oehdr.put_e_ident(e_ident);
409
410 elfcpp::ET e_type;
411 if (parameters->options().relocatable())
412 e_type = elfcpp::ET_REL;
413 else if (parameters->options().shared())
414 e_type = elfcpp::ET_DYN;
415 else
416 e_type = elfcpp::ET_EXEC;
417 oehdr.put_e_type(e_type);
418
419 oehdr.put_e_machine(this->target_->machine_code());
420 oehdr.put_e_version(elfcpp::EV_CURRENT);
421
422 oehdr.put_e_entry(this->entry<size>());
423
424 if (this->segment_header_ == NULL)
425 oehdr.put_e_phoff(0);
426 else
427 oehdr.put_e_phoff(this->segment_header_->offset());
428
429 oehdr.put_e_shoff(this->section_header_->offset());
430
431 // FIXME: The target needs to set the flags.
432 oehdr.put_e_flags(0);
433
434 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
435
436 if (this->segment_header_ == NULL)
437 {
438 oehdr.put_e_phentsize(0);
439 oehdr.put_e_phnum(0);
440 }
441 else
442 {
443 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
444 oehdr.put_e_phnum(this->segment_header_->data_size()
445 / elfcpp::Elf_sizes<size>::phdr_size);
446 }
447
448 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
449 oehdr.put_e_shnum(this->section_header_->data_size()
450 / elfcpp::Elf_sizes<size>::shdr_size);
451 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
452
453 of->write_output_view(0, ehdr_size, view);
454 }
455
456 // Return the value to use for the entry address. THIS->ENTRY_ is the
457 // symbol specified on the command line, if any.
458
459 template<int size>
460 typename elfcpp::Elf_types<size>::Elf_Addr
461 Output_file_header::entry()
462 {
463 const bool should_issue_warning = (this->entry_ != NULL
464 && !parameters->options().relocatable()
465 && !parameters->options().shared());
466
467 // FIXME: Need to support target specific entry symbol.
468 const char* entry = this->entry_;
469 if (entry == NULL)
470 entry = "_start";
471
472 Symbol* sym = this->symtab_->lookup(entry);
473
474 typename Sized_symbol<size>::Value_type v;
475 if (sym != NULL)
476 {
477 Sized_symbol<size>* ssym;
478 ssym = this->symtab_->get_sized_symbol<size>(sym);
479 if (!ssym->is_defined() && should_issue_warning)
480 gold_warning("entry symbol '%s' exists but is not defined", entry);
481 v = ssym->value();
482 }
483 else
484 {
485 // We couldn't find the entry symbol. See if we can parse it as
486 // a number. This supports, e.g., -e 0x1000.
487 char* endptr;
488 v = strtoull(entry, &endptr, 0);
489 if (*endptr != '\0')
490 {
491 if (should_issue_warning)
492 gold_warning("cannot find entry symbol '%s'", entry);
493 v = 0;
494 }
495 }
496
497 return v;
498 }
499
500 // Output_data_const methods.
501
502 void
503 Output_data_const::do_write(Output_file* of)
504 {
505 of->write(this->offset(), this->data_.data(), this->data_.size());
506 }
507
508 // Output_data_const_buffer methods.
509
510 void
511 Output_data_const_buffer::do_write(Output_file* of)
512 {
513 of->write(this->offset(), this->p_, this->data_size());
514 }
515
516 // Output_section_data methods.
517
518 // Record the output section, and set the entry size and such.
519
520 void
521 Output_section_data::set_output_section(Output_section* os)
522 {
523 gold_assert(this->output_section_ == NULL);
524 this->output_section_ = os;
525 this->do_adjust_output_section(os);
526 }
527
528 // Return the section index of the output section.
529
530 unsigned int
531 Output_section_data::do_out_shndx() const
532 {
533 gold_assert(this->output_section_ != NULL);
534 return this->output_section_->out_shndx();
535 }
536
537 // Output_data_strtab methods.
538
539 // Set the final data size.
540
541 void
542 Output_data_strtab::set_final_data_size()
543 {
544 this->strtab_->set_string_offsets();
545 this->set_data_size(this->strtab_->get_strtab_size());
546 }
547
548 // Write out a string table.
549
550 void
551 Output_data_strtab::do_write(Output_file* of)
552 {
553 this->strtab_->write(of, this->offset());
554 }
555
556 // Output_reloc methods.
557
558 // A reloc against a global symbol.
559
560 template<bool dynamic, int size, bool big_endian>
561 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
562 Symbol* gsym,
563 unsigned int type,
564 Output_data* od,
565 Address address,
566 bool is_relative)
567 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
568 is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
569 {
570 // this->type_ is a bitfield; make sure TYPE fits.
571 gold_assert(this->type_ == type);
572 this->u1_.gsym = gsym;
573 this->u2_.od = od;
574 if (dynamic)
575 this->set_needs_dynsym_index();
576 }
577
578 template<bool dynamic, int size, bool big_endian>
579 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
580 Symbol* gsym,
581 unsigned int type,
582 Relobj* relobj,
583 unsigned int shndx,
584 Address address,
585 bool is_relative)
586 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
587 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
588 {
589 gold_assert(shndx != INVALID_CODE);
590 // this->type_ is a bitfield; make sure TYPE fits.
591 gold_assert(this->type_ == type);
592 this->u1_.gsym = gsym;
593 this->u2_.relobj = relobj;
594 if (dynamic)
595 this->set_needs_dynsym_index();
596 }
597
598 // A reloc against a local symbol.
599
600 template<bool dynamic, int size, bool big_endian>
601 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
602 Sized_relobj<size, big_endian>* relobj,
603 unsigned int local_sym_index,
604 unsigned int type,
605 Output_data* od,
606 Address address,
607 bool is_relative,
608 bool is_section_symbol)
609 : address_(address), local_sym_index_(local_sym_index), type_(type),
610 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
611 shndx_(INVALID_CODE)
612 {
613 gold_assert(local_sym_index != GSYM_CODE
614 && local_sym_index != INVALID_CODE);
615 // this->type_ is a bitfield; make sure TYPE fits.
616 gold_assert(this->type_ == type);
617 this->u1_.relobj = relobj;
618 this->u2_.od = od;
619 if (dynamic)
620 this->set_needs_dynsym_index();
621 }
622
623 template<bool dynamic, int size, bool big_endian>
624 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
625 Sized_relobj<size, big_endian>* relobj,
626 unsigned int local_sym_index,
627 unsigned int type,
628 unsigned int shndx,
629 Address address,
630 bool is_relative,
631 bool is_section_symbol)
632 : address_(address), local_sym_index_(local_sym_index), type_(type),
633 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
634 shndx_(shndx)
635 {
636 gold_assert(local_sym_index != GSYM_CODE
637 && local_sym_index != INVALID_CODE);
638 gold_assert(shndx != INVALID_CODE);
639 // this->type_ is a bitfield; make sure TYPE fits.
640 gold_assert(this->type_ == type);
641 this->u1_.relobj = relobj;
642 this->u2_.relobj = relobj;
643 if (dynamic)
644 this->set_needs_dynsym_index();
645 }
646
647 // A reloc against the STT_SECTION symbol of an output section.
648
649 template<bool dynamic, int size, bool big_endian>
650 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
651 Output_section* os,
652 unsigned int type,
653 Output_data* od,
654 Address address)
655 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
656 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
657 {
658 // this->type_ is a bitfield; make sure TYPE fits.
659 gold_assert(this->type_ == type);
660 this->u1_.os = os;
661 this->u2_.od = od;
662 if (dynamic)
663 this->set_needs_dynsym_index();
664 else
665 os->set_needs_symtab_index();
666 }
667
668 template<bool dynamic, int size, bool big_endian>
669 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
670 Output_section* os,
671 unsigned int type,
672 Relobj* relobj,
673 unsigned int shndx,
674 Address address)
675 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
676 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
677 {
678 gold_assert(shndx != INVALID_CODE);
679 // this->type_ is a bitfield; make sure TYPE fits.
680 gold_assert(this->type_ == type);
681 this->u1_.os = os;
682 this->u2_.relobj = relobj;
683 if (dynamic)
684 this->set_needs_dynsym_index();
685 else
686 os->set_needs_symtab_index();
687 }
688
689 // Record that we need a dynamic symbol index for this relocation.
690
691 template<bool dynamic, int size, bool big_endian>
692 void
693 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
694 set_needs_dynsym_index()
695 {
696 if (this->is_relative_)
697 return;
698 switch (this->local_sym_index_)
699 {
700 case INVALID_CODE:
701 gold_unreachable();
702
703 case GSYM_CODE:
704 this->u1_.gsym->set_needs_dynsym_entry();
705 break;
706
707 case SECTION_CODE:
708 this->u1_.os->set_needs_dynsym_index();
709 break;
710
711 case 0:
712 break;
713
714 default:
715 {
716 const unsigned int lsi = this->local_sym_index_;
717 if (!this->is_section_symbol_)
718 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
719 else
720 {
721 section_offset_type dummy;
722 Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
723 gold_assert(os != NULL);
724 os->set_needs_dynsym_index();
725 }
726 }
727 break;
728 }
729 }
730
731 // Get the symbol index of a relocation.
732
733 template<bool dynamic, int size, bool big_endian>
734 unsigned int
735 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
736 const
737 {
738 unsigned int index;
739 switch (this->local_sym_index_)
740 {
741 case INVALID_CODE:
742 gold_unreachable();
743
744 case GSYM_CODE:
745 if (this->u1_.gsym == NULL)
746 index = 0;
747 else if (dynamic)
748 index = this->u1_.gsym->dynsym_index();
749 else
750 index = this->u1_.gsym->symtab_index();
751 break;
752
753 case SECTION_CODE:
754 if (dynamic)
755 index = this->u1_.os->dynsym_index();
756 else
757 index = this->u1_.os->symtab_index();
758 break;
759
760 case 0:
761 // Relocations without symbols use a symbol index of 0.
762 index = 0;
763 break;
764
765 default:
766 {
767 const unsigned int lsi = this->local_sym_index_;
768 if (!this->is_section_symbol_)
769 {
770 if (dynamic)
771 index = this->u1_.relobj->dynsym_index(lsi);
772 else
773 index = this->u1_.relobj->symtab_index(lsi);
774 }
775 else
776 {
777 section_offset_type dummy;
778 Output_section* os = this->u1_.relobj->output_section(lsi, &dummy);
779 gold_assert(os != NULL);
780 if (dynamic)
781 index = os->dynsym_index();
782 else
783 index = os->symtab_index();
784 }
785 }
786 break;
787 }
788 gold_assert(index != -1U);
789 return index;
790 }
791
792 // For a local section symbol, get the section offset of the input
793 // section within the output section.
794
795 template<bool dynamic, int size, bool big_endian>
796 section_offset_type
797 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
798 local_section_offset() const
799 {
800 const unsigned int lsi = this->local_sym_index_;
801 section_offset_type offset;
802 Output_section* os = this->u1_.relobj->output_section(lsi, &offset);
803 gold_assert(os != NULL);
804 return offset;
805 }
806
807 // Write out the offset and info fields of a Rel or Rela relocation
808 // entry.
809
810 template<bool dynamic, int size, bool big_endian>
811 template<typename Write_rel>
812 void
813 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
814 Write_rel* wr) const
815 {
816 Address address = this->address_;
817 if (this->shndx_ != INVALID_CODE)
818 {
819 section_offset_type off;
820 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
821 &off);
822 gold_assert(os != NULL);
823 if (off != -1)
824 address += os->address() + off;
825 else
826 {
827 address = os->output_address(this->u2_.relobj, this->shndx_,
828 address);
829 gold_assert(address != -1U);
830 }
831 }
832 else if (this->u2_.od != NULL)
833 address += this->u2_.od->address();
834 wr->put_r_offset(address);
835 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
836 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
837 }
838
839 // Write out a Rel relocation.
840
841 template<bool dynamic, int size, bool big_endian>
842 void
843 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
844 unsigned char* pov) const
845 {
846 elfcpp::Rel_write<size, big_endian> orel(pov);
847 this->write_rel(&orel);
848 }
849
850 // Get the value of the symbol referred to by a Rel relocation.
851
852 template<bool dynamic, int size, bool big_endian>
853 typename elfcpp::Elf_types<size>::Elf_Addr
854 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value() const
855 {
856 if (this->local_sym_index_ == GSYM_CODE)
857 {
858 const Sized_symbol<size>* sym;
859 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
860 return sym->value();
861 }
862 gold_assert(this->local_sym_index_ != SECTION_CODE
863 && this->local_sym_index_ != INVALID_CODE);
864 const Sized_relobj<size, big_endian>* relobj = this->u1_.relobj;
865 return relobj->local_symbol_value(this->local_sym_index_);
866 }
867
868 // Write out a Rela relocation.
869
870 template<bool dynamic, int size, bool big_endian>
871 void
872 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
873 unsigned char* pov) const
874 {
875 elfcpp::Rela_write<size, big_endian> orel(pov);
876 this->rel_.write_rel(&orel);
877 Addend addend = this->addend_;
878 if (this->rel_.is_relative())
879 addend += this->rel_.symbol_value();
880 if (this->rel_.is_local_section_symbol())
881 addend += this->rel_.local_section_offset();
882 orel.put_r_addend(addend);
883 }
884
885 // Output_data_reloc_base methods.
886
887 // Adjust the output section.
888
889 template<int sh_type, bool dynamic, int size, bool big_endian>
890 void
891 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
892 ::do_adjust_output_section(Output_section* os)
893 {
894 if (sh_type == elfcpp::SHT_REL)
895 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
896 else if (sh_type == elfcpp::SHT_RELA)
897 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
898 else
899 gold_unreachable();
900 if (dynamic)
901 os->set_should_link_to_dynsym();
902 else
903 os->set_should_link_to_symtab();
904 }
905
906 // Write out relocation data.
907
908 template<int sh_type, bool dynamic, int size, bool big_endian>
909 void
910 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
911 Output_file* of)
912 {
913 const off_t off = this->offset();
914 const off_t oview_size = this->data_size();
915 unsigned char* const oview = of->get_output_view(off, oview_size);
916
917 unsigned char* pov = oview;
918 for (typename Relocs::const_iterator p = this->relocs_.begin();
919 p != this->relocs_.end();
920 ++p)
921 {
922 p->write(pov);
923 pov += reloc_size;
924 }
925
926 gold_assert(pov - oview == oview_size);
927
928 of->write_output_view(off, oview_size, oview);
929
930 // We no longer need the relocation entries.
931 this->relocs_.clear();
932 }
933
934 // Class Output_relocatable_relocs.
935
936 template<int sh_type, int size, bool big_endian>
937 void
938 Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
939 {
940 this->set_data_size(this->rr_->output_reloc_count()
941 * Reloc_types<sh_type, size, big_endian>::reloc_size);
942 }
943
944 // class Output_data_group.
945
946 template<int size, bool big_endian>
947 Output_data_group<size, big_endian>::Output_data_group(
948 Sized_relobj<size, big_endian>* relobj,
949 section_size_type entry_count,
950 const elfcpp::Elf_Word* contents)
951 : Output_section_data(entry_count * 4, 4),
952 relobj_(relobj)
953 {
954 this->flags_ = elfcpp::Swap<32, big_endian>::readval(contents);
955 for (section_size_type i = 1; i < entry_count; ++i)
956 {
957 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
958 this->input_sections_.push_back(shndx);
959 }
960 }
961
962 // Write out the section group, which means translating the section
963 // indexes to apply to the output file.
964
965 template<int size, bool big_endian>
966 void
967 Output_data_group<size, big_endian>::do_write(Output_file* of)
968 {
969 const off_t off = this->offset();
970 const section_size_type oview_size =
971 convert_to_section_size_type(this->data_size());
972 unsigned char* const oview = of->get_output_view(off, oview_size);
973
974 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
975 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
976 ++contents;
977
978 for (std::vector<unsigned int>::const_iterator p =
979 this->input_sections_.begin();
980 p != this->input_sections_.end();
981 ++p, ++contents)
982 {
983 section_offset_type dummy;
984 Output_section* os = this->relobj_->output_section(*p, &dummy);
985
986 unsigned int output_shndx;
987 if (os != NULL)
988 output_shndx = os->out_shndx();
989 else
990 {
991 this->relobj_->error(_("section group retained but "
992 "group element discarded"));
993 output_shndx = 0;
994 }
995
996 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
997 }
998
999 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1000 gold_assert(wrote == oview_size);
1001
1002 of->write_output_view(off, oview_size, oview);
1003
1004 // We no longer need this information.
1005 this->input_sections_.clear();
1006 }
1007
1008 // Output_data_got::Got_entry methods.
1009
1010 // Write out the entry.
1011
1012 template<int size, bool big_endian>
1013 void
1014 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1015 {
1016 Valtype val = 0;
1017
1018 switch (this->local_sym_index_)
1019 {
1020 case GSYM_CODE:
1021 {
1022 // If the symbol is resolved locally, we need to write out the
1023 // link-time value, which will be relocated dynamically by a
1024 // RELATIVE relocation.
1025 Symbol* gsym = this->u_.gsym;
1026 Sized_symbol<size>* sgsym;
1027 // This cast is a bit ugly. We don't want to put a
1028 // virtual method in Symbol, because we want Symbol to be
1029 // as small as possible.
1030 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1031 val = sgsym->value();
1032 }
1033 break;
1034
1035 case CONSTANT_CODE:
1036 val = this->u_.constant;
1037 break;
1038
1039 default:
1040 val = this->u_.object->local_symbol_value(this->local_sym_index_);
1041 break;
1042 }
1043
1044 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1045 }
1046
1047 // Output_data_got methods.
1048
1049 // Add an entry for a global symbol to the GOT. This returns true if
1050 // this is a new GOT entry, false if the symbol already had a GOT
1051 // entry.
1052
1053 template<int size, bool big_endian>
1054 bool
1055 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
1056 {
1057 if (gsym->has_got_offset())
1058 return false;
1059
1060 this->entries_.push_back(Got_entry(gsym));
1061 this->set_got_size();
1062 gsym->set_got_offset(this->last_got_offset());
1063 return true;
1064 }
1065
1066 // Add an entry for a global symbol to the GOT, and add a dynamic
1067 // relocation of type R_TYPE for the GOT entry.
1068 template<int size, bool big_endian>
1069 void
1070 Output_data_got<size, big_endian>::add_global_with_rel(
1071 Symbol* gsym,
1072 Rel_dyn* rel_dyn,
1073 unsigned int r_type)
1074 {
1075 if (gsym->has_got_offset())
1076 return;
1077
1078 this->entries_.push_back(Got_entry());
1079 this->set_got_size();
1080 unsigned int got_offset = this->last_got_offset();
1081 gsym->set_got_offset(got_offset);
1082 rel_dyn->add_global(gsym, r_type, this, got_offset);
1083 }
1084
1085 template<int size, bool big_endian>
1086 void
1087 Output_data_got<size, big_endian>::add_global_with_rela(
1088 Symbol* gsym,
1089 Rela_dyn* rela_dyn,
1090 unsigned int r_type)
1091 {
1092 if (gsym->has_got_offset())
1093 return;
1094
1095 this->entries_.push_back(Got_entry());
1096 this->set_got_size();
1097 unsigned int got_offset = this->last_got_offset();
1098 gsym->set_got_offset(got_offset);
1099 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1100 }
1101
1102 // Add an entry for a local symbol to the GOT. This returns true if
1103 // this is a new GOT entry, false if the symbol already has a GOT
1104 // entry.
1105
1106 template<int size, bool big_endian>
1107 bool
1108 Output_data_got<size, big_endian>::add_local(
1109 Sized_relobj<size, big_endian>* object,
1110 unsigned int symndx)
1111 {
1112 if (object->local_has_got_offset(symndx))
1113 return false;
1114
1115 this->entries_.push_back(Got_entry(object, symndx));
1116 this->set_got_size();
1117 object->set_local_got_offset(symndx, this->last_got_offset());
1118 return true;
1119 }
1120
1121 // Add an entry for a local symbol to the GOT, and add a dynamic
1122 // relocation of type R_TYPE for the GOT entry.
1123 template<int size, bool big_endian>
1124 void
1125 Output_data_got<size, big_endian>::add_local_with_rel(
1126 Sized_relobj<size, big_endian>* object,
1127 unsigned int symndx,
1128 Rel_dyn* rel_dyn,
1129 unsigned int r_type)
1130 {
1131 if (object->local_has_got_offset(symndx))
1132 return;
1133
1134 this->entries_.push_back(Got_entry());
1135 this->set_got_size();
1136 unsigned int got_offset = this->last_got_offset();
1137 object->set_local_got_offset(symndx, got_offset);
1138 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1139 }
1140
1141 template<int size, bool big_endian>
1142 void
1143 Output_data_got<size, big_endian>::add_local_with_rela(
1144 Sized_relobj<size, big_endian>* object,
1145 unsigned int symndx,
1146 Rela_dyn* rela_dyn,
1147 unsigned int r_type)
1148 {
1149 if (object->local_has_got_offset(symndx))
1150 return;
1151
1152 this->entries_.push_back(Got_entry());
1153 this->set_got_size();
1154 unsigned int got_offset = this->last_got_offset();
1155 object->set_local_got_offset(symndx, got_offset);
1156 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1157 }
1158
1159 // Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
1160 // In a pair of entries, the first value in the pair will be used for the
1161 // module index, and the second value will be used for the dtv-relative
1162 // offset. This returns true if this is a new GOT entry, false if the symbol
1163 // already has a GOT entry.
1164
1165 template<int size, bool big_endian>
1166 bool
1167 Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym, bool need_pair)
1168 {
1169 if (gsym->has_tls_got_offset(need_pair))
1170 return false;
1171
1172 this->entries_.push_back(Got_entry(gsym));
1173 gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
1174 if (need_pair)
1175 this->entries_.push_back(Got_entry(gsym));
1176 this->set_got_size();
1177 return true;
1178 }
1179
1180 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1181 // relocation of type R_TYPE.
1182 template<int size, bool big_endian>
1183 void
1184 Output_data_got<size, big_endian>::add_global_tls_with_rel(
1185 Symbol* gsym,
1186 Rel_dyn* rel_dyn,
1187 unsigned int r_type)
1188 {
1189 if (gsym->has_tls_got_offset(false))
1190 return;
1191
1192 this->entries_.push_back(Got_entry());
1193 this->set_got_size();
1194 unsigned int got_offset = this->last_got_offset();
1195 gsym->set_tls_got_offset(got_offset, false);
1196 rel_dyn->add_global(gsym, r_type, this, got_offset);
1197 }
1198
1199 template<int size, bool big_endian>
1200 void
1201 Output_data_got<size, big_endian>::add_global_tls_with_rela(
1202 Symbol* gsym,
1203 Rela_dyn* rela_dyn,
1204 unsigned int r_type)
1205 {
1206 if (gsym->has_tls_got_offset(false))
1207 return;
1208
1209 this->entries_.push_back(Got_entry());
1210 this->set_got_size();
1211 unsigned int got_offset = this->last_got_offset();
1212 gsym->set_tls_got_offset(got_offset, false);
1213 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1214 }
1215
1216 // Add a pair of entries for a global TLS symbol to the GOT, and add
1217 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1218 template<int size, bool big_endian>
1219 void
1220 Output_data_got<size, big_endian>::add_global_tls_with_rel(
1221 Symbol* gsym,
1222 Rel_dyn* rel_dyn,
1223 unsigned int mod_r_type,
1224 unsigned int dtv_r_type)
1225 {
1226 if (gsym->has_tls_got_offset(true))
1227 return;
1228
1229 this->entries_.push_back(Got_entry());
1230 unsigned int got_offset = this->last_got_offset();
1231 gsym->set_tls_got_offset(got_offset, true);
1232 rel_dyn->add_global(gsym, mod_r_type, this, got_offset);
1233
1234 this->entries_.push_back(Got_entry());
1235 this->set_got_size();
1236 got_offset = this->last_got_offset();
1237 rel_dyn->add_global(gsym, dtv_r_type, this, got_offset);
1238 }
1239
1240 template<int size, bool big_endian>
1241 void
1242 Output_data_got<size, big_endian>::add_global_tls_with_rela(
1243 Symbol* gsym,
1244 Rela_dyn* rela_dyn,
1245 unsigned int mod_r_type,
1246 unsigned int dtv_r_type)
1247 {
1248 if (gsym->has_tls_got_offset(true))
1249 return;
1250
1251 this->entries_.push_back(Got_entry());
1252 unsigned int got_offset = this->last_got_offset();
1253 gsym->set_tls_got_offset(got_offset, true);
1254 rela_dyn->add_global(gsym, mod_r_type, this, got_offset, 0);
1255
1256 this->entries_.push_back(Got_entry());
1257 this->set_got_size();
1258 got_offset = this->last_got_offset();
1259 rela_dyn->add_global(gsym, dtv_r_type, this, got_offset, 0);
1260 }
1261
1262 // Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1263 // In a pair of entries, the first value in the pair will be used for the
1264 // module index, and the second value will be used for the dtv-relative
1265 // offset. This returns true if this is a new GOT entry, false if the symbol
1266 // already has a GOT entry.
1267
1268 template<int size, bool big_endian>
1269 bool
1270 Output_data_got<size, big_endian>::add_local_tls(
1271 Sized_relobj<size, big_endian>* object,
1272 unsigned int symndx,
1273 bool need_pair)
1274 {
1275 if (object->local_has_tls_got_offset(symndx, need_pair))
1276 return false;
1277
1278 this->entries_.push_back(Got_entry(object, symndx));
1279 object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
1280 if (need_pair)
1281 this->entries_.push_back(Got_entry(object, symndx));
1282 this->set_got_size();
1283 return true;
1284 }
1285
1286 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1287 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1288 // Because this is a local symbol, the first GOT entry can be relocated
1289 // relative to a section symbol, and the second GOT entry will have an
1290 // dtv-relative value that can be computed at link time.
1291 template<int size, bool big_endian>
1292 void
1293 Output_data_got<size, big_endian>::add_local_tls_with_rel(
1294 Sized_relobj<size, big_endian>* object,
1295 unsigned int symndx,
1296 unsigned int shndx,
1297 bool need_pair,
1298 Rel_dyn* rel_dyn,
1299 unsigned int r_type)
1300 {
1301 if (object->local_has_tls_got_offset(symndx, need_pair))
1302 return;
1303
1304 this->entries_.push_back(Got_entry());
1305 unsigned int got_offset = this->last_got_offset();
1306 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1307 section_offset_type off;
1308 Output_section* os = object->output_section(shndx, &off);
1309 rel_dyn->add_output_section(os, r_type, this, got_offset);
1310
1311 // The second entry of the pair will be statically initialized
1312 // with the TLS offset of the symbol.
1313 if (need_pair)
1314 this->entries_.push_back(Got_entry(object, symndx));
1315
1316 this->set_got_size();
1317 }
1318
1319 template<int size, bool big_endian>
1320 void
1321 Output_data_got<size, big_endian>::add_local_tls_with_rela(
1322 Sized_relobj<size, big_endian>* object,
1323 unsigned int symndx,
1324 unsigned int shndx,
1325 bool need_pair,
1326 Rela_dyn* rela_dyn,
1327 unsigned int r_type)
1328 {
1329 if (object->local_has_tls_got_offset(symndx, need_pair))
1330 return;
1331
1332 this->entries_.push_back(Got_entry());
1333 unsigned int got_offset = this->last_got_offset();
1334 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
1335 section_offset_type off;
1336 Output_section* os = object->output_section(shndx, &off);
1337 rela_dyn->add_output_section(os, r_type, this, got_offset, 0);
1338
1339 // The second entry of the pair will be statically initialized
1340 // with the TLS offset of the symbol.
1341 if (need_pair)
1342 this->entries_.push_back(Got_entry(object, symndx));
1343
1344 this->set_got_size();
1345 }
1346
1347 // Write out the GOT.
1348
1349 template<int size, bool big_endian>
1350 void
1351 Output_data_got<size, big_endian>::do_write(Output_file* of)
1352 {
1353 const int add = size / 8;
1354
1355 const off_t off = this->offset();
1356 const off_t oview_size = this->data_size();
1357 unsigned char* const oview = of->get_output_view(off, oview_size);
1358
1359 unsigned char* pov = oview;
1360 for (typename Got_entries::const_iterator p = this->entries_.begin();
1361 p != this->entries_.end();
1362 ++p)
1363 {
1364 p->write(pov);
1365 pov += add;
1366 }
1367
1368 gold_assert(pov - oview == oview_size);
1369
1370 of->write_output_view(off, oview_size, oview);
1371
1372 // We no longer need the GOT entries.
1373 this->entries_.clear();
1374 }
1375
1376 // Output_data_dynamic::Dynamic_entry methods.
1377
1378 // Write out the entry.
1379
1380 template<int size, bool big_endian>
1381 void
1382 Output_data_dynamic::Dynamic_entry::write(
1383 unsigned char* pov,
1384 const Stringpool* pool
1385 ACCEPT_SIZE_ENDIAN) const
1386 {
1387 typename elfcpp::Elf_types<size>::Elf_WXword val;
1388 switch (this->classification_)
1389 {
1390 case DYNAMIC_NUMBER:
1391 val = this->u_.val;
1392 break;
1393
1394 case DYNAMIC_SECTION_ADDRESS:
1395 val = this->u_.od->address();
1396 break;
1397
1398 case DYNAMIC_SECTION_SIZE:
1399 val = this->u_.od->data_size();
1400 break;
1401
1402 case DYNAMIC_SYMBOL:
1403 {
1404 const Sized_symbol<size>* s =
1405 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1406 val = s->value();
1407 }
1408 break;
1409
1410 case DYNAMIC_STRING:
1411 val = pool->get_offset(this->u_.str);
1412 break;
1413
1414 default:
1415 gold_unreachable();
1416 }
1417
1418 elfcpp::Dyn_write<size, big_endian> dw(pov);
1419 dw.put_d_tag(this->tag_);
1420 dw.put_d_val(val);
1421 }
1422
1423 // Output_data_dynamic methods.
1424
1425 // Adjust the output section to set the entry size.
1426
1427 void
1428 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1429 {
1430 if (parameters->target().get_size() == 32)
1431 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1432 else if (parameters->target().get_size() == 64)
1433 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1434 else
1435 gold_unreachable();
1436 }
1437
1438 // Set the final data size.
1439
1440 void
1441 Output_data_dynamic::set_final_data_size()
1442 {
1443 // Add the terminating entry.
1444 this->add_constant(elfcpp::DT_NULL, 0);
1445
1446 int dyn_size;
1447 if (parameters->target().get_size() == 32)
1448 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1449 else if (parameters->target().get_size() == 64)
1450 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1451 else
1452 gold_unreachable();
1453 this->set_data_size(this->entries_.size() * dyn_size);
1454 }
1455
1456 // Write out the dynamic entries.
1457
1458 void
1459 Output_data_dynamic::do_write(Output_file* of)
1460 {
1461 switch (parameters->size_and_endianness())
1462 {
1463 #ifdef HAVE_TARGET_32_LITTLE
1464 case Parameters::TARGET_32_LITTLE:
1465 this->sized_write<32, false>(of);
1466 break;
1467 #endif
1468 #ifdef HAVE_TARGET_32_BIG
1469 case Parameters::TARGET_32_BIG:
1470 this->sized_write<32, true>(of);
1471 break;
1472 #endif
1473 #ifdef HAVE_TARGET_64_LITTLE
1474 case Parameters::TARGET_64_LITTLE:
1475 this->sized_write<64, false>(of);
1476 break;
1477 #endif
1478 #ifdef HAVE_TARGET_64_BIG
1479 case Parameters::TARGET_64_BIG:
1480 this->sized_write<64, true>(of);
1481 break;
1482 #endif
1483 default:
1484 gold_unreachable();
1485 }
1486 }
1487
1488 template<int size, bool big_endian>
1489 void
1490 Output_data_dynamic::sized_write(Output_file* of)
1491 {
1492 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1493
1494 const off_t offset = this->offset();
1495 const off_t oview_size = this->data_size();
1496 unsigned char* const oview = of->get_output_view(offset, oview_size);
1497
1498 unsigned char* pov = oview;
1499 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1500 p != this->entries_.end();
1501 ++p)
1502 {
1503 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1504 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
1505 pov += dyn_size;
1506 }
1507
1508 gold_assert(pov - oview == oview_size);
1509
1510 of->write_output_view(offset, oview_size, oview);
1511
1512 // We no longer need the dynamic entries.
1513 this->entries_.clear();
1514 }
1515
1516 // Output_section::Input_section methods.
1517
1518 // Return the data size. For an input section we store the size here.
1519 // For an Output_section_data, we have to ask it for the size.
1520
1521 off_t
1522 Output_section::Input_section::data_size() const
1523 {
1524 if (this->is_input_section())
1525 return this->u1_.data_size;
1526 else
1527 return this->u2_.posd->data_size();
1528 }
1529
1530 // Set the address and file offset.
1531
1532 void
1533 Output_section::Input_section::set_address_and_file_offset(
1534 uint64_t address,
1535 off_t file_offset,
1536 off_t section_file_offset)
1537 {
1538 if (this->is_input_section())
1539 this->u2_.object->set_section_offset(this->shndx_,
1540 file_offset - section_file_offset);
1541 else
1542 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1543 }
1544
1545 // Reset the address and file offset.
1546
1547 void
1548 Output_section::Input_section::reset_address_and_file_offset()
1549 {
1550 if (!this->is_input_section())
1551 this->u2_.posd->reset_address_and_file_offset();
1552 }
1553
1554 // Finalize the data size.
1555
1556 void
1557 Output_section::Input_section::finalize_data_size()
1558 {
1559 if (!this->is_input_section())
1560 this->u2_.posd->finalize_data_size();
1561 }
1562
1563 // Try to turn an input offset into an output offset. We want to
1564 // return the output offset relative to the start of this
1565 // Input_section in the output section.
1566
1567 inline bool
1568 Output_section::Input_section::output_offset(
1569 const Relobj* object,
1570 unsigned int shndx,
1571 section_offset_type offset,
1572 section_offset_type *poutput) const
1573 {
1574 if (!this->is_input_section())
1575 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1576 else
1577 {
1578 if (this->shndx_ != shndx || this->u2_.object != object)
1579 return false;
1580 *poutput = offset;
1581 return true;
1582 }
1583 }
1584
1585 // Return whether this is the merge section for the input section
1586 // SHNDX in OBJECT.
1587
1588 inline bool
1589 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1590 unsigned int shndx) const
1591 {
1592 if (this->is_input_section())
1593 return false;
1594 return this->u2_.posd->is_merge_section_for(object, shndx);
1595 }
1596
1597 // Write out the data. We don't have to do anything for an input
1598 // section--they are handled via Object::relocate--but this is where
1599 // we write out the data for an Output_section_data.
1600
1601 void
1602 Output_section::Input_section::write(Output_file* of)
1603 {
1604 if (!this->is_input_section())
1605 this->u2_.posd->write(of);
1606 }
1607
1608 // Write the data to a buffer. As for write(), we don't have to do
1609 // anything for an input section.
1610
1611 void
1612 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1613 {
1614 if (!this->is_input_section())
1615 this->u2_.posd->write_to_buffer(buffer);
1616 }
1617
1618 // Output_section methods.
1619
1620 // Construct an Output_section. NAME will point into a Stringpool.
1621
1622 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1623 elfcpp::Elf_Xword flags)
1624 : name_(name),
1625 addralign_(0),
1626 entsize_(0),
1627 load_address_(0),
1628 link_section_(NULL),
1629 link_(0),
1630 info_section_(NULL),
1631 info_symndx_(NULL),
1632 info_(0),
1633 type_(type),
1634 flags_(flags),
1635 out_shndx_(-1U),
1636 symtab_index_(0),
1637 dynsym_index_(0),
1638 input_sections_(),
1639 first_input_offset_(0),
1640 fills_(),
1641 postprocessing_buffer_(NULL),
1642 needs_symtab_index_(false),
1643 needs_dynsym_index_(false),
1644 should_link_to_symtab_(false),
1645 should_link_to_dynsym_(false),
1646 after_input_sections_(false),
1647 requires_postprocessing_(false),
1648 found_in_sections_clause_(false),
1649 has_load_address_(false),
1650 info_uses_section_index_(false),
1651 tls_offset_(0)
1652 {
1653 // An unallocated section has no address. Forcing this means that
1654 // we don't need special treatment for symbols defined in debug
1655 // sections.
1656 if ((flags & elfcpp::SHF_ALLOC) == 0)
1657 this->set_address(0);
1658 }
1659
1660 Output_section::~Output_section()
1661 {
1662 }
1663
1664 // Set the entry size.
1665
1666 void
1667 Output_section::set_entsize(uint64_t v)
1668 {
1669 if (this->entsize_ == 0)
1670 this->entsize_ = v;
1671 else
1672 gold_assert(this->entsize_ == v);
1673 }
1674
1675 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1676 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1677 // relocation section which applies to this section, or 0 if none, or
1678 // -1U if more than one. Return the offset of the input section
1679 // within the output section. Return -1 if the input section will
1680 // receive special handling. In the normal case we don't always keep
1681 // track of input sections for an Output_section. Instead, each
1682 // Object keeps track of the Output_section for each of its input
1683 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1684 // track of input sections here; this is used when SECTIONS appears in
1685 // a linker script.
1686
1687 template<int size, bool big_endian>
1688 off_t
1689 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1690 unsigned int shndx,
1691 const char* secname,
1692 const elfcpp::Shdr<size, big_endian>& shdr,
1693 unsigned int reloc_shndx,
1694 bool have_sections_script)
1695 {
1696 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1697 if ((addralign & (addralign - 1)) != 0)
1698 {
1699 object->error(_("invalid alignment %lu for section \"%s\""),
1700 static_cast<unsigned long>(addralign), secname);
1701 addralign = 1;
1702 }
1703
1704 if (addralign > this->addralign_)
1705 this->addralign_ = addralign;
1706
1707 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1708 this->flags_ |= (sh_flags
1709 & (elfcpp::SHF_WRITE
1710 | elfcpp::SHF_ALLOC
1711 | elfcpp::SHF_EXECINSTR));
1712
1713 uint64_t entsize = shdr.get_sh_entsize();
1714
1715 // .debug_str is a mergeable string section, but is not always so
1716 // marked by compilers. Mark manually here so we can optimize.
1717 if (strcmp(secname, ".debug_str") == 0)
1718 {
1719 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1720 entsize = 1;
1721 }
1722
1723 // If this is a SHF_MERGE section, we pass all the input sections to
1724 // a Output_data_merge. We don't try to handle relocations for such
1725 // a section.
1726 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1727 && reloc_shndx == 0)
1728 {
1729 if (this->add_merge_input_section(object, shndx, sh_flags,
1730 entsize, addralign))
1731 {
1732 // Tell the relocation routines that they need to call the
1733 // output_offset method to determine the final address.
1734 return -1;
1735 }
1736 }
1737
1738 off_t offset_in_section = this->current_data_size_for_child();
1739 off_t aligned_offset_in_section = align_address(offset_in_section,
1740 addralign);
1741
1742 if (aligned_offset_in_section > offset_in_section
1743 && !have_sections_script
1744 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1745 && object->target()->has_code_fill())
1746 {
1747 // We need to add some fill data. Using fill_list_ when
1748 // possible is an optimization, since we will often have fill
1749 // sections without input sections.
1750 off_t fill_len = aligned_offset_in_section - offset_in_section;
1751 if (this->input_sections_.empty())
1752 this->fills_.push_back(Fill(offset_in_section, fill_len));
1753 else
1754 {
1755 // FIXME: When relaxing, the size needs to adjust to
1756 // maintain a constant alignment.
1757 std::string fill_data(object->target()->code_fill(fill_len));
1758 Output_data_const* odc = new Output_data_const(fill_data, 1);
1759 this->input_sections_.push_back(Input_section(odc));
1760 }
1761 }
1762
1763 this->set_current_data_size_for_child(aligned_offset_in_section
1764 + shdr.get_sh_size());
1765
1766 // We need to keep track of this section if we are already keeping
1767 // track of sections, or if we are relaxing. FIXME: Add test for
1768 // relaxing.
1769 if (have_sections_script || !this->input_sections_.empty())
1770 this->input_sections_.push_back(Input_section(object, shndx,
1771 shdr.get_sh_size(),
1772 addralign));
1773
1774 return aligned_offset_in_section;
1775 }
1776
1777 // Add arbitrary data to an output section.
1778
1779 void
1780 Output_section::add_output_section_data(Output_section_data* posd)
1781 {
1782 Input_section inp(posd);
1783 this->add_output_section_data(&inp);
1784
1785 if (posd->is_data_size_valid())
1786 {
1787 off_t offset_in_section = this->current_data_size_for_child();
1788 off_t aligned_offset_in_section = align_address(offset_in_section,
1789 posd->addralign());
1790 this->set_current_data_size_for_child(aligned_offset_in_section
1791 + posd->data_size());
1792 }
1793 }
1794
1795 // Add arbitrary data to an output section by Input_section.
1796
1797 void
1798 Output_section::add_output_section_data(Input_section* inp)
1799 {
1800 if (this->input_sections_.empty())
1801 this->first_input_offset_ = this->current_data_size_for_child();
1802
1803 this->input_sections_.push_back(*inp);
1804
1805 uint64_t addralign = inp->addralign();
1806 if (addralign > this->addralign_)
1807 this->addralign_ = addralign;
1808
1809 inp->set_output_section(this);
1810 }
1811
1812 // Add a merge section to an output section.
1813
1814 void
1815 Output_section::add_output_merge_section(Output_section_data* posd,
1816 bool is_string, uint64_t entsize)
1817 {
1818 Input_section inp(posd, is_string, entsize);
1819 this->add_output_section_data(&inp);
1820 }
1821
1822 // Add an input section to a SHF_MERGE section.
1823
1824 bool
1825 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1826 uint64_t flags, uint64_t entsize,
1827 uint64_t addralign)
1828 {
1829 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1830
1831 // We only merge strings if the alignment is not more than the
1832 // character size. This could be handled, but it's unusual.
1833 if (is_string && addralign > entsize)
1834 return false;
1835
1836 Input_section_list::iterator p;
1837 for (p = this->input_sections_.begin();
1838 p != this->input_sections_.end();
1839 ++p)
1840 if (p->is_merge_section(is_string, entsize, addralign))
1841 {
1842 p->add_input_section(object, shndx);
1843 return true;
1844 }
1845
1846 // We handle the actual constant merging in Output_merge_data or
1847 // Output_merge_string_data.
1848 Output_section_data* posd;
1849 if (!is_string)
1850 posd = new Output_merge_data(entsize, addralign);
1851 else
1852 {
1853 switch (entsize)
1854 {
1855 case 1:
1856 posd = new Output_merge_string<char>(addralign);
1857 break;
1858 case 2:
1859 posd = new Output_merge_string<uint16_t>(addralign);
1860 break;
1861 case 4:
1862 posd = new Output_merge_string<uint32_t>(addralign);
1863 break;
1864 default:
1865 return false;
1866 }
1867 }
1868
1869 this->add_output_merge_section(posd, is_string, entsize);
1870 posd->add_input_section(object, shndx);
1871
1872 return true;
1873 }
1874
1875 // Given an address OFFSET relative to the start of input section
1876 // SHNDX in OBJECT, return whether this address is being included in
1877 // the final link. This should only be called if SHNDX in OBJECT has
1878 // a special mapping.
1879
1880 bool
1881 Output_section::is_input_address_mapped(const Relobj* object,
1882 unsigned int shndx,
1883 off_t offset) const
1884 {
1885 gold_assert(object->is_section_specially_mapped(shndx));
1886
1887 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1888 p != this->input_sections_.end();
1889 ++p)
1890 {
1891 section_offset_type output_offset;
1892 if (p->output_offset(object, shndx, offset, &output_offset))
1893 return output_offset != -1;
1894 }
1895
1896 // By default we assume that the address is mapped. This should
1897 // only be called after we have passed all sections to Layout. At
1898 // that point we should know what we are discarding.
1899 return true;
1900 }
1901
1902 // Given an address OFFSET relative to the start of input section
1903 // SHNDX in object OBJECT, return the output offset relative to the
1904 // start of the input section in the output section. This should only
1905 // be called if SHNDX in OBJECT has a special mapping.
1906
1907 section_offset_type
1908 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1909 section_offset_type offset) const
1910 {
1911 gold_assert(object->is_section_specially_mapped(shndx));
1912 // This can only be called meaningfully when layout is complete.
1913 gold_assert(Output_data::is_layout_complete());
1914
1915 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1916 p != this->input_sections_.end();
1917 ++p)
1918 {
1919 section_offset_type output_offset;
1920 if (p->output_offset(object, shndx, offset, &output_offset))
1921 return output_offset;
1922 }
1923 gold_unreachable();
1924 }
1925
1926 // Return the output virtual address of OFFSET relative to the start
1927 // of input section SHNDX in object OBJECT.
1928
1929 uint64_t
1930 Output_section::output_address(const Relobj* object, unsigned int shndx,
1931 off_t offset) const
1932 {
1933 gold_assert(object->is_section_specially_mapped(shndx));
1934
1935 uint64_t addr = this->address() + this->first_input_offset_;
1936 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1937 p != this->input_sections_.end();
1938 ++p)
1939 {
1940 addr = align_address(addr, p->addralign());
1941 section_offset_type output_offset;
1942 if (p->output_offset(object, shndx, offset, &output_offset))
1943 {
1944 if (output_offset == -1)
1945 return -1U;
1946 return addr + output_offset;
1947 }
1948 addr += p->data_size();
1949 }
1950
1951 // If we get here, it means that we don't know the mapping for this
1952 // input section. This might happen in principle if
1953 // add_input_section were called before add_output_section_data.
1954 // But it should never actually happen.
1955
1956 gold_unreachable();
1957 }
1958
1959 // Return the output address of the start of the merged section for
1960 // input section SHNDX in object OBJECT.
1961
1962 uint64_t
1963 Output_section::starting_output_address(const Relobj* object,
1964 unsigned int shndx) const
1965 {
1966 gold_assert(object->is_section_specially_mapped(shndx));
1967
1968 uint64_t addr = this->address() + this->first_input_offset_;
1969 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1970 p != this->input_sections_.end();
1971 ++p)
1972 {
1973 addr = align_address(addr, p->addralign());
1974
1975 // It would be nice if we could use the existing output_offset
1976 // method to get the output offset of input offset 0.
1977 // Unfortunately we don't know for sure that input offset 0 is
1978 // mapped at all.
1979 if (p->is_merge_section_for(object, shndx))
1980 return addr;
1981
1982 addr += p->data_size();
1983 }
1984 gold_unreachable();
1985 }
1986
1987 // Set the data size of an Output_section. This is where we handle
1988 // setting the addresses of any Output_section_data objects.
1989
1990 void
1991 Output_section::set_final_data_size()
1992 {
1993 if (this->input_sections_.empty())
1994 {
1995 this->set_data_size(this->current_data_size_for_child());
1996 return;
1997 }
1998
1999 uint64_t address = this->address();
2000 off_t startoff = this->offset();
2001 off_t off = startoff + this->first_input_offset_;
2002 for (Input_section_list::iterator p = this->input_sections_.begin();
2003 p != this->input_sections_.end();
2004 ++p)
2005 {
2006 off = align_address(off, p->addralign());
2007 p->set_address_and_file_offset(address + (off - startoff), off,
2008 startoff);
2009 off += p->data_size();
2010 }
2011
2012 this->set_data_size(off - startoff);
2013 }
2014
2015 // Reset the address and file offset.
2016
2017 void
2018 Output_section::do_reset_address_and_file_offset()
2019 {
2020 for (Input_section_list::iterator p = this->input_sections_.begin();
2021 p != this->input_sections_.end();
2022 ++p)
2023 p->reset_address_and_file_offset();
2024 }
2025
2026 // Set the TLS offset. Called only for SHT_TLS sections.
2027
2028 void
2029 Output_section::do_set_tls_offset(uint64_t tls_base)
2030 {
2031 this->tls_offset_ = this->address() - tls_base;
2032 }
2033
2034 // Write the section header to *OSHDR.
2035
2036 template<int size, bool big_endian>
2037 void
2038 Output_section::write_header(const Layout* layout,
2039 const Stringpool* secnamepool,
2040 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2041 {
2042 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2043 oshdr->put_sh_type(this->type_);
2044
2045 elfcpp::Elf_Xword flags = this->flags_;
2046 if (this->info_section_ != NULL && this->info_uses_section_index_)
2047 flags |= elfcpp::SHF_INFO_LINK;
2048 oshdr->put_sh_flags(flags);
2049
2050 oshdr->put_sh_addr(this->address());
2051 oshdr->put_sh_offset(this->offset());
2052 oshdr->put_sh_size(this->data_size());
2053 if (this->link_section_ != NULL)
2054 oshdr->put_sh_link(this->link_section_->out_shndx());
2055 else if (this->should_link_to_symtab_)
2056 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2057 else if (this->should_link_to_dynsym_)
2058 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2059 else
2060 oshdr->put_sh_link(this->link_);
2061
2062 elfcpp::Elf_Word info;
2063 if (this->info_section_ != NULL)
2064 {
2065 if (this->info_uses_section_index_)
2066 info = this->info_section_->out_shndx();
2067 else
2068 info = this->info_section_->symtab_index();
2069 }
2070 else if (this->info_symndx_ != NULL)
2071 info = this->info_symndx_->symtab_index();
2072 else
2073 info = this->info_;
2074 oshdr->put_sh_info(info);
2075
2076 oshdr->put_sh_addralign(this->addralign_);
2077 oshdr->put_sh_entsize(this->entsize_);
2078 }
2079
2080 // Write out the data. For input sections the data is written out by
2081 // Object::relocate, but we have to handle Output_section_data objects
2082 // here.
2083
2084 void
2085 Output_section::do_write(Output_file* of)
2086 {
2087 gold_assert(!this->requires_postprocessing());
2088
2089 off_t output_section_file_offset = this->offset();
2090 for (Fill_list::iterator p = this->fills_.begin();
2091 p != this->fills_.end();
2092 ++p)
2093 {
2094 std::string fill_data(parameters->target().code_fill(p->length()));
2095 of->write(output_section_file_offset + p->section_offset(),
2096 fill_data.data(), fill_data.size());
2097 }
2098
2099 for (Input_section_list::iterator p = this->input_sections_.begin();
2100 p != this->input_sections_.end();
2101 ++p)
2102 p->write(of);
2103 }
2104
2105 // If a section requires postprocessing, create the buffer to use.
2106
2107 void
2108 Output_section::create_postprocessing_buffer()
2109 {
2110 gold_assert(this->requires_postprocessing());
2111
2112 if (this->postprocessing_buffer_ != NULL)
2113 return;
2114
2115 if (!this->input_sections_.empty())
2116 {
2117 off_t off = this->first_input_offset_;
2118 for (Input_section_list::iterator p = this->input_sections_.begin();
2119 p != this->input_sections_.end();
2120 ++p)
2121 {
2122 off = align_address(off, p->addralign());
2123 p->finalize_data_size();
2124 off += p->data_size();
2125 }
2126 this->set_current_data_size_for_child(off);
2127 }
2128
2129 off_t buffer_size = this->current_data_size_for_child();
2130 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2131 }
2132
2133 // Write all the data of an Output_section into the postprocessing
2134 // buffer. This is used for sections which require postprocessing,
2135 // such as compression. Input sections are handled by
2136 // Object::Relocate.
2137
2138 void
2139 Output_section::write_to_postprocessing_buffer()
2140 {
2141 gold_assert(this->requires_postprocessing());
2142
2143 unsigned char* buffer = this->postprocessing_buffer();
2144 for (Fill_list::iterator p = this->fills_.begin();
2145 p != this->fills_.end();
2146 ++p)
2147 {
2148 std::string fill_data(parameters->target().code_fill(p->length()));
2149 memcpy(buffer + p->section_offset(), fill_data.data(),
2150 fill_data.size());
2151 }
2152
2153 off_t off = this->first_input_offset_;
2154 for (Input_section_list::iterator p = this->input_sections_.begin();
2155 p != this->input_sections_.end();
2156 ++p)
2157 {
2158 off = align_address(off, p->addralign());
2159 p->write_to_buffer(buffer + off);
2160 off += p->data_size();
2161 }
2162 }
2163
2164 // Get the input sections for linker script processing. We leave
2165 // behind the Output_section_data entries. Note that this may be
2166 // slightly incorrect for merge sections. We will leave them behind,
2167 // but it is possible that the script says that they should follow
2168 // some other input sections, as in:
2169 // .rodata { *(.rodata) *(.rodata.cst*) }
2170 // For that matter, we don't handle this correctly:
2171 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2172 // With luck this will never matter.
2173
2174 uint64_t
2175 Output_section::get_input_sections(
2176 uint64_t address,
2177 const std::string& fill,
2178 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2179 {
2180 uint64_t orig_address = address;
2181
2182 address = align_address(address, this->addralign());
2183
2184 Input_section_list remaining;
2185 for (Input_section_list::iterator p = this->input_sections_.begin();
2186 p != this->input_sections_.end();
2187 ++p)
2188 {
2189 if (p->is_input_section())
2190 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2191 else
2192 {
2193 uint64_t aligned_address = align_address(address, p->addralign());
2194 if (aligned_address != address && !fill.empty())
2195 {
2196 section_size_type length =
2197 convert_to_section_size_type(aligned_address - address);
2198 std::string this_fill;
2199 this_fill.reserve(length);
2200 while (this_fill.length() + fill.length() <= length)
2201 this_fill += fill;
2202 if (this_fill.length() < length)
2203 this_fill.append(fill, 0, length - this_fill.length());
2204
2205 Output_section_data* posd = new Output_data_const(this_fill, 0);
2206 remaining.push_back(Input_section(posd));
2207 }
2208 address = aligned_address;
2209
2210 remaining.push_back(*p);
2211
2212 p->finalize_data_size();
2213 address += p->data_size();
2214 }
2215 }
2216
2217 this->input_sections_.swap(remaining);
2218 this->first_input_offset_ = 0;
2219
2220 uint64_t data_size = address - orig_address;
2221 this->set_current_data_size_for_child(data_size);
2222 return data_size;
2223 }
2224
2225 // Add an input section from a script.
2226
2227 void
2228 Output_section::add_input_section_for_script(Relobj* object,
2229 unsigned int shndx,
2230 off_t data_size,
2231 uint64_t addralign)
2232 {
2233 if (addralign > this->addralign_)
2234 this->addralign_ = addralign;
2235
2236 off_t offset_in_section = this->current_data_size_for_child();
2237 off_t aligned_offset_in_section = align_address(offset_in_section,
2238 addralign);
2239
2240 this->set_current_data_size_for_child(aligned_offset_in_section
2241 + data_size);
2242
2243 this->input_sections_.push_back(Input_section(object, shndx,
2244 data_size, addralign));
2245 }
2246
2247 // Print stats for merge sections to stderr.
2248
2249 void
2250 Output_section::print_merge_stats()
2251 {
2252 Input_section_list::iterator p;
2253 for (p = this->input_sections_.begin();
2254 p != this->input_sections_.end();
2255 ++p)
2256 p->print_merge_stats(this->name_);
2257 }
2258
2259 // Output segment methods.
2260
2261 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2262 : output_data_(),
2263 output_bss_(),
2264 vaddr_(0),
2265 paddr_(0),
2266 memsz_(0),
2267 max_align_(0),
2268 min_p_align_(0),
2269 offset_(0),
2270 filesz_(0),
2271 type_(type),
2272 flags_(flags),
2273 is_max_align_known_(false),
2274 are_addresses_set_(false)
2275 {
2276 }
2277
2278 // Add an Output_section to an Output_segment.
2279
2280 void
2281 Output_segment::add_output_section(Output_section* os,
2282 elfcpp::Elf_Word seg_flags,
2283 bool front)
2284 {
2285 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
2286 gold_assert(!this->is_max_align_known_);
2287
2288 // Update the segment flags.
2289 this->flags_ |= seg_flags;
2290
2291 Output_segment::Output_data_list* pdl;
2292 if (os->type() == elfcpp::SHT_NOBITS)
2293 pdl = &this->output_bss_;
2294 else
2295 pdl = &this->output_data_;
2296
2297 // So that PT_NOTE segments will work correctly, we need to ensure
2298 // that all SHT_NOTE sections are adjacent. This will normally
2299 // happen automatically, because all the SHT_NOTE input sections
2300 // will wind up in the same output section. However, it is possible
2301 // for multiple SHT_NOTE input sections to have different section
2302 // flags, and thus be in different output sections, but for the
2303 // different section flags to map into the same segment flags and
2304 // thus the same output segment.
2305
2306 // Note that while there may be many input sections in an output
2307 // section, there are normally only a few output sections in an
2308 // output segment. This loop is expected to be fast.
2309
2310 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
2311 {
2312 Output_segment::Output_data_list::iterator p = pdl->end();
2313 do
2314 {
2315 --p;
2316 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2317 {
2318 // We don't worry about the FRONT parameter.
2319 ++p;
2320 pdl->insert(p, os);
2321 return;
2322 }
2323 }
2324 while (p != pdl->begin());
2325 }
2326
2327 // Similarly, so that PT_TLS segments will work, we need to group
2328 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2329 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2330 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2331 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2332 // and the PT_TLS segment -- we do this grouping only for the
2333 // PT_LOAD segment.
2334 if (this->type_ != elfcpp::PT_TLS
2335 && (os->flags() & elfcpp::SHF_TLS) != 0
2336 && !this->output_data_.empty())
2337 {
2338 pdl = &this->output_data_;
2339 bool nobits = os->type() == elfcpp::SHT_NOBITS;
2340 bool sawtls = false;
2341 Output_segment::Output_data_list::iterator p = pdl->end();
2342 do
2343 {
2344 --p;
2345 bool insert;
2346 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2347 {
2348 sawtls = true;
2349 // Put a NOBITS section after the first TLS section.
2350 // But a PROGBITS section after the first TLS/PROGBITS
2351 // section.
2352 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2353 }
2354 else
2355 {
2356 // If we've gone past the TLS sections, but we've seen a
2357 // TLS section, then we need to insert this section now.
2358 insert = sawtls;
2359 }
2360
2361 if (insert)
2362 {
2363 // We don't worry about the FRONT parameter.
2364 ++p;
2365 pdl->insert(p, os);
2366 return;
2367 }
2368 }
2369 while (p != pdl->begin());
2370
2371 // There are no TLS sections yet; put this one at the requested
2372 // location in the section list.
2373 }
2374
2375 if (front)
2376 pdl->push_front(os);
2377 else
2378 pdl->push_back(os);
2379 }
2380
2381 // Add an Output_data (which is not an Output_section) to the start of
2382 // a segment.
2383
2384 void
2385 Output_segment::add_initial_output_data(Output_data* od)
2386 {
2387 gold_assert(!this->is_max_align_known_);
2388 this->output_data_.push_front(od);
2389 }
2390
2391 // Return the maximum alignment of the Output_data in Output_segment.
2392
2393 uint64_t
2394 Output_segment::maximum_alignment()
2395 {
2396 if (!this->is_max_align_known_)
2397 {
2398 uint64_t addralign;
2399
2400 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2401 if (addralign > this->max_align_)
2402 this->max_align_ = addralign;
2403
2404 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2405 if (addralign > this->max_align_)
2406 this->max_align_ = addralign;
2407
2408 this->is_max_align_known_ = true;
2409 }
2410
2411 return this->max_align_;
2412 }
2413
2414 // Return the maximum alignment of a list of Output_data.
2415
2416 uint64_t
2417 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
2418 {
2419 uint64_t ret = 0;
2420 for (Output_data_list::const_iterator p = pdl->begin();
2421 p != pdl->end();
2422 ++p)
2423 {
2424 uint64_t addralign = (*p)->addralign();
2425 if (addralign > ret)
2426 ret = addralign;
2427 }
2428 return ret;
2429 }
2430
2431 // Return the number of dynamic relocs applied to this segment.
2432
2433 unsigned int
2434 Output_segment::dynamic_reloc_count() const
2435 {
2436 return (this->dynamic_reloc_count_list(&this->output_data_)
2437 + this->dynamic_reloc_count_list(&this->output_bss_));
2438 }
2439
2440 // Return the number of dynamic relocs applied to an Output_data_list.
2441
2442 unsigned int
2443 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2444 {
2445 unsigned int count = 0;
2446 for (Output_data_list::const_iterator p = pdl->begin();
2447 p != pdl->end();
2448 ++p)
2449 count += (*p)->dynamic_reloc_count();
2450 return count;
2451 }
2452
2453 // Set the section addresses for an Output_segment. If RESET is true,
2454 // reset the addresses first. ADDR is the address and *POFF is the
2455 // file offset. Set the section indexes starting with *PSHNDX.
2456 // Return the address of the immediately following segment. Update
2457 // *POFF and *PSHNDX.
2458
2459 uint64_t
2460 Output_segment::set_section_addresses(bool reset, uint64_t addr, off_t* poff,
2461 unsigned int* pshndx)
2462 {
2463 gold_assert(this->type_ == elfcpp::PT_LOAD);
2464
2465 if (!reset && this->are_addresses_set_)
2466 {
2467 gold_assert(this->paddr_ == addr);
2468 addr = this->vaddr_;
2469 }
2470 else
2471 {
2472 this->vaddr_ = addr;
2473 this->paddr_ = addr;
2474 this->are_addresses_set_ = true;
2475 }
2476
2477 off_t orig_off = *poff;
2478 this->offset_ = orig_off;
2479
2480 addr = this->set_section_list_addresses(reset, &this->output_data_,
2481 addr, poff, pshndx);
2482 this->filesz_ = *poff - orig_off;
2483
2484 off_t off = *poff;
2485
2486 uint64_t ret = this->set_section_list_addresses(reset, &this->output_bss_,
2487 addr, poff, pshndx);
2488 this->memsz_ = *poff - orig_off;
2489
2490 // Ignore the file offset adjustments made by the BSS Output_data
2491 // objects.
2492 *poff = off;
2493
2494 return ret;
2495 }
2496
2497 // Set the addresses and file offsets in a list of Output_data
2498 // structures.
2499
2500 uint64_t
2501 Output_segment::set_section_list_addresses(bool reset, Output_data_list* pdl,
2502 uint64_t addr, off_t* poff,
2503 unsigned int* pshndx)
2504 {
2505 off_t startoff = *poff;
2506
2507 off_t off = startoff;
2508 for (Output_data_list::iterator p = pdl->begin();
2509 p != pdl->end();
2510 ++p)
2511 {
2512 if (reset)
2513 (*p)->reset_address_and_file_offset();
2514
2515 // When using a linker script the section will most likely
2516 // already have an address.
2517 if (!(*p)->is_address_valid())
2518 {
2519 off = align_address(off, (*p)->addralign());
2520 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2521 }
2522 else
2523 {
2524 // The script may have inserted a skip forward, but it
2525 // better not have moved backward.
2526 gold_assert((*p)->address() >= addr + (off - startoff));
2527 off += (*p)->address() - (addr + (off - startoff));
2528 (*p)->set_file_offset(off);
2529 (*p)->finalize_data_size();
2530 }
2531
2532 // Unless this is a PT_TLS segment, we want to ignore the size
2533 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
2534 // affect the size of a PT_LOAD segment.
2535 if (this->type_ == elfcpp::PT_TLS
2536 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2537 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2538 off += (*p)->data_size();
2539
2540 if ((*p)->is_section())
2541 {
2542 (*p)->set_out_shndx(*pshndx);
2543 ++*pshndx;
2544 }
2545 }
2546
2547 *poff = off;
2548 return addr + (off - startoff);
2549 }
2550
2551 // For a non-PT_LOAD segment, set the offset from the sections, if
2552 // any.
2553
2554 void
2555 Output_segment::set_offset()
2556 {
2557 gold_assert(this->type_ != elfcpp::PT_LOAD);
2558
2559 gold_assert(!this->are_addresses_set_);
2560
2561 if (this->output_data_.empty() && this->output_bss_.empty())
2562 {
2563 this->vaddr_ = 0;
2564 this->paddr_ = 0;
2565 this->are_addresses_set_ = true;
2566 this->memsz_ = 0;
2567 this->min_p_align_ = 0;
2568 this->offset_ = 0;
2569 this->filesz_ = 0;
2570 return;
2571 }
2572
2573 const Output_data* first;
2574 if (this->output_data_.empty())
2575 first = this->output_bss_.front();
2576 else
2577 first = this->output_data_.front();
2578 this->vaddr_ = first->address();
2579 this->paddr_ = (first->has_load_address()
2580 ? first->load_address()
2581 : this->vaddr_);
2582 this->are_addresses_set_ = true;
2583 this->offset_ = first->offset();
2584
2585 if (this->output_data_.empty())
2586 this->filesz_ = 0;
2587 else
2588 {
2589 const Output_data* last_data = this->output_data_.back();
2590 this->filesz_ = (last_data->address()
2591 + last_data->data_size()
2592 - this->vaddr_);
2593 }
2594
2595 const Output_data* last;
2596 if (this->output_bss_.empty())
2597 last = this->output_data_.back();
2598 else
2599 last = this->output_bss_.back();
2600 this->memsz_ = (last->address()
2601 + last->data_size()
2602 - this->vaddr_);
2603 }
2604
2605 // Set the TLS offsets of the sections in the PT_TLS segment.
2606
2607 void
2608 Output_segment::set_tls_offsets()
2609 {
2610 gold_assert(this->type_ == elfcpp::PT_TLS);
2611
2612 for (Output_data_list::iterator p = this->output_data_.begin();
2613 p != this->output_data_.end();
2614 ++p)
2615 (*p)->set_tls_offset(this->vaddr_);
2616
2617 for (Output_data_list::iterator p = this->output_bss_.begin();
2618 p != this->output_bss_.end();
2619 ++p)
2620 (*p)->set_tls_offset(this->vaddr_);
2621 }
2622
2623 // Return the address of the first section.
2624
2625 uint64_t
2626 Output_segment::first_section_load_address() const
2627 {
2628 for (Output_data_list::const_iterator p = this->output_data_.begin();
2629 p != this->output_data_.end();
2630 ++p)
2631 if ((*p)->is_section())
2632 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2633
2634 for (Output_data_list::const_iterator p = this->output_bss_.begin();
2635 p != this->output_bss_.end();
2636 ++p)
2637 if ((*p)->is_section())
2638 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2639
2640 gold_unreachable();
2641 }
2642
2643 // Return the number of Output_sections in an Output_segment.
2644
2645 unsigned int
2646 Output_segment::output_section_count() const
2647 {
2648 return (this->output_section_count_list(&this->output_data_)
2649 + this->output_section_count_list(&this->output_bss_));
2650 }
2651
2652 // Return the number of Output_sections in an Output_data_list.
2653
2654 unsigned int
2655 Output_segment::output_section_count_list(const Output_data_list* pdl) const
2656 {
2657 unsigned int count = 0;
2658 for (Output_data_list::const_iterator p = pdl->begin();
2659 p != pdl->end();
2660 ++p)
2661 {
2662 if ((*p)->is_section())
2663 ++count;
2664 }
2665 return count;
2666 }
2667
2668 // Return the section attached to the list segment with the lowest
2669 // load address. This is used when handling a PHDRS clause in a
2670 // linker script.
2671
2672 Output_section*
2673 Output_segment::section_with_lowest_load_address() const
2674 {
2675 Output_section* found = NULL;
2676 uint64_t found_lma = 0;
2677 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
2678
2679 Output_section* found_data = found;
2680 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
2681 if (found != found_data && found_data != NULL)
2682 {
2683 gold_error(_("nobits section %s may not precede progbits section %s "
2684 "in same segment"),
2685 found->name(), found_data->name());
2686 return NULL;
2687 }
2688
2689 return found;
2690 }
2691
2692 // Look through a list for a section with a lower load address.
2693
2694 void
2695 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
2696 Output_section** found,
2697 uint64_t* found_lma) const
2698 {
2699 for (Output_data_list::const_iterator p = pdl->begin();
2700 p != pdl->end();
2701 ++p)
2702 {
2703 if (!(*p)->is_section())
2704 continue;
2705 Output_section* os = static_cast<Output_section*>(*p);
2706 uint64_t lma = (os->has_load_address()
2707 ? os->load_address()
2708 : os->address());
2709 if (*found == NULL || lma < *found_lma)
2710 {
2711 *found = os;
2712 *found_lma = lma;
2713 }
2714 }
2715 }
2716
2717 // Write the segment data into *OPHDR.
2718
2719 template<int size, bool big_endian>
2720 void
2721 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
2722 {
2723 ophdr->put_p_type(this->type_);
2724 ophdr->put_p_offset(this->offset_);
2725 ophdr->put_p_vaddr(this->vaddr_);
2726 ophdr->put_p_paddr(this->paddr_);
2727 ophdr->put_p_filesz(this->filesz_);
2728 ophdr->put_p_memsz(this->memsz_);
2729 ophdr->put_p_flags(this->flags_);
2730 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
2731 }
2732
2733 // Write the section headers into V.
2734
2735 template<int size, bool big_endian>
2736 unsigned char*
2737 Output_segment::write_section_headers(const Layout* layout,
2738 const Stringpool* secnamepool,
2739 unsigned char* v,
2740 unsigned int *pshndx
2741 ACCEPT_SIZE_ENDIAN) const
2742 {
2743 // Every section that is attached to a segment must be attached to a
2744 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2745 // segments.
2746 if (this->type_ != elfcpp::PT_LOAD)
2747 return v;
2748
2749 v = this->write_section_headers_list
2750 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
2751 layout, secnamepool, &this->output_data_, v, pshndx
2752 SELECT_SIZE_ENDIAN(size, big_endian));
2753 v = this->write_section_headers_list
2754 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
2755 layout, secnamepool, &this->output_bss_, v, pshndx
2756 SELECT_SIZE_ENDIAN(size, big_endian));
2757 return v;
2758 }
2759
2760 template<int size, bool big_endian>
2761 unsigned char*
2762 Output_segment::write_section_headers_list(const Layout* layout,
2763 const Stringpool* secnamepool,
2764 const Output_data_list* pdl,
2765 unsigned char* v,
2766 unsigned int* pshndx
2767 ACCEPT_SIZE_ENDIAN) const
2768 {
2769 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2770 for (Output_data_list::const_iterator p = pdl->begin();
2771 p != pdl->end();
2772 ++p)
2773 {
2774 if ((*p)->is_section())
2775 {
2776 const Output_section* ps = static_cast<const Output_section*>(*p);
2777 gold_assert(*pshndx == ps->out_shndx());
2778 elfcpp::Shdr_write<size, big_endian> oshdr(v);
2779 ps->write_header(layout, secnamepool, &oshdr);
2780 v += shdr_size;
2781 ++*pshndx;
2782 }
2783 }
2784 return v;
2785 }
2786
2787 // Output_file methods.
2788
2789 Output_file::Output_file(const char* name)
2790 : name_(name),
2791 o_(-1),
2792 file_size_(0),
2793 base_(NULL),
2794 map_is_anonymous_(false),
2795 is_temporary_(false)
2796 {
2797 }
2798
2799 // Open the output file.
2800
2801 void
2802 Output_file::open(off_t file_size)
2803 {
2804 this->file_size_ = file_size;
2805
2806 // Unlink the file first; otherwise the open() may fail if the file
2807 // is busy (e.g. it's an executable that's currently being executed).
2808 //
2809 // However, the linker may be part of a system where a zero-length
2810 // file is created for it to write to, with tight permissions (gcc
2811 // 2.95 did something like this). Unlinking the file would work
2812 // around those permission controls, so we only unlink if the file
2813 // has a non-zero size. We also unlink only regular files to avoid
2814 // trouble with directories/etc.
2815 //
2816 // If we fail, continue; this command is merely a best-effort attempt
2817 // to improve the odds for open().
2818
2819 // We let the name "-" mean "stdout"
2820 if (!this->is_temporary_)
2821 {
2822 if (strcmp(this->name_, "-") == 0)
2823 this->o_ = STDOUT_FILENO;
2824 else
2825 {
2826 struct stat s;
2827 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
2828 unlink_if_ordinary(this->name_);
2829
2830 int mode = parameters->options().relocatable() ? 0666 : 0777;
2831 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
2832 if (o < 0)
2833 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
2834 this->o_ = o;
2835 }
2836 }
2837
2838 this->map();
2839 }
2840
2841 // Resize the output file.
2842
2843 void
2844 Output_file::resize(off_t file_size)
2845 {
2846 // If the mmap is mapping an anonymous memory buffer, this is easy:
2847 // just mremap to the new size. If it's mapping to a file, we want
2848 // to unmap to flush to the file, then remap after growing the file.
2849 if (this->map_is_anonymous_)
2850 {
2851 void* base = ::mremap(this->base_, this->file_size_, file_size,
2852 MREMAP_MAYMOVE);
2853 if (base == MAP_FAILED)
2854 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
2855 this->base_ = static_cast<unsigned char*>(base);
2856 this->file_size_ = file_size;
2857 }
2858 else
2859 {
2860 this->unmap();
2861 this->file_size_ = file_size;
2862 this->map();
2863 }
2864 }
2865
2866 // Map the file into memory.
2867
2868 void
2869 Output_file::map()
2870 {
2871 const int o = this->o_;
2872
2873 // If the output file is not a regular file, don't try to mmap it;
2874 // instead, we'll mmap a block of memory (an anonymous buffer), and
2875 // then later write the buffer to the file.
2876 void* base;
2877 struct stat statbuf;
2878 if (o == STDOUT_FILENO || o == STDERR_FILENO
2879 || ::fstat(o, &statbuf) != 0
2880 || !S_ISREG(statbuf.st_mode)
2881 || this->is_temporary_)
2882 {
2883 this->map_is_anonymous_ = true;
2884 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2885 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2886 }
2887 else
2888 {
2889 // Write out one byte to make the file the right size.
2890 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
2891 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
2892 char b = 0;
2893 if (::write(o, &b, 1) != 1)
2894 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
2895
2896 // Map the file into memory.
2897 this->map_is_anonymous_ = false;
2898 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2899 MAP_SHARED, o, 0);
2900 }
2901 if (base == MAP_FAILED)
2902 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
2903 this->base_ = static_cast<unsigned char*>(base);
2904 }
2905
2906 // Unmap the file from memory.
2907
2908 void
2909 Output_file::unmap()
2910 {
2911 if (::munmap(this->base_, this->file_size_) < 0)
2912 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
2913 this->base_ = NULL;
2914 }
2915
2916 // Close the output file.
2917
2918 void
2919 Output_file::close()
2920 {
2921 // If the map isn't file-backed, we need to write it now.
2922 if (this->map_is_anonymous_ && !this->is_temporary_)
2923 {
2924 size_t bytes_to_write = this->file_size_;
2925 while (bytes_to_write > 0)
2926 {
2927 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
2928 if (bytes_written == 0)
2929 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
2930 else if (bytes_written < 0)
2931 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
2932 else
2933 bytes_to_write -= bytes_written;
2934 }
2935 }
2936 this->unmap();
2937
2938 // We don't close stdout or stderr
2939 if (this->o_ != STDOUT_FILENO
2940 && this->o_ != STDERR_FILENO
2941 && !this->is_temporary_)
2942 if (::close(this->o_) < 0)
2943 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
2944 this->o_ = -1;
2945 }
2946
2947 // Instantiate the templates we need. We could use the configure
2948 // script to restrict this to only the ones for implemented targets.
2949
2950 #ifdef HAVE_TARGET_32_LITTLE
2951 template
2952 off_t
2953 Output_section::add_input_section<32, false>(
2954 Sized_relobj<32, false>* object,
2955 unsigned int shndx,
2956 const char* secname,
2957 const elfcpp::Shdr<32, false>& shdr,
2958 unsigned int reloc_shndx,
2959 bool have_sections_script);
2960 #endif
2961
2962 #ifdef HAVE_TARGET_32_BIG
2963 template
2964 off_t
2965 Output_section::add_input_section<32, true>(
2966 Sized_relobj<32, true>* object,
2967 unsigned int shndx,
2968 const char* secname,
2969 const elfcpp::Shdr<32, true>& shdr,
2970 unsigned int reloc_shndx,
2971 bool have_sections_script);
2972 #endif
2973
2974 #ifdef HAVE_TARGET_64_LITTLE
2975 template
2976 off_t
2977 Output_section::add_input_section<64, false>(
2978 Sized_relobj<64, false>* object,
2979 unsigned int shndx,
2980 const char* secname,
2981 const elfcpp::Shdr<64, false>& shdr,
2982 unsigned int reloc_shndx,
2983 bool have_sections_script);
2984 #endif
2985
2986 #ifdef HAVE_TARGET_64_BIG
2987 template
2988 off_t
2989 Output_section::add_input_section<64, true>(
2990 Sized_relobj<64, true>* object,
2991 unsigned int shndx,
2992 const char* secname,
2993 const elfcpp::Shdr<64, true>& shdr,
2994 unsigned int reloc_shndx,
2995 bool have_sections_script);
2996 #endif
2997
2998 #ifdef HAVE_TARGET_32_LITTLE
2999 template
3000 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
3001 #endif
3002
3003 #ifdef HAVE_TARGET_32_BIG
3004 template
3005 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
3006 #endif
3007
3008 #ifdef HAVE_TARGET_64_LITTLE
3009 template
3010 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
3011 #endif
3012
3013 #ifdef HAVE_TARGET_64_BIG
3014 template
3015 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
3016 #endif
3017
3018 #ifdef HAVE_TARGET_32_LITTLE
3019 template
3020 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
3021 #endif
3022
3023 #ifdef HAVE_TARGET_32_BIG
3024 template
3025 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
3026 #endif
3027
3028 #ifdef HAVE_TARGET_64_LITTLE
3029 template
3030 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
3031 #endif
3032
3033 #ifdef HAVE_TARGET_64_BIG
3034 template
3035 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
3036 #endif
3037
3038 #ifdef HAVE_TARGET_32_LITTLE
3039 template
3040 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
3041 #endif
3042
3043 #ifdef HAVE_TARGET_32_BIG
3044 template
3045 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
3046 #endif
3047
3048 #ifdef HAVE_TARGET_64_LITTLE
3049 template
3050 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
3051 #endif
3052
3053 #ifdef HAVE_TARGET_64_BIG
3054 template
3055 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
3056 #endif
3057
3058 #ifdef HAVE_TARGET_32_LITTLE
3059 template
3060 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
3061 #endif
3062
3063 #ifdef HAVE_TARGET_32_BIG
3064 template
3065 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
3066 #endif
3067
3068 #ifdef HAVE_TARGET_64_LITTLE
3069 template
3070 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
3071 #endif
3072
3073 #ifdef HAVE_TARGET_64_BIG
3074 template
3075 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
3076 #endif
3077
3078 #ifdef HAVE_TARGET_32_LITTLE
3079 template
3080 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3081 #endif
3082
3083 #ifdef HAVE_TARGET_32_BIG
3084 template
3085 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3086 #endif
3087
3088 #ifdef HAVE_TARGET_64_LITTLE
3089 template
3090 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3091 #endif
3092
3093 #ifdef HAVE_TARGET_64_BIG
3094 template
3095 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3096 #endif
3097
3098 #ifdef HAVE_TARGET_32_LITTLE
3099 template
3100 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3101 #endif
3102
3103 #ifdef HAVE_TARGET_32_BIG
3104 template
3105 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3106 #endif
3107
3108 #ifdef HAVE_TARGET_64_LITTLE
3109 template
3110 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3111 #endif
3112
3113 #ifdef HAVE_TARGET_64_BIG
3114 template
3115 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3116 #endif
3117
3118 #ifdef HAVE_TARGET_32_LITTLE
3119 template
3120 class Output_data_group<32, false>;
3121 #endif
3122
3123 #ifdef HAVE_TARGET_32_BIG
3124 template
3125 class Output_data_group<32, true>;
3126 #endif
3127
3128 #ifdef HAVE_TARGET_64_LITTLE
3129 template
3130 class Output_data_group<64, false>;
3131 #endif
3132
3133 #ifdef HAVE_TARGET_64_BIG
3134 template
3135 class Output_data_group<64, true>;
3136 #endif
3137
3138 #ifdef HAVE_TARGET_32_LITTLE
3139 template
3140 class Output_data_got<32, false>;
3141 #endif
3142
3143 #ifdef HAVE_TARGET_32_BIG
3144 template
3145 class Output_data_got<32, true>;
3146 #endif
3147
3148 #ifdef HAVE_TARGET_64_LITTLE
3149 template
3150 class Output_data_got<64, false>;
3151 #endif
3152
3153 #ifdef HAVE_TARGET_64_BIG
3154 template
3155 class Output_data_got<64, true>;
3156 #endif
3157
3158 } // End namespace gold.
This page took 0.120962 seconds and 5 git commands to generate.