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