Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // output.h -- manage the output file for gold -*- C++ -*- |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
a2fb1b05 ILT |
23 | #ifndef GOLD_OUTPUT_H |
24 | #define GOLD_OUTPUT_H | |
25 | ||
26 | #include <list> | |
ead1e424 | 27 | #include <vector> |
a2fb1b05 ILT |
28 | |
29 | #include "elfcpp.h" | |
54dc6425 | 30 | #include "layout.h" |
c06b7b0b | 31 | #include "reloc-types.h" |
a2fb1b05 ILT |
32 | |
33 | namespace gold | |
34 | { | |
35 | ||
61ba1cf9 | 36 | class General_options; |
a2fb1b05 | 37 | class Object; |
a3ad94ed | 38 | class Symbol; |
a2fb1b05 | 39 | class Output_file; |
c06b7b0b | 40 | class Output_section; |
a3ad94ed | 41 | class Target; |
54dc6425 ILT |
42 | template<int size, bool big_endian> |
43 | class Sized_target; | |
c06b7b0b ILT |
44 | template<int size, bool big_endian> |
45 | class Sized_relobj; | |
54dc6425 ILT |
46 | |
47 | // An abtract class for data which has to go into the output file. | |
a2fb1b05 ILT |
48 | |
49 | class Output_data | |
50 | { | |
51 | public: | |
27bc2bce ILT |
52 | explicit Output_data() |
53 | : address_(0), data_size_(0), offset_(-1), | |
54 | is_address_valid_(false), is_data_size_valid_(false), | |
55 | is_offset_valid_(false), | |
4f4c5f80 | 56 | dynamic_reloc_count_(0) |
a2fb1b05 ILT |
57 | { } |
58 | ||
59 | virtual | |
60 | ~Output_data(); | |
61 | ||
27bc2bce ILT |
62 | // Return the address. For allocated sections, this is only valid |
63 | // after Layout::finalize is finished. | |
75f65a3e ILT |
64 | uint64_t |
65 | address() const | |
27bc2bce ILT |
66 | { |
67 | gold_assert(this->is_address_valid_); | |
68 | return this->address_; | |
69 | } | |
75f65a3e | 70 | |
27bc2bce ILT |
71 | // Return the size of the data. For allocated sections, this must |
72 | // be valid after Layout::finalize calls set_address, but need not | |
73 | // be valid before then. | |
a2fb1b05 | 74 | off_t |
75f65a3e | 75 | data_size() const |
27bc2bce ILT |
76 | { |
77 | gold_assert(this->is_data_size_valid_); | |
78 | return this->data_size_; | |
79 | } | |
75f65a3e | 80 | |
ead1e424 | 81 | // Return the file offset. This is only valid after |
27bc2bce ILT |
82 | // Layout::finalize is finished. For some non-allocated sections, |
83 | // it may not be valid until near the end of the link. | |
75f65a3e ILT |
84 | off_t |
85 | offset() const | |
27bc2bce ILT |
86 | { |
87 | gold_assert(this->is_offset_valid_); | |
88 | return this->offset_; | |
89 | } | |
75f65a3e ILT |
90 | |
91 | // Return the required alignment. | |
92 | uint64_t | |
93 | addralign() const | |
94 | { return this->do_addralign(); } | |
95 | ||
96 | // Return whether this is an Output_section. | |
97 | bool | |
98 | is_section() const | |
99 | { return this->do_is_section(); } | |
100 | ||
101 | // Return whether this is an Output_section of the specified type. | |
102 | bool | |
103 | is_section_type(elfcpp::Elf_Word stt) const | |
104 | { return this->do_is_section_type(stt); } | |
105 | ||
106 | // Return whether this is an Output_section with the specified flag | |
107 | // set. | |
108 | bool | |
109 | is_section_flag_set(elfcpp::Elf_Xword shf) const | |
110 | { return this->do_is_section_flag_set(shf); } | |
111 | ||
ead1e424 ILT |
112 | // Return the output section index, if there is an output section. |
113 | unsigned int | |
114 | out_shndx() const | |
115 | { return this->do_out_shndx(); } | |
116 | ||
117 | // Set the output section index, if this is an output section. | |
118 | void | |
119 | set_out_shndx(unsigned int shndx) | |
120 | { this->do_set_out_shndx(shndx); } | |
121 | ||
27bc2bce ILT |
122 | // Set the address and file offset of this data, and finalize the |
123 | // size of the data. This is called during Layout::finalize for | |
124 | // allocated sections. | |
75f65a3e | 125 | void |
27bc2bce ILT |
126 | set_address_and_file_offset(uint64_t addr, off_t off) |
127 | { | |
128 | this->set_address(addr); | |
129 | this->set_file_offset(off); | |
130 | this->finalize_data_size(); | |
131 | } | |
132 | ||
133 | // Set the address. | |
134 | void | |
135 | set_address(uint64_t addr) | |
136 | { | |
137 | gold_assert(!this->is_address_valid_); | |
138 | this->address_ = addr; | |
139 | this->is_address_valid_ = true; | |
140 | } | |
141 | ||
142 | // Set the file offset. | |
143 | void | |
144 | set_file_offset(off_t off) | |
145 | { | |
146 | gold_assert(!this->is_offset_valid_); | |
147 | this->offset_ = off; | |
148 | this->is_offset_valid_ = true; | |
149 | } | |
150 | ||
151 | // Finalize the data size. | |
152 | void | |
153 | finalize_data_size() | |
154 | { | |
155 | if (!this->is_data_size_valid_) | |
156 | { | |
157 | // Tell the child class to set the data size. | |
158 | this->set_final_data_size(); | |
159 | gold_assert(this->is_data_size_valid_); | |
160 | } | |
161 | } | |
75f65a3e | 162 | |
ead1e424 ILT |
163 | // Write the data to the output file. This is called after |
164 | // Layout::finalize is complete. | |
75f65a3e ILT |
165 | void |
166 | write(Output_file* file) | |
167 | { this->do_write(file); } | |
a2fb1b05 | 168 | |
27bc2bce ILT |
169 | // This is called by Layout::finalize to note that the sizes of |
170 | // allocated sections must now be fixed. | |
a3ad94ed ILT |
171 | static void |
172 | layout_complete() | |
27bc2bce | 173 | { Output_data::allocated_sizes_are_fixed = true; } |
a3ad94ed | 174 | |
730cdc88 ILT |
175 | // Used to check that layout has been done. |
176 | static bool | |
177 | is_layout_complete() | |
27bc2bce | 178 | { return Output_data::allocated_sizes_are_fixed; } |
730cdc88 | 179 | |
4f4c5f80 ILT |
180 | // Count the number of dynamic relocations applied to this section. |
181 | void | |
182 | add_dynamic_reloc() | |
183 | { ++this->dynamic_reloc_count_; } | |
184 | ||
185 | // Return the number of dynamic relocations applied to this section. | |
186 | unsigned int | |
187 | dynamic_reloc_count() const | |
188 | { return this->dynamic_reloc_count_; } | |
189 | ||
75f65a3e ILT |
190 | protected: |
191 | // Functions that child classes may or in some cases must implement. | |
192 | ||
193 | // Write the data to the output file. | |
a2fb1b05 | 194 | virtual void |
75f65a3e ILT |
195 | do_write(Output_file*) = 0; |
196 | ||
197 | // Return the required alignment. | |
198 | virtual uint64_t | |
199 | do_addralign() const = 0; | |
200 | ||
201 | // Return whether this is an Output_section. | |
202 | virtual bool | |
203 | do_is_section() const | |
204 | { return false; } | |
a2fb1b05 | 205 | |
54dc6425 | 206 | // Return whether this is an Output_section of the specified type. |
75f65a3e | 207 | // This only needs to be implement by Output_section. |
54dc6425 | 208 | virtual bool |
75f65a3e | 209 | do_is_section_type(elfcpp::Elf_Word) const |
54dc6425 ILT |
210 | { return false; } |
211 | ||
75f65a3e ILT |
212 | // Return whether this is an Output_section with the specific flag |
213 | // set. This only needs to be implemented by Output_section. | |
54dc6425 | 214 | virtual bool |
75f65a3e | 215 | do_is_section_flag_set(elfcpp::Elf_Xword) const |
54dc6425 ILT |
216 | { return false; } |
217 | ||
ead1e424 ILT |
218 | // Return the output section index, if there is an output section. |
219 | virtual unsigned int | |
220 | do_out_shndx() const | |
a3ad94ed | 221 | { gold_unreachable(); } |
ead1e424 ILT |
222 | |
223 | // Set the output section index, if this is an output section. | |
224 | virtual void | |
225 | do_set_out_shndx(unsigned int) | |
a3ad94ed | 226 | { gold_unreachable(); } |
ead1e424 | 227 | |
27bc2bce ILT |
228 | // This is a hook for derived classes to set the data size. This is |
229 | // called by finalize_data_size, normally called during | |
230 | // Layout::finalize, when the section address is set. | |
75f65a3e | 231 | virtual void |
27bc2bce ILT |
232 | set_final_data_size() |
233 | { gold_unreachable(); } | |
75f65a3e ILT |
234 | |
235 | // Functions that child classes may call. | |
236 | ||
27bc2bce ILT |
237 | // Whether the address is valid. |
238 | bool | |
239 | is_address_valid() const | |
240 | { return this->is_address_valid_; } | |
241 | ||
242 | // Whether the file offset is valid. | |
243 | bool | |
244 | is_offset_valid() const | |
245 | { return this->is_offset_valid_; } | |
246 | ||
247 | // Whether the data size is valid. | |
248 | bool | |
249 | is_data_size_valid() const | |
250 | { return this->is_data_size_valid_; } | |
251 | ||
a2fb1b05 ILT |
252 | // Set the size of the data. |
253 | void | |
75f65a3e | 254 | set_data_size(off_t data_size) |
a3ad94ed | 255 | { |
27bc2bce ILT |
256 | gold_assert(!this->is_data_size_valid_); |
257 | this->data_size_ = data_size; | |
258 | this->is_data_size_valid_ = true; | |
259 | } | |
260 | ||
261 | // Get the current data size--this is for the convenience of | |
262 | // sections which build up their size over time. | |
263 | off_t | |
264 | current_data_size_for_child() const | |
265 | { return this->data_size_; } | |
266 | ||
267 | // Set the current data size--this is for the convenience of | |
268 | // sections which build up their size over time. | |
269 | void | |
270 | set_current_data_size_for_child(off_t data_size) | |
271 | { | |
272 | gold_assert(!this->is_data_size_valid_); | |
a3ad94ed ILT |
273 | this->data_size_ = data_size; |
274 | } | |
75f65a3e | 275 | |
730cdc88 ILT |
276 | // Return default alignment for the target size. |
277 | static uint64_t | |
278 | default_alignment(); | |
279 | ||
280 | // Return default alignment for a specified size--32 or 64. | |
75f65a3e | 281 | static uint64_t |
730cdc88 | 282 | default_alignment_for_size(int size); |
a2fb1b05 ILT |
283 | |
284 | private: | |
285 | Output_data(const Output_data&); | |
286 | Output_data& operator=(const Output_data&); | |
287 | ||
a3ad94ed | 288 | // This is used for verification, to make sure that we don't try to |
27bc2bce ILT |
289 | // change any sizes of allocated sections after we set the section |
290 | // addresses. | |
291 | static bool allocated_sizes_are_fixed; | |
a3ad94ed | 292 | |
27bc2bce | 293 | // Memory address in output file. |
75f65a3e | 294 | uint64_t address_; |
27bc2bce | 295 | // Size of data in output file. |
75f65a3e | 296 | off_t data_size_; |
27bc2bce | 297 | // File offset of contents in output file. |
75f65a3e | 298 | off_t offset_; |
27bc2bce ILT |
299 | // Whether address_ is valid. |
300 | bool is_address_valid_; | |
301 | // Whether data_size_ is valid. | |
302 | bool is_data_size_valid_; | |
303 | // Whether offset_ is valid. | |
304 | bool is_offset_valid_; | |
4f4c5f80 ILT |
305 | // Count of dynamic relocations applied to this section. |
306 | unsigned int dynamic_reloc_count_; | |
a2fb1b05 ILT |
307 | }; |
308 | ||
54dc6425 ILT |
309 | // Output the section headers. |
310 | ||
311 | class Output_section_headers : public Output_data | |
312 | { | |
313 | public: | |
9025d29d | 314 | Output_section_headers(const Layout*, |
16649710 ILT |
315 | const Layout::Segment_list*, |
316 | const Layout::Section_list*, | |
61ba1cf9 | 317 | const Stringpool*); |
54dc6425 | 318 | |
27bc2bce | 319 | protected: |
54dc6425 ILT |
320 | // Write the data to the file. |
321 | void | |
75f65a3e ILT |
322 | do_write(Output_file*); |
323 | ||
324 | // Return the required alignment. | |
325 | uint64_t | |
326 | do_addralign() const | |
730cdc88 | 327 | { return Output_data::default_alignment(); } |
54dc6425 ILT |
328 | |
329 | private: | |
61ba1cf9 ILT |
330 | // Write the data to the file with the right size and endianness. |
331 | template<int size, bool big_endian> | |
332 | void | |
333 | do_sized_write(Output_file*); | |
334 | ||
16649710 ILT |
335 | const Layout* layout_; |
336 | const Layout::Segment_list* segment_list_; | |
337 | const Layout::Section_list* unattached_section_list_; | |
61ba1cf9 | 338 | const Stringpool* secnamepool_; |
54dc6425 ILT |
339 | }; |
340 | ||
341 | // Output the segment headers. | |
342 | ||
343 | class Output_segment_headers : public Output_data | |
344 | { | |
345 | public: | |
9025d29d | 346 | Output_segment_headers(const Layout::Segment_list& segment_list); |
54dc6425 | 347 | |
27bc2bce | 348 | protected: |
54dc6425 ILT |
349 | // Write the data to the file. |
350 | void | |
75f65a3e ILT |
351 | do_write(Output_file*); |
352 | ||
353 | // Return the required alignment. | |
354 | uint64_t | |
355 | do_addralign() const | |
730cdc88 | 356 | { return Output_data::default_alignment(); } |
54dc6425 ILT |
357 | |
358 | private: | |
61ba1cf9 ILT |
359 | // Write the data to the file with the right size and endianness. |
360 | template<int size, bool big_endian> | |
361 | void | |
362 | do_sized_write(Output_file*); | |
363 | ||
54dc6425 ILT |
364 | const Layout::Segment_list& segment_list_; |
365 | }; | |
366 | ||
367 | // Output the ELF file header. | |
368 | ||
369 | class Output_file_header : public Output_data | |
370 | { | |
371 | public: | |
9025d29d | 372 | Output_file_header(const Target*, |
54dc6425 | 373 | const Symbol_table*, |
75f65a3e ILT |
374 | const Output_segment_headers*); |
375 | ||
376 | // Add information about the section headers. We lay out the ELF | |
377 | // file header before we create the section headers. | |
378 | void set_section_info(const Output_section_headers*, | |
379 | const Output_section* shstrtab); | |
54dc6425 | 380 | |
27bc2bce | 381 | protected: |
54dc6425 ILT |
382 | // Write the data to the file. |
383 | void | |
75f65a3e ILT |
384 | do_write(Output_file*); |
385 | ||
386 | // Return the required alignment. | |
387 | uint64_t | |
388 | do_addralign() const | |
730cdc88 | 389 | { return Output_data::default_alignment(); } |
75f65a3e | 390 | |
54dc6425 | 391 | private: |
61ba1cf9 ILT |
392 | // Write the data to the file with the right size and endianness. |
393 | template<int size, bool big_endian> | |
394 | void | |
395 | do_sized_write(Output_file*); | |
396 | ||
54dc6425 ILT |
397 | const Target* target_; |
398 | const Symbol_table* symtab_; | |
61ba1cf9 | 399 | const Output_segment_headers* segment_header_; |
54dc6425 ILT |
400 | const Output_section_headers* section_header_; |
401 | const Output_section* shstrtab_; | |
402 | }; | |
403 | ||
ead1e424 ILT |
404 | // Output sections are mainly comprised of input sections. However, |
405 | // there are cases where we have data to write out which is not in an | |
406 | // input section. Output_section_data is used in such cases. This is | |
407 | // an abstract base class. | |
408 | ||
409 | class Output_section_data : public Output_data | |
410 | { | |
411 | public: | |
412 | Output_section_data(off_t data_size, uint64_t addralign) | |
27bc2bce ILT |
413 | : Output_data(), output_section_(NULL), addralign_(addralign) |
414 | { this->set_data_size(data_size); } | |
ead1e424 ILT |
415 | |
416 | Output_section_data(uint64_t addralign) | |
27bc2bce | 417 | : Output_data(), output_section_(NULL), addralign_(addralign) |
ead1e424 ILT |
418 | { } |
419 | ||
16649710 ILT |
420 | // Return the output section. |
421 | const Output_section* | |
422 | output_section() const | |
423 | { return this->output_section_; } | |
424 | ||
ead1e424 ILT |
425 | // Record the output section. |
426 | void | |
16649710 | 427 | set_output_section(Output_section* os); |
ead1e424 | 428 | |
b8e6aad9 ILT |
429 | // Add an input section, for SHF_MERGE sections. This returns true |
430 | // if the section was handled. | |
431 | bool | |
432 | add_input_section(Relobj* object, unsigned int shndx) | |
433 | { return this->do_add_input_section(object, shndx); } | |
434 | ||
435 | // Given an input OBJECT, an input section index SHNDX within that | |
436 | // object, and an OFFSET relative to the start of that input | |
730cdc88 ILT |
437 | // section, return whether or not the corresponding offset within |
438 | // the output section is known. If this function returns true, it | |
439 | // sets *POUTPUT to the output offset. The value -1 indicates that | |
440 | // this input offset is being discarded. | |
b8e6aad9 | 441 | virtual bool |
730cdc88 ILT |
442 | output_offset(const Relobj* object, unsigned int shndx, off_t offset, |
443 | off_t *poutput) const | |
444 | { return this->do_output_offset(object, shndx, offset, poutput); } | |
b8e6aad9 | 445 | |
ead1e424 ILT |
446 | protected: |
447 | // The child class must implement do_write. | |
448 | ||
16649710 ILT |
449 | // The child class may implement specific adjustments to the output |
450 | // section. | |
451 | virtual void | |
452 | do_adjust_output_section(Output_section*) | |
453 | { } | |
454 | ||
b8e6aad9 ILT |
455 | // May be implemented by child class. Return true if the section |
456 | // was handled. | |
457 | virtual bool | |
458 | do_add_input_section(Relobj*, unsigned int) | |
459 | { gold_unreachable(); } | |
460 | ||
730cdc88 | 461 | // The child class may implement output_offset. |
b8e6aad9 | 462 | virtual bool |
730cdc88 | 463 | do_output_offset(const Relobj*, unsigned int, off_t, off_t*) const |
b8e6aad9 ILT |
464 | { return false; } |
465 | ||
ead1e424 ILT |
466 | // Return the required alignment. |
467 | uint64_t | |
468 | do_addralign() const | |
469 | { return this->addralign_; } | |
470 | ||
471 | // Return the section index of the output section. | |
472 | unsigned int | |
473 | do_out_shndx() const; | |
474 | ||
5a6f7e2d ILT |
475 | // Set the alignment. |
476 | void | |
477 | set_addralign(uint64_t addralign) | |
478 | { this->addralign_ = addralign; } | |
479 | ||
ead1e424 ILT |
480 | private: |
481 | // The output section for this section. | |
482 | const Output_section* output_section_; | |
483 | // The required alignment. | |
484 | uint64_t addralign_; | |
485 | }; | |
486 | ||
27bc2bce ILT |
487 | // Some Output_section_data classes build up their data step by step, |
488 | // rather than all at once. This class provides an interface for | |
489 | // them. | |
490 | ||
491 | class Output_section_data_build : public Output_section_data | |
492 | { | |
493 | public: | |
494 | Output_section_data_build(uint64_t addralign) | |
495 | : Output_section_data(addralign) | |
496 | { } | |
497 | ||
498 | // Get the current data size. | |
499 | off_t | |
500 | current_data_size() const | |
501 | { return this->current_data_size_for_child(); } | |
502 | ||
503 | // Set the current data size. | |
504 | void | |
505 | set_current_data_size(off_t data_size) | |
506 | { this->set_current_data_size_for_child(data_size); } | |
507 | ||
508 | protected: | |
509 | // Set the final data size. | |
510 | virtual void | |
511 | set_final_data_size() | |
512 | { this->set_data_size(this->current_data_size_for_child()); } | |
513 | }; | |
514 | ||
dbe717ef ILT |
515 | // A simple case of Output_data in which we have constant data to |
516 | // output. | |
ead1e424 | 517 | |
dbe717ef | 518 | class Output_data_const : public Output_section_data |
ead1e424 ILT |
519 | { |
520 | public: | |
dbe717ef ILT |
521 | Output_data_const(const std::string& data, uint64_t addralign) |
522 | : Output_section_data(data.size(), addralign), data_(data) | |
523 | { } | |
524 | ||
525 | Output_data_const(const char* p, off_t len, uint64_t addralign) | |
526 | : Output_section_data(len, addralign), data_(p, len) | |
527 | { } | |
528 | ||
529 | Output_data_const(const unsigned char* p, off_t len, uint64_t addralign) | |
530 | : Output_section_data(len, addralign), | |
531 | data_(reinterpret_cast<const char*>(p), len) | |
532 | { } | |
533 | ||
27bc2bce | 534 | protected: |
a3ad94ed | 535 | // Write the data to the output file. |
dbe717ef | 536 | void |
a3ad94ed | 537 | do_write(Output_file*); |
dbe717ef ILT |
538 | |
539 | private: | |
540 | std::string data_; | |
541 | }; | |
542 | ||
a3ad94ed ILT |
543 | // Another version of Output_data with constant data, in which the |
544 | // buffer is allocated by the caller. | |
dbe717ef | 545 | |
a3ad94ed | 546 | class Output_data_const_buffer : public Output_section_data |
dbe717ef ILT |
547 | { |
548 | public: | |
a3ad94ed ILT |
549 | Output_data_const_buffer(const unsigned char* p, off_t len, |
550 | uint64_t addralign) | |
551 | : Output_section_data(len, addralign), p_(p) | |
552 | { } | |
553 | ||
27bc2bce | 554 | protected: |
a3ad94ed ILT |
555 | // Write the data the output file. |
556 | void | |
557 | do_write(Output_file*); | |
558 | ||
559 | private: | |
560 | const unsigned char* p_; | |
561 | }; | |
562 | ||
27bc2bce ILT |
563 | // A place holder for a fixed amount of data written out via some |
564 | // other mechanism. | |
a3ad94ed | 565 | |
27bc2bce | 566 | class Output_data_fixed_space : public Output_section_data |
a3ad94ed ILT |
567 | { |
568 | public: | |
27bc2bce | 569 | Output_data_fixed_space(off_t data_size, uint64_t addralign) |
a3ad94ed ILT |
570 | : Output_section_data(data_size, addralign) |
571 | { } | |
572 | ||
27bc2bce ILT |
573 | protected: |
574 | // Write out the data--the actual data must be written out | |
575 | // elsewhere. | |
576 | void | |
577 | do_write(Output_file*) | |
ead1e424 | 578 | { } |
27bc2bce | 579 | }; |
ead1e424 | 580 | |
27bc2bce ILT |
581 | // A place holder for variable sized data written out via some other |
582 | // mechanism. | |
583 | ||
584 | class Output_data_space : public Output_section_data_build | |
585 | { | |
586 | public: | |
587 | explicit Output_data_space(uint64_t addralign) | |
588 | : Output_section_data_build(addralign) | |
589 | { } | |
ead1e424 | 590 | |
5a6f7e2d ILT |
591 | // Set the alignment. |
592 | void | |
593 | set_space_alignment(uint64_t align) | |
594 | { this->set_addralign(align); } | |
595 | ||
27bc2bce ILT |
596 | protected: |
597 | // Write out the data--the actual data must be written out | |
598 | // elsewhere. | |
ead1e424 ILT |
599 | void |
600 | do_write(Output_file*) | |
601 | { } | |
602 | }; | |
603 | ||
a3ad94ed ILT |
604 | // A string table which goes into an output section. |
605 | ||
606 | class Output_data_strtab : public Output_section_data | |
607 | { | |
608 | public: | |
609 | Output_data_strtab(Stringpool* strtab) | |
610 | : Output_section_data(1), strtab_(strtab) | |
611 | { } | |
612 | ||
27bc2bce | 613 | protected: |
a3ad94ed ILT |
614 | // This is called to set the address and file offset. Here we make |
615 | // sure that the Stringpool is finalized. | |
616 | void | |
27bc2bce | 617 | set_final_data_size(); |
a3ad94ed ILT |
618 | |
619 | // Write out the data. | |
620 | void | |
621 | do_write(Output_file*); | |
622 | ||
623 | private: | |
624 | Stringpool* strtab_; | |
625 | }; | |
626 | ||
c06b7b0b ILT |
627 | // This POD class is used to represent a single reloc in the output |
628 | // file. This could be a private class within Output_data_reloc, but | |
629 | // the templatization is complex enough that I broke it out into a | |
630 | // separate class. The class is templatized on either elfcpp::SHT_REL | |
631 | // or elfcpp::SHT_RELA, and also on whether this is a dynamic | |
632 | // relocation or an ordinary relocation. | |
633 | ||
634 | // A relocation can be against a global symbol, a local symbol, an | |
635 | // output section, or the undefined symbol at index 0. We represent | |
636 | // the latter by using a NULL global symbol. | |
637 | ||
638 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
639 | class Output_reloc; | |
640 | ||
641 | template<bool dynamic, int size, bool big_endian> | |
642 | class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
643 | { | |
644 | public: | |
645 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
646 | ||
647 | // An uninitialized entry. We need this because we want to put | |
648 | // instances of this class into an STL container. | |
649 | Output_reloc() | |
650 | : local_sym_index_(INVALID_CODE) | |
651 | { } | |
652 | ||
653 | // A reloc against a global symbol. | |
5a6f7e2d | 654 | |
a3ad94ed ILT |
655 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
656 | Address address) | |
5a6f7e2d ILT |
657 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), |
658 | shndx_(INVALID_CODE) | |
659 | { | |
660 | this->u1_.gsym = gsym; | |
661 | this->u2_.od = od; | |
662 | } | |
663 | ||
664 | Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj, | |
665 | unsigned int shndx, Address address) | |
666 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), | |
667 | shndx_(shndx) | |
668 | { | |
669 | gold_assert(shndx != INVALID_CODE); | |
670 | this->u1_.gsym = gsym; | |
671 | this->u2_.relobj = relobj; | |
672 | } | |
c06b7b0b ILT |
673 | |
674 | // A reloc against a local symbol. | |
5a6f7e2d ILT |
675 | |
676 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
c06b7b0b | 677 | unsigned int local_sym_index, |
a3ad94ed ILT |
678 | unsigned int type, |
679 | Output_data* od, | |
680 | Address address) | |
5a6f7e2d ILT |
681 | : address_(address), local_sym_index_(local_sym_index), type_(type), |
682 | shndx_(INVALID_CODE) | |
c06b7b0b | 683 | { |
a3ad94ed ILT |
684 | gold_assert(local_sym_index != GSYM_CODE |
685 | && local_sym_index != INVALID_CODE); | |
5a6f7e2d ILT |
686 | this->u1_.relobj = relobj; |
687 | this->u2_.od = od; | |
688 | } | |
689 | ||
690 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
691 | unsigned int local_sym_index, | |
692 | unsigned int type, | |
693 | unsigned int shndx, | |
694 | Address address) | |
695 | : address_(address), local_sym_index_(local_sym_index), type_(type), | |
696 | shndx_(shndx) | |
697 | { | |
698 | gold_assert(local_sym_index != GSYM_CODE | |
699 | && local_sym_index != INVALID_CODE); | |
700 | gold_assert(shndx != INVALID_CODE); | |
701 | this->u1_.relobj = relobj; | |
702 | this->u2_.relobj = relobj; | |
c06b7b0b ILT |
703 | } |
704 | ||
705 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 706 | |
a3ad94ed ILT |
707 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
708 | Address address) | |
5a6f7e2d ILT |
709 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), |
710 | shndx_(INVALID_CODE) | |
711 | { | |
712 | this->u1_.os = os; | |
713 | this->u2_.od = od; | |
714 | } | |
715 | ||
716 | Output_reloc(Output_section* os, unsigned int type, Relobj* relobj, | |
717 | unsigned int shndx, Address address) | |
718 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), | |
719 | shndx_(shndx) | |
720 | { | |
721 | gold_assert(shndx != INVALID_CODE); | |
722 | this->u1_.os = os; | |
723 | this->u2_.relobj = relobj; | |
724 | } | |
c06b7b0b ILT |
725 | |
726 | // Write the reloc entry to an output view. | |
727 | void | |
728 | write(unsigned char* pov) const; | |
729 | ||
730 | // Write the offset and info fields to Write_rel. | |
731 | template<typename Write_rel> | |
732 | void write_rel(Write_rel*) const; | |
733 | ||
734 | private: | |
735 | // Return the symbol index. We can't do a double template | |
736 | // specialization, so we do a secondary template here. | |
737 | unsigned int | |
738 | get_symbol_index() const; | |
739 | ||
740 | // Codes for local_sym_index_. | |
741 | enum | |
742 | { | |
743 | // Global symbol. | |
744 | GSYM_CODE = -1U, | |
745 | // Output section. | |
746 | SECTION_CODE = -2U, | |
747 | // Invalid uninitialized entry. | |
748 | INVALID_CODE = -3U | |
749 | }; | |
750 | ||
751 | union | |
752 | { | |
753 | // For a local symbol, the object. We will never generate a | |
754 | // relocation against a local symbol in a dynamic object; that | |
755 | // doesn't make sense. And our callers will always be | |
756 | // templatized, so we use Sized_relobj here. | |
5a6f7e2d | 757 | Sized_relobj<size, big_endian>* relobj; |
c06b7b0b ILT |
758 | // For a global symbol, the symbol. If this is NULL, it indicates |
759 | // a relocation against the undefined 0 symbol. | |
760 | Symbol* gsym; | |
761 | // For a relocation against an output section, the output section. | |
762 | Output_section* os; | |
5a6f7e2d ILT |
763 | } u1_; |
764 | union | |
765 | { | |
766 | // If shndx_ is not INVALID CODE, the object which holds the input | |
767 | // section being used to specify the reloc address. | |
768 | Relobj* relobj; | |
769 | // If shndx_ is INVALID_CODE, the output data being used to | |
770 | // specify the reloc address. This may be NULL if the reloc | |
771 | // address is absolute. | |
772 | Output_data* od; | |
773 | } u2_; | |
774 | // The address offset within the input section or the Output_data. | |
775 | Address address_; | |
c06b7b0b ILT |
776 | // For a local symbol, the local symbol index. This is GSYM_CODE |
777 | // for a global symbol, or INVALID_CODE for an uninitialized value. | |
778 | unsigned int local_sym_index_; | |
a3ad94ed | 779 | // The reloc type--a processor specific code. |
c06b7b0b | 780 | unsigned int type_; |
5a6f7e2d ILT |
781 | // If the reloc address is an input section in an object, the |
782 | // section index. This is INVALID_CODE if the reloc address is | |
783 | // specified in some other way. | |
784 | unsigned int shndx_; | |
c06b7b0b ILT |
785 | }; |
786 | ||
787 | // The SHT_RELA version of Output_reloc<>. This is just derived from | |
788 | // the SHT_REL version of Output_reloc, but it adds an addend. | |
789 | ||
790 | template<bool dynamic, int size, bool big_endian> | |
791 | class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
792 | { | |
793 | public: | |
794 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
795 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; | |
796 | ||
797 | // An uninitialized entry. | |
798 | Output_reloc() | |
799 | : rel_() | |
800 | { } | |
801 | ||
802 | // A reloc against a global symbol. | |
5a6f7e2d | 803 | |
a3ad94ed ILT |
804 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
805 | Address address, Addend addend) | |
806 | : rel_(gsym, type, od, address), addend_(addend) | |
c06b7b0b ILT |
807 | { } |
808 | ||
5a6f7e2d ILT |
809 | Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj, |
810 | unsigned int shndx, Address address, Addend addend) | |
811 | : rel_(gsym, type, relobj, shndx, address), addend_(addend) | |
812 | { } | |
813 | ||
c06b7b0b | 814 | // A reloc against a local symbol. |
5a6f7e2d ILT |
815 | |
816 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
c06b7b0b | 817 | unsigned int local_sym_index, |
a3ad94ed ILT |
818 | unsigned int type, Output_data* od, Address address, |
819 | Addend addend) | |
5a6f7e2d ILT |
820 | : rel_(relobj, local_sym_index, type, od, address), addend_(addend) |
821 | { } | |
822 | ||
823 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
824 | unsigned int local_sym_index, | |
825 | unsigned int type, | |
826 | unsigned int shndx, | |
827 | Address address, | |
828 | Addend addend) | |
829 | : rel_(relobj, local_sym_index, type, shndx, address), | |
830 | addend_(addend) | |
c06b7b0b ILT |
831 | { } |
832 | ||
833 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 834 | |
a3ad94ed ILT |
835 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
836 | Address address, Addend addend) | |
837 | : rel_(os, type, od, address), addend_(addend) | |
c06b7b0b ILT |
838 | { } |
839 | ||
5a6f7e2d ILT |
840 | Output_reloc(Output_section* os, unsigned int type, Relobj* relobj, |
841 | unsigned int shndx, Address address, Addend addend) | |
842 | : rel_(os, type, relobj, shndx, address), addend_(addend) | |
843 | { } | |
844 | ||
c06b7b0b ILT |
845 | // Write the reloc entry to an output view. |
846 | void | |
847 | write(unsigned char* pov) const; | |
848 | ||
849 | private: | |
850 | // The basic reloc. | |
851 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_; | |
852 | // The addend. | |
853 | Addend addend_; | |
854 | }; | |
855 | ||
856 | // Output_data_reloc is used to manage a section containing relocs. | |
857 | // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC | |
858 | // indicates whether this is a dynamic relocation or a normal | |
859 | // relocation. Output_data_reloc_base is a base class. | |
860 | // Output_data_reloc is the real class, which we specialize based on | |
861 | // the reloc type. | |
862 | ||
863 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
27bc2bce | 864 | class Output_data_reloc_base : public Output_section_data_build |
c06b7b0b ILT |
865 | { |
866 | public: | |
867 | typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type; | |
868 | typedef typename Output_reloc_type::Address Address; | |
869 | static const int reloc_size = | |
870 | Reloc_types<sh_type, size, big_endian>::reloc_size; | |
871 | ||
872 | // Construct the section. | |
873 | Output_data_reloc_base() | |
27bc2bce | 874 | : Output_section_data_build(Output_data::default_alignment_for_size(size)) |
c06b7b0b ILT |
875 | { } |
876 | ||
27bc2bce | 877 | protected: |
c06b7b0b ILT |
878 | // Write out the data. |
879 | void | |
880 | do_write(Output_file*); | |
881 | ||
16649710 ILT |
882 | // Set the entry size and the link. |
883 | void | |
884 | do_adjust_output_section(Output_section *os); | |
885 | ||
c06b7b0b ILT |
886 | // Add a relocation entry. |
887 | void | |
4f4c5f80 | 888 | add(Output_data *od, const Output_reloc_type& reloc) |
c06b7b0b ILT |
889 | { |
890 | this->relocs_.push_back(reloc); | |
27bc2bce | 891 | this->set_current_data_size(this->relocs_.size() * reloc_size); |
4f4c5f80 | 892 | od->add_dynamic_reloc(); |
c06b7b0b ILT |
893 | } |
894 | ||
895 | private: | |
896 | typedef std::vector<Output_reloc_type> Relocs; | |
897 | ||
898 | Relocs relocs_; | |
899 | }; | |
900 | ||
901 | // The class which callers actually create. | |
902 | ||
903 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
904 | class Output_data_reloc; | |
905 | ||
906 | // The SHT_REL version of Output_data_reloc. | |
907 | ||
908 | template<bool dynamic, int size, bool big_endian> | |
909 | class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
910 | : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian> | |
911 | { | |
912 | private: | |
913 | typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, | |
914 | big_endian> Base; | |
915 | ||
916 | public: | |
917 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
918 | typedef typename Output_reloc_type::Address Address; | |
919 | ||
920 | Output_data_reloc() | |
921 | : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>() | |
922 | { } | |
923 | ||
924 | // Add a reloc against a global symbol. | |
5a6f7e2d | 925 | |
c06b7b0b | 926 | void |
a3ad94ed | 927 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address) |
4f4c5f80 | 928 | { this->add(od, Output_reloc_type(gsym, type, od, address)); } |
c06b7b0b | 929 | |
5a6f7e2d | 930 | void |
4f4c5f80 | 931 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj, |
5a6f7e2d | 932 | unsigned int shndx, Address address) |
4f4c5f80 | 933 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address)); } |
5a6f7e2d | 934 | |
c06b7b0b | 935 | // Add a reloc against a local symbol. |
5a6f7e2d | 936 | |
c06b7b0b | 937 | void |
5a6f7e2d | 938 | add_local(Sized_relobj<size, big_endian>* relobj, |
a3ad94ed ILT |
939 | unsigned int local_sym_index, unsigned int type, |
940 | Output_data* od, Address address) | |
4f4c5f80 ILT |
941 | { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, |
942 | address)); } | |
5a6f7e2d ILT |
943 | |
944 | void | |
945 | add_local(Sized_relobj<size, big_endian>* relobj, | |
946 | unsigned int local_sym_index, unsigned int type, | |
4f4c5f80 ILT |
947 | Output_data* od, unsigned int shndx, Address address) |
948 | { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
949 | address)); } | |
5a6f7e2d | 950 | |
c06b7b0b ILT |
951 | |
952 | // A reloc against the STT_SECTION symbol of an output section. | |
4f4c5f80 ILT |
953 | // OS is the Output_section that the relocation refers to; OD is |
954 | // the Output_data object being relocated. | |
5a6f7e2d | 955 | |
c06b7b0b | 956 | void |
a3ad94ed ILT |
957 | add_output_section(Output_section* os, unsigned int type, |
958 | Output_data* od, Address address) | |
4f4c5f80 | 959 | { this->add(od, Output_reloc_type(os, type, od, address)); } |
5a6f7e2d ILT |
960 | |
961 | void | |
4f4c5f80 | 962 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
5a6f7e2d | 963 | Relobj* relobj, unsigned int shndx, Address address) |
4f4c5f80 | 964 | { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); } |
c06b7b0b ILT |
965 | }; |
966 | ||
967 | // The SHT_RELA version of Output_data_reloc. | |
968 | ||
969 | template<bool dynamic, int size, bool big_endian> | |
970 | class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
971 | : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
972 | { | |
973 | private: | |
974 | typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, | |
975 | big_endian> Base; | |
976 | ||
977 | public: | |
978 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
979 | typedef typename Output_reloc_type::Address Address; | |
980 | typedef typename Output_reloc_type::Addend Addend; | |
981 | ||
982 | Output_data_reloc() | |
983 | : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>() | |
984 | { } | |
985 | ||
986 | // Add a reloc against a global symbol. | |
5a6f7e2d | 987 | |
c06b7b0b | 988 | void |
a3ad94ed ILT |
989 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
990 | Address address, Addend addend) | |
4f4c5f80 | 991 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend)); } |
c06b7b0b | 992 | |
5a6f7e2d | 993 | void |
4f4c5f80 ILT |
994 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj, |
995 | unsigned int shndx, Address address, | |
996 | Addend addend) | |
997 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, | |
998 | addend)); } | |
5a6f7e2d | 999 | |
c06b7b0b | 1000 | // Add a reloc against a local symbol. |
5a6f7e2d | 1001 | |
c06b7b0b | 1002 | void |
5a6f7e2d | 1003 | add_local(Sized_relobj<size, big_endian>* relobj, |
c06b7b0b | 1004 | unsigned int local_sym_index, unsigned int type, |
a3ad94ed | 1005 | Output_data* od, Address address, Addend addend) |
c06b7b0b | 1006 | { |
4f4c5f80 ILT |
1007 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
1008 | addend)); | |
5a6f7e2d ILT |
1009 | } |
1010 | ||
1011 | void | |
1012 | add_local(Sized_relobj<size, big_endian>* relobj, | |
1013 | unsigned int local_sym_index, unsigned int type, | |
4f4c5f80 ILT |
1014 | Output_data* od, unsigned int shndx, Address address, |
1015 | Addend addend) | |
5a6f7e2d | 1016 | { |
4f4c5f80 ILT |
1017 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
1018 | address, addend)); | |
c06b7b0b ILT |
1019 | } |
1020 | ||
1021 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 1022 | |
c06b7b0b | 1023 | void |
a3ad94ed ILT |
1024 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
1025 | Address address, Addend addend) | |
4f4c5f80 | 1026 | { this->add(os, Output_reloc_type(os, type, od, address, addend)); } |
5a6f7e2d ILT |
1027 | |
1028 | void | |
1029 | add_output_section(Output_section* os, unsigned int type, Relobj* relobj, | |
1030 | unsigned int shndx, Address address, Addend addend) | |
4f4c5f80 ILT |
1031 | { this->add(os, Output_reloc_type(os, type, relobj, shndx, address, |
1032 | addend)); } | |
c06b7b0b ILT |
1033 | }; |
1034 | ||
dbe717ef ILT |
1035 | // Output_data_got is used to manage a GOT. Each entry in the GOT is |
1036 | // for one symbol--either a global symbol or a local symbol in an | |
ead1e424 | 1037 | // object. The target specific code adds entries to the GOT as |
dbe717ef | 1038 | // needed. |
ead1e424 ILT |
1039 | |
1040 | template<int size, bool big_endian> | |
27bc2bce | 1041 | class Output_data_got : public Output_section_data_build |
ead1e424 ILT |
1042 | { |
1043 | public: | |
1044 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; | |
1045 | ||
7e1edb90 | 1046 | Output_data_got() |
27bc2bce | 1047 | : Output_section_data_build(Output_data::default_alignment_for_size(size)), |
730cdc88 | 1048 | entries_() |
ead1e424 ILT |
1049 | { } |
1050 | ||
dbe717ef ILT |
1051 | // Add an entry for a global symbol to the GOT. Return true if this |
1052 | // is a new GOT entry, false if the symbol was already in the GOT. | |
1053 | bool | |
1054 | add_global(Symbol* gsym); | |
ead1e424 | 1055 | |
e727fa71 ILT |
1056 | // Add an entry for a local symbol to the GOT. This returns true if |
1057 | // this is a new GOT entry, false if the symbol already has a GOT | |
1058 | // entry. | |
1059 | bool | |
1060 | add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index); | |
ead1e424 | 1061 | |
07f397ab ILT |
1062 | // Add an entry (or pair of entries) for a global TLS symbol to the GOT. |
1063 | // Return true if this is a new GOT entry, false if the symbol was | |
1064 | // already in the GOT. | |
1065 | bool | |
1066 | add_global_tls(Symbol* gsym, bool need_pair); | |
1067 | ||
1068 | // Add an entry (or pair of entries) for a local TLS symbol to the GOT. | |
1069 | // This returns true if this is a new GOT entry, false if the symbol | |
1070 | // already has a GOT entry. | |
1071 | bool | |
1072 | add_local_tls(Sized_relobj<size, big_endian>* object, | |
1073 | unsigned int sym_index, bool need_pair); | |
1074 | ||
ead1e424 ILT |
1075 | // Add a constant to the GOT. This returns the offset of the new |
1076 | // entry from the start of the GOT. | |
1077 | unsigned int | |
1078 | add_constant(Valtype constant) | |
1079 | { | |
1080 | this->entries_.push_back(Got_entry(constant)); | |
1081 | this->set_got_size(); | |
1082 | return this->last_got_offset(); | |
1083 | } | |
1084 | ||
27bc2bce | 1085 | protected: |
ead1e424 ILT |
1086 | // Write out the GOT table. |
1087 | void | |
1088 | do_write(Output_file*); | |
1089 | ||
1090 | private: | |
1091 | // This POD class holds a single GOT entry. | |
1092 | class Got_entry | |
1093 | { | |
1094 | public: | |
1095 | // Create a zero entry. | |
1096 | Got_entry() | |
1097 | : local_sym_index_(CONSTANT_CODE) | |
1098 | { this->u_.constant = 0; } | |
1099 | ||
1100 | // Create a global symbol entry. | |
a3ad94ed | 1101 | explicit Got_entry(Symbol* gsym) |
ead1e424 ILT |
1102 | : local_sym_index_(GSYM_CODE) |
1103 | { this->u_.gsym = gsym; } | |
1104 | ||
1105 | // Create a local symbol entry. | |
e727fa71 ILT |
1106 | Got_entry(Sized_relobj<size, big_endian>* object, |
1107 | unsigned int local_sym_index) | |
ead1e424 ILT |
1108 | : local_sym_index_(local_sym_index) |
1109 | { | |
a3ad94ed ILT |
1110 | gold_assert(local_sym_index != GSYM_CODE |
1111 | && local_sym_index != CONSTANT_CODE); | |
ead1e424 ILT |
1112 | this->u_.object = object; |
1113 | } | |
1114 | ||
1115 | // Create a constant entry. The constant is a host value--it will | |
1116 | // be swapped, if necessary, when it is written out. | |
a3ad94ed | 1117 | explicit Got_entry(Valtype constant) |
ead1e424 ILT |
1118 | : local_sym_index_(CONSTANT_CODE) |
1119 | { this->u_.constant = constant; } | |
1120 | ||
1121 | // Write the GOT entry to an output view. | |
1122 | void | |
7e1edb90 | 1123 | write(unsigned char* pov) const; |
ead1e424 ILT |
1124 | |
1125 | private: | |
1126 | enum | |
1127 | { | |
1128 | GSYM_CODE = -1U, | |
1129 | CONSTANT_CODE = -2U | |
1130 | }; | |
1131 | ||
1132 | union | |
1133 | { | |
1134 | // For a local symbol, the object. | |
e727fa71 | 1135 | Sized_relobj<size, big_endian>* object; |
ead1e424 ILT |
1136 | // For a global symbol, the symbol. |
1137 | Symbol* gsym; | |
1138 | // For a constant, the constant. | |
1139 | Valtype constant; | |
1140 | } u_; | |
c06b7b0b ILT |
1141 | // For a local symbol, the local symbol index. This is GSYM_CODE |
1142 | // for a global symbol, or CONSTANT_CODE for a constant. | |
ead1e424 ILT |
1143 | unsigned int local_sym_index_; |
1144 | }; | |
1145 | ||
1146 | typedef std::vector<Got_entry> Got_entries; | |
1147 | ||
1148 | // Return the offset into the GOT of GOT entry I. | |
1149 | unsigned int | |
1150 | got_offset(unsigned int i) const | |
1151 | { return i * (size / 8); } | |
1152 | ||
1153 | // Return the offset into the GOT of the last entry added. | |
1154 | unsigned int | |
1155 | last_got_offset() const | |
1156 | { return this->got_offset(this->entries_.size() - 1); } | |
1157 | ||
1158 | // Set the size of the section. | |
1159 | void | |
1160 | set_got_size() | |
27bc2bce | 1161 | { this->set_current_data_size(this->got_offset(this->entries_.size())); } |
ead1e424 ILT |
1162 | |
1163 | // The list of GOT entries. | |
1164 | Got_entries entries_; | |
1165 | }; | |
1166 | ||
a3ad94ed ILT |
1167 | // Output_data_dynamic is used to hold the data in SHT_DYNAMIC |
1168 | // section. | |
1169 | ||
1170 | class Output_data_dynamic : public Output_section_data | |
1171 | { | |
1172 | public: | |
9025d29d | 1173 | Output_data_dynamic(Stringpool* pool) |
730cdc88 | 1174 | : Output_section_data(Output_data::default_alignment()), |
9025d29d | 1175 | entries_(), pool_(pool) |
a3ad94ed ILT |
1176 | { } |
1177 | ||
1178 | // Add a new dynamic entry with a fixed numeric value. | |
1179 | void | |
1180 | add_constant(elfcpp::DT tag, unsigned int val) | |
1181 | { this->add_entry(Dynamic_entry(tag, val)); } | |
1182 | ||
16649710 | 1183 | // Add a new dynamic entry with the address of output data. |
a3ad94ed | 1184 | void |
16649710 ILT |
1185 | add_section_address(elfcpp::DT tag, const Output_data* od) |
1186 | { this->add_entry(Dynamic_entry(tag, od, false)); } | |
a3ad94ed | 1187 | |
16649710 | 1188 | // Add a new dynamic entry with the size of output data. |
a3ad94ed | 1189 | void |
16649710 ILT |
1190 | add_section_size(elfcpp::DT tag, const Output_data* od) |
1191 | { this->add_entry(Dynamic_entry(tag, od, true)); } | |
a3ad94ed ILT |
1192 | |
1193 | // Add a new dynamic entry with the address of a symbol. | |
1194 | void | |
16649710 | 1195 | add_symbol(elfcpp::DT tag, const Symbol* sym) |
a3ad94ed ILT |
1196 | { this->add_entry(Dynamic_entry(tag, sym)); } |
1197 | ||
1198 | // Add a new dynamic entry with a string. | |
1199 | void | |
1200 | add_string(elfcpp::DT tag, const char* str) | |
cfd73a4e | 1201 | { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); } |
a3ad94ed | 1202 | |
41f542e7 ILT |
1203 | void |
1204 | add_string(elfcpp::DT tag, const std::string& str) | |
1205 | { this->add_string(tag, str.c_str()); } | |
1206 | ||
27bc2bce ILT |
1207 | protected: |
1208 | // Adjust the output section to set the entry size. | |
1209 | void | |
1210 | do_adjust_output_section(Output_section*); | |
1211 | ||
a3ad94ed ILT |
1212 | // Set the final data size. |
1213 | void | |
27bc2bce | 1214 | set_final_data_size(); |
a3ad94ed ILT |
1215 | |
1216 | // Write out the dynamic entries. | |
1217 | void | |
1218 | do_write(Output_file*); | |
1219 | ||
1220 | private: | |
1221 | // This POD class holds a single dynamic entry. | |
1222 | class Dynamic_entry | |
1223 | { | |
1224 | public: | |
1225 | // Create an entry with a fixed numeric value. | |
1226 | Dynamic_entry(elfcpp::DT tag, unsigned int val) | |
1227 | : tag_(tag), classification_(DYNAMIC_NUMBER) | |
1228 | { this->u_.val = val; } | |
1229 | ||
1230 | // Create an entry with the size or address of a section. | |
16649710 | 1231 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size) |
a3ad94ed ILT |
1232 | : tag_(tag), |
1233 | classification_(section_size | |
1234 | ? DYNAMIC_SECTION_SIZE | |
1235 | : DYNAMIC_SECTION_ADDRESS) | |
16649710 | 1236 | { this->u_.od = od; } |
a3ad94ed ILT |
1237 | |
1238 | // Create an entry with the address of a symbol. | |
16649710 | 1239 | Dynamic_entry(elfcpp::DT tag, const Symbol* sym) |
a3ad94ed ILT |
1240 | : tag_(tag), classification_(DYNAMIC_SYMBOL) |
1241 | { this->u_.sym = sym; } | |
1242 | ||
1243 | // Create an entry with a string. | |
1244 | Dynamic_entry(elfcpp::DT tag, const char* str) | |
1245 | : tag_(tag), classification_(DYNAMIC_STRING) | |
1246 | { this->u_.str = str; } | |
1247 | ||
1248 | // Write the dynamic entry to an output view. | |
1249 | template<int size, bool big_endian> | |
1250 | void | |
1ddbd1e6 | 1251 | write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const; |
a3ad94ed ILT |
1252 | |
1253 | private: | |
1254 | enum Classification | |
1255 | { | |
1256 | // Number. | |
1257 | DYNAMIC_NUMBER, | |
1258 | // Section address. | |
1259 | DYNAMIC_SECTION_ADDRESS, | |
1260 | // Section size. | |
1261 | DYNAMIC_SECTION_SIZE, | |
1262 | // Symbol adress. | |
1263 | DYNAMIC_SYMBOL, | |
1264 | // String. | |
1265 | DYNAMIC_STRING | |
1266 | }; | |
1267 | ||
1268 | union | |
1269 | { | |
1270 | // For DYNAMIC_NUMBER. | |
1271 | unsigned int val; | |
1272 | // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE. | |
16649710 | 1273 | const Output_data* od; |
a3ad94ed | 1274 | // For DYNAMIC_SYMBOL. |
16649710 | 1275 | const Symbol* sym; |
a3ad94ed ILT |
1276 | // For DYNAMIC_STRING. |
1277 | const char* str; | |
1278 | } u_; | |
1279 | // The dynamic tag. | |
1280 | elfcpp::DT tag_; | |
1281 | // The type of entry. | |
1282 | Classification classification_; | |
1283 | }; | |
1284 | ||
1285 | // Add an entry to the list. | |
1286 | void | |
1287 | add_entry(const Dynamic_entry& entry) | |
1288 | { this->entries_.push_back(entry); } | |
1289 | ||
1290 | // Sized version of write function. | |
1291 | template<int size, bool big_endian> | |
1292 | void | |
1293 | sized_write(Output_file* of); | |
1294 | ||
1295 | // The type of the list of entries. | |
1296 | typedef std::vector<Dynamic_entry> Dynamic_entries; | |
1297 | ||
a3ad94ed ILT |
1298 | // The entries. |
1299 | Dynamic_entries entries_; | |
1300 | // The pool used for strings. | |
1301 | Stringpool* pool_; | |
1302 | }; | |
1303 | ||
a2fb1b05 ILT |
1304 | // An output section. We don't expect to have too many output |
1305 | // sections, so we don't bother to do a template on the size. | |
1306 | ||
54dc6425 | 1307 | class Output_section : public Output_data |
a2fb1b05 ILT |
1308 | { |
1309 | public: | |
1310 | // Create an output section, giving the name, type, and flags. | |
b8e6aad9 | 1311 | Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword); |
54dc6425 | 1312 | virtual ~Output_section(); |
a2fb1b05 | 1313 | |
ead1e424 | 1314 | // Add a new input section SHNDX, named NAME, with header SHDR, from |
730cdc88 ILT |
1315 | // object OBJECT. RELOC_SHNDX is the index of a relocation section |
1316 | // which applies to this section, or 0 if none, or -1U if more than | |
1317 | // one. Return the offset within the output section. | |
a2fb1b05 ILT |
1318 | template<int size, bool big_endian> |
1319 | off_t | |
730cdc88 ILT |
1320 | add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx, |
1321 | const char *name, | |
1322 | const elfcpp::Shdr<size, big_endian>& shdr, | |
1323 | unsigned int reloc_shndx); | |
a2fb1b05 | 1324 | |
b8e6aad9 | 1325 | // Add generated data POSD to this output section. |
c06b7b0b | 1326 | void |
ead1e424 ILT |
1327 | add_output_section_data(Output_section_data* posd); |
1328 | ||
a2fb1b05 ILT |
1329 | // Return the section name. |
1330 | const char* | |
1331 | name() const | |
1332 | { return this->name_; } | |
1333 | ||
1334 | // Return the section type. | |
1335 | elfcpp::Elf_Word | |
1336 | type() const | |
1337 | { return this->type_; } | |
1338 | ||
1339 | // Return the section flags. | |
1340 | elfcpp::Elf_Xword | |
1341 | flags() const | |
1342 | { return this->flags_; } | |
1343 | ||
a3ad94ed ILT |
1344 | // Return the entsize field. |
1345 | uint64_t | |
1346 | entsize() const | |
1347 | { return this->entsize_; } | |
1348 | ||
61ba1cf9 ILT |
1349 | // Set the entsize field. |
1350 | void | |
16649710 | 1351 | set_entsize(uint64_t v); |
61ba1cf9 | 1352 | |
16649710 ILT |
1353 | // Set the link field to the output section index of a section. |
1354 | void | |
14b31740 | 1355 | set_link_section(const Output_data* od) |
16649710 ILT |
1356 | { |
1357 | gold_assert(this->link_ == 0 | |
1358 | && !this->should_link_to_symtab_ | |
1359 | && !this->should_link_to_dynsym_); | |
1360 | this->link_section_ = od; | |
1361 | } | |
1362 | ||
1363 | // Set the link field to a constant. | |
61ba1cf9 ILT |
1364 | void |
1365 | set_link(unsigned int v) | |
16649710 ILT |
1366 | { |
1367 | gold_assert(this->link_section_ == NULL | |
1368 | && !this->should_link_to_symtab_ | |
1369 | && !this->should_link_to_dynsym_); | |
1370 | this->link_ = v; | |
1371 | } | |
61ba1cf9 | 1372 | |
16649710 ILT |
1373 | // Record that this section should link to the normal symbol table. |
1374 | void | |
1375 | set_should_link_to_symtab() | |
1376 | { | |
1377 | gold_assert(this->link_section_ == NULL | |
1378 | && this->link_ == 0 | |
1379 | && !this->should_link_to_dynsym_); | |
1380 | this->should_link_to_symtab_ = true; | |
1381 | } | |
1382 | ||
1383 | // Record that this section should link to the dynamic symbol table. | |
1384 | void | |
1385 | set_should_link_to_dynsym() | |
1386 | { | |
1387 | gold_assert(this->link_section_ == NULL | |
1388 | && this->link_ == 0 | |
1389 | && !this->should_link_to_symtab_); | |
1390 | this->should_link_to_dynsym_ = true; | |
1391 | } | |
1392 | ||
1393 | // Return the info field. | |
1394 | unsigned int | |
1395 | info() const | |
1396 | { | |
1397 | gold_assert(this->info_section_ == NULL); | |
1398 | return this->info_; | |
1399 | } | |
1400 | ||
1401 | // Set the info field to the output section index of a section. | |
1402 | void | |
14b31740 | 1403 | set_info_section(const Output_data* od) |
16649710 ILT |
1404 | { |
1405 | gold_assert(this->info_ == 0); | |
1406 | this->info_section_ = od; | |
1407 | } | |
1408 | ||
1409 | // Set the info field to a constant. | |
61ba1cf9 ILT |
1410 | void |
1411 | set_info(unsigned int v) | |
16649710 ILT |
1412 | { |
1413 | gold_assert(this->info_section_ == NULL); | |
1414 | this->info_ = v; | |
1415 | } | |
61ba1cf9 ILT |
1416 | |
1417 | // Set the addralign field. | |
1418 | void | |
1419 | set_addralign(uint64_t v) | |
1420 | { this->addralign_ = v; } | |
1421 | ||
c06b7b0b ILT |
1422 | // Indicate that we need a symtab index. |
1423 | void | |
1424 | set_needs_symtab_index() | |
1425 | { this->needs_symtab_index_ = true; } | |
1426 | ||
1427 | // Return whether we need a symtab index. | |
1428 | bool | |
1429 | needs_symtab_index() const | |
1430 | { return this->needs_symtab_index_; } | |
1431 | ||
1432 | // Get the symtab index. | |
1433 | unsigned int | |
1434 | symtab_index() const | |
1435 | { | |
a3ad94ed | 1436 | gold_assert(this->symtab_index_ != 0); |
c06b7b0b ILT |
1437 | return this->symtab_index_; |
1438 | } | |
1439 | ||
1440 | // Set the symtab index. | |
1441 | void | |
1442 | set_symtab_index(unsigned int index) | |
1443 | { | |
a3ad94ed | 1444 | gold_assert(index != 0); |
c06b7b0b ILT |
1445 | this->symtab_index_ = index; |
1446 | } | |
1447 | ||
1448 | // Indicate that we need a dynsym index. | |
1449 | void | |
1450 | set_needs_dynsym_index() | |
1451 | { this->needs_dynsym_index_ = true; } | |
1452 | ||
1453 | // Return whether we need a dynsym index. | |
1454 | bool | |
1455 | needs_dynsym_index() const | |
1456 | { return this->needs_dynsym_index_; } | |
1457 | ||
1458 | // Get the dynsym index. | |
1459 | unsigned int | |
1460 | dynsym_index() const | |
1461 | { | |
a3ad94ed | 1462 | gold_assert(this->dynsym_index_ != 0); |
c06b7b0b ILT |
1463 | return this->dynsym_index_; |
1464 | } | |
1465 | ||
1466 | // Set the dynsym index. | |
1467 | void | |
1468 | set_dynsym_index(unsigned int index) | |
1469 | { | |
a3ad94ed | 1470 | gold_assert(index != 0); |
c06b7b0b ILT |
1471 | this->dynsym_index_ = index; |
1472 | } | |
1473 | ||
730cdc88 ILT |
1474 | // Return whether this section should be written after all the input |
1475 | // sections are complete. | |
1476 | bool | |
1477 | after_input_sections() const | |
1478 | { return this->after_input_sections_; } | |
1479 | ||
1480 | // Record that this section should be written after all the input | |
1481 | // sections are complete. | |
1482 | void | |
1483 | set_after_input_sections() | |
1484 | { this->after_input_sections_ = true; } | |
1485 | ||
27bc2bce ILT |
1486 | // Return whether this section requires postprocessing after all |
1487 | // relocations have been applied. | |
1488 | bool | |
1489 | requires_postprocessing() const | |
1490 | { return this->requires_postprocessing_; } | |
1491 | ||
1492 | // Record that this section requires postprocessing after all | |
1493 | // relocations have been applied. | |
1494 | void | |
1495 | set_requires_postprocessing() | |
1496 | { this->requires_postprocessing_ = true; } | |
1497 | ||
730cdc88 ILT |
1498 | // Return whether the offset OFFSET in the input section SHNDX in |
1499 | // object OBJECT is being included in the link. | |
1500 | bool | |
1501 | is_input_address_mapped(const Relobj* object, unsigned int shndx, | |
1502 | off_t offset) const; | |
1503 | ||
1504 | // Return the offset within the output section of OFFSET relative to | |
1505 | // the start of input section SHNDX in object OBJECT. | |
1506 | off_t | |
1507 | output_offset(const Relobj* object, unsigned int shndx, off_t offset) const; | |
1508 | ||
b8e6aad9 ILT |
1509 | // Return the output virtual address of OFFSET relative to the start |
1510 | // of input section SHNDX in object OBJECT. | |
1511 | uint64_t | |
1512 | output_address(const Relobj* object, unsigned int shndx, | |
1513 | off_t offset) const; | |
1514 | ||
27bc2bce ILT |
1515 | // Write the section header into *OPHDR. |
1516 | template<int size, bool big_endian> | |
1517 | void | |
1518 | write_header(const Layout*, const Stringpool*, | |
1519 | elfcpp::Shdr_write<size, big_endian>*) const; | |
1520 | ||
1521 | protected: | |
1522 | // Return the section index in the output file. | |
1523 | unsigned int | |
1524 | do_out_shndx() const | |
1525 | { | |
1526 | gold_assert(this->out_shndx_ != -1U); | |
1527 | return this->out_shndx_; | |
1528 | } | |
1529 | ||
1530 | // Set the output section index. | |
1531 | void | |
1532 | do_set_out_shndx(unsigned int shndx) | |
1533 | { | |
1534 | gold_assert(this->out_shndx_ == -1U); | |
1535 | this->out_shndx_ = shndx; | |
1536 | } | |
1537 | ||
1538 | // Set the final data size of the Output_section. For a typical | |
ead1e424 | 1539 | // Output_section, there is nothing to do, but if there are any |
27bc2bce | 1540 | // Output_section_data objects we need to set their final addresses |
ead1e424 ILT |
1541 | // here. |
1542 | void | |
27bc2bce | 1543 | set_final_data_size(); |
ead1e424 | 1544 | |
54dc6425 | 1545 | // Write the data to the file. For a typical Output_section, this |
ead1e424 ILT |
1546 | // does nothing: the data is written out by calling Object::Relocate |
1547 | // on each input object. But if there are any Output_section_data | |
1548 | // objects we do need to write them out here. | |
a3ad94ed | 1549 | void |
ead1e424 | 1550 | do_write(Output_file*); |
54dc6425 | 1551 | |
75f65a3e ILT |
1552 | // Return the address alignment--function required by parent class. |
1553 | uint64_t | |
1554 | do_addralign() const | |
1555 | { return this->addralign_; } | |
1556 | ||
1557 | // Return whether this is an Output_section. | |
1558 | bool | |
1559 | do_is_section() const | |
1560 | { return true; } | |
1561 | ||
54dc6425 ILT |
1562 | // Return whether this is a section of the specified type. |
1563 | bool | |
75f65a3e | 1564 | do_is_section_type(elfcpp::Elf_Word type) const |
54dc6425 ILT |
1565 | { return this->type_ == type; } |
1566 | ||
1567 | // Return whether the specified section flag is set. | |
1568 | bool | |
75f65a3e | 1569 | do_is_section_flag_set(elfcpp::Elf_Xword flag) const |
54dc6425 ILT |
1570 | { return (this->flags_ & flag) != 0; } |
1571 | ||
a2fb1b05 | 1572 | private: |
ead1e424 ILT |
1573 | // In some cases we need to keep a list of the input sections |
1574 | // associated with this output section. We only need the list if we | |
1575 | // might have to change the offsets of the input section within the | |
1576 | // output section after we add the input section. The ordinary | |
1577 | // input sections will be written out when we process the object | |
1578 | // file, and as such we don't need to track them here. We do need | |
1579 | // to track Output_section_data objects here. We store instances of | |
1580 | // this structure in a std::vector, so it must be a POD. There can | |
1581 | // be many instances of this structure, so we use a union to save | |
1582 | // some space. | |
1583 | class Input_section | |
1584 | { | |
1585 | public: | |
1586 | Input_section() | |
b8e6aad9 ILT |
1587 | : shndx_(0), p2align_(0) |
1588 | { | |
1589 | this->u1_.data_size = 0; | |
1590 | this->u2_.object = NULL; | |
1591 | } | |
ead1e424 | 1592 | |
b8e6aad9 | 1593 | // For an ordinary input section. |
f6ce93d6 | 1594 | Input_section(Relobj* object, unsigned int shndx, off_t data_size, |
ead1e424 ILT |
1595 | uint64_t addralign) |
1596 | : shndx_(shndx), | |
b8e6aad9 | 1597 | p2align_(ffsll(static_cast<long long>(addralign))) |
ead1e424 | 1598 | { |
b8e6aad9 ILT |
1599 | gold_assert(shndx != OUTPUT_SECTION_CODE |
1600 | && shndx != MERGE_DATA_SECTION_CODE | |
1601 | && shndx != MERGE_STRING_SECTION_CODE); | |
1602 | this->u1_.data_size = data_size; | |
1603 | this->u2_.object = object; | |
ead1e424 ILT |
1604 | } |
1605 | ||
b8e6aad9 | 1606 | // For a non-merge output section. |
ead1e424 | 1607 | Input_section(Output_section_data* posd) |
b8e6aad9 ILT |
1608 | : shndx_(OUTPUT_SECTION_CODE), |
1609 | p2align_(ffsll(static_cast<long long>(posd->addralign()))) | |
1610 | { | |
1611 | this->u1_.data_size = 0; | |
1612 | this->u2_.posd = posd; | |
1613 | } | |
1614 | ||
1615 | // For a merge section. | |
1616 | Input_section(Output_section_data* posd, bool is_string, uint64_t entsize) | |
1617 | : shndx_(is_string | |
1618 | ? MERGE_STRING_SECTION_CODE | |
1619 | : MERGE_DATA_SECTION_CODE), | |
1620 | p2align_(ffsll(static_cast<long long>(posd->addralign()))) | |
1621 | { | |
1622 | this->u1_.entsize = entsize; | |
1623 | this->u2_.posd = posd; | |
1624 | } | |
ead1e424 ILT |
1625 | |
1626 | // The required alignment. | |
1627 | uint64_t | |
1628 | addralign() const | |
a3ad94ed ILT |
1629 | { |
1630 | return (this->p2align_ == 0 | |
1631 | ? 0 | |
1632 | : static_cast<uint64_t>(1) << (this->p2align_ - 1)); | |
1633 | } | |
ead1e424 ILT |
1634 | |
1635 | // Return the required size. | |
1636 | off_t | |
1637 | data_size() const; | |
1638 | ||
b8e6aad9 ILT |
1639 | // Return whether this is a merge section which matches the |
1640 | // parameters. | |
1641 | bool | |
87f95776 ILT |
1642 | is_merge_section(bool is_string, uint64_t entsize, |
1643 | uint64_t addralign) const | |
b8e6aad9 ILT |
1644 | { |
1645 | return (this->shndx_ == (is_string | |
1646 | ? MERGE_STRING_SECTION_CODE | |
1647 | : MERGE_DATA_SECTION_CODE) | |
87f95776 ILT |
1648 | && this->u1_.entsize == entsize |
1649 | && this->addralign() == addralign); | |
b8e6aad9 ILT |
1650 | } |
1651 | ||
1652 | // Set the output section. | |
1653 | void | |
1654 | set_output_section(Output_section* os) | |
1655 | { | |
1656 | gold_assert(!this->is_input_section()); | |
1657 | this->u2_.posd->set_output_section(os); | |
1658 | } | |
1659 | ||
ead1e424 ILT |
1660 | // Set the address and file offset. This is called during |
1661 | // Layout::finalize. SECOFF is the file offset of the enclosing | |
1662 | // section. | |
1663 | void | |
1664 | set_address(uint64_t addr, off_t off, off_t secoff); | |
1665 | ||
b8e6aad9 ILT |
1666 | // Add an input section, for SHF_MERGE sections. |
1667 | bool | |
1668 | add_input_section(Relobj* object, unsigned int shndx) | |
1669 | { | |
1670 | gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE | |
1671 | || this->shndx_ == MERGE_STRING_SECTION_CODE); | |
1672 | return this->u2_.posd->add_input_section(object, shndx); | |
1673 | } | |
1674 | ||
1675 | // Given an input OBJECT, an input section index SHNDX within that | |
1676 | // object, and an OFFSET relative to the start of that input | |
730cdc88 ILT |
1677 | // section, return whether or not the output offset is known. If |
1678 | // this function returns true, it sets *POUTPUT to the output | |
1679 | // offset. | |
b8e6aad9 | 1680 | bool |
730cdc88 ILT |
1681 | output_offset(const Relobj* object, unsigned int shndx, off_t offset, |
1682 | off_t *poutput) const; | |
b8e6aad9 | 1683 | |
ead1e424 ILT |
1684 | // Write out the data. This does nothing for an input section. |
1685 | void | |
1686 | write(Output_file*); | |
1687 | ||
1688 | private: | |
b8e6aad9 ILT |
1689 | // Code values which appear in shndx_. If the value is not one of |
1690 | // these codes, it is the input section index in the object file. | |
1691 | enum | |
1692 | { | |
1693 | // An Output_section_data. | |
1694 | OUTPUT_SECTION_CODE = -1U, | |
1695 | // An Output_section_data for an SHF_MERGE section with | |
1696 | // SHF_STRINGS not set. | |
1697 | MERGE_DATA_SECTION_CODE = -2U, | |
1698 | // An Output_section_data for an SHF_MERGE section with | |
1699 | // SHF_STRINGS set. | |
1700 | MERGE_STRING_SECTION_CODE = -3U | |
1701 | }; | |
1702 | ||
ead1e424 ILT |
1703 | // Whether this is an input section. |
1704 | bool | |
1705 | is_input_section() const | |
b8e6aad9 ILT |
1706 | { |
1707 | return (this->shndx_ != OUTPUT_SECTION_CODE | |
1708 | && this->shndx_ != MERGE_DATA_SECTION_CODE | |
1709 | && this->shndx_ != MERGE_STRING_SECTION_CODE); | |
1710 | } | |
ead1e424 | 1711 | |
b8e6aad9 ILT |
1712 | // For an ordinary input section, this is the section index in the |
1713 | // input file. For an Output_section_data, this is | |
1714 | // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or | |
1715 | // MERGE_STRING_SECTION_CODE. | |
ead1e424 ILT |
1716 | unsigned int shndx_; |
1717 | // The required alignment, stored as a power of 2. | |
1718 | unsigned int p2align_; | |
ead1e424 ILT |
1719 | union |
1720 | { | |
b8e6aad9 ILT |
1721 | // For an ordinary input section, the section size. |
1722 | off_t data_size; | |
1723 | // For OUTPUT_SECTION_CODE, this is not used. For | |
1724 | // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the | |
1725 | // entity size. | |
1726 | uint64_t entsize; | |
1727 | } u1_; | |
1728 | union | |
1729 | { | |
1730 | // For an ordinary input section, the object which holds the | |
ead1e424 | 1731 | // input section. |
f6ce93d6 | 1732 | Relobj* object; |
b8e6aad9 ILT |
1733 | // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or |
1734 | // MERGE_STRING_SECTION_CODE, the data. | |
ead1e424 | 1735 | Output_section_data* posd; |
b8e6aad9 | 1736 | } u2_; |
ead1e424 ILT |
1737 | }; |
1738 | ||
1739 | typedef std::vector<Input_section> Input_section_list; | |
1740 | ||
c51e6221 ILT |
1741 | // Fill data. This is used to fill in data between input sections. |
1742 | // When we have to keep track of the input sections, we can use an | |
1743 | // Output_data_const, but we don't want to have to keep track of | |
1744 | // input sections just to implement fills. For a fill we record the | |
1745 | // offset, and the actual data to be written out. | |
1746 | class Fill | |
1747 | { | |
1748 | public: | |
1749 | Fill(off_t section_offset, off_t length) | |
1750 | : section_offset_(section_offset), length_(length) | |
1751 | { } | |
1752 | ||
1753 | // Return section offset. | |
1754 | off_t | |
1755 | section_offset() const | |
1756 | { return this->section_offset_; } | |
1757 | ||
1758 | // Return fill length. | |
1759 | off_t | |
1760 | length() const | |
1761 | { return this->length_; } | |
1762 | ||
1763 | private: | |
1764 | // The offset within the output section. | |
1765 | off_t section_offset_; | |
1766 | // The length of the space to fill. | |
1767 | off_t length_; | |
1768 | }; | |
1769 | ||
1770 | typedef std::vector<Fill> Fill_list; | |
1771 | ||
b8e6aad9 ILT |
1772 | // Add a new output section by Input_section. |
1773 | void | |
1774 | add_output_section_data(Input_section*); | |
1775 | ||
1776 | // Add an SHF_MERGE input section. Returns true if the section was | |
1777 | // handled. | |
1778 | bool | |
1779 | add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags, | |
1780 | uint64_t entsize, uint64_t addralign); | |
1781 | ||
1782 | // Add an output SHF_MERGE section POSD to this output section. | |
1783 | // IS_STRING indicates whether it is a SHF_STRINGS section, and | |
1784 | // ENTSIZE is the entity size. This returns the entry added to | |
1785 | // input_sections_. | |
1786 | void | |
1787 | add_output_merge_section(Output_section_data* posd, bool is_string, | |
1788 | uint64_t entsize); | |
1789 | ||
a2fb1b05 ILT |
1790 | // Most of these fields are only valid after layout. |
1791 | ||
1792 | // The name of the section. This will point into a Stringpool. | |
27bc2bce | 1793 | const char* const name_; |
75f65a3e | 1794 | // The section address is in the parent class. |
a2fb1b05 ILT |
1795 | // The section alignment. |
1796 | uint64_t addralign_; | |
1797 | // The section entry size. | |
1798 | uint64_t entsize_; | |
75f65a3e | 1799 | // The file offset is in the parent class. |
16649710 | 1800 | // Set the section link field to the index of this section. |
14b31740 | 1801 | const Output_data* link_section_; |
16649710 | 1802 | // If link_section_ is NULL, this is the link field. |
a2fb1b05 | 1803 | unsigned int link_; |
16649710 | 1804 | // Set the section info field to the index of this section. |
14b31740 | 1805 | const Output_data* info_section_; |
16649710 | 1806 | // If info_section_ is NULL, this is the section info field. |
a2fb1b05 ILT |
1807 | unsigned int info_; |
1808 | // The section type. | |
27bc2bce | 1809 | const elfcpp::Elf_Word type_; |
a2fb1b05 | 1810 | // The section flags. |
27bc2bce | 1811 | const elfcpp::Elf_Xword flags_; |
61ba1cf9 | 1812 | // The section index. |
ead1e424 | 1813 | unsigned int out_shndx_; |
c06b7b0b ILT |
1814 | // If there is a STT_SECTION for this output section in the normal |
1815 | // symbol table, this is the symbol index. This starts out as zero. | |
1816 | // It is initialized in Layout::finalize() to be the index, or -1U | |
1817 | // if there isn't one. | |
1818 | unsigned int symtab_index_; | |
1819 | // If there is a STT_SECTION for this output section in the dynamic | |
1820 | // symbol table, this is the symbol index. This starts out as zero. | |
1821 | // It is initialized in Layout::finalize() to be the index, or -1U | |
1822 | // if there isn't one. | |
1823 | unsigned int dynsym_index_; | |
ead1e424 ILT |
1824 | // The input sections. This will be empty in cases where we don't |
1825 | // need to keep track of them. | |
1826 | Input_section_list input_sections_; | |
1827 | // The offset of the first entry in input_sections_. | |
1828 | off_t first_input_offset_; | |
c51e6221 ILT |
1829 | // The fill data. This is separate from input_sections_ because we |
1830 | // often will need fill sections without needing to keep track of | |
1831 | // input sections. | |
1832 | Fill_list fills_; | |
c06b7b0b ILT |
1833 | // Whether this output section needs a STT_SECTION symbol in the |
1834 | // normal symbol table. This will be true if there is a relocation | |
1835 | // which needs it. | |
1836 | bool needs_symtab_index_ : 1; | |
1837 | // Whether this output section needs a STT_SECTION symbol in the | |
1838 | // dynamic symbol table. This will be true if there is a dynamic | |
1839 | // relocation which needs it. | |
1840 | bool needs_dynsym_index_ : 1; | |
16649710 ILT |
1841 | // Whether the link field of this output section should point to the |
1842 | // normal symbol table. | |
1843 | bool should_link_to_symtab_ : 1; | |
1844 | // Whether the link field of this output section should point to the | |
1845 | // dynamic symbol table. | |
1846 | bool should_link_to_dynsym_ : 1; | |
730cdc88 ILT |
1847 | // Whether this section should be written after all the input |
1848 | // sections are complete. | |
1849 | bool after_input_sections_ : 1; | |
27bc2bce ILT |
1850 | // Whether this section requires post processing after all |
1851 | // relocations have been applied. | |
1852 | bool requires_postprocessing_ : 1; | |
a2fb1b05 ILT |
1853 | }; |
1854 | ||
1855 | // An output segment. PT_LOAD segments are built from collections of | |
1856 | // output sections. Other segments typically point within PT_LOAD | |
1857 | // segments, and are built directly as needed. | |
1858 | ||
1859 | class Output_segment | |
1860 | { | |
1861 | public: | |
1862 | // Create an output segment, specifying the type and flags. | |
1863 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); | |
1864 | ||
1865 | // Return the virtual address. | |
1866 | uint64_t | |
1867 | vaddr() const | |
1868 | { return this->vaddr_; } | |
1869 | ||
1870 | // Return the physical address. | |
1871 | uint64_t | |
1872 | paddr() const | |
1873 | { return this->paddr_; } | |
1874 | ||
1875 | // Return the segment type. | |
1876 | elfcpp::Elf_Word | |
1877 | type() const | |
1878 | { return this->type_; } | |
1879 | ||
1880 | // Return the segment flags. | |
1881 | elfcpp::Elf_Word | |
1882 | flags() const | |
1883 | { return this->flags_; } | |
1884 | ||
92e059d8 ILT |
1885 | // Return the memory size. |
1886 | uint64_t | |
1887 | memsz() const | |
1888 | { return this->memsz_; } | |
1889 | ||
ead1e424 ILT |
1890 | // Return the file size. |
1891 | off_t | |
1892 | filesz() const | |
1893 | { return this->filesz_; } | |
1894 | ||
75f65a3e ILT |
1895 | // Return the maximum alignment of the Output_data. |
1896 | uint64_t | |
ead1e424 | 1897 | addralign(); |
75f65a3e | 1898 | |
a2fb1b05 ILT |
1899 | // Add an Output_section to this segment. |
1900 | void | |
dbe717ef ILT |
1901 | add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) |
1902 | { this->add_output_section(os, seg_flags, false); } | |
1903 | ||
1904 | // Add an Output_section to the start of this segment. | |
1905 | void | |
1906 | add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) | |
1907 | { this->add_output_section(os, seg_flags, true); } | |
75f65a3e ILT |
1908 | |
1909 | // Add an Output_data (which is not an Output_section) to the start | |
1910 | // of this segment. | |
1911 | void | |
1912 | add_initial_output_data(Output_data*); | |
1913 | ||
4f4c5f80 ILT |
1914 | // Return the number of dynamic relocations applied to this segment. |
1915 | unsigned int | |
1916 | dynamic_reloc_count() const; | |
1917 | ||
75f65a3e ILT |
1918 | // Set the address of the segment to ADDR and the offset to *POFF |
1919 | // (aligned if necessary), and set the addresses and offsets of all | |
ead1e424 ILT |
1920 | // contained output sections accordingly. Set the section indexes |
1921 | // of all contained output sections starting with *PSHNDX. Return | |
1922 | // the address of the immediately following segment. Update *POFF | |
1923 | // and *PSHNDX. This should only be called for a PT_LOAD segment. | |
75f65a3e | 1924 | uint64_t |
ead1e424 | 1925 | set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx); |
75f65a3e | 1926 | |
0496d5e5 ILT |
1927 | // Set the minimum alignment of this segment. This may be adjusted |
1928 | // upward based on the section alignments. | |
1929 | void | |
1930 | set_minimum_addralign(uint64_t align) | |
1931 | { | |
1932 | gold_assert(!this->is_align_known_); | |
1933 | this->align_ = align; | |
1934 | } | |
1935 | ||
75f65a3e ILT |
1936 | // Set the offset of this segment based on the section. This should |
1937 | // only be called for a non-PT_LOAD segment. | |
1938 | void | |
1939 | set_offset(); | |
1940 | ||
1941 | // Return the number of output sections. | |
1942 | unsigned int | |
1943 | output_section_count() const; | |
a2fb1b05 | 1944 | |
61ba1cf9 ILT |
1945 | // Write the segment header into *OPHDR. |
1946 | template<int size, bool big_endian> | |
1947 | void | |
ead1e424 | 1948 | write_header(elfcpp::Phdr_write<size, big_endian>*); |
61ba1cf9 ILT |
1949 | |
1950 | // Write the section headers of associated sections into V. | |
1951 | template<int size, bool big_endian> | |
1952 | unsigned char* | |
16649710 | 1953 | write_section_headers(const Layout*, const Stringpool*, unsigned char* v, |
ead1e424 | 1954 | unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const; |
61ba1cf9 | 1955 | |
a2fb1b05 ILT |
1956 | private: |
1957 | Output_segment(const Output_segment&); | |
1958 | Output_segment& operator=(const Output_segment&); | |
1959 | ||
54dc6425 | 1960 | typedef std::list<Output_data*> Output_data_list; |
a2fb1b05 | 1961 | |
dbe717ef ILT |
1962 | // Add an Output_section to this segment, specifying front or back. |
1963 | void | |
1964 | add_output_section(Output_section*, elfcpp::Elf_Word seg_flags, | |
1965 | bool front); | |
1966 | ||
ead1e424 ILT |
1967 | // Find the maximum alignment in an Output_data_list. |
1968 | static uint64_t | |
1969 | maximum_alignment(const Output_data_list*); | |
1970 | ||
75f65a3e ILT |
1971 | // Set the section addresses in an Output_data_list. |
1972 | uint64_t | |
ead1e424 ILT |
1973 | set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff, |
1974 | unsigned int* pshndx); | |
75f65a3e ILT |
1975 | |
1976 | // Return the number of Output_sections in an Output_data_list. | |
1977 | unsigned int | |
1978 | output_section_count_list(const Output_data_list*) const; | |
1979 | ||
4f4c5f80 ILT |
1980 | // Return the number of dynamic relocs in an Output_data_list. |
1981 | unsigned int | |
1982 | dynamic_reloc_count_list(const Output_data_list*) const; | |
1983 | ||
61ba1cf9 ILT |
1984 | // Write the section headers in the list into V. |
1985 | template<int size, bool big_endian> | |
1986 | unsigned char* | |
16649710 ILT |
1987 | write_section_headers_list(const Layout*, const Stringpool*, |
1988 | const Output_data_list*, unsigned char* v, | |
ead1e424 | 1989 | unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const; |
61ba1cf9 | 1990 | |
75f65a3e | 1991 | // The list of output data with contents attached to this segment. |
54dc6425 | 1992 | Output_data_list output_data_; |
75f65a3e ILT |
1993 | // The list of output data without contents attached to this segment. |
1994 | Output_data_list output_bss_; | |
a2fb1b05 ILT |
1995 | // The segment virtual address. |
1996 | uint64_t vaddr_; | |
1997 | // The segment physical address. | |
1998 | uint64_t paddr_; | |
1999 | // The size of the segment in memory. | |
2000 | uint64_t memsz_; | |
0496d5e5 ILT |
2001 | // The segment alignment. The is_align_known_ field indicates |
2002 | // whether this has been finalized. It can be set to a minimum | |
2003 | // value before it is finalized. | |
a2fb1b05 ILT |
2004 | uint64_t align_; |
2005 | // The offset of the segment data within the file. | |
2006 | off_t offset_; | |
2007 | // The size of the segment data in the file. | |
2008 | off_t filesz_; | |
2009 | // The segment type; | |
2010 | elfcpp::Elf_Word type_; | |
2011 | // The segment flags. | |
2012 | elfcpp::Elf_Word flags_; | |
0496d5e5 | 2013 | // Whether we have finalized align_. |
ead1e424 | 2014 | bool is_align_known_; |
a2fb1b05 ILT |
2015 | }; |
2016 | ||
61ba1cf9 | 2017 | // This class represents the output file. |
a2fb1b05 ILT |
2018 | |
2019 | class Output_file | |
2020 | { | |
2021 | public: | |
c51e6221 ILT |
2022 | Output_file(const General_options& options, Target*); |
2023 | ||
2024 | // Get a pointer to the target. | |
2025 | Target* | |
2026 | target() const | |
2027 | { return this->target_; } | |
61ba1cf9 ILT |
2028 | |
2029 | // Open the output file. FILE_SIZE is the final size of the file. | |
2030 | void | |
2031 | open(off_t file_size); | |
2032 | ||
27bc2bce ILT |
2033 | // Resize the output file. |
2034 | void | |
2035 | resize(off_t file_size); | |
2036 | ||
61ba1cf9 ILT |
2037 | // Close the output file and make sure there are no error. |
2038 | void | |
2039 | close(); | |
2040 | ||
2041 | // We currently always use mmap which makes the view handling quite | |
2042 | // simple. In the future we may support other approaches. | |
a2fb1b05 ILT |
2043 | |
2044 | // Write data to the output file. | |
2045 | void | |
61ba1cf9 ILT |
2046 | write(off_t offset, const void* data, off_t len) |
2047 | { memcpy(this->base_ + offset, data, len); } | |
2048 | ||
2049 | // Get a buffer to use to write to the file, given the offset into | |
2050 | // the file and the size. | |
2051 | unsigned char* | |
2052 | get_output_view(off_t start, off_t size) | |
2053 | { | |
a3ad94ed | 2054 | gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_); |
61ba1cf9 ILT |
2055 | return this->base_ + start; |
2056 | } | |
2057 | ||
2058 | // VIEW must have been returned by get_output_view. Write the | |
2059 | // buffer to the file, passing in the offset and the size. | |
2060 | void | |
2061 | write_output_view(off_t, off_t, unsigned char*) | |
2062 | { } | |
2063 | ||
730cdc88 ILT |
2064 | // Get a read/write buffer. This is used when we want to write part |
2065 | // of the file, read it in, and write it again. | |
2066 | unsigned char* | |
2067 | get_input_output_view(off_t start, off_t size) | |
2068 | { return this->get_output_view(start, size); } | |
2069 | ||
2070 | // Write a read/write buffer back to the file. | |
2071 | void | |
2072 | write_input_output_view(off_t, off_t, unsigned char*) | |
2073 | { } | |
2074 | ||
2075 | // Get a read buffer. This is used when we just want to read part | |
2076 | // of the file back it in. | |
2077 | const unsigned char* | |
2078 | get_input_view(off_t start, off_t size) | |
2079 | { return this->get_output_view(start, size); } | |
2080 | ||
2081 | // Release a read bfufer. | |
2082 | void | |
2083 | free_input_view(off_t, off_t, const unsigned char*) | |
2084 | { } | |
2085 | ||
61ba1cf9 | 2086 | private: |
27bc2bce ILT |
2087 | // Map the file into memory. |
2088 | void | |
2089 | map(); | |
2090 | ||
61ba1cf9 ILT |
2091 | // General options. |
2092 | const General_options& options_; | |
c51e6221 ILT |
2093 | // Target. |
2094 | Target* target_; | |
61ba1cf9 ILT |
2095 | // File name. |
2096 | const char* name_; | |
2097 | // File descriptor. | |
2098 | int o_; | |
2099 | // File size. | |
2100 | off_t file_size_; | |
2101 | // Base of file mapped into memory. | |
2102 | unsigned char* base_; | |
a2fb1b05 ILT |
2103 | }; |
2104 | ||
2105 | } // End namespace gold. | |
2106 | ||
2107 | #endif // !defined(GOLD_OUTPUT_H) |