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

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.20.1/] [gold/] [script-sections.cc] - Blame information for rev 373

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

Line No. Rev Author Line
1 205 julius
// script-sections.cc -- linker script SECTIONS for gold
2
 
3
// Copyright 2008, 2009 Free Software Foundation, Inc.
4
// Written by Ian Lance Taylor <iant@google.com>.
5
 
6
// This file is part of gold.
7
 
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License as published by
10
// the Free Software Foundation; either version 3 of the License, or
11
// (at your option) any later version.
12
 
13
// This program is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
// GNU General Public License for more details.
17
 
18
// You should have received a copy of the GNU General Public License
19
// along with this program; if not, write to the Free Software
20
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
// MA 02110-1301, USA.
22
 
23
#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
// Manage orphan sections.  This is intended to be largely compatible
47
// with the GNU linker.  The Linux kernel implicitly relies on
48
// something similar to the GNU linker's orphan placement.  We
49
// originally used a simpler scheme here, but it caused the kernel
50
// build to fail, and was also rather inefficient.
51
 
52
class Orphan_section_placement
53
{
54
 private:
55
  typedef Script_sections::Elements_iterator Elements_iterator;
56
 
57
 public:
58
  Orphan_section_placement();
59
 
60
  // Handle an output section during initialization of this mapping.
61
  void
62
  output_section_init(const std::string& name, Output_section*,
63
                      Elements_iterator location);
64
 
65
  // Initialize the last location.
66
  void
67
  last_init(Elements_iterator location);
68
 
69
  // Set *PWHERE to the address of an iterator pointing to the
70
  // location to use for an orphan section.  Return true if the
71
  // iterator has a value, false otherwise.
72
  bool
73
  find_place(Output_section*, Elements_iterator** pwhere);
74
 
75
  // Return the iterator being used for sections at the very end of
76
  // the linker script.
77
  Elements_iterator
78
  last_place() const;
79
 
80
 private:
81
  // The places that we specifically recognize.  This list is copied
82
  // from the GNU linker.
83
  enum Place_index
84
  {
85
    PLACE_TEXT,
86
    PLACE_RODATA,
87
    PLACE_DATA,
88
    PLACE_BSS,
89
    PLACE_REL,
90
    PLACE_INTERP,
91
    PLACE_NONALLOC,
92
    PLACE_LAST,
93
    PLACE_MAX
94
  };
95
 
96
  // The information we keep for a specific place.
97
  struct Place
98
  {
99
    // The name of sections for this place.
100
    const char* name;
101
    // Whether we have a location for this place.
102
    bool have_location;
103
    // The iterator for this place.
104
    Elements_iterator location;
105
  };
106
 
107
  // Initialize one place element.
108
  void
109
  initialize_place(Place_index, const char*);
110
 
111
  // The places.
112
  Place places_[PLACE_MAX];
113
  // True if this is the first call to output_section_init.
114
  bool first_init_;
115
};
116
 
117
// Initialize Orphan_section_placement.
118
 
119
Orphan_section_placement::Orphan_section_placement()
120
  : first_init_(true)
121
{
122
  this->initialize_place(PLACE_TEXT, ".text");
123
  this->initialize_place(PLACE_RODATA, ".rodata");
124
  this->initialize_place(PLACE_DATA, ".data");
125
  this->initialize_place(PLACE_BSS, ".bss");
126
  this->initialize_place(PLACE_REL, NULL);
127
  this->initialize_place(PLACE_INTERP, ".interp");
128
  this->initialize_place(PLACE_NONALLOC, NULL);
129
  this->initialize_place(PLACE_LAST, NULL);
130
}
131
 
132
// Initialize one place element.
133
 
134
void
135
Orphan_section_placement::initialize_place(Place_index index, const char* name)
136
{
137
  this->places_[index].name = name;
138
  this->places_[index].have_location = false;
139
}
140
 
141
// While initializing the Orphan_section_placement information, this
142
// is called once for each output section named in the linker script.
143
// If we found an output section during the link, it will be passed in
144
// OS.
145
 
146
void
147
Orphan_section_placement::output_section_init(const std::string& name,
148
                                              Output_section* os,
149
                                              Elements_iterator location)
150
{
151
  bool first_init = this->first_init_;
152
  this->first_init_ = false;
153
 
154
  for (int i = 0; i < PLACE_MAX; ++i)
155
    {
156
      if (this->places_[i].name != NULL && this->places_[i].name == name)
157
        {
158
          if (this->places_[i].have_location)
159
            {
160
              // We have already seen a section with this name.
161
              return;
162
            }
163
 
164
          this->places_[i].location = location;
165
          this->places_[i].have_location = true;
166
 
167
          // If we just found the .bss section, restart the search for
168
          // an unallocated section.  This follows the GNU linker's
169
          // behaviour.
170
          if (i == PLACE_BSS)
171
            this->places_[PLACE_NONALLOC].have_location = false;
172
 
173
          return;
174
        }
175
    }
176
 
177
  // Relocation sections.
178
  if (!this->places_[PLACE_REL].have_location
179
      && os != NULL
180
      && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
181
      && (os->flags() & elfcpp::SHF_ALLOC) != 0)
182
    {
183
      this->places_[PLACE_REL].location = location;
184
      this->places_[PLACE_REL].have_location = true;
185
    }
186
 
187
  // We find the location for unallocated sections by finding the
188
  // first debugging or comment section after the BSS section (if
189
  // there is one).
190
  if (!this->places_[PLACE_NONALLOC].have_location
191
      && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
192
    {
193
      // We add orphan sections after the location in PLACES_.  We
194
      // want to store unallocated sections before LOCATION.  If this
195
      // is the very first section, we can't use it.
196
      if (!first_init)
197
        {
198
          --location;
199
          this->places_[PLACE_NONALLOC].location = location;
200
          this->places_[PLACE_NONALLOC].have_location = true;
201
        }
202
    }
203
}
204
 
205
// Initialize the last location.
206
 
207
void
208
Orphan_section_placement::last_init(Elements_iterator location)
209
{
210
  this->places_[PLACE_LAST].location = location;
211
  this->places_[PLACE_LAST].have_location = true;
212
}
213
 
214
// Set *PWHERE to the address of an iterator pointing to the location
215
// to use for an orphan section.  Return true if the iterator has a
216
// value, false otherwise.
217
 
218
bool
219
Orphan_section_placement::find_place(Output_section* os,
220
                                     Elements_iterator** pwhere)
221
{
222
  // Figure out where OS should go.  This is based on the GNU linker
223
  // code.  FIXME: The GNU linker handles small data sections
224
  // specially, but we don't.
225
  elfcpp::Elf_Word type = os->type();
226
  elfcpp::Elf_Xword flags = os->flags();
227
  Place_index index;
228
  if ((flags & elfcpp::SHF_ALLOC) == 0
229
      && !Layout::is_debug_info_section(os->name()))
230
    index = PLACE_NONALLOC;
231
  else if ((flags & elfcpp::SHF_ALLOC) == 0)
232
    index = PLACE_LAST;
233
  else if (type == elfcpp::SHT_NOTE)
234
    index = PLACE_INTERP;
235
  else if (type == elfcpp::SHT_NOBITS)
236
    index = PLACE_BSS;
237
  else if ((flags & elfcpp::SHF_WRITE) != 0)
238
    index = PLACE_DATA;
239
  else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
240
    index = PLACE_REL;
241
  else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
242
    index = PLACE_RODATA;
243
  else
244
    index = PLACE_TEXT;
245
 
246
  // If we don't have a location yet, try to find one based on a
247
  // plausible ordering of sections.
248
  if (!this->places_[index].have_location)
249
    {
250
      Place_index follow;
251
      switch (index)
252
        {
253
        default:
254
          follow = PLACE_MAX;
255
          break;
256
        case PLACE_RODATA:
257
          follow = PLACE_TEXT;
258
          break;
259
        case PLACE_BSS:
260
          follow = PLACE_DATA;
261
          break;
262
        case PLACE_REL:
263
          follow = PLACE_TEXT;
264
          break;
265
        case PLACE_INTERP:
266
          follow = PLACE_TEXT;
267
          break;
268
        }
269
      if (follow != PLACE_MAX && this->places_[follow].have_location)
270
        {
271
          // Set the location of INDEX to the location of FOLLOW.  The
272
          // location of INDEX will then be incremented by the caller,
273
          // so anything in INDEX will continue to be after anything
274
          // in FOLLOW.
275
          this->places_[index].location = this->places_[follow].location;
276
          this->places_[index].have_location = true;
277
        }
278
    }
279
 
280
  *pwhere = &this->places_[index].location;
281
  bool ret = this->places_[index].have_location;
282
 
283
  // The caller will set the location.
284
  this->places_[index].have_location = true;
285
 
286
  return ret;
287
}
288
 
289
// Return the iterator being used for sections at the very end of the
290
// linker script.
291
 
292
Orphan_section_placement::Elements_iterator
293
Orphan_section_placement::last_place() const
294
{
295
  gold_assert(this->places_[PLACE_LAST].have_location);
296
  return this->places_[PLACE_LAST].location;
297
}
298
 
299
// An element in a SECTIONS clause.
300
 
301
class Sections_element
302
{
303
 public:
304
  Sections_element()
305
  { }
306
 
307
  virtual ~Sections_element()
308
  { }
309
 
310
  // Return whether an output section is relro.
311
  virtual bool
312
  is_relro() const
313
  { return false; }
314
 
315
  // Record that an output section is relro.
316
  virtual void
317
  set_is_relro()
318
  { }
319
 
320
  // Create any required output sections.  The only real
321
  // implementation is in Output_section_definition.
322
  virtual void
323
  create_sections(Layout*)
324
  { }
325
 
326
  // Add any symbol being defined to the symbol table.
327
  virtual void
328
  add_symbols_to_table(Symbol_table*)
329
  { }
330
 
331
  // Finalize symbols and check assertions.
332
  virtual void
333
  finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
334
  { }
335
 
336
  // Return the output section name to use for an input file name and
337
  // section name.  This only real implementation is in
338
  // Output_section_definition.
339
  virtual const char*
340
  output_section_name(const char*, const char*, Output_section***)
341
  { return NULL; }
342
 
343
  // Initialize OSP with an output section.
344
  virtual void
345
  orphan_section_init(Orphan_section_placement*,
346
                      Script_sections::Elements_iterator)
347
  { }
348
 
349
  // Set section addresses.  This includes applying assignments if the
350
  // the expression is an absolute value.
351
  virtual void
352
  set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*)
353
  { }
354
 
355
  // Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
356
  // this section is constrained, and the input sections do not match,
357
  // return the constraint, and set *POSD.
358
  virtual Section_constraint
359
  check_constraint(Output_section_definition**)
360
  { return CONSTRAINT_NONE; }
361
 
362
  // See if this is the alternate output section for a constrained
363
  // output section.  If it is, transfer the Output_section and return
364
  // true.  Otherwise return false.
365
  virtual bool
366
  alternate_constraint(Output_section_definition*, Section_constraint)
367
  { return false; }
368
 
369
  // Get the list of segments to use for an allocated section when
370
  // using a PHDRS clause.  If this is an allocated section, return
371
  // the Output_section, and set *PHDRS_LIST (the first parameter) to
372
  // the list of PHDRS to which it should be attached.  If the PHDRS
373
  // were not specified, don't change *PHDRS_LIST.  When not returning
374
  // NULL, set *ORPHAN (the second parameter) according to whether
375
  // this is an orphan section--one that is not mentioned in the
376
  // linker script.
377
  virtual Output_section*
378
  allocate_to_segment(String_list**, bool*)
379
  { return NULL; }
380
 
381
  // Look for an output section by name and return the address, the
382
  // load address, the alignment, and the size.  This is used when an
383
  // expression refers to an output section which was not actually
384
  // created.  This returns true if the section was found, false
385
  // otherwise.  The only real definition is for
386
  // Output_section_definition.
387
  virtual bool
388
  get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
389
                          uint64_t*) const
390
  { return false; }
391
 
392
  // Return the associated Output_section if there is one.
393
  virtual Output_section*
394
  get_output_section() const
395
  { return NULL; }
396
 
397
  // Print the element for debugging purposes.
398
  virtual void
399
  print(FILE* f) const = 0;
400
};
401
 
402
// An assignment in a SECTIONS clause outside of an output section.
403
 
404
class Sections_element_assignment : public Sections_element
405
{
406
 public:
407
  Sections_element_assignment(const char* name, size_t namelen,
408
                              Expression* val, bool provide, bool hidden)
409
    : assignment_(name, namelen, val, provide, hidden)
410
  { }
411
 
412
  // Add the symbol to the symbol table.
413
  void
414
  add_symbols_to_table(Symbol_table* symtab)
415
  { this->assignment_.add_to_table(symtab); }
416
 
417
  // Finalize the symbol.
418
  void
419
  finalize_symbols(Symbol_table* symtab, const Layout* layout,
420
                   uint64_t* dot_value)
421
  {
422
    this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
423
  }
424
 
425
  // Set the section address.  There is no section here, but if the
426
  // value is absolute, we set the symbol.  This permits us to use
427
  // absolute symbols when setting dot.
428
  void
429
  set_section_addresses(Symbol_table* symtab, Layout* layout,
430
                        uint64_t* dot_value, uint64_t*)
431
  {
432
    this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
433
  }
434
 
435
  // Print for debugging.
436
  void
437
  print(FILE* f) const
438
  {
439
    fprintf(f, "  ");
440
    this->assignment_.print(f);
441
  }
442
 
443
 private:
444
  Symbol_assignment assignment_;
445
};
446
 
447
// An assignment to the dot symbol in a SECTIONS clause outside of an
448
// output section.
449
 
450
class Sections_element_dot_assignment : public Sections_element
451
{
452
 public:
453
  Sections_element_dot_assignment(Expression* val)
454
    : val_(val)
455
  { }
456
 
457
  // Finalize the symbol.
458
  void
459
  finalize_symbols(Symbol_table* symtab, const Layout* layout,
460
                   uint64_t* dot_value)
461
  {
462
    // We ignore the section of the result because outside of an
463
    // output section definition the dot symbol is always considered
464
    // to be absolute.
465
    Output_section* dummy;
466
    *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
467
                                           NULL, &dummy);
468
  }
469
 
470
  // Update the dot symbol while setting section addresses.
471
  void
472
  set_section_addresses(Symbol_table* symtab, Layout* layout,
473
                        uint64_t* dot_value, uint64_t* load_address)
474
  {
475
    Output_section* dummy;
476
    *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
477
                                           NULL, &dummy);
478
    *load_address = *dot_value;
479
  }
480
 
481
  // Print for debugging.
482
  void
483
  print(FILE* f) const
484
  {
485
    fprintf(f, "  . = ");
486
    this->val_->print(f);
487
    fprintf(f, "\n");
488
  }
489
 
490
 private:
491
  Expression* val_;
492
};
493
 
494
// An assertion in a SECTIONS clause outside of an output section.
495
 
496
class Sections_element_assertion : public Sections_element
497
{
498
 public:
499
  Sections_element_assertion(Expression* check, const char* message,
500
                             size_t messagelen)
501
    : assertion_(check, message, messagelen)
502
  { }
503
 
504
  // Check the assertion.
505
  void
506
  finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
507
  { this->assertion_.check(symtab, layout); }
508
 
509
  // Print for debugging.
510
  void
511
  print(FILE* f) const
512
  {
513
    fprintf(f, "  ");
514
    this->assertion_.print(f);
515
  }
516
 
517
 private:
518
  Script_assertion assertion_;
519
};
520
 
521
// An element in an output section in a SECTIONS clause.
522
 
523
class Output_section_element
524
{
525
 public:
526
  // A list of input sections.
527
  typedef std::list<Output_section::Simple_input_section> Input_section_list;
528
 
529
  Output_section_element()
530
  { }
531
 
532
  virtual ~Output_section_element()
533
  { }
534
 
535
  // Return whether this element requires an output section to exist.
536
  virtual bool
537
  needs_output_section() const
538
  { return false; }
539
 
540
  // Add any symbol being defined to the symbol table.
541
  virtual void
542
  add_symbols_to_table(Symbol_table*)
543
  { }
544
 
545
  // Finalize symbols and check assertions.
546
  virtual void
547
  finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
548
  { }
549
 
550
  // Return whether this element matches FILE_NAME and SECTION_NAME.
551
  // The only real implementation is in Output_section_element_input.
552
  virtual bool
553
  match_name(const char*, const char*) const
554
  { return false; }
555
 
556
  // Set section addresses.  This includes applying assignments if the
557
  // the expression is an absolute value.
558
  virtual void
559
  set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
560
                        uint64_t*, Output_section**, std::string*,
561
                        Input_section_list*)
562
  { }
563
 
564
  // Print the element for debugging purposes.
565
  virtual void
566
  print(FILE* f) const = 0;
567
 
568
 protected:
569
  // Return a fill string that is LENGTH bytes long, filling it with
570
  // FILL.
571
  std::string
572
  get_fill_string(const std::string* fill, section_size_type length) const;
573
};
574
 
575
std::string
576
Output_section_element::get_fill_string(const std::string* fill,
577
                                        section_size_type length) const
578
{
579
  std::string this_fill;
580
  this_fill.reserve(length);
581
  while (this_fill.length() + fill->length() <= length)
582
    this_fill += *fill;
583
  if (this_fill.length() < length)
584
    this_fill.append(*fill, 0, length - this_fill.length());
585
  return this_fill;
586
}
587
 
588
// A symbol assignment in an output section.
589
 
590
class Output_section_element_assignment : public Output_section_element
591
{
592
 public:
593
  Output_section_element_assignment(const char* name, size_t namelen,
594
                                    Expression* val, bool provide,
595
                                    bool hidden)
596
    : assignment_(name, namelen, val, provide, hidden)
597
  { }
598
 
599
  // Add the symbol to the symbol table.
600
  void
601
  add_symbols_to_table(Symbol_table* symtab)
602
  { this->assignment_.add_to_table(symtab); }
603
 
604
  // Finalize the symbol.
605
  void
606
  finalize_symbols(Symbol_table* symtab, const Layout* layout,
607
                   uint64_t* dot_value, Output_section** dot_section)
608
  {
609
    this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
610
                                        *dot_section);
611
  }
612
 
613
  // Set the section address.  There is no section here, but if the
614
  // value is absolute, we set the symbol.  This permits us to use
615
  // absolute symbols when setting dot.
616
  void
617
  set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
618
                        uint64_t, uint64_t* dot_value, Output_section**,
619
                        std::string*, Input_section_list*)
620
  {
621
    this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
622
  }
623
 
624
  // Print for debugging.
625
  void
626
  print(FILE* f) const
627
  {
628
    fprintf(f, "    ");
629
    this->assignment_.print(f);
630
  }
631
 
632
 private:
633
  Symbol_assignment assignment_;
634
};
635
 
636
// An assignment to the dot symbol in an output section.
637
 
638
class Output_section_element_dot_assignment : public Output_section_element
639
{
640
 public:
641
  Output_section_element_dot_assignment(Expression* val)
642
    : val_(val)
643
  { }
644
 
645
  // Finalize the symbol.
646
  void
647
  finalize_symbols(Symbol_table* symtab, const Layout* layout,
648
                   uint64_t* dot_value, Output_section** dot_section)
649
  {
650
    *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
651
                                           *dot_section, dot_section);
652
  }
653
 
654
  // Update the dot symbol while setting section addresses.
655
  void
656
  set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
657
                        uint64_t, uint64_t* dot_value, Output_section**,
658
                        std::string*, Input_section_list*);
659
 
660
  // Print for debugging.
661
  void
662
  print(FILE* f) const
663
  {
664
    fprintf(f, "    . = ");
665
    this->val_->print(f);
666
    fprintf(f, "\n");
667
  }
668
 
669
 private:
670
  Expression* val_;
671
};
672
 
673
// Update the dot symbol while setting section addresses.
674
 
675
void
676
Output_section_element_dot_assignment::set_section_addresses(
677
    Symbol_table* symtab,
678
    Layout* layout,
679
    Output_section* output_section,
680
    uint64_t,
681
    uint64_t* dot_value,
682
    Output_section** dot_section,
683
    std::string* fill,
684
    Input_section_list*)
685
{
686
  uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
687
                                                *dot_value, *dot_section,
688
                                                dot_section);
689
  if (next_dot < *dot_value)
690
    gold_error(_("dot may not move backward"));
691
  if (next_dot > *dot_value && output_section != NULL)
692
    {
693
      section_size_type length = convert_to_section_size_type(next_dot
694
                                                              - *dot_value);
695
      Output_section_data* posd;
696
      if (fill->empty())
697
        posd = new Output_data_zero_fill(length, 0);
698
      else
699
        {
700
          std::string this_fill = this->get_fill_string(fill, length);
701
          posd = new Output_data_const(this_fill, 0);
702
        }
703
      output_section->add_output_section_data(posd);
704
      layout->new_output_section_data_from_script(posd);
705
    }
706
  *dot_value = next_dot;
707
}
708
 
709
// An assertion in an output section.
710
 
711
class Output_section_element_assertion : public Output_section_element
712
{
713
 public:
714
  Output_section_element_assertion(Expression* check, const char* message,
715
                                   size_t messagelen)
716
    : assertion_(check, message, messagelen)
717
  { }
718
 
719
  void
720
  print(FILE* f) const
721
  {
722
    fprintf(f, "    ");
723
    this->assertion_.print(f);
724
  }
725
 
726
 private:
727
  Script_assertion assertion_;
728
};
729
 
730
// We use a special instance of Output_section_data to handle BYTE,
731
// SHORT, etc.  This permits forward references to symbols in the
732
// expressions.
733
 
734
class Output_data_expression : public Output_section_data
735
{
736
 public:
737
  Output_data_expression(int size, bool is_signed, Expression* val,
738
                         const Symbol_table* symtab, const Layout* layout,
739
                         uint64_t dot_value, Output_section* dot_section)
740
    : Output_section_data(size, 0, true),
741
      is_signed_(is_signed), val_(val), symtab_(symtab),
742
      layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
743
  { }
744
 
745
 protected:
746
  // Write the data to the output file.
747
  void
748
  do_write(Output_file*);
749
 
750
  // Write the data to a buffer.
751
  void
752
  do_write_to_buffer(unsigned char*);
753
 
754
  // Write to a map file.
755
  void
756
  do_print_to_mapfile(Mapfile* mapfile) const
757
  { mapfile->print_output_data(this, _("** expression")); }
758
 
759
 private:
760
  template<bool big_endian>
761
  void
762
  endian_write_to_buffer(uint64_t, unsigned char*);
763
 
764
  bool is_signed_;
765
  Expression* val_;
766
  const Symbol_table* symtab_;
767
  const Layout* layout_;
768
  uint64_t dot_value_;
769
  Output_section* dot_section_;
770
};
771
 
772
// Write the data element to the output file.
773
 
774
void
775
Output_data_expression::do_write(Output_file* of)
776
{
777
  unsigned char* view = of->get_output_view(this->offset(), this->data_size());
778
  this->write_to_buffer(view);
779
  of->write_output_view(this->offset(), this->data_size(), view);
780
}
781
 
782
// Write the data element to a buffer.
783
 
784
void
785
Output_data_expression::do_write_to_buffer(unsigned char* buf)
786
{
787
  Output_section* dummy;
788
  uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
789
                                           true, this->dot_value_,
790
                                           this->dot_section_, &dummy);
791
 
792
  if (parameters->target().is_big_endian())
793
    this->endian_write_to_buffer<true>(val, buf);
794
  else
795
    this->endian_write_to_buffer<false>(val, buf);
796
}
797
 
798
template<bool big_endian>
799
void
800
Output_data_expression::endian_write_to_buffer(uint64_t val,
801
                                               unsigned char* buf)
802
{
803
  switch (this->data_size())
804
    {
805
    case 1:
806
      elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
807
      break;
808
    case 2:
809
      elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
810
      break;
811
    case 4:
812
      elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
813
      break;
814
    case 8:
815
      if (parameters->target().get_size() == 32)
816
        {
817
          val &= 0xffffffff;
818
          if (this->is_signed_ && (val & 0x80000000) != 0)
819
            val |= 0xffffffff00000000LL;
820
        }
821
      elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
822
      break;
823
    default:
824
      gold_unreachable();
825
    }
826
}
827
 
828
// A data item in an output section.
829
 
830
class Output_section_element_data : public Output_section_element
831
{
832
 public:
833
  Output_section_element_data(int size, bool is_signed, Expression* val)
834
    : size_(size), is_signed_(is_signed), val_(val)
835
  { }
836
 
837
  // If there is a data item, then we must create an output section.
838
  bool
839
  needs_output_section() const
840
  { return true; }
841
 
842
  // Finalize symbols--we just need to update dot.
843
  void
844
  finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
845
                   Output_section**)
846
  { *dot_value += this->size_; }
847
 
848
  // Store the value in the section.
849
  void
850
  set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
851
                        uint64_t* dot_value, Output_section**, std::string*,
852
                        Input_section_list*);
853
 
854
  // Print for debugging.
855
  void
856
  print(FILE*) const;
857
 
858
 private:
859
  // The size in bytes.
860
  int size_;
861
  // Whether the value is signed.
862
  bool is_signed_;
863
  // The value.
864
  Expression* val_;
865
};
866
 
867
// Store the value in the section.
868
 
869
void
870
Output_section_element_data::set_section_addresses(
871
    Symbol_table* symtab,
872
    Layout* layout,
873
    Output_section* os,
874
    uint64_t,
875
    uint64_t* dot_value,
876
    Output_section** dot_section,
877
    std::string*,
878
    Input_section_list*)
879
{
880
  gold_assert(os != NULL);
881
  Output_data_expression* expression =
882
    new Output_data_expression(this->size_, this->is_signed_, this->val_,
883
                               symtab, layout, *dot_value, *dot_section);
884
  os->add_output_section_data(expression);
885
  layout->new_output_section_data_from_script(expression);
886
  *dot_value += this->size_;
887
}
888
 
889
// Print for debugging.
890
 
891
void
892
Output_section_element_data::print(FILE* f) const
893
{
894
  const char* s;
895
  switch (this->size_)
896
    {
897
    case 1:
898
      s = "BYTE";
899
      break;
900
    case 2:
901
      s = "SHORT";
902
      break;
903
    case 4:
904
      s = "LONG";
905
      break;
906
    case 8:
907
      if (this->is_signed_)
908
        s = "SQUAD";
909
      else
910
        s = "QUAD";
911
      break;
912
    default:
913
      gold_unreachable();
914
    }
915
  fprintf(f, "    %s(", s);
916
  this->val_->print(f);
917
  fprintf(f, ")\n");
918
}
919
 
920
// A fill value setting in an output section.
921
 
922
class Output_section_element_fill : public Output_section_element
923
{
924
 public:
925
  Output_section_element_fill(Expression* val)
926
    : val_(val)
927
  { }
928
 
929
  // Update the fill value while setting section addresses.
930
  void
931
  set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
932
                        uint64_t, uint64_t* dot_value,
933
                        Output_section** dot_section,
934
                        std::string* fill, Input_section_list*)
935
  {
936
    Output_section* fill_section;
937
    uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
938
                                                  *dot_value, *dot_section,
939
                                                  &fill_section);
940
    if (fill_section != NULL)
941
      gold_warning(_("fill value is not absolute"));
942
    // FIXME: The GNU linker supports fill values of arbitrary length.
943
    unsigned char fill_buff[4];
944
    elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
945
    fill->assign(reinterpret_cast<char*>(fill_buff), 4);
946
  }
947
 
948
  // Print for debugging.
949
  void
950
  print(FILE* f) const
951
  {
952
    fprintf(f, "    FILL(");
953
    this->val_->print(f);
954
    fprintf(f, ")\n");
955
  }
956
 
957
 private:
958
  // The new fill value.
959
  Expression* val_;
960
};
961
 
962
// Return whether STRING contains a wildcard character.  This is used
963
// to speed up matching.
964
 
965
static inline bool
966
is_wildcard_string(const std::string& s)
967
{
968
  return strpbrk(s.c_str(), "?*[") != NULL;
969
}
970
 
971
// An input section specification in an output section
972
 
973
class Output_section_element_input : public Output_section_element
974
{
975
 public:
976
  Output_section_element_input(const Input_section_spec* spec, bool keep);
977
 
978
  // Finalize symbols--just update the value of the dot symbol.
979
  void
980
  finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
981
                   Output_section** dot_section)
982
  {
983
    *dot_value = this->final_dot_value_;
984
    *dot_section = this->final_dot_section_;
985
  }
986
 
987
  // See whether we match FILE_NAME and SECTION_NAME as an input
988
  // section.
989
  bool
990
  match_name(const char* file_name, const char* section_name) const;
991
 
992
  // Set the section address.
993
  void
994
  set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
995
                        uint64_t subalign, uint64_t* dot_value,
996
                        Output_section**, std::string* fill,
997
                        Input_section_list*);
998
 
999
  // Print for debugging.
1000
  void
1001
  print(FILE* f) const;
1002
 
1003
 private:
1004
  // An input section pattern.
1005
  struct Input_section_pattern
1006
  {
1007
    std::string pattern;
1008
    bool pattern_is_wildcard;
1009
    Sort_wildcard sort;
1010
 
1011
    Input_section_pattern(const char* patterna, size_t patternlena,
1012
                          Sort_wildcard sorta)
1013
      : pattern(patterna, patternlena),
1014
        pattern_is_wildcard(is_wildcard_string(this->pattern)),
1015
        sort(sorta)
1016
    { }
1017
  };
1018
 
1019
  typedef std::vector<Input_section_pattern> Input_section_patterns;
1020
 
1021
  // Filename_exclusions is a pair of filename pattern and a bool
1022
  // indicating whether the filename is a wildcard.
1023
  typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1024
 
1025
  // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1026
  // indicates whether this is a wildcard pattern.
1027
  static inline bool
1028
  match(const char* string, const char* pattern, bool is_wildcard_pattern)
1029
  {
1030
    return (is_wildcard_pattern
1031
            ? fnmatch(pattern, string, 0) == 0
1032
            : strcmp(string, pattern) == 0);
1033
  }
1034
 
1035
  // See if we match a file name.
1036
  bool
1037
  match_file_name(const char* file_name) const;
1038
 
1039
  // The file name pattern.  If this is the empty string, we match all
1040
  // files.
1041
  std::string filename_pattern_;
1042
  // Whether the file name pattern is a wildcard.
1043
  bool filename_is_wildcard_;
1044
  // How the file names should be sorted.  This may only be
1045
  // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1046
  Sort_wildcard filename_sort_;
1047
  // The list of file names to exclude.
1048
  Filename_exclusions filename_exclusions_;
1049
  // The list of input section patterns.
1050
  Input_section_patterns input_section_patterns_;
1051
  // Whether to keep this section when garbage collecting.
1052
  bool keep_;
1053
  // The value of dot after including all matching sections.
1054
  uint64_t final_dot_value_;
1055
  // The section where dot is defined after including all matching
1056
  // sections.
1057
  Output_section* final_dot_section_;
1058
};
1059
 
1060
// Construct Output_section_element_input.  The parser records strings
1061
// as pointers into a copy of the script file, which will go away when
1062
// parsing is complete.  We make sure they are in std::string objects.
1063
 
1064
Output_section_element_input::Output_section_element_input(
1065
    const Input_section_spec* spec,
1066
    bool keep)
1067
  : filename_pattern_(),
1068
    filename_is_wildcard_(false),
1069
    filename_sort_(spec->file.sort),
1070
    filename_exclusions_(),
1071
    input_section_patterns_(),
1072
    keep_(keep),
1073
    final_dot_value_(0),
1074
    final_dot_section_(NULL)
1075
{
1076
  // The filename pattern "*" is common, and matches all files.  Turn
1077
  // it into the empty string.
1078
  if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1079
    this->filename_pattern_.assign(spec->file.name.value,
1080
                                   spec->file.name.length);
1081
  this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_);
1082
 
1083
  if (spec->input_sections.exclude != NULL)
1084
    {
1085
      for (String_list::const_iterator p =
1086
             spec->input_sections.exclude->begin();
1087
           p != spec->input_sections.exclude->end();
1088
           ++p)
1089
        {
1090
          bool is_wildcard = is_wildcard_string(*p);
1091
          this->filename_exclusions_.push_back(std::make_pair(*p,
1092
                                                              is_wildcard));
1093
        }
1094
    }
1095
 
1096
  if (spec->input_sections.sections != NULL)
1097
    {
1098
      Input_section_patterns& isp(this->input_section_patterns_);
1099
      for (String_sort_list::const_iterator p =
1100
             spec->input_sections.sections->begin();
1101
           p != spec->input_sections.sections->end();
1102
           ++p)
1103
        isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1104
                                            p->sort));
1105
    }
1106
}
1107
 
1108
// See whether we match FILE_NAME.
1109
 
1110
bool
1111
Output_section_element_input::match_file_name(const char* file_name) const
1112
{
1113
  if (!this->filename_pattern_.empty())
1114
    {
1115
      // If we were called with no filename, we refuse to match a
1116
      // pattern which requires a file name.
1117
      if (file_name == NULL)
1118
        return false;
1119
 
1120
      if (!match(file_name, this->filename_pattern_.c_str(),
1121
                 this->filename_is_wildcard_))
1122
        return false;
1123
    }
1124
 
1125
  if (file_name != NULL)
1126
    {
1127
      // Now we have to see whether FILE_NAME matches one of the
1128
      // exclusion patterns, if any.
1129
      for (Filename_exclusions::const_iterator p =
1130
             this->filename_exclusions_.begin();
1131
           p != this->filename_exclusions_.end();
1132
           ++p)
1133
        {
1134
          if (match(file_name, p->first.c_str(), p->second))
1135
            return false;
1136
        }
1137
    }
1138
 
1139
  return true;
1140
}
1141
 
1142
// See whether we match FILE_NAME and SECTION_NAME.
1143
 
1144
bool
1145
Output_section_element_input::match_name(const char* file_name,
1146
                                         const char* section_name) const
1147
{
1148
  if (!this->match_file_name(file_name))
1149
    return false;
1150
 
1151
  // If there are no section name patterns, then we match.
1152
  if (this->input_section_patterns_.empty())
1153
    return true;
1154
 
1155
  // See whether we match the section name patterns.
1156
  for (Input_section_patterns::const_iterator p =
1157
         this->input_section_patterns_.begin();
1158
       p != this->input_section_patterns_.end();
1159
       ++p)
1160
    {
1161
      if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1162
        return true;
1163
    }
1164
 
1165
  // We didn't match any section names, so we didn't match.
1166
  return false;
1167
}
1168
 
1169
// Information we use to sort the input sections.
1170
 
1171
class Input_section_info
1172
{
1173
 public:
1174
  Input_section_info(const Output_section::Simple_input_section& input_section)
1175
    : input_section_(input_section), section_name_(),
1176
      size_(0), addralign_(1)
1177
  { }
1178
 
1179
  // Return the simple input section.
1180
  const Output_section::Simple_input_section&
1181
  input_section() const
1182
  { return this->input_section_; }
1183
 
1184
  // Return the object.
1185
  Relobj*
1186
  relobj() const
1187
  { return this->input_section_.relobj(); }
1188
 
1189
  // Return the section index.
1190
  unsigned int
1191
  shndx()
1192
  { return this->input_section_.shndx(); }
1193
 
1194
  // Return the section name.
1195
  const std::string&
1196
  section_name() const
1197
  { return this->section_name_; }
1198
 
1199
  // Set the section name.
1200
  void
1201
  set_section_name(const std::string name)
1202
  { this->section_name_ = name; }
1203
 
1204
  // Return the section size.
1205
  uint64_t
1206
  size() const
1207
  { return this->size_; }
1208
 
1209
  // Set the section size.
1210
  void
1211
  set_size(uint64_t size)
1212
  { this->size_ = size; }
1213
 
1214
  // Return the address alignment.
1215
  uint64_t
1216
  addralign() const
1217
  { return this->addralign_; }
1218
 
1219
  // Set the address alignment.
1220
  void
1221
  set_addralign(uint64_t addralign)
1222
  { this->addralign_ = addralign; }
1223
 
1224
 private:
1225
  // Input section, can be a relaxed section.
1226
  Output_section::Simple_input_section input_section_;
1227
  // Name of the section. 
1228
  std::string section_name_;
1229
  // Section size.
1230
  uint64_t size_;
1231
  // Address alignment.
1232
  uint64_t addralign_;
1233
};
1234
 
1235
// A class to sort the input sections.
1236
 
1237
class Input_section_sorter
1238
{
1239
 public:
1240
  Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1241
    : filename_sort_(filename_sort), section_sort_(section_sort)
1242
  { }
1243
 
1244
  bool
1245
  operator()(const Input_section_info&, const Input_section_info&) const;
1246
 
1247
 private:
1248
  Sort_wildcard filename_sort_;
1249
  Sort_wildcard section_sort_;
1250
};
1251
 
1252
bool
1253
Input_section_sorter::operator()(const Input_section_info& isi1,
1254
                                 const Input_section_info& isi2) const
1255
{
1256
  if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1257
      || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1258
      || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1259
          && isi1.addralign() == isi2.addralign()))
1260
    {
1261
      if (isi1.section_name() != isi2.section_name())
1262
        return isi1.section_name() < isi2.section_name();
1263
    }
1264
  if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1265
      || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1266
      || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1267
    {
1268
      if (isi1.addralign() != isi2.addralign())
1269
        return isi1.addralign() < isi2.addralign();
1270
    }
1271
  if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1272
    {
1273
      if (isi1.relobj()->name() != isi2.relobj()->name())
1274
        return (isi1.relobj()->name() < isi2.relobj()->name());
1275
    }
1276
 
1277
  // Otherwise we leave them in the same order.
1278
  return false;
1279
}
1280
 
1281
// Set the section address.  Look in INPUT_SECTIONS for sections which
1282
// match this spec, sort them as specified, and add them to the output
1283
// section.
1284
 
1285
void
1286
Output_section_element_input::set_section_addresses(
1287
    Symbol_table*,
1288
    Layout* layout,
1289
    Output_section* output_section,
1290
    uint64_t subalign,
1291
    uint64_t* dot_value,
1292
    Output_section** dot_section,
1293
    std::string* fill,
1294
    Input_section_list* input_sections)
1295
{
1296
  // We build a list of sections which match each
1297
  // Input_section_pattern.
1298
 
1299
  typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1300
  size_t input_pattern_count = this->input_section_patterns_.size();
1301
  if (input_pattern_count == 0)
1302
    input_pattern_count = 1;
1303
  Matching_sections matching_sections(input_pattern_count);
1304
 
1305
  // Look through the list of sections for this output section.  Add
1306
  // each one which matches to one of the elements of
1307
  // MATCHING_SECTIONS.
1308
 
1309
  Input_section_list::iterator p = input_sections->begin();
1310
  while (p != input_sections->end())
1311
    {
1312
      Relobj* relobj = p->relobj();
1313
      unsigned int shndx = p->shndx();
1314
      Input_section_info isi(*p);
1315
 
1316
      // Calling section_name and section_addralign is not very
1317
      // efficient.
1318
 
1319
      // Lock the object so that we can get information about the
1320
      // section.  This is OK since we know we are single-threaded
1321
      // here.
1322
      {
1323
        const Task* task = reinterpret_cast<const Task*>(-1);
1324
        Task_lock_obj<Object> tl(task, relobj);
1325
 
1326
        isi.set_section_name(relobj->section_name(shndx));
1327
        if (p->is_relaxed_input_section())
1328
          {
1329
            // We use current data size because relxed section sizes may not
1330
            // have finalized yet.
1331
            isi.set_size(p->relaxed_input_section()->current_data_size());
1332
            isi.set_addralign(p->relaxed_input_section()->addralign());
1333
          }
1334
        else
1335
          {
1336
            isi.set_size(relobj->section_size(shndx));
1337
            isi.set_addralign(relobj->section_addralign(shndx));
1338
          }
1339
      }
1340
 
1341
      if (!this->match_file_name(relobj->name().c_str()))
1342
        ++p;
1343
      else if (this->input_section_patterns_.empty())
1344
        {
1345
          matching_sections[0].push_back(isi);
1346
          p = input_sections->erase(p);
1347
        }
1348
      else
1349
        {
1350
          size_t i;
1351
          for (i = 0; i < input_pattern_count; ++i)
1352
            {
1353
              const Input_section_pattern&
1354
                isp(this->input_section_patterns_[i]);
1355
              if (match(isi.section_name().c_str(), isp.pattern.c_str(),
1356
                        isp.pattern_is_wildcard))
1357
                break;
1358
            }
1359
 
1360
          if (i >= this->input_section_patterns_.size())
1361
            ++p;
1362
          else
1363
            {
1364
              matching_sections[i].push_back(isi);
1365
              p = input_sections->erase(p);
1366
            }
1367
        }
1368
    }
1369
 
1370
  // Look through MATCHING_SECTIONS.  Sort each one as specified,
1371
  // using a stable sort so that we get the default order when
1372
  // sections are otherwise equal.  Add each input section to the
1373
  // output section.
1374
 
1375
  uint64_t dot = *dot_value;
1376
  for (size_t i = 0; i < input_pattern_count; ++i)
1377
    {
1378
      if (matching_sections[i].empty())
1379
        continue;
1380
 
1381
      gold_assert(output_section != NULL);
1382
 
1383
      const Input_section_pattern& isp(this->input_section_patterns_[i]);
1384
      if (isp.sort != SORT_WILDCARD_NONE
1385
          || this->filename_sort_ != SORT_WILDCARD_NONE)
1386
        std::stable_sort(matching_sections[i].begin(),
1387
                         matching_sections[i].end(),
1388
                         Input_section_sorter(this->filename_sort_,
1389
                                              isp.sort));
1390
 
1391
      for (std::vector<Input_section_info>::const_iterator p =
1392
             matching_sections[i].begin();
1393
           p != matching_sections[i].end();
1394
           ++p)
1395
        {
1396
          uint64_t this_subalign = p->addralign();
1397
          if (this_subalign < subalign)
1398
            this_subalign = subalign;
1399
 
1400
          uint64_t address = align_address(dot, this_subalign);
1401
 
1402
          if (address > dot && !fill->empty())
1403
            {
1404
              section_size_type length =
1405
                convert_to_section_size_type(address - dot);
1406
              std::string this_fill = this->get_fill_string(fill, length);
1407
              Output_section_data* posd = new Output_data_const(this_fill, 0);
1408
              output_section->add_output_section_data(posd);
1409
              layout->new_output_section_data_from_script(posd);
1410
            }
1411
 
1412
          output_section->add_input_section_for_script(p->input_section(),
1413
                                                       p->size(),
1414
                                                       this_subalign);
1415
 
1416
          dot = address + p->size();
1417
        }
1418
    }
1419
 
1420
  // An SHF_TLS/SHT_NOBITS section does not take up any
1421
  // address space.
1422
  if (output_section == NULL
1423
      || (output_section->flags() & elfcpp::SHF_TLS) == 0
1424
      || output_section->type() != elfcpp::SHT_NOBITS)
1425
    *dot_value = dot;
1426
 
1427
  this->final_dot_value_ = *dot_value;
1428
  this->final_dot_section_ = *dot_section;
1429
}
1430
 
1431
// Print for debugging.
1432
 
1433
void
1434
Output_section_element_input::print(FILE* f) const
1435
{
1436
  fprintf(f, "    ");
1437
 
1438
  if (this->keep_)
1439
    fprintf(f, "KEEP(");
1440
 
1441
  if (!this->filename_pattern_.empty())
1442
    {
1443
      bool need_close_paren = false;
1444
      switch (this->filename_sort_)
1445
        {
1446
        case SORT_WILDCARD_NONE:
1447
          break;
1448
        case SORT_WILDCARD_BY_NAME:
1449
          fprintf(f, "SORT_BY_NAME(");
1450
          need_close_paren = true;
1451
          break;
1452
        default:
1453
          gold_unreachable();
1454
        }
1455
 
1456
      fprintf(f, "%s", this->filename_pattern_.c_str());
1457
 
1458
      if (need_close_paren)
1459
        fprintf(f, ")");
1460
    }
1461
 
1462
  if (!this->input_section_patterns_.empty()
1463
      || !this->filename_exclusions_.empty())
1464
    {
1465
      fprintf(f, "(");
1466
 
1467
      bool need_space = false;
1468
      if (!this->filename_exclusions_.empty())
1469
        {
1470
          fprintf(f, "EXCLUDE_FILE(");
1471
          bool need_comma = false;
1472
          for (Filename_exclusions::const_iterator p =
1473
                 this->filename_exclusions_.begin();
1474
               p != this->filename_exclusions_.end();
1475
               ++p)
1476
            {
1477
              if (need_comma)
1478
                fprintf(f, ", ");
1479
              fprintf(f, "%s", p->first.c_str());
1480
              need_comma = true;
1481
            }
1482
          fprintf(f, ")");
1483
          need_space = true;
1484
        }
1485
 
1486
      for (Input_section_patterns::const_iterator p =
1487
             this->input_section_patterns_.begin();
1488
           p != this->input_section_patterns_.end();
1489
           ++p)
1490
        {
1491
          if (need_space)
1492
            fprintf(f, " ");
1493
 
1494
          int close_parens = 0;
1495
          switch (p->sort)
1496
            {
1497
            case SORT_WILDCARD_NONE:
1498
              break;
1499
            case SORT_WILDCARD_BY_NAME:
1500
              fprintf(f, "SORT_BY_NAME(");
1501
              close_parens = 1;
1502
              break;
1503
            case SORT_WILDCARD_BY_ALIGNMENT:
1504
              fprintf(f, "SORT_BY_ALIGNMENT(");
1505
              close_parens = 1;
1506
              break;
1507
            case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1508
              fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1509
              close_parens = 2;
1510
              break;
1511
            case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1512
              fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1513
              close_parens = 2;
1514
              break;
1515
            default:
1516
              gold_unreachable();
1517
            }
1518
 
1519
          fprintf(f, "%s", p->pattern.c_str());
1520
 
1521
          for (int i = 0; i < close_parens; ++i)
1522
            fprintf(f, ")");
1523
 
1524
          need_space = true;
1525
        }
1526
 
1527
      fprintf(f, ")");
1528
    }
1529
 
1530
  if (this->keep_)
1531
    fprintf(f, ")");
1532
 
1533
  fprintf(f, "\n");
1534
}
1535
 
1536
// An output section.
1537
 
1538
class Output_section_definition : public Sections_element
1539
{
1540
 public:
1541
  typedef Output_section_element::Input_section_list Input_section_list;
1542
 
1543
  Output_section_definition(const char* name, size_t namelen,
1544
                            const Parser_output_section_header* header);
1545
 
1546
  // Finish the output section with the information in the trailer.
1547
  void
1548
  finish(const Parser_output_section_trailer* trailer);
1549
 
1550
  // Add a symbol to be defined.
1551
  void
1552
  add_symbol_assignment(const char* name, size_t length, Expression* value,
1553
                        bool provide, bool hidden);
1554
 
1555
  // Add an assignment to the special dot symbol.
1556
  void
1557
  add_dot_assignment(Expression* value);
1558
 
1559
  // Add an assertion.
1560
  void
1561
  add_assertion(Expression* check, const char* message, size_t messagelen);
1562
 
1563
  // Add a data item to the current output section.
1564
  void
1565
  add_data(int size, bool is_signed, Expression* val);
1566
 
1567
  // Add a setting for the fill value.
1568
  void
1569
  add_fill(Expression* val);
1570
 
1571
  // Add an input section specification.
1572
  void
1573
  add_input_section(const Input_section_spec* spec, bool keep);
1574
 
1575
  // Return whether the output section is relro.
1576
  bool
1577
  is_relro() const
1578
  { return this->is_relro_; }
1579
 
1580
  // Record that the output section is relro.
1581
  void
1582
  set_is_relro()
1583
  { this->is_relro_ = true; }
1584
 
1585
  // Create any required output sections.
1586
  void
1587
  create_sections(Layout*);
1588
 
1589
  // Add any symbols being defined to the symbol table.
1590
  void
1591
  add_symbols_to_table(Symbol_table* symtab);
1592
 
1593
  // Finalize symbols and check assertions.
1594
  void
1595
  finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
1596
 
1597
  // Return the output section name to use for an input file name and
1598
  // section name.
1599
  const char*
1600
  output_section_name(const char* file_name, const char* section_name,
1601
                      Output_section***);
1602
 
1603
  // Initialize OSP with an output section.
1604
  void
1605
  orphan_section_init(Orphan_section_placement* osp,
1606
                      Script_sections::Elements_iterator p)
1607
  { osp->output_section_init(this->name_, this->output_section_, p); }
1608
 
1609
  // Set the section address.
1610
  void
1611
  set_section_addresses(Symbol_table* symtab, Layout* layout,
1612
                        uint64_t* dot_value, uint64_t* load_address);
1613
 
1614
  // Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
1615
  // this section is constrained, and the input sections do not match,
1616
  // return the constraint, and set *POSD.
1617
  Section_constraint
1618
  check_constraint(Output_section_definition** posd);
1619
 
1620
  // See if this is the alternate output section for a constrained
1621
  // output section.  If it is, transfer the Output_section and return
1622
  // true.  Otherwise return false.
1623
  bool
1624
  alternate_constraint(Output_section_definition*, Section_constraint);
1625
 
1626
  // Get the list of segments to use for an allocated section when
1627
  // using a PHDRS clause.
1628
  Output_section*
1629
  allocate_to_segment(String_list** phdrs_list, bool* orphan);
1630
 
1631
  // Look for an output section by name and return the address, the
1632
  // load address, the alignment, and the size.  This is used when an
1633
  // expression refers to an output section which was not actually
1634
  // created.  This returns true if the section was found, false
1635
  // otherwise.
1636
  bool
1637
  get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1638
                          uint64_t*) const;
1639
 
1640
  // Return the associated Output_section if there is one.
1641
  Output_section*
1642
  get_output_section() const
1643
  { return this->output_section_; }
1644
 
1645
  // Print the contents to the FILE.  This is for debugging.
1646
  void
1647
  print(FILE*) const;
1648
 
1649
 private:
1650
  typedef std::vector<Output_section_element*> Output_section_elements;
1651
 
1652
  // The output section name.
1653
  std::string name_;
1654
  // The address.  This may be NULL.
1655
  Expression* address_;
1656
  // The load address.  This may be NULL.
1657
  Expression* load_address_;
1658
  // The alignment.  This may be NULL.
1659
  Expression* align_;
1660
  // The input section alignment.  This may be NULL.
1661
  Expression* subalign_;
1662
  // The constraint, if any.
1663
  Section_constraint constraint_;
1664
  // The fill value.  This may be NULL.
1665
  Expression* fill_;
1666
  // The list of segments this section should go into.  This may be
1667
  // NULL.
1668
  String_list* phdrs_;
1669
  // The list of elements defining the section.
1670
  Output_section_elements elements_;
1671
  // The Output_section created for this definition.  This will be
1672
  // NULL if none was created.
1673
  Output_section* output_section_;
1674
  // The address after it has been evaluated.
1675
  uint64_t evaluated_address_;
1676
  // The load address after it has been evaluated.
1677
  uint64_t evaluated_load_address_;
1678
  // The alignment after it has been evaluated.
1679
  uint64_t evaluated_addralign_;
1680
  // The output section is relro.
1681
  bool is_relro_;
1682
};
1683
 
1684
// Constructor.
1685
 
1686
Output_section_definition::Output_section_definition(
1687
    const char* name,
1688
    size_t namelen,
1689
    const Parser_output_section_header* header)
1690
  : name_(name, namelen),
1691
    address_(header->address),
1692
    load_address_(header->load_address),
1693
    align_(header->align),
1694
    subalign_(header->subalign),
1695
    constraint_(header->constraint),
1696
    fill_(NULL),
1697
    phdrs_(NULL),
1698
    elements_(),
1699
    output_section_(NULL),
1700
    evaluated_address_(0),
1701
    evaluated_load_address_(0),
1702
    evaluated_addralign_(0),
1703
    is_relro_(false)
1704
{
1705
}
1706
 
1707
// Finish an output section.
1708
 
1709
void
1710
Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1711
{
1712
  this->fill_ = trailer->fill;
1713
  this->phdrs_ = trailer->phdrs;
1714
}
1715
 
1716
// Add a symbol to be defined.
1717
 
1718
void
1719
Output_section_definition::add_symbol_assignment(const char* name,
1720
                                                 size_t length,
1721
                                                 Expression* value,
1722
                                                 bool provide,
1723
                                                 bool hidden)
1724
{
1725
  Output_section_element* p = new Output_section_element_assignment(name,
1726
                                                                    length,
1727
                                                                    value,
1728
                                                                    provide,
1729
                                                                    hidden);
1730
  this->elements_.push_back(p);
1731
}
1732
 
1733
// Add an assignment to the special dot symbol.
1734
 
1735
void
1736
Output_section_definition::add_dot_assignment(Expression* value)
1737
{
1738
  Output_section_element* p = new Output_section_element_dot_assignment(value);
1739
  this->elements_.push_back(p);
1740
}
1741
 
1742
// Add an assertion.
1743
 
1744
void
1745
Output_section_definition::add_assertion(Expression* check,
1746
                                         const char* message,
1747
                                         size_t messagelen)
1748
{
1749
  Output_section_element* p = new Output_section_element_assertion(check,
1750
                                                                   message,
1751
                                                                   messagelen);
1752
  this->elements_.push_back(p);
1753
}
1754
 
1755
// Add a data item to the current output section.
1756
 
1757
void
1758
Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1759
{
1760
  Output_section_element* p = new Output_section_element_data(size, is_signed,
1761
                                                              val);
1762
  this->elements_.push_back(p);
1763
}
1764
 
1765
// Add a setting for the fill value.
1766
 
1767
void
1768
Output_section_definition::add_fill(Expression* val)
1769
{
1770
  Output_section_element* p = new Output_section_element_fill(val);
1771
  this->elements_.push_back(p);
1772
}
1773
 
1774
// Add an input section specification.
1775
 
1776
void
1777
Output_section_definition::add_input_section(const Input_section_spec* spec,
1778
                                             bool keep)
1779
{
1780
  Output_section_element* p = new Output_section_element_input(spec, keep);
1781
  this->elements_.push_back(p);
1782
}
1783
 
1784
// Create any required output sections.  We need an output section if
1785
// there is a data statement here.
1786
 
1787
void
1788
Output_section_definition::create_sections(Layout* layout)
1789
{
1790
  if (this->output_section_ != NULL)
1791
    return;
1792
  for (Output_section_elements::const_iterator p = this->elements_.begin();
1793
       p != this->elements_.end();
1794
       ++p)
1795
    {
1796
      if ((*p)->needs_output_section())
1797
        {
1798
          const char* name = this->name_.c_str();
1799
          this->output_section_ = layout->make_output_section_for_script(name);
1800
          return;
1801
        }
1802
    }
1803
}
1804
 
1805
// Add any symbols being defined to the symbol table.
1806
 
1807
void
1808
Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
1809
{
1810
  for (Output_section_elements::iterator p = this->elements_.begin();
1811
       p != this->elements_.end();
1812
       ++p)
1813
    (*p)->add_symbols_to_table(symtab);
1814
}
1815
 
1816
// Finalize symbols and check assertions.
1817
 
1818
void
1819
Output_section_definition::finalize_symbols(Symbol_table* symtab,
1820
                                            const Layout* layout,
1821
                                            uint64_t* dot_value)
1822
{
1823
  if (this->output_section_ != NULL)
1824
    *dot_value = this->output_section_->address();
1825
  else
1826
    {
1827
      uint64_t address = *dot_value;
1828
      if (this->address_ != NULL)
1829
        {
1830
          Output_section* dummy;
1831
          address = this->address_->eval_with_dot(symtab, layout, true,
1832
                                                  *dot_value, NULL,
1833
                                                  &dummy);
1834
        }
1835
      if (this->align_ != NULL)
1836
        {
1837
          Output_section* dummy;
1838
          uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
1839
                                                       *dot_value,
1840
                                                       NULL,
1841
                                                       &dummy);
1842
          address = align_address(address, align);
1843
        }
1844
      *dot_value = address;
1845
    }
1846
 
1847
  Output_section* dot_section = this->output_section_;
1848
  for (Output_section_elements::iterator p = this->elements_.begin();
1849
       p != this->elements_.end();
1850
       ++p)
1851
    (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
1852
}
1853
 
1854
// Return the output section name to use for an input section name.
1855
 
1856
const char*
1857
Output_section_definition::output_section_name(const char* file_name,
1858
                                               const char* section_name,
1859
                                               Output_section*** slot)
1860
{
1861
  // Ask each element whether it matches NAME.
1862
  for (Output_section_elements::const_iterator p = this->elements_.begin();
1863
       p != this->elements_.end();
1864
       ++p)
1865
    {
1866
      if ((*p)->match_name(file_name, section_name))
1867
        {
1868
          // We found a match for NAME, which means that it should go
1869
          // into this output section.
1870
          *slot = &this->output_section_;
1871
          return this->name_.c_str();
1872
        }
1873
    }
1874
 
1875
  // We don't know about this section name.
1876
  return NULL;
1877
}
1878
 
1879
// Set the section address.  Note that the OUTPUT_SECTION_ field will
1880
// be NULL if no input sections were mapped to this output section.
1881
// We still have to adjust dot and process symbol assignments.
1882
 
1883
void
1884
Output_section_definition::set_section_addresses(Symbol_table* symtab,
1885
                                                 Layout* layout,
1886
                                                 uint64_t* dot_value,
1887
                                                 uint64_t* load_address)
1888
{
1889
  uint64_t address;
1890
  if (this->address_ == NULL)
1891
    address = *dot_value;
1892
  else
1893
    {
1894
      Output_section* dummy;
1895
      address = this->address_->eval_with_dot(symtab, layout, true,
1896
                                              *dot_value, NULL, &dummy);
1897
    }
1898
 
1899
  uint64_t align;
1900
  if (this->align_ == NULL)
1901
    {
1902
      if (this->output_section_ == NULL)
1903
        align = 0;
1904
      else
1905
        align = this->output_section_->addralign();
1906
    }
1907
  else
1908
    {
1909
      Output_section* align_section;
1910
      align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
1911
                                          NULL, &align_section);
1912
      if (align_section != NULL)
1913
        gold_warning(_("alignment of section %s is not absolute"),
1914
                     this->name_.c_str());
1915
      if (this->output_section_ != NULL)
1916
        this->output_section_->set_addralign(align);
1917
    }
1918
 
1919
  address = align_address(address, align);
1920
 
1921
  uint64_t start_address = address;
1922
 
1923
  *dot_value = address;
1924
 
1925
  // The address of non-SHF_ALLOC sections is forced to zero,
1926
  // regardless of what the linker script wants.
1927
  if (this->output_section_ != NULL
1928
      && (this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0)
1929
    this->output_section_->set_address(address);
1930
 
1931
  this->evaluated_address_ = address;
1932
  this->evaluated_addralign_ = align;
1933
 
1934
  if (this->load_address_ == NULL)
1935
    this->evaluated_load_address_ = address;
1936
  else
1937
    {
1938
      Output_section* dummy;
1939
      uint64_t laddr =
1940
        this->load_address_->eval_with_dot(symtab, layout, true, *dot_value,
1941
                                           this->output_section_, &dummy);
1942
      if (this->output_section_ != NULL)
1943
        this->output_section_->set_load_address(laddr);
1944
      this->evaluated_load_address_ = laddr;
1945
    }
1946
 
1947
  uint64_t subalign;
1948
  if (this->subalign_ == NULL)
1949
    subalign = 0;
1950
  else
1951
    {
1952
      Output_section* subalign_section;
1953
      subalign = this->subalign_->eval_with_dot(symtab, layout, true,
1954
                                                *dot_value, NULL,
1955
                                                &subalign_section);
1956
      if (subalign_section != NULL)
1957
        gold_warning(_("subalign of section %s is not absolute"),
1958
                     this->name_.c_str());
1959
    }
1960
 
1961
  std::string fill;
1962
  if (this->fill_ != NULL)
1963
    {
1964
      // FIXME: The GNU linker supports fill values of arbitrary
1965
      // length.
1966
      Output_section* fill_section;
1967
      uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
1968
                                                     *dot_value,
1969
                                                     NULL,
1970
                                                     &fill_section);
1971
      if (fill_section != NULL)
1972
        gold_warning(_("fill of section %s is not absolute"),
1973
                     this->name_.c_str());
1974
      unsigned char fill_buff[4];
1975
      elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1976
      fill.assign(reinterpret_cast<char*>(fill_buff), 4);
1977
    }
1978
 
1979
  Input_section_list input_sections;
1980
  if (this->output_section_ != NULL)
1981
    {
1982
      // Get the list of input sections attached to this output
1983
      // section.  This will leave the output section with only
1984
      // Output_section_data entries.
1985
      address += this->output_section_->get_input_sections(address,
1986
                                                           fill,
1987
                                                           &input_sections);
1988
      *dot_value = address;
1989
    }
1990
 
1991
  Output_section* dot_section = this->output_section_;
1992
  for (Output_section_elements::iterator p = this->elements_.begin();
1993
       p != this->elements_.end();
1994
       ++p)
1995
    (*p)->set_section_addresses(symtab, layout, this->output_section_,
1996
                                subalign, dot_value, &dot_section, &fill,
1997
                                &input_sections);
1998
 
1999
  gold_assert(input_sections.empty());
2000
 
2001
  if (this->load_address_ == NULL || this->output_section_ == NULL)
2002
    *load_address = *dot_value;
2003
  else
2004
    *load_address = (this->output_section_->load_address()
2005
                     + (*dot_value - start_address));
2006
 
2007
  if (this->output_section_ != NULL)
2008
    {
2009
      if (this->is_relro_)
2010
        this->output_section_->set_is_relro();
2011
      else
2012
        this->output_section_->clear_is_relro();
2013
    }
2014
}
2015
 
2016
// Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
2017
// this section is constrained, and the input sections do not match,
2018
// return the constraint, and set *POSD.
2019
 
2020
Section_constraint
2021
Output_section_definition::check_constraint(Output_section_definition** posd)
2022
{
2023
  switch (this->constraint_)
2024
    {
2025
    case CONSTRAINT_NONE:
2026
      return CONSTRAINT_NONE;
2027
 
2028
    case CONSTRAINT_ONLY_IF_RO:
2029
      if (this->output_section_ != NULL
2030
          && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2031
        {
2032
          *posd = this;
2033
          return CONSTRAINT_ONLY_IF_RO;
2034
        }
2035
      return CONSTRAINT_NONE;
2036
 
2037
    case CONSTRAINT_ONLY_IF_RW:
2038
      if (this->output_section_ != NULL
2039
          && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2040
        {
2041
          *posd = this;
2042
          return CONSTRAINT_ONLY_IF_RW;
2043
        }
2044
      return CONSTRAINT_NONE;
2045
 
2046
    case CONSTRAINT_SPECIAL:
2047
      if (this->output_section_ != NULL)
2048
        gold_error(_("SPECIAL constraints are not implemented"));
2049
      return CONSTRAINT_NONE;
2050
 
2051
    default:
2052
      gold_unreachable();
2053
    }
2054
}
2055
 
2056
// See if this is the alternate output section for a constrained
2057
// output section.  If it is, transfer the Output_section and return
2058
// true.  Otherwise return false.
2059
 
2060
bool
2061
Output_section_definition::alternate_constraint(
2062
    Output_section_definition* posd,
2063
    Section_constraint constraint)
2064
{
2065
  if (this->name_ != posd->name_)
2066
    return false;
2067
 
2068
  switch (constraint)
2069
    {
2070
    case CONSTRAINT_ONLY_IF_RO:
2071
      if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2072
        return false;
2073
      break;
2074
 
2075
    case CONSTRAINT_ONLY_IF_RW:
2076
      if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2077
        return false;
2078
      break;
2079
 
2080
    default:
2081
      gold_unreachable();
2082
    }
2083
 
2084
  // We have found the alternate constraint.  We just need to move
2085
  // over the Output_section.  When constraints are used properly,
2086
  // THIS should not have an output_section pointer, as all the input
2087
  // sections should have matched the other definition.
2088
 
2089
  if (this->output_section_ != NULL)
2090
    gold_error(_("mismatched definition for constrained sections"));
2091
 
2092
  this->output_section_ = posd->output_section_;
2093
  posd->output_section_ = NULL;
2094
 
2095
  if (this->is_relro_)
2096
    this->output_section_->set_is_relro();
2097
  else
2098
    this->output_section_->clear_is_relro();
2099
 
2100
  return true;
2101
}
2102
 
2103
// Get the list of segments to use for an allocated section when using
2104
// a PHDRS clause.
2105
 
2106
Output_section*
2107
Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2108
                                               bool* orphan)
2109
{
2110
  if (this->output_section_ == NULL)
2111
    return NULL;
2112
  if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2113
    return NULL;
2114
  *orphan = false;
2115
  if (this->phdrs_ != NULL)
2116
    *phdrs_list = this->phdrs_;
2117
  return this->output_section_;
2118
}
2119
 
2120
// Look for an output section by name and return the address, the load
2121
// address, the alignment, and the size.  This is used when an
2122
// expression refers to an output section which was not actually
2123
// created.  This returns true if the section was found, false
2124
// otherwise.
2125
 
2126
bool
2127
Output_section_definition::get_output_section_info(const char* name,
2128
                                                   uint64_t* address,
2129
                                                   uint64_t* load_address,
2130
                                                   uint64_t* addralign,
2131
                                                   uint64_t* size) const
2132
{
2133
  if (this->name_ != name)
2134
    return false;
2135
 
2136
  if (this->output_section_ != NULL)
2137
    {
2138
      *address = this->output_section_->address();
2139
      if (this->output_section_->has_load_address())
2140
        *load_address = this->output_section_->load_address();
2141
      else
2142
        *load_address = *address;
2143
      *addralign = this->output_section_->addralign();
2144
      *size = this->output_section_->current_data_size();
2145
    }
2146
  else
2147
    {
2148
      *address = this->evaluated_address_;
2149
      *load_address = this->evaluated_load_address_;
2150
      *addralign = this->evaluated_addralign_;
2151
      *size = 0;
2152
    }
2153
 
2154
  return true;
2155
}
2156
 
2157
// Print for debugging.
2158
 
2159
void
2160
Output_section_definition::print(FILE* f) const
2161
{
2162
  fprintf(f, "  %s ", this->name_.c_str());
2163
 
2164
  if (this->address_ != NULL)
2165
    {
2166
      this->address_->print(f);
2167
      fprintf(f, " ");
2168
    }
2169
 
2170
  fprintf(f, ": ");
2171
 
2172
  if (this->load_address_ != NULL)
2173
    {
2174
      fprintf(f, "AT(");
2175
      this->load_address_->print(f);
2176
      fprintf(f, ") ");
2177
    }
2178
 
2179
  if (this->align_ != NULL)
2180
    {
2181
      fprintf(f, "ALIGN(");
2182
      this->align_->print(f);
2183
      fprintf(f, ") ");
2184
    }
2185
 
2186
  if (this->subalign_ != NULL)
2187
    {
2188
      fprintf(f, "SUBALIGN(");
2189
      this->subalign_->print(f);
2190
      fprintf(f, ") ");
2191
    }
2192
 
2193
  fprintf(f, "{\n");
2194
 
2195
  for (Output_section_elements::const_iterator p = this->elements_.begin();
2196
       p != this->elements_.end();
2197
       ++p)
2198
    (*p)->print(f);
2199
 
2200
  fprintf(f, "  }");
2201
 
2202
  if (this->fill_ != NULL)
2203
    {
2204
      fprintf(f, " = ");
2205
      this->fill_->print(f);
2206
    }
2207
 
2208
  if (this->phdrs_ != NULL)
2209
    {
2210
      for (String_list::const_iterator p = this->phdrs_->begin();
2211
           p != this->phdrs_->end();
2212
           ++p)
2213
        fprintf(f, " :%s", p->c_str());
2214
    }
2215
 
2216
  fprintf(f, "\n");
2217
}
2218
 
2219
// An output section created to hold orphaned input sections.  These
2220
// do not actually appear in linker scripts.  However, for convenience
2221
// when setting the output section addresses, we put a marker to these
2222
// sections in the appropriate place in the list of SECTIONS elements.
2223
 
2224
class Orphan_output_section : public Sections_element
2225
{
2226
 public:
2227
  Orphan_output_section(Output_section* os)
2228
    : os_(os)
2229
  { }
2230
 
2231
  // Return whether the orphan output section is relro.  We can just
2232
  // check the output section because we always set the flag, if
2233
  // needed, just after we create the Orphan_output_section.
2234
  bool
2235
  is_relro() const
2236
  { return this->os_->is_relro(); }
2237
 
2238
  // Initialize OSP with an output section.  This should have been
2239
  // done already.
2240
  void
2241
  orphan_section_init(Orphan_section_placement*,
2242
                      Script_sections::Elements_iterator)
2243
  { gold_unreachable(); }
2244
 
2245
  // Set section addresses.
2246
  void
2247
  set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*);
2248
 
2249
  // Get the list of segments to use for an allocated section when
2250
  // using a PHDRS clause.
2251
  Output_section*
2252
  allocate_to_segment(String_list**, bool*);
2253
 
2254
  // Return the associated Output_section.
2255
  Output_section*
2256
  get_output_section() const
2257
  { return this->os_; }
2258
 
2259
  // Print for debugging.
2260
  void
2261
  print(FILE* f) const
2262
  {
2263
    fprintf(f, "  marker for orphaned output section %s\n",
2264
            this->os_->name());
2265
  }
2266
 
2267
 private:
2268
  Output_section* os_;
2269
};
2270
 
2271
// Set section addresses.
2272
 
2273
void
2274
Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
2275
                                             uint64_t* dot_value,
2276
                                             uint64_t* load_address)
2277
{
2278
  typedef std::list<Output_section::Simple_input_section> Input_section_list;
2279
 
2280
  bool have_load_address = *load_address != *dot_value;
2281
 
2282
  uint64_t address = *dot_value;
2283
  address = align_address(address, this->os_->addralign());
2284
 
2285
  if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
2286
    {
2287
      this->os_->set_address(address);
2288
      if (have_load_address)
2289
        this->os_->set_load_address(align_address(*load_address,
2290
                                                  this->os_->addralign()));
2291
    }
2292
 
2293
  Input_section_list input_sections;
2294
  address += this->os_->get_input_sections(address, "", &input_sections);
2295
 
2296
  for (Input_section_list::iterator p = input_sections.begin();
2297
       p != input_sections.end();
2298
       ++p)
2299
    {
2300
      uint64_t addralign;
2301
      uint64_t size;
2302
 
2303
      // We know what are single-threaded, so it is OK to lock the
2304
      // object.
2305
      {
2306
        const Task* task = reinterpret_cast<const Task*>(-1);
2307
        Task_lock_obj<Object> tl(task, p->relobj());
2308
        addralign = p->relobj()->section_addralign(p->shndx());
2309
        if (p->is_relaxed_input_section())
2310
          // We use current data size because relxed section sizes may not
2311
          // have finalized yet.
2312
          size = p->relaxed_input_section()->current_data_size();
2313
        else
2314
          size = p->relobj()->section_size(p->shndx());
2315
      }
2316
 
2317
      address = align_address(address, addralign);
2318
      this->os_->add_input_section_for_script(*p, size, addralign);
2319
      address += size;
2320
    }
2321
 
2322
  // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2323
  if (this->os_ == NULL
2324
      || (this->os_->flags() & elfcpp::SHF_TLS) == 0
2325
      || this->os_->type() != elfcpp::SHT_NOBITS)
2326
    {
2327
      if (!have_load_address)
2328
        *load_address = address;
2329
      else
2330
        *load_address += address - *dot_value;
2331
 
2332
      *dot_value = address;
2333
    }
2334
}
2335
 
2336
// Get the list of segments to use for an allocated section when using
2337
// a PHDRS clause.  If this is an allocated section, return the
2338
// Output_section.  We don't change the list of segments.
2339
 
2340
Output_section*
2341
Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
2342
{
2343
  if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2344
    return NULL;
2345
  *orphan = true;
2346
  return this->os_;
2347
}
2348
 
2349
// Class Phdrs_element.  A program header from a PHDRS clause.
2350
 
2351
class Phdrs_element
2352
{
2353
 public:
2354
  Phdrs_element(const char* name, size_t namelen, unsigned int type,
2355
                bool includes_filehdr, bool includes_phdrs,
2356
                bool is_flags_valid, unsigned int flags,
2357
                Expression* load_address)
2358
    : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2359
      includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2360
      flags_(flags), load_address_(load_address), load_address_value_(0),
2361
      segment_(NULL)
2362
  { }
2363
 
2364
  // Return the name of this segment.
2365
  const std::string&
2366
  name() const
2367
  { return this->name_; }
2368
 
2369
  // Return the type of the segment.
2370
  unsigned int
2371
  type() const
2372
  { return this->type_; }
2373
 
2374
  // Whether to include the file header.
2375
  bool
2376
  includes_filehdr() const
2377
  { return this->includes_filehdr_; }
2378
 
2379
  // Whether to include the program headers.
2380
  bool
2381
  includes_phdrs() const
2382
  { return this->includes_phdrs_; }
2383
 
2384
  // Return whether there is a load address.
2385
  bool
2386
  has_load_address() const
2387
  { return this->load_address_ != NULL; }
2388
 
2389
  // Evaluate the load address expression if there is one.
2390
  void
2391
  eval_load_address(Symbol_table* symtab, Layout* layout)
2392
  {
2393
    if (this->load_address_ != NULL)
2394
      this->load_address_value_ = this->load_address_->eval(symtab, layout,
2395
                                                            true);
2396
  }
2397
 
2398
  // Return the load address.
2399
  uint64_t
2400
  load_address() const
2401
  {
2402
    gold_assert(this->load_address_ != NULL);
2403
    return this->load_address_value_;
2404
  }
2405
 
2406
  // Create the segment.
2407
  Output_segment*
2408
  create_segment(Layout* layout)
2409
  {
2410
    this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2411
    return this->segment_;
2412
  }
2413
 
2414
  // Return the segment.
2415
  Output_segment*
2416
  segment()
2417
  { return this->segment_; }
2418
 
2419
  // Release the segment.
2420
  void
2421
  release_segment()
2422
  { this->segment_ = NULL; }
2423
 
2424
  // Set the segment flags if appropriate.
2425
  void
2426
  set_flags_if_valid()
2427
  {
2428
    if (this->is_flags_valid_)
2429
      this->segment_->set_flags(this->flags_);
2430
  }
2431
 
2432
  // Print for debugging.
2433
  void
2434
  print(FILE*) const;
2435
 
2436
 private:
2437
  // The name used in the script.
2438
  std::string name_;
2439
  // The type of the segment (PT_LOAD, etc.).
2440
  unsigned int type_;
2441
  // Whether this segment includes the file header.
2442
  bool includes_filehdr_;
2443
  // Whether this segment includes the section headers.
2444
  bool includes_phdrs_;
2445
  // Whether the flags were explicitly specified.
2446
  bool is_flags_valid_;
2447
  // The flags for this segment (PF_R, etc.) if specified.
2448
  unsigned int flags_;
2449
  // The expression for the load address for this segment.  This may
2450
  // be NULL.
2451
  Expression* load_address_;
2452
  // The actual load address from evaluating the expression.
2453
  uint64_t load_address_value_;
2454
  // The segment itself.
2455
  Output_segment* segment_;
2456
};
2457
 
2458
// Print for debugging.
2459
 
2460
void
2461
Phdrs_element::print(FILE* f) const
2462
{
2463
  fprintf(f, "  %s 0x%x", this->name_.c_str(), this->type_);
2464
  if (this->includes_filehdr_)
2465
    fprintf(f, " FILEHDR");
2466
  if (this->includes_phdrs_)
2467
    fprintf(f, " PHDRS");
2468
  if (this->is_flags_valid_)
2469
    fprintf(f, " FLAGS(%u)", this->flags_);
2470
  if (this->load_address_ != NULL)
2471
    {
2472
      fprintf(f, " AT(");
2473
      this->load_address_->print(f);
2474
      fprintf(f, ")");
2475
    }
2476
  fprintf(f, ";\n");
2477
}
2478
 
2479
// Class Script_sections.
2480
 
2481
Script_sections::Script_sections()
2482
  : saw_sections_clause_(false),
2483
    in_sections_clause_(false),
2484
    sections_elements_(NULL),
2485
    output_section_(NULL),
2486
    phdrs_elements_(NULL),
2487
    orphan_section_placement_(NULL),
2488
    data_segment_align_start_(),
2489
    saw_data_segment_align_(false),
2490
    saw_relro_end_(false)
2491
{
2492
}
2493
 
2494
// Start a SECTIONS clause.
2495
 
2496
void
2497
Script_sections::start_sections()
2498
{
2499
  gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2500
  this->saw_sections_clause_ = true;
2501
  this->in_sections_clause_ = true;
2502
  if (this->sections_elements_ == NULL)
2503
    this->sections_elements_ = new Sections_elements;
2504
}
2505
 
2506
// Finish a SECTIONS clause.
2507
 
2508
void
2509
Script_sections::finish_sections()
2510
{
2511
  gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2512
  this->in_sections_clause_ = false;
2513
}
2514
 
2515
// Add a symbol to be defined.
2516
 
2517
void
2518
Script_sections::add_symbol_assignment(const char* name, size_t length,
2519
                                       Expression* val, bool provide,
2520
                                       bool hidden)
2521
{
2522
  if (this->output_section_ != NULL)
2523
    this->output_section_->add_symbol_assignment(name, length, val,
2524
                                                 provide, hidden);
2525
  else
2526
    {
2527
      Sections_element* p = new Sections_element_assignment(name, length,
2528
                                                            val, provide,
2529
                                                            hidden);
2530
      this->sections_elements_->push_back(p);
2531
    }
2532
}
2533
 
2534
// Add an assignment to the special dot symbol.
2535
 
2536
void
2537
Script_sections::add_dot_assignment(Expression* val)
2538
{
2539
  if (this->output_section_ != NULL)
2540
    this->output_section_->add_dot_assignment(val);
2541
  else
2542
    {
2543
      // The GNU linker permits assignments to . to appears outside of
2544
      // a SECTIONS clause, and treats it as appearing inside, so
2545
      // sections_elements_ may be NULL here.
2546
      if (this->sections_elements_ == NULL)
2547
        {
2548
          this->sections_elements_ = new Sections_elements;
2549
          this->saw_sections_clause_ = true;
2550
        }
2551
 
2552
      Sections_element* p = new Sections_element_dot_assignment(val);
2553
      this->sections_elements_->push_back(p);
2554
    }
2555
}
2556
 
2557
// Add an assertion.
2558
 
2559
void
2560
Script_sections::add_assertion(Expression* check, const char* message,
2561
                               size_t messagelen)
2562
{
2563
  if (this->output_section_ != NULL)
2564
    this->output_section_->add_assertion(check, message, messagelen);
2565
  else
2566
    {
2567
      Sections_element* p = new Sections_element_assertion(check, message,
2568
                                                           messagelen);
2569
      this->sections_elements_->push_back(p);
2570
    }
2571
}
2572
 
2573
// Start processing entries for an output section.
2574
 
2575
void
2576
Script_sections::start_output_section(
2577
    const char* name,
2578
    size_t namelen,
2579
    const Parser_output_section_header *header)
2580
{
2581
  Output_section_definition* posd = new Output_section_definition(name,
2582
                                                                  namelen,
2583
                                                                  header);
2584
  this->sections_elements_->push_back(posd);
2585
  gold_assert(this->output_section_ == NULL);
2586
  this->output_section_ = posd;
2587
}
2588
 
2589
// Stop processing entries for an output section.
2590
 
2591
void
2592
Script_sections::finish_output_section(
2593
    const Parser_output_section_trailer* trailer)
2594
{
2595
  gold_assert(this->output_section_ != NULL);
2596
  this->output_section_->finish(trailer);
2597
  this->output_section_ = NULL;
2598
}
2599
 
2600
// Add a data item to the current output section.
2601
 
2602
void
2603
Script_sections::add_data(int size, bool is_signed, Expression* val)
2604
{
2605
  gold_assert(this->output_section_ != NULL);
2606
  this->output_section_->add_data(size, is_signed, val);
2607
}
2608
 
2609
// Add a fill value setting to the current output section.
2610
 
2611
void
2612
Script_sections::add_fill(Expression* val)
2613
{
2614
  gold_assert(this->output_section_ != NULL);
2615
  this->output_section_->add_fill(val);
2616
}
2617
 
2618
// Add an input section specification to the current output section.
2619
 
2620
void
2621
Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2622
{
2623
  gold_assert(this->output_section_ != NULL);
2624
  this->output_section_->add_input_section(spec, keep);
2625
}
2626
 
2627
// This is called when we see DATA_SEGMENT_ALIGN.  It means that any
2628
// subsequent output sections may be relro.
2629
 
2630
void
2631
Script_sections::data_segment_align()
2632
{
2633
  if (this->saw_data_segment_align_)
2634
    gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2635
  gold_assert(!this->sections_elements_->empty());
2636
  Sections_elements::iterator p = this->sections_elements_->end();
2637
  --p;
2638
  this->data_segment_align_start_ = p;
2639
  this->saw_data_segment_align_ = true;
2640
}
2641
 
2642
// This is called when we see DATA_SEGMENT_RELRO_END.  It means that
2643
// any output sections seen since DATA_SEGMENT_ALIGN are relro.
2644
 
2645
void
2646
Script_sections::data_segment_relro_end()
2647
{
2648
  if (this->saw_relro_end_)
2649
    gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2650
                 "in a linker script"));
2651
  this->saw_relro_end_ = true;
2652
 
2653
  if (!this->saw_data_segment_align_)
2654
    gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2655
  else
2656
    {
2657
      Sections_elements::iterator p = this->data_segment_align_start_;
2658
      for (++p; p != this->sections_elements_->end(); ++p)
2659
        (*p)->set_is_relro();
2660
    }
2661
}
2662
 
2663
// Create any required sections.
2664
 
2665
void
2666
Script_sections::create_sections(Layout* layout)
2667
{
2668
  if (!this->saw_sections_clause_)
2669
    return;
2670
  for (Sections_elements::iterator p = this->sections_elements_->begin();
2671
       p != this->sections_elements_->end();
2672
       ++p)
2673
    (*p)->create_sections(layout);
2674
}
2675
 
2676
// Add any symbols we are defining to the symbol table.
2677
 
2678
void
2679
Script_sections::add_symbols_to_table(Symbol_table* symtab)
2680
{
2681
  if (!this->saw_sections_clause_)
2682
    return;
2683
  for (Sections_elements::iterator p = this->sections_elements_->begin();
2684
       p != this->sections_elements_->end();
2685
       ++p)
2686
    (*p)->add_symbols_to_table(symtab);
2687
}
2688
 
2689
// Finalize symbols and check assertions.
2690
 
2691
void
2692
Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2693
{
2694
  if (!this->saw_sections_clause_)
2695
    return;
2696
  uint64_t dot_value = 0;
2697
  for (Sections_elements::iterator p = this->sections_elements_->begin();
2698
       p != this->sections_elements_->end();
2699
       ++p)
2700
    (*p)->finalize_symbols(symtab, layout, &dot_value);
2701
}
2702
 
2703
// Return the name of the output section to use for an input file name
2704
// and section name.
2705
 
2706
const char*
2707
Script_sections::output_section_name(const char* file_name,
2708
                                     const char* section_name,
2709
                                     Output_section*** output_section_slot)
2710
{
2711
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2712
       p != this->sections_elements_->end();
2713
       ++p)
2714
    {
2715
      const char* ret = (*p)->output_section_name(file_name, section_name,
2716
                                                  output_section_slot);
2717
 
2718
      if (ret != NULL)
2719
        {
2720
          // The special name /DISCARD/ means that the input section
2721
          // should be discarded.
2722
          if (strcmp(ret, "/DISCARD/") == 0)
2723
            {
2724
              *output_section_slot = NULL;
2725
              return NULL;
2726
            }
2727
          return ret;
2728
        }
2729
    }
2730
 
2731
  // If we couldn't find a mapping for the name, the output section
2732
  // gets the name of the input section.
2733
 
2734
  *output_section_slot = NULL;
2735
 
2736
  return section_name;
2737
}
2738
 
2739
// Place a marker for an orphan output section into the SECTIONS
2740
// clause.
2741
 
2742
void
2743
Script_sections::place_orphan(Output_section* os)
2744
{
2745
  Orphan_section_placement* osp = this->orphan_section_placement_;
2746
  if (osp == NULL)
2747
    {
2748
      // Initialize the Orphan_section_placement structure.
2749
      osp = new Orphan_section_placement();
2750
      for (Sections_elements::iterator p = this->sections_elements_->begin();
2751
           p != this->sections_elements_->end();
2752
           ++p)
2753
        (*p)->orphan_section_init(osp, p);
2754
      gold_assert(!this->sections_elements_->empty());
2755
      Sections_elements::iterator last = this->sections_elements_->end();
2756
      --last;
2757
      osp->last_init(last);
2758
      this->orphan_section_placement_ = osp;
2759
    }
2760
 
2761
  Orphan_output_section* orphan = new Orphan_output_section(os);
2762
 
2763
  // Look for where to put ORPHAN.
2764
  Sections_elements::iterator* where;
2765
  if (osp->find_place(os, &where))
2766
    {
2767
      if ((**where)->is_relro())
2768
        os->set_is_relro();
2769
      else
2770
        os->clear_is_relro();
2771
 
2772
      // We want to insert ORPHAN after *WHERE, and then update *WHERE
2773
      // so that the next one goes after this one.
2774
      Sections_elements::iterator p = *where;
2775
      gold_assert(p != this->sections_elements_->end());
2776
      ++p;
2777
      *where = this->sections_elements_->insert(p, orphan);
2778
    }
2779
  else
2780
    {
2781
      os->clear_is_relro();
2782
      // We don't have a place to put this orphan section.  Put it,
2783
      // and all other sections like it, at the end, but before the
2784
      // sections which always come at the end.
2785
      Sections_elements::iterator last = osp->last_place();
2786
      *where = this->sections_elements_->insert(last, orphan);
2787
    }
2788
}
2789
 
2790
// Set the addresses of all the output sections.  Walk through all the
2791
// elements, tracking the dot symbol.  Apply assignments which set
2792
// absolute symbol values, in case they are used when setting dot.
2793
// Fill in data statement values.  As we find output sections, set the
2794
// address, set the address of all associated input sections, and
2795
// update dot.  Return the segment which should hold the file header
2796
// and segment headers, if any.
2797
 
2798
Output_segment*
2799
Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2800
{
2801
  gold_assert(this->saw_sections_clause_);
2802
 
2803
  // Implement ONLY_IF_RO/ONLY_IF_RW constraints.  These are a pain
2804
  // for our representation.
2805
  for (Sections_elements::iterator p = this->sections_elements_->begin();
2806
       p != this->sections_elements_->end();
2807
       ++p)
2808
    {
2809
      Output_section_definition* posd;
2810
      Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2811
      if (failed_constraint != CONSTRAINT_NONE)
2812
        {
2813
          Sections_elements::iterator q;
2814
          for (q = this->sections_elements_->begin();
2815
               q != this->sections_elements_->end();
2816
               ++q)
2817
            {
2818
              if (q != p)
2819
                {
2820
                  if ((*q)->alternate_constraint(posd, failed_constraint))
2821
                    break;
2822
                }
2823
            }
2824
 
2825
          if (q == this->sections_elements_->end())
2826
            gold_error(_("no matching section constraint"));
2827
        }
2828
    }
2829
 
2830
  // Force the alignment of the first TLS section to be the maximum
2831
  // alignment of all TLS sections.
2832
  Output_section* first_tls = NULL;
2833
  uint64_t tls_align = 0;
2834
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2835
       p != this->sections_elements_->end();
2836
       ++p)
2837
    {
2838
      Output_section *os = (*p)->get_output_section();
2839
      if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
2840
        {
2841
          if (first_tls == NULL)
2842
            first_tls = os;
2843
          if (os->addralign() > tls_align)
2844
            tls_align = os->addralign();
2845
        }
2846
    }
2847
  if (first_tls != NULL)
2848
    first_tls->set_addralign(tls_align);
2849
 
2850
  // For a relocatable link, we implicitly set dot to zero.
2851
  uint64_t dot_value = 0;
2852
  uint64_t load_address = 0;
2853
  for (Sections_elements::iterator p = this->sections_elements_->begin();
2854
       p != this->sections_elements_->end();
2855
       ++p)
2856
    (*p)->set_section_addresses(symtab, layout, &dot_value, &load_address);
2857
 
2858
  if (this->phdrs_elements_ != NULL)
2859
    {
2860
      for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2861
           p != this->phdrs_elements_->end();
2862
           ++p)
2863
        (*p)->eval_load_address(symtab, layout);
2864
    }
2865
 
2866
  return this->create_segments(layout);
2867
}
2868
 
2869
// Sort the sections in order to put them into segments.
2870
 
2871
class Sort_output_sections
2872
{
2873
 public:
2874
  bool
2875
  operator()(const Output_section* os1, const Output_section* os2) const;
2876
};
2877
 
2878
bool
2879
Sort_output_sections::operator()(const Output_section* os1,
2880
                                 const Output_section* os2) const
2881
{
2882
  // Sort first by the load address.
2883
  uint64_t lma1 = (os1->has_load_address()
2884
                   ? os1->load_address()
2885
                   : os1->address());
2886
  uint64_t lma2 = (os2->has_load_address()
2887
                   ? os2->load_address()
2888
                   : os2->address());
2889
  if (lma1 != lma2)
2890
    return lma1 < lma2;
2891
 
2892
  // Then sort by the virtual address.
2893
  if (os1->address() != os2->address())
2894
    return os1->address() < os2->address();
2895
 
2896
  // Sort TLS sections to the end.
2897
  bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
2898
  bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
2899
  if (tls1 != tls2)
2900
    return tls2;
2901
 
2902
  // Sort PROGBITS before NOBITS.
2903
  if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
2904
    return true;
2905
  if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
2906
    return false;
2907
 
2908
  // Otherwise we don't care.
2909
  return false;
2910
}
2911
 
2912
// Return whether OS is a BSS section.  This is a SHT_NOBITS section.
2913
// We treat a section with the SHF_TLS flag set as taking up space
2914
// even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2915
// space for them in the file.
2916
 
2917
bool
2918
Script_sections::is_bss_section(const Output_section* os)
2919
{
2920
  return (os->type() == elfcpp::SHT_NOBITS
2921
          && (os->flags() & elfcpp::SHF_TLS) == 0);
2922
}
2923
 
2924
// Return the size taken by the file header and the program headers.
2925
 
2926
size_t
2927
Script_sections::total_header_size(Layout* layout) const
2928
{
2929
  size_t segment_count = layout->segment_count();
2930
  size_t file_header_size;
2931
  size_t segment_headers_size;
2932
  if (parameters->target().get_size() == 32)
2933
    {
2934
      file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
2935
      segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
2936
    }
2937
  else if (parameters->target().get_size() == 64)
2938
    {
2939
      file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
2940
      segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
2941
    }
2942
  else
2943
    gold_unreachable();
2944
 
2945
  return file_header_size + segment_headers_size;
2946
}
2947
 
2948
// Return the amount we have to subtract from the LMA to accomodate
2949
// headers of the given size.  The complication is that the file
2950
// header have to be at the start of a page, as otherwise it will not
2951
// be at the start of the file.
2952
 
2953
uint64_t
2954
Script_sections::header_size_adjustment(uint64_t lma,
2955
                                        size_t sizeof_headers) const
2956
{
2957
  const uint64_t abi_pagesize = parameters->target().abi_pagesize();
2958
  uint64_t hdr_lma = lma - sizeof_headers;
2959
  hdr_lma &= ~(abi_pagesize - 1);
2960
  return lma - hdr_lma;
2961
}
2962
 
2963
// Create the PT_LOAD segments when using a SECTIONS clause.  Returns
2964
// the segment which should hold the file header and segment headers,
2965
// if any.
2966
 
2967
Output_segment*
2968
Script_sections::create_segments(Layout* layout)
2969
{
2970
  gold_assert(this->saw_sections_clause_);
2971
 
2972
  if (parameters->options().relocatable())
2973
    return NULL;
2974
 
2975
  if (this->saw_phdrs_clause())
2976
    return create_segments_from_phdrs_clause(layout);
2977
 
2978
  Layout::Section_list sections;
2979
  layout->get_allocated_sections(&sections);
2980
 
2981
  // Sort the sections by address.
2982
  std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
2983
 
2984
  this->create_note_and_tls_segments(layout, &sections);
2985
 
2986
  // Walk through the sections adding them to PT_LOAD segments.
2987
  const uint64_t abi_pagesize = parameters->target().abi_pagesize();
2988
  Output_segment* first_seg = NULL;
2989
  Output_segment* current_seg = NULL;
2990
  bool is_current_seg_readonly = true;
2991
  Layout::Section_list::iterator plast = sections.end();
2992
  uint64_t last_vma = 0;
2993
  uint64_t last_lma = 0;
2994
  uint64_t last_size = 0;
2995
  for (Layout::Section_list::iterator p = sections.begin();
2996
       p != sections.end();
2997
       ++p)
2998
    {
2999
      const uint64_t vma = (*p)->address();
3000
      const uint64_t lma = ((*p)->has_load_address()
3001
                            ? (*p)->load_address()
3002
                            : vma);
3003
      const uint64_t size = (*p)->current_data_size();
3004
 
3005
      bool need_new_segment;
3006
      if (current_seg == NULL)
3007
        need_new_segment = true;
3008
      else if (lma - vma != last_lma - last_vma)
3009
        {
3010
          // This section has a different LMA relationship than the
3011
          // last one; we need a new segment.
3012
          need_new_segment = true;
3013
        }
3014
      else if (align_address(last_lma + last_size, abi_pagesize)
3015
               < align_address(lma, abi_pagesize))
3016
        {
3017
          // Putting this section in the segment would require
3018
          // skipping a page.
3019
          need_new_segment = true;
3020
        }
3021
      else if (is_bss_section(*plast) && !is_bss_section(*p))
3022
        {
3023
          // A non-BSS section can not follow a BSS section in the
3024
          // same segment.
3025
          need_new_segment = true;
3026
        }
3027
      else if (is_current_seg_readonly
3028
               && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3029
               && !parameters->options().omagic())
3030
        {
3031
          // Don't put a writable section in the same segment as a
3032
          // non-writable section.
3033
          need_new_segment = true;
3034
        }
3035
      else
3036
        {
3037
          // Otherwise, reuse the existing segment.
3038
          need_new_segment = false;
3039
        }
3040
 
3041
      elfcpp::Elf_Word seg_flags =
3042
        Layout::section_flags_to_segment((*p)->flags());
3043
 
3044
      if (need_new_segment)
3045
        {
3046
          current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3047
                                                    seg_flags);
3048
          current_seg->set_addresses(vma, lma);
3049
          if (first_seg == NULL)
3050
            first_seg = current_seg;
3051
          is_current_seg_readonly = true;
3052
        }
3053
 
3054
      current_seg->add_output_section(*p, seg_flags, false);
3055
 
3056
      if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3057
        is_current_seg_readonly = false;
3058
 
3059
      plast = p;
3060
      last_vma = vma;
3061
      last_lma = lma;
3062
      last_size = size;
3063
    }
3064
 
3065
  // An ELF program should work even if the program headers are not in
3066
  // a PT_LOAD segment.  However, it appears that the Linux kernel
3067
  // does not set the AT_PHDR auxiliary entry in that case.  It sets
3068
  // the load address to p_vaddr - p_offset of the first PT_LOAD
3069
  // segment.  It then sets AT_PHDR to the load address plus the
3070
  // offset to the program headers, e_phoff in the file header.  This
3071
  // fails when the program headers appear in the file before the
3072
  // first PT_LOAD segment.  Therefore, we always create a PT_LOAD
3073
  // segment to hold the file header and the program headers.  This is
3074
  // effectively what the GNU linker does, and it is slightly more
3075
  // efficient in any case.  We try to use the first PT_LOAD segment
3076
  // if we can, otherwise we make a new one.
3077
 
3078
  if (first_seg == NULL)
3079
    return NULL;
3080
 
3081
  // -n or -N mean that the program is not demand paged and there is
3082
  // no need to put the program headers in a PT_LOAD segment.
3083
  if (parameters->options().nmagic() || parameters->options().omagic())
3084
    return NULL;
3085
 
3086
  size_t sizeof_headers = this->total_header_size(layout);
3087
 
3088
  uint64_t vma = first_seg->vaddr();
3089
  uint64_t lma = first_seg->paddr();
3090
 
3091
  uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3092
 
3093
  if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3094
    {
3095
      first_seg->set_addresses(vma - subtract, lma - subtract);
3096
      return first_seg;
3097
    }
3098
 
3099
  // If there is no room to squeeze in the headers, then punt.  The
3100
  // resulting executable probably won't run on GNU/Linux, but we
3101
  // trust that the user knows what they are doing.
3102
  if (lma < subtract || vma < subtract)
3103
    return NULL;
3104
 
3105
  Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3106
                                                         elfcpp::PF_R);
3107
  load_seg->set_addresses(vma - subtract, lma - subtract);
3108
 
3109
  return load_seg;
3110
}
3111
 
3112
// Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3113
// segment if there are any SHT_TLS sections.
3114
 
3115
void
3116
Script_sections::create_note_and_tls_segments(
3117
    Layout* layout,
3118
    const Layout::Section_list* sections)
3119
{
3120
  gold_assert(!this->saw_phdrs_clause());
3121
 
3122
  bool saw_tls = false;
3123
  for (Layout::Section_list::const_iterator p = sections->begin();
3124
       p != sections->end();
3125
       ++p)
3126
    {
3127
      if ((*p)->type() == elfcpp::SHT_NOTE)
3128
        {
3129
          elfcpp::Elf_Word seg_flags =
3130
            Layout::section_flags_to_segment((*p)->flags());
3131
          Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3132
                                                             seg_flags);
3133
          oseg->add_output_section(*p, seg_flags, false);
3134
 
3135
          // Incorporate any subsequent SHT_NOTE sections, in the
3136
          // hopes that the script is sensible.
3137
          Layout::Section_list::const_iterator pnext = p + 1;
3138
          while (pnext != sections->end()
3139
                 && (*pnext)->type() == elfcpp::SHT_NOTE)
3140
            {
3141
              seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3142
              oseg->add_output_section(*pnext, seg_flags, false);
3143
              p = pnext;
3144
              ++pnext;
3145
            }
3146
        }
3147
 
3148
      if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3149
        {
3150
          if (saw_tls)
3151
            gold_error(_("TLS sections are not adjacent"));
3152
 
3153
          elfcpp::Elf_Word seg_flags =
3154
            Layout::section_flags_to_segment((*p)->flags());
3155
          Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3156
                                                             seg_flags);
3157
          oseg->add_output_section(*p, seg_flags, false);
3158
 
3159
          Layout::Section_list::const_iterator pnext = p + 1;
3160
          while (pnext != sections->end()
3161
                 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3162
            {
3163
              seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3164
              oseg->add_output_section(*pnext, seg_flags, false);
3165
              p = pnext;
3166
              ++pnext;
3167
            }
3168
 
3169
          saw_tls = true;
3170
        }
3171
    }
3172
}
3173
 
3174
// Add a program header.  The PHDRS clause is syntactically distinct
3175
// from the SECTIONS clause, but we implement it with the SECTIONS
3176
// support because PHDRS is useless if there is no SECTIONS clause.
3177
 
3178
void
3179
Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3180
                          bool includes_filehdr, bool includes_phdrs,
3181
                          bool is_flags_valid, unsigned int flags,
3182
                          Expression* load_address)
3183
{
3184
  if (this->phdrs_elements_ == NULL)
3185
    this->phdrs_elements_ = new Phdrs_elements();
3186
  this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3187
                                                     includes_filehdr,
3188
                                                     includes_phdrs,
3189
                                                     is_flags_valid, flags,
3190
                                                     load_address));
3191
}
3192
 
3193
// Return the number of segments we expect to create based on the
3194
// SECTIONS clause.  This is used to implement SIZEOF_HEADERS.
3195
 
3196
size_t
3197
Script_sections::expected_segment_count(const Layout* layout) const
3198
{
3199
  if (this->saw_phdrs_clause())
3200
    return this->phdrs_elements_->size();
3201
 
3202
  Layout::Section_list sections;
3203
  layout->get_allocated_sections(&sections);
3204
 
3205
  // We assume that we will need two PT_LOAD segments.
3206
  size_t ret = 2;
3207
 
3208
  bool saw_note = false;
3209
  bool saw_tls = false;
3210
  for (Layout::Section_list::const_iterator p = sections.begin();
3211
       p != sections.end();
3212
       ++p)
3213
    {
3214
      if ((*p)->type() == elfcpp::SHT_NOTE)
3215
        {
3216
          // Assume that all note sections will fit into a single
3217
          // PT_NOTE segment.
3218
          if (!saw_note)
3219
            {
3220
              ++ret;
3221
              saw_note = true;
3222
            }
3223
        }
3224
      else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3225
        {
3226
          // There can only be one PT_TLS segment.
3227
          if (!saw_tls)
3228
            {
3229
              ++ret;
3230
              saw_tls = true;
3231
            }
3232
        }
3233
    }
3234
 
3235
  return ret;
3236
}
3237
 
3238
// Create the segments from a PHDRS clause.  Return the segment which
3239
// should hold the file header and program headers, if any.
3240
 
3241
Output_segment*
3242
Script_sections::create_segments_from_phdrs_clause(Layout* layout)
3243
{
3244
  this->attach_sections_using_phdrs_clause(layout);
3245
  return this->set_phdrs_clause_addresses(layout);
3246
}
3247
 
3248
// Create the segments from the PHDRS clause, and put the output
3249
// sections in them.
3250
 
3251
void
3252
Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
3253
{
3254
  typedef std::map<std::string, Output_segment*> Name_to_segment;
3255
  Name_to_segment name_to_segment;
3256
  for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3257
       p != this->phdrs_elements_->end();
3258
       ++p)
3259
    name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
3260
 
3261
  // Walk through the output sections and attach them to segments.
3262
  // Output sections in the script which do not list segments are
3263
  // attached to the same set of segments as the immediately preceding
3264
  // output section.
3265
 
3266
  String_list* phdr_names = NULL;
3267
  bool load_segments_only = false;
3268
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3269
       p != this->sections_elements_->end();
3270
       ++p)
3271
    {
3272
      bool orphan;
3273
      String_list* old_phdr_names = phdr_names;
3274
      Output_section* os = (*p)->allocate_to_segment(&phdr_names, &orphan);
3275
      if (os == NULL)
3276
        continue;
3277
 
3278
      if (phdr_names == NULL)
3279
        {
3280
          gold_error(_("allocated section not in any segment"));
3281
          continue;
3282
        }
3283
 
3284
      // We see a list of segments names.  Disable PT_LOAD segment only
3285
      // filtering.
3286
      if (old_phdr_names != phdr_names)
3287
        load_segments_only = false;
3288
 
3289
      // If this is an orphan section--one that was not explicitly
3290
      // mentioned in the linker script--then it should not inherit
3291
      // any segment type other than PT_LOAD.  Otherwise, e.g., the
3292
      // PT_INTERP segment will pick up following orphan sections,
3293
      // which does not make sense.  If this is not an orphan section,
3294
      // we trust the linker script.
3295
      if (orphan)
3296
        {
3297
          // Enable PT_LOAD segments only filtering until we see another
3298
          // list of segment names.
3299
          load_segments_only = true;
3300
        }
3301
 
3302
      bool in_load_segment = false;
3303
      for (String_list::const_iterator q = phdr_names->begin();
3304
           q != phdr_names->end();
3305
           ++q)
3306
        {
3307
          Name_to_segment::const_iterator r = name_to_segment.find(*q);
3308
          if (r == name_to_segment.end())
3309
            gold_error(_("no segment %s"), q->c_str());
3310
          else
3311
            {
3312
              if (load_segments_only
3313
                  && r->second->type() != elfcpp::PT_LOAD)
3314
                continue;
3315
 
3316
              elfcpp::Elf_Word seg_flags =
3317
                Layout::section_flags_to_segment(os->flags());
3318
              r->second->add_output_section(os, seg_flags, false);
3319
 
3320
              if (r->second->type() == elfcpp::PT_LOAD)
3321
                {
3322
                  if (in_load_segment)
3323
                    gold_error(_("section in two PT_LOAD segments"));
3324
                  in_load_segment = true;
3325
                }
3326
            }
3327
        }
3328
 
3329
      if (!in_load_segment)
3330
        gold_error(_("allocated section not in any PT_LOAD segment"));
3331
    }
3332
}
3333
 
3334
// Set the addresses for segments created from a PHDRS clause.  Return
3335
// the segment which should hold the file header and program headers,
3336
// if any.
3337
 
3338
Output_segment*
3339
Script_sections::set_phdrs_clause_addresses(Layout* layout)
3340
{
3341
  Output_segment* load_seg = NULL;
3342
  for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3343
       p != this->phdrs_elements_->end();
3344
       ++p)
3345
    {
3346
      // Note that we have to set the flags after adding the output
3347
      // sections to the segment, as adding an output segment can
3348
      // change the flags.
3349
      (*p)->set_flags_if_valid();
3350
 
3351
      Output_segment* oseg = (*p)->segment();
3352
 
3353
      if (oseg->type() != elfcpp::PT_LOAD)
3354
        {
3355
          // The addresses of non-PT_LOAD segments are set from the
3356
          // PT_LOAD segments.
3357
          if ((*p)->has_load_address())
3358
            gold_error(_("may only specify load address for PT_LOAD segment"));
3359
          continue;
3360
        }
3361
 
3362
      // The output sections should have addresses from the SECTIONS
3363
      // clause.  The addresses don't have to be in order, so find the
3364
      // one with the lowest load address.  Use that to set the
3365
      // address of the segment.
3366
 
3367
      Output_section* osec = oseg->section_with_lowest_load_address();
3368
      if (osec == NULL)
3369
        {
3370
          oseg->set_addresses(0, 0);
3371
          continue;
3372
        }
3373
 
3374
      uint64_t vma = osec->address();
3375
      uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
3376
 
3377
      // Override the load address of the section with the load
3378
      // address specified for the segment.
3379
      if ((*p)->has_load_address())
3380
        {
3381
          if (osec->has_load_address())
3382
            gold_warning(_("PHDRS load address overrides "
3383
                           "section %s load address"),
3384
                         osec->name());
3385
 
3386
          lma = (*p)->load_address();
3387
        }
3388
 
3389
      bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
3390
      if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
3391
        {
3392
          // We could support this if we wanted to.
3393
          gold_error(_("using only one of FILEHDR and PHDRS is "
3394
                       "not currently supported"));
3395
        }
3396
      if (headers)
3397
        {
3398
          size_t sizeof_headers = this->total_header_size(layout);
3399
          uint64_t subtract = this->header_size_adjustment(lma,
3400
                                                           sizeof_headers);
3401
          if (lma >= subtract && vma >= subtract)
3402
            {
3403
              lma -= subtract;
3404
              vma -= subtract;
3405
            }
3406
          else
3407
            {
3408
              gold_error(_("sections loaded on first page without room "
3409
                           "for file and program headers "
3410
                           "are not supported"));
3411
            }
3412
 
3413
          if (load_seg != NULL)
3414
            gold_error(_("using FILEHDR and PHDRS on more than one "
3415
                         "PT_LOAD segment is not currently supported"));
3416
          load_seg = oseg;
3417
        }
3418
 
3419
      oseg->set_addresses(vma, lma);
3420
    }
3421
 
3422
  return load_seg;
3423
}
3424
 
3425
// Add the file header and segment headers to non-load segments
3426
// specified in the PHDRS clause.
3427
 
3428
void
3429
Script_sections::put_headers_in_phdrs(Output_data* file_header,
3430
                                      Output_data* segment_headers)
3431
{
3432
  gold_assert(this->saw_phdrs_clause());
3433
  for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3434
       p != this->phdrs_elements_->end();
3435
       ++p)
3436
    {
3437
      if ((*p)->type() != elfcpp::PT_LOAD)
3438
        {
3439
          if ((*p)->includes_phdrs())
3440
            (*p)->segment()->add_initial_output_data(segment_headers);
3441
          if ((*p)->includes_filehdr())
3442
            (*p)->segment()->add_initial_output_data(file_header);
3443
        }
3444
    }
3445
}
3446
 
3447
// Look for an output section by name and return the address, the load
3448
// address, the alignment, and the size.  This is used when an
3449
// expression refers to an output section which was not actually
3450
// created.  This returns true if the section was found, false
3451
// otherwise.
3452
 
3453
bool
3454
Script_sections::get_output_section_info(const char* name, uint64_t* address,
3455
                                         uint64_t* load_address,
3456
                                         uint64_t* addralign,
3457
                                         uint64_t* size) const
3458
{
3459
  if (!this->saw_sections_clause_)
3460
    return false;
3461
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3462
       p != this->sections_elements_->end();
3463
       ++p)
3464
    if ((*p)->get_output_section_info(name, address, load_address, addralign,
3465
                                      size))
3466
      return true;
3467
  return false;
3468
}
3469
 
3470
// Release all Output_segments.  This remove all pointers to all
3471
// Output_segments.
3472
 
3473
void
3474
Script_sections::release_segments()
3475
{
3476
  if (this->saw_phdrs_clause())
3477
    {
3478
      for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3479
           p != this->phdrs_elements_->end();
3480
           ++p)
3481
        (*p)->release_segment();
3482
    }
3483
}
3484
 
3485
// Print the SECTIONS clause to F for debugging.
3486
 
3487
void
3488
Script_sections::print(FILE* f) const
3489
{
3490
  if (!this->saw_sections_clause_)
3491
    return;
3492
 
3493
  fprintf(f, "SECTIONS {\n");
3494
 
3495
  for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3496
       p != this->sections_elements_->end();
3497
       ++p)
3498
    (*p)->print(f);
3499
 
3500
  fprintf(f, "}\n");
3501
 
3502
  if (this->phdrs_elements_ != NULL)
3503
    {
3504
      fprintf(f, "PHDRS {\n");
3505
      for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3506
           p != this->phdrs_elements_->end();
3507
           ++p)
3508
        (*p)->print(f);
3509
      fprintf(f, "}\n");
3510
    }
3511
}
3512
 
3513
} // End namespace gold.

powered by: WebSVN 2.1.0

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