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