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