Automatic date update in version.in
[deliverable/binutils-gdb.git] / gold / script-sections.cc
CommitLineData
494e05f4
ILT
1// script-sections.cc -- linker script SECTIONS for gold
2
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
ILT
1459 set_section_name(const std::string name)
1460 { this->section_name_ = name; }
20e6d0d6
DK
1461
1462 // Return the section size.
1463 uint64_t
1464 size() const
1465 { return this->size_; }
1466
1467 // Set the section size.
1468 void
2ea97941
ILT
1469 set_size(uint64_t size)
1470 { this->size_ = size; }
20e6d0d6
DK
1471
1472 // Return the address alignment.
1473 uint64_t
1474 addralign() const
1475 { return this->addralign_; }
1476
1477 // Set the address alignment.
1478 void
2ea97941
ILT
1479 set_addralign(uint64_t addralign)
1480 { this->addralign_ = addralign; }
20e6d0d6
DK
1481
1482 private:
1483 // Input section, can be a relaxed section.
6625d24e 1484 Output_section::Input_section input_section_;
20e6d0d6
DK
1485 // Name of the section.
1486 std::string section_name_;
1487 // Section size.
1488 uint64_t size_;
1489 // Address alignment.
1490 uint64_t addralign_;
a445fddf
ILT
1491};
1492
1493// A class to sort the input sections.
1494
1495class Input_section_sorter
1496{
1497 public:
1498 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1499 : filename_sort_(filename_sort), section_sort_(section_sort)
1500 { }
1501
1502 bool
1503 operator()(const Input_section_info&, const Input_section_info&) const;
1504
1505 private:
1506 Sort_wildcard filename_sort_;
1507 Sort_wildcard section_sort_;
1508};
1509
1510bool
1511Input_section_sorter::operator()(const Input_section_info& isi1,
1512 const Input_section_info& isi2) const
1513{
1514 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1515 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1516 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
20e6d0d6 1517 && isi1.addralign() == isi2.addralign()))
a445fddf 1518 {
20e6d0d6
DK
1519 if (isi1.section_name() != isi2.section_name())
1520 return isi1.section_name() < isi2.section_name();
a445fddf
ILT
1521 }
1522 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1523 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1524 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1525 {
20e6d0d6
DK
1526 if (isi1.addralign() != isi2.addralign())
1527 return isi1.addralign() < isi2.addralign();
a445fddf
ILT
1528 }
1529 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1530 {
20e6d0d6
DK
1531 if (isi1.relobj()->name() != isi2.relobj()->name())
1532 return (isi1.relobj()->name() < isi2.relobj()->name());
a445fddf
ILT
1533 }
1534
1535 // Otherwise we leave them in the same order.
1536 return false;
1537}
1538
1539// Set the section address. Look in INPUT_SECTIONS for sections which
1540// match this spec, sort them as specified, and add them to the output
1541// section.
1542
1543void
1544Output_section_element_input::set_section_addresses(
1545 Symbol_table*,
20e6d0d6 1546 Layout* layout,
a445fddf
ILT
1547 Output_section* output_section,
1548 uint64_t subalign,
1549 uint64_t* dot_value,
f6973bdc 1550 uint64_t*,
77e65537 1551 Output_section** dot_section,
a445fddf
ILT
1552 std::string* fill,
1553 Input_section_list* input_sections)
1554{
1555 // We build a list of sections which match each
1556 // Input_section_pattern.
1557
1558 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1559 size_t input_pattern_count = this->input_section_patterns_.size();
1560 if (input_pattern_count == 0)
1561 input_pattern_count = 1;
1562 Matching_sections matching_sections(input_pattern_count);
1563
1564 // Look through the list of sections for this output section. Add
1565 // each one which matches to one of the elements of
1566 // MATCHING_SECTIONS.
1567
1568 Input_section_list::iterator p = input_sections->begin();
1569 while (p != input_sections->end())
1570 {
20e6d0d6
DK
1571 Relobj* relobj = p->relobj();
1572 unsigned int shndx = p->shndx();
1573 Input_section_info isi(*p);
1574
a445fddf
ILT
1575 // Calling section_name and section_addralign is not very
1576 // efficient.
a445fddf
ILT
1577
1578 // Lock the object so that we can get information about the
1579 // section. This is OK since we know we are single-threaded
1580 // here.
1581 {
1582 const Task* task = reinterpret_cast<const Task*>(-1);
20e6d0d6
DK
1583 Task_lock_obj<Object> tl(task, relobj);
1584
1585 isi.set_section_name(relobj->section_name(shndx));
1586 if (p->is_relaxed_input_section())
c0a62865 1587 {
ea5cae92 1588 // We use current data size because relaxed section sizes may not
c0a62865
DK
1589 // have finalized yet.
1590 isi.set_size(p->relaxed_input_section()->current_data_size());
1591 isi.set_addralign(p->relaxed_input_section()->addralign());
1592 }
20e6d0d6 1593 else
c0a62865
DK
1594 {
1595 isi.set_size(relobj->section_size(shndx));
1596 isi.set_addralign(relobj->section_addralign(shndx));
1597 }
a445fddf
ILT
1598 }
1599
20e6d0d6 1600 if (!this->match_file_name(relobj->name().c_str()))
a445fddf
ILT
1601 ++p;
1602 else if (this->input_section_patterns_.empty())
1603 {
1604 matching_sections[0].push_back(isi);
1605 p = input_sections->erase(p);
1606 }
1607 else
1608 {
1609 size_t i;
1610 for (i = 0; i < input_pattern_count; ++i)
1611 {
1612 const Input_section_pattern&
1613 isp(this->input_section_patterns_[i]);
20e6d0d6 1614 if (match(isi.section_name().c_str(), isp.pattern.c_str(),
a445fddf
ILT
1615 isp.pattern_is_wildcard))
1616 break;
1617 }
1618
1619 if (i >= this->input_section_patterns_.size())
1620 ++p;
1621 else
1622 {
1623 matching_sections[i].push_back(isi);
1624 p = input_sections->erase(p);
1625 }
1626 }
1627 }
1628
1629 // Look through MATCHING_SECTIONS. Sort each one as specified,
1630 // using a stable sort so that we get the default order when
1631 // sections are otherwise equal. Add each input section to the
1632 // output section.
1633
661be1e2 1634 uint64_t dot = *dot_value;
a445fddf
ILT
1635 for (size_t i = 0; i < input_pattern_count; ++i)
1636 {
1637 if (matching_sections[i].empty())
1638 continue;
1639
1640 gold_assert(output_section != NULL);
1641
1642 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1643 if (isp.sort != SORT_WILDCARD_NONE
1644 || this->filename_sort_ != SORT_WILDCARD_NONE)
1645 std::stable_sort(matching_sections[i].begin(),
1646 matching_sections[i].end(),
1647 Input_section_sorter(this->filename_sort_,
1648 isp.sort));
1649
2ea97941 1650 for (std::vector<Input_section_info>::const_iterator p =
a445fddf 1651 matching_sections[i].begin();
2ea97941
ILT
1652 p != matching_sections[i].end();
1653 ++p)
a445fddf 1654 {
6625d24e
DK
1655 // Override the original address alignment if SUBALIGN is specified
1656 // and is greater than the original alignment. We need to make a
1657 // copy of the input section to modify the alignment.
1658 Output_section::Input_section sis(p->input_section());
1659
1660 uint64_t this_subalign = sis.addralign();
1661 if (!sis.is_input_section())
1662 sis.output_section_data()->finalize_data_size();
1663 uint64_t data_size = sis.data_size();
a445fddf 1664 if (this_subalign < subalign)
6625d24e
DK
1665 {
1666 this_subalign = subalign;
1667 sis.set_addralign(subalign);
1668 }
a445fddf 1669
661be1e2 1670 uint64_t address = align_address(dot, this_subalign);
a445fddf 1671
661be1e2 1672 if (address > dot && !fill->empty())
a445fddf
ILT
1673 {
1674 section_size_type length =
661be1e2 1675 convert_to_section_size_type(address - dot);
a445fddf
ILT
1676 std::string this_fill = this->get_fill_string(fill, length);
1677 Output_section_data* posd = new Output_data_const(this_fill, 0);
1678 output_section->add_output_section_data(posd);
20e6d0d6 1679 layout->new_output_section_data_from_script(posd);
a445fddf
ILT
1680 }
1681
6625d24e
DK
1682 output_section->add_script_input_section(sis);
1683 dot = address + data_size;
a445fddf
ILT
1684 }
1685 }
1686
661be1e2
ILT
1687 // An SHF_TLS/SHT_NOBITS section does not take up any
1688 // address space.
1689 if (output_section == NULL
1690 || (output_section->flags() & elfcpp::SHF_TLS) == 0
1691 || output_section->type() != elfcpp::SHT_NOBITS)
1692 *dot_value = dot;
1693
a445fddf 1694 this->final_dot_value_ = *dot_value;
77e65537 1695 this->final_dot_section_ = *dot_section;
a445fddf
ILT
1696}
1697
494e05f4
ILT
1698// Print for debugging.
1699
1700void
1701Output_section_element_input::print(FILE* f) const
1702{
1703 fprintf(f, " ");
1704
1705 if (this->keep_)
1706 fprintf(f, "KEEP(");
1707
1708 if (!this->filename_pattern_.empty())
1709 {
1710 bool need_close_paren = false;
1711 switch (this->filename_sort_)
1712 {
1713 case SORT_WILDCARD_NONE:
1714 break;
1715 case SORT_WILDCARD_BY_NAME:
1716 fprintf(f, "SORT_BY_NAME(");
1717 need_close_paren = true;
1718 break;
1719 default:
1720 gold_unreachable();
1721 }
1722
1723 fprintf(f, "%s", this->filename_pattern_.c_str());
1724
1725 if (need_close_paren)
1726 fprintf(f, ")");
1727 }
1728
1729 if (!this->input_section_patterns_.empty()
1730 || !this->filename_exclusions_.empty())
1731 {
1732 fprintf(f, "(");
1733
1734 bool need_space = false;
1735 if (!this->filename_exclusions_.empty())
1736 {
1737 fprintf(f, "EXCLUDE_FILE(");
1738 bool need_comma = false;
1739 for (Filename_exclusions::const_iterator p =
1740 this->filename_exclusions_.begin();
1741 p != this->filename_exclusions_.end();
1742 ++p)
1743 {
1744 if (need_comma)
1745 fprintf(f, ", ");
a445fddf 1746 fprintf(f, "%s", p->first.c_str());
494e05f4
ILT
1747 need_comma = true;
1748 }
1749 fprintf(f, ")");
1750 need_space = true;
1751 }
1752
1753 for (Input_section_patterns::const_iterator p =
1754 this->input_section_patterns_.begin();
1755 p != this->input_section_patterns_.end();
1756 ++p)
1757 {
1758 if (need_space)
1759 fprintf(f, " ");
1760
1761 int close_parens = 0;
1762 switch (p->sort)
1763 {
1764 case SORT_WILDCARD_NONE:
1765 break;
1766 case SORT_WILDCARD_BY_NAME:
1767 fprintf(f, "SORT_BY_NAME(");
1768 close_parens = 1;
1769 break;
1770 case SORT_WILDCARD_BY_ALIGNMENT:
1771 fprintf(f, "SORT_BY_ALIGNMENT(");
1772 close_parens = 1;
1773 break;
1774 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1775 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1776 close_parens = 2;
1777 break;
1778 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1779 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1780 close_parens = 2;
1781 break;
1782 default:
1783 gold_unreachable();
1784 }
1785
1786 fprintf(f, "%s", p->pattern.c_str());
1787
1788 for (int i = 0; i < close_parens; ++i)
1789 fprintf(f, ")");
1790
1791 need_space = true;
1792 }
1793
1794 fprintf(f, ")");
1795 }
1796
1797 if (this->keep_)
1798 fprintf(f, ")");
1799
1800 fprintf(f, "\n");
1801}
1802
1803// An output section.
1804
1805class Output_section_definition : public Sections_element
1806{
1807 public:
a445fddf
ILT
1808 typedef Output_section_element::Input_section_list Input_section_list;
1809
494e05f4
ILT
1810 Output_section_definition(const char* name, size_t namelen,
1811 const Parser_output_section_header* header);
1812
1813 // Finish the output section with the information in the trailer.
1814 void
1815 finish(const Parser_output_section_trailer* trailer);
1816
1817 // Add a symbol to be defined.
1818 void
1819 add_symbol_assignment(const char* name, size_t length, Expression* value,
1820 bool provide, bool hidden);
a445fddf
ILT
1821
1822 // Add an assignment to the special dot symbol.
1823 void
1824 add_dot_assignment(Expression* value);
1825
494e05f4
ILT
1826 // Add an assertion.
1827 void
1828 add_assertion(Expression* check, const char* message, size_t messagelen);
1829
1830 // Add a data item to the current output section.
1831 void
1832 add_data(int size, bool is_signed, Expression* val);
1833
1834 // Add a setting for the fill value.
1835 void
1836 add_fill(Expression* val);
1837
1838 // Add an input section specification.
1839 void
1840 add_input_section(const Input_section_spec* spec, bool keep);
1841
0d371ad3
ILT
1842 // Return whether the output section is relro.
1843 bool
1844 is_relro() const
1845 { return this->is_relro_; }
1846
2d924fd9
ILT
1847 // Record that the output section is relro.
1848 void
1849 set_is_relro()
1850 { this->is_relro_ = true; }
1851
919ed24c
ILT
1852 // Create any required output sections.
1853 void
1854 create_sections(Layout*);
1855
a445fddf
ILT
1856 // Add any symbols being defined to the symbol table.
1857 void
1858 add_symbols_to_table(Symbol_table* symtab);
1859
1860 // Finalize symbols and check assertions.
1861 void
77e65537 1862 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
a445fddf
ILT
1863
1864 // Return the output section name to use for an input file name and
1865 // section name.
1866 const char*
1867 output_section_name(const char* file_name, const char* section_name,
b9b2ae8b
NC
1868 Output_section***, Script_sections::Section_type*,
1869 bool*);
a445fddf 1870
0d371ad3
ILT
1871 // Initialize OSP with an output section.
1872 void
1873 orphan_section_init(Orphan_section_placement* osp,
1874 Script_sections::Elements_iterator p)
1875 { osp->output_section_init(this->name_, this->output_section_, p); }
a445fddf
ILT
1876
1877 // Set the section address.
1878 void
1879 set_section_addresses(Symbol_table* symtab, Layout* layout,
f6973bdc
ILT
1880 uint64_t* dot_value, uint64_t*,
1881 uint64_t* load_address);
a445fddf 1882
3802b2dd
ILT
1883 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1884 // this section is constrained, and the input sections do not match,
1885 // return the constraint, and set *POSD.
1886 Section_constraint
1887 check_constraint(Output_section_definition** posd);
1888
1889 // See if this is the alternate output section for a constrained
1890 // output section. If it is, transfer the Output_section and return
1891 // true. Otherwise return false.
1892 bool
1893 alternate_constraint(Output_section_definition*, Section_constraint);
1894
1c4f3631 1895 // Get the list of segments to use for an allocated section when
2cefc357 1896 // using a PHDRS clause.
1c4f3631 1897 Output_section*
2cefc357 1898 allocate_to_segment(String_list** phdrs_list, bool* orphan);
1c4f3631 1899
8f2eb564
ILT
1900 // Look for an output section by name and return the address, the
1901 // load address, the alignment, and the size. This is used when an
1902 // expression refers to an output section which was not actually
1903 // created. This returns true if the section was found, false
1904 // otherwise.
1905 bool
1906 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1907 uint64_t*) const;
1908
2d924fd9
ILT
1909 // Return the associated Output_section if there is one.
1910 Output_section*
1911 get_output_section() const
1912 { return this->output_section_; }
1913
494e05f4
ILT
1914 // Print the contents to the FILE. This is for debugging.
1915 void
1916 print(FILE*) const;
1917
1e5d2fb1
DK
1918 // Return the output section type if specified or Script_sections::ST_NONE.
1919 Script_sections::Section_type
1920 section_type() const;
1921
7f8cd844
NC
1922 // Store the memory region to use.
1923 void
1924 set_memory_region(Memory_region*, bool set_vma);
1925
1926 void
1927 set_section_vma(Expression* address)
1928 { this->address_ = address; }
1929
1930 void
1931 set_section_lma(Expression* address)
1932 { this->load_address_ = address; }
1933
ea5cae92
NC
1934 const std::string&
1935 get_section_name() const
7f8cd844
NC
1936 { return this->name_; }
1937
494e05f4 1938 private:
1e5d2fb1
DK
1939 static const char*
1940 script_section_type_name(Script_section_type);
1941
494e05f4
ILT
1942 typedef std::vector<Output_section_element*> Output_section_elements;
1943
1944 // The output section name.
1945 std::string name_;
1946 // The address. This may be NULL.
1947 Expression* address_;
1948 // The load address. This may be NULL.
1949 Expression* load_address_;
1950 // The alignment. This may be NULL.
1951 Expression* align_;
1952 // The input section alignment. This may be NULL.
1953 Expression* subalign_;
3802b2dd
ILT
1954 // The constraint, if any.
1955 Section_constraint constraint_;
494e05f4
ILT
1956 // The fill value. This may be NULL.
1957 Expression* fill_;
1c4f3631
ILT
1958 // The list of segments this section should go into. This may be
1959 // NULL.
1960 String_list* phdrs_;
494e05f4
ILT
1961 // The list of elements defining the section.
1962 Output_section_elements elements_;
a445fddf
ILT
1963 // The Output_section created for this definition. This will be
1964 // NULL if none was created.
1965 Output_section* output_section_;
8f2eb564
ILT
1966 // The address after it has been evaluated.
1967 uint64_t evaluated_address_;
1968 // The load address after it has been evaluated.
1969 uint64_t evaluated_load_address_;
1970 // The alignment after it has been evaluated.
1971 uint64_t evaluated_addralign_;
2d924fd9
ILT
1972 // The output section is relro.
1973 bool is_relro_;
1e5d2fb1
DK
1974 // The output section type if specified.
1975 enum Script_section_type script_section_type_;
494e05f4
ILT
1976};
1977
1978// Constructor.
1979
1980Output_section_definition::Output_section_definition(
1981 const char* name,
1982 size_t namelen,
1983 const Parser_output_section_header* header)
1984 : name_(name, namelen),
1985 address_(header->address),
1986 load_address_(header->load_address),
1987 align_(header->align),
1988 subalign_(header->subalign),
3802b2dd 1989 constraint_(header->constraint),
494e05f4 1990 fill_(NULL),
1c4f3631 1991 phdrs_(NULL),
a445fddf 1992 elements_(),
2d924fd9
ILT
1993 output_section_(NULL),
1994 evaluated_address_(0),
1995 evaluated_load_address_(0),
1996 evaluated_addralign_(0),
1e5d2fb1
DK
1997 is_relro_(false),
1998 script_section_type_(header->section_type)
494e05f4
ILT
1999{
2000}
2001
2002// Finish an output section.
2003
2004void
2005Output_section_definition::finish(const Parser_output_section_trailer* trailer)
2006{
2007 this->fill_ = trailer->fill;
1c4f3631 2008 this->phdrs_ = trailer->phdrs;
494e05f4
ILT
2009}
2010
2011// Add a symbol to be defined.
2012
2013void
2014Output_section_definition::add_symbol_assignment(const char* name,
2015 size_t length,
2016 Expression* value,
2017 bool provide,
2018 bool hidden)
2019{
2020 Output_section_element* p = new Output_section_element_assignment(name,
2021 length,
2022 value,
2023 provide,
2024 hidden);
2025 this->elements_.push_back(p);
2026}
2027
a445fddf 2028// Add an assignment to the special dot symbol.
494e05f4
ILT
2029
2030void
a445fddf
ILT
2031Output_section_definition::add_dot_assignment(Expression* value)
2032{
2033 Output_section_element* p = new Output_section_element_dot_assignment(value);
2034 this->elements_.push_back(p);
2035}
2036
2037// Add an assertion.
2038
2039void
2040Output_section_definition::add_assertion(Expression* check,
2041 const char* message,
494e05f4
ILT
2042 size_t messagelen)
2043{
2044 Output_section_element* p = new Output_section_element_assertion(check,
2045 message,
2046 messagelen);
2047 this->elements_.push_back(p);
2048}
2049
2050// Add a data item to the current output section.
2051
2052void
2053Output_section_definition::add_data(int size, bool is_signed, Expression* val)
2054{
2055 Output_section_element* p = new Output_section_element_data(size, is_signed,
2056 val);
2057 this->elements_.push_back(p);
2058}
2059
2060// Add a setting for the fill value.
2061
2062void
2063Output_section_definition::add_fill(Expression* val)
2064{
2065 Output_section_element* p = new Output_section_element_fill(val);
2066 this->elements_.push_back(p);
2067}
2068
2069// Add an input section specification.
2070
2071void
2072Output_section_definition::add_input_section(const Input_section_spec* spec,
2073 bool keep)
2074{
2075 Output_section_element* p = new Output_section_element_input(spec, keep);
2076 this->elements_.push_back(p);
2077}
2078
919ed24c
ILT
2079// Create any required output sections. We need an output section if
2080// there is a data statement here.
2081
2082void
2083Output_section_definition::create_sections(Layout* layout)
2084{
2085 if (this->output_section_ != NULL)
2086 return;
2087 for (Output_section_elements::const_iterator p = this->elements_.begin();
2088 p != this->elements_.end();
2089 ++p)
2090 {
2091 if ((*p)->needs_output_section())
2092 {
2093 const char* name = this->name_.c_str();
1e5d2fb1
DK
2094 this->output_section_ =
2095 layout->make_output_section_for_script(name, this->section_type());
919ed24c
ILT
2096 return;
2097 }
2098 }
2099}
2100
a445fddf
ILT
2101// Add any symbols being defined to the symbol table.
2102
2103void
2104Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
2105{
2106 for (Output_section_elements::iterator p = this->elements_.begin();
2107 p != this->elements_.end();
2108 ++p)
2109 (*p)->add_symbols_to_table(symtab);
2110}
2111
2112// Finalize symbols and check assertions.
2113
2114void
2115Output_section_definition::finalize_symbols(Symbol_table* symtab,
2116 const Layout* layout,
a445fddf
ILT
2117 uint64_t* dot_value)
2118{
2119 if (this->output_section_ != NULL)
2120 *dot_value = this->output_section_->address();
2121 else
2122 {
2123 uint64_t address = *dot_value;
2124 if (this->address_ != NULL)
2125 {
919ed24c 2126 address = this->address_->eval_with_dot(symtab, layout, true,
77e65537 2127 *dot_value, NULL,
286adcf4 2128 NULL, NULL, false);
a445fddf
ILT
2129 }
2130 if (this->align_ != NULL)
2131 {
919ed24c 2132 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
bacff3ab 2133 *dot_value, NULL,
286adcf4 2134 NULL, NULL, false);
a445fddf
ILT
2135 address = align_address(address, align);
2136 }
2137 *dot_value = address;
2138 }
a445fddf 2139
77e65537 2140 Output_section* dot_section = this->output_section_;
a445fddf
ILT
2141 for (Output_section_elements::iterator p = this->elements_.begin();
2142 p != this->elements_.end();
2143 ++p)
77e65537 2144 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
a445fddf
ILT
2145}
2146
2147// Return the output section name to use for an input section name.
2148
2149const char*
1e5d2fb1
DK
2150Output_section_definition::output_section_name(
2151 const char* file_name,
2152 const char* section_name,
2153 Output_section*** slot,
b9b2ae8b
NC
2154 Script_sections::Section_type* psection_type,
2155 bool* keep)
a445fddf
ILT
2156{
2157 // Ask each element whether it matches NAME.
2158 for (Output_section_elements::const_iterator p = this->elements_.begin();
2159 p != this->elements_.end();
2160 ++p)
2161 {
b9b2ae8b 2162 if ((*p)->match_name(file_name, section_name, keep))
a445fddf
ILT
2163 {
2164 // We found a match for NAME, which means that it should go
2165 // into this output section.
2166 *slot = &this->output_section_;
1e5d2fb1 2167 *psection_type = this->section_type();
a445fddf
ILT
2168 return this->name_.c_str();
2169 }
2170 }
2171
2172 // We don't know about this section name.
2173 return NULL;
2174}
2175
ea5cae92
NC
2176// Return true if memory from START to START + LENGTH is contained
2177// within a memory region.
2178
2179bool
2180Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
2181 uint64_t start, uint64_t length) const
2182{
2183 if (this->memory_regions_ == NULL)
2184 return false;
2185
2186 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2187 mr != this->memory_regions_->end();
2188 ++mr)
2189 {
2190 uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
2191 uint64_t l = (*mr)->length()->eval(symtab, layout, false);
2192
2193 if (s <= start
2194 && (s + l) >= (start + length))
2195 return true;
2196 }
2197
2198 return false;
2199}
2200
2201// Find a memory region that should be used by a given output SECTION.
2202// If provided set PREVIOUS_SECTION_RETURN to point to the last section
2203// that used the return memory region.
2204
2205Memory_region*
2206Script_sections::find_memory_region(
2207 Output_section_definition* section,
2208 bool find_vma_region,
2209 Output_section_definition** previous_section_return)
2210{
2211 if (previous_section_return != NULL)
2212 * previous_section_return = NULL;
2213
2214 // Walk the memory regions specified in this script, if any.
2215 if (this->memory_regions_ == NULL)
2216 return NULL;
2217
2218 // The /DISCARD/ section never gets assigned to any region.
2219 if (section->get_section_name() == "/DISCARD/")
2220 return NULL;
2221
2222 Memory_region* first_match = NULL;
2223
2224 // First check to see if a region has been assigned to this section.
2225 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2226 mr != this->memory_regions_->end();
2227 ++mr)
2228 {
2229 if (find_vma_region)
2230 {
2231 for (Memory_region::Section_list::const_iterator s =
2232 (*mr)->get_vma_section_list_start();
2233 s != (*mr)->get_vma_section_list_end();
2234 ++s)
2235 if ((*s) == section)
2236 {
2237 (*mr)->set_last_section(section);
2238 return *mr;
2239 }
2240 }
2241 else
2242 {
2243 for (Memory_region::Section_list::const_iterator s =
2244 (*mr)->get_lma_section_list_start();
2245 s != (*mr)->get_lma_section_list_end();
2246 ++s)
2247 if ((*s) == section)
2248 {
2249 (*mr)->set_last_section(section);
2250 return *mr;
2251 }
2252 }
2253
2254 // Make a note of the first memory region whose attributes
2255 // are compatible with the section. If we do not find an
2256 // explicit region assignment, then we will return this region.
2257 Output_section* out_sec = section->get_output_section();
2258 if (first_match == NULL
3f9a3278 2259 && out_sec != NULL
ea5cae92
NC
2260 && (*mr)->attributes_compatible(out_sec->flags(),
2261 out_sec->type()))
2262 first_match = *mr;
2263 }
2264
2265 // With LMA computations, if an explicit region has not been specified then
2266 // we will want to set the difference between the VMA and the LMA of the
2267 // section were searching for to be the same as the difference between the
2268 // VMA and LMA of the last section to be added to first matched region.
2269 // Hence, if it was asked for, we return a pointer to the last section
2270 // known to be used by the first matched region.
2271 if (first_match != NULL
2272 && previous_section_return != NULL)
2273 *previous_section_return = first_match->get_last_section();
2274
2275 return first_match;
2276}
2277
a445fddf
ILT
2278// Set the section address. Note that the OUTPUT_SECTION_ field will
2279// be NULL if no input sections were mapped to this output section.
2280// We still have to adjust dot and process symbol assignments.
2281
2282void
2283Output_section_definition::set_section_addresses(Symbol_table* symtab,
2284 Layout* layout,
fd247bfe 2285 uint64_t* dot_value,
f6973bdc 2286 uint64_t* dot_alignment,
fd247bfe 2287 uint64_t* load_address)
a445fddf 2288{
ea5cae92
NC
2289 Memory_region* vma_region = NULL;
2290 Memory_region* lma_region = NULL;
2291 Script_sections* script_sections =
2292 layout->script_options()->script_sections();
a445fddf 2293 uint64_t address;
1e5d2fb1
DK
2294 uint64_t old_dot_value = *dot_value;
2295 uint64_t old_load_address = *load_address;
2296
9e9143bc
ST
2297 // If input section sorting is requested via --section-ordering-file or
2298 // linker plugins, then do it here. This is important because we want
2299 // any sorting specified in the linker scripts, which will be done after
2300 // this, to take precedence. The final order of input sections is then
2301 // guaranteed to be according to the linker script specification.
2302 if (this->output_section_ != NULL
2303 && this->output_section_->input_section_order_specified())
2304 this->output_section_->sort_attached_input_sections();
2305
ea5cae92
NC
2306 // Decide the start address for the section. The algorithm is:
2307 // 1) If an address has been specified in a linker script, use that.
2308 // 2) Otherwise if a memory region has been specified for the section,
2309 // use the next free address in the region.
2310 // 3) Otherwise if memory regions have been specified find the first
2311 // region whose attributes are compatible with this section and
2312 // install it into that region.
2313 // 4) Otherwise use the current location counter.
2314
2315 if (this->output_section_ != NULL
2316 // Check for --section-start.
2317 && parameters->options().section_start(this->output_section_->name(),
2318 &address))
2319 ;
2320 else if (this->address_ == NULL)
a445fddf 2321 {
ea5cae92
NC
2322 vma_region = script_sections->find_memory_region(this, true, NULL);
2323
2324 if (vma_region != NULL)
2325 address = vma_region->get_current_address()->eval(symtab, layout,
2326 false);
f4187277 2327 else
ea5cae92 2328 address = *dot_value;
a445fddf 2329 }
ea5cae92
NC
2330 else
2331 address = this->address_->eval_with_dot(symtab, layout, true,
2332 *dot_value, NULL, NULL,
286adcf4 2333 dot_alignment, false);
a445fddf
ILT
2334 uint64_t align;
2335 if (this->align_ == NULL)
2336 {
2337 if (this->output_section_ == NULL)
2338 align = 0;
2339 else
2340 align = this->output_section_->addralign();
2341 }
2342 else
2343 {
77e65537 2344 Output_section* align_section;
919ed24c 2345 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
286adcf4 2346 NULL, &align_section, NULL, false);
77e65537
ILT
2347 if (align_section != NULL)
2348 gold_warning(_("alignment of section %s is not absolute"),
2349 this->name_.c_str());
a445fddf
ILT
2350 if (this->output_section_ != NULL)
2351 this->output_section_->set_addralign(align);
2352 }
2353
2354 address = align_address(address, align);
2355
fd247bfe
ILT
2356 uint64_t start_address = address;
2357
a445fddf 2358 *dot_value = address;
a445fddf 2359
1e5d2fb1
DK
2360 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2361 // forced to zero, regardless of what the linker script wants.
a445fddf 2362 if (this->output_section_ != NULL
1e5d2fb1
DK
2363 && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
2364 || this->output_section_->is_noload()))
a445fddf
ILT
2365 this->output_section_->set_address(address);
2366
8f2eb564
ILT
2367 this->evaluated_address_ = address;
2368 this->evaluated_addralign_ = align;
2369
ea5cae92
NC
2370 uint64_t laddr;
2371
8f2eb564 2372 if (this->load_address_ == NULL)
ea5cae92
NC
2373 {
2374 Output_section_definition* previous_section;
2375
2376 // Determine if an LMA region has been set for this section.
2377 lma_region = script_sections->find_memory_region(this, false,
2378 &previous_section);
2379
2380 if (lma_region != NULL)
2381 {
2382 if (previous_section == NULL)
2383 // The LMA address was explicitly set to the given region.
2384 laddr = lma_region->get_current_address()->eval(symtab, layout,
2385 false);
2386 else
2387 {
2388 // We are not going to use the discovered lma_region, so
2389 // make sure that we do not update it in the code below.
2390 lma_region = NULL;
2391
2392 if (this->address_ != NULL || previous_section == this)
2393 {
2394 // Either an explicit VMA address has been set, or an
2395 // explicit VMA region has been set, so set the LMA equal to
2396 // the VMA.
2397 laddr = address;
2398 }
2399 else
2400 {
2401 // The LMA address was not explicitly or implicitly set.
2402 //
2403 // We have been given the first memory region that is
2404 // compatible with the current section and a pointer to the
2405 // last section to use this region. Set the LMA of this
2406 // section so that the difference between its' VMA and LMA
2407 // is the same as the difference between the VMA and LMA of
2408 // the last section in the given region.
2409 laddr = address + (previous_section->evaluated_load_address_
2410 - previous_section->evaluated_address_);
2411 }
2412 }
2413
2414 if (this->output_section_ != NULL)
2415 this->output_section_->set_load_address(laddr);
2416 }
2417 else
2418 {
2419 // Do not set the load address of the output section, if one exists.
2420 // This allows future sections to determine what the load address
2421 // should be. If none is ever set, it will default to being the
2422 // same as the vma address.
2423 laddr = address;
2424 }
2425 }
8f2eb564 2426 else
a445fddf 2427 {
ea5cae92
NC
2428 laddr = this->load_address_->eval_with_dot(symtab, layout, true,
2429 *dot_value,
2430 this->output_section_,
286adcf4 2431 NULL, NULL, false);
8f2eb564 2432 if (this->output_section_ != NULL)
55458500 2433 this->output_section_->set_load_address(laddr);
a445fddf
ILT
2434 }
2435
ea5cae92
NC
2436 this->evaluated_load_address_ = laddr;
2437
a445fddf
ILT
2438 uint64_t subalign;
2439 if (this->subalign_ == NULL)
2440 subalign = 0;
2441 else
2442 {
77e65537 2443 Output_section* subalign_section;
919ed24c
ILT
2444 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2445 *dot_value, NULL,
286adcf4
CC
2446 &subalign_section, NULL,
2447 false);
77e65537
ILT
2448 if (subalign_section != NULL)
2449 gold_warning(_("subalign of section %s is not absolute"),
2450 this->name_.c_str());
a445fddf
ILT
2451 }
2452
2453 std::string fill;
2454 if (this->fill_ != NULL)
2455 {
2456 // FIXME: The GNU linker supports fill values of arbitrary
2457 // length.
77e65537 2458 Output_section* fill_section;
919ed24c 2459 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
a445fddf 2460 *dot_value,
f6973bdc 2461 NULL, &fill_section,
286adcf4 2462 NULL, false);
77e65537
ILT
2463 if (fill_section != NULL)
2464 gold_warning(_("fill of section %s is not absolute"),
2465 this->name_.c_str());
a445fddf
ILT
2466 unsigned char fill_buff[4];
2467 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2468 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2469 }
2470
2471 Input_section_list input_sections;
2472 if (this->output_section_ != NULL)
2473 {
2474 // Get the list of input sections attached to this output
2475 // section. This will leave the output section with only
2476 // Output_section_data entries.
2477 address += this->output_section_->get_input_sections(address,
2478 fill,
2479 &input_sections);
2480 *dot_value = address;
2481 }
2482
77e65537 2483 Output_section* dot_section = this->output_section_;
a445fddf
ILT
2484 for (Output_section_elements::iterator p = this->elements_.begin();
2485 p != this->elements_.end();
2486 ++p)
2487 (*p)->set_section_addresses(symtab, layout, this->output_section_,
f6973bdc
ILT
2488 subalign, dot_value, dot_alignment,
2489 &dot_section, &fill, &input_sections);
a445fddf
ILT
2490
2491 gold_assert(input_sections.empty());
fd247bfe 2492
ea5cae92
NC
2493 if (vma_region != NULL)
2494 {
2495 // Update the VMA region being used by the section now that we know how
2496 // big it is. Use the current address in the region, rather than
2497 // start_address because that might have been aligned upwards and we
2498 // need to allow for the padding.
2499 Expression* addr = vma_region->get_current_address();
2500 uint64_t size = *dot_value - addr->eval(symtab, layout, false);
2501
2502 vma_region->increment_offset(this->get_section_name(), size,
2503 symtab, layout);
2504 }
2505
2506 // If the LMA region is different from the VMA region, then increment the
2507 // offset there as well. Note that we use the same "dot_value -
2508 // start_address" formula that is used in the load_address assignment below.
2509 if (lma_region != NULL && lma_region != vma_region)
2510 lma_region->increment_offset(this->get_section_name(),
2511 *dot_value - start_address,
2512 symtab, layout);
2513
2514 // Compute the load address for the following section.
2515 if (this->output_section_ == NULL)
fd247bfe 2516 *load_address = *dot_value;
ea5cae92
NC
2517 else if (this->load_address_ == NULL)
2518 {
2519 if (lma_region == NULL)
2520 *load_address = *dot_value;
2521 else
2522 *load_address =
2523 lma_region->get_current_address()->eval(symtab, layout, false);
2524 }
fd247bfe
ILT
2525 else
2526 *load_address = (this->output_section_->load_address()
2527 + (*dot_value - start_address));
2d924fd9
ILT
2528
2529 if (this->output_section_ != NULL)
2530 {
2531 if (this->is_relro_)
2532 this->output_section_->set_is_relro();
2533 else
2534 this->output_section_->clear_is_relro();
1e5d2fb1
DK
2535
2536 // If this is a NOLOAD section, keep dot and load address unchanged.
2537 if (this->output_section_->is_noload())
2538 {
2539 *dot_value = old_dot_value;
2540 *load_address = old_load_address;
2541 }
2d924fd9 2542 }
a445fddf
ILT
2543}
2544
3802b2dd
ILT
2545// Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2546// this section is constrained, and the input sections do not match,
2547// return the constraint, and set *POSD.
2548
2549Section_constraint
2550Output_section_definition::check_constraint(Output_section_definition** posd)
2551{
2552 switch (this->constraint_)
2553 {
2554 case CONSTRAINT_NONE:
2555 return CONSTRAINT_NONE;
2556
2557 case CONSTRAINT_ONLY_IF_RO:
2558 if (this->output_section_ != NULL
2559 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2560 {
2561 *posd = this;
2562 return CONSTRAINT_ONLY_IF_RO;
2563 }
2564 return CONSTRAINT_NONE;
2565
2566 case CONSTRAINT_ONLY_IF_RW:
2567 if (this->output_section_ != NULL
2568 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2569 {
2570 *posd = this;
2571 return CONSTRAINT_ONLY_IF_RW;
2572 }
2573 return CONSTRAINT_NONE;
2574
2575 case CONSTRAINT_SPECIAL:
2576 if (this->output_section_ != NULL)
2577 gold_error(_("SPECIAL constraints are not implemented"));
2578 return CONSTRAINT_NONE;
2579
2580 default:
2581 gold_unreachable();
2582 }
2583}
2584
2585// See if this is the alternate output section for a constrained
2586// output section. If it is, transfer the Output_section and return
2587// true. Otherwise return false.
2588
2589bool
2590Output_section_definition::alternate_constraint(
2591 Output_section_definition* posd,
2592 Section_constraint constraint)
2593{
2594 if (this->name_ != posd->name_)
2595 return false;
2596
2597 switch (constraint)
2598 {
2599 case CONSTRAINT_ONLY_IF_RO:
2600 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2601 return false;
2602 break;
2603
2604 case CONSTRAINT_ONLY_IF_RW:
2605 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2606 return false;
2607 break;
2608
2609 default:
2610 gold_unreachable();
2611 }
2612
2613 // We have found the alternate constraint. We just need to move
2614 // over the Output_section. When constraints are used properly,
2615 // THIS should not have an output_section pointer, as all the input
2616 // sections should have matched the other definition.
2617
2618 if (this->output_section_ != NULL)
2619 gold_error(_("mismatched definition for constrained sections"));
2620
2621 this->output_section_ = posd->output_section_;
2622 posd->output_section_ = NULL;
2623
2d924fd9
ILT
2624 if (this->is_relro_)
2625 this->output_section_->set_is_relro();
2626 else
2627 this->output_section_->clear_is_relro();
2628
3802b2dd
ILT
2629 return true;
2630}
2631
1c4f3631 2632// Get the list of segments to use for an allocated section when using
2cefc357 2633// a PHDRS clause.
1c4f3631
ILT
2634
2635Output_section*
2cefc357
ILT
2636Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2637 bool* orphan)
1c4f3631 2638{
d103a984
RÁE
2639 // Update phdrs_list even if we don't have an output section. It
2640 // might be used by the following sections.
2641 if (this->phdrs_ != NULL)
2642 *phdrs_list = this->phdrs_;
2643
1c4f3631
ILT
2644 if (this->output_section_ == NULL)
2645 return NULL;
2646 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2647 return NULL;
2cefc357 2648 *orphan = false;
1c4f3631
ILT
2649 return this->output_section_;
2650}
2651
8f2eb564
ILT
2652// Look for an output section by name and return the address, the load
2653// address, the alignment, and the size. This is used when an
2654// expression refers to an output section which was not actually
2655// created. This returns true if the section was found, false
2656// otherwise.
2657
2658bool
2659Output_section_definition::get_output_section_info(const char* name,
2660 uint64_t* address,
2661 uint64_t* load_address,
2ea97941 2662 uint64_t* addralign,
8f2eb564
ILT
2663 uint64_t* size) const
2664{
2665 if (this->name_ != name)
2666 return false;
2667
2668 if (this->output_section_ != NULL)
2669 {
2670 *address = this->output_section_->address();
2671 if (this->output_section_->has_load_address())
2672 *load_address = this->output_section_->load_address();
2673 else
2674 *load_address = *address;
2ea97941 2675 *addralign = this->output_section_->addralign();
8f2eb564
ILT
2676 *size = this->output_section_->current_data_size();
2677 }
2678 else
2679 {
2680 *address = this->evaluated_address_;
2681 *load_address = this->evaluated_load_address_;
2ea97941 2682 *addralign = this->evaluated_addralign_;
8f2eb564
ILT
2683 *size = 0;
2684 }
2685
2686 return true;
2687}
2688
494e05f4
ILT
2689// Print for debugging.
2690
2691void
2692Output_section_definition::print(FILE* f) const
2693{
2694 fprintf(f, " %s ", this->name_.c_str());
2695
2696 if (this->address_ != NULL)
2697 {
2698 this->address_->print(f);
2699 fprintf(f, " ");
2700 }
2701
1e5d2fb1
DK
2702 if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2703 fprintf(f, "(%s) ",
2704 this->script_section_type_name(this->script_section_type_));
2705
494e05f4
ILT
2706 fprintf(f, ": ");
2707
2708 if (this->load_address_ != NULL)
2709 {
2710 fprintf(f, "AT(");
2711 this->load_address_->print(f);
2712 fprintf(f, ") ");
2713 }
2714
2715 if (this->align_ != NULL)
2716 {
2717 fprintf(f, "ALIGN(");
2718 this->align_->print(f);
2719 fprintf(f, ") ");
2720 }
2721
2722 if (this->subalign_ != NULL)
2723 {
2724 fprintf(f, "SUBALIGN(");
2725 this->subalign_->print(f);
2726 fprintf(f, ") ");
2727 }
2728
2729 fprintf(f, "{\n");
2730
2731 for (Output_section_elements::const_iterator p = this->elements_.begin();
2732 p != this->elements_.end();
2733 ++p)
2734 (*p)->print(f);
2735
2736 fprintf(f, " }");
2737
2738 if (this->fill_ != NULL)
2739 {
2740 fprintf(f, " = ");
2741 this->fill_->print(f);
2742 }
2743
7d26c6cc
ILT
2744 if (this->phdrs_ != NULL)
2745 {
2746 for (String_list::const_iterator p = this->phdrs_->begin();
2747 p != this->phdrs_->end();
2748 ++p)
2749 fprintf(f, " :%s", p->c_str());
2750 }
2751
494e05f4
ILT
2752 fprintf(f, "\n");
2753}
2754
1e5d2fb1
DK
2755Script_sections::Section_type
2756Output_section_definition::section_type() const
2757{
2758 switch (this->script_section_type_)
2759 {
2760 case SCRIPT_SECTION_TYPE_NONE:
2761 return Script_sections::ST_NONE;
2762 case SCRIPT_SECTION_TYPE_NOLOAD:
2763 return Script_sections::ST_NOLOAD;
2764 case SCRIPT_SECTION_TYPE_COPY:
2765 case SCRIPT_SECTION_TYPE_DSECT:
2766 case SCRIPT_SECTION_TYPE_INFO:
2767 case SCRIPT_SECTION_TYPE_OVERLAY:
2768 // There are not really support so we treat them as ST_NONE. The
2769 // parse should have issued errors for them already.
2770 return Script_sections::ST_NONE;
2771 default:
2772 gold_unreachable();
2773 }
2774}
2775
2776// Return the name of a script section type.
2777
2778const char*
ca09d69a 2779Output_section_definition::script_section_type_name(
1e5d2fb1
DK
2780 Script_section_type script_section_type)
2781{
2782 switch (script_section_type)
2783 {
2784 case SCRIPT_SECTION_TYPE_NONE:
2785 return "NONE";
2786 case SCRIPT_SECTION_TYPE_NOLOAD:
2787 return "NOLOAD";
2788 case SCRIPT_SECTION_TYPE_DSECT:
2789 return "DSECT";
2790 case SCRIPT_SECTION_TYPE_COPY:
2791 return "COPY";
2792 case SCRIPT_SECTION_TYPE_INFO:
2793 return "INFO";
2794 case SCRIPT_SECTION_TYPE_OVERLAY:
2795 return "OVERLAY";
2796 default:
2797 gold_unreachable();
2798 }
2799}
2800
7f8cd844
NC
2801void
2802Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
2803{
2804 gold_assert(mr != NULL);
2805 // Add the current section to the specified region's list.
2806 mr->add_section(this, set_vma);
2807}
2808
a445fddf
ILT
2809// An output section created to hold orphaned input sections. These
2810// do not actually appear in linker scripts. However, for convenience
2811// when setting the output section addresses, we put a marker to these
2812// sections in the appropriate place in the list of SECTIONS elements.
2813
2814class Orphan_output_section : public Sections_element
2815{
2816 public:
2817 Orphan_output_section(Output_section* os)
2818 : os_(os)
2819 { }
2820
0d371ad3
ILT
2821 // Return whether the orphan output section is relro. We can just
2822 // check the output section because we always set the flag, if
2823 // needed, just after we create the Orphan_output_section.
a445fddf 2824 bool
0d371ad3
ILT
2825 is_relro() const
2826 { return this->os_->is_relro(); }
2827
2828 // Initialize OSP with an output section. This should have been
2829 // done already.
2830 void
2831 orphan_section_init(Orphan_section_placement*,
2832 Script_sections::Elements_iterator)
2833 { gold_unreachable(); }
a445fddf
ILT
2834
2835 // Set section addresses.
2836 void
f6973bdc
ILT
2837 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2838 uint64_t*);
a445fddf 2839
1c4f3631 2840 // Get the list of segments to use for an allocated section when
2cefc357 2841 // using a PHDRS clause.
1c4f3631 2842 Output_section*
2cefc357 2843 allocate_to_segment(String_list**, bool*);
1c4f3631 2844
2d924fd9
ILT
2845 // Return the associated Output_section.
2846 Output_section*
2847 get_output_section() const
2848 { return this->os_; }
2849
a445fddf
ILT
2850 // Print for debugging.
2851 void
2852 print(FILE* f) const
2853 {
2854 fprintf(f, " marker for orphaned output section %s\n",
2855 this->os_->name());
2856 }
2857
2858 private:
2859 Output_section* os_;
2860};
2861
a445fddf
ILT
2862// Set section addresses.
2863
2864void
2865Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
fd247bfe 2866 uint64_t* dot_value,
f6973bdc 2867 uint64_t*,
fd247bfe 2868 uint64_t* load_address)
a445fddf 2869{
6625d24e 2870 typedef std::list<Output_section::Input_section> Input_section_list;
a445fddf 2871
fd247bfe
ILT
2872 bool have_load_address = *load_address != *dot_value;
2873
a445fddf
ILT
2874 uint64_t address = *dot_value;
2875 address = align_address(address, this->os_->addralign());
2876
9e9143bc
ST
2877 // If input section sorting is requested via --section-ordering-file or
2878 // linker plugins, then do it here. This is important because we want
2879 // any sorting specified in the linker scripts, which will be done after
2880 // this, to take precedence. The final order of input sections is then
2881 // guaranteed to be according to the linker script specification.
2882 if (this->os_ != NULL
2883 && this->os_->input_section_order_specified())
2884 this->os_->sort_attached_input_sections();
2885
a94907d9
ILT
2886 // For a relocatable link, all orphan sections are put at
2887 // address 0. In general we expect all sections to be at
2888 // address 0 for a relocatable link, but we permit the linker
2889 // script to override that for specific output sections.
2890 if (parameters->options().relocatable())
2891 {
2892 address = 0;
2893 *load_address = 0;
2894 have_load_address = false;
2895 }
2896
a445fddf 2897 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
fd247bfe
ILT
2898 {
2899 this->os_->set_address(address);
2900 if (have_load_address)
2901 this->os_->set_load_address(align_address(*load_address,
2902 this->os_->addralign()));
2903 }
a445fddf
ILT
2904
2905 Input_section_list input_sections;
2906 address += this->os_->get_input_sections(address, "", &input_sections);
2907
2908 for (Input_section_list::iterator p = input_sections.begin();
2909 p != input_sections.end();
2910 ++p)
2911 {
6625d24e
DK
2912 uint64_t addralign = p->addralign();
2913 if (!p->is_input_section())
2914 p->output_section_data()->finalize_data_size();
2915 uint64_t size = p->data_size();
2ea97941 2916 address = align_address(address, addralign);
6625d24e 2917 this->os_->add_script_input_section(*p);
a445fddf
ILT
2918 address += size;
2919 }
2920
2199fbe7
CC
2921 if (parameters->options().relocatable())
2922 {
2923 // For a relocatable link, reset DOT_VALUE to 0.
2924 *dot_value = 0;
2925 *load_address = 0;
2926 }
2927 else if (this->os_ == NULL
2928 || (this->os_->flags() & elfcpp::SHF_TLS) == 0
2929 || this->os_->type() != elfcpp::SHT_NOBITS)
661be1e2 2930 {
2199fbe7 2931 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
661be1e2
ILT
2932 if (!have_load_address)
2933 *load_address = address;
2934 else
2935 *load_address += address - *dot_value;
fd247bfe 2936
661be1e2
ILT
2937 *dot_value = address;
2938 }
a445fddf
ILT
2939}
2940
1c4f3631
ILT
2941// Get the list of segments to use for an allocated section when using
2942// a PHDRS clause. If this is an allocated section, return the
2943// Output_section. We don't change the list of segments.
2944
2945Output_section*
2cefc357 2946Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
1c4f3631
ILT
2947{
2948 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2949 return NULL;
2cefc357 2950 *orphan = true;
1c4f3631
ILT
2951 return this->os_;
2952}
2953
2954// Class Phdrs_element. A program header from a PHDRS clause.
2955
2956class Phdrs_element
2957{
2958 public:
2ea97941
ILT
2959 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2960 bool includes_filehdr, bool includes_phdrs,
1c4f3631 2961 bool is_flags_valid, unsigned int flags,
2ea97941
ILT
2962 Expression* load_address)
2963 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2964 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2965 flags_(flags), load_address_(load_address), load_address_value_(0),
1c4f3631
ILT
2966 segment_(NULL)
2967 { }
2968
2969 // Return the name of this segment.
2970 const std::string&
2971 name() const
2972 { return this->name_; }
2973
2974 // Return the type of the segment.
2975 unsigned int
2976 type() const
2977 { return this->type_; }
2978
2979 // Whether to include the file header.
2980 bool
2981 includes_filehdr() const
2982 { return this->includes_filehdr_; }
2983
2984 // Whether to include the program headers.
2985 bool
2986 includes_phdrs() const
2987 { return this->includes_phdrs_; }
2988
2989 // Return whether there is a load address.
2990 bool
2991 has_load_address() const
2992 { return this->load_address_ != NULL; }
2993
2994 // Evaluate the load address expression if there is one.
2995 void
2ea97941 2996 eval_load_address(Symbol_table* symtab, Layout* layout)
1c4f3631
ILT
2997 {
2998 if (this->load_address_ != NULL)
2ea97941 2999 this->load_address_value_ = this->load_address_->eval(symtab, layout,
919ed24c 3000 true);
1c4f3631
ILT
3001 }
3002
3003 // Return the load address.
3004 uint64_t
3005 load_address() const
3006 {
3007 gold_assert(this->load_address_ != NULL);
3008 return this->load_address_value_;
3009 }
3010
3011 // Create the segment.
3012 Output_segment*
3013 create_segment(Layout* layout)
3014 {
3015 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
3016 return this->segment_;
3017 }
3018
3019 // Return the segment.
3020 Output_segment*
3021 segment()
3022 { return this->segment_; }
3023
20e6d0d6
DK
3024 // Release the segment.
3025 void
3026 release_segment()
3027 { this->segment_ = NULL; }
3028
1c4f3631
ILT
3029 // Set the segment flags if appropriate.
3030 void
3031 set_flags_if_valid()
3032 {
3033 if (this->is_flags_valid_)
3034 this->segment_->set_flags(this->flags_);
3035 }
3036
7d26c6cc
ILT
3037 // Print for debugging.
3038 void
3039 print(FILE*) const;
3040
1c4f3631
ILT
3041 private:
3042 // The name used in the script.
3043 std::string name_;
3044 // The type of the segment (PT_LOAD, etc.).
3045 unsigned int type_;
3046 // Whether this segment includes the file header.
3047 bool includes_filehdr_;
3048 // Whether this segment includes the section headers.
3049 bool includes_phdrs_;
3050 // Whether the flags were explicitly specified.
3051 bool is_flags_valid_;
3052 // The flags for this segment (PF_R, etc.) if specified.
3053 unsigned int flags_;
3054 // The expression for the load address for this segment. This may
3055 // be NULL.
3056 Expression* load_address_;
3057 // The actual load address from evaluating the expression.
3058 uint64_t load_address_value_;
3059 // The segment itself.
3060 Output_segment* segment_;
3061};
3062
7d26c6cc
ILT
3063// Print for debugging.
3064
3065void
3066Phdrs_element::print(FILE* f) const
3067{
3068 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
3069 if (this->includes_filehdr_)
3070 fprintf(f, " FILEHDR");
3071 if (this->includes_phdrs_)
3072 fprintf(f, " PHDRS");
3073 if (this->is_flags_valid_)
3074 fprintf(f, " FLAGS(%u)", this->flags_);
3075 if (this->load_address_ != NULL)
3076 {
3077 fprintf(f, " AT(");
3078 this->load_address_->print(f);
3079 fprintf(f, ")");
3080 }
3081 fprintf(f, ";\n");
3082}
3083
7f8cd844
NC
3084// Add a memory region.
3085
3086void
3087Script_sections::add_memory_region(const char* name, size_t namelen,
3088 unsigned int attributes,
3089 Expression* start, Expression* length)
3090{
3091 if (this->memory_regions_ == NULL)
3092 this->memory_regions_ = new Memory_regions();
3093 else if (this->find_memory_region(name, namelen))
3094 {
ea5cae92 3095 gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
33dbc701 3096 name);
7f8cd844
NC
3097 // FIXME: Add a GOLD extension to allow multiple regions with the same
3098 // name. This would amount to a single region covering disjoint blocks
3099 // of memory, which is useful for embedded devices.
3100 }
3101
3102 // FIXME: Check the length and start values. Currently we allow
3103 // non-constant expressions for these values, whereas LD does not.
3104
3105 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
3106 // describe a region that packs from the end address going down, rather
3107 // than the start address going up. This would be useful for embedded
3108 // devices.
3109
3110 this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
3111 start, length));
3112}
3113
3114// Find a memory region.
3115
3116Memory_region*
3117Script_sections::find_memory_region(const char* name, size_t namelen)
3118{
3119 if (this->memory_regions_ == NULL)
3120 return NULL;
3121
3122 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3123 m != this->memory_regions_->end();
3124 ++m)
3125 if ((*m)->name_match(name, namelen))
3126 return *m;
3127
3128 return NULL;
3129}
3130
3131// Find a memory region's origin.
3132
3133Expression*
3134Script_sections::find_memory_region_origin(const char* name, size_t namelen)
3135{
3136 Memory_region* mr = find_memory_region(name, namelen);
3137 if (mr == NULL)
3138 return NULL;
3139
3140 return mr->start_address();
3141}
3142
3143// Find a memory region's length.
3144
3145Expression*
3146Script_sections::find_memory_region_length(const char* name, size_t namelen)
3147{
3148 Memory_region* mr = find_memory_region(name, namelen);
3149 if (mr == NULL)
3150 return NULL;
3151
3152 return mr->length();
3153}
3154
3155// Set the memory region to use for the current section.
3156
3157void
3158Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
3159{
3160 gold_assert(!this->sections_elements_->empty());
3161 this->sections_elements_->back()->set_memory_region(mr, set_vma);
3162}
3163
494e05f4
ILT
3164// Class Script_sections.
3165
3166Script_sections::Script_sections()
3167 : saw_sections_clause_(false),
3168 in_sections_clause_(false),
3169 sections_elements_(NULL),
1c4f3631 3170 output_section_(NULL),
7f8cd844 3171 memory_regions_(NULL),
2d924fd9 3172 phdrs_elements_(NULL),
0d371ad3
ILT
3173 orphan_section_placement_(NULL),
3174 data_segment_align_start_(),
3175 saw_data_segment_align_(false),
3c12dcdb
DK
3176 saw_relro_end_(false),
3177 saw_segment_start_expression_(false)
494e05f4
ILT
3178{
3179}
3180
3181// Start a SECTIONS clause.
3182
3183void
3184Script_sections::start_sections()
3185{
3186 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
3187 this->saw_sections_clause_ = true;
3188 this->in_sections_clause_ = true;
3189 if (this->sections_elements_ == NULL)
3190 this->sections_elements_ = new Sections_elements;
3191}
3192
3193// Finish a SECTIONS clause.
3194
3195void
3196Script_sections::finish_sections()
3197{
3198 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
3199 this->in_sections_clause_ = false;
3200}
3201
3202// Add a symbol to be defined.
3203
3204void
3205Script_sections::add_symbol_assignment(const char* name, size_t length,
3206 Expression* val, bool provide,
3207 bool hidden)
3208{
3209 if (this->output_section_ != NULL)
3210 this->output_section_->add_symbol_assignment(name, length, val,
3211 provide, hidden);
3212 else
3213 {
3214 Sections_element* p = new Sections_element_assignment(name, length,
3215 val, provide,
3216 hidden);
3217 this->sections_elements_->push_back(p);
3218 }
3219}
3220
a445fddf
ILT
3221// Add an assignment to the special dot symbol.
3222
3223void
3224Script_sections::add_dot_assignment(Expression* val)
3225{
3226 if (this->output_section_ != NULL)
3227 this->output_section_->add_dot_assignment(val);
3228 else
3229 {
12edd763
ILT
3230 // The GNU linker permits assignments to . to appears outside of
3231 // a SECTIONS clause, and treats it as appearing inside, so
3232 // sections_elements_ may be NULL here.
3233 if (this->sections_elements_ == NULL)
3234 {
3235 this->sections_elements_ = new Sections_elements;
3236 this->saw_sections_clause_ = true;
3237 }
3238
a445fddf
ILT
3239 Sections_element* p = new Sections_element_dot_assignment(val);
3240 this->sections_elements_->push_back(p);
3241 }
3242}
3243
494e05f4
ILT
3244// Add an assertion.
3245
3246void
3247Script_sections::add_assertion(Expression* check, const char* message,
3248 size_t messagelen)
3249{
3250 if (this->output_section_ != NULL)
3251 this->output_section_->add_assertion(check, message, messagelen);
3252 else
3253 {
3254 Sections_element* p = new Sections_element_assertion(check, message,
3255 messagelen);
3256 this->sections_elements_->push_back(p);
3257 }
3258}
3259
3260// Start processing entries for an output section.
3261
3262void
3263Script_sections::start_output_section(
3264 const char* name,
3265 size_t namelen,
ca09d69a 3266 const Parser_output_section_header* header)
494e05f4
ILT
3267{
3268 Output_section_definition* posd = new Output_section_definition(name,
3269 namelen,
3270 header);
3271 this->sections_elements_->push_back(posd);
3272 gold_assert(this->output_section_ == NULL);
3273 this->output_section_ = posd;
3274}
3275
3276// Stop processing entries for an output section.
3277
3278void
3279Script_sections::finish_output_section(
3280 const Parser_output_section_trailer* trailer)
3281{
3282 gold_assert(this->output_section_ != NULL);
3283 this->output_section_->finish(trailer);
3284 this->output_section_ = NULL;
3285}
3286
3287// Add a data item to the current output section.
3288
3289void
3290Script_sections::add_data(int size, bool is_signed, Expression* val)
3291{
3292 gold_assert(this->output_section_ != NULL);
3293 this->output_section_->add_data(size, is_signed, val);
3294}
3295
3296// Add a fill value setting to the current output section.
3297
3298void
3299Script_sections::add_fill(Expression* val)
3300{
3301 gold_assert(this->output_section_ != NULL);
3302 this->output_section_->add_fill(val);
3303}
3304
3305// Add an input section specification to the current output section.
3306
3307void
3308Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3309{
3310 gold_assert(this->output_section_ != NULL);
3311 this->output_section_->add_input_section(spec, keep);
3312}
3313
2d924fd9
ILT
3314// This is called when we see DATA_SEGMENT_ALIGN. It means that any
3315// subsequent output sections may be relro.
3316
3317void
3318Script_sections::data_segment_align()
3319{
0d371ad3 3320 if (this->saw_data_segment_align_)
2d924fd9 3321 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
0d371ad3
ILT
3322 gold_assert(!this->sections_elements_->empty());
3323 Sections_elements::iterator p = this->sections_elements_->end();
3324 --p;
3325 this->data_segment_align_start_ = p;
3326 this->saw_data_segment_align_ = true;
2d924fd9
ILT
3327}
3328
3329// This is called when we see DATA_SEGMENT_RELRO_END. It means that
3330// any output sections seen since DATA_SEGMENT_ALIGN are relro.
3331
3332void
3333Script_sections::data_segment_relro_end()
3334{
3335 if (this->saw_relro_end_)
3336 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3337 "in a linker script"));
3338 this->saw_relro_end_ = true;
3339
0d371ad3 3340 if (!this->saw_data_segment_align_)
2d924fd9
ILT
3341 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3342 else
3343 {
0d371ad3
ILT
3344 Sections_elements::iterator p = this->data_segment_align_start_;
3345 for (++p; p != this->sections_elements_->end(); ++p)
3346 (*p)->set_is_relro();
2d924fd9
ILT
3347 }
3348}
3349
919ed24c
ILT
3350// Create any required sections.
3351
3352void
3353Script_sections::create_sections(Layout* layout)
3354{
3355 if (!this->saw_sections_clause_)
3356 return;
3357 for (Sections_elements::iterator p = this->sections_elements_->begin();
3358 p != this->sections_elements_->end();
3359 ++p)
3360 (*p)->create_sections(layout);
3361}
3362
a445fddf
ILT
3363// Add any symbols we are defining to the symbol table.
3364
3365void
3366Script_sections::add_symbols_to_table(Symbol_table* symtab)
3367{
3368 if (!this->saw_sections_clause_)
3369 return;
3370 for (Sections_elements::iterator p = this->sections_elements_->begin();
3371 p != this->sections_elements_->end();
3372 ++p)
3373 (*p)->add_symbols_to_table(symtab);
3374}
3375
3376// Finalize symbols and check assertions.
3377
3378void
3379Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3380{
3381 if (!this->saw_sections_clause_)
3382 return;
a445fddf
ILT
3383 uint64_t dot_value = 0;
3384 for (Sections_elements::iterator p = this->sections_elements_->begin();
3385 p != this->sections_elements_->end();
3386 ++p)
77e65537 3387 (*p)->finalize_symbols(symtab, layout, &dot_value);
a445fddf
ILT
3388}
3389
3390// Return the name of the output section to use for an input file name
3391// and section name.
3392
3393const char*
1e5d2fb1
DK
3394Script_sections::output_section_name(
3395 const char* file_name,
3396 const char* section_name,
3397 Output_section*** output_section_slot,
b9b2ae8b
NC
3398 Script_sections::Section_type* psection_type,
3399 bool* keep)
a445fddf
ILT
3400{
3401 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3402 p != this->sections_elements_->end();
3403 ++p)
3404 {
3405 const char* ret = (*p)->output_section_name(file_name, section_name,
1e5d2fb1 3406 output_section_slot,
b9b2ae8b 3407 psection_type, keep);
a445fddf
ILT
3408
3409 if (ret != NULL)
3410 {
3411 // The special name /DISCARD/ means that the input section
3412 // should be discarded.
3413 if (strcmp(ret, "/DISCARD/") == 0)
3414 {
3415 *output_section_slot = NULL;
1e5d2fb1 3416 *psection_type = Script_sections::ST_NONE;
a445fddf
ILT
3417 return NULL;
3418 }
3419 return ret;
3420 }
3421 }
3422
3423 // If we couldn't find a mapping for the name, the output section
3424 // gets the name of the input section.
3425
3426 *output_section_slot = NULL;
1e5d2fb1 3427 *psection_type = Script_sections::ST_NONE;
a445fddf
ILT
3428
3429 return section_name;
3430}
3431
3432// Place a marker for an orphan output section into the SECTIONS
3433// clause.
3434
3435void
3436Script_sections::place_orphan(Output_section* os)
3437{
0d371ad3
ILT
3438 Orphan_section_placement* osp = this->orphan_section_placement_;
3439 if (osp == NULL)
a445fddf 3440 {
0d371ad3
ILT
3441 // Initialize the Orphan_section_placement structure.
3442 osp = new Orphan_section_placement();
3443 for (Sections_elements::iterator p = this->sections_elements_->begin();
3444 p != this->sections_elements_->end();
3445 ++p)
3446 (*p)->orphan_section_init(osp, p);
3447 gold_assert(!this->sections_elements_->empty());
3448 Sections_elements::iterator last = this->sections_elements_->end();
3449 --last;
3450 osp->last_init(last);
3451 this->orphan_section_placement_ = osp;
a445fddf
ILT
3452 }
3453
0d371ad3 3454 Orphan_output_section* orphan = new Orphan_output_section(os);
2d924fd9 3455
0d371ad3
ILT
3456 // Look for where to put ORPHAN.
3457 Sections_elements::iterator* where;
3458 if (osp->find_place(os, &where))
3459 {
3460 if ((**where)->is_relro())
3461 os->set_is_relro();
3462 else
3463 os->clear_is_relro();
3464
3465 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3466 // so that the next one goes after this one.
3467 Sections_elements::iterator p = *where;
3468 gold_assert(p != this->sections_elements_->end());
3469 ++p;
3470 *where = this->sections_elements_->insert(p, orphan);
3471 }
2d924fd9 3472 else
0d371ad3
ILT
3473 {
3474 os->clear_is_relro();
3475 // We don't have a place to put this orphan section. Put it,
3476 // and all other sections like it, at the end, but before the
3477 // sections which always come at the end.
3478 Sections_elements::iterator last = osp->last_place();
3479 *where = this->sections_elements_->insert(last, orphan);
3480 }
a445fddf
ILT
3481}
3482
3483// Set the addresses of all the output sections. Walk through all the
3484// elements, tracking the dot symbol. Apply assignments which set
3485// absolute symbol values, in case they are used when setting dot.
3486// Fill in data statement values. As we find output sections, set the
3487// address, set the address of all associated input sections, and
3488// update dot. Return the segment which should hold the file header
3489// and segment headers, if any.
3490
3491Output_segment*
3492Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3493{
3494 gold_assert(this->saw_sections_clause_);
7f8cd844 3495
3802b2dd
ILT
3496 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3497 // for our representation.
3498 for (Sections_elements::iterator p = this->sections_elements_->begin();
3499 p != this->sections_elements_->end();
3500 ++p)
3501 {
3502 Output_section_definition* posd;
3503 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3504 if (failed_constraint != CONSTRAINT_NONE)
3505 {
3506 Sections_elements::iterator q;
3507 for (q = this->sections_elements_->begin();
3508 q != this->sections_elements_->end();
3509 ++q)
3510 {
3511 if (q != p)
3512 {
3513 if ((*q)->alternate_constraint(posd, failed_constraint))
3514 break;
3515 }
3516 }
3517
3518 if (q == this->sections_elements_->end())
3519 gold_error(_("no matching section constraint"));
3520 }
3521 }
3522
2d924fd9
ILT
3523 // Force the alignment of the first TLS section to be the maximum
3524 // alignment of all TLS sections.
3525 Output_section* first_tls = NULL;
3526 uint64_t tls_align = 0;
3527 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3528 p != this->sections_elements_->end();
3529 ++p)
3530 {
ca09d69a 3531 Output_section* os = (*p)->get_output_section();
2d924fd9
ILT
3532 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3533 {
3534 if (first_tls == NULL)
3535 first_tls = os;
3536 if (os->addralign() > tls_align)
3537 tls_align = os->addralign();
3538 }
3539 }
3540 if (first_tls != NULL)
3541 first_tls->set_addralign(tls_align);
3542
77e65537 3543 // For a relocatable link, we implicitly set dot to zero.
a445fddf 3544 uint64_t dot_value = 0;
f6973bdc 3545 uint64_t dot_alignment = 0;
fd247bfe 3546 uint64_t load_address = 0;
3c12dcdb
DK
3547
3548 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3549 // to set section addresses. If the script has any SEGMENT_START
3550 // expression, we do not set the section addresses.
3551 bool use_tsection_options =
3552 (!this->saw_segment_start_expression_
3553 && (parameters->options().user_set_Ttext()
3554 || parameters->options().user_set_Tdata()
3555 || parameters->options().user_set_Tbss()));
3556
a445fddf
ILT
3557 for (Sections_elements::iterator p = this->sections_elements_->begin();
3558 p != this->sections_elements_->end();
3559 ++p)
3c12dcdb
DK
3560 {
3561 Output_section* os = (*p)->get_output_section();
3562
3563 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3564 // the special sections by names and doing dot assignments.
3565 if (use_tsection_options
3566 && os != NULL
3567 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3568 {
3569 uint64_t new_dot_value = dot_value;
3570
3571 if (parameters->options().user_set_Ttext()
3572 && strcmp(os->name(), ".text") == 0)
3573 new_dot_value = parameters->options().Ttext();
3574 else if (parameters->options().user_set_Tdata()
3575 && strcmp(os->name(), ".data") == 0)
3576 new_dot_value = parameters->options().Tdata();
3577 else if (parameters->options().user_set_Tbss()
3578 && strcmp(os->name(), ".bss") == 0)
3579 new_dot_value = parameters->options().Tbss();
3580
3581 // Update dot and load address if necessary.
3582 if (new_dot_value < dot_value)
3583 gold_error(_("dot may not move backward"));
3584 else if (new_dot_value != dot_value)
3585 {
3586 dot_value = new_dot_value;
3587 load_address = new_dot_value;
3588 }
3589 }
3590
f6973bdc
ILT
3591 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3592 &load_address);
3c12dcdb 3593 }
a445fddf 3594
1c4f3631
ILT
3595 if (this->phdrs_elements_ != NULL)
3596 {
3597 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3598 p != this->phdrs_elements_->end();
3599 ++p)
3600 (*p)->eval_load_address(symtab, layout);
3601 }
3602
f6973bdc 3603 return this->create_segments(layout, dot_alignment);
a445fddf
ILT
3604}
3605
3606// Sort the sections in order to put them into segments.
3607
3608class Sort_output_sections
3609{
3610 public:
eb373049
ILT
3611 Sort_output_sections(const Script_sections::Sections_elements* elements)
3612 : elements_(elements)
3613 { }
3614
a445fddf
ILT
3615 bool
3616 operator()(const Output_section* os1, const Output_section* os2) const;
eb373049
ILT
3617
3618 private:
fd7a005d
ILT
3619 int
3620 script_compare(const Output_section* os1, const Output_section* os2) const;
eb373049
ILT
3621
3622 private:
3623 const Script_sections::Sections_elements* elements_;
a445fddf
ILT
3624};
3625
3626bool
3627Sort_output_sections::operator()(const Output_section* os1,
3628 const Output_section* os2) const
3629{
3630 // Sort first by the load address.
3631 uint64_t lma1 = (os1->has_load_address()
3632 ? os1->load_address()
3633 : os1->address());
3634 uint64_t lma2 = (os2->has_load_address()
3635 ? os2->load_address()
3636 : os2->address());
3637 if (lma1 != lma2)
3638 return lma1 < lma2;
3639
3640 // Then sort by the virtual address.
3641 if (os1->address() != os2->address())
3642 return os1->address() < os2->address();
3643
fd7a005d
ILT
3644 // If the linker script says which of these sections is first, go
3645 // with what it says.
3646 int i = this->script_compare(os1, os2);
3647 if (i != 0)
3648 return i < 0;
3649
0aa45fac
CC
3650 // Sort PROGBITS before NOBITS.
3651 bool nobits1 = os1->type() == elfcpp::SHT_NOBITS;
3652 bool nobits2 = os2->type() == elfcpp::SHT_NOBITS;
3653 if (nobits1 != nobits2)
3654 return nobits2;
3655
3656 // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
3657 // beginning.
a445fddf
ILT
3658 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3659 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3660 if (tls1 != tls2)
0aa45fac 3661 return nobits1 ? tls1 : tls2;
a445fddf 3662
1e5d2fb1
DK
3663 // Sort non-NOLOAD before NOLOAD.
3664 if (os1->is_noload() && !os2->is_noload())
3665 return true;
3666 if (!os1->is_noload() && os2->is_noload())
3667 return true;
fd7a005d
ILT
3668
3669 // The sections seem practically identical. Sort by name to get a
3670 // stable sort.
3671 return os1->name() < os2->name();
eb373049
ILT
3672}
3673
fd7a005d
ILT
3674// Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
3675// if either OS1 or OS2 is not mentioned. This ensures that we keep
3676// empty sections in the order in which they appear in a linker
3677// script.
eb373049 3678
fd7a005d
ILT
3679int
3680Sort_output_sections::script_compare(const Output_section* os1,
3681 const Output_section* os2) const
eb373049
ILT
3682{
3683 if (this->elements_ == NULL)
fd7a005d 3684 return 0;
eb373049 3685
fd7a005d
ILT
3686 bool found_os1 = false;
3687 bool found_os2 = false;
eb373049
ILT
3688 for (Script_sections::Sections_elements::const_iterator
3689 p = this->elements_->begin();
3690 p != this->elements_->end();
3691 ++p)
3692 {
fd7a005d 3693 if (os2 == (*p)->get_output_section())
eb373049 3694 {
fd7a005d
ILT
3695 if (found_os1)
3696 return -1;
3697 found_os2 = true;
3698 }
3699 else if (os1 == (*p)->get_output_section())
3700 {
3701 if (found_os2)
3702 return 1;
3703 found_os1 = true;
eb373049
ILT
3704 }
3705 }
3706
fd7a005d 3707 return 0;
a445fddf
ILT
3708}
3709
3710// Return whether OS is a BSS section. This is a SHT_NOBITS section.
3711// We treat a section with the SHF_TLS flag set as taking up space
3712// even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3713// space for them in the file.
3714
3715bool
3716Script_sections::is_bss_section(const Output_section* os)
3717{
3718 return (os->type() == elfcpp::SHT_NOBITS
3719 && (os->flags() & elfcpp::SHF_TLS) == 0);
3720}
3721
1c4f3631
ILT
3722// Return the size taken by the file header and the program headers.
3723
3724size_t
3725Script_sections::total_header_size(Layout* layout) const
3726{
3727 size_t segment_count = layout->segment_count();
3728 size_t file_header_size;
3729 size_t segment_headers_size;
8851ecca 3730 if (parameters->target().get_size() == 32)
1c4f3631
ILT
3731 {
3732 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3733 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3734 }
8851ecca 3735 else if (parameters->target().get_size() == 64)
1c4f3631
ILT
3736 {
3737 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3738 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3739 }
3740 else
3741 gold_unreachable();
3742
3743 return file_header_size + segment_headers_size;
3744}
3745
9b547ce6 3746// Return the amount we have to subtract from the LMA to accommodate
1c4f3631
ILT
3747// headers of the given size. The complication is that the file
3748// header have to be at the start of a page, as otherwise it will not
3749// be at the start of the file.
3750
3751uint64_t
3752Script_sections::header_size_adjustment(uint64_t lma,
3753 size_t sizeof_headers) const
3754{
8851ecca 3755 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
1c4f3631
ILT
3756 uint64_t hdr_lma = lma - sizeof_headers;
3757 hdr_lma &= ~(abi_pagesize - 1);
3758 return lma - hdr_lma;
3759}
3760
a445fddf
ILT
3761// Create the PT_LOAD segments when using a SECTIONS clause. Returns
3762// the segment which should hold the file header and segment headers,
3763// if any.
3764
3765Output_segment*
f6973bdc 3766Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
a445fddf
ILT
3767{
3768 gold_assert(this->saw_sections_clause_);
3769
8851ecca 3770 if (parameters->options().relocatable())
a445fddf
ILT
3771 return NULL;
3772
1c4f3631 3773 if (this->saw_phdrs_clause())
f6973bdc 3774 return create_segments_from_phdrs_clause(layout, dot_alignment);
1c4f3631 3775
a445fddf
ILT
3776 Layout::Section_list sections;
3777 layout->get_allocated_sections(&sections);
3778
3779 // Sort the sections by address.
eb373049
ILT
3780 std::stable_sort(sections.begin(), sections.end(),
3781 Sort_output_sections(this->sections_elements_));
a445fddf
ILT
3782
3783 this->create_note_and_tls_segments(layout, &sections);
3784
3785 // Walk through the sections adding them to PT_LOAD segments.
8851ecca 3786 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
a445fddf
ILT
3787 Output_segment* first_seg = NULL;
3788 Output_segment* current_seg = NULL;
3789 bool is_current_seg_readonly = true;
3790 Layout::Section_list::iterator plast = sections.end();
3791 uint64_t last_vma = 0;
3792 uint64_t last_lma = 0;
3793 uint64_t last_size = 0;
3794 for (Layout::Section_list::iterator p = sections.begin();
3795 p != sections.end();
3796 ++p)
3797 {
3798 const uint64_t vma = (*p)->address();
3799 const uint64_t lma = ((*p)->has_load_address()
3800 ? (*p)->load_address()
3801 : vma);
3802 const uint64_t size = (*p)->current_data_size();
3803
3804 bool need_new_segment;
3805 if (current_seg == NULL)
3806 need_new_segment = true;
3807 else if (lma - vma != last_lma - last_vma)
3808 {
3809 // This section has a different LMA relationship than the
3810 // last one; we need a new segment.
3811 need_new_segment = true;
3812 }
3813 else if (align_address(last_lma + last_size, abi_pagesize)
3814 < align_address(lma, abi_pagesize))
3815 {
3816 // Putting this section in the segment would require
3817 // skipping a page.
3818 need_new_segment = true;
3819 }
3820 else if (is_bss_section(*plast) && !is_bss_section(*p))
3821 {
3822 // A non-BSS section can not follow a BSS section in the
3823 // same segment.
3824 need_new_segment = true;
3825 }
3826 else if (is_current_seg_readonly
af6156ef
ILT
3827 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3828 && !parameters->options().omagic())
a445fddf
ILT
3829 {
3830 // Don't put a writable section in the same segment as a
3831 // non-writable section.
3832 need_new_segment = true;
3833 }
3834 else
3835 {
3836 // Otherwise, reuse the existing segment.
3837 need_new_segment = false;
3838 }
3839
3840 elfcpp::Elf_Word seg_flags =
3841 Layout::section_flags_to_segment((*p)->flags());
3842
3843 if (need_new_segment)
3844 {
3845 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3846 seg_flags);
3847 current_seg->set_addresses(vma, lma);
f6973bdc 3848 current_seg->set_minimum_p_align(dot_alignment);
a445fddf
ILT
3849 if (first_seg == NULL)
3850 first_seg = current_seg;
3851 is_current_seg_readonly = true;
3852 }
3853
22f0da72 3854 current_seg->add_output_section_to_load(layout, *p, seg_flags);
a445fddf
ILT
3855
3856 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3857 is_current_seg_readonly = false;
3858
3859 plast = p;
3860 last_vma = vma;
3861 last_lma = lma;
3862 last_size = size;
3863 }
3864
3865 // An ELF program should work even if the program headers are not in
3866 // a PT_LOAD segment. However, it appears that the Linux kernel
3867 // does not set the AT_PHDR auxiliary entry in that case. It sets
3868 // the load address to p_vaddr - p_offset of the first PT_LOAD
3869 // segment. It then sets AT_PHDR to the load address plus the
3870 // offset to the program headers, e_phoff in the file header. This
3871 // fails when the program headers appear in the file before the
3872 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3873 // segment to hold the file header and the program headers. This is
3874 // effectively what the GNU linker does, and it is slightly more
3875 // efficient in any case. We try to use the first PT_LOAD segment
3876 // if we can, otherwise we make a new one.
3877
919ed24c
ILT
3878 if (first_seg == NULL)
3879 return NULL;
3880
3ee173de
ILT
3881 // -n or -N mean that the program is not demand paged and there is
3882 // no need to put the program headers in a PT_LOAD segment.
3883 if (parameters->options().nmagic() || parameters->options().omagic())
3884 return NULL;
3885
1c4f3631 3886 size_t sizeof_headers = this->total_header_size(layout);
3802b2dd 3887
919ed24c
ILT
3888 uint64_t vma = first_seg->vaddr();
3889 uint64_t lma = first_seg->paddr();
3890
3891 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3892
e6188289
ILT
3893 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3894 {
3895 first_seg->set_addresses(vma - subtract, lma - subtract);
3896 return first_seg;
3897 }
3898
919ed24c
ILT
3899 // If there is no room to squeeze in the headers, then punt. The
3900 // resulting executable probably won't run on GNU/Linux, but we
3901 // trust that the user knows what they are doing.
3902 if (lma < subtract || vma < subtract)
3903 return NULL;
3904
ea5cae92
NC
3905 // If memory regions have been specified and the address range
3906 // we are about to use is not contained within any region then
3907 // issue a warning message about the segment we are going to
3908 // create. It will be outside of any region and so possibly
3909 // using non-existent or protected memory. We test LMA rather
3910 // than VMA since we assume that the headers will never be
3911 // relocated.
3912 if (this->memory_regions_ != NULL
3913 && !this->block_in_region (NULL, layout, lma - subtract, subtract))
3914 gold_warning(_("creating a segment to contain the file and program"
3915 " headers outside of any MEMORY region"));
3916
a445fddf
ILT
3917 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3918 elfcpp::PF_R);
919ed24c 3919 load_seg->set_addresses(vma - subtract, lma - subtract);
a445fddf
ILT
3920
3921 return load_seg;
3922}
3923
3924// Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3925// segment if there are any SHT_TLS sections.
3926
3927void
3928Script_sections::create_note_and_tls_segments(
3929 Layout* layout,
3930 const Layout::Section_list* sections)
3931{
1c4f3631
ILT
3932 gold_assert(!this->saw_phdrs_clause());
3933
a445fddf
ILT
3934 bool saw_tls = false;
3935 for (Layout::Section_list::const_iterator p = sections->begin();
3936 p != sections->end();
3937 ++p)
3938 {
3939 if ((*p)->type() == elfcpp::SHT_NOTE)
3940 {
3941 elfcpp::Elf_Word seg_flags =
3942 Layout::section_flags_to_segment((*p)->flags());
3943 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3944 seg_flags);
22f0da72 3945 oseg->add_output_section_to_nonload(*p, seg_flags);
a445fddf
ILT
3946
3947 // Incorporate any subsequent SHT_NOTE sections, in the
3948 // hopes that the script is sensible.
3949 Layout::Section_list::const_iterator pnext = p + 1;
3950 while (pnext != sections->end()
3951 && (*pnext)->type() == elfcpp::SHT_NOTE)
3952 {
3953 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
22f0da72 3954 oseg->add_output_section_to_nonload(*pnext, seg_flags);
a445fddf
ILT
3955 p = pnext;
3956 ++pnext;
3957 }
3958 }
3959
3960 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3961 {
3962 if (saw_tls)
3963 gold_error(_("TLS sections are not adjacent"));
3964
3965 elfcpp::Elf_Word seg_flags =
3966 Layout::section_flags_to_segment((*p)->flags());
3967 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3968 seg_flags);
22f0da72 3969 oseg->add_output_section_to_nonload(*p, seg_flags);
a445fddf
ILT
3970
3971 Layout::Section_list::const_iterator pnext = p + 1;
3972 while (pnext != sections->end()
3973 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3974 {
3975 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
22f0da72 3976 oseg->add_output_section_to_nonload(*pnext, seg_flags);
a445fddf
ILT
3977 p = pnext;
3978 ++pnext;
3979 }
3980
3981 saw_tls = true;
3982 }
10b4f102
ILT
3983
3984 // If we are making a shared library, and we see a section named
e1f74f98
ILT
3985 // .interp then put the .interp section in a PT_INTERP segment.
3986 // This is for GNU ld compatibility.
3987 if (strcmp((*p)->name(), ".interp") == 0)
10b4f102
ILT
3988 {
3989 elfcpp::Elf_Word seg_flags =
3990 Layout::section_flags_to_segment((*p)->flags());
3991 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_INTERP,
3992 seg_flags);
3993 oseg->add_output_section_to_nonload(*p, seg_flags);
3994 }
a445fddf
ILT
3995 }
3996}
3997
1c4f3631
ILT
3998// Add a program header. The PHDRS clause is syntactically distinct
3999// from the SECTIONS clause, but we implement it with the SECTIONS
55458500 4000// support because PHDRS is useless if there is no SECTIONS clause.
1c4f3631
ILT
4001
4002void
4003Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
4004 bool includes_filehdr, bool includes_phdrs,
4005 bool is_flags_valid, unsigned int flags,
4006 Expression* load_address)
4007{
4008 if (this->phdrs_elements_ == NULL)
4009 this->phdrs_elements_ = new Phdrs_elements();
4010 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
4011 includes_filehdr,
4012 includes_phdrs,
4013 is_flags_valid, flags,
4014 load_address));
4015}
4016
3802b2dd
ILT
4017// Return the number of segments we expect to create based on the
4018// SECTIONS clause. This is used to implement SIZEOF_HEADERS.
4019
4020size_t
4021Script_sections::expected_segment_count(const Layout* layout) const
4022{
1c4f3631
ILT
4023 if (this->saw_phdrs_clause())
4024 return this->phdrs_elements_->size();
4025
3802b2dd
ILT
4026 Layout::Section_list sections;
4027 layout->get_allocated_sections(&sections);
4028
4029 // We assume that we will need two PT_LOAD segments.
4030 size_t ret = 2;
4031
4032 bool saw_note = false;
4033 bool saw_tls = false;
4034 for (Layout::Section_list::const_iterator p = sections.begin();
4035 p != sections.end();
4036 ++p)
4037 {
4038 if ((*p)->type() == elfcpp::SHT_NOTE)
4039 {
4040 // Assume that all note sections will fit into a single
4041 // PT_NOTE segment.
4042 if (!saw_note)
4043 {
4044 ++ret;
4045 saw_note = true;
4046 }
4047 }
4048 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
4049 {
4050 // There can only be one PT_TLS segment.
4051 if (!saw_tls)
4052 {
4053 ++ret;
4054 saw_tls = true;
4055 }
4056 }
4057 }
4058
4059 return ret;
4060}
4061
1c4f3631
ILT
4062// Create the segments from a PHDRS clause. Return the segment which
4063// should hold the file header and program headers, if any.
4064
4065Output_segment*
f6973bdc
ILT
4066Script_sections::create_segments_from_phdrs_clause(Layout* layout,
4067 uint64_t dot_alignment)
1c4f3631
ILT
4068{
4069 this->attach_sections_using_phdrs_clause(layout);
f6973bdc 4070 return this->set_phdrs_clause_addresses(layout, dot_alignment);
1c4f3631
ILT
4071}
4072
4073// Create the segments from the PHDRS clause, and put the output
4074// sections in them.
4075
4076void
4077Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
4078{
4079 typedef std::map<std::string, Output_segment*> Name_to_segment;
4080 Name_to_segment name_to_segment;
4081 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4082 p != this->phdrs_elements_->end();
4083 ++p)
4084 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
4085
4086 // Walk through the output sections and attach them to segments.
4087 // Output sections in the script which do not list segments are
4088 // attached to the same set of segments as the immediately preceding
4089 // output section.
20e6d0d6 4090
1c4f3631 4091 String_list* phdr_names = NULL;
20e6d0d6 4092 bool load_segments_only = false;
1c4f3631
ILT
4093 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4094 p != this->sections_elements_->end();
4095 ++p)
4096 {
aecf301f 4097 bool is_orphan;
20e6d0d6 4098 String_list* old_phdr_names = phdr_names;
aecf301f 4099 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &is_orphan);
1c4f3631
ILT
4100 if (os == NULL)
4101 continue;
4102
aecf301f
ILT
4103 elfcpp::Elf_Word seg_flags =
4104 Layout::section_flags_to_segment(os->flags());
4105
1c4f3631
ILT
4106 if (phdr_names == NULL)
4107 {
aecf301f
ILT
4108 // Don't worry about empty orphan sections.
4109 if (is_orphan && os->current_data_size() > 0)
4110 gold_error(_("allocated section %s not in any segment"),
4111 os->name());
4112
4113 // To avoid later crashes drop this section into the first
4114 // PT_LOAD segment.
4115 for (Phdrs_elements::const_iterator ppe =
4116 this->phdrs_elements_->begin();
4117 ppe != this->phdrs_elements_->end();
4118 ++ppe)
4119 {
4120 Output_segment* oseg = (*ppe)->segment();
4121 if (oseg->type() == elfcpp::PT_LOAD)
4122 {
4123 oseg->add_output_section_to_load(layout, os, seg_flags);
4124 break;
4125 }
4126 }
4127
1c4f3631
ILT
4128 continue;
4129 }
4130
20e6d0d6
DK
4131 // We see a list of segments names. Disable PT_LOAD segment only
4132 // filtering.
4133 if (old_phdr_names != phdr_names)
4134 load_segments_only = false;
4135
2cefc357
ILT
4136 // If this is an orphan section--one that was not explicitly
4137 // mentioned in the linker script--then it should not inherit
4138 // any segment type other than PT_LOAD. Otherwise, e.g., the
4139 // PT_INTERP segment will pick up following orphan sections,
4140 // which does not make sense. If this is not an orphan section,
4141 // we trust the linker script.
aecf301f 4142 if (is_orphan)
2cefc357 4143 {
20e6d0d6
DK
4144 // Enable PT_LOAD segments only filtering until we see another
4145 // list of segment names.
4146 load_segments_only = true;
2cefc357
ILT
4147 }
4148
1c4f3631
ILT
4149 bool in_load_segment = false;
4150 for (String_list::const_iterator q = phdr_names->begin();
4151 q != phdr_names->end();
4152 ++q)
4153 {
4154 Name_to_segment::const_iterator r = name_to_segment.find(*q);
4155 if (r == name_to_segment.end())
4156 gold_error(_("no segment %s"), q->c_str());
4157 else
4158 {
20e6d0d6
DK
4159 if (load_segments_only
4160 && r->second->type() != elfcpp::PT_LOAD)
4161 continue;
4162
22f0da72
ILT
4163 if (r->second->type() != elfcpp::PT_LOAD)
4164 r->second->add_output_section_to_nonload(os, seg_flags);
4165 else
1c4f3631 4166 {
22f0da72 4167 r->second->add_output_section_to_load(layout, os, seg_flags);
1c4f3631
ILT
4168 if (in_load_segment)
4169 gold_error(_("section in two PT_LOAD segments"));
4170 in_load_segment = true;
4171 }
4172 }
4173 }
4174
4175 if (!in_load_segment)
4176 gold_error(_("allocated section not in any PT_LOAD segment"));
4177 }
4178}
4179
4180// Set the addresses for segments created from a PHDRS clause. Return
4181// the segment which should hold the file header and program headers,
4182// if any.
4183
4184Output_segment*
f6973bdc
ILT
4185Script_sections::set_phdrs_clause_addresses(Layout* layout,
4186 uint64_t dot_alignment)
1c4f3631
ILT
4187{
4188 Output_segment* load_seg = NULL;
4189 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4190 p != this->phdrs_elements_->end();
4191 ++p)
4192 {
4193 // Note that we have to set the flags after adding the output
4194 // sections to the segment, as adding an output segment can
4195 // change the flags.
4196 (*p)->set_flags_if_valid();
4197
4198 Output_segment* oseg = (*p)->segment();
4199
4200 if (oseg->type() != elfcpp::PT_LOAD)
4201 {
4202 // The addresses of non-PT_LOAD segments are set from the
4203 // PT_LOAD segments.
4204 if ((*p)->has_load_address())
4205 gold_error(_("may only specify load address for PT_LOAD segment"));
4206 continue;
4207 }
4208
f6973bdc
ILT
4209 oseg->set_minimum_p_align(dot_alignment);
4210
1c4f3631
ILT
4211 // The output sections should have addresses from the SECTIONS
4212 // clause. The addresses don't have to be in order, so find the
4213 // one with the lowest load address. Use that to set the
4214 // address of the segment.
4215
4216 Output_section* osec = oseg->section_with_lowest_load_address();
4217 if (osec == NULL)
4218 {
4219 oseg->set_addresses(0, 0);
4220 continue;
4221 }
4222
4223 uint64_t vma = osec->address();
4224 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
4225
4226 // Override the load address of the section with the load
4227 // address specified for the segment.
4228 if ((*p)->has_load_address())
4229 {
4230 if (osec->has_load_address())
4231 gold_warning(_("PHDRS load address overrides "
4232 "section %s load address"),
4233 osec->name());
4234
4235 lma = (*p)->load_address();
4236 }
4237
4238 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
4239 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
4240 {
4241 // We could support this if we wanted to.
4242 gold_error(_("using only one of FILEHDR and PHDRS is "
4243 "not currently supported"));
4244 }
4245 if (headers)
4246 {
4247 size_t sizeof_headers = this->total_header_size(layout);
4248 uint64_t subtract = this->header_size_adjustment(lma,
4249 sizeof_headers);
4250 if (lma >= subtract && vma >= subtract)
4251 {
4252 lma -= subtract;
4253 vma -= subtract;
4254 }
4255 else
4256 {
4257 gold_error(_("sections loaded on first page without room "
4258 "for file and program headers "
4259 "are not supported"));
4260 }
4261
4262 if (load_seg != NULL)
4263 gold_error(_("using FILEHDR and PHDRS on more than one "
4264 "PT_LOAD segment is not currently supported"));
4265 load_seg = oseg;
4266 }
4267
4268 oseg->set_addresses(vma, lma);
4269 }
4270
4271 return load_seg;
4272}
4273
4274// Add the file header and segment headers to non-load segments
4275// specified in the PHDRS clause.
4276
4277void
4278Script_sections::put_headers_in_phdrs(Output_data* file_header,
4279 Output_data* segment_headers)
4280{
4281 gold_assert(this->saw_phdrs_clause());
4282 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
4283 p != this->phdrs_elements_->end();
4284 ++p)
4285 {
4286 if ((*p)->type() != elfcpp::PT_LOAD)
4287 {
4288 if ((*p)->includes_phdrs())
4289 (*p)->segment()->add_initial_output_data(segment_headers);
4290 if ((*p)->includes_filehdr())
4291 (*p)->segment()->add_initial_output_data(file_header);
4292 }
4293 }
4294}
4295
8f2eb564
ILT
4296// Look for an output section by name and return the address, the load
4297// address, the alignment, and the size. This is used when an
4298// expression refers to an output section which was not actually
4299// created. This returns true if the section was found, false
4300// otherwise.
4301
4302bool
4303Script_sections::get_output_section_info(const char* name, uint64_t* address,
4304 uint64_t* load_address,
2ea97941 4305 uint64_t* addralign,
8f2eb564
ILT
4306 uint64_t* size) const
4307{
4308 if (!this->saw_sections_clause_)
4309 return false;
4310 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4311 p != this->sections_elements_->end();
4312 ++p)
2ea97941 4313 if ((*p)->get_output_section_info(name, address, load_address, addralign,
8f2eb564
ILT
4314 size))
4315 return true;
4316 return false;
4317}
4318
20e6d0d6
DK
4319// Release all Output_segments. This remove all pointers to all
4320// Output_segments.
4321
4322void
4323Script_sections::release_segments()
4324{
4325 if (this->saw_phdrs_clause())
4326 {
4327 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4328 p != this->phdrs_elements_->end();
4329 ++p)
4330 (*p)->release_segment();
4331 }
4332}
4333
494e05f4
ILT
4334// Print the SECTIONS clause to F for debugging.
4335
4336void
4337Script_sections::print(FILE* f) const
4338{
7f8cd844
NC
4339 if (this->phdrs_elements_ != NULL)
4340 {
4341 fprintf(f, "PHDRS {\n");
4342 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4343 p != this->phdrs_elements_->end();
4344 ++p)
4345 (*p)->print(f);
4346 fprintf(f, "}\n");
4347 }
4348
4349 if (this->memory_regions_ != NULL)
4350 {
4351 fprintf(f, "MEMORY {\n");
4352 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
4353 m != this->memory_regions_->end();
4354 ++m)
4355 (*m)->print(f);
4356 fprintf(f, "}\n");
4357 }
4358
494e05f4
ILT
4359 if (!this->saw_sections_clause_)
4360 return;
4361
4362 fprintf(f, "SECTIONS {\n");
4363
4364 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4365 p != this->sections_elements_->end();
4366 ++p)
4367 (*p)->print(f);
4368
4369 fprintf(f, "}\n");
4370}
4371
4372} // End namespace gold.
This page took 0.547939 seconds and 4 git commands to generate.