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.
32 #include "parameters.h"
38 #include "script-sections.h"
40 // Support for the SECTIONS clause in linker scripts.
45 // An element in a SECTIONS clause.
47 class Sections_element
53 virtual ~Sections_element()
56 // Add any symbol being defined to the symbol table.
58 add_symbols_to_table(Symbol_table
*)
61 // Finalize symbols and check assertions.
63 finalize_symbols(Symbol_table
*, const Layout
*, bool*, uint64_t*)
66 // Return the output section name to use for an input file name and
67 // section name. This only real implementation is in
68 // Output_section_definition.
70 output_section_name(const char*, const char*, Output_section
***)
73 // Return whether to place an orphan output section after this
76 place_orphan_here(const Output_section
*, bool*) const
79 // Set section addresses. This includes applying assignments if the
80 // the expression is an absolute value.
82 set_section_addresses(Symbol_table
*, Layout
*, bool*, uint64_t*)
85 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
86 // this section is constrained, and the input sections do not match,
87 // return the constraint, and set *POSD.
88 virtual Section_constraint
89 check_constraint(Output_section_definition
**)
90 { return CONSTRAINT_NONE
; }
92 // See if this is the alternate output section for a constrained
93 // output section. If it is, transfer the Output_section and return
94 // true. Otherwise return false.
96 alternate_constraint(Output_section_definition
*, Section_constraint
)
99 // Print the element for debugging purposes.
101 print(FILE* f
) const = 0;
104 // An assignment in a SECTIONS clause outside of an output section.
106 class Sections_element_assignment
: public Sections_element
109 Sections_element_assignment(const char* name
, size_t namelen
,
110 Expression
* val
, bool provide
, bool hidden
)
111 : assignment_(name
, namelen
, val
, provide
, hidden
)
114 // Add the symbol to the symbol table.
116 add_symbols_to_table(Symbol_table
* symtab
)
117 { this->assignment_
.add_to_table(symtab
); }
119 // Finalize the symbol.
121 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
122 bool* dot_has_value
, uint64_t* dot_value
)
124 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_has_value
,
128 // Set the section address. There is no section here, but if the
129 // value is absolute, we set the symbol. This permits us to use
130 // absolute symbols when setting dot.
132 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
133 bool* dot_has_value
, uint64_t* dot_value
)
135 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_has_value
,
139 // Print for debugging.
144 this->assignment_
.print(f
);
148 Symbol_assignment assignment_
;
151 // An assignment to the dot symbol in a SECTIONS clause outside of an
154 class Sections_element_dot_assignment
: public Sections_element
157 Sections_element_dot_assignment(Expression
* val
)
161 // Finalize the symbol.
163 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
164 bool* dot_has_value
, uint64_t* dot_value
)
167 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
169 *dot_has_value
= true;
172 // Update the dot symbol while setting section addresses.
174 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
175 bool* dot_has_value
, uint64_t* dot_value
)
178 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
179 *dot_value
, &is_absolute
);
181 gold_error(_("dot set to non-absolute value"));
182 *dot_has_value
= true;
185 // Print for debugging.
190 this->val_
->print(f
);
198 // An assertion in a SECTIONS clause outside of an output section.
200 class Sections_element_assertion
: public Sections_element
203 Sections_element_assertion(Expression
* check
, const char* message
,
205 : assertion_(check
, message
, messagelen
)
208 // Check the assertion.
210 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, bool*,
212 { this->assertion_
.check(symtab
, layout
); }
214 // Print for debugging.
219 this->assertion_
.print(f
);
223 Script_assertion assertion_
;
226 // An element in an output section in a SECTIONS clause.
228 class Output_section_element
231 // A list of input sections.
232 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
234 Output_section_element()
237 virtual ~Output_section_element()
240 // Add any symbol being defined to the symbol table.
242 add_symbols_to_table(Symbol_table
*)
245 // Finalize symbols and check assertions.
247 finalize_symbols(Symbol_table
*, const Layout
*, bool*, uint64_t*)
250 // Return whether this element matches FILE_NAME and SECTION_NAME.
251 // The only real implementation is in Output_section_element_input.
253 match_name(const char*, const char*) const
256 // Set section addresses. This includes applying assignments if the
257 // the expression is an absolute value.
259 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
260 uint64_t*, std::string
*, Input_section_list
*)
263 // Print the element for debugging purposes.
265 print(FILE* f
) const = 0;
268 // Return a fill string that is LENGTH bytes long, filling it with
271 get_fill_string(const std::string
* fill
, section_size_type length
) const;
275 Output_section_element::get_fill_string(const std::string
* fill
,
276 section_size_type length
) const
278 std::string this_fill
;
279 this_fill
.reserve(length
);
280 while (this_fill
.length() + fill
->length() <= length
)
282 if (this_fill
.length() < length
)
283 this_fill
.append(*fill
, 0, length
- this_fill
.length());
287 // A symbol assignment in an output section.
289 class Output_section_element_assignment
: public Output_section_element
292 Output_section_element_assignment(const char* name
, size_t namelen
,
293 Expression
* val
, bool provide
,
295 : assignment_(name
, namelen
, val
, provide
, hidden
)
298 // Add the symbol to the symbol table.
300 add_symbols_to_table(Symbol_table
* symtab
)
301 { this->assignment_
.add_to_table(symtab
); }
303 // Finalize the symbol.
305 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
306 bool* dot_has_value
, uint64_t* dot_value
)
308 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_has_value
,
312 // Set the section address. There is no section here, but if the
313 // value is absolute, we set the symbol. This permits us to use
314 // absolute symbols when setting dot.
316 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
317 uint64_t, uint64_t* dot_value
, std::string
*,
320 this->assignment_
.set_if_absolute(symtab
, layout
, true, true, *dot_value
);
323 // Print for debugging.
328 this->assignment_
.print(f
);
332 Symbol_assignment assignment_
;
335 // An assignment to the dot symbol in an output section.
337 class Output_section_element_dot_assignment
: public Output_section_element
340 Output_section_element_dot_assignment(Expression
* val
)
344 // Finalize the symbol.
346 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
347 bool* dot_has_value
, uint64_t* dot_value
)
350 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
352 *dot_has_value
= true;
355 // Update the dot symbol while setting section addresses.
357 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
358 uint64_t, uint64_t* dot_value
, std::string
*,
359 Input_section_list
*);
361 // Print for debugging.
366 this->val_
->print(f
);
374 // Update the dot symbol while setting section addresses.
377 Output_section_element_dot_assignment::set_section_addresses(
378 Symbol_table
* symtab
,
380 Output_section
* output_section
,
387 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, true,
388 *dot_value
, &is_absolute
);
390 gold_error(_("dot set to non-absolute value"));
391 if (next_dot
< *dot_value
)
392 gold_error(_("dot may not move backward"));
393 if (next_dot
> *dot_value
&& output_section
!= NULL
)
395 section_size_type length
= convert_to_section_size_type(next_dot
397 Output_section_data
* posd
;
399 posd
= new Output_data_fixed_space(length
, 0);
402 std::string this_fill
= this->get_fill_string(fill
, length
);
403 posd
= new Output_data_const(this_fill
, 0);
405 output_section
->add_output_section_data(posd
);
407 *dot_value
= next_dot
;
410 // An assertion in an output section.
412 class Output_section_element_assertion
: public Output_section_element
415 Output_section_element_assertion(Expression
* check
, const char* message
,
417 : assertion_(check
, message
, messagelen
)
424 this->assertion_
.print(f
);
428 Script_assertion assertion_
;
431 // A data item in an output section.
433 class Output_section_element_data
: public Output_section_element
436 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
437 : size_(size
), is_signed_(is_signed
), val_(val
)
440 // Finalize symbols--we just need to update dot.
442 finalize_symbols(Symbol_table
*, const Layout
*, bool*, uint64_t* dot_value
)
443 { *dot_value
+= this->size_
; }
445 // Store the value in the section.
447 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
448 uint64_t* dot_value
, std::string
*,
449 Input_section_list
*);
451 // Print for debugging.
456 template<bool big_endian
>
458 set_fill_string(uint64_t);
460 // The size in bytes.
462 // Whether the value is signed.
468 // Store the value in the section.
471 Output_section_element_data::set_section_addresses(Symbol_table
* symtab
,
479 gold_assert(os
!= NULL
);
482 uint64_t val
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
485 gold_error(_("data directive with non-absolute value"));
488 if (parameters
->is_big_endian())
489 fill
= this->set_fill_string
<true>(val
);
491 fill
= this->set_fill_string
<false>(val
);
493 os
->add_output_section_data(new Output_data_const(fill
, 0));
495 *dot_value
+= this->size_
;
498 // Get the value to store in a std::string.
500 template<bool big_endian
>
502 Output_section_element_data::set_fill_string(uint64_t val
)
505 unsigned char buf
[8];
509 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
510 ret
.assign(reinterpret_cast<char*>(buf
), 1);
513 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
514 ret
.assign(reinterpret_cast<char*>(buf
), 2);
517 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
518 ret
.assign(reinterpret_cast<char*>(buf
), 4);
521 if (parameters
->get_size() == 32)
524 if (this->is_signed_
&& (val
& 0x80000000) != 0)
525 val
|= 0xffffffff00000000LL
;
527 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
528 ret
.assign(reinterpret_cast<char*>(buf
), 8);
536 // Print for debugging.
539 Output_section_element_data::print(FILE* f
) const
554 if (this->is_signed_
)
562 fprintf(f
, " %s(", s
);
563 this->val_
->print(f
);
567 // A fill value setting in an output section.
569 class Output_section_element_fill
: public Output_section_element
572 Output_section_element_fill(Expression
* val
)
576 // Update the fill value while setting section addresses.
578 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
579 uint64_t, uint64_t* dot_value
, std::string
* fill
,
583 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, true,
587 gold_error(_("fill set to non-absolute value"));
588 // FIXME: The GNU linker supports fill values of arbitrary length.
589 unsigned char fill_buff
[4];
590 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
591 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
594 // Print for debugging.
598 fprintf(f
, " FILL(");
599 this->val_
->print(f
);
604 // The new fill value.
608 // Return whether STRING contains a wildcard character. This is used
609 // to speed up matching.
612 is_wildcard_string(const std::string
& s
)
614 return strpbrk(s
.c_str(), "?*[") != NULL
;
617 // An input section specification in an output section
619 class Output_section_element_input
: public Output_section_element
622 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
624 // Finalize symbols--just update the value of the dot symbol.
626 finalize_symbols(Symbol_table
*, const Layout
*, bool* dot_has_value
,
629 *dot_value
= this->final_dot_value_
;
630 *dot_has_value
= true;
633 // See whether we match FILE_NAME and SECTION_NAME as an input
636 match_name(const char* file_name
, const char* section_name
) const;
638 // Set the section address.
640 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
641 uint64_t subalign
, uint64_t* dot_value
,
642 std::string
* fill
, Input_section_list
*);
644 // Print for debugging.
646 print(FILE* f
) const;
649 // An input section pattern.
650 struct Input_section_pattern
653 bool pattern_is_wildcard
;
656 Input_section_pattern(const char* patterna
, size_t patternlena
,
658 : pattern(patterna
, patternlena
),
659 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
664 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
666 // Filename_exclusions is a pair of filename pattern and a bool
667 // indicating whether the filename is a wildcard.
668 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
670 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
671 // indicates whether this is a wildcard pattern.
673 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
675 return (is_wildcard_pattern
676 ? fnmatch(pattern
, string
, 0) == 0
677 : strcmp(string
, pattern
) == 0);
680 // See if we match a file name.
682 match_file_name(const char* file_name
) const;
684 // The file name pattern. If this is the empty string, we match all
686 std::string filename_pattern_
;
687 // Whether the file name pattern is a wildcard.
688 bool filename_is_wildcard_
;
689 // How the file names should be sorted. This may only be
690 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
691 Sort_wildcard filename_sort_
;
692 // The list of file names to exclude.
693 Filename_exclusions filename_exclusions_
;
694 // The list of input section patterns.
695 Input_section_patterns input_section_patterns_
;
696 // Whether to keep this section when garbage collecting.
698 // The value of dot after including all matching sections.
699 uint64_t final_dot_value_
;
702 // Construct Output_section_element_input. The parser records strings
703 // as pointers into a copy of the script file, which will go away when
704 // parsing is complete. We make sure they are in std::string objects.
706 Output_section_element_input::Output_section_element_input(
707 const Input_section_spec
* spec
,
709 : filename_pattern_(),
710 filename_is_wildcard_(false),
711 filename_sort_(spec
->file
.sort
),
712 filename_exclusions_(),
713 input_section_patterns_(),
717 // The filename pattern "*" is common, and matches all files. Turn
718 // it into the empty string.
719 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
720 this->filename_pattern_
.assign(spec
->file
.name
.value
,
721 spec
->file
.name
.length
);
722 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
724 if (spec
->input_sections
.exclude
!= NULL
)
726 for (String_list::const_iterator p
=
727 spec
->input_sections
.exclude
->begin();
728 p
!= spec
->input_sections
.exclude
->end();
731 bool is_wildcard
= is_wildcard_string(*p
);
732 this->filename_exclusions_
.push_back(std::make_pair(*p
,
737 if (spec
->input_sections
.sections
!= NULL
)
739 Input_section_patterns
& isp(this->input_section_patterns_
);
740 for (String_sort_list::const_iterator p
=
741 spec
->input_sections
.sections
->begin();
742 p
!= spec
->input_sections
.sections
->end();
744 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
749 // See whether we match FILE_NAME.
752 Output_section_element_input::match_file_name(const char* file_name
) const
754 if (!this->filename_pattern_
.empty())
756 // If we were called with no filename, we refuse to match a
757 // pattern which requires a file name.
758 if (file_name
== NULL
)
761 if (!match(file_name
, this->filename_pattern_
.c_str(),
762 this->filename_is_wildcard_
))
766 if (file_name
!= NULL
)
768 // Now we have to see whether FILE_NAME matches one of the
769 // exclusion patterns, if any.
770 for (Filename_exclusions::const_iterator p
=
771 this->filename_exclusions_
.begin();
772 p
!= this->filename_exclusions_
.end();
775 if (match(file_name
, p
->first
.c_str(), p
->second
))
783 // See whether we match FILE_NAME and SECTION_NAME.
786 Output_section_element_input::match_name(const char* file_name
,
787 const char* section_name
) const
789 if (!this->match_file_name(file_name
))
792 // If there are no section name patterns, then we match.
793 if (this->input_section_patterns_
.empty())
796 // See whether we match the section name patterns.
797 for (Input_section_patterns::const_iterator p
=
798 this->input_section_patterns_
.begin();
799 p
!= this->input_section_patterns_
.end();
802 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
806 // We didn't match any section names, so we didn't match.
810 // Information we use to sort the input sections.
812 struct Input_section_info
816 std::string section_name
;
821 // A class to sort the input sections.
823 class Input_section_sorter
826 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
827 : filename_sort_(filename_sort
), section_sort_(section_sort
)
831 operator()(const Input_section_info
&, const Input_section_info
&) const;
834 Sort_wildcard filename_sort_
;
835 Sort_wildcard section_sort_
;
839 Input_section_sorter::operator()(const Input_section_info
& isi1
,
840 const Input_section_info
& isi2
) const
842 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
843 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
844 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
845 && isi1
.addralign
== isi2
.addralign
))
847 if (isi1
.section_name
!= isi2
.section_name
)
848 return isi1
.section_name
< isi2
.section_name
;
850 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
851 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
852 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
854 if (isi1
.addralign
!= isi2
.addralign
)
855 return isi1
.addralign
< isi2
.addralign
;
857 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
859 if (isi1
.relobj
->name() != isi2
.relobj
->name())
860 return isi1
.relobj
->name() < isi2
.relobj
->name();
863 // Otherwise we leave them in the same order.
867 // Set the section address. Look in INPUT_SECTIONS for sections which
868 // match this spec, sort them as specified, and add them to the output
872 Output_section_element_input::set_section_addresses(
875 Output_section
* output_section
,
879 Input_section_list
* input_sections
)
881 // We build a list of sections which match each
882 // Input_section_pattern.
884 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
885 size_t input_pattern_count
= this->input_section_patterns_
.size();
886 if (input_pattern_count
== 0)
887 input_pattern_count
= 1;
888 Matching_sections
matching_sections(input_pattern_count
);
890 // Look through the list of sections for this output section. Add
891 // each one which matches to one of the elements of
892 // MATCHING_SECTIONS.
894 Input_section_list::iterator p
= input_sections
->begin();
895 while (p
!= input_sections
->end())
897 // Calling section_name and section_addralign is not very
899 Input_section_info isi
;
900 isi
.relobj
= p
->first
;
901 isi
.shndx
= p
->second
;
903 // Lock the object so that we can get information about the
904 // section. This is OK since we know we are single-threaded
907 const Task
* task
= reinterpret_cast<const Task
*>(-1);
908 Task_lock_obj
<Object
> tl(task
, p
->first
);
910 isi
.section_name
= p
->first
->section_name(p
->second
);
911 isi
.size
= p
->first
->section_size(p
->second
);
912 isi
.addralign
= p
->first
->section_addralign(p
->second
);
915 if (!this->match_file_name(isi
.relobj
->name().c_str()))
917 else if (this->input_section_patterns_
.empty())
919 matching_sections
[0].push_back(isi
);
920 p
= input_sections
->erase(p
);
925 for (i
= 0; i
< input_pattern_count
; ++i
)
927 const Input_section_pattern
&
928 isp(this->input_section_patterns_
[i
]);
929 if (match(isi
.section_name
.c_str(), isp
.pattern
.c_str(),
930 isp
.pattern_is_wildcard
))
934 if (i
>= this->input_section_patterns_
.size())
938 matching_sections
[i
].push_back(isi
);
939 p
= input_sections
->erase(p
);
944 // Look through MATCHING_SECTIONS. Sort each one as specified,
945 // using a stable sort so that we get the default order when
946 // sections are otherwise equal. Add each input section to the
949 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
951 if (matching_sections
[i
].empty())
954 gold_assert(output_section
!= NULL
);
956 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
957 if (isp
.sort
!= SORT_WILDCARD_NONE
958 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
959 std::stable_sort(matching_sections
[i
].begin(),
960 matching_sections
[i
].end(),
961 Input_section_sorter(this->filename_sort_
,
964 for (std::vector
<Input_section_info
>::const_iterator p
=
965 matching_sections
[i
].begin();
966 p
!= matching_sections
[i
].end();
969 uint64_t this_subalign
= p
->addralign
;
970 if (this_subalign
< subalign
)
971 this_subalign
= subalign
;
973 uint64_t address
= align_address(*dot_value
, this_subalign
);
975 if (address
> *dot_value
&& !fill
->empty())
977 section_size_type length
=
978 convert_to_section_size_type(address
- *dot_value
);
979 std::string this_fill
= this->get_fill_string(fill
, length
);
980 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
981 output_section
->add_output_section_data(posd
);
984 output_section
->add_input_section_for_script(p
->relobj
,
989 *dot_value
= address
+ p
->size
;
993 this->final_dot_value_
= *dot_value
;
996 // Print for debugging.
999 Output_section_element_input::print(FILE* f
) const
1004 fprintf(f
, "KEEP(");
1006 if (!this->filename_pattern_
.empty())
1008 bool need_close_paren
= false;
1009 switch (this->filename_sort_
)
1011 case SORT_WILDCARD_NONE
:
1013 case SORT_WILDCARD_BY_NAME
:
1014 fprintf(f
, "SORT_BY_NAME(");
1015 need_close_paren
= true;
1021 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1023 if (need_close_paren
)
1027 if (!this->input_section_patterns_
.empty()
1028 || !this->filename_exclusions_
.empty())
1032 bool need_space
= false;
1033 if (!this->filename_exclusions_
.empty())
1035 fprintf(f
, "EXCLUDE_FILE(");
1036 bool need_comma
= false;
1037 for (Filename_exclusions::const_iterator p
=
1038 this->filename_exclusions_
.begin();
1039 p
!= this->filename_exclusions_
.end();
1044 fprintf(f
, "%s", p
->first
.c_str());
1051 for (Input_section_patterns::const_iterator p
=
1052 this->input_section_patterns_
.begin();
1053 p
!= this->input_section_patterns_
.end();
1059 int close_parens
= 0;
1062 case SORT_WILDCARD_NONE
:
1064 case SORT_WILDCARD_BY_NAME
:
1065 fprintf(f
, "SORT_BY_NAME(");
1068 case SORT_WILDCARD_BY_ALIGNMENT
:
1069 fprintf(f
, "SORT_BY_ALIGNMENT(");
1072 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1073 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1076 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1077 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1084 fprintf(f
, "%s", p
->pattern
.c_str());
1086 for (int i
= 0; i
< close_parens
; ++i
)
1101 // An output section.
1103 class Output_section_definition
: public Sections_element
1106 typedef Output_section_element::Input_section_list Input_section_list
;
1108 Output_section_definition(const char* name
, size_t namelen
,
1109 const Parser_output_section_header
* header
);
1111 // Finish the output section with the information in the trailer.
1113 finish(const Parser_output_section_trailer
* trailer
);
1115 // Add a symbol to be defined.
1117 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1118 bool provide
, bool hidden
);
1120 // Add an assignment to the special dot symbol.
1122 add_dot_assignment(Expression
* value
);
1124 // Add an assertion.
1126 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1128 // Add a data item to the current output section.
1130 add_data(int size
, bool is_signed
, Expression
* val
);
1132 // Add a setting for the fill value.
1134 add_fill(Expression
* val
);
1136 // Add an input section specification.
1138 add_input_section(const Input_section_spec
* spec
, bool keep
);
1140 // Add any symbols being defined to the symbol table.
1142 add_symbols_to_table(Symbol_table
* symtab
);
1144 // Finalize symbols and check assertions.
1146 finalize_symbols(Symbol_table
*, const Layout
*, bool*, uint64_t*);
1148 // Return the output section name to use for an input file name and
1151 output_section_name(const char* file_name
, const char* section_name
,
1154 // Return whether to place an orphan section after this one.
1156 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1158 // Set the section address.
1160 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1161 bool* dot_has_value
, uint64_t* dot_value
);
1163 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1164 // this section is constrained, and the input sections do not match,
1165 // return the constraint, and set *POSD.
1167 check_constraint(Output_section_definition
** posd
);
1169 // See if this is the alternate output section for a constrained
1170 // output section. If it is, transfer the Output_section and return
1171 // true. Otherwise return false.
1173 alternate_constraint(Output_section_definition
*, Section_constraint
);
1175 // Print the contents to the FILE. This is for debugging.
1180 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1182 // The output section name.
1184 // The address. This may be NULL.
1185 Expression
* address_
;
1186 // The load address. This may be NULL.
1187 Expression
* load_address_
;
1188 // The alignment. This may be NULL.
1190 // The input section alignment. This may be NULL.
1191 Expression
* subalign_
;
1192 // The constraint, if any.
1193 Section_constraint constraint_
;
1194 // The fill value. This may be NULL.
1196 // The list of elements defining the section.
1197 Output_section_elements elements_
;
1198 // The Output_section created for this definition. This will be
1199 // NULL if none was created.
1200 Output_section
* output_section_
;
1205 Output_section_definition::Output_section_definition(
1208 const Parser_output_section_header
* header
)
1209 : name_(name
, namelen
),
1210 address_(header
->address
),
1211 load_address_(header
->load_address
),
1212 align_(header
->align
),
1213 subalign_(header
->subalign
),
1214 constraint_(header
->constraint
),
1217 output_section_(NULL
)
1221 // Finish an output section.
1224 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1226 this->fill_
= trailer
->fill
;
1229 // Add a symbol to be defined.
1232 Output_section_definition::add_symbol_assignment(const char* name
,
1238 Output_section_element
* p
= new Output_section_element_assignment(name
,
1243 this->elements_
.push_back(p
);
1246 // Add an assignment to the special dot symbol.
1249 Output_section_definition::add_dot_assignment(Expression
* value
)
1251 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1252 this->elements_
.push_back(p
);
1255 // Add an assertion.
1258 Output_section_definition::add_assertion(Expression
* check
,
1259 const char* message
,
1262 Output_section_element
* p
= new Output_section_element_assertion(check
,
1265 this->elements_
.push_back(p
);
1268 // Add a data item to the current output section.
1271 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1273 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1275 this->elements_
.push_back(p
);
1278 // Add a setting for the fill value.
1281 Output_section_definition::add_fill(Expression
* val
)
1283 Output_section_element
* p
= new Output_section_element_fill(val
);
1284 this->elements_
.push_back(p
);
1287 // Add an input section specification.
1290 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1293 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1294 this->elements_
.push_back(p
);
1297 // Add any symbols being defined to the symbol table.
1300 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1302 for (Output_section_elements::iterator p
= this->elements_
.begin();
1303 p
!= this->elements_
.end();
1305 (*p
)->add_symbols_to_table(symtab
);
1308 // Finalize symbols and check assertions.
1311 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1312 const Layout
* layout
,
1313 bool* dot_has_value
,
1314 uint64_t* dot_value
)
1316 if (this->output_section_
!= NULL
)
1317 *dot_value
= this->output_section_
->address();
1320 uint64_t address
= *dot_value
;
1321 if (this->address_
!= NULL
)
1324 address
= this->address_
->eval_with_dot(symtab
, layout
,
1325 *dot_has_value
, *dot_value
,
1328 if (this->align_
!= NULL
)
1331 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
,
1335 address
= align_address(address
, align
);
1337 *dot_value
= address
;
1339 *dot_has_value
= true;
1341 for (Output_section_elements::iterator p
= this->elements_
.begin();
1342 p
!= this->elements_
.end();
1344 (*p
)->finalize_symbols(symtab
, layout
, dot_has_value
, dot_value
);
1347 // Return the output section name to use for an input section name.
1350 Output_section_definition::output_section_name(const char* file_name
,
1351 const char* section_name
,
1352 Output_section
*** slot
)
1354 // Ask each element whether it matches NAME.
1355 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1356 p
!= this->elements_
.end();
1359 if ((*p
)->match_name(file_name
, section_name
))
1361 // We found a match for NAME, which means that it should go
1362 // into this output section.
1363 *slot
= &this->output_section_
;
1364 return this->name_
.c_str();
1368 // We don't know about this section name.
1372 // Return whether to place an orphan output section after this
1376 Output_section_definition::place_orphan_here(const Output_section
*os
,
1379 // Check for the simple case first.
1380 if (this->output_section_
!= NULL
1381 && this->output_section_
->type() == os
->type()
1382 && this->output_section_
->flags() == os
->flags())
1388 // Otherwise use some heuristics.
1390 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1393 if (os
->type() == elfcpp::SHT_NOBITS
)
1395 if (this->output_section_
!= NULL
1396 && this->output_section_
->type() == elfcpp::SHT_NOBITS
)
1398 if (this->name_
== ".bss")
1401 else if (os
->type() == elfcpp::SHT_NOTE
)
1403 if (this->output_section_
!= NULL
1404 && this->output_section_
->type() == elfcpp::SHT_NOTE
)
1406 if (this->name_
== ".interp"
1407 || this->name_
.compare(0, 5, ".note") == 0)
1410 else if (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
1412 if (this->output_section_
!= NULL
1413 && (this->output_section_
->type() == elfcpp::SHT_REL
1414 || this->output_section_
->type() == elfcpp::SHT_RELA
))
1416 if (this->name_
.compare(0, 4, ".rel") == 0)
1419 else if (os
->type() == elfcpp::SHT_PROGBITS
1420 && (os
->flags() & elfcpp::SHF_WRITE
) != 0)
1422 if (this->output_section_
!= NULL
1423 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1424 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1426 if (this->name_
== ".data")
1429 else if (os
->type() == elfcpp::SHT_PROGBITS
1430 && (os
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1432 if (this->output_section_
!= NULL
1433 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1434 && (this->output_section_
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1436 if (this->name_
== ".text")
1439 else if (os
->type() == elfcpp::SHT_PROGBITS
)
1441 if (this->output_section_
!= NULL
1442 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1443 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0
1444 && (this->output_section_
->flags() & elfcpp::SHF_EXECINSTR
) == 0)
1446 if (this->name_
== ".rodata")
1453 // Set the section address. Note that the OUTPUT_SECTION_ field will
1454 // be NULL if no input sections were mapped to this output section.
1455 // We still have to adjust dot and process symbol assignments.
1458 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1460 bool* dot_has_value
,
1461 uint64_t* dot_value
)
1465 if (this->address_
!= NULL
)
1467 address
= this->address_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
1468 *dot_value
, &is_absolute
);
1470 gold_error(_("address of section %s is not absolute"),
1471 this->name_
.c_str());
1475 if (!*dot_has_value
)
1476 gold_error(_("no address given for section %s"),
1477 this->name_
.c_str());
1478 address
= *dot_value
;
1482 if (this->align_
== NULL
)
1484 if (this->output_section_
== NULL
)
1487 align
= this->output_section_
->addralign();
1491 align
= this->align_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
1492 *dot_value
, &is_absolute
);
1494 gold_error(_("alignment of section %s is not absolute"),
1495 this->name_
.c_str());
1496 if (this->output_section_
!= NULL
)
1497 this->output_section_
->set_addralign(align
);
1500 address
= align_address(address
, align
);
1502 *dot_value
= address
;
1503 *dot_has_value
= true;
1505 // The address of non-SHF_ALLOC sections is forced to zero,
1506 // regardless of what the linker script wants.
1507 if (this->output_section_
!= NULL
1508 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1509 this->output_section_
->set_address(address
);
1511 if (this->load_address_
!= NULL
&& this->output_section_
!= NULL
)
1513 uint64_t load_address
=
1514 this->load_address_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
1515 *dot_value
, &is_absolute
);
1517 gold_error(_("load address of section %s is not absolute"),
1518 this->name_
.c_str());
1519 this->output_section_
->set_load_address(load_address
);
1523 if (this->subalign_
== NULL
)
1527 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
1528 *dot_value
, &is_absolute
);
1530 gold_error(_("subalign of section %s is not absolute"),
1531 this->name_
.c_str());
1535 if (this->fill_
!= NULL
)
1537 // FIXME: The GNU linker supports fill values of arbitrary
1539 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
,
1544 gold_error(_("fill of section %s is not absolute"),
1545 this->name_
.c_str());
1546 unsigned char fill_buff
[4];
1547 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1548 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1551 Input_section_list input_sections
;
1552 if (this->output_section_
!= NULL
)
1554 // Get the list of input sections attached to this output
1555 // section. This will leave the output section with only
1556 // Output_section_data entries.
1557 address
+= this->output_section_
->get_input_sections(address
,
1560 *dot_value
= address
;
1563 for (Output_section_elements::iterator p
= this->elements_
.begin();
1564 p
!= this->elements_
.end();
1566 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
1567 subalign
, dot_value
, &fill
, &input_sections
);
1569 gold_assert(input_sections
.empty());
1572 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1573 // this section is constrained, and the input sections do not match,
1574 // return the constraint, and set *POSD.
1577 Output_section_definition::check_constraint(Output_section_definition
** posd
)
1579 switch (this->constraint_
)
1581 case CONSTRAINT_NONE
:
1582 return CONSTRAINT_NONE
;
1584 case CONSTRAINT_ONLY_IF_RO
:
1585 if (this->output_section_
!= NULL
1586 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1589 return CONSTRAINT_ONLY_IF_RO
;
1591 return CONSTRAINT_NONE
;
1593 case CONSTRAINT_ONLY_IF_RW
:
1594 if (this->output_section_
!= NULL
1595 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1598 return CONSTRAINT_ONLY_IF_RW
;
1600 return CONSTRAINT_NONE
;
1602 case CONSTRAINT_SPECIAL
:
1603 if (this->output_section_
!= NULL
)
1604 gold_error(_("SPECIAL constraints are not implemented"));
1605 return CONSTRAINT_NONE
;
1612 // See if this is the alternate output section for a constrained
1613 // output section. If it is, transfer the Output_section and return
1614 // true. Otherwise return false.
1617 Output_section_definition::alternate_constraint(
1618 Output_section_definition
* posd
,
1619 Section_constraint constraint
)
1621 if (this->name_
!= posd
->name_
)
1626 case CONSTRAINT_ONLY_IF_RO
:
1627 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
1631 case CONSTRAINT_ONLY_IF_RW
:
1632 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
1640 // We have found the alternate constraint. We just need to move
1641 // over the Output_section. When constraints are used properly,
1642 // THIS should not have an output_section pointer, as all the input
1643 // sections should have matched the other definition.
1645 if (this->output_section_
!= NULL
)
1646 gold_error(_("mismatched definition for constrained sections"));
1648 this->output_section_
= posd
->output_section_
;
1649 posd
->output_section_
= NULL
;
1654 // Print for debugging.
1657 Output_section_definition::print(FILE* f
) const
1659 fprintf(f
, " %s ", this->name_
.c_str());
1661 if (this->address_
!= NULL
)
1663 this->address_
->print(f
);
1669 if (this->load_address_
!= NULL
)
1672 this->load_address_
->print(f
);
1676 if (this->align_
!= NULL
)
1678 fprintf(f
, "ALIGN(");
1679 this->align_
->print(f
);
1683 if (this->subalign_
!= NULL
)
1685 fprintf(f
, "SUBALIGN(");
1686 this->subalign_
->print(f
);
1692 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1693 p
!= this->elements_
.end();
1699 if (this->fill_
!= NULL
)
1702 this->fill_
->print(f
);
1708 // An output section created to hold orphaned input sections. These
1709 // do not actually appear in linker scripts. However, for convenience
1710 // when setting the output section addresses, we put a marker to these
1711 // sections in the appropriate place in the list of SECTIONS elements.
1713 class Orphan_output_section
: public Sections_element
1716 Orphan_output_section(Output_section
* os
)
1720 // Return whether to place an orphan section after this one.
1722 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1724 // Set section addresses.
1726 set_section_addresses(Symbol_table
*, Layout
*, bool*, uint64_t*);
1728 // Print for debugging.
1730 print(FILE* f
) const
1732 fprintf(f
, " marker for orphaned output section %s\n",
1737 Output_section
* os_
;
1740 // Whether to place another orphan section after this one.
1743 Orphan_output_section::place_orphan_here(const Output_section
* os
,
1746 if (this->os_
->type() == os
->type()
1747 && this->os_
->flags() == os
->flags())
1755 // Set section addresses.
1758 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
1759 bool* dot_has_value
,
1760 uint64_t* dot_value
)
1762 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
1764 if (!*dot_has_value
)
1765 gold_error(_("no address for orphan section %s"), this->os_
->name());
1767 uint64_t address
= *dot_value
;
1768 address
= align_address(address
, this->os_
->addralign());
1770 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1771 this->os_
->set_address(address
);
1773 Input_section_list input_sections
;
1774 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
1776 for (Input_section_list::iterator p
= input_sections
.begin();
1777 p
!= input_sections
.end();
1783 // We know what are single-threaded, so it is OK to lock the
1786 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1787 Task_lock_obj
<Object
> tl(task
, p
->first
);
1788 addralign
= p
->first
->section_addralign(p
->second
);
1789 size
= p
->first
->section_size(p
->second
);
1792 address
= align_address(address
, addralign
);
1793 this->os_
->add_input_section_for_script(p
->first
, p
->second
, size
, 0);
1797 *dot_value
= address
;
1800 // Class Script_sections.
1802 Script_sections::Script_sections()
1803 : saw_sections_clause_(false),
1804 in_sections_clause_(false),
1805 sections_elements_(NULL
),
1806 output_section_(NULL
)
1810 // Start a SECTIONS clause.
1813 Script_sections::start_sections()
1815 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
1816 this->saw_sections_clause_
= true;
1817 this->in_sections_clause_
= true;
1818 if (this->sections_elements_
== NULL
)
1819 this->sections_elements_
= new Sections_elements
;
1822 // Finish a SECTIONS clause.
1825 Script_sections::finish_sections()
1827 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
1828 this->in_sections_clause_
= false;
1831 // Add a symbol to be defined.
1834 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
1835 Expression
* val
, bool provide
,
1838 if (this->output_section_
!= NULL
)
1839 this->output_section_
->add_symbol_assignment(name
, length
, val
,
1843 Sections_element
* p
= new Sections_element_assignment(name
, length
,
1846 this->sections_elements_
->push_back(p
);
1850 // Add an assignment to the special dot symbol.
1853 Script_sections::add_dot_assignment(Expression
* val
)
1855 if (this->output_section_
!= NULL
)
1856 this->output_section_
->add_dot_assignment(val
);
1859 Sections_element
* p
= new Sections_element_dot_assignment(val
);
1860 this->sections_elements_
->push_back(p
);
1864 // Add an assertion.
1867 Script_sections::add_assertion(Expression
* check
, const char* message
,
1870 if (this->output_section_
!= NULL
)
1871 this->output_section_
->add_assertion(check
, message
, messagelen
);
1874 Sections_element
* p
= new Sections_element_assertion(check
, message
,
1876 this->sections_elements_
->push_back(p
);
1880 // Start processing entries for an output section.
1883 Script_sections::start_output_section(
1886 const Parser_output_section_header
*header
)
1888 Output_section_definition
* posd
= new Output_section_definition(name
,
1891 this->sections_elements_
->push_back(posd
);
1892 gold_assert(this->output_section_
== NULL
);
1893 this->output_section_
= posd
;
1896 // Stop processing entries for an output section.
1899 Script_sections::finish_output_section(
1900 const Parser_output_section_trailer
* trailer
)
1902 gold_assert(this->output_section_
!= NULL
);
1903 this->output_section_
->finish(trailer
);
1904 this->output_section_
= NULL
;
1907 // Add a data item to the current output section.
1910 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
1912 gold_assert(this->output_section_
!= NULL
);
1913 this->output_section_
->add_data(size
, is_signed
, val
);
1916 // Add a fill value setting to the current output section.
1919 Script_sections::add_fill(Expression
* val
)
1921 gold_assert(this->output_section_
!= NULL
);
1922 this->output_section_
->add_fill(val
);
1925 // Add an input section specification to the current output section.
1928 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
1930 gold_assert(this->output_section_
!= NULL
);
1931 this->output_section_
->add_input_section(spec
, keep
);
1934 // Add any symbols we are defining to the symbol table.
1937 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
1939 if (!this->saw_sections_clause_
)
1941 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
1942 p
!= this->sections_elements_
->end();
1944 (*p
)->add_symbols_to_table(symtab
);
1947 // Finalize symbols and check assertions.
1950 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
1952 if (!this->saw_sections_clause_
)
1954 bool dot_has_value
= false;
1955 uint64_t dot_value
= 0;
1956 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
1957 p
!= this->sections_elements_
->end();
1959 (*p
)->finalize_symbols(symtab
, layout
, &dot_has_value
, &dot_value
);
1962 // Return the name of the output section to use for an input file name
1963 // and section name.
1966 Script_sections::output_section_name(const char* file_name
,
1967 const char* section_name
,
1968 Output_section
*** output_section_slot
)
1970 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
1971 p
!= this->sections_elements_
->end();
1974 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
1975 output_section_slot
);
1979 // The special name /DISCARD/ means that the input section
1980 // should be discarded.
1981 if (strcmp(ret
, "/DISCARD/") == 0)
1983 *output_section_slot
= NULL
;
1990 // If we couldn't find a mapping for the name, the output section
1991 // gets the name of the input section.
1993 *output_section_slot
= NULL
;
1995 return section_name
;
1998 // Place a marker for an orphan output section into the SECTIONS
2002 Script_sections::place_orphan(Output_section
* os
)
2004 // Look for an output section definition which matches the output
2005 // section. Put a marker after that section.
2006 Sections_elements::iterator place
= this->sections_elements_
->end();
2007 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2008 p
!= this->sections_elements_
->end();
2012 if ((*p
)->place_orphan_here(os
, &exact
))
2020 // The insert function puts the new element before the iterator.
2021 if (place
!= this->sections_elements_
->end())
2024 this->sections_elements_
->insert(place
, new Orphan_output_section(os
));
2027 // Set the addresses of all the output sections. Walk through all the
2028 // elements, tracking the dot symbol. Apply assignments which set
2029 // absolute symbol values, in case they are used when setting dot.
2030 // Fill in data statement values. As we find output sections, set the
2031 // address, set the address of all associated input sections, and
2032 // update dot. Return the segment which should hold the file header
2033 // and segment headers, if any.
2036 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2038 gold_assert(this->saw_sections_clause_
);
2040 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2041 // for our representation.
2042 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2043 p
!= this->sections_elements_
->end();
2046 Output_section_definition
* posd
;
2047 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2048 if (failed_constraint
!= CONSTRAINT_NONE
)
2050 Sections_elements::iterator q
;
2051 for (q
= this->sections_elements_
->begin();
2052 q
!= this->sections_elements_
->end();
2057 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2062 if (q
== this->sections_elements_
->end())
2063 gold_error(_("no matching section constraint"));
2067 bool dot_has_value
= false;
2068 uint64_t dot_value
= 0;
2069 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2070 p
!= this->sections_elements_
->end();
2072 (*p
)->set_section_addresses(symtab
, layout
, &dot_has_value
, &dot_value
);
2074 return this->create_segments(layout
);
2077 // Sort the sections in order to put them into segments.
2079 class Sort_output_sections
2083 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2087 Sort_output_sections::operator()(const Output_section
* os1
,
2088 const Output_section
* os2
) const
2090 // Sort first by the load address.
2091 uint64_t lma1
= (os1
->has_load_address()
2092 ? os1
->load_address()
2094 uint64_t lma2
= (os2
->has_load_address()
2095 ? os2
->load_address()
2100 // Then sort by the virtual address.
2101 if (os1
->address() != os2
->address())
2102 return os1
->address() < os2
->address();
2104 // Sort TLS sections to the end.
2105 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2106 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2110 // Sort PROGBITS before NOBITS.
2111 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2113 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2116 // Otherwise we don't care.
2120 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2121 // We treat a section with the SHF_TLS flag set as taking up space
2122 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2123 // space for them in the file.
2126 Script_sections::is_bss_section(const Output_section
* os
)
2128 return (os
->type() == elfcpp::SHT_NOBITS
2129 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2132 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2133 // the segment which should hold the file header and segment headers,
2137 Script_sections::create_segments(Layout
* layout
)
2139 gold_assert(this->saw_sections_clause_
);
2141 if (parameters
->output_is_object())
2144 Layout::Section_list sections
;
2145 layout
->get_allocated_sections(§ions
);
2147 // Sort the sections by address.
2148 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2150 this->create_note_and_tls_segments(layout
, §ions
);
2152 // Walk through the sections adding them to PT_LOAD segments.
2153 const uint64_t abi_pagesize
= parameters
->target()->abi_pagesize();
2154 Output_segment
* first_seg
= NULL
;
2155 Output_segment
* current_seg
= NULL
;
2156 bool is_current_seg_readonly
= true;
2157 Layout::Section_list::iterator plast
= sections
.end();
2158 uint64_t last_vma
= 0;
2159 uint64_t last_lma
= 0;
2160 uint64_t last_size
= 0;
2161 for (Layout::Section_list::iterator p
= sections
.begin();
2162 p
!= sections
.end();
2165 const uint64_t vma
= (*p
)->address();
2166 const uint64_t lma
= ((*p
)->has_load_address()
2167 ? (*p
)->load_address()
2169 const uint64_t size
= (*p
)->current_data_size();
2171 bool need_new_segment
;
2172 if (current_seg
== NULL
)
2173 need_new_segment
= true;
2174 else if (lma
- vma
!= last_lma
- last_vma
)
2176 // This section has a different LMA relationship than the
2177 // last one; we need a new segment.
2178 need_new_segment
= true;
2180 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2181 < align_address(lma
, abi_pagesize
))
2183 // Putting this section in the segment would require
2185 need_new_segment
= true;
2187 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2189 // A non-BSS section can not follow a BSS section in the
2191 need_new_segment
= true;
2193 else if (is_current_seg_readonly
2194 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2196 // Don't put a writable section in the same segment as a
2197 // non-writable section.
2198 need_new_segment
= true;
2202 // Otherwise, reuse the existing segment.
2203 need_new_segment
= false;
2206 elfcpp::Elf_Word seg_flags
=
2207 Layout::section_flags_to_segment((*p
)->flags());
2209 if (need_new_segment
)
2211 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2213 current_seg
->set_addresses(vma
, lma
);
2214 if (first_seg
== NULL
)
2215 first_seg
= current_seg
;
2216 is_current_seg_readonly
= true;
2219 current_seg
->add_output_section(*p
, seg_flags
);
2221 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2222 is_current_seg_readonly
= false;
2230 // An ELF program should work even if the program headers are not in
2231 // a PT_LOAD segment. However, it appears that the Linux kernel
2232 // does not set the AT_PHDR auxiliary entry in that case. It sets
2233 // the load address to p_vaddr - p_offset of the first PT_LOAD
2234 // segment. It then sets AT_PHDR to the load address plus the
2235 // offset to the program headers, e_phoff in the file header. This
2236 // fails when the program headers appear in the file before the
2237 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2238 // segment to hold the file header and the program headers. This is
2239 // effectively what the GNU linker does, and it is slightly more
2240 // efficient in any case. We try to use the first PT_LOAD segment
2241 // if we can, otherwise we make a new one.
2243 size_t segment_count
= layout
->segment_count();
2244 size_t file_header_size
;
2245 size_t segment_headers_size
;
2246 if (parameters
->get_size() == 32)
2248 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2249 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2251 else if (parameters
->get_size() == 64)
2253 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2254 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2259 size_t sizeof_headers
= file_header_size
+ segment_headers_size
;
2261 if (first_seg
!= NULL
2262 && (first_seg
->paddr() & (abi_pagesize
- 1)) >= sizeof_headers
)
2264 first_seg
->set_addresses(first_seg
->vaddr() - sizeof_headers
,
2265 first_seg
->paddr() - sizeof_headers
);
2269 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2271 if (first_seg
== NULL
)
2272 load_seg
->set_addresses(0, 0);
2275 uint64_t vma
= first_seg
->vaddr();
2276 uint64_t lma
= first_seg
->paddr();
2278 // We want a segment with the same relationship between VMA and
2279 // LMA, but with enough room for the headers, and aligned to
2280 // load at the start of a page.
2281 uint64_t hdr_lma
= lma
- sizeof_headers
;
2282 hdr_lma
&= ~(abi_pagesize
- 1);
2283 if (lma
>= hdr_lma
&& vma
>= (lma
- hdr_lma
))
2284 load_seg
->set_addresses(vma
- (lma
- hdr_lma
), hdr_lma
);
2287 // We could handle this case by create the file header
2288 // outside of any PT_LOAD segment, and creating a new
2289 // PT_LOAD segment after the others to hold the segment
2291 gold_error(_("sections loaded on first page without room for "
2292 "file and program headers are not supported"));
2299 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2300 // segment if there are any SHT_TLS sections.
2303 Script_sections::create_note_and_tls_segments(
2305 const Layout::Section_list
* sections
)
2307 bool saw_tls
= false;
2308 for (Layout::Section_list::const_iterator p
= sections
->begin();
2309 p
!= sections
->end();
2312 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2314 elfcpp::Elf_Word seg_flags
=
2315 Layout::section_flags_to_segment((*p
)->flags());
2316 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
2318 oseg
->add_output_section(*p
, seg_flags
);
2320 // Incorporate any subsequent SHT_NOTE sections, in the
2321 // hopes that the script is sensible.
2322 Layout::Section_list::const_iterator pnext
= p
+ 1;
2323 while (pnext
!= sections
->end()
2324 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
2326 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2327 oseg
->add_output_section(*pnext
, seg_flags
);
2333 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2336 gold_error(_("TLS sections are not adjacent"));
2338 elfcpp::Elf_Word seg_flags
=
2339 Layout::section_flags_to_segment((*p
)->flags());
2340 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
2342 oseg
->add_output_section(*p
, seg_flags
);
2344 Layout::Section_list::const_iterator pnext
= p
+ 1;
2345 while (pnext
!= sections
->end()
2346 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
2348 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2349 oseg
->add_output_section(*pnext
, seg_flags
);
2359 // Return the number of segments we expect to create based on the
2360 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2363 Script_sections::expected_segment_count(const Layout
* layout
) const
2365 Layout::Section_list sections
;
2366 layout
->get_allocated_sections(§ions
);
2368 // We assume that we will need two PT_LOAD segments.
2371 bool saw_note
= false;
2372 bool saw_tls
= false;
2373 for (Layout::Section_list::const_iterator p
= sections
.begin();
2374 p
!= sections
.end();
2377 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2379 // Assume that all note sections will fit into a single
2387 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2389 // There can only be one PT_TLS segment.
2401 // Print the SECTIONS clause to F for debugging.
2404 Script_sections::print(FILE* f
) const
2406 if (!this->saw_sections_clause_
)
2409 fprintf(f
, "SECTIONS {\n");
2411 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2412 p
!= this->sections_elements_
->end();
2419 } // End namespace gold.