gas/
[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 <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->shndx_ != shndx
843 || this->u2_.object != object)
844 return false;
845 off_t output_offset;
846 Output_section* os = object->output_section(shndx, &output_offset);
847 gold_assert(os != NULL);
848 *poutput = output_section_address + output_offset + offset;
849 return true;
850 }
851 }
852
853 // Write out the data. We don't have to do anything for an input
854 // section--they are handled via Object::relocate--but this is where
855 // we write out the data for an Output_section_data.
856
857 void
858 Output_section::Input_section::write(Output_file* of)
859 {
860 if (!this->is_input_section())
861 this->u2_.posd->write(of);
862 }
863
864 // Output_section methods.
865
866 // Construct an Output_section. NAME will point into a Stringpool.
867
868 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
869 elfcpp::Elf_Xword flags)
870 : name_(name),
871 addralign_(0),
872 entsize_(0),
873 link_section_(NULL),
874 link_(0),
875 info_section_(NULL),
876 info_(0),
877 type_(type),
878 flags_(flags),
879 out_shndx_(0),
880 symtab_index_(0),
881 dynsym_index_(0),
882 input_sections_(),
883 first_input_offset_(0),
884 fills_(),
885 needs_symtab_index_(false),
886 needs_dynsym_index_(false),
887 should_link_to_symtab_(false),
888 should_link_to_dynsym_(false)
889 {
890 }
891
892 Output_section::~Output_section()
893 {
894 }
895
896 // Set the entry size.
897
898 void
899 Output_section::set_entsize(uint64_t v)
900 {
901 if (this->entsize_ == 0)
902 this->entsize_ = v;
903 else
904 gold_assert(this->entsize_ == v);
905 }
906
907 // Add the input section SHNDX, with header SHDR, named SECNAME, in
908 // OBJECT, to the Output_section. Return the offset of the input
909 // section within the output section. We don't always keep track of
910 // input sections for an Output_section. Instead, each Object keeps
911 // track of the Output_section for each of its input sections.
912
913 template<int size, bool big_endian>
914 off_t
915 Output_section::add_input_section(Relobj* object, unsigned int shndx,
916 const char* secname,
917 const elfcpp::Shdr<size, big_endian>& shdr)
918 {
919 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
920 if ((addralign & (addralign - 1)) != 0)
921 {
922 fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
923 program_name, object->name().c_str(),
924 static_cast<unsigned long>(addralign), secname);
925 gold_exit(false);
926 }
927
928 if (addralign > this->addralign_)
929 this->addralign_ = addralign;
930
931 // If this is a SHF_MERGE section, we pass all the input sections to
932 // a Output_data_merge.
933 if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0)
934 {
935 if (this->add_merge_input_section(object, shndx, shdr.get_sh_flags(),
936 shdr.get_sh_entsize(),
937 addralign))
938 {
939 // Tell the relocation routines that they need to call the
940 // output_address method to determine the final address.
941 return -1;
942 }
943 }
944
945 off_t offset_in_section = this->data_size();
946 off_t aligned_offset_in_section = align_address(offset_in_section,
947 addralign);
948
949 if (aligned_offset_in_section > offset_in_section
950 && (shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0
951 && object->target()->has_code_fill())
952 {
953 // We need to add some fill data. Using fill_list_ when
954 // possible is an optimization, since we will often have fill
955 // sections without input sections.
956 off_t fill_len = aligned_offset_in_section - offset_in_section;
957 if (this->input_sections_.empty())
958 this->fills_.push_back(Fill(offset_in_section, fill_len));
959 else
960 {
961 // FIXME: When relaxing, the size needs to adjust to
962 // maintain a constant alignment.
963 std::string fill_data(object->target()->code_fill(fill_len));
964 Output_data_const* odc = new Output_data_const(fill_data, 1);
965 this->input_sections_.push_back(Input_section(odc));
966 }
967 }
968
969 this->set_data_size(aligned_offset_in_section + shdr.get_sh_size());
970
971 // We need to keep track of this section if we are already keeping
972 // track of sections, or if we are relaxing. FIXME: Add test for
973 // relaxing.
974 if (!this->input_sections_.empty())
975 this->input_sections_.push_back(Input_section(object, shndx,
976 shdr.get_sh_size(),
977 addralign));
978
979 return aligned_offset_in_section;
980 }
981
982 // Add arbitrary data to an output section.
983
984 void
985 Output_section::add_output_section_data(Output_section_data* posd)
986 {
987 Input_section inp(posd);
988 this->add_output_section_data(&inp);
989 }
990
991 // Add arbitrary data to an output section by Input_section.
992
993 void
994 Output_section::add_output_section_data(Input_section* inp)
995 {
996 if (this->input_sections_.empty())
997 this->first_input_offset_ = this->data_size();
998
999 this->input_sections_.push_back(*inp);
1000
1001 uint64_t addralign = inp->addralign();
1002 if (addralign > this->addralign_)
1003 this->addralign_ = addralign;
1004
1005 inp->set_output_section(this);
1006 }
1007
1008 // Add a merge section to an output section.
1009
1010 void
1011 Output_section::add_output_merge_section(Output_section_data* posd,
1012 bool is_string, uint64_t entsize)
1013 {
1014 Input_section inp(posd, is_string, entsize);
1015 this->add_output_section_data(&inp);
1016 }
1017
1018 // Add an input section to a SHF_MERGE section.
1019
1020 bool
1021 Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1022 uint64_t flags, uint64_t entsize,
1023 uint64_t addralign)
1024 {
1025 // We only merge constants if the alignment is not more than the
1026 // entry size. This could be handled, but it's unusual.
1027 if (addralign > entsize)
1028 return false;
1029
1030 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1031 Input_section_list::iterator p;
1032 for (p = this->input_sections_.begin();
1033 p != this->input_sections_.end();
1034 ++p)
1035 if (p->is_merge_section(is_string, entsize))
1036 break;
1037
1038 // We handle the actual constant merging in Output_merge_data or
1039 // Output_merge_string_data.
1040 if (p != this->input_sections_.end())
1041 p->add_input_section(object, shndx);
1042 else
1043 {
1044 Output_section_data* posd;
1045 if (!is_string)
1046 posd = new Output_merge_data(entsize);
1047 else if (entsize == 1)
1048 posd = new Output_merge_string<char>();
1049 else if (entsize == 2)
1050 posd = new Output_merge_string<uint16_t>();
1051 else if (entsize == 4)
1052 posd = new Output_merge_string<uint32_t>();
1053 else
1054 return false;
1055
1056 this->add_output_merge_section(posd, is_string, entsize);
1057 posd->add_input_section(object, shndx);
1058 }
1059
1060 return true;
1061 }
1062
1063 // Return the output virtual address of OFFSET relative to the start
1064 // of input section SHNDX in object OBJECT.
1065
1066 uint64_t
1067 Output_section::output_address(const Relobj* object, unsigned int shndx,
1068 off_t offset) const
1069 {
1070 uint64_t addr = this->address() + this->first_input_offset_;
1071 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1072 p != this->input_sections_.end();
1073 ++p)
1074 {
1075 addr = align_address(addr, p->addralign());
1076 uint64_t output;
1077 if (p->output_address(object, shndx, offset, addr, &output))
1078 return output;
1079 addr += p->data_size();
1080 }
1081
1082 // If we get here, it means that we don't know the mapping for this
1083 // input section. This might happen in principle if
1084 // add_input_section were called before add_output_section_data.
1085 // But it should never actually happen.
1086
1087 gold_unreachable();
1088 }
1089
1090 // Set the address of an Output_section. This is where we handle
1091 // setting the addresses of any Output_section_data objects.
1092
1093 void
1094 Output_section::do_set_address(uint64_t address, off_t startoff)
1095 {
1096 if (this->input_sections_.empty())
1097 return;
1098
1099 off_t off = startoff + this->first_input_offset_;
1100 for (Input_section_list::iterator p = this->input_sections_.begin();
1101 p != this->input_sections_.end();
1102 ++p)
1103 {
1104 off = align_address(off, p->addralign());
1105 p->set_address(address + (off - startoff), off, startoff);
1106 off += p->data_size();
1107 }
1108
1109 this->set_data_size(off - startoff);
1110 }
1111
1112 // Write the section header to *OSHDR.
1113
1114 template<int size, bool big_endian>
1115 void
1116 Output_section::write_header(const Layout* layout,
1117 const Stringpool* secnamepool,
1118 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1119 {
1120 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1121 oshdr->put_sh_type(this->type_);
1122 oshdr->put_sh_flags(this->flags_);
1123 oshdr->put_sh_addr(this->address());
1124 oshdr->put_sh_offset(this->offset());
1125 oshdr->put_sh_size(this->data_size());
1126 if (this->link_section_ != NULL)
1127 oshdr->put_sh_link(this->link_section_->out_shndx());
1128 else if (this->should_link_to_symtab_)
1129 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1130 else if (this->should_link_to_dynsym_)
1131 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1132 else
1133 oshdr->put_sh_link(this->link_);
1134 if (this->info_section_ != NULL)
1135 oshdr->put_sh_info(this->info_section_->out_shndx());
1136 else
1137 oshdr->put_sh_info(this->info_);
1138 oshdr->put_sh_addralign(this->addralign_);
1139 oshdr->put_sh_entsize(this->entsize_);
1140 }
1141
1142 // Write out the data. For input sections the data is written out by
1143 // Object::relocate, but we have to handle Output_section_data objects
1144 // here.
1145
1146 void
1147 Output_section::do_write(Output_file* of)
1148 {
1149 off_t output_section_file_offset = this->offset();
1150 for (Fill_list::iterator p = this->fills_.begin();
1151 p != this->fills_.end();
1152 ++p)
1153 {
1154 std::string fill_data(of->target()->code_fill(p->length()));
1155 of->write(output_section_file_offset + p->section_offset(),
1156 fill_data.data(), fill_data.size());
1157 }
1158
1159 for (Input_section_list::iterator p = this->input_sections_.begin();
1160 p != this->input_sections_.end();
1161 ++p)
1162 p->write(of);
1163 }
1164
1165 // Output segment methods.
1166
1167 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
1168 : output_data_(),
1169 output_bss_(),
1170 vaddr_(0),
1171 paddr_(0),
1172 memsz_(0),
1173 align_(0),
1174 offset_(0),
1175 filesz_(0),
1176 type_(type),
1177 flags_(flags),
1178 is_align_known_(false)
1179 {
1180 }
1181
1182 // Add an Output_section to an Output_segment.
1183
1184 void
1185 Output_segment::add_output_section(Output_section* os,
1186 elfcpp::Elf_Word seg_flags,
1187 bool front)
1188 {
1189 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1190 gold_assert(!this->is_align_known_);
1191
1192 // Update the segment flags.
1193 this->flags_ |= seg_flags;
1194
1195 Output_segment::Output_data_list* pdl;
1196 if (os->type() == elfcpp::SHT_NOBITS)
1197 pdl = &this->output_bss_;
1198 else
1199 pdl = &this->output_data_;
1200
1201 // So that PT_NOTE segments will work correctly, we need to ensure
1202 // that all SHT_NOTE sections are adjacent. This will normally
1203 // happen automatically, because all the SHT_NOTE input sections
1204 // will wind up in the same output section. However, it is possible
1205 // for multiple SHT_NOTE input sections to have different section
1206 // flags, and thus be in different output sections, but for the
1207 // different section flags to map into the same segment flags and
1208 // thus the same output segment.
1209
1210 // Note that while there may be many input sections in an output
1211 // section, there are normally only a few output sections in an
1212 // output segment. This loop is expected to be fast.
1213
1214 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
1215 {
1216 Output_segment::Output_data_list::iterator p = pdl->end();
1217 do
1218 {
1219 --p;
1220 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1221 {
1222 // We don't worry about the FRONT parameter.
1223 ++p;
1224 pdl->insert(p, os);
1225 return;
1226 }
1227 }
1228 while (p != pdl->begin());
1229 }
1230
1231 // Similarly, so that PT_TLS segments will work, we need to group
1232 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1233 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1234 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
1235 // correctly.
1236 if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
1237 {
1238 pdl = &this->output_data_;
1239 bool nobits = os->type() == elfcpp::SHT_NOBITS;
1240 bool sawtls = false;
1241 Output_segment::Output_data_list::iterator p = pdl->end();
1242 do
1243 {
1244 --p;
1245 bool insert;
1246 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1247 {
1248 sawtls = true;
1249 // Put a NOBITS section after the first TLS section.
1250 // But a PROGBITS section after the first TLS/PROGBITS
1251 // section.
1252 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1253 }
1254 else
1255 {
1256 // If we've gone past the TLS sections, but we've seen a
1257 // TLS section, then we need to insert this section now.
1258 insert = sawtls;
1259 }
1260
1261 if (insert)
1262 {
1263 // We don't worry about the FRONT parameter.
1264 ++p;
1265 pdl->insert(p, os);
1266 return;
1267 }
1268 }
1269 while (p != pdl->begin());
1270
1271 // There are no TLS sections yet; put this one at the requested
1272 // location in the section list.
1273 }
1274
1275 if (front)
1276 pdl->push_front(os);
1277 else
1278 pdl->push_back(os);
1279 }
1280
1281 // Add an Output_data (which is not an Output_section) to the start of
1282 // a segment.
1283
1284 void
1285 Output_segment::add_initial_output_data(Output_data* od)
1286 {
1287 gold_assert(!this->is_align_known_);
1288 this->output_data_.push_front(od);
1289 }
1290
1291 // Return the maximum alignment of the Output_data in Output_segment.
1292 // Once we compute this, we prohibit new sections from being added.
1293
1294 uint64_t
1295 Output_segment::addralign()
1296 {
1297 if (!this->is_align_known_)
1298 {
1299 uint64_t addralign;
1300
1301 addralign = Output_segment::maximum_alignment(&this->output_data_);
1302 if (addralign > this->align_)
1303 this->align_ = addralign;
1304
1305 addralign = Output_segment::maximum_alignment(&this->output_bss_);
1306 if (addralign > this->align_)
1307 this->align_ = addralign;
1308
1309 this->is_align_known_ = true;
1310 }
1311
1312 return this->align_;
1313 }
1314
1315 // Return the maximum alignment of a list of Output_data.
1316
1317 uint64_t
1318 Output_segment::maximum_alignment(const Output_data_list* pdl)
1319 {
1320 uint64_t ret = 0;
1321 for (Output_data_list::const_iterator p = pdl->begin();
1322 p != pdl->end();
1323 ++p)
1324 {
1325 uint64_t addralign = (*p)->addralign();
1326 if (addralign > ret)
1327 ret = addralign;
1328 }
1329 return ret;
1330 }
1331
1332 // Set the section addresses for an Output_segment. ADDR is the
1333 // address and *POFF is the file offset. Set the section indexes
1334 // starting with *PSHNDX. Return the address of the immediately
1335 // following segment. Update *POFF and *PSHNDX.
1336
1337 uint64_t
1338 Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
1339 unsigned int* pshndx)
1340 {
1341 gold_assert(this->type_ == elfcpp::PT_LOAD);
1342
1343 this->vaddr_ = addr;
1344 this->paddr_ = addr;
1345
1346 off_t orig_off = *poff;
1347 this->offset_ = orig_off;
1348
1349 *poff = align_address(*poff, this->addralign());
1350
1351 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
1352 pshndx);
1353 this->filesz_ = *poff - orig_off;
1354
1355 off_t off = *poff;
1356
1357 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
1358 poff, pshndx);
1359 this->memsz_ = *poff - orig_off;
1360
1361 // Ignore the file offset adjustments made by the BSS Output_data
1362 // objects.
1363 *poff = off;
1364
1365 return ret;
1366 }
1367
1368 // Set the addresses and file offsets in a list of Output_data
1369 // structures.
1370
1371 uint64_t
1372 Output_segment::set_section_list_addresses(Output_data_list* pdl,
1373 uint64_t addr, off_t* poff,
1374 unsigned int* pshndx)
1375 {
1376 off_t startoff = *poff;
1377
1378 off_t off = startoff;
1379 for (Output_data_list::iterator p = pdl->begin();
1380 p != pdl->end();
1381 ++p)
1382 {
1383 off = align_address(off, (*p)->addralign());
1384 (*p)->set_address(addr + (off - startoff), off);
1385
1386 // Unless this is a PT_TLS segment, we want to ignore the size
1387 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
1388 // affect the size of a PT_LOAD segment.
1389 if (this->type_ == elfcpp::PT_TLS
1390 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
1391 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
1392 off += (*p)->data_size();
1393
1394 if ((*p)->is_section())
1395 {
1396 (*p)->set_out_shndx(*pshndx);
1397 ++*pshndx;
1398 }
1399 }
1400
1401 *poff = off;
1402 return addr + (off - startoff);
1403 }
1404
1405 // For a non-PT_LOAD segment, set the offset from the sections, if
1406 // any.
1407
1408 void
1409 Output_segment::set_offset()
1410 {
1411 gold_assert(this->type_ != elfcpp::PT_LOAD);
1412
1413 if (this->output_data_.empty() && this->output_bss_.empty())
1414 {
1415 this->vaddr_ = 0;
1416 this->paddr_ = 0;
1417 this->memsz_ = 0;
1418 this->align_ = 0;
1419 this->offset_ = 0;
1420 this->filesz_ = 0;
1421 return;
1422 }
1423
1424 const Output_data* first;
1425 if (this->output_data_.empty())
1426 first = this->output_bss_.front();
1427 else
1428 first = this->output_data_.front();
1429 this->vaddr_ = first->address();
1430 this->paddr_ = this->vaddr_;
1431 this->offset_ = first->offset();
1432
1433 if (this->output_data_.empty())
1434 this->filesz_ = 0;
1435 else
1436 {
1437 const Output_data* last_data = this->output_data_.back();
1438 this->filesz_ = (last_data->address()
1439 + last_data->data_size()
1440 - this->vaddr_);
1441 }
1442
1443 const Output_data* last;
1444 if (this->output_bss_.empty())
1445 last = this->output_data_.back();
1446 else
1447 last = this->output_bss_.back();
1448 this->memsz_ = (last->address()
1449 + last->data_size()
1450 - this->vaddr_);
1451 }
1452
1453 // Return the number of Output_sections in an Output_segment.
1454
1455 unsigned int
1456 Output_segment::output_section_count() const
1457 {
1458 return (this->output_section_count_list(&this->output_data_)
1459 + this->output_section_count_list(&this->output_bss_));
1460 }
1461
1462 // Return the number of Output_sections in an Output_data_list.
1463
1464 unsigned int
1465 Output_segment::output_section_count_list(const Output_data_list* pdl) const
1466 {
1467 unsigned int count = 0;
1468 for (Output_data_list::const_iterator p = pdl->begin();
1469 p != pdl->end();
1470 ++p)
1471 {
1472 if ((*p)->is_section())
1473 ++count;
1474 }
1475 return count;
1476 }
1477
1478 // Write the segment data into *OPHDR.
1479
1480 template<int size, bool big_endian>
1481 void
1482 Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
1483 {
1484 ophdr->put_p_type(this->type_);
1485 ophdr->put_p_offset(this->offset_);
1486 ophdr->put_p_vaddr(this->vaddr_);
1487 ophdr->put_p_paddr(this->paddr_);
1488 ophdr->put_p_filesz(this->filesz_);
1489 ophdr->put_p_memsz(this->memsz_);
1490 ophdr->put_p_flags(this->flags_);
1491 ophdr->put_p_align(this->addralign());
1492 }
1493
1494 // Write the section headers into V.
1495
1496 template<int size, bool big_endian>
1497 unsigned char*
1498 Output_segment::write_section_headers(const Layout* layout,
1499 const Stringpool* secnamepool,
1500 unsigned char* v,
1501 unsigned int *pshndx
1502 ACCEPT_SIZE_ENDIAN) const
1503 {
1504 // Every section that is attached to a segment must be attached to a
1505 // PT_LOAD segment, so we only write out section headers for PT_LOAD
1506 // segments.
1507 if (this->type_ != elfcpp::PT_LOAD)
1508 return v;
1509
1510 v = this->write_section_headers_list
1511 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1512 layout, secnamepool, &this->output_data_, v, pshndx
1513 SELECT_SIZE_ENDIAN(size, big_endian));
1514 v = this->write_section_headers_list
1515 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1516 layout, secnamepool, &this->output_bss_, v, pshndx
1517 SELECT_SIZE_ENDIAN(size, big_endian));
1518 return v;
1519 }
1520
1521 template<int size, bool big_endian>
1522 unsigned char*
1523 Output_segment::write_section_headers_list(const Layout* layout,
1524 const Stringpool* secnamepool,
1525 const Output_data_list* pdl,
1526 unsigned char* v,
1527 unsigned int* pshndx
1528 ACCEPT_SIZE_ENDIAN) const
1529 {
1530 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1531 for (Output_data_list::const_iterator p = pdl->begin();
1532 p != pdl->end();
1533 ++p)
1534 {
1535 if ((*p)->is_section())
1536 {
1537 const Output_section* ps = static_cast<const Output_section*>(*p);
1538 gold_assert(*pshndx == ps->out_shndx());
1539 elfcpp::Shdr_write<size, big_endian> oshdr(v);
1540 ps->write_header(layout, secnamepool, &oshdr);
1541 v += shdr_size;
1542 ++*pshndx;
1543 }
1544 }
1545 return v;
1546 }
1547
1548 // Output_file methods.
1549
1550 Output_file::Output_file(const General_options& options, Target* target)
1551 : options_(options),
1552 target_(target),
1553 name_(options.output_file_name()),
1554 o_(-1),
1555 file_size_(0),
1556 base_(NULL)
1557 {
1558 }
1559
1560 // Open the output file.
1561
1562 void
1563 Output_file::open(off_t file_size)
1564 {
1565 this->file_size_ = file_size;
1566
1567 int mode = parameters->output_is_object() ? 0666 : 0777;
1568 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1569 if (o < 0)
1570 {
1571 fprintf(stderr, _("%s: %s: open: %s\n"),
1572 program_name, this->name_, strerror(errno));
1573 gold_exit(false);
1574 }
1575 this->o_ = o;
1576
1577 // Write out one byte to make the file the right size.
1578 if (::lseek(o, file_size - 1, SEEK_SET) < 0)
1579 {
1580 fprintf(stderr, _("%s: %s: lseek: %s\n"),
1581 program_name, this->name_, strerror(errno));
1582 gold_exit(false);
1583 }
1584 char b = 0;
1585 if (::write(o, &b, 1) != 1)
1586 {
1587 fprintf(stderr, _("%s: %s: write: %s\n"),
1588 program_name, this->name_, strerror(errno));
1589 gold_exit(false);
1590 }
1591
1592 // Map the file into memory.
1593 void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
1594 MAP_SHARED, o, 0);
1595 if (base == MAP_FAILED)
1596 {
1597 fprintf(stderr, _("%s: %s: mmap: %s\n"),
1598 program_name, this->name_, strerror(errno));
1599 gold_exit(false);
1600 }
1601 this->base_ = static_cast<unsigned char*>(base);
1602 }
1603
1604 // Close the output file.
1605
1606 void
1607 Output_file::close()
1608 {
1609 if (::munmap(this->base_, this->file_size_) < 0)
1610 {
1611 fprintf(stderr, _("%s: %s: munmap: %s\n"),
1612 program_name, this->name_, strerror(errno));
1613 gold_exit(false);
1614 }
1615 this->base_ = NULL;
1616
1617 if (::close(this->o_) < 0)
1618 {
1619 fprintf(stderr, _("%s: %s: close: %s\n"),
1620 program_name, this->name_, strerror(errno));
1621 gold_exit(false);
1622 }
1623 this->o_ = -1;
1624 }
1625
1626 // Instantiate the templates we need. We could use the configure
1627 // script to restrict this to only the ones for implemented targets.
1628
1629 #ifdef HAVE_TARGET_32_LITTLE
1630 template
1631 off_t
1632 Output_section::add_input_section<32, false>(
1633 Relobj* object,
1634 unsigned int shndx,
1635 const char* secname,
1636 const elfcpp::Shdr<32, false>& shdr);
1637 #endif
1638
1639 #ifdef HAVE_TARGET_32_BIG
1640 template
1641 off_t
1642 Output_section::add_input_section<32, true>(
1643 Relobj* object,
1644 unsigned int shndx,
1645 const char* secname,
1646 const elfcpp::Shdr<32, true>& shdr);
1647 #endif
1648
1649 #ifdef HAVE_TARGET_64_LITTLE
1650 template
1651 off_t
1652 Output_section::add_input_section<64, false>(
1653 Relobj* object,
1654 unsigned int shndx,
1655 const char* secname,
1656 const elfcpp::Shdr<64, false>& shdr);
1657 #endif
1658
1659 #ifdef HAVE_TARGET_64_BIG
1660 template
1661 off_t
1662 Output_section::add_input_section<64, true>(
1663 Relobj* object,
1664 unsigned int shndx,
1665 const char* secname,
1666 const elfcpp::Shdr<64, true>& shdr);
1667 #endif
1668
1669 #ifdef HAVE_TARGET_32_LITTLE
1670 template
1671 class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
1672 #endif
1673
1674 #ifdef HAVE_TARGET_32_BIG
1675 template
1676 class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
1677 #endif
1678
1679 #ifdef HAVE_TARGET_64_LITTLE
1680 template
1681 class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
1682 #endif
1683
1684 #ifdef HAVE_TARGET_64_BIG
1685 template
1686 class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
1687 #endif
1688
1689 #ifdef HAVE_TARGET_32_LITTLE
1690 template
1691 class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
1692 #endif
1693
1694 #ifdef HAVE_TARGET_32_BIG
1695 template
1696 class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
1697 #endif
1698
1699 #ifdef HAVE_TARGET_64_LITTLE
1700 template
1701 class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
1702 #endif
1703
1704 #ifdef HAVE_TARGET_64_BIG
1705 template
1706 class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
1707 #endif
1708
1709 #ifdef HAVE_TARGET_32_LITTLE
1710 template
1711 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
1712 #endif
1713
1714 #ifdef HAVE_TARGET_32_BIG
1715 template
1716 class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
1717 #endif
1718
1719 #ifdef HAVE_TARGET_64_LITTLE
1720 template
1721 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
1722 #endif
1723
1724 #ifdef HAVE_TARGET_64_BIG
1725 template
1726 class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
1727 #endif
1728
1729 #ifdef HAVE_TARGET_32_LITTLE
1730 template
1731 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
1732 #endif
1733
1734 #ifdef HAVE_TARGET_32_BIG
1735 template
1736 class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
1737 #endif
1738
1739 #ifdef HAVE_TARGET_64_LITTLE
1740 template
1741 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
1742 #endif
1743
1744 #ifdef HAVE_TARGET_64_BIG
1745 template
1746 class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
1747 #endif
1748
1749 #ifdef HAVE_TARGET_32_LITTLE
1750 template
1751 class Output_data_got<32, false>;
1752 #endif
1753
1754 #ifdef HAVE_TARGET_32_BIG
1755 template
1756 class Output_data_got<32, true>;
1757 #endif
1758
1759 #ifdef HAVE_TARGET_64_LITTLE
1760 template
1761 class Output_data_got<64, false>;
1762 #endif
1763
1764 #ifdef HAVE_TARGET_64_BIG
1765 template
1766 class Output_data_got<64, true>;
1767 #endif
1768
1769 } // End namespace gold.
This page took 0.068499 seconds and 4 git commands to generate.