1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008, 2009, 2010, 2011 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.
33 #include "parameters.h"
39 #include "script-sections.h"
41 // Support for the SECTIONS clause in linker scripts.
46 // A region of memory.
50 Memory_region(const char* name, size_t namelen, unsigned int attributes,
51 Expression* start, Expression* length)
52 : name_(name, namelen),
53 attributes_(attributes),
62 // Return the name of this region.
65 { return this->name_; }
67 // Return the start address of this region.
70 { return this->start_; }
72 // Return the length of this region.
75 { return this->length_; }
77 // Print the region (when debugging).
81 // Return true if <name,namelen> matches this region.
83 name_match(const char* name, size_t namelen)
85 return (this->name_.length() == namelen
86 && strncmp(this->name_.c_str(), name, namelen) == 0);
90 get_current_address() const
93 script_exp_binary_add(this->start_,
94 script_exp_integer(this->current_offset_));
98 increment_offset(std::string section_name, uint64_t amount,
99 const Symbol_table* symtab, const Layout* layout)
101 this->current_offset_ += amount;
103 if (this->current_offset_
104 > this->length_->eval(symtab, layout, false))
105 gold_error(_("section %s overflows end of region %s"),
106 section_name.c_str(), this->name_.c_str());
109 // Returns true iff there is room left in this region
110 // for AMOUNT more bytes of data.
112 has_room_for(const Symbol_table* symtab, const Layout* layout,
113 uint64_t amount) const
115 return (this->current_offset_ + amount
116 < this->length_->eval(symtab, layout, false));
119 // Return true if the provided section flags
120 // are compatible with this region's attributes.
122 attributes_compatible(elfcpp::Elf_Xword flags, elfcpp::Elf_Xword type) const;
125 add_section(Output_section_definition* sec, bool vma)
128 this->vma_sections_.push_back(sec);
130 this->lma_sections_.push_back(sec);
133 typedef std::vector<Output_section_definition*> Section_list;
135 // Return the start of the list of sections
136 // whose VMAs are taken from this region.
137 Section_list::const_iterator
138 get_vma_section_list_start() const
139 { return this->vma_sections_.begin(); }
141 // Return the start of the list of sections
142 // whose LMAs are taken from this region.
143 Section_list::const_iterator
144 get_lma_section_list_start() const
145 { return this->lma_sections_.begin(); }
147 // Return the end of the list of sections
148 // whose VMAs are taken from this region.
149 Section_list::const_iterator
150 get_vma_section_list_end() const
151 { return this->vma_sections_.end(); }
153 // Return the end of the list of sections
154 // whose LMAs are taken from this region.
155 Section_list::const_iterator
156 get_lma_section_list_end() const
157 { return this->lma_sections_.end(); }
159 Output_section_definition*
160 get_last_section() const
161 { return this->last_section_; }
164 set_last_section(Output_section_definition* sec)
165 { this->last_section_ = sec; }
170 unsigned int attributes_;
173 // The offset to the next free byte in the region.
174 // Note - for compatibility with GNU LD we only maintain one offset
175 // regardless of whether the region is being used for VMA values,
176 // LMA values, or both.
177 uint64_t current_offset_;
178 // A list of sections whose VMAs are set inside this region.
179 Section_list vma_sections_;
180 // A list of sections whose LMAs are set inside this region.
181 Section_list lma_sections_;
182 // The latest section to make use of this region.
183 Output_section_definition* last_section_;
186 // Return true if the provided section flags
187 // are compatible with this region's attributes.
190 Memory_region::attributes_compatible(elfcpp::Elf_Xword flags,
191 elfcpp::Elf_Xword type) const
193 unsigned int attrs = this->attributes_;
195 // No attributes means that this region is not compatible with anything.
202 switch (attrs & - attrs)
205 if ((flags & elfcpp::SHF_EXECINSTR) == 0)
210 if ((flags & elfcpp::SHF_WRITE) == 0)
215 // All sections are presumed readable.
218 case MEM_ALLOCATABLE:
219 if ((flags & elfcpp::SHF_ALLOC) == 0)
223 case MEM_INITIALIZED:
224 if ((type & elfcpp::SHT_NOBITS) != 0)
228 attrs &= ~ (attrs & - attrs);
235 // Print a memory region.
238 Memory_region::print(FILE* f) const
240 fprintf(f, " %s", this->name_.c_str());
242 unsigned int attrs = this->attributes_;
248 switch (attrs & - attrs)
250 case MEM_EXECUTABLE: fputc('x', f); break;
251 case MEM_WRITEABLE: fputc('w', f); break;
252 case MEM_READABLE: fputc('r', f); break;
253 case MEM_ALLOCATABLE: fputc('a', f); break;
254 case MEM_INITIALIZED: fputc('i', f); break;
258 attrs &= ~ (attrs & - attrs);
264 fprintf(f, " : origin = ");
265 this->start_->print(f);
266 fprintf(f, ", length = ");
267 this->length_->print(f);
271 // Manage orphan sections. This is intended to be largely compatible
272 // with the GNU linker. The Linux kernel implicitly relies on
273 // something similar to the GNU linker's orphan placement. We
274 // originally used a simpler scheme here, but it caused the kernel
275 // build to fail, and was also rather inefficient.
277 class Orphan_section_placement
280 typedef Script_sections::Elements_iterator Elements_iterator;
283 Orphan_section_placement();
285 // Handle an output section during initialization of this mapping.
287 output_section_init(const std::string& name, Output_section*,
288 Elements_iterator location);
290 // Initialize the last location.
292 last_init(Elements_iterator location);
294 // Set *PWHERE to the address of an iterator pointing to the
295 // location to use for an orphan section. Return true if the
296 // iterator has a value, false otherwise.
298 find_place(Output_section*, Elements_iterator** pwhere);
300 // Return the iterator being used for sections at the very end of
301 // the linker script.
306 // The places that we specifically recognize. This list is copied
307 // from the GNU linker.
323 // The information we keep for a specific place.
326 // The name of sections for this place.
328 // Whether we have a location for this place.
330 // The iterator for this place.
331 Elements_iterator location;
334 // Initialize one place element.
336 initialize_place(Place_index, const char*);
339 Place places_[PLACE_MAX];
340 // True if this is the first call to output_section_init.
344 // Initialize Orphan_section_placement.
346 Orphan_section_placement::Orphan_section_placement()
349 this->initialize_place(PLACE_TEXT, ".text");
350 this->initialize_place(PLACE_RODATA, ".rodata");
351 this->initialize_place(PLACE_DATA, ".data");
352 this->initialize_place(PLACE_TLS, NULL);
353 this->initialize_place(PLACE_TLS_BSS, NULL);
354 this->initialize_place(PLACE_BSS, ".bss");
355 this->initialize_place(PLACE_REL, NULL);
356 this->initialize_place(PLACE_INTERP, ".interp");
357 this->initialize_place(PLACE_NONALLOC, NULL);
358 this->initialize_place(PLACE_LAST, NULL);
361 // Initialize one place element.
364 Orphan_section_placement::initialize_place(Place_index index, const char* name)
366 this->places_[index].name = name;
367 this->places_[index].have_location = false;
370 // While initializing the Orphan_section_placement information, this
371 // is called once for each output section named in the linker script.
372 // If we found an output section during the link, it will be passed in
376 Orphan_section_placement::output_section_init(const std::string& name,
378 Elements_iterator location)
380 bool first_init = this->first_init_;
381 this->first_init_ = false;
383 for (int i = 0; i < PLACE_MAX; ++i)
385 if (this->places_[i].name != NULL && this->places_[i].name == name)
387 if (this->places_[i].have_location)
389 // We have already seen a section with this name.
393 this->places_[i].location = location;
394 this->places_[i].have_location = true;
396 // If we just found the .bss section, restart the search for
397 // an unallocated section. This follows the GNU linker's
400 this->places_[PLACE_NONALLOC].have_location = false;
406 // Relocation sections.
407 if (!this->places_[PLACE_REL].have_location
409 && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
410 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
412 this->places_[PLACE_REL].location = location;
413 this->places_[PLACE_REL].have_location = true;
416 // We find the location for unallocated sections by finding the
417 // first debugging or comment section after the BSS section (if
419 if (!this->places_[PLACE_NONALLOC].have_location
420 && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
422 // We add orphan sections after the location in PLACES_. We
423 // want to store unallocated sections before LOCATION. If this
424 // is the very first section, we can't use it.
428 this->places_[PLACE_NONALLOC].location = location;
429 this->places_[PLACE_NONALLOC].have_location = true;
434 // Initialize the last location.
437 Orphan_section_placement::last_init(Elements_iterator location)
439 this->places_[PLACE_LAST].location = location;
440 this->places_[PLACE_LAST].have_location = true;
443 // Set *PWHERE to the address of an iterator pointing to the location
444 // to use for an orphan section. Return true if the iterator has a
445 // value, false otherwise.
448 Orphan_section_placement::find_place(Output_section* os,
449 Elements_iterator** pwhere)
451 // Figure out where OS should go. This is based on the GNU linker
452 // code. FIXME: The GNU linker handles small data sections
453 // specially, but we don't.
454 elfcpp::Elf_Word type = os->type();
455 elfcpp::Elf_Xword flags = os->flags();
457 if ((flags & elfcpp::SHF_ALLOC) == 0
458 && !Layout::is_debug_info_section(os->name()))
459 index = PLACE_NONALLOC;
460 else if ((flags & elfcpp::SHF_ALLOC) == 0)
462 else if (type == elfcpp::SHT_NOTE)
463 index = PLACE_INTERP;
464 else if ((flags & elfcpp::SHF_TLS) != 0)
466 if (type == elfcpp::SHT_NOBITS)
467 index = PLACE_TLS_BSS;
471 else if (type == elfcpp::SHT_NOBITS)
473 else if ((flags & elfcpp::SHF_WRITE) != 0)
475 else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
477 else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
478 index = PLACE_RODATA;
482 // If we don't have a location yet, try to find one based on a
483 // plausible ordering of sections.
484 if (!this->places_[index].have_location)
509 if (!this->places_[PLACE_TLS].have_location)
513 if (follow != PLACE_MAX && this->places_[follow].have_location)
515 // Set the location of INDEX to the location of FOLLOW. The
516 // location of INDEX will then be incremented by the caller,
517 // so anything in INDEX will continue to be after anything
519 this->places_[index].location = this->places_[follow].location;
520 this->places_[index].have_location = true;
524 *pwhere = &this->places_[index].location;
525 bool ret = this->places_[index].have_location;
527 // The caller will set the location.
528 this->places_[index].have_location = true;
533 // Return the iterator being used for sections at the very end of the
536 Orphan_section_placement::Elements_iterator
537 Orphan_section_placement::last_place() const
539 gold_assert(this->places_[PLACE_LAST].have_location);
540 return this->places_[PLACE_LAST].location;
543 // An element in a SECTIONS clause.
545 class Sections_element
551 virtual ~Sections_element()
554 // Return whether an output section is relro.
559 // Record that an output section is relro.
564 // Create any required output sections. The only real
565 // implementation is in Output_section_definition.
567 create_sections(Layout*)
570 // Add any symbol being defined to the symbol table.
572 add_symbols_to_table(Symbol_table*)
575 // Finalize symbols and check assertions.
577 finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
580 // Return the output section name to use for an input file name and
581 // section name. This only real implementation is in
582 // Output_section_definition.
584 output_section_name(const char*, const char*, Output_section***,
585 Script_sections::Section_type*, bool*)
588 // Initialize OSP with an output section.
590 orphan_section_init(Orphan_section_placement*,
591 Script_sections::Elements_iterator)
594 // Set section addresses. This includes applying assignments if the
595 // expression is an absolute value.
597 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
601 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
602 // this section is constrained, and the input sections do not match,
603 // return the constraint, and set *POSD.
604 virtual Section_constraint
605 check_constraint(Output_section_definition**)
606 { return CONSTRAINT_NONE; }
608 // See if this is the alternate output section for a constrained
609 // output section. If it is, transfer the Output_section and return
610 // true. Otherwise return false.
612 alternate_constraint(Output_section_definition*, Section_constraint)
615 // Get the list of segments to use for an allocated section when
616 // using a PHDRS clause. If this is an allocated section, return
617 // the Output_section, and set *PHDRS_LIST (the first parameter) to
618 // the list of PHDRS to which it should be attached. If the PHDRS
619 // were not specified, don't change *PHDRS_LIST. When not returning
620 // NULL, set *ORPHAN (the second parameter) according to whether
621 // this is an orphan section--one that is not mentioned in the
623 virtual Output_section*
624 allocate_to_segment(String_list**, bool*)
627 // Look for an output section by name and return the address, the
628 // load address, the alignment, and the size. This is used when an
629 // expression refers to an output section which was not actually
630 // created. This returns true if the section was found, false
631 // otherwise. The only real definition is for
632 // Output_section_definition.
634 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
638 // Return the associated Output_section if there is one.
639 virtual Output_section*
640 get_output_section() const
643 // Set the section's memory regions.
645 set_memory_region(Memory_region*, bool)
646 { gold_error(_("Attempt to set a memory region for a non-output section")); }
648 // Print the element for debugging purposes.
650 print(FILE* f) const = 0;
653 // An assignment in a SECTIONS clause outside of an output section.
655 class Sections_element_assignment : public Sections_element
658 Sections_element_assignment(const char* name, size_t namelen,
659 Expression* val, bool provide, bool hidden)
660 : assignment_(name, namelen, false, val, provide, hidden)
663 // Add the symbol to the symbol table.
665 add_symbols_to_table(Symbol_table* symtab)
666 { this->assignment_.add_to_table(symtab); }
668 // Finalize the symbol.
670 finalize_symbols(Symbol_table* symtab, const Layout* layout,
673 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
676 // Set the section address. There is no section here, but if the
677 // value is absolute, we set the symbol. This permits us to use
678 // absolute symbols when setting dot.
680 set_section_addresses(Symbol_table* symtab, Layout* layout,
681 uint64_t* dot_value, uint64_t*, uint64_t*)
683 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value, NULL);
686 // Print for debugging.
691 this->assignment_.print(f);
695 Symbol_assignment assignment_;
698 // An assignment to the dot symbol in a SECTIONS clause outside of an
701 class Sections_element_dot_assignment : public Sections_element
704 Sections_element_dot_assignment(Expression* val)
708 // Finalize the symbol.
710 finalize_symbols(Symbol_table* symtab, const Layout* layout,
713 // We ignore the section of the result because outside of an
714 // output section definition the dot symbol is always considered
716 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
717 NULL, NULL, NULL, false);
720 // Update the dot symbol while setting section addresses.
722 set_section_addresses(Symbol_table* symtab, Layout* layout,
723 uint64_t* dot_value, uint64_t* dot_alignment,
724 uint64_t* load_address)
726 *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
727 NULL, NULL, dot_alignment, false);
728 *load_address = *dot_value;
731 // Print for debugging.
736 this->val_->print(f);
744 // An assertion in a SECTIONS clause outside of an output section.
746 class Sections_element_assertion : public Sections_element
749 Sections_element_assertion(Expression* check, const char* message,
751 : assertion_(check, message, messagelen)
754 // Check the assertion.
756 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
757 { this->assertion_.check(symtab, layout); }
759 // Print for debugging.
764 this->assertion_.print(f);
768 Script_assertion assertion_;
771 // An element in an output section in a SECTIONS clause.
773 class Output_section_element
776 // A list of input sections.
777 typedef std::list<Output_section::Input_section> Input_section_list;
779 Output_section_element()
782 virtual ~Output_section_element()
785 // Return whether this element requires an output section to exist.
787 needs_output_section() const
790 // Add any symbol being defined to the symbol table.
792 add_symbols_to_table(Symbol_table*)
795 // Finalize symbols and check assertions.
797 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
800 // Return whether this element matches FILE_NAME and SECTION_NAME.
801 // The only real implementation is in Output_section_element_input.
803 match_name(const char*, const char*, bool *) const
806 // Set section addresses. This includes applying assignments if the
807 // expression is an absolute value.
809 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
810 uint64_t*, uint64_t*, Output_section**, std::string*,
814 // Print the element for debugging purposes.
816 print(FILE* f) const = 0;
819 // Return a fill string that is LENGTH bytes long, filling it with
822 get_fill_string(const std::string* fill, section_size_type length) const;
826 Output_section_element::get_fill_string(const std::string* fill,
827 section_size_type length) const
829 std::string this_fill;
830 this_fill.reserve(length);
831 while (this_fill.length() + fill->length() <= length)
833 if (this_fill.length() < length)
834 this_fill.append(*fill, 0, length - this_fill.length());
838 // A symbol assignment in an output section.
840 class Output_section_element_assignment : public Output_section_element
843 Output_section_element_assignment(const char* name, size_t namelen,
844 Expression* val, bool provide,
846 : assignment_(name, namelen, false, val, provide, hidden)
849 // Add the symbol to the symbol table.
851 add_symbols_to_table(Symbol_table* symtab)
852 { this->assignment_.add_to_table(symtab); }
854 // Finalize the symbol.
856 finalize_symbols(Symbol_table* symtab, const Layout* layout,
857 uint64_t* dot_value, Output_section** dot_section)
859 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
863 // Set the section address. There is no section here, but if the
864 // value is absolute, we set the symbol. This permits us to use
865 // absolute symbols when setting dot.
867 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
868 uint64_t, uint64_t* dot_value, uint64_t*,
869 Output_section** dot_section, std::string*,
872 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value,
876 // Print for debugging.
881 this->assignment_.print(f);
885 Symbol_assignment assignment_;
888 // An assignment to the dot symbol in an output section.
890 class Output_section_element_dot_assignment : public Output_section_element
893 Output_section_element_dot_assignment(Expression* val)
897 // An assignment to dot within an output section is enough to force
898 // the output section to exist.
900 needs_output_section() const
903 // Finalize the symbol.
905 finalize_symbols(Symbol_table* symtab, const Layout* layout,
906 uint64_t* dot_value, Output_section** dot_section)
908 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
909 *dot_section, dot_section, NULL,
913 // Update the dot symbol while setting section addresses.
915 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
916 uint64_t, uint64_t* dot_value, uint64_t*,
917 Output_section** dot_section, std::string*,
918 Input_section_list*);
920 // Print for debugging.
925 this->val_->print(f);
933 // Update the dot symbol while setting section addresses.
936 Output_section_element_dot_assignment::set_section_addresses(
937 Symbol_table* symtab,
939 Output_section* output_section,
942 uint64_t* dot_alignment,
943 Output_section** dot_section,
947 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
948 *dot_value, *dot_section,
949 dot_section, dot_alignment,
951 if (next_dot < *dot_value)
952 gold_error(_("dot may not move backward"));
953 if (next_dot > *dot_value && output_section != NULL)
955 section_size_type length = convert_to_section_size_type(next_dot
957 Output_section_data* posd;
959 posd = new Output_data_zero_fill(length, 0);
962 std::string this_fill = this->get_fill_string(fill, length);
963 posd = new Output_data_const(this_fill, 0);
965 output_section->add_output_section_data(posd);
966 layout->new_output_section_data_from_script(posd);
968 *dot_value = next_dot;
971 // An assertion in an output section.
973 class Output_section_element_assertion : public Output_section_element
976 Output_section_element_assertion(Expression* check, const char* message,
978 : assertion_(check, message, messagelen)
985 this->assertion_.print(f);
989 Script_assertion assertion_;
992 // We use a special instance of Output_section_data to handle BYTE,
993 // SHORT, etc. This permits forward references to symbols in the
996 class Output_data_expression : public Output_section_data
999 Output_data_expression(int size, bool is_signed, Expression* val,
1000 const Symbol_table* symtab, const Layout* layout,
1001 uint64_t dot_value, Output_section* dot_section)
1002 : Output_section_data(size, 0, true),
1003 is_signed_(is_signed), val_(val), symtab_(symtab),
1004 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
1008 // Write the data to the output file.
1010 do_write(Output_file*);
1012 // Write the data to a buffer.
1014 do_write_to_buffer(unsigned char*);
1016 // Write to a map file.
1018 do_print_to_mapfile(Mapfile* mapfile) const
1019 { mapfile->print_output_data(this, _("** expression")); }
1022 template<bool big_endian>
1024 endian_write_to_buffer(uint64_t, unsigned char*);
1028 const Symbol_table* symtab_;
1029 const Layout* layout_;
1030 uint64_t dot_value_;
1031 Output_section* dot_section_;
1034 // Write the data element to the output file.
1037 Output_data_expression::do_write(Output_file* of)
1039 unsigned char* view = of->get_output_view(this->offset(), this->data_size());
1040 this->write_to_buffer(view);
1041 of->write_output_view(this->offset(), this->data_size(), view);
1044 // Write the data element to a buffer.
1047 Output_data_expression::do_write_to_buffer(unsigned char* buf)
1049 uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
1050 true, this->dot_value_,
1051 this->dot_section_, NULL, NULL,
1054 if (parameters->target().is_big_endian())
1055 this->endian_write_to_buffer<true>(val, buf);
1057 this->endian_write_to_buffer<false>(val, buf);
1060 template<bool big_endian>
1062 Output_data_expression::endian_write_to_buffer(uint64_t val,
1065 switch (this->data_size())
1068 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
1071 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
1074 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
1077 if (parameters->target().get_size() == 32)
1080 if (this->is_signed_ && (val & 0x80000000) != 0)
1081 val |= 0xffffffff00000000LL;
1083 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
1090 // A data item in an output section.
1092 class Output_section_element_data : public Output_section_element
1095 Output_section_element_data(int size, bool is_signed, Expression* val)
1096 : size_(size), is_signed_(is_signed), val_(val)
1099 // If there is a data item, then we must create an output section.
1101 needs_output_section() const
1104 // Finalize symbols--we just need to update dot.
1106 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1108 { *dot_value += this->size_; }
1110 // Store the value in the section.
1112 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
1113 uint64_t* dot_value, uint64_t*, Output_section**,
1114 std::string*, Input_section_list*);
1116 // Print for debugging.
1121 // The size in bytes.
1123 // Whether the value is signed.
1129 // Store the value in the section.
1132 Output_section_element_data::set_section_addresses(
1133 Symbol_table* symtab,
1137 uint64_t* dot_value,
1139 Output_section** dot_section,
1141 Input_section_list*)
1143 gold_assert(os != NULL);
1144 Output_data_expression* expression =
1145 new Output_data_expression(this->size_, this->is_signed_, this->val_,
1146 symtab, layout, *dot_value, *dot_section);
1147 os->add_output_section_data(expression);
1148 layout->new_output_section_data_from_script(expression);
1149 *dot_value += this->size_;
1152 // Print for debugging.
1155 Output_section_element_data::print(FILE* f) const
1158 switch (this->size_)
1170 if (this->is_signed_)
1178 fprintf(f, " %s(", s);
1179 this->val_->print(f);
1183 // A fill value setting in an output section.
1185 class Output_section_element_fill : public Output_section_element
1188 Output_section_element_fill(Expression* val)
1192 // Update the fill value while setting section addresses.
1194 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1195 uint64_t, uint64_t* dot_value, uint64_t*,
1196 Output_section** dot_section,
1197 std::string* fill, Input_section_list*)
1199 Output_section* fill_section;
1200 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
1201 *dot_value, *dot_section,
1202 &fill_section, NULL, false);
1203 if (fill_section != NULL)
1204 gold_warning(_("fill value is not absolute"));
1205 // FIXME: The GNU linker supports fill values of arbitrary length.
1206 unsigned char fill_buff[4];
1207 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1208 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
1211 // Print for debugging.
1213 print(FILE* f) const
1215 fprintf(f, " FILL(");
1216 this->val_->print(f);
1221 // The new fill value.
1225 // An input section specification in an output section
1227 class Output_section_element_input : public Output_section_element
1230 Output_section_element_input(const Input_section_spec* spec, bool keep);
1232 // Finalize symbols--just update the value of the dot symbol.
1234 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1235 Output_section** dot_section)
1237 *dot_value = this->final_dot_value_;
1238 *dot_section = this->final_dot_section_;
1241 // See whether we match FILE_NAME and SECTION_NAME as an input section.
1242 // If we do then also indicate whether the section should be KEPT.
1244 match_name(const char* file_name, const char* section_name, bool* keep) const;
1246 // Set the section address.
1248 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1249 uint64_t subalign, uint64_t* dot_value, uint64_t*,
1250 Output_section**, std::string* fill,
1251 Input_section_list*);
1253 // Print for debugging.
1255 print(FILE* f) const;
1258 // An input section pattern.
1259 struct Input_section_pattern
1261 std::string pattern;
1262 bool pattern_is_wildcard;
1265 Input_section_pattern(const char* patterna, size_t patternlena,
1266 Sort_wildcard sorta)
1267 : pattern(patterna, patternlena),
1268 pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
1273 typedef std::vector<Input_section_pattern> Input_section_patterns;
1275 // Filename_exclusions is a pair of filename pattern and a bool
1276 // indicating whether the filename is a wildcard.
1277 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1279 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1280 // indicates whether this is a wildcard pattern.
1282 match(const char* string, const char* pattern, bool is_wildcard_pattern)
1284 return (is_wildcard_pattern
1285 ? fnmatch(pattern, string, 0) == 0
1286 : strcmp(string, pattern) == 0);
1289 // See if we match a file name.
1291 match_file_name(const char* file_name) const;
1293 // The file name pattern. If this is the empty string, we match all
1295 std::string filename_pattern_;
1296 // Whether the file name pattern is a wildcard.
1297 bool filename_is_wildcard_;
1298 // How the file names should be sorted. This may only be
1299 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1300 Sort_wildcard filename_sort_;
1301 // The list of file names to exclude.
1302 Filename_exclusions filename_exclusions_;
1303 // The list of input section patterns.
1304 Input_section_patterns input_section_patterns_;
1305 // Whether to keep this section when garbage collecting.
1307 // The value of dot after including all matching sections.
1308 uint64_t final_dot_value_;
1309 // The section where dot is defined after including all matching
1311 Output_section* final_dot_section_;
1314 // Construct Output_section_element_input. The parser records strings
1315 // as pointers into a copy of the script file, which will go away when
1316 // parsing is complete. We make sure they are in std::string objects.
1318 Output_section_element_input::Output_section_element_input(
1319 const Input_section_spec* spec,
1321 : filename_pattern_(),
1322 filename_is_wildcard_(false),
1323 filename_sort_(spec->file.sort),
1324 filename_exclusions_(),
1325 input_section_patterns_(),
1327 final_dot_value_(0),
1328 final_dot_section_(NULL)
1330 // The filename pattern "*" is common, and matches all files. Turn
1331 // it into the empty string.
1332 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1333 this->filename_pattern_.assign(spec->file.name.value,
1334 spec->file.name.length);
1335 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
1337 if (spec->input_sections.exclude != NULL)
1339 for (String_list::const_iterator p =
1340 spec->input_sections.exclude->begin();
1341 p != spec->input_sections.exclude->end();
1344 bool is_wildcard = is_wildcard_string((*p).c_str());
1345 this->filename_exclusions_.push_back(std::make_pair(*p,
1350 if (spec->input_sections.sections != NULL)
1352 Input_section_patterns& isp(this->input_section_patterns_);
1353 for (String_sort_list::const_iterator p =
1354 spec->input_sections.sections->begin();
1355 p != spec->input_sections.sections->end();
1357 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1362 // See whether we match FILE_NAME.
1365 Output_section_element_input::match_file_name(const char* file_name) const
1367 if (!this->filename_pattern_.empty())
1369 // If we were called with no filename, we refuse to match a
1370 // pattern which requires a file name.
1371 if (file_name == NULL)
1374 if (!match(file_name, this->filename_pattern_.c_str(),
1375 this->filename_is_wildcard_))
1379 if (file_name != NULL)
1381 // Now we have to see whether FILE_NAME matches one of the
1382 // exclusion patterns, if any.
1383 for (Filename_exclusions::const_iterator p =
1384 this->filename_exclusions_.begin();
1385 p != this->filename_exclusions_.end();
1388 if (match(file_name, p->first.c_str(), p->second))
1396 // See whether we match FILE_NAME and SECTION_NAME. If we do then
1397 // KEEP indicates whether the section should survive garbage collection.
1400 Output_section_element_input::match_name(const char* file_name,
1401 const char* section_name,
1404 if (!this->match_file_name(file_name))
1407 *keep = this->keep_;
1409 // If there are no section name patterns, then we match.
1410 if (this->input_section_patterns_.empty())
1413 // See whether we match the section name patterns.
1414 for (Input_section_patterns::const_iterator p =
1415 this->input_section_patterns_.begin();
1416 p != this->input_section_patterns_.end();
1419 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1423 // We didn't match any section names, so we didn't match.
1427 // Information we use to sort the input sections.
1429 class Input_section_info
1432 Input_section_info(const Output_section::Input_section& input_section)
1433 : input_section_(input_section), section_name_(),
1434 size_(0), addralign_(1)
1437 // Return the simple input section.
1438 const Output_section::Input_section&
1439 input_section() const
1440 { return this->input_section_; }
1442 // Return the object.
1445 { return this->input_section_.relobj(); }
1447 // Return the section index.
1450 { return this->input_section_.shndx(); }
1452 // Return the section name.
1454 section_name() const
1455 { return this->section_name_; }
1457 // Set the section name.
1459 set_section_name(const std::string name)
1460 { this->section_name_ = name; }
1462 // Return the section size.
1465 { return this->size_; }
1467 // Set the section size.
1469 set_size(uint64_t size)
1470 { this->size_ = size; }
1472 // Return the address alignment.
1475 { return this->addralign_; }
1477 // Set the address alignment.
1479 set_addralign(uint64_t addralign)
1480 { this->addralign_ = addralign; }
1483 // Input section, can be a relaxed section.
1484 Output_section::Input_section input_section_;
1485 // Name of the section.
1486 std::string section_name_;
1489 // Address alignment.
1490 uint64_t addralign_;
1493 // A class to sort the input sections.
1495 class Input_section_sorter
1498 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1499 : filename_sort_(filename_sort), section_sort_(section_sort)
1503 operator()(const Input_section_info&, const Input_section_info&) const;
1506 Sort_wildcard filename_sort_;
1507 Sort_wildcard section_sort_;
1511 Input_section_sorter::operator()(const Input_section_info& isi1,
1512 const Input_section_info& isi2) const
1514 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1515 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1516 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1517 && isi1.addralign() == isi2.addralign()))
1519 if (isi1.section_name() != isi2.section_name())
1520 return isi1.section_name() < isi2.section_name();
1522 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1523 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1524 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1526 if (isi1.addralign() != isi2.addralign())
1527 return isi1.addralign() < isi2.addralign();
1529 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1531 if (isi1.relobj()->name() != isi2.relobj()->name())
1532 return (isi1.relobj()->name() < isi2.relobj()->name());
1535 // Otherwise we leave them in the same order.
1539 // Set the section address. Look in INPUT_SECTIONS for sections which
1540 // match this spec, sort them as specified, and add them to the output
1544 Output_section_element_input::set_section_addresses(
1547 Output_section* output_section,
1549 uint64_t* dot_value,
1551 Output_section** dot_section,
1553 Input_section_list* input_sections)
1555 // We build a list of sections which match each
1556 // Input_section_pattern.
1558 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1559 size_t input_pattern_count = this->input_section_patterns_.size();
1560 if (input_pattern_count == 0)
1561 input_pattern_count = 1;
1562 Matching_sections matching_sections(input_pattern_count);
1564 // Look through the list of sections for this output section. Add
1565 // each one which matches to one of the elements of
1566 // MATCHING_SECTIONS.
1568 Input_section_list::iterator p = input_sections->begin();
1569 while (p != input_sections->end())
1571 Relobj* relobj = p->relobj();
1572 unsigned int shndx = p->shndx();
1573 Input_section_info isi(*p);
1575 // Calling section_name and section_addralign is not very
1578 // Lock the object so that we can get information about the
1579 // section. This is OK since we know we are single-threaded
1582 const Task* task = reinterpret_cast<const Task*>(-1);
1583 Task_lock_obj<Object> tl(task, relobj);
1585 isi.set_section_name(relobj->section_name(shndx));
1586 if (p->is_relaxed_input_section())
1588 // We use current data size because relaxed section sizes may not
1589 // have finalized yet.
1590 isi.set_size(p->relaxed_input_section()->current_data_size());
1591 isi.set_addralign(p->relaxed_input_section()->addralign());
1595 isi.set_size(relobj->section_size(shndx));
1596 isi.set_addralign(relobj->section_addralign(shndx));
1600 if (!this->match_file_name(relobj->name().c_str()))
1602 else if (this->input_section_patterns_.empty())
1604 matching_sections[0].push_back(isi);
1605 p = input_sections->erase(p);
1610 for (i = 0; i < input_pattern_count; ++i)
1612 const Input_section_pattern&
1613 isp(this->input_section_patterns_[i]);
1614 if (match(isi.section_name().c_str(), isp.pattern.c_str(),
1615 isp.pattern_is_wildcard))
1619 if (i >= this->input_section_patterns_.size())
1623 matching_sections[i].push_back(isi);
1624 p = input_sections->erase(p);
1629 // Look through MATCHING_SECTIONS. Sort each one as specified,
1630 // using a stable sort so that we get the default order when
1631 // sections are otherwise equal. Add each input section to the
1634 uint64_t dot = *dot_value;
1635 for (size_t i = 0; i < input_pattern_count; ++i)
1637 if (matching_sections[i].empty())
1640 gold_assert(output_section != NULL);
1642 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1643 if (isp.sort != SORT_WILDCARD_NONE
1644 || this->filename_sort_ != SORT_WILDCARD_NONE)
1645 std::stable_sort(matching_sections[i].begin(),
1646 matching_sections[i].end(),
1647 Input_section_sorter(this->filename_sort_,
1650 for (std::vector<Input_section_info>::const_iterator p =
1651 matching_sections[i].begin();
1652 p != matching_sections[i].end();
1655 // Override the original address alignment if SUBALIGN is specified
1656 // and is greater than the original alignment. We need to make a
1657 // copy of the input section to modify the alignment.
1658 Output_section::Input_section sis(p->input_section());
1660 uint64_t this_subalign = sis.addralign();
1661 if (!sis.is_input_section())
1662 sis.output_section_data()->finalize_data_size();
1663 uint64_t data_size = sis.data_size();
1664 if (this_subalign < subalign)
1666 this_subalign = subalign;
1667 sis.set_addralign(subalign);
1670 uint64_t address = align_address(dot, this_subalign);
1672 if (address > dot && !fill->empty())
1674 section_size_type length =
1675 convert_to_section_size_type(address - dot);
1676 std::string this_fill = this->get_fill_string(fill, length);
1677 Output_section_data* posd = new Output_data_const(this_fill, 0);
1678 output_section->add_output_section_data(posd);
1679 layout->new_output_section_data_from_script(posd);
1682 output_section->add_script_input_section(sis);
1683 dot = address + data_size;
1687 // An SHF_TLS/SHT_NOBITS section does not take up any
1689 if (output_section == NULL
1690 || (output_section->flags() & elfcpp::SHF_TLS) == 0
1691 || output_section->type() != elfcpp::SHT_NOBITS)
1694 this->final_dot_value_ = *dot_value;
1695 this->final_dot_section_ = *dot_section;
1698 // Print for debugging.
1701 Output_section_element_input::print(FILE* f) const
1706 fprintf(f, "KEEP(");
1708 if (!this->filename_pattern_.empty())
1710 bool need_close_paren = false;
1711 switch (this->filename_sort_)
1713 case SORT_WILDCARD_NONE:
1715 case SORT_WILDCARD_BY_NAME:
1716 fprintf(f, "SORT_BY_NAME(");
1717 need_close_paren = true;
1723 fprintf(f, "%s", this->filename_pattern_.c_str());
1725 if (need_close_paren)
1729 if (!this->input_section_patterns_.empty()
1730 || !this->filename_exclusions_.empty())
1734 bool need_space = false;
1735 if (!this->filename_exclusions_.empty())
1737 fprintf(f, "EXCLUDE_FILE(");
1738 bool need_comma = false;
1739 for (Filename_exclusions::const_iterator p =
1740 this->filename_exclusions_.begin();
1741 p != this->filename_exclusions_.end();
1746 fprintf(f, "%s", p->first.c_str());
1753 for (Input_section_patterns::const_iterator p =
1754 this->input_section_patterns_.begin();
1755 p != this->input_section_patterns_.end();
1761 int close_parens = 0;
1764 case SORT_WILDCARD_NONE:
1766 case SORT_WILDCARD_BY_NAME:
1767 fprintf(f, "SORT_BY_NAME(");
1770 case SORT_WILDCARD_BY_ALIGNMENT:
1771 fprintf(f, "SORT_BY_ALIGNMENT(");
1774 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1775 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1778 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1779 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1786 fprintf(f, "%s", p->pattern.c_str());
1788 for (int i = 0; i < close_parens; ++i)
1803 // An output section.
1805 class Output_section_definition : public Sections_element
1808 typedef Output_section_element::Input_section_list Input_section_list;
1810 Output_section_definition(const char* name, size_t namelen,
1811 const Parser_output_section_header* header);
1813 // Finish the output section with the information in the trailer.
1815 finish(const Parser_output_section_trailer* trailer);
1817 // Add a symbol to be defined.
1819 add_symbol_assignment(const char* name, size_t length, Expression* value,
1820 bool provide, bool hidden);
1822 // Add an assignment to the special dot symbol.
1824 add_dot_assignment(Expression* value);
1826 // Add an assertion.
1828 add_assertion(Expression* check, const char* message, size_t messagelen);
1830 // Add a data item to the current output section.
1832 add_data(int size, bool is_signed, Expression* val);
1834 // Add a setting for the fill value.
1836 add_fill(Expression* val);
1838 // Add an input section specification.
1840 add_input_section(const Input_section_spec* spec, bool keep);
1842 // Return whether the output section is relro.
1845 { return this->is_relro_; }
1847 // Record that the output section is relro.
1850 { this->is_relro_ = true; }
1852 // Create any required output sections.
1854 create_sections(Layout*);
1856 // Add any symbols being defined to the symbol table.
1858 add_symbols_to_table(Symbol_table* symtab);
1860 // Finalize symbols and check assertions.
1862 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
1864 // Return the output section name to use for an input file name and
1867 output_section_name(const char* file_name, const char* section_name,
1868 Output_section***, Script_sections::Section_type*,
1871 // Initialize OSP with an output section.
1873 orphan_section_init(Orphan_section_placement* osp,
1874 Script_sections::Elements_iterator p)
1875 { osp->output_section_init(this->name_, this->output_section_, p); }
1877 // Set the section address.
1879 set_section_addresses(Symbol_table* symtab, Layout* layout,
1880 uint64_t* dot_value, uint64_t*,
1881 uint64_t* load_address);
1883 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1884 // this section is constrained, and the input sections do not match,
1885 // return the constraint, and set *POSD.
1887 check_constraint(Output_section_definition** posd);
1889 // See if this is the alternate output section for a constrained
1890 // output section. If it is, transfer the Output_section and return
1891 // true. Otherwise return false.
1893 alternate_constraint(Output_section_definition*, Section_constraint);
1895 // Get the list of segments to use for an allocated section when
1896 // using a PHDRS clause.
1898 allocate_to_segment(String_list** phdrs_list, bool* orphan);
1900 // Look for an output section by name and return the address, the
1901 // load address, the alignment, and the size. This is used when an
1902 // expression refers to an output section which was not actually
1903 // created. This returns true if the section was found, false
1906 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1909 // Return the associated Output_section if there is one.
1911 get_output_section() const
1912 { return this->output_section_; }
1914 // Print the contents to the FILE. This is for debugging.
1918 // Return the output section type if specified or Script_sections::ST_NONE.
1919 Script_sections::Section_type
1920 section_type() const;
1922 // Store the memory region to use.
1924 set_memory_region(Memory_region*, bool set_vma);
1927 set_section_vma(Expression* address)
1928 { this->address_ = address; }
1931 set_section_lma(Expression* address)
1932 { this->load_address_ = address; }
1935 get_section_name() const
1936 { return this->name_; }
1940 script_section_type_name(Script_section_type);
1942 typedef std::vector<Output_section_element*> Output_section_elements;
1944 // The output section name.
1946 // The address. This may be NULL.
1947 Expression* address_;
1948 // The load address. This may be NULL.
1949 Expression* load_address_;
1950 // The alignment. This may be NULL.
1952 // The input section alignment. This may be NULL.
1953 Expression* subalign_;
1954 // The constraint, if any.
1955 Section_constraint constraint_;
1956 // The fill value. This may be NULL.
1958 // The list of segments this section should go into. This may be
1960 String_list* phdrs_;
1961 // The list of elements defining the section.
1962 Output_section_elements elements_;
1963 // The Output_section created for this definition. This will be
1964 // NULL if none was created.
1965 Output_section* output_section_;
1966 // The address after it has been evaluated.
1967 uint64_t evaluated_address_;
1968 // The load address after it has been evaluated.
1969 uint64_t evaluated_load_address_;
1970 // The alignment after it has been evaluated.
1971 uint64_t evaluated_addralign_;
1972 // The output section is relro.
1974 // The output section type if specified.
1975 enum Script_section_type script_section_type_;
1980 Output_section_definition::Output_section_definition(
1983 const Parser_output_section_header* header)
1984 : name_(name, namelen),
1985 address_(header->address),
1986 load_address_(header->load_address),
1987 align_(header->align),
1988 subalign_(header->subalign),
1989 constraint_(header->constraint),
1993 output_section_(NULL),
1994 evaluated_address_(0),
1995 evaluated_load_address_(0),
1996 evaluated_addralign_(0),
1998 script_section_type_(header->section_type)
2002 // Finish an output section.
2005 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
2007 this->fill_ = trailer->fill;
2008 this->phdrs_ = trailer->phdrs;
2011 // Add a symbol to be defined.
2014 Output_section_definition::add_symbol_assignment(const char* name,
2020 Output_section_element* p = new Output_section_element_assignment(name,
2025 this->elements_.push_back(p);
2028 // Add an assignment to the special dot symbol.
2031 Output_section_definition::add_dot_assignment(Expression* value)
2033 Output_section_element* p = new Output_section_element_dot_assignment(value);
2034 this->elements_.push_back(p);
2037 // Add an assertion.
2040 Output_section_definition::add_assertion(Expression* check,
2041 const char* message,
2044 Output_section_element* p = new Output_section_element_assertion(check,
2047 this->elements_.push_back(p);
2050 // Add a data item to the current output section.
2053 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
2055 Output_section_element* p = new Output_section_element_data(size, is_signed,
2057 this->elements_.push_back(p);
2060 // Add a setting for the fill value.
2063 Output_section_definition::add_fill(Expression* val)
2065 Output_section_element* p = new Output_section_element_fill(val);
2066 this->elements_.push_back(p);
2069 // Add an input section specification.
2072 Output_section_definition::add_input_section(const Input_section_spec* spec,
2075 Output_section_element* p = new Output_section_element_input(spec, keep);
2076 this->elements_.push_back(p);
2079 // Create any required output sections. We need an output section if
2080 // there is a data statement here.
2083 Output_section_definition::create_sections(Layout* layout)
2085 if (this->output_section_ != NULL)
2087 for (Output_section_elements::const_iterator p = this->elements_.begin();
2088 p != this->elements_.end();
2091 if ((*p)->needs_output_section())
2093 const char* name = this->name_.c_str();
2094 this->output_section_ =
2095 layout->make_output_section_for_script(name, this->section_type());
2101 // Add any symbols being defined to the symbol table.
2104 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
2106 for (Output_section_elements::iterator p = this->elements_.begin();
2107 p != this->elements_.end();
2109 (*p)->add_symbols_to_table(symtab);
2112 // Finalize symbols and check assertions.
2115 Output_section_definition::finalize_symbols(Symbol_table* symtab,
2116 const Layout* layout,
2117 uint64_t* dot_value)
2119 if (this->output_section_ != NULL)
2120 *dot_value = this->output_section_->address();
2123 uint64_t address = *dot_value;
2124 if (this->address_ != NULL)
2126 address = this->address_->eval_with_dot(symtab, layout, true,
2130 if (this->align_ != NULL)
2132 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
2135 address = align_address(address, align);
2137 *dot_value = address;
2140 Output_section* dot_section = this->output_section_;
2141 for (Output_section_elements::iterator p = this->elements_.begin();
2142 p != this->elements_.end();
2144 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
2147 // Return the output section name to use for an input section name.
2150 Output_section_definition::output_section_name(
2151 const char* file_name,
2152 const char* section_name,
2153 Output_section*** slot,
2154 Script_sections::Section_type* psection_type,
2157 // Ask each element whether it matches NAME.
2158 for (Output_section_elements::const_iterator p = this->elements_.begin();
2159 p != this->elements_.end();
2162 if ((*p)->match_name(file_name, section_name, keep))
2164 // We found a match for NAME, which means that it should go
2165 // into this output section.
2166 *slot = &this->output_section_;
2167 *psection_type = this->section_type();
2168 return this->name_.c_str();
2172 // We don't know about this section name.
2176 // Return true if memory from START to START + LENGTH is contained
2177 // within a memory region.
2180 Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
2181 uint64_t start, uint64_t length) const
2183 if (this->memory_regions_ == NULL)
2186 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2187 mr != this->memory_regions_->end();
2190 uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
2191 uint64_t l = (*mr)->length()->eval(symtab, layout, false);
2194 && (s + l) >= (start + length))
2201 // Find a memory region that should be used by a given output SECTION.
2202 // If provided set PREVIOUS_SECTION_RETURN to point to the last section
2203 // that used the return memory region.
2206 Script_sections::find_memory_region(
2207 Output_section_definition* section,
2208 bool find_vma_region,
2209 Output_section_definition** previous_section_return)
2211 if (previous_section_return != NULL)
2212 * previous_section_return = NULL;
2214 // Walk the memory regions specified in this script, if any.
2215 if (this->memory_regions_ == NULL)
2218 // The /DISCARD/ section never gets assigned to any region.
2219 if (section->get_section_name() == "/DISCARD/")
2222 Memory_region* first_match = NULL;
2224 // First check to see if a region has been assigned to this section.
2225 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2226 mr != this->memory_regions_->end();
2229 if (find_vma_region)
2231 for (Memory_region::Section_list::const_iterator s =
2232 (*mr)->get_vma_section_list_start();
2233 s != (*mr)->get_vma_section_list_end();
2235 if ((*s) == section)
2237 (*mr)->set_last_section(section);
2243 for (Memory_region::Section_list::const_iterator s =
2244 (*mr)->get_lma_section_list_start();
2245 s != (*mr)->get_lma_section_list_end();
2247 if ((*s) == section)
2249 (*mr)->set_last_section(section);
2254 // Make a note of the first memory region whose attributes
2255 // are compatible with the section. If we do not find an
2256 // explicit region assignment, then we will return this region.
2257 Output_section* out_sec = section->get_output_section();
2258 if (first_match == NULL
2260 && (*mr)->attributes_compatible(out_sec->flags(),
2265 // With LMA computations, if an explicit region has not been specified then
2266 // we will want to set the difference between the VMA and the LMA of the
2267 // section were searching for to be the same as the difference between the
2268 // VMA and LMA of the last section to be added to first matched region.
2269 // Hence, if it was asked for, we return a pointer to the last section
2270 // known to be used by the first matched region.
2271 if (first_match != NULL
2272 && previous_section_return != NULL)
2273 *previous_section_return = first_match->get_last_section();
2278 // Set the section address. Note that the OUTPUT_SECTION_ field will
2279 // be NULL if no input sections were mapped to this output section.
2280 // We still have to adjust dot and process symbol assignments.
2283 Output_section_definition::set_section_addresses(Symbol_table* symtab,
2285 uint64_t* dot_value,
2286 uint64_t* dot_alignment,
2287 uint64_t* load_address)
2289 Memory_region* vma_region = NULL;
2290 Memory_region* lma_region = NULL;
2291 Script_sections* script_sections =
2292 layout->script_options()->script_sections();
2294 uint64_t old_dot_value = *dot_value;
2295 uint64_t old_load_address = *load_address;
2297 // If input section sorting is requested via --section-ordering-file or
2298 // linker plugins, then do it here. This is important because we want
2299 // any sorting specified in the linker scripts, which will be done after
2300 // this, to take precedence. The final order of input sections is then
2301 // guaranteed to be according to the linker script specification.
2302 if (this->output_section_ != NULL
2303 && this->output_section_->input_section_order_specified())
2304 this->output_section_->sort_attached_input_sections();
2306 // Decide the start address for the section. The algorithm is:
2307 // 1) If an address has been specified in a linker script, use that.
2308 // 2) Otherwise if a memory region has been specified for the section,
2309 // use the next free address in the region.
2310 // 3) Otherwise if memory regions have been specified find the first
2311 // region whose attributes are compatible with this section and
2312 // install it into that region.
2313 // 4) Otherwise use the current location counter.
2315 if (this->output_section_ != NULL
2316 // Check for --section-start.
2317 && parameters->options().section_start(this->output_section_->name(),
2320 else if (this->address_ == NULL)
2322 vma_region = script_sections->find_memory_region(this, true, NULL);
2324 if (vma_region != NULL)
2325 address = vma_region->get_current_address()->eval(symtab, layout,
2328 address = *dot_value;
2331 address = this->address_->eval_with_dot(symtab, layout, true,
2332 *dot_value, NULL, NULL,
2333 dot_alignment, false);
2335 if (this->align_ == NULL)
2337 if (this->output_section_ == NULL)
2340 align = this->output_section_->addralign();
2344 Output_section* align_section;
2345 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
2346 NULL, &align_section, NULL, false);
2347 if (align_section != NULL)
2348 gold_warning(_("alignment of section %s is not absolute"),
2349 this->name_.c_str());
2350 if (this->output_section_ != NULL)
2351 this->output_section_->set_addralign(align);
2354 address = align_address(address, align);
2356 uint64_t start_address = address;
2358 *dot_value = address;
2360 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2361 // forced to zero, regardless of what the linker script wants.
2362 if (this->output_section_ != NULL
2363 && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
2364 || this->output_section_->is_noload()))
2365 this->output_section_->set_address(address);
2367 this->evaluated_address_ = address;
2368 this->evaluated_addralign_ = align;
2372 if (this->load_address_ == NULL)
2374 Output_section_definition* previous_section;
2376 // Determine if an LMA region has been set for this section.
2377 lma_region = script_sections->find_memory_region(this, false,
2380 if (lma_region != NULL)
2382 if (previous_section == NULL)
2383 // The LMA address was explicitly set to the given region.
2384 laddr = lma_region->get_current_address()->eval(symtab, layout,
2388 // We are not going to use the discovered lma_region, so
2389 // make sure that we do not update it in the code below.
2392 if (this->address_ != NULL || previous_section == this)
2394 // Either an explicit VMA address has been set, or an
2395 // explicit VMA region has been set, so set the LMA equal to
2401 // The LMA address was not explicitly or implicitly set.
2403 // We have been given the first memory region that is
2404 // compatible with the current section and a pointer to the
2405 // last section to use this region. Set the LMA of this
2406 // section so that the difference between its' VMA and LMA
2407 // is the same as the difference between the VMA and LMA of
2408 // the last section in the given region.
2409 laddr = address + (previous_section->evaluated_load_address_
2410 - previous_section->evaluated_address_);
2414 if (this->output_section_ != NULL)
2415 this->output_section_->set_load_address(laddr);
2419 // Do not set the load address of the output section, if one exists.
2420 // This allows future sections to determine what the load address
2421 // should be. If none is ever set, it will default to being the
2422 // same as the vma address.
2428 laddr = this->load_address_->eval_with_dot(symtab, layout, true,
2430 this->output_section_,
2432 if (this->output_section_ != NULL)
2433 this->output_section_->set_load_address(laddr);
2436 this->evaluated_load_address_ = laddr;
2439 if (this->subalign_ == NULL)
2443 Output_section* subalign_section;
2444 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2446 &subalign_section, NULL,
2448 if (subalign_section != NULL)
2449 gold_warning(_("subalign of section %s is not absolute"),
2450 this->name_.c_str());
2454 if (this->fill_ != NULL)
2456 // FIXME: The GNU linker supports fill values of arbitrary
2458 Output_section* fill_section;
2459 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
2461 NULL, &fill_section,
2463 if (fill_section != NULL)
2464 gold_warning(_("fill of section %s is not absolute"),
2465 this->name_.c_str());
2466 unsigned char fill_buff[4];
2467 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2468 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2471 Input_section_list input_sections;
2472 if (this->output_section_ != NULL)
2474 // Get the list of input sections attached to this output
2475 // section. This will leave the output section with only
2476 // Output_section_data entries.
2477 address += this->output_section_->get_input_sections(address,
2480 *dot_value = address;
2483 Output_section* dot_section = this->output_section_;
2484 for (Output_section_elements::iterator p = this->elements_.begin();
2485 p != this->elements_.end();
2487 (*p)->set_section_addresses(symtab, layout, this->output_section_,
2488 subalign, dot_value, dot_alignment,
2489 &dot_section, &fill, &input_sections);
2491 gold_assert(input_sections.empty());
2493 if (vma_region != NULL)
2495 // Update the VMA region being used by the section now that we know how
2496 // big it is. Use the current address in the region, rather than
2497 // start_address because that might have been aligned upwards and we
2498 // need to allow for the padding.
2499 Expression* addr = vma_region->get_current_address();
2500 uint64_t size = *dot_value - addr->eval(symtab, layout, false);
2502 vma_region->increment_offset(this->get_section_name(), size,
2506 // If the LMA region is different from the VMA region, then increment the
2507 // offset there as well. Note that we use the same "dot_value -
2508 // start_address" formula that is used in the load_address assignment below.
2509 if (lma_region != NULL && lma_region != vma_region)
2510 lma_region->increment_offset(this->get_section_name(),
2511 *dot_value - start_address,
2514 // Compute the load address for the following section.
2515 if (this->output_section_ == NULL)
2516 *load_address = *dot_value;
2517 else if (this->load_address_ == NULL)
2519 if (lma_region == NULL)
2520 *load_address = *dot_value;
2523 lma_region->get_current_address()->eval(symtab, layout, false);
2526 *load_address = (this->output_section_->load_address()
2527 + (*dot_value - start_address));
2529 if (this->output_section_ != NULL)
2531 if (this->is_relro_)
2532 this->output_section_->set_is_relro();
2534 this->output_section_->clear_is_relro();
2536 // If this is a NOLOAD section, keep dot and load address unchanged.
2537 if (this->output_section_->is_noload())
2539 *dot_value = old_dot_value;
2540 *load_address = old_load_address;
2545 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2546 // this section is constrained, and the input sections do not match,
2547 // return the constraint, and set *POSD.
2550 Output_section_definition::check_constraint(Output_section_definition** posd)
2552 switch (this->constraint_)
2554 case CONSTRAINT_NONE:
2555 return CONSTRAINT_NONE;
2557 case CONSTRAINT_ONLY_IF_RO:
2558 if (this->output_section_ != NULL
2559 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2562 return CONSTRAINT_ONLY_IF_RO;
2564 return CONSTRAINT_NONE;
2566 case CONSTRAINT_ONLY_IF_RW:
2567 if (this->output_section_ != NULL
2568 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2571 return CONSTRAINT_ONLY_IF_RW;
2573 return CONSTRAINT_NONE;
2575 case CONSTRAINT_SPECIAL:
2576 if (this->output_section_ != NULL)
2577 gold_error(_("SPECIAL constraints are not implemented"));
2578 return CONSTRAINT_NONE;
2585 // See if this is the alternate output section for a constrained
2586 // output section. If it is, transfer the Output_section and return
2587 // true. Otherwise return false.
2590 Output_section_definition::alternate_constraint(
2591 Output_section_definition* posd,
2592 Section_constraint constraint)
2594 if (this->name_ != posd->name_)
2599 case CONSTRAINT_ONLY_IF_RO:
2600 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2604 case CONSTRAINT_ONLY_IF_RW:
2605 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2613 // We have found the alternate constraint. We just need to move
2614 // over the Output_section. When constraints are used properly,
2615 // THIS should not have an output_section pointer, as all the input
2616 // sections should have matched the other definition.
2618 if (this->output_section_ != NULL)
2619 gold_error(_("mismatched definition for constrained sections"));
2621 this->output_section_ = posd->output_section_;
2622 posd->output_section_ = NULL;
2624 if (this->is_relro_)
2625 this->output_section_->set_is_relro();
2627 this->output_section_->clear_is_relro();
2632 // Get the list of segments to use for an allocated section when using
2636 Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2639 // Update phdrs_list even if we don't have an output section. It
2640 // might be used by the following sections.
2641 if (this->phdrs_ != NULL)
2642 *phdrs_list = this->phdrs_;
2644 if (this->output_section_ == NULL)
2646 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2649 return this->output_section_;
2652 // Look for an output section by name and return the address, the load
2653 // address, the alignment, and the size. This is used when an
2654 // expression refers to an output section which was not actually
2655 // created. This returns true if the section was found, false
2659 Output_section_definition::get_output_section_info(const char* name,
2661 uint64_t* load_address,
2662 uint64_t* addralign,
2663 uint64_t* size) const
2665 if (this->name_ != name)
2668 if (this->output_section_ != NULL)
2670 *address = this->output_section_->address();
2671 if (this->output_section_->has_load_address())
2672 *load_address = this->output_section_->load_address();
2674 *load_address = *address;
2675 *addralign = this->output_section_->addralign();
2676 *size = this->output_section_->current_data_size();
2680 *address = this->evaluated_address_;
2681 *load_address = this->evaluated_load_address_;
2682 *addralign = this->evaluated_addralign_;
2689 // Print for debugging.
2692 Output_section_definition::print(FILE* f) const
2694 fprintf(f, " %s ", this->name_.c_str());
2696 if (this->address_ != NULL)
2698 this->address_->print(f);
2702 if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2704 this->script_section_type_name(this->script_section_type_));
2708 if (this->load_address_ != NULL)
2711 this->load_address_->print(f);
2715 if (this->align_ != NULL)
2717 fprintf(f, "ALIGN(");
2718 this->align_->print(f);
2722 if (this->subalign_ != NULL)
2724 fprintf(f, "SUBALIGN(");
2725 this->subalign_->print(f);
2731 for (Output_section_elements::const_iterator p = this->elements_.begin();
2732 p != this->elements_.end();
2738 if (this->fill_ != NULL)
2741 this->fill_->print(f);
2744 if (this->phdrs_ != NULL)
2746 for (String_list::const_iterator p = this->phdrs_->begin();
2747 p != this->phdrs_->end();
2749 fprintf(f, " :%s", p->c_str());
2755 Script_sections::Section_type
2756 Output_section_definition::section_type() const
2758 switch (this->script_section_type_)
2760 case SCRIPT_SECTION_TYPE_NONE:
2761 return Script_sections::ST_NONE;
2762 case SCRIPT_SECTION_TYPE_NOLOAD:
2763 return Script_sections::ST_NOLOAD;
2764 case SCRIPT_SECTION_TYPE_COPY:
2765 case SCRIPT_SECTION_TYPE_DSECT:
2766 case SCRIPT_SECTION_TYPE_INFO:
2767 case SCRIPT_SECTION_TYPE_OVERLAY:
2768 // There are not really support so we treat them as ST_NONE. The
2769 // parse should have issued errors for them already.
2770 return Script_sections::ST_NONE;
2776 // Return the name of a script section type.
2779 Output_section_definition::script_section_type_name(
2780 Script_section_type script_section_type)
2782 switch (script_section_type)
2784 case SCRIPT_SECTION_TYPE_NONE:
2786 case SCRIPT_SECTION_TYPE_NOLOAD:
2788 case SCRIPT_SECTION_TYPE_DSECT:
2790 case SCRIPT_SECTION_TYPE_COPY:
2792 case SCRIPT_SECTION_TYPE_INFO:
2794 case SCRIPT_SECTION_TYPE_OVERLAY:
2802 Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
2804 gold_assert(mr != NULL);
2805 // Add the current section to the specified region's list.
2806 mr->add_section(this, set_vma);
2809 // An output section created to hold orphaned input sections. These
2810 // do not actually appear in linker scripts. However, for convenience
2811 // when setting the output section addresses, we put a marker to these
2812 // sections in the appropriate place in the list of SECTIONS elements.
2814 class Orphan_output_section : public Sections_element
2817 Orphan_output_section(Output_section* os)
2821 // Return whether the orphan output section is relro. We can just
2822 // check the output section because we always set the flag, if
2823 // needed, just after we create the Orphan_output_section.
2826 { return this->os_->is_relro(); }
2828 // Initialize OSP with an output section. This should have been
2831 orphan_section_init(Orphan_section_placement*,
2832 Script_sections::Elements_iterator)
2833 { gold_unreachable(); }
2835 // Set section addresses.
2837 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2840 // Get the list of segments to use for an allocated section when
2841 // using a PHDRS clause.
2843 allocate_to_segment(String_list**, bool*);
2845 // Return the associated Output_section.
2847 get_output_section() const
2848 { return this->os_; }
2850 // Print for debugging.
2852 print(FILE* f) const
2854 fprintf(f, " marker for orphaned output section %s\n",
2859 Output_section* os_;
2862 // Set section addresses.
2865 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
2866 uint64_t* dot_value,
2868 uint64_t* load_address)
2870 typedef std::list<Output_section::Input_section> Input_section_list;
2872 bool have_load_address = *load_address != *dot_value;
2874 uint64_t address = *dot_value;
2875 address = align_address(address, this->os_->addralign());
2877 // If input section sorting is requested via --section-ordering-file or
2878 // linker plugins, then do it here. This is important because we want
2879 // any sorting specified in the linker scripts, which will be done after
2880 // this, to take precedence. The final order of input sections is then
2881 // guaranteed to be according to the linker script specification.
2882 if (this->os_ != NULL
2883 && this->os_->input_section_order_specified())
2884 this->os_->sort_attached_input_sections();
2886 // For a relocatable link, all orphan sections are put at
2887 // address 0. In general we expect all sections to be at
2888 // address 0 for a relocatable link, but we permit the linker
2889 // script to override that for specific output sections.
2890 if (parameters->options().relocatable())
2894 have_load_address = false;
2897 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
2899 this->os_->set_address(address);
2900 if (have_load_address)
2901 this->os_->set_load_address(align_address(*load_address,
2902 this->os_->addralign()));
2905 Input_section_list input_sections;
2906 address += this->os_->get_input_sections(address, "", &input_sections);
2908 for (Input_section_list::iterator p = input_sections.begin();
2909 p != input_sections.end();
2912 uint64_t addralign = p->addralign();
2913 if (!p->is_input_section())
2914 p->output_section_data()->finalize_data_size();
2915 uint64_t size = p->data_size();
2916 address = align_address(address, addralign);
2917 this->os_->add_script_input_section(*p);
2921 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2922 if (this->os_ == NULL
2923 || (this->os_->flags() & elfcpp::SHF_TLS) == 0
2924 || this->os_->type() != elfcpp::SHT_NOBITS)
2926 if (!have_load_address)
2927 *load_address = address;
2929 *load_address += address - *dot_value;
2931 *dot_value = address;
2935 // Get the list of segments to use for an allocated section when using
2936 // a PHDRS clause. If this is an allocated section, return the
2937 // Output_section. We don't change the list of segments.
2940 Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
2942 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2948 // Class Phdrs_element. A program header from a PHDRS clause.
2953 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2954 bool includes_filehdr, bool includes_phdrs,
2955 bool is_flags_valid, unsigned int flags,
2956 Expression* load_address)
2957 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2958 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2959 flags_(flags), load_address_(load_address), load_address_value_(0),
2963 // Return the name of this segment.
2966 { return this->name_; }
2968 // Return the type of the segment.
2971 { return this->type_; }
2973 // Whether to include the file header.
2975 includes_filehdr() const
2976 { return this->includes_filehdr_; }
2978 // Whether to include the program headers.
2980 includes_phdrs() const
2981 { return this->includes_phdrs_; }
2983 // Return whether there is a load address.
2985 has_load_address() const
2986 { return this->load_address_ != NULL; }
2988 // Evaluate the load address expression if there is one.
2990 eval_load_address(Symbol_table* symtab, Layout* layout)
2992 if (this->load_address_ != NULL)
2993 this->load_address_value_ = this->load_address_->eval(symtab, layout,
2997 // Return the load address.
2999 load_address() const
3001 gold_assert(this->load_address_ != NULL);
3002 return this->load_address_value_;
3005 // Create the segment.
3007 create_segment(Layout* layout)
3009 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
3010 return this->segment_;
3013 // Return the segment.
3016 { return this->segment_; }
3018 // Release the segment.
3021 { this->segment_ = NULL; }
3023 // Set the segment flags if appropriate.
3025 set_flags_if_valid()
3027 if (this->is_flags_valid_)
3028 this->segment_->set_flags(this->flags_);
3031 // Print for debugging.
3036 // The name used in the script.
3038 // The type of the segment (PT_LOAD, etc.).
3040 // Whether this segment includes the file header.
3041 bool includes_filehdr_;
3042 // Whether this segment includes the section headers.
3043 bool includes_phdrs_;
3044 // Whether the flags were explicitly specified.
3045 bool is_flags_valid_;
3046 // The flags for this segment (PF_R, etc.) if specified.
3047 unsigned int flags_;
3048 // The expression for the load address for this segment. This may
3050 Expression* load_address_;
3051 // The actual load address from evaluating the expression.
3052 uint64_t load_address_value_;
3053 // The segment itself.
3054 Output_segment* segment_;
3057 // Print for debugging.
3060 Phdrs_element::print(FILE* f) const
3062 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
3063 if (this->includes_filehdr_)
3064 fprintf(f, " FILEHDR");
3065 if (this->includes_phdrs_)
3066 fprintf(f, " PHDRS");
3067 if (this->is_flags_valid_)
3068 fprintf(f, " FLAGS(%u)", this->flags_);
3069 if (this->load_address_ != NULL)
3072 this->load_address_->print(f);
3078 // Add a memory region.
3081 Script_sections::add_memory_region(const char* name, size_t namelen,
3082 unsigned int attributes,
3083 Expression* start, Expression* length)
3085 if (this->memory_regions_ == NULL)
3086 this->memory_regions_ = new Memory_regions();
3087 else if (this->find_memory_region(name, namelen))
3089 gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
3091 // FIXME: Add a GOLD extension to allow multiple regions with the same
3092 // name. This would amount to a single region covering disjoint blocks
3093 // of memory, which is useful for embedded devices.
3096 // FIXME: Check the length and start values. Currently we allow
3097 // non-constant expressions for these values, whereas LD does not.
3099 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
3100 // describe a region that packs from the end address going down, rather
3101 // than the start address going up. This would be useful for embedded
3104 this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
3108 // Find a memory region.
3111 Script_sections::find_memory_region(const char* name, size_t namelen)
3113 if (this->memory_regions_ == NULL)
3116 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3117 m != this->memory_regions_->end();
3119 if ((*m)->name_match(name, namelen))
3125 // Find a memory region's origin.
3128 Script_sections::find_memory_region_origin(const char* name, size_t namelen)
3130 Memory_region* mr = find_memory_region(name, namelen);
3134 return mr->start_address();
3137 // Find a memory region's length.
3140 Script_sections::find_memory_region_length(const char* name, size_t namelen)
3142 Memory_region* mr = find_memory_region(name, namelen);
3146 return mr->length();
3149 // Set the memory region to use for the current section.
3152 Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
3154 gold_assert(!this->sections_elements_->empty());
3155 this->sections_elements_->back()->set_memory_region(mr, set_vma);
3158 // Class Script_sections.
3160 Script_sections::Script_sections()
3161 : saw_sections_clause_(false),
3162 in_sections_clause_(false),
3163 sections_elements_(NULL),
3164 output_section_(NULL),
3165 memory_regions_(NULL),
3166 phdrs_elements_(NULL),
3167 orphan_section_placement_(NULL),
3168 data_segment_align_start_(),
3169 saw_data_segment_align_(false),
3170 saw_relro_end_(false),
3171 saw_segment_start_expression_(false)
3175 // Start a SECTIONS clause.
3178 Script_sections::start_sections()
3180 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
3181 this->saw_sections_clause_ = true;
3182 this->in_sections_clause_ = true;
3183 if (this->sections_elements_ == NULL)
3184 this->sections_elements_ = new Sections_elements;
3187 // Finish a SECTIONS clause.
3190 Script_sections::finish_sections()
3192 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
3193 this->in_sections_clause_ = false;
3196 // Add a symbol to be defined.
3199 Script_sections::add_symbol_assignment(const char* name, size_t length,
3200 Expression* val, bool provide,
3203 if (this->output_section_ != NULL)
3204 this->output_section_->add_symbol_assignment(name, length, val,
3208 Sections_element* p = new Sections_element_assignment(name, length,
3211 this->sections_elements_->push_back(p);
3215 // Add an assignment to the special dot symbol.
3218 Script_sections::add_dot_assignment(Expression* val)
3220 if (this->output_section_ != NULL)
3221 this->output_section_->add_dot_assignment(val);
3224 // The GNU linker permits assignments to . to appears outside of
3225 // a SECTIONS clause, and treats it as appearing inside, so
3226 // sections_elements_ may be NULL here.
3227 if (this->sections_elements_ == NULL)
3229 this->sections_elements_ = new Sections_elements;
3230 this->saw_sections_clause_ = true;
3233 Sections_element* p = new Sections_element_dot_assignment(val);
3234 this->sections_elements_->push_back(p);
3238 // Add an assertion.
3241 Script_sections::add_assertion(Expression* check, const char* message,
3244 if (this->output_section_ != NULL)
3245 this->output_section_->add_assertion(check, message, messagelen);
3248 Sections_element* p = new Sections_element_assertion(check, message,
3250 this->sections_elements_->push_back(p);
3254 // Start processing entries for an output section.
3257 Script_sections::start_output_section(
3260 const Parser_output_section_header* header)
3262 Output_section_definition* posd = new Output_section_definition(name,
3265 this->sections_elements_->push_back(posd);
3266 gold_assert(this->output_section_ == NULL);
3267 this->output_section_ = posd;
3270 // Stop processing entries for an output section.
3273 Script_sections::finish_output_section(
3274 const Parser_output_section_trailer* trailer)
3276 gold_assert(this->output_section_ != NULL);
3277 this->output_section_->finish(trailer);
3278 this->output_section_ = NULL;
3281 // Add a data item to the current output section.
3284 Script_sections::add_data(int size, bool is_signed, Expression* val)
3286 gold_assert(this->output_section_ != NULL);
3287 this->output_section_->add_data(size, is_signed, val);
3290 // Add a fill value setting to the current output section.
3293 Script_sections::add_fill(Expression* val)
3295 gold_assert(this->output_section_ != NULL);
3296 this->output_section_->add_fill(val);
3299 // Add an input section specification to the current output section.
3302 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3304 gold_assert(this->output_section_ != NULL);
3305 this->output_section_->add_input_section(spec, keep);
3308 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
3309 // subsequent output sections may be relro.
3312 Script_sections::data_segment_align()
3314 if (this->saw_data_segment_align_)
3315 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3316 gold_assert(!this->sections_elements_->empty());
3317 Sections_elements::iterator p = this->sections_elements_->end();
3319 this->data_segment_align_start_ = p;
3320 this->saw_data_segment_align_ = true;
3323 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
3324 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3327 Script_sections::data_segment_relro_end()
3329 if (this->saw_relro_end_)
3330 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3331 "in a linker script"));
3332 this->saw_relro_end_ = true;
3334 if (!this->saw_data_segment_align_)
3335 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3338 Sections_elements::iterator p = this->data_segment_align_start_;
3339 for (++p; p != this->sections_elements_->end(); ++p)
3340 (*p)->set_is_relro();
3344 // Create any required sections.
3347 Script_sections::create_sections(Layout* layout)
3349 if (!this->saw_sections_clause_)
3351 for (Sections_elements::iterator p = this->sections_elements_->begin();
3352 p != this->sections_elements_->end();
3354 (*p)->create_sections(layout);
3357 // Add any symbols we are defining to the symbol table.
3360 Script_sections::add_symbols_to_table(Symbol_table* symtab)
3362 if (!this->saw_sections_clause_)
3364 for (Sections_elements::iterator p = this->sections_elements_->begin();
3365 p != this->sections_elements_->end();
3367 (*p)->add_symbols_to_table(symtab);
3370 // Finalize symbols and check assertions.
3373 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3375 if (!this->saw_sections_clause_)
3377 uint64_t dot_value = 0;
3378 for (Sections_elements::iterator p = this->sections_elements_->begin();
3379 p != this->sections_elements_->end();
3381 (*p)->finalize_symbols(symtab, layout, &dot_value);
3384 // Return the name of the output section to use for an input file name
3385 // and section name.
3388 Script_sections::output_section_name(
3389 const char* file_name,
3390 const char* section_name,
3391 Output_section*** output_section_slot,
3392 Script_sections::Section_type* psection_type,
3395 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3396 p != this->sections_elements_->end();
3399 const char* ret = (*p)->output_section_name(file_name, section_name,
3400 output_section_slot,
3401 psection_type, keep);
3405 // The special name /DISCARD/ means that the input section
3406 // should be discarded.
3407 if (strcmp(ret, "/DISCARD/") == 0)
3409 *output_section_slot = NULL;
3410 *psection_type = Script_sections::ST_NONE;
3417 // If we couldn't find a mapping for the name, the output section
3418 // gets the name of the input section.
3420 *output_section_slot = NULL;
3421 *psection_type = Script_sections::ST_NONE;
3423 return section_name;
3426 // Place a marker for an orphan output section into the SECTIONS
3430 Script_sections::place_orphan(Output_section* os)
3432 Orphan_section_placement* osp = this->orphan_section_placement_;
3435 // Initialize the Orphan_section_placement structure.
3436 osp = new Orphan_section_placement();
3437 for (Sections_elements::iterator p = this->sections_elements_->begin();
3438 p != this->sections_elements_->end();
3440 (*p)->orphan_section_init(osp, p);
3441 gold_assert(!this->sections_elements_->empty());
3442 Sections_elements::iterator last = this->sections_elements_->end();
3444 osp->last_init(last);
3445 this->orphan_section_placement_ = osp;
3448 Orphan_output_section* orphan = new Orphan_output_section(os);
3450 // Look for where to put ORPHAN.
3451 Sections_elements::iterator* where;
3452 if (osp->find_place(os, &where))
3454 if ((**where)->is_relro())
3457 os->clear_is_relro();
3459 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3460 // so that the next one goes after this one.
3461 Sections_elements::iterator p = *where;
3462 gold_assert(p != this->sections_elements_->end());
3464 *where = this->sections_elements_->insert(p, orphan);
3468 os->clear_is_relro();
3469 // We don't have a place to put this orphan section. Put it,
3470 // and all other sections like it, at the end, but before the
3471 // sections which always come at the end.
3472 Sections_elements::iterator last = osp->last_place();
3473 *where = this->sections_elements_->insert(last, orphan);
3477 // Set the addresses of all the output sections. Walk through all the
3478 // elements, tracking the dot symbol. Apply assignments which set
3479 // absolute symbol values, in case they are used when setting dot.
3480 // Fill in data statement values. As we find output sections, set the
3481 // address, set the address of all associated input sections, and
3482 // update dot. Return the segment which should hold the file header
3483 // and segment headers, if any.
3486 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3488 gold_assert(this->saw_sections_clause_);
3490 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3491 // for our representation.
3492 for (Sections_elements::iterator p = this->sections_elements_->begin();
3493 p != this->sections_elements_->end();
3496 Output_section_definition* posd;
3497 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3498 if (failed_constraint != CONSTRAINT_NONE)
3500 Sections_elements::iterator q;
3501 for (q = this->sections_elements_->begin();
3502 q != this->sections_elements_->end();
3507 if ((*q)->alternate_constraint(posd, failed_constraint))
3512 if (q == this->sections_elements_->end())
3513 gold_error(_("no matching section constraint"));
3517 // Force the alignment of the first TLS section to be the maximum
3518 // alignment of all TLS sections.
3519 Output_section* first_tls = NULL;
3520 uint64_t tls_align = 0;
3521 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3522 p != this->sections_elements_->end();
3525 Output_section* os = (*p)->get_output_section();
3526 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3528 if (first_tls == NULL)
3530 if (os->addralign() > tls_align)
3531 tls_align = os->addralign();
3534 if (first_tls != NULL)
3535 first_tls->set_addralign(tls_align);
3537 // For a relocatable link, we implicitly set dot to zero.
3538 uint64_t dot_value = 0;
3539 uint64_t dot_alignment = 0;
3540 uint64_t load_address = 0;
3542 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3543 // to set section addresses. If the script has any SEGMENT_START
3544 // expression, we do not set the section addresses.
3545 bool use_tsection_options =
3546 (!this->saw_segment_start_expression_
3547 && (parameters->options().user_set_Ttext()
3548 || parameters->options().user_set_Tdata()
3549 || parameters->options().user_set_Tbss()));
3551 for (Sections_elements::iterator p = this->sections_elements_->begin();
3552 p != this->sections_elements_->end();
3555 Output_section* os = (*p)->get_output_section();
3557 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3558 // the special sections by names and doing dot assignments.
3559 if (use_tsection_options
3561 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3563 uint64_t new_dot_value = dot_value;
3565 if (parameters->options().user_set_Ttext()
3566 && strcmp(os->name(), ".text") == 0)
3567 new_dot_value = parameters->options().Ttext();
3568 else if (parameters->options().user_set_Tdata()
3569 && strcmp(os->name(), ".data") == 0)
3570 new_dot_value = parameters->options().Tdata();
3571 else if (parameters->options().user_set_Tbss()
3572 && strcmp(os->name(), ".bss") == 0)
3573 new_dot_value = parameters->options().Tbss();
3575 // Update dot and load address if necessary.
3576 if (new_dot_value < dot_value)
3577 gold_error(_("dot may not move backward"));
3578 else if (new_dot_value != dot_value)
3580 dot_value = new_dot_value;
3581 load_address = new_dot_value;
3585 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3589 if (this->phdrs_elements_ != NULL)
3591 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3592 p != this->phdrs_elements_->end();
3594 (*p)->eval_load_address(symtab, layout);
3597 return this->create_segments(layout, dot_alignment);
3600 // Sort the sections in order to put them into segments.
3602 class Sort_output_sections
3605 Sort_output_sections(const Script_sections::Sections_elements* elements)
3606 : elements_(elements)
3610 operator()(const Output_section* os1, const Output_section* os2) const;
3614 script_compare(const Output_section* os1, const Output_section* os2) const;
3617 const Script_sections::Sections_elements* elements_;
3621 Sort_output_sections::operator()(const Output_section* os1,
3622 const Output_section* os2) const
3624 // Sort first by the load address.
3625 uint64_t lma1 = (os1->has_load_address()
3626 ? os1->load_address()
3628 uint64_t lma2 = (os2->has_load_address()
3629 ? os2->load_address()
3634 // Then sort by the virtual address.
3635 if (os1->address() != os2->address())
3636 return os1->address() < os2->address();
3638 // If the linker script says which of these sections is first, go
3639 // with what it says.
3640 int i = this->script_compare(os1, os2);
3644 // Sort PROGBITS before NOBITS.
3645 bool nobits1 = os1->type() == elfcpp::SHT_NOBITS;
3646 bool nobits2 = os2->type() == elfcpp::SHT_NOBITS;
3647 if (nobits1 != nobits2)
3650 // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
3652 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3653 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3655 return nobits1 ? tls1 : tls2;
3657 // Sort non-NOLOAD before NOLOAD.
3658 if (os1->is_noload() && !os2->is_noload())
3660 if (!os1->is_noload() && os2->is_noload())
3663 // The sections seem practically identical. Sort by name to get a
3665 return os1->name() < os2->name();
3668 // Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
3669 // if either OS1 or OS2 is not mentioned. This ensures that we keep
3670 // empty sections in the order in which they appear in a linker
3674 Sort_output_sections::script_compare(const Output_section* os1,
3675 const Output_section* os2) const
3677 if (this->elements_ == NULL)
3680 bool found_os1 = false;
3681 bool found_os2 = false;
3682 for (Script_sections::Sections_elements::const_iterator
3683 p = this->elements_->begin();
3684 p != this->elements_->end();
3687 if (os2 == (*p)->get_output_section())
3693 else if (os1 == (*p)->get_output_section())
3704 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3705 // We treat a section with the SHF_TLS flag set as taking up space
3706 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3707 // space for them in the file.
3710 Script_sections::is_bss_section(const Output_section* os)
3712 return (os->type() == elfcpp::SHT_NOBITS
3713 && (os->flags() & elfcpp::SHF_TLS) == 0);
3716 // Return the size taken by the file header and the program headers.
3719 Script_sections::total_header_size(Layout* layout) const
3721 size_t segment_count = layout->segment_count();
3722 size_t file_header_size;
3723 size_t segment_headers_size;
3724 if (parameters->target().get_size() == 32)
3726 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3727 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3729 else if (parameters->target().get_size() == 64)
3731 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3732 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3737 return file_header_size + segment_headers_size;
3740 // Return the amount we have to subtract from the LMA to accommodate
3741 // headers of the given size. The complication is that the file
3742 // header have to be at the start of a page, as otherwise it will not
3743 // be at the start of the file.
3746 Script_sections::header_size_adjustment(uint64_t lma,
3747 size_t sizeof_headers) const
3749 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3750 uint64_t hdr_lma = lma - sizeof_headers;
3751 hdr_lma &= ~(abi_pagesize - 1);
3752 return lma - hdr_lma;
3755 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3756 // the segment which should hold the file header and segment headers,
3760 Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
3762 gold_assert(this->saw_sections_clause_);
3764 if (parameters->options().relocatable())
3767 if (this->saw_phdrs_clause())
3768 return create_segments_from_phdrs_clause(layout, dot_alignment);
3770 Layout::Section_list sections;
3771 layout->get_allocated_sections(§ions);
3773 // Sort the sections by address.
3774 std::stable_sort(sections.begin(), sections.end(),
3775 Sort_output_sections(this->sections_elements_));
3777 this->create_note_and_tls_segments(layout, §ions);
3779 // Walk through the sections adding them to PT_LOAD segments.
3780 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3781 Output_segment* first_seg = NULL;
3782 Output_segment* current_seg = NULL;
3783 bool is_current_seg_readonly = true;
3784 Layout::Section_list::iterator plast = sections.end();
3785 uint64_t last_vma = 0;
3786 uint64_t last_lma = 0;
3787 uint64_t last_size = 0;
3788 for (Layout::Section_list::iterator p = sections.begin();
3789 p != sections.end();
3792 const uint64_t vma = (*p)->address();
3793 const uint64_t lma = ((*p)->has_load_address()
3794 ? (*p)->load_address()
3796 const uint64_t size = (*p)->current_data_size();
3798 bool need_new_segment;
3799 if (current_seg == NULL)
3800 need_new_segment = true;
3801 else if (lma - vma != last_lma - last_vma)
3803 // This section has a different LMA relationship than the
3804 // last one; we need a new segment.
3805 need_new_segment = true;
3807 else if (align_address(last_lma + last_size, abi_pagesize)
3808 < align_address(lma, abi_pagesize))
3810 // Putting this section in the segment would require
3812 need_new_segment = true;
3814 else if (is_bss_section(*plast) && !is_bss_section(*p))
3816 // A non-BSS section can not follow a BSS section in the
3818 need_new_segment = true;
3820 else if (is_current_seg_readonly
3821 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3822 && !parameters->options().omagic())
3824 // Don't put a writable section in the same segment as a
3825 // non-writable section.
3826 need_new_segment = true;
3830 // Otherwise, reuse the existing segment.
3831 need_new_segment = false;
3834 elfcpp::Elf_Word seg_flags =
3835 Layout::section_flags_to_segment((*p)->flags());
3837 if (need_new_segment)
3839 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3841 current_seg->set_addresses(vma, lma);
3842 current_seg->set_minimum_p_align(dot_alignment);
3843 if (first_seg == NULL)
3844 first_seg = current_seg;
3845 is_current_seg_readonly = true;
3848 current_seg->add_output_section_to_load(layout, *p, seg_flags);
3850 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3851 is_current_seg_readonly = false;
3859 // An ELF program should work even if the program headers are not in
3860 // a PT_LOAD segment. However, it appears that the Linux kernel
3861 // does not set the AT_PHDR auxiliary entry in that case. It sets
3862 // the load address to p_vaddr - p_offset of the first PT_LOAD
3863 // segment. It then sets AT_PHDR to the load address plus the
3864 // offset to the program headers, e_phoff in the file header. This
3865 // fails when the program headers appear in the file before the
3866 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3867 // segment to hold the file header and the program headers. This is
3868 // effectively what the GNU linker does, and it is slightly more
3869 // efficient in any case. We try to use the first PT_LOAD segment
3870 // if we can, otherwise we make a new one.
3872 if (first_seg == NULL)
3875 // -n or -N mean that the program is not demand paged and there is
3876 // no need to put the program headers in a PT_LOAD segment.
3877 if (parameters->options().nmagic() || parameters->options().omagic())
3880 size_t sizeof_headers = this->total_header_size(layout);
3882 uint64_t vma = first_seg->vaddr();
3883 uint64_t lma = first_seg->paddr();
3885 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3887 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3889 first_seg->set_addresses(vma - subtract, lma - subtract);
3893 // If there is no room to squeeze in the headers, then punt. The
3894 // resulting executable probably won't run on GNU/Linux, but we
3895 // trust that the user knows what they are doing.
3896 if (lma < subtract || vma < subtract)
3899 // If memory regions have been specified and the address range
3900 // we are about to use is not contained within any region then
3901 // issue a warning message about the segment we are going to
3902 // create. It will be outside of any region and so possibly
3903 // using non-existent or protected memory. We test LMA rather
3904 // than VMA since we assume that the headers will never be
3906 if (this->memory_regions_ != NULL
3907 && !this->block_in_region (NULL, layout, lma - subtract, subtract))
3908 gold_warning(_("creating a segment to contain the file and program"
3909 " headers outside of any MEMORY region"));
3911 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3913 load_seg->set_addresses(vma - subtract, lma - subtract);
3918 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3919 // segment if there are any SHT_TLS sections.
3922 Script_sections::create_note_and_tls_segments(
3924 const Layout::Section_list* sections)
3926 gold_assert(!this->saw_phdrs_clause());
3928 bool saw_tls = false;
3929 for (Layout::Section_list::const_iterator p = sections->begin();
3930 p != sections->end();
3933 if ((*p)->type() == elfcpp::SHT_NOTE)
3935 elfcpp::Elf_Word seg_flags =
3936 Layout::section_flags_to_segment((*p)->flags());
3937 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3939 oseg->add_output_section_to_nonload(*p, seg_flags);
3941 // Incorporate any subsequent SHT_NOTE sections, in the
3942 // hopes that the script is sensible.
3943 Layout::Section_list::const_iterator pnext = p + 1;
3944 while (pnext != sections->end()
3945 && (*pnext)->type() == elfcpp::SHT_NOTE)
3947 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3948 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3954 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3957 gold_error(_("TLS sections are not adjacent"));
3959 elfcpp::Elf_Word seg_flags =
3960 Layout::section_flags_to_segment((*p)->flags());
3961 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3963 oseg->add_output_section_to_nonload(*p, seg_flags);
3965 Layout::Section_list::const_iterator pnext = p + 1;
3966 while (pnext != sections->end()
3967 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3969 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3970 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3978 // If we are making a shared library, and we see a section named
3979 // .interp then put the .interp section in a PT_INTERP segment.
3980 // This is for GNU ld compatibility.
3981 if (strcmp((*p)->name(), ".interp") == 0)
3983 elfcpp::Elf_Word seg_flags =
3984 Layout::section_flags_to_segment((*p)->flags());
3985 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_INTERP,
3987 oseg->add_output_section_to_nonload(*p, seg_flags);
3992 // Add a program header. The PHDRS clause is syntactically distinct
3993 // from the SECTIONS clause, but we implement it with the SECTIONS
3994 // support because PHDRS is useless if there is no SECTIONS clause.
3997 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3998 bool includes_filehdr, bool includes_phdrs,
3999 bool is_flags_valid, unsigned int flags,
4000 Expression* load_address)
4002 if (this->phdrs_elements_ == NULL)
4003 this->phdrs_elements_ = new Phdrs_elements();
4004 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
4007 is_flags_valid, flags,
4011 // Return the number of segments we expect to create based on the
4012 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
4015 Script_sections::expected_segment_count(const Layout* layout) const
4017 if (this->saw_phdrs_clause())
4018 return this->phdrs_elements_->size();
4020 Layout::Section_list sections;
4021 layout->get_allocated_sections(§ions);
4023 // We assume that we will need two PT_LOAD segments.
4026 bool saw_note = false;
4027 bool saw_tls = false;
4028 for (Layout::Section_list::const_iterator p = sections.begin();
4029 p != sections.end();
4032 if ((*p)->type() == elfcpp::SHT_NOTE)
4034 // Assume that all note sections will fit into a single
4042 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
4044 // There can only be one PT_TLS segment.
4056 // Create the segments from a PHDRS clause. Return the segment which
4057 // should hold the file header and program headers, if any.
4060 Script_sections::create_segments_from_phdrs_clause(Layout* layout,
4061 uint64_t dot_alignment)
4063 this->attach_sections_using_phdrs_clause(layout);
4064 return this->set_phdrs_clause_addresses(layout, dot_alignment);
4067 // Create the segments from the PHDRS clause, and put the output
4068 // sections in them.
4071 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
4073 typedef std::map<std::string, Output_segment*> Name_to_segment;
4074 Name_to_segment name_to_segment;
4075 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4076 p != this->phdrs_elements_->end();
4078 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
4080 // Walk through the output sections and attach them to segments.
4081 // Output sections in the script which do not list segments are
4082 // attached to the same set of segments as the immediately preceding
4085 String_list* phdr_names = NULL;
4086 bool load_segments_only = false;
4087 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4088 p != this->sections_elements_->end();
4092 String_list* old_phdr_names = phdr_names;
4093 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &is_orphan);
4097 elfcpp::Elf_Word seg_flags =
4098 Layout::section_flags_to_segment(os->flags());
4100 if (phdr_names == NULL)
4102 // Don't worry about empty orphan sections.
4103 if (is_orphan && os->current_data_size() > 0)
4104 gold_error(_("allocated section %s not in any segment"),
4107 // To avoid later crashes drop this section into the first
4109 for (Phdrs_elements::const_iterator ppe =
4110 this->phdrs_elements_->begin();
4111 ppe != this->phdrs_elements_->end();
4114 Output_segment* oseg = (*ppe)->segment();
4115 if (oseg->type() == elfcpp::PT_LOAD)
4117 oseg->add_output_section_to_load(layout, os, seg_flags);
4125 // We see a list of segments names. Disable PT_LOAD segment only
4127 if (old_phdr_names != phdr_names)
4128 load_segments_only = false;
4130 // If this is an orphan section--one that was not explicitly
4131 // mentioned in the linker script--then it should not inherit
4132 // any segment type other than PT_LOAD. Otherwise, e.g., the
4133 // PT_INTERP segment will pick up following orphan sections,
4134 // which does not make sense. If this is not an orphan section,
4135 // we trust the linker script.
4138 // Enable PT_LOAD segments only filtering until we see another
4139 // list of segment names.
4140 load_segments_only = true;
4143 bool in_load_segment = false;
4144 for (String_list::const_iterator q = phdr_names->begin();
4145 q != phdr_names->end();
4148 Name_to_segment::const_iterator r = name_to_segment.find(*q);
4149 if (r == name_to_segment.end())
4150 gold_error(_("no segment %s"), q->c_str());
4153 if (load_segments_only
4154 && r->second->type() != elfcpp::PT_LOAD)
4157 if (r->second->type() != elfcpp::PT_LOAD)
4158 r->second->add_output_section_to_nonload(os, seg_flags);
4161 r->second->add_output_section_to_load(layout, os, seg_flags);
4162 if (in_load_segment)
4163 gold_error(_("section in two PT_LOAD segments"));
4164 in_load_segment = true;
4169 if (!in_load_segment)
4170 gold_error(_("allocated section not in any PT_LOAD segment"));
4174 // Set the addresses for segments created from a PHDRS clause. Return
4175 // the segment which should hold the file header and program headers,
4179 Script_sections::set_phdrs_clause_addresses(Layout* layout,
4180 uint64_t dot_alignment)
4182 Output_segment* load_seg = NULL;
4183 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4184 p != this->phdrs_elements_->end();
4187 // Note that we have to set the flags after adding the output
4188 // sections to the segment, as adding an output segment can
4189 // change the flags.
4190 (*p)->set_flags_if_valid();
4192 Output_segment* oseg = (*p)->segment();
4194 if (oseg->type() != elfcpp::PT_LOAD)
4196 // The addresses of non-PT_LOAD segments are set from the
4197 // PT_LOAD segments.
4198 if ((*p)->has_load_address())
4199 gold_error(_("may only specify load address for PT_LOAD segment"));
4203 oseg->set_minimum_p_align(dot_alignment);
4205 // The output sections should have addresses from the SECTIONS
4206 // clause. The addresses don't have to be in order, so find the
4207 // one with the lowest load address. Use that to set the
4208 // address of the segment.
4210 Output_section* osec = oseg->section_with_lowest_load_address();
4213 oseg->set_addresses(0, 0);
4217 uint64_t vma = osec->address();
4218 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
4220 // Override the load address of the section with the load
4221 // address specified for the segment.
4222 if ((*p)->has_load_address())
4224 if (osec->has_load_address())
4225 gold_warning(_("PHDRS load address overrides "
4226 "section %s load address"),
4229 lma = (*p)->load_address();
4232 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
4233 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
4235 // We could support this if we wanted to.
4236 gold_error(_("using only one of FILEHDR and PHDRS is "
4237 "not currently supported"));
4241 size_t sizeof_headers = this->total_header_size(layout);
4242 uint64_t subtract = this->header_size_adjustment(lma,
4244 if (lma >= subtract && vma >= subtract)
4251 gold_error(_("sections loaded on first page without room "
4252 "for file and program headers "
4253 "are not supported"));
4256 if (load_seg != NULL)
4257 gold_error(_("using FILEHDR and PHDRS on more than one "
4258 "PT_LOAD segment is not currently supported"));
4262 oseg->set_addresses(vma, lma);
4268 // Add the file header and segment headers to non-load segments
4269 // specified in the PHDRS clause.
4272 Script_sections::put_headers_in_phdrs(Output_data* file_header,
4273 Output_data* segment_headers)
4275 gold_assert(this->saw_phdrs_clause());
4276 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
4277 p != this->phdrs_elements_->end();
4280 if ((*p)->type() != elfcpp::PT_LOAD)
4282 if ((*p)->includes_phdrs())
4283 (*p)->segment()->add_initial_output_data(segment_headers);
4284 if ((*p)->includes_filehdr())
4285 (*p)->segment()->add_initial_output_data(file_header);
4290 // Look for an output section by name and return the address, the load
4291 // address, the alignment, and the size. This is used when an
4292 // expression refers to an output section which was not actually
4293 // created. This returns true if the section was found, false
4297 Script_sections::get_output_section_info(const char* name, uint64_t* address,
4298 uint64_t* load_address,
4299 uint64_t* addralign,
4300 uint64_t* size) const
4302 if (!this->saw_sections_clause_)
4304 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4305 p != this->sections_elements_->end();
4307 if ((*p)->get_output_section_info(name, address, load_address, addralign,
4313 // Release all Output_segments. This remove all pointers to all
4317 Script_sections::release_segments()
4319 if (this->saw_phdrs_clause())
4321 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4322 p != this->phdrs_elements_->end();
4324 (*p)->release_segment();
4328 // Print the SECTIONS clause to F for debugging.
4331 Script_sections::print(FILE* f) const
4333 if (this->phdrs_elements_ != NULL)
4335 fprintf(f, "PHDRS {\n");
4336 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4337 p != this->phdrs_elements_->end();
4343 if (this->memory_regions_ != NULL)
4345 fprintf(f, "MEMORY {\n");
4346 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
4347 m != this->memory_regions_->end();
4353 if (!this->saw_sections_clause_)
4356 fprintf(f, "SECTIONS {\n");
4358 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4359 p != this->sections_elements_->end();
4366 } // End namespace gold.