1 // output.cc -- manage the output file for gold
18 // Output_data methods.
20 Output_data::~Output_data()
24 // Set the address and offset.
27 Output_data::set_address(uint64_t addr
, off_t off
)
29 this->address_
= addr
;
32 // Let the child class know.
33 this->do_set_address(addr
, off
);
36 // Return the default alignment for a size--32 or 64.
39 Output_data::default_alignment(int size
)
49 // Output_data_const methods.
52 Output_data_const::do_write(Output_file
* output
)
54 output
->write(this->offset(), data_
.data(), data_
.size());
57 // Output_section_header methods. This currently assumes that the
58 // segment and section lists are complete at construction time.
60 Output_section_headers::Output_section_headers(
63 const Layout::Segment_list
& segment_list
,
64 const Layout::Section_list
& section_list
,
65 const Stringpool
* secnamepool
)
67 big_endian_(big_endian
),
68 segment_list_(segment_list
),
69 section_list_(section_list
),
70 secnamepool_(secnamepool
)
72 // Count all the sections. Start with 1 for the null section.
74 for (Layout::Segment_list::const_iterator p
= segment_list
.begin();
75 p
!= segment_list
.end();
77 count
+= (*p
)->output_section_count();
78 count
+= section_list
.size();
82 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
84 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
88 this->set_data_size(count
* shdr_size
);
91 // Write out the section headers.
94 Output_section_headers::do_write(Output_file
* of
)
96 if (this->size_
== 32)
98 if (this->big_endian_
)
99 this->do_sized_write
<32, true>(of
);
101 this->do_sized_write
<32, false>(of
);
103 else if (this->size_
== 64)
105 if (this->big_endian_
)
106 this->do_sized_write
<64, true>(of
);
108 this->do_sized_write
<64, false>(of
);
114 template<int size
, bool big_endian
>
116 Output_section_headers::do_sized_write(Output_file
* of
)
118 off_t all_shdrs_size
= this->data_size();
119 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
121 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
122 unsigned char* v
= view
;
125 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
126 oshdr
.put_sh_name(0);
127 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
128 oshdr
.put_sh_flags(0);
129 oshdr
.put_sh_addr(0);
130 oshdr
.put_sh_offset(0);
131 oshdr
.put_sh_size(0);
132 oshdr
.put_sh_link(0);
133 oshdr
.put_sh_info(0);
134 oshdr
.put_sh_addralign(0);
135 oshdr
.put_sh_entsize(0);
140 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
141 p
!= this->segment_list_
.end();
143 v
= (*p
)->write_section_headers
SELECT_SIZE_ENDIAN_NAME (
144 this->secnamepool_
, v
SELECT_SIZE_ENDIAN(size
, big_endian
));
145 for (Layout::Section_list::const_iterator p
= this->section_list_
.begin();
146 p
!= this->section_list_
.end();
149 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
150 (*p
)->write_header(this->secnamepool_
, &oshdr
);
154 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
157 // Output_segment_header methods.
159 Output_segment_headers::Output_segment_headers(
162 const Layout::Segment_list
& segment_list
)
163 : size_(size
), big_endian_(big_endian
), segment_list_(segment_list
)
167 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
169 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
173 this->set_data_size(segment_list
.size() * phdr_size
);
177 Output_segment_headers::do_write(Output_file
* of
)
179 if (this->size_
== 32)
181 if (this->big_endian_
)
182 this->do_sized_write
<32, true>(of
);
184 this->do_sized_write
<32, false>(of
);
186 else if (this->size_
== 64)
188 if (this->big_endian_
)
189 this->do_sized_write
<64, true>(of
);
191 this->do_sized_write
<64, false>(of
);
197 template<int size
, bool big_endian
>
199 Output_segment_headers::do_sized_write(Output_file
* of
)
201 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
202 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
203 unsigned char* view
= of
->get_output_view(this->offset(),
205 unsigned char* v
= view
;
206 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
207 p
!= this->segment_list_
.end();
210 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
211 (*p
)->write_header(&ophdr
);
215 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
218 // Output_file_header methods.
220 Output_file_header::Output_file_header(int size
,
222 const General_options
& options
,
223 const Target
* target
,
224 const Symbol_table
* symtab
,
225 const Output_segment_headers
* osh
)
227 big_endian_(big_endian
),
231 segment_header_(osh
),
232 section_header_(NULL
),
237 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
239 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
243 this->set_data_size(ehdr_size
);
246 // Set the section table information for a file header.
249 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
250 const Output_section
* shstrtab
)
252 this->section_header_
= shdrs
;
253 this->shstrtab_
= shstrtab
;
256 // Write out the file header.
259 Output_file_header::do_write(Output_file
* of
)
261 if (this->size_
== 32)
263 if (this->big_endian_
)
264 this->do_sized_write
<32, true>(of
);
266 this->do_sized_write
<32, false>(of
);
268 else if (this->size_
== 64)
270 if (this->big_endian_
)
271 this->do_sized_write
<64, true>(of
);
273 this->do_sized_write
<64, false>(of
);
279 // Write out the file header with appropriate size and endianess.
281 template<int size
, bool big_endian
>
283 Output_file_header::do_sized_write(Output_file
* of
)
285 assert(this->offset() == 0);
287 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
288 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
289 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
291 unsigned char e_ident
[elfcpp::EI_NIDENT
];
292 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
293 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
294 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
295 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
296 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
298 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
300 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
303 e_ident
[elfcpp::EI_DATA
] = (big_endian
304 ? elfcpp::ELFDATA2MSB
305 : elfcpp::ELFDATA2LSB
);
306 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
307 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
308 oehdr
.put_e_ident(e_ident
);
312 if (this->options_
.is_relocatable())
313 e_type
= elfcpp::ET_REL
;
315 e_type
= elfcpp::ET_EXEC
;
316 oehdr
.put_e_type(e_type
);
318 oehdr
.put_e_machine(this->target_
->machine_code());
319 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
321 Symbol
* sym
= this->symtab_
->lookup("_start");
322 typename Sized_symbol
<size
>::Value_type v
;
327 Sized_symbol
<size
>* ssym
;
328 ssym
= this->symtab_
->get_sized_symbol
SELECT_SIZE_NAME (
329 sym
SELECT_SIZE(size
));
332 oehdr
.put_e_entry(v
);
334 oehdr
.put_e_phoff(this->segment_header_
->offset());
335 oehdr
.put_e_shoff(this->section_header_
->offset());
337 // FIXME: The target needs to set the flags.
338 oehdr
.put_e_flags(0);
340 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
341 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
342 oehdr
.put_e_phnum(this->segment_header_
->data_size()
343 / elfcpp::Elf_sizes
<size
>::phdr_size
);
344 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
345 oehdr
.put_e_shnum(this->section_header_
->data_size()
346 / elfcpp::Elf_sizes
<size
>::shdr_size
);
347 oehdr
.put_e_shstrndx(this->shstrtab_
->shndx());
349 of
->write_output_view(0, ehdr_size
, view
);
352 // Output_section methods.
354 // Construct an Output_section. NAME will point into a Stringpool.
356 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
357 elfcpp::Elf_Xword flags
, unsigned int shndx
)
369 Output_section::~Output_section()
373 // Add an input section to an Output_section. We don't keep track of
374 // input sections for an Output_section. Instead, each Object keeps
375 // track of the Output_section for each of its input sections.
377 template<int size
, bool big_endian
>
379 Output_section::add_input_section(Object
* object
, const char* secname
,
380 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
382 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
383 if ((addralign
& (addralign
- 1)) != 0)
385 fprintf(stderr
, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
386 program_name
, object
->name().c_str(),
387 static_cast<unsigned long>(addralign
), secname
);
391 if (addralign
> this->addralign_
)
392 this->addralign_
= addralign
;
394 off_t ssize
= this->data_size();
395 ssize
= (ssize
+ addralign
- 1) &~ (addralign
- 1);
397 // SHF_TLS/SHT_NOBITS sections are handled specially: they are
398 // treated as having no size and taking up no space. We only use
399 // the real size when setting the pt_memsz field of the PT_TLS
401 if ((this->flags_
& elfcpp::SHF_TLS
) == 0
402 || this->type_
!= elfcpp::SHT_NOBITS
)
403 this->set_data_size(ssize
+ shdr
.get_sh_size());
408 // Write the section header to *OSHDR.
410 template<int size
, bool big_endian
>
412 Output_section::write_header(const Stringpool
* secnamepool
,
413 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
415 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
416 oshdr
->put_sh_type(this->type_
);
417 oshdr
->put_sh_flags(this->flags_
);
418 oshdr
->put_sh_addr(this->address());
419 oshdr
->put_sh_offset(this->offset());
420 oshdr
->put_sh_size(this->data_size());
421 oshdr
->put_sh_link(this->link_
);
422 oshdr
->put_sh_info(this->info_
);
423 oshdr
->put_sh_addralign(this->addralign_
);
424 oshdr
->put_sh_entsize(this->entsize_
);
427 // Output_section_symtab methods.
429 Output_section_symtab::Output_section_symtab(const char* name
, off_t size
,
431 : Output_section(name
, elfcpp::SHT_SYMTAB
, 0, shndx
)
433 this->set_data_size(size
);
436 // Output_section_strtab methods.
438 Output_section_strtab::Output_section_strtab(const char* name
,
439 Stringpool
* contents
,
441 : Output_section(name
, elfcpp::SHT_STRTAB
, 0, shndx
),
444 this->set_data_size(contents
->get_strtab_size());
448 Output_section_strtab::do_write(Output_file
* of
)
450 this->contents_
->write(of
, this->offset());
453 // Output segment methods.
455 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
469 // Add an Output_section to an Output_segment.
472 Output_segment::add_output_section(Output_section
* os
,
473 elfcpp::Elf_Word seg_flags
)
475 assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
477 // Update the segment flags and alignment.
478 this->flags_
|= seg_flags
;
479 uint64_t addralign
= os
->addralign();
480 if (addralign
> this->align_
)
481 this->align_
= addralign
;
483 Output_segment::Output_data_list
* pdl
;
484 if (os
->type() == elfcpp::SHT_NOBITS
)
485 pdl
= &this->output_bss_
;
487 pdl
= &this->output_data_
;
489 // So that PT_NOTE segments will work correctly, we need to ensure
490 // that all SHT_NOTE sections are adjacent. This will normally
491 // happen automatically, because all the SHT_NOTE input sections
492 // will wind up in the same output section. However, it is possible
493 // for multiple SHT_NOTE input sections to have different section
494 // flags, and thus be in different output sections, but for the
495 // different section flags to map into the same segment flags and
496 // thus the same output segment.
498 // Note that while there may be many input sections in an output
499 // section, there are normally only a few output sections in an
500 // output segment. This loop is expected to be fast.
502 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
504 Layout::Data_list::iterator p
= pdl
->end();
508 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
515 while (p
!= pdl
->begin());
518 // Similarly, so that PT_TLS segments will work, we need to group
519 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
520 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
521 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
523 if ((os
->flags() & elfcpp::SHF_TLS
) != 0 && !this->output_data_
.empty())
525 pdl
= &this->output_data_
;
526 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
527 Layout::Data_list::iterator p
= pdl
->end();
531 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
532 && (nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
)))
539 while (p
!= pdl
->begin());
545 // Add an Output_data (which is not an Output_section) to the start of
549 Output_segment::add_initial_output_data(Output_data
* od
)
551 uint64_t addralign
= od
->addralign();
552 if (addralign
> this->align_
)
553 this->align_
= addralign
;
555 this->output_data_
.push_front(od
);
558 // Return the maximum alignment of the Output_data in Output_segment.
559 // We keep this up to date as we add Output_sections and Output_data.
562 Output_segment::max_data_align() const
567 // Set the section addresses for an Output_segment. ADDR is the
568 // address and *POFF is the file offset. Return the address of the
569 // immediately following segment. Update *POFF.
572 Output_segment::set_section_addresses(uint64_t addr
, off_t
* poff
)
574 assert(this->type_
== elfcpp::PT_LOAD
);
579 off_t orig_off
= *poff
;
580 this->offset_
= orig_off
;
582 addr
= this->set_section_list_addresses(&this->output_data_
, addr
, poff
);
583 this->filesz_
= *poff
- orig_off
;
587 uint64_t ret
= this->set_section_list_addresses(&this->output_bss_
, addr
,
589 this->memsz_
= *poff
- orig_off
;
591 // Ignore the file offset adjustments made by the BSS Output_data
598 // Set the addresses in a list of Output_data structures.
601 Output_segment::set_section_list_addresses(Output_data_list
* pdl
,
602 uint64_t addr
, off_t
* poff
)
606 for (Output_data_list::iterator p
= pdl
->begin();
610 uint64_t addralign
= (*p
)->addralign();
611 addr
= (addr
+ addralign
- 1) & ~ (addralign
- 1);
612 off
= (off
+ addralign
- 1) & ~ (addralign
- 1);
613 (*p
)->set_address(addr
, off
);
615 uint64_t size
= (*p
)->data_size();
624 // For a non-PT_LOAD segment, set the offset from the sections, if
628 Output_segment::set_offset()
630 assert(this->type_
!= elfcpp::PT_LOAD
);
632 if (this->output_data_
.empty() && this->output_bss_
.empty())
643 const Output_data
* first
;
644 if (this->output_data_
.empty())
645 first
= this->output_bss_
.front();
647 first
= this->output_data_
.front();
648 this->vaddr_
= first
->address();
649 this->paddr_
= this->vaddr_
;
650 this->offset_
= first
->offset();
652 if (this->output_data_
.empty())
656 const Output_data
* last_data
= this->output_data_
.back();
657 this->filesz_
= (last_data
->address()
658 + last_data
->data_size()
662 const Output_data
* last
;
663 if (this->output_bss_
.empty())
664 last
= this->output_data_
.back();
666 last
= this->output_bss_
.back();
667 this->memsz_
= (last
->address()
671 // this->align_ was set as we added items.
674 // Return the number of Output_sections in an Output_segment.
677 Output_segment::output_section_count() const
679 return (this->output_section_count_list(&this->output_data_
)
680 + this->output_section_count_list(&this->output_bss_
));
683 // Return the number of Output_sections in an Output_data_list.
686 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
688 unsigned int count
= 0;
689 for (Output_data_list::const_iterator p
= pdl
->begin();
693 if ((*p
)->is_section())
699 // Write the segment data into *OPHDR.
701 template<int size
, bool big_endian
>
703 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
) const
705 ophdr
->put_p_type(this->type_
);
706 ophdr
->put_p_offset(this->offset_
);
707 ophdr
->put_p_vaddr(this->vaddr_
);
708 ophdr
->put_p_paddr(this->paddr_
);
709 ophdr
->put_p_filesz(this->filesz_
);
710 ophdr
->put_p_memsz(this->memsz_
);
711 ophdr
->put_p_flags(this->flags_
);
712 ophdr
->put_p_align(this->align_
);
715 // Write the section headers into V.
717 template<int size
, bool big_endian
>
719 Output_segment::write_section_headers(const Stringpool
* secnamepool
,
721 ACCEPT_SIZE_ENDIAN
) const
723 v
= this->write_section_headers_list
SELECT_SIZE_ENDIAN_NAME (
724 secnamepool
, &this->output_data_
, v
SELECT_SIZE_ENDIAN(size
, big_endian
));
725 v
= this->write_section_headers_list
SELECT_SIZE_ENDIAN_NAME (
726 secnamepool
, &this->output_bss_
, v
SELECT_SIZE_ENDIAN(size
, big_endian
));
730 template<int size
, bool big_endian
>
732 Output_segment::write_section_headers_list(const Stringpool
* secnamepool
,
733 const Output_data_list
* pdl
,
735 ACCEPT_SIZE_ENDIAN
) const
737 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
738 for (Output_data_list::const_iterator p
= pdl
->begin();
742 if ((*p
)->is_section())
744 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
745 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
746 ps
->write_header(secnamepool
, &oshdr
);
753 // Output_file methods.
755 Output_file::Output_file(const General_options
& options
)
757 name_(options
.output_file_name()),
764 // Open the output file.
767 Output_file::open(off_t file_size
)
769 this->file_size_
= file_size
;
771 int mode
= this->options_
.is_relocatable() ? 0666 : 0777;
772 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
775 fprintf(stderr
, _("%s: %s: open: %s\n"),
776 program_name
, this->name_
, strerror(errno
));
781 // Write out one byte to make the file the right size.
782 if (::lseek(o
, file_size
- 1, SEEK_SET
) < 0)
784 fprintf(stderr
, _("%s: %s: lseek: %s\n"),
785 program_name
, this->name_
, strerror(errno
));
789 if (::write(o
, &b
, 1) != 1)
791 fprintf(stderr
, _("%s: %s: write: %s\n"),
792 program_name
, this->name_
, strerror(errno
));
796 // Map the file into memory.
797 void* base
= ::mmap(NULL
, file_size
, PROT_READ
| PROT_WRITE
,
799 if (base
== MAP_FAILED
)
801 fprintf(stderr
, _("%s: %s: mmap: %s\n"),
802 program_name
, this->name_
, strerror(errno
));
805 this->base_
= static_cast<unsigned char*>(base
);
808 // Close the output file.
813 if (::munmap(this->base_
, this->file_size_
) < 0)
815 fprintf(stderr
, _("%s: %s: munmap: %s\n"),
816 program_name
, this->name_
, strerror(errno
));
821 if (::close(this->o_
) < 0)
823 fprintf(stderr
, _("%s: %s: close: %s\n"),
824 program_name
, this->name_
, strerror(errno
));
830 // Instantiate the templates we need. We could use the configure
831 // script to restrict this to only the ones for implemented targets.
835 Output_section::add_input_section
<32, false>(
838 const elfcpp::Shdr
<32, false>& shdr
);
842 Output_section::add_input_section
<32, true>(
845 const elfcpp::Shdr
<32, true>& shdr
);
849 Output_section::add_input_section
<64, false>(
852 const elfcpp::Shdr
<64, false>& shdr
);
856 Output_section::add_input_section
<64, true>(
859 const elfcpp::Shdr
<64, true>& shdr
);
861 } // End namespace gold.