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