1 // output.cc -- manage the output file for gold
14 // Output_data methods.
16 Output_data::~Output_data()
20 // Set the address and offset.
23 Output_data::set_address(uint64_t addr, off_t off)
25 this->address_ = addr;
28 // Let the child class know.
29 this->do_set_address(addr, off);
32 // Return the default alignment for a size--32 or 64.
35 Output_data::default_alignment(int size)
45 // Output_data_const methods.
48 Output_data_const::do_write(Output_file* output)
50 output->write(this->offset(), data_.data(), data_.size());
53 // Output_section_header methods. This currently assumes that the
54 // segment and section lists are complete at construction time.
56 Output_section_headers::Output_section_headers(
58 const Layout::Segment_list& segment_list,
59 const Layout::Section_list& section_list)
61 segment_list_(segment_list),
62 section_list_(section_list)
64 // Count all the sections.
66 for (Layout::Segment_list::const_iterator p = segment_list.begin();
67 p != segment_list.end();
69 count += (*p)->output_section_count();
70 count += section_list.size();
74 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
76 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
80 this->set_data_size(count * shdr_size);
84 Output_section_headers::do_write(Output_file*)
86 // FIXME: Unimplemented.
90 // Output_segment_header methods.
93 Output_segment_headers::do_write(Output_file*)
95 // FIXME: Unimplemented.
99 // Output_file_header methods.
101 Output_file_header::Output_file_header(int size,
102 const General_options& options,
103 const Target* target,
104 const Symbol_table* symtab,
105 const Output_segment_headers* osh)
110 program_header_(osh),
111 section_header_(NULL),
116 // Set the section table information for a file header.
119 Output_file_header::set_section_info(const Output_section_headers* shdrs,
120 const Output_section* shstrtab)
122 this->section_header_ = shdrs;
123 this->shstrtab_ = shstrtab;
126 // Write out the file header.
129 Output_file_header::do_write(Output_file*)
131 // FIXME: Unimplemented.
135 // Output_section methods.
137 // Construct an Output_section. NAME will point into a Stringpool.
139 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
140 elfcpp::Elf_Xword flags)
151 Output_section::~Output_section()
155 // Add an input section to an Output_section. We don't keep track of
156 // input sections for an Output_section. Instead, each Object keeps
157 // track of the Output_section for each of its input sections.
159 template<int size, bool big_endian>
161 Output_section::add_input_section(Object* object, const char* secname,
162 const elfcpp::Shdr<size, big_endian>& shdr)
164 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
165 if ((addralign & (addralign - 1)) != 0)
167 fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
168 program_name, object->name().c_str(),
169 static_cast<unsigned long>(addralign), secname);
173 if (addralign > this->addralign_)
174 this->addralign_ = addralign;
176 off_t ssize = this->data_size();
177 ssize = (ssize + addralign - 1) &~ (addralign - 1);
179 // SHF_TLS/SHT_NOBITS sections are handled specially: they are
180 // treated as having no size and taking up no space. We only use
181 // the real size when setting the pt_memsz field of the PT_TLS
183 if ((this->flags_ & elfcpp::SHF_TLS) == 0
184 || this->type_ != elfcpp::SHT_NOBITS)
185 this->set_data_size(ssize + shdr.get_sh_size());
190 // Output_section_symtab methods.
192 Output_section_symtab::Output_section_symtab(const char* name, off_t size)
193 : Output_section(name, elfcpp::SHT_SYMTAB, 0)
195 this->set_data_size(size);
198 // Output_section_strtab methods.
200 Output_section_strtab::Output_section_strtab(const char* name,
201 Stringpool* contents)
202 : Output_section(name, elfcpp::SHT_STRTAB, 0),
208 Output_section_strtab::do_write(Output_file*)
210 // FIXME: Unimplemented.
214 // Output segment methods.
216 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
230 // Add an Output_section to an Output_segment.
233 Output_segment::add_output_section(Output_section* os,
234 elfcpp::Elf_Word seg_flags)
236 assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
238 // Update the segment flags and alignment.
239 this->flags_ |= seg_flags;
240 uint64_t addralign = os->addralign();
241 if (addralign > this->align_)
242 this->align_ = addralign;
244 Output_segment::Output_data_list* pdl;
245 if (os->type() == elfcpp::SHT_NOBITS)
246 pdl = &this->output_bss_;
248 pdl = &this->output_data_;
250 // So that PT_NOTE segments will work correctly, we need to ensure
251 // that all SHT_NOTE sections are adjacent. This will normally
252 // happen automatically, because all the SHT_NOTE input sections
253 // will wind up in the same output section. However, it is possible
254 // for multiple SHT_NOTE input sections to have different section
255 // flags, and thus be in different output sections, but for the
256 // different section flags to map into the same segment flags and
257 // thus the same output segment.
259 // Note that while there may be many input sections in an output
260 // section, there are normally only a few output sections in an
261 // output segment. This loop is expected to be fast.
263 if (os->type() == elfcpp::SHT_NOTE)
265 Layout::Data_list::iterator p = pdl->end();
269 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
276 while (p != pdl->begin());
279 // Similarly, so that PT_TLS segments will work, we need to group
280 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
281 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
282 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
284 if ((os->flags() & elfcpp::SHF_TLS) != 0)
286 pdl = &this->output_data_;
287 bool nobits = os->type() == elfcpp::SHT_NOBITS;
288 Layout::Data_list::iterator p = pdl->end();
292 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
293 && (nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS)))
300 while (p != pdl->begin());
306 // Add an Output_data (which is not an Output_section) to the start of
310 Output_segment::add_initial_output_data(Output_data* od)
312 uint64_t addralign = od->addralign();
313 if (addralign > this->align_)
314 this->align_ = addralign;
316 this->output_data_.push_front(od);
319 // Return the maximum alignment of the Output_data in Output_segment.
320 // We keep this up to date as we add Output_sections and Output_data.
323 Output_segment::max_data_align() const
328 // Set the section addresses for an Output_segment. ADDR is the
329 // address and *POFF is the file offset. Return the address of the
330 // immediately following segment. Update *POFF.
333 Output_segment::set_section_addresses(uint64_t addr, off_t* poff)
335 assert(this->type_ == elfcpp::PT_LOAD);
340 off_t orig_off = *poff;
341 this->offset_ = orig_off;
343 addr = this->set_section_list_addresses(&this->output_data_, addr, poff);
344 this->filesz_ = *poff - orig_off;
348 return this->set_section_list_addresses(&this->output_bss_, addr, poff);
349 this->memsz_ = *poff - orig_off;
351 // Ignore the file offset adjustments made by the BSS Output_data
356 // Set the addresses in a list of Output_data structures.
359 Output_segment::set_section_list_addresses(Output_data_list* pdl,
360 uint64_t addr, off_t* poff)
364 for (Output_data_list::iterator p = pdl->begin();
368 uint64_t addralign = (*p)->addralign();
369 addr = (addr + addralign - 1) & ~ (addralign - 1);
370 off = (off + addralign - 1) & ~ (addralign - 1);
371 (*p)->set_address(addr, off);
373 uint64_t size = (*p)->data_size();
382 // For a non-PT_LOAD segment, set the offset from the sections, if
386 Output_segment::set_offset()
388 assert(this->type_ != elfcpp::PT_LOAD);
390 if (this->output_data_.empty() && this->output_bss_.empty())
401 const Output_data* first;
402 if (this->output_data_.empty())
403 first = this->output_bss_.front();
405 first = this->output_data_.front();
406 this->vaddr_ = first->address();
407 this->paddr_ = this->vaddr_;
408 this->offset_ = first->offset();
410 if (this->output_data_.empty())
414 const Output_data* last_data = this->output_data_.back();
415 this->filesz_ = (last_data->address()
416 + last_data->data_size()
420 const Output_data* last;
421 if (this->output_bss_.empty())
422 last = this->output_data_.back();
424 last = this->output_bss_.back();
425 this->memsz_ = (last->address()
429 // this->align_ was set as we added items.
432 // Return the number of Output_sections in an Output_segment.
435 Output_segment::output_section_count() const
437 return (this->output_section_count_list(&this->output_data_)
438 + this->output_section_count_list(&this->output_bss_));
441 // Return the number of Output_sections in an Output_data_list.
444 Output_segment::output_section_count_list(const Output_data_list* pdl) const
446 unsigned int count = 0;
447 for (Output_data_list::const_iterator p = pdl->begin();
451 if ((*p)->is_section())
457 // Output_file methods.
460 Output_file::write(off_t, const void*, off_t)
465 // Instantiate the templates we need. We could use the configure
466 // script to restrict this to only the ones for implemented targets.
470 Output_section::add_input_section<32, false>(
473 const elfcpp::Shdr<32, false>& shdr);
477 Output_section::add_input_section<32, true>(
480 const elfcpp::Shdr<32, true>& shdr);
484 Output_section::add_input_section<64, false>(
487 const elfcpp::Shdr<64, false>& shdr);
491 Output_section::add_input_section<64, true>(
494 const elfcpp::Shdr<64, true>& shdr);
496 } // End namespace gold.