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