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