OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [output.h] - Blame information for rev 178

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

Line No. Rev Author Line
1 27 khays
// output.h -- manage the output file for gold   -*- C++ -*-
2
 
3
// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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
template<int size, bool big_endian>
50
class Sized_relobj_file;
51
 
52
// An abtract class for data which has to go into the output file.
53
 
54
class Output_data
55
{
56
 public:
57
  explicit Output_data()
58
    : address_(0), data_size_(0), offset_(-1),
59
      is_address_valid_(false), is_data_size_valid_(false),
60
      is_offset_valid_(false), is_data_size_fixed_(false),
61
      has_dynamic_reloc_(false)
62
  { }
63
 
64
  virtual
65
  ~Output_data();
66
 
67
  // Return the address.  For allocated sections, this is only valid
68
  // after Layout::finalize is finished.
69
  uint64_t
70
  address() const
71
  {
72
    gold_assert(this->is_address_valid_);
73
    return this->address_;
74
  }
75
 
76
  // Return the size of the data.  For allocated sections, this must
77
  // be valid after Layout::finalize calls set_address, but need not
78
  // be valid before then.
79
  off_t
80
  data_size() const
81
  {
82
    gold_assert(this->is_data_size_valid_);
83
    return this->data_size_;
84
  }
85
 
86
  // Get the current data size.
87
  off_t
88
  current_data_size() const
89
  { return this->current_data_size_for_child(); }
90
 
91
  // Return true if data size is fixed.
92
  bool
93
  is_data_size_fixed() const
94
  { return this->is_data_size_fixed_; }
95
 
96
  // Return the file offset.  This is only valid after
97
  // Layout::finalize is finished.  For some non-allocated sections,
98
  // it may not be valid until near the end of the link.
99
  off_t
100
  offset() const
101
  {
102
    gold_assert(this->is_offset_valid_);
103
    return this->offset_;
104
  }
105
 
106
  // Reset the address and file offset.  This essentially disables the
107
  // sanity testing about duplicate and unknown settings.
108
  void
109
  reset_address_and_file_offset()
110
  {
111
    this->is_address_valid_ = false;
112
    this->is_offset_valid_ = false;
113
    if (!this->is_data_size_fixed_)
114
      this->is_data_size_valid_ = false;
115
    this->do_reset_address_and_file_offset();
116
  }
117
 
118
  // Return true if address and file offset already have reset values. In
119
  // other words, calling reset_address_and_file_offset will not change them.
120
  bool
121
  address_and_file_offset_have_reset_values() const
122
  { return this->do_address_and_file_offset_have_reset_values(); }
123
 
124
  // Return the required alignment.
125
  uint64_t
126
  addralign() const
127
  { return this->do_addralign(); }
128
 
129
  // Return whether this has a load address.
130
  bool
131
  has_load_address() const
132
  { return this->do_has_load_address(); }
133
 
134
  // Return the load address.
135
  uint64_t
136
  load_address() const
137
  { return this->do_load_address(); }
138
 
139
  // Return whether this is an Output_section.
140
  bool
141
  is_section() const
142
  { return this->do_is_section(); }
143
 
144
  // Return whether this is an Output_section of the specified type.
145
  bool
146
  is_section_type(elfcpp::Elf_Word stt) const
147
  { return this->do_is_section_type(stt); }
148
 
149
  // Return whether this is an Output_section with the specified flag
150
  // set.
151
  bool
152
  is_section_flag_set(elfcpp::Elf_Xword shf) const
153
  { return this->do_is_section_flag_set(shf); }
154
 
155
  // Return the output section that this goes in, if there is one.
156
  Output_section*
157
  output_section()
158
  { return this->do_output_section(); }
159
 
160
  const Output_section*
161
  output_section() const
162
  { return this->do_output_section(); }
163
 
164
  // Return the output section index, if there is an output section.
165
  unsigned int
166
  out_shndx() const
167
  { return this->do_out_shndx(); }
168
 
169
  // Set the output section index, if this is an output section.
170
  void
171
  set_out_shndx(unsigned int shndx)
172
  { this->do_set_out_shndx(shndx); }
173
 
174
  // Set the address and file offset of this data, and finalize the
175
  // size of the data.  This is called during Layout::finalize for
176
  // allocated sections.
177
  void
178
  set_address_and_file_offset(uint64_t addr, off_t off)
179
  {
180
    this->set_address(addr);
181
    this->set_file_offset(off);
182
    this->finalize_data_size();
183
  }
184
 
185
  // Set the address.
186
  void
187
  set_address(uint64_t addr)
188
  {
189
    gold_assert(!this->is_address_valid_);
190
    this->address_ = addr;
191
    this->is_address_valid_ = true;
192
  }
193
 
194
  // Set the file offset.
195
  void
196
  set_file_offset(off_t off)
197
  {
198
    gold_assert(!this->is_offset_valid_);
199
    this->offset_ = off;
200
    this->is_offset_valid_ = true;
201
  }
202
 
203
  // Update the data size without finalizing it.
204
  void
205
  pre_finalize_data_size()
206
  {
207
    if (!this->is_data_size_valid_)
208
      {
209
        // Tell the child class to update the data size.
210
        this->update_data_size();
211
      }
212
  }
213
 
214
  // Finalize the data size.
215
  void
216
  finalize_data_size()
217
  {
218
    if (!this->is_data_size_valid_)
219
      {
220
        // Tell the child class to set the data size.
221
        this->set_final_data_size();
222
        gold_assert(this->is_data_size_valid_);
223
      }
224
  }
225
 
226
  // Set the TLS offset.  Called only for SHT_TLS sections.
227
  void
228
  set_tls_offset(uint64_t tls_base)
229
  { this->do_set_tls_offset(tls_base); }
230
 
231
  // Return the TLS offset, relative to the base of the TLS segment.
232
  // Valid only for SHT_TLS sections.
233
  uint64_t
234
  tls_offset() const
235
  { return this->do_tls_offset(); }
236
 
237
  // Write the data to the output file.  This is called after
238
  // Layout::finalize is complete.
239
  void
240
  write(Output_file* file)
241
  { this->do_write(file); }
242
 
243
  // This is called by Layout::finalize to note that the sizes of
244
  // allocated sections must now be fixed.
245
  static void
246
  layout_complete()
247
  { Output_data::allocated_sizes_are_fixed = true; }
248
 
249
  // Used to check that layout has been done.
250
  static bool
251
  is_layout_complete()
252
  { return Output_data::allocated_sizes_are_fixed; }
253
 
254
  // Note that a dynamic reloc has been applied to this data.
255
  void
256
  add_dynamic_reloc()
257
  { this->has_dynamic_reloc_ = true; }
258
 
259
  // Return whether a dynamic reloc has been applied.
260
  bool
261
  has_dynamic_reloc() const
262
  { return this->has_dynamic_reloc_; }
263
 
264
  // Whether the address is valid.
265
  bool
266
  is_address_valid() const
267
  { return this->is_address_valid_; }
268
 
269
  // Whether the file offset is valid.
270
  bool
271
  is_offset_valid() const
272
  { return this->is_offset_valid_; }
273
 
274
  // Whether the data size is valid.
275
  bool
276
  is_data_size_valid() const
277
  { return this->is_data_size_valid_; }
278
 
279
  // Print information to the map file.
280
  void
281
  print_to_mapfile(Mapfile* mapfile) const
282
  { return this->do_print_to_mapfile(mapfile); }
283
 
284
 protected:
285
  // Functions that child classes may or in some cases must implement.
286
 
287
  // Write the data to the output file.
288
  virtual void
289
  do_write(Output_file*) = 0;
290
 
291
  // Return the required alignment.
292
  virtual uint64_t
293
  do_addralign() const = 0;
294
 
295
  // Return whether this has a load address.
296
  virtual bool
297
  do_has_load_address() const
298
  { return false; }
299
 
300
  // Return the load address.
301
  virtual uint64_t
302
  do_load_address() const
303
  { gold_unreachable(); }
304
 
305
  // Return whether this is an Output_section.
306
  virtual bool
307
  do_is_section() const
308
  { return false; }
309
 
310
  // Return whether this is an Output_section of the specified type.
311
  // This only needs to be implement by Output_section.
312
  virtual bool
313
  do_is_section_type(elfcpp::Elf_Word) const
314
  { return false; }
315
 
316
  // Return whether this is an Output_section with the specific flag
317
  // set.  This only needs to be implemented by Output_section.
318
  virtual bool
319
  do_is_section_flag_set(elfcpp::Elf_Xword) const
320
  { return false; }
321
 
322
  // Return the output section, if there is one.
323
  virtual Output_section*
324
  do_output_section()
325
  { return NULL; }
326
 
327
  virtual const Output_section*
328
  do_output_section() const
329
  { return NULL; }
330
 
331
  // Return the output section index, if there is an output section.
332
  virtual unsigned int
333
  do_out_shndx() const
334
  { gold_unreachable(); }
335
 
336
  // Set the output section index, if this is an output section.
337
  virtual void
338
  do_set_out_shndx(unsigned int)
339
  { gold_unreachable(); }
340
 
341
  // This is a hook for derived classes to set the preliminary data size.
342
  // This is called by pre_finalize_data_size, normally called during
343
  // Layout::finalize, before the section address is set, and is used
344
  // during an incremental update, when we need to know the size of a
345
  // section before allocating space in the output file.  For classes
346
  // where the current data size is up to date, this default version of
347
  // the method can be inherited.
348
  virtual void
349
  update_data_size()
350
  { }
351
 
352
  // This is a hook for derived classes to set the data size.  This is
353
  // called by finalize_data_size, normally called during
354
  // Layout::finalize, when the section address is set.
355
  virtual void
356
  set_final_data_size()
357
  { gold_unreachable(); }
358
 
359
  // A hook for resetting the address and file offset.
360
  virtual void
361
  do_reset_address_and_file_offset()
362
  { }
363
 
364
  // Return true if address and file offset already have reset values. In
365
  // other words, calling reset_address_and_file_offset will not change them.
366
  // A child class overriding do_reset_address_and_file_offset may need to
367
  // also override this.
368
  virtual bool
369
  do_address_and_file_offset_have_reset_values() const
370
  { return !this->is_address_valid_ && !this->is_offset_valid_; }
371
 
372
  // Set the TLS offset.  Called only for SHT_TLS sections.
373
  virtual void
374
  do_set_tls_offset(uint64_t)
375
  { gold_unreachable(); }
376
 
377
  // Return the TLS offset, relative to the base of the TLS segment.
378
  // Valid only for SHT_TLS sections.
379
  virtual uint64_t
380
  do_tls_offset() const
381
  { gold_unreachable(); }
382
 
383
  // Print to the map file.  This only needs to be implemented by
384
  // classes which may appear in a PT_LOAD segment.
385
  virtual void
386
  do_print_to_mapfile(Mapfile*) const
387
  { gold_unreachable(); }
388
 
389
  // Functions that child classes may call.
390
 
391
  // Reset the address.  The Output_section class needs this when an
392
  // SHF_ALLOC input section is added to an output section which was
393
  // formerly not SHF_ALLOC.
394
  void
395
  mark_address_invalid()
396
  { this->is_address_valid_ = false; }
397
 
398
  // Set the size of the data.
399
  void
400
  set_data_size(off_t data_size)
401
  {
402
    gold_assert(!this->is_data_size_valid_
403
                && !this->is_data_size_fixed_);
404
    this->data_size_ = data_size;
405
    this->is_data_size_valid_ = true;
406
  }
407
 
408
  // Fix the data size.  Once it is fixed, it cannot be changed
409
  // and the data size remains always valid. 
410
  void
411
  fix_data_size()
412
  {
413
    gold_assert(this->is_data_size_valid_);
414
    this->is_data_size_fixed_ = true;
415
  }
416
 
417
  // Get the current data size--this is for the convenience of
418
  // sections which build up their size over time.
419
  off_t
420
  current_data_size_for_child() const
421
  { return this->data_size_; }
422
 
423
  // Set the current data size--this is for the convenience of
424
  // sections which build up their size over time.
425
  void
426
  set_current_data_size_for_child(off_t data_size)
427
  {
428
    gold_assert(!this->is_data_size_valid_);
429
    this->data_size_ = data_size;
430
  }
431
 
432
  // Return default alignment for the target size.
433
  static uint64_t
434
  default_alignment();
435
 
436
  // Return default alignment for a specified size--32 or 64.
437
  static uint64_t
438
  default_alignment_for_size(int size);
439
 
440
 private:
441
  Output_data(const Output_data&);
442
  Output_data& operator=(const Output_data&);
443
 
444
  // This is used for verification, to make sure that we don't try to
445
  // change any sizes of allocated sections after we set the section
446
  // addresses.
447
  static bool allocated_sizes_are_fixed;
448
 
449
  // Memory address in output file.
450
  uint64_t address_;
451
  // Size of data in output file.
452
  off_t data_size_;
453
  // File offset of contents in output file.
454
  off_t offset_;
455
  // Whether address_ is valid.
456
  bool is_address_valid_ : 1;
457
  // Whether data_size_ is valid.
458
  bool is_data_size_valid_ : 1;
459
  // Whether offset_ is valid.
460
  bool is_offset_valid_ : 1;
461
  // Whether data size is fixed.
462
  bool is_data_size_fixed_ : 1;
463
  // Whether any dynamic relocs have been applied to this section.
464
  bool has_dynamic_reloc_ : 1;
465
};
466
 
467
// Output the section headers.
468
 
469
class Output_section_headers : public Output_data
470
{
471
 public:
472
  Output_section_headers(const Layout*,
473
                         const Layout::Segment_list*,
474
                         const Layout::Section_list*,
475
                         const Layout::Section_list*,
476
                         const Stringpool*,
477
                         const Output_section*);
478
 
479
 protected:
480
  // Write the data to the file.
481
  void
482
  do_write(Output_file*);
483
 
484
  // Return the required alignment.
485
  uint64_t
486
  do_addralign() const
487
  { return Output_data::default_alignment(); }
488
 
489
  // Write to a map file.
490
  void
491
  do_print_to_mapfile(Mapfile* mapfile) const
492
  { mapfile->print_output_data(this, _("** section headers")); }
493
 
494
  // Update the data size.
495
  void
496
  update_data_size()
497
  { this->set_data_size(this->do_size()); }
498
 
499
  // Set final data size.
500
  void
501
  set_final_data_size()
502
  { this->set_data_size(this->do_size()); }
503
 
504
 private:
505
  // Write the data to the file with the right size and endianness.
506
  template<int size, bool big_endian>
507
  void
508
  do_sized_write(Output_file*);
509
 
510
  // Compute data size.
511
  off_t
512
  do_size() const;
513
 
514
  const Layout* layout_;
515
  const Layout::Segment_list* segment_list_;
516
  const Layout::Section_list* section_list_;
517
  const Layout::Section_list* unattached_section_list_;
518
  const Stringpool* secnamepool_;
519
  const Output_section* shstrtab_section_;
520
};
521
 
522
// Output the segment headers.
523
 
524
class Output_segment_headers : public Output_data
525
{
526
 public:
527
  Output_segment_headers(const Layout::Segment_list& segment_list);
528
 
529
 protected:
530
  // Write the data to the file.
531
  void
532
  do_write(Output_file*);
533
 
534
  // Return the required alignment.
535
  uint64_t
536
  do_addralign() const
537
  { return Output_data::default_alignment(); }
538
 
539
  // Write to a map file.
540
  void
541
  do_print_to_mapfile(Mapfile* mapfile) const
542
  { mapfile->print_output_data(this, _("** segment headers")); }
543
 
544
  // Set final data size.
545
  void
546
  set_final_data_size()
547
  { this->set_data_size(this->do_size()); }
548
 
549
 private:
550
  // Write the data to the file with the right size and endianness.
551
  template<int size, bool big_endian>
552
  void
553
  do_sized_write(Output_file*);
554
 
555
  // Compute the current size.
556
  off_t
557
  do_size() const;
558
 
559
  const Layout::Segment_list& segment_list_;
560
};
561
 
562
// Output the ELF file header.
563
 
564
class Output_file_header : public Output_data
565
{
566
 public:
567
  Output_file_header(const Target*,
568
                     const Symbol_table*,
569
                     const Output_segment_headers*);
570
 
571
  // Add information about the section headers.  We lay out the ELF
572
  // file header before we create the section headers.
573
  void set_section_info(const Output_section_headers*,
574
                        const Output_section* shstrtab);
575
 
576
 protected:
577
  // Write the data to the file.
578
  void
579
  do_write(Output_file*);
580
 
581
  // Return the required alignment.
582
  uint64_t
583
  do_addralign() const
584
  { return Output_data::default_alignment(); }
585
 
586
  // Write to a map file.
587
  void
588
  do_print_to_mapfile(Mapfile* mapfile) const
589
  { mapfile->print_output_data(this, _("** file header")); }
590
 
591
  // Set final data size.
592
  void
593
  set_final_data_size(void)
594
  { this->set_data_size(this->do_size()); }
595
 
596
 private:
597
  // Write the data to the file with the right size and endianness.
598
  template<int size, bool big_endian>
599
  void
600
  do_sized_write(Output_file*);
601
 
602
  // Return the value to use for the entry address.
603
  template<int size>
604
  typename elfcpp::Elf_types<size>::Elf_Addr
605
  entry();
606
 
607
  // Compute the current data size.
608
  off_t
609
  do_size() const;
610
 
611
  const Target* target_;
612
  const Symbol_table* symtab_;
613
  const Output_segment_headers* segment_header_;
614
  const Output_section_headers* section_header_;
615
  const Output_section* shstrtab_;
616
};
617
 
618
// Output sections are mainly comprised of input sections.  However,
619
// there are cases where we have data to write out which is not in an
620
// input section.  Output_section_data is used in such cases.  This is
621
// an abstract base class.
622
 
623
class Output_section_data : public Output_data
624
{
625
 public:
626
  Output_section_data(off_t data_size, uint64_t addralign,
627
                      bool is_data_size_fixed)
628
    : Output_data(), output_section_(NULL), addralign_(addralign)
629
  {
630
    this->set_data_size(data_size);
631
    if (is_data_size_fixed)
632
      this->fix_data_size();
633
  }
634
 
635
  Output_section_data(uint64_t addralign)
636
    : Output_data(), output_section_(NULL), addralign_(addralign)
637
  { }
638
 
639
  // Return the output section.
640
  Output_section*
641
  output_section()
642
  { return this->output_section_; }
643
 
644
  const Output_section*
645
  output_section() const
646
  { return this->output_section_; }
647
 
648
  // Record the output section.
649
  void
650
  set_output_section(Output_section* os);
651
 
652
  // Add an input section, for SHF_MERGE sections.  This returns true
653
  // if the section was handled.
654
  bool
655
  add_input_section(Relobj* object, unsigned int shndx)
656
  { return this->do_add_input_section(object, shndx); }
657
 
658
  // Given an input OBJECT, an input section index SHNDX within that
659
  // object, and an OFFSET relative to the start of that input
660
  // section, return whether or not the corresponding offset within
661
  // the output section is known.  If this function returns true, it
662
  // sets *POUTPUT to the output offset.  The value -1 indicates that
663
  // this input offset is being discarded.
664
  bool
665
  output_offset(const Relobj* object, unsigned int shndx,
666
                section_offset_type offset,
667
                section_offset_type* poutput) const
668
  { return this->do_output_offset(object, shndx, offset, poutput); }
669
 
670
  // Return whether this is the merge section for the input section
671
  // SHNDX in OBJECT.  This should return true when output_offset
672
  // would return true for some values of OFFSET.
673
  bool
674
  is_merge_section_for(const Relobj* object, unsigned int shndx) const
675
  { return this->do_is_merge_section_for(object, shndx); }
676
 
677
  // Write the contents to a buffer.  This is used for sections which
678
  // require postprocessing, such as compression.
679
  void
680
  write_to_buffer(unsigned char* buffer)
681
  { this->do_write_to_buffer(buffer); }
682
 
683
  // Print merge stats to stderr.  This should only be called for
684
  // SHF_MERGE sections.
685
  void
686
  print_merge_stats(const char* section_name)
687
  { this->do_print_merge_stats(section_name); }
688
 
689
 protected:
690
  // The child class must implement do_write.
691
 
692
  // The child class may implement specific adjustments to the output
693
  // section.
694
  virtual void
695
  do_adjust_output_section(Output_section*)
696
  { }
697
 
698
  // May be implemented by child class.  Return true if the section
699
  // was handled.
700
  virtual bool
701
  do_add_input_section(Relobj*, unsigned int)
702
  { gold_unreachable(); }
703
 
704
  // The child class may implement output_offset.
705
  virtual bool
706
  do_output_offset(const Relobj*, unsigned int, section_offset_type,
707
                   section_offset_type*) const
708
  { return false; }
709
 
710
  // The child class may implement is_merge_section_for.
711
  virtual bool
712
  do_is_merge_section_for(const Relobj*, unsigned int) const
713
  { return false; }
714
 
715
  // The child class may implement write_to_buffer.  Most child
716
  // classes can not appear in a compressed section, and they do not
717
  // implement this.
718
  virtual void
719
  do_write_to_buffer(unsigned char*)
720
  { gold_unreachable(); }
721
 
722
  // Print merge statistics.
723
  virtual void
724
  do_print_merge_stats(const char*)
725
  { gold_unreachable(); }
726
 
727
  // Return the required alignment.
728
  uint64_t
729
  do_addralign() const
730
  { return this->addralign_; }
731
 
732
  // Return the output section.
733
  Output_section*
734
  do_output_section()
735
  { return this->output_section_; }
736
 
737
  const Output_section*
738
  do_output_section() const
739
  { return this->output_section_; }
740
 
741
  // Return the section index of the output section.
742
  unsigned int
743
  do_out_shndx() const;
744
 
745
  // Set the alignment.
746
  void
747
  set_addralign(uint64_t addralign);
748
 
749
 private:
750
  // The output section for this section.
751
  Output_section* output_section_;
752
  // The required alignment.
753
  uint64_t addralign_;
754
};
755
 
756
// Some Output_section_data classes build up their data step by step,
757
// rather than all at once.  This class provides an interface for
758
// them.
759
 
760
class Output_section_data_build : public Output_section_data
761
{
762
 public:
763
  Output_section_data_build(uint64_t addralign)
764
    : Output_section_data(addralign)
765
  { }
766
 
767
  Output_section_data_build(off_t data_size, uint64_t addralign)
768
    : Output_section_data(data_size, addralign, false)
769
  { }
770
 
771
  // Set the current data size.
772
  void
773
  set_current_data_size(off_t data_size)
774
  { this->set_current_data_size_for_child(data_size); }
775
 
776
 protected:
777
  // Set the final data size.
778
  virtual void
779
  set_final_data_size()
780
  { this->set_data_size(this->current_data_size_for_child()); }
781
};
782
 
783
// A simple case of Output_data in which we have constant data to
784
// output.
785
 
786
class Output_data_const : public Output_section_data
787
{
788
 public:
789
  Output_data_const(const std::string& data, uint64_t addralign)
790
    : Output_section_data(data.size(), addralign, true), data_(data)
791
  { }
792
 
793
  Output_data_const(const char* p, off_t len, uint64_t addralign)
794
    : Output_section_data(len, addralign, true), data_(p, len)
795
  { }
796
 
797
  Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
798
    : Output_section_data(len, addralign, true),
799
      data_(reinterpret_cast<const char*>(p), len)
800
  { }
801
 
802
 protected:
803
  // Write the data to the output file.
804
  void
805
  do_write(Output_file*);
806
 
807
  // Write the data to a buffer.
808
  void
809
  do_write_to_buffer(unsigned char* buffer)
810
  { memcpy(buffer, this->data_.data(), this->data_.size()); }
811
 
812
  // Write to a map file.
813
  void
814
  do_print_to_mapfile(Mapfile* mapfile) const
815
  { mapfile->print_output_data(this, _("** fill")); }
816
 
817
 private:
818
  std::string data_;
819
};
820
 
821
// Another version of Output_data with constant data, in which the
822
// buffer is allocated by the caller.
823
 
824
class Output_data_const_buffer : public Output_section_data
825
{
826
 public:
827
  Output_data_const_buffer(const unsigned char* p, off_t len,
828
                           uint64_t addralign, const char* map_name)
829
    : Output_section_data(len, addralign, true),
830
      p_(p), map_name_(map_name)
831
  { }
832
 
833
 protected:
834
  // Write the data the output file.
835
  void
836
  do_write(Output_file*);
837
 
838
  // Write the data to a buffer.
839
  void
840
  do_write_to_buffer(unsigned char* buffer)
841
  { memcpy(buffer, this->p_, this->data_size()); }
842
 
843
  // Write to a map file.
844
  void
845
  do_print_to_mapfile(Mapfile* mapfile) const
846
  { mapfile->print_output_data(this, _(this->map_name_)); }
847
 
848
 private:
849
  // The data to output.
850
  const unsigned char* p_;
851
  // Name to use in a map file.  Maps are a rarely used feature, but
852
  // the space usage is minor as aren't very many of these objects.
853
  const char* map_name_;
854
};
855
 
856
// A place holder for a fixed amount of data written out via some
857
// other mechanism.
858
 
859
class Output_data_fixed_space : public Output_section_data
860
{
861
 public:
862
  Output_data_fixed_space(off_t data_size, uint64_t addralign,
863
                          const char* map_name)
864
    : Output_section_data(data_size, addralign, true),
865
      map_name_(map_name)
866
  { }
867
 
868
 protected:
869
  // Write out the data--the actual data must be written out
870
  // elsewhere.
871
  void
872
  do_write(Output_file*)
873
  { }
874
 
875
  // Write to a map file.
876
  void
877
  do_print_to_mapfile(Mapfile* mapfile) const
878
  { mapfile->print_output_data(this, _(this->map_name_)); }
879
 
880
 private:
881
  // Name to use in a map file.  Maps are a rarely used feature, but
882
  // the space usage is minor as aren't very many of these objects.
883
  const char* map_name_;
884
};
885
 
886
// A place holder for variable sized data written out via some other
887
// mechanism.
888
 
889
class Output_data_space : public Output_section_data_build
890
{
891
 public:
892
  explicit Output_data_space(uint64_t addralign, const char* map_name)
893
    : Output_section_data_build(addralign),
894
      map_name_(map_name)
895
  { }
896
 
897
  explicit Output_data_space(off_t data_size, uint64_t addralign,
898
                             const char* map_name)
899
    : Output_section_data_build(data_size, addralign),
900
      map_name_(map_name)
901
  { }
902
 
903
  // Set the alignment.
904
  void
905
  set_space_alignment(uint64_t align)
906
  { this->set_addralign(align); }
907
 
908
 protected:
909
  // Write out the data--the actual data must be written out
910
  // elsewhere.
911
  void
912
  do_write(Output_file*)
913
  { }
914
 
915
  // Write to a map file.
916
  void
917
  do_print_to_mapfile(Mapfile* mapfile) const
918
  { mapfile->print_output_data(this, _(this->map_name_)); }
919
 
920
 private:
921
  // Name to use in a map file.  Maps are a rarely used feature, but
922
  // the space usage is minor as aren't very many of these objects.
923
  const char* map_name_;
924
};
925
 
926
// Fill fixed space with zeroes.  This is just like
927
// Output_data_fixed_space, except that the map name is known.
928
 
929
class Output_data_zero_fill : public Output_section_data
930
{
931
 public:
932
  Output_data_zero_fill(off_t data_size, uint64_t addralign)
933
    : Output_section_data(data_size, addralign, true)
934
  { }
935
 
936
 protected:
937
  // There is no data to write out.
938
  void
939
  do_write(Output_file*)
940
  { }
941
 
942
  // Write to a map file.
943
  void
944
  do_print_to_mapfile(Mapfile* mapfile) const
945
  { mapfile->print_output_data(this, "** zero fill"); }
946
};
947
 
948
// A string table which goes into an output section.
949
 
950
class Output_data_strtab : public Output_section_data
951
{
952
 public:
953
  Output_data_strtab(Stringpool* strtab)
954
    : Output_section_data(1), strtab_(strtab)
955
  { }
956
 
957
 protected:
958
  // This is called to update the section size prior to assigning
959
  // the address and file offset.
960
  void
961
  update_data_size()
962
  { this->set_final_data_size(); }
963
 
964
  // This is called to set the address and file offset.  Here we make
965
  // sure that the Stringpool is finalized.
966
  void
967
  set_final_data_size();
968
 
969
  // Write out the data.
970
  void
971
  do_write(Output_file*);
972
 
973
  // Write the data to a buffer.
974
  void
975
  do_write_to_buffer(unsigned char* buffer)
976
  { this->strtab_->write_to_buffer(buffer, this->data_size()); }
977
 
978
  // Write to a map file.
979
  void
980
  do_print_to_mapfile(Mapfile* mapfile) const
981
  { mapfile->print_output_data(this, _("** string table")); }
982
 
983
 private:
984
  Stringpool* strtab_;
985
};
986
 
987
// This POD class is used to represent a single reloc in the output
988
// file.  This could be a private class within Output_data_reloc, but
989
// the templatization is complex enough that I broke it out into a
990
// separate class.  The class is templatized on either elfcpp::SHT_REL
991
// or elfcpp::SHT_RELA, and also on whether this is a dynamic
992
// relocation or an ordinary relocation.
993
 
994
// A relocation can be against a global symbol, a local symbol, a
995
// local section symbol, an output section, or the undefined symbol at
996
// index 0.  We represent the latter by using a NULL global symbol.
997
 
998
template<int sh_type, bool dynamic, int size, bool big_endian>
999
class Output_reloc;
1000
 
1001
template<bool dynamic, int size, bool big_endian>
1002
class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1003
{
1004
 public:
1005
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1006
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1007
 
1008
  static const Address invalid_address = static_cast<Address>(0) - 1;
1009
 
1010
  // An uninitialized entry.  We need this because we want to put
1011
  // instances of this class into an STL container.
1012
  Output_reloc()
1013
    : local_sym_index_(INVALID_CODE)
1014
  { }
1015
 
1016
  // We have a bunch of different constructors.  They come in pairs
1017
  // depending on how the address of the relocation is specified.  It
1018
  // can either be an offset in an Output_data or an offset in an
1019
  // input section.
1020
 
1021
  // A reloc against a global symbol.
1022
 
1023
  Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1024
               Address address, bool is_relative, bool is_symbolless);
1025
 
1026
  Output_reloc(Symbol* gsym, unsigned int type,
1027
               Sized_relobj<size, big_endian>* relobj,
1028
               unsigned int shndx, Address address, bool is_relative,
1029
               bool is_symbolless);
1030
 
1031
  // A reloc against a local symbol or local section symbol.
1032
 
1033
  Output_reloc(Sized_relobj<size, big_endian>* relobj,
1034
               unsigned int local_sym_index, unsigned int type,
1035
               Output_data* od, Address address, bool is_relative,
1036 163 khays
               bool is_symbolless, bool is_section_symbol,
1037
               bool use_plt_offset);
1038 27 khays
 
1039
  Output_reloc(Sized_relobj<size, big_endian>* relobj,
1040
               unsigned int local_sym_index, unsigned int type,
1041
               unsigned int shndx, Address address, bool is_relative,
1042 163 khays
               bool is_symbolless, bool is_section_symbol,
1043
               bool use_plt_offset);
1044 27 khays
 
1045
  // A reloc against the STT_SECTION symbol of an output section.
1046
 
1047
  Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1048
               Address address);
1049
 
1050
  Output_reloc(Output_section* os, unsigned int type,
1051
               Sized_relobj<size, big_endian>* relobj,
1052
               unsigned int shndx, Address address);
1053
 
1054
  // An absolute relocation with no symbol.
1055
 
1056
  Output_reloc(unsigned int type, Output_data* od, Address address);
1057
 
1058
  Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1059
               unsigned int shndx, Address address);
1060
 
1061
  // A target specific relocation.  The target will be called to get
1062
  // the symbol index, passing ARG.  The type and offset will be set
1063
  // as for other relocation types.
1064
 
1065
  Output_reloc(unsigned int type, void* arg, Output_data* od,
1066
               Address address);
1067
 
1068
  Output_reloc(unsigned int type, void* arg,
1069
               Sized_relobj<size, big_endian>* relobj,
1070
               unsigned int shndx, Address address);
1071
 
1072
  // Return the reloc type.
1073
  unsigned int
1074
  type() const
1075
  { return this->type_; }
1076
 
1077
  // Return whether this is a RELATIVE relocation.
1078
  bool
1079
  is_relative() const
1080
  { return this->is_relative_; }
1081
 
1082
  // Return whether this is a relocation which should not use
1083
  // a symbol, but which obtains its addend from a symbol.
1084
  bool
1085
  is_symbolless() const
1086
  { return this->is_symbolless_; }
1087
 
1088
  // Return whether this is against a local section symbol.
1089
  bool
1090
  is_local_section_symbol() const
1091
  {
1092
    return (this->local_sym_index_ != GSYM_CODE
1093
            && this->local_sym_index_ != SECTION_CODE
1094
            && this->local_sym_index_ != INVALID_CODE
1095
            && this->local_sym_index_ != TARGET_CODE
1096
            && this->is_section_symbol_);
1097
  }
1098
 
1099
  // Return whether this is a target specific relocation.
1100
  bool
1101
  is_target_specific() const
1102
  { return this->local_sym_index_ == TARGET_CODE; }
1103
 
1104
  // Return the argument to pass to the target for a target specific
1105
  // relocation.
1106
  void*
1107
  target_arg() const
1108
  {
1109
    gold_assert(this->local_sym_index_ == TARGET_CODE);
1110
    return this->u1_.arg;
1111
  }
1112
 
1113
  // For a local section symbol, return the offset of the input
1114
  // section within the output section.  ADDEND is the addend being
1115
  // applied to the input section.
1116
  Address
1117
  local_section_offset(Addend addend) const;
1118
 
1119
  // Get the value of the symbol referred to by a Rel relocation when
1120
  // we are adding the given ADDEND.
1121
  Address
1122
  symbol_value(Addend addend) const;
1123
 
1124
  // If this relocation is against an input section, return the
1125
  // relocatable object containing the input section.
1126
  Sized_relobj<size, big_endian>*
1127
  get_relobj() const
1128
  {
1129
    if (this->shndx_ == INVALID_CODE)
1130
      return NULL;
1131
    return this->u2_.relobj;
1132
  }
1133
 
1134
  // Write the reloc entry to an output view.
1135
  void
1136
  write(unsigned char* pov) const;
1137
 
1138
  // Write the offset and info fields to Write_rel.
1139
  template<typename Write_rel>
1140
  void write_rel(Write_rel*) const;
1141
 
1142
  // This is used when sorting dynamic relocs.  Return -1 to sort this
1143
  // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1144
  int
1145
  compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1146
    const;
1147
 
1148
  // Return whether this reloc should be sorted before the argument
1149
  // when sorting dynamic relocs.
1150
  bool
1151
  sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1152
              r2) const
1153
  { return this->compare(r2) < 0; }
1154
 
1155
 private:
1156
  // Record that we need a dynamic symbol index.
1157
  void
1158
  set_needs_dynsym_index();
1159
 
1160
  // Return the symbol index.
1161
  unsigned int
1162
  get_symbol_index() const;
1163
 
1164
  // Return the output address.
1165
  Address
1166
  get_address() const;
1167
 
1168
  // Codes for local_sym_index_.
1169
  enum
1170
  {
1171
    // Global symbol.
1172
    GSYM_CODE = -1U,
1173
    // Output section.
1174
    SECTION_CODE = -2U,
1175
    // Target specific.
1176
    TARGET_CODE = -3U,
1177
    // Invalid uninitialized entry.
1178
    INVALID_CODE = -4U
1179
  };
1180
 
1181
  union
1182
  {
1183
    // For a local symbol or local section symbol
1184
    // (this->local_sym_index_ >= 0), the object.  We will never
1185
    // generate a relocation against a local symbol in a dynamic
1186
    // object; that doesn't make sense.  And our callers will always
1187
    // be templatized, so we use Sized_relobj here.
1188
    Sized_relobj<size, big_endian>* relobj;
1189
    // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1190
    // symbol.  If this is NULL, it indicates a relocation against the
1191
    // undefined 0 symbol.
1192
    Symbol* gsym;
1193
    // For a relocation against an output section
1194
    // (this->local_sym_index_ == SECTION_CODE), the output section.
1195
    Output_section* os;
1196
    // For a target specific relocation, an argument to pass to the
1197
    // target.
1198
    void* arg;
1199
  } u1_;
1200
  union
1201
  {
1202
    // If this->shndx_ is not INVALID CODE, the object which holds the
1203
    // input section being used to specify the reloc address.
1204
    Sized_relobj<size, big_endian>* relobj;
1205
    // If this->shndx_ is INVALID_CODE, the output data being used to
1206
    // specify the reloc address.  This may be NULL if the reloc
1207
    // address is absolute.
1208
    Output_data* od;
1209
  } u2_;
1210
  // The address offset within the input section or the Output_data.
1211
  Address address_;
1212
  // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1213
  // relocation against an output section, or TARGET_CODE for a target
1214
  // specific relocation, or INVALID_CODE for an uninitialized value.
1215
  // Otherwise, for a local symbol (this->is_section_symbol_ is
1216
  // false), the local symbol index.  For a local section symbol
1217
  // (this->is_section_symbol_ is true), the section index in the
1218
  // input file.
1219
  unsigned int local_sym_index_;
1220
  // The reloc type--a processor specific code.
1221 163 khays
  unsigned int type_ : 28;
1222 27 khays
  // True if the relocation is a RELATIVE relocation.
1223
  bool is_relative_ : 1;
1224
  // True if the relocation is one which should not use
1225
  // a symbol, but which obtains its addend from a symbol.
1226
  bool is_symbolless_ : 1;
1227
  // True if the relocation is against a section symbol.
1228
  bool is_section_symbol_ : 1;
1229 163 khays
  // True if the addend should be the PLT offset.  This is used only
1230
  // for RELATIVE relocations to local symbols.
1231
  // (Used only for RELA, but stored here for space.)
1232
  bool use_plt_offset_ : 1;
1233 27 khays
  // If the reloc address is an input section in an object, the
1234
  // section index.  This is INVALID_CODE if the reloc address is
1235
  // specified in some other way.
1236
  unsigned int shndx_;
1237
};
1238
 
1239
// The SHT_RELA version of Output_reloc<>.  This is just derived from
1240
// the SHT_REL version of Output_reloc, but it adds an addend.
1241
 
1242
template<bool dynamic, int size, bool big_endian>
1243
class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1244
{
1245
 public:
1246
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1247
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1248
 
1249
  // An uninitialized entry.
1250
  Output_reloc()
1251
    : rel_()
1252
  { }
1253
 
1254
  // A reloc against a global symbol.
1255
 
1256
  Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
1257
               Address address, Addend addend, bool is_relative,
1258
               bool is_symbolless)
1259
    : rel_(gsym, type, od, address, is_relative, is_symbolless),
1260
      addend_(addend)
1261
  { }
1262
 
1263
  Output_reloc(Symbol* gsym, unsigned int type,
1264
               Sized_relobj<size, big_endian>* relobj,
1265
               unsigned int shndx, Address address, Addend addend,
1266
               bool is_relative, bool is_symbolless)
1267
    : rel_(gsym, type, relobj, shndx, address, is_relative,
1268
           is_symbolless), addend_(addend)
1269
  { }
1270
 
1271
  // A reloc against a local symbol.
1272
 
1273
  Output_reloc(Sized_relobj<size, big_endian>* relobj,
1274
               unsigned int local_sym_index, unsigned int type,
1275
               Output_data* od, Address address,
1276
               Addend addend, bool is_relative,
1277 163 khays
               bool is_symbolless, bool is_section_symbol,
1278
               bool use_plt_offset)
1279 27 khays
    : rel_(relobj, local_sym_index, type, od, address, is_relative,
1280 163 khays
           is_symbolless, is_section_symbol, use_plt_offset),
1281 27 khays
      addend_(addend)
1282
  { }
1283
 
1284
  Output_reloc(Sized_relobj<size, big_endian>* relobj,
1285
               unsigned int local_sym_index, unsigned int type,
1286
               unsigned int shndx, Address address,
1287
               Addend addend, bool is_relative,
1288 163 khays
               bool is_symbolless, bool is_section_symbol,
1289
               bool use_plt_offset)
1290 27 khays
    : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1291 163 khays
           is_symbolless, is_section_symbol, use_plt_offset),
1292 27 khays
      addend_(addend)
1293
  { }
1294
 
1295
  // A reloc against the STT_SECTION symbol of an output section.
1296
 
1297
  Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1298
               Address address, Addend addend)
1299
    : rel_(os, type, od, address), addend_(addend)
1300
  { }
1301
 
1302
  Output_reloc(Output_section* os, unsigned int type,
1303
               Sized_relobj<size, big_endian>* relobj,
1304
               unsigned int shndx, Address address, Addend addend)
1305
    : rel_(os, type, relobj, shndx, address), addend_(addend)
1306
  { }
1307
 
1308
  // An absolute relocation with no symbol.
1309
 
1310
  Output_reloc(unsigned int type, Output_data* od, Address address,
1311
               Addend addend)
1312
    : rel_(type, od, address), addend_(addend)
1313
  { }
1314
 
1315
  Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1316
               unsigned int shndx, Address address, Addend addend)
1317
    : rel_(type, relobj, shndx, address), addend_(addend)
1318
  { }
1319
 
1320
  // A target specific relocation.  The target will be called to get
1321
  // the symbol index and the addend, passing ARG.  The type and
1322
  // offset will be set as for other relocation types.
1323
 
1324
  Output_reloc(unsigned int type, void* arg, Output_data* od,
1325
               Address address, Addend addend)
1326
    : rel_(type, arg, od, address), addend_(addend)
1327
  { }
1328
 
1329
  Output_reloc(unsigned int type, void* arg,
1330
               Sized_relobj<size, big_endian>* relobj,
1331
               unsigned int shndx, Address address, Addend addend)
1332
    : rel_(type, arg, relobj, shndx, address), addend_(addend)
1333
  { }
1334
 
1335
  // Return whether this is a RELATIVE relocation.
1336
  bool
1337
  is_relative() const
1338
  { return this->rel_.is_relative(); }
1339
 
1340
  // Return whether this is a relocation which should not use
1341
  // a symbol, but which obtains its addend from a symbol.
1342
  bool
1343
  is_symbolless() const
1344
  { return this->rel_.is_symbolless(); }
1345
 
1346
  // If this relocation is against an input section, return the
1347
  // relocatable object containing the input section.
1348
  Sized_relobj<size, big_endian>*
1349
  get_relobj() const
1350
  { return this->rel_.get_relobj(); }
1351
 
1352
  // Write the reloc entry to an output view.
1353
  void
1354
  write(unsigned char* pov) const;
1355
 
1356
  // Return whether this reloc should be sorted before the argument
1357
  // when sorting dynamic relocs.
1358
  bool
1359
  sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1360
              r2) const
1361
  {
1362
    int i = this->rel_.compare(r2.rel_);
1363
    if (i < 0)
1364
      return true;
1365
    else if (i > 0)
1366
      return false;
1367
    else
1368
      return this->addend_ < r2.addend_;
1369
  }
1370
 
1371
 private:
1372
  // The basic reloc.
1373
  Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1374
  // The addend.
1375
  Addend addend_;
1376
};
1377
 
1378
// Output_data_reloc_generic is a non-template base class for
1379
// Output_data_reloc_base.  This gives the generic code a way to hold
1380
// a pointer to a reloc section.
1381
 
1382
class Output_data_reloc_generic : public Output_section_data_build
1383
{
1384
 public:
1385
  Output_data_reloc_generic(int size, bool sort_relocs)
1386
    : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1387
      relative_reloc_count_(0), sort_relocs_(sort_relocs)
1388
  { }
1389
 
1390
  // Return the number of relative relocs in this section.
1391
  size_t
1392
  relative_reloc_count() const
1393
  { return this->relative_reloc_count_; }
1394
 
1395
  // Whether we should sort the relocs.
1396
  bool
1397
  sort_relocs() const
1398
  { return this->sort_relocs_; }
1399
 
1400 166 khays
  // Add a reloc of type TYPE against the global symbol GSYM.  The
1401
  // relocation applies to the data at offset ADDRESS within OD.
1402
  virtual void
1403
  add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1404
                     uint64_t address, uint64_t addend) = 0;
1405
 
1406
  // Add a reloc of type TYPE against the global symbol GSYM.  The
1407
  // relocation applies to data at offset ADDRESS within section SHNDX
1408
  // of object file RELOBJ.  OD is the associated output section.
1409
  virtual void
1410
  add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1411
                     Relobj* relobj, unsigned int shndx, uint64_t address,
1412
                     uint64_t addend) = 0;
1413
 
1414
  // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1415
  // in RELOBJ.  The relocation applies to the data at offset ADDRESS
1416
  // within OD.
1417
  virtual void
1418
  add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1419
                    unsigned int type, Output_data* od, uint64_t address,
1420
                    uint64_t addend) = 0;
1421
 
1422
  // Add a reloc of type TYPE against the local symbol LOCAL_SYM_INDEX
1423
  // in RELOBJ.  The relocation applies to the data at offset ADDRESS
1424
  // within section SHNDX of RELOBJ.  OD is the associated output
1425
  // section.
1426
  virtual void
1427
  add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1428
                    unsigned int type, Output_data* od, unsigned int shndx,
1429
                    uint64_t address, uint64_t addend) = 0;
1430
 
1431
  // Add a reloc of type TYPE against the STT_SECTION symbol of the
1432
  // output section OS.  The relocation applies to the data at offset
1433
  // ADDRESS within OD.
1434
  virtual void
1435
  add_output_section_generic(Output_section *os, unsigned int type,
1436
                             Output_data* od, uint64_t address,
1437
                             uint64_t addend) = 0;
1438
 
1439
  // Add a reloc of type TYPE against the STT_SECTION symbol of the
1440
  // output section OS.  The relocation applies to the data at offset
1441
  // ADDRESS within section SHNDX of RELOBJ.  OD is the associated
1442
  // output section.
1443
  virtual void
1444
  add_output_section_generic(Output_section* os, unsigned int type,
1445
                             Output_data* od, Relobj* relobj,
1446
                             unsigned int shndx, uint64_t address,
1447
                             uint64_t addend) = 0;
1448
 
1449 27 khays
 protected:
1450
  // Note that we've added another relative reloc.
1451
  void
1452
  bump_relative_reloc_count()
1453
  { ++this->relative_reloc_count_; }
1454
 
1455
 private:
1456
  // The number of relative relocs added to this section.  This is to
1457
  // support DT_RELCOUNT.
1458
  size_t relative_reloc_count_;
1459
  // Whether to sort the relocations when writing them out, to make
1460
  // the dynamic linker more efficient.
1461
  bool sort_relocs_;
1462
};
1463
 
1464
// Output_data_reloc is used to manage a section containing relocs.
1465
// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA.  DYNAMIC
1466
// indicates whether this is a dynamic relocation or a normal
1467
// relocation.  Output_data_reloc_base is a base class.
1468
// Output_data_reloc is the real class, which we specialize based on
1469
// the reloc type.
1470
 
1471
template<int sh_type, bool dynamic, int size, bool big_endian>
1472
class Output_data_reloc_base : public Output_data_reloc_generic
1473
{
1474
 public:
1475
  typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1476
  typedef typename Output_reloc_type::Address Address;
1477
  static const int reloc_size =
1478
    Reloc_types<sh_type, size, big_endian>::reloc_size;
1479
 
1480
  // Construct the section.
1481
  Output_data_reloc_base(bool sort_relocs)
1482
    : Output_data_reloc_generic(size, sort_relocs)
1483
  { }
1484
 
1485
 protected:
1486
  // Write out the data.
1487
  void
1488
  do_write(Output_file*);
1489
 
1490
  // Set the entry size and the link.
1491
  void
1492
  do_adjust_output_section(Output_section* os);
1493
 
1494
  // Write to a map file.
1495
  void
1496
  do_print_to_mapfile(Mapfile* mapfile) const
1497
  {
1498
    mapfile->print_output_data(this,
1499
                               (dynamic
1500
                                ? _("** dynamic relocs")
1501
                                : _("** relocs")));
1502
  }
1503
 
1504
  // Add a relocation entry.
1505
  void
1506
  add(Output_data* od, const Output_reloc_type& reloc)
1507
  {
1508
    this->relocs_.push_back(reloc);
1509
    this->set_current_data_size(this->relocs_.size() * reloc_size);
1510 166 khays
    if (dynamic)
1511
      od->add_dynamic_reloc();
1512 27 khays
    if (reloc.is_relative())
1513
      this->bump_relative_reloc_count();
1514
    Sized_relobj<size, big_endian>* relobj = reloc.get_relobj();
1515
    if (relobj != NULL)
1516
      relobj->add_dyn_reloc(this->relocs_.size() - 1);
1517
  }
1518
 
1519
 private:
1520
  typedef std::vector<Output_reloc_type> Relocs;
1521
 
1522
  // The class used to sort the relocations.
1523
  struct Sort_relocs_comparison
1524
  {
1525
    bool
1526
    operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1527
    { return r1.sort_before(r2); }
1528
  };
1529
 
1530
  // The relocations in this section.
1531
  Relocs relocs_;
1532
};
1533
 
1534
// The class which callers actually create.
1535
 
1536
template<int sh_type, bool dynamic, int size, bool big_endian>
1537
class Output_data_reloc;
1538
 
1539
// The SHT_REL version of Output_data_reloc.
1540
 
1541
template<bool dynamic, int size, bool big_endian>
1542
class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1543
  : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1544
{
1545
 private:
1546
  typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1547
                                 big_endian> Base;
1548
 
1549
 public:
1550
  typedef typename Base::Output_reloc_type Output_reloc_type;
1551
  typedef typename Output_reloc_type::Address Address;
1552
 
1553
  Output_data_reloc(bool sr)
1554
    : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1555
  { }
1556
 
1557
  // Add a reloc against a global symbol.
1558
 
1559
  void
1560
  add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1561
  { this->add(od, Output_reloc_type(gsym, type, od, address, false, false)); }
1562
 
1563
  void
1564
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1565
             Sized_relobj<size, big_endian>* relobj,
1566
             unsigned int shndx, Address address)
1567
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1568
                                    false, false)); }
1569
 
1570
  void
1571 166 khays
  add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1572
                     uint64_t address, uint64_t addend)
1573 27 khays
  {
1574
    gold_assert(addend == 0);
1575 166 khays
    this->add(od, Output_reloc_type(gsym, type, od,
1576
                                    convert_types<Address, uint64_t>(address),
1577
                                    false, false));
1578 27 khays
  }
1579
 
1580
  void
1581 166 khays
  add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1582
                     Relobj* relobj, unsigned int shndx, uint64_t address,
1583
                     uint64_t addend)
1584 27 khays
  {
1585
    gold_assert(addend == 0);
1586 166 khays
    Sized_relobj<size, big_endian>* sized_relobj =
1587
      static_cast<Sized_relobj<size, big_endian>*>(relobj);
1588
    this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1589
                                    convert_types<Address, uint64_t>(address),
1590
                                    false, false));
1591 27 khays
  }
1592
 
1593
  // Add a RELATIVE reloc against a global symbol.  The final relocation
1594
  // will not reference the symbol.
1595
 
1596
  void
1597
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1598
                      Address address)
1599
  { this->add(od, Output_reloc_type(gsym, type, od, address, true, true)); }
1600
 
1601
  void
1602
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1603
                      Sized_relobj<size, big_endian>* relobj,
1604
                      unsigned int shndx, Address address)
1605
  {
1606
    this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1607
                                    true, true));
1608
  }
1609
 
1610
  // Add a global relocation which does not use a symbol for the relocation,
1611
  // but which gets its addend from a symbol.
1612
 
1613
  void
1614
  add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1615
                               Output_data* od, Address address)
1616
  { this->add(od, Output_reloc_type(gsym, type, od, address, false, true)); }
1617
 
1618
  void
1619
  add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1620
                               Output_data* od,
1621
                               Sized_relobj<size, big_endian>* relobj,
1622
                               unsigned int shndx, Address address)
1623
  {
1624
    this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1625
                                    false, true));
1626
  }
1627
 
1628
  // Add a reloc against a local symbol.
1629
 
1630
  void
1631
  add_local(Sized_relobj<size, big_endian>* relobj,
1632
            unsigned int local_sym_index, unsigned int type,
1633
            Output_data* od, Address address)
1634
  {
1635
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1636 163 khays
                                    address, false, false, false, false));
1637 27 khays
  }
1638
 
1639
  void
1640
  add_local(Sized_relobj<size, big_endian>* relobj,
1641
            unsigned int local_sym_index, unsigned int type,
1642
            Output_data* od, unsigned int shndx, Address address)
1643
  {
1644
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1645 163 khays
                                    address, false, false, false, false));
1646 27 khays
  }
1647
 
1648 166 khays
  void
1649
  add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1650
                    unsigned int type, Output_data* od, uint64_t address,
1651
                    uint64_t addend)
1652
  {
1653
    gold_assert(addend == 0);
1654
    Sized_relobj<size, big_endian>* sized_relobj =
1655
      static_cast<Sized_relobj<size, big_endian> *>(relobj);
1656
    this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1657
                                    convert_types<Address, uint64_t>(address),
1658
                                    false, false, false, false));
1659
  }
1660
 
1661
  void
1662
  add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1663
                    unsigned int type, Output_data* od, unsigned int shndx,
1664
                    uint64_t address, uint64_t addend)
1665
  {
1666
    gold_assert(addend == 0);
1667
    Sized_relobj<size, big_endian>* sized_relobj =
1668
      static_cast<Sized_relobj<size, big_endian>*>(relobj);
1669
    this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
1670
                                    convert_types<Address, uint64_t>(address),
1671
                                    false, false, false, false));
1672
  }
1673
 
1674 27 khays
  // Add a RELATIVE reloc against a local symbol.
1675
 
1676
  void
1677
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1678
                     unsigned int local_sym_index, unsigned int type,
1679
                     Output_data* od, Address address)
1680
  {
1681
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1682 163 khays
                                    address, true, true, false, false));
1683 27 khays
  }
1684
 
1685
  void
1686
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1687
                     unsigned int local_sym_index, unsigned int type,
1688
                     Output_data* od, unsigned int shndx, Address address)
1689
  {
1690
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1691 163 khays
                                    address, true, true, false, false));
1692 27 khays
  }
1693
 
1694
  // Add a local relocation which does not use a symbol for the relocation,
1695
  // but which gets its addend from a symbol.
1696
 
1697
  void
1698
  add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1699
                              unsigned int local_sym_index, unsigned int type,
1700
                              Output_data* od, Address address)
1701
  {
1702
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1703 163 khays
                                    address, false, true, false, false));
1704 27 khays
  }
1705
 
1706
  void
1707
  add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1708
                              unsigned int local_sym_index, unsigned int type,
1709
                              Output_data* od, unsigned int shndx,
1710
                              Address address)
1711
  {
1712
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1713 163 khays
                                    address, false, true, false, false));
1714 27 khays
  }
1715
 
1716
  // Add a reloc against a local section symbol.  This will be
1717
  // converted into a reloc against the STT_SECTION symbol of the
1718
  // output section.
1719
 
1720
  void
1721
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1722
                    unsigned int input_shndx, unsigned int type,
1723
                    Output_data* od, Address address)
1724
  {
1725
    this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1726 163 khays
                                    address, false, false, true, false));
1727 27 khays
  }
1728
 
1729
  void
1730
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1731
                    unsigned int input_shndx, unsigned int type,
1732
                    Output_data* od, unsigned int shndx, Address address)
1733
  {
1734
    this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1735 163 khays
                                    address, false, false, true, false));
1736 27 khays
  }
1737
 
1738
  // A reloc against the STT_SECTION symbol of an output section.
1739
  // OS is the Output_section that the relocation refers to; OD is
1740
  // the Output_data object being relocated.
1741
 
1742
  void
1743
  add_output_section(Output_section* os, unsigned int type,
1744
                     Output_data* od, Address address)
1745
  { this->add(od, Output_reloc_type(os, type, od, address)); }
1746
 
1747
  void
1748
  add_output_section(Output_section* os, unsigned int type, Output_data* od,
1749
                     Sized_relobj<size, big_endian>* relobj,
1750
                     unsigned int shndx, Address address)
1751
  { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1752
 
1753 166 khays
  void
1754
  add_output_section_generic(Output_section* os, unsigned int type,
1755
                             Output_data* od, uint64_t address,
1756
                             uint64_t addend)
1757
  {
1758
    gold_assert(addend == 0);
1759
    this->add(od, Output_reloc_type(os, type, od,
1760
                                    convert_types<Address, uint64_t>(address)));
1761
  }
1762
 
1763
  void
1764
  add_output_section_generic(Output_section* os, unsigned int type,
1765
                             Output_data* od, Relobj* relobj,
1766
                             unsigned int shndx, uint64_t address,
1767
                             uint64_t addend)
1768
  {
1769
    gold_assert(addend == 0);
1770
    Sized_relobj<size, big_endian>* sized_relobj =
1771
      static_cast<Sized_relobj<size, big_endian>*>(relobj);
1772
    this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
1773
                                    convert_types<Address, uint64_t>(address)));
1774
  }
1775
 
1776 27 khays
  // Add an absolute relocation.
1777
 
1778
  void
1779
  add_absolute(unsigned int type, Output_data* od, Address address)
1780
  { this->add(od, Output_reloc_type(type, od, address)); }
1781
 
1782
  void
1783
  add_absolute(unsigned int type, Output_data* od,
1784
               Sized_relobj<size, big_endian>* relobj,
1785
               unsigned int shndx, Address address)
1786
  { this->add(od, Output_reloc_type(type, relobj, shndx, address)); }
1787
 
1788
  // Add a target specific relocation.  A target which calls this must
1789
  // define the reloc_symbol_index and reloc_addend virtual functions.
1790
 
1791
  void
1792
  add_target_specific(unsigned int type, void* arg, Output_data* od,
1793
                      Address address)
1794
  { this->add(od, Output_reloc_type(type, arg, od, address)); }
1795
 
1796
  void
1797
  add_target_specific(unsigned int type, void* arg, Output_data* od,
1798
                      Sized_relobj<size, big_endian>* relobj,
1799
                      unsigned int shndx, Address address)
1800
  { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1801
};
1802
 
1803
// The SHT_RELA version of Output_data_reloc.
1804
 
1805
template<bool dynamic, int size, bool big_endian>
1806
class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1807
  : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1808
{
1809
 private:
1810
  typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1811
                                 big_endian> Base;
1812
 
1813
 public:
1814
  typedef typename Base::Output_reloc_type Output_reloc_type;
1815
  typedef typename Output_reloc_type::Address Address;
1816
  typedef typename Output_reloc_type::Addend Addend;
1817
 
1818
  Output_data_reloc(bool sr)
1819
    : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1820
  { }
1821
 
1822
  // Add a reloc against a global symbol.
1823
 
1824
  void
1825
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1826
             Address address, Addend addend)
1827
  { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1828
                                    false, false)); }
1829
 
1830
  void
1831
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1832
             Sized_relobj<size, big_endian>* relobj,
1833
             unsigned int shndx, Address address,
1834
             Addend addend)
1835
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1836
                                    addend, false, false)); }
1837
 
1838 166 khays
  void
1839
  add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1840
                     uint64_t address, uint64_t addend)
1841
  {
1842
    this->add(od, Output_reloc_type(gsym, type, od,
1843
                                    convert_types<Address, uint64_t>(address),
1844
                                    convert_types<Addend, uint64_t>(addend),
1845
                                    false, false));
1846
  }
1847
 
1848
  void
1849
  add_global_generic(Symbol* gsym, unsigned int type, Output_data* od,
1850
                     Relobj* relobj, unsigned int shndx, uint64_t address,
1851
                     uint64_t addend)
1852
  {
1853
    Sized_relobj<size, big_endian>* sized_relobj =
1854
      static_cast<Sized_relobj<size, big_endian>*>(relobj);
1855
    this->add(od, Output_reloc_type(gsym, type, sized_relobj, shndx,
1856
                                    convert_types<Address, uint64_t>(address),
1857
                                    convert_types<Addend, uint64_t>(addend),
1858
                                    false, false));
1859
  }
1860
 
1861 27 khays
  // Add a RELATIVE reloc against a global symbol.  The final output
1862
  // relocation will not reference the symbol, but we must keep the symbol
1863
  // information long enough to set the addend of the relocation correctly
1864
  // when it is written.
1865
 
1866
  void
1867
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1868
                      Address address, Addend addend)
1869
  { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1870
                                    true)); }
1871
 
1872
  void
1873
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1874
                      Sized_relobj<size, big_endian>* relobj,
1875
                      unsigned int shndx, Address address, Addend addend)
1876
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1877
                                    addend, true, true)); }
1878
 
1879
  // Add a global relocation which does not use a symbol for the relocation,
1880
  // but which gets its addend from a symbol.
1881
 
1882
  void
1883
  add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1884
                               Address address, Addend addend)
1885
  { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1886
                                    false, true)); }
1887
 
1888
  void
1889
  add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1890
                               Output_data* od,
1891
                               Sized_relobj<size, big_endian>* relobj,
1892
                               unsigned int shndx, Address address, Addend addend)
1893
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1894
                                    addend, false, true)); }
1895
 
1896
  // Add a reloc against a local symbol.
1897
 
1898
  void
1899
  add_local(Sized_relobj<size, big_endian>* relobj,
1900
            unsigned int local_sym_index, unsigned int type,
1901
            Output_data* od, Address address, Addend addend)
1902
  {
1903
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1904 163 khays
                                    addend, false, false, false, false));
1905 27 khays
  }
1906
 
1907
  void
1908
  add_local(Sized_relobj<size, big_endian>* relobj,
1909
            unsigned int local_sym_index, unsigned int type,
1910
            Output_data* od, unsigned int shndx, Address address,
1911
            Addend addend)
1912
  {
1913
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1914 163 khays
                                    address, addend, false, false, false,
1915
                                    false));
1916 27 khays
  }
1917
 
1918 166 khays
  void
1919
  add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1920
                    unsigned int type, Output_data* od, uint64_t address,
1921
                    uint64_t addend)
1922
  {
1923
    Sized_relobj<size, big_endian>* sized_relobj =
1924
      static_cast<Sized_relobj<size, big_endian> *>(relobj);
1925
    this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, od,
1926
                                    convert_types<Address, uint64_t>(address),
1927
                                    convert_types<Addend, uint64_t>(addend),
1928
                                    false, false, false, false));
1929
  }
1930
 
1931
  void
1932
  add_local_generic(Relobj* relobj, unsigned int local_sym_index,
1933
                    unsigned int type, Output_data* od, unsigned int shndx,
1934
                    uint64_t address, uint64_t addend)
1935
  {
1936
    Sized_relobj<size, big_endian>* sized_relobj =
1937
      static_cast<Sized_relobj<size, big_endian>*>(relobj);
1938
    this->add(od, Output_reloc_type(sized_relobj, local_sym_index, type, shndx,
1939
                                    convert_types<Address, uint64_t>(address),
1940
                                    convert_types<Addend, uint64_t>(addend),
1941
                                    false, false, false, false));
1942
  }
1943
 
1944 27 khays
  // Add a RELATIVE reloc against a local symbol.
1945
 
1946
  void
1947
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1948
                     unsigned int local_sym_index, unsigned int type,
1949 163 khays
                     Output_data* od, Address address, Addend addend,
1950
                     bool use_plt_offset)
1951 27 khays
  {
1952
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1953 163 khays
                                    addend, true, true, false,
1954
                                    use_plt_offset));
1955 27 khays
  }
1956
 
1957
  void
1958
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1959
                     unsigned int local_sym_index, unsigned int type,
1960
                     Output_data* od, unsigned int shndx, Address address,
1961 163 khays
                     Addend addend, bool use_plt_offset)
1962 27 khays
  {
1963
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1964 163 khays
                                    address, addend, true, true, false,
1965
                                    use_plt_offset));
1966 27 khays
  }
1967
 
1968
  // Add a local relocation which does not use a symbol for the relocation,
1969
  // but which gets it's addend from a symbol.
1970
 
1971
  void
1972
  add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1973
                              unsigned int local_sym_index, unsigned int type,
1974
                              Output_data* od, Address address, Addend addend)
1975
  {
1976
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1977 163 khays
                                    addend, false, true, false, false));
1978 27 khays
  }
1979
 
1980
  void
1981
  add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1982
                              unsigned int local_sym_index, unsigned int type,
1983
                              Output_data* od, unsigned int shndx,
1984
                              Address address, Addend addend)
1985
  {
1986
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1987 163 khays
                                    address, addend, false, true, false,
1988
                                    false));
1989 27 khays
  }
1990
 
1991
  // Add a reloc against a local section symbol.  This will be
1992
  // converted into a reloc against the STT_SECTION symbol of the
1993
  // output section.
1994
 
1995
  void
1996
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1997
                    unsigned int input_shndx, unsigned int type,
1998
                    Output_data* od, Address address, Addend addend)
1999
  {
2000
    this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
2001 163 khays
                                    addend, false, false, true, false));
2002 27 khays
  }
2003
 
2004
  void
2005
  add_local_section(Sized_relobj<size, big_endian>* relobj,
2006
                    unsigned int input_shndx, unsigned int type,
2007
                    Output_data* od, unsigned int shndx, Address address,
2008
                    Addend addend)
2009
  {
2010
    this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
2011 163 khays
                                    address, addend, false, false, true,
2012
                                    false));
2013 27 khays
  }
2014
 
2015
  // A reloc against the STT_SECTION symbol of an output section.
2016
 
2017
  void
2018
  add_output_section(Output_section* os, unsigned int type, Output_data* od,
2019
                     Address address, Addend addend)
2020
  { this->add(od, Output_reloc_type(os, type, od, address, addend)); }
2021
 
2022
  void
2023
  add_output_section(Output_section* os, unsigned int type, Output_data* od,
2024
                     Sized_relobj<size, big_endian>* relobj,
2025
                     unsigned int shndx, Address address, Addend addend)
2026
  { this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
2027
                                    addend)); }
2028
 
2029 166 khays
  void
2030
  add_output_section_generic(Output_section* os, unsigned int type,
2031
                             Output_data* od, uint64_t address,
2032
                             uint64_t addend)
2033
  {
2034
    this->add(od, Output_reloc_type(os, type, od,
2035
                                    convert_types<Address, uint64_t>(address),
2036
                                    convert_types<Addend, uint64_t>(addend)));
2037
  }
2038
 
2039
  void
2040
  add_output_section_generic(Output_section* os, unsigned int type,
2041
                             Output_data* od, Relobj* relobj,
2042
                             unsigned int shndx, uint64_t address,
2043
                             uint64_t addend)
2044
  {
2045
    Sized_relobj<size, big_endian>* sized_relobj =
2046
      static_cast<Sized_relobj<size, big_endian>*>(relobj);
2047
    this->add(od, Output_reloc_type(os, type, sized_relobj, shndx,
2048
                                    convert_types<Address, uint64_t>(address),
2049
                                    convert_types<Addend, uint64_t>(addend)));
2050
  }
2051
 
2052 27 khays
  // Add an absolute relocation.
2053
 
2054
  void
2055
  add_absolute(unsigned int type, Output_data* od, Address address,
2056
               Addend addend)
2057
  { this->add(od, Output_reloc_type(type, od, address, addend)); }
2058
 
2059
  void
2060
  add_absolute(unsigned int type, Output_data* od,
2061
               Sized_relobj<size, big_endian>* relobj,
2062
               unsigned int shndx, Address address, Addend addend)
2063
  { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); }
2064
 
2065
  // Add a target specific relocation.  A target which calls this must
2066
  // define the reloc_symbol_index and reloc_addend virtual functions.
2067
 
2068
  void
2069
  add_target_specific(unsigned int type, void* arg, Output_data* od,
2070
                      Address address, Addend addend)
2071
  { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
2072
 
2073
  void
2074
  add_target_specific(unsigned int type, void* arg, Output_data* od,
2075
                      Sized_relobj<size, big_endian>* relobj,
2076
                      unsigned int shndx, Address address, Addend addend)
2077
  {
2078
    this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
2079
                                    addend));
2080
  }
2081
};
2082
 
2083
// Output_relocatable_relocs represents a relocation section in a
2084
// relocatable link.  The actual data is written out in the target
2085
// hook relocate_for_relocatable.  This just saves space for it.
2086
 
2087
template<int sh_type, int size, bool big_endian>
2088
class Output_relocatable_relocs : public Output_section_data
2089
{
2090
 public:
2091
  Output_relocatable_relocs(Relocatable_relocs* rr)
2092
    : Output_section_data(Output_data::default_alignment_for_size(size)),
2093
      rr_(rr)
2094
  { }
2095
 
2096
  void
2097
  set_final_data_size();
2098
 
2099
  // Write out the data.  There is nothing to do here.
2100
  void
2101
  do_write(Output_file*)
2102
  { }
2103
 
2104
  // Write to a map file.
2105
  void
2106
  do_print_to_mapfile(Mapfile* mapfile) const
2107
  { mapfile->print_output_data(this, _("** relocs")); }
2108
 
2109
 private:
2110
  // The relocs associated with this input section.
2111
  Relocatable_relocs* rr_;
2112
};
2113
 
2114
// Handle a GROUP section.
2115
 
2116
template<int size, bool big_endian>
2117
class Output_data_group : public Output_section_data
2118
{
2119
 public:
2120
  // The constructor clears *INPUT_SHNDXES.
2121
  Output_data_group(Sized_relobj_file<size, big_endian>* relobj,
2122
                    section_size_type entry_count,
2123
                    elfcpp::Elf_Word flags,
2124
                    std::vector<unsigned int>* input_shndxes);
2125
 
2126
  void
2127
  do_write(Output_file*);
2128
 
2129
  // Write to a map file.
2130
  void
2131
  do_print_to_mapfile(Mapfile* mapfile) const
2132
  { mapfile->print_output_data(this, _("** group")); }
2133
 
2134
  // Set final data size.
2135
  void
2136
  set_final_data_size()
2137
  { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
2138
 
2139
 private:
2140
  // The input object.
2141
  Sized_relobj_file<size, big_endian>* relobj_;
2142
  // The group flag word.
2143
  elfcpp::Elf_Word flags_;
2144
  // The section indexes of the input sections in this group.
2145
  std::vector<unsigned int> input_shndxes_;
2146
};
2147
 
2148
// Output_data_got is used to manage a GOT.  Each entry in the GOT is
2149
// for one symbol--either a global symbol or a local symbol in an
2150
// object.  The target specific code adds entries to the GOT as
2151 166 khays
// needed.  The GOT_SIZE template parameter is the size in bits of a
2152
// GOT entry, typically 32 or 64.
2153 27 khays
 
2154 166 khays
class Output_data_got_base : public Output_section_data_build
2155 27 khays
{
2156
 public:
2157 166 khays
  Output_data_got_base(uint64_t align)
2158
    : Output_section_data_build(align)
2159
  { }
2160 27 khays
 
2161 166 khays
  Output_data_got_base(off_t data_size, uint64_t align)
2162
    : Output_section_data_build(data_size, align)
2163
  { }
2164
 
2165
  // Reserve the slot at index I in the GOT.
2166
  void
2167
  reserve_slot(unsigned int i)
2168
  { this->do_reserve_slot(i); }
2169
 
2170
 protected:
2171
  // Reserve the slot at index I in the GOT.
2172
  virtual void
2173
  do_reserve_slot(unsigned int i) = 0;
2174
};
2175
 
2176
template<int got_size, bool big_endian>
2177
class Output_data_got : public Output_data_got_base
2178
{
2179
 public:
2180
  typedef typename elfcpp::Elf_types<got_size>::Elf_Addr Valtype;
2181
 
2182 27 khays
  Output_data_got()
2183 166 khays
    : Output_data_got_base(Output_data::default_alignment_for_size(got_size)),
2184 27 khays
      entries_(), free_list_()
2185
  { }
2186
 
2187
  Output_data_got(off_t data_size)
2188 166 khays
    : Output_data_got_base(data_size,
2189
                           Output_data::default_alignment_for_size(got_size)),
2190 27 khays
      entries_(), free_list_()
2191
  {
2192
    // For an incremental update, we have an existing GOT section.
2193
    // Initialize the list of entries and the free list.
2194 166 khays
    this->entries_.resize(data_size / (got_size / 8));
2195 27 khays
    this->free_list_.init(data_size, false);
2196
  }
2197
 
2198
  // Add an entry for a global symbol to the GOT.  Return true if this
2199
  // is a new GOT entry, false if the symbol was already in the GOT.
2200
  bool
2201
  add_global(Symbol* gsym, unsigned int got_type);
2202
 
2203
  // Like add_global, but use the PLT offset of the global symbol if
2204
  // it has one.
2205
  bool
2206
  add_global_plt(Symbol* gsym, unsigned int got_type);
2207
 
2208
  // Add an entry for a global symbol to the GOT, and add a dynamic
2209
  // relocation of type R_TYPE for the GOT entry.
2210
  void
2211
  add_global_with_rel(Symbol* gsym, unsigned int got_type,
2212 166 khays
                      Output_data_reloc_generic* rel_dyn, unsigned int r_type);
2213 27 khays
 
2214
  // Add a pair of entries for a global symbol to the GOT, and add
2215
  // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2216
  void
2217
  add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2218 166 khays
                           Output_data_reloc_generic* rel_dyn,
2219
                           unsigned int r_type_1, unsigned int r_type_2);
2220 27 khays
 
2221
  // Add an entry for a local symbol to the GOT.  This returns true if
2222
  // this is a new GOT entry, false if the symbol already has a GOT
2223
  // entry.
2224
  bool
2225 166 khays
  add_local(Relobj* object, unsigned int sym_index, unsigned int got_type);
2226 27 khays
 
2227
  // Like add_local, but use the PLT offset of the local symbol if it
2228
  // has one.
2229
  bool
2230 166 khays
  add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type);
2231 27 khays
 
2232
  // Add an entry for a local symbol to the GOT, and add a dynamic
2233
  // relocation of type R_TYPE for the GOT entry.
2234
  void
2235 166 khays
  add_local_with_rel(Relobj* object, unsigned int sym_index,
2236
                     unsigned int got_type, Output_data_reloc_generic* rel_dyn,
2237
                     unsigned int r_type);
2238 27 khays
 
2239
  // Add a pair of entries for a local symbol to the GOT, and add
2240
  // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2241
  void
2242 166 khays
  add_local_pair_with_rel(Relobj* object, unsigned int sym_index,
2243
                          unsigned int shndx, unsigned int got_type,
2244
                          Output_data_reloc_generic* rel_dyn,
2245 27 khays
                          unsigned int r_type_1, unsigned int r_type_2);
2246
 
2247
  // Add a constant to the GOT.  This returns the offset of the new
2248
  // entry from the start of the GOT.
2249
  unsigned int
2250
  add_constant(Valtype constant)
2251
  {
2252
    unsigned int got_offset = this->add_got_entry(Got_entry(constant));
2253
    return got_offset;
2254
  }
2255
 
2256
  // Reserve a slot in the GOT for a local symbol.
2257
  void
2258 166 khays
  reserve_local(unsigned int i, Relobj* object, unsigned int sym_index,
2259
                unsigned int got_type);
2260 27 khays
 
2261
  // Reserve a slot in the GOT for a global symbol.
2262
  void
2263
  reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type);
2264
 
2265
 protected:
2266
  // Write out the GOT table.
2267
  void
2268
  do_write(Output_file*);
2269
 
2270
  // Write to a map file.
2271
  void
2272
  do_print_to_mapfile(Mapfile* mapfile) const
2273
  { mapfile->print_output_data(this, _("** GOT")); }
2274
 
2275 166 khays
  // Reserve the slot at index I in the GOT.
2276
  virtual void
2277
  do_reserve_slot(unsigned int i)
2278
  { this->free_list_.remove(i * got_size / 8, (i + 1) * got_size / 8); }
2279
 
2280 27 khays
 private:
2281
  // This POD class holds a single GOT entry.
2282
  class Got_entry
2283
  {
2284
   public:
2285
    // Create a zero entry.
2286
    Got_entry()
2287
      : local_sym_index_(RESERVED_CODE), use_plt_offset_(false)
2288
    { this->u_.constant = 0; }
2289
 
2290
    // Create a global symbol entry.
2291
    Got_entry(Symbol* gsym, bool use_plt_offset)
2292
      : local_sym_index_(GSYM_CODE), use_plt_offset_(use_plt_offset)
2293
    { this->u_.gsym = gsym; }
2294
 
2295
    // Create a local symbol entry.
2296 166 khays
    Got_entry(Relobj* object, unsigned int local_sym_index,
2297
              bool use_plt_offset)
2298 27 khays
      : local_sym_index_(local_sym_index), use_plt_offset_(use_plt_offset)
2299
    {
2300
      gold_assert(local_sym_index != GSYM_CODE
2301
                  && local_sym_index != CONSTANT_CODE
2302
                  && local_sym_index != RESERVED_CODE
2303
                  && local_sym_index == this->local_sym_index_);
2304
      this->u_.object = object;
2305
    }
2306
 
2307
    // Create a constant entry.  The constant is a host value--it will
2308
    // be swapped, if necessary, when it is written out.
2309
    explicit Got_entry(Valtype constant)
2310
      : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
2311
    { this->u_.constant = constant; }
2312
 
2313
    // Write the GOT entry to an output view.
2314
    void
2315
    write(unsigned char* pov) const;
2316
 
2317
   private:
2318
    enum
2319
    {
2320
      GSYM_CODE = 0x7fffffff,
2321
      CONSTANT_CODE = 0x7ffffffe,
2322
      RESERVED_CODE = 0x7ffffffd
2323
    };
2324
 
2325
    union
2326
    {
2327
      // For a local symbol, the object.
2328 166 khays
      Relobj* object;
2329 27 khays
      // For a global symbol, the symbol.
2330
      Symbol* gsym;
2331
      // For a constant, the constant.
2332
      Valtype constant;
2333
    } u_;
2334
    // For a local symbol, the local symbol index.  This is GSYM_CODE
2335
    // for a global symbol, or CONSTANT_CODE for a constant.
2336
    unsigned int local_sym_index_ : 31;
2337
    // Whether to use the PLT offset of the symbol if it has one.
2338
    bool use_plt_offset_ : 1;
2339
  };
2340
 
2341
  typedef std::vector<Got_entry> Got_entries;
2342
 
2343
  // Create a new GOT entry and return its offset.
2344
  unsigned int
2345
  add_got_entry(Got_entry got_entry);
2346
 
2347
  // Create a pair of new GOT entries and return the offset of the first.
2348
  unsigned int
2349
  add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2350
 
2351
  // Return the offset into the GOT of GOT entry I.
2352
  unsigned int
2353
  got_offset(unsigned int i) const
2354 166 khays
  { return i * (got_size / 8); }
2355 27 khays
 
2356
  // Return the offset into the GOT of the last entry added.
2357
  unsigned int
2358
  last_got_offset() const
2359
  { return this->got_offset(this->entries_.size() - 1); }
2360
 
2361
  // Set the size of the section.
2362
  void
2363
  set_got_size()
2364
  { this->set_current_data_size(this->got_offset(this->entries_.size())); }
2365
 
2366
  // The list of GOT entries.
2367
  Got_entries entries_;
2368
 
2369
  // List of available regions within the section, for incremental
2370
  // update links.
2371
  Free_list free_list_;
2372
};
2373
 
2374
// Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2375
// section.
2376
 
2377
class Output_data_dynamic : public Output_section_data
2378
{
2379
 public:
2380
  Output_data_dynamic(Stringpool* pool)
2381
    : Output_section_data(Output_data::default_alignment()),
2382
      entries_(), pool_(pool)
2383
  { }
2384
 
2385
  // Add a new dynamic entry with a fixed numeric value.
2386
  void
2387
  add_constant(elfcpp::DT tag, unsigned int val)
2388
  { this->add_entry(Dynamic_entry(tag, val)); }
2389
 
2390
  // Add a new dynamic entry with the address of output data.
2391
  void
2392
  add_section_address(elfcpp::DT tag, const Output_data* od)
2393
  { this->add_entry(Dynamic_entry(tag, od, false)); }
2394
 
2395
  // Add a new dynamic entry with the address of output data
2396
  // plus a constant offset.
2397
  void
2398
  add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2399
                          unsigned int offset)
2400
  { this->add_entry(Dynamic_entry(tag, od, offset)); }
2401
 
2402
  // Add a new dynamic entry with the size of output data.
2403
  void
2404
  add_section_size(elfcpp::DT tag, const Output_data* od)
2405
  { this->add_entry(Dynamic_entry(tag, od, true)); }
2406
 
2407
  // Add a new dynamic entry with the total size of two output datas.
2408
  void
2409
  add_section_size(elfcpp::DT tag, const Output_data* od,
2410
                   const Output_data* od2)
2411
  { this->add_entry(Dynamic_entry(tag, od, od2)); }
2412
 
2413
  // Add a new dynamic entry with the address of a symbol.
2414
  void
2415
  add_symbol(elfcpp::DT tag, const Symbol* sym)
2416
  { this->add_entry(Dynamic_entry(tag, sym)); }
2417
 
2418
  // Add a new dynamic entry with a string.
2419
  void
2420
  add_string(elfcpp::DT tag, const char* str)
2421
  { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2422
 
2423
  void
2424
  add_string(elfcpp::DT tag, const std::string& str)
2425
  { this->add_string(tag, str.c_str()); }
2426
 
2427
 protected:
2428
  // Adjust the output section to set the entry size.
2429
  void
2430
  do_adjust_output_section(Output_section*);
2431
 
2432
  // Set the final data size.
2433
  void
2434
  set_final_data_size();
2435
 
2436
  // Write out the dynamic entries.
2437
  void
2438
  do_write(Output_file*);
2439
 
2440
  // Write to a map file.
2441
  void
2442
  do_print_to_mapfile(Mapfile* mapfile) const
2443
  { mapfile->print_output_data(this, _("** dynamic")); }
2444
 
2445
 private:
2446
  // This POD class holds a single dynamic entry.
2447
  class Dynamic_entry
2448
  {
2449
   public:
2450
    // Create an entry with a fixed numeric value.
2451
    Dynamic_entry(elfcpp::DT tag, unsigned int val)
2452
      : tag_(tag), offset_(DYNAMIC_NUMBER)
2453
    { this->u_.val = val; }
2454
 
2455
    // Create an entry with the size or address of a section.
2456
    Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2457
      : tag_(tag),
2458
        offset_(section_size
2459
                ? DYNAMIC_SECTION_SIZE
2460
                : DYNAMIC_SECTION_ADDRESS)
2461
    {
2462
      this->u_.od = od;
2463
      this->od2 = NULL;
2464
    }
2465
 
2466
    // Create an entry with the size of two sections.
2467
    Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2468
      : tag_(tag),
2469
        offset_(DYNAMIC_SECTION_SIZE)
2470
    {
2471
      this->u_.od = od;
2472
      this->od2 = od2;
2473
    }
2474
 
2475
    // Create an entry with the address of a section plus a constant offset.
2476
    Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2477
      : tag_(tag),
2478
        offset_(offset)
2479
    { this->u_.od = od; }
2480
 
2481
    // Create an entry with the address of a symbol.
2482
    Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2483
      : tag_(tag), offset_(DYNAMIC_SYMBOL)
2484
    { this->u_.sym = sym; }
2485
 
2486
    // Create an entry with a string.
2487
    Dynamic_entry(elfcpp::DT tag, const char* str)
2488
      : tag_(tag), offset_(DYNAMIC_STRING)
2489
    { this->u_.str = str; }
2490
 
2491
    // Return the tag of this entry.
2492
    elfcpp::DT
2493
    tag() const
2494
    { return this->tag_; }
2495
 
2496
    // Write the dynamic entry to an output view.
2497
    template<int size, bool big_endian>
2498
    void
2499
    write(unsigned char* pov, const Stringpool*) const;
2500
 
2501
   private:
2502
    // Classification is encoded in the OFFSET field.
2503
    enum Classification
2504
    {
2505
      // Section address.
2506
      DYNAMIC_SECTION_ADDRESS = 0,
2507
      // Number.
2508
      DYNAMIC_NUMBER = -1U,
2509
      // Section size.
2510
      DYNAMIC_SECTION_SIZE = -2U,
2511
      // Symbol adress.
2512
      DYNAMIC_SYMBOL = -3U,
2513
      // String.
2514
      DYNAMIC_STRING = -4U
2515
      // Any other value indicates a section address plus OFFSET.
2516
    };
2517
 
2518
    union
2519
    {
2520
      // For DYNAMIC_NUMBER.
2521
      unsigned int val;
2522
      // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2523
      const Output_data* od;
2524
      // For DYNAMIC_SYMBOL.
2525
      const Symbol* sym;
2526
      // For DYNAMIC_STRING.
2527
      const char* str;
2528
    } u_;
2529
    // For DYNAMIC_SYMBOL with two sections.
2530
    const Output_data* od2;
2531
    // The dynamic tag.
2532
    elfcpp::DT tag_;
2533
    // The type of entry (Classification) or offset within a section.
2534
    unsigned int offset_;
2535
  };
2536
 
2537
  // Add an entry to the list.
2538
  void
2539
  add_entry(const Dynamic_entry& entry)
2540
  { this->entries_.push_back(entry); }
2541
 
2542
  // Sized version of write function.
2543
  template<int size, bool big_endian>
2544
  void
2545
  sized_write(Output_file* of);
2546
 
2547
  // The type of the list of entries.
2548
  typedef std::vector<Dynamic_entry> Dynamic_entries;
2549
 
2550
  // The entries.
2551
  Dynamic_entries entries_;
2552
  // The pool used for strings.
2553
  Stringpool* pool_;
2554
};
2555
 
2556
// Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2557
// which may be required if the object file has more than
2558
// SHN_LORESERVE sections.
2559
 
2560
class Output_symtab_xindex : public Output_section_data
2561
{
2562
 public:
2563
  Output_symtab_xindex(size_t symcount)
2564
    : Output_section_data(symcount * 4, 4, true),
2565
      entries_()
2566
  { }
2567
 
2568
  // Add an entry: symbol number SYMNDX has section SHNDX.
2569
  void
2570
  add(unsigned int symndx, unsigned int shndx)
2571
  { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2572
 
2573
 protected:
2574
  void
2575
  do_write(Output_file*);
2576
 
2577
  // Write to a map file.
2578
  void
2579
  do_print_to_mapfile(Mapfile* mapfile) const
2580
  { mapfile->print_output_data(this, _("** symtab xindex")); }
2581
 
2582
 private:
2583
  template<bool big_endian>
2584
  void
2585
  endian_do_write(unsigned char*);
2586
 
2587
  // It is likely that most symbols will not require entries.  Rather
2588
  // than keep a vector for all symbols, we keep pairs of symbol index
2589
  // and section index.
2590
  typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2591
 
2592
  // The entries we need.
2593
  Xindex_entries entries_;
2594
};
2595
 
2596
// A relaxed input section.
2597
class Output_relaxed_input_section : public Output_section_data_build
2598
{
2599
 public:
2600
  // We would like to call relobj->section_addralign(shndx) to get the
2601
  // alignment but we do not want the constructor to fail.  So callers
2602
  // are repsonsible for ensuring that.
2603
  Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2604
                               uint64_t addralign)
2605
    : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2606
  { }
2607
 
2608
  // Return the Relobj of this relaxed input section.
2609
  Relobj*
2610
  relobj() const
2611
  { return this->relobj_; }
2612
 
2613
  // Return the section index of this relaxed input section.
2614
  unsigned int
2615
  shndx() const
2616
  { return this->shndx_; }
2617
 
2618
 private:
2619
  Relobj* relobj_;
2620
  unsigned int shndx_;
2621
};
2622
 
2623
// This class describes properties of merge data sections.  It is used
2624
// as a key type for maps.
2625
class Merge_section_properties
2626
{
2627
 public:
2628
  Merge_section_properties(bool is_string, uint64_t entsize,
2629
                             uint64_t addralign)
2630
    : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2631
  { }
2632
 
2633
  // Whether this equals to another Merge_section_properties MSP.
2634
  bool
2635
  eq(const Merge_section_properties& msp) const
2636
  {
2637
    return ((this->is_string_ == msp.is_string_)
2638
            && (this->entsize_ == msp.entsize_)
2639
            && (this->addralign_ == msp.addralign_));
2640
  }
2641
 
2642
  // Compute a hash value for this using 64-bit FNV-1a hash.
2643
  size_t
2644
  hash_value() const
2645
  {
2646
    uint64_t h = 14695981039346656037ULL;       // FNV offset basis.
2647
    uint64_t prime = 1099511628211ULL;
2648
    h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2649
    h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2650
    h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2651
    return h;
2652
  }
2653
 
2654
  // Functors for associative containers.
2655
  struct equal_to
2656
  {
2657
    bool
2658
    operator()(const Merge_section_properties& msp1,
2659
               const Merge_section_properties& msp2) const
2660
    { return msp1.eq(msp2); }
2661
  };
2662
 
2663
  struct hash
2664
  {
2665
    size_t
2666
    operator()(const Merge_section_properties& msp) const
2667
    { return msp.hash_value(); }
2668
  };
2669
 
2670
 private:
2671
  // Whether this merge data section is for strings.
2672
  bool is_string_;
2673
  // Entsize of this merge data section.
2674
  uint64_t entsize_;
2675
  // Address alignment.
2676
  uint64_t addralign_;
2677
};
2678
 
2679
// This class is used to speed up look up of special input sections in an
2680
// Output_section.
2681
 
2682
class Output_section_lookup_maps
2683
{
2684
 public:
2685
  Output_section_lookup_maps()
2686
    : is_valid_(true), merge_sections_by_properties_(),
2687
      merge_sections_by_id_(), relaxed_input_sections_by_id_()
2688
  { }
2689
 
2690
  // Whether the maps are valid.
2691
  bool
2692
  is_valid() const
2693
  { return this->is_valid_; }
2694
 
2695
  // Invalidate the maps.
2696
  void
2697
  invalidate()
2698
  { this->is_valid_ = false; }
2699
 
2700
  // Clear the maps.
2701
  void
2702
  clear()
2703
  {
2704
    this->merge_sections_by_properties_.clear();
2705
    this->merge_sections_by_id_.clear();
2706
    this->relaxed_input_sections_by_id_.clear();
2707
    // A cleared map is valid.
2708
    this->is_valid_ = true;
2709
  }
2710
 
2711
  // Find a merge section by merge section properties.  Return NULL if none
2712
  // is found.
2713
  Output_merge_base*
2714
  find_merge_section(const Merge_section_properties& msp) const
2715
  {
2716
    gold_assert(this->is_valid_);
2717
    Merge_sections_by_properties::const_iterator p =
2718
      this->merge_sections_by_properties_.find(msp);
2719
    return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2720
  }
2721
 
2722
  // Find a merge section by section ID of a merge input section.  Return NULL
2723
  // if none is found.
2724
  Output_merge_base*
2725
  find_merge_section(const Object* object, unsigned int shndx) const
2726
  {
2727
    gold_assert(this->is_valid_);
2728
    Merge_sections_by_id::const_iterator p =
2729
      this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2730
    return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2731
  }
2732
 
2733
  // Add a merge section pointed by POMB with properties MSP.
2734
  void
2735
  add_merge_section(const Merge_section_properties& msp,
2736
                    Output_merge_base* pomb)
2737
  {
2738
    std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2739
    std::pair<Merge_sections_by_properties::iterator, bool> result =
2740
      this->merge_sections_by_properties_.insert(value);
2741
    gold_assert(result.second);
2742
  }
2743
 
2744
  // Add a mapping from a merged input section in OBJECT with index SHNDX
2745
  // to a merge output section pointed by POMB.
2746
  void
2747
  add_merge_input_section(const Object* object, unsigned int shndx,
2748
                          Output_merge_base* pomb)
2749
  {
2750
    Const_section_id csid(object, shndx);
2751
    std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2752
    std::pair<Merge_sections_by_id::iterator, bool> result =
2753
      this->merge_sections_by_id_.insert(value);
2754
    gold_assert(result.second);
2755
  }
2756
 
2757
  // Find a relaxed input section of OBJECT with index SHNDX.
2758
  Output_relaxed_input_section*
2759
  find_relaxed_input_section(const Object* object, unsigned int shndx) const
2760
  {
2761
    gold_assert(this->is_valid_);
2762
    Relaxed_input_sections_by_id::const_iterator p =
2763
      this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2764
    return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2765
  }
2766
 
2767
  // Add a relaxed input section pointed by POMB and whose original input
2768
  // section is in OBJECT with index SHNDX.
2769
  void
2770
  add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2771
                            Output_relaxed_input_section* poris)
2772
  {
2773
    Const_section_id csid(relobj, shndx);
2774
    std::pair<Const_section_id, Output_relaxed_input_section*>
2775
      value(csid, poris);
2776
    std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2777
      this->relaxed_input_sections_by_id_.insert(value);
2778
    gold_assert(result.second);
2779
  }
2780
 
2781
 private:
2782
  typedef Unordered_map<Const_section_id, Output_merge_base*,
2783
                        Const_section_id_hash>
2784
    Merge_sections_by_id;
2785
 
2786
  typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2787
                        Merge_section_properties::hash,
2788
                        Merge_section_properties::equal_to>
2789
    Merge_sections_by_properties;
2790
 
2791
  typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2792
                        Const_section_id_hash>
2793
    Relaxed_input_sections_by_id;
2794
 
2795
  // Whether this is valid
2796
  bool is_valid_;
2797
  // Merge sections by merge section properties.
2798
  Merge_sections_by_properties merge_sections_by_properties_;
2799
  // Merge sections by section IDs.
2800
  Merge_sections_by_id merge_sections_by_id_;
2801
  // Relaxed sections by section IDs.
2802
  Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2803
};
2804
 
2805 159 khays
// This abstract base class defines the interface for the
2806
// types of methods used to fill free space left in an output
2807
// section during an incremental link.  These methods are used
2808
// to insert dummy compilation units into debug info so that
2809
// debug info consumers can scan the debug info serially.
2810
 
2811
class Output_fill
2812
{
2813
 public:
2814
  Output_fill()
2815
    : is_big_endian_(parameters->target().is_big_endian())
2816
  { }
2817
 
2818
  // Return the smallest size chunk of free space that can be
2819
  // filled with a dummy compilation unit.
2820
  size_t
2821
  minimum_hole_size() const
2822
  { return this->do_minimum_hole_size(); }
2823
 
2824
  // Write a fill pattern of length LEN at offset OFF in the file.
2825
  void
2826
  write(Output_file* of, off_t off, size_t len) const
2827
  { this->do_write(of, off, len); }
2828
 
2829
 protected:
2830
  virtual size_t
2831
  do_minimum_hole_size() const = 0;
2832
 
2833
  virtual void
2834
  do_write(Output_file* of, off_t off, size_t len) const = 0;
2835
 
2836
  bool
2837
  is_big_endian() const
2838
  { return this->is_big_endian_; }
2839
 
2840
 private:
2841
  bool is_big_endian_;
2842
};
2843
 
2844
// Fill method that introduces a dummy compilation unit in
2845
// a .debug_info or .debug_types section.
2846
 
2847
class Output_fill_debug_info : public Output_fill
2848
{
2849
 public:
2850
  Output_fill_debug_info(bool is_debug_types)
2851
    : is_debug_types_(is_debug_types)
2852
  { }
2853
 
2854
 protected:
2855
  virtual size_t
2856
  do_minimum_hole_size() const;
2857
 
2858
  virtual void
2859
  do_write(Output_file* of, off_t off, size_t len) const;
2860
 
2861
 private:
2862
  // Version of the header.
2863
  static const int version = 4;
2864
  // True if this is a .debug_types section.
2865
  bool is_debug_types_;
2866
};
2867
 
2868
// Fill method that introduces a dummy compilation unit in
2869
// a .debug_line section.
2870
 
2871
class Output_fill_debug_line : public Output_fill
2872
{
2873
 public:
2874
  Output_fill_debug_line()
2875
  { }
2876
 
2877
 protected:
2878
  virtual size_t
2879
  do_minimum_hole_size() const;
2880
 
2881
  virtual void
2882
  do_write(Output_file* of, off_t off, size_t len) const;
2883
 
2884
 private:
2885
  // Version of the header.  We write a DWARF-3 header because it's smaller
2886
  // and many tools have not yet been updated to understand the DWARF-4 header.
2887
  static const int version = 3;
2888
  // Length of the portion of the header that follows the header_length
2889
  // field.  This includes the following fields:
2890
  // minimum_instruction_length, default_is_stmt, line_base, line_range,
2891
  // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2892
  // The standard_opcode_lengths array is 12 bytes long, and the
2893
  // include_directories and filenames fields each contain only a single
2894
  // null byte.
2895
  static const size_t header_length = 19;
2896
};
2897
 
2898 27 khays
// An output section.  We don't expect to have too many output
2899
// sections, so we don't bother to do a template on the size.
2900
 
2901
class Output_section : public Output_data
2902
{
2903
 public:
2904
  // Create an output section, giving the name, type, and flags.
2905
  Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2906
  virtual ~Output_section();
2907
 
2908
  // Add a new input section SHNDX, named NAME, with header SHDR, from
2909
  // object OBJECT.  RELOC_SHNDX is the index of a relocation section
2910
  // which applies to this section, or 0 if none, or -1 if more than
2911
  // one.  HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2912
  // in a linker script; in that case we need to keep track of input
2913
  // sections associated with an output section.  Return the offset
2914
  // within the output section.
2915
  template<int size, bool big_endian>
2916
  off_t
2917
  add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
2918
                    unsigned int shndx, const char* name,
2919
                    const elfcpp::Shdr<size, big_endian>& shdr,
2920
                    unsigned int reloc_shndx, bool have_sections_script);
2921
 
2922
  // Add generated data POSD to this output section.
2923
  void
2924
  add_output_section_data(Output_section_data* posd);
2925
 
2926
  // Add a relaxed input section PORIS called NAME to this output section
2927
  // with LAYOUT.
2928
  void
2929
  add_relaxed_input_section(Layout* layout,
2930
                            Output_relaxed_input_section* poris,
2931
                            const std::string& name);
2932
 
2933
  // Return the section name.
2934
  const char*
2935
  name() const
2936
  { return this->name_; }
2937
 
2938
  // Return the section type.
2939
  elfcpp::Elf_Word
2940
  type() const
2941
  { return this->type_; }
2942
 
2943
  // Return the section flags.
2944
  elfcpp::Elf_Xword
2945
  flags() const
2946
  { return this->flags_; }
2947
 
2948 159 khays
  typedef std::map<Section_id, unsigned int> Section_layout_order;
2949
 
2950
  void
2951 163 khays
  update_section_layout(const Section_layout_order* order_map);
2952 159 khays
 
2953 27 khays
  // Update the output section flags based on input section flags.
2954
  void
2955
  update_flags_for_input_section(elfcpp::Elf_Xword flags);
2956
 
2957
  // Return the entsize field.
2958
  uint64_t
2959
  entsize() const
2960
  { return this->entsize_; }
2961
 
2962
  // Set the entsize field.
2963
  void
2964
  set_entsize(uint64_t v);
2965
 
2966
  // Set the load address.
2967
  void
2968
  set_load_address(uint64_t load_address)
2969
  {
2970
    this->load_address_ = load_address;
2971
    this->has_load_address_ = true;
2972
  }
2973
 
2974
  // Set the link field to the output section index of a section.
2975
  void
2976
  set_link_section(const Output_data* od)
2977
  {
2978
    gold_assert(this->link_ == 0
2979
                && !this->should_link_to_symtab_
2980
                && !this->should_link_to_dynsym_);
2981
    this->link_section_ = od;
2982
  }
2983
 
2984
  // Set the link field to a constant.
2985
  void
2986
  set_link(unsigned int v)
2987
  {
2988
    gold_assert(this->link_section_ == NULL
2989
                && !this->should_link_to_symtab_
2990
                && !this->should_link_to_dynsym_);
2991
    this->link_ = v;
2992
  }
2993
 
2994
  // Record that this section should link to the normal symbol table.
2995
  void
2996
  set_should_link_to_symtab()
2997
  {
2998
    gold_assert(this->link_section_ == NULL
2999
                && this->link_ == 0
3000
                && !this->should_link_to_dynsym_);
3001
    this->should_link_to_symtab_ = true;
3002
  }
3003
 
3004
  // Record that this section should link to the dynamic symbol table.
3005
  void
3006
  set_should_link_to_dynsym()
3007
  {
3008
    gold_assert(this->link_section_ == NULL
3009
                && this->link_ == 0
3010
                && !this->should_link_to_symtab_);
3011
    this->should_link_to_dynsym_ = true;
3012
  }
3013
 
3014
  // Return the info field.
3015
  unsigned int
3016
  info() const
3017
  {
3018
    gold_assert(this->info_section_ == NULL
3019
                && this->info_symndx_ == NULL);
3020
    return this->info_;
3021
  }
3022
 
3023
  // Set the info field to the output section index of a section.
3024
  void
3025
  set_info_section(const Output_section* os)
3026
  {
3027
    gold_assert((this->info_section_ == NULL
3028
                 || (this->info_section_ == os
3029
                     && this->info_uses_section_index_))
3030
                && this->info_symndx_ == NULL
3031
                && this->info_ == 0);
3032
    this->info_section_ = os;
3033
    this->info_uses_section_index_= true;
3034
  }
3035
 
3036
  // Set the info field to the symbol table index of a symbol.
3037
  void
3038
  set_info_symndx(const Symbol* sym)
3039
  {
3040
    gold_assert(this->info_section_ == NULL
3041
                && (this->info_symndx_ == NULL
3042
                    || this->info_symndx_ == sym)
3043
                && this->info_ == 0);
3044
    this->info_symndx_ = sym;
3045
  }
3046
 
3047
  // Set the info field to the symbol table index of a section symbol.
3048
  void
3049
  set_info_section_symndx(const Output_section* os)
3050
  {
3051
    gold_assert((this->info_section_ == NULL
3052
                 || (this->info_section_ == os
3053
                     && !this->info_uses_section_index_))
3054
                && this->info_symndx_ == NULL
3055
                && this->info_ == 0);
3056
    this->info_section_ = os;
3057
    this->info_uses_section_index_ = false;
3058
  }
3059
 
3060
  // Set the info field to a constant.
3061
  void
3062
  set_info(unsigned int v)
3063
  {
3064
    gold_assert(this->info_section_ == NULL
3065
                && this->info_symndx_ == NULL
3066
                && (this->info_ == 0
3067
                    || this->info_ == v));
3068
    this->info_ = v;
3069
  }
3070
 
3071
  // Set the addralign field.
3072
  void
3073
  set_addralign(uint64_t v)
3074
  { this->addralign_ = v; }
3075
 
3076
  // Whether the output section index has been set.
3077
  bool
3078
  has_out_shndx() const
3079
  { return this->out_shndx_ != -1U; }
3080
 
3081
  // Indicate that we need a symtab index.
3082
  void
3083
  set_needs_symtab_index()
3084
  { this->needs_symtab_index_ = true; }
3085
 
3086
  // Return whether we need a symtab index.
3087
  bool
3088
  needs_symtab_index() const
3089
  { return this->needs_symtab_index_; }
3090
 
3091
  // Get the symtab index.
3092
  unsigned int
3093
  symtab_index() const
3094
  {
3095
    gold_assert(this->symtab_index_ != 0);
3096
    return this->symtab_index_;
3097
  }
3098
 
3099
  // Set the symtab index.
3100
  void
3101
  set_symtab_index(unsigned int index)
3102
  {
3103
    gold_assert(index != 0);
3104
    this->symtab_index_ = index;
3105
  }
3106
 
3107
  // Indicate that we need a dynsym index.
3108
  void
3109
  set_needs_dynsym_index()
3110
  { this->needs_dynsym_index_ = true; }
3111
 
3112
  // Return whether we need a dynsym index.
3113
  bool
3114
  needs_dynsym_index() const
3115
  { return this->needs_dynsym_index_; }
3116
 
3117
  // Get the dynsym index.
3118
  unsigned int
3119
  dynsym_index() const
3120
  {
3121
    gold_assert(this->dynsym_index_ != 0);
3122
    return this->dynsym_index_;
3123
  }
3124
 
3125
  // Set the dynsym index.
3126
  void
3127
  set_dynsym_index(unsigned int index)
3128
  {
3129
    gold_assert(index != 0);
3130
    this->dynsym_index_ = index;
3131
  }
3132
 
3133
  // Return whether the input sections sections attachd to this output
3134
  // section may require sorting.  This is used to handle constructor
3135
  // priorities compatibly with GNU ld.
3136
  bool
3137
  may_sort_attached_input_sections() const
3138
  { return this->may_sort_attached_input_sections_; }
3139
 
3140
  // Record that the input sections attached to this output section
3141
  // may require sorting.
3142
  void
3143
  set_may_sort_attached_input_sections()
3144
  { this->may_sort_attached_input_sections_ = true; }
3145
 
3146
   // Returns true if input sections must be sorted according to the
3147
  // order in which their name appear in the --section-ordering-file.
3148
  bool
3149
  input_section_order_specified()
3150
  { return this->input_section_order_specified_; }
3151
 
3152
  // Record that input sections must be sorted as some of their names
3153
  // match the patterns specified through --section-ordering-file.
3154
  void
3155
  set_input_section_order_specified()
3156
  { this->input_section_order_specified_ = true; }
3157
 
3158
  // Return whether the input sections attached to this output section
3159
  // require sorting.  This is used to handle constructor priorities
3160
  // compatibly with GNU ld.
3161
  bool
3162
  must_sort_attached_input_sections() const
3163
  { return this->must_sort_attached_input_sections_; }
3164
 
3165
  // Record that the input sections attached to this output section
3166
  // require sorting.
3167
  void
3168
  set_must_sort_attached_input_sections()
3169
  { this->must_sort_attached_input_sections_ = true; }
3170
 
3171
  // Get the order in which this section appears in the PT_LOAD output
3172
  // segment.
3173
  Output_section_order
3174
  order() const
3175
  { return this->order_; }
3176
 
3177
  // Set the order for this section.
3178
  void
3179
  set_order(Output_section_order order)
3180
  { this->order_ = order; }
3181
 
3182
  // Return whether this section holds relro data--data which has
3183
  // dynamic relocations but which may be marked read-only after the
3184
  // dynamic relocations have been completed.
3185
  bool
3186
  is_relro() const
3187
  { return this->is_relro_; }
3188
 
3189
  // Record that this section holds relro data.
3190
  void
3191
  set_is_relro()
3192
  { this->is_relro_ = true; }
3193
 
3194
  // Record that this section does not hold relro data.
3195
  void
3196
  clear_is_relro()
3197
  { this->is_relro_ = false; }
3198
 
3199
  // True if this is a small section: a section which holds small
3200
  // variables.
3201
  bool
3202
  is_small_section() const
3203
  { return this->is_small_section_; }
3204
 
3205
  // Record that this is a small section.
3206
  void
3207
  set_is_small_section()
3208
  { this->is_small_section_ = true; }
3209
 
3210
  // True if this is a large section: a section which holds large
3211
  // variables.
3212
  bool
3213
  is_large_section() const
3214
  { return this->is_large_section_; }
3215
 
3216
  // Record that this is a large section.
3217
  void
3218
  set_is_large_section()
3219
  { this->is_large_section_ = true; }
3220
 
3221
  // True if this is a large data (not BSS) section.
3222
  bool
3223
  is_large_data_section()
3224
  { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
3225
 
3226
  // Return whether this section should be written after all the input
3227
  // sections are complete.
3228
  bool
3229
  after_input_sections() const
3230
  { return this->after_input_sections_; }
3231
 
3232
  // Record that this section should be written after all the input
3233
  // sections are complete.
3234
  void
3235
  set_after_input_sections()
3236
  { this->after_input_sections_ = true; }
3237
 
3238
  // Return whether this section requires postprocessing after all
3239
  // relocations have been applied.
3240
  bool
3241
  requires_postprocessing() const
3242
  { return this->requires_postprocessing_; }
3243
 
3244
  // If a section requires postprocessing, return the buffer to use.
3245
  unsigned char*
3246
  postprocessing_buffer() const
3247
  {
3248
    gold_assert(this->postprocessing_buffer_ != NULL);
3249
    return this->postprocessing_buffer_;
3250
  }
3251
 
3252
  // If a section requires postprocessing, create the buffer to use.
3253
  void
3254
  create_postprocessing_buffer();
3255
 
3256
  // If a section requires postprocessing, this is the size of the
3257
  // buffer to which relocations should be applied.
3258
  off_t
3259
  postprocessing_buffer_size() const
3260
  { return this->current_data_size_for_child(); }
3261
 
3262
  // Modify the section name.  This is only permitted for an
3263
  // unallocated section, and only before the size has been finalized.
3264
  // Otherwise the name will not get into Layout::namepool_.
3265
  void
3266
  set_name(const char* newname)
3267
  {
3268
    gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
3269
    gold_assert(!this->is_data_size_valid());
3270
    this->name_ = newname;
3271
  }
3272
 
3273
  // Return whether the offset OFFSET in the input section SHNDX in
3274
  // object OBJECT is being included in the link.
3275
  bool
3276
  is_input_address_mapped(const Relobj* object, unsigned int shndx,
3277
                          off_t offset) const;
3278
 
3279
  // Return the offset within the output section of OFFSET relative to
3280
  // the start of input section SHNDX in object OBJECT.
3281
  section_offset_type
3282
  output_offset(const Relobj* object, unsigned int shndx,
3283
                section_offset_type offset) const;
3284
 
3285
  // Return the output virtual address of OFFSET relative to the start
3286
  // of input section SHNDX in object OBJECT.
3287
  uint64_t
3288
  output_address(const Relobj* object, unsigned int shndx,
3289
                 off_t offset) const;
3290
 
3291
  // Look for the merged section for input section SHNDX in object
3292
  // OBJECT.  If found, return true, and set *ADDR to the address of
3293
  // the start of the merged section.  This is not necessary the
3294
  // output offset corresponding to input offset 0 in the section,
3295
  // since the section may be mapped arbitrarily.
3296
  bool
3297
  find_starting_output_address(const Relobj* object, unsigned int shndx,
3298
                               uint64_t* addr) const;
3299
 
3300
  // Record that this output section was found in the SECTIONS clause
3301
  // of a linker script.
3302
  void
3303
  set_found_in_sections_clause()
3304
  { this->found_in_sections_clause_ = true; }
3305
 
3306
  // Return whether this output section was found in the SECTIONS
3307
  // clause of a linker script.
3308
  bool
3309
  found_in_sections_clause() const
3310
  { return this->found_in_sections_clause_; }
3311
 
3312
  // Write the section header into *OPHDR.
3313
  template<int size, bool big_endian>
3314
  void
3315
  write_header(const Layout*, const Stringpool*,
3316
               elfcpp::Shdr_write<size, big_endian>*) const;
3317
 
3318
  // The next few calls are for linker script support.
3319
 
3320
  // In some cases we need to keep a list of the input sections
3321
  // associated with this output section.  We only need the list if we
3322
  // might have to change the offsets of the input section within the
3323
  // output section after we add the input section.  The ordinary
3324
  // input sections will be written out when we process the object
3325
  // file, and as such we don't need to track them here.  We do need
3326
  // to track Output_section_data objects here.  We store instances of
3327
  // this structure in a std::vector, so it must be a POD.  There can
3328
  // be many instances of this structure, so we use a union to save
3329
  // some space.
3330
  class Input_section
3331
  {
3332
   public:
3333
    Input_section()
3334
      : shndx_(0), p2align_(0)
3335
    {
3336
      this->u1_.data_size = 0;
3337
      this->u2_.object = NULL;
3338
    }
3339
 
3340
    // For an ordinary input section.
3341
    Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3342
                  uint64_t addralign)
3343
      : shndx_(shndx),
3344
        p2align_(ffsll(static_cast<long long>(addralign))),
3345
        section_order_index_(0)
3346
    {
3347
      gold_assert(shndx != OUTPUT_SECTION_CODE
3348
                  && shndx != MERGE_DATA_SECTION_CODE
3349
                  && shndx != MERGE_STRING_SECTION_CODE
3350
                  && shndx != RELAXED_INPUT_SECTION_CODE);
3351
      this->u1_.data_size = data_size;
3352
      this->u2_.object = object;
3353
    }
3354
 
3355
    // For a non-merge output section.
3356
    Input_section(Output_section_data* posd)
3357
      : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3358
        section_order_index_(0)
3359
    {
3360
      this->u1_.data_size = 0;
3361
      this->u2_.posd = posd;
3362
    }
3363
 
3364
    // For a merge section.
3365
    Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3366
      : shndx_(is_string
3367
               ? MERGE_STRING_SECTION_CODE
3368
               : MERGE_DATA_SECTION_CODE),
3369
        p2align_(0),
3370
        section_order_index_(0)
3371
    {
3372
      this->u1_.entsize = entsize;
3373
      this->u2_.posd = posd;
3374
    }
3375
 
3376
    // For a relaxed input section.
3377
    Input_section(Output_relaxed_input_section* psection)
3378
      : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3379
        section_order_index_(0)
3380
    {
3381
      this->u1_.data_size = 0;
3382
      this->u2_.poris = psection;
3383
    }
3384
 
3385
    unsigned int
3386
    section_order_index() const
3387
    {
3388
      return this->section_order_index_;
3389
    }
3390
 
3391
    void
3392
    set_section_order_index(unsigned int number)
3393
    {
3394
      this->section_order_index_ = number;
3395
    }
3396
 
3397
    // The required alignment.
3398
    uint64_t
3399
    addralign() const
3400
    {
3401
      if (this->p2align_ != 0)
3402
        return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3403
      else if (!this->is_input_section())
3404
        return this->u2_.posd->addralign();
3405
      else
3406
        return 0;
3407
    }
3408
 
3409
    // Set the required alignment, which must be either 0 or a power of 2.
3410
    // For input sections that are sub-classes of Output_section_data, a
3411
    // alignment of zero means asking the underlying object for alignment.
3412
    void
3413
    set_addralign(uint64_t addralign)
3414
    {
3415
      if (addralign == 0)
3416
        this->p2align_ = 0;
3417
      else
3418
        {
3419
          gold_assert((addralign & (addralign - 1)) == 0);
3420
          this->p2align_ = ffsll(static_cast<long long>(addralign));
3421
        }
3422
    }
3423
 
3424
    // Return the current required size, without finalization.
3425
    off_t
3426
    current_data_size() const;
3427
 
3428
    // Return the required size.
3429
    off_t
3430
    data_size() const;
3431
 
3432
    // Whether this is an input section.
3433
    bool
3434
    is_input_section() const
3435
    {
3436
      return (this->shndx_ != OUTPUT_SECTION_CODE
3437
              && this->shndx_ != MERGE_DATA_SECTION_CODE
3438
              && this->shndx_ != MERGE_STRING_SECTION_CODE
3439
              && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3440
    }
3441
 
3442
    // Return whether this is a merge section which matches the
3443
    // parameters.
3444
    bool
3445
    is_merge_section(bool is_string, uint64_t entsize,
3446
                     uint64_t addralign) const
3447
    {
3448
      return (this->shndx_ == (is_string
3449
                               ? MERGE_STRING_SECTION_CODE
3450
                               : MERGE_DATA_SECTION_CODE)
3451
              && this->u1_.entsize == entsize
3452
              && this->addralign() == addralign);
3453
    }
3454
 
3455
    // Return whether this is a merge section for some input section.
3456
    bool
3457
    is_merge_section() const
3458
    {
3459
      return (this->shndx_ == MERGE_DATA_SECTION_CODE
3460
              || this->shndx_ == MERGE_STRING_SECTION_CODE);
3461
    }
3462
 
3463
    // Return whether this is a relaxed input section.
3464
    bool
3465
    is_relaxed_input_section() const
3466
    { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3467
 
3468
    // Return whether this is a generic Output_section_data.
3469
    bool
3470
    is_output_section_data() const
3471
    {
3472
      return this->shndx_ == OUTPUT_SECTION_CODE;
3473
    }
3474
 
3475
    // Return the object for an input section.
3476
    Relobj*
3477
    relobj() const;
3478
 
3479
    // Return the input section index for an input section.
3480
    unsigned int
3481
    shndx() const;
3482
 
3483
    // For non-input-sections, return the associated Output_section_data
3484
    // object.
3485
    Output_section_data*
3486
    output_section_data() const
3487
    {
3488
      gold_assert(!this->is_input_section());
3489
      return this->u2_.posd;
3490
    }
3491
 
3492
    // For a merge section, return the Output_merge_base pointer.
3493
    Output_merge_base*
3494
    output_merge_base() const
3495
    {
3496
      gold_assert(this->is_merge_section());
3497
      return this->u2_.pomb;
3498
    }
3499
 
3500
    // Return the Output_relaxed_input_section object.
3501
    Output_relaxed_input_section*
3502
    relaxed_input_section() const
3503
    {
3504
      gold_assert(this->is_relaxed_input_section());
3505
      return this->u2_.poris;
3506
    }
3507
 
3508
    // Set the output section.
3509
    void
3510
    set_output_section(Output_section* os)
3511
    {
3512
      gold_assert(!this->is_input_section());
3513
      Output_section_data* posd =
3514
        this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3515
      posd->set_output_section(os);
3516
    }
3517
 
3518
    // Set the address and file offset.  This is called during
3519
    // Layout::finalize.  SECTION_FILE_OFFSET is the file offset of
3520
    // the enclosing section.
3521
    void
3522
    set_address_and_file_offset(uint64_t address, off_t file_offset,
3523
                                off_t section_file_offset);
3524
 
3525
    // Reset the address and file offset.
3526
    void
3527
    reset_address_and_file_offset();
3528
 
3529
    // Finalize the data size.
3530
    void
3531
    finalize_data_size();
3532
 
3533
    // Add an input section, for SHF_MERGE sections.
3534
    bool
3535
    add_input_section(Relobj* object, unsigned int shndx)
3536
    {
3537
      gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3538
                  || this->shndx_ == MERGE_STRING_SECTION_CODE);
3539
      return this->u2_.posd->add_input_section(object, shndx);
3540
    }
3541
 
3542
    // Given an input OBJECT, an input section index SHNDX within that
3543
    // object, and an OFFSET relative to the start of that input
3544
    // section, return whether or not the output offset is known.  If
3545
    // this function returns true, it sets *POUTPUT to the offset in
3546
    // the output section, relative to the start of the input section
3547
    // in the output section.  *POUTPUT may be different from OFFSET
3548
    // for a merged section.
3549
    bool
3550
    output_offset(const Relobj* object, unsigned int shndx,
3551
                  section_offset_type offset,
3552
                  section_offset_type* poutput) const;
3553
 
3554
    // Return whether this is the merge section for the input section
3555
    // SHNDX in OBJECT.
3556
    bool
3557
    is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3558
 
3559
    // Write out the data.  This does nothing for an input section.
3560
    void
3561
    write(Output_file*);
3562
 
3563
    // Write the data to a buffer.  This does nothing for an input
3564
    // section.
3565
    void
3566
    write_to_buffer(unsigned char*);
3567
 
3568
    // Print to a map file.
3569
    void
3570
    print_to_mapfile(Mapfile*) const;
3571
 
3572
    // Print statistics about merge sections to stderr.
3573
    void
3574
    print_merge_stats(const char* section_name)
3575
    {
3576
      if (this->shndx_ == MERGE_DATA_SECTION_CODE
3577
          || this->shndx_ == MERGE_STRING_SECTION_CODE)
3578
        this->u2_.posd->print_merge_stats(section_name);
3579
    }
3580
 
3581
   private:
3582
    // Code values which appear in shndx_.  If the value is not one of
3583
    // these codes, it is the input section index in the object file.
3584
    enum
3585
    {
3586
      // An Output_section_data.
3587
      OUTPUT_SECTION_CODE = -1U,
3588
      // An Output_section_data for an SHF_MERGE section with
3589
      // SHF_STRINGS not set.
3590
      MERGE_DATA_SECTION_CODE = -2U,
3591
      // An Output_section_data for an SHF_MERGE section with
3592
      // SHF_STRINGS set.
3593
      MERGE_STRING_SECTION_CODE = -3U,
3594
      // An Output_section_data for a relaxed input section.
3595
      RELAXED_INPUT_SECTION_CODE = -4U
3596
    };
3597
 
3598
    // For an ordinary input section, this is the section index in the
3599
    // input file.  For an Output_section_data, this is
3600
    // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3601
    // MERGE_STRING_SECTION_CODE.
3602
    unsigned int shndx_;
3603
    // The required alignment, stored as a power of 2.
3604
    unsigned int p2align_;
3605
    union
3606
    {
3607
      // For an ordinary input section, the section size.
3608
      off_t data_size;
3609
      // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3610
      // used.  For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3611
      // entity size.
3612
      uint64_t entsize;
3613
    } u1_;
3614
    union
3615
    {
3616
      // For an ordinary input section, the object which holds the
3617
      // input section.
3618
      Relobj* object;
3619
      // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3620
      // MERGE_STRING_SECTION_CODE, the data.
3621
      Output_section_data* posd;
3622
      Output_merge_base* pomb;
3623
      // For RELAXED_INPUT_SECTION_CODE, the data.
3624
      Output_relaxed_input_section* poris;
3625
    } u2_;
3626
    // The line number of the pattern it matches in the --section-ordering-file
3627
    // file.  It is 0 if does not match any pattern.
3628
    unsigned int section_order_index_;
3629
  };
3630
 
3631
  // Store the list of input sections for this Output_section into the
3632
  // list passed in.  This removes the input sections, leaving only
3633
  // any Output_section_data elements.  This returns the size of those
3634
  // Output_section_data elements.  ADDRESS is the address of this
3635
  // output section.  FILL is the fill value to use, in case there are
3636
  // any spaces between the remaining Output_section_data elements.
3637
  uint64_t
3638
  get_input_sections(uint64_t address, const std::string& fill,
3639
                     std::list<Input_section>*);
3640
 
3641
  // Add a script input section.  A script input section can either be
3642
  // a plain input section or a sub-class of Output_section_data.
3643
  void
3644
  add_script_input_section(const Input_section& input_section);
3645
 
3646
  // Set the current size of the output section.
3647
  void
3648
  set_current_data_size(off_t size)
3649
  { this->set_current_data_size_for_child(size); }
3650
 
3651
  // End of linker script support.
3652
 
3653
  // Save states before doing section layout.
3654
  // This is used for relaxation.
3655
  void
3656
  save_states();
3657
 
3658
  // Restore states prior to section layout.
3659
  void
3660
  restore_states();
3661
 
3662
  // Discard states.
3663
  void
3664
  discard_states();
3665
 
3666
  // Convert existing input sections to relaxed input sections.
3667
  void
3668
  convert_input_sections_to_relaxed_sections(
3669
      const std::vector<Output_relaxed_input_section*>& sections);
3670
 
3671
  // Find a relaxed input section to an input section in OBJECT
3672
  // with index SHNDX.  Return NULL if none is found.
3673
  const Output_relaxed_input_section*
3674
  find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3675
 
3676
  // Whether section offsets need adjustment due to relaxation.
3677
  bool
3678
  section_offsets_need_adjustment() const
3679
  { return this->section_offsets_need_adjustment_; }
3680
 
3681
  // Set section_offsets_need_adjustment to be true.
3682
  void
3683
  set_section_offsets_need_adjustment()
3684
  { this->section_offsets_need_adjustment_ = true; }
3685
 
3686
  // Adjust section offsets of input sections in this.  This is
3687
  // requires if relaxation caused some input sections to change sizes.
3688
  void
3689
  adjust_section_offsets();
3690
 
3691
  // Whether this is a NOLOAD section.
3692
  bool
3693
  is_noload() const
3694
  { return this->is_noload_; }
3695
 
3696
  // Set NOLOAD flag.
3697
  void
3698
  set_is_noload()
3699
  { this->is_noload_ = true; }
3700
 
3701
  // Print merge statistics to stderr.
3702
  void
3703
  print_merge_stats();
3704
 
3705
  // Set a fixed layout for the section.  Used for incremental update links.
3706
  void
3707
  set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3708
                   uint64_t sh_addralign);
3709
 
3710
  // Return TRUE if the section has a fixed layout.
3711
  bool
3712
  has_fixed_layout() const
3713
  { return this->has_fixed_layout_; }
3714
 
3715 159 khays
  // Set flag to allow patch space for this section.  Used for full
3716
  // incremental links.
3717
  void
3718
  set_is_patch_space_allowed()
3719
  { this->is_patch_space_allowed_ = true; }
3720
 
3721
  // Set a fill method to use for free space left in the output section
3722
  // during incremental links.
3723
  void
3724
  set_free_space_fill(Output_fill* free_space_fill)
3725
  {
3726
    this->free_space_fill_ = free_space_fill;
3727
    this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
3728
  }
3729
 
3730 27 khays
  // Reserve space within the fixed layout for the section.  Used for
3731
  // incremental update links.
3732
  void
3733
  reserve(uint64_t sh_offset, uint64_t sh_size);
3734
 
3735 148 khays
  // Allocate space from the free list for the section.  Used for
3736
  // incremental update links.
3737
  off_t
3738
  allocate(off_t len, uint64_t addralign);
3739
 
3740 27 khays
 protected:
3741
  // Return the output section--i.e., the object itself.
3742
  Output_section*
3743
  do_output_section()
3744
  { return this; }
3745
 
3746
  const Output_section*
3747
  do_output_section() const
3748
  { return this; }
3749
 
3750
  // Return the section index in the output file.
3751
  unsigned int
3752
  do_out_shndx() const
3753
  {
3754
    gold_assert(this->out_shndx_ != -1U);
3755
    return this->out_shndx_;
3756
  }
3757
 
3758
  // Set the output section index.
3759
  void
3760
  do_set_out_shndx(unsigned int shndx)
3761
  {
3762
    gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3763
    this->out_shndx_ = shndx;
3764
  }
3765
 
3766
  // Update the data size of the Output_section.  For a typical
3767
  // Output_section, there is nothing to do, but if there are any
3768
  // Output_section_data objects we need to do a trial layout
3769
  // here.
3770
  virtual void
3771
  update_data_size();
3772
 
3773
  // Set the final data size of the Output_section.  For a typical
3774
  // Output_section, there is nothing to do, but if there are any
3775
  // Output_section_data objects we need to set their final addresses
3776
  // here.
3777
  virtual void
3778
  set_final_data_size();
3779
 
3780
  // Reset the address and file offset.
3781
  void
3782
  do_reset_address_and_file_offset();
3783
 
3784
  // Return true if address and file offset already have reset values. In
3785
  // other words, calling reset_address_and_file_offset will not change them.
3786
  bool
3787
  do_address_and_file_offset_have_reset_values() const;
3788
 
3789
  // Write the data to the file.  For a typical Output_section, this
3790
  // does nothing: the data is written out by calling Object::Relocate
3791
  // on each input object.  But if there are any Output_section_data
3792
  // objects we do need to write them out here.
3793
  virtual void
3794
  do_write(Output_file*);
3795
 
3796
  // Return the address alignment--function required by parent class.
3797
  uint64_t
3798
  do_addralign() const
3799
  { return this->addralign_; }
3800
 
3801
  // Return whether there is a load address.
3802
  bool
3803
  do_has_load_address() const
3804
  { return this->has_load_address_; }
3805
 
3806
  // Return the load address.
3807
  uint64_t
3808
  do_load_address() const
3809
  {
3810
    gold_assert(this->has_load_address_);
3811
    return this->load_address_;
3812
  }
3813
 
3814
  // Return whether this is an Output_section.
3815
  bool
3816
  do_is_section() const
3817
  { return true; }
3818
 
3819
  // Return whether this is a section of the specified type.
3820
  bool
3821
  do_is_section_type(elfcpp::Elf_Word type) const
3822
  { return this->type_ == type; }
3823
 
3824
  // Return whether the specified section flag is set.
3825
  bool
3826
  do_is_section_flag_set(elfcpp::Elf_Xword flag) const
3827
  { return (this->flags_ & flag) != 0; }
3828
 
3829
  // Set the TLS offset.  Called only for SHT_TLS sections.
3830
  void
3831
  do_set_tls_offset(uint64_t tls_base);
3832
 
3833
  // Return the TLS offset, relative to the base of the TLS segment.
3834
  // Valid only for SHT_TLS sections.
3835
  uint64_t
3836
  do_tls_offset() const
3837
  { return this->tls_offset_; }
3838
 
3839
  // This may be implemented by a child class.
3840
  virtual void
3841
  do_finalize_name(Layout*)
3842
  { }
3843
 
3844
  // Print to the map file.
3845
  virtual void
3846
  do_print_to_mapfile(Mapfile*) const;
3847
 
3848
  // Record that this section requires postprocessing after all
3849
  // relocations have been applied.  This is called by a child class.
3850
  void
3851
  set_requires_postprocessing()
3852
  {
3853
    this->requires_postprocessing_ = true;
3854
    this->after_input_sections_ = true;
3855
  }
3856
 
3857
  // Write all the data of an Output_section into the postprocessing
3858
  // buffer.
3859
  void
3860
  write_to_postprocessing_buffer();
3861
 
3862
  typedef std::vector<Input_section> Input_section_list;
3863
 
3864
  // Allow a child class to access the input sections.
3865
  const Input_section_list&
3866
  input_sections() const
3867
  { return this->input_sections_; }
3868
 
3869
  // Whether this always keeps an input section list
3870
  bool
3871
  always_keeps_input_sections() const
3872
  { return this->always_keeps_input_sections_; }
3873
 
3874
  // Always keep an input section list.
3875
  void
3876
  set_always_keeps_input_sections()
3877
  {
3878
    gold_assert(this->current_data_size_for_child() == 0);
3879
    this->always_keeps_input_sections_ = true;
3880
  }
3881
 
3882
 private:
3883
  // We only save enough information to undo the effects of section layout.
3884
  class Checkpoint_output_section
3885
  {
3886
   public:
3887
    Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3888
                              const Input_section_list& input_sections,
3889
                              off_t first_input_offset,
3890
                              bool attached_input_sections_are_sorted)
3891
      : addralign_(addralign), flags_(flags),
3892
        input_sections_(input_sections),
3893
        input_sections_size_(input_sections_.size()),
3894
        input_sections_copy_(), first_input_offset_(first_input_offset),
3895
        attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3896
    { }
3897
 
3898
    virtual
3899
    ~Checkpoint_output_section()
3900
    { }
3901
 
3902
    // Return the address alignment.
3903
    uint64_t
3904
    addralign() const
3905
    { return this->addralign_; }
3906
 
3907
    // Return the section flags.
3908
    elfcpp::Elf_Xword
3909
    flags() const
3910
    { return this->flags_; }
3911
 
3912
    // Return a reference to the input section list copy.
3913
    Input_section_list*
3914
    input_sections()
3915
    { return &this->input_sections_copy_; }
3916
 
3917
    // Return the size of input_sections at the time when checkpoint is
3918
    // taken.
3919
    size_t
3920
    input_sections_size() const
3921
    { return this->input_sections_size_; }
3922
 
3923
    // Whether input sections are copied.
3924
    bool
3925
    input_sections_saved() const
3926
    { return this->input_sections_copy_.size() == this->input_sections_size_; }
3927
 
3928
    off_t
3929
    first_input_offset() const
3930
    { return this->first_input_offset_; }
3931
 
3932
    bool
3933
    attached_input_sections_are_sorted() const
3934
    { return this->attached_input_sections_are_sorted_; }
3935
 
3936
    // Save input sections.
3937
    void
3938
    save_input_sections()
3939
    {
3940
      this->input_sections_copy_.reserve(this->input_sections_size_);
3941
      this->input_sections_copy_.clear();
3942
      Input_section_list::const_iterator p = this->input_sections_.begin();
3943
      gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3944
      for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3945
        this->input_sections_copy_.push_back(*p);
3946
    }
3947
 
3948
   private:
3949
    // The section alignment.
3950
    uint64_t addralign_;
3951
    // The section flags.
3952
    elfcpp::Elf_Xword flags_;
3953
    // Reference to the input sections to be checkpointed.
3954
    const Input_section_list& input_sections_;
3955
    // Size of the checkpointed portion of input_sections_;
3956
    size_t input_sections_size_;
3957
    // Copy of input sections.
3958
    Input_section_list input_sections_copy_;
3959
    // The offset of the first entry in input_sections_.
3960
    off_t first_input_offset_;
3961
    // True if the input sections attached to this output section have
3962
    // already been sorted.
3963
    bool attached_input_sections_are_sorted_;
3964
  };
3965
 
3966
  // This class is used to sort the input sections.
3967
  class Input_section_sort_entry;
3968
 
3969
  // This is the sort comparison function for ctors and dtors.
3970
  struct Input_section_sort_compare
3971
  {
3972
    bool
3973
    operator()(const Input_section_sort_entry&,
3974
               const Input_section_sort_entry&) const;
3975
  };
3976
 
3977
  // This is the sort comparison function for .init_array and .fini_array.
3978
  struct Input_section_sort_init_fini_compare
3979
  {
3980
    bool
3981
    operator()(const Input_section_sort_entry&,
3982
               const Input_section_sort_entry&) const;
3983
  };
3984
 
3985
  // This is the sort comparison function when a section order is specified
3986
  // from an input file.
3987
  struct Input_section_sort_section_order_index_compare
3988
  {
3989
    bool
3990
    operator()(const Input_section_sort_entry&,
3991
               const Input_section_sort_entry&) const;
3992
  };
3993
 
3994
  // Fill data.  This is used to fill in data between input sections.
3995
  // It is also used for data statements (BYTE, WORD, etc.) in linker
3996
  // scripts.  When we have to keep track of the input sections, we
3997
  // can use an Output_data_const, but we don't want to have to keep
3998
  // track of input sections just to implement fills.
3999
  class Fill
4000
  {
4001
   public:
4002
    Fill(off_t section_offset, off_t length)
4003
      : section_offset_(section_offset),
4004
        length_(convert_to_section_size_type(length))
4005
    { }
4006
 
4007
    // Return section offset.
4008
    off_t
4009
    section_offset() const
4010
    { return this->section_offset_; }
4011
 
4012
    // Return fill length.
4013
    section_size_type
4014
    length() const
4015
    { return this->length_; }
4016
 
4017
   private:
4018
    // The offset within the output section.
4019
    off_t section_offset_;
4020
    // The length of the space to fill.
4021
    section_size_type length_;
4022
  };
4023
 
4024
  typedef std::vector<Fill> Fill_list;
4025
 
4026
  // Map used during relaxation of existing sections.  This map
4027
  // a section id an input section list index.  We assume that
4028
  // Input_section_list is a vector.
4029
  typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
4030
 
4031
  // Add a new output section by Input_section.
4032
  void
4033
  add_output_section_data(Input_section*);
4034
 
4035
  // Add an SHF_MERGE input section.  Returns true if the section was
4036
  // handled.  If KEEPS_INPUT_SECTIONS is true, the output merge section
4037
  // stores information about the merged input sections.
4038
  bool
4039
  add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
4040
                          uint64_t entsize, uint64_t addralign,
4041
                          bool keeps_input_sections);
4042
 
4043
  // Add an output SHF_MERGE section POSD to this output section.
4044
  // IS_STRING indicates whether it is a SHF_STRINGS section, and
4045
  // ENTSIZE is the entity size.  This returns the entry added to
4046
  // input_sections_.
4047
  void
4048
  add_output_merge_section(Output_section_data* posd, bool is_string,
4049
                           uint64_t entsize);
4050
 
4051
  // Sort the attached input sections.
4052
  void
4053
  sort_attached_input_sections();
4054
 
4055
  // Find the merge section into which an input section with index SHNDX in
4056
  // OBJECT has been added.  Return NULL if none found.
4057
  Output_section_data*
4058
  find_merge_section(const Relobj* object, unsigned int shndx) const;
4059
 
4060
  // Build a relaxation map.
4061
  void
4062
  build_relaxation_map(
4063
      const Input_section_list& input_sections,
4064
      size_t limit,
4065
      Relaxation_map* map) const;
4066
 
4067
  // Convert input sections in an input section list into relaxed sections.
4068
  void
4069
  convert_input_sections_in_list_to_relaxed_sections(
4070
      const std::vector<Output_relaxed_input_section*>& relaxed_sections,
4071
      const Relaxation_map& map,
4072
      Input_section_list* input_sections);
4073
 
4074
  // Build the lookup maps for merge and relaxed input sections.
4075
  void
4076
  build_lookup_maps() const;
4077
 
4078
  // Most of these fields are only valid after layout.
4079
 
4080
  // The name of the section.  This will point into a Stringpool.
4081
  const char* name_;
4082
  // The section address is in the parent class.
4083
  // The section alignment.
4084
  uint64_t addralign_;
4085
  // The section entry size.
4086
  uint64_t entsize_;
4087
  // The load address.  This is only used when using a linker script
4088
  // with a SECTIONS clause.  The has_load_address_ field indicates
4089
  // whether this field is valid.
4090
  uint64_t load_address_;
4091
  // The file offset is in the parent class.
4092
  // Set the section link field to the index of this section.
4093
  const Output_data* link_section_;
4094
  // If link_section_ is NULL, this is the link field.
4095
  unsigned int link_;
4096
  // Set the section info field to the index of this section.
4097
  const Output_section* info_section_;
4098
  // If info_section_ is NULL, set the info field to the symbol table
4099
  // index of this symbol.
4100
  const Symbol* info_symndx_;
4101
  // If info_section_ and info_symndx_ are NULL, this is the section
4102
  // info field.
4103
  unsigned int info_;
4104
  // The section type.
4105
  const elfcpp::Elf_Word type_;
4106
  // The section flags.
4107
  elfcpp::Elf_Xword flags_;
4108
  // The order of this section in the output segment.
4109
  Output_section_order order_;
4110
  // The section index.
4111
  unsigned int out_shndx_;
4112
  // If there is a STT_SECTION for this output section in the normal
4113
  // symbol table, this is the symbol index.  This starts out as zero.
4114
  // It is initialized in Layout::finalize() to be the index, or -1U
4115
  // if there isn't one.
4116
  unsigned int symtab_index_;
4117
  // If there is a STT_SECTION for this output section in the dynamic
4118
  // symbol table, this is the symbol index.  This starts out as zero.
4119
  // It is initialized in Layout::finalize() to be the index, or -1U
4120
  // if there isn't one.
4121
  unsigned int dynsym_index_;
4122
  // The input sections.  This will be empty in cases where we don't
4123
  // need to keep track of them.
4124
  Input_section_list input_sections_;
4125
  // The offset of the first entry in input_sections_.
4126
  off_t first_input_offset_;
4127
  // The fill data.  This is separate from input_sections_ because we
4128
  // often will need fill sections without needing to keep track of
4129
  // input sections.
4130
  Fill_list fills_;
4131
  // If the section requires postprocessing, this buffer holds the
4132
  // section contents during relocation.
4133
  unsigned char* postprocessing_buffer_;
4134
  // Whether this output section needs a STT_SECTION symbol in the
4135
  // normal symbol table.  This will be true if there is a relocation
4136
  // which needs it.
4137
  bool needs_symtab_index_ : 1;
4138
  // Whether this output section needs a STT_SECTION symbol in the
4139
  // dynamic symbol table.  This will be true if there is a dynamic
4140
  // relocation which needs it.
4141
  bool needs_dynsym_index_ : 1;
4142
  // Whether the link field of this output section should point to the
4143
  // normal symbol table.
4144
  bool should_link_to_symtab_ : 1;
4145
  // Whether the link field of this output section should point to the
4146
  // dynamic symbol table.
4147
  bool should_link_to_dynsym_ : 1;
4148
  // Whether this section should be written after all the input
4149
  // sections are complete.
4150
  bool after_input_sections_ : 1;
4151
  // Whether this section requires post processing after all
4152
  // relocations have been applied.
4153
  bool requires_postprocessing_ : 1;
4154
  // Whether an input section was mapped to this output section
4155
  // because of a SECTIONS clause in a linker script.
4156
  bool found_in_sections_clause_ : 1;
4157
  // Whether this section has an explicitly specified load address.
4158
  bool has_load_address_ : 1;
4159
  // True if the info_section_ field means the section index of the
4160
  // section, false if it means the symbol index of the corresponding
4161
  // section symbol.
4162
  bool info_uses_section_index_ : 1;
4163
  // True if input sections attached to this output section have to be
4164
  // sorted according to a specified order.
4165
  bool input_section_order_specified_ : 1;
4166
  // True if the input sections attached to this output section may
4167
  // need sorting.
4168
  bool may_sort_attached_input_sections_ : 1;
4169
  // True if the input sections attached to this output section must
4170
  // be sorted.
4171
  bool must_sort_attached_input_sections_ : 1;
4172
  // True if the input sections attached to this output section have
4173
  // already been sorted.
4174
  bool attached_input_sections_are_sorted_ : 1;
4175
  // True if this section holds relro data.
4176
  bool is_relro_ : 1;
4177
  // True if this is a small section.
4178
  bool is_small_section_ : 1;
4179
  // True if this is a large section.
4180
  bool is_large_section_ : 1;
4181
  // Whether code-fills are generated at write.
4182
  bool generate_code_fills_at_write_ : 1;
4183
  // Whether the entry size field should be zero.
4184
  bool is_entsize_zero_ : 1;
4185
  // Whether section offsets need adjustment due to relaxation.
4186
  bool section_offsets_need_adjustment_ : 1;
4187
  // Whether this is a NOLOAD section.
4188
  bool is_noload_ : 1;
4189
  // Whether this always keeps input section.
4190
  bool always_keeps_input_sections_ : 1;
4191
  // Whether this section has a fixed layout, for incremental update links.
4192
  bool has_fixed_layout_ : 1;
4193 159 khays
  // True if we can add patch space to this section.
4194
  bool is_patch_space_allowed_ : 1;
4195 27 khays
  // For SHT_TLS sections, the offset of this section relative to the base
4196
  // of the TLS segment.
4197
  uint64_t tls_offset_;
4198
  // Saved checkpoint.
4199
  Checkpoint_output_section* checkpoint_;
4200
  // Fast lookup maps for merged and relaxed input sections.
4201
  Output_section_lookup_maps* lookup_maps_;
4202
  // List of available regions within the section, for incremental
4203
  // update links.
4204
  Free_list free_list_;
4205 159 khays
  // Method for filling chunks of free space.
4206
  Output_fill* free_space_fill_;
4207
  // Amount added as patch space for incremental linking.
4208
  off_t patch_space_;
4209 27 khays
};
4210
 
4211
// An output segment.  PT_LOAD segments are built from collections of
4212
// output sections.  Other segments typically point within PT_LOAD
4213
// segments, and are built directly as needed.
4214
//
4215
// NOTE: We want to use the copy constructor for this class.  During
4216
// relaxation, we may try built the segments multiple times.  We do
4217
// that by copying the original segment list before lay-out, doing
4218
// a trial lay-out and roll-back to the saved copied if we need to
4219
// to the lay-out again.
4220
 
4221
class Output_segment
4222
{
4223
 public:
4224
  // Create an output segment, specifying the type and flags.
4225
  Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
4226
 
4227
  // Return the virtual address.
4228
  uint64_t
4229
  vaddr() const
4230
  { return this->vaddr_; }
4231
 
4232
  // Return the physical address.
4233
  uint64_t
4234
  paddr() const
4235
  { return this->paddr_; }
4236
 
4237
  // Return the segment type.
4238
  elfcpp::Elf_Word
4239
  type() const
4240
  { return this->type_; }
4241
 
4242
  // Return the segment flags.
4243
  elfcpp::Elf_Word
4244
  flags() const
4245
  { return this->flags_; }
4246
 
4247
  // Return the memory size.
4248
  uint64_t
4249
  memsz() const
4250
  { return this->memsz_; }
4251
 
4252
  // Return the file size.
4253
  off_t
4254
  filesz() const
4255
  { return this->filesz_; }
4256
 
4257
  // Return the file offset.
4258
  off_t
4259
  offset() const
4260
  { return this->offset_; }
4261
 
4262
  // Whether this is a segment created to hold large data sections.
4263
  bool
4264
  is_large_data_segment() const
4265
  { return this->is_large_data_segment_; }
4266
 
4267
  // Record that this is a segment created to hold large data
4268
  // sections.
4269
  void
4270
  set_is_large_data_segment()
4271
  { this->is_large_data_segment_ = true; }
4272
 
4273
  // Return the maximum alignment of the Output_data.
4274
  uint64_t
4275
  maximum_alignment();
4276
 
4277
  // Add the Output_section OS to this PT_LOAD segment.  SEG_FLAGS is
4278
  // the segment flags to use.
4279
  void
4280
  add_output_section_to_load(Layout* layout, Output_section* os,
4281
                             elfcpp::Elf_Word seg_flags);
4282
 
4283
  // Add the Output_section OS to this non-PT_LOAD segment.  SEG_FLAGS
4284
  // is the segment flags to use.
4285
  void
4286
  add_output_section_to_nonload(Output_section* os,
4287
                                elfcpp::Elf_Word seg_flags);
4288
 
4289
  // Remove an Output_section from this segment.  It is an error if it
4290
  // is not present.
4291
  void
4292
  remove_output_section(Output_section* os);
4293
 
4294
  // Add an Output_data (which need not be an Output_section) to the
4295
  // start of this segment.
4296
  void
4297
  add_initial_output_data(Output_data*);
4298
 
4299
  // Return true if this segment has any sections which hold actual
4300
  // data, rather than being a BSS section.
4301
  bool
4302
  has_any_data_sections() const;
4303
 
4304
  // Whether this segment has a dynamic relocs.
4305
  bool
4306
  has_dynamic_reloc() const;
4307
 
4308
  // Return the address of the first section.
4309
  uint64_t
4310
  first_section_load_address() const;
4311
 
4312
  // Return whether the addresses have been set already.
4313
  bool
4314
  are_addresses_set() const
4315
  { return this->are_addresses_set_; }
4316
 
4317
  // Set the addresses.
4318
  void
4319
  set_addresses(uint64_t vaddr, uint64_t paddr)
4320
  {
4321
    this->vaddr_ = vaddr;
4322
    this->paddr_ = paddr;
4323
    this->are_addresses_set_ = true;
4324
  }
4325
 
4326
  // Update the flags for the flags of an output section added to this
4327
  // segment.
4328
  void
4329
  update_flags_for_output_section(elfcpp::Elf_Xword flags)
4330
  {
4331
    // The ELF ABI specifies that a PT_TLS segment should always have
4332
    // PF_R as the flags.
4333
    if (this->type() != elfcpp::PT_TLS)
4334
      this->flags_ |= flags;
4335
  }
4336
 
4337
  // Set the segment flags.  This is only used if we have a PHDRS
4338
  // clause which explicitly specifies the flags.
4339
  void
4340
  set_flags(elfcpp::Elf_Word flags)
4341
  { this->flags_ = flags; }
4342
 
4343
  // Set the address of the segment to ADDR and the offset to *POFF
4344
  // and set the addresses and offsets of all contained output
4345
  // sections accordingly.  Set the section indexes of all contained
4346
  // output sections starting with *PSHNDX.  If RESET is true, first
4347
  // reset the addresses of the contained sections.  Return the
4348
  // address of the immediately following segment.  Update *POFF and
4349
  // *PSHNDX.  This should only be called for a PT_LOAD segment.
4350
  uint64_t
4351
  set_section_addresses(Layout*, bool reset, uint64_t addr,
4352
                        unsigned int* increase_relro, bool* has_relro,
4353
                        off_t* poff, unsigned int* pshndx);
4354
 
4355
  // Set the minimum alignment of this segment.  This may be adjusted
4356
  // upward based on the section alignments.
4357
  void
4358
  set_minimum_p_align(uint64_t align)
4359
  {
4360
    if (align > this->min_p_align_)
4361
      this->min_p_align_ = align;
4362
  }
4363
 
4364
  // Set the offset of this segment based on the section.  This should
4365
  // only be called for a non-PT_LOAD segment.
4366
  void
4367
  set_offset(unsigned int increase);
4368
 
4369
  // Set the TLS offsets of the sections contained in the PT_TLS segment.
4370
  void
4371
  set_tls_offsets();
4372
 
4373
  // Return the number of output sections.
4374
  unsigned int
4375
  output_section_count() const;
4376
 
4377
  // Return the section attached to the list segment with the lowest
4378
  // load address.  This is used when handling a PHDRS clause in a
4379
  // linker script.
4380
  Output_section*
4381
  section_with_lowest_load_address() const;
4382
 
4383
  // Write the segment header into *OPHDR.
4384
  template<int size, bool big_endian>
4385
  void
4386
  write_header(elfcpp::Phdr_write<size, big_endian>*);
4387
 
4388
  // Write the section headers of associated sections into V.
4389
  template<int size, bool big_endian>
4390
  unsigned char*
4391
  write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4392
                        unsigned int* pshndx) const;
4393
 
4394
  // Print the output sections in the map file.
4395
  void
4396
  print_sections_to_mapfile(Mapfile*) const;
4397
 
4398
 private:
4399
  typedef std::vector<Output_data*> Output_data_list;
4400
 
4401
  // Find the maximum alignment in an Output_data_list.
4402
  static uint64_t
4403
  maximum_alignment_list(const Output_data_list*);
4404
 
4405
  // Return whether the first data section is a relro section.
4406
  bool
4407
  is_first_section_relro() const;
4408
 
4409
  // Set the section addresses in an Output_data_list.
4410
  uint64_t
4411
  set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4412
                             uint64_t addr, off_t* poff, unsigned int* pshndx,
4413
                             bool* in_tls);
4414
 
4415
  // Return the number of Output_sections in an Output_data_list.
4416
  unsigned int
4417
  output_section_count_list(const Output_data_list*) const;
4418
 
4419
  // Return whether an Output_data_list has a dynamic reloc.
4420
  bool
4421
  has_dynamic_reloc_list(const Output_data_list*) const;
4422
 
4423
  // Find the section with the lowest load address in an
4424
  // Output_data_list.
4425
  void
4426
  lowest_load_address_in_list(const Output_data_list* pdl,
4427
                              Output_section** found,
4428
                              uint64_t* found_lma) const;
4429
 
4430
  // Find the first and last entries by address.
4431
  void
4432
  find_first_and_last_list(const Output_data_list* pdl,
4433
                           const Output_data** pfirst,
4434
                           const Output_data** plast) const;
4435
 
4436
  // Write the section headers in the list into V.
4437
  template<int size, bool big_endian>
4438
  unsigned char*
4439
  write_section_headers_list(const Layout*, const Stringpool*,
4440
                             const Output_data_list*, unsigned char* v,
4441
                             unsigned int* pshdx) const;
4442
 
4443
  // Print a section list to the mapfile.
4444
  void
4445
  print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4446
 
4447
  // NOTE: We want to use the copy constructor.  Currently, shallow copy
4448
  // works for us so we do not need to write our own copy constructor.
4449
 
4450
  // The list of output data attached to this segment.
4451
  Output_data_list output_lists_[ORDER_MAX];
4452
  // The segment virtual address.
4453
  uint64_t vaddr_;
4454
  // The segment physical address.
4455
  uint64_t paddr_;
4456
  // The size of the segment in memory.
4457
  uint64_t memsz_;
4458
  // The maximum section alignment.  The is_max_align_known_ field
4459
  // indicates whether this has been finalized.
4460
  uint64_t max_align_;
4461
  // The required minimum value for the p_align field.  This is used
4462
  // for PT_LOAD segments.  Note that this does not mean that
4463
  // addresses should be aligned to this value; it means the p_paddr
4464
  // and p_vaddr fields must be congruent modulo this value.  For
4465
  // non-PT_LOAD segments, the dynamic linker works more efficiently
4466
  // if the p_align field has the more conventional value, although it
4467
  // can align as needed.
4468
  uint64_t min_p_align_;
4469
  // The offset of the segment data within the file.
4470
  off_t offset_;
4471
  // The size of the segment data in the file.
4472
  off_t filesz_;
4473
  // The segment type;
4474
  elfcpp::Elf_Word type_;
4475
  // The segment flags.
4476
  elfcpp::Elf_Word flags_;
4477
  // Whether we have finalized max_align_.
4478
  bool is_max_align_known_ : 1;
4479
  // Whether vaddr and paddr were set by a linker script.
4480
  bool are_addresses_set_ : 1;
4481
  // Whether this segment holds large data sections.
4482
  bool is_large_data_segment_ : 1;
4483
};
4484
 
4485
// This class represents the output file.
4486
 
4487
class Output_file
4488
{
4489
 public:
4490
  Output_file(const char* name);
4491
 
4492
  // Indicate that this is a temporary file which should not be
4493
  // output.
4494
  void
4495
  set_is_temporary()
4496
  { this->is_temporary_ = true; }
4497
 
4498
  // Try to open an existing file. Returns false if the file doesn't
4499
  // exist, has a size of 0 or can't be mmaped.  This method is
4500
  // thread-unsafe.  If BASE_NAME is not NULL, use the contents of
4501
  // that file as the base for incremental linking.
4502
  bool
4503
  open_base_file(const char* base_name, bool writable);
4504
 
4505
  // Open the output file.  FILE_SIZE is the final size of the file.
4506
  // If the file already exists, it is deleted/truncated.  This method
4507
  // is thread-unsafe.
4508
  void
4509
  open(off_t file_size);
4510
 
4511
  // Resize the output file.  This method is thread-unsafe.
4512
  void
4513
  resize(off_t file_size);
4514
 
4515
  // Close the output file (flushing all buffered data) and make sure
4516
  // there are no errors.  This method is thread-unsafe.
4517
  void
4518
  close();
4519
 
4520
  // Return the size of this file.
4521
  off_t
4522
  filesize()
4523
  { return this->file_size_; }
4524
 
4525
  // Return the name of this file.
4526
  const char*
4527
  filename()
4528
  { return this->name_; }
4529
 
4530
  // We currently always use mmap which makes the view handling quite
4531
  // simple.  In the future we may support other approaches.
4532
 
4533
  // Write data to the output file.
4534
  void
4535
  write(off_t offset, const void* data, size_t len)
4536
  { memcpy(this->base_ + offset, data, len); }
4537
 
4538
  // Get a buffer to use to write to the file, given the offset into
4539
  // the file and the size.
4540
  unsigned char*
4541
  get_output_view(off_t start, size_t size)
4542
  {
4543
    gold_assert(start >= 0
4544
                && start + static_cast<off_t>(size) <= this->file_size_);
4545
    return this->base_ + start;
4546
  }
4547
 
4548
  // VIEW must have been returned by get_output_view.  Write the
4549
  // buffer to the file, passing in the offset and the size.
4550
  void
4551
  write_output_view(off_t, size_t, unsigned char*)
4552
  { }
4553
 
4554
  // Get a read/write buffer.  This is used when we want to write part
4555
  // of the file, read it in, and write it again.
4556
  unsigned char*
4557
  get_input_output_view(off_t start, size_t size)
4558
  { return this->get_output_view(start, size); }
4559
 
4560
  // Write a read/write buffer back to the file.
4561
  void
4562
  write_input_output_view(off_t, size_t, unsigned char*)
4563
  { }
4564
 
4565
  // Get a read buffer.  This is used when we just want to read part
4566
  // of the file back it in.
4567
  const unsigned char*
4568
  get_input_view(off_t start, size_t size)
4569
  { return this->get_output_view(start, size); }
4570
 
4571
  // Release a read bfufer.
4572
  void
4573
  free_input_view(off_t, size_t, const unsigned char*)
4574
  { }
4575
 
4576
 private:
4577
  // Map the file into memory or, if that fails, allocate anonymous
4578
  // memory.
4579
  void
4580
  map();
4581
 
4582
  // Allocate anonymous memory for the file.
4583
  bool
4584
  map_anonymous();
4585
 
4586
  // Map the file into memory.
4587
  bool
4588
  map_no_anonymous(bool);
4589
 
4590
  // Unmap the file from memory (and flush to disk buffers).
4591
  void
4592
  unmap();
4593
 
4594
  // File name.
4595
  const char* name_;
4596
  // File descriptor.
4597
  int o_;
4598
  // File size.
4599
  off_t file_size_;
4600
  // Base of file mapped into memory.
4601
  unsigned char* base_;
4602
  // True iff base_ points to a memory buffer rather than an output file.
4603
  bool map_is_anonymous_;
4604
  // True if base_ was allocated using new rather than mmap.
4605
  bool map_is_allocated_;
4606
  // True if this is a temporary file which should not be output.
4607
  bool is_temporary_;
4608
};
4609
 
4610
} // End namespace gold.
4611
 
4612
#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.