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 // Print the element for debugging purposes.
87 print(FILE* f
) const = 0;
90 // An assignment in a SECTIONS clause outside of an output section.
92 class Sections_element_assignment
: public Sections_element
95 Sections_element_assignment(const char* name
, size_t namelen
,
96 Expression
* val
, bool provide
, bool hidden
)
97 : assignment_(name
, namelen
, val
, provide
, hidden
)
100 // Add the symbol to the symbol table.
102 add_symbols_to_table(Symbol_table
* symtab
)
103 { this->assignment_
.add_to_table(symtab
); }
105 // Finalize the symbol.
107 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
108 bool* dot_has_value
, uint64_t* dot_value
)
110 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_has_value
,
114 // Set the section address. There is no section here, but if the
115 // value is absolute, we set the symbol. This permits us to use
116 // absolute symbols when setting dot.
118 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
119 bool* dot_has_value
, uint64_t* dot_value
)
121 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_has_value
,
125 // Print for debugging.
130 this->assignment_
.print(f
);
134 Symbol_assignment assignment_
;
137 // An assignment to the dot symbol in a SECTIONS clause outside of an
140 class Sections_element_dot_assignment
: public Sections_element
143 Sections_element_dot_assignment(Expression
* val
)
147 // Finalize the symbol.
149 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
150 bool* dot_has_value
, uint64_t* dot_value
)
153 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
155 *dot_has_value
= true;
158 // Update the dot symbol while setting section addresses.
160 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
161 bool* dot_has_value
, uint64_t* dot_value
)
164 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
165 *dot_value
, &is_absolute
);
167 gold_error(_("dot set to non-absolute value"));
168 *dot_has_value
= true;
171 // Print for debugging.
176 this->val_
->print(f
);
184 // An assertion in a SECTIONS clause outside of an output section.
186 class Sections_element_assertion
: public Sections_element
189 Sections_element_assertion(Expression
* check
, const char* message
,
191 : assertion_(check
, message
, messagelen
)
194 // Check the assertion.
196 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, bool*,
198 { this->assertion_
.check(symtab
, layout
); }
200 // Print for debugging.
205 this->assertion_
.print(f
);
209 Script_assertion assertion_
;
212 // An element in an output section in a SECTIONS clause.
214 class Output_section_element
217 // A list of input sections.
218 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
220 Output_section_element()
223 virtual ~Output_section_element()
226 // Add any symbol being defined to the symbol table.
228 add_symbols_to_table(Symbol_table
*)
231 // Finalize symbols and check assertions.
233 finalize_symbols(Symbol_table
*, const Layout
*, bool*, uint64_t*)
236 // Return whether this element matches FILE_NAME and SECTION_NAME.
237 // The only real implementation is in Output_section_element_input.
239 match_name(const char*, const char*) const
242 // Set section addresses. This includes applying assignments if the
243 // the expression is an absolute value.
245 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
246 uint64_t*, std::string
*, Input_section_list
*)
249 // Print the element for debugging purposes.
251 print(FILE* f
) const = 0;
254 // Return a fill string that is LENGTH bytes long, filling it with
257 get_fill_string(const std::string
* fill
, section_size_type length
) const;
261 Output_section_element::get_fill_string(const std::string
* fill
,
262 section_size_type length
) const
264 std::string this_fill
;
265 this_fill
.reserve(length
);
266 while (this_fill
.length() + fill
->length() <= length
)
268 if (this_fill
.length() < length
)
269 this_fill
.append(*fill
, 0, length
- this_fill
.length());
273 // A symbol assignment in an output section.
275 class Output_section_element_assignment
: public Output_section_element
278 Output_section_element_assignment(const char* name
, size_t namelen
,
279 Expression
* val
, bool provide
,
281 : assignment_(name
, namelen
, val
, provide
, hidden
)
284 // Add the symbol to the symbol table.
286 add_symbols_to_table(Symbol_table
* symtab
)
287 { this->assignment_
.add_to_table(symtab
); }
289 // Finalize the symbol.
291 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
292 bool* dot_has_value
, uint64_t* dot_value
)
294 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_has_value
,
298 // Set the section address. There is no section here, but if the
299 // value is absolute, we set the symbol. This permits us to use
300 // absolute symbols when setting dot.
302 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
303 uint64_t, uint64_t* dot_value
, std::string
*,
306 this->assignment_
.set_if_absolute(symtab
, layout
, true, true, *dot_value
);
309 // Print for debugging.
314 this->assignment_
.print(f
);
318 Symbol_assignment assignment_
;
321 // An assignment to the dot symbol in an output section.
323 class Output_section_element_dot_assignment
: public Output_section_element
326 Output_section_element_dot_assignment(Expression
* val
)
330 // Finalize the symbol.
332 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
333 bool* dot_has_value
, uint64_t* dot_value
)
336 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
338 *dot_has_value
= true;
341 // Update the dot symbol while setting section addresses.
343 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
344 uint64_t, uint64_t* dot_value
, std::string
*,
345 Input_section_list
*);
347 // Print for debugging.
352 this->val_
->print(f
);
360 // Update the dot symbol while setting section addresses.
363 Output_section_element_dot_assignment::set_section_addresses(
364 Symbol_table
* symtab
,
366 Output_section
* output_section
,
373 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, true,
374 *dot_value
, &is_absolute
);
376 gold_error(_("dot set to non-absolute value"));
377 if (next_dot
< *dot_value
)
378 gold_error(_("dot may not move backward"));
379 if (next_dot
> *dot_value
&& output_section
!= NULL
)
381 section_size_type length
= convert_to_section_size_type(next_dot
383 Output_section_data
* posd
;
385 posd
= new Output_data_fixed_space(length
, 0);
388 std::string this_fill
= this->get_fill_string(fill
, length
);
389 posd
= new Output_data_const(this_fill
, 0);
391 output_section
->add_output_section_data(posd
);
393 *dot_value
= next_dot
;
396 // An assertion in an output section.
398 class Output_section_element_assertion
: public Output_section_element
401 Output_section_element_assertion(Expression
* check
, const char* message
,
403 : assertion_(check
, message
, messagelen
)
410 this->assertion_
.print(f
);
414 Script_assertion assertion_
;
417 // A data item in an output section.
419 class Output_section_element_data
: public Output_section_element
422 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
423 : size_(size
), is_signed_(is_signed
), val_(val
)
426 // Finalize symbols--we just need to update dot.
428 finalize_symbols(Symbol_table
*, const Layout
*, bool*, uint64_t* dot_value
)
429 { *dot_value
+= this->size_
; }
431 // Store the value in the section.
433 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
434 uint64_t* dot_value
, std::string
*,
435 Input_section_list
*);
437 // Print for debugging.
442 template<bool big_endian
>
444 set_fill_string(uint64_t);
446 // The size in bytes.
448 // Whether the value is signed.
454 // Store the value in the section.
457 Output_section_element_data::set_section_addresses(Symbol_table
* symtab
,
465 gold_assert(os
!= NULL
);
468 uint64_t val
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
471 gold_error(_("data directive with non-absolute value"));
474 if (parameters
->is_big_endian())
475 fill
= this->set_fill_string
<true>(val
);
477 fill
= this->set_fill_string
<false>(val
);
479 os
->add_output_section_data(new Output_data_const(fill
, 0));
481 *dot_value
+= this->size_
;
484 // Get the value to store in a std::string.
486 template<bool big_endian
>
488 Output_section_element_data::set_fill_string(uint64_t val
)
491 unsigned char buf
[8];
495 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
496 ret
.assign(reinterpret_cast<char*>(buf
), 1);
499 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
500 ret
.assign(reinterpret_cast<char*>(buf
), 2);
503 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
504 ret
.assign(reinterpret_cast<char*>(buf
), 4);
507 if (parameters
->get_size() == 32)
510 if (this->is_signed_
&& (val
& 0x80000000) != 0)
511 val
|= 0xffffffff00000000LL
;
513 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
514 ret
.assign(reinterpret_cast<char*>(buf
), 8);
522 // Print for debugging.
525 Output_section_element_data::print(FILE* f
) const
540 if (this->is_signed_
)
548 fprintf(f
, " %s(", s
);
549 this->val_
->print(f
);
553 // A fill value setting in an output section.
555 class Output_section_element_fill
: public Output_section_element
558 Output_section_element_fill(Expression
* val
)
562 // Update the fill value while setting section addresses.
564 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
565 uint64_t, uint64_t* dot_value
, std::string
* fill
,
569 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, true,
573 gold_error(_("fill set to non-absolute value"));
574 // FIXME: The GNU linker supports fill values of arbitrary length.
575 unsigned char fill_buff
[4];
576 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
577 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
580 // Print for debugging.
584 fprintf(f
, " FILL(");
585 this->val_
->print(f
);
590 // The new fill value.
594 // Return whether STRING contains a wildcard character. This is used
595 // to speed up matching.
598 is_wildcard_string(const std::string
& s
)
600 return strpbrk(s
.c_str(), "?*[") != NULL
;
603 // An input section specification in an output section
605 class Output_section_element_input
: public Output_section_element
608 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
610 // Finalize symbols--just update the value of the dot symbol.
612 finalize_symbols(Symbol_table
*, const Layout
*, bool* dot_has_value
,
615 *dot_value
= this->final_dot_value_
;
616 *dot_has_value
= true;
619 // See whether we match FILE_NAME and SECTION_NAME as an input
622 match_name(const char* file_name
, const char* section_name
) const;
624 // Set the section address.
626 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
627 uint64_t subalign
, uint64_t* dot_value
,
628 std::string
* fill
, Input_section_list
*);
630 // Print for debugging.
632 print(FILE* f
) const;
635 // An input section pattern.
636 struct Input_section_pattern
639 bool pattern_is_wildcard
;
642 Input_section_pattern(const char* patterna
, size_t patternlena
,
644 : pattern(patterna
, patternlena
),
645 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
650 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
652 // Filename_exclusions is a pair of filename pattern and a bool
653 // indicating whether the filename is a wildcard.
654 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
656 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
657 // indicates whether this is a wildcard pattern.
659 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
661 return (is_wildcard_pattern
662 ? fnmatch(pattern
, string
, 0) == 0
663 : strcmp(string
, pattern
) == 0);
666 // See if we match a file name.
668 match_file_name(const char* file_name
) const;
670 // The file name pattern. If this is the empty string, we match all
672 std::string filename_pattern_
;
673 // Whether the file name pattern is a wildcard.
674 bool filename_is_wildcard_
;
675 // How the file names should be sorted. This may only be
676 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
677 Sort_wildcard filename_sort_
;
678 // The list of file names to exclude.
679 Filename_exclusions filename_exclusions_
;
680 // The list of input section patterns.
681 Input_section_patterns input_section_patterns_
;
682 // Whether to keep this section when garbage collecting.
684 // The value of dot after including all matching sections.
685 uint64_t final_dot_value_
;
688 // Construct Output_section_element_input. The parser records strings
689 // as pointers into a copy of the script file, which will go away when
690 // parsing is complete. We make sure they are in std::string objects.
692 Output_section_element_input::Output_section_element_input(
693 const Input_section_spec
* spec
,
695 : filename_pattern_(),
696 filename_is_wildcard_(false),
697 filename_sort_(spec
->file
.sort
),
698 filename_exclusions_(),
699 input_section_patterns_(),
703 // The filename pattern "*" is common, and matches all files. Turn
704 // it into the empty string.
705 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
706 this->filename_pattern_
.assign(spec
->file
.name
.value
,
707 spec
->file
.name
.length
);
708 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
710 if (spec
->input_sections
.exclude
!= NULL
)
712 for (String_list::const_iterator p
=
713 spec
->input_sections
.exclude
->begin();
714 p
!= spec
->input_sections
.exclude
->end();
717 bool is_wildcard
= is_wildcard_string(*p
);
718 this->filename_exclusions_
.push_back(std::make_pair(*p
,
723 if (spec
->input_sections
.sections
!= NULL
)
725 Input_section_patterns
& isp(this->input_section_patterns_
);
726 for (String_sort_list::const_iterator p
=
727 spec
->input_sections
.sections
->begin();
728 p
!= spec
->input_sections
.sections
->end();
730 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
735 // See whether we match FILE_NAME.
738 Output_section_element_input::match_file_name(const char* file_name
) const
740 if (!this->filename_pattern_
.empty())
742 // If we were called with no filename, we refuse to match a
743 // pattern which requires a file name.
744 if (file_name
== NULL
)
747 if (!match(file_name
, this->filename_pattern_
.c_str(),
748 this->filename_is_wildcard_
))
752 if (file_name
!= NULL
)
754 // Now we have to see whether FILE_NAME matches one of the
755 // exclusion patterns, if any.
756 for (Filename_exclusions::const_iterator p
=
757 this->filename_exclusions_
.begin();
758 p
!= this->filename_exclusions_
.end();
761 if (match(file_name
, p
->first
.c_str(), p
->second
))
769 // See whether we match FILE_NAME and SECTION_NAME.
772 Output_section_element_input::match_name(const char* file_name
,
773 const char* section_name
) const
775 if (!this->match_file_name(file_name
))
778 // If there are no section name patterns, then we match.
779 if (this->input_section_patterns_
.empty())
782 // See whether we match the section name patterns.
783 for (Input_section_patterns::const_iterator p
=
784 this->input_section_patterns_
.begin();
785 p
!= this->input_section_patterns_
.end();
788 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
792 // We didn't match any section names, so we didn't match.
796 // Information we use to sort the input sections.
798 struct Input_section_info
802 std::string section_name
;
807 // A class to sort the input sections.
809 class Input_section_sorter
812 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
813 : filename_sort_(filename_sort
), section_sort_(section_sort
)
817 operator()(const Input_section_info
&, const Input_section_info
&) const;
820 Sort_wildcard filename_sort_
;
821 Sort_wildcard section_sort_
;
825 Input_section_sorter::operator()(const Input_section_info
& isi1
,
826 const Input_section_info
& isi2
) const
828 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
829 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
830 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
831 && isi1
.addralign
== isi2
.addralign
))
833 if (isi1
.section_name
!= isi2
.section_name
)
834 return isi1
.section_name
< isi2
.section_name
;
836 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
837 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
838 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
840 if (isi1
.addralign
!= isi2
.addralign
)
841 return isi1
.addralign
< isi2
.addralign
;
843 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
845 if (isi1
.relobj
->name() != isi2
.relobj
->name())
846 return isi1
.relobj
->name() < isi2
.relobj
->name();
849 // Otherwise we leave them in the same order.
853 // Set the section address. Look in INPUT_SECTIONS for sections which
854 // match this spec, sort them as specified, and add them to the output
858 Output_section_element_input::set_section_addresses(
861 Output_section
* output_section
,
865 Input_section_list
* input_sections
)
867 // We build a list of sections which match each
868 // Input_section_pattern.
870 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
871 size_t input_pattern_count
= this->input_section_patterns_
.size();
872 if (input_pattern_count
== 0)
873 input_pattern_count
= 1;
874 Matching_sections
matching_sections(input_pattern_count
);
876 // Look through the list of sections for this output section. Add
877 // each one which matches to one of the elements of
878 // MATCHING_SECTIONS.
880 Input_section_list::iterator p
= input_sections
->begin();
881 while (p
!= input_sections
->end())
883 // Calling section_name and section_addralign is not very
885 Input_section_info isi
;
886 isi
.relobj
= p
->first
;
887 isi
.shndx
= p
->second
;
889 // Lock the object so that we can get information about the
890 // section. This is OK since we know we are single-threaded
893 const Task
* task
= reinterpret_cast<const Task
*>(-1);
894 Task_lock_obj
<Object
> tl(task
, p
->first
);
896 isi
.section_name
= p
->first
->section_name(p
->second
);
897 isi
.size
= p
->first
->section_size(p
->second
);
898 isi
.addralign
= p
->first
->section_addralign(p
->second
);
901 if (!this->match_file_name(isi
.relobj
->name().c_str()))
903 else if (this->input_section_patterns_
.empty())
905 matching_sections
[0].push_back(isi
);
906 p
= input_sections
->erase(p
);
911 for (i
= 0; i
< input_pattern_count
; ++i
)
913 const Input_section_pattern
&
914 isp(this->input_section_patterns_
[i
]);
915 if (match(isi
.section_name
.c_str(), isp
.pattern
.c_str(),
916 isp
.pattern_is_wildcard
))
920 if (i
>= this->input_section_patterns_
.size())
924 matching_sections
[i
].push_back(isi
);
925 p
= input_sections
->erase(p
);
930 // Look through MATCHING_SECTIONS. Sort each one as specified,
931 // using a stable sort so that we get the default order when
932 // sections are otherwise equal. Add each input section to the
935 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
937 if (matching_sections
[i
].empty())
940 gold_assert(output_section
!= NULL
);
942 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
943 if (isp
.sort
!= SORT_WILDCARD_NONE
944 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
945 std::stable_sort(matching_sections
[i
].begin(),
946 matching_sections
[i
].end(),
947 Input_section_sorter(this->filename_sort_
,
950 for (std::vector
<Input_section_info
>::const_iterator p
=
951 matching_sections
[i
].begin();
952 p
!= matching_sections
[i
].end();
955 uint64_t this_subalign
= p
->addralign
;
956 if (this_subalign
< subalign
)
957 this_subalign
= subalign
;
959 uint64_t address
= align_address(*dot_value
, this_subalign
);
961 if (address
> *dot_value
&& !fill
->empty())
963 section_size_type length
=
964 convert_to_section_size_type(address
- *dot_value
);
965 std::string this_fill
= this->get_fill_string(fill
, length
);
966 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
967 output_section
->add_output_section_data(posd
);
970 output_section
->add_input_section_for_script(p
->relobj
,
975 *dot_value
= address
+ p
->size
;
979 this->final_dot_value_
= *dot_value
;
982 // Print for debugging.
985 Output_section_element_input::print(FILE* f
) const
992 if (!this->filename_pattern_
.empty())
994 bool need_close_paren
= false;
995 switch (this->filename_sort_
)
997 case SORT_WILDCARD_NONE
:
999 case SORT_WILDCARD_BY_NAME
:
1000 fprintf(f
, "SORT_BY_NAME(");
1001 need_close_paren
= true;
1007 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1009 if (need_close_paren
)
1013 if (!this->input_section_patterns_
.empty()
1014 || !this->filename_exclusions_
.empty())
1018 bool need_space
= false;
1019 if (!this->filename_exclusions_
.empty())
1021 fprintf(f
, "EXCLUDE_FILE(");
1022 bool need_comma
= false;
1023 for (Filename_exclusions::const_iterator p
=
1024 this->filename_exclusions_
.begin();
1025 p
!= this->filename_exclusions_
.end();
1030 fprintf(f
, "%s", p
->first
.c_str());
1037 for (Input_section_patterns::const_iterator p
=
1038 this->input_section_patterns_
.begin();
1039 p
!= this->input_section_patterns_
.end();
1045 int close_parens
= 0;
1048 case SORT_WILDCARD_NONE
:
1050 case SORT_WILDCARD_BY_NAME
:
1051 fprintf(f
, "SORT_BY_NAME(");
1054 case SORT_WILDCARD_BY_ALIGNMENT
:
1055 fprintf(f
, "SORT_BY_ALIGNMENT(");
1058 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1059 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1062 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1063 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1070 fprintf(f
, "%s", p
->pattern
.c_str());
1072 for (int i
= 0; i
< close_parens
; ++i
)
1087 // An output section.
1089 class Output_section_definition
: public Sections_element
1092 typedef Output_section_element::Input_section_list Input_section_list
;
1094 Output_section_definition(const char* name
, size_t namelen
,
1095 const Parser_output_section_header
* header
);
1097 // Finish the output section with the information in the trailer.
1099 finish(const Parser_output_section_trailer
* trailer
);
1101 // Add a symbol to be defined.
1103 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1104 bool provide
, bool hidden
);
1106 // Add an assignment to the special dot symbol.
1108 add_dot_assignment(Expression
* value
);
1110 // Add an assertion.
1112 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1114 // Add a data item to the current output section.
1116 add_data(int size
, bool is_signed
, Expression
* val
);
1118 // Add a setting for the fill value.
1120 add_fill(Expression
* val
);
1122 // Add an input section specification.
1124 add_input_section(const Input_section_spec
* spec
, bool keep
);
1126 // Add any symbols being defined to the symbol table.
1128 add_symbols_to_table(Symbol_table
* symtab
);
1130 // Finalize symbols and check assertions.
1132 finalize_symbols(Symbol_table
*, const Layout
*, bool*, uint64_t*);
1134 // Return the output section name to use for an input file name and
1137 output_section_name(const char* file_name
, const char* section_name
,
1140 // Return whether to place an orphan section after this one.
1142 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1144 // Set the section address.
1146 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1147 bool* dot_has_value
, uint64_t* dot_value
);
1149 // Print the contents to the FILE. This is for debugging.
1154 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1156 // The output section name.
1158 // The address. This may be NULL.
1159 Expression
* address_
;
1160 // The load address. This may be NULL.
1161 Expression
* load_address_
;
1162 // The alignment. This may be NULL.
1164 // The input section alignment. This may be NULL.
1165 Expression
* subalign_
;
1166 // The fill value. This may be NULL.
1168 // The list of elements defining the section.
1169 Output_section_elements elements_
;
1170 // The Output_section created for this definition. This will be
1171 // NULL if none was created.
1172 Output_section
* output_section_
;
1177 Output_section_definition::Output_section_definition(
1180 const Parser_output_section_header
* header
)
1181 : name_(name
, namelen
),
1182 address_(header
->address
),
1183 load_address_(header
->load_address
),
1184 align_(header
->align
),
1185 subalign_(header
->subalign
),
1188 output_section_(NULL
)
1192 // Finish an output section.
1195 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1197 this->fill_
= trailer
->fill
;
1200 // Add a symbol to be defined.
1203 Output_section_definition::add_symbol_assignment(const char* name
,
1209 Output_section_element
* p
= new Output_section_element_assignment(name
,
1214 this->elements_
.push_back(p
);
1217 // Add an assignment to the special dot symbol.
1220 Output_section_definition::add_dot_assignment(Expression
* value
)
1222 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1223 this->elements_
.push_back(p
);
1226 // Add an assertion.
1229 Output_section_definition::add_assertion(Expression
* check
,
1230 const char* message
,
1233 Output_section_element
* p
= new Output_section_element_assertion(check
,
1236 this->elements_
.push_back(p
);
1239 // Add a data item to the current output section.
1242 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1244 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1246 this->elements_
.push_back(p
);
1249 // Add a setting for the fill value.
1252 Output_section_definition::add_fill(Expression
* val
)
1254 Output_section_element
* p
= new Output_section_element_fill(val
);
1255 this->elements_
.push_back(p
);
1258 // Add an input section specification.
1261 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1264 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1265 this->elements_
.push_back(p
);
1268 // Add any symbols being defined to the symbol table.
1271 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1273 for (Output_section_elements::iterator p
= this->elements_
.begin();
1274 p
!= this->elements_
.end();
1276 (*p
)->add_symbols_to_table(symtab
);
1279 // Finalize symbols and check assertions.
1282 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1283 const Layout
* layout
,
1284 bool* dot_has_value
,
1285 uint64_t* dot_value
)
1287 if (this->output_section_
!= NULL
)
1288 *dot_value
= this->output_section_
->address();
1291 uint64_t address
= *dot_value
;
1292 if (this->address_
!= NULL
)
1295 address
= this->address_
->eval_with_dot(symtab
, layout
,
1296 *dot_has_value
, *dot_value
,
1299 if (this->align_
!= NULL
)
1302 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
,
1306 address
= align_address(address
, align
);
1308 *dot_value
= address
;
1310 *dot_has_value
= true;
1312 for (Output_section_elements::iterator p
= this->elements_
.begin();
1313 p
!= this->elements_
.end();
1315 (*p
)->finalize_symbols(symtab
, layout
, dot_has_value
, dot_value
);
1318 // Return the output section name to use for an input section name.
1321 Output_section_definition::output_section_name(const char* file_name
,
1322 const char* section_name
,
1323 Output_section
*** slot
)
1325 // Ask each element whether it matches NAME.
1326 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1327 p
!= this->elements_
.end();
1330 if ((*p
)->match_name(file_name
, section_name
))
1332 // We found a match for NAME, which means that it should go
1333 // into this output section.
1334 *slot
= &this->output_section_
;
1335 return this->name_
.c_str();
1339 // We don't know about this section name.
1343 // Return whether to place an orphan output section after this
1347 Output_section_definition::place_orphan_here(const Output_section
*os
,
1350 // Check for the simple case first.
1351 if (this->output_section_
!= NULL
1352 && this->output_section_
->type() == os
->type()
1353 && this->output_section_
->flags() == os
->flags())
1359 // Otherwise use some heuristics.
1361 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1364 if (os
->type() == elfcpp::SHT_NOBITS
)
1366 if (this->output_section_
!= NULL
1367 && this->output_section_
->type() == elfcpp::SHT_NOBITS
)
1369 if (this->name_
== ".bss")
1372 else if (os
->type() == elfcpp::SHT_NOTE
)
1374 if (this->output_section_
!= NULL
1375 && this->output_section_
->type() == elfcpp::SHT_NOTE
)
1377 if (this->name_
== ".interp"
1378 || this->name_
.compare(0, 5, ".note") == 0)
1381 else if (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
1383 if (this->output_section_
!= NULL
1384 && (this->output_section_
->type() == elfcpp::SHT_REL
1385 || this->output_section_
->type() == elfcpp::SHT_RELA
))
1387 if (this->name_
.compare(0, 4, ".rel") == 0)
1390 else if (os
->type() == elfcpp::SHT_PROGBITS
1391 && (os
->flags() & elfcpp::SHF_WRITE
) != 0)
1393 if (this->output_section_
!= NULL
1394 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1395 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1397 if (this->name_
== ".data")
1400 else if (os
->type() == elfcpp::SHT_PROGBITS
1401 && (os
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1403 if (this->output_section_
!= NULL
1404 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1405 && (this->output_section_
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1407 if (this->name_
== ".text")
1410 else if (os
->type() == elfcpp::SHT_PROGBITS
)
1412 if (this->output_section_
!= NULL
1413 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1414 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0
1415 && (this->output_section_
->flags() & elfcpp::SHF_EXECINSTR
) == 0)
1417 if (this->name_
== ".rodata")
1424 // Set the section address. Note that the OUTPUT_SECTION_ field will
1425 // be NULL if no input sections were mapped to this output section.
1426 // We still have to adjust dot and process symbol assignments.
1429 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1431 bool* dot_has_value
,
1432 uint64_t* dot_value
)
1436 if (this->address_
!= NULL
)
1438 address
= this->address_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
1439 *dot_value
, &is_absolute
);
1441 gold_error(_("address of section %s is not absolute"),
1442 this->name_
.c_str());
1446 if (!*dot_has_value
)
1447 gold_error(_("no address given for section %s"),
1448 this->name_
.c_str());
1449 address
= *dot_value
;
1453 if (this->align_
== NULL
)
1455 if (this->output_section_
== NULL
)
1458 align
= this->output_section_
->addralign();
1462 align
= this->align_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
1463 *dot_value
, &is_absolute
);
1465 gold_error(_("alignment of section %s is not absolute"),
1466 this->name_
.c_str());
1467 if (this->output_section_
!= NULL
)
1468 this->output_section_
->set_addralign(align
);
1471 address
= align_address(address
, align
);
1473 *dot_value
= address
;
1474 *dot_has_value
= true;
1476 // The address of non-SHF_ALLOC sections is forced to zero,
1477 // regardless of what the linker script wants.
1478 if (this->output_section_
!= NULL
1479 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1480 this->output_section_
->set_address(address
);
1482 if (this->load_address_
!= NULL
&& this->output_section_
!= NULL
)
1484 uint64_t load_address
=
1485 this->load_address_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
1486 *dot_value
, &is_absolute
);
1488 gold_error(_("load address of section %s is not absolute"),
1489 this->name_
.c_str());
1490 this->output_section_
->set_load_address(load_address
);
1494 if (this->subalign_
== NULL
)
1498 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, *dot_has_value
,
1499 *dot_value
, &is_absolute
);
1501 gold_error(_("subalign of section %s is not absolute"),
1502 this->name_
.c_str());
1506 if (this->fill_
!= NULL
)
1508 // FIXME: The GNU linker supports fill values of arbitrary
1510 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
,
1515 gold_error(_("fill of section %s is not absolute"),
1516 this->name_
.c_str());
1517 unsigned char fill_buff
[4];
1518 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1519 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1522 Input_section_list input_sections
;
1523 if (this->output_section_
!= NULL
)
1525 // Get the list of input sections attached to this output
1526 // section. This will leave the output section with only
1527 // Output_section_data entries.
1528 address
+= this->output_section_
->get_input_sections(address
,
1531 *dot_value
= address
;
1534 for (Output_section_elements::iterator p
= this->elements_
.begin();
1535 p
!= this->elements_
.end();
1537 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
1538 subalign
, dot_value
, &fill
, &input_sections
);
1540 gold_assert(input_sections
.empty());
1543 // Print for debugging.
1546 Output_section_definition::print(FILE* f
) const
1548 fprintf(f
, " %s ", this->name_
.c_str());
1550 if (this->address_
!= NULL
)
1552 this->address_
->print(f
);
1558 if (this->load_address_
!= NULL
)
1561 this->load_address_
->print(f
);
1565 if (this->align_
!= NULL
)
1567 fprintf(f
, "ALIGN(");
1568 this->align_
->print(f
);
1572 if (this->subalign_
!= NULL
)
1574 fprintf(f
, "SUBALIGN(");
1575 this->subalign_
->print(f
);
1581 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1582 p
!= this->elements_
.end();
1588 if (this->fill_
!= NULL
)
1591 this->fill_
->print(f
);
1597 // An output section created to hold orphaned input sections. These
1598 // do not actually appear in linker scripts. However, for convenience
1599 // when setting the output section addresses, we put a marker to these
1600 // sections in the appropriate place in the list of SECTIONS elements.
1602 class Orphan_output_section
: public Sections_element
1605 Orphan_output_section(Output_section
* os
)
1609 // Return whether to place an orphan section after this one.
1611 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1613 // Set section addresses.
1615 set_section_addresses(Symbol_table
*, Layout
*, bool*, uint64_t*);
1617 // Print for debugging.
1619 print(FILE* f
) const
1621 fprintf(f
, " marker for orphaned output section %s\n",
1626 Output_section
* os_
;
1629 // Whether to place another orphan section after this one.
1632 Orphan_output_section::place_orphan_here(const Output_section
* os
,
1635 if (this->os_
->type() == os
->type()
1636 && this->os_
->flags() == os
->flags())
1644 // Set section addresses.
1647 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
1648 bool* dot_has_value
,
1649 uint64_t* dot_value
)
1651 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
1653 if (!*dot_has_value
)
1654 gold_error(_("no address for orphan section %s"), this->os_
->name());
1656 uint64_t address
= *dot_value
;
1657 address
= align_address(address
, this->os_
->addralign());
1659 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1660 this->os_
->set_address(address
);
1662 Input_section_list input_sections
;
1663 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
1665 for (Input_section_list::iterator p
= input_sections
.begin();
1666 p
!= input_sections
.end();
1672 // We know what are single-threaded, so it is OK to lock the
1675 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1676 Task_lock_obj
<Object
> tl(task
, p
->first
);
1677 addralign
= p
->first
->section_addralign(p
->second
);
1678 size
= p
->first
->section_size(p
->second
);
1681 address
= align_address(address
, addralign
);
1682 this->os_
->add_input_section_for_script(p
->first
, p
->second
, size
, 0);
1686 *dot_value
= address
;
1689 // Class Script_sections.
1691 Script_sections::Script_sections()
1692 : saw_sections_clause_(false),
1693 in_sections_clause_(false),
1694 sections_elements_(NULL
),
1695 output_section_(NULL
)
1699 // Start a SECTIONS clause.
1702 Script_sections::start_sections()
1704 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
1705 this->saw_sections_clause_
= true;
1706 this->in_sections_clause_
= true;
1707 if (this->sections_elements_
== NULL
)
1708 this->sections_elements_
= new Sections_elements
;
1711 // Finish a SECTIONS clause.
1714 Script_sections::finish_sections()
1716 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
1717 this->in_sections_clause_
= false;
1720 // Add a symbol to be defined.
1723 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
1724 Expression
* val
, bool provide
,
1727 if (this->output_section_
!= NULL
)
1728 this->output_section_
->add_symbol_assignment(name
, length
, val
,
1732 Sections_element
* p
= new Sections_element_assignment(name
, length
,
1735 this->sections_elements_
->push_back(p
);
1739 // Add an assignment to the special dot symbol.
1742 Script_sections::add_dot_assignment(Expression
* val
)
1744 if (this->output_section_
!= NULL
)
1745 this->output_section_
->add_dot_assignment(val
);
1748 Sections_element
* p
= new Sections_element_dot_assignment(val
);
1749 this->sections_elements_
->push_back(p
);
1753 // Add an assertion.
1756 Script_sections::add_assertion(Expression
* check
, const char* message
,
1759 if (this->output_section_
!= NULL
)
1760 this->output_section_
->add_assertion(check
, message
, messagelen
);
1763 Sections_element
* p
= new Sections_element_assertion(check
, message
,
1765 this->sections_elements_
->push_back(p
);
1769 // Start processing entries for an output section.
1772 Script_sections::start_output_section(
1775 const Parser_output_section_header
*header
)
1777 Output_section_definition
* posd
= new Output_section_definition(name
,
1780 this->sections_elements_
->push_back(posd
);
1781 gold_assert(this->output_section_
== NULL
);
1782 this->output_section_
= posd
;
1785 // Stop processing entries for an output section.
1788 Script_sections::finish_output_section(
1789 const Parser_output_section_trailer
* trailer
)
1791 gold_assert(this->output_section_
!= NULL
);
1792 this->output_section_
->finish(trailer
);
1793 this->output_section_
= NULL
;
1796 // Add a data item to the current output section.
1799 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
1801 gold_assert(this->output_section_
!= NULL
);
1802 this->output_section_
->add_data(size
, is_signed
, val
);
1805 // Add a fill value setting to the current output section.
1808 Script_sections::add_fill(Expression
* val
)
1810 gold_assert(this->output_section_
!= NULL
);
1811 this->output_section_
->add_fill(val
);
1814 // Add an input section specification to the current output section.
1817 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
1819 gold_assert(this->output_section_
!= NULL
);
1820 this->output_section_
->add_input_section(spec
, keep
);
1823 // Add any symbols we are defining to the symbol table.
1826 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
1828 if (!this->saw_sections_clause_
)
1830 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
1831 p
!= this->sections_elements_
->end();
1833 (*p
)->add_symbols_to_table(symtab
);
1836 // Finalize symbols and check assertions.
1839 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
1841 if (!this->saw_sections_clause_
)
1843 bool dot_has_value
= false;
1844 uint64_t dot_value
= 0;
1845 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
1846 p
!= this->sections_elements_
->end();
1848 (*p
)->finalize_symbols(symtab
, layout
, &dot_has_value
, &dot_value
);
1851 // Return the name of the output section to use for an input file name
1852 // and section name.
1855 Script_sections::output_section_name(const char* file_name
,
1856 const char* section_name
,
1857 Output_section
*** output_section_slot
)
1859 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
1860 p
!= this->sections_elements_
->end();
1863 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
1864 output_section_slot
);
1868 // The special name /DISCARD/ means that the input section
1869 // should be discarded.
1870 if (strcmp(ret
, "/DISCARD/") == 0)
1872 *output_section_slot
= NULL
;
1879 // If we couldn't find a mapping for the name, the output section
1880 // gets the name of the input section.
1882 *output_section_slot
= NULL
;
1884 return section_name
;
1887 // Place a marker for an orphan output section into the SECTIONS
1891 Script_sections::place_orphan(Output_section
* os
)
1893 // Look for an output section definition which matches the output
1894 // section. Put a marker after that section.
1895 Sections_elements::iterator place
= this->sections_elements_
->end();
1896 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
1897 p
!= this->sections_elements_
->end();
1901 if ((*p
)->place_orphan_here(os
, &exact
))
1909 // The insert function puts the new element before the iterator.
1910 if (place
!= this->sections_elements_
->end())
1913 this->sections_elements_
->insert(place
, new Orphan_output_section(os
));
1916 // Set the addresses of all the output sections. Walk through all the
1917 // elements, tracking the dot symbol. Apply assignments which set
1918 // absolute symbol values, in case they are used when setting dot.
1919 // Fill in data statement values. As we find output sections, set the
1920 // address, set the address of all associated input sections, and
1921 // update dot. Return the segment which should hold the file header
1922 // and segment headers, if any.
1925 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
1927 gold_assert(this->saw_sections_clause_
);
1929 bool dot_has_value
= false;
1930 uint64_t dot_value
= 0;
1931 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
1932 p
!= this->sections_elements_
->end();
1934 (*p
)->set_section_addresses(symtab
, layout
, &dot_has_value
, &dot_value
);
1936 return this->create_segments(layout
);
1939 // Sort the sections in order to put them into segments.
1941 class Sort_output_sections
1945 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
1949 Sort_output_sections::operator()(const Output_section
* os1
,
1950 const Output_section
* os2
) const
1952 // Sort first by the load address.
1953 uint64_t lma1
= (os1
->has_load_address()
1954 ? os1
->load_address()
1956 uint64_t lma2
= (os2
->has_load_address()
1957 ? os2
->load_address()
1962 // Then sort by the virtual address.
1963 if (os1
->address() != os2
->address())
1964 return os1
->address() < os2
->address();
1966 // Sort TLS sections to the end.
1967 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
1968 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
1972 // Sort PROGBITS before NOBITS.
1973 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
1975 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
1978 // Otherwise we don't care.
1982 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
1983 // We treat a section with the SHF_TLS flag set as taking up space
1984 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
1985 // space for them in the file.
1988 Script_sections::is_bss_section(const Output_section
* os
)
1990 return (os
->type() == elfcpp::SHT_NOBITS
1991 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
1994 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
1995 // the segment which should hold the file header and segment headers,
1999 Script_sections::create_segments(Layout
* layout
)
2001 gold_assert(this->saw_sections_clause_
);
2003 if (parameters
->output_is_object())
2006 Layout::Section_list sections
;
2007 layout
->get_allocated_sections(§ions
);
2009 // Sort the sections by address.
2010 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2012 this->create_note_and_tls_segments(layout
, §ions
);
2014 // Walk through the sections adding them to PT_LOAD segments.
2015 const uint64_t abi_pagesize
= parameters
->target()->abi_pagesize();
2016 Output_segment
* first_seg
= NULL
;
2017 Output_segment
* current_seg
= NULL
;
2018 bool is_current_seg_readonly
= true;
2019 Layout::Section_list::iterator plast
= sections
.end();
2020 uint64_t last_vma
= 0;
2021 uint64_t last_lma
= 0;
2022 uint64_t last_size
= 0;
2023 for (Layout::Section_list::iterator p
= sections
.begin();
2024 p
!= sections
.end();
2027 const uint64_t vma
= (*p
)->address();
2028 const uint64_t lma
= ((*p
)->has_load_address()
2029 ? (*p
)->load_address()
2031 const uint64_t size
= (*p
)->current_data_size();
2033 bool need_new_segment
;
2034 if (current_seg
== NULL
)
2035 need_new_segment
= true;
2036 else if (lma
- vma
!= last_lma
- last_vma
)
2038 // This section has a different LMA relationship than the
2039 // last one; we need a new segment.
2040 need_new_segment
= true;
2042 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2043 < align_address(lma
, abi_pagesize
))
2045 // Putting this section in the segment would require
2047 need_new_segment
= true;
2049 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2051 // A non-BSS section can not follow a BSS section in the
2053 need_new_segment
= true;
2055 else if (is_current_seg_readonly
2056 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2058 // Don't put a writable section in the same segment as a
2059 // non-writable section.
2060 need_new_segment
= true;
2064 // Otherwise, reuse the existing segment.
2065 need_new_segment
= false;
2068 elfcpp::Elf_Word seg_flags
=
2069 Layout::section_flags_to_segment((*p
)->flags());
2071 if (need_new_segment
)
2073 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2075 current_seg
->set_addresses(vma
, lma
);
2076 if (first_seg
== NULL
)
2077 first_seg
= current_seg
;
2078 is_current_seg_readonly
= true;
2081 current_seg
->add_output_section(*p
, seg_flags
);
2083 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2084 is_current_seg_readonly
= false;
2092 // An ELF program should work even if the program headers are not in
2093 // a PT_LOAD segment. However, it appears that the Linux kernel
2094 // does not set the AT_PHDR auxiliary entry in that case. It sets
2095 // the load address to p_vaddr - p_offset of the first PT_LOAD
2096 // segment. It then sets AT_PHDR to the load address plus the
2097 // offset to the program headers, e_phoff in the file header. This
2098 // fails when the program headers appear in the file before the
2099 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2100 // segment to hold the file header and the program headers. This is
2101 // effectively what the GNU linker does, and it is slightly more
2102 // efficient in any case. We try to use the first PT_LOAD segment
2103 // if we can, otherwise we make a new one.
2105 size_t segment_count
= layout
->segment_count();
2106 size_t file_header_size
;
2107 size_t segment_headers_size
;
2108 if (parameters
->get_size() == 32)
2110 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2111 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2113 else if (parameters
->get_size() == 64)
2115 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2116 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2121 if (first_seg
!= NULL
2122 && ((first_seg
->paddr() & (abi_pagesize
- 1))
2123 >= file_header_size
+ segment_headers_size
))
2126 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2128 if (first_seg
== NULL
)
2129 load_seg
->set_addresses(0, 0);
2132 uint64_t vma
= first_seg
->vaddr();
2133 uint64_t lma
= first_seg
->paddr();
2135 if (lma
>= file_header_size
+ segment_headers_size
2136 && lma
>= abi_pagesize
)
2138 // We want a segment with the same relationship between VMA
2139 // and LMA, but with enough room for the headers.
2140 uint64_t size_for_page
= align_address((file_header_size
2141 + segment_headers_size
),
2143 load_seg
->set_addresses(vma
- size_for_page
, lma
- size_for_page
);
2147 // We could handle this case by create the file header
2148 // outside of any PT_LOAD segment, and creating a new
2149 // PT_LOAD segment after the others to hold the segment
2151 gold_error(_("sections loaded on first page without room for "
2152 "file and program headers are not supported"));
2159 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2160 // segment if there are any SHT_TLS sections.
2163 Script_sections::create_note_and_tls_segments(
2165 const Layout::Section_list
* sections
)
2167 bool saw_tls
= false;
2168 for (Layout::Section_list::const_iterator p
= sections
->begin();
2169 p
!= sections
->end();
2172 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2174 elfcpp::Elf_Word seg_flags
=
2175 Layout::section_flags_to_segment((*p
)->flags());
2176 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
2178 oseg
->add_output_section(*p
, seg_flags
);
2180 // Incorporate any subsequent SHT_NOTE sections, in the
2181 // hopes that the script is sensible.
2182 Layout::Section_list::const_iterator pnext
= p
+ 1;
2183 while (pnext
!= sections
->end()
2184 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
2186 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2187 oseg
->add_output_section(*pnext
, seg_flags
);
2193 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2196 gold_error(_("TLS sections are not adjacent"));
2198 elfcpp::Elf_Word seg_flags
=
2199 Layout::section_flags_to_segment((*p
)->flags());
2200 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
2202 oseg
->add_output_section(*p
, seg_flags
);
2204 Layout::Section_list::const_iterator pnext
= p
+ 1;
2205 while (pnext
!= sections
->end()
2206 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
2208 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2209 oseg
->add_output_section(*pnext
, seg_flags
);
2219 // Print the SECTIONS clause to F for debugging.
2222 Script_sections::print(FILE* f
) const
2224 if (!this->saw_sections_clause_
)
2227 fprintf(f
, "SECTIONS {\n");
2229 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2230 p
!= this->sections_elements_
->end();
2237 } // End namespace gold.