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