]>
Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // output.h -- manage the output file for gold -*- C++ -*- |
2 | ||
e29e076a | 3 | // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <[email protected]>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
a2fb1b05 ILT |
23 | #ifndef GOLD_OUTPUT_H |
24 | #define GOLD_OUTPUT_H | |
25 | ||
26 | #include <list> | |
ead1e424 | 27 | #include <vector> |
a2fb1b05 ILT |
28 | |
29 | #include "elfcpp.h" | |
7d9e3d98 | 30 | #include "mapfile.h" |
54dc6425 | 31 | #include "layout.h" |
c06b7b0b | 32 | #include "reloc-types.h" |
a2fb1b05 ILT |
33 | |
34 | namespace gold | |
35 | { | |
36 | ||
61ba1cf9 | 37 | class General_options; |
a2fb1b05 | 38 | class Object; |
a3ad94ed | 39 | class Symbol; |
a2fb1b05 | 40 | class Output_file; |
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 | 876 | |
eff45813 CC |
877 | static const Address invalid_address = static_cast<Address>(0) - 1; |
878 | ||
c06b7b0b ILT |
879 | // An uninitialized entry. We need this because we want to put |
880 | // instances of this class into an STL container. | |
881 | Output_reloc() | |
882 | : local_sym_index_(INVALID_CODE) | |
883 | { } | |
884 | ||
dceae3c1 ILT |
885 | // We have a bunch of different constructors. They come in pairs |
886 | // depending on how the address of the relocation is specified. It | |
887 | // can either be an offset in an Output_data or an offset in an | |
888 | // input section. | |
889 | ||
c06b7b0b | 890 | // A reloc against a global symbol. |
5a6f7e2d | 891 | |
a3ad94ed | 892 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
e8c846c3 | 893 | Address address, bool is_relative); |
5a6f7e2d | 894 | |
ef9beddf ILT |
895 | Output_reloc(Symbol* gsym, unsigned int type, |
896 | Sized_relobj<size, big_endian>* relobj, | |
e8c846c3 | 897 | unsigned int shndx, Address address, bool is_relative); |
c06b7b0b | 898 | |
dceae3c1 | 899 | // A reloc against a local symbol or local section symbol. |
5a6f7e2d ILT |
900 | |
901 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
7bf1f802 | 902 | unsigned int local_sym_index, unsigned int type, |
dceae3c1 ILT |
903 | Output_data* od, Address address, bool is_relative, |
904 | bool is_section_symbol); | |
5a6f7e2d ILT |
905 | |
906 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
7bf1f802 | 907 | unsigned int local_sym_index, unsigned int type, |
dceae3c1 ILT |
908 | unsigned int shndx, Address address, bool is_relative, |
909 | bool is_section_symbol); | |
c06b7b0b ILT |
910 | |
911 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 912 | |
a3ad94ed | 913 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
7bf1f802 | 914 | Address address); |
5a6f7e2d | 915 | |
ef9beddf ILT |
916 | Output_reloc(Output_section* os, unsigned int type, |
917 | Sized_relobj<size, big_endian>* relobj, | |
7bf1f802 | 918 | unsigned int shndx, Address address); |
c06b7b0b | 919 | |
e8c846c3 ILT |
920 | // Return TRUE if this is a RELATIVE relocation. |
921 | bool | |
922 | is_relative() const | |
923 | { return this->is_relative_; } | |
924 | ||
dceae3c1 ILT |
925 | // Return whether this is against a local section symbol. |
926 | bool | |
927 | is_local_section_symbol() const | |
928 | { | |
929 | return (this->local_sym_index_ != GSYM_CODE | |
930 | && this->local_sym_index_ != SECTION_CODE | |
931 | && this->local_sym_index_ != INVALID_CODE | |
932 | && this->is_section_symbol_); | |
933 | } | |
934 | ||
935 | // For a local section symbol, return the offset of the input | |
624f8810 ILT |
936 | // section within the output section. ADDEND is the addend being |
937 | // applied to the input section. | |
ef9beddf | 938 | Address |
624f8810 | 939 | local_section_offset(Addend addend) const; |
dceae3c1 | 940 | |
d1f003c6 ILT |
941 | // Get the value of the symbol referred to by a Rel relocation when |
942 | // we are adding the given ADDEND. | |
e8c846c3 | 943 | Address |
624f8810 | 944 | symbol_value(Addend addend) const; |
e8c846c3 | 945 | |
c06b7b0b ILT |
946 | // Write the reloc entry to an output view. |
947 | void | |
948 | write(unsigned char* pov) const; | |
949 | ||
950 | // Write the offset and info fields to Write_rel. | |
951 | template<typename Write_rel> | |
952 | void write_rel(Write_rel*) const; | |
953 | ||
d98bc257 ILT |
954 | // This is used when sorting dynamic relocs. Return -1 to sort this |
955 | // reloc before R2, 0 to sort the same as R2, 1 to sort after R2. | |
956 | int | |
957 | compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2) | |
958 | const; | |
959 | ||
960 | // Return whether this reloc should be sorted before the argument | |
961 | // when sorting dynamic relocs. | |
962 | bool | |
963 | sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& | |
964 | r2) const | |
965 | { return this->compare(r2) < 0; } | |
966 | ||
c06b7b0b | 967 | private: |
dceae3c1 ILT |
968 | // Record that we need a dynamic symbol index. |
969 | void | |
970 | set_needs_dynsym_index(); | |
971 | ||
972 | // Return the symbol index. | |
c06b7b0b ILT |
973 | unsigned int |
974 | get_symbol_index() const; | |
975 | ||
d98bc257 | 976 | // Return the output address. |
a984ee1d | 977 | Address |
d98bc257 ILT |
978 | get_address() const; |
979 | ||
c06b7b0b ILT |
980 | // Codes for local_sym_index_. |
981 | enum | |
982 | { | |
983 | // Global symbol. | |
984 | GSYM_CODE = -1U, | |
985 | // Output section. | |
986 | SECTION_CODE = -2U, | |
987 | // Invalid uninitialized entry. | |
988 | INVALID_CODE = -3U | |
989 | }; | |
990 | ||
991 | union | |
992 | { | |
dceae3c1 ILT |
993 | // For a local symbol or local section symbol |
994 | // (this->local_sym_index_ >= 0), the object. We will never | |
995 | // generate a relocation against a local symbol in a dynamic | |
996 | // object; that doesn't make sense. And our callers will always | |
997 | // be templatized, so we use Sized_relobj here. | |
5a6f7e2d | 998 | Sized_relobj<size, big_endian>* relobj; |
dceae3c1 ILT |
999 | // For a global symbol (this->local_sym_index_ == GSYM_CODE, the |
1000 | // symbol. If this is NULL, it indicates a relocation against the | |
1001 | // undefined 0 symbol. | |
c06b7b0b | 1002 | Symbol* gsym; |
dceae3c1 ILT |
1003 | // For a relocation against an output section |
1004 | // (this->local_sym_index_ == SECTION_CODE), the output section. | |
c06b7b0b | 1005 | Output_section* os; |
5a6f7e2d ILT |
1006 | } u1_; |
1007 | union | |
1008 | { | |
dceae3c1 ILT |
1009 | // If this->shndx_ is not INVALID CODE, the object which holds the |
1010 | // input section being used to specify the reloc address. | |
ef9beddf | 1011 | Sized_relobj<size, big_endian>* relobj; |
dceae3c1 | 1012 | // If this->shndx_ is INVALID_CODE, the output data being used to |
5a6f7e2d ILT |
1013 | // specify the reloc address. This may be NULL if the reloc |
1014 | // address is absolute. | |
1015 | Output_data* od; | |
1016 | } u2_; | |
1017 | // The address offset within the input section or the Output_data. | |
1018 | Address address_; | |
dceae3c1 ILT |
1019 | // This is GSYM_CODE for a global symbol, or SECTION_CODE for a |
1020 | // relocation against an output section, or INVALID_CODE for an | |
1021 | // uninitialized value. Otherwise, for a local symbol | |
1022 | // (this->is_section_symbol_ is false), the local symbol index. For | |
1023 | // a local section symbol (this->is_section_symbol_ is true), the | |
1024 | // section index in the input file. | |
c06b7b0b | 1025 | unsigned int local_sym_index_; |
a3ad94ed | 1026 | // The reloc type--a processor specific code. |
dceae3c1 | 1027 | unsigned int type_ : 30; |
e8c846c3 ILT |
1028 | // True if the relocation is a RELATIVE relocation. |
1029 | bool is_relative_ : 1; | |
dceae3c1 ILT |
1030 | // True if the relocation is against a section symbol. |
1031 | bool is_section_symbol_ : 1; | |
5a6f7e2d ILT |
1032 | // If the reloc address is an input section in an object, the |
1033 | // section index. This is INVALID_CODE if the reloc address is | |
1034 | // specified in some other way. | |
1035 | unsigned int shndx_; | |
c06b7b0b ILT |
1036 | }; |
1037 | ||
1038 | // The SHT_RELA version of Output_reloc<>. This is just derived from | |
1039 | // the SHT_REL version of Output_reloc, but it adds an addend. | |
1040 | ||
1041 | template<bool dynamic, int size, bool big_endian> | |
1042 | class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
1043 | { | |
1044 | public: | |
1045 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
1046 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; | |
1047 | ||
1048 | // An uninitialized entry. | |
1049 | Output_reloc() | |
1050 | : rel_() | |
1051 | { } | |
1052 | ||
1053 | // A reloc against a global symbol. | |
5a6f7e2d | 1054 | |
a3ad94ed | 1055 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
e8c846c3 ILT |
1056 | Address address, Addend addend, bool is_relative) |
1057 | : rel_(gsym, type, od, address, is_relative), addend_(addend) | |
c06b7b0b ILT |
1058 | { } |
1059 | ||
ef9beddf ILT |
1060 | Output_reloc(Symbol* gsym, unsigned int type, |
1061 | Sized_relobj<size, big_endian>* relobj, | |
e8c846c3 ILT |
1062 | unsigned int shndx, Address address, Addend addend, |
1063 | bool is_relative) | |
1064 | : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend) | |
5a6f7e2d ILT |
1065 | { } |
1066 | ||
c06b7b0b | 1067 | // A reloc against a local symbol. |
5a6f7e2d ILT |
1068 | |
1069 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
e8c846c3 ILT |
1070 | unsigned int local_sym_index, unsigned int type, |
1071 | Output_data* od, Address address, | |
dceae3c1 ILT |
1072 | Addend addend, bool is_relative, bool is_section_symbol) |
1073 | : rel_(relobj, local_sym_index, type, od, address, is_relative, | |
1074 | is_section_symbol), | |
e8c846c3 | 1075 | addend_(addend) |
5a6f7e2d ILT |
1076 | { } |
1077 | ||
1078 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
e8c846c3 ILT |
1079 | unsigned int local_sym_index, unsigned int type, |
1080 | unsigned int shndx, Address address, | |
dceae3c1 ILT |
1081 | Addend addend, bool is_relative, bool is_section_symbol) |
1082 | : rel_(relobj, local_sym_index, type, shndx, address, is_relative, | |
1083 | is_section_symbol), | |
5a6f7e2d | 1084 | addend_(addend) |
c06b7b0b ILT |
1085 | { } |
1086 | ||
1087 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 1088 | |
a3ad94ed ILT |
1089 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
1090 | Address address, Addend addend) | |
1091 | : rel_(os, type, od, address), addend_(addend) | |
c06b7b0b ILT |
1092 | { } |
1093 | ||
ef9beddf ILT |
1094 | Output_reloc(Output_section* os, unsigned int type, |
1095 | Sized_relobj<size, big_endian>* relobj, | |
5a6f7e2d ILT |
1096 | unsigned int shndx, Address address, Addend addend) |
1097 | : rel_(os, type, relobj, shndx, address), addend_(addend) | |
1098 | { } | |
1099 | ||
c06b7b0b ILT |
1100 | // Write the reloc entry to an output view. |
1101 | void | |
1102 | write(unsigned char* pov) const; | |
1103 | ||
d98bc257 ILT |
1104 | // Return whether this reloc should be sorted before the argument |
1105 | // when sorting dynamic relocs. | |
1106 | bool | |
1107 | sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>& | |
1108 | r2) const | |
1109 | { | |
1110 | int i = this->rel_.compare(r2.rel_); | |
1111 | if (i < 0) | |
d98bc257 | 1112 | return true; |
cc28ec61 ILT |
1113 | else if (i > 0) |
1114 | return false; | |
d98bc257 ILT |
1115 | else |
1116 | return this->addend_ < r2.addend_; | |
1117 | } | |
1118 | ||
c06b7b0b ILT |
1119 | private: |
1120 | // The basic reloc. | |
1121 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_; | |
1122 | // The addend. | |
1123 | Addend addend_; | |
1124 | }; | |
1125 | ||
1126 | // Output_data_reloc is used to manage a section containing relocs. | |
1127 | // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC | |
1128 | // indicates whether this is a dynamic relocation or a normal | |
1129 | // relocation. Output_data_reloc_base is a base class. | |
1130 | // Output_data_reloc is the real class, which we specialize based on | |
1131 | // the reloc type. | |
1132 | ||
1133 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
27bc2bce | 1134 | class Output_data_reloc_base : public Output_section_data_build |
c06b7b0b ILT |
1135 | { |
1136 | public: | |
1137 | typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type; | |
1138 | typedef typename Output_reloc_type::Address Address; | |
1139 | static const int reloc_size = | |
1140 | Reloc_types<sh_type, size, big_endian>::reloc_size; | |
1141 | ||
1142 | // Construct the section. | |
d98bc257 ILT |
1143 | Output_data_reloc_base(bool sort_relocs) |
1144 | : Output_section_data_build(Output_data::default_alignment_for_size(size)), | |
1145 | sort_relocs_(sort_relocs) | |
c06b7b0b ILT |
1146 | { } |
1147 | ||
27bc2bce | 1148 | protected: |
c06b7b0b ILT |
1149 | // Write out the data. |
1150 | void | |
1151 | do_write(Output_file*); | |
1152 | ||
16649710 ILT |
1153 | // Set the entry size and the link. |
1154 | void | |
1155 | do_adjust_output_section(Output_section *os); | |
1156 | ||
7d9e3d98 ILT |
1157 | // Write to a map file. |
1158 | void | |
1159 | do_print_to_mapfile(Mapfile* mapfile) const | |
1160 | { | |
1161 | mapfile->print_output_data(this, | |
1162 | (dynamic | |
1163 | ? _("** dynamic relocs") | |
1164 | : _("** relocs"))); | |
1165 | } | |
1166 | ||
c06b7b0b ILT |
1167 | // Add a relocation entry. |
1168 | void | |
4f4c5f80 | 1169 | add(Output_data *od, const Output_reloc_type& reloc) |
c06b7b0b ILT |
1170 | { |
1171 | this->relocs_.push_back(reloc); | |
27bc2bce | 1172 | this->set_current_data_size(this->relocs_.size() * reloc_size); |
4f4c5f80 | 1173 | od->add_dynamic_reloc(); |
c06b7b0b ILT |
1174 | } |
1175 | ||
1176 | private: | |
1177 | typedef std::vector<Output_reloc_type> Relocs; | |
1178 | ||
d98bc257 ILT |
1179 | // The class used to sort the relocations. |
1180 | struct Sort_relocs_comparison | |
1181 | { | |
1182 | bool | |
1183 | operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const | |
1184 | { return r1.sort_before(r2); } | |
1185 | }; | |
1186 | ||
1187 | // The relocations in this section. | |
c06b7b0b | 1188 | Relocs relocs_; |
d98bc257 ILT |
1189 | // Whether to sort the relocations when writing them out, to make |
1190 | // the dynamic linker more efficient. | |
1191 | bool sort_relocs_; | |
c06b7b0b ILT |
1192 | }; |
1193 | ||
1194 | // The class which callers actually create. | |
1195 | ||
1196 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
1197 | class Output_data_reloc; | |
1198 | ||
1199 | // The SHT_REL version of Output_data_reloc. | |
1200 | ||
1201 | template<bool dynamic, int size, bool big_endian> | |
1202 | class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
1203 | : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian> | |
1204 | { | |
dceae3c1 | 1205 | private: |
c06b7b0b ILT |
1206 | typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, |
1207 | big_endian> Base; | |
1208 | ||
1209 | public: | |
1210 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
1211 | typedef typename Output_reloc_type::Address Address; | |
1212 | ||
d98bc257 ILT |
1213 | Output_data_reloc(bool sr) |
1214 | : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr) | |
c06b7b0b ILT |
1215 | { } |
1216 | ||
1217 | // Add a reloc against a global symbol. | |
5a6f7e2d | 1218 | |
c06b7b0b | 1219 | void |
a3ad94ed | 1220 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address) |
e8c846c3 | 1221 | { this->add(od, Output_reloc_type(gsym, type, od, address, false)); } |
c06b7b0b | 1222 | |
5a6f7e2d | 1223 | void |
ef9beddf ILT |
1224 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
1225 | Sized_relobj<size, big_endian>* relobj, | |
5a6f7e2d | 1226 | unsigned int shndx, Address address) |
e8c846c3 ILT |
1227 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
1228 | false)); } | |
1229 | ||
12c0daef ILT |
1230 | // These are to simplify the Copy_relocs class. |
1231 | ||
1232 | void | |
1233 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address, | |
1234 | Address addend) | |
1235 | { | |
1236 | gold_assert(addend == 0); | |
1237 | this->add_global(gsym, type, od, address); | |
1238 | } | |
1239 | ||
1240 | void | |
ef9beddf ILT |
1241 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
1242 | Sized_relobj<size, big_endian>* relobj, | |
12c0daef ILT |
1243 | unsigned int shndx, Address address, Address addend) |
1244 | { | |
1245 | gold_assert(addend == 0); | |
1246 | this->add_global(gsym, type, od, relobj, shndx, address); | |
1247 | } | |
1248 | ||
e8c846c3 ILT |
1249 | // Add a RELATIVE reloc against a global symbol. The final relocation |
1250 | // will not reference the symbol. | |
1251 | ||
1252 | void | |
1253 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, | |
1254 | Address address) | |
1255 | { this->add(od, Output_reloc_type(gsym, type, od, address, true)); } | |
1256 | ||
1257 | void | |
1258 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, | |
ef9beddf ILT |
1259 | Sized_relobj<size, big_endian>* relobj, |
1260 | unsigned int shndx, Address address) | |
dceae3c1 ILT |
1261 | { |
1262 | this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, | |
1263 | true)); | |
1264 | } | |
5a6f7e2d | 1265 | |
c06b7b0b | 1266 | // Add a reloc against a local symbol. |
5a6f7e2d | 1267 | |
c06b7b0b | 1268 | void |
5a6f7e2d | 1269 | add_local(Sized_relobj<size, big_endian>* relobj, |
a3ad94ed ILT |
1270 | unsigned int local_sym_index, unsigned int type, |
1271 | Output_data* od, Address address) | |
dceae3c1 ILT |
1272 | { |
1273 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, | |
1274 | address, false, false)); | |
1275 | } | |
5a6f7e2d ILT |
1276 | |
1277 | void | |
1278 | add_local(Sized_relobj<size, big_endian>* relobj, | |
1279 | unsigned int local_sym_index, unsigned int type, | |
4f4c5f80 | 1280 | Output_data* od, unsigned int shndx, Address address) |
dceae3c1 ILT |
1281 | { |
1282 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
1283 | address, false, false)); | |
1284 | } | |
e8c846c3 ILT |
1285 | |
1286 | // Add a RELATIVE reloc against a local symbol. | |
5a6f7e2d | 1287 | |
e8c846c3 ILT |
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, Address address) | |
dceae3c1 ILT |
1292 | { |
1293 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, | |
1294 | address, true, false)); | |
1295 | } | |
e8c846c3 ILT |
1296 | |
1297 | void | |
1298 | add_local_relative(Sized_relobj<size, big_endian>* relobj, | |
1299 | unsigned int local_sym_index, unsigned int type, | |
1300 | Output_data* od, unsigned int shndx, Address address) | |
dceae3c1 ILT |
1301 | { |
1302 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
1303 | address, true, false)); | |
1304 | } | |
1305 | ||
1306 | // Add a reloc against a local section symbol. This will be | |
1307 | // converted into a reloc against the STT_SECTION symbol of the | |
1308 | // output section. | |
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, Address address) | |
1314 | { | |
1315 | this->add(od, Output_reloc_type(relobj, input_shndx, type, od, | |
1316 | address, false, true)); | |
1317 | } | |
1318 | ||
1319 | void | |
1320 | add_local_section(Sized_relobj<size, big_endian>* relobj, | |
1321 | unsigned int input_shndx, unsigned int type, | |
1322 | Output_data* od, unsigned int shndx, Address address) | |
1323 | { | |
1324 | this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx, | |
1325 | address, false, true)); | |
1326 | } | |
c06b7b0b ILT |
1327 | |
1328 | // A reloc against the STT_SECTION symbol of an output section. | |
4f4c5f80 ILT |
1329 | // OS is the Output_section that the relocation refers to; OD is |
1330 | // the Output_data object being relocated. | |
5a6f7e2d | 1331 | |
c06b7b0b | 1332 | void |
a3ad94ed ILT |
1333 | add_output_section(Output_section* os, unsigned int type, |
1334 | Output_data* od, Address address) | |
4f4c5f80 | 1335 | { this->add(od, Output_reloc_type(os, type, od, address)); } |
5a6f7e2d ILT |
1336 | |
1337 | void | |
4f4c5f80 | 1338 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
ef9beddf ILT |
1339 | Sized_relobj<size, big_endian>* relobj, |
1340 | unsigned int shndx, Address address) | |
4f4c5f80 | 1341 | { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); } |
c06b7b0b ILT |
1342 | }; |
1343 | ||
1344 | // The SHT_RELA version of Output_data_reloc. | |
1345 | ||
1346 | template<bool dynamic, int size, bool big_endian> | |
1347 | class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
1348 | : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
1349 | { | |
dceae3c1 | 1350 | private: |
c06b7b0b ILT |
1351 | typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, |
1352 | big_endian> Base; | |
1353 | ||
1354 | public: | |
1355 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
1356 | typedef typename Output_reloc_type::Address Address; | |
1357 | typedef typename Output_reloc_type::Addend Addend; | |
1358 | ||
d98bc257 ILT |
1359 | Output_data_reloc(bool sr) |
1360 | : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr) | |
c06b7b0b ILT |
1361 | { } |
1362 | ||
1363 | // Add a reloc against a global symbol. | |
5a6f7e2d | 1364 | |
c06b7b0b | 1365 | void |
a3ad94ed ILT |
1366 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
1367 | Address address, Addend addend) | |
e8c846c3 ILT |
1368 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend, |
1369 | false)); } | |
c06b7b0b | 1370 | |
5a6f7e2d | 1371 | void |
ef9beddf ILT |
1372 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
1373 | Sized_relobj<size, big_endian>* relobj, | |
4f4c5f80 ILT |
1374 | unsigned int shndx, Address address, |
1375 | Addend addend) | |
1376 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, | |
e8c846c3 ILT |
1377 | addend, false)); } |
1378 | ||
1379 | // Add a RELATIVE reloc against a global symbol. The final output | |
1380 | // relocation will not reference the symbol, but we must keep the symbol | |
1381 | // information long enough to set the addend of the relocation correctly | |
1382 | // when it is written. | |
1383 | ||
1384 | void | |
1385 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, | |
1386 | Address address, Addend addend) | |
1387 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); } | |
1388 | ||
1389 | void | |
1390 | add_global_relative(Symbol* gsym, unsigned int type, Output_data* od, | |
ef9beddf ILT |
1391 | Sized_relobj<size, big_endian>* relobj, |
1392 | unsigned int shndx, Address address, Addend addend) | |
e8c846c3 ILT |
1393 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, |
1394 | addend, true)); } | |
5a6f7e2d | 1395 | |
c06b7b0b | 1396 | // Add a reloc against a local symbol. |
5a6f7e2d | 1397 | |
c06b7b0b | 1398 | void |
5a6f7e2d | 1399 | add_local(Sized_relobj<size, big_endian>* relobj, |
c06b7b0b | 1400 | unsigned int local_sym_index, unsigned int type, |
a3ad94ed | 1401 | Output_data* od, Address address, Addend addend) |
c06b7b0b | 1402 | { |
4f4c5f80 | 1403 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
dceae3c1 | 1404 | addend, false, false)); |
5a6f7e2d ILT |
1405 | } |
1406 | ||
1407 | void | |
1408 | add_local(Sized_relobj<size, big_endian>* relobj, | |
1409 | unsigned int local_sym_index, unsigned int type, | |
4f4c5f80 ILT |
1410 | Output_data* od, unsigned int shndx, Address address, |
1411 | Addend addend) | |
5a6f7e2d | 1412 | { |
4f4c5f80 | 1413 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
dceae3c1 | 1414 | address, addend, false, false)); |
e8c846c3 ILT |
1415 | } |
1416 | ||
1417 | // Add a RELATIVE reloc against a local symbol. | |
1418 | ||
1419 | void | |
1420 | add_local_relative(Sized_relobj<size, big_endian>* relobj, | |
1421 | unsigned int local_sym_index, unsigned int type, | |
1422 | Output_data* od, Address address, Addend addend) | |
1423 | { | |
1424 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, | |
dceae3c1 | 1425 | addend, true, false)); |
e8c846c3 ILT |
1426 | } |
1427 | ||
1428 | void | |
1429 | add_local_relative(Sized_relobj<size, big_endian>* relobj, | |
1430 | unsigned int local_sym_index, unsigned int type, | |
1431 | Output_data* od, unsigned int shndx, Address address, | |
1432 | Addend addend) | |
1433 | { | |
1434 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
dceae3c1 ILT |
1435 | address, addend, true, false)); |
1436 | } | |
1437 | ||
1438 | // Add a reloc against a local section symbol. This will be | |
1439 | // converted into a reloc against the STT_SECTION symbol of the | |
1440 | // output section. | |
1441 | ||
1442 | void | |
1443 | add_local_section(Sized_relobj<size, big_endian>* relobj, | |
1444 | unsigned int input_shndx, unsigned int type, | |
1445 | Output_data* od, Address address, Addend addend) | |
1446 | { | |
1447 | this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address, | |
1448 | addend, false, true)); | |
1449 | } | |
1450 | ||
1451 | void | |
1452 | add_local_section(Sized_relobj<size, big_endian>* relobj, | |
1453 | unsigned int input_shndx, unsigned int type, | |
1454 | Output_data* od, unsigned int shndx, Address address, | |
1455 | Addend addend) | |
1456 | { | |
1457 | this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx, | |
1458 | address, addend, false, true)); | |
c06b7b0b ILT |
1459 | } |
1460 | ||
1461 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 1462 | |
c06b7b0b | 1463 | void |
a3ad94ed ILT |
1464 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
1465 | Address address, Addend addend) | |
4f4c5f80 | 1466 | { this->add(os, Output_reloc_type(os, type, od, address, addend)); } |
5a6f7e2d ILT |
1467 | |
1468 | void | |
ef9beddf ILT |
1469 | add_output_section(Output_section* os, unsigned int type, |
1470 | Sized_relobj<size, big_endian>* relobj, | |
5a6f7e2d | 1471 | unsigned int shndx, Address address, Addend addend) |
4f4c5f80 ILT |
1472 | { this->add(os, Output_reloc_type(os, type, relobj, shndx, address, |
1473 | addend)); } | |
c06b7b0b ILT |
1474 | }; |
1475 | ||
6a74a719 ILT |
1476 | // Output_relocatable_relocs represents a relocation section in a |
1477 | // relocatable link. The actual data is written out in the target | |
1478 | // hook relocate_for_relocatable. This just saves space for it. | |
1479 | ||
1480 | template<int sh_type, int size, bool big_endian> | |
1481 | class Output_relocatable_relocs : public Output_section_data | |
1482 | { | |
1483 | public: | |
1484 | Output_relocatable_relocs(Relocatable_relocs* rr) | |
1485 | : Output_section_data(Output_data::default_alignment_for_size(size)), | |
1486 | rr_(rr) | |
1487 | { } | |
1488 | ||
1489 | void | |
1490 | set_final_data_size(); | |
1491 | ||
1492 | // Write out the data. There is nothing to do here. | |
1493 | void | |
1494 | do_write(Output_file*) | |
1495 | { } | |
1496 | ||
7d9e3d98 ILT |
1497 | // Write to a map file. |
1498 | void | |
1499 | do_print_to_mapfile(Mapfile* mapfile) const | |
1500 | { mapfile->print_output_data(this, _("** relocs")); } | |
1501 | ||
6a74a719 ILT |
1502 | private: |
1503 | // The relocs associated with this input section. | |
1504 | Relocatable_relocs* rr_; | |
1505 | }; | |
1506 | ||
1507 | // Handle a GROUP section. | |
1508 | ||
1509 | template<int size, bool big_endian> | |
1510 | class Output_data_group : public Output_section_data | |
1511 | { | |
1512 | public: | |
8825ac63 | 1513 | // The constructor clears *INPUT_SHNDXES. |
6a74a719 ILT |
1514 | Output_data_group(Sized_relobj<size, big_endian>* relobj, |
1515 | section_size_type entry_count, | |
8825ac63 ILT |
1516 | elfcpp::Elf_Word flags, |
1517 | std::vector<unsigned int>* input_shndxes); | |
6a74a719 ILT |
1518 | |
1519 | void | |
1520 | do_write(Output_file*); | |
1521 | ||
7d9e3d98 ILT |
1522 | // Write to a map file. |
1523 | void | |
1524 | do_print_to_mapfile(Mapfile* mapfile) const | |
1525 | { mapfile->print_output_data(this, _("** group")); } | |
1526 | ||
6a74a719 ILT |
1527 | private: |
1528 | // The input object. | |
1529 | Sized_relobj<size, big_endian>* relobj_; | |
1530 | // The group flag word. | |
1531 | elfcpp::Elf_Word flags_; | |
1532 | // The section indexes of the input sections in this group. | |
8825ac63 | 1533 | std::vector<unsigned int> input_shndxes_; |
6a74a719 ILT |
1534 | }; |
1535 | ||
dbe717ef ILT |
1536 | // Output_data_got is used to manage a GOT. Each entry in the GOT is |
1537 | // for one symbol--either a global symbol or a local symbol in an | |
ead1e424 | 1538 | // object. The target specific code adds entries to the GOT as |
dbe717ef | 1539 | // needed. |
ead1e424 ILT |
1540 | |
1541 | template<int size, bool big_endian> | |
27bc2bce | 1542 | class Output_data_got : public Output_section_data_build |
ead1e424 ILT |
1543 | { |
1544 | public: | |
1545 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; | |
7bf1f802 ILT |
1546 | typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn; |
1547 | typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn; | |
ead1e424 | 1548 | |
7e1edb90 | 1549 | Output_data_got() |
27bc2bce | 1550 | : Output_section_data_build(Output_data::default_alignment_for_size(size)), |
730cdc88 | 1551 | entries_() |
ead1e424 ILT |
1552 | { } |
1553 | ||
dbe717ef ILT |
1554 | // Add an entry for a global symbol to the GOT. Return true if this |
1555 | // is a new GOT entry, false if the symbol was already in the GOT. | |
1556 | bool | |
0a65a3a7 | 1557 | add_global(Symbol* gsym, unsigned int got_type); |
ead1e424 | 1558 | |
7bf1f802 ILT |
1559 | // Add an entry for a global symbol to the GOT, and add a dynamic |
1560 | // relocation of type R_TYPE for the GOT entry. | |
1561 | void | |
0a65a3a7 CC |
1562 | add_global_with_rel(Symbol* gsym, unsigned int got_type, |
1563 | Rel_dyn* rel_dyn, unsigned int r_type); | |
7bf1f802 ILT |
1564 | |
1565 | void | |
0a65a3a7 CC |
1566 | add_global_with_rela(Symbol* gsym, unsigned int got_type, |
1567 | Rela_dyn* rela_dyn, unsigned int r_type); | |
1568 | ||
1569 | // Add a pair of entries for a global symbol to the GOT, and add | |
1570 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. | |
1571 | void | |
1572 | add_global_pair_with_rel(Symbol* gsym, unsigned int got_type, | |
1573 | Rel_dyn* rel_dyn, unsigned int r_type_1, | |
1574 | unsigned int r_type_2); | |
1575 | ||
1576 | void | |
1577 | add_global_pair_with_rela(Symbol* gsym, unsigned int got_type, | |
1578 | Rela_dyn* rela_dyn, unsigned int r_type_1, | |
1579 | unsigned int r_type_2); | |
7bf1f802 | 1580 | |
e727fa71 ILT |
1581 | // Add an entry for a local symbol to the GOT. This returns true if |
1582 | // this is a new GOT entry, false if the symbol already has a GOT | |
1583 | // entry. | |
1584 | bool | |
0a65a3a7 CC |
1585 | add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index, |
1586 | unsigned int got_type); | |
ead1e424 | 1587 | |
0a65a3a7 | 1588 | // Add an entry for a local symbol to the GOT, and add a dynamic |
7bf1f802 ILT |
1589 | // relocation of type R_TYPE for the GOT entry. |
1590 | void | |
1591 | add_local_with_rel(Sized_relobj<size, big_endian>* object, | |
0a65a3a7 CC |
1592 | unsigned int sym_index, unsigned int got_type, |
1593 | Rel_dyn* rel_dyn, unsigned int r_type); | |
7bf1f802 ILT |
1594 | |
1595 | void | |
1596 | add_local_with_rela(Sized_relobj<size, big_endian>* object, | |
0a65a3a7 CC |
1597 | unsigned int sym_index, unsigned int got_type, |
1598 | Rela_dyn* rela_dyn, unsigned int r_type); | |
07f397ab | 1599 | |
0a65a3a7 CC |
1600 | // Add a pair of entries for a local symbol to the GOT, and add |
1601 | // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively. | |
7bf1f802 | 1602 | void |
0a65a3a7 CC |
1603 | add_local_pair_with_rel(Sized_relobj<size, big_endian>* object, |
1604 | unsigned int sym_index, unsigned int shndx, | |
1605 | unsigned int got_type, Rel_dyn* rel_dyn, | |
1606 | unsigned int r_type_1, unsigned int r_type_2); | |
7bf1f802 ILT |
1607 | |
1608 | void | |
0a65a3a7 CC |
1609 | add_local_pair_with_rela(Sized_relobj<size, big_endian>* object, |
1610 | unsigned int sym_index, unsigned int shndx, | |
1611 | unsigned int got_type, Rela_dyn* rela_dyn, | |
1612 | unsigned int r_type_1, unsigned int r_type_2); | |
7bf1f802 | 1613 | |
ead1e424 ILT |
1614 | // Add a constant to the GOT. This returns the offset of the new |
1615 | // entry from the start of the GOT. | |
1616 | unsigned int | |
1617 | add_constant(Valtype constant) | |
1618 | { | |
1619 | this->entries_.push_back(Got_entry(constant)); | |
1620 | this->set_got_size(); | |
1621 | return this->last_got_offset(); | |
1622 | } | |
1623 | ||
27bc2bce | 1624 | protected: |
ead1e424 ILT |
1625 | // Write out the GOT table. |
1626 | void | |
1627 | do_write(Output_file*); | |
1628 | ||
7d9e3d98 ILT |
1629 | // Write to a map file. |
1630 | void | |
1631 | do_print_to_mapfile(Mapfile* mapfile) const | |
1632 | { mapfile->print_output_data(this, _("** GOT")); } | |
1633 | ||
ead1e424 ILT |
1634 | private: |
1635 | // This POD class holds a single GOT entry. | |
1636 | class Got_entry | |
1637 | { | |
1638 | public: | |
1639 | // Create a zero entry. | |
1640 | Got_entry() | |
1641 | : local_sym_index_(CONSTANT_CODE) | |
1642 | { this->u_.constant = 0; } | |
1643 | ||
1644 | // Create a global symbol entry. | |
a3ad94ed | 1645 | explicit Got_entry(Symbol* gsym) |
ead1e424 ILT |
1646 | : local_sym_index_(GSYM_CODE) |
1647 | { this->u_.gsym = gsym; } | |
1648 | ||
1649 | // Create a local symbol entry. | |
e727fa71 ILT |
1650 | Got_entry(Sized_relobj<size, big_endian>* object, |
1651 | unsigned int local_sym_index) | |
ead1e424 ILT |
1652 | : local_sym_index_(local_sym_index) |
1653 | { | |
a3ad94ed ILT |
1654 | gold_assert(local_sym_index != GSYM_CODE |
1655 | && local_sym_index != CONSTANT_CODE); | |
ead1e424 ILT |
1656 | this->u_.object = object; |
1657 | } | |
1658 | ||
1659 | // Create a constant entry. The constant is a host value--it will | |
1660 | // be swapped, if necessary, when it is written out. | |
a3ad94ed | 1661 | explicit Got_entry(Valtype constant) |
ead1e424 ILT |
1662 | : local_sym_index_(CONSTANT_CODE) |
1663 | { this->u_.constant = constant; } | |
1664 | ||
1665 | // Write the GOT entry to an output view. | |
1666 | void | |
7e1edb90 | 1667 | write(unsigned char* pov) const; |
ead1e424 ILT |
1668 | |
1669 | private: | |
1670 | enum | |
1671 | { | |
1672 | GSYM_CODE = -1U, | |
1673 | CONSTANT_CODE = -2U | |
1674 | }; | |
1675 | ||
1676 | union | |
1677 | { | |
1678 | // For a local symbol, the object. | |
e727fa71 | 1679 | Sized_relobj<size, big_endian>* object; |
ead1e424 ILT |
1680 | // For a global symbol, the symbol. |
1681 | Symbol* gsym; | |
1682 | // For a constant, the constant. | |
1683 | Valtype constant; | |
1684 | } u_; | |
c06b7b0b ILT |
1685 | // For a local symbol, the local symbol index. This is GSYM_CODE |
1686 | // for a global symbol, or CONSTANT_CODE for a constant. | |
ead1e424 ILT |
1687 | unsigned int local_sym_index_; |
1688 | }; | |
1689 | ||
1690 | typedef std::vector<Got_entry> Got_entries; | |
1691 | ||
1692 | // Return the offset into the GOT of GOT entry I. | |
1693 | unsigned int | |
1694 | got_offset(unsigned int i) const | |
1695 | { return i * (size / 8); } | |
1696 | ||
1697 | // Return the offset into the GOT of the last entry added. | |
1698 | unsigned int | |
1699 | last_got_offset() const | |
1700 | { return this->got_offset(this->entries_.size() - 1); } | |
1701 | ||
1702 | // Set the size of the section. | |
1703 | void | |
1704 | set_got_size() | |
27bc2bce | 1705 | { this->set_current_data_size(this->got_offset(this->entries_.size())); } |
ead1e424 ILT |
1706 | |
1707 | // The list of GOT entries. | |
1708 | Got_entries entries_; | |
1709 | }; | |
1710 | ||
a3ad94ed ILT |
1711 | // Output_data_dynamic is used to hold the data in SHT_DYNAMIC |
1712 | // section. | |
1713 | ||
1714 | class Output_data_dynamic : public Output_section_data | |
1715 | { | |
1716 | public: | |
9025d29d | 1717 | Output_data_dynamic(Stringpool* pool) |
730cdc88 | 1718 | : Output_section_data(Output_data::default_alignment()), |
9025d29d | 1719 | entries_(), pool_(pool) |
a3ad94ed ILT |
1720 | { } |
1721 | ||
1722 | // Add a new dynamic entry with a fixed numeric value. | |
1723 | void | |
1724 | add_constant(elfcpp::DT tag, unsigned int val) | |
1725 | { this->add_entry(Dynamic_entry(tag, val)); } | |
1726 | ||
16649710 | 1727 | // Add a new dynamic entry with the address of output data. |
a3ad94ed | 1728 | void |
16649710 ILT |
1729 | add_section_address(elfcpp::DT tag, const Output_data* od) |
1730 | { this->add_entry(Dynamic_entry(tag, od, false)); } | |
a3ad94ed | 1731 | |
c2b45e22 CC |
1732 | // Add a new dynamic entry with the address of output data |
1733 | // plus a constant offset. | |
1734 | void | |
1735 | add_section_plus_offset(elfcpp::DT tag, const Output_data* od, | |
1736 | unsigned int offset) | |
1737 | { this->add_entry(Dynamic_entry(tag, od, offset)); } | |
1738 | ||
16649710 | 1739 | // Add a new dynamic entry with the size of output data. |
a3ad94ed | 1740 | void |
16649710 ILT |
1741 | add_section_size(elfcpp::DT tag, const Output_data* od) |
1742 | { this->add_entry(Dynamic_entry(tag, od, true)); } | |
a3ad94ed ILT |
1743 | |
1744 | // Add a new dynamic entry with the address of a symbol. | |
1745 | void | |
16649710 | 1746 | add_symbol(elfcpp::DT tag, const Symbol* sym) |
a3ad94ed ILT |
1747 | { this->add_entry(Dynamic_entry(tag, sym)); } |
1748 | ||
1749 | // Add a new dynamic entry with a string. | |
1750 | void | |
1751 | add_string(elfcpp::DT tag, const char* str) | |
cfd73a4e | 1752 | { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); } |
a3ad94ed | 1753 | |
41f542e7 ILT |
1754 | void |
1755 | add_string(elfcpp::DT tag, const std::string& str) | |
1756 | { this->add_string(tag, str.c_str()); } | |
1757 | ||
27bc2bce ILT |
1758 | protected: |
1759 | // Adjust the output section to set the entry size. | |
1760 | void | |
1761 | do_adjust_output_section(Output_section*); | |
1762 | ||
a3ad94ed ILT |
1763 | // Set the final data size. |
1764 | void | |
27bc2bce | 1765 | set_final_data_size(); |
a3ad94ed ILT |
1766 | |
1767 | // Write out the dynamic entries. | |
1768 | void | |
1769 | do_write(Output_file*); | |
1770 | ||
7d9e3d98 ILT |
1771 | // Write to a map file. |
1772 | void | |
1773 | do_print_to_mapfile(Mapfile* mapfile) const | |
1774 | { mapfile->print_output_data(this, _("** dynamic")); } | |
1775 | ||
a3ad94ed ILT |
1776 | private: |
1777 | // This POD class holds a single dynamic entry. | |
1778 | class Dynamic_entry | |
1779 | { | |
1780 | public: | |
1781 | // Create an entry with a fixed numeric value. | |
1782 | Dynamic_entry(elfcpp::DT tag, unsigned int val) | |
c2b45e22 | 1783 | : tag_(tag), offset_(DYNAMIC_NUMBER) |
a3ad94ed ILT |
1784 | { this->u_.val = val; } |
1785 | ||
1786 | // Create an entry with the size or address of a section. | |
16649710 | 1787 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size) |
a3ad94ed | 1788 | : tag_(tag), |
c2b45e22 CC |
1789 | offset_(section_size |
1790 | ? DYNAMIC_SECTION_SIZE | |
1791 | : DYNAMIC_SECTION_ADDRESS) | |
1792 | { this->u_.od = od; } | |
1793 | ||
1794 | // Create an entry with the address of a section plus a constant offset. | |
1795 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset) | |
1796 | : tag_(tag), | |
1797 | offset_(offset) | |
16649710 | 1798 | { this->u_.od = od; } |
a3ad94ed ILT |
1799 | |
1800 | // Create an entry with the address of a symbol. | |
16649710 | 1801 | Dynamic_entry(elfcpp::DT tag, const Symbol* sym) |
c2b45e22 | 1802 | : tag_(tag), offset_(DYNAMIC_SYMBOL) |
a3ad94ed ILT |
1803 | { this->u_.sym = sym; } |
1804 | ||
1805 | // Create an entry with a string. | |
1806 | Dynamic_entry(elfcpp::DT tag, const char* str) | |
c2b45e22 | 1807 | : tag_(tag), offset_(DYNAMIC_STRING) |
a3ad94ed ILT |
1808 | { this->u_.str = str; } |
1809 | ||
1810 | // Write the dynamic entry to an output view. | |
1811 | template<int size, bool big_endian> | |
1812 | void | |
7d1a9ebb | 1813 | write(unsigned char* pov, const Stringpool*) const; |
a3ad94ed ILT |
1814 | |
1815 | private: | |
c2b45e22 | 1816 | // Classification is encoded in the OFFSET field. |
a3ad94ed ILT |
1817 | enum Classification |
1818 | { | |
a3ad94ed | 1819 | // Section address. |
c2b45e22 CC |
1820 | DYNAMIC_SECTION_ADDRESS = 0, |
1821 | // Number. | |
1822 | DYNAMIC_NUMBER = -1U, | |
a3ad94ed | 1823 | // Section size. |
c2b45e22 | 1824 | DYNAMIC_SECTION_SIZE = -2U, |
a3ad94ed | 1825 | // Symbol adress. |
c2b45e22 | 1826 | DYNAMIC_SYMBOL = -3U, |
a3ad94ed | 1827 | // String. |
c2b45e22 CC |
1828 | DYNAMIC_STRING = -4U |
1829 | // Any other value indicates a section address plus OFFSET. | |
a3ad94ed ILT |
1830 | }; |
1831 | ||
1832 | union | |
1833 | { | |
1834 | // For DYNAMIC_NUMBER. | |
1835 | unsigned int val; | |
c2b45e22 | 1836 | // For DYNAMIC_SECTION_SIZE and section address plus OFFSET. |
16649710 | 1837 | const Output_data* od; |
a3ad94ed | 1838 | // For DYNAMIC_SYMBOL. |
16649710 | 1839 | const Symbol* sym; |
a3ad94ed ILT |
1840 | // For DYNAMIC_STRING. |
1841 | const char* str; | |
1842 | } u_; | |
1843 | // The dynamic tag. | |
1844 | elfcpp::DT tag_; | |
c2b45e22 CC |
1845 | // The type of entry (Classification) or offset within a section. |
1846 | unsigned int offset_; | |
a3ad94ed ILT |
1847 | }; |
1848 | ||
1849 | // Add an entry to the list. | |
1850 | void | |
1851 | add_entry(const Dynamic_entry& entry) | |
1852 | { this->entries_.push_back(entry); } | |
1853 | ||
1854 | // Sized version of write function. | |
1855 | template<int size, bool big_endian> | |
1856 | void | |
1857 | sized_write(Output_file* of); | |
1858 | ||
1859 | // The type of the list of entries. | |
1860 | typedef std::vector<Dynamic_entry> Dynamic_entries; | |
1861 | ||
a3ad94ed ILT |
1862 | // The entries. |
1863 | Dynamic_entries entries_; | |
1864 | // The pool used for strings. | |
1865 | Stringpool* pool_; | |
1866 | }; | |
1867 | ||
d491d34e ILT |
1868 | // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections, |
1869 | // which may be required if the object file has more than | |
1870 | // SHN_LORESERVE sections. | |
1871 | ||
1872 | class Output_symtab_xindex : public Output_section_data | |
1873 | { | |
1874 | public: | |
1875 | Output_symtab_xindex(size_t symcount) | |
1876 | : Output_section_data(symcount * 4, 4), | |
1877 | entries_() | |
1878 | { } | |
1879 | ||
1880 | // Add an entry: symbol number SYMNDX has section SHNDX. | |
1881 | void | |
1882 | add(unsigned int symndx, unsigned int shndx) | |
1883 | { this->entries_.push_back(std::make_pair(symndx, shndx)); } | |
1884 | ||
1885 | protected: | |
1886 | void | |
1887 | do_write(Output_file*); | |
1888 | ||
7d9e3d98 ILT |
1889 | // Write to a map file. |
1890 | void | |
1891 | do_print_to_mapfile(Mapfile* mapfile) const | |
1892 | { mapfile->print_output_data(this, _("** symtab xindex")); } | |
1893 | ||
d491d34e ILT |
1894 | private: |
1895 | template<bool big_endian> | |
1896 | void | |
1897 | endian_do_write(unsigned char*); | |
1898 | ||
1899 | // It is likely that most symbols will not require entries. Rather | |
1900 | // than keep a vector for all symbols, we keep pairs of symbol index | |
1901 | // and section index. | |
1902 | typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries; | |
1903 | ||
1904 | // The entries we need. | |
1905 | Xindex_entries entries_; | |
1906 | }; | |
1907 | ||
a2fb1b05 ILT |
1908 | // An output section. We don't expect to have too many output |
1909 | // sections, so we don't bother to do a template on the size. | |
1910 | ||
54dc6425 | 1911 | class Output_section : public Output_data |
a2fb1b05 ILT |
1912 | { |
1913 | public: | |
1914 | // Create an output section, giving the name, type, and flags. | |
96803768 | 1915 | Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword); |
54dc6425 | 1916 | virtual ~Output_section(); |
a2fb1b05 | 1917 | |
ead1e424 | 1918 | // Add a new input section SHNDX, named NAME, with header SHDR, from |
730cdc88 | 1919 | // object OBJECT. RELOC_SHNDX is the index of a relocation section |
eff45813 | 1920 | // which applies to this section, or 0 if none, or -1 if more than |
a445fddf ILT |
1921 | // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause |
1922 | // in a linker script; in that case we need to keep track of input | |
1923 | // sections associated with an output section. Return the offset | |
1924 | // within the output section. | |
a2fb1b05 ILT |
1925 | template<int size, bool big_endian> |
1926 | off_t | |
730cdc88 ILT |
1927 | add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx, |
1928 | const char *name, | |
1929 | const elfcpp::Shdr<size, big_endian>& shdr, | |
a445fddf | 1930 | unsigned int reloc_shndx, bool have_sections_script); |
a2fb1b05 | 1931 | |
b8e6aad9 | 1932 | // Add generated data POSD to this output section. |
c06b7b0b | 1933 | void |
ead1e424 ILT |
1934 | add_output_section_data(Output_section_data* posd); |
1935 | ||
a2fb1b05 ILT |
1936 | // Return the section name. |
1937 | const char* | |
1938 | name() const | |
1939 | { return this->name_; } | |
1940 | ||
1941 | // Return the section type. | |
1942 | elfcpp::Elf_Word | |
1943 | type() const | |
1944 | { return this->type_; } | |
1945 | ||
1946 | // Return the section flags. | |
1947 | elfcpp::Elf_Xword | |
1948 | flags() const | |
1949 | { return this->flags_; } | |
1950 | ||
1650c4ff ILT |
1951 | // Set the section flags. This may only be used with the Layout |
1952 | // code when it is prepared to move the section to a different | |
1953 | // segment. | |
1954 | void | |
1955 | set_flags(elfcpp::Elf_Xword flags) | |
1956 | { this->flags_ = flags; } | |
1957 | ||
154e0e9a ILT |
1958 | // Update the output section flags based on input section flags. |
1959 | void | |
1960 | update_flags_for_input_section(elfcpp::Elf_Xword flags) | |
1961 | { | |
1962 | this->flags_ |= (flags | |
1963 | & (elfcpp::SHF_WRITE | |
1964 | | elfcpp::SHF_ALLOC | |
1965 | | elfcpp::SHF_EXECINSTR)); | |
1966 | } | |
1967 | ||
a3ad94ed ILT |
1968 | // Return the entsize field. |
1969 | uint64_t | |
1970 | entsize() const | |
1971 | { return this->entsize_; } | |
1972 | ||
61ba1cf9 ILT |
1973 | // Set the entsize field. |
1974 | void | |
16649710 | 1975 | set_entsize(uint64_t v); |
61ba1cf9 | 1976 | |
a445fddf ILT |
1977 | // Set the load address. |
1978 | void | |
1979 | set_load_address(uint64_t load_address) | |
1980 | { | |
1981 | this->load_address_ = load_address; | |
1982 | this->has_load_address_ = true; | |
1983 | } | |
1984 | ||
16649710 ILT |
1985 | // Set the link field to the output section index of a section. |
1986 | void | |
14b31740 | 1987 | set_link_section(const Output_data* od) |
16649710 ILT |
1988 | { |
1989 | gold_assert(this->link_ == 0 | |
1990 | && !this->should_link_to_symtab_ | |
1991 | && !this->should_link_to_dynsym_); | |
1992 | this->link_section_ = od; | |
1993 | } | |
1994 | ||
1995 | // Set the link field to a constant. | |
61ba1cf9 ILT |
1996 | void |
1997 | set_link(unsigned int v) | |
16649710 ILT |
1998 | { |
1999 | gold_assert(this->link_section_ == NULL | |
2000 | && !this->should_link_to_symtab_ | |
2001 | && !this->should_link_to_dynsym_); | |
2002 | this->link_ = v; | |
2003 | } | |
61ba1cf9 | 2004 | |
16649710 ILT |
2005 | // Record that this section should link to the normal symbol table. |
2006 | void | |
2007 | set_should_link_to_symtab() | |
2008 | { | |
2009 | gold_assert(this->link_section_ == NULL | |
2010 | && this->link_ == 0 | |
2011 | && !this->should_link_to_dynsym_); | |
2012 | this->should_link_to_symtab_ = true; | |
2013 | } | |
2014 | ||
2015 | // Record that this section should link to the dynamic symbol table. | |
2016 | void | |
2017 | set_should_link_to_dynsym() | |
2018 | { | |
2019 | gold_assert(this->link_section_ == NULL | |
2020 | && this->link_ == 0 | |
2021 | && !this->should_link_to_symtab_); | |
2022 | this->should_link_to_dynsym_ = true; | |
2023 | } | |
2024 | ||
2025 | // Return the info field. | |
2026 | unsigned int | |
2027 | info() const | |
2028 | { | |
755ab8af ILT |
2029 | gold_assert(this->info_section_ == NULL |
2030 | && this->info_symndx_ == NULL); | |
16649710 ILT |
2031 | return this->info_; |
2032 | } | |
2033 | ||
2034 | // Set the info field to the output section index of a section. | |
2035 | void | |
755ab8af | 2036 | set_info_section(const Output_section* os) |
16649710 | 2037 | { |
755ab8af ILT |
2038 | gold_assert((this->info_section_ == NULL |
2039 | || (this->info_section_ == os | |
2040 | && this->info_uses_section_index_)) | |
2041 | && this->info_symndx_ == NULL | |
2042 | && this->info_ == 0); | |
2043 | this->info_section_ = os; | |
2044 | this->info_uses_section_index_= true; | |
16649710 ILT |
2045 | } |
2046 | ||
6a74a719 ILT |
2047 | // Set the info field to the symbol table index of a symbol. |
2048 | void | |
2049 | set_info_symndx(const Symbol* sym) | |
2050 | { | |
755ab8af ILT |
2051 | gold_assert(this->info_section_ == NULL |
2052 | && (this->info_symndx_ == NULL | |
2053 | || this->info_symndx_ == sym) | |
2054 | && this->info_ == 0); | |
6a74a719 ILT |
2055 | this->info_symndx_ = sym; |
2056 | } | |
2057 | ||
755ab8af ILT |
2058 | // Set the info field to the symbol table index of a section symbol. |
2059 | void | |
2060 | set_info_section_symndx(const Output_section* os) | |
2061 | { | |
2062 | gold_assert((this->info_section_ == NULL | |
2063 | || (this->info_section_ == os | |
2064 | && !this->info_uses_section_index_)) | |
2065 | && this->info_symndx_ == NULL | |
2066 | && this->info_ == 0); | |
2067 | this->info_section_ = os; | |
2068 | this->info_uses_section_index_ = false; | |
2069 | } | |
2070 | ||
16649710 | 2071 | // Set the info field to a constant. |
61ba1cf9 ILT |
2072 | void |
2073 | set_info(unsigned int v) | |
16649710 | 2074 | { |
755ab8af ILT |
2075 | gold_assert(this->info_section_ == NULL |
2076 | && this->info_symndx_ == NULL | |
2077 | && (this->info_ == 0 | |
2078 | || this->info_ == v)); | |
16649710 ILT |
2079 | this->info_ = v; |
2080 | } | |
61ba1cf9 ILT |
2081 | |
2082 | // Set the addralign field. | |
2083 | void | |
2084 | set_addralign(uint64_t v) | |
2085 | { this->addralign_ = v; } | |
2086 | ||
d491d34e ILT |
2087 | // Whether the output section index has been set. |
2088 | bool | |
2089 | has_out_shndx() const | |
2090 | { return this->out_shndx_ != -1U; } | |
2091 | ||
c06b7b0b ILT |
2092 | // Indicate that we need a symtab index. |
2093 | void | |
2094 | set_needs_symtab_index() | |
2095 | { this->needs_symtab_index_ = true; } | |
2096 | ||
2097 | // Return whether we need a symtab index. | |
2098 | bool | |
2099 | needs_symtab_index() const | |
2100 | { return this->needs_symtab_index_; } | |
2101 | ||
2102 | // Get the symtab index. | |
2103 | unsigned int | |
2104 | symtab_index() const | |
2105 | { | |
a3ad94ed | 2106 | gold_assert(this->symtab_index_ != 0); |
c06b7b0b ILT |
2107 | return this->symtab_index_; |
2108 | } | |
2109 | ||
2110 | // Set the symtab index. | |
2111 | void | |
2112 | set_symtab_index(unsigned int index) | |
2113 | { | |
a3ad94ed | 2114 | gold_assert(index != 0); |
c06b7b0b ILT |
2115 | this->symtab_index_ = index; |
2116 | } | |
2117 | ||
2118 | // Indicate that we need a dynsym index. | |
2119 | void | |
2120 | set_needs_dynsym_index() | |
2121 | { this->needs_dynsym_index_ = true; } | |
2122 | ||
2123 | // Return whether we need a dynsym index. | |
2124 | bool | |
2125 | needs_dynsym_index() const | |
2126 | { return this->needs_dynsym_index_; } | |
2127 | ||
2128 | // Get the dynsym index. | |
2129 | unsigned int | |
2130 | dynsym_index() const | |
2131 | { | |
a3ad94ed | 2132 | gold_assert(this->dynsym_index_ != 0); |
c06b7b0b ILT |
2133 | return this->dynsym_index_; |
2134 | } | |
2135 | ||
2136 | // Set the dynsym index. | |
2137 | void | |
2138 | set_dynsym_index(unsigned int index) | |
2139 | { | |
a3ad94ed | 2140 | gold_assert(index != 0); |
c06b7b0b ILT |
2141 | this->dynsym_index_ = index; |
2142 | } | |
2143 | ||
2fd32231 ILT |
2144 | // Return whether the input sections sections attachd to this output |
2145 | // section may require sorting. This is used to handle constructor | |
2146 | // priorities compatibly with GNU ld. | |
2147 | bool | |
2148 | may_sort_attached_input_sections() const | |
2149 | { return this->may_sort_attached_input_sections_; } | |
2150 | ||
2151 | // Record that the input sections attached to this output section | |
2152 | // may require sorting. | |
2153 | void | |
2154 | set_may_sort_attached_input_sections() | |
2155 | { this->may_sort_attached_input_sections_ = true; } | |
2156 | ||
2157 | // Return whether the input sections attached to this output section | |
2158 | // require sorting. This is used to handle constructor priorities | |
2159 | // compatibly with GNU ld. | |
2160 | bool | |
2161 | must_sort_attached_input_sections() const | |
2162 | { return this->must_sort_attached_input_sections_; } | |
2163 | ||
2164 | // Record that the input sections attached to this output section | |
2165 | // require sorting. | |
2166 | void | |
2167 | set_must_sort_attached_input_sections() | |
2168 | { this->must_sort_attached_input_sections_ = true; } | |
2169 | ||
9f1d377b ILT |
2170 | // Return whether this section holds relro data--data which has |
2171 | // dynamic relocations but which may be marked read-only after the | |
2172 | // dynamic relocations have been completed. | |
2173 | bool | |
2174 | is_relro() const | |
2175 | { return this->is_relro_; } | |
2176 | ||
2177 | // Record that this section holds relro data. | |
2178 | void | |
2179 | set_is_relro() | |
2180 | { this->is_relro_ = true; } | |
2181 | ||
2d924fd9 ILT |
2182 | // Record that this section does not hold relro data. |
2183 | void | |
2184 | clear_is_relro() | |
2185 | { this->is_relro_ = false; } | |
2186 | ||
9f1d377b ILT |
2187 | // True if this section holds relro local data--relro data for which |
2188 | // the dynamic relocations are all RELATIVE relocations. | |
2189 | bool | |
2190 | is_relro_local() const | |
2191 | { return this->is_relro_local_; } | |
2192 | ||
2193 | // Record that this section holds relro local data. | |
2194 | void | |
2195 | set_is_relro_local() | |
2196 | { this->is_relro_local_ = true; } | |
2197 | ||
730cdc88 ILT |
2198 | // Return whether this section should be written after all the input |
2199 | // sections are complete. | |
2200 | bool | |
2201 | after_input_sections() const | |
2202 | { return this->after_input_sections_; } | |
2203 | ||
2204 | // Record that this section should be written after all the input | |
2205 | // sections are complete. | |
2206 | void | |
2207 | set_after_input_sections() | |
2208 | { this->after_input_sections_ = true; } | |
2209 | ||
27bc2bce ILT |
2210 | // Return whether this section requires postprocessing after all |
2211 | // relocations have been applied. | |
2212 | bool | |
2213 | requires_postprocessing() const | |
2214 | { return this->requires_postprocessing_; } | |
2215 | ||
96803768 ILT |
2216 | // If a section requires postprocessing, return the buffer to use. |
2217 | unsigned char* | |
2218 | postprocessing_buffer() const | |
2219 | { | |
2220 | gold_assert(this->postprocessing_buffer_ != NULL); | |
2221 | return this->postprocessing_buffer_; | |
2222 | } | |
2223 | ||
2224 | // If a section requires postprocessing, create the buffer to use. | |
27bc2bce | 2225 | void |
96803768 ILT |
2226 | create_postprocessing_buffer(); |
2227 | ||
2228 | // If a section requires postprocessing, this is the size of the | |
2229 | // buffer to which relocations should be applied. | |
2230 | off_t | |
2231 | postprocessing_buffer_size() const | |
2232 | { return this->current_data_size_for_child(); } | |
27bc2bce | 2233 | |
755ab8af ILT |
2234 | // Modify the section name. This is only permitted for an |
2235 | // unallocated section, and only before the size has been finalized. | |
2236 | // Otherwise the name will not get into Layout::namepool_. | |
2237 | void | |
2238 | set_name(const char* newname) | |
2239 | { | |
2240 | gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0); | |
2241 | gold_assert(!this->is_data_size_valid()); | |
2242 | this->name_ = newname; | |
2243 | } | |
2244 | ||
730cdc88 ILT |
2245 | // Return whether the offset OFFSET in the input section SHNDX in |
2246 | // object OBJECT is being included in the link. | |
2247 | bool | |
2248 | is_input_address_mapped(const Relobj* object, unsigned int shndx, | |
2249 | off_t offset) const; | |
2250 | ||
2251 | // Return the offset within the output section of OFFSET relative to | |
2252 | // the start of input section SHNDX in object OBJECT. | |
8383303e ILT |
2253 | section_offset_type |
2254 | output_offset(const Relobj* object, unsigned int shndx, | |
2255 | section_offset_type offset) const; | |
730cdc88 | 2256 | |
b8e6aad9 ILT |
2257 | // Return the output virtual address of OFFSET relative to the start |
2258 | // of input section SHNDX in object OBJECT. | |
2259 | uint64_t | |
2260 | output_address(const Relobj* object, unsigned int shndx, | |
2261 | off_t offset) const; | |
2262 | ||
e29e076a ILT |
2263 | // Look for the merged section for input section SHNDX in object |
2264 | // OBJECT. If found, return true, and set *ADDR to the address of | |
2265 | // the start of the merged section. This is not necessary the | |
2266 | // output offset corresponding to input offset 0 in the section, | |
2267 | // since the section may be mapped arbitrarily. | |
2268 | bool | |
2269 | find_starting_output_address(const Relobj* object, unsigned int shndx, | |
2270 | uint64_t* addr) const; | |
a9a60db6 | 2271 | |
a445fddf ILT |
2272 | // Record that this output section was found in the SECTIONS clause |
2273 | // of a linker script. | |
2274 | void | |
2275 | set_found_in_sections_clause() | |
2276 | { this->found_in_sections_clause_ = true; } | |
2277 | ||
2278 | // Return whether this output section was found in the SECTIONS | |
2279 | // clause of a linker script. | |
2280 | bool | |
2281 | found_in_sections_clause() const | |
2282 | { return this->found_in_sections_clause_; } | |
2283 | ||
27bc2bce ILT |
2284 | // Write the section header into *OPHDR. |
2285 | template<int size, bool big_endian> | |
2286 | void | |
2287 | write_header(const Layout*, const Stringpool*, | |
2288 | elfcpp::Shdr_write<size, big_endian>*) const; | |
2289 | ||
a445fddf ILT |
2290 | // The next few calls are for linker script support. |
2291 | ||
2292 | // Store the list of input sections for this Output_section into the | |
2293 | // list passed in. This removes the input sections, leaving only | |
2294 | // any Output_section_data elements. This returns the size of those | |
2295 | // Output_section_data elements. ADDRESS is the address of this | |
2296 | // output section. FILL is the fill value to use, in case there are | |
2297 | // any spaces between the remaining Output_section_data elements. | |
2298 | uint64_t | |
2299 | get_input_sections(uint64_t address, const std::string& fill, | |
2300 | std::list<std::pair<Relobj*, unsigned int > >*); | |
2301 | ||
2302 | // Add an input section from a script. | |
2303 | void | |
2304 | add_input_section_for_script(Relobj* object, unsigned int shndx, | |
2305 | off_t data_size, uint64_t addralign); | |
2306 | ||
2307 | // Set the current size of the output section. | |
2308 | void | |
2309 | set_current_data_size(off_t size) | |
2310 | { this->set_current_data_size_for_child(size); } | |
2311 | ||
2312 | // Get the current size of the output section. | |
2313 | off_t | |
2314 | current_data_size() const | |
2315 | { return this->current_data_size_for_child(); } | |
2316 | ||
2317 | // End of linker script support. | |
2318 | ||
38c5e8b4 ILT |
2319 | // Print merge statistics to stderr. |
2320 | void | |
2321 | print_merge_stats(); | |
2322 | ||
27bc2bce | 2323 | protected: |
77e65537 ILT |
2324 | // Return the output section--i.e., the object itself. |
2325 | Output_section* | |
2326 | do_output_section() | |
2327 | { return this; } | |
2328 | ||
27bc2bce ILT |
2329 | // Return the section index in the output file. |
2330 | unsigned int | |
2331 | do_out_shndx() const | |
2332 | { | |
2333 | gold_assert(this->out_shndx_ != -1U); | |
2334 | return this->out_shndx_; | |
2335 | } | |
2336 | ||
2337 | // Set the output section index. | |
2338 | void | |
2339 | do_set_out_shndx(unsigned int shndx) | |
2340 | { | |
a445fddf | 2341 | gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx); |
27bc2bce ILT |
2342 | this->out_shndx_ = shndx; |
2343 | } | |
2344 | ||
2345 | // Set the final data size of the Output_section. For a typical | |
ead1e424 | 2346 | // Output_section, there is nothing to do, but if there are any |
27bc2bce | 2347 | // Output_section_data objects we need to set their final addresses |
ead1e424 | 2348 | // here. |
96803768 | 2349 | virtual void |
27bc2bce | 2350 | set_final_data_size(); |
ead1e424 | 2351 | |
a445fddf ILT |
2352 | // Reset the address and file offset. |
2353 | void | |
2354 | do_reset_address_and_file_offset(); | |
2355 | ||
54dc6425 | 2356 | // Write the data to the file. For a typical Output_section, this |
ead1e424 ILT |
2357 | // does nothing: the data is written out by calling Object::Relocate |
2358 | // on each input object. But if there are any Output_section_data | |
2359 | // objects we do need to write them out here. | |
96803768 | 2360 | virtual void |
ead1e424 | 2361 | do_write(Output_file*); |
54dc6425 | 2362 | |
75f65a3e ILT |
2363 | // Return the address alignment--function required by parent class. |
2364 | uint64_t | |
2365 | do_addralign() const | |
2366 | { return this->addralign_; } | |
2367 | ||
a445fddf ILT |
2368 | // Return whether there is a load address. |
2369 | bool | |
2370 | do_has_load_address() const | |
2371 | { return this->has_load_address_; } | |
2372 | ||
2373 | // Return the load address. | |
2374 | uint64_t | |
2375 | do_load_address() const | |
2376 | { | |
2377 | gold_assert(this->has_load_address_); | |
2378 | return this->load_address_; | |
2379 | } | |
2380 | ||
75f65a3e ILT |
2381 | // Return whether this is an Output_section. |
2382 | bool | |
2383 | do_is_section() const | |
2384 | { return true; } | |
2385 | ||
54dc6425 ILT |
2386 | // Return whether this is a section of the specified type. |
2387 | bool | |
75f65a3e | 2388 | do_is_section_type(elfcpp::Elf_Word type) const |
54dc6425 ILT |
2389 | { return this->type_ == type; } |
2390 | ||
2391 | // Return whether the specified section flag is set. | |
2392 | bool | |
75f65a3e | 2393 | do_is_section_flag_set(elfcpp::Elf_Xword flag) const |
54dc6425 ILT |
2394 | { return (this->flags_ & flag) != 0; } |
2395 | ||
7bf1f802 ILT |
2396 | // Set the TLS offset. Called only for SHT_TLS sections. |
2397 | void | |
2398 | do_set_tls_offset(uint64_t tls_base); | |
2399 | ||
2400 | // Return the TLS offset, relative to the base of the TLS segment. | |
2401 | // Valid only for SHT_TLS sections. | |
2402 | uint64_t | |
2403 | do_tls_offset() const | |
2404 | { return this->tls_offset_; } | |
2405 | ||
96803768 ILT |
2406 | // This may be implemented by a child class. |
2407 | virtual void | |
2408 | do_finalize_name(Layout*) | |
2409 | { } | |
2410 | ||
7d9e3d98 ILT |
2411 | // Print to the map file. |
2412 | virtual void | |
2413 | do_print_to_mapfile(Mapfile*) const; | |
2414 | ||
96803768 ILT |
2415 | // Record that this section requires postprocessing after all |
2416 | // relocations have been applied. This is called by a child class. | |
2417 | void | |
2418 | set_requires_postprocessing() | |
2419 | { | |
2420 | this->requires_postprocessing_ = true; | |
2421 | this->after_input_sections_ = true; | |
2422 | } | |
2423 | ||
2424 | // Write all the data of an Output_section into the postprocessing | |
2425 | // buffer. | |
2426 | void | |
2427 | write_to_postprocessing_buffer(); | |
2428 | ||
a2fb1b05 | 2429 | private: |
ead1e424 ILT |
2430 | // In some cases we need to keep a list of the input sections |
2431 | // associated with this output section. We only need the list if we | |
2432 | // might have to change the offsets of the input section within the | |
2433 | // output section after we add the input section. The ordinary | |
2434 | // input sections will be written out when we process the object | |
2435 | // file, and as such we don't need to track them here. We do need | |
2436 | // to track Output_section_data objects here. We store instances of | |
2437 | // this structure in a std::vector, so it must be a POD. There can | |
2438 | // be many instances of this structure, so we use a union to save | |
2439 | // some space. | |
2440 | class Input_section | |
2441 | { | |
2442 | public: | |
2443 | Input_section() | |
b8e6aad9 ILT |
2444 | : shndx_(0), p2align_(0) |
2445 | { | |
2446 | this->u1_.data_size = 0; | |
2447 | this->u2_.object = NULL; | |
2448 | } | |
ead1e424 | 2449 | |
b8e6aad9 | 2450 | // For an ordinary input section. |
f6ce93d6 | 2451 | Input_section(Relobj* object, unsigned int shndx, off_t data_size, |
ead1e424 ILT |
2452 | uint64_t addralign) |
2453 | : shndx_(shndx), | |
b8e6aad9 | 2454 | p2align_(ffsll(static_cast<long long>(addralign))) |
ead1e424 | 2455 | { |
b8e6aad9 ILT |
2456 | gold_assert(shndx != OUTPUT_SECTION_CODE |
2457 | && shndx != MERGE_DATA_SECTION_CODE | |
2458 | && shndx != MERGE_STRING_SECTION_CODE); | |
2459 | this->u1_.data_size = data_size; | |
2460 | this->u2_.object = object; | |
ead1e424 ILT |
2461 | } |
2462 | ||
b8e6aad9 | 2463 | // For a non-merge output section. |
ead1e424 | 2464 | Input_section(Output_section_data* posd) |
f34787f8 | 2465 | : shndx_(OUTPUT_SECTION_CODE), p2align_(0) |
b8e6aad9 ILT |
2466 | { |
2467 | this->u1_.data_size = 0; | |
2468 | this->u2_.posd = posd; | |
2469 | } | |
2470 | ||
2471 | // For a merge section. | |
2472 | Input_section(Output_section_data* posd, bool is_string, uint64_t entsize) | |
2473 | : shndx_(is_string | |
2474 | ? MERGE_STRING_SECTION_CODE | |
2475 | : MERGE_DATA_SECTION_CODE), | |
f34787f8 | 2476 | p2align_(0) |
b8e6aad9 ILT |
2477 | { |
2478 | this->u1_.entsize = entsize; | |
2479 | this->u2_.posd = posd; | |
2480 | } | |
ead1e424 ILT |
2481 | |
2482 | // The required alignment. | |
2483 | uint64_t | |
2484 | addralign() const | |
a3ad94ed | 2485 | { |
f34787f8 ILT |
2486 | if (!this->is_input_section()) |
2487 | return this->u2_.posd->addralign(); | |
a3ad94ed ILT |
2488 | return (this->p2align_ == 0 |
2489 | ? 0 | |
2490 | : static_cast<uint64_t>(1) << (this->p2align_ - 1)); | |
2491 | } | |
ead1e424 ILT |
2492 | |
2493 | // Return the required size. | |
2494 | off_t | |
2495 | data_size() const; | |
2496 | ||
a445fddf ILT |
2497 | // Whether this is an input section. |
2498 | bool | |
2499 | is_input_section() const | |
2500 | { | |
2501 | return (this->shndx_ != OUTPUT_SECTION_CODE | |
2502 | && this->shndx_ != MERGE_DATA_SECTION_CODE | |
2503 | && this->shndx_ != MERGE_STRING_SECTION_CODE); | |
2504 | } | |
2505 | ||
b8e6aad9 ILT |
2506 | // Return whether this is a merge section which matches the |
2507 | // parameters. | |
2508 | bool | |
87f95776 ILT |
2509 | is_merge_section(bool is_string, uint64_t entsize, |
2510 | uint64_t addralign) const | |
b8e6aad9 ILT |
2511 | { |
2512 | return (this->shndx_ == (is_string | |
2513 | ? MERGE_STRING_SECTION_CODE | |
2514 | : MERGE_DATA_SECTION_CODE) | |
87f95776 ILT |
2515 | && this->u1_.entsize == entsize |
2516 | && this->addralign() == addralign); | |
b8e6aad9 ILT |
2517 | } |
2518 | ||
a445fddf ILT |
2519 | // Return the object for an input section. |
2520 | Relobj* | |
2521 | relobj() const | |
2522 | { | |
2523 | gold_assert(this->is_input_section()); | |
2524 | return this->u2_.object; | |
2525 | } | |
2526 | ||
2527 | // Return the input section index for an input section. | |
2528 | unsigned int | |
2529 | shndx() const | |
2530 | { | |
2531 | gold_assert(this->is_input_section()); | |
2532 | return this->shndx_; | |
2533 | } | |
2534 | ||
b8e6aad9 ILT |
2535 | // Set the output section. |
2536 | void | |
2537 | set_output_section(Output_section* os) | |
2538 | { | |
2539 | gold_assert(!this->is_input_section()); | |
2540 | this->u2_.posd->set_output_section(os); | |
2541 | } | |
2542 | ||
ead1e424 | 2543 | // Set the address and file offset. This is called during |
96803768 ILT |
2544 | // Layout::finalize. SECTION_FILE_OFFSET is the file offset of |
2545 | // the enclosing section. | |
ead1e424 | 2546 | void |
96803768 ILT |
2547 | set_address_and_file_offset(uint64_t address, off_t file_offset, |
2548 | off_t section_file_offset); | |
ead1e424 | 2549 | |
a445fddf ILT |
2550 | // Reset the address and file offset. |
2551 | void | |
2552 | reset_address_and_file_offset(); | |
2553 | ||
96803768 ILT |
2554 | // Finalize the data size. |
2555 | void | |
2556 | finalize_data_size(); | |
9a0910c3 | 2557 | |
b8e6aad9 ILT |
2558 | // Add an input section, for SHF_MERGE sections. |
2559 | bool | |
2560 | add_input_section(Relobj* object, unsigned int shndx) | |
2561 | { | |
2562 | gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE | |
2563 | || this->shndx_ == MERGE_STRING_SECTION_CODE); | |
2564 | return this->u2_.posd->add_input_section(object, shndx); | |
2565 | } | |
2566 | ||
2567 | // Given an input OBJECT, an input section index SHNDX within that | |
2568 | // object, and an OFFSET relative to the start of that input | |
730cdc88 | 2569 | // section, return whether or not the output offset is known. If |
1e983657 ILT |
2570 | // this function returns true, it sets *POUTPUT to the offset in |
2571 | // the output section, relative to the start of the input section | |
2572 | // in the output section. *POUTPUT may be different from OFFSET | |
2573 | // for a merged section. | |
b8e6aad9 | 2574 | bool |
8383303e ILT |
2575 | output_offset(const Relobj* object, unsigned int shndx, |
2576 | section_offset_type offset, | |
2577 | section_offset_type *poutput) const; | |
b8e6aad9 | 2578 | |
a9a60db6 ILT |
2579 | // Return whether this is the merge section for the input section |
2580 | // SHNDX in OBJECT. | |
2581 | bool | |
2582 | is_merge_section_for(const Relobj* object, unsigned int shndx) const; | |
2583 | ||
ead1e424 ILT |
2584 | // Write out the data. This does nothing for an input section. |
2585 | void | |
2586 | write(Output_file*); | |
2587 | ||
96803768 ILT |
2588 | // Write the data to a buffer. This does nothing for an input |
2589 | // section. | |
2590 | void | |
2591 | write_to_buffer(unsigned char*); | |
2592 | ||
7d9e3d98 ILT |
2593 | // Print to a map file. |
2594 | void | |
2595 | print_to_mapfile(Mapfile*) const; | |
2596 | ||
38c5e8b4 ILT |
2597 | // Print statistics about merge sections to stderr. |
2598 | void | |
2599 | print_merge_stats(const char* section_name) | |
2600 | { | |
2601 | if (this->shndx_ == MERGE_DATA_SECTION_CODE | |
2602 | || this->shndx_ == MERGE_STRING_SECTION_CODE) | |
2603 | this->u2_.posd->print_merge_stats(section_name); | |
2604 | } | |
2605 | ||
ead1e424 | 2606 | private: |
b8e6aad9 ILT |
2607 | // Code values which appear in shndx_. If the value is not one of |
2608 | // these codes, it is the input section index in the object file. | |
2609 | enum | |
2610 | { | |
2611 | // An Output_section_data. | |
2612 | OUTPUT_SECTION_CODE = -1U, | |
2613 | // An Output_section_data for an SHF_MERGE section with | |
2614 | // SHF_STRINGS not set. | |
2615 | MERGE_DATA_SECTION_CODE = -2U, | |
2616 | // An Output_section_data for an SHF_MERGE section with | |
2617 | // SHF_STRINGS set. | |
2618 | MERGE_STRING_SECTION_CODE = -3U | |
2619 | }; | |
2620 | ||
b8e6aad9 ILT |
2621 | // For an ordinary input section, this is the section index in the |
2622 | // input file. For an Output_section_data, this is | |
2623 | // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or | |
2624 | // MERGE_STRING_SECTION_CODE. | |
ead1e424 ILT |
2625 | unsigned int shndx_; |
2626 | // The required alignment, stored as a power of 2. | |
2627 | unsigned int p2align_; | |
ead1e424 ILT |
2628 | union |
2629 | { | |
b8e6aad9 ILT |
2630 | // For an ordinary input section, the section size. |
2631 | off_t data_size; | |
2632 | // For OUTPUT_SECTION_CODE, this is not used. For | |
2633 | // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the | |
2634 | // entity size. | |
2635 | uint64_t entsize; | |
2636 | } u1_; | |
2637 | union | |
2638 | { | |
2639 | // For an ordinary input section, the object which holds the | |
ead1e424 | 2640 | // input section. |
f6ce93d6 | 2641 | Relobj* object; |
b8e6aad9 ILT |
2642 | // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or |
2643 | // MERGE_STRING_SECTION_CODE, the data. | |
ead1e424 | 2644 | Output_section_data* posd; |
b8e6aad9 | 2645 | } u2_; |
ead1e424 ILT |
2646 | }; |
2647 | ||
2648 | typedef std::vector<Input_section> Input_section_list; | |
2649 | ||
2fd32231 ILT |
2650 | // This class is used to sort the input sections. |
2651 | class Input_section_sort_entry; | |
2652 | ||
2653 | // This is the sort comparison function. | |
2654 | struct Input_section_sort_compare | |
2655 | { | |
2656 | bool | |
2657 | operator()(const Input_section_sort_entry&, | |
2658 | const Input_section_sort_entry&) const; | |
2659 | }; | |
2660 | ||
c51e6221 | 2661 | // Fill data. This is used to fill in data between input sections. |
a445fddf ILT |
2662 | // It is also used for data statements (BYTE, WORD, etc.) in linker |
2663 | // scripts. When we have to keep track of the input sections, we | |
2664 | // can use an Output_data_const, but we don't want to have to keep | |
2665 | // track of input sections just to implement fills. | |
c51e6221 ILT |
2666 | class Fill |
2667 | { | |
2668 | public: | |
2669 | Fill(off_t section_offset, off_t length) | |
a445fddf ILT |
2670 | : section_offset_(section_offset), |
2671 | length_(convert_to_section_size_type(length)) | |
c51e6221 ILT |
2672 | { } |
2673 | ||
2674 | // Return section offset. | |
2675 | off_t | |
2676 | section_offset() const | |
2677 | { return this->section_offset_; } | |
2678 | ||
2679 | // Return fill length. | |
a445fddf | 2680 | section_size_type |
c51e6221 ILT |
2681 | length() const |
2682 | { return this->length_; } | |
2683 | ||
2684 | private: | |
2685 | // The offset within the output section. | |
2686 | off_t section_offset_; | |
2687 | // The length of the space to fill. | |
a445fddf | 2688 | section_size_type length_; |
c51e6221 ILT |
2689 | }; |
2690 | ||
2691 | typedef std::vector<Fill> Fill_list; | |
2692 | ||
b8e6aad9 ILT |
2693 | // Add a new output section by Input_section. |
2694 | void | |
2695 | add_output_section_data(Input_section*); | |
2696 | ||
2697 | // Add an SHF_MERGE input section. Returns true if the section was | |
2698 | // handled. | |
2699 | bool | |
2700 | add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags, | |
96803768 | 2701 | uint64_t entsize, uint64_t addralign); |
b8e6aad9 ILT |
2702 | |
2703 | // Add an output SHF_MERGE section POSD to this output section. | |
2704 | // IS_STRING indicates whether it is a SHF_STRINGS section, and | |
2705 | // ENTSIZE is the entity size. This returns the entry added to | |
2706 | // input_sections_. | |
2707 | void | |
2708 | add_output_merge_section(Output_section_data* posd, bool is_string, | |
2709 | uint64_t entsize); | |
2710 | ||
2fd32231 ILT |
2711 | // Sort the attached input sections. |
2712 | void | |
2713 | sort_attached_input_sections(); | |
2714 | ||
a2fb1b05 ILT |
2715 | // Most of these fields are only valid after layout. |
2716 | ||
2717 | // The name of the section. This will point into a Stringpool. | |
9a0910c3 | 2718 | const char* name_; |
75f65a3e | 2719 | // The section address is in the parent class. |
a2fb1b05 ILT |
2720 | // The section alignment. |
2721 | uint64_t addralign_; | |
2722 | // The section entry size. | |
2723 | uint64_t entsize_; | |
a445fddf ILT |
2724 | // The load address. This is only used when using a linker script |
2725 | // with a SECTIONS clause. The has_load_address_ field indicates | |
2726 | // whether this field is valid. | |
2727 | uint64_t load_address_; | |
75f65a3e | 2728 | // The file offset is in the parent class. |
16649710 | 2729 | // Set the section link field to the index of this section. |
14b31740 | 2730 | const Output_data* link_section_; |
16649710 | 2731 | // If link_section_ is NULL, this is the link field. |
a2fb1b05 | 2732 | unsigned int link_; |
16649710 | 2733 | // Set the section info field to the index of this section. |
755ab8af | 2734 | const Output_section* info_section_; |
6a74a719 ILT |
2735 | // If info_section_ is NULL, set the info field to the symbol table |
2736 | // index of this symbol. | |
2737 | const Symbol* info_symndx_; | |
2738 | // If info_section_ and info_symndx_ are NULL, this is the section | |
2739 | // info field. | |
a2fb1b05 ILT |
2740 | unsigned int info_; |
2741 | // The section type. | |
27bc2bce | 2742 | const elfcpp::Elf_Word type_; |
a2fb1b05 | 2743 | // The section flags. |
a445fddf | 2744 | elfcpp::Elf_Xword flags_; |
61ba1cf9 | 2745 | // The section index. |
ead1e424 | 2746 | unsigned int out_shndx_; |
c06b7b0b ILT |
2747 | // If there is a STT_SECTION for this output section in the normal |
2748 | // symbol table, this is the symbol index. This starts out as zero. | |
2749 | // It is initialized in Layout::finalize() to be the index, or -1U | |
2750 | // if there isn't one. | |
2751 | unsigned int symtab_index_; | |
2752 | // If there is a STT_SECTION for this output section in the dynamic | |
2753 | // symbol table, this is the symbol index. This starts out as zero. | |
2754 | // It is initialized in Layout::finalize() to be the index, or -1U | |
2755 | // if there isn't one. | |
2756 | unsigned int dynsym_index_; | |
ead1e424 ILT |
2757 | // The input sections. This will be empty in cases where we don't |
2758 | // need to keep track of them. | |
2759 | Input_section_list input_sections_; | |
2760 | // The offset of the first entry in input_sections_. | |
2761 | off_t first_input_offset_; | |
c51e6221 ILT |
2762 | // The fill data. This is separate from input_sections_ because we |
2763 | // often will need fill sections without needing to keep track of | |
2764 | // input sections. | |
2765 | Fill_list fills_; | |
96803768 ILT |
2766 | // If the section requires postprocessing, this buffer holds the |
2767 | // section contents during relocation. | |
2768 | unsigned char* postprocessing_buffer_; | |
c06b7b0b ILT |
2769 | // Whether this output section needs a STT_SECTION symbol in the |
2770 | // normal symbol table. This will be true if there is a relocation | |
2771 | // which needs it. | |
2772 | bool needs_symtab_index_ : 1; | |
2773 | // Whether this output section needs a STT_SECTION symbol in the | |
2774 | // dynamic symbol table. This will be true if there is a dynamic | |
2775 | // relocation which needs it. | |
2776 | bool needs_dynsym_index_ : 1; | |
16649710 ILT |
2777 | // Whether the link field of this output section should point to the |
2778 | // normal symbol table. | |
2779 | bool should_link_to_symtab_ : 1; | |
2780 | // Whether the link field of this output section should point to the | |
2781 | // dynamic symbol table. | |
2782 | bool should_link_to_dynsym_ : 1; | |
730cdc88 ILT |
2783 | // Whether this section should be written after all the input |
2784 | // sections are complete. | |
2785 | bool after_input_sections_ : 1; | |
27bc2bce ILT |
2786 | // Whether this section requires post processing after all |
2787 | // relocations have been applied. | |
2788 | bool requires_postprocessing_ : 1; | |
a445fddf ILT |
2789 | // Whether an input section was mapped to this output section |
2790 | // because of a SECTIONS clause in a linker script. | |
2791 | bool found_in_sections_clause_ : 1; | |
2792 | // Whether this section has an explicitly specified load address. | |
2793 | bool has_load_address_ : 1; | |
755ab8af ILT |
2794 | // True if the info_section_ field means the section index of the |
2795 | // section, false if it means the symbol index of the corresponding | |
2796 | // section symbol. | |
2797 | bool info_uses_section_index_ : 1; | |
2fd32231 ILT |
2798 | // True if the input sections attached to this output section may |
2799 | // need sorting. | |
2800 | bool may_sort_attached_input_sections_ : 1; | |
2801 | // True if the input sections attached to this output section must | |
2802 | // be sorted. | |
2803 | bool must_sort_attached_input_sections_ : 1; | |
2804 | // True if the input sections attached to this output section have | |
2805 | // already been sorted. | |
2806 | bool attached_input_sections_are_sorted_ : 1; | |
9f1d377b ILT |
2807 | // True if this section holds relro data. |
2808 | bool is_relro_ : 1; | |
2809 | // True if this section holds relro local data. | |
2810 | bool is_relro_local_ : 1; | |
7bf1f802 ILT |
2811 | // For SHT_TLS sections, the offset of this section relative to the base |
2812 | // of the TLS segment. | |
2813 | uint64_t tls_offset_; | |
a2fb1b05 ILT |
2814 | }; |
2815 | ||
2816 | // An output segment. PT_LOAD segments are built from collections of | |
2817 | // output sections. Other segments typically point within PT_LOAD | |
2818 | // segments, and are built directly as needed. | |
2819 | ||
2820 | class Output_segment | |
2821 | { | |
2822 | public: | |
2823 | // Create an output segment, specifying the type and flags. | |
2824 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); | |
2825 | ||
2826 | // Return the virtual address. | |
2827 | uint64_t | |
2828 | vaddr() const | |
2829 | { return this->vaddr_; } | |
2830 | ||
2831 | // Return the physical address. | |
2832 | uint64_t | |
2833 | paddr() const | |
2834 | { return this->paddr_; } | |
2835 | ||
2836 | // Return the segment type. | |
2837 | elfcpp::Elf_Word | |
2838 | type() const | |
2839 | { return this->type_; } | |
2840 | ||
2841 | // Return the segment flags. | |
2842 | elfcpp::Elf_Word | |
2843 | flags() const | |
2844 | { return this->flags_; } | |
2845 | ||
92e059d8 ILT |
2846 | // Return the memory size. |
2847 | uint64_t | |
2848 | memsz() const | |
2849 | { return this->memsz_; } | |
2850 | ||
ead1e424 ILT |
2851 | // Return the file size. |
2852 | off_t | |
2853 | filesz() const | |
2854 | { return this->filesz_; } | |
2855 | ||
516cb3d0 ILT |
2856 | // Return the file offset. |
2857 | off_t | |
2858 | offset() const | |
2859 | { return this->offset_; } | |
2860 | ||
75f65a3e ILT |
2861 | // Return the maximum alignment of the Output_data. |
2862 | uint64_t | |
a445fddf | 2863 | maximum_alignment(); |
75f65a3e | 2864 | |
a2fb1b05 ILT |
2865 | // Add an Output_section to this segment. |
2866 | void | |
01676dcd | 2867 | add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags); |
75f65a3e | 2868 | |
1650c4ff ILT |
2869 | // Remove an Output_section from this segment. It is an error if it |
2870 | // is not present. | |
2871 | void | |
2872 | remove_output_section(Output_section* os); | |
2873 | ||
75f65a3e ILT |
2874 | // Add an Output_data (which is not an Output_section) to the start |
2875 | // of this segment. | |
2876 | void | |
2877 | add_initial_output_data(Output_data*); | |
2878 | ||
756ac4a8 ILT |
2879 | // Return true if this segment has any sections which hold actual |
2880 | // data, rather than being a BSS section. | |
2881 | bool | |
2882 | has_any_data_sections() const | |
2883 | { return !this->output_data_.empty(); } | |
2884 | ||
4f4c5f80 ILT |
2885 | // Return the number of dynamic relocations applied to this segment. |
2886 | unsigned int | |
2887 | dynamic_reloc_count() const; | |
2888 | ||
a445fddf ILT |
2889 | // Return the address of the first section. |
2890 | uint64_t | |
2891 | first_section_load_address() const; | |
2892 | ||
2893 | // Return whether the addresses have been set already. | |
2894 | bool | |
2895 | are_addresses_set() const | |
2896 | { return this->are_addresses_set_; } | |
2897 | ||
2898 | // Set the addresses. | |
2899 | void | |
2900 | set_addresses(uint64_t vaddr, uint64_t paddr) | |
2901 | { | |
2902 | this->vaddr_ = vaddr; | |
2903 | this->paddr_ = paddr; | |
2904 | this->are_addresses_set_ = true; | |
2905 | } | |
2906 | ||
1c4f3631 ILT |
2907 | // Set the segment flags. This is only used if we have a PHDRS |
2908 | // clause which explicitly specifies the flags. | |
2909 | void | |
2910 | set_flags(elfcpp::Elf_Word flags) | |
2911 | { this->flags_ = flags; } | |
2912 | ||
75f65a3e | 2913 | // Set the address of the segment to ADDR and the offset to *POFF |
a445fddf ILT |
2914 | // and set the addresses and offsets of all contained output |
2915 | // sections accordingly. Set the section indexes of all contained | |
2916 | // output sections starting with *PSHNDX. If RESET is true, first | |
2917 | // reset the addresses of the contained sections. Return the | |
2918 | // address of the immediately following segment. Update *POFF and | |
2919 | // *PSHNDX. This should only be called for a PT_LOAD segment. | |
75f65a3e | 2920 | uint64_t |
96a2b4e4 | 2921 | set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff, |
a445fddf | 2922 | unsigned int* pshndx); |
75f65a3e | 2923 | |
0496d5e5 ILT |
2924 | // Set the minimum alignment of this segment. This may be adjusted |
2925 | // upward based on the section alignments. | |
2926 | void | |
a445fddf ILT |
2927 | set_minimum_p_align(uint64_t align) |
2928 | { this->min_p_align_ = align; } | |
0496d5e5 | 2929 | |
75f65a3e ILT |
2930 | // Set the offset of this segment based on the section. This should |
2931 | // only be called for a non-PT_LOAD segment. | |
2932 | void | |
2933 | set_offset(); | |
2934 | ||
7bf1f802 ILT |
2935 | // Set the TLS offsets of the sections contained in the PT_TLS segment. |
2936 | void | |
2937 | set_tls_offsets(); | |
2938 | ||
75f65a3e ILT |
2939 | // Return the number of output sections. |
2940 | unsigned int | |
2941 | output_section_count() const; | |
a2fb1b05 | 2942 | |
1c4f3631 ILT |
2943 | // Return the section attached to the list segment with the lowest |
2944 | // load address. This is used when handling a PHDRS clause in a | |
2945 | // linker script. | |
2946 | Output_section* | |
2947 | section_with_lowest_load_address() const; | |
2948 | ||
61ba1cf9 ILT |
2949 | // Write the segment header into *OPHDR. |
2950 | template<int size, bool big_endian> | |
2951 | void | |
ead1e424 | 2952 | write_header(elfcpp::Phdr_write<size, big_endian>*); |
61ba1cf9 ILT |
2953 | |
2954 | // Write the section headers of associated sections into V. | |
2955 | template<int size, bool big_endian> | |
2956 | unsigned char* | |
16649710 | 2957 | write_section_headers(const Layout*, const Stringpool*, unsigned char* v, |
7d1a9ebb | 2958 | unsigned int* pshndx) const; |
61ba1cf9 | 2959 | |
7d9e3d98 ILT |
2960 | // Print the output sections in the map file. |
2961 | void | |
2962 | print_sections_to_mapfile(Mapfile*) const; | |
2963 | ||
a2fb1b05 ILT |
2964 | private: |
2965 | Output_segment(const Output_segment&); | |
2966 | Output_segment& operator=(const Output_segment&); | |
2967 | ||
54dc6425 | 2968 | typedef std::list<Output_data*> Output_data_list; |
a2fb1b05 | 2969 | |
ead1e424 ILT |
2970 | // Find the maximum alignment in an Output_data_list. |
2971 | static uint64_t | |
a445fddf | 2972 | maximum_alignment_list(const Output_data_list*); |
ead1e424 | 2973 | |
9f1d377b ILT |
2974 | // Return whether the first data section is a relro section. |
2975 | bool | |
2976 | is_first_section_relro() const; | |
2977 | ||
75f65a3e ILT |
2978 | // Set the section addresses in an Output_data_list. |
2979 | uint64_t | |
96a2b4e4 ILT |
2980 | set_section_list_addresses(const Layout*, bool reset, Output_data_list*, |
2981 | uint64_t addr, off_t* poff, unsigned int* pshndx, | |
9f1d377b | 2982 | bool* in_tls, bool* in_relro); |
75f65a3e ILT |
2983 | |
2984 | // Return the number of Output_sections in an Output_data_list. | |
2985 | unsigned int | |
2986 | output_section_count_list(const Output_data_list*) const; | |
2987 | ||
4f4c5f80 ILT |
2988 | // Return the number of dynamic relocs in an Output_data_list. |
2989 | unsigned int | |
2990 | dynamic_reloc_count_list(const Output_data_list*) const; | |
2991 | ||
1c4f3631 ILT |
2992 | // Find the section with the lowest load address in an |
2993 | // Output_data_list. | |
2994 | void | |
2995 | lowest_load_address_in_list(const Output_data_list* pdl, | |
2996 | Output_section** found, | |
2997 | uint64_t* found_lma) const; | |
2998 | ||
61ba1cf9 ILT |
2999 | // Write the section headers in the list into V. |
3000 | template<int size, bool big_endian> | |
3001 | unsigned char* | |
16649710 ILT |
3002 | write_section_headers_list(const Layout*, const Stringpool*, |
3003 | const Output_data_list*, unsigned char* v, | |
7d1a9ebb | 3004 | unsigned int* pshdx) const; |
61ba1cf9 | 3005 | |
7d9e3d98 ILT |
3006 | // Print a section list to the mapfile. |
3007 | void | |
3008 | print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const; | |
3009 | ||
75f65a3e | 3010 | // The list of output data with contents attached to this segment. |
54dc6425 | 3011 | Output_data_list output_data_; |
75f65a3e ILT |
3012 | // The list of output data without contents attached to this segment. |
3013 | Output_data_list output_bss_; | |
a2fb1b05 ILT |
3014 | // The segment virtual address. |
3015 | uint64_t vaddr_; | |
3016 | // The segment physical address. | |
3017 | uint64_t paddr_; | |
3018 | // The size of the segment in memory. | |
3019 | uint64_t memsz_; | |
a445fddf ILT |
3020 | // The maximum section alignment. The is_max_align_known_ field |
3021 | // indicates whether this has been finalized. | |
3022 | uint64_t max_align_; | |
3023 | // The required minimum value for the p_align field. This is used | |
3024 | // for PT_LOAD segments. Note that this does not mean that | |
3025 | // addresses should be aligned to this value; it means the p_paddr | |
3026 | // and p_vaddr fields must be congruent modulo this value. For | |
3027 | // non-PT_LOAD segments, the dynamic linker works more efficiently | |
3028 | // if the p_align field has the more conventional value, although it | |
3029 | // can align as needed. | |
3030 | uint64_t min_p_align_; | |
a2fb1b05 ILT |
3031 | // The offset of the segment data within the file. |
3032 | off_t offset_; | |
3033 | // The size of the segment data in the file. | |
3034 | off_t filesz_; | |
3035 | // The segment type; | |
3036 | elfcpp::Elf_Word type_; | |
3037 | // The segment flags. | |
3038 | elfcpp::Elf_Word flags_; | |
a445fddf ILT |
3039 | // Whether we have finalized max_align_. |
3040 | bool is_max_align_known_ : 1; | |
3041 | // Whether vaddr and paddr were set by a linker script. | |
3042 | bool are_addresses_set_ : 1; | |
a2fb1b05 ILT |
3043 | }; |
3044 | ||
61ba1cf9 | 3045 | // This class represents the output file. |
a2fb1b05 ILT |
3046 | |
3047 | class Output_file | |
3048 | { | |
3049 | public: | |
14144f39 | 3050 | Output_file(const char* name); |
61ba1cf9 | 3051 | |
516cb3d0 ILT |
3052 | // Indicate that this is a temporary file which should not be |
3053 | // output. | |
3054 | void | |
3055 | set_is_temporary() | |
3056 | { this->is_temporary_ = true; } | |
3057 | ||
61ba1cf9 ILT |
3058 | // Open the output file. FILE_SIZE is the final size of the file. |
3059 | void | |
3060 | open(off_t file_size); | |
3061 | ||
27bc2bce ILT |
3062 | // Resize the output file. |
3063 | void | |
3064 | resize(off_t file_size); | |
3065 | ||
c420411f ILT |
3066 | // Close the output file (flushing all buffered data) and make sure |
3067 | // there are no errors. | |
61ba1cf9 ILT |
3068 | void |
3069 | close(); | |
3070 | ||
3071 | // We currently always use mmap which makes the view handling quite | |
3072 | // simple. In the future we may support other approaches. | |
a2fb1b05 ILT |
3073 | |
3074 | // Write data to the output file. | |
3075 | void | |
fe8718a4 | 3076 | write(off_t offset, const void* data, size_t len) |
61ba1cf9 ILT |
3077 | { memcpy(this->base_ + offset, data, len); } |
3078 | ||
3079 | // Get a buffer to use to write to the file, given the offset into | |
3080 | // the file and the size. | |
3081 | unsigned char* | |
fe8718a4 | 3082 | get_output_view(off_t start, size_t size) |
61ba1cf9 | 3083 | { |
8d32f935 ILT |
3084 | gold_assert(start >= 0 |
3085 | && start + static_cast<off_t>(size) <= this->file_size_); | |
61ba1cf9 ILT |
3086 | return this->base_ + start; |
3087 | } | |
3088 | ||
3089 | // VIEW must have been returned by get_output_view. Write the | |
3090 | // buffer to the file, passing in the offset and the size. | |
3091 | void | |
fe8718a4 | 3092 | write_output_view(off_t, size_t, unsigned char*) |
61ba1cf9 ILT |
3093 | { } |
3094 | ||
730cdc88 ILT |
3095 | // Get a read/write buffer. This is used when we want to write part |
3096 | // of the file, read it in, and write it again. | |
3097 | unsigned char* | |
fe8718a4 | 3098 | get_input_output_view(off_t start, size_t size) |
730cdc88 ILT |
3099 | { return this->get_output_view(start, size); } |
3100 | ||
3101 | // Write a read/write buffer back to the file. | |
3102 | void | |
fe8718a4 | 3103 | write_input_output_view(off_t, size_t, unsigned char*) |
730cdc88 ILT |
3104 | { } |
3105 | ||
3106 | // Get a read buffer. This is used when we just want to read part | |
3107 | // of the file back it in. | |
3108 | const unsigned char* | |
fe8718a4 | 3109 | get_input_view(off_t start, size_t size) |
730cdc88 ILT |
3110 | { return this->get_output_view(start, size); } |
3111 | ||
3112 | // Release a read bfufer. | |
3113 | void | |
fe8718a4 | 3114 | free_input_view(off_t, size_t, const unsigned char*) |
730cdc88 ILT |
3115 | { } |
3116 | ||
61ba1cf9 | 3117 | private: |
26736d8e | 3118 | // Map the file into memory. |
27bc2bce ILT |
3119 | void |
3120 | map(); | |
3121 | ||
26736d8e ILT |
3122 | // Allocate anonymous memory for the file. |
3123 | void* | |
3124 | map_anonymous(); | |
3125 | ||
c420411f ILT |
3126 | // Unmap the file from memory (and flush to disk buffers). |
3127 | void | |
3128 | unmap(); | |
3129 | ||
61ba1cf9 ILT |
3130 | // File name. |
3131 | const char* name_; | |
3132 | // File descriptor. | |
3133 | int o_; | |
3134 | // File size. | |
3135 | off_t file_size_; | |
3136 | // Base of file mapped into memory. | |
3137 | unsigned char* base_; | |
c420411f ILT |
3138 | // True iff base_ points to a memory buffer rather than an output file. |
3139 | bool map_is_anonymous_; | |
516cb3d0 ILT |
3140 | // True if this is a temporary file which should not be output. |
3141 | bool is_temporary_; | |
a2fb1b05 ILT |
3142 | }; |
3143 | ||
3144 | } // End namespace gold. | |
3145 | ||
3146 | #endif // !defined(GOLD_OUTPUT_H) |