Fully implement the SECTIONS clause.
[binutils-gdb.git] / gold / output.h
1 // output.h -- manage the output file for gold -*- C++ -*-
2
3 // Copyright 2006, 2007 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 "layout.h"
31 #include "reloc-types.h"
32
33 namespace gold
34 {
35
36 class General_options;
37 class Object;
38 class Symbol;
39 class Output_file;
40 class Output_section;
41 class Target;
42 template<int size, bool big_endian>
43 class Sized_target;
44 template<int size, bool big_endian>
45 class Sized_relobj;
46
47 // An abtract class for data which has to go into the output file.
48
49 class Output_data
50 {
51 public:
52 explicit Output_data()
53 : address_(0), data_size_(0), offset_(-1),
54 is_address_valid_(false), is_data_size_valid_(false),
55 is_offset_valid_(false),
56 dynamic_reloc_count_(0)
57 { }
58
59 virtual
60 ~Output_data();
61
62 // Return the address. For allocated sections, this is only valid
63 // after Layout::finalize is finished.
64 uint64_t
65 address() const
66 {
67 gold_assert(this->is_address_valid_);
68 return this->address_;
69 }
70
71 // Return the size of the data. For allocated sections, this must
72 // be valid after Layout::finalize calls set_address, but need not
73 // be valid before then.
74 off_t
75 data_size() const
76 {
77 gold_assert(this->is_data_size_valid_);
78 return this->data_size_;
79 }
80
81 // Return the file offset. This is only valid after
82 // Layout::finalize is finished. For some non-allocated sections,
83 // it may not be valid until near the end of the link.
84 off_t
85 offset() const
86 {
87 gold_assert(this->is_offset_valid_);
88 return this->offset_;
89 }
90
91 // Reset the address and file offset. This essentially disables the
92 // sanity testing about duplicate and unknown settings.
93 void
94 reset_address_and_file_offset()
95 {
96 this->is_address_valid_ = false;
97 this->is_offset_valid_ = false;
98 this->is_data_size_valid_ = false;
99 this->do_reset_address_and_file_offset();
100 }
101
102 // Return the required alignment.
103 uint64_t
104 addralign() const
105 { return this->do_addralign(); }
106
107 // Return whether this has a load address.
108 bool
109 has_load_address() const
110 { return this->do_has_load_address(); }
111
112 // Return the load address.
113 uint64_t
114 load_address() const
115 { return this->do_load_address(); }
116
117 // Return whether this is an Output_section.
118 bool
119 is_section() const
120 { return this->do_is_section(); }
121
122 // Return whether this is an Output_section of the specified type.
123 bool
124 is_section_type(elfcpp::Elf_Word stt) const
125 { return this->do_is_section_type(stt); }
126
127 // Return whether this is an Output_section with the specified flag
128 // set.
129 bool
130 is_section_flag_set(elfcpp::Elf_Xword shf) const
131 { return this->do_is_section_flag_set(shf); }
132
133 // Return the output section index, if there is an output section.
134 unsigned int
135 out_shndx() const
136 { return this->do_out_shndx(); }
137
138 // Set the output section index, if this is an output section.
139 void
140 set_out_shndx(unsigned int shndx)
141 { this->do_set_out_shndx(shndx); }
142
143 // Set the address and file offset of this data, and finalize the
144 // size of the data. This is called during Layout::finalize for
145 // allocated sections.
146 void
147 set_address_and_file_offset(uint64_t addr, off_t off)
148 {
149 this->set_address(addr);
150 this->set_file_offset(off);
151 this->finalize_data_size();
152 }
153
154 // Set the address.
155 void
156 set_address(uint64_t addr)
157 {
158 gold_assert(!this->is_address_valid_);
159 this->address_ = addr;
160 this->is_address_valid_ = true;
161 }
162
163 // Set the file offset.
164 void
165 set_file_offset(off_t off)
166 {
167 gold_assert(!this->is_offset_valid_);
168 this->offset_ = off;
169 this->is_offset_valid_ = true;
170 }
171
172 // Finalize the data size.
173 void
174 finalize_data_size()
175 {
176 if (!this->is_data_size_valid_)
177 {
178 // Tell the child class to set the data size.
179 this->set_final_data_size();
180 gold_assert(this->is_data_size_valid_);
181 }
182 }
183
184 // Set the TLS offset. Called only for SHT_TLS sections.
185 void
186 set_tls_offset(uint64_t tls_base)
187 { this->do_set_tls_offset(tls_base); }
188
189 // Return the TLS offset, relative to the base of the TLS segment.
190 // Valid only for SHT_TLS sections.
191 uint64_t
192 tls_offset() const
193 { return this->do_tls_offset(); }
194
195 // Write the data to the output file. This is called after
196 // Layout::finalize is complete.
197 void
198 write(Output_file* file)
199 { this->do_write(file); }
200
201 // This is called by Layout::finalize to note that the sizes of
202 // allocated sections must now be fixed.
203 static void
204 layout_complete()
205 { Output_data::allocated_sizes_are_fixed = true; }
206
207 // Used to check that layout has been done.
208 static bool
209 is_layout_complete()
210 { return Output_data::allocated_sizes_are_fixed; }
211
212 // Count the number of dynamic relocations applied to this section.
213 void
214 add_dynamic_reloc()
215 { ++this->dynamic_reloc_count_; }
216
217 // Return the number of dynamic relocations applied to this section.
218 unsigned int
219 dynamic_reloc_count() const
220 { return this->dynamic_reloc_count_; }
221
222 // Whether the address is valid.
223 bool
224 is_address_valid() const
225 { return this->is_address_valid_; }
226
227 // Whether the file offset is valid.
228 bool
229 is_offset_valid() const
230 { return this->is_offset_valid_; }
231
232 // Whether the data size is valid.
233 bool
234 is_data_size_valid() const
235 { return this->is_data_size_valid_; }
236
237 protected:
238 // Functions that child classes may or in some cases must implement.
239
240 // Write the data to the output file.
241 virtual void
242 do_write(Output_file*) = 0;
243
244 // Return the required alignment.
245 virtual uint64_t
246 do_addralign() const = 0;
247
248 // Return whether this has a load address.
249 virtual bool
250 do_has_load_address() const
251 { return false; }
252
253 // Return the load address.
254 virtual uint64_t
255 do_load_address() const
256 { gold_unreachable(); }
257
258 // Return whether this is an Output_section.
259 virtual bool
260 do_is_section() const
261 { return false; }
262
263 // Return whether this is an Output_section of the specified type.
264 // This only needs to be implement by Output_section.
265 virtual bool
266 do_is_section_type(elfcpp::Elf_Word) const
267 { return false; }
268
269 // Return whether this is an Output_section with the specific flag
270 // set. This only needs to be implemented by Output_section.
271 virtual bool
272 do_is_section_flag_set(elfcpp::Elf_Xword) const
273 { return false; }
274
275 // Return the output section index, if there is an output section.
276 virtual unsigned int
277 do_out_shndx() const
278 { gold_unreachable(); }
279
280 // Set the output section index, if this is an output section.
281 virtual void
282 do_set_out_shndx(unsigned int)
283 { gold_unreachable(); }
284
285 // This is a hook for derived classes to set the data size. This is
286 // called by finalize_data_size, normally called during
287 // Layout::finalize, when the section address is set.
288 virtual void
289 set_final_data_size()
290 { gold_unreachable(); }
291
292 // A hook for resetting the address and file offset.
293 virtual void
294 do_reset_address_and_file_offset()
295 { }
296
297 // Set the TLS offset. Called only for SHT_TLS sections.
298 virtual void
299 do_set_tls_offset(uint64_t)
300 { gold_unreachable(); }
301
302 // Return the TLS offset, relative to the base of the TLS segment.
303 // Valid only for SHT_TLS sections.
304 virtual uint64_t
305 do_tls_offset() const
306 { gold_unreachable(); }
307
308 // Functions that child classes may call.
309
310 // Set the size of the data.
311 void
312 set_data_size(off_t data_size)
313 {
314 gold_assert(!this->is_data_size_valid_);
315 this->data_size_ = data_size;
316 this->is_data_size_valid_ = true;
317 }
318
319 // Get the current data size--this is for the convenience of
320 // sections which build up their size over time.
321 off_t
322 current_data_size_for_child() const
323 { return this->data_size_; }
324
325 // Set the current data size--this is for the convenience of
326 // sections which build up their size over time.
327 void
328 set_current_data_size_for_child(off_t data_size)
329 {
330 gold_assert(!this->is_data_size_valid_);
331 this->data_size_ = data_size;
332 }
333
334 // Return default alignment for the target size.
335 static uint64_t
336 default_alignment();
337
338 // Return default alignment for a specified size--32 or 64.
339 static uint64_t
340 default_alignment_for_size(int size);
341
342 private:
343 Output_data(const Output_data&);
344 Output_data& operator=(const Output_data&);
345
346 // This is used for verification, to make sure that we don't try to
347 // change any sizes of allocated sections after we set the section
348 // addresses.
349 static bool allocated_sizes_are_fixed;
350
351 // Memory address in output file.
352 uint64_t address_;
353 // Size of data in output file.
354 off_t data_size_;
355 // File offset of contents in output file.
356 off_t offset_;
357 // Whether address_ is valid.
358 bool is_address_valid_;
359 // Whether data_size_ is valid.
360 bool is_data_size_valid_;
361 // Whether offset_ is valid.
362 bool is_offset_valid_;
363 // Count of dynamic relocations applied to this section.
364 unsigned int dynamic_reloc_count_;
365 };
366
367 // Output the section headers.
368
369 class Output_section_headers : public Output_data
370 {
371 public:
372 Output_section_headers(const Layout*,
373 const Layout::Segment_list*,
374 const Layout::Section_list*,
375 const Stringpool*);
376
377 protected:
378 // Write the data to the file.
379 void
380 do_write(Output_file*);
381
382 // Return the required alignment.
383 uint64_t
384 do_addralign() const
385 { return Output_data::default_alignment(); }
386
387 private:
388 // Write the data to the file with the right size and endianness.
389 template<int size, bool big_endian>
390 void
391 do_sized_write(Output_file*);
392
393 const Layout* layout_;
394 const Layout::Segment_list* segment_list_;
395 const Layout::Section_list* unattached_section_list_;
396 const Stringpool* secnamepool_;
397 };
398
399 // Output the segment headers.
400
401 class Output_segment_headers : public Output_data
402 {
403 public:
404 Output_segment_headers(const Layout::Segment_list& segment_list);
405
406 protected:
407 // Write the data to the file.
408 void
409 do_write(Output_file*);
410
411 // Return the required alignment.
412 uint64_t
413 do_addralign() const
414 { return Output_data::default_alignment(); }
415
416 private:
417 // Write the data to the file with the right size and endianness.
418 template<int size, bool big_endian>
419 void
420 do_sized_write(Output_file*);
421
422 const Layout::Segment_list& segment_list_;
423 };
424
425 // Output the ELF file header.
426
427 class Output_file_header : public Output_data
428 {
429 public:
430 Output_file_header(const Target*,
431 const Symbol_table*,
432 const Output_segment_headers*,
433 const char* entry);
434
435 // Add information about the section headers. We lay out the ELF
436 // file header before we create the section headers.
437 void set_section_info(const Output_section_headers*,
438 const Output_section* shstrtab);
439
440 protected:
441 // Write the data to the file.
442 void
443 do_write(Output_file*);
444
445 // Return the required alignment.
446 uint64_t
447 do_addralign() const
448 { return Output_data::default_alignment(); }
449
450 private:
451 // Write the data to the file with the right size and endianness.
452 template<int size, bool big_endian>
453 void
454 do_sized_write(Output_file*);
455
456 // Return the value to use for the entry address.
457 template<int size>
458 typename elfcpp::Elf_types<size>::Elf_Addr
459 entry();
460
461 const Target* target_;
462 const Symbol_table* symtab_;
463 const Output_segment_headers* segment_header_;
464 const Output_section_headers* section_header_;
465 const Output_section* shstrtab_;
466 const char* entry_;
467 };
468
469 // Output sections are mainly comprised of input sections. However,
470 // there are cases where we have data to write out which is not in an
471 // input section. Output_section_data is used in such cases. This is
472 // an abstract base class.
473
474 class Output_section_data : public Output_data
475 {
476 public:
477 Output_section_data(off_t data_size, uint64_t addralign)
478 : Output_data(), output_section_(NULL), addralign_(addralign)
479 { this->set_data_size(data_size); }
480
481 Output_section_data(uint64_t addralign)
482 : Output_data(), output_section_(NULL), addralign_(addralign)
483 { }
484
485 // Return the output section.
486 const Output_section*
487 output_section() const
488 { return this->output_section_; }
489
490 // Record the output section.
491 void
492 set_output_section(Output_section* os);
493
494 // Add an input section, for SHF_MERGE sections. This returns true
495 // if the section was handled.
496 bool
497 add_input_section(Relobj* object, unsigned int shndx)
498 { return this->do_add_input_section(object, shndx); }
499
500 // Given an input OBJECT, an input section index SHNDX within that
501 // object, and an OFFSET relative to the start of that input
502 // section, return whether or not the corresponding offset within
503 // the output section is known. If this function returns true, it
504 // sets *POUTPUT to the output offset. The value -1 indicates that
505 // this input offset is being discarded.
506 bool
507 output_offset(const Relobj* object, unsigned int shndx,
508 section_offset_type offset,
509 section_offset_type *poutput) const
510 { return this->do_output_offset(object, shndx, offset, poutput); }
511
512 // Return whether this is the merge section for the input section
513 // SHNDX in OBJECT. This should return true when output_offset
514 // would return true for some values of OFFSET.
515 bool
516 is_merge_section_for(const Relobj* object, unsigned int shndx) const
517 { return this->do_is_merge_section_for(object, shndx); }
518
519 // Write the contents to a buffer. This is used for sections which
520 // require postprocessing, such as compression.
521 void
522 write_to_buffer(unsigned char* buffer)
523 { this->do_write_to_buffer(buffer); }
524
525 // Print merge stats to stderr. This should only be called for
526 // SHF_MERGE sections.
527 void
528 print_merge_stats(const char* section_name)
529 { this->do_print_merge_stats(section_name); }
530
531 protected:
532 // The child class must implement do_write.
533
534 // The child class may implement specific adjustments to the output
535 // section.
536 virtual void
537 do_adjust_output_section(Output_section*)
538 { }
539
540 // May be implemented by child class. Return true if the section
541 // was handled.
542 virtual bool
543 do_add_input_section(Relobj*, unsigned int)
544 { gold_unreachable(); }
545
546 // The child class may implement output_offset.
547 virtual bool
548 do_output_offset(const Relobj*, unsigned int, section_offset_type,
549 section_offset_type*) const
550 { return false; }
551
552 // The child class may implement is_merge_section_for.
553 virtual bool
554 do_is_merge_section_for(const Relobj*, unsigned int) const
555 { return false; }
556
557 // The child class may implement write_to_buffer. Most child
558 // classes can not appear in a compressed section, and they do not
559 // implement this.
560 virtual void
561 do_write_to_buffer(unsigned char*)
562 { gold_unreachable(); }
563
564 // Print merge statistics.
565 virtual void
566 do_print_merge_stats(const char*)
567 { gold_unreachable(); }
568
569 // Return the required alignment.
570 uint64_t
571 do_addralign() const
572 { return this->addralign_; }
573
574 // Return the section index of the output section.
575 unsigned int
576 do_out_shndx() const;
577
578 // Set the alignment.
579 void
580 set_addralign(uint64_t addralign)
581 { this->addralign_ = addralign; }
582
583 private:
584 // The output section for this section.
585 const Output_section* output_section_;
586 // The required alignment.
587 uint64_t addralign_;
588 };
589
590 // Some Output_section_data classes build up their data step by step,
591 // rather than all at once. This class provides an interface for
592 // them.
593
594 class Output_section_data_build : public Output_section_data
595 {
596 public:
597 Output_section_data_build(uint64_t addralign)
598 : Output_section_data(addralign)
599 { }
600
601 // Get the current data size.
602 off_t
603 current_data_size() const
604 { return this->current_data_size_for_child(); }
605
606 // Set the current data size.
607 void
608 set_current_data_size(off_t data_size)
609 { this->set_current_data_size_for_child(data_size); }
610
611 protected:
612 // Set the final data size.
613 virtual void
614 set_final_data_size()
615 { this->set_data_size(this->current_data_size_for_child()); }
616 };
617
618 // A simple case of Output_data in which we have constant data to
619 // output.
620
621 class Output_data_const : public Output_section_data
622 {
623 public:
624 Output_data_const(const std::string& data, uint64_t addralign)
625 : Output_section_data(data.size(), addralign), data_(data)
626 { }
627
628 Output_data_const(const char* p, off_t len, uint64_t addralign)
629 : Output_section_data(len, addralign), data_(p, len)
630 { }
631
632 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
633 : Output_section_data(len, addralign),
634 data_(reinterpret_cast<const char*>(p), len)
635 { }
636
637 protected:
638 // Write the data to the output file.
639 void
640 do_write(Output_file*);
641
642 // Write the data to a buffer.
643 void
644 do_write_to_buffer(unsigned char* buffer)
645 { memcpy(buffer, this->data_.data(), this->data_.size()); }
646
647 private:
648 std::string data_;
649 };
650
651 // Another version of Output_data with constant data, in which the
652 // buffer is allocated by the caller.
653
654 class Output_data_const_buffer : public Output_section_data
655 {
656 public:
657 Output_data_const_buffer(const unsigned char* p, off_t len,
658 uint64_t addralign)
659 : Output_section_data(len, addralign), p_(p)
660 { }
661
662 protected:
663 // Write the data the output file.
664 void
665 do_write(Output_file*);
666
667 // Write the data to a buffer.
668 void
669 do_write_to_buffer(unsigned char* buffer)
670 { memcpy(buffer, this->p_, this->data_size()); }
671
672 private:
673 const unsigned char* p_;
674 };
675
676 // A place holder for a fixed amount of data written out via some
677 // other mechanism.
678
679 class Output_data_fixed_space : public Output_section_data
680 {
681 public:
682 Output_data_fixed_space(off_t data_size, uint64_t addralign)
683 : Output_section_data(data_size, addralign)
684 { }
685
686 protected:
687 // Write out the data--the actual data must be written out
688 // elsewhere.
689 void
690 do_write(Output_file*)
691 { }
692 };
693
694 // A place holder for variable sized data written out via some other
695 // mechanism.
696
697 class Output_data_space : public Output_section_data_build
698 {
699 public:
700 explicit Output_data_space(uint64_t addralign)
701 : Output_section_data_build(addralign)
702 { }
703
704 // Set the alignment.
705 void
706 set_space_alignment(uint64_t align)
707 { this->set_addralign(align); }
708
709 protected:
710 // Write out the data--the actual data must be written out
711 // elsewhere.
712 void
713 do_write(Output_file*)
714 { }
715 };
716
717 // A string table which goes into an output section.
718
719 class Output_data_strtab : public Output_section_data
720 {
721 public:
722 Output_data_strtab(Stringpool* strtab)
723 : Output_section_data(1), strtab_(strtab)
724 { }
725
726 protected:
727 // This is called to set the address and file offset. Here we make
728 // sure that the Stringpool is finalized.
729 void
730 set_final_data_size();
731
732 // Write out the data.
733 void
734 do_write(Output_file*);
735
736 // Write the data to a buffer.
737 void
738 do_write_to_buffer(unsigned char* buffer)
739 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
740
741 private:
742 Stringpool* strtab_;
743 };
744
745 // This POD class is used to represent a single reloc in the output
746 // file. This could be a private class within Output_data_reloc, but
747 // the templatization is complex enough that I broke it out into a
748 // separate class. The class is templatized on either elfcpp::SHT_REL
749 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
750 // relocation or an ordinary relocation.
751
752 // A relocation can be against a global symbol, a local symbol, an
753 // output section, or the undefined symbol at index 0. We represent
754 // the latter by using a NULL global symbol.
755
756 template<int sh_type, bool dynamic, int size, bool big_endian>
757 class Output_reloc;
758
759 template<bool dynamic, int size, bool big_endian>
760 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
761 {
762 public:
763 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
764
765 // An uninitialized entry. We need this because we want to put
766 // instances of this class into an STL container.
767 Output_reloc()
768 : local_sym_index_(INVALID_CODE)
769 { }
770
771 // A reloc against a global symbol.
772
773 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
774 Address address, bool is_relative);
775
776 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
777 unsigned int shndx, Address address, bool is_relative);
778
779 // A reloc against a local symbol.
780
781 Output_reloc(Sized_relobj<size, big_endian>* relobj,
782 unsigned int local_sym_index, unsigned int type,
783 Output_data* od, Address address, bool is_relative);
784
785 Output_reloc(Sized_relobj<size, big_endian>* relobj,
786 unsigned int local_sym_index, unsigned int type,
787 unsigned int shndx, Address address, bool is_relative);
788
789 // A reloc against the STT_SECTION symbol of an output section.
790
791 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
792 Address address);
793
794 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
795 unsigned int shndx, Address address);
796
797 // Return TRUE if this is a RELATIVE relocation.
798 bool
799 is_relative() const
800 { return this->is_relative_; }
801
802 // Get the value of the symbol referred to by a Rel relocation.
803
804 Address
805 symbol_value() const;
806
807 // Write the reloc entry to an output view.
808 void
809 write(unsigned char* pov) const;
810
811 // Write the offset and info fields to Write_rel.
812 template<typename Write_rel>
813 void write_rel(Write_rel*) const;
814
815 private:
816 // Return the symbol index. We can't do a double template
817 // specialization, so we do a secondary template here.
818 unsigned int
819 get_symbol_index() const;
820
821 // Codes for local_sym_index_.
822 enum
823 {
824 // Global symbol.
825 GSYM_CODE = -1U,
826 // Output section.
827 SECTION_CODE = -2U,
828 // Invalid uninitialized entry.
829 INVALID_CODE = -3U
830 };
831
832 union
833 {
834 // For a local symbol, the object. We will never generate a
835 // relocation against a local symbol in a dynamic object; that
836 // doesn't make sense. And our callers will always be
837 // templatized, so we use Sized_relobj here.
838 Sized_relobj<size, big_endian>* relobj;
839 // For a global symbol, the symbol. If this is NULL, it indicates
840 // a relocation against the undefined 0 symbol.
841 Symbol* gsym;
842 // For a relocation against an output section, the output section.
843 Output_section* os;
844 } u1_;
845 union
846 {
847 // If shndx_ is not INVALID CODE, the object which holds the input
848 // section being used to specify the reloc address.
849 Relobj* relobj;
850 // If shndx_ is INVALID_CODE, the output data being used to
851 // specify the reloc address. This may be NULL if the reloc
852 // address is absolute.
853 Output_data* od;
854 } u2_;
855 // The address offset within the input section or the Output_data.
856 Address address_;
857 // For a local symbol, the local symbol index. This is GSYM_CODE
858 // for a global symbol, or INVALID_CODE for an uninitialized value.
859 unsigned int local_sym_index_;
860 // The reloc type--a processor specific code.
861 unsigned int type_ : 31;
862 // True if the relocation is a RELATIVE relocation.
863 bool is_relative_ : 1;
864 // If the reloc address is an input section in an object, the
865 // section index. This is INVALID_CODE if the reloc address is
866 // specified in some other way.
867 unsigned int shndx_;
868 };
869
870 // The SHT_RELA version of Output_reloc<>. This is just derived from
871 // the SHT_REL version of Output_reloc, but it adds an addend.
872
873 template<bool dynamic, int size, bool big_endian>
874 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
875 {
876 public:
877 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
878 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
879
880 // An uninitialized entry.
881 Output_reloc()
882 : rel_()
883 { }
884
885 // A reloc against a global symbol.
886
887 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
888 Address address, Addend addend, bool is_relative)
889 : rel_(gsym, type, od, address, is_relative), addend_(addend)
890 { }
891
892 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
893 unsigned int shndx, Address address, Addend addend,
894 bool is_relative)
895 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
896 { }
897
898 // A reloc against a local symbol.
899
900 Output_reloc(Sized_relobj<size, big_endian>* relobj,
901 unsigned int local_sym_index, unsigned int type,
902 Output_data* od, Address address,
903 Addend addend, bool is_relative)
904 : rel_(relobj, local_sym_index, type, od, address, is_relative),
905 addend_(addend)
906 { }
907
908 Output_reloc(Sized_relobj<size, big_endian>* relobj,
909 unsigned int local_sym_index, unsigned int type,
910 unsigned int shndx, Address address,
911 Addend addend, bool is_relative)
912 : rel_(relobj, local_sym_index, type, shndx, address, is_relative),
913 addend_(addend)
914 { }
915
916 // A reloc against the STT_SECTION symbol of an output section.
917
918 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
919 Address address, Addend addend)
920 : rel_(os, type, od, address), addend_(addend)
921 { }
922
923 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
924 unsigned int shndx, Address address, Addend addend)
925 : rel_(os, type, relobj, shndx, address), addend_(addend)
926 { }
927
928 // Write the reloc entry to an output view.
929 void
930 write(unsigned char* pov) const;
931
932 private:
933 // The basic reloc.
934 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
935 // The addend.
936 Addend addend_;
937 };
938
939 // Output_data_reloc is used to manage a section containing relocs.
940 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
941 // indicates whether this is a dynamic relocation or a normal
942 // relocation. Output_data_reloc_base is a base class.
943 // Output_data_reloc is the real class, which we specialize based on
944 // the reloc type.
945
946 template<int sh_type, bool dynamic, int size, bool big_endian>
947 class Output_data_reloc_base : public Output_section_data_build
948 {
949 public:
950 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
951 typedef typename Output_reloc_type::Address Address;
952 static const int reloc_size =
953 Reloc_types<sh_type, size, big_endian>::reloc_size;
954
955 // Construct the section.
956 Output_data_reloc_base()
957 : Output_section_data_build(Output_data::default_alignment_for_size(size))
958 { }
959
960 protected:
961 // Write out the data.
962 void
963 do_write(Output_file*);
964
965 // Set the entry size and the link.
966 void
967 do_adjust_output_section(Output_section *os);
968
969 // Add a relocation entry.
970 void
971 add(Output_data *od, const Output_reloc_type& reloc)
972 {
973 this->relocs_.push_back(reloc);
974 this->set_current_data_size(this->relocs_.size() * reloc_size);
975 od->add_dynamic_reloc();
976 }
977
978 private:
979 typedef std::vector<Output_reloc_type> Relocs;
980
981 Relocs relocs_;
982 };
983
984 // The class which callers actually create.
985
986 template<int sh_type, bool dynamic, int size, bool big_endian>
987 class Output_data_reloc;
988
989 // The SHT_REL version of Output_data_reloc.
990
991 template<bool dynamic, int size, bool big_endian>
992 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
993 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
994 {
995 private:
996 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
997 big_endian> Base;
998
999 public:
1000 typedef typename Base::Output_reloc_type Output_reloc_type;
1001 typedef typename Output_reloc_type::Address Address;
1002
1003 Output_data_reloc()
1004 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1005 { }
1006
1007 // Add a reloc against a global symbol.
1008
1009 void
1010 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1011 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1012
1013 void
1014 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1015 unsigned int shndx, Address address)
1016 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1017 false)); }
1018
1019 // Add a RELATIVE reloc against a global symbol. The final relocation
1020 // will not reference the symbol.
1021
1022 void
1023 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1024 Address address)
1025 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1026
1027 void
1028 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1029 Relobj* relobj, unsigned int shndx, Address address)
1030 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1031 true)); }
1032
1033 // Add a reloc against a local symbol.
1034
1035 void
1036 add_local(Sized_relobj<size, big_endian>* relobj,
1037 unsigned int local_sym_index, unsigned int type,
1038 Output_data* od, Address address)
1039 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1040 address, false)); }
1041
1042 void
1043 add_local(Sized_relobj<size, big_endian>* relobj,
1044 unsigned int local_sym_index, unsigned int type,
1045 Output_data* od, unsigned int shndx, Address address)
1046 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1047 address, false)); }
1048
1049 // Add a RELATIVE reloc against a local symbol.
1050
1051 void
1052 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1053 unsigned int local_sym_index, unsigned int type,
1054 Output_data* od, Address address)
1055 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1056 address, true)); }
1057
1058 void
1059 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1060 unsigned int local_sym_index, unsigned int type,
1061 Output_data* od, unsigned int shndx, Address address)
1062 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1063 address, true)); }
1064
1065 // A reloc against the STT_SECTION symbol of an output section.
1066 // OS is the Output_section that the relocation refers to; OD is
1067 // the Output_data object being relocated.
1068
1069 void
1070 add_output_section(Output_section* os, unsigned int type,
1071 Output_data* od, Address address)
1072 { this->add(od, Output_reloc_type(os, type, od, address)); }
1073
1074 void
1075 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1076 Relobj* relobj, unsigned int shndx, Address address)
1077 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1078 };
1079
1080 // The SHT_RELA version of Output_data_reloc.
1081
1082 template<bool dynamic, int size, bool big_endian>
1083 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1084 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1085 {
1086 private:
1087 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1088 big_endian> Base;
1089
1090 public:
1091 typedef typename Base::Output_reloc_type Output_reloc_type;
1092 typedef typename Output_reloc_type::Address Address;
1093 typedef typename Output_reloc_type::Addend Addend;
1094
1095 Output_data_reloc()
1096 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1097 { }
1098
1099 // Add a reloc against a global symbol.
1100
1101 void
1102 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1103 Address address, Addend addend)
1104 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1105 false)); }
1106
1107 void
1108 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1109 unsigned int shndx, Address address,
1110 Addend addend)
1111 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1112 addend, false)); }
1113
1114 // Add a RELATIVE reloc against a global symbol. The final output
1115 // relocation will not reference the symbol, but we must keep the symbol
1116 // information long enough to set the addend of the relocation correctly
1117 // when it is written.
1118
1119 void
1120 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1121 Address address, Addend addend)
1122 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1123
1124 void
1125 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1126 Relobj* relobj, unsigned int shndx, Address address,
1127 Addend addend)
1128 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1129 addend, true)); }
1130
1131 // Add a reloc against a local symbol.
1132
1133 void
1134 add_local(Sized_relobj<size, big_endian>* relobj,
1135 unsigned int local_sym_index, unsigned int type,
1136 Output_data* od, Address address, Addend addend)
1137 {
1138 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1139 addend, false));
1140 }
1141
1142 void
1143 add_local(Sized_relobj<size, big_endian>* relobj,
1144 unsigned int local_sym_index, unsigned int type,
1145 Output_data* od, unsigned int shndx, Address address,
1146 Addend addend)
1147 {
1148 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1149 address, addend, false));
1150 }
1151
1152 // Add a RELATIVE reloc against a local symbol.
1153
1154 void
1155 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1156 unsigned int local_sym_index, unsigned int type,
1157 Output_data* od, Address address, Addend addend)
1158 {
1159 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1160 addend, true));
1161 }
1162
1163 void
1164 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1165 unsigned int local_sym_index, unsigned int type,
1166 Output_data* od, unsigned int shndx, Address address,
1167 Addend addend)
1168 {
1169 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1170 address, addend, true));
1171 }
1172
1173 // A reloc against the STT_SECTION symbol of an output section.
1174
1175 void
1176 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1177 Address address, Addend addend)
1178 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1179
1180 void
1181 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1182 unsigned int shndx, Address address, Addend addend)
1183 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1184 addend)); }
1185 };
1186
1187 // Output_data_got is used to manage a GOT. Each entry in the GOT is
1188 // for one symbol--either a global symbol or a local symbol in an
1189 // object. The target specific code adds entries to the GOT as
1190 // needed.
1191
1192 template<int size, bool big_endian>
1193 class Output_data_got : public Output_section_data_build
1194 {
1195 public:
1196 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1197 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1198 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1199
1200 Output_data_got()
1201 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1202 entries_()
1203 { }
1204
1205 // Add an entry for a global symbol to the GOT. Return true if this
1206 // is a new GOT entry, false if the symbol was already in the GOT.
1207 bool
1208 add_global(Symbol* gsym);
1209
1210 // Add an entry for a global symbol to the GOT, and add a dynamic
1211 // relocation of type R_TYPE for the GOT entry.
1212 void
1213 add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1214
1215 void
1216 add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1217
1218 // Add an entry for a local symbol to the GOT. This returns true if
1219 // this is a new GOT entry, false if the symbol already has a GOT
1220 // entry.
1221 bool
1222 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
1223
1224 // Add an entry for a global symbol to the GOT, and add a dynamic
1225 // relocation of type R_TYPE for the GOT entry.
1226 void
1227 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1228 unsigned int sym_index, Rel_dyn* rel_dyn,
1229 unsigned int r_type);
1230
1231 void
1232 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1233 unsigned int sym_index, Rela_dyn* rela_dyn,
1234 unsigned int r_type);
1235
1236 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1237 // Return true if this is a new GOT entry, false if the symbol was
1238 // already in the GOT.
1239 bool
1240 add_global_tls(Symbol* gsym, bool need_pair);
1241
1242 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1243 // relocation of type R_TYPE.
1244 void
1245 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1246 unsigned int r_type);
1247
1248 void
1249 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1250 unsigned int r_type);
1251
1252 // Add a pair of entries for a global TLS symbol to the GOT, and add
1253 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1254 void
1255 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1256 unsigned int mod_r_type,
1257 unsigned int dtv_r_type);
1258
1259 void
1260 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1261 unsigned int mod_r_type,
1262 unsigned int dtv_r_type);
1263
1264 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1265 // This returns true if this is a new GOT entry, false if the symbol
1266 // already has a GOT entry.
1267 bool
1268 add_local_tls(Sized_relobj<size, big_endian>* object,
1269 unsigned int sym_index, bool need_pair);
1270
1271 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1272 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1273 // Because this is a local symbol, the first GOT entry can be relocated
1274 // relative to a section symbol, and the second GOT entry will have an
1275 // dtv-relative value that can be computed at link time.
1276 void
1277 add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1278 unsigned int sym_index, unsigned int shndx,
1279 bool need_pair, Rel_dyn* rel_dyn,
1280 unsigned int r_type);
1281
1282 void
1283 add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1284 unsigned int sym_index, unsigned int shndx,
1285 bool need_pair, Rela_dyn* rela_dyn,
1286 unsigned int r_type);
1287
1288 // Add a constant to the GOT. This returns the offset of the new
1289 // entry from the start of the GOT.
1290 unsigned int
1291 add_constant(Valtype constant)
1292 {
1293 this->entries_.push_back(Got_entry(constant));
1294 this->set_got_size();
1295 return this->last_got_offset();
1296 }
1297
1298 protected:
1299 // Write out the GOT table.
1300 void
1301 do_write(Output_file*);
1302
1303 private:
1304 // This POD class holds a single GOT entry.
1305 class Got_entry
1306 {
1307 public:
1308 // Create a zero entry.
1309 Got_entry()
1310 : local_sym_index_(CONSTANT_CODE)
1311 { this->u_.constant = 0; }
1312
1313 // Create a global symbol entry.
1314 explicit Got_entry(Symbol* gsym)
1315 : local_sym_index_(GSYM_CODE)
1316 { this->u_.gsym = gsym; }
1317
1318 // Create a local symbol entry.
1319 Got_entry(Sized_relobj<size, big_endian>* object,
1320 unsigned int local_sym_index)
1321 : local_sym_index_(local_sym_index)
1322 {
1323 gold_assert(local_sym_index != GSYM_CODE
1324 && local_sym_index != CONSTANT_CODE);
1325 this->u_.object = object;
1326 }
1327
1328 // Create a constant entry. The constant is a host value--it will
1329 // be swapped, if necessary, when it is written out.
1330 explicit Got_entry(Valtype constant)
1331 : local_sym_index_(CONSTANT_CODE)
1332 { this->u_.constant = constant; }
1333
1334 // Write the GOT entry to an output view.
1335 void
1336 write(unsigned char* pov) const;
1337
1338 private:
1339 enum
1340 {
1341 GSYM_CODE = -1U,
1342 CONSTANT_CODE = -2U
1343 };
1344
1345 union
1346 {
1347 // For a local symbol, the object.
1348 Sized_relobj<size, big_endian>* object;
1349 // For a global symbol, the symbol.
1350 Symbol* gsym;
1351 // For a constant, the constant.
1352 Valtype constant;
1353 } u_;
1354 // For a local symbol, the local symbol index. This is GSYM_CODE
1355 // for a global symbol, or CONSTANT_CODE for a constant.
1356 unsigned int local_sym_index_;
1357 };
1358
1359 typedef std::vector<Got_entry> Got_entries;
1360
1361 // Return the offset into the GOT of GOT entry I.
1362 unsigned int
1363 got_offset(unsigned int i) const
1364 { return i * (size / 8); }
1365
1366 // Return the offset into the GOT of the last entry added.
1367 unsigned int
1368 last_got_offset() const
1369 { return this->got_offset(this->entries_.size() - 1); }
1370
1371 // Set the size of the section.
1372 void
1373 set_got_size()
1374 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1375
1376 // The list of GOT entries.
1377 Got_entries entries_;
1378 };
1379
1380 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1381 // section.
1382
1383 class Output_data_dynamic : public Output_section_data
1384 {
1385 public:
1386 Output_data_dynamic(Stringpool* pool)
1387 : Output_section_data(Output_data::default_alignment()),
1388 entries_(), pool_(pool)
1389 { }
1390
1391 // Add a new dynamic entry with a fixed numeric value.
1392 void
1393 add_constant(elfcpp::DT tag, unsigned int val)
1394 { this->add_entry(Dynamic_entry(tag, val)); }
1395
1396 // Add a new dynamic entry with the address of output data.
1397 void
1398 add_section_address(elfcpp::DT tag, const Output_data* od)
1399 { this->add_entry(Dynamic_entry(tag, od, false)); }
1400
1401 // Add a new dynamic entry with the size of output data.
1402 void
1403 add_section_size(elfcpp::DT tag, const Output_data* od)
1404 { this->add_entry(Dynamic_entry(tag, od, true)); }
1405
1406 // Add a new dynamic entry with the address of a symbol.
1407 void
1408 add_symbol(elfcpp::DT tag, const Symbol* sym)
1409 { this->add_entry(Dynamic_entry(tag, sym)); }
1410
1411 // Add a new dynamic entry with a string.
1412 void
1413 add_string(elfcpp::DT tag, const char* str)
1414 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1415
1416 void
1417 add_string(elfcpp::DT tag, const std::string& str)
1418 { this->add_string(tag, str.c_str()); }
1419
1420 protected:
1421 // Adjust the output section to set the entry size.
1422 void
1423 do_adjust_output_section(Output_section*);
1424
1425 // Set the final data size.
1426 void
1427 set_final_data_size();
1428
1429 // Write out the dynamic entries.
1430 void
1431 do_write(Output_file*);
1432
1433 private:
1434 // This POD class holds a single dynamic entry.
1435 class Dynamic_entry
1436 {
1437 public:
1438 // Create an entry with a fixed numeric value.
1439 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1440 : tag_(tag), classification_(DYNAMIC_NUMBER)
1441 { this->u_.val = val; }
1442
1443 // Create an entry with the size or address of a section.
1444 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1445 : tag_(tag),
1446 classification_(section_size
1447 ? DYNAMIC_SECTION_SIZE
1448 : DYNAMIC_SECTION_ADDRESS)
1449 { this->u_.od = od; }
1450
1451 // Create an entry with the address of a symbol.
1452 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1453 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1454 { this->u_.sym = sym; }
1455
1456 // Create an entry with a string.
1457 Dynamic_entry(elfcpp::DT tag, const char* str)
1458 : tag_(tag), classification_(DYNAMIC_STRING)
1459 { this->u_.str = str; }
1460
1461 // Write the dynamic entry to an output view.
1462 template<int size, bool big_endian>
1463 void
1464 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1465
1466 private:
1467 enum Classification
1468 {
1469 // Number.
1470 DYNAMIC_NUMBER,
1471 // Section address.
1472 DYNAMIC_SECTION_ADDRESS,
1473 // Section size.
1474 DYNAMIC_SECTION_SIZE,
1475 // Symbol adress.
1476 DYNAMIC_SYMBOL,
1477 // String.
1478 DYNAMIC_STRING
1479 };
1480
1481 union
1482 {
1483 // For DYNAMIC_NUMBER.
1484 unsigned int val;
1485 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1486 const Output_data* od;
1487 // For DYNAMIC_SYMBOL.
1488 const Symbol* sym;
1489 // For DYNAMIC_STRING.
1490 const char* str;
1491 } u_;
1492 // The dynamic tag.
1493 elfcpp::DT tag_;
1494 // The type of entry.
1495 Classification classification_;
1496 };
1497
1498 // Add an entry to the list.
1499 void
1500 add_entry(const Dynamic_entry& entry)
1501 { this->entries_.push_back(entry); }
1502
1503 // Sized version of write function.
1504 template<int size, bool big_endian>
1505 void
1506 sized_write(Output_file* of);
1507
1508 // The type of the list of entries.
1509 typedef std::vector<Dynamic_entry> Dynamic_entries;
1510
1511 // The entries.
1512 Dynamic_entries entries_;
1513 // The pool used for strings.
1514 Stringpool* pool_;
1515 };
1516
1517 // An output section. We don't expect to have too many output
1518 // sections, so we don't bother to do a template on the size.
1519
1520 class Output_section : public Output_data
1521 {
1522 public:
1523 // Create an output section, giving the name, type, and flags.
1524 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1525 virtual ~Output_section();
1526
1527 // Add a new input section SHNDX, named NAME, with header SHDR, from
1528 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1529 // which applies to this section, or 0 if none, or -1U if more than
1530 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1531 // in a linker script; in that case we need to keep track of input
1532 // sections associated with an output section. Return the offset
1533 // within the output section.
1534 template<int size, bool big_endian>
1535 off_t
1536 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1537 const char *name,
1538 const elfcpp::Shdr<size, big_endian>& shdr,
1539 unsigned int reloc_shndx, bool have_sections_script);
1540
1541 // Add generated data POSD to this output section.
1542 void
1543 add_output_section_data(Output_section_data* posd);
1544
1545 // Return the section name.
1546 const char*
1547 name() const
1548 { return this->name_; }
1549
1550 // Return the section type.
1551 elfcpp::Elf_Word
1552 type() const
1553 { return this->type_; }
1554
1555 // Return the section flags.
1556 elfcpp::Elf_Xword
1557 flags() const
1558 { return this->flags_; }
1559
1560 // Return the entsize field.
1561 uint64_t
1562 entsize() const
1563 { return this->entsize_; }
1564
1565 // Set the entsize field.
1566 void
1567 set_entsize(uint64_t v);
1568
1569 // Set the load address.
1570 void
1571 set_load_address(uint64_t load_address)
1572 {
1573 this->load_address_ = load_address;
1574 this->has_load_address_ = true;
1575 }
1576
1577 // Set the link field to the output section index of a section.
1578 void
1579 set_link_section(const Output_data* od)
1580 {
1581 gold_assert(this->link_ == 0
1582 && !this->should_link_to_symtab_
1583 && !this->should_link_to_dynsym_);
1584 this->link_section_ = od;
1585 }
1586
1587 // Set the link field to a constant.
1588 void
1589 set_link(unsigned int v)
1590 {
1591 gold_assert(this->link_section_ == NULL
1592 && !this->should_link_to_symtab_
1593 && !this->should_link_to_dynsym_);
1594 this->link_ = v;
1595 }
1596
1597 // Record that this section should link to the normal symbol table.
1598 void
1599 set_should_link_to_symtab()
1600 {
1601 gold_assert(this->link_section_ == NULL
1602 && this->link_ == 0
1603 && !this->should_link_to_dynsym_);
1604 this->should_link_to_symtab_ = true;
1605 }
1606
1607 // Record that this section should link to the dynamic symbol table.
1608 void
1609 set_should_link_to_dynsym()
1610 {
1611 gold_assert(this->link_section_ == NULL
1612 && this->link_ == 0
1613 && !this->should_link_to_symtab_);
1614 this->should_link_to_dynsym_ = true;
1615 }
1616
1617 // Return the info field.
1618 unsigned int
1619 info() const
1620 {
1621 gold_assert(this->info_section_ == NULL);
1622 return this->info_;
1623 }
1624
1625 // Set the info field to the output section index of a section.
1626 void
1627 set_info_section(const Output_data* od)
1628 {
1629 gold_assert(this->info_ == 0);
1630 this->info_section_ = od;
1631 }
1632
1633 // Set the info field to a constant.
1634 void
1635 set_info(unsigned int v)
1636 {
1637 gold_assert(this->info_section_ == NULL);
1638 this->info_ = v;
1639 }
1640
1641 // Set the addralign field.
1642 void
1643 set_addralign(uint64_t v)
1644 { this->addralign_ = v; }
1645
1646 // Indicate that we need a symtab index.
1647 void
1648 set_needs_symtab_index()
1649 { this->needs_symtab_index_ = true; }
1650
1651 // Return whether we need a symtab index.
1652 bool
1653 needs_symtab_index() const
1654 { return this->needs_symtab_index_; }
1655
1656 // Get the symtab index.
1657 unsigned int
1658 symtab_index() const
1659 {
1660 gold_assert(this->symtab_index_ != 0);
1661 return this->symtab_index_;
1662 }
1663
1664 // Set the symtab index.
1665 void
1666 set_symtab_index(unsigned int index)
1667 {
1668 gold_assert(index != 0);
1669 this->symtab_index_ = index;
1670 }
1671
1672 // Indicate that we need a dynsym index.
1673 void
1674 set_needs_dynsym_index()
1675 { this->needs_dynsym_index_ = true; }
1676
1677 // Return whether we need a dynsym index.
1678 bool
1679 needs_dynsym_index() const
1680 { return this->needs_dynsym_index_; }
1681
1682 // Get the dynsym index.
1683 unsigned int
1684 dynsym_index() const
1685 {
1686 gold_assert(this->dynsym_index_ != 0);
1687 return this->dynsym_index_;
1688 }
1689
1690 // Set the dynsym index.
1691 void
1692 set_dynsym_index(unsigned int index)
1693 {
1694 gold_assert(index != 0);
1695 this->dynsym_index_ = index;
1696 }
1697
1698 // Return whether this section should be written after all the input
1699 // sections are complete.
1700 bool
1701 after_input_sections() const
1702 { return this->after_input_sections_; }
1703
1704 // Record that this section should be written after all the input
1705 // sections are complete.
1706 void
1707 set_after_input_sections()
1708 { this->after_input_sections_ = true; }
1709
1710 // Return whether this section requires postprocessing after all
1711 // relocations have been applied.
1712 bool
1713 requires_postprocessing() const
1714 { return this->requires_postprocessing_; }
1715
1716 // If a section requires postprocessing, return the buffer to use.
1717 unsigned char*
1718 postprocessing_buffer() const
1719 {
1720 gold_assert(this->postprocessing_buffer_ != NULL);
1721 return this->postprocessing_buffer_;
1722 }
1723
1724 // If a section requires postprocessing, create the buffer to use.
1725 void
1726 create_postprocessing_buffer();
1727
1728 // If a section requires postprocessing, this is the size of the
1729 // buffer to which relocations should be applied.
1730 off_t
1731 postprocessing_buffer_size() const
1732 { return this->current_data_size_for_child(); }
1733
1734 // Return whether the offset OFFSET in the input section SHNDX in
1735 // object OBJECT is being included in the link.
1736 bool
1737 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1738 off_t offset) const;
1739
1740 // Return the offset within the output section of OFFSET relative to
1741 // the start of input section SHNDX in object OBJECT.
1742 section_offset_type
1743 output_offset(const Relobj* object, unsigned int shndx,
1744 section_offset_type offset) const;
1745
1746 // Return the output virtual address of OFFSET relative to the start
1747 // of input section SHNDX in object OBJECT.
1748 uint64_t
1749 output_address(const Relobj* object, unsigned int shndx,
1750 off_t offset) const;
1751
1752 // Return the output address of the start of the merged section for
1753 // input section SHNDX in object OBJECT. This is not necessarily
1754 // the offset corresponding to input offset 0 in the section, since
1755 // the section may be mapped arbitrarily.
1756 uint64_t
1757 starting_output_address(const Relobj* object, unsigned int shndx) const;
1758
1759 // Record that this output section was found in the SECTIONS clause
1760 // of a linker script.
1761 void
1762 set_found_in_sections_clause()
1763 { this->found_in_sections_clause_ = true; }
1764
1765 // Return whether this output section was found in the SECTIONS
1766 // clause of a linker script.
1767 bool
1768 found_in_sections_clause() const
1769 { return this->found_in_sections_clause_; }
1770
1771 // Write the section header into *OPHDR.
1772 template<int size, bool big_endian>
1773 void
1774 write_header(const Layout*, const Stringpool*,
1775 elfcpp::Shdr_write<size, big_endian>*) const;
1776
1777 // The next few calls are for linker script support.
1778
1779 // Store the list of input sections for this Output_section into the
1780 // list passed in. This removes the input sections, leaving only
1781 // any Output_section_data elements. This returns the size of those
1782 // Output_section_data elements. ADDRESS is the address of this
1783 // output section. FILL is the fill value to use, in case there are
1784 // any spaces between the remaining Output_section_data elements.
1785 uint64_t
1786 get_input_sections(uint64_t address, const std::string& fill,
1787 std::list<std::pair<Relobj*, unsigned int > >*);
1788
1789 // Add an input section from a script.
1790 void
1791 add_input_section_for_script(Relobj* object, unsigned int shndx,
1792 off_t data_size, uint64_t addralign);
1793
1794 // Set the current size of the output section.
1795 void
1796 set_current_data_size(off_t size)
1797 { this->set_current_data_size_for_child(size); }
1798
1799 // Get the current size of the output section.
1800 off_t
1801 current_data_size() const
1802 { return this->current_data_size_for_child(); }
1803
1804 // End of linker script support.
1805
1806 // Print merge statistics to stderr.
1807 void
1808 print_merge_stats();
1809
1810 protected:
1811 // Return the section index in the output file.
1812 unsigned int
1813 do_out_shndx() const
1814 {
1815 gold_assert(this->out_shndx_ != -1U);
1816 return this->out_shndx_;
1817 }
1818
1819 // Set the output section index.
1820 void
1821 do_set_out_shndx(unsigned int shndx)
1822 {
1823 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
1824 this->out_shndx_ = shndx;
1825 }
1826
1827 // Set the final data size of the Output_section. For a typical
1828 // Output_section, there is nothing to do, but if there are any
1829 // Output_section_data objects we need to set their final addresses
1830 // here.
1831 virtual void
1832 set_final_data_size();
1833
1834 // Reset the address and file offset.
1835 void
1836 do_reset_address_and_file_offset();
1837
1838 // Write the data to the file. For a typical Output_section, this
1839 // does nothing: the data is written out by calling Object::Relocate
1840 // on each input object. But if there are any Output_section_data
1841 // objects we do need to write them out here.
1842 virtual void
1843 do_write(Output_file*);
1844
1845 // Return the address alignment--function required by parent class.
1846 uint64_t
1847 do_addralign() const
1848 { return this->addralign_; }
1849
1850 // Return whether there is a load address.
1851 bool
1852 do_has_load_address() const
1853 { return this->has_load_address_; }
1854
1855 // Return the load address.
1856 uint64_t
1857 do_load_address() const
1858 {
1859 gold_assert(this->has_load_address_);
1860 return this->load_address_;
1861 }
1862
1863 // Return whether this is an Output_section.
1864 bool
1865 do_is_section() const
1866 { return true; }
1867
1868 // Return whether this is a section of the specified type.
1869 bool
1870 do_is_section_type(elfcpp::Elf_Word type) const
1871 { return this->type_ == type; }
1872
1873 // Return whether the specified section flag is set.
1874 bool
1875 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1876 { return (this->flags_ & flag) != 0; }
1877
1878 // Set the TLS offset. Called only for SHT_TLS sections.
1879 void
1880 do_set_tls_offset(uint64_t tls_base);
1881
1882 // Return the TLS offset, relative to the base of the TLS segment.
1883 // Valid only for SHT_TLS sections.
1884 uint64_t
1885 do_tls_offset() const
1886 { return this->tls_offset_; }
1887
1888 // Modify the section name. This is only permitted for an
1889 // unallocated section, and only before the size has been finalized.
1890 // Otherwise the name will not get into Layout::namepool_.
1891 void
1892 set_name(const char* newname)
1893 {
1894 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1895 gold_assert(!this->is_data_size_valid());
1896 this->name_ = newname;
1897 }
1898
1899 // This may be implemented by a child class.
1900 virtual void
1901 do_finalize_name(Layout*)
1902 { }
1903
1904 // Record that this section requires postprocessing after all
1905 // relocations have been applied. This is called by a child class.
1906 void
1907 set_requires_postprocessing()
1908 {
1909 this->requires_postprocessing_ = true;
1910 this->after_input_sections_ = true;
1911 }
1912
1913 // Write all the data of an Output_section into the postprocessing
1914 // buffer.
1915 void
1916 write_to_postprocessing_buffer();
1917
1918 private:
1919 // In some cases we need to keep a list of the input sections
1920 // associated with this output section. We only need the list if we
1921 // might have to change the offsets of the input section within the
1922 // output section after we add the input section. The ordinary
1923 // input sections will be written out when we process the object
1924 // file, and as such we don't need to track them here. We do need
1925 // to track Output_section_data objects here. We store instances of
1926 // this structure in a std::vector, so it must be a POD. There can
1927 // be many instances of this structure, so we use a union to save
1928 // some space.
1929 class Input_section
1930 {
1931 public:
1932 Input_section()
1933 : shndx_(0), p2align_(0)
1934 {
1935 this->u1_.data_size = 0;
1936 this->u2_.object = NULL;
1937 }
1938
1939 // For an ordinary input section.
1940 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
1941 uint64_t addralign)
1942 : shndx_(shndx),
1943 p2align_(ffsll(static_cast<long long>(addralign)))
1944 {
1945 gold_assert(shndx != OUTPUT_SECTION_CODE
1946 && shndx != MERGE_DATA_SECTION_CODE
1947 && shndx != MERGE_STRING_SECTION_CODE);
1948 this->u1_.data_size = data_size;
1949 this->u2_.object = object;
1950 }
1951
1952 // For a non-merge output section.
1953 Input_section(Output_section_data* posd)
1954 : shndx_(OUTPUT_SECTION_CODE),
1955 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1956 {
1957 this->u1_.data_size = 0;
1958 this->u2_.posd = posd;
1959 }
1960
1961 // For a merge section.
1962 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
1963 : shndx_(is_string
1964 ? MERGE_STRING_SECTION_CODE
1965 : MERGE_DATA_SECTION_CODE),
1966 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1967 {
1968 this->u1_.entsize = entsize;
1969 this->u2_.posd = posd;
1970 }
1971
1972 // The required alignment.
1973 uint64_t
1974 addralign() const
1975 {
1976 return (this->p2align_ == 0
1977 ? 0
1978 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
1979 }
1980
1981 // Return the required size.
1982 off_t
1983 data_size() const;
1984
1985 // Whether this is an input section.
1986 bool
1987 is_input_section() const
1988 {
1989 return (this->shndx_ != OUTPUT_SECTION_CODE
1990 && this->shndx_ != MERGE_DATA_SECTION_CODE
1991 && this->shndx_ != MERGE_STRING_SECTION_CODE);
1992 }
1993
1994 // Return whether this is a merge section which matches the
1995 // parameters.
1996 bool
1997 is_merge_section(bool is_string, uint64_t entsize,
1998 uint64_t addralign) const
1999 {
2000 return (this->shndx_ == (is_string
2001 ? MERGE_STRING_SECTION_CODE
2002 : MERGE_DATA_SECTION_CODE)
2003 && this->u1_.entsize == entsize
2004 && this->addralign() == addralign);
2005 }
2006
2007 // Return the object for an input section.
2008 Relobj*
2009 relobj() const
2010 {
2011 gold_assert(this->is_input_section());
2012 return this->u2_.object;
2013 }
2014
2015 // Return the input section index for an input section.
2016 unsigned int
2017 shndx() const
2018 {
2019 gold_assert(this->is_input_section());
2020 return this->shndx_;
2021 }
2022
2023 // Set the output section.
2024 void
2025 set_output_section(Output_section* os)
2026 {
2027 gold_assert(!this->is_input_section());
2028 this->u2_.posd->set_output_section(os);
2029 }
2030
2031 // Set the address and file offset. This is called during
2032 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2033 // the enclosing section.
2034 void
2035 set_address_and_file_offset(uint64_t address, off_t file_offset,
2036 off_t section_file_offset);
2037
2038 // Reset the address and file offset.
2039 void
2040 reset_address_and_file_offset();
2041
2042 // Finalize the data size.
2043 void
2044 finalize_data_size();
2045
2046 // Add an input section, for SHF_MERGE sections.
2047 bool
2048 add_input_section(Relobj* object, unsigned int shndx)
2049 {
2050 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2051 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2052 return this->u2_.posd->add_input_section(object, shndx);
2053 }
2054
2055 // Given an input OBJECT, an input section index SHNDX within that
2056 // object, and an OFFSET relative to the start of that input
2057 // section, return whether or not the output offset is known. If
2058 // this function returns true, it sets *POUTPUT to the offset in
2059 // the output section, relative to the start of the input section
2060 // in the output section. *POUTPUT may be different from OFFSET
2061 // for a merged section.
2062 bool
2063 output_offset(const Relobj* object, unsigned int shndx,
2064 section_offset_type offset,
2065 section_offset_type *poutput) const;
2066
2067 // Return whether this is the merge section for the input section
2068 // SHNDX in OBJECT.
2069 bool
2070 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2071
2072 // Write out the data. This does nothing for an input section.
2073 void
2074 write(Output_file*);
2075
2076 // Write the data to a buffer. This does nothing for an input
2077 // section.
2078 void
2079 write_to_buffer(unsigned char*);
2080
2081 // Print statistics about merge sections to stderr.
2082 void
2083 print_merge_stats(const char* section_name)
2084 {
2085 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2086 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2087 this->u2_.posd->print_merge_stats(section_name);
2088 }
2089
2090 private:
2091 // Code values which appear in shndx_. If the value is not one of
2092 // these codes, it is the input section index in the object file.
2093 enum
2094 {
2095 // An Output_section_data.
2096 OUTPUT_SECTION_CODE = -1U,
2097 // An Output_section_data for an SHF_MERGE section with
2098 // SHF_STRINGS not set.
2099 MERGE_DATA_SECTION_CODE = -2U,
2100 // An Output_section_data for an SHF_MERGE section with
2101 // SHF_STRINGS set.
2102 MERGE_STRING_SECTION_CODE = -3U
2103 };
2104
2105 // For an ordinary input section, this is the section index in the
2106 // input file. For an Output_section_data, this is
2107 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2108 // MERGE_STRING_SECTION_CODE.
2109 unsigned int shndx_;
2110 // The required alignment, stored as a power of 2.
2111 unsigned int p2align_;
2112 union
2113 {
2114 // For an ordinary input section, the section size.
2115 off_t data_size;
2116 // For OUTPUT_SECTION_CODE, this is not used. For
2117 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2118 // entity size.
2119 uint64_t entsize;
2120 } u1_;
2121 union
2122 {
2123 // For an ordinary input section, the object which holds the
2124 // input section.
2125 Relobj* object;
2126 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2127 // MERGE_STRING_SECTION_CODE, the data.
2128 Output_section_data* posd;
2129 } u2_;
2130 };
2131
2132 typedef std::vector<Input_section> Input_section_list;
2133
2134 // Fill data. This is used to fill in data between input sections.
2135 // It is also used for data statements (BYTE, WORD, etc.) in linker
2136 // scripts. When we have to keep track of the input sections, we
2137 // can use an Output_data_const, but we don't want to have to keep
2138 // track of input sections just to implement fills.
2139 class Fill
2140 {
2141 public:
2142 Fill(off_t section_offset, off_t length)
2143 : section_offset_(section_offset),
2144 length_(convert_to_section_size_type(length))
2145 { }
2146
2147 // Return section offset.
2148 off_t
2149 section_offset() const
2150 { return this->section_offset_; }
2151
2152 // Return fill length.
2153 section_size_type
2154 length() const
2155 { return this->length_; }
2156
2157 private:
2158 // The offset within the output section.
2159 off_t section_offset_;
2160 // The length of the space to fill.
2161 section_size_type length_;
2162 };
2163
2164 typedef std::vector<Fill> Fill_list;
2165
2166 // Add a new output section by Input_section.
2167 void
2168 add_output_section_data(Input_section*);
2169
2170 // Add an SHF_MERGE input section. Returns true if the section was
2171 // handled.
2172 bool
2173 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
2174 uint64_t entsize, uint64_t addralign);
2175
2176 // Add an output SHF_MERGE section POSD to this output section.
2177 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2178 // ENTSIZE is the entity size. This returns the entry added to
2179 // input_sections_.
2180 void
2181 add_output_merge_section(Output_section_data* posd, bool is_string,
2182 uint64_t entsize);
2183
2184 // Most of these fields are only valid after layout.
2185
2186 // The name of the section. This will point into a Stringpool.
2187 const char* name_;
2188 // The section address is in the parent class.
2189 // The section alignment.
2190 uint64_t addralign_;
2191 // The section entry size.
2192 uint64_t entsize_;
2193 // The load address. This is only used when using a linker script
2194 // with a SECTIONS clause. The has_load_address_ field indicates
2195 // whether this field is valid.
2196 uint64_t load_address_;
2197 // The file offset is in the parent class.
2198 // Set the section link field to the index of this section.
2199 const Output_data* link_section_;
2200 // If link_section_ is NULL, this is the link field.
2201 unsigned int link_;
2202 // Set the section info field to the index of this section.
2203 const Output_data* info_section_;
2204 // If info_section_ is NULL, this is the section info field.
2205 unsigned int info_;
2206 // The section type.
2207 const elfcpp::Elf_Word type_;
2208 // The section flags.
2209 elfcpp::Elf_Xword flags_;
2210 // The section index.
2211 unsigned int out_shndx_;
2212 // If there is a STT_SECTION for this output section in the normal
2213 // symbol table, this is the symbol index. This starts out as zero.
2214 // It is initialized in Layout::finalize() to be the index, or -1U
2215 // if there isn't one.
2216 unsigned int symtab_index_;
2217 // If there is a STT_SECTION for this output section in the dynamic
2218 // symbol table, this is the symbol index. This starts out as zero.
2219 // It is initialized in Layout::finalize() to be the index, or -1U
2220 // if there isn't one.
2221 unsigned int dynsym_index_;
2222 // The input sections. This will be empty in cases where we don't
2223 // need to keep track of them.
2224 Input_section_list input_sections_;
2225 // The offset of the first entry in input_sections_.
2226 off_t first_input_offset_;
2227 // The fill data. This is separate from input_sections_ because we
2228 // often will need fill sections without needing to keep track of
2229 // input sections.
2230 Fill_list fills_;
2231 // If the section requires postprocessing, this buffer holds the
2232 // section contents during relocation.
2233 unsigned char* postprocessing_buffer_;
2234 // Whether this output section needs a STT_SECTION symbol in the
2235 // normal symbol table. This will be true if there is a relocation
2236 // which needs it.
2237 bool needs_symtab_index_ : 1;
2238 // Whether this output section needs a STT_SECTION symbol in the
2239 // dynamic symbol table. This will be true if there is a dynamic
2240 // relocation which needs it.
2241 bool needs_dynsym_index_ : 1;
2242 // Whether the link field of this output section should point to the
2243 // normal symbol table.
2244 bool should_link_to_symtab_ : 1;
2245 // Whether the link field of this output section should point to the
2246 // dynamic symbol table.
2247 bool should_link_to_dynsym_ : 1;
2248 // Whether this section should be written after all the input
2249 // sections are complete.
2250 bool after_input_sections_ : 1;
2251 // Whether this section requires post processing after all
2252 // relocations have been applied.
2253 bool requires_postprocessing_ : 1;
2254 // Whether an input section was mapped to this output section
2255 // because of a SECTIONS clause in a linker script.
2256 bool found_in_sections_clause_ : 1;
2257 // Whether this section has an explicitly specified load address.
2258 bool has_load_address_ : 1;
2259 // For SHT_TLS sections, the offset of this section relative to the base
2260 // of the TLS segment.
2261 uint64_t tls_offset_;
2262 };
2263
2264 // An output segment. PT_LOAD segments are built from collections of
2265 // output sections. Other segments typically point within PT_LOAD
2266 // segments, and are built directly as needed.
2267
2268 class Output_segment
2269 {
2270 public:
2271 // Create an output segment, specifying the type and flags.
2272 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2273
2274 // Return the virtual address.
2275 uint64_t
2276 vaddr() const
2277 { return this->vaddr_; }
2278
2279 // Return the physical address.
2280 uint64_t
2281 paddr() const
2282 { return this->paddr_; }
2283
2284 // Return the segment type.
2285 elfcpp::Elf_Word
2286 type() const
2287 { return this->type_; }
2288
2289 // Return the segment flags.
2290 elfcpp::Elf_Word
2291 flags() const
2292 { return this->flags_; }
2293
2294 // Return the memory size.
2295 uint64_t
2296 memsz() const
2297 { return this->memsz_; }
2298
2299 // Return the file size.
2300 off_t
2301 filesz() const
2302 { return this->filesz_; }
2303
2304 // Return the maximum alignment of the Output_data.
2305 uint64_t
2306 maximum_alignment();
2307
2308 // Add an Output_section to this segment.
2309 void
2310 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2311 { this->add_output_section(os, seg_flags, false); }
2312
2313 // Add an Output_section to the start of this segment.
2314 void
2315 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2316 { this->add_output_section(os, seg_flags, true); }
2317
2318 // Add an Output_data (which is not an Output_section) to the start
2319 // of this segment.
2320 void
2321 add_initial_output_data(Output_data*);
2322
2323 // Return the number of dynamic relocations applied to this segment.
2324 unsigned int
2325 dynamic_reloc_count() const;
2326
2327 // Return the address of the first section.
2328 uint64_t
2329 first_section_load_address() const;
2330
2331 // Return whether the addresses have been set already.
2332 bool
2333 are_addresses_set() const
2334 { return this->are_addresses_set_; }
2335
2336 // Set the addresses.
2337 void
2338 set_addresses(uint64_t vaddr, uint64_t paddr)
2339 {
2340 this->vaddr_ = vaddr;
2341 this->paddr_ = paddr;
2342 this->are_addresses_set_ = true;
2343 }
2344
2345 // Set the address of the segment to ADDR and the offset to *POFF
2346 // and set the addresses and offsets of all contained output
2347 // sections accordingly. Set the section indexes of all contained
2348 // output sections starting with *PSHNDX. If RESET is true, first
2349 // reset the addresses of the contained sections. Return the
2350 // address of the immediately following segment. Update *POFF and
2351 // *PSHNDX. This should only be called for a PT_LOAD segment.
2352 uint64_t
2353 set_section_addresses(bool reset, uint64_t addr, off_t* poff,
2354 unsigned int* pshndx);
2355
2356 // Set the minimum alignment of this segment. This may be adjusted
2357 // upward based on the section alignments.
2358 void
2359 set_minimum_p_align(uint64_t align)
2360 { this->min_p_align_ = align; }
2361
2362 // Set the offset of this segment based on the section. This should
2363 // only be called for a non-PT_LOAD segment.
2364 void
2365 set_offset();
2366
2367 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2368 void
2369 set_tls_offsets();
2370
2371 // Return the number of output sections.
2372 unsigned int
2373 output_section_count() const;
2374
2375 // Write the segment header into *OPHDR.
2376 template<int size, bool big_endian>
2377 void
2378 write_header(elfcpp::Phdr_write<size, big_endian>*);
2379
2380 // Write the section headers of associated sections into V.
2381 template<int size, bool big_endian>
2382 unsigned char*
2383 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
2384 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
2385
2386 private:
2387 Output_segment(const Output_segment&);
2388 Output_segment& operator=(const Output_segment&);
2389
2390 typedef std::list<Output_data*> Output_data_list;
2391
2392 // Add an Output_section to this segment, specifying front or back.
2393 void
2394 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2395 bool front);
2396
2397 // Find the maximum alignment in an Output_data_list.
2398 static uint64_t
2399 maximum_alignment_list(const Output_data_list*);
2400
2401 // Set the section addresses in an Output_data_list.
2402 uint64_t
2403 set_section_list_addresses(bool reset, Output_data_list*, uint64_t addr,
2404 off_t* poff, unsigned int* pshndx);
2405
2406 // Return the number of Output_sections in an Output_data_list.
2407 unsigned int
2408 output_section_count_list(const Output_data_list*) const;
2409
2410 // Return the number of dynamic relocs in an Output_data_list.
2411 unsigned int
2412 dynamic_reloc_count_list(const Output_data_list*) const;
2413
2414 // Write the section headers in the list into V.
2415 template<int size, bool big_endian>
2416 unsigned char*
2417 write_section_headers_list(const Layout*, const Stringpool*,
2418 const Output_data_list*, unsigned char* v,
2419 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
2420
2421 // The list of output data with contents attached to this segment.
2422 Output_data_list output_data_;
2423 // The list of output data without contents attached to this segment.
2424 Output_data_list output_bss_;
2425 // The segment virtual address.
2426 uint64_t vaddr_;
2427 // The segment physical address.
2428 uint64_t paddr_;
2429 // The size of the segment in memory.
2430 uint64_t memsz_;
2431 // The maximum section alignment. The is_max_align_known_ field
2432 // indicates whether this has been finalized.
2433 uint64_t max_align_;
2434 // The required minimum value for the p_align field. This is used
2435 // for PT_LOAD segments. Note that this does not mean that
2436 // addresses should be aligned to this value; it means the p_paddr
2437 // and p_vaddr fields must be congruent modulo this value. For
2438 // non-PT_LOAD segments, the dynamic linker works more efficiently
2439 // if the p_align field has the more conventional value, although it
2440 // can align as needed.
2441 uint64_t min_p_align_;
2442 // The offset of the segment data within the file.
2443 off_t offset_;
2444 // The size of the segment data in the file.
2445 off_t filesz_;
2446 // The segment type;
2447 elfcpp::Elf_Word type_;
2448 // The segment flags.
2449 elfcpp::Elf_Word flags_;
2450 // Whether we have finalized max_align_.
2451 bool is_max_align_known_ : 1;
2452 // Whether vaddr and paddr were set by a linker script.
2453 bool are_addresses_set_ : 1;
2454 };
2455
2456 // This class represents the output file.
2457
2458 class Output_file
2459 {
2460 public:
2461 Output_file(const char* name);
2462
2463 // Open the output file. FILE_SIZE is the final size of the file.
2464 void
2465 open(off_t file_size);
2466
2467 // Resize the output file.
2468 void
2469 resize(off_t file_size);
2470
2471 // Close the output file (flushing all buffered data) and make sure
2472 // there are no errors.
2473 void
2474 close();
2475
2476 // We currently always use mmap which makes the view handling quite
2477 // simple. In the future we may support other approaches.
2478
2479 // Write data to the output file.
2480 void
2481 write(off_t offset, const void* data, size_t len)
2482 { memcpy(this->base_ + offset, data, len); }
2483
2484 // Get a buffer to use to write to the file, given the offset into
2485 // the file and the size.
2486 unsigned char*
2487 get_output_view(off_t start, size_t size)
2488 {
2489 gold_assert(start >= 0
2490 && start + static_cast<off_t>(size) <= this->file_size_);
2491 return this->base_ + start;
2492 }
2493
2494 // VIEW must have been returned by get_output_view. Write the
2495 // buffer to the file, passing in the offset and the size.
2496 void
2497 write_output_view(off_t, size_t, unsigned char*)
2498 { }
2499
2500 // Get a read/write buffer. This is used when we want to write part
2501 // of the file, read it in, and write it again.
2502 unsigned char*
2503 get_input_output_view(off_t start, size_t size)
2504 { return this->get_output_view(start, size); }
2505
2506 // Write a read/write buffer back to the file.
2507 void
2508 write_input_output_view(off_t, size_t, unsigned char*)
2509 { }
2510
2511 // Get a read buffer. This is used when we just want to read part
2512 // of the file back it in.
2513 const unsigned char*
2514 get_input_view(off_t start, size_t size)
2515 { return this->get_output_view(start, size); }
2516
2517 // Release a read bfufer.
2518 void
2519 free_input_view(off_t, size_t, const unsigned char*)
2520 { }
2521
2522 private:
2523 // Map the file into memory and return a pointer to the map.
2524 void
2525 map();
2526
2527 // Unmap the file from memory (and flush to disk buffers).
2528 void
2529 unmap();
2530
2531 // File name.
2532 const char* name_;
2533 // File descriptor.
2534 int o_;
2535 // File size.
2536 off_t file_size_;
2537 // Base of file mapped into memory.
2538 unsigned char* base_;
2539 // True iff base_ points to a memory buffer rather than an output file.
2540 bool map_is_anonymous_;
2541 };
2542
2543 } // End namespace gold.
2544
2545 #endif // !defined(GOLD_OUTPUT_H)