OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.20.1/] [gold/] [output.h] - Blame information for rev 339

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 205 julius
// output.h -- manage the output file for gold   -*- C++ -*-
2
 
3
// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4
// Written by Ian Lance Taylor <iant@google.com>.
5
 
6
// This file is part of gold.
7
 
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License as published by
10
// the Free Software Foundation; either version 3 of the License, or
11
// (at your option) any later version.
12
 
13
// This program is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
// GNU General Public License for more details.
17
 
18
// You should have received a copy of the GNU General Public License
19
// along with this program; if not, write to the Free Software
20
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
// MA 02110-1301, USA.
22
 
23
#ifndef GOLD_OUTPUT_H
24
#define GOLD_OUTPUT_H
25
 
26
#include <list>
27
#include <vector>
28
 
29
#include "elfcpp.h"
30
#include "mapfile.h"
31
#include "layout.h"
32
#include "reloc-types.h"
33
 
34
namespace gold
35
{
36
 
37
class General_options;
38
class Object;
39
class Symbol;
40
class Output_file;
41
class Output_merge_base;
42
class Output_section;
43
class Relocatable_relocs;
44
class Target;
45
template<int size, bool big_endian>
46
class Sized_target;
47
template<int size, bool big_endian>
48
class Sized_relobj;
49
 
50
// This class specifies an input section.  It is used as a key type
51
// for maps.
52
 
53
class Input_section_specifier
54
{
55
 public:
56
  Input_section_specifier(const Relobj* relobj, unsigned int shndx)
57
    : relobj_(relobj), shndx_(shndx)
58
  { }
59
 
60
  // Return Relobj of this.
61
  const Relobj*
62
  relobj() const
63
  { return this->relobj_; }
64
 
65
  // Return section index of this.
66
  unsigned int
67
  shndx() const
68
  { return this->shndx_; }
69
 
70
  // Whether this equals to another specifier ISS.
71
  bool
72
  eq(const Input_section_specifier& iss) const
73
  { return this->relobj_ == iss.relobj_ && this->shndx_ == iss.shndx_; }
74
 
75
  // Compute a hash value of this.
76
  size_t
77
  hash_value() const
78
  { return this->string_hash(this->relobj_->name().c_str()) ^ this->shndx_; }
79
 
80
  // Functors for containers.
81
  struct equal_to
82
  {
83
    bool
84
    operator()(const Input_section_specifier& iss1,
85
               const Input_section_specifier& iss2) const
86
    { return iss1.eq(iss2); }
87
  };
88
 
89
  struct hash
90
  {
91
    size_t
92
    operator()(const Input_section_specifier& iss) const
93
    { return iss.hash_value(); }
94
  };
95
 
96
 private:
97
  // For portability, we use our own string hash function instead of assuming
98
  // __gnu_cxx::hash or std::tr1::hash is available.  This is the same hash
99
  // function used in Stringpool_template::string_hash.
100
  static size_t
101
  string_hash(const char* s)
102
  {
103
    size_t h = 5381;
104
    while (*s != '\0')
105
      h = h * 33 + *s++;
106
    return h;
107
  }
108
 
109
  // An object.
110
  const Relobj* relobj_;
111
  // A section index. 
112
  unsigned int shndx_;
113
};
114
 
115
// An abtract class for data which has to go into the output file.
116
 
117
class Output_data
118
{
119
 public:
120
  explicit Output_data()
121
    : address_(0), data_size_(0), offset_(-1),
122
      is_address_valid_(false), is_data_size_valid_(false),
123
      is_offset_valid_(false), is_data_size_fixed_(false),
124
      dynamic_reloc_count_(0)
125
  { }
126
 
127
  virtual
128
  ~Output_data();
129
 
130
  // Return the address.  For allocated sections, this is only valid
131
  // after Layout::finalize is finished.
132
  uint64_t
133
  address() const
134
  {
135
    gold_assert(this->is_address_valid_);
136
    return this->address_;
137
  }
138
 
139
  // Return the size of the data.  For allocated sections, this must
140
  // be valid after Layout::finalize calls set_address, but need not
141
  // be valid before then.
142
  off_t
143
  data_size() const
144
  {
145
    gold_assert(this->is_data_size_valid_);
146
    return this->data_size_;
147
  }
148
 
149
  // Return true if data size is fixed.
150
  bool
151
  is_data_size_fixed() const
152
  { return this->is_data_size_fixed_; }
153
 
154
  // Return the file offset.  This is only valid after
155
  // Layout::finalize is finished.  For some non-allocated sections,
156
  // it may not be valid until near the end of the link.
157
  off_t
158
  offset() const
159
  {
160
    gold_assert(this->is_offset_valid_);
161
    return this->offset_;
162
  }
163
 
164
  // Reset the address and file offset.  This essentially disables the
165
  // sanity testing about duplicate and unknown settings.
166
  void
167
  reset_address_and_file_offset()
168
  {
169
    this->is_address_valid_ = false;
170
    this->is_offset_valid_ = false;
171
    if (!this->is_data_size_fixed_)
172
      this->is_data_size_valid_ = false;
173
    this->do_reset_address_and_file_offset();
174
  }
175
 
176
  // Return true if address and file offset already have reset values. In
177
  // other words, calling reset_address_and_file_offset will not change them.
178
  bool
179
  address_and_file_offset_have_reset_values() const
180
  { return this->do_address_and_file_offset_have_reset_values(); }
181
 
182
  // Return the required alignment.
183
  uint64_t
184
  addralign() const
185
  { return this->do_addralign(); }
186
 
187
  // Return whether this has a load address.
188
  bool
189
  has_load_address() const
190
  { return this->do_has_load_address(); }
191
 
192
  // Return the load address.
193
  uint64_t
194
  load_address() const
195
  { return this->do_load_address(); }
196
 
197
  // Return whether this is an Output_section.
198
  bool
199
  is_section() const
200
  { return this->do_is_section(); }
201
 
202
  // Return whether this is an Output_section of the specified type.
203
  bool
204
  is_section_type(elfcpp::Elf_Word stt) const
205
  { return this->do_is_section_type(stt); }
206
 
207
  // Return whether this is an Output_section with the specified flag
208
  // set.
209
  bool
210
  is_section_flag_set(elfcpp::Elf_Xword shf) const
211
  { return this->do_is_section_flag_set(shf); }
212
 
213
  // Return the output section that this goes in, if there is one.
214
  Output_section*
215
  output_section()
216
  { return this->do_output_section(); }
217
 
218
  // Return the output section index, if there is an output section.
219
  unsigned int
220
  out_shndx() const
221
  { return this->do_out_shndx(); }
222
 
223
  // Set the output section index, if this is an output section.
224
  void
225
  set_out_shndx(unsigned int shndx)
226
  { this->do_set_out_shndx(shndx); }
227
 
228
  // Set the address and file offset of this data, and finalize the
229
  // size of the data.  This is called during Layout::finalize for
230
  // allocated sections.
231
  void
232
  set_address_and_file_offset(uint64_t addr, off_t off)
233
  {
234
    this->set_address(addr);
235
    this->set_file_offset(off);
236
    this->finalize_data_size();
237
  }
238
 
239
  // Set the address.
240
  void
241
  set_address(uint64_t addr)
242
  {
243
    gold_assert(!this->is_address_valid_);
244
    this->address_ = addr;
245
    this->is_address_valid_ = true;
246
  }
247
 
248
  // Set the file offset.
249
  void
250
  set_file_offset(off_t off)
251
  {
252
    gold_assert(!this->is_offset_valid_);
253
    this->offset_ = off;
254
    this->is_offset_valid_ = true;
255
  }
256
 
257
  // Finalize the data size.
258
  void
259
  finalize_data_size()
260
  {
261
    if (!this->is_data_size_valid_)
262
      {
263
        // Tell the child class to set the data size.
264
        this->set_final_data_size();
265
        gold_assert(this->is_data_size_valid_);
266
      }
267
  }
268
 
269
  // Set the TLS offset.  Called only for SHT_TLS sections.
270
  void
271
  set_tls_offset(uint64_t tls_base)
272
  { this->do_set_tls_offset(tls_base); }
273
 
274
  // Return the TLS offset, relative to the base of the TLS segment.
275
  // Valid only for SHT_TLS sections.
276
  uint64_t
277
  tls_offset() const
278
  { return this->do_tls_offset(); }
279
 
280
  // Write the data to the output file.  This is called after
281
  // Layout::finalize is complete.
282
  void
283
  write(Output_file* file)
284
  { this->do_write(file); }
285
 
286
  // This is called by Layout::finalize to note that the sizes of
287
  // allocated sections must now be fixed.
288
  static void
289
  layout_complete()
290
  { Output_data::allocated_sizes_are_fixed = true; }
291
 
292
  // Used to check that layout has been done.
293
  static bool
294
  is_layout_complete()
295
  { return Output_data::allocated_sizes_are_fixed; }
296
 
297
  // Count the number of dynamic relocations applied to this section.
298
  void
299
  add_dynamic_reloc()
300
  { ++this->dynamic_reloc_count_; }
301
 
302
  // Return the number of dynamic relocations applied to this section.
303
  unsigned int
304
  dynamic_reloc_count() const
305
  { return this->dynamic_reloc_count_; }
306
 
307
  // Whether the address is valid.
308
  bool
309
  is_address_valid() const
310
  { return this->is_address_valid_; }
311
 
312
  // Whether the file offset is valid.
313
  bool
314
  is_offset_valid() const
315
  { return this->is_offset_valid_; }
316
 
317
  // Whether the data size is valid.
318
  bool
319
  is_data_size_valid() const
320
  { return this->is_data_size_valid_; }
321
 
322
  // Print information to the map file.
323
  void
324
  print_to_mapfile(Mapfile* mapfile) const
325
  { return this->do_print_to_mapfile(mapfile); }
326
 
327
 protected:
328
  // Functions that child classes may or in some cases must implement.
329
 
330
  // Write the data to the output file.
331
  virtual void
332
  do_write(Output_file*) = 0;
333
 
334
  // Return the required alignment.
335
  virtual uint64_t
336
  do_addralign() const = 0;
337
 
338
  // Return whether this has a load address.
339
  virtual bool
340
  do_has_load_address() const
341
  { return false; }
342
 
343
  // Return the load address.
344
  virtual uint64_t
345
  do_load_address() const
346
  { gold_unreachable(); }
347
 
348
  // Return whether this is an Output_section.
349
  virtual bool
350
  do_is_section() const
351
  { return false; }
352
 
353
  // Return whether this is an Output_section of the specified type.
354
  // This only needs to be implement by Output_section.
355
  virtual bool
356
  do_is_section_type(elfcpp::Elf_Word) const
357
  { return false; }
358
 
359
  // Return whether this is an Output_section with the specific flag
360
  // set.  This only needs to be implemented by Output_section.
361
  virtual bool
362
  do_is_section_flag_set(elfcpp::Elf_Xword) const
363
  { return false; }
364
 
365
  // Return the output section, if there is one.
366
  virtual Output_section*
367
  do_output_section()
368
  { return NULL; }
369
 
370
  // Return the output section index, if there is an output section.
371
  virtual unsigned int
372
  do_out_shndx() const
373
  { gold_unreachable(); }
374
 
375
  // Set the output section index, if this is an output section.
376
  virtual void
377
  do_set_out_shndx(unsigned int)
378
  { gold_unreachable(); }
379
 
380
  // This is a hook for derived classes to set the data size.  This is
381
  // called by finalize_data_size, normally called during
382
  // Layout::finalize, when the section address is set.
383
  virtual void
384
  set_final_data_size()
385
  { gold_unreachable(); }
386
 
387
  // A hook for resetting the address and file offset.
388
  virtual void
389
  do_reset_address_and_file_offset()
390
  { }
391
 
392
  // Return true if address and file offset already have reset values. In
393
  // other words, calling reset_address_and_file_offset will not change them.
394
  // A child class overriding do_reset_address_and_file_offset may need to
395
  // also override this.
396
  virtual bool
397
  do_address_and_file_offset_have_reset_values() const
398
  { return !this->is_address_valid_ && !this->is_offset_valid_; }
399
 
400
  // Set the TLS offset.  Called only for SHT_TLS sections.
401
  virtual void
402
  do_set_tls_offset(uint64_t)
403
  { gold_unreachable(); }
404
 
405
  // Return the TLS offset, relative to the base of the TLS segment.
406
  // Valid only for SHT_TLS sections.
407
  virtual uint64_t
408
  do_tls_offset() const
409
  { gold_unreachable(); }
410
 
411
  // Print to the map file.  This only needs to be implemented by
412
  // classes which may appear in a PT_LOAD segment.
413
  virtual void
414
  do_print_to_mapfile(Mapfile*) const
415
  { gold_unreachable(); }
416
 
417
  // Functions that child classes may call.
418
 
419
  // Reset the address.  The Output_section class needs this when an
420
  // SHF_ALLOC input section is added to an output section which was
421
  // formerly not SHF_ALLOC.
422
  void
423
  mark_address_invalid()
424
  { this->is_address_valid_ = false; }
425
 
426
  // Set the size of the data.
427
  void
428
  set_data_size(off_t data_size)
429
  {
430
    gold_assert(!this->is_data_size_valid_
431
                && !this->is_data_size_fixed_);
432
    this->data_size_ = data_size;
433
    this->is_data_size_valid_ = true;
434
  }
435
 
436
  // Fix the data size.  Once it is fixed, it cannot be changed
437
  // and the data size remains always valid. 
438
  void
439
  fix_data_size()
440
  {
441
    gold_assert(this->is_data_size_valid_);
442
    this->is_data_size_fixed_ = true;
443
  }
444
 
445
  // Get the current data size--this is for the convenience of
446
  // sections which build up their size over time.
447
  off_t
448
  current_data_size_for_child() const
449
  { return this->data_size_; }
450
 
451
  // Set the current data size--this is for the convenience of
452
  // sections which build up their size over time.
453
  void
454
  set_current_data_size_for_child(off_t data_size)
455
  {
456
    gold_assert(!this->is_data_size_valid_);
457
    this->data_size_ = data_size;
458
  }
459
 
460
  // Return default alignment for the target size.
461
  static uint64_t
462
  default_alignment();
463
 
464
  // Return default alignment for a specified size--32 or 64.
465
  static uint64_t
466
  default_alignment_for_size(int size);
467
 
468
 private:
469
  Output_data(const Output_data&);
470
  Output_data& operator=(const Output_data&);
471
 
472
  // This is used for verification, to make sure that we don't try to
473
  // change any sizes of allocated sections after we set the section
474
  // addresses.
475
  static bool allocated_sizes_are_fixed;
476
 
477
  // Memory address in output file.
478
  uint64_t address_;
479
  // Size of data in output file.
480
  off_t data_size_;
481
  // File offset of contents in output file.
482
  off_t offset_;
483
  // Whether address_ is valid.
484
  bool is_address_valid_;
485
  // Whether data_size_ is valid.
486
  bool is_data_size_valid_;
487
  // Whether offset_ is valid.
488
  bool is_offset_valid_;
489
  // Whether data size is fixed.
490
  bool is_data_size_fixed_;
491
  // Count of dynamic relocations applied to this section.
492
  unsigned int dynamic_reloc_count_;
493
};
494
 
495
// Output the section headers.
496
 
497
class Output_section_headers : public Output_data
498
{
499
 public:
500
  Output_section_headers(const Layout*,
501
                         const Layout::Segment_list*,
502
                         const Layout::Section_list*,
503
                         const Layout::Section_list*,
504
                         const Stringpool*,
505
                         const Output_section*);
506
 
507
 protected:
508
  // Write the data to the file.
509
  void
510
  do_write(Output_file*);
511
 
512
  // Return the required alignment.
513
  uint64_t
514
  do_addralign() const
515
  { return Output_data::default_alignment(); }
516
 
517
  // Write to a map file.
518
  void
519
  do_print_to_mapfile(Mapfile* mapfile) const
520
  { mapfile->print_output_data(this, _("** section headers")); }
521
 
522
  // Set final data size.
523
  void
524
  set_final_data_size()
525
  { this->set_data_size(this->do_size()); }
526
 
527
 private:
528
  // Write the data to the file with the right size and endianness.
529
  template<int size, bool big_endian>
530
  void
531
  do_sized_write(Output_file*);
532
 
533
  // Compute data size.
534
  off_t
535
  do_size() const;
536
 
537
  const Layout* layout_;
538
  const Layout::Segment_list* segment_list_;
539
  const Layout::Section_list* section_list_;
540
  const Layout::Section_list* unattached_section_list_;
541
  const Stringpool* secnamepool_;
542
  const Output_section* shstrtab_section_;
543
};
544
 
545
// Output the segment headers.
546
 
547
class Output_segment_headers : public Output_data
548
{
549
 public:
550
  Output_segment_headers(const Layout::Segment_list& segment_list);
551
 
552
 protected:
553
  // Write the data to the file.
554
  void
555
  do_write(Output_file*);
556
 
557
  // Return the required alignment.
558
  uint64_t
559
  do_addralign() const
560
  { return Output_data::default_alignment(); }
561
 
562
  // Write to a map file.
563
  void
564
  do_print_to_mapfile(Mapfile* mapfile) const
565
  { mapfile->print_output_data(this, _("** segment headers")); }
566
 
567
  // Set final data size.
568
  void
569
  set_final_data_size()
570
  { this->set_data_size(this->do_size()); }
571
 
572
 private:
573
  // Write the data to the file with the right size and endianness.
574
  template<int size, bool big_endian>
575
  void
576
  do_sized_write(Output_file*);
577
 
578
  // Compute the current size.
579
  off_t
580
  do_size() const;
581
 
582
  const Layout::Segment_list& segment_list_;
583
};
584
 
585
// Output the ELF file header.
586
 
587
class Output_file_header : public Output_data
588
{
589
 public:
590
  Output_file_header(const Target*,
591
                     const Symbol_table*,
592
                     const Output_segment_headers*,
593
                     const char* entry);
594
 
595
  // Add information about the section headers.  We lay out the ELF
596
  // file header before we create the section headers.
597
  void set_section_info(const Output_section_headers*,
598
                        const Output_section* shstrtab);
599
 
600
 protected:
601
  // Write the data to the file.
602
  void
603
  do_write(Output_file*);
604
 
605
  // Return the required alignment.
606
  uint64_t
607
  do_addralign() const
608
  { return Output_data::default_alignment(); }
609
 
610
  // Write to a map file.
611
  void
612
  do_print_to_mapfile(Mapfile* mapfile) const
613
  { mapfile->print_output_data(this, _("** file header")); }
614
 
615
  // Set final data size.
616
  void
617
  set_final_data_size(void)
618
  { this->set_data_size(this->do_size()); }
619
 
620
 private:
621
  // Write the data to the file with the right size and endianness.
622
  template<int size, bool big_endian>
623
  void
624
  do_sized_write(Output_file*);
625
 
626
  // Return the value to use for the entry address.
627
  template<int size>
628
  typename elfcpp::Elf_types<size>::Elf_Addr
629
  entry();
630
 
631
  // Compute the current data size.
632
  off_t
633
  do_size() const;
634
 
635
  const Target* target_;
636
  const Symbol_table* symtab_;
637
  const Output_segment_headers* segment_header_;
638
  const Output_section_headers* section_header_;
639
  const Output_section* shstrtab_;
640
  const char* entry_;
641
};
642
 
643
// Output sections are mainly comprised of input sections.  However,
644
// there are cases where we have data to write out which is not in an
645
// input section.  Output_section_data is used in such cases.  This is
646
// an abstract base class.
647
 
648
class Output_section_data : public Output_data
649
{
650
 public:
651
  Output_section_data(off_t data_size, uint64_t addralign,
652
                      bool is_data_size_fixed)
653
    : Output_data(), output_section_(NULL), addralign_(addralign)
654
  {
655
    this->set_data_size(data_size);
656
    if (is_data_size_fixed)
657
      this->fix_data_size();
658
  }
659
 
660
  Output_section_data(uint64_t addralign)
661
    : Output_data(), output_section_(NULL), addralign_(addralign)
662
  { }
663
 
664
  // Return the output section.
665
  const Output_section*
666
  output_section() const
667
  { return this->output_section_; }
668
 
669
  // Record the output section.
670
  void
671
  set_output_section(Output_section* os);
672
 
673
  // Add an input section, for SHF_MERGE sections.  This returns true
674
  // if the section was handled.
675
  bool
676
  add_input_section(Relobj* object, unsigned int shndx)
677
  { return this->do_add_input_section(object, shndx); }
678
 
679
  // Given an input OBJECT, an input section index SHNDX within that
680
  // object, and an OFFSET relative to the start of that input
681
  // section, return whether or not the corresponding offset within
682
  // the output section is known.  If this function returns true, it
683
  // sets *POUTPUT to the output offset.  The value -1 indicates that
684
  // this input offset is being discarded.
685
  bool
686
  output_offset(const Relobj* object, unsigned int shndx,
687
                section_offset_type offset,
688
                section_offset_type *poutput) const
689
  { return this->do_output_offset(object, shndx, offset, poutput); }
690
 
691
  // Return whether this is the merge section for the input section
692
  // SHNDX in OBJECT.  This should return true when output_offset
693
  // would return true for some values of OFFSET.
694
  bool
695
  is_merge_section_for(const Relobj* object, unsigned int shndx) const
696
  { return this->do_is_merge_section_for(object, shndx); }
697
 
698
  // Write the contents to a buffer.  This is used for sections which
699
  // require postprocessing, such as compression.
700
  void
701
  write_to_buffer(unsigned char* buffer)
702
  { this->do_write_to_buffer(buffer); }
703
 
704
  // Print merge stats to stderr.  This should only be called for
705
  // SHF_MERGE sections.
706
  void
707
  print_merge_stats(const char* section_name)
708
  { this->do_print_merge_stats(section_name); }
709
 
710
 protected:
711
  // The child class must implement do_write.
712
 
713
  // The child class may implement specific adjustments to the output
714
  // section.
715
  virtual void
716
  do_adjust_output_section(Output_section*)
717
  { }
718
 
719
  // May be implemented by child class.  Return true if the section
720
  // was handled.
721
  virtual bool
722
  do_add_input_section(Relobj*, unsigned int)
723
  { gold_unreachable(); }
724
 
725
  // The child class may implement output_offset.
726
  virtual bool
727
  do_output_offset(const Relobj*, unsigned int, section_offset_type,
728
                   section_offset_type*) const
729
  { return false; }
730
 
731
  // The child class may implement is_merge_section_for.
732
  virtual bool
733
  do_is_merge_section_for(const Relobj*, unsigned int) const
734
  { return false; }
735
 
736
  // The child class may implement write_to_buffer.  Most child
737
  // classes can not appear in a compressed section, and they do not
738
  // implement this.
739
  virtual void
740
  do_write_to_buffer(unsigned char*)
741
  { gold_unreachable(); }
742
 
743
  // Print merge statistics.
744
  virtual void
745
  do_print_merge_stats(const char*)
746
  { gold_unreachable(); }
747
 
748
  // Return the required alignment.
749
  uint64_t
750
  do_addralign() const
751
  { return this->addralign_; }
752
 
753
  // Return the output section.
754
  Output_section*
755
  do_output_section()
756
  { return this->output_section_; }
757
 
758
  // Return the section index of the output section.
759
  unsigned int
760
  do_out_shndx() const;
761
 
762
  // Set the alignment.
763
  void
764
  set_addralign(uint64_t addralign);
765
 
766
 private:
767
  // The output section for this section.
768
  Output_section* output_section_;
769
  // The required alignment.
770
  uint64_t addralign_;
771
};
772
 
773
// Some Output_section_data classes build up their data step by step,
774
// rather than all at once.  This class provides an interface for
775
// them.
776
 
777
class Output_section_data_build : public Output_section_data
778
{
779
 public:
780
  Output_section_data_build(uint64_t addralign)
781
    : Output_section_data(addralign)
782
  { }
783
 
784
  // Get the current data size.
785
  off_t
786
  current_data_size() const
787
  { return this->current_data_size_for_child(); }
788
 
789
  // Set the current data size.
790
  void
791
  set_current_data_size(off_t data_size)
792
  { this->set_current_data_size_for_child(data_size); }
793
 
794
 protected:
795
  // Set the final data size.
796
  virtual void
797
  set_final_data_size()
798
  { this->set_data_size(this->current_data_size_for_child()); }
799
};
800
 
801
// A simple case of Output_data in which we have constant data to
802
// output.
803
 
804
class Output_data_const : public Output_section_data
805
{
806
 public:
807
  Output_data_const(const std::string& data, uint64_t addralign)
808
    : Output_section_data(data.size(), addralign, true), data_(data)
809
  { }
810
 
811
  Output_data_const(const char* p, off_t len, uint64_t addralign)
812
    : Output_section_data(len, addralign, true), data_(p, len)
813
  { }
814
 
815
  Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
816
    : Output_section_data(len, addralign, true),
817
      data_(reinterpret_cast<const char*>(p), len)
818
  { }
819
 
820
 protected:
821
  // Write the data to the output file.
822
  void
823
  do_write(Output_file*);
824
 
825
  // Write the data to a buffer.
826
  void
827
  do_write_to_buffer(unsigned char* buffer)
828
  { memcpy(buffer, this->data_.data(), this->data_.size()); }
829
 
830
  // Write to a map file.
831
  void
832
  do_print_to_mapfile(Mapfile* mapfile) const
833
  { mapfile->print_output_data(this, _("** fill")); }
834
 
835
 private:
836
  std::string data_;
837
};
838
 
839
// Another version of Output_data with constant data, in which the
840
// buffer is allocated by the caller.
841
 
842
class Output_data_const_buffer : public Output_section_data
843
{
844
 public:
845
  Output_data_const_buffer(const unsigned char* p, off_t len,
846
                           uint64_t addralign, const char* map_name)
847
    : Output_section_data(len, addralign, true),
848
      p_(p), map_name_(map_name)
849
  { }
850
 
851
 protected:
852
  // Write the data the output file.
853
  void
854
  do_write(Output_file*);
855
 
856
  // Write the data to a buffer.
857
  void
858
  do_write_to_buffer(unsigned char* buffer)
859
  { memcpy(buffer, this->p_, this->data_size()); }
860
 
861
  // Write to a map file.
862
  void
863
  do_print_to_mapfile(Mapfile* mapfile) const
864
  { mapfile->print_output_data(this, _(this->map_name_)); }
865
 
866
 private:
867
  // The data to output.
868
  const unsigned char* p_;
869
  // Name to use in a map file.  Maps are a rarely used feature, but
870
  // the space usage is minor as aren't very many of these objects.
871
  const char* map_name_;
872
};
873
 
874
// A place holder for a fixed amount of data written out via some
875
// other mechanism.
876
 
877
class Output_data_fixed_space : public Output_section_data
878
{
879
 public:
880
  Output_data_fixed_space(off_t data_size, uint64_t addralign,
881
                          const char* map_name)
882
    : Output_section_data(data_size, addralign, true),
883
      map_name_(map_name)
884
  { }
885
 
886
 protected:
887
  // Write out the data--the actual data must be written out
888
  // elsewhere.
889
  void
890
  do_write(Output_file*)
891
  { }
892
 
893
  // Write to a map file.
894
  void
895
  do_print_to_mapfile(Mapfile* mapfile) const
896
  { mapfile->print_output_data(this, _(this->map_name_)); }
897
 
898
 private:
899
  // Name to use in a map file.  Maps are a rarely used feature, but
900
  // the space usage is minor as aren't very many of these objects.
901
  const char* map_name_;
902
};
903
 
904
// A place holder for variable sized data written out via some other
905
// mechanism.
906
 
907
class Output_data_space : public Output_section_data_build
908
{
909
 public:
910
  explicit Output_data_space(uint64_t addralign, const char* map_name)
911
    : Output_section_data_build(addralign),
912
      map_name_(map_name)
913
  { }
914
 
915
  // Set the alignment.
916
  void
917
  set_space_alignment(uint64_t align)
918
  { this->set_addralign(align); }
919
 
920
 protected:
921
  // Write out the data--the actual data must be written out
922
  // elsewhere.
923
  void
924
  do_write(Output_file*)
925
  { }
926
 
927
  // Write to a map file.
928
  void
929
  do_print_to_mapfile(Mapfile* mapfile) const
930
  { mapfile->print_output_data(this, _(this->map_name_)); }
931
 
932
 private:
933
  // Name to use in a map file.  Maps are a rarely used feature, but
934
  // the space usage is minor as aren't very many of these objects.
935
  const char* map_name_;
936
};
937
 
938
// Fill fixed space with zeroes.  This is just like
939
// Output_data_fixed_space, except that the map name is known.
940
 
941
class Output_data_zero_fill : public Output_section_data
942
{
943
 public:
944
  Output_data_zero_fill(off_t data_size, uint64_t addralign)
945
    : Output_section_data(data_size, addralign, true)
946
  { }
947
 
948
 protected:
949
  // There is no data to write out.
950
  void
951
  do_write(Output_file*)
952
  { }
953
 
954
  // Write to a map file.
955
  void
956
  do_print_to_mapfile(Mapfile* mapfile) const
957
  { mapfile->print_output_data(this, "** zero fill"); }
958
};
959
 
960
// A string table which goes into an output section.
961
 
962
class Output_data_strtab : public Output_section_data
963
{
964
 public:
965
  Output_data_strtab(Stringpool* strtab)
966
    : Output_section_data(1), strtab_(strtab)
967
  { }
968
 
969
 protected:
970
  // This is called to set the address and file offset.  Here we make
971
  // sure that the Stringpool is finalized.
972
  void
973
  set_final_data_size();
974
 
975
  // Write out the data.
976
  void
977
  do_write(Output_file*);
978
 
979
  // Write the data to a buffer.
980
  void
981
  do_write_to_buffer(unsigned char* buffer)
982
  { this->strtab_->write_to_buffer(buffer, this->data_size()); }
983
 
984
  // Write to a map file.
985
  void
986
  do_print_to_mapfile(Mapfile* mapfile) const
987
  { mapfile->print_output_data(this, _("** string table")); }
988
 
989
 private:
990
  Stringpool* strtab_;
991
};
992
 
993
// This POD class is used to represent a single reloc in the output
994
// file.  This could be a private class within Output_data_reloc, but
995
// the templatization is complex enough that I broke it out into a
996
// separate class.  The class is templatized on either elfcpp::SHT_REL
997
// or elfcpp::SHT_RELA, and also on whether this is a dynamic
998
// relocation or an ordinary relocation.
999
 
1000
// A relocation can be against a global symbol, a local symbol, a
1001
// local section symbol, an output section, or the undefined symbol at
1002
// index 0.  We represent the latter by using a NULL global symbol.
1003
 
1004
template<int sh_type, bool dynamic, int size, bool big_endian>
1005
class Output_reloc;
1006
 
1007
template<bool dynamic, int size, bool big_endian>
1008
class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1009
{
1010
 public:
1011
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1012
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1013
 
1014
  static const Address invalid_address = static_cast<Address>(0) - 1;
1015
 
1016
  // An uninitialized entry.  We need this because we want to put
1017
  // instances of this class into an STL container.
1018
  Output_reloc()
1019
    : local_sym_index_(INVALID_CODE)
1020
  { }
1021
 
1022
  // We have a bunch of different constructors.  They come in pairs
1023
  // depending on how the address of the relocation is specified.  It
1024
  // can either be an offset in an Output_data or an offset in an
1025
  // input section.
1026
 
1027
  // A reloc against a global symbol.
1028
 
1029
  Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1030
               Address address, bool is_relative);
1031
 
1032
  Output_reloc(Symbol* gsym, unsigned int type,
1033
               Sized_relobj<size, big_endian>* relobj,
1034
               unsigned int shndx, Address address, bool is_relative);
1035
 
1036
  // A reloc against a local symbol or local section symbol.
1037
 
1038
  Output_reloc(Sized_relobj<size, big_endian>* relobj,
1039
               unsigned int local_sym_index, unsigned int type,
1040
               Output_data* od, Address address, bool is_relative,
1041
               bool is_section_symbol);
1042
 
1043
  Output_reloc(Sized_relobj<size, big_endian>* relobj,
1044
               unsigned int local_sym_index, unsigned int type,
1045
               unsigned int shndx, Address address, bool is_relative,
1046
               bool is_section_symbol);
1047
 
1048
  // A reloc against the STT_SECTION symbol of an output section.
1049
 
1050
  Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1051
               Address address);
1052
 
1053
  Output_reloc(Output_section* os, unsigned int type,
1054
               Sized_relobj<size, big_endian>* relobj,
1055
               unsigned int shndx, Address address);
1056
 
1057
  // Return TRUE if this is a RELATIVE relocation.
1058
  bool
1059
  is_relative() const
1060
  { return this->is_relative_; }
1061
 
1062
  // Return whether this is against a local section symbol.
1063
  bool
1064
  is_local_section_symbol() const
1065
  {
1066
    return (this->local_sym_index_ != GSYM_CODE
1067
            && this->local_sym_index_ != SECTION_CODE
1068
            && this->local_sym_index_ != INVALID_CODE
1069
            && this->is_section_symbol_);
1070
  }
1071
 
1072
  // For a local section symbol, return the offset of the input
1073
  // section within the output section.  ADDEND is the addend being
1074
  // applied to the input section.
1075
  Address
1076
  local_section_offset(Addend addend) const;
1077
 
1078
  // Get the value of the symbol referred to by a Rel relocation when
1079
  // we are adding the given ADDEND.
1080
  Address
1081
  symbol_value(Addend addend) const;
1082
 
1083
  // Write the reloc entry to an output view.
1084
  void
1085
  write(unsigned char* pov) const;
1086
 
1087
  // Write the offset and info fields to Write_rel.
1088
  template<typename Write_rel>
1089
  void write_rel(Write_rel*) const;
1090
 
1091
  // This is used when sorting dynamic relocs.  Return -1 to sort this
1092
  // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1093
  int
1094
  compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1095
    const;
1096
 
1097
  // Return whether this reloc should be sorted before the argument
1098
  // when sorting dynamic relocs.
1099
  bool
1100
  sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1101
              r2) const
1102
  { return this->compare(r2) < 0; }
1103
 
1104
 private:
1105
  // Record that we need a dynamic symbol index.
1106
  void
1107
  set_needs_dynsym_index();
1108
 
1109
  // Return the symbol index.
1110
  unsigned int
1111
  get_symbol_index() const;
1112
 
1113
  // Return the output address.
1114
  Address
1115
  get_address() const;
1116
 
1117
  // Codes for local_sym_index_.
1118
  enum
1119
  {
1120
    // Global symbol.
1121
    GSYM_CODE = -1U,
1122
    // Output section.
1123
    SECTION_CODE = -2U,
1124
    // Invalid uninitialized entry.
1125
    INVALID_CODE = -3U
1126
  };
1127
 
1128
  union
1129
  {
1130
    // For a local symbol or local section symbol
1131
    // (this->local_sym_index_ >= 0), the object.  We will never
1132
    // generate a relocation against a local symbol in a dynamic
1133
    // object; that doesn't make sense.  And our callers will always
1134
    // be templatized, so we use Sized_relobj here.
1135
    Sized_relobj<size, big_endian>* relobj;
1136
    // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1137
    // symbol.  If this is NULL, it indicates a relocation against the
1138
    // undefined 0 symbol.
1139
    Symbol* gsym;
1140
    // For a relocation against an output section
1141
    // (this->local_sym_index_ == SECTION_CODE), the output section.
1142
    Output_section* os;
1143
  } u1_;
1144
  union
1145
  {
1146
    // If this->shndx_ is not INVALID CODE, the object which holds the
1147
    // input section being used to specify the reloc address.
1148
    Sized_relobj<size, big_endian>* relobj;
1149
    // If this->shndx_ is INVALID_CODE, the output data being used to
1150
    // specify the reloc address.  This may be NULL if the reloc
1151
    // address is absolute.
1152
    Output_data* od;
1153
  } u2_;
1154
  // The address offset within the input section or the Output_data.
1155
  Address address_;
1156
  // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1157
  // relocation against an output section, or INVALID_CODE for an
1158
  // uninitialized value.  Otherwise, for a local symbol
1159
  // (this->is_section_symbol_ is false), the local symbol index.  For
1160
  // a local section symbol (this->is_section_symbol_ is true), the
1161
  // section index in the input file.
1162
  unsigned int local_sym_index_;
1163
  // The reloc type--a processor specific code.
1164
  unsigned int type_ : 30;
1165
  // True if the relocation is a RELATIVE relocation.
1166
  bool is_relative_ : 1;
1167
  // True if the relocation is against a section symbol.
1168
  bool is_section_symbol_ : 1;
1169
  // If the reloc address is an input section in an object, the
1170
  // section index.  This is INVALID_CODE if the reloc address is
1171
  // specified in some other way.
1172
  unsigned int shndx_;
1173
};
1174
 
1175
// The SHT_RELA version of Output_reloc<>.  This is just derived from
1176
// the SHT_REL version of Output_reloc, but it adds an addend.
1177
 
1178
template<bool dynamic, int size, bool big_endian>
1179
class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1180
{
1181
 public:
1182
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1183
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1184
 
1185
  // An uninitialized entry.
1186
  Output_reloc()
1187
    : rel_()
1188
  { }
1189
 
1190
  // A reloc against a global symbol.
1191
 
1192
  Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1193
               Address address, Addend addend, bool is_relative)
1194
    : rel_(gsym, type, od, address, is_relative), addend_(addend)
1195
  { }
1196
 
1197
  Output_reloc(Symbol* gsym, unsigned int type,
1198
               Sized_relobj<size, big_endian>* relobj,
1199
               unsigned int shndx, Address address, Addend addend,
1200
               bool is_relative)
1201
    : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
1202
  { }
1203
 
1204
  // A reloc against a local symbol.
1205
 
1206
  Output_reloc(Sized_relobj<size, big_endian>* relobj,
1207
               unsigned int local_sym_index, unsigned int type,
1208
               Output_data* od, Address address,
1209
               Addend addend, bool is_relative, bool is_section_symbol)
1210
    : rel_(relobj, local_sym_index, type, od, address, is_relative,
1211
           is_section_symbol),
1212
      addend_(addend)
1213
  { }
1214
 
1215
  Output_reloc(Sized_relobj<size, big_endian>* relobj,
1216
               unsigned int local_sym_index, unsigned int type,
1217
               unsigned int shndx, Address address,
1218
               Addend addend, bool is_relative, bool is_section_symbol)
1219
    : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1220
           is_section_symbol),
1221
      addend_(addend)
1222
  { }
1223
 
1224
  // A reloc against the STT_SECTION symbol of an output section.
1225
 
1226
  Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1227
               Address address, Addend addend)
1228
    : rel_(os, type, od, address), addend_(addend)
1229
  { }
1230
 
1231
  Output_reloc(Output_section* os, unsigned int type,
1232
               Sized_relobj<size, big_endian>* relobj,
1233
               unsigned int shndx, Address address, Addend addend)
1234
    : rel_(os, type, relobj, shndx, address), addend_(addend)
1235
  { }
1236
 
1237
  // Write the reloc entry to an output view.
1238
  void
1239
  write(unsigned char* pov) const;
1240
 
1241
  // Return whether this reloc should be sorted before the argument
1242
  // when sorting dynamic relocs.
1243
  bool
1244
  sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1245
              r2) const
1246
  {
1247
    int i = this->rel_.compare(r2.rel_);
1248
    if (i < 0)
1249
      return true;
1250
    else if (i > 0)
1251
      return false;
1252
    else
1253
      return this->addend_ < r2.addend_;
1254
  }
1255
 
1256
 private:
1257
  // The basic reloc.
1258
  Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1259
  // The addend.
1260
  Addend addend_;
1261
};
1262
 
1263
// Output_data_reloc is used to manage a section containing relocs.
1264
// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA.  DYNAMIC
1265
// indicates whether this is a dynamic relocation or a normal
1266
// relocation.  Output_data_reloc_base is a base class.
1267
// Output_data_reloc is the real class, which we specialize based on
1268
// the reloc type.
1269
 
1270
template<int sh_type, bool dynamic, int size, bool big_endian>
1271
class Output_data_reloc_base : public Output_section_data_build
1272
{
1273
 public:
1274
  typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1275
  typedef typename Output_reloc_type::Address Address;
1276
  static const int reloc_size =
1277
    Reloc_types<sh_type, size, big_endian>::reloc_size;
1278
 
1279
  // Construct the section.
1280
  Output_data_reloc_base(bool sort_relocs)
1281
    : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1282
      sort_relocs_(sort_relocs)
1283
  { }
1284
 
1285
 protected:
1286
  // Write out the data.
1287
  void
1288
  do_write(Output_file*);
1289
 
1290
  // Set the entry size and the link.
1291
  void
1292
  do_adjust_output_section(Output_section *os);
1293
 
1294
  // Write to a map file.
1295
  void
1296
  do_print_to_mapfile(Mapfile* mapfile) const
1297
  {
1298
    mapfile->print_output_data(this,
1299
                               (dynamic
1300
                                ? _("** dynamic relocs")
1301
                                : _("** relocs")));
1302
  }
1303
 
1304
  // Add a relocation entry.
1305
  void
1306
  add(Output_data *od, const Output_reloc_type& reloc)
1307
  {
1308
    this->relocs_.push_back(reloc);
1309
    this->set_current_data_size(this->relocs_.size() * reloc_size);
1310
    od->add_dynamic_reloc();
1311
  }
1312
 
1313
 private:
1314
  typedef std::vector<Output_reloc_type> Relocs;
1315
 
1316
  // The class used to sort the relocations.
1317
  struct Sort_relocs_comparison
1318
  {
1319
    bool
1320
    operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1321
    { return r1.sort_before(r2); }
1322
  };
1323
 
1324
  // The relocations in this section.
1325
  Relocs relocs_;
1326
  // Whether to sort the relocations when writing them out, to make
1327
  // the dynamic linker more efficient.
1328
  bool sort_relocs_;
1329
};
1330
 
1331
// The class which callers actually create.
1332
 
1333
template<int sh_type, bool dynamic, int size, bool big_endian>
1334
class Output_data_reloc;
1335
 
1336
// The SHT_REL version of Output_data_reloc.
1337
 
1338
template<bool dynamic, int size, bool big_endian>
1339
class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1340
  : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1341
{
1342
 private:
1343
  typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1344
                                 big_endian> Base;
1345
 
1346
 public:
1347
  typedef typename Base::Output_reloc_type Output_reloc_type;
1348
  typedef typename Output_reloc_type::Address Address;
1349
 
1350
  Output_data_reloc(bool sr)
1351
    : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1352
  { }
1353
 
1354
  // Add a reloc against a global symbol.
1355
 
1356
  void
1357
  add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1358
  { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
1359
 
1360
  void
1361
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1362
             Sized_relobj<size, big_endian>* relobj,
1363
             unsigned int shndx, Address address)
1364
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1365
                                    false)); }
1366
 
1367
  // These are to simplify the Copy_relocs class.
1368
 
1369
  void
1370
  add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1371
             Address addend)
1372
  {
1373
    gold_assert(addend == 0);
1374
    this->add_global(gsym, type, od, address);
1375
  }
1376
 
1377
  void
1378
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1379
             Sized_relobj<size, big_endian>* relobj,
1380
             unsigned int shndx, Address address, Address addend)
1381
  {
1382
    gold_assert(addend == 0);
1383
    this->add_global(gsym, type, od, relobj, shndx, address);
1384
  }
1385
 
1386
  // Add a RELATIVE reloc against a global symbol.  The final relocation
1387
  // will not reference the symbol.
1388
 
1389
  void
1390
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1391
                      Address address)
1392
  { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1393
 
1394
  void
1395
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1396
                      Sized_relobj<size, big_endian>* relobj,
1397
                      unsigned int shndx, Address address)
1398
  {
1399
    this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1400
                                    true));
1401
  }
1402
 
1403
  // Add a reloc against a local symbol.
1404
 
1405
  void
1406
  add_local(Sized_relobj<size, big_endian>* relobj,
1407
            unsigned int local_sym_index, unsigned int type,
1408
            Output_data* od, Address address)
1409
  {
1410
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1411
                                    address, false, false));
1412
  }
1413
 
1414
  void
1415
  add_local(Sized_relobj<size, big_endian>* relobj,
1416
            unsigned int local_sym_index, unsigned int type,
1417
            Output_data* od, unsigned int shndx, Address address)
1418
  {
1419
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1420
                                    address, false, false));
1421
  }
1422
 
1423
  // Add a RELATIVE reloc against a local symbol.
1424
 
1425
  void
1426
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1427
                     unsigned int local_sym_index, unsigned int type,
1428
                     Output_data* od, Address address)
1429
  {
1430
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1431
                                    address, true, false));
1432
  }
1433
 
1434
  void
1435
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1436
                     unsigned int local_sym_index, unsigned int type,
1437
                     Output_data* od, unsigned int shndx, Address address)
1438
  {
1439
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1440
                                    address, true, false));
1441
  }
1442
 
1443
  // Add a reloc against a local section symbol.  This will be
1444
  // converted into a reloc against the STT_SECTION symbol of the
1445
  // output section.
1446
 
1447
  void
1448
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1449
                    unsigned int input_shndx, unsigned int type,
1450
                    Output_data* od, Address address)
1451
  {
1452
    this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1453
                                    address, false, true));
1454
  }
1455
 
1456
  void
1457
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1458
                    unsigned int input_shndx, unsigned int type,
1459
                    Output_data* od, unsigned int shndx, Address address)
1460
  {
1461
    this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1462
                                    address, false, true));
1463
  }
1464
 
1465
  // A reloc against the STT_SECTION symbol of an output section.
1466
  // OS is the Output_section that the relocation refers to; OD is
1467
  // the Output_data object being relocated.
1468
 
1469
  void
1470
  add_output_section(Output_section* os, unsigned int type,
1471
                     Output_data* od, Address address)
1472
  { this->add(od, Output_reloc_type(os, type, od, address)); }
1473
 
1474
  void
1475
  add_output_section(Output_section* os, unsigned int type, Output_data* od,
1476
                     Sized_relobj<size, big_endian>* relobj,
1477
                     unsigned int shndx, Address address)
1478
  { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1479
};
1480
 
1481
// The SHT_RELA version of Output_data_reloc.
1482
 
1483
template<bool dynamic, int size, bool big_endian>
1484
class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1485
  : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1486
{
1487
 private:
1488
  typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1489
                                 big_endian> Base;
1490
 
1491
 public:
1492
  typedef typename Base::Output_reloc_type Output_reloc_type;
1493
  typedef typename Output_reloc_type::Address Address;
1494
  typedef typename Output_reloc_type::Addend Addend;
1495
 
1496
  Output_data_reloc(bool sr)
1497
    : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1498
  { }
1499
 
1500
  // Add a reloc against a global symbol.
1501
 
1502
  void
1503
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1504
             Address address, Addend addend)
1505
  { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1506
                                    false)); }
1507
 
1508
  void
1509
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1510
             Sized_relobj<size, big_endian>* relobj,
1511
             unsigned int shndx, Address address,
1512
             Addend addend)
1513
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1514
                                    addend, false)); }
1515
 
1516
  // Add a RELATIVE reloc against a global symbol.  The final output
1517
  // relocation will not reference the symbol, but we must keep the symbol
1518
  // information long enough to set the addend of the relocation correctly
1519
  // when it is written.
1520
 
1521
  void
1522
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1523
                      Address address, Addend addend)
1524
  { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1525
 
1526
  void
1527
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1528
                      Sized_relobj<size, big_endian>* relobj,
1529
                      unsigned int shndx, Address address, Addend addend)
1530
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1531
                                    addend, true)); }
1532
 
1533
  // Add a reloc against a local symbol.
1534
 
1535
  void
1536
  add_local(Sized_relobj<size, big_endian>* relobj,
1537
            unsigned int local_sym_index, unsigned int type,
1538
            Output_data* od, Address address, Addend addend)
1539
  {
1540
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1541
                                    addend, false, false));
1542
  }
1543
 
1544
  void
1545
  add_local(Sized_relobj<size, big_endian>* relobj,
1546
            unsigned int local_sym_index, unsigned int type,
1547
            Output_data* od, unsigned int shndx, Address address,
1548
            Addend addend)
1549
  {
1550
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1551
                                    address, addend, false, false));
1552
  }
1553
 
1554
  // Add a RELATIVE reloc against a local symbol.
1555
 
1556
  void
1557
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1558
                     unsigned int local_sym_index, unsigned int type,
1559
                     Output_data* od, Address address, Addend addend)
1560
  {
1561
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1562
                                    addend, true, false));
1563
  }
1564
 
1565
  void
1566
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1567
                     unsigned int local_sym_index, unsigned int type,
1568
                     Output_data* od, unsigned int shndx, Address address,
1569
                     Addend addend)
1570
  {
1571
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1572
                                    address, addend, true, false));
1573
  }
1574
 
1575
  // Add a reloc against a local section symbol.  This will be
1576
  // converted into a reloc against the STT_SECTION symbol of the
1577
  // output section.
1578
 
1579
  void
1580
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1581
                    unsigned int input_shndx, unsigned int type,
1582
                    Output_data* od, Address address, Addend addend)
1583
  {
1584
    this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1585
                                    addend, false, true));
1586
  }
1587
 
1588
  void
1589
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1590
                     unsigned int input_shndx, unsigned int type,
1591
                     Output_data* od, unsigned int shndx, Address address,
1592
                     Addend addend)
1593
  {
1594
    this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1595
                                    address, addend, false, true));
1596
  }
1597
 
1598
  // A reloc against the STT_SECTION symbol of an output section.
1599
 
1600
  void
1601
  add_output_section(Output_section* os, unsigned int type, Output_data* od,
1602
                     Address address, Addend addend)
1603
  { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
1604
 
1605
  void
1606
  add_output_section(Output_section* os, unsigned int type,
1607
                     Sized_relobj<size, big_endian>* relobj,
1608
                     unsigned int shndx, Address address, Addend addend)
1609
  { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1610
                                    addend)); }
1611
};
1612
 
1613
// Output_relocatable_relocs represents a relocation section in a
1614
// relocatable link.  The actual data is written out in the target
1615
// hook relocate_for_relocatable.  This just saves space for it.
1616
 
1617
template<int sh_type, int size, bool big_endian>
1618
class Output_relocatable_relocs : public Output_section_data
1619
{
1620
 public:
1621
  Output_relocatable_relocs(Relocatable_relocs* rr)
1622
    : Output_section_data(Output_data::default_alignment_for_size(size)),
1623
      rr_(rr)
1624
  { }
1625
 
1626
  void
1627
  set_final_data_size();
1628
 
1629
  // Write out the data.  There is nothing to do here.
1630
  void
1631
  do_write(Output_file*)
1632
  { }
1633
 
1634
  // Write to a map file.
1635
  void
1636
  do_print_to_mapfile(Mapfile* mapfile) const
1637
  { mapfile->print_output_data(this, _("** relocs")); }
1638
 
1639
 private:
1640
  // The relocs associated with this input section.
1641
  Relocatable_relocs* rr_;
1642
};
1643
 
1644
// Handle a GROUP section.
1645
 
1646
template<int size, bool big_endian>
1647
class Output_data_group : public Output_section_data
1648
{
1649
 public:
1650
  // The constructor clears *INPUT_SHNDXES.
1651
  Output_data_group(Sized_relobj<size, big_endian>* relobj,
1652
                    section_size_type entry_count,
1653
                    elfcpp::Elf_Word flags,
1654
                    std::vector<unsigned int>* input_shndxes);
1655
 
1656
  void
1657
  do_write(Output_file*);
1658
 
1659
  // Write to a map file.
1660
  void
1661
  do_print_to_mapfile(Mapfile* mapfile) const
1662
  { mapfile->print_output_data(this, _("** group")); }
1663
 
1664
  // Set final data size.
1665
  void
1666
  set_final_data_size()
1667
  { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1668
 
1669
 private:
1670
  // The input object.
1671
  Sized_relobj<size, big_endian>* relobj_;
1672
  // The group flag word.
1673
  elfcpp::Elf_Word flags_;
1674
  // The section indexes of the input sections in this group.
1675
  std::vector<unsigned int> input_shndxes_;
1676
};
1677
 
1678
// Output_data_got is used to manage a GOT.  Each entry in the GOT is
1679
// for one symbol--either a global symbol or a local symbol in an
1680
// object.  The target specific code adds entries to the GOT as
1681
// needed.
1682
 
1683
template<int size, bool big_endian>
1684
class Output_data_got : public Output_section_data_build
1685
{
1686
 public:
1687
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1688
  typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1689
  typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1690
 
1691
  Output_data_got()
1692
    : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1693
      entries_()
1694
  { }
1695
 
1696
  // Add an entry for a global symbol to the GOT.  Return true if this
1697
  // is a new GOT entry, false if the symbol was already in the GOT.
1698
  bool
1699
  add_global(Symbol* gsym, unsigned int got_type);
1700
 
1701
  // Add an entry for a global symbol to the GOT, and add a dynamic
1702
  // relocation of type R_TYPE for the GOT entry.
1703
  void
1704
  add_global_with_rel(Symbol* gsym, unsigned int got_type,
1705
                      Rel_dyn* rel_dyn, unsigned int r_type);
1706
 
1707
  void
1708
  add_global_with_rela(Symbol* gsym, unsigned int got_type,
1709
                       Rela_dyn* rela_dyn, unsigned int r_type);
1710
 
1711
  // Add a pair of entries for a global symbol to the GOT, and add
1712
  // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1713
  void
1714
  add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1715
                           Rel_dyn* rel_dyn, unsigned int r_type_1,
1716
                           unsigned int r_type_2);
1717
 
1718
  void
1719
  add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1720
                            Rela_dyn* rela_dyn, unsigned int r_type_1,
1721
                            unsigned int r_type_2);
1722
 
1723
  // Add an entry for a local symbol to the GOT.  This returns true if
1724
  // this is a new GOT entry, false if the symbol already has a GOT
1725
  // entry.
1726
  bool
1727
  add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1728
            unsigned int got_type);
1729
 
1730
  // Add an entry for a local symbol to the GOT, and add a dynamic
1731
  // relocation of type R_TYPE for the GOT entry.
1732
  void
1733
  add_local_with_rel(Sized_relobj<size, big_endian>* object,
1734
                     unsigned int sym_index, unsigned int got_type,
1735
                     Rel_dyn* rel_dyn, unsigned int r_type);
1736
 
1737
  void
1738
  add_local_with_rela(Sized_relobj<size, big_endian>* object,
1739
                      unsigned int sym_index, unsigned int got_type,
1740
                      Rela_dyn* rela_dyn, unsigned int r_type);
1741
 
1742
  // Add a pair of entries for a local symbol to the GOT, and add
1743
  // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1744
  void
1745
  add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1746
                          unsigned int sym_index, unsigned int shndx,
1747
                          unsigned int got_type, Rel_dyn* rel_dyn,
1748
                          unsigned int r_type_1, unsigned int r_type_2);
1749
 
1750
  void
1751
  add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1752
                          unsigned int sym_index, unsigned int shndx,
1753
                          unsigned int got_type, Rela_dyn* rela_dyn,
1754
                          unsigned int r_type_1, unsigned int r_type_2);
1755
 
1756
  // Add a constant to the GOT.  This returns the offset of the new
1757
  // entry from the start of the GOT.
1758
  unsigned int
1759
  add_constant(Valtype constant)
1760
  {
1761
    this->entries_.push_back(Got_entry(constant));
1762
    this->set_got_size();
1763
    return this->last_got_offset();
1764
  }
1765
 
1766
 protected:
1767
  // Write out the GOT table.
1768
  void
1769
  do_write(Output_file*);
1770
 
1771
  // Write to a map file.
1772
  void
1773
  do_print_to_mapfile(Mapfile* mapfile) const
1774
  { mapfile->print_output_data(this, _("** GOT")); }
1775
 
1776
 private:
1777
  // This POD class holds a single GOT entry.
1778
  class Got_entry
1779
  {
1780
   public:
1781
    // Create a zero entry.
1782
    Got_entry()
1783
      : local_sym_index_(CONSTANT_CODE)
1784
    { this->u_.constant = 0; }
1785
 
1786
    // Create a global symbol entry.
1787
    explicit Got_entry(Symbol* gsym)
1788
      : local_sym_index_(GSYM_CODE)
1789
    { this->u_.gsym = gsym; }
1790
 
1791
    // Create a local symbol entry.
1792
    Got_entry(Sized_relobj<size, big_endian>* object,
1793
              unsigned int local_sym_index)
1794
      : local_sym_index_(local_sym_index)
1795
    {
1796
      gold_assert(local_sym_index != GSYM_CODE
1797
                  && local_sym_index != CONSTANT_CODE);
1798
      this->u_.object = object;
1799
    }
1800
 
1801
    // Create a constant entry.  The constant is a host value--it will
1802
    // be swapped, if necessary, when it is written out.
1803
    explicit Got_entry(Valtype constant)
1804
      : local_sym_index_(CONSTANT_CODE)
1805
    { this->u_.constant = constant; }
1806
 
1807
    // Write the GOT entry to an output view.
1808
    void
1809
    write(unsigned char* pov) const;
1810
 
1811
   private:
1812
    enum
1813
    {
1814
      GSYM_CODE = -1U,
1815
      CONSTANT_CODE = -2U
1816
    };
1817
 
1818
    union
1819
    {
1820
      // For a local symbol, the object.
1821
      Sized_relobj<size, big_endian>* object;
1822
      // For a global symbol, the symbol.
1823
      Symbol* gsym;
1824
      // For a constant, the constant.
1825
      Valtype constant;
1826
    } u_;
1827
    // For a local symbol, the local symbol index.  This is GSYM_CODE
1828
    // for a global symbol, or CONSTANT_CODE for a constant.
1829
    unsigned int local_sym_index_;
1830
  };
1831
 
1832
  typedef std::vector<Got_entry> Got_entries;
1833
 
1834
  // Return the offset into the GOT of GOT entry I.
1835
  unsigned int
1836
  got_offset(unsigned int i) const
1837
  { return i * (size / 8); }
1838
 
1839
  // Return the offset into the GOT of the last entry added.
1840
  unsigned int
1841
  last_got_offset() const
1842
  { return this->got_offset(this->entries_.size() - 1); }
1843
 
1844
  // Set the size of the section.
1845
  void
1846
  set_got_size()
1847
  { this->set_current_data_size(this->got_offset(this->entries_.size())); }
1848
 
1849
  // The list of GOT entries.
1850
  Got_entries entries_;
1851
};
1852
 
1853
// Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1854
// section.
1855
 
1856
class Output_data_dynamic : public Output_section_data
1857
{
1858
 public:
1859
  Output_data_dynamic(Stringpool* pool)
1860
    : Output_section_data(Output_data::default_alignment()),
1861
      entries_(), pool_(pool)
1862
  { }
1863
 
1864
  // Add a new dynamic entry with a fixed numeric value.
1865
  void
1866
  add_constant(elfcpp::DT tag, unsigned int val)
1867
  { this->add_entry(Dynamic_entry(tag, val)); }
1868
 
1869
  // Add a new dynamic entry with the address of output data.
1870
  void
1871
  add_section_address(elfcpp::DT tag, const Output_data* od)
1872
  { this->add_entry(Dynamic_entry(tag, od, false)); }
1873
 
1874
  // Add a new dynamic entry with the address of output data
1875
  // plus a constant offset.
1876
  void
1877
  add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1878
                          unsigned int offset)
1879
  { this->add_entry(Dynamic_entry(tag, od, offset)); }
1880
 
1881
  // Add a new dynamic entry with the size of output data.
1882
  void
1883
  add_section_size(elfcpp::DT tag, const Output_data* od)
1884
  { this->add_entry(Dynamic_entry(tag, od, true)); }
1885
 
1886
  // Add a new dynamic entry with the address of a symbol.
1887
  void
1888
  add_symbol(elfcpp::DT tag, const Symbol* sym)
1889
  { this->add_entry(Dynamic_entry(tag, sym)); }
1890
 
1891
  // Add a new dynamic entry with a string.
1892
  void
1893
  add_string(elfcpp::DT tag, const char* str)
1894
  { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
1895
 
1896
  void
1897
  add_string(elfcpp::DT tag, const std::string& str)
1898
  { this->add_string(tag, str.c_str()); }
1899
 
1900
 protected:
1901
  // Adjust the output section to set the entry size.
1902
  void
1903
  do_adjust_output_section(Output_section*);
1904
 
1905
  // Set the final data size.
1906
  void
1907
  set_final_data_size();
1908
 
1909
  // Write out the dynamic entries.
1910
  void
1911
  do_write(Output_file*);
1912
 
1913
  // Write to a map file.
1914
  void
1915
  do_print_to_mapfile(Mapfile* mapfile) const
1916
  { mapfile->print_output_data(this, _("** dynamic")); }
1917
 
1918
 private:
1919
  // This POD class holds a single dynamic entry.
1920
  class Dynamic_entry
1921
  {
1922
   public:
1923
    // Create an entry with a fixed numeric value.
1924
    Dynamic_entry(elfcpp::DT tag, unsigned int val)
1925
      : tag_(tag), offset_(DYNAMIC_NUMBER)
1926
    { this->u_.val = val; }
1927
 
1928
    // Create an entry with the size or address of a section.
1929
    Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
1930
      : tag_(tag),
1931
        offset_(section_size
1932
                ? DYNAMIC_SECTION_SIZE
1933
                : DYNAMIC_SECTION_ADDRESS)
1934
    { this->u_.od = od; }
1935
 
1936
    // Create an entry with the address of a section plus a constant offset.
1937
    Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1938
      : tag_(tag),
1939
        offset_(offset)
1940
    { this->u_.od = od; }
1941
 
1942
    // Create an entry with the address of a symbol.
1943
    Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
1944
      : tag_(tag), offset_(DYNAMIC_SYMBOL)
1945
    { this->u_.sym = sym; }
1946
 
1947
    // Create an entry with a string.
1948
    Dynamic_entry(elfcpp::DT tag, const char* str)
1949
      : tag_(tag), offset_(DYNAMIC_STRING)
1950
    { this->u_.str = str; }
1951
 
1952
    // Return the tag of this entry.
1953
    elfcpp::DT
1954
    tag() const
1955
    { return this->tag_; }
1956
 
1957
    // Write the dynamic entry to an output view.
1958
    template<int size, bool big_endian>
1959
    void
1960
    write(unsigned char* pov, const Stringpool*) const;
1961
 
1962
   private:
1963
    // Classification is encoded in the OFFSET field.
1964
    enum Classification
1965
    {
1966
      // Section address.
1967
      DYNAMIC_SECTION_ADDRESS = 0,
1968
      // Number.
1969
      DYNAMIC_NUMBER = -1U,
1970
      // Section size.
1971
      DYNAMIC_SECTION_SIZE = -2U,
1972
      // Symbol adress.
1973
      DYNAMIC_SYMBOL = -3U,
1974
      // String.
1975
      DYNAMIC_STRING = -4U
1976
      // Any other value indicates a section address plus OFFSET.
1977
    };
1978
 
1979
    union
1980
    {
1981
      // For DYNAMIC_NUMBER.
1982
      unsigned int val;
1983
      // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
1984
      const Output_data* od;
1985
      // For DYNAMIC_SYMBOL.
1986
      const Symbol* sym;
1987
      // For DYNAMIC_STRING.
1988
      const char* str;
1989
    } u_;
1990
    // The dynamic tag.
1991
    elfcpp::DT tag_;
1992
    // The type of entry (Classification) or offset within a section.
1993
    unsigned int offset_;
1994
  };
1995
 
1996
  // Add an entry to the list.
1997
  void
1998
  add_entry(const Dynamic_entry& entry)
1999
  { this->entries_.push_back(entry); }
2000
 
2001
  // Sized version of write function.
2002
  template<int size, bool big_endian>
2003
  void
2004
  sized_write(Output_file* of);
2005
 
2006
  // The type of the list of entries.
2007
  typedef std::vector<Dynamic_entry> Dynamic_entries;
2008
 
2009
  // The entries.
2010
  Dynamic_entries entries_;
2011
  // The pool used for strings.
2012
  Stringpool* pool_;
2013
};
2014
 
2015
// Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2016
// which may be required if the object file has more than
2017
// SHN_LORESERVE sections.
2018
 
2019
class Output_symtab_xindex : public Output_section_data
2020
{
2021
 public:
2022
  Output_symtab_xindex(size_t symcount)
2023
    : Output_section_data(symcount * 4, 4, true),
2024
      entries_()
2025
  { }
2026
 
2027
  // Add an entry: symbol number SYMNDX has section SHNDX.
2028
  void
2029
  add(unsigned int symndx, unsigned int shndx)
2030
  { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2031
 
2032
 protected:
2033
  void
2034
  do_write(Output_file*);
2035
 
2036
  // Write to a map file.
2037
  void
2038
  do_print_to_mapfile(Mapfile* mapfile) const
2039
  { mapfile->print_output_data(this, _("** symtab xindex")); }
2040
 
2041
 private:
2042
  template<bool big_endian>
2043
  void
2044
  endian_do_write(unsigned char*);
2045
 
2046
  // It is likely that most symbols will not require entries.  Rather
2047
  // than keep a vector for all symbols, we keep pairs of symbol index
2048
  // and section index.
2049
  typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2050
 
2051
  // The entries we need.
2052
  Xindex_entries entries_;
2053
};
2054
 
2055
// A relaxed input section.
2056
class Output_relaxed_input_section : public Output_section_data_build
2057
{
2058
 public:
2059
  // We would like to call relobj->section_addralign(shndx) to get the
2060
  // alignment but we do not want the constructor to fail.  So callers
2061
  // are repsonsible for ensuring that.
2062
  Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2063
                               uint64_t addralign)
2064
    : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2065
  { }
2066
 
2067
  // Return the Relobj of this relaxed input section.
2068
  Relobj*
2069
  relobj() const
2070
  { return this->relobj_; }
2071
 
2072
  // Return the section index of this relaxed input section.
2073
  unsigned int
2074
  shndx() const
2075
  { return this->shndx_; }
2076
 
2077
 private:
2078
  Relobj* relobj_;
2079
  unsigned int shndx_;
2080
};
2081
 
2082
// An output section.  We don't expect to have too many output
2083
// sections, so we don't bother to do a template on the size.
2084
 
2085
class Output_section : public Output_data
2086
{
2087
 public:
2088
  // Create an output section, giving the name, type, and flags.
2089
  Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2090
  virtual ~Output_section();
2091
 
2092
  // Add a new input section SHNDX, named NAME, with header SHDR, from
2093
  // object OBJECT.  RELOC_SHNDX is the index of a relocation section
2094
  // which applies to this section, or 0 if none, or -1 if more than
2095
  // one.  HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2096
  // in a linker script; in that case we need to keep track of input
2097
  // sections associated with an output section.  Return the offset
2098
  // within the output section.
2099
  template<int size, bool big_endian>
2100
  off_t
2101
  add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
2102
                    const char *name,
2103
                    const elfcpp::Shdr<size, big_endian>& shdr,
2104
                    unsigned int reloc_shndx, bool have_sections_script);
2105
 
2106
  // Add generated data POSD to this output section.
2107
  void
2108
  add_output_section_data(Output_section_data* posd);
2109
 
2110
  // Add a relaxed input section PORIS to this output section.
2111
  void
2112
  add_relaxed_input_section(Output_relaxed_input_section* poris);
2113
 
2114
  // Return the section name.
2115
  const char*
2116
  name() const
2117
  { return this->name_; }
2118
 
2119
  // Return the section type.
2120
  elfcpp::Elf_Word
2121
  type() const
2122
  { return this->type_; }
2123
 
2124
  // Return the section flags.
2125
  elfcpp::Elf_Xword
2126
  flags() const
2127
  { return this->flags_; }
2128
 
2129
  // Update the output section flags based on input section flags.
2130
  void
2131
  update_flags_for_input_section(elfcpp::Elf_Xword flags);
2132
 
2133
  // Return the entsize field.
2134
  uint64_t
2135
  entsize() const
2136
  { return this->entsize_; }
2137
 
2138
  // Set the entsize field.
2139
  void
2140
  set_entsize(uint64_t v);
2141
 
2142
  // Set the load address.
2143
  void
2144
  set_load_address(uint64_t load_address)
2145
  {
2146
    this->load_address_ = load_address;
2147
    this->has_load_address_ = true;
2148
  }
2149
 
2150
  // Set the link field to the output section index of a section.
2151
  void
2152
  set_link_section(const Output_data* od)
2153
  {
2154
    gold_assert(this->link_ == 0
2155
                && !this->should_link_to_symtab_
2156
                && !this->should_link_to_dynsym_);
2157
    this->link_section_ = od;
2158
  }
2159
 
2160
  // Set the link field to a constant.
2161
  void
2162
  set_link(unsigned int v)
2163
  {
2164
    gold_assert(this->link_section_ == NULL
2165
                && !this->should_link_to_symtab_
2166
                && !this->should_link_to_dynsym_);
2167
    this->link_ = v;
2168
  }
2169
 
2170
  // Record that this section should link to the normal symbol table.
2171
  void
2172
  set_should_link_to_symtab()
2173
  {
2174
    gold_assert(this->link_section_ == NULL
2175
                && this->link_ == 0
2176
                && !this->should_link_to_dynsym_);
2177
    this->should_link_to_symtab_ = true;
2178
  }
2179
 
2180
  // Record that this section should link to the dynamic symbol table.
2181
  void
2182
  set_should_link_to_dynsym()
2183
  {
2184
    gold_assert(this->link_section_ == NULL
2185
                && this->link_ == 0
2186
                && !this->should_link_to_symtab_);
2187
    this->should_link_to_dynsym_ = true;
2188
  }
2189
 
2190
  // Return the info field.
2191
  unsigned int
2192
  info() const
2193
  {
2194
    gold_assert(this->info_section_ == NULL
2195
                && this->info_symndx_ == NULL);
2196
    return this->info_;
2197
  }
2198
 
2199
  // Set the info field to the output section index of a section.
2200
  void
2201
  set_info_section(const Output_section* os)
2202
  {
2203
    gold_assert((this->info_section_ == NULL
2204
                 || (this->info_section_ == os
2205
                     && this->info_uses_section_index_))
2206
                && this->info_symndx_ == NULL
2207
                && this->info_ == 0);
2208
    this->info_section_ = os;
2209
    this->info_uses_section_index_= true;
2210
  }
2211
 
2212
  // Set the info field to the symbol table index of a symbol.
2213
  void
2214
  set_info_symndx(const Symbol* sym)
2215
  {
2216
    gold_assert(this->info_section_ == NULL
2217
                && (this->info_symndx_ == NULL
2218
                    || this->info_symndx_ == sym)
2219
                && this->info_ == 0);
2220
    this->info_symndx_ = sym;
2221
  }
2222
 
2223
  // Set the info field to the symbol table index of a section symbol.
2224
  void
2225
  set_info_section_symndx(const Output_section* os)
2226
  {
2227
    gold_assert((this->info_section_ == NULL
2228
                 || (this->info_section_ == os
2229
                     && !this->info_uses_section_index_))
2230
                && this->info_symndx_ == NULL
2231
                && this->info_ == 0);
2232
    this->info_section_ = os;
2233
    this->info_uses_section_index_ = false;
2234
  }
2235
 
2236
  // Set the info field to a constant.
2237
  void
2238
  set_info(unsigned int v)
2239
  {
2240
    gold_assert(this->info_section_ == NULL
2241
                && this->info_symndx_ == NULL
2242
                && (this->info_ == 0
2243
                    || this->info_ == v));
2244
    this->info_ = v;
2245
  }
2246
 
2247
  // Set the addralign field.
2248
  void
2249
  set_addralign(uint64_t v)
2250
  { this->addralign_ = v; }
2251
 
2252
  // Whether the output section index has been set.
2253
  bool
2254
  has_out_shndx() const
2255
  { return this->out_shndx_ != -1U; }
2256
 
2257
  // Indicate that we need a symtab index.
2258
  void
2259
  set_needs_symtab_index()
2260
  { this->needs_symtab_index_ = true; }
2261
 
2262
  // Return whether we need a symtab index.
2263
  bool
2264
  needs_symtab_index() const
2265
  { return this->needs_symtab_index_; }
2266
 
2267
  // Get the symtab index.
2268
  unsigned int
2269
  symtab_index() const
2270
  {
2271
    gold_assert(this->symtab_index_ != 0);
2272
    return this->symtab_index_;
2273
  }
2274
 
2275
  // Set the symtab index.
2276
  void
2277
  set_symtab_index(unsigned int index)
2278
  {
2279
    gold_assert(index != 0);
2280
    this->symtab_index_ = index;
2281
  }
2282
 
2283
  // Indicate that we need a dynsym index.
2284
  void
2285
  set_needs_dynsym_index()
2286
  { this->needs_dynsym_index_ = true; }
2287
 
2288
  // Return whether we need a dynsym index.
2289
  bool
2290
  needs_dynsym_index() const
2291
  { return this->needs_dynsym_index_; }
2292
 
2293
  // Get the dynsym index.
2294
  unsigned int
2295
  dynsym_index() const
2296
  {
2297
    gold_assert(this->dynsym_index_ != 0);
2298
    return this->dynsym_index_;
2299
  }
2300
 
2301
  // Set the dynsym index.
2302
  void
2303
  set_dynsym_index(unsigned int index)
2304
  {
2305
    gold_assert(index != 0);
2306
    this->dynsym_index_ = index;
2307
  }
2308
 
2309
  // Return whether the input sections sections attachd to this output
2310
  // section may require sorting.  This is used to handle constructor
2311
  // priorities compatibly with GNU ld.
2312
  bool
2313
  may_sort_attached_input_sections() const
2314
  { return this->may_sort_attached_input_sections_; }
2315
 
2316
  // Record that the input sections attached to this output section
2317
  // may require sorting.
2318
  void
2319
  set_may_sort_attached_input_sections()
2320
  { this->may_sort_attached_input_sections_ = true; }
2321
 
2322
  // Return whether the input sections attached to this output section
2323
  // require sorting.  This is used to handle constructor priorities
2324
  // compatibly with GNU ld.
2325
  bool
2326
  must_sort_attached_input_sections() const
2327
  { return this->must_sort_attached_input_sections_; }
2328
 
2329
  // Record that the input sections attached to this output section
2330
  // require sorting.
2331
  void
2332
  set_must_sort_attached_input_sections()
2333
  { this->must_sort_attached_input_sections_ = true; }
2334
 
2335
  // Return whether this section holds relro data--data which has
2336
  // dynamic relocations but which may be marked read-only after the
2337
  // dynamic relocations have been completed.
2338
  bool
2339
  is_relro() const
2340
  { return this->is_relro_; }
2341
 
2342
  // Record that this section holds relro data.
2343
  void
2344
  set_is_relro()
2345
  { this->is_relro_ = true; }
2346
 
2347
  // Record that this section does not hold relro data.
2348
  void
2349
  clear_is_relro()
2350
  { this->is_relro_ = false; }
2351
 
2352
  // True if this section holds relro local data--relro data for which
2353
  // the dynamic relocations are all RELATIVE relocations.
2354
  bool
2355
  is_relro_local() const
2356
  { return this->is_relro_local_; }
2357
 
2358
  // Record that this section holds relro local data.
2359
  void
2360
  set_is_relro_local()
2361
  { this->is_relro_local_ = true; }
2362
 
2363
  // True if this is a small section: a section which holds small
2364
  // variables.
2365
  bool
2366
  is_small_section() const
2367
  { return this->is_small_section_; }
2368
 
2369
  // Record that this is a small section.
2370
  void
2371
  set_is_small_section()
2372
  { this->is_small_section_ = true; }
2373
 
2374
  // True if this is a large section: a section which holds large
2375
  // variables.
2376
  bool
2377
  is_large_section() const
2378
  { return this->is_large_section_; }
2379
 
2380
  // Record that this is a large section.
2381
  void
2382
  set_is_large_section()
2383
  { this->is_large_section_ = true; }
2384
 
2385
  // True if this is a large data (not BSS) section.
2386
  bool
2387
  is_large_data_section()
2388
  { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2389
 
2390
  // True if this is the .interp section which goes into the PT_INTERP
2391
  // segment.
2392
  bool
2393
  is_interp() const
2394
  { return this->is_interp_; }
2395
 
2396
  // Record that this is the interp section.
2397
  void
2398
  set_is_interp()
2399
  { this->is_interp_ = true; }
2400
 
2401
  // True if this is a section used by the dynamic linker.
2402
  bool
2403
  is_dynamic_linker_section() const
2404
  { return this->is_dynamic_linker_section_; }
2405
 
2406
  // Record that this is a section used by the dynamic linker.
2407
  void
2408
  set_is_dynamic_linker_section()
2409
  { this->is_dynamic_linker_section_ = true; }
2410
 
2411
  // Return whether this section should be written after all the input
2412
  // sections are complete.
2413
  bool
2414
  after_input_sections() const
2415
  { return this->after_input_sections_; }
2416
 
2417
  // Record that this section should be written after all the input
2418
  // sections are complete.
2419
  void
2420
  set_after_input_sections()
2421
  { this->after_input_sections_ = true; }
2422
 
2423
  // Return whether this section requires postprocessing after all
2424
  // relocations have been applied.
2425
  bool
2426
  requires_postprocessing() const
2427
  { return this->requires_postprocessing_; }
2428
 
2429
  // If a section requires postprocessing, return the buffer to use.
2430
  unsigned char*
2431
  postprocessing_buffer() const
2432
  {
2433
    gold_assert(this->postprocessing_buffer_ != NULL);
2434
    return this->postprocessing_buffer_;
2435
  }
2436
 
2437
  // If a section requires postprocessing, create the buffer to use.
2438
  void
2439
  create_postprocessing_buffer();
2440
 
2441
  // If a section requires postprocessing, this is the size of the
2442
  // buffer to which relocations should be applied.
2443
  off_t
2444
  postprocessing_buffer_size() const
2445
  { return this->current_data_size_for_child(); }
2446
 
2447
  // Modify the section name.  This is only permitted for an
2448
  // unallocated section, and only before the size has been finalized.
2449
  // Otherwise the name will not get into Layout::namepool_.
2450
  void
2451
  set_name(const char* newname)
2452
  {
2453
    gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2454
    gold_assert(!this->is_data_size_valid());
2455
    this->name_ = newname;
2456
  }
2457
 
2458
  // Return whether the offset OFFSET in the input section SHNDX in
2459
  // object OBJECT is being included in the link.
2460
  bool
2461
  is_input_address_mapped(const Relobj* object, unsigned int shndx,
2462
                          off_t offset) const;
2463
 
2464
  // Return the offset within the output section of OFFSET relative to
2465
  // the start of input section SHNDX in object OBJECT.
2466
  section_offset_type
2467
  output_offset(const Relobj* object, unsigned int shndx,
2468
                section_offset_type offset) const;
2469
 
2470
  // Return the output virtual address of OFFSET relative to the start
2471
  // of input section SHNDX in object OBJECT.
2472
  uint64_t
2473
  output_address(const Relobj* object, unsigned int shndx,
2474
                 off_t offset) const;
2475
 
2476
  // Look for the merged section for input section SHNDX in object
2477
  // OBJECT.  If found, return true, and set *ADDR to the address of
2478
  // the start of the merged section.  This is not necessary the
2479
  // output offset corresponding to input offset 0 in the section,
2480
  // since the section may be mapped arbitrarily.
2481
  bool
2482
  find_starting_output_address(const Relobj* object, unsigned int shndx,
2483
                               uint64_t* addr) const;
2484
 
2485
  // Record that this output section was found in the SECTIONS clause
2486
  // of a linker script.
2487
  void
2488
  set_found_in_sections_clause()
2489
  { this->found_in_sections_clause_ = true; }
2490
 
2491
  // Return whether this output section was found in the SECTIONS
2492
  // clause of a linker script.
2493
  bool
2494
  found_in_sections_clause() const
2495
  { return this->found_in_sections_clause_; }
2496
 
2497
  // Write the section header into *OPHDR.
2498
  template<int size, bool big_endian>
2499
  void
2500
  write_header(const Layout*, const Stringpool*,
2501
               elfcpp::Shdr_write<size, big_endian>*) const;
2502
 
2503
  // The next few calls are for linker script support.
2504
 
2505
  // We need to export the input sections to linker scripts.  Previously
2506
  // we export a pair of Relobj pointer and section index.  We now need to
2507
  // handle relaxed input sections as well.  So we use this class.
2508
  class Simple_input_section
2509
  {
2510
   private:
2511
    static const unsigned int invalid_shndx = static_cast<unsigned int>(-1);
2512
 
2513
   public:
2514
    Simple_input_section(Relobj *relobj, unsigned int shndx)
2515
      : shndx_(shndx)
2516
    {
2517
      gold_assert(shndx != invalid_shndx);
2518
      this->u_.relobj = relobj;
2519
    }
2520
 
2521
    Simple_input_section(Output_relaxed_input_section* section)
2522
      : shndx_(invalid_shndx)
2523
    { this->u_.relaxed_input_section = section; }
2524
 
2525
    // Whether this is a relaxed section.
2526
    bool
2527
    is_relaxed_input_section() const
2528
    { return this->shndx_ == invalid_shndx; }
2529
 
2530
    // Return object of an input section.
2531
    Relobj*
2532
    relobj() const
2533
    {
2534
      return ((this->shndx_ != invalid_shndx)
2535
              ? this->u_.relobj
2536
              : this->u_.relaxed_input_section->relobj());
2537
    }
2538
 
2539
    // Return index of an input section.
2540
    unsigned int
2541
    shndx() const
2542
    {
2543
      return ((this->shndx_ != invalid_shndx)
2544
              ? this->shndx_
2545
              : this->u_.relaxed_input_section->shndx());
2546
    }
2547
 
2548
    // Return the Output_relaxed_input_section object of a relaxed section.
2549
    Output_relaxed_input_section*
2550
    relaxed_input_section() const
2551
    {
2552
      gold_assert(this->shndx_ == invalid_shndx);
2553
      return this->u_.relaxed_input_section;
2554
    }
2555
 
2556
   private:
2557
    // Pointer to either an Relobj or an Output_relaxed_input_section.
2558
    union
2559
    {
2560
      Relobj* relobj;
2561
      Output_relaxed_input_section* relaxed_input_section;
2562
    } u_;
2563
    // Section index for an non-relaxed section or invalid_shndx for
2564
    // a relaxed section.
2565
    unsigned int shndx_;
2566
  };
2567
 
2568
  // Store the list of input sections for this Output_section into the
2569
  // list passed in.  This removes the input sections, leaving only
2570
  // any Output_section_data elements.  This returns the size of those
2571
  // Output_section_data elements.  ADDRESS is the address of this
2572
  // output section.  FILL is the fill value to use, in case there are
2573
  // any spaces between the remaining Output_section_data elements.
2574
  uint64_t
2575
  get_input_sections(uint64_t address, const std::string& fill,
2576
                     std::list<Simple_input_section>*);
2577
 
2578
  // Add an input section from a script.
2579
  void
2580
  add_input_section_for_script(const Simple_input_section& input_section,
2581
                               off_t data_size, uint64_t addralign);
2582
 
2583
  // Set the current size of the output section.
2584
  void
2585
  set_current_data_size(off_t size)
2586
  { this->set_current_data_size_for_child(size); }
2587
 
2588
  // Get the current size of the output section.
2589
  off_t
2590
  current_data_size() const
2591
  { return this->current_data_size_for_child(); }
2592
 
2593
  // End of linker script support.
2594
 
2595
  // Save states before doing section layout.
2596
  // This is used for relaxation.
2597
  void
2598
  save_states();
2599
 
2600
  // Restore states prior to section layout.
2601
  void
2602
  restore_states();
2603
 
2604
  // Convert existing input sections to relaxed input sections.
2605
  void
2606
  convert_input_sections_to_relaxed_sections(
2607
      const std::vector<Output_relaxed_input_section*>& sections);
2608
 
2609
  // Print merge statistics to stderr.
2610
  void
2611
  print_merge_stats();
2612
 
2613
 protected:
2614
  // Return the output section--i.e., the object itself.
2615
  Output_section*
2616
  do_output_section()
2617
  { return this; }
2618
 
2619
  // Return the section index in the output file.
2620
  unsigned int
2621
  do_out_shndx() const
2622
  {
2623
    gold_assert(this->out_shndx_ != -1U);
2624
    return this->out_shndx_;
2625
  }
2626
 
2627
  // Set the output section index.
2628
  void
2629
  do_set_out_shndx(unsigned int shndx)
2630
  {
2631
    gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
2632
    this->out_shndx_ = shndx;
2633
  }
2634
 
2635
  // Set the final data size of the Output_section.  For a typical
2636
  // Output_section, there is nothing to do, but if there are any
2637
  // Output_section_data objects we need to set their final addresses
2638
  // here.
2639
  virtual void
2640
  set_final_data_size();
2641
 
2642
  // Reset the address and file offset.
2643
  void
2644
  do_reset_address_and_file_offset();
2645
 
2646
  // Return true if address and file offset already have reset values. In
2647
  // other words, calling reset_address_and_file_offset will not change them.
2648
  bool
2649
  do_address_and_file_offset_have_reset_values() const;
2650
 
2651
  // Write the data to the file.  For a typical Output_section, this
2652
  // does nothing: the data is written out by calling Object::Relocate
2653
  // on each input object.  But if there are any Output_section_data
2654
  // objects we do need to write them out here.
2655
  virtual void
2656
  do_write(Output_file*);
2657
 
2658
  // Return the address alignment--function required by parent class.
2659
  uint64_t
2660
  do_addralign() const
2661
  { return this->addralign_; }
2662
 
2663
  // Return whether there is a load address.
2664
  bool
2665
  do_has_load_address() const
2666
  { return this->has_load_address_; }
2667
 
2668
  // Return the load address.
2669
  uint64_t
2670
  do_load_address() const
2671
  {
2672
    gold_assert(this->has_load_address_);
2673
    return this->load_address_;
2674
  }
2675
 
2676
  // Return whether this is an Output_section.
2677
  bool
2678
  do_is_section() const
2679
  { return true; }
2680
 
2681
  // Return whether this is a section of the specified type.
2682
  bool
2683
  do_is_section_type(elfcpp::Elf_Word type) const
2684
  { return this->type_ == type; }
2685
 
2686
  // Return whether the specified section flag is set.
2687
  bool
2688
  do_is_section_flag_set(elfcpp::Elf_Xword flag) const
2689
  { return (this->flags_ & flag) != 0; }
2690
 
2691
  // Set the TLS offset.  Called only for SHT_TLS sections.
2692
  void
2693
  do_set_tls_offset(uint64_t tls_base);
2694
 
2695
  // Return the TLS offset, relative to the base of the TLS segment.
2696
  // Valid only for SHT_TLS sections.
2697
  uint64_t
2698
  do_tls_offset() const
2699
  { return this->tls_offset_; }
2700
 
2701
  // This may be implemented by a child class.
2702
  virtual void
2703
  do_finalize_name(Layout*)
2704
  { }
2705
 
2706
  // Print to the map file.
2707
  virtual void
2708
  do_print_to_mapfile(Mapfile*) const;
2709
 
2710
  // Record that this section requires postprocessing after all
2711
  // relocations have been applied.  This is called by a child class.
2712
  void
2713
  set_requires_postprocessing()
2714
  {
2715
    this->requires_postprocessing_ = true;
2716
    this->after_input_sections_ = true;
2717
  }
2718
 
2719
  // Write all the data of an Output_section into the postprocessing
2720
  // buffer.
2721
  void
2722
  write_to_postprocessing_buffer();
2723
 
2724
  // In some cases we need to keep a list of the input sections
2725
  // associated with this output section.  We only need the list if we
2726
  // might have to change the offsets of the input section within the
2727
  // output section after we add the input section.  The ordinary
2728
  // input sections will be written out when we process the object
2729
  // file, and as such we don't need to track them here.  We do need
2730
  // to track Output_section_data objects here.  We store instances of
2731
  // this structure in a std::vector, so it must be a POD.  There can
2732
  // be many instances of this structure, so we use a union to save
2733
  // some space.
2734
  class Input_section
2735
  {
2736
   public:
2737
    Input_section()
2738
      : shndx_(0), p2align_(0)
2739
    {
2740
      this->u1_.data_size = 0;
2741
      this->u2_.object = NULL;
2742
    }
2743
 
2744
    // For an ordinary input section.
2745
    Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2746
                  uint64_t addralign)
2747
      : shndx_(shndx),
2748
        p2align_(ffsll(static_cast<long long>(addralign)))
2749
    {
2750
      gold_assert(shndx != OUTPUT_SECTION_CODE
2751
                  && shndx != MERGE_DATA_SECTION_CODE
2752
                  && shndx != MERGE_STRING_SECTION_CODE
2753
                  && shndx != RELAXED_INPUT_SECTION_CODE);
2754
      this->u1_.data_size = data_size;
2755
      this->u2_.object = object;
2756
    }
2757
 
2758
    // For a non-merge output section.
2759
    Input_section(Output_section_data* posd)
2760
      : shndx_(OUTPUT_SECTION_CODE), p2align_(0)
2761
    {
2762
      this->u1_.data_size = 0;
2763
      this->u2_.posd = posd;
2764
    }
2765
 
2766
    // For a merge section.
2767
    Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2768
      : shndx_(is_string
2769
               ? MERGE_STRING_SECTION_CODE
2770
               : MERGE_DATA_SECTION_CODE),
2771
        p2align_(0)
2772
    {
2773
      this->u1_.entsize = entsize;
2774
      this->u2_.posd = posd;
2775
    }
2776
 
2777
    // For a relaxed input section.
2778
    Input_section(Output_relaxed_input_section *psection)
2779
      : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0)
2780
    {
2781
      this->u1_.data_size = 0;
2782
      this->u2_.poris = psection;
2783
    }
2784
 
2785
    // The required alignment.
2786
    uint64_t
2787
    addralign() const
2788
    {
2789
      if (!this->is_input_section())
2790
        return this->u2_.posd->addralign();
2791
      return (this->p2align_ == 0
2792
              ? 0
2793
              : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2794
    }
2795
 
2796
    // Return the required size.
2797
    off_t
2798
    data_size() const;
2799
 
2800
    // Whether this is an input section.
2801
    bool
2802
    is_input_section() const
2803
    {
2804
      return (this->shndx_ != OUTPUT_SECTION_CODE
2805
              && this->shndx_ != MERGE_DATA_SECTION_CODE
2806
              && this->shndx_ != MERGE_STRING_SECTION_CODE
2807
              && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
2808
    }
2809
 
2810
    // Return whether this is a merge section which matches the
2811
    // parameters.
2812
    bool
2813
    is_merge_section(bool is_string, uint64_t entsize,
2814
                     uint64_t addralign) const
2815
    {
2816
      return (this->shndx_ == (is_string
2817
                               ? MERGE_STRING_SECTION_CODE
2818
                               : MERGE_DATA_SECTION_CODE)
2819
              && this->u1_.entsize == entsize
2820
              && this->addralign() == addralign);
2821
    }
2822
 
2823
    // Return whether this is a relaxed input section.
2824
    bool
2825
    is_relaxed_input_section() const
2826
    { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
2827
 
2828
    // Return whether this is a generic Output_section_data.
2829
    bool
2830
    is_output_section_data() const
2831
    {
2832
      return this->shndx_ == OUTPUT_SECTION_CODE;
2833
    }
2834
 
2835
    // Return the object for an input section.
2836
    Relobj*
2837
    relobj() const
2838
    {
2839
      if (this->is_input_section())
2840
        return this->u2_.object;
2841
      else if (this->is_relaxed_input_section())
2842
        return this->u2_.poris->relobj();
2843
      else
2844
        gold_unreachable();
2845
    }
2846
 
2847
    // Return the input section index for an input section.
2848
    unsigned int
2849
    shndx() const
2850
    {
2851
      if (this->is_input_section())
2852
        return this->shndx_;
2853
      else if (this->is_relaxed_input_section())
2854
        return this->u2_.poris->shndx();
2855
      else
2856
        gold_unreachable();
2857
    }
2858
 
2859
    // For non-input-sections, return the associated Output_section_data
2860
    // object.
2861
    Output_section_data*
2862
    output_section_data() const
2863
    {
2864
      gold_assert(!this->is_input_section());
2865
      return this->u2_.posd;
2866
    }
2867
 
2868
    // Return the Output_relaxed_input_section object.
2869
    Output_relaxed_input_section*
2870
    relaxed_input_section() const
2871
    {
2872
      gold_assert(this->is_relaxed_input_section());
2873
      return this->u2_.poris;
2874
    }
2875
 
2876
    // Set the output section.
2877
    void
2878
    set_output_section(Output_section* os)
2879
    {
2880
      gold_assert(!this->is_input_section());
2881
      Output_section_data *posd =
2882
        this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
2883
      posd->set_output_section(os);
2884
    }
2885
 
2886
    // Set the address and file offset.  This is called during
2887
    // Layout::finalize.  SECTION_FILE_OFFSET is the file offset of
2888
    // the enclosing section.
2889
    void
2890
    set_address_and_file_offset(uint64_t address, off_t file_offset,
2891
                                off_t section_file_offset);
2892
 
2893
    // Reset the address and file offset.
2894
    void
2895
    reset_address_and_file_offset();
2896
 
2897
    // Finalize the data size.
2898
    void
2899
    finalize_data_size();
2900
 
2901
    // Add an input section, for SHF_MERGE sections.
2902
    bool
2903
    add_input_section(Relobj* object, unsigned int shndx)
2904
    {
2905
      gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2906
                  || this->shndx_ == MERGE_STRING_SECTION_CODE);
2907
      return this->u2_.posd->add_input_section(object, shndx);
2908
    }
2909
 
2910
    // Given an input OBJECT, an input section index SHNDX within that
2911
    // object, and an OFFSET relative to the start of that input
2912
    // section, return whether or not the output offset is known.  If
2913
    // this function returns true, it sets *POUTPUT to the offset in
2914
    // the output section, relative to the start of the input section
2915
    // in the output section.  *POUTPUT may be different from OFFSET
2916
    // for a merged section.
2917
    bool
2918
    output_offset(const Relobj* object, unsigned int shndx,
2919
                  section_offset_type offset,
2920
                  section_offset_type *poutput) const;
2921
 
2922
    // Return whether this is the merge section for the input section
2923
    // SHNDX in OBJECT.
2924
    bool
2925
    is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2926
 
2927
    // Write out the data.  This does nothing for an input section.
2928
    void
2929
    write(Output_file*);
2930
 
2931
    // Write the data to a buffer.  This does nothing for an input
2932
    // section.
2933
    void
2934
    write_to_buffer(unsigned char*);
2935
 
2936
    // Print to a map file.
2937
    void
2938
    print_to_mapfile(Mapfile*) const;
2939
 
2940
    // Print statistics about merge sections to stderr.
2941
    void
2942
    print_merge_stats(const char* section_name)
2943
    {
2944
      if (this->shndx_ == MERGE_DATA_SECTION_CODE
2945
          || this->shndx_ == MERGE_STRING_SECTION_CODE)
2946
        this->u2_.posd->print_merge_stats(section_name);
2947
    }
2948
 
2949
   private:
2950
    // Code values which appear in shndx_.  If the value is not one of
2951
    // these codes, it is the input section index in the object file.
2952
    enum
2953
    {
2954
      // An Output_section_data.
2955
      OUTPUT_SECTION_CODE = -1U,
2956
      // An Output_section_data for an SHF_MERGE section with
2957
      // SHF_STRINGS not set.
2958
      MERGE_DATA_SECTION_CODE = -2U,
2959
      // An Output_section_data for an SHF_MERGE section with
2960
      // SHF_STRINGS set.
2961
      MERGE_STRING_SECTION_CODE = -3U,
2962
      // An Output_section_data for a relaxed input section.
2963
      RELAXED_INPUT_SECTION_CODE = -4U
2964
    };
2965
 
2966
    // For an ordinary input section, this is the section index in the
2967
    // input file.  For an Output_section_data, this is
2968
    // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2969
    // MERGE_STRING_SECTION_CODE.
2970
    unsigned int shndx_;
2971
    // The required alignment, stored as a power of 2.
2972
    unsigned int p2align_;
2973
    union
2974
    {
2975
      // For an ordinary input section, the section size.
2976
      off_t data_size;
2977
      // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
2978
      // used.  For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2979
      // entity size.
2980
      uint64_t entsize;
2981
    } u1_;
2982
    union
2983
    {
2984
      // For an ordinary input section, the object which holds the
2985
      // input section.
2986
      Relobj* object;
2987
      // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2988
      // MERGE_STRING_SECTION_CODE, the data.
2989
      Output_section_data* posd;
2990
      // For RELAXED_INPUT_SECTION_CODE, the data.
2991
      Output_relaxed_input_section* poris;
2992
    } u2_;
2993
  };
2994
 
2995
  typedef std::vector<Input_section> Input_section_list;
2996
 
2997
  // Allow a child class to access the input sections.
2998
  const Input_section_list&
2999
  input_sections() const
3000
  { return this->input_sections_; }
3001
 
3002
 private:
3003
  // We only save enough information to undo the effects of section layout.
3004
  class Checkpoint_output_section
3005
  {
3006
   public:
3007
    Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3008
                              const Input_section_list& input_sections,
3009
                              off_t first_input_offset,
3010
                              bool attached_input_sections_are_sorted)
3011
      : addralign_(addralign), flags_(flags),
3012
        input_sections_(input_sections),
3013
        input_sections_size_(input_sections_.size()),
3014
        input_sections_copy_(), first_input_offset_(first_input_offset),
3015
        attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3016
    { }
3017
 
3018
    virtual
3019
    ~Checkpoint_output_section()
3020
    { }
3021
 
3022
    // Return the address alignment.
3023
    uint64_t
3024
    addralign() const
3025
    { return this->addralign_; }
3026
 
3027
    // Return the section flags.
3028
    elfcpp::Elf_Xword
3029
    flags() const
3030
    { return this->flags_; }
3031
 
3032
    // Return a reference to the input section list copy.
3033
    Input_section_list*
3034
    input_sections()
3035
    { return &this->input_sections_copy_; }
3036
 
3037
    // Return the size of input_sections at the time when checkpoint is
3038
    // taken.
3039
    size_t
3040
    input_sections_size() const
3041
    { return this->input_sections_size_; }
3042
 
3043
    // Whether input sections are copied.
3044
    bool
3045
    input_sections_saved() const
3046
    { return this->input_sections_copy_.size() == this->input_sections_size_; }
3047
 
3048
    off_t
3049
    first_input_offset() const
3050
    { return this->first_input_offset_; }
3051
 
3052
    bool
3053
    attached_input_sections_are_sorted() const
3054
    { return this->attached_input_sections_are_sorted_; }
3055
 
3056
    // Save input sections.
3057
    void
3058
    save_input_sections()
3059
    {
3060
      this->input_sections_copy_.reserve(this->input_sections_size_);
3061
      this->input_sections_copy_.clear();
3062
      Input_section_list::const_iterator p = this->input_sections_.begin();
3063
      gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3064
      for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3065
        this->input_sections_copy_.push_back(*p);
3066
    }
3067
 
3068
   private:
3069
    // The section alignment.
3070
    uint64_t addralign_;
3071
    // The section flags.
3072
    elfcpp::Elf_Xword flags_;
3073
    // Reference to the input sections to be checkpointed.
3074
    const Input_section_list& input_sections_;
3075
    // Size of the checkpointed portion of input_sections_;
3076
    size_t input_sections_size_;
3077
    // Copy of input sections.
3078
    Input_section_list input_sections_copy_;
3079
    // The offset of the first entry in input_sections_.
3080
    off_t first_input_offset_;
3081
    // True if the input sections attached to this output section have
3082
    // already been sorted.
3083
    bool attached_input_sections_are_sorted_;
3084
  };
3085
 
3086
  // This class is used to sort the input sections.
3087
  class Input_section_sort_entry;
3088
 
3089
  // This is the sort comparison function.
3090
  struct Input_section_sort_compare
3091
  {
3092
    bool
3093
    operator()(const Input_section_sort_entry&,
3094
               const Input_section_sort_entry&) const;
3095
  };
3096
 
3097
  // Fill data.  This is used to fill in data between input sections.
3098
  // It is also used for data statements (BYTE, WORD, etc.) in linker
3099
  // scripts.  When we have to keep track of the input sections, we
3100
  // can use an Output_data_const, but we don't want to have to keep
3101
  // track of input sections just to implement fills.
3102
  class Fill
3103
  {
3104
   public:
3105
    Fill(off_t section_offset, off_t length)
3106
      : section_offset_(section_offset),
3107
        length_(convert_to_section_size_type(length))
3108
    { }
3109
 
3110
    // Return section offset.
3111
    off_t
3112
    section_offset() const
3113
    { return this->section_offset_; }
3114
 
3115
    // Return fill length.
3116
    section_size_type
3117
    length() const
3118
    { return this->length_; }
3119
 
3120
   private:
3121
    // The offset within the output section.
3122
    off_t section_offset_;
3123
    // The length of the space to fill.
3124
    section_size_type length_;
3125
  };
3126
 
3127
  typedef std::vector<Fill> Fill_list;
3128
 
3129
  // This class describes properties of merge data sections.  It is used
3130
  // as a key type for maps.
3131
  class Merge_section_properties
3132
  {
3133
   public:
3134
    Merge_section_properties(bool is_string, uint64_t entsize,
3135
                             uint64_t addralign)
3136
      : is_string_(is_string), entsize_(entsize), addralign_(addralign)
3137
    { }
3138
 
3139
    // Whether this equals to another Merge_section_properties MSP.
3140
    bool
3141
    eq(const Merge_section_properties& msp) const
3142
    {
3143
      return ((this->is_string_ == msp.is_string_)
3144
              && (this->entsize_ == msp.entsize_)
3145
              && (this->addralign_ == msp.addralign_));
3146
    }
3147
 
3148
    // Compute a hash value for this using 64-bit FNV-1a hash.
3149
    size_t
3150
    hash_value() const
3151
    {
3152
      uint64_t h = 14695981039346656037ULL;     // FNV offset basis.
3153
      uint64_t prime = 1099511628211ULL;
3154
      h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
3155
      h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
3156
      h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
3157
      return h;
3158
    }
3159
 
3160
    // Functors for associative containers.
3161
    struct equal_to
3162
    {
3163
      bool
3164
      operator()(const Merge_section_properties& msp1,
3165
                 const Merge_section_properties& msp2) const
3166
      { return msp1.eq(msp2); }
3167
    };
3168
 
3169
    struct hash
3170
    {
3171
      size_t
3172
      operator()(const Merge_section_properties& msp) const
3173
      { return msp.hash_value(); }
3174
    };
3175
 
3176
   private:
3177
    // Whether this merge data section is for strings.
3178
    bool is_string_;
3179
    // Entsize of this merge data section.
3180
    uint64_t entsize_;
3181
    // Address alignment.
3182
    uint64_t addralign_;
3183
  };
3184
 
3185
  // Map that link Merge_section_properties to Output_merge_base.
3186
  typedef Unordered_map<Merge_section_properties, Output_merge_base*,
3187
                        Merge_section_properties::hash,
3188
                        Merge_section_properties::equal_to>
3189
    Merge_section_by_properties_map;
3190
 
3191
  // Map that link Input_section_specifier to Output_section_data.
3192
  typedef Unordered_map<Input_section_specifier, Output_section_data*,
3193
                        Input_section_specifier::hash,
3194
                        Input_section_specifier::equal_to>
3195
    Output_section_data_by_input_section_map;
3196
 
3197
  // Map used during relaxation of existing sections.  This map
3198
  // an input section specifier to an input section list index.
3199
  // We assume that Input_section_list is a vector.
3200
  typedef Unordered_map<Input_section_specifier, size_t,
3201
                        Input_section_specifier::hash,
3202
                        Input_section_specifier::equal_to>
3203
    Relaxation_map;
3204
 
3205
  // Add a new output section by Input_section.
3206
  void
3207
  add_output_section_data(Input_section*);
3208
 
3209
  // Add an SHF_MERGE input section.  Returns true if the section was
3210
  // handled.
3211
  bool
3212
  add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3213
                          uint64_t entsize, uint64_t addralign);
3214
 
3215
  // Add an output SHF_MERGE section POSD to this output section.
3216
  // IS_STRING indicates whether it is a SHF_STRINGS section, and
3217
  // ENTSIZE is the entity size.  This returns the entry added to
3218
  // input_sections_.
3219
  void
3220
  add_output_merge_section(Output_section_data* posd, bool is_string,
3221
                           uint64_t entsize);
3222
 
3223
  // Sort the attached input sections.
3224
  void
3225
  sort_attached_input_sections();
3226
 
3227
  // Find the merge section into which an input section with index SHNDX in
3228
  // OBJECT has been added.  Return NULL if none found.
3229
  Output_section_data*
3230
  find_merge_section(const Relobj* object, unsigned int shndx) const;
3231
 
3232
  // Find a relaxed input section to an input section in OBJECT
3233
  // with index SHNDX.  Return NULL if none is found.
3234
  const Output_section_data*
3235
  find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3236
 
3237
  // Build a relaxation map.
3238
  void
3239
  build_relaxation_map(
3240
      const Input_section_list& input_sections,
3241
      size_t limit,
3242
      Relaxation_map* map) const;
3243
 
3244
  // Convert input sections in an input section list into relaxed sections.
3245
  void
3246
  convert_input_sections_in_list_to_relaxed_sections(
3247
      const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3248
      const Relaxation_map& map,
3249
      Input_section_list* input_sections);
3250
 
3251
  // Most of these fields are only valid after layout.
3252
 
3253
  // The name of the section.  This will point into a Stringpool.
3254
  const char* name_;
3255
  // The section address is in the parent class.
3256
  // The section alignment.
3257
  uint64_t addralign_;
3258
  // The section entry size.
3259
  uint64_t entsize_;
3260
  // The load address.  This is only used when using a linker script
3261
  // with a SECTIONS clause.  The has_load_address_ field indicates
3262
  // whether this field is valid.
3263
  uint64_t load_address_;
3264
  // The file offset is in the parent class.
3265
  // Set the section link field to the index of this section.
3266
  const Output_data* link_section_;
3267
  // If link_section_ is NULL, this is the link field.
3268
  unsigned int link_;
3269
  // Set the section info field to the index of this section.
3270
  const Output_section* info_section_;
3271
  // If info_section_ is NULL, set the info field to the symbol table
3272
  // index of this symbol.
3273
  const Symbol* info_symndx_;
3274
  // If info_section_ and info_symndx_ are NULL, this is the section
3275
  // info field.
3276
  unsigned int info_;
3277
  // The section type.
3278
  const elfcpp::Elf_Word type_;
3279
  // The section flags.
3280
  elfcpp::Elf_Xword flags_;
3281
  // The section index.
3282
  unsigned int out_shndx_;
3283
  // If there is a STT_SECTION for this output section in the normal
3284
  // symbol table, this is the symbol index.  This starts out as zero.
3285
  // It is initialized in Layout::finalize() to be the index, or -1U
3286
  // if there isn't one.
3287
  unsigned int symtab_index_;
3288
  // If there is a STT_SECTION for this output section in the dynamic
3289
  // symbol table, this is the symbol index.  This starts out as zero.
3290
  // It is initialized in Layout::finalize() to be the index, or -1U
3291
  // if there isn't one.
3292
  unsigned int dynsym_index_;
3293
  // The input sections.  This will be empty in cases where we don't
3294
  // need to keep track of them.
3295
  Input_section_list input_sections_;
3296
  // The offset of the first entry in input_sections_.
3297
  off_t first_input_offset_;
3298
  // The fill data.  This is separate from input_sections_ because we
3299
  // often will need fill sections without needing to keep track of
3300
  // input sections.
3301
  Fill_list fills_;
3302
  // If the section requires postprocessing, this buffer holds the
3303
  // section contents during relocation.
3304
  unsigned char* postprocessing_buffer_;
3305
  // Whether this output section needs a STT_SECTION symbol in the
3306
  // normal symbol table.  This will be true if there is a relocation
3307
  // which needs it.
3308
  bool needs_symtab_index_ : 1;
3309
  // Whether this output section needs a STT_SECTION symbol in the
3310
  // dynamic symbol table.  This will be true if there is a dynamic
3311
  // relocation which needs it.
3312
  bool needs_dynsym_index_ : 1;
3313
  // Whether the link field of this output section should point to the
3314
  // normal symbol table.
3315
  bool should_link_to_symtab_ : 1;
3316
  // Whether the link field of this output section should point to the
3317
  // dynamic symbol table.
3318
  bool should_link_to_dynsym_ : 1;
3319
  // Whether this section should be written after all the input
3320
  // sections are complete.
3321
  bool after_input_sections_ : 1;
3322
  // Whether this section requires post processing after all
3323
  // relocations have been applied.
3324
  bool requires_postprocessing_ : 1;
3325
  // Whether an input section was mapped to this output section
3326
  // because of a SECTIONS clause in a linker script.
3327
  bool found_in_sections_clause_ : 1;
3328
  // Whether this section has an explicitly specified load address.
3329
  bool has_load_address_ : 1;
3330
  // True if the info_section_ field means the section index of the
3331
  // section, false if it means the symbol index of the corresponding
3332
  // section symbol.
3333
  bool info_uses_section_index_ : 1;
3334
  // True if the input sections attached to this output section may
3335
  // need sorting.
3336
  bool may_sort_attached_input_sections_ : 1;
3337
  // True if the input sections attached to this output section must
3338
  // be sorted.
3339
  bool must_sort_attached_input_sections_ : 1;
3340
  // True if the input sections attached to this output section have
3341
  // already been sorted.
3342
  bool attached_input_sections_are_sorted_ : 1;
3343
  // True if this section holds relro data.
3344
  bool is_relro_ : 1;
3345
  // True if this section holds relro local data.
3346
  bool is_relro_local_ : 1;
3347
  // True if this is a small section.
3348
  bool is_small_section_ : 1;
3349
  // True if this is a large section.
3350
  bool is_large_section_ : 1;
3351
  // True if this is the .interp section going into the PT_INTERP
3352
  // segment.
3353
  bool is_interp_ : 1;
3354
  // True if this is section is read by the dynamic linker.
3355
  bool is_dynamic_linker_section_ : 1;
3356
  // Whether code-fills are generated at write.
3357
  bool generate_code_fills_at_write_ : 1;
3358
  // For SHT_TLS sections, the offset of this section relative to the base
3359
  // of the TLS segment.
3360
  uint64_t tls_offset_;
3361
  // Saved checkpoint.
3362
  Checkpoint_output_section* checkpoint_;
3363
  // Map from input sections to merge sections.
3364
  Output_section_data_by_input_section_map merge_section_map_;
3365
  // Map from merge section properties to merge_sections;
3366
  Merge_section_by_properties_map merge_section_by_properties_map_;
3367
  // Map from input sections to relaxed input sections.  This is mutable
3368
  // because it is updated lazily.  We may need to update it in a
3369
  // const qualified method.
3370
  mutable Output_section_data_by_input_section_map relaxed_input_section_map_;
3371
  // Whether relaxed_input_section_map_ is valid.
3372
  mutable bool is_relaxed_input_section_map_valid_;
3373
};
3374
 
3375
// An output segment.  PT_LOAD segments are built from collections of
3376
// output sections.  Other segments typically point within PT_LOAD
3377
// segments, and are built directly as needed.
3378
//
3379
// NOTE: We want to use the copy constructor for this class.  During
3380
// relaxation, we may try built the segments multiple times.  We do
3381
// that by copying the original segment list before lay-out, doing
3382
// a trial lay-out and roll-back to the saved copied if we need to
3383
// to the lay-out again.
3384
 
3385
class Output_segment
3386
{
3387
 public:
3388
  // Create an output segment, specifying the type and flags.
3389
  Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3390
 
3391
  // Return the virtual address.
3392
  uint64_t
3393
  vaddr() const
3394
  { return this->vaddr_; }
3395
 
3396
  // Return the physical address.
3397
  uint64_t
3398
  paddr() const
3399
  { return this->paddr_; }
3400
 
3401
  // Return the segment type.
3402
  elfcpp::Elf_Word
3403
  type() const
3404
  { return this->type_; }
3405
 
3406
  // Return the segment flags.
3407
  elfcpp::Elf_Word
3408
  flags() const
3409
  { return this->flags_; }
3410
 
3411
  // Return the memory size.
3412
  uint64_t
3413
  memsz() const
3414
  { return this->memsz_; }
3415
 
3416
  // Return the file size.
3417
  off_t
3418
  filesz() const
3419
  { return this->filesz_; }
3420
 
3421
  // Return the file offset.
3422
  off_t
3423
  offset() const
3424
  { return this->offset_; }
3425
 
3426
  // Whether this is a segment created to hold large data sections.
3427
  bool
3428
  is_large_data_segment() const
3429
  { return this->is_large_data_segment_; }
3430
 
3431
  // Record that this is a segment created to hold large data
3432
  // sections.
3433
  void
3434
  set_is_large_data_segment()
3435
  { this->is_large_data_segment_ = true; }
3436
 
3437
  // Return the maximum alignment of the Output_data.
3438
  uint64_t
3439
  maximum_alignment();
3440
 
3441
  // Add the Output_section OS to this segment.  SEG_FLAGS is the
3442
  // segment flags to use.  DO_SORT is true if we should sort the
3443
  // placement of the input section for more efficient generated code.
3444
  void
3445
  add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags,
3446
                     bool do_sort);
3447
 
3448
  // Remove an Output_section from this segment.  It is an error if it
3449
  // is not present.
3450
  void
3451
  remove_output_section(Output_section* os);
3452
 
3453
  // Add an Output_data (which is not an Output_section) to the start
3454
  // of this segment.
3455
  void
3456
  add_initial_output_data(Output_data*);
3457
 
3458
  // Return true if this segment has any sections which hold actual
3459
  // data, rather than being a BSS section.
3460
  bool
3461
  has_any_data_sections() const
3462
  { return !this->output_data_.empty(); }
3463
 
3464
  // Return the number of dynamic relocations applied to this segment.
3465
  unsigned int
3466
  dynamic_reloc_count() const;
3467
 
3468
  // Return the address of the first section.
3469
  uint64_t
3470
  first_section_load_address() const;
3471
 
3472
  // Return whether the addresses have been set already.
3473
  bool
3474
  are_addresses_set() const
3475
  { return this->are_addresses_set_; }
3476
 
3477
  // Set the addresses.
3478
  void
3479
  set_addresses(uint64_t vaddr, uint64_t paddr)
3480
  {
3481
    this->vaddr_ = vaddr;
3482
    this->paddr_ = paddr;
3483
    this->are_addresses_set_ = true;
3484
  }
3485
 
3486
  // Set the segment flags.  This is only used if we have a PHDRS
3487
  // clause which explicitly specifies the flags.
3488
  void
3489
  set_flags(elfcpp::Elf_Word flags)
3490
  { this->flags_ = flags; }
3491
 
3492
  // Set the address of the segment to ADDR and the offset to *POFF
3493
  // and set the addresses and offsets of all contained output
3494
  // sections accordingly.  Set the section indexes of all contained
3495
  // output sections starting with *PSHNDX.  If RESET is true, first
3496
  // reset the addresses of the contained sections.  Return the
3497
  // address of the immediately following segment.  Update *POFF and
3498
  // *PSHNDX.  This should only be called for a PT_LOAD segment.
3499
  uint64_t
3500
  set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
3501
                        unsigned int* pshndx);
3502
 
3503
  // Set the minimum alignment of this segment.  This may be adjusted
3504
  // upward based on the section alignments.
3505
  void
3506
  set_minimum_p_align(uint64_t align)
3507
  { this->min_p_align_ = align; }
3508
 
3509
  // Set the offset of this segment based on the section.  This should
3510
  // only be called for a non-PT_LOAD segment.
3511
  void
3512
  set_offset();
3513
 
3514
  // Set the TLS offsets of the sections contained in the PT_TLS segment.
3515
  void
3516
  set_tls_offsets();
3517
 
3518
  // Return the number of output sections.
3519
  unsigned int
3520
  output_section_count() const;
3521
 
3522
  // Return the section attached to the list segment with the lowest
3523
  // load address.  This is used when handling a PHDRS clause in a
3524
  // linker script.
3525
  Output_section*
3526
  section_with_lowest_load_address() const;
3527
 
3528
  // Write the segment header into *OPHDR.
3529
  template<int size, bool big_endian>
3530
  void
3531
  write_header(elfcpp::Phdr_write<size, big_endian>*);
3532
 
3533
  // Write the section headers of associated sections into V.
3534
  template<int size, bool big_endian>
3535
  unsigned char*
3536
  write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
3537
                        unsigned int* pshndx) const;
3538
 
3539
  // Print the output sections in the map file.
3540
  void
3541
  print_sections_to_mapfile(Mapfile*) const;
3542
 
3543
 private:
3544
  typedef std::list<Output_data*> Output_data_list;
3545
 
3546
  // Find the maximum alignment in an Output_data_list.
3547
  static uint64_t
3548
  maximum_alignment_list(const Output_data_list*);
3549
 
3550
  // Return whether the first data section is a relro section.
3551
  bool
3552
  is_first_section_relro() const;
3553
 
3554
  // Set the section addresses in an Output_data_list.
3555
  uint64_t
3556
  set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3557
                             uint64_t addr, off_t* poff, unsigned int* pshndx,
3558
                             bool* in_tls, bool* in_relro);
3559
 
3560
  // Return the number of Output_sections in an Output_data_list.
3561
  unsigned int
3562
  output_section_count_list(const Output_data_list*) const;
3563
 
3564
  // Return the number of dynamic relocs in an Output_data_list.
3565
  unsigned int
3566
  dynamic_reloc_count_list(const Output_data_list*) const;
3567
 
3568
  // Find the section with the lowest load address in an
3569
  // Output_data_list.
3570
  void
3571
  lowest_load_address_in_list(const Output_data_list* pdl,
3572
                              Output_section** found,
3573
                              uint64_t* found_lma) const;
3574
 
3575
  // Write the section headers in the list into V.
3576
  template<int size, bool big_endian>
3577
  unsigned char*
3578
  write_section_headers_list(const Layout*, const Stringpool*,
3579
                             const Output_data_list*, unsigned char* v,
3580
                             unsigned int* pshdx) const;
3581
 
3582
  // Print a section list to the mapfile.
3583
  void
3584
  print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3585
 
3586
  // NOTE: We want to use the copy constructor.  Currently, shallow copy
3587
  // works for us so we do not need to write our own copy constructor.
3588
 
3589
  // The list of output data with contents attached to this segment.
3590
  Output_data_list output_data_;
3591
  // The list of output data without contents attached to this segment.
3592
  Output_data_list output_bss_;
3593
  // The segment virtual address.
3594
  uint64_t vaddr_;
3595
  // The segment physical address.
3596
  uint64_t paddr_;
3597
  // The size of the segment in memory.
3598
  uint64_t memsz_;
3599
  // The maximum section alignment.  The is_max_align_known_ field
3600
  // indicates whether this has been finalized.
3601
  uint64_t max_align_;
3602
  // The required minimum value for the p_align field.  This is used
3603
  // for PT_LOAD segments.  Note that this does not mean that
3604
  // addresses should be aligned to this value; it means the p_paddr
3605
  // and p_vaddr fields must be congruent modulo this value.  For
3606
  // non-PT_LOAD segments, the dynamic linker works more efficiently
3607
  // if the p_align field has the more conventional value, although it
3608
  // can align as needed.
3609
  uint64_t min_p_align_;
3610
  // The offset of the segment data within the file.
3611
  off_t offset_;
3612
  // The size of the segment data in the file.
3613
  off_t filesz_;
3614
  // The segment type;
3615
  elfcpp::Elf_Word type_;
3616
  // The segment flags.
3617
  elfcpp::Elf_Word flags_;
3618
  // Whether we have finalized max_align_.
3619
  bool is_max_align_known_ : 1;
3620
  // Whether vaddr and paddr were set by a linker script.
3621
  bool are_addresses_set_ : 1;
3622
  // Whether this segment holds large data sections.
3623
  bool is_large_data_segment_ : 1;
3624
};
3625
 
3626
// This class represents the output file.
3627
 
3628
class Output_file
3629
{
3630
 public:
3631
  Output_file(const char* name);
3632
 
3633
  // Indicate that this is a temporary file which should not be
3634
  // output.
3635
  void
3636
  set_is_temporary()
3637
  { this->is_temporary_ = true; }
3638
 
3639
  // Try to open an existing file. Returns false if the file doesn't
3640
  // exist, has a size of 0 or can't be mmaped.  This method is
3641
  // thread-unsafe.
3642
  bool
3643
  open_for_modification();
3644
 
3645
  // Open the output file.  FILE_SIZE is the final size of the file.
3646
  // If the file already exists, it is deleted/truncated.  This method
3647
  // is thread-unsafe.
3648
  void
3649
  open(off_t file_size);
3650
 
3651
  // Resize the output file.  This method is thread-unsafe.
3652
  void
3653
  resize(off_t file_size);
3654
 
3655
  // Close the output file (flushing all buffered data) and make sure
3656
  // there are no errors.  This method is thread-unsafe.
3657
  void
3658
  close();
3659
 
3660
  // Return the size of this file.
3661
  off_t
3662
  filesize()
3663
  { return this->file_size_; }
3664
 
3665
  // We currently always use mmap which makes the view handling quite
3666
  // simple.  In the future we may support other approaches.
3667
 
3668
  // Write data to the output file.
3669
  void
3670
  write(off_t offset, const void* data, size_t len)
3671
  { memcpy(this->base_ + offset, data, len); }
3672
 
3673
  // Get a buffer to use to write to the file, given the offset into
3674
  // the file and the size.
3675
  unsigned char*
3676
  get_output_view(off_t start, size_t size)
3677
  {
3678
    gold_assert(start >= 0
3679
                && start + static_cast<off_t>(size) <= this->file_size_);
3680
    return this->base_ + start;
3681
  }
3682
 
3683
  // VIEW must have been returned by get_output_view.  Write the
3684
  // buffer to the file, passing in the offset and the size.
3685
  void
3686
  write_output_view(off_t, size_t, unsigned char*)
3687
  { }
3688
 
3689
  // Get a read/write buffer.  This is used when we want to write part
3690
  // of the file, read it in, and write it again.
3691
  unsigned char*
3692
  get_input_output_view(off_t start, size_t size)
3693
  { return this->get_output_view(start, size); }
3694
 
3695
  // Write a read/write buffer back to the file.
3696
  void
3697
  write_input_output_view(off_t, size_t, unsigned char*)
3698
  { }
3699
 
3700
  // Get a read buffer.  This is used when we just want to read part
3701
  // of the file back it in.
3702
  const unsigned char*
3703
  get_input_view(off_t start, size_t size)
3704
  { return this->get_output_view(start, size); }
3705
 
3706
  // Release a read bfufer.
3707
  void
3708
  free_input_view(off_t, size_t, const unsigned char*)
3709
  { }
3710
 
3711
 private:
3712
  // Map the file into memory or, if that fails, allocate anonymous
3713
  // memory.
3714
  void
3715
  map();
3716
 
3717
  // Allocate anonymous memory for the file.
3718
  bool
3719
  map_anonymous();
3720
 
3721
  // Map the file into memory.
3722
  bool
3723
  map_no_anonymous();
3724
 
3725
  // Unmap the file from memory (and flush to disk buffers).
3726
  void
3727
  unmap();
3728
 
3729
  // File name.
3730
  const char* name_;
3731
  // File descriptor.
3732
  int o_;
3733
  // File size.
3734
  off_t file_size_;
3735
  // Base of file mapped into memory.
3736
  unsigned char* base_;
3737
  // True iff base_ points to a memory buffer rather than an output file.
3738
  bool map_is_anonymous_;
3739
  // True if this is a temporary file which should not be output.
3740
  bool is_temporary_;
3741
};
3742
 
3743
} // End namespace gold.
3744
 
3745
#endif // !defined(GOLD_OUTPUT_H)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.