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