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