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