* Makefile.in (arm-tdep.o): Update.
[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 const elfcpp::Elf_Word* contents)
1001 : Output_section_data(entry_count * 4, 4),
1002 relobj_(relobj)
1003 {
1004 this->flags_ = elfcpp::Swap<32, big_endian>::readval(contents);
1005 for (section_size_type i = 1; i < entry_count; ++i)
1006 {
1007 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
1008 this->input_sections_.push_back(shndx);
1009 }
1010 }
1011
1012 // Write out the section group, which means translating the section
1013 // indexes to apply to the output file.
1014
1015 template<int size, bool big_endian>
1016 void
1017 Output_data_group<size, big_endian>::do_write(Output_file* of)
1018 {
1019 const off_t off = this->offset();
1020 const section_size_type oview_size =
1021 convert_to_section_size_type(this->data_size());
1022 unsigned char* const oview = of->get_output_view(off, oview_size);
1023
1024 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1025 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1026 ++contents;
1027
1028 for (std::vector<unsigned int>::const_iterator p =
1029 this->input_sections_.begin();
1030 p != this->input_sections_.end();
1031 ++p, ++contents)
1032 {
1033 section_offset_type dummy;
1034 Output_section* os = this->relobj_->output_section(*p, &dummy);
1035
1036 unsigned int output_shndx;
1037 if (os != NULL)
1038 output_shndx = os->out_shndx();
1039 else
1040 {
1041 this->relobj_->error(_("section group retained but "
1042 "group element discarded"));
1043 output_shndx = 0;
1044 }
1045
1046 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1047 }
1048
1049 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1050 gold_assert(wrote == oview_size);
1051
1052 of->write_output_view(off, oview_size, oview);
1053
1054 // We no longer need this information.
1055 this->input_sections_.clear();
1056 }
1057
1058 // Output_data_got::Got_entry methods.
1059
1060 // Write out the entry.
1061
1062 template<int size, bool big_endian>
1063 void
1064 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
1065 {
1066 Valtype val = 0;
1067
1068 switch (this->local_sym_index_)
1069 {
1070 case GSYM_CODE:
1071 {
1072 // If the symbol is resolved locally, we need to write out the
1073 // link-time value, which will be relocated dynamically by a
1074 // RELATIVE relocation.
1075 Symbol* gsym = this->u_.gsym;
1076 Sized_symbol<size>* sgsym;
1077 // This cast is a bit ugly. We don't want to put a
1078 // virtual method in Symbol, because we want Symbol to be
1079 // as small as possible.
1080 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1081 val = sgsym->value();
1082 }
1083 break;
1084
1085 case CONSTANT_CODE:
1086 val = this->u_.constant;
1087 break;
1088
1089 default:
1090 {
1091 const unsigned int lsi = this->local_sym_index_;
1092 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1093 val = symval->value(this->u_.object, 0);
1094 }
1095 break;
1096 }
1097
1098 elfcpp::Swap<size, big_endian>::writeval(pov, val);
1099 }
1100
1101 // Output_data_got methods.
1102
1103 // Add an entry for a global symbol to the GOT. This returns true if
1104 // this is a new GOT entry, false if the symbol already had a GOT
1105 // entry.
1106
1107 template<int size, bool big_endian>
1108 bool
1109 Output_data_got<size, big_endian>::add_global(
1110 Symbol* gsym,
1111 unsigned int got_type)
1112 {
1113 if (gsym->has_got_offset(got_type))
1114 return false;
1115
1116 this->entries_.push_back(Got_entry(gsym));
1117 this->set_got_size();
1118 gsym->set_got_offset(got_type, this->last_got_offset());
1119 return true;
1120 }
1121
1122 // Add an entry for a global symbol to the GOT, and add a dynamic
1123 // relocation of type R_TYPE for the GOT entry.
1124 template<int size, bool big_endian>
1125 void
1126 Output_data_got<size, big_endian>::add_global_with_rel(
1127 Symbol* gsym,
1128 unsigned int got_type,
1129 Rel_dyn* rel_dyn,
1130 unsigned int r_type)
1131 {
1132 if (gsym->has_got_offset(got_type))
1133 return;
1134
1135 this->entries_.push_back(Got_entry());
1136 this->set_got_size();
1137 unsigned int got_offset = this->last_got_offset();
1138 gsym->set_got_offset(got_type, got_offset);
1139 rel_dyn->add_global(gsym, r_type, this, got_offset);
1140 }
1141
1142 template<int size, bool big_endian>
1143 void
1144 Output_data_got<size, big_endian>::add_global_with_rela(
1145 Symbol* gsym,
1146 unsigned int got_type,
1147 Rela_dyn* rela_dyn,
1148 unsigned int r_type)
1149 {
1150 if (gsym->has_got_offset(got_type))
1151 return;
1152
1153 this->entries_.push_back(Got_entry());
1154 this->set_got_size();
1155 unsigned int got_offset = this->last_got_offset();
1156 gsym->set_got_offset(got_type, got_offset);
1157 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1158 }
1159
1160 // Add a pair of entries for a global symbol to the GOT, and add
1161 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1162 // If R_TYPE_2 == 0, add the second entry with no relocation.
1163 template<int size, bool big_endian>
1164 void
1165 Output_data_got<size, big_endian>::add_global_pair_with_rel(
1166 Symbol* gsym,
1167 unsigned int got_type,
1168 Rel_dyn* rel_dyn,
1169 unsigned int r_type_1,
1170 unsigned int r_type_2)
1171 {
1172 if (gsym->has_got_offset(got_type))
1173 return;
1174
1175 this->entries_.push_back(Got_entry());
1176 unsigned int got_offset = this->last_got_offset();
1177 gsym->set_got_offset(got_type, got_offset);
1178 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1179
1180 this->entries_.push_back(Got_entry());
1181 if (r_type_2 != 0)
1182 {
1183 got_offset = this->last_got_offset();
1184 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1185 }
1186
1187 this->set_got_size();
1188 }
1189
1190 template<int size, bool big_endian>
1191 void
1192 Output_data_got<size, big_endian>::add_global_pair_with_rela(
1193 Symbol* gsym,
1194 unsigned int got_type,
1195 Rela_dyn* rela_dyn,
1196 unsigned int r_type_1,
1197 unsigned int r_type_2)
1198 {
1199 if (gsym->has_got_offset(got_type))
1200 return;
1201
1202 this->entries_.push_back(Got_entry());
1203 unsigned int got_offset = this->last_got_offset();
1204 gsym->set_got_offset(got_type, got_offset);
1205 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1206
1207 this->entries_.push_back(Got_entry());
1208 if (r_type_2 != 0)
1209 {
1210 got_offset = this->last_got_offset();
1211 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1212 }
1213
1214 this->set_got_size();
1215 }
1216
1217 // Add an entry for a local symbol to the GOT. This returns true if
1218 // this is a new GOT entry, false if the symbol already has a GOT
1219 // entry.
1220
1221 template<int size, bool big_endian>
1222 bool
1223 Output_data_got<size, big_endian>::add_local(
1224 Sized_relobj<size, big_endian>* object,
1225 unsigned int symndx,
1226 unsigned int got_type)
1227 {
1228 if (object->local_has_got_offset(symndx, got_type))
1229 return false;
1230
1231 this->entries_.push_back(Got_entry(object, symndx));
1232 this->set_got_size();
1233 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
1234 return true;
1235 }
1236
1237 // Add an entry for a local symbol to the GOT, and add a dynamic
1238 // relocation of type R_TYPE for the GOT entry.
1239 template<int size, bool big_endian>
1240 void
1241 Output_data_got<size, big_endian>::add_local_with_rel(
1242 Sized_relobj<size, big_endian>* object,
1243 unsigned int symndx,
1244 unsigned int got_type,
1245 Rel_dyn* rel_dyn,
1246 unsigned int r_type)
1247 {
1248 if (object->local_has_got_offset(symndx, got_type))
1249 return;
1250
1251 this->entries_.push_back(Got_entry());
1252 this->set_got_size();
1253 unsigned int got_offset = this->last_got_offset();
1254 object->set_local_got_offset(symndx, got_type, got_offset);
1255 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1256 }
1257
1258 template<int size, bool big_endian>
1259 void
1260 Output_data_got<size, big_endian>::add_local_with_rela(
1261 Sized_relobj<size, big_endian>* object,
1262 unsigned int symndx,
1263 unsigned int got_type,
1264 Rela_dyn* rela_dyn,
1265 unsigned int r_type)
1266 {
1267 if (object->local_has_got_offset(symndx, got_type))
1268 return;
1269
1270 this->entries_.push_back(Got_entry());
1271 this->set_got_size();
1272 unsigned int got_offset = this->last_got_offset();
1273 object->set_local_got_offset(symndx, got_type, got_offset);
1274 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1275 }
1276
1277 // Add a pair of entries for a local symbol to the GOT, and add
1278 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1279 // If R_TYPE_2 == 0, add the second entry with no relocation.
1280 template<int size, bool big_endian>
1281 void
1282 Output_data_got<size, big_endian>::add_local_pair_with_rel(
1283 Sized_relobj<size, big_endian>* object,
1284 unsigned int symndx,
1285 unsigned int shndx,
1286 unsigned int got_type,
1287 Rel_dyn* rel_dyn,
1288 unsigned int r_type_1,
1289 unsigned int r_type_2)
1290 {
1291 if (object->local_has_got_offset(symndx, got_type))
1292 return;
1293
1294 this->entries_.push_back(Got_entry());
1295 unsigned int got_offset = this->last_got_offset();
1296 object->set_local_got_offset(symndx, got_type, got_offset);
1297 section_offset_type off;
1298 Output_section* os = object->output_section(shndx, &off);
1299 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
1300
1301 this->entries_.push_back(Got_entry(object, symndx));
1302 if (r_type_2 != 0)
1303 {
1304 got_offset = this->last_got_offset();
1305 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1306 }
1307
1308 this->set_got_size();
1309 }
1310
1311 template<int size, bool big_endian>
1312 void
1313 Output_data_got<size, big_endian>::add_local_pair_with_rela(
1314 Sized_relobj<size, big_endian>* object,
1315 unsigned int symndx,
1316 unsigned int shndx,
1317 unsigned int got_type,
1318 Rela_dyn* rela_dyn,
1319 unsigned int r_type_1,
1320 unsigned int r_type_2)
1321 {
1322 if (object->local_has_got_offset(symndx, got_type))
1323 return;
1324
1325 this->entries_.push_back(Got_entry());
1326 unsigned int got_offset = this->last_got_offset();
1327 object->set_local_got_offset(symndx, got_type, got_offset);
1328 section_offset_type off;
1329 Output_section* os = object->output_section(shndx, &off);
1330 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
1331
1332 this->entries_.push_back(Got_entry(object, symndx));
1333 if (r_type_2 != 0)
1334 {
1335 got_offset = this->last_got_offset();
1336 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1337 }
1338
1339 this->set_got_size();
1340 }
1341
1342 // Write out the GOT.
1343
1344 template<int size, bool big_endian>
1345 void
1346 Output_data_got<size, big_endian>::do_write(Output_file* of)
1347 {
1348 const int add = size / 8;
1349
1350 const off_t off = this->offset();
1351 const off_t oview_size = this->data_size();
1352 unsigned char* const oview = of->get_output_view(off, oview_size);
1353
1354 unsigned char* pov = oview;
1355 for (typename Got_entries::const_iterator p = this->entries_.begin();
1356 p != this->entries_.end();
1357 ++p)
1358 {
1359 p->write(pov);
1360 pov += add;
1361 }
1362
1363 gold_assert(pov - oview == oview_size);
1364
1365 of->write_output_view(off, oview_size, oview);
1366
1367 // We no longer need the GOT entries.
1368 this->entries_.clear();
1369 }
1370
1371 // Output_data_dynamic::Dynamic_entry methods.
1372
1373 // Write out the entry.
1374
1375 template<int size, bool big_endian>
1376 void
1377 Output_data_dynamic::Dynamic_entry::write(
1378 unsigned char* pov,
1379 const Stringpool* pool) const
1380 {
1381 typename elfcpp::Elf_types<size>::Elf_WXword val;
1382 switch (this->offset_)
1383 {
1384 case DYNAMIC_NUMBER:
1385 val = this->u_.val;
1386 break;
1387
1388 case DYNAMIC_SECTION_SIZE:
1389 val = this->u_.od->data_size();
1390 break;
1391
1392 case DYNAMIC_SYMBOL:
1393 {
1394 const Sized_symbol<size>* s =
1395 static_cast<const Sized_symbol<size>*>(this->u_.sym);
1396 val = s->value();
1397 }
1398 break;
1399
1400 case DYNAMIC_STRING:
1401 val = pool->get_offset(this->u_.str);
1402 break;
1403
1404 default:
1405 val = this->u_.od->address() + this->offset_;
1406 break;
1407 }
1408
1409 elfcpp::Dyn_write<size, big_endian> dw(pov);
1410 dw.put_d_tag(this->tag_);
1411 dw.put_d_val(val);
1412 }
1413
1414 // Output_data_dynamic methods.
1415
1416 // Adjust the output section to set the entry size.
1417
1418 void
1419 Output_data_dynamic::do_adjust_output_section(Output_section* os)
1420 {
1421 if (parameters->target().get_size() == 32)
1422 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
1423 else if (parameters->target().get_size() == 64)
1424 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1425 else
1426 gold_unreachable();
1427 }
1428
1429 // Set the final data size.
1430
1431 void
1432 Output_data_dynamic::set_final_data_size()
1433 {
1434 // Add the terminating entry.
1435 this->add_constant(elfcpp::DT_NULL, 0);
1436
1437 int dyn_size;
1438 if (parameters->target().get_size() == 32)
1439 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
1440 else if (parameters->target().get_size() == 64)
1441 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1442 else
1443 gold_unreachable();
1444 this->set_data_size(this->entries_.size() * dyn_size);
1445 }
1446
1447 // Write out the dynamic entries.
1448
1449 void
1450 Output_data_dynamic::do_write(Output_file* of)
1451 {
1452 switch (parameters->size_and_endianness())
1453 {
1454 #ifdef HAVE_TARGET_32_LITTLE
1455 case Parameters::TARGET_32_LITTLE:
1456 this->sized_write<32, false>(of);
1457 break;
1458 #endif
1459 #ifdef HAVE_TARGET_32_BIG
1460 case Parameters::TARGET_32_BIG:
1461 this->sized_write<32, true>(of);
1462 break;
1463 #endif
1464 #ifdef HAVE_TARGET_64_LITTLE
1465 case Parameters::TARGET_64_LITTLE:
1466 this->sized_write<64, false>(of);
1467 break;
1468 #endif
1469 #ifdef HAVE_TARGET_64_BIG
1470 case Parameters::TARGET_64_BIG:
1471 this->sized_write<64, true>(of);
1472 break;
1473 #endif
1474 default:
1475 gold_unreachable();
1476 }
1477 }
1478
1479 template<int size, bool big_endian>
1480 void
1481 Output_data_dynamic::sized_write(Output_file* of)
1482 {
1483 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1484
1485 const off_t offset = this->offset();
1486 const off_t oview_size = this->data_size();
1487 unsigned char* const oview = of->get_output_view(offset, oview_size);
1488
1489 unsigned char* pov = oview;
1490 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1491 p != this->entries_.end();
1492 ++p)
1493 {
1494 p->write<size, big_endian>(pov, this->pool_);
1495 pov += dyn_size;
1496 }
1497
1498 gold_assert(pov - oview == oview_size);
1499
1500 of->write_output_view(offset, oview_size, oview);
1501
1502 // We no longer need the dynamic entries.
1503 this->entries_.clear();
1504 }
1505
1506 // Class Output_symtab_xindex.
1507
1508 void
1509 Output_symtab_xindex::do_write(Output_file* of)
1510 {
1511 const off_t offset = this->offset();
1512 const off_t oview_size = this->data_size();
1513 unsigned char* const oview = of->get_output_view(offset, oview_size);
1514
1515 memset(oview, 0, oview_size);
1516
1517 if (parameters->target().is_big_endian())
1518 this->endian_do_write<true>(oview);
1519 else
1520 this->endian_do_write<false>(oview);
1521
1522 of->write_output_view(offset, oview_size, oview);
1523
1524 // We no longer need the data.
1525 this->entries_.clear();
1526 }
1527
1528 template<bool big_endian>
1529 void
1530 Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1531 {
1532 for (Xindex_entries::const_iterator p = this->entries_.begin();
1533 p != this->entries_.end();
1534 ++p)
1535 elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
1536 }
1537
1538 // Output_section::Input_section methods.
1539
1540 // Return the data size. For an input section we store the size here.
1541 // For an Output_section_data, we have to ask it for the size.
1542
1543 off_t
1544 Output_section::Input_section::data_size() const
1545 {
1546 if (this->is_input_section())
1547 return this->u1_.data_size;
1548 else
1549 return this->u2_.posd->data_size();
1550 }
1551
1552 // Set the address and file offset.
1553
1554 void
1555 Output_section::Input_section::set_address_and_file_offset(
1556 uint64_t address,
1557 off_t file_offset,
1558 off_t section_file_offset)
1559 {
1560 if (this->is_input_section())
1561 this->u2_.object->set_section_offset(this->shndx_,
1562 file_offset - section_file_offset);
1563 else
1564 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1565 }
1566
1567 // Reset the address and file offset.
1568
1569 void
1570 Output_section::Input_section::reset_address_and_file_offset()
1571 {
1572 if (!this->is_input_section())
1573 this->u2_.posd->reset_address_and_file_offset();
1574 }
1575
1576 // Finalize the data size.
1577
1578 void
1579 Output_section::Input_section::finalize_data_size()
1580 {
1581 if (!this->is_input_section())
1582 this->u2_.posd->finalize_data_size();
1583 }
1584
1585 // Try to turn an input offset into an output offset. We want to
1586 // return the output offset relative to the start of this
1587 // Input_section in the output section.
1588
1589 inline bool
1590 Output_section::Input_section::output_offset(
1591 const Relobj* object,
1592 unsigned int shndx,
1593 section_offset_type offset,
1594 section_offset_type *poutput) const
1595 {
1596 if (!this->is_input_section())
1597 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
1598 else
1599 {
1600 if (this->shndx_ != shndx || this->u2_.object != object)
1601 return false;
1602 *poutput = offset;
1603 return true;
1604 }
1605 }
1606
1607 // Return whether this is the merge section for the input section
1608 // SHNDX in OBJECT.
1609
1610 inline bool
1611 Output_section::Input_section::is_merge_section_for(const Relobj* object,
1612 unsigned int shndx) const
1613 {
1614 if (this->is_input_section())
1615 return false;
1616 return this->u2_.posd->is_merge_section_for(object, shndx);
1617 }
1618
1619 // Write out the data. We don't have to do anything for an input
1620 // section--they are handled via Object::relocate--but this is where
1621 // we write out the data for an Output_section_data.
1622
1623 void
1624 Output_section::Input_section::write(Output_file* of)
1625 {
1626 if (!this->is_input_section())
1627 this->u2_.posd->write(of);
1628 }
1629
1630 // Write the data to a buffer. As for write(), we don't have to do
1631 // anything for an input section.
1632
1633 void
1634 Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1635 {
1636 if (!this->is_input_section())
1637 this->u2_.posd->write_to_buffer(buffer);
1638 }
1639
1640 // Output_section methods.
1641
1642 // Construct an Output_section. NAME will point into a Stringpool.
1643
1644 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
1645 elfcpp::Elf_Xword flags)
1646 : name_(name),
1647 addralign_(0),
1648 entsize_(0),
1649 load_address_(0),
1650 link_section_(NULL),
1651 link_(0),
1652 info_section_(NULL),
1653 info_symndx_(NULL),
1654 info_(0),
1655 type_(type),
1656 flags_(flags),
1657 out_shndx_(-1U),
1658 symtab_index_(0),
1659 dynsym_index_(0),
1660 input_sections_(),
1661 first_input_offset_(0),
1662 fills_(),
1663 postprocessing_buffer_(NULL),
1664 needs_symtab_index_(false),
1665 needs_dynsym_index_(false),
1666 should_link_to_symtab_(false),
1667 should_link_to_dynsym_(false),
1668 after_input_sections_(false),
1669 requires_postprocessing_(false),
1670 found_in_sections_clause_(false),
1671 has_load_address_(false),
1672 info_uses_section_index_(false),
1673 may_sort_attached_input_sections_(false),
1674 must_sort_attached_input_sections_(false),
1675 attached_input_sections_are_sorted_(false),
1676 tls_offset_(0)
1677 {
1678 // An unallocated section has no address. Forcing this means that
1679 // we don't need special treatment for symbols defined in debug
1680 // sections.
1681 if ((flags & elfcpp::SHF_ALLOC) == 0)
1682 this->set_address(0);
1683 }
1684
1685 Output_section::~Output_section()
1686 {
1687 }
1688
1689 // Set the entry size.
1690
1691 void
1692 Output_section::set_entsize(uint64_t v)
1693 {
1694 if (this->entsize_ == 0)
1695 this->entsize_ = v;
1696 else
1697 gold_assert(this->entsize_ == v);
1698 }
1699
1700 // Add the input section SHNDX, with header SHDR, named SECNAME, in
1701 // OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1702 // relocation section which applies to this section, or 0 if none, or
1703 // -1U if more than one. Return the offset of the input section
1704 // within the output section. Return -1 if the input section will
1705 // receive special handling. In the normal case we don't always keep
1706 // track of input sections for an Output_section. Instead, each
1707 // Object keeps track of the Output_section for each of its input
1708 // sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1709 // track of input sections here; this is used when SECTIONS appears in
1710 // a linker script.
1711
1712 template<int size, bool big_endian>
1713 off_t
1714 Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1715 unsigned int shndx,
1716 const char* secname,
1717 const elfcpp::Shdr<size, big_endian>& shdr,
1718 unsigned int reloc_shndx,
1719 bool have_sections_script)
1720 {
1721 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1722 if ((addralign & (addralign - 1)) != 0)
1723 {
1724 object->error(_("invalid alignment %lu for section \"%s\""),
1725 static_cast<unsigned long>(addralign), secname);
1726 addralign = 1;
1727 }
1728
1729 if (addralign > this->addralign_)
1730 this->addralign_ = addralign;
1731
1732 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1733 this->update_flags_for_input_section(sh_flags);
1734
1735 uint64_t entsize = shdr.get_sh_entsize();
1736
1737 // .debug_str is a mergeable string section, but is not always so
1738 // marked by compilers. Mark manually here so we can optimize.
1739 if (strcmp(secname, ".debug_str") == 0)
1740 {
1741 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1742 entsize = 1;
1743 }
1744
1745 // If this is a SHF_MERGE section, we pass all the input sections to
1746 // a Output_data_merge. We don't try to handle relocations for such
1747 // a section.
1748 if ((sh_flags & elfcpp::SHF_MERGE) != 0
1749 && reloc_shndx == 0)
1750 {
1751 if (this->add_merge_input_section(object, shndx, sh_flags,
1752 entsize, addralign))
1753 {
1754 // Tell the relocation routines that they need to call the
1755 // output_offset method to determine the final address.
1756 return -1;
1757 }
1758 }
1759
1760 off_t offset_in_section = this->current_data_size_for_child();
1761 off_t aligned_offset_in_section = align_address(offset_in_section,
1762 addralign);
1763
1764 if (aligned_offset_in_section > offset_in_section
1765 && !have_sections_script
1766 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1767 && object->target()->has_code_fill())
1768 {
1769 // We need to add some fill data. Using fill_list_ when
1770 // possible is an optimization, since we will often have fill
1771 // sections without input sections.
1772 off_t fill_len = aligned_offset_in_section - offset_in_section;
1773 if (this->input_sections_.empty())
1774 this->fills_.push_back(Fill(offset_in_section, fill_len));
1775 else
1776 {
1777 // FIXME: When relaxing, the size needs to adjust to
1778 // maintain a constant alignment.
1779 std::string fill_data(object->target()->code_fill(fill_len));
1780 Output_data_const* odc = new Output_data_const(fill_data, 1);
1781 this->input_sections_.push_back(Input_section(odc));
1782 }
1783 }
1784
1785 this->set_current_data_size_for_child(aligned_offset_in_section
1786 + shdr.get_sh_size());
1787
1788 // We need to keep track of this section if we are already keeping
1789 // track of sections, or if we are relaxing. Also, if this is a
1790 // section which requires sorting, or which may require sorting in
1791 // the future, we keep track of the sections. FIXME: Add test for
1792 // relaxing.
1793 if (have_sections_script
1794 || !this->input_sections_.empty()
1795 || this->may_sort_attached_input_sections()
1796 || this->must_sort_attached_input_sections())
1797 this->input_sections_.push_back(Input_section(object, shndx,
1798 shdr.get_sh_size(),
1799 addralign));
1800
1801 return aligned_offset_in_section;
1802 }
1803
1804 // Add arbitrary data to an output section.
1805
1806 void
1807 Output_section::add_output_section_data(Output_section_data* posd)
1808 {
1809 Input_section inp(posd);
1810 this->add_output_section_data(&inp);
1811
1812 if (posd->is_data_size_valid())
1813 {
1814 off_t offset_in_section = this->current_data_size_for_child();
1815 off_t aligned_offset_in_section = align_address(offset_in_section,
1816 posd->addralign());
1817 this->set_current_data_size_for_child(aligned_offset_in_section
1818 + posd->data_size());
1819 }
1820 }
1821
1822 // Add arbitrary data to an output section by Input_section.
1823
1824 void
1825 Output_section::add_output_section_data(Input_section* inp)
1826 {
1827 if (this->input_sections_.empty())
1828 this->first_input_offset_ = this->current_data_size_for_child();
1829
1830 this->input_sections_.push_back(*inp);
1831
1832 uint64_t addralign = inp->addralign();
1833 if (addralign > this->addralign_)
1834 this->addralign_ = addralign;
1835
1836 inp->set_output_section(this);
1837 }
1838
1839 // Add a merge section to an output section.
1840
1841 void
1842 Output_section::add_output_merge_section(Output_section_data* posd,
1843 bool is_string, uint64_t entsize)
1844 {
1845 Input_section inp(posd, is_string, entsize);
1846 this->add_output_section_data(&inp);
1847 }
1848
1849 // Add an input section to a SHF_MERGE section.
1850
1851 bool
1852 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1853 uint64_t flags, uint64_t entsize,
1854 uint64_t addralign)
1855 {
1856 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1857
1858 // We only merge strings if the alignment is not more than the
1859 // character size. This could be handled, but it's unusual.
1860 if (is_string && addralign > entsize)
1861 return false;
1862
1863 Input_section_list::iterator p;
1864 for (p = this->input_sections_.begin();
1865 p != this->input_sections_.end();
1866 ++p)
1867 if (p->is_merge_section(is_string, entsize, addralign))
1868 {
1869 p->add_input_section(object, shndx);
1870 return true;
1871 }
1872
1873 // We handle the actual constant merging in Output_merge_data or
1874 // Output_merge_string_data.
1875 Output_section_data* posd;
1876 if (!is_string)
1877 posd = new Output_merge_data(entsize, addralign);
1878 else
1879 {
1880 switch (entsize)
1881 {
1882 case 1:
1883 posd = new Output_merge_string<char>(addralign);
1884 break;
1885 case 2:
1886 posd = new Output_merge_string<uint16_t>(addralign);
1887 break;
1888 case 4:
1889 posd = new Output_merge_string<uint32_t>(addralign);
1890 break;
1891 default:
1892 return false;
1893 }
1894 }
1895
1896 this->add_output_merge_section(posd, is_string, entsize);
1897 posd->add_input_section(object, shndx);
1898
1899 return true;
1900 }
1901
1902 // Given an address OFFSET relative to the start of input section
1903 // SHNDX in OBJECT, return whether this address is being included in
1904 // the final link. This should only be called if SHNDX in OBJECT has
1905 // a special mapping.
1906
1907 bool
1908 Output_section::is_input_address_mapped(const Relobj* object,
1909 unsigned int shndx,
1910 off_t offset) const
1911 {
1912 gold_assert(object->is_section_specially_mapped(shndx));
1913
1914 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1915 p != this->input_sections_.end();
1916 ++p)
1917 {
1918 section_offset_type output_offset;
1919 if (p->output_offset(object, shndx, offset, &output_offset))
1920 return output_offset != -1;
1921 }
1922
1923 // By default we assume that the address is mapped. This should
1924 // only be called after we have passed all sections to Layout. At
1925 // that point we should know what we are discarding.
1926 return true;
1927 }
1928
1929 // Given an address OFFSET relative to the start of input section
1930 // SHNDX in object OBJECT, return the output offset relative to the
1931 // start of the input section in the output section. This should only
1932 // be called if SHNDX in OBJECT has a special mapping.
1933
1934 section_offset_type
1935 Output_section::output_offset(const Relobj* object, unsigned int shndx,
1936 section_offset_type offset) const
1937 {
1938 gold_assert(object->is_section_specially_mapped(shndx));
1939 // This can only be called meaningfully when layout is complete.
1940 gold_assert(Output_data::is_layout_complete());
1941
1942 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1943 p != this->input_sections_.end();
1944 ++p)
1945 {
1946 section_offset_type output_offset;
1947 if (p->output_offset(object, shndx, offset, &output_offset))
1948 return output_offset;
1949 }
1950 gold_unreachable();
1951 }
1952
1953 // Return the output virtual address of OFFSET relative to the start
1954 // of input section SHNDX in object OBJECT.
1955
1956 uint64_t
1957 Output_section::output_address(const Relobj* object, unsigned int shndx,
1958 off_t offset) const
1959 {
1960 gold_assert(object->is_section_specially_mapped(shndx));
1961
1962 uint64_t addr = this->address() + this->first_input_offset_;
1963 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1964 p != this->input_sections_.end();
1965 ++p)
1966 {
1967 addr = align_address(addr, p->addralign());
1968 section_offset_type output_offset;
1969 if (p->output_offset(object, shndx, offset, &output_offset))
1970 {
1971 if (output_offset == -1)
1972 return -1U;
1973 return addr + output_offset;
1974 }
1975 addr += p->data_size();
1976 }
1977
1978 // If we get here, it means that we don't know the mapping for this
1979 // input section. This might happen in principle if
1980 // add_input_section were called before add_output_section_data.
1981 // But it should never actually happen.
1982
1983 gold_unreachable();
1984 }
1985
1986 // Return the output address of the start of the merged section for
1987 // input section SHNDX in object OBJECT.
1988
1989 uint64_t
1990 Output_section::starting_output_address(const Relobj* object,
1991 unsigned int shndx) const
1992 {
1993 gold_assert(object->is_section_specially_mapped(shndx));
1994
1995 uint64_t addr = this->address() + this->first_input_offset_;
1996 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1997 p != this->input_sections_.end();
1998 ++p)
1999 {
2000 addr = align_address(addr, p->addralign());
2001
2002 // It would be nice if we could use the existing output_offset
2003 // method to get the output offset of input offset 0.
2004 // Unfortunately we don't know for sure that input offset 0 is
2005 // mapped at all.
2006 if (p->is_merge_section_for(object, shndx))
2007 return addr;
2008
2009 addr += p->data_size();
2010 }
2011 gold_unreachable();
2012 }
2013
2014 // Set the data size of an Output_section. This is where we handle
2015 // setting the addresses of any Output_section_data objects.
2016
2017 void
2018 Output_section::set_final_data_size()
2019 {
2020 if (this->input_sections_.empty())
2021 {
2022 this->set_data_size(this->current_data_size_for_child());
2023 return;
2024 }
2025
2026 if (this->must_sort_attached_input_sections())
2027 this->sort_attached_input_sections();
2028
2029 uint64_t address = this->address();
2030 off_t startoff = this->offset();
2031 off_t off = startoff + this->first_input_offset_;
2032 for (Input_section_list::iterator p = this->input_sections_.begin();
2033 p != this->input_sections_.end();
2034 ++p)
2035 {
2036 off = align_address(off, p->addralign());
2037 p->set_address_and_file_offset(address + (off - startoff), off,
2038 startoff);
2039 off += p->data_size();
2040 }
2041
2042 this->set_data_size(off - startoff);
2043 }
2044
2045 // Reset the address and file offset.
2046
2047 void
2048 Output_section::do_reset_address_and_file_offset()
2049 {
2050 for (Input_section_list::iterator p = this->input_sections_.begin();
2051 p != this->input_sections_.end();
2052 ++p)
2053 p->reset_address_and_file_offset();
2054 }
2055
2056 // Set the TLS offset. Called only for SHT_TLS sections.
2057
2058 void
2059 Output_section::do_set_tls_offset(uint64_t tls_base)
2060 {
2061 this->tls_offset_ = this->address() - tls_base;
2062 }
2063
2064 // In a few cases we need to sort the input sections attached to an
2065 // output section. This is used to implement the type of constructor
2066 // priority ordering implemented by the GNU linker, in which the
2067 // priority becomes part of the section name and the sections are
2068 // sorted by name. We only do this for an output section if we see an
2069 // attached input section matching ".ctor.*", ".dtor.*",
2070 // ".init_array.*" or ".fini_array.*".
2071
2072 class Output_section::Input_section_sort_entry
2073 {
2074 public:
2075 Input_section_sort_entry()
2076 : input_section_(), index_(-1U), section_has_name_(false),
2077 section_name_()
2078 { }
2079
2080 Input_section_sort_entry(const Input_section& input_section,
2081 unsigned int index)
2082 : input_section_(input_section), index_(index),
2083 section_has_name_(input_section.is_input_section())
2084 {
2085 if (this->section_has_name_)
2086 {
2087 // This is only called single-threaded from Layout::finalize,
2088 // so it is OK to lock. Unfortunately we have no way to pass
2089 // in a Task token.
2090 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2091 Object* obj = input_section.relobj();
2092 Task_lock_obj<Object> tl(dummy_task, obj);
2093
2094 // This is a slow operation, which should be cached in
2095 // Layout::layout if this becomes a speed problem.
2096 this->section_name_ = obj->section_name(input_section.shndx());
2097 }
2098 }
2099
2100 // Return the Input_section.
2101 const Input_section&
2102 input_section() const
2103 {
2104 gold_assert(this->index_ != -1U);
2105 return this->input_section_;
2106 }
2107
2108 // The index of this entry in the original list. This is used to
2109 // make the sort stable.
2110 unsigned int
2111 index() const
2112 {
2113 gold_assert(this->index_ != -1U);
2114 return this->index_;
2115 }
2116
2117 // Whether there is a section name.
2118 bool
2119 section_has_name() const
2120 { return this->section_has_name_; }
2121
2122 // The section name.
2123 const std::string&
2124 section_name() const
2125 {
2126 gold_assert(this->section_has_name_);
2127 return this->section_name_;
2128 }
2129
2130 // Return true if the section name has a priority. This is assumed
2131 // to be true if it has a dot after the initial dot.
2132 bool
2133 has_priority() const
2134 {
2135 gold_assert(this->section_has_name_);
2136 return this->section_name_.find('.', 1);
2137 }
2138
2139 // Return true if this an input file whose base name matches
2140 // FILE_NAME. The base name must have an extension of ".o", and
2141 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2142 // This is to match crtbegin.o as well as crtbeginS.o without
2143 // getting confused by other possibilities. Overall matching the
2144 // file name this way is a dreadful hack, but the GNU linker does it
2145 // in order to better support gcc, and we need to be compatible.
2146 bool
2147 match_file_name(const char* match_file_name) const
2148 {
2149 const std::string& file_name(this->input_section_.relobj()->name());
2150 const char* base_name = lbasename(file_name.c_str());
2151 size_t match_len = strlen(match_file_name);
2152 if (strncmp(base_name, match_file_name, match_len) != 0)
2153 return false;
2154 size_t base_len = strlen(base_name);
2155 if (base_len != match_len + 2 && base_len != match_len + 3)
2156 return false;
2157 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2158 }
2159
2160 private:
2161 // The Input_section we are sorting.
2162 Input_section input_section_;
2163 // The index of this Input_section in the original list.
2164 unsigned int index_;
2165 // Whether this Input_section has a section name--it won't if this
2166 // is some random Output_section_data.
2167 bool section_has_name_;
2168 // The section name if there is one.
2169 std::string section_name_;
2170 };
2171
2172 // Return true if S1 should come before S2 in the output section.
2173
2174 bool
2175 Output_section::Input_section_sort_compare::operator()(
2176 const Output_section::Input_section_sort_entry& s1,
2177 const Output_section::Input_section_sort_entry& s2) const
2178 {
2179 // crtbegin.o must come first.
2180 bool s1_begin = s1.match_file_name("crtbegin");
2181 bool s2_begin = s2.match_file_name("crtbegin");
2182 if (s1_begin || s2_begin)
2183 {
2184 if (!s1_begin)
2185 return false;
2186 if (!s2_begin)
2187 return true;
2188 return s1.index() < s2.index();
2189 }
2190
2191 // crtend.o must come last.
2192 bool s1_end = s1.match_file_name("crtend");
2193 bool s2_end = s2.match_file_name("crtend");
2194 if (s1_end || s2_end)
2195 {
2196 if (!s1_end)
2197 return true;
2198 if (!s2_end)
2199 return false;
2200 return s1.index() < s2.index();
2201 }
2202
2203 // We sort all the sections with no names to the end.
2204 if (!s1.section_has_name() || !s2.section_has_name())
2205 {
2206 if (s1.section_has_name())
2207 return true;
2208 if (s2.section_has_name())
2209 return false;
2210 return s1.index() < s2.index();
2211 }
2212
2213 // A section with a priority follows a section without a priority.
2214 // The GNU linker does this for all but .init_array sections; until
2215 // further notice we'll assume that that is an mistake.
2216 bool s1_has_priority = s1.has_priority();
2217 bool s2_has_priority = s2.has_priority();
2218 if (s1_has_priority && !s2_has_priority)
2219 return false;
2220 if (!s1_has_priority && s2_has_priority)
2221 return true;
2222
2223 // Otherwise we sort by name.
2224 int compare = s1.section_name().compare(s2.section_name());
2225 if (compare != 0)
2226 return compare < 0;
2227
2228 // Otherwise we keep the input order.
2229 return s1.index() < s2.index();
2230 }
2231
2232 // Sort the input sections attached to an output section.
2233
2234 void
2235 Output_section::sort_attached_input_sections()
2236 {
2237 if (this->attached_input_sections_are_sorted_)
2238 return;
2239
2240 // The only thing we know about an input section is the object and
2241 // the section index. We need the section name. Recomputing this
2242 // is slow but this is an unusual case. If this becomes a speed
2243 // problem we can cache the names as required in Layout::layout.
2244
2245 // We start by building a larger vector holding a copy of each
2246 // Input_section, plus its current index in the list and its name.
2247 std::vector<Input_section_sort_entry> sort_list;
2248
2249 unsigned int i = 0;
2250 for (Input_section_list::iterator p = this->input_sections_.begin();
2251 p != this->input_sections_.end();
2252 ++p, ++i)
2253 sort_list.push_back(Input_section_sort_entry(*p, i));
2254
2255 // Sort the input sections.
2256 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2257
2258 // Copy the sorted input sections back to our list.
2259 this->input_sections_.clear();
2260 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2261 p != sort_list.end();
2262 ++p)
2263 this->input_sections_.push_back(p->input_section());
2264
2265 // Remember that we sorted the input sections, since we might get
2266 // called again.
2267 this->attached_input_sections_are_sorted_ = true;
2268 }
2269
2270 // Write the section header to *OSHDR.
2271
2272 template<int size, bool big_endian>
2273 void
2274 Output_section::write_header(const Layout* layout,
2275 const Stringpool* secnamepool,
2276 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2277 {
2278 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2279 oshdr->put_sh_type(this->type_);
2280
2281 elfcpp::Elf_Xword flags = this->flags_;
2282 if (this->info_section_ != NULL && this->info_uses_section_index_)
2283 flags |= elfcpp::SHF_INFO_LINK;
2284 oshdr->put_sh_flags(flags);
2285
2286 oshdr->put_sh_addr(this->address());
2287 oshdr->put_sh_offset(this->offset());
2288 oshdr->put_sh_size(this->data_size());
2289 if (this->link_section_ != NULL)
2290 oshdr->put_sh_link(this->link_section_->out_shndx());
2291 else if (this->should_link_to_symtab_)
2292 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2293 else if (this->should_link_to_dynsym_)
2294 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2295 else
2296 oshdr->put_sh_link(this->link_);
2297
2298 elfcpp::Elf_Word info;
2299 if (this->info_section_ != NULL)
2300 {
2301 if (this->info_uses_section_index_)
2302 info = this->info_section_->out_shndx();
2303 else
2304 info = this->info_section_->symtab_index();
2305 }
2306 else if (this->info_symndx_ != NULL)
2307 info = this->info_symndx_->symtab_index();
2308 else
2309 info = this->info_;
2310 oshdr->put_sh_info(info);
2311
2312 oshdr->put_sh_addralign(this->addralign_);
2313 oshdr->put_sh_entsize(this->entsize_);
2314 }
2315
2316 // Write out the data. For input sections the data is written out by
2317 // Object::relocate, but we have to handle Output_section_data objects
2318 // here.
2319
2320 void
2321 Output_section::do_write(Output_file* of)
2322 {
2323 gold_assert(!this->requires_postprocessing());
2324
2325 off_t output_section_file_offset = this->offset();
2326 for (Fill_list::iterator p = this->fills_.begin();
2327 p != this->fills_.end();
2328 ++p)
2329 {
2330 std::string fill_data(parameters->target().code_fill(p->length()));
2331 of->write(output_section_file_offset + p->section_offset(),
2332 fill_data.data(), fill_data.size());
2333 }
2334
2335 for (Input_section_list::iterator p = this->input_sections_.begin();
2336 p != this->input_sections_.end();
2337 ++p)
2338 p->write(of);
2339 }
2340
2341 // If a section requires postprocessing, create the buffer to use.
2342
2343 void
2344 Output_section::create_postprocessing_buffer()
2345 {
2346 gold_assert(this->requires_postprocessing());
2347
2348 if (this->postprocessing_buffer_ != NULL)
2349 return;
2350
2351 if (!this->input_sections_.empty())
2352 {
2353 off_t off = this->first_input_offset_;
2354 for (Input_section_list::iterator p = this->input_sections_.begin();
2355 p != this->input_sections_.end();
2356 ++p)
2357 {
2358 off = align_address(off, p->addralign());
2359 p->finalize_data_size();
2360 off += p->data_size();
2361 }
2362 this->set_current_data_size_for_child(off);
2363 }
2364
2365 off_t buffer_size = this->current_data_size_for_child();
2366 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2367 }
2368
2369 // Write all the data of an Output_section into the postprocessing
2370 // buffer. This is used for sections which require postprocessing,
2371 // such as compression. Input sections are handled by
2372 // Object::Relocate.
2373
2374 void
2375 Output_section::write_to_postprocessing_buffer()
2376 {
2377 gold_assert(this->requires_postprocessing());
2378
2379 unsigned char* buffer = this->postprocessing_buffer();
2380 for (Fill_list::iterator p = this->fills_.begin();
2381 p != this->fills_.end();
2382 ++p)
2383 {
2384 std::string fill_data(parameters->target().code_fill(p->length()));
2385 memcpy(buffer + p->section_offset(), fill_data.data(),
2386 fill_data.size());
2387 }
2388
2389 off_t off = this->first_input_offset_;
2390 for (Input_section_list::iterator p = this->input_sections_.begin();
2391 p != this->input_sections_.end();
2392 ++p)
2393 {
2394 off = align_address(off, p->addralign());
2395 p->write_to_buffer(buffer + off);
2396 off += p->data_size();
2397 }
2398 }
2399
2400 // Get the input sections for linker script processing. We leave
2401 // behind the Output_section_data entries. Note that this may be
2402 // slightly incorrect for merge sections. We will leave them behind,
2403 // but it is possible that the script says that they should follow
2404 // some other input sections, as in:
2405 // .rodata { *(.rodata) *(.rodata.cst*) }
2406 // For that matter, we don't handle this correctly:
2407 // .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2408 // With luck this will never matter.
2409
2410 uint64_t
2411 Output_section::get_input_sections(
2412 uint64_t address,
2413 const std::string& fill,
2414 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2415 {
2416 uint64_t orig_address = address;
2417
2418 address = align_address(address, this->addralign());
2419
2420 Input_section_list remaining;
2421 for (Input_section_list::iterator p = this->input_sections_.begin();
2422 p != this->input_sections_.end();
2423 ++p)
2424 {
2425 if (p->is_input_section())
2426 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2427 else
2428 {
2429 uint64_t aligned_address = align_address(address, p->addralign());
2430 if (aligned_address != address && !fill.empty())
2431 {
2432 section_size_type length =
2433 convert_to_section_size_type(aligned_address - address);
2434 std::string this_fill;
2435 this_fill.reserve(length);
2436 while (this_fill.length() + fill.length() <= length)
2437 this_fill += fill;
2438 if (this_fill.length() < length)
2439 this_fill.append(fill, 0, length - this_fill.length());
2440
2441 Output_section_data* posd = new Output_data_const(this_fill, 0);
2442 remaining.push_back(Input_section(posd));
2443 }
2444 address = aligned_address;
2445
2446 remaining.push_back(*p);
2447
2448 p->finalize_data_size();
2449 address += p->data_size();
2450 }
2451 }
2452
2453 this->input_sections_.swap(remaining);
2454 this->first_input_offset_ = 0;
2455
2456 uint64_t data_size = address - orig_address;
2457 this->set_current_data_size_for_child(data_size);
2458 return data_size;
2459 }
2460
2461 // Add an input section from a script.
2462
2463 void
2464 Output_section::add_input_section_for_script(Relobj* object,
2465 unsigned int shndx,
2466 off_t data_size,
2467 uint64_t addralign)
2468 {
2469 if (addralign > this->addralign_)
2470 this->addralign_ = addralign;
2471
2472 off_t offset_in_section = this->current_data_size_for_child();
2473 off_t aligned_offset_in_section = align_address(offset_in_section,
2474 addralign);
2475
2476 this->set_current_data_size_for_child(aligned_offset_in_section
2477 + data_size);
2478
2479 this->input_sections_.push_back(Input_section(object, shndx,
2480 data_size, addralign));
2481 }
2482
2483 // Print stats for merge sections to stderr.
2484
2485 void
2486 Output_section::print_merge_stats()
2487 {
2488 Input_section_list::iterator p;
2489 for (p = this->input_sections_.begin();
2490 p != this->input_sections_.end();
2491 ++p)
2492 p->print_merge_stats(this->name_);
2493 }
2494
2495 // Output segment methods.
2496
2497 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2498 : output_data_(),
2499 output_bss_(),
2500 vaddr_(0),
2501 paddr_(0),
2502 memsz_(0),
2503 max_align_(0),
2504 min_p_align_(0),
2505 offset_(0),
2506 filesz_(0),
2507 type_(type),
2508 flags_(flags),
2509 is_max_align_known_(false),
2510 are_addresses_set_(false)
2511 {
2512 }
2513
2514 // Add an Output_section to an Output_segment.
2515
2516 void
2517 Output_segment::add_output_section(Output_section* os,
2518 elfcpp::Elf_Word seg_flags,
2519 bool front)
2520 {
2521 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
2522 gold_assert(!this->is_max_align_known_);
2523
2524 // Update the segment flags.
2525 this->flags_ |= seg_flags;
2526
2527 Output_segment::Output_data_list* pdl;
2528 if (os->type() == elfcpp::SHT_NOBITS)
2529 pdl = &this->output_bss_;
2530 else
2531 pdl = &this->output_data_;
2532
2533 // So that PT_NOTE segments will work correctly, we need to ensure
2534 // that all SHT_NOTE sections are adjacent. This will normally
2535 // happen automatically, because all the SHT_NOTE input sections
2536 // will wind up in the same output section. However, it is possible
2537 // for multiple SHT_NOTE input sections to have different section
2538 // flags, and thus be in different output sections, but for the
2539 // different section flags to map into the same segment flags and
2540 // thus the same output segment.
2541
2542 // Note that while there may be many input sections in an output
2543 // section, there are normally only a few output sections in an
2544 // output segment. This loop is expected to be fast.
2545
2546 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
2547 {
2548 Output_segment::Output_data_list::iterator p = pdl->end();
2549 do
2550 {
2551 --p;
2552 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2553 {
2554 // We don't worry about the FRONT parameter.
2555 ++p;
2556 pdl->insert(p, os);
2557 return;
2558 }
2559 }
2560 while (p != pdl->begin());
2561 }
2562
2563 // Similarly, so that PT_TLS segments will work, we need to group
2564 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2565 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2566 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
2567 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2568 // and the PT_TLS segment -- we do this grouping only for the
2569 // PT_LOAD segment.
2570 if (this->type_ != elfcpp::PT_TLS
2571 && (os->flags() & elfcpp::SHF_TLS) != 0
2572 && !this->output_data_.empty())
2573 {
2574 pdl = &this->output_data_;
2575 bool nobits = os->type() == elfcpp::SHT_NOBITS;
2576 bool sawtls = false;
2577 Output_segment::Output_data_list::iterator p = pdl->end();
2578 do
2579 {
2580 --p;
2581 bool insert;
2582 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2583 {
2584 sawtls = true;
2585 // Put a NOBITS section after the first TLS section.
2586 // But a PROGBITS section after the first TLS/PROGBITS
2587 // section.
2588 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2589 }
2590 else
2591 {
2592 // If we've gone past the TLS sections, but we've seen a
2593 // TLS section, then we need to insert this section now.
2594 insert = sawtls;
2595 }
2596
2597 if (insert)
2598 {
2599 // We don't worry about the FRONT parameter.
2600 ++p;
2601 pdl->insert(p, os);
2602 return;
2603 }
2604 }
2605 while (p != pdl->begin());
2606
2607 // There are no TLS sections yet; put this one at the requested
2608 // location in the section list.
2609 }
2610
2611 if (front)
2612 pdl->push_front(os);
2613 else
2614 pdl->push_back(os);
2615 }
2616
2617 // Remove an Output_section from this segment. It is an error if it
2618 // is not present.
2619
2620 void
2621 Output_segment::remove_output_section(Output_section* os)
2622 {
2623 // We only need this for SHT_PROGBITS.
2624 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2625 for (Output_data_list::iterator p = this->output_data_.begin();
2626 p != this->output_data_.end();
2627 ++p)
2628 {
2629 if (*p == os)
2630 {
2631 this->output_data_.erase(p);
2632 return;
2633 }
2634 }
2635 gold_unreachable();
2636 }
2637
2638 // Add an Output_data (which is not an Output_section) to the start of
2639 // a segment.
2640
2641 void
2642 Output_segment::add_initial_output_data(Output_data* od)
2643 {
2644 gold_assert(!this->is_max_align_known_);
2645 this->output_data_.push_front(od);
2646 }
2647
2648 // Return the maximum alignment of the Output_data in Output_segment.
2649
2650 uint64_t
2651 Output_segment::maximum_alignment()
2652 {
2653 if (!this->is_max_align_known_)
2654 {
2655 uint64_t addralign;
2656
2657 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2658 if (addralign > this->max_align_)
2659 this->max_align_ = addralign;
2660
2661 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2662 if (addralign > this->max_align_)
2663 this->max_align_ = addralign;
2664
2665 this->is_max_align_known_ = true;
2666 }
2667
2668 return this->max_align_;
2669 }
2670
2671 // Return the maximum alignment of a list of Output_data.
2672
2673 uint64_t
2674 Output_segment::maximum_alignment_list(const Output_data_list* pdl)
2675 {
2676 uint64_t ret = 0;
2677 for (Output_data_list::const_iterator p = pdl->begin();
2678 p != pdl->end();
2679 ++p)
2680 {
2681 uint64_t addralign = (*p)->addralign();
2682 if (addralign > ret)
2683 ret = addralign;
2684 }
2685 return ret;
2686 }
2687
2688 // Return the number of dynamic relocs applied to this segment.
2689
2690 unsigned int
2691 Output_segment::dynamic_reloc_count() const
2692 {
2693 return (this->dynamic_reloc_count_list(&this->output_data_)
2694 + this->dynamic_reloc_count_list(&this->output_bss_));
2695 }
2696
2697 // Return the number of dynamic relocs applied to an Output_data_list.
2698
2699 unsigned int
2700 Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2701 {
2702 unsigned int count = 0;
2703 for (Output_data_list::const_iterator p = pdl->begin();
2704 p != pdl->end();
2705 ++p)
2706 count += (*p)->dynamic_reloc_count();
2707 return count;
2708 }
2709
2710 // Set the section addresses for an Output_segment. If RESET is true,
2711 // reset the addresses first. ADDR is the address and *POFF is the
2712 // file offset. Set the section indexes starting with *PSHNDX.
2713 // Return the address of the immediately following segment. Update
2714 // *POFF and *PSHNDX.
2715
2716 uint64_t
2717 Output_segment::set_section_addresses(const Layout* layout, bool reset,
2718 uint64_t addr, off_t* poff,
2719 unsigned int* pshndx)
2720 {
2721 gold_assert(this->type_ == elfcpp::PT_LOAD);
2722
2723 if (!reset && this->are_addresses_set_)
2724 {
2725 gold_assert(this->paddr_ == addr);
2726 addr = this->vaddr_;
2727 }
2728 else
2729 {
2730 this->vaddr_ = addr;
2731 this->paddr_ = addr;
2732 this->are_addresses_set_ = true;
2733 }
2734
2735 bool in_tls = false;
2736
2737 off_t orig_off = *poff;
2738 this->offset_ = orig_off;
2739
2740 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
2741 addr, poff, pshndx, &in_tls);
2742 this->filesz_ = *poff - orig_off;
2743
2744 off_t off = *poff;
2745
2746 uint64_t ret = this->set_section_list_addresses(layout, reset,
2747 &this->output_bss_,
2748 addr, poff, pshndx,
2749 &in_tls);
2750
2751 // If the last section was a TLS section, align upward to the
2752 // alignment of the TLS segment, so that the overall size of the TLS
2753 // segment is aligned.
2754 if (in_tls)
2755 {
2756 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2757 *poff = align_address(*poff, segment_align);
2758 }
2759
2760 this->memsz_ = *poff - orig_off;
2761
2762 // Ignore the file offset adjustments made by the BSS Output_data
2763 // objects.
2764 *poff = off;
2765
2766 return ret;
2767 }
2768
2769 // Set the addresses and file offsets in a list of Output_data
2770 // structures.
2771
2772 uint64_t
2773 Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
2774 Output_data_list* pdl,
2775 uint64_t addr, off_t* poff,
2776 unsigned int* pshndx,
2777 bool* in_tls)
2778 {
2779 off_t startoff = *poff;
2780
2781 off_t off = startoff;
2782 for (Output_data_list::iterator p = pdl->begin();
2783 p != pdl->end();
2784 ++p)
2785 {
2786 if (reset)
2787 (*p)->reset_address_and_file_offset();
2788
2789 // When using a linker script the section will most likely
2790 // already have an address.
2791 if (!(*p)->is_address_valid())
2792 {
2793 uint64_t align = (*p)->addralign();
2794
2795 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2796 {
2797 // Give the first TLS section the alignment of the
2798 // entire TLS segment. Otherwise the TLS segment as a
2799 // whole may be misaligned.
2800 if (!*in_tls)
2801 {
2802 Output_segment* tls_segment = layout->tls_segment();
2803 gold_assert(tls_segment != NULL);
2804 uint64_t segment_align = tls_segment->maximum_alignment();
2805 gold_assert(segment_align >= align);
2806 align = segment_align;
2807
2808 *in_tls = true;
2809 }
2810 }
2811 else
2812 {
2813 // If this is the first section after the TLS segment,
2814 // align it to at least the alignment of the TLS
2815 // segment, so that the size of the overall TLS segment
2816 // is aligned.
2817 if (*in_tls)
2818 {
2819 uint64_t segment_align =
2820 layout->tls_segment()->maximum_alignment();
2821 if (segment_align > align)
2822 align = segment_align;
2823
2824 *in_tls = false;
2825 }
2826 }
2827
2828 off = align_address(off, align);
2829 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2830 }
2831 else
2832 {
2833 // The script may have inserted a skip forward, but it
2834 // better not have moved backward.
2835 gold_assert((*p)->address() >= addr + (off - startoff));
2836 off += (*p)->address() - (addr + (off - startoff));
2837 (*p)->set_file_offset(off);
2838 (*p)->finalize_data_size();
2839 }
2840
2841 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2842 // section. Such a section does not affect the size of a
2843 // PT_LOAD segment.
2844 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2845 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2846 off += (*p)->data_size();
2847
2848 if ((*p)->is_section())
2849 {
2850 (*p)->set_out_shndx(*pshndx);
2851 ++*pshndx;
2852 }
2853 }
2854
2855 *poff = off;
2856 return addr + (off - startoff);
2857 }
2858
2859 // For a non-PT_LOAD segment, set the offset from the sections, if
2860 // any.
2861
2862 void
2863 Output_segment::set_offset()
2864 {
2865 gold_assert(this->type_ != elfcpp::PT_LOAD);
2866
2867 gold_assert(!this->are_addresses_set_);
2868
2869 if (this->output_data_.empty() && this->output_bss_.empty())
2870 {
2871 this->vaddr_ = 0;
2872 this->paddr_ = 0;
2873 this->are_addresses_set_ = true;
2874 this->memsz_ = 0;
2875 this->min_p_align_ = 0;
2876 this->offset_ = 0;
2877 this->filesz_ = 0;
2878 return;
2879 }
2880
2881 const Output_data* first;
2882 if (this->output_data_.empty())
2883 first = this->output_bss_.front();
2884 else
2885 first = this->output_data_.front();
2886 this->vaddr_ = first->address();
2887 this->paddr_ = (first->has_load_address()
2888 ? first->load_address()
2889 : this->vaddr_);
2890 this->are_addresses_set_ = true;
2891 this->offset_ = first->offset();
2892
2893 if (this->output_data_.empty())
2894 this->filesz_ = 0;
2895 else
2896 {
2897 const Output_data* last_data = this->output_data_.back();
2898 this->filesz_ = (last_data->address()
2899 + last_data->data_size()
2900 - this->vaddr_);
2901 }
2902
2903 const Output_data* last;
2904 if (this->output_bss_.empty())
2905 last = this->output_data_.back();
2906 else
2907 last = this->output_bss_.back();
2908 this->memsz_ = (last->address()
2909 + last->data_size()
2910 - this->vaddr_);
2911
2912 // If this is a TLS segment, align the memory size. The code in
2913 // set_section_list ensures that the section after the TLS segment
2914 // is aligned to give us room.
2915 if (this->type_ == elfcpp::PT_TLS)
2916 {
2917 uint64_t segment_align = this->maximum_alignment();
2918 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
2919 this->memsz_ = align_address(this->memsz_, segment_align);
2920 }
2921 }
2922
2923 // Set the TLS offsets of the sections in the PT_TLS segment.
2924
2925 void
2926 Output_segment::set_tls_offsets()
2927 {
2928 gold_assert(this->type_ == elfcpp::PT_TLS);
2929
2930 for (Output_data_list::iterator p = this->output_data_.begin();
2931 p != this->output_data_.end();
2932 ++p)
2933 (*p)->set_tls_offset(this->vaddr_);
2934
2935 for (Output_data_list::iterator p = this->output_bss_.begin();
2936 p != this->output_bss_.end();
2937 ++p)
2938 (*p)->set_tls_offset(this->vaddr_);
2939 }
2940
2941 // Return the address of the first section.
2942
2943 uint64_t
2944 Output_segment::first_section_load_address() const
2945 {
2946 for (Output_data_list::const_iterator p = this->output_data_.begin();
2947 p != this->output_data_.end();
2948 ++p)
2949 if ((*p)->is_section())
2950 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2951
2952 for (Output_data_list::const_iterator p = this->output_bss_.begin();
2953 p != this->output_bss_.end();
2954 ++p)
2955 if ((*p)->is_section())
2956 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2957
2958 gold_unreachable();
2959 }
2960
2961 // Return the number of Output_sections in an Output_segment.
2962
2963 unsigned int
2964 Output_segment::output_section_count() const
2965 {
2966 return (this->output_section_count_list(&this->output_data_)
2967 + this->output_section_count_list(&this->output_bss_));
2968 }
2969
2970 // Return the number of Output_sections in an Output_data_list.
2971
2972 unsigned int
2973 Output_segment::output_section_count_list(const Output_data_list* pdl) const
2974 {
2975 unsigned int count = 0;
2976 for (Output_data_list::const_iterator p = pdl->begin();
2977 p != pdl->end();
2978 ++p)
2979 {
2980 if ((*p)->is_section())
2981 ++count;
2982 }
2983 return count;
2984 }
2985
2986 // Return the section attached to the list segment with the lowest
2987 // load address. This is used when handling a PHDRS clause in a
2988 // linker script.
2989
2990 Output_section*
2991 Output_segment::section_with_lowest_load_address() const
2992 {
2993 Output_section* found = NULL;
2994 uint64_t found_lma = 0;
2995 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
2996
2997 Output_section* found_data = found;
2998 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
2999 if (found != found_data && found_data != NULL)
3000 {
3001 gold_error(_("nobits section %s may not precede progbits section %s "
3002 "in same segment"),
3003 found->name(), found_data->name());
3004 return NULL;
3005 }
3006
3007 return found;
3008 }
3009
3010 // Look through a list for a section with a lower load address.
3011
3012 void
3013 Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3014 Output_section** found,
3015 uint64_t* found_lma) const
3016 {
3017 for (Output_data_list::const_iterator p = pdl->begin();
3018 p != pdl->end();
3019 ++p)
3020 {
3021 if (!(*p)->is_section())
3022 continue;
3023 Output_section* os = static_cast<Output_section*>(*p);
3024 uint64_t lma = (os->has_load_address()
3025 ? os->load_address()
3026 : os->address());
3027 if (*found == NULL || lma < *found_lma)
3028 {
3029 *found = os;
3030 *found_lma = lma;
3031 }
3032 }
3033 }
3034
3035 // Write the segment data into *OPHDR.
3036
3037 template<int size, bool big_endian>
3038 void
3039 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
3040 {
3041 ophdr->put_p_type(this->type_);
3042 ophdr->put_p_offset(this->offset_);
3043 ophdr->put_p_vaddr(this->vaddr_);
3044 ophdr->put_p_paddr(this->paddr_);
3045 ophdr->put_p_filesz(this->filesz_);
3046 ophdr->put_p_memsz(this->memsz_);
3047 ophdr->put_p_flags(this->flags_);
3048 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
3049 }
3050
3051 // Write the section headers into V.
3052
3053 template<int size, bool big_endian>
3054 unsigned char*
3055 Output_segment::write_section_headers(const Layout* layout,
3056 const Stringpool* secnamepool,
3057 unsigned char* v,
3058 unsigned int *pshndx) const
3059 {
3060 // Every section that is attached to a segment must be attached to a
3061 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3062 // segments.
3063 if (this->type_ != elfcpp::PT_LOAD)
3064 return v;
3065
3066 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3067 &this->output_data_,
3068 v, pshndx);
3069 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3070 &this->output_bss_,
3071 v, pshndx);
3072 return v;
3073 }
3074
3075 template<int size, bool big_endian>
3076 unsigned char*
3077 Output_segment::write_section_headers_list(const Layout* layout,
3078 const Stringpool* secnamepool,
3079 const Output_data_list* pdl,
3080 unsigned char* v,
3081 unsigned int* pshndx) const
3082 {
3083 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3084 for (Output_data_list::const_iterator p = pdl->begin();
3085 p != pdl->end();
3086 ++p)
3087 {
3088 if ((*p)->is_section())
3089 {
3090 const Output_section* ps = static_cast<const Output_section*>(*p);
3091 gold_assert(*pshndx == ps->out_shndx());
3092 elfcpp::Shdr_write<size, big_endian> oshdr(v);
3093 ps->write_header(layout, secnamepool, &oshdr);
3094 v += shdr_size;
3095 ++*pshndx;
3096 }
3097 }
3098 return v;
3099 }
3100
3101 // Output_file methods.
3102
3103 Output_file::Output_file(const char* name)
3104 : name_(name),
3105 o_(-1),
3106 file_size_(0),
3107 base_(NULL),
3108 map_is_anonymous_(false),
3109 is_temporary_(false)
3110 {
3111 }
3112
3113 // Open the output file.
3114
3115 void
3116 Output_file::open(off_t file_size)
3117 {
3118 this->file_size_ = file_size;
3119
3120 // Unlink the file first; otherwise the open() may fail if the file
3121 // is busy (e.g. it's an executable that's currently being executed).
3122 //
3123 // However, the linker may be part of a system where a zero-length
3124 // file is created for it to write to, with tight permissions (gcc
3125 // 2.95 did something like this). Unlinking the file would work
3126 // around those permission controls, so we only unlink if the file
3127 // has a non-zero size. We also unlink only regular files to avoid
3128 // trouble with directories/etc.
3129 //
3130 // If we fail, continue; this command is merely a best-effort attempt
3131 // to improve the odds for open().
3132
3133 // We let the name "-" mean "stdout"
3134 if (!this->is_temporary_)
3135 {
3136 if (strcmp(this->name_, "-") == 0)
3137 this->o_ = STDOUT_FILENO;
3138 else
3139 {
3140 struct stat s;
3141 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
3142 unlink_if_ordinary(this->name_);
3143
3144 int mode = parameters->options().relocatable() ? 0666 : 0777;
3145 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
3146 if (o < 0)
3147 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3148 this->o_ = o;
3149 }
3150 }
3151
3152 this->map();
3153 }
3154
3155 // Resize the output file.
3156
3157 void
3158 Output_file::resize(off_t file_size)
3159 {
3160 // If the mmap is mapping an anonymous memory buffer, this is easy:
3161 // just mremap to the new size. If it's mapping to a file, we want
3162 // to unmap to flush to the file, then remap after growing the file.
3163 if (this->map_is_anonymous_)
3164 {
3165 void* base = ::mremap(this->base_, this->file_size_, file_size,
3166 MREMAP_MAYMOVE);
3167 if (base == MAP_FAILED)
3168 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3169 this->base_ = static_cast<unsigned char*>(base);
3170 this->file_size_ = file_size;
3171 }
3172 else
3173 {
3174 this->unmap();
3175 this->file_size_ = file_size;
3176 this->map();
3177 }
3178 }
3179
3180 // Map the file into memory.
3181
3182 void
3183 Output_file::map()
3184 {
3185 const int o = this->o_;
3186
3187 // If the output file is not a regular file, don't try to mmap it;
3188 // instead, we'll mmap a block of memory (an anonymous buffer), and
3189 // then later write the buffer to the file.
3190 void* base;
3191 struct stat statbuf;
3192 if (o == STDOUT_FILENO || o == STDERR_FILENO
3193 || ::fstat(o, &statbuf) != 0
3194 || !S_ISREG(statbuf.st_mode)
3195 || this->is_temporary_)
3196 {
3197 this->map_is_anonymous_ = true;
3198 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3199 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3200 }
3201 else
3202 {
3203 // Write out one byte to make the file the right size.
3204 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
3205 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
3206 char b = 0;
3207 if (::write(o, &b, 1) != 1)
3208 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
3209
3210 // Map the file into memory.
3211 this->map_is_anonymous_ = false;
3212 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3213 MAP_SHARED, o, 0);
3214 }
3215 if (base == MAP_FAILED)
3216 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
3217 this->base_ = static_cast<unsigned char*>(base);
3218 }
3219
3220 // Unmap the file from memory.
3221
3222 void
3223 Output_file::unmap()
3224 {
3225 if (::munmap(this->base_, this->file_size_) < 0)
3226 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
3227 this->base_ = NULL;
3228 }
3229
3230 // Close the output file.
3231
3232 void
3233 Output_file::close()
3234 {
3235 // If the map isn't file-backed, we need to write it now.
3236 if (this->map_is_anonymous_ && !this->is_temporary_)
3237 {
3238 size_t bytes_to_write = this->file_size_;
3239 while (bytes_to_write > 0)
3240 {
3241 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
3242 if (bytes_written == 0)
3243 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
3244 else if (bytes_written < 0)
3245 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
3246 else
3247 bytes_to_write -= bytes_written;
3248 }
3249 }
3250 this->unmap();
3251
3252 // We don't close stdout or stderr
3253 if (this->o_ != STDOUT_FILENO
3254 && this->o_ != STDERR_FILENO
3255 && !this->is_temporary_)
3256 if (::close(this->o_) < 0)
3257 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
3258 this->o_ = -1;
3259 }
3260
3261 // Instantiate the templates we need. We could use the configure
3262 // script to restrict this to only the ones for implemented targets.
3263
3264 #ifdef HAVE_TARGET_32_LITTLE
3265 template
3266 off_t
3267 Output_section::add_input_section<32, false>(
3268 Sized_relobj<32, false>* object,
3269 unsigned int shndx,
3270 const char* secname,
3271 const elfcpp::Shdr<32, false>& shdr,
3272 unsigned int reloc_shndx,
3273 bool have_sections_script);
3274 #endif
3275
3276 #ifdef HAVE_TARGET_32_BIG
3277 template
3278 off_t
3279 Output_section::add_input_section<32, true>(
3280 Sized_relobj<32, true>* object,
3281 unsigned int shndx,
3282 const char* secname,
3283 const elfcpp::Shdr<32, true>& shdr,
3284 unsigned int reloc_shndx,
3285 bool have_sections_script);
3286 #endif
3287
3288 #ifdef HAVE_TARGET_64_LITTLE
3289 template
3290 off_t
3291 Output_section::add_input_section<64, false>(
3292 Sized_relobj<64, false>* object,
3293 unsigned int shndx,
3294 const char* secname,
3295 const elfcpp::Shdr<64, false>& shdr,
3296 unsigned int reloc_shndx,
3297 bool have_sections_script);
3298 #endif
3299
3300 #ifdef HAVE_TARGET_64_BIG
3301 template
3302 off_t
3303 Output_section::add_input_section<64, true>(
3304 Sized_relobj<64, true>* object,
3305 unsigned int shndx,
3306 const char* secname,
3307 const elfcpp::Shdr<64, true>& shdr,
3308 unsigned int reloc_shndx,
3309 bool have_sections_script);
3310 #endif
3311
3312 #ifdef HAVE_TARGET_32_LITTLE
3313 template
3314 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
3315 #endif
3316
3317 #ifdef HAVE_TARGET_32_BIG
3318 template
3319 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
3320 #endif
3321
3322 #ifdef HAVE_TARGET_64_LITTLE
3323 template
3324 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
3325 #endif
3326
3327 #ifdef HAVE_TARGET_64_BIG
3328 template
3329 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
3330 #endif
3331
3332 #ifdef HAVE_TARGET_32_LITTLE
3333 template
3334 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
3335 #endif
3336
3337 #ifdef HAVE_TARGET_32_BIG
3338 template
3339 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
3340 #endif
3341
3342 #ifdef HAVE_TARGET_64_LITTLE
3343 template
3344 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
3345 #endif
3346
3347 #ifdef HAVE_TARGET_64_BIG
3348 template
3349 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
3350 #endif
3351
3352 #ifdef HAVE_TARGET_32_LITTLE
3353 template
3354 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
3355 #endif
3356
3357 #ifdef HAVE_TARGET_32_BIG
3358 template
3359 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
3360 #endif
3361
3362 #ifdef HAVE_TARGET_64_LITTLE
3363 template
3364 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
3365 #endif
3366
3367 #ifdef HAVE_TARGET_64_BIG
3368 template
3369 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
3370 #endif
3371
3372 #ifdef HAVE_TARGET_32_LITTLE
3373 template
3374 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
3375 #endif
3376
3377 #ifdef HAVE_TARGET_32_BIG
3378 template
3379 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
3380 #endif
3381
3382 #ifdef HAVE_TARGET_64_LITTLE
3383 template
3384 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
3385 #endif
3386
3387 #ifdef HAVE_TARGET_64_BIG
3388 template
3389 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
3390 #endif
3391
3392 #ifdef HAVE_TARGET_32_LITTLE
3393 template
3394 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3395 #endif
3396
3397 #ifdef HAVE_TARGET_32_BIG
3398 template
3399 class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3400 #endif
3401
3402 #ifdef HAVE_TARGET_64_LITTLE
3403 template
3404 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3405 #endif
3406
3407 #ifdef HAVE_TARGET_64_BIG
3408 template
3409 class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3410 #endif
3411
3412 #ifdef HAVE_TARGET_32_LITTLE
3413 template
3414 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3415 #endif
3416
3417 #ifdef HAVE_TARGET_32_BIG
3418 template
3419 class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3420 #endif
3421
3422 #ifdef HAVE_TARGET_64_LITTLE
3423 template
3424 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3425 #endif
3426
3427 #ifdef HAVE_TARGET_64_BIG
3428 template
3429 class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3430 #endif
3431
3432 #ifdef HAVE_TARGET_32_LITTLE
3433 template
3434 class Output_data_group<32, false>;
3435 #endif
3436
3437 #ifdef HAVE_TARGET_32_BIG
3438 template
3439 class Output_data_group<32, true>;
3440 #endif
3441
3442 #ifdef HAVE_TARGET_64_LITTLE
3443 template
3444 class Output_data_group<64, false>;
3445 #endif
3446
3447 #ifdef HAVE_TARGET_64_BIG
3448 template
3449 class Output_data_group<64, true>;
3450 #endif
3451
3452 #ifdef HAVE_TARGET_32_LITTLE
3453 template
3454 class Output_data_got<32, false>;
3455 #endif
3456
3457 #ifdef HAVE_TARGET_32_BIG
3458 template
3459 class Output_data_got<32, true>;
3460 #endif
3461
3462 #ifdef HAVE_TARGET_64_LITTLE
3463 template
3464 class Output_data_got<64, false>;
3465 #endif
3466
3467 #ifdef HAVE_TARGET_64_BIG
3468 template
3469 class Output_data_got<64, true>;
3470 #endif
3471
3472 } // End namespace gold.