1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
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*)
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 // the 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);
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,
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);
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*) const
806 // Set section addresses. This includes applying assignments if the
807 // the 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**, std::string*, Input_section_list*)
871 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
874 // Print for debugging.
879 this->assignment_.print(f);
883 Symbol_assignment assignment_;
886 // An assignment to the dot symbol in an output section.
888 class Output_section_element_dot_assignment : public Output_section_element
891 Output_section_element_dot_assignment(Expression* val)
895 // Finalize the symbol.
897 finalize_symbols(Symbol_table* symtab, const Layout* layout,
898 uint64_t* dot_value, Output_section** dot_section)
900 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
901 *dot_section, dot_section, NULL);
904 // Update the dot symbol while setting section addresses.
906 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
907 uint64_t, uint64_t* dot_value, uint64_t*,
908 Output_section**, std::string*, Input_section_list*);
910 // Print for debugging.
915 this->val_->print(f);
923 // Update the dot symbol while setting section addresses.
926 Output_section_element_dot_assignment::set_section_addresses(
927 Symbol_table* symtab,
929 Output_section* output_section,
932 uint64_t* dot_alignment,
933 Output_section** dot_section,
937 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
938 *dot_value, *dot_section,
939 dot_section, dot_alignment);
940 if (next_dot < *dot_value)
941 gold_error(_("dot may not move backward"));
942 if (next_dot > *dot_value && output_section != NULL)
944 section_size_type length = convert_to_section_size_type(next_dot
946 Output_section_data* posd;
948 posd = new Output_data_zero_fill(length, 0);
951 std::string this_fill = this->get_fill_string(fill, length);
952 posd = new Output_data_const(this_fill, 0);
954 output_section->add_output_section_data(posd);
955 layout->new_output_section_data_from_script(posd);
957 *dot_value = next_dot;
960 // An assertion in an output section.
962 class Output_section_element_assertion : public Output_section_element
965 Output_section_element_assertion(Expression* check, const char* message,
967 : assertion_(check, message, messagelen)
974 this->assertion_.print(f);
978 Script_assertion assertion_;
981 // We use a special instance of Output_section_data to handle BYTE,
982 // SHORT, etc. This permits forward references to symbols in the
985 class Output_data_expression : public Output_section_data
988 Output_data_expression(int size, bool is_signed, Expression* val,
989 const Symbol_table* symtab, const Layout* layout,
990 uint64_t dot_value, Output_section* dot_section)
991 : Output_section_data(size, 0, true),
992 is_signed_(is_signed), val_(val), symtab_(symtab),
993 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
997 // Write the data to the output file.
999 do_write(Output_file*);
1001 // Write the data to a buffer.
1003 do_write_to_buffer(unsigned char*);
1005 // Write to a map file.
1007 do_print_to_mapfile(Mapfile* mapfile) const
1008 { mapfile->print_output_data(this, _("** expression")); }
1011 template<bool big_endian>
1013 endian_write_to_buffer(uint64_t, unsigned char*);
1017 const Symbol_table* symtab_;
1018 const Layout* layout_;
1019 uint64_t dot_value_;
1020 Output_section* dot_section_;
1023 // Write the data element to the output file.
1026 Output_data_expression::do_write(Output_file* of)
1028 unsigned char* view = of->get_output_view(this->offset(), this->data_size());
1029 this->write_to_buffer(view);
1030 of->write_output_view(this->offset(), this->data_size(), view);
1033 // Write the data element to a buffer.
1036 Output_data_expression::do_write_to_buffer(unsigned char* buf)
1038 uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
1039 true, this->dot_value_,
1040 this->dot_section_, NULL, NULL);
1042 if (parameters->target().is_big_endian())
1043 this->endian_write_to_buffer<true>(val, buf);
1045 this->endian_write_to_buffer<false>(val, buf);
1048 template<bool big_endian>
1050 Output_data_expression::endian_write_to_buffer(uint64_t val,
1053 switch (this->data_size())
1056 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
1059 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
1062 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
1065 if (parameters->target().get_size() == 32)
1068 if (this->is_signed_ && (val & 0x80000000) != 0)
1069 val |= 0xffffffff00000000LL;
1071 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
1078 // A data item in an output section.
1080 class Output_section_element_data : public Output_section_element
1083 Output_section_element_data(int size, bool is_signed, Expression* val)
1084 : size_(size), is_signed_(is_signed), val_(val)
1087 // If there is a data item, then we must create an output section.
1089 needs_output_section() const
1092 // Finalize symbols--we just need to update dot.
1094 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1096 { *dot_value += this->size_; }
1098 // Store the value in the section.
1100 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
1101 uint64_t* dot_value, uint64_t*, Output_section**,
1102 std::string*, Input_section_list*);
1104 // Print for debugging.
1109 // The size in bytes.
1111 // Whether the value is signed.
1117 // Store the value in the section.
1120 Output_section_element_data::set_section_addresses(
1121 Symbol_table* symtab,
1125 uint64_t* dot_value,
1127 Output_section** dot_section,
1129 Input_section_list*)
1131 gold_assert(os != NULL);
1132 Output_data_expression* expression =
1133 new Output_data_expression(this->size_, this->is_signed_, this->val_,
1134 symtab, layout, *dot_value, *dot_section);
1135 os->add_output_section_data(expression);
1136 layout->new_output_section_data_from_script(expression);
1137 *dot_value += this->size_;
1140 // Print for debugging.
1143 Output_section_element_data::print(FILE* f) const
1146 switch (this->size_)
1158 if (this->is_signed_)
1166 fprintf(f, " %s(", s);
1167 this->val_->print(f);
1171 // A fill value setting in an output section.
1173 class Output_section_element_fill : public Output_section_element
1176 Output_section_element_fill(Expression* val)
1180 // Update the fill value while setting section addresses.
1182 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1183 uint64_t, uint64_t* dot_value, uint64_t*,
1184 Output_section** dot_section,
1185 std::string* fill, Input_section_list*)
1187 Output_section* fill_section;
1188 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
1189 *dot_value, *dot_section,
1190 &fill_section, NULL);
1191 if (fill_section != NULL)
1192 gold_warning(_("fill value is not absolute"));
1193 // FIXME: The GNU linker supports fill values of arbitrary length.
1194 unsigned char fill_buff[4];
1195 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1196 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
1199 // Print for debugging.
1201 print(FILE* f) const
1203 fprintf(f, " FILL(");
1204 this->val_->print(f);
1209 // The new fill value.
1213 // An input section specification in an output section
1215 class Output_section_element_input : public Output_section_element
1218 Output_section_element_input(const Input_section_spec* spec, bool keep);
1220 // Finalize symbols--just update the value of the dot symbol.
1222 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1223 Output_section** dot_section)
1225 *dot_value = this->final_dot_value_;
1226 *dot_section = this->final_dot_section_;
1229 // See whether we match FILE_NAME and SECTION_NAME as an input
1232 match_name(const char* file_name, const char* section_name) const;
1234 // Set the section address.
1236 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1237 uint64_t subalign, uint64_t* dot_value, uint64_t*,
1238 Output_section**, std::string* fill,
1239 Input_section_list*);
1241 // Print for debugging.
1243 print(FILE* f) const;
1246 // An input section pattern.
1247 struct Input_section_pattern
1249 std::string pattern;
1250 bool pattern_is_wildcard;
1253 Input_section_pattern(const char* patterna, size_t patternlena,
1254 Sort_wildcard sorta)
1255 : pattern(patterna, patternlena),
1256 pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
1261 typedef std::vector<Input_section_pattern> Input_section_patterns;
1263 // Filename_exclusions is a pair of filename pattern and a bool
1264 // indicating whether the filename is a wildcard.
1265 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1267 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1268 // indicates whether this is a wildcard pattern.
1270 match(const char* string, const char* pattern, bool is_wildcard_pattern)
1272 return (is_wildcard_pattern
1273 ? fnmatch(pattern, string, 0) == 0
1274 : strcmp(string, pattern) == 0);
1277 // See if we match a file name.
1279 match_file_name(const char* file_name) const;
1281 // The file name pattern. If this is the empty string, we match all
1283 std::string filename_pattern_;
1284 // Whether the file name pattern is a wildcard.
1285 bool filename_is_wildcard_;
1286 // How the file names should be sorted. This may only be
1287 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1288 Sort_wildcard filename_sort_;
1289 // The list of file names to exclude.
1290 Filename_exclusions filename_exclusions_;
1291 // The list of input section patterns.
1292 Input_section_patterns input_section_patterns_;
1293 // Whether to keep this section when garbage collecting.
1295 // The value of dot after including all matching sections.
1296 uint64_t final_dot_value_;
1297 // The section where dot is defined after including all matching
1299 Output_section* final_dot_section_;
1302 // Construct Output_section_element_input. The parser records strings
1303 // as pointers into a copy of the script file, which will go away when
1304 // parsing is complete. We make sure they are in std::string objects.
1306 Output_section_element_input::Output_section_element_input(
1307 const Input_section_spec* spec,
1309 : filename_pattern_(),
1310 filename_is_wildcard_(false),
1311 filename_sort_(spec->file.sort),
1312 filename_exclusions_(),
1313 input_section_patterns_(),
1315 final_dot_value_(0),
1316 final_dot_section_(NULL)
1318 // The filename pattern "*" is common, and matches all files. Turn
1319 // it into the empty string.
1320 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1321 this->filename_pattern_.assign(spec->file.name.value,
1322 spec->file.name.length);
1323 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
1325 if (spec->input_sections.exclude != NULL)
1327 for (String_list::const_iterator p =
1328 spec->input_sections.exclude->begin();
1329 p != spec->input_sections.exclude->end();
1332 bool is_wildcard = is_wildcard_string((*p).c_str());
1333 this->filename_exclusions_.push_back(std::make_pair(*p,
1338 if (spec->input_sections.sections != NULL)
1340 Input_section_patterns& isp(this->input_section_patterns_);
1341 for (String_sort_list::const_iterator p =
1342 spec->input_sections.sections->begin();
1343 p != spec->input_sections.sections->end();
1345 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1350 // See whether we match FILE_NAME.
1353 Output_section_element_input::match_file_name(const char* file_name) const
1355 if (!this->filename_pattern_.empty())
1357 // If we were called with no filename, we refuse to match a
1358 // pattern which requires a file name.
1359 if (file_name == NULL)
1362 if (!match(file_name, this->filename_pattern_.c_str(),
1363 this->filename_is_wildcard_))
1367 if (file_name != NULL)
1369 // Now we have to see whether FILE_NAME matches one of the
1370 // exclusion patterns, if any.
1371 for (Filename_exclusions::const_iterator p =
1372 this->filename_exclusions_.begin();
1373 p != this->filename_exclusions_.end();
1376 if (match(file_name, p->first.c_str(), p->second))
1384 // See whether we match FILE_NAME and SECTION_NAME.
1387 Output_section_element_input::match_name(const char* file_name,
1388 const char* section_name) const
1390 if (!this->match_file_name(file_name))
1393 // If there are no section name patterns, then we match.
1394 if (this->input_section_patterns_.empty())
1397 // See whether we match the section name patterns.
1398 for (Input_section_patterns::const_iterator p =
1399 this->input_section_patterns_.begin();
1400 p != this->input_section_patterns_.end();
1403 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1407 // We didn't match any section names, so we didn't match.
1411 // Information we use to sort the input sections.
1413 class Input_section_info
1416 Input_section_info(const Output_section::Input_section& input_section)
1417 : input_section_(input_section), section_name_(),
1418 size_(0), addralign_(1)
1421 // Return the simple input section.
1422 const Output_section::Input_section&
1423 input_section() const
1424 { return this->input_section_; }
1426 // Return the object.
1429 { return this->input_section_.relobj(); }
1431 // Return the section index.
1434 { return this->input_section_.shndx(); }
1436 // Return the section name.
1438 section_name() const
1439 { return this->section_name_; }
1441 // Set the section name.
1443 set_section_name(const std::string name)
1444 { this->section_name_ = name; }
1446 // Return the section size.
1449 { return this->size_; }
1451 // Set the section size.
1453 set_size(uint64_t size)
1454 { this->size_ = size; }
1456 // Return the address alignment.
1459 { return this->addralign_; }
1461 // Set the address alignment.
1463 set_addralign(uint64_t addralign)
1464 { this->addralign_ = addralign; }
1467 // Input section, can be a relaxed section.
1468 Output_section::Input_section input_section_;
1469 // Name of the section.
1470 std::string section_name_;
1473 // Address alignment.
1474 uint64_t addralign_;
1477 // A class to sort the input sections.
1479 class Input_section_sorter
1482 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1483 : filename_sort_(filename_sort), section_sort_(section_sort)
1487 operator()(const Input_section_info&, const Input_section_info&) const;
1490 Sort_wildcard filename_sort_;
1491 Sort_wildcard section_sort_;
1495 Input_section_sorter::operator()(const Input_section_info& isi1,
1496 const Input_section_info& isi2) const
1498 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1499 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1500 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1501 && isi1.addralign() == isi2.addralign()))
1503 if (isi1.section_name() != isi2.section_name())
1504 return isi1.section_name() < isi2.section_name();
1506 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1507 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1508 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1510 if (isi1.addralign() != isi2.addralign())
1511 return isi1.addralign() < isi2.addralign();
1513 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1515 if (isi1.relobj()->name() != isi2.relobj()->name())
1516 return (isi1.relobj()->name() < isi2.relobj()->name());
1519 // Otherwise we leave them in the same order.
1523 // Set the section address. Look in INPUT_SECTIONS for sections which
1524 // match this spec, sort them as specified, and add them to the output
1528 Output_section_element_input::set_section_addresses(
1531 Output_section* output_section,
1533 uint64_t* dot_value,
1535 Output_section** dot_section,
1537 Input_section_list* input_sections)
1539 // We build a list of sections which match each
1540 // Input_section_pattern.
1542 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1543 size_t input_pattern_count = this->input_section_patterns_.size();
1544 if (input_pattern_count == 0)
1545 input_pattern_count = 1;
1546 Matching_sections matching_sections(input_pattern_count);
1548 // Look through the list of sections for this output section. Add
1549 // each one which matches to one of the elements of
1550 // MATCHING_SECTIONS.
1552 Input_section_list::iterator p = input_sections->begin();
1553 while (p != input_sections->end())
1555 Relobj* relobj = p->relobj();
1556 unsigned int shndx = p->shndx();
1557 Input_section_info isi(*p);
1559 // Calling section_name and section_addralign is not very
1562 // Lock the object so that we can get information about the
1563 // section. This is OK since we know we are single-threaded
1566 const Task* task = reinterpret_cast<const Task*>(-1);
1567 Task_lock_obj<Object> tl(task, relobj);
1569 isi.set_section_name(relobj->section_name(shndx));
1570 if (p->is_relaxed_input_section())
1572 // We use current data size because relaxed section sizes may not
1573 // have finalized yet.
1574 isi.set_size(p->relaxed_input_section()->current_data_size());
1575 isi.set_addralign(p->relaxed_input_section()->addralign());
1579 isi.set_size(relobj->section_size(shndx));
1580 isi.set_addralign(relobj->section_addralign(shndx));
1584 if (!this->match_file_name(relobj->name().c_str()))
1586 else if (this->input_section_patterns_.empty())
1588 matching_sections[0].push_back(isi);
1589 p = input_sections->erase(p);
1594 for (i = 0; i < input_pattern_count; ++i)
1596 const Input_section_pattern&
1597 isp(this->input_section_patterns_[i]);
1598 if (match(isi.section_name().c_str(), isp.pattern.c_str(),
1599 isp.pattern_is_wildcard))
1603 if (i >= this->input_section_patterns_.size())
1607 matching_sections[i].push_back(isi);
1608 p = input_sections->erase(p);
1613 // Look through MATCHING_SECTIONS. Sort each one as specified,
1614 // using a stable sort so that we get the default order when
1615 // sections are otherwise equal. Add each input section to the
1618 uint64_t dot = *dot_value;
1619 for (size_t i = 0; i < input_pattern_count; ++i)
1621 if (matching_sections[i].empty())
1624 gold_assert(output_section != NULL);
1626 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1627 if (isp.sort != SORT_WILDCARD_NONE
1628 || this->filename_sort_ != SORT_WILDCARD_NONE)
1629 std::stable_sort(matching_sections[i].begin(),
1630 matching_sections[i].end(),
1631 Input_section_sorter(this->filename_sort_,
1634 for (std::vector<Input_section_info>::const_iterator p =
1635 matching_sections[i].begin();
1636 p != matching_sections[i].end();
1639 // Override the original address alignment if SUBALIGN is specified
1640 // and is greater than the original alignment. We need to make a
1641 // copy of the input section to modify the alignment.
1642 Output_section::Input_section sis(p->input_section());
1644 uint64_t this_subalign = sis.addralign();
1645 if (!sis.is_input_section())
1646 sis.output_section_data()->finalize_data_size();
1647 uint64_t data_size = sis.data_size();
1648 if (this_subalign < subalign)
1650 this_subalign = subalign;
1651 sis.set_addralign(subalign);
1654 uint64_t address = align_address(dot, this_subalign);
1656 if (address > dot && !fill->empty())
1658 section_size_type length =
1659 convert_to_section_size_type(address - dot);
1660 std::string this_fill = this->get_fill_string(fill, length);
1661 Output_section_data* posd = new Output_data_const(this_fill, 0);
1662 output_section->add_output_section_data(posd);
1663 layout->new_output_section_data_from_script(posd);
1666 output_section->add_script_input_section(sis);
1667 dot = address + data_size;
1671 // An SHF_TLS/SHT_NOBITS section does not take up any
1673 if (output_section == NULL
1674 || (output_section->flags() & elfcpp::SHF_TLS) == 0
1675 || output_section->type() != elfcpp::SHT_NOBITS)
1678 this->final_dot_value_ = *dot_value;
1679 this->final_dot_section_ = *dot_section;
1682 // Print for debugging.
1685 Output_section_element_input::print(FILE* f) const
1690 fprintf(f, "KEEP(");
1692 if (!this->filename_pattern_.empty())
1694 bool need_close_paren = false;
1695 switch (this->filename_sort_)
1697 case SORT_WILDCARD_NONE:
1699 case SORT_WILDCARD_BY_NAME:
1700 fprintf(f, "SORT_BY_NAME(");
1701 need_close_paren = true;
1707 fprintf(f, "%s", this->filename_pattern_.c_str());
1709 if (need_close_paren)
1713 if (!this->input_section_patterns_.empty()
1714 || !this->filename_exclusions_.empty())
1718 bool need_space = false;
1719 if (!this->filename_exclusions_.empty())
1721 fprintf(f, "EXCLUDE_FILE(");
1722 bool need_comma = false;
1723 for (Filename_exclusions::const_iterator p =
1724 this->filename_exclusions_.begin();
1725 p != this->filename_exclusions_.end();
1730 fprintf(f, "%s", p->first.c_str());
1737 for (Input_section_patterns::const_iterator p =
1738 this->input_section_patterns_.begin();
1739 p != this->input_section_patterns_.end();
1745 int close_parens = 0;
1748 case SORT_WILDCARD_NONE:
1750 case SORT_WILDCARD_BY_NAME:
1751 fprintf(f, "SORT_BY_NAME(");
1754 case SORT_WILDCARD_BY_ALIGNMENT:
1755 fprintf(f, "SORT_BY_ALIGNMENT(");
1758 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1759 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1762 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1763 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1770 fprintf(f, "%s", p->pattern.c_str());
1772 for (int i = 0; i < close_parens; ++i)
1787 // An output section.
1789 class Output_section_definition : public Sections_element
1792 typedef Output_section_element::Input_section_list Input_section_list;
1794 Output_section_definition(const char* name, size_t namelen,
1795 const Parser_output_section_header* header);
1797 // Finish the output section with the information in the trailer.
1799 finish(const Parser_output_section_trailer* trailer);
1801 // Add a symbol to be defined.
1803 add_symbol_assignment(const char* name, size_t length, Expression* value,
1804 bool provide, bool hidden);
1806 // Add an assignment to the special dot symbol.
1808 add_dot_assignment(Expression* value);
1810 // Add an assertion.
1812 add_assertion(Expression* check, const char* message, size_t messagelen);
1814 // Add a data item to the current output section.
1816 add_data(int size, bool is_signed, Expression* val);
1818 // Add a setting for the fill value.
1820 add_fill(Expression* val);
1822 // Add an input section specification.
1824 add_input_section(const Input_section_spec* spec, bool keep);
1826 // Return whether the output section is relro.
1829 { return this->is_relro_; }
1831 // Record that the output section is relro.
1834 { this->is_relro_ = true; }
1836 // Create any required output sections.
1838 create_sections(Layout*);
1840 // Add any symbols being defined to the symbol table.
1842 add_symbols_to_table(Symbol_table* symtab);
1844 // Finalize symbols and check assertions.
1846 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
1848 // Return the output section name to use for an input file name and
1851 output_section_name(const char* file_name, const char* section_name,
1852 Output_section***, Script_sections::Section_type*);
1854 // Initialize OSP with an output section.
1856 orphan_section_init(Orphan_section_placement* osp,
1857 Script_sections::Elements_iterator p)
1858 { osp->output_section_init(this->name_, this->output_section_, p); }
1860 // Set the section address.
1862 set_section_addresses(Symbol_table* symtab, Layout* layout,
1863 uint64_t* dot_value, uint64_t*,
1864 uint64_t* load_address);
1866 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1867 // this section is constrained, and the input sections do not match,
1868 // return the constraint, and set *POSD.
1870 check_constraint(Output_section_definition** posd);
1872 // See if this is the alternate output section for a constrained
1873 // output section. If it is, transfer the Output_section and return
1874 // true. Otherwise return false.
1876 alternate_constraint(Output_section_definition*, Section_constraint);
1878 // Get the list of segments to use for an allocated section when
1879 // using a PHDRS clause.
1881 allocate_to_segment(String_list** phdrs_list, bool* orphan);
1883 // Look for an output section by name and return the address, the
1884 // load address, the alignment, and the size. This is used when an
1885 // expression refers to an output section which was not actually
1886 // created. This returns true if the section was found, false
1889 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1892 // Return the associated Output_section if there is one.
1894 get_output_section() const
1895 { return this->output_section_; }
1897 // Print the contents to the FILE. This is for debugging.
1901 // Return the output section type if specified or Script_sections::ST_NONE.
1902 Script_sections::Section_type
1903 section_type() const;
1905 // Store the memory region to use.
1907 set_memory_region(Memory_region*, bool set_vma);
1910 set_section_vma(Expression* address)
1911 { this->address_ = address; }
1914 set_section_lma(Expression* address)
1915 { this->load_address_ = address; }
1918 get_section_name() const
1919 { return this->name_; }
1923 script_section_type_name(Script_section_type);
1925 typedef std::vector<Output_section_element*> Output_section_elements;
1927 // The output section name.
1929 // The address. This may be NULL.
1930 Expression* address_;
1931 // The load address. This may be NULL.
1932 Expression* load_address_;
1933 // The alignment. This may be NULL.
1935 // The input section alignment. This may be NULL.
1936 Expression* subalign_;
1937 // The constraint, if any.
1938 Section_constraint constraint_;
1939 // The fill value. This may be NULL.
1941 // The list of segments this section should go into. This may be
1943 String_list* phdrs_;
1944 // The list of elements defining the section.
1945 Output_section_elements elements_;
1946 // The Output_section created for this definition. This will be
1947 // NULL if none was created.
1948 Output_section* output_section_;
1949 // The address after it has been evaluated.
1950 uint64_t evaluated_address_;
1951 // The load address after it has been evaluated.
1952 uint64_t evaluated_load_address_;
1953 // The alignment after it has been evaluated.
1954 uint64_t evaluated_addralign_;
1955 // The output section is relro.
1957 // The output section type if specified.
1958 enum Script_section_type script_section_type_;
1963 Output_section_definition::Output_section_definition(
1966 const Parser_output_section_header* header)
1967 : name_(name, namelen),
1968 address_(header->address),
1969 load_address_(header->load_address),
1970 align_(header->align),
1971 subalign_(header->subalign),
1972 constraint_(header->constraint),
1976 output_section_(NULL),
1977 evaluated_address_(0),
1978 evaluated_load_address_(0),
1979 evaluated_addralign_(0),
1981 script_section_type_(header->section_type)
1985 // Finish an output section.
1988 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1990 this->fill_ = trailer->fill;
1991 this->phdrs_ = trailer->phdrs;
1994 // Add a symbol to be defined.
1997 Output_section_definition::add_symbol_assignment(const char* name,
2003 Output_section_element* p = new Output_section_element_assignment(name,
2008 this->elements_.push_back(p);
2011 // Add an assignment to the special dot symbol.
2014 Output_section_definition::add_dot_assignment(Expression* value)
2016 Output_section_element* p = new Output_section_element_dot_assignment(value);
2017 this->elements_.push_back(p);
2020 // Add an assertion.
2023 Output_section_definition::add_assertion(Expression* check,
2024 const char* message,
2027 Output_section_element* p = new Output_section_element_assertion(check,
2030 this->elements_.push_back(p);
2033 // Add a data item to the current output section.
2036 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
2038 Output_section_element* p = new Output_section_element_data(size, is_signed,
2040 this->elements_.push_back(p);
2043 // Add a setting for the fill value.
2046 Output_section_definition::add_fill(Expression* val)
2048 Output_section_element* p = new Output_section_element_fill(val);
2049 this->elements_.push_back(p);
2052 // Add an input section specification.
2055 Output_section_definition::add_input_section(const Input_section_spec* spec,
2058 Output_section_element* p = new Output_section_element_input(spec, keep);
2059 this->elements_.push_back(p);
2062 // Create any required output sections. We need an output section if
2063 // there is a data statement here.
2066 Output_section_definition::create_sections(Layout* layout)
2068 if (this->output_section_ != NULL)
2070 for (Output_section_elements::const_iterator p = this->elements_.begin();
2071 p != this->elements_.end();
2074 if ((*p)->needs_output_section())
2076 const char* name = this->name_.c_str();
2077 this->output_section_ =
2078 layout->make_output_section_for_script(name, this->section_type());
2084 // Add any symbols being defined to the symbol table.
2087 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
2089 for (Output_section_elements::iterator p = this->elements_.begin();
2090 p != this->elements_.end();
2092 (*p)->add_symbols_to_table(symtab);
2095 // Finalize symbols and check assertions.
2098 Output_section_definition::finalize_symbols(Symbol_table* symtab,
2099 const Layout* layout,
2100 uint64_t* dot_value)
2102 if (this->output_section_ != NULL)
2103 *dot_value = this->output_section_->address();
2106 uint64_t address = *dot_value;
2107 if (this->address_ != NULL)
2109 address = this->address_->eval_with_dot(symtab, layout, true,
2113 if (this->align_ != NULL)
2115 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
2118 address = align_address(address, align);
2120 *dot_value = address;
2123 Output_section* dot_section = this->output_section_;
2124 for (Output_section_elements::iterator p = this->elements_.begin();
2125 p != this->elements_.end();
2127 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
2130 // Return the output section name to use for an input section name.
2133 Output_section_definition::output_section_name(
2134 const char* file_name,
2135 const char* section_name,
2136 Output_section*** slot,
2137 Script_sections::Section_type* psection_type)
2139 // Ask each element whether it matches NAME.
2140 for (Output_section_elements::const_iterator p = this->elements_.begin();
2141 p != this->elements_.end();
2144 if ((*p)->match_name(file_name, section_name))
2146 // We found a match for NAME, which means that it should go
2147 // into this output section.
2148 *slot = &this->output_section_;
2149 *psection_type = this->section_type();
2150 return this->name_.c_str();
2154 // We don't know about this section name.
2158 // Return true if memory from START to START + LENGTH is contained
2159 // within a memory region.
2162 Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
2163 uint64_t start, uint64_t length) const
2165 if (this->memory_regions_ == NULL)
2168 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2169 mr != this->memory_regions_->end();
2172 uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
2173 uint64_t l = (*mr)->length()->eval(symtab, layout, false);
2176 && (s + l) >= (start + length))
2183 // Find a memory region that should be used by a given output SECTION.
2184 // If provided set PREVIOUS_SECTION_RETURN to point to the last section
2185 // that used the return memory region.
2188 Script_sections::find_memory_region(
2189 Output_section_definition* section,
2190 bool find_vma_region,
2191 Output_section_definition** previous_section_return)
2193 if (previous_section_return != NULL)
2194 * previous_section_return = NULL;
2196 // Walk the memory regions specified in this script, if any.
2197 if (this->memory_regions_ == NULL)
2200 // The /DISCARD/ section never gets assigned to any region.
2201 if (section->get_section_name() == "/DISCARD/")
2204 Memory_region* first_match = NULL;
2206 // First check to see if a region has been assigned to this section.
2207 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2208 mr != this->memory_regions_->end();
2211 if (find_vma_region)
2213 for (Memory_region::Section_list::const_iterator s =
2214 (*mr)->get_vma_section_list_start();
2215 s != (*mr)->get_vma_section_list_end();
2217 if ((*s) == section)
2219 (*mr)->set_last_section(section);
2225 for (Memory_region::Section_list::const_iterator s =
2226 (*mr)->get_lma_section_list_start();
2227 s != (*mr)->get_lma_section_list_end();
2229 if ((*s) == section)
2231 (*mr)->set_last_section(section);
2236 // Make a note of the first memory region whose attributes
2237 // are compatible with the section. If we do not find an
2238 // explicit region assignment, then we will return this region.
2239 Output_section* out_sec = section->get_output_section();
2240 if (first_match == NULL
2241 && (*mr)->attributes_compatible(out_sec->flags(),
2246 // With LMA computations, if an explicit region has not been specified then
2247 // we will want to set the difference between the VMA and the LMA of the
2248 // section were searching for to be the same as the difference between the
2249 // VMA and LMA of the last section to be added to first matched region.
2250 // Hence, if it was asked for, we return a pointer to the last section
2251 // known to be used by the first matched region.
2252 if (first_match != NULL
2253 && previous_section_return != NULL)
2254 *previous_section_return = first_match->get_last_section();
2259 // Set the section address. Note that the OUTPUT_SECTION_ field will
2260 // be NULL if no input sections were mapped to this output section.
2261 // We still have to adjust dot and process symbol assignments.
2264 Output_section_definition::set_section_addresses(Symbol_table* symtab,
2266 uint64_t* dot_value,
2267 uint64_t* dot_alignment,
2268 uint64_t* load_address)
2270 Memory_region* vma_region = NULL;
2271 Memory_region* lma_region = NULL;
2272 Script_sections* script_sections =
2273 layout->script_options()->script_sections();
2275 uint64_t old_dot_value = *dot_value;
2276 uint64_t old_load_address = *load_address;
2278 // Decide the start address for the section. The algorithm is:
2279 // 1) If an address has been specified in a linker script, use that.
2280 // 2) Otherwise if a memory region has been specified for the section,
2281 // use the next free address in the region.
2282 // 3) Otherwise if memory regions have been specified find the first
2283 // region whose attributes are compatible with this section and
2284 // install it into that region.
2285 // 4) Otherwise use the current location counter.
2287 if (this->output_section_ != NULL
2288 // Check for --section-start.
2289 && parameters->options().section_start(this->output_section_->name(),
2292 else if (this->address_ == NULL)
2294 vma_region = script_sections->find_memory_region(this, true, NULL);
2296 if (vma_region != NULL)
2297 address = vma_region->get_current_address()->eval(symtab, layout,
2300 address = *dot_value;
2303 address = this->address_->eval_with_dot(symtab, layout, true,
2304 *dot_value, NULL, NULL,
2307 if (this->align_ == NULL)
2309 if (this->output_section_ == NULL)
2312 align = this->output_section_->addralign();
2316 Output_section* align_section;
2317 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
2318 NULL, &align_section, NULL);
2319 if (align_section != NULL)
2320 gold_warning(_("alignment of section %s is not absolute"),
2321 this->name_.c_str());
2322 if (this->output_section_ != NULL)
2323 this->output_section_->set_addralign(align);
2326 address = align_address(address, align);
2328 uint64_t start_address = address;
2330 *dot_value = address;
2332 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2333 // forced to zero, regardless of what the linker script wants.
2334 if (this->output_section_ != NULL
2335 && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
2336 || this->output_section_->is_noload()))
2337 this->output_section_->set_address(address);
2339 this->evaluated_address_ = address;
2340 this->evaluated_addralign_ = align;
2344 if (this->load_address_ == NULL)
2346 Output_section_definition* previous_section;
2348 // Determine if an LMA region has been set for this section.
2349 lma_region = script_sections->find_memory_region(this, false,
2352 if (lma_region != NULL)
2354 if (previous_section == NULL)
2355 // The LMA address was explicitly set to the given region.
2356 laddr = lma_region->get_current_address()->eval(symtab, layout,
2360 // We are not going to use the discovered lma_region, so
2361 // make sure that we do not update it in the code below.
2364 if (this->address_ != NULL || previous_section == this)
2366 // Either an explicit VMA address has been set, or an
2367 // explicit VMA region has been set, so set the LMA equal to
2373 // The LMA address was not explicitly or implicitly set.
2375 // We have been given the first memory region that is
2376 // compatible with the current section and a pointer to the
2377 // last section to use this region. Set the LMA of this
2378 // section so that the difference between its' VMA and LMA
2379 // is the same as the difference between the VMA and LMA of
2380 // the last section in the given region.
2381 laddr = address + (previous_section->evaluated_load_address_
2382 - previous_section->evaluated_address_);
2386 if (this->output_section_ != NULL)
2387 this->output_section_->set_load_address(laddr);
2391 // Do not set the load address of the output section, if one exists.
2392 // This allows future sections to determine what the load address
2393 // should be. If none is ever set, it will default to being the
2394 // same as the vma address.
2400 laddr = this->load_address_->eval_with_dot(symtab, layout, true,
2402 this->output_section_,
2404 if (this->output_section_ != NULL)
2405 this->output_section_->set_load_address(laddr);
2408 this->evaluated_load_address_ = laddr;
2411 if (this->subalign_ == NULL)
2415 Output_section* subalign_section;
2416 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2418 &subalign_section, NULL);
2419 if (subalign_section != NULL)
2420 gold_warning(_("subalign of section %s is not absolute"),
2421 this->name_.c_str());
2425 if (this->fill_ != NULL)
2427 // FIXME: The GNU linker supports fill values of arbitrary
2429 Output_section* fill_section;
2430 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
2432 NULL, &fill_section,
2434 if (fill_section != NULL)
2435 gold_warning(_("fill of section %s is not absolute"),
2436 this->name_.c_str());
2437 unsigned char fill_buff[4];
2438 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2439 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2442 Input_section_list input_sections;
2443 if (this->output_section_ != NULL)
2445 // Get the list of input sections attached to this output
2446 // section. This will leave the output section with only
2447 // Output_section_data entries.
2448 address += this->output_section_->get_input_sections(address,
2451 *dot_value = address;
2454 Output_section* dot_section = this->output_section_;
2455 for (Output_section_elements::iterator p = this->elements_.begin();
2456 p != this->elements_.end();
2458 (*p)->set_section_addresses(symtab, layout, this->output_section_,
2459 subalign, dot_value, dot_alignment,
2460 &dot_section, &fill, &input_sections);
2462 gold_assert(input_sections.empty());
2464 if (vma_region != NULL)
2466 // Update the VMA region being used by the section now that we know how
2467 // big it is. Use the current address in the region, rather than
2468 // start_address because that might have been aligned upwards and we
2469 // need to allow for the padding.
2470 Expression* addr = vma_region->get_current_address();
2471 uint64_t size = *dot_value - addr->eval(symtab, layout, false);
2473 vma_region->increment_offset(this->get_section_name(), size,
2477 // If the LMA region is different from the VMA region, then increment the
2478 // offset there as well. Note that we use the same "dot_value -
2479 // start_address" formula that is used in the load_address assignment below.
2480 if (lma_region != NULL && lma_region != vma_region)
2481 lma_region->increment_offset(this->get_section_name(),
2482 *dot_value - start_address,
2485 // Compute the load address for the following section.
2486 if (this->output_section_ == NULL)
2487 *load_address = *dot_value;
2488 else if (this->load_address_ == NULL)
2490 if (lma_region == NULL)
2491 *load_address = *dot_value;
2494 lma_region->get_current_address()->eval(symtab, layout, false);
2497 *load_address = (this->output_section_->load_address()
2498 + (*dot_value - start_address));
2500 if (this->output_section_ != NULL)
2502 if (this->is_relro_)
2503 this->output_section_->set_is_relro();
2505 this->output_section_->clear_is_relro();
2507 // If this is a NOLOAD section, keep dot and load address unchanged.
2508 if (this->output_section_->is_noload())
2510 *dot_value = old_dot_value;
2511 *load_address = old_load_address;
2516 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2517 // this section is constrained, and the input sections do not match,
2518 // return the constraint, and set *POSD.
2521 Output_section_definition::check_constraint(Output_section_definition** posd)
2523 switch (this->constraint_)
2525 case CONSTRAINT_NONE:
2526 return CONSTRAINT_NONE;
2528 case CONSTRAINT_ONLY_IF_RO:
2529 if (this->output_section_ != NULL
2530 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2533 return CONSTRAINT_ONLY_IF_RO;
2535 return CONSTRAINT_NONE;
2537 case CONSTRAINT_ONLY_IF_RW:
2538 if (this->output_section_ != NULL
2539 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2542 return CONSTRAINT_ONLY_IF_RW;
2544 return CONSTRAINT_NONE;
2546 case CONSTRAINT_SPECIAL:
2547 if (this->output_section_ != NULL)
2548 gold_error(_("SPECIAL constraints are not implemented"));
2549 return CONSTRAINT_NONE;
2556 // See if this is the alternate output section for a constrained
2557 // output section. If it is, transfer the Output_section and return
2558 // true. Otherwise return false.
2561 Output_section_definition::alternate_constraint(
2562 Output_section_definition* posd,
2563 Section_constraint constraint)
2565 if (this->name_ != posd->name_)
2570 case CONSTRAINT_ONLY_IF_RO:
2571 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2575 case CONSTRAINT_ONLY_IF_RW:
2576 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2584 // We have found the alternate constraint. We just need to move
2585 // over the Output_section. When constraints are used properly,
2586 // THIS should not have an output_section pointer, as all the input
2587 // sections should have matched the other definition.
2589 if (this->output_section_ != NULL)
2590 gold_error(_("mismatched definition for constrained sections"));
2592 this->output_section_ = posd->output_section_;
2593 posd->output_section_ = NULL;
2595 if (this->is_relro_)
2596 this->output_section_->set_is_relro();
2598 this->output_section_->clear_is_relro();
2603 // Get the list of segments to use for an allocated section when using
2607 Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2610 // Update phdrs_list even if we don't have an output section. It
2611 // might be used by the following sections.
2612 if (this->phdrs_ != NULL)
2613 *phdrs_list = this->phdrs_;
2615 if (this->output_section_ == NULL)
2617 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2620 return this->output_section_;
2623 // Look for an output section by name and return the address, the load
2624 // address, the alignment, and the size. This is used when an
2625 // expression refers to an output section which was not actually
2626 // created. This returns true if the section was found, false
2630 Output_section_definition::get_output_section_info(const char* name,
2632 uint64_t* load_address,
2633 uint64_t* addralign,
2634 uint64_t* size) const
2636 if (this->name_ != name)
2639 if (this->output_section_ != NULL)
2641 *address = this->output_section_->address();
2642 if (this->output_section_->has_load_address())
2643 *load_address = this->output_section_->load_address();
2645 *load_address = *address;
2646 *addralign = this->output_section_->addralign();
2647 *size = this->output_section_->current_data_size();
2651 *address = this->evaluated_address_;
2652 *load_address = this->evaluated_load_address_;
2653 *addralign = this->evaluated_addralign_;
2660 // Print for debugging.
2663 Output_section_definition::print(FILE* f) const
2665 fprintf(f, " %s ", this->name_.c_str());
2667 if (this->address_ != NULL)
2669 this->address_->print(f);
2673 if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2675 this->script_section_type_name(this->script_section_type_));
2679 if (this->load_address_ != NULL)
2682 this->load_address_->print(f);
2686 if (this->align_ != NULL)
2688 fprintf(f, "ALIGN(");
2689 this->align_->print(f);
2693 if (this->subalign_ != NULL)
2695 fprintf(f, "SUBALIGN(");
2696 this->subalign_->print(f);
2702 for (Output_section_elements::const_iterator p = this->elements_.begin();
2703 p != this->elements_.end();
2709 if (this->fill_ != NULL)
2712 this->fill_->print(f);
2715 if (this->phdrs_ != NULL)
2717 for (String_list::const_iterator p = this->phdrs_->begin();
2718 p != this->phdrs_->end();
2720 fprintf(f, " :%s", p->c_str());
2726 Script_sections::Section_type
2727 Output_section_definition::section_type() const
2729 switch (this->script_section_type_)
2731 case SCRIPT_SECTION_TYPE_NONE:
2732 return Script_sections::ST_NONE;
2733 case SCRIPT_SECTION_TYPE_NOLOAD:
2734 return Script_sections::ST_NOLOAD;
2735 case SCRIPT_SECTION_TYPE_COPY:
2736 case SCRIPT_SECTION_TYPE_DSECT:
2737 case SCRIPT_SECTION_TYPE_INFO:
2738 case SCRIPT_SECTION_TYPE_OVERLAY:
2739 // There are not really support so we treat them as ST_NONE. The
2740 // parse should have issued errors for them already.
2741 return Script_sections::ST_NONE;
2747 // Return the name of a script section type.
2750 Output_section_definition::script_section_type_name(
2751 Script_section_type script_section_type)
2753 switch (script_section_type)
2755 case SCRIPT_SECTION_TYPE_NONE:
2757 case SCRIPT_SECTION_TYPE_NOLOAD:
2759 case SCRIPT_SECTION_TYPE_DSECT:
2761 case SCRIPT_SECTION_TYPE_COPY:
2763 case SCRIPT_SECTION_TYPE_INFO:
2765 case SCRIPT_SECTION_TYPE_OVERLAY:
2773 Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
2775 gold_assert(mr != NULL);
2776 // Add the current section to the specified region's list.
2777 mr->add_section(this, set_vma);
2780 // An output section created to hold orphaned input sections. These
2781 // do not actually appear in linker scripts. However, for convenience
2782 // when setting the output section addresses, we put a marker to these
2783 // sections in the appropriate place in the list of SECTIONS elements.
2785 class Orphan_output_section : public Sections_element
2788 Orphan_output_section(Output_section* os)
2792 // Return whether the orphan output section is relro. We can just
2793 // check the output section because we always set the flag, if
2794 // needed, just after we create the Orphan_output_section.
2797 { return this->os_->is_relro(); }
2799 // Initialize OSP with an output section. This should have been
2802 orphan_section_init(Orphan_section_placement*,
2803 Script_sections::Elements_iterator)
2804 { gold_unreachable(); }
2806 // Set section addresses.
2808 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2811 // Get the list of segments to use for an allocated section when
2812 // using a PHDRS clause.
2814 allocate_to_segment(String_list**, bool*);
2816 // Return the associated Output_section.
2818 get_output_section() const
2819 { return this->os_; }
2821 // Print for debugging.
2823 print(FILE* f) const
2825 fprintf(f, " marker for orphaned output section %s\n",
2830 Output_section* os_;
2833 // Set section addresses.
2836 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
2837 uint64_t* dot_value,
2839 uint64_t* load_address)
2841 typedef std::list<Output_section::Input_section> Input_section_list;
2843 bool have_load_address = *load_address != *dot_value;
2845 uint64_t address = *dot_value;
2846 address = align_address(address, this->os_->addralign());
2848 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
2850 this->os_->set_address(address);
2851 if (have_load_address)
2852 this->os_->set_load_address(align_address(*load_address,
2853 this->os_->addralign()));
2856 Input_section_list input_sections;
2857 address += this->os_->get_input_sections(address, "", &input_sections);
2859 for (Input_section_list::iterator p = input_sections.begin();
2860 p != input_sections.end();
2863 uint64_t addralign = p->addralign();
2864 if (!p->is_input_section())
2865 p->output_section_data()->finalize_data_size();
2866 uint64_t size = p->data_size();
2867 address = align_address(address, addralign);
2868 this->os_->add_script_input_section(*p);
2872 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2873 if (this->os_ == NULL
2874 || (this->os_->flags() & elfcpp::SHF_TLS) == 0
2875 || this->os_->type() != elfcpp::SHT_NOBITS)
2877 if (!have_load_address)
2878 *load_address = address;
2880 *load_address += address - *dot_value;
2882 *dot_value = address;
2886 // Get the list of segments to use for an allocated section when using
2887 // a PHDRS clause. If this is an allocated section, return the
2888 // Output_section. We don't change the list of segments.
2891 Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
2893 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2899 // Class Phdrs_element. A program header from a PHDRS clause.
2904 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2905 bool includes_filehdr, bool includes_phdrs,
2906 bool is_flags_valid, unsigned int flags,
2907 Expression* load_address)
2908 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2909 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2910 flags_(flags), load_address_(load_address), load_address_value_(0),
2914 // Return the name of this segment.
2917 { return this->name_; }
2919 // Return the type of the segment.
2922 { return this->type_; }
2924 // Whether to include the file header.
2926 includes_filehdr() const
2927 { return this->includes_filehdr_; }
2929 // Whether to include the program headers.
2931 includes_phdrs() const
2932 { return this->includes_phdrs_; }
2934 // Return whether there is a load address.
2936 has_load_address() const
2937 { return this->load_address_ != NULL; }
2939 // Evaluate the load address expression if there is one.
2941 eval_load_address(Symbol_table* symtab, Layout* layout)
2943 if (this->load_address_ != NULL)
2944 this->load_address_value_ = this->load_address_->eval(symtab, layout,
2948 // Return the load address.
2950 load_address() const
2952 gold_assert(this->load_address_ != NULL);
2953 return this->load_address_value_;
2956 // Create the segment.
2958 create_segment(Layout* layout)
2960 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2961 return this->segment_;
2964 // Return the segment.
2967 { return this->segment_; }
2969 // Release the segment.
2972 { this->segment_ = NULL; }
2974 // Set the segment flags if appropriate.
2976 set_flags_if_valid()
2978 if (this->is_flags_valid_)
2979 this->segment_->set_flags(this->flags_);
2982 // Print for debugging.
2987 // The name used in the script.
2989 // The type of the segment (PT_LOAD, etc.).
2991 // Whether this segment includes the file header.
2992 bool includes_filehdr_;
2993 // Whether this segment includes the section headers.
2994 bool includes_phdrs_;
2995 // Whether the flags were explicitly specified.
2996 bool is_flags_valid_;
2997 // The flags for this segment (PF_R, etc.) if specified.
2998 unsigned int flags_;
2999 // The expression for the load address for this segment. This may
3001 Expression* load_address_;
3002 // The actual load address from evaluating the expression.
3003 uint64_t load_address_value_;
3004 // The segment itself.
3005 Output_segment* segment_;
3008 // Print for debugging.
3011 Phdrs_element::print(FILE* f) const
3013 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
3014 if (this->includes_filehdr_)
3015 fprintf(f, " FILEHDR");
3016 if (this->includes_phdrs_)
3017 fprintf(f, " PHDRS");
3018 if (this->is_flags_valid_)
3019 fprintf(f, " FLAGS(%u)", this->flags_);
3020 if (this->load_address_ != NULL)
3023 this->load_address_->print(f);
3029 // Add a memory region.
3032 Script_sections::add_memory_region(const char* name, size_t namelen,
3033 unsigned int attributes,
3034 Expression* start, Expression* length)
3036 if (this->memory_regions_ == NULL)
3037 this->memory_regions_ = new Memory_regions();
3038 else if (this->find_memory_region(name, namelen))
3040 gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
3042 // FIXME: Add a GOLD extension to allow multiple regions with the same
3043 // name. This would amount to a single region covering disjoint blocks
3044 // of memory, which is useful for embedded devices.
3047 // FIXME: Check the length and start values. Currently we allow
3048 // non-constant expressions for these values, whereas LD does not.
3050 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
3051 // describe a region that packs from the end address going down, rather
3052 // than the start address going up. This would be useful for embedded
3055 this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
3059 // Find a memory region.
3062 Script_sections::find_memory_region(const char* name, size_t namelen)
3064 if (this->memory_regions_ == NULL)
3067 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3068 m != this->memory_regions_->end();
3070 if ((*m)->name_match(name, namelen))
3076 // Find a memory region's origin.
3079 Script_sections::find_memory_region_origin(const char* name, size_t namelen)
3081 Memory_region* mr = find_memory_region(name, namelen);
3085 return mr->start_address();
3088 // Find a memory region's length.
3091 Script_sections::find_memory_region_length(const char* name, size_t namelen)
3093 Memory_region* mr = find_memory_region(name, namelen);
3097 return mr->length();
3100 // Set the memory region to use for the current section.
3103 Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
3105 gold_assert(!this->sections_elements_->empty());
3106 this->sections_elements_->back()->set_memory_region(mr, set_vma);
3109 // Class Script_sections.
3111 Script_sections::Script_sections()
3112 : saw_sections_clause_(false),
3113 in_sections_clause_(false),
3114 sections_elements_(NULL),
3115 output_section_(NULL),
3116 memory_regions_(NULL),
3117 phdrs_elements_(NULL),
3118 orphan_section_placement_(NULL),
3119 data_segment_align_start_(),
3120 saw_data_segment_align_(false),
3121 saw_relro_end_(false),
3122 saw_segment_start_expression_(false)
3126 // Start a SECTIONS clause.
3129 Script_sections::start_sections()
3131 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
3132 this->saw_sections_clause_ = true;
3133 this->in_sections_clause_ = true;
3134 if (this->sections_elements_ == NULL)
3135 this->sections_elements_ = new Sections_elements;
3138 // Finish a SECTIONS clause.
3141 Script_sections::finish_sections()
3143 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
3144 this->in_sections_clause_ = false;
3147 // Add a symbol to be defined.
3150 Script_sections::add_symbol_assignment(const char* name, size_t length,
3151 Expression* val, bool provide,
3154 if (this->output_section_ != NULL)
3155 this->output_section_->add_symbol_assignment(name, length, val,
3159 Sections_element* p = new Sections_element_assignment(name, length,
3162 this->sections_elements_->push_back(p);
3166 // Add an assignment to the special dot symbol.
3169 Script_sections::add_dot_assignment(Expression* val)
3171 if (this->output_section_ != NULL)
3172 this->output_section_->add_dot_assignment(val);
3175 // The GNU linker permits assignments to . to appears outside of
3176 // a SECTIONS clause, and treats it as appearing inside, so
3177 // sections_elements_ may be NULL here.
3178 if (this->sections_elements_ == NULL)
3180 this->sections_elements_ = new Sections_elements;
3181 this->saw_sections_clause_ = true;
3184 Sections_element* p = new Sections_element_dot_assignment(val);
3185 this->sections_elements_->push_back(p);
3189 // Add an assertion.
3192 Script_sections::add_assertion(Expression* check, const char* message,
3195 if (this->output_section_ != NULL)
3196 this->output_section_->add_assertion(check, message, messagelen);
3199 Sections_element* p = new Sections_element_assertion(check, message,
3201 this->sections_elements_->push_back(p);
3205 // Start processing entries for an output section.
3208 Script_sections::start_output_section(
3211 const Parser_output_section_header* header)
3213 Output_section_definition* posd = new Output_section_definition(name,
3216 this->sections_elements_->push_back(posd);
3217 gold_assert(this->output_section_ == NULL);
3218 this->output_section_ = posd;
3221 // Stop processing entries for an output section.
3224 Script_sections::finish_output_section(
3225 const Parser_output_section_trailer* trailer)
3227 gold_assert(this->output_section_ != NULL);
3228 this->output_section_->finish(trailer);
3229 this->output_section_ = NULL;
3232 // Add a data item to the current output section.
3235 Script_sections::add_data(int size, bool is_signed, Expression* val)
3237 gold_assert(this->output_section_ != NULL);
3238 this->output_section_->add_data(size, is_signed, val);
3241 // Add a fill value setting to the current output section.
3244 Script_sections::add_fill(Expression* val)
3246 gold_assert(this->output_section_ != NULL);
3247 this->output_section_->add_fill(val);
3250 // Add an input section specification to the current output section.
3253 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3255 gold_assert(this->output_section_ != NULL);
3256 this->output_section_->add_input_section(spec, keep);
3259 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
3260 // subsequent output sections may be relro.
3263 Script_sections::data_segment_align()
3265 if (this->saw_data_segment_align_)
3266 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3267 gold_assert(!this->sections_elements_->empty());
3268 Sections_elements::iterator p = this->sections_elements_->end();
3270 this->data_segment_align_start_ = p;
3271 this->saw_data_segment_align_ = true;
3274 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
3275 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3278 Script_sections::data_segment_relro_end()
3280 if (this->saw_relro_end_)
3281 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3282 "in a linker script"));
3283 this->saw_relro_end_ = true;
3285 if (!this->saw_data_segment_align_)
3286 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3289 Sections_elements::iterator p = this->data_segment_align_start_;
3290 for (++p; p != this->sections_elements_->end(); ++p)
3291 (*p)->set_is_relro();
3295 // Create any required sections.
3298 Script_sections::create_sections(Layout* layout)
3300 if (!this->saw_sections_clause_)
3302 for (Sections_elements::iterator p = this->sections_elements_->begin();
3303 p != this->sections_elements_->end();
3305 (*p)->create_sections(layout);
3308 // Add any symbols we are defining to the symbol table.
3311 Script_sections::add_symbols_to_table(Symbol_table* symtab)
3313 if (!this->saw_sections_clause_)
3315 for (Sections_elements::iterator p = this->sections_elements_->begin();
3316 p != this->sections_elements_->end();
3318 (*p)->add_symbols_to_table(symtab);
3321 // Finalize symbols and check assertions.
3324 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3326 if (!this->saw_sections_clause_)
3328 uint64_t dot_value = 0;
3329 for (Sections_elements::iterator p = this->sections_elements_->begin();
3330 p != this->sections_elements_->end();
3332 (*p)->finalize_symbols(symtab, layout, &dot_value);
3335 // Return the name of the output section to use for an input file name
3336 // and section name.
3339 Script_sections::output_section_name(
3340 const char* file_name,
3341 const char* section_name,
3342 Output_section*** output_section_slot,
3343 Script_sections::Section_type* psection_type)
3345 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3346 p != this->sections_elements_->end();
3349 const char* ret = (*p)->output_section_name(file_name, section_name,
3350 output_section_slot,
3355 // The special name /DISCARD/ means that the input section
3356 // should be discarded.
3357 if (strcmp(ret, "/DISCARD/") == 0)
3359 *output_section_slot = NULL;
3360 *psection_type = Script_sections::ST_NONE;
3367 // If we couldn't find a mapping for the name, the output section
3368 // gets the name of the input section.
3370 *output_section_slot = NULL;
3371 *psection_type = Script_sections::ST_NONE;
3373 return section_name;
3376 // Place a marker for an orphan output section into the SECTIONS
3380 Script_sections::place_orphan(Output_section* os)
3382 Orphan_section_placement* osp = this->orphan_section_placement_;
3385 // Initialize the Orphan_section_placement structure.
3386 osp = new Orphan_section_placement();
3387 for (Sections_elements::iterator p = this->sections_elements_->begin();
3388 p != this->sections_elements_->end();
3390 (*p)->orphan_section_init(osp, p);
3391 gold_assert(!this->sections_elements_->empty());
3392 Sections_elements::iterator last = this->sections_elements_->end();
3394 osp->last_init(last);
3395 this->orphan_section_placement_ = osp;
3398 Orphan_output_section* orphan = new Orphan_output_section(os);
3400 // Look for where to put ORPHAN.
3401 Sections_elements::iterator* where;
3402 if (osp->find_place(os, &where))
3404 if ((**where)->is_relro())
3407 os->clear_is_relro();
3409 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3410 // so that the next one goes after this one.
3411 Sections_elements::iterator p = *where;
3412 gold_assert(p != this->sections_elements_->end());
3414 *where = this->sections_elements_->insert(p, orphan);
3418 os->clear_is_relro();
3419 // We don't have a place to put this orphan section. Put it,
3420 // and all other sections like it, at the end, but before the
3421 // sections which always come at the end.
3422 Sections_elements::iterator last = osp->last_place();
3423 *where = this->sections_elements_->insert(last, orphan);
3427 // Set the addresses of all the output sections. Walk through all the
3428 // elements, tracking the dot symbol. Apply assignments which set
3429 // absolute symbol values, in case they are used when setting dot.
3430 // Fill in data statement values. As we find output sections, set the
3431 // address, set the address of all associated input sections, and
3432 // update dot. Return the segment which should hold the file header
3433 // and segment headers, if any.
3436 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3438 gold_assert(this->saw_sections_clause_);
3440 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3441 // for our representation.
3442 for (Sections_elements::iterator p = this->sections_elements_->begin();
3443 p != this->sections_elements_->end();
3446 Output_section_definition* posd;
3447 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3448 if (failed_constraint != CONSTRAINT_NONE)
3450 Sections_elements::iterator q;
3451 for (q = this->sections_elements_->begin();
3452 q != this->sections_elements_->end();
3457 if ((*q)->alternate_constraint(posd, failed_constraint))
3462 if (q == this->sections_elements_->end())
3463 gold_error(_("no matching section constraint"));
3467 // Force the alignment of the first TLS section to be the maximum
3468 // alignment of all TLS sections.
3469 Output_section* first_tls = NULL;
3470 uint64_t tls_align = 0;
3471 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3472 p != this->sections_elements_->end();
3475 Output_section* os = (*p)->get_output_section();
3476 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3478 if (first_tls == NULL)
3480 if (os->addralign() > tls_align)
3481 tls_align = os->addralign();
3484 if (first_tls != NULL)
3485 first_tls->set_addralign(tls_align);
3487 // For a relocatable link, we implicitly set dot to zero.
3488 uint64_t dot_value = 0;
3489 uint64_t dot_alignment = 0;
3490 uint64_t load_address = 0;
3492 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3493 // to set section addresses. If the script has any SEGMENT_START
3494 // expression, we do not set the section addresses.
3495 bool use_tsection_options =
3496 (!this->saw_segment_start_expression_
3497 && (parameters->options().user_set_Ttext()
3498 || parameters->options().user_set_Tdata()
3499 || parameters->options().user_set_Tbss()));
3501 for (Sections_elements::iterator p = this->sections_elements_->begin();
3502 p != this->sections_elements_->end();
3505 Output_section* os = (*p)->get_output_section();
3507 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3508 // the special sections by names and doing dot assignments.
3509 if (use_tsection_options
3511 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3513 uint64_t new_dot_value = dot_value;
3515 if (parameters->options().user_set_Ttext()
3516 && strcmp(os->name(), ".text") == 0)
3517 new_dot_value = parameters->options().Ttext();
3518 else if (parameters->options().user_set_Tdata()
3519 && strcmp(os->name(), ".data") == 0)
3520 new_dot_value = parameters->options().Tdata();
3521 else if (parameters->options().user_set_Tbss()
3522 && strcmp(os->name(), ".bss") == 0)
3523 new_dot_value = parameters->options().Tbss();
3525 // Update dot and load address if necessary.
3526 if (new_dot_value < dot_value)
3527 gold_error(_("dot may not move backward"));
3528 else if (new_dot_value != dot_value)
3530 dot_value = new_dot_value;
3531 load_address = new_dot_value;
3535 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3539 if (this->phdrs_elements_ != NULL)
3541 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3542 p != this->phdrs_elements_->end();
3544 (*p)->eval_load_address(symtab, layout);
3547 return this->create_segments(layout, dot_alignment);
3550 // Sort the sections in order to put them into segments.
3552 class Sort_output_sections
3555 Sort_output_sections(const Script_sections::Sections_elements* elements)
3556 : elements_(elements)
3560 operator()(const Output_section* os1, const Output_section* os2) const;
3564 is_before(const Output_section* os1, const Output_section* os2) const;
3567 const Script_sections::Sections_elements* elements_;
3571 Sort_output_sections::operator()(const Output_section* os1,
3572 const Output_section* os2) const
3574 // Sort first by the load address.
3575 uint64_t lma1 = (os1->has_load_address()
3576 ? os1->load_address()
3578 uint64_t lma2 = (os2->has_load_address()
3579 ? os2->load_address()
3584 // Then sort by the virtual address.
3585 if (os1->address() != os2->address())
3586 return os1->address() < os2->address();
3588 // Sort TLS sections to the end.
3589 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3590 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3594 // Sort PROGBITS before NOBITS.
3595 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
3597 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
3600 // Sort non-NOLOAD before NOLOAD.
3601 if (os1->is_noload() && !os2->is_noload())
3603 if (!os1->is_noload() && os2->is_noload())
3606 // The sections have the same address. Check the section positions
3607 // in accordance with the linker script.
3608 return this->is_before(os1, os2);
3611 // Return true if OS1 comes before OS2 in ELEMENTS_. This ensures
3612 // that we keep empty sections in the order in which they appear in a
3616 Sort_output_sections::is_before(const Output_section* os1,
3617 const Output_section* os2) const
3619 if (this->elements_ == NULL)
3622 for (Script_sections::Sections_elements::const_iterator
3623 p = this->elements_->begin();
3624 p != this->elements_->end();
3627 if (os1 == (*p)->get_output_section())
3629 for (++p; p != this->elements_->end(); ++p)
3630 if (os2 == (*p)->get_output_section())
3639 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3640 // We treat a section with the SHF_TLS flag set as taking up space
3641 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3642 // space for them in the file.
3645 Script_sections::is_bss_section(const Output_section* os)
3647 return (os->type() == elfcpp::SHT_NOBITS
3648 && (os->flags() & elfcpp::SHF_TLS) == 0);
3651 // Return the size taken by the file header and the program headers.
3654 Script_sections::total_header_size(Layout* layout) const
3656 size_t segment_count = layout->segment_count();
3657 size_t file_header_size;
3658 size_t segment_headers_size;
3659 if (parameters->target().get_size() == 32)
3661 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3662 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3664 else if (parameters->target().get_size() == 64)
3666 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3667 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3672 return file_header_size + segment_headers_size;
3675 // Return the amount we have to subtract from the LMA to accomodate
3676 // headers of the given size. The complication is that the file
3677 // header have to be at the start of a page, as otherwise it will not
3678 // be at the start of the file.
3681 Script_sections::header_size_adjustment(uint64_t lma,
3682 size_t sizeof_headers) const
3684 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3685 uint64_t hdr_lma = lma - sizeof_headers;
3686 hdr_lma &= ~(abi_pagesize - 1);
3687 return lma - hdr_lma;
3690 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3691 // the segment which should hold the file header and segment headers,
3695 Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
3697 gold_assert(this->saw_sections_clause_);
3699 if (parameters->options().relocatable())
3702 if (this->saw_phdrs_clause())
3703 return create_segments_from_phdrs_clause(layout, dot_alignment);
3705 Layout::Section_list sections;
3706 layout->get_allocated_sections(§ions);
3708 // Sort the sections by address.
3709 std::stable_sort(sections.begin(), sections.end(),
3710 Sort_output_sections(this->sections_elements_));
3712 this->create_note_and_tls_segments(layout, §ions);
3714 // Walk through the sections adding them to PT_LOAD segments.
3715 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3716 Output_segment* first_seg = NULL;
3717 Output_segment* current_seg = NULL;
3718 bool is_current_seg_readonly = true;
3719 Layout::Section_list::iterator plast = sections.end();
3720 uint64_t last_vma = 0;
3721 uint64_t last_lma = 0;
3722 uint64_t last_size = 0;
3723 for (Layout::Section_list::iterator p = sections.begin();
3724 p != sections.end();
3727 const uint64_t vma = (*p)->address();
3728 const uint64_t lma = ((*p)->has_load_address()
3729 ? (*p)->load_address()
3731 const uint64_t size = (*p)->current_data_size();
3733 bool need_new_segment;
3734 if (current_seg == NULL)
3735 need_new_segment = true;
3736 else if (lma - vma != last_lma - last_vma)
3738 // This section has a different LMA relationship than the
3739 // last one; we need a new segment.
3740 need_new_segment = true;
3742 else if (align_address(last_lma + last_size, abi_pagesize)
3743 < align_address(lma, abi_pagesize))
3745 // Putting this section in the segment would require
3747 need_new_segment = true;
3749 else if (is_bss_section(*plast) && !is_bss_section(*p))
3751 // A non-BSS section can not follow a BSS section in the
3753 need_new_segment = true;
3755 else if (is_current_seg_readonly
3756 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3757 && !parameters->options().omagic())
3759 // Don't put a writable section in the same segment as a
3760 // non-writable section.
3761 need_new_segment = true;
3765 // Otherwise, reuse the existing segment.
3766 need_new_segment = false;
3769 elfcpp::Elf_Word seg_flags =
3770 Layout::section_flags_to_segment((*p)->flags());
3772 if (need_new_segment)
3774 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3776 current_seg->set_addresses(vma, lma);
3777 current_seg->set_minimum_p_align(dot_alignment);
3778 if (first_seg == NULL)
3779 first_seg = current_seg;
3780 is_current_seg_readonly = true;
3783 current_seg->add_output_section_to_load(layout, *p, seg_flags);
3785 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3786 is_current_seg_readonly = false;
3794 // An ELF program should work even if the program headers are not in
3795 // a PT_LOAD segment. However, it appears that the Linux kernel
3796 // does not set the AT_PHDR auxiliary entry in that case. It sets
3797 // the load address to p_vaddr - p_offset of the first PT_LOAD
3798 // segment. It then sets AT_PHDR to the load address plus the
3799 // offset to the program headers, e_phoff in the file header. This
3800 // fails when the program headers appear in the file before the
3801 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3802 // segment to hold the file header and the program headers. This is
3803 // effectively what the GNU linker does, and it is slightly more
3804 // efficient in any case. We try to use the first PT_LOAD segment
3805 // if we can, otherwise we make a new one.
3807 if (first_seg == NULL)
3810 // -n or -N mean that the program is not demand paged and there is
3811 // no need to put the program headers in a PT_LOAD segment.
3812 if (parameters->options().nmagic() || parameters->options().omagic())
3815 size_t sizeof_headers = this->total_header_size(layout);
3817 uint64_t vma = first_seg->vaddr();
3818 uint64_t lma = first_seg->paddr();
3820 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3822 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3824 first_seg->set_addresses(vma - subtract, lma - subtract);
3828 // If there is no room to squeeze in the headers, then punt. The
3829 // resulting executable probably won't run on GNU/Linux, but we
3830 // trust that the user knows what they are doing.
3831 if (lma < subtract || vma < subtract)
3834 // If memory regions have been specified and the address range
3835 // we are about to use is not contained within any region then
3836 // issue a warning message about the segment we are going to
3837 // create. It will be outside of any region and so possibly
3838 // using non-existent or protected memory. We test LMA rather
3839 // than VMA since we assume that the headers will never be
3841 if (this->memory_regions_ != NULL
3842 && !this->block_in_region (NULL, layout, lma - subtract, subtract))
3843 gold_warning(_("creating a segment to contain the file and program"
3844 " headers outside of any MEMORY region"));
3846 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3848 load_seg->set_addresses(vma - subtract, lma - subtract);
3853 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3854 // segment if there are any SHT_TLS sections.
3857 Script_sections::create_note_and_tls_segments(
3859 const Layout::Section_list* sections)
3861 gold_assert(!this->saw_phdrs_clause());
3863 bool saw_tls = false;
3864 for (Layout::Section_list::const_iterator p = sections->begin();
3865 p != sections->end();
3868 if ((*p)->type() == elfcpp::SHT_NOTE)
3870 elfcpp::Elf_Word seg_flags =
3871 Layout::section_flags_to_segment((*p)->flags());
3872 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3874 oseg->add_output_section_to_nonload(*p, seg_flags);
3876 // Incorporate any subsequent SHT_NOTE sections, in the
3877 // hopes that the script is sensible.
3878 Layout::Section_list::const_iterator pnext = p + 1;
3879 while (pnext != sections->end()
3880 && (*pnext)->type() == elfcpp::SHT_NOTE)
3882 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3883 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3889 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3892 gold_error(_("TLS sections are not adjacent"));
3894 elfcpp::Elf_Word seg_flags =
3895 Layout::section_flags_to_segment((*p)->flags());
3896 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3898 oseg->add_output_section_to_nonload(*p, seg_flags);
3900 Layout::Section_list::const_iterator pnext = p + 1;
3901 while (pnext != sections->end()
3902 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3904 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3905 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3915 // Add a program header. The PHDRS clause is syntactically distinct
3916 // from the SECTIONS clause, but we implement it with the SECTIONS
3917 // support because PHDRS is useless if there is no SECTIONS clause.
3920 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3921 bool includes_filehdr, bool includes_phdrs,
3922 bool is_flags_valid, unsigned int flags,
3923 Expression* load_address)
3925 if (this->phdrs_elements_ == NULL)
3926 this->phdrs_elements_ = new Phdrs_elements();
3927 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3930 is_flags_valid, flags,
3934 // Return the number of segments we expect to create based on the
3935 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3938 Script_sections::expected_segment_count(const Layout* layout) const
3940 if (this->saw_phdrs_clause())
3941 return this->phdrs_elements_->size();
3943 Layout::Section_list sections;
3944 layout->get_allocated_sections(§ions);
3946 // We assume that we will need two PT_LOAD segments.
3949 bool saw_note = false;
3950 bool saw_tls = false;
3951 for (Layout::Section_list::const_iterator p = sections.begin();
3952 p != sections.end();
3955 if ((*p)->type() == elfcpp::SHT_NOTE)
3957 // Assume that all note sections will fit into a single
3965 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3967 // There can only be one PT_TLS segment.
3979 // Create the segments from a PHDRS clause. Return the segment which
3980 // should hold the file header and program headers, if any.
3983 Script_sections::create_segments_from_phdrs_clause(Layout* layout,
3984 uint64_t dot_alignment)
3986 this->attach_sections_using_phdrs_clause(layout);
3987 return this->set_phdrs_clause_addresses(layout, dot_alignment);
3990 // Create the segments from the PHDRS clause, and put the output
3991 // sections in them.
3994 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
3996 typedef std::map<std::string, Output_segment*> Name_to_segment;
3997 Name_to_segment name_to_segment;
3998 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3999 p != this->phdrs_elements_->end();
4001 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
4003 // Walk through the output sections and attach them to segments.
4004 // Output sections in the script which do not list segments are
4005 // attached to the same set of segments as the immediately preceding
4008 String_list* phdr_names = NULL;
4009 bool load_segments_only = false;
4010 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4011 p != this->sections_elements_->end();
4015 String_list* old_phdr_names = phdr_names;
4016 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &orphan);
4020 if (phdr_names == NULL)
4022 gold_error(_("allocated section not in any segment"));
4026 // We see a list of segments names. Disable PT_LOAD segment only
4028 if (old_phdr_names != phdr_names)
4029 load_segments_only = false;
4031 // If this is an orphan section--one that was not explicitly
4032 // mentioned in the linker script--then it should not inherit
4033 // any segment type other than PT_LOAD. Otherwise, e.g., the
4034 // PT_INTERP segment will pick up following orphan sections,
4035 // which does not make sense. If this is not an orphan section,
4036 // we trust the linker script.
4039 // Enable PT_LOAD segments only filtering until we see another
4040 // list of segment names.
4041 load_segments_only = true;
4044 bool in_load_segment = false;
4045 for (String_list::const_iterator q = phdr_names->begin();
4046 q != phdr_names->end();
4049 Name_to_segment::const_iterator r = name_to_segment.find(*q);
4050 if (r == name_to_segment.end())
4051 gold_error(_("no segment %s"), q->c_str());
4054 if (load_segments_only
4055 && r->second->type() != elfcpp::PT_LOAD)
4058 elfcpp::Elf_Word seg_flags =
4059 Layout::section_flags_to_segment(os->flags());
4061 if (r->second->type() != elfcpp::PT_LOAD)
4062 r->second->add_output_section_to_nonload(os, seg_flags);
4065 r->second->add_output_section_to_load(layout, os, seg_flags);
4066 if (in_load_segment)
4067 gold_error(_("section in two PT_LOAD segments"));
4068 in_load_segment = true;
4073 if (!in_load_segment)
4074 gold_error(_("allocated section not in any PT_LOAD segment"));
4078 // Set the addresses for segments created from a PHDRS clause. Return
4079 // the segment which should hold the file header and program headers,
4083 Script_sections::set_phdrs_clause_addresses(Layout* layout,
4084 uint64_t dot_alignment)
4086 Output_segment* load_seg = NULL;
4087 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4088 p != this->phdrs_elements_->end();
4091 // Note that we have to set the flags after adding the output
4092 // sections to the segment, as adding an output segment can
4093 // change the flags.
4094 (*p)->set_flags_if_valid();
4096 Output_segment* oseg = (*p)->segment();
4098 if (oseg->type() != elfcpp::PT_LOAD)
4100 // The addresses of non-PT_LOAD segments are set from the
4101 // PT_LOAD segments.
4102 if ((*p)->has_load_address())
4103 gold_error(_("may only specify load address for PT_LOAD segment"));
4107 oseg->set_minimum_p_align(dot_alignment);
4109 // The output sections should have addresses from the SECTIONS
4110 // clause. The addresses don't have to be in order, so find the
4111 // one with the lowest load address. Use that to set the
4112 // address of the segment.
4114 Output_section* osec = oseg->section_with_lowest_load_address();
4117 oseg->set_addresses(0, 0);
4121 uint64_t vma = osec->address();
4122 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
4124 // Override the load address of the section with the load
4125 // address specified for the segment.
4126 if ((*p)->has_load_address())
4128 if (osec->has_load_address())
4129 gold_warning(_("PHDRS load address overrides "
4130 "section %s load address"),
4133 lma = (*p)->load_address();
4136 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
4137 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
4139 // We could support this if we wanted to.
4140 gold_error(_("using only one of FILEHDR and PHDRS is "
4141 "not currently supported"));
4145 size_t sizeof_headers = this->total_header_size(layout);
4146 uint64_t subtract = this->header_size_adjustment(lma,
4148 if (lma >= subtract && vma >= subtract)
4155 gold_error(_("sections loaded on first page without room "
4156 "for file and program headers "
4157 "are not supported"));
4160 if (load_seg != NULL)
4161 gold_error(_("using FILEHDR and PHDRS on more than one "
4162 "PT_LOAD segment is not currently supported"));
4166 oseg->set_addresses(vma, lma);
4172 // Add the file header and segment headers to non-load segments
4173 // specified in the PHDRS clause.
4176 Script_sections::put_headers_in_phdrs(Output_data* file_header,
4177 Output_data* segment_headers)
4179 gold_assert(this->saw_phdrs_clause());
4180 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
4181 p != this->phdrs_elements_->end();
4184 if ((*p)->type() != elfcpp::PT_LOAD)
4186 if ((*p)->includes_phdrs())
4187 (*p)->segment()->add_initial_output_data(segment_headers);
4188 if ((*p)->includes_filehdr())
4189 (*p)->segment()->add_initial_output_data(file_header);
4194 // Look for an output section by name and return the address, the load
4195 // address, the alignment, and the size. This is used when an
4196 // expression refers to an output section which was not actually
4197 // created. This returns true if the section was found, false
4201 Script_sections::get_output_section_info(const char* name, uint64_t* address,
4202 uint64_t* load_address,
4203 uint64_t* addralign,
4204 uint64_t* size) const
4206 if (!this->saw_sections_clause_)
4208 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4209 p != this->sections_elements_->end();
4211 if ((*p)->get_output_section_info(name, address, load_address, addralign,
4217 // Release all Output_segments. This remove all pointers to all
4221 Script_sections::release_segments()
4223 if (this->saw_phdrs_clause())
4225 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4226 p != this->phdrs_elements_->end();
4228 (*p)->release_segment();
4232 // Print the SECTIONS clause to F for debugging.
4235 Script_sections::print(FILE* f) const
4237 if (this->phdrs_elements_ != NULL)
4239 fprintf(f, "PHDRS {\n");
4240 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4241 p != this->phdrs_elements_->end();
4247 if (this->memory_regions_ != NULL)
4249 fprintf(f, "MEMORY {\n");
4250 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
4251 m != this->memory_regions_->end();
4257 if (!this->saw_sections_clause_)
4260 fprintf(f, "SECTIONS {\n");
4262 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4263 p != this->sections_elements_->end();
4270 } // End namespace gold.