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