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