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 // Add any symbol being defined to the symbol table.
59 add_symbols_to_table(Symbol_table*)
62 // Finalize symbols and check assertions.
64 finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*)
67 // Return the output section name to use for an input file name and
68 // section name. This only real implementation is in
69 // Output_section_definition.
71 output_section_name(const char*, const char*, Output_section***)
74 // Return whether to place an orphan output section after this
77 place_orphan_here(const Output_section *, bool*) const
80 // Set section addresses. This includes applying assignments if the
81 // the expression is an absolute value.
83 set_section_addresses(Symbol_table*, Layout*, bool*, uint64_t*)
86 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
87 // this section is constrained, and the input sections do not match,
88 // return the constraint, and set *POSD.
89 virtual Section_constraint
90 check_constraint(Output_section_definition**)
91 { return CONSTRAINT_NONE; }
93 // See if this is the alternate output section for a constrained
94 // output section. If it is, transfer the Output_section and return
95 // true. Otherwise return false.
97 alternate_constraint(Output_section_definition*, Section_constraint)
100 // Get the list of segments to use for an allocated section when
101 // using a PHDRS clause. If this is an allocated section, return
102 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
103 // which it should be attached. If the PHDRS were not specified,
104 // don't change *PHDRS_LIST.
105 virtual Output_section*
106 allocate_to_segment(String_list**)
109 // Print the element for debugging purposes.
111 print(FILE* f) const = 0;
114 // An assignment in a SECTIONS clause outside of an output section.
116 class Sections_element_assignment : public Sections_element
119 Sections_element_assignment(const char* name, size_t namelen,
120 Expression* val, bool provide, bool hidden)
121 : assignment_(name, namelen, val, provide, hidden)
124 // Add the symbol to the symbol table.
126 add_symbols_to_table(Symbol_table* symtab)
127 { this->assignment_.add_to_table(symtab); }
129 // Finalize the symbol.
131 finalize_symbols(Symbol_table* symtab, const Layout* layout,
132 bool* dot_has_value, uint64_t* dot_value)
134 this->assignment_.finalize_with_dot(symtab, layout, *dot_has_value,
138 // Set the section address. There is no section here, but if the
139 // value is absolute, we set the symbol. This permits us to use
140 // absolute symbols when setting dot.
142 set_section_addresses(Symbol_table* symtab, Layout* layout,
143 bool* dot_has_value, uint64_t* dot_value)
145 this->assignment_.set_if_absolute(symtab, layout, true, *dot_has_value,
149 // Print for debugging.
154 this->assignment_.print(f);
158 Symbol_assignment assignment_;
161 // An assignment to the dot symbol in a SECTIONS clause outside of an
164 class Sections_element_dot_assignment : public Sections_element
167 Sections_element_dot_assignment(Expression* val)
171 // Finalize the symbol.
173 finalize_symbols(Symbol_table* symtab, const Layout* layout,
174 bool* dot_has_value, uint64_t* dot_value)
177 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value,
179 *dot_has_value = true;
182 // Update the dot symbol while setting section addresses.
184 set_section_addresses(Symbol_table* symtab, Layout* layout,
185 bool* dot_has_value, uint64_t* dot_value)
188 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value,
189 *dot_value, &is_absolute);
191 gold_error(_("dot set to non-absolute value"));
192 *dot_has_value = true;
195 // Print for debugging.
200 this->val_->print(f);
208 // An assertion in a SECTIONS clause outside of an output section.
210 class Sections_element_assertion : public Sections_element
213 Sections_element_assertion(Expression* check, const char* message,
215 : assertion_(check, message, messagelen)
218 // Check the assertion.
220 finalize_symbols(Symbol_table* symtab, const Layout* layout, bool*,
222 { this->assertion_.check(symtab, layout); }
224 // Print for debugging.
229 this->assertion_.print(f);
233 Script_assertion assertion_;
236 // An element in an output section in a SECTIONS clause.
238 class Output_section_element
241 // A list of input sections.
242 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
244 Output_section_element()
247 virtual ~Output_section_element()
250 // Add any symbol being defined to the symbol table.
252 add_symbols_to_table(Symbol_table*)
255 // Finalize symbols and check assertions.
257 finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*)
260 // Return whether this element matches FILE_NAME and SECTION_NAME.
261 // The only real implementation is in Output_section_element_input.
263 match_name(const char*, const char*) const
266 // Set section addresses. This includes applying assignments if the
267 // the expression is an absolute value.
269 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
270 uint64_t*, std::string*, Input_section_list*)
273 // Print the element for debugging purposes.
275 print(FILE* f) const = 0;
278 // Return a fill string that is LENGTH bytes long, filling it with
281 get_fill_string(const std::string* fill, section_size_type length) const;
285 Output_section_element::get_fill_string(const std::string* fill,
286 section_size_type length) const
288 std::string this_fill;
289 this_fill.reserve(length);
290 while (this_fill.length() + fill->length() <= length)
292 if (this_fill.length() < length)
293 this_fill.append(*fill, 0, length - this_fill.length());
297 // A symbol assignment in an output section.
299 class Output_section_element_assignment : public Output_section_element
302 Output_section_element_assignment(const char* name, size_t namelen,
303 Expression* val, bool provide,
305 : assignment_(name, namelen, val, provide, hidden)
308 // Add the symbol to the symbol table.
310 add_symbols_to_table(Symbol_table* symtab)
311 { this->assignment_.add_to_table(symtab); }
313 // Finalize the symbol.
315 finalize_symbols(Symbol_table* symtab, const Layout* layout,
316 bool* dot_has_value, uint64_t* dot_value)
318 this->assignment_.finalize_with_dot(symtab, layout, *dot_has_value,
322 // Set the section address. There is no section here, but if the
323 // value is absolute, we set the symbol. This permits us to use
324 // absolute symbols when setting dot.
326 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
327 uint64_t, uint64_t* dot_value, std::string*,
330 this->assignment_.set_if_absolute(symtab, layout, true, true, *dot_value);
333 // Print for debugging.
338 this->assignment_.print(f);
342 Symbol_assignment assignment_;
345 // An assignment to the dot symbol in an output section.
347 class Output_section_element_dot_assignment : public Output_section_element
350 Output_section_element_dot_assignment(Expression* val)
354 // Finalize the symbol.
356 finalize_symbols(Symbol_table* symtab, const Layout* layout,
357 bool* dot_has_value, uint64_t* dot_value)
360 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value,
362 *dot_has_value = true;
365 // Update the dot symbol while setting section addresses.
367 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
368 uint64_t, uint64_t* dot_value, std::string*,
369 Input_section_list*);
371 // Print for debugging.
376 this->val_->print(f);
384 // Update the dot symbol while setting section addresses.
387 Output_section_element_dot_assignment::set_section_addresses(
388 Symbol_table* symtab,
390 Output_section* output_section,
397 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, true,
398 *dot_value, &is_absolute);
400 gold_error(_("dot set to non-absolute value"));
401 if (next_dot < *dot_value)
402 gold_error(_("dot may not move backward"));
403 if (next_dot > *dot_value && output_section != NULL)
405 section_size_type length = convert_to_section_size_type(next_dot
407 Output_section_data* posd;
409 posd = new Output_data_fixed_space(length, 0);
412 std::string this_fill = this->get_fill_string(fill, length);
413 posd = new Output_data_const(this_fill, 0);
415 output_section->add_output_section_data(posd);
417 *dot_value = next_dot;
420 // An assertion in an output section.
422 class Output_section_element_assertion : public Output_section_element
425 Output_section_element_assertion(Expression* check, const char* message,
427 : assertion_(check, message, messagelen)
434 this->assertion_.print(f);
438 Script_assertion assertion_;
441 // A data item in an output section.
443 class Output_section_element_data : public Output_section_element
446 Output_section_element_data(int size, bool is_signed, Expression* val)
447 : size_(size), is_signed_(is_signed), val_(val)
450 // Finalize symbols--we just need to update dot.
452 finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t* dot_value)
453 { *dot_value += this->size_; }
455 // Store the value in the section.
457 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
458 uint64_t* dot_value, std::string*,
459 Input_section_list*);
461 // Print for debugging.
466 template<bool big_endian>
468 set_fill_string(uint64_t);
470 // The size in bytes.
472 // Whether the value is signed.
478 // Store the value in the section.
481 Output_section_element_data::set_section_addresses(Symbol_table* symtab,
489 gold_assert(os != NULL);
492 uint64_t val = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
495 gold_error(_("data directive with non-absolute value"));
498 if (parameters->is_big_endian())
499 fill = this->set_fill_string<true>(val);
501 fill = this->set_fill_string<false>(val);
503 os->add_output_section_data(new Output_data_const(fill, 0));
505 *dot_value += this->size_;
508 // Get the value to store in a std::string.
510 template<bool big_endian>
512 Output_section_element_data::set_fill_string(uint64_t val)
515 unsigned char buf[8];
519 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
520 ret.assign(reinterpret_cast<char*>(buf), 1);
523 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
524 ret.assign(reinterpret_cast<char*>(buf), 2);
527 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
528 ret.assign(reinterpret_cast<char*>(buf), 4);
531 if (parameters->get_size() == 32)
534 if (this->is_signed_ && (val & 0x80000000) != 0)
535 val |= 0xffffffff00000000LL;
537 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
538 ret.assign(reinterpret_cast<char*>(buf), 8);
546 // Print for debugging.
549 Output_section_element_data::print(FILE* f) const
564 if (this->is_signed_)
572 fprintf(f, " %s(", s);
573 this->val_->print(f);
577 // A fill value setting in an output section.
579 class Output_section_element_fill : public Output_section_element
582 Output_section_element_fill(Expression* val)
586 // Update the fill value while setting section addresses.
588 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
589 uint64_t, uint64_t* dot_value, std::string* fill,
593 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, true,
597 gold_error(_("fill set to non-absolute value"));
598 // FIXME: The GNU linker supports fill values of arbitrary length.
599 unsigned char fill_buff[4];
600 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
601 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
604 // Print for debugging.
608 fprintf(f, " FILL(");
609 this->val_->print(f);
614 // The new fill value.
618 // Return whether STRING contains a wildcard character. This is used
619 // to speed up matching.
622 is_wildcard_string(const std::string& s)
624 return strpbrk(s.c_str(), "?*[") != NULL;
627 // An input section specification in an output section
629 class Output_section_element_input : public Output_section_element
632 Output_section_element_input(const Input_section_spec* spec, bool keep);
634 // Finalize symbols--just update the value of the dot symbol.
636 finalize_symbols(Symbol_table*, const Layout*, bool* dot_has_value,
639 *dot_value = this->final_dot_value_;
640 *dot_has_value = true;
643 // See whether we match FILE_NAME and SECTION_NAME as an input
646 match_name(const char* file_name, const char* section_name) const;
648 // Set the section address.
650 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
651 uint64_t subalign, uint64_t* dot_value,
652 std::string* fill, Input_section_list*);
654 // Print for debugging.
656 print(FILE* f) const;
659 // An input section pattern.
660 struct Input_section_pattern
663 bool pattern_is_wildcard;
666 Input_section_pattern(const char* patterna, size_t patternlena,
668 : pattern(patterna, patternlena),
669 pattern_is_wildcard(is_wildcard_string(this->pattern)),
674 typedef std::vector<Input_section_pattern> Input_section_patterns;
676 // Filename_exclusions is a pair of filename pattern and a bool
677 // indicating whether the filename is a wildcard.
678 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
680 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
681 // indicates whether this is a wildcard pattern.
683 match(const char* string, const char* pattern, bool is_wildcard_pattern)
685 return (is_wildcard_pattern
686 ? fnmatch(pattern, string, 0) == 0
687 : strcmp(string, pattern) == 0);
690 // See if we match a file name.
692 match_file_name(const char* file_name) const;
694 // The file name pattern. If this is the empty string, we match all
696 std::string filename_pattern_;
697 // Whether the file name pattern is a wildcard.
698 bool filename_is_wildcard_;
699 // How the file names should be sorted. This may only be
700 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
701 Sort_wildcard filename_sort_;
702 // The list of file names to exclude.
703 Filename_exclusions filename_exclusions_;
704 // The list of input section patterns.
705 Input_section_patterns input_section_patterns_;
706 // Whether to keep this section when garbage collecting.
708 // The value of dot after including all matching sections.
709 uint64_t final_dot_value_;
712 // Construct Output_section_element_input. The parser records strings
713 // as pointers into a copy of the script file, which will go away when
714 // parsing is complete. We make sure they are in std::string objects.
716 Output_section_element_input::Output_section_element_input(
717 const Input_section_spec* spec,
719 : filename_pattern_(),
720 filename_is_wildcard_(false),
721 filename_sort_(spec->file.sort),
722 filename_exclusions_(),
723 input_section_patterns_(),
727 // The filename pattern "*" is common, and matches all files. Turn
728 // it into the empty string.
729 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
730 this->filename_pattern_.assign(spec->file.name.value,
731 spec->file.name.length);
732 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_);
734 if (spec->input_sections.exclude != NULL)
736 for (String_list::const_iterator p =
737 spec->input_sections.exclude->begin();
738 p != spec->input_sections.exclude->end();
741 bool is_wildcard = is_wildcard_string(*p);
742 this->filename_exclusions_.push_back(std::make_pair(*p,
747 if (spec->input_sections.sections != NULL)
749 Input_section_patterns& isp(this->input_section_patterns_);
750 for (String_sort_list::const_iterator p =
751 spec->input_sections.sections->begin();
752 p != spec->input_sections.sections->end();
754 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
759 // See whether we match FILE_NAME.
762 Output_section_element_input::match_file_name(const char* file_name) const
764 if (!this->filename_pattern_.empty())
766 // If we were called with no filename, we refuse to match a
767 // pattern which requires a file name.
768 if (file_name == NULL)
771 if (!match(file_name, this->filename_pattern_.c_str(),
772 this->filename_is_wildcard_))
776 if (file_name != NULL)
778 // Now we have to see whether FILE_NAME matches one of the
779 // exclusion patterns, if any.
780 for (Filename_exclusions::const_iterator p =
781 this->filename_exclusions_.begin();
782 p != this->filename_exclusions_.end();
785 if (match(file_name, p->first.c_str(), p->second))
793 // See whether we match FILE_NAME and SECTION_NAME.
796 Output_section_element_input::match_name(const char* file_name,
797 const char* section_name) const
799 if (!this->match_file_name(file_name))
802 // If there are no section name patterns, then we match.
803 if (this->input_section_patterns_.empty())
806 // See whether we match the section name patterns.
807 for (Input_section_patterns::const_iterator p =
808 this->input_section_patterns_.begin();
809 p != this->input_section_patterns_.end();
812 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
816 // We didn't match any section names, so we didn't match.
820 // Information we use to sort the input sections.
822 struct Input_section_info
826 std::string section_name;
831 // A class to sort the input sections.
833 class Input_section_sorter
836 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
837 : filename_sort_(filename_sort), section_sort_(section_sort)
841 operator()(const Input_section_info&, const Input_section_info&) const;
844 Sort_wildcard filename_sort_;
845 Sort_wildcard section_sort_;
849 Input_section_sorter::operator()(const Input_section_info& isi1,
850 const Input_section_info& isi2) const
852 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
853 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
854 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
855 && isi1.addralign == isi2.addralign))
857 if (isi1.section_name != isi2.section_name)
858 return isi1.section_name < isi2.section_name;
860 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
861 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
862 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
864 if (isi1.addralign != isi2.addralign)
865 return isi1.addralign < isi2.addralign;
867 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
869 if (isi1.relobj->name() != isi2.relobj->name())
870 return isi1.relobj->name() < isi2.relobj->name();
873 // Otherwise we leave them in the same order.
877 // Set the section address. Look in INPUT_SECTIONS for sections which
878 // match this spec, sort them as specified, and add them to the output
882 Output_section_element_input::set_section_addresses(
885 Output_section* output_section,
889 Input_section_list* input_sections)
891 // We build a list of sections which match each
892 // Input_section_pattern.
894 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
895 size_t input_pattern_count = this->input_section_patterns_.size();
896 if (input_pattern_count == 0)
897 input_pattern_count = 1;
898 Matching_sections matching_sections(input_pattern_count);
900 // Look through the list of sections for this output section. Add
901 // each one which matches to one of the elements of
902 // MATCHING_SECTIONS.
904 Input_section_list::iterator p = input_sections->begin();
905 while (p != input_sections->end())
907 // Calling section_name and section_addralign is not very
909 Input_section_info isi;
910 isi.relobj = p->first;
911 isi.shndx = p->second;
913 // Lock the object so that we can get information about the
914 // section. This is OK since we know we are single-threaded
917 const Task* task = reinterpret_cast<const Task*>(-1);
918 Task_lock_obj<Object> tl(task, p->first);
920 isi.section_name = p->first->section_name(p->second);
921 isi.size = p->first->section_size(p->second);
922 isi.addralign = p->first->section_addralign(p->second);
925 if (!this->match_file_name(isi.relobj->name().c_str()))
927 else if (this->input_section_patterns_.empty())
929 matching_sections[0].push_back(isi);
930 p = input_sections->erase(p);
935 for (i = 0; i < input_pattern_count; ++i)
937 const Input_section_pattern&
938 isp(this->input_section_patterns_[i]);
939 if (match(isi.section_name.c_str(), isp.pattern.c_str(),
940 isp.pattern_is_wildcard))
944 if (i >= this->input_section_patterns_.size())
948 matching_sections[i].push_back(isi);
949 p = input_sections->erase(p);
954 // Look through MATCHING_SECTIONS. Sort each one as specified,
955 // using a stable sort so that we get the default order when
956 // sections are otherwise equal. Add each input section to the
959 for (size_t i = 0; i < input_pattern_count; ++i)
961 if (matching_sections[i].empty())
964 gold_assert(output_section != NULL);
966 const Input_section_pattern& isp(this->input_section_patterns_[i]);
967 if (isp.sort != SORT_WILDCARD_NONE
968 || this->filename_sort_ != SORT_WILDCARD_NONE)
969 std::stable_sort(matching_sections[i].begin(),
970 matching_sections[i].end(),
971 Input_section_sorter(this->filename_sort_,
974 for (std::vector<Input_section_info>::const_iterator p =
975 matching_sections[i].begin();
976 p != matching_sections[i].end();
979 uint64_t this_subalign = p->addralign;
980 if (this_subalign < subalign)
981 this_subalign = subalign;
983 uint64_t address = align_address(*dot_value, this_subalign);
985 if (address > *dot_value && !fill->empty())
987 section_size_type length =
988 convert_to_section_size_type(address - *dot_value);
989 std::string this_fill = this->get_fill_string(fill, length);
990 Output_section_data* posd = new Output_data_const(this_fill, 0);
991 output_section->add_output_section_data(posd);
994 output_section->add_input_section_for_script(p->relobj,
999 *dot_value = address + p->size;
1003 this->final_dot_value_ = *dot_value;
1006 // Print for debugging.
1009 Output_section_element_input::print(FILE* f) const
1014 fprintf(f, "KEEP(");
1016 if (!this->filename_pattern_.empty())
1018 bool need_close_paren = false;
1019 switch (this->filename_sort_)
1021 case SORT_WILDCARD_NONE:
1023 case SORT_WILDCARD_BY_NAME:
1024 fprintf(f, "SORT_BY_NAME(");
1025 need_close_paren = true;
1031 fprintf(f, "%s", this->filename_pattern_.c_str());
1033 if (need_close_paren)
1037 if (!this->input_section_patterns_.empty()
1038 || !this->filename_exclusions_.empty())
1042 bool need_space = false;
1043 if (!this->filename_exclusions_.empty())
1045 fprintf(f, "EXCLUDE_FILE(");
1046 bool need_comma = false;
1047 for (Filename_exclusions::const_iterator p =
1048 this->filename_exclusions_.begin();
1049 p != this->filename_exclusions_.end();
1054 fprintf(f, "%s", p->first.c_str());
1061 for (Input_section_patterns::const_iterator p =
1062 this->input_section_patterns_.begin();
1063 p != this->input_section_patterns_.end();
1069 int close_parens = 0;
1072 case SORT_WILDCARD_NONE:
1074 case SORT_WILDCARD_BY_NAME:
1075 fprintf(f, "SORT_BY_NAME(");
1078 case SORT_WILDCARD_BY_ALIGNMENT:
1079 fprintf(f, "SORT_BY_ALIGNMENT(");
1082 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1083 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1086 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1087 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1094 fprintf(f, "%s", p->pattern.c_str());
1096 for (int i = 0; i < close_parens; ++i)
1111 // An output section.
1113 class Output_section_definition : public Sections_element
1116 typedef Output_section_element::Input_section_list Input_section_list;
1118 Output_section_definition(const char* name, size_t namelen,
1119 const Parser_output_section_header* header);
1121 // Finish the output section with the information in the trailer.
1123 finish(const Parser_output_section_trailer* trailer);
1125 // Add a symbol to be defined.
1127 add_symbol_assignment(const char* name, size_t length, Expression* value,
1128 bool provide, bool hidden);
1130 // Add an assignment to the special dot symbol.
1132 add_dot_assignment(Expression* value);
1134 // Add an assertion.
1136 add_assertion(Expression* check, const char* message, size_t messagelen);
1138 // Add a data item to the current output section.
1140 add_data(int size, bool is_signed, Expression* val);
1142 // Add a setting for the fill value.
1144 add_fill(Expression* val);
1146 // Add an input section specification.
1148 add_input_section(const Input_section_spec* spec, bool keep);
1150 // Add any symbols being defined to the symbol table.
1152 add_symbols_to_table(Symbol_table* symtab);
1154 // Finalize symbols and check assertions.
1156 finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*);
1158 // Return the output section name to use for an input file name and
1161 output_section_name(const char* file_name, const char* section_name,
1164 // Return whether to place an orphan section after this one.
1166 place_orphan_here(const Output_section *os, bool* exact) const;
1168 // Set the section address.
1170 set_section_addresses(Symbol_table* symtab, Layout* layout,
1171 bool* dot_has_value, uint64_t* dot_value);
1173 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1174 // this section is constrained, and the input sections do not match,
1175 // return the constraint, and set *POSD.
1177 check_constraint(Output_section_definition** posd);
1179 // See if this is the alternate output section for a constrained
1180 // output section. If it is, transfer the Output_section and return
1181 // true. Otherwise return false.
1183 alternate_constraint(Output_section_definition*, Section_constraint);
1185 // Get the list of segments to use for an allocated section when
1186 // using a PHDRS clause. If this is an allocated section, return
1187 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
1188 // which it should be attached. If the PHDRS were not specified,
1189 // don't change *PHDRS_LIST.
1191 allocate_to_segment(String_list** phdrs_list);
1193 // Print the contents to the FILE. This is for debugging.
1198 typedef std::vector<Output_section_element*> Output_section_elements;
1200 // The output section name.
1202 // The address. This may be NULL.
1203 Expression* address_;
1204 // The load address. This may be NULL.
1205 Expression* load_address_;
1206 // The alignment. This may be NULL.
1208 // The input section alignment. This may be NULL.
1209 Expression* subalign_;
1210 // The constraint, if any.
1211 Section_constraint constraint_;
1212 // The fill value. This may be NULL.
1214 // The list of segments this section should go into. This may be
1216 String_list* phdrs_;
1217 // The list of elements defining the section.
1218 Output_section_elements elements_;
1219 // The Output_section created for this definition. This will be
1220 // NULL if none was created.
1221 Output_section* output_section_;
1226 Output_section_definition::Output_section_definition(
1229 const Parser_output_section_header* header)
1230 : name_(name, namelen),
1231 address_(header->address),
1232 load_address_(header->load_address),
1233 align_(header->align),
1234 subalign_(header->subalign),
1235 constraint_(header->constraint),
1239 output_section_(NULL)
1243 // Finish an output section.
1246 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1248 this->fill_ = trailer->fill;
1249 this->phdrs_ = trailer->phdrs;
1252 // Add a symbol to be defined.
1255 Output_section_definition::add_symbol_assignment(const char* name,
1261 Output_section_element* p = new Output_section_element_assignment(name,
1266 this->elements_.push_back(p);
1269 // Add an assignment to the special dot symbol.
1272 Output_section_definition::add_dot_assignment(Expression* value)
1274 Output_section_element* p = new Output_section_element_dot_assignment(value);
1275 this->elements_.push_back(p);
1278 // Add an assertion.
1281 Output_section_definition::add_assertion(Expression* check,
1282 const char* message,
1285 Output_section_element* p = new Output_section_element_assertion(check,
1288 this->elements_.push_back(p);
1291 // Add a data item to the current output section.
1294 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1296 Output_section_element* p = new Output_section_element_data(size, is_signed,
1298 this->elements_.push_back(p);
1301 // Add a setting for the fill value.
1304 Output_section_definition::add_fill(Expression* val)
1306 Output_section_element* p = new Output_section_element_fill(val);
1307 this->elements_.push_back(p);
1310 // Add an input section specification.
1313 Output_section_definition::add_input_section(const Input_section_spec* spec,
1316 Output_section_element* p = new Output_section_element_input(spec, keep);
1317 this->elements_.push_back(p);
1320 // Add any symbols being defined to the symbol table.
1323 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
1325 for (Output_section_elements::iterator p = this->elements_.begin();
1326 p != this->elements_.end();
1328 (*p)->add_symbols_to_table(symtab);
1331 // Finalize symbols and check assertions.
1334 Output_section_definition::finalize_symbols(Symbol_table* symtab,
1335 const Layout* layout,
1336 bool* dot_has_value,
1337 uint64_t* dot_value)
1339 if (this->output_section_ != NULL)
1340 *dot_value = this->output_section_->address();
1343 uint64_t address = *dot_value;
1344 if (this->address_ != NULL)
1347 address = this->address_->eval_with_dot(symtab, layout,
1348 *dot_has_value, *dot_value,
1351 if (this->align_ != NULL)
1354 uint64_t align = this->align_->eval_with_dot(symtab, layout,
1358 address = align_address(address, align);
1360 *dot_value = address;
1362 *dot_has_value = true;
1364 for (Output_section_elements::iterator p = this->elements_.begin();
1365 p != this->elements_.end();
1367 (*p)->finalize_symbols(symtab, layout, dot_has_value, dot_value);
1370 // Return the output section name to use for an input section name.
1373 Output_section_definition::output_section_name(const char* file_name,
1374 const char* section_name,
1375 Output_section*** slot)
1377 // Ask each element whether it matches NAME.
1378 for (Output_section_elements::const_iterator p = this->elements_.begin();
1379 p != this->elements_.end();
1382 if ((*p)->match_name(file_name, section_name))
1384 // We found a match for NAME, which means that it should go
1385 // into this output section.
1386 *slot = &this->output_section_;
1387 return this->name_.c_str();
1391 // We don't know about this section name.
1395 // Return whether to place an orphan output section after this
1399 Output_section_definition::place_orphan_here(const Output_section *os,
1402 // Check for the simple case first.
1403 if (this->output_section_ != NULL
1404 && this->output_section_->type() == os->type()
1405 && this->output_section_->flags() == os->flags())
1411 // Otherwise use some heuristics.
1413 if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
1416 if (os->type() == elfcpp::SHT_NOBITS)
1418 if (this->name_ == ".bss")
1423 if (this->output_section_ != NULL
1424 && this->output_section_->type() == elfcpp::SHT_NOBITS)
1427 else if (os->type() == elfcpp::SHT_NOTE)
1429 if (this->output_section_ != NULL
1430 && this->output_section_->type() == elfcpp::SHT_NOTE)
1435 if (this->name_.compare(0, 5, ".note") == 0)
1440 if (this->name_ == ".interp")
1442 if (this->output_section_ != NULL
1443 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1444 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1447 else if (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
1449 if (this->name_.compare(0, 4, ".rel") == 0)
1454 if (this->output_section_ != NULL
1455 && (this->output_section_->type() == elfcpp::SHT_REL
1456 || this->output_section_->type() == elfcpp::SHT_RELA))
1461 if (this->output_section_ != NULL
1462 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1463 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1466 else if (os->type() == elfcpp::SHT_PROGBITS
1467 && (os->flags() & elfcpp::SHF_WRITE) != 0)
1469 if (this->name_ == ".data")
1474 if (this->output_section_ != NULL
1475 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1476 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
1479 else if (os->type() == elfcpp::SHT_PROGBITS
1480 && (os->flags() & elfcpp::SHF_EXECINSTR) != 0)
1482 if (this->name_ == ".text")
1487 if (this->output_section_ != NULL
1488 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1489 && (this->output_section_->flags() & elfcpp::SHF_EXECINSTR) != 0)
1492 else if (os->type() == elfcpp::SHT_PROGBITS
1493 || (os->type() != elfcpp::SHT_PROGBITS
1494 && (os->flags() & elfcpp::SHF_WRITE) == 0))
1496 if (this->name_ == ".rodata")
1501 if (this->output_section_ != NULL
1502 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1503 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1510 // Set the section address. Note that the OUTPUT_SECTION_ field will
1511 // be NULL if no input sections were mapped to this output section.
1512 // We still have to adjust dot and process symbol assignments.
1515 Output_section_definition::set_section_addresses(Symbol_table* symtab,
1517 bool* dot_has_value,
1518 uint64_t* dot_value)
1522 if (this->address_ != NULL)
1524 address = this->address_->eval_with_dot(symtab, layout, *dot_has_value,
1525 *dot_value, &is_absolute);
1527 gold_error(_("address of section %s is not absolute"),
1528 this->name_.c_str());
1532 if (!*dot_has_value)
1533 gold_error(_("no address given for section %s"),
1534 this->name_.c_str());
1535 address = *dot_value;
1539 if (this->align_ == NULL)
1541 if (this->output_section_ == NULL)
1544 align = this->output_section_->addralign();
1548 align = this->align_->eval_with_dot(symtab, layout, *dot_has_value,
1549 *dot_value, &is_absolute);
1551 gold_error(_("alignment of section %s is not absolute"),
1552 this->name_.c_str());
1553 if (this->output_section_ != NULL)
1554 this->output_section_->set_addralign(align);
1557 address = align_address(address, align);
1559 *dot_value = address;
1560 *dot_has_value = true;
1562 // The address of non-SHF_ALLOC sections is forced to zero,
1563 // regardless of what the linker script wants.
1564 if (this->output_section_ != NULL
1565 && (this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0)
1566 this->output_section_->set_address(address);
1568 if (this->load_address_ != NULL && this->output_section_ != NULL)
1570 uint64_t load_address =
1571 this->load_address_->eval_with_dot(symtab, layout, *dot_has_value,
1572 *dot_value, &is_absolute);
1574 gold_error(_("load address of section %s is not absolute"),
1575 this->name_.c_str());
1576 this->output_section_->set_load_address(load_address);
1580 if (this->subalign_ == NULL)
1584 subalign = this->subalign_->eval_with_dot(symtab, layout, *dot_has_value,
1585 *dot_value, &is_absolute);
1587 gold_error(_("subalign of section %s is not absolute"),
1588 this->name_.c_str());
1592 if (this->fill_ != NULL)
1594 // FIXME: The GNU linker supports fill values of arbitrary
1596 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout,
1601 gold_error(_("fill of section %s is not absolute"),
1602 this->name_.c_str());
1603 unsigned char fill_buff[4];
1604 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1605 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
1608 Input_section_list input_sections;
1609 if (this->output_section_ != NULL)
1611 // Get the list of input sections attached to this output
1612 // section. This will leave the output section with only
1613 // Output_section_data entries.
1614 address += this->output_section_->get_input_sections(address,
1617 *dot_value = address;
1620 for (Output_section_elements::iterator p = this->elements_.begin();
1621 p != this->elements_.end();
1623 (*p)->set_section_addresses(symtab, layout, this->output_section_,
1624 subalign, dot_value, &fill, &input_sections);
1626 gold_assert(input_sections.empty());
1629 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1630 // this section is constrained, and the input sections do not match,
1631 // return the constraint, and set *POSD.
1634 Output_section_definition::check_constraint(Output_section_definition** posd)
1636 switch (this->constraint_)
1638 case CONSTRAINT_NONE:
1639 return CONSTRAINT_NONE;
1641 case CONSTRAINT_ONLY_IF_RO:
1642 if (this->output_section_ != NULL
1643 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
1646 return CONSTRAINT_ONLY_IF_RO;
1648 return CONSTRAINT_NONE;
1650 case CONSTRAINT_ONLY_IF_RW:
1651 if (this->output_section_ != NULL
1652 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1655 return CONSTRAINT_ONLY_IF_RW;
1657 return CONSTRAINT_NONE;
1659 case CONSTRAINT_SPECIAL:
1660 if (this->output_section_ != NULL)
1661 gold_error(_("SPECIAL constraints are not implemented"));
1662 return CONSTRAINT_NONE;
1669 // See if this is the alternate output section for a constrained
1670 // output section. If it is, transfer the Output_section and return
1671 // true. Otherwise return false.
1674 Output_section_definition::alternate_constraint(
1675 Output_section_definition* posd,
1676 Section_constraint constraint)
1678 if (this->name_ != posd->name_)
1683 case CONSTRAINT_ONLY_IF_RO:
1684 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
1688 case CONSTRAINT_ONLY_IF_RW:
1689 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
1697 // We have found the alternate constraint. We just need to move
1698 // over the Output_section. When constraints are used properly,
1699 // THIS should not have an output_section pointer, as all the input
1700 // sections should have matched the other definition.
1702 if (this->output_section_ != NULL)
1703 gold_error(_("mismatched definition for constrained sections"));
1705 this->output_section_ = posd->output_section_;
1706 posd->output_section_ = NULL;
1711 // Get the list of segments to use for an allocated section when using
1712 // a PHDRS clause. If this is an allocated section, return the
1713 // Output_section, and set *PHDRS_LIST to the list of PHDRS to which
1714 // it should be attached. If the PHDRS were not specified, don't
1715 // change *PHDRS_LIST.
1718 Output_section_definition::allocate_to_segment(String_list** phdrs_list)
1720 if (this->output_section_ == NULL)
1722 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
1724 if (this->phdrs_ != NULL)
1725 *phdrs_list = this->phdrs_;
1726 return this->output_section_;
1729 // Print for debugging.
1732 Output_section_definition::print(FILE* f) const
1734 fprintf(f, " %s ", this->name_.c_str());
1736 if (this->address_ != NULL)
1738 this->address_->print(f);
1744 if (this->load_address_ != NULL)
1747 this->load_address_->print(f);
1751 if (this->align_ != NULL)
1753 fprintf(f, "ALIGN(");
1754 this->align_->print(f);
1758 if (this->subalign_ != NULL)
1760 fprintf(f, "SUBALIGN(");
1761 this->subalign_->print(f);
1767 for (Output_section_elements::const_iterator p = this->elements_.begin();
1768 p != this->elements_.end();
1774 if (this->fill_ != NULL)
1777 this->fill_->print(f);
1780 if (this->phdrs_ != NULL)
1782 for (String_list::const_iterator p = this->phdrs_->begin();
1783 p != this->phdrs_->end();
1785 fprintf(f, " :%s", p->c_str());
1791 // An output section created to hold orphaned input sections. These
1792 // do not actually appear in linker scripts. However, for convenience
1793 // when setting the output section addresses, we put a marker to these
1794 // sections in the appropriate place in the list of SECTIONS elements.
1796 class Orphan_output_section : public Sections_element
1799 Orphan_output_section(Output_section* os)
1803 // Return whether to place an orphan section after this one.
1805 place_orphan_here(const Output_section *os, bool* exact) const;
1807 // Set section addresses.
1809 set_section_addresses(Symbol_table*, Layout*, bool*, uint64_t*);
1811 // Get the list of segments to use for an allocated section when
1812 // using a PHDRS clause. If this is an allocated section, return
1813 // the Output_section.
1815 allocate_to_segment(String_list**);
1817 // Print for debugging.
1819 print(FILE* f) const
1821 fprintf(f, " marker for orphaned output section %s\n",
1826 Output_section* os_;
1829 // Whether to place another orphan section after this one.
1832 Orphan_output_section::place_orphan_here(const Output_section* os,
1835 if (this->os_->type() == os->type()
1836 && this->os_->flags() == os->flags())
1844 // Set section addresses.
1847 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
1848 bool* dot_has_value,
1849 uint64_t* dot_value)
1851 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
1853 if (!*dot_has_value)
1854 gold_error(_("no address for orphan section %s"), this->os_->name());
1856 uint64_t address = *dot_value;
1857 address = align_address(address, this->os_->addralign());
1859 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
1860 this->os_->set_address(address);
1862 Input_section_list input_sections;
1863 address += this->os_->get_input_sections(address, "", &input_sections);
1865 for (Input_section_list::iterator p = input_sections.begin();
1866 p != input_sections.end();
1872 // We know what are single-threaded, so it is OK to lock the
1875 const Task* task = reinterpret_cast<const Task*>(-1);
1876 Task_lock_obj<Object> tl(task, p->first);
1877 addralign = p->first->section_addralign(p->second);
1878 size = p->first->section_size(p->second);
1881 address = align_address(address, addralign);
1882 this->os_->add_input_section_for_script(p->first, p->second, size, 0);
1886 *dot_value = address;
1889 // Get the list of segments to use for an allocated section when using
1890 // a PHDRS clause. If this is an allocated section, return the
1891 // Output_section. We don't change the list of segments.
1894 Orphan_output_section::allocate_to_segment(String_list**)
1896 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
1901 // Class Phdrs_element. A program header from a PHDRS clause.
1906 Phdrs_element(const char* name, size_t namelen, unsigned int type,
1907 bool includes_filehdr, bool includes_phdrs,
1908 bool is_flags_valid, unsigned int flags,
1909 Expression* load_address)
1910 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
1911 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
1912 flags_(flags), load_address_(load_address), load_address_value_(0),
1916 // Return the name of this segment.
1919 { return this->name_; }
1921 // Return the type of the segment.
1924 { return this->type_; }
1926 // Whether to include the file header.
1928 includes_filehdr() const
1929 { return this->includes_filehdr_; }
1931 // Whether to include the program headers.
1933 includes_phdrs() const
1934 { return this->includes_phdrs_; }
1936 // Return whether there is a load address.
1938 has_load_address() const
1939 { return this->load_address_ != NULL; }
1941 // Evaluate the load address expression if there is one.
1943 eval_load_address(Symbol_table* symtab, Layout* layout)
1945 if (this->load_address_ != NULL)
1946 this->load_address_value_ = this->load_address_->eval(symtab, layout);
1949 // Return the load address.
1951 load_address() const
1953 gold_assert(this->load_address_ != NULL);
1954 return this->load_address_value_;
1957 // Create the segment.
1959 create_segment(Layout* layout)
1961 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
1962 return this->segment_;
1965 // Return the segment.
1968 { return this->segment_; }
1970 // Set the segment flags if appropriate.
1972 set_flags_if_valid()
1974 if (this->is_flags_valid_)
1975 this->segment_->set_flags(this->flags_);
1978 // Print for debugging.
1983 // The name used in the script.
1985 // The type of the segment (PT_LOAD, etc.).
1987 // Whether this segment includes the file header.
1988 bool includes_filehdr_;
1989 // Whether this segment includes the section headers.
1990 bool includes_phdrs_;
1991 // Whether the flags were explicitly specified.
1992 bool is_flags_valid_;
1993 // The flags for this segment (PF_R, etc.) if specified.
1994 unsigned int flags_;
1995 // The expression for the load address for this segment. This may
1997 Expression* load_address_;
1998 // The actual load address from evaluating the expression.
1999 uint64_t load_address_value_;
2000 // The segment itself.
2001 Output_segment* segment_;
2004 // Print for debugging.
2007 Phdrs_element::print(FILE* f) const
2009 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
2010 if (this->includes_filehdr_)
2011 fprintf(f, " FILEHDR");
2012 if (this->includes_phdrs_)
2013 fprintf(f, " PHDRS");
2014 if (this->is_flags_valid_)
2015 fprintf(f, " FLAGS(%u)", this->flags_);
2016 if (this->load_address_ != NULL)
2019 this->load_address_->print(f);
2025 // Class Script_sections.
2027 Script_sections::Script_sections()
2028 : saw_sections_clause_(false),
2029 in_sections_clause_(false),
2030 sections_elements_(NULL),
2031 output_section_(NULL),
2032 phdrs_elements_(NULL)
2036 // Start a SECTIONS clause.
2039 Script_sections::start_sections()
2041 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2042 this->saw_sections_clause_ = true;
2043 this->in_sections_clause_ = true;
2044 if (this->sections_elements_ == NULL)
2045 this->sections_elements_ = new Sections_elements;
2048 // Finish a SECTIONS clause.
2051 Script_sections::finish_sections()
2053 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2054 this->in_sections_clause_ = false;
2057 // Add a symbol to be defined.
2060 Script_sections::add_symbol_assignment(const char* name, size_t length,
2061 Expression* val, bool provide,
2064 if (this->output_section_ != NULL)
2065 this->output_section_->add_symbol_assignment(name, length, val,
2069 Sections_element* p = new Sections_element_assignment(name, length,
2072 this->sections_elements_->push_back(p);
2076 // Add an assignment to the special dot symbol.
2079 Script_sections::add_dot_assignment(Expression* val)
2081 if (this->output_section_ != NULL)
2082 this->output_section_->add_dot_assignment(val);
2085 Sections_element* p = new Sections_element_dot_assignment(val);
2086 this->sections_elements_->push_back(p);
2090 // Add an assertion.
2093 Script_sections::add_assertion(Expression* check, const char* message,
2096 if (this->output_section_ != NULL)
2097 this->output_section_->add_assertion(check, message, messagelen);
2100 Sections_element* p = new Sections_element_assertion(check, message,
2102 this->sections_elements_->push_back(p);
2106 // Start processing entries for an output section.
2109 Script_sections::start_output_section(
2112 const Parser_output_section_header *header)
2114 Output_section_definition* posd = new Output_section_definition(name,
2117 this->sections_elements_->push_back(posd);
2118 gold_assert(this->output_section_ == NULL);
2119 this->output_section_ = posd;
2122 // Stop processing entries for an output section.
2125 Script_sections::finish_output_section(
2126 const Parser_output_section_trailer* trailer)
2128 gold_assert(this->output_section_ != NULL);
2129 this->output_section_->finish(trailer);
2130 this->output_section_ = NULL;
2133 // Add a data item to the current output section.
2136 Script_sections::add_data(int size, bool is_signed, Expression* val)
2138 gold_assert(this->output_section_ != NULL);
2139 this->output_section_->add_data(size, is_signed, val);
2142 // Add a fill value setting to the current output section.
2145 Script_sections::add_fill(Expression* val)
2147 gold_assert(this->output_section_ != NULL);
2148 this->output_section_->add_fill(val);
2151 // Add an input section specification to the current output section.
2154 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2156 gold_assert(this->output_section_ != NULL);
2157 this->output_section_->add_input_section(spec, keep);
2160 // Add any symbols we are defining to the symbol table.
2163 Script_sections::add_symbols_to_table(Symbol_table* symtab)
2165 if (!this->saw_sections_clause_)
2167 for (Sections_elements::iterator p = this->sections_elements_->begin();
2168 p != this->sections_elements_->end();
2170 (*p)->add_symbols_to_table(symtab);
2173 // Finalize symbols and check assertions.
2176 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2178 if (!this->saw_sections_clause_)
2180 bool dot_has_value = false;
2181 uint64_t dot_value = 0;
2182 for (Sections_elements::iterator p = this->sections_elements_->begin();
2183 p != this->sections_elements_->end();
2185 (*p)->finalize_symbols(symtab, layout, &dot_has_value, &dot_value);
2188 // Return the name of the output section to use for an input file name
2189 // and section name.
2192 Script_sections::output_section_name(const char* file_name,
2193 const char* section_name,
2194 Output_section*** output_section_slot)
2196 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2197 p != this->sections_elements_->end();
2200 const char* ret = (*p)->output_section_name(file_name, section_name,
2201 output_section_slot);
2205 // The special name /DISCARD/ means that the input section
2206 // should be discarded.
2207 if (strcmp(ret, "/DISCARD/") == 0)
2209 *output_section_slot = NULL;
2216 // If we couldn't find a mapping for the name, the output section
2217 // gets the name of the input section.
2219 *output_section_slot = NULL;
2221 return section_name;
2224 // Place a marker for an orphan output section into the SECTIONS
2228 Script_sections::place_orphan(Output_section* os)
2230 // Look for an output section definition which matches the output
2231 // section. Put a marker after that section.
2232 Sections_elements::iterator place = this->sections_elements_->end();
2233 for (Sections_elements::iterator p = this->sections_elements_->begin();
2234 p != this->sections_elements_->end();
2238 if ((*p)->place_orphan_here(os, &exact))
2246 // The insert function puts the new element before the iterator.
2247 if (place != this->sections_elements_->end())
2250 this->sections_elements_->insert(place, new Orphan_output_section(os));
2253 // Set the addresses of all the output sections. Walk through all the
2254 // elements, tracking the dot symbol. Apply assignments which set
2255 // absolute symbol values, in case they are used when setting dot.
2256 // Fill in data statement values. As we find output sections, set the
2257 // address, set the address of all associated input sections, and
2258 // update dot. Return the segment which should hold the file header
2259 // and segment headers, if any.
2262 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2264 gold_assert(this->saw_sections_clause_);
2266 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2267 // for our representation.
2268 for (Sections_elements::iterator p = this->sections_elements_->begin();
2269 p != this->sections_elements_->end();
2272 Output_section_definition* posd;
2273 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2274 if (failed_constraint != CONSTRAINT_NONE)
2276 Sections_elements::iterator q;
2277 for (q = this->sections_elements_->begin();
2278 q != this->sections_elements_->end();
2283 if ((*q)->alternate_constraint(posd, failed_constraint))
2288 if (q == this->sections_elements_->end())
2289 gold_error(_("no matching section constraint"));
2293 bool dot_has_value = false;
2294 uint64_t dot_value = 0;
2295 for (Sections_elements::iterator p = this->sections_elements_->begin();
2296 p != this->sections_elements_->end();
2298 (*p)->set_section_addresses(symtab, layout, &dot_has_value, &dot_value);
2300 if (this->phdrs_elements_ != NULL)
2302 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2303 p != this->phdrs_elements_->end();
2305 (*p)->eval_load_address(symtab, layout);
2308 return this->create_segments(layout);
2311 // Sort the sections in order to put them into segments.
2313 class Sort_output_sections
2317 operator()(const Output_section* os1, const Output_section* os2) const;
2321 Sort_output_sections::operator()(const Output_section* os1,
2322 const Output_section* os2) const
2324 // Sort first by the load address.
2325 uint64_t lma1 = (os1->has_load_address()
2326 ? os1->load_address()
2328 uint64_t lma2 = (os2->has_load_address()
2329 ? os2->load_address()
2334 // Then sort by the virtual address.
2335 if (os1->address() != os2->address())
2336 return os1->address() < os2->address();
2338 // Sort TLS sections to the end.
2339 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
2340 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
2344 // Sort PROGBITS before NOBITS.
2345 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
2347 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
2350 // Otherwise we don't care.
2354 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2355 // We treat a section with the SHF_TLS flag set as taking up space
2356 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2357 // space for them in the file.
2360 Script_sections::is_bss_section(const Output_section* os)
2362 return (os->type() == elfcpp::SHT_NOBITS
2363 && (os->flags() & elfcpp::SHF_TLS) == 0);
2366 // Return the size taken by the file header and the program headers.
2369 Script_sections::total_header_size(Layout* layout) const
2371 size_t segment_count = layout->segment_count();
2372 size_t file_header_size;
2373 size_t segment_headers_size;
2374 if (parameters->get_size() == 32)
2376 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
2377 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
2379 else if (parameters->get_size() == 64)
2381 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
2382 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
2387 return file_header_size + segment_headers_size;
2390 // Return the amount we have to subtract from the LMA to accomodate
2391 // headers of the given size. The complication is that the file
2392 // header have to be at the start of a page, as otherwise it will not
2393 // be at the start of the file.
2396 Script_sections::header_size_adjustment(uint64_t lma,
2397 size_t sizeof_headers) const
2399 const uint64_t abi_pagesize = parameters->target()->abi_pagesize();
2400 uint64_t hdr_lma = lma - sizeof_headers;
2401 hdr_lma &= ~(abi_pagesize - 1);
2402 return lma - hdr_lma;
2405 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2406 // the segment which should hold the file header and segment headers,
2410 Script_sections::create_segments(Layout* layout)
2412 gold_assert(this->saw_sections_clause_);
2414 if (parameters->output_is_object())
2417 if (this->saw_phdrs_clause())
2418 return create_segments_from_phdrs_clause(layout);
2420 Layout::Section_list sections;
2421 layout->get_allocated_sections(§ions);
2423 // Sort the sections by address.
2424 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
2426 this->create_note_and_tls_segments(layout, §ions);
2428 // Walk through the sections adding them to PT_LOAD segments.
2429 const uint64_t abi_pagesize = parameters->target()->abi_pagesize();
2430 Output_segment* first_seg = NULL;
2431 Output_segment* current_seg = NULL;
2432 bool is_current_seg_readonly = true;
2433 Layout::Section_list::iterator plast = sections.end();
2434 uint64_t last_vma = 0;
2435 uint64_t last_lma = 0;
2436 uint64_t last_size = 0;
2437 for (Layout::Section_list::iterator p = sections.begin();
2438 p != sections.end();
2441 const uint64_t vma = (*p)->address();
2442 const uint64_t lma = ((*p)->has_load_address()
2443 ? (*p)->load_address()
2445 const uint64_t size = (*p)->current_data_size();
2447 bool need_new_segment;
2448 if (current_seg == NULL)
2449 need_new_segment = true;
2450 else if (lma - vma != last_lma - last_vma)
2452 // This section has a different LMA relationship than the
2453 // last one; we need a new segment.
2454 need_new_segment = true;
2456 else if (align_address(last_lma + last_size, abi_pagesize)
2457 < align_address(lma, abi_pagesize))
2459 // Putting this section in the segment would require
2461 need_new_segment = true;
2463 else if (is_bss_section(*plast) && !is_bss_section(*p))
2465 // A non-BSS section can not follow a BSS section in the
2467 need_new_segment = true;
2469 else if (is_current_seg_readonly
2470 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2472 // Don't put a writable section in the same segment as a
2473 // non-writable section.
2474 need_new_segment = true;
2478 // Otherwise, reuse the existing segment.
2479 need_new_segment = false;
2482 elfcpp::Elf_Word seg_flags =
2483 Layout::section_flags_to_segment((*p)->flags());
2485 if (need_new_segment)
2487 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2489 current_seg->set_addresses(vma, lma);
2490 if (first_seg == NULL)
2491 first_seg = current_seg;
2492 is_current_seg_readonly = true;
2495 current_seg->add_output_section(*p, seg_flags);
2497 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2498 is_current_seg_readonly = false;
2506 // An ELF program should work even if the program headers are not in
2507 // a PT_LOAD segment. However, it appears that the Linux kernel
2508 // does not set the AT_PHDR auxiliary entry in that case. It sets
2509 // the load address to p_vaddr - p_offset of the first PT_LOAD
2510 // segment. It then sets AT_PHDR to the load address plus the
2511 // offset to the program headers, e_phoff in the file header. This
2512 // fails when the program headers appear in the file before the
2513 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2514 // segment to hold the file header and the program headers. This is
2515 // effectively what the GNU linker does, and it is slightly more
2516 // efficient in any case. We try to use the first PT_LOAD segment
2517 // if we can, otherwise we make a new one.
2519 size_t sizeof_headers = this->total_header_size(layout);
2521 if (first_seg != NULL
2522 && (first_seg->paddr() & (abi_pagesize - 1)) >= sizeof_headers)
2524 first_seg->set_addresses(first_seg->vaddr() - sizeof_headers,
2525 first_seg->paddr() - sizeof_headers);
2529 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2531 if (first_seg == NULL)
2532 load_seg->set_addresses(0, 0);
2535 uint64_t vma = first_seg->vaddr();
2536 uint64_t lma = first_seg->paddr();
2538 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
2539 if (lma >= subtract && vma >= subtract)
2540 load_seg->set_addresses(vma - subtract, lma - subtract);
2543 // We could handle this case by create the file header
2544 // outside of any PT_LOAD segment, and creating a new
2545 // PT_LOAD segment after the others to hold the segment
2547 gold_error(_("sections loaded on first page without room for "
2548 "file and program headers are not supported"));
2555 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2556 // segment if there are any SHT_TLS sections.
2559 Script_sections::create_note_and_tls_segments(
2561 const Layout::Section_list* sections)
2563 gold_assert(!this->saw_phdrs_clause());
2565 bool saw_tls = false;
2566 for (Layout::Section_list::const_iterator p = sections->begin();
2567 p != sections->end();
2570 if ((*p)->type() == elfcpp::SHT_NOTE)
2572 elfcpp::Elf_Word seg_flags =
2573 Layout::section_flags_to_segment((*p)->flags());
2574 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
2576 oseg->add_output_section(*p, seg_flags);
2578 // Incorporate any subsequent SHT_NOTE sections, in the
2579 // hopes that the script is sensible.
2580 Layout::Section_list::const_iterator pnext = p + 1;
2581 while (pnext != sections->end()
2582 && (*pnext)->type() == elfcpp::SHT_NOTE)
2584 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2585 oseg->add_output_section(*pnext, seg_flags);
2591 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2594 gold_error(_("TLS sections are not adjacent"));
2596 elfcpp::Elf_Word seg_flags =
2597 Layout::section_flags_to_segment((*p)->flags());
2598 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
2600 oseg->add_output_section(*p, seg_flags);
2602 Layout::Section_list::const_iterator pnext = p + 1;
2603 while (pnext != sections->end()
2604 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
2606 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2607 oseg->add_output_section(*pnext, seg_flags);
2617 // Add a program header. The PHDRS clause is syntactically distinct
2618 // from the SECTIONS clause, but we implement it with the SECTIONS
2619 // support becauase PHDRS is useless if there is no SECTIONS clause.
2622 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
2623 bool includes_filehdr, bool includes_phdrs,
2624 bool is_flags_valid, unsigned int flags,
2625 Expression* load_address)
2627 if (this->phdrs_elements_ == NULL)
2628 this->phdrs_elements_ = new Phdrs_elements();
2629 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
2632 is_flags_valid, flags,
2636 // Return the number of segments we expect to create based on the
2637 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2640 Script_sections::expected_segment_count(const Layout* layout) const
2642 if (this->saw_phdrs_clause())
2643 return this->phdrs_elements_->size();
2645 Layout::Section_list sections;
2646 layout->get_allocated_sections(§ions);
2648 // We assume that we will need two PT_LOAD segments.
2651 bool saw_note = false;
2652 bool saw_tls = false;
2653 for (Layout::Section_list::const_iterator p = sections.begin();
2654 p != sections.end();
2657 if ((*p)->type() == elfcpp::SHT_NOTE)
2659 // Assume that all note sections will fit into a single
2667 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2669 // There can only be one PT_TLS segment.
2681 // Create the segments from a PHDRS clause. Return the segment which
2682 // should hold the file header and program headers, if any.
2685 Script_sections::create_segments_from_phdrs_clause(Layout* layout)
2687 this->attach_sections_using_phdrs_clause(layout);
2688 return this->set_phdrs_clause_addresses(layout);
2691 // Create the segments from the PHDRS clause, and put the output
2692 // sections in them.
2695 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
2697 typedef std::map<std::string, Output_segment*> Name_to_segment;
2698 Name_to_segment name_to_segment;
2699 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2700 p != this->phdrs_elements_->end();
2702 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
2704 // Walk through the output sections and attach them to segments.
2705 // Output sections in the script which do not list segments are
2706 // attached to the same set of segments as the immediately preceding
2708 String_list* phdr_names = NULL;
2709 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2710 p != this->sections_elements_->end();
2713 Output_section* os = (*p)->allocate_to_segment(&phdr_names);
2717 if (phdr_names == NULL)
2719 gold_error(_("allocated section not in any segment"));
2723 bool in_load_segment = false;
2724 for (String_list::const_iterator q = phdr_names->begin();
2725 q != phdr_names->end();
2728 Name_to_segment::const_iterator r = name_to_segment.find(*q);
2729 if (r == name_to_segment.end())
2730 gold_error(_("no segment %s"), q->c_str());
2733 elfcpp::Elf_Word seg_flags =
2734 Layout::section_flags_to_segment(os->flags());
2735 r->second->add_output_section(os, seg_flags);
2737 if (r->second->type() == elfcpp::PT_LOAD)
2739 if (in_load_segment)
2740 gold_error(_("section in two PT_LOAD segments"));
2741 in_load_segment = true;
2746 if (!in_load_segment)
2747 gold_error(_("allocated section not in any PT_LOAD segment"));
2751 // Set the addresses for segments created from a PHDRS clause. Return
2752 // the segment which should hold the file header and program headers,
2756 Script_sections::set_phdrs_clause_addresses(Layout* layout)
2758 Output_segment* load_seg = NULL;
2759 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2760 p != this->phdrs_elements_->end();
2763 // Note that we have to set the flags after adding the output
2764 // sections to the segment, as adding an output segment can
2765 // change the flags.
2766 (*p)->set_flags_if_valid();
2768 Output_segment* oseg = (*p)->segment();
2770 if (oseg->type() != elfcpp::PT_LOAD)
2772 // The addresses of non-PT_LOAD segments are set from the
2773 // PT_LOAD segments.
2774 if ((*p)->has_load_address())
2775 gold_error(_("may only specify load address for PT_LOAD segment"));
2779 // The output sections should have addresses from the SECTIONS
2780 // clause. The addresses don't have to be in order, so find the
2781 // one with the lowest load address. Use that to set the
2782 // address of the segment.
2784 Output_section* osec = oseg->section_with_lowest_load_address();
2787 oseg->set_addresses(0, 0);
2791 uint64_t vma = osec->address();
2792 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
2794 // Override the load address of the section with the load
2795 // address specified for the segment.
2796 if ((*p)->has_load_address())
2798 if (osec->has_load_address())
2799 gold_warning(_("PHDRS load address overrides "
2800 "section %s load address"),
2803 lma = (*p)->load_address();
2806 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
2807 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
2809 // We could support this if we wanted to.
2810 gold_error(_("using only one of FILEHDR and PHDRS is "
2811 "not currently supported"));
2815 size_t sizeof_headers = this->total_header_size(layout);
2816 uint64_t subtract = this->header_size_adjustment(lma,
2818 if (lma >= subtract && vma >= subtract)
2825 gold_error(_("sections loaded on first page without room "
2826 "for file and program headers "
2827 "are not supported"));
2830 if (load_seg != NULL)
2831 gold_error(_("using FILEHDR and PHDRS on more than one "
2832 "PT_LOAD segment is not currently supported"));
2836 oseg->set_addresses(vma, lma);
2842 // Add the file header and segment headers to non-load segments
2843 // specified in the PHDRS clause.
2846 Script_sections::put_headers_in_phdrs(Output_data* file_header,
2847 Output_data* segment_headers)
2849 gold_assert(this->saw_phdrs_clause());
2850 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2851 p != this->phdrs_elements_->end();
2854 if ((*p)->type() != elfcpp::PT_LOAD)
2856 if ((*p)->includes_phdrs())
2857 (*p)->segment()->add_initial_output_data(segment_headers);
2858 if ((*p)->includes_filehdr())
2859 (*p)->segment()->add_initial_output_data(file_header);
2864 // Print the SECTIONS clause to F for debugging.
2867 Script_sections::print(FILE* f) const
2869 if (!this->saw_sections_clause_)
2872 fprintf(f, "SECTIONS {\n");
2874 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2875 p != this->sections_elements_->end();
2881 if (this->phdrs_elements_ != NULL)
2883 fprintf(f, "PHDRS {\n");
2884 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2885 p != this->phdrs_elements_->end();
2892 } // End namespace gold.