909230103685b9960a71c12070aef7b7be467fc0
1 // output.cc -- manage the output file for gold
13 // Output_data methods.
15 Output_data::~Output_data()
19 // Output_data_const methods.
22 Output_data_const::write(Output_file
* output
, off_t off
)
24 output
->write(off
, data_
.data(), data_
.size());
27 // Output_segment_header methods.
30 Output_segment_headers::write(Output_file
*, off_t
)
32 // FIXME: Unimplemented.
36 // Output_section methods.
38 // Construct an Output_section. NAME will point into a Stringpool.
40 Output_section::Output_section(const char* name
, elfcpp::Elf_Word type
,
41 elfcpp::Elf_Xword flags
)
54 Output_section::~Output_section()
58 // Add an input section to an Output_section. We don't keep track of
59 // input sections for an Output_section. Instead, each Object keeps
60 // track of the Output_section for each of its input sections.
62 template<int size
, bool big_endian
>
64 Output_section::add_input_section(Object
* object
, const char* secname
,
65 const elfcpp::Shdr
<size
, big_endian
>& shdr
)
67 elfcpp::Elf_Xword addralign
= shdr
.get_sh_addralign();
68 if ((addralign
& (addralign
- 1)) != 0)
70 fprintf(stderr
, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
71 program_name
, object
->name().c_str(),
72 static_cast<unsigned long>(addralign
), secname
);
76 if (addralign
> this->addralign_
)
77 this->addralign_
= addralign
;
79 off_t ssize
= this->get_size();
80 ssize
= (ssize
+ addralign
- 1) &~ (addralign
- 1);
82 this->set_size(ssize
+ shdr
.get_sh_size());
87 // Output segment methods.
89 Output_segment::Output_segment(elfcpp::Elf_Word type
, elfcpp::Elf_Word flags
)
102 // Add an Output_section to an Output_segment.
105 Output_segment::add_output_section(Output_section
* os
)
107 // Update the segment flags.
108 this->flags_
|= os
->flags() & (elfcpp::PF_R
| elfcpp::PF_W
| elfcpp::PF_X
);
110 // So that PT_NOTE segments will work correctly, we need to ensure
111 // that all SHT_NOTE sections are adjacent. This will normally
112 // happen automatically, because all the SHT_NOTE input sections
113 // will wind up in the same output section. However, it is possible
114 // for multiple SHT_NOTE input sections to have different section
115 // flags, and thus be in different output sections, but for the
116 // different section flags to map into the same segment flags and
117 // thus the same output segment.
119 // Note that while there may be many input sections in an output
120 // section, there are normally only a few output sections in an
121 // output segment. This loop is expected to be fast.
123 if (os
->type() == elfcpp::SHT_NOTE
)
125 for (Layout::Data_list::iterator p
= this->output_data_
.begin();
126 p
!= this->output_data_
.end();
129 if ((*p
)->is_section_type(elfcpp::SHT_NOTE
))
132 this->output_data_
.insert(p
, os
);
138 // Similarly, so that PT_TLS segments will work, we need to group
140 if ((os
->flags() & elfcpp::SHF_TLS
) != 0)
142 for (Layout::Data_list::iterator p
= this->output_data_
.begin();
143 p
!= this->output_data_
.end();
146 if ((*p
)->is_section_flag_set(elfcpp::SHF_TLS
))
149 this->output_data_
.insert(p
, os
);
155 this->output_data_
.push_back(os
);
158 // Output_file methods.
161 Output_file::write(off_t
, const void*, off_t
)
166 // Instantiate the templates we need. We could use the configure
167 // script to restrict this to only the ones for implemented targets.
171 Output_section::add_input_section
<32, false>(
174 const elfcpp::Shdr
<32, false>& shdr
);
178 Output_section::add_input_section
<32, true>(
181 const elfcpp::Shdr
<32, true>& shdr
);
185 Output_section::add_input_section
<64, false>(
188 const elfcpp::Shdr
<64, false>& shdr
);
192 Output_section::add_input_section
<64, true>(
195 const elfcpp::Shdr
<64, true>& shdr
);
197 } // End namespace gold.