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