1 // output.h -- manage the output file for gold -*- C++ -*-
15 class General_options;
19 template<int size, bool big_endian>
22 // An abtract class for data which has to go into the output file.
27 explicit Output_data(off_t data_size = 0)
28 : address_(0), data_size_(data_size), offset_(-1)
34 // Return the address.
37 { return this->address_; }
39 // Return the size of the data.
42 { return this->data_size_; }
44 // Return the file offset.
47 { return this->offset_; }
49 // Return the required alignment.
52 { return this->do_addralign(); }
54 // Return whether this is an Output_section.
57 { return this->do_is_section(); }
59 // Return whether this is an Output_section of the specified type.
61 is_section_type(elfcpp::Elf_Word stt) const
62 { return this->do_is_section_type(stt); }
64 // Return whether this is an Output_section with the specified flag
67 is_section_flag_set(elfcpp::Elf_Xword shf) const
68 { return this->do_is_section_flag_set(shf); }
70 // Set the address and file offset of this data.
72 set_address(uint64_t addr, off_t off);
74 // Write the data to the output file.
76 write(Output_file* file)
77 { this->do_write(file); }
80 // Functions that child classes may or in some cases must implement.
82 // Write the data to the output file.
84 do_write(Output_file*) = 0;
86 // Return the required alignment.
88 do_addralign() const = 0;
90 // Return whether this is an Output_section.
95 // Return whether this is an Output_section of the specified type.
96 // This only needs to be implement by Output_section.
98 do_is_section_type(elfcpp::Elf_Word) const
101 // Return whether this is an Output_section with the specific flag
102 // set. This only needs to be implemented by Output_section.
104 do_is_section_flag_set(elfcpp::Elf_Xword) const
107 // Set the address and file offset of the data. This only needs to
108 // be implemented if the child needs to know.
110 do_set_address(uint64_t, off_t)
113 // Functions that child classes may call.
115 // Set the size of the data.
117 set_data_size(off_t data_size)
118 { this->data_size_ = data_size; }
120 // Return default alignment for a size--32 or 64.
122 default_alignment(int size);
125 Output_data(const Output_data&);
126 Output_data& operator=(const Output_data&);
128 // Memory address in file (not always meaningful).
130 // Size of data in file.
132 // Offset within file.
136 // A simple case of Output_data in which we have constant data to
139 class Output_data_const : public Output_data
142 Output_data_const(const std::string& data, uint64_t addralign)
143 : Output_data(data.size()), data_(data), addralign_(addralign)
146 Output_data_const(const char* p, off_t len, uint64_t addralign)
147 : Output_data(len), data_(p, len), addralign_(addralign)
150 // Write the data to the file.
152 do_write(Output_file* output);
154 // Return the required alignment.
157 { return this->addralign_; }
164 // Output the section headers.
166 class Output_section_headers : public Output_data
169 Output_section_headers(int size,
171 const Layout::Segment_list&,
172 const Layout::Section_list&,
175 // Write the data to the file.
177 do_write(Output_file*);
179 // Return the required alignment.
182 { return Output_data::default_alignment(this->size_); }
185 // Write the data to the file with the right size and endianness.
186 template<int size, bool big_endian>
188 do_sized_write(Output_file*);
192 const Layout::Segment_list& segment_list_;
193 const Layout::Section_list& section_list_;
194 const Stringpool* secnamepool_;
197 // Output the segment headers.
199 class Output_segment_headers : public Output_data
202 Output_segment_headers(int size, bool big_endian,
203 const Layout::Segment_list& segment_list);
205 // Write the data to the file.
207 do_write(Output_file*);
209 // Return the required alignment.
212 { return Output_data::default_alignment(this->size_); }
215 // Write the data to the file with the right size and endianness.
216 template<int size, bool big_endian>
218 do_sized_write(Output_file*);
222 const Layout::Segment_list& segment_list_;
225 // Output the ELF file header.
227 class Output_file_header : public Output_data
230 Output_file_header(int size,
232 const General_options&,
235 const Output_segment_headers*);
237 // Add information about the section headers. We lay out the ELF
238 // file header before we create the section headers.
239 void set_section_info(const Output_section_headers*,
240 const Output_section* shstrtab);
242 // Write the data to the file.
244 do_write(Output_file*);
246 // Return the required alignment.
249 { return Output_data::default_alignment(this->size_); }
251 // Set the address and offset--we only implement this for error
254 do_set_address(uint64_t, off_t off) const
255 { assert(off == 0); }
258 // Write the data to the file with the right size and endianness.
259 template<int size, bool big_endian>
261 do_sized_write(Output_file*);
265 const General_options& options_;
266 const Target* target_;
267 const Symbol_table* symtab_;
268 const Output_segment_headers* segment_header_;
269 const Output_section_headers* section_header_;
270 const Output_section* shstrtab_;
273 // An output section. We don't expect to have too many output
274 // sections, so we don't bother to do a template on the size.
276 class Output_section : public Output_data
279 // Create an output section, giving the name, type, and flags.
280 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword,
282 virtual ~Output_section();
284 // Add a new input section named NAME with header SHDR from object
285 // OBJECT. Return the offset within the output section.
286 template<int size, bool big_endian>
288 add_input_section(Object* object, const char *name,
289 const elfcpp::Shdr<size, big_endian>& shdr);
291 // Return the section name.
294 { return this->name_; }
296 // Return the section type.
299 { return this->type_; }
301 // Return the section flags.
304 { return this->flags_; }
306 // Return the address alignment.
309 { return this->addralign_; }
311 // Return the section index.
314 { return this->shndx_; }
316 // Set the entsize field.
318 set_entsize(uint64_t v)
319 { this->entsize_ = v; }
321 // Set the link field.
323 set_link(unsigned int v)
326 // Set the info field.
328 set_info(unsigned int v)
331 // Set the addralign field.
333 set_addralign(uint64_t v)
334 { this->addralign_ = v; }
336 // Write the data to the file. For a typical Output_section, this
337 // does nothing. We write out the data by looping over all the
340 do_write(Output_file*)
343 // Return the address alignment--function required by parent class.
346 { return this->addralign_; }
348 // Return whether this is an Output_section.
350 do_is_section() const
353 // Return whether this is a section of the specified type.
355 do_is_section_type(elfcpp::Elf_Word type) const
356 { return this->type_ == type; }
358 // Return whether the specified section flag is set.
360 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
361 { return (this->flags_ & flag) != 0; }
363 // Write the section header into *OPHDR.
364 template<int size, bool big_endian>
366 write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const;
369 // Most of these fields are only valid after layout.
371 // The name of the section. This will point into a Stringpool.
373 // The section address is in the parent class.
374 // The section alignment.
376 // The section entry size.
378 // The file offset is in the parent class.
379 // The section link field.
381 // The section info field.
384 elfcpp::Elf_Word type_;
385 // The section flags.
386 elfcpp::Elf_Xword flags_;
387 // The section index.
391 // A special Output_section which represents the symbol table
394 class Output_section_symtab : public Output_section
397 Output_section_symtab(const char* name, off_t size, unsigned int shndx);
400 // A special Output_section which holds a string table.
402 class Output_section_strtab : public Output_section
405 Output_section_strtab(const char* name, Stringpool* contents,
408 // Write out the data.
410 do_write(Output_file*);
413 Stringpool* contents_;
416 // An output segment. PT_LOAD segments are built from collections of
417 // output sections. Other segments typically point within PT_LOAD
418 // segments, and are built directly as needed.
423 // Create an output segment, specifying the type and flags.
424 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
426 // Return the virtual address.
429 { return this->vaddr_; }
431 // Return the physical address.
434 { return this->paddr_; }
436 // Return the segment type.
439 { return this->type_; }
441 // Return the segment flags.
444 { return this->flags_; }
446 // Return the maximum alignment of the Output_data.
448 max_data_align() const;
450 // Add an Output_section to this segment.
452 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags);
454 // Add an Output_data (which is not an Output_section) to the start
457 add_initial_output_data(Output_data*);
459 // Set the address of the segment to ADDR and the offset to *POFF
460 // (aligned if necessary), and set the addresses and offsets of all
461 // contained output sections accordingly. Return the address of the
462 // immediately following segment. Update *POFF. This should only
463 // be called for a PT_LOAD segment.
465 set_section_addresses(uint64_t addr, off_t* poff);
467 // Set the offset of this segment based on the section. This should
468 // only be called for a non-PT_LOAD segment.
472 // Return the number of output sections.
474 output_section_count() const;
476 // Write the segment header into *OPHDR.
477 template<int size, bool big_endian>
479 write_header(elfcpp::Phdr_write<size, big_endian>*) const;
481 // Write the section headers of associated sections into V.
482 template<int size, bool big_endian>
484 write_section_headers(const Stringpool*, unsigned char* v) const;
487 Output_segment(const Output_segment&);
488 Output_segment& operator=(const Output_segment&);
490 typedef std::list<Output_data*> Output_data_list;
492 // Set the section addresses in an Output_data_list.
494 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff);
496 // Return the number of Output_sections in an Output_data_list.
498 output_section_count_list(const Output_data_list*) const;
500 // Write the section headers in the list into V.
501 template<int size, bool big_endian>
503 write_section_headers_list(const Stringpool*, const Output_data_list*,
504 unsigned char* v) const;
506 // The list of output data with contents attached to this segment.
507 Output_data_list output_data_;
508 // The list of output data without contents attached to this segment.
509 Output_data_list output_bss_;
510 // The segment virtual address.
512 // The segment physical address.
514 // The size of the segment in memory.
516 // The segment alignment.
518 // The offset of the segment data within the file.
520 // The size of the segment data in the file.
523 elfcpp::Elf_Word type_;
524 // The segment flags.
525 elfcpp::Elf_Word flags_;
528 // This class represents the output file.
533 Output_file(const General_options& options);
535 // Open the output file. FILE_SIZE is the final size of the file.
537 open(off_t file_size);
539 // Close the output file and make sure there are no error.
543 // We currently always use mmap which makes the view handling quite
544 // simple. In the future we may support other approaches.
546 // Write data to the output file.
548 write(off_t offset, const void* data, off_t len)
549 { memcpy(this->base_ + offset, data, len); }
551 // Get a buffer to use to write to the file, given the offset into
552 // the file and the size.
554 get_output_view(off_t start, off_t size)
556 assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
557 return this->base_ + start;
560 // VIEW must have been returned by get_output_view. Write the
561 // buffer to the file, passing in the offset and the size.
563 write_output_view(off_t, off_t, unsigned char*)
568 const General_options& options_;
575 // Base of file mapped into memory.
576 unsigned char* base_;
579 } // End namespace gold.
581 #endif // !defined(GOLD_OUTPUT_H)