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