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