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