1 // output.cc -- manage the output file for gold
20 // Output_data methods.
22 Output_data::~Output_data()
26 // Set the address and offset.
29 Output_data::set_address(uint64_t addr
, off_t off
)
31 this->address_
= addr
;
34 // Let the child class know.
35 this->do_set_address(addr
, off
);
38 // Return the default alignment for a size--32 or 64.
41 Output_data::default_alignment(int size
)
51 // Output_data_const methods.
54 Output_data_const::do_write(Output_file
* output
)
56 output
->write(this->offset(), data_
.data(), data_
.size());
59 // Output_section_header methods. This currently assumes that the
60 // segment and section lists are complete at construction time.
62 Output_section_headers::Output_section_headers(
65 const Layout::Segment_list
& segment_list
,
66 const Layout::Section_list
& section_list
,
67 const Stringpool
* secnamepool
)
69 big_endian_(big_endian
),
70 segment_list_(segment_list
),
71 section_list_(section_list
),
72 secnamepool_(secnamepool
)
74 // Count all the sections. Start with 1 for the null section.
76 for (Layout::Segment_list::const_iterator p
= segment_list
.begin();
77 p
!= segment_list
.end();
79 if ((*p
)->type() == elfcpp::PT_LOAD
)
80 count
+= (*p
)->output_section_count();
81 count
+= section_list
.size();
85 shdr_size
= elfcpp::Elf_sizes
<32>::shdr_size
;
87 shdr_size
= elfcpp::Elf_sizes
<64>::shdr_size
;
91 this->set_data_size(count
* shdr_size
);
94 // Write out the section headers.
97 Output_section_headers::do_write(Output_file
* of
)
99 if (this->size_
== 32)
101 if (this->big_endian_
)
102 this->do_sized_write
<32, true>(of
);
104 this->do_sized_write
<32, false>(of
);
106 else if (this->size_
== 64)
108 if (this->big_endian_
)
109 this->do_sized_write
<64, true>(of
);
111 this->do_sized_write
<64, false>(of
);
117 template<int size
, bool big_endian
>
119 Output_section_headers::do_sized_write(Output_file
* of
)
121 off_t all_shdrs_size
= this->data_size();
122 unsigned char* view
= of
->get_output_view(this->offset(), all_shdrs_size
);
124 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
125 unsigned char* v
= view
;
128 typename
elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
129 oshdr
.put_sh_name(0);
130 oshdr
.put_sh_type(elfcpp::SHT_NULL
);
131 oshdr
.put_sh_flags(0);
132 oshdr
.put_sh_addr(0);
133 oshdr
.put_sh_offset(0);
134 oshdr
.put_sh_size(0);
135 oshdr
.put_sh_link(0);
136 oshdr
.put_sh_info(0);
137 oshdr
.put_sh_addralign(0);
138 oshdr
.put_sh_entsize(0);
144 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
145 p
!= this->segment_list_
.end();
147 v
= (*p
)->write_section_headers
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
148 this->secnamepool_
, v
, &shndx
149 SELECT_SIZE_ENDIAN(size
, big_endian
));
150 for (Layout::Section_list::const_iterator p
= this->section_list_
.begin();
151 p
!= this->section_list_
.end();
154 assert(shndx
== (*p
)->out_shndx());
155 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
156 (*p
)->write_header(this->secnamepool_
, &oshdr
);
161 of
->write_output_view(this->offset(), all_shdrs_size
, view
);
164 // Output_segment_header methods.
166 Output_segment_headers::Output_segment_headers(
169 const Layout::Segment_list
& segment_list
)
170 : size_(size
), big_endian_(big_endian
), segment_list_(segment_list
)
174 phdr_size
= elfcpp::Elf_sizes
<32>::phdr_size
;
176 phdr_size
= elfcpp::Elf_sizes
<64>::phdr_size
;
180 this->set_data_size(segment_list
.size() * phdr_size
);
184 Output_segment_headers::do_write(Output_file
* of
)
186 if (this->size_
== 32)
188 if (this->big_endian_
)
189 this->do_sized_write
<32, true>(of
);
191 this->do_sized_write
<32, false>(of
);
193 else if (this->size_
== 64)
195 if (this->big_endian_
)
196 this->do_sized_write
<64, true>(of
);
198 this->do_sized_write
<64, false>(of
);
204 template<int size
, bool big_endian
>
206 Output_segment_headers::do_sized_write(Output_file
* of
)
208 const int phdr_size
= elfcpp::Elf_sizes
<size
>::phdr_size
;
209 off_t all_phdrs_size
= this->segment_list_
.size() * phdr_size
;
210 unsigned char* view
= of
->get_output_view(this->offset(),
212 unsigned char* v
= view
;
213 for (Layout::Segment_list::const_iterator p
= this->segment_list_
.begin();
214 p
!= this->segment_list_
.end();
217 elfcpp::Phdr_write
<size
, big_endian
> ophdr(v
);
218 (*p
)->write_header(&ophdr
);
222 of
->write_output_view(this->offset(), all_phdrs_size
, view
);
225 // Output_file_header methods.
227 Output_file_header::Output_file_header(int size
,
229 const General_options
& options
,
230 const Target
* target
,
231 const Symbol_table
* symtab
,
232 const Output_segment_headers
* osh
)
234 big_endian_(big_endian
),
238 segment_header_(osh
),
239 section_header_(NULL
),
244 ehdr_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
246 ehdr_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
250 this->set_data_size(ehdr_size
);
253 // Set the section table information for a file header.
256 Output_file_header::set_section_info(const Output_section_headers
* shdrs
,
257 const Output_section
* shstrtab
)
259 this->section_header_
= shdrs
;
260 this->shstrtab_
= shstrtab
;
263 // Write out the file header.
266 Output_file_header::do_write(Output_file
* of
)
268 if (this->size_
== 32)
270 if (this->big_endian_
)
271 this->do_sized_write
<32, true>(of
);
273 this->do_sized_write
<32, false>(of
);
275 else if (this->size_
== 64)
277 if (this->big_endian_
)
278 this->do_sized_write
<64, true>(of
);
280 this->do_sized_write
<64, false>(of
);
286 // Write out the file header with appropriate size and endianess.
288 template<int size
, bool big_endian
>
290 Output_file_header::do_sized_write(Output_file
* of
)
292 assert(this->offset() == 0);
294 int ehdr_size
= elfcpp::Elf_sizes
<size
>::ehdr_size
;
295 unsigned char* view
= of
->get_output_view(0, ehdr_size
);
296 elfcpp::Ehdr_write
<size
, big_endian
> oehdr(view
);
298 unsigned char e_ident
[elfcpp::EI_NIDENT
];
299 memset(e_ident
, 0, elfcpp::EI_NIDENT
);
300 e_ident
[elfcpp::EI_MAG0
] = elfcpp::ELFMAG0
;
301 e_ident
[elfcpp::EI_MAG1
] = elfcpp::ELFMAG1
;
302 e_ident
[elfcpp::EI_MAG2
] = elfcpp::ELFMAG2
;
303 e_ident
[elfcpp::EI_MAG3
] = elfcpp::ELFMAG3
;
305 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS32
;
307 e_ident
[elfcpp::EI_CLASS
] = elfcpp::ELFCLASS64
;
310 e_ident
[elfcpp::EI_DATA
] = (big_endian
311 ? elfcpp::ELFDATA2MSB
312 : elfcpp::ELFDATA2LSB
);
313 e_ident
[elfcpp::EI_VERSION
] = elfcpp::EV_CURRENT
;
314 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
315 oehdr
.put_e_ident(e_ident
);
319 if (this->options_
.is_relocatable())
320 e_type
= elfcpp::ET_REL
;
322 e_type
= elfcpp::ET_EXEC
;
323 oehdr
.put_e_type(e_type
);
325 oehdr
.put_e_machine(this->target_
->machine_code());
326 oehdr
.put_e_version(elfcpp::EV_CURRENT
);
328 // FIXME: Need to support -e, and target specific entry symbol.
329 Symbol
* sym
= this->symtab_
->lookup("_start");
330 typename Sized_symbol
<size
>::Value_type v
;
335 Sized_symbol
<size
>* ssym
;
336 ssym
= this->symtab_
->get_sized_symbol
SELECT_SIZE_NAME(size
) (
337 sym
SELECT_SIZE(size
));
340 oehdr
.put_e_entry(v
);
342 oehdr
.put_e_phoff(this->segment_header_
->offset());
343 oehdr
.put_e_shoff(this->section_header_
->offset());
345 // FIXME: The target needs to set the flags.
346 oehdr
.put_e_flags(0);
348 oehdr
.put_e_ehsize(elfcpp::Elf_sizes
<size
>::ehdr_size
);
349 oehdr
.put_e_phentsize(elfcpp::Elf_sizes
<size
>::phdr_size
);
350 oehdr
.put_e_phnum(this->segment_header_
->data_size()
351 / elfcpp::Elf_sizes
<size
>::phdr_size
);
352 oehdr
.put_e_shentsize(elfcpp::Elf_sizes
<size
>::shdr_size
);
353 oehdr
.put_e_shnum(this->section_header_
->data_size()
354 / elfcpp::Elf_sizes
<size
>::shdr_size
);
355 oehdr
.put_e_shstrndx(this->shstrtab_
->out_shndx());
357 of
->write_output_view(0, ehdr_size
, view
);
360 // Output_section_got::Got_entry methods.
362 // Write out the entry.
364 template<int size
, bool big_endian
>
366 Output_section_got
<size
, big_endian
>::Got_entry::write(unsigned char* pov
)
371 switch (this->local_sym_index_
)
375 Symbol
* gsym
= this->u_
.gsym
;
377 // If the symbol is resolved locally, we need to write out its
378 // value. Otherwise we just write zero. The target code is
379 // responsible for creating a relocation entry to fill in the
381 if (gsym
->is_resolved_locally())
383 Sized_symbol
<size
>* sgsym
;
384 // This cast is a bit ugly. We don't want to put a
385 // virtual method in Symbol, because we want Symbol to be
386 // as small as possible.
387 sgsym
= static_cast<Sized_symbol
<size
>*>(gsym
);
388 val
= sgsym
->value();
394 val
= this->u_
.constant
;
401 Valtype
* povv
= reinterpret_cast<Valtype
*>(pov
);
402 Swap
<size
, big_endian
>::writeval(povv
, val
);
405 // Output_section_data methods.
408 Output_section_data::do_out_shndx() const
410 assert(this->output_section_
!= NULL
);
411 return this->output_section_
->out_shndx();
414 // Output_section_got methods.
416 // Write out the GOT.
418 template<int size
, bool big_endian
>
420 Output_section_got
<size
, big_endian
>::do_write(Output_file
* of
)
422 const int add
= size
/ 8;
424 const off_t off
= this->offset();
425 const off_t oview_size
= this->entries_
.size() * add
;
426 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
428 unsigned char* pov
= oview
;
429 for (typename
Got_entries::const_iterator p
= this->entries_
.begin();
430 p
!= this->entries_
.end();
437 of
->write_output_view(off
, oview_size
, oview
);
439 // We no longer need the GOT entries.
440 this->entries_
.clear();
443 // Output_section::Input_section methods.
445 // Return the data size. For an input section we store the size here.
446 // For an Output_section_data, we have to ask it for the size.
449 Output_section::Input_section::data_size() const
451 if (this->is_input_section())
452 return this->data_size_
;
454 return this->u_
.posd
->data_size();
457 // Set the address and file offset.
460 Output_section::Input_section::set_address(uint64_t addr
, off_t off
,
463 if (this->is_input_section())
464 this->u_
.object
->set_section_offset(this->shndx_
, off
- secoff
);
466 this->u_
.posd
->set_address(addr
, off
);
469 // Write out the data. We don't have to do anything for an input
470 // section--they are handled via Object::relocate--but this is where
471 // we write out the data for an Output_section_data.
474 Output_section::Input_section::write(Output_file
* of
)
476 if (!this->is_input_section())
477 this->u_
.posd
->write(of
);
480 // Output_section methods.
482 // Construct an Output_section. NAME will point into a Stringpool.
484 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
485 elfcpp::Elf_Xword flags
, bool may_add_data
)
495 first_input_offset_(0),
496 may_add_data_(may_add_data
)
500 Output_section::~Output_section()
504 // Add the input section SHNDX, with header SHDR, named SECNAME, in
505 // OBJECT, to the Output_section. Return the offset of the input
506 // section within the output section. We don't always keep track of
507 // input sections for an Output_section. Instead, each Object keeps
508 // track of the Output_section for each of its input sections.
510 template<int size
, bool big_endian
>
512 Output_section::add_input_section(Object
* object
, unsigned int shndx
,
514 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
516 assert(this->may_add_data_
);
518 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
519 if ((addralign
& (addralign
- 1)) != 0)
521 fprintf(stderr
, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
522 program_name
, object
->name().c_str(),
523 static_cast<unsigned long>(addralign
), secname
);
527 if (addralign
> this->addralign_
)
528 this->addralign_
= addralign
;
530 off_t ssize
= this->data_size();
531 ssize
= align_address(ssize
, addralign
);
532 this->set_data_size(ssize
+ shdr
.get_sh_size());
534 // We need to keep track of this section if we are already keeping
535 // track of sections, or if we are relaxing. FIXME: Add test for
537 if (! this->input_sections_
.empty())
538 this->input_sections_
.push_back(Input_section(object
, shndx
,
545 // Add arbitrary data to an output section.
548 Output_section::add_output_section_data(Output_section_data
* posd
)
550 if (this->input_sections_
.empty())
551 this->first_input_offset_
= this->data_size();
552 this->input_sections_
.push_back(Input_section(posd
));
553 uint64_t addralign
= posd
->addralign();
554 if (addralign
> this->addralign_
)
555 this->addralign_
= addralign
;
556 posd
->set_output_section(this);
559 // Set the address of an Output_section. This is where we handle
560 // setting the addresses of any Output_section_data objects.
563 Output_section::do_set_address(uint64_t address
, off_t startoff
)
565 if (this->input_sections_
.empty())
568 off_t off
= startoff
+ this->first_input_offset_
;
569 for (Input_section_list::iterator p
= this->input_sections_
.begin();
570 p
!= this->input_sections_
.end();
573 off
= align_address(off
, p
->addralign());
574 p
->set_address(address
+ (off
- startoff
), off
, startoff
);
575 off
+= p
->data_size();
578 this->set_data_size(off
- startoff
);
581 // Write the section header to *OSHDR.
583 template<int size
, bool big_endian
>
585 Output_section::write_header(const Stringpool
* secnamepool
,
586 elfcpp::Shdr_write
<size
, big_endian
>* oshdr
) const
588 oshdr
->put_sh_name(secnamepool
->get_offset(this->name_
));
589 oshdr
->put_sh_type(this->type_
);
590 oshdr
->put_sh_flags(this->flags_
);
591 oshdr
->put_sh_addr(this->address());
592 oshdr
->put_sh_offset(this->offset());
593 oshdr
->put_sh_size(this->data_size());
594 oshdr
->put_sh_link(this->link_
);
595 oshdr
->put_sh_info(this->info_
);
596 oshdr
->put_sh_addralign(this->addralign_
);
597 oshdr
->put_sh_entsize(this->entsize_
);
600 // Write out the data. For input sections the data is written out by
601 // Object::relocate, but we have to handle Output_section_data objects
605 Output_section::do_write(Output_file
* of
)
607 for (Input_section_list::iterator p
= this->input_sections_
.begin();
608 p
!= this->input_sections_
.end();
613 // Output_section_symtab methods.
615 Output_section_symtab::Output_section_symtab(const char* name
, off_t size
)
616 : Output_section(name
, elfcpp::SHT_SYMTAB
, 0, false)
618 this->set_data_size(size
);
621 // Output_section_strtab methods.
623 Output_section_strtab::Output_section_strtab(const char* name
,
624 Stringpool
* contents
)
625 : Output_section(name
, elfcpp::SHT_STRTAB
, 0, false),
628 this->set_data_size(contents
->get_strtab_size());
632 Output_section_strtab::do_write(Output_file
* of
)
634 this->contents_
->write(of
, this->offset());
637 // Output segment methods.
639 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
650 is_align_known_(false)
654 // Add an Output_section to an Output_segment.
657 Output_segment::add_output_section(Output_section
* os
,
658 elfcpp::Elf_Word seg_flags
)
660 assert((os
->flags() & elfcpp::SHF_ALLOC
) != 0);
661 assert(!this->is_align_known_
);
663 // Update the segment flags.
664 this->flags_
|= seg_flags
;
666 Output_segment::Output_data_list
* pdl
;
667 if (os
->type() == elfcpp::SHT_NOBITS
)
668 pdl
= &this->output_bss_
;
670 pdl
= &this->output_data_
;
672 // So that PT_NOTE segments will work correctly, we need to ensure
673 // that all SHT_NOTE sections are adjacent. This will normally
674 // happen automatically, because all the SHT_NOTE input sections
675 // will wind up in the same output section. However, it is possible
676 // for multiple SHT_NOTE input sections to have different section
677 // flags, and thus be in different output sections, but for the
678 // different section flags to map into the same segment flags and
679 // thus the same output segment.
681 // Note that while there may be many input sections in an output
682 // section, there are normally only a few output sections in an
683 // output segment. This loop is expected to be fast.
685 if (os
->type() == elfcpp::SHT_NOTE
&& !pdl
->empty())
687 Layout::Data_list::iterator p
= pdl
->end();
691 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
698 while (p
!= pdl
->begin());
701 // Similarly, so that PT_TLS segments will work, we need to group
702 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
703 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
704 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
706 if ((os
->flags() & elfcpp::SHF_TLS
) != 0 && !this->output_data_
.empty())
708 pdl
= &this->output_data_
;
709 bool nobits
= os
->type() == elfcpp::SHT_NOBITS
;
711 Layout::Data_list::iterator p
= pdl
->end();
716 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
719 // Put a NOBITS section after the first TLS section.
720 // But a PROGBITS section after the first TLS/PROGBITS
722 insert
= nobits
|| !(*p
)->is_section_type(elfcpp::SHT_NOBITS
);
726 // If we've gone past the TLS sections, but we've seen a
727 // TLS section, then we need to insert this section now.
738 while (p
!= pdl
->begin());
740 // There are no TLS sections yet; put this one at the end of the
747 // Add an Output_data (which is not an Output_section) to the start of
751 Output_segment::add_initial_output_data(Output_data
* od
)
753 assert(!this->is_align_known_
);
754 this->output_data_
.push_front(od
);
757 // Return the maximum alignment of the Output_data in Output_segment.
758 // Once we compute this, we prohibit new sections from being added.
761 Output_segment::addralign()
763 if (!this->is_align_known_
)
767 addralign
= Output_segment::maximum_alignment(&this->output_data_
);
768 if (addralign
> this->align_
)
769 this->align_
= addralign
;
771 addralign
= Output_segment::maximum_alignment(&this->output_bss_
);
772 if (addralign
> this->align_
)
773 this->align_
= addralign
;
775 this->is_align_known_
= true;
781 // Return the maximum alignment of a list of Output_data.
784 Output_segment::maximum_alignment(const Output_data_list
* pdl
)
787 for (Output_data_list::const_iterator p
= pdl
->begin();
791 uint64_t addralign
= (*p
)->addralign();
798 // Set the section addresses for an Output_segment. ADDR is the
799 // address and *POFF is the file offset. Set the section indexes
800 // starting with *PSHNDX. Return the address of the immediately
801 // following segment. Update *POFF and *PSHNDX.
804 Output_segment::set_section_addresses(uint64_t addr
, off_t
* poff
,
805 unsigned int* pshndx
)
807 assert(this->type_
== elfcpp::PT_LOAD
);
812 off_t orig_off
= *poff
;
813 this->offset_
= orig_off
;
815 *poff
= align_address(*poff
, this->addralign());
817 addr
= this->set_section_list_addresses(&this->output_data_
, addr
, poff
,
819 this->filesz_
= *poff
- orig_off
;
823 uint64_t ret
= this->set_section_list_addresses(&this->output_bss_
, addr
,
825 this->memsz_
= *poff
- orig_off
;
827 // Ignore the file offset adjustments made by the BSS Output_data
834 // Set the addresses in a list of Output_data structures.
837 Output_segment::set_section_list_addresses(Output_data_list
* pdl
,
838 uint64_t addr
, off_t
* poff
,
839 unsigned int* pshndx
)
841 off_t startoff
= *poff
;
843 off_t off
= startoff
;
844 for (Output_data_list::iterator p
= pdl
->begin();
848 off
= align_address(off
, (*p
)->addralign());
849 (*p
)->set_address(addr
+ (off
- startoff
), off
);
851 // Unless this is a PT_TLS segment, we want to ignore the size
852 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
853 // affect the size of a PT_LOAD segment.
854 if (this->type_
== elfcpp::PT_TLS
855 || !(*p
)->is_section_flag_set(elfcpp::SHF_TLS
)
856 || !(*p
)->is_section_type(elfcpp::SHT_NOBITS
))
857 off
+= (*p
)->data_size();
859 if ((*p
)->is_section())
861 (*p
)->set_out_shndx(*pshndx
);
867 return addr
+ (off
- startoff
);
870 // For a non-PT_LOAD segment, set the offset from the sections, if
874 Output_segment::set_offset()
876 assert(this->type_
!= elfcpp::PT_LOAD
);
878 if (this->output_data_
.empty() && this->output_bss_
.empty())
889 const Output_data
* first
;
890 if (this->output_data_
.empty())
891 first
= this->output_bss_
.front();
893 first
= this->output_data_
.front();
894 this->vaddr_
= first
->address();
895 this->paddr_
= this->vaddr_
;
896 this->offset_
= first
->offset();
898 if (this->output_data_
.empty())
902 const Output_data
* last_data
= this->output_data_
.back();
903 this->filesz_
= (last_data
->address()
904 + last_data
->data_size()
908 const Output_data
* last
;
909 if (this->output_bss_
.empty())
910 last
= this->output_data_
.back();
912 last
= this->output_bss_
.back();
913 this->memsz_
= (last
->address()
918 // Return the number of Output_sections in an Output_segment.
921 Output_segment::output_section_count() const
923 return (this->output_section_count_list(&this->output_data_
)
924 + this->output_section_count_list(&this->output_bss_
));
927 // Return the number of Output_sections in an Output_data_list.
930 Output_segment::output_section_count_list(const Output_data_list
* pdl
) const
932 unsigned int count
= 0;
933 for (Output_data_list::const_iterator p
= pdl
->begin();
937 if ((*p
)->is_section())
943 // Write the segment data into *OPHDR.
945 template<int size
, bool big_endian
>
947 Output_segment::write_header(elfcpp::Phdr_write
<size
, big_endian
>* ophdr
)
949 ophdr
->put_p_type(this->type_
);
950 ophdr
->put_p_offset(this->offset_
);
951 ophdr
->put_p_vaddr(this->vaddr_
);
952 ophdr
->put_p_paddr(this->paddr_
);
953 ophdr
->put_p_filesz(this->filesz_
);
954 ophdr
->put_p_memsz(this->memsz_
);
955 ophdr
->put_p_flags(this->flags_
);
956 ophdr
->put_p_align(this->addralign());
959 // Write the section headers into V.
961 template<int size
, bool big_endian
>
963 Output_segment::write_section_headers(const Stringpool
* secnamepool
,
966 ACCEPT_SIZE_ENDIAN
) const
968 // Every section that is attached to a segment must be attached to a
969 // PT_LOAD segment, so we only write out section headers for PT_LOAD
971 if (this->type_
!= elfcpp::PT_LOAD
)
974 v
= this->write_section_headers_list
975 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
976 secnamepool
, &this->output_data_
, v
, pshndx
977 SELECT_SIZE_ENDIAN(size
, big_endian
));
978 v
= this->write_section_headers_list
979 SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
980 secnamepool
, &this->output_bss_
, v
, pshndx
981 SELECT_SIZE_ENDIAN(size
, big_endian
));
985 template<int size
, bool big_endian
>
987 Output_segment::write_section_headers_list(const Stringpool
* secnamepool
,
988 const Output_data_list
* pdl
,
991 ACCEPT_SIZE_ENDIAN
) const
993 const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
994 for (Output_data_list::const_iterator p
= pdl
->begin();
998 if ((*p
)->is_section())
1000 const Output_section
* ps
= static_cast<const Output_section
*>(*p
);
1001 assert(*pshndx
== ps
->out_shndx());
1002 elfcpp::Shdr_write
<size
, big_endian
> oshdr(v
);
1003 ps
->write_header(secnamepool
, &oshdr
);
1011 // Output_file methods.
1013 Output_file::Output_file(const General_options
& options
)
1014 : options_(options
),
1015 name_(options
.output_file_name()),
1022 // Open the output file.
1025 Output_file::open(off_t file_size
)
1027 this->file_size_
= file_size
;
1029 int mode
= this->options_
.is_relocatable() ? 0666 : 0777;
1030 int o
= ::open(this->name_
, O_RDWR
| O_CREAT
| O_TRUNC
, mode
);
1033 fprintf(stderr
, _("%s: %s: open: %s\n"),
1034 program_name
, this->name_
, strerror(errno
));
1039 // Write out one byte to make the file the right size.
1040 if (::lseek(o
, file_size
- 1, SEEK_SET
) < 0)
1042 fprintf(stderr
, _("%s: %s: lseek: %s\n"),
1043 program_name
, this->name_
, strerror(errno
));
1047 if (::write(o
, &b
, 1) != 1)
1049 fprintf(stderr
, _("%s: %s: write: %s\n"),
1050 program_name
, this->name_
, strerror(errno
));
1054 // Map the file into memory.
1055 void* base
= ::mmap(NULL
, file_size
, PROT_READ
| PROT_WRITE
,
1057 if (base
== MAP_FAILED
)
1059 fprintf(stderr
, _("%s: %s: mmap: %s\n"),
1060 program_name
, this->name_
, strerror(errno
));
1063 this->base_
= static_cast<unsigned char*>(base
);
1066 // Close the output file.
1069 Output_file::close()
1071 if (::munmap(this->base_
, this->file_size_
) < 0)
1073 fprintf(stderr
, _("%s: %s: munmap: %s\n"),
1074 program_name
, this->name_
, strerror(errno
));
1079 if (::close(this->o_
) < 0)
1081 fprintf(stderr
, _("%s: %s: close: %s\n"),
1082 program_name
, this->name_
, strerror(errno
));
1088 // Instantiate the templates we need. We could use the configure
1089 // script to restrict this to only the ones for implemented targets.
1093 Output_section::add_input_section
<32, false>(
1096 const char* secname
,
1097 const elfcpp::Shdr
<32, false>& shdr
);
1101 Output_section::add_input_section
<32, true>(
1104 const char* secname
,
1105 const elfcpp::Shdr
<32, true>& shdr
);
1109 Output_section::add_input_section
<64, false>(
1112 const char* secname
,
1113 const elfcpp::Shdr
<64, false>& shdr
);
1117 Output_section::add_input_section
<64, true>(
1120 const char* secname
,
1121 const elfcpp::Shdr
<64, true>& shdr
);
1125 Output_section_got
<32, false>::do_write(Output_file
* of
);
1129 Output_section_got
<32, true>::do_write(Output_file
* of
);
1133 Output_section_got
<64, false>::do_write(Output_file
* of
);
1137 Output_section_got
<64, true>::do_write(Output_file
* of
);
1139 } // End namespace gold.