Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // output.h -- manage the output file for gold -*- C++ -*- |
2 | ||
3 | #ifndef GOLD_OUTPUT_H | |
4 | #define GOLD_OUTPUT_H | |
5 | ||
75f65a3e | 6 | #include <cassert> |
a2fb1b05 | 7 | #include <list> |
ead1e424 | 8 | #include <vector> |
a2fb1b05 ILT |
9 | |
10 | #include "elfcpp.h" | |
54dc6425 | 11 | #include "layout.h" |
a2fb1b05 ILT |
12 | |
13 | namespace gold | |
14 | { | |
15 | ||
61ba1cf9 | 16 | class General_options; |
a2fb1b05 ILT |
17 | class Object; |
18 | class Output_file; | |
19 | ||
54dc6425 ILT |
20 | template<int size, bool big_endian> |
21 | class Sized_target; | |
22 | ||
23 | // An abtract class for data which has to go into the output file. | |
a2fb1b05 ILT |
24 | |
25 | class Output_data | |
26 | { | |
27 | public: | |
75f65a3e | 28 | explicit Output_data(off_t data_size = 0) |
61ba1cf9 | 29 | : address_(0), data_size_(data_size), offset_(-1) |
a2fb1b05 ILT |
30 | { } |
31 | ||
32 | virtual | |
33 | ~Output_data(); | |
34 | ||
ead1e424 ILT |
35 | // Return the address. This is only valid after Layout::finalize is |
36 | // finished. | |
75f65a3e ILT |
37 | uint64_t |
38 | address() const | |
39 | { return this->address_; } | |
40 | ||
ead1e424 ILT |
41 | // Return the size of the data. This must be valid after |
42 | // Layout::finalize calls set_address, but need not be valid before | |
43 | // then. | |
a2fb1b05 | 44 | off_t |
75f65a3e ILT |
45 | data_size() const |
46 | { return this->data_size_; } | |
47 | ||
ead1e424 ILT |
48 | // Return the file offset. This is only valid after |
49 | // Layout::finalize is finished. | |
75f65a3e ILT |
50 | off_t |
51 | offset() const | |
52 | { return this->offset_; } | |
53 | ||
54 | // Return the required alignment. | |
55 | uint64_t | |
56 | addralign() const | |
57 | { return this->do_addralign(); } | |
58 | ||
59 | // Return whether this is an Output_section. | |
60 | bool | |
61 | is_section() const | |
62 | { return this->do_is_section(); } | |
63 | ||
64 | // Return whether this is an Output_section of the specified type. | |
65 | bool | |
66 | is_section_type(elfcpp::Elf_Word stt) const | |
67 | { return this->do_is_section_type(stt); } | |
68 | ||
69 | // Return whether this is an Output_section with the specified flag | |
70 | // set. | |
71 | bool | |
72 | is_section_flag_set(elfcpp::Elf_Xword shf) const | |
73 | { return this->do_is_section_flag_set(shf); } | |
74 | ||
ead1e424 ILT |
75 | // Return the output section index, if there is an output section. |
76 | unsigned int | |
77 | out_shndx() const | |
78 | { return this->do_out_shndx(); } | |
79 | ||
80 | // Set the output section index, if this is an output section. | |
81 | void | |
82 | set_out_shndx(unsigned int shndx) | |
83 | { this->do_set_out_shndx(shndx); } | |
84 | ||
85 | // Set the address and file offset of this data. This is called | |
86 | // during Layout::finalize. | |
75f65a3e ILT |
87 | void |
88 | set_address(uint64_t addr, off_t off); | |
89 | ||
ead1e424 ILT |
90 | // Write the data to the output file. This is called after |
91 | // Layout::finalize is complete. | |
75f65a3e ILT |
92 | void |
93 | write(Output_file* file) | |
94 | { this->do_write(file); } | |
a2fb1b05 | 95 | |
75f65a3e ILT |
96 | protected: |
97 | // Functions that child classes may or in some cases must implement. | |
98 | ||
99 | // Write the data to the output file. | |
a2fb1b05 | 100 | virtual void |
75f65a3e ILT |
101 | do_write(Output_file*) = 0; |
102 | ||
103 | // Return the required alignment. | |
104 | virtual uint64_t | |
105 | do_addralign() const = 0; | |
106 | ||
107 | // Return whether this is an Output_section. | |
108 | virtual bool | |
109 | do_is_section() const | |
110 | { return false; } | |
a2fb1b05 | 111 | |
54dc6425 | 112 | // Return whether this is an Output_section of the specified type. |
75f65a3e | 113 | // This only needs to be implement by Output_section. |
54dc6425 | 114 | virtual bool |
75f65a3e | 115 | do_is_section_type(elfcpp::Elf_Word) const |
54dc6425 ILT |
116 | { return false; } |
117 | ||
75f65a3e ILT |
118 | // Return whether this is an Output_section with the specific flag |
119 | // set. This only needs to be implemented by Output_section. | |
54dc6425 | 120 | virtual bool |
75f65a3e | 121 | do_is_section_flag_set(elfcpp::Elf_Xword) const |
54dc6425 ILT |
122 | { return false; } |
123 | ||
ead1e424 ILT |
124 | // Return the output section index, if there is an output section. |
125 | virtual unsigned int | |
126 | do_out_shndx() const | |
127 | { abort(); } | |
128 | ||
129 | // Set the output section index, if this is an output section. | |
130 | virtual void | |
131 | do_set_out_shndx(unsigned int) | |
132 | { abort(); } | |
133 | ||
75f65a3e ILT |
134 | // Set the address and file offset of the data. This only needs to |
135 | // be implemented if the child needs to know. | |
136 | virtual void | |
137 | do_set_address(uint64_t, off_t) | |
138 | { } | |
139 | ||
140 | // Functions that child classes may call. | |
141 | ||
a2fb1b05 ILT |
142 | // Set the size of the data. |
143 | void | |
75f65a3e ILT |
144 | set_data_size(off_t data_size) |
145 | { this->data_size_ = data_size; } | |
146 | ||
147 | // Return default alignment for a size--32 or 64. | |
148 | static uint64_t | |
149 | default_alignment(int size); | |
a2fb1b05 ILT |
150 | |
151 | private: | |
152 | Output_data(const Output_data&); | |
153 | Output_data& operator=(const Output_data&); | |
154 | ||
75f65a3e ILT |
155 | // Memory address in file (not always meaningful). |
156 | uint64_t address_; | |
a2fb1b05 | 157 | // Size of data in file. |
75f65a3e ILT |
158 | off_t data_size_; |
159 | // Offset within file. | |
160 | off_t offset_; | |
a2fb1b05 ILT |
161 | }; |
162 | ||
54dc6425 ILT |
163 | // Output the section headers. |
164 | ||
165 | class Output_section_headers : public Output_data | |
166 | { | |
167 | public: | |
75f65a3e | 168 | Output_section_headers(int size, |
61ba1cf9 | 169 | bool big_endian, |
75f65a3e | 170 | const Layout::Segment_list&, |
61ba1cf9 ILT |
171 | const Layout::Section_list&, |
172 | const Stringpool*); | |
54dc6425 ILT |
173 | |
174 | // Write the data to the file. | |
175 | void | |
75f65a3e ILT |
176 | do_write(Output_file*); |
177 | ||
178 | // Return the required alignment. | |
179 | uint64_t | |
180 | do_addralign() const | |
181 | { return Output_data::default_alignment(this->size_); } | |
54dc6425 ILT |
182 | |
183 | private: | |
61ba1cf9 ILT |
184 | // Write the data to the file with the right size and endianness. |
185 | template<int size, bool big_endian> | |
186 | void | |
187 | do_sized_write(Output_file*); | |
188 | ||
75f65a3e | 189 | int size_; |
61ba1cf9 | 190 | bool big_endian_; |
54dc6425 ILT |
191 | const Layout::Segment_list& segment_list_; |
192 | const Layout::Section_list& section_list_; | |
61ba1cf9 | 193 | const Stringpool* secnamepool_; |
54dc6425 ILT |
194 | }; |
195 | ||
196 | // Output the segment headers. | |
197 | ||
198 | class Output_segment_headers : public Output_data | |
199 | { | |
200 | public: | |
61ba1cf9 ILT |
201 | Output_segment_headers(int size, bool big_endian, |
202 | const Layout::Segment_list& segment_list); | |
54dc6425 ILT |
203 | |
204 | // Write the data to the file. | |
205 | void | |
75f65a3e ILT |
206 | do_write(Output_file*); |
207 | ||
208 | // Return the required alignment. | |
209 | uint64_t | |
210 | do_addralign() const | |
211 | { return Output_data::default_alignment(this->size_); } | |
54dc6425 ILT |
212 | |
213 | private: | |
61ba1cf9 ILT |
214 | // Write the data to the file with the right size and endianness. |
215 | template<int size, bool big_endian> | |
216 | void | |
217 | do_sized_write(Output_file*); | |
218 | ||
75f65a3e | 219 | int size_; |
61ba1cf9 | 220 | bool big_endian_; |
54dc6425 ILT |
221 | const Layout::Segment_list& segment_list_; |
222 | }; | |
223 | ||
224 | // Output the ELF file header. | |
225 | ||
226 | class Output_file_header : public Output_data | |
227 | { | |
228 | public: | |
75f65a3e | 229 | Output_file_header(int size, |
61ba1cf9 | 230 | bool big_endian, |
75f65a3e | 231 | const General_options&, |
54dc6425 ILT |
232 | const Target*, |
233 | const Symbol_table*, | |
75f65a3e ILT |
234 | const Output_segment_headers*); |
235 | ||
236 | // Add information about the section headers. We lay out the ELF | |
237 | // file header before we create the section headers. | |
238 | void set_section_info(const Output_section_headers*, | |
239 | const Output_section* shstrtab); | |
54dc6425 ILT |
240 | |
241 | // Write the data to the file. | |
242 | void | |
75f65a3e ILT |
243 | do_write(Output_file*); |
244 | ||
245 | // Return the required alignment. | |
246 | uint64_t | |
247 | do_addralign() const | |
248 | { return Output_data::default_alignment(this->size_); } | |
249 | ||
250 | // Set the address and offset--we only implement this for error | |
251 | // checking. | |
252 | void | |
253 | do_set_address(uint64_t, off_t off) const | |
254 | { assert(off == 0); } | |
54dc6425 ILT |
255 | |
256 | private: | |
61ba1cf9 ILT |
257 | // Write the data to the file with the right size and endianness. |
258 | template<int size, bool big_endian> | |
259 | void | |
260 | do_sized_write(Output_file*); | |
261 | ||
75f65a3e | 262 | int size_; |
61ba1cf9 | 263 | bool big_endian_; |
54dc6425 ILT |
264 | const General_options& options_; |
265 | const Target* target_; | |
266 | const Symbol_table* symtab_; | |
61ba1cf9 | 267 | const Output_segment_headers* segment_header_; |
54dc6425 ILT |
268 | const Output_section_headers* section_header_; |
269 | const Output_section* shstrtab_; | |
270 | }; | |
271 | ||
ead1e424 ILT |
272 | // Output sections are mainly comprised of input sections. However, |
273 | // there are cases where we have data to write out which is not in an | |
274 | // input section. Output_section_data is used in such cases. This is | |
275 | // an abstract base class. | |
276 | ||
277 | class Output_section_data : public Output_data | |
278 | { | |
279 | public: | |
280 | Output_section_data(off_t data_size, uint64_t addralign) | |
281 | : Output_data(data_size), output_section_(NULL), addralign_(addralign) | |
282 | { } | |
283 | ||
284 | Output_section_data(uint64_t addralign) | |
285 | : Output_data(0), output_section_(NULL), addralign_(addralign) | |
286 | { } | |
287 | ||
288 | // Record the output section. | |
289 | void | |
290 | set_output_section(Output_section* os) | |
291 | { | |
292 | assert(this->output_section_ == NULL); | |
293 | this->output_section_ = os; | |
294 | } | |
295 | ||
296 | protected: | |
297 | // The child class must implement do_write. | |
298 | ||
299 | // Return the required alignment. | |
300 | uint64_t | |
301 | do_addralign() const | |
302 | { return this->addralign_; } | |
303 | ||
304 | // Return the section index of the output section. | |
305 | unsigned int | |
306 | do_out_shndx() const; | |
307 | ||
308 | private: | |
309 | // The output section for this section. | |
310 | const Output_section* output_section_; | |
311 | // The required alignment. | |
312 | uint64_t addralign_; | |
313 | }; | |
314 | ||
dbe717ef ILT |
315 | // A simple case of Output_data in which we have constant data to |
316 | // output. | |
ead1e424 | 317 | |
dbe717ef | 318 | class Output_data_const : public Output_section_data |
ead1e424 ILT |
319 | { |
320 | public: | |
dbe717ef ILT |
321 | Output_data_const(const std::string& data, uint64_t addralign) |
322 | : Output_section_data(data.size(), addralign), data_(data) | |
323 | { } | |
324 | ||
325 | Output_data_const(const char* p, off_t len, uint64_t addralign) | |
326 | : Output_section_data(len, addralign), data_(p, len) | |
327 | { } | |
328 | ||
329 | Output_data_const(const unsigned char* p, off_t len, uint64_t addralign) | |
330 | : Output_section_data(len, addralign), | |
331 | data_(reinterpret_cast<const char*>(p), len) | |
332 | { } | |
333 | ||
334 | // Write the data to the file. | |
335 | void | |
336 | do_write(Output_file* output); | |
337 | ||
338 | private: | |
339 | std::string data_; | |
340 | }; | |
341 | ||
342 | // Output_data_common is used to handle the common symbols. This is | |
343 | // quite simple. | |
344 | ||
345 | class Output_data_common : public Output_section_data | |
346 | { | |
347 | public: | |
348 | Output_data_common(uint64_t addralign) | |
ead1e424 ILT |
349 | : Output_section_data(addralign) |
350 | { } | |
351 | ||
352 | // Set the size. | |
353 | void | |
354 | set_common_size(off_t common_size) | |
355 | { this->set_data_size(common_size); } | |
356 | ||
357 | // Write out the data--there is nothing to do, as common symbols are | |
358 | // always zero and are stored in the BSS. | |
359 | void | |
360 | do_write(Output_file*) | |
361 | { } | |
362 | }; | |
363 | ||
dbe717ef ILT |
364 | // Output_data_got is used to manage a GOT. Each entry in the GOT is |
365 | // for one symbol--either a global symbol or a local symbol in an | |
ead1e424 | 366 | // object. The target specific code adds entries to the GOT as |
dbe717ef | 367 | // needed. |
ead1e424 ILT |
368 | |
369 | template<int size, bool big_endian> | |
dbe717ef | 370 | class Output_data_got : public Output_section_data |
ead1e424 ILT |
371 | { |
372 | public: | |
373 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; | |
374 | ||
dbe717ef | 375 | Output_data_got() |
ead1e424 ILT |
376 | : Output_section_data(Output_data::default_alignment(size)), |
377 | entries_() | |
378 | { } | |
379 | ||
dbe717ef ILT |
380 | // Add an entry for a global symbol to the GOT. Return true if this |
381 | // is a new GOT entry, false if the symbol was already in the GOT. | |
382 | bool | |
383 | add_global(Symbol* gsym); | |
ead1e424 ILT |
384 | |
385 | // Add an entry for a local symbol to the GOT. This returns the | |
386 | // offset of the new entry from the start of the GOT. | |
387 | unsigned int | |
388 | add_local(Object* object, unsigned int sym_index) | |
389 | { | |
390 | this->entries_.push_back(Got_entry(object, sym_index)); | |
391 | this->set_got_size(); | |
392 | return this->last_got_offset(); | |
393 | } | |
394 | ||
395 | // Add a constant to the GOT. This returns the offset of the new | |
396 | // entry from the start of the GOT. | |
397 | unsigned int | |
398 | add_constant(Valtype constant) | |
399 | { | |
400 | this->entries_.push_back(Got_entry(constant)); | |
401 | this->set_got_size(); | |
402 | return this->last_got_offset(); | |
403 | } | |
404 | ||
405 | // Write out the GOT table. | |
406 | void | |
407 | do_write(Output_file*); | |
408 | ||
409 | private: | |
410 | // This POD class holds a single GOT entry. | |
411 | class Got_entry | |
412 | { | |
413 | public: | |
414 | // Create a zero entry. | |
415 | Got_entry() | |
416 | : local_sym_index_(CONSTANT_CODE) | |
417 | { this->u_.constant = 0; } | |
418 | ||
419 | // Create a global symbol entry. | |
420 | Got_entry(Symbol* gsym) | |
421 | : local_sym_index_(GSYM_CODE) | |
422 | { this->u_.gsym = gsym; } | |
423 | ||
424 | // Create a local symbol entry. | |
425 | Got_entry(Object* object, unsigned int local_sym_index) | |
426 | : local_sym_index_(local_sym_index) | |
427 | { | |
428 | assert(local_sym_index != GSYM_CODE | |
429 | && local_sym_index != CONSTANT_CODE); | |
430 | this->u_.object = object; | |
431 | } | |
432 | ||
433 | // Create a constant entry. The constant is a host value--it will | |
434 | // be swapped, if necessary, when it is written out. | |
435 | Got_entry(Valtype constant) | |
436 | : local_sym_index_(CONSTANT_CODE) | |
437 | { this->u_.constant = constant; } | |
438 | ||
439 | // Write the GOT entry to an output view. | |
440 | void | |
441 | write(unsigned char* pov) const; | |
442 | ||
443 | private: | |
444 | enum | |
445 | { | |
446 | GSYM_CODE = -1U, | |
447 | CONSTANT_CODE = -2U | |
448 | }; | |
449 | ||
450 | union | |
451 | { | |
452 | // For a local symbol, the object. | |
453 | Object* object; | |
454 | // For a global symbol, the symbol. | |
455 | Symbol* gsym; | |
456 | // For a constant, the constant. | |
457 | Valtype constant; | |
458 | } u_; | |
459 | // For a local symbol, the local symbol index. This is -1U for a | |
460 | // global symbol, or -2U for a constant. | |
461 | unsigned int local_sym_index_; | |
462 | }; | |
463 | ||
464 | typedef std::vector<Got_entry> Got_entries; | |
465 | ||
466 | // Return the offset into the GOT of GOT entry I. | |
467 | unsigned int | |
468 | got_offset(unsigned int i) const | |
469 | { return i * (size / 8); } | |
470 | ||
471 | // Return the offset into the GOT of the last entry added. | |
472 | unsigned int | |
473 | last_got_offset() const | |
474 | { return this->got_offset(this->entries_.size() - 1); } | |
475 | ||
476 | // Set the size of the section. | |
477 | void | |
478 | set_got_size() | |
479 | { this->set_data_size(this->got_offset(this->entries_.size())); } | |
480 | ||
481 | // The list of GOT entries. | |
482 | Got_entries entries_; | |
483 | }; | |
484 | ||
a2fb1b05 ILT |
485 | // An output section. We don't expect to have too many output |
486 | // sections, so we don't bother to do a template on the size. | |
487 | ||
54dc6425 | 488 | class Output_section : public Output_data |
a2fb1b05 ILT |
489 | { |
490 | public: | |
491 | // Create an output section, giving the name, type, and flags. | |
61ba1cf9 | 492 | Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword, |
ead1e424 | 493 | bool may_add_data); |
54dc6425 | 494 | virtual ~Output_section(); |
a2fb1b05 | 495 | |
ead1e424 ILT |
496 | // Add a new input section SHNDX, named NAME, with header SHDR, from |
497 | // object OBJECT. Return the offset within the output section. | |
a2fb1b05 ILT |
498 | template<int size, bool big_endian> |
499 | off_t | |
f6ce93d6 | 500 | add_input_section(Relobj* object, unsigned int shndx, const char *name, |
a2fb1b05 ILT |
501 | const elfcpp::Shdr<size, big_endian>& shdr); |
502 | ||
ead1e424 ILT |
503 | // Add generated data ODATA to this output section. |
504 | virtual void | |
505 | add_output_section_data(Output_section_data* posd); | |
506 | ||
a2fb1b05 ILT |
507 | // Return the section name. |
508 | const char* | |
509 | name() const | |
510 | { return this->name_; } | |
511 | ||
512 | // Return the section type. | |
513 | elfcpp::Elf_Word | |
514 | type() const | |
515 | { return this->type_; } | |
516 | ||
517 | // Return the section flags. | |
518 | elfcpp::Elf_Xword | |
519 | flags() const | |
520 | { return this->flags_; } | |
521 | ||
ead1e424 | 522 | // Return the section index in the output file. |
61ba1cf9 | 523 | unsigned int |
ead1e424 ILT |
524 | do_out_shndx() const |
525 | { return this->out_shndx_; } | |
526 | ||
527 | // Set the output section index. | |
528 | void | |
529 | do_set_out_shndx(unsigned int shndx) | |
530 | { this->out_shndx_ = shndx; } | |
61ba1cf9 ILT |
531 | |
532 | // Set the entsize field. | |
533 | void | |
534 | set_entsize(uint64_t v) | |
535 | { this->entsize_ = v; } | |
536 | ||
537 | // Set the link field. | |
538 | void | |
539 | set_link(unsigned int v) | |
540 | { this->link_ = v; } | |
541 | ||
542 | // Set the info field. | |
543 | void | |
544 | set_info(unsigned int v) | |
545 | { this->info_ = v; } | |
546 | ||
547 | // Set the addralign field. | |
548 | void | |
549 | set_addralign(uint64_t v) | |
550 | { this->addralign_ = v; } | |
551 | ||
ead1e424 ILT |
552 | // Set the address of the Output_section. For a typical |
553 | // Output_section, there is nothing to do, but if there are any | |
554 | // Output_section_data objects we need to set the final addresses | |
555 | // here. | |
556 | void | |
557 | do_set_address(uint64_t, off_t); | |
558 | ||
54dc6425 | 559 | // Write the data to the file. For a typical Output_section, this |
ead1e424 ILT |
560 | // does nothing: the data is written out by calling Object::Relocate |
561 | // on each input object. But if there are any Output_section_data | |
562 | // objects we do need to write them out here. | |
54dc6425 | 563 | virtual void |
ead1e424 | 564 | do_write(Output_file*); |
54dc6425 | 565 | |
75f65a3e ILT |
566 | // Return the address alignment--function required by parent class. |
567 | uint64_t | |
568 | do_addralign() const | |
569 | { return this->addralign_; } | |
570 | ||
571 | // Return whether this is an Output_section. | |
572 | bool | |
573 | do_is_section() const | |
574 | { return true; } | |
575 | ||
54dc6425 ILT |
576 | // Return whether this is a section of the specified type. |
577 | bool | |
75f65a3e | 578 | do_is_section_type(elfcpp::Elf_Word type) const |
54dc6425 ILT |
579 | { return this->type_ == type; } |
580 | ||
581 | // Return whether the specified section flag is set. | |
582 | bool | |
75f65a3e | 583 | do_is_section_flag_set(elfcpp::Elf_Xword flag) const |
54dc6425 ILT |
584 | { return (this->flags_ & flag) != 0; } |
585 | ||
61ba1cf9 ILT |
586 | // Write the section header into *OPHDR. |
587 | template<int size, bool big_endian> | |
588 | void | |
589 | write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const; | |
590 | ||
a2fb1b05 | 591 | private: |
ead1e424 ILT |
592 | // In some cases we need to keep a list of the input sections |
593 | // associated with this output section. We only need the list if we | |
594 | // might have to change the offsets of the input section within the | |
595 | // output section after we add the input section. The ordinary | |
596 | // input sections will be written out when we process the object | |
597 | // file, and as such we don't need to track them here. We do need | |
598 | // to track Output_section_data objects here. We store instances of | |
599 | // this structure in a std::vector, so it must be a POD. There can | |
600 | // be many instances of this structure, so we use a union to save | |
601 | // some space. | |
602 | class Input_section | |
603 | { | |
604 | public: | |
605 | Input_section() | |
606 | : shndx_(0), p2align_(0), data_size_(0) | |
607 | { this->u_.object = NULL; } | |
608 | ||
f6ce93d6 | 609 | Input_section(Relobj* object, unsigned int shndx, off_t data_size, |
ead1e424 ILT |
610 | uint64_t addralign) |
611 | : shndx_(shndx), | |
612 | p2align_(ffsll(static_cast<long long>(addralign))), | |
613 | data_size_(data_size) | |
614 | { | |
615 | assert(shndx != -1U); | |
616 | this->u_.object = object; | |
617 | } | |
618 | ||
619 | Input_section(Output_section_data* posd) | |
620 | : shndx_(-1U), | |
621 | p2align_(ffsll(static_cast<long long>(posd->addralign()))), | |
622 | data_size_(0) | |
623 | { this->u_.posd = posd; } | |
624 | ||
625 | // The required alignment. | |
626 | uint64_t | |
627 | addralign() const | |
628 | { return static_cast<uint64_t>(1) << this->p2align_; } | |
629 | ||
630 | // Return the required size. | |
631 | off_t | |
632 | data_size() const; | |
633 | ||
634 | // Set the address and file offset. This is called during | |
635 | // Layout::finalize. SECOFF is the file offset of the enclosing | |
636 | // section. | |
637 | void | |
638 | set_address(uint64_t addr, off_t off, off_t secoff); | |
639 | ||
640 | // Write out the data. This does nothing for an input section. | |
641 | void | |
642 | write(Output_file*); | |
643 | ||
644 | private: | |
645 | // Whether this is an input section. | |
646 | bool | |
647 | is_input_section() const | |
648 | { return this->shndx_ != -1U; } | |
649 | ||
650 | // For an ordinary input section, this is the section index in | |
651 | // the input file. For an Output_section_data, this is -1U. | |
652 | unsigned int shndx_; | |
653 | // The required alignment, stored as a power of 2. | |
654 | unsigned int p2align_; | |
655 | // For an ordinary input section, the section size. | |
656 | off_t data_size_; | |
657 | union | |
658 | { | |
659 | // If shndx_ != -1U, this points to the object which holds the | |
660 | // input section. | |
f6ce93d6 | 661 | Relobj* object; |
ead1e424 ILT |
662 | // If shndx_ == -1U, this is the data to write out. |
663 | Output_section_data* posd; | |
664 | } u_; | |
665 | }; | |
666 | ||
667 | typedef std::vector<Input_section> Input_section_list; | |
668 | ||
a2fb1b05 ILT |
669 | // Most of these fields are only valid after layout. |
670 | ||
671 | // The name of the section. This will point into a Stringpool. | |
672 | const char* name_; | |
75f65a3e | 673 | // The section address is in the parent class. |
a2fb1b05 ILT |
674 | // The section alignment. |
675 | uint64_t addralign_; | |
676 | // The section entry size. | |
677 | uint64_t entsize_; | |
75f65a3e | 678 | // The file offset is in the parent class. |
a2fb1b05 ILT |
679 | // The section link field. |
680 | unsigned int link_; | |
681 | // The section info field. | |
682 | unsigned int info_; | |
683 | // The section type. | |
684 | elfcpp::Elf_Word type_; | |
685 | // The section flags. | |
686 | elfcpp::Elf_Xword flags_; | |
61ba1cf9 | 687 | // The section index. |
ead1e424 ILT |
688 | unsigned int out_shndx_; |
689 | // The input sections. This will be empty in cases where we don't | |
690 | // need to keep track of them. | |
691 | Input_section_list input_sections_; | |
692 | // The offset of the first entry in input_sections_. | |
693 | off_t first_input_offset_; | |
694 | // Whether we permit adding data. | |
695 | bool may_add_data_; | |
a2fb1b05 ILT |
696 | }; |
697 | ||
54dc6425 | 698 | // A special Output_section which represents the symbol table |
ead1e424 ILT |
699 | // (SHT_SYMTAB). The actual data is written out by |
700 | // Symbol_table::write_globals. | |
54dc6425 ILT |
701 | |
702 | class Output_section_symtab : public Output_section | |
703 | { | |
704 | public: | |
ead1e424 ILT |
705 | Output_section_symtab(const char* name, off_t size); |
706 | ||
707 | // The data is written out by Symbol_table::write_globals. We don't | |
708 | // do anything here. | |
709 | void | |
710 | do_write(Output_file*) | |
711 | { } | |
712 | ||
713 | // We don't expect to see any input sections or data here. | |
714 | void | |
715 | add_output_section_data(Output_section_data*) | |
716 | { abort(); } | |
75f65a3e ILT |
717 | }; |
718 | ||
719 | // A special Output_section which holds a string table. | |
720 | ||
721 | class Output_section_strtab : public Output_section | |
722 | { | |
723 | public: | |
ead1e424 | 724 | Output_section_strtab(const char* name, Stringpool* contents); |
75f65a3e ILT |
725 | |
726 | // Write out the data. | |
727 | void | |
728 | do_write(Output_file*); | |
729 | ||
ead1e424 ILT |
730 | // We don't expect to see any input sections or data here. |
731 | void | |
732 | add_output_section_data(Output_section_data*) | |
733 | { abort(); } | |
734 | ||
75f65a3e ILT |
735 | private: |
736 | Stringpool* contents_; | |
54dc6425 ILT |
737 | }; |
738 | ||
a2fb1b05 ILT |
739 | // An output segment. PT_LOAD segments are built from collections of |
740 | // output sections. Other segments typically point within PT_LOAD | |
741 | // segments, and are built directly as needed. | |
742 | ||
743 | class Output_segment | |
744 | { | |
745 | public: | |
746 | // Create an output segment, specifying the type and flags. | |
747 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); | |
748 | ||
749 | // Return the virtual address. | |
750 | uint64_t | |
751 | vaddr() const | |
752 | { return this->vaddr_; } | |
753 | ||
754 | // Return the physical address. | |
755 | uint64_t | |
756 | paddr() const | |
757 | { return this->paddr_; } | |
758 | ||
759 | // Return the segment type. | |
760 | elfcpp::Elf_Word | |
761 | type() const | |
762 | { return this->type_; } | |
763 | ||
764 | // Return the segment flags. | |
765 | elfcpp::Elf_Word | |
766 | flags() const | |
767 | { return this->flags_; } | |
768 | ||
92e059d8 ILT |
769 | // Return the memory size. |
770 | uint64_t | |
771 | memsz() const | |
772 | { return this->memsz_; } | |
773 | ||
ead1e424 ILT |
774 | // Return the file size. |
775 | off_t | |
776 | filesz() const | |
777 | { return this->filesz_; } | |
778 | ||
75f65a3e ILT |
779 | // Return the maximum alignment of the Output_data. |
780 | uint64_t | |
ead1e424 | 781 | addralign(); |
75f65a3e | 782 | |
a2fb1b05 ILT |
783 | // Add an Output_section to this segment. |
784 | void | |
dbe717ef ILT |
785 | add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) |
786 | { this->add_output_section(os, seg_flags, false); } | |
787 | ||
788 | // Add an Output_section to the start of this segment. | |
789 | void | |
790 | add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) | |
791 | { this->add_output_section(os, seg_flags, true); } | |
75f65a3e ILT |
792 | |
793 | // Add an Output_data (which is not an Output_section) to the start | |
794 | // of this segment. | |
795 | void | |
796 | add_initial_output_data(Output_data*); | |
797 | ||
798 | // Set the address of the segment to ADDR and the offset to *POFF | |
799 | // (aligned if necessary), and set the addresses and offsets of all | |
ead1e424 ILT |
800 | // contained output sections accordingly. Set the section indexes |
801 | // of all contained output sections starting with *PSHNDX. Return | |
802 | // the address of the immediately following segment. Update *POFF | |
803 | // and *PSHNDX. This should only be called for a PT_LOAD segment. | |
75f65a3e | 804 | uint64_t |
ead1e424 | 805 | set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx); |
75f65a3e ILT |
806 | |
807 | // Set the offset of this segment based on the section. This should | |
808 | // only be called for a non-PT_LOAD segment. | |
809 | void | |
810 | set_offset(); | |
811 | ||
812 | // Return the number of output sections. | |
813 | unsigned int | |
814 | output_section_count() const; | |
a2fb1b05 | 815 | |
61ba1cf9 ILT |
816 | // Write the segment header into *OPHDR. |
817 | template<int size, bool big_endian> | |
818 | void | |
ead1e424 | 819 | write_header(elfcpp::Phdr_write<size, big_endian>*); |
61ba1cf9 ILT |
820 | |
821 | // Write the section headers of associated sections into V. | |
822 | template<int size, bool big_endian> | |
823 | unsigned char* | |
5482377d | 824 | write_section_headers(const Stringpool*, |
ead1e424 ILT |
825 | unsigned char* v, |
826 | unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const; | |
61ba1cf9 | 827 | |
a2fb1b05 ILT |
828 | private: |
829 | Output_segment(const Output_segment&); | |
830 | Output_segment& operator=(const Output_segment&); | |
831 | ||
54dc6425 | 832 | typedef std::list<Output_data*> Output_data_list; |
a2fb1b05 | 833 | |
dbe717ef ILT |
834 | // Add an Output_section to this segment, specifying front or back. |
835 | void | |
836 | add_output_section(Output_section*, elfcpp::Elf_Word seg_flags, | |
837 | bool front); | |
838 | ||
ead1e424 ILT |
839 | // Find the maximum alignment in an Output_data_list. |
840 | static uint64_t | |
841 | maximum_alignment(const Output_data_list*); | |
842 | ||
75f65a3e ILT |
843 | // Set the section addresses in an Output_data_list. |
844 | uint64_t | |
ead1e424 ILT |
845 | set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff, |
846 | unsigned int* pshndx); | |
75f65a3e ILT |
847 | |
848 | // Return the number of Output_sections in an Output_data_list. | |
849 | unsigned int | |
850 | output_section_count_list(const Output_data_list*) const; | |
851 | ||
61ba1cf9 ILT |
852 | // Write the section headers in the list into V. |
853 | template<int size, bool big_endian> | |
854 | unsigned char* | |
855 | write_section_headers_list(const Stringpool*, const Output_data_list*, | |
ead1e424 ILT |
856 | unsigned char* v, |
857 | unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const; | |
61ba1cf9 | 858 | |
75f65a3e | 859 | // The list of output data with contents attached to this segment. |
54dc6425 | 860 | Output_data_list output_data_; |
75f65a3e ILT |
861 | // The list of output data without contents attached to this segment. |
862 | Output_data_list output_bss_; | |
a2fb1b05 ILT |
863 | // The segment virtual address. |
864 | uint64_t vaddr_; | |
865 | // The segment physical address. | |
866 | uint64_t paddr_; | |
867 | // The size of the segment in memory. | |
868 | uint64_t memsz_; | |
869 | // The segment alignment. | |
870 | uint64_t align_; | |
871 | // The offset of the segment data within the file. | |
872 | off_t offset_; | |
873 | // The size of the segment data in the file. | |
874 | off_t filesz_; | |
875 | // The segment type; | |
876 | elfcpp::Elf_Word type_; | |
877 | // The segment flags. | |
878 | elfcpp::Elf_Word flags_; | |
ead1e424 ILT |
879 | // Whether we have set align_. |
880 | bool is_align_known_; | |
a2fb1b05 ILT |
881 | }; |
882 | ||
61ba1cf9 | 883 | // This class represents the output file. |
a2fb1b05 ILT |
884 | |
885 | class Output_file | |
886 | { | |
887 | public: | |
61ba1cf9 ILT |
888 | Output_file(const General_options& options); |
889 | ||
890 | // Open the output file. FILE_SIZE is the final size of the file. | |
891 | void | |
892 | open(off_t file_size); | |
893 | ||
894 | // Close the output file and make sure there are no error. | |
895 | void | |
896 | close(); | |
897 | ||
898 | // We currently always use mmap which makes the view handling quite | |
899 | // simple. In the future we may support other approaches. | |
a2fb1b05 ILT |
900 | |
901 | // Write data to the output file. | |
902 | void | |
61ba1cf9 ILT |
903 | write(off_t offset, const void* data, off_t len) |
904 | { memcpy(this->base_ + offset, data, len); } | |
905 | ||
906 | // Get a buffer to use to write to the file, given the offset into | |
907 | // the file and the size. | |
908 | unsigned char* | |
909 | get_output_view(off_t start, off_t size) | |
910 | { | |
911 | assert(start >= 0 && size >= 0 && start + size <= this->file_size_); | |
912 | return this->base_ + start; | |
913 | } | |
914 | ||
915 | // VIEW must have been returned by get_output_view. Write the | |
916 | // buffer to the file, passing in the offset and the size. | |
917 | void | |
918 | write_output_view(off_t, off_t, unsigned char*) | |
919 | { } | |
920 | ||
921 | private: | |
922 | // General options. | |
923 | const General_options& options_; | |
924 | // File name. | |
925 | const char* name_; | |
926 | // File descriptor. | |
927 | int o_; | |
928 | // File size. | |
929 | off_t file_size_; | |
930 | // Base of file mapped into memory. | |
931 | unsigned char* base_; | |
a2fb1b05 ILT |
932 | }; |
933 | ||
934 | } // End namespace gold. | |
935 | ||
936 | #endif // !defined(GOLD_OUTPUT_H) |