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