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