Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // output.h -- manage the output file for gold -*- C++ -*- |
2 | ||
e29e076a | 3 | // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
6cb15b7f ILT |
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" | |
7d9e3d98 | 30 | #include "mapfile.h" |
54dc6425 | 31 | #include "layout.h" |
c06b7b0b | 32 | #include "reloc-types.h" |
a2fb1b05 ILT |
33 | |
34 | namespace gold | |
35 | { | |
36 | ||
61ba1cf9 | 37 | class General_options; |
a2fb1b05 | 38 | class Object; |
a3ad94ed | 39 | class Symbol; |
a2fb1b05 | 40 | class Output_file; |
c0a62865 | 41 | class Output_merge_base; |
c06b7b0b | 42 | class Output_section; |
6a74a719 | 43 | class Relocatable_relocs; |
a3ad94ed | 44 | class Target; |
54dc6425 ILT |
45 | template<int size, bool big_endian> |
46 | class Sized_target; | |
c06b7b0b ILT |
47 | template<int size, bool big_endian> |
48 | class Sized_relobj; | |
54dc6425 | 49 | |
c0a62865 DK |
50 | // This class specifies an input section. It is used as a key type |
51 | // for maps. | |
52 | ||
53 | class Input_section_specifier | |
54 | { | |
55 | public: | |
2ea97941 ILT |
56 | Input_section_specifier(const Relobj* relobj, unsigned int shndx) |
57 | : relobj_(relobj), shndx_(shndx) | |
c0a62865 DK |
58 | { } |
59 | ||
60 | // Return Relobj of this. | |
61 | const Relobj* | |
62 | relobj() const | |
63 | { return this->relobj_; } | |
64 | ||
65 | // Return section index of this. | |
66 | unsigned int | |
67 | shndx() const | |
68 | { return this->shndx_; } | |
69 | ||
70 | // Whether this equals to another specifier ISS. | |
71 | bool | |
72 | eq(const Input_section_specifier& iss) const | |
73 | { return this->relobj_ == iss.relobj_ && this->shndx_ == iss.shndx_; } | |
74 | ||
75 | // Compute a hash value of this. | |
76 | size_t | |
77 | hash_value() const | |
b569affa DK |
78 | { |
79 | return (gold::string_hash<char>(this->relobj_->name().c_str()) | |
80 | ^ this->shndx_); | |
81 | } | |
c0a62865 DK |
82 | |
83 | // Functors for containers. | |
84 | struct equal_to | |
85 | { | |
86 | bool | |
87 | operator()(const Input_section_specifier& iss1, | |
88 | const Input_section_specifier& iss2) const | |
89 | { return iss1.eq(iss2); } | |
90 | }; | |
91 | ||
92 | struct hash | |
93 | { | |
94 | size_t | |
95 | operator()(const Input_section_specifier& iss) const | |
96 | { return iss.hash_value(); } | |
97 | }; | |
98 | ||
99 | private: | |
c0a62865 DK |
100 | // An object. |
101 | const Relobj* relobj_; | |
102 | // A section index. | |
103 | unsigned int shndx_; | |
104 | }; | |
105 | ||
54dc6425 | 106 | // An abtract class for data which has to go into the output file. |
a2fb1b05 ILT |
107 | |
108 | class Output_data | |
109 | { | |
110 | public: | |
27bc2bce ILT |
111 | explicit Output_data() |
112 | : address_(0), data_size_(0), offset_(-1), | |
113 | is_address_valid_(false), is_data_size_valid_(false), | |
20e6d0d6 | 114 | is_offset_valid_(false), is_data_size_fixed_(false), |
4f4c5f80 | 115 | dynamic_reloc_count_(0) |
a2fb1b05 ILT |
116 | { } |
117 | ||
118 | virtual | |
119 | ~Output_data(); | |
120 | ||
27bc2bce ILT |
121 | // Return the address. For allocated sections, this is only valid |
122 | // after Layout::finalize is finished. | |
75f65a3e ILT |
123 | uint64_t |
124 | address() const | |
27bc2bce ILT |
125 | { |
126 | gold_assert(this->is_address_valid_); | |
127 | return this->address_; | |
128 | } | |
75f65a3e | 129 | |
27bc2bce ILT |
130 | // Return the size of the data. For allocated sections, this must |
131 | // be valid after Layout::finalize calls set_address, but need not | |
132 | // be valid before then. | |
a2fb1b05 | 133 | off_t |
75f65a3e | 134 | data_size() const |
27bc2bce ILT |
135 | { |
136 | gold_assert(this->is_data_size_valid_); | |
137 | return this->data_size_; | |
138 | } | |
75f65a3e | 139 | |
20e6d0d6 DK |
140 | // Return true if data size is fixed. |
141 | bool | |
142 | is_data_size_fixed() const | |
143 | { return this->is_data_size_fixed_; } | |
144 | ||
ead1e424 | 145 | // Return the file offset. This is only valid after |
27bc2bce ILT |
146 | // Layout::finalize is finished. For some non-allocated sections, |
147 | // it may not be valid until near the end of the link. | |
75f65a3e ILT |
148 | off_t |
149 | offset() const | |
27bc2bce ILT |
150 | { |
151 | gold_assert(this->is_offset_valid_); | |
152 | return this->offset_; | |
153 | } | |
75f65a3e | 154 | |
a445fddf ILT |
155 | // Reset the address and file offset. This essentially disables the |
156 | // sanity testing about duplicate and unknown settings. | |
157 | void | |
158 | reset_address_and_file_offset() | |
159 | { | |
160 | this->is_address_valid_ = false; | |
161 | this->is_offset_valid_ = false; | |
20e6d0d6 DK |
162 | if (!this->is_data_size_fixed_) |
163 | this->is_data_size_valid_ = false; | |
a445fddf ILT |
164 | this->do_reset_address_and_file_offset(); |
165 | } | |
166 | ||
20e6d0d6 DK |
167 | // Return true if address and file offset already have reset values. In |
168 | // other words, calling reset_address_and_file_offset will not change them. | |
169 | bool | |
170 | address_and_file_offset_have_reset_values() const | |
171 | { return this->do_address_and_file_offset_have_reset_values(); } | |
172 | ||
75f65a3e ILT |
173 | // Return the required alignment. |
174 | uint64_t | |
175 | addralign() const | |
176 | { return this->do_addralign(); } | |
177 | ||
a445fddf ILT |
178 | // Return whether this has a load address. |
179 | bool | |
180 | has_load_address() const | |
181 | { return this->do_has_load_address(); } | |
182 | ||
183 | // Return the load address. | |
184 | uint64_t | |
185 | load_address() const | |
186 | { return this->do_load_address(); } | |
187 | ||
75f65a3e ILT |
188 | // Return whether this is an Output_section. |
189 | bool | |
190 | is_section() const | |
191 | { return this->do_is_section(); } | |
192 | ||
193 | // Return whether this is an Output_section of the specified type. | |
194 | bool | |
195 | is_section_type(elfcpp::Elf_Word stt) const | |
196 | { return this->do_is_section_type(stt); } | |
197 | ||
198 | // Return whether this is an Output_section with the specified flag | |
199 | // set. | |
200 | bool | |
201 | is_section_flag_set(elfcpp::Elf_Xword shf) const | |
202 | { return this->do_is_section_flag_set(shf); } | |
203 | ||
77e65537 ILT |
204 | // Return the output section that this goes in, if there is one. |
205 | Output_section* | |
206 | output_section() | |
207 | { return this->do_output_section(); } | |
208 | ||
ea715a34 ILT |
209 | const Output_section* |
210 | output_section() const | |
211 | { return this->do_output_section(); } | |
212 | ||
ead1e424 ILT |
213 | // Return the output section index, if there is an output section. |
214 | unsigned int | |
215 | out_shndx() const | |
216 | { return this->do_out_shndx(); } | |
217 | ||
218 | // Set the output section index, if this is an output section. | |
219 | void | |
220 | set_out_shndx(unsigned int shndx) | |
221 | { this->do_set_out_shndx(shndx); } | |
222 | ||
27bc2bce ILT |
223 | // Set the address and file offset of this data, and finalize the |
224 | // size of the data. This is called during Layout::finalize for | |
225 | // allocated sections. | |
75f65a3e | 226 | void |
27bc2bce ILT |
227 | set_address_and_file_offset(uint64_t addr, off_t off) |
228 | { | |
229 | this->set_address(addr); | |
230 | this->set_file_offset(off); | |
231 | this->finalize_data_size(); | |
232 | } | |
233 | ||
234 | // Set the address. | |
235 | void | |
236 | set_address(uint64_t addr) | |
237 | { | |
238 | gold_assert(!this->is_address_valid_); | |
239 | this->address_ = addr; | |
240 | this->is_address_valid_ = true; | |
241 | } | |
242 | ||
243 | // Set the file offset. | |
244 | void | |
245 | set_file_offset(off_t off) | |
246 | { | |
247 | gold_assert(!this->is_offset_valid_); | |
248 | this->offset_ = off; | |
249 | this->is_offset_valid_ = true; | |
250 | } | |
251 | ||
252 | // Finalize the data size. | |
253 | void | |
254 | finalize_data_size() | |
255 | { | |
256 | if (!this->is_data_size_valid_) | |
257 | { | |
258 | // Tell the child class to set the data size. | |
259 | this->set_final_data_size(); | |
260 | gold_assert(this->is_data_size_valid_); | |
261 | } | |
262 | } | |
75f65a3e | 263 | |
7bf1f802 ILT |
264 | // Set the TLS offset. Called only for SHT_TLS sections. |
265 | void | |
266 | set_tls_offset(uint64_t tls_base) | |
267 | { this->do_set_tls_offset(tls_base); } | |
268 | ||
269 | // Return the TLS offset, relative to the base of the TLS segment. | |
270 | // Valid only for SHT_TLS sections. | |
271 | uint64_t | |
272 | tls_offset() const | |
273 | { return this->do_tls_offset(); } | |
274 | ||
ead1e424 ILT |
275 | // Write the data to the output file. This is called after |
276 | // Layout::finalize is complete. | |
75f65a3e ILT |
277 | void |
278 | write(Output_file* file) | |
279 | { this->do_write(file); } | |
a2fb1b05 | 280 | |
27bc2bce ILT |
281 | // This is called by Layout::finalize to note that the sizes of |
282 | // allocated sections must now be fixed. | |
a3ad94ed ILT |
283 | static void |
284 | layout_complete() | |
27bc2bce | 285 | { Output_data::allocated_sizes_are_fixed = true; } |
a3ad94ed | 286 | |
730cdc88 ILT |
287 | // Used to check that layout has been done. |
288 | static bool | |
289 | is_layout_complete() | |
27bc2bce | 290 | { return Output_data::allocated_sizes_are_fixed; } |
730cdc88 | 291 | |
4f4c5f80 ILT |
292 | // Count the number of dynamic relocations applied to this section. |
293 | void | |
294 | add_dynamic_reloc() | |
295 | { ++this->dynamic_reloc_count_; } | |
296 | ||
297 | // Return the number of dynamic relocations applied to this section. | |
298 | unsigned int | |
299 | dynamic_reloc_count() const | |
300 | { return this->dynamic_reloc_count_; } | |
301 | ||
a9a60db6 ILT |
302 | // Whether the address is valid. |
303 | bool | |
304 | is_address_valid() const | |
305 | { return this->is_address_valid_; } | |
306 | ||
307 | // Whether the file offset is valid. | |
308 | bool | |
309 | is_offset_valid() const | |
310 | { return this->is_offset_valid_; } | |
311 | ||
312 | // Whether the data size is valid. | |
313 | bool | |
314 | is_data_size_valid() const | |
315 | { return this->is_data_size_valid_; } | |
316 | ||
7d9e3d98 ILT |
317 | // Print information to the map file. |
318 | void | |
319 | print_to_mapfile(Mapfile* mapfile) const | |
320 | { return this->do_print_to_mapfile(mapfile); } | |
321 | ||
75f65a3e ILT |
322 | protected: |
323 | // Functions that child classes may or in some cases must implement. | |
324 | ||
325 | // Write the data to the output file. | |
a2fb1b05 | 326 | virtual void |
75f65a3e ILT |
327 | do_write(Output_file*) = 0; |
328 | ||
329 | // Return the required alignment. | |
330 | virtual uint64_t | |
331 | do_addralign() const = 0; | |
332 | ||
a445fddf ILT |
333 | // Return whether this has a load address. |
334 | virtual bool | |
335 | do_has_load_address() const | |
336 | { return false; } | |
337 | ||
338 | // Return the load address. | |
339 | virtual uint64_t | |
340 | do_load_address() const | |
341 | { gold_unreachable(); } | |
342 | ||
75f65a3e ILT |
343 | // Return whether this is an Output_section. |
344 | virtual bool | |
345 | do_is_section() const | |
346 | { return false; } | |
a2fb1b05 | 347 | |
54dc6425 | 348 | // Return whether this is an Output_section of the specified type. |
75f65a3e | 349 | // This only needs to be implement by Output_section. |
54dc6425 | 350 | virtual bool |
75f65a3e | 351 | do_is_section_type(elfcpp::Elf_Word) const |
54dc6425 ILT |
352 | { return false; } |
353 | ||
75f65a3e ILT |
354 | // Return whether this is an Output_section with the specific flag |
355 | // set. This only needs to be implemented by Output_section. | |
54dc6425 | 356 | virtual bool |
75f65a3e | 357 | do_is_section_flag_set(elfcpp::Elf_Xword) const |
54dc6425 ILT |
358 | { return false; } |
359 | ||
77e65537 ILT |
360 | // Return the output section, if there is one. |
361 | virtual Output_section* | |
362 | do_output_section() | |
363 | { return NULL; } | |
364 | ||
ea715a34 ILT |
365 | virtual const Output_section* |
366 | do_output_section() const | |
367 | { return NULL; } | |
368 | ||
ead1e424 ILT |
369 | // Return the output section index, if there is an output section. |
370 | virtual unsigned int | |
371 | do_out_shndx() const | |
a3ad94ed | 372 | { gold_unreachable(); } |
ead1e424 ILT |
373 | |
374 | // Set the output section index, if this is an output section. | |
375 | virtual void | |
376 | do_set_out_shndx(unsigned int) | |
a3ad94ed | 377 | { gold_unreachable(); } |
ead1e424 | 378 | |
27bc2bce ILT |
379 | // This is a hook for derived classes to set the data size. This is |
380 | // called by finalize_data_size, normally called during | |
381 | // Layout::finalize, when the section address is set. | |
75f65a3e | 382 | virtual void |
27bc2bce ILT |
383 | set_final_data_size() |
384 | { gold_unreachable(); } | |
75f65a3e | 385 | |
a445fddf ILT |
386 | // A hook for resetting the address and file offset. |
387 | virtual void | |
388 | do_reset_address_and_file_offset() | |
389 | { } | |
390 | ||
20e6d0d6 DK |
391 | // Return true if address and file offset already have reset values. In |
392 | // other words, calling reset_address_and_file_offset will not change them. | |
393 | // A child class overriding do_reset_address_and_file_offset may need to | |
394 | // also override this. | |
395 | virtual bool | |
396 | do_address_and_file_offset_have_reset_values() const | |
397 | { return !this->is_address_valid_ && !this->is_offset_valid_; } | |
398 | ||
7bf1f802 ILT |
399 | // Set the TLS offset. Called only for SHT_TLS sections. |
400 | virtual void | |
401 | do_set_tls_offset(uint64_t) | |
402 | { gold_unreachable(); } | |
403 | ||
404 | // Return the TLS offset, relative to the base of the TLS segment. | |
405 | // Valid only for SHT_TLS sections. | |
406 | virtual uint64_t | |
407 | do_tls_offset() const | |
408 | { gold_unreachable(); } | |
409 | ||
7d9e3d98 ILT |
410 | // Print to the map file. This only needs to be implemented by |
411 | // classes which may appear in a PT_LOAD segment. | |
412 | virtual void | |
413 | do_print_to_mapfile(Mapfile*) const | |
414 | { gold_unreachable(); } | |
415 | ||
75f65a3e ILT |
416 | // Functions that child classes may call. |
417 | ||
9c547ec3 ILT |
418 | // Reset the address. The Output_section class needs this when an |
419 | // SHF_ALLOC input section is added to an output section which was | |
420 | // formerly not SHF_ALLOC. | |
421 | void | |
422 | mark_address_invalid() | |
423 | { this->is_address_valid_ = false; } | |
424 | ||
a2fb1b05 ILT |
425 | // Set the size of the data. |
426 | void | |
2ea97941 | 427 | set_data_size(off_t data_size) |
a3ad94ed | 428 | { |
20e6d0d6 DK |
429 | gold_assert(!this->is_data_size_valid_ |
430 | && !this->is_data_size_fixed_); | |
2ea97941 | 431 | this->data_size_ = data_size; |
27bc2bce ILT |
432 | this->is_data_size_valid_ = true; |
433 | } | |
434 | ||
20e6d0d6 DK |
435 | // Fix the data size. Once it is fixed, it cannot be changed |
436 | // and the data size remains always valid. | |
437 | void | |
438 | fix_data_size() | |
439 | { | |
440 | gold_assert(this->is_data_size_valid_); | |
441 | this->is_data_size_fixed_ = true; | |
442 | } | |
443 | ||
27bc2bce ILT |
444 | // Get the current data size--this is for the convenience of |
445 | // sections which build up their size over time. | |
446 | off_t | |
447 | current_data_size_for_child() const | |
448 | { return this->data_size_; } | |
449 | ||
450 | // Set the current data size--this is for the convenience of | |
451 | // sections which build up their size over time. | |
452 | void | |
2ea97941 | 453 | set_current_data_size_for_child(off_t data_size) |
27bc2bce ILT |
454 | { |
455 | gold_assert(!this->is_data_size_valid_); | |
2ea97941 | 456 | this->data_size_ = data_size; |
a3ad94ed | 457 | } |
75f65a3e | 458 | |
730cdc88 ILT |
459 | // Return default alignment for the target size. |
460 | static uint64_t | |
461 | default_alignment(); | |
462 | ||
463 | // Return default alignment for a specified size--32 or 64. | |
75f65a3e | 464 | static uint64_t |
730cdc88 | 465 | default_alignment_for_size(int size); |
a2fb1b05 ILT |
466 | |
467 | private: | |
468 | Output_data(const Output_data&); | |
469 | Output_data& operator=(const Output_data&); | |
470 | ||
a3ad94ed | 471 | // This is used for verification, to make sure that we don't try to |
27bc2bce ILT |
472 | // change any sizes of allocated sections after we set the section |
473 | // addresses. | |
474 | static bool allocated_sizes_are_fixed; | |
a3ad94ed | 475 | |
27bc2bce | 476 | // Memory address in output file. |
75f65a3e | 477 | uint64_t address_; |
27bc2bce | 478 | // Size of data in output file. |
75f65a3e | 479 | off_t data_size_; |
27bc2bce | 480 | // File offset of contents in output file. |
75f65a3e | 481 | off_t offset_; |
27bc2bce ILT |
482 | // Whether address_ is valid. |
483 | bool is_address_valid_; | |
484 | // Whether data_size_ is valid. | |
485 | bool is_data_size_valid_; | |
486 | // Whether offset_ is valid. | |
487 | bool is_offset_valid_; | |
20e6d0d6 DK |
488 | // Whether data size is fixed. |
489 | bool is_data_size_fixed_; | |
4f4c5f80 ILT |
490 | // Count of dynamic relocations applied to this section. |
491 | unsigned int dynamic_reloc_count_; | |
a2fb1b05 ILT |
492 | }; |
493 | ||
54dc6425 ILT |
494 | // Output the section headers. |
495 | ||
496 | class Output_section_headers : public Output_data | |
497 | { | |
498 | public: | |
9025d29d | 499 | Output_section_headers(const Layout*, |
16649710 ILT |
500 | const Layout::Segment_list*, |
501 | const Layout::Section_list*, | |
6a74a719 | 502 | const Layout::Section_list*, |
d491d34e ILT |
503 | const Stringpool*, |
504 | const Output_section*); | |
54dc6425 | 505 | |
27bc2bce | 506 | protected: |
54dc6425 ILT |
507 | // Write the data to the file. |
508 | void | |
75f65a3e ILT |
509 | do_write(Output_file*); |
510 | ||
511 | // Return the required alignment. | |
512 | uint64_t | |
513 | do_addralign() const | |
730cdc88 | 514 | { return Output_data::default_alignment(); } |
54dc6425 | 515 | |
7d9e3d98 ILT |
516 | // Write to a map file. |
517 | void | |
518 | do_print_to_mapfile(Mapfile* mapfile) const | |
519 | { mapfile->print_output_data(this, _("** section headers")); } | |
520 | ||
20e6d0d6 DK |
521 | // Set final data size. |
522 | void | |
523 | set_final_data_size() | |
524 | { this->set_data_size(this->do_size()); } | |
525 | ||
54dc6425 | 526 | private: |
61ba1cf9 ILT |
527 | // Write the data to the file with the right size and endianness. |
528 | template<int size, bool big_endian> | |
529 | void | |
530 | do_sized_write(Output_file*); | |
531 | ||
20e6d0d6 DK |
532 | // Compute data size. |
533 | off_t | |
534 | do_size() const; | |
535 | ||
16649710 ILT |
536 | const Layout* layout_; |
537 | const Layout::Segment_list* segment_list_; | |
6a74a719 | 538 | const Layout::Section_list* section_list_; |
16649710 | 539 | const Layout::Section_list* unattached_section_list_; |
61ba1cf9 | 540 | const Stringpool* secnamepool_; |
d491d34e | 541 | const Output_section* shstrtab_section_; |
54dc6425 ILT |
542 | }; |
543 | ||
544 | // Output the segment headers. | |
545 | ||
546 | class Output_segment_headers : public Output_data | |
547 | { | |
548 | public: | |
9025d29d | 549 | Output_segment_headers(const Layout::Segment_list& segment_list); |
54dc6425 | 550 | |
27bc2bce | 551 | protected: |
54dc6425 ILT |
552 | // Write the data to the file. |
553 | void | |
75f65a3e ILT |
554 | do_write(Output_file*); |
555 | ||
556 | // Return the required alignment. | |
557 | uint64_t | |
558 | do_addralign() const | |
730cdc88 | 559 | { return Output_data::default_alignment(); } |
54dc6425 | 560 | |
7d9e3d98 ILT |
561 | // Write to a map file. |
562 | void | |
563 | do_print_to_mapfile(Mapfile* mapfile) const | |
564 | { mapfile->print_output_data(this, _("** segment headers")); } | |
565 | ||
20e6d0d6 DK |
566 | // Set final data size. |
567 | void | |
568 | set_final_data_size() | |
569 | { this->set_data_size(this->do_size()); } | |
570 | ||
54dc6425 | 571 | private: |
61ba1cf9 ILT |
572 | // Write the data to the file with the right size and endianness. |
573 | template<int size, bool big_endian> | |
574 | void | |
575 | do_sized_write(Output_file*); | |
576 | ||
20e6d0d6 DK |
577 | // Compute the current size. |
578 | off_t | |
579 | do_size() const; | |
580 | ||
54dc6425 ILT |
581 | const Layout::Segment_list& segment_list_; |
582 | }; | |
583 | ||
584 | // Output the ELF file header. | |
585 | ||
586 | class Output_file_header : public Output_data | |
587 | { | |
588 | public: | |
9025d29d | 589 | Output_file_header(const Target*, |
54dc6425 | 590 | const Symbol_table*, |
d391083d ILT |
591 | const Output_segment_headers*, |
592 | const char* entry); | |
75f65a3e ILT |
593 | |
594 | // Add information about the section headers. We lay out the ELF | |
595 | // file header before we create the section headers. | |
596 | void set_section_info(const Output_section_headers*, | |
597 | const Output_section* shstrtab); | |
54dc6425 | 598 | |
27bc2bce | 599 | protected: |
54dc6425 ILT |
600 | // Write the data to the file. |
601 | void | |
75f65a3e ILT |
602 | do_write(Output_file*); |
603 | ||
604 | // Return the required alignment. | |
605 | uint64_t | |
606 | do_addralign() const | |
730cdc88 | 607 | { return Output_data::default_alignment(); } |
75f65a3e | 608 | |
7d9e3d98 ILT |
609 | // Write to a map file. |
610 | void | |
611 | do_print_to_mapfile(Mapfile* mapfile) const | |
612 | { mapfile->print_output_data(this, _("** file header")); } | |
613 | ||
20e6d0d6 DK |
614 | // Set final data size. |
615 | void | |
616 | set_final_data_size(void) | |
617 | { this->set_data_size(this->do_size()); } | |
618 | ||
54dc6425 | 619 | private: |
61ba1cf9 ILT |
620 | // Write the data to the file with the right size and endianness. |
621 | template<int size, bool big_endian> | |
622 | void | |
623 | do_sized_write(Output_file*); | |
624 | ||
d391083d ILT |
625 | // Return the value to use for the entry address. |
626 | template<int size> | |
627 | typename elfcpp::Elf_types<size>::Elf_Addr | |
628 | entry(); | |
629 | ||
20e6d0d6 DK |
630 | // Compute the current data size. |
631 | off_t | |
632 | do_size() const; | |
633 | ||
54dc6425 ILT |
634 | const Target* target_; |
635 | const Symbol_table* symtab_; | |
61ba1cf9 | 636 | const Output_segment_headers* segment_header_; |
54dc6425 ILT |
637 | const Output_section_headers* section_header_; |
638 | const Output_section* shstrtab_; | |
d391083d | 639 | const char* entry_; |
54dc6425 ILT |
640 | }; |
641 | ||
ead1e424 ILT |
642 | // Output sections are mainly comprised of input sections. However, |
643 | // there are cases where we have data to write out which is not in an | |
644 | // input section. Output_section_data is used in such cases. This is | |
645 | // an abstract base class. | |
646 | ||
647 | class Output_section_data : public Output_data | |
648 | { | |
649 | public: | |
2ea97941 ILT |
650 | Output_section_data(off_t data_size, uint64_t addralign, |
651 | bool is_data_size_fixed) | |
652 | : Output_data(), output_section_(NULL), addralign_(addralign) | |
20e6d0d6 | 653 | { |
2ea97941 ILT |
654 | this->set_data_size(data_size); |
655 | if (is_data_size_fixed) | |
20e6d0d6 DK |
656 | this->fix_data_size(); |
657 | } | |
ead1e424 | 658 | |
2ea97941 ILT |
659 | Output_section_data(uint64_t addralign) |
660 | : Output_data(), output_section_(NULL), addralign_(addralign) | |
ead1e424 ILT |
661 | { } |
662 | ||
16649710 ILT |
663 | // Return the output section. |
664 | const Output_section* | |
665 | output_section() const | |
666 | { return this->output_section_; } | |
667 | ||
ead1e424 ILT |
668 | // Record the output section. |
669 | void | |
16649710 | 670 | set_output_section(Output_section* os); |
ead1e424 | 671 | |
b8e6aad9 ILT |
672 | // Add an input section, for SHF_MERGE sections. This returns true |
673 | // if the section was handled. | |
674 | bool | |
675 | add_input_section(Relobj* object, unsigned int shndx) | |
676 | { return this->do_add_input_section(object, shndx); } | |
677 | ||
678 | // Given an input OBJECT, an input section index SHNDX within that | |
679 | // object, and an OFFSET relative to the start of that input | |
730cdc88 ILT |
680 | // section, return whether or not the corresponding offset within |
681 | // the output section is known. If this function returns true, it | |
682 | // sets *POUTPUT to the output offset. The value -1 indicates that | |
683 | // this input offset is being discarded. | |
8f00aeb8 | 684 | bool |
8383303e | 685 | output_offset(const Relobj* object, unsigned int shndx, |
2ea97941 | 686 | section_offset_type offset, |
8383303e | 687 | section_offset_type *poutput) const |
2ea97941 | 688 | { return this->do_output_offset(object, shndx, offset, poutput); } |
b8e6aad9 | 689 | |
a9a60db6 ILT |
690 | // Return whether this is the merge section for the input section |
691 | // SHNDX in OBJECT. This should return true when output_offset | |
692 | // would return true for some values of OFFSET. | |
693 | bool | |
694 | is_merge_section_for(const Relobj* object, unsigned int shndx) const | |
695 | { return this->do_is_merge_section_for(object, shndx); } | |
696 | ||
96803768 ILT |
697 | // Write the contents to a buffer. This is used for sections which |
698 | // require postprocessing, such as compression. | |
699 | void | |
700 | write_to_buffer(unsigned char* buffer) | |
701 | { this->do_write_to_buffer(buffer); } | |
702 | ||
38c5e8b4 ILT |
703 | // Print merge stats to stderr. This should only be called for |
704 | // SHF_MERGE sections. | |
705 | void | |
706 | print_merge_stats(const char* section_name) | |
707 | { this->do_print_merge_stats(section_name); } | |
708 | ||
ead1e424 ILT |
709 | protected: |
710 | // The child class must implement do_write. | |
711 | ||
16649710 ILT |
712 | // The child class may implement specific adjustments to the output |
713 | // section. | |
714 | virtual void | |
715 | do_adjust_output_section(Output_section*) | |
716 | { } | |
717 | ||
b8e6aad9 ILT |
718 | // May be implemented by child class. Return true if the section |
719 | // was handled. | |
720 | virtual bool | |
721 | do_add_input_section(Relobj*, unsigned int) | |
722 | { gold_unreachable(); } | |
723 | ||
730cdc88 | 724 | // The child class may implement output_offset. |
b8e6aad9 | 725 | virtual bool |
8383303e ILT |
726 | do_output_offset(const Relobj*, unsigned int, section_offset_type, |
727 | section_offset_type*) const | |
b8e6aad9 ILT |
728 | { return false; } |
729 | ||
a9a60db6 ILT |
730 | // The child class may implement is_merge_section_for. |
731 | virtual bool | |
732 | do_is_merge_section_for(const Relobj*, unsigned int) const | |
733 | { return false; } | |
734 | ||
96803768 ILT |
735 | // The child class may implement write_to_buffer. Most child |
736 | // classes can not appear in a compressed section, and they do not | |
737 | // implement this. | |
738 | virtual void | |
739 | do_write_to_buffer(unsigned char*) | |
740 | { gold_unreachable(); } | |
741 | ||
38c5e8b4 ILT |
742 | // Print merge statistics. |
743 | virtual void | |
744 | do_print_merge_stats(const char*) | |
745 | { gold_unreachable(); } | |
746 | ||
ead1e424 ILT |
747 | // Return the required alignment. |
748 | uint64_t | |
749 | do_addralign() const | |
750 | { return this->addralign_; } | |
751 | ||
77e65537 ILT |
752 | // Return the output section. |
753 | Output_section* | |
754 | do_output_section() | |
755 | { return this->output_section_; } | |
756 | ||
ea715a34 ILT |
757 | const Output_section* |
758 | do_output_section() const | |
759 | { return this->output_section_; } | |
760 | ||
ead1e424 ILT |
761 | // Return the section index of the output section. |
762 | unsigned int | |
763 | do_out_shndx() const; | |
764 | ||
5a6f7e2d ILT |
765 | // Set the alignment. |
766 | void | |
759b1a24 | 767 | set_addralign(uint64_t addralign); |
5a6f7e2d | 768 | |
ead1e424 ILT |
769 | private: |
770 | // The output section for this section. | |
77e65537 | 771 | Output_section* output_section_; |
ead1e424 ILT |
772 | // The required alignment. |
773 | uint64_t addralign_; | |
774 | }; | |
775 | ||
27bc2bce ILT |
776 | // Some Output_section_data classes build up their data step by step, |
777 | // rather than all at once. This class provides an interface for | |
778 | // them. | |
779 | ||
780 | class Output_section_data_build : public Output_section_data | |
781 | { | |
782 | public: | |
2ea97941 ILT |
783 | Output_section_data_build(uint64_t addralign) |
784 | : Output_section_data(addralign) | |
27bc2bce ILT |
785 | { } |
786 | ||
787 | // Get the current data size. | |
788 | off_t | |
789 | current_data_size() const | |
790 | { return this->current_data_size_for_child(); } | |
791 | ||
792 | // Set the current data size. | |
793 | void | |
2ea97941 ILT |
794 | set_current_data_size(off_t data_size) |
795 | { this->set_current_data_size_for_child(data_size); } | |
27bc2bce ILT |
796 | |
797 | protected: | |
798 | // Set the final data size. | |
799 | virtual void | |
800 | set_final_data_size() | |
801 | { this->set_data_size(this->current_data_size_for_child()); } | |
802 | }; | |
803 | ||
dbe717ef ILT |
804 | // A simple case of Output_data in which we have constant data to |
805 | // output. | |
ead1e424 | 806 | |
dbe717ef | 807 | class Output_data_const : public Output_section_data |
ead1e424 ILT |
808 | { |
809 | public: | |
2ea97941 ILT |
810 | Output_data_const(const std::string& data, uint64_t addralign) |
811 | : Output_section_data(data.size(), addralign, true), data_(data) | |
dbe717ef ILT |
812 | { } |
813 | ||
2ea97941 ILT |
814 | Output_data_const(const char* p, off_t len, uint64_t addralign) |
815 | : Output_section_data(len, addralign, true), data_(p, len) | |
dbe717ef ILT |
816 | { } |
817 | ||
2ea97941 ILT |
818 | Output_data_const(const unsigned char* p, off_t len, uint64_t addralign) |
819 | : Output_section_data(len, addralign, true), | |
dbe717ef ILT |
820 | data_(reinterpret_cast<const char*>(p), len) |
821 | { } | |
822 | ||
27bc2bce | 823 | protected: |
a3ad94ed | 824 | // Write the data to the output file. |
dbe717ef | 825 | void |
a3ad94ed | 826 | do_write(Output_file*); |
dbe717ef | 827 | |
96803768 ILT |
828 | // Write the data to a buffer. |
829 | void | |
830 | do_write_to_buffer(unsigned char* buffer) | |
831 | { memcpy(buffer, this->data_.data(), this->data_.size()); } | |
832 | ||
7d9e3d98 ILT |
833 | // Write to a map file. |
834 | void | |
835 | do_print_to_mapfile(Mapfile* mapfile) const | |
836 | { mapfile->print_output_data(this, _("** fill")); } | |
837 | ||
dbe717ef ILT |
838 | private: |
839 | std::string data_; | |
840 | }; | |
841 | ||
a3ad94ed ILT |
842 | // Another version of Output_data with constant data, in which the |
843 | // buffer is allocated by the caller. | |
dbe717ef | 844 | |
a3ad94ed | 845 | class Output_data_const_buffer : public Output_section_data |
dbe717ef ILT |
846 | { |
847 | public: | |
a3ad94ed | 848 | Output_data_const_buffer(const unsigned char* p, off_t len, |
2ea97941 ILT |
849 | uint64_t addralign, const char* map_name) |
850 | : Output_section_data(len, addralign, true), | |
7d9e3d98 | 851 | p_(p), map_name_(map_name) |
a3ad94ed ILT |
852 | { } |
853 | ||
27bc2bce | 854 | protected: |
a3ad94ed ILT |
855 | // Write the data the output file. |
856 | void | |
857 | do_write(Output_file*); | |
858 | ||
96803768 ILT |
859 | // Write the data to a buffer. |
860 | void | |
861 | do_write_to_buffer(unsigned char* buffer) | |
862 | { memcpy(buffer, this->p_, this->data_size()); } | |
863 | ||
7d9e3d98 ILT |
864 | // Write to a map file. |
865 | void | |
866 | do_print_to_mapfile(Mapfile* mapfile) const | |
867 | { mapfile->print_output_data(this, _(this->map_name_)); } | |
868 | ||
a3ad94ed | 869 | private: |
7d9e3d98 | 870 | // The data to output. |
a3ad94ed | 871 | const unsigned char* p_; |
7d9e3d98 ILT |
872 | // Name to use in a map file. Maps are a rarely used feature, but |
873 | // the space usage is minor as aren't very many of these objects. | |
874 | const char* map_name_; | |
a3ad94ed ILT |
875 | }; |
876 | ||
27bc2bce ILT |
877 | // A place holder for a fixed amount of data written out via some |
878 | // other mechanism. | |
a3ad94ed | 879 | |
27bc2bce | 880 | class Output_data_fixed_space : public Output_section_data |
a3ad94ed ILT |
881 | { |
882 | public: | |
2ea97941 | 883 | Output_data_fixed_space(off_t data_size, uint64_t addralign, |
7d9e3d98 | 884 | const char* map_name) |
2ea97941 | 885 | : Output_section_data(data_size, addralign, true), |
7d9e3d98 | 886 | map_name_(map_name) |
a3ad94ed ILT |
887 | { } |
888 | ||
27bc2bce ILT |
889 | protected: |
890 | // Write out the data--the actual data must be written out | |
891 | // elsewhere. | |
892 | void | |
893 | do_write(Output_file*) | |
ead1e424 | 894 | { } |
7d9e3d98 ILT |
895 | |
896 | // Write to a map file. | |
897 | void | |
898 | do_print_to_mapfile(Mapfile* mapfile) const | |
899 | { mapfile->print_output_data(this, _(this->map_name_)); } | |
900 | ||
901 | private: | |
902 | // Name to use in a map file. Maps are a rarely used feature, but | |
903 | // the space usage is minor as aren't very many of these objects. | |
904 | const char* map_name_; | |
27bc2bce | 905 | }; |
ead1e424 | 906 | |
27bc2bce ILT |
907 | // A place holder for variable sized data written out via some other |
908 | // mechanism. | |
909 | ||
910 | class Output_data_space : public Output_section_data_build | |
911 | { | |
912 | public: | |
2ea97941 ILT |
913 | explicit Output_data_space(uint64_t addralign, const char* map_name) |
914 | : Output_section_data_build(addralign), | |
7d9e3d98 | 915 | map_name_(map_name) |
27bc2bce | 916 | { } |
ead1e424 | 917 | |
5a6f7e2d ILT |
918 | // Set the alignment. |
919 | void | |
920 | set_space_alignment(uint64_t align) | |
921 | { this->set_addralign(align); } | |
922 | ||
27bc2bce ILT |
923 | protected: |
924 | // Write out the data--the actual data must be written out | |
925 | // elsewhere. | |
ead1e424 ILT |
926 | void |
927 | do_write(Output_file*) | |
928 | { } | |
7d9e3d98 ILT |
929 | |
930 | // Write to a map file. | |
931 | void | |
932 | do_print_to_mapfile(Mapfile* mapfile) const | |
933 | { mapfile->print_output_data(this, _(this->map_name_)); } | |
934 | ||
935 | private: | |
936 | // Name to use in a map file. Maps are a rarely used feature, but | |
937 | // the space usage is minor as aren't very many of these objects. | |
938 | const char* map_name_; | |
939 | }; | |
940 | ||
941 | // Fill fixed space with zeroes. This is just like | |
942 | // Output_data_fixed_space, except that the map name is known. | |
943 | ||
944 | class Output_data_zero_fill : public Output_section_data | |
945 | { | |
946 | public: | |
2ea97941 ILT |
947 | Output_data_zero_fill(off_t data_size, uint64_t addralign) |
948 | : Output_section_data(data_size, addralign, true) | |
7d9e3d98 ILT |
949 | { } |
950 | ||
951 | protected: | |
952 | // There is no data to write out. | |
953 | void | |
954 | do_write(Output_file*) | |
955 | { } | |
956 | ||
957 | // Write to a map file. | |
958 | void | |
959 | do_print_to_mapfile(Mapfile* mapfile) const | |
960 | { mapfile->print_output_data(this, "** zero fill"); } | |
ead1e424 ILT |
961 | }; |
962 | ||
a3ad94ed ILT |
963 | // A string table which goes into an output section. |
964 | ||
965 | class Output_data_strtab : public Output_section_data | |
966 | { | |
967 | public: | |
968 | Output_data_strtab(Stringpool* strtab) | |
969 | : Output_section_data(1), strtab_(strtab) | |
970 | { } | |
971 | ||
27bc2bce | 972 | protected: |
a3ad94ed ILT |
973 | // This is called to set the address and file offset. Here we make |
974 | // sure that the Stringpool is finalized. | |
975 | void | |
27bc2bce | 976 | set_final_data_size(); |
a3ad94ed ILT |
977 | |
978 | // Write out the data. | |
979 | void | |
980 | do_write(Output_file*); | |
981 | ||
96803768 ILT |
982 | // Write the data to a buffer. |
983 | void | |
984 | do_write_to_buffer(unsigned char* buffer) | |
985 | { this->strtab_->write_to_buffer(buffer, this->data_size()); } | |
986 | ||
7d9e3d98 ILT |
987 | // Write to a map file. |
988 | void | |
989 | do_print_to_mapfile(Mapfile* mapfile) const | |
990 | { mapfile->print_output_data(this, _("** string table")); } | |
991 | ||
a3ad94ed ILT |
992 | private: |
993 | Stringpool* strtab_; | |
994 | }; | |
995 | ||
c06b7b0b ILT |
996 | // This POD class is used to represent a single reloc in the output |
997 | // file. This could be a private class within Output_data_reloc, but | |
998 | // the templatization is complex enough that I broke it out into a | |
999 | // separate class. The class is templatized on either elfcpp::SHT_REL | |
1000 | // or elfcpp::SHT_RELA, and also on whether this is a dynamic | |
1001 | // relocation or an ordinary relocation. | |
1002 | ||
dceae3c1 ILT |
1003 | // A relocation can be against a global symbol, a local symbol, a |
1004 | // local section symbol, an output section, or the undefined symbol at | |
1005 | // index 0. We represent the latter by using a NULL global symbol. | |
c06b7b0b ILT |
1006 | |
1007 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
1008 | class Output_reloc; | |
1009 | ||
1010 | template<bool dynamic, int size, bool big_endian> | |
1011 | class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
1012 | { | |
1013 | public: | |
1014 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
624f8810 | 1015 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; |
c06b7b0b | 1016 | |
eff45813 CC |
1017 | static const Address invalid_address = static_cast<Address>(0) - 1; |
1018 | ||
c06b7b0b ILT |
1019 | // An uninitialized entry. We need this because we want to put |
1020 | // instances of this class into an STL container. | |
1021 | Output_reloc() | |
1022 | : local_sym_index_(INVALID_CODE) | |
1023 | { } | |
1024 | ||
dceae3c1 ILT |
1025 | // We have a bunch of different constructors. They come in pairs |
1026 | // depending on how the address of the relocation is specified. It | |
1027 | // can either be an offset in an Output_data or an offset in an | |
1028 | // input section. | |
1029 | ||
c06b7b0b | 1030 | // A reloc against a global symbol. |
5a6f7e2d | 1031 | |
a3ad94ed | 1032 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
e8c846c3 | 1033 | Address address, bool is_relative); |
5a6f7e2d | 1034 | |
ef9beddf ILT |
1035 | Output_reloc(Symbol* gsym, unsigned int type, |
1036 | Sized_relobj<size, big_endian>* relobj, | |
e8c846c3 | 1037 | unsigned int shndx, Address address, bool is_relative); |
c06b7b0b | 1038 | |
dceae3c1 | 1039 | // A reloc against a local symbol or local section symbol. |
5a6f7e2d ILT |
1040 | |
1041 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
7bf1f802 | 1042 | unsigned int local_sym_index, unsigned int type, |
dceae3c1 ILT |
1043 | Output_data* od, Address address, bool is_relative, |
1044 | bool is_section_symbol); | |
5a6f7e2d ILT |
1045 | |
1046 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
7bf1f802 | 1047 | unsigned int local_sym_index, unsigned int type, |
dceae3c1 ILT |
1048 | unsigned int shndx, Address address, bool is_relative, |
1049 | bool is_section_symbol); | |
c06b7b0b ILT |
1050 | |
1051 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 1052 | |
a3ad94ed | 1053 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
7bf1f802 | 1054 | Address address); |
5a6f7e2d | 1055 | |
ef9beddf ILT |
1056 | Output_reloc(Output_section* os, unsigned int type, |
1057 | Sized_relobj<size, big_endian>* relobj, | |
7bf1f802 | 1058 | unsigned int shndx, Address address); |
c06b7b0b | 1059 | |
e8c846c3 ILT |
1060 | // Return TRUE if this is a RELATIVE relocation. |
1061 | bool | |
1062 | is_relative() const | |
1063 | { return this->is_relative_; } | |
1064 | ||
dceae3c1 ILT |
1065 | // Return whether this is against a local section symbol. |
1066 | bool | |
1067 | is_local_section_symbol() const | |
1068 | { | |
1069 | return (this->local_sym_index_ != GSYM_CODE | |
1070 | && this->local_sym_index_ != SECTION_CODE | |
1071 | && this->local_sym_index_ != INVALID_CODE | |
1072 | && this->is_section_symbol_); | |
1073 | } | |
1074 | ||
1075 | // For a local section symbol, return the offset of the input | |
624f8810 ILT |
1076 | // section within the output section. ADDEND is the addend being |
1077 | // applied to the input section. | |
ef9beddf | 1078 | Address |
624f8810 | 1079 | local_section_offset(Addend addend) const; |
dceae3c1 | 1080 | |
d1f003c6 ILT |
1081 | // Get the value of the symbol referred to by a Rel relocation when |
1082 | // we are adding the given ADDEND. | |
e8c846c3 | 1083 | Address |
624f8810 | 1084 | symbol_value(Addend addend) const; |
e8c846c3 | 1085 | |
c06b7b0b ILT |
1086 | // Write the reloc entry to an output view. |
1087 | void | |
1088 | write(unsigned char* pov) const; | |
1089 | ||
1090 | // Write the offset and info fields to Write_rel. | |
1091 | template<typename Write_rel> | |
1092 | void write_rel(Write_rel*) const; | |
1093 | ||
d98bc257 ILT |
1094 | // This is used when sorting dynamic relocs. Return -1 to sort this |
1095 | // reloc before R2, 0 to sort the same as R2, 1 to sort after R2. | |
1096 | int | |
1097 | compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2) | |
1098 | const; | |
1099 | ||
1100 | // Return whether this reloc should be sorted before the argument | |
1101 | // when sorting dynamic relocs. | |
1102 | bool | |
1103 | sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& | |
1104 | r2) const | |
1105 | { return this->compare(r2) < 0; } | |
1106 | ||
c06b7b0b | 1107 | private: |
dceae3c1 ILT |
1108 | // Record that we need a dynamic symbol index. |
1109 | void | |
1110 | set_needs_dynsym_index(); | |
1111 | ||
1112 | // Return the symbol index. | |
c06b7b0b ILT |
1113 | unsigned int |
1114 | get_symbol_index() const; | |
1115 | ||
d98bc257 | 1116 | // Return the output address. |
a984ee1d | 1117 | Address |
d98bc257 ILT |
1118 | get_address() const; |
1119 | ||
c06b7b0b ILT |
1120 | // Codes for local_sym_index_. |
1121 | enum | |
1122 | { | |
1123 | // Global symbol. | |
1124 | GSYM_CODE = -1U, | |
1125 | // Output section. | |
1126 | SECTION_CODE = -2U, | |
1127 | // Invalid uninitialized entry. | |
1128 | INVALID_CODE = -3U | |
1129 | }; | |
1130 | ||
1131 | union | |
1132 | { | |
dceae3c1 ILT |
1133 | // For a local symbol or local section symbol |
1134 | // (this->local_sym_index_ >= 0), the object. We will never | |
1135 | // generate a relocation against a local symbol in a dynamic | |
1136 | // object; that doesn't make sense. And our callers will always | |
1137 | // be templatized, so we use Sized_relobj here. | |
5a6f7e2d | 1138 | Sized_relobj<size, big_endian>* relobj; |
dceae3c1 ILT |
1139 | // For a global symbol (this->local_sym_index_ == GSYM_CODE, the |
1140 | // symbol. If this is NULL, it indicates a relocation against the | |
1141 | // undefined 0 symbol. | |
c06b7b0b | 1142 | Symbol* gsym; |
dceae3c1 ILT |
1143 | // For a relocation against an output section |
1144 | // (this->local_sym_index_ == SECTION_CODE), the output section. | |
c06b7b0b | 1145 | Output_section* os; |
5a6f7e2d ILT |
1146 | } u1_; |
1147 | union | |
1148 | { | |
dceae3c1 ILT |
1149 | // If this->shndx_ is not INVALID CODE, the object which holds the |
1150 | // input section being used to specify the reloc address. | |
ef9beddf | 1151 | Sized_relobj<size, big_endian>* relobj; |
dceae3c1 | 1152 | // If this->shndx_ is INVALID_CODE, the output data being used to |
5a6f7e2d ILT |
1153 | // specify the reloc address. This may be NULL if the reloc |
1154 | // address is absolute. | |
1155 | Output_data* od; | |
1156 | } u2_; | |
1157 | // The address offset within the input section or the Output_data. | |
1158 | Address address_; | |
dceae3c1 ILT |
1159 | // This is GSYM_CODE for a global symbol, or SECTION_CODE for a |
1160 | // relocation against an output section, or INVALID_CODE for an | |
1161 | // uninitialized value. Otherwise, for a local symbol | |
1162 | // (this->is_section_symbol_ is false), the local symbol index. For | |
1163 | // a local section symbol (this->is_section_symbol_ is true), the | |
1164 | // section index in the input file. | |
c06b7b0b | 1165 | unsigned int local_sym_index_; |
a3ad94ed | 1166 | // The reloc type--a processor specific code. |
dceae3c1 | 1167 | unsigned int type_ : 30; |
e8c846c3 ILT |
1168 | // True if the relocation is a RELATIVE relocation. |
1169 | bool is_relative_ : 1; | |
dceae3c1 ILT |
1170 | // True if the relocation is against a section symbol. |
1171 | bool is_section_symbol_ : 1; | |
5a6f7e2d ILT |
1172 | // If the reloc address is an input section in an object, the |
1173 | // section index. This is INVALID_CODE if the reloc address is | |
1174 | // specified in some other way. | |
1175 | unsigned int shndx_; | |
c06b7b0b ILT |
1176 | }; |
1177 | ||
1178 | // The SHT_RELA version of Output_reloc<>. This is just derived from | |
1179 | // the SHT_REL version of Output_reloc, but it adds an addend. | |
1180 | ||
1181 | template<bool dynamic, int size, bool big_endian> | |
1182 | class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
1183 | { | |
1184 | public: | |
1185 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
1186 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; | |
1187 | ||
1188 | // An uninitialized entry. | |
1189 | Output_reloc() | |
1190 | : rel_() | |
1191 | { } | |
1192 | ||
1193 | // A reloc against a global symbol. | |
5a6f7e2d | 1194 | |
a3ad94ed | 1195 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
2ea97941 ILT |
1196 | Address address, Addend addend, bool is_relative) |
1197 | : rel_(gsym, type, od, address, is_relative), addend_(addend) | |
c06b7b0b ILT |
1198 | { } |
1199 | ||
ef9beddf ILT |
1200 | Output_reloc(Symbol* gsym, unsigned int type, |
1201 | Sized_relobj<size, big_endian>* relobj, | |
2ea97941 | 1202 | unsigned int shndx, Address address, Addend addend, |
e8c846c3 | 1203 | bool is_relative) |
2ea97941 | 1204 | : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend) |
5a6f7e2d ILT |
1205 | { } |
1206 | ||
c06b7b0b | 1207 | // A reloc against a local symbol. |
5a6f7e2d ILT |
1208 | |
1209 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
e8c846c3 | 1210 | unsigned int local_sym_index, unsigned int type, |
2ea97941 | 1211 | Output_data* od, Address address, |
dceae3c1 | 1212 | Addend addend, bool is_relative, bool is_section_symbol) |
2ea97941 | 1213 | : rel_(relobj, local_sym_index, type, od, address, is_relative, |
dceae3c1 | 1214 | is_section_symbol), |
e8c846c3 | 1215 | addend_(addend) |
5a6f7e2d ILT |
1216 | { } |
1217 | ||
1218 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
e8c846c3 | 1219 | unsigned int local_sym_index, unsigned int type, |
2ea97941 | 1220 | unsigned int shndx, Address address, |
dceae3c1 | 1221 | Addend addend, bool is_relative, bool is_section_symbol) |
2ea97941 | 1222 | : rel_(relobj, local_sym_index, type, shndx, address, is_relative, |
dceae3c1 | 1223 | is_section_symbol), |
5a6f7e2d | 1224 | addend_(addend) |
c06b7b0b ILT |
1225 | { } |
1226 | ||
1227 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 1228 | |
a3ad94ed | 1229 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
2ea97941 ILT |
1230 | Address address, Addend addend) |
1231 | : rel_(os, type, od, address), addend_(addend) | |
c06b7b0b ILT |
1232 | { } |
1233 | ||
ef9beddf ILT |
1234 | Output_reloc(Output_section* os, unsigned int type, |
1235 | Sized_relobj<size, big_endian>* relobj, | |
2ea97941 ILT |
1236 | unsigned int shndx, Address address, Addend addend) |
1237 | : rel_(os, type, relobj, shndx, address), addend_(addend) | |
5a6f7e2d ILT |
1238 | { } |
1239 | ||
c06b7b0b ILT |
1240 | // Write the reloc entry to an output view. |
1241 | void | |
1242 | write(unsigned char* pov) const; | |
1243 | ||
d98bc257 ILT |
1244 | // Return whether this reloc should be sorted before the argument |
1245 | // when sorting dynamic relocs. | |
1246 | bool | |
1247 | sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>& | |
1248 | r2) const | |
1249 | { | |
1250 | int i = this->rel_.compare(r2.rel_); | |
1251 | if (i < 0) | |
d98bc257 | 1252 | return true; |
cc28ec61 ILT |
1253 | else if (i > 0) |
1254 | return false; | |
d98bc257 ILT |
1255 | else |
1256 | return this->addend_ < r2.addend_; | |
1257 | } | |
1258 | ||
c06b7b0b ILT |
1259 | private: |
1260 | // The basic reloc. | |
1261 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_; | |
1262 | // The addend. | |
1263 | Addend addend_; | |
1264 | }; | |
1265 | ||
1266 | // Output_data_reloc is used to manage a section containing relocs. | |
1267 | // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC | |
1268 | // indicates whether this is a dynamic relocation or a normal | |
1269 | // relocation. Output_data_reloc_base is a base class. | |
1270 | // Output_data_reloc is the real class, which we specialize based on | |
1271 | // the reloc type. | |
1272 | ||
1273 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
27bc2bce | 1274 | class Output_data_reloc_base : public Output_section_data_build |
c06b7b0b ILT |
1275 | { |
1276 | public: | |
1277 | typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type; | |
1278 | typedef typename Output_reloc_type::Address Address; | |
1279 | static const int reloc_size = | |
1280 | Reloc_types<sh_type, size, big_endian>::reloc_size; | |
1281 | ||
1282 | // Construct the section. | |
d98bc257 ILT |
1283 | Output_data_reloc_base(bool sort_relocs) |
1284 | : Output_section_data_build(Output_data::default_alignment_for_size(size)), | |
1285 | sort_relocs_(sort_relocs) | |
c06b7b0b ILT |
1286 | { } |
1287 | ||
27bc2bce | 1288 | protected: |
c06b7b0b ILT |
1289 | // Write out the data. |
1290 | void | |
1291 | do_write(Output_file*); | |
1292 | ||
16649710 ILT |
1293 | // Set the entry size and the link. |
1294 | void | |
1295 | do_adjust_output_section(Output_section *os); | |
1296 | ||
7d9e3d98 ILT |
1297 | // Write to a map file. |
1298 | void | |
1299 | do_print_to_mapfile(Mapfile* mapfile) const | |
1300 | { | |
1301 | mapfile->print_output_data(this, | |
1302 | (dynamic | |
1303 | ? _("** dynamic relocs") | |
1304 | : _("** relocs"))); | |
1305 | } | |
1306 | ||
c06b7b0b ILT |
1307 | // Add a relocation entry. |
1308 | void | |
4f4c5f80 | 1309 | add(Output_data *od, const Output_reloc_type& reloc) |
c06b7b0b ILT |
1310 | { |
1311 | this->relocs_.push_back(reloc); | |
27bc2bce | 1312 | this->set_current_data_size(this->relocs_.size() * reloc_size); |
4f4c5f80 | 1313 | od->add_dynamic_reloc(); |
c06b7b0b ILT |
1314 | } |
1315 | ||
1316 | private: | |
1317 | typedef std::vector<Output_reloc_type> Relocs; | |
1318 | ||
d98bc257 ILT |
1319 | // The class used to sort the relocations. |
1320 | struct Sort_relocs_comparison | |
1321 | { | |
1322 | bool | |
1323 | operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const | |
1324 | { return r1.sort_before(r2); } | |
1325 | }; | |
1326 | ||
1327 | // The relocations in this section. | |
c06b7b0b | 1328 | Relocs relocs_; |
d98bc257 ILT |
1329 | // Whether to sort the relocations when writing them out, to make |
1330 | // the dynamic linker more efficient. | |
1331 | bool sort_relocs_; | |
c06b7b0b ILT |
1332 | }; |
1333 | ||
1334 | // The class which callers actually create. | |
1335 | ||
1336 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
1337 | class Output_data_reloc; | |
1338 | ||
1339 | // The SHT_REL version of Output_data_reloc. | |
1340 | ||
1341 | template<bool dynamic, int size, bool big_endian> | |
1342 | class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
1343 | : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian> | |
1344 | { | |
dceae3c1 | 1345 | private: |
c06b7b0b ILT |
1346 | typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, |
1347 | big_endian> Base; | |
1348 | ||
1349 | public: | |
1350 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
1351 | typedef typename Output_reloc_type::Address Address; | |
1352 | ||
d98bc257 ILT |
1353 | Output_data_reloc(bool sr) |
1354 | : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr) | |
c06b7b0b ILT |
1355 | { } |
1356 | ||
1357 | // Add a reloc against a global symbol. | |
5a6f7e2d | 1358 | |
c06b7b0b | 1359 | void |
2ea97941 ILT |
1360 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address) |
1361 | { this->add(od, Output_reloc_type(gsym, type, od, address, false)); } | |
c06b7b0b | 1362 | |
5a6f7e2d | 1363 | void |
ef9beddf ILT |
1364 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
1365 | Sized_relobj<size, big_endian>* relobj, | |
2ea97941 ILT |
1366 | unsigned int shndx, Address address) |
1367 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, | |
e8c846c3 ILT |
1368 | false)); } |
1369 | ||
12c0daef ILT |
1370 | // These are to simplify the Copy_relocs class. |
1371 | ||
1372 | void | |
2ea97941 | 1373 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address, |
12c0daef ILT |
1374 | Address addend) |
1375 | { | |
1376 | gold_assert(addend == 0); | |
2ea97941 | 1377 | this->add_global(gsym, type, od, address); |
12c0daef ILT |
1378 | } |
1379 | ||
1380 | void | |
ef9beddf ILT |
1381 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
1382 | Sized_relobj<size, big_endian>* relobj, | |
2ea97941 | 1383 | unsigned int shndx, Address address, Address addend) |
12c0daef ILT |
1384 | { |
1385 | gold_assert(addend == 0); | |
2ea97941 | 1386 | this->add_global(gsym, type, od, relobj, shndx, address); |
12c0daef ILT |
1387 | } |
1388 | ||
e8c846c3 ILT |
1389 | // Add a RELATIVE reloc against a global symbol. The final relocation |
1390 | // will not reference the symbol. | |
1391 | ||
1392 | void | |
1393 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, | |
2ea97941 ILT |
1394 | Address address) |
1395 | { this->add(od, Output_reloc_type(gsym, type, od, address, true)); } | |
e8c846c3 ILT |
1396 | |
1397 | void | |
1398 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, | |
ef9beddf | 1399 | Sized_relobj<size, big_endian>* relobj, |
2ea97941 | 1400 | unsigned int shndx, Address address) |
dceae3c1 | 1401 | { |
2ea97941 | 1402 | this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
dceae3c1 ILT |
1403 | true)); |
1404 | } | |
5a6f7e2d | 1405 | |
c06b7b0b | 1406 | // Add a reloc against a local symbol. |
5a6f7e2d | 1407 | |
c06b7b0b | 1408 | void |
5a6f7e2d | 1409 | add_local(Sized_relobj<size, big_endian>* relobj, |
a3ad94ed | 1410 | unsigned int local_sym_index, unsigned int type, |
2ea97941 | 1411 | Output_data* od, Address address) |
dceae3c1 ILT |
1412 | { |
1413 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, | |
2ea97941 | 1414 | address, false, false)); |
dceae3c1 | 1415 | } |
5a6f7e2d ILT |
1416 | |
1417 | void | |
1418 | add_local(Sized_relobj<size, big_endian>* relobj, | |
1419 | unsigned int local_sym_index, unsigned int type, | |
2ea97941 | 1420 | Output_data* od, unsigned int shndx, Address address) |
dceae3c1 ILT |
1421 | { |
1422 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
2ea97941 | 1423 | address, false, false)); |
dceae3c1 | 1424 | } |
e8c846c3 ILT |
1425 | |
1426 | // Add a RELATIVE reloc against a local symbol. | |
5a6f7e2d | 1427 | |
e8c846c3 ILT |
1428 | void |
1429 | add_local_relative(Sized_relobj<size, big_endian>* relobj, | |
1430 | unsigned int local_sym_index, unsigned int type, | |
2ea97941 | 1431 | Output_data* od, Address address) |
dceae3c1 ILT |
1432 | { |
1433 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, | |
2ea97941 | 1434 | address, true, false)); |
dceae3c1 | 1435 | } |
e8c846c3 ILT |
1436 | |
1437 | void | |
1438 | add_local_relative(Sized_relobj<size, big_endian>* relobj, | |
1439 | unsigned int local_sym_index, unsigned int type, | |
2ea97941 | 1440 | Output_data* od, unsigned int shndx, Address address) |
dceae3c1 ILT |
1441 | { |
1442 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
2ea97941 | 1443 | address, true, false)); |
dceae3c1 ILT |
1444 | } |
1445 | ||
1446 | // Add a reloc against a local section symbol. This will be | |
1447 | // converted into a reloc against the STT_SECTION symbol of the | |
1448 | // output section. | |
1449 | ||
1450 | void | |
1451 | add_local_section(Sized_relobj<size, big_endian>* relobj, | |
1452 | unsigned int input_shndx, unsigned int type, | |
2ea97941 | 1453 | Output_data* od, Address address) |
dceae3c1 ILT |
1454 | { |
1455 | this->add(od, Output_reloc_type(relobj, input_shndx, type, od, | |
2ea97941 | 1456 | address, false, true)); |
dceae3c1 ILT |
1457 | } |
1458 | ||
1459 | void | |
1460 | add_local_section(Sized_relobj<size, big_endian>* relobj, | |
1461 | unsigned int input_shndx, unsigned int type, | |
2ea97941 | 1462 | Output_data* od, unsigned int shndx, Address address) |
dceae3c1 ILT |
1463 | { |
1464 | this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx, | |
2ea97941 | 1465 | address, false, true)); |
dceae3c1 | 1466 | } |
c06b7b0b ILT |
1467 | |
1468 | // A reloc against the STT_SECTION symbol of an output section. | |
4f4c5f80 ILT |
1469 | // OS is the Output_section that the relocation refers to; OD is |
1470 | // the Output_data object being relocated. | |
5a6f7e2d | 1471 | |
c06b7b0b | 1472 | void |
a3ad94ed | 1473 | add_output_section(Output_section* os, unsigned int type, |
2ea97941 ILT |
1474 | Output_data* od, Address address) |
1475 | { this->add(od, Output_reloc_type(os, type, od, address)); } | |
5a6f7e2d ILT |
1476 | |
1477 | void | |
4f4c5f80 | 1478 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
ef9beddf | 1479 | Sized_relobj<size, big_endian>* relobj, |
2ea97941 ILT |
1480 | unsigned int shndx, Address address) |
1481 | { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); } | |
c06b7b0b ILT |
1482 | }; |
1483 | ||
1484 | // The SHT_RELA version of Output_data_reloc. | |
1485 | ||
1486 | template<bool dynamic, int size, bool big_endian> | |
1487 | class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
1488 | : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
1489 | { | |
dceae3c1 | 1490 | private: |
c06b7b0b ILT |
1491 | typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, |
1492 | big_endian> Base; | |
1493 | ||
1494 | public: | |
1495 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
1496 | typedef typename Output_reloc_type::Address Address; | |
1497 | typedef typename Output_reloc_type::Addend Addend; | |
1498 | ||
d98bc257 ILT |
1499 | Output_data_reloc(bool sr) |
1500 | : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr) | |
c06b7b0b ILT |
1501 | { } |
1502 | ||
1503 | // Add a reloc against a global symbol. | |
5a6f7e2d | 1504 | |
c06b7b0b | 1505 | void |
a3ad94ed | 1506 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
2ea97941 ILT |
1507 | Address address, Addend addend) |
1508 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend, | |
e8c846c3 | 1509 | false)); } |
c06b7b0b | 1510 | |
5a6f7e2d | 1511 | void |
ef9beddf ILT |
1512 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
1513 | Sized_relobj<size, big_endian>* relobj, | |
2ea97941 | 1514 | unsigned int shndx, Address address, |
4f4c5f80 | 1515 | Addend addend) |
2ea97941 | 1516 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
e8c846c3 ILT |
1517 | addend, false)); } |
1518 | ||
1519 | // Add a RELATIVE reloc against a global symbol. The final output | |
1520 | // relocation will not reference the symbol, but we must keep the symbol | |
1521 | // information long enough to set the addend of the relocation correctly | |
1522 | // when it is written. | |
1523 | ||
1524 | void | |
1525 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, | |
2ea97941 ILT |
1526 | Address address, Addend addend) |
1527 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); } | |
e8c846c3 ILT |
1528 | |
1529 | void | |
1530 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, | |
ef9beddf | 1531 | Sized_relobj<size, big_endian>* relobj, |
2ea97941 ILT |
1532 | unsigned int shndx, Address address, Addend addend) |
1533 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, | |
e8c846c3 | 1534 | addend, true)); } |
5a6f7e2d | 1535 | |
c06b7b0b | 1536 | // Add a reloc against a local symbol. |
5a6f7e2d | 1537 | |
c06b7b0b | 1538 | void |
5a6f7e2d | 1539 | add_local(Sized_relobj<size, big_endian>* relobj, |
c06b7b0b | 1540 | unsigned int local_sym_index, unsigned int type, |
2ea97941 | 1541 | Output_data* od, Address address, Addend addend) |
c06b7b0b | 1542 | { |
2ea97941 | 1543 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
dceae3c1 | 1544 | addend, false, false)); |
5a6f7e2d ILT |
1545 | } |
1546 | ||
1547 | void | |
1548 | add_local(Sized_relobj<size, big_endian>* relobj, | |
1549 | unsigned int local_sym_index, unsigned int type, | |
2ea97941 | 1550 | Output_data* od, unsigned int shndx, Address address, |
4f4c5f80 | 1551 | Addend addend) |
5a6f7e2d | 1552 | { |
4f4c5f80 | 1553 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
2ea97941 | 1554 | address, addend, false, false)); |
e8c846c3 ILT |
1555 | } |
1556 | ||
1557 | // Add a RELATIVE reloc against a local symbol. | |
1558 | ||
1559 | void | |
1560 | add_local_relative(Sized_relobj<size, big_endian>* relobj, | |
1561 | unsigned int local_sym_index, unsigned int type, | |
2ea97941 | 1562 | Output_data* od, Address address, Addend addend) |
e8c846c3 | 1563 | { |
2ea97941 | 1564 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
dceae3c1 | 1565 | addend, true, false)); |
e8c846c3 ILT |
1566 | } |
1567 | ||
1568 | void | |
1569 | add_local_relative(Sized_relobj<size, big_endian>* relobj, | |
1570 | unsigned int local_sym_index, unsigned int type, | |
2ea97941 | 1571 | Output_data* od, unsigned int shndx, Address address, |
e8c846c3 ILT |
1572 | Addend addend) |
1573 | { | |
1574 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
2ea97941 | 1575 | address, addend, true, false)); |
dceae3c1 ILT |
1576 | } |
1577 | ||
1578 | // Add a reloc against a local section symbol. This will be | |
1579 | // converted into a reloc against the STT_SECTION symbol of the | |
1580 | // output section. | |
1581 | ||
1582 | void | |
1583 | add_local_section(Sized_relobj<size, big_endian>* relobj, | |
1584 | unsigned int input_shndx, unsigned int type, | |
2ea97941 | 1585 | Output_data* od, Address address, Addend addend) |
dceae3c1 | 1586 | { |
2ea97941 | 1587 | this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address, |
dceae3c1 ILT |
1588 | addend, false, true)); |
1589 | } | |
1590 | ||
1591 | void | |
1592 | add_local_section(Sized_relobj<size, big_endian>* relobj, | |
1593 | unsigned int input_shndx, unsigned int type, | |
2ea97941 | 1594 | Output_data* od, unsigned int shndx, Address address, |
dceae3c1 ILT |
1595 | Addend addend) |
1596 | { | |
1597 | this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx, | |
2ea97941 | 1598 | address, addend, false, true)); |
c06b7b0b ILT |
1599 | } |
1600 | ||
1601 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 1602 | |
c06b7b0b | 1603 | void |
a3ad94ed | 1604 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
2ea97941 ILT |
1605 | Address address, Addend addend) |
1606 | { this->add(os, Output_reloc_type(os, type, od, address, addend)); } | |
5a6f7e2d ILT |
1607 | |
1608 | void | |
ef9beddf ILT |
1609 | add_output_section(Output_section* os, unsigned int type, |
1610 | Sized_relobj<size, big_endian>* relobj, | |
2ea97941 ILT |
1611 | unsigned int shndx, Address address, Addend addend) |
1612 | { this->add(os, Output_reloc_type(os, type, relobj, shndx, address, | |
4f4c5f80 | 1613 | addend)); } |
c06b7b0b ILT |
1614 | }; |
1615 | ||
6a74a719 ILT |
1616 | // Output_relocatable_relocs represents a relocation section in a |
1617 | // relocatable link. The actual data is written out in the target | |
1618 | // hook relocate_for_relocatable. This just saves space for it. | |
1619 | ||
1620 | template<int sh_type, int size, bool big_endian> | |
1621 | class Output_relocatable_relocs : public Output_section_data | |
1622 | { | |
1623 | public: | |
1624 | Output_relocatable_relocs(Relocatable_relocs* rr) | |
1625 | : Output_section_data(Output_data::default_alignment_for_size(size)), | |
1626 | rr_(rr) | |
1627 | { } | |
1628 | ||
1629 | void | |
1630 | set_final_data_size(); | |
1631 | ||
1632 | // Write out the data. There is nothing to do here. | |
1633 | void | |
1634 | do_write(Output_file*) | |
1635 | { } | |
1636 | ||
7d9e3d98 ILT |
1637 | // Write to a map file. |
1638 | void | |
1639 | do_print_to_mapfile(Mapfile* mapfile) const | |
1640 | { mapfile->print_output_data(this, _("** relocs")); } | |
1641 | ||
6a74a719 ILT |
1642 | private: |
1643 | // The relocs associated with this input section. | |
1644 | Relocatable_relocs* rr_; | |
1645 | }; | |
1646 | ||
1647 | // Handle a GROUP section. | |
1648 | ||
1649 | template<int size, bool big_endian> | |
1650 | class Output_data_group : public Output_section_data | |
1651 | { | |
1652 | public: | |
8825ac63 | 1653 | // The constructor clears *INPUT_SHNDXES. |
6a74a719 ILT |
1654 | Output_data_group(Sized_relobj<size, big_endian>* relobj, |
1655 | section_size_type entry_count, | |
8825ac63 ILT |
1656 | elfcpp::Elf_Word flags, |
1657 | std::vector<unsigned int>* input_shndxes); | |
6a74a719 ILT |
1658 | |
1659 | void | |
1660 | do_write(Output_file*); | |
1661 | ||
7d9e3d98 ILT |
1662 | // Write to a map file. |
1663 | void | |
1664 | do_print_to_mapfile(Mapfile* mapfile) const | |
1665 | { mapfile->print_output_data(this, _("** group")); } | |
1666 | ||
20e6d0d6 DK |
1667 | // Set final data size. |
1668 | void | |
1669 | set_final_data_size() | |
1670 | { this->set_data_size((this->input_shndxes_.size() + 1) * 4); } | |
1671 | ||
6a74a719 ILT |
1672 | private: |
1673 | // The input object. | |
1674 | Sized_relobj<size, big_endian>* relobj_; | |
1675 | // The group flag word. | |
1676 | elfcpp::Elf_Word flags_; | |
1677 | // The section indexes of the input sections in this group. | |
8825ac63 | 1678 | std::vector<unsigned int> input_shndxes_; |
6a74a719 ILT |
1679 | }; |
1680 | ||
dbe717ef ILT |
1681 | // Output_data_got is used to manage a GOT. Each entry in the GOT is |
1682 | // for one symbol--either a global symbol or a local symbol in an | |
ead1e424 | 1683 | // object. The target specific code adds entries to the GOT as |
dbe717ef | 1684 | // needed. |
ead1e424 ILT |
1685 | |
1686 | template<int size, bool big_endian> | |
27bc2bce | 1687 | class Output_data_got : public Output_section_data_build |
ead1e424 ILT |
1688 | { |
1689 | public: | |
1690 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; | |
7bf1f802 ILT |
1691 | typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn; |
1692 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn; | |
ead1e424 | 1693 | |
7e1edb90 | 1694 | Output_data_got() |
27bc2bce | 1695 | : Output_section_data_build(Output_data::default_alignment_for_size(size)), |
730cdc88 | 1696 | entries_() |
ead1e424 ILT |
1697 | { } |
1698 | ||
dbe717ef ILT |
1699 | // Add an entry for a global symbol to the GOT. Return true if this |
1700 | // is a new GOT entry, false if the symbol was already in the GOT. | |
1701 | bool | |
0a65a3a7 | 1702 | add_global(Symbol* gsym, unsigned int got_type); |
ead1e424 | 1703 | |
7bf1f802 ILT |
1704 | // Add an entry for a global symbol to the GOT, and add a dynamic |
1705 | // relocation of type R_TYPE for the GOT entry. | |
1706 | void | |
0a65a3a7 CC |
1707 | add_global_with_rel(Symbol* gsym, unsigned int got_type, |
1708 | Rel_dyn* rel_dyn, unsigned int r_type); | |
7bf1f802 ILT |
1709 | |
1710 | void | |
0a65a3a7 CC |
1711 | add_global_with_rela(Symbol* gsym, unsigned int got_type, |
1712 | Rela_dyn* rela_dyn, unsigned int r_type); | |
1713 | ||
1714 | // Add a pair of entries for a global symbol to the GOT, and add | |
1715 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. | |
1716 | void | |
1717 | add_global_pair_with_rel(Symbol* gsym, unsigned int got_type, | |
1718 | Rel_dyn* rel_dyn, unsigned int r_type_1, | |
1719 | unsigned int r_type_2); | |
1720 | ||
1721 | void | |
1722 | add_global_pair_with_rela(Symbol* gsym, unsigned int got_type, | |
1723 | Rela_dyn* rela_dyn, unsigned int r_type_1, | |
1724 | unsigned int r_type_2); | |
7bf1f802 | 1725 | |
e727fa71 ILT |
1726 | // Add an entry for a local symbol to the GOT. This returns true if |
1727 | // this is a new GOT entry, false if the symbol already has a GOT | |
1728 | // entry. | |
1729 | bool | |
0a65a3a7 CC |
1730 | add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index, |
1731 | unsigned int got_type); | |
ead1e424 | 1732 | |
0a65a3a7 | 1733 | // Add an entry for a local symbol to the GOT, and add a dynamic |
7bf1f802 ILT |
1734 | // relocation of type R_TYPE for the GOT entry. |
1735 | void | |
1736 | add_local_with_rel(Sized_relobj<size, big_endian>* object, | |
0a65a3a7 CC |
1737 | unsigned int sym_index, unsigned int got_type, |
1738 | Rel_dyn* rel_dyn, unsigned int r_type); | |
7bf1f802 ILT |
1739 | |
1740 | void | |
1741 | add_local_with_rela(Sized_relobj<size, big_endian>* object, | |
0a65a3a7 CC |
1742 | unsigned int sym_index, unsigned int got_type, |
1743 | Rela_dyn* rela_dyn, unsigned int r_type); | |
07f397ab | 1744 | |
0a65a3a7 CC |
1745 | // Add a pair of entries for a local symbol to the GOT, and add |
1746 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. | |
7bf1f802 | 1747 | void |
0a65a3a7 CC |
1748 | add_local_pair_with_rel(Sized_relobj<size, big_endian>* object, |
1749 | unsigned int sym_index, unsigned int shndx, | |
1750 | unsigned int got_type, Rel_dyn* rel_dyn, | |
1751 | unsigned int r_type_1, unsigned int r_type_2); | |
7bf1f802 ILT |
1752 | |
1753 | void | |
0a65a3a7 CC |
1754 | add_local_pair_with_rela(Sized_relobj<size, big_endian>* object, |
1755 | unsigned int sym_index, unsigned int shndx, | |
1756 | unsigned int got_type, Rela_dyn* rela_dyn, | |
1757 | unsigned int r_type_1, unsigned int r_type_2); | |
7bf1f802 | 1758 | |
ead1e424 ILT |
1759 | // Add a constant to the GOT. This returns the offset of the new |
1760 | // entry from the start of the GOT. | |
1761 | unsigned int | |
1762 | add_constant(Valtype constant) | |
1763 | { | |
1764 | this->entries_.push_back(Got_entry(constant)); | |
1765 | this->set_got_size(); | |
1766 | return this->last_got_offset(); | |
1767 | } | |
1768 | ||
27bc2bce | 1769 | protected: |
ead1e424 ILT |
1770 | // Write out the GOT table. |
1771 | void | |
1772 | do_write(Output_file*); | |
1773 | ||
7d9e3d98 ILT |
1774 | // Write to a map file. |
1775 | void | |
1776 | do_print_to_mapfile(Mapfile* mapfile) const | |
1777 | { mapfile->print_output_data(this, _("** GOT")); } | |
1778 | ||
ead1e424 ILT |
1779 | private: |
1780 | // This POD class holds a single GOT entry. | |
1781 | class Got_entry | |
1782 | { | |
1783 | public: | |
1784 | // Create a zero entry. | |
1785 | Got_entry() | |
1786 | : local_sym_index_(CONSTANT_CODE) | |
1787 | { this->u_.constant = 0; } | |
1788 | ||
1789 | // Create a global symbol entry. | |
a3ad94ed | 1790 | explicit Got_entry(Symbol* gsym) |
ead1e424 ILT |
1791 | : local_sym_index_(GSYM_CODE) |
1792 | { this->u_.gsym = gsym; } | |
1793 | ||
1794 | // Create a local symbol entry. | |
e727fa71 ILT |
1795 | Got_entry(Sized_relobj<size, big_endian>* object, |
1796 | unsigned int local_sym_index) | |
ead1e424 ILT |
1797 | : local_sym_index_(local_sym_index) |
1798 | { | |
a3ad94ed ILT |
1799 | gold_assert(local_sym_index != GSYM_CODE |
1800 | && local_sym_index != CONSTANT_CODE); | |
ead1e424 ILT |
1801 | this->u_.object = object; |
1802 | } | |
1803 | ||
1804 | // Create a constant entry. The constant is a host value--it will | |
1805 | // be swapped, if necessary, when it is written out. | |
a3ad94ed | 1806 | explicit Got_entry(Valtype constant) |
ead1e424 ILT |
1807 | : local_sym_index_(CONSTANT_CODE) |
1808 | { this->u_.constant = constant; } | |
1809 | ||
1810 | // Write the GOT entry to an output view. | |
1811 | void | |
7e1edb90 | 1812 | write(unsigned char* pov) const; |
ead1e424 ILT |
1813 | |
1814 | private: | |
1815 | enum | |
1816 | { | |
1817 | GSYM_CODE = -1U, | |
1818 | CONSTANT_CODE = -2U | |
1819 | }; | |
1820 | ||
1821 | union | |
1822 | { | |
1823 | // For a local symbol, the object. | |
e727fa71 | 1824 | Sized_relobj<size, big_endian>* object; |
ead1e424 ILT |
1825 | // For a global symbol, the symbol. |
1826 | Symbol* gsym; | |
1827 | // For a constant, the constant. | |
1828 | Valtype constant; | |
1829 | } u_; | |
c06b7b0b ILT |
1830 | // For a local symbol, the local symbol index. This is GSYM_CODE |
1831 | // for a global symbol, or CONSTANT_CODE for a constant. | |
ead1e424 ILT |
1832 | unsigned int local_sym_index_; |
1833 | }; | |
1834 | ||
1835 | typedef std::vector<Got_entry> Got_entries; | |
1836 | ||
1837 | // Return the offset into the GOT of GOT entry I. | |
1838 | unsigned int | |
1839 | got_offset(unsigned int i) const | |
1840 | { return i * (size / 8); } | |
1841 | ||
1842 | // Return the offset into the GOT of the last entry added. | |
1843 | unsigned int | |
1844 | last_got_offset() const | |
1845 | { return this->got_offset(this->entries_.size() - 1); } | |
1846 | ||
1847 | // Set the size of the section. | |
1848 | void | |
1849 | set_got_size() | |
27bc2bce | 1850 | { this->set_current_data_size(this->got_offset(this->entries_.size())); } |
ead1e424 ILT |
1851 | |
1852 | // The list of GOT entries. | |
1853 | Got_entries entries_; | |
1854 | }; | |
1855 | ||
a3ad94ed ILT |
1856 | // Output_data_dynamic is used to hold the data in SHT_DYNAMIC |
1857 | // section. | |
1858 | ||
1859 | class Output_data_dynamic : public Output_section_data | |
1860 | { | |
1861 | public: | |
9025d29d | 1862 | Output_data_dynamic(Stringpool* pool) |
730cdc88 | 1863 | : Output_section_data(Output_data::default_alignment()), |
9025d29d | 1864 | entries_(), pool_(pool) |
a3ad94ed ILT |
1865 | { } |
1866 | ||
1867 | // Add a new dynamic entry with a fixed numeric value. | |
1868 | void | |
1869 | add_constant(elfcpp::DT tag, unsigned int val) | |
1870 | { this->add_entry(Dynamic_entry(tag, val)); } | |
1871 | ||
16649710 | 1872 | // Add a new dynamic entry with the address of output data. |
a3ad94ed | 1873 | void |
16649710 ILT |
1874 | add_section_address(elfcpp::DT tag, const Output_data* od) |
1875 | { this->add_entry(Dynamic_entry(tag, od, false)); } | |
a3ad94ed | 1876 | |
c2b45e22 CC |
1877 | // Add a new dynamic entry with the address of output data |
1878 | // plus a constant offset. | |
1879 | void | |
1880 | add_section_plus_offset(elfcpp::DT tag, const Output_data* od, | |
2ea97941 ILT |
1881 | unsigned int offset) |
1882 | { this->add_entry(Dynamic_entry(tag, od, offset)); } | |
c2b45e22 | 1883 | |
16649710 | 1884 | // Add a new dynamic entry with the size of output data. |
a3ad94ed | 1885 | void |
16649710 ILT |
1886 | add_section_size(elfcpp::DT tag, const Output_data* od) |
1887 | { this->add_entry(Dynamic_entry(tag, od, true)); } | |
a3ad94ed ILT |
1888 | |
1889 | // Add a new dynamic entry with the address of a symbol. | |
1890 | void | |
16649710 | 1891 | add_symbol(elfcpp::DT tag, const Symbol* sym) |
a3ad94ed ILT |
1892 | { this->add_entry(Dynamic_entry(tag, sym)); } |
1893 | ||
1894 | // Add a new dynamic entry with a string. | |
1895 | void | |
1896 | add_string(elfcpp::DT tag, const char* str) | |
cfd73a4e | 1897 | { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); } |
a3ad94ed | 1898 | |
41f542e7 ILT |
1899 | void |
1900 | add_string(elfcpp::DT tag, const std::string& str) | |
1901 | { this->add_string(tag, str.c_str()); } | |
1902 | ||
27bc2bce ILT |
1903 | protected: |
1904 | // Adjust the output section to set the entry size. | |
1905 | void | |
1906 | do_adjust_output_section(Output_section*); | |
1907 | ||
a3ad94ed ILT |
1908 | // Set the final data size. |
1909 | void | |
27bc2bce | 1910 | set_final_data_size(); |
a3ad94ed ILT |
1911 | |
1912 | // Write out the dynamic entries. | |
1913 | void | |
1914 | do_write(Output_file*); | |
1915 | ||
7d9e3d98 ILT |
1916 | // Write to a map file. |
1917 | void | |
1918 | do_print_to_mapfile(Mapfile* mapfile) const | |
1919 | { mapfile->print_output_data(this, _("** dynamic")); } | |
1920 | ||
a3ad94ed ILT |
1921 | private: |
1922 | // This POD class holds a single dynamic entry. | |
1923 | class Dynamic_entry | |
1924 | { | |
1925 | public: | |
1926 | // Create an entry with a fixed numeric value. | |
2ea97941 ILT |
1927 | Dynamic_entry(elfcpp::DT tag, unsigned int val) |
1928 | : tag_(tag), offset_(DYNAMIC_NUMBER) | |
a3ad94ed ILT |
1929 | { this->u_.val = val; } |
1930 | ||
1931 | // Create an entry with the size or address of a section. | |
2ea97941 ILT |
1932 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size) |
1933 | : tag_(tag), | |
c2b45e22 CC |
1934 | offset_(section_size |
1935 | ? DYNAMIC_SECTION_SIZE | |
1936 | : DYNAMIC_SECTION_ADDRESS) | |
1937 | { this->u_.od = od; } | |
1938 | ||
1939 | // Create an entry with the address of a section plus a constant offset. | |
2ea97941 ILT |
1940 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset) |
1941 | : tag_(tag), | |
c2b45e22 | 1942 | offset_(offset) |
16649710 | 1943 | { this->u_.od = od; } |
a3ad94ed ILT |
1944 | |
1945 | // Create an entry with the address of a symbol. | |
2ea97941 ILT |
1946 | Dynamic_entry(elfcpp::DT tag, const Symbol* sym) |
1947 | : tag_(tag), offset_(DYNAMIC_SYMBOL) | |
a3ad94ed ILT |
1948 | { this->u_.sym = sym; } |
1949 | ||
1950 | // Create an entry with a string. | |
2ea97941 ILT |
1951 | Dynamic_entry(elfcpp::DT tag, const char* str) |
1952 | : tag_(tag), offset_(DYNAMIC_STRING) | |
a3ad94ed ILT |
1953 | { this->u_.str = str; } |
1954 | ||
20e6d0d6 DK |
1955 | // Return the tag of this entry. |
1956 | elfcpp::DT | |
1957 | tag() const | |
1958 | { return this->tag_; } | |
1959 | ||
a3ad94ed ILT |
1960 | // Write the dynamic entry to an output view. |
1961 | template<int size, bool big_endian> | |
1962 | void | |
7d1a9ebb | 1963 | write(unsigned char* pov, const Stringpool*) const; |
a3ad94ed ILT |
1964 | |
1965 | private: | |
c2b45e22 | 1966 | // Classification is encoded in the OFFSET field. |
a3ad94ed ILT |
1967 | enum Classification |
1968 | { | |
a3ad94ed | 1969 | // Section address. |
c2b45e22 CC |
1970 | DYNAMIC_SECTION_ADDRESS = 0, |
1971 | // Number. | |
1972 | DYNAMIC_NUMBER = -1U, | |
a3ad94ed | 1973 | // Section size. |
c2b45e22 | 1974 | DYNAMIC_SECTION_SIZE = -2U, |
a3ad94ed | 1975 | // Symbol adress. |
c2b45e22 | 1976 | DYNAMIC_SYMBOL = -3U, |
a3ad94ed | 1977 | // String. |
c2b45e22 CC |
1978 | DYNAMIC_STRING = -4U |
1979 | // Any other value indicates a section address plus OFFSET. | |
a3ad94ed ILT |
1980 | }; |
1981 | ||
1982 | union | |
1983 | { | |
1984 | // For DYNAMIC_NUMBER. | |
1985 | unsigned int val; | |
c2b45e22 | 1986 | // For DYNAMIC_SECTION_SIZE and section address plus OFFSET. |
16649710 | 1987 | const Output_data* od; |
a3ad94ed | 1988 | // For DYNAMIC_SYMBOL. |
16649710 | 1989 | const Symbol* sym; |
a3ad94ed ILT |
1990 | // For DYNAMIC_STRING. |
1991 | const char* str; | |
1992 | } u_; | |
1993 | // The dynamic tag. | |
1994 | elfcpp::DT tag_; | |
c2b45e22 CC |
1995 | // The type of entry (Classification) or offset within a section. |
1996 | unsigned int offset_; | |
a3ad94ed ILT |
1997 | }; |
1998 | ||
1999 | // Add an entry to the list. | |
2000 | void | |
2001 | add_entry(const Dynamic_entry& entry) | |
2002 | { this->entries_.push_back(entry); } | |
2003 | ||
2004 | // Sized version of write function. | |
2005 | template<int size, bool big_endian> | |
2006 | void | |
2007 | sized_write(Output_file* of); | |
2008 | ||
2009 | // The type of the list of entries. | |
2010 | typedef std::vector<Dynamic_entry> Dynamic_entries; | |
2011 | ||
a3ad94ed ILT |
2012 | // The entries. |
2013 | Dynamic_entries entries_; | |
2014 | // The pool used for strings. | |
2015 | Stringpool* pool_; | |
2016 | }; | |
2017 | ||
d491d34e ILT |
2018 | // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections, |
2019 | // which may be required if the object file has more than | |
2020 | // SHN_LORESERVE sections. | |
2021 | ||
2022 | class Output_symtab_xindex : public Output_section_data | |
2023 | { | |
2024 | public: | |
2025 | Output_symtab_xindex(size_t symcount) | |
20e6d0d6 | 2026 | : Output_section_data(symcount * 4, 4, true), |
d491d34e ILT |
2027 | entries_() |
2028 | { } | |
2029 | ||
2030 | // Add an entry: symbol number SYMNDX has section SHNDX. | |
2031 | void | |
2032 | add(unsigned int symndx, unsigned int shndx) | |
2033 | { this->entries_.push_back(std::make_pair(symndx, shndx)); } | |
2034 | ||
2035 | protected: | |
2036 | void | |
2037 | do_write(Output_file*); | |
2038 | ||
7d9e3d98 ILT |
2039 | // Write to a map file. |
2040 | void | |
2041 | do_print_to_mapfile(Mapfile* mapfile) const | |
2042 | { mapfile->print_output_data(this, _("** symtab xindex")); } | |
2043 | ||
d491d34e ILT |
2044 | private: |
2045 | template<bool big_endian> | |
2046 | void | |
2047 | endian_do_write(unsigned char*); | |
2048 | ||
2049 | // It is likely that most symbols will not require entries. Rather | |
2050 | // than keep a vector for all symbols, we keep pairs of symbol index | |
2051 | // and section index. | |
2052 | typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries; | |
2053 | ||
2054 | // The entries we need. | |
2055 | Xindex_entries entries_; | |
2056 | }; | |
2057 | ||
20e6d0d6 | 2058 | // A relaxed input section. |
c0a62865 | 2059 | class Output_relaxed_input_section : public Output_section_data_build |
20e6d0d6 DK |
2060 | { |
2061 | public: | |
2062 | // We would like to call relobj->section_addralign(shndx) to get the | |
2063 | // alignment but we do not want the constructor to fail. So callers | |
2064 | // are repsonsible for ensuring that. | |
2ea97941 ILT |
2065 | Output_relaxed_input_section(Relobj* relobj, unsigned int shndx, |
2066 | uint64_t addralign) | |
2067 | : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx) | |
20e6d0d6 DK |
2068 | { } |
2069 | ||
2070 | // Return the Relobj of this relaxed input section. | |
2071 | Relobj* | |
2072 | relobj() const | |
2073 | { return this->relobj_; } | |
2074 | ||
2075 | // Return the section index of this relaxed input section. | |
2076 | unsigned int | |
2077 | shndx() const | |
2078 | { return this->shndx_; } | |
2079 | ||
2080 | private: | |
2081 | Relobj* relobj_; | |
2082 | unsigned int shndx_; | |
2083 | }; | |
2084 | ||
a2fb1b05 ILT |
2085 | // An output section. We don't expect to have too many output |
2086 | // sections, so we don't bother to do a template on the size. | |
2087 | ||
54dc6425 | 2088 | class Output_section : public Output_data |
a2fb1b05 ILT |
2089 | { |
2090 | public: | |
2091 | // Create an output section, giving the name, type, and flags. | |
96803768 | 2092 | Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword); |
54dc6425 | 2093 | virtual ~Output_section(); |
a2fb1b05 | 2094 | |
ead1e424 | 2095 | // Add a new input section SHNDX, named NAME, with header SHDR, from |
730cdc88 | 2096 | // object OBJECT. RELOC_SHNDX is the index of a relocation section |
eff45813 | 2097 | // which applies to this section, or 0 if none, or -1 if more than |
a445fddf ILT |
2098 | // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause |
2099 | // in a linker script; in that case we need to keep track of input | |
2100 | // sections associated with an output section. Return the offset | |
2101 | // within the output section. | |
a2fb1b05 ILT |
2102 | template<int size, bool big_endian> |
2103 | off_t | |
730cdc88 ILT |
2104 | add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx, |
2105 | const char *name, | |
2106 | const elfcpp::Shdr<size, big_endian>& shdr, | |
a445fddf | 2107 | unsigned int reloc_shndx, bool have_sections_script); |
a2fb1b05 | 2108 | |
b8e6aad9 | 2109 | // Add generated data POSD to this output section. |
c06b7b0b | 2110 | void |
ead1e424 ILT |
2111 | add_output_section_data(Output_section_data* posd); |
2112 | ||
c0a62865 DK |
2113 | // Add a relaxed input section PORIS to this output section. |
2114 | void | |
2115 | add_relaxed_input_section(Output_relaxed_input_section* poris); | |
2116 | ||
a2fb1b05 ILT |
2117 | // Return the section name. |
2118 | const char* | |
2119 | name() const | |
2120 | { return this->name_; } | |
2121 | ||
2122 | // Return the section type. | |
2123 | elfcpp::Elf_Word | |
2124 | type() const | |
2125 | { return this->type_; } | |
2126 | ||
2127 | // Return the section flags. | |
2128 | elfcpp::Elf_Xword | |
2129 | flags() const | |
2130 | { return this->flags_; } | |
2131 | ||
154e0e9a ILT |
2132 | // Update the output section flags based on input section flags. |
2133 | void | |
9c547ec3 | 2134 | update_flags_for_input_section(elfcpp::Elf_Xword flags); |
154e0e9a | 2135 | |
a3ad94ed ILT |
2136 | // Return the entsize field. |
2137 | uint64_t | |
2138 | entsize() const | |
2139 | { return this->entsize_; } | |
2140 | ||
61ba1cf9 ILT |
2141 | // Set the entsize field. |
2142 | void | |
16649710 | 2143 | set_entsize(uint64_t v); |
61ba1cf9 | 2144 | |
a445fddf ILT |
2145 | // Set the load address. |
2146 | void | |
2ea97941 | 2147 | set_load_address(uint64_t load_address) |
a445fddf | 2148 | { |
2ea97941 | 2149 | this->load_address_ = load_address; |
a445fddf ILT |
2150 | this->has_load_address_ = true; |
2151 | } | |
2152 | ||
16649710 ILT |
2153 | // Set the link field to the output section index of a section. |
2154 | void | |
14b31740 | 2155 | set_link_section(const Output_data* od) |
16649710 ILT |
2156 | { |
2157 | gold_assert(this->link_ == 0 | |
2158 | && !this->should_link_to_symtab_ | |
2159 | && !this->should_link_to_dynsym_); | |
2160 | this->link_section_ = od; | |
2161 | } | |
2162 | ||
2163 | // Set the link field to a constant. | |
61ba1cf9 ILT |
2164 | void |
2165 | set_link(unsigned int v) | |
16649710 ILT |
2166 | { |
2167 | gold_assert(this->link_section_ == NULL | |
2168 | && !this->should_link_to_symtab_ | |
2169 | && !this->should_link_to_dynsym_); | |
2170 | this->link_ = v; | |
2171 | } | |
61ba1cf9 | 2172 | |
16649710 ILT |
2173 | // Record that this section should link to the normal symbol table. |
2174 | void | |
2175 | set_should_link_to_symtab() | |
2176 | { | |
2177 | gold_assert(this->link_section_ == NULL | |
2178 | && this->link_ == 0 | |
2179 | && !this->should_link_to_dynsym_); | |
2180 | this->should_link_to_symtab_ = true; | |
2181 | } | |
2182 | ||
2183 | // Record that this section should link to the dynamic symbol table. | |
2184 | void | |
2185 | set_should_link_to_dynsym() | |
2186 | { | |
2187 | gold_assert(this->link_section_ == NULL | |
2188 | && this->link_ == 0 | |
2189 | && !this->should_link_to_symtab_); | |
2190 | this->should_link_to_dynsym_ = true; | |
2191 | } | |
2192 | ||
2193 | // Return the info field. | |
2194 | unsigned int | |
2195 | info() const | |
2196 | { | |
755ab8af ILT |
2197 | gold_assert(this->info_section_ == NULL |
2198 | && this->info_symndx_ == NULL); | |
16649710 ILT |
2199 | return this->info_; |
2200 | } | |
2201 | ||
2202 | // Set the info field to the output section index of a section. | |
2203 | void | |
755ab8af | 2204 | set_info_section(const Output_section* os) |
16649710 | 2205 | { |
755ab8af ILT |
2206 | gold_assert((this->info_section_ == NULL |
2207 | || (this->info_section_ == os | |
2208 | && this->info_uses_section_index_)) | |
2209 | && this->info_symndx_ == NULL | |
2210 | && this->info_ == 0); | |
2211 | this->info_section_ = os; | |
2212 | this->info_uses_section_index_= true; | |
16649710 ILT |
2213 | } |
2214 | ||
6a74a719 ILT |
2215 | // Set the info field to the symbol table index of a symbol. |
2216 | void | |
2217 | set_info_symndx(const Symbol* sym) | |
2218 | { | |
755ab8af ILT |
2219 | gold_assert(this->info_section_ == NULL |
2220 | && (this->info_symndx_ == NULL | |
2221 | || this->info_symndx_ == sym) | |
2222 | && this->info_ == 0); | |
6a74a719 ILT |
2223 | this->info_symndx_ = sym; |
2224 | } | |
2225 | ||
755ab8af ILT |
2226 | // Set the info field to the symbol table index of a section symbol. |
2227 | void | |
2228 | set_info_section_symndx(const Output_section* os) | |
2229 | { | |
2230 | gold_assert((this->info_section_ == NULL | |
2231 | || (this->info_section_ == os | |
2232 | && !this->info_uses_section_index_)) | |
2233 | && this->info_symndx_ == NULL | |
2234 | && this->info_ == 0); | |
2235 | this->info_section_ = os; | |
2236 | this->info_uses_section_index_ = false; | |
2237 | } | |
2238 | ||
16649710 | 2239 | // Set the info field to a constant. |
61ba1cf9 ILT |
2240 | void |
2241 | set_info(unsigned int v) | |
16649710 | 2242 | { |
755ab8af ILT |
2243 | gold_assert(this->info_section_ == NULL |
2244 | && this->info_symndx_ == NULL | |
2245 | && (this->info_ == 0 | |
2246 | || this->info_ == v)); | |
16649710 ILT |
2247 | this->info_ = v; |
2248 | } | |
61ba1cf9 ILT |
2249 | |
2250 | // Set the addralign field. | |
2251 | void | |
2252 | set_addralign(uint64_t v) | |
2253 | { this->addralign_ = v; } | |
2254 | ||
d491d34e ILT |
2255 | // Whether the output section index has been set. |
2256 | bool | |
2257 | has_out_shndx() const | |
2258 | { return this->out_shndx_ != -1U; } | |
2259 | ||
c06b7b0b ILT |
2260 | // Indicate that we need a symtab index. |
2261 | void | |
2262 | set_needs_symtab_index() | |
2263 | { this->needs_symtab_index_ = true; } | |
2264 | ||
2265 | // Return whether we need a symtab index. | |
2266 | bool | |
2267 | needs_symtab_index() const | |
2268 | { return this->needs_symtab_index_; } | |
2269 | ||
2270 | // Get the symtab index. | |
2271 | unsigned int | |
2272 | symtab_index() const | |
2273 | { | |
a3ad94ed | 2274 | gold_assert(this->symtab_index_ != 0); |
c06b7b0b ILT |
2275 | return this->symtab_index_; |
2276 | } | |
2277 | ||
2278 | // Set the symtab index. | |
2279 | void | |
2280 | set_symtab_index(unsigned int index) | |
2281 | { | |
a3ad94ed | 2282 | gold_assert(index != 0); |
c06b7b0b ILT |
2283 | this->symtab_index_ = index; |
2284 | } | |
2285 | ||
2286 | // Indicate that we need a dynsym index. | |
2287 | void | |
2288 | set_needs_dynsym_index() | |
2289 | { this->needs_dynsym_index_ = true; } | |
2290 | ||
2291 | // Return whether we need a dynsym index. | |
2292 | bool | |
2293 | needs_dynsym_index() const | |
2294 | { return this->needs_dynsym_index_; } | |
2295 | ||
2296 | // Get the dynsym index. | |
2297 | unsigned int | |
2298 | dynsym_index() const | |
2299 | { | |
a3ad94ed | 2300 | gold_assert(this->dynsym_index_ != 0); |
c06b7b0b ILT |
2301 | return this->dynsym_index_; |
2302 | } | |
2303 | ||
2304 | // Set the dynsym index. | |
2305 | void | |
2306 | set_dynsym_index(unsigned int index) | |
2307 | { | |
a3ad94ed | 2308 | gold_assert(index != 0); |
c06b7b0b ILT |
2309 | this->dynsym_index_ = index; |
2310 | } | |
2311 | ||
2fd32231 ILT |
2312 | // Return whether the input sections sections attachd to this output |
2313 | // section may require sorting. This is used to handle constructor | |
2314 | // priorities compatibly with GNU ld. | |
2315 | bool | |
2316 | may_sort_attached_input_sections() const | |
2317 | { return this->may_sort_attached_input_sections_; } | |
2318 | ||
2319 | // Record that the input sections attached to this output section | |
2320 | // may require sorting. | |
2321 | void | |
2322 | set_may_sort_attached_input_sections() | |
2323 | { this->may_sort_attached_input_sections_ = true; } | |
2324 | ||
2325 | // Return whether the input sections attached to this output section | |
2326 | // require sorting. This is used to handle constructor priorities | |
2327 | // compatibly with GNU ld. | |
2328 | bool | |
2329 | must_sort_attached_input_sections() const | |
2330 | { return this->must_sort_attached_input_sections_; } | |
2331 | ||
2332 | // Record that the input sections attached to this output section | |
2333 | // require sorting. | |
2334 | void | |
2335 | set_must_sort_attached_input_sections() | |
2336 | { this->must_sort_attached_input_sections_ = true; } | |
2337 | ||
9f1d377b ILT |
2338 | // Return whether this section holds relro data--data which has |
2339 | // dynamic relocations but which may be marked read-only after the | |
2340 | // dynamic relocations have been completed. | |
2341 | bool | |
2342 | is_relro() const | |
2343 | { return this->is_relro_; } | |
2344 | ||
2345 | // Record that this section holds relro data. | |
2346 | void | |
2347 | set_is_relro() | |
2348 | { this->is_relro_ = true; } | |
2349 | ||
2d924fd9 ILT |
2350 | // Record that this section does not hold relro data. |
2351 | void | |
2352 | clear_is_relro() | |
2353 | { this->is_relro_ = false; } | |
2354 | ||
9f1d377b ILT |
2355 | // True if this section holds relro local data--relro data for which |
2356 | // the dynamic relocations are all RELATIVE relocations. | |
2357 | bool | |
2358 | is_relro_local() const | |
2359 | { return this->is_relro_local_; } | |
2360 | ||
2361 | // Record that this section holds relro local data. | |
2362 | void | |
2363 | set_is_relro_local() | |
2364 | { this->is_relro_local_ = true; } | |
2365 | ||
1a2dff53 ILT |
2366 | // True if this must be the last relro section. |
2367 | bool | |
2368 | is_last_relro() const | |
2369 | { return this->is_last_relro_; } | |
2370 | ||
2371 | // Record that this must be the last relro section. | |
2372 | void | |
2373 | set_is_last_relro() | |
2374 | { | |
2375 | gold_assert(this->is_relro_); | |
2376 | this->is_last_relro_ = true; | |
2377 | } | |
2378 | ||
2379 | // True if this must be the first section following the relro sections. | |
2380 | bool | |
2381 | is_first_non_relro() const | |
2382 | { | |
2383 | gold_assert(!this->is_relro_); | |
2384 | return this->is_first_non_relro_; | |
2385 | } | |
2386 | ||
2387 | // Record that this must be the first non-relro section. | |
2388 | void | |
2389 | set_is_first_non_relro() | |
2390 | { | |
2391 | gold_assert(!this->is_relro_); | |
2392 | this->is_first_non_relro_ = true; | |
2393 | } | |
2394 | ||
8a5e3e08 ILT |
2395 | // True if this is a small section: a section which holds small |
2396 | // variables. | |
2397 | bool | |
2398 | is_small_section() const | |
2399 | { return this->is_small_section_; } | |
2400 | ||
2401 | // Record that this is a small section. | |
2402 | void | |
2403 | set_is_small_section() | |
2404 | { this->is_small_section_ = true; } | |
2405 | ||
2406 | // True if this is a large section: a section which holds large | |
2407 | // variables. | |
2408 | bool | |
2409 | is_large_section() const | |
2410 | { return this->is_large_section_; } | |
2411 | ||
2412 | // Record that this is a large section. | |
2413 | void | |
2414 | set_is_large_section() | |
2415 | { this->is_large_section_ = true; } | |
2416 | ||
2417 | // True if this is a large data (not BSS) section. | |
2418 | bool | |
2419 | is_large_data_section() | |
2420 | { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; } | |
2421 | ||
f5c870d2 ILT |
2422 | // True if this is the .interp section which goes into the PT_INTERP |
2423 | // segment. | |
2424 | bool | |
2425 | is_interp() const | |
2426 | { return this->is_interp_; } | |
2427 | ||
2428 | // Record that this is the interp section. | |
2429 | void | |
2430 | set_is_interp() | |
2431 | { this->is_interp_ = true; } | |
2432 | ||
2433 | // True if this is a section used by the dynamic linker. | |
2434 | bool | |
2435 | is_dynamic_linker_section() const | |
2436 | { return this->is_dynamic_linker_section_; } | |
2437 | ||
2438 | // Record that this is a section used by the dynamic linker. | |
2439 | void | |
2440 | set_is_dynamic_linker_section() | |
2441 | { this->is_dynamic_linker_section_ = true; } | |
2442 | ||
730cdc88 ILT |
2443 | // Return whether this section should be written after all the input |
2444 | // sections are complete. | |
2445 | bool | |
2446 | after_input_sections() const | |
2447 | { return this->after_input_sections_; } | |
2448 | ||
2449 | // Record that this section should be written after all the input | |
2450 | // sections are complete. | |
2451 | void | |
2452 | set_after_input_sections() | |
2453 | { this->after_input_sections_ = true; } | |
2454 | ||
27bc2bce ILT |
2455 | // Return whether this section requires postprocessing after all |
2456 | // relocations have been applied. | |
2457 | bool | |
2458 | requires_postprocessing() const | |
2459 | { return this->requires_postprocessing_; } | |
2460 | ||
96803768 ILT |
2461 | // If a section requires postprocessing, return the buffer to use. |
2462 | unsigned char* | |
2463 | postprocessing_buffer() const | |
2464 | { | |
2465 | gold_assert(this->postprocessing_buffer_ != NULL); | |
2466 | return this->postprocessing_buffer_; | |
2467 | } | |
2468 | ||
2469 | // If a section requires postprocessing, create the buffer to use. | |
27bc2bce | 2470 | void |
96803768 ILT |
2471 | create_postprocessing_buffer(); |
2472 | ||
2473 | // If a section requires postprocessing, this is the size of the | |
2474 | // buffer to which relocations should be applied. | |
2475 | off_t | |
2476 | postprocessing_buffer_size() const | |
2477 | { return this->current_data_size_for_child(); } | |
27bc2bce | 2478 | |
755ab8af ILT |
2479 | // Modify the section name. This is only permitted for an |
2480 | // unallocated section, and only before the size has been finalized. | |
2481 | // Otherwise the name will not get into Layout::namepool_. | |
2482 | void | |
2483 | set_name(const char* newname) | |
2484 | { | |
2485 | gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0); | |
2486 | gold_assert(!this->is_data_size_valid()); | |
2487 | this->name_ = newname; | |
2488 | } | |
2489 | ||
730cdc88 ILT |
2490 | // Return whether the offset OFFSET in the input section SHNDX in |
2491 | // object OBJECT is being included in the link. | |
2492 | bool | |
2493 | is_input_address_mapped(const Relobj* object, unsigned int shndx, | |
2494 | off_t offset) const; | |
2495 | ||
2496 | // Return the offset within the output section of OFFSET relative to | |
2497 | // the start of input section SHNDX in object OBJECT. | |
8383303e ILT |
2498 | section_offset_type |
2499 | output_offset(const Relobj* object, unsigned int shndx, | |
2500 | section_offset_type offset) const; | |
730cdc88 | 2501 | |
b8e6aad9 ILT |
2502 | // Return the output virtual address of OFFSET relative to the start |
2503 | // of input section SHNDX in object OBJECT. | |
2504 | uint64_t | |
2505 | output_address(const Relobj* object, unsigned int shndx, | |
2506 | off_t offset) const; | |
2507 | ||
e29e076a ILT |
2508 | // Look for the merged section for input section SHNDX in object |
2509 | // OBJECT. If found, return true, and set *ADDR to the address of | |
2510 | // the start of the merged section. This is not necessary the | |
2511 | // output offset corresponding to input offset 0 in the section, | |
2512 | // since the section may be mapped arbitrarily. | |
2513 | bool | |
2514 | find_starting_output_address(const Relobj* object, unsigned int shndx, | |
2515 | uint64_t* addr) const; | |
a9a60db6 | 2516 | |
a445fddf ILT |
2517 | // Record that this output section was found in the SECTIONS clause |
2518 | // of a linker script. | |
2519 | void | |
2520 | set_found_in_sections_clause() | |
2521 | { this->found_in_sections_clause_ = true; } | |
2522 | ||
2523 | // Return whether this output section was found in the SECTIONS | |
2524 | // clause of a linker script. | |
2525 | bool | |
2526 | found_in_sections_clause() const | |
2527 | { return this->found_in_sections_clause_; } | |
2528 | ||
27bc2bce ILT |
2529 | // Write the section header into *OPHDR. |
2530 | template<int size, bool big_endian> | |
2531 | void | |
2532 | write_header(const Layout*, const Stringpool*, | |
2533 | elfcpp::Shdr_write<size, big_endian>*) const; | |
2534 | ||
a445fddf ILT |
2535 | // The next few calls are for linker script support. |
2536 | ||
20e6d0d6 DK |
2537 | // We need to export the input sections to linker scripts. Previously |
2538 | // we export a pair of Relobj pointer and section index. We now need to | |
2539 | // handle relaxed input sections as well. So we use this class. | |
2540 | class Simple_input_section | |
2541 | { | |
2542 | private: | |
2543 | static const unsigned int invalid_shndx = static_cast<unsigned int>(-1); | |
2544 | ||
2545 | public: | |
2ea97941 ILT |
2546 | Simple_input_section(Relobj *relobj, unsigned int shndx) |
2547 | : shndx_(shndx) | |
20e6d0d6 | 2548 | { |
2ea97941 ILT |
2549 | gold_assert(shndx != invalid_shndx); |
2550 | this->u_.relobj = relobj; | |
20e6d0d6 DK |
2551 | } |
2552 | ||
2553 | Simple_input_section(Output_relaxed_input_section* section) | |
2554 | : shndx_(invalid_shndx) | |
2555 | { this->u_.relaxed_input_section = section; } | |
2556 | ||
2557 | // Whether this is a relaxed section. | |
2558 | bool | |
2559 | is_relaxed_input_section() const | |
2560 | { return this->shndx_ == invalid_shndx; } | |
2561 | ||
2562 | // Return object of an input section. | |
2563 | Relobj* | |
2564 | relobj() const | |
2565 | { | |
2566 | return ((this->shndx_ != invalid_shndx) | |
2567 | ? this->u_.relobj | |
2568 | : this->u_.relaxed_input_section->relobj()); | |
2569 | } | |
2570 | ||
2571 | // Return index of an input section. | |
2572 | unsigned int | |
2573 | shndx() const | |
2574 | { | |
2575 | return ((this->shndx_ != invalid_shndx) | |
2576 | ? this->shndx_ | |
2577 | : this->u_.relaxed_input_section->shndx()); | |
2578 | } | |
2579 | ||
2580 | // Return the Output_relaxed_input_section object of a relaxed section. | |
2581 | Output_relaxed_input_section* | |
2582 | relaxed_input_section() const | |
2583 | { | |
2584 | gold_assert(this->shndx_ == invalid_shndx); | |
2585 | return this->u_.relaxed_input_section; | |
2586 | } | |
2587 | ||
2588 | private: | |
2589 | // Pointer to either an Relobj or an Output_relaxed_input_section. | |
2590 | union | |
2591 | { | |
2592 | Relobj* relobj; | |
2593 | Output_relaxed_input_section* relaxed_input_section; | |
2594 | } u_; | |
2595 | // Section index for an non-relaxed section or invalid_shndx for | |
2596 | // a relaxed section. | |
2597 | unsigned int shndx_; | |
2598 | }; | |
2599 | ||
a445fddf ILT |
2600 | // Store the list of input sections for this Output_section into the |
2601 | // list passed in. This removes the input sections, leaving only | |
2602 | // any Output_section_data elements. This returns the size of those | |
2603 | // Output_section_data elements. ADDRESS is the address of this | |
2604 | // output section. FILL is the fill value to use, in case there are | |
2605 | // any spaces between the remaining Output_section_data elements. | |
2606 | uint64_t | |
2607 | get_input_sections(uint64_t address, const std::string& fill, | |
20e6d0d6 | 2608 | std::list<Simple_input_section>*); |
a445fddf ILT |
2609 | |
2610 | // Add an input section from a script. | |
2611 | void | |
20e6d0d6 | 2612 | add_input_section_for_script(const Simple_input_section& input_section, |
a445fddf ILT |
2613 | off_t data_size, uint64_t addralign); |
2614 | ||
2615 | // Set the current size of the output section. | |
2616 | void | |
2617 | set_current_data_size(off_t size) | |
2618 | { this->set_current_data_size_for_child(size); } | |
2619 | ||
2620 | // Get the current size of the output section. | |
2621 | off_t | |
2622 | current_data_size() const | |
2623 | { return this->current_data_size_for_child(); } | |
2624 | ||
2625 | // End of linker script support. | |
2626 | ||
20e6d0d6 DK |
2627 | // Save states before doing section layout. |
2628 | // This is used for relaxation. | |
2629 | void | |
2630 | save_states(); | |
2631 | ||
2632 | // Restore states prior to section layout. | |
2633 | void | |
2634 | restore_states(); | |
2635 | ||
c0a62865 DK |
2636 | // Convert existing input sections to relaxed input sections. |
2637 | void | |
2638 | convert_input_sections_to_relaxed_sections( | |
2639 | const std::vector<Output_relaxed_input_section*>& sections); | |
2640 | ||
6c172549 DK |
2641 | // Find a relaxed input section to an input section in OBJECT |
2642 | // with index SHNDX. Return NULL if none is found. | |
d6344fb5 | 2643 | const Output_relaxed_input_section* |
6c172549 DK |
2644 | find_relaxed_input_section(const Relobj* object, unsigned int shndx) const; |
2645 | ||
38c5e8b4 ILT |
2646 | // Print merge statistics to stderr. |
2647 | void | |
2648 | print_merge_stats(); | |
2649 | ||
27bc2bce | 2650 | protected: |
77e65537 ILT |
2651 | // Return the output section--i.e., the object itself. |
2652 | Output_section* | |
2653 | do_output_section() | |
2654 | { return this; } | |
2655 | ||
ea715a34 ILT |
2656 | const Output_section* |
2657 | do_output_section() const | |
2658 | { return this; } | |
2659 | ||
27bc2bce ILT |
2660 | // Return the section index in the output file. |
2661 | unsigned int | |
2662 | do_out_shndx() const | |
2663 | { | |
2664 | gold_assert(this->out_shndx_ != -1U); | |
2665 | return this->out_shndx_; | |
2666 | } | |
2667 | ||
2668 | // Set the output section index. | |
2669 | void | |
2670 | do_set_out_shndx(unsigned int shndx) | |
2671 | { | |
a445fddf | 2672 | gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx); |
27bc2bce ILT |
2673 | this->out_shndx_ = shndx; |
2674 | } | |
2675 | ||
2676 | // Set the final data size of the Output_section. For a typical | |
ead1e424 | 2677 | // Output_section, there is nothing to do, but if there are any |
27bc2bce | 2678 | // Output_section_data objects we need to set their final addresses |
ead1e424 | 2679 | // here. |
96803768 | 2680 | virtual void |
27bc2bce | 2681 | set_final_data_size(); |
ead1e424 | 2682 | |
a445fddf ILT |
2683 | // Reset the address and file offset. |
2684 | void | |
2685 | do_reset_address_and_file_offset(); | |
2686 | ||
20e6d0d6 DK |
2687 | // Return true if address and file offset already have reset values. In |
2688 | // other words, calling reset_address_and_file_offset will not change them. | |
2689 | bool | |
2690 | do_address_and_file_offset_have_reset_values() const; | |
2691 | ||
54dc6425 | 2692 | // Write the data to the file. For a typical Output_section, this |
ead1e424 ILT |
2693 | // does nothing: the data is written out by calling Object::Relocate |
2694 | // on each input object. But if there are any Output_section_data | |
2695 | // objects we do need to write them out here. | |
96803768 | 2696 | virtual void |
ead1e424 | 2697 | do_write(Output_file*); |
54dc6425 | 2698 | |
75f65a3e ILT |
2699 | // Return the address alignment--function required by parent class. |
2700 | uint64_t | |
2701 | do_addralign() const | |
2702 | { return this->addralign_; } | |
2703 | ||
a445fddf ILT |
2704 | // Return whether there is a load address. |
2705 | bool | |
2706 | do_has_load_address() const | |
2707 | { return this->has_load_address_; } | |
2708 | ||
2709 | // Return the load address. | |
2710 | uint64_t | |
2711 | do_load_address() const | |
2712 | { | |
2713 | gold_assert(this->has_load_address_); | |
2714 | return this->load_address_; | |
2715 | } | |
2716 | ||
75f65a3e ILT |
2717 | // Return whether this is an Output_section. |
2718 | bool | |
2719 | do_is_section() const | |
2720 | { return true; } | |
2721 | ||
54dc6425 ILT |
2722 | // Return whether this is a section of the specified type. |
2723 | bool | |
2ea97941 ILT |
2724 | do_is_section_type(elfcpp::Elf_Word type) const |
2725 | { return this->type_ == type; } | |
54dc6425 ILT |
2726 | |
2727 | // Return whether the specified section flag is set. | |
2728 | bool | |
75f65a3e | 2729 | do_is_section_flag_set(elfcpp::Elf_Xword flag) const |
54dc6425 ILT |
2730 | { return (this->flags_ & flag) != 0; } |
2731 | ||
7bf1f802 ILT |
2732 | // Set the TLS offset. Called only for SHT_TLS sections. |
2733 | void | |
2734 | do_set_tls_offset(uint64_t tls_base); | |
2735 | ||
2736 | // Return the TLS offset, relative to the base of the TLS segment. | |
2737 | // Valid only for SHT_TLS sections. | |
2738 | uint64_t | |
2739 | do_tls_offset() const | |
2740 | { return this->tls_offset_; } | |
2741 | ||
96803768 ILT |
2742 | // This may be implemented by a child class. |
2743 | virtual void | |
2744 | do_finalize_name(Layout*) | |
2745 | { } | |
2746 | ||
7d9e3d98 ILT |
2747 | // Print to the map file. |
2748 | virtual void | |
2749 | do_print_to_mapfile(Mapfile*) const; | |
2750 | ||
96803768 ILT |
2751 | // Record that this section requires postprocessing after all |
2752 | // relocations have been applied. This is called by a child class. | |
2753 | void | |
2754 | set_requires_postprocessing() | |
2755 | { | |
2756 | this->requires_postprocessing_ = true; | |
2757 | this->after_input_sections_ = true; | |
2758 | } | |
2759 | ||
2760 | // Write all the data of an Output_section into the postprocessing | |
2761 | // buffer. | |
2762 | void | |
2763 | write_to_postprocessing_buffer(); | |
2764 | ||
ead1e424 ILT |
2765 | // In some cases we need to keep a list of the input sections |
2766 | // associated with this output section. We only need the list if we | |
2767 | // might have to change the offsets of the input section within the | |
2768 | // output section after we add the input section. The ordinary | |
2769 | // input sections will be written out when we process the object | |
2770 | // file, and as such we don't need to track them here. We do need | |
2771 | // to track Output_section_data objects here. We store instances of | |
2772 | // this structure in a std::vector, so it must be a POD. There can | |
2773 | // be many instances of this structure, so we use a union to save | |
2774 | // some space. | |
2775 | class Input_section | |
2776 | { | |
2777 | public: | |
2778 | Input_section() | |
b8e6aad9 ILT |
2779 | : shndx_(0), p2align_(0) |
2780 | { | |
2781 | this->u1_.data_size = 0; | |
2782 | this->u2_.object = NULL; | |
2783 | } | |
ead1e424 | 2784 | |
b8e6aad9 | 2785 | // For an ordinary input section. |
2ea97941 ILT |
2786 | Input_section(Relobj* object, unsigned int shndx, off_t data_size, |
2787 | uint64_t addralign) | |
2788 | : shndx_(shndx), | |
2789 | p2align_(ffsll(static_cast<long long>(addralign))) | |
ead1e424 | 2790 | { |
2ea97941 ILT |
2791 | gold_assert(shndx != OUTPUT_SECTION_CODE |
2792 | && shndx != MERGE_DATA_SECTION_CODE | |
2793 | && shndx != MERGE_STRING_SECTION_CODE | |
2794 | && shndx != RELAXED_INPUT_SECTION_CODE); | |
2795 | this->u1_.data_size = data_size; | |
b8e6aad9 | 2796 | this->u2_.object = object; |
ead1e424 ILT |
2797 | } |
2798 | ||
b8e6aad9 | 2799 | // For a non-merge output section. |
ead1e424 | 2800 | Input_section(Output_section_data* posd) |
f34787f8 | 2801 | : shndx_(OUTPUT_SECTION_CODE), p2align_(0) |
b8e6aad9 ILT |
2802 | { |
2803 | this->u1_.data_size = 0; | |
2804 | this->u2_.posd = posd; | |
2805 | } | |
2806 | ||
2807 | // For a merge section. | |
2808 | Input_section(Output_section_data* posd, bool is_string, uint64_t entsize) | |
2809 | : shndx_(is_string | |
2810 | ? MERGE_STRING_SECTION_CODE | |
2811 | : MERGE_DATA_SECTION_CODE), | |
f34787f8 | 2812 | p2align_(0) |
b8e6aad9 ILT |
2813 | { |
2814 | this->u1_.entsize = entsize; | |
2815 | this->u2_.posd = posd; | |
2816 | } | |
ead1e424 | 2817 | |
20e6d0d6 DK |
2818 | // For a relaxed input section. |
2819 | Input_section(Output_relaxed_input_section *psection) | |
2820 | : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0) | |
2821 | { | |
2822 | this->u1_.data_size = 0; | |
2823 | this->u2_.poris = psection; | |
2824 | } | |
2825 | ||
ead1e424 ILT |
2826 | // The required alignment. |
2827 | uint64_t | |
2828 | addralign() const | |
a3ad94ed | 2829 | { |
f34787f8 ILT |
2830 | if (!this->is_input_section()) |
2831 | return this->u2_.posd->addralign(); | |
a3ad94ed ILT |
2832 | return (this->p2align_ == 0 |
2833 | ? 0 | |
2834 | : static_cast<uint64_t>(1) << (this->p2align_ - 1)); | |
2835 | } | |
ead1e424 ILT |
2836 | |
2837 | // Return the required size. | |
2838 | off_t | |
2839 | data_size() const; | |
2840 | ||
a445fddf ILT |
2841 | // Whether this is an input section. |
2842 | bool | |
2843 | is_input_section() const | |
2844 | { | |
2845 | return (this->shndx_ != OUTPUT_SECTION_CODE | |
2846 | && this->shndx_ != MERGE_DATA_SECTION_CODE | |
20e6d0d6 DK |
2847 | && this->shndx_ != MERGE_STRING_SECTION_CODE |
2848 | && this->shndx_ != RELAXED_INPUT_SECTION_CODE); | |
a445fddf ILT |
2849 | } |
2850 | ||
b8e6aad9 ILT |
2851 | // Return whether this is a merge section which matches the |
2852 | // parameters. | |
2853 | bool | |
87f95776 | 2854 | is_merge_section(bool is_string, uint64_t entsize, |
2ea97941 | 2855 | uint64_t addralign) const |
b8e6aad9 ILT |
2856 | { |
2857 | return (this->shndx_ == (is_string | |
2858 | ? MERGE_STRING_SECTION_CODE | |
2859 | : MERGE_DATA_SECTION_CODE) | |
87f95776 | 2860 | && this->u1_.entsize == entsize |
2ea97941 | 2861 | && this->addralign() == addralign); |
b8e6aad9 ILT |
2862 | } |
2863 | ||
20e6d0d6 DK |
2864 | // Return whether this is a relaxed input section. |
2865 | bool | |
2866 | is_relaxed_input_section() const | |
2867 | { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; } | |
2868 | ||
2869 | // Return whether this is a generic Output_section_data. | |
2870 | bool | |
2871 | is_output_section_data() const | |
2872 | { | |
2873 | return this->shndx_ == OUTPUT_SECTION_CODE; | |
2874 | } | |
2875 | ||
a445fddf ILT |
2876 | // Return the object for an input section. |
2877 | Relobj* | |
2878 | relobj() const | |
2879 | { | |
c0a62865 DK |
2880 | if (this->is_input_section()) |
2881 | return this->u2_.object; | |
2882 | else if (this->is_relaxed_input_section()) | |
2883 | return this->u2_.poris->relobj(); | |
2884 | else | |
2885 | gold_unreachable(); | |
a445fddf ILT |
2886 | } |
2887 | ||
2888 | // Return the input section index for an input section. | |
2889 | unsigned int | |
2890 | shndx() const | |
2891 | { | |
c0a62865 DK |
2892 | if (this->is_input_section()) |
2893 | return this->shndx_; | |
2894 | else if (this->is_relaxed_input_section()) | |
2895 | return this->u2_.poris->shndx(); | |
2896 | else | |
2897 | gold_unreachable(); | |
a445fddf ILT |
2898 | } |
2899 | ||
20e6d0d6 DK |
2900 | // For non-input-sections, return the associated Output_section_data |
2901 | // object. | |
2902 | Output_section_data* | |
2903 | output_section_data() const | |
2904 | { | |
2905 | gold_assert(!this->is_input_section()); | |
2906 | return this->u2_.posd; | |
2907 | } | |
2908 | ||
2909 | // Return the Output_relaxed_input_section object. | |
2910 | Output_relaxed_input_section* | |
2911 | relaxed_input_section() const | |
2912 | { | |
2913 | gold_assert(this->is_relaxed_input_section()); | |
2914 | return this->u2_.poris; | |
2915 | } | |
2916 | ||
b8e6aad9 ILT |
2917 | // Set the output section. |
2918 | void | |
2919 | set_output_section(Output_section* os) | |
2920 | { | |
2921 | gold_assert(!this->is_input_section()); | |
20e6d0d6 DK |
2922 | Output_section_data *posd = |
2923 | this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd; | |
2924 | posd->set_output_section(os); | |
b8e6aad9 ILT |
2925 | } |
2926 | ||
ead1e424 | 2927 | // Set the address and file offset. This is called during |
96803768 ILT |
2928 | // Layout::finalize. SECTION_FILE_OFFSET is the file offset of |
2929 | // the enclosing section. | |
ead1e424 | 2930 | void |
96803768 ILT |
2931 | set_address_and_file_offset(uint64_t address, off_t file_offset, |
2932 | off_t section_file_offset); | |
ead1e424 | 2933 | |
a445fddf ILT |
2934 | // Reset the address and file offset. |
2935 | void | |
2936 | reset_address_and_file_offset(); | |
2937 | ||
96803768 ILT |
2938 | // Finalize the data size. |
2939 | void | |
2940 | finalize_data_size(); | |
9a0910c3 | 2941 | |
b8e6aad9 ILT |
2942 | // Add an input section, for SHF_MERGE sections. |
2943 | bool | |
2ea97941 | 2944 | add_input_section(Relobj* object, unsigned int shndx) |
b8e6aad9 ILT |
2945 | { |
2946 | gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE | |
2947 | || this->shndx_ == MERGE_STRING_SECTION_CODE); | |
2ea97941 | 2948 | return this->u2_.posd->add_input_section(object, shndx); |
b8e6aad9 ILT |
2949 | } |
2950 | ||
2951 | // Given an input OBJECT, an input section index SHNDX within that | |
2952 | // object, and an OFFSET relative to the start of that input | |
730cdc88 | 2953 | // section, return whether or not the output offset is known. If |
1e983657 ILT |
2954 | // this function returns true, it sets *POUTPUT to the offset in |
2955 | // the output section, relative to the start of the input section | |
2956 | // in the output section. *POUTPUT may be different from OFFSET | |
2957 | // for a merged section. | |
b8e6aad9 | 2958 | bool |
8383303e ILT |
2959 | output_offset(const Relobj* object, unsigned int shndx, |
2960 | section_offset_type offset, | |
2961 | section_offset_type *poutput) const; | |
b8e6aad9 | 2962 | |
a9a60db6 ILT |
2963 | // Return whether this is the merge section for the input section |
2964 | // SHNDX in OBJECT. | |
2965 | bool | |
2966 | is_merge_section_for(const Relobj* object, unsigned int shndx) const; | |
2967 | ||
ead1e424 ILT |
2968 | // Write out the data. This does nothing for an input section. |
2969 | void | |
2970 | write(Output_file*); | |
2971 | ||
96803768 ILT |
2972 | // Write the data to a buffer. This does nothing for an input |
2973 | // section. | |
2974 | void | |
2975 | write_to_buffer(unsigned char*); | |
2976 | ||
7d9e3d98 ILT |
2977 | // Print to a map file. |
2978 | void | |
2979 | print_to_mapfile(Mapfile*) const; | |
2980 | ||
38c5e8b4 ILT |
2981 | // Print statistics about merge sections to stderr. |
2982 | void | |
2983 | print_merge_stats(const char* section_name) | |
2984 | { | |
2985 | if (this->shndx_ == MERGE_DATA_SECTION_CODE | |
2986 | || this->shndx_ == MERGE_STRING_SECTION_CODE) | |
2987 | this->u2_.posd->print_merge_stats(section_name); | |
2988 | } | |
2989 | ||
ead1e424 | 2990 | private: |
b8e6aad9 ILT |
2991 | // Code values which appear in shndx_. If the value is not one of |
2992 | // these codes, it is the input section index in the object file. | |
2993 | enum | |
2994 | { | |
2995 | // An Output_section_data. | |
2996 | OUTPUT_SECTION_CODE = -1U, | |
2997 | // An Output_section_data for an SHF_MERGE section with | |
2998 | // SHF_STRINGS not set. | |
2999 | MERGE_DATA_SECTION_CODE = -2U, | |
3000 | // An Output_section_data for an SHF_MERGE section with | |
3001 | // SHF_STRINGS set. | |
20e6d0d6 DK |
3002 | MERGE_STRING_SECTION_CODE = -3U, |
3003 | // An Output_section_data for a relaxed input section. | |
3004 | RELAXED_INPUT_SECTION_CODE = -4U | |
b8e6aad9 ILT |
3005 | }; |
3006 | ||
b8e6aad9 ILT |
3007 | // For an ordinary input section, this is the section index in the |
3008 | // input file. For an Output_section_data, this is | |
3009 | // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or | |
3010 | // MERGE_STRING_SECTION_CODE. | |
ead1e424 ILT |
3011 | unsigned int shndx_; |
3012 | // The required alignment, stored as a power of 2. | |
3013 | unsigned int p2align_; | |
ead1e424 ILT |
3014 | union |
3015 | { | |
b8e6aad9 ILT |
3016 | // For an ordinary input section, the section size. |
3017 | off_t data_size; | |
20e6d0d6 DK |
3018 | // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not |
3019 | // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the | |
b8e6aad9 ILT |
3020 | // entity size. |
3021 | uint64_t entsize; | |
3022 | } u1_; | |
3023 | union | |
3024 | { | |
3025 | // For an ordinary input section, the object which holds the | |
ead1e424 | 3026 | // input section. |
f6ce93d6 | 3027 | Relobj* object; |
b8e6aad9 ILT |
3028 | // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or |
3029 | // MERGE_STRING_SECTION_CODE, the data. | |
ead1e424 | 3030 | Output_section_data* posd; |
20e6d0d6 DK |
3031 | // For RELAXED_INPUT_SECTION_CODE, the data. |
3032 | Output_relaxed_input_section* poris; | |
b8e6aad9 | 3033 | } u2_; |
ead1e424 ILT |
3034 | }; |
3035 | ||
3036 | typedef std::vector<Input_section> Input_section_list; | |
3037 | ||
c0a62865 DK |
3038 | // Allow a child class to access the input sections. |
3039 | const Input_section_list& | |
3040 | input_sections() const | |
3041 | { return this->input_sections_; } | |
3042 | ||
3043 | private: | |
20e6d0d6 DK |
3044 | // We only save enough information to undo the effects of section layout. |
3045 | class Checkpoint_output_section | |
3046 | { | |
3047 | public: | |
2ea97941 ILT |
3048 | Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags, |
3049 | const Input_section_list& input_sections, | |
3050 | off_t first_input_offset, | |
3051 | bool attached_input_sections_are_sorted) | |
3052 | : addralign_(addralign), flags_(flags), | |
3053 | input_sections_(input_sections), | |
20e6d0d6 | 3054 | input_sections_size_(input_sections_.size()), |
2ea97941 ILT |
3055 | input_sections_copy_(), first_input_offset_(first_input_offset), |
3056 | attached_input_sections_are_sorted_(attached_input_sections_are_sorted) | |
20e6d0d6 DK |
3057 | { } |
3058 | ||
3059 | virtual | |
3060 | ~Checkpoint_output_section() | |
3061 | { } | |
3062 | ||
3063 | // Return the address alignment. | |
3064 | uint64_t | |
3065 | addralign() const | |
3066 | { return this->addralign_; } | |
3067 | ||
3068 | // Return the section flags. | |
3069 | elfcpp::Elf_Xword | |
3070 | flags() const | |
3071 | { return this->flags_; } | |
3072 | ||
3073 | // Return a reference to the input section list copy. | |
c0a62865 DK |
3074 | Input_section_list* |
3075 | input_sections() | |
3076 | { return &this->input_sections_copy_; } | |
20e6d0d6 DK |
3077 | |
3078 | // Return the size of input_sections at the time when checkpoint is | |
3079 | // taken. | |
3080 | size_t | |
3081 | input_sections_size() const | |
3082 | { return this->input_sections_size_; } | |
3083 | ||
3084 | // Whether input sections are copied. | |
3085 | bool | |
3086 | input_sections_saved() const | |
3087 | { return this->input_sections_copy_.size() == this->input_sections_size_; } | |
3088 | ||
3089 | off_t | |
3090 | first_input_offset() const | |
3091 | { return this->first_input_offset_; } | |
3092 | ||
3093 | bool | |
3094 | attached_input_sections_are_sorted() const | |
3095 | { return this->attached_input_sections_are_sorted_; } | |
3096 | ||
3097 | // Save input sections. | |
3098 | void | |
3099 | save_input_sections() | |
3100 | { | |
3101 | this->input_sections_copy_.reserve(this->input_sections_size_); | |
3102 | this->input_sections_copy_.clear(); | |
3103 | Input_section_list::const_iterator p = this->input_sections_.begin(); | |
3104 | gold_assert(this->input_sections_size_ >= this->input_sections_.size()); | |
3105 | for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p) | |
3106 | this->input_sections_copy_.push_back(*p); | |
3107 | } | |
3108 | ||
3109 | private: | |
3110 | // The section alignment. | |
3111 | uint64_t addralign_; | |
3112 | // The section flags. | |
3113 | elfcpp::Elf_Xword flags_; | |
3114 | // Reference to the input sections to be checkpointed. | |
3115 | const Input_section_list& input_sections_; | |
3116 | // Size of the checkpointed portion of input_sections_; | |
3117 | size_t input_sections_size_; | |
3118 | // Copy of input sections. | |
3119 | Input_section_list input_sections_copy_; | |
3120 | // The offset of the first entry in input_sections_. | |
3121 | off_t first_input_offset_; | |
3122 | // True if the input sections attached to this output section have | |
3123 | // already been sorted. | |
3124 | bool attached_input_sections_are_sorted_; | |
3125 | }; | |
3126 | ||
2fd32231 ILT |
3127 | // This class is used to sort the input sections. |
3128 | class Input_section_sort_entry; | |
3129 | ||
3130 | // This is the sort comparison function. | |
3131 | struct Input_section_sort_compare | |
3132 | { | |
3133 | bool | |
3134 | operator()(const Input_section_sort_entry&, | |
3135 | const Input_section_sort_entry&) const; | |
3136 | }; | |
3137 | ||
c51e6221 | 3138 | // Fill data. This is used to fill in data between input sections. |
a445fddf ILT |
3139 | // It is also used for data statements (BYTE, WORD, etc.) in linker |
3140 | // scripts. When we have to keep track of the input sections, we | |
3141 | // can use an Output_data_const, but we don't want to have to keep | |
3142 | // track of input sections just to implement fills. | |
c51e6221 ILT |
3143 | class Fill |
3144 | { | |
3145 | public: | |
2ea97941 ILT |
3146 | Fill(off_t section_offset, off_t length) |
3147 | : section_offset_(section_offset), | |
3148 | length_(convert_to_section_size_type(length)) | |
c51e6221 ILT |
3149 | { } |
3150 | ||
3151 | // Return section offset. | |
3152 | off_t | |
3153 | section_offset() const | |
3154 | { return this->section_offset_; } | |
3155 | ||
3156 | // Return fill length. | |
a445fddf | 3157 | section_size_type |
c51e6221 ILT |
3158 | length() const |
3159 | { return this->length_; } | |
3160 | ||
3161 | private: | |
3162 | // The offset within the output section. | |
3163 | off_t section_offset_; | |
3164 | // The length of the space to fill. | |
a445fddf | 3165 | section_size_type length_; |
c51e6221 ILT |
3166 | }; |
3167 | ||
3168 | typedef std::vector<Fill> Fill_list; | |
3169 | ||
c0a62865 DK |
3170 | // This class describes properties of merge data sections. It is used |
3171 | // as a key type for maps. | |
3172 | class Merge_section_properties | |
3173 | { | |
3174 | public: | |
3175 | Merge_section_properties(bool is_string, uint64_t entsize, | |
3176 | uint64_t addralign) | |
3177 | : is_string_(is_string), entsize_(entsize), addralign_(addralign) | |
3178 | { } | |
3179 | ||
3180 | // Whether this equals to another Merge_section_properties MSP. | |
3181 | bool | |
3182 | eq(const Merge_section_properties& msp) const | |
3183 | { | |
3184 | return ((this->is_string_ == msp.is_string_) | |
3185 | && (this->entsize_ == msp.entsize_) | |
3186 | && (this->addralign_ == msp.addralign_)); | |
3187 | } | |
3188 | ||
3189 | // Compute a hash value for this using 64-bit FNV-1a hash. | |
3190 | size_t | |
3191 | hash_value() const | |
3192 | { | |
3193 | uint64_t h = 14695981039346656037ULL; // FNV offset basis. | |
3194 | uint64_t prime = 1099511628211ULL; | |
3195 | h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime; | |
3196 | h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime; | |
3197 | h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime; | |
3198 | return h; | |
3199 | } | |
3200 | ||
3201 | // Functors for associative containers. | |
3202 | struct equal_to | |
3203 | { | |
3204 | bool | |
3205 | operator()(const Merge_section_properties& msp1, | |
3206 | const Merge_section_properties& msp2) const | |
3207 | { return msp1.eq(msp2); } | |
3208 | }; | |
3209 | ||
3210 | struct hash | |
3211 | { | |
3212 | size_t | |
3213 | operator()(const Merge_section_properties& msp) const | |
3214 | { return msp.hash_value(); } | |
3215 | }; | |
3216 | ||
3217 | private: | |
3218 | // Whether this merge data section is for strings. | |
3219 | bool is_string_; | |
3220 | // Entsize of this merge data section. | |
3221 | uint64_t entsize_; | |
3222 | // Address alignment. | |
3223 | uint64_t addralign_; | |
3224 | }; | |
3225 | ||
3226 | // Map that link Merge_section_properties to Output_merge_base. | |
3227 | typedef Unordered_map<Merge_section_properties, Output_merge_base*, | |
3228 | Merge_section_properties::hash, | |
3229 | Merge_section_properties::equal_to> | |
3230 | Merge_section_by_properties_map; | |
3231 | ||
3232 | // Map that link Input_section_specifier to Output_section_data. | |
3233 | typedef Unordered_map<Input_section_specifier, Output_section_data*, | |
3234 | Input_section_specifier::hash, | |
3235 | Input_section_specifier::equal_to> | |
3236 | Output_section_data_by_input_section_map; | |
3237 | ||
d6344fb5 DK |
3238 | // Map that link Input_section_specifier to Output_relaxed_input_section. |
3239 | typedef Unordered_map<Input_section_specifier, Output_relaxed_input_section*, | |
3240 | Input_section_specifier::hash, | |
3241 | Input_section_specifier::equal_to> | |
3242 | Output_relaxed_input_section_by_input_section_map; | |
3243 | ||
c0a62865 DK |
3244 | // Map used during relaxation of existing sections. This map |
3245 | // an input section specifier to an input section list index. | |
3246 | // We assume that Input_section_list is a vector. | |
3247 | typedef Unordered_map<Input_section_specifier, size_t, | |
3248 | Input_section_specifier::hash, | |
3249 | Input_section_specifier::equal_to> | |
3250 | Relaxation_map; | |
3251 | ||
b8e6aad9 ILT |
3252 | // Add a new output section by Input_section. |
3253 | void | |
3254 | add_output_section_data(Input_section*); | |
3255 | ||
3256 | // Add an SHF_MERGE input section. Returns true if the section was | |
3257 | // handled. | |
3258 | bool | |
3259 | add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags, | |
96803768 | 3260 | uint64_t entsize, uint64_t addralign); |
b8e6aad9 ILT |
3261 | |
3262 | // Add an output SHF_MERGE section POSD to this output section. | |
3263 | // IS_STRING indicates whether it is a SHF_STRINGS section, and | |
3264 | // ENTSIZE is the entity size. This returns the entry added to | |
3265 | // input_sections_. | |
3266 | void | |
3267 | add_output_merge_section(Output_section_data* posd, bool is_string, | |
3268 | uint64_t entsize); | |
3269 | ||
2fd32231 ILT |
3270 | // Sort the attached input sections. |
3271 | void | |
3272 | sort_attached_input_sections(); | |
3273 | ||
c0a62865 DK |
3274 | // Find the merge section into which an input section with index SHNDX in |
3275 | // OBJECT has been added. Return NULL if none found. | |
3276 | Output_section_data* | |
3277 | find_merge_section(const Relobj* object, unsigned int shndx) const; | |
3278 | ||
c0a62865 DK |
3279 | // Build a relaxation map. |
3280 | void | |
3281 | build_relaxation_map( | |
3282 | const Input_section_list& input_sections, | |
3283 | size_t limit, | |
3284 | Relaxation_map* map) const; | |
3285 | ||
3286 | // Convert input sections in an input section list into relaxed sections. | |
3287 | void | |
3288 | convert_input_sections_in_list_to_relaxed_sections( | |
3289 | const std::vector<Output_relaxed_input_section*>& relaxed_sections, | |
3290 | const Relaxation_map& map, | |
3291 | Input_section_list* input_sections); | |
3292 | ||
a2fb1b05 ILT |
3293 | // Most of these fields are only valid after layout. |
3294 | ||
3295 | // The name of the section. This will point into a Stringpool. | |
9a0910c3 | 3296 | const char* name_; |
75f65a3e | 3297 | // The section address is in the parent class. |
a2fb1b05 ILT |
3298 | // The section alignment. |
3299 | uint64_t addralign_; | |
3300 | // The section entry size. | |
3301 | uint64_t entsize_; | |
a445fddf ILT |
3302 | // The load address. This is only used when using a linker script |
3303 | // with a SECTIONS clause. The has_load_address_ field indicates | |
3304 | // whether this field is valid. | |
3305 | uint64_t load_address_; | |
75f65a3e | 3306 | // The file offset is in the parent class. |
16649710 | 3307 | // Set the section link field to the index of this section. |
14b31740 | 3308 | const Output_data* link_section_; |
16649710 | 3309 | // If link_section_ is NULL, this is the link field. |
a2fb1b05 | 3310 | unsigned int link_; |
16649710 | 3311 | // Set the section info field to the index of this section. |
755ab8af | 3312 | const Output_section* info_section_; |
6a74a719 ILT |
3313 | // If info_section_ is NULL, set the info field to the symbol table |
3314 | // index of this symbol. | |
3315 | const Symbol* info_symndx_; | |
3316 | // If info_section_ and info_symndx_ are NULL, this is the section | |
3317 | // info field. | |
a2fb1b05 ILT |
3318 | unsigned int info_; |
3319 | // The section type. | |
27bc2bce | 3320 | const elfcpp::Elf_Word type_; |
a2fb1b05 | 3321 | // The section flags. |
a445fddf | 3322 | elfcpp::Elf_Xword flags_; |
61ba1cf9 | 3323 | // The section index. |
ead1e424 | 3324 | unsigned int out_shndx_; |
c06b7b0b ILT |
3325 | // If there is a STT_SECTION for this output section in the normal |
3326 | // symbol table, this is the symbol index. This starts out as zero. | |
3327 | // It is initialized in Layout::finalize() to be the index, or -1U | |
3328 | // if there isn't one. | |
3329 | unsigned int symtab_index_; | |
3330 | // If there is a STT_SECTION for this output section in the dynamic | |
3331 | // symbol table, this is the symbol index. This starts out as zero. | |
3332 | // It is initialized in Layout::finalize() to be the index, or -1U | |
3333 | // if there isn't one. | |
3334 | unsigned int dynsym_index_; | |
ead1e424 ILT |
3335 | // The input sections. This will be empty in cases where we don't |
3336 | // need to keep track of them. | |
3337 | Input_section_list input_sections_; | |
3338 | // The offset of the first entry in input_sections_. | |
3339 | off_t first_input_offset_; | |
c51e6221 ILT |
3340 | // The fill data. This is separate from input_sections_ because we |
3341 | // often will need fill sections without needing to keep track of | |
3342 | // input sections. | |
3343 | Fill_list fills_; | |
96803768 ILT |
3344 | // If the section requires postprocessing, this buffer holds the |
3345 | // section contents during relocation. | |
3346 | unsigned char* postprocessing_buffer_; | |
c06b7b0b ILT |
3347 | // Whether this output section needs a STT_SECTION symbol in the |
3348 | // normal symbol table. This will be true if there is a relocation | |
3349 | // which needs it. | |
3350 | bool needs_symtab_index_ : 1; | |
3351 | // Whether this output section needs a STT_SECTION symbol in the | |
3352 | // dynamic symbol table. This will be true if there is a dynamic | |
3353 | // relocation which needs it. | |
3354 | bool needs_dynsym_index_ : 1; | |
16649710 ILT |
3355 | // Whether the link field of this output section should point to the |
3356 | // normal symbol table. | |
3357 | bool should_link_to_symtab_ : 1; | |
3358 | // Whether the link field of this output section should point to the | |
3359 | // dynamic symbol table. | |
3360 | bool should_link_to_dynsym_ : 1; | |
730cdc88 ILT |
3361 | // Whether this section should be written after all the input |
3362 | // sections are complete. | |
3363 | bool after_input_sections_ : 1; | |
27bc2bce ILT |
3364 | // Whether this section requires post processing after all |
3365 | // relocations have been applied. | |
3366 | bool requires_postprocessing_ : 1; | |
a445fddf ILT |
3367 | // Whether an input section was mapped to this output section |
3368 | // because of a SECTIONS clause in a linker script. | |
3369 | bool found_in_sections_clause_ : 1; | |
3370 | // Whether this section has an explicitly specified load address. | |
3371 | bool has_load_address_ : 1; | |
755ab8af ILT |
3372 | // True if the info_section_ field means the section index of the |
3373 | // section, false if it means the symbol index of the corresponding | |
3374 | // section symbol. | |
3375 | bool info_uses_section_index_ : 1; | |
2fd32231 ILT |
3376 | // True if the input sections attached to this output section may |
3377 | // need sorting. | |
3378 | bool may_sort_attached_input_sections_ : 1; | |
3379 | // True if the input sections attached to this output section must | |
3380 | // be sorted. | |
3381 | bool must_sort_attached_input_sections_ : 1; | |
3382 | // True if the input sections attached to this output section have | |
3383 | // already been sorted. | |
3384 | bool attached_input_sections_are_sorted_ : 1; | |
9f1d377b ILT |
3385 | // True if this section holds relro data. |
3386 | bool is_relro_ : 1; | |
3387 | // True if this section holds relro local data. | |
3388 | bool is_relro_local_ : 1; | |
1a2dff53 ILT |
3389 | // True if this must be the last relro section. |
3390 | bool is_last_relro_ : 1; | |
3391 | // True if this must be the first section after the relro sections. | |
3392 | bool is_first_non_relro_ : 1; | |
8a5e3e08 ILT |
3393 | // True if this is a small section. |
3394 | bool is_small_section_ : 1; | |
3395 | // True if this is a large section. | |
3396 | bool is_large_section_ : 1; | |
f5c870d2 ILT |
3397 | // True if this is the .interp section going into the PT_INTERP |
3398 | // segment. | |
3399 | bool is_interp_ : 1; | |
3400 | // True if this is section is read by the dynamic linker. | |
3401 | bool is_dynamic_linker_section_ : 1; | |
3402 | // Whether code-fills are generated at write. | |
3403 | bool generate_code_fills_at_write_ : 1; | |
e8cd95c7 ILT |
3404 | // Whether the entry size field should be zero. |
3405 | bool is_entsize_zero_ : 1; | |
7bf1f802 ILT |
3406 | // For SHT_TLS sections, the offset of this section relative to the base |
3407 | // of the TLS segment. | |
3408 | uint64_t tls_offset_; | |
20e6d0d6 DK |
3409 | // Saved checkpoint. |
3410 | Checkpoint_output_section* checkpoint_; | |
c0a62865 DK |
3411 | // Map from input sections to merge sections. |
3412 | Output_section_data_by_input_section_map merge_section_map_; | |
3413 | // Map from merge section properties to merge_sections; | |
3414 | Merge_section_by_properties_map merge_section_by_properties_map_; | |
3415 | // Map from input sections to relaxed input sections. This is mutable | |
f5c870d2 | 3416 | // because it is updated lazily. We may need to update it in a |
c0a62865 | 3417 | // const qualified method. |
d6344fb5 DK |
3418 | mutable Output_relaxed_input_section_by_input_section_map |
3419 | relaxed_input_section_map_; | |
c0a62865 DK |
3420 | // Whether relaxed_input_section_map_ is valid. |
3421 | mutable bool is_relaxed_input_section_map_valid_; | |
a2fb1b05 ILT |
3422 | }; |
3423 | ||
3424 | // An output segment. PT_LOAD segments are built from collections of | |
3425 | // output sections. Other segments typically point within PT_LOAD | |
3426 | // segments, and are built directly as needed. | |
20e6d0d6 DK |
3427 | // |
3428 | // NOTE: We want to use the copy constructor for this class. During | |
3429 | // relaxation, we may try built the segments multiple times. We do | |
3430 | // that by copying the original segment list before lay-out, doing | |
3431 | // a trial lay-out and roll-back to the saved copied if we need to | |
3432 | // to the lay-out again. | |
a2fb1b05 ILT |
3433 | |
3434 | class Output_segment | |
3435 | { | |
3436 | public: | |
3437 | // Create an output segment, specifying the type and flags. | |
3438 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); | |
3439 | ||
3440 | // Return the virtual address. | |
3441 | uint64_t | |
3442 | vaddr() const | |
3443 | { return this->vaddr_; } | |
3444 | ||
3445 | // Return the physical address. | |
3446 | uint64_t | |
3447 | paddr() const | |
3448 | { return this->paddr_; } | |
3449 | ||
3450 | // Return the segment type. | |
3451 | elfcpp::Elf_Word | |
3452 | type() const | |
3453 | { return this->type_; } | |
3454 | ||
3455 | // Return the segment flags. | |
3456 | elfcpp::Elf_Word | |
3457 | flags() const | |
3458 | { return this->flags_; } | |
3459 | ||
92e059d8 ILT |
3460 | // Return the memory size. |
3461 | uint64_t | |
3462 | memsz() const | |
3463 | { return this->memsz_; } | |
3464 | ||
ead1e424 ILT |
3465 | // Return the file size. |
3466 | off_t | |
3467 | filesz() const | |
3468 | { return this->filesz_; } | |
3469 | ||
516cb3d0 ILT |
3470 | // Return the file offset. |
3471 | off_t | |
3472 | offset() const | |
3473 | { return this->offset_; } | |
3474 | ||
8a5e3e08 ILT |
3475 | // Whether this is a segment created to hold large data sections. |
3476 | bool | |
3477 | is_large_data_segment() const | |
3478 | { return this->is_large_data_segment_; } | |
3479 | ||
3480 | // Record that this is a segment created to hold large data | |
3481 | // sections. | |
3482 | void | |
3483 | set_is_large_data_segment() | |
3484 | { this->is_large_data_segment_ = true; } | |
3485 | ||
75f65a3e ILT |
3486 | // Return the maximum alignment of the Output_data. |
3487 | uint64_t | |
a445fddf | 3488 | maximum_alignment(); |
75f65a3e | 3489 | |
f5c870d2 ILT |
3490 | // Add the Output_section OS to this segment. SEG_FLAGS is the |
3491 | // segment flags to use. DO_SORT is true if we should sort the | |
3492 | // placement of the input section for more efficient generated code. | |
a2fb1b05 | 3493 | void |
f5c870d2 ILT |
3494 | add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags, |
3495 | bool do_sort); | |
75f65a3e | 3496 | |
1650c4ff ILT |
3497 | // Remove an Output_section from this segment. It is an error if it |
3498 | // is not present. | |
3499 | void | |
3500 | remove_output_section(Output_section* os); | |
3501 | ||
a192ba05 ILT |
3502 | // Add an Output_data (which need not be an Output_section) to the |
3503 | // start of this segment. | |
75f65a3e ILT |
3504 | void |
3505 | add_initial_output_data(Output_data*); | |
3506 | ||
756ac4a8 ILT |
3507 | // Return true if this segment has any sections which hold actual |
3508 | // data, rather than being a BSS section. | |
3509 | bool | |
3510 | has_any_data_sections() const | |
3511 | { return !this->output_data_.empty(); } | |
3512 | ||
4f4c5f80 ILT |
3513 | // Return the number of dynamic relocations applied to this segment. |
3514 | unsigned int | |
3515 | dynamic_reloc_count() const; | |
3516 | ||
a445fddf ILT |
3517 | // Return the address of the first section. |
3518 | uint64_t | |
3519 | first_section_load_address() const; | |
3520 | ||
3521 | // Return whether the addresses have been set already. | |
3522 | bool | |
3523 | are_addresses_set() const | |
3524 | { return this->are_addresses_set_; } | |
3525 | ||
3526 | // Set the addresses. | |
3527 | void | |
2ea97941 | 3528 | set_addresses(uint64_t vaddr, uint64_t paddr) |
a445fddf | 3529 | { |
2ea97941 ILT |
3530 | this->vaddr_ = vaddr; |
3531 | this->paddr_ = paddr; | |
a445fddf ILT |
3532 | this->are_addresses_set_ = true; |
3533 | } | |
3534 | ||
a192ba05 ILT |
3535 | // Update the flags for the flags of an output section added to this |
3536 | // segment. | |
3537 | void | |
3538 | update_flags_for_output_section(elfcpp::Elf_Xword flags) | |
3539 | { | |
3540 | // The ELF ABI specifies that a PT_TLS segment should always have | |
3541 | // PF_R as the flags. | |
3542 | if (this->type() != elfcpp::PT_TLS) | |
3543 | this->flags_ |= flags; | |
3544 | } | |
3545 | ||
1c4f3631 ILT |
3546 | // Set the segment flags. This is only used if we have a PHDRS |
3547 | // clause which explicitly specifies the flags. | |
3548 | void | |
2ea97941 ILT |
3549 | set_flags(elfcpp::Elf_Word flags) |
3550 | { this->flags_ = flags; } | |
1c4f3631 | 3551 | |
75f65a3e | 3552 | // Set the address of the segment to ADDR and the offset to *POFF |
a445fddf ILT |
3553 | // and set the addresses and offsets of all contained output |
3554 | // sections accordingly. Set the section indexes of all contained | |
3555 | // output sections starting with *PSHNDX. If RESET is true, first | |
3556 | // reset the addresses of the contained sections. Return the | |
3557 | // address of the immediately following segment. Update *POFF and | |
3558 | // *PSHNDX. This should only be called for a PT_LOAD segment. | |
75f65a3e | 3559 | uint64_t |
1a2dff53 ILT |
3560 | set_section_addresses(const Layout*, bool reset, uint64_t addr, |
3561 | unsigned int increase_relro, off_t* poff, | |
a445fddf | 3562 | unsigned int* pshndx); |
75f65a3e | 3563 | |
0496d5e5 ILT |
3564 | // Set the minimum alignment of this segment. This may be adjusted |
3565 | // upward based on the section alignments. | |
3566 | void | |
a445fddf ILT |
3567 | set_minimum_p_align(uint64_t align) |
3568 | { this->min_p_align_ = align; } | |
0496d5e5 | 3569 | |
75f65a3e ILT |
3570 | // Set the offset of this segment based on the section. This should |
3571 | // only be called for a non-PT_LOAD segment. | |
3572 | void | |
1a2dff53 | 3573 | set_offset(unsigned int increase); |
75f65a3e | 3574 | |
7bf1f802 ILT |
3575 | // Set the TLS offsets of the sections contained in the PT_TLS segment. |
3576 | void | |
3577 | set_tls_offsets(); | |
3578 | ||
75f65a3e ILT |
3579 | // Return the number of output sections. |
3580 | unsigned int | |
3581 | output_section_count() const; | |
a2fb1b05 | 3582 | |
1c4f3631 ILT |
3583 | // Return the section attached to the list segment with the lowest |
3584 | // load address. This is used when handling a PHDRS clause in a | |
3585 | // linker script. | |
3586 | Output_section* | |
3587 | section_with_lowest_load_address() const; | |
3588 | ||
61ba1cf9 ILT |
3589 | // Write the segment header into *OPHDR. |
3590 | template<int size, bool big_endian> | |
3591 | void | |
ead1e424 | 3592 | write_header(elfcpp::Phdr_write<size, big_endian>*); |
61ba1cf9 ILT |
3593 | |
3594 | // Write the section headers of associated sections into V. | |
3595 | template<int size, bool big_endian> | |
3596 | unsigned char* | |
16649710 | 3597 | write_section_headers(const Layout*, const Stringpool*, unsigned char* v, |
7d1a9ebb | 3598 | unsigned int* pshndx) const; |
61ba1cf9 | 3599 | |
7d9e3d98 ILT |
3600 | // Print the output sections in the map file. |
3601 | void | |
3602 | print_sections_to_mapfile(Mapfile*) const; | |
3603 | ||
a2fb1b05 | 3604 | private: |
54dc6425 | 3605 | typedef std::list<Output_data*> Output_data_list; |
a2fb1b05 | 3606 | |
ead1e424 ILT |
3607 | // Find the maximum alignment in an Output_data_list. |
3608 | static uint64_t | |
a445fddf | 3609 | maximum_alignment_list(const Output_data_list*); |
ead1e424 | 3610 | |
9f1d377b ILT |
3611 | // Return whether the first data section is a relro section. |
3612 | bool | |
3613 | is_first_section_relro() const; | |
3614 | ||
75f65a3e ILT |
3615 | // Set the section addresses in an Output_data_list. |
3616 | uint64_t | |
96a2b4e4 ILT |
3617 | set_section_list_addresses(const Layout*, bool reset, Output_data_list*, |
3618 | uint64_t addr, off_t* poff, unsigned int* pshndx, | |
1a2dff53 | 3619 | bool* in_tls); |
75f65a3e ILT |
3620 | |
3621 | // Return the number of Output_sections in an Output_data_list. | |
3622 | unsigned int | |
3623 | output_section_count_list(const Output_data_list*) const; | |
3624 | ||
4f4c5f80 ILT |
3625 | // Return the number of dynamic relocs in an Output_data_list. |
3626 | unsigned int | |
3627 | dynamic_reloc_count_list(const Output_data_list*) const; | |
3628 | ||
1c4f3631 ILT |
3629 | // Find the section with the lowest load address in an |
3630 | // Output_data_list. | |
3631 | void | |
3632 | lowest_load_address_in_list(const Output_data_list* pdl, | |
3633 | Output_section** found, | |
3634 | uint64_t* found_lma) const; | |
3635 | ||
61ba1cf9 ILT |
3636 | // Write the section headers in the list into V. |
3637 | template<int size, bool big_endian> | |
3638 | unsigned char* | |
16649710 ILT |
3639 | write_section_headers_list(const Layout*, const Stringpool*, |
3640 | const Output_data_list*, unsigned char* v, | |
7d1a9ebb | 3641 | unsigned int* pshdx) const; |
61ba1cf9 | 3642 | |
7d9e3d98 ILT |
3643 | // Print a section list to the mapfile. |
3644 | void | |
3645 | print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const; | |
3646 | ||
20e6d0d6 DK |
3647 | // NOTE: We want to use the copy constructor. Currently, shallow copy |
3648 | // works for us so we do not need to write our own copy constructor. | |
3649 | ||
75f65a3e | 3650 | // The list of output data with contents attached to this segment. |
54dc6425 | 3651 | Output_data_list output_data_; |
75f65a3e ILT |
3652 | // The list of output data without contents attached to this segment. |
3653 | Output_data_list output_bss_; | |
a2fb1b05 ILT |
3654 | // The segment virtual address. |
3655 | uint64_t vaddr_; | |
3656 | // The segment physical address. | |
3657 | uint64_t paddr_; | |
3658 | // The size of the segment in memory. | |
3659 | uint64_t memsz_; | |
a445fddf ILT |
3660 | // The maximum section alignment. The is_max_align_known_ field |
3661 | // indicates whether this has been finalized. | |
3662 | uint64_t max_align_; | |
3663 | // The required minimum value for the p_align field. This is used | |
3664 | // for PT_LOAD segments. Note that this does not mean that | |
3665 | // addresses should be aligned to this value; it means the p_paddr | |
3666 | // and p_vaddr fields must be congruent modulo this value. For | |
3667 | // non-PT_LOAD segments, the dynamic linker works more efficiently | |
3668 | // if the p_align field has the more conventional value, although it | |
3669 | // can align as needed. | |
3670 | uint64_t min_p_align_; | |
a2fb1b05 ILT |
3671 | // The offset of the segment data within the file. |
3672 | off_t offset_; | |
3673 | // The size of the segment data in the file. | |
3674 | off_t filesz_; | |
3675 | // The segment type; | |
3676 | elfcpp::Elf_Word type_; | |
3677 | // The segment flags. | |
3678 | elfcpp::Elf_Word flags_; | |
a445fddf ILT |
3679 | // Whether we have finalized max_align_. |
3680 | bool is_max_align_known_ : 1; | |
3681 | // Whether vaddr and paddr were set by a linker script. | |
3682 | bool are_addresses_set_ : 1; | |
8a5e3e08 ILT |
3683 | // Whether this segment holds large data sections. |
3684 | bool is_large_data_segment_ : 1; | |
a2fb1b05 ILT |
3685 | }; |
3686 | ||
61ba1cf9 | 3687 | // This class represents the output file. |
a2fb1b05 ILT |
3688 | |
3689 | class Output_file | |
3690 | { | |
3691 | public: | |
14144f39 | 3692 | Output_file(const char* name); |
61ba1cf9 | 3693 | |
516cb3d0 ILT |
3694 | // Indicate that this is a temporary file which should not be |
3695 | // output. | |
3696 | void | |
3697 | set_is_temporary() | |
3698 | { this->is_temporary_ = true; } | |
3699 | ||
404c2abb ILT |
3700 | // Try to open an existing file. Returns false if the file doesn't |
3701 | // exist, has a size of 0 or can't be mmaped. This method is | |
3702 | // thread-unsafe. | |
3703 | bool | |
3704 | open_for_modification(); | |
3705 | ||
61ba1cf9 | 3706 | // Open the output file. FILE_SIZE is the final size of the file. |
404c2abb ILT |
3707 | // If the file already exists, it is deleted/truncated. This method |
3708 | // is thread-unsafe. | |
61ba1cf9 ILT |
3709 | void |
3710 | open(off_t file_size); | |
3711 | ||
404c2abb | 3712 | // Resize the output file. This method is thread-unsafe. |
27bc2bce ILT |
3713 | void |
3714 | resize(off_t file_size); | |
3715 | ||
c420411f | 3716 | // Close the output file (flushing all buffered data) and make sure |
404c2abb | 3717 | // there are no errors. This method is thread-unsafe. |
61ba1cf9 ILT |
3718 | void |
3719 | close(); | |
3720 | ||
c549a694 ILT |
3721 | // Return the size of this file. |
3722 | off_t | |
3723 | filesize() | |
3724 | { return this->file_size_; } | |
3725 | ||
3aec4f9c RÁE |
3726 | // Return the name of this file. |
3727 | const char* | |
3728 | filename() | |
3729 | { return this->name_; } | |
3730 | ||
61ba1cf9 ILT |
3731 | // We currently always use mmap which makes the view handling quite |
3732 | // simple. In the future we may support other approaches. | |
a2fb1b05 ILT |
3733 | |
3734 | // Write data to the output file. | |
3735 | void | |
fe8718a4 | 3736 | write(off_t offset, const void* data, size_t len) |
61ba1cf9 ILT |
3737 | { memcpy(this->base_ + offset, data, len); } |
3738 | ||
3739 | // Get a buffer to use to write to the file, given the offset into | |
3740 | // the file and the size. | |
3741 | unsigned char* | |
fe8718a4 | 3742 | get_output_view(off_t start, size_t size) |
61ba1cf9 | 3743 | { |
8d32f935 ILT |
3744 | gold_assert(start >= 0 |
3745 | && start + static_cast<off_t>(size) <= this->file_size_); | |
61ba1cf9 ILT |
3746 | return this->base_ + start; |
3747 | } | |
3748 | ||
3749 | // VIEW must have been returned by get_output_view. Write the | |
3750 | // buffer to the file, passing in the offset and the size. | |
3751 | void | |
fe8718a4 | 3752 | write_output_view(off_t, size_t, unsigned char*) |
61ba1cf9 ILT |
3753 | { } |
3754 | ||
730cdc88 ILT |
3755 | // Get a read/write buffer. This is used when we want to write part |
3756 | // of the file, read it in, and write it again. | |
3757 | unsigned char* | |
fe8718a4 | 3758 | get_input_output_view(off_t start, size_t size) |
730cdc88 ILT |
3759 | { return this->get_output_view(start, size); } |
3760 | ||
3761 | // Write a read/write buffer back to the file. | |
3762 | void | |
fe8718a4 | 3763 | write_input_output_view(off_t, size_t, unsigned char*) |
730cdc88 ILT |
3764 | { } |
3765 | ||
3766 | // Get a read buffer. This is used when we just want to read part | |
3767 | // of the file back it in. | |
3768 | const unsigned char* | |
fe8718a4 | 3769 | get_input_view(off_t start, size_t size) |
730cdc88 ILT |
3770 | { return this->get_output_view(start, size); } |
3771 | ||
3772 | // Release a read bfufer. | |
3773 | void | |
fe8718a4 | 3774 | free_input_view(off_t, size_t, const unsigned char*) |
730cdc88 ILT |
3775 | { } |
3776 | ||
61ba1cf9 | 3777 | private: |
404c2abb ILT |
3778 | // Map the file into memory or, if that fails, allocate anonymous |
3779 | // memory. | |
27bc2bce ILT |
3780 | void |
3781 | map(); | |
3782 | ||
26736d8e | 3783 | // Allocate anonymous memory for the file. |
404c2abb | 3784 | bool |
26736d8e ILT |
3785 | map_anonymous(); |
3786 | ||
404c2abb ILT |
3787 | // Map the file into memory. |
3788 | bool | |
3789 | map_no_anonymous(); | |
3790 | ||
c420411f ILT |
3791 | // Unmap the file from memory (and flush to disk buffers). |
3792 | void | |
3793 | unmap(); | |
3794 | ||
61ba1cf9 ILT |
3795 | // File name. |
3796 | const char* name_; | |
3797 | // File descriptor. | |
3798 | int o_; | |
3799 | // File size. | |
3800 | off_t file_size_; | |
3801 | // Base of file mapped into memory. | |
3802 | unsigned char* base_; | |
c420411f ILT |
3803 | // True iff base_ points to a memory buffer rather than an output file. |
3804 | bool map_is_anonymous_; | |
516cb3d0 ILT |
3805 | // True if this is a temporary file which should not be output. |
3806 | bool is_temporary_; | |
a2fb1b05 ILT |
3807 | }; |
3808 | ||
3809 | } // End namespace gold. | |
3810 | ||
3811 | #endif // !defined(GOLD_OUTPUT_H) |