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