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