1 // output.h -- manage the output file for gold -*- C++ -*-
18 template<int size
, bool big_endian
>
21 // An abtract class for data which has to go into the output file.
26 explicit Output_data(off_t data_size
= 0)
27 : address_(0), data_size_(data_size
), offset_(0)
33 // Return the address.
36 { return this->address_
; }
38 // Return the size of the data.
41 { return this->data_size_
; }
43 // Return the file offset.
46 { return this->offset_
; }
48 // Return the required alignment.
51 { return this->do_addralign(); }
53 // Return whether this is an Output_section.
56 { return this->do_is_section(); }
58 // Return whether this is an Output_section of the specified type.
60 is_section_type(elfcpp::Elf_Word stt
) const
61 { return this->do_is_section_type(stt
); }
63 // Return whether this is an Output_section with the specified flag
66 is_section_flag_set(elfcpp::Elf_Xword shf
) const
67 { return this->do_is_section_flag_set(shf
); }
69 // Set the address and file offset of this data.
71 set_address(uint64_t addr
, off_t off
);
73 // Write the data to the output file.
75 write(Output_file
* file
)
76 { this->do_write(file
); }
79 // Functions that child classes may or in some cases must implement.
81 // Write the data to the output file.
83 do_write(Output_file
*) = 0;
85 // Return the required alignment.
87 do_addralign() const = 0;
89 // Return whether this is an Output_section.
94 // Return whether this is an Output_section of the specified type.
95 // This only needs to be implement by Output_section.
97 do_is_section_type(elfcpp::Elf_Word
) const
100 // Return whether this is an Output_section with the specific flag
101 // set. This only needs to be implemented by Output_section.
103 do_is_section_flag_set(elfcpp::Elf_Xword
) const
106 // Set the address and file offset of the data. This only needs to
107 // be implemented if the child needs to know.
109 do_set_address(uint64_t, off_t
)
112 // Functions that child classes may call.
114 // Set the size of the data.
116 set_data_size(off_t data_size
)
117 { this->data_size_
= data_size
; }
119 // Return default alignment for a size--32 or 64.
121 default_alignment(int size
);
124 Output_data(const Output_data
&);
125 Output_data
& operator=(const Output_data
&);
127 // Memory address in file (not always meaningful).
129 // Size of data in file.
131 // Offset within file.
135 // A simple case of Output_data in which we have constant data to
138 class Output_data_const
: public Output_data
141 Output_data_const(const std::string
& data
, uint64_t addralign
)
142 : Output_data(data
.size()), data_(data
), addralign_(addralign
)
145 Output_data_const(const char* p
, off_t len
, uint64_t addralign
)
146 : Output_data(len
), data_(p
, len
), addralign_(addralign
)
149 // Write the data to the file.
151 do_write(Output_file
* output
);
153 // Return the required alignment.
156 { return this->addralign_
; }
163 // Output the section headers.
165 class Output_section_headers
: public Output_data
168 Output_section_headers(int size
,
169 const Layout::Segment_list
&,
170 const Layout::Section_list
&);
172 // Write the data to the file.
174 do_write(Output_file
*);
176 // Return the required alignment.
179 { return Output_data::default_alignment(this->size_
); }
183 const Layout::Segment_list
& segment_list_
;
184 const Layout::Section_list
& section_list_
;
187 // Output the segment headers.
189 class Output_segment_headers
: public Output_data
192 Output_segment_headers(int size
, const Layout::Segment_list
& segment_list
)
193 : size_(size
), segment_list_(segment_list
)
196 // Write the data to the file.
198 do_write(Output_file
*);
200 // Return the required alignment.
203 { return Output_data::default_alignment(this->size_
); }
207 const Layout::Segment_list
& segment_list_
;
210 // Output the ELF file header.
212 class Output_file_header
: public Output_data
215 Output_file_header(int size
,
216 const General_options
&,
219 const Output_segment_headers
*);
221 // Add information about the section headers. We lay out the ELF
222 // file header before we create the section headers.
223 void set_section_info(const Output_section_headers
*,
224 const Output_section
* shstrtab
);
226 // Write the data to the file.
228 do_write(Output_file
*);
230 // Return the required alignment.
233 { return Output_data::default_alignment(this->size_
); }
235 // Set the address and offset--we only implement this for error
238 do_set_address(uint64_t, off_t off
) const
239 { assert(off
== 0); }
243 const General_options
& options_
;
244 const Target
* target_
;
245 const Symbol_table
* symtab_
;
246 const Output_segment_headers
* program_header_
;
247 const Output_section_headers
* section_header_
;
248 const Output_section
* shstrtab_
;
251 // An output section. We don't expect to have too many output
252 // sections, so we don't bother to do a template on the size.
254 class Output_section
: public Output_data
257 // Create an output section, giving the name, type, and flags.
258 Output_section(const char* name
, elfcpp::Elf_Word
, elfcpp::Elf_Xword
);
259 virtual ~Output_section();
261 // Add a new input section named NAME with header SHDR from object
262 // OBJECT. Return the offset within the output section.
263 template<int size
, bool big_endian
>
265 add_input_section(Object
* object
, const char *name
,
266 const elfcpp::Shdr
<size
, big_endian
>& shdr
);
268 // Return the section name.
271 { return this->name_
; }
273 // Return the section type.
276 { return this->type_
; }
278 // Return the section flags.
281 { return this->flags_
; }
283 // Return the address alignment.
286 { return this->addralign_
; }
288 // Write the data to the file. For a typical Output_section, this
289 // does nothing. We write out the data by looping over all the
292 do_write(Output_file
*)
295 // Return the address alignment--function required by parent class.
298 { return this->addralign_
; }
300 // Return whether this is an Output_section.
302 do_is_section() const
305 // Return whether this is a section of the specified type.
307 do_is_section_type(elfcpp::Elf_Word type
) const
308 { return this->type_
== type
; }
310 // Return whether the specified section flag is set.
312 do_is_section_flag_set(elfcpp::Elf_Xword flag
) const
313 { return (this->flags_
& flag
) != 0; }
316 // Most of these fields are only valid after layout.
318 // The name of the section. This will point into a Stringpool.
320 // The section address is in the parent class.
321 // The section alignment.
323 // The section entry size.
325 // The file offset is in the parent class.
326 // The section link field.
328 // The section info field.
331 elfcpp::Elf_Word type_
;
332 // The section flags.
333 elfcpp::Elf_Xword flags_
;
336 // A special Output_section which represents the symbol table
339 class Output_section_symtab
: public Output_section
342 Output_section_symtab(const char* name
, off_t size
);
345 // A special Output_section which holds a string table.
347 class Output_section_strtab
: public Output_section
350 Output_section_strtab(const char* name
, Stringpool
* contents
);
352 // Write out the data.
354 do_write(Output_file
*);
357 Stringpool
* contents_
;
360 // An output segment. PT_LOAD segments are built from collections of
361 // output sections. Other segments typically point within PT_LOAD
362 // segments, and are built directly as needed.
367 // Create an output segment, specifying the type and flags.
368 Output_segment(elfcpp::Elf_Word
, elfcpp::Elf_Word
);
370 // Return the virtual address.
373 { return this->vaddr_
; }
375 // Return the physical address.
378 { return this->paddr_
; }
380 // Return the segment type.
383 { return this->type_
; }
385 // Return the segment flags.
388 { return this->flags_
; }
390 // Return the maximum alignment of the Output_data.
392 max_data_align() const;
394 // Add an Output_section to this segment.
396 add_output_section(Output_section
*, elfcpp::Elf_Word seg_flags
);
398 // Add an Output_data (which is not an Output_section) to the start
401 add_initial_output_data(Output_data
*);
403 // Set the address of the segment to ADDR and the offset to *POFF
404 // (aligned if necessary), and set the addresses and offsets of all
405 // contained output sections accordingly. Return the address of the
406 // immediately following segment. Update *POFF. This should only
407 // be called for a PT_LOAD segment.
409 set_section_addresses(uint64_t addr
, off_t
* poff
);
411 // Set the offset of this segment based on the section. This should
412 // only be called for a non-PT_LOAD segment.
416 // Return the number of output sections.
418 output_section_count() const;
421 Output_segment(const Output_segment
&);
422 Output_segment
& operator=(const Output_segment
&);
424 typedef std::list
<Output_data
*> Output_data_list
;
426 // Set the section addresses in an Output_data_list.
428 set_section_list_addresses(Output_data_list
*, uint64_t addr
, off_t
* poff
);
430 // Return the number of Output_sections in an Output_data_list.
432 output_section_count_list(const Output_data_list
*) const;
434 // The list of output data with contents attached to this segment.
435 Output_data_list output_data_
;
436 // The list of output data without contents attached to this segment.
437 Output_data_list output_bss_
;
438 // The segment virtual address.
440 // The segment physical address.
442 // The size of the segment in memory.
444 // The segment alignment.
446 // The offset of the segment data within the file.
448 // The size of the segment data in the file.
451 elfcpp::Elf_Word type_
;
452 // The segment flags.
453 elfcpp::Elf_Word flags_
;
456 // This class represents the output file. The output file is a
457 // collection of output segments and a collection of output sections
458 // which are not associated with segments.
466 // Write data to the output file.
468 write(off_t off
, const void* data
, off_t len
);
471 } // End namespace gold.
473 #endif // !defined(GOLD_OUTPUT_H)