* README: Remove claim that MEMORY is not supported.
[deliverable/binutils-gdb.git] / gold / script-sections.cc
1 // script-sections.cc -- linker script SECTIONS for gold
2
3 // Copyright 2008, 2009 Free Software Foundation, Inc.
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
25 #include <cstring>
26 #include <algorithm>
27 #include <list>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <fnmatch.h>
32
33 #include "parameters.h"
34 #include "object.h"
35 #include "layout.h"
36 #include "output.h"
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
43 namespace gold
44 {
45
46 // A region of memory.
47 class 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),
58 vma_sections_(NULL),
59 lma_sections_(NULL)
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
180 void
181 Memory_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
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
220 class 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,
256 PLACE_TLS,
257 PLACE_TLS_BSS,
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
289 Orphan_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");
295 this->initialize_place(PLACE_TLS, NULL);
296 this->initialize_place(PLACE_TLS_BSS, NULL);
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
306 void
307 Orphan_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
318 void
319 Orphan_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
379 void
380 Orphan_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
390 bool
391 Orphan_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;
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 }
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;
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;
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
479 Orphan_section_placement::Elements_iterator
480 Orphan_section_placement::last_place() const
481 {
482 gold_assert(this->places_[PLACE_LAST].have_location);
483 return this->places_[PLACE_LAST].location;
484 }
485
486 // An element in a SECTIONS clause.
487
488 class Sections_element
489 {
490 public:
491 Sections_element()
492 { }
493
494 virtual ~Sections_element()
495 { }
496
497 // Return whether an output section is relro.
498 virtual bool
499 is_relro() const
500 { return false; }
501
502 // Record that an output section is relro.
503 virtual void
504 set_is_relro()
505 { }
506
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
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
520 finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
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*
527 output_section_name(const char*, const char*, Output_section***,
528 Script_sections::Section_type*)
529 { return NULL; }
530
531 // Initialize OSP with an output section.
532 virtual void
533 orphan_section_init(Orphan_section_placement*,
534 Script_sections::Elements_iterator)
535 { }
536
537 // Set section addresses. This includes applying assignments if the
538 // the expression is an absolute value.
539 virtual void
540 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
541 uint64_t*)
542 { }
543
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
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
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.
566 virtual Output_section*
567 allocate_to_segment(String_list**, bool*)
568 { return NULL; }
569
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
581 // Return the associated Output_section if there is one.
582 virtual Output_section*
583 get_output_section() const
584 { return NULL; }
585
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
591 // Print the element for debugging purposes.
592 virtual void
593 print(FILE* f) const = 0;
594 };
595
596 // An assignment in a SECTIONS clause outside of an output section.
597
598 class 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)
603 : assignment_(name, namelen, false, val, provide, hidden)
604 { }
605
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,
614 uint64_t* dot_value)
615 {
616 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
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,
624 uint64_t* dot_value, uint64_t*, uint64_t*)
625 {
626 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
627 }
628
629 // Print for debugging.
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
641 // An assignment to the dot symbol in a SECTIONS clause outside of an
642 // output section.
643
644 class 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,
654 uint64_t* dot_value)
655 {
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;
660 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
661 NULL, &dummy, NULL);
662 }
663
664 // Update the dot symbol while setting section addresses.
665 void
666 set_section_addresses(Symbol_table* symtab, Layout* layout,
667 uint64_t* dot_value, uint64_t* dot_alignment,
668 uint64_t* load_address)
669 {
670 Output_section* dummy;
671 *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
672 NULL, &dummy, dot_alignment);
673 *load_address = *dot_value;
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
689 // An assertion in a SECTIONS clause outside of an output section.
690
691 class 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
699 // Check the assertion.
700 void
701 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
702 { this->assertion_.check(symtab, layout); }
703
704 // Print for debugging.
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
718 class Output_section_element
719 {
720 public:
721 // A list of input sections.
722 typedef std::list<Output_section::Input_section> Input_section_list;
723
724 Output_section_element()
725 { }
726
727 virtual ~Output_section_element()
728 { }
729
730 // Return whether this element requires an output section to exist.
731 virtual bool
732 needs_output_section() const
733 { return false; }
734
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
742 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
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,
755 uint64_t*, uint64_t*, Output_section**, std::string*,
756 Input_section_list*)
757 { }
758
759 // Print the element for debugging purposes.
760 virtual void
761 print(FILE* f) const = 0;
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;
768 };
769
770 std::string
771 Output_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
783 // A symbol assignment in an output section.
784
785 class 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)
791 : assignment_(name, namelen, false, val, provide, hidden)
792 { }
793
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,
802 uint64_t* dot_value, Output_section** dot_section)
803 {
804 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
805 *dot_section);
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*,
813 uint64_t, uint64_t* dot_value, uint64_t*,
814 Output_section**, std::string*, Input_section_list*)
815 {
816 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
817 }
818
819 // Print for debugging.
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
831 // An assignment to the dot symbol in an output section.
832
833 class 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,
843 uint64_t* dot_value, Output_section** dot_section)
844 {
845 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
846 *dot_section, dot_section, NULL);
847 }
848
849 // Update the dot symbol while setting section addresses.
850 void
851 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
852 uint64_t, uint64_t* dot_value, uint64_t*,
853 Output_section**, std::string*, Input_section_list*);
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
870 void
871 Output_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,
877 uint64_t* dot_alignment,
878 Output_section** dot_section,
879 std::string* fill,
880 Input_section_list*)
881 {
882 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
883 *dot_value, *dot_section,
884 dot_section, dot_alignment);
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())
893 posd = new Output_data_zero_fill(length, 0);
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);
900 layout->new_output_section_data_from_script(posd);
901 }
902 *dot_value = next_dot;
903 }
904
905 // An assertion in an output section.
906
907 class 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
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.
929
930 class Output_data_expression : public Output_section_data
931 {
932 public:
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)
936 : Output_section_data(size, 0, true),
937 is_signed_(is_signed), val_(val), symtab_(symtab),
938 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
939 { }
940
941 protected:
942 // Write the data to the output file.
943 void
944 do_write(Output_file*);
945
946 // Write the data to a buffer.
947 void
948 do_write_to_buffer(unsigned char*);
949
950 // Write to a map file.
951 void
952 do_print_to_mapfile(Mapfile* mapfile) const
953 { mapfile->print_output_data(this, _("** expression")); }
954
955 private:
956 template<bool big_endian>
957 void
958 endian_write_to_buffer(uint64_t, unsigned char*);
959
960 bool is_signed_;
961 Expression* val_;
962 const Symbol_table* symtab_;
963 const Layout* layout_;
964 uint64_t dot_value_;
965 Output_section* dot_section_;
966 };
967
968 // Write the data element to the output file.
969
970 void
971 Output_data_expression::do_write(Output_file* of)
972 {
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 }
977
978 // Write the data element to a buffer.
979
980 void
981 Output_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_,
985 true, this->dot_value_,
986 this->dot_section_, &dummy, NULL);
987
988 if (parameters->target().is_big_endian())
989 this->endian_write_to_buffer<true>(val, buf);
990 else
991 this->endian_write_to_buffer<false>(val, buf);
992 }
993
994 template<bool big_endian>
995 void
996 Output_data_expression::endian_write_to_buffer(uint64_t val,
997 unsigned char* buf)
998 {
999 switch (this->data_size())
1000 {
1001 case 1:
1002 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
1003 break;
1004 case 2:
1005 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
1006 break;
1007 case 4:
1008 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
1009 break;
1010 case 8:
1011 if (parameters->target().get_size() == 32)
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);
1018 break;
1019 default:
1020 gold_unreachable();
1021 }
1022 }
1023
1024 // A data item in an output section.
1025
1026 class 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
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
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,
1047 uint64_t* dot_value, uint64_t*, Output_section**,
1048 std::string*, Input_section_list*);
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
1065 void
1066 Output_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,
1072 uint64_t*,
1073 Output_section** dot_section,
1074 std::string*,
1075 Input_section_list*)
1076 {
1077 gold_assert(os != NULL);
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);
1083 *dot_value += this->size_;
1084 }
1085
1086 // Print for debugging.
1087
1088 void
1089 Output_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
1119 class Output_section_element_fill : public Output_section_element
1120 {
1121 public:
1122 Output_section_element_fill(Expression* val)
1123 : val_(val)
1124 { }
1125
1126 // Update the fill value while setting section addresses.
1127 void
1128 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1129 uint64_t, uint64_t* dot_value, uint64_t*,
1130 Output_section** dot_section,
1131 std::string* fill, Input_section_list*)
1132 {
1133 Output_section* fill_section;
1134 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
1135 *dot_value, *dot_section,
1136 &fill_section, NULL);
1137 if (fill_section != NULL)
1138 gold_warning(_("fill value is not absolute"));
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.
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
1161 class Output_section_element_input : public Output_section_element
1162 {
1163 public:
1164 Output_section_element_input(const Input_section_spec* spec, bool keep);
1165
1166 // Finalize symbols--just update the value of the dot symbol.
1167 void
1168 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1169 Output_section** dot_section)
1170 {
1171 *dot_value = this->final_dot_value_;
1172 *dot_section = this->final_dot_section_;
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*,
1183 uint64_t subalign, uint64_t* dot_value, uint64_t*,
1184 Output_section**, std::string* fill,
1185 Input_section_list*);
1186
1187 // Print for debugging.
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;
1196 bool pattern_is_wildcard;
1197 Sort_wildcard sort;
1198
1199 Input_section_pattern(const char* patterna, size_t patternlena,
1200 Sort_wildcard sorta)
1201 : pattern(patterna, patternlena),
1202 pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
1203 sort(sorta)
1204 { }
1205 };
1206
1207 typedef std::vector<Input_section_pattern> Input_section_patterns;
1208
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 }
1222
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.
1229 std::string filename_pattern_;
1230 // Whether the file name pattern is a wildcard.
1231 bool filename_is_wildcard_;
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_;
1241 // The value of dot after including all matching sections.
1242 uint64_t final_dot_value_;
1243 // The section where dot is defined after including all matching
1244 // sections.
1245 Output_section* final_dot_section_;
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
1252 Output_section_element_input::Output_section_element_input(
1253 const Input_section_spec* spec,
1254 bool keep)
1255 : filename_pattern_(),
1256 filename_is_wildcard_(false),
1257 filename_sort_(spec->file.sort),
1258 filename_exclusions_(),
1259 input_section_patterns_(),
1260 keep_(keep),
1261 final_dot_value_(0),
1262 final_dot_section_(NULL)
1263 {
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);
1269 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
1270
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)
1277 {
1278 bool is_wildcard = is_wildcard_string((*p).c_str());
1279 this->filename_exclusions_.push_back(std::make_pair(*p,
1280 is_wildcard));
1281 }
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
1296 // See whether we match FILE_NAME.
1297
1298 bool
1299 Output_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
1332 bool
1333 Output_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
1359 class Input_section_info
1360 {
1361 public:
1362 Input_section_info(const Output_section::Input_section& input_section)
1363 : input_section_(input_section), section_name_(),
1364 size_(0), addralign_(1)
1365 { }
1366
1367 // Return the simple input section.
1368 const Output_section::Input_section&
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
1389 set_section_name(const std::string name)
1390 { this->section_name_ = name; }
1391
1392 // Return the section size.
1393 uint64_t
1394 size() const
1395 { return this->size_; }
1396
1397 // Set the section size.
1398 void
1399 set_size(uint64_t size)
1400 { this->size_ = size; }
1401
1402 // Return the address alignment.
1403 uint64_t
1404 addralign() const
1405 { return this->addralign_; }
1406
1407 // Set the address alignment.
1408 void
1409 set_addralign(uint64_t addralign)
1410 { this->addralign_ = addralign; }
1411
1412 private:
1413 // Input section, can be a relaxed section.
1414 Output_section::Input_section input_section_;
1415 // Name of the section.
1416 std::string section_name_;
1417 // Section size.
1418 uint64_t size_;
1419 // Address alignment.
1420 uint64_t addralign_;
1421 };
1422
1423 // A class to sort the input sections.
1424
1425 class 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
1440 bool
1441 Input_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
1447 && isi1.addralign() == isi2.addralign()))
1448 {
1449 if (isi1.section_name() != isi2.section_name())
1450 return isi1.section_name() < isi2.section_name();
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 {
1456 if (isi1.addralign() != isi2.addralign())
1457 return isi1.addralign() < isi2.addralign();
1458 }
1459 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1460 {
1461 if (isi1.relobj()->name() != isi2.relobj()->name())
1462 return (isi1.relobj()->name() < isi2.relobj()->name());
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
1473 void
1474 Output_section_element_input::set_section_addresses(
1475 Symbol_table*,
1476 Layout* layout,
1477 Output_section* output_section,
1478 uint64_t subalign,
1479 uint64_t* dot_value,
1480 uint64_t*,
1481 Output_section** dot_section,
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 {
1501 Relobj* relobj = p->relobj();
1502 unsigned int shndx = p->shndx();
1503 Input_section_info isi(*p);
1504
1505 // Calling section_name and section_addralign is not very
1506 // efficient.
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);
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())
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 }
1523 else
1524 {
1525 isi.set_size(relobj->section_size(shndx));
1526 isi.set_addralign(relobj->section_addralign(shndx));
1527 }
1528 }
1529
1530 if (!this->match_file_name(relobj->name().c_str()))
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]);
1544 if (match(isi.section_name().c_str(), isp.pattern.c_str(),
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
1564 uint64_t dot = *dot_value;
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
1580 for (std::vector<Input_section_info>::const_iterator p =
1581 matching_sections[i].begin();
1582 p != matching_sections[i].end();
1583 ++p)
1584 {
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();
1594 if (this_subalign < subalign)
1595 {
1596 this_subalign = subalign;
1597 sis.set_addralign(subalign);
1598 }
1599
1600 uint64_t address = align_address(dot, this_subalign);
1601
1602 if (address > dot && !fill->empty())
1603 {
1604 section_size_type length =
1605 convert_to_section_size_type(address - dot);
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);
1609 layout->new_output_section_data_from_script(posd);
1610 }
1611
1612 output_section->add_script_input_section(sis);
1613 dot = address + data_size;
1614 }
1615 }
1616
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
1624 this->final_dot_value_ = *dot_value;
1625 this->final_dot_section_ = *dot_section;
1626 }
1627
1628 // Print for debugging.
1629
1630 void
1631 Output_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, ", ");
1676 fprintf(f, "%s", p->first.c_str());
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
1735 class Output_section_definition : public Sections_element
1736 {
1737 public:
1738 typedef Output_section_element::Input_section_list Input_section_list;
1739
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);
1751
1752 // Add an assignment to the special dot symbol.
1753 void
1754 add_dot_assignment(Expression* value);
1755
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
1772 // Return whether the output section is relro.
1773 bool
1774 is_relro() const
1775 { return this->is_relro_; }
1776
1777 // Record that the output section is relro.
1778 void
1779 set_is_relro()
1780 { this->is_relro_ = true; }
1781
1782 // Create any required output sections.
1783 void
1784 create_sections(Layout*);
1785
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
1792 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
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,
1798 Output_section***, Script_sections::Section_type*);
1799
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); }
1805
1806 // Set the section address.
1807 void
1808 set_section_addresses(Symbol_table* symtab, Layout* layout,
1809 uint64_t* dot_value, uint64_t*,
1810 uint64_t* load_address);
1811
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
1824 // Get the list of segments to use for an allocated section when
1825 // using a PHDRS clause.
1826 Output_section*
1827 allocate_to_segment(String_list** phdrs_list, bool* orphan);
1828
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
1838 // Return the associated Output_section if there is one.
1839 Output_section*
1840 get_output_section() const
1841 { return this->output_section_; }
1842
1843 // Print the contents to the FILE. This is for debugging.
1844 void
1845 print(FILE*) const;
1846
1847 // Return the output section type if specified or Script_sections::ST_NONE.
1848 Script_sections::Section_type
1849 section_type() const;
1850
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
1867 private:
1868 static const char*
1869 script_section_type_name(Script_section_type);
1870
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_;
1883 // The constraint, if any.
1884 Section_constraint constraint_;
1885 // The fill value. This may be NULL.
1886 Expression* fill_;
1887 // The list of segments this section should go into. This may be
1888 // NULL.
1889 String_list* phdrs_;
1890 // The list of elements defining the section.
1891 Output_section_elements elements_;
1892 // The Output_section created for this definition. This will be
1893 // NULL if none was created.
1894 Output_section* output_section_;
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_;
1901 // The output section is relro.
1902 bool is_relro_;
1903 // The output section type if specified.
1904 enum Script_section_type script_section_type_;
1905 };
1906
1907 // Constructor.
1908
1909 Output_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),
1918 constraint_(header->constraint),
1919 fill_(NULL),
1920 phdrs_(NULL),
1921 elements_(),
1922 output_section_(NULL),
1923 evaluated_address_(0),
1924 evaluated_load_address_(0),
1925 evaluated_addralign_(0),
1926 is_relro_(false),
1927 script_section_type_(header->section_type)
1928 {
1929 }
1930
1931 // Finish an output section.
1932
1933 void
1934 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1935 {
1936 this->fill_ = trailer->fill;
1937 this->phdrs_ = trailer->phdrs;
1938 }
1939
1940 // Add a symbol to be defined.
1941
1942 void
1943 Output_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
1957 // Add an assignment to the special dot symbol.
1958
1959 void
1960 Output_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
1968 void
1969 Output_section_definition::add_assertion(Expression* check,
1970 const char* message,
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
1981 void
1982 Output_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
1991 void
1992 Output_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
2000 void
2001 Output_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
2008 // Create any required output sections. We need an output section if
2009 // there is a data statement here.
2010
2011 void
2012 Output_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();
2023 this->output_section_ =
2024 layout->make_output_section_for_script(name, this->section_type());
2025 return;
2026 }
2027 }
2028 }
2029
2030 // Add any symbols being defined to the symbol table.
2031
2032 void
2033 Output_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
2043 void
2044 Output_section_definition::finalize_symbols(Symbol_table* symtab,
2045 const Layout* layout,
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 {
2055 Output_section* dummy;
2056 address = this->address_->eval_with_dot(symtab, layout, true,
2057 *dot_value, NULL,
2058 &dummy, NULL);
2059 }
2060 if (this->align_ != NULL)
2061 {
2062 Output_section* dummy;
2063 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
2064 *dot_value,
2065 NULL,
2066 &dummy, NULL);
2067 address = align_address(address, align);
2068 }
2069 *dot_value = address;
2070 }
2071
2072 Output_section* dot_section = this->output_section_;
2073 for (Output_section_elements::iterator p = this->elements_.begin();
2074 p != this->elements_.end();
2075 ++p)
2076 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
2077 }
2078
2079 // Return the output section name to use for an input section name.
2080
2081 const char*
2082 Output_section_definition::output_section_name(
2083 const char* file_name,
2084 const char* section_name,
2085 Output_section*** slot,
2086 Script_sections::Section_type* psection_type)
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_;
2098 *psection_type = this->section_type();
2099 return this->name_.c_str();
2100 }
2101 }
2102
2103 // We don't know about this section name.
2104 return NULL;
2105 }
2106
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
2111 void
2112 Output_section_definition::set_section_addresses(Symbol_table* symtab,
2113 Layout* layout,
2114 uint64_t* dot_value,
2115 uint64_t* dot_alignment,
2116 uint64_t* load_address)
2117 {
2118 uint64_t address;
2119 uint64_t old_dot_value = *dot_value;
2120 uint64_t old_load_address = *load_address;
2121
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)
2129 {
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 }
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 {
2151 Output_section* align_section;
2152 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
2153 NULL, &align_section, NULL);
2154 if (align_section != NULL)
2155 gold_warning(_("alignment of section %s is not absolute"),
2156 this->name_.c_str());
2157 if (this->output_section_ != NULL)
2158 this->output_section_->set_addralign(align);
2159 }
2160
2161 address = align_address(address, align);
2162
2163 uint64_t start_address = address;
2164
2165 *dot_value = address;
2166
2167 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2168 // forced to zero, regardless of what the linker script wants.
2169 if (this->output_section_ != NULL
2170 && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
2171 || this->output_section_->is_noload()))
2172 this->output_section_->set_address(address);
2173
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
2180 {
2181 Output_section* dummy;
2182 uint64_t laddr =
2183 this->load_address_->eval_with_dot(symtab, layout, true, *dot_value,
2184 this->output_section_, &dummy,
2185 NULL);
2186 if (this->output_section_ != NULL)
2187 this->output_section_->set_load_address(laddr);
2188 this->evaluated_load_address_ = laddr;
2189 }
2190
2191 uint64_t subalign;
2192 if (this->subalign_ == NULL)
2193 subalign = 0;
2194 else
2195 {
2196 Output_section* subalign_section;
2197 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2198 *dot_value, NULL,
2199 &subalign_section, NULL);
2200 if (subalign_section != NULL)
2201 gold_warning(_("subalign of section %s is not absolute"),
2202 this->name_.c_str());
2203 }
2204
2205 std::string fill;
2206 if (this->fill_ != NULL)
2207 {
2208 // FIXME: The GNU linker supports fill values of arbitrary
2209 // length.
2210 Output_section* fill_section;
2211 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
2212 *dot_value,
2213 NULL, &fill_section,
2214 NULL);
2215 if (fill_section != NULL)
2216 gold_warning(_("fill of section %s is not absolute"),
2217 this->name_.c_str());
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
2235 Output_section* dot_section = this->output_section_;
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_,
2240 subalign, dot_value, dot_alignment,
2241 &dot_section, &fill, &input_sections);
2242
2243 gold_assert(input_sections.empty());
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));
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();
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 }
2264 }
2265 }
2266
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
2271 Section_constraint
2272 Output_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
2311 bool
2312 Output_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
2346 if (this->is_relro_)
2347 this->output_section_->set_is_relro();
2348 else
2349 this->output_section_->clear_is_relro();
2350
2351 return true;
2352 }
2353
2354 // Get the list of segments to use for an allocated section when using
2355 // a PHDRS clause.
2356
2357 Output_section*
2358 Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2359 bool* orphan)
2360 {
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
2366 if (this->output_section_ == NULL)
2367 return NULL;
2368 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2369 return NULL;
2370 *orphan = false;
2371 return this->output_section_;
2372 }
2373
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
2380 bool
2381 Output_section_definition::get_output_section_info(const char* name,
2382 uint64_t* address,
2383 uint64_t* load_address,
2384 uint64_t* addralign,
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;
2397 *addralign = this->output_section_->addralign();
2398 *size = this->output_section_->current_data_size();
2399 }
2400 else
2401 {
2402 *address = this->evaluated_address_;
2403 *load_address = this->evaluated_load_address_;
2404 *addralign = this->evaluated_addralign_;
2405 *size = 0;
2406 }
2407
2408 return true;
2409 }
2410
2411 // Print for debugging.
2412
2413 void
2414 Output_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
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
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
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
2474 fprintf(f, "\n");
2475 }
2476
2477 Script_sections::Section_type
2478 Output_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
2500 const char*
2501 Output_section_definition::script_section_type_name(
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
2523 void
2524 Output_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
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
2536 class Orphan_output_section : public Sections_element
2537 {
2538 public:
2539 Orphan_output_section(Output_section* os)
2540 : os_(os)
2541 { }
2542
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.
2546 bool
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(); }
2556
2557 // Set section addresses.
2558 void
2559 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2560 uint64_t*);
2561
2562 // Get the list of segments to use for an allocated section when
2563 // using a PHDRS clause.
2564 Output_section*
2565 allocate_to_segment(String_list**, bool*);
2566
2567 // Return the associated Output_section.
2568 Output_section*
2569 get_output_section() const
2570 { return this->os_; }
2571
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
2584 // Set section addresses.
2585
2586 void
2587 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
2588 uint64_t* dot_value,
2589 uint64_t*,
2590 uint64_t* load_address)
2591 {
2592 typedef std::list<Output_section::Input_section> Input_section_list;
2593
2594 bool have_load_address = *load_address != *dot_value;
2595
2596 uint64_t address = *dot_value;
2597 address = align_address(address, this->os_->addralign());
2598
2599 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
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 }
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 {
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();
2618 address = align_address(address, addralign);
2619 this->os_->add_script_input_section(*p);
2620 address += size;
2621 }
2622
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;
2632
2633 *dot_value = address;
2634 }
2635 }
2636
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
2641 Output_section*
2642 Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
2643 {
2644 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2645 return NULL;
2646 *orphan = true;
2647 return this->os_;
2648 }
2649
2650 // Class Phdrs_element. A program header from a PHDRS clause.
2651
2652 class Phdrs_element
2653 {
2654 public:
2655 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2656 bool includes_filehdr, bool includes_phdrs,
2657 bool is_flags_valid, unsigned int flags,
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),
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
2692 eval_load_address(Symbol_table* symtab, Layout* layout)
2693 {
2694 if (this->load_address_ != NULL)
2695 this->load_address_value_ = this->load_address_->eval(symtab, layout,
2696 true);
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
2720 // Release the segment.
2721 void
2722 release_segment()
2723 { this->segment_ = NULL; }
2724
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
2733 // Print for debugging.
2734 void
2735 print(FILE*) const;
2736
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
2759 // Print for debugging.
2760
2761 void
2762 Phdrs_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
2780 // Add a memory region.
2781
2782 void
2783 Script_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 {
2791 gold_error (_("region '%.*s' already defined"), namelen, name);
2792 // FIXME: Add a GOLD extension to allow multiple regions with the same
2793 // name. This would amount to a single region covering disjoint blocks
2794 // of memory, which is useful for embedded devices.
2795 }
2796
2797 // FIXME: Check the length and start values. Currently we allow
2798 // non-constant expressions for these values, whereas LD does not.
2799
2800 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
2801 // describe a region that packs from the end address going down, rather
2802 // than the start address going up. This would be useful for embedded
2803 // devices.
2804
2805 this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
2806 start, length));
2807 }
2808
2809 // Find a memory region.
2810
2811 Memory_region*
2812 Script_sections::find_memory_region(const char* name, size_t namelen)
2813 {
2814 if (this->memory_regions_ == NULL)
2815 return NULL;
2816
2817 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
2818 m != this->memory_regions_->end();
2819 ++m)
2820 if ((*m)->name_match(name, namelen))
2821 return *m;
2822
2823 return NULL;
2824 }
2825
2826 // Find a memory region's origin.
2827
2828 Expression*
2829 Script_sections::find_memory_region_origin(const char* name, size_t namelen)
2830 {
2831 Memory_region* mr = find_memory_region(name, namelen);
2832 if (mr == NULL)
2833 return NULL;
2834
2835 return mr->start_address();
2836 }
2837
2838 // Find a memory region's length.
2839
2840 Expression*
2841 Script_sections::find_memory_region_length(const char* name, size_t namelen)
2842 {
2843 Memory_region* mr = find_memory_region(name, namelen);
2844 if (mr == NULL)
2845 return NULL;
2846
2847 return mr->length();
2848 }
2849
2850 // Set the memory region to use for the current section.
2851
2852 void
2853 Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
2854 {
2855 gold_assert(!this->sections_elements_->empty());
2856 this->sections_elements_->back()->set_memory_region(mr, set_vma);
2857 }
2858
2859 // Class Script_sections.
2860
2861 Script_sections::Script_sections()
2862 : saw_sections_clause_(false),
2863 in_sections_clause_(false),
2864 sections_elements_(NULL),
2865 output_section_(NULL),
2866 memory_regions_(NULL),
2867 phdrs_elements_(NULL),
2868 orphan_section_placement_(NULL),
2869 data_segment_align_start_(),
2870 saw_data_segment_align_(false),
2871 saw_relro_end_(false),
2872 saw_segment_start_expression_(false)
2873 {
2874 }
2875
2876 // Start a SECTIONS clause.
2877
2878 void
2879 Script_sections::start_sections()
2880 {
2881 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2882 this->saw_sections_clause_ = true;
2883 this->in_sections_clause_ = true;
2884 if (this->sections_elements_ == NULL)
2885 this->sections_elements_ = new Sections_elements;
2886 }
2887
2888 // Finish a SECTIONS clause.
2889
2890 void
2891 Script_sections::finish_sections()
2892 {
2893 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2894 this->in_sections_clause_ = false;
2895 }
2896
2897 // Add a symbol to be defined.
2898
2899 void
2900 Script_sections::add_symbol_assignment(const char* name, size_t length,
2901 Expression* val, bool provide,
2902 bool hidden)
2903 {
2904 if (this->output_section_ != NULL)
2905 this->output_section_->add_symbol_assignment(name, length, val,
2906 provide, hidden);
2907 else
2908 {
2909 Sections_element* p = new Sections_element_assignment(name, length,
2910 val, provide,
2911 hidden);
2912 this->sections_elements_->push_back(p);
2913 }
2914 }
2915
2916 // Add an assignment to the special dot symbol.
2917
2918 void
2919 Script_sections::add_dot_assignment(Expression* val)
2920 {
2921 if (this->output_section_ != NULL)
2922 this->output_section_->add_dot_assignment(val);
2923 else
2924 {
2925 // The GNU linker permits assignments to . to appears outside of
2926 // a SECTIONS clause, and treats it as appearing inside, so
2927 // sections_elements_ may be NULL here.
2928 if (this->sections_elements_ == NULL)
2929 {
2930 this->sections_elements_ = new Sections_elements;
2931 this->saw_sections_clause_ = true;
2932 }
2933
2934 Sections_element* p = new Sections_element_dot_assignment(val);
2935 this->sections_elements_->push_back(p);
2936 }
2937 }
2938
2939 // Add an assertion.
2940
2941 void
2942 Script_sections::add_assertion(Expression* check, const char* message,
2943 size_t messagelen)
2944 {
2945 if (this->output_section_ != NULL)
2946 this->output_section_->add_assertion(check, message, messagelen);
2947 else
2948 {
2949 Sections_element* p = new Sections_element_assertion(check, message,
2950 messagelen);
2951 this->sections_elements_->push_back(p);
2952 }
2953 }
2954
2955 // Start processing entries for an output section.
2956
2957 void
2958 Script_sections::start_output_section(
2959 const char* name,
2960 size_t namelen,
2961 const Parser_output_section_header* header)
2962 {
2963 Output_section_definition* posd = new Output_section_definition(name,
2964 namelen,
2965 header);
2966 this->sections_elements_->push_back(posd);
2967 gold_assert(this->output_section_ == NULL);
2968 this->output_section_ = posd;
2969 }
2970
2971 // Stop processing entries for an output section.
2972
2973 void
2974 Script_sections::finish_output_section(
2975 const Parser_output_section_trailer* trailer)
2976 {
2977 gold_assert(this->output_section_ != NULL);
2978 this->output_section_->finish(trailer);
2979 this->output_section_ = NULL;
2980 }
2981
2982 // Add a data item to the current output section.
2983
2984 void
2985 Script_sections::add_data(int size, bool is_signed, Expression* val)
2986 {
2987 gold_assert(this->output_section_ != NULL);
2988 this->output_section_->add_data(size, is_signed, val);
2989 }
2990
2991 // Add a fill value setting to the current output section.
2992
2993 void
2994 Script_sections::add_fill(Expression* val)
2995 {
2996 gold_assert(this->output_section_ != NULL);
2997 this->output_section_->add_fill(val);
2998 }
2999
3000 // Add an input section specification to the current output section.
3001
3002 void
3003 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3004 {
3005 gold_assert(this->output_section_ != NULL);
3006 this->output_section_->add_input_section(spec, keep);
3007 }
3008
3009 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
3010 // subsequent output sections may be relro.
3011
3012 void
3013 Script_sections::data_segment_align()
3014 {
3015 if (this->saw_data_segment_align_)
3016 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3017 gold_assert(!this->sections_elements_->empty());
3018 Sections_elements::iterator p = this->sections_elements_->end();
3019 --p;
3020 this->data_segment_align_start_ = p;
3021 this->saw_data_segment_align_ = true;
3022 }
3023
3024 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
3025 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3026
3027 void
3028 Script_sections::data_segment_relro_end()
3029 {
3030 if (this->saw_relro_end_)
3031 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3032 "in a linker script"));
3033 this->saw_relro_end_ = true;
3034
3035 if (!this->saw_data_segment_align_)
3036 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3037 else
3038 {
3039 Sections_elements::iterator p = this->data_segment_align_start_;
3040 for (++p; p != this->sections_elements_->end(); ++p)
3041 (*p)->set_is_relro();
3042 }
3043 }
3044
3045 // Create any required sections.
3046
3047 void
3048 Script_sections::create_sections(Layout* layout)
3049 {
3050 if (!this->saw_sections_clause_)
3051 return;
3052 for (Sections_elements::iterator p = this->sections_elements_->begin();
3053 p != this->sections_elements_->end();
3054 ++p)
3055 (*p)->create_sections(layout);
3056 }
3057
3058 // Add any symbols we are defining to the symbol table.
3059
3060 void
3061 Script_sections::add_symbols_to_table(Symbol_table* symtab)
3062 {
3063 if (!this->saw_sections_clause_)
3064 return;
3065 for (Sections_elements::iterator p = this->sections_elements_->begin();
3066 p != this->sections_elements_->end();
3067 ++p)
3068 (*p)->add_symbols_to_table(symtab);
3069 }
3070
3071 // Finalize symbols and check assertions.
3072
3073 void
3074 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3075 {
3076 if (!this->saw_sections_clause_)
3077 return;
3078 uint64_t dot_value = 0;
3079 for (Sections_elements::iterator p = this->sections_elements_->begin();
3080 p != this->sections_elements_->end();
3081 ++p)
3082 (*p)->finalize_symbols(symtab, layout, &dot_value);
3083 }
3084
3085 // Return the name of the output section to use for an input file name
3086 // and section name.
3087
3088 const char*
3089 Script_sections::output_section_name(
3090 const char* file_name,
3091 const char* section_name,
3092 Output_section*** output_section_slot,
3093 Script_sections::Section_type* psection_type)
3094 {
3095 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3096 p != this->sections_elements_->end();
3097 ++p)
3098 {
3099 const char* ret = (*p)->output_section_name(file_name, section_name,
3100 output_section_slot,
3101 psection_type);
3102
3103 if (ret != NULL)
3104 {
3105 // The special name /DISCARD/ means that the input section
3106 // should be discarded.
3107 if (strcmp(ret, "/DISCARD/") == 0)
3108 {
3109 *output_section_slot = NULL;
3110 *psection_type = Script_sections::ST_NONE;
3111 return NULL;
3112 }
3113 return ret;
3114 }
3115 }
3116
3117 // If we couldn't find a mapping for the name, the output section
3118 // gets the name of the input section.
3119
3120 *output_section_slot = NULL;
3121 *psection_type = Script_sections::ST_NONE;
3122
3123 return section_name;
3124 }
3125
3126 // Place a marker for an orphan output section into the SECTIONS
3127 // clause.
3128
3129 void
3130 Script_sections::place_orphan(Output_section* os)
3131 {
3132 Orphan_section_placement* osp = this->orphan_section_placement_;
3133 if (osp == NULL)
3134 {
3135 // Initialize the Orphan_section_placement structure.
3136 osp = new Orphan_section_placement();
3137 for (Sections_elements::iterator p = this->sections_elements_->begin();
3138 p != this->sections_elements_->end();
3139 ++p)
3140 (*p)->orphan_section_init(osp, p);
3141 gold_assert(!this->sections_elements_->empty());
3142 Sections_elements::iterator last = this->sections_elements_->end();
3143 --last;
3144 osp->last_init(last);
3145 this->orphan_section_placement_ = osp;
3146 }
3147
3148 Orphan_output_section* orphan = new Orphan_output_section(os);
3149
3150 // Look for where to put ORPHAN.
3151 Sections_elements::iterator* where;
3152 if (osp->find_place(os, &where))
3153 {
3154 if ((**where)->is_relro())
3155 os->set_is_relro();
3156 else
3157 os->clear_is_relro();
3158
3159 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3160 // so that the next one goes after this one.
3161 Sections_elements::iterator p = *where;
3162 gold_assert(p != this->sections_elements_->end());
3163 ++p;
3164 *where = this->sections_elements_->insert(p, orphan);
3165 }
3166 else
3167 {
3168 os->clear_is_relro();
3169 // We don't have a place to put this orphan section. Put it,
3170 // and all other sections like it, at the end, but before the
3171 // sections which always come at the end.
3172 Sections_elements::iterator last = osp->last_place();
3173 *where = this->sections_elements_->insert(last, orphan);
3174 }
3175 }
3176
3177 // Set the addresses of all the output sections. Walk through all the
3178 // elements, tracking the dot symbol. Apply assignments which set
3179 // absolute symbol values, in case they are used when setting dot.
3180 // Fill in data statement values. As we find output sections, set the
3181 // address, set the address of all associated input sections, and
3182 // update dot. Return the segment which should hold the file header
3183 // and segment headers, if any.
3184
3185 Output_segment*
3186 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3187 {
3188 gold_assert(this->saw_sections_clause_);
3189
3190 // Walk the memory regions specified in this script, if any.
3191 if (this->memory_regions_ != NULL)
3192 {
3193 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
3194 mr != this->memory_regions_->end();
3195 ++mr)
3196 {
3197 // FIXME: What should we do with the attributes of the regions ?
3198
3199 // For each region, set the VMA of the sections associated with it.
3200 for (Memory_region::Section_list::const_iterator s =
3201 (*mr)->get_vma_section_list_start();
3202 s != (*mr)->get_vma_section_list_end();
3203 ++s)
3204 {
3205 (*s)->set_section_vma((*mr)->get_current_vma_address());
3206 (*mr)->increment_vma_offset((*s)->get_section_name(),
3207 (*s)->get_output_section()->current_data_size(),
3208 symtab, layout);
3209 }
3210
3211 // Similarly, set the LMA values.
3212 for (Memory_region::Section_list::const_iterator s =
3213 (*mr)->get_lma_section_list_start();
3214 s != (*mr)->get_lma_section_list_end();
3215 ++s)
3216 {
3217 (*s)->set_section_lma((*mr)->get_current_lma_address());
3218 (*mr)->increment_lma_offset((*s)->get_section_name(),
3219 (*s)->get_output_section()->current_data_size(),
3220 symtab, layout);
3221 }
3222 }
3223 }
3224
3225 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3226 // for our representation.
3227 for (Sections_elements::iterator p = this->sections_elements_->begin();
3228 p != this->sections_elements_->end();
3229 ++p)
3230 {
3231 Output_section_definition* posd;
3232 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3233 if (failed_constraint != CONSTRAINT_NONE)
3234 {
3235 Sections_elements::iterator q;
3236 for (q = this->sections_elements_->begin();
3237 q != this->sections_elements_->end();
3238 ++q)
3239 {
3240 if (q != p)
3241 {
3242 if ((*q)->alternate_constraint(posd, failed_constraint))
3243 break;
3244 }
3245 }
3246
3247 if (q == this->sections_elements_->end())
3248 gold_error(_("no matching section constraint"));
3249 }
3250 }
3251
3252 // Force the alignment of the first TLS section to be the maximum
3253 // alignment of all TLS sections.
3254 Output_section* first_tls = NULL;
3255 uint64_t tls_align = 0;
3256 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3257 p != this->sections_elements_->end();
3258 ++p)
3259 {
3260 Output_section* os = (*p)->get_output_section();
3261 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3262 {
3263 if (first_tls == NULL)
3264 first_tls = os;
3265 if (os->addralign() > tls_align)
3266 tls_align = os->addralign();
3267 }
3268 }
3269 if (first_tls != NULL)
3270 first_tls->set_addralign(tls_align);
3271
3272 // For a relocatable link, we implicitly set dot to zero.
3273 uint64_t dot_value = 0;
3274 uint64_t dot_alignment = 0;
3275 uint64_t load_address = 0;
3276
3277 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3278 // to set section addresses. If the script has any SEGMENT_START
3279 // expression, we do not set the section addresses.
3280 bool use_tsection_options =
3281 (!this->saw_segment_start_expression_
3282 && (parameters->options().user_set_Ttext()
3283 || parameters->options().user_set_Tdata()
3284 || parameters->options().user_set_Tbss()));
3285
3286 for (Sections_elements::iterator p = this->sections_elements_->begin();
3287 p != this->sections_elements_->end();
3288 ++p)
3289 {
3290 Output_section* os = (*p)->get_output_section();
3291
3292 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3293 // the special sections by names and doing dot assignments.
3294 if (use_tsection_options
3295 && os != NULL
3296 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3297 {
3298 uint64_t new_dot_value = dot_value;
3299
3300 if (parameters->options().user_set_Ttext()
3301 && strcmp(os->name(), ".text") == 0)
3302 new_dot_value = parameters->options().Ttext();
3303 else if (parameters->options().user_set_Tdata()
3304 && strcmp(os->name(), ".data") == 0)
3305 new_dot_value = parameters->options().Tdata();
3306 else if (parameters->options().user_set_Tbss()
3307 && strcmp(os->name(), ".bss") == 0)
3308 new_dot_value = parameters->options().Tbss();
3309
3310 // Update dot and load address if necessary.
3311 if (new_dot_value < dot_value)
3312 gold_error(_("dot may not move backward"));
3313 else if (new_dot_value != dot_value)
3314 {
3315 dot_value = new_dot_value;
3316 load_address = new_dot_value;
3317 }
3318 }
3319
3320 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3321 &load_address);
3322 }
3323
3324 if (this->phdrs_elements_ != NULL)
3325 {
3326 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3327 p != this->phdrs_elements_->end();
3328 ++p)
3329 (*p)->eval_load_address(symtab, layout);
3330 }
3331
3332 return this->create_segments(layout, dot_alignment);
3333 }
3334
3335 // Sort the sections in order to put them into segments.
3336
3337 class Sort_output_sections
3338 {
3339 public:
3340 bool
3341 operator()(const Output_section* os1, const Output_section* os2) const;
3342 };
3343
3344 bool
3345 Sort_output_sections::operator()(const Output_section* os1,
3346 const Output_section* os2) const
3347 {
3348 // Sort first by the load address.
3349 uint64_t lma1 = (os1->has_load_address()
3350 ? os1->load_address()
3351 : os1->address());
3352 uint64_t lma2 = (os2->has_load_address()
3353 ? os2->load_address()
3354 : os2->address());
3355 if (lma1 != lma2)
3356 return lma1 < lma2;
3357
3358 // Then sort by the virtual address.
3359 if (os1->address() != os2->address())
3360 return os1->address() < os2->address();
3361
3362 // Sort TLS sections to the end.
3363 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3364 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3365 if (tls1 != tls2)
3366 return tls2;
3367
3368 // Sort PROGBITS before NOBITS.
3369 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
3370 return true;
3371 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
3372 return false;
3373
3374 // Sort non-NOLOAD before NOLOAD.
3375 if (os1->is_noload() && !os2->is_noload())
3376 return true;
3377 if (!os1->is_noload() && os2->is_noload())
3378 return true;
3379
3380 // Otherwise we don't care.
3381 return false;
3382 }
3383
3384 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3385 // We treat a section with the SHF_TLS flag set as taking up space
3386 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3387 // space for them in the file.
3388
3389 bool
3390 Script_sections::is_bss_section(const Output_section* os)
3391 {
3392 return (os->type() == elfcpp::SHT_NOBITS
3393 && (os->flags() & elfcpp::SHF_TLS) == 0);
3394 }
3395
3396 // Return the size taken by the file header and the program headers.
3397
3398 size_t
3399 Script_sections::total_header_size(Layout* layout) const
3400 {
3401 size_t segment_count = layout->segment_count();
3402 size_t file_header_size;
3403 size_t segment_headers_size;
3404 if (parameters->target().get_size() == 32)
3405 {
3406 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3407 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3408 }
3409 else if (parameters->target().get_size() == 64)
3410 {
3411 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3412 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3413 }
3414 else
3415 gold_unreachable();
3416
3417 return file_header_size + segment_headers_size;
3418 }
3419
3420 // Return the amount we have to subtract from the LMA to accomodate
3421 // headers of the given size. The complication is that the file
3422 // header have to be at the start of a page, as otherwise it will not
3423 // be at the start of the file.
3424
3425 uint64_t
3426 Script_sections::header_size_adjustment(uint64_t lma,
3427 size_t sizeof_headers) const
3428 {
3429 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3430 uint64_t hdr_lma = lma - sizeof_headers;
3431 hdr_lma &= ~(abi_pagesize - 1);
3432 return lma - hdr_lma;
3433 }
3434
3435 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3436 // the segment which should hold the file header and segment headers,
3437 // if any.
3438
3439 Output_segment*
3440 Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
3441 {
3442 gold_assert(this->saw_sections_clause_);
3443
3444 if (parameters->options().relocatable())
3445 return NULL;
3446
3447 if (this->saw_phdrs_clause())
3448 return create_segments_from_phdrs_clause(layout, dot_alignment);
3449
3450 Layout::Section_list sections;
3451 layout->get_allocated_sections(&sections);
3452
3453 // Sort the sections by address.
3454 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
3455
3456 this->create_note_and_tls_segments(layout, &sections);
3457
3458 // Walk through the sections adding them to PT_LOAD segments.
3459 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3460 Output_segment* first_seg = NULL;
3461 Output_segment* current_seg = NULL;
3462 bool is_current_seg_readonly = true;
3463 Layout::Section_list::iterator plast = sections.end();
3464 uint64_t last_vma = 0;
3465 uint64_t last_lma = 0;
3466 uint64_t last_size = 0;
3467 for (Layout::Section_list::iterator p = sections.begin();
3468 p != sections.end();
3469 ++p)
3470 {
3471 const uint64_t vma = (*p)->address();
3472 const uint64_t lma = ((*p)->has_load_address()
3473 ? (*p)->load_address()
3474 : vma);
3475 const uint64_t size = (*p)->current_data_size();
3476
3477 bool need_new_segment;
3478 if (current_seg == NULL)
3479 need_new_segment = true;
3480 else if (lma - vma != last_lma - last_vma)
3481 {
3482 // This section has a different LMA relationship than the
3483 // last one; we need a new segment.
3484 need_new_segment = true;
3485 }
3486 else if (align_address(last_lma + last_size, abi_pagesize)
3487 < align_address(lma, abi_pagesize))
3488 {
3489 // Putting this section in the segment would require
3490 // skipping a page.
3491 need_new_segment = true;
3492 }
3493 else if (is_bss_section(*plast) && !is_bss_section(*p))
3494 {
3495 // A non-BSS section can not follow a BSS section in the
3496 // same segment.
3497 need_new_segment = true;
3498 }
3499 else if (is_current_seg_readonly
3500 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3501 && !parameters->options().omagic())
3502 {
3503 // Don't put a writable section in the same segment as a
3504 // non-writable section.
3505 need_new_segment = true;
3506 }
3507 else
3508 {
3509 // Otherwise, reuse the existing segment.
3510 need_new_segment = false;
3511 }
3512
3513 elfcpp::Elf_Word seg_flags =
3514 Layout::section_flags_to_segment((*p)->flags());
3515
3516 if (need_new_segment)
3517 {
3518 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3519 seg_flags);
3520 current_seg->set_addresses(vma, lma);
3521 current_seg->set_minimum_p_align(dot_alignment);
3522 if (first_seg == NULL)
3523 first_seg = current_seg;
3524 is_current_seg_readonly = true;
3525 }
3526
3527 current_seg->add_output_section_to_load(layout, *p, seg_flags);
3528
3529 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3530 is_current_seg_readonly = false;
3531
3532 plast = p;
3533 last_vma = vma;
3534 last_lma = lma;
3535 last_size = size;
3536 }
3537
3538 // An ELF program should work even if the program headers are not in
3539 // a PT_LOAD segment. However, it appears that the Linux kernel
3540 // does not set the AT_PHDR auxiliary entry in that case. It sets
3541 // the load address to p_vaddr - p_offset of the first PT_LOAD
3542 // segment. It then sets AT_PHDR to the load address plus the
3543 // offset to the program headers, e_phoff in the file header. This
3544 // fails when the program headers appear in the file before the
3545 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3546 // segment to hold the file header and the program headers. This is
3547 // effectively what the GNU linker does, and it is slightly more
3548 // efficient in any case. We try to use the first PT_LOAD segment
3549 // if we can, otherwise we make a new one.
3550
3551 if (first_seg == NULL)
3552 return NULL;
3553
3554 // -n or -N mean that the program is not demand paged and there is
3555 // no need to put the program headers in a PT_LOAD segment.
3556 if (parameters->options().nmagic() || parameters->options().omagic())
3557 return NULL;
3558
3559 size_t sizeof_headers = this->total_header_size(layout);
3560
3561 uint64_t vma = first_seg->vaddr();
3562 uint64_t lma = first_seg->paddr();
3563
3564 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3565
3566 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3567 {
3568 first_seg->set_addresses(vma - subtract, lma - subtract);
3569 return first_seg;
3570 }
3571
3572 // If there is no room to squeeze in the headers, then punt. The
3573 // resulting executable probably won't run on GNU/Linux, but we
3574 // trust that the user knows what they are doing.
3575 if (lma < subtract || vma < subtract)
3576 return NULL;
3577
3578 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3579 elfcpp::PF_R);
3580 load_seg->set_addresses(vma - subtract, lma - subtract);
3581
3582 return load_seg;
3583 }
3584
3585 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3586 // segment if there are any SHT_TLS sections.
3587
3588 void
3589 Script_sections::create_note_and_tls_segments(
3590 Layout* layout,
3591 const Layout::Section_list* sections)
3592 {
3593 gold_assert(!this->saw_phdrs_clause());
3594
3595 bool saw_tls = false;
3596 for (Layout::Section_list::const_iterator p = sections->begin();
3597 p != sections->end();
3598 ++p)
3599 {
3600 if ((*p)->type() == elfcpp::SHT_NOTE)
3601 {
3602 elfcpp::Elf_Word seg_flags =
3603 Layout::section_flags_to_segment((*p)->flags());
3604 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3605 seg_flags);
3606 oseg->add_output_section_to_nonload(*p, seg_flags);
3607
3608 // Incorporate any subsequent SHT_NOTE sections, in the
3609 // hopes that the script is sensible.
3610 Layout::Section_list::const_iterator pnext = p + 1;
3611 while (pnext != sections->end()
3612 && (*pnext)->type() == elfcpp::SHT_NOTE)
3613 {
3614 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3615 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3616 p = pnext;
3617 ++pnext;
3618 }
3619 }
3620
3621 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3622 {
3623 if (saw_tls)
3624 gold_error(_("TLS sections are not adjacent"));
3625
3626 elfcpp::Elf_Word seg_flags =
3627 Layout::section_flags_to_segment((*p)->flags());
3628 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3629 seg_flags);
3630 oseg->add_output_section_to_nonload(*p, seg_flags);
3631
3632 Layout::Section_list::const_iterator pnext = p + 1;
3633 while (pnext != sections->end()
3634 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3635 {
3636 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3637 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3638 p = pnext;
3639 ++pnext;
3640 }
3641
3642 saw_tls = true;
3643 }
3644 }
3645 }
3646
3647 // Add a program header. The PHDRS clause is syntactically distinct
3648 // from the SECTIONS clause, but we implement it with the SECTIONS
3649 // support because PHDRS is useless if there is no SECTIONS clause.
3650
3651 void
3652 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3653 bool includes_filehdr, bool includes_phdrs,
3654 bool is_flags_valid, unsigned int flags,
3655 Expression* load_address)
3656 {
3657 if (this->phdrs_elements_ == NULL)
3658 this->phdrs_elements_ = new Phdrs_elements();
3659 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3660 includes_filehdr,
3661 includes_phdrs,
3662 is_flags_valid, flags,
3663 load_address));
3664 }
3665
3666 // Return the number of segments we expect to create based on the
3667 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3668
3669 size_t
3670 Script_sections::expected_segment_count(const Layout* layout) const
3671 {
3672 if (this->saw_phdrs_clause())
3673 return this->phdrs_elements_->size();
3674
3675 Layout::Section_list sections;
3676 layout->get_allocated_sections(&sections);
3677
3678 // We assume that we will need two PT_LOAD segments.
3679 size_t ret = 2;
3680
3681 bool saw_note = false;
3682 bool saw_tls = false;
3683 for (Layout::Section_list::const_iterator p = sections.begin();
3684 p != sections.end();
3685 ++p)
3686 {
3687 if ((*p)->type() == elfcpp::SHT_NOTE)
3688 {
3689 // Assume that all note sections will fit into a single
3690 // PT_NOTE segment.
3691 if (!saw_note)
3692 {
3693 ++ret;
3694 saw_note = true;
3695 }
3696 }
3697 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3698 {
3699 // There can only be one PT_TLS segment.
3700 if (!saw_tls)
3701 {
3702 ++ret;
3703 saw_tls = true;
3704 }
3705 }
3706 }
3707
3708 return ret;
3709 }
3710
3711 // Create the segments from a PHDRS clause. Return the segment which
3712 // should hold the file header and program headers, if any.
3713
3714 Output_segment*
3715 Script_sections::create_segments_from_phdrs_clause(Layout* layout,
3716 uint64_t dot_alignment)
3717 {
3718 this->attach_sections_using_phdrs_clause(layout);
3719 return this->set_phdrs_clause_addresses(layout, dot_alignment);
3720 }
3721
3722 // Create the segments from the PHDRS clause, and put the output
3723 // sections in them.
3724
3725 void
3726 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
3727 {
3728 typedef std::map<std::string, Output_segment*> Name_to_segment;
3729 Name_to_segment name_to_segment;
3730 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3731 p != this->phdrs_elements_->end();
3732 ++p)
3733 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
3734
3735 // Walk through the output sections and attach them to segments.
3736 // Output sections in the script which do not list segments are
3737 // attached to the same set of segments as the immediately preceding
3738 // output section.
3739
3740 String_list* phdr_names = NULL;
3741 bool load_segments_only = false;
3742 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3743 p != this->sections_elements_->end();
3744 ++p)
3745 {
3746 bool orphan;
3747 String_list* old_phdr_names = phdr_names;
3748 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &orphan);
3749 if (os == NULL)
3750 continue;
3751
3752 if (phdr_names == NULL)
3753 {
3754 gold_error(_("allocated section not in any segment"));
3755 continue;
3756 }
3757
3758 // We see a list of segments names. Disable PT_LOAD segment only
3759 // filtering.
3760 if (old_phdr_names != phdr_names)
3761 load_segments_only = false;
3762
3763 // If this is an orphan section--one that was not explicitly
3764 // mentioned in the linker script--then it should not inherit
3765 // any segment type other than PT_LOAD. Otherwise, e.g., the
3766 // PT_INTERP segment will pick up following orphan sections,
3767 // which does not make sense. If this is not an orphan section,
3768 // we trust the linker script.
3769 if (orphan)
3770 {
3771 // Enable PT_LOAD segments only filtering until we see another
3772 // list of segment names.
3773 load_segments_only = true;
3774 }
3775
3776 bool in_load_segment = false;
3777 for (String_list::const_iterator q = phdr_names->begin();
3778 q != phdr_names->end();
3779 ++q)
3780 {
3781 Name_to_segment::const_iterator r = name_to_segment.find(*q);
3782 if (r == name_to_segment.end())
3783 gold_error(_("no segment %s"), q->c_str());
3784 else
3785 {
3786 if (load_segments_only
3787 && r->second->type() != elfcpp::PT_LOAD)
3788 continue;
3789
3790 elfcpp::Elf_Word seg_flags =
3791 Layout::section_flags_to_segment(os->flags());
3792
3793 if (r->second->type() != elfcpp::PT_LOAD)
3794 r->second->add_output_section_to_nonload(os, seg_flags);
3795 else
3796 {
3797 r->second->add_output_section_to_load(layout, os, seg_flags);
3798 if (in_load_segment)
3799 gold_error(_("section in two PT_LOAD segments"));
3800 in_load_segment = true;
3801 }
3802 }
3803 }
3804
3805 if (!in_load_segment)
3806 gold_error(_("allocated section not in any PT_LOAD segment"));
3807 }
3808 }
3809
3810 // Set the addresses for segments created from a PHDRS clause. Return
3811 // the segment which should hold the file header and program headers,
3812 // if any.
3813
3814 Output_segment*
3815 Script_sections::set_phdrs_clause_addresses(Layout* layout,
3816 uint64_t dot_alignment)
3817 {
3818 Output_segment* load_seg = NULL;
3819 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3820 p != this->phdrs_elements_->end();
3821 ++p)
3822 {
3823 // Note that we have to set the flags after adding the output
3824 // sections to the segment, as adding an output segment can
3825 // change the flags.
3826 (*p)->set_flags_if_valid();
3827
3828 Output_segment* oseg = (*p)->segment();
3829
3830 if (oseg->type() != elfcpp::PT_LOAD)
3831 {
3832 // The addresses of non-PT_LOAD segments are set from the
3833 // PT_LOAD segments.
3834 if ((*p)->has_load_address())
3835 gold_error(_("may only specify load address for PT_LOAD segment"));
3836 continue;
3837 }
3838
3839 oseg->set_minimum_p_align(dot_alignment);
3840
3841 // The output sections should have addresses from the SECTIONS
3842 // clause. The addresses don't have to be in order, so find the
3843 // one with the lowest load address. Use that to set the
3844 // address of the segment.
3845
3846 Output_section* osec = oseg->section_with_lowest_load_address();
3847 if (osec == NULL)
3848 {
3849 oseg->set_addresses(0, 0);
3850 continue;
3851 }
3852
3853 uint64_t vma = osec->address();
3854 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
3855
3856 // Override the load address of the section with the load
3857 // address specified for the segment.
3858 if ((*p)->has_load_address())
3859 {
3860 if (osec->has_load_address())
3861 gold_warning(_("PHDRS load address overrides "
3862 "section %s load address"),
3863 osec->name());
3864
3865 lma = (*p)->load_address();
3866 }
3867
3868 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
3869 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
3870 {
3871 // We could support this if we wanted to.
3872 gold_error(_("using only one of FILEHDR and PHDRS is "
3873 "not currently supported"));
3874 }
3875 if (headers)
3876 {
3877 size_t sizeof_headers = this->total_header_size(layout);
3878 uint64_t subtract = this->header_size_adjustment(lma,
3879 sizeof_headers);
3880 if (lma >= subtract && vma >= subtract)
3881 {
3882 lma -= subtract;
3883 vma -= subtract;
3884 }
3885 else
3886 {
3887 gold_error(_("sections loaded on first page without room "
3888 "for file and program headers "
3889 "are not supported"));
3890 }
3891
3892 if (load_seg != NULL)
3893 gold_error(_("using FILEHDR and PHDRS on more than one "
3894 "PT_LOAD segment is not currently supported"));
3895 load_seg = oseg;
3896 }
3897
3898 oseg->set_addresses(vma, lma);
3899 }
3900
3901 return load_seg;
3902 }
3903
3904 // Add the file header and segment headers to non-load segments
3905 // specified in the PHDRS clause.
3906
3907 void
3908 Script_sections::put_headers_in_phdrs(Output_data* file_header,
3909 Output_data* segment_headers)
3910 {
3911 gold_assert(this->saw_phdrs_clause());
3912 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3913 p != this->phdrs_elements_->end();
3914 ++p)
3915 {
3916 if ((*p)->type() != elfcpp::PT_LOAD)
3917 {
3918 if ((*p)->includes_phdrs())
3919 (*p)->segment()->add_initial_output_data(segment_headers);
3920 if ((*p)->includes_filehdr())
3921 (*p)->segment()->add_initial_output_data(file_header);
3922 }
3923 }
3924 }
3925
3926 // Look for an output section by name and return the address, the load
3927 // address, the alignment, and the size. This is used when an
3928 // expression refers to an output section which was not actually
3929 // created. This returns true if the section was found, false
3930 // otherwise.
3931
3932 bool
3933 Script_sections::get_output_section_info(const char* name, uint64_t* address,
3934 uint64_t* load_address,
3935 uint64_t* addralign,
3936 uint64_t* size) const
3937 {
3938 if (!this->saw_sections_clause_)
3939 return false;
3940 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3941 p != this->sections_elements_->end();
3942 ++p)
3943 if ((*p)->get_output_section_info(name, address, load_address, addralign,
3944 size))
3945 return true;
3946 return false;
3947 }
3948
3949 // Release all Output_segments. This remove all pointers to all
3950 // Output_segments.
3951
3952 void
3953 Script_sections::release_segments()
3954 {
3955 if (this->saw_phdrs_clause())
3956 {
3957 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3958 p != this->phdrs_elements_->end();
3959 ++p)
3960 (*p)->release_segment();
3961 }
3962 }
3963
3964 // Print the SECTIONS clause to F for debugging.
3965
3966 void
3967 Script_sections::print(FILE* f) const
3968 {
3969 if (this->phdrs_elements_ != NULL)
3970 {
3971 fprintf(f, "PHDRS {\n");
3972 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3973 p != this->phdrs_elements_->end();
3974 ++p)
3975 (*p)->print(f);
3976 fprintf(f, "}\n");
3977 }
3978
3979 if (this->memory_regions_ != NULL)
3980 {
3981 fprintf(f, "MEMORY {\n");
3982 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3983 m != this->memory_regions_->end();
3984 ++m)
3985 (*m)->print(f);
3986 fprintf(f, "}\n");
3987 }
3988
3989 if (!this->saw_sections_clause_)
3990 return;
3991
3992 fprintf(f, "SECTIONS {\n");
3993
3994 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3995 p != this->sections_elements_->end();
3996 ++p)
3997 (*p)->print(f);
3998
3999 fprintf(f, "}\n");
4000 }
4001
4002 } // End namespace gold.
This page took 0.180305 seconds and 5 git commands to generate.