Add licensing text to every source file.
[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 <algorithm>
31
32 #include "parameters.h"
33 #include "object.h"
34 #include "symtab.h"
35 #include "reloc.h"
36 #include "merge.h"
37 #include "output.h"
38
39 namespace gold
40 {
41
42 // Output_data variables.
43
44 bool Output_data::sizes_are_fixed;
45
46 // Output_data methods.
47
48 Output_data::~Output_data()
49 {
50 }
51
52 // Set the address and offset.
53
54 void
55 Output_data::set_address(uint64_t addr, off_t off)
56 {
57 this->address_ = addr;
58 this->offset_ = off;
59
60 // Let the child class know.
61 this->do_set_address(addr, off);
62 }
63
64 // Return the default alignment for a size--32 or 64.
65
66 uint64_t
67 Output_data::default_alignment(int size)
68 {
69 if (size == 32)
70 return 4;
71 else if (size == 64)
72 return 8;
73 else
74 gold_unreachable();
75 }
76
77 // Output_section_header methods. This currently assumes that the
78 // segment and section lists are complete at construction time.
79
80 Output_section_headers::Output_section_headers(
81 int size,
82 bool big_endian,
83 const Layout* layout,
84 const Layout::Segment_list* segment_list,
85 const Layout::Section_list* unattached_section_list,
86 const Stringpool* secnamepool)
87 : size_(size),
88 big_endian_(big_endian),
89 layout_(layout),
90 segment_list_(segment_list),
91 unattached_section_list_(unattached_section_list),
92 secnamepool_(secnamepool)
93 {
94 // Count all the sections. Start with 1 for the null section.
95 off_t count = 1;
96 for (Layout::Segment_list::const_iterator p = segment_list->begin();
97 p != segment_list->end();
98 ++p)
99 if ((*p)->type() == elfcpp::PT_LOAD)
100 count += (*p)->output_section_count();
101 count += unattached_section_list->size();
102
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 (this->size_ == 32)
120 {
121 if (this->big_endian_)
122 this->do_sized_write<32, true>(of);
123 else
124 this->do_sized_write<32, false>(of);
125 }
126 else if (this->size_ == 64)
127 {
128 if (this->big_endian_)
129 this->do_sized_write<64, true>(of);
130 else
131 this->do_sized_write<64, false>(of);
132 }
133 else
134 gold_unreachable();
135 }
136
137 template<int size, bool big_endian>
138 void
139 Output_section_headers::do_sized_write(Output_file* of)
140 {
141 off_t all_shdrs_size = this->data_size();
142 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
143
144 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
145 unsigned char* v = view;
146
147 {
148 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
149 oshdr.put_sh_name(0);
150 oshdr.put_sh_type(elfcpp::SHT_NULL);
151 oshdr.put_sh_flags(0);
152 oshdr.put_sh_addr(0);
153 oshdr.put_sh_offset(0);
154 oshdr.put_sh_size(0);
155 oshdr.put_sh_link(0);
156 oshdr.put_sh_info(0);
157 oshdr.put_sh_addralign(0);
158 oshdr.put_sh_entsize(0);
159 }
160
161 v += shdr_size;
162
163 unsigned shndx = 1;
164 for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
165 p != this->segment_list_->end();
166 ++p)
167 v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
168 this->layout_, this->secnamepool_, v, &shndx
169 SELECT_SIZE_ENDIAN(size, big_endian));
170 for (Layout::Section_list::const_iterator p =
171 this->unattached_section_list_->begin();
172 p != this->unattached_section_list_->end();
173 ++p)
174 {
175 gold_assert(shndx == (*p)->out_shndx());
176 elfcpp::Shdr_write<size, big_endian> oshdr(v);
177 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
178 v += shdr_size;
179 ++shndx;
180 }
181
182 of->write_output_view(this->offset(), all_shdrs_size, view);
183 }
184
185 // Output_segment_header methods.
186
187 Output_segment_headers::Output_segment_headers(
188 int size,
189 bool big_endian,
190 const Layout::Segment_list& segment_list)
191 : size_(size), big_endian_(big_endian), segment_list_(segment_list)
192 {
193 int phdr_size;
194 if (size == 32)
195 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
196 else if (size == 64)
197 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
198 else
199 gold_unreachable();
200
201 this->set_data_size(segment_list.size() * phdr_size);
202 }
203
204 void
205 Output_segment_headers::do_write(Output_file* of)
206 {
207 if (this->size_ == 32)
208 {
209 if (this->big_endian_)
210 this->do_sized_write<32, true>(of);
211 else
212 this->do_sized_write<32, false>(of);
213 }
214 else if (this->size_ == 64)
215 {
216 if (this->big_endian_)
217 this->do_sized_write<64, true>(of);
218 else
219 this->do_sized_write<64, false>(of);
220 }
221 else
222 gold_unreachable();
223 }
224
225 template<int size, bool big_endian>
226 void
227 Output_segment_headers::do_sized_write(Output_file* of)
228 {
229 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
230 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
231 unsigned char* view = of->get_output_view(this->offset(),
232 all_phdrs_size);
233 unsigned char* v = view;
234 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
235 p != this->segment_list_.end();
236 ++p)
237 {
238 elfcpp::Phdr_write<size, big_endian> ophdr(v);
239 (*p)->write_header(&ophdr);
240 v += phdr_size;
241 }
242
243 of->write_output_view(this->offset(), all_phdrs_size, view);
244 }
245
246 // Output_file_header methods.
247
248 Output_file_header::Output_file_header(int size,
249 bool big_endian,
250 const Target* target,
251 const Symbol_table* symtab,
252 const Output_segment_headers* osh)
253 : size_(size),
254 big_endian_(big_endian),
255 target_(target),
256 symtab_(symtab),
257 segment_header_(osh),
258 section_header_(NULL),
259 shstrtab_(NULL)
260 {
261 int ehdr_size;
262 if (size == 32)
263 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
264 else if (size == 64)
265 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
266 else
267 gold_unreachable();
268
269 this->set_data_size(ehdr_size);
270 }
271
272 // Set the section table information for a file header.
273
274 void
275 Output_file_header::set_section_info(const Output_section_headers* shdrs,
276 const Output_section* shstrtab)
277 {
278 this->section_header_ = shdrs;
279 this->shstrtab_ = shstrtab;
280 }
281
282 // Write out the file header.
283
284 void
285 Output_file_header::do_write(Output_file* of)
286 {
287 if (this->size_ == 32)
288 {
289 if (this->big_endian_)
290 this->do_sized_write<32, true>(of);
291 else
292 this->do_sized_write<32, false>(of);
293 }
294 else if (this->size_ == 64)
295 {
296 if (this->big_endian_)
297 this->do_sized_write<64, true>(of);
298 else
299 this->do_sized_write<64, false>(of);
300 }
301 else
302 gold_unreachable();
303 }
304
305 // Write out the file header with appropriate size and endianess.
306
307 template<int size, bool big_endian>
308 void
309 Output_file_header::do_sized_write(Output_file* of)
310 {
311 gold_assert(this->offset() == 0);
312
313 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
314 unsigned char* view = of->get_output_view(0, ehdr_size);
315 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
316
317 unsigned char e_ident[elfcpp::EI_NIDENT];
318 memset(e_ident, 0, elfcpp::EI_NIDENT);
319 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
320 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
321 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
322 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
323 if (size == 32)
324 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
325 else if (size == 64)
326 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
327 else
328 gold_unreachable();
329 e_ident[elfcpp::EI_DATA] = (big_endian
330 ? elfcpp::ELFDATA2MSB
331 : elfcpp::ELFDATA2LSB);
332 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
333 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
334 oehdr.put_e_ident(e_ident);
335
336 elfcpp::ET e_type;
337 // FIXME: ET_DYN.
338 if (parameters->output_is_object())
339 e_type = elfcpp::ET_REL;
340 else
341 e_type = elfcpp::ET_EXEC;
342 oehdr.put_e_type(e_type);
343
344 oehdr.put_e_machine(this->target_->machine_code());
345 oehdr.put_e_version(elfcpp::EV_CURRENT);
346
347 // FIXME: Need to support -e, and target specific entry symbol.
348 Symbol* sym = this->symtab_->lookup("_start");
349 typename Sized_symbol<size>::Value_type v;
350 if (sym == NULL)
351 v = 0;
352 else
353 {
354 Sized_symbol<size>* ssym;
355 ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
356 sym SELECT_SIZE(size));
357 v = ssym->value();
358 }
359 oehdr.put_e_entry(v);
360
361 oehdr.put_e_phoff(this->segment_header_->offset());
362 oehdr.put_e_shoff(this->section_header_->offset());
363
364 // FIXME: The target needs to set the flags.
365 oehdr.put_e_flags(0);
366
367 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
368 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
369 oehdr.put_e_phnum(this->segment_header_->data_size()
370 / elfcpp::Elf_sizes<size>::phdr_size);
371 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
372 oehdr.put_e_shnum(this->section_header_->data_size()
373 / elfcpp::Elf_sizes<size>::shdr_size);
374 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
375
376 of->write_output_view(0, ehdr_size, view);
377 }
378
379 // Output_data_const methods.
380
381 void
382 Output_data_const::do_write(Output_file* of)
383 {
384 of->write(this->offset(), this->data_.data(), this->data_.size());
385 }
386
387 // Output_data_const_buffer methods.
388
389 void
390 Output_data_const_buffer::do_write(Output_file* of)
391 {
392 of->write(this->offset(), this->p_, this->data_size());
393 }
394
395 // Output_section_data methods.
396
397 // Record the output section, and set the entry size and such.
398
399 void
400 Output_section_data::set_output_section(Output_section* os)
401 {
402 gold_assert(this->output_section_ == NULL);
403 this->output_section_ = os;
404 this->do_adjust_output_section(os);
405 }
406
407 // Return the section index of the output section.
408
409 unsigned int
410 Output_section_data::do_out_shndx() const
411 {
412 gold_assert(this->output_section_ != NULL);
413 return this->output_section_->out_shndx();
414 }
415
416 // Output_data_strtab methods.
417
418 // Set the address. We don't actually care about the address, but we
419 // do set our final size.
420
421 void
422 Output_data_strtab::do_set_address(uint64_t, off_t)
423 {
424 this->strtab_->set_string_offsets();
425 this->set_data_size(this->strtab_->get_strtab_size());
426 }
427
428 // Write out a string table.
429
430 void
431 Output_data_strtab::do_write(Output_file* of)
432 {
433 this->strtab_->write(of, this->offset());
434 }
435
436 // Output_reloc methods.
437
438 // Get the symbol index of a relocation.
439
440 template<bool dynamic, int size, bool big_endian>
441 unsigned int
442 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
443 const
444 {
445 unsigned int index;
446 switch (this->local_sym_index_)
447 {
448 case INVALID_CODE:
449 gold_unreachable();
450
451 case GSYM_CODE:
452 if (this->u1_.gsym == NULL)
453 index = 0;
454 else if (dynamic)
455 index = this->u1_.gsym->dynsym_index();
456 else
457 index = this->u1_.gsym->symtab_index();
458 break;
459
460 case SECTION_CODE:
461 if (dynamic)
462 index = this->u1_.os->dynsym_index();
463 else
464 index = this->u1_.os->symtab_index();
465 break;
466
467 default:
468 if (dynamic)
469 {
470 // FIXME: It seems that some targets may need to generate
471 // dynamic relocations against local symbols for some
472 // reasons. This will have to be addressed at some point.
473 gold_unreachable();
474 }
475 else
476 index = this->u1_.relobj->symtab_index(this->local_sym_index_);
477 break;
478 }
479 gold_assert(index != -1U);
480 return index;
481 }
482
483 // Write out the offset and info fields of a Rel or Rela relocation
484 // entry.
485
486 template<bool dynamic, int size, bool big_endian>
487 template<typename Write_rel>
488 void
489 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
490 Write_rel* wr) const
491 {
492 Address address = this->address_;
493 if (this->shndx_ != INVALID_CODE)
494 {
495 off_t off;
496 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
497 &off);
498 gold_assert(os != NULL);
499 address += os->address() + off;
500 }
501 else if (this->u2_.od != NULL)
502 address += this->u2_.od->address();
503 wr->put_r_offset(address);
504 wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(),
505 this->type_));
506 }
507
508 // Write out a Rel relocation.
509
510 template<bool dynamic, int size, bool big_endian>
511 void
512 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
513 unsigned char* pov) const
514 {
515 elfcpp::Rel_write<size, big_endian> orel(pov);
516 this->write_rel(&orel);
517 }
518
519 // Write out a Rela relocation.
520
521 template<bool dynamic, int size, bool big_endian>
522 void
523 Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
524 unsigned char* pov) const
525 {
526 elfcpp::Rela_write<size, big_endian> orel(pov);
527 this->rel_.write_rel(&orel);
528 orel.put_r_addend(this->addend_);
529 }
530
531 // Output_data_reloc_base methods.
532
533 // Adjust the output section.
534
535 template<int sh_type, bool dynamic, int size, bool big_endian>
536 void
537 Output_data_reloc_base<sh_type, dynamic, size, big_endian>
538 ::do_adjust_output_section(Output_section* os)
539 {
540 if (sh_type == elfcpp::SHT_REL)
541 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
542 else if (sh_type == elfcpp::SHT_RELA)
543 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
544 else
545 gold_unreachable();
546 if (dynamic)
547 os->set_should_link_to_dynsym();
548 else
549 os->set_should_link_to_symtab();
550 }
551
552 // Write out relocation data.
553
554 template<int sh_type, bool dynamic, int size, bool big_endian>
555 void
556 Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
557 Output_file* of)
558 {
559 const off_t off = this->offset();
560 const off_t oview_size = this->data_size();
561 unsigned char* const oview = of->get_output_view(off, oview_size);
562
563 unsigned char* pov = oview;
564 for (typename Relocs::const_iterator p = this->relocs_.begin();
565 p != this->relocs_.end();
566 ++p)
567 {
568 p->write(pov);
569 pov += reloc_size;
570 }
571
572 gold_assert(pov - oview == oview_size);
573
574 of->write_output_view(off, oview_size, oview);
575
576 // We no longer need the relocation entries.
577 this->relocs_.clear();
578 }
579
580 // Output_data_got::Got_entry methods.
581
582 // Write out the entry.
583
584 template<int size, bool big_endian>
585 void
586 Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
587 {
588 Valtype val = 0;
589
590 switch (this->local_sym_index_)
591 {
592 case GSYM_CODE:
593 {
594 Symbol* gsym = this->u_.gsym;
595
596 // If the symbol is resolved locally, we need to write out its
597 // value. Otherwise we just write zero. The target code is
598 // responsible for creating a relocation entry to fill in the
599 // value at runtime.
600 if (gsym->final_value_is_known())
601 {
602 Sized_symbol<size>* sgsym;
603 // This cast is a bit ugly. We don't want to put a
604 // virtual method in Symbol, because we want Symbol to be
605 // as small as possible.
606 sgsym = static_cast<Sized_symbol<size>*>(gsym);
607 val = sgsym->value();
608 }
609 }
610 break;
611
612 case CONSTANT_CODE:
613 val = this->u_.constant;
614 break;
615
616 default:
617 gold_unreachable();
618 }
619
620 elfcpp::Swap<size, big_endian>::writeval(pov, val);
621 }
622
623 // Output_data_got methods.
624
625 // Add an entry for a global symbol to the GOT. This returns true if
626 // this is a new GOT entry, false if the symbol already had a GOT
627 // entry.
628
629 template<int size, bool big_endian>
630 bool
631 Output_data_got<size, big_endian>::add_global(Symbol* gsym)
632 {
633 if (gsym->has_got_offset())
634 return false;
635
636 this->entries_.push_back(Got_entry(gsym));
637 this->set_got_size();
638 gsym->set_got_offset(this->last_got_offset());
639 return true;
640 }
641
642 // Write out the GOT.
643
644 template<int size, bool big_endian>
645 void
646 Output_data_got<size, big_endian>::do_write(Output_file* of)
647 {
648 const int add = size / 8;
649
650 const off_t off = this->offset();
651 const off_t oview_size = this->data_size();
652 unsigned char* const oview = of->get_output_view(off, oview_size);
653
654 unsigned char* pov = oview;
655 for (typename Got_entries::const_iterator p = this->entries_.begin();
656 p != this->entries_.end();
657 ++p)
658 {
659 p->write(pov);
660 pov += add;
661 }
662
663 gold_assert(pov - oview == oview_size);
664
665 of->write_output_view(off, oview_size, oview);
666
667 // We no longer need the GOT entries.
668 this->entries_.clear();
669 }
670
671 // Output_data_dynamic::Dynamic_entry methods.
672
673 // Write out the entry.
674
675 template<int size, bool big_endian>
676 void
677 Output_data_dynamic::Dynamic_entry::write(
678 unsigned char* pov,
679 const Stringpool* pool
680 ACCEPT_SIZE_ENDIAN) const
681 {
682 typename elfcpp::Elf_types<size>::Elf_WXword val;
683 switch (this->classification_)
684 {
685 case DYNAMIC_NUMBER:
686 val = this->u_.val;
687 break;
688
689 case DYNAMIC_SECTION_ADDRESS:
690 val = this->u_.od->address();
691 break;
692
693 case DYNAMIC_SECTION_SIZE:
694 val = this->u_.od->data_size();
695 break;
696
697 case DYNAMIC_SYMBOL:
698 {
699 const Sized_symbol<size>* s =
700 static_cast<const Sized_symbol<size>*>(this->u_.sym);
701 val = s->value();
702 }
703 break;
704
705 case DYNAMIC_STRING:
706 val = pool->get_offset(this->u_.str);
707 break;
708
709 default:
710 gold_unreachable();
711 }
712
713 elfcpp::Dyn_write<size, big_endian> dw(pov);
714 dw.put_d_tag(this->tag_);
715 dw.put_d_val(val);
716 }
717
718 // Output_data_dynamic methods.
719
720 // Adjust the output section to set the entry size.
721
722 void
723 Output_data_dynamic::do_adjust_output_section(Output_section* os)
724 {
725 if (this->target_->get_size() == 32)
726 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
727 else if (this->target_->get_size() == 64)
728 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
729 else
730 gold_unreachable();
731 }
732
733 // Set the final data size.
734
735 void
736 Output_data_dynamic::do_set_address(uint64_t, off_t)
737 {
738 // Add the terminating entry.
739 this->add_constant(elfcpp::DT_NULL, 0);
740
741 int dyn_size;
742 if (this->target_->get_size() == 32)
743 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
744 else if (this->target_->get_size() == 64)
745 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
746 else
747 gold_unreachable();
748 this->set_data_size(this->entries_.size() * dyn_size);
749 }
750
751 // Write out the dynamic entries.
752
753 void
754 Output_data_dynamic::do_write(Output_file* of)
755 {
756 if (this->target_->get_size() == 32)
757 {
758 if (this->target_->is_big_endian())
759 this->sized_write<32, true>(of);
760 else
761 this->sized_write<32, false>(of);
762 }
763 else if (this->target_->get_size() == 64)
764 {
765 if (this->target_->is_big_endian())
766 this->sized_write<64, true>(of);
767 else
768 this->sized_write<64, false>(of);
769 }
770 else
771 gold_unreachable();
772 }
773
774 template<int size, bool big_endian>
775 void
776 Output_data_dynamic::sized_write(Output_file* of)
777 {
778 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
779
780 const off_t offset = this->offset();
781 const off_t oview_size = this->data_size();
782 unsigned char* const oview = of->get_output_view(offset, oview_size);
783
784 unsigned char* pov = oview;
785 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
786 p != this->entries_.end();
787 ++p)
788 {
789 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
790 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
791 pov += dyn_size;
792 }
793
794 gold_assert(pov - oview == oview_size);
795
796 of->write_output_view(offset, oview_size, oview);
797
798 // We no longer need the dynamic entries.
799 this->entries_.clear();
800 }
801
802 // Output_section::Input_section methods.
803
804 // Return the data size. For an input section we store the size here.
805 // For an Output_section_data, we have to ask it for the size.
806
807 off_t
808 Output_section::Input_section::data_size() const
809 {
810 if (this->is_input_section())
811 return this->u1_.data_size;
812 else
813 return this->u2_.posd->data_size();
814 }
815
816 // Set the address and file offset.
817
818 void
819 Output_section::Input_section::set_address(uint64_t addr, off_t off,
820 off_t secoff)
821 {
822 if (this->is_input_section())
823 this->u2_.object->set_section_offset(this->shndx_, off - secoff);
824 else
825 this->u2_.posd->set_address(addr, off);
826 }
827
828 // Try to turn an input address into an output address.
829
830 bool
831 Output_section::Input_section::output_address(const Relobj* object,
832 unsigned int shndx,
833 off_t offset,
834 uint64_t output_section_address,
835 uint64_t *poutput) const
836 {
837 if (!this->is_input_section())
838 return this->u2_.posd->output_address(object, shndx, offset,
839 output_section_address, poutput);
840 else
841 {
842 if (this->u2_.object != object)
843 return false;
844 off_t output_offset;
845 Output_section* os = object->output_section(shndx, &output_offset);
846 gold_assert(os != NULL);
847 *poutput = output_section_address + output_offset + offset;
848 return true;
849 }
850 }
851
852 // Write out the data. We don't have to do anything for an input
853 // section--they are handled via Object::relocate--but this is where
854 // we write out the data for an Output_section_data.
855
856 void
857 Output_section::Input_section::write(Output_file* of)
858 {
859 if (!this->is_input_section())
860 this->u2_.posd->write(of);
861 }
862
863 // Output_section methods.
864
865 // Construct an Output_section. NAME will point into a Stringpool.
866
867 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
868 elfcpp::Elf_Xword flags)
869 : name_(name),
870 addralign_(0),
871 entsize_(0),
872 link_section_(NULL),
873 link_(0),
874 info_section_(NULL),
875 info_(0),
876 type_(type),
877 flags_(flags),
878 out_shndx_(0),
879 symtab_index_(0),
880 dynsym_index_(0),
881 input_sections_(),
882 first_input_offset_(0),
883 fills_(),
884 needs_symtab_index_(false),
885 needs_dynsym_index_(false),
886 should_link_to_symtab_(false),
887 should_link_to_dynsym_(false)
888 {
889 }
890
891 Output_section::~Output_section()
892 {
893 }
894
895 // Set the entry size.
896
897 void
898 Output_section::set_entsize(uint64_t v)
899 {
900 if (this->entsize_ == 0)
901 this->entsize_ = v;
902 else
903 gold_assert(this->entsize_ == v);
904 }
905
906 // Add the input section SHNDX, with header SHDR, named SECNAME, in
907 // OBJECT, to the Output_section. Return the offset of the input
908 // section within the output section. We don't always keep track of
909 // input sections for an Output_section. Instead, each Object keeps
910 // track of the Output_section for each of its input sections.
911
912 template<int size, bool big_endian>
913 off_t
914 Output_section::add_input_section(Relobj* object, unsigned int shndx,
915 const char* secname,
916 const elfcpp::Shdr<size, big_endian>& shdr)
917 {
918 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
919 if ((addralign & (addralign - 1)) != 0)
920 {
921 fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
922 program_name, object->name().c_str(),
923 static_cast<unsigned long>(addralign), secname);
924 gold_exit(false);
925 }
926
927 if (addralign > this->addralign_)
928 this->addralign_ = addralign;
929
930 // If this is a SHF_MERGE section, we pass all the input sections to
931 // a Output_data_merge.
932 if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0)
933 {
934 if (this->add_merge_input_section(object, shndx, shdr.get_sh_flags(),
935 shdr.get_sh_entsize(),
936 addralign))
937 {
938 // Tell the relocation routines that they need to call the
939 // output_address method to determine the final address.
940 return -1;
941 }
942 }
943
944 off_t offset_in_section = this->data_size();
945 off_t aligned_offset_in_section = align_address(offset_in_section,
946 addralign);
947
948 if (aligned_offset_in_section > offset_in_section
949 && (shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0
950 && object->target()->has_code_fill())
951 {
952 // We need to add some fill data. Using fill_list_ when
953 // possible is an optimization, since we will often have fill
954 // sections without input sections.
955 off_t fill_len = aligned_offset_in_section - offset_in_section;
956 if (this->input_sections_.empty())
957 this->fills_.push_back(Fill(offset_in_section, fill_len));
958 else
959 {
960 // FIXME: When relaxing, the size needs to adjust to
961 // maintain a constant alignment.
962 std::string fill_data(object->target()->code_fill(fill_len));
963 Output_data_const* odc = new Output_data_const(fill_data, 1);
964 this->input_sections_.push_back(Input_section(odc));
965 }
966 }
967
968 this->set_data_size(aligned_offset_in_section + shdr.get_sh_size());
969
970 // We need to keep track of this section if we are already keeping
971 // track of sections, or if we are relaxing. FIXME: Add test for
972 // relaxing.
973 if (!this->input_sections_.empty())
974 this->input_sections_.push_back(Input_section(object, shndx,
975 shdr.get_sh_size(),
976 addralign));
977
978 return aligned_offset_in_section;
979 }
980
981 // Add arbitrary data to an output section.
982
983 void
984 Output_section::add_output_section_data(Output_section_data* posd)
985 {
986 Input_section inp(posd);
987 this->add_output_section_data(&inp);
988 }
989
990 // Add arbitrary data to an output section by Input_section.
991
992 void
993 Output_section::add_output_section_data(Input_section* inp)
994 {
995 if (this->input_sections_.empty())
996 this->first_input_offset_ = this->data_size();
997
998 this->input_sections_.push_back(*inp);
999
1000 uint64_t addralign = inp->addralign();
1001 if (addralign > this->addralign_)
1002 this->addralign_ = addralign;
1003
1004 inp->set_output_section(this);
1005 }
1006
1007 // Add a merge section to an output section.
1008
1009 void
1010 Output_section::add_output_merge_section(Output_section_data* posd,
1011 bool is_string, uint64_t entsize)
1012 {
1013 Input_section inp(posd, is_string, entsize);
1014 this->add_output_section_data(&inp);
1015 }
1016
1017 // Add an input section to a SHF_MERGE section.
1018
1019 bool
1020 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1021 uint64_t flags, uint64_t entsize,
1022 uint64_t addralign)
1023 {
1024 // We only merge constants if the alignment is not more than the
1025 // entry size. This could be handled, but it's unusual.
1026 if (addralign > entsize)
1027 return false;
1028
1029 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1030 Input_section_list::iterator p;
1031 for (p = this->input_sections_.begin();
1032 p != this->input_sections_.end();
1033 ++p)
1034 if (p->is_merge_section(is_string, entsize))
1035 break;
1036
1037 // We handle the actual constant merging in Output_merge_data or
1038 // Output_merge_string_data.
1039 if (p != this->input_sections_.end())
1040 p->add_input_section(object, shndx);
1041 else
1042 {
1043 Output_section_data* posd;
1044 if (!is_string)
1045 posd = new Output_merge_data(entsize);
1046 else if (entsize == 1)
1047 posd = new Output_merge_string<char>();
1048 else if (entsize == 2)
1049 posd = new Output_merge_string<uint16_t>();
1050 else if (entsize == 4)
1051 posd = new Output_merge_string<uint32_t>();
1052 else
1053 return false;
1054
1055 this->add_output_merge_section(posd, is_string, entsize);
1056 posd->add_input_section(object, shndx);
1057 }
1058
1059 return true;
1060 }
1061
1062 // Return the output virtual address of OFFSET relative to the start
1063 // of input section SHNDX in object OBJECT.
1064
1065 uint64_t
1066 Output_section::output_address(const Relobj* object, unsigned int shndx,
1067 off_t offset) const
1068 {
1069 uint64_t addr = this->address() + this->first_input_offset_;
1070 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1071 p != this->input_sections_.end();
1072 ++p)
1073 {
1074 addr = align_address(addr, p->addralign());
1075 uint64_t output;
1076 if (p->output_address(object, shndx, offset, addr, &output))
1077 return output;
1078 addr += p->data_size();
1079 }
1080
1081 // If we get here, it means that we don't know the mapping for this
1082 // input section. This might happen in principle if
1083 // add_input_section were called before add_output_section_data.
1084 // But it should never actually happen.
1085
1086 gold_unreachable();
1087 }
1088
1089 // Set the address of an Output_section. This is where we handle
1090 // setting the addresses of any Output_section_data objects.
1091
1092 void
1093 Output_section::do_set_address(uint64_t address, off_t startoff)
1094 {
1095 if (this->input_sections_.empty())
1096 return;
1097
1098 off_t off = startoff + this->first_input_offset_;
1099 for (Input_section_list::iterator p = this->input_sections_.begin();
1100 p != this->input_sections_.end();
1101 ++p)
1102 {
1103 off = align_address(off, p->addralign());
1104 p->set_address(address + (off - startoff), off, startoff);
1105 off += p->data_size();
1106 }
1107
1108 this->set_data_size(off - startoff);
1109 }
1110
1111 // Write the section header to *OSHDR.
1112
1113 template<int size, bool big_endian>
1114 void
1115 Output_section::write_header(const Layout* layout,
1116 const Stringpool* secnamepool,
1117 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1118 {
1119 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1120 oshdr->put_sh_type(this->type_);
1121 oshdr->put_sh_flags(this->flags_);
1122 oshdr->put_sh_addr(this->address());
1123 oshdr->put_sh_offset(this->offset());
1124 oshdr->put_sh_size(this->data_size());
1125 if (this->link_section_ != NULL)
1126 oshdr->put_sh_link(this->link_section_->out_shndx());
1127 else if (this->should_link_to_symtab_)
1128 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1129 else if (this->should_link_to_dynsym_)
1130 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1131 else
1132 oshdr->put_sh_link(this->link_);
1133 if (this->info_section_ != NULL)
1134 oshdr->put_sh_info(this->info_section_->out_shndx());
1135 else
1136 oshdr->put_sh_info(this->info_);
1137 oshdr->put_sh_addralign(this->addralign_);
1138 oshdr->put_sh_entsize(this->entsize_);
1139 }
1140
1141 // Write out the data. For input sections the data is written out by
1142 // Object::relocate, but we have to handle Output_section_data objects
1143 // here.
1144
1145 void
1146 Output_section::do_write(Output_file* of)
1147 {
1148 off_t output_section_file_offset = this->offset();
1149 for (Fill_list::iterator p = this->fills_.begin();
1150 p != this->fills_.end();
1151 ++p)
1152 {
1153 std::string fill_data(of->target()->code_fill(p->length()));
1154 of->write(output_section_file_offset + p->section_offset(),
1155 fill_data.data(), fill_data.size());
1156 }
1157
1158 for (Input_section_list::iterator p = this->input_sections_.begin();
1159 p != this->input_sections_.end();
1160 ++p)
1161 p->write(of);
1162 }
1163
1164 // Output segment methods.
1165
1166 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1167 : output_data_(),
1168 output_bss_(),
1169 vaddr_(0),
1170 paddr_(0),
1171 memsz_(0),
1172 align_(0),
1173 offset_(0),
1174 filesz_(0),
1175 type_(type),
1176 flags_(flags),
1177 is_align_known_(false)
1178 {
1179 }
1180
1181 // Add an Output_section to an Output_segment.
1182
1183 void
1184 Output_segment::add_output_section(Output_section* os,
1185 elfcpp::Elf_Word seg_flags,
1186 bool front)
1187 {
1188 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1189 gold_assert(!this->is_align_known_);
1190
1191 // Update the segment flags.
1192 this->flags_ |= seg_flags;
1193
1194 Output_segment::Output_data_list* pdl;
1195 if (os->type() == elfcpp::SHT_NOBITS)
1196 pdl = &this->output_bss_;
1197 else
1198 pdl = &this->output_data_;
1199
1200 // So that PT_NOTE segments will work correctly, we need to ensure
1201 // that all SHT_NOTE sections are adjacent. This will normally
1202 // happen automatically, because all the SHT_NOTE input sections
1203 // will wind up in the same output section. However, it is possible
1204 // for multiple SHT_NOTE input sections to have different section
1205 // flags, and thus be in different output sections, but for the
1206 // different section flags to map into the same segment flags and
1207 // thus the same output segment.
1208
1209 // Note that while there may be many input sections in an output
1210 // section, there are normally only a few output sections in an
1211 // output segment. This loop is expected to be fast.
1212
1213 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1214 {
1215 Output_segment::Output_data_list::iterator p = pdl->end();
1216 do
1217 {
1218 --p;
1219 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1220 {
1221 // We don't worry about the FRONT parameter.
1222 ++p;
1223 pdl->insert(p, os);
1224 return;
1225 }
1226 }
1227 while (p != pdl->begin());
1228 }
1229
1230 // Similarly, so that PT_TLS segments will work, we need to group
1231 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1232 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1233 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
1234 // correctly.
1235 if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
1236 {
1237 pdl = &this->output_data_;
1238 bool nobits = os->type() == elfcpp::SHT_NOBITS;
1239 bool sawtls = false;
1240 Output_segment::Output_data_list::iterator p = pdl->end();
1241 do
1242 {
1243 --p;
1244 bool insert;
1245 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1246 {
1247 sawtls = true;
1248 // Put a NOBITS section after the first TLS section.
1249 // But a PROGBITS section after the first TLS/PROGBITS
1250 // section.
1251 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1252 }
1253 else
1254 {
1255 // If we've gone past the TLS sections, but we've seen a
1256 // TLS section, then we need to insert this section now.
1257 insert = sawtls;
1258 }
1259
1260 if (insert)
1261 {
1262 // We don't worry about the FRONT parameter.
1263 ++p;
1264 pdl->insert(p, os);
1265 return;
1266 }
1267 }
1268 while (p != pdl->begin());
1269
1270 // There are no TLS sections yet; put this one at the requested
1271 // location in the section list.
1272 }
1273
1274 if (front)
1275 pdl->push_front(os);
1276 else
1277 pdl->push_back(os);
1278 }
1279
1280 // Add an Output_data (which is not an Output_section) to the start of
1281 // a segment.
1282
1283 void
1284 Output_segment::add_initial_output_data(Output_data* od)
1285 {
1286 gold_assert(!this->is_align_known_);
1287 this->output_data_.push_front(od);
1288 }
1289
1290 // Return the maximum alignment of the Output_data in Output_segment.
1291 // Once we compute this, we prohibit new sections from being added.
1292
1293 uint64_t
1294 Output_segment::addralign()
1295 {
1296 if (!this->is_align_known_)
1297 {
1298 uint64_t addralign;
1299
1300 addralign = Output_segment::maximum_alignment(&this->output_data_);
1301 if (addralign > this->align_)
1302 this->align_ = addralign;
1303
1304 addralign = Output_segment::maximum_alignment(&this->output_bss_);
1305 if (addralign > this->align_)
1306 this->align_ = addralign;
1307
1308 this->is_align_known_ = true;
1309 }
1310
1311 return this->align_;
1312 }
1313
1314 // Return the maximum alignment of a list of Output_data.
1315
1316 uint64_t
1317 Output_segment::maximum_alignment(const Output_data_list* pdl)
1318 {
1319 uint64_t ret = 0;
1320 for (Output_data_list::const_iterator p = pdl->begin();
1321 p != pdl->end();
1322 ++p)
1323 {
1324 uint64_t addralign = (*p)->addralign();
1325 if (addralign > ret)
1326 ret = addralign;
1327 }
1328 return ret;
1329 }
1330
1331 // Set the section addresses for an Output_segment. ADDR is the
1332 // address and *POFF is the file offset. Set the section indexes
1333 // starting with *PSHNDX. Return the address of the immediately
1334 // following segment. Update *POFF and *PSHNDX.
1335
1336 uint64_t
1337 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
1338 unsigned int* pshndx)
1339 {
1340 gold_assert(this->type_ == elfcpp::PT_LOAD);
1341
1342 this->vaddr_ = addr;
1343 this->paddr_ = addr;
1344
1345 off_t orig_off = *poff;
1346 this->offset_ = orig_off;
1347
1348 *poff = align_address(*poff, this->addralign());
1349
1350 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
1351 pshndx);
1352 this->filesz_ = *poff - orig_off;
1353
1354 off_t off = *poff;
1355
1356 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
1357 poff, pshndx);
1358 this->memsz_ = *poff - orig_off;
1359
1360 // Ignore the file offset adjustments made by the BSS Output_data
1361 // objects.
1362 *poff = off;
1363
1364 return ret;
1365 }
1366
1367 // Set the addresses and file offsets in a list of Output_data
1368 // structures.
1369
1370 uint64_t
1371 Output_segment::set_section_list_addresses(Output_data_list* pdl,
1372 uint64_t addr, off_t* poff,
1373 unsigned int* pshndx)
1374 {
1375 off_t startoff = *poff;
1376
1377 off_t off = startoff;
1378 for (Output_data_list::iterator p = pdl->begin();
1379 p != pdl->end();
1380 ++p)
1381 {
1382 off = align_address(off, (*p)->addralign());
1383 (*p)->set_address(addr + (off - startoff), off);
1384
1385 // Unless this is a PT_TLS segment, we want to ignore the size
1386 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
1387 // affect the size of a PT_LOAD segment.
1388 if (this->type_ == elfcpp::PT_TLS
1389 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
1390 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
1391 off += (*p)->data_size();
1392
1393 if ((*p)->is_section())
1394 {
1395 (*p)->set_out_shndx(*pshndx);
1396 ++*pshndx;
1397 }
1398 }
1399
1400 *poff = off;
1401 return addr + (off - startoff);
1402 }
1403
1404 // For a non-PT_LOAD segment, set the offset from the sections, if
1405 // any.
1406
1407 void
1408 Output_segment::set_offset()
1409 {
1410 gold_assert(this->type_ != elfcpp::PT_LOAD);
1411
1412 if (this->output_data_.empty() && this->output_bss_.empty())
1413 {
1414 this->vaddr_ = 0;
1415 this->paddr_ = 0;
1416 this->memsz_ = 0;
1417 this->align_ = 0;
1418 this->offset_ = 0;
1419 this->filesz_ = 0;
1420 return;
1421 }
1422
1423 const Output_data* first;
1424 if (this->output_data_.empty())
1425 first = this->output_bss_.front();
1426 else
1427 first = this->output_data_.front();
1428 this->vaddr_ = first->address();
1429 this->paddr_ = this->vaddr_;
1430 this->offset_ = first->offset();
1431
1432 if (this->output_data_.empty())
1433 this->filesz_ = 0;
1434 else
1435 {
1436 const Output_data* last_data = this->output_data_.back();
1437 this->filesz_ = (last_data->address()
1438 + last_data->data_size()
1439 - this->vaddr_);
1440 }
1441
1442 const Output_data* last;
1443 if (this->output_bss_.empty())
1444 last = this->output_data_.back();
1445 else
1446 last = this->output_bss_.back();
1447 this->memsz_ = (last->address()
1448 + last->data_size()
1449 - this->vaddr_);
1450 }
1451
1452 // Return the number of Output_sections in an Output_segment.
1453
1454 unsigned int
1455 Output_segment::output_section_count() const
1456 {
1457 return (this->output_section_count_list(&this->output_data_)
1458 + this->output_section_count_list(&this->output_bss_));
1459 }
1460
1461 // Return the number of Output_sections in an Output_data_list.
1462
1463 unsigned int
1464 Output_segment::output_section_count_list(const Output_data_list* pdl) const
1465 {
1466 unsigned int count = 0;
1467 for (Output_data_list::const_iterator p = pdl->begin();
1468 p != pdl->end();
1469 ++p)
1470 {
1471 if ((*p)->is_section())
1472 ++count;
1473 }
1474 return count;
1475 }
1476
1477 // Write the segment data into *OPHDR.
1478
1479 template<int size, bool big_endian>
1480 void
1481 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
1482 {
1483 ophdr->put_p_type(this->type_);
1484 ophdr->put_p_offset(this->offset_);
1485 ophdr->put_p_vaddr(this->vaddr_);
1486 ophdr->put_p_paddr(this->paddr_);
1487 ophdr->put_p_filesz(this->filesz_);
1488 ophdr->put_p_memsz(this->memsz_);
1489 ophdr->put_p_flags(this->flags_);
1490 ophdr->put_p_align(this->addralign());
1491 }
1492
1493 // Write the section headers into V.
1494
1495 template<int size, bool big_endian>
1496 unsigned char*
1497 Output_segment::write_section_headers(const Layout* layout,
1498 const Stringpool* secnamepool,
1499 unsigned char* v,
1500 unsigned int *pshndx
1501 ACCEPT_SIZE_ENDIAN) const
1502 {
1503 // Every section that is attached to a segment must be attached to a
1504 // PT_LOAD segment, so we only write out section headers for PT_LOAD
1505 // segments.
1506 if (this->type_ != elfcpp::PT_LOAD)
1507 return v;
1508
1509 v = this->write_section_headers_list
1510 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1511 layout, secnamepool, &this->output_data_, v, pshndx
1512 SELECT_SIZE_ENDIAN(size, big_endian));
1513 v = this->write_section_headers_list
1514 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1515 layout, secnamepool, &this->output_bss_, v, pshndx
1516 SELECT_SIZE_ENDIAN(size, big_endian));
1517 return v;
1518 }
1519
1520 template<int size, bool big_endian>
1521 unsigned char*
1522 Output_segment::write_section_headers_list(const Layout* layout,
1523 const Stringpool* secnamepool,
1524 const Output_data_list* pdl,
1525 unsigned char* v,
1526 unsigned int* pshndx
1527 ACCEPT_SIZE_ENDIAN) const
1528 {
1529 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1530 for (Output_data_list::const_iterator p = pdl->begin();
1531 p != pdl->end();
1532 ++p)
1533 {
1534 if ((*p)->is_section())
1535 {
1536 const Output_section* ps = static_cast<const Output_section*>(*p);
1537 gold_assert(*pshndx == ps->out_shndx());
1538 elfcpp::Shdr_write<size, big_endian> oshdr(v);
1539 ps->write_header(layout, secnamepool, &oshdr);
1540 v += shdr_size;
1541 ++*pshndx;
1542 }
1543 }
1544 return v;
1545 }
1546
1547 // Output_file methods.
1548
1549 Output_file::Output_file(const General_options& options, Target* target)
1550 : options_(options),
1551 target_(target),
1552 name_(options.output_file_name()),
1553 o_(-1),
1554 file_size_(0),
1555 base_(NULL)
1556 {
1557 }
1558
1559 // Open the output file.
1560
1561 void
1562 Output_file::open(off_t file_size)
1563 {
1564 this->file_size_ = file_size;
1565
1566 int mode = parameters->output_is_object() ? 0666 : 0777;
1567 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1568 if (o < 0)
1569 {
1570 fprintf(stderr, _("%s: %s: open: %s\n"),
1571 program_name, this->name_, strerror(errno));
1572 gold_exit(false);
1573 }
1574 this->o_ = o;
1575
1576 // Write out one byte to make the file the right size.
1577 if (::lseek(o, file_size - 1, SEEK_SET) < 0)
1578 {
1579 fprintf(stderr, _("%s: %s: lseek: %s\n"),
1580 program_name, this->name_, strerror(errno));
1581 gold_exit(false);
1582 }
1583 char b = 0;
1584 if (::write(o, &b, 1) != 1)
1585 {
1586 fprintf(stderr, _("%s: %s: write: %s\n"),
1587 program_name, this->name_, strerror(errno));
1588 gold_exit(false);
1589 }
1590
1591 // Map the file into memory.
1592 void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
1593 MAP_SHARED, o, 0);
1594 if (base == MAP_FAILED)
1595 {
1596 fprintf(stderr, _("%s: %s: mmap: %s\n"),
1597 program_name, this->name_, strerror(errno));
1598 gold_exit(false);
1599 }
1600 this->base_ = static_cast<unsigned char*>(base);
1601 }
1602
1603 // Close the output file.
1604
1605 void
1606 Output_file::close()
1607 {
1608 if (::munmap(this->base_, this->file_size_) < 0)
1609 {
1610 fprintf(stderr, _("%s: %s: munmap: %s\n"),
1611 program_name, this->name_, strerror(errno));
1612 gold_exit(false);
1613 }
1614 this->base_ = NULL;
1615
1616 if (::close(this->o_) < 0)
1617 {
1618 fprintf(stderr, _("%s: %s: close: %s\n"),
1619 program_name, this->name_, strerror(errno));
1620 gold_exit(false);
1621 }
1622 this->o_ = -1;
1623 }
1624
1625 // Instantiate the templates we need. We could use the configure
1626 // script to restrict this to only the ones for implemented targets.
1627
1628 #ifdef HAVE_TARGET_32_LITTLE
1629 template
1630 off_t
1631 Output_section::add_input_section<32, false>(
1632 Relobj* object,
1633 unsigned int shndx,
1634 const char* secname,
1635 const elfcpp::Shdr<32, false>& shdr);
1636 #endif
1637
1638 #ifdef HAVE_TARGET_32_BIG
1639 template
1640 off_t
1641 Output_section::add_input_section<32, true>(
1642 Relobj* object,
1643 unsigned int shndx,
1644 const char* secname,
1645 const elfcpp::Shdr<32, true>& shdr);
1646 #endif
1647
1648 #ifdef HAVE_TARGET_64_LITTLE
1649 template
1650 off_t
1651 Output_section::add_input_section<64, false>(
1652 Relobj* object,
1653 unsigned int shndx,
1654 const char* secname,
1655 const elfcpp::Shdr<64, false>& shdr);
1656 #endif
1657
1658 #ifdef HAVE_TARGET_64_BIG
1659 template
1660 off_t
1661 Output_section::add_input_section<64, true>(
1662 Relobj* object,
1663 unsigned int shndx,
1664 const char* secname,
1665 const elfcpp::Shdr<64, true>& shdr);
1666 #endif
1667
1668 #ifdef HAVE_TARGET_32_LITTLE
1669 template
1670 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
1671 #endif
1672
1673 #ifdef HAVE_TARGET_32_BIG
1674 template
1675 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
1676 #endif
1677
1678 #ifdef HAVE_TARGET_64_LITTLE
1679 template
1680 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
1681 #endif
1682
1683 #ifdef HAVE_TARGET_64_BIG
1684 template
1685 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
1686 #endif
1687
1688 #ifdef HAVE_TARGET_32_LITTLE
1689 template
1690 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
1691 #endif
1692
1693 #ifdef HAVE_TARGET_32_BIG
1694 template
1695 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
1696 #endif
1697
1698 #ifdef HAVE_TARGET_64_LITTLE
1699 template
1700 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
1701 #endif
1702
1703 #ifdef HAVE_TARGET_64_BIG
1704 template
1705 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
1706 #endif
1707
1708 #ifdef HAVE_TARGET_32_LITTLE
1709 template
1710 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
1711 #endif
1712
1713 #ifdef HAVE_TARGET_32_BIG
1714 template
1715 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
1716 #endif
1717
1718 #ifdef HAVE_TARGET_64_LITTLE
1719 template
1720 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
1721 #endif
1722
1723 #ifdef HAVE_TARGET_64_BIG
1724 template
1725 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
1726 #endif
1727
1728 #ifdef HAVE_TARGET_32_LITTLE
1729 template
1730 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
1731 #endif
1732
1733 #ifdef HAVE_TARGET_32_BIG
1734 template
1735 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
1736 #endif
1737
1738 #ifdef HAVE_TARGET_64_LITTLE
1739 template
1740 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
1741 #endif
1742
1743 #ifdef HAVE_TARGET_64_BIG
1744 template
1745 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
1746 #endif
1747
1748 #ifdef HAVE_TARGET_32_LITTLE
1749 template
1750 class Output_data_got<32, false>;
1751 #endif
1752
1753 #ifdef HAVE_TARGET_32_BIG
1754 template
1755 class Output_data_got<32, true>;
1756 #endif
1757
1758 #ifdef HAVE_TARGET_64_LITTLE
1759 template
1760 class Output_data_got<64, false>;
1761 #endif
1762
1763 #ifdef HAVE_TARGET_64_BIG
1764 template
1765 class Output_data_got<64, true>;
1766 #endif
1767
1768 } // End namespace gold.