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