From Andrew Chatham: add signedrel<>, rel32s and friends.
[deliverable/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 #include "parameters.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 Target;
43 template<int size, bool big_endian>
44 class Sized_target;
45 template<int size, bool big_endian>
46 class Sized_relobj;
47
48 // An abtract class for data which has to go into the output file.
49
50 class Output_data
51 {
52 public:
53 explicit Output_data(off_t data_size = 0)
54 : address_(0), data_size_(data_size), offset_(-1)
55 { }
56
57 virtual
58 ~Output_data();
59
60 // Return the address. This is only valid after Layout::finalize is
61 // finished.
62 uint64_t
63 address() const
64 { return this->address_; }
65
66 // Return the size of the data. This must be valid after
67 // Layout::finalize calls set_address, but need not be valid before
68 // then.
69 off_t
70 data_size() const
71 { return this->data_size_; }
72
73 // Return the file offset. This is only valid after
74 // Layout::finalize is finished.
75 off_t
76 offset() const
77 { return this->offset_; }
78
79 // Return the required alignment.
80 uint64_t
81 addralign() const
82 { return this->do_addralign(); }
83
84 // Return whether this is an Output_section.
85 bool
86 is_section() const
87 { return this->do_is_section(); }
88
89 // Return whether this is an Output_section of the specified type.
90 bool
91 is_section_type(elfcpp::Elf_Word stt) const
92 { return this->do_is_section_type(stt); }
93
94 // Return whether this is an Output_section with the specified flag
95 // set.
96 bool
97 is_section_flag_set(elfcpp::Elf_Xword shf) const
98 { return this->do_is_section_flag_set(shf); }
99
100 // Return the output section index, if there is an output section.
101 unsigned int
102 out_shndx() const
103 { return this->do_out_shndx(); }
104
105 // Set the output section index, if this is an output section.
106 void
107 set_out_shndx(unsigned int shndx)
108 { this->do_set_out_shndx(shndx); }
109
110 // Set the address and file offset of this data. This is called
111 // during Layout::finalize.
112 void
113 set_address(uint64_t addr, off_t off);
114
115 // Write the data to the output file. This is called after
116 // Layout::finalize is complete.
117 void
118 write(Output_file* file)
119 { this->do_write(file); }
120
121 // This is called by Layout::finalize to note that all sizes must
122 // now be fixed.
123 static void
124 layout_complete()
125 { Output_data::sizes_are_fixed = true; }
126
127 protected:
128 // Functions that child classes may or in some cases must implement.
129
130 // Write the data to the output file.
131 virtual void
132 do_write(Output_file*) = 0;
133
134 // Return the required alignment.
135 virtual uint64_t
136 do_addralign() const = 0;
137
138 // Return whether this is an Output_section.
139 virtual bool
140 do_is_section() const
141 { return false; }
142
143 // Return whether this is an Output_section of the specified type.
144 // This only needs to be implement by Output_section.
145 virtual bool
146 do_is_section_type(elfcpp::Elf_Word) const
147 { return false; }
148
149 // Return whether this is an Output_section with the specific flag
150 // set. This only needs to be implemented by Output_section.
151 virtual bool
152 do_is_section_flag_set(elfcpp::Elf_Xword) const
153 { return false; }
154
155 // Return the output section index, if there is an output section.
156 virtual unsigned int
157 do_out_shndx() const
158 { gold_unreachable(); }
159
160 // Set the output section index, if this is an output section.
161 virtual void
162 do_set_out_shndx(unsigned int)
163 { gold_unreachable(); }
164
165 // Set the address and file offset of the data. This only needs to
166 // be implemented if the child needs to know. The child class can
167 // set its size in this call.
168 virtual void
169 do_set_address(uint64_t, off_t)
170 { }
171
172 // Functions that child classes may call.
173
174 // Set the size of the data.
175 void
176 set_data_size(off_t data_size)
177 {
178 gold_assert(!Output_data::sizes_are_fixed);
179 this->data_size_ = data_size;
180 }
181
182 // Return default alignment for a size--32 or 64.
183 static uint64_t
184 default_alignment(int size);
185
186 private:
187 Output_data(const Output_data&);
188 Output_data& operator=(const Output_data&);
189
190 // This is used for verification, to make sure that we don't try to
191 // change any sizes after we set the section addresses.
192 static bool sizes_are_fixed;
193
194 // Memory address in file (not always meaningful).
195 uint64_t address_;
196 // Size of data in file.
197 off_t data_size_;
198 // Offset within file.
199 off_t offset_;
200 };
201
202 // Output the section headers.
203
204 class Output_section_headers : public Output_data
205 {
206 public:
207 Output_section_headers(const Layout*,
208 const Layout::Segment_list*,
209 const Layout::Section_list*,
210 const Stringpool*);
211
212 // Write the data to the file.
213 void
214 do_write(Output_file*);
215
216 // Return the required alignment.
217 uint64_t
218 do_addralign() const
219 { return Output_data::default_alignment(parameters->get_size()); }
220
221 private:
222 // Write the data to the file with the right size and endianness.
223 template<int size, bool big_endian>
224 void
225 do_sized_write(Output_file*);
226
227 const Layout* layout_;
228 const Layout::Segment_list* segment_list_;
229 const Layout::Section_list* unattached_section_list_;
230 const Stringpool* secnamepool_;
231 };
232
233 // Output the segment headers.
234
235 class Output_segment_headers : public Output_data
236 {
237 public:
238 Output_segment_headers(const Layout::Segment_list& segment_list);
239
240 // Write the data to the file.
241 void
242 do_write(Output_file*);
243
244 // Return the required alignment.
245 uint64_t
246 do_addralign() const
247 { return Output_data::default_alignment(parameters->get_size()); }
248
249 private:
250 // Write the data to the file with the right size and endianness.
251 template<int size, bool big_endian>
252 void
253 do_sized_write(Output_file*);
254
255 const Layout::Segment_list& segment_list_;
256 };
257
258 // Output the ELF file header.
259
260 class Output_file_header : public Output_data
261 {
262 public:
263 Output_file_header(const Target*,
264 const Symbol_table*,
265 const Output_segment_headers*);
266
267 // Add information about the section headers. We lay out the ELF
268 // file header before we create the section headers.
269 void set_section_info(const Output_section_headers*,
270 const Output_section* shstrtab);
271
272 // Write the data to the file.
273 void
274 do_write(Output_file*);
275
276 // Return the required alignment.
277 uint64_t
278 do_addralign() const
279 { return Output_data::default_alignment(parameters->get_size()); }
280
281 // Set the address and offset--we only implement this for error
282 // checking.
283 void
284 do_set_address(uint64_t, off_t off) const
285 { gold_assert(off == 0); }
286
287 private:
288 // Write the data to the file with the right size and endianness.
289 template<int size, bool big_endian>
290 void
291 do_sized_write(Output_file*);
292
293 const Target* target_;
294 const Symbol_table* symtab_;
295 const Output_segment_headers* segment_header_;
296 const Output_section_headers* section_header_;
297 const Output_section* shstrtab_;
298 };
299
300 // Output sections are mainly comprised of input sections. However,
301 // there are cases where we have data to write out which is not in an
302 // input section. Output_section_data is used in such cases. This is
303 // an abstract base class.
304
305 class Output_section_data : public Output_data
306 {
307 public:
308 Output_section_data(off_t data_size, uint64_t addralign)
309 : Output_data(data_size), output_section_(NULL), addralign_(addralign)
310 { }
311
312 Output_section_data(uint64_t addralign)
313 : Output_data(0), output_section_(NULL), addralign_(addralign)
314 { }
315
316 // Return the output section.
317 const Output_section*
318 output_section() const
319 { return this->output_section_; }
320
321 // Record the output section.
322 void
323 set_output_section(Output_section* os);
324
325 // Add an input section, for SHF_MERGE sections. This returns true
326 // if the section was handled.
327 bool
328 add_input_section(Relobj* object, unsigned int shndx)
329 { return this->do_add_input_section(object, shndx); }
330
331 // Given an input OBJECT, an input section index SHNDX within that
332 // object, and an OFFSET relative to the start of that input
333 // section, return whether or not the output address is known.
334 // OUTPUT_SECTION_ADDRESS is the address of the output section which
335 // this is a part of. If this function returns true, it sets
336 // *POUTPUT to the output address.
337 virtual bool
338 output_address(const Relobj* object, unsigned int shndx, off_t offset,
339 uint64_t output_section_address, uint64_t *poutput) const
340 {
341 return this->do_output_address(object, shndx, offset,
342 output_section_address, poutput);
343 }
344
345 protected:
346 // The child class must implement do_write.
347
348 // The child class may implement specific adjustments to the output
349 // section.
350 virtual void
351 do_adjust_output_section(Output_section*)
352 { }
353
354 // May be implemented by child class. Return true if the section
355 // was handled.
356 virtual bool
357 do_add_input_section(Relobj*, unsigned int)
358 { gold_unreachable(); }
359
360 // The child class may implement output_address.
361 virtual bool
362 do_output_address(const Relobj*, unsigned int, off_t, uint64_t,
363 uint64_t*) const
364 { return false; }
365
366 // Return the required alignment.
367 uint64_t
368 do_addralign() const
369 { return this->addralign_; }
370
371 // Return the section index of the output section.
372 unsigned int
373 do_out_shndx() const;
374
375 // Set the alignment.
376 void
377 set_addralign(uint64_t addralign)
378 { this->addralign_ = addralign; }
379
380 private:
381 // The output section for this section.
382 const Output_section* output_section_;
383 // The required alignment.
384 uint64_t addralign_;
385 };
386
387 // A simple case of Output_data in which we have constant data to
388 // output.
389
390 class Output_data_const : public Output_section_data
391 {
392 public:
393 Output_data_const(const std::string& data, uint64_t addralign)
394 : Output_section_data(data.size(), addralign), data_(data)
395 { }
396
397 Output_data_const(const char* p, off_t len, uint64_t addralign)
398 : Output_section_data(len, addralign), data_(p, len)
399 { }
400
401 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
402 : Output_section_data(len, addralign),
403 data_(reinterpret_cast<const char*>(p), len)
404 { }
405
406 // Add more data.
407 void
408 add_data(const std::string& add)
409 {
410 this->data_.append(add);
411 this->set_data_size(this->data_.size());
412 }
413
414 // Write the data to the output file.
415 void
416 do_write(Output_file*);
417
418 private:
419 std::string data_;
420 };
421
422 // Another version of Output_data with constant data, in which the
423 // buffer is allocated by the caller.
424
425 class Output_data_const_buffer : public Output_section_data
426 {
427 public:
428 Output_data_const_buffer(const unsigned char* p, off_t len,
429 uint64_t addralign)
430 : Output_section_data(len, addralign), p_(p)
431 { }
432
433 // Write the data the output file.
434 void
435 do_write(Output_file*);
436
437 private:
438 const unsigned char* p_;
439 };
440
441 // A place holder for data written out via some other mechanism.
442
443 class Output_data_space : public Output_section_data
444 {
445 public:
446 Output_data_space(off_t data_size, uint64_t addralign)
447 : Output_section_data(data_size, addralign)
448 { }
449
450 explicit Output_data_space(uint64_t addralign)
451 : Output_section_data(addralign)
452 { }
453
454 // Set the size.
455 void
456 set_space_size(off_t space_size)
457 { this->set_data_size(space_size); }
458
459 // Set the alignment.
460 void
461 set_space_alignment(uint64_t align)
462 { this->set_addralign(align); }
463
464 // Write out the data--this must be handled elsewhere.
465 void
466 do_write(Output_file*)
467 { }
468 };
469
470 // A string table which goes into an output section.
471
472 class Output_data_strtab : public Output_section_data
473 {
474 public:
475 Output_data_strtab(Stringpool* strtab)
476 : Output_section_data(1), strtab_(strtab)
477 { }
478
479 // This is called to set the address and file offset. Here we make
480 // sure that the Stringpool is finalized.
481 void
482 do_set_address(uint64_t, off_t);
483
484 // Write out the data.
485 void
486 do_write(Output_file*);
487
488 private:
489 Stringpool* strtab_;
490 };
491
492 // This POD class is used to represent a single reloc in the output
493 // file. This could be a private class within Output_data_reloc, but
494 // the templatization is complex enough that I broke it out into a
495 // separate class. The class is templatized on either elfcpp::SHT_REL
496 // or elfcpp::SHT_RELA, and also on whether this is a dynamic
497 // relocation or an ordinary relocation.
498
499 // A relocation can be against a global symbol, a local symbol, an
500 // output section, or the undefined symbol at index 0. We represent
501 // the latter by using a NULL global symbol.
502
503 template<int sh_type, bool dynamic, int size, bool big_endian>
504 class Output_reloc;
505
506 template<bool dynamic, int size, bool big_endian>
507 class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
508 {
509 public:
510 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
511
512 // An uninitialized entry. We need this because we want to put
513 // instances of this class into an STL container.
514 Output_reloc()
515 : local_sym_index_(INVALID_CODE)
516 { }
517
518 // A reloc against a global symbol.
519
520 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
521 Address address)
522 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
523 shndx_(INVALID_CODE)
524 {
525 this->u1_.gsym = gsym;
526 this->u2_.od = od;
527 }
528
529 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
530 unsigned int shndx, Address address)
531 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
532 shndx_(shndx)
533 {
534 gold_assert(shndx != INVALID_CODE);
535 this->u1_.gsym = gsym;
536 this->u2_.relobj = relobj;
537 }
538
539 // A reloc against a local symbol.
540
541 Output_reloc(Sized_relobj<size, big_endian>* relobj,
542 unsigned int local_sym_index,
543 unsigned int type,
544 Output_data* od,
545 Address address)
546 : address_(address), local_sym_index_(local_sym_index), type_(type),
547 shndx_(INVALID_CODE)
548 {
549 gold_assert(local_sym_index != GSYM_CODE
550 && local_sym_index != INVALID_CODE);
551 this->u1_.relobj = relobj;
552 this->u2_.od = od;
553 }
554
555 Output_reloc(Sized_relobj<size, big_endian>* relobj,
556 unsigned int local_sym_index,
557 unsigned int type,
558 unsigned int shndx,
559 Address address)
560 : address_(address), local_sym_index_(local_sym_index), type_(type),
561 shndx_(shndx)
562 {
563 gold_assert(local_sym_index != GSYM_CODE
564 && local_sym_index != INVALID_CODE);
565 gold_assert(shndx != INVALID_CODE);
566 this->u1_.relobj = relobj;
567 this->u2_.relobj = relobj;
568 }
569
570 // A reloc against the STT_SECTION symbol of an output section.
571
572 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
573 Address address)
574 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
575 shndx_(INVALID_CODE)
576 {
577 this->u1_.os = os;
578 this->u2_.od = od;
579 }
580
581 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
582 unsigned int shndx, Address address)
583 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
584 shndx_(shndx)
585 {
586 gold_assert(shndx != INVALID_CODE);
587 this->u1_.os = os;
588 this->u2_.relobj = relobj;
589 }
590
591 // Write the reloc entry to an output view.
592 void
593 write(unsigned char* pov) const;
594
595 // Write the offset and info fields to Write_rel.
596 template<typename Write_rel>
597 void write_rel(Write_rel*) const;
598
599 private:
600 // Return the symbol index. We can't do a double template
601 // specialization, so we do a secondary template here.
602 unsigned int
603 get_symbol_index() const;
604
605 // Codes for local_sym_index_.
606 enum
607 {
608 // Global symbol.
609 GSYM_CODE = -1U,
610 // Output section.
611 SECTION_CODE = -2U,
612 // Invalid uninitialized entry.
613 INVALID_CODE = -3U
614 };
615
616 union
617 {
618 // For a local symbol, the object. We will never generate a
619 // relocation against a local symbol in a dynamic object; that
620 // doesn't make sense. And our callers will always be
621 // templatized, so we use Sized_relobj here.
622 Sized_relobj<size, big_endian>* relobj;
623 // For a global symbol, the symbol. If this is NULL, it indicates
624 // a relocation against the undefined 0 symbol.
625 Symbol* gsym;
626 // For a relocation against an output section, the output section.
627 Output_section* os;
628 } u1_;
629 union
630 {
631 // If shndx_ is not INVALID CODE, the object which holds the input
632 // section being used to specify the reloc address.
633 Relobj* relobj;
634 // If shndx_ is INVALID_CODE, the output data being used to
635 // specify the reloc address. This may be NULL if the reloc
636 // address is absolute.
637 Output_data* od;
638 } u2_;
639 // The address offset within the input section or the Output_data.
640 Address address_;
641 // For a local symbol, the local symbol index. This is GSYM_CODE
642 // for a global symbol, or INVALID_CODE for an uninitialized value.
643 unsigned int local_sym_index_;
644 // The reloc type--a processor specific code.
645 unsigned int type_;
646 // If the reloc address is an input section in an object, the
647 // section index. This is INVALID_CODE if the reloc address is
648 // specified in some other way.
649 unsigned int shndx_;
650 };
651
652 // The SHT_RELA version of Output_reloc<>. This is just derived from
653 // the SHT_REL version of Output_reloc, but it adds an addend.
654
655 template<bool dynamic, int size, bool big_endian>
656 class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
657 {
658 public:
659 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
660 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
661
662 // An uninitialized entry.
663 Output_reloc()
664 : rel_()
665 { }
666
667 // A reloc against a global symbol.
668
669 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
670 Address address, Addend addend)
671 : rel_(gsym, type, od, address), addend_(addend)
672 { }
673
674 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
675 unsigned int shndx, Address address, Addend addend)
676 : rel_(gsym, type, relobj, shndx, address), addend_(addend)
677 { }
678
679 // A reloc against a local symbol.
680
681 Output_reloc(Sized_relobj<size, big_endian>* relobj,
682 unsigned int local_sym_index,
683 unsigned int type, Output_data* od, Address address,
684 Addend addend)
685 : rel_(relobj, local_sym_index, type, od, address), addend_(addend)
686 { }
687
688 Output_reloc(Sized_relobj<size, big_endian>* relobj,
689 unsigned int local_sym_index,
690 unsigned int type,
691 unsigned int shndx,
692 Address address,
693 Addend addend)
694 : rel_(relobj, local_sym_index, type, shndx, address),
695 addend_(addend)
696 { }
697
698 // A reloc against the STT_SECTION symbol of an output section.
699
700 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
701 Address address, Addend addend)
702 : rel_(os, type, od, address), addend_(addend)
703 { }
704
705 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
706 unsigned int shndx, Address address, Addend addend)
707 : rel_(os, type, relobj, shndx, address), addend_(addend)
708 { }
709
710 // Write the reloc entry to an output view.
711 void
712 write(unsigned char* pov) const;
713
714 private:
715 // The basic reloc.
716 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
717 // The addend.
718 Addend addend_;
719 };
720
721 // Output_data_reloc is used to manage a section containing relocs.
722 // SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
723 // indicates whether this is a dynamic relocation or a normal
724 // relocation. Output_data_reloc_base is a base class.
725 // Output_data_reloc is the real class, which we specialize based on
726 // the reloc type.
727
728 template<int sh_type, bool dynamic, int size, bool big_endian>
729 class Output_data_reloc_base : public Output_section_data
730 {
731 public:
732 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
733 typedef typename Output_reloc_type::Address Address;
734 static const int reloc_size =
735 Reloc_types<sh_type, size, big_endian>::reloc_size;
736
737 // Construct the section.
738 Output_data_reloc_base()
739 : Output_section_data(Output_data::default_alignment(size))
740 { }
741
742 // Write out the data.
743 void
744 do_write(Output_file*);
745
746 protected:
747 // Set the entry size and the link.
748 void
749 do_adjust_output_section(Output_section *os);
750
751 // Add a relocation entry.
752 void
753 add(const Output_reloc_type& reloc)
754 {
755 this->relocs_.push_back(reloc);
756 this->set_data_size(this->relocs_.size() * reloc_size);
757 }
758
759 private:
760 typedef std::vector<Output_reloc_type> Relocs;
761
762 Relocs relocs_;
763 };
764
765 // The class which callers actually create.
766
767 template<int sh_type, bool dynamic, int size, bool big_endian>
768 class Output_data_reloc;
769
770 // The SHT_REL version of Output_data_reloc.
771
772 template<bool dynamic, int size, bool big_endian>
773 class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
774 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
775 {
776 private:
777 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
778 big_endian> Base;
779
780 public:
781 typedef typename Base::Output_reloc_type Output_reloc_type;
782 typedef typename Output_reloc_type::Address Address;
783
784 Output_data_reloc()
785 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
786 { }
787
788 // Add a reloc against a global symbol.
789
790 void
791 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
792 { this->add(Output_reloc_type(gsym, type, od, address)); }
793
794 void
795 add_global(Symbol* gsym, unsigned int type, Relobj* relobj,
796 unsigned int shndx, Address address)
797 { this->add(Output_reloc_type(gsym, type, relobj, shndx, address)); }
798
799 // Add a reloc against a local symbol.
800
801 void
802 add_local(Sized_relobj<size, big_endian>* relobj,
803 unsigned int local_sym_index, unsigned int type,
804 Output_data* od, Address address)
805 { this->add(Output_reloc_type(relobj, local_sym_index, type, od, address)); }
806
807 void
808 add_local(Sized_relobj<size, big_endian>* relobj,
809 unsigned int local_sym_index, unsigned int type,
810 unsigned int shndx, Address address)
811 { this->add(Output_reloc_type(relobj, local_sym_index, type, shndx,
812 address)); }
813
814
815 // A reloc against the STT_SECTION symbol of an output section.
816
817 void
818 add_output_section(Output_section* os, unsigned int type,
819 Output_data* od, Address address)
820 { this->add(Output_reloc_type(os, type, od, address)); }
821
822 void
823 add_output_section(Output_section* os, unsigned int type,
824 Relobj* relobj, unsigned int shndx, Address address)
825 { this->add(Output_reloc_type(os, type, relobj, shndx, address)); }
826 };
827
828 // The SHT_RELA version of Output_data_reloc.
829
830 template<bool dynamic, int size, bool big_endian>
831 class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
832 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
833 {
834 private:
835 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
836 big_endian> Base;
837
838 public:
839 typedef typename Base::Output_reloc_type Output_reloc_type;
840 typedef typename Output_reloc_type::Address Address;
841 typedef typename Output_reloc_type::Addend Addend;
842
843 Output_data_reloc()
844 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
845 { }
846
847 // Add a reloc against a global symbol.
848
849 void
850 add_global(Symbol* gsym, unsigned int type, Output_data* od,
851 Address address, Addend addend)
852 { this->add(Output_reloc_type(gsym, type, od, address, addend)); }
853
854 void
855 add_global(Symbol* gsym, unsigned int type, Relobj* relobj,
856 unsigned int shndx, Address address, Addend addend)
857 { this->add(Output_reloc_type(gsym, type, relobj, shndx, address, addend)); }
858
859 // Add a reloc against a local symbol.
860
861 void
862 add_local(Sized_relobj<size, big_endian>* relobj,
863 unsigned int local_sym_index, unsigned int type,
864 Output_data* od, Address address, Addend addend)
865 {
866 this->add(Output_reloc_type(relobj, local_sym_index, type, od, address,
867 addend));
868 }
869
870 void
871 add_local(Sized_relobj<size, big_endian>* relobj,
872 unsigned int local_sym_index, unsigned int type,
873 unsigned int shndx, Address address, Addend addend)
874 {
875 this->add(Output_reloc_type(relobj, local_sym_index, type, shndx, address,
876 addend));
877 }
878
879 // A reloc against the STT_SECTION symbol of an output section.
880
881 void
882 add_output_section(Output_section* os, unsigned int type, Output_data* od,
883 Address address, Addend addend)
884 { this->add(Output_reloc_type(os, type, od, address, addend)); }
885
886 void
887 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
888 unsigned int shndx, Address address, Addend addend)
889 { this->add(Output_reloc_type(os, type, relobj, shndx, address, addend)); }
890 };
891
892 // Output_data_got is used to manage a GOT. Each entry in the GOT is
893 // for one symbol--either a global symbol or a local symbol in an
894 // object. The target specific code adds entries to the GOT as
895 // needed.
896
897 template<int size, bool big_endian>
898 class Output_data_got : public Output_section_data
899 {
900 public:
901 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
902
903 Output_data_got()
904 : Output_section_data(Output_data::default_alignment(size)), entries_()
905 { }
906
907 // Add an entry for a global symbol to the GOT. Return true if this
908 // is a new GOT entry, false if the symbol was already in the GOT.
909 bool
910 add_global(Symbol* gsym);
911
912 // Add an entry for a local symbol to the GOT. This returns the
913 // offset of the new entry from the start of the GOT.
914 unsigned int
915 add_local(Object* object, unsigned int sym_index)
916 {
917 this->entries_.push_back(Got_entry(object, sym_index));
918 this->set_got_size();
919 return this->last_got_offset();
920 }
921
922 // Add a constant to the GOT. This returns the offset of the new
923 // entry from the start of the GOT.
924 unsigned int
925 add_constant(Valtype constant)
926 {
927 this->entries_.push_back(Got_entry(constant));
928 this->set_got_size();
929 return this->last_got_offset();
930 }
931
932 // Write out the GOT table.
933 void
934 do_write(Output_file*);
935
936 private:
937 // This POD class holds a single GOT entry.
938 class Got_entry
939 {
940 public:
941 // Create a zero entry.
942 Got_entry()
943 : local_sym_index_(CONSTANT_CODE)
944 { this->u_.constant = 0; }
945
946 // Create a global symbol entry.
947 explicit Got_entry(Symbol* gsym)
948 : local_sym_index_(GSYM_CODE)
949 { this->u_.gsym = gsym; }
950
951 // Create a local symbol entry.
952 Got_entry(Object* object, unsigned int local_sym_index)
953 : local_sym_index_(local_sym_index)
954 {
955 gold_assert(local_sym_index != GSYM_CODE
956 && local_sym_index != CONSTANT_CODE);
957 this->u_.object = object;
958 }
959
960 // Create a constant entry. The constant is a host value--it will
961 // be swapped, if necessary, when it is written out.
962 explicit Got_entry(Valtype constant)
963 : local_sym_index_(CONSTANT_CODE)
964 { this->u_.constant = constant; }
965
966 // Write the GOT entry to an output view.
967 void
968 write(unsigned char* pov) const;
969
970 private:
971 enum
972 {
973 GSYM_CODE = -1U,
974 CONSTANT_CODE = -2U
975 };
976
977 union
978 {
979 // For a local symbol, the object.
980 Object* object;
981 // For a global symbol, the symbol.
982 Symbol* gsym;
983 // For a constant, the constant.
984 Valtype constant;
985 } u_;
986 // For a local symbol, the local symbol index. This is GSYM_CODE
987 // for a global symbol, or CONSTANT_CODE for a constant.
988 unsigned int local_sym_index_;
989 };
990
991 typedef std::vector<Got_entry> Got_entries;
992
993 // Return the offset into the GOT of GOT entry I.
994 unsigned int
995 got_offset(unsigned int i) const
996 { return i * (size / 8); }
997
998 // Return the offset into the GOT of the last entry added.
999 unsigned int
1000 last_got_offset() const
1001 { return this->got_offset(this->entries_.size() - 1); }
1002
1003 // Set the size of the section.
1004 void
1005 set_got_size()
1006 { this->set_data_size(this->got_offset(this->entries_.size())); }
1007
1008 // The list of GOT entries.
1009 Got_entries entries_;
1010 };
1011
1012 // Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1013 // section.
1014
1015 class Output_data_dynamic : public Output_section_data
1016 {
1017 public:
1018 Output_data_dynamic(Stringpool* pool)
1019 : Output_section_data(Output_data::default_alignment(
1020 parameters->get_size())),
1021 entries_(), pool_(pool)
1022 { }
1023
1024 // Add a new dynamic entry with a fixed numeric value.
1025 void
1026 add_constant(elfcpp::DT tag, unsigned int val)
1027 { this->add_entry(Dynamic_entry(tag, val)); }
1028
1029 // Add a new dynamic entry with the address of output data.
1030 void
1031 add_section_address(elfcpp::DT tag, const Output_data* od)
1032 { this->add_entry(Dynamic_entry(tag, od, false)); }
1033
1034 // Add a new dynamic entry with the size of output data.
1035 void
1036 add_section_size(elfcpp::DT tag, const Output_data* od)
1037 { this->add_entry(Dynamic_entry(tag, od, true)); }
1038
1039 // Add a new dynamic entry with the address of a symbol.
1040 void
1041 add_symbol(elfcpp::DT tag, const Symbol* sym)
1042 { this->add_entry(Dynamic_entry(tag, sym)); }
1043
1044 // Add a new dynamic entry with a string.
1045 void
1046 add_string(elfcpp::DT tag, const char* str)
1047 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, NULL))); }
1048
1049 void
1050 add_string(elfcpp::DT tag, const std::string& str)
1051 { this->add_string(tag, str.c_str()); }
1052
1053 // Set the final data size.
1054 void
1055 do_set_address(uint64_t, off_t);
1056
1057 // Write out the dynamic entries.
1058 void
1059 do_write(Output_file*);
1060
1061 protected:
1062 // Adjust the output section to set the entry size.
1063 void
1064 do_adjust_output_section(Output_section*);
1065
1066 private:
1067 // This POD class holds a single dynamic entry.
1068 class Dynamic_entry
1069 {
1070 public:
1071 // Create an entry with a fixed numeric value.
1072 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1073 : tag_(tag), classification_(DYNAMIC_NUMBER)
1074 { this->u_.val = val; }
1075
1076 // Create an entry with the size or address of a section.
1077 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1078 : tag_(tag),
1079 classification_(section_size
1080 ? DYNAMIC_SECTION_SIZE
1081 : DYNAMIC_SECTION_ADDRESS)
1082 { this->u_.od = od; }
1083
1084 // Create an entry with the address of a symbol.
1085 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1086 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1087 { this->u_.sym = sym; }
1088
1089 // Create an entry with a string.
1090 Dynamic_entry(elfcpp::DT tag, const char* str)
1091 : tag_(tag), classification_(DYNAMIC_STRING)
1092 { this->u_.str = str; }
1093
1094 // Write the dynamic entry to an output view.
1095 template<int size, bool big_endian>
1096 void
1097 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
1098
1099 private:
1100 enum Classification
1101 {
1102 // Number.
1103 DYNAMIC_NUMBER,
1104 // Section address.
1105 DYNAMIC_SECTION_ADDRESS,
1106 // Section size.
1107 DYNAMIC_SECTION_SIZE,
1108 // Symbol adress.
1109 DYNAMIC_SYMBOL,
1110 // String.
1111 DYNAMIC_STRING
1112 };
1113
1114 union
1115 {
1116 // For DYNAMIC_NUMBER.
1117 unsigned int val;
1118 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
1119 const Output_data* od;
1120 // For DYNAMIC_SYMBOL.
1121 const Symbol* sym;
1122 // For DYNAMIC_STRING.
1123 const char* str;
1124 } u_;
1125 // The dynamic tag.
1126 elfcpp::DT tag_;
1127 // The type of entry.
1128 Classification classification_;
1129 };
1130
1131 // Add an entry to the list.
1132 void
1133 add_entry(const Dynamic_entry& entry)
1134 { this->entries_.push_back(entry); }
1135
1136 // Sized version of write function.
1137 template<int size, bool big_endian>
1138 void
1139 sized_write(Output_file* of);
1140
1141 // The type of the list of entries.
1142 typedef std::vector<Dynamic_entry> Dynamic_entries;
1143
1144 // The entries.
1145 Dynamic_entries entries_;
1146 // The pool used for strings.
1147 Stringpool* pool_;
1148 };
1149
1150 // An output section. We don't expect to have too many output
1151 // sections, so we don't bother to do a template on the size.
1152
1153 class Output_section : public Output_data
1154 {
1155 public:
1156 // Create an output section, giving the name, type, and flags.
1157 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
1158 virtual ~Output_section();
1159
1160 // Add a new input section SHNDX, named NAME, with header SHDR, from
1161 // object OBJECT. Return the offset within the output section.
1162 template<int size, bool big_endian>
1163 off_t
1164 add_input_section(Relobj* object, unsigned int shndx, const char *name,
1165 const elfcpp::Shdr<size, big_endian>& shdr);
1166
1167 // Add generated data POSD to this output section.
1168 void
1169 add_output_section_data(Output_section_data* posd);
1170
1171 // Return the section name.
1172 const char*
1173 name() const
1174 { return this->name_; }
1175
1176 // Return the section type.
1177 elfcpp::Elf_Word
1178 type() const
1179 { return this->type_; }
1180
1181 // Return the section flags.
1182 elfcpp::Elf_Xword
1183 flags() const
1184 { return this->flags_; }
1185
1186 // Return the section index in the output file.
1187 unsigned int
1188 do_out_shndx() const
1189 { return this->out_shndx_; }
1190
1191 // Set the output section index.
1192 void
1193 do_set_out_shndx(unsigned int shndx)
1194 { this->out_shndx_ = shndx; }
1195
1196 // Return the entsize field.
1197 uint64_t
1198 entsize() const
1199 { return this->entsize_; }
1200
1201 // Set the entsize field.
1202 void
1203 set_entsize(uint64_t v);
1204
1205 // Set the link field to the output section index of a section.
1206 void
1207 set_link_section(const Output_data* od)
1208 {
1209 gold_assert(this->link_ == 0
1210 && !this->should_link_to_symtab_
1211 && !this->should_link_to_dynsym_);
1212 this->link_section_ = od;
1213 }
1214
1215 // Set the link field to a constant.
1216 void
1217 set_link(unsigned int v)
1218 {
1219 gold_assert(this->link_section_ == NULL
1220 && !this->should_link_to_symtab_
1221 && !this->should_link_to_dynsym_);
1222 this->link_ = v;
1223 }
1224
1225 // Record that this section should link to the normal symbol table.
1226 void
1227 set_should_link_to_symtab()
1228 {
1229 gold_assert(this->link_section_ == NULL
1230 && this->link_ == 0
1231 && !this->should_link_to_dynsym_);
1232 this->should_link_to_symtab_ = true;
1233 }
1234
1235 // Record that this section should link to the dynamic symbol table.
1236 void
1237 set_should_link_to_dynsym()
1238 {
1239 gold_assert(this->link_section_ == NULL
1240 && this->link_ == 0
1241 && !this->should_link_to_symtab_);
1242 this->should_link_to_dynsym_ = true;
1243 }
1244
1245 // Return the info field.
1246 unsigned int
1247 info() const
1248 {
1249 gold_assert(this->info_section_ == NULL);
1250 return this->info_;
1251 }
1252
1253 // Set the info field to the output section index of a section.
1254 void
1255 set_info_section(const Output_data* od)
1256 {
1257 gold_assert(this->info_ == 0);
1258 this->info_section_ = od;
1259 }
1260
1261 // Set the info field to a constant.
1262 void
1263 set_info(unsigned int v)
1264 {
1265 gold_assert(this->info_section_ == NULL);
1266 this->info_ = v;
1267 }
1268
1269 // Set the addralign field.
1270 void
1271 set_addralign(uint64_t v)
1272 { this->addralign_ = v; }
1273
1274 // Indicate that we need a symtab index.
1275 void
1276 set_needs_symtab_index()
1277 { this->needs_symtab_index_ = true; }
1278
1279 // Return whether we need a symtab index.
1280 bool
1281 needs_symtab_index() const
1282 { return this->needs_symtab_index_; }
1283
1284 // Get the symtab index.
1285 unsigned int
1286 symtab_index() const
1287 {
1288 gold_assert(this->symtab_index_ != 0);
1289 return this->symtab_index_;
1290 }
1291
1292 // Set the symtab index.
1293 void
1294 set_symtab_index(unsigned int index)
1295 {
1296 gold_assert(index != 0);
1297 this->symtab_index_ = index;
1298 }
1299
1300 // Indicate that we need a dynsym index.
1301 void
1302 set_needs_dynsym_index()
1303 { this->needs_dynsym_index_ = true; }
1304
1305 // Return whether we need a dynsym index.
1306 bool
1307 needs_dynsym_index() const
1308 { return this->needs_dynsym_index_; }
1309
1310 // Get the dynsym index.
1311 unsigned int
1312 dynsym_index() const
1313 {
1314 gold_assert(this->dynsym_index_ != 0);
1315 return this->dynsym_index_;
1316 }
1317
1318 // Set the dynsym index.
1319 void
1320 set_dynsym_index(unsigned int index)
1321 {
1322 gold_assert(index != 0);
1323 this->dynsym_index_ = index;
1324 }
1325
1326 // Return the output virtual address of OFFSET relative to the start
1327 // of input section SHNDX in object OBJECT.
1328 uint64_t
1329 output_address(const Relobj* object, unsigned int shndx,
1330 off_t offset) const;
1331
1332 // Set the address of the Output_section. For a typical
1333 // Output_section, there is nothing to do, but if there are any
1334 // Output_section_data objects we need to set the final addresses
1335 // here.
1336 void
1337 do_set_address(uint64_t, off_t);
1338
1339 // Write the data to the file. For a typical Output_section, this
1340 // does nothing: the data is written out by calling Object::Relocate
1341 // on each input object. But if there are any Output_section_data
1342 // objects we do need to write them out here.
1343 void
1344 do_write(Output_file*);
1345
1346 // Return the address alignment--function required by parent class.
1347 uint64_t
1348 do_addralign() const
1349 { return this->addralign_; }
1350
1351 // Return whether this is an Output_section.
1352 bool
1353 do_is_section() const
1354 { return true; }
1355
1356 // Return whether this is a section of the specified type.
1357 bool
1358 do_is_section_type(elfcpp::Elf_Word type) const
1359 { return this->type_ == type; }
1360
1361 // Return whether the specified section flag is set.
1362 bool
1363 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
1364 { return (this->flags_ & flag) != 0; }
1365
1366 // Write the section header into *OPHDR.
1367 template<int size, bool big_endian>
1368 void
1369 write_header(const Layout*, const Stringpool*,
1370 elfcpp::Shdr_write<size, big_endian>*) const;
1371
1372 private:
1373 // In some cases we need to keep a list of the input sections
1374 // associated with this output section. We only need the list if we
1375 // might have to change the offsets of the input section within the
1376 // output section after we add the input section. The ordinary
1377 // input sections will be written out when we process the object
1378 // file, and as such we don't need to track them here. We do need
1379 // to track Output_section_data objects here. We store instances of
1380 // this structure in a std::vector, so it must be a POD. There can
1381 // be many instances of this structure, so we use a union to save
1382 // some space.
1383 class Input_section
1384 {
1385 public:
1386 Input_section()
1387 : shndx_(0), p2align_(0)
1388 {
1389 this->u1_.data_size = 0;
1390 this->u2_.object = NULL;
1391 }
1392
1393 // For an ordinary input section.
1394 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
1395 uint64_t addralign)
1396 : shndx_(shndx),
1397 p2align_(ffsll(static_cast<long long>(addralign)))
1398 {
1399 gold_assert(shndx != OUTPUT_SECTION_CODE
1400 && shndx != MERGE_DATA_SECTION_CODE
1401 && shndx != MERGE_STRING_SECTION_CODE);
1402 this->u1_.data_size = data_size;
1403 this->u2_.object = object;
1404 }
1405
1406 // For a non-merge output section.
1407 Input_section(Output_section_data* posd)
1408 : shndx_(OUTPUT_SECTION_CODE),
1409 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1410 {
1411 this->u1_.data_size = 0;
1412 this->u2_.posd = posd;
1413 }
1414
1415 // For a merge section.
1416 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
1417 : shndx_(is_string
1418 ? MERGE_STRING_SECTION_CODE
1419 : MERGE_DATA_SECTION_CODE),
1420 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1421 {
1422 this->u1_.entsize = entsize;
1423 this->u2_.posd = posd;
1424 }
1425
1426 // The required alignment.
1427 uint64_t
1428 addralign() const
1429 {
1430 return (this->p2align_ == 0
1431 ? 0
1432 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
1433 }
1434
1435 // Return the required size.
1436 off_t
1437 data_size() const;
1438
1439 // Return whether this is a merge section which matches the
1440 // parameters.
1441 bool
1442 is_merge_section(bool is_string, uint64_t entsize) const
1443 {
1444 return (this->shndx_ == (is_string
1445 ? MERGE_STRING_SECTION_CODE
1446 : MERGE_DATA_SECTION_CODE)
1447 && this->u1_.entsize == entsize);
1448 }
1449
1450 // Set the output section.
1451 void
1452 set_output_section(Output_section* os)
1453 {
1454 gold_assert(!this->is_input_section());
1455 this->u2_.posd->set_output_section(os);
1456 }
1457
1458 // Set the address and file offset. This is called during
1459 // Layout::finalize. SECOFF is the file offset of the enclosing
1460 // section.
1461 void
1462 set_address(uint64_t addr, off_t off, off_t secoff);
1463
1464 // Add an input section, for SHF_MERGE sections.
1465 bool
1466 add_input_section(Relobj* object, unsigned int shndx)
1467 {
1468 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
1469 || this->shndx_ == MERGE_STRING_SECTION_CODE);
1470 return this->u2_.posd->add_input_section(object, shndx);
1471 }
1472
1473 // Given an input OBJECT, an input section index SHNDX within that
1474 // object, and an OFFSET relative to the start of that input
1475 // section, return whether or not the output address is known.
1476 // OUTPUT_SECTION_ADDRESS is the address of the output section
1477 // which this is a part of. If this function returns true, it
1478 // sets *POUTPUT to the output address.
1479 bool
1480 output_address(const Relobj* object, unsigned int shndx, off_t offset,
1481 uint64_t output_section_address, uint64_t *poutput) const;
1482
1483 // Write out the data. This does nothing for an input section.
1484 void
1485 write(Output_file*);
1486
1487 private:
1488 // Code values which appear in shndx_. If the value is not one of
1489 // these codes, it is the input section index in the object file.
1490 enum
1491 {
1492 // An Output_section_data.
1493 OUTPUT_SECTION_CODE = -1U,
1494 // An Output_section_data for an SHF_MERGE section with
1495 // SHF_STRINGS not set.
1496 MERGE_DATA_SECTION_CODE = -2U,
1497 // An Output_section_data for an SHF_MERGE section with
1498 // SHF_STRINGS set.
1499 MERGE_STRING_SECTION_CODE = -3U
1500 };
1501
1502 // Whether this is an input section.
1503 bool
1504 is_input_section() const
1505 {
1506 return (this->shndx_ != OUTPUT_SECTION_CODE
1507 && this->shndx_ != MERGE_DATA_SECTION_CODE
1508 && this->shndx_ != MERGE_STRING_SECTION_CODE);
1509 }
1510
1511 // For an ordinary input section, this is the section index in the
1512 // input file. For an Output_section_data, this is
1513 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1514 // MERGE_STRING_SECTION_CODE.
1515 unsigned int shndx_;
1516 // The required alignment, stored as a power of 2.
1517 unsigned int p2align_;
1518 union
1519 {
1520 // For an ordinary input section, the section size.
1521 off_t data_size;
1522 // For OUTPUT_SECTION_CODE, this is not used. For
1523 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1524 // entity size.
1525 uint64_t entsize;
1526 } u1_;
1527 union
1528 {
1529 // For an ordinary input section, the object which holds the
1530 // input section.
1531 Relobj* object;
1532 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1533 // MERGE_STRING_SECTION_CODE, the data.
1534 Output_section_data* posd;
1535 } u2_;
1536 };
1537
1538 typedef std::vector<Input_section> Input_section_list;
1539
1540 // Fill data. This is used to fill in data between input sections.
1541 // When we have to keep track of the input sections, we can use an
1542 // Output_data_const, but we don't want to have to keep track of
1543 // input sections just to implement fills. For a fill we record the
1544 // offset, and the actual data to be written out.
1545 class Fill
1546 {
1547 public:
1548 Fill(off_t section_offset, off_t length)
1549 : section_offset_(section_offset), length_(length)
1550 { }
1551
1552 // Return section offset.
1553 off_t
1554 section_offset() const
1555 { return this->section_offset_; }
1556
1557 // Return fill length.
1558 off_t
1559 length() const
1560 { return this->length_; }
1561
1562 private:
1563 // The offset within the output section.
1564 off_t section_offset_;
1565 // The length of the space to fill.
1566 off_t length_;
1567 };
1568
1569 typedef std::vector<Fill> Fill_list;
1570
1571 // Add a new output section by Input_section.
1572 void
1573 add_output_section_data(Input_section*);
1574
1575 // Add an SHF_MERGE input section. Returns true if the section was
1576 // handled.
1577 bool
1578 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
1579 uint64_t entsize, uint64_t addralign);
1580
1581 // Add an output SHF_MERGE section POSD to this output section.
1582 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1583 // ENTSIZE is the entity size. This returns the entry added to
1584 // input_sections_.
1585 void
1586 add_output_merge_section(Output_section_data* posd, bool is_string,
1587 uint64_t entsize);
1588
1589 // Most of these fields are only valid after layout.
1590
1591 // The name of the section. This will point into a Stringpool.
1592 const char* name_;
1593 // The section address is in the parent class.
1594 // The section alignment.
1595 uint64_t addralign_;
1596 // The section entry size.
1597 uint64_t entsize_;
1598 // The file offset is in the parent class.
1599 // Set the section link field to the index of this section.
1600 const Output_data* link_section_;
1601 // If link_section_ is NULL, this is the link field.
1602 unsigned int link_;
1603 // Set the section info field to the index of this section.
1604 const Output_data* info_section_;
1605 // If info_section_ is NULL, this is the section info field.
1606 unsigned int info_;
1607 // The section type.
1608 elfcpp::Elf_Word type_;
1609 // The section flags.
1610 elfcpp::Elf_Xword flags_;
1611 // The section index.
1612 unsigned int out_shndx_;
1613 // If there is a STT_SECTION for this output section in the normal
1614 // symbol table, this is the symbol index. This starts out as zero.
1615 // It is initialized in Layout::finalize() to be the index, or -1U
1616 // if there isn't one.
1617 unsigned int symtab_index_;
1618 // If there is a STT_SECTION for this output section in the dynamic
1619 // symbol table, this is the symbol index. This starts out as zero.
1620 // It is initialized in Layout::finalize() to be the index, or -1U
1621 // if there isn't one.
1622 unsigned int dynsym_index_;
1623 // The input sections. This will be empty in cases where we don't
1624 // need to keep track of them.
1625 Input_section_list input_sections_;
1626 // The offset of the first entry in input_sections_.
1627 off_t first_input_offset_;
1628 // The fill data. This is separate from input_sections_ because we
1629 // often will need fill sections without needing to keep track of
1630 // input sections.
1631 Fill_list fills_;
1632 // Whether this output section needs a STT_SECTION symbol in the
1633 // normal symbol table. This will be true if there is a relocation
1634 // which needs it.
1635 bool needs_symtab_index_ : 1;
1636 // Whether this output section needs a STT_SECTION symbol in the
1637 // dynamic symbol table. This will be true if there is a dynamic
1638 // relocation which needs it.
1639 bool needs_dynsym_index_ : 1;
1640 // Whether the link field of this output section should point to the
1641 // normal symbol table.
1642 bool should_link_to_symtab_ : 1;
1643 // Whether the link field of this output section should point to the
1644 // dynamic symbol table.
1645 bool should_link_to_dynsym_ : 1;
1646 };
1647
1648 // An output segment. PT_LOAD segments are built from collections of
1649 // output sections. Other segments typically point within PT_LOAD
1650 // segments, and are built directly as needed.
1651
1652 class Output_segment
1653 {
1654 public:
1655 // Create an output segment, specifying the type and flags.
1656 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
1657
1658 // Return the virtual address.
1659 uint64_t
1660 vaddr() const
1661 { return this->vaddr_; }
1662
1663 // Return the physical address.
1664 uint64_t
1665 paddr() const
1666 { return this->paddr_; }
1667
1668 // Return the segment type.
1669 elfcpp::Elf_Word
1670 type() const
1671 { return this->type_; }
1672
1673 // Return the segment flags.
1674 elfcpp::Elf_Word
1675 flags() const
1676 { return this->flags_; }
1677
1678 // Return the memory size.
1679 uint64_t
1680 memsz() const
1681 { return this->memsz_; }
1682
1683 // Return the file size.
1684 off_t
1685 filesz() const
1686 { return this->filesz_; }
1687
1688 // Return the maximum alignment of the Output_data.
1689 uint64_t
1690 addralign();
1691
1692 // Add an Output_section to this segment.
1693 void
1694 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1695 { this->add_output_section(os, seg_flags, false); }
1696
1697 // Add an Output_section to the start of this segment.
1698 void
1699 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
1700 { this->add_output_section(os, seg_flags, true); }
1701
1702 // Add an Output_data (which is not an Output_section) to the start
1703 // of this segment.
1704 void
1705 add_initial_output_data(Output_data*);
1706
1707 // Set the address of the segment to ADDR and the offset to *POFF
1708 // (aligned if necessary), and set the addresses and offsets of all
1709 // contained output sections accordingly. Set the section indexes
1710 // of all contained output sections starting with *PSHNDX. Return
1711 // the address of the immediately following segment. Update *POFF
1712 // and *PSHNDX. This should only be called for a PT_LOAD segment.
1713 uint64_t
1714 set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx);
1715
1716 // Set the minimum alignment of this segment. This may be adjusted
1717 // upward based on the section alignments.
1718 void
1719 set_minimum_addralign(uint64_t align)
1720 {
1721 gold_assert(!this->is_align_known_);
1722 this->align_ = align;
1723 }
1724
1725 // Set the offset of this segment based on the section. This should
1726 // only be called for a non-PT_LOAD segment.
1727 void
1728 set_offset();
1729
1730 // Return the number of output sections.
1731 unsigned int
1732 output_section_count() const;
1733
1734 // Write the segment header into *OPHDR.
1735 template<int size, bool big_endian>
1736 void
1737 write_header(elfcpp::Phdr_write<size, big_endian>*);
1738
1739 // Write the section headers of associated sections into V.
1740 template<int size, bool big_endian>
1741 unsigned char*
1742 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
1743 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
1744
1745 private:
1746 Output_segment(const Output_segment&);
1747 Output_segment& operator=(const Output_segment&);
1748
1749 typedef std::list<Output_data*> Output_data_list;
1750
1751 // Add an Output_section to this segment, specifying front or back.
1752 void
1753 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
1754 bool front);
1755
1756 // Find the maximum alignment in an Output_data_list.
1757 static uint64_t
1758 maximum_alignment(const Output_data_list*);
1759
1760 // Set the section addresses in an Output_data_list.
1761 uint64_t
1762 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff,
1763 unsigned int* pshndx);
1764
1765 // Return the number of Output_sections in an Output_data_list.
1766 unsigned int
1767 output_section_count_list(const Output_data_list*) const;
1768
1769 // Write the section headers in the list into V.
1770 template<int size, bool big_endian>
1771 unsigned char*
1772 write_section_headers_list(const Layout*, const Stringpool*,
1773 const Output_data_list*, unsigned char* v,
1774 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
1775
1776 // The list of output data with contents attached to this segment.
1777 Output_data_list output_data_;
1778 // The list of output data without contents attached to this segment.
1779 Output_data_list output_bss_;
1780 // The segment virtual address.
1781 uint64_t vaddr_;
1782 // The segment physical address.
1783 uint64_t paddr_;
1784 // The size of the segment in memory.
1785 uint64_t memsz_;
1786 // The segment alignment. The is_align_known_ field indicates
1787 // whether this has been finalized. It can be set to a minimum
1788 // value before it is finalized.
1789 uint64_t align_;
1790 // The offset of the segment data within the file.
1791 off_t offset_;
1792 // The size of the segment data in the file.
1793 off_t filesz_;
1794 // The segment type;
1795 elfcpp::Elf_Word type_;
1796 // The segment flags.
1797 elfcpp::Elf_Word flags_;
1798 // Whether we have finalized align_.
1799 bool is_align_known_;
1800 };
1801
1802 // This class represents the output file.
1803
1804 class Output_file
1805 {
1806 public:
1807 Output_file(const General_options& options, Target*);
1808
1809 // Get a pointer to the target.
1810 Target*
1811 target() const
1812 { return this->target_; }
1813
1814 // Open the output file. FILE_SIZE is the final size of the file.
1815 void
1816 open(off_t file_size);
1817
1818 // Close the output file and make sure there are no error.
1819 void
1820 close();
1821
1822 // We currently always use mmap which makes the view handling quite
1823 // simple. In the future we may support other approaches.
1824
1825 // Write data to the output file.
1826 void
1827 write(off_t offset, const void* data, off_t len)
1828 { memcpy(this->base_ + offset, data, len); }
1829
1830 // Get a buffer to use to write to the file, given the offset into
1831 // the file and the size.
1832 unsigned char*
1833 get_output_view(off_t start, off_t size)
1834 {
1835 gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
1836 return this->base_ + start;
1837 }
1838
1839 // VIEW must have been returned by get_output_view. Write the
1840 // buffer to the file, passing in the offset and the size.
1841 void
1842 write_output_view(off_t, off_t, unsigned char*)
1843 { }
1844
1845 private:
1846 // General options.
1847 const General_options& options_;
1848 // Target.
1849 Target* target_;
1850 // File name.
1851 const char* name_;
1852 // File descriptor.
1853 int o_;
1854 // File size.
1855 off_t file_size_;
1856 // Base of file mapped into memory.
1857 unsigned char* base_;
1858 };
1859
1860 } // End namespace gold.
1861
1862 #endif // !defined(GOLD_OUTPUT_H)
This page took 0.068619 seconds and 4 git commands to generate.