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