]>
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 <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
a2fb1b05 ILT |
23 | #ifndef GOLD_OUTPUT_H |
24 | #define GOLD_OUTPUT_H | |
25 | ||
26 | #include <list> | |
ead1e424 | 27 | #include <vector> |
a2fb1b05 ILT |
28 | |
29 | #include "elfcpp.h" | |
54dc6425 | 30 | #include "layout.h" |
c06b7b0b | 31 | #include "reloc-types.h" |
a2fb1b05 ILT |
32 | |
33 | namespace gold | |
34 | { | |
35 | ||
61ba1cf9 | 36 | class General_options; |
a2fb1b05 | 37 | class Object; |
a3ad94ed | 38 | class Symbol; |
a2fb1b05 | 39 | class Output_file; |
c06b7b0b | 40 | class Output_section; |
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: | |
75f65a3e | 52 | explicit Output_data(off_t data_size = 0) |
4f4c5f80 ILT |
53 | : address_(0), data_size_(data_size), offset_(-1), |
54 | dynamic_reloc_count_(0) | |
a2fb1b05 ILT |
55 | { } |
56 | ||
57 | virtual | |
58 | ~Output_data(); | |
59 | ||
ead1e424 ILT |
60 | // Return the address. This is only valid after Layout::finalize is |
61 | // finished. | |
75f65a3e ILT |
62 | uint64_t |
63 | address() const | |
64 | { return this->address_; } | |
65 | ||
ead1e424 ILT |
66 | // Return the size of the data. This must be valid after |
67 | // Layout::finalize calls set_address, but need not be valid before | |
68 | // then. | |
a2fb1b05 | 69 | off_t |
75f65a3e ILT |
70 | data_size() const |
71 | { return this->data_size_; } | |
72 | ||
ead1e424 ILT |
73 | // Return the file offset. This is only valid after |
74 | // Layout::finalize is finished. | |
75f65a3e ILT |
75 | off_t |
76 | offset() const | |
77 | { return this->offset_; } | |
78 | ||
79 | // Return the required alignment. | |
80 | uint64_t | |
81 | addralign() const | |
82 | { return this->do_addralign(); } | |
83 | ||
84 | // Return whether this is an Output_section. | |
85 | bool | |
86 | is_section() const | |
87 | { return this->do_is_section(); } | |
88 | ||
89 | // Return whether this is an Output_section of the specified type. | |
90 | bool | |
91 | is_section_type(elfcpp::Elf_Word stt) const | |
92 | { return this->do_is_section_type(stt); } | |
93 | ||
94 | // Return whether this is an Output_section with the specified flag | |
95 | // set. | |
96 | bool | |
97 | is_section_flag_set(elfcpp::Elf_Xword shf) const | |
98 | { return this->do_is_section_flag_set(shf); } | |
99 | ||
ead1e424 ILT |
100 | // Return the output section index, if there is an output section. |
101 | unsigned int | |
102 | out_shndx() const | |
103 | { return this->do_out_shndx(); } | |
104 | ||
105 | // Set the output section index, if this is an output section. | |
106 | void | |
107 | set_out_shndx(unsigned int shndx) | |
108 | { this->do_set_out_shndx(shndx); } | |
109 | ||
110 | // Set the address and file offset of this data. This is called | |
111 | // during Layout::finalize. | |
75f65a3e ILT |
112 | void |
113 | set_address(uint64_t addr, off_t off); | |
114 | ||
ead1e424 ILT |
115 | // Write the data to the output file. This is called after |
116 | // Layout::finalize is complete. | |
75f65a3e ILT |
117 | void |
118 | write(Output_file* file) | |
119 | { this->do_write(file); } | |
a2fb1b05 | 120 | |
a3ad94ed ILT |
121 | // This is called by Layout::finalize to note that all sizes must |
122 | // now be fixed. | |
123 | static void | |
124 | layout_complete() | |
125 | { Output_data::sizes_are_fixed = true; } | |
126 | ||
730cdc88 ILT |
127 | // Used to check that layout has been done. |
128 | static bool | |
129 | is_layout_complete() | |
130 | { return Output_data::sizes_are_fixed; } | |
131 | ||
4f4c5f80 ILT |
132 | // Count the number of dynamic relocations applied to this section. |
133 | void | |
134 | add_dynamic_reloc() | |
135 | { ++this->dynamic_reloc_count_; } | |
136 | ||
137 | // Return the number of dynamic relocations applied to this section. | |
138 | unsigned int | |
139 | dynamic_reloc_count() const | |
140 | { return this->dynamic_reloc_count_; } | |
141 | ||
75f65a3e ILT |
142 | protected: |
143 | // Functions that child classes may or in some cases must implement. | |
144 | ||
145 | // Write the data to the output file. | |
a2fb1b05 | 146 | virtual void |
75f65a3e ILT |
147 | do_write(Output_file*) = 0; |
148 | ||
149 | // Return the required alignment. | |
150 | virtual uint64_t | |
151 | do_addralign() const = 0; | |
152 | ||
153 | // Return whether this is an Output_section. | |
154 | virtual bool | |
155 | do_is_section() const | |
156 | { return false; } | |
a2fb1b05 | 157 | |
54dc6425 | 158 | // Return whether this is an Output_section of the specified type. |
75f65a3e | 159 | // This only needs to be implement by Output_section. |
54dc6425 | 160 | virtual bool |
75f65a3e | 161 | do_is_section_type(elfcpp::Elf_Word) const |
54dc6425 ILT |
162 | { return false; } |
163 | ||
75f65a3e ILT |
164 | // Return whether this is an Output_section with the specific flag |
165 | // set. This only needs to be implemented by Output_section. | |
54dc6425 | 166 | virtual bool |
75f65a3e | 167 | do_is_section_flag_set(elfcpp::Elf_Xword) const |
54dc6425 ILT |
168 | { return false; } |
169 | ||
ead1e424 ILT |
170 | // Return the output section index, if there is an output section. |
171 | virtual unsigned int | |
172 | do_out_shndx() const | |
a3ad94ed | 173 | { gold_unreachable(); } |
ead1e424 ILT |
174 | |
175 | // Set the output section index, if this is an output section. | |
176 | virtual void | |
177 | do_set_out_shndx(unsigned int) | |
a3ad94ed | 178 | { gold_unreachable(); } |
ead1e424 | 179 | |
75f65a3e | 180 | // Set the address and file offset of the data. This only needs to |
a3ad94ed ILT |
181 | // be implemented if the child needs to know. The child class can |
182 | // set its size in this call. | |
75f65a3e ILT |
183 | virtual void |
184 | do_set_address(uint64_t, off_t) | |
185 | { } | |
186 | ||
187 | // Functions that child classes may call. | |
188 | ||
a2fb1b05 ILT |
189 | // Set the size of the data. |
190 | void | |
75f65a3e | 191 | set_data_size(off_t data_size) |
a3ad94ed ILT |
192 | { |
193 | gold_assert(!Output_data::sizes_are_fixed); | |
194 | this->data_size_ = data_size; | |
195 | } | |
75f65a3e | 196 | |
730cdc88 ILT |
197 | // Return default alignment for the target size. |
198 | static uint64_t | |
199 | default_alignment(); | |
200 | ||
201 | // Return default alignment for a specified size--32 or 64. | |
75f65a3e | 202 | static uint64_t |
730cdc88 | 203 | default_alignment_for_size(int size); |
a2fb1b05 ILT |
204 | |
205 | private: | |
206 | Output_data(const Output_data&); | |
207 | Output_data& operator=(const Output_data&); | |
208 | ||
a3ad94ed ILT |
209 | // This is used for verification, to make sure that we don't try to |
210 | // change any sizes after we set the section addresses. | |
211 | static bool sizes_are_fixed; | |
212 | ||
75f65a3e ILT |
213 | // Memory address in file (not always meaningful). |
214 | uint64_t address_; | |
a2fb1b05 | 215 | // Size of data in file. |
75f65a3e ILT |
216 | off_t data_size_; |
217 | // Offset within file. | |
218 | off_t offset_; | |
4f4c5f80 ILT |
219 | // Count of dynamic relocations applied to this section. |
220 | unsigned int dynamic_reloc_count_; | |
a2fb1b05 ILT |
221 | }; |
222 | ||
54dc6425 ILT |
223 | // Output the section headers. |
224 | ||
225 | class Output_section_headers : public Output_data | |
226 | { | |
227 | public: | |
9025d29d | 228 | Output_section_headers(const Layout*, |
16649710 ILT |
229 | const Layout::Segment_list*, |
230 | const Layout::Section_list*, | |
61ba1cf9 | 231 | const Stringpool*); |
54dc6425 ILT |
232 | |
233 | // Write the data to the file. | |
234 | void | |
75f65a3e ILT |
235 | do_write(Output_file*); |
236 | ||
237 | // Return the required alignment. | |
238 | uint64_t | |
239 | do_addralign() const | |
730cdc88 | 240 | { return Output_data::default_alignment(); } |
54dc6425 ILT |
241 | |
242 | private: | |
61ba1cf9 ILT |
243 | // Write the data to the file with the right size and endianness. |
244 | template<int size, bool big_endian> | |
245 | void | |
246 | do_sized_write(Output_file*); | |
247 | ||
16649710 ILT |
248 | const Layout* layout_; |
249 | const Layout::Segment_list* segment_list_; | |
250 | const Layout::Section_list* unattached_section_list_; | |
61ba1cf9 | 251 | const Stringpool* secnamepool_; |
54dc6425 ILT |
252 | }; |
253 | ||
254 | // Output the segment headers. | |
255 | ||
256 | class Output_segment_headers : public Output_data | |
257 | { | |
258 | public: | |
9025d29d | 259 | Output_segment_headers(const Layout::Segment_list& segment_list); |
54dc6425 ILT |
260 | |
261 | // Write the data to the file. | |
262 | void | |
75f65a3e ILT |
263 | do_write(Output_file*); |
264 | ||
265 | // Return the required alignment. | |
266 | uint64_t | |
267 | do_addralign() const | |
730cdc88 | 268 | { return Output_data::default_alignment(); } |
54dc6425 ILT |
269 | |
270 | private: | |
61ba1cf9 ILT |
271 | // Write the data to the file with the right size and endianness. |
272 | template<int size, bool big_endian> | |
273 | void | |
274 | do_sized_write(Output_file*); | |
275 | ||
54dc6425 ILT |
276 | const Layout::Segment_list& segment_list_; |
277 | }; | |
278 | ||
279 | // Output the ELF file header. | |
280 | ||
281 | class Output_file_header : public Output_data | |
282 | { | |
283 | public: | |
9025d29d | 284 | Output_file_header(const Target*, |
54dc6425 | 285 | const Symbol_table*, |
75f65a3e ILT |
286 | const Output_segment_headers*); |
287 | ||
288 | // Add information about the section headers. We lay out the ELF | |
289 | // file header before we create the section headers. | |
290 | void set_section_info(const Output_section_headers*, | |
291 | const Output_section* shstrtab); | |
54dc6425 ILT |
292 | |
293 | // Write the data to the file. | |
294 | void | |
75f65a3e ILT |
295 | do_write(Output_file*); |
296 | ||
297 | // Return the required alignment. | |
298 | uint64_t | |
299 | do_addralign() const | |
730cdc88 | 300 | { return Output_data::default_alignment(); } |
75f65a3e ILT |
301 | |
302 | // Set the address and offset--we only implement this for error | |
303 | // checking. | |
304 | void | |
305 | do_set_address(uint64_t, off_t off) const | |
a3ad94ed | 306 | { gold_assert(off == 0); } |
54dc6425 ILT |
307 | |
308 | private: | |
61ba1cf9 ILT |
309 | // Write the data to the file with the right size and endianness. |
310 | template<int size, bool big_endian> | |
311 | void | |
312 | do_sized_write(Output_file*); | |
313 | ||
54dc6425 ILT |
314 | const Target* target_; |
315 | const Symbol_table* symtab_; | |
61ba1cf9 | 316 | const Output_segment_headers* segment_header_; |
54dc6425 ILT |
317 | const Output_section_headers* section_header_; |
318 | const Output_section* shstrtab_; | |
319 | }; | |
320 | ||
ead1e424 ILT |
321 | // Output sections are mainly comprised of input sections. However, |
322 | // there are cases where we have data to write out which is not in an | |
323 | // input section. Output_section_data is used in such cases. This is | |
324 | // an abstract base class. | |
325 | ||
326 | class Output_section_data : public Output_data | |
327 | { | |
328 | public: | |
329 | Output_section_data(off_t data_size, uint64_t addralign) | |
330 | : Output_data(data_size), output_section_(NULL), addralign_(addralign) | |
331 | { } | |
332 | ||
333 | Output_section_data(uint64_t addralign) | |
334 | : Output_data(0), output_section_(NULL), addralign_(addralign) | |
335 | { } | |
336 | ||
16649710 ILT |
337 | // Return the output section. |
338 | const Output_section* | |
339 | output_section() const | |
340 | { return this->output_section_; } | |
341 | ||
ead1e424 ILT |
342 | // Record the output section. |
343 | void | |
16649710 | 344 | set_output_section(Output_section* os); |
ead1e424 | 345 | |
b8e6aad9 ILT |
346 | // Add an input section, for SHF_MERGE sections. This returns true |
347 | // if the section was handled. | |
348 | bool | |
349 | add_input_section(Relobj* object, unsigned int shndx) | |
350 | { return this->do_add_input_section(object, shndx); } | |
351 | ||
352 | // Given an input OBJECT, an input section index SHNDX within that | |
353 | // object, and an OFFSET relative to the start of that input | |
730cdc88 ILT |
354 | // section, return whether or not the corresponding offset within |
355 | // the output section is known. If this function returns true, it | |
356 | // sets *POUTPUT to the output offset. The value -1 indicates that | |
357 | // this input offset is being discarded. | |
b8e6aad9 | 358 | virtual bool |
730cdc88 ILT |
359 | output_offset(const Relobj* object, unsigned int shndx, off_t offset, |
360 | off_t *poutput) const | |
361 | { return this->do_output_offset(object, shndx, offset, poutput); } | |
b8e6aad9 | 362 | |
ead1e424 ILT |
363 | protected: |
364 | // The child class must implement do_write. | |
365 | ||
16649710 ILT |
366 | // The child class may implement specific adjustments to the output |
367 | // section. | |
368 | virtual void | |
369 | do_adjust_output_section(Output_section*) | |
370 | { } | |
371 | ||
b8e6aad9 ILT |
372 | // May be implemented by child class. Return true if the section |
373 | // was handled. | |
374 | virtual bool | |
375 | do_add_input_section(Relobj*, unsigned int) | |
376 | { gold_unreachable(); } | |
377 | ||
730cdc88 | 378 | // The child class may implement output_offset. |
b8e6aad9 | 379 | virtual bool |
730cdc88 | 380 | do_output_offset(const Relobj*, unsigned int, off_t, off_t*) const |
b8e6aad9 ILT |
381 | { return false; } |
382 | ||
ead1e424 ILT |
383 | // Return the required alignment. |
384 | uint64_t | |
385 | do_addralign() const | |
386 | { return this->addralign_; } | |
387 | ||
388 | // Return the section index of the output section. | |
389 | unsigned int | |
390 | do_out_shndx() const; | |
391 | ||
5a6f7e2d ILT |
392 | // Set the alignment. |
393 | void | |
394 | set_addralign(uint64_t addralign) | |
395 | { this->addralign_ = addralign; } | |
396 | ||
ead1e424 ILT |
397 | private: |
398 | // The output section for this section. | |
399 | const Output_section* output_section_; | |
400 | // The required alignment. | |
401 | uint64_t addralign_; | |
402 | }; | |
403 | ||
dbe717ef ILT |
404 | // A simple case of Output_data in which we have constant data to |
405 | // output. | |
ead1e424 | 406 | |
dbe717ef | 407 | class Output_data_const : public Output_section_data |
ead1e424 ILT |
408 | { |
409 | public: | |
dbe717ef ILT |
410 | Output_data_const(const std::string& data, uint64_t addralign) |
411 | : Output_section_data(data.size(), addralign), data_(data) | |
412 | { } | |
413 | ||
414 | Output_data_const(const char* p, off_t len, uint64_t addralign) | |
415 | : Output_section_data(len, addralign), data_(p, len) | |
416 | { } | |
417 | ||
418 | Output_data_const(const unsigned char* p, off_t len, uint64_t addralign) | |
419 | : Output_section_data(len, addralign), | |
420 | data_(reinterpret_cast<const char*>(p), len) | |
421 | { } | |
422 | ||
a3ad94ed ILT |
423 | // Add more data. |
424 | void | |
425 | add_data(const std::string& add) | |
426 | { | |
427 | this->data_.append(add); | |
428 | this->set_data_size(this->data_.size()); | |
429 | } | |
430 | ||
431 | // Write the data to the output file. | |
dbe717ef | 432 | void |
a3ad94ed | 433 | do_write(Output_file*); |
dbe717ef ILT |
434 | |
435 | private: | |
436 | std::string data_; | |
437 | }; | |
438 | ||
a3ad94ed ILT |
439 | // Another version of Output_data with constant data, in which the |
440 | // buffer is allocated by the caller. | |
dbe717ef | 441 | |
a3ad94ed | 442 | class Output_data_const_buffer : public Output_section_data |
dbe717ef ILT |
443 | { |
444 | public: | |
a3ad94ed ILT |
445 | Output_data_const_buffer(const unsigned char* p, off_t len, |
446 | uint64_t addralign) | |
447 | : Output_section_data(len, addralign), p_(p) | |
448 | { } | |
449 | ||
450 | // Write the data the output file. | |
451 | void | |
452 | do_write(Output_file*); | |
453 | ||
454 | private: | |
455 | const unsigned char* p_; | |
456 | }; | |
457 | ||
458 | // A place holder for data written out via some other mechanism. | |
459 | ||
460 | class Output_data_space : public Output_section_data | |
461 | { | |
462 | public: | |
463 | Output_data_space(off_t data_size, uint64_t addralign) | |
464 | : Output_section_data(data_size, addralign) | |
465 | { } | |
466 | ||
467 | explicit Output_data_space(uint64_t addralign) | |
ead1e424 ILT |
468 | : Output_section_data(addralign) |
469 | { } | |
470 | ||
471 | // Set the size. | |
472 | void | |
a3ad94ed ILT |
473 | set_space_size(off_t space_size) |
474 | { this->set_data_size(space_size); } | |
ead1e424 | 475 | |
5a6f7e2d ILT |
476 | // Set the alignment. |
477 | void | |
478 | set_space_alignment(uint64_t align) | |
479 | { this->set_addralign(align); } | |
480 | ||
a3ad94ed | 481 | // Write out the data--this must be handled elsewhere. |
ead1e424 ILT |
482 | void |
483 | do_write(Output_file*) | |
484 | { } | |
485 | }; | |
486 | ||
a3ad94ed ILT |
487 | // A string table which goes into an output section. |
488 | ||
489 | class Output_data_strtab : public Output_section_data | |
490 | { | |
491 | public: | |
492 | Output_data_strtab(Stringpool* strtab) | |
493 | : Output_section_data(1), strtab_(strtab) | |
494 | { } | |
495 | ||
496 | // This is called to set the address and file offset. Here we make | |
497 | // sure that the Stringpool is finalized. | |
498 | void | |
499 | do_set_address(uint64_t, off_t); | |
500 | ||
501 | // Write out the data. | |
502 | void | |
503 | do_write(Output_file*); | |
504 | ||
505 | private: | |
506 | Stringpool* strtab_; | |
507 | }; | |
508 | ||
c06b7b0b ILT |
509 | // This POD class is used to represent a single reloc in the output |
510 | // file. This could be a private class within Output_data_reloc, but | |
511 | // the templatization is complex enough that I broke it out into a | |
512 | // separate class. The class is templatized on either elfcpp::SHT_REL | |
513 | // or elfcpp::SHT_RELA, and also on whether this is a dynamic | |
514 | // relocation or an ordinary relocation. | |
515 | ||
516 | // A relocation can be against a global symbol, a local symbol, an | |
517 | // output section, or the undefined symbol at index 0. We represent | |
518 | // the latter by using a NULL global symbol. | |
519 | ||
520 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
521 | class Output_reloc; | |
522 | ||
523 | template<bool dynamic, int size, bool big_endian> | |
524 | class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
525 | { | |
526 | public: | |
527 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
528 | ||
529 | // An uninitialized entry. We need this because we want to put | |
530 | // instances of this class into an STL container. | |
531 | Output_reloc() | |
532 | : local_sym_index_(INVALID_CODE) | |
533 | { } | |
534 | ||
535 | // A reloc against a global symbol. | |
5a6f7e2d | 536 | |
a3ad94ed ILT |
537 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
538 | Address address) | |
5a6f7e2d ILT |
539 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), |
540 | shndx_(INVALID_CODE) | |
541 | { | |
542 | this->u1_.gsym = gsym; | |
543 | this->u2_.od = od; | |
544 | } | |
545 | ||
546 | Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj, | |
547 | unsigned int shndx, Address address) | |
548 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), | |
549 | shndx_(shndx) | |
550 | { | |
551 | gold_assert(shndx != INVALID_CODE); | |
552 | this->u1_.gsym = gsym; | |
553 | this->u2_.relobj = relobj; | |
554 | } | |
c06b7b0b ILT |
555 | |
556 | // A reloc against a local symbol. | |
5a6f7e2d ILT |
557 | |
558 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
c06b7b0b | 559 | unsigned int local_sym_index, |
a3ad94ed ILT |
560 | unsigned int type, |
561 | Output_data* od, | |
562 | Address address) | |
5a6f7e2d ILT |
563 | : address_(address), local_sym_index_(local_sym_index), type_(type), |
564 | shndx_(INVALID_CODE) | |
c06b7b0b | 565 | { |
a3ad94ed ILT |
566 | gold_assert(local_sym_index != GSYM_CODE |
567 | && local_sym_index != INVALID_CODE); | |
5a6f7e2d ILT |
568 | this->u1_.relobj = relobj; |
569 | this->u2_.od = od; | |
570 | } | |
571 | ||
572 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
573 | unsigned int local_sym_index, | |
574 | unsigned int type, | |
575 | unsigned int shndx, | |
576 | Address address) | |
577 | : address_(address), local_sym_index_(local_sym_index), type_(type), | |
578 | shndx_(shndx) | |
579 | { | |
580 | gold_assert(local_sym_index != GSYM_CODE | |
581 | && local_sym_index != INVALID_CODE); | |
582 | gold_assert(shndx != INVALID_CODE); | |
583 | this->u1_.relobj = relobj; | |
584 | this->u2_.relobj = relobj; | |
c06b7b0b ILT |
585 | } |
586 | ||
587 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 588 | |
a3ad94ed ILT |
589 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
590 | Address address) | |
5a6f7e2d ILT |
591 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), |
592 | shndx_(INVALID_CODE) | |
593 | { | |
594 | this->u1_.os = os; | |
595 | this->u2_.od = od; | |
596 | } | |
597 | ||
598 | Output_reloc(Output_section* os, unsigned int type, Relobj* relobj, | |
599 | unsigned int shndx, Address address) | |
600 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), | |
601 | shndx_(shndx) | |
602 | { | |
603 | gold_assert(shndx != INVALID_CODE); | |
604 | this->u1_.os = os; | |
605 | this->u2_.relobj = relobj; | |
606 | } | |
c06b7b0b ILT |
607 | |
608 | // Write the reloc entry to an output view. | |
609 | void | |
610 | write(unsigned char* pov) const; | |
611 | ||
612 | // Write the offset and info fields to Write_rel. | |
613 | template<typename Write_rel> | |
614 | void write_rel(Write_rel*) const; | |
615 | ||
616 | private: | |
617 | // Return the symbol index. We can't do a double template | |
618 | // specialization, so we do a secondary template here. | |
619 | unsigned int | |
620 | get_symbol_index() const; | |
621 | ||
622 | // Codes for local_sym_index_. | |
623 | enum | |
624 | { | |
625 | // Global symbol. | |
626 | GSYM_CODE = -1U, | |
627 | // Output section. | |
628 | SECTION_CODE = -2U, | |
629 | // Invalid uninitialized entry. | |
630 | INVALID_CODE = -3U | |
631 | }; | |
632 | ||
633 | union | |
634 | { | |
635 | // For a local symbol, the object. We will never generate a | |
636 | // relocation against a local symbol in a dynamic object; that | |
637 | // doesn't make sense. And our callers will always be | |
638 | // templatized, so we use Sized_relobj here. | |
5a6f7e2d | 639 | Sized_relobj<size, big_endian>* relobj; |
c06b7b0b ILT |
640 | // For a global symbol, the symbol. If this is NULL, it indicates |
641 | // a relocation against the undefined 0 symbol. | |
642 | Symbol* gsym; | |
643 | // For a relocation against an output section, the output section. | |
644 | Output_section* os; | |
5a6f7e2d ILT |
645 | } u1_; |
646 | union | |
647 | { | |
648 | // If shndx_ is not INVALID CODE, the object which holds the input | |
649 | // section being used to specify the reloc address. | |
650 | Relobj* relobj; | |
651 | // If shndx_ is INVALID_CODE, the output data being used to | |
652 | // specify the reloc address. This may be NULL if the reloc | |
653 | // address is absolute. | |
654 | Output_data* od; | |
655 | } u2_; | |
656 | // The address offset within the input section or the Output_data. | |
657 | Address address_; | |
c06b7b0b ILT |
658 | // For a local symbol, the local symbol index. This is GSYM_CODE |
659 | // for a global symbol, or INVALID_CODE for an uninitialized value. | |
660 | unsigned int local_sym_index_; | |
a3ad94ed | 661 | // The reloc type--a processor specific code. |
c06b7b0b | 662 | unsigned int type_; |
5a6f7e2d ILT |
663 | // If the reloc address is an input section in an object, the |
664 | // section index. This is INVALID_CODE if the reloc address is | |
665 | // specified in some other way. | |
666 | unsigned int shndx_; | |
c06b7b0b ILT |
667 | }; |
668 | ||
669 | // The SHT_RELA version of Output_reloc<>. This is just derived from | |
670 | // the SHT_REL version of Output_reloc, but it adds an addend. | |
671 | ||
672 | template<bool dynamic, int size, bool big_endian> | |
673 | class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
674 | { | |
675 | public: | |
676 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
677 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; | |
678 | ||
679 | // An uninitialized entry. | |
680 | Output_reloc() | |
681 | : rel_() | |
682 | { } | |
683 | ||
684 | // A reloc against a global symbol. | |
5a6f7e2d | 685 | |
a3ad94ed ILT |
686 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
687 | Address address, Addend addend) | |
688 | : rel_(gsym, type, od, address), addend_(addend) | |
c06b7b0b ILT |
689 | { } |
690 | ||
5a6f7e2d ILT |
691 | Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj, |
692 | unsigned int shndx, Address address, Addend addend) | |
693 | : rel_(gsym, type, relobj, shndx, address), addend_(addend) | |
694 | { } | |
695 | ||
c06b7b0b | 696 | // A reloc against a local symbol. |
5a6f7e2d ILT |
697 | |
698 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
c06b7b0b | 699 | unsigned int local_sym_index, |
a3ad94ed ILT |
700 | unsigned int type, Output_data* od, Address address, |
701 | Addend addend) | |
5a6f7e2d ILT |
702 | : rel_(relobj, local_sym_index, type, od, address), addend_(addend) |
703 | { } | |
704 | ||
705 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
706 | unsigned int local_sym_index, | |
707 | unsigned int type, | |
708 | unsigned int shndx, | |
709 | Address address, | |
710 | Addend addend) | |
711 | : rel_(relobj, local_sym_index, type, shndx, address), | |
712 | addend_(addend) | |
c06b7b0b ILT |
713 | { } |
714 | ||
715 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 716 | |
a3ad94ed ILT |
717 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
718 | Address address, Addend addend) | |
719 | : rel_(os, type, od, address), addend_(addend) | |
c06b7b0b ILT |
720 | { } |
721 | ||
5a6f7e2d ILT |
722 | Output_reloc(Output_section* os, unsigned int type, Relobj* relobj, |
723 | unsigned int shndx, Address address, Addend addend) | |
724 | : rel_(os, type, relobj, shndx, address), addend_(addend) | |
725 | { } | |
726 | ||
c06b7b0b ILT |
727 | // Write the reloc entry to an output view. |
728 | void | |
729 | write(unsigned char* pov) const; | |
730 | ||
731 | private: | |
732 | // The basic reloc. | |
733 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_; | |
734 | // The addend. | |
735 | Addend addend_; | |
736 | }; | |
737 | ||
738 | // Output_data_reloc is used to manage a section containing relocs. | |
739 | // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC | |
740 | // indicates whether this is a dynamic relocation or a normal | |
741 | // relocation. Output_data_reloc_base is a base class. | |
742 | // Output_data_reloc is the real class, which we specialize based on | |
743 | // the reloc type. | |
744 | ||
745 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
746 | class Output_data_reloc_base : public Output_section_data | |
747 | { | |
748 | public: | |
749 | typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type; | |
750 | typedef typename Output_reloc_type::Address Address; | |
751 | static const int reloc_size = | |
752 | Reloc_types<sh_type, size, big_endian>::reloc_size; | |
753 | ||
754 | // Construct the section. | |
755 | Output_data_reloc_base() | |
730cdc88 | 756 | : Output_section_data(Output_data::default_alignment_for_size(size)) |
c06b7b0b ILT |
757 | { } |
758 | ||
759 | // Write out the data. | |
760 | void | |
761 | do_write(Output_file*); | |
762 | ||
763 | protected: | |
16649710 ILT |
764 | // Set the entry size and the link. |
765 | void | |
766 | do_adjust_output_section(Output_section *os); | |
767 | ||
c06b7b0b ILT |
768 | // Add a relocation entry. |
769 | void | |
4f4c5f80 | 770 | add(Output_data *od, const Output_reloc_type& reloc) |
c06b7b0b ILT |
771 | { |
772 | this->relocs_.push_back(reloc); | |
773 | this->set_data_size(this->relocs_.size() * reloc_size); | |
4f4c5f80 | 774 | od->add_dynamic_reloc(); |
c06b7b0b ILT |
775 | } |
776 | ||
777 | private: | |
778 | typedef std::vector<Output_reloc_type> Relocs; | |
779 | ||
780 | Relocs relocs_; | |
781 | }; | |
782 | ||
783 | // The class which callers actually create. | |
784 | ||
785 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
786 | class Output_data_reloc; | |
787 | ||
788 | // The SHT_REL version of Output_data_reloc. | |
789 | ||
790 | template<bool dynamic, int size, bool big_endian> | |
791 | class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
792 | : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian> | |
793 | { | |
794 | private: | |
795 | typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, | |
796 | big_endian> Base; | |
797 | ||
798 | public: | |
799 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
800 | typedef typename Output_reloc_type::Address Address; | |
801 | ||
802 | Output_data_reloc() | |
803 | : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>() | |
804 | { } | |
805 | ||
806 | // Add a reloc against a global symbol. | |
5a6f7e2d | 807 | |
c06b7b0b | 808 | void |
a3ad94ed | 809 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address) |
4f4c5f80 | 810 | { this->add(od, Output_reloc_type(gsym, type, od, address)); } |
c06b7b0b | 811 | |
5a6f7e2d | 812 | void |
4f4c5f80 | 813 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj, |
5a6f7e2d | 814 | unsigned int shndx, Address address) |
4f4c5f80 | 815 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address)); } |
5a6f7e2d | 816 | |
c06b7b0b | 817 | // Add a reloc against a local symbol. |
5a6f7e2d | 818 | |
c06b7b0b | 819 | void |
5a6f7e2d | 820 | add_local(Sized_relobj<size, big_endian>* relobj, |
a3ad94ed ILT |
821 | unsigned int local_sym_index, unsigned int type, |
822 | Output_data* od, Address address) | |
4f4c5f80 ILT |
823 | { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, |
824 | address)); } | |
5a6f7e2d ILT |
825 | |
826 | void | |
827 | add_local(Sized_relobj<size, big_endian>* relobj, | |
828 | unsigned int local_sym_index, unsigned int type, | |
4f4c5f80 ILT |
829 | Output_data* od, unsigned int shndx, Address address) |
830 | { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, | |
831 | address)); } | |
5a6f7e2d | 832 | |
c06b7b0b ILT |
833 | |
834 | // A reloc against the STT_SECTION symbol of an output section. | |
4f4c5f80 ILT |
835 | // OS is the Output_section that the relocation refers to; OD is |
836 | // the Output_data object being relocated. | |
5a6f7e2d | 837 | |
c06b7b0b | 838 | void |
a3ad94ed ILT |
839 | add_output_section(Output_section* os, unsigned int type, |
840 | Output_data* od, Address address) | |
4f4c5f80 | 841 | { this->add(od, Output_reloc_type(os, type, od, address)); } |
5a6f7e2d ILT |
842 | |
843 | void | |
4f4c5f80 | 844 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
5a6f7e2d | 845 | Relobj* relobj, unsigned int shndx, Address address) |
4f4c5f80 | 846 | { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); } |
c06b7b0b ILT |
847 | }; |
848 | ||
849 | // The SHT_RELA version of Output_data_reloc. | |
850 | ||
851 | template<bool dynamic, int size, bool big_endian> | |
852 | class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
853 | : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
854 | { | |
855 | private: | |
856 | typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, | |
857 | big_endian> Base; | |
858 | ||
859 | public: | |
860 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
861 | typedef typename Output_reloc_type::Address Address; | |
862 | typedef typename Output_reloc_type::Addend Addend; | |
863 | ||
864 | Output_data_reloc() | |
865 | : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>() | |
866 | { } | |
867 | ||
868 | // Add a reloc against a global symbol. | |
5a6f7e2d | 869 | |
c06b7b0b | 870 | void |
a3ad94ed ILT |
871 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
872 | Address address, Addend addend) | |
4f4c5f80 | 873 | { this->add(od, Output_reloc_type(gsym, type, od, address, addend)); } |
c06b7b0b | 874 | |
5a6f7e2d | 875 | void |
4f4c5f80 ILT |
876 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj, |
877 | unsigned int shndx, Address address, | |
878 | Addend addend) | |
879 | { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address, | |
880 | addend)); } | |
5a6f7e2d | 881 | |
c06b7b0b | 882 | // Add a reloc against a local symbol. |
5a6f7e2d | 883 | |
c06b7b0b | 884 | void |
5a6f7e2d | 885 | add_local(Sized_relobj<size, big_endian>* relobj, |
c06b7b0b | 886 | unsigned int local_sym_index, unsigned int type, |
a3ad94ed | 887 | Output_data* od, Address address, Addend addend) |
c06b7b0b | 888 | { |
4f4c5f80 ILT |
889 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address, |
890 | addend)); | |
5a6f7e2d ILT |
891 | } |
892 | ||
893 | void | |
894 | add_local(Sized_relobj<size, big_endian>* relobj, | |
895 | unsigned int local_sym_index, unsigned int type, | |
4f4c5f80 ILT |
896 | Output_data* od, unsigned int shndx, Address address, |
897 | Addend addend) | |
5a6f7e2d | 898 | { |
4f4c5f80 ILT |
899 | this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx, |
900 | address, addend)); | |
c06b7b0b ILT |
901 | } |
902 | ||
903 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 904 | |
c06b7b0b | 905 | void |
a3ad94ed ILT |
906 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
907 | Address address, Addend addend) | |
4f4c5f80 | 908 | { this->add(os, Output_reloc_type(os, type, od, address, addend)); } |
5a6f7e2d ILT |
909 | |
910 | void | |
911 | add_output_section(Output_section* os, unsigned int type, Relobj* relobj, | |
912 | unsigned int shndx, Address address, Addend addend) | |
4f4c5f80 ILT |
913 | { this->add(os, Output_reloc_type(os, type, relobj, shndx, address, |
914 | addend)); } | |
c06b7b0b ILT |
915 | }; |
916 | ||
dbe717ef ILT |
917 | // Output_data_got is used to manage a GOT. Each entry in the GOT is |
918 | // for one symbol--either a global symbol or a local symbol in an | |
ead1e424 | 919 | // object. The target specific code adds entries to the GOT as |
dbe717ef | 920 | // needed. |
ead1e424 ILT |
921 | |
922 | template<int size, bool big_endian> | |
dbe717ef | 923 | class Output_data_got : public Output_section_data |
ead1e424 ILT |
924 | { |
925 | public: | |
926 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; | |
927 | ||
7e1edb90 | 928 | Output_data_got() |
730cdc88 ILT |
929 | : Output_section_data(Output_data::default_alignment_for_size(size)), |
930 | entries_() | |
ead1e424 ILT |
931 | { } |
932 | ||
dbe717ef ILT |
933 | // Add an entry for a global symbol to the GOT. Return true if this |
934 | // is a new GOT entry, false if the symbol was already in the GOT. | |
935 | bool | |
936 | add_global(Symbol* gsym); | |
ead1e424 | 937 | |
e727fa71 ILT |
938 | // Add an entry for a local symbol to the GOT. This returns true if |
939 | // this is a new GOT entry, false if the symbol already has a GOT | |
940 | // entry. | |
941 | bool | |
942 | add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index); | |
ead1e424 | 943 | |
07f397ab ILT |
944 | // Add an entry (or pair of entries) for a global TLS symbol to the GOT. |
945 | // Return true if this is a new GOT entry, false if the symbol was | |
946 | // already in the GOT. | |
947 | bool | |
948 | add_global_tls(Symbol* gsym, bool need_pair); | |
949 | ||
950 | // Add an entry (or pair of entries) for a local TLS symbol to the GOT. | |
951 | // This returns true if this is a new GOT entry, false if the symbol | |
952 | // already has a GOT entry. | |
953 | bool | |
954 | add_local_tls(Sized_relobj<size, big_endian>* object, | |
955 | unsigned int sym_index, bool need_pair); | |
956 | ||
ead1e424 ILT |
957 | // Add a constant to the GOT. This returns the offset of the new |
958 | // entry from the start of the GOT. | |
959 | unsigned int | |
960 | add_constant(Valtype constant) | |
961 | { | |
962 | this->entries_.push_back(Got_entry(constant)); | |
963 | this->set_got_size(); | |
964 | return this->last_got_offset(); | |
965 | } | |
966 | ||
967 | // Write out the GOT table. | |
968 | void | |
969 | do_write(Output_file*); | |
970 | ||
971 | private: | |
972 | // This POD class holds a single GOT entry. | |
973 | class Got_entry | |
974 | { | |
975 | public: | |
976 | // Create a zero entry. | |
977 | Got_entry() | |
978 | : local_sym_index_(CONSTANT_CODE) | |
979 | { this->u_.constant = 0; } | |
980 | ||
981 | // Create a global symbol entry. | |
a3ad94ed | 982 | explicit Got_entry(Symbol* gsym) |
ead1e424 ILT |
983 | : local_sym_index_(GSYM_CODE) |
984 | { this->u_.gsym = gsym; } | |
985 | ||
986 | // Create a local symbol entry. | |
e727fa71 ILT |
987 | Got_entry(Sized_relobj<size, big_endian>* object, |
988 | unsigned int local_sym_index) | |
ead1e424 ILT |
989 | : local_sym_index_(local_sym_index) |
990 | { | |
a3ad94ed ILT |
991 | gold_assert(local_sym_index != GSYM_CODE |
992 | && local_sym_index != CONSTANT_CODE); | |
ead1e424 ILT |
993 | this->u_.object = object; |
994 | } | |
995 | ||
996 | // Create a constant entry. The constant is a host value--it will | |
997 | // be swapped, if necessary, when it is written out. | |
a3ad94ed | 998 | explicit Got_entry(Valtype constant) |
ead1e424 ILT |
999 | : local_sym_index_(CONSTANT_CODE) |
1000 | { this->u_.constant = constant; } | |
1001 | ||
1002 | // Write the GOT entry to an output view. | |
1003 | void | |
7e1edb90 | 1004 | write(unsigned char* pov) const; |
ead1e424 ILT |
1005 | |
1006 | private: | |
1007 | enum | |
1008 | { | |
1009 | GSYM_CODE = -1U, | |
1010 | CONSTANT_CODE = -2U | |
1011 | }; | |
1012 | ||
1013 | union | |
1014 | { | |
1015 | // For a local symbol, the object. | |
e727fa71 | 1016 | Sized_relobj<size, big_endian>* object; |
ead1e424 ILT |
1017 | // For a global symbol, the symbol. |
1018 | Symbol* gsym; | |
1019 | // For a constant, the constant. | |
1020 | Valtype constant; | |
1021 | } u_; | |
c06b7b0b ILT |
1022 | // For a local symbol, the local symbol index. This is GSYM_CODE |
1023 | // for a global symbol, or CONSTANT_CODE for a constant. | |
ead1e424 ILT |
1024 | unsigned int local_sym_index_; |
1025 | }; | |
1026 | ||
1027 | typedef std::vector<Got_entry> Got_entries; | |
1028 | ||
1029 | // Return the offset into the GOT of GOT entry I. | |
1030 | unsigned int | |
1031 | got_offset(unsigned int i) const | |
1032 | { return i * (size / 8); } | |
1033 | ||
1034 | // Return the offset into the GOT of the last entry added. | |
1035 | unsigned int | |
1036 | last_got_offset() const | |
1037 | { return this->got_offset(this->entries_.size() - 1); } | |
1038 | ||
1039 | // Set the size of the section. | |
1040 | void | |
1041 | set_got_size() | |
1042 | { this->set_data_size(this->got_offset(this->entries_.size())); } | |
1043 | ||
1044 | // The list of GOT entries. | |
1045 | Got_entries entries_; | |
1046 | }; | |
1047 | ||
a3ad94ed ILT |
1048 | // Output_data_dynamic is used to hold the data in SHT_DYNAMIC |
1049 | // section. | |
1050 | ||
1051 | class Output_data_dynamic : public Output_section_data | |
1052 | { | |
1053 | public: | |
9025d29d | 1054 | Output_data_dynamic(Stringpool* pool) |
730cdc88 | 1055 | : Output_section_data(Output_data::default_alignment()), |
9025d29d | 1056 | entries_(), pool_(pool) |
a3ad94ed ILT |
1057 | { } |
1058 | ||
1059 | // Add a new dynamic entry with a fixed numeric value. | |
1060 | void | |
1061 | add_constant(elfcpp::DT tag, unsigned int val) | |
1062 | { this->add_entry(Dynamic_entry(tag, val)); } | |
1063 | ||
16649710 | 1064 | // Add a new dynamic entry with the address of output data. |
a3ad94ed | 1065 | void |
16649710 ILT |
1066 | add_section_address(elfcpp::DT tag, const Output_data* od) |
1067 | { this->add_entry(Dynamic_entry(tag, od, false)); } | |
a3ad94ed | 1068 | |
16649710 | 1069 | // Add a new dynamic entry with the size of output data. |
a3ad94ed | 1070 | void |
16649710 ILT |
1071 | add_section_size(elfcpp::DT tag, const Output_data* od) |
1072 | { this->add_entry(Dynamic_entry(tag, od, true)); } | |
a3ad94ed ILT |
1073 | |
1074 | // Add a new dynamic entry with the address of a symbol. | |
1075 | void | |
16649710 | 1076 | add_symbol(elfcpp::DT tag, const Symbol* sym) |
a3ad94ed ILT |
1077 | { this->add_entry(Dynamic_entry(tag, sym)); } |
1078 | ||
1079 | // Add a new dynamic entry with a string. | |
1080 | void | |
1081 | add_string(elfcpp::DT tag, const char* str) | |
cfd73a4e | 1082 | { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); } |
a3ad94ed | 1083 | |
41f542e7 ILT |
1084 | void |
1085 | add_string(elfcpp::DT tag, const std::string& str) | |
1086 | { this->add_string(tag, str.c_str()); } | |
1087 | ||
a3ad94ed ILT |
1088 | // Set the final data size. |
1089 | void | |
1090 | do_set_address(uint64_t, off_t); | |
1091 | ||
1092 | // Write out the dynamic entries. | |
1093 | void | |
1094 | do_write(Output_file*); | |
1095 | ||
16649710 ILT |
1096 | protected: |
1097 | // Adjust the output section to set the entry size. | |
1098 | void | |
1099 | do_adjust_output_section(Output_section*); | |
1100 | ||
a3ad94ed ILT |
1101 | private: |
1102 | // This POD class holds a single dynamic entry. | |
1103 | class Dynamic_entry | |
1104 | { | |
1105 | public: | |
1106 | // Create an entry with a fixed numeric value. | |
1107 | Dynamic_entry(elfcpp::DT tag, unsigned int val) | |
1108 | : tag_(tag), classification_(DYNAMIC_NUMBER) | |
1109 | { this->u_.val = val; } | |
1110 | ||
1111 | // Create an entry with the size or address of a section. | |
16649710 | 1112 | Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size) |
a3ad94ed ILT |
1113 | : tag_(tag), |
1114 | classification_(section_size | |
1115 | ? DYNAMIC_SECTION_SIZE | |
1116 | : DYNAMIC_SECTION_ADDRESS) | |
16649710 | 1117 | { this->u_.od = od; } |
a3ad94ed ILT |
1118 | |
1119 | // Create an entry with the address of a symbol. | |
16649710 | 1120 | Dynamic_entry(elfcpp::DT tag, const Symbol* sym) |
a3ad94ed ILT |
1121 | : tag_(tag), classification_(DYNAMIC_SYMBOL) |
1122 | { this->u_.sym = sym; } | |
1123 | ||
1124 | // Create an entry with a string. | |
1125 | Dynamic_entry(elfcpp::DT tag, const char* str) | |
1126 | : tag_(tag), classification_(DYNAMIC_STRING) | |
1127 | { this->u_.str = str; } | |
1128 | ||
1129 | // Write the dynamic entry to an output view. | |
1130 | template<int size, bool big_endian> | |
1131 | void | |
1ddbd1e6 | 1132 | write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const; |
a3ad94ed ILT |
1133 | |
1134 | private: | |
1135 | enum Classification | |
1136 | { | |
1137 | // Number. | |
1138 | DYNAMIC_NUMBER, | |
1139 | // Section address. | |
1140 | DYNAMIC_SECTION_ADDRESS, | |
1141 | // Section size. | |
1142 | DYNAMIC_SECTION_SIZE, | |
1143 | // Symbol adress. | |
1144 | DYNAMIC_SYMBOL, | |
1145 | // String. | |
1146 | DYNAMIC_STRING | |
1147 | }; | |
1148 | ||
1149 | union | |
1150 | { | |
1151 | // For DYNAMIC_NUMBER. | |
1152 | unsigned int val; | |
1153 | // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE. | |
16649710 | 1154 | const Output_data* od; |
a3ad94ed | 1155 | // For DYNAMIC_SYMBOL. |
16649710 | 1156 | const Symbol* sym; |
a3ad94ed ILT |
1157 | // For DYNAMIC_STRING. |
1158 | const char* str; | |
1159 | } u_; | |
1160 | // The dynamic tag. | |
1161 | elfcpp::DT tag_; | |
1162 | // The type of entry. | |
1163 | Classification classification_; | |
1164 | }; | |
1165 | ||
1166 | // Add an entry to the list. | |
1167 | void | |
1168 | add_entry(const Dynamic_entry& entry) | |
1169 | { this->entries_.push_back(entry); } | |
1170 | ||
1171 | // Sized version of write function. | |
1172 | template<int size, bool big_endian> | |
1173 | void | |
1174 | sized_write(Output_file* of); | |
1175 | ||
1176 | // The type of the list of entries. | |
1177 | typedef std::vector<Dynamic_entry> Dynamic_entries; | |
1178 | ||
a3ad94ed ILT |
1179 | // The entries. |
1180 | Dynamic_entries entries_; | |
1181 | // The pool used for strings. | |
1182 | Stringpool* pool_; | |
1183 | }; | |
1184 | ||
a2fb1b05 ILT |
1185 | // An output section. We don't expect to have too many output |
1186 | // sections, so we don't bother to do a template on the size. | |
1187 | ||
54dc6425 | 1188 | class Output_section : public Output_data |
a2fb1b05 ILT |
1189 | { |
1190 | public: | |
1191 | // Create an output section, giving the name, type, and flags. | |
b8e6aad9 | 1192 | Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword); |
54dc6425 | 1193 | virtual ~Output_section(); |
a2fb1b05 | 1194 | |
ead1e424 | 1195 | // Add a new input section SHNDX, named NAME, with header SHDR, from |
730cdc88 ILT |
1196 | // object OBJECT. RELOC_SHNDX is the index of a relocation section |
1197 | // which applies to this section, or 0 if none, or -1U if more than | |
1198 | // one. Return the offset within the output section. | |
a2fb1b05 ILT |
1199 | template<int size, bool big_endian> |
1200 | off_t | |
730cdc88 ILT |
1201 | add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx, |
1202 | const char *name, | |
1203 | const elfcpp::Shdr<size, big_endian>& shdr, | |
1204 | unsigned int reloc_shndx); | |
a2fb1b05 | 1205 | |
b8e6aad9 | 1206 | // Add generated data POSD to this output section. |
c06b7b0b | 1207 | void |
ead1e424 ILT |
1208 | add_output_section_data(Output_section_data* posd); |
1209 | ||
a2fb1b05 ILT |
1210 | // Return the section name. |
1211 | const char* | |
1212 | name() const | |
1213 | { return this->name_; } | |
1214 | ||
1215 | // Return the section type. | |
1216 | elfcpp::Elf_Word | |
1217 | type() const | |
1218 | { return this->type_; } | |
1219 | ||
1220 | // Return the section flags. | |
1221 | elfcpp::Elf_Xword | |
1222 | flags() const | |
1223 | { return this->flags_; } | |
1224 | ||
ead1e424 | 1225 | // Return the section index in the output file. |
61ba1cf9 | 1226 | unsigned int |
ead1e424 | 1227 | do_out_shndx() const |
91ea499d ILT |
1228 | { |
1229 | gold_assert(this->out_shndx_ != -1U); | |
1230 | return this->out_shndx_; | |
1231 | } | |
ead1e424 ILT |
1232 | |
1233 | // Set the output section index. | |
1234 | void | |
1235 | do_set_out_shndx(unsigned int shndx) | |
91ea499d ILT |
1236 | { |
1237 | gold_assert(this->out_shndx_ == -1U); | |
1238 | this->out_shndx_ = shndx; | |
1239 | } | |
61ba1cf9 | 1240 | |
a3ad94ed ILT |
1241 | // Return the entsize field. |
1242 | uint64_t | |
1243 | entsize() const | |
1244 | { return this->entsize_; } | |
1245 | ||
61ba1cf9 ILT |
1246 | // Set the entsize field. |
1247 | void | |
16649710 | 1248 | set_entsize(uint64_t v); |
61ba1cf9 | 1249 | |
16649710 ILT |
1250 | // Set the link field to the output section index of a section. |
1251 | void | |
14b31740 | 1252 | set_link_section(const Output_data* od) |
16649710 ILT |
1253 | { |
1254 | gold_assert(this->link_ == 0 | |
1255 | && !this->should_link_to_symtab_ | |
1256 | && !this->should_link_to_dynsym_); | |
1257 | this->link_section_ = od; | |
1258 | } | |
1259 | ||
1260 | // Set the link field to a constant. | |
61ba1cf9 ILT |
1261 | void |
1262 | set_link(unsigned int v) | |
16649710 ILT |
1263 | { |
1264 | gold_assert(this->link_section_ == NULL | |
1265 | && !this->should_link_to_symtab_ | |
1266 | && !this->should_link_to_dynsym_); | |
1267 | this->link_ = v; | |
1268 | } | |
61ba1cf9 | 1269 | |
16649710 ILT |
1270 | // Record that this section should link to the normal symbol table. |
1271 | void | |
1272 | set_should_link_to_symtab() | |
1273 | { | |
1274 | gold_assert(this->link_section_ == NULL | |
1275 | && this->link_ == 0 | |
1276 | && !this->should_link_to_dynsym_); | |
1277 | this->should_link_to_symtab_ = true; | |
1278 | } | |
1279 | ||
1280 | // Record that this section should link to the dynamic symbol table. | |
1281 | void | |
1282 | set_should_link_to_dynsym() | |
1283 | { | |
1284 | gold_assert(this->link_section_ == NULL | |
1285 | && this->link_ == 0 | |
1286 | && !this->should_link_to_symtab_); | |
1287 | this->should_link_to_dynsym_ = true; | |
1288 | } | |
1289 | ||
1290 | // Return the info field. | |
1291 | unsigned int | |
1292 | info() const | |
1293 | { | |
1294 | gold_assert(this->info_section_ == NULL); | |
1295 | return this->info_; | |
1296 | } | |
1297 | ||
1298 | // Set the info field to the output section index of a section. | |
1299 | void | |
14b31740 | 1300 | set_info_section(const Output_data* od) |
16649710 ILT |
1301 | { |
1302 | gold_assert(this->info_ == 0); | |
1303 | this->info_section_ = od; | |
1304 | } | |
1305 | ||
1306 | // Set the info field to a constant. | |
61ba1cf9 ILT |
1307 | void |
1308 | set_info(unsigned int v) | |
16649710 ILT |
1309 | { |
1310 | gold_assert(this->info_section_ == NULL); | |
1311 | this->info_ = v; | |
1312 | } | |
61ba1cf9 ILT |
1313 | |
1314 | // Set the addralign field. | |
1315 | void | |
1316 | set_addralign(uint64_t v) | |
1317 | { this->addralign_ = v; } | |
1318 | ||
c06b7b0b ILT |
1319 | // Indicate that we need a symtab index. |
1320 | void | |
1321 | set_needs_symtab_index() | |
1322 | { this->needs_symtab_index_ = true; } | |
1323 | ||
1324 | // Return whether we need a symtab index. | |
1325 | bool | |
1326 | needs_symtab_index() const | |
1327 | { return this->needs_symtab_index_; } | |
1328 | ||
1329 | // Get the symtab index. | |
1330 | unsigned int | |
1331 | symtab_index() const | |
1332 | { | |
a3ad94ed | 1333 | gold_assert(this->symtab_index_ != 0); |
c06b7b0b ILT |
1334 | return this->symtab_index_; |
1335 | } | |
1336 | ||
1337 | // Set the symtab index. | |
1338 | void | |
1339 | set_symtab_index(unsigned int index) | |
1340 | { | |
a3ad94ed | 1341 | gold_assert(index != 0); |
c06b7b0b ILT |
1342 | this->symtab_index_ = index; |
1343 | } | |
1344 | ||
1345 | // Indicate that we need a dynsym index. | |
1346 | void | |
1347 | set_needs_dynsym_index() | |
1348 | { this->needs_dynsym_index_ = true; } | |
1349 | ||
1350 | // Return whether we need a dynsym index. | |
1351 | bool | |
1352 | needs_dynsym_index() const | |
1353 | { return this->needs_dynsym_index_; } | |
1354 | ||
1355 | // Get the dynsym index. | |
1356 | unsigned int | |
1357 | dynsym_index() const | |
1358 | { | |
a3ad94ed | 1359 | gold_assert(this->dynsym_index_ != 0); |
c06b7b0b ILT |
1360 | return this->dynsym_index_; |
1361 | } | |
1362 | ||
1363 | // Set the dynsym index. | |
1364 | void | |
1365 | set_dynsym_index(unsigned int index) | |
1366 | { | |
a3ad94ed | 1367 | gold_assert(index != 0); |
c06b7b0b ILT |
1368 | this->dynsym_index_ = index; |
1369 | } | |
1370 | ||
730cdc88 ILT |
1371 | // Return whether this section should be written after all the input |
1372 | // sections are complete. | |
1373 | bool | |
1374 | after_input_sections() const | |
1375 | { return this->after_input_sections_; } | |
1376 | ||
1377 | // Record that this section should be written after all the input | |
1378 | // sections are complete. | |
1379 | void | |
1380 | set_after_input_sections() | |
1381 | { this->after_input_sections_ = true; } | |
1382 | ||
1383 | // Return whether the offset OFFSET in the input section SHNDX in | |
1384 | // object OBJECT is being included in the link. | |
1385 | bool | |
1386 | is_input_address_mapped(const Relobj* object, unsigned int shndx, | |
1387 | off_t offset) const; | |
1388 | ||
1389 | // Return the offset within the output section of OFFSET relative to | |
1390 | // the start of input section SHNDX in object OBJECT. | |
1391 | off_t | |
1392 | output_offset(const Relobj* object, unsigned int shndx, off_t offset) const; | |
1393 | ||
b8e6aad9 ILT |
1394 | // Return the output virtual address of OFFSET relative to the start |
1395 | // of input section SHNDX in object OBJECT. | |
1396 | uint64_t | |
1397 | output_address(const Relobj* object, unsigned int shndx, | |
1398 | off_t offset) const; | |
1399 | ||
ead1e424 ILT |
1400 | // Set the address of the Output_section. For a typical |
1401 | // Output_section, there is nothing to do, but if there are any | |
1402 | // Output_section_data objects we need to set the final addresses | |
1403 | // here. | |
1404 | void | |
1405 | do_set_address(uint64_t, off_t); | |
1406 | ||
54dc6425 | 1407 | // Write the data to the file. For a typical Output_section, this |
ead1e424 ILT |
1408 | // does nothing: the data is written out by calling Object::Relocate |
1409 | // on each input object. But if there are any Output_section_data | |
1410 | // objects we do need to write them out here. | |
a3ad94ed | 1411 | void |
ead1e424 | 1412 | do_write(Output_file*); |
54dc6425 | 1413 | |
75f65a3e ILT |
1414 | // Return the address alignment--function required by parent class. |
1415 | uint64_t | |
1416 | do_addralign() const | |
1417 | { return this->addralign_; } | |
1418 | ||
1419 | // Return whether this is an Output_section. | |
1420 | bool | |
1421 | do_is_section() const | |
1422 | { return true; } | |
1423 | ||
54dc6425 ILT |
1424 | // Return whether this is a section of the specified type. |
1425 | bool | |
75f65a3e | 1426 | do_is_section_type(elfcpp::Elf_Word type) const |
54dc6425 ILT |
1427 | { return this->type_ == type; } |
1428 | ||
1429 | // Return whether the specified section flag is set. | |
1430 | bool | |
75f65a3e | 1431 | do_is_section_flag_set(elfcpp::Elf_Xword flag) const |
54dc6425 ILT |
1432 | { return (this->flags_ & flag) != 0; } |
1433 | ||
61ba1cf9 ILT |
1434 | // Write the section header into *OPHDR. |
1435 | template<int size, bool big_endian> | |
1436 | void | |
16649710 ILT |
1437 | write_header(const Layout*, const Stringpool*, |
1438 | elfcpp::Shdr_write<size, big_endian>*) const; | |
61ba1cf9 | 1439 | |
a2fb1b05 | 1440 | private: |
ead1e424 ILT |
1441 | // In some cases we need to keep a list of the input sections |
1442 | // associated with this output section. We only need the list if we | |
1443 | // might have to change the offsets of the input section within the | |
1444 | // output section after we add the input section. The ordinary | |
1445 | // input sections will be written out when we process the object | |
1446 | // file, and as such we don't need to track them here. We do need | |
1447 | // to track Output_section_data objects here. We store instances of | |
1448 | // this structure in a std::vector, so it must be a POD. There can | |
1449 | // be many instances of this structure, so we use a union to save | |
1450 | // some space. | |
1451 | class Input_section | |
1452 | { | |
1453 | public: | |
1454 | Input_section() | |
b8e6aad9 ILT |
1455 | : shndx_(0), p2align_(0) |
1456 | { | |
1457 | this->u1_.data_size = 0; | |
1458 | this->u2_.object = NULL; | |
1459 | } | |
ead1e424 | 1460 | |
b8e6aad9 | 1461 | // For an ordinary input section. |
f6ce93d6 | 1462 | Input_section(Relobj* object, unsigned int shndx, off_t data_size, |
ead1e424 ILT |
1463 | uint64_t addralign) |
1464 | : shndx_(shndx), | |
b8e6aad9 | 1465 | p2align_(ffsll(static_cast<long long>(addralign))) |
ead1e424 | 1466 | { |
b8e6aad9 ILT |
1467 | gold_assert(shndx != OUTPUT_SECTION_CODE |
1468 | && shndx != MERGE_DATA_SECTION_CODE | |
1469 | && shndx != MERGE_STRING_SECTION_CODE); | |
1470 | this->u1_.data_size = data_size; | |
1471 | this->u2_.object = object; | |
ead1e424 ILT |
1472 | } |
1473 | ||
b8e6aad9 | 1474 | // For a non-merge output section. |
ead1e424 | 1475 | Input_section(Output_section_data* posd) |
b8e6aad9 ILT |
1476 | : shndx_(OUTPUT_SECTION_CODE), |
1477 | p2align_(ffsll(static_cast<long long>(posd->addralign()))) | |
1478 | { | |
1479 | this->u1_.data_size = 0; | |
1480 | this->u2_.posd = posd; | |
1481 | } | |
1482 | ||
1483 | // For a merge section. | |
1484 | Input_section(Output_section_data* posd, bool is_string, uint64_t entsize) | |
1485 | : shndx_(is_string | |
1486 | ? MERGE_STRING_SECTION_CODE | |
1487 | : MERGE_DATA_SECTION_CODE), | |
1488 | p2align_(ffsll(static_cast<long long>(posd->addralign()))) | |
1489 | { | |
1490 | this->u1_.entsize = entsize; | |
1491 | this->u2_.posd = posd; | |
1492 | } | |
ead1e424 ILT |
1493 | |
1494 | // The required alignment. | |
1495 | uint64_t | |
1496 | addralign() const | |
a3ad94ed ILT |
1497 | { |
1498 | return (this->p2align_ == 0 | |
1499 | ? 0 | |
1500 | : static_cast<uint64_t>(1) << (this->p2align_ - 1)); | |
1501 | } | |
ead1e424 ILT |
1502 | |
1503 | // Return the required size. | |
1504 | off_t | |
1505 | data_size() const; | |
1506 | ||
b8e6aad9 ILT |
1507 | // Return whether this is a merge section which matches the |
1508 | // parameters. | |
1509 | bool | |
87f95776 ILT |
1510 | is_merge_section(bool is_string, uint64_t entsize, |
1511 | uint64_t addralign) const | |
b8e6aad9 ILT |
1512 | { |
1513 | return (this->shndx_ == (is_string | |
1514 | ? MERGE_STRING_SECTION_CODE | |
1515 | : MERGE_DATA_SECTION_CODE) | |
87f95776 ILT |
1516 | && this->u1_.entsize == entsize |
1517 | && this->addralign() == addralign); | |
b8e6aad9 ILT |
1518 | } |
1519 | ||
1520 | // Set the output section. | |
1521 | void | |
1522 | set_output_section(Output_section* os) | |
1523 | { | |
1524 | gold_assert(!this->is_input_section()); | |
1525 | this->u2_.posd->set_output_section(os); | |
1526 | } | |
1527 | ||
ead1e424 ILT |
1528 | // Set the address and file offset. This is called during |
1529 | // Layout::finalize. SECOFF is the file offset of the enclosing | |
1530 | // section. | |
1531 | void | |
1532 | set_address(uint64_t addr, off_t off, off_t secoff); | |
1533 | ||
b8e6aad9 ILT |
1534 | // Add an input section, for SHF_MERGE sections. |
1535 | bool | |
1536 | add_input_section(Relobj* object, unsigned int shndx) | |
1537 | { | |
1538 | gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE | |
1539 | || this->shndx_ == MERGE_STRING_SECTION_CODE); | |
1540 | return this->u2_.posd->add_input_section(object, shndx); | |
1541 | } | |
1542 | ||
1543 | // Given an input OBJECT, an input section index SHNDX within that | |
1544 | // object, and an OFFSET relative to the start of that input | |
730cdc88 ILT |
1545 | // section, return whether or not the output offset is known. If |
1546 | // this function returns true, it sets *POUTPUT to the output | |
1547 | // offset. | |
b8e6aad9 | 1548 | bool |
730cdc88 ILT |
1549 | output_offset(const Relobj* object, unsigned int shndx, off_t offset, |
1550 | off_t *poutput) const; | |
b8e6aad9 | 1551 | |
ead1e424 ILT |
1552 | // Write out the data. This does nothing for an input section. |
1553 | void | |
1554 | write(Output_file*); | |
1555 | ||
1556 | private: | |
b8e6aad9 ILT |
1557 | // Code values which appear in shndx_. If the value is not one of |
1558 | // these codes, it is the input section index in the object file. | |
1559 | enum | |
1560 | { | |
1561 | // An Output_section_data. | |
1562 | OUTPUT_SECTION_CODE = -1U, | |
1563 | // An Output_section_data for an SHF_MERGE section with | |
1564 | // SHF_STRINGS not set. | |
1565 | MERGE_DATA_SECTION_CODE = -2U, | |
1566 | // An Output_section_data for an SHF_MERGE section with | |
1567 | // SHF_STRINGS set. | |
1568 | MERGE_STRING_SECTION_CODE = -3U | |
1569 | }; | |
1570 | ||
ead1e424 ILT |
1571 | // Whether this is an input section. |
1572 | bool | |
1573 | is_input_section() const | |
b8e6aad9 ILT |
1574 | { |
1575 | return (this->shndx_ != OUTPUT_SECTION_CODE | |
1576 | && this->shndx_ != MERGE_DATA_SECTION_CODE | |
1577 | && this->shndx_ != MERGE_STRING_SECTION_CODE); | |
1578 | } | |
ead1e424 | 1579 | |
b8e6aad9 ILT |
1580 | // For an ordinary input section, this is the section index in the |
1581 | // input file. For an Output_section_data, this is | |
1582 | // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or | |
1583 | // MERGE_STRING_SECTION_CODE. | |
ead1e424 ILT |
1584 | unsigned int shndx_; |
1585 | // The required alignment, stored as a power of 2. | |
1586 | unsigned int p2align_; | |
ead1e424 ILT |
1587 | union |
1588 | { | |
b8e6aad9 ILT |
1589 | // For an ordinary input section, the section size. |
1590 | off_t data_size; | |
1591 | // For OUTPUT_SECTION_CODE, this is not used. For | |
1592 | // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the | |
1593 | // entity size. | |
1594 | uint64_t entsize; | |
1595 | } u1_; | |
1596 | union | |
1597 | { | |
1598 | // For an ordinary input section, the object which holds the | |
ead1e424 | 1599 | // input section. |
f6ce93d6 | 1600 | Relobj* object; |
b8e6aad9 ILT |
1601 | // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or |
1602 | // MERGE_STRING_SECTION_CODE, the data. | |
ead1e424 | 1603 | Output_section_data* posd; |
b8e6aad9 | 1604 | } u2_; |
ead1e424 ILT |
1605 | }; |
1606 | ||
1607 | typedef std::vector<Input_section> Input_section_list; | |
1608 | ||
c51e6221 ILT |
1609 | // Fill data. This is used to fill in data between input sections. |
1610 | // When we have to keep track of the input sections, we can use an | |
1611 | // Output_data_const, but we don't want to have to keep track of | |
1612 | // input sections just to implement fills. For a fill we record the | |
1613 | // offset, and the actual data to be written out. | |
1614 | class Fill | |
1615 | { | |
1616 | public: | |
1617 | Fill(off_t section_offset, off_t length) | |
1618 | : section_offset_(section_offset), length_(length) | |
1619 | { } | |
1620 | ||
1621 | // Return section offset. | |
1622 | off_t | |
1623 | section_offset() const | |
1624 | { return this->section_offset_; } | |
1625 | ||
1626 | // Return fill length. | |
1627 | off_t | |
1628 | length() const | |
1629 | { return this->length_; } | |
1630 | ||
1631 | private: | |
1632 | // The offset within the output section. | |
1633 | off_t section_offset_; | |
1634 | // The length of the space to fill. | |
1635 | off_t length_; | |
1636 | }; | |
1637 | ||
1638 | typedef std::vector<Fill> Fill_list; | |
1639 | ||
b8e6aad9 ILT |
1640 | // Add a new output section by Input_section. |
1641 | void | |
1642 | add_output_section_data(Input_section*); | |
1643 | ||
1644 | // Add an SHF_MERGE input section. Returns true if the section was | |
1645 | // handled. | |
1646 | bool | |
1647 | add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags, | |
1648 | uint64_t entsize, uint64_t addralign); | |
1649 | ||
1650 | // Add an output SHF_MERGE section POSD to this output section. | |
1651 | // IS_STRING indicates whether it is a SHF_STRINGS section, and | |
1652 | // ENTSIZE is the entity size. This returns the entry added to | |
1653 | // input_sections_. | |
1654 | void | |
1655 | add_output_merge_section(Output_section_data* posd, bool is_string, | |
1656 | uint64_t entsize); | |
1657 | ||
a2fb1b05 ILT |
1658 | // Most of these fields are only valid after layout. |
1659 | ||
1660 | // The name of the section. This will point into a Stringpool. | |
1661 | const char* name_; | |
75f65a3e | 1662 | // The section address is in the parent class. |
a2fb1b05 ILT |
1663 | // The section alignment. |
1664 | uint64_t addralign_; | |
1665 | // The section entry size. | |
1666 | uint64_t entsize_; | |
75f65a3e | 1667 | // The file offset is in the parent class. |
16649710 | 1668 | // Set the section link field to the index of this section. |
14b31740 | 1669 | const Output_data* link_section_; |
16649710 | 1670 | // If link_section_ is NULL, this is the link field. |
a2fb1b05 | 1671 | unsigned int link_; |
16649710 | 1672 | // Set the section info field to the index of this section. |
14b31740 | 1673 | const Output_data* info_section_; |
16649710 | 1674 | // If info_section_ is NULL, this is the section info field. |
a2fb1b05 ILT |
1675 | unsigned int info_; |
1676 | // The section type. | |
1677 | elfcpp::Elf_Word type_; | |
1678 | // The section flags. | |
1679 | elfcpp::Elf_Xword flags_; | |
61ba1cf9 | 1680 | // The section index. |
ead1e424 | 1681 | unsigned int out_shndx_; |
c06b7b0b ILT |
1682 | // If there is a STT_SECTION for this output section in the normal |
1683 | // symbol table, this is the symbol index. This starts out as zero. | |
1684 | // It is initialized in Layout::finalize() to be the index, or -1U | |
1685 | // if there isn't one. | |
1686 | unsigned int symtab_index_; | |
1687 | // If there is a STT_SECTION for this output section in the dynamic | |
1688 | // symbol table, this is the symbol index. This starts out as zero. | |
1689 | // It is initialized in Layout::finalize() to be the index, or -1U | |
1690 | // if there isn't one. | |
1691 | unsigned int dynsym_index_; | |
ead1e424 ILT |
1692 | // The input sections. This will be empty in cases where we don't |
1693 | // need to keep track of them. | |
1694 | Input_section_list input_sections_; | |
1695 | // The offset of the first entry in input_sections_. | |
1696 | off_t first_input_offset_; | |
c51e6221 ILT |
1697 | // The fill data. This is separate from input_sections_ because we |
1698 | // often will need fill sections without needing to keep track of | |
1699 | // input sections. | |
1700 | Fill_list fills_; | |
c06b7b0b ILT |
1701 | // Whether this output section needs a STT_SECTION symbol in the |
1702 | // normal symbol table. This will be true if there is a relocation | |
1703 | // which needs it. | |
1704 | bool needs_symtab_index_ : 1; | |
1705 | // Whether this output section needs a STT_SECTION symbol in the | |
1706 | // dynamic symbol table. This will be true if there is a dynamic | |
1707 | // relocation which needs it. | |
1708 | bool needs_dynsym_index_ : 1; | |
16649710 ILT |
1709 | // Whether the link field of this output section should point to the |
1710 | // normal symbol table. | |
1711 | bool should_link_to_symtab_ : 1; | |
1712 | // Whether the link field of this output section should point to the | |
1713 | // dynamic symbol table. | |
1714 | bool should_link_to_dynsym_ : 1; | |
730cdc88 ILT |
1715 | // Whether this section should be written after all the input |
1716 | // sections are complete. | |
1717 | bool after_input_sections_ : 1; | |
a2fb1b05 ILT |
1718 | }; |
1719 | ||
1720 | // An output segment. PT_LOAD segments are built from collections of | |
1721 | // output sections. Other segments typically point within PT_LOAD | |
1722 | // segments, and are built directly as needed. | |
1723 | ||
1724 | class Output_segment | |
1725 | { | |
1726 | public: | |
1727 | // Create an output segment, specifying the type and flags. | |
1728 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); | |
1729 | ||
1730 | // Return the virtual address. | |
1731 | uint64_t | |
1732 | vaddr() const | |
1733 | { return this->vaddr_; } | |
1734 | ||
1735 | // Return the physical address. | |
1736 | uint64_t | |
1737 | paddr() const | |
1738 | { return this->paddr_; } | |
1739 | ||
1740 | // Return the segment type. | |
1741 | elfcpp::Elf_Word | |
1742 | type() const | |
1743 | { return this->type_; } | |
1744 | ||
1745 | // Return the segment flags. | |
1746 | elfcpp::Elf_Word | |
1747 | flags() const | |
1748 | { return this->flags_; } | |
1749 | ||
92e059d8 ILT |
1750 | // Return the memory size. |
1751 | uint64_t | |
1752 | memsz() const | |
1753 | { return this->memsz_; } | |
1754 | ||
ead1e424 ILT |
1755 | // Return the file size. |
1756 | off_t | |
1757 | filesz() const | |
1758 | { return this->filesz_; } | |
1759 | ||
75f65a3e ILT |
1760 | // Return the maximum alignment of the Output_data. |
1761 | uint64_t | |
ead1e424 | 1762 | addralign(); |
75f65a3e | 1763 | |
a2fb1b05 ILT |
1764 | // Add an Output_section to this segment. |
1765 | void | |
dbe717ef ILT |
1766 | add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) |
1767 | { this->add_output_section(os, seg_flags, false); } | |
1768 | ||
1769 | // Add an Output_section to the start of this segment. | |
1770 | void | |
1771 | add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) | |
1772 | { this->add_output_section(os, seg_flags, true); } | |
75f65a3e ILT |
1773 | |
1774 | // Add an Output_data (which is not an Output_section) to the start | |
1775 | // of this segment. | |
1776 | void | |
1777 | add_initial_output_data(Output_data*); | |
1778 | ||
4f4c5f80 ILT |
1779 | // Return the number of dynamic relocations applied to this segment. |
1780 | unsigned int | |
1781 | dynamic_reloc_count() const; | |
1782 | ||
75f65a3e ILT |
1783 | // Set the address of the segment to ADDR and the offset to *POFF |
1784 | // (aligned if necessary), and set the addresses and offsets of all | |
ead1e424 ILT |
1785 | // contained output sections accordingly. Set the section indexes |
1786 | // of all contained output sections starting with *PSHNDX. Return | |
1787 | // the address of the immediately following segment. Update *POFF | |
1788 | // and *PSHNDX. This should only be called for a PT_LOAD segment. | |
75f65a3e | 1789 | uint64_t |
ead1e424 | 1790 | set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx); |
75f65a3e | 1791 | |
0496d5e5 ILT |
1792 | // Set the minimum alignment of this segment. This may be adjusted |
1793 | // upward based on the section alignments. | |
1794 | void | |
1795 | set_minimum_addralign(uint64_t align) | |
1796 | { | |
1797 | gold_assert(!this->is_align_known_); | |
1798 | this->align_ = align; | |
1799 | } | |
1800 | ||
75f65a3e ILT |
1801 | // Set the offset of this segment based on the section. This should |
1802 | // only be called for a non-PT_LOAD segment. | |
1803 | void | |
1804 | set_offset(); | |
1805 | ||
1806 | // Return the number of output sections. | |
1807 | unsigned int | |
1808 | output_section_count() const; | |
a2fb1b05 | 1809 | |
61ba1cf9 ILT |
1810 | // Write the segment header into *OPHDR. |
1811 | template<int size, bool big_endian> | |
1812 | void | |
ead1e424 | 1813 | write_header(elfcpp::Phdr_write<size, big_endian>*); |
61ba1cf9 ILT |
1814 | |
1815 | // Write the section headers of associated sections into V. | |
1816 | template<int size, bool big_endian> | |
1817 | unsigned char* | |
16649710 | 1818 | write_section_headers(const Layout*, const Stringpool*, unsigned char* v, |
ead1e424 | 1819 | unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const; |
61ba1cf9 | 1820 | |
a2fb1b05 ILT |
1821 | private: |
1822 | Output_segment(const Output_segment&); | |
1823 | Output_segment& operator=(const Output_segment&); | |
1824 | ||
54dc6425 | 1825 | typedef std::list<Output_data*> Output_data_list; |
a2fb1b05 | 1826 | |
dbe717ef ILT |
1827 | // Add an Output_section to this segment, specifying front or back. |
1828 | void | |
1829 | add_output_section(Output_section*, elfcpp::Elf_Word seg_flags, | |
1830 | bool front); | |
1831 | ||
ead1e424 ILT |
1832 | // Find the maximum alignment in an Output_data_list. |
1833 | static uint64_t | |
1834 | maximum_alignment(const Output_data_list*); | |
1835 | ||
75f65a3e ILT |
1836 | // Set the section addresses in an Output_data_list. |
1837 | uint64_t | |
ead1e424 ILT |
1838 | set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff, |
1839 | unsigned int* pshndx); | |
75f65a3e ILT |
1840 | |
1841 | // Return the number of Output_sections in an Output_data_list. | |
1842 | unsigned int | |
1843 | output_section_count_list(const Output_data_list*) const; | |
1844 | ||
4f4c5f80 ILT |
1845 | // Return the number of dynamic relocs in an Output_data_list. |
1846 | unsigned int | |
1847 | dynamic_reloc_count_list(const Output_data_list*) const; | |
1848 | ||
61ba1cf9 ILT |
1849 | // Write the section headers in the list into V. |
1850 | template<int size, bool big_endian> | |
1851 | unsigned char* | |
16649710 ILT |
1852 | write_section_headers_list(const Layout*, const Stringpool*, |
1853 | const Output_data_list*, unsigned char* v, | |
ead1e424 | 1854 | unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const; |
61ba1cf9 | 1855 | |
75f65a3e | 1856 | // The list of output data with contents attached to this segment. |
54dc6425 | 1857 | Output_data_list output_data_; |
75f65a3e ILT |
1858 | // The list of output data without contents attached to this segment. |
1859 | Output_data_list output_bss_; | |
a2fb1b05 ILT |
1860 | // The segment virtual address. |
1861 | uint64_t vaddr_; | |
1862 | // The segment physical address. | |
1863 | uint64_t paddr_; | |
1864 | // The size of the segment in memory. | |
1865 | uint64_t memsz_; | |
0496d5e5 ILT |
1866 | // The segment alignment. The is_align_known_ field indicates |
1867 | // whether this has been finalized. It can be set to a minimum | |
1868 | // value before it is finalized. | |
a2fb1b05 ILT |
1869 | uint64_t align_; |
1870 | // The offset of the segment data within the file. | |
1871 | off_t offset_; | |
1872 | // The size of the segment data in the file. | |
1873 | off_t filesz_; | |
1874 | // The segment type; | |
1875 | elfcpp::Elf_Word type_; | |
1876 | // The segment flags. | |
1877 | elfcpp::Elf_Word flags_; | |
0496d5e5 | 1878 | // Whether we have finalized align_. |
ead1e424 | 1879 | bool is_align_known_; |
a2fb1b05 ILT |
1880 | }; |
1881 | ||
61ba1cf9 | 1882 | // This class represents the output file. |
a2fb1b05 ILT |
1883 | |
1884 | class Output_file | |
1885 | { | |
1886 | public: | |
c51e6221 ILT |
1887 | Output_file(const General_options& options, Target*); |
1888 | ||
1889 | // Get a pointer to the target. | |
1890 | Target* | |
1891 | target() const | |
1892 | { return this->target_; } | |
61ba1cf9 ILT |
1893 | |
1894 | // Open the output file. FILE_SIZE is the final size of the file. | |
1895 | void | |
1896 | open(off_t file_size); | |
1897 | ||
1898 | // Close the output file and make sure there are no error. | |
1899 | void | |
1900 | close(); | |
1901 | ||
1902 | // We currently always use mmap which makes the view handling quite | |
1903 | // simple. In the future we may support other approaches. | |
a2fb1b05 ILT |
1904 | |
1905 | // Write data to the output file. | |
1906 | void | |
61ba1cf9 ILT |
1907 | write(off_t offset, const void* data, off_t len) |
1908 | { memcpy(this->base_ + offset, data, len); } | |
1909 | ||
1910 | // Get a buffer to use to write to the file, given the offset into | |
1911 | // the file and the size. | |
1912 | unsigned char* | |
1913 | get_output_view(off_t start, off_t size) | |
1914 | { | |
a3ad94ed | 1915 | gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_); |
61ba1cf9 ILT |
1916 | return this->base_ + start; |
1917 | } | |
1918 | ||
1919 | // VIEW must have been returned by get_output_view. Write the | |
1920 | // buffer to the file, passing in the offset and the size. | |
1921 | void | |
1922 | write_output_view(off_t, off_t, unsigned char*) | |
1923 | { } | |
1924 | ||
730cdc88 ILT |
1925 | // Get a read/write buffer. This is used when we want to write part |
1926 | // of the file, read it in, and write it again. | |
1927 | unsigned char* | |
1928 | get_input_output_view(off_t start, off_t size) | |
1929 | { return this->get_output_view(start, size); } | |
1930 | ||
1931 | // Write a read/write buffer back to the file. | |
1932 | void | |
1933 | write_input_output_view(off_t, off_t, unsigned char*) | |
1934 | { } | |
1935 | ||
1936 | // Get a read buffer. This is used when we just want to read part | |
1937 | // of the file back it in. | |
1938 | const unsigned char* | |
1939 | get_input_view(off_t start, off_t size) | |
1940 | { return this->get_output_view(start, size); } | |
1941 | ||
1942 | // Release a read bfufer. | |
1943 | void | |
1944 | free_input_view(off_t, off_t, const unsigned char*) | |
1945 | { } | |
1946 | ||
61ba1cf9 ILT |
1947 | private: |
1948 | // General options. | |
1949 | const General_options& options_; | |
c51e6221 ILT |
1950 | // Target. |
1951 | Target* target_; | |
61ba1cf9 ILT |
1952 | // File name. |
1953 | const char* name_; | |
1954 | // File descriptor. | |
1955 | int o_; | |
1956 | // File size. | |
1957 | off_t file_size_; | |
1958 | // Base of file mapped into memory. | |
1959 | unsigned char* base_; | |
a2fb1b05 ILT |
1960 | }; |
1961 | ||
1962 | } // End namespace gold. | |
1963 | ||
1964 | #endif // !defined(GOLD_OUTPUT_H) |