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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [script-sections.cc] - Blame information for rev 253

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

Line No. Rev Author Line
1 27 khays
// script-sections.cc -- linker script SECTIONS for gold
2
 
3
// Copyright 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
#include "gold.h"
24
 
25
#include <cstring>
26
#include <algorithm>
27
#include <list>
28
#include <map>
29
#include <string>
30
#include <vector>
31
#include <fnmatch.h>
32
 
33
#include "parameters.h"
34
#include "object.h"
35
#include "layout.h"
36
#include "output.h"
37
#include "script-c.h"
38
#include "script.h"
39
#include "script-sections.h"
40
 
41
// Support for the SECTIONS clause in linker scripts.
42
 
43
namespace gold
44
{
45
 
46
// A region of memory.
47
class Memory_region
48
{
49
 public:
50
  Memory_region(const char* name, size_t namelen, unsigned int attributes,
51
                Expression* start, Expression* length)
52
    : name_(name, namelen),
53
      attributes_(attributes),
54
      start_(start),
55
      length_(length),
56
      current_offset_(0),
57
      vma_sections_(),
58
      lma_sections_(),
59
      last_section_(NULL)
60
  { }
61
 
62
  // Return the name of this region.
63
  const std::string&
64
  name() const
65
  { return this->name_; }
66
 
67
  // Return the start address of this region.
68
  Expression*
69
  start_address() const
70
  { return this->start_; }
71
 
72
  // Return the length of this region.
73
  Expression*
74
  length() const
75
  { return this->length_; }
76
 
77
  // Print the region (when debugging).
78
  void
79
  print(FILE*) const;
80
 
81
  // Return true if <name,namelen> matches this region.
82
  bool
83
  name_match(const char* name, size_t namelen)
84
  {
85
    return (this->name_.length() == namelen
86
            && strncmp(this->name_.c_str(), name, namelen) == 0);
87
  }
88
 
89
  Expression*
90
  get_current_address() const
91
  {
92
    return
93
      script_exp_binary_add(this->start_,
94
                            script_exp_integer(this->current_offset_));
95
  }
96
 
97
  void
98
  increment_offset(std::string section_name, uint64_t amount,
99
                   const Symbol_table* symtab, const Layout* layout)
100
  {
101
    this->current_offset_ += amount;
102
 
103
    if (this->current_offset_
104
        > this->length_->eval(symtab, layout, false))
105
      gold_error(_("section %s overflows end of region %s"),
106
                 section_name.c_str(), this->name_.c_str());
107
  }
108
 
109
  // Returns true iff there is room left in this region
110
  // for AMOUNT more bytes of data.
111
  bool
112
  has_room_for(const Symbol_table* symtab, const Layout* layout,
113
               uint64_t amount) const
114
  {
115
    return (this->current_offset_ + amount
116
            < this->length_->eval(symtab, layout, false));
117
  }
118
 
119
  // Return true if the provided section flags
120
  // are compatible with this region's attributes.
121
  bool
122
  attributes_compatible(elfcpp::Elf_Xword flags, elfcpp::Elf_Xword type) const;
123
 
124
  void
125
  add_section(Output_section_definition* sec, bool vma)
126
  {
127
    if (vma)
128
      this->vma_sections_.push_back(sec);
129
    else
130
      this->lma_sections_.push_back(sec);
131
  }
132
 
133
  typedef std::vector<Output_section_definition*> Section_list;
134
 
135
  // Return the start of the list of sections
136
  // whose VMAs are taken from this region.
137
  Section_list::const_iterator
138
  get_vma_section_list_start() const
139
  { return this->vma_sections_.begin(); }
140
 
141
  // Return the start of the list of sections
142
  // whose LMAs are taken from this region.
143
  Section_list::const_iterator
144
  get_lma_section_list_start() const
145
  { return this->lma_sections_.begin(); }
146
 
147
  // Return the end of the list of sections
148
  // whose VMAs are taken from this region.
149
  Section_list::const_iterator
150
  get_vma_section_list_end() const
151
  { return this->vma_sections_.end(); }
152
 
153
  // Return the end of the list of sections
154
  // whose LMAs are taken from this region.
155
  Section_list::const_iterator
156
  get_lma_section_list_end() const
157
  { return this->lma_sections_.end(); }
158
 
159
  Output_section_definition*
160
  get_last_section() const
161
  { return this->last_section_; }
162
 
163
  void
164
  set_last_section(Output_section_definition* sec)
165
  { this->last_section_ = sec; }
166
 
167
 private:
168
 
169
  std::string name_;
170
  unsigned int attributes_;
171
  Expression* start_;
172
  Expression* length_;
173
  // The offset to the next free byte in the region.
174
  // Note - for compatibility with GNU LD we only maintain one offset
175
  // regardless of whether the region is being used for VMA values,
176
  // LMA values, or both.
177
  uint64_t current_offset_;
178
  // A list of sections whose VMAs are set inside this region.
179
  Section_list vma_sections_;
180
  // A list of sections whose LMAs are set inside this region.
181
  Section_list lma_sections_;
182
  // The latest section to make use of this region.
183
  Output_section_definition* last_section_;
184
};
185
 
186
// Return true if the provided section flags
187
// are compatible with this region's attributes.
188
 
189
bool
190
Memory_region::attributes_compatible(elfcpp::Elf_Xword flags,
191
                                     elfcpp::Elf_Xword type) const
192
{
193
  unsigned int attrs = this->attributes_;
194
 
195
  // No attributes means that this region is not compatible with anything.
196
  if (attrs == 0)
197
    return false;
198
 
199
  bool match = true;
200
  do
201
    {
202
      switch (attrs & - attrs)
203
        {
204
        case MEM_EXECUTABLE:
205
          if ((flags & elfcpp::SHF_EXECINSTR) == 0)
206
            match = false;
207
          break;
208
 
209
        case MEM_WRITEABLE:
210
          if ((flags & elfcpp::SHF_WRITE) == 0)
211
            match = false;
212
          break;
213
 
214
        case MEM_READABLE:
215
          // All sections are presumed readable.
216
          break;
217
 
218
        case MEM_ALLOCATABLE:
219
          if ((flags & elfcpp::SHF_ALLOC) == 0)
220
            match = false;
221
          break;
222
 
223
        case MEM_INITIALIZED:
224
          if ((type & elfcpp::SHT_NOBITS) != 0)
225
            match = false;
226
          break;
227
        }
228
      attrs &= ~ (attrs & - attrs);
229
    }
230
  while (attrs != 0);
231
 
232
  return match;
233
}
234
 
235
// Print a memory region.
236
 
237
void
238
Memory_region::print(FILE* f) const
239
{
240
  fprintf(f, "  %s", this->name_.c_str());
241
 
242
  unsigned int attrs = this->attributes_;
243
  if (attrs != 0)
244
    {
245
      fprintf(f, " (");
246
      do
247
        {
248
          switch (attrs & - attrs)
249
            {
250
            case MEM_EXECUTABLE:  fputc('x', f); break;
251
            case MEM_WRITEABLE:   fputc('w', f); break;
252
            case MEM_READABLE:    fputc('r', f); break;
253
            case MEM_ALLOCATABLE: fputc('a', f); break;
254
            case MEM_INITIALIZED: fputc('i', f); break;
255
            default:
256
              gold_unreachable();
257
            }
258
          attrs &= ~ (attrs & - attrs);
259
        }
260
      while (attrs != 0);
261
      fputc(')', f);
262
    }
263
 
264
  fprintf(f, " : origin = ");
265
  this->start_->print(f);
266
  fprintf(f, ", length = ");
267
  this->length_->print(f);
268
  fprintf(f, "\n");
269
}
270
 
271
// Manage orphan sections.  This is intended to be largely compatible
272
// with the GNU linker.  The Linux kernel implicitly relies on
273
// something similar to the GNU linker's orphan placement.  We
274
// originally used a simpler scheme here, but it caused the kernel
275
// build to fail, and was also rather inefficient.
276
 
277
class Orphan_section_placement
278
{
279
 private:
280
  typedef Script_sections::Elements_iterator Elements_iterator;
281
 
282
 public:
283
  Orphan_section_placement();
284
 
285
  // Handle an output section during initialization of this mapping.
286
  void
287
  output_section_init(const std::string& name, Output_section*,
288
                      Elements_iterator location);
289
 
290
  // Initialize the last location.
291
  void
292
  last_init(Elements_iterator location);
293
 
294
  // Set *PWHERE to the address of an iterator pointing to the
295
  // location to use for an orphan section.  Return true if the
296
  // iterator has a value, false otherwise.
297
  bool
298
  find_place(Output_section*, Elements_iterator** pwhere);
299
 
300
  // Return the iterator being used for sections at the very end of
301
  // the linker script.
302
  Elements_iterator
303
  last_place() const;
304
 
305
 private:
306
  // The places that we specifically recognize.  This list is copied
307
  // from the GNU linker.
308
  enum Place_index
309
  {
310
    PLACE_TEXT,
311
    PLACE_RODATA,
312
    PLACE_DATA,
313
    PLACE_TLS,
314
    PLACE_TLS_BSS,
315
    PLACE_BSS,
316
    PLACE_REL,
317
    PLACE_INTERP,
318
    PLACE_NONALLOC,
319
    PLACE_LAST,
320
    PLACE_MAX
321
  };
322
 
323
  // The information we keep for a specific place.
324
  struct Place
325
  {
326
    // The name of sections for this place.
327
    const char* name;
328
    // Whether we have a location for this place.
329
    bool have_location;
330
    // The iterator for this place.
331
    Elements_iterator location;
332
  };
333
 
334
  // Initialize one place element.
335
  void
336
  initialize_place(Place_index, const char*);
337
 
338
  // The places.
339
  Place places_[PLACE_MAX];
340
  // True if this is the first call to output_section_init.
341
  bool first_init_;
342
};
343
 
344
// Initialize Orphan_section_placement.
345
 
346
Orphan_section_placement::Orphan_section_placement()
347
  : first_init_(true)
348
{
349
  this->initialize_place(PLACE_TEXT, ".text");
350
  this->initialize_place(PLACE_RODATA, ".rodata");
351
  this->initialize_place(PLACE_DATA, ".data");
352
  this->initialize_place(PLACE_TLS, NULL);
353
  this->initialize_place(PLACE_TLS_BSS, NULL);
354
  this->initialize_place(PLACE_BSS, ".bss");
355
  this->initialize_place(PLACE_REL, NULL);
356
  this->initialize_place(PLACE_INTERP, ".interp");
357
  this->initialize_place(PLACE_NONALLOC, NULL);
358
  this->initialize_place(PLACE_LAST, NULL);
359
}
360
 
361
// Initialize one place element.
362
 
363
void
364
Orphan_section_placement::initialize_place(Place_index index, const char* name)
365
{
366
  this->places_[index].name = name;
367
  this->places_[index].have_location = false;
368
}
369
 
370
// While initializing the Orphan_section_placement information, this
371
// is called once for each output section named in the linker script.
372
// If we found an output section during the link, it will be passed in
373
// OS.
374
 
375
void
376
Orphan_section_placement::output_section_init(const std::string& name,
377
                                              Output_section* os,
378
                                              Elements_iterator location)
379
{
380
  bool first_init = this->first_init_;
381
  this->first_init_ = false;
382
 
383
  for (int i = 0; i < PLACE_MAX; ++i)
384
    {
385
      if (this->places_[i].name != NULL && this->places_[i].name == name)
386
        {
387
          if (this->places_[i].have_location)
388
            {
389
              // We have already seen a section with this name.
390
              return;
391
            }
392
 
393
          this->places_[i].location = location;
394
          this->places_[i].have_location = true;
395
 
396
          // If we just found the .bss section, restart the search for
397
          // an unallocated section.  This follows the GNU linker's
398
          // behaviour.
399
          if (i == PLACE_BSS)
400
            this->places_[PLACE_NONALLOC].have_location = false;
401
 
402
          return;
403
        }
404
    }
405
 
406
  // Relocation sections.
407
  if (!this->places_[PLACE_REL].have_location
408
      && os != NULL
409
      && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
410
      && (os->flags() & elfcpp::SHF_ALLOC) != 0)
411
    {
412
      this->places_[PLACE_REL].location = location;
413
      this->places_[PLACE_REL].have_location = true;
414
    }
415
 
416
  // We find the location for unallocated sections by finding the
417
  // first debugging or comment section after the BSS section (if
418
  // there is one).
419
  if (!this->places_[PLACE_NONALLOC].have_location
420
      && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
421
    {
422
      // We add orphan sections after the location in PLACES_.  We
423
      // want to store unallocated sections before LOCATION.  If this
424
      // is the very first section, we can't use it.
425
      if (!first_init)
426
        {
427
          --location;
428
          this->places_[PLACE_NONALLOC].location = location;
429
          this->places_[PLACE_NONALLOC].have_location = true;
430
        }
431
    }
432
}
433
 
434
// Initialize the last location.
435
 
436
void
437
Orphan_section_placement::last_init(Elements_iterator location)
438
{
439
  this->places_[PLACE_LAST].location = location;
440
  this->places_[PLACE_LAST].have_location = true;
441
}
442
 
443
// Set *PWHERE to the address of an iterator pointing to the location
444
// to use for an orphan section.  Return true if the iterator has a
445
// value, false otherwise.
446
 
447
bool
448
Orphan_section_placement::find_place(Output_section* os,
449
                                     Elements_iterator** pwhere)
450
{
451
  // Figure out where OS should go.  This is based on the GNU linker
452
  // code.  FIXME: The GNU linker handles small data sections
453
  // specially, but we don't.
454
  elfcpp::Elf_Word type = os->type();
455
  elfcpp::Elf_Xword flags = os->flags();
456
  Place_index index;
457
  if ((flags & elfcpp::SHF_ALLOC) == 0
458
      && !Layout::is_debug_info_section(os->name()))
459
    index = PLACE_NONALLOC;
460
  else if ((flags & elfcpp::SHF_ALLOC) == 0)
461
    index = PLACE_LAST;
462
  else if (type == elfcpp::SHT_NOTE)
463
    index = PLACE_INTERP;
464
  else if ((flags & elfcpp::SHF_TLS) != 0)
465
    {
466
      if (type == elfcpp::SHT_NOBITS)
467
        index = PLACE_TLS_BSS;
468
      else
469
        index = PLACE_TLS;
470
    }
471
  else if (type == elfcpp::SHT_NOBITS)
472
    index = PLACE_BSS;
473
  else if ((flags & elfcpp::SHF_WRITE) != 0)
474
    index = PLACE_DATA;
475
  else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
476
    index = PLACE_REL;
477
  else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
478
    index = PLACE_RODATA;
479
  else
480
    index = PLACE_TEXT;
481
 
482
  // If we don't have a location yet, try to find one based on a
483
  // plausible ordering of sections.
484
  if (!this->places_[index].have_location)
485
    {
486
      Place_index follow;
487
      switch (index)
488
        {
489
        default:
490
          follow = PLACE_MAX;
491
          break;
492
        case PLACE_RODATA:
493
          follow = PLACE_TEXT;
494
          break;
495
        case PLACE_BSS:
496
          follow = PLACE_DATA;
497
          break;
498
        case PLACE_REL:
499
          follow = PLACE_TEXT;
500
          break;
501
        case PLACE_INTERP:
502
          follow = PLACE_TEXT;
503
          break;
504
        case PLACE_TLS:
505
          follow = PLACE_DATA;
506
          break;
507
        case PLACE_TLS_BSS:
508
          follow = PLACE_TLS;
509
          if (!this->places_[PLACE_TLS].have_location)
510
            follow = PLACE_DATA;
511
          break;
512
        }
513
      if (follow != PLACE_MAX && this->places_[follow].have_location)
514
        {
515
          // Set the location of INDEX to the location of FOLLOW.  The
516
          // location of INDEX will then be incremented by the caller,
517
          // so anything in INDEX will continue to be after anything
518
          // in FOLLOW.
519
          this->places_[index].location = this->places_[follow].location;
520
          this->places_[index].have_location = true;
521
        }
522
    }
523
 
524
  *pwhere = &this->places_[index].location;
525
  bool ret = this->places_[index].have_location;
526
 
527
  // The caller will set the location.
528
  this->places_[index].have_location = true;
529
 
530
  return ret;
531
}
532
 
533
// Return the iterator being used for sections at the very end of the
534
// linker script.
535
 
536
Orphan_section_placement::Elements_iterator
537
Orphan_section_placement::last_place() const
538
{
539
  gold_assert(this->places_[PLACE_LAST].have_location);
540
  return this->places_[PLACE_LAST].location;
541
}
542
 
543
// An element in a SECTIONS clause.
544
 
545
class Sections_element
546
{
547
 public:
548
  Sections_element()
549
  { }
550
 
551
  virtual ~Sections_element()
552
  { }
553
 
554
  // Return whether an output section is relro.
555
  virtual bool
556
  is_relro() const
557
  { return false; }
558
 
559
  // Record that an output section is relro.
560
  virtual void
561
  set_is_relro()
562
  { }
563
 
564
  // Create any required output sections.  The only real
565
  // implementation is in Output_section_definition.
566
  virtual void
567
  create_sections(Layout*)
568
  { }
569
 
570
  // Add any symbol being defined to the symbol table.
571
  virtual void
572
  add_symbols_to_table(Symbol_table*)
573
  { }
574
 
575
  // Finalize symbols and check assertions.
576
  virtual void
577
  finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
578
  { }
579
 
580
  // Return the output section name to use for an input file name and
581
  // section name.  This only real implementation is in
582
  // Output_section_definition.
583
  virtual const char*
584
  output_section_name(const char*, const char*, Output_section***,
585
                      Script_sections::Section_type*)
586
  { return NULL; }
587
 
588
  // Initialize OSP with an output section.
589
  virtual void
590
  orphan_section_init(Orphan_section_placement*,
591
                      Script_sections::Elements_iterator)
592
  { }
593
 
594
  // Set section addresses.  This includes applying assignments if the
595
  // expression is an absolute value.
596
  virtual void
597
  set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
598
                        uint64_t*)
599
  { }
600
 
601
  // Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
602
  // this section is constrained, and the input sections do not match,
603
  // return the constraint, and set *POSD.
604
  virtual Section_constraint
605
  check_constraint(Output_section_definition**)
606
  { return CONSTRAINT_NONE; }
607
 
608
  // See if this is the alternate output section for a constrained
609
  // output section.  If it is, transfer the Output_section and return
610
  // true.  Otherwise return false.
611
  virtual bool
612
  alternate_constraint(Output_section_definition*, Section_constraint)
613
  { return false; }
614
 
615
  // Get the list of segments to use for an allocated section when
616
  // using a PHDRS clause.  If this is an allocated section, return
617
  // the Output_section, and set *PHDRS_LIST (the first parameter) to
618
  // the list of PHDRS to which it should be attached.  If the PHDRS
619
  // were not specified, don't change *PHDRS_LIST.  When not returning
620
  // NULL, set *ORPHAN (the second parameter) according to whether
621
  // this is an orphan section--one that is not mentioned in the
622
  // linker script.
623
  virtual Output_section*
624
  allocate_to_segment(String_list**, bool*)
625
  { return NULL; }
626
 
627
  // Look for an output section by name and return the address, the
628
  // load address, the alignment, and the size.  This is used when an
629
  // expression refers to an output section which was not actually
630
  // created.  This returns true if the section was found, false
631
  // otherwise.  The only real definition is for
632
  // Output_section_definition.
633
  virtual bool
634
  get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
635
                          uint64_t*) const
636
  { return false; }
637
 
638
  // Return the associated Output_section if there is one.
639
  virtual Output_section*
640
  get_output_section() const
641
  { return NULL; }
642
 
643
  // Set the section's memory regions.
644
  virtual void
645
  set_memory_region(Memory_region*, bool)
646
  { gold_error(_("Attempt to set a memory region for a non-output section")); }
647
 
648
  // Print the element for debugging purposes.
649
  virtual void
650
  print(FILE* f) const = 0;
651
};
652
 
653
// An assignment in a SECTIONS clause outside of an output section.
654
 
655
class Sections_element_assignment : public Sections_element
656
{
657
 public:
658
  Sections_element_assignment(const char* name, size_t namelen,
659
                              Expression* val, bool provide, bool hidden)
660
    : assignment_(name, namelen, false, val, provide, hidden)
661
  { }
662
 
663
  // Add the symbol to the symbol table.
664
  void
665
  add_symbols_to_table(Symbol_table* symtab)
666
  { this->assignment_.add_to_table(symtab); }
667
 
668
  // Finalize the symbol.
669
  void
670
  finalize_symbols(Symbol_table* symtab, const Layout* layout,
671
                   uint64_t* dot_value)
672
  {
673
    this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
674
  }
675
 
676
  // Set the section address.  There is no section here, but if the
677
  // value is absolute, we set the symbol.  This permits us to use
678
  // absolute symbols when setting dot.
679
  void
680
  set_section_addresses(Symbol_table* symtab, Layout* layout,
681
                        uint64_t* dot_value, uint64_t*, uint64_t*)
682
  {
683 163 khays
    this->assignment_.set_if_absolute(symtab, layout, true, *dot_value, NULL);
684 27 khays
  }
685
 
686
  // Print for debugging.
687
  void
688
  print(FILE* f) const
689
  {
690
    fprintf(f, "  ");
691
    this->assignment_.print(f);
692
  }
693
 
694
 private:
695
  Symbol_assignment assignment_;
696
};
697
 
698
// An assignment to the dot symbol in a SECTIONS clause outside of an
699
// output section.
700
 
701
class Sections_element_dot_assignment : public Sections_element
702
{
703
 public:
704
  Sections_element_dot_assignment(Expression* val)
705
    : val_(val)
706
  { }
707
 
708
  // Finalize the symbol.
709
  void
710
  finalize_symbols(Symbol_table* symtab, const Layout* layout,
711
                   uint64_t* dot_value)
712
  {
713
    // We ignore the section of the result because outside of an
714
    // output section definition the dot symbol is always considered
715
    // to be absolute.
716
    *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
717 163 khays
                                           NULL, NULL, NULL, false);
718 27 khays
  }
719
 
720
  // Update the dot symbol while setting section addresses.
721
  void
722
  set_section_addresses(Symbol_table* symtab, Layout* layout,
723
                        uint64_t* dot_value, uint64_t* dot_alignment,
724
                        uint64_t* load_address)
725
  {
726
    *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
727 163 khays
                                           NULL, NULL, dot_alignment, false);
728 27 khays
    *load_address = *dot_value;
729
  }
730
 
731
  // Print for debugging.
732
  void
733
  print(FILE* f) const
734
  {
735
    fprintf(f, "  . = ");
736
    this->val_->print(f);
737
    fprintf(f, "\n");
738
  }
739
 
740
 private:
741
  Expression* val_;
742
};
743
 
744
// An assertion in a SECTIONS clause outside of an output section.
745
 
746
class Sections_element_assertion : public Sections_element
747
{
748
 public:
749
  Sections_element_assertion(Expression* check, const char* message,
750
                             size_t messagelen)
751
    : assertion_(check, message, messagelen)
752
  { }
753
 
754
  // Check the assertion.
755
  void
756
  finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
757
  { this->assertion_.check(symtab, layout); }
758
 
759
  // Print for debugging.
760
  void
761
  print(FILE* f) const
762
  {
763
    fprintf(f, "  ");
764
    this->assertion_.print(f);
765
  }
766
 
767
 private:
768
  Script_assertion assertion_;
769
};
770
 
771
// An element in an output section in a SECTIONS clause.
772
 
773
class Output_section_element
774
{
775
 public:
776
  // A list of input sections.
777
  typedef std::list<Output_section::Input_section> Input_section_list;
778
 
779
  Output_section_element()
780
  { }
781
 
782
  virtual ~Output_section_element()
783
  { }
784
 
785
  // Return whether this element requires an output section to exist.
786
  virtual bool
787
  needs_output_section() const
788
  { return false; }
789
 
790
  // Add any symbol being defined to the symbol table.
791
  virtual void
792
  add_symbols_to_table(Symbol_table*)
793
  { }
794
 
795
  // Finalize symbols and check assertions.
796
  virtual void
797
  finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
798
  { }
799
 
800
  // Return whether this element matches FILE_NAME and SECTION_NAME.
801
  // The only real implementation is in Output_section_element_input.
802
  virtual bool
803
  match_name(const char*, const char*) const
804
  { return false; }
805
 
806
  // Set section addresses.  This includes applying assignments if the
807
  // expression is an absolute value.
808
  virtual void
809
  set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
810
                        uint64_t*, uint64_t*, Output_section**, std::string*,
811
                        Input_section_list*)
812
  { }
813
 
814
  // Print the element for debugging purposes.
815
  virtual void
816
  print(FILE* f) const = 0;
817
 
818
 protected:
819
  // Return a fill string that is LENGTH bytes long, filling it with
820
  // FILL.
821
  std::string
822
  get_fill_string(const std::string* fill, section_size_type length) const;
823
};
824
 
825
std::string
826
Output_section_element::get_fill_string(const std::string* fill,
827
                                        section_size_type length) const
828
{
829
  std::string this_fill;
830
  this_fill.reserve(length);
831
  while (this_fill.length() + fill->length() <= length)
832
    this_fill += *fill;
833
  if (this_fill.length() < length)
834
    this_fill.append(*fill, 0, length - this_fill.length());
835
  return this_fill;
836
}
837
 
838
// A symbol assignment in an output section.
839
 
840
class Output_section_element_assignment : public Output_section_element
841
{
842
 public:
843
  Output_section_element_assignment(const char* name, size_t namelen,
844
                                    Expression* val, bool provide,
845
                                    bool hidden)
846
    : assignment_(name, namelen, false, val, provide, hidden)
847
  { }
848
 
849
  // Add the symbol to the symbol table.
850
  void
851
  add_symbols_to_table(Symbol_table* symtab)
852
  { this->assignment_.add_to_table(symtab); }
853
 
854
  // Finalize the symbol.
855
  void
856
  finalize_symbols(Symbol_table* symtab, const Layout* layout,
857
                   uint64_t* dot_value, Output_section** dot_section)
858
  {
859
    this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
860
                                        *dot_section);
861
  }
862
 
863
  // Set the section address.  There is no section here, but if the
864
  // value is absolute, we set the symbol.  This permits us to use
865
  // absolute symbols when setting dot.
866
  void
867
  set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
868
                        uint64_t, uint64_t* dot_value, uint64_t*,
869 163 khays
                        Output_section** dot_section, std::string*,
870
                        Input_section_list*)
871 27 khays
  {
872 163 khays
    this->assignment_.set_if_absolute(symtab, layout, true, *dot_value,
873
                                      *dot_section);
874 27 khays
  }
875
 
876
  // Print for debugging.
877
  void
878
  print(FILE* f) const
879
  {
880
    fprintf(f, "    ");
881
    this->assignment_.print(f);
882
  }
883
 
884
 private:
885
  Symbol_assignment assignment_;
886
};
887
 
888
// An assignment to the dot symbol in an output section.
889
 
890
class Output_section_element_dot_assignment : public Output_section_element
891
{
892
 public:
893
  Output_section_element_dot_assignment(Expression* val)
894
    : val_(val)
895
  { }
896
 
897 163 khays
  // An assignment to dot within an output section is enough to force
898
  // the output section to exist.
899
  bool
900
  needs_output_section() const
901
  { return true; }
902
 
903 27 khays
  // Finalize the symbol.
904
  void
905
  finalize_symbols(Symbol_table* symtab, const Layout* layout,
906
                   uint64_t* dot_value, Output_section** dot_section)
907
  {
908
    *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
909 163 khays
                                           *dot_section, dot_section, NULL,
910
                                           true);
911 27 khays
  }
912
 
913
  // Update the dot symbol while setting section addresses.
914
  void
915
  set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
916
                        uint64_t, uint64_t* dot_value, uint64_t*,
917 163 khays
                        Output_section** dot_section, std::string*,
918
                        Input_section_list*);
919 27 khays
 
920
  // Print for debugging.
921
  void
922
  print(FILE* f) const
923
  {
924
    fprintf(f, "    . = ");
925
    this->val_->print(f);
926
    fprintf(f, "\n");
927
  }
928
 
929
 private:
930
  Expression* val_;
931
};
932
 
933
// Update the dot symbol while setting section addresses.
934
 
935
void
936
Output_section_element_dot_assignment::set_section_addresses(
937
    Symbol_table* symtab,
938
    Layout* layout,
939
    Output_section* output_section,
940
    uint64_t,
941
    uint64_t* dot_value,
942
    uint64_t* dot_alignment,
943
    Output_section** dot_section,
944
    std::string* fill,
945
    Input_section_list*)
946
{
947
  uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
948
                                                *dot_value, *dot_section,
949 163 khays
                                                dot_section, dot_alignment,
950
                                                true);
951 27 khays
  if (next_dot < *dot_value)
952
    gold_error(_("dot may not move backward"));
953
  if (next_dot > *dot_value && output_section != NULL)
954
    {
955
      section_size_type length = convert_to_section_size_type(next_dot
956
                                                              - *dot_value);
957
      Output_section_data* posd;
958
      if (fill->empty())
959
        posd = new Output_data_zero_fill(length, 0);
960
      else
961
        {
962
          std::string this_fill = this->get_fill_string(fill, length);
963
          posd = new Output_data_const(this_fill, 0);
964
        }
965
      output_section->add_output_section_data(posd);
966
      layout->new_output_section_data_from_script(posd);
967
    }
968
  *dot_value = next_dot;
969
}
970
 
971
// An assertion in an output section.
972
 
973
class Output_section_element_assertion : public Output_section_element
974
{
975
 public:
976
  Output_section_element_assertion(Expression* check, const char* message,
977
                                   size_t messagelen)
978
    : assertion_(check, message, messagelen)
979
  { }
980
 
981
  void
982
  print(FILE* f) const
983
  {
984
    fprintf(f, "    ");
985
    this->assertion_.print(f);
986
  }
987
 
988
 private:
989
  Script_assertion assertion_;
990
};
991
 
992
// We use a special instance of Output_section_data to handle BYTE,
993
// SHORT, etc.  This permits forward references to symbols in the
994
// expressions.
995
 
996
class Output_data_expression : public Output_section_data
997
{
998
 public:
999
  Output_data_expression(int size, bool is_signed, Expression* val,
1000
                         const Symbol_table* symtab, const Layout* layout,
1001
                         uint64_t dot_value, Output_section* dot_section)
1002
    : Output_section_data(size, 0, true),
1003
      is_signed_(is_signed), val_(val), symtab_(symtab),
1004
      layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
1005
  { }
1006
 
1007
 protected:
1008
  // Write the data to the output file.
1009
  void
1010
  do_write(Output_file*);
1011
 
1012
  // Write the data to a buffer.
1013
  void
1014
  do_write_to_buffer(unsigned char*);
1015
 
1016
  // Write to a map file.
1017
  void
1018
  do_print_to_mapfile(Mapfile* mapfile) const
1019
  { mapfile->print_output_data(this, _("** expression")); }
1020
 
1021
 private:
1022
  template<bool big_endian>
1023
  void
1024
  endian_write_to_buffer(uint64_t, unsigned char*);
1025
 
1026
  bool is_signed_;
1027
  Expression* val_;
1028
  const Symbol_table* symtab_;
1029
  const Layout* layout_;
1030
  uint64_t dot_value_;
1031
  Output_section* dot_section_;
1032
};
1033
 
1034
// Write the data element to the output file.
1035
 
1036
void
1037
Output_data_expression::do_write(Output_file* of)
1038
{
1039
  unsigned char* view = of->get_output_view(this->offset(), this->data_size());
1040
  this->write_to_buffer(view);
1041
  of->write_output_view(this->offset(), this->data_size(), view);
1042
}
1043
 
1044
// Write the data element to a buffer.
1045
 
1046
void
1047
Output_data_expression::do_write_to_buffer(unsigned char* buf)
1048
{
1049
  uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
1050
                                           true, this->dot_value_,
1051 163 khays
                                           this->dot_section_, NULL, NULL,
1052
                                           false);
1053 27 khays
 
1054
  if (parameters->target().is_big_endian())
1055
    this->endian_write_to_buffer<true>(val, buf);
1056
  else
1057
    this->endian_write_to_buffer<false>(val, buf);
1058
}
1059
 
1060
template<bool big_endian>
1061
void
1062
Output_data_expression::endian_write_to_buffer(uint64_t val,
1063
                                               unsigned char* buf)
1064
{
1065
  switch (this->data_size())
1066
    {
1067
    case 1:
1068
      elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
1069
      break;
1070
    case 2:
1071
      elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
1072
      break;
1073
    case 4:
1074
      elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
1075
      break;
1076
    case 8:
1077
      if (parameters->target().get_size() == 32)
1078
        {
1079
          val &= 0xffffffff;
1080
          if (this->is_signed_ && (val & 0x80000000) != 0)
1081
            val |= 0xffffffff00000000LL;
1082
        }
1083
      elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
1084
      break;
1085
    default:
1086
      gold_unreachable();
1087
    }
1088
}
1089
 
1090
// A data item in an output section.
1091
 
1092
class Output_section_element_data : public Output_section_element
1093
{
1094
 public:
1095
  Output_section_element_data(int size, bool is_signed, Expression* val)
1096
    : size_(size), is_signed_(is_signed), val_(val)
1097
  { }
1098
 
1099
  // If there is a data item, then we must create an output section.
1100
  bool
1101
  needs_output_section() const
1102
  { return true; }
1103
 
1104
  // Finalize symbols--we just need to update dot.
1105
  void
1106
  finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1107
                   Output_section**)
1108
  { *dot_value += this->size_; }
1109
 
1110
  // Store the value in the section.
1111
  void
1112
  set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
1113
                        uint64_t* dot_value, uint64_t*, Output_section**,
1114
                        std::string*, Input_section_list*);
1115
 
1116
  // Print for debugging.
1117
  void
1118
  print(FILE*) const;
1119
 
1120
 private:
1121
  // The size in bytes.
1122
  int size_;
1123
  // Whether the value is signed.
1124
  bool is_signed_;
1125
  // The value.
1126
  Expression* val_;
1127
};
1128
 
1129
// Store the value in the section.
1130
 
1131
void
1132
Output_section_element_data::set_section_addresses(
1133
    Symbol_table* symtab,
1134
    Layout* layout,
1135
    Output_section* os,
1136
    uint64_t,
1137
    uint64_t* dot_value,
1138
    uint64_t*,
1139
    Output_section** dot_section,
1140
    std::string*,
1141
    Input_section_list*)
1142
{
1143
  gold_assert(os != NULL);
1144
  Output_data_expression* expression =
1145
    new Output_data_expression(this->size_, this->is_signed_, this->val_,
1146
                               symtab, layout, *dot_value, *dot_section);
1147
  os->add_output_section_data(expression);
1148
  layout->new_output_section_data_from_script(expression);
1149
  *dot_value += this->size_;
1150
}
1151
 
1152
// Print for debugging.
1153
 
1154
void
1155
Output_section_element_data::print(FILE* f) const
1156
{
1157
  const char* s;
1158
  switch (this->size_)
1159
    {
1160
    case 1:
1161
      s = "BYTE";
1162
      break;
1163
    case 2:
1164
      s = "SHORT";
1165
      break;
1166
    case 4:
1167
      s = "LONG";
1168
      break;
1169
    case 8:
1170
      if (this->is_signed_)
1171
        s = "SQUAD";
1172
      else
1173
        s = "QUAD";
1174
      break;
1175
    default:
1176
      gold_unreachable();
1177
    }
1178
  fprintf(f, "    %s(", s);
1179
  this->val_->print(f);
1180
  fprintf(f, ")\n");
1181
}
1182
 
1183
// A fill value setting in an output section.
1184
 
1185
class Output_section_element_fill : public Output_section_element
1186
{
1187
 public:
1188
  Output_section_element_fill(Expression* val)
1189
    : val_(val)
1190
  { }
1191
 
1192
  // Update the fill value while setting section addresses.
1193
  void
1194
  set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1195
                        uint64_t, uint64_t* dot_value, uint64_t*,
1196
                        Output_section** dot_section,
1197
                        std::string* fill, Input_section_list*)
1198
  {
1199
    Output_section* fill_section;
1200
    uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
1201
                                                  *dot_value, *dot_section,
1202 163 khays
                                                  &fill_section, NULL, false);
1203 27 khays
    if (fill_section != NULL)
1204
      gold_warning(_("fill value is not absolute"));
1205
    // FIXME: The GNU linker supports fill values of arbitrary length.
1206
    unsigned char fill_buff[4];
1207
    elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1208
    fill->assign(reinterpret_cast<char*>(fill_buff), 4);
1209
  }
1210
 
1211
  // Print for debugging.
1212
  void
1213
  print(FILE* f) const
1214
  {
1215
    fprintf(f, "    FILL(");
1216
    this->val_->print(f);
1217
    fprintf(f, ")\n");
1218
  }
1219
 
1220
 private:
1221
  // The new fill value.
1222
  Expression* val_;
1223
};
1224
 
1225
// An input section specification in an output section
1226
 
1227
class Output_section_element_input : public Output_section_element
1228
{
1229
 public:
1230
  Output_section_element_input(const Input_section_spec* spec, bool keep);
1231
 
1232
  // Finalize symbols--just update the value of the dot symbol.
1233
  void
1234
  finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1235
                   Output_section** dot_section)
1236
  {
1237
    *dot_value = this->final_dot_value_;
1238
    *dot_section = this->final_dot_section_;
1239
  }
1240
 
1241
  // See whether we match FILE_NAME and SECTION_NAME as an input
1242
  // section.
1243
  bool
1244
  match_name(const char* file_name, const char* section_name) const;
1245
 
1246
  // Set the section address.
1247
  void
1248
  set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1249
                        uint64_t subalign, uint64_t* dot_value, uint64_t*,
1250
                        Output_section**, std::string* fill,
1251
                        Input_section_list*);
1252
 
1253
  // Print for debugging.
1254
  void
1255
  print(FILE* f) const;
1256
 
1257
 private:
1258
  // An input section pattern.
1259
  struct Input_section_pattern
1260
  {
1261
    std::string pattern;
1262
    bool pattern_is_wildcard;
1263
    Sort_wildcard sort;
1264
 
1265
    Input_section_pattern(const char* patterna, size_t patternlena,
1266
                          Sort_wildcard sorta)
1267
      : pattern(patterna, patternlena),
1268
        pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
1269
        sort(sorta)
1270
    { }
1271
  };
1272
 
1273
  typedef std::vector<Input_section_pattern> Input_section_patterns;
1274
 
1275
  // Filename_exclusions is a pair of filename pattern and a bool
1276
  // indicating whether the filename is a wildcard.
1277
  typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1278
 
1279
  // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1280
  // indicates whether this is a wildcard pattern.
1281
  static inline bool
1282
  match(const char* string, const char* pattern, bool is_wildcard_pattern)
1283
  {
1284
    return (is_wildcard_pattern
1285
            ? fnmatch(pattern, string, 0) == 0
1286
            : strcmp(string, pattern) == 0);
1287
  }
1288
 
1289
  // See if we match a file name.
1290
  bool
1291
  match_file_name(const char* file_name) const;
1292
 
1293
  // The file name pattern.  If this is the empty string, we match all
1294
  // files.
1295
  std::string filename_pattern_;
1296
  // Whether the file name pattern is a wildcard.
1297
  bool filename_is_wildcard_;
1298
  // How the file names should be sorted.  This may only be
1299
  // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1300
  Sort_wildcard filename_sort_;
1301
  // The list of file names to exclude.
1302
  Filename_exclusions filename_exclusions_;
1303
  // The list of input section patterns.
1304
  Input_section_patterns input_section_patterns_;
1305
  // Whether to keep this section when garbage collecting.
1306
  bool keep_;
1307
  // The value of dot after including all matching sections.
1308
  uint64_t final_dot_value_;
1309
  // The section where dot is defined after including all matching
1310
  // sections.
1311
  Output_section* final_dot_section_;
1312
};
1313
 
1314
// Construct Output_section_element_input.  The parser records strings
1315
// as pointers into a copy of the script file, which will go away when
1316
// parsing is complete.  We make sure they are in std::string objects.
1317
 
1318
Output_section_element_input::Output_section_element_input(
1319
    const Input_section_spec* spec,
1320
    bool keep)
1321
  : filename_pattern_(),
1322
    filename_is_wildcard_(false),
1323
    filename_sort_(spec->file.sort),
1324
    filename_exclusions_(),
1325
    input_section_patterns_(),
1326
    keep_(keep),
1327
    final_dot_value_(0),
1328
    final_dot_section_(NULL)
1329
{
1330
  // The filename pattern "*" is common, and matches all files.  Turn
1331
  // it into the empty string.
1332
  if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1333
    this->filename_pattern_.assign(spec->file.name.value,
1334
                                   spec->file.name.length);
1335
  this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
1336
 
1337
  if (spec->input_sections.exclude != NULL)
1338
    {
1339
      for (String_list::const_iterator p =
1340
             spec->input_sections.exclude->begin();
1341
           p != spec->input_sections.exclude->end();
1342
           ++p)
1343
        {
1344
          bool is_wildcard = is_wildcard_string((*p).c_str());
1345
          this->filename_exclusions_.push_back(std::make_pair(*p,
1346
                                                              is_wildcard));
1347
        }
1348
    }
1349
 
1350
  if (spec->input_sections.sections != NULL)
1351
    {
1352
      Input_section_patterns& isp(this->input_section_patterns_);
1353
      for (String_sort_list::const_iterator p =
1354
             spec->input_sections.sections->begin();
1355
           p != spec->input_sections.sections->end();
1356
           ++p)
1357
        isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1358
                                            p->sort));
1359
    }
1360
}
1361
 
1362
// See whether we match FILE_NAME.
1363
 
1364
bool
1365
Output_section_element_input::match_file_name(const char* file_name) const
1366
{
1367
  if (!this->filename_pattern_.empty())
1368
    {
1369
      // If we were called with no filename, we refuse to match a
1370
      // pattern which requires a file name.
1371
      if (file_name == NULL)
1372
        return false;
1373
 
1374
      if (!match(file_name, this->filename_pattern_.c_str(),
1375
                 this->filename_is_wildcard_))
1376
        return false;
1377
    }
1378
 
1379
  if (file_name != NULL)
1380
    {
1381
      // Now we have to see whether FILE_NAME matches one of the
1382
      // exclusion patterns, if any.
1383
      for (Filename_exclusions::const_iterator p =
1384
             this->filename_exclusions_.begin();
1385
           p != this->filename_exclusions_.end();
1386
           ++p)
1387
        {
1388
          if (match(file_name, p->first.c_str(), p->second))
1389
            return false;
1390
        }
1391
    }
1392
 
1393
  return true;
1394
}
1395
 
1396
// See whether we match FILE_NAME and SECTION_NAME.
1397
 
1398
bool
1399
Output_section_element_input::match_name(const char* file_name,
1400
                                         const char* section_name) const
1401
{
1402
  if (!this->match_file_name(file_name))
1403
    return false;
1404
 
1405
  // If there are no section name patterns, then we match.
1406
  if (this->input_section_patterns_.empty())
1407
    return true;
1408
 
1409
  // See whether we match the section name patterns.
1410
  for (Input_section_patterns::const_iterator p =
1411
         this->input_section_patterns_.begin();
1412
       p != this->input_section_patterns_.end();
1413
       ++p)
1414
    {
1415
      if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1416
        return true;
1417
    }
1418
 
1419
  // We didn't match any section names, so we didn't match.
1420
  return false;
1421
}
1422
 
1423
// Information we use to sort the input sections.
1424
 
1425
class Input_section_info
1426
{
1427
 public:
1428
  Input_section_info(const Output_section::Input_section& input_section)
1429
    : input_section_(input_section), section_name_(),
1430
      size_(0), addralign_(1)
1431
  { }
1432
 
1433
  // Return the simple input section.
1434
  const Output_section::Input_section&
1435
  input_section() const
1436
  { return this->input_section_; }
1437
 
1438
  // Return the object.
1439
  Relobj*
1440
  relobj() const
1441
  { return this->input_section_.relobj(); }
1442
 
1443
  // Return the section index.
1444
  unsigned int
1445
  shndx()
1446
  { return this->input_section_.shndx(); }
1447
 
1448
  // Return the section name.
1449
  const std::string&
1450
  section_name() const
1451
  { return this->section_name_; }
1452
 
1453
  // Set the section name.
1454
  void
1455
  set_section_name(const std::string name)
1456
  { this->section_name_ = name; }
1457
 
1458
  // Return the section size.
1459
  uint64_t
1460
  size() const
1461
  { return this->size_; }
1462
 
1463
  // Set the section size.
1464
  void
1465
  set_size(uint64_t size)
1466
  { this->size_ = size; }
1467
 
1468
  // Return the address alignment.
1469
  uint64_t
1470
  addralign() const
1471
  { return this->addralign_; }
1472
 
1473
  // Set the address alignment.
1474
  void
1475
  set_addralign(uint64_t addralign)
1476
  { this->addralign_ = addralign; }
1477
 
1478
 private:
1479
  // Input section, can be a relaxed section.
1480
  Output_section::Input_section input_section_;
1481
  // Name of the section. 
1482
  std::string section_name_;
1483
  // Section size.
1484
  uint64_t size_;
1485
  // Address alignment.
1486
  uint64_t addralign_;
1487
};
1488
 
1489
// A class to sort the input sections.
1490
 
1491
class Input_section_sorter
1492
{
1493
 public:
1494
  Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1495
    : filename_sort_(filename_sort), section_sort_(section_sort)
1496
  { }
1497
 
1498
  bool
1499
  operator()(const Input_section_info&, const Input_section_info&) const;
1500
 
1501
 private:
1502
  Sort_wildcard filename_sort_;
1503
  Sort_wildcard section_sort_;
1504
};
1505
 
1506
bool
1507
Input_section_sorter::operator()(const Input_section_info& isi1,
1508
                                 const Input_section_info& isi2) const
1509
{
1510
  if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1511
      || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1512
      || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1513
          && isi1.addralign() == isi2.addralign()))
1514
    {
1515
      if (isi1.section_name() != isi2.section_name())
1516
        return isi1.section_name() < isi2.section_name();
1517
    }
1518
  if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1519
      || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1520
      || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1521
    {
1522
      if (isi1.addralign() != isi2.addralign())
1523
        return isi1.addralign() < isi2.addralign();
1524
    }
1525
  if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1526
    {
1527
      if (isi1.relobj()->name() != isi2.relobj()->name())
1528
        return (isi1.relobj()->name() < isi2.relobj()->name());
1529
    }
1530
 
1531
  // Otherwise we leave them in the same order.
1532
  return false;
1533
}
1534
 
1535
// Set the section address.  Look in INPUT_SECTIONS for sections which
1536
// match this spec, sort them as specified, and add them to the output
1537
// section.
1538
 
1539
void
1540
Output_section_element_input::set_section_addresses(
1541
    Symbol_table*,
1542
    Layout* layout,
1543
    Output_section* output_section,
1544
    uint64_t subalign,
1545
    uint64_t* dot_value,
1546
    uint64_t*,
1547
    Output_section** dot_section,
1548
    std::string* fill,
1549
    Input_section_list* input_sections)
1550
{
1551
  // We build a list of sections which match each
1552
  // Input_section_pattern.
1553
 
1554
  typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1555
  size_t input_pattern_count = this->input_section_patterns_.size();
1556
  if (input_pattern_count == 0)
1557
    input_pattern_count = 1;
1558
  Matching_sections matching_sections(input_pattern_count);
1559
 
1560
  // Look through the list of sections for this output section.  Add
1561
  // each one which matches to one of the elements of
1562
  // MATCHING_SECTIONS.
1563
 
1564
  Input_section_list::iterator p = input_sections->begin();
1565
  while (p != input_sections->end())
1566
    {
1567
      Relobj* relobj = p->relobj();
1568
      unsigned int shndx = p->shndx();
1569
      Input_section_info isi(*p);
1570
 
1571
      // Calling section_name and section_addralign is not very
1572
      // efficient.
1573
 
1574
      // Lock the object so that we can get information about the
1575
      // section.  This is OK since we know we are single-threaded
1576
      // here.
1577
      {
1578
        const Task* task = reinterpret_cast<const Task*>(-1);
1579
        Task_lock_obj<Object> tl(task, relobj);
1580
 
1581
        isi.set_section_name(relobj->section_name(shndx));
1582
        if (p->is_relaxed_input_section())
1583
          {
1584
            // We use current data size because relaxed section sizes may not
1585
            // have finalized yet.
1586
            isi.set_size(p->relaxed_input_section()->current_data_size());
1587
            isi.set_addralign(p->relaxed_input_section()->addralign());
1588
          }
1589
        else
1590
          {
1591
            isi.set_size(relobj->section_size(shndx));
1592
            isi.set_addralign(relobj->section_addralign(shndx));
1593
          }
1594
      }
1595
 
1596
      if (!this->match_file_name(relobj->name().c_str()))
1597
        ++p;
1598
      else if (this->input_section_patterns_.empty())
1599
        {
1600
          matching_sections[0].push_back(isi);
1601
          p = input_sections->erase(p);
1602
        }
1603
      else
1604
        {
1605
          size_t i;
1606
          for (i = 0; i < input_pattern_count; ++i)
1607
            {
1608
              const Input_section_pattern&
1609
                isp(this->input_section_patterns_[i]);
1610
              if (match(isi.section_name().c_str(), isp.pattern.c_str(),
1611
                        isp.pattern_is_wildcard))
1612
                break;
1613
            }
1614
 
1615
          if (i >= this->input_section_patterns_.size())
1616
            ++p;
1617
          else
1618
            {
1619
              matching_sections[i].push_back(isi);
1620
              p = input_sections->erase(p);
1621
            }
1622
        }
1623
    }
1624
 
1625
  // Look through MATCHING_SECTIONS.  Sort each one as specified,
1626
  // using a stable sort so that we get the default order when
1627
  // sections are otherwise equal.  Add each input section to the
1628
  // output section.
1629
 
1630
  uint64_t dot = *dot_value;
1631
  for (size_t i = 0; i < input_pattern_count; ++i)
1632
    {
1633
      if (matching_sections[i].empty())
1634
        continue;
1635
 
1636
      gold_assert(output_section != NULL);
1637
 
1638
      const Input_section_pattern& isp(this->input_section_patterns_[i]);
1639
      if (isp.sort != SORT_WILDCARD_NONE
1640
          || this->filename_sort_ != SORT_WILDCARD_NONE)
1641
        std::stable_sort(matching_sections[i].begin(),
1642
                         matching_sections[i].end(),
1643
                         Input_section_sorter(this->filename_sort_,
1644
                                              isp.sort));
1645
 
1646
      for (std::vector<Input_section_info>::const_iterator p =
1647
             matching_sections[i].begin();
1648
           p != matching_sections[i].end();
1649
           ++p)
1650
        {
1651
          // Override the original address alignment if SUBALIGN is specified
1652
          // and is greater than the original alignment.  We need to make a
1653
          // copy of the input section to modify the alignment.
1654
          Output_section::Input_section sis(p->input_section());
1655
 
1656
          uint64_t this_subalign = sis.addralign();
1657
          if (!sis.is_input_section())
1658
            sis.output_section_data()->finalize_data_size();
1659
          uint64_t data_size = sis.data_size();
1660
          if (this_subalign < subalign)
1661
            {
1662
              this_subalign = subalign;
1663
              sis.set_addralign(subalign);
1664
            }
1665
 
1666
          uint64_t address = align_address(dot, this_subalign);
1667
 
1668
          if (address > dot && !fill->empty())
1669
            {
1670
              section_size_type length =
1671
                convert_to_section_size_type(address - dot);
1672
              std::string this_fill = this->get_fill_string(fill, length);
1673
              Output_section_data* posd = new Output_data_const(this_fill, 0);
1674
              output_section->add_output_section_data(posd);
1675
              layout->new_output_section_data_from_script(posd);
1676
            }
1677
 
1678
          output_section->add_script_input_section(sis);
1679
          dot = address + data_size;
1680
        }
1681
    }
1682
 
1683
  // An SHF_TLS/SHT_NOBITS section does not take up any
1684
  // address space.
1685
  if (output_section == NULL
1686
      || (output_section->flags() & elfcpp::SHF_TLS) == 0
1687
      || output_section->type() != elfcpp::SHT_NOBITS)
1688
    *dot_value = dot;
1689
 
1690
  this->final_dot_value_ = *dot_value;
1691
  this->final_dot_section_ = *dot_section;
1692
}
1693
 
1694
// Print for debugging.
1695
 
1696
void
1697
Output_section_element_input::print(FILE* f) const
1698
{
1699
  fprintf(f, "    ");
1700
 
1701
  if (this->keep_)
1702
    fprintf(f, "KEEP(");
1703
 
1704
  if (!this->filename_pattern_.empty())
1705
    {
1706
      bool need_close_paren = false;
1707
      switch (this->filename_sort_)
1708
        {
1709
        case SORT_WILDCARD_NONE:
1710
          break;
1711
        case SORT_WILDCARD_BY_NAME:
1712
          fprintf(f, "SORT_BY_NAME(");
1713
          need_close_paren = true;
1714
          break;
1715
        default:
1716
          gold_unreachable();
1717
        }
1718
 
1719
      fprintf(f, "%s", this->filename_pattern_.c_str());
1720
 
1721
      if (need_close_paren)
1722
        fprintf(f, ")");
1723
    }
1724
 
1725
  if (!this->input_section_patterns_.empty()
1726
      || !this->filename_exclusions_.empty())
1727
    {
1728
      fprintf(f, "(");
1729
 
1730
      bool need_space = false;
1731
      if (!this->filename_exclusions_.empty())
1732
        {
1733
          fprintf(f, "EXCLUDE_FILE(");
1734
          bool need_comma = false;
1735
          for (Filename_exclusions::const_iterator p =
1736
                 this->filename_exclusions_.begin();
1737
               p != this->filename_exclusions_.end();
1738
               ++p)
1739
            {
1740
              if (need_comma)
1741
                fprintf(f, ", ");
1742
              fprintf(f, "%s", p->first.c_str());
1743
              need_comma = true;
1744
            }
1745
          fprintf(f, ")");
1746
          need_space = true;
1747
        }
1748
 
1749
      for (Input_section_patterns::const_iterator p =
1750
             this->input_section_patterns_.begin();
1751
           p != this->input_section_patterns_.end();
1752
           ++p)
1753
        {
1754
          if (need_space)
1755
            fprintf(f, " ");
1756
 
1757
          int close_parens = 0;
1758
          switch (p->sort)
1759
            {
1760
            case SORT_WILDCARD_NONE:
1761
              break;
1762
            case SORT_WILDCARD_BY_NAME:
1763
              fprintf(f, "SORT_BY_NAME(");
1764
              close_parens = 1;
1765
              break;
1766
            case SORT_WILDCARD_BY_ALIGNMENT:
1767
              fprintf(f, "SORT_BY_ALIGNMENT(");
1768
              close_parens = 1;
1769
              break;
1770
            case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1771
              fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1772
              close_parens = 2;
1773
              break;
1774
            case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1775
              fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1776
              close_parens = 2;
1777
              break;
1778
            default:
1779
              gold_unreachable();
1780
            }
1781
 
1782
          fprintf(f, "%s", p->pattern.c_str());
1783
 
1784
          for (int i = 0; i < close_parens; ++i)
1785
            fprintf(f, ")");
1786
 
1787
          need_space = true;
1788
        }
1789
 
1790
      fprintf(f, ")");
1791
    }
1792
 
1793
  if (this->keep_)
1794
    fprintf(f, ")");
1795
 
1796
  fprintf(f, "\n");
1797
}
1798
 
1799
// An output section.
1800
 
1801
class Output_section_definition : public Sections_element
1802
{
1803
 public:
1804
  typedef Output_section_element::Input_section_list Input_section_list;
1805
 
1806
  Output_section_definition(const char* name, size_t namelen,
1807
                            const Parser_output_section_header* header);
1808
 
1809
  // Finish the output section with the information in the trailer.
1810
  void
1811
  finish(const Parser_output_section_trailer* trailer);
1812
 
1813
  // Add a symbol to be defined.
1814
  void
1815
  add_symbol_assignment(const char* name, size_t length, Expression* value,
1816
                        bool provide, bool hidden);
1817
 
1818
  // Add an assignment to the special dot symbol.
1819
  void
1820
  add_dot_assignment(Expression* value);
1821
 
1822
  // Add an assertion.
1823
  void
1824
  add_assertion(Expression* check, const char* message, size_t messagelen);
1825
 
1826
  // Add a data item to the current output section.
1827
  void
1828
  add_data(int size, bool is_signed, Expression* val);
1829
 
1830
  // Add a setting for the fill value.
1831
  void
1832
  add_fill(Expression* val);
1833
 
1834
  // Add an input section specification.
1835
  void
1836
  add_input_section(const Input_section_spec* spec, bool keep);
1837
 
1838
  // Return whether the output section is relro.
1839
  bool
1840
  is_relro() const
1841
  { return this->is_relro_; }
1842
 
1843
  // Record that the output section is relro.
1844
  void
1845
  set_is_relro()
1846
  { this->is_relro_ = true; }
1847
 
1848
  // Create any required output sections.
1849
  void
1850
  create_sections(Layout*);
1851
 
1852
  // Add any symbols being defined to the symbol table.
1853
  void
1854
  add_symbols_to_table(Symbol_table* symtab);
1855
 
1856
  // Finalize symbols and check assertions.
1857
  void
1858
  finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
1859
 
1860
  // Return the output section name to use for an input file name and
1861
  // section name.
1862
  const char*
1863
  output_section_name(const char* file_name, const char* section_name,
1864
                      Output_section***, Script_sections::Section_type*);
1865
 
1866
  // Initialize OSP with an output section.
1867
  void
1868
  orphan_section_init(Orphan_section_placement* osp,
1869
                      Script_sections::Elements_iterator p)
1870
  { osp->output_section_init(this->name_, this->output_section_, p); }
1871
 
1872
  // Set the section address.
1873
  void
1874
  set_section_addresses(Symbol_table* symtab, Layout* layout,
1875
                        uint64_t* dot_value, uint64_t*,
1876
                        uint64_t* load_address);
1877
 
1878
  // Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
1879
  // this section is constrained, and the input sections do not match,
1880
  // return the constraint, and set *POSD.
1881
  Section_constraint
1882
  check_constraint(Output_section_definition** posd);
1883
 
1884
  // See if this is the alternate output section for a constrained
1885
  // output section.  If it is, transfer the Output_section and return
1886
  // true.  Otherwise return false.
1887
  bool
1888
  alternate_constraint(Output_section_definition*, Section_constraint);
1889
 
1890
  // Get the list of segments to use for an allocated section when
1891
  // using a PHDRS clause.
1892
  Output_section*
1893
  allocate_to_segment(String_list** phdrs_list, bool* orphan);
1894
 
1895
  // Look for an output section by name and return the address, the
1896
  // load address, the alignment, and the size.  This is used when an
1897
  // expression refers to an output section which was not actually
1898
  // created.  This returns true if the section was found, false
1899
  // otherwise.
1900
  bool
1901
  get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1902
                          uint64_t*) const;
1903
 
1904
  // Return the associated Output_section if there is one.
1905
  Output_section*
1906
  get_output_section() const
1907
  { return this->output_section_; }
1908
 
1909
  // Print the contents to the FILE.  This is for debugging.
1910
  void
1911
  print(FILE*) const;
1912
 
1913
  // Return the output section type if specified or Script_sections::ST_NONE.
1914
  Script_sections::Section_type
1915
  section_type() const;
1916
 
1917
  // Store the memory region to use.
1918
  void
1919
  set_memory_region(Memory_region*, bool set_vma);
1920
 
1921
  void
1922
  set_section_vma(Expression* address)
1923
  { this->address_ = address; }
1924
 
1925
  void
1926
  set_section_lma(Expression* address)
1927
  { this->load_address_ = address; }
1928
 
1929
  const std::string&
1930
  get_section_name() const
1931
  { return this->name_; }
1932
 
1933
 private:
1934
  static const char*
1935
  script_section_type_name(Script_section_type);
1936
 
1937
  typedef std::vector<Output_section_element*> Output_section_elements;
1938
 
1939
  // The output section name.
1940
  std::string name_;
1941
  // The address.  This may be NULL.
1942
  Expression* address_;
1943
  // The load address.  This may be NULL.
1944
  Expression* load_address_;
1945
  // The alignment.  This may be NULL.
1946
  Expression* align_;
1947
  // The input section alignment.  This may be NULL.
1948
  Expression* subalign_;
1949
  // The constraint, if any.
1950
  Section_constraint constraint_;
1951
  // The fill value.  This may be NULL.
1952
  Expression* fill_;
1953
  // The list of segments this section should go into.  This may be
1954
  // NULL.
1955
  String_list* phdrs_;
1956
  // The list of elements defining the section.
1957
  Output_section_elements elements_;
1958
  // The Output_section created for this definition.  This will be
1959
  // NULL if none was created.
1960
  Output_section* output_section_;
1961
  // The address after it has been evaluated.
1962
  uint64_t evaluated_address_;
1963
  // The load address after it has been evaluated.
1964
  uint64_t evaluated_load_address_;
1965
  // The alignment after it has been evaluated.
1966
  uint64_t evaluated_addralign_;
1967
  // The output section is relro.
1968
  bool is_relro_;
1969
  // The output section type if specified.
1970
  enum Script_section_type script_section_type_;
1971
};
1972
 
1973
// Constructor.
1974
 
1975
Output_section_definition::Output_section_definition(
1976
    const char* name,
1977
    size_t namelen,
1978
    const Parser_output_section_header* header)
1979
  : name_(name, namelen),
1980
    address_(header->address),
1981
    load_address_(header->load_address),
1982
    align_(header->align),
1983
    subalign_(header->subalign),
1984
    constraint_(header->constraint),
1985
    fill_(NULL),
1986
    phdrs_(NULL),
1987
    elements_(),
1988
    output_section_(NULL),
1989
    evaluated_address_(0),
1990
    evaluated_load_address_(0),
1991
    evaluated_addralign_(0),
1992
    is_relro_(false),
1993
    script_section_type_(header->section_type)
1994
{
1995
}
1996
 
1997
// Finish an output section.
1998
 
1999
void
2000
Output_section_definition::finish(const Parser_output_section_trailer* trailer)
2001
{
2002
  this->fill_ = trailer->fill;
2003
  this->phdrs_ = trailer->phdrs;
2004
}
2005
 
2006
// Add a symbol to be defined.
2007
 
2008
void
2009
Output_section_definition::add_symbol_assignment(const char* name,
2010
                                                 size_t length,
2011
                                                 Expression* value,
2012
                                                 bool provide,
2013
                                                 bool hidden)
2014
{
2015
  Output_section_element* p = new Output_section_element_assignment(name,
2016
                                                                    length,
2017
                                                                    value,
2018
                                                                    provide,
2019
                                                                    hidden);
2020
  this->elements_.push_back(p);
2021
}
2022
 
2023
// Add an assignment to the special dot symbol.
2024
 
2025
void
2026
Output_section_definition::add_dot_assignment(Expression* value)
2027
{
2028
  Output_section_element* p = new Output_section_element_dot_assignment(value);
2029
  this->elements_.push_back(p);
2030
}
2031
 
2032
// Add an assertion.
2033
 
2034
void
2035
Output_section_definition::add_assertion(Expression* check,
2036
                                         const char* message,
2037
                                         size_t messagelen)
2038
{
2039
  Output_section_element* p = new Output_section_element_assertion(check,
2040
                                                                   message,
2041
                                                                   messagelen);
2042
  this->elements_.push_back(p);
2043
}
2044
 
2045
// Add a data item to the current output section.
2046
 
2047
void
2048
Output_section_definition::add_data(int size, bool is_signed, Expression* val)
2049
{
2050
  Output_section_element* p = new Output_section_element_data(size, is_signed,
2051
                                                              val);
2052
  this->elements_.push_back(p);
2053
}
2054
 
2055
// Add a setting for the fill value.
2056
 
2057
void
2058
Output_section_definition::add_fill(Expression* val)
2059
{
2060
  Output_section_element* p = new Output_section_element_fill(val);
2061
  this->elements_.push_back(p);
2062
}
2063
 
2064
// Add an input section specification.
2065
 
2066
void
2067
Output_section_definition::add_input_section(const Input_section_spec* spec,
2068
                                             bool keep)
2069
{
2070
  Output_section_element* p = new Output_section_element_input(spec, keep);
2071
  this->elements_.push_back(p);
2072
}
2073
 
2074
// Create any required output sections.  We need an output section if
2075
// there is a data statement here.
2076
 
2077
void
2078
Output_section_definition::create_sections(Layout* layout)
2079
{
2080
  if (this->output_section_ != NULL)
2081
    return;
2082
  for (Output_section_elements::const_iterator p = this->elements_.begin();
2083
       p != this->elements_.end();
2084
       ++p)
2085
    {
2086
      if ((*p)->needs_output_section())
2087
        {
2088
          const char* name = this->name_.c_str();
2089
          this->output_section_ =
2090
            layout->make_output_section_for_script(name, this->section_type());
2091
          return;
2092
        }
2093
    }
2094
}
2095
 
2096
// Add any symbols being defined to the symbol table.
2097
 
2098
void
2099
Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
2100
{
2101
  for (Output_section_elements::iterator p = this->elements_.begin();
2102
       p != this->elements_.end();
2103
       ++p)
2104
    (*p)->add_symbols_to_table(symtab);
2105
}
2106
 
2107
// Finalize symbols and check assertions.
2108
 
2109
void
2110
Output_section_definition::finalize_symbols(Symbol_table* symtab,
2111
                                            const Layout* layout,
2112
                                            uint64_t* dot_value)
2113
{
2114
  if (this->output_section_ != NULL)
2115
    *dot_value = this->output_section_->address();
2116
  else
2117
    {
2118
      uint64_t address = *dot_value;
2119
      if (this->address_ != NULL)
2120
        {
2121
          address = this->address_->eval_with_dot(symtab, layout, true,
2122
                                                  *dot_value, NULL,
2123 163 khays
                                                  NULL, NULL, false);
2124 27 khays
        }
2125
      if (this->align_ != NULL)
2126
        {
2127
          uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
2128
                                                       *dot_value, NULL,
2129 163 khays
                                                       NULL, NULL, false);
2130 27 khays
          address = align_address(address, align);
2131
        }
2132
      *dot_value = address;
2133
    }
2134
 
2135
  Output_section* dot_section = this->output_section_;
2136
  for (Output_section_elements::iterator p = this->elements_.begin();
2137
       p != this->elements_.end();
2138
       ++p)
2139
    (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
2140
}
2141
 
2142
// Return the output section name to use for an input section name.
2143
 
2144
const char*
2145
Output_section_definition::output_section_name(
2146
    const char* file_name,
2147
    const char* section_name,
2148
    Output_section*** slot,
2149
    Script_sections::Section_type* psection_type)
2150
{
2151
  // Ask each element whether it matches NAME.
2152
  for (Output_section_elements::const_iterator p = this->elements_.begin();
2153
       p != this->elements_.end();
2154
       ++p)
2155
    {
2156
      if ((*p)->match_name(file_name, section_name))
2157
        {
2158
          // We found a match for NAME, which means that it should go
2159
          // into this output section.
2160
          *slot = &this->output_section_;
2161
          *psection_type = this->section_type();
2162
          return this->name_.c_str();
2163
        }
2164
    }
2165
 
2166
  // We don't know about this section name.
2167
  return NULL;
2168
}
2169
 
2170
// Return true if memory from START to START + LENGTH is contained
2171
// within a memory region.
2172
 
2173
bool
2174
Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
2175
                                 uint64_t start, uint64_t length) const
2176
{
2177
  if (this->memory_regions_ == NULL)
2178
    return false;
2179
 
2180
  for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2181
       mr != this->memory_regions_->end();
2182
       ++mr)
2183
    {
2184
      uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
2185
      uint64_t l = (*mr)->length()->eval(symtab, layout, false);
2186
 
2187
      if (s <= start
2188
          && (s + l) >= (start + length))
2189
        return true;
2190
    }
2191
 
2192
  return false;
2193
}
2194
 
2195
// Find a memory region that should be used by a given output SECTION.
2196
// If provided set PREVIOUS_SECTION_RETURN to point to the last section
2197
// that used the return memory region.
2198
 
2199
Memory_region*
2200
Script_sections::find_memory_region(
2201
    Output_section_definition* section,
2202
    bool find_vma_region,
2203
    Output_section_definition** previous_section_return)
2204
{
2205
  if (previous_section_return != NULL)
2206
    * previous_section_return = NULL;
2207
 
2208
  // Walk the memory regions specified in this script, if any.
2209
  if (this->memory_regions_ == NULL)
2210
    return NULL;
2211
 
2212
  // The /DISCARD/ section never gets assigned to any region.
2213
  if (section->get_section_name() == "/DISCARD/")
2214
    return NULL;
2215
 
2216
  Memory_region* first_match = NULL;
2217
 
2218
  // First check to see if a region has been assigned to this section.
2219
  for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2220
       mr != this->memory_regions_->end();
2221
       ++mr)
2222
    {
2223
      if (find_vma_region)
2224
        {
2225
          for (Memory_region::Section_list::const_iterator s =
2226
                 (*mr)->get_vma_section_list_start();
2227
               s != (*mr)->get_vma_section_list_end();
2228
               ++s)
2229
            if ((*s) == section)
2230
              {
2231
                (*mr)->set_last_section(section);
2232
                return *mr;
2233
              }
2234
        }
2235
      else
2236
        {
2237
          for (Memory_region::Section_list::const_iterator s =
2238
                 (*mr)->get_lma_section_list_start();
2239
               s != (*mr)->get_lma_section_list_end();
2240
               ++s)
2241
            if ((*s) == section)
2242
              {
2243
                (*mr)->set_last_section(section);
2244
                return *mr;
2245
              }
2246
        }
2247
 
2248
      // Make a note of the first memory region whose attributes
2249
      // are compatible with the section.  If we do not find an
2250
      // explicit region assignment, then we will return this region.
2251
      Output_section* out_sec = section->get_output_section();
2252
      if (first_match == NULL
2253
          && out_sec != NULL
2254
          && (*mr)->attributes_compatible(out_sec->flags(),
2255
                                          out_sec->type()))
2256
        first_match = *mr;
2257
    }
2258
 
2259
  // With LMA computations, if an explicit region has not been specified then
2260
  // we will want to set the difference between the VMA and the LMA of the
2261
  // section were searching for to be the same as the difference between the
2262
  // VMA and LMA of the last section to be added to first matched region.
2263
  // Hence, if it was asked for, we return a pointer to the last section
2264
  // known to be used by the first matched region.
2265
  if (first_match != NULL
2266
      && previous_section_return != NULL)
2267
    *previous_section_return = first_match->get_last_section();
2268
 
2269
  return first_match;
2270
}
2271
 
2272
// Set the section address.  Note that the OUTPUT_SECTION_ field will
2273
// be NULL if no input sections were mapped to this output section.
2274
// We still have to adjust dot and process symbol assignments.
2275
 
2276
void
2277
Output_section_definition::set_section_addresses(Symbol_table* symtab,
2278
                                                 Layout* layout,
2279
                                                 uint64_t* dot_value,
2280
                                                 uint64_t* dot_alignment,
2281
                                                 uint64_t* load_address)
2282
{
2283
  Memory_region* vma_region = NULL;
2284
  Memory_region* lma_region = NULL;
2285
  Script_sections* script_sections =
2286
    layout->script_options()->script_sections();
2287
  uint64_t address;
2288
  uint64_t old_dot_value = *dot_value;
2289
  uint64_t old_load_address = *load_address;
2290
 
2291
  // Decide the start address for the section.  The algorithm is:
2292
  // 1) If an address has been specified in a linker script, use that.
2293
  // 2) Otherwise if a memory region has been specified for the section,
2294
  //    use the next free address in the region.
2295
  // 3) Otherwise if memory regions have been specified find the first
2296
  //    region whose attributes are compatible with this section and
2297
  //    install it into that region.
2298
  // 4) Otherwise use the current location counter.
2299
 
2300
  if (this->output_section_ != NULL
2301
      // Check for --section-start.
2302
      && parameters->options().section_start(this->output_section_->name(),
2303
                                             &address))
2304
    ;
2305
  else if (this->address_ == NULL)
2306
    {
2307
      vma_region = script_sections->find_memory_region(this, true, NULL);
2308
 
2309
      if (vma_region != NULL)
2310
        address = vma_region->get_current_address()->eval(symtab, layout,
2311
                                                          false);
2312
      else
2313
        address = *dot_value;
2314
    }
2315
  else
2316
    address = this->address_->eval_with_dot(symtab, layout, true,
2317
                                            *dot_value, NULL, NULL,
2318 163 khays
                                            dot_alignment, false);
2319 27 khays
  uint64_t align;
2320
  if (this->align_ == NULL)
2321
    {
2322
      if (this->output_section_ == NULL)
2323
        align = 0;
2324
      else
2325
        align = this->output_section_->addralign();
2326
    }
2327
  else
2328
    {
2329
      Output_section* align_section;
2330
      align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
2331 163 khays
                                          NULL, &align_section, NULL, false);
2332 27 khays
      if (align_section != NULL)
2333
        gold_warning(_("alignment of section %s is not absolute"),
2334
                     this->name_.c_str());
2335
      if (this->output_section_ != NULL)
2336
        this->output_section_->set_addralign(align);
2337
    }
2338
 
2339
  address = align_address(address, align);
2340
 
2341
  uint64_t start_address = address;
2342
 
2343
  *dot_value = address;
2344
 
2345
  // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2346
  // forced to zero, regardless of what the linker script wants.
2347
  if (this->output_section_ != NULL
2348
      && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
2349
          || this->output_section_->is_noload()))
2350
    this->output_section_->set_address(address);
2351
 
2352
  this->evaluated_address_ = address;
2353
  this->evaluated_addralign_ = align;
2354
 
2355
  uint64_t laddr;
2356
 
2357
  if (this->load_address_ == NULL)
2358
    {
2359
      Output_section_definition* previous_section;
2360
 
2361
      // Determine if an LMA region has been set for this section.
2362
      lma_region = script_sections->find_memory_region(this, false,
2363
                                                       &previous_section);
2364
 
2365
      if (lma_region != NULL)
2366
        {
2367
          if (previous_section == NULL)
2368
            // The LMA address was explicitly set to the given region.
2369
            laddr = lma_region->get_current_address()->eval(symtab, layout,
2370
                                                            false);
2371
          else
2372
            {
2373
              // We are not going to use the discovered lma_region, so
2374
              // make sure that we do not update it in the code below.
2375
              lma_region = NULL;
2376
 
2377
              if (this->address_ != NULL || previous_section == this)
2378
                {
2379
                  // Either an explicit VMA address has been set, or an
2380
                  // explicit VMA region has been set, so set the LMA equal to
2381
                  // the VMA.
2382
                  laddr = address;
2383
                }
2384
              else
2385
                {
2386
                  // The LMA address was not explicitly or implicitly set.
2387
                  //
2388
                  // We have been given the first memory region that is
2389
                  // compatible with the current section and a pointer to the
2390
                  // last section to use this region.  Set the LMA of this
2391
                  // section so that the difference between its' VMA and LMA
2392
                  // is the same as the difference between the VMA and LMA of
2393
                  // the last section in the given region.
2394
                  laddr = address + (previous_section->evaluated_load_address_
2395
                                     - previous_section->evaluated_address_);
2396
                }
2397
            }
2398
 
2399
          if (this->output_section_ != NULL)
2400
            this->output_section_->set_load_address(laddr);
2401
        }
2402
      else
2403
        {
2404
          // Do not set the load address of the output section, if one exists.
2405
          // This allows future sections to determine what the load address
2406
          // should be.  If none is ever set, it will default to being the
2407
          // same as the vma address.
2408
          laddr = address;
2409
        }
2410
    }
2411
  else
2412
    {
2413
      laddr = this->load_address_->eval_with_dot(symtab, layout, true,
2414
                                                 *dot_value,
2415
                                                 this->output_section_,
2416 163 khays
                                                 NULL, NULL, false);
2417 27 khays
      if (this->output_section_ != NULL)
2418
        this->output_section_->set_load_address(laddr);
2419
    }
2420
 
2421
  this->evaluated_load_address_ = laddr;
2422
 
2423
  uint64_t subalign;
2424
  if (this->subalign_ == NULL)
2425
    subalign = 0;
2426
  else
2427
    {
2428
      Output_section* subalign_section;
2429
      subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2430
                                                *dot_value, NULL,
2431 163 khays
                                                &subalign_section, NULL,
2432
                                                false);
2433 27 khays
      if (subalign_section != NULL)
2434
        gold_warning(_("subalign of section %s is not absolute"),
2435
                     this->name_.c_str());
2436
    }
2437
 
2438
  std::string fill;
2439
  if (this->fill_ != NULL)
2440
    {
2441
      // FIXME: The GNU linker supports fill values of arbitrary
2442
      // length.
2443
      Output_section* fill_section;
2444
      uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
2445
                                                     *dot_value,
2446
                                                     NULL, &fill_section,
2447 163 khays
                                                     NULL, false);
2448 27 khays
      if (fill_section != NULL)
2449
        gold_warning(_("fill of section %s is not absolute"),
2450
                     this->name_.c_str());
2451
      unsigned char fill_buff[4];
2452
      elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2453
      fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2454
    }
2455
 
2456
  Input_section_list input_sections;
2457
  if (this->output_section_ != NULL)
2458
    {
2459
      // Get the list of input sections attached to this output
2460
      // section.  This will leave the output section with only
2461
      // Output_section_data entries.
2462
      address += this->output_section_->get_input_sections(address,
2463
                                                           fill,
2464
                                                           &input_sections);
2465
      *dot_value = address;
2466
    }
2467
 
2468
  Output_section* dot_section = this->output_section_;
2469
  for (Output_section_elements::iterator p = this->elements_.begin();
2470
       p != this->elements_.end();
2471
       ++p)
2472
    (*p)->set_section_addresses(symtab, layout, this->output_section_,
2473
                                subalign, dot_value, dot_alignment,
2474
                                &dot_section, &fill, &input_sections);
2475
 
2476
  gold_assert(input_sections.empty());
2477
 
2478
  if (vma_region != NULL)
2479
    {
2480
      // Update the VMA region being used by the section now that we know how
2481
      // big it is.  Use the current address in the region, rather than
2482
      // start_address because that might have been aligned upwards and we
2483
      // need to allow for the padding.
2484
      Expression* addr = vma_region->get_current_address();
2485
      uint64_t size = *dot_value - addr->eval(symtab, layout, false);
2486
 
2487
      vma_region->increment_offset(this->get_section_name(), size,
2488
                                   symtab, layout);
2489
    }
2490
 
2491
  // If the LMA region is different from the VMA region, then increment the
2492
  // offset there as well.  Note that we use the same "dot_value -
2493
  // start_address" formula that is used in the load_address assignment below.
2494
  if (lma_region != NULL && lma_region != vma_region)
2495
    lma_region->increment_offset(this->get_section_name(),
2496
                                 *dot_value - start_address,
2497
                                 symtab, layout);
2498
 
2499
  // Compute the load address for the following section.
2500
  if (this->output_section_ == NULL)
2501
    *load_address = *dot_value;
2502
  else if (this->load_address_ == NULL)
2503
    {
2504
      if (lma_region == NULL)
2505
        *load_address = *dot_value;
2506
      else
2507
        *load_address =
2508
          lma_region->get_current_address()->eval(symtab, layout, false);
2509
    }
2510
  else
2511
    *load_address = (this->output_section_->load_address()
2512
                     + (*dot_value - start_address));
2513
 
2514
  if (this->output_section_ != NULL)
2515
    {
2516
      if (this->is_relro_)
2517
        this->output_section_->set_is_relro();
2518
      else
2519
        this->output_section_->clear_is_relro();
2520
 
2521
      // If this is a NOLOAD section, keep dot and load address unchanged.
2522
      if (this->output_section_->is_noload())
2523
        {
2524
          *dot_value = old_dot_value;
2525
          *load_address = old_load_address;
2526
        }
2527
    }
2528
}
2529
 
2530
// Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
2531
// this section is constrained, and the input sections do not match,
2532
// return the constraint, and set *POSD.
2533
 
2534
Section_constraint
2535
Output_section_definition::check_constraint(Output_section_definition** posd)
2536
{
2537
  switch (this->constraint_)
2538
    {
2539
    case CONSTRAINT_NONE:
2540
      return CONSTRAINT_NONE;
2541
 
2542
    case CONSTRAINT_ONLY_IF_RO:
2543
      if (this->output_section_ != NULL
2544
          && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2545
        {
2546
          *posd = this;
2547
          return CONSTRAINT_ONLY_IF_RO;
2548
        }
2549
      return CONSTRAINT_NONE;
2550
 
2551
    case CONSTRAINT_ONLY_IF_RW:
2552
      if (this->output_section_ != NULL
2553
          && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2554
        {
2555
          *posd = this;
2556
          return CONSTRAINT_ONLY_IF_RW;
2557
        }
2558
      return CONSTRAINT_NONE;
2559
 
2560
    case CONSTRAINT_SPECIAL:
2561
      if (this->output_section_ != NULL)
2562
        gold_error(_("SPECIAL constraints are not implemented"));
2563
      return CONSTRAINT_NONE;
2564
 
2565
    default:
2566
      gold_unreachable();
2567
    }
2568
}
2569
 
2570
// See if this is the alternate output section for a constrained
2571
// output section.  If it is, transfer the Output_section and return
2572
// true.  Otherwise return false.
2573
 
2574
bool
2575
Output_section_definition::alternate_constraint(
2576
    Output_section_definition* posd,
2577
    Section_constraint constraint)
2578
{
2579
  if (this->name_ != posd->name_)
2580
    return false;
2581
 
2582
  switch (constraint)
2583
    {
2584
    case CONSTRAINT_ONLY_IF_RO:
2585
      if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2586
        return false;
2587
      break;
2588
 
2589
    case CONSTRAINT_ONLY_IF_RW:
2590
      if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2591
        return false;
2592
      break;
2593
 
2594
    default:
2595
      gold_unreachable();
2596
    }
2597
 
2598
  // We have found the alternate constraint.  We just need to move
2599
  // over the Output_section.  When constraints are used properly,
2600
  // THIS should not have an output_section pointer, as all the input
2601
  // sections should have matched the other definition.
2602
 
2603
  if (this->output_section_ != NULL)
2604
    gold_error(_("mismatched definition for constrained sections"));
2605
 
2606
  this->output_section_ = posd->output_section_;
2607
  posd->output_section_ = NULL;
2608
 
2609
  if (this->is_relro_)
2610
    this->output_section_->set_is_relro();
2611
  else
2612
    this->output_section_->clear_is_relro();
2613
 
2614
  return true;
2615
}
2616
 
2617
// Get the list of segments to use for an allocated section when using
2618
// a PHDRS clause.
2619
 
2620
Output_section*
2621
Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2622
                                               bool* orphan)
2623
{
2624
  // Update phdrs_list even if we don't have an output section. It
2625
  // might be used by the following sections.
2626
  if (this->phdrs_ != NULL)
2627
    *phdrs_list = this->phdrs_;
2628
 
2629
  if (this->output_section_ == NULL)
2630
    return NULL;
2631
  if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2632
    return NULL;
2633
  *orphan = false;
2634
  return this->output_section_;
2635
}
2636
 
2637
// Look for an output section by name and return the address, the load
2638
// address, the alignment, and the size.  This is used when an
2639
// expression refers to an output section which was not actually
2640
// created.  This returns true if the section was found, false
2641
// otherwise.
2642
 
2643
bool
2644
Output_section_definition::get_output_section_info(const char* name,
2645
                                                   uint64_t* address,
2646
                                                   uint64_t* load_address,
2647
                                                   uint64_t* addralign,
2648
                                                   uint64_t* size) const
2649
{
2650
  if (this->name_ != name)
2651
    return false;
2652
 
2653
  if (this->output_section_ != NULL)
2654
    {
2655
      *address = this->output_section_->address();
2656
      if (this->output_section_->has_load_address())
2657
        *load_address = this->output_section_->load_address();
2658
      else
2659
        *load_address = *address;
2660
      *addralign = this->output_section_->addralign();
2661
      *size = this->output_section_->current_data_size();
2662
    }
2663
  else
2664
    {
2665
      *address = this->evaluated_address_;
2666
      *load_address = this->evaluated_load_address_;
2667
      *addralign = this->evaluated_addralign_;
2668
      *size = 0;
2669
    }
2670
 
2671
  return true;
2672
}
2673
 
2674
// Print for debugging.
2675
 
2676
void
2677
Output_section_definition::print(FILE* f) const
2678
{
2679
  fprintf(f, "  %s ", this->name_.c_str());
2680
 
2681
  if (this->address_ != NULL)
2682
    {
2683
      this->address_->print(f);
2684
      fprintf(f, " ");
2685
    }
2686
 
2687
  if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2688
      fprintf(f, "(%s) ",
2689
              this->script_section_type_name(this->script_section_type_));
2690
 
2691
  fprintf(f, ": ");
2692
 
2693
  if (this->load_address_ != NULL)
2694
    {
2695
      fprintf(f, "AT(");
2696
      this->load_address_->print(f);
2697
      fprintf(f, ") ");
2698
    }
2699
 
2700
  if (this->align_ != NULL)
2701
    {
2702
      fprintf(f, "ALIGN(");
2703
      this->align_->print(f);
2704
      fprintf(f, ") ");
2705
    }
2706
 
2707
  if (this->subalign_ != NULL)
2708
    {
2709
      fprintf(f, "SUBALIGN(");
2710
      this->subalign_->print(f);
2711
      fprintf(f, ") ");
2712
    }
2713
 
2714
  fprintf(f, "{\n");
2715
 
2716
  for (Output_section_elements::const_iterator p = this->elements_.begin();
2717
       p != this->elements_.end();
2718
       ++p)
2719
    (*p)->print(f);
2720
 
2721
  fprintf(f, "  }");
2722
 
2723
  if (this->fill_ != NULL)
2724
    {
2725
      fprintf(f, " = ");
2726
      this->fill_->print(f);
2727
    }
2728
 
2729
  if (this->phdrs_ != NULL)
2730
    {
2731
      for (String_list::const_iterator p = this->phdrs_->begin();
2732
           p != this->phdrs_->end();
2733
           ++p)
2734
        fprintf(f, " :%s", p->c_str());
2735
    }
2736
 
2737
  fprintf(f, "\n");
2738
}
2739
 
2740
Script_sections::Section_type
2741
Output_section_definition::section_type() const
2742
{
2743
  switch (this->script_section_type_)
2744
    {
2745
    case SCRIPT_SECTION_TYPE_NONE:
2746
      return Script_sections::ST_NONE;
2747
    case SCRIPT_SECTION_TYPE_NOLOAD:
2748
      return Script_sections::ST_NOLOAD;
2749
    case SCRIPT_SECTION_TYPE_COPY:
2750
    case SCRIPT_SECTION_TYPE_DSECT:
2751
    case SCRIPT_SECTION_TYPE_INFO:
2752
    case SCRIPT_SECTION_TYPE_OVERLAY:
2753
      // There are not really support so we treat them as ST_NONE.  The
2754
      // parse should have issued errors for them already.
2755
      return Script_sections::ST_NONE;
2756
    default:
2757
      gold_unreachable();
2758
    }
2759
}
2760
 
2761
// Return the name of a script section type.
2762
 
2763
const char*
2764
Output_section_definition::script_section_type_name(
2765
    Script_section_type script_section_type)
2766
{
2767
  switch (script_section_type)
2768
    {
2769
    case SCRIPT_SECTION_TYPE_NONE:
2770
      return "NONE";
2771
    case SCRIPT_SECTION_TYPE_NOLOAD:
2772
      return "NOLOAD";
2773
    case SCRIPT_SECTION_TYPE_DSECT:
2774
      return "DSECT";
2775
    case SCRIPT_SECTION_TYPE_COPY:
2776
      return "COPY";
2777
    case SCRIPT_SECTION_TYPE_INFO:
2778
      return "INFO";
2779
    case SCRIPT_SECTION_TYPE_OVERLAY:
2780
      return "OVERLAY";
2781
    default:
2782
      gold_unreachable();
2783
    }
2784
}
2785
 
2786
void
2787
Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
2788
{
2789
  gold_assert(mr != NULL);
2790
  // Add the current section to the specified region's list.
2791
  mr->add_section(this, set_vma);
2792
}
2793
 
2794
// An output section created to hold orphaned input sections.  These
2795
// do not actually appear in linker scripts.  However, for convenience
2796
// when setting the output section addresses, we put a marker to these
2797
// sections in the appropriate place in the list of SECTIONS elements.
2798
 
2799
class Orphan_output_section : public Sections_element
2800
{
2801
 public:
2802
  Orphan_output_section(Output_section* os)
2803
    : os_(os)
2804
  { }
2805
 
2806
  // Return whether the orphan output section is relro.  We can just
2807
  // check the output section because we always set the flag, if
2808
  // needed, just after we create the Orphan_output_section.
2809
  bool
2810
  is_relro() const
2811
  { return this->os_->is_relro(); }
2812
 
2813
  // Initialize OSP with an output section.  This should have been
2814
  // done already.
2815
  void
2816
  orphan_section_init(Orphan_section_placement*,
2817
                      Script_sections::Elements_iterator)
2818
  { gold_unreachable(); }
2819
 
2820
  // Set section addresses.
2821
  void
2822
  set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2823
                        uint64_t*);
2824
 
2825
  // Get the list of segments to use for an allocated section when
2826
  // using a PHDRS clause.
2827
  Output_section*
2828
  allocate_to_segment(String_list**, bool*);
2829
 
2830
  // Return the associated Output_section.
2831
  Output_section*
2832
  get_output_section() const
2833
  { return this->os_; }
2834
 
2835
  // Print for debugging.
2836
  void
2837
  print(FILE* f) const
2838
  {
2839
    fprintf(f, "  marker for orphaned output section %s\n",
2840
            this->os_->name());
2841
  }
2842
 
2843
 private:
2844
  Output_section* os_;
2845
};
2846
 
2847
// Set section addresses.
2848
 
2849
void
2850
Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
2851
                                             uint64_t* dot_value,
2852
                                             uint64_t*,
2853
                                             uint64_t* load_address)
2854
{
2855
  typedef std::list<Output_section::Input_section> Input_section_list;
2856
 
2857
  bool have_load_address = *load_address != *dot_value;
2858
 
2859
  uint64_t address = *dot_value;
2860
  address = align_address(address, this->os_->addralign());
2861
 
2862 148 khays
  // For a relocatable link, all orphan sections are put at
2863
  // address 0.  In general we expect all sections to be at
2864
  // address 0 for a relocatable link, but we permit the linker
2865
  // script to override that for specific output sections.
2866
  if (parameters->options().relocatable())
2867
    {
2868
      address = 0;
2869
      *load_address = 0;
2870
      have_load_address = false;
2871
    }
2872
 
2873 27 khays
  if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
2874
    {
2875
      this->os_->set_address(address);
2876
      if (have_load_address)
2877
        this->os_->set_load_address(align_address(*load_address,
2878
                                                  this->os_->addralign()));
2879
    }
2880
 
2881
  Input_section_list input_sections;
2882
  address += this->os_->get_input_sections(address, "", &input_sections);
2883
 
2884
  for (Input_section_list::iterator p = input_sections.begin();
2885
       p != input_sections.end();
2886
       ++p)
2887
    {
2888
      uint64_t addralign = p->addralign();
2889
      if (!p->is_input_section())
2890
        p->output_section_data()->finalize_data_size();
2891
      uint64_t size = p->data_size();
2892
      address = align_address(address, addralign);
2893
      this->os_->add_script_input_section(*p);
2894
      address += size;
2895
    }
2896
 
2897
  // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2898
  if (this->os_ == NULL
2899
      || (this->os_->flags() & elfcpp::SHF_TLS) == 0
2900
      || this->os_->type() != elfcpp::SHT_NOBITS)
2901
    {
2902
      if (!have_load_address)
2903
        *load_address = address;
2904
      else
2905
        *load_address += address - *dot_value;
2906
 
2907
      *dot_value = address;
2908
    }
2909
}
2910
 
2911
// Get the list of segments to use for an allocated section when using
2912
// a PHDRS clause.  If this is an allocated section, return the
2913
// Output_section.  We don't change the list of segments.
2914
 
2915
Output_section*
2916
Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
2917
{
2918
  if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2919
    return NULL;
2920
  *orphan = true;
2921
  return this->os_;
2922
}
2923
 
2924
// Class Phdrs_element.  A program header from a PHDRS clause.
2925
 
2926
class Phdrs_element
2927
{
2928
 public:
2929
  Phdrs_element(const char* name, size_t namelen, unsigned int type,
2930
                bool includes_filehdr, bool includes_phdrs,
2931
                bool is_flags_valid, unsigned int flags,
2932
                Expression* load_address)
2933
    : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2934
      includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2935
      flags_(flags), load_address_(load_address), load_address_value_(0),
2936
      segment_(NULL)
2937
  { }
2938
 
2939
  // Return the name of this segment.
2940
  const std::string&
2941
  name() const
2942
  { return this->name_; }
2943
 
2944
  // Return the type of the segment.
2945
  unsigned int
2946
  type() const
2947
  { return this->type_; }
2948
 
2949
  // Whether to include the file header.
2950
  bool
2951
  includes_filehdr() const
2952
  { return this->includes_filehdr_; }
2953
 
2954
  // Whether to include the program headers.
2955
  bool
2956
  includes_phdrs() const
2957
  { return this->includes_phdrs_; }
2958
 
2959
  // Return whether there is a load address.
2960
  bool
2961
  has_load_address() const
2962
  { return this->load_address_ != NULL; }
2963
 
2964
  // Evaluate the load address expression if there is one.
2965
  void
2966
  eval_load_address(Symbol_table* symtab, Layout* layout)
2967
  {
2968
    if (this->load_address_ != NULL)
2969
      this->load_address_value_ = this->load_address_->eval(symtab, layout,
2970
                                                            true);
2971
  }
2972
 
2973
  // Return the load address.
2974
  uint64_t
2975
  load_address() const
2976
  {
2977
    gold_assert(this->load_address_ != NULL);
2978
    return this->load_address_value_;
2979
  }
2980
 
2981
  // Create the segment.
2982
  Output_segment*
2983
  create_segment(Layout* layout)
2984
  {
2985
    this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2986
    return this->segment_;
2987
  }
2988
 
2989
  // Return the segment.
2990
  Output_segment*
2991
  segment()
2992
  { return this->segment_; }
2993
 
2994
  // Release the segment.
2995
  void
2996
  release_segment()
2997
  { this->segment_ = NULL; }
2998
 
2999
  // Set the segment flags if appropriate.
3000
  void
3001
  set_flags_if_valid()
3002
  {
3003
    if (this->is_flags_valid_)
3004
      this->segment_->set_flags(this->flags_);
3005
  }
3006
 
3007
  // Print for debugging.
3008
  void
3009
  print(FILE*) const;
3010
 
3011
 private:
3012
  // The name used in the script.
3013
  std::string name_;
3014
  // The type of the segment (PT_LOAD, etc.).
3015
  unsigned int type_;
3016
  // Whether this segment includes the file header.
3017
  bool includes_filehdr_;
3018
  // Whether this segment includes the section headers.
3019
  bool includes_phdrs_;
3020
  // Whether the flags were explicitly specified.
3021
  bool is_flags_valid_;
3022
  // The flags for this segment (PF_R, etc.) if specified.
3023
  unsigned int flags_;
3024
  // The expression for the load address for this segment.  This may
3025
  // be NULL.
3026
  Expression* load_address_;
3027
  // The actual load address from evaluating the expression.
3028
  uint64_t load_address_value_;
3029
  // The segment itself.
3030
  Output_segment* segment_;
3031
};
3032
 
3033
// Print for debugging.
3034
 
3035
void
3036
Phdrs_element::print(FILE* f) const
3037
{
3038
  fprintf(f, "  %s 0x%x", this->name_.c_str(), this->type_);
3039
  if (this->includes_filehdr_)
3040
    fprintf(f, " FILEHDR");
3041
  if (this->includes_phdrs_)
3042
    fprintf(f, " PHDRS");
3043
  if (this->is_flags_valid_)
3044
    fprintf(f, " FLAGS(%u)", this->flags_);
3045
  if (this->load_address_ != NULL)
3046
    {
3047
      fprintf(f, " AT(");
3048
      this->load_address_->print(f);
3049
      fprintf(f, ")");
3050
    }
3051
  fprintf(f, ";\n");
3052
}
3053
 
3054
// Add a memory region.
3055
 
3056
void
3057
Script_sections::add_memory_region(const char* name, size_t namelen,
3058
                                   unsigned int attributes,
3059
                                   Expression* start, Expression* length)
3060
{
3061
  if (this->memory_regions_ == NULL)
3062
    this->memory_regions_ = new Memory_regions();
3063
  else if (this->find_memory_region(name, namelen))
3064
    {
3065
      gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
3066
                  name);
3067
      // FIXME: Add a GOLD extension to allow multiple regions with the same
3068
      // name.  This would amount to a single region covering disjoint blocks
3069
      // of memory, which is useful for embedded devices.
3070
    }
3071
 
3072
  // FIXME: Check the length and start values.  Currently we allow
3073
  // non-constant expressions for these values, whereas LD does not.
3074
 
3075
  // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS.  This would
3076
  // describe a region that packs from the end address going down, rather
3077
  // than the start address going up.  This would be useful for embedded
3078
  // devices.
3079
 
3080
  this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
3081
                                                     start, length));
3082
}
3083
 
3084
// Find a memory region.
3085
 
3086
Memory_region*
3087
Script_sections::find_memory_region(const char* name, size_t namelen)
3088
{
3089
  if (this->memory_regions_ == NULL)
3090
    return NULL;
3091
 
3092
  for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3093
       m != this->memory_regions_->end();
3094
       ++m)
3095
    if ((*m)->name_match(name, namelen))
3096
      return *m;
3097
 
3098
  return NULL;
3099
}
3100
 
3101
// Find a memory region's origin.
3102
 
3103
Expression*
3104
Script_sections::find_memory_region_origin(const char* name, size_t namelen)
3105
{
3106
  Memory_region* mr = find_memory_region(name, namelen);
3107
  if (mr == NULL)
3108
    return NULL;
3109
 
3110
  return mr->start_address();
3111
}
3112
 
3113
// Find a memory region's length.
3114
 
3115
Expression*
3116
Script_sections::find_memory_region_length(const char* name, size_t namelen)
3117
{
3118
  Memory_region* mr = find_memory_region(name, namelen);
3119
  if (mr == NULL)
3120
    return NULL;
3121
 
3122
  return mr->length();
3123
}
3124
 
3125
// Set the memory region to use for the current section.
3126
 
3127
void
3128
Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
3129
{
3130
  gold_assert(!this->sections_elements_->empty());
3131
  this->sections_elements_->back()->set_memory_region(mr, set_vma);
3132
}
3133
 
3134
// Class Script_sections.
3135
 
3136
Script_sections::Script_sections()
3137
  : saw_sections_clause_(false),
3138
    in_sections_clause_(false),
3139
    sections_elements_(NULL),
3140
    output_section_(NULL),
3141
    memory_regions_(NULL),
3142
    phdrs_elements_(NULL),
3143
    orphan_section_placement_(NULL),
3144
    data_segment_align_start_(),
3145
    saw_data_segment_align_(false),
3146
    saw_relro_end_(false),
3147
    saw_segment_start_expression_(false)
3148
{
3149
}
3150
 
3151
// Start a SECTIONS clause.
3152
 
3153
void
3154
Script_sections::start_sections()
3155
{
3156
  gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
3157
  this->saw_sections_clause_ = true;
3158
  this->in_sections_clause_ = true;
3159
  if (this->sections_elements_ == NULL)
3160
    this->sections_elements_ = new Sections_elements;
3161
}
3162
 
3163
// Finish a SECTIONS clause.
3164
 
3165
void
3166
Script_sections::finish_sections()
3167
{
3168
  gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
3169
  this->in_sections_clause_ = false;
3170
}
3171
 
3172
// Add a symbol to be defined.
3173
 
3174
void
3175
Script_sections::add_symbol_assignment(const char* name, size_t length,
3176
                                       Expression* val, bool provide,
3177
                                       bool hidden)
3178
{
3179
  if (this->output_section_ != NULL)
3180
    this->output_section_->add_symbol_assignment(name, length, val,
3181
                                                 provide, hidden);
3182
  else
3183
    {
3184
      Sections_element* p = new Sections_element_assignment(name, length,
3185
                                                            val, provide,
3186
                                                            hidden);
3187
      this->sections_elements_->push_back(p);
3188
    }
3189
}
3190
 
3191
// Add an assignment to the special dot symbol.
3192
 
3193
void
3194
Script_sections::add_dot_assignment(Expression* val)
3195
{
3196
  if (this->output_section_ != NULL)
3197
    this->output_section_->add_dot_assignment(val);
3198
  else
3199
    {
3200
      // The GNU linker permits assignments to . to appears outside of
3201
      // a SECTIONS clause, and treats it as appearing inside, so
3202
      // sections_elements_ may be NULL here.
3203
      if (this->sections_elements_ == NULL)
3204
        {
3205
          this->sections_elements_ = new Sections_elements;
3206
          this->saw_sections_clause_ = true;
3207
        }
3208
 
3209
      Sections_element* p = new Sections_element_dot_assignment(val);
3210
      this->sections_elements_->push_back(p);
3211
    }
3212
}
3213
 
3214
// Add an assertion.
3215
 
3216
void
3217
Script_sections::add_assertion(Expression* check, const char* message,
3218
                               size_t messagelen)
3219
{
3220
  if (this->output_section_ != NULL)
3221
    this->output_section_->add_assertion(check, message, messagelen);
3222
  else
3223
    {
3224
      Sections_element* p = new Sections_element_assertion(check, message,
3225
                                                           messagelen);
3226
      this->sections_elements_->push_back(p);
3227
    }
3228
}
3229
 
3230
// Start processing entries for an output section.
3231
 
3232
void
3233
Script_sections::start_output_section(
3234
    const char* name,
3235
    size_t namelen,
3236
    const Parser_output_section_header* header)
3237
{
3238
  Output_section_definition* posd = new Output_section_definition(name,
3239
                                                                  namelen,
3240
                                                                  header);
3241
  this->sections_elements_->push_back(posd);
3242
  gold_assert(this->output_section_ == NULL);
3243
  this->output_section_ = posd;
3244
}
3245
 
3246
// Stop processing entries for an output section.
3247
 
3248
void
3249
Script_sections::finish_output_section(
3250
    const Parser_output_section_trailer* trailer)
3251
{
3252
  gold_assert(this->output_section_ != NULL);
3253
  this->output_section_->finish(trailer);
3254
  this->output_section_ = NULL;
3255
}
3256
 
3257
// Add a data item to the current output section.
3258
 
3259
void
3260
Script_sections::add_data(int size, bool is_signed, Expression* val)
3261
{
3262
  gold_assert(this->output_section_ != NULL);
3263
  this->output_section_->add_data(size, is_signed, val);
3264
}
3265
 
3266
// Add a fill value setting to the current output section.
3267
 
3268
void
3269
Script_sections::add_fill(Expression* val)
3270
{
3271
  gold_assert(this->output_section_ != NULL);
3272
  this->output_section_->add_fill(val);
3273
}
3274
 
3275
// Add an input section specification to the current output section.
3276
 
3277
void
3278
Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3279
{
3280
  gold_assert(this->output_section_ != NULL);
3281
  this->output_section_->add_input_section(spec, keep);
3282
}
3283
 
3284
// This is called when we see DATA_SEGMENT_ALIGN.  It means that any
3285
// subsequent output sections may be relro.
3286
 
3287
void
3288
Script_sections::data_segment_align()
3289
{
3290
  if (this->saw_data_segment_align_)
3291
    gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3292
  gold_assert(!this->sections_elements_->empty());
3293
  Sections_elements::iterator p = this->sections_elements_->end();
3294
  --p;
3295
  this->data_segment_align_start_ = p;
3296
  this->saw_data_segment_align_ = true;
3297
}
3298
 
3299
// This is called when we see DATA_SEGMENT_RELRO_END.  It means that
3300
// any output sections seen since DATA_SEGMENT_ALIGN are relro.
3301
 
3302
void
3303
Script_sections::data_segment_relro_end()
3304
{
3305
  if (this->saw_relro_end_)
3306
    gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3307
                 "in a linker script"));
3308
  this->saw_relro_end_ = true;
3309
 
3310
  if (!this->saw_data_segment_align_)
3311
    gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3312
  else
3313
    {
3314
      Sections_elements::iterator p = this->data_segment_align_start_;
3315
      for (++p; p != this->sections_elements_->end(); ++p)
3316
        (*p)->set_is_relro();
3317
    }
3318
}
3319
 
3320
// Create any required sections.
3321
 
3322
void
3323
Script_sections::create_sections(Layout* layout)
3324
{
3325
  if (!this->saw_sections_clause_)
3326
    return;
3327
  for (Sections_elements::iterator p = this->sections_elements_->begin();
3328
       p != this->sections_elements_->end();
3329
       ++p)
3330
    (*p)->create_sections(layout);
3331
}
3332
 
3333
// Add any symbols we are defining to the symbol table.
3334
 
3335
void
3336
Script_sections::add_symbols_to_table(Symbol_table* symtab)
3337
{
3338
  if (!this->saw_sections_clause_)
3339
    return;
3340
  for (Sections_elements::iterator p = this->sections_elements_->begin();
3341
       p != this->sections_elements_->end();
3342
       ++p)
3343
    (*p)->add_symbols_to_table(symtab);
3344
}
3345
 
3346
// Finalize symbols and check assertions.
3347
 
3348
void
3349
Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3350
{
3351
  if (!this->saw_sections_clause_)
3352
    return;
3353
  uint64_t dot_value = 0;
3354
  for (Sections_elements::iterator p = this->sections_elements_->begin();
3355
       p != this->sections_elements_->end();
3356
       ++p)
3357
    (*p)->finalize_symbols(symtab, layout, &dot_value);
3358
}
3359
 
3360
// Return the name of the output section to use for an input file name
3361
// and section name.
3362
 
3363
const char*
3364
Script_sections::output_section_name(
3365
    const char* file_name,
3366
    const char* section_name,
3367
    Output_section*** output_section_slot,
3368
    Script_sections::Section_type* psection_type)
3369
{
3370
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3371
       p != this->sections_elements_->end();
3372
       ++p)
3373
    {
3374
      const char* ret = (*p)->output_section_name(file_name, section_name,
3375
                                                  output_section_slot,
3376
                                                  psection_type);
3377
 
3378
      if (ret != NULL)
3379
        {
3380
          // The special name /DISCARD/ means that the input section
3381
          // should be discarded.
3382
          if (strcmp(ret, "/DISCARD/") == 0)
3383
            {
3384
              *output_section_slot = NULL;
3385
              *psection_type = Script_sections::ST_NONE;
3386
              return NULL;
3387
            }
3388
          return ret;
3389
        }
3390
    }
3391
 
3392
  // If we couldn't find a mapping for the name, the output section
3393
  // gets the name of the input section.
3394
 
3395
  *output_section_slot = NULL;
3396
  *psection_type = Script_sections::ST_NONE;
3397
 
3398
  return section_name;
3399
}
3400
 
3401
// Place a marker for an orphan output section into the SECTIONS
3402
// clause.
3403
 
3404
void
3405
Script_sections::place_orphan(Output_section* os)
3406
{
3407
  Orphan_section_placement* osp = this->orphan_section_placement_;
3408
  if (osp == NULL)
3409
    {
3410
      // Initialize the Orphan_section_placement structure.
3411
      osp = new Orphan_section_placement();
3412
      for (Sections_elements::iterator p = this->sections_elements_->begin();
3413
           p != this->sections_elements_->end();
3414
           ++p)
3415
        (*p)->orphan_section_init(osp, p);
3416
      gold_assert(!this->sections_elements_->empty());
3417
      Sections_elements::iterator last = this->sections_elements_->end();
3418
      --last;
3419
      osp->last_init(last);
3420
      this->orphan_section_placement_ = osp;
3421
    }
3422
 
3423
  Orphan_output_section* orphan = new Orphan_output_section(os);
3424
 
3425
  // Look for where to put ORPHAN.
3426
  Sections_elements::iterator* where;
3427
  if (osp->find_place(os, &where))
3428
    {
3429
      if ((**where)->is_relro())
3430
        os->set_is_relro();
3431
      else
3432
        os->clear_is_relro();
3433
 
3434
      // We want to insert ORPHAN after *WHERE, and then update *WHERE
3435
      // so that the next one goes after this one.
3436
      Sections_elements::iterator p = *where;
3437
      gold_assert(p != this->sections_elements_->end());
3438
      ++p;
3439
      *where = this->sections_elements_->insert(p, orphan);
3440
    }
3441
  else
3442
    {
3443
      os->clear_is_relro();
3444
      // We don't have a place to put this orphan section.  Put it,
3445
      // and all other sections like it, at the end, but before the
3446
      // sections which always come at the end.
3447
      Sections_elements::iterator last = osp->last_place();
3448
      *where = this->sections_elements_->insert(last, orphan);
3449
    }
3450
}
3451
 
3452
// Set the addresses of all the output sections.  Walk through all the
3453
// elements, tracking the dot symbol.  Apply assignments which set
3454
// absolute symbol values, in case they are used when setting dot.
3455
// Fill in data statement values.  As we find output sections, set the
3456
// address, set the address of all associated input sections, and
3457
// update dot.  Return the segment which should hold the file header
3458
// and segment headers, if any.
3459
 
3460
Output_segment*
3461
Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3462
{
3463
  gold_assert(this->saw_sections_clause_);
3464
 
3465
  // Implement ONLY_IF_RO/ONLY_IF_RW constraints.  These are a pain
3466
  // for our representation.
3467
  for (Sections_elements::iterator p = this->sections_elements_->begin();
3468
       p != this->sections_elements_->end();
3469
       ++p)
3470
    {
3471
      Output_section_definition* posd;
3472
      Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3473
      if (failed_constraint != CONSTRAINT_NONE)
3474
        {
3475
          Sections_elements::iterator q;
3476
          for (q = this->sections_elements_->begin();
3477
               q != this->sections_elements_->end();
3478
               ++q)
3479
            {
3480
              if (q != p)
3481
                {
3482
                  if ((*q)->alternate_constraint(posd, failed_constraint))
3483
                    break;
3484
                }
3485
            }
3486
 
3487
          if (q == this->sections_elements_->end())
3488
            gold_error(_("no matching section constraint"));
3489
        }
3490
    }
3491
 
3492
  // Force the alignment of the first TLS section to be the maximum
3493
  // alignment of all TLS sections.
3494
  Output_section* first_tls = NULL;
3495
  uint64_t tls_align = 0;
3496
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3497
       p != this->sections_elements_->end();
3498
       ++p)
3499
    {
3500
      Output_section* os = (*p)->get_output_section();
3501
      if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3502
        {
3503
          if (first_tls == NULL)
3504
            first_tls = os;
3505
          if (os->addralign() > tls_align)
3506
            tls_align = os->addralign();
3507
        }
3508
    }
3509
  if (first_tls != NULL)
3510
    first_tls->set_addralign(tls_align);
3511
 
3512
  // For a relocatable link, we implicitly set dot to zero.
3513
  uint64_t dot_value = 0;
3514
  uint64_t dot_alignment = 0;
3515
  uint64_t load_address = 0;
3516
 
3517
  // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3518
  // to set section addresses.  If the script has any SEGMENT_START
3519
  // expression, we do not set the section addresses.
3520
  bool use_tsection_options =
3521
    (!this->saw_segment_start_expression_
3522
     && (parameters->options().user_set_Ttext()
3523
         || parameters->options().user_set_Tdata()
3524
         || parameters->options().user_set_Tbss()));
3525
 
3526
  for (Sections_elements::iterator p = this->sections_elements_->begin();
3527
       p != this->sections_elements_->end();
3528
       ++p)
3529
    {
3530
      Output_section* os = (*p)->get_output_section();
3531
 
3532
      // Handle -Ttext, -Tdata and -Tbss options.  We do this by looking for
3533
      // the special sections by names and doing dot assignments. 
3534
      if (use_tsection_options
3535
          && os != NULL
3536
          && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3537
        {
3538
          uint64_t new_dot_value = dot_value;
3539
 
3540
          if (parameters->options().user_set_Ttext()
3541
              && strcmp(os->name(), ".text") == 0)
3542
            new_dot_value = parameters->options().Ttext();
3543
          else if (parameters->options().user_set_Tdata()
3544
              && strcmp(os->name(), ".data") == 0)
3545
            new_dot_value = parameters->options().Tdata();
3546
          else if (parameters->options().user_set_Tbss()
3547
              && strcmp(os->name(), ".bss") == 0)
3548
            new_dot_value = parameters->options().Tbss();
3549
 
3550
          // Update dot and load address if necessary.
3551
          if (new_dot_value < dot_value)
3552
            gold_error(_("dot may not move backward"));
3553
          else if (new_dot_value != dot_value)
3554
            {
3555
              dot_value = new_dot_value;
3556
              load_address = new_dot_value;
3557
            }
3558
        }
3559
 
3560
      (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3561
                                  &load_address);
3562
    }
3563
 
3564
  if (this->phdrs_elements_ != NULL)
3565
    {
3566
      for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3567
           p != this->phdrs_elements_->end();
3568
           ++p)
3569
        (*p)->eval_load_address(symtab, layout);
3570
    }
3571
 
3572
  return this->create_segments(layout, dot_alignment);
3573
}
3574
 
3575
// Sort the sections in order to put them into segments.
3576
 
3577
class Sort_output_sections
3578
{
3579
 public:
3580
  Sort_output_sections(const Script_sections::Sections_elements* elements)
3581
   : elements_(elements)
3582
  { }
3583
 
3584
  bool
3585
  operator()(const Output_section* os1, const Output_section* os2) const;
3586
 
3587
 private:
3588
  int
3589
  script_compare(const Output_section* os1, const Output_section* os2) const;
3590
 
3591
 private:
3592
  const Script_sections::Sections_elements* elements_;
3593
};
3594
 
3595
bool
3596
Sort_output_sections::operator()(const Output_section* os1,
3597
                                 const Output_section* os2) const
3598
{
3599
  // Sort first by the load address.
3600
  uint64_t lma1 = (os1->has_load_address()
3601
                   ? os1->load_address()
3602
                   : os1->address());
3603
  uint64_t lma2 = (os2->has_load_address()
3604
                   ? os2->load_address()
3605
                   : os2->address());
3606
  if (lma1 != lma2)
3607
    return lma1 < lma2;
3608
 
3609
  // Then sort by the virtual address.
3610
  if (os1->address() != os2->address())
3611
    return os1->address() < os2->address();
3612
 
3613
  // If the linker script says which of these sections is first, go
3614
  // with what it says.
3615
  int i = this->script_compare(os1, os2);
3616
  if (i != 0)
3617
    return i < 0;
3618
 
3619
  // Sort PROGBITS before NOBITS.
3620
  bool nobits1 = os1->type() == elfcpp::SHT_NOBITS;
3621
  bool nobits2 = os2->type() == elfcpp::SHT_NOBITS;
3622
  if (nobits1 != nobits2)
3623
    return nobits2;
3624
 
3625
  // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
3626
  // beginning.
3627
  bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3628
  bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3629
  if (tls1 != tls2)
3630
    return nobits1 ? tls1 : tls2;
3631
 
3632
  // Sort non-NOLOAD before NOLOAD.
3633
  if (os1->is_noload() && !os2->is_noload())
3634
    return true;
3635
  if (!os1->is_noload() && os2->is_noload())
3636
    return true;
3637
 
3638
  // The sections seem practically identical.  Sort by name to get a
3639
  // stable sort.
3640
  return os1->name() < os2->name();
3641
}
3642
 
3643
// Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
3644
// if either OS1 or OS2 is not mentioned.  This ensures that we keep
3645
// empty sections in the order in which they appear in a linker
3646
// script.
3647
 
3648
int
3649
Sort_output_sections::script_compare(const Output_section* os1,
3650
                                     const Output_section* os2) const
3651
{
3652
  if (this->elements_ == NULL)
3653
    return 0;
3654
 
3655
  bool found_os1 = false;
3656
  bool found_os2 = false;
3657
  for (Script_sections::Sections_elements::const_iterator
3658
         p = this->elements_->begin();
3659
       p != this->elements_->end();
3660
       ++p)
3661
    {
3662
      if (os2 == (*p)->get_output_section())
3663
        {
3664
          if (found_os1)
3665
            return -1;
3666
          found_os2 = true;
3667
        }
3668
      else if (os1 == (*p)->get_output_section())
3669
        {
3670
          if (found_os2)
3671
            return 1;
3672
          found_os1 = true;
3673
        }
3674
    }
3675
 
3676
  return 0;
3677
}
3678
 
3679
// Return whether OS is a BSS section.  This is a SHT_NOBITS section.
3680
// We treat a section with the SHF_TLS flag set as taking up space
3681
// even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3682
// space for them in the file.
3683
 
3684
bool
3685
Script_sections::is_bss_section(const Output_section* os)
3686
{
3687
  return (os->type() == elfcpp::SHT_NOBITS
3688
          && (os->flags() & elfcpp::SHF_TLS) == 0);
3689
}
3690
 
3691
// Return the size taken by the file header and the program headers.
3692
 
3693
size_t
3694
Script_sections::total_header_size(Layout* layout) const
3695
{
3696
  size_t segment_count = layout->segment_count();
3697
  size_t file_header_size;
3698
  size_t segment_headers_size;
3699
  if (parameters->target().get_size() == 32)
3700
    {
3701
      file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3702
      segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3703
    }
3704
  else if (parameters->target().get_size() == 64)
3705
    {
3706
      file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3707
      segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3708
    }
3709
  else
3710
    gold_unreachable();
3711
 
3712
  return file_header_size + segment_headers_size;
3713
}
3714
 
3715
// Return the amount we have to subtract from the LMA to accommodate
3716
// headers of the given size.  The complication is that the file
3717
// header have to be at the start of a page, as otherwise it will not
3718
// be at the start of the file.
3719
 
3720
uint64_t
3721
Script_sections::header_size_adjustment(uint64_t lma,
3722
                                        size_t sizeof_headers) const
3723
{
3724
  const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3725
  uint64_t hdr_lma = lma - sizeof_headers;
3726
  hdr_lma &= ~(abi_pagesize - 1);
3727
  return lma - hdr_lma;
3728
}
3729
 
3730
// Create the PT_LOAD segments when using a SECTIONS clause.  Returns
3731
// the segment which should hold the file header and segment headers,
3732
// if any.
3733
 
3734
Output_segment*
3735
Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
3736
{
3737
  gold_assert(this->saw_sections_clause_);
3738
 
3739
  if (parameters->options().relocatable())
3740
    return NULL;
3741
 
3742
  if (this->saw_phdrs_clause())
3743
    return create_segments_from_phdrs_clause(layout, dot_alignment);
3744
 
3745
  Layout::Section_list sections;
3746
  layout->get_allocated_sections(&sections);
3747
 
3748
  // Sort the sections by address.
3749
  std::stable_sort(sections.begin(), sections.end(),
3750
                   Sort_output_sections(this->sections_elements_));
3751
 
3752
  this->create_note_and_tls_segments(layout, &sections);
3753
 
3754
  // Walk through the sections adding them to PT_LOAD segments.
3755
  const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3756
  Output_segment* first_seg = NULL;
3757
  Output_segment* current_seg = NULL;
3758
  bool is_current_seg_readonly = true;
3759
  Layout::Section_list::iterator plast = sections.end();
3760
  uint64_t last_vma = 0;
3761
  uint64_t last_lma = 0;
3762
  uint64_t last_size = 0;
3763
  for (Layout::Section_list::iterator p = sections.begin();
3764
       p != sections.end();
3765
       ++p)
3766
    {
3767
      const uint64_t vma = (*p)->address();
3768
      const uint64_t lma = ((*p)->has_load_address()
3769
                            ? (*p)->load_address()
3770
                            : vma);
3771
      const uint64_t size = (*p)->current_data_size();
3772
 
3773
      bool need_new_segment;
3774
      if (current_seg == NULL)
3775
        need_new_segment = true;
3776
      else if (lma - vma != last_lma - last_vma)
3777
        {
3778
          // This section has a different LMA relationship than the
3779
          // last one; we need a new segment.
3780
          need_new_segment = true;
3781
        }
3782
      else if (align_address(last_lma + last_size, abi_pagesize)
3783
               < align_address(lma, abi_pagesize))
3784
        {
3785
          // Putting this section in the segment would require
3786
          // skipping a page.
3787
          need_new_segment = true;
3788
        }
3789
      else if (is_bss_section(*plast) && !is_bss_section(*p))
3790
        {
3791
          // A non-BSS section can not follow a BSS section in the
3792
          // same segment.
3793
          need_new_segment = true;
3794
        }
3795
      else if (is_current_seg_readonly
3796
               && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3797
               && !parameters->options().omagic())
3798
        {
3799
          // Don't put a writable section in the same segment as a
3800
          // non-writable section.
3801
          need_new_segment = true;
3802
        }
3803
      else
3804
        {
3805
          // Otherwise, reuse the existing segment.
3806
          need_new_segment = false;
3807
        }
3808
 
3809
      elfcpp::Elf_Word seg_flags =
3810
        Layout::section_flags_to_segment((*p)->flags());
3811
 
3812
      if (need_new_segment)
3813
        {
3814
          current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3815
                                                    seg_flags);
3816
          current_seg->set_addresses(vma, lma);
3817
          current_seg->set_minimum_p_align(dot_alignment);
3818
          if (first_seg == NULL)
3819
            first_seg = current_seg;
3820
          is_current_seg_readonly = true;
3821
        }
3822
 
3823
      current_seg->add_output_section_to_load(layout, *p, seg_flags);
3824
 
3825
      if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3826
        is_current_seg_readonly = false;
3827
 
3828
      plast = p;
3829
      last_vma = vma;
3830
      last_lma = lma;
3831
      last_size = size;
3832
    }
3833
 
3834
  // An ELF program should work even if the program headers are not in
3835
  // a PT_LOAD segment.  However, it appears that the Linux kernel
3836
  // does not set the AT_PHDR auxiliary entry in that case.  It sets
3837
  // the load address to p_vaddr - p_offset of the first PT_LOAD
3838
  // segment.  It then sets AT_PHDR to the load address plus the
3839
  // offset to the program headers, e_phoff in the file header.  This
3840
  // fails when the program headers appear in the file before the
3841
  // first PT_LOAD segment.  Therefore, we always create a PT_LOAD
3842
  // segment to hold the file header and the program headers.  This is
3843
  // effectively what the GNU linker does, and it is slightly more
3844
  // efficient in any case.  We try to use the first PT_LOAD segment
3845
  // if we can, otherwise we make a new one.
3846
 
3847
  if (first_seg == NULL)
3848
    return NULL;
3849
 
3850
  // -n or -N mean that the program is not demand paged and there is
3851
  // no need to put the program headers in a PT_LOAD segment.
3852
  if (parameters->options().nmagic() || parameters->options().omagic())
3853
    return NULL;
3854
 
3855
  size_t sizeof_headers = this->total_header_size(layout);
3856
 
3857
  uint64_t vma = first_seg->vaddr();
3858
  uint64_t lma = first_seg->paddr();
3859
 
3860
  uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3861
 
3862
  if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3863
    {
3864
      first_seg->set_addresses(vma - subtract, lma - subtract);
3865
      return first_seg;
3866
    }
3867
 
3868
  // If there is no room to squeeze in the headers, then punt.  The
3869
  // resulting executable probably won't run on GNU/Linux, but we
3870
  // trust that the user knows what they are doing.
3871
  if (lma < subtract || vma < subtract)
3872
    return NULL;
3873
 
3874
  // If memory regions have been specified and the address range
3875
  // we are about to use is not contained within any region then
3876
  // issue a warning message about the segment we are going to
3877
  // create.  It will be outside of any region and so possibly
3878
  // using non-existent or protected memory.  We test LMA rather
3879
  // than VMA since we assume that the headers will never be
3880
  // relocated.
3881
  if (this->memory_regions_ != NULL
3882
      && !this->block_in_region (NULL, layout, lma - subtract, subtract))
3883
    gold_warning(_("creating a segment to contain the file and program"
3884
                   " headers outside of any MEMORY region"));
3885
 
3886
  Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3887
                                                         elfcpp::PF_R);
3888
  load_seg->set_addresses(vma - subtract, lma - subtract);
3889
 
3890
  return load_seg;
3891
}
3892
 
3893
// Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3894
// segment if there are any SHT_TLS sections.
3895
 
3896
void
3897
Script_sections::create_note_and_tls_segments(
3898
    Layout* layout,
3899
    const Layout::Section_list* sections)
3900
{
3901
  gold_assert(!this->saw_phdrs_clause());
3902
 
3903
  bool saw_tls = false;
3904
  for (Layout::Section_list::const_iterator p = sections->begin();
3905
       p != sections->end();
3906
       ++p)
3907
    {
3908
      if ((*p)->type() == elfcpp::SHT_NOTE)
3909
        {
3910
          elfcpp::Elf_Word seg_flags =
3911
            Layout::section_flags_to_segment((*p)->flags());
3912
          Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3913
                                                             seg_flags);
3914
          oseg->add_output_section_to_nonload(*p, seg_flags);
3915
 
3916
          // Incorporate any subsequent SHT_NOTE sections, in the
3917
          // hopes that the script is sensible.
3918
          Layout::Section_list::const_iterator pnext = p + 1;
3919
          while (pnext != sections->end()
3920
                 && (*pnext)->type() == elfcpp::SHT_NOTE)
3921
            {
3922
              seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3923
              oseg->add_output_section_to_nonload(*pnext, seg_flags);
3924
              p = pnext;
3925
              ++pnext;
3926
            }
3927
        }
3928
 
3929
      if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3930
        {
3931
          if (saw_tls)
3932
            gold_error(_("TLS sections are not adjacent"));
3933
 
3934
          elfcpp::Elf_Word seg_flags =
3935
            Layout::section_flags_to_segment((*p)->flags());
3936
          Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3937
                                                             seg_flags);
3938
          oseg->add_output_section_to_nonload(*p, seg_flags);
3939
 
3940
          Layout::Section_list::const_iterator pnext = p + 1;
3941
          while (pnext != sections->end()
3942
                 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3943
            {
3944
              seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3945
              oseg->add_output_section_to_nonload(*pnext, seg_flags);
3946
              p = pnext;
3947
              ++pnext;
3948
            }
3949
 
3950
          saw_tls = true;
3951
        }
3952 159 khays
 
3953
      // If we are making a shared library, and we see a section named
3954
      // .interp then put the .interp section in a PT_INTERP segment.
3955
      // This is for GNU ld compatibility.
3956
      if (strcmp((*p)->name(), ".interp") == 0)
3957
        {
3958
          elfcpp::Elf_Word seg_flags =
3959
            Layout::section_flags_to_segment((*p)->flags());
3960
          Output_segment* oseg = layout->make_output_segment(elfcpp::PT_INTERP,
3961
                                                             seg_flags);
3962
          oseg->add_output_section_to_nonload(*p, seg_flags);
3963
        }
3964 27 khays
    }
3965
}
3966
 
3967
// Add a program header.  The PHDRS clause is syntactically distinct
3968
// from the SECTIONS clause, but we implement it with the SECTIONS
3969
// support because PHDRS is useless if there is no SECTIONS clause.
3970
 
3971
void
3972
Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3973
                          bool includes_filehdr, bool includes_phdrs,
3974
                          bool is_flags_valid, unsigned int flags,
3975
                          Expression* load_address)
3976
{
3977
  if (this->phdrs_elements_ == NULL)
3978
    this->phdrs_elements_ = new Phdrs_elements();
3979
  this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3980
                                                     includes_filehdr,
3981
                                                     includes_phdrs,
3982
                                                     is_flags_valid, flags,
3983
                                                     load_address));
3984
}
3985
 
3986
// Return the number of segments we expect to create based on the
3987
// SECTIONS clause.  This is used to implement SIZEOF_HEADERS.
3988
 
3989
size_t
3990
Script_sections::expected_segment_count(const Layout* layout) const
3991
{
3992
  if (this->saw_phdrs_clause())
3993
    return this->phdrs_elements_->size();
3994
 
3995
  Layout::Section_list sections;
3996
  layout->get_allocated_sections(&sections);
3997
 
3998
  // We assume that we will need two PT_LOAD segments.
3999
  size_t ret = 2;
4000
 
4001
  bool saw_note = false;
4002
  bool saw_tls = false;
4003
  for (Layout::Section_list::const_iterator p = sections.begin();
4004
       p != sections.end();
4005
       ++p)
4006
    {
4007
      if ((*p)->type() == elfcpp::SHT_NOTE)
4008
        {
4009
          // Assume that all note sections will fit into a single
4010
          // PT_NOTE segment.
4011
          if (!saw_note)
4012
            {
4013
              ++ret;
4014
              saw_note = true;
4015
            }
4016
        }
4017
      else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
4018
        {
4019
          // There can only be one PT_TLS segment.
4020
          if (!saw_tls)
4021
            {
4022
              ++ret;
4023
              saw_tls = true;
4024
            }
4025
        }
4026
    }
4027
 
4028
  return ret;
4029
}
4030
 
4031
// Create the segments from a PHDRS clause.  Return the segment which
4032
// should hold the file header and program headers, if any.
4033
 
4034
Output_segment*
4035
Script_sections::create_segments_from_phdrs_clause(Layout* layout,
4036
                                                   uint64_t dot_alignment)
4037
{
4038
  this->attach_sections_using_phdrs_clause(layout);
4039
  return this->set_phdrs_clause_addresses(layout, dot_alignment);
4040
}
4041
 
4042
// Create the segments from the PHDRS clause, and put the output
4043
// sections in them.
4044
 
4045
void
4046
Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
4047
{
4048
  typedef std::map<std::string, Output_segment*> Name_to_segment;
4049
  Name_to_segment name_to_segment;
4050
  for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4051
       p != this->phdrs_elements_->end();
4052
       ++p)
4053
    name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
4054
 
4055
  // Walk through the output sections and attach them to segments.
4056
  // Output sections in the script which do not list segments are
4057
  // attached to the same set of segments as the immediately preceding
4058
  // output section.
4059
 
4060
  String_list* phdr_names = NULL;
4061
  bool load_segments_only = false;
4062
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4063
       p != this->sections_elements_->end();
4064
       ++p)
4065
    {
4066 159 khays
      bool is_orphan;
4067 27 khays
      String_list* old_phdr_names = phdr_names;
4068 159 khays
      Output_section* os = (*p)->allocate_to_segment(&phdr_names, &is_orphan);
4069 27 khays
      if (os == NULL)
4070
        continue;
4071
 
4072 159 khays
      elfcpp::Elf_Word seg_flags =
4073
        Layout::section_flags_to_segment(os->flags());
4074
 
4075 27 khays
      if (phdr_names == NULL)
4076
        {
4077 159 khays
          // Don't worry about empty orphan sections.
4078
          if (is_orphan && os->current_data_size() > 0)
4079
            gold_error(_("allocated section %s not in any segment"),
4080
                       os->name());
4081
 
4082
          // To avoid later crashes drop this section into the first
4083
          // PT_LOAD segment.
4084
          for (Phdrs_elements::const_iterator ppe =
4085
                 this->phdrs_elements_->begin();
4086
               ppe != this->phdrs_elements_->end();
4087
               ++ppe)
4088
            {
4089
              Output_segment* oseg = (*ppe)->segment();
4090
              if (oseg->type() == elfcpp::PT_LOAD)
4091
                {
4092
                  oseg->add_output_section_to_load(layout, os, seg_flags);
4093
                  break;
4094
                }
4095
            }
4096
 
4097 27 khays
          continue;
4098
        }
4099
 
4100
      // We see a list of segments names.  Disable PT_LOAD segment only
4101
      // filtering.
4102
      if (old_phdr_names != phdr_names)
4103
        load_segments_only = false;
4104
 
4105
      // If this is an orphan section--one that was not explicitly
4106
      // mentioned in the linker script--then it should not inherit
4107
      // any segment type other than PT_LOAD.  Otherwise, e.g., the
4108
      // PT_INTERP segment will pick up following orphan sections,
4109
      // which does not make sense.  If this is not an orphan section,
4110
      // we trust the linker script.
4111 159 khays
      if (is_orphan)
4112 27 khays
        {
4113
          // Enable PT_LOAD segments only filtering until we see another
4114
          // list of segment names.
4115
          load_segments_only = true;
4116
        }
4117
 
4118
      bool in_load_segment = false;
4119
      for (String_list::const_iterator q = phdr_names->begin();
4120
           q != phdr_names->end();
4121
           ++q)
4122
        {
4123
          Name_to_segment::const_iterator r = name_to_segment.find(*q);
4124
          if (r == name_to_segment.end())
4125
            gold_error(_("no segment %s"), q->c_str());
4126
          else
4127
            {
4128
              if (load_segments_only
4129
                  && r->second->type() != elfcpp::PT_LOAD)
4130
                continue;
4131
 
4132
              if (r->second->type() != elfcpp::PT_LOAD)
4133
                r->second->add_output_section_to_nonload(os, seg_flags);
4134
              else
4135
                {
4136
                  r->second->add_output_section_to_load(layout, os, seg_flags);
4137
                  if (in_load_segment)
4138
                    gold_error(_("section in two PT_LOAD segments"));
4139
                  in_load_segment = true;
4140
                }
4141
            }
4142
        }
4143
 
4144
      if (!in_load_segment)
4145
        gold_error(_("allocated section not in any PT_LOAD segment"));
4146
    }
4147
}
4148
 
4149
// Set the addresses for segments created from a PHDRS clause.  Return
4150
// the segment which should hold the file header and program headers,
4151
// if any.
4152
 
4153
Output_segment*
4154
Script_sections::set_phdrs_clause_addresses(Layout* layout,
4155
                                            uint64_t dot_alignment)
4156
{
4157
  Output_segment* load_seg = NULL;
4158
  for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4159
       p != this->phdrs_elements_->end();
4160
       ++p)
4161
    {
4162
      // Note that we have to set the flags after adding the output
4163
      // sections to the segment, as adding an output segment can
4164
      // change the flags.
4165
      (*p)->set_flags_if_valid();
4166
 
4167
      Output_segment* oseg = (*p)->segment();
4168
 
4169
      if (oseg->type() != elfcpp::PT_LOAD)
4170
        {
4171
          // The addresses of non-PT_LOAD segments are set from the
4172
          // PT_LOAD segments.
4173
          if ((*p)->has_load_address())
4174
            gold_error(_("may only specify load address for PT_LOAD segment"));
4175
          continue;
4176
        }
4177
 
4178
      oseg->set_minimum_p_align(dot_alignment);
4179
 
4180
      // The output sections should have addresses from the SECTIONS
4181
      // clause.  The addresses don't have to be in order, so find the
4182
      // one with the lowest load address.  Use that to set the
4183
      // address of the segment.
4184
 
4185
      Output_section* osec = oseg->section_with_lowest_load_address();
4186
      if (osec == NULL)
4187
        {
4188
          oseg->set_addresses(0, 0);
4189
          continue;
4190
        }
4191
 
4192
      uint64_t vma = osec->address();
4193
      uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
4194
 
4195
      // Override the load address of the section with the load
4196
      // address specified for the segment.
4197
      if ((*p)->has_load_address())
4198
        {
4199
          if (osec->has_load_address())
4200
            gold_warning(_("PHDRS load address overrides "
4201
                           "section %s load address"),
4202
                         osec->name());
4203
 
4204
          lma = (*p)->load_address();
4205
        }
4206
 
4207
      bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
4208
      if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
4209
        {
4210
          // We could support this if we wanted to.
4211
          gold_error(_("using only one of FILEHDR and PHDRS is "
4212
                       "not currently supported"));
4213
        }
4214
      if (headers)
4215
        {
4216
          size_t sizeof_headers = this->total_header_size(layout);
4217
          uint64_t subtract = this->header_size_adjustment(lma,
4218
                                                           sizeof_headers);
4219
          if (lma >= subtract && vma >= subtract)
4220
            {
4221
              lma -= subtract;
4222
              vma -= subtract;
4223
            }
4224
          else
4225
            {
4226
              gold_error(_("sections loaded on first page without room "
4227
                           "for file and program headers "
4228
                           "are not supported"));
4229
            }
4230
 
4231
          if (load_seg != NULL)
4232
            gold_error(_("using FILEHDR and PHDRS on more than one "
4233
                         "PT_LOAD segment is not currently supported"));
4234
          load_seg = oseg;
4235
        }
4236
 
4237
      oseg->set_addresses(vma, lma);
4238
    }
4239
 
4240
  return load_seg;
4241
}
4242
 
4243
// Add the file header and segment headers to non-load segments
4244
// specified in the PHDRS clause.
4245
 
4246
void
4247
Script_sections::put_headers_in_phdrs(Output_data* file_header,
4248
                                      Output_data* segment_headers)
4249
{
4250
  gold_assert(this->saw_phdrs_clause());
4251
  for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
4252
       p != this->phdrs_elements_->end();
4253
       ++p)
4254
    {
4255
      if ((*p)->type() != elfcpp::PT_LOAD)
4256
        {
4257
          if ((*p)->includes_phdrs())
4258
            (*p)->segment()->add_initial_output_data(segment_headers);
4259
          if ((*p)->includes_filehdr())
4260
            (*p)->segment()->add_initial_output_data(file_header);
4261
        }
4262
    }
4263
}
4264
 
4265
// Look for an output section by name and return the address, the load
4266
// address, the alignment, and the size.  This is used when an
4267
// expression refers to an output section which was not actually
4268
// created.  This returns true if the section was found, false
4269
// otherwise.
4270
 
4271
bool
4272
Script_sections::get_output_section_info(const char* name, uint64_t* address,
4273
                                         uint64_t* load_address,
4274
                                         uint64_t* addralign,
4275
                                         uint64_t* size) const
4276
{
4277
  if (!this->saw_sections_clause_)
4278
    return false;
4279
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4280
       p != this->sections_elements_->end();
4281
       ++p)
4282
    if ((*p)->get_output_section_info(name, address, load_address, addralign,
4283
                                      size))
4284
      return true;
4285
  return false;
4286
}
4287
 
4288
// Release all Output_segments.  This remove all pointers to all
4289
// Output_segments.
4290
 
4291
void
4292
Script_sections::release_segments()
4293
{
4294
  if (this->saw_phdrs_clause())
4295
    {
4296
      for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4297
           p != this->phdrs_elements_->end();
4298
           ++p)
4299
        (*p)->release_segment();
4300
    }
4301
}
4302
 
4303
// Print the SECTIONS clause to F for debugging.
4304
 
4305
void
4306
Script_sections::print(FILE* f) const
4307
{
4308
  if (this->phdrs_elements_ != NULL)
4309
    {
4310
      fprintf(f, "PHDRS {\n");
4311
      for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4312
           p != this->phdrs_elements_->end();
4313
           ++p)
4314
        (*p)->print(f);
4315
      fprintf(f, "}\n");
4316
    }
4317
 
4318
  if (this->memory_regions_ != NULL)
4319
    {
4320
      fprintf(f, "MEMORY {\n");
4321
      for (Memory_regions::const_iterator m = this->memory_regions_->begin();
4322
           m != this->memory_regions_->end();
4323
           ++m)
4324
        (*m)->print(f);
4325
      fprintf(f, "}\n");
4326
    }
4327
 
4328
  if (!this->saw_sections_clause_)
4329
    return;
4330
 
4331
  fprintf(f, "SECTIONS {\n");
4332
 
4333
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4334
       p != this->sections_elements_->end();
4335
       ++p)
4336
    (*p)->print(f);
4337
 
4338
  fprintf(f, "}\n");
4339
}
4340
 
4341
} // End namespace gold.

powered by: WebSVN 2.1.0

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