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, |
75f65a3e | 188 | const Layout::Segment_list&, |
61ba1cf9 ILT |
189 | const Layout::Section_list&, |
190 | const Stringpool*); | |
54dc6425 ILT |
191 | |
192 | // Write the data to the file. | |
193 | void | |
75f65a3e ILT |
194 | do_write(Output_file*); |
195 | ||
196 | // Return the required alignment. | |
197 | uint64_t | |
198 | do_addralign() const | |
199 | { return Output_data::default_alignment(this->size_); } | |
54dc6425 ILT |
200 | |
201 | private: | |
61ba1cf9 ILT |
202 | // Write the data to the file with the right size and endianness. |
203 | template<int size, bool big_endian> | |
204 | void | |
205 | do_sized_write(Output_file*); | |
206 | ||
75f65a3e | 207 | int size_; |
61ba1cf9 | 208 | bool big_endian_; |
54dc6425 | 209 | const Layout::Segment_list& segment_list_; |
a3ad94ed | 210 | const Layout::Section_list& unattached_section_list_; |
61ba1cf9 | 211 | const Stringpool* secnamepool_; |
54dc6425 ILT |
212 | }; |
213 | ||
214 | // Output the segment headers. | |
215 | ||
216 | class Output_segment_headers : public Output_data | |
217 | { | |
218 | public: | |
61ba1cf9 ILT |
219 | Output_segment_headers(int size, bool big_endian, |
220 | const Layout::Segment_list& segment_list); | |
54dc6425 ILT |
221 | |
222 | // Write the data to the file. | |
223 | void | |
75f65a3e ILT |
224 | do_write(Output_file*); |
225 | ||
226 | // Return the required alignment. | |
227 | uint64_t | |
228 | do_addralign() const | |
229 | { return Output_data::default_alignment(this->size_); } | |
54dc6425 ILT |
230 | |
231 | private: | |
61ba1cf9 ILT |
232 | // Write the data to the file with the right size and endianness. |
233 | template<int size, bool big_endian> | |
234 | void | |
235 | do_sized_write(Output_file*); | |
236 | ||
75f65a3e | 237 | int size_; |
61ba1cf9 | 238 | bool big_endian_; |
54dc6425 ILT |
239 | const Layout::Segment_list& segment_list_; |
240 | }; | |
241 | ||
242 | // Output the ELF file header. | |
243 | ||
244 | class Output_file_header : public Output_data | |
245 | { | |
246 | public: | |
75f65a3e | 247 | Output_file_header(int size, |
61ba1cf9 | 248 | bool big_endian, |
75f65a3e | 249 | const General_options&, |
54dc6425 ILT |
250 | const Target*, |
251 | const Symbol_table*, | |
75f65a3e ILT |
252 | const Output_segment_headers*); |
253 | ||
254 | // Add information about the section headers. We lay out the ELF | |
255 | // file header before we create the section headers. | |
256 | void set_section_info(const Output_section_headers*, | |
257 | const Output_section* shstrtab); | |
54dc6425 ILT |
258 | |
259 | // Write the data to the file. | |
260 | void | |
75f65a3e ILT |
261 | do_write(Output_file*); |
262 | ||
263 | // Return the required alignment. | |
264 | uint64_t | |
265 | do_addralign() const | |
266 | { return Output_data::default_alignment(this->size_); } | |
267 | ||
268 | // Set the address and offset--we only implement this for error | |
269 | // checking. | |
270 | void | |
271 | do_set_address(uint64_t, off_t off) const | |
a3ad94ed | 272 | { gold_assert(off == 0); } |
54dc6425 ILT |
273 | |
274 | private: | |
61ba1cf9 ILT |
275 | // Write the data to the file with the right size and endianness. |
276 | template<int size, bool big_endian> | |
277 | void | |
278 | do_sized_write(Output_file*); | |
279 | ||
75f65a3e | 280 | int size_; |
61ba1cf9 | 281 | bool big_endian_; |
54dc6425 ILT |
282 | const General_options& options_; |
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 | ||
306 | // Record the output section. | |
307 | void | |
308 | set_output_section(Output_section* os) | |
309 | { | |
a3ad94ed | 310 | gold_assert(this->output_section_ == NULL); |
ead1e424 ILT |
311 | this->output_section_ = os; |
312 | } | |
313 | ||
314 | protected: | |
315 | // The child class must implement do_write. | |
316 | ||
317 | // Return the required alignment. | |
318 | uint64_t | |
319 | do_addralign() const | |
320 | { return this->addralign_; } | |
321 | ||
322 | // Return the section index of the output section. | |
323 | unsigned int | |
324 | do_out_shndx() const; | |
325 | ||
5a6f7e2d ILT |
326 | // Set the alignment. |
327 | void | |
328 | set_addralign(uint64_t addralign) | |
329 | { this->addralign_ = addralign; } | |
330 | ||
ead1e424 ILT |
331 | private: |
332 | // The output section for this section. | |
333 | const Output_section* output_section_; | |
334 | // The required alignment. | |
335 | uint64_t addralign_; | |
336 | }; | |
337 | ||
dbe717ef ILT |
338 | // A simple case of Output_data in which we have constant data to |
339 | // output. | |
ead1e424 | 340 | |
dbe717ef | 341 | class Output_data_const : public Output_section_data |
ead1e424 ILT |
342 | { |
343 | public: | |
dbe717ef ILT |
344 | Output_data_const(const std::string& data, uint64_t addralign) |
345 | : Output_section_data(data.size(), addralign), data_(data) | |
346 | { } | |
347 | ||
348 | Output_data_const(const char* p, off_t len, uint64_t addralign) | |
349 | : Output_section_data(len, addralign), data_(p, len) | |
350 | { } | |
351 | ||
352 | Output_data_const(const unsigned char* p, off_t len, uint64_t addralign) | |
353 | : Output_section_data(len, addralign), | |
354 | data_(reinterpret_cast<const char*>(p), len) | |
355 | { } | |
356 | ||
a3ad94ed ILT |
357 | // Add more data. |
358 | void | |
359 | add_data(const std::string& add) | |
360 | { | |
361 | this->data_.append(add); | |
362 | this->set_data_size(this->data_.size()); | |
363 | } | |
364 | ||
365 | // Write the data to the output file. | |
dbe717ef | 366 | void |
a3ad94ed | 367 | do_write(Output_file*); |
dbe717ef ILT |
368 | |
369 | private: | |
370 | std::string data_; | |
371 | }; | |
372 | ||
a3ad94ed ILT |
373 | // Another version of Output_data with constant data, in which the |
374 | // buffer is allocated by the caller. | |
dbe717ef | 375 | |
a3ad94ed | 376 | class Output_data_const_buffer : public Output_section_data |
dbe717ef ILT |
377 | { |
378 | public: | |
a3ad94ed ILT |
379 | Output_data_const_buffer(const unsigned char* p, off_t len, |
380 | uint64_t addralign) | |
381 | : Output_section_data(len, addralign), p_(p) | |
382 | { } | |
383 | ||
384 | // Write the data the output file. | |
385 | void | |
386 | do_write(Output_file*); | |
387 | ||
388 | private: | |
389 | const unsigned char* p_; | |
390 | }; | |
391 | ||
392 | // A place holder for data written out via some other mechanism. | |
393 | ||
394 | class Output_data_space : public Output_section_data | |
395 | { | |
396 | public: | |
397 | Output_data_space(off_t data_size, uint64_t addralign) | |
398 | : Output_section_data(data_size, addralign) | |
399 | { } | |
400 | ||
401 | explicit Output_data_space(uint64_t addralign) | |
ead1e424 ILT |
402 | : Output_section_data(addralign) |
403 | { } | |
404 | ||
405 | // Set the size. | |
406 | void | |
a3ad94ed ILT |
407 | set_space_size(off_t space_size) |
408 | { this->set_data_size(space_size); } | |
ead1e424 | 409 | |
5a6f7e2d ILT |
410 | // Set the alignment. |
411 | void | |
412 | set_space_alignment(uint64_t align) | |
413 | { this->set_addralign(align); } | |
414 | ||
a3ad94ed | 415 | // Write out the data--this must be handled elsewhere. |
ead1e424 ILT |
416 | void |
417 | do_write(Output_file*) | |
418 | { } | |
419 | }; | |
420 | ||
a3ad94ed ILT |
421 | // A string table which goes into an output section. |
422 | ||
423 | class Output_data_strtab : public Output_section_data | |
424 | { | |
425 | public: | |
426 | Output_data_strtab(Stringpool* strtab) | |
427 | : Output_section_data(1), strtab_(strtab) | |
428 | { } | |
429 | ||
430 | // This is called to set the address and file offset. Here we make | |
431 | // sure that the Stringpool is finalized. | |
432 | void | |
433 | do_set_address(uint64_t, off_t); | |
434 | ||
435 | // Write out the data. | |
436 | void | |
437 | do_write(Output_file*); | |
438 | ||
439 | private: | |
440 | Stringpool* strtab_; | |
441 | }; | |
442 | ||
c06b7b0b ILT |
443 | // This POD class is used to represent a single reloc in the output |
444 | // file. This could be a private class within Output_data_reloc, but | |
445 | // the templatization is complex enough that I broke it out into a | |
446 | // separate class. The class is templatized on either elfcpp::SHT_REL | |
447 | // or elfcpp::SHT_RELA, and also on whether this is a dynamic | |
448 | // relocation or an ordinary relocation. | |
449 | ||
450 | // A relocation can be against a global symbol, a local symbol, an | |
451 | // output section, or the undefined symbol at index 0. We represent | |
452 | // the latter by using a NULL global symbol. | |
453 | ||
454 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
455 | class Output_reloc; | |
456 | ||
457 | template<bool dynamic, int size, bool big_endian> | |
458 | class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
459 | { | |
460 | public: | |
461 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
462 | ||
463 | // An uninitialized entry. We need this because we want to put | |
464 | // instances of this class into an STL container. | |
465 | Output_reloc() | |
466 | : local_sym_index_(INVALID_CODE) | |
467 | { } | |
468 | ||
469 | // A reloc against a global symbol. | |
5a6f7e2d | 470 | |
a3ad94ed ILT |
471 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
472 | Address address) | |
5a6f7e2d ILT |
473 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), |
474 | shndx_(INVALID_CODE) | |
475 | { | |
476 | this->u1_.gsym = gsym; | |
477 | this->u2_.od = od; | |
478 | } | |
479 | ||
480 | Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj, | |
481 | unsigned int shndx, Address address) | |
482 | : address_(address), local_sym_index_(GSYM_CODE), type_(type), | |
483 | shndx_(shndx) | |
484 | { | |
485 | gold_assert(shndx != INVALID_CODE); | |
486 | this->u1_.gsym = gsym; | |
487 | this->u2_.relobj = relobj; | |
488 | } | |
c06b7b0b ILT |
489 | |
490 | // A reloc against a local symbol. | |
5a6f7e2d ILT |
491 | |
492 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
c06b7b0b | 493 | unsigned int local_sym_index, |
a3ad94ed ILT |
494 | unsigned int type, |
495 | Output_data* od, | |
496 | Address address) | |
5a6f7e2d ILT |
497 | : address_(address), local_sym_index_(local_sym_index), type_(type), |
498 | shndx_(INVALID_CODE) | |
c06b7b0b | 499 | { |
a3ad94ed ILT |
500 | gold_assert(local_sym_index != GSYM_CODE |
501 | && local_sym_index != INVALID_CODE); | |
5a6f7e2d ILT |
502 | this->u1_.relobj = relobj; |
503 | this->u2_.od = od; | |
504 | } | |
505 | ||
506 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
507 | unsigned int local_sym_index, | |
508 | unsigned int type, | |
509 | unsigned int shndx, | |
510 | Address address) | |
511 | : address_(address), local_sym_index_(local_sym_index), type_(type), | |
512 | shndx_(shndx) | |
513 | { | |
514 | gold_assert(local_sym_index != GSYM_CODE | |
515 | && local_sym_index != INVALID_CODE); | |
516 | gold_assert(shndx != INVALID_CODE); | |
517 | this->u1_.relobj = relobj; | |
518 | this->u2_.relobj = relobj; | |
c06b7b0b ILT |
519 | } |
520 | ||
521 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 522 | |
a3ad94ed ILT |
523 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
524 | Address address) | |
5a6f7e2d ILT |
525 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), |
526 | shndx_(INVALID_CODE) | |
527 | { | |
528 | this->u1_.os = os; | |
529 | this->u2_.od = od; | |
530 | } | |
531 | ||
532 | Output_reloc(Output_section* os, unsigned int type, Relobj* relobj, | |
533 | unsigned int shndx, Address address) | |
534 | : address_(address), local_sym_index_(SECTION_CODE), type_(type), | |
535 | shndx_(shndx) | |
536 | { | |
537 | gold_assert(shndx != INVALID_CODE); | |
538 | this->u1_.os = os; | |
539 | this->u2_.relobj = relobj; | |
540 | } | |
c06b7b0b ILT |
541 | |
542 | // Write the reloc entry to an output view. | |
543 | void | |
544 | write(unsigned char* pov) const; | |
545 | ||
546 | // Write the offset and info fields to Write_rel. | |
547 | template<typename Write_rel> | |
548 | void write_rel(Write_rel*) const; | |
549 | ||
550 | private: | |
551 | // Return the symbol index. We can't do a double template | |
552 | // specialization, so we do a secondary template here. | |
553 | unsigned int | |
554 | get_symbol_index() const; | |
555 | ||
556 | // Codes for local_sym_index_. | |
557 | enum | |
558 | { | |
559 | // Global symbol. | |
560 | GSYM_CODE = -1U, | |
561 | // Output section. | |
562 | SECTION_CODE = -2U, | |
563 | // Invalid uninitialized entry. | |
564 | INVALID_CODE = -3U | |
565 | }; | |
566 | ||
567 | union | |
568 | { | |
569 | // For a local symbol, the object. We will never generate a | |
570 | // relocation against a local symbol in a dynamic object; that | |
571 | // doesn't make sense. And our callers will always be | |
572 | // templatized, so we use Sized_relobj here. | |
5a6f7e2d | 573 | Sized_relobj<size, big_endian>* relobj; |
c06b7b0b ILT |
574 | // For a global symbol, the symbol. If this is NULL, it indicates |
575 | // a relocation against the undefined 0 symbol. | |
576 | Symbol* gsym; | |
577 | // For a relocation against an output section, the output section. | |
578 | Output_section* os; | |
5a6f7e2d ILT |
579 | } u1_; |
580 | union | |
581 | { | |
582 | // If shndx_ is not INVALID CODE, the object which holds the input | |
583 | // section being used to specify the reloc address. | |
584 | Relobj* relobj; | |
585 | // If shndx_ is INVALID_CODE, the output data being used to | |
586 | // specify the reloc address. This may be NULL if the reloc | |
587 | // address is absolute. | |
588 | Output_data* od; | |
589 | } u2_; | |
590 | // The address offset within the input section or the Output_data. | |
591 | Address address_; | |
c06b7b0b ILT |
592 | // For a local symbol, the local symbol index. This is GSYM_CODE |
593 | // for a global symbol, or INVALID_CODE for an uninitialized value. | |
594 | unsigned int local_sym_index_; | |
a3ad94ed | 595 | // The reloc type--a processor specific code. |
c06b7b0b | 596 | unsigned int type_; |
5a6f7e2d ILT |
597 | // If the reloc address is an input section in an object, the |
598 | // section index. This is INVALID_CODE if the reloc address is | |
599 | // specified in some other way. | |
600 | unsigned int shndx_; | |
c06b7b0b ILT |
601 | }; |
602 | ||
603 | // The SHT_RELA version of Output_reloc<>. This is just derived from | |
604 | // the SHT_REL version of Output_reloc, but it adds an addend. | |
605 | ||
606 | template<bool dynamic, int size, bool big_endian> | |
607 | class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
608 | { | |
609 | public: | |
610 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; | |
611 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend; | |
612 | ||
613 | // An uninitialized entry. | |
614 | Output_reloc() | |
615 | : rel_() | |
616 | { } | |
617 | ||
618 | // A reloc against a global symbol. | |
5a6f7e2d | 619 | |
a3ad94ed ILT |
620 | Output_reloc(Symbol* gsym, unsigned int type, Output_data* od, |
621 | Address address, Addend addend) | |
622 | : rel_(gsym, type, od, address), addend_(addend) | |
c06b7b0b ILT |
623 | { } |
624 | ||
5a6f7e2d ILT |
625 | Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj, |
626 | unsigned int shndx, Address address, Addend addend) | |
627 | : rel_(gsym, type, relobj, shndx, address), addend_(addend) | |
628 | { } | |
629 | ||
c06b7b0b | 630 | // A reloc against a local symbol. |
5a6f7e2d ILT |
631 | |
632 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
c06b7b0b | 633 | unsigned int local_sym_index, |
a3ad94ed ILT |
634 | unsigned int type, Output_data* od, Address address, |
635 | Addend addend) | |
5a6f7e2d ILT |
636 | : rel_(relobj, local_sym_index, type, od, address), addend_(addend) |
637 | { } | |
638 | ||
639 | Output_reloc(Sized_relobj<size, big_endian>* relobj, | |
640 | unsigned int local_sym_index, | |
641 | unsigned int type, | |
642 | unsigned int shndx, | |
643 | Address address, | |
644 | Addend addend) | |
645 | : rel_(relobj, local_sym_index, type, shndx, address), | |
646 | addend_(addend) | |
c06b7b0b ILT |
647 | { } |
648 | ||
649 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 650 | |
a3ad94ed ILT |
651 | Output_reloc(Output_section* os, unsigned int type, Output_data* od, |
652 | Address address, Addend addend) | |
653 | : rel_(os, type, od, address), addend_(addend) | |
c06b7b0b ILT |
654 | { } |
655 | ||
5a6f7e2d ILT |
656 | Output_reloc(Output_section* os, unsigned int type, Relobj* relobj, |
657 | unsigned int shndx, Address address, Addend addend) | |
658 | : rel_(os, type, relobj, shndx, address), addend_(addend) | |
659 | { } | |
660 | ||
c06b7b0b ILT |
661 | // Write the reloc entry to an output view. |
662 | void | |
663 | write(unsigned char* pov) const; | |
664 | ||
665 | private: | |
666 | // The basic reloc. | |
667 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_; | |
668 | // The addend. | |
669 | Addend addend_; | |
670 | }; | |
671 | ||
672 | // Output_data_reloc is used to manage a section containing relocs. | |
673 | // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC | |
674 | // indicates whether this is a dynamic relocation or a normal | |
675 | // relocation. Output_data_reloc_base is a base class. | |
676 | // Output_data_reloc is the real class, which we specialize based on | |
677 | // the reloc type. | |
678 | ||
679 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
680 | class Output_data_reloc_base : public Output_section_data | |
681 | { | |
682 | public: | |
683 | typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type; | |
684 | typedef typename Output_reloc_type::Address Address; | |
685 | static const int reloc_size = | |
686 | Reloc_types<sh_type, size, big_endian>::reloc_size; | |
687 | ||
688 | // Construct the section. | |
689 | Output_data_reloc_base() | |
690 | : Output_section_data(Output_data::default_alignment(size)) | |
691 | { } | |
692 | ||
693 | // Write out the data. | |
694 | void | |
695 | do_write(Output_file*); | |
696 | ||
697 | protected: | |
698 | // Add a relocation entry. | |
699 | void | |
700 | add(const Output_reloc_type& reloc) | |
701 | { | |
702 | this->relocs_.push_back(reloc); | |
703 | this->set_data_size(this->relocs_.size() * reloc_size); | |
704 | } | |
705 | ||
706 | private: | |
707 | typedef std::vector<Output_reloc_type> Relocs; | |
708 | ||
709 | Relocs relocs_; | |
710 | }; | |
711 | ||
712 | // The class which callers actually create. | |
713 | ||
714 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
715 | class Output_data_reloc; | |
716 | ||
717 | // The SHT_REL version of Output_data_reloc. | |
718 | ||
719 | template<bool dynamic, int size, bool big_endian> | |
720 | class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> | |
721 | : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian> | |
722 | { | |
723 | private: | |
724 | typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, | |
725 | big_endian> Base; | |
726 | ||
727 | public: | |
728 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
729 | typedef typename Output_reloc_type::Address Address; | |
730 | ||
731 | Output_data_reloc() | |
732 | : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>() | |
733 | { } | |
734 | ||
735 | // Add a reloc against a global symbol. | |
5a6f7e2d | 736 | |
c06b7b0b | 737 | void |
a3ad94ed ILT |
738 | add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address) |
739 | { this->add(Output_reloc_type(gsym, type, od, address)); } | |
c06b7b0b | 740 | |
5a6f7e2d ILT |
741 | void |
742 | add_global(Symbol* gsym, unsigned int type, Relobj* relobj, | |
743 | unsigned int shndx, Address address) | |
744 | { this->add(Output_reloc_type(gsym, type, relobj, shndx, address)); } | |
745 | ||
c06b7b0b | 746 | // Add a reloc against a local symbol. |
5a6f7e2d | 747 | |
c06b7b0b | 748 | void |
5a6f7e2d | 749 | add_local(Sized_relobj<size, big_endian>* relobj, |
a3ad94ed ILT |
750 | unsigned int local_sym_index, unsigned int type, |
751 | Output_data* od, Address address) | |
5a6f7e2d ILT |
752 | { this->add(Output_reloc_type(relobj, local_sym_index, type, od, address)); } |
753 | ||
754 | void | |
755 | add_local(Sized_relobj<size, big_endian>* relobj, | |
756 | unsigned int local_sym_index, unsigned int type, | |
757 | unsigned int shndx, Address address) | |
758 | { this->add(Output_reloc_type(relobj, local_sym_index, type, shndx, | |
759 | address)); } | |
760 | ||
c06b7b0b ILT |
761 | |
762 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 763 | |
c06b7b0b | 764 | void |
a3ad94ed ILT |
765 | add_output_section(Output_section* os, unsigned int type, |
766 | Output_data* od, Address address) | |
767 | { this->add(Output_reloc_type(os, type, od, address)); } | |
5a6f7e2d ILT |
768 | |
769 | void | |
770 | add_output_section(Output_section* os, unsigned int type, | |
771 | Relobj* relobj, unsigned int shndx, Address address) | |
772 | { this->add(Output_reloc_type(os, type, relobj, shndx, address)); } | |
c06b7b0b ILT |
773 | }; |
774 | ||
775 | // The SHT_RELA version of Output_data_reloc. | |
776 | ||
777 | template<bool dynamic, int size, bool big_endian> | |
778 | class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
779 | : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian> | |
780 | { | |
781 | private: | |
782 | typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, | |
783 | big_endian> Base; | |
784 | ||
785 | public: | |
786 | typedef typename Base::Output_reloc_type Output_reloc_type; | |
787 | typedef typename Output_reloc_type::Address Address; | |
788 | typedef typename Output_reloc_type::Addend Addend; | |
789 | ||
790 | Output_data_reloc() | |
791 | : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>() | |
792 | { } | |
793 | ||
794 | // Add a reloc against a global symbol. | |
5a6f7e2d | 795 | |
c06b7b0b | 796 | void |
a3ad94ed ILT |
797 | add_global(Symbol* gsym, unsigned int type, Output_data* od, |
798 | Address address, Addend addend) | |
799 | { this->add(Output_reloc_type(gsym, type, od, address, addend)); } | |
c06b7b0b | 800 | |
5a6f7e2d ILT |
801 | void |
802 | add_global(Symbol* gsym, unsigned int type, Relobj* relobj, | |
803 | unsigned int shndx, Address address, Addend addend) | |
804 | { this->add(Output_reloc_type(gsym, type, relobj, shndx, address, addend)); } | |
805 | ||
c06b7b0b | 806 | // Add a reloc against a local symbol. |
5a6f7e2d | 807 | |
c06b7b0b | 808 | void |
5a6f7e2d | 809 | add_local(Sized_relobj<size, big_endian>* relobj, |
c06b7b0b | 810 | unsigned int local_sym_index, unsigned int type, |
a3ad94ed | 811 | Output_data* od, Address address, Addend addend) |
c06b7b0b | 812 | { |
5a6f7e2d ILT |
813 | this->add(Output_reloc_type(relobj, local_sym_index, type, od, address, |
814 | addend)); | |
815 | } | |
816 | ||
817 | void | |
818 | add_local(Sized_relobj<size, big_endian>* relobj, | |
819 | unsigned int local_sym_index, unsigned int type, | |
820 | unsigned int shndx, Address address, Addend addend) | |
821 | { | |
822 | this->add(Output_reloc_type(relobj, local_sym_index, type, shndx, address, | |
c06b7b0b ILT |
823 | addend)); |
824 | } | |
825 | ||
826 | // A reloc against the STT_SECTION symbol of an output section. | |
5a6f7e2d | 827 | |
c06b7b0b | 828 | void |
a3ad94ed ILT |
829 | add_output_section(Output_section* os, unsigned int type, Output_data* od, |
830 | Address address, Addend addend) | |
831 | { this->add(Output_reloc_type(os, type, od, address, addend)); } | |
5a6f7e2d ILT |
832 | |
833 | void | |
834 | add_output_section(Output_section* os, unsigned int type, Relobj* relobj, | |
835 | unsigned int shndx, Address address, Addend addend) | |
836 | { this->add(Output_reloc_type(os, type, relobj, shndx, address, addend)); } | |
c06b7b0b ILT |
837 | }; |
838 | ||
dbe717ef ILT |
839 | // Output_data_got is used to manage a GOT. Each entry in the GOT is |
840 | // for one symbol--either a global symbol or a local symbol in an | |
ead1e424 | 841 | // object. The target specific code adds entries to the GOT as |
dbe717ef | 842 | // needed. |
ead1e424 ILT |
843 | |
844 | template<int size, bool big_endian> | |
dbe717ef | 845 | class Output_data_got : public Output_section_data |
ead1e424 ILT |
846 | { |
847 | public: | |
848 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype; | |
849 | ||
a3ad94ed | 850 | Output_data_got(const General_options* options) |
ead1e424 | 851 | : Output_section_data(Output_data::default_alignment(size)), |
a3ad94ed | 852 | options_(options), entries_() |
ead1e424 ILT |
853 | { } |
854 | ||
dbe717ef ILT |
855 | // Add an entry for a global symbol to the GOT. Return true if this |
856 | // is a new GOT entry, false if the symbol was already in the GOT. | |
857 | bool | |
858 | add_global(Symbol* gsym); | |
ead1e424 ILT |
859 | |
860 | // Add an entry for a local symbol to the GOT. This returns the | |
861 | // offset of the new entry from the start of the GOT. | |
862 | unsigned int | |
863 | add_local(Object* object, unsigned int sym_index) | |
864 | { | |
865 | this->entries_.push_back(Got_entry(object, sym_index)); | |
866 | this->set_got_size(); | |
867 | return this->last_got_offset(); | |
868 | } | |
869 | ||
870 | // Add a constant to the GOT. This returns the offset of the new | |
871 | // entry from the start of the GOT. | |
872 | unsigned int | |
873 | add_constant(Valtype constant) | |
874 | { | |
875 | this->entries_.push_back(Got_entry(constant)); | |
876 | this->set_got_size(); | |
877 | return this->last_got_offset(); | |
878 | } | |
879 | ||
880 | // Write out the GOT table. | |
881 | void | |
882 | do_write(Output_file*); | |
883 | ||
884 | private: | |
885 | // This POD class holds a single GOT entry. | |
886 | class Got_entry | |
887 | { | |
888 | public: | |
889 | // Create a zero entry. | |
890 | Got_entry() | |
891 | : local_sym_index_(CONSTANT_CODE) | |
892 | { this->u_.constant = 0; } | |
893 | ||
894 | // Create a global symbol entry. | |
a3ad94ed | 895 | explicit Got_entry(Symbol* gsym) |
ead1e424 ILT |
896 | : local_sym_index_(GSYM_CODE) |
897 | { this->u_.gsym = gsym; } | |
898 | ||
899 | // Create a local symbol entry. | |
900 | Got_entry(Object* object, unsigned int local_sym_index) | |
901 | : local_sym_index_(local_sym_index) | |
902 | { | |
a3ad94ed ILT |
903 | gold_assert(local_sym_index != GSYM_CODE |
904 | && local_sym_index != CONSTANT_CODE); | |
ead1e424 ILT |
905 | this->u_.object = object; |
906 | } | |
907 | ||
908 | // Create a constant entry. The constant is a host value--it will | |
909 | // be swapped, if necessary, when it is written out. | |
a3ad94ed | 910 | explicit Got_entry(Valtype constant) |
ead1e424 ILT |
911 | : local_sym_index_(CONSTANT_CODE) |
912 | { this->u_.constant = constant; } | |
913 | ||
914 | // Write the GOT entry to an output view. | |
915 | void | |
a3ad94ed | 916 | write(const General_options*, unsigned char* pov) const; |
ead1e424 ILT |
917 | |
918 | private: | |
919 | enum | |
920 | { | |
921 | GSYM_CODE = -1U, | |
922 | CONSTANT_CODE = -2U | |
923 | }; | |
924 | ||
925 | union | |
926 | { | |
927 | // For a local symbol, the object. | |
928 | Object* object; | |
929 | // For a global symbol, the symbol. | |
930 | Symbol* gsym; | |
931 | // For a constant, the constant. | |
932 | Valtype constant; | |
933 | } u_; | |
c06b7b0b ILT |
934 | // For a local symbol, the local symbol index. This is GSYM_CODE |
935 | // for a global symbol, or CONSTANT_CODE for a constant. | |
ead1e424 ILT |
936 | unsigned int local_sym_index_; |
937 | }; | |
938 | ||
939 | typedef std::vector<Got_entry> Got_entries; | |
940 | ||
941 | // Return the offset into the GOT of GOT entry I. | |
942 | unsigned int | |
943 | got_offset(unsigned int i) const | |
944 | { return i * (size / 8); } | |
945 | ||
946 | // Return the offset into the GOT of the last entry added. | |
947 | unsigned int | |
948 | last_got_offset() const | |
949 | { return this->got_offset(this->entries_.size() - 1); } | |
950 | ||
951 | // Set the size of the section. | |
952 | void | |
953 | set_got_size() | |
954 | { this->set_data_size(this->got_offset(this->entries_.size())); } | |
955 | ||
a3ad94ed ILT |
956 | // Options. |
957 | const General_options* options_; | |
ead1e424 ILT |
958 | // The list of GOT entries. |
959 | Got_entries entries_; | |
960 | }; | |
961 | ||
a3ad94ed ILT |
962 | // Output_data_dynamic is used to hold the data in SHT_DYNAMIC |
963 | // section. | |
964 | ||
965 | class Output_data_dynamic : public Output_section_data | |
966 | { | |
967 | public: | |
968 | Output_data_dynamic(const Target* target, Stringpool* pool) | |
969 | : Output_section_data(Output_data::default_alignment(target->get_size())), | |
970 | target_(target), entries_(), pool_(pool) | |
971 | { } | |
972 | ||
973 | // Add a new dynamic entry with a fixed numeric value. | |
974 | void | |
975 | add_constant(elfcpp::DT tag, unsigned int val) | |
976 | { this->add_entry(Dynamic_entry(tag, val)); } | |
977 | ||
978 | // Add a new dynamic entry with the address of a section. | |
979 | void | |
980 | add_section_address(elfcpp::DT tag, Output_section* os) | |
981 | { this->add_entry(Dynamic_entry(tag, os, false)); } | |
982 | ||
983 | // Add a new dynamic entry with the size of a section. | |
984 | void | |
985 | add_section_size(elfcpp::DT tag, Output_section* os) | |
986 | { this->add_entry(Dynamic_entry(tag, os, true)); } | |
987 | ||
988 | // Add a new dynamic entry with the address of a symbol. | |
989 | void | |
990 | add_symbol(elfcpp::DT tag, Symbol* sym) | |
991 | { this->add_entry(Dynamic_entry(tag, sym)); } | |
992 | ||
993 | // Add a new dynamic entry with a string. | |
994 | void | |
995 | add_string(elfcpp::DT tag, const char* str) | |
996 | { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, NULL))); } | |
997 | ||
998 | // Set the final data size. | |
999 | void | |
1000 | do_set_address(uint64_t, off_t); | |
1001 | ||
1002 | // Write out the dynamic entries. | |
1003 | void | |
1004 | do_write(Output_file*); | |
1005 | ||
1006 | private: | |
1007 | // This POD class holds a single dynamic entry. | |
1008 | class Dynamic_entry | |
1009 | { | |
1010 | public: | |
1011 | // Create an entry with a fixed numeric value. | |
1012 | Dynamic_entry(elfcpp::DT tag, unsigned int val) | |
1013 | : tag_(tag), classification_(DYNAMIC_NUMBER) | |
1014 | { this->u_.val = val; } | |
1015 | ||
1016 | // Create an entry with the size or address of a section. | |
1017 | Dynamic_entry(elfcpp::DT tag, Output_section* os, bool section_size) | |
1018 | : tag_(tag), | |
1019 | classification_(section_size | |
1020 | ? DYNAMIC_SECTION_SIZE | |
1021 | : DYNAMIC_SECTION_ADDRESS) | |
1022 | { this->u_.os = os; } | |
1023 | ||
1024 | // Create an entry with the address of a symbol. | |
1025 | Dynamic_entry(elfcpp::DT tag, Symbol* sym) | |
1026 | : tag_(tag), classification_(DYNAMIC_SYMBOL) | |
1027 | { this->u_.sym = sym; } | |
1028 | ||
1029 | // Create an entry with a string. | |
1030 | Dynamic_entry(elfcpp::DT tag, const char* str) | |
1031 | : tag_(tag), classification_(DYNAMIC_STRING) | |
1032 | { this->u_.str = str; } | |
1033 | ||
1034 | // Write the dynamic entry to an output view. | |
1035 | template<int size, bool big_endian> | |
1036 | void | |
1037 | write(unsigned char* pov, const Stringpool*) const; | |
1038 | ||
1039 | private: | |
1040 | enum Classification | |
1041 | { | |
1042 | // Number. | |
1043 | DYNAMIC_NUMBER, | |
1044 | // Section address. | |
1045 | DYNAMIC_SECTION_ADDRESS, | |
1046 | // Section size. | |
1047 | DYNAMIC_SECTION_SIZE, | |
1048 | // Symbol adress. | |
1049 | DYNAMIC_SYMBOL, | |
1050 | // String. | |
1051 | DYNAMIC_STRING | |
1052 | }; | |
1053 | ||
1054 | union | |
1055 | { | |
1056 | // For DYNAMIC_NUMBER. | |
1057 | unsigned int val; | |
1058 | // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE. | |
1059 | Output_section* os; | |
1060 | // For DYNAMIC_SYMBOL. | |
1061 | Symbol* sym; | |
1062 | // For DYNAMIC_STRING. | |
1063 | const char* str; | |
1064 | } u_; | |
1065 | // The dynamic tag. | |
1066 | elfcpp::DT tag_; | |
1067 | // The type of entry. | |
1068 | Classification classification_; | |
1069 | }; | |
1070 | ||
1071 | // Add an entry to the list. | |
1072 | void | |
1073 | add_entry(const Dynamic_entry& entry) | |
1074 | { this->entries_.push_back(entry); } | |
1075 | ||
1076 | // Sized version of write function. | |
1077 | template<int size, bool big_endian> | |
1078 | void | |
1079 | sized_write(Output_file* of); | |
1080 | ||
1081 | // The type of the list of entries. | |
1082 | typedef std::vector<Dynamic_entry> Dynamic_entries; | |
1083 | ||
1084 | // The target. | |
1085 | const Target* target_; | |
1086 | // The entries. | |
1087 | Dynamic_entries entries_; | |
1088 | // The pool used for strings. | |
1089 | Stringpool* pool_; | |
1090 | }; | |
1091 | ||
a2fb1b05 ILT |
1092 | // An output section. We don't expect to have too many output |
1093 | // sections, so we don't bother to do a template on the size. | |
1094 | ||
54dc6425 | 1095 | class Output_section : public Output_data |
a2fb1b05 ILT |
1096 | { |
1097 | public: | |
1098 | // Create an output section, giving the name, type, and flags. | |
61ba1cf9 | 1099 | Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword, |
ead1e424 | 1100 | bool may_add_data); |
54dc6425 | 1101 | virtual ~Output_section(); |
a2fb1b05 | 1102 | |
ead1e424 ILT |
1103 | // Add a new input section SHNDX, named NAME, with header SHDR, from |
1104 | // object OBJECT. Return the offset within the output section. | |
a2fb1b05 ILT |
1105 | template<int size, bool big_endian> |
1106 | off_t | |
f6ce93d6 | 1107 | add_input_section(Relobj* object, unsigned int shndx, const char *name, |
a2fb1b05 ILT |
1108 | const elfcpp::Shdr<size, big_endian>& shdr); |
1109 | ||
ead1e424 | 1110 | // Add generated data ODATA to this output section. |
c06b7b0b | 1111 | void |
ead1e424 ILT |
1112 | add_output_section_data(Output_section_data* posd); |
1113 | ||
a2fb1b05 ILT |
1114 | // Return the section name. |
1115 | const char* | |
1116 | name() const | |
1117 | { return this->name_; } | |
1118 | ||
1119 | // Return the section type. | |
1120 | elfcpp::Elf_Word | |
1121 | type() const | |
1122 | { return this->type_; } | |
1123 | ||
1124 | // Return the section flags. | |
1125 | elfcpp::Elf_Xword | |
1126 | flags() const | |
1127 | { return this->flags_; } | |
1128 | ||
ead1e424 | 1129 | // Return the section index in the output file. |
61ba1cf9 | 1130 | unsigned int |
ead1e424 ILT |
1131 | do_out_shndx() const |
1132 | { return this->out_shndx_; } | |
1133 | ||
1134 | // Set the output section index. | |
1135 | void | |
1136 | do_set_out_shndx(unsigned int shndx) | |
1137 | { this->out_shndx_ = shndx; } | |
61ba1cf9 | 1138 | |
a3ad94ed ILT |
1139 | // Return the entsize field. |
1140 | uint64_t | |
1141 | entsize() const | |
1142 | { return this->entsize_; } | |
1143 | ||
61ba1cf9 ILT |
1144 | // Set the entsize field. |
1145 | void | |
1146 | set_entsize(uint64_t v) | |
1147 | { this->entsize_ = v; } | |
1148 | ||
1149 | // Set the link field. | |
1150 | void | |
1151 | set_link(unsigned int v) | |
1152 | { this->link_ = v; } | |
1153 | ||
1154 | // Set the info field. | |
1155 | void | |
1156 | set_info(unsigned int v) | |
1157 | { this->info_ = v; } | |
1158 | ||
1159 | // Set the addralign field. | |
1160 | void | |
1161 | set_addralign(uint64_t v) | |
1162 | { this->addralign_ = v; } | |
1163 | ||
c06b7b0b ILT |
1164 | // Indicate that we need a symtab index. |
1165 | void | |
1166 | set_needs_symtab_index() | |
1167 | { this->needs_symtab_index_ = true; } | |
1168 | ||
1169 | // Return whether we need a symtab index. | |
1170 | bool | |
1171 | needs_symtab_index() const | |
1172 | { return this->needs_symtab_index_; } | |
1173 | ||
1174 | // Get the symtab index. | |
1175 | unsigned int | |
1176 | symtab_index() const | |
1177 | { | |
a3ad94ed | 1178 | gold_assert(this->symtab_index_ != 0); |
c06b7b0b ILT |
1179 | return this->symtab_index_; |
1180 | } | |
1181 | ||
1182 | // Set the symtab index. | |
1183 | void | |
1184 | set_symtab_index(unsigned int index) | |
1185 | { | |
a3ad94ed | 1186 | gold_assert(index != 0); |
c06b7b0b ILT |
1187 | this->symtab_index_ = index; |
1188 | } | |
1189 | ||
1190 | // Indicate that we need a dynsym index. | |
1191 | void | |
1192 | set_needs_dynsym_index() | |
1193 | { this->needs_dynsym_index_ = true; } | |
1194 | ||
1195 | // Return whether we need a dynsym index. | |
1196 | bool | |
1197 | needs_dynsym_index() const | |
1198 | { return this->needs_dynsym_index_; } | |
1199 | ||
1200 | // Get the dynsym index. | |
1201 | unsigned int | |
1202 | dynsym_index() const | |
1203 | { | |
a3ad94ed | 1204 | gold_assert(this->dynsym_index_ != 0); |
c06b7b0b ILT |
1205 | return this->dynsym_index_; |
1206 | } | |
1207 | ||
1208 | // Set the dynsym index. | |
1209 | void | |
1210 | set_dynsym_index(unsigned int index) | |
1211 | { | |
a3ad94ed | 1212 | gold_assert(index != 0); |
c06b7b0b ILT |
1213 | this->dynsym_index_ = index; |
1214 | } | |
1215 | ||
ead1e424 ILT |
1216 | // Set the address of the Output_section. For a typical |
1217 | // Output_section, there is nothing to do, but if there are any | |
1218 | // Output_section_data objects we need to set the final addresses | |
1219 | // here. | |
1220 | void | |
1221 | do_set_address(uint64_t, off_t); | |
1222 | ||
54dc6425 | 1223 | // Write the data to the file. For a typical Output_section, this |
ead1e424 ILT |
1224 | // does nothing: the data is written out by calling Object::Relocate |
1225 | // on each input object. But if there are any Output_section_data | |
1226 | // objects we do need to write them out here. | |
a3ad94ed | 1227 | void |
ead1e424 | 1228 | do_write(Output_file*); |
54dc6425 | 1229 | |
75f65a3e ILT |
1230 | // Return the address alignment--function required by parent class. |
1231 | uint64_t | |
1232 | do_addralign() const | |
1233 | { return this->addralign_; } | |
1234 | ||
1235 | // Return whether this is an Output_section. | |
1236 | bool | |
1237 | do_is_section() const | |
1238 | { return true; } | |
1239 | ||
54dc6425 ILT |
1240 | // Return whether this is a section of the specified type. |
1241 | bool | |
75f65a3e | 1242 | do_is_section_type(elfcpp::Elf_Word type) const |
54dc6425 ILT |
1243 | { return this->type_ == type; } |
1244 | ||
1245 | // Return whether the specified section flag is set. | |
1246 | bool | |
75f65a3e | 1247 | do_is_section_flag_set(elfcpp::Elf_Xword flag) const |
54dc6425 ILT |
1248 | { return (this->flags_ & flag) != 0; } |
1249 | ||
61ba1cf9 ILT |
1250 | // Write the section header into *OPHDR. |
1251 | template<int size, bool big_endian> | |
1252 | void | |
1253 | write_header(const Stringpool*, elfcpp::Shdr_write<size, big_endian>*) const; | |
1254 | ||
a2fb1b05 | 1255 | private: |
ead1e424 ILT |
1256 | // In some cases we need to keep a list of the input sections |
1257 | // associated with this output section. We only need the list if we | |
1258 | // might have to change the offsets of the input section within the | |
1259 | // output section after we add the input section. The ordinary | |
1260 | // input sections will be written out when we process the object | |
1261 | // file, and as such we don't need to track them here. We do need | |
1262 | // to track Output_section_data objects here. We store instances of | |
1263 | // this structure in a std::vector, so it must be a POD. There can | |
1264 | // be many instances of this structure, so we use a union to save | |
1265 | // some space. | |
1266 | class Input_section | |
1267 | { | |
1268 | public: | |
1269 | Input_section() | |
1270 | : shndx_(0), p2align_(0), data_size_(0) | |
1271 | { this->u_.object = NULL; } | |
1272 | ||
f6ce93d6 | 1273 | Input_section(Relobj* object, unsigned int shndx, off_t data_size, |
ead1e424 ILT |
1274 | uint64_t addralign) |
1275 | : shndx_(shndx), | |
1276 | p2align_(ffsll(static_cast<long long>(addralign))), | |
1277 | data_size_(data_size) | |
1278 | { | |
a3ad94ed | 1279 | gold_assert(shndx != -1U); |
ead1e424 ILT |
1280 | this->u_.object = object; |
1281 | } | |
1282 | ||
1283 | Input_section(Output_section_data* posd) | |
1284 | : shndx_(-1U), | |
1285 | p2align_(ffsll(static_cast<long long>(posd->addralign()))), | |
1286 | data_size_(0) | |
1287 | { this->u_.posd = posd; } | |
1288 | ||
1289 | // The required alignment. | |
1290 | uint64_t | |
1291 | addralign() const | |
a3ad94ed ILT |
1292 | { |
1293 | return (this->p2align_ == 0 | |
1294 | ? 0 | |
1295 | : static_cast<uint64_t>(1) << (this->p2align_ - 1)); | |
1296 | } | |
ead1e424 ILT |
1297 | |
1298 | // Return the required size. | |
1299 | off_t | |
1300 | data_size() const; | |
1301 | ||
1302 | // Set the address and file offset. This is called during | |
1303 | // Layout::finalize. SECOFF is the file offset of the enclosing | |
1304 | // section. | |
1305 | void | |
1306 | set_address(uint64_t addr, off_t off, off_t secoff); | |
1307 | ||
1308 | // Write out the data. This does nothing for an input section. | |
1309 | void | |
1310 | write(Output_file*); | |
1311 | ||
1312 | private: | |
1313 | // Whether this is an input section. | |
1314 | bool | |
1315 | is_input_section() const | |
1316 | { return this->shndx_ != -1U; } | |
1317 | ||
1318 | // For an ordinary input section, this is the section index in | |
1319 | // the input file. For an Output_section_data, this is -1U. | |
1320 | unsigned int shndx_; | |
1321 | // The required alignment, stored as a power of 2. | |
1322 | unsigned int p2align_; | |
1323 | // For an ordinary input section, the section size. | |
1324 | off_t data_size_; | |
1325 | union | |
1326 | { | |
1327 | // If shndx_ != -1U, this points to the object which holds the | |
1328 | // input section. | |
f6ce93d6 | 1329 | Relobj* object; |
ead1e424 ILT |
1330 | // If shndx_ == -1U, this is the data to write out. |
1331 | Output_section_data* posd; | |
1332 | } u_; | |
1333 | }; | |
1334 | ||
1335 | typedef std::vector<Input_section> Input_section_list; | |
1336 | ||
a2fb1b05 ILT |
1337 | // Most of these fields are only valid after layout. |
1338 | ||
1339 | // The name of the section. This will point into a Stringpool. | |
1340 | const char* name_; | |
75f65a3e | 1341 | // The section address is in the parent class. |
a2fb1b05 ILT |
1342 | // The section alignment. |
1343 | uint64_t addralign_; | |
1344 | // The section entry size. | |
1345 | uint64_t entsize_; | |
75f65a3e | 1346 | // The file offset is in the parent class. |
a2fb1b05 ILT |
1347 | // The section link field. |
1348 | unsigned int link_; | |
1349 | // The section info field. | |
1350 | unsigned int info_; | |
1351 | // The section type. | |
1352 | elfcpp::Elf_Word type_; | |
1353 | // The section flags. | |
1354 | elfcpp::Elf_Xword flags_; | |
61ba1cf9 | 1355 | // The section index. |
ead1e424 | 1356 | unsigned int out_shndx_; |
c06b7b0b ILT |
1357 | // If there is a STT_SECTION for this output section in the normal |
1358 | // symbol table, this is the symbol index. This starts out as zero. | |
1359 | // It is initialized in Layout::finalize() to be the index, or -1U | |
1360 | // if there isn't one. | |
1361 | unsigned int symtab_index_; | |
1362 | // If there is a STT_SECTION for this output section in the dynamic | |
1363 | // symbol table, this is the symbol index. This starts out as zero. | |
1364 | // It is initialized in Layout::finalize() to be the index, or -1U | |
1365 | // if there isn't one. | |
1366 | unsigned int dynsym_index_; | |
ead1e424 ILT |
1367 | // The input sections. This will be empty in cases where we don't |
1368 | // need to keep track of them. | |
1369 | Input_section_list input_sections_; | |
1370 | // The offset of the first entry in input_sections_. | |
1371 | off_t first_input_offset_; | |
1372 | // Whether we permit adding data. | |
c06b7b0b ILT |
1373 | bool may_add_data_ : 1; |
1374 | // Whether this output section needs a STT_SECTION symbol in the | |
1375 | // normal symbol table. This will be true if there is a relocation | |
1376 | // which needs it. | |
1377 | bool needs_symtab_index_ : 1; | |
1378 | // Whether this output section needs a STT_SECTION symbol in the | |
1379 | // dynamic symbol table. This will be true if there is a dynamic | |
1380 | // relocation which needs it. | |
1381 | bool needs_dynsym_index_ : 1; | |
a2fb1b05 ILT |
1382 | }; |
1383 | ||
1384 | // An output segment. PT_LOAD segments are built from collections of | |
1385 | // output sections. Other segments typically point within PT_LOAD | |
1386 | // segments, and are built directly as needed. | |
1387 | ||
1388 | class Output_segment | |
1389 | { | |
1390 | public: | |
1391 | // Create an output segment, specifying the type and flags. | |
1392 | Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word); | |
1393 | ||
1394 | // Return the virtual address. | |
1395 | uint64_t | |
1396 | vaddr() const | |
1397 | { return this->vaddr_; } | |
1398 | ||
1399 | // Return the physical address. | |
1400 | uint64_t | |
1401 | paddr() const | |
1402 | { return this->paddr_; } | |
1403 | ||
1404 | // Return the segment type. | |
1405 | elfcpp::Elf_Word | |
1406 | type() const | |
1407 | { return this->type_; } | |
1408 | ||
1409 | // Return the segment flags. | |
1410 | elfcpp::Elf_Word | |
1411 | flags() const | |
1412 | { return this->flags_; } | |
1413 | ||
92e059d8 ILT |
1414 | // Return the memory size. |
1415 | uint64_t | |
1416 | memsz() const | |
1417 | { return this->memsz_; } | |
1418 | ||
ead1e424 ILT |
1419 | // Return the file size. |
1420 | off_t | |
1421 | filesz() const | |
1422 | { return this->filesz_; } | |
1423 | ||
75f65a3e ILT |
1424 | // Return the maximum alignment of the Output_data. |
1425 | uint64_t | |
ead1e424 | 1426 | addralign(); |
75f65a3e | 1427 | |
a2fb1b05 ILT |
1428 | // Add an Output_section to this segment. |
1429 | void | |
dbe717ef ILT |
1430 | add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) |
1431 | { this->add_output_section(os, seg_flags, false); } | |
1432 | ||
1433 | // Add an Output_section to the start of this segment. | |
1434 | void | |
1435 | add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags) | |
1436 | { this->add_output_section(os, seg_flags, true); } | |
75f65a3e ILT |
1437 | |
1438 | // Add an Output_data (which is not an Output_section) to the start | |
1439 | // of this segment. | |
1440 | void | |
1441 | add_initial_output_data(Output_data*); | |
1442 | ||
1443 | // Set the address of the segment to ADDR and the offset to *POFF | |
1444 | // (aligned if necessary), and set the addresses and offsets of all | |
ead1e424 ILT |
1445 | // contained output sections accordingly. Set the section indexes |
1446 | // of all contained output sections starting with *PSHNDX. Return | |
1447 | // the address of the immediately following segment. Update *POFF | |
1448 | // and *PSHNDX. This should only be called for a PT_LOAD segment. | |
75f65a3e | 1449 | uint64_t |
ead1e424 | 1450 | set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx); |
75f65a3e ILT |
1451 | |
1452 | // Set the offset of this segment based on the section. This should | |
1453 | // only be called for a non-PT_LOAD segment. | |
1454 | void | |
1455 | set_offset(); | |
1456 | ||
1457 | // Return the number of output sections. | |
1458 | unsigned int | |
1459 | output_section_count() const; | |
a2fb1b05 | 1460 | |
61ba1cf9 ILT |
1461 | // Write the segment header into *OPHDR. |
1462 | template<int size, bool big_endian> | |
1463 | void | |
ead1e424 | 1464 | write_header(elfcpp::Phdr_write<size, big_endian>*); |
61ba1cf9 ILT |
1465 | |
1466 | // Write the section headers of associated sections into V. | |
1467 | template<int size, bool big_endian> | |
1468 | unsigned char* | |
5482377d | 1469 | write_section_headers(const Stringpool*, |
ead1e424 ILT |
1470 | unsigned char* v, |
1471 | unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const; | |
61ba1cf9 | 1472 | |
a2fb1b05 ILT |
1473 | private: |
1474 | Output_segment(const Output_segment&); | |
1475 | Output_segment& operator=(const Output_segment&); | |
1476 | ||
54dc6425 | 1477 | typedef std::list<Output_data*> Output_data_list; |
a2fb1b05 | 1478 | |
dbe717ef ILT |
1479 | // Add an Output_section to this segment, specifying front or back. |
1480 | void | |
1481 | add_output_section(Output_section*, elfcpp::Elf_Word seg_flags, | |
1482 | bool front); | |
1483 | ||
ead1e424 ILT |
1484 | // Find the maximum alignment in an Output_data_list. |
1485 | static uint64_t | |
1486 | maximum_alignment(const Output_data_list*); | |
1487 | ||
75f65a3e ILT |
1488 | // Set the section addresses in an Output_data_list. |
1489 | uint64_t | |
ead1e424 ILT |
1490 | set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff, |
1491 | unsigned int* pshndx); | |
75f65a3e ILT |
1492 | |
1493 | // Return the number of Output_sections in an Output_data_list. | |
1494 | unsigned int | |
1495 | output_section_count_list(const Output_data_list*) const; | |
1496 | ||
61ba1cf9 ILT |
1497 | // Write the section headers in the list into V. |
1498 | template<int size, bool big_endian> | |
1499 | unsigned char* | |
1500 | write_section_headers_list(const Stringpool*, const Output_data_list*, | |
ead1e424 ILT |
1501 | unsigned char* v, |
1502 | unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const; | |
61ba1cf9 | 1503 | |
75f65a3e | 1504 | // The list of output data with contents attached to this segment. |
54dc6425 | 1505 | Output_data_list output_data_; |
75f65a3e ILT |
1506 | // The list of output data without contents attached to this segment. |
1507 | Output_data_list output_bss_; | |
a2fb1b05 ILT |
1508 | // The segment virtual address. |
1509 | uint64_t vaddr_; | |
1510 | // The segment physical address. | |
1511 | uint64_t paddr_; | |
1512 | // The size of the segment in memory. | |
1513 | uint64_t memsz_; | |
1514 | // The segment alignment. | |
1515 | uint64_t align_; | |
1516 | // The offset of the segment data within the file. | |
1517 | off_t offset_; | |
1518 | // The size of the segment data in the file. | |
1519 | off_t filesz_; | |
1520 | // The segment type; | |
1521 | elfcpp::Elf_Word type_; | |
1522 | // The segment flags. | |
1523 | elfcpp::Elf_Word flags_; | |
ead1e424 ILT |
1524 | // Whether we have set align_. |
1525 | bool is_align_known_; | |
a2fb1b05 ILT |
1526 | }; |
1527 | ||
61ba1cf9 | 1528 | // This class represents the output file. |
a2fb1b05 ILT |
1529 | |
1530 | class Output_file | |
1531 | { | |
1532 | public: | |
61ba1cf9 ILT |
1533 | Output_file(const General_options& options); |
1534 | ||
1535 | // Open the output file. FILE_SIZE is the final size of the file. | |
1536 | void | |
1537 | open(off_t file_size); | |
1538 | ||
1539 | // Close the output file and make sure there are no error. | |
1540 | void | |
1541 | close(); | |
1542 | ||
1543 | // We currently always use mmap which makes the view handling quite | |
1544 | // simple. In the future we may support other approaches. | |
a2fb1b05 ILT |
1545 | |
1546 | // Write data to the output file. | |
1547 | void | |
61ba1cf9 ILT |
1548 | write(off_t offset, const void* data, off_t len) |
1549 | { memcpy(this->base_ + offset, data, len); } | |
1550 | ||
1551 | // Get a buffer to use to write to the file, given the offset into | |
1552 | // the file and the size. | |
1553 | unsigned char* | |
1554 | get_output_view(off_t start, off_t size) | |
1555 | { | |
a3ad94ed | 1556 | gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_); |
61ba1cf9 ILT |
1557 | return this->base_ + start; |
1558 | } | |
1559 | ||
1560 | // VIEW must have been returned by get_output_view. Write the | |
1561 | // buffer to the file, passing in the offset and the size. | |
1562 | void | |
1563 | write_output_view(off_t, off_t, unsigned char*) | |
1564 | { } | |
1565 | ||
1566 | private: | |
1567 | // General options. | |
1568 | const General_options& options_; | |
1569 | // File name. | |
1570 | const char* name_; | |
1571 | // File descriptor. | |
1572 | int o_; | |
1573 | // File size. | |
1574 | off_t file_size_; | |
1575 | // Base of file mapped into memory. | |
1576 | unsigned char* base_; | |
a2fb1b05 ILT |
1577 | }; |
1578 | ||
1579 | } // End namespace gold. | |
1580 | ||
1581 | #endif // !defined(GOLD_OUTPUT_H) |