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*, 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*, 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,
134 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
137 // Set the section address. There is no section here, but if the
138 // value is absolute, we set the symbol. This permits us to use
139 // absolute symbols when setting dot.
141 set_section_addresses(Symbol_table* symtab, Layout* layout,
144 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
147 // Print for debugging.
152 this->assignment_.print(f);
156 Symbol_assignment assignment_;
159 // An assignment to the dot symbol in a SECTIONS clause outside of an
162 class Sections_element_dot_assignment : public Sections_element
165 Sections_element_dot_assignment(Expression* val)
169 // Finalize the symbol.
171 finalize_symbols(Symbol_table* symtab, const Layout* layout,
174 // We ignore the section of the result because outside of an
175 // output section definition the dot symbol is always considered
177 Output_section* dummy;
178 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_value,
182 // Update the dot symbol while setting section addresses.
184 set_section_addresses(Symbol_table* symtab, Layout* layout,
187 Output_section* dummy;
188 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_value,
192 // Print for debugging.
197 this->val_->print(f);
205 // An assertion in a SECTIONS clause outside of an output section.
207 class Sections_element_assertion : public Sections_element
210 Sections_element_assertion(Expression* check, const char* message,
212 : assertion_(check, message, messagelen)
215 // Check the assertion.
217 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
218 { this->assertion_.check(symtab, layout); }
220 // Print for debugging.
225 this->assertion_.print(f);
229 Script_assertion assertion_;
232 // An element in an output section in a SECTIONS clause.
234 class Output_section_element
237 // A list of input sections.
238 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
240 Output_section_element()
243 virtual ~Output_section_element()
246 // Add any symbol being defined to the symbol table.
248 add_symbols_to_table(Symbol_table*)
251 // Finalize symbols and check assertions.
253 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
256 // Return whether this element matches FILE_NAME and SECTION_NAME.
257 // The only real implementation is in Output_section_element_input.
259 match_name(const char*, const char*) const
262 // Set section addresses. This includes applying assignments if the
263 // the expression is an absolute value.
265 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
266 uint64_t*, Output_section**, std::string*,
270 // Print the element for debugging purposes.
272 print(FILE* f) const = 0;
275 // Return a fill string that is LENGTH bytes long, filling it with
278 get_fill_string(const std::string* fill, section_size_type length) const;
282 Output_section_element::get_fill_string(const std::string* fill,
283 section_size_type length) const
285 std::string this_fill;
286 this_fill.reserve(length);
287 while (this_fill.length() + fill->length() <= length)
289 if (this_fill.length() < length)
290 this_fill.append(*fill, 0, length - this_fill.length());
294 // A symbol assignment in an output section.
296 class Output_section_element_assignment : public Output_section_element
299 Output_section_element_assignment(const char* name, size_t namelen,
300 Expression* val, bool provide,
302 : assignment_(name, namelen, val, provide, hidden)
305 // Add the symbol to the symbol table.
307 add_symbols_to_table(Symbol_table* symtab)
308 { this->assignment_.add_to_table(symtab); }
310 // Finalize the symbol.
312 finalize_symbols(Symbol_table* symtab, const Layout* layout,
313 uint64_t* dot_value, Output_section** dot_section)
315 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
319 // Set the section address. There is no section here, but if the
320 // value is absolute, we set the symbol. This permits us to use
321 // absolute symbols when setting dot.
323 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
324 uint64_t, uint64_t* dot_value, Output_section**,
325 std::string*, Input_section_list*)
327 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
330 // Print for debugging.
335 this->assignment_.print(f);
339 Symbol_assignment assignment_;
342 // An assignment to the dot symbol in an output section.
344 class Output_section_element_dot_assignment : public Output_section_element
347 Output_section_element_dot_assignment(Expression* val)
351 // Finalize the symbol.
353 finalize_symbols(Symbol_table* symtab, const Layout* layout,
354 uint64_t* dot_value, Output_section** dot_section)
356 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_value,
357 *dot_section, dot_section);
360 // Update the dot symbol while setting section addresses.
362 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
363 uint64_t, uint64_t* dot_value, Output_section**,
364 std::string*, Input_section_list*);
366 // Print for debugging.
371 this->val_->print(f);
379 // Update the dot symbol while setting section addresses.
382 Output_section_element_dot_assignment::set_section_addresses(
383 Symbol_table* symtab,
385 Output_section* output_section,
388 Output_section** dot_section,
392 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, *dot_value,
393 *dot_section, dot_section);
394 if (next_dot < *dot_value)
395 gold_error(_("dot may not move backward"));
396 if (next_dot > *dot_value && output_section != NULL)
398 section_size_type length = convert_to_section_size_type(next_dot
400 Output_section_data* posd;
402 posd = new Output_data_fixed_space(length, 0);
405 std::string this_fill = this->get_fill_string(fill, length);
406 posd = new Output_data_const(this_fill, 0);
408 output_section->add_output_section_data(posd);
410 *dot_value = next_dot;
413 // An assertion in an output section.
415 class Output_section_element_assertion : public Output_section_element
418 Output_section_element_assertion(Expression* check, const char* message,
420 : assertion_(check, message, messagelen)
427 this->assertion_.print(f);
431 Script_assertion assertion_;
434 // We use a special instance of Output_section_data to handle BYTE,
435 // SHORT, etc. This permits forward references to symbols in the
438 class Output_data_expression : public Output_section_data
441 Output_data_expression(int size, bool is_signed, Expression* val,
442 const Symbol_table* symtab, const Layout* layout,
443 uint64_t dot_value, Output_section* dot_section)
444 : Output_section_data(size, 0),
445 is_signed_(is_signed), val_(val), symtab_(symtab),
446 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
450 // Write the data to the output file.
452 do_write(Output_file*);
454 // Write the data to a buffer.
456 do_write_to_buffer(unsigned char*);
459 template<bool big_endian>
461 endian_write_to_buffer(uint64_t, unsigned char*);
465 const Symbol_table* symtab_;
466 const Layout* layout_;
468 Output_section* dot_section_;
471 // Write the data element to the output file.
474 Output_data_expression::do_write(Output_file* of)
476 unsigned char* view = of->get_output_view(this->offset(), this->data_size());
477 this->write_to_buffer(view);
478 of->write_output_view(this->offset(), this->data_size(), view);
481 // Write the data element to a buffer.
484 Output_data_expression::do_write_to_buffer(unsigned char* buf)
486 Output_section* dummy;
487 uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
489 this->dot_section_, &dummy);
491 if (parameters->is_big_endian())
492 this->endian_write_to_buffer<true>(val, buf);
494 this->endian_write_to_buffer<false>(val, buf);
497 template<bool big_endian>
499 Output_data_expression::endian_write_to_buffer(uint64_t val,
502 switch (this->data_size())
505 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
508 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
511 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
514 if (parameters->get_size() == 32)
517 if (this->is_signed_ && (val & 0x80000000) != 0)
518 val |= 0xffffffff00000000LL;
520 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
527 // A data item in an output section.
529 class Output_section_element_data : public Output_section_element
532 Output_section_element_data(int size, bool is_signed, Expression* val)
533 : size_(size), is_signed_(is_signed), val_(val)
536 // Finalize symbols--we just need to update dot.
538 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
540 { *dot_value += this->size_; }
542 // Store the value in the section.
544 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
545 uint64_t* dot_value, Output_section**, std::string*,
546 Input_section_list*);
548 // Print for debugging.
553 // The size in bytes.
555 // Whether the value is signed.
561 // Store the value in the section.
564 Output_section_element_data::set_section_addresses(
565 Symbol_table* symtab,
570 Output_section** dot_section,
574 gold_assert(os != NULL);
575 os->add_output_section_data(new Output_data_expression(this->size_,
582 *dot_value += this->size_;
585 // Print for debugging.
588 Output_section_element_data::print(FILE* f) const
603 if (this->is_signed_)
611 fprintf(f, " %s(", s);
612 this->val_->print(f);
616 // A fill value setting in an output section.
618 class Output_section_element_fill : public Output_section_element
621 Output_section_element_fill(Expression* val)
625 // Update the fill value while setting section addresses.
627 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
628 uint64_t, uint64_t* dot_value,
629 Output_section** dot_section,
630 std::string* fill, Input_section_list*)
632 Output_section* fill_section;
633 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout,
634 *dot_value, *dot_section,
636 if (fill_section != NULL)
637 gold_warning(_("fill value is not absolute"));
638 // FIXME: The GNU linker supports fill values of arbitrary length.
639 unsigned char fill_buff[4];
640 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
641 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
644 // Print for debugging.
648 fprintf(f, " FILL(");
649 this->val_->print(f);
654 // The new fill value.
658 // Return whether STRING contains a wildcard character. This is used
659 // to speed up matching.
662 is_wildcard_string(const std::string& s)
664 return strpbrk(s.c_str(), "?*[") != NULL;
667 // An input section specification in an output section
669 class Output_section_element_input : public Output_section_element
672 Output_section_element_input(const Input_section_spec* spec, bool keep);
674 // Finalize symbols--just update the value of the dot symbol.
676 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
677 Output_section** dot_section)
679 *dot_value = this->final_dot_value_;
680 *dot_section = this->final_dot_section_;
683 // See whether we match FILE_NAME and SECTION_NAME as an input
686 match_name(const char* file_name, const char* section_name) const;
688 // Set the section address.
690 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
691 uint64_t subalign, uint64_t* dot_value,
692 Output_section**, std::string* fill,
693 Input_section_list*);
695 // Print for debugging.
697 print(FILE* f) const;
700 // An input section pattern.
701 struct Input_section_pattern
704 bool pattern_is_wildcard;
707 Input_section_pattern(const char* patterna, size_t patternlena,
709 : pattern(patterna, patternlena),
710 pattern_is_wildcard(is_wildcard_string(this->pattern)),
715 typedef std::vector<Input_section_pattern> Input_section_patterns;
717 // Filename_exclusions is a pair of filename pattern and a bool
718 // indicating whether the filename is a wildcard.
719 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
721 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
722 // indicates whether this is a wildcard pattern.
724 match(const char* string, const char* pattern, bool is_wildcard_pattern)
726 return (is_wildcard_pattern
727 ? fnmatch(pattern, string, 0) == 0
728 : strcmp(string, pattern) == 0);
731 // See if we match a file name.
733 match_file_name(const char* file_name) const;
735 // The file name pattern. If this is the empty string, we match all
737 std::string filename_pattern_;
738 // Whether the file name pattern is a wildcard.
739 bool filename_is_wildcard_;
740 // How the file names should be sorted. This may only be
741 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
742 Sort_wildcard filename_sort_;
743 // The list of file names to exclude.
744 Filename_exclusions filename_exclusions_;
745 // The list of input section patterns.
746 Input_section_patterns input_section_patterns_;
747 // Whether to keep this section when garbage collecting.
749 // The value of dot after including all matching sections.
750 uint64_t final_dot_value_;
751 // The section where dot is defined after including all matching
753 Output_section* final_dot_section_;
756 // Construct Output_section_element_input. The parser records strings
757 // as pointers into a copy of the script file, which will go away when
758 // parsing is complete. We make sure they are in std::string objects.
760 Output_section_element_input::Output_section_element_input(
761 const Input_section_spec* spec,
763 : filename_pattern_(),
764 filename_is_wildcard_(false),
765 filename_sort_(spec->file.sort),
766 filename_exclusions_(),
767 input_section_patterns_(),
770 final_dot_section_(NULL)
772 // The filename pattern "*" is common, and matches all files. Turn
773 // it into the empty string.
774 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
775 this->filename_pattern_.assign(spec->file.name.value,
776 spec->file.name.length);
777 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_);
779 if (spec->input_sections.exclude != NULL)
781 for (String_list::const_iterator p =
782 spec->input_sections.exclude->begin();
783 p != spec->input_sections.exclude->end();
786 bool is_wildcard = is_wildcard_string(*p);
787 this->filename_exclusions_.push_back(std::make_pair(*p,
792 if (spec->input_sections.sections != NULL)
794 Input_section_patterns& isp(this->input_section_patterns_);
795 for (String_sort_list::const_iterator p =
796 spec->input_sections.sections->begin();
797 p != spec->input_sections.sections->end();
799 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
804 // See whether we match FILE_NAME.
807 Output_section_element_input::match_file_name(const char* file_name) const
809 if (!this->filename_pattern_.empty())
811 // If we were called with no filename, we refuse to match a
812 // pattern which requires a file name.
813 if (file_name == NULL)
816 if (!match(file_name, this->filename_pattern_.c_str(),
817 this->filename_is_wildcard_))
821 if (file_name != NULL)
823 // Now we have to see whether FILE_NAME matches one of the
824 // exclusion patterns, if any.
825 for (Filename_exclusions::const_iterator p =
826 this->filename_exclusions_.begin();
827 p != this->filename_exclusions_.end();
830 if (match(file_name, p->first.c_str(), p->second))
838 // See whether we match FILE_NAME and SECTION_NAME.
841 Output_section_element_input::match_name(const char* file_name,
842 const char* section_name) const
844 if (!this->match_file_name(file_name))
847 // If there are no section name patterns, then we match.
848 if (this->input_section_patterns_.empty())
851 // See whether we match the section name patterns.
852 for (Input_section_patterns::const_iterator p =
853 this->input_section_patterns_.begin();
854 p != this->input_section_patterns_.end();
857 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
861 // We didn't match any section names, so we didn't match.
865 // Information we use to sort the input sections.
867 struct Input_section_info
871 std::string section_name;
876 // A class to sort the input sections.
878 class Input_section_sorter
881 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
882 : filename_sort_(filename_sort), section_sort_(section_sort)
886 operator()(const Input_section_info&, const Input_section_info&) const;
889 Sort_wildcard filename_sort_;
890 Sort_wildcard section_sort_;
894 Input_section_sorter::operator()(const Input_section_info& isi1,
895 const Input_section_info& isi2) const
897 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
898 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
899 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
900 && isi1.addralign == isi2.addralign))
902 if (isi1.section_name != isi2.section_name)
903 return isi1.section_name < isi2.section_name;
905 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
906 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
907 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
909 if (isi1.addralign != isi2.addralign)
910 return isi1.addralign < isi2.addralign;
912 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
914 if (isi1.relobj->name() != isi2.relobj->name())
915 return isi1.relobj->name() < isi2.relobj->name();
918 // Otherwise we leave them in the same order.
922 // Set the section address. Look in INPUT_SECTIONS for sections which
923 // match this spec, sort them as specified, and add them to the output
927 Output_section_element_input::set_section_addresses(
930 Output_section* output_section,
933 Output_section** dot_section,
935 Input_section_list* input_sections)
937 // We build a list of sections which match each
938 // Input_section_pattern.
940 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
941 size_t input_pattern_count = this->input_section_patterns_.size();
942 if (input_pattern_count == 0)
943 input_pattern_count = 1;
944 Matching_sections matching_sections(input_pattern_count);
946 // Look through the list of sections for this output section. Add
947 // each one which matches to one of the elements of
948 // MATCHING_SECTIONS.
950 Input_section_list::iterator p = input_sections->begin();
951 while (p != input_sections->end())
953 // Calling section_name and section_addralign is not very
955 Input_section_info isi;
956 isi.relobj = p->first;
957 isi.shndx = p->second;
959 // Lock the object so that we can get information about the
960 // section. This is OK since we know we are single-threaded
963 const Task* task = reinterpret_cast<const Task*>(-1);
964 Task_lock_obj<Object> tl(task, p->first);
966 isi.section_name = p->first->section_name(p->second);
967 isi.size = p->first->section_size(p->second);
968 isi.addralign = p->first->section_addralign(p->second);
971 if (!this->match_file_name(isi.relobj->name().c_str()))
973 else if (this->input_section_patterns_.empty())
975 matching_sections[0].push_back(isi);
976 p = input_sections->erase(p);
981 for (i = 0; i < input_pattern_count; ++i)
983 const Input_section_pattern&
984 isp(this->input_section_patterns_[i]);
985 if (match(isi.section_name.c_str(), isp.pattern.c_str(),
986 isp.pattern_is_wildcard))
990 if (i >= this->input_section_patterns_.size())
994 matching_sections[i].push_back(isi);
995 p = input_sections->erase(p);
1000 // Look through MATCHING_SECTIONS. Sort each one as specified,
1001 // using a stable sort so that we get the default order when
1002 // sections are otherwise equal. Add each input section to the
1005 for (size_t i = 0; i < input_pattern_count; ++i)
1007 if (matching_sections[i].empty())
1010 gold_assert(output_section != NULL);
1012 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1013 if (isp.sort != SORT_WILDCARD_NONE
1014 || this->filename_sort_ != SORT_WILDCARD_NONE)
1015 std::stable_sort(matching_sections[i].begin(),
1016 matching_sections[i].end(),
1017 Input_section_sorter(this->filename_sort_,
1020 for (std::vector<Input_section_info>::const_iterator p =
1021 matching_sections[i].begin();
1022 p != matching_sections[i].end();
1025 uint64_t this_subalign = p->addralign;
1026 if (this_subalign < subalign)
1027 this_subalign = subalign;
1029 uint64_t address = align_address(*dot_value, this_subalign);
1031 if (address > *dot_value && !fill->empty())
1033 section_size_type length =
1034 convert_to_section_size_type(address - *dot_value);
1035 std::string this_fill = this->get_fill_string(fill, length);
1036 Output_section_data* posd = new Output_data_const(this_fill, 0);
1037 output_section->add_output_section_data(posd);
1040 output_section->add_input_section_for_script(p->relobj,
1045 *dot_value = address + p->size;
1049 this->final_dot_value_ = *dot_value;
1050 this->final_dot_section_ = *dot_section;
1053 // Print for debugging.
1056 Output_section_element_input::print(FILE* f) const
1061 fprintf(f, "KEEP(");
1063 if (!this->filename_pattern_.empty())
1065 bool need_close_paren = false;
1066 switch (this->filename_sort_)
1068 case SORT_WILDCARD_NONE:
1070 case SORT_WILDCARD_BY_NAME:
1071 fprintf(f, "SORT_BY_NAME(");
1072 need_close_paren = true;
1078 fprintf(f, "%s", this->filename_pattern_.c_str());
1080 if (need_close_paren)
1084 if (!this->input_section_patterns_.empty()
1085 || !this->filename_exclusions_.empty())
1089 bool need_space = false;
1090 if (!this->filename_exclusions_.empty())
1092 fprintf(f, "EXCLUDE_FILE(");
1093 bool need_comma = false;
1094 for (Filename_exclusions::const_iterator p =
1095 this->filename_exclusions_.begin();
1096 p != this->filename_exclusions_.end();
1101 fprintf(f, "%s", p->first.c_str());
1108 for (Input_section_patterns::const_iterator p =
1109 this->input_section_patterns_.begin();
1110 p != this->input_section_patterns_.end();
1116 int close_parens = 0;
1119 case SORT_WILDCARD_NONE:
1121 case SORT_WILDCARD_BY_NAME:
1122 fprintf(f, "SORT_BY_NAME(");
1125 case SORT_WILDCARD_BY_ALIGNMENT:
1126 fprintf(f, "SORT_BY_ALIGNMENT(");
1129 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1130 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1133 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1134 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1141 fprintf(f, "%s", p->pattern.c_str());
1143 for (int i = 0; i < close_parens; ++i)
1158 // An output section.
1160 class Output_section_definition : public Sections_element
1163 typedef Output_section_element::Input_section_list Input_section_list;
1165 Output_section_definition(const char* name, size_t namelen,
1166 const Parser_output_section_header* header);
1168 // Finish the output section with the information in the trailer.
1170 finish(const Parser_output_section_trailer* trailer);
1172 // Add a symbol to be defined.
1174 add_symbol_assignment(const char* name, size_t length, Expression* value,
1175 bool provide, bool hidden);
1177 // Add an assignment to the special dot symbol.
1179 add_dot_assignment(Expression* value);
1181 // Add an assertion.
1183 add_assertion(Expression* check, const char* message, size_t messagelen);
1185 // Add a data item to the current output section.
1187 add_data(int size, bool is_signed, Expression* val);
1189 // Add a setting for the fill value.
1191 add_fill(Expression* val);
1193 // Add an input section specification.
1195 add_input_section(const Input_section_spec* spec, bool keep);
1197 // Add any symbols being defined to the symbol table.
1199 add_symbols_to_table(Symbol_table* symtab);
1201 // Finalize symbols and check assertions.
1203 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
1205 // Return the output section name to use for an input file name and
1208 output_section_name(const char* file_name, const char* section_name,
1211 // Return whether to place an orphan section after this one.
1213 place_orphan_here(const Output_section *os, bool* exact) const;
1215 // Set the section address.
1217 set_section_addresses(Symbol_table* symtab, Layout* layout,
1218 uint64_t* dot_value);
1220 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1221 // this section is constrained, and the input sections do not match,
1222 // return the constraint, and set *POSD.
1224 check_constraint(Output_section_definition** posd);
1226 // See if this is the alternate output section for a constrained
1227 // output section. If it is, transfer the Output_section and return
1228 // true. Otherwise return false.
1230 alternate_constraint(Output_section_definition*, Section_constraint);
1232 // Get the list of segments to use for an allocated section when
1233 // using a PHDRS clause. If this is an allocated section, return
1234 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
1235 // which it should be attached. If the PHDRS were not specified,
1236 // don't change *PHDRS_LIST.
1238 allocate_to_segment(String_list** phdrs_list);
1240 // Print the contents to the FILE. This is for debugging.
1245 typedef std::vector<Output_section_element*> Output_section_elements;
1247 // The output section name.
1249 // The address. This may be NULL.
1250 Expression* address_;
1251 // The load address. This may be NULL.
1252 Expression* load_address_;
1253 // The alignment. This may be NULL.
1255 // The input section alignment. This may be NULL.
1256 Expression* subalign_;
1257 // The constraint, if any.
1258 Section_constraint constraint_;
1259 // The fill value. This may be NULL.
1261 // The list of segments this section should go into. This may be
1263 String_list* phdrs_;
1264 // The list of elements defining the section.
1265 Output_section_elements elements_;
1266 // The Output_section created for this definition. This will be
1267 // NULL if none was created.
1268 Output_section* output_section_;
1273 Output_section_definition::Output_section_definition(
1276 const Parser_output_section_header* header)
1277 : name_(name, namelen),
1278 address_(header->address),
1279 load_address_(header->load_address),
1280 align_(header->align),
1281 subalign_(header->subalign),
1282 constraint_(header->constraint),
1286 output_section_(NULL)
1290 // Finish an output section.
1293 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1295 this->fill_ = trailer->fill;
1296 this->phdrs_ = trailer->phdrs;
1299 // Add a symbol to be defined.
1302 Output_section_definition::add_symbol_assignment(const char* name,
1308 Output_section_element* p = new Output_section_element_assignment(name,
1313 this->elements_.push_back(p);
1316 // Add an assignment to the special dot symbol.
1319 Output_section_definition::add_dot_assignment(Expression* value)
1321 Output_section_element* p = new Output_section_element_dot_assignment(value);
1322 this->elements_.push_back(p);
1325 // Add an assertion.
1328 Output_section_definition::add_assertion(Expression* check,
1329 const char* message,
1332 Output_section_element* p = new Output_section_element_assertion(check,
1335 this->elements_.push_back(p);
1338 // Add a data item to the current output section.
1341 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1343 Output_section_element* p = new Output_section_element_data(size, is_signed,
1345 this->elements_.push_back(p);
1348 // Add a setting for the fill value.
1351 Output_section_definition::add_fill(Expression* val)
1353 Output_section_element* p = new Output_section_element_fill(val);
1354 this->elements_.push_back(p);
1357 // Add an input section specification.
1360 Output_section_definition::add_input_section(const Input_section_spec* spec,
1363 Output_section_element* p = new Output_section_element_input(spec, keep);
1364 this->elements_.push_back(p);
1367 // Add any symbols being defined to the symbol table.
1370 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
1372 for (Output_section_elements::iterator p = this->elements_.begin();
1373 p != this->elements_.end();
1375 (*p)->add_symbols_to_table(symtab);
1378 // Finalize symbols and check assertions.
1381 Output_section_definition::finalize_symbols(Symbol_table* symtab,
1382 const Layout* layout,
1383 uint64_t* dot_value)
1385 if (this->output_section_ != NULL)
1386 *dot_value = this->output_section_->address();
1389 uint64_t address = *dot_value;
1390 if (this->address_ != NULL)
1392 Output_section* dummy;
1393 address = this->address_->eval_with_dot(symtab, layout,
1397 if (this->align_ != NULL)
1399 Output_section* dummy;
1400 uint64_t align = this->align_->eval_with_dot(symtab, layout,
1404 address = align_address(address, align);
1406 *dot_value = address;
1409 Output_section* dot_section = this->output_section_;
1410 for (Output_section_elements::iterator p = this->elements_.begin();
1411 p != this->elements_.end();
1413 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
1416 // Return the output section name to use for an input section name.
1419 Output_section_definition::output_section_name(const char* file_name,
1420 const char* section_name,
1421 Output_section*** slot)
1423 // Ask each element whether it matches NAME.
1424 for (Output_section_elements::const_iterator p = this->elements_.begin();
1425 p != this->elements_.end();
1428 if ((*p)->match_name(file_name, section_name))
1430 // We found a match for NAME, which means that it should go
1431 // into this output section.
1432 *slot = &this->output_section_;
1433 return this->name_.c_str();
1437 // We don't know about this section name.
1441 // Return whether to place an orphan output section after this
1445 Output_section_definition::place_orphan_here(const Output_section *os,
1448 // Check for the simple case first.
1449 if (this->output_section_ != NULL
1450 && this->output_section_->type() == os->type()
1451 && this->output_section_->flags() == os->flags())
1457 // Otherwise use some heuristics.
1459 if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
1462 if (os->type() == elfcpp::SHT_NOBITS)
1464 if (this->name_ == ".bss")
1469 if (this->output_section_ != NULL
1470 && this->output_section_->type() == elfcpp::SHT_NOBITS)
1473 else if (os->type() == elfcpp::SHT_NOTE)
1475 if (this->output_section_ != NULL
1476 && this->output_section_->type() == elfcpp::SHT_NOTE)
1481 if (this->name_.compare(0, 5, ".note") == 0)
1486 if (this->name_ == ".interp")
1488 if (this->output_section_ != NULL
1489 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1490 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1493 else if (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
1495 if (this->name_.compare(0, 4, ".rel") == 0)
1500 if (this->output_section_ != NULL
1501 && (this->output_section_->type() == elfcpp::SHT_REL
1502 || this->output_section_->type() == elfcpp::SHT_RELA))
1507 if (this->output_section_ != NULL
1508 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1509 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1512 else if (os->type() == elfcpp::SHT_PROGBITS
1513 && (os->flags() & elfcpp::SHF_WRITE) != 0)
1515 if (this->name_ == ".data")
1520 if (this->output_section_ != NULL
1521 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1522 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
1525 else if (os->type() == elfcpp::SHT_PROGBITS
1526 && (os->flags() & elfcpp::SHF_EXECINSTR) != 0)
1528 if (this->name_ == ".text")
1533 if (this->output_section_ != NULL
1534 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1535 && (this->output_section_->flags() & elfcpp::SHF_EXECINSTR) != 0)
1538 else if (os->type() == elfcpp::SHT_PROGBITS
1539 || (os->type() != elfcpp::SHT_PROGBITS
1540 && (os->flags() & elfcpp::SHF_WRITE) == 0))
1542 if (this->name_ == ".rodata")
1547 if (this->output_section_ != NULL
1548 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1549 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1556 // Set the section address. Note that the OUTPUT_SECTION_ field will
1557 // be NULL if no input sections were mapped to this output section.
1558 // We still have to adjust dot and process symbol assignments.
1561 Output_section_definition::set_section_addresses(Symbol_table* symtab,
1563 uint64_t* dot_value)
1566 if (this->address_ == NULL)
1567 address = *dot_value;
1570 Output_section* dummy;
1571 address = this->address_->eval_with_dot(symtab, layout, *dot_value,
1576 if (this->align_ == NULL)
1578 if (this->output_section_ == NULL)
1581 align = this->output_section_->addralign();
1585 Output_section* align_section;
1586 align = this->align_->eval_with_dot(symtab, layout, *dot_value,
1587 NULL, &align_section);
1588 if (align_section != NULL)
1589 gold_warning(_("alignment of section %s is not absolute"),
1590 this->name_.c_str());
1591 if (this->output_section_ != NULL)
1592 this->output_section_->set_addralign(align);
1595 address = align_address(address, align);
1597 *dot_value = address;
1599 // The address of non-SHF_ALLOC sections is forced to zero,
1600 // regardless of what the linker script wants.
1601 if (this->output_section_ != NULL
1602 && (this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0)
1603 this->output_section_->set_address(address);
1605 if (this->load_address_ != NULL && this->output_section_ != NULL)
1607 Output_section* dummy;
1608 uint64_t load_address =
1609 this->load_address_->eval_with_dot(symtab, layout, *dot_value,
1610 this->output_section_, &dummy);
1611 this->output_section_->set_load_address(load_address);
1615 if (this->subalign_ == NULL)
1619 Output_section* subalign_section;
1620 subalign = this->subalign_->eval_with_dot(symtab, layout, *dot_value,
1621 NULL, &subalign_section);
1622 if (subalign_section != NULL)
1623 gold_warning(_("subalign of section %s is not absolute"),
1624 this->name_.c_str());
1628 if (this->fill_ != NULL)
1630 // FIXME: The GNU linker supports fill values of arbitrary
1632 Output_section* fill_section;
1633 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout,
1637 if (fill_section != NULL)
1638 gold_warning(_("fill of section %s is not absolute"),
1639 this->name_.c_str());
1640 unsigned char fill_buff[4];
1641 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1642 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
1645 Input_section_list input_sections;
1646 if (this->output_section_ != NULL)
1648 // Get the list of input sections attached to this output
1649 // section. This will leave the output section with only
1650 // Output_section_data entries.
1651 address += this->output_section_->get_input_sections(address,
1654 *dot_value = address;
1657 Output_section* dot_section = this->output_section_;
1658 for (Output_section_elements::iterator p = this->elements_.begin();
1659 p != this->elements_.end();
1661 (*p)->set_section_addresses(symtab, layout, this->output_section_,
1662 subalign, dot_value, &dot_section, &fill,
1665 gold_assert(input_sections.empty());
1668 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1669 // this section is constrained, and the input sections do not match,
1670 // return the constraint, and set *POSD.
1673 Output_section_definition::check_constraint(Output_section_definition** posd)
1675 switch (this->constraint_)
1677 case CONSTRAINT_NONE:
1678 return CONSTRAINT_NONE;
1680 case CONSTRAINT_ONLY_IF_RO:
1681 if (this->output_section_ != NULL
1682 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
1685 return CONSTRAINT_ONLY_IF_RO;
1687 return CONSTRAINT_NONE;
1689 case CONSTRAINT_ONLY_IF_RW:
1690 if (this->output_section_ != NULL
1691 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1694 return CONSTRAINT_ONLY_IF_RW;
1696 return CONSTRAINT_NONE;
1698 case CONSTRAINT_SPECIAL:
1699 if (this->output_section_ != NULL)
1700 gold_error(_("SPECIAL constraints are not implemented"));
1701 return CONSTRAINT_NONE;
1708 // See if this is the alternate output section for a constrained
1709 // output section. If it is, transfer the Output_section and return
1710 // true. Otherwise return false.
1713 Output_section_definition::alternate_constraint(
1714 Output_section_definition* posd,
1715 Section_constraint constraint)
1717 if (this->name_ != posd->name_)
1722 case CONSTRAINT_ONLY_IF_RO:
1723 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
1727 case CONSTRAINT_ONLY_IF_RW:
1728 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
1736 // We have found the alternate constraint. We just need to move
1737 // over the Output_section. When constraints are used properly,
1738 // THIS should not have an output_section pointer, as all the input
1739 // sections should have matched the other definition.
1741 if (this->output_section_ != NULL)
1742 gold_error(_("mismatched definition for constrained sections"));
1744 this->output_section_ = posd->output_section_;
1745 posd->output_section_ = NULL;
1750 // Get the list of segments to use for an allocated section when using
1751 // a PHDRS clause. If this is an allocated section, return the
1752 // Output_section, and set *PHDRS_LIST to the list of PHDRS to which
1753 // it should be attached. If the PHDRS were not specified, don't
1754 // change *PHDRS_LIST.
1757 Output_section_definition::allocate_to_segment(String_list** phdrs_list)
1759 if (this->output_section_ == NULL)
1761 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
1763 if (this->phdrs_ != NULL)
1764 *phdrs_list = this->phdrs_;
1765 return this->output_section_;
1768 // Print for debugging.
1771 Output_section_definition::print(FILE* f) const
1773 fprintf(f, " %s ", this->name_.c_str());
1775 if (this->address_ != NULL)
1777 this->address_->print(f);
1783 if (this->load_address_ != NULL)
1786 this->load_address_->print(f);
1790 if (this->align_ != NULL)
1792 fprintf(f, "ALIGN(");
1793 this->align_->print(f);
1797 if (this->subalign_ != NULL)
1799 fprintf(f, "SUBALIGN(");
1800 this->subalign_->print(f);
1806 for (Output_section_elements::const_iterator p = this->elements_.begin();
1807 p != this->elements_.end();
1813 if (this->fill_ != NULL)
1816 this->fill_->print(f);
1819 if (this->phdrs_ != NULL)
1821 for (String_list::const_iterator p = this->phdrs_->begin();
1822 p != this->phdrs_->end();
1824 fprintf(f, " :%s", p->c_str());
1830 // An output section created to hold orphaned input sections. These
1831 // do not actually appear in linker scripts. However, for convenience
1832 // when setting the output section addresses, we put a marker to these
1833 // sections in the appropriate place in the list of SECTIONS elements.
1835 class Orphan_output_section : public Sections_element
1838 Orphan_output_section(Output_section* os)
1842 // Return whether to place an orphan section after this one.
1844 place_orphan_here(const Output_section *os, bool* exact) const;
1846 // Set section addresses.
1848 set_section_addresses(Symbol_table*, Layout*, uint64_t*);
1850 // Get the list of segments to use for an allocated section when
1851 // using a PHDRS clause. If this is an allocated section, return
1852 // the Output_section.
1854 allocate_to_segment(String_list**);
1856 // Print for debugging.
1858 print(FILE* f) const
1860 fprintf(f, " marker for orphaned output section %s\n",
1865 Output_section* os_;
1868 // Whether to place another orphan section after this one.
1871 Orphan_output_section::place_orphan_here(const Output_section* os,
1874 if (this->os_->type() == os->type()
1875 && this->os_->flags() == os->flags())
1883 // Set section addresses.
1886 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
1887 uint64_t* dot_value)
1889 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
1891 uint64_t address = *dot_value;
1892 address = align_address(address, this->os_->addralign());
1894 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
1895 this->os_->set_address(address);
1897 Input_section_list input_sections;
1898 address += this->os_->get_input_sections(address, "", &input_sections);
1900 for (Input_section_list::iterator p = input_sections.begin();
1901 p != input_sections.end();
1907 // We know what are single-threaded, so it is OK to lock the
1910 const Task* task = reinterpret_cast<const Task*>(-1);
1911 Task_lock_obj<Object> tl(task, p->first);
1912 addralign = p->first->section_addralign(p->second);
1913 size = p->first->section_size(p->second);
1916 address = align_address(address, addralign);
1917 this->os_->add_input_section_for_script(p->first, p->second, size, 0);
1921 *dot_value = address;
1924 // Get the list of segments to use for an allocated section when using
1925 // a PHDRS clause. If this is an allocated section, return the
1926 // Output_section. We don't change the list of segments.
1929 Orphan_output_section::allocate_to_segment(String_list**)
1931 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
1936 // Class Phdrs_element. A program header from a PHDRS clause.
1941 Phdrs_element(const char* name, size_t namelen, unsigned int type,
1942 bool includes_filehdr, bool includes_phdrs,
1943 bool is_flags_valid, unsigned int flags,
1944 Expression* load_address)
1945 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
1946 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
1947 flags_(flags), load_address_(load_address), load_address_value_(0),
1951 // Return the name of this segment.
1954 { return this->name_; }
1956 // Return the type of the segment.
1959 { return this->type_; }
1961 // Whether to include the file header.
1963 includes_filehdr() const
1964 { return this->includes_filehdr_; }
1966 // Whether to include the program headers.
1968 includes_phdrs() const
1969 { return this->includes_phdrs_; }
1971 // Return whether there is a load address.
1973 has_load_address() const
1974 { return this->load_address_ != NULL; }
1976 // Evaluate the load address expression if there is one.
1978 eval_load_address(Symbol_table* symtab, Layout* layout)
1980 if (this->load_address_ != NULL)
1981 this->load_address_value_ = this->load_address_->eval(symtab, layout);
1984 // Return the load address.
1986 load_address() const
1988 gold_assert(this->load_address_ != NULL);
1989 return this->load_address_value_;
1992 // Create the segment.
1994 create_segment(Layout* layout)
1996 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
1997 return this->segment_;
2000 // Return the segment.
2003 { return this->segment_; }
2005 // Set the segment flags if appropriate.
2007 set_flags_if_valid()
2009 if (this->is_flags_valid_)
2010 this->segment_->set_flags(this->flags_);
2013 // Print for debugging.
2018 // The name used in the script.
2020 // The type of the segment (PT_LOAD, etc.).
2022 // Whether this segment includes the file header.
2023 bool includes_filehdr_;
2024 // Whether this segment includes the section headers.
2025 bool includes_phdrs_;
2026 // Whether the flags were explicitly specified.
2027 bool is_flags_valid_;
2028 // The flags for this segment (PF_R, etc.) if specified.
2029 unsigned int flags_;
2030 // The expression for the load address for this segment. This may
2032 Expression* load_address_;
2033 // The actual load address from evaluating the expression.
2034 uint64_t load_address_value_;
2035 // The segment itself.
2036 Output_segment* segment_;
2039 // Print for debugging.
2042 Phdrs_element::print(FILE* f) const
2044 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
2045 if (this->includes_filehdr_)
2046 fprintf(f, " FILEHDR");
2047 if (this->includes_phdrs_)
2048 fprintf(f, " PHDRS");
2049 if (this->is_flags_valid_)
2050 fprintf(f, " FLAGS(%u)", this->flags_);
2051 if (this->load_address_ != NULL)
2054 this->load_address_->print(f);
2060 // Class Script_sections.
2062 Script_sections::Script_sections()
2063 : saw_sections_clause_(false),
2064 in_sections_clause_(false),
2065 sections_elements_(NULL),
2066 output_section_(NULL),
2067 phdrs_elements_(NULL)
2071 // Start a SECTIONS clause.
2074 Script_sections::start_sections()
2076 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2077 this->saw_sections_clause_ = true;
2078 this->in_sections_clause_ = true;
2079 if (this->sections_elements_ == NULL)
2080 this->sections_elements_ = new Sections_elements;
2083 // Finish a SECTIONS clause.
2086 Script_sections::finish_sections()
2088 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2089 this->in_sections_clause_ = false;
2092 // Add a symbol to be defined.
2095 Script_sections::add_symbol_assignment(const char* name, size_t length,
2096 Expression* val, bool provide,
2099 if (this->output_section_ != NULL)
2100 this->output_section_->add_symbol_assignment(name, length, val,
2104 Sections_element* p = new Sections_element_assignment(name, length,
2107 this->sections_elements_->push_back(p);
2111 // Add an assignment to the special dot symbol.
2114 Script_sections::add_dot_assignment(Expression* val)
2116 if (this->output_section_ != NULL)
2117 this->output_section_->add_dot_assignment(val);
2120 Sections_element* p = new Sections_element_dot_assignment(val);
2121 this->sections_elements_->push_back(p);
2125 // Add an assertion.
2128 Script_sections::add_assertion(Expression* check, const char* message,
2131 if (this->output_section_ != NULL)
2132 this->output_section_->add_assertion(check, message, messagelen);
2135 Sections_element* p = new Sections_element_assertion(check, message,
2137 this->sections_elements_->push_back(p);
2141 // Start processing entries for an output section.
2144 Script_sections::start_output_section(
2147 const Parser_output_section_header *header)
2149 Output_section_definition* posd = new Output_section_definition(name,
2152 this->sections_elements_->push_back(posd);
2153 gold_assert(this->output_section_ == NULL);
2154 this->output_section_ = posd;
2157 // Stop processing entries for an output section.
2160 Script_sections::finish_output_section(
2161 const Parser_output_section_trailer* trailer)
2163 gold_assert(this->output_section_ != NULL);
2164 this->output_section_->finish(trailer);
2165 this->output_section_ = NULL;
2168 // Add a data item to the current output section.
2171 Script_sections::add_data(int size, bool is_signed, Expression* val)
2173 gold_assert(this->output_section_ != NULL);
2174 this->output_section_->add_data(size, is_signed, val);
2177 // Add a fill value setting to the current output section.
2180 Script_sections::add_fill(Expression* val)
2182 gold_assert(this->output_section_ != NULL);
2183 this->output_section_->add_fill(val);
2186 // Add an input section specification to the current output section.
2189 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2191 gold_assert(this->output_section_ != NULL);
2192 this->output_section_->add_input_section(spec, keep);
2195 // Add any symbols we are defining to the symbol table.
2198 Script_sections::add_symbols_to_table(Symbol_table* symtab)
2200 if (!this->saw_sections_clause_)
2202 for (Sections_elements::iterator p = this->sections_elements_->begin();
2203 p != this->sections_elements_->end();
2205 (*p)->add_symbols_to_table(symtab);
2208 // Finalize symbols and check assertions.
2211 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2213 if (!this->saw_sections_clause_)
2215 uint64_t dot_value = 0;
2216 for (Sections_elements::iterator p = this->sections_elements_->begin();
2217 p != this->sections_elements_->end();
2219 (*p)->finalize_symbols(symtab, layout, &dot_value);
2222 // Return the name of the output section to use for an input file name
2223 // and section name.
2226 Script_sections::output_section_name(const char* file_name,
2227 const char* section_name,
2228 Output_section*** output_section_slot)
2230 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2231 p != this->sections_elements_->end();
2234 const char* ret = (*p)->output_section_name(file_name, section_name,
2235 output_section_slot);
2239 // The special name /DISCARD/ means that the input section
2240 // should be discarded.
2241 if (strcmp(ret, "/DISCARD/") == 0)
2243 *output_section_slot = NULL;
2250 // If we couldn't find a mapping for the name, the output section
2251 // gets the name of the input section.
2253 *output_section_slot = NULL;
2255 return section_name;
2258 // Place a marker for an orphan output section into the SECTIONS
2262 Script_sections::place_orphan(Output_section* os)
2264 // Look for an output section definition which matches the output
2265 // section. Put a marker after that section.
2266 Sections_elements::iterator place = this->sections_elements_->end();
2267 for (Sections_elements::iterator p = this->sections_elements_->begin();
2268 p != this->sections_elements_->end();
2272 if ((*p)->place_orphan_here(os, &exact))
2280 // The insert function puts the new element before the iterator.
2281 if (place != this->sections_elements_->end())
2284 this->sections_elements_->insert(place, new Orphan_output_section(os));
2287 // Set the addresses of all the output sections. Walk through all the
2288 // elements, tracking the dot symbol. Apply assignments which set
2289 // absolute symbol values, in case they are used when setting dot.
2290 // Fill in data statement values. As we find output sections, set the
2291 // address, set the address of all associated input sections, and
2292 // update dot. Return the segment which should hold the file header
2293 // and segment headers, if any.
2296 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2298 gold_assert(this->saw_sections_clause_);
2300 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2301 // for our representation.
2302 for (Sections_elements::iterator p = this->sections_elements_->begin();
2303 p != this->sections_elements_->end();
2306 Output_section_definition* posd;
2307 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2308 if (failed_constraint != CONSTRAINT_NONE)
2310 Sections_elements::iterator q;
2311 for (q = this->sections_elements_->begin();
2312 q != this->sections_elements_->end();
2317 if ((*q)->alternate_constraint(posd, failed_constraint))
2322 if (q == this->sections_elements_->end())
2323 gold_error(_("no matching section constraint"));
2327 // For a relocatable link, we implicitly set dot to zero.
2328 uint64_t dot_value = 0;
2329 for (Sections_elements::iterator p = this->sections_elements_->begin();
2330 p != this->sections_elements_->end();
2332 (*p)->set_section_addresses(symtab, layout, &dot_value);
2334 if (this->phdrs_elements_ != NULL)
2336 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2337 p != this->phdrs_elements_->end();
2339 (*p)->eval_load_address(symtab, layout);
2342 return this->create_segments(layout);
2345 // Sort the sections in order to put them into segments.
2347 class Sort_output_sections
2351 operator()(const Output_section* os1, const Output_section* os2) const;
2355 Sort_output_sections::operator()(const Output_section* os1,
2356 const Output_section* os2) const
2358 // Sort first by the load address.
2359 uint64_t lma1 = (os1->has_load_address()
2360 ? os1->load_address()
2362 uint64_t lma2 = (os2->has_load_address()
2363 ? os2->load_address()
2368 // Then sort by the virtual address.
2369 if (os1->address() != os2->address())
2370 return os1->address() < os2->address();
2372 // Sort TLS sections to the end.
2373 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
2374 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
2378 // Sort PROGBITS before NOBITS.
2379 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
2381 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
2384 // Otherwise we don't care.
2388 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2389 // We treat a section with the SHF_TLS flag set as taking up space
2390 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2391 // space for them in the file.
2394 Script_sections::is_bss_section(const Output_section* os)
2396 return (os->type() == elfcpp::SHT_NOBITS
2397 && (os->flags() & elfcpp::SHF_TLS) == 0);
2400 // Return the size taken by the file header and the program headers.
2403 Script_sections::total_header_size(Layout* layout) const
2405 size_t segment_count = layout->segment_count();
2406 size_t file_header_size;
2407 size_t segment_headers_size;
2408 if (parameters->get_size() == 32)
2410 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
2411 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
2413 else if (parameters->get_size() == 64)
2415 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
2416 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
2421 return file_header_size + segment_headers_size;
2424 // Return the amount we have to subtract from the LMA to accomodate
2425 // headers of the given size. The complication is that the file
2426 // header have to be at the start of a page, as otherwise it will not
2427 // be at the start of the file.
2430 Script_sections::header_size_adjustment(uint64_t lma,
2431 size_t sizeof_headers) const
2433 const uint64_t abi_pagesize = parameters->target()->abi_pagesize();
2434 uint64_t hdr_lma = lma - sizeof_headers;
2435 hdr_lma &= ~(abi_pagesize - 1);
2436 return lma - hdr_lma;
2439 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2440 // the segment which should hold the file header and segment headers,
2444 Script_sections::create_segments(Layout* layout)
2446 gold_assert(this->saw_sections_clause_);
2448 if (parameters->output_is_object())
2451 if (this->saw_phdrs_clause())
2452 return create_segments_from_phdrs_clause(layout);
2454 Layout::Section_list sections;
2455 layout->get_allocated_sections(§ions);
2457 // Sort the sections by address.
2458 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
2460 this->create_note_and_tls_segments(layout, §ions);
2462 // Walk through the sections adding them to PT_LOAD segments.
2463 const uint64_t abi_pagesize = parameters->target()->abi_pagesize();
2464 Output_segment* first_seg = NULL;
2465 Output_segment* current_seg = NULL;
2466 bool is_current_seg_readonly = true;
2467 Layout::Section_list::iterator plast = sections.end();
2468 uint64_t last_vma = 0;
2469 uint64_t last_lma = 0;
2470 uint64_t last_size = 0;
2471 for (Layout::Section_list::iterator p = sections.begin();
2472 p != sections.end();
2475 const uint64_t vma = (*p)->address();
2476 const uint64_t lma = ((*p)->has_load_address()
2477 ? (*p)->load_address()
2479 const uint64_t size = (*p)->current_data_size();
2481 bool need_new_segment;
2482 if (current_seg == NULL)
2483 need_new_segment = true;
2484 else if (lma - vma != last_lma - last_vma)
2486 // This section has a different LMA relationship than the
2487 // last one; we need a new segment.
2488 need_new_segment = true;
2490 else if (align_address(last_lma + last_size, abi_pagesize)
2491 < align_address(lma, abi_pagesize))
2493 // Putting this section in the segment would require
2495 need_new_segment = true;
2497 else if (is_bss_section(*plast) && !is_bss_section(*p))
2499 // A non-BSS section can not follow a BSS section in the
2501 need_new_segment = true;
2503 else if (is_current_seg_readonly
2504 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2506 // Don't put a writable section in the same segment as a
2507 // non-writable section.
2508 need_new_segment = true;
2512 // Otherwise, reuse the existing segment.
2513 need_new_segment = false;
2516 elfcpp::Elf_Word seg_flags =
2517 Layout::section_flags_to_segment((*p)->flags());
2519 if (need_new_segment)
2521 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2523 current_seg->set_addresses(vma, lma);
2524 if (first_seg == NULL)
2525 first_seg = current_seg;
2526 is_current_seg_readonly = true;
2529 current_seg->add_output_section(*p, seg_flags);
2531 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2532 is_current_seg_readonly = false;
2540 // An ELF program should work even if the program headers are not in
2541 // a PT_LOAD segment. However, it appears that the Linux kernel
2542 // does not set the AT_PHDR auxiliary entry in that case. It sets
2543 // the load address to p_vaddr - p_offset of the first PT_LOAD
2544 // segment. It then sets AT_PHDR to the load address plus the
2545 // offset to the program headers, e_phoff in the file header. This
2546 // fails when the program headers appear in the file before the
2547 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2548 // segment to hold the file header and the program headers. This is
2549 // effectively what the GNU linker does, and it is slightly more
2550 // efficient in any case. We try to use the first PT_LOAD segment
2551 // if we can, otherwise we make a new one.
2553 size_t sizeof_headers = this->total_header_size(layout);
2555 if (first_seg != NULL
2556 && (first_seg->paddr() & (abi_pagesize - 1)) >= sizeof_headers)
2558 first_seg->set_addresses(first_seg->vaddr() - sizeof_headers,
2559 first_seg->paddr() - sizeof_headers);
2563 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2565 if (first_seg == NULL)
2566 load_seg->set_addresses(0, 0);
2569 uint64_t vma = first_seg->vaddr();
2570 uint64_t lma = first_seg->paddr();
2572 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
2573 if (lma >= subtract && vma >= subtract)
2574 load_seg->set_addresses(vma - subtract, lma - subtract);
2577 // We could handle this case by create the file header
2578 // outside of any PT_LOAD segment, and creating a new
2579 // PT_LOAD segment after the others to hold the segment
2581 gold_error(_("sections loaded on first page without room for "
2582 "file and program headers are not supported"));
2589 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2590 // segment if there are any SHT_TLS sections.
2593 Script_sections::create_note_and_tls_segments(
2595 const Layout::Section_list* sections)
2597 gold_assert(!this->saw_phdrs_clause());
2599 bool saw_tls = false;
2600 for (Layout::Section_list::const_iterator p = sections->begin();
2601 p != sections->end();
2604 if ((*p)->type() == elfcpp::SHT_NOTE)
2606 elfcpp::Elf_Word seg_flags =
2607 Layout::section_flags_to_segment((*p)->flags());
2608 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
2610 oseg->add_output_section(*p, seg_flags);
2612 // Incorporate any subsequent SHT_NOTE sections, in the
2613 // hopes that the script is sensible.
2614 Layout::Section_list::const_iterator pnext = p + 1;
2615 while (pnext != sections->end()
2616 && (*pnext)->type() == elfcpp::SHT_NOTE)
2618 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2619 oseg->add_output_section(*pnext, seg_flags);
2625 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2628 gold_error(_("TLS sections are not adjacent"));
2630 elfcpp::Elf_Word seg_flags =
2631 Layout::section_flags_to_segment((*p)->flags());
2632 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
2634 oseg->add_output_section(*p, seg_flags);
2636 Layout::Section_list::const_iterator pnext = p + 1;
2637 while (pnext != sections->end()
2638 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
2640 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2641 oseg->add_output_section(*pnext, seg_flags);
2651 // Add a program header. The PHDRS clause is syntactically distinct
2652 // from the SECTIONS clause, but we implement it with the SECTIONS
2653 // support becauase PHDRS is useless if there is no SECTIONS clause.
2656 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
2657 bool includes_filehdr, bool includes_phdrs,
2658 bool is_flags_valid, unsigned int flags,
2659 Expression* load_address)
2661 if (this->phdrs_elements_ == NULL)
2662 this->phdrs_elements_ = new Phdrs_elements();
2663 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
2666 is_flags_valid, flags,
2670 // Return the number of segments we expect to create based on the
2671 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2674 Script_sections::expected_segment_count(const Layout* layout) const
2676 if (this->saw_phdrs_clause())
2677 return this->phdrs_elements_->size();
2679 Layout::Section_list sections;
2680 layout->get_allocated_sections(§ions);
2682 // We assume that we will need two PT_LOAD segments.
2685 bool saw_note = false;
2686 bool saw_tls = false;
2687 for (Layout::Section_list::const_iterator p = sections.begin();
2688 p != sections.end();
2691 if ((*p)->type() == elfcpp::SHT_NOTE)
2693 // Assume that all note sections will fit into a single
2701 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2703 // There can only be one PT_TLS segment.
2715 // Create the segments from a PHDRS clause. Return the segment which
2716 // should hold the file header and program headers, if any.
2719 Script_sections::create_segments_from_phdrs_clause(Layout* layout)
2721 this->attach_sections_using_phdrs_clause(layout);
2722 return this->set_phdrs_clause_addresses(layout);
2725 // Create the segments from the PHDRS clause, and put the output
2726 // sections in them.
2729 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
2731 typedef std::map<std::string, Output_segment*> Name_to_segment;
2732 Name_to_segment name_to_segment;
2733 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2734 p != this->phdrs_elements_->end();
2736 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
2738 // Walk through the output sections and attach them to segments.
2739 // Output sections in the script which do not list segments are
2740 // attached to the same set of segments as the immediately preceding
2742 String_list* phdr_names = NULL;
2743 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2744 p != this->sections_elements_->end();
2747 Output_section* os = (*p)->allocate_to_segment(&phdr_names);
2751 if (phdr_names == NULL)
2753 gold_error(_("allocated section not in any segment"));
2757 bool in_load_segment = false;
2758 for (String_list::const_iterator q = phdr_names->begin();
2759 q != phdr_names->end();
2762 Name_to_segment::const_iterator r = name_to_segment.find(*q);
2763 if (r == name_to_segment.end())
2764 gold_error(_("no segment %s"), q->c_str());
2767 elfcpp::Elf_Word seg_flags =
2768 Layout::section_flags_to_segment(os->flags());
2769 r->second->add_output_section(os, seg_flags);
2771 if (r->second->type() == elfcpp::PT_LOAD)
2773 if (in_load_segment)
2774 gold_error(_("section in two PT_LOAD segments"));
2775 in_load_segment = true;
2780 if (!in_load_segment)
2781 gold_error(_("allocated section not in any PT_LOAD segment"));
2785 // Set the addresses for segments created from a PHDRS clause. Return
2786 // the segment which should hold the file header and program headers,
2790 Script_sections::set_phdrs_clause_addresses(Layout* layout)
2792 Output_segment* load_seg = NULL;
2793 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2794 p != this->phdrs_elements_->end();
2797 // Note that we have to set the flags after adding the output
2798 // sections to the segment, as adding an output segment can
2799 // change the flags.
2800 (*p)->set_flags_if_valid();
2802 Output_segment* oseg = (*p)->segment();
2804 if (oseg->type() != elfcpp::PT_LOAD)
2806 // The addresses of non-PT_LOAD segments are set from the
2807 // PT_LOAD segments.
2808 if ((*p)->has_load_address())
2809 gold_error(_("may only specify load address for PT_LOAD segment"));
2813 // The output sections should have addresses from the SECTIONS
2814 // clause. The addresses don't have to be in order, so find the
2815 // one with the lowest load address. Use that to set the
2816 // address of the segment.
2818 Output_section* osec = oseg->section_with_lowest_load_address();
2821 oseg->set_addresses(0, 0);
2825 uint64_t vma = osec->address();
2826 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
2828 // Override the load address of the section with the load
2829 // address specified for the segment.
2830 if ((*p)->has_load_address())
2832 if (osec->has_load_address())
2833 gold_warning(_("PHDRS load address overrides "
2834 "section %s load address"),
2837 lma = (*p)->load_address();
2840 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
2841 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
2843 // We could support this if we wanted to.
2844 gold_error(_("using only one of FILEHDR and PHDRS is "
2845 "not currently supported"));
2849 size_t sizeof_headers = this->total_header_size(layout);
2850 uint64_t subtract = this->header_size_adjustment(lma,
2852 if (lma >= subtract && vma >= subtract)
2859 gold_error(_("sections loaded on first page without room "
2860 "for file and program headers "
2861 "are not supported"));
2864 if (load_seg != NULL)
2865 gold_error(_("using FILEHDR and PHDRS on more than one "
2866 "PT_LOAD segment is not currently supported"));
2870 oseg->set_addresses(vma, lma);
2876 // Add the file header and segment headers to non-load segments
2877 // specified in the PHDRS clause.
2880 Script_sections::put_headers_in_phdrs(Output_data* file_header,
2881 Output_data* segment_headers)
2883 gold_assert(this->saw_phdrs_clause());
2884 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2885 p != this->phdrs_elements_->end();
2888 if ((*p)->type() != elfcpp::PT_LOAD)
2890 if ((*p)->includes_phdrs())
2891 (*p)->segment()->add_initial_output_data(segment_headers);
2892 if ((*p)->includes_filehdr())
2893 (*p)->segment()->add_initial_output_data(file_header);
2898 // Print the SECTIONS clause to F for debugging.
2901 Script_sections::print(FILE* f) const
2903 if (!this->saw_sections_clause_)
2906 fprintf(f, "SECTIONS {\n");
2908 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2909 p != this->sections_elements_->end();
2915 if (this->phdrs_elements_ != NULL)
2917 fprintf(f, "PHDRS {\n");
2918 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2919 p != this->phdrs_elements_->end();
2926 } // End namespace gold.