1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008 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 // An element in a SECTIONS clause.
48 class Sections_element
54 virtual ~Sections_element()
57 // Create any required output sections. The only real
58 // implementation is in Output_section_definition.
60 create_sections(Layout*)
63 // Add any symbol being defined to the symbol table.
65 add_symbols_to_table(Symbol_table*)
68 // Finalize symbols and check assertions.
70 finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
73 // Return the output section name to use for an input file name and
74 // section name. This only real implementation is in
75 // Output_section_definition.
77 output_section_name(const char*, const char*, Output_section***)
80 // Return whether to place an orphan output section after this
83 place_orphan_here(const Output_section *, bool*) const
86 // Set section addresses. This includes applying assignments if the
87 // the expression is an absolute value.
89 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*)
92 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
93 // this section is constrained, and the input sections do not match,
94 // return the constraint, and set *POSD.
95 virtual Section_constraint
96 check_constraint(Output_section_definition**)
97 { return CONSTRAINT_NONE; }
99 // See if this is the alternate output section for a constrained
100 // output section. If it is, transfer the Output_section and return
101 // true. Otherwise return false.
103 alternate_constraint(Output_section_definition*, Section_constraint)
106 // Get the list of segments to use for an allocated section when
107 // using a PHDRS clause. If this is an allocated section, return
108 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
109 // which it should be attached. If the PHDRS were not specified,
110 // don't change *PHDRS_LIST.
111 virtual Output_section*
112 allocate_to_segment(String_list**)
115 // Print the element for debugging purposes.
117 print(FILE* f) const = 0;
120 // An assignment in a SECTIONS clause outside of an output section.
122 class Sections_element_assignment : public Sections_element
125 Sections_element_assignment(const char* name, size_t namelen,
126 Expression* val, bool provide, bool hidden)
127 : assignment_(name, namelen, val, provide, hidden)
130 // Add the symbol to the symbol table.
132 add_symbols_to_table(Symbol_table* symtab)
133 { this->assignment_.add_to_table(symtab); }
135 // Finalize the symbol.
137 finalize_symbols(Symbol_table* symtab, const Layout* layout,
140 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
143 // Set the section address. There is no section here, but if the
144 // value is absolute, we set the symbol. This permits us to use
145 // absolute symbols when setting dot.
147 set_section_addresses(Symbol_table* symtab, Layout* layout,
148 uint64_t* dot_value, uint64_t*)
150 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
153 // Print for debugging.
158 this->assignment_.print(f);
162 Symbol_assignment assignment_;
165 // An assignment to the dot symbol in a SECTIONS clause outside of an
168 class Sections_element_dot_assignment : public Sections_element
171 Sections_element_dot_assignment(Expression* val)
175 // Finalize the symbol.
177 finalize_symbols(Symbol_table* symtab, const Layout* layout,
180 // We ignore the section of the result because outside of an
181 // output section definition the dot symbol is always considered
183 Output_section* dummy;
184 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
188 // Update the dot symbol while setting section addresses.
190 set_section_addresses(Symbol_table* symtab, Layout* layout,
191 uint64_t* dot_value, uint64_t* load_address)
193 Output_section* dummy;
194 *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
196 *load_address = *dot_value;
199 // Print for debugging.
204 this->val_->print(f);
212 // An assertion in a SECTIONS clause outside of an output section.
214 class Sections_element_assertion : public Sections_element
217 Sections_element_assertion(Expression* check, const char* message,
219 : assertion_(check, message, messagelen)
222 // Check the assertion.
224 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
225 { this->assertion_.check(symtab, layout); }
227 // Print for debugging.
232 this->assertion_.print(f);
236 Script_assertion assertion_;
239 // An element in an output section in a SECTIONS clause.
241 class Output_section_element
244 // A list of input sections.
245 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
247 Output_section_element()
250 virtual ~Output_section_element()
253 // Return whether this element requires an output section to exist.
255 needs_output_section() const
258 // Add any symbol being defined to the symbol table.
260 add_symbols_to_table(Symbol_table*)
263 // Finalize symbols and check assertions.
265 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
268 // Return whether this element matches FILE_NAME and SECTION_NAME.
269 // The only real implementation is in Output_section_element_input.
271 match_name(const char*, const char*) const
274 // Set section addresses. This includes applying assignments if the
275 // the expression is an absolute value.
277 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
278 uint64_t*, Output_section**, std::string*,
282 // Print the element for debugging purposes.
284 print(FILE* f) const = 0;
287 // Return a fill string that is LENGTH bytes long, filling it with
290 get_fill_string(const std::string* fill, section_size_type length) const;
294 Output_section_element::get_fill_string(const std::string* fill,
295 section_size_type length) const
297 std::string this_fill;
298 this_fill.reserve(length);
299 while (this_fill.length() + fill->length() <= length)
301 if (this_fill.length() < length)
302 this_fill.append(*fill, 0, length - this_fill.length());
306 // A symbol assignment in an output section.
308 class Output_section_element_assignment : public Output_section_element
311 Output_section_element_assignment(const char* name, size_t namelen,
312 Expression* val, bool provide,
314 : assignment_(name, namelen, val, provide, hidden)
317 // Add the symbol to the symbol table.
319 add_symbols_to_table(Symbol_table* symtab)
320 { this->assignment_.add_to_table(symtab); }
322 // Finalize the symbol.
324 finalize_symbols(Symbol_table* symtab, const Layout* layout,
325 uint64_t* dot_value, Output_section** dot_section)
327 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
331 // Set the section address. There is no section here, but if the
332 // value is absolute, we set the symbol. This permits us to use
333 // absolute symbols when setting dot.
335 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
336 uint64_t, uint64_t* dot_value, Output_section**,
337 std::string*, Input_section_list*)
339 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
342 // Print for debugging.
347 this->assignment_.print(f);
351 Symbol_assignment assignment_;
354 // An assignment to the dot symbol in an output section.
356 class Output_section_element_dot_assignment : public Output_section_element
359 Output_section_element_dot_assignment(Expression* val)
363 // Finalize the symbol.
365 finalize_symbols(Symbol_table* symtab, const Layout* layout,
366 uint64_t* dot_value, Output_section** dot_section)
368 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
369 *dot_section, dot_section);
372 // Update the dot symbol while setting section addresses.
374 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
375 uint64_t, uint64_t* dot_value, Output_section**,
376 std::string*, Input_section_list*);
378 // Print for debugging.
383 this->val_->print(f);
391 // Update the dot symbol while setting section addresses.
394 Output_section_element_dot_assignment::set_section_addresses(
395 Symbol_table* symtab,
397 Output_section* output_section,
400 Output_section** dot_section,
404 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
405 *dot_value, *dot_section,
407 if (next_dot < *dot_value)
408 gold_error(_("dot may not move backward"));
409 if (next_dot > *dot_value && output_section != NULL)
411 section_size_type length = convert_to_section_size_type(next_dot
413 Output_section_data* posd;
415 posd = new Output_data_fixed_space(length, 0);
418 std::string this_fill = this->get_fill_string(fill, length);
419 posd = new Output_data_const(this_fill, 0);
421 output_section->add_output_section_data(posd);
423 *dot_value = next_dot;
426 // An assertion in an output section.
428 class Output_section_element_assertion : public Output_section_element
431 Output_section_element_assertion(Expression* check, const char* message,
433 : assertion_(check, message, messagelen)
440 this->assertion_.print(f);
444 Script_assertion assertion_;
447 // We use a special instance of Output_section_data to handle BYTE,
448 // SHORT, etc. This permits forward references to symbols in the
451 class Output_data_expression : public Output_section_data
454 Output_data_expression(int size, bool is_signed, Expression* val,
455 const Symbol_table* symtab, const Layout* layout,
456 uint64_t dot_value, Output_section* dot_section)
457 : Output_section_data(size, 0),
458 is_signed_(is_signed), val_(val), symtab_(symtab),
459 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
463 // Write the data to the output file.
465 do_write(Output_file*);
467 // Write the data to a buffer.
469 do_write_to_buffer(unsigned char*);
472 template<bool big_endian>
474 endian_write_to_buffer(uint64_t, unsigned char*);
478 const Symbol_table* symtab_;
479 const Layout* layout_;
481 Output_section* dot_section_;
484 // Write the data element to the output file.
487 Output_data_expression::do_write(Output_file* of)
489 unsigned char* view = of->get_output_view(this->offset(), this->data_size());
490 this->write_to_buffer(view);
491 of->write_output_view(this->offset(), this->data_size(), view);
494 // Write the data element to a buffer.
497 Output_data_expression::do_write_to_buffer(unsigned char* buf)
499 Output_section* dummy;
500 uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
501 true, this->dot_value_,
502 this->dot_section_, &dummy);
504 if (parameters->target().is_big_endian())
505 this->endian_write_to_buffer<true>(val, buf);
507 this->endian_write_to_buffer<false>(val, buf);
510 template<bool big_endian>
512 Output_data_expression::endian_write_to_buffer(uint64_t val,
515 switch (this->data_size())
518 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
521 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
524 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
527 if (parameters->target().get_size() == 32)
530 if (this->is_signed_ && (val & 0x80000000) != 0)
531 val |= 0xffffffff00000000LL;
533 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
540 // A data item in an output section.
542 class Output_section_element_data : public Output_section_element
545 Output_section_element_data(int size, bool is_signed, Expression* val)
546 : size_(size), is_signed_(is_signed), val_(val)
549 // If there is a data item, then we must create an output section.
551 needs_output_section() const
554 // Finalize symbols--we just need to update dot.
556 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
558 { *dot_value += this->size_; }
560 // Store the value in the section.
562 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
563 uint64_t* dot_value, Output_section**, std::string*,
564 Input_section_list*);
566 // Print for debugging.
571 // The size in bytes.
573 // Whether the value is signed.
579 // Store the value in the section.
582 Output_section_element_data::set_section_addresses(
583 Symbol_table* symtab,
588 Output_section** dot_section,
592 gold_assert(os != NULL);
593 os->add_output_section_data(new Output_data_expression(this->size_,
600 *dot_value += this->size_;
603 // Print for debugging.
606 Output_section_element_data::print(FILE* f) const
621 if (this->is_signed_)
629 fprintf(f, " %s(", s);
630 this->val_->print(f);
634 // A fill value setting in an output section.
636 class Output_section_element_fill : public Output_section_element
639 Output_section_element_fill(Expression* val)
643 // Update the fill value while setting section addresses.
645 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
646 uint64_t, uint64_t* dot_value,
647 Output_section** dot_section,
648 std::string* fill, Input_section_list*)
650 Output_section* fill_section;
651 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
652 *dot_value, *dot_section,
654 if (fill_section != NULL)
655 gold_warning(_("fill value is not absolute"));
656 // FIXME: The GNU linker supports fill values of arbitrary length.
657 unsigned char fill_buff[4];
658 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
659 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
662 // Print for debugging.
666 fprintf(f, " FILL(");
667 this->val_->print(f);
672 // The new fill value.
676 // Return whether STRING contains a wildcard character. This is used
677 // to speed up matching.
680 is_wildcard_string(const std::string& s)
682 return strpbrk(s.c_str(), "?*[") != NULL;
685 // An input section specification in an output section
687 class Output_section_element_input : public Output_section_element
690 Output_section_element_input(const Input_section_spec* spec, bool keep);
692 // Finalize symbols--just update the value of the dot symbol.
694 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
695 Output_section** dot_section)
697 *dot_value = this->final_dot_value_;
698 *dot_section = this->final_dot_section_;
701 // See whether we match FILE_NAME and SECTION_NAME as an input
704 match_name(const char* file_name, const char* section_name) const;
706 // Set the section address.
708 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
709 uint64_t subalign, uint64_t* dot_value,
710 Output_section**, std::string* fill,
711 Input_section_list*);
713 // Print for debugging.
715 print(FILE* f) const;
718 // An input section pattern.
719 struct Input_section_pattern
722 bool pattern_is_wildcard;
725 Input_section_pattern(const char* patterna, size_t patternlena,
727 : pattern(patterna, patternlena),
728 pattern_is_wildcard(is_wildcard_string(this->pattern)),
733 typedef std::vector<Input_section_pattern> Input_section_patterns;
735 // Filename_exclusions is a pair of filename pattern and a bool
736 // indicating whether the filename is a wildcard.
737 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
739 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
740 // indicates whether this is a wildcard pattern.
742 match(const char* string, const char* pattern, bool is_wildcard_pattern)
744 return (is_wildcard_pattern
745 ? fnmatch(pattern, string, 0) == 0
746 : strcmp(string, pattern) == 0);
749 // See if we match a file name.
751 match_file_name(const char* file_name) const;
753 // The file name pattern. If this is the empty string, we match all
755 std::string filename_pattern_;
756 // Whether the file name pattern is a wildcard.
757 bool filename_is_wildcard_;
758 // How the file names should be sorted. This may only be
759 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
760 Sort_wildcard filename_sort_;
761 // The list of file names to exclude.
762 Filename_exclusions filename_exclusions_;
763 // The list of input section patterns.
764 Input_section_patterns input_section_patterns_;
765 // Whether to keep this section when garbage collecting.
767 // The value of dot after including all matching sections.
768 uint64_t final_dot_value_;
769 // The section where dot is defined after including all matching
771 Output_section* final_dot_section_;
774 // Construct Output_section_element_input. The parser records strings
775 // as pointers into a copy of the script file, which will go away when
776 // parsing is complete. We make sure they are in std::string objects.
778 Output_section_element_input::Output_section_element_input(
779 const Input_section_spec* spec,
781 : filename_pattern_(),
782 filename_is_wildcard_(false),
783 filename_sort_(spec->file.sort),
784 filename_exclusions_(),
785 input_section_patterns_(),
788 final_dot_section_(NULL)
790 // The filename pattern "*" is common, and matches all files. Turn
791 // it into the empty string.
792 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
793 this->filename_pattern_.assign(spec->file.name.value,
794 spec->file.name.length);
795 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_);
797 if (spec->input_sections.exclude != NULL)
799 for (String_list::const_iterator p =
800 spec->input_sections.exclude->begin();
801 p != spec->input_sections.exclude->end();
804 bool is_wildcard = is_wildcard_string(*p);
805 this->filename_exclusions_.push_back(std::make_pair(*p,
810 if (spec->input_sections.sections != NULL)
812 Input_section_patterns& isp(this->input_section_patterns_);
813 for (String_sort_list::const_iterator p =
814 spec->input_sections.sections->begin();
815 p != spec->input_sections.sections->end();
817 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
822 // See whether we match FILE_NAME.
825 Output_section_element_input::match_file_name(const char* file_name) const
827 if (!this->filename_pattern_.empty())
829 // If we were called with no filename, we refuse to match a
830 // pattern which requires a file name.
831 if (file_name == NULL)
834 if (!match(file_name, this->filename_pattern_.c_str(),
835 this->filename_is_wildcard_))
839 if (file_name != NULL)
841 // Now we have to see whether FILE_NAME matches one of the
842 // exclusion patterns, if any.
843 for (Filename_exclusions::const_iterator p =
844 this->filename_exclusions_.begin();
845 p != this->filename_exclusions_.end();
848 if (match(file_name, p->first.c_str(), p->second))
856 // See whether we match FILE_NAME and SECTION_NAME.
859 Output_section_element_input::match_name(const char* file_name,
860 const char* section_name) const
862 if (!this->match_file_name(file_name))
865 // If there are no section name patterns, then we match.
866 if (this->input_section_patterns_.empty())
869 // See whether we match the section name patterns.
870 for (Input_section_patterns::const_iterator p =
871 this->input_section_patterns_.begin();
872 p != this->input_section_patterns_.end();
875 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
879 // We didn't match any section names, so we didn't match.
883 // Information we use to sort the input sections.
885 struct Input_section_info
889 std::string section_name;
894 // A class to sort the input sections.
896 class Input_section_sorter
899 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
900 : filename_sort_(filename_sort), section_sort_(section_sort)
904 operator()(const Input_section_info&, const Input_section_info&) const;
907 Sort_wildcard filename_sort_;
908 Sort_wildcard section_sort_;
912 Input_section_sorter::operator()(const Input_section_info& isi1,
913 const Input_section_info& isi2) const
915 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
916 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
917 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
918 && isi1.addralign == isi2.addralign))
920 if (isi1.section_name != isi2.section_name)
921 return isi1.section_name < isi2.section_name;
923 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
924 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
925 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
927 if (isi1.addralign != isi2.addralign)
928 return isi1.addralign < isi2.addralign;
930 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
932 if (isi1.relobj->name() != isi2.relobj->name())
933 return isi1.relobj->name() < isi2.relobj->name();
936 // Otherwise we leave them in the same order.
940 // Set the section address. Look in INPUT_SECTIONS for sections which
941 // match this spec, sort them as specified, and add them to the output
945 Output_section_element_input::set_section_addresses(
948 Output_section* output_section,
951 Output_section** dot_section,
953 Input_section_list* input_sections)
955 // We build a list of sections which match each
956 // Input_section_pattern.
958 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
959 size_t input_pattern_count = this->input_section_patterns_.size();
960 if (input_pattern_count == 0)
961 input_pattern_count = 1;
962 Matching_sections matching_sections(input_pattern_count);
964 // Look through the list of sections for this output section. Add
965 // each one which matches to one of the elements of
966 // MATCHING_SECTIONS.
968 Input_section_list::iterator p = input_sections->begin();
969 while (p != input_sections->end())
971 // Calling section_name and section_addralign is not very
973 Input_section_info isi;
974 isi.relobj = p->first;
975 isi.shndx = p->second;
977 // Lock the object so that we can get information about the
978 // section. This is OK since we know we are single-threaded
981 const Task* task = reinterpret_cast<const Task*>(-1);
982 Task_lock_obj<Object> tl(task, p->first);
984 isi.section_name = p->first->section_name(p->second);
985 isi.size = p->first->section_size(p->second);
986 isi.addralign = p->first->section_addralign(p->second);
989 if (!this->match_file_name(isi.relobj->name().c_str()))
991 else if (this->input_section_patterns_.empty())
993 matching_sections[0].push_back(isi);
994 p = input_sections->erase(p);
999 for (i = 0; i < input_pattern_count; ++i)
1001 const Input_section_pattern&
1002 isp(this->input_section_patterns_[i]);
1003 if (match(isi.section_name.c_str(), isp.pattern.c_str(),
1004 isp.pattern_is_wildcard))
1008 if (i >= this->input_section_patterns_.size())
1012 matching_sections[i].push_back(isi);
1013 p = input_sections->erase(p);
1018 // Look through MATCHING_SECTIONS. Sort each one as specified,
1019 // using a stable sort so that we get the default order when
1020 // sections are otherwise equal. Add each input section to the
1023 for (size_t i = 0; i < input_pattern_count; ++i)
1025 if (matching_sections[i].empty())
1028 gold_assert(output_section != NULL);
1030 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1031 if (isp.sort != SORT_WILDCARD_NONE
1032 || this->filename_sort_ != SORT_WILDCARD_NONE)
1033 std::stable_sort(matching_sections[i].begin(),
1034 matching_sections[i].end(),
1035 Input_section_sorter(this->filename_sort_,
1038 for (std::vector<Input_section_info>::const_iterator p =
1039 matching_sections[i].begin();
1040 p != matching_sections[i].end();
1043 uint64_t this_subalign = p->addralign;
1044 if (this_subalign < subalign)
1045 this_subalign = subalign;
1047 uint64_t address = align_address(*dot_value, this_subalign);
1049 if (address > *dot_value && !fill->empty())
1051 section_size_type length =
1052 convert_to_section_size_type(address - *dot_value);
1053 std::string this_fill = this->get_fill_string(fill, length);
1054 Output_section_data* posd = new Output_data_const(this_fill, 0);
1055 output_section->add_output_section_data(posd);
1058 output_section->add_input_section_for_script(p->relobj,
1063 *dot_value = address + p->size;
1067 this->final_dot_value_ = *dot_value;
1068 this->final_dot_section_ = *dot_section;
1071 // Print for debugging.
1074 Output_section_element_input::print(FILE* f) const
1079 fprintf(f, "KEEP(");
1081 if (!this->filename_pattern_.empty())
1083 bool need_close_paren = false;
1084 switch (this->filename_sort_)
1086 case SORT_WILDCARD_NONE:
1088 case SORT_WILDCARD_BY_NAME:
1089 fprintf(f, "SORT_BY_NAME(");
1090 need_close_paren = true;
1096 fprintf(f, "%s", this->filename_pattern_.c_str());
1098 if (need_close_paren)
1102 if (!this->input_section_patterns_.empty()
1103 || !this->filename_exclusions_.empty())
1107 bool need_space = false;
1108 if (!this->filename_exclusions_.empty())
1110 fprintf(f, "EXCLUDE_FILE(");
1111 bool need_comma = false;
1112 for (Filename_exclusions::const_iterator p =
1113 this->filename_exclusions_.begin();
1114 p != this->filename_exclusions_.end();
1119 fprintf(f, "%s", p->first.c_str());
1126 for (Input_section_patterns::const_iterator p =
1127 this->input_section_patterns_.begin();
1128 p != this->input_section_patterns_.end();
1134 int close_parens = 0;
1137 case SORT_WILDCARD_NONE:
1139 case SORT_WILDCARD_BY_NAME:
1140 fprintf(f, "SORT_BY_NAME(");
1143 case SORT_WILDCARD_BY_ALIGNMENT:
1144 fprintf(f, "SORT_BY_ALIGNMENT(");
1147 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1148 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1151 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1152 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1159 fprintf(f, "%s", p->pattern.c_str());
1161 for (int i = 0; i < close_parens; ++i)
1176 // An output section.
1178 class Output_section_definition : public Sections_element
1181 typedef Output_section_element::Input_section_list Input_section_list;
1183 Output_section_definition(const char* name, size_t namelen,
1184 const Parser_output_section_header* header);
1186 // Finish the output section with the information in the trailer.
1188 finish(const Parser_output_section_trailer* trailer);
1190 // Add a symbol to be defined.
1192 add_symbol_assignment(const char* name, size_t length, Expression* value,
1193 bool provide, bool hidden);
1195 // Add an assignment to the special dot symbol.
1197 add_dot_assignment(Expression* value);
1199 // Add an assertion.
1201 add_assertion(Expression* check, const char* message, size_t messagelen);
1203 // Add a data item to the current output section.
1205 add_data(int size, bool is_signed, Expression* val);
1207 // Add a setting for the fill value.
1209 add_fill(Expression* val);
1211 // Add an input section specification.
1213 add_input_section(const Input_section_spec* spec, bool keep);
1215 // Create any required output sections.
1217 create_sections(Layout*);
1219 // Add any symbols being defined to the symbol table.
1221 add_symbols_to_table(Symbol_table* symtab);
1223 // Finalize symbols and check assertions.
1225 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
1227 // Return the output section name to use for an input file name and
1230 output_section_name(const char* file_name, const char* section_name,
1233 // Return whether to place an orphan section after this one.
1235 place_orphan_here(const Output_section *os, bool* exact) const;
1237 // Set the section address.
1239 set_section_addresses(Symbol_table* symtab, Layout* layout,
1240 uint64_t* dot_value, uint64_t* load_address);
1242 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1243 // this section is constrained, and the input sections do not match,
1244 // return the constraint, and set *POSD.
1246 check_constraint(Output_section_definition** posd);
1248 // See if this is the alternate output section for a constrained
1249 // output section. If it is, transfer the Output_section and return
1250 // true. Otherwise return false.
1252 alternate_constraint(Output_section_definition*, Section_constraint);
1254 // Get the list of segments to use for an allocated section when
1255 // using a PHDRS clause. If this is an allocated section, return
1256 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
1257 // which it should be attached. If the PHDRS were not specified,
1258 // don't change *PHDRS_LIST.
1260 allocate_to_segment(String_list** phdrs_list);
1262 // Print the contents to the FILE. This is for debugging.
1267 typedef std::vector<Output_section_element*> Output_section_elements;
1269 // The output section name.
1271 // The address. This may be NULL.
1272 Expression* address_;
1273 // The load address. This may be NULL.
1274 Expression* load_address_;
1275 // The alignment. This may be NULL.
1277 // The input section alignment. This may be NULL.
1278 Expression* subalign_;
1279 // The constraint, if any.
1280 Section_constraint constraint_;
1281 // The fill value. This may be NULL.
1283 // The list of segments this section should go into. This may be
1285 String_list* phdrs_;
1286 // The list of elements defining the section.
1287 Output_section_elements elements_;
1288 // The Output_section created for this definition. This will be
1289 // NULL if none was created.
1290 Output_section* output_section_;
1295 Output_section_definition::Output_section_definition(
1298 const Parser_output_section_header* header)
1299 : name_(name, namelen),
1300 address_(header->address),
1301 load_address_(header->load_address),
1302 align_(header->align),
1303 subalign_(header->subalign),
1304 constraint_(header->constraint),
1308 output_section_(NULL)
1312 // Finish an output section.
1315 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1317 this->fill_ = trailer->fill;
1318 this->phdrs_ = trailer->phdrs;
1321 // Add a symbol to be defined.
1324 Output_section_definition::add_symbol_assignment(const char* name,
1330 Output_section_element* p = new Output_section_element_assignment(name,
1335 this->elements_.push_back(p);
1338 // Add an assignment to the special dot symbol.
1341 Output_section_definition::add_dot_assignment(Expression* value)
1343 Output_section_element* p = new Output_section_element_dot_assignment(value);
1344 this->elements_.push_back(p);
1347 // Add an assertion.
1350 Output_section_definition::add_assertion(Expression* check,
1351 const char* message,
1354 Output_section_element* p = new Output_section_element_assertion(check,
1357 this->elements_.push_back(p);
1360 // Add a data item to the current output section.
1363 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1365 Output_section_element* p = new Output_section_element_data(size, is_signed,
1367 this->elements_.push_back(p);
1370 // Add a setting for the fill value.
1373 Output_section_definition::add_fill(Expression* val)
1375 Output_section_element* p = new Output_section_element_fill(val);
1376 this->elements_.push_back(p);
1379 // Add an input section specification.
1382 Output_section_definition::add_input_section(const Input_section_spec* spec,
1385 Output_section_element* p = new Output_section_element_input(spec, keep);
1386 this->elements_.push_back(p);
1389 // Create any required output sections. We need an output section if
1390 // there is a data statement here.
1393 Output_section_definition::create_sections(Layout* layout)
1395 if (this->output_section_ != NULL)
1397 for (Output_section_elements::const_iterator p = this->elements_.begin();
1398 p != this->elements_.end();
1401 if ((*p)->needs_output_section())
1403 const char* name = this->name_.c_str();
1404 this->output_section_ = layout->make_output_section_for_script(name);
1410 // Add any symbols being defined to the symbol table.
1413 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
1415 for (Output_section_elements::iterator p = this->elements_.begin();
1416 p != this->elements_.end();
1418 (*p)->add_symbols_to_table(symtab);
1421 // Finalize symbols and check assertions.
1424 Output_section_definition::finalize_symbols(Symbol_table* symtab,
1425 const Layout* layout,
1426 uint64_t* dot_value)
1428 if (this->output_section_ != NULL)
1429 *dot_value = this->output_section_->address();
1432 uint64_t address = *dot_value;
1433 if (this->address_ != NULL)
1435 Output_section* dummy;
1436 address = this->address_->eval_with_dot(symtab, layout, true,
1440 if (this->align_ != NULL)
1442 Output_section* dummy;
1443 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
1447 address = align_address(address, align);
1449 *dot_value = address;
1452 Output_section* dot_section = this->output_section_;
1453 for (Output_section_elements::iterator p = this->elements_.begin();
1454 p != this->elements_.end();
1456 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
1459 // Return the output section name to use for an input section name.
1462 Output_section_definition::output_section_name(const char* file_name,
1463 const char* section_name,
1464 Output_section*** slot)
1466 // Ask each element whether it matches NAME.
1467 for (Output_section_elements::const_iterator p = this->elements_.begin();
1468 p != this->elements_.end();
1471 if ((*p)->match_name(file_name, section_name))
1473 // We found a match for NAME, which means that it should go
1474 // into this output section.
1475 *slot = &this->output_section_;
1476 return this->name_.c_str();
1480 // We don't know about this section name.
1484 // Return whether to place an orphan output section after this
1488 Output_section_definition::place_orphan_here(const Output_section *os,
1491 // Check for the simple case first.
1492 if (this->output_section_ != NULL
1493 && this->output_section_->type() == os->type()
1494 && this->output_section_->flags() == os->flags())
1500 // Otherwise use some heuristics.
1502 if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
1505 if (os->type() == elfcpp::SHT_NOBITS)
1507 if (this->name_ == ".bss")
1512 if (this->output_section_ != NULL
1513 && this->output_section_->type() == elfcpp::SHT_NOBITS)
1516 else if (os->type() == elfcpp::SHT_NOTE)
1518 if (this->output_section_ != NULL
1519 && this->output_section_->type() == elfcpp::SHT_NOTE)
1524 if (this->name_.compare(0, 5, ".note") == 0)
1529 if (this->name_ == ".interp")
1531 if (this->output_section_ != NULL
1532 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1533 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1536 else if (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
1538 if (this->name_.compare(0, 4, ".rel") == 0)
1543 if (this->output_section_ != NULL
1544 && (this->output_section_->type() == elfcpp::SHT_REL
1545 || this->output_section_->type() == elfcpp::SHT_RELA))
1550 if (this->output_section_ != NULL
1551 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1552 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1555 else if (os->type() == elfcpp::SHT_PROGBITS
1556 && (os->flags() & elfcpp::SHF_WRITE) != 0)
1558 if (this->name_ == ".data")
1563 if (this->output_section_ != NULL
1564 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1565 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
1568 else if (os->type() == elfcpp::SHT_PROGBITS
1569 && (os->flags() & elfcpp::SHF_EXECINSTR) != 0)
1571 if (this->name_ == ".text")
1576 if (this->output_section_ != NULL
1577 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1578 && (this->output_section_->flags() & elfcpp::SHF_EXECINSTR) != 0)
1581 else if (os->type() == elfcpp::SHT_PROGBITS
1582 || (os->type() != elfcpp::SHT_PROGBITS
1583 && (os->flags() & elfcpp::SHF_WRITE) == 0))
1585 if (this->name_ == ".rodata")
1590 if (this->output_section_ != NULL
1591 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1592 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1599 // Set the section address. Note that the OUTPUT_SECTION_ field will
1600 // be NULL if no input sections were mapped to this output section.
1601 // We still have to adjust dot and process symbol assignments.
1604 Output_section_definition::set_section_addresses(Symbol_table* symtab,
1606 uint64_t* dot_value,
1607 uint64_t* load_address)
1610 if (this->address_ == NULL)
1611 address = *dot_value;
1614 Output_section* dummy;
1615 address = this->address_->eval_with_dot(symtab, layout, true,
1616 *dot_value, NULL, &dummy);
1620 if (this->align_ == NULL)
1622 if (this->output_section_ == NULL)
1625 align = this->output_section_->addralign();
1629 Output_section* align_section;
1630 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
1631 NULL, &align_section);
1632 if (align_section != NULL)
1633 gold_warning(_("alignment of section %s is not absolute"),
1634 this->name_.c_str());
1635 if (this->output_section_ != NULL)
1636 this->output_section_->set_addralign(align);
1639 address = align_address(address, align);
1641 uint64_t start_address = address;
1643 *dot_value = address;
1645 // The address of non-SHF_ALLOC sections is forced to zero,
1646 // regardless of what the linker script wants.
1647 if (this->output_section_ != NULL
1648 && (this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0)
1649 this->output_section_->set_address(address);
1651 if (this->load_address_ != NULL && this->output_section_ != NULL)
1653 Output_section* dummy;
1654 uint64_t load_address =
1655 this->load_address_->eval_with_dot(symtab, layout, true, *dot_value,
1656 this->output_section_, &dummy);
1657 this->output_section_->set_load_address(load_address);
1661 if (this->subalign_ == NULL)
1665 Output_section* subalign_section;
1666 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
1669 if (subalign_section != NULL)
1670 gold_warning(_("subalign of section %s is not absolute"),
1671 this->name_.c_str());
1675 if (this->fill_ != NULL)
1677 // FIXME: The GNU linker supports fill values of arbitrary
1679 Output_section* fill_section;
1680 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
1684 if (fill_section != NULL)
1685 gold_warning(_("fill of section %s is not absolute"),
1686 this->name_.c_str());
1687 unsigned char fill_buff[4];
1688 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1689 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
1692 Input_section_list input_sections;
1693 if (this->output_section_ != NULL)
1695 // Get the list of input sections attached to this output
1696 // section. This will leave the output section with only
1697 // Output_section_data entries.
1698 address += this->output_section_->get_input_sections(address,
1701 *dot_value = address;
1704 Output_section* dot_section = this->output_section_;
1705 for (Output_section_elements::iterator p = this->elements_.begin();
1706 p != this->elements_.end();
1708 (*p)->set_section_addresses(symtab, layout, this->output_section_,
1709 subalign, dot_value, &dot_section, &fill,
1712 gold_assert(input_sections.empty());
1714 if (this->load_address_ == NULL || this->output_section_ == NULL)
1715 *load_address = *dot_value;
1717 *load_address = (this->output_section_->load_address()
1718 + (*dot_value - start_address));
1721 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1722 // this section is constrained, and the input sections do not match,
1723 // return the constraint, and set *POSD.
1726 Output_section_definition::check_constraint(Output_section_definition** posd)
1728 switch (this->constraint_)
1730 case CONSTRAINT_NONE:
1731 return CONSTRAINT_NONE;
1733 case CONSTRAINT_ONLY_IF_RO:
1734 if (this->output_section_ != NULL
1735 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
1738 return CONSTRAINT_ONLY_IF_RO;
1740 return CONSTRAINT_NONE;
1742 case CONSTRAINT_ONLY_IF_RW:
1743 if (this->output_section_ != NULL
1744 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1747 return CONSTRAINT_ONLY_IF_RW;
1749 return CONSTRAINT_NONE;
1751 case CONSTRAINT_SPECIAL:
1752 if (this->output_section_ != NULL)
1753 gold_error(_("SPECIAL constraints are not implemented"));
1754 return CONSTRAINT_NONE;
1761 // See if this is the alternate output section for a constrained
1762 // output section. If it is, transfer the Output_section and return
1763 // true. Otherwise return false.
1766 Output_section_definition::alternate_constraint(
1767 Output_section_definition* posd,
1768 Section_constraint constraint)
1770 if (this->name_ != posd->name_)
1775 case CONSTRAINT_ONLY_IF_RO:
1776 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
1780 case CONSTRAINT_ONLY_IF_RW:
1781 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
1789 // We have found the alternate constraint. We just need to move
1790 // over the Output_section. When constraints are used properly,
1791 // THIS should not have an output_section pointer, as all the input
1792 // sections should have matched the other definition.
1794 if (this->output_section_ != NULL)
1795 gold_error(_("mismatched definition for constrained sections"));
1797 this->output_section_ = posd->output_section_;
1798 posd->output_section_ = NULL;
1803 // Get the list of segments to use for an allocated section when using
1804 // a PHDRS clause. If this is an allocated section, return the
1805 // Output_section, and set *PHDRS_LIST to the list of PHDRS to which
1806 // it should be attached. If the PHDRS were not specified, don't
1807 // change *PHDRS_LIST.
1810 Output_section_definition::allocate_to_segment(String_list** phdrs_list)
1812 if (this->output_section_ == NULL)
1814 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
1816 if (this->phdrs_ != NULL)
1817 *phdrs_list = this->phdrs_;
1818 return this->output_section_;
1821 // Print for debugging.
1824 Output_section_definition::print(FILE* f) const
1826 fprintf(f, " %s ", this->name_.c_str());
1828 if (this->address_ != NULL)
1830 this->address_->print(f);
1836 if (this->load_address_ != NULL)
1839 this->load_address_->print(f);
1843 if (this->align_ != NULL)
1845 fprintf(f, "ALIGN(");
1846 this->align_->print(f);
1850 if (this->subalign_ != NULL)
1852 fprintf(f, "SUBALIGN(");
1853 this->subalign_->print(f);
1859 for (Output_section_elements::const_iterator p = this->elements_.begin();
1860 p != this->elements_.end();
1866 if (this->fill_ != NULL)
1869 this->fill_->print(f);
1872 if (this->phdrs_ != NULL)
1874 for (String_list::const_iterator p = this->phdrs_->begin();
1875 p != this->phdrs_->end();
1877 fprintf(f, " :%s", p->c_str());
1883 // An output section created to hold orphaned input sections. These
1884 // do not actually appear in linker scripts. However, for convenience
1885 // when setting the output section addresses, we put a marker to these
1886 // sections in the appropriate place in the list of SECTIONS elements.
1888 class Orphan_output_section : public Sections_element
1891 Orphan_output_section(Output_section* os)
1895 // Return whether to place an orphan section after this one.
1897 place_orphan_here(const Output_section *os, bool* exact) const;
1899 // Set section addresses.
1901 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*);
1903 // Get the list of segments to use for an allocated section when
1904 // using a PHDRS clause. If this is an allocated section, return
1905 // the Output_section.
1907 allocate_to_segment(String_list**);
1909 // Print for debugging.
1911 print(FILE* f) const
1913 fprintf(f, " marker for orphaned output section %s\n",
1918 Output_section* os_;
1921 // Whether to place another orphan section after this one.
1924 Orphan_output_section::place_orphan_here(const Output_section* os,
1927 if (this->os_->type() == os->type()
1928 && this->os_->flags() == os->flags())
1936 // Set section addresses.
1939 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
1940 uint64_t* dot_value,
1941 uint64_t* load_address)
1943 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
1945 bool have_load_address = *load_address != *dot_value;
1947 uint64_t address = *dot_value;
1948 address = align_address(address, this->os_->addralign());
1950 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
1952 this->os_->set_address(address);
1953 if (have_load_address)
1954 this->os_->set_load_address(align_address(*load_address,
1955 this->os_->addralign()));
1958 Input_section_list input_sections;
1959 address += this->os_->get_input_sections(address, "", &input_sections);
1961 for (Input_section_list::iterator p = input_sections.begin();
1962 p != input_sections.end();
1968 // We know what are single-threaded, so it is OK to lock the
1971 const Task* task = reinterpret_cast<const Task*>(-1);
1972 Task_lock_obj<Object> tl(task, p->first);
1973 addralign = p->first->section_addralign(p->second);
1974 size = p->first->section_size(p->second);
1977 address = align_address(address, addralign);
1978 this->os_->add_input_section_for_script(p->first, p->second, size,
1983 if (!have_load_address)
1984 *load_address = address;
1986 *load_address += address - *dot_value;
1988 *dot_value = address;
1991 // Get the list of segments to use for an allocated section when using
1992 // a PHDRS clause. If this is an allocated section, return the
1993 // Output_section. We don't change the list of segments.
1996 Orphan_output_section::allocate_to_segment(String_list**)
1998 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2003 // Class Phdrs_element. A program header from a PHDRS clause.
2008 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2009 bool includes_filehdr, bool includes_phdrs,
2010 bool is_flags_valid, unsigned int flags,
2011 Expression* load_address)
2012 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2013 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2014 flags_(flags), load_address_(load_address), load_address_value_(0),
2018 // Return the name of this segment.
2021 { return this->name_; }
2023 // Return the type of the segment.
2026 { return this->type_; }
2028 // Whether to include the file header.
2030 includes_filehdr() const
2031 { return this->includes_filehdr_; }
2033 // Whether to include the program headers.
2035 includes_phdrs() const
2036 { return this->includes_phdrs_; }
2038 // Return whether there is a load address.
2040 has_load_address() const
2041 { return this->load_address_ != NULL; }
2043 // Evaluate the load address expression if there is one.
2045 eval_load_address(Symbol_table* symtab, Layout* layout)
2047 if (this->load_address_ != NULL)
2048 this->load_address_value_ = this->load_address_->eval(symtab, layout,
2052 // Return the load address.
2054 load_address() const
2056 gold_assert(this->load_address_ != NULL);
2057 return this->load_address_value_;
2060 // Create the segment.
2062 create_segment(Layout* layout)
2064 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2065 return this->segment_;
2068 // Return the segment.
2071 { return this->segment_; }
2073 // Set the segment flags if appropriate.
2075 set_flags_if_valid()
2077 if (this->is_flags_valid_)
2078 this->segment_->set_flags(this->flags_);
2081 // Print for debugging.
2086 // The name used in the script.
2088 // The type of the segment (PT_LOAD, etc.).
2090 // Whether this segment includes the file header.
2091 bool includes_filehdr_;
2092 // Whether this segment includes the section headers.
2093 bool includes_phdrs_;
2094 // Whether the flags were explicitly specified.
2095 bool is_flags_valid_;
2096 // The flags for this segment (PF_R, etc.) if specified.
2097 unsigned int flags_;
2098 // The expression for the load address for this segment. This may
2100 Expression* load_address_;
2101 // The actual load address from evaluating the expression.
2102 uint64_t load_address_value_;
2103 // The segment itself.
2104 Output_segment* segment_;
2107 // Print for debugging.
2110 Phdrs_element::print(FILE* f) const
2112 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
2113 if (this->includes_filehdr_)
2114 fprintf(f, " FILEHDR");
2115 if (this->includes_phdrs_)
2116 fprintf(f, " PHDRS");
2117 if (this->is_flags_valid_)
2118 fprintf(f, " FLAGS(%u)", this->flags_);
2119 if (this->load_address_ != NULL)
2122 this->load_address_->print(f);
2128 // Class Script_sections.
2130 Script_sections::Script_sections()
2131 : saw_sections_clause_(false),
2132 in_sections_clause_(false),
2133 sections_elements_(NULL),
2134 output_section_(NULL),
2135 phdrs_elements_(NULL)
2139 // Start a SECTIONS clause.
2142 Script_sections::start_sections()
2144 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2145 this->saw_sections_clause_ = true;
2146 this->in_sections_clause_ = true;
2147 if (this->sections_elements_ == NULL)
2148 this->sections_elements_ = new Sections_elements;
2151 // Finish a SECTIONS clause.
2154 Script_sections::finish_sections()
2156 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2157 this->in_sections_clause_ = false;
2160 // Add a symbol to be defined.
2163 Script_sections::add_symbol_assignment(const char* name, size_t length,
2164 Expression* val, bool provide,
2167 if (this->output_section_ != NULL)
2168 this->output_section_->add_symbol_assignment(name, length, val,
2172 Sections_element* p = new Sections_element_assignment(name, length,
2175 this->sections_elements_->push_back(p);
2179 // Add an assignment to the special dot symbol.
2182 Script_sections::add_dot_assignment(Expression* val)
2184 if (this->output_section_ != NULL)
2185 this->output_section_->add_dot_assignment(val);
2188 Sections_element* p = new Sections_element_dot_assignment(val);
2189 this->sections_elements_->push_back(p);
2193 // Add an assertion.
2196 Script_sections::add_assertion(Expression* check, const char* message,
2199 if (this->output_section_ != NULL)
2200 this->output_section_->add_assertion(check, message, messagelen);
2203 Sections_element* p = new Sections_element_assertion(check, message,
2205 this->sections_elements_->push_back(p);
2209 // Start processing entries for an output section.
2212 Script_sections::start_output_section(
2215 const Parser_output_section_header *header)
2217 Output_section_definition* posd = new Output_section_definition(name,
2220 this->sections_elements_->push_back(posd);
2221 gold_assert(this->output_section_ == NULL);
2222 this->output_section_ = posd;
2225 // Stop processing entries for an output section.
2228 Script_sections::finish_output_section(
2229 const Parser_output_section_trailer* trailer)
2231 gold_assert(this->output_section_ != NULL);
2232 this->output_section_->finish(trailer);
2233 this->output_section_ = NULL;
2236 // Add a data item to the current output section.
2239 Script_sections::add_data(int size, bool is_signed, Expression* val)
2241 gold_assert(this->output_section_ != NULL);
2242 this->output_section_->add_data(size, is_signed, val);
2245 // Add a fill value setting to the current output section.
2248 Script_sections::add_fill(Expression* val)
2250 gold_assert(this->output_section_ != NULL);
2251 this->output_section_->add_fill(val);
2254 // Add an input section specification to the current output section.
2257 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2259 gold_assert(this->output_section_ != NULL);
2260 this->output_section_->add_input_section(spec, keep);
2263 // Create any required sections.
2266 Script_sections::create_sections(Layout* layout)
2268 if (!this->saw_sections_clause_)
2270 for (Sections_elements::iterator p = this->sections_elements_->begin();
2271 p != this->sections_elements_->end();
2273 (*p)->create_sections(layout);
2276 // Add any symbols we are defining to the symbol table.
2279 Script_sections::add_symbols_to_table(Symbol_table* symtab)
2281 if (!this->saw_sections_clause_)
2283 for (Sections_elements::iterator p = this->sections_elements_->begin();
2284 p != this->sections_elements_->end();
2286 (*p)->add_symbols_to_table(symtab);
2289 // Finalize symbols and check assertions.
2292 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2294 if (!this->saw_sections_clause_)
2296 uint64_t dot_value = 0;
2297 for (Sections_elements::iterator p = this->sections_elements_->begin();
2298 p != this->sections_elements_->end();
2300 (*p)->finalize_symbols(symtab, layout, &dot_value);
2303 // Return the name of the output section to use for an input file name
2304 // and section name.
2307 Script_sections::output_section_name(const char* file_name,
2308 const char* section_name,
2309 Output_section*** output_section_slot)
2311 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2312 p != this->sections_elements_->end();
2315 const char* ret = (*p)->output_section_name(file_name, section_name,
2316 output_section_slot);
2320 // The special name /DISCARD/ means that the input section
2321 // should be discarded.
2322 if (strcmp(ret, "/DISCARD/") == 0)
2324 *output_section_slot = NULL;
2331 // If we couldn't find a mapping for the name, the output section
2332 // gets the name of the input section.
2334 *output_section_slot = NULL;
2336 return section_name;
2339 // Place a marker for an orphan output section into the SECTIONS
2343 Script_sections::place_orphan(Output_section* os)
2345 // Look for an output section definition which matches the output
2346 // section. Put a marker after that section.
2347 Sections_elements::iterator place = this->sections_elements_->end();
2348 for (Sections_elements::iterator p = this->sections_elements_->begin();
2349 p != this->sections_elements_->end();
2353 if ((*p)->place_orphan_here(os, &exact))
2361 // The insert function puts the new element before the iterator.
2362 if (place != this->sections_elements_->end())
2365 this->sections_elements_->insert(place, new Orphan_output_section(os));
2368 // Set the addresses of all the output sections. Walk through all the
2369 // elements, tracking the dot symbol. Apply assignments which set
2370 // absolute symbol values, in case they are used when setting dot.
2371 // Fill in data statement values. As we find output sections, set the
2372 // address, set the address of all associated input sections, and
2373 // update dot. Return the segment which should hold the file header
2374 // and segment headers, if any.
2377 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2379 gold_assert(this->saw_sections_clause_);
2381 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2382 // for our representation.
2383 for (Sections_elements::iterator p = this->sections_elements_->begin();
2384 p != this->sections_elements_->end();
2387 Output_section_definition* posd;
2388 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2389 if (failed_constraint != CONSTRAINT_NONE)
2391 Sections_elements::iterator q;
2392 for (q = this->sections_elements_->begin();
2393 q != this->sections_elements_->end();
2398 if ((*q)->alternate_constraint(posd, failed_constraint))
2403 if (q == this->sections_elements_->end())
2404 gold_error(_("no matching section constraint"));
2408 // For a relocatable link, we implicitly set dot to zero.
2409 uint64_t dot_value = 0;
2410 uint64_t load_address = 0;
2411 for (Sections_elements::iterator p = this->sections_elements_->begin();
2412 p != this->sections_elements_->end();
2414 (*p)->set_section_addresses(symtab, layout, &dot_value, &load_address);
2416 if (this->phdrs_elements_ != NULL)
2418 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2419 p != this->phdrs_elements_->end();
2421 (*p)->eval_load_address(symtab, layout);
2424 return this->create_segments(layout);
2427 // Sort the sections in order to put them into segments.
2429 class Sort_output_sections
2433 operator()(const Output_section* os1, const Output_section* os2) const;
2437 Sort_output_sections::operator()(const Output_section* os1,
2438 const Output_section* os2) const
2440 // Sort first by the load address.
2441 uint64_t lma1 = (os1->has_load_address()
2442 ? os1->load_address()
2444 uint64_t lma2 = (os2->has_load_address()
2445 ? os2->load_address()
2450 // Then sort by the virtual address.
2451 if (os1->address() != os2->address())
2452 return os1->address() < os2->address();
2454 // Sort TLS sections to the end.
2455 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
2456 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
2460 // Sort PROGBITS before NOBITS.
2461 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
2463 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
2466 // Otherwise we don't care.
2470 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2471 // We treat a section with the SHF_TLS flag set as taking up space
2472 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2473 // space for them in the file.
2476 Script_sections::is_bss_section(const Output_section* os)
2478 return (os->type() == elfcpp::SHT_NOBITS
2479 && (os->flags() & elfcpp::SHF_TLS) == 0);
2482 // Return the size taken by the file header and the program headers.
2485 Script_sections::total_header_size(Layout* layout) const
2487 size_t segment_count = layout->segment_count();
2488 size_t file_header_size;
2489 size_t segment_headers_size;
2490 if (parameters->target().get_size() == 32)
2492 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
2493 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
2495 else if (parameters->target().get_size() == 64)
2497 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
2498 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
2503 return file_header_size + segment_headers_size;
2506 // Return the amount we have to subtract from the LMA to accomodate
2507 // headers of the given size. The complication is that the file
2508 // header have to be at the start of a page, as otherwise it will not
2509 // be at the start of the file.
2512 Script_sections::header_size_adjustment(uint64_t lma,
2513 size_t sizeof_headers) const
2515 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
2516 uint64_t hdr_lma = lma - sizeof_headers;
2517 hdr_lma &= ~(abi_pagesize - 1);
2518 return lma - hdr_lma;
2521 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2522 // the segment which should hold the file header and segment headers,
2526 Script_sections::create_segments(Layout* layout)
2528 gold_assert(this->saw_sections_clause_);
2530 if (parameters->options().relocatable())
2533 if (this->saw_phdrs_clause())
2534 return create_segments_from_phdrs_clause(layout);
2536 Layout::Section_list sections;
2537 layout->get_allocated_sections(§ions);
2539 // Sort the sections by address.
2540 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
2542 this->create_note_and_tls_segments(layout, §ions);
2544 // Walk through the sections adding them to PT_LOAD segments.
2545 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
2546 Output_segment* first_seg = NULL;
2547 Output_segment* current_seg = NULL;
2548 bool is_current_seg_readonly = true;
2549 Layout::Section_list::iterator plast = sections.end();
2550 uint64_t last_vma = 0;
2551 uint64_t last_lma = 0;
2552 uint64_t last_size = 0;
2553 for (Layout::Section_list::iterator p = sections.begin();
2554 p != sections.end();
2557 const uint64_t vma = (*p)->address();
2558 const uint64_t lma = ((*p)->has_load_address()
2559 ? (*p)->load_address()
2561 const uint64_t size = (*p)->current_data_size();
2563 bool need_new_segment;
2564 if (current_seg == NULL)
2565 need_new_segment = true;
2566 else if (lma - vma != last_lma - last_vma)
2568 // This section has a different LMA relationship than the
2569 // last one; we need a new segment.
2570 need_new_segment = true;
2572 else if (align_address(last_lma + last_size, abi_pagesize)
2573 < align_address(lma, abi_pagesize))
2575 // Putting this section in the segment would require
2577 need_new_segment = true;
2579 else if (is_bss_section(*plast) && !is_bss_section(*p))
2581 // A non-BSS section can not follow a BSS section in the
2583 need_new_segment = true;
2585 else if (is_current_seg_readonly
2586 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2588 // Don't put a writable section in the same segment as a
2589 // non-writable section.
2590 need_new_segment = true;
2594 // Otherwise, reuse the existing segment.
2595 need_new_segment = false;
2598 elfcpp::Elf_Word seg_flags =
2599 Layout::section_flags_to_segment((*p)->flags());
2601 if (need_new_segment)
2603 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2605 current_seg->set_addresses(vma, lma);
2606 if (first_seg == NULL)
2607 first_seg = current_seg;
2608 is_current_seg_readonly = true;
2611 current_seg->add_output_section(*p, seg_flags);
2613 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2614 is_current_seg_readonly = false;
2622 // An ELF program should work even if the program headers are not in
2623 // a PT_LOAD segment. However, it appears that the Linux kernel
2624 // does not set the AT_PHDR auxiliary entry in that case. It sets
2625 // the load address to p_vaddr - p_offset of the first PT_LOAD
2626 // segment. It then sets AT_PHDR to the load address plus the
2627 // offset to the program headers, e_phoff in the file header. This
2628 // fails when the program headers appear in the file before the
2629 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2630 // segment to hold the file header and the program headers. This is
2631 // effectively what the GNU linker does, and it is slightly more
2632 // efficient in any case. We try to use the first PT_LOAD segment
2633 // if we can, otherwise we make a new one.
2635 if (first_seg == NULL)
2638 size_t sizeof_headers = this->total_header_size(layout);
2640 if ((first_seg->paddr() & (abi_pagesize - 1)) >= sizeof_headers)
2642 first_seg->set_addresses(first_seg->vaddr() - sizeof_headers,
2643 first_seg->paddr() - sizeof_headers);
2647 uint64_t vma = first_seg->vaddr();
2648 uint64_t lma = first_seg->paddr();
2650 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
2652 // If there is no room to squeeze in the headers, then punt. The
2653 // resulting executable probably won't run on GNU/Linux, but we
2654 // trust that the user knows what they are doing.
2655 if (lma < subtract || vma < subtract)
2658 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2660 load_seg->set_addresses(vma - subtract, lma - subtract);
2665 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2666 // segment if there are any SHT_TLS sections.
2669 Script_sections::create_note_and_tls_segments(
2671 const Layout::Section_list* sections)
2673 gold_assert(!this->saw_phdrs_clause());
2675 bool saw_tls = false;
2676 for (Layout::Section_list::const_iterator p = sections->begin();
2677 p != sections->end();
2680 if ((*p)->type() == elfcpp::SHT_NOTE)
2682 elfcpp::Elf_Word seg_flags =
2683 Layout::section_flags_to_segment((*p)->flags());
2684 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
2686 oseg->add_output_section(*p, seg_flags);
2688 // Incorporate any subsequent SHT_NOTE sections, in the
2689 // hopes that the script is sensible.
2690 Layout::Section_list::const_iterator pnext = p + 1;
2691 while (pnext != sections->end()
2692 && (*pnext)->type() == elfcpp::SHT_NOTE)
2694 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2695 oseg->add_output_section(*pnext, seg_flags);
2701 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2704 gold_error(_("TLS sections are not adjacent"));
2706 elfcpp::Elf_Word seg_flags =
2707 Layout::section_flags_to_segment((*p)->flags());
2708 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
2710 oseg->add_output_section(*p, seg_flags);
2712 Layout::Section_list::const_iterator pnext = p + 1;
2713 while (pnext != sections->end()
2714 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
2716 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2717 oseg->add_output_section(*pnext, seg_flags);
2727 // Add a program header. The PHDRS clause is syntactically distinct
2728 // from the SECTIONS clause, but we implement it with the SECTIONS
2729 // support becauase PHDRS is useless if there is no SECTIONS clause.
2732 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
2733 bool includes_filehdr, bool includes_phdrs,
2734 bool is_flags_valid, unsigned int flags,
2735 Expression* load_address)
2737 if (this->phdrs_elements_ == NULL)
2738 this->phdrs_elements_ = new Phdrs_elements();
2739 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
2742 is_flags_valid, flags,
2746 // Return the number of segments we expect to create based on the
2747 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2750 Script_sections::expected_segment_count(const Layout* layout) const
2752 if (this->saw_phdrs_clause())
2753 return this->phdrs_elements_->size();
2755 Layout::Section_list sections;
2756 layout->get_allocated_sections(§ions);
2758 // We assume that we will need two PT_LOAD segments.
2761 bool saw_note = false;
2762 bool saw_tls = false;
2763 for (Layout::Section_list::const_iterator p = sections.begin();
2764 p != sections.end();
2767 if ((*p)->type() == elfcpp::SHT_NOTE)
2769 // Assume that all note sections will fit into a single
2777 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2779 // There can only be one PT_TLS segment.
2791 // Create the segments from a PHDRS clause. Return the segment which
2792 // should hold the file header and program headers, if any.
2795 Script_sections::create_segments_from_phdrs_clause(Layout* layout)
2797 this->attach_sections_using_phdrs_clause(layout);
2798 return this->set_phdrs_clause_addresses(layout);
2801 // Create the segments from the PHDRS clause, and put the output
2802 // sections in them.
2805 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
2807 typedef std::map<std::string, Output_segment*> Name_to_segment;
2808 Name_to_segment name_to_segment;
2809 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2810 p != this->phdrs_elements_->end();
2812 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
2814 // Walk through the output sections and attach them to segments.
2815 // Output sections in the script which do not list segments are
2816 // attached to the same set of segments as the immediately preceding
2818 String_list* phdr_names = NULL;
2819 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2820 p != this->sections_elements_->end();
2823 Output_section* os = (*p)->allocate_to_segment(&phdr_names);
2827 if (phdr_names == NULL)
2829 gold_error(_("allocated section not in any segment"));
2833 bool in_load_segment = false;
2834 for (String_list::const_iterator q = phdr_names->begin();
2835 q != phdr_names->end();
2838 Name_to_segment::const_iterator r = name_to_segment.find(*q);
2839 if (r == name_to_segment.end())
2840 gold_error(_("no segment %s"), q->c_str());
2843 elfcpp::Elf_Word seg_flags =
2844 Layout::section_flags_to_segment(os->flags());
2845 r->second->add_output_section(os, seg_flags);
2847 if (r->second->type() == elfcpp::PT_LOAD)
2849 if (in_load_segment)
2850 gold_error(_("section in two PT_LOAD segments"));
2851 in_load_segment = true;
2856 if (!in_load_segment)
2857 gold_error(_("allocated section not in any PT_LOAD segment"));
2861 // Set the addresses for segments created from a PHDRS clause. Return
2862 // the segment which should hold the file header and program headers,
2866 Script_sections::set_phdrs_clause_addresses(Layout* layout)
2868 Output_segment* load_seg = NULL;
2869 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2870 p != this->phdrs_elements_->end();
2873 // Note that we have to set the flags after adding the output
2874 // sections to the segment, as adding an output segment can
2875 // change the flags.
2876 (*p)->set_flags_if_valid();
2878 Output_segment* oseg = (*p)->segment();
2880 if (oseg->type() != elfcpp::PT_LOAD)
2882 // The addresses of non-PT_LOAD segments are set from the
2883 // PT_LOAD segments.
2884 if ((*p)->has_load_address())
2885 gold_error(_("may only specify load address for PT_LOAD segment"));
2889 // The output sections should have addresses from the SECTIONS
2890 // clause. The addresses don't have to be in order, so find the
2891 // one with the lowest load address. Use that to set the
2892 // address of the segment.
2894 Output_section* osec = oseg->section_with_lowest_load_address();
2897 oseg->set_addresses(0, 0);
2901 uint64_t vma = osec->address();
2902 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
2904 // Override the load address of the section with the load
2905 // address specified for the segment.
2906 if ((*p)->has_load_address())
2908 if (osec->has_load_address())
2909 gold_warning(_("PHDRS load address overrides "
2910 "section %s load address"),
2913 lma = (*p)->load_address();
2916 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
2917 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
2919 // We could support this if we wanted to.
2920 gold_error(_("using only one of FILEHDR and PHDRS is "
2921 "not currently supported"));
2925 size_t sizeof_headers = this->total_header_size(layout);
2926 uint64_t subtract = this->header_size_adjustment(lma,
2928 if (lma >= subtract && vma >= subtract)
2935 gold_error(_("sections loaded on first page without room "
2936 "for file and program headers "
2937 "are not supported"));
2940 if (load_seg != NULL)
2941 gold_error(_("using FILEHDR and PHDRS on more than one "
2942 "PT_LOAD segment is not currently supported"));
2946 oseg->set_addresses(vma, lma);
2952 // Add the file header and segment headers to non-load segments
2953 // specified in the PHDRS clause.
2956 Script_sections::put_headers_in_phdrs(Output_data* file_header,
2957 Output_data* segment_headers)
2959 gold_assert(this->saw_phdrs_clause());
2960 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2961 p != this->phdrs_elements_->end();
2964 if ((*p)->type() != elfcpp::PT_LOAD)
2966 if ((*p)->includes_phdrs())
2967 (*p)->segment()->add_initial_output_data(segment_headers);
2968 if ((*p)->includes_filehdr())
2969 (*p)->segment()->add_initial_output_data(file_header);
2974 // Print the SECTIONS clause to F for debugging.
2977 Script_sections::print(FILE* f) const
2979 if (!this->saw_sections_clause_)
2982 fprintf(f, "SECTIONS {\n");
2984 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2985 p != this->sections_elements_->end();
2991 if (this->phdrs_elements_ != NULL)
2993 fprintf(f, "PHDRS {\n");
2994 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2995 p != this->phdrs_elements_->end();
3002 } // End namespace gold.