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 | ||
9a0910c3 ILT |
435 | // This class may change the output section name. This is called |
436 | // right before shstrtab is written, so after all input-section | |
437 | // layout processing is done. The input is the old name, and the | |
438 | // output should be a new name (which will be copied into permanent | |
439 | // storage) to change the name, or NULL to keep the name as-is. | |
440 | virtual const char* | |
441 | do_modified_output_section_name(const char*) | |
442 | { return NULL; } | |
443 | ||
b8e6aad9 ILT |
444 | // Given an input OBJECT, an input section index SHNDX within that |
445 | // object, and an OFFSET relative to the start of that input | |
730cdc88 ILT |
446 | // section, return whether or not the corresponding offset within |
447 | // the output section is known. If this function returns true, it | |
448 | // sets *POUTPUT to the output offset. The value -1 indicates that | |
449 | // this input offset is being discarded. | |
b8e6aad9 | 450 | virtual bool |
730cdc88 ILT |
451 | output_offset(const Relobj* object, unsigned int shndx, off_t offset, |
452 | off_t *poutput) const | |
453 | { return this->do_output_offset(object, shndx, offset, poutput); } | |
b8e6aad9 | 454 | |
ead1e424 ILT |
455 | protected: |
456 | // The child class must implement do_write. | |
457 | ||
16649710 ILT |
458 | // The child class may implement specific adjustments to the output |
459 | // section. | |
460 | virtual void | |
461 | do_adjust_output_section(Output_section*) | |
462 | { } | |
463 | ||
b8e6aad9 ILT |
464 | // May be implemented by child class. Return true if the section |
465 | // was handled. | |
466 | virtual bool | |
467 | do_add_input_section(Relobj*, unsigned int) | |
468 | { gold_unreachable(); } | |
469 | ||
730cdc88 | 470 | // The child class may implement output_offset. |
b8e6aad9 | 471 | virtual bool |
730cdc88 | 472 | do_output_offset(const Relobj*, unsigned int, off_t, off_t*) const |
b8e6aad9 ILT |
473 | { return false; } |
474 | ||
ead1e424 ILT |
475 | // Return the required alignment. |
476 | uint64_t | |
477 | do_addralign() const | |
478 | { return this->addralign_; } | |
479 | ||
480 | // Return the section index of the output section. | |
481 | unsigned int | |
482 | do_out_shndx() const; | |
483 | ||
5a6f7e2d ILT |
484 | // Set the alignment. |
485 | void | |
486 | set_addralign(uint64_t addralign) | |
487 | { this->addralign_ = addralign; } | |
488 | ||
ead1e424 ILT |
489 | private: |
490 | // The output section for this section. | |
491 | const Output_section* output_section_; | |
492 | // The required alignment. | |
493 | uint64_t addralign_; | |
494 | }; | |
495 | ||
27bc2bce ILT |
496 | // Some Output_section_data classes build up their data step by step, |
497 | // rather than all at once. This class provides an interface for | |
498 | // them. | |
499 | ||
500 | class Output_section_data_build : public Output_section_data | |
501 | { | |
502 | public: | |
503 | Output_section_data_build(uint64_t addralign) | |
504 | : Output_section_data(addralign) | |
505 | { } | |
506 | ||
507 | // Get the current data size. | |
508 | off_t | |
509 | current_data_size() const | |
510 | { return this->current_data_size_for_child(); } | |
511 | ||
512 | // Set the current data size. | |
513 | void | |
514 | set_current_data_size(off_t data_size) | |
515 | { this->set_current_data_size_for_child(data_size); } | |
516 | ||
517 | protected: | |
518 | // Set the final data size. | |
519 | virtual void | |
520 | set_final_data_size() | |
521 | { this->set_data_size(this->current_data_size_for_child()); } | |
522 | }; | |
523 | ||
dbe717ef ILT |
524 | // A simple case of Output_data in which we have constant data to |
525 | // output. | |
ead1e424 | 526 | |
dbe717ef | 527 | class Output_data_const : public Output_section_data |
ead1e424 ILT |
528 | { |
529 | public: | |
dbe717ef ILT |
530 | Output_data_const(const std::string& data, uint64_t addralign) |
531 | : Output_section_data(data.size(), addralign), data_(data) | |
532 | { } | |
533 | ||
534 | Output_data_const(const char* p, off_t len, uint64_t addralign) | |
535 | : Output_section_data(len, addralign), data_(p, len) | |
536 | { } | |
537 | ||
538 | Output_data_const(const unsigned char* p, off_t len, uint64_t addralign) | |
539 | : Output_section_data(len, addralign), | |
540 | data_(reinterpret_cast<const char*>(p), len) | |
541 | { } | |
542 | ||
27bc2bce | 543 | protected: |
a3ad94ed | 544 | // Write the data to the output file. |
dbe717ef | 545 | void |
a3ad94ed | 546 | do_write(Output_file*); |
dbe717ef ILT |
547 | |
548 | private: | |
549 | std::string data_; | |
550 | }; | |
551 | ||
a3ad94ed ILT |
552 | // Another version of Output_data with constant data, in which the |
553 | // buffer is allocated by the caller. | |
dbe717ef | 554 | |
a3ad94ed | 555 | class Output_data_const_buffer : public Output_section_data |
dbe717ef ILT |
556 | { |
557 | public: | |
a3ad94ed ILT |
558 | Output_data_const_buffer(const unsigned char* p, off_t len, |
559 | uint64_t addralign) | |
560 | : Output_section_data(len, addralign), p_(p) | |
561 | { } | |
562 | ||
27bc2bce | 563 | protected: |
a3ad94ed ILT |
564 | // Write the data the output file. |
565 | void | |
566 | do_write(Output_file*); | |
567 | ||
568 | private: | |
569 | const unsigned char* p_; | |
570 | }; | |
571 | ||
27bc2bce ILT |
572 | // A place holder for a fixed amount of data written out via some |
573 | // other mechanism. | |
a3ad94ed | 574 | |
27bc2bce | 575 | class Output_data_fixed_space : public Output_section_data |
a3ad94ed ILT |
576 | { |
577 | public: | |
27bc2bce | 578 | Output_data_fixed_space(off_t data_size, uint64_t addralign) |
a3ad94ed ILT |
579 | : Output_section_data(data_size, addralign) |
580 | { } | |
581 | ||
27bc2bce ILT |
582 | protected: |
583 | // Write out the data--the actual data must be written out | |
584 | // elsewhere. | |
585 | void | |
586 | do_write(Output_file*) | |
ead1e424 | 587 | { } |
27bc2bce | 588 | }; |
ead1e424 | 589 | |
27bc2bce ILT |
590 | // A place holder for variable sized data written out via some other |
591 | // mechanism. | |
592 | ||
593 | class Output_data_space : public Output_section_data_build | |
594 | { | |
595 | public: | |
596 | explicit Output_data_space(uint64_t addralign) | |
597 | : Output_section_data_build(addralign) | |
598 | { } | |
ead1e424 | 599 | |
5a6f7e2d ILT |
600 | // Set the alignment. |
601 | void | |
602 | set_space_alignment(uint64_t align) | |
603 | { this->set_addralign(align); } | |
604 | ||
27bc2bce ILT |
605 | protected: |
606 | // Write out the data--the actual data must be written out | |
607 | // elsewhere. | |
ead1e424 ILT |
608 | void |
609 | do_write(Output_file*) | |
610 | { } | |
611 | }; | |
612 | ||
a3ad94ed ILT |
613 | // A string table which goes into an output section. |
614 | ||
615 | class Output_data_strtab : public Output_section_data | |
616 | { | |
617 | public: | |
618 | Output_data_strtab(Stringpool* strtab) | |
619 | : Output_section_data(1), strtab_(strtab) | |
620 | { } | |
621 | ||
27bc2bce | 622 | protected: |
a3ad94ed ILT |
623 | // This is called to set the address and file offset. Here we make |
624 | // sure that the Stringpool is finalized. | |
625 | void | |
27bc2bce | 626 | set_final_data_size(); |
a3ad94ed ILT |
627 | |
628 | // Write out the data. | |
629 | void | |
630 | do_write(Output_file*); | |
631 | ||
632 | private: | |
633 | Stringpool* strtab_; | |
634 | }; | |
635 | ||
c06b7b0b ILT |
636 | // This POD class is used to represent a single reloc in the output |
637 | // file. This could be a private class within Output_data_reloc, but | |
638 | // the templatization is complex enough that I broke it out into a | |
639 | // separate class. The class is templatized on either elfcpp::SHT_REL | |
640 | // or elfcpp::SHT_RELA, and also on whether this is a dynamic | |
641 | // relocation or an ordinary relocation. | |
642 | ||
643 | // A relocation can be against a global symbol, a local symbol, an | |
644 | // output section, or the undefined symbol at index 0. We represent | |
645 | // the latter by using a NULL global symbol. | |
646 | ||
647 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
648 | class Output_reloc; | |
649 | ||
650 | template<bool dynamic, int size, bool big_endian> | |
651 | class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
652 | { | |
653 | public: | |
654 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
655 | ||
656 | // An uninitialized entry. We need this because we want to put | |
657 | // instances of this class into an STL container. | |
658 | Output_reloc() | |
659 | : local_sym_index_(INVALID_CODE) | |
660 | { } | |
661 | ||
662 | // A reloc against a global symbol. | |
5a6f7e2d | 663 | |
a3ad94ed ILT |
664 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
665 | Address address) | |
5a6f7e2d ILT |
666 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), |
667 | shndx_(INVALID_CODE) | |
668 | { | |
669 | this->u1_.gsym = gsym; | |
670 | this->u2_.od = od; | |
671 | } | |
672 | ||
673 | Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj, | |
674 | unsigned int shndx, Address address) | |
675 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), | |
676 | shndx_(shndx) | |
677 | { | |
678 | gold_assert(shndx != INVALID_CODE); | |
679 | this->u1_.gsym = gsym; | |
680 | this->u2_.relobj = relobj; | |
681 | } | |
c06b7b0b ILT |
682 | |
683 | // A reloc against a local symbol. | |
5a6f7e2d ILT |
684 | |
685 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
c06b7b0b | 686 | unsigned int local_sym_index, |
a3ad94ed ILT |
687 | unsigned int type, |
688 | Output_data* od, | |
689 | Address address) | |
5a6f7e2d ILT |
690 | : address_(address), local_sym_index_(local_sym_index), type_(type), |
691 | shndx_(INVALID_CODE) | |
c06b7b0b | 692 | { |
a3ad94ed ILT |
693 | gold_assert(local_sym_index != GSYM_CODE |
694 | && local_sym_index != INVALID_CODE); | |
5a6f7e2d ILT |
695 | this->u1_.relobj = relobj; |
696 | this->u2_.od = od; | |
697 | } | |
698 | ||
699 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
700 | unsigned int local_sym_index, | |
701 | unsigned int type, | |
702 | unsigned int shndx, | |
703 | Address address) | |
704 | : address_(address), local_sym_index_(local_sym_index), type_(type), | |
705 | shndx_(shndx) | |
706 | { | |
707 | gold_assert(local_sym_index != GSYM_CODE | |
708 | && local_sym_index != INVALID_CODE); | |
709 | gold_assert(shndx != INVALID_CODE); | |
710 | this->u1_.relobj = relobj; | |
711 | this->u2_.relobj = relobj; | |
c06b7b0b ILT |
712 | } |
713 | ||
714 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 715 | |
a3ad94ed ILT |
716 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
717 | Address address) | |
5a6f7e2d ILT |
718 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), |
719 | shndx_(INVALID_CODE) | |
720 | { | |
721 | this->u1_.os = os; | |
722 | this->u2_.od = od; | |
723 | } | |
724 | ||
725 | Output_reloc(Output_section* os, unsigned int type, Relobj* relobj, | |
726 | unsigned int shndx, Address address) | |
727 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), | |
728 | shndx_(shndx) | |
729 | { | |
730 | gold_assert(shndx != INVALID_CODE); | |
731 | this->u1_.os = os; | |
732 | this->u2_.relobj = relobj; | |
733 | } | |
c06b7b0b ILT |
734 | |
735 | // Write the reloc entry to an output view. | |
736 | void | |
737 | write(unsigned char* pov) const; | |
738 | ||
739 | // Write the offset and info fields to Write_rel. | |
740 | template<typename Write_rel> | |
741 | void write_rel(Write_rel*) const; | |
742 | ||
743 | private: | |
744 | // Return the symbol index. We can't do a double template | |
745 | // specialization, so we do a secondary template here. | |
746 | unsigned int | |
747 | get_symbol_index() const; | |
748 | ||
749 | // Codes for local_sym_index_. | |
750 | enum | |
751 | { | |
752 | // Global symbol. | |
753 | GSYM_CODE = -1U, | |
754 | // Output section. | |
755 | SECTION_CODE = -2U, | |
756 | // Invalid uninitialized entry. | |
757 | INVALID_CODE = -3U | |
758 | }; | |
759 | ||
760 | union | |
761 | { | |
762 | // For a local symbol, the object. We will never generate a | |
763 | // relocation against a local symbol in a dynamic object; that | |
764 | // doesn't make sense. And our callers will always be | |
765 | // templatized, so we use Sized_relobj here. | |
5a6f7e2d | 766 | Sized_relobj<size, big_endian>* relobj; |
c06b7b0b ILT |
767 | // For a global symbol, the symbol. If this is NULL, it indicates |
768 | // a relocation against the undefined 0 symbol. | |
769 | Symbol* gsym; | |
770 | // For a relocation against an output section, the output section. | |
771 | Output_section* os; | |
5a6f7e2d ILT |
772 | } u1_; |
773 | union | |
774 | { | |
775 | // If shndx_ is not INVALID CODE, the object which holds the input | |
776 | // section being used to specify the reloc address. | |
777 | Relobj* relobj; | |
778 | // If shndx_ is INVALID_CODE, the output data being used to | |
779 | // specify the reloc address. This may be NULL if the reloc | |
780 | // address is absolute. | |
781 | Output_data* od; | |
782 | } u2_; | |
783 | // The address offset within the input section or the Output_data. | |
784 | Address address_; | |
c06b7b0b ILT |
785 | // For a local symbol, the local symbol index. This is GSYM_CODE |
786 | // for a global symbol, or INVALID_CODE for an uninitialized value. | |
787 | unsigned int local_sym_index_; | |
a3ad94ed | 788 | // The reloc type--a processor specific code. |
c06b7b0b | 789 | unsigned int type_; |
5a6f7e2d ILT |
790 | // If the reloc address is an input section in an object, the |
791 | // section index. This is INVALID_CODE if the reloc address is | |
792 | // specified in some other way. | |
793 | unsigned int shndx_; | |
c06b7b0b ILT |
794 | }; |
795 | ||
796 | // The SHT_RELA version of Output_reloc<>. This is just derived from | |
797 | // the SHT_REL version of Output_reloc, but it adds an addend. | |
798 | ||
799 | template<bool dynamic, int size, bool big_endian> | |
800 | class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
801 | { | |
802 | public: | |
803 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
804 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; | |
805 | ||
806 | // An uninitialized entry. | |
807 | Output_reloc() | |
808 | : rel_() | |
809 | { } | |
810 | ||
811 | // A reloc against a global symbol. | |
5a6f7e2d | 812 | |
a3ad94ed ILT |
813 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
814 | Address address, Addend addend) | |
815 | : rel_(gsym, type, od, address), addend_(addend) | |
c06b7b0b ILT |
816 | { } |
817 | ||
5a6f7e2d ILT |
818 | Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj, |
819 | unsigned int shndx, Address address, Addend addend) | |
820 | : rel_(gsym, type, relobj, shndx, address), addend_(addend) | |
821 | { } | |
822 | ||
c06b7b0b | 823 | // A reloc against a local symbol. |
5a6f7e2d ILT |
824 | |
825 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
c06b7b0b | 826 | unsigned int local_sym_index, |
a3ad94ed ILT |
827 | unsigned int type, Output_data* od, Address address, |
828 | Addend addend) | |
5a6f7e2d ILT |
829 | : rel_(relobj, local_sym_index, type, od, address), addend_(addend) |
830 | { } | |
831 | ||
832 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
833 | unsigned int local_sym_index, | |
834 | unsigned int type, | |
835 | unsigned int shndx, | |
836 | Address address, | |
837 | Addend addend) | |
838 | : rel_(relobj, local_sym_index, type, shndx, address), | |
839 | addend_(addend) | |
c06b7b0b ILT |
840 | { } |
841 | ||
842 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 843 | |
a3ad94ed ILT |
844 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
845 | Address address, Addend addend) | |
846 | : rel_(os, type, od, address), addend_(addend) | |
c06b7b0b ILT |
847 | { } |
848 | ||
5a6f7e2d ILT |
849 | Output_reloc(Output_section* os, unsigned int type, Relobj* relobj, |
850 | unsigned int shndx, Address address, Addend addend) | |
851 | : rel_(os, type, relobj, shndx, address), addend_(addend) | |
852 | { } | |
853 | ||
c06b7b0b ILT |
854 | // Write the reloc entry to an output view. |
855 | void | |
856 | write(unsigned char* pov) const; | |
857 | ||
858 | private: | |
859 | // The basic reloc. | |
860 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_; | |
861 | // The addend. | |
862 | Addend addend_; | |
863 | }; | |
864 | ||
865 | // Output_data_reloc is used to manage a section containing relocs. | |
866 | // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC | |
867 | // indicates whether this is a dynamic relocation or a normal | |
868 | // relocation. Output_data_reloc_base is a base class. | |
869 | // Output_data_reloc is the real class, which we specialize based on | |
870 | // the reloc type. | |
871 | ||
872 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
27bc2bce | 873 | class Output_data_reloc_base : public Output_section_data_build |
c06b7b0b ILT |
874 | { |
875 | public: | |
876 | typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type; | |
877 | typedef typename Output_reloc_type::Address Address; | |
878 | static const int reloc_size = | |
879 | Reloc_types<sh_type, size, big_endian>::reloc_size; | |
880 | ||
881 | // Construct the section. | |
882 | Output_data_reloc_base() | |
27bc2bce | 883 | : Output_section_data_build(Output_data::default_alignment_for_size(size)) |
c06b7b0b ILT |
884 | { } |
885 | ||
27bc2bce | 886 | protected: |
c06b7b0b ILT |
887 | // Write out the data. |
888 | void | |
889 | do_write(Output_file*); | |
890 | ||
16649710 ILT |
891 | // Set the entry size and the link. |
892 | void | |
893 | do_adjust_output_section(Output_section *os); | |
894 | ||
c06b7b0b ILT |
895 | // Add a relocation entry. |
896 | void | |
4f4c5f80 | 897 | add(Output_data *od, const Output_reloc_type& reloc) |
c06b7b0b ILT |
898 | { |
899 | this->relocs_.push_back(reloc); | |
27bc2bce | 900 | this->set_current_data_size(this->relocs_.size() * reloc_size); |
4f4c5f80 | 901 | od->add_dynamic_reloc(); |
c06b7b0b ILT |
902 | } |
903 | ||
904 | private: | |
905 | typedef std::vector<Output_reloc_type> Relocs; | |
906 | ||
907 | Relocs relocs_; | |
908 | }; | |
909 | ||
910 | // The class which callers actually create. | |
911 | ||
912 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
913 | class Output_data_reloc; | |
914 | ||
915 | // The SHT_REL version of Output_data_reloc. | |
916 | ||
917 | template<bool dynamic, int size, bool big_endian> | |
918 | class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
919 | : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian> | |
920 | { | |
921 | private: | |
922 | typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, | |
923 | big_endian> Base; | |
924 | ||
925 | public: | |
926 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
927 | typedef typename Output_reloc_type::Address Address; | |
928 | ||
929 | Output_data_reloc() | |
930 | : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>() | |
931 | { } | |
932 | ||
933 | // Add a reloc against a global symbol. | |
5a6f7e2d | 934 | |
c06b7b0b | 935 | void |
a3ad94ed | 936 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address) |
4f4c5f80 | 937 | { this->add(od, Output_reloc_type(gsym, type, od, address)); } |
c06b7b0b | 938 | |
5a6f7e2d | 939 | void |
4f4c5f80 | 940 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj, |
5a6f7e2d | 941 | unsigned int shndx, Address address) |
4f4c5f80 | 942 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address)); } |
5a6f7e2d | 943 | |
c06b7b0b | 944 | // Add a reloc against a local symbol. |
5a6f7e2d | 945 | |
c06b7b0b | 946 | void |
5a6f7e2d | 947 | add_local(Sized_relobj<size, big_endian>* relobj, |
a3ad94ed ILT |
948 | unsigned int local_sym_index, unsigned int type, |
949 | Output_data* od, Address address) | |
4f4c5f80 ILT |
950 | { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, |
951 | address)); } | |
5a6f7e2d ILT |
952 | |
953 | void | |
954 | add_local(Sized_relobj<size, big_endian>* relobj, | |
955 | unsigned int local_sym_index, unsigned int type, | |
4f4c5f80 ILT |
956 | Output_data* od, unsigned int shndx, Address address) |
957 | { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
958 | address)); } | |
5a6f7e2d | 959 | |
c06b7b0b ILT |
960 | |
961 | // A reloc against the STT_SECTION symbol of an output section. | |
4f4c5f80 ILT |
962 | // OS is the Output_section that the relocation refers to; OD is |
963 | // the Output_data object being relocated. | |
5a6f7e2d | 964 | |
c06b7b0b | 965 | void |
a3ad94ed ILT |
966 | add_output_section(Output_section* os, unsigned int type, |
967 | Output_data* od, Address address) | |
4f4c5f80 | 968 | { this->add(od, Output_reloc_type(os, type, od, address)); } |
5a6f7e2d ILT |
969 | |
970 | void | |
4f4c5f80 | 971 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
5a6f7e2d | 972 | Relobj* relobj, unsigned int shndx, Address address) |
4f4c5f80 | 973 | { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); } |
c06b7b0b ILT |
974 | }; |
975 | ||
976 | // The SHT_RELA version of Output_data_reloc. | |
977 | ||
978 | template<bool dynamic, int size, bool big_endian> | |
979 | class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
980 | : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
981 | { | |
982 | private: | |
983 | typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, | |
984 | big_endian> Base; | |
985 | ||
986 | public: | |
987 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
988 | typedef typename Output_reloc_type::Address Address; | |
989 | typedef typename Output_reloc_type::Addend Addend; | |
990 | ||
991 | Output_data_reloc() | |
992 | : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>() | |
993 | { } | |
994 | ||
995 | // Add a reloc against a global symbol. | |
5a6f7e2d | 996 | |
c06b7b0b | 997 | void |
a3ad94ed ILT |
998 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
999 | Address address, Addend addend) | |
4f4c5f80 | 1000 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend)); } |
c06b7b0b | 1001 | |
5a6f7e2d | 1002 | void |
4f4c5f80 ILT |
1003 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj, |
1004 | unsigned int shndx, Address address, | |
1005 | Addend addend) | |
1006 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, | |
1007 | addend)); } | |
5a6f7e2d | 1008 | |
c06b7b0b | 1009 | // Add a reloc against a local symbol. |
5a6f7e2d | 1010 | |
c06b7b0b | 1011 | void |
5a6f7e2d | 1012 | add_local(Sized_relobj<size, big_endian>* relobj, |
c06b7b0b | 1013 | unsigned int local_sym_index, unsigned int type, |
a3ad94ed | 1014 | Output_data* od, Address address, Addend addend) |
c06b7b0b | 1015 | { |
4f4c5f80 ILT |
1016 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
1017 | addend)); | |
5a6f7e2d ILT |
1018 | } |
1019 | ||
1020 | void | |
1021 | add_local(Sized_relobj<size, big_endian>* relobj, | |
1022 | unsigned int local_sym_index, unsigned int type, | |
4f4c5f80 ILT |
1023 | Output_data* od, unsigned int shndx, Address address, |
1024 | Addend addend) | |
5a6f7e2d | 1025 | { |
4f4c5f80 ILT |
1026 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
1027 | address, addend)); | |
c06b7b0b ILT |
1028 | } |
1029 | ||
1030 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 1031 | |
c06b7b0b | 1032 | void |
a3ad94ed ILT |
1033 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
1034 | Address address, Addend addend) | |
4f4c5f80 | 1035 | { this->add(os, Output_reloc_type(os, type, od, address, addend)); } |
5a6f7e2d ILT |
1036 | |
1037 | void | |
1038 | add_output_section(Output_section* os, unsigned int type, Relobj* relobj, | |
1039 | unsigned int shndx, Address address, Addend addend) | |
4f4c5f80 ILT |
1040 | { this->add(os, Output_reloc_type(os, type, relobj, shndx, address, |
1041 | addend)); } | |
c06b7b0b ILT |
1042 | }; |
1043 | ||
dbe717ef ILT |
1044 | // Output_data_got is used to manage a GOT. Each entry in the GOT is |
1045 | // for one symbol--either a global symbol or a local symbol in an | |
ead1e424 | 1046 | // object. The target specific code adds entries to the GOT as |
dbe717ef | 1047 | // needed. |
ead1e424 ILT |
1048 | |
1049 | template<int size, bool big_endian> | |
27bc2bce | 1050 | class Output_data_got : public Output_section_data_build |
ead1e424 ILT |
1051 | { |
1052 | public: | |
1053 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; | |
1054 | ||
7e1edb90 | 1055 | Output_data_got() |
27bc2bce | 1056 | : Output_section_data_build(Output_data::default_alignment_for_size(size)), |
730cdc88 | 1057 | entries_() |
ead1e424 ILT |
1058 | { } |
1059 | ||
dbe717ef ILT |
1060 | // Add an entry for a global symbol to the GOT. Return true if this |
1061 | // is a new GOT entry, false if the symbol was already in the GOT. | |
1062 | bool | |
1063 | add_global(Symbol* gsym); | |
ead1e424 | 1064 | |
e727fa71 ILT |
1065 | // Add an entry for a local symbol to the GOT. This returns true if |
1066 | // this is a new GOT entry, false if the symbol already has a GOT | |
1067 | // entry. | |
1068 | bool | |
1069 | add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index); | |
ead1e424 | 1070 | |
07f397ab ILT |
1071 | // Add an entry (or pair of entries) for a global TLS symbol to the GOT. |
1072 | // Return true if this is a new GOT entry, false if the symbol was | |
1073 | // already in the GOT. | |
1074 | bool | |
1075 | add_global_tls(Symbol* gsym, bool need_pair); | |
1076 | ||
1077 | // Add an entry (or pair of entries) for a local TLS symbol to the GOT. | |
1078 | // This returns true if this is a new GOT entry, false if the symbol | |
1079 | // already has a GOT entry. | |
1080 | bool | |
1081 | add_local_tls(Sized_relobj<size, big_endian>* object, | |
1082 | unsigned int sym_index, bool need_pair); | |
1083 | ||
ead1e424 ILT |
1084 | // Add a constant to the GOT. This returns the offset of the new |
1085 | // entry from the start of the GOT. | |
1086 | unsigned int | |
1087 | add_constant(Valtype constant) | |
1088 | { | |
1089 | this->entries_.push_back(Got_entry(constant)); | |
1090 | this->set_got_size(); | |
1091 | return this->last_got_offset(); | |
1092 | } | |
1093 | ||
27bc2bce | 1094 | protected: |
ead1e424 ILT |
1095 | // Write out the GOT table. |
1096 | void | |
1097 | do_write(Output_file*); | |
1098 | ||
1099 | private: | |
1100 | // This POD class holds a single GOT entry. | |
1101 | class Got_entry | |
1102 | { | |
1103 | public: | |
1104 | // Create a zero entry. | |
1105 | Got_entry() | |
1106 | : local_sym_index_(CONSTANT_CODE) | |
1107 | { this->u_.constant = 0; } | |
1108 | ||
1109 | // Create a global symbol entry. | |
a3ad94ed | 1110 | explicit Got_entry(Symbol* gsym) |
ead1e424 ILT |
1111 | : local_sym_index_(GSYM_CODE) |
1112 | { this->u_.gsym = gsym; } | |
1113 | ||
1114 | // Create a local symbol entry. | |
e727fa71 ILT |
1115 | Got_entry(Sized_relobj<size, big_endian>* object, |
1116 | unsigned int local_sym_index) | |
ead1e424 ILT |
1117 | : local_sym_index_(local_sym_index) |
1118 | { | |
a3ad94ed ILT |
1119 | gold_assert(local_sym_index != GSYM_CODE |
1120 | && local_sym_index != CONSTANT_CODE); | |
ead1e424 ILT |
1121 | this->u_.object = object; |
1122 | } | |
1123 | ||
1124 | // Create a constant entry. The constant is a host value--it will | |
1125 | // be swapped, if necessary, when it is written out. | |
a3ad94ed | 1126 | explicit Got_entry(Valtype constant) |
ead1e424 ILT |
1127 | : local_sym_index_(CONSTANT_CODE) |
1128 | { this->u_.constant = constant; } | |
1129 | ||
1130 | // Write the GOT entry to an output view. | |
1131 | void | |
7e1edb90 | 1132 | write(unsigned char* pov) const; |
ead1e424 ILT |
1133 | |
1134 | private: | |
1135 | enum | |
1136 | { | |
1137 | GSYM_CODE = -1U, | |
1138 | CONSTANT_CODE = -2U | |
1139 | }; | |
1140 | ||
1141 | union | |
1142 | { | |
1143 | // For a local symbol, the object. | |
e727fa71 | 1144 | Sized_relobj<size, big_endian>* object; |
ead1e424 ILT |
1145 | // For a global symbol, the symbol. |
1146 | Symbol* gsym; | |
1147 | // For a constant, the constant. | |
1148 | Valtype constant; | |
1149 | } u_; | |
c06b7b0b ILT |
1150 | // For a local symbol, the local symbol index. This is GSYM_CODE |
1151 | // for a global symbol, or CONSTANT_CODE for a constant. | |
ead1e424 ILT |
1152 | unsigned int local_sym_index_; |
1153 | }; | |
1154 | ||
1155 | typedef std::vector<Got_entry> Got_entries; | |
1156 | ||
1157 | // Return the offset into the GOT of GOT entry I. | |
1158 | unsigned int | |
1159 | got_offset(unsigned int i) const | |
1160 | { return i * (size / 8); } | |
1161 | ||
1162 | // Return the offset into the GOT of the last entry added. | |
1163 | unsigned int | |
1164 | last_got_offset() const | |
1165 | { return this->got_offset(this->entries_.size() - 1); } | |
1166 | ||
1167 | // Set the size of the section. | |
1168 | void | |
1169 | set_got_size() | |
27bc2bce | 1170 | { this->set_current_data_size(this->got_offset(this->entries_.size())); } |
ead1e424 ILT |
1171 | |
1172 | // The list of GOT entries. | |
1173 | Got_entries entries_; | |
1174 | }; | |
1175 | ||
a3ad94ed ILT |
1176 | // Output_data_dynamic is used to hold the data in SHT_DYNAMIC |
1177 | // section. | |
1178 | ||
1179 | class Output_data_dynamic : public Output_section_data | |
1180 | { | |
1181 | public: | |
9025d29d | 1182 | Output_data_dynamic(Stringpool* pool) |
730cdc88 | 1183 | : Output_section_data(Output_data::default_alignment()), |
9025d29d | 1184 | entries_(), pool_(pool) |
a3ad94ed ILT |
1185 | { } |
1186 | ||
1187 | // Add a new dynamic entry with a fixed numeric value. | |
1188 | void | |
1189 | add_constant(elfcpp::DT tag, unsigned int val) | |
1190 | { this->add_entry(Dynamic_entry(tag, val)); } | |
1191 | ||
16649710 | 1192 | // Add a new dynamic entry with the address of output data. |
a3ad94ed | 1193 | void |
16649710 ILT |
1194 | add_section_address(elfcpp::DT tag, const Output_data* od) |
1195 | { this->add_entry(Dynamic_entry(tag, od, false)); } | |
a3ad94ed | 1196 | |
16649710 | 1197 | // Add a new dynamic entry with the size of output data. |
a3ad94ed | 1198 | void |
16649710 ILT |
1199 | add_section_size(elfcpp::DT tag, const Output_data* od) |
1200 | { this->add_entry(Dynamic_entry(tag, od, true)); } | |
a3ad94ed ILT |
1201 | |
1202 | // Add a new dynamic entry with the address of a symbol. | |
1203 | void | |
16649710 | 1204 | add_symbol(elfcpp::DT tag, const Symbol* sym) |
a3ad94ed ILT |
1205 | { this->add_entry(Dynamic_entry(tag, sym)); } |
1206 | ||
1207 | // Add a new dynamic entry with a string. | |
1208 | void | |
1209 | add_string(elfcpp::DT tag, const char* str) | |
cfd73a4e | 1210 | { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); } |
a3ad94ed | 1211 | |
41f542e7 ILT |
1212 | void |
1213 | add_string(elfcpp::DT tag, const std::string& str) | |
1214 | { this->add_string(tag, str.c_str()); } | |
1215 | ||
27bc2bce ILT |
1216 | protected: |
1217 | // Adjust the output section to set the entry size. | |
1218 | void | |
1219 | do_adjust_output_section(Output_section*); | |
1220 | ||
a3ad94ed ILT |
1221 | // Set the final data size. |
1222 | void | |
27bc2bce | 1223 | set_final_data_size(); |
a3ad94ed ILT |
1224 | |
1225 | // Write out the dynamic entries. | |
1226 | void | |
1227 | do_write(Output_file*); | |
1228 | ||
1229 | private: | |
1230 | // This POD class holds a single dynamic entry. | |
1231 | class Dynamic_entry | |
1232 | { | |
1233 | public: | |
1234 | // Create an entry with a fixed numeric value. | |
1235 | Dynamic_entry(elfcpp::DT tag, unsigned int val) | |
1236 | : tag_(tag), classification_(DYNAMIC_NUMBER) | |
1237 | { this->u_.val = val; } | |
1238 | ||
1239 | // Create an entry with the size or address of a section. | |
16649710 | 1240 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size) |
a3ad94ed ILT |
1241 | : tag_(tag), |
1242 | classification_(section_size | |
1243 | ? DYNAMIC_SECTION_SIZE | |
1244 | : DYNAMIC_SECTION_ADDRESS) | |
16649710 | 1245 | { this->u_.od = od; } |
a3ad94ed ILT |
1246 | |
1247 | // Create an entry with the address of a symbol. | |
16649710 | 1248 | Dynamic_entry(elfcpp::DT tag, const Symbol* sym) |
a3ad94ed ILT |
1249 | : tag_(tag), classification_(DYNAMIC_SYMBOL) |
1250 | { this->u_.sym = sym; } | |
1251 | ||
1252 | // Create an entry with a string. | |
1253 | Dynamic_entry(elfcpp::DT tag, const char* str) | |
1254 | : tag_(tag), classification_(DYNAMIC_STRING) | |
1255 | { this->u_.str = str; } | |
1256 | ||
1257 | // Write the dynamic entry to an output view. | |
1258 | template<int size, bool big_endian> | |
1259 | void | |
1ddbd1e6 | 1260 | write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const; |
a3ad94ed ILT |
1261 | |
1262 | private: | |
1263 | enum Classification | |
1264 | { | |
1265 | // Number. | |
1266 | DYNAMIC_NUMBER, | |
1267 | // Section address. | |
1268 | DYNAMIC_SECTION_ADDRESS, | |
1269 | // Section size. | |
1270 | DYNAMIC_SECTION_SIZE, | |
1271 | // Symbol adress. | |
1272 | DYNAMIC_SYMBOL, | |
1273 | // String. | |
1274 | DYNAMIC_STRING | |
1275 | }; | |
1276 | ||
1277 | union | |
1278 | { | |
1279 | // For DYNAMIC_NUMBER. | |
1280 | unsigned int val; | |
1281 | // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE. | |
16649710 | 1282 | const Output_data* od; |
a3ad94ed | 1283 | // For DYNAMIC_SYMBOL. |
16649710 | 1284 | const Symbol* sym; |
a3ad94ed ILT |
1285 | // For DYNAMIC_STRING. |
1286 | const char* str; | |
1287 | } u_; | |
1288 | // The dynamic tag. | |
1289 | elfcpp::DT tag_; | |
1290 | // The type of entry. | |
1291 | Classification classification_; | |
1292 | }; | |
1293 | ||
1294 | // Add an entry to the list. | |
1295 | void | |
1296 | add_entry(const Dynamic_entry& entry) | |
1297 | { this->entries_.push_back(entry); } | |
1298 | ||
1299 | // Sized version of write function. | |
1300 | template<int size, bool big_endian> | |
1301 | void | |
1302 | sized_write(Output_file* of); | |
1303 | ||
1304 | // The type of the list of entries. | |
1305 | typedef std::vector<Dynamic_entry> Dynamic_entries; | |
1306 | ||
a3ad94ed ILT |
1307 | // The entries. |
1308 | Dynamic_entries entries_; | |
1309 | // The pool used for strings. | |
1310 | Stringpool* pool_; | |
1311 | }; | |
1312 | ||
a2fb1b05 ILT |
1313 | // An output section. We don't expect to have too many output |
1314 | // sections, so we don't bother to do a template on the size. | |
1315 | ||
54dc6425 | 1316 | class Output_section : public Output_data |
a2fb1b05 ILT |
1317 | { |
1318 | public: | |
1319 | // Create an output section, giving the name, type, and flags. | |
9a0910c3 ILT |
1320 | Output_section(const General_options& options, |
1321 | const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword); | |
54dc6425 | 1322 | virtual ~Output_section(); |
a2fb1b05 | 1323 | |
ead1e424 | 1324 | // Add a new input section SHNDX, named NAME, with header SHDR, from |
730cdc88 ILT |
1325 | // object OBJECT. RELOC_SHNDX is the index of a relocation section |
1326 | // which applies to this section, or 0 if none, or -1U if more than | |
1327 | // one. Return the offset within the output section. | |
a2fb1b05 ILT |
1328 | template<int size, bool big_endian> |
1329 | off_t | |
730cdc88 ILT |
1330 | add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx, |
1331 | const char *name, | |
1332 | const elfcpp::Shdr<size, big_endian>& shdr, | |
1333 | unsigned int reloc_shndx); | |
a2fb1b05 | 1334 | |
b8e6aad9 | 1335 | // Add generated data POSD to this output section. |
c06b7b0b | 1336 | void |
ead1e424 ILT |
1337 | add_output_section_data(Output_section_data* posd); |
1338 | ||
a2fb1b05 ILT |
1339 | // Return the section name. |
1340 | const char* | |
1341 | name() const | |
1342 | { return this->name_; } | |
1343 | ||
9a0910c3 ILT |
1344 | // Modify the section name. This should be called only after this |
1345 | // section is done being constructed. The input should be a pointer | |
1346 | // into layout's namepool_. | |
1347 | void | |
1348 | set_name(const char* newname) | |
1349 | { this->name_ = newname; } | |
1350 | ||
a2fb1b05 ILT |
1351 | // Return the section type. |
1352 | elfcpp::Elf_Word | |
1353 | type() const | |
1354 | { return this->type_; } | |
1355 | ||
1356 | // Return the section flags. | |
1357 | elfcpp::Elf_Xword | |
1358 | flags() const | |
1359 | { return this->flags_; } | |
1360 | ||
a3ad94ed ILT |
1361 | // Return the entsize field. |
1362 | uint64_t | |
1363 | entsize() const | |
1364 | { return this->entsize_; } | |
1365 | ||
61ba1cf9 ILT |
1366 | // Set the entsize field. |
1367 | void | |
16649710 | 1368 | set_entsize(uint64_t v); |
61ba1cf9 | 1369 | |
16649710 ILT |
1370 | // Set the link field to the output section index of a section. |
1371 | void | |
14b31740 | 1372 | set_link_section(const Output_data* od) |
16649710 ILT |
1373 | { |
1374 | gold_assert(this->link_ == 0 | |
1375 | && !this->should_link_to_symtab_ | |
1376 | && !this->should_link_to_dynsym_); | |
1377 | this->link_section_ = od; | |
1378 | } | |
1379 | ||
1380 | // Set the link field to a constant. | |
61ba1cf9 ILT |
1381 | void |
1382 | set_link(unsigned int v) | |
16649710 ILT |
1383 | { |
1384 | gold_assert(this->link_section_ == NULL | |
1385 | && !this->should_link_to_symtab_ | |
1386 | && !this->should_link_to_dynsym_); | |
1387 | this->link_ = v; | |
1388 | } | |
61ba1cf9 | 1389 | |
16649710 ILT |
1390 | // Record that this section should link to the normal symbol table. |
1391 | void | |
1392 | set_should_link_to_symtab() | |
1393 | { | |
1394 | gold_assert(this->link_section_ == NULL | |
1395 | && this->link_ == 0 | |
1396 | && !this->should_link_to_dynsym_); | |
1397 | this->should_link_to_symtab_ = true; | |
1398 | } | |
1399 | ||
1400 | // Record that this section should link to the dynamic symbol table. | |
1401 | void | |
1402 | set_should_link_to_dynsym() | |
1403 | { | |
1404 | gold_assert(this->link_section_ == NULL | |
1405 | && this->link_ == 0 | |
1406 | && !this->should_link_to_symtab_); | |
1407 | this->should_link_to_dynsym_ = true; | |
1408 | } | |
1409 | ||
1410 | // Return the info field. | |
1411 | unsigned int | |
1412 | info() const | |
1413 | { | |
1414 | gold_assert(this->info_section_ == NULL); | |
1415 | return this->info_; | |
1416 | } | |
1417 | ||
1418 | // Set the info field to the output section index of a section. | |
1419 | void | |
14b31740 | 1420 | set_info_section(const Output_data* od) |
16649710 ILT |
1421 | { |
1422 | gold_assert(this->info_ == 0); | |
1423 | this->info_section_ = od; | |
1424 | } | |
1425 | ||
1426 | // Set the info field to a constant. | |
61ba1cf9 ILT |
1427 | void |
1428 | set_info(unsigned int v) | |
16649710 ILT |
1429 | { |
1430 | gold_assert(this->info_section_ == NULL); | |
1431 | this->info_ = v; | |
1432 | } | |
61ba1cf9 ILT |
1433 | |
1434 | // Set the addralign field. | |
1435 | void | |
1436 | set_addralign(uint64_t v) | |
1437 | { this->addralign_ = v; } | |
1438 | ||
c06b7b0b ILT |
1439 | // Indicate that we need a symtab index. |
1440 | void | |
1441 | set_needs_symtab_index() | |
1442 | { this->needs_symtab_index_ = true; } | |
1443 | ||
1444 | // Return whether we need a symtab index. | |
1445 | bool | |
1446 | needs_symtab_index() const | |
1447 | { return this->needs_symtab_index_; } | |
1448 | ||
1449 | // Get the symtab index. | |
1450 | unsigned int | |
1451 | symtab_index() const | |
1452 | { | |
a3ad94ed | 1453 | gold_assert(this->symtab_index_ != 0); |
c06b7b0b ILT |
1454 | return this->symtab_index_; |
1455 | } | |
1456 | ||
1457 | // Set the symtab index. | |
1458 | void | |
1459 | set_symtab_index(unsigned int index) | |
1460 | { | |
a3ad94ed | 1461 | gold_assert(index != 0); |
c06b7b0b ILT |
1462 | this->symtab_index_ = index; |
1463 | } | |
1464 | ||
1465 | // Indicate that we need a dynsym index. | |
1466 | void | |
1467 | set_needs_dynsym_index() | |
1468 | { this->needs_dynsym_index_ = true; } | |
1469 | ||
1470 | // Return whether we need a dynsym index. | |
1471 | bool | |
1472 | needs_dynsym_index() const | |
1473 | { return this->needs_dynsym_index_; } | |
1474 | ||
1475 | // Get the dynsym index. | |
1476 | unsigned int | |
1477 | dynsym_index() const | |
1478 | { | |
a3ad94ed | 1479 | gold_assert(this->dynsym_index_ != 0); |
c06b7b0b ILT |
1480 | return this->dynsym_index_; |
1481 | } | |
1482 | ||
1483 | // Set the dynsym index. | |
1484 | void | |
1485 | set_dynsym_index(unsigned int index) | |
1486 | { | |
a3ad94ed | 1487 | gold_assert(index != 0); |
c06b7b0b ILT |
1488 | this->dynsym_index_ = index; |
1489 | } | |
1490 | ||
730cdc88 ILT |
1491 | // Return whether this section should be written after all the input |
1492 | // sections are complete. | |
1493 | bool | |
1494 | after_input_sections() const | |
1495 | { return this->after_input_sections_; } | |
1496 | ||
1497 | // Record that this section should be written after all the input | |
1498 | // sections are complete. | |
1499 | void | |
1500 | set_after_input_sections() | |
1501 | { this->after_input_sections_ = true; } | |
1502 | ||
27bc2bce ILT |
1503 | // Return whether this section requires postprocessing after all |
1504 | // relocations have been applied. | |
1505 | bool | |
1506 | requires_postprocessing() const | |
1507 | { return this->requires_postprocessing_; } | |
1508 | ||
1509 | // Record that this section requires postprocessing after all | |
1510 | // relocations have been applied. | |
1511 | void | |
1512 | set_requires_postprocessing() | |
1513 | { this->requires_postprocessing_ = true; } | |
1514 | ||
730cdc88 ILT |
1515 | // Return whether the offset OFFSET in the input section SHNDX in |
1516 | // object OBJECT is being included in the link. | |
1517 | bool | |
1518 | is_input_address_mapped(const Relobj* object, unsigned int shndx, | |
1519 | off_t offset) const; | |
1520 | ||
1521 | // Return the offset within the output section of OFFSET relative to | |
1522 | // the start of input section SHNDX in object OBJECT. | |
1523 | off_t | |
1524 | output_offset(const Relobj* object, unsigned int shndx, off_t offset) const; | |
1525 | ||
b8e6aad9 ILT |
1526 | // Return the output virtual address of OFFSET relative to the start |
1527 | // of input section SHNDX in object OBJECT. | |
1528 | uint64_t | |
1529 | output_address(const Relobj* object, unsigned int shndx, | |
1530 | off_t offset) const; | |
1531 | ||
27bc2bce ILT |
1532 | // Write the section header into *OPHDR. |
1533 | template<int size, bool big_endian> | |
1534 | void | |
1535 | write_header(const Layout*, const Stringpool*, | |
1536 | elfcpp::Shdr_write<size, big_endian>*) const; | |
1537 | ||
9a0910c3 ILT |
1538 | // This class may change the output section name. This is called |
1539 | // right before shstrtab is written, so after all input-section | |
1540 | // layout processing is done. This calls | |
1541 | // do_modified_output_section_name() on all its output_section_data | |
1542 | // members, and changes the name if any member so suggests. If | |
1543 | // several members would suggest, this takes the first, arbitrarily. | |
1544 | // Return true if the name was modified, false else. | |
1545 | bool | |
1546 | maybe_modify_output_section_name(); | |
1547 | ||
27bc2bce ILT |
1548 | protected: |
1549 | // Return the section index in the output file. | |
1550 | unsigned int | |
1551 | do_out_shndx() const | |
1552 | { | |
1553 | gold_assert(this->out_shndx_ != -1U); | |
1554 | return this->out_shndx_; | |
1555 | } | |
1556 | ||
1557 | // Set the output section index. | |
1558 | void | |
1559 | do_set_out_shndx(unsigned int shndx) | |
1560 | { | |
1561 | gold_assert(this->out_shndx_ == -1U); | |
1562 | this->out_shndx_ = shndx; | |
1563 | } | |
1564 | ||
1565 | // Set the final data size of the Output_section. For a typical | |
ead1e424 | 1566 | // Output_section, there is nothing to do, but if there are any |
27bc2bce | 1567 | // Output_section_data objects we need to set their final addresses |
ead1e424 ILT |
1568 | // here. |
1569 | void | |
27bc2bce | 1570 | set_final_data_size(); |
ead1e424 | 1571 | |
54dc6425 | 1572 | // Write the data to the file. For a typical Output_section, this |
ead1e424 ILT |
1573 | // does nothing: the data is written out by calling Object::Relocate |
1574 | // on each input object. But if there are any Output_section_data | |
1575 | // objects we do need to write them out here. | |
a3ad94ed | 1576 | void |
ead1e424 | 1577 | do_write(Output_file*); |
54dc6425 | 1578 | |
75f65a3e ILT |
1579 | // Return the address alignment--function required by parent class. |
1580 | uint64_t | |
1581 | do_addralign() const | |
1582 | { return this->addralign_; } | |
1583 | ||
1584 | // Return whether this is an Output_section. | |
1585 | bool | |
1586 | do_is_section() const | |
1587 | { return true; } | |
1588 | ||
54dc6425 ILT |
1589 | // Return whether this is a section of the specified type. |
1590 | bool | |
75f65a3e | 1591 | do_is_section_type(elfcpp::Elf_Word type) const |
54dc6425 ILT |
1592 | { return this->type_ == type; } |
1593 | ||
1594 | // Return whether the specified section flag is set. | |
1595 | bool | |
75f65a3e | 1596 | do_is_section_flag_set(elfcpp::Elf_Xword flag) const |
54dc6425 ILT |
1597 | { return (this->flags_ & flag) != 0; } |
1598 | ||
a2fb1b05 | 1599 | private: |
ead1e424 ILT |
1600 | // In some cases we need to keep a list of the input sections |
1601 | // associated with this output section. We only need the list if we | |
1602 | // might have to change the offsets of the input section within the | |
1603 | // output section after we add the input section. The ordinary | |
1604 | // input sections will be written out when we process the object | |
1605 | // file, and as such we don't need to track them here. We do need | |
1606 | // to track Output_section_data objects here. We store instances of | |
1607 | // this structure in a std::vector, so it must be a POD. There can | |
1608 | // be many instances of this structure, so we use a union to save | |
1609 | // some space. | |
1610 | class Input_section | |
1611 | { | |
1612 | public: | |
1613 | Input_section() | |
b8e6aad9 ILT |
1614 | : shndx_(0), p2align_(0) |
1615 | { | |
1616 | this->u1_.data_size = 0; | |
1617 | this->u2_.object = NULL; | |
1618 | } | |
ead1e424 | 1619 | |
b8e6aad9 | 1620 | // For an ordinary input section. |
f6ce93d6 | 1621 | Input_section(Relobj* object, unsigned int shndx, off_t data_size, |
ead1e424 ILT |
1622 | uint64_t addralign) |
1623 | : shndx_(shndx), | |
b8e6aad9 | 1624 | p2align_(ffsll(static_cast<long long>(addralign))) |
ead1e424 | 1625 | { |
b8e6aad9 ILT |
1626 | gold_assert(shndx != OUTPUT_SECTION_CODE |
1627 | && shndx != MERGE_DATA_SECTION_CODE | |
1628 | && shndx != MERGE_STRING_SECTION_CODE); | |
1629 | this->u1_.data_size = data_size; | |
1630 | this->u2_.object = object; | |
ead1e424 ILT |
1631 | } |
1632 | ||
b8e6aad9 | 1633 | // For a non-merge output section. |
ead1e424 | 1634 | Input_section(Output_section_data* posd) |
b8e6aad9 ILT |
1635 | : shndx_(OUTPUT_SECTION_CODE), |
1636 | p2align_(ffsll(static_cast<long long>(posd->addralign()))) | |
1637 | { | |
1638 | this->u1_.data_size = 0; | |
1639 | this->u2_.posd = posd; | |
1640 | } | |
1641 | ||
1642 | // For a merge section. | |
1643 | Input_section(Output_section_data* posd, bool is_string, uint64_t entsize) | |
1644 | : shndx_(is_string | |
1645 | ? MERGE_STRING_SECTION_CODE | |
1646 | : MERGE_DATA_SECTION_CODE), | |
1647 | p2align_(ffsll(static_cast<long long>(posd->addralign()))) | |
1648 | { | |
1649 | this->u1_.entsize = entsize; | |
1650 | this->u2_.posd = posd; | |
1651 | } | |
ead1e424 ILT |
1652 | |
1653 | // The required alignment. | |
1654 | uint64_t | |
1655 | addralign() const | |
a3ad94ed ILT |
1656 | { |
1657 | return (this->p2align_ == 0 | |
1658 | ? 0 | |
1659 | : static_cast<uint64_t>(1) << (this->p2align_ - 1)); | |
1660 | } | |
ead1e424 ILT |
1661 | |
1662 | // Return the required size. | |
1663 | off_t | |
1664 | data_size() const; | |
1665 | ||
b8e6aad9 ILT |
1666 | // Return whether this is a merge section which matches the |
1667 | // parameters. | |
1668 | bool | |
87f95776 ILT |
1669 | is_merge_section(bool is_string, uint64_t entsize, |
1670 | uint64_t addralign) const | |
b8e6aad9 ILT |
1671 | { |
1672 | return (this->shndx_ == (is_string | |
1673 | ? MERGE_STRING_SECTION_CODE | |
1674 | : MERGE_DATA_SECTION_CODE) | |
87f95776 ILT |
1675 | && this->u1_.entsize == entsize |
1676 | && this->addralign() == addralign); | |
b8e6aad9 ILT |
1677 | } |
1678 | ||
1679 | // Set the output section. | |
1680 | void | |
1681 | set_output_section(Output_section* os) | |
1682 | { | |
1683 | gold_assert(!this->is_input_section()); | |
1684 | this->u2_.posd->set_output_section(os); | |
1685 | } | |
1686 | ||
ead1e424 ILT |
1687 | // Set the address and file offset. This is called during |
1688 | // Layout::finalize. SECOFF is the file offset of the enclosing | |
1689 | // section. | |
1690 | void | |
1691 | set_address(uint64_t addr, off_t off, off_t secoff); | |
1692 | ||
9a0910c3 ILT |
1693 | // Call modified_output_section_name on the output-section-data object. |
1694 | const char* | |
1695 | modified_output_section_name(const char* name) const | |
1696 | { | |
1697 | if (this->is_input_section()) | |
1698 | return NULL; | |
1699 | return this->u2_.posd->do_modified_output_section_name(name); | |
1700 | } | |
1701 | ||
b8e6aad9 ILT |
1702 | // Add an input section, for SHF_MERGE sections. |
1703 | bool | |
1704 | add_input_section(Relobj* object, unsigned int shndx) | |
1705 | { | |
1706 | gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE | |
1707 | || this->shndx_ == MERGE_STRING_SECTION_CODE); | |
1708 | return this->u2_.posd->add_input_section(object, shndx); | |
1709 | } | |
1710 | ||
1711 | // Given an input OBJECT, an input section index SHNDX within that | |
1712 | // object, and an OFFSET relative to the start of that input | |
730cdc88 ILT |
1713 | // section, return whether or not the output offset is known. If |
1714 | // this function returns true, it sets *POUTPUT to the output | |
1715 | // offset. | |
b8e6aad9 | 1716 | bool |
730cdc88 ILT |
1717 | output_offset(const Relobj* object, unsigned int shndx, off_t offset, |
1718 | off_t *poutput) const; | |
b8e6aad9 | 1719 | |
ead1e424 ILT |
1720 | // Write out the data. This does nothing for an input section. |
1721 | void | |
1722 | write(Output_file*); | |
1723 | ||
1724 | private: | |
b8e6aad9 ILT |
1725 | // Code values which appear in shndx_. If the value is not one of |
1726 | // these codes, it is the input section index in the object file. | |
1727 | enum | |
1728 | { | |
1729 | // An Output_section_data. | |
1730 | OUTPUT_SECTION_CODE = -1U, | |
1731 | // An Output_section_data for an SHF_MERGE section with | |
1732 | // SHF_STRINGS not set. | |
1733 | MERGE_DATA_SECTION_CODE = -2U, | |
1734 | // An Output_section_data for an SHF_MERGE section with | |
1735 | // SHF_STRINGS set. | |
1736 | MERGE_STRING_SECTION_CODE = -3U | |
1737 | }; | |
1738 | ||
ead1e424 ILT |
1739 | // Whether this is an input section. |
1740 | bool | |
1741 | is_input_section() const | |
b8e6aad9 ILT |
1742 | { |
1743 | return (this->shndx_ != OUTPUT_SECTION_CODE | |
1744 | && this->shndx_ != MERGE_DATA_SECTION_CODE | |
1745 | && this->shndx_ != MERGE_STRING_SECTION_CODE); | |
1746 | } | |
ead1e424 | 1747 | |
b8e6aad9 ILT |
1748 | // For an ordinary input section, this is the section index in the |
1749 | // input file. For an Output_section_data, this is | |
1750 | // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or | |
1751 | // MERGE_STRING_SECTION_CODE. | |
ead1e424 ILT |
1752 | unsigned int shndx_; |
1753 | // The required alignment, stored as a power of 2. | |
1754 | unsigned int p2align_; | |
ead1e424 ILT |
1755 | union |
1756 | { | |
b8e6aad9 ILT |
1757 | // For an ordinary input section, the section size. |
1758 | off_t data_size; | |
1759 | // For OUTPUT_SECTION_CODE, this is not used. For | |
1760 | // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the | |
1761 | // entity size. | |
1762 | uint64_t entsize; | |
1763 | } u1_; | |
1764 | union | |
1765 | { | |
1766 | // For an ordinary input section, the object which holds the | |
ead1e424 | 1767 | // input section. |
f6ce93d6 | 1768 | Relobj* object; |
b8e6aad9 ILT |
1769 | // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or |
1770 | // MERGE_STRING_SECTION_CODE, the data. | |
ead1e424 | 1771 | Output_section_data* posd; |
b8e6aad9 | 1772 | } u2_; |
ead1e424 ILT |
1773 | }; |
1774 | ||
1775 | typedef std::vector<Input_section> Input_section_list; | |
1776 | ||
c51e6221 ILT |
1777 | // Fill data. This is used to fill in data between input sections. |
1778 | // When we have to keep track of the input sections, we can use an | |
1779 | // Output_data_const, but we don't want to have to keep track of | |
1780 | // input sections just to implement fills. For a fill we record the | |
1781 | // offset, and the actual data to be written out. | |
1782 | class Fill | |
1783 | { | |
1784 | public: | |
1785 | Fill(off_t section_offset, off_t length) | |
1786 | : section_offset_(section_offset), length_(length) | |
1787 | { } | |
1788 | ||
1789 | // Return section offset. | |
1790 | off_t | |
1791 | section_offset() const | |
1792 | { return this->section_offset_; } | |
1793 | ||
1794 | // Return fill length. | |
1795 | off_t | |
1796 | length() const | |
1797 | { return this->length_; } | |
1798 | ||
1799 | private: | |
1800 | // The offset within the output section. | |
1801 | off_t section_offset_; | |
1802 | // The length of the space to fill. | |
1803 | off_t length_; | |
1804 | }; | |
1805 | ||
1806 | typedef std::vector<Fill> Fill_list; | |
1807 | ||
b8e6aad9 ILT |
1808 | // Add a new output section by Input_section. |
1809 | void | |
1810 | add_output_section_data(Input_section*); | |
1811 | ||
1812 | // Add an SHF_MERGE input section. Returns true if the section was | |
1813 | // handled. | |
1814 | bool | |
1815 | add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags, | |
9a0910c3 ILT |
1816 | uint64_t entsize, uint64_t addralign, |
1817 | bool can_compress_section); | |
b8e6aad9 ILT |
1818 | |
1819 | // Add an output SHF_MERGE section POSD to this output section. | |
1820 | // IS_STRING indicates whether it is a SHF_STRINGS section, and | |
1821 | // ENTSIZE is the entity size. This returns the entry added to | |
1822 | // input_sections_. | |
1823 | void | |
1824 | add_output_merge_section(Output_section_data* posd, bool is_string, | |
1825 | uint64_t entsize); | |
1826 | ||
a2fb1b05 ILT |
1827 | // Most of these fields are only valid after layout. |
1828 | ||
9a0910c3 ILT |
1829 | // General options. |
1830 | const General_options& options_; | |
a2fb1b05 | 1831 | // The name of the section. This will point into a Stringpool. |
9a0910c3 | 1832 | const char* name_; |
75f65a3e | 1833 | // The section address is in the parent class. |
a2fb1b05 ILT |
1834 | // The section alignment. |
1835 | uint64_t addralign_; | |
1836 | // The section entry size. | |
1837 | uint64_t entsize_; | |
75f65a3e | 1838 | // The file offset is in the parent class. |
16649710 | 1839 | // Set the section link field to the index of this section. |
14b31740 | 1840 | const Output_data* link_section_; |
16649710 | 1841 | // If link_section_ is NULL, this is the link field. |
a2fb1b05 | 1842 | unsigned int link_; |
16649710 | 1843 | // Set the section info field to the index of this section. |
14b31740 | 1844 | const Output_data* info_section_; |
16649710 | 1845 | // If info_section_ is NULL, this is the section info field. |
a2fb1b05 ILT |
1846 | unsigned int info_; |
1847 | // The section type. | |
27bc2bce | 1848 | const elfcpp::Elf_Word type_; |
a2fb1b05 | 1849 | // The section flags. |
27bc2bce | 1850 | const elfcpp::Elf_Xword flags_; |
61ba1cf9 | 1851 | // The section index. |
ead1e424 | 1852 | unsigned int out_shndx_; |
c06b7b0b ILT |
1853 | // If there is a STT_SECTION for this output section in the normal |
1854 | // symbol table, this is the symbol index. This starts out as zero. | |
1855 | // It is initialized in Layout::finalize() to be the index, or -1U | |
1856 | // if there isn't one. | |
1857 | unsigned int symtab_index_; | |
1858 | // If there is a STT_SECTION for this output section in the dynamic | |
1859 | // symbol table, this is the symbol index. This starts out as zero. | |
1860 | // It is initialized in Layout::finalize() to be the index, or -1U | |
1861 | // if there isn't one. | |
1862 | unsigned int dynsym_index_; | |
ead1e424 ILT |
1863 | // The input sections. This will be empty in cases where we don't |
1864 | // need to keep track of them. | |
1865 | Input_section_list input_sections_; | |
1866 | // The offset of the first entry in input_sections_. | |
1867 | off_t first_input_offset_; | |
c51e6221 ILT |
1868 | // The fill data. This is separate from input_sections_ because we |
1869 | // often will need fill sections without needing to keep track of | |
1870 | // input sections. | |
1871 | Fill_list fills_; | |
c06b7b0b ILT |
1872 | // Whether this output section needs a STT_SECTION symbol in the |
1873 | // normal symbol table. This will be true if there is a relocation | |
1874 | // which needs it. | |
1875 | bool needs_symtab_index_ : 1; | |
1876 | // Whether this output section needs a STT_SECTION symbol in the | |
1877 | // dynamic symbol table. This will be true if there is a dynamic | |
1878 | // relocation which needs it. | |
1879 | bool needs_dynsym_index_ : 1; | |
16649710 ILT |
1880 | // Whether the link field of this output section should point to the |
1881 | // normal symbol table. | |
1882 | bool should_link_to_symtab_ : 1; | |
1883 | // Whether the link field of this output section should point to the | |
1884 | // dynamic symbol table. | |
1885 | bool should_link_to_dynsym_ : 1; | |
730cdc88 ILT |
1886 | // Whether this section should be written after all the input |
1887 | // sections are complete. | |
1888 | bool after_input_sections_ : 1; | |
27bc2bce ILT |
1889 | // Whether this section requires post processing after all |
1890 | // relocations have been applied. | |
1891 | bool requires_postprocessing_ : 1; | |
a2fb1b05 ILT |
1892 | }; |
1893 | ||
1894 | // An output segment. PT_LOAD segments are built from collections of | |
1895 | // output sections. Other segments typically point within PT_LOAD | |
1896 | // segments, and are built directly as needed. | |
1897 | ||
1898 | class Output_segment | |
1899 | { | |
1900 | public: | |
1901 | // Create an output segment, specifying the type and flags. | |
1902 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); | |
1903 | ||
1904 | // Return the virtual address. | |
1905 | uint64_t | |
1906 | vaddr() const | |
1907 | { return this->vaddr_; } | |
1908 | ||
1909 | // Return the physical address. | |
1910 | uint64_t | |
1911 | paddr() const | |
1912 | { return this->paddr_; } | |
1913 | ||
1914 | // Return the segment type. | |
1915 | elfcpp::Elf_Word | |
1916 | type() const | |
1917 | { return this->type_; } | |
1918 | ||
1919 | // Return the segment flags. | |
1920 | elfcpp::Elf_Word | |
1921 | flags() const | |
1922 | { return this->flags_; } | |
1923 | ||
92e059d8 ILT |
1924 | // Return the memory size. |
1925 | uint64_t | |
1926 | memsz() const | |
1927 | { return this->memsz_; } | |
1928 | ||
ead1e424 ILT |
1929 | // Return the file size. |
1930 | off_t | |
1931 | filesz() const | |
1932 | { return this->filesz_; } | |
1933 | ||
75f65a3e ILT |
1934 | // Return the maximum alignment of the Output_data. |
1935 | uint64_t | |
ead1e424 | 1936 | addralign(); |
75f65a3e | 1937 | |
a2fb1b05 ILT |
1938 | // Add an Output_section to this segment. |
1939 | void | |
dbe717ef ILT |
1940 | add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) |
1941 | { this->add_output_section(os, seg_flags, false); } | |
1942 | ||
1943 | // Add an Output_section to the start of this segment. | |
1944 | void | |
1945 | add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) | |
1946 | { this->add_output_section(os, seg_flags, true); } | |
75f65a3e ILT |
1947 | |
1948 | // Add an Output_data (which is not an Output_section) to the start | |
1949 | // of this segment. | |
1950 | void | |
1951 | add_initial_output_data(Output_data*); | |
1952 | ||
4f4c5f80 ILT |
1953 | // Return the number of dynamic relocations applied to this segment. |
1954 | unsigned int | |
1955 | dynamic_reloc_count() const; | |
1956 | ||
75f65a3e ILT |
1957 | // Set the address of the segment to ADDR and the offset to *POFF |
1958 | // (aligned if necessary), and set the addresses and offsets of all | |
ead1e424 ILT |
1959 | // contained output sections accordingly. Set the section indexes |
1960 | // of all contained output sections starting with *PSHNDX. Return | |
1961 | // the address of the immediately following segment. Update *POFF | |
1962 | // and *PSHNDX. This should only be called for a PT_LOAD segment. | |
75f65a3e | 1963 | uint64_t |
ead1e424 | 1964 | set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx); |
75f65a3e | 1965 | |
0496d5e5 ILT |
1966 | // Set the minimum alignment of this segment. This may be adjusted |
1967 | // upward based on the section alignments. | |
1968 | void | |
1969 | set_minimum_addralign(uint64_t align) | |
1970 | { | |
1971 | gold_assert(!this->is_align_known_); | |
1972 | this->align_ = align; | |
1973 | } | |
1974 | ||
75f65a3e ILT |
1975 | // Set the offset of this segment based on the section. This should |
1976 | // only be called for a non-PT_LOAD segment. | |
1977 | void | |
1978 | set_offset(); | |
1979 | ||
1980 | // Return the number of output sections. | |
1981 | unsigned int | |
1982 | output_section_count() const; | |
a2fb1b05 | 1983 | |
61ba1cf9 ILT |
1984 | // Write the segment header into *OPHDR. |
1985 | template<int size, bool big_endian> | |
1986 | void | |
ead1e424 | 1987 | write_header(elfcpp::Phdr_write<size, big_endian>*); |
61ba1cf9 ILT |
1988 | |
1989 | // Write the section headers of associated sections into V. | |
1990 | template<int size, bool big_endian> | |
1991 | unsigned char* | |
16649710 | 1992 | write_section_headers(const Layout*, const Stringpool*, unsigned char* v, |
ead1e424 | 1993 | unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const; |
61ba1cf9 | 1994 | |
a2fb1b05 ILT |
1995 | private: |
1996 | Output_segment(const Output_segment&); | |
1997 | Output_segment& operator=(const Output_segment&); | |
1998 | ||
54dc6425 | 1999 | typedef std::list<Output_data*> Output_data_list; |
a2fb1b05 | 2000 | |
dbe717ef ILT |
2001 | // Add an Output_section to this segment, specifying front or back. |
2002 | void | |
2003 | add_output_section(Output_section*, elfcpp::Elf_Word seg_flags, | |
2004 | bool front); | |
2005 | ||
ead1e424 ILT |
2006 | // Find the maximum alignment in an Output_data_list. |
2007 | static uint64_t | |
2008 | maximum_alignment(const Output_data_list*); | |
2009 | ||
75f65a3e ILT |
2010 | // Set the section addresses in an Output_data_list. |
2011 | uint64_t | |
ead1e424 ILT |
2012 | set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff, |
2013 | unsigned int* pshndx); | |
75f65a3e ILT |
2014 | |
2015 | // Return the number of Output_sections in an Output_data_list. | |
2016 | unsigned int | |
2017 | output_section_count_list(const Output_data_list*) const; | |
2018 | ||
4f4c5f80 ILT |
2019 | // Return the number of dynamic relocs in an Output_data_list. |
2020 | unsigned int | |
2021 | dynamic_reloc_count_list(const Output_data_list*) const; | |
2022 | ||
61ba1cf9 ILT |
2023 | // Write the section headers in the list into V. |
2024 | template<int size, bool big_endian> | |
2025 | unsigned char* | |
16649710 ILT |
2026 | write_section_headers_list(const Layout*, const Stringpool*, |
2027 | const Output_data_list*, unsigned char* v, | |
ead1e424 | 2028 | unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const; |
61ba1cf9 | 2029 | |
75f65a3e | 2030 | // The list of output data with contents attached to this segment. |
54dc6425 | 2031 | Output_data_list output_data_; |
75f65a3e ILT |
2032 | // The list of output data without contents attached to this segment. |
2033 | Output_data_list output_bss_; | |
a2fb1b05 ILT |
2034 | // The segment virtual address. |
2035 | uint64_t vaddr_; | |
2036 | // The segment physical address. | |
2037 | uint64_t paddr_; | |
2038 | // The size of the segment in memory. | |
2039 | uint64_t memsz_; | |
0496d5e5 ILT |
2040 | // The segment alignment. The is_align_known_ field indicates |
2041 | // whether this has been finalized. It can be set to a minimum | |
2042 | // value before it is finalized. | |
a2fb1b05 ILT |
2043 | uint64_t align_; |
2044 | // The offset of the segment data within the file. | |
2045 | off_t offset_; | |
2046 | // The size of the segment data in the file. | |
2047 | off_t filesz_; | |
2048 | // The segment type; | |
2049 | elfcpp::Elf_Word type_; | |
2050 | // The segment flags. | |
2051 | elfcpp::Elf_Word flags_; | |
0496d5e5 | 2052 | // Whether we have finalized align_. |
ead1e424 | 2053 | bool is_align_known_; |
a2fb1b05 ILT |
2054 | }; |
2055 | ||
61ba1cf9 | 2056 | // This class represents the output file. |
a2fb1b05 ILT |
2057 | |
2058 | class Output_file | |
2059 | { | |
2060 | public: | |
c51e6221 ILT |
2061 | Output_file(const General_options& options, Target*); |
2062 | ||
2063 | // Get a pointer to the target. | |
2064 | Target* | |
2065 | target() const | |
2066 | { return this->target_; } | |
61ba1cf9 ILT |
2067 | |
2068 | // Open the output file. FILE_SIZE is the final size of the file. | |
2069 | void | |
2070 | open(off_t file_size); | |
2071 | ||
27bc2bce ILT |
2072 | // Resize the output file. |
2073 | void | |
2074 | resize(off_t file_size); | |
2075 | ||
61ba1cf9 ILT |
2076 | // Close the output file and make sure there are no error. |
2077 | void | |
2078 | close(); | |
2079 | ||
2080 | // We currently always use mmap which makes the view handling quite | |
2081 | // simple. In the future we may support other approaches. | |
a2fb1b05 ILT |
2082 | |
2083 | // Write data to the output file. | |
2084 | void | |
61ba1cf9 ILT |
2085 | write(off_t offset, const void* data, off_t len) |
2086 | { memcpy(this->base_ + offset, data, len); } | |
2087 | ||
2088 | // Get a buffer to use to write to the file, given the offset into | |
2089 | // the file and the size. | |
2090 | unsigned char* | |
2091 | get_output_view(off_t start, off_t size) | |
2092 | { | |
a3ad94ed | 2093 | gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_); |
61ba1cf9 ILT |
2094 | return this->base_ + start; |
2095 | } | |
2096 | ||
2097 | // VIEW must have been returned by get_output_view. Write the | |
2098 | // buffer to the file, passing in the offset and the size. | |
2099 | void | |
2100 | write_output_view(off_t, off_t, unsigned char*) | |
2101 | { } | |
2102 | ||
730cdc88 ILT |
2103 | // Get a read/write buffer. This is used when we want to write part |
2104 | // of the file, read it in, and write it again. | |
2105 | unsigned char* | |
2106 | get_input_output_view(off_t start, off_t size) | |
2107 | { return this->get_output_view(start, size); } | |
2108 | ||
2109 | // Write a read/write buffer back to the file. | |
2110 | void | |
2111 | write_input_output_view(off_t, off_t, unsigned char*) | |
2112 | { } | |
2113 | ||
2114 | // Get a read buffer. This is used when we just want to read part | |
2115 | // of the file back it in. | |
2116 | const unsigned char* | |
2117 | get_input_view(off_t start, off_t size) | |
2118 | { return this->get_output_view(start, size); } | |
2119 | ||
2120 | // Release a read bfufer. | |
2121 | void | |
2122 | free_input_view(off_t, off_t, const unsigned char*) | |
2123 | { } | |
2124 | ||
61ba1cf9 | 2125 | private: |
27bc2bce ILT |
2126 | // Map the file into memory. |
2127 | void | |
2128 | map(); | |
2129 | ||
61ba1cf9 ILT |
2130 | // General options. |
2131 | const General_options& options_; | |
c51e6221 ILT |
2132 | // Target. |
2133 | Target* target_; | |
61ba1cf9 ILT |
2134 | // File name. |
2135 | const char* name_; | |
2136 | // File descriptor. | |
2137 | int o_; | |
2138 | // File size. | |
2139 | off_t file_size_; | |
2140 | // Base of file mapped into memory. | |
2141 | unsigned char* base_; | |
a2fb1b05 ILT |
2142 | }; |
2143 | ||
2144 | } // End namespace gold. | |
2145 | ||
2146 | #endif // !defined(GOLD_OUTPUT_H) |