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