gas: blackfin: clarify some errors with register usage in insns
[binutils.git] / gold / script-sections.cc
blobeebbb5eb7ee022f05b665ed436ab048002f7d90f
1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cstring>
26 #include <algorithm>
27 #include <list>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <fnmatch.h>
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"
41 // Support for the SECTIONS clause in linker scripts.
43 namespace gold
46 // A region of memory.
47 class Memory_region
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_(),
59 lma_sections_()
60 { }
62 // Return the name of this region.
63 const std::string&
64 name() const
65 { return this->name_; }
67 // Return the start address of this region.
68 Expression*
69 start_address() const
70 { return this->start_; }
72 // Return the length of this region.
73 Expression*
74 length() const
75 { return this->length_; }
77 // Print the region (when debugging).
78 void
79 print(FILE*) const;
81 // Return true if <name,namelen> matches this region.
82 bool
83 name_match(const char* name, size_t namelen)
85 return (this->name_.length() == namelen
86 && strncmp(this->name_.c_str(), name, namelen) == 0);
89 Expression*
90 get_current_vma_address(void) const
92 return
93 script_exp_binary_add(this->start_,
94 script_exp_integer(this->current_vma_offset_));
97 Expression*
98 get_current_lma_address(void) const
100 return
101 script_exp_binary_add(this->start_,
102 script_exp_integer(this->current_lma_offset_));
105 void
106 increment_vma_offset(std::string section_name, uint64_t amount,
107 const Symbol_table* symtab, const Layout* layout)
109 this->current_vma_offset_ += amount;
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());
117 void
118 increment_lma_offset(std::string section_name, uint64_t amount,
119 const Symbol_table* symtab, const Layout* layout)
121 this->current_lma_offset_ += amount;
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());
129 void
130 add_section(Output_section_definition* sec, bool vma)
132 if (vma)
133 this->vma_sections_.push_back(sec);
134 else
135 this->lma_sections_.push_back(sec);
138 typedef std::vector<Output_section_definition*> Section_list;
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(); }
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(); }
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(); }
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(); }
164 private:
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_;
178 // Print a memory region.
180 void
181 Memory_region::print(FILE* f) const
183 fprintf(f, " %s", this->name_.c_str());
185 unsigned int attrs = this->attributes_;
186 if (attrs != 0)
188 fprintf(f, " (");
191 switch (attrs & - attrs)
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();
201 attrs &= ~ (attrs & - attrs);
203 while (attrs != 0);
204 fputc(')', f);
207 fprintf(f, " : origin = ");
208 this->start_->print(f);
209 fprintf(f, ", length = ");
210 this->length_->print(f);
211 fprintf(f, "\n");
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.
220 class Orphan_section_placement
222 private:
223 typedef Script_sections::Elements_iterator Elements_iterator;
225 public:
226 Orphan_section_placement();
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);
233 // Initialize the last location.
234 void
235 last_init(Elements_iterator location);
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);
243 // Return the iterator being used for sections at the very end of
244 // the linker script.
245 Elements_iterator
246 last_place() const;
248 private:
249 // The places that we specifically recognize. This list is copied
250 // from the GNU linker.
251 enum Place_index
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
266 // The information we keep for a specific place.
267 struct Place
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;
277 // Initialize one place element.
278 void
279 initialize_place(Place_index, const char*);
281 // The places.
282 Place places_[PLACE_MAX];
283 // True if this is the first call to output_section_init.
284 bool first_init_;
287 // Initialize Orphan_section_placement.
289 Orphan_section_placement::Orphan_section_placement()
290 : first_init_(true)
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);
304 // Initialize one place element.
306 void
307 Orphan_section_placement::initialize_place(Place_index index, const char* name)
309 this->places_[index].name = name;
310 this->places_[index].have_location = false;
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.
318 void
319 Orphan_section_placement::output_section_init(const std::string& name,
320 Output_section* os,
321 Elements_iterator location)
323 bool first_init = this->first_init_;
324 this->first_init_ = false;
326 for (int i = 0; i < PLACE_MAX; ++i)
328 if (this->places_[i].name != NULL && this->places_[i].name == name)
330 if (this->places_[i].have_location)
332 // We have already seen a section with this name.
333 return;
336 this->places_[i].location = location;
337 this->places_[i].have_location = true;
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;
345 return;
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)
355 this->places_[PLACE_REL].location = location;
356 this->places_[PLACE_REL].have_location = true;
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())))
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)
370 --location;
371 this->places_[PLACE_NONALLOC].location = location;
372 this->places_[PLACE_NONALLOC].have_location = true;
377 // Initialize the last location.
379 void
380 Orphan_section_placement::last_init(Elements_iterator location)
382 this->places_[PLACE_LAST].location = location;
383 this->places_[PLACE_LAST].have_location = true;
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.
390 bool
391 Orphan_section_placement::find_place(Output_section* os,
392 Elements_iterator** pwhere)
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)
409 if (type == elfcpp::SHT_NOBITS)
410 index = PLACE_TLS_BSS;
411 else
412 index = PLACE_TLS;
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;
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)
429 Place_index follow;
430 switch (index)
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;
456 if (follow != PLACE_MAX && this->places_[follow].have_location)
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;
467 *pwhere = &this->places_[index].location;
468 bool ret = this->places_[index].have_location;
470 // The caller will set the location.
471 this->places_[index].have_location = true;
473 return ret;
476 // Return the iterator being used for sections at the very end of the
477 // linker script.
479 Orphan_section_placement::Elements_iterator
480 Orphan_section_placement::last_place() const
482 gold_assert(this->places_[PLACE_LAST].have_location);
483 return this->places_[PLACE_LAST].location;
486 // An element in a SECTIONS clause.
488 class Sections_element
490 public:
491 Sections_element()
494 virtual ~Sections_element()
497 // Return whether an output section is relro.
498 virtual bool
499 is_relro() const
500 { return false; }
502 // Record that an output section is relro.
503 virtual void
504 set_is_relro()
507 // Create any required output sections. The only real
508 // implementation is in Output_section_definition.
509 virtual void
510 create_sections(Layout*)
513 // Add any symbol being defined to the symbol table.
514 virtual void
515 add_symbols_to_table(Symbol_table*)
518 // Finalize symbols and check assertions.
519 virtual void
520 finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
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; }
531 // Initialize OSP with an output section.
532 virtual void
533 orphan_section_init(Orphan_section_placement*,
534 Script_sections::Elements_iterator)
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*)
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; }
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; }
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; }
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; }
581 // Return the associated Output_section if there is one.
582 virtual Output_section*
583 get_output_section() const
584 { return NULL; }
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")); }
591 // Print the element for debugging purposes.
592 virtual void
593 print(FILE* f) const = 0;
596 // An assignment in a SECTIONS clause outside of an output section.
598 class Sections_element_assignment : public Sections_element
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)
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); }
611 // Finalize the symbol.
612 void
613 finalize_symbols(Symbol_table* symtab, const Layout* layout,
614 uint64_t* dot_value)
616 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
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*)
626 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
629 // Print for debugging.
630 void
631 print(FILE* f) const
633 fprintf(f, " ");
634 this->assignment_.print(f);
637 private:
638 Symbol_assignment assignment_;
641 // An assignment to the dot symbol in a SECTIONS clause outside of an
642 // output section.
644 class Sections_element_dot_assignment : public Sections_element
646 public:
647 Sections_element_dot_assignment(Expression* val)
648 : val_(val)
651 // Finalize the symbol.
652 void
653 finalize_symbols(Symbol_table* symtab, const Layout* layout,
654 uint64_t* dot_value)
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);
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)
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;
676 // Print for debugging.
677 void
678 print(FILE* f) const
680 fprintf(f, " . = ");
681 this->val_->print(f);
682 fprintf(f, "\n");
685 private:
686 Expression* val_;
689 // An assertion in a SECTIONS clause outside of an output section.
691 class Sections_element_assertion : public Sections_element
693 public:
694 Sections_element_assertion(Expression* check, const char* message,
695 size_t messagelen)
696 : assertion_(check, message, messagelen)
699 // Check the assertion.
700 void
701 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
702 { this->assertion_.check(symtab, layout); }
704 // Print for debugging.
705 void
706 print(FILE* f) const
708 fprintf(f, " ");
709 this->assertion_.print(f);
712 private:
713 Script_assertion assertion_;
716 // An element in an output section in a SECTIONS clause.
718 class Output_section_element
720 public:
721 // A list of input sections.
722 typedef std::list<Output_section::Input_section> Input_section_list;
724 Output_section_element()
727 virtual ~Output_section_element()
730 // Return whether this element requires an output section to exist.
731 virtual bool
732 needs_output_section() const
733 { return false; }
735 // Add any symbol being defined to the symbol table.
736 virtual void
737 add_symbols_to_table(Symbol_table*)
740 // Finalize symbols and check assertions.
741 virtual void
742 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
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; }
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*)
759 // Print the element for debugging purposes.
760 virtual void
761 print(FILE* f) const = 0;
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;
770 std::string
771 Output_section_element::get_fill_string(const std::string* fill,
772 section_size_type length) const
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;
783 // A symbol assignment in an output section.
785 class Output_section_element_assignment : public Output_section_element
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)
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); }
799 // Finalize the symbol.
800 void
801 finalize_symbols(Symbol_table* symtab, const Layout* layout,
802 uint64_t* dot_value, Output_section** dot_section)
804 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
805 *dot_section);
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*)
816 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
819 // Print for debugging.
820 void
821 print(FILE* f) const
823 fprintf(f, " ");
824 this->assignment_.print(f);
827 private:
828 Symbol_assignment assignment_;
831 // An assignment to the dot symbol in an output section.
833 class Output_section_element_dot_assignment : public Output_section_element
835 public:
836 Output_section_element_dot_assignment(Expression* val)
837 : val_(val)
840 // Finalize the symbol.
841 void
842 finalize_symbols(Symbol_table* symtab, const Layout* layout,
843 uint64_t* dot_value, Output_section** dot_section)
845 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
846 *dot_section, dot_section, NULL);
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*);
855 // Print for debugging.
856 void
857 print(FILE* f) const
859 fprintf(f, " . = ");
860 this->val_->print(f);
861 fprintf(f, "\n");
864 private:
865 Expression* val_;
868 // Update the dot symbol while setting section addresses.
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*)
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)
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
896 std::string this_fill = this->get_fill_string(fill, length);
897 posd = new Output_data_const(this_fill, 0);
899 output_section->add_output_section_data(posd);
900 layout->new_output_section_data_from_script(posd);
902 *dot_value = next_dot;
905 // An assertion in an output section.
907 class Output_section_element_assertion : public Output_section_element
909 public:
910 Output_section_element_assertion(Expression* check, const char* message,
911 size_t messagelen)
912 : assertion_(check, message, messagelen)
915 void
916 print(FILE* f) const
918 fprintf(f, " ");
919 this->assertion_.print(f);
922 private:
923 Script_assertion assertion_;
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.
930 class Output_data_expression : public Output_section_data
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)
941 protected:
942 // Write the data to the output file.
943 void
944 do_write(Output_file*);
946 // Write the data to a buffer.
947 void
948 do_write_to_buffer(unsigned char*);
950 // Write to a map file.
951 void
952 do_print_to_mapfile(Mapfile* mapfile) const
953 { mapfile->print_output_data(this, _("** expression")); }
955 private:
956 template<bool big_endian>
957 void
958 endian_write_to_buffer(uint64_t, unsigned char*);
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_;
968 // Write the data element to the output file.
970 void
971 Output_data_expression::do_write(Output_file* of)
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);
978 // Write the data element to a buffer.
980 void
981 Output_data_expression::do_write_to_buffer(unsigned char* buf)
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);
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);
994 template<bool big_endian>
995 void
996 Output_data_expression::endian_write_to_buffer(uint64_t val,
997 unsigned char* buf)
999 switch (this->data_size())
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)
1013 val &= 0xffffffff;
1014 if (this->is_signed_ && (val & 0x80000000) != 0)
1015 val |= 0xffffffff00000000LL;
1017 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
1018 break;
1019 default:
1020 gold_unreachable();
1024 // A data item in an output section.
1026 class Output_section_element_data : public Output_section_element
1028 public:
1029 Output_section_element_data(int size, bool is_signed, Expression* val)
1030 : size_(size), is_signed_(is_signed), val_(val)
1033 // If there is a data item, then we must create an output section.
1034 bool
1035 needs_output_section() const
1036 { return true; }
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_; }
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*);
1050 // Print for debugging.
1051 void
1052 print(FILE*) const;
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_;
1063 // Store the value in the section.
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*)
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_;
1086 // Print for debugging.
1088 void
1089 Output_section_element_data::print(FILE* f) const
1091 const char* s;
1092 switch (this->size_)
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();
1112 fprintf(f, " %s(", s);
1113 this->val_->print(f);
1114 fprintf(f, ")\n");
1117 // A fill value setting in an output section.
1119 class Output_section_element_fill : public Output_section_element
1121 public:
1122 Output_section_element_fill(Expression* val)
1123 : val_(val)
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*)
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);
1145 // Print for debugging.
1146 void
1147 print(FILE* f) const
1149 fprintf(f, " FILL(");
1150 this->val_->print(f);
1151 fprintf(f, ")\n");
1154 private:
1155 // The new fill value.
1156 Expression* val_;
1159 // An input section specification in an output section
1161 class Output_section_element_input : public Output_section_element
1163 public:
1164 Output_section_element_input(const Input_section_spec* spec, bool keep);
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)
1171 *dot_value = this->final_dot_value_;
1172 *dot_section = this->final_dot_section_;
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;
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*);
1187 // Print for debugging.
1188 void
1189 print(FILE* f) const;
1191 private:
1192 // An input section pattern.
1193 struct Input_section_pattern
1195 std::string pattern;
1196 bool pattern_is_wildcard;
1197 Sort_wildcard sort;
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)
1207 typedef std::vector<Input_section_pattern> Input_section_patterns;
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;
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)
1218 return (is_wildcard_pattern
1219 ? fnmatch(pattern, string, 0) == 0
1220 : strcmp(string, pattern) == 0);
1223 // See if we match a file name.
1224 bool
1225 match_file_name(const char* file_name) const;
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_;
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.
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)
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());
1271 if (spec->input_sections.exclude != NULL)
1273 for (String_list::const_iterator p =
1274 spec->input_sections.exclude->begin();
1275 p != spec->input_sections.exclude->end();
1276 ++p)
1278 bool is_wildcard = is_wildcard_string((*p).c_str());
1279 this->filename_exclusions_.push_back(std::make_pair(*p,
1280 is_wildcard));
1284 if (spec->input_sections.sections != NULL)
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));
1296 // See whether we match FILE_NAME.
1298 bool
1299 Output_section_element_input::match_file_name(const char* file_name) const
1301 if (!this->filename_pattern_.empty())
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;
1308 if (!match(file_name, this->filename_pattern_.c_str(),
1309 this->filename_is_wildcard_))
1310 return false;
1313 if (file_name != NULL)
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)
1322 if (match(file_name, p->first.c_str(), p->second))
1323 return false;
1327 return true;
1330 // See whether we match FILE_NAME and SECTION_NAME.
1332 bool
1333 Output_section_element_input::match_name(const char* file_name,
1334 const char* section_name) const
1336 if (!this->match_file_name(file_name))
1337 return false;
1339 // If there are no section name patterns, then we match.
1340 if (this->input_section_patterns_.empty())
1341 return true;
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)
1349 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1350 return true;
1353 // We didn't match any section names, so we didn't match.
1354 return false;
1357 // Information we use to sort the input sections.
1359 class Input_section_info
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)
1367 // Return the simple input section.
1368 const Output_section::Input_section&
1369 input_section() const
1370 { return this->input_section_; }
1372 // Return the object.
1373 Relobj*
1374 relobj() const
1375 { return this->input_section_.relobj(); }
1377 // Return the section index.
1378 unsigned int
1379 shndx()
1380 { return this->input_section_.shndx(); }
1382 // Return the section name.
1383 const std::string&
1384 section_name() const
1385 { return this->section_name_; }
1387 // Set the section name.
1388 void
1389 set_section_name(const std::string name)
1390 { this->section_name_ = name; }
1392 // Return the section size.
1393 uint64_t
1394 size() const
1395 { return this->size_; }
1397 // Set the section size.
1398 void
1399 set_size(uint64_t size)
1400 { this->size_ = size; }
1402 // Return the address alignment.
1403 uint64_t
1404 addralign() const
1405 { return this->addralign_; }
1407 // Set the address alignment.
1408 void
1409 set_addralign(uint64_t addralign)
1410 { this->addralign_ = addralign; }
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_;
1423 // A class to sort the input sections.
1425 class Input_section_sorter
1427 public:
1428 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1429 : filename_sort_(filename_sort), section_sort_(section_sort)
1432 bool
1433 operator()(const Input_section_info&, const Input_section_info&) const;
1435 private:
1436 Sort_wildcard filename_sort_;
1437 Sort_wildcard section_sort_;
1440 bool
1441 Input_section_sorter::operator()(const Input_section_info& isi1,
1442 const Input_section_info& isi2) const
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()))
1449 if (isi1.section_name() != isi2.section_name())
1450 return isi1.section_name() < isi2.section_name();
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)
1456 if (isi1.addralign() != isi2.addralign())
1457 return isi1.addralign() < isi2.addralign();
1459 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1461 if (isi1.relobj()->name() != isi2.relobj()->name())
1462 return (isi1.relobj()->name() < isi2.relobj()->name());
1465 // Otherwise we leave them in the same order.
1466 return false;
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.
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)
1485 // We build a list of sections which match each
1486 // Input_section_pattern.
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);
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.
1498 Input_section_list::iterator p = input_sections->begin();
1499 while (p != input_sections->end())
1501 Relobj* relobj = p->relobj();
1502 unsigned int shndx = p->shndx();
1503 Input_section_info isi(*p);
1505 // Calling section_name and section_addralign is not very
1506 // efficient.
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.
1512 const Task* task = reinterpret_cast<const Task*>(-1);
1513 Task_lock_obj<Object> tl(task, relobj);
1515 isi.set_section_name(relobj->section_name(shndx));
1516 if (p->is_relaxed_input_section())
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());
1523 else
1525 isi.set_size(relobj->section_size(shndx));
1526 isi.set_addralign(relobj->section_addralign(shndx));
1530 if (!this->match_file_name(relobj->name().c_str()))
1531 ++p;
1532 else if (this->input_section_patterns_.empty())
1534 matching_sections[0].push_back(isi);
1535 p = input_sections->erase(p);
1537 else
1539 size_t i;
1540 for (i = 0; i < input_pattern_count; ++i)
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;
1549 if (i >= this->input_section_patterns_.size())
1550 ++p;
1551 else
1553 matching_sections[i].push_back(isi);
1554 p = input_sections->erase(p);
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.
1564 uint64_t dot = *dot_value;
1565 for (size_t i = 0; i < input_pattern_count; ++i)
1567 if (matching_sections[i].empty())
1568 continue;
1570 gold_assert(output_section != NULL);
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));
1580 for (std::vector<Input_section_info>::const_iterator p =
1581 matching_sections[i].begin();
1582 p != matching_sections[i].end();
1583 ++p)
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());
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)
1596 this_subalign = subalign;
1597 sis.set_addralign(subalign);
1600 uint64_t address = align_address(dot, this_subalign);
1602 if (address > dot && !fill->empty())
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);
1612 output_section->add_script_input_section(sis);
1613 dot = address + data_size;
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;
1624 this->final_dot_value_ = *dot_value;
1625 this->final_dot_section_ = *dot_section;
1628 // Print for debugging.
1630 void
1631 Output_section_element_input::print(FILE* f) const
1633 fprintf(f, " ");
1635 if (this->keep_)
1636 fprintf(f, "KEEP(");
1638 if (!this->filename_pattern_.empty())
1640 bool need_close_paren = false;
1641 switch (this->filename_sort_)
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();
1653 fprintf(f, "%s", this->filename_pattern_.c_str());
1655 if (need_close_paren)
1656 fprintf(f, ")");
1659 if (!this->input_section_patterns_.empty()
1660 || !this->filename_exclusions_.empty())
1662 fprintf(f, "(");
1664 bool need_space = false;
1665 if (!this->filename_exclusions_.empty())
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)
1674 if (need_comma)
1675 fprintf(f, ", ");
1676 fprintf(f, "%s", p->first.c_str());
1677 need_comma = true;
1679 fprintf(f, ")");
1680 need_space = true;
1683 for (Input_section_patterns::const_iterator p =
1684 this->input_section_patterns_.begin();
1685 p != this->input_section_patterns_.end();
1686 ++p)
1688 if (need_space)
1689 fprintf(f, " ");
1691 int close_parens = 0;
1692 switch (p->sort)
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();
1716 fprintf(f, "%s", p->pattern.c_str());
1718 for (int i = 0; i < close_parens; ++i)
1719 fprintf(f, ")");
1721 need_space = true;
1724 fprintf(f, ")");
1727 if (this->keep_)
1728 fprintf(f, ")");
1730 fprintf(f, "\n");
1733 // An output section.
1735 class Output_section_definition : public Sections_element
1737 public:
1738 typedef Output_section_element::Input_section_list Input_section_list;
1740 Output_section_definition(const char* name, size_t namelen,
1741 const Parser_output_section_header* header);
1743 // Finish the output section with the information in the trailer.
1744 void
1745 finish(const Parser_output_section_trailer* trailer);
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);
1752 // Add an assignment to the special dot symbol.
1753 void
1754 add_dot_assignment(Expression* value);
1756 // Add an assertion.
1757 void
1758 add_assertion(Expression* check, const char* message, size_t messagelen);
1760 // Add a data item to the current output section.
1761 void
1762 add_data(int size, bool is_signed, Expression* val);
1764 // Add a setting for the fill value.
1765 void
1766 add_fill(Expression* val);
1768 // Add an input section specification.
1769 void
1770 add_input_section(const Input_section_spec* spec, bool keep);
1772 // Return whether the output section is relro.
1773 bool
1774 is_relro() const
1775 { return this->is_relro_; }
1777 // Record that the output section is relro.
1778 void
1779 set_is_relro()
1780 { this->is_relro_ = true; }
1782 // Create any required output sections.
1783 void
1784 create_sections(Layout*);
1786 // Add any symbols being defined to the symbol table.
1787 void
1788 add_symbols_to_table(Symbol_table* symtab);
1790 // Finalize symbols and check assertions.
1791 void
1792 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
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*);
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); }
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);
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);
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);
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);
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;
1838 // Return the associated Output_section if there is one.
1839 Output_section*
1840 get_output_section() const
1841 { return this->output_section_; }
1843 // Print the contents to the FILE. This is for debugging.
1844 void
1845 print(FILE*) const;
1847 // Return the output section type if specified or Script_sections::ST_NONE.
1848 Script_sections::Section_type
1849 section_type() const;
1851 // Store the memory region to use.
1852 void
1853 set_memory_region(Memory_region*, bool set_vma);
1855 void
1856 set_section_vma(Expression* address)
1857 { this->address_ = address; }
1859 void
1860 set_section_lma(Expression* address)
1861 { this->load_address_ = address; }
1863 std::string
1864 get_section_name(void) const
1865 { return this->name_; }
1867 private:
1868 static const char*
1869 script_section_type_name(Script_section_type);
1871 typedef std::vector<Output_section_element*> Output_section_elements;
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_;
1907 // Constructor.
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)
1931 // Finish an output section.
1933 void
1934 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1936 this->fill_ = trailer->fill;
1937 this->phdrs_ = trailer->phdrs;
1940 // Add a symbol to be defined.
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)
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);
1957 // Add an assignment to the special dot symbol.
1959 void
1960 Output_section_definition::add_dot_assignment(Expression* value)
1962 Output_section_element* p = new Output_section_element_dot_assignment(value);
1963 this->elements_.push_back(p);
1966 // Add an assertion.
1968 void
1969 Output_section_definition::add_assertion(Expression* check,
1970 const char* message,
1971 size_t messagelen)
1973 Output_section_element* p = new Output_section_element_assertion(check,
1974 message,
1975 messagelen);
1976 this->elements_.push_back(p);
1979 // Add a data item to the current output section.
1981 void
1982 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1984 Output_section_element* p = new Output_section_element_data(size, is_signed,
1985 val);
1986 this->elements_.push_back(p);
1989 // Add a setting for the fill value.
1991 void
1992 Output_section_definition::add_fill(Expression* val)
1994 Output_section_element* p = new Output_section_element_fill(val);
1995 this->elements_.push_back(p);
1998 // Add an input section specification.
2000 void
2001 Output_section_definition::add_input_section(const Input_section_spec* spec,
2002 bool keep)
2004 Output_section_element* p = new Output_section_element_input(spec, keep);
2005 this->elements_.push_back(p);
2008 // Create any required output sections. We need an output section if
2009 // there is a data statement here.
2011 void
2012 Output_section_definition::create_sections(Layout* layout)
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)
2020 if ((*p)->needs_output_section())
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;
2030 // Add any symbols being defined to the symbol table.
2032 void
2033 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
2035 for (Output_section_elements::iterator p = this->elements_.begin();
2036 p != this->elements_.end();
2037 ++p)
2038 (*p)->add_symbols_to_table(symtab);
2041 // Finalize symbols and check assertions.
2043 void
2044 Output_section_definition::finalize_symbols(Symbol_table* symtab,
2045 const Layout* layout,
2046 uint64_t* dot_value)
2048 if (this->output_section_ != NULL)
2049 *dot_value = this->output_section_->address();
2050 else
2052 uint64_t address = *dot_value;
2053 if (this->address_ != NULL)
2055 Output_section* dummy;
2056 address = this->address_->eval_with_dot(symtab, layout, true,
2057 *dot_value, NULL,
2058 &dummy, NULL);
2060 if (this->align_ != NULL)
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);
2069 *dot_value = address;
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);
2079 // Return the output section name to use for an input section name.
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)
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)
2093 if ((*p)->match_name(file_name, section_name))
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();
2103 // We don't know about this section name.
2104 return NULL;
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.
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)
2118 uint64_t address;
2119 uint64_t old_dot_value = *dot_value;
2120 uint64_t old_load_address = *load_address;
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)
2130 if (this->address_ == NULL)
2131 address = *dot_value;
2132 else
2134 Output_section* dummy;
2135 address = this->address_->eval_with_dot(symtab, layout, true,
2136 *dot_value, NULL, &dummy,
2137 dot_alignment);
2141 uint64_t align;
2142 if (this->align_ == NULL)
2144 if (this->output_section_ == NULL)
2145 align = 0;
2146 else
2147 align = this->output_section_->addralign();
2149 else
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);
2161 address = align_address(address, align);
2163 uint64_t start_address = address;
2165 *dot_value = address;
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);
2174 this->evaluated_address_ = address;
2175 this->evaluated_addralign_ = align;
2177 if (this->load_address_ == NULL)
2178 this->evaluated_load_address_ = address;
2179 else
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;
2191 uint64_t subalign;
2192 if (this->subalign_ == NULL)
2193 subalign = 0;
2194 else
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());
2205 std::string fill;
2206 if (this->fill_ != NULL)
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);
2223 Input_section_list input_sections;
2224 if (this->output_section_ != NULL)
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;
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);
2243 gold_assert(input_sections.empty());
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));
2251 if (this->output_section_ != NULL)
2253 if (this->is_relro_)
2254 this->output_section_->set_is_relro();
2255 else
2256 this->output_section_->clear_is_relro();
2258 // If this is a NOLOAD section, keep dot and load address unchanged.
2259 if (this->output_section_->is_noload())
2261 *dot_value = old_dot_value;
2262 *load_address = old_load_address;
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.
2271 Section_constraint
2272 Output_section_definition::check_constraint(Output_section_definition** posd)
2274 switch (this->constraint_)
2276 case CONSTRAINT_NONE:
2277 return CONSTRAINT_NONE;
2279 case CONSTRAINT_ONLY_IF_RO:
2280 if (this->output_section_ != NULL
2281 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2283 *posd = this;
2284 return CONSTRAINT_ONLY_IF_RO;
2286 return CONSTRAINT_NONE;
2288 case CONSTRAINT_ONLY_IF_RW:
2289 if (this->output_section_ != NULL
2290 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2292 *posd = this;
2293 return CONSTRAINT_ONLY_IF_RW;
2295 return CONSTRAINT_NONE;
2297 case CONSTRAINT_SPECIAL:
2298 if (this->output_section_ != NULL)
2299 gold_error(_("SPECIAL constraints are not implemented"));
2300 return CONSTRAINT_NONE;
2302 default:
2303 gold_unreachable();
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.
2311 bool
2312 Output_section_definition::alternate_constraint(
2313 Output_section_definition* posd,
2314 Section_constraint constraint)
2316 if (this->name_ != posd->name_)
2317 return false;
2319 switch (constraint)
2321 case CONSTRAINT_ONLY_IF_RO:
2322 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2323 return false;
2324 break;
2326 case CONSTRAINT_ONLY_IF_RW:
2327 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2328 return false;
2329 break;
2331 default:
2332 gold_unreachable();
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.
2340 if (this->output_section_ != NULL)
2341 gold_error(_("mismatched definition for constrained sections"));
2343 this->output_section_ = posd->output_section_;
2344 posd->output_section_ = NULL;
2346 if (this->is_relro_)
2347 this->output_section_->set_is_relro();
2348 else
2349 this->output_section_->clear_is_relro();
2351 return true;
2354 // Get the list of segments to use for an allocated section when using
2355 // a PHDRS clause.
2357 Output_section*
2358 Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2359 bool* orphan)
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_;
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_;
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.
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
2387 if (this->name_ != name)
2388 return false;
2390 if (this->output_section_ != NULL)
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();
2400 else
2402 *address = this->evaluated_address_;
2403 *load_address = this->evaluated_load_address_;
2404 *addralign = this->evaluated_addralign_;
2405 *size = 0;
2408 return true;
2411 // Print for debugging.
2413 void
2414 Output_section_definition::print(FILE* f) const
2416 fprintf(f, " %s ", this->name_.c_str());
2418 if (this->address_ != NULL)
2420 this->address_->print(f);
2421 fprintf(f, " ");
2424 if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2425 fprintf(f, "(%s) ",
2426 this->script_section_type_name(this->script_section_type_));
2428 fprintf(f, ": ");
2430 if (this->load_address_ != NULL)
2432 fprintf(f, "AT(");
2433 this->load_address_->print(f);
2434 fprintf(f, ") ");
2437 if (this->align_ != NULL)
2439 fprintf(f, "ALIGN(");
2440 this->align_->print(f);
2441 fprintf(f, ") ");
2444 if (this->subalign_ != NULL)
2446 fprintf(f, "SUBALIGN(");
2447 this->subalign_->print(f);
2448 fprintf(f, ") ");
2451 fprintf(f, "{\n");
2453 for (Output_section_elements::const_iterator p = this->elements_.begin();
2454 p != this->elements_.end();
2455 ++p)
2456 (*p)->print(f);
2458 fprintf(f, " }");
2460 if (this->fill_ != NULL)
2462 fprintf(f, " = ");
2463 this->fill_->print(f);
2466 if (this->phdrs_ != NULL)
2468 for (String_list::const_iterator p = this->phdrs_->begin();
2469 p != this->phdrs_->end();
2470 ++p)
2471 fprintf(f, " :%s", p->c_str());
2474 fprintf(f, "\n");
2477 Script_sections::Section_type
2478 Output_section_definition::section_type() const
2480 switch (this->script_section_type_)
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();
2498 // Return the name of a script section type.
2500 const char*
2501 Output_section_definition::script_section_type_name(
2502 Script_section_type script_section_type)
2504 switch (script_section_type)
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();
2523 void
2524 Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
2526 gold_assert(mr != NULL);
2527 // Add the current section to the specified region's list.
2528 mr->add_section(this, set_vma);
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.
2536 class Orphan_output_section : public Sections_element
2538 public:
2539 Orphan_output_section(Output_section* os)
2540 : os_(os)
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(); }
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(); }
2557 // Set section addresses.
2558 void
2559 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2560 uint64_t*);
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*);
2567 // Return the associated Output_section.
2568 Output_section*
2569 get_output_section() const
2570 { return this->os_; }
2572 // Print for debugging.
2573 void
2574 print(FILE* f) const
2576 fprintf(f, " marker for orphaned output section %s\n",
2577 this->os_->name());
2580 private:
2581 Output_section* os_;
2584 // Set section addresses.
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)
2592 typedef std::list<Output_section::Input_section> Input_section_list;
2594 bool have_load_address = *load_address != *dot_value;
2596 uint64_t address = *dot_value;
2597 address = align_address(address, this->os_->addralign());
2599 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
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()));
2607 Input_section_list input_sections;
2608 address += this->os_->get_input_sections(address, "", &input_sections);
2610 for (Input_section_list::iterator p = input_sections.begin();
2611 p != input_sections.end();
2612 ++p)
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;
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)
2628 if (!have_load_address)
2629 *load_address = address;
2630 else
2631 *load_address += address - *dot_value;
2633 *dot_value = address;
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.
2641 Output_section*
2642 Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
2644 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2645 return NULL;
2646 *orphan = true;
2647 return this->os_;
2650 // Class Phdrs_element. A program header from a PHDRS clause.
2652 class Phdrs_element
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)
2665 // Return the name of this segment.
2666 const std::string&
2667 name() const
2668 { return this->name_; }
2670 // Return the type of the segment.
2671 unsigned int
2672 type() const
2673 { return this->type_; }
2675 // Whether to include the file header.
2676 bool
2677 includes_filehdr() const
2678 { return this->includes_filehdr_; }
2680 // Whether to include the program headers.
2681 bool
2682 includes_phdrs() const
2683 { return this->includes_phdrs_; }
2685 // Return whether there is a load address.
2686 bool
2687 has_load_address() const
2688 { return this->load_address_ != NULL; }
2690 // Evaluate the load address expression if there is one.
2691 void
2692 eval_load_address(Symbol_table* symtab, Layout* layout)
2694 if (this->load_address_ != NULL)
2695 this->load_address_value_ = this->load_address_->eval(symtab, layout,
2696 true);
2699 // Return the load address.
2700 uint64_t
2701 load_address() const
2703 gold_assert(this->load_address_ != NULL);
2704 return this->load_address_value_;
2707 // Create the segment.
2708 Output_segment*
2709 create_segment(Layout* layout)
2711 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2712 return this->segment_;
2715 // Return the segment.
2716 Output_segment*
2717 segment()
2718 { return this->segment_; }
2720 // Release the segment.
2721 void
2722 release_segment()
2723 { this->segment_ = NULL; }
2725 // Set the segment flags if appropriate.
2726 void
2727 set_flags_if_valid()
2729 if (this->is_flags_valid_)
2730 this->segment_->set_flags(this->flags_);
2733 // Print for debugging.
2734 void
2735 print(FILE*) const;
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_;
2759 // Print for debugging.
2761 void
2762 Phdrs_element::print(FILE* f) const
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)
2773 fprintf(f, " AT(");
2774 this->load_address_->print(f);
2775 fprintf(f, ")");
2777 fprintf(f, ";\n");
2780 // Add a memory region.
2782 void
2783 Script_sections::add_memory_region(const char* name, size_t namelen,
2784 unsigned int attributes,
2785 Expression* start, Expression* length)
2787 if (this->memory_regions_ == NULL)
2788 this->memory_regions_ = new Memory_regions();
2789 else if (this->find_memory_region(name, namelen))
2791 gold_error (_("region '%.*s' already defined"), static_cast<int>(namelen),
2792 name);
2793 // FIXME: Add a GOLD extension to allow multiple regions with the same
2794 // name. This would amount to a single region covering disjoint blocks
2795 // of memory, which is useful for embedded devices.
2798 // FIXME: Check the length and start values. Currently we allow
2799 // non-constant expressions for these values, whereas LD does not.
2801 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
2802 // describe a region that packs from the end address going down, rather
2803 // than the start address going up. This would be useful for embedded
2804 // devices.
2806 this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
2807 start, length));
2810 // Find a memory region.
2812 Memory_region*
2813 Script_sections::find_memory_region(const char* name, size_t namelen)
2815 if (this->memory_regions_ == NULL)
2816 return NULL;
2818 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
2819 m != this->memory_regions_->end();
2820 ++m)
2821 if ((*m)->name_match(name, namelen))
2822 return *m;
2824 return NULL;
2827 // Find a memory region's origin.
2829 Expression*
2830 Script_sections::find_memory_region_origin(const char* name, size_t namelen)
2832 Memory_region* mr = find_memory_region(name, namelen);
2833 if (mr == NULL)
2834 return NULL;
2836 return mr->start_address();
2839 // Find a memory region's length.
2841 Expression*
2842 Script_sections::find_memory_region_length(const char* name, size_t namelen)
2844 Memory_region* mr = find_memory_region(name, namelen);
2845 if (mr == NULL)
2846 return NULL;
2848 return mr->length();
2851 // Set the memory region to use for the current section.
2853 void
2854 Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
2856 gold_assert(!this->sections_elements_->empty());
2857 this->sections_elements_->back()->set_memory_region(mr, set_vma);
2860 // Class Script_sections.
2862 Script_sections::Script_sections()
2863 : saw_sections_clause_(false),
2864 in_sections_clause_(false),
2865 sections_elements_(NULL),
2866 output_section_(NULL),
2867 memory_regions_(NULL),
2868 phdrs_elements_(NULL),
2869 orphan_section_placement_(NULL),
2870 data_segment_align_start_(),
2871 saw_data_segment_align_(false),
2872 saw_relro_end_(false),
2873 saw_segment_start_expression_(false)
2877 // Start a SECTIONS clause.
2879 void
2880 Script_sections::start_sections()
2882 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2883 this->saw_sections_clause_ = true;
2884 this->in_sections_clause_ = true;
2885 if (this->sections_elements_ == NULL)
2886 this->sections_elements_ = new Sections_elements;
2889 // Finish a SECTIONS clause.
2891 void
2892 Script_sections::finish_sections()
2894 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2895 this->in_sections_clause_ = false;
2898 // Add a symbol to be defined.
2900 void
2901 Script_sections::add_symbol_assignment(const char* name, size_t length,
2902 Expression* val, bool provide,
2903 bool hidden)
2905 if (this->output_section_ != NULL)
2906 this->output_section_->add_symbol_assignment(name, length, val,
2907 provide, hidden);
2908 else
2910 Sections_element* p = new Sections_element_assignment(name, length,
2911 val, provide,
2912 hidden);
2913 this->sections_elements_->push_back(p);
2917 // Add an assignment to the special dot symbol.
2919 void
2920 Script_sections::add_dot_assignment(Expression* val)
2922 if (this->output_section_ != NULL)
2923 this->output_section_->add_dot_assignment(val);
2924 else
2926 // The GNU linker permits assignments to . to appears outside of
2927 // a SECTIONS clause, and treats it as appearing inside, so
2928 // sections_elements_ may be NULL here.
2929 if (this->sections_elements_ == NULL)
2931 this->sections_elements_ = new Sections_elements;
2932 this->saw_sections_clause_ = true;
2935 Sections_element* p = new Sections_element_dot_assignment(val);
2936 this->sections_elements_->push_back(p);
2940 // Add an assertion.
2942 void
2943 Script_sections::add_assertion(Expression* check, const char* message,
2944 size_t messagelen)
2946 if (this->output_section_ != NULL)
2947 this->output_section_->add_assertion(check, message, messagelen);
2948 else
2950 Sections_element* p = new Sections_element_assertion(check, message,
2951 messagelen);
2952 this->sections_elements_->push_back(p);
2956 // Start processing entries for an output section.
2958 void
2959 Script_sections::start_output_section(
2960 const char* name,
2961 size_t namelen,
2962 const Parser_output_section_header* header)
2964 Output_section_definition* posd = new Output_section_definition(name,
2965 namelen,
2966 header);
2967 this->sections_elements_->push_back(posd);
2968 gold_assert(this->output_section_ == NULL);
2969 this->output_section_ = posd;
2972 // Stop processing entries for an output section.
2974 void
2975 Script_sections::finish_output_section(
2976 const Parser_output_section_trailer* trailer)
2978 gold_assert(this->output_section_ != NULL);
2979 this->output_section_->finish(trailer);
2980 this->output_section_ = NULL;
2983 // Add a data item to the current output section.
2985 void
2986 Script_sections::add_data(int size, bool is_signed, Expression* val)
2988 gold_assert(this->output_section_ != NULL);
2989 this->output_section_->add_data(size, is_signed, val);
2992 // Add a fill value setting to the current output section.
2994 void
2995 Script_sections::add_fill(Expression* val)
2997 gold_assert(this->output_section_ != NULL);
2998 this->output_section_->add_fill(val);
3001 // Add an input section specification to the current output section.
3003 void
3004 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3006 gold_assert(this->output_section_ != NULL);
3007 this->output_section_->add_input_section(spec, keep);
3010 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
3011 // subsequent output sections may be relro.
3013 void
3014 Script_sections::data_segment_align()
3016 if (this->saw_data_segment_align_)
3017 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3018 gold_assert(!this->sections_elements_->empty());
3019 Sections_elements::iterator p = this->sections_elements_->end();
3020 --p;
3021 this->data_segment_align_start_ = p;
3022 this->saw_data_segment_align_ = true;
3025 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
3026 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3028 void
3029 Script_sections::data_segment_relro_end()
3031 if (this->saw_relro_end_)
3032 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3033 "in a linker script"));
3034 this->saw_relro_end_ = true;
3036 if (!this->saw_data_segment_align_)
3037 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3038 else
3040 Sections_elements::iterator p = this->data_segment_align_start_;
3041 for (++p; p != this->sections_elements_->end(); ++p)
3042 (*p)->set_is_relro();
3046 // Create any required sections.
3048 void
3049 Script_sections::create_sections(Layout* layout)
3051 if (!this->saw_sections_clause_)
3052 return;
3053 for (Sections_elements::iterator p = this->sections_elements_->begin();
3054 p != this->sections_elements_->end();
3055 ++p)
3056 (*p)->create_sections(layout);
3059 // Add any symbols we are defining to the symbol table.
3061 void
3062 Script_sections::add_symbols_to_table(Symbol_table* symtab)
3064 if (!this->saw_sections_clause_)
3065 return;
3066 for (Sections_elements::iterator p = this->sections_elements_->begin();
3067 p != this->sections_elements_->end();
3068 ++p)
3069 (*p)->add_symbols_to_table(symtab);
3072 // Finalize symbols and check assertions.
3074 void
3075 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3077 if (!this->saw_sections_clause_)
3078 return;
3079 uint64_t dot_value = 0;
3080 for (Sections_elements::iterator p = this->sections_elements_->begin();
3081 p != this->sections_elements_->end();
3082 ++p)
3083 (*p)->finalize_symbols(symtab, layout, &dot_value);
3086 // Return the name of the output section to use for an input file name
3087 // and section name.
3089 const char*
3090 Script_sections::output_section_name(
3091 const char* file_name,
3092 const char* section_name,
3093 Output_section*** output_section_slot,
3094 Script_sections::Section_type* psection_type)
3096 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3097 p != this->sections_elements_->end();
3098 ++p)
3100 const char* ret = (*p)->output_section_name(file_name, section_name,
3101 output_section_slot,
3102 psection_type);
3104 if (ret != NULL)
3106 // The special name /DISCARD/ means that the input section
3107 // should be discarded.
3108 if (strcmp(ret, "/DISCARD/") == 0)
3110 *output_section_slot = NULL;
3111 *psection_type = Script_sections::ST_NONE;
3112 return NULL;
3114 return ret;
3118 // If we couldn't find a mapping for the name, the output section
3119 // gets the name of the input section.
3121 *output_section_slot = NULL;
3122 *psection_type = Script_sections::ST_NONE;
3124 return section_name;
3127 // Place a marker for an orphan output section into the SECTIONS
3128 // clause.
3130 void
3131 Script_sections::place_orphan(Output_section* os)
3133 Orphan_section_placement* osp = this->orphan_section_placement_;
3134 if (osp == NULL)
3136 // Initialize the Orphan_section_placement structure.
3137 osp = new Orphan_section_placement();
3138 for (Sections_elements::iterator p = this->sections_elements_->begin();
3139 p != this->sections_elements_->end();
3140 ++p)
3141 (*p)->orphan_section_init(osp, p);
3142 gold_assert(!this->sections_elements_->empty());
3143 Sections_elements::iterator last = this->sections_elements_->end();
3144 --last;
3145 osp->last_init(last);
3146 this->orphan_section_placement_ = osp;
3149 Orphan_output_section* orphan = new Orphan_output_section(os);
3151 // Look for where to put ORPHAN.
3152 Sections_elements::iterator* where;
3153 if (osp->find_place(os, &where))
3155 if ((**where)->is_relro())
3156 os->set_is_relro();
3157 else
3158 os->clear_is_relro();
3160 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3161 // so that the next one goes after this one.
3162 Sections_elements::iterator p = *where;
3163 gold_assert(p != this->sections_elements_->end());
3164 ++p;
3165 *where = this->sections_elements_->insert(p, orphan);
3167 else
3169 os->clear_is_relro();
3170 // We don't have a place to put this orphan section. Put it,
3171 // and all other sections like it, at the end, but before the
3172 // sections which always come at the end.
3173 Sections_elements::iterator last = osp->last_place();
3174 *where = this->sections_elements_->insert(last, orphan);
3178 // Set the addresses of all the output sections. Walk through all the
3179 // elements, tracking the dot symbol. Apply assignments which set
3180 // absolute symbol values, in case they are used when setting dot.
3181 // Fill in data statement values. As we find output sections, set the
3182 // address, set the address of all associated input sections, and
3183 // update dot. Return the segment which should hold the file header
3184 // and segment headers, if any.
3186 Output_segment*
3187 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3189 gold_assert(this->saw_sections_clause_);
3191 // Walk the memory regions specified in this script, if any.
3192 if (this->memory_regions_ != NULL)
3194 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
3195 mr != this->memory_regions_->end();
3196 ++mr)
3198 // FIXME: What should we do with the attributes of the regions ?
3200 // For each region, set the VMA of the sections associated with it.
3201 for (Memory_region::Section_list::const_iterator s =
3202 (*mr)->get_vma_section_list_start();
3203 s != (*mr)->get_vma_section_list_end();
3204 ++s)
3206 (*s)->set_section_vma((*mr)->get_current_vma_address());
3207 (*mr)->increment_vma_offset((*s)->get_section_name(),
3208 (*s)->get_output_section()->current_data_size(),
3209 symtab, layout);
3212 // Similarly, set the LMA values.
3213 for (Memory_region::Section_list::const_iterator s =
3214 (*mr)->get_lma_section_list_start();
3215 s != (*mr)->get_lma_section_list_end();
3216 ++s)
3218 (*s)->set_section_lma((*mr)->get_current_lma_address());
3219 (*mr)->increment_lma_offset((*s)->get_section_name(),
3220 (*s)->get_output_section()->current_data_size(),
3221 symtab, layout);
3226 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3227 // for our representation.
3228 for (Sections_elements::iterator p = this->sections_elements_->begin();
3229 p != this->sections_elements_->end();
3230 ++p)
3232 Output_section_definition* posd;
3233 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3234 if (failed_constraint != CONSTRAINT_NONE)
3236 Sections_elements::iterator q;
3237 for (q = this->sections_elements_->begin();
3238 q != this->sections_elements_->end();
3239 ++q)
3241 if (q != p)
3243 if ((*q)->alternate_constraint(posd, failed_constraint))
3244 break;
3248 if (q == this->sections_elements_->end())
3249 gold_error(_("no matching section constraint"));
3253 // Force the alignment of the first TLS section to be the maximum
3254 // alignment of all TLS sections.
3255 Output_section* first_tls = NULL;
3256 uint64_t tls_align = 0;
3257 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3258 p != this->sections_elements_->end();
3259 ++p)
3261 Output_section* os = (*p)->get_output_section();
3262 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3264 if (first_tls == NULL)
3265 first_tls = os;
3266 if (os->addralign() > tls_align)
3267 tls_align = os->addralign();
3270 if (first_tls != NULL)
3271 first_tls->set_addralign(tls_align);
3273 // For a relocatable link, we implicitly set dot to zero.
3274 uint64_t dot_value = 0;
3275 uint64_t dot_alignment = 0;
3276 uint64_t load_address = 0;
3278 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3279 // to set section addresses. If the script has any SEGMENT_START
3280 // expression, we do not set the section addresses.
3281 bool use_tsection_options =
3282 (!this->saw_segment_start_expression_
3283 && (parameters->options().user_set_Ttext()
3284 || parameters->options().user_set_Tdata()
3285 || parameters->options().user_set_Tbss()));
3287 for (Sections_elements::iterator p = this->sections_elements_->begin();
3288 p != this->sections_elements_->end();
3289 ++p)
3291 Output_section* os = (*p)->get_output_section();
3293 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3294 // the special sections by names and doing dot assignments.
3295 if (use_tsection_options
3296 && os != NULL
3297 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3299 uint64_t new_dot_value = dot_value;
3301 if (parameters->options().user_set_Ttext()
3302 && strcmp(os->name(), ".text") == 0)
3303 new_dot_value = parameters->options().Ttext();
3304 else if (parameters->options().user_set_Tdata()
3305 && strcmp(os->name(), ".data") == 0)
3306 new_dot_value = parameters->options().Tdata();
3307 else if (parameters->options().user_set_Tbss()
3308 && strcmp(os->name(), ".bss") == 0)
3309 new_dot_value = parameters->options().Tbss();
3311 // Update dot and load address if necessary.
3312 if (new_dot_value < dot_value)
3313 gold_error(_("dot may not move backward"));
3314 else if (new_dot_value != dot_value)
3316 dot_value = new_dot_value;
3317 load_address = new_dot_value;
3321 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3322 &load_address);
3325 if (this->phdrs_elements_ != NULL)
3327 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3328 p != this->phdrs_elements_->end();
3329 ++p)
3330 (*p)->eval_load_address(symtab, layout);
3333 return this->create_segments(layout, dot_alignment);
3336 // Sort the sections in order to put them into segments.
3338 class Sort_output_sections
3340 public:
3341 bool
3342 operator()(const Output_section* os1, const Output_section* os2) const;
3345 bool
3346 Sort_output_sections::operator()(const Output_section* os1,
3347 const Output_section* os2) const
3349 // Sort first by the load address.
3350 uint64_t lma1 = (os1->has_load_address()
3351 ? os1->load_address()
3352 : os1->address());
3353 uint64_t lma2 = (os2->has_load_address()
3354 ? os2->load_address()
3355 : os2->address());
3356 if (lma1 != lma2)
3357 return lma1 < lma2;
3359 // Then sort by the virtual address.
3360 if (os1->address() != os2->address())
3361 return os1->address() < os2->address();
3363 // Sort TLS sections to the end.
3364 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3365 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3366 if (tls1 != tls2)
3367 return tls2;
3369 // Sort PROGBITS before NOBITS.
3370 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
3371 return true;
3372 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
3373 return false;
3375 // Sort non-NOLOAD before NOLOAD.
3376 if (os1->is_noload() && !os2->is_noload())
3377 return true;
3378 if (!os1->is_noload() && os2->is_noload())
3379 return true;
3381 // Otherwise we don't care.
3382 return false;
3385 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3386 // We treat a section with the SHF_TLS flag set as taking up space
3387 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3388 // space for them in the file.
3390 bool
3391 Script_sections::is_bss_section(const Output_section* os)
3393 return (os->type() == elfcpp::SHT_NOBITS
3394 && (os->flags() & elfcpp::SHF_TLS) == 0);
3397 // Return the size taken by the file header and the program headers.
3399 size_t
3400 Script_sections::total_header_size(Layout* layout) const
3402 size_t segment_count = layout->segment_count();
3403 size_t file_header_size;
3404 size_t segment_headers_size;
3405 if (parameters->target().get_size() == 32)
3407 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3408 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3410 else if (parameters->target().get_size() == 64)
3412 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3413 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3415 else
3416 gold_unreachable();
3418 return file_header_size + segment_headers_size;
3421 // Return the amount we have to subtract from the LMA to accomodate
3422 // headers of the given size. The complication is that the file
3423 // header have to be at the start of a page, as otherwise it will not
3424 // be at the start of the file.
3426 uint64_t
3427 Script_sections::header_size_adjustment(uint64_t lma,
3428 size_t sizeof_headers) const
3430 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3431 uint64_t hdr_lma = lma - sizeof_headers;
3432 hdr_lma &= ~(abi_pagesize - 1);
3433 return lma - hdr_lma;
3436 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3437 // the segment which should hold the file header and segment headers,
3438 // if any.
3440 Output_segment*
3441 Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
3443 gold_assert(this->saw_sections_clause_);
3445 if (parameters->options().relocatable())
3446 return NULL;
3448 if (this->saw_phdrs_clause())
3449 return create_segments_from_phdrs_clause(layout, dot_alignment);
3451 Layout::Section_list sections;
3452 layout->get_allocated_sections(&sections);
3454 // Sort the sections by address.
3455 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
3457 this->create_note_and_tls_segments(layout, &sections);
3459 // Walk through the sections adding them to PT_LOAD segments.
3460 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3461 Output_segment* first_seg = NULL;
3462 Output_segment* current_seg = NULL;
3463 bool is_current_seg_readonly = true;
3464 Layout::Section_list::iterator plast = sections.end();
3465 uint64_t last_vma = 0;
3466 uint64_t last_lma = 0;
3467 uint64_t last_size = 0;
3468 for (Layout::Section_list::iterator p = sections.begin();
3469 p != sections.end();
3470 ++p)
3472 const uint64_t vma = (*p)->address();
3473 const uint64_t lma = ((*p)->has_load_address()
3474 ? (*p)->load_address()
3475 : vma);
3476 const uint64_t size = (*p)->current_data_size();
3478 bool need_new_segment;
3479 if (current_seg == NULL)
3480 need_new_segment = true;
3481 else if (lma - vma != last_lma - last_vma)
3483 // This section has a different LMA relationship than the
3484 // last one; we need a new segment.
3485 need_new_segment = true;
3487 else if (align_address(last_lma + last_size, abi_pagesize)
3488 < align_address(lma, abi_pagesize))
3490 // Putting this section in the segment would require
3491 // skipping a page.
3492 need_new_segment = true;
3494 else if (is_bss_section(*plast) && !is_bss_section(*p))
3496 // A non-BSS section can not follow a BSS section in the
3497 // same segment.
3498 need_new_segment = true;
3500 else if (is_current_seg_readonly
3501 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3502 && !parameters->options().omagic())
3504 // Don't put a writable section in the same segment as a
3505 // non-writable section.
3506 need_new_segment = true;
3508 else
3510 // Otherwise, reuse the existing segment.
3511 need_new_segment = false;
3514 elfcpp::Elf_Word seg_flags =
3515 Layout::section_flags_to_segment((*p)->flags());
3517 if (need_new_segment)
3519 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3520 seg_flags);
3521 current_seg->set_addresses(vma, lma);
3522 current_seg->set_minimum_p_align(dot_alignment);
3523 if (first_seg == NULL)
3524 first_seg = current_seg;
3525 is_current_seg_readonly = true;
3528 current_seg->add_output_section_to_load(layout, *p, seg_flags);
3530 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3531 is_current_seg_readonly = false;
3533 plast = p;
3534 last_vma = vma;
3535 last_lma = lma;
3536 last_size = size;
3539 // An ELF program should work even if the program headers are not in
3540 // a PT_LOAD segment. However, it appears that the Linux kernel
3541 // does not set the AT_PHDR auxiliary entry in that case. It sets
3542 // the load address to p_vaddr - p_offset of the first PT_LOAD
3543 // segment. It then sets AT_PHDR to the load address plus the
3544 // offset to the program headers, e_phoff in the file header. This
3545 // fails when the program headers appear in the file before the
3546 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3547 // segment to hold the file header and the program headers. This is
3548 // effectively what the GNU linker does, and it is slightly more
3549 // efficient in any case. We try to use the first PT_LOAD segment
3550 // if we can, otherwise we make a new one.
3552 if (first_seg == NULL)
3553 return NULL;
3555 // -n or -N mean that the program is not demand paged and there is
3556 // no need to put the program headers in a PT_LOAD segment.
3557 if (parameters->options().nmagic() || parameters->options().omagic())
3558 return NULL;
3560 size_t sizeof_headers = this->total_header_size(layout);
3562 uint64_t vma = first_seg->vaddr();
3563 uint64_t lma = first_seg->paddr();
3565 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3567 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3569 first_seg->set_addresses(vma - subtract, lma - subtract);
3570 return first_seg;
3573 // If there is no room to squeeze in the headers, then punt. The
3574 // resulting executable probably won't run on GNU/Linux, but we
3575 // trust that the user knows what they are doing.
3576 if (lma < subtract || vma < subtract)
3577 return NULL;
3579 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3580 elfcpp::PF_R);
3581 load_seg->set_addresses(vma - subtract, lma - subtract);
3583 return load_seg;
3586 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3587 // segment if there are any SHT_TLS sections.
3589 void
3590 Script_sections::create_note_and_tls_segments(
3591 Layout* layout,
3592 const Layout::Section_list* sections)
3594 gold_assert(!this->saw_phdrs_clause());
3596 bool saw_tls = false;
3597 for (Layout::Section_list::const_iterator p = sections->begin();
3598 p != sections->end();
3599 ++p)
3601 if ((*p)->type() == elfcpp::SHT_NOTE)
3603 elfcpp::Elf_Word seg_flags =
3604 Layout::section_flags_to_segment((*p)->flags());
3605 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3606 seg_flags);
3607 oseg->add_output_section_to_nonload(*p, seg_flags);
3609 // Incorporate any subsequent SHT_NOTE sections, in the
3610 // hopes that the script is sensible.
3611 Layout::Section_list::const_iterator pnext = p + 1;
3612 while (pnext != sections->end()
3613 && (*pnext)->type() == elfcpp::SHT_NOTE)
3615 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3616 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3617 p = pnext;
3618 ++pnext;
3622 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3624 if (saw_tls)
3625 gold_error(_("TLS sections are not adjacent"));
3627 elfcpp::Elf_Word seg_flags =
3628 Layout::section_flags_to_segment((*p)->flags());
3629 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3630 seg_flags);
3631 oseg->add_output_section_to_nonload(*p, seg_flags);
3633 Layout::Section_list::const_iterator pnext = p + 1;
3634 while (pnext != sections->end()
3635 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3637 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3638 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3639 p = pnext;
3640 ++pnext;
3643 saw_tls = true;
3648 // Add a program header. The PHDRS clause is syntactically distinct
3649 // from the SECTIONS clause, but we implement it with the SECTIONS
3650 // support because PHDRS is useless if there is no SECTIONS clause.
3652 void
3653 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3654 bool includes_filehdr, bool includes_phdrs,
3655 bool is_flags_valid, unsigned int flags,
3656 Expression* load_address)
3658 if (this->phdrs_elements_ == NULL)
3659 this->phdrs_elements_ = new Phdrs_elements();
3660 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3661 includes_filehdr,
3662 includes_phdrs,
3663 is_flags_valid, flags,
3664 load_address));
3667 // Return the number of segments we expect to create based on the
3668 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3670 size_t
3671 Script_sections::expected_segment_count(const Layout* layout) const
3673 if (this->saw_phdrs_clause())
3674 return this->phdrs_elements_->size();
3676 Layout::Section_list sections;
3677 layout->get_allocated_sections(&sections);
3679 // We assume that we will need two PT_LOAD segments.
3680 size_t ret = 2;
3682 bool saw_note = false;
3683 bool saw_tls = false;
3684 for (Layout::Section_list::const_iterator p = sections.begin();
3685 p != sections.end();
3686 ++p)
3688 if ((*p)->type() == elfcpp::SHT_NOTE)
3690 // Assume that all note sections will fit into a single
3691 // PT_NOTE segment.
3692 if (!saw_note)
3694 ++ret;
3695 saw_note = true;
3698 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3700 // There can only be one PT_TLS segment.
3701 if (!saw_tls)
3703 ++ret;
3704 saw_tls = true;
3709 return ret;
3712 // Create the segments from a PHDRS clause. Return the segment which
3713 // should hold the file header and program headers, if any.
3715 Output_segment*
3716 Script_sections::create_segments_from_phdrs_clause(Layout* layout,
3717 uint64_t dot_alignment)
3719 this->attach_sections_using_phdrs_clause(layout);
3720 return this->set_phdrs_clause_addresses(layout, dot_alignment);
3723 // Create the segments from the PHDRS clause, and put the output
3724 // sections in them.
3726 void
3727 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
3729 typedef std::map<std::string, Output_segment*> Name_to_segment;
3730 Name_to_segment name_to_segment;
3731 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3732 p != this->phdrs_elements_->end();
3733 ++p)
3734 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
3736 // Walk through the output sections and attach them to segments.
3737 // Output sections in the script which do not list segments are
3738 // attached to the same set of segments as the immediately preceding
3739 // output section.
3741 String_list* phdr_names = NULL;
3742 bool load_segments_only = false;
3743 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3744 p != this->sections_elements_->end();
3745 ++p)
3747 bool orphan;
3748 String_list* old_phdr_names = phdr_names;
3749 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &orphan);
3750 if (os == NULL)
3751 continue;
3753 if (phdr_names == NULL)
3755 gold_error(_("allocated section not in any segment"));
3756 continue;
3759 // We see a list of segments names. Disable PT_LOAD segment only
3760 // filtering.
3761 if (old_phdr_names != phdr_names)
3762 load_segments_only = false;
3764 // If this is an orphan section--one that was not explicitly
3765 // mentioned in the linker script--then it should not inherit
3766 // any segment type other than PT_LOAD. Otherwise, e.g., the
3767 // PT_INTERP segment will pick up following orphan sections,
3768 // which does not make sense. If this is not an orphan section,
3769 // we trust the linker script.
3770 if (orphan)
3772 // Enable PT_LOAD segments only filtering until we see another
3773 // list of segment names.
3774 load_segments_only = true;
3777 bool in_load_segment = false;
3778 for (String_list::const_iterator q = phdr_names->begin();
3779 q != phdr_names->end();
3780 ++q)
3782 Name_to_segment::const_iterator r = name_to_segment.find(*q);
3783 if (r == name_to_segment.end())
3784 gold_error(_("no segment %s"), q->c_str());
3785 else
3787 if (load_segments_only
3788 && r->second->type() != elfcpp::PT_LOAD)
3789 continue;
3791 elfcpp::Elf_Word seg_flags =
3792 Layout::section_flags_to_segment(os->flags());
3794 if (r->second->type() != elfcpp::PT_LOAD)
3795 r->second->add_output_section_to_nonload(os, seg_flags);
3796 else
3798 r->second->add_output_section_to_load(layout, os, seg_flags);
3799 if (in_load_segment)
3800 gold_error(_("section in two PT_LOAD segments"));
3801 in_load_segment = true;
3806 if (!in_load_segment)
3807 gold_error(_("allocated section not in any PT_LOAD segment"));
3811 // Set the addresses for segments created from a PHDRS clause. Return
3812 // the segment which should hold the file header and program headers,
3813 // if any.
3815 Output_segment*
3816 Script_sections::set_phdrs_clause_addresses(Layout* layout,
3817 uint64_t dot_alignment)
3819 Output_segment* load_seg = NULL;
3820 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3821 p != this->phdrs_elements_->end();
3822 ++p)
3824 // Note that we have to set the flags after adding the output
3825 // sections to the segment, as adding an output segment can
3826 // change the flags.
3827 (*p)->set_flags_if_valid();
3829 Output_segment* oseg = (*p)->segment();
3831 if (oseg->type() != elfcpp::PT_LOAD)
3833 // The addresses of non-PT_LOAD segments are set from the
3834 // PT_LOAD segments.
3835 if ((*p)->has_load_address())
3836 gold_error(_("may only specify load address for PT_LOAD segment"));
3837 continue;
3840 oseg->set_minimum_p_align(dot_alignment);
3842 // The output sections should have addresses from the SECTIONS
3843 // clause. The addresses don't have to be in order, so find the
3844 // one with the lowest load address. Use that to set the
3845 // address of the segment.
3847 Output_section* osec = oseg->section_with_lowest_load_address();
3848 if (osec == NULL)
3850 oseg->set_addresses(0, 0);
3851 continue;
3854 uint64_t vma = osec->address();
3855 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
3857 // Override the load address of the section with the load
3858 // address specified for the segment.
3859 if ((*p)->has_load_address())
3861 if (osec->has_load_address())
3862 gold_warning(_("PHDRS load address overrides "
3863 "section %s load address"),
3864 osec->name());
3866 lma = (*p)->load_address();
3869 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
3870 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
3872 // We could support this if we wanted to.
3873 gold_error(_("using only one of FILEHDR and PHDRS is "
3874 "not currently supported"));
3876 if (headers)
3878 size_t sizeof_headers = this->total_header_size(layout);
3879 uint64_t subtract = this->header_size_adjustment(lma,
3880 sizeof_headers);
3881 if (lma >= subtract && vma >= subtract)
3883 lma -= subtract;
3884 vma -= subtract;
3886 else
3888 gold_error(_("sections loaded on first page without room "
3889 "for file and program headers "
3890 "are not supported"));
3893 if (load_seg != NULL)
3894 gold_error(_("using FILEHDR and PHDRS on more than one "
3895 "PT_LOAD segment is not currently supported"));
3896 load_seg = oseg;
3899 oseg->set_addresses(vma, lma);
3902 return load_seg;
3905 // Add the file header and segment headers to non-load segments
3906 // specified in the PHDRS clause.
3908 void
3909 Script_sections::put_headers_in_phdrs(Output_data* file_header,
3910 Output_data* segment_headers)
3912 gold_assert(this->saw_phdrs_clause());
3913 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3914 p != this->phdrs_elements_->end();
3915 ++p)
3917 if ((*p)->type() != elfcpp::PT_LOAD)
3919 if ((*p)->includes_phdrs())
3920 (*p)->segment()->add_initial_output_data(segment_headers);
3921 if ((*p)->includes_filehdr())
3922 (*p)->segment()->add_initial_output_data(file_header);
3927 // Look for an output section by name and return the address, the load
3928 // address, the alignment, and the size. This is used when an
3929 // expression refers to an output section which was not actually
3930 // created. This returns true if the section was found, false
3931 // otherwise.
3933 bool
3934 Script_sections::get_output_section_info(const char* name, uint64_t* address,
3935 uint64_t* load_address,
3936 uint64_t* addralign,
3937 uint64_t* size) const
3939 if (!this->saw_sections_clause_)
3940 return false;
3941 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3942 p != this->sections_elements_->end();
3943 ++p)
3944 if ((*p)->get_output_section_info(name, address, load_address, addralign,
3945 size))
3946 return true;
3947 return false;
3950 // Release all Output_segments. This remove all pointers to all
3951 // Output_segments.
3953 void
3954 Script_sections::release_segments()
3956 if (this->saw_phdrs_clause())
3958 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3959 p != this->phdrs_elements_->end();
3960 ++p)
3961 (*p)->release_segment();
3965 // Print the SECTIONS clause to F for debugging.
3967 void
3968 Script_sections::print(FILE* f) const
3970 if (this->phdrs_elements_ != NULL)
3972 fprintf(f, "PHDRS {\n");
3973 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3974 p != this->phdrs_elements_->end();
3975 ++p)
3976 (*p)->print(f);
3977 fprintf(f, "}\n");
3980 if (this->memory_regions_ != NULL)
3982 fprintf(f, "MEMORY {\n");
3983 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3984 m != this->memory_regions_->end();
3985 ++m)
3986 (*m)->print(f);
3987 fprintf(f, "}\n");
3990 if (!this->saw_sections_clause_)
3991 return;
3993 fprintf(f, "SECTIONS {\n");
3995 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3996 p != this->sections_elements_->end();
3997 ++p)
3998 (*p)->print(f);
4000 fprintf(f, "}\n");
4003 } // End namespace gold.