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