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