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