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