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 163

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
 protected:
1401
  // Note that we've added another relative reloc.
1402
  void
1403
  bump_relative_reloc_count()
1404
  { ++this->relative_reloc_count_; }
1405
 
1406
 private:
1407
  // The number of relative relocs added to this section.  This is to
1408
  // support DT_RELCOUNT.
1409
  size_t relative_reloc_count_;
1410
  // Whether to sort the relocations when writing them out, to make
1411
  // the dynamic linker more efficient.
1412
  bool sort_relocs_;
1413
};
1414
 
1415
// Output_data_reloc is used to manage a section containing relocs.
1416
// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA.  DYNAMIC
1417
// indicates whether this is a dynamic relocation or a normal
1418
// relocation.  Output_data_reloc_base is a base class.
1419
// Output_data_reloc is the real class, which we specialize based on
1420
// the reloc type.
1421
 
1422
template<int sh_type, bool dynamic, int size, bool big_endian>
1423
class Output_data_reloc_base : public Output_data_reloc_generic
1424
{
1425
 public:
1426
  typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1427
  typedef typename Output_reloc_type::Address Address;
1428
  static const int reloc_size =
1429
    Reloc_types<sh_type, size, big_endian>::reloc_size;
1430
 
1431
  // Construct the section.
1432
  Output_data_reloc_base(bool sort_relocs)
1433
    : Output_data_reloc_generic(size, sort_relocs)
1434
  { }
1435
 
1436
 protected:
1437
  // Write out the data.
1438
  void
1439
  do_write(Output_file*);
1440
 
1441
  // Set the entry size and the link.
1442
  void
1443
  do_adjust_output_section(Output_section* os);
1444
 
1445
  // Write to a map file.
1446
  void
1447
  do_print_to_mapfile(Mapfile* mapfile) const
1448
  {
1449
    mapfile->print_output_data(this,
1450
                               (dynamic
1451
                                ? _("** dynamic relocs")
1452
                                : _("** relocs")));
1453
  }
1454
 
1455
  // Add a relocation entry.
1456
  void
1457
  add(Output_data* od, const Output_reloc_type& reloc)
1458
  {
1459
    this->relocs_.push_back(reloc);
1460
    this->set_current_data_size(this->relocs_.size() * reloc_size);
1461
    od->add_dynamic_reloc();
1462
    if (reloc.is_relative())
1463
      this->bump_relative_reloc_count();
1464
    Sized_relobj<size, big_endian>* relobj = reloc.get_relobj();
1465
    if (relobj != NULL)
1466
      relobj->add_dyn_reloc(this->relocs_.size() - 1);
1467
  }
1468
 
1469
 private:
1470
  typedef std::vector<Output_reloc_type> Relocs;
1471
 
1472
  // The class used to sort the relocations.
1473
  struct Sort_relocs_comparison
1474
  {
1475
    bool
1476
    operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1477
    { return r1.sort_before(r2); }
1478
  };
1479
 
1480
  // The relocations in this section.
1481
  Relocs relocs_;
1482
};
1483
 
1484
// The class which callers actually create.
1485
 
1486
template<int sh_type, bool dynamic, int size, bool big_endian>
1487
class Output_data_reloc;
1488
 
1489
// The SHT_REL version of Output_data_reloc.
1490
 
1491
template<bool dynamic, int size, bool big_endian>
1492
class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1493
  : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1494
{
1495
 private:
1496
  typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1497
                                 big_endian> Base;
1498
 
1499
 public:
1500
  typedef typename Base::Output_reloc_type Output_reloc_type;
1501
  typedef typename Output_reloc_type::Address Address;
1502
 
1503
  Output_data_reloc(bool sr)
1504
    : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
1505
  { }
1506
 
1507
  // Add a reloc against a global symbol.
1508
 
1509
  void
1510
  add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
1511
  { this->add(od, Output_reloc_type(gsym, type, od, address, false, false)); }
1512
 
1513
  void
1514
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1515
             Sized_relobj<size, big_endian>* relobj,
1516
             unsigned int shndx, Address address)
1517
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1518
                                    false, false)); }
1519
 
1520
  // These are to simplify the Copy_relocs class.
1521
 
1522
  void
1523
  add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1524
             Address addend)
1525
  {
1526
    gold_assert(addend == 0);
1527
    this->add_global(gsym, type, od, address);
1528
  }
1529
 
1530
  void
1531
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1532
             Sized_relobj<size, big_endian>* relobj,
1533
             unsigned int shndx, Address address, Address addend)
1534
  {
1535
    gold_assert(addend == 0);
1536
    this->add_global(gsym, type, od, relobj, shndx, address);
1537
  }
1538
 
1539
  // Add a RELATIVE reloc against a global symbol.  The final relocation
1540
  // will not reference the symbol.
1541
 
1542
  void
1543
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1544
                      Address address)
1545
  { this->add(od, Output_reloc_type(gsym, type, od, address, true, true)); }
1546
 
1547
  void
1548
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1549
                      Sized_relobj<size, big_endian>* relobj,
1550
                      unsigned int shndx, Address address)
1551
  {
1552
    this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1553
                                    true, true));
1554
  }
1555
 
1556
  // Add a global relocation which does not use a symbol for the relocation,
1557
  // but which gets its addend from a symbol.
1558
 
1559
  void
1560
  add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1561
                               Output_data* od, Address address)
1562
  { this->add(od, Output_reloc_type(gsym, type, od, address, false, true)); }
1563
 
1564
  void
1565
  add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1566
                               Output_data* od,
1567
                               Sized_relobj<size, big_endian>* relobj,
1568
                               unsigned int shndx, Address address)
1569
  {
1570
    this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1571
                                    false, true));
1572
  }
1573
 
1574
  // Add a reloc against a local symbol.
1575
 
1576
  void
1577
  add_local(Sized_relobj<size, big_endian>* relobj,
1578
            unsigned int local_sym_index, unsigned int type,
1579
            Output_data* od, Address address)
1580
  {
1581
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1582 163 khays
                                    address, false, false, false, false));
1583 27 khays
  }
1584
 
1585
  void
1586
  add_local(Sized_relobj<size, big_endian>* relobj,
1587
            unsigned int local_sym_index, unsigned int type,
1588
            Output_data* od, unsigned int shndx, Address address)
1589
  {
1590
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1591 163 khays
                                    address, false, false, false, false));
1592 27 khays
  }
1593
 
1594
  // Add a RELATIVE reloc against a local symbol.
1595
 
1596
  void
1597
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1598
                     unsigned int local_sym_index, unsigned int type,
1599
                     Output_data* od, Address address)
1600
  {
1601
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1602 163 khays
                                    address, true, true, false, false));
1603 27 khays
  }
1604
 
1605
  void
1606
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1607
                     unsigned int local_sym_index, unsigned int type,
1608
                     Output_data* od, unsigned int shndx, Address address)
1609
  {
1610
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1611 163 khays
                                    address, true, true, false, false));
1612 27 khays
  }
1613
 
1614
  // Add a local relocation which does not use a symbol for the relocation,
1615
  // but which gets its addend from a symbol.
1616
 
1617
  void
1618
  add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1619
                              unsigned int local_sym_index, unsigned int type,
1620
                              Output_data* od, Address address)
1621
  {
1622
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1623 163 khays
                                    address, false, true, false, false));
1624 27 khays
  }
1625
 
1626
  void
1627
  add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1628
                              unsigned int local_sym_index, unsigned int type,
1629
                              Output_data* od, unsigned int shndx,
1630
                              Address address)
1631
  {
1632
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1633 163 khays
                                    address, false, true, false, false));
1634 27 khays
  }
1635
 
1636
  // Add a reloc against a local section symbol.  This will be
1637
  // converted into a reloc against the STT_SECTION symbol of the
1638
  // output section.
1639
 
1640
  void
1641
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1642
                    unsigned int input_shndx, unsigned int type,
1643
                    Output_data* od, Address address)
1644
  {
1645
    this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1646 163 khays
                                    address, false, false, true, false));
1647 27 khays
  }
1648
 
1649
  void
1650
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1651
                    unsigned int input_shndx, unsigned int type,
1652
                    Output_data* od, unsigned int shndx, Address address)
1653
  {
1654
    this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1655 163 khays
                                    address, false, false, true, false));
1656 27 khays
  }
1657
 
1658
  // A reloc against the STT_SECTION symbol of an output section.
1659
  // OS is the Output_section that the relocation refers to; OD is
1660
  // the Output_data object being relocated.
1661
 
1662
  void
1663
  add_output_section(Output_section* os, unsigned int type,
1664
                     Output_data* od, Address address)
1665
  { this->add(od, Output_reloc_type(os, type, od, address)); }
1666
 
1667
  void
1668
  add_output_section(Output_section* os, unsigned int type, Output_data* od,
1669
                     Sized_relobj<size, big_endian>* relobj,
1670
                     unsigned int shndx, Address address)
1671
  { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
1672
 
1673
  // Add an absolute relocation.
1674
 
1675
  void
1676
  add_absolute(unsigned int type, Output_data* od, Address address)
1677
  { this->add(od, Output_reloc_type(type, od, address)); }
1678
 
1679
  void
1680
  add_absolute(unsigned int type, Output_data* od,
1681
               Sized_relobj<size, big_endian>* relobj,
1682
               unsigned int shndx, Address address)
1683
  { this->add(od, Output_reloc_type(type, relobj, shndx, address)); }
1684
 
1685
  // Add a target specific relocation.  A target which calls this must
1686
  // define the reloc_symbol_index and reloc_addend virtual functions.
1687
 
1688
  void
1689
  add_target_specific(unsigned int type, void* arg, Output_data* od,
1690
                      Address address)
1691
  { this->add(od, Output_reloc_type(type, arg, od, address)); }
1692
 
1693
  void
1694
  add_target_specific(unsigned int type, void* arg, Output_data* od,
1695
                      Sized_relobj<size, big_endian>* relobj,
1696
                      unsigned int shndx, Address address)
1697
  { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
1698
};
1699
 
1700
// The SHT_RELA version of Output_data_reloc.
1701
 
1702
template<bool dynamic, int size, bool big_endian>
1703
class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1704
  : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1705
{
1706
 private:
1707
  typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1708
                                 big_endian> Base;
1709
 
1710
 public:
1711
  typedef typename Base::Output_reloc_type Output_reloc_type;
1712
  typedef typename Output_reloc_type::Address Address;
1713
  typedef typename Output_reloc_type::Addend Addend;
1714
 
1715
  Output_data_reloc(bool sr)
1716
    : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
1717
  { }
1718
 
1719
  // Add a reloc against a global symbol.
1720
 
1721
  void
1722
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1723
             Address address, Addend addend)
1724
  { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1725
                                    false, false)); }
1726
 
1727
  void
1728
  add_global(Symbol* gsym, unsigned int type, Output_data* od,
1729
             Sized_relobj<size, big_endian>* relobj,
1730
             unsigned int shndx, Address address,
1731
             Addend addend)
1732
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1733
                                    addend, false, false)); }
1734
 
1735
  // Add a RELATIVE reloc against a global symbol.  The final output
1736
  // relocation will not reference the symbol, but we must keep the symbol
1737
  // information long enough to set the addend of the relocation correctly
1738
  // when it is written.
1739
 
1740
  void
1741
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1742
                      Address address, Addend addend)
1743
  { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1744
                                    true)); }
1745
 
1746
  void
1747
  add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1748
                      Sized_relobj<size, big_endian>* relobj,
1749
                      unsigned int shndx, Address address, Addend addend)
1750
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1751
                                    addend, true, true)); }
1752
 
1753
  // Add a global relocation which does not use a symbol for the relocation,
1754
  // but which gets its addend from a symbol.
1755
 
1756
  void
1757
  add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1758
                               Address address, Addend addend)
1759
  { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1760
                                    false, true)); }
1761
 
1762
  void
1763
  add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1764
                               Output_data* od,
1765
                               Sized_relobj<size, big_endian>* relobj,
1766
                               unsigned int shndx, Address address, Addend addend)
1767
  { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1768
                                    addend, false, true)); }
1769
 
1770
  // Add a reloc against a local symbol.
1771
 
1772
  void
1773
  add_local(Sized_relobj<size, big_endian>* relobj,
1774
            unsigned int local_sym_index, unsigned int type,
1775
            Output_data* od, Address address, Addend addend)
1776
  {
1777
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1778 163 khays
                                    addend, false, false, false, false));
1779 27 khays
  }
1780
 
1781
  void
1782
  add_local(Sized_relobj<size, big_endian>* relobj,
1783
            unsigned int local_sym_index, unsigned int type,
1784
            Output_data* od, unsigned int shndx, Address address,
1785
            Addend addend)
1786
  {
1787
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1788 163 khays
                                    address, addend, false, false, false,
1789
                                    false));
1790 27 khays
  }
1791
 
1792
  // Add a RELATIVE reloc against a local symbol.
1793
 
1794
  void
1795
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1796
                     unsigned int local_sym_index, unsigned int type,
1797 163 khays
                     Output_data* od, Address address, Addend addend,
1798
                     bool use_plt_offset)
1799 27 khays
  {
1800
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1801 163 khays
                                    addend, true, true, false,
1802
                                    use_plt_offset));
1803 27 khays
  }
1804
 
1805
  void
1806
  add_local_relative(Sized_relobj<size, big_endian>* relobj,
1807
                     unsigned int local_sym_index, unsigned int type,
1808
                     Output_data* od, unsigned int shndx, Address address,
1809 163 khays
                     Addend addend, bool use_plt_offset)
1810 27 khays
  {
1811
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1812 163 khays
                                    address, addend, true, true, false,
1813
                                    use_plt_offset));
1814 27 khays
  }
1815
 
1816
  // Add a local relocation which does not use a symbol for the relocation,
1817
  // but which gets it's addend from a symbol.
1818
 
1819
  void
1820
  add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1821
                              unsigned int local_sym_index, unsigned int type,
1822
                              Output_data* od, Address address, Addend addend)
1823
  {
1824
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1825 163 khays
                                    addend, false, true, false, false));
1826 27 khays
  }
1827
 
1828
  void
1829
  add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1830
                              unsigned int local_sym_index, unsigned int type,
1831
                              Output_data* od, unsigned int shndx,
1832
                              Address address, Addend addend)
1833
  {
1834
    this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1835 163 khays
                                    address, addend, false, true, false,
1836
                                    false));
1837 27 khays
  }
1838
 
1839
  // Add a reloc against a local section symbol.  This will be
1840
  // converted into a reloc against the STT_SECTION symbol of the
1841
  // output section.
1842
 
1843
  void
1844
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1845
                    unsigned int input_shndx, unsigned int type,
1846
                    Output_data* od, Address address, Addend addend)
1847
  {
1848
    this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1849 163 khays
                                    addend, false, false, true, false));
1850 27 khays
  }
1851
 
1852
  void
1853
  add_local_section(Sized_relobj<size, big_endian>* relobj,
1854
                    unsigned int input_shndx, unsigned int type,
1855
                    Output_data* od, unsigned int shndx, Address address,
1856
                    Addend addend)
1857
  {
1858
    this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1859 163 khays
                                    address, addend, false, false, true,
1860
                                    false));
1861 27 khays
  }
1862
 
1863
  // A reloc against the STT_SECTION symbol of an output section.
1864
 
1865
  void
1866
  add_output_section(Output_section* os, unsigned int type, Output_data* od,
1867
                     Address address, Addend addend)
1868
  { this->add(od, Output_reloc_type(os, type, od, address, addend)); }
1869
 
1870
  void
1871
  add_output_section(Output_section* os, unsigned int type, Output_data* od,
1872
                     Sized_relobj<size, big_endian>* relobj,
1873
                     unsigned int shndx, Address address, Addend addend)
1874
  { this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
1875
                                    addend)); }
1876
 
1877
  // Add an absolute relocation.
1878
 
1879
  void
1880
  add_absolute(unsigned int type, Output_data* od, Address address,
1881
               Addend addend)
1882
  { this->add(od, Output_reloc_type(type, od, address, addend)); }
1883
 
1884
  void
1885
  add_absolute(unsigned int type, Output_data* od,
1886
               Sized_relobj<size, big_endian>* relobj,
1887
               unsigned int shndx, Address address, Addend addend)
1888
  { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); }
1889
 
1890
  // Add a target specific relocation.  A target which calls this must
1891
  // define the reloc_symbol_index and reloc_addend virtual functions.
1892
 
1893
  void
1894
  add_target_specific(unsigned int type, void* arg, Output_data* od,
1895
                      Address address, Addend addend)
1896
  { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
1897
 
1898
  void
1899
  add_target_specific(unsigned int type, void* arg, Output_data* od,
1900
                      Sized_relobj<size, big_endian>* relobj,
1901
                      unsigned int shndx, Address address, Addend addend)
1902
  {
1903
    this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
1904
                                    addend));
1905
  }
1906
};
1907
 
1908
// Output_relocatable_relocs represents a relocation section in a
1909
// relocatable link.  The actual data is written out in the target
1910
// hook relocate_for_relocatable.  This just saves space for it.
1911
 
1912
template<int sh_type, int size, bool big_endian>
1913
class Output_relocatable_relocs : public Output_section_data
1914
{
1915
 public:
1916
  Output_relocatable_relocs(Relocatable_relocs* rr)
1917
    : Output_section_data(Output_data::default_alignment_for_size(size)),
1918
      rr_(rr)
1919
  { }
1920
 
1921
  void
1922
  set_final_data_size();
1923
 
1924
  // Write out the data.  There is nothing to do here.
1925
  void
1926
  do_write(Output_file*)
1927
  { }
1928
 
1929
  // Write to a map file.
1930
  void
1931
  do_print_to_mapfile(Mapfile* mapfile) const
1932
  { mapfile->print_output_data(this, _("** relocs")); }
1933
 
1934
 private:
1935
  // The relocs associated with this input section.
1936
  Relocatable_relocs* rr_;
1937
};
1938
 
1939
// Handle a GROUP section.
1940
 
1941
template<int size, bool big_endian>
1942
class Output_data_group : public Output_section_data
1943
{
1944
 public:
1945
  // The constructor clears *INPUT_SHNDXES.
1946
  Output_data_group(Sized_relobj_file<size, big_endian>* relobj,
1947
                    section_size_type entry_count,
1948
                    elfcpp::Elf_Word flags,
1949
                    std::vector<unsigned int>* input_shndxes);
1950
 
1951
  void
1952
  do_write(Output_file*);
1953
 
1954
  // Write to a map file.
1955
  void
1956
  do_print_to_mapfile(Mapfile* mapfile) const
1957
  { mapfile->print_output_data(this, _("** group")); }
1958
 
1959
  // Set final data size.
1960
  void
1961
  set_final_data_size()
1962
  { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1963
 
1964
 private:
1965
  // The input object.
1966
  Sized_relobj_file<size, big_endian>* relobj_;
1967
  // The group flag word.
1968
  elfcpp::Elf_Word flags_;
1969
  // The section indexes of the input sections in this group.
1970
  std::vector<unsigned int> input_shndxes_;
1971
};
1972
 
1973
// Output_data_got is used to manage a GOT.  Each entry in the GOT is
1974
// for one symbol--either a global symbol or a local symbol in an
1975
// object.  The target specific code adds entries to the GOT as
1976
// needed.
1977
 
1978
template<int size, bool big_endian>
1979
class Output_data_got : public Output_section_data_build
1980
{
1981
 public:
1982
  typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1983
  typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1984
  typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1985
 
1986
  Output_data_got()
1987
    : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1988
      entries_(), free_list_()
1989
  { }
1990
 
1991
  Output_data_got(off_t data_size)
1992
    : Output_section_data_build(data_size,
1993
                                Output_data::default_alignment_for_size(size)),
1994
      entries_(), free_list_()
1995
  {
1996
    // For an incremental update, we have an existing GOT section.
1997
    // Initialize the list of entries and the free list.
1998
    this->entries_.resize(data_size / (size / 8));
1999
    this->free_list_.init(data_size, false);
2000
  }
2001
 
2002
  // Add an entry for a global symbol to the GOT.  Return true if this
2003
  // is a new GOT entry, false if the symbol was already in the GOT.
2004
  bool
2005
  add_global(Symbol* gsym, unsigned int got_type);
2006
 
2007
  // Like add_global, but use the PLT offset of the global symbol if
2008
  // it has one.
2009
  bool
2010
  add_global_plt(Symbol* gsym, unsigned int got_type);
2011
 
2012
  // Add an entry for a global symbol to the GOT, and add a dynamic
2013
  // relocation of type R_TYPE for the GOT entry.
2014
  void
2015
  add_global_with_rel(Symbol* gsym, unsigned int got_type,
2016
                      Rel_dyn* rel_dyn, unsigned int r_type);
2017
 
2018
  void
2019
  add_global_with_rela(Symbol* gsym, unsigned int got_type,
2020
                       Rela_dyn* rela_dyn, unsigned int r_type);
2021
 
2022
  // Add a pair of entries for a global symbol to the GOT, and add
2023
  // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2024
  void
2025
  add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2026
                           Rel_dyn* rel_dyn, unsigned int r_type_1,
2027
                           unsigned int r_type_2);
2028
 
2029
  void
2030
  add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
2031
                            Rela_dyn* rela_dyn, unsigned int r_type_1,
2032
                            unsigned int r_type_2);
2033
 
2034
  // Add an entry for a local symbol to the GOT.  This returns true if
2035
  // this is a new GOT entry, false if the symbol already has a GOT
2036
  // entry.
2037
  bool
2038
  add_local(Sized_relobj_file<size, big_endian>* object, unsigned int sym_index,
2039
            unsigned int got_type);
2040
 
2041
  // Like add_local, but use the PLT offset of the local symbol if it
2042
  // has one.
2043
  bool
2044
  add_local_plt(Sized_relobj_file<size, big_endian>* object,
2045
                unsigned int sym_index,
2046
                unsigned int got_type);
2047
 
2048
  // Add an entry for a local symbol to the GOT, and add a dynamic
2049
  // relocation of type R_TYPE for the GOT entry.
2050
  void
2051
  add_local_with_rel(Sized_relobj_file<size, big_endian>* object,
2052
                     unsigned int sym_index, unsigned int got_type,
2053
                     Rel_dyn* rel_dyn, unsigned int r_type);
2054
 
2055
  void
2056
  add_local_with_rela(Sized_relobj_file<size, big_endian>* object,
2057
                      unsigned int sym_index, unsigned int got_type,
2058
                      Rela_dyn* rela_dyn, unsigned int r_type);
2059
 
2060
  // Add a pair of entries for a local symbol to the GOT, and add
2061
  // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
2062
  void
2063
  add_local_pair_with_rel(Sized_relobj_file<size, big_endian>* object,
2064
                          unsigned int sym_index, unsigned int shndx,
2065
                          unsigned int got_type, Rel_dyn* rel_dyn,
2066
                          unsigned int r_type_1, unsigned int r_type_2);
2067
 
2068
  void
2069
  add_local_pair_with_rela(Sized_relobj_file<size, big_endian>* object,
2070
                          unsigned int sym_index, unsigned int shndx,
2071
                          unsigned int got_type, Rela_dyn* rela_dyn,
2072
                          unsigned int r_type_1, unsigned int r_type_2);
2073
 
2074
  // Add a constant to the GOT.  This returns the offset of the new
2075
  // entry from the start of the GOT.
2076
  unsigned int
2077
  add_constant(Valtype constant)
2078
  {
2079
    unsigned int got_offset = this->add_got_entry(Got_entry(constant));
2080
    return got_offset;
2081
  }
2082
 
2083
  // Reserve a slot in the GOT.
2084
  void
2085
  reserve_slot(unsigned int i)
2086
  { this->free_list_.remove(i * size / 8, (i + 1) * size / 8); }
2087
 
2088
  // Reserve a slot in the GOT for a local symbol.
2089
  void
2090
  reserve_local(unsigned int i, Sized_relobj<size, big_endian>* object,
2091
                unsigned int sym_index, unsigned int got_type);
2092
 
2093
  // Reserve a slot in the GOT for a global symbol.
2094
  void
2095
  reserve_global(unsigned int i, Symbol* gsym, unsigned int got_type);
2096
 
2097
 protected:
2098
  // Write out the GOT table.
2099
  void
2100
  do_write(Output_file*);
2101
 
2102
  // Write to a map file.
2103
  void
2104
  do_print_to_mapfile(Mapfile* mapfile) const
2105
  { mapfile->print_output_data(this, _("** GOT")); }
2106
 
2107
 private:
2108
  // This POD class holds a single GOT entry.
2109
  class Got_entry
2110
  {
2111
   public:
2112
    // Create a zero entry.
2113
    Got_entry()
2114
      : local_sym_index_(RESERVED_CODE), use_plt_offset_(false)
2115
    { this->u_.constant = 0; }
2116
 
2117
    // Create a global symbol entry.
2118
    Got_entry(Symbol* gsym, bool use_plt_offset)
2119
      : local_sym_index_(GSYM_CODE), use_plt_offset_(use_plt_offset)
2120
    { this->u_.gsym = gsym; }
2121
 
2122
    // Create a local symbol entry.
2123
    Got_entry(Sized_relobj_file<size, big_endian>* object,
2124
              unsigned int local_sym_index, bool use_plt_offset)
2125
      : local_sym_index_(local_sym_index), use_plt_offset_(use_plt_offset)
2126
    {
2127
      gold_assert(local_sym_index != GSYM_CODE
2128
                  && local_sym_index != CONSTANT_CODE
2129
                  && local_sym_index != RESERVED_CODE
2130
                  && local_sym_index == this->local_sym_index_);
2131
      this->u_.object = object;
2132
    }
2133
 
2134
    // Create a constant entry.  The constant is a host value--it will
2135
    // be swapped, if necessary, when it is written out.
2136
    explicit Got_entry(Valtype constant)
2137
      : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
2138
    { this->u_.constant = constant; }
2139
 
2140
    // Write the GOT entry to an output view.
2141
    void
2142
    write(unsigned char* pov) const;
2143
 
2144
   private:
2145
    enum
2146
    {
2147
      GSYM_CODE = 0x7fffffff,
2148
      CONSTANT_CODE = 0x7ffffffe,
2149
      RESERVED_CODE = 0x7ffffffd
2150
    };
2151
 
2152
    union
2153
    {
2154
      // For a local symbol, the object.
2155
      Sized_relobj_file<size, big_endian>* object;
2156
      // For a global symbol, the symbol.
2157
      Symbol* gsym;
2158
      // For a constant, the constant.
2159
      Valtype constant;
2160
    } u_;
2161
    // For a local symbol, the local symbol index.  This is GSYM_CODE
2162
    // for a global symbol, or CONSTANT_CODE for a constant.
2163
    unsigned int local_sym_index_ : 31;
2164
    // Whether to use the PLT offset of the symbol if it has one.
2165
    bool use_plt_offset_ : 1;
2166
  };
2167
 
2168
  typedef std::vector<Got_entry> Got_entries;
2169
 
2170
  // Create a new GOT entry and return its offset.
2171
  unsigned int
2172
  add_got_entry(Got_entry got_entry);
2173
 
2174
  // Create a pair of new GOT entries and return the offset of the first.
2175
  unsigned int
2176
  add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2);
2177
 
2178
  // Return the offset into the GOT of GOT entry I.
2179
  unsigned int
2180
  got_offset(unsigned int i) const
2181
  { return i * (size / 8); }
2182
 
2183
  // Return the offset into the GOT of the last entry added.
2184
  unsigned int
2185
  last_got_offset() const
2186
  { return this->got_offset(this->entries_.size() - 1); }
2187
 
2188
  // Set the size of the section.
2189
  void
2190
  set_got_size()
2191
  { this->set_current_data_size(this->got_offset(this->entries_.size())); }
2192
 
2193
  // The list of GOT entries.
2194
  Got_entries entries_;
2195
 
2196
  // List of available regions within the section, for incremental
2197
  // update links.
2198
  Free_list free_list_;
2199
};
2200
 
2201
// Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2202
// section.
2203
 
2204
class Output_data_dynamic : public Output_section_data
2205
{
2206
 public:
2207
  Output_data_dynamic(Stringpool* pool)
2208
    : Output_section_data(Output_data::default_alignment()),
2209
      entries_(), pool_(pool)
2210
  { }
2211
 
2212
  // Add a new dynamic entry with a fixed numeric value.
2213
  void
2214
  add_constant(elfcpp::DT tag, unsigned int val)
2215
  { this->add_entry(Dynamic_entry(tag, val)); }
2216
 
2217
  // Add a new dynamic entry with the address of output data.
2218
  void
2219
  add_section_address(elfcpp::DT tag, const Output_data* od)
2220
  { this->add_entry(Dynamic_entry(tag, od, false)); }
2221
 
2222
  // Add a new dynamic entry with the address of output data
2223
  // plus a constant offset.
2224
  void
2225
  add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2226
                          unsigned int offset)
2227
  { this->add_entry(Dynamic_entry(tag, od, offset)); }
2228
 
2229
  // Add a new dynamic entry with the size of output data.
2230
  void
2231
  add_section_size(elfcpp::DT tag, const Output_data* od)
2232
  { this->add_entry(Dynamic_entry(tag, od, true)); }
2233
 
2234
  // Add a new dynamic entry with the total size of two output datas.
2235
  void
2236
  add_section_size(elfcpp::DT tag, const Output_data* od,
2237
                   const Output_data* od2)
2238
  { this->add_entry(Dynamic_entry(tag, od, od2)); }
2239
 
2240
  // Add a new dynamic entry with the address of a symbol.
2241
  void
2242
  add_symbol(elfcpp::DT tag, const Symbol* sym)
2243
  { this->add_entry(Dynamic_entry(tag, sym)); }
2244
 
2245
  // Add a new dynamic entry with a string.
2246
  void
2247
  add_string(elfcpp::DT tag, const char* str)
2248
  { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
2249
 
2250
  void
2251
  add_string(elfcpp::DT tag, const std::string& str)
2252
  { this->add_string(tag, str.c_str()); }
2253
 
2254
 protected:
2255
  // Adjust the output section to set the entry size.
2256
  void
2257
  do_adjust_output_section(Output_section*);
2258
 
2259
  // Set the final data size.
2260
  void
2261
  set_final_data_size();
2262
 
2263
  // Write out the dynamic entries.
2264
  void
2265
  do_write(Output_file*);
2266
 
2267
  // Write to a map file.
2268
  void
2269
  do_print_to_mapfile(Mapfile* mapfile) const
2270
  { mapfile->print_output_data(this, _("** dynamic")); }
2271
 
2272
 private:
2273
  // This POD class holds a single dynamic entry.
2274
  class Dynamic_entry
2275
  {
2276
   public:
2277
    // Create an entry with a fixed numeric value.
2278
    Dynamic_entry(elfcpp::DT tag, unsigned int val)
2279
      : tag_(tag), offset_(DYNAMIC_NUMBER)
2280
    { this->u_.val = val; }
2281
 
2282
    // Create an entry with the size or address of a section.
2283
    Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2284
      : tag_(tag),
2285
        offset_(section_size
2286
                ? DYNAMIC_SECTION_SIZE
2287
                : DYNAMIC_SECTION_ADDRESS)
2288
    {
2289
      this->u_.od = od;
2290
      this->od2 = NULL;
2291
    }
2292
 
2293
    // Create an entry with the size of two sections.
2294
    Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2295
      : tag_(tag),
2296
        offset_(DYNAMIC_SECTION_SIZE)
2297
    {
2298
      this->u_.od = od;
2299
      this->od2 = od2;
2300
    }
2301
 
2302
    // Create an entry with the address of a section plus a constant offset.
2303
    Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2304
      : tag_(tag),
2305
        offset_(offset)
2306
    { this->u_.od = od; }
2307
 
2308
    // Create an entry with the address of a symbol.
2309
    Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2310
      : tag_(tag), offset_(DYNAMIC_SYMBOL)
2311
    { this->u_.sym = sym; }
2312
 
2313
    // Create an entry with a string.
2314
    Dynamic_entry(elfcpp::DT tag, const char* str)
2315
      : tag_(tag), offset_(DYNAMIC_STRING)
2316
    { this->u_.str = str; }
2317
 
2318
    // Return the tag of this entry.
2319
    elfcpp::DT
2320
    tag() const
2321
    { return this->tag_; }
2322
 
2323
    // Write the dynamic entry to an output view.
2324
    template<int size, bool big_endian>
2325
    void
2326
    write(unsigned char* pov, const Stringpool*) const;
2327
 
2328
   private:
2329
    // Classification is encoded in the OFFSET field.
2330
    enum Classification
2331
    {
2332
      // Section address.
2333
      DYNAMIC_SECTION_ADDRESS = 0,
2334
      // Number.
2335
      DYNAMIC_NUMBER = -1U,
2336
      // Section size.
2337
      DYNAMIC_SECTION_SIZE = -2U,
2338
      // Symbol adress.
2339
      DYNAMIC_SYMBOL = -3U,
2340
      // String.
2341
      DYNAMIC_STRING = -4U
2342
      // Any other value indicates a section address plus OFFSET.
2343
    };
2344
 
2345
    union
2346
    {
2347
      // For DYNAMIC_NUMBER.
2348
      unsigned int val;
2349
      // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
2350
      const Output_data* od;
2351
      // For DYNAMIC_SYMBOL.
2352
      const Symbol* sym;
2353
      // For DYNAMIC_STRING.
2354
      const char* str;
2355
    } u_;
2356
    // For DYNAMIC_SYMBOL with two sections.
2357
    const Output_data* od2;
2358
    // The dynamic tag.
2359
    elfcpp::DT tag_;
2360
    // The type of entry (Classification) or offset within a section.
2361
    unsigned int offset_;
2362
  };
2363
 
2364
  // Add an entry to the list.
2365
  void
2366
  add_entry(const Dynamic_entry& entry)
2367
  { this->entries_.push_back(entry); }
2368
 
2369
  // Sized version of write function.
2370
  template<int size, bool big_endian>
2371
  void
2372
  sized_write(Output_file* of);
2373
 
2374
  // The type of the list of entries.
2375
  typedef std::vector<Dynamic_entry> Dynamic_entries;
2376
 
2377
  // The entries.
2378
  Dynamic_entries entries_;
2379
  // The pool used for strings.
2380
  Stringpool* pool_;
2381
};
2382
 
2383
// Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2384
// which may be required if the object file has more than
2385
// SHN_LORESERVE sections.
2386
 
2387
class Output_symtab_xindex : public Output_section_data
2388
{
2389
 public:
2390
  Output_symtab_xindex(size_t symcount)
2391
    : Output_section_data(symcount * 4, 4, true),
2392
      entries_()
2393
  { }
2394
 
2395
  // Add an entry: symbol number SYMNDX has section SHNDX.
2396
  void
2397
  add(unsigned int symndx, unsigned int shndx)
2398
  { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2399
 
2400
 protected:
2401
  void
2402
  do_write(Output_file*);
2403
 
2404
  // Write to a map file.
2405
  void
2406
  do_print_to_mapfile(Mapfile* mapfile) const
2407
  { mapfile->print_output_data(this, _("** symtab xindex")); }
2408
 
2409
 private:
2410
  template<bool big_endian>
2411
  void
2412
  endian_do_write(unsigned char*);
2413
 
2414
  // It is likely that most symbols will not require entries.  Rather
2415
  // than keep a vector for all symbols, we keep pairs of symbol index
2416
  // and section index.
2417
  typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2418
 
2419
  // The entries we need.
2420
  Xindex_entries entries_;
2421
};
2422
 
2423
// A relaxed input section.
2424
class Output_relaxed_input_section : public Output_section_data_build
2425
{
2426
 public:
2427
  // We would like to call relobj->section_addralign(shndx) to get the
2428
  // alignment but we do not want the constructor to fail.  So callers
2429
  // are repsonsible for ensuring that.
2430
  Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2431
                               uint64_t addralign)
2432
    : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
2433
  { }
2434
 
2435
  // Return the Relobj of this relaxed input section.
2436
  Relobj*
2437
  relobj() const
2438
  { return this->relobj_; }
2439
 
2440
  // Return the section index of this relaxed input section.
2441
  unsigned int
2442
  shndx() const
2443
  { return this->shndx_; }
2444
 
2445
 private:
2446
  Relobj* relobj_;
2447
  unsigned int shndx_;
2448
};
2449
 
2450
// This class describes properties of merge data sections.  It is used
2451
// as a key type for maps.
2452
class Merge_section_properties
2453
{
2454
 public:
2455
  Merge_section_properties(bool is_string, uint64_t entsize,
2456
                             uint64_t addralign)
2457
    : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2458
  { }
2459
 
2460
  // Whether this equals to another Merge_section_properties MSP.
2461
  bool
2462
  eq(const Merge_section_properties& msp) const
2463
  {
2464
    return ((this->is_string_ == msp.is_string_)
2465
            && (this->entsize_ == msp.entsize_)
2466
            && (this->addralign_ == msp.addralign_));
2467
  }
2468
 
2469
  // Compute a hash value for this using 64-bit FNV-1a hash.
2470
  size_t
2471
  hash_value() const
2472
  {
2473
    uint64_t h = 14695981039346656037ULL;       // FNV offset basis.
2474
    uint64_t prime = 1099511628211ULL;
2475
    h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2476
    h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2477
    h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2478
    return h;
2479
  }
2480
 
2481
  // Functors for associative containers.
2482
  struct equal_to
2483
  {
2484
    bool
2485
    operator()(const Merge_section_properties& msp1,
2486
               const Merge_section_properties& msp2) const
2487
    { return msp1.eq(msp2); }
2488
  };
2489
 
2490
  struct hash
2491
  {
2492
    size_t
2493
    operator()(const Merge_section_properties& msp) const
2494
    { return msp.hash_value(); }
2495
  };
2496
 
2497
 private:
2498
  // Whether this merge data section is for strings.
2499
  bool is_string_;
2500
  // Entsize of this merge data section.
2501
  uint64_t entsize_;
2502
  // Address alignment.
2503
  uint64_t addralign_;
2504
};
2505
 
2506
// This class is used to speed up look up of special input sections in an
2507
// Output_section.
2508
 
2509
class Output_section_lookup_maps
2510
{
2511
 public:
2512
  Output_section_lookup_maps()
2513
    : is_valid_(true), merge_sections_by_properties_(),
2514
      merge_sections_by_id_(), relaxed_input_sections_by_id_()
2515
  { }
2516
 
2517
  // Whether the maps are valid.
2518
  bool
2519
  is_valid() const
2520
  { return this->is_valid_; }
2521
 
2522
  // Invalidate the maps.
2523
  void
2524
  invalidate()
2525
  { this->is_valid_ = false; }
2526
 
2527
  // Clear the maps.
2528
  void
2529
  clear()
2530
  {
2531
    this->merge_sections_by_properties_.clear();
2532
    this->merge_sections_by_id_.clear();
2533
    this->relaxed_input_sections_by_id_.clear();
2534
    // A cleared map is valid.
2535
    this->is_valid_ = true;
2536
  }
2537
 
2538
  // Find a merge section by merge section properties.  Return NULL if none
2539
  // is found.
2540
  Output_merge_base*
2541
  find_merge_section(const Merge_section_properties& msp) const
2542
  {
2543
    gold_assert(this->is_valid_);
2544
    Merge_sections_by_properties::const_iterator p =
2545
      this->merge_sections_by_properties_.find(msp);
2546
    return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2547
  }
2548
 
2549
  // Find a merge section by section ID of a merge input section.  Return NULL
2550
  // if none is found.
2551
  Output_merge_base*
2552
  find_merge_section(const Object* object, unsigned int shndx) const
2553
  {
2554
    gold_assert(this->is_valid_);
2555
    Merge_sections_by_id::const_iterator p =
2556
      this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2557
    return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2558
  }
2559
 
2560
  // Add a merge section pointed by POMB with properties MSP.
2561
  void
2562
  add_merge_section(const Merge_section_properties& msp,
2563
                    Output_merge_base* pomb)
2564
  {
2565
    std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2566
    std::pair<Merge_sections_by_properties::iterator, bool> result =
2567
      this->merge_sections_by_properties_.insert(value);
2568
    gold_assert(result.second);
2569
  }
2570
 
2571
  // Add a mapping from a merged input section in OBJECT with index SHNDX
2572
  // to a merge output section pointed by POMB.
2573
  void
2574
  add_merge_input_section(const Object* object, unsigned int shndx,
2575
                          Output_merge_base* pomb)
2576
  {
2577
    Const_section_id csid(object, shndx);
2578
    std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2579
    std::pair<Merge_sections_by_id::iterator, bool> result =
2580
      this->merge_sections_by_id_.insert(value);
2581
    gold_assert(result.second);
2582
  }
2583
 
2584
  // Find a relaxed input section of OBJECT with index SHNDX.
2585
  Output_relaxed_input_section*
2586
  find_relaxed_input_section(const Object* object, unsigned int shndx) const
2587
  {
2588
    gold_assert(this->is_valid_);
2589
    Relaxed_input_sections_by_id::const_iterator p =
2590
      this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2591
    return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2592
  }
2593
 
2594
  // Add a relaxed input section pointed by POMB and whose original input
2595
  // section is in OBJECT with index SHNDX.
2596
  void
2597
  add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2598
                            Output_relaxed_input_section* poris)
2599
  {
2600
    Const_section_id csid(relobj, shndx);
2601
    std::pair<Const_section_id, Output_relaxed_input_section*>
2602
      value(csid, poris);
2603
    std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2604
      this->relaxed_input_sections_by_id_.insert(value);
2605
    gold_assert(result.second);
2606
  }
2607
 
2608
 private:
2609
  typedef Unordered_map<Const_section_id, Output_merge_base*,
2610
                        Const_section_id_hash>
2611
    Merge_sections_by_id;
2612
 
2613
  typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2614
                        Merge_section_properties::hash,
2615
                        Merge_section_properties::equal_to>
2616
    Merge_sections_by_properties;
2617
 
2618
  typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2619
                        Const_section_id_hash>
2620
    Relaxed_input_sections_by_id;
2621
 
2622
  // Whether this is valid
2623
  bool is_valid_;
2624
  // Merge sections by merge section properties.
2625
  Merge_sections_by_properties merge_sections_by_properties_;
2626
  // Merge sections by section IDs.
2627
  Merge_sections_by_id merge_sections_by_id_;
2628
  // Relaxed sections by section IDs.
2629
  Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2630
};
2631
 
2632 159 khays
// This abstract base class defines the interface for the
2633
// types of methods used to fill free space left in an output
2634
// section during an incremental link.  These methods are used
2635
// to insert dummy compilation units into debug info so that
2636
// debug info consumers can scan the debug info serially.
2637
 
2638
class Output_fill
2639
{
2640
 public:
2641
  Output_fill()
2642
    : is_big_endian_(parameters->target().is_big_endian())
2643
  { }
2644
 
2645
  // Return the smallest size chunk of free space that can be
2646
  // filled with a dummy compilation unit.
2647
  size_t
2648
  minimum_hole_size() const
2649
  { return this->do_minimum_hole_size(); }
2650
 
2651
  // Write a fill pattern of length LEN at offset OFF in the file.
2652
  void
2653
  write(Output_file* of, off_t off, size_t len) const
2654
  { this->do_write(of, off, len); }
2655
 
2656
 protected:
2657
  virtual size_t
2658
  do_minimum_hole_size() const = 0;
2659
 
2660
  virtual void
2661
  do_write(Output_file* of, off_t off, size_t len) const = 0;
2662
 
2663
  bool
2664
  is_big_endian() const
2665
  { return this->is_big_endian_; }
2666
 
2667
 private:
2668
  bool is_big_endian_;
2669
};
2670
 
2671
// Fill method that introduces a dummy compilation unit in
2672
// a .debug_info or .debug_types section.
2673
 
2674
class Output_fill_debug_info : public Output_fill
2675
{
2676
 public:
2677
  Output_fill_debug_info(bool is_debug_types)
2678
    : is_debug_types_(is_debug_types)
2679
  { }
2680
 
2681
 protected:
2682
  virtual size_t
2683
  do_minimum_hole_size() const;
2684
 
2685
  virtual void
2686
  do_write(Output_file* of, off_t off, size_t len) const;
2687
 
2688
 private:
2689
  // Version of the header.
2690
  static const int version = 4;
2691
  // True if this is a .debug_types section.
2692
  bool is_debug_types_;
2693
};
2694
 
2695
// Fill method that introduces a dummy compilation unit in
2696
// a .debug_line section.
2697
 
2698
class Output_fill_debug_line : public Output_fill
2699
{
2700
 public:
2701
  Output_fill_debug_line()
2702
  { }
2703
 
2704
 protected:
2705
  virtual size_t
2706
  do_minimum_hole_size() const;
2707
 
2708
  virtual void
2709
  do_write(Output_file* of, off_t off, size_t len) const;
2710
 
2711
 private:
2712
  // Version of the header.  We write a DWARF-3 header because it's smaller
2713
  // and many tools have not yet been updated to understand the DWARF-4 header.
2714
  static const int version = 3;
2715
  // Length of the portion of the header that follows the header_length
2716
  // field.  This includes the following fields:
2717
  // minimum_instruction_length, default_is_stmt, line_base, line_range,
2718
  // opcode_base, standard_opcode_lengths[], include_directories, filenames.
2719
  // The standard_opcode_lengths array is 12 bytes long, and the
2720
  // include_directories and filenames fields each contain only a single
2721
  // null byte.
2722
  static const size_t header_length = 19;
2723
};
2724
 
2725 27 khays
// An output section.  We don't expect to have too many output
2726
// sections, so we don't bother to do a template on the size.
2727
 
2728
class Output_section : public Output_data
2729
{
2730
 public:
2731
  // Create an output section, giving the name, type, and flags.
2732
  Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
2733
  virtual ~Output_section();
2734
 
2735
  // Add a new input section SHNDX, named NAME, with header SHDR, from
2736
  // object OBJECT.  RELOC_SHNDX is the index of a relocation section
2737
  // which applies to this section, or 0 if none, or -1 if more than
2738
  // one.  HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2739
  // in a linker script; in that case we need to keep track of input
2740
  // sections associated with an output section.  Return the offset
2741
  // within the output section.
2742
  template<int size, bool big_endian>
2743
  off_t
2744
  add_input_section(Layout* layout, Sized_relobj_file<size, big_endian>* object,
2745
                    unsigned int shndx, const char* name,
2746
                    const elfcpp::Shdr<size, big_endian>& shdr,
2747
                    unsigned int reloc_shndx, bool have_sections_script);
2748
 
2749
  // Add generated data POSD to this output section.
2750
  void
2751
  add_output_section_data(Output_section_data* posd);
2752
 
2753
  // Add a relaxed input section PORIS called NAME to this output section
2754
  // with LAYOUT.
2755
  void
2756
  add_relaxed_input_section(Layout* layout,
2757
                            Output_relaxed_input_section* poris,
2758
                            const std::string& name);
2759
 
2760
  // Return the section name.
2761
  const char*
2762
  name() const
2763
  { return this->name_; }
2764
 
2765
  // Return the section type.
2766
  elfcpp::Elf_Word
2767
  type() const
2768
  { return this->type_; }
2769
 
2770
  // Return the section flags.
2771
  elfcpp::Elf_Xword
2772
  flags() const
2773
  { return this->flags_; }
2774
 
2775 159 khays
  typedef std::map<Section_id, unsigned int> Section_layout_order;
2776
 
2777
  void
2778 163 khays
  update_section_layout(const Section_layout_order* order_map);
2779 159 khays
 
2780 27 khays
  // Update the output section flags based on input section flags.
2781
  void
2782
  update_flags_for_input_section(elfcpp::Elf_Xword flags);
2783
 
2784
  // Return the entsize field.
2785
  uint64_t
2786
  entsize() const
2787
  { return this->entsize_; }
2788
 
2789
  // Set the entsize field.
2790
  void
2791
  set_entsize(uint64_t v);
2792
 
2793
  // Set the load address.
2794
  void
2795
  set_load_address(uint64_t load_address)
2796
  {
2797
    this->load_address_ = load_address;
2798
    this->has_load_address_ = true;
2799
  }
2800
 
2801
  // Set the link field to the output section index of a section.
2802
  void
2803
  set_link_section(const Output_data* od)
2804
  {
2805
    gold_assert(this->link_ == 0
2806
                && !this->should_link_to_symtab_
2807
                && !this->should_link_to_dynsym_);
2808
    this->link_section_ = od;
2809
  }
2810
 
2811
  // Set the link field to a constant.
2812
  void
2813
  set_link(unsigned int v)
2814
  {
2815
    gold_assert(this->link_section_ == NULL
2816
                && !this->should_link_to_symtab_
2817
                && !this->should_link_to_dynsym_);
2818
    this->link_ = v;
2819
  }
2820
 
2821
  // Record that this section should link to the normal symbol table.
2822
  void
2823
  set_should_link_to_symtab()
2824
  {
2825
    gold_assert(this->link_section_ == NULL
2826
                && this->link_ == 0
2827
                && !this->should_link_to_dynsym_);
2828
    this->should_link_to_symtab_ = true;
2829
  }
2830
 
2831
  // Record that this section should link to the dynamic symbol table.
2832
  void
2833
  set_should_link_to_dynsym()
2834
  {
2835
    gold_assert(this->link_section_ == NULL
2836
                && this->link_ == 0
2837
                && !this->should_link_to_symtab_);
2838
    this->should_link_to_dynsym_ = true;
2839
  }
2840
 
2841
  // Return the info field.
2842
  unsigned int
2843
  info() const
2844
  {
2845
    gold_assert(this->info_section_ == NULL
2846
                && this->info_symndx_ == NULL);
2847
    return this->info_;
2848
  }
2849
 
2850
  // Set the info field to the output section index of a section.
2851
  void
2852
  set_info_section(const Output_section* os)
2853
  {
2854
    gold_assert((this->info_section_ == NULL
2855
                 || (this->info_section_ == os
2856
                     && this->info_uses_section_index_))
2857
                && this->info_symndx_ == NULL
2858
                && this->info_ == 0);
2859
    this->info_section_ = os;
2860
    this->info_uses_section_index_= true;
2861
  }
2862
 
2863
  // Set the info field to the symbol table index of a symbol.
2864
  void
2865
  set_info_symndx(const Symbol* sym)
2866
  {
2867
    gold_assert(this->info_section_ == NULL
2868
                && (this->info_symndx_ == NULL
2869
                    || this->info_symndx_ == sym)
2870
                && this->info_ == 0);
2871
    this->info_symndx_ = sym;
2872
  }
2873
 
2874
  // Set the info field to the symbol table index of a section symbol.
2875
  void
2876
  set_info_section_symndx(const Output_section* os)
2877
  {
2878
    gold_assert((this->info_section_ == NULL
2879
                 || (this->info_section_ == os
2880
                     && !this->info_uses_section_index_))
2881
                && this->info_symndx_ == NULL
2882
                && this->info_ == 0);
2883
    this->info_section_ = os;
2884
    this->info_uses_section_index_ = false;
2885
  }
2886
 
2887
  // Set the info field to a constant.
2888
  void
2889
  set_info(unsigned int v)
2890
  {
2891
    gold_assert(this->info_section_ == NULL
2892
                && this->info_symndx_ == NULL
2893
                && (this->info_ == 0
2894
                    || this->info_ == v));
2895
    this->info_ = v;
2896
  }
2897
 
2898
  // Set the addralign field.
2899
  void
2900
  set_addralign(uint64_t v)
2901
  { this->addralign_ = v; }
2902
 
2903
  // Whether the output section index has been set.
2904
  bool
2905
  has_out_shndx() const
2906
  { return this->out_shndx_ != -1U; }
2907
 
2908
  // Indicate that we need a symtab index.
2909
  void
2910
  set_needs_symtab_index()
2911
  { this->needs_symtab_index_ = true; }
2912
 
2913
  // Return whether we need a symtab index.
2914
  bool
2915
  needs_symtab_index() const
2916
  { return this->needs_symtab_index_; }
2917
 
2918
  // Get the symtab index.
2919
  unsigned int
2920
  symtab_index() const
2921
  {
2922
    gold_assert(this->symtab_index_ != 0);
2923
    return this->symtab_index_;
2924
  }
2925
 
2926
  // Set the symtab index.
2927
  void
2928
  set_symtab_index(unsigned int index)
2929
  {
2930
    gold_assert(index != 0);
2931
    this->symtab_index_ = index;
2932
  }
2933
 
2934
  // Indicate that we need a dynsym index.
2935
  void
2936
  set_needs_dynsym_index()
2937
  { this->needs_dynsym_index_ = true; }
2938
 
2939
  // Return whether we need a dynsym index.
2940
  bool
2941
  needs_dynsym_index() const
2942
  { return this->needs_dynsym_index_; }
2943
 
2944
  // Get the dynsym index.
2945
  unsigned int
2946
  dynsym_index() const
2947
  {
2948
    gold_assert(this->dynsym_index_ != 0);
2949
    return this->dynsym_index_;
2950
  }
2951
 
2952
  // Set the dynsym index.
2953
  void
2954
  set_dynsym_index(unsigned int index)
2955
  {
2956
    gold_assert(index != 0);
2957
    this->dynsym_index_ = index;
2958
  }
2959
 
2960
  // Return whether the input sections sections attachd to this output
2961
  // section may require sorting.  This is used to handle constructor
2962
  // priorities compatibly with GNU ld.
2963
  bool
2964
  may_sort_attached_input_sections() const
2965
  { return this->may_sort_attached_input_sections_; }
2966
 
2967
  // Record that the input sections attached to this output section
2968
  // may require sorting.
2969
  void
2970
  set_may_sort_attached_input_sections()
2971
  { this->may_sort_attached_input_sections_ = true; }
2972
 
2973
   // Returns true if input sections must be sorted according to the
2974
  // order in which their name appear in the --section-ordering-file.
2975
  bool
2976
  input_section_order_specified()
2977
  { return this->input_section_order_specified_; }
2978
 
2979
  // Record that input sections must be sorted as some of their names
2980
  // match the patterns specified through --section-ordering-file.
2981
  void
2982
  set_input_section_order_specified()
2983
  { this->input_section_order_specified_ = true; }
2984
 
2985
  // Return whether the input sections attached to this output section
2986
  // require sorting.  This is used to handle constructor priorities
2987
  // compatibly with GNU ld.
2988
  bool
2989
  must_sort_attached_input_sections() const
2990
  { return this->must_sort_attached_input_sections_; }
2991
 
2992
  // Record that the input sections attached to this output section
2993
  // require sorting.
2994
  void
2995
  set_must_sort_attached_input_sections()
2996
  { this->must_sort_attached_input_sections_ = true; }
2997
 
2998
  // Get the order in which this section appears in the PT_LOAD output
2999
  // segment.
3000
  Output_section_order
3001
  order() const
3002
  { return this->order_; }
3003
 
3004
  // Set the order for this section.
3005
  void
3006
  set_order(Output_section_order order)
3007
  { this->order_ = order; }
3008
 
3009
  // Return whether this section holds relro data--data which has
3010
  // dynamic relocations but which may be marked read-only after the
3011
  // dynamic relocations have been completed.
3012
  bool
3013
  is_relro() const
3014
  { return this->is_relro_; }
3015
 
3016
  // Record that this section holds relro data.
3017
  void
3018
  set_is_relro()
3019
  { this->is_relro_ = true; }
3020
 
3021
  // Record that this section does not hold relro data.
3022
  void
3023
  clear_is_relro()
3024
  { this->is_relro_ = false; }
3025
 
3026
  // True if this is a small section: a section which holds small
3027
  // variables.
3028
  bool
3029
  is_small_section() const
3030
  { return this->is_small_section_; }
3031
 
3032
  // Record that this is a small section.
3033
  void
3034
  set_is_small_section()
3035
  { this->is_small_section_ = true; }
3036
 
3037
  // True if this is a large section: a section which holds large
3038
  // variables.
3039
  bool
3040
  is_large_section() const
3041
  { return this->is_large_section_; }
3042
 
3043
  // Record that this is a large section.
3044
  void
3045
  set_is_large_section()
3046
  { this->is_large_section_ = true; }
3047
 
3048
  // True if this is a large data (not BSS) section.
3049
  bool
3050
  is_large_data_section()
3051
  { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
3052
 
3053
  // Return whether this section should be written after all the input
3054
  // sections are complete.
3055
  bool
3056
  after_input_sections() const
3057
  { return this->after_input_sections_; }
3058
 
3059
  // Record that this section should be written after all the input
3060
  // sections are complete.
3061
  void
3062
  set_after_input_sections()
3063
  { this->after_input_sections_ = true; }
3064
 
3065
  // Return whether this section requires postprocessing after all
3066
  // relocations have been applied.
3067
  bool
3068
  requires_postprocessing() const
3069
  { return this->requires_postprocessing_; }
3070
 
3071
  // If a section requires postprocessing, return the buffer to use.
3072
  unsigned char*
3073
  postprocessing_buffer() const
3074
  {
3075
    gold_assert(this->postprocessing_buffer_ != NULL);
3076
    return this->postprocessing_buffer_;
3077
  }
3078
 
3079
  // If a section requires postprocessing, create the buffer to use.
3080
  void
3081
  create_postprocessing_buffer();
3082
 
3083
  // If a section requires postprocessing, this is the size of the
3084
  // buffer to which relocations should be applied.
3085
  off_t
3086
  postprocessing_buffer_size() const
3087
  { return this->current_data_size_for_child(); }
3088
 
3089
  // Modify the section name.  This is only permitted for an
3090
  // unallocated section, and only before the size has been finalized.
3091
  // Otherwise the name will not get into Layout::namepool_.
3092
  void
3093
  set_name(const char* newname)
3094
  {
3095
    gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
3096
    gold_assert(!this->is_data_size_valid());
3097
    this->name_ = newname;
3098
  }
3099
 
3100
  // Return whether the offset OFFSET in the input section SHNDX in
3101
  // object OBJECT is being included in the link.
3102
  bool
3103
  is_input_address_mapped(const Relobj* object, unsigned int shndx,
3104
                          off_t offset) const;
3105
 
3106
  // Return the offset within the output section of OFFSET relative to
3107
  // the start of input section SHNDX in object OBJECT.
3108
  section_offset_type
3109
  output_offset(const Relobj* object, unsigned int shndx,
3110
                section_offset_type offset) const;
3111
 
3112
  // Return the output virtual address of OFFSET relative to the start
3113
  // of input section SHNDX in object OBJECT.
3114
  uint64_t
3115
  output_address(const Relobj* object, unsigned int shndx,
3116
                 off_t offset) const;
3117
 
3118
  // Look for the merged section for input section SHNDX in object
3119
  // OBJECT.  If found, return true, and set *ADDR to the address of
3120
  // the start of the merged section.  This is not necessary the
3121
  // output offset corresponding to input offset 0 in the section,
3122
  // since the section may be mapped arbitrarily.
3123
  bool
3124
  find_starting_output_address(const Relobj* object, unsigned int shndx,
3125
                               uint64_t* addr) const;
3126
 
3127
  // Record that this output section was found in the SECTIONS clause
3128
  // of a linker script.
3129
  void
3130
  set_found_in_sections_clause()
3131
  { this->found_in_sections_clause_ = true; }
3132
 
3133
  // Return whether this output section was found in the SECTIONS
3134
  // clause of a linker script.
3135
  bool
3136
  found_in_sections_clause() const
3137
  { return this->found_in_sections_clause_; }
3138
 
3139
  // Write the section header into *OPHDR.
3140
  template<int size, bool big_endian>
3141
  void
3142
  write_header(const Layout*, const Stringpool*,
3143
               elfcpp::Shdr_write<size, big_endian>*) const;
3144
 
3145
  // The next few calls are for linker script support.
3146
 
3147
  // In some cases we need to keep a list of the input sections
3148
  // associated with this output section.  We only need the list if we
3149
  // might have to change the offsets of the input section within the
3150
  // output section after we add the input section.  The ordinary
3151
  // input sections will be written out when we process the object
3152
  // file, and as such we don't need to track them here.  We do need
3153
  // to track Output_section_data objects here.  We store instances of
3154
  // this structure in a std::vector, so it must be a POD.  There can
3155
  // be many instances of this structure, so we use a union to save
3156
  // some space.
3157
  class Input_section
3158
  {
3159
   public:
3160
    Input_section()
3161
      : shndx_(0), p2align_(0)
3162
    {
3163
      this->u1_.data_size = 0;
3164
      this->u2_.object = NULL;
3165
    }
3166
 
3167
    // For an ordinary input section.
3168
    Input_section(Relobj* object, unsigned int shndx, off_t data_size,
3169
                  uint64_t addralign)
3170
      : shndx_(shndx),
3171
        p2align_(ffsll(static_cast<long long>(addralign))),
3172
        section_order_index_(0)
3173
    {
3174
      gold_assert(shndx != OUTPUT_SECTION_CODE
3175
                  && shndx != MERGE_DATA_SECTION_CODE
3176
                  && shndx != MERGE_STRING_SECTION_CODE
3177
                  && shndx != RELAXED_INPUT_SECTION_CODE);
3178
      this->u1_.data_size = data_size;
3179
      this->u2_.object = object;
3180
    }
3181
 
3182
    // For a non-merge output section.
3183
    Input_section(Output_section_data* posd)
3184
      : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3185
        section_order_index_(0)
3186
    {
3187
      this->u1_.data_size = 0;
3188
      this->u2_.posd = posd;
3189
    }
3190
 
3191
    // For a merge section.
3192
    Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3193
      : shndx_(is_string
3194
               ? MERGE_STRING_SECTION_CODE
3195
               : MERGE_DATA_SECTION_CODE),
3196
        p2align_(0),
3197
        section_order_index_(0)
3198
    {
3199
      this->u1_.entsize = entsize;
3200
      this->u2_.posd = posd;
3201
    }
3202
 
3203
    // For a relaxed input section.
3204
    Input_section(Output_relaxed_input_section* psection)
3205
      : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3206
        section_order_index_(0)
3207
    {
3208
      this->u1_.data_size = 0;
3209
      this->u2_.poris = psection;
3210
    }
3211
 
3212
    unsigned int
3213
    section_order_index() const
3214
    {
3215
      return this->section_order_index_;
3216
    }
3217
 
3218
    void
3219
    set_section_order_index(unsigned int number)
3220
    {
3221
      this->section_order_index_ = number;
3222
    }
3223
 
3224
    // The required alignment.
3225
    uint64_t
3226
    addralign() const
3227
    {
3228
      if (this->p2align_ != 0)
3229
        return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3230
      else if (!this->is_input_section())
3231
        return this->u2_.posd->addralign();
3232
      else
3233
        return 0;
3234
    }
3235
 
3236
    // Set the required alignment, which must be either 0 or a power of 2.
3237
    // For input sections that are sub-classes of Output_section_data, a
3238
    // alignment of zero means asking the underlying object for alignment.
3239
    void
3240
    set_addralign(uint64_t addralign)
3241
    {
3242
      if (addralign == 0)
3243
        this->p2align_ = 0;
3244
      else
3245
        {
3246
          gold_assert((addralign & (addralign - 1)) == 0);
3247
          this->p2align_ = ffsll(static_cast<long long>(addralign));
3248
        }
3249
    }
3250
 
3251
    // Return the current required size, without finalization.
3252
    off_t
3253
    current_data_size() const;
3254
 
3255
    // Return the required size.
3256
    off_t
3257
    data_size() const;
3258
 
3259
    // Whether this is an input section.
3260
    bool
3261
    is_input_section() const
3262
    {
3263
      return (this->shndx_ != OUTPUT_SECTION_CODE
3264
              && this->shndx_ != MERGE_DATA_SECTION_CODE
3265
              && this->shndx_ != MERGE_STRING_SECTION_CODE
3266
              && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
3267
    }
3268
 
3269
    // Return whether this is a merge section which matches the
3270
    // parameters.
3271
    bool
3272
    is_merge_section(bool is_string, uint64_t entsize,
3273
                     uint64_t addralign) const
3274
    {
3275
      return (this->shndx_ == (is_string
3276
                               ? MERGE_STRING_SECTION_CODE
3277
                               : MERGE_DATA_SECTION_CODE)
3278
              && this->u1_.entsize == entsize
3279
              && this->addralign() == addralign);
3280
    }
3281
 
3282
    // Return whether this is a merge section for some input section.
3283
    bool
3284
    is_merge_section() const
3285
    {
3286
      return (this->shndx_ == MERGE_DATA_SECTION_CODE
3287
              || this->shndx_ == MERGE_STRING_SECTION_CODE);
3288
    }
3289
 
3290
    // Return whether this is a relaxed input section.
3291
    bool
3292
    is_relaxed_input_section() const
3293
    { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3294
 
3295
    // Return whether this is a generic Output_section_data.
3296
    bool
3297
    is_output_section_data() const
3298
    {
3299
      return this->shndx_ == OUTPUT_SECTION_CODE;
3300
    }
3301
 
3302
    // Return the object for an input section.
3303
    Relobj*
3304
    relobj() const;
3305
 
3306
    // Return the input section index for an input section.
3307
    unsigned int
3308
    shndx() const;
3309
 
3310
    // For non-input-sections, return the associated Output_section_data
3311
    // object.
3312
    Output_section_data*
3313
    output_section_data() const
3314
    {
3315
      gold_assert(!this->is_input_section());
3316
      return this->u2_.posd;
3317
    }
3318
 
3319
    // For a merge section, return the Output_merge_base pointer.
3320
    Output_merge_base*
3321
    output_merge_base() const
3322
    {
3323
      gold_assert(this->is_merge_section());
3324
      return this->u2_.pomb;
3325
    }
3326
 
3327
    // Return the Output_relaxed_input_section object.
3328
    Output_relaxed_input_section*
3329
    relaxed_input_section() const
3330
    {
3331
      gold_assert(this->is_relaxed_input_section());
3332
      return this->u2_.poris;
3333
    }
3334
 
3335
    // Set the output section.
3336
    void
3337
    set_output_section(Output_section* os)
3338
    {
3339
      gold_assert(!this->is_input_section());
3340
      Output_section_data* posd =
3341
        this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3342
      posd->set_output_section(os);
3343
    }
3344
 
3345
    // Set the address and file offset.  This is called during
3346
    // Layout::finalize.  SECTION_FILE_OFFSET is the file offset of
3347
    // the enclosing section.
3348
    void
3349
    set_address_and_file_offset(uint64_t address, off_t file_offset,
3350
                                off_t section_file_offset);
3351
 
3352
    // Reset the address and file offset.
3353
    void
3354
    reset_address_and_file_offset();
3355
 
3356
    // Finalize the data size.
3357
    void
3358
    finalize_data_size();
3359
 
3360
    // Add an input section, for SHF_MERGE sections.
3361
    bool
3362
    add_input_section(Relobj* object, unsigned int shndx)
3363
    {
3364
      gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3365
                  || this->shndx_ == MERGE_STRING_SECTION_CODE);
3366
      return this->u2_.posd->add_input_section(object, shndx);
3367
    }
3368
 
3369
    // Given an input OBJECT, an input section index SHNDX within that
3370
    // object, and an OFFSET relative to the start of that input
3371
    // section, return whether or not the output offset is known.  If
3372
    // this function returns true, it sets *POUTPUT to the offset in
3373
    // the output section, relative to the start of the input section
3374
    // in the output section.  *POUTPUT may be different from OFFSET
3375
    // for a merged section.
3376
    bool
3377
    output_offset(const Relobj* object, unsigned int shndx,
3378
                  section_offset_type offset,
3379
                  section_offset_type* poutput) const;
3380
 
3381
    // Return whether this is the merge section for the input section
3382
    // SHNDX in OBJECT.
3383
    bool
3384
    is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3385
 
3386
    // Write out the data.  This does nothing for an input section.
3387
    void
3388
    write(Output_file*);
3389
 
3390
    // Write the data to a buffer.  This does nothing for an input
3391
    // section.
3392
    void
3393
    write_to_buffer(unsigned char*);
3394
 
3395
    // Print to a map file.
3396
    void
3397
    print_to_mapfile(Mapfile*) const;
3398
 
3399
    // Print statistics about merge sections to stderr.
3400
    void
3401
    print_merge_stats(const char* section_name)
3402
    {
3403
      if (this->shndx_ == MERGE_DATA_SECTION_CODE
3404
          || this->shndx_ == MERGE_STRING_SECTION_CODE)
3405
        this->u2_.posd->print_merge_stats(section_name);
3406
    }
3407
 
3408
   private:
3409
    // Code values which appear in shndx_.  If the value is not one of
3410
    // these codes, it is the input section index in the object file.
3411
    enum
3412
    {
3413
      // An Output_section_data.
3414
      OUTPUT_SECTION_CODE = -1U,
3415
      // An Output_section_data for an SHF_MERGE section with
3416
      // SHF_STRINGS not set.
3417
      MERGE_DATA_SECTION_CODE = -2U,
3418
      // An Output_section_data for an SHF_MERGE section with
3419
      // SHF_STRINGS set.
3420
      MERGE_STRING_SECTION_CODE = -3U,
3421
      // An Output_section_data for a relaxed input section.
3422
      RELAXED_INPUT_SECTION_CODE = -4U
3423
    };
3424
 
3425
    // For an ordinary input section, this is the section index in the
3426
    // input file.  For an Output_section_data, this is
3427
    // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3428
    // MERGE_STRING_SECTION_CODE.
3429
    unsigned int shndx_;
3430
    // The required alignment, stored as a power of 2.
3431
    unsigned int p2align_;
3432
    union
3433
    {
3434
      // For an ordinary input section, the section size.
3435
      off_t data_size;
3436
      // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3437
      // used.  For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
3438
      // entity size.
3439
      uint64_t entsize;
3440
    } u1_;
3441
    union
3442
    {
3443
      // For an ordinary input section, the object which holds the
3444
      // input section.
3445
      Relobj* object;
3446
      // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3447
      // MERGE_STRING_SECTION_CODE, the data.
3448
      Output_section_data* posd;
3449
      Output_merge_base* pomb;
3450
      // For RELAXED_INPUT_SECTION_CODE, the data.
3451
      Output_relaxed_input_section* poris;
3452
    } u2_;
3453
    // The line number of the pattern it matches in the --section-ordering-file
3454
    // file.  It is 0 if does not match any pattern.
3455
    unsigned int section_order_index_;
3456
  };
3457
 
3458
  // Store the list of input sections for this Output_section into the
3459
  // list passed in.  This removes the input sections, leaving only
3460
  // any Output_section_data elements.  This returns the size of those
3461
  // Output_section_data elements.  ADDRESS is the address of this
3462
  // output section.  FILL is the fill value to use, in case there are
3463
  // any spaces between the remaining Output_section_data elements.
3464
  uint64_t
3465
  get_input_sections(uint64_t address, const std::string& fill,
3466
                     std::list<Input_section>*);
3467
 
3468
  // Add a script input section.  A script input section can either be
3469
  // a plain input section or a sub-class of Output_section_data.
3470
  void
3471
  add_script_input_section(const Input_section& input_section);
3472
 
3473
  // Set the current size of the output section.
3474
  void
3475
  set_current_data_size(off_t size)
3476
  { this->set_current_data_size_for_child(size); }
3477
 
3478
  // End of linker script support.
3479
 
3480
  // Save states before doing section layout.
3481
  // This is used for relaxation.
3482
  void
3483
  save_states();
3484
 
3485
  // Restore states prior to section layout.
3486
  void
3487
  restore_states();
3488
 
3489
  // Discard states.
3490
  void
3491
  discard_states();
3492
 
3493
  // Convert existing input sections to relaxed input sections.
3494
  void
3495
  convert_input_sections_to_relaxed_sections(
3496
      const std::vector<Output_relaxed_input_section*>& sections);
3497
 
3498
  // Find a relaxed input section to an input section in OBJECT
3499
  // with index SHNDX.  Return NULL if none is found.
3500
  const Output_relaxed_input_section*
3501
  find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3502
 
3503
  // Whether section offsets need adjustment due to relaxation.
3504
  bool
3505
  section_offsets_need_adjustment() const
3506
  { return this->section_offsets_need_adjustment_; }
3507
 
3508
  // Set section_offsets_need_adjustment to be true.
3509
  void
3510
  set_section_offsets_need_adjustment()
3511
  { this->section_offsets_need_adjustment_ = true; }
3512
 
3513
  // Adjust section offsets of input sections in this.  This is
3514
  // requires if relaxation caused some input sections to change sizes.
3515
  void
3516
  adjust_section_offsets();
3517
 
3518
  // Whether this is a NOLOAD section.
3519
  bool
3520
  is_noload() const
3521
  { return this->is_noload_; }
3522
 
3523
  // Set NOLOAD flag.
3524
  void
3525
  set_is_noload()
3526
  { this->is_noload_ = true; }
3527
 
3528
  // Print merge statistics to stderr.
3529
  void
3530
  print_merge_stats();
3531
 
3532
  // Set a fixed layout for the section.  Used for incremental update links.
3533
  void
3534
  set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3535
                   uint64_t sh_addralign);
3536
 
3537
  // Return TRUE if the section has a fixed layout.
3538
  bool
3539
  has_fixed_layout() const
3540
  { return this->has_fixed_layout_; }
3541
 
3542 159 khays
  // Set flag to allow patch space for this section.  Used for full
3543
  // incremental links.
3544
  void
3545
  set_is_patch_space_allowed()
3546
  { this->is_patch_space_allowed_ = true; }
3547
 
3548
  // Set a fill method to use for free space left in the output section
3549
  // during incremental links.
3550
  void
3551
  set_free_space_fill(Output_fill* free_space_fill)
3552
  {
3553
    this->free_space_fill_ = free_space_fill;
3554
    this->free_list_.set_min_hole_size(free_space_fill->minimum_hole_size());
3555
  }
3556
 
3557 27 khays
  // Reserve space within the fixed layout for the section.  Used for
3558
  // incremental update links.
3559
  void
3560
  reserve(uint64_t sh_offset, uint64_t sh_size);
3561
 
3562 148 khays
  // Allocate space from the free list for the section.  Used for
3563
  // incremental update links.
3564
  off_t
3565
  allocate(off_t len, uint64_t addralign);
3566
 
3567 27 khays
 protected:
3568
  // Return the output section--i.e., the object itself.
3569
  Output_section*
3570
  do_output_section()
3571
  { return this; }
3572
 
3573
  const Output_section*
3574
  do_output_section() const
3575
  { return this; }
3576
 
3577
  // Return the section index in the output file.
3578
  unsigned int
3579
  do_out_shndx() const
3580
  {
3581
    gold_assert(this->out_shndx_ != -1U);
3582
    return this->out_shndx_;
3583
  }
3584
 
3585
  // Set the output section index.
3586
  void
3587
  do_set_out_shndx(unsigned int shndx)
3588
  {
3589
    gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3590
    this->out_shndx_ = shndx;
3591
  }
3592
 
3593
  // Update the data size of the Output_section.  For a typical
3594
  // Output_section, there is nothing to do, but if there are any
3595
  // Output_section_data objects we need to do a trial layout
3596
  // here.
3597
  virtual void
3598
  update_data_size();
3599
 
3600
  // Set the final data size of the Output_section.  For a typical
3601
  // Output_section, there is nothing to do, but if there are any
3602
  // Output_section_data objects we need to set their final addresses
3603
  // here.
3604
  virtual void
3605
  set_final_data_size();
3606
 
3607
  // Reset the address and file offset.
3608
  void
3609
  do_reset_address_and_file_offset();
3610
 
3611
  // Return true if address and file offset already have reset values. In
3612
  // other words, calling reset_address_and_file_offset will not change them.
3613
  bool
3614
  do_address_and_file_offset_have_reset_values() const;
3615
 
3616
  // Write the data to the file.  For a typical Output_section, this
3617
  // does nothing: the data is written out by calling Object::Relocate
3618
  // on each input object.  But if there are any Output_section_data
3619
  // objects we do need to write them out here.
3620
  virtual void
3621
  do_write(Output_file*);
3622
 
3623
  // Return the address alignment--function required by parent class.
3624
  uint64_t
3625
  do_addralign() const
3626
  { return this->addralign_; }
3627
 
3628
  // Return whether there is a load address.
3629
  bool
3630
  do_has_load_address() const
3631
  { return this->has_load_address_; }
3632
 
3633
  // Return the load address.
3634
  uint64_t
3635
  do_load_address() const
3636
  {
3637
    gold_assert(this->has_load_address_);
3638
    return this->load_address_;
3639
  }
3640
 
3641
  // Return whether this is an Output_section.
3642
  bool
3643
  do_is_section() const
3644
  { return true; }
3645
 
3646
  // Return whether this is a section of the specified type.
3647
  bool
3648
  do_is_section_type(elfcpp::Elf_Word type) const
3649
  { return this->type_ == type; }
3650
 
3651
  // Return whether the specified section flag is set.
3652
  bool
3653
  do_is_section_flag_set(elfcpp::Elf_Xword flag) const
3654
  { return (this->flags_ & flag) != 0; }
3655
 
3656
  // Set the TLS offset.  Called only for SHT_TLS sections.
3657
  void
3658
  do_set_tls_offset(uint64_t tls_base);
3659
 
3660
  // Return the TLS offset, relative to the base of the TLS segment.
3661
  // Valid only for SHT_TLS sections.
3662
  uint64_t
3663
  do_tls_offset() const
3664
  { return this->tls_offset_; }
3665
 
3666
  // This may be implemented by a child class.
3667
  virtual void
3668
  do_finalize_name(Layout*)
3669
  { }
3670
 
3671
  // Print to the map file.
3672
  virtual void
3673
  do_print_to_mapfile(Mapfile*) const;
3674
 
3675
  // Record that this section requires postprocessing after all
3676
  // relocations have been applied.  This is called by a child class.
3677
  void
3678
  set_requires_postprocessing()
3679
  {
3680
    this->requires_postprocessing_ = true;
3681
    this->after_input_sections_ = true;
3682
  }
3683
 
3684
  // Write all the data of an Output_section into the postprocessing
3685
  // buffer.
3686
  void
3687
  write_to_postprocessing_buffer();
3688
 
3689
  typedef std::vector<Input_section> Input_section_list;
3690
 
3691
  // Allow a child class to access the input sections.
3692
  const Input_section_list&
3693
  input_sections() const
3694
  { return this->input_sections_; }
3695
 
3696
  // Whether this always keeps an input section list
3697
  bool
3698
  always_keeps_input_sections() const
3699
  { return this->always_keeps_input_sections_; }
3700
 
3701
  // Always keep an input section list.
3702
  void
3703
  set_always_keeps_input_sections()
3704
  {
3705
    gold_assert(this->current_data_size_for_child() == 0);
3706
    this->always_keeps_input_sections_ = true;
3707
  }
3708
 
3709
 private:
3710
  // We only save enough information to undo the effects of section layout.
3711
  class Checkpoint_output_section
3712
  {
3713
   public:
3714
    Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3715
                              const Input_section_list& input_sections,
3716
                              off_t first_input_offset,
3717
                              bool attached_input_sections_are_sorted)
3718
      : addralign_(addralign), flags_(flags),
3719
        input_sections_(input_sections),
3720
        input_sections_size_(input_sections_.size()),
3721
        input_sections_copy_(), first_input_offset_(first_input_offset),
3722
        attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
3723
    { }
3724
 
3725
    virtual
3726
    ~Checkpoint_output_section()
3727
    { }
3728
 
3729
    // Return the address alignment.
3730
    uint64_t
3731
    addralign() const
3732
    { return this->addralign_; }
3733
 
3734
    // Return the section flags.
3735
    elfcpp::Elf_Xword
3736
    flags() const
3737
    { return this->flags_; }
3738
 
3739
    // Return a reference to the input section list copy.
3740
    Input_section_list*
3741
    input_sections()
3742
    { return &this->input_sections_copy_; }
3743
 
3744
    // Return the size of input_sections at the time when checkpoint is
3745
    // taken.
3746
    size_t
3747
    input_sections_size() const
3748
    { return this->input_sections_size_; }
3749
 
3750
    // Whether input sections are copied.
3751
    bool
3752
    input_sections_saved() const
3753
    { return this->input_sections_copy_.size() == this->input_sections_size_; }
3754
 
3755
    off_t
3756
    first_input_offset() const
3757
    { return this->first_input_offset_; }
3758
 
3759
    bool
3760
    attached_input_sections_are_sorted() const
3761
    { return this->attached_input_sections_are_sorted_; }
3762
 
3763
    // Save input sections.
3764
    void
3765
    save_input_sections()
3766
    {
3767
      this->input_sections_copy_.reserve(this->input_sections_size_);
3768
      this->input_sections_copy_.clear();
3769
      Input_section_list::const_iterator p = this->input_sections_.begin();
3770
      gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3771
      for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3772
        this->input_sections_copy_.push_back(*p);
3773
    }
3774
 
3775
   private:
3776
    // The section alignment.
3777
    uint64_t addralign_;
3778
    // The section flags.
3779
    elfcpp::Elf_Xword flags_;
3780
    // Reference to the input sections to be checkpointed.
3781
    const Input_section_list& input_sections_;
3782
    // Size of the checkpointed portion of input_sections_;
3783
    size_t input_sections_size_;
3784
    // Copy of input sections.
3785
    Input_section_list input_sections_copy_;
3786
    // The offset of the first entry in input_sections_.
3787
    off_t first_input_offset_;
3788
    // True if the input sections attached to this output section have
3789
    // already been sorted.
3790
    bool attached_input_sections_are_sorted_;
3791
  };
3792
 
3793
  // This class is used to sort the input sections.
3794
  class Input_section_sort_entry;
3795
 
3796
  // This is the sort comparison function for ctors and dtors.
3797
  struct Input_section_sort_compare
3798
  {
3799
    bool
3800
    operator()(const Input_section_sort_entry&,
3801
               const Input_section_sort_entry&) const;
3802
  };
3803
 
3804
  // This is the sort comparison function for .init_array and .fini_array.
3805
  struct Input_section_sort_init_fini_compare
3806
  {
3807
    bool
3808
    operator()(const Input_section_sort_entry&,
3809
               const Input_section_sort_entry&) const;
3810
  };
3811
 
3812
  // This is the sort comparison function when a section order is specified
3813
  // from an input file.
3814
  struct Input_section_sort_section_order_index_compare
3815
  {
3816
    bool
3817
    operator()(const Input_section_sort_entry&,
3818
               const Input_section_sort_entry&) const;
3819
  };
3820
 
3821
  // Fill data.  This is used to fill in data between input sections.
3822
  // It is also used for data statements (BYTE, WORD, etc.) in linker
3823
  // scripts.  When we have to keep track of the input sections, we
3824
  // can use an Output_data_const, but we don't want to have to keep
3825
  // track of input sections just to implement fills.
3826
  class Fill
3827
  {
3828
   public:
3829
    Fill(off_t section_offset, off_t length)
3830
      : section_offset_(section_offset),
3831
        length_(convert_to_section_size_type(length))
3832
    { }
3833
 
3834
    // Return section offset.
3835
    off_t
3836
    section_offset() const
3837
    { return this->section_offset_; }
3838
 
3839
    // Return fill length.
3840
    section_size_type
3841
    length() const
3842
    { return this->length_; }
3843
 
3844
   private:
3845
    // The offset within the output section.
3846
    off_t section_offset_;
3847
    // The length of the space to fill.
3848
    section_size_type length_;
3849
  };
3850
 
3851
  typedef std::vector<Fill> Fill_list;
3852
 
3853
  // Map used during relaxation of existing sections.  This map
3854
  // a section id an input section list index.  We assume that
3855
  // Input_section_list is a vector.
3856
  typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
3857
 
3858
  // Add a new output section by Input_section.
3859
  void
3860
  add_output_section_data(Input_section*);
3861
 
3862
  // Add an SHF_MERGE input section.  Returns true if the section was
3863
  // handled.  If KEEPS_INPUT_SECTIONS is true, the output merge section
3864
  // stores information about the merged input sections.
3865
  bool
3866
  add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
3867
                          uint64_t entsize, uint64_t addralign,
3868
                          bool keeps_input_sections);
3869
 
3870
  // Add an output SHF_MERGE section POSD to this output section.
3871
  // IS_STRING indicates whether it is a SHF_STRINGS section, and
3872
  // ENTSIZE is the entity size.  This returns the entry added to
3873
  // input_sections_.
3874
  void
3875
  add_output_merge_section(Output_section_data* posd, bool is_string,
3876
                           uint64_t entsize);
3877
 
3878
  // Sort the attached input sections.
3879
  void
3880
  sort_attached_input_sections();
3881
 
3882
  // Find the merge section into which an input section with index SHNDX in
3883
  // OBJECT has been added.  Return NULL if none found.
3884
  Output_section_data*
3885
  find_merge_section(const Relobj* object, unsigned int shndx) const;
3886
 
3887
  // Build a relaxation map.
3888
  void
3889
  build_relaxation_map(
3890
      const Input_section_list& input_sections,
3891
      size_t limit,
3892
      Relaxation_map* map) const;
3893
 
3894
  // Convert input sections in an input section list into relaxed sections.
3895
  void
3896
  convert_input_sections_in_list_to_relaxed_sections(
3897
      const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3898
      const Relaxation_map& map,
3899
      Input_section_list* input_sections);
3900
 
3901
  // Build the lookup maps for merge and relaxed input sections.
3902
  void
3903
  build_lookup_maps() const;
3904
 
3905
  // Most of these fields are only valid after layout.
3906
 
3907
  // The name of the section.  This will point into a Stringpool.
3908
  const char* name_;
3909
  // The section address is in the parent class.
3910
  // The section alignment.
3911
  uint64_t addralign_;
3912
  // The section entry size.
3913
  uint64_t entsize_;
3914
  // The load address.  This is only used when using a linker script
3915
  // with a SECTIONS clause.  The has_load_address_ field indicates
3916
  // whether this field is valid.
3917
  uint64_t load_address_;
3918
  // The file offset is in the parent class.
3919
  // Set the section link field to the index of this section.
3920
  const Output_data* link_section_;
3921
  // If link_section_ is NULL, this is the link field.
3922
  unsigned int link_;
3923
  // Set the section info field to the index of this section.
3924
  const Output_section* info_section_;
3925
  // If info_section_ is NULL, set the info field to the symbol table
3926
  // index of this symbol.
3927
  const Symbol* info_symndx_;
3928
  // If info_section_ and info_symndx_ are NULL, this is the section
3929
  // info field.
3930
  unsigned int info_;
3931
  // The section type.
3932
  const elfcpp::Elf_Word type_;
3933
  // The section flags.
3934
  elfcpp::Elf_Xword flags_;
3935
  // The order of this section in the output segment.
3936
  Output_section_order order_;
3937
  // The section index.
3938
  unsigned int out_shndx_;
3939
  // If there is a STT_SECTION for this output section in the normal
3940
  // symbol table, this is the symbol index.  This starts out as zero.
3941
  // It is initialized in Layout::finalize() to be the index, or -1U
3942
  // if there isn't one.
3943
  unsigned int symtab_index_;
3944
  // If there is a STT_SECTION for this output section in the dynamic
3945
  // symbol table, this is the symbol index.  This starts out as zero.
3946
  // It is initialized in Layout::finalize() to be the index, or -1U
3947
  // if there isn't one.
3948
  unsigned int dynsym_index_;
3949
  // The input sections.  This will be empty in cases where we don't
3950
  // need to keep track of them.
3951
  Input_section_list input_sections_;
3952
  // The offset of the first entry in input_sections_.
3953
  off_t first_input_offset_;
3954
  // The fill data.  This is separate from input_sections_ because we
3955
  // often will need fill sections without needing to keep track of
3956
  // input sections.
3957
  Fill_list fills_;
3958
  // If the section requires postprocessing, this buffer holds the
3959
  // section contents during relocation.
3960
  unsigned char* postprocessing_buffer_;
3961
  // Whether this output section needs a STT_SECTION symbol in the
3962
  // normal symbol table.  This will be true if there is a relocation
3963
  // which needs it.
3964
  bool needs_symtab_index_ : 1;
3965
  // Whether this output section needs a STT_SECTION symbol in the
3966
  // dynamic symbol table.  This will be true if there is a dynamic
3967
  // relocation which needs it.
3968
  bool needs_dynsym_index_ : 1;
3969
  // Whether the link field of this output section should point to the
3970
  // normal symbol table.
3971
  bool should_link_to_symtab_ : 1;
3972
  // Whether the link field of this output section should point to the
3973
  // dynamic symbol table.
3974
  bool should_link_to_dynsym_ : 1;
3975
  // Whether this section should be written after all the input
3976
  // sections are complete.
3977
  bool after_input_sections_ : 1;
3978
  // Whether this section requires post processing after all
3979
  // relocations have been applied.
3980
  bool requires_postprocessing_ : 1;
3981
  // Whether an input section was mapped to this output section
3982
  // because of a SECTIONS clause in a linker script.
3983
  bool found_in_sections_clause_ : 1;
3984
  // Whether this section has an explicitly specified load address.
3985
  bool has_load_address_ : 1;
3986
  // True if the info_section_ field means the section index of the
3987
  // section, false if it means the symbol index of the corresponding
3988
  // section symbol.
3989
  bool info_uses_section_index_ : 1;
3990
  // True if input sections attached to this output section have to be
3991
  // sorted according to a specified order.
3992
  bool input_section_order_specified_ : 1;
3993
  // True if the input sections attached to this output section may
3994
  // need sorting.
3995
  bool may_sort_attached_input_sections_ : 1;
3996
  // True if the input sections attached to this output section must
3997
  // be sorted.
3998
  bool must_sort_attached_input_sections_ : 1;
3999
  // True if the input sections attached to this output section have
4000
  // already been sorted.
4001
  bool attached_input_sections_are_sorted_ : 1;
4002
  // True if this section holds relro data.
4003
  bool is_relro_ : 1;
4004
  // True if this is a small section.
4005
  bool is_small_section_ : 1;
4006
  // True if this is a large section.
4007
  bool is_large_section_ : 1;
4008
  // Whether code-fills are generated at write.
4009
  bool generate_code_fills_at_write_ : 1;
4010
  // Whether the entry size field should be zero.
4011
  bool is_entsize_zero_ : 1;
4012
  // Whether section offsets need adjustment due to relaxation.
4013
  bool section_offsets_need_adjustment_ : 1;
4014
  // Whether this is a NOLOAD section.
4015
  bool is_noload_ : 1;
4016
  // Whether this always keeps input section.
4017
  bool always_keeps_input_sections_ : 1;
4018
  // Whether this section has a fixed layout, for incremental update links.
4019
  bool has_fixed_layout_ : 1;
4020 159 khays
  // True if we can add patch space to this section.
4021
  bool is_patch_space_allowed_ : 1;
4022 27 khays
  // For SHT_TLS sections, the offset of this section relative to the base
4023
  // of the TLS segment.
4024
  uint64_t tls_offset_;
4025
  // Saved checkpoint.
4026
  Checkpoint_output_section* checkpoint_;
4027
  // Fast lookup maps for merged and relaxed input sections.
4028
  Output_section_lookup_maps* lookup_maps_;
4029
  // List of available regions within the section, for incremental
4030
  // update links.
4031
  Free_list free_list_;
4032 159 khays
  // Method for filling chunks of free space.
4033
  Output_fill* free_space_fill_;
4034
  // Amount added as patch space for incremental linking.
4035
  off_t patch_space_;
4036 27 khays
};
4037
 
4038
// An output segment.  PT_LOAD segments are built from collections of
4039
// output sections.  Other segments typically point within PT_LOAD
4040
// segments, and are built directly as needed.
4041
//
4042
// NOTE: We want to use the copy constructor for this class.  During
4043
// relaxation, we may try built the segments multiple times.  We do
4044
// that by copying the original segment list before lay-out, doing
4045
// a trial lay-out and roll-back to the saved copied if we need to
4046
// to the lay-out again.
4047
 
4048
class Output_segment
4049
{
4050
 public:
4051
  // Create an output segment, specifying the type and flags.
4052
  Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
4053
 
4054
  // Return the virtual address.
4055
  uint64_t
4056
  vaddr() const
4057
  { return this->vaddr_; }
4058
 
4059
  // Return the physical address.
4060
  uint64_t
4061
  paddr() const
4062
  { return this->paddr_; }
4063
 
4064
  // Return the segment type.
4065
  elfcpp::Elf_Word
4066
  type() const
4067
  { return this->type_; }
4068
 
4069
  // Return the segment flags.
4070
  elfcpp::Elf_Word
4071
  flags() const
4072
  { return this->flags_; }
4073
 
4074
  // Return the memory size.
4075
  uint64_t
4076
  memsz() const
4077
  { return this->memsz_; }
4078
 
4079
  // Return the file size.
4080
  off_t
4081
  filesz() const
4082
  { return this->filesz_; }
4083
 
4084
  // Return the file offset.
4085
  off_t
4086
  offset() const
4087
  { return this->offset_; }
4088
 
4089
  // Whether this is a segment created to hold large data sections.
4090
  bool
4091
  is_large_data_segment() const
4092
  { return this->is_large_data_segment_; }
4093
 
4094
  // Record that this is a segment created to hold large data
4095
  // sections.
4096
  void
4097
  set_is_large_data_segment()
4098
  { this->is_large_data_segment_ = true; }
4099
 
4100
  // Return the maximum alignment of the Output_data.
4101
  uint64_t
4102
  maximum_alignment();
4103
 
4104
  // Add the Output_section OS to this PT_LOAD segment.  SEG_FLAGS is
4105
  // the segment flags to use.
4106
  void
4107
  add_output_section_to_load(Layout* layout, Output_section* os,
4108
                             elfcpp::Elf_Word seg_flags);
4109
 
4110
  // Add the Output_section OS to this non-PT_LOAD segment.  SEG_FLAGS
4111
  // is the segment flags to use.
4112
  void
4113
  add_output_section_to_nonload(Output_section* os,
4114
                                elfcpp::Elf_Word seg_flags);
4115
 
4116
  // Remove an Output_section from this segment.  It is an error if it
4117
  // is not present.
4118
  void
4119
  remove_output_section(Output_section* os);
4120
 
4121
  // Add an Output_data (which need not be an Output_section) to the
4122
  // start of this segment.
4123
  void
4124
  add_initial_output_data(Output_data*);
4125
 
4126
  // Return true if this segment has any sections which hold actual
4127
  // data, rather than being a BSS section.
4128
  bool
4129
  has_any_data_sections() const;
4130
 
4131
  // Whether this segment has a dynamic relocs.
4132
  bool
4133
  has_dynamic_reloc() const;
4134
 
4135
  // Return the address of the first section.
4136
  uint64_t
4137
  first_section_load_address() const;
4138
 
4139
  // Return whether the addresses have been set already.
4140
  bool
4141
  are_addresses_set() const
4142
  { return this->are_addresses_set_; }
4143
 
4144
  // Set the addresses.
4145
  void
4146
  set_addresses(uint64_t vaddr, uint64_t paddr)
4147
  {
4148
    this->vaddr_ = vaddr;
4149
    this->paddr_ = paddr;
4150
    this->are_addresses_set_ = true;
4151
  }
4152
 
4153
  // Update the flags for the flags of an output section added to this
4154
  // segment.
4155
  void
4156
  update_flags_for_output_section(elfcpp::Elf_Xword flags)
4157
  {
4158
    // The ELF ABI specifies that a PT_TLS segment should always have
4159
    // PF_R as the flags.
4160
    if (this->type() != elfcpp::PT_TLS)
4161
      this->flags_ |= flags;
4162
  }
4163
 
4164
  // Set the segment flags.  This is only used if we have a PHDRS
4165
  // clause which explicitly specifies the flags.
4166
  void
4167
  set_flags(elfcpp::Elf_Word flags)
4168
  { this->flags_ = flags; }
4169
 
4170
  // Set the address of the segment to ADDR and the offset to *POFF
4171
  // and set the addresses and offsets of all contained output
4172
  // sections accordingly.  Set the section indexes of all contained
4173
  // output sections starting with *PSHNDX.  If RESET is true, first
4174
  // reset the addresses of the contained sections.  Return the
4175
  // address of the immediately following segment.  Update *POFF and
4176
  // *PSHNDX.  This should only be called for a PT_LOAD segment.
4177
  uint64_t
4178
  set_section_addresses(Layout*, bool reset, uint64_t addr,
4179
                        unsigned int* increase_relro, bool* has_relro,
4180
                        off_t* poff, unsigned int* pshndx);
4181
 
4182
  // Set the minimum alignment of this segment.  This may be adjusted
4183
  // upward based on the section alignments.
4184
  void
4185
  set_minimum_p_align(uint64_t align)
4186
  {
4187
    if (align > this->min_p_align_)
4188
      this->min_p_align_ = align;
4189
  }
4190
 
4191
  // Set the offset of this segment based on the section.  This should
4192
  // only be called for a non-PT_LOAD segment.
4193
  void
4194
  set_offset(unsigned int increase);
4195
 
4196
  // Set the TLS offsets of the sections contained in the PT_TLS segment.
4197
  void
4198
  set_tls_offsets();
4199
 
4200
  // Return the number of output sections.
4201
  unsigned int
4202
  output_section_count() const;
4203
 
4204
  // Return the section attached to the list segment with the lowest
4205
  // load address.  This is used when handling a PHDRS clause in a
4206
  // linker script.
4207
  Output_section*
4208
  section_with_lowest_load_address() const;
4209
 
4210
  // Write the segment header into *OPHDR.
4211
  template<int size, bool big_endian>
4212
  void
4213
  write_header(elfcpp::Phdr_write<size, big_endian>*);
4214
 
4215
  // Write the section headers of associated sections into V.
4216
  template<int size, bool big_endian>
4217
  unsigned char*
4218
  write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
4219
                        unsigned int* pshndx) const;
4220
 
4221
  // Print the output sections in the map file.
4222
  void
4223
  print_sections_to_mapfile(Mapfile*) const;
4224
 
4225
 private:
4226
  typedef std::vector<Output_data*> Output_data_list;
4227
 
4228
  // Find the maximum alignment in an Output_data_list.
4229
  static uint64_t
4230
  maximum_alignment_list(const Output_data_list*);
4231
 
4232
  // Return whether the first data section is a relro section.
4233
  bool
4234
  is_first_section_relro() const;
4235
 
4236
  // Set the section addresses in an Output_data_list.
4237
  uint64_t
4238
  set_section_list_addresses(Layout*, bool reset, Output_data_list*,
4239
                             uint64_t addr, off_t* poff, unsigned int* pshndx,
4240
                             bool* in_tls);
4241
 
4242
  // Return the number of Output_sections in an Output_data_list.
4243
  unsigned int
4244
  output_section_count_list(const Output_data_list*) const;
4245
 
4246
  // Return whether an Output_data_list has a dynamic reloc.
4247
  bool
4248
  has_dynamic_reloc_list(const Output_data_list*) const;
4249
 
4250
  // Find the section with the lowest load address in an
4251
  // Output_data_list.
4252
  void
4253
  lowest_load_address_in_list(const Output_data_list* pdl,
4254
                              Output_section** found,
4255
                              uint64_t* found_lma) const;
4256
 
4257
  // Find the first and last entries by address.
4258
  void
4259
  find_first_and_last_list(const Output_data_list* pdl,
4260
                           const Output_data** pfirst,
4261
                           const Output_data** plast) const;
4262
 
4263
  // Write the section headers in the list into V.
4264
  template<int size, bool big_endian>
4265
  unsigned char*
4266
  write_section_headers_list(const Layout*, const Stringpool*,
4267
                             const Output_data_list*, unsigned char* v,
4268
                             unsigned int* pshdx) const;
4269
 
4270
  // Print a section list to the mapfile.
4271
  void
4272
  print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4273
 
4274
  // NOTE: We want to use the copy constructor.  Currently, shallow copy
4275
  // works for us so we do not need to write our own copy constructor.
4276
 
4277
  // The list of output data attached to this segment.
4278
  Output_data_list output_lists_[ORDER_MAX];
4279
  // The segment virtual address.
4280
  uint64_t vaddr_;
4281
  // The segment physical address.
4282
  uint64_t paddr_;
4283
  // The size of the segment in memory.
4284
  uint64_t memsz_;
4285
  // The maximum section alignment.  The is_max_align_known_ field
4286
  // indicates whether this has been finalized.
4287
  uint64_t max_align_;
4288
  // The required minimum value for the p_align field.  This is used
4289
  // for PT_LOAD segments.  Note that this does not mean that
4290
  // addresses should be aligned to this value; it means the p_paddr
4291
  // and p_vaddr fields must be congruent modulo this value.  For
4292
  // non-PT_LOAD segments, the dynamic linker works more efficiently
4293
  // if the p_align field has the more conventional value, although it
4294
  // can align as needed.
4295
  uint64_t min_p_align_;
4296
  // The offset of the segment data within the file.
4297
  off_t offset_;
4298
  // The size of the segment data in the file.
4299
  off_t filesz_;
4300
  // The segment type;
4301
  elfcpp::Elf_Word type_;
4302
  // The segment flags.
4303
  elfcpp::Elf_Word flags_;
4304
  // Whether we have finalized max_align_.
4305
  bool is_max_align_known_ : 1;
4306
  // Whether vaddr and paddr were set by a linker script.
4307
  bool are_addresses_set_ : 1;
4308
  // Whether this segment holds large data sections.
4309
  bool is_large_data_segment_ : 1;
4310
};
4311
 
4312
// This class represents the output file.
4313
 
4314
class Output_file
4315
{
4316
 public:
4317
  Output_file(const char* name);
4318
 
4319
  // Indicate that this is a temporary file which should not be
4320
  // output.
4321
  void
4322
  set_is_temporary()
4323
  { this->is_temporary_ = true; }
4324
 
4325
  // Try to open an existing file. Returns false if the file doesn't
4326
  // exist, has a size of 0 or can't be mmaped.  This method is
4327
  // thread-unsafe.  If BASE_NAME is not NULL, use the contents of
4328
  // that file as the base for incremental linking.
4329
  bool
4330
  open_base_file(const char* base_name, bool writable);
4331
 
4332
  // Open the output file.  FILE_SIZE is the final size of the file.
4333
  // If the file already exists, it is deleted/truncated.  This method
4334
  // is thread-unsafe.
4335
  void
4336
  open(off_t file_size);
4337
 
4338
  // Resize the output file.  This method is thread-unsafe.
4339
  void
4340
  resize(off_t file_size);
4341
 
4342
  // Close the output file (flushing all buffered data) and make sure
4343
  // there are no errors.  This method is thread-unsafe.
4344
  void
4345
  close();
4346
 
4347
  // Return the size of this file.
4348
  off_t
4349
  filesize()
4350
  { return this->file_size_; }
4351
 
4352
  // Return the name of this file.
4353
  const char*
4354
  filename()
4355
  { return this->name_; }
4356
 
4357
  // We currently always use mmap which makes the view handling quite
4358
  // simple.  In the future we may support other approaches.
4359
 
4360
  // Write data to the output file.
4361
  void
4362
  write(off_t offset, const void* data, size_t len)
4363
  { memcpy(this->base_ + offset, data, len); }
4364
 
4365
  // Get a buffer to use to write to the file, given the offset into
4366
  // the file and the size.
4367
  unsigned char*
4368
  get_output_view(off_t start, size_t size)
4369
  {
4370
    gold_assert(start >= 0
4371
                && start + static_cast<off_t>(size) <= this->file_size_);
4372
    return this->base_ + start;
4373
  }
4374
 
4375
  // VIEW must have been returned by get_output_view.  Write the
4376
  // buffer to the file, passing in the offset and the size.
4377
  void
4378
  write_output_view(off_t, size_t, unsigned char*)
4379
  { }
4380
 
4381
  // Get a read/write buffer.  This is used when we want to write part
4382
  // of the file, read it in, and write it again.
4383
  unsigned char*
4384
  get_input_output_view(off_t start, size_t size)
4385
  { return this->get_output_view(start, size); }
4386
 
4387
  // Write a read/write buffer back to the file.
4388
  void
4389
  write_input_output_view(off_t, size_t, unsigned char*)
4390
  { }
4391
 
4392
  // Get a read buffer.  This is used when we just want to read part
4393
  // of the file back it in.
4394
  const unsigned char*
4395
  get_input_view(off_t start, size_t size)
4396
  { return this->get_output_view(start, size); }
4397
 
4398
  // Release a read bfufer.
4399
  void
4400
  free_input_view(off_t, size_t, const unsigned char*)
4401
  { }
4402
 
4403
 private:
4404
  // Map the file into memory or, if that fails, allocate anonymous
4405
  // memory.
4406
  void
4407
  map();
4408
 
4409
  // Allocate anonymous memory for the file.
4410
  bool
4411
  map_anonymous();
4412
 
4413
  // Map the file into memory.
4414
  bool
4415
  map_no_anonymous(bool);
4416
 
4417
  // Unmap the file from memory (and flush to disk buffers).
4418
  void
4419
  unmap();
4420
 
4421
  // File name.
4422
  const char* name_;
4423
  // File descriptor.
4424
  int o_;
4425
  // File size.
4426
  off_t file_size_;
4427
  // Base of file mapped into memory.
4428
  unsigned char* base_;
4429
  // True iff base_ points to a memory buffer rather than an output file.
4430
  bool map_is_anonymous_;
4431
  // True if base_ was allocated using new rather than mmap.
4432
  bool map_is_allocated_;
4433
  // True if this is a temporary file which should not be output.
4434
  bool is_temporary_;
4435
};
4436
 
4437
} // End namespace gold.
4438
 
4439
#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.