* archive.cc: Formatting fixes: Remove whitespace between
[deliverable/binutils-gdb.git] / gold / output.h
1 // output.h -- manage the output file for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #ifndef GOLD_OUTPUT_H
24 #define GOLD_OUTPUT_H
25
26 #include <list>
27 #include <vector>
28
29 #include "elfcpp.h"
30 #include "mapfile.h"
31 #include "layout.h"
32 #include "reloc-types.h"
33
34 namespace gold
35 {
36
37 class General_options;
38 class Object;
39 class Symbol;
40 class Output_file;
41 class Output_merge_base;
42 class Output_section;
43 class Relocatable_relocs;
44 class Target;
45 template<int size, bool big_endian>
46 class Sized_target;
47 template<int size, bool big_endian>
48 class Sized_relobj;
49
50 // An abtract class for data which has to go into the output file.
51
52 class Output_data
53 {
54 public:
55 explicit Output_data()
56 : address_(0), data_size_(0), offset_(-1),
57 is_address_valid_(false), is_data_size_valid_(false),
58 is_offset_valid_(false), is_data_size_fixed_(false),
59 has_dynamic_reloc_(false)
60 { }
61
62 virtual
63 ~Output_data();
64
65 // Return the address. For allocated sections, this is only valid
66 // after Layout::finalize is finished.
67 uint64_t
68 address() const
69 {
70 gold_assert(this->is_address_valid_);
71 return this->address_;
72 }
73
74 // Return the size of the data. For allocated sections, this must
75 // be valid after Layout::finalize calls set_address, but need not
76 // be valid before then.
77 off_t
78 data_size() const
79 {
80 gold_assert(this->is_data_size_valid_);
81 return this->data_size_;
82 }
83
84 // Return true if data size is fixed.
85 bool
86 is_data_size_fixed() const
87 { return this->is_data_size_fixed_; }
88
89 // Return the file offset. This is only valid after
90 // Layout::finalize is finished. For some non-allocated sections,
91 // it may not be valid until near the end of the link.
92 off_t
93 offset() const
94 {
95 gold_assert(this->is_offset_valid_);
96 return this->offset_;
97 }
98
99 // Reset the address and file offset. This essentially disables the
100 // sanity testing about duplicate and unknown settings.
101 void
102 reset_address_and_file_offset()
103 {
104 this->is_address_valid_ = false;
105 this->is_offset_valid_ = false;
106 if (!this->is_data_size_fixed_)
107 this->is_data_size_valid_ = false;
108 this->do_reset_address_and_file_offset();
109 }
110
111 // Return true if address and file offset already have reset values. In
112 // other words, calling reset_address_and_file_offset will not change them.
113 bool
114 address_and_file_offset_have_reset_values() const
115 { return this->do_address_and_file_offset_have_reset_values(); }
116
117 // Return the required alignment.
118 uint64_t
119 addralign() const
120 { return this->do_addralign(); }
121
122 // Return whether this has a load address.
123 bool
124 has_load_address() const
125 { return this->do_has_load_address(); }
126
127 // Return the load address.
128 uint64_t
129 load_address() const
130 { return this->do_load_address(); }
131
132 // Return whether this is an Output_section.
133 bool
134 is_section() const
135 { return this->do_is_section(); }
136
137 // Return whether this is an Output_section of the specified type.
138 bool
139 is_section_type(elfcpp::Elf_Word stt) const
140 { return this->do_is_section_type(stt); }
141
142 // Return whether this is an Output_section with the specified flag
143 // set.
144 bool
145 is_section_flag_set(elfcpp::Elf_Xword shf) const
146 { return this->do_is_section_flag_set(shf); }
147
148 // Return the output section that this goes in, if there is one.
149 Output_section*
150 output_section()
151 { return this->do_output_section(); }
152
153 const Output_section*
154 output_section() const
155 { return this->do_output_section(); }
156
157 // Return the output section index, if there is an output section.
158 unsigned int
159 out_shndx() const
160 { return this->do_out_shndx(); }
161
162 // Set the output section index, if this is an output section.
163 void
164 set_out_shndx(unsigned int shndx)
165 { this->do_set_out_shndx(shndx); }
166
167 // Set the address and file offset of this data, and finalize the
168 // size of the data. This is called during Layout::finalize for
169 // allocated sections.
170 void
171 set_address_and_file_offset(uint64_t addr, off_t off)
172 {
173 this->set_address(addr);
174 this->set_file_offset(off);
175 this->finalize_data_size();
176 }
177
178 // Set the address.
179 void
180 set_address(uint64_t addr)
181 {
182 gold_assert(!this->is_address_valid_);
183 this->address_ = addr;
184 this->is_address_valid_ = true;
185 }
186
187 // Set the file offset.
188 void
189 set_file_offset(off_t off)
190 {
191 gold_assert(!this->is_offset_valid_);
192 this->offset_ = off;
193 this->is_offset_valid_ = true;
194 }
195
196 // Finalize the data size.
197 void
198 finalize_data_size()
199 {
200 if (!this->is_data_size_valid_)
201 {
202 // Tell the child class to set the data size.
203 this->set_final_data_size();
204 gold_assert(this->is_data_size_valid_);
205 }
206 }
207
208 // Set the TLS offset. Called only for SHT_TLS sections.
209 void
210 set_tls_offset(uint64_t tls_base)
211 { this->do_set_tls_offset(tls_base); }
212
213 // Return the TLS offset, relative to the base of the TLS segment.
214 // Valid only for SHT_TLS sections.
215 uint64_t
216 tls_offset() const
217 { return this->do_tls_offset(); }
218
219 // Write the data to the output file. This is called after
220 // Layout::finalize is complete.
221 void
222 write(Output_file* file)
223 { this->do_write(file); }
224
225 // This is called by Layout::finalize to note that the sizes of
226 // allocated sections must now be fixed.
227 static void
228 layout_complete()
229 { Output_data::allocated_sizes_are_fixed = true; }
230
231 // Used to check that layout has been done.
232 static bool
233 is_layout_complete()
234 { return Output_data::allocated_sizes_are_fixed; }
235
236 // Note that a dynamic reloc has been applied to this data.
237 void
238 add_dynamic_reloc()
239 { this->has_dynamic_reloc_ = true; }
240
241 // Return whether a dynamic reloc has been applied.
242 bool
243 has_dynamic_reloc() const
244 { return this->has_dynamic_reloc_; }
245
246 // Whether the address is valid.
247 bool
248 is_address_valid() const
249 { return this->is_address_valid_; }
250
251 // Whether the file offset is valid.
252 bool
253 is_offset_valid() const
254 { return this->is_offset_valid_; }
255
256 // Whether the data size is valid.
257 bool
258 is_data_size_valid() const
259 { return this->is_data_size_valid_; }
260
261 // Print information to the map file.
262 void
263 print_to_mapfile(Mapfile* mapfile) const
264 { return this->do_print_to_mapfile(mapfile); }
265
266 protected:
267 // Functions that child classes may or in some cases must implement.
268
269 // Write the data to the output file.
270 virtual void
271 do_write(Output_file*) = 0;
272
273 // Return the required alignment.
274 virtual uint64_t
275 do_addralign() const = 0;
276
277 // Return whether this has a load address.
278 virtual bool
279 do_has_load_address() const
280 { return false; }
281
282 // Return the load address.
283 virtual uint64_t
284 do_load_address() const
285 { gold_unreachable(); }
286
287 // Return whether this is an Output_section.
288 virtual bool
289 do_is_section() const
290 { return false; }
291
292 // Return whether this is an Output_section of the specified type.
293 // This only needs to be implement by Output_section.
294 virtual bool
295 do_is_section_type(elfcpp::Elf_Word) const
296 { return false; }
297
298 // Return whether this is an Output_section with the specific flag
299 // set. This only needs to be implemented by Output_section.
300 virtual bool
301 do_is_section_flag_set(elfcpp::Elf_Xword) const
302 { return false; }
303
304 // Return the output section, if there is one.
305 virtual Output_section*
306 do_output_section()
307 { return NULL; }
308
309 virtual const Output_section*
310 do_output_section() const
311 { return NULL; }
312
313 // Return the output section index, if there is an output section.
314 virtual unsigned int
315 do_out_shndx() const
316 { gold_unreachable(); }
317
318 // Set the output section index, if this is an output section.
319 virtual void
320 do_set_out_shndx(unsigned int)
321 { gold_unreachable(); }
322
323 // This is a hook for derived classes to set the data size. This is
324 // called by finalize_data_size, normally called during
325 // Layout::finalize, when the section address is set.
326 virtual void
327 set_final_data_size()
328 { gold_unreachable(); }
329
330 // A hook for resetting the address and file offset.
331 virtual void
332 do_reset_address_and_file_offset()
333 { }
334
335 // Return true if address and file offset already have reset values. In
336 // other words, calling reset_address_and_file_offset will not change them.
337 // A child class overriding do_reset_address_and_file_offset may need to
338 // also override this.
339 virtual bool
340 do_address_and_file_offset_have_reset_values() const
341 { return !this->is_address_valid_ && !this->is_offset_valid_; }
342
343 // Set the TLS offset. Called only for SHT_TLS sections.
344 virtual void
345 do_set_tls_offset(uint64_t)
346 { gold_unreachable(); }
347
348 // Return the TLS offset, relative to the base of the TLS segment.
349 // Valid only for SHT_TLS sections.
350 virtual uint64_t
351 do_tls_offset() const
352 { gold_unreachable(); }
353
354 // Print to the map file. This only needs to be implemented by
355 // classes which may appear in a PT_LOAD segment.
356 virtual void
357 do_print_to_mapfile(Mapfile*) const
358 { gold_unreachable(); }
359
360 // Functions that child classes may call.
361
362 // Reset the address. The Output_section class needs this when an
363 // SHF_ALLOC input section is added to an output section which was
364 // formerly not SHF_ALLOC.
365 void
366 mark_address_invalid()
367 { this->is_address_valid_ = false; }
368
369 // Set the size of the data.
370 void
371 set_data_size(off_t data_size)
372 {
373 gold_assert(!this->is_data_size_valid_
374 && !this->is_data_size_fixed_);
375 this->data_size_ = data_size;
376 this->is_data_size_valid_ = true;
377 }
378
379 // Fix the data size. Once it is fixed, it cannot be changed
380 // and the data size remains always valid.
381 void
382 fix_data_size()
383 {
384 gold_assert(this->is_data_size_valid_);
385 this->is_data_size_fixed_ = true;
386 }
387
388 // Get the current data size--this is for the convenience of
389 // sections which build up their size over time.
390 off_t
391 current_data_size_for_child() const
392 { return this->data_size_; }
393
394 // Set the current data size--this is for the convenience of
395 // sections which build up their size over time.
396 void
397 set_current_data_size_for_child(off_t data_size)
398 {
399 gold_assert(!this->is_data_size_valid_);
400 this->data_size_ = data_size;
401 }
402
403 // Return default alignment for the target size.
404 static uint64_t
405 default_alignment();
406
407 // Return default alignment for a specified size--32 or 64.
408 static uint64_t
409 default_alignment_for_size(int size);
410
411 private:
412 Output_data(const Output_data&);
413 Output_data& operator=(const Output_data&);
414
415 // This is used for verification, to make sure that we don't try to
416 // change any sizes of allocated sections after we set the section
417 // addresses.
418 static bool allocated_sizes_are_fixed;
419
420 // Memory address in output file.
421 uint64_t address_;
422 // Size of data in output file.
423 off_t data_size_;
424 // File offset of contents in output file.
425 off_t offset_;
426 // Whether address_ is valid.
427 bool is_address_valid_ : 1;
428 // Whether data_size_ is valid.
429 bool is_data_size_valid_ : 1;
430 // Whether offset_ is valid.
431 bool is_offset_valid_ : 1;
432 // Whether data size is fixed.
433 bool is_data_size_fixed_ : 1;
434 // Whether any dynamic relocs have been applied to this section.
435 bool has_dynamic_reloc_ : 1;
436 };
437
438 // Output the section headers.
439
440 class Output_section_headers : public Output_data
441 {
442 public:
443 Output_section_headers(const Layout*,
444 const Layout::Segment_list*,
445 const Layout::Section_list*,
446 const Layout::Section_list*,
447 const Stringpool*,
448 const Output_section*);
449
450 protected:
451 // Write the data to the file.
452 void
453 do_write(Output_file*);
454
455 // Return the required alignment.
456 uint64_t
457 do_addralign() const
458 { return Output_data::default_alignment(); }
459
460 // Write to a map file.
461 void
462 do_print_to_mapfile(Mapfile* mapfile) const
463 { mapfile->print_output_data(this, _("** section headers")); }
464
465 // Set final data size.
466 void
467 set_final_data_size()
468 { this->set_data_size(this->do_size()); }
469
470 private:
471 // Write the data to the file with the right size and endianness.
472 template<int size, bool big_endian>
473 void
474 do_sized_write(Output_file*);
475
476 // Compute data size.
477 off_t
478 do_size() const;
479
480 const Layout* layout_;
481 const Layout::Segment_list* segment_list_;
482 const Layout::Section_list* section_list_;
483 const Layout::Section_list* unattached_section_list_;
484 const Stringpool* secnamepool_;
485 const Output_section* shstrtab_section_;
486 };
487
488 // Output the segment headers.
489
490 class Output_segment_headers : public Output_data
491 {
492 public:
493 Output_segment_headers(const Layout::Segment_list& segment_list);
494
495 protected:
496 // Write the data to the file.
497 void
498 do_write(Output_file*);
499
500 // Return the required alignment.
501 uint64_t
502 do_addralign() const
503 { return Output_data::default_alignment(); }
504
505 // Write to a map file.
506 void
507 do_print_to_mapfile(Mapfile* mapfile) const
508 { mapfile->print_output_data(this, _("** segment headers")); }
509
510 // Set final data size.
511 void
512 set_final_data_size()
513 { this->set_data_size(this->do_size()); }
514
515 private:
516 // Write the data to the file with the right size and endianness.
517 template<int size, bool big_endian>
518 void
519 do_sized_write(Output_file*);
520
521 // Compute the current size.
522 off_t
523 do_size() const;
524
525 const Layout::Segment_list& segment_list_;
526 };
527
528 // Output the ELF file header.
529
530 class Output_file_header : public Output_data
531 {
532 public:
533 Output_file_header(const Target*,
534 const Symbol_table*,
535 const Output_segment_headers*,
536 const char* entry);
537
538 // Add information about the section headers. We lay out the ELF
539 // file header before we create the section headers.
540 void set_section_info(const Output_section_headers*,
541 const Output_section* shstrtab);
542
543 protected:
544 // Write the data to the file.
545 void
546 do_write(Output_file*);
547
548 // Return the required alignment.
549 uint64_t
550 do_addralign() const
551 { return Output_data::default_alignment(); }
552
553 // Write to a map file.
554 void
555 do_print_to_mapfile(Mapfile* mapfile) const
556 { mapfile->print_output_data(this, _("** file header")); }
557
558 // Set final data size.
559 void
560 set_final_data_size(void)
561 { this->set_data_size(this->do_size()); }
562
563 private:
564 // Write the data to the file with the right size and endianness.
565 template<int size, bool big_endian>
566 void
567 do_sized_write(Output_file*);
568
569 // Return the value to use for the entry address.
570 template<int size>
571 typename elfcpp::Elf_types<size>::Elf_Addr
572 entry();
573
574 // Compute the current data size.
575 off_t
576 do_size() const;
577
578 const Target* target_;
579 const Symbol_table* symtab_;
580 const Output_segment_headers* segment_header_;
581 const Output_section_headers* section_header_;
582 const Output_section* shstrtab_;
583 const char* entry_;
584 };
585
586 // Output sections are mainly comprised of input sections. However,
587 // there are cases where we have data to write out which is not in an
588 // input section. Output_section_data is used in such cases. This is
589 // an abstract base class.
590
591 class Output_section_data : public Output_data
592 {
593 public:
594 Output_section_data(off_t data_size, uint64_t addralign,
595 bool is_data_size_fixed)
596 : Output_data(), output_section_(NULL), addralign_(addralign)
597 {
598 this->set_data_size(data_size);
599 if (is_data_size_fixed)
600 this->fix_data_size();
601 }
602
603 Output_section_data(uint64_t addralign)
604 : Output_data(), output_section_(NULL), addralign_(addralign)
605 { }
606
607 // Return the output section.
608 Output_section*
609 output_section()
610 { return this->output_section_; }
611
612 const Output_section*
613 output_section() const
614 { return this->output_section_; }
615
616 // Record the output section.
617 void
618 set_output_section(Output_section* os);
619
620 // Add an input section, for SHF_MERGE sections. This returns true
621 // if the section was handled.
622 bool
623 add_input_section(Relobj* object, unsigned int shndx)
624 { return this->do_add_input_section(object, shndx); }
625
626 // Given an input OBJECT, an input section index SHNDX within that
627 // object, and an OFFSET relative to the start of that input
628 // section, return whether or not the corresponding offset within
629 // the output section is known. If this function returns true, it
630 // sets *POUTPUT to the output offset. The value -1 indicates that
631 // this input offset is being discarded.
632 bool
633 output_offset(const Relobj* object, unsigned int shndx,
634 section_offset_type offset,
635 section_offset_type* poutput) const
636 { return this->do_output_offset(object, shndx, offset, poutput); }
637
638 // Return whether this is the merge section for the input section
639 // SHNDX in OBJECT. This should return true when output_offset
640 // would return true for some values of OFFSET.
641 bool
642 is_merge_section_for(const Relobj* object, unsigned int shndx) const
643 { return this->do_is_merge_section_for(object, shndx); }
644
645 // Write the contents to a buffer. This is used for sections which
646 // require postprocessing, such as compression.
647 void
648 write_to_buffer(unsigned char* buffer)
649 { this->do_write_to_buffer(buffer); }
650
651 // Print merge stats to stderr. This should only be called for
652 // SHF_MERGE sections.
653 void
654 print_merge_stats(const char* section_name)
655 { this->do_print_merge_stats(section_name); }
656
657 protected:
658 // The child class must implement do_write.
659
660 // The child class may implement specific adjustments to the output
661 // section.
662 virtual void
663 do_adjust_output_section(Output_section*)
664 { }
665
666 // May be implemented by child class. Return true if the section
667 // was handled.
668 virtual bool
669 do_add_input_section(Relobj*, unsigned int)
670 { gold_unreachable(); }
671
672 // The child class may implement output_offset.
673 virtual bool
674 do_output_offset(const Relobj*, unsigned int, section_offset_type,
675 section_offset_type*) const
676 { return false; }
677
678 // The child class may implement is_merge_section_for.
679 virtual bool
680 do_is_merge_section_for(const Relobj*, unsigned int) const
681 { return false; }
682
683 // The child class may implement write_to_buffer. Most child
684 // classes can not appear in a compressed section, and they do not
685 // implement this.
686 virtual void
687 do_write_to_buffer(unsigned char*)
688 { gold_unreachable(); }
689
690 // Print merge statistics.
691 virtual void
692 do_print_merge_stats(const char*)
693 { gold_unreachable(); }
694
695 // Return the required alignment.
696 uint64_t
697 do_addralign() const
698 { return this->addralign_; }
699
700 // Return the output section.
701 Output_section*
702 do_output_section()
703 { return this->output_section_; }
704
705 const Output_section*
706 do_output_section() const
707 { return this->output_section_; }
708
709 // Return the section index of the output section.
710 unsigned int
711 do_out_shndx() const;
712
713 // Set the alignment.
714 void
715 set_addralign(uint64_t addralign);
716
717 private:
718 // The output section for this section.
719 Output_section* output_section_;
720 // The required alignment.
721 uint64_t addralign_;
722 };
723
724 // Some Output_section_data classes build up their data step by step,
725 // rather than all at once. This class provides an interface for
726 // them.
727
728 class Output_section_data_build : public Output_section_data
729 {
730 public:
731 Output_section_data_build(uint64_t addralign)
732 : Output_section_data(addralign)
733 { }
734
735 // Get the current data size.
736 off_t
737 current_data_size() const
738 { return this->current_data_size_for_child(); }
739
740 // Set the current data size.
741 void
742 set_current_data_size(off_t data_size)
743 { this->set_current_data_size_for_child(data_size); }
744
745 protected:
746 // Set the final data size.
747 virtual void
748 set_final_data_size()
749 { this->set_data_size(this->current_data_size_for_child()); }
750 };
751
752 // A simple case of Output_data in which we have constant data to
753 // output.
754
755 class Output_data_const : public Output_section_data
756 {
757 public:
758 Output_data_const(const std::string& data, uint64_t addralign)
759 : Output_section_data(data.size(), addralign, true), data_(data)
760 { }
761
762 Output_data_const(const char* p, off_t len, uint64_t addralign)
763 : Output_section_data(len, addralign, true), data_(p, len)
764 { }
765
766 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
767 : Output_section_data(len, addralign, true),
768 data_(reinterpret_cast<const char*>(p), len)
769 { }
770
771 protected:
772 // Write the data to the output file.
773 void
774 do_write(Output_file*);
775
776 // Write the data to a buffer.
777 void
778 do_write_to_buffer(unsigned char* buffer)
779 { memcpy(buffer, this->data_.data(), this->data_.size()); }
780
781 // Write to a map file.
782 void
783 do_print_to_mapfile(Mapfile* mapfile) const
784 { mapfile->print_output_data(this, _("** fill")); }
785
786 private:
787 std::string data_;
788 };
789
790 // Another version of Output_data with constant data, in which the
791 // buffer is allocated by the caller.
792
793 class Output_data_const_buffer : public Output_section_data
794 {
795 public:
796 Output_data_const_buffer(const unsigned char* p, off_t len,
797 uint64_t addralign, const char* map_name)
798 : Output_section_data(len, addralign, true),
799 p_(p), map_name_(map_name)
800 { }
801
802 protected:
803 // Write the data the output file.
804 void
805 do_write(Output_file*);
806
807 // Write the data to a buffer.
808 void
809 do_write_to_buffer(unsigned char* buffer)
810 { memcpy(buffer, this->p_, this->data_size()); }
811
812 // Write to a map file.
813 void
814 do_print_to_mapfile(Mapfile* mapfile) const
815 { mapfile->print_output_data(this, _(this->map_name_)); }
816
817 private:
818 // The data to output.
819 const unsigned char* p_;
820 // Name to use in a map file. Maps are a rarely used feature, but
821 // the space usage is minor as aren't very many of these objects.
822 const char* map_name_;
823 };
824
825 // A place holder for a fixed amount of data written out via some
826 // other mechanism.
827
828 class Output_data_fixed_space : public Output_section_data
829 {
830 public:
831 Output_data_fixed_space(off_t data_size, uint64_t addralign,
832 const char* map_name)
833 : Output_section_data(data_size, addralign, true),
834 map_name_(map_name)
835 { }
836
837 protected:
838 // Write out the data--the actual data must be written out
839 // elsewhere.
840 void
841 do_write(Output_file*)
842 { }
843
844 // Write to a map file.
845 void
846 do_print_to_mapfile(Mapfile* mapfile) const
847 { mapfile->print_output_data(this, _(this->map_name_)); }
848
849 private:
850 // Name to use in a map file. Maps are a rarely used feature, but
851 // the space usage is minor as aren't very many of these objects.
852 const char* map_name_;
853 };
854
855 // A place holder for variable sized data written out via some other
856 // mechanism.
857
858 class Output_data_space : public Output_section_data_build
859 {
860 public:
861 explicit Output_data_space(uint64_t addralign, const char* map_name)
862 : Output_section_data_build(addralign),
863 map_name_(map_name)
864 { }
865
866 // Set the alignment.
867 void
868 set_space_alignment(uint64_t align)
869 { this->set_addralign(align); }
870
871 protected:
872 // Write out the data--the actual data must be written out
873 // elsewhere.
874 void
875 do_write(Output_file*)
876 { }
877
878 // Write to a map file.
879 void
880 do_print_to_mapfile(Mapfile* mapfile) const
881 { mapfile->print_output_data(this, _(this->map_name_)); }
882
883 private:
884 // Name to use in a map file. Maps are a rarely used feature, but
885 // the space usage is minor as aren't very many of these objects.
886 const char* map_name_;
887 };
888
889 // Fill fixed space with zeroes. This is just like
890 // Output_data_fixed_space, except that the map name is known.
891
892 class Output_data_zero_fill : public Output_section_data
893 {
894 public:
895 Output_data_zero_fill(off_t data_size, uint64_t addralign)
896 : Output_section_data(data_size, addralign, true)
897 { }
898
899 protected:
900 // There is no data to write out.
901 void
902 do_write(Output_file*)
903 { }
904
905 // Write to a map file.
906 void
907 do_print_to_mapfile(Mapfile* mapfile) const
908 { mapfile->print_output_data(this, "** zero fill"); }
909 };
910
911 // A string table which goes into an output section.
912
913 class Output_data_strtab : public Output_section_data
914 {
915 public:
916 Output_data_strtab(Stringpool* strtab)
917 : Output_section_data(1), strtab_(strtab)
918 { }
919
920 protected:
921 // This is called to set the address and file offset. Here we make
922 // sure that the Stringpool is finalized.
923 void
924 set_final_data_size();
925
926 // Write out the data.
927 void
928 do_write(Output_file*);
929
930 // Write the data to a buffer.
931 void
932 do_write_to_buffer(unsigned char* buffer)
933 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
934
935 // Write to a map file.
936 void
937 do_print_to_mapfile(Mapfile* mapfile) const
938 { mapfile->print_output_data(this, _("** string table")); }
939
940 private:
941 Stringpool* strtab_;
942 };
943
944 // This POD class is used to represent a single reloc in the output
945 // file. This could be a private class within Output_data_reloc, but
946 // the templatization is complex enough that I broke it out into a
947 // separate class. The class is templatized on either elfcpp::SHT_REL
948 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
949 // relocation or an ordinary relocation.
950
951 // A relocation can be against a global symbol, a local symbol, a
952 // local section symbol, an output section, or the undefined symbol at
953 // index 0. We represent the latter by using a NULL global symbol.
954
955 template<int sh_type, bool dynamic, int size, bool big_endian>
956 class Output_reloc;
957
958 template<bool dynamic, int size, bool big_endian>
959 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
960 {
961 public:
962 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
963 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
964
965 static const Address invalid_address = static_cast<Address>(0) - 1;
966
967 // An uninitialized entry. We need this because we want to put
968 // instances of this class into an STL container.
969 Output_reloc()
970 : local_sym_index_(INVALID_CODE)
971 { }
972
973 // We have a bunch of different constructors. They come in pairs
974 // depending on how the address of the relocation is specified. It
975 // can either be an offset in an Output_data or an offset in an
976 // input section.
977
978 // A reloc against a global symbol.
979
980 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
981 Address address, bool is_relative, bool is_symbolless);
982
983 Output_reloc(Symbol* gsym, unsigned int type,
984 Sized_relobj<size, big_endian>* relobj,
985 unsigned int shndx, Address address, bool is_relative,
986 bool is_symbolless);
987
988 // A reloc against a local symbol or local section symbol.
989
990 Output_reloc(Sized_relobj<size, big_endian>* relobj,
991 unsigned int local_sym_index, unsigned int type,
992 Output_data* od, Address address, bool is_relative,
993 bool is_symbolless, bool is_section_symbol);
994
995 Output_reloc(Sized_relobj<size, big_endian>* relobj,
996 unsigned int local_sym_index, unsigned int type,
997 unsigned int shndx, Address address, bool is_relative,
998 bool is_symbolless, bool is_section_symbol);
999
1000 // A reloc against the STT_SECTION symbol of an output section.
1001
1002 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1003 Address address);
1004
1005 Output_reloc(Output_section* os, unsigned int type,
1006 Sized_relobj<size, big_endian>* relobj,
1007 unsigned int shndx, Address address);
1008
1009 // An absolute relocation with no symbol.
1010
1011 Output_reloc(unsigned int type, Output_data* od, Address address);
1012
1013 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1014 unsigned int shndx, Address address);
1015
1016 // A target specific relocation. The target will be called to get
1017 // the symbol index, passing ARG. The type and offset will be set
1018 // as for other relocation types.
1019
1020 Output_reloc(unsigned int type, void* arg, Output_data* od,
1021 Address address);
1022
1023 Output_reloc(unsigned int type, void* arg,
1024 Sized_relobj<size, big_endian>* relobj,
1025 unsigned int shndx, Address address);
1026
1027 // Return the reloc type.
1028 unsigned int
1029 type() const
1030 { return this->type_; }
1031
1032 // Return whether this is a RELATIVE relocation.
1033 bool
1034 is_relative() const
1035 { return this->is_relative_; }
1036
1037 // Return whether this is a relocation which should not use
1038 // a symbol, but which obtains its addend from a symbol.
1039 bool
1040 is_symbolless() const
1041 { return this->is_symbolless_; }
1042
1043 // Return whether this is against a local section symbol.
1044 bool
1045 is_local_section_symbol() const
1046 {
1047 return (this->local_sym_index_ != GSYM_CODE
1048 && this->local_sym_index_ != SECTION_CODE
1049 && this->local_sym_index_ != INVALID_CODE
1050 && this->local_sym_index_ != TARGET_CODE
1051 && this->is_section_symbol_);
1052 }
1053
1054 // Return whether this is a target specific relocation.
1055 bool
1056 is_target_specific() const
1057 { return this->local_sym_index_ == TARGET_CODE; }
1058
1059 // Return the argument to pass to the target for a target specific
1060 // relocation.
1061 void*
1062 target_arg() const
1063 {
1064 gold_assert(this->local_sym_index_ == TARGET_CODE);
1065 return this->u1_.arg;
1066 }
1067
1068 // For a local section symbol, return the offset of the input
1069 // section within the output section. ADDEND is the addend being
1070 // applied to the input section.
1071 Address
1072 local_section_offset(Addend addend) const;
1073
1074 // Get the value of the symbol referred to by a Rel relocation when
1075 // we are adding the given ADDEND.
1076 Address
1077 symbol_value(Addend addend) const;
1078
1079 // Write the reloc entry to an output view.
1080 void
1081 write(unsigned char* pov) const;
1082
1083 // Write the offset and info fields to Write_rel.
1084 template<typename Write_rel>
1085 void write_rel(Write_rel*) const;
1086
1087 // This is used when sorting dynamic relocs. Return -1 to sort this
1088 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1089 int
1090 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1091 const;
1092
1093 // Return whether this reloc should be sorted before the argument
1094 // when sorting dynamic relocs.
1095 bool
1096 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1097 r2) const
1098 { return this->compare(r2) < 0; }
1099
1100 private:
1101 // Record that we need a dynamic symbol index.
1102 void
1103 set_needs_dynsym_index();
1104
1105 // Return the symbol index.
1106 unsigned int
1107 get_symbol_index() const;
1108
1109 // Return the output address.
1110 Address
1111 get_address() const;
1112
1113 // Codes for local_sym_index_.
1114 enum
1115 {
1116 // Global symbol.
1117 GSYM_CODE = -1U,
1118 // Output section.
1119 SECTION_CODE = -2U,
1120 // Target specific.
1121 TARGET_CODE = -3U,
1122 // Invalid uninitialized entry.
1123 INVALID_CODE = -4U
1124 };
1125
1126 union
1127 {
1128 // For a local symbol or local section symbol
1129 // (this->local_sym_index_ >= 0), the object. We will never
1130 // generate a relocation against a local symbol in a dynamic
1131 // object; that doesn't make sense. And our callers will always
1132 // be templatized, so we use Sized_relobj here.
1133 Sized_relobj<size, big_endian>* relobj;
1134 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1135 // symbol. If this is NULL, it indicates a relocation against the
1136 // undefined 0 symbol.
1137 Symbol* gsym;
1138 // For a relocation against an output section
1139 // (this->local_sym_index_ == SECTION_CODE), the output section.
1140 Output_section* os;
1141 // For a target specific relocation, an argument to pass to the
1142 // target.
1143 void* arg;
1144 } u1_;
1145 union
1146 {
1147 // If this->shndx_ is not INVALID CODE, the object which holds the
1148 // input section being used to specify the reloc address.
1149 Sized_relobj<size, big_endian>* relobj;
1150 // If this->shndx_ is INVALID_CODE, the output data being used to
1151 // specify the reloc address. This may be NULL if the reloc
1152 // address is absolute.
1153 Output_data* od;
1154 } u2_;
1155 // The address offset within the input section or the Output_data.
1156 Address address_;
1157 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1158 // relocation against an output section, or TARGET_CODE for a target
1159 // specific relocation, or INVALID_CODE for an uninitialized value.
1160 // Otherwise, for a local symbol (this->is_section_symbol_ is
1161 // false), the local symbol index. For a local section symbol
1162 // (this->is_section_symbol_ is true), the section index in the
1163 // input file.
1164 unsigned int local_sym_index_;
1165 // The reloc type--a processor specific code.
1166 unsigned int type_ : 29;
1167 // True if the relocation is a RELATIVE relocation.
1168 bool is_relative_ : 1;
1169 // True if the relocation is one which should not use
1170 // a symbol, but which obtains its addend from a symbol.
1171 bool is_symbolless_ : 1;
1172 // True if the relocation is against a section symbol.
1173 bool is_section_symbol_ : 1;
1174 // If the reloc address is an input section in an object, the
1175 // section index. This is INVALID_CODE if the reloc address is
1176 // specified in some other way.
1177 unsigned int shndx_;
1178 };
1179
1180 // The SHT_RELA version of Output_reloc<>. This is just derived from
1181 // the SHT_REL version of Output_reloc, but it adds an addend.
1182
1183 template<bool dynamic, int size, bool big_endian>
1184 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1185 {
1186 public:
1187 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1188 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1189
1190 // An uninitialized entry.
1191 Output_reloc()
1192 : rel_()
1193 { }
1194
1195 // A reloc against a global symbol.
1196
1197 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1198 Address address, Addend addend, bool is_relative,
1199 bool is_symbolless)
1200 : rel_(gsym, type, od, address, is_relative, is_symbolless),
1201 addend_(addend)
1202 { }
1203
1204 Output_reloc(Symbol* gsym, unsigned int type,
1205 Sized_relobj<size, big_endian>* relobj,
1206 unsigned int shndx, Address address, Addend addend,
1207 bool is_relative, bool is_symbolless)
1208 : rel_(gsym, type, relobj, shndx, address, is_relative,
1209 is_symbolless), addend_(addend)
1210 { }
1211
1212 // A reloc against a local symbol.
1213
1214 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1215 unsigned int local_sym_index, unsigned int type,
1216 Output_data* od, Address address,
1217 Addend addend, bool is_relative,
1218 bool is_symbolless, bool is_section_symbol)
1219 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1220 is_symbolless, is_section_symbol),
1221 addend_(addend)
1222 { }
1223
1224 Output_reloc(Sized_relobj<size, big_endian>* relobj,
1225 unsigned int local_sym_index, unsigned int type,
1226 unsigned int shndx, Address address,
1227 Addend addend, bool is_relative,
1228 bool is_symbolless, bool is_section_symbol)
1229 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1230 is_symbolless, is_section_symbol),
1231 addend_(addend)
1232 { }
1233
1234 // A reloc against the STT_SECTION symbol of an output section.
1235
1236 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1237 Address address, Addend addend)
1238 : rel_(os, type, od, address), addend_(addend)
1239 { }
1240
1241 Output_reloc(Output_section* os, unsigned int type,
1242 Sized_relobj<size, big_endian>* relobj,
1243 unsigned int shndx, Address address, Addend addend)
1244 : rel_(os, type, relobj, shndx, address), addend_(addend)
1245 { }
1246
1247 // An absolute relocation with no symbol.
1248
1249 Output_reloc(unsigned int type, Output_data* od, Address address,
1250 Addend addend)
1251 : rel_(type, od, address), addend_(addend)
1252 { }
1253
1254 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1255 unsigned int shndx, Address address, Addend addend)
1256 : rel_(type, relobj, shndx, address), addend_(addend)
1257 { }
1258
1259 // A target specific relocation. The target will be called to get
1260 // the symbol index and the addend, passing ARG. The type and
1261 // offset will be set as for other relocation types.
1262
1263 Output_reloc(unsigned int type, void* arg, Output_data* od,
1264 Address address, Addend addend)
1265 : rel_(type, arg, od, address), addend_(addend)
1266 { }
1267
1268 Output_reloc(unsigned int type, void* arg,
1269 Sized_relobj<size, big_endian>* relobj,
1270 unsigned int shndx, Address address, Addend addend)
1271 : rel_(type, arg, relobj, shndx, address), addend_(addend)
1272 { }
1273
1274 // Return whether this is a RELATIVE relocation.
1275 bool
1276 is_relative() const
1277 { return this->rel_.is_relative(); }
1278
1279 // Return whether this is a relocation which should not use
1280 // a symbol, but which obtains its addend from a symbol.
1281 bool
1282 is_symbolless() const
1283 { return this->rel_.is_symbolless(); }
1284
1285 // Write the reloc entry to an output view.
1286 void
1287 write(unsigned char* pov) const;
1288
1289 // Return whether this reloc should be sorted before the argument
1290 // when sorting dynamic relocs.
1291 bool
1292 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1293 r2) const
1294 {
1295 int i = this->rel_.compare(r2.rel_);
1296 if (i < 0)
1297 return true;
1298 else if (i > 0)
1299 return false;
1300 else
1301 return this->addend_ < r2.addend_;
1302 }
1303
1304 private:
1305 // The basic reloc.
1306 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1307 // The addend.
1308 Addend addend_;
1309 };
1310
1311 // Output_data_reloc_generic is a non-template base class for
1312 // Output_data_reloc_base. This gives the generic code a way to hold
1313 // a pointer to a reloc section.
1314
1315 class Output_data_reloc_generic : public Output_section_data_build
1316 {
1317 public:
1318 Output_data_reloc_generic(int size, bool sort_relocs)
1319 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1320 relative_reloc_count_(0), sort_relocs_(sort_relocs)
1321 { }
1322
1323 // Return the number of relative relocs in this section.
1324 size_t
1325 relative_reloc_count() const
1326 { return this->relative_reloc_count_; }
1327
1328 // Whether we should sort the relocs.
1329 bool
1330 sort_relocs() const
1331 { return this->sort_relocs_; }
1332
1333 protected:
1334 // Note that we've added another relative reloc.
1335 void
1336 bump_relative_reloc_count()
1337 { ++this->relative_reloc_count_; }
1338
1339 private:
1340 // The number of relative relocs added to this section. This is to
1341 // support DT_RELCOUNT.
1342 size_t relative_reloc_count_;
1343 // Whether to sort the relocations when writing them out, to make
1344 // the dynamic linker more efficient.
1345 bool sort_relocs_;
1346 };
1347
1348 // Output_data_reloc is used to manage a section containing relocs.
1349 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1350 // indicates whether this is a dynamic relocation or a normal
1351 // relocation. Output_data_reloc_base is a base class.
1352 // Output_data_reloc is the real class, which we specialize based on
1353 // the reloc type.
1354
1355 template<int sh_type, bool dynamic, int size, bool big_endian>
1356 class Output_data_reloc_base : public Output_data_reloc_generic
1357 {
1358 public:
1359 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1360 typedef typename Output_reloc_type::Address Address;
1361 static const int reloc_size =
1362 Reloc_types<sh_type, size, big_endian>::reloc_size;
1363
1364 // Construct the section.
1365 Output_data_reloc_base(bool sort_relocs)
1366 : Output_data_reloc_generic(size, sort_relocs)
1367 { }
1368
1369 protected:
1370 // Write out the data.
1371 void
1372 do_write(Output_file*);
1373
1374 // Set the entry size and the link.
1375 void
1376 do_adjust_output_section(Output_section* os);
1377
1378 // Write to a map file.
1379 void
1380 do_print_to_mapfile(Mapfile* mapfile) const
1381 {
1382 mapfile->print_output_data(this,
1383 (dynamic
1384 ? _("** dynamic relocs")
1385 : _("** relocs")));
1386 }
1387
1388 // Add a relocation entry.
1389 void
1390 add(Output_data* od, const Output_reloc_type& reloc)
1391 {
1392 this->relocs_.push_back(reloc);
1393 this->set_current_data_size(this->relocs_.size() * reloc_size);
1394 od->add_dynamic_reloc();
1395 if (reloc.is_relative())
1396 this->bump_relative_reloc_count();
1397 }
1398
1399 private:
1400 typedef std::vector<Output_reloc_type> Relocs;
1401
1402 // The class used to sort the relocations.
1403 struct Sort_relocs_comparison
1404 {
1405 bool
1406 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1407 { return r1.sort_before(r2); }
1408 };
1409
1410 // The relocations in this section.
1411 Relocs relocs_;
1412 };
1413
1414 // The class which callers actually create.
1415
1416 template<int sh_type, bool dynamic, int size, bool big_endian>
1417 class Output_data_reloc;
1418
1419 // The SHT_REL version of Output_data_reloc.
1420
1421 template<bool dynamic, int size, bool big_endian>
1422 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1423 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1424 {
1425 private:
1426 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1427 big_endian> Base;
1428
1429 public:
1430 typedef typename Base::Output_reloc_type Output_reloc_type;
1431 typedef typename Output_reloc_type::Address Address;
1432
1433 Output_data_reloc(bool sr)
1434 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1435 { }
1436
1437 // Add a reloc against a global symbol.
1438
1439 void
1440 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1441 { this->add(od, Output_reloc_type(gsym, type, od, address, false, false)); }
1442
1443 void
1444 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1445 Sized_relobj<size, big_endian>* relobj,
1446 unsigned int shndx, Address address)
1447 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1448 false, false)); }
1449
1450 // These are to simplify the Copy_relocs class.
1451
1452 void
1453 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1454 Address addend)
1455 {
1456 gold_assert(addend == 0);
1457 this->add_global(gsym, type, od, address);
1458 }
1459
1460 void
1461 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1462 Sized_relobj<size, big_endian>* relobj,
1463 unsigned int shndx, Address address, Address addend)
1464 {
1465 gold_assert(addend == 0);
1466 this->add_global(gsym, type, od, relobj, shndx, address);
1467 }
1468
1469 // Add a RELATIVE reloc against a global symbol. The final relocation
1470 // will not reference the symbol.
1471
1472 void
1473 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1474 Address address)
1475 { this->add(od, Output_reloc_type(gsym, type, od, address, true, true)); }
1476
1477 void
1478 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1479 Sized_relobj<size, big_endian>* relobj,
1480 unsigned int shndx, Address address)
1481 {
1482 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1483 true, true));
1484 }
1485
1486 // Add a global relocation which does not use a symbol for the relocation,
1487 // but which gets its addend from a symbol.
1488
1489 void
1490 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1491 Output_data* od, Address address)
1492 { this->add(od, Output_reloc_type(gsym, type, od, address, false, true)); }
1493
1494 void
1495 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1496 Output_data* od,
1497 Sized_relobj<size, big_endian>* relobj,
1498 unsigned int shndx, Address address)
1499 {
1500 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1501 false, true));
1502 }
1503
1504 // Add a reloc against a local symbol.
1505
1506 void
1507 add_local(Sized_relobj<size, big_endian>* relobj,
1508 unsigned int local_sym_index, unsigned int type,
1509 Output_data* od, Address address)
1510 {
1511 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1512 address, false, false, false));
1513 }
1514
1515 void
1516 add_local(Sized_relobj<size, big_endian>* relobj,
1517 unsigned int local_sym_index, unsigned int type,
1518 Output_data* od, unsigned int shndx, Address address)
1519 {
1520 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1521 address, false, false, false));
1522 }
1523
1524 // Add a RELATIVE reloc against a local symbol.
1525
1526 void
1527 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1528 unsigned int local_sym_index, unsigned int type,
1529 Output_data* od, Address address)
1530 {
1531 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1532 address, true, true, false));
1533 }
1534
1535 void
1536 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1537 unsigned int local_sym_index, unsigned int type,
1538 Output_data* od, unsigned int shndx, Address address)
1539 {
1540 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1541 address, true, true, false));
1542 }
1543
1544 // Add a local relocation which does not use a symbol for the relocation,
1545 // but which gets its addend from a symbol.
1546
1547 void
1548 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1549 unsigned int local_sym_index, unsigned int type,
1550 Output_data* od, Address address)
1551 {
1552 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1553 address, false, true, false));
1554 }
1555
1556 void
1557 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1558 unsigned int local_sym_index, unsigned int type,
1559 Output_data* od, unsigned int shndx,
1560 Address address)
1561 {
1562 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1563 address, false, true, false));
1564 }
1565
1566 // Add a reloc against a local section symbol. This will be
1567 // converted into a reloc against the STT_SECTION symbol of the
1568 // output section.
1569
1570 void
1571 add_local_section(Sized_relobj<size, big_endian>* relobj,
1572 unsigned int input_shndx, unsigned int type,
1573 Output_data* od, Address address)
1574 {
1575 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1576 address, false, false, true));
1577 }
1578
1579 void
1580 add_local_section(Sized_relobj<size, big_endian>* relobj,
1581 unsigned int input_shndx, unsigned int type,
1582 Output_data* od, unsigned int shndx, Address address)
1583 {
1584 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1585 address, false, false, true));
1586 }
1587
1588 // A reloc against the STT_SECTION symbol of an output section.
1589 // OS is the Output_section that the relocation refers to; OD is
1590 // the Output_data object being relocated.
1591
1592 void
1593 add_output_section(Output_section* os, unsigned int type,
1594 Output_data* od, Address address)
1595 { this->add(od, Output_reloc_type(os, type, od, address)); }
1596
1597 void
1598 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1599 Sized_relobj<size, big_endian>* relobj,
1600 unsigned int shndx, Address address)
1601 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1602
1603 // Add an absolute relocation.
1604
1605 void
1606 add_absolute(unsigned int type, Output_data* od, Address address)
1607 { this->add(od, Output_reloc_type(type, od, address)); }
1608
1609 void
1610 add_absolute(unsigned int type, Output_data* od,
1611 Sized_relobj<size, big_endian>* relobj,
1612 unsigned int shndx, Address address)
1613 { this->add(od, Output_reloc_type(type, relobj, shndx, address)); }
1614
1615 // Add a target specific relocation. A target which calls this must
1616 // define the reloc_symbol_index and reloc_addend virtual functions.
1617
1618 void
1619 add_target_specific(unsigned int type, void* arg, Output_data* od,
1620 Address address)
1621 { this->add(od, Output_reloc_type(type, arg, od, address)); }
1622
1623 void
1624 add_target_specific(unsigned int type, void* arg, Output_data* od,
1625 Sized_relobj<size, big_endian>* relobj,
1626 unsigned int shndx, Address address)
1627 { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1628 };
1629
1630 // The SHT_RELA version of Output_data_reloc.
1631
1632 template<bool dynamic, int size, bool big_endian>
1633 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1634 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1635 {
1636 private:
1637 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1638 big_endian> Base;
1639
1640 public:
1641 typedef typename Base::Output_reloc_type Output_reloc_type;
1642 typedef typename Output_reloc_type::Address Address;
1643 typedef typename Output_reloc_type::Addend Addend;
1644
1645 Output_data_reloc(bool sr)
1646 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1647 { }
1648
1649 // Add a reloc against a global symbol.
1650
1651 void
1652 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1653 Address address, Addend addend)
1654 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1655 false, false)); }
1656
1657 void
1658 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1659 Sized_relobj<size, big_endian>* relobj,
1660 unsigned int shndx, Address address,
1661 Addend addend)
1662 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1663 addend, false, false)); }
1664
1665 // Add a RELATIVE reloc against a global symbol. The final output
1666 // relocation will not reference the symbol, but we must keep the symbol
1667 // information long enough to set the addend of the relocation correctly
1668 // when it is written.
1669
1670 void
1671 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1672 Address address, Addend addend)
1673 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1674 true)); }
1675
1676 void
1677 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1678 Sized_relobj<size, big_endian>* relobj,
1679 unsigned int shndx, Address address, Addend addend)
1680 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1681 addend, true, true)); }
1682
1683 // Add a global relocation which does not use a symbol for the relocation,
1684 // but which gets its addend from a symbol.
1685
1686 void
1687 add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1688 Address address, Addend addend)
1689 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1690 false, true)); }
1691
1692 void
1693 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1694 Output_data* od,
1695 Sized_relobj<size, big_endian>* relobj,
1696 unsigned int shndx, Address address, Addend addend)
1697 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1698 addend, false, true)); }
1699
1700 // Add a reloc against a local symbol.
1701
1702 void
1703 add_local(Sized_relobj<size, big_endian>* relobj,
1704 unsigned int local_sym_index, unsigned int type,
1705 Output_data* od, Address address, Addend addend)
1706 {
1707 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1708 addend, false, false, false));
1709 }
1710
1711 void
1712 add_local(Sized_relobj<size, big_endian>* relobj,
1713 unsigned int local_sym_index, unsigned int type,
1714 Output_data* od, unsigned int shndx, Address address,
1715 Addend addend)
1716 {
1717 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1718 address, addend, false, false, false));
1719 }
1720
1721 // Add a RELATIVE reloc against a local symbol.
1722
1723 void
1724 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1725 unsigned int local_sym_index, unsigned int type,
1726 Output_data* od, Address address, Addend addend)
1727 {
1728 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1729 addend, true, true, false));
1730 }
1731
1732 void
1733 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1734 unsigned int local_sym_index, unsigned int type,
1735 Output_data* od, unsigned int shndx, Address address,
1736 Addend addend)
1737 {
1738 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1739 address, addend, true, true, false));
1740 }
1741
1742 // Add a local relocation which does not use a symbol for the relocation,
1743 // but which gets it's addend from a symbol.
1744
1745 void
1746 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1747 unsigned int local_sym_index, unsigned int type,
1748 Output_data* od, Address address, Addend addend)
1749 {
1750 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1751 addend, false, true, false));
1752 }
1753
1754 void
1755 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1756 unsigned int local_sym_index, unsigned int type,
1757 Output_data* od, unsigned int shndx,
1758 Address address, Addend addend)
1759 {
1760 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1761 address, addend, false, true, false));
1762 }
1763
1764 // Add a reloc against a local section symbol. This will be
1765 // converted into a reloc against the STT_SECTION symbol of the
1766 // output section.
1767
1768 void
1769 add_local_section(Sized_relobj<size, big_endian>* relobj,
1770 unsigned int input_shndx, unsigned int type,
1771 Output_data* od, Address address, Addend addend)
1772 {
1773 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1774 addend, false, false, true));
1775 }
1776
1777 void
1778 add_local_section(Sized_relobj<size, big_endian>* relobj,
1779 unsigned int input_shndx, unsigned int type,
1780 Output_data* od, unsigned int shndx, Address address,
1781 Addend addend)
1782 {
1783 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1784 address, addend, false, false, true));
1785 }
1786
1787 // A reloc against the STT_SECTION symbol of an output section.
1788
1789 void
1790 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1791 Address address, Addend addend)
1792 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1793
1794 void
1795 add_output_section(Output_section* os, unsigned int type,
1796 Sized_relobj<size, big_endian>* relobj,
1797 unsigned int shndx, Address address, Addend addend)
1798 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1799 addend)); }
1800
1801 // Add an absolute relocation.
1802
1803 void
1804 add_absolute(unsigned int type, Output_data* od, Address address,
1805 Addend addend)
1806 { this->add(od, Output_reloc_type(type, od, address, addend)); }
1807
1808 void
1809 add_absolute(unsigned int type, Output_data* od,
1810 Sized_relobj<size, big_endian>* relobj,
1811 unsigned int shndx, Address address, Addend addend)
1812 { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); }
1813
1814 // Add a target specific relocation. A target which calls this must
1815 // define the reloc_symbol_index and reloc_addend virtual functions.
1816
1817 void
1818 add_target_specific(unsigned int type, void* arg, Output_data* od,
1819 Address address, Addend addend)
1820 { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
1821
1822 void
1823 add_target_specific(unsigned int type, void* arg, Output_data* od,
1824 Sized_relobj<size, big_endian>* relobj,
1825 unsigned int shndx, Address address, Addend addend)
1826 {
1827 this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
1828 addend));
1829 }
1830 };
1831
1832 // Output_relocatable_relocs represents a relocation section in a
1833 // relocatable link. The actual data is written out in the target
1834 // hook relocate_for_relocatable. This just saves space for it.
1835
1836 template<int sh_type, int size, bool big_endian>
1837 class Output_relocatable_relocs : public Output_section_data
1838 {
1839 public:
1840 Output_relocatable_relocs(Relocatable_relocs* rr)
1841 : Output_section_data(Output_data::default_alignment_for_size(size)),
1842 rr_(rr)
1843 { }
1844
1845 void
1846 set_final_data_size();
1847
1848 // Write out the data. There is nothing to do here.
1849 void
1850 do_write(Output_file*)
1851 { }
1852
1853 // Write to a map file.
1854 void
1855 do_print_to_mapfile(Mapfile* mapfile) const
1856 { mapfile->print_output_data(this, _("** relocs")); }
1857
1858 private:
1859 // The relocs associated with this input section.
1860 Relocatable_relocs* rr_;
1861 };
1862
1863 // Handle a GROUP section.
1864
1865 template<int size, bool big_endian>
1866 class Output_data_group : public Output_section_data
1867 {
1868 public:
1869 // The constructor clears *INPUT_SHNDXES.
1870 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1871 section_size_type entry_count,
1872 elfcpp::Elf_Word flags,
1873 std::vector<unsigned int>* input_shndxes);
1874
1875 void
1876 do_write(Output_file*);
1877
1878 // Write to a map file.
1879 void
1880 do_print_to_mapfile(Mapfile* mapfile) const
1881 { mapfile->print_output_data(this, _("** group")); }
1882
1883 // Set final data size.
1884 void
1885 set_final_data_size()
1886 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1887
1888 private:
1889 // The input object.
1890 Sized_relobj<size, big_endian>* relobj_;
1891 // The group flag word.
1892 elfcpp::Elf_Word flags_;
1893 // The section indexes of the input sections in this group.
1894 std::vector<unsigned int> input_shndxes_;
1895 };
1896
1897 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1898 // for one symbol--either a global symbol or a local symbol in an
1899 // object. The target specific code adds entries to the GOT as
1900 // needed.
1901
1902 template<int size, bool big_endian>
1903 class Output_data_got : public Output_section_data_build
1904 {
1905 public:
1906 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1907 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1908 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1909
1910 Output_data_got()
1911 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1912 entries_()
1913 { }
1914
1915 // Add an entry for a global symbol to the GOT. Return true if this
1916 // is a new GOT entry, false if the symbol was already in the GOT.
1917 bool
1918 add_global(Symbol* gsym, unsigned int got_type);
1919
1920 // Like add_global, but use the PLT offset of the global symbol if
1921 // it has one.
1922 bool
1923 add_global_plt(Symbol* gsym, unsigned int got_type);
1924
1925 // Add an entry for a global symbol to the GOT, and add a dynamic
1926 // relocation of type R_TYPE for the GOT entry.
1927 void
1928 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1929 Rel_dyn* rel_dyn, unsigned int r_type);
1930
1931 void
1932 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1933 Rela_dyn* rela_dyn, unsigned int r_type);
1934
1935 // Add a pair of entries for a global symbol to the GOT, and add
1936 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1937 void
1938 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1939 Rel_dyn* rel_dyn, unsigned int r_type_1,
1940 unsigned int r_type_2);
1941
1942 void
1943 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1944 Rela_dyn* rela_dyn, unsigned int r_type_1,
1945 unsigned int r_type_2);
1946
1947 // Add an entry for a local symbol to the GOT. This returns true if
1948 // this is a new GOT entry, false if the symbol already has a GOT
1949 // entry.
1950 bool
1951 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1952 unsigned int got_type);
1953
1954 // Like add_local, but use the PLT offset of the local symbol if it
1955 // has one.
1956 bool
1957 add_local_plt(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1958 unsigned int got_type);
1959
1960 // Add an entry for a local symbol to the GOT, and add a dynamic
1961 // relocation of type R_TYPE for the GOT entry.
1962 void
1963 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1964 unsigned int sym_index, unsigned int got_type,
1965 Rel_dyn* rel_dyn, unsigned int r_type);
1966
1967 void
1968 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1969 unsigned int sym_index, unsigned int got_type,
1970 Rela_dyn* rela_dyn, unsigned int r_type);
1971
1972 // Add a pair of entries for a local symbol to the GOT, and add
1973 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1974 void
1975 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1976 unsigned int sym_index, unsigned int shndx,
1977 unsigned int got_type, Rel_dyn* rel_dyn,
1978 unsigned int r_type_1, unsigned int r_type_2);
1979
1980 void
1981 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1982 unsigned int sym_index, unsigned int shndx,
1983 unsigned int got_type, Rela_dyn* rela_dyn,
1984 unsigned int r_type_1, unsigned int r_type_2);
1985
1986 // Add a constant to the GOT. This returns the offset of the new
1987 // entry from the start of the GOT.
1988 unsigned int
1989 add_constant(Valtype constant)
1990 {
1991 this->entries_.push_back(Got_entry(constant));
1992 this->set_got_size();
1993 return this->last_got_offset();
1994 }
1995
1996 protected:
1997 // Write out the GOT table.
1998 void
1999 do_write(Output_file*);
2000
2001 // Write to a map file.
2002 void
2003 do_print_to_mapfile(Mapfile* mapfile) const
2004 { mapfile->print_output_data(this, _("** GOT")); }
2005
2006 private:
2007 // This POD class holds a single GOT entry.
2008 class Got_entry
2009 {
2010 public:
2011 // Create a zero entry.
2012 Got_entry()
2013 : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
2014 { this->u_.constant = 0; }
2015
2016 // Create a global symbol entry.
2017 Got_entry(Symbol* gsym, bool use_plt_offset)
2018 : local_sym_index_(GSYM_CODE), use_plt_offset_(use_plt_offset)
2019 { this->u_.gsym = gsym; }
2020
2021 // Create a local symbol entry.
2022 Got_entry(Sized_relobj<size, big_endian>* object,
2023 unsigned int local_sym_index, bool use_plt_offset)
2024 : local_sym_index_(local_sym_index), use_plt_offset_(use_plt_offset)
2025 {
2026 gold_assert(local_sym_index != GSYM_CODE
2027 && local_sym_index != CONSTANT_CODE
2028 && local_sym_index == this->local_sym_index_);
2029 this->u_.object = object;
2030 }
2031
2032 // Create a constant entry. The constant is a host value--it will
2033 // be swapped, if necessary, when it is written out.
2034 explicit Got_entry(Valtype constant)
2035 : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
2036 { this->u_.constant = constant; }
2037
2038 // Write the GOT entry to an output view.
2039 void
2040 write(unsigned char* pov) const;
2041
2042 private:
2043 enum
2044 {
2045 GSYM_CODE = 0x7fffffff,
2046 CONSTANT_CODE = 0x7ffffffe
2047 };
2048
2049 union
2050 {
2051 // For a local symbol, the object.
2052 Sized_relobj<size, big_endian>* object;
2053 // For a global symbol, the symbol.
2054 Symbol* gsym;
2055 // For a constant, the constant.
2056 Valtype constant;
2057 } u_;
2058 // For a local symbol, the local symbol index. This is GSYM_CODE
2059 // for a global symbol, or CONSTANT_CODE for a constant.
2060 unsigned int local_sym_index_ : 31;
2061 // Whether to use the PLT offset of the symbol if it has one.
2062 bool use_plt_offset_ : 1;
2063 };
2064
2065 typedef std::vector<Got_entry> Got_entries;
2066
2067 // Return the offset into the GOT of GOT entry I.
2068 unsigned int
2069 got_offset(unsigned int i) const
2070 { return i * (size / 8); }
2071
2072 // Return the offset into the GOT of the last entry added.
2073 unsigned int
2074 last_got_offset() const
2075 { return this->got_offset(this->entries_.size() - 1); }
2076
2077 // Set the size of the section.
2078 void
2079 set_got_size()
2080 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
2081
2082 // The list of GOT entries.
2083 Got_entries entries_;
2084 };
2085
2086 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2087 // section.
2088
2089 class Output_data_dynamic : public Output_section_data
2090 {
2091 public:
2092 Output_data_dynamic(Stringpool* pool)
2093 : Output_section_data(Output_data::default_alignment()),
2094 entries_(), pool_(pool)
2095 { }
2096
2097 // Add a new dynamic entry with a fixed numeric value.
2098 void
2099 add_constant(elfcpp::DT tag, unsigned int val)
2100 { this->add_entry(Dynamic_entry(tag, val)); }
2101
2102 // Add a new dynamic entry with the address of output data.
2103 void
2104 add_section_address(elfcpp::DT tag, const Output_data* od)
2105 { this->add_entry(Dynamic_entry(tag, od, false)); }
2106
2107 // Add a new dynamic entry with the address of output data
2108 // plus a constant offset.
2109 void
2110 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2111 unsigned int offset)
2112 { this->add_entry(Dynamic_entry(tag, od, offset)); }
2113
2114 // Add a new dynamic entry with the size of output data.
2115 void
2116 add_section_size(elfcpp::DT tag, const Output_data* od)
2117 { this->add_entry(Dynamic_entry(tag, od, true)); }
2118
2119 // Add a new dynamic entry with the total size of two output datas.
2120 void
2121 add_section_size(elfcpp::DT tag, const Output_data* od,
2122 const Output_data* od2)
2123 { this->add_entry(Dynamic_entry(tag, od, od2)); }
2124
2125 // Add a new dynamic entry with the address of a symbol.
2126 void
2127 add_symbol(elfcpp::DT tag, const Symbol* sym)
2128 { this->add_entry(Dynamic_entry(tag, sym)); }
2129
2130 // Add a new dynamic entry with a string.
2131 void
2132 add_string(elfcpp::DT tag, const char* str)
2133 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2134
2135 void
2136 add_string(elfcpp::DT tag, const std::string& str)
2137 { this->add_string(tag, str.c_str()); }
2138
2139 protected:
2140 // Adjust the output section to set the entry size.
2141 void
2142 do_adjust_output_section(Output_section*);
2143
2144 // Set the final data size.
2145 void
2146 set_final_data_size();
2147
2148 // Write out the dynamic entries.
2149 void
2150 do_write(Output_file*);
2151
2152 // Write to a map file.
2153 void
2154 do_print_to_mapfile(Mapfile* mapfile) const
2155 { mapfile->print_output_data(this, _("** dynamic")); }
2156
2157 private:
2158 // This POD class holds a single dynamic entry.
2159 class Dynamic_entry
2160 {
2161 public:
2162 // Create an entry with a fixed numeric value.
2163 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2164 : tag_(tag), offset_(DYNAMIC_NUMBER)
2165 { this->u_.val = val; }
2166
2167 // Create an entry with the size or address of a section.
2168 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2169 : tag_(tag),
2170 offset_(section_size
2171 ? DYNAMIC_SECTION_SIZE
2172 : DYNAMIC_SECTION_ADDRESS)
2173 {
2174 this->u_.od = od;
2175 this->od2 = NULL;
2176 }
2177
2178 // Create an entry with the size of two sections.
2179 Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2180 : tag_(tag),
2181 offset_(DYNAMIC_SECTION_SIZE)
2182 {
2183 this->u_.od = od;
2184 this->od2 = od2;
2185 }
2186
2187 // Create an entry with the address of a section plus a constant offset.
2188 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2189 : tag_(tag),
2190 offset_(offset)
2191 { this->u_.od = od; }
2192
2193 // Create an entry with the address of a symbol.
2194 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2195 : tag_(tag), offset_(DYNAMIC_SYMBOL)
2196 { this->u_.sym = sym; }
2197
2198 // Create an entry with a string.
2199 Dynamic_entry(elfcpp::DT tag, const char* str)
2200 : tag_(tag), offset_(DYNAMIC_STRING)
2201 { this->u_.str = str; }
2202
2203 // Return the tag of this entry.
2204 elfcpp::DT
2205 tag() const
2206 { return this->tag_; }
2207
2208 // Write the dynamic entry to an output view.
2209 template<int size, bool big_endian>
2210 void
2211 write(unsigned char* pov, const Stringpool*) const;
2212
2213 private:
2214 // Classification is encoded in the OFFSET field.
2215 enum Classification
2216 {
2217 // Section address.
2218 DYNAMIC_SECTION_ADDRESS = 0,
2219 // Number.
2220 DYNAMIC_NUMBER = -1U,
2221 // Section size.
2222 DYNAMIC_SECTION_SIZE = -2U,
2223 // Symbol adress.
2224 DYNAMIC_SYMBOL = -3U,
2225 // String.
2226 DYNAMIC_STRING = -4U
2227 // Any other value indicates a section address plus OFFSET.
2228 };
2229
2230 union
2231 {
2232 // For DYNAMIC_NUMBER.
2233 unsigned int val;
2234 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2235 const Output_data* od;
2236 // For DYNAMIC_SYMBOL.
2237 const Symbol* sym;
2238 // For DYNAMIC_STRING.
2239 const char* str;
2240 } u_;
2241 // For DYNAMIC_SYMBOL with two sections.
2242 const Output_data* od2;
2243 // The dynamic tag.
2244 elfcpp::DT tag_;
2245 // The type of entry (Classification) or offset within a section.
2246 unsigned int offset_;
2247 };
2248
2249 // Add an entry to the list.
2250 void
2251 add_entry(const Dynamic_entry& entry)
2252 { this->entries_.push_back(entry); }
2253
2254 // Sized version of write function.
2255 template<int size, bool big_endian>
2256 void
2257 sized_write(Output_file* of);
2258
2259 // The type of the list of entries.
2260 typedef std::vector<Dynamic_entry> Dynamic_entries;
2261
2262 // The entries.
2263 Dynamic_entries entries_;
2264 // The pool used for strings.
2265 Stringpool* pool_;
2266 };
2267
2268 // Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2269 // which may be required if the object file has more than
2270 // SHN_LORESERVE sections.
2271
2272 class Output_symtab_xindex : public Output_section_data
2273 {
2274 public:
2275 Output_symtab_xindex(size_t symcount)
2276 : Output_section_data(symcount * 4, 4, true),
2277 entries_()
2278 { }
2279
2280 // Add an entry: symbol number SYMNDX has section SHNDX.
2281 void
2282 add(unsigned int symndx, unsigned int shndx)
2283 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2284
2285 protected:
2286 void
2287 do_write(Output_file*);
2288
2289 // Write to a map file.
2290 void
2291 do_print_to_mapfile(Mapfile* mapfile) const
2292 { mapfile->print_output_data(this, _("** symtab xindex")); }
2293
2294 private:
2295 template<bool big_endian>
2296 void
2297 endian_do_write(unsigned char*);
2298
2299 // It is likely that most symbols will not require entries. Rather
2300 // than keep a vector for all symbols, we keep pairs of symbol index
2301 // and section index.
2302 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2303
2304 // The entries we need.
2305 Xindex_entries entries_;
2306 };
2307
2308 // A relaxed input section.
2309 class Output_relaxed_input_section : public Output_section_data_build
2310 {
2311 public:
2312 // We would like to call relobj->section_addralign(shndx) to get the
2313 // alignment but we do not want the constructor to fail. So callers
2314 // are repsonsible for ensuring that.
2315 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2316 uint64_t addralign)
2317 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2318 { }
2319
2320 // Return the Relobj of this relaxed input section.
2321 Relobj*
2322 relobj() const
2323 { return this->relobj_; }
2324
2325 // Return the section index of this relaxed input section.
2326 unsigned int
2327 shndx() const
2328 { return this->shndx_; }
2329
2330 private:
2331 Relobj* relobj_;
2332 unsigned int shndx_;
2333 };
2334
2335 // This class describes properties of merge data sections. It is used
2336 // as a key type for maps.
2337 class Merge_section_properties
2338 {
2339 public:
2340 Merge_section_properties(bool is_string, uint64_t entsize,
2341 uint64_t addralign)
2342 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2343 { }
2344
2345 // Whether this equals to another Merge_section_properties MSP.
2346 bool
2347 eq(const Merge_section_properties& msp) const
2348 {
2349 return ((this->is_string_ == msp.is_string_)
2350 && (this->entsize_ == msp.entsize_)
2351 && (this->addralign_ == msp.addralign_));
2352 }
2353
2354 // Compute a hash value for this using 64-bit FNV-1a hash.
2355 size_t
2356 hash_value() const
2357 {
2358 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
2359 uint64_t prime = 1099511628211ULL;
2360 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2361 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2362 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2363 return h;
2364 }
2365
2366 // Functors for associative containers.
2367 struct equal_to
2368 {
2369 bool
2370 operator()(const Merge_section_properties& msp1,
2371 const Merge_section_properties& msp2) const
2372 { return msp1.eq(msp2); }
2373 };
2374
2375 struct hash
2376 {
2377 size_t
2378 operator()(const Merge_section_properties& msp) const
2379 { return msp.hash_value(); }
2380 };
2381
2382 private:
2383 // Whether this merge data section is for strings.
2384 bool is_string_;
2385 // Entsize of this merge data section.
2386 uint64_t entsize_;
2387 // Address alignment.
2388 uint64_t addralign_;
2389 };
2390
2391 // This class is used to speed up look up of special input sections in an
2392 // Output_section.
2393
2394 class Output_section_lookup_maps
2395 {
2396 public:
2397 Output_section_lookup_maps()
2398 : is_valid_(true), merge_sections_by_properties_(),
2399 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2400 { }
2401
2402 // Whether the maps are valid.
2403 bool
2404 is_valid() const
2405 { return this->is_valid_; }
2406
2407 // Invalidate the maps.
2408 void
2409 invalidate()
2410 { this->is_valid_ = false; }
2411
2412 // Clear the maps.
2413 void
2414 clear()
2415 {
2416 this->merge_sections_by_properties_.clear();
2417 this->merge_sections_by_id_.clear();
2418 this->relaxed_input_sections_by_id_.clear();
2419 // A cleared map is valid.
2420 this->is_valid_ = true;
2421 }
2422
2423 // Find a merge section by merge section properties. Return NULL if none
2424 // is found.
2425 Output_merge_base*
2426 find_merge_section(const Merge_section_properties& msp) const
2427 {
2428 gold_assert(this->is_valid_);
2429 Merge_sections_by_properties::const_iterator p =
2430 this->merge_sections_by_properties_.find(msp);
2431 return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2432 }
2433
2434 // Find a merge section by section ID of a merge input section. Return NULL
2435 // if none is found.
2436 Output_merge_base*
2437 find_merge_section(const Object* object, unsigned int shndx) const
2438 {
2439 gold_assert(this->is_valid_);
2440 Merge_sections_by_id::const_iterator p =
2441 this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2442 return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2443 }
2444
2445 // Add a merge section pointed by POMB with properties MSP.
2446 void
2447 add_merge_section(const Merge_section_properties& msp,
2448 Output_merge_base* pomb)
2449 {
2450 std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2451 std::pair<Merge_sections_by_properties::iterator, bool> result =
2452 this->merge_sections_by_properties_.insert(value);
2453 gold_assert(result.second);
2454 }
2455
2456 // Add a mapping from a merged input section in OBJECT with index SHNDX
2457 // to a merge output section pointed by POMB.
2458 void
2459 add_merge_input_section(const Object* object, unsigned int shndx,
2460 Output_merge_base* pomb)
2461 {
2462 Const_section_id csid(object, shndx);
2463 std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2464 std::pair<Merge_sections_by_id::iterator, bool> result =
2465 this->merge_sections_by_id_.insert(value);
2466 gold_assert(result.second);
2467 }
2468
2469 // Find a relaxed input section of OBJECT with index SHNDX.
2470 Output_relaxed_input_section*
2471 find_relaxed_input_section(const Object* object, unsigned int shndx) const
2472 {
2473 gold_assert(this->is_valid_);
2474 Relaxed_input_sections_by_id::const_iterator p =
2475 this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2476 return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2477 }
2478
2479 // Add a relaxed input section pointed by POMB and whose original input
2480 // section is in OBJECT with index SHNDX.
2481 void
2482 add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2483 Output_relaxed_input_section* poris)
2484 {
2485 Const_section_id csid(relobj, shndx);
2486 std::pair<Const_section_id, Output_relaxed_input_section*>
2487 value(csid, poris);
2488 std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2489 this->relaxed_input_sections_by_id_.insert(value);
2490 gold_assert(result.second);
2491 }
2492
2493 private:
2494 typedef Unordered_map<Const_section_id, Output_merge_base*,
2495 Const_section_id_hash>
2496 Merge_sections_by_id;
2497
2498 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2499 Merge_section_properties::hash,
2500 Merge_section_properties::equal_to>
2501 Merge_sections_by_properties;
2502
2503 typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2504 Const_section_id_hash>
2505 Relaxed_input_sections_by_id;
2506
2507 // Whether this is valid
2508 bool is_valid_;
2509 // Merge sections by merge section properties.
2510 Merge_sections_by_properties merge_sections_by_properties_;
2511 // Merge sections by section IDs.
2512 Merge_sections_by_id merge_sections_by_id_;
2513 // Relaxed sections by section IDs.
2514 Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2515 };
2516
2517 // An output section. We don't expect to have too many output
2518 // sections, so we don't bother to do a template on the size.
2519
2520 class Output_section : public Output_data
2521 {
2522 public:
2523 // Create an output section, giving the name, type, and flags.
2524 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2525 virtual ~Output_section();
2526
2527 // Add a new input section SHNDX, named NAME, with header SHDR, from
2528 // object OBJECT. RELOC_SHNDX is the index of a relocation section
2529 // which applies to this section, or 0 if none, or -1 if more than
2530 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2531 // in a linker script; in that case we need to keep track of input
2532 // sections associated with an output section. Return the offset
2533 // within the output section.
2534 template<int size, bool big_endian>
2535 off_t
2536 add_input_section(Layout* layout, Sized_relobj<size, big_endian>* object,
2537 unsigned int shndx, const char* name,
2538 const elfcpp::Shdr<size, big_endian>& shdr,
2539 unsigned int reloc_shndx, bool have_sections_script);
2540
2541 // Add generated data POSD to this output section.
2542 void
2543 add_output_section_data(Output_section_data* posd);
2544
2545 // Add a relaxed input section PORIS to this output section.
2546 void
2547 add_relaxed_input_section(Output_relaxed_input_section* poris);
2548
2549 // Return the section name.
2550 const char*
2551 name() const
2552 { return this->name_; }
2553
2554 // Return the section type.
2555 elfcpp::Elf_Word
2556 type() const
2557 { return this->type_; }
2558
2559 // Return the section flags.
2560 elfcpp::Elf_Xword
2561 flags() const
2562 { return this->flags_; }
2563
2564 // Update the output section flags based on input section flags.
2565 void
2566 update_flags_for_input_section(elfcpp::Elf_Xword flags);
2567
2568 // Return the entsize field.
2569 uint64_t
2570 entsize() const
2571 { return this->entsize_; }
2572
2573 // Set the entsize field.
2574 void
2575 set_entsize(uint64_t v);
2576
2577 // Set the load address.
2578 void
2579 set_load_address(uint64_t load_address)
2580 {
2581 this->load_address_ = load_address;
2582 this->has_load_address_ = true;
2583 }
2584
2585 // Set the link field to the output section index of a section.
2586 void
2587 set_link_section(const Output_data* od)
2588 {
2589 gold_assert(this->link_ == 0
2590 && !this->should_link_to_symtab_
2591 && !this->should_link_to_dynsym_);
2592 this->link_section_ = od;
2593 }
2594
2595 // Set the link field to a constant.
2596 void
2597 set_link(unsigned int v)
2598 {
2599 gold_assert(this->link_section_ == NULL
2600 && !this->should_link_to_symtab_
2601 && !this->should_link_to_dynsym_);
2602 this->link_ = v;
2603 }
2604
2605 // Record that this section should link to the normal symbol table.
2606 void
2607 set_should_link_to_symtab()
2608 {
2609 gold_assert(this->link_section_ == NULL
2610 && this->link_ == 0
2611 && !this->should_link_to_dynsym_);
2612 this->should_link_to_symtab_ = true;
2613 }
2614
2615 // Record that this section should link to the dynamic symbol table.
2616 void
2617 set_should_link_to_dynsym()
2618 {
2619 gold_assert(this->link_section_ == NULL
2620 && this->link_ == 0
2621 && !this->should_link_to_symtab_);
2622 this->should_link_to_dynsym_ = true;
2623 }
2624
2625 // Return the info field.
2626 unsigned int
2627 info() const
2628 {
2629 gold_assert(this->info_section_ == NULL
2630 && this->info_symndx_ == NULL);
2631 return this->info_;
2632 }
2633
2634 // Set the info field to the output section index of a section.
2635 void
2636 set_info_section(const Output_section* os)
2637 {
2638 gold_assert((this->info_section_ == NULL
2639 || (this->info_section_ == os
2640 && this->info_uses_section_index_))
2641 && this->info_symndx_ == NULL
2642 && this->info_ == 0);
2643 this->info_section_ = os;
2644 this->info_uses_section_index_= true;
2645 }
2646
2647 // Set the info field to the symbol table index of a symbol.
2648 void
2649 set_info_symndx(const Symbol* sym)
2650 {
2651 gold_assert(this->info_section_ == NULL
2652 && (this->info_symndx_ == NULL
2653 || this->info_symndx_ == sym)
2654 && this->info_ == 0);
2655 this->info_symndx_ = sym;
2656 }
2657
2658 // Set the info field to the symbol table index of a section symbol.
2659 void
2660 set_info_section_symndx(const Output_section* os)
2661 {
2662 gold_assert((this->info_section_ == NULL
2663 || (this->info_section_ == os
2664 && !this->info_uses_section_index_))
2665 && this->info_symndx_ == NULL
2666 && this->info_ == 0);
2667 this->info_section_ = os;
2668 this->info_uses_section_index_ = false;
2669 }
2670
2671 // Set the info field to a constant.
2672 void
2673 set_info(unsigned int v)
2674 {
2675 gold_assert(this->info_section_ == NULL
2676 && this->info_symndx_ == NULL
2677 && (this->info_ == 0
2678 || this->info_ == v));
2679 this->info_ = v;
2680 }
2681
2682 // Set the addralign field.
2683 void
2684 set_addralign(uint64_t v)
2685 { this->addralign_ = v; }
2686
2687 // Whether the output section index has been set.
2688 bool
2689 has_out_shndx() const
2690 { return this->out_shndx_ != -1U; }
2691
2692 // Indicate that we need a symtab index.
2693 void
2694 set_needs_symtab_index()
2695 { this->needs_symtab_index_ = true; }
2696
2697 // Return whether we need a symtab index.
2698 bool
2699 needs_symtab_index() const
2700 { return this->needs_symtab_index_; }
2701
2702 // Get the symtab index.
2703 unsigned int
2704 symtab_index() const
2705 {
2706 gold_assert(this->symtab_index_ != 0);
2707 return this->symtab_index_;
2708 }
2709
2710 // Set the symtab index.
2711 void
2712 set_symtab_index(unsigned int index)
2713 {
2714 gold_assert(index != 0);
2715 this->symtab_index_ = index;
2716 }
2717
2718 // Indicate that we need a dynsym index.
2719 void
2720 set_needs_dynsym_index()
2721 { this->needs_dynsym_index_ = true; }
2722
2723 // Return whether we need a dynsym index.
2724 bool
2725 needs_dynsym_index() const
2726 { return this->needs_dynsym_index_; }
2727
2728 // Get the dynsym index.
2729 unsigned int
2730 dynsym_index() const
2731 {
2732 gold_assert(this->dynsym_index_ != 0);
2733 return this->dynsym_index_;
2734 }
2735
2736 // Set the dynsym index.
2737 void
2738 set_dynsym_index(unsigned int index)
2739 {
2740 gold_assert(index != 0);
2741 this->dynsym_index_ = index;
2742 }
2743
2744 // Return whether the input sections sections attachd to this output
2745 // section may require sorting. This is used to handle constructor
2746 // priorities compatibly with GNU ld.
2747 bool
2748 may_sort_attached_input_sections() const
2749 { return this->may_sort_attached_input_sections_; }
2750
2751 // Record that the input sections attached to this output section
2752 // may require sorting.
2753 void
2754 set_may_sort_attached_input_sections()
2755 { this->may_sort_attached_input_sections_ = true; }
2756
2757 // Returns true if input sections must be sorted according to the
2758 // order in which their name appear in the --section-ordering-file.
2759 bool
2760 input_section_order_specified()
2761 { return this->input_section_order_specified_; }
2762
2763 // Record that input sections must be sorted as some of their names
2764 // match the patterns specified through --section-ordering-file.
2765 void
2766 set_input_section_order_specified()
2767 { this->input_section_order_specified_ = true; }
2768
2769 // Return whether the input sections attached to this output section
2770 // require sorting. This is used to handle constructor priorities
2771 // compatibly with GNU ld.
2772 bool
2773 must_sort_attached_input_sections() const
2774 { return this->must_sort_attached_input_sections_; }
2775
2776 // Record that the input sections attached to this output section
2777 // require sorting.
2778 void
2779 set_must_sort_attached_input_sections()
2780 { this->must_sort_attached_input_sections_ = true; }
2781
2782 // Get the order in which this section appears in the PT_LOAD output
2783 // segment.
2784 Output_section_order
2785 order() const
2786 { return this->order_; }
2787
2788 // Set the order for this section.
2789 void
2790 set_order(Output_section_order order)
2791 { this->order_ = order; }
2792
2793 // Return whether this section holds relro data--data which has
2794 // dynamic relocations but which may be marked read-only after the
2795 // dynamic relocations have been completed.
2796 bool
2797 is_relro() const
2798 { return this->is_relro_; }
2799
2800 // Record that this section holds relro data.
2801 void
2802 set_is_relro()
2803 { this->is_relro_ = true; }
2804
2805 // Record that this section does not hold relro data.
2806 void
2807 clear_is_relro()
2808 { this->is_relro_ = false; }
2809
2810 // True if this is a small section: a section which holds small
2811 // variables.
2812 bool
2813 is_small_section() const
2814 { return this->is_small_section_; }
2815
2816 // Record that this is a small section.
2817 void
2818 set_is_small_section()
2819 { this->is_small_section_ = true; }
2820
2821 // True if this is a large section: a section which holds large
2822 // variables.
2823 bool
2824 is_large_section() const
2825 { return this->is_large_section_; }
2826
2827 // Record that this is a large section.
2828 void
2829 set_is_large_section()
2830 { this->is_large_section_ = true; }
2831
2832 // True if this is a large data (not BSS) section.
2833 bool
2834 is_large_data_section()
2835 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2836
2837 // Return whether this section should be written after all the input
2838 // sections are complete.
2839 bool
2840 after_input_sections() const
2841 { return this->after_input_sections_; }
2842
2843 // Record that this section should be written after all the input
2844 // sections are complete.
2845 void
2846 set_after_input_sections()
2847 { this->after_input_sections_ = true; }
2848
2849 // Return whether this section requires postprocessing after all
2850 // relocations have been applied.
2851 bool
2852 requires_postprocessing() const
2853 { return this->requires_postprocessing_; }
2854
2855 // If a section requires postprocessing, return the buffer to use.
2856 unsigned char*
2857 postprocessing_buffer() const
2858 {
2859 gold_assert(this->postprocessing_buffer_ != NULL);
2860 return this->postprocessing_buffer_;
2861 }
2862
2863 // If a section requires postprocessing, create the buffer to use.
2864 void
2865 create_postprocessing_buffer();
2866
2867 // If a section requires postprocessing, this is the size of the
2868 // buffer to which relocations should be applied.
2869 off_t
2870 postprocessing_buffer_size() const
2871 { return this->current_data_size_for_child(); }
2872
2873 // Modify the section name. This is only permitted for an
2874 // unallocated section, and only before the size has been finalized.
2875 // Otherwise the name will not get into Layout::namepool_.
2876 void
2877 set_name(const char* newname)
2878 {
2879 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2880 gold_assert(!this->is_data_size_valid());
2881 this->name_ = newname;
2882 }
2883
2884 // Return whether the offset OFFSET in the input section SHNDX in
2885 // object OBJECT is being included in the link.
2886 bool
2887 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2888 off_t offset) const;
2889
2890 // Return the offset within the output section of OFFSET relative to
2891 // the start of input section SHNDX in object OBJECT.
2892 section_offset_type
2893 output_offset(const Relobj* object, unsigned int shndx,
2894 section_offset_type offset) const;
2895
2896 // Return the output virtual address of OFFSET relative to the start
2897 // of input section SHNDX in object OBJECT.
2898 uint64_t
2899 output_address(const Relobj* object, unsigned int shndx,
2900 off_t offset) const;
2901
2902 // Look for the merged section for input section SHNDX in object
2903 // OBJECT. If found, return true, and set *ADDR to the address of
2904 // the start of the merged section. This is not necessary the
2905 // output offset corresponding to input offset 0 in the section,
2906 // since the section may be mapped arbitrarily.
2907 bool
2908 find_starting_output_address(const Relobj* object, unsigned int shndx,
2909 uint64_t* addr) const;
2910
2911 // Record that this output section was found in the SECTIONS clause
2912 // of a linker script.
2913 void
2914 set_found_in_sections_clause()
2915 { this->found_in_sections_clause_ = true; }
2916
2917 // Return whether this output section was found in the SECTIONS
2918 // clause of a linker script.
2919 bool
2920 found_in_sections_clause() const
2921 { return this->found_in_sections_clause_; }
2922
2923 // Write the section header into *OPHDR.
2924 template<int size, bool big_endian>
2925 void
2926 write_header(const Layout*, const Stringpool*,
2927 elfcpp::Shdr_write<size, big_endian>*) const;
2928
2929 // The next few calls are for linker script support.
2930
2931 // In some cases we need to keep a list of the input sections
2932 // associated with this output section. We only need the list if we
2933 // might have to change the offsets of the input section within the
2934 // output section after we add the input section. The ordinary
2935 // input sections will be written out when we process the object
2936 // file, and as such we don't need to track them here. We do need
2937 // to track Output_section_data objects here. We store instances of
2938 // this structure in a std::vector, so it must be a POD. There can
2939 // be many instances of this structure, so we use a union to save
2940 // some space.
2941 class Input_section
2942 {
2943 public:
2944 Input_section()
2945 : shndx_(0), p2align_(0)
2946 {
2947 this->u1_.data_size = 0;
2948 this->u2_.object = NULL;
2949 }
2950
2951 // For an ordinary input section.
2952 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2953 uint64_t addralign)
2954 : shndx_(shndx),
2955 p2align_(ffsll(static_cast<long long>(addralign))),
2956 section_order_index_(0)
2957 {
2958 gold_assert(shndx != OUTPUT_SECTION_CODE
2959 && shndx != MERGE_DATA_SECTION_CODE
2960 && shndx != MERGE_STRING_SECTION_CODE
2961 && shndx != RELAXED_INPUT_SECTION_CODE);
2962 this->u1_.data_size = data_size;
2963 this->u2_.object = object;
2964 }
2965
2966 // For a non-merge output section.
2967 Input_section(Output_section_data* posd)
2968 : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
2969 section_order_index_(0)
2970 {
2971 this->u1_.data_size = 0;
2972 this->u2_.posd = posd;
2973 }
2974
2975 // For a merge section.
2976 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2977 : shndx_(is_string
2978 ? MERGE_STRING_SECTION_CODE
2979 : MERGE_DATA_SECTION_CODE),
2980 p2align_(0),
2981 section_order_index_(0)
2982 {
2983 this->u1_.entsize = entsize;
2984 this->u2_.posd = posd;
2985 }
2986
2987 // For a relaxed input section.
2988 Input_section(Output_relaxed_input_section* psection)
2989 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
2990 section_order_index_(0)
2991 {
2992 this->u1_.data_size = 0;
2993 this->u2_.poris = psection;
2994 }
2995
2996 unsigned int
2997 section_order_index() const
2998 {
2999 return this->section_order_index_;
3000 }
3001
3002 void
3003 set_section_order_index(unsigned int number)
3004 {
3005 this->section_order_index_ = number;
3006 }
3007
3008 // The required alignment.
3009 uint64_t
3010 addralign() const
3011 {
3012 if (this->p2align_ != 0)
3013 return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3014 else if (!this->is_input_section())
3015 return this->u2_.posd->addralign();
3016 else
3017 return 0;
3018 }
3019
3020 // Set the required alignment, which must be either 0 or a power of 2.
3021 // For input sections that are sub-classes of Output_section_data, a
3022 // alignment of zero means asking the underlying object for alignment.
3023 void
3024 set_addralign(uint64_t addralign)
3025 {
3026 if (addralign == 0)
3027 this->p2align_ = 0;
3028 else
3029 {
3030 gold_assert((addralign & (addralign - 1)) == 0);
3031 this->p2align_ = ffsll(static_cast<long long>(addralign));
3032 }
3033 }
3034
3035 // Return the required size.
3036 off_t
3037 data_size() const;
3038
3039 // Whether this is an input section.
3040 bool
3041 is_input_section() const
3042 {
3043 return (this->shndx_ != OUTPUT_SECTION_CODE
3044 && this->shndx_ != MERGE_DATA_SECTION_CODE
3045 && this->shndx_ != MERGE_STRING_SECTION_CODE
3046 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3047 }
3048
3049 // Return whether this is a merge section which matches the
3050 // parameters.
3051 bool
3052 is_merge_section(bool is_string, uint64_t entsize,
3053 uint64_t addralign) const
3054 {
3055 return (this->shndx_ == (is_string
3056 ? MERGE_STRING_SECTION_CODE
3057 : MERGE_DATA_SECTION_CODE)
3058 && this->u1_.entsize == entsize
3059 && this->addralign() == addralign);
3060 }
3061
3062 // Return whether this is a merge section for some input section.
3063 bool
3064 is_merge_section() const
3065 {
3066 return (this->shndx_ == MERGE_DATA_SECTION_CODE
3067 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3068 }
3069
3070 // Return whether this is a relaxed input section.
3071 bool
3072 is_relaxed_input_section() const
3073 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3074
3075 // Return whether this is a generic Output_section_data.
3076 bool
3077 is_output_section_data() const
3078 {
3079 return this->shndx_ == OUTPUT_SECTION_CODE;
3080 }
3081
3082 // Return the object for an input section.
3083 Relobj*
3084 relobj() const;
3085
3086 // Return the input section index for an input section.
3087 unsigned int
3088 shndx() const;
3089
3090 // For non-input-sections, return the associated Output_section_data
3091 // object.
3092 Output_section_data*
3093 output_section_data() const
3094 {
3095 gold_assert(!this->is_input_section());
3096 return this->u2_.posd;
3097 }
3098
3099 // For a merge section, return the Output_merge_base pointer.
3100 Output_merge_base*
3101 output_merge_base() const
3102 {
3103 gold_assert(this->is_merge_section());
3104 return this->u2_.pomb;
3105 }
3106
3107 // Return the Output_relaxed_input_section object.
3108 Output_relaxed_input_section*
3109 relaxed_input_section() const
3110 {
3111 gold_assert(this->is_relaxed_input_section());
3112 return this->u2_.poris;
3113 }
3114
3115 // Set the output section.
3116 void
3117 set_output_section(Output_section* os)
3118 {
3119 gold_assert(!this->is_input_section());
3120 Output_section_data* posd =
3121 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3122 posd->set_output_section(os);
3123 }
3124
3125 // Set the address and file offset. This is called during
3126 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3127 // the enclosing section.
3128 void
3129 set_address_and_file_offset(uint64_t address, off_t file_offset,
3130 off_t section_file_offset);
3131
3132 // Reset the address and file offset.
3133 void
3134 reset_address_and_file_offset();
3135
3136 // Finalize the data size.
3137 void
3138 finalize_data_size();
3139
3140 // Add an input section, for SHF_MERGE sections.
3141 bool
3142 add_input_section(Relobj* object, unsigned int shndx)
3143 {
3144 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3145 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3146 return this->u2_.posd->add_input_section(object, shndx);
3147 }
3148
3149 // Given an input OBJECT, an input section index SHNDX within that
3150 // object, and an OFFSET relative to the start of that input
3151 // section, return whether or not the output offset is known. If
3152 // this function returns true, it sets *POUTPUT to the offset in
3153 // the output section, relative to the start of the input section
3154 // in the output section. *POUTPUT may be different from OFFSET
3155 // for a merged section.
3156 bool
3157 output_offset(const Relobj* object, unsigned int shndx,
3158 section_offset_type offset,
3159 section_offset_type* poutput) const;
3160
3161 // Return whether this is the merge section for the input section
3162 // SHNDX in OBJECT.
3163 bool
3164 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3165
3166 // Write out the data. This does nothing for an input section.
3167 void
3168 write(Output_file*);
3169
3170 // Write the data to a buffer. This does nothing for an input
3171 // section.
3172 void
3173 write_to_buffer(unsigned char*);
3174
3175 // Print to a map file.
3176 void
3177 print_to_mapfile(Mapfile*) const;
3178
3179 // Print statistics about merge sections to stderr.
3180 void
3181 print_merge_stats(const char* section_name)
3182 {
3183 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3184 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3185 this->u2_.posd->print_merge_stats(section_name);
3186 }
3187
3188 private:
3189 // Code values which appear in shndx_. If the value is not one of
3190 // these codes, it is the input section index in the object file.
3191 enum
3192 {
3193 // An Output_section_data.
3194 OUTPUT_SECTION_CODE = -1U,
3195 // An Output_section_data for an SHF_MERGE section with
3196 // SHF_STRINGS not set.
3197 MERGE_DATA_SECTION_CODE = -2U,
3198 // An Output_section_data for an SHF_MERGE section with
3199 // SHF_STRINGS set.
3200 MERGE_STRING_SECTION_CODE = -3U,
3201 // An Output_section_data for a relaxed input section.
3202 RELAXED_INPUT_SECTION_CODE = -4U
3203 };
3204
3205 // For an ordinary input section, this is the section index in the
3206 // input file. For an Output_section_data, this is
3207 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3208 // MERGE_STRING_SECTION_CODE.
3209 unsigned int shndx_;
3210 // The required alignment, stored as a power of 2.
3211 unsigned int p2align_;
3212 union
3213 {
3214 // For an ordinary input section, the section size.
3215 off_t data_size;
3216 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3217 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3218 // entity size.
3219 uint64_t entsize;
3220 } u1_;
3221 union
3222 {
3223 // For an ordinary input section, the object which holds the
3224 // input section.
3225 Relobj* object;
3226 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3227 // MERGE_STRING_SECTION_CODE, the data.
3228 Output_section_data* posd;
3229 Output_merge_base* pomb;
3230 // For RELAXED_INPUT_SECTION_CODE, the data.
3231 Output_relaxed_input_section* poris;
3232 } u2_;
3233 // The line number of the pattern it matches in the --section-ordering-file
3234 // file. It is 0 if does not match any pattern.
3235 unsigned int section_order_index_;
3236 };
3237
3238 // Store the list of input sections for this Output_section into the
3239 // list passed in. This removes the input sections, leaving only
3240 // any Output_section_data elements. This returns the size of those
3241 // Output_section_data elements. ADDRESS is the address of this
3242 // output section. FILL is the fill value to use, in case there are
3243 // any spaces between the remaining Output_section_data elements.
3244 uint64_t
3245 get_input_sections(uint64_t address, const std::string& fill,
3246 std::list<Input_section>*);
3247
3248 // Add a script input section. A script input section can either be
3249 // a plain input section or a sub-class of Output_section_data.
3250 void
3251 add_script_input_section(const Input_section& input_section);
3252
3253 // Set the current size of the output section.
3254 void
3255 set_current_data_size(off_t size)
3256 { this->set_current_data_size_for_child(size); }
3257
3258 // Get the current size of the output section.
3259 off_t
3260 current_data_size() const
3261 { return this->current_data_size_for_child(); }
3262
3263 // End of linker script support.
3264
3265 // Save states before doing section layout.
3266 // This is used for relaxation.
3267 void
3268 save_states();
3269
3270 // Restore states prior to section layout.
3271 void
3272 restore_states();
3273
3274 // Discard states.
3275 void
3276 discard_states();
3277
3278 // Convert existing input sections to relaxed input sections.
3279 void
3280 convert_input_sections_to_relaxed_sections(
3281 const std::vector<Output_relaxed_input_section*>& sections);
3282
3283 // Find a relaxed input section to an input section in OBJECT
3284 // with index SHNDX. Return NULL if none is found.
3285 const Output_relaxed_input_section*
3286 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3287
3288 // Whether section offsets need adjustment due to relaxation.
3289 bool
3290 section_offsets_need_adjustment() const
3291 { return this->section_offsets_need_adjustment_; }
3292
3293 // Set section_offsets_need_adjustment to be true.
3294 void
3295 set_section_offsets_need_adjustment()
3296 { this->section_offsets_need_adjustment_ = true; }
3297
3298 // Adjust section offsets of input sections in this. This is
3299 // requires if relaxation caused some input sections to change sizes.
3300 void
3301 adjust_section_offsets();
3302
3303 // Whether this is a NOLOAD section.
3304 bool
3305 is_noload() const
3306 { return this->is_noload_; }
3307
3308 // Set NOLOAD flag.
3309 void
3310 set_is_noload()
3311 { this->is_noload_ = true; }
3312
3313 // Print merge statistics to stderr.
3314 void
3315 print_merge_stats();
3316
3317 protected:
3318 // Return the output section--i.e., the object itself.
3319 Output_section*
3320 do_output_section()
3321 { return this; }
3322
3323 const Output_section*
3324 do_output_section() const
3325 { return this; }
3326
3327 // Return the section index in the output file.
3328 unsigned int
3329 do_out_shndx() const
3330 {
3331 gold_assert(this->out_shndx_ != -1U);
3332 return this->out_shndx_;
3333 }
3334
3335 // Set the output section index.
3336 void
3337 do_set_out_shndx(unsigned int shndx)
3338 {
3339 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3340 this->out_shndx_ = shndx;
3341 }
3342
3343 // Set the final data size of the Output_section. For a typical
3344 // Output_section, there is nothing to do, but if there are any
3345 // Output_section_data objects we need to set their final addresses
3346 // here.
3347 virtual void
3348 set_final_data_size();
3349
3350 // Reset the address and file offset.
3351 void
3352 do_reset_address_and_file_offset();
3353
3354 // Return true if address and file offset already have reset values. In
3355 // other words, calling reset_address_and_file_offset will not change them.
3356 bool
3357 do_address_and_file_offset_have_reset_values() const;
3358
3359 // Write the data to the file. For a typical Output_section, this
3360 // does nothing: the data is written out by calling Object::Relocate
3361 // on each input object. But if there are any Output_section_data
3362 // objects we do need to write them out here.
3363 virtual void
3364 do_write(Output_file*);
3365
3366 // Return the address alignment--function required by parent class.
3367 uint64_t
3368 do_addralign() const
3369 { return this->addralign_; }
3370
3371 // Return whether there is a load address.
3372 bool
3373 do_has_load_address() const
3374 { return this->has_load_address_; }
3375
3376 // Return the load address.
3377 uint64_t
3378 do_load_address() const
3379 {
3380 gold_assert(this->has_load_address_);
3381 return this->load_address_;
3382 }
3383
3384 // Return whether this is an Output_section.
3385 bool
3386 do_is_section() const
3387 { return true; }
3388
3389 // Return whether this is a section of the specified type.
3390 bool
3391 do_is_section_type(elfcpp::Elf_Word type) const
3392 { return this->type_ == type; }
3393
3394 // Return whether the specified section flag is set.
3395 bool
3396 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
3397 { return (this->flags_ & flag) != 0; }
3398
3399 // Set the TLS offset. Called only for SHT_TLS sections.
3400 void
3401 do_set_tls_offset(uint64_t tls_base);
3402
3403 // Return the TLS offset, relative to the base of the TLS segment.
3404 // Valid only for SHT_TLS sections.
3405 uint64_t
3406 do_tls_offset() const
3407 { return this->tls_offset_; }
3408
3409 // This may be implemented by a child class.
3410 virtual void
3411 do_finalize_name(Layout*)
3412 { }
3413
3414 // Print to the map file.
3415 virtual void
3416 do_print_to_mapfile(Mapfile*) const;
3417
3418 // Record that this section requires postprocessing after all
3419 // relocations have been applied. This is called by a child class.
3420 void
3421 set_requires_postprocessing()
3422 {
3423 this->requires_postprocessing_ = true;
3424 this->after_input_sections_ = true;
3425 }
3426
3427 // Write all the data of an Output_section into the postprocessing
3428 // buffer.
3429 void
3430 write_to_postprocessing_buffer();
3431
3432 typedef std::vector<Input_section> Input_section_list;
3433
3434 // Allow a child class to access the input sections.
3435 const Input_section_list&
3436 input_sections() const
3437 { return this->input_sections_; }
3438
3439 // Whether this always keeps an input section list
3440 bool
3441 always_keeps_input_sections() const
3442 { return this->always_keeps_input_sections_; }
3443
3444 // Always keep an input section list.
3445 void
3446 set_always_keeps_input_sections()
3447 {
3448 gold_assert(this->current_data_size_for_child() == 0);
3449 this->always_keeps_input_sections_ = true;
3450 }
3451
3452 private:
3453 // We only save enough information to undo the effects of section layout.
3454 class Checkpoint_output_section
3455 {
3456 public:
3457 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3458 const Input_section_list& input_sections,
3459 off_t first_input_offset,
3460 bool attached_input_sections_are_sorted)
3461 : addralign_(addralign), flags_(flags),
3462 input_sections_(input_sections),
3463 input_sections_size_(input_sections_.size()),
3464 input_sections_copy_(), first_input_offset_(first_input_offset),
3465 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3466 { }
3467
3468 virtual
3469 ~Checkpoint_output_section()
3470 { }
3471
3472 // Return the address alignment.
3473 uint64_t
3474 addralign() const
3475 { return this->addralign_; }
3476
3477 // Return the section flags.
3478 elfcpp::Elf_Xword
3479 flags() const
3480 { return this->flags_; }
3481
3482 // Return a reference to the input section list copy.
3483 Input_section_list*
3484 input_sections()
3485 { return &this->input_sections_copy_; }
3486
3487 // Return the size of input_sections at the time when checkpoint is
3488 // taken.
3489 size_t
3490 input_sections_size() const
3491 { return this->input_sections_size_; }
3492
3493 // Whether input sections are copied.
3494 bool
3495 input_sections_saved() const
3496 { return this->input_sections_copy_.size() == this->input_sections_size_; }
3497
3498 off_t
3499 first_input_offset() const
3500 { return this->first_input_offset_; }
3501
3502 bool
3503 attached_input_sections_are_sorted() const
3504 { return this->attached_input_sections_are_sorted_; }
3505
3506 // Save input sections.
3507 void
3508 save_input_sections()
3509 {
3510 this->input_sections_copy_.reserve(this->input_sections_size_);
3511 this->input_sections_copy_.clear();
3512 Input_section_list::const_iterator p = this->input_sections_.begin();
3513 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3514 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3515 this->input_sections_copy_.push_back(*p);
3516 }
3517
3518 private:
3519 // The section alignment.
3520 uint64_t addralign_;
3521 // The section flags.
3522 elfcpp::Elf_Xword flags_;
3523 // Reference to the input sections to be checkpointed.
3524 const Input_section_list& input_sections_;
3525 // Size of the checkpointed portion of input_sections_;
3526 size_t input_sections_size_;
3527 // Copy of input sections.
3528 Input_section_list input_sections_copy_;
3529 // The offset of the first entry in input_sections_.
3530 off_t first_input_offset_;
3531 // True if the input sections attached to this output section have
3532 // already been sorted.
3533 bool attached_input_sections_are_sorted_;
3534 };
3535
3536 // This class is used to sort the input sections.
3537 class Input_section_sort_entry;
3538
3539 // This is the sort comparison function for ctors and dtors.
3540 struct Input_section_sort_compare
3541 {
3542 bool
3543 operator()(const Input_section_sort_entry&,
3544 const Input_section_sort_entry&) const;
3545 };
3546
3547 // This is the sort comparison function for .init_array and .fini_array.
3548 struct Input_section_sort_init_fini_compare
3549 {
3550 bool
3551 operator()(const Input_section_sort_entry&,
3552 const Input_section_sort_entry&) const;
3553 };
3554
3555 // This is the sort comparison function when a section order is specified
3556 // from an input file.
3557 struct Input_section_sort_section_order_index_compare
3558 {
3559 bool
3560 operator()(const Input_section_sort_entry&,
3561 const Input_section_sort_entry&) const;
3562 };
3563
3564 // Fill data. This is used to fill in data between input sections.
3565 // It is also used for data statements (BYTE, WORD, etc.) in linker
3566 // scripts. When we have to keep track of the input sections, we
3567 // can use an Output_data_const, but we don't want to have to keep
3568 // track of input sections just to implement fills.
3569 class Fill
3570 {
3571 public:
3572 Fill(off_t section_offset, off_t length)
3573 : section_offset_(section_offset),
3574 length_(convert_to_section_size_type(length))
3575 { }
3576
3577 // Return section offset.
3578 off_t
3579 section_offset() const
3580 { return this->section_offset_; }
3581
3582 // Return fill length.
3583 section_size_type
3584 length() const
3585 { return this->length_; }
3586
3587 private:
3588 // The offset within the output section.
3589 off_t section_offset_;
3590 // The length of the space to fill.
3591 section_size_type length_;
3592 };
3593
3594 typedef std::vector<Fill> Fill_list;
3595
3596 // Map used during relaxation of existing sections. This map
3597 // a section id an input section list index. We assume that
3598 // Input_section_list is a vector.
3599 typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
3600
3601 // Add a new output section by Input_section.
3602 void
3603 add_output_section_data(Input_section*);
3604
3605 // Add an SHF_MERGE input section. Returns true if the section was
3606 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
3607 // stores information about the merged input sections.
3608 bool
3609 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3610 uint64_t entsize, uint64_t addralign,
3611 bool keeps_input_sections);
3612
3613 // Add an output SHF_MERGE section POSD to this output section.
3614 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3615 // ENTSIZE is the entity size. This returns the entry added to
3616 // input_sections_.
3617 void
3618 add_output_merge_section(Output_section_data* posd, bool is_string,
3619 uint64_t entsize);
3620
3621 // Sort the attached input sections.
3622 void
3623 sort_attached_input_sections();
3624
3625 // Find the merge section into which an input section with index SHNDX in
3626 // OBJECT has been added. Return NULL if none found.
3627 Output_section_data*
3628 find_merge_section(const Relobj* object, unsigned int shndx) const;
3629
3630 // Build a relaxation map.
3631 void
3632 build_relaxation_map(
3633 const Input_section_list& input_sections,
3634 size_t limit,
3635 Relaxation_map* map) const;
3636
3637 // Convert input sections in an input section list into relaxed sections.
3638 void
3639 convert_input_sections_in_list_to_relaxed_sections(
3640 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3641 const Relaxation_map& map,
3642 Input_section_list* input_sections);
3643
3644 // Build the lookup maps for merge and relaxed input sections.
3645 void
3646 build_lookup_maps() const;
3647
3648 // Most of these fields are only valid after layout.
3649
3650 // The name of the section. This will point into a Stringpool.
3651 const char* name_;
3652 // The section address is in the parent class.
3653 // The section alignment.
3654 uint64_t addralign_;
3655 // The section entry size.
3656 uint64_t entsize_;
3657 // The load address. This is only used when using a linker script
3658 // with a SECTIONS clause. The has_load_address_ field indicates
3659 // whether this field is valid.
3660 uint64_t load_address_;
3661 // The file offset is in the parent class.
3662 // Set the section link field to the index of this section.
3663 const Output_data* link_section_;
3664 // If link_section_ is NULL, this is the link field.
3665 unsigned int link_;
3666 // Set the section info field to the index of this section.
3667 const Output_section* info_section_;
3668 // If info_section_ is NULL, set the info field to the symbol table
3669 // index of this symbol.
3670 const Symbol* info_symndx_;
3671 // If info_section_ and info_symndx_ are NULL, this is the section
3672 // info field.
3673 unsigned int info_;
3674 // The section type.
3675 const elfcpp::Elf_Word type_;
3676 // The section flags.
3677 elfcpp::Elf_Xword flags_;
3678 // The order of this section in the output segment.
3679 Output_section_order order_;
3680 // The section index.
3681 unsigned int out_shndx_;
3682 // If there is a STT_SECTION for this output section in the normal
3683 // symbol table, this is the symbol index. This starts out as zero.
3684 // It is initialized in Layout::finalize() to be the index, or -1U
3685 // if there isn't one.
3686 unsigned int symtab_index_;
3687 // If there is a STT_SECTION for this output section in the dynamic
3688 // symbol table, this is the symbol index. This starts out as zero.
3689 // It is initialized in Layout::finalize() to be the index, or -1U
3690 // if there isn't one.
3691 unsigned int dynsym_index_;
3692 // The input sections. This will be empty in cases where we don't
3693 // need to keep track of them.
3694 Input_section_list input_sections_;
3695 // The offset of the first entry in input_sections_.
3696 off_t first_input_offset_;
3697 // The fill data. This is separate from input_sections_ because we
3698 // often will need fill sections without needing to keep track of
3699 // input sections.
3700 Fill_list fills_;
3701 // If the section requires postprocessing, this buffer holds the
3702 // section contents during relocation.
3703 unsigned char* postprocessing_buffer_;
3704 // Whether this output section needs a STT_SECTION symbol in the
3705 // normal symbol table. This will be true if there is a relocation
3706 // which needs it.
3707 bool needs_symtab_index_ : 1;
3708 // Whether this output section needs a STT_SECTION symbol in the
3709 // dynamic symbol table. This will be true if there is a dynamic
3710 // relocation which needs it.
3711 bool needs_dynsym_index_ : 1;
3712 // Whether the link field of this output section should point to the
3713 // normal symbol table.
3714 bool should_link_to_symtab_ : 1;
3715 // Whether the link field of this output section should point to the
3716 // dynamic symbol table.
3717 bool should_link_to_dynsym_ : 1;
3718 // Whether this section should be written after all the input
3719 // sections are complete.
3720 bool after_input_sections_ : 1;
3721 // Whether this section requires post processing after all
3722 // relocations have been applied.
3723 bool requires_postprocessing_ : 1;
3724 // Whether an input section was mapped to this output section
3725 // because of a SECTIONS clause in a linker script.
3726 bool found_in_sections_clause_ : 1;
3727 // Whether this section has an explicitly specified load address.
3728 bool has_load_address_ : 1;
3729 // True if the info_section_ field means the section index of the
3730 // section, false if it means the symbol index of the corresponding
3731 // section symbol.
3732 bool info_uses_section_index_ : 1;
3733 // True if input sections attached to this output section have to be
3734 // sorted according to a specified order.
3735 bool input_section_order_specified_ : 1;
3736 // True if the input sections attached to this output section may
3737 // need sorting.
3738 bool may_sort_attached_input_sections_ : 1;
3739 // True if the input sections attached to this output section must
3740 // be sorted.
3741 bool must_sort_attached_input_sections_ : 1;
3742 // True if the input sections attached to this output section have
3743 // already been sorted.
3744 bool attached_input_sections_are_sorted_ : 1;
3745 // True if this section holds relro data.
3746 bool is_relro_ : 1;
3747 // True if this is a small section.
3748 bool is_small_section_ : 1;
3749 // True if this is a large section.
3750 bool is_large_section_ : 1;
3751 // Whether code-fills are generated at write.
3752 bool generate_code_fills_at_write_ : 1;
3753 // Whether the entry size field should be zero.
3754 bool is_entsize_zero_ : 1;
3755 // Whether section offsets need adjustment due to relaxation.
3756 bool section_offsets_need_adjustment_ : 1;
3757 // Whether this is a NOLOAD section.
3758 bool is_noload_ : 1;
3759 // Whether this always keeps input section.
3760 bool always_keeps_input_sections_ : 1;
3761 // For SHT_TLS sections, the offset of this section relative to the base
3762 // of the TLS segment.
3763 uint64_t tls_offset_;
3764 // Saved checkpoint.
3765 Checkpoint_output_section* checkpoint_;
3766 // Fast lookup maps for merged and relaxed input sections.
3767 Output_section_lookup_maps* lookup_maps_;
3768 };
3769
3770 // An output segment. PT_LOAD segments are built from collections of
3771 // output sections. Other segments typically point within PT_LOAD
3772 // segments, and are built directly as needed.
3773 //
3774 // NOTE: We want to use the copy constructor for this class. During
3775 // relaxation, we may try built the segments multiple times. We do
3776 // that by copying the original segment list before lay-out, doing
3777 // a trial lay-out and roll-back to the saved copied if we need to
3778 // to the lay-out again.
3779
3780 class Output_segment
3781 {
3782 public:
3783 // Create an output segment, specifying the type and flags.
3784 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3785
3786 // Return the virtual address.
3787 uint64_t
3788 vaddr() const
3789 { return this->vaddr_; }
3790
3791 // Return the physical address.
3792 uint64_t
3793 paddr() const
3794 { return this->paddr_; }
3795
3796 // Return the segment type.
3797 elfcpp::Elf_Word
3798 type() const
3799 { return this->type_; }
3800
3801 // Return the segment flags.
3802 elfcpp::Elf_Word
3803 flags() const
3804 { return this->flags_; }
3805
3806 // Return the memory size.
3807 uint64_t
3808 memsz() const
3809 { return this->memsz_; }
3810
3811 // Return the file size.
3812 off_t
3813 filesz() const
3814 { return this->filesz_; }
3815
3816 // Return the file offset.
3817 off_t
3818 offset() const
3819 { return this->offset_; }
3820
3821 // Whether this is a segment created to hold large data sections.
3822 bool
3823 is_large_data_segment() const
3824 { return this->is_large_data_segment_; }
3825
3826 // Record that this is a segment created to hold large data
3827 // sections.
3828 void
3829 set_is_large_data_segment()
3830 { this->is_large_data_segment_ = true; }
3831
3832 // Return the maximum alignment of the Output_data.
3833 uint64_t
3834 maximum_alignment();
3835
3836 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
3837 // the segment flags to use.
3838 void
3839 add_output_section_to_load(Layout* layout, Output_section* os,
3840 elfcpp::Elf_Word seg_flags);
3841
3842 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
3843 // is the segment flags to use.
3844 void
3845 add_output_section_to_nonload(Output_section* os,
3846 elfcpp::Elf_Word seg_flags);
3847
3848 // Remove an Output_section from this segment. It is an error if it
3849 // is not present.
3850 void
3851 remove_output_section(Output_section* os);
3852
3853 // Add an Output_data (which need not be an Output_section) to the
3854 // start of this segment.
3855 void
3856 add_initial_output_data(Output_data*);
3857
3858 // Return true if this segment has any sections which hold actual
3859 // data, rather than being a BSS section.
3860 bool
3861 has_any_data_sections() const;
3862
3863 // Whether this segment has a dynamic relocs.
3864 bool
3865 has_dynamic_reloc() const;
3866
3867 // Return the address of the first section.
3868 uint64_t
3869 first_section_load_address() const;
3870
3871 // Return whether the addresses have been set already.
3872 bool
3873 are_addresses_set() const
3874 { return this->are_addresses_set_; }
3875
3876 // Set the addresses.
3877 void
3878 set_addresses(uint64_t vaddr, uint64_t paddr)
3879 {
3880 this->vaddr_ = vaddr;
3881 this->paddr_ = paddr;
3882 this->are_addresses_set_ = true;
3883 }
3884
3885 // Update the flags for the flags of an output section added to this
3886 // segment.
3887 void
3888 update_flags_for_output_section(elfcpp::Elf_Xword flags)
3889 {
3890 // The ELF ABI specifies that a PT_TLS segment should always have
3891 // PF_R as the flags.
3892 if (this->type() != elfcpp::PT_TLS)
3893 this->flags_ |= flags;
3894 }
3895
3896 // Set the segment flags. This is only used if we have a PHDRS
3897 // clause which explicitly specifies the flags.
3898 void
3899 set_flags(elfcpp::Elf_Word flags)
3900 { this->flags_ = flags; }
3901
3902 // Set the address of the segment to ADDR and the offset to *POFF
3903 // and set the addresses and offsets of all contained output
3904 // sections accordingly. Set the section indexes of all contained
3905 // output sections starting with *PSHNDX. If RESET is true, first
3906 // reset the addresses of the contained sections. Return the
3907 // address of the immediately following segment. Update *POFF and
3908 // *PSHNDX. This should only be called for a PT_LOAD segment.
3909 uint64_t
3910 set_section_addresses(const Layout*, bool reset, uint64_t addr,
3911 unsigned int increase_relro, off_t* poff,
3912 unsigned int* pshndx);
3913
3914 // Set the minimum alignment of this segment. This may be adjusted
3915 // upward based on the section alignments.
3916 void
3917 set_minimum_p_align(uint64_t align)
3918 {
3919 if (align > this->min_p_align_)
3920 this->min_p_align_ = align;
3921 }
3922
3923 // Set the offset of this segment based on the section. This should
3924 // only be called for a non-PT_LOAD segment.
3925 void
3926 set_offset(unsigned int increase);
3927
3928 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3929 void
3930 set_tls_offsets();
3931
3932 // Return the number of output sections.
3933 unsigned int
3934 output_section_count() const;
3935
3936 // Return the section attached to the list segment with the lowest
3937 // load address. This is used when handling a PHDRS clause in a
3938 // linker script.
3939 Output_section*
3940 section_with_lowest_load_address() const;
3941
3942 // Write the segment header into *OPHDR.
3943 template<int size, bool big_endian>
3944 void
3945 write_header(elfcpp::Phdr_write<size, big_endian>*);
3946
3947 // Write the section headers of associated sections into V.
3948 template<int size, bool big_endian>
3949 unsigned char*
3950 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
3951 unsigned int* pshndx) const;
3952
3953 // Print the output sections in the map file.
3954 void
3955 print_sections_to_mapfile(Mapfile*) const;
3956
3957 private:
3958 typedef std::vector<Output_data*> Output_data_list;
3959
3960 // Find the maximum alignment in an Output_data_list.
3961 static uint64_t
3962 maximum_alignment_list(const Output_data_list*);
3963
3964 // Return whether the first data section is a relro section.
3965 bool
3966 is_first_section_relro() const;
3967
3968 // Set the section addresses in an Output_data_list.
3969 uint64_t
3970 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3971 uint64_t addr, off_t* poff, unsigned int* pshndx,
3972 bool* in_tls);
3973
3974 // Return the number of Output_sections in an Output_data_list.
3975 unsigned int
3976 output_section_count_list(const Output_data_list*) const;
3977
3978 // Return whether an Output_data_list has a dynamic reloc.
3979 bool
3980 has_dynamic_reloc_list(const Output_data_list*) const;
3981
3982 // Find the section with the lowest load address in an
3983 // Output_data_list.
3984 void
3985 lowest_load_address_in_list(const Output_data_list* pdl,
3986 Output_section** found,
3987 uint64_t* found_lma) const;
3988
3989 // Find the first and last entries by address.
3990 void
3991 find_first_and_last_list(const Output_data_list* pdl,
3992 const Output_data** pfirst,
3993 const Output_data** plast) const;
3994
3995 // Write the section headers in the list into V.
3996 template<int size, bool big_endian>
3997 unsigned char*
3998 write_section_headers_list(const Layout*, const Stringpool*,
3999 const Output_data_list*, unsigned char* v,
4000 unsigned int* pshdx) const;
4001
4002 // Print a section list to the mapfile.
4003 void
4004 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4005
4006 // NOTE: We want to use the copy constructor. Currently, shallow copy
4007 // works for us so we do not need to write our own copy constructor.
4008
4009 // The list of output data attached to this segment.
4010 Output_data_list output_lists_[ORDER_MAX];
4011 // The segment virtual address.
4012 uint64_t vaddr_;
4013 // The segment physical address.
4014 uint64_t paddr_;
4015 // The size of the segment in memory.
4016 uint64_t memsz_;
4017 // The maximum section alignment. The is_max_align_known_ field
4018 // indicates whether this has been finalized.
4019 uint64_t max_align_;
4020 // The required minimum value for the p_align field. This is used
4021 // for PT_LOAD segments. Note that this does not mean that
4022 // addresses should be aligned to this value; it means the p_paddr
4023 // and p_vaddr fields must be congruent modulo this value. For
4024 // non-PT_LOAD segments, the dynamic linker works more efficiently
4025 // if the p_align field has the more conventional value, although it
4026 // can align as needed.
4027 uint64_t min_p_align_;
4028 // The offset of the segment data within the file.
4029 off_t offset_;
4030 // The size of the segment data in the file.
4031 off_t filesz_;
4032 // The segment type;
4033 elfcpp::Elf_Word type_;
4034 // The segment flags.
4035 elfcpp::Elf_Word flags_;
4036 // Whether we have finalized max_align_.
4037 bool is_max_align_known_ : 1;
4038 // Whether vaddr and paddr were set by a linker script.
4039 bool are_addresses_set_ : 1;
4040 // Whether this segment holds large data sections.
4041 bool is_large_data_segment_ : 1;
4042 };
4043
4044 // This class represents the output file.
4045
4046 class Output_file
4047 {
4048 public:
4049 Output_file(const char* name);
4050
4051 // Indicate that this is a temporary file which should not be
4052 // output.
4053 void
4054 set_is_temporary()
4055 { this->is_temporary_ = true; }
4056
4057 // Try to open an existing file. Returns false if the file doesn't
4058 // exist, has a size of 0 or can't be mmaped. This method is
4059 // thread-unsafe.
4060 bool
4061 open_for_modification();
4062
4063 // Open the output file. FILE_SIZE is the final size of the file.
4064 // If the file already exists, it is deleted/truncated. This method
4065 // is thread-unsafe.
4066 void
4067 open(off_t file_size);
4068
4069 // Resize the output file. This method is thread-unsafe.
4070 void
4071 resize(off_t file_size);
4072
4073 // Close the output file (flushing all buffered data) and make sure
4074 // there are no errors. This method is thread-unsafe.
4075 void
4076 close();
4077
4078 // Return the size of this file.
4079 off_t
4080 filesize()
4081 { return this->file_size_; }
4082
4083 // Return the name of this file.
4084 const char*
4085 filename()
4086 { return this->name_; }
4087
4088 // We currently always use mmap which makes the view handling quite
4089 // simple. In the future we may support other approaches.
4090
4091 // Write data to the output file.
4092 void
4093 write(off_t offset, const void* data, size_t len)
4094 { memcpy(this->base_ + offset, data, len); }
4095
4096 // Get a buffer to use to write to the file, given the offset into
4097 // the file and the size.
4098 unsigned char*
4099 get_output_view(off_t start, size_t size)
4100 {
4101 gold_assert(start >= 0
4102 && start + static_cast<off_t>(size) <= this->file_size_);
4103 return this->base_ + start;
4104 }
4105
4106 // VIEW must have been returned by get_output_view. Write the
4107 // buffer to the file, passing in the offset and the size.
4108 void
4109 write_output_view(off_t, size_t, unsigned char*)
4110 { }
4111
4112 // Get a read/write buffer. This is used when we want to write part
4113 // of the file, read it in, and write it again.
4114 unsigned char*
4115 get_input_output_view(off_t start, size_t size)
4116 { return this->get_output_view(start, size); }
4117
4118 // Write a read/write buffer back to the file.
4119 void
4120 write_input_output_view(off_t, size_t, unsigned char*)
4121 { }
4122
4123 // Get a read buffer. This is used when we just want to read part
4124 // of the file back it in.
4125 const unsigned char*
4126 get_input_view(off_t start, size_t size)
4127 { return this->get_output_view(start, size); }
4128
4129 // Release a read bfufer.
4130 void
4131 free_input_view(off_t, size_t, const unsigned char*)
4132 { }
4133
4134 private:
4135 // Map the file into memory or, if that fails, allocate anonymous
4136 // memory.
4137 void
4138 map();
4139
4140 // Allocate anonymous memory for the file.
4141 bool
4142 map_anonymous();
4143
4144 // Map the file into memory.
4145 bool
4146 map_no_anonymous();
4147
4148 // Unmap the file from memory (and flush to disk buffers).
4149 void
4150 unmap();
4151
4152 // File name.
4153 const char* name_;
4154 // File descriptor.
4155 int o_;
4156 // File size.
4157 off_t file_size_;
4158 // Base of file mapped into memory.
4159 unsigned char* base_;
4160 // True iff base_ points to a memory buffer rather than an output file.
4161 bool map_is_anonymous_;
4162 // True if this is a temporary file which should not be output.
4163 bool is_temporary_;
4164 };
4165
4166 } // End namespace gold.
4167
4168 #endif // !defined(GOLD_OUTPUT_H)
This page took 0.138687 seconds and 5 git commands to generate.