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