1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
33 #include "parameters.h"
39 #include "script-sections.h"
41 // Support for the SECTIONS clause in linker scripts.
46 // An element in a SECTIONS clause.
48 class Sections_element
54 virtual ~Sections_element()
57 // Add any symbol being defined to the symbol table.
59 add_symbols_to_table(Symbol_table
*)
62 // Finalize symbols and check assertions.
64 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*)
67 // Return the output section name to use for an input file name and
68 // section name. This only real implementation is in
69 // Output_section_definition.
71 output_section_name(const char*, const char*, Output_section
***)
74 // Return whether to place an orphan output section after this
77 place_orphan_here(const Output_section
*, bool*) const
80 // Set section addresses. This includes applying assignments if the
81 // the expression is an absolute value.
83 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*)
86 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
87 // this section is constrained, and the input sections do not match,
88 // return the constraint, and set *POSD.
89 virtual Section_constraint
90 check_constraint(Output_section_definition
**)
91 { return CONSTRAINT_NONE
; }
93 // See if this is the alternate output section for a constrained
94 // output section. If it is, transfer the Output_section and return
95 // true. Otherwise return false.
97 alternate_constraint(Output_section_definition
*, Section_constraint
)
100 // Get the list of segments to use for an allocated section when
101 // using a PHDRS clause. If this is an allocated section, return
102 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
103 // which it should be attached. If the PHDRS were not specified,
104 // don't change *PHDRS_LIST.
105 virtual Output_section
*
106 allocate_to_segment(String_list
**)
109 // Print the element for debugging purposes.
111 print(FILE* f
) const = 0;
114 // An assignment in a SECTIONS clause outside of an output section.
116 class Sections_element_assignment
: public Sections_element
119 Sections_element_assignment(const char* name
, size_t namelen
,
120 Expression
* val
, bool provide
, bool hidden
)
121 : assignment_(name
, namelen
, val
, provide
, hidden
)
124 // Add the symbol to the symbol table.
126 add_symbols_to_table(Symbol_table
* symtab
)
127 { this->assignment_
.add_to_table(symtab
); }
129 // Finalize the symbol.
131 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
134 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
137 // Set the section address. There is no section here, but if the
138 // value is absolute, we set the symbol. This permits us to use
139 // absolute symbols when setting dot.
141 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
142 uint64_t* dot_value
, uint64_t*)
144 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
147 // Print for debugging.
152 this->assignment_
.print(f
);
156 Symbol_assignment assignment_
;
159 // An assignment to the dot symbol in a SECTIONS clause outside of an
162 class Sections_element_dot_assignment
: public Sections_element
165 Sections_element_dot_assignment(Expression
* val
)
169 // Finalize the symbol.
171 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
174 // We ignore the section of the result because outside of an
175 // output section definition the dot symbol is always considered
177 Output_section
* dummy
;
178 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_value
,
182 // Update the dot symbol while setting section addresses.
184 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
185 uint64_t* dot_value
, uint64_t* load_address
)
187 Output_section
* dummy
;
188 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_value
,
190 *load_address
= *dot_value
;
193 // Print for debugging.
198 this->val_
->print(f
);
206 // An assertion in a SECTIONS clause outside of an output section.
208 class Sections_element_assertion
: public Sections_element
211 Sections_element_assertion(Expression
* check
, const char* message
,
213 : assertion_(check
, message
, messagelen
)
216 // Check the assertion.
218 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
219 { this->assertion_
.check(symtab
, layout
); }
221 // Print for debugging.
226 this->assertion_
.print(f
);
230 Script_assertion assertion_
;
233 // An element in an output section in a SECTIONS clause.
235 class Output_section_element
238 // A list of input sections.
239 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
241 Output_section_element()
244 virtual ~Output_section_element()
247 // Add any symbol being defined to the symbol table.
249 add_symbols_to_table(Symbol_table
*)
252 // Finalize symbols and check assertions.
254 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
257 // Return whether this element matches FILE_NAME and SECTION_NAME.
258 // The only real implementation is in Output_section_element_input.
260 match_name(const char*, const char*) const
263 // Set section addresses. This includes applying assignments if the
264 // the expression is an absolute value.
266 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
267 uint64_t*, Output_section
**, std::string
*,
271 // Print the element for debugging purposes.
273 print(FILE* f
) const = 0;
276 // Return a fill string that is LENGTH bytes long, filling it with
279 get_fill_string(const std::string
* fill
, section_size_type length
) const;
283 Output_section_element::get_fill_string(const std::string
* fill
,
284 section_size_type length
) const
286 std::string this_fill
;
287 this_fill
.reserve(length
);
288 while (this_fill
.length() + fill
->length() <= length
)
290 if (this_fill
.length() < length
)
291 this_fill
.append(*fill
, 0, length
- this_fill
.length());
295 // A symbol assignment in an output section.
297 class Output_section_element_assignment
: public Output_section_element
300 Output_section_element_assignment(const char* name
, size_t namelen
,
301 Expression
* val
, bool provide
,
303 : assignment_(name
, namelen
, val
, provide
, hidden
)
306 // Add the symbol to the symbol table.
308 add_symbols_to_table(Symbol_table
* symtab
)
309 { this->assignment_
.add_to_table(symtab
); }
311 // Finalize the symbol.
313 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
314 uint64_t* dot_value
, Output_section
** dot_section
)
316 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
320 // Set the section address. There is no section here, but if the
321 // value is absolute, we set the symbol. This permits us to use
322 // absolute symbols when setting dot.
324 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
325 uint64_t, uint64_t* dot_value
, Output_section
**,
326 std::string
*, Input_section_list
*)
328 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
331 // Print for debugging.
336 this->assignment_
.print(f
);
340 Symbol_assignment assignment_
;
343 // An assignment to the dot symbol in an output section.
345 class Output_section_element_dot_assignment
: public Output_section_element
348 Output_section_element_dot_assignment(Expression
* val
)
352 // Finalize the symbol.
354 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
355 uint64_t* dot_value
, Output_section
** dot_section
)
357 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, *dot_value
,
358 *dot_section
, dot_section
);
361 // Update the dot symbol while setting section addresses.
363 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
364 uint64_t, uint64_t* dot_value
, Output_section
**,
365 std::string
*, Input_section_list
*);
367 // Print for debugging.
372 this->val_
->print(f
);
380 // Update the dot symbol while setting section addresses.
383 Output_section_element_dot_assignment::set_section_addresses(
384 Symbol_table
* symtab
,
386 Output_section
* output_section
,
389 Output_section
** dot_section
,
393 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, *dot_value
,
394 *dot_section
, dot_section
);
395 if (next_dot
< *dot_value
)
396 gold_error(_("dot may not move backward"));
397 if (next_dot
> *dot_value
&& output_section
!= NULL
)
399 section_size_type length
= convert_to_section_size_type(next_dot
401 Output_section_data
* posd
;
403 posd
= new Output_data_fixed_space(length
, 0);
406 std::string this_fill
= this->get_fill_string(fill
, length
);
407 posd
= new Output_data_const(this_fill
, 0);
409 output_section
->add_output_section_data(posd
);
411 *dot_value
= next_dot
;
414 // An assertion in an output section.
416 class Output_section_element_assertion
: public Output_section_element
419 Output_section_element_assertion(Expression
* check
, const char* message
,
421 : assertion_(check
, message
, messagelen
)
428 this->assertion_
.print(f
);
432 Script_assertion assertion_
;
435 // We use a special instance of Output_section_data to handle BYTE,
436 // SHORT, etc. This permits forward references to symbols in the
439 class Output_data_expression
: public Output_section_data
442 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
443 const Symbol_table
* symtab
, const Layout
* layout
,
444 uint64_t dot_value
, Output_section
* dot_section
)
445 : Output_section_data(size
, 0),
446 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
447 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
451 // Write the data to the output file.
453 do_write(Output_file
*);
455 // Write the data to a buffer.
457 do_write_to_buffer(unsigned char*);
460 template<bool big_endian
>
462 endian_write_to_buffer(uint64_t, unsigned char*);
466 const Symbol_table
* symtab_
;
467 const Layout
* layout_
;
469 Output_section
* dot_section_
;
472 // Write the data element to the output file.
475 Output_data_expression::do_write(Output_file
* of
)
477 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
478 this->write_to_buffer(view
);
479 of
->write_output_view(this->offset(), this->data_size(), view
);
482 // Write the data element to a buffer.
485 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
487 Output_section
* dummy
;
488 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
490 this->dot_section_
, &dummy
);
492 if (parameters
->is_big_endian())
493 this->endian_write_to_buffer
<true>(val
, buf
);
495 this->endian_write_to_buffer
<false>(val
, buf
);
498 template<bool big_endian
>
500 Output_data_expression::endian_write_to_buffer(uint64_t val
,
503 switch (this->data_size())
506 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
509 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
512 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
515 if (parameters
->get_size() == 32)
518 if (this->is_signed_
&& (val
& 0x80000000) != 0)
519 val
|= 0xffffffff00000000LL
;
521 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
528 // A data item in an output section.
530 class Output_section_element_data
: public Output_section_element
533 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
534 : size_(size
), is_signed_(is_signed
), val_(val
)
537 // Finalize symbols--we just need to update dot.
539 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
541 { *dot_value
+= this->size_
; }
543 // Store the value in the section.
545 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
546 uint64_t* dot_value
, Output_section
**, std::string
*,
547 Input_section_list
*);
549 // Print for debugging.
554 // The size in bytes.
556 // Whether the value is signed.
562 // Store the value in the section.
565 Output_section_element_data::set_section_addresses(
566 Symbol_table
* symtab
,
571 Output_section
** dot_section
,
575 gold_assert(os
!= NULL
);
576 os
->add_output_section_data(new Output_data_expression(this->size_
,
583 *dot_value
+= this->size_
;
586 // Print for debugging.
589 Output_section_element_data::print(FILE* f
) const
604 if (this->is_signed_
)
612 fprintf(f
, " %s(", s
);
613 this->val_
->print(f
);
617 // A fill value setting in an output section.
619 class Output_section_element_fill
: public Output_section_element
622 Output_section_element_fill(Expression
* val
)
626 // Update the fill value while setting section addresses.
628 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
629 uint64_t, uint64_t* dot_value
,
630 Output_section
** dot_section
,
631 std::string
* fill
, Input_section_list
*)
633 Output_section
* fill_section
;
634 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
,
635 *dot_value
, *dot_section
,
637 if (fill_section
!= NULL
)
638 gold_warning(_("fill value is not absolute"));
639 // FIXME: The GNU linker supports fill values of arbitrary length.
640 unsigned char fill_buff
[4];
641 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
642 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
645 // Print for debugging.
649 fprintf(f
, " FILL(");
650 this->val_
->print(f
);
655 // The new fill value.
659 // Return whether STRING contains a wildcard character. This is used
660 // to speed up matching.
663 is_wildcard_string(const std::string
& s
)
665 return strpbrk(s
.c_str(), "?*[") != NULL
;
668 // An input section specification in an output section
670 class Output_section_element_input
: public Output_section_element
673 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
675 // Finalize symbols--just update the value of the dot symbol.
677 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
678 Output_section
** dot_section
)
680 *dot_value
= this->final_dot_value_
;
681 *dot_section
= this->final_dot_section_
;
684 // See whether we match FILE_NAME and SECTION_NAME as an input
687 match_name(const char* file_name
, const char* section_name
) const;
689 // Set the section address.
691 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
692 uint64_t subalign
, uint64_t* dot_value
,
693 Output_section
**, std::string
* fill
,
694 Input_section_list
*);
696 // Print for debugging.
698 print(FILE* f
) const;
701 // An input section pattern.
702 struct Input_section_pattern
705 bool pattern_is_wildcard
;
708 Input_section_pattern(const char* patterna
, size_t patternlena
,
710 : pattern(patterna
, patternlena
),
711 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
716 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
718 // Filename_exclusions is a pair of filename pattern and a bool
719 // indicating whether the filename is a wildcard.
720 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
722 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
723 // indicates whether this is a wildcard pattern.
725 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
727 return (is_wildcard_pattern
728 ? fnmatch(pattern
, string
, 0) == 0
729 : strcmp(string
, pattern
) == 0);
732 // See if we match a file name.
734 match_file_name(const char* file_name
) const;
736 // The file name pattern. If this is the empty string, we match all
738 std::string filename_pattern_
;
739 // Whether the file name pattern is a wildcard.
740 bool filename_is_wildcard_
;
741 // How the file names should be sorted. This may only be
742 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
743 Sort_wildcard filename_sort_
;
744 // The list of file names to exclude.
745 Filename_exclusions filename_exclusions_
;
746 // The list of input section patterns.
747 Input_section_patterns input_section_patterns_
;
748 // Whether to keep this section when garbage collecting.
750 // The value of dot after including all matching sections.
751 uint64_t final_dot_value_
;
752 // The section where dot is defined after including all matching
754 Output_section
* final_dot_section_
;
757 // Construct Output_section_element_input. The parser records strings
758 // as pointers into a copy of the script file, which will go away when
759 // parsing is complete. We make sure they are in std::string objects.
761 Output_section_element_input::Output_section_element_input(
762 const Input_section_spec
* spec
,
764 : filename_pattern_(),
765 filename_is_wildcard_(false),
766 filename_sort_(spec
->file
.sort
),
767 filename_exclusions_(),
768 input_section_patterns_(),
771 final_dot_section_(NULL
)
773 // The filename pattern "*" is common, and matches all files. Turn
774 // it into the empty string.
775 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
776 this->filename_pattern_
.assign(spec
->file
.name
.value
,
777 spec
->file
.name
.length
);
778 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
780 if (spec
->input_sections
.exclude
!= NULL
)
782 for (String_list::const_iterator p
=
783 spec
->input_sections
.exclude
->begin();
784 p
!= spec
->input_sections
.exclude
->end();
787 bool is_wildcard
= is_wildcard_string(*p
);
788 this->filename_exclusions_
.push_back(std::make_pair(*p
,
793 if (spec
->input_sections
.sections
!= NULL
)
795 Input_section_patterns
& isp(this->input_section_patterns_
);
796 for (String_sort_list::const_iterator p
=
797 spec
->input_sections
.sections
->begin();
798 p
!= spec
->input_sections
.sections
->end();
800 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
805 // See whether we match FILE_NAME.
808 Output_section_element_input::match_file_name(const char* file_name
) const
810 if (!this->filename_pattern_
.empty())
812 // If we were called with no filename, we refuse to match a
813 // pattern which requires a file name.
814 if (file_name
== NULL
)
817 if (!match(file_name
, this->filename_pattern_
.c_str(),
818 this->filename_is_wildcard_
))
822 if (file_name
!= NULL
)
824 // Now we have to see whether FILE_NAME matches one of the
825 // exclusion patterns, if any.
826 for (Filename_exclusions::const_iterator p
=
827 this->filename_exclusions_
.begin();
828 p
!= this->filename_exclusions_
.end();
831 if (match(file_name
, p
->first
.c_str(), p
->second
))
839 // See whether we match FILE_NAME and SECTION_NAME.
842 Output_section_element_input::match_name(const char* file_name
,
843 const char* section_name
) const
845 if (!this->match_file_name(file_name
))
848 // If there are no section name patterns, then we match.
849 if (this->input_section_patterns_
.empty())
852 // See whether we match the section name patterns.
853 for (Input_section_patterns::const_iterator p
=
854 this->input_section_patterns_
.begin();
855 p
!= this->input_section_patterns_
.end();
858 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
862 // We didn't match any section names, so we didn't match.
866 // Information we use to sort the input sections.
868 struct Input_section_info
872 std::string section_name
;
877 // A class to sort the input sections.
879 class Input_section_sorter
882 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
883 : filename_sort_(filename_sort
), section_sort_(section_sort
)
887 operator()(const Input_section_info
&, const Input_section_info
&) const;
890 Sort_wildcard filename_sort_
;
891 Sort_wildcard section_sort_
;
895 Input_section_sorter::operator()(const Input_section_info
& isi1
,
896 const Input_section_info
& isi2
) const
898 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
899 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
900 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
901 && isi1
.addralign
== isi2
.addralign
))
903 if (isi1
.section_name
!= isi2
.section_name
)
904 return isi1
.section_name
< isi2
.section_name
;
906 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
907 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
908 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
910 if (isi1
.addralign
!= isi2
.addralign
)
911 return isi1
.addralign
< isi2
.addralign
;
913 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
915 if (isi1
.relobj
->name() != isi2
.relobj
->name())
916 return isi1
.relobj
->name() < isi2
.relobj
->name();
919 // Otherwise we leave them in the same order.
923 // Set the section address. Look in INPUT_SECTIONS for sections which
924 // match this spec, sort them as specified, and add them to the output
928 Output_section_element_input::set_section_addresses(
931 Output_section
* output_section
,
934 Output_section
** dot_section
,
936 Input_section_list
* input_sections
)
938 // We build a list of sections which match each
939 // Input_section_pattern.
941 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
942 size_t input_pattern_count
= this->input_section_patterns_
.size();
943 if (input_pattern_count
== 0)
944 input_pattern_count
= 1;
945 Matching_sections
matching_sections(input_pattern_count
);
947 // Look through the list of sections for this output section. Add
948 // each one which matches to one of the elements of
949 // MATCHING_SECTIONS.
951 Input_section_list::iterator p
= input_sections
->begin();
952 while (p
!= input_sections
->end())
954 // Calling section_name and section_addralign is not very
956 Input_section_info isi
;
957 isi
.relobj
= p
->first
;
958 isi
.shndx
= p
->second
;
960 // Lock the object so that we can get information about the
961 // section. This is OK since we know we are single-threaded
964 const Task
* task
= reinterpret_cast<const Task
*>(-1);
965 Task_lock_obj
<Object
> tl(task
, p
->first
);
967 isi
.section_name
= p
->first
->section_name(p
->second
);
968 isi
.size
= p
->first
->section_size(p
->second
);
969 isi
.addralign
= p
->first
->section_addralign(p
->second
);
972 if (!this->match_file_name(isi
.relobj
->name().c_str()))
974 else if (this->input_section_patterns_
.empty())
976 matching_sections
[0].push_back(isi
);
977 p
= input_sections
->erase(p
);
982 for (i
= 0; i
< input_pattern_count
; ++i
)
984 const Input_section_pattern
&
985 isp(this->input_section_patterns_
[i
]);
986 if (match(isi
.section_name
.c_str(), isp
.pattern
.c_str(),
987 isp
.pattern_is_wildcard
))
991 if (i
>= this->input_section_patterns_
.size())
995 matching_sections
[i
].push_back(isi
);
996 p
= input_sections
->erase(p
);
1001 // Look through MATCHING_SECTIONS. Sort each one as specified,
1002 // using a stable sort so that we get the default order when
1003 // sections are otherwise equal. Add each input section to the
1006 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1008 if (matching_sections
[i
].empty())
1011 gold_assert(output_section
!= NULL
);
1013 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1014 if (isp
.sort
!= SORT_WILDCARD_NONE
1015 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1016 std::stable_sort(matching_sections
[i
].begin(),
1017 matching_sections
[i
].end(),
1018 Input_section_sorter(this->filename_sort_
,
1021 for (std::vector
<Input_section_info
>::const_iterator p
=
1022 matching_sections
[i
].begin();
1023 p
!= matching_sections
[i
].end();
1026 uint64_t this_subalign
= p
->addralign
;
1027 if (this_subalign
< subalign
)
1028 this_subalign
= subalign
;
1030 uint64_t address
= align_address(*dot_value
, this_subalign
);
1032 if (address
> *dot_value
&& !fill
->empty())
1034 section_size_type length
=
1035 convert_to_section_size_type(address
- *dot_value
);
1036 std::string this_fill
= this->get_fill_string(fill
, length
);
1037 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1038 output_section
->add_output_section_data(posd
);
1041 output_section
->add_input_section_for_script(p
->relobj
,
1046 *dot_value
= address
+ p
->size
;
1050 this->final_dot_value_
= *dot_value
;
1051 this->final_dot_section_
= *dot_section
;
1054 // Print for debugging.
1057 Output_section_element_input::print(FILE* f
) const
1062 fprintf(f
, "KEEP(");
1064 if (!this->filename_pattern_
.empty())
1066 bool need_close_paren
= false;
1067 switch (this->filename_sort_
)
1069 case SORT_WILDCARD_NONE
:
1071 case SORT_WILDCARD_BY_NAME
:
1072 fprintf(f
, "SORT_BY_NAME(");
1073 need_close_paren
= true;
1079 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1081 if (need_close_paren
)
1085 if (!this->input_section_patterns_
.empty()
1086 || !this->filename_exclusions_
.empty())
1090 bool need_space
= false;
1091 if (!this->filename_exclusions_
.empty())
1093 fprintf(f
, "EXCLUDE_FILE(");
1094 bool need_comma
= false;
1095 for (Filename_exclusions::const_iterator p
=
1096 this->filename_exclusions_
.begin();
1097 p
!= this->filename_exclusions_
.end();
1102 fprintf(f
, "%s", p
->first
.c_str());
1109 for (Input_section_patterns::const_iterator p
=
1110 this->input_section_patterns_
.begin();
1111 p
!= this->input_section_patterns_
.end();
1117 int close_parens
= 0;
1120 case SORT_WILDCARD_NONE
:
1122 case SORT_WILDCARD_BY_NAME
:
1123 fprintf(f
, "SORT_BY_NAME(");
1126 case SORT_WILDCARD_BY_ALIGNMENT
:
1127 fprintf(f
, "SORT_BY_ALIGNMENT(");
1130 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1131 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1134 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1135 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1142 fprintf(f
, "%s", p
->pattern
.c_str());
1144 for (int i
= 0; i
< close_parens
; ++i
)
1159 // An output section.
1161 class Output_section_definition
: public Sections_element
1164 typedef Output_section_element::Input_section_list Input_section_list
;
1166 Output_section_definition(const char* name
, size_t namelen
,
1167 const Parser_output_section_header
* header
);
1169 // Finish the output section with the information in the trailer.
1171 finish(const Parser_output_section_trailer
* trailer
);
1173 // Add a symbol to be defined.
1175 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1176 bool provide
, bool hidden
);
1178 // Add an assignment to the special dot symbol.
1180 add_dot_assignment(Expression
* value
);
1182 // Add an assertion.
1184 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1186 // Add a data item to the current output section.
1188 add_data(int size
, bool is_signed
, Expression
* val
);
1190 // Add a setting for the fill value.
1192 add_fill(Expression
* val
);
1194 // Add an input section specification.
1196 add_input_section(const Input_section_spec
* spec
, bool keep
);
1198 // Add any symbols being defined to the symbol table.
1200 add_symbols_to_table(Symbol_table
* symtab
);
1202 // Finalize symbols and check assertions.
1204 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1206 // Return the output section name to use for an input file name and
1209 output_section_name(const char* file_name
, const char* section_name
,
1212 // Return whether to place an orphan section after this one.
1214 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1216 // Set the section address.
1218 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1219 uint64_t* dot_value
, uint64_t* load_address
);
1221 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1222 // this section is constrained, and the input sections do not match,
1223 // return the constraint, and set *POSD.
1225 check_constraint(Output_section_definition
** posd
);
1227 // See if this is the alternate output section for a constrained
1228 // output section. If it is, transfer the Output_section and return
1229 // true. Otherwise return false.
1231 alternate_constraint(Output_section_definition
*, Section_constraint
);
1233 // Get the list of segments to use for an allocated section when
1234 // using a PHDRS clause. If this is an allocated section, return
1235 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
1236 // which it should be attached. If the PHDRS were not specified,
1237 // don't change *PHDRS_LIST.
1239 allocate_to_segment(String_list
** phdrs_list
);
1241 // Print the contents to the FILE. This is for debugging.
1246 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1248 // The output section name.
1250 // The address. This may be NULL.
1251 Expression
* address_
;
1252 // The load address. This may be NULL.
1253 Expression
* load_address_
;
1254 // The alignment. This may be NULL.
1256 // The input section alignment. This may be NULL.
1257 Expression
* subalign_
;
1258 // The constraint, if any.
1259 Section_constraint constraint_
;
1260 // The fill value. This may be NULL.
1262 // The list of segments this section should go into. This may be
1264 String_list
* phdrs_
;
1265 // The list of elements defining the section.
1266 Output_section_elements elements_
;
1267 // The Output_section created for this definition. This will be
1268 // NULL if none was created.
1269 Output_section
* output_section_
;
1274 Output_section_definition::Output_section_definition(
1277 const Parser_output_section_header
* header
)
1278 : name_(name
, namelen
),
1279 address_(header
->address
),
1280 load_address_(header
->load_address
),
1281 align_(header
->align
),
1282 subalign_(header
->subalign
),
1283 constraint_(header
->constraint
),
1287 output_section_(NULL
)
1291 // Finish an output section.
1294 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1296 this->fill_
= trailer
->fill
;
1297 this->phdrs_
= trailer
->phdrs
;
1300 // Add a symbol to be defined.
1303 Output_section_definition::add_symbol_assignment(const char* name
,
1309 Output_section_element
* p
= new Output_section_element_assignment(name
,
1314 this->elements_
.push_back(p
);
1317 // Add an assignment to the special dot symbol.
1320 Output_section_definition::add_dot_assignment(Expression
* value
)
1322 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1323 this->elements_
.push_back(p
);
1326 // Add an assertion.
1329 Output_section_definition::add_assertion(Expression
* check
,
1330 const char* message
,
1333 Output_section_element
* p
= new Output_section_element_assertion(check
,
1336 this->elements_
.push_back(p
);
1339 // Add a data item to the current output section.
1342 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1344 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1346 this->elements_
.push_back(p
);
1349 // Add a setting for the fill value.
1352 Output_section_definition::add_fill(Expression
* val
)
1354 Output_section_element
* p
= new Output_section_element_fill(val
);
1355 this->elements_
.push_back(p
);
1358 // Add an input section specification.
1361 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1364 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1365 this->elements_
.push_back(p
);
1368 // Add any symbols being defined to the symbol table.
1371 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1373 for (Output_section_elements::iterator p
= this->elements_
.begin();
1374 p
!= this->elements_
.end();
1376 (*p
)->add_symbols_to_table(symtab
);
1379 // Finalize symbols and check assertions.
1382 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1383 const Layout
* layout
,
1384 uint64_t* dot_value
)
1386 if (this->output_section_
!= NULL
)
1387 *dot_value
= this->output_section_
->address();
1390 uint64_t address
= *dot_value
;
1391 if (this->address_
!= NULL
)
1393 Output_section
* dummy
;
1394 address
= this->address_
->eval_with_dot(symtab
, layout
,
1398 if (this->align_
!= NULL
)
1400 Output_section
* dummy
;
1401 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
,
1405 address
= align_address(address
, align
);
1407 *dot_value
= address
;
1410 Output_section
* dot_section
= this->output_section_
;
1411 for (Output_section_elements::iterator p
= this->elements_
.begin();
1412 p
!= this->elements_
.end();
1414 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1417 // Return the output section name to use for an input section name.
1420 Output_section_definition::output_section_name(const char* file_name
,
1421 const char* section_name
,
1422 Output_section
*** slot
)
1424 // Ask each element whether it matches NAME.
1425 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1426 p
!= this->elements_
.end();
1429 if ((*p
)->match_name(file_name
, section_name
))
1431 // We found a match for NAME, which means that it should go
1432 // into this output section.
1433 *slot
= &this->output_section_
;
1434 return this->name_
.c_str();
1438 // We don't know about this section name.
1442 // Return whether to place an orphan output section after this
1446 Output_section_definition::place_orphan_here(const Output_section
*os
,
1449 // Check for the simple case first.
1450 if (this->output_section_
!= NULL
1451 && this->output_section_
->type() == os
->type()
1452 && this->output_section_
->flags() == os
->flags())
1458 // Otherwise use some heuristics.
1460 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1463 if (os
->type() == elfcpp::SHT_NOBITS
)
1465 if (this->name_
== ".bss")
1470 if (this->output_section_
!= NULL
1471 && this->output_section_
->type() == elfcpp::SHT_NOBITS
)
1474 else if (os
->type() == elfcpp::SHT_NOTE
)
1476 if (this->output_section_
!= NULL
1477 && this->output_section_
->type() == elfcpp::SHT_NOTE
)
1482 if (this->name_
.compare(0, 5, ".note") == 0)
1487 if (this->name_
== ".interp")
1489 if (this->output_section_
!= NULL
1490 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1491 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1494 else if (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
1496 if (this->name_
.compare(0, 4, ".rel") == 0)
1501 if (this->output_section_
!= NULL
1502 && (this->output_section_
->type() == elfcpp::SHT_REL
1503 || this->output_section_
->type() == elfcpp::SHT_RELA
))
1508 if (this->output_section_
!= NULL
1509 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1510 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1513 else if (os
->type() == elfcpp::SHT_PROGBITS
1514 && (os
->flags() & elfcpp::SHF_WRITE
) != 0)
1516 if (this->name_
== ".data")
1521 if (this->output_section_
!= NULL
1522 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1523 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1526 else if (os
->type() == elfcpp::SHT_PROGBITS
1527 && (os
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1529 if (this->name_
== ".text")
1534 if (this->output_section_
!= NULL
1535 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1536 && (this->output_section_
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1539 else if (os
->type() == elfcpp::SHT_PROGBITS
1540 || (os
->type() != elfcpp::SHT_PROGBITS
1541 && (os
->flags() & elfcpp::SHF_WRITE
) == 0))
1543 if (this->name_
== ".rodata")
1548 if (this->output_section_
!= NULL
1549 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1550 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1557 // Set the section address. Note that the OUTPUT_SECTION_ field will
1558 // be NULL if no input sections were mapped to this output section.
1559 // We still have to adjust dot and process symbol assignments.
1562 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1564 uint64_t* dot_value
,
1565 uint64_t* load_address
)
1568 if (this->address_
== NULL
)
1569 address
= *dot_value
;
1572 Output_section
* dummy
;
1573 address
= this->address_
->eval_with_dot(symtab
, layout
, *dot_value
,
1578 if (this->align_
== NULL
)
1580 if (this->output_section_
== NULL
)
1583 align
= this->output_section_
->addralign();
1587 Output_section
* align_section
;
1588 align
= this->align_
->eval_with_dot(symtab
, layout
, *dot_value
,
1589 NULL
, &align_section
);
1590 if (align_section
!= NULL
)
1591 gold_warning(_("alignment of section %s is not absolute"),
1592 this->name_
.c_str());
1593 if (this->output_section_
!= NULL
)
1594 this->output_section_
->set_addralign(align
);
1597 address
= align_address(address
, align
);
1599 uint64_t start_address
= address
;
1601 *dot_value
= address
;
1603 // The address of non-SHF_ALLOC sections is forced to zero,
1604 // regardless of what the linker script wants.
1605 if (this->output_section_
!= NULL
1606 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1607 this->output_section_
->set_address(address
);
1609 if (this->load_address_
!= NULL
&& this->output_section_
!= NULL
)
1611 Output_section
* dummy
;
1612 uint64_t load_address
=
1613 this->load_address_
->eval_with_dot(symtab
, layout
, *dot_value
,
1614 this->output_section_
, &dummy
);
1615 this->output_section_
->set_load_address(load_address
);
1619 if (this->subalign_
== NULL
)
1623 Output_section
* subalign_section
;
1624 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, *dot_value
,
1625 NULL
, &subalign_section
);
1626 if (subalign_section
!= NULL
)
1627 gold_warning(_("subalign of section %s is not absolute"),
1628 this->name_
.c_str());
1632 if (this->fill_
!= NULL
)
1634 // FIXME: The GNU linker supports fill values of arbitrary
1636 Output_section
* fill_section
;
1637 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
,
1641 if (fill_section
!= NULL
)
1642 gold_warning(_("fill of section %s is not absolute"),
1643 this->name_
.c_str());
1644 unsigned char fill_buff
[4];
1645 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1646 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1649 Input_section_list input_sections
;
1650 if (this->output_section_
!= NULL
)
1652 // Get the list of input sections attached to this output
1653 // section. This will leave the output section with only
1654 // Output_section_data entries.
1655 address
+= this->output_section_
->get_input_sections(address
,
1658 *dot_value
= address
;
1661 Output_section
* dot_section
= this->output_section_
;
1662 for (Output_section_elements::iterator p
= this->elements_
.begin();
1663 p
!= this->elements_
.end();
1665 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
1666 subalign
, dot_value
, &dot_section
, &fill
,
1669 gold_assert(input_sections
.empty());
1671 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
1672 *load_address
= *dot_value
;
1674 *load_address
= (this->output_section_
->load_address()
1675 + (*dot_value
- start_address
));
1678 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1679 // this section is constrained, and the input sections do not match,
1680 // return the constraint, and set *POSD.
1683 Output_section_definition::check_constraint(Output_section_definition
** posd
)
1685 switch (this->constraint_
)
1687 case CONSTRAINT_NONE
:
1688 return CONSTRAINT_NONE
;
1690 case CONSTRAINT_ONLY_IF_RO
:
1691 if (this->output_section_
!= NULL
1692 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1695 return CONSTRAINT_ONLY_IF_RO
;
1697 return CONSTRAINT_NONE
;
1699 case CONSTRAINT_ONLY_IF_RW
:
1700 if (this->output_section_
!= NULL
1701 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1704 return CONSTRAINT_ONLY_IF_RW
;
1706 return CONSTRAINT_NONE
;
1708 case CONSTRAINT_SPECIAL
:
1709 if (this->output_section_
!= NULL
)
1710 gold_error(_("SPECIAL constraints are not implemented"));
1711 return CONSTRAINT_NONE
;
1718 // See if this is the alternate output section for a constrained
1719 // output section. If it is, transfer the Output_section and return
1720 // true. Otherwise return false.
1723 Output_section_definition::alternate_constraint(
1724 Output_section_definition
* posd
,
1725 Section_constraint constraint
)
1727 if (this->name_
!= posd
->name_
)
1732 case CONSTRAINT_ONLY_IF_RO
:
1733 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
1737 case CONSTRAINT_ONLY_IF_RW
:
1738 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
1746 // We have found the alternate constraint. We just need to move
1747 // over the Output_section. When constraints are used properly,
1748 // THIS should not have an output_section pointer, as all the input
1749 // sections should have matched the other definition.
1751 if (this->output_section_
!= NULL
)
1752 gold_error(_("mismatched definition for constrained sections"));
1754 this->output_section_
= posd
->output_section_
;
1755 posd
->output_section_
= NULL
;
1760 // Get the list of segments to use for an allocated section when using
1761 // a PHDRS clause. If this is an allocated section, return the
1762 // Output_section, and set *PHDRS_LIST to the list of PHDRS to which
1763 // it should be attached. If the PHDRS were not specified, don't
1764 // change *PHDRS_LIST.
1767 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
)
1769 if (this->output_section_
== NULL
)
1771 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
1773 if (this->phdrs_
!= NULL
)
1774 *phdrs_list
= this->phdrs_
;
1775 return this->output_section_
;
1778 // Print for debugging.
1781 Output_section_definition::print(FILE* f
) const
1783 fprintf(f
, " %s ", this->name_
.c_str());
1785 if (this->address_
!= NULL
)
1787 this->address_
->print(f
);
1793 if (this->load_address_
!= NULL
)
1796 this->load_address_
->print(f
);
1800 if (this->align_
!= NULL
)
1802 fprintf(f
, "ALIGN(");
1803 this->align_
->print(f
);
1807 if (this->subalign_
!= NULL
)
1809 fprintf(f
, "SUBALIGN(");
1810 this->subalign_
->print(f
);
1816 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1817 p
!= this->elements_
.end();
1823 if (this->fill_
!= NULL
)
1826 this->fill_
->print(f
);
1829 if (this->phdrs_
!= NULL
)
1831 for (String_list::const_iterator p
= this->phdrs_
->begin();
1832 p
!= this->phdrs_
->end();
1834 fprintf(f
, " :%s", p
->c_str());
1840 // An output section created to hold orphaned input sections. These
1841 // do not actually appear in linker scripts. However, for convenience
1842 // when setting the output section addresses, we put a marker to these
1843 // sections in the appropriate place in the list of SECTIONS elements.
1845 class Orphan_output_section
: public Sections_element
1848 Orphan_output_section(Output_section
* os
)
1852 // Return whether to place an orphan section after this one.
1854 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1856 // Set section addresses.
1858 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
1860 // Get the list of segments to use for an allocated section when
1861 // using a PHDRS clause. If this is an allocated section, return
1862 // the Output_section.
1864 allocate_to_segment(String_list
**);
1866 // Print for debugging.
1868 print(FILE* f
) const
1870 fprintf(f
, " marker for orphaned output section %s\n",
1875 Output_section
* os_
;
1878 // Whether to place another orphan section after this one.
1881 Orphan_output_section::place_orphan_here(const Output_section
* os
,
1884 if (this->os_
->type() == os
->type()
1885 && this->os_
->flags() == os
->flags())
1893 // Set section addresses.
1896 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
1897 uint64_t* dot_value
,
1898 uint64_t* load_address
)
1900 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
1902 bool have_load_address
= *load_address
!= *dot_value
;
1904 uint64_t address
= *dot_value
;
1905 address
= align_address(address
, this->os_
->addralign());
1907 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1909 this->os_
->set_address(address
);
1910 if (have_load_address
)
1911 this->os_
->set_load_address(align_address(*load_address
,
1912 this->os_
->addralign()));
1915 Input_section_list input_sections
;
1916 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
1918 for (Input_section_list::iterator p
= input_sections
.begin();
1919 p
!= input_sections
.end();
1925 // We know what are single-threaded, so it is OK to lock the
1928 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1929 Task_lock_obj
<Object
> tl(task
, p
->first
);
1930 addralign
= p
->first
->section_addralign(p
->second
);
1931 size
= p
->first
->section_size(p
->second
);
1934 address
= align_address(address
, addralign
);
1935 this->os_
->add_input_section_for_script(p
->first
, p
->second
, size
,
1940 if (!have_load_address
)
1941 *load_address
= address
;
1943 *load_address
+= address
- *dot_value
;
1945 *dot_value
= address
;
1948 // Get the list of segments to use for an allocated section when using
1949 // a PHDRS clause. If this is an allocated section, return the
1950 // Output_section. We don't change the list of segments.
1953 Orphan_output_section::allocate_to_segment(String_list
**)
1955 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
1960 // Class Phdrs_element. A program header from a PHDRS clause.
1965 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
1966 bool includes_filehdr
, bool includes_phdrs
,
1967 bool is_flags_valid
, unsigned int flags
,
1968 Expression
* load_address
)
1969 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
1970 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
1971 flags_(flags
), load_address_(load_address
), load_address_value_(0),
1975 // Return the name of this segment.
1978 { return this->name_
; }
1980 // Return the type of the segment.
1983 { return this->type_
; }
1985 // Whether to include the file header.
1987 includes_filehdr() const
1988 { return this->includes_filehdr_
; }
1990 // Whether to include the program headers.
1992 includes_phdrs() const
1993 { return this->includes_phdrs_
; }
1995 // Return whether there is a load address.
1997 has_load_address() const
1998 { return this->load_address_
!= NULL
; }
2000 // Evaluate the load address expression if there is one.
2002 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2004 if (this->load_address_
!= NULL
)
2005 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
);
2008 // Return the load address.
2010 load_address() const
2012 gold_assert(this->load_address_
!= NULL
);
2013 return this->load_address_value_
;
2016 // Create the segment.
2018 create_segment(Layout
* layout
)
2020 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2021 return this->segment_
;
2024 // Return the segment.
2027 { return this->segment_
; }
2029 // Set the segment flags if appropriate.
2031 set_flags_if_valid()
2033 if (this->is_flags_valid_
)
2034 this->segment_
->set_flags(this->flags_
);
2037 // Print for debugging.
2042 // The name used in the script.
2044 // The type of the segment (PT_LOAD, etc.).
2046 // Whether this segment includes the file header.
2047 bool includes_filehdr_
;
2048 // Whether this segment includes the section headers.
2049 bool includes_phdrs_
;
2050 // Whether the flags were explicitly specified.
2051 bool is_flags_valid_
;
2052 // The flags for this segment (PF_R, etc.) if specified.
2053 unsigned int flags_
;
2054 // The expression for the load address for this segment. This may
2056 Expression
* load_address_
;
2057 // The actual load address from evaluating the expression.
2058 uint64_t load_address_value_
;
2059 // The segment itself.
2060 Output_segment
* segment_
;
2063 // Print for debugging.
2066 Phdrs_element::print(FILE* f
) const
2068 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2069 if (this->includes_filehdr_
)
2070 fprintf(f
, " FILEHDR");
2071 if (this->includes_phdrs_
)
2072 fprintf(f
, " PHDRS");
2073 if (this->is_flags_valid_
)
2074 fprintf(f
, " FLAGS(%u)", this->flags_
);
2075 if (this->load_address_
!= NULL
)
2078 this->load_address_
->print(f
);
2084 // Class Script_sections.
2086 Script_sections::Script_sections()
2087 : saw_sections_clause_(false),
2088 in_sections_clause_(false),
2089 sections_elements_(NULL
),
2090 output_section_(NULL
),
2091 phdrs_elements_(NULL
)
2095 // Start a SECTIONS clause.
2098 Script_sections::start_sections()
2100 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2101 this->saw_sections_clause_
= true;
2102 this->in_sections_clause_
= true;
2103 if (this->sections_elements_
== NULL
)
2104 this->sections_elements_
= new Sections_elements
;
2107 // Finish a SECTIONS clause.
2110 Script_sections::finish_sections()
2112 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2113 this->in_sections_clause_
= false;
2116 // Add a symbol to be defined.
2119 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2120 Expression
* val
, bool provide
,
2123 if (this->output_section_
!= NULL
)
2124 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2128 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2131 this->sections_elements_
->push_back(p
);
2135 // Add an assignment to the special dot symbol.
2138 Script_sections::add_dot_assignment(Expression
* val
)
2140 if (this->output_section_
!= NULL
)
2141 this->output_section_
->add_dot_assignment(val
);
2144 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2145 this->sections_elements_
->push_back(p
);
2149 // Add an assertion.
2152 Script_sections::add_assertion(Expression
* check
, const char* message
,
2155 if (this->output_section_
!= NULL
)
2156 this->output_section_
->add_assertion(check
, message
, messagelen
);
2159 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2161 this->sections_elements_
->push_back(p
);
2165 // Start processing entries for an output section.
2168 Script_sections::start_output_section(
2171 const Parser_output_section_header
*header
)
2173 Output_section_definition
* posd
= new Output_section_definition(name
,
2176 this->sections_elements_
->push_back(posd
);
2177 gold_assert(this->output_section_
== NULL
);
2178 this->output_section_
= posd
;
2181 // Stop processing entries for an output section.
2184 Script_sections::finish_output_section(
2185 const Parser_output_section_trailer
* trailer
)
2187 gold_assert(this->output_section_
!= NULL
);
2188 this->output_section_
->finish(trailer
);
2189 this->output_section_
= NULL
;
2192 // Add a data item to the current output section.
2195 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2197 gold_assert(this->output_section_
!= NULL
);
2198 this->output_section_
->add_data(size
, is_signed
, val
);
2201 // Add a fill value setting to the current output section.
2204 Script_sections::add_fill(Expression
* val
)
2206 gold_assert(this->output_section_
!= NULL
);
2207 this->output_section_
->add_fill(val
);
2210 // Add an input section specification to the current output section.
2213 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2215 gold_assert(this->output_section_
!= NULL
);
2216 this->output_section_
->add_input_section(spec
, keep
);
2219 // Add any symbols we are defining to the symbol table.
2222 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2224 if (!this->saw_sections_clause_
)
2226 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2227 p
!= this->sections_elements_
->end();
2229 (*p
)->add_symbols_to_table(symtab
);
2232 // Finalize symbols and check assertions.
2235 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2237 if (!this->saw_sections_clause_
)
2239 uint64_t dot_value
= 0;
2240 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2241 p
!= this->sections_elements_
->end();
2243 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2246 // Return the name of the output section to use for an input file name
2247 // and section name.
2250 Script_sections::output_section_name(const char* file_name
,
2251 const char* section_name
,
2252 Output_section
*** output_section_slot
)
2254 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2255 p
!= this->sections_elements_
->end();
2258 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2259 output_section_slot
);
2263 // The special name /DISCARD/ means that the input section
2264 // should be discarded.
2265 if (strcmp(ret
, "/DISCARD/") == 0)
2267 *output_section_slot
= NULL
;
2274 // If we couldn't find a mapping for the name, the output section
2275 // gets the name of the input section.
2277 *output_section_slot
= NULL
;
2279 return section_name
;
2282 // Place a marker for an orphan output section into the SECTIONS
2286 Script_sections::place_orphan(Output_section
* os
)
2288 // Look for an output section definition which matches the output
2289 // section. Put a marker after that section.
2290 Sections_elements::iterator place
= this->sections_elements_
->end();
2291 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2292 p
!= this->sections_elements_
->end();
2296 if ((*p
)->place_orphan_here(os
, &exact
))
2304 // The insert function puts the new element before the iterator.
2305 if (place
!= this->sections_elements_
->end())
2308 this->sections_elements_
->insert(place
, new Orphan_output_section(os
));
2311 // Set the addresses of all the output sections. Walk through all the
2312 // elements, tracking the dot symbol. Apply assignments which set
2313 // absolute symbol values, in case they are used when setting dot.
2314 // Fill in data statement values. As we find output sections, set the
2315 // address, set the address of all associated input sections, and
2316 // update dot. Return the segment which should hold the file header
2317 // and segment headers, if any.
2320 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2322 gold_assert(this->saw_sections_clause_
);
2324 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2325 // for our representation.
2326 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2327 p
!= this->sections_elements_
->end();
2330 Output_section_definition
* posd
;
2331 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2332 if (failed_constraint
!= CONSTRAINT_NONE
)
2334 Sections_elements::iterator q
;
2335 for (q
= this->sections_elements_
->begin();
2336 q
!= this->sections_elements_
->end();
2341 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2346 if (q
== this->sections_elements_
->end())
2347 gold_error(_("no matching section constraint"));
2351 // For a relocatable link, we implicitly set dot to zero.
2352 uint64_t dot_value
= 0;
2353 uint64_t load_address
= 0;
2354 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2355 p
!= this->sections_elements_
->end();
2357 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
2359 if (this->phdrs_elements_
!= NULL
)
2361 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2362 p
!= this->phdrs_elements_
->end();
2364 (*p
)->eval_load_address(symtab
, layout
);
2367 return this->create_segments(layout
);
2370 // Sort the sections in order to put them into segments.
2372 class Sort_output_sections
2376 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2380 Sort_output_sections::operator()(const Output_section
* os1
,
2381 const Output_section
* os2
) const
2383 // Sort first by the load address.
2384 uint64_t lma1
= (os1
->has_load_address()
2385 ? os1
->load_address()
2387 uint64_t lma2
= (os2
->has_load_address()
2388 ? os2
->load_address()
2393 // Then sort by the virtual address.
2394 if (os1
->address() != os2
->address())
2395 return os1
->address() < os2
->address();
2397 // Sort TLS sections to the end.
2398 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2399 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2403 // Sort PROGBITS before NOBITS.
2404 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2406 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2409 // Otherwise we don't care.
2413 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2414 // We treat a section with the SHF_TLS flag set as taking up space
2415 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2416 // space for them in the file.
2419 Script_sections::is_bss_section(const Output_section
* os
)
2421 return (os
->type() == elfcpp::SHT_NOBITS
2422 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2425 // Return the size taken by the file header and the program headers.
2428 Script_sections::total_header_size(Layout
* layout
) const
2430 size_t segment_count
= layout
->segment_count();
2431 size_t file_header_size
;
2432 size_t segment_headers_size
;
2433 if (parameters
->get_size() == 32)
2435 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2436 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2438 else if (parameters
->get_size() == 64)
2440 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2441 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2446 return file_header_size
+ segment_headers_size
;
2449 // Return the amount we have to subtract from the LMA to accomodate
2450 // headers of the given size. The complication is that the file
2451 // header have to be at the start of a page, as otherwise it will not
2452 // be at the start of the file.
2455 Script_sections::header_size_adjustment(uint64_t lma
,
2456 size_t sizeof_headers
) const
2458 const uint64_t abi_pagesize
= parameters
->target()->abi_pagesize();
2459 uint64_t hdr_lma
= lma
- sizeof_headers
;
2460 hdr_lma
&= ~(abi_pagesize
- 1);
2461 return lma
- hdr_lma
;
2464 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2465 // the segment which should hold the file header and segment headers,
2469 Script_sections::create_segments(Layout
* layout
)
2471 gold_assert(this->saw_sections_clause_
);
2473 if (parameters
->output_is_object())
2476 if (this->saw_phdrs_clause())
2477 return create_segments_from_phdrs_clause(layout
);
2479 Layout::Section_list sections
;
2480 layout
->get_allocated_sections(§ions
);
2482 // Sort the sections by address.
2483 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2485 this->create_note_and_tls_segments(layout
, §ions
);
2487 // Walk through the sections adding them to PT_LOAD segments.
2488 const uint64_t abi_pagesize
= parameters
->target()->abi_pagesize();
2489 Output_segment
* first_seg
= NULL
;
2490 Output_segment
* current_seg
= NULL
;
2491 bool is_current_seg_readonly
= true;
2492 Layout::Section_list::iterator plast
= sections
.end();
2493 uint64_t last_vma
= 0;
2494 uint64_t last_lma
= 0;
2495 uint64_t last_size
= 0;
2496 for (Layout::Section_list::iterator p
= sections
.begin();
2497 p
!= sections
.end();
2500 const uint64_t vma
= (*p
)->address();
2501 const uint64_t lma
= ((*p
)->has_load_address()
2502 ? (*p
)->load_address()
2504 const uint64_t size
= (*p
)->current_data_size();
2506 bool need_new_segment
;
2507 if (current_seg
== NULL
)
2508 need_new_segment
= true;
2509 else if (lma
- vma
!= last_lma
- last_vma
)
2511 // This section has a different LMA relationship than the
2512 // last one; we need a new segment.
2513 need_new_segment
= true;
2515 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2516 < align_address(lma
, abi_pagesize
))
2518 // Putting this section in the segment would require
2520 need_new_segment
= true;
2522 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2524 // A non-BSS section can not follow a BSS section in the
2526 need_new_segment
= true;
2528 else if (is_current_seg_readonly
2529 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2531 // Don't put a writable section in the same segment as a
2532 // non-writable section.
2533 need_new_segment
= true;
2537 // Otherwise, reuse the existing segment.
2538 need_new_segment
= false;
2541 elfcpp::Elf_Word seg_flags
=
2542 Layout::section_flags_to_segment((*p
)->flags());
2544 if (need_new_segment
)
2546 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2548 current_seg
->set_addresses(vma
, lma
);
2549 if (first_seg
== NULL
)
2550 first_seg
= current_seg
;
2551 is_current_seg_readonly
= true;
2554 current_seg
->add_output_section(*p
, seg_flags
);
2556 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2557 is_current_seg_readonly
= false;
2565 // An ELF program should work even if the program headers are not in
2566 // a PT_LOAD segment. However, it appears that the Linux kernel
2567 // does not set the AT_PHDR auxiliary entry in that case. It sets
2568 // the load address to p_vaddr - p_offset of the first PT_LOAD
2569 // segment. It then sets AT_PHDR to the load address plus the
2570 // offset to the program headers, e_phoff in the file header. This
2571 // fails when the program headers appear in the file before the
2572 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2573 // segment to hold the file header and the program headers. This is
2574 // effectively what the GNU linker does, and it is slightly more
2575 // efficient in any case. We try to use the first PT_LOAD segment
2576 // if we can, otherwise we make a new one.
2578 size_t sizeof_headers
= this->total_header_size(layout
);
2580 if (first_seg
!= NULL
2581 && (first_seg
->paddr() & (abi_pagesize
- 1)) >= sizeof_headers
)
2583 first_seg
->set_addresses(first_seg
->vaddr() - sizeof_headers
,
2584 first_seg
->paddr() - sizeof_headers
);
2588 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2590 if (first_seg
== NULL
)
2591 load_seg
->set_addresses(0, 0);
2594 uint64_t vma
= first_seg
->vaddr();
2595 uint64_t lma
= first_seg
->paddr();
2597 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
2598 if (lma
>= subtract
&& vma
>= subtract
)
2599 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
2602 // We could handle this case by create the file header
2603 // outside of any PT_LOAD segment, and creating a new
2604 // PT_LOAD segment after the others to hold the segment
2606 gold_error(_("sections loaded on first page without room for "
2607 "file and program headers are not supported"));
2614 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2615 // segment if there are any SHT_TLS sections.
2618 Script_sections::create_note_and_tls_segments(
2620 const Layout::Section_list
* sections
)
2622 gold_assert(!this->saw_phdrs_clause());
2624 bool saw_tls
= false;
2625 for (Layout::Section_list::const_iterator p
= sections
->begin();
2626 p
!= sections
->end();
2629 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2631 elfcpp::Elf_Word seg_flags
=
2632 Layout::section_flags_to_segment((*p
)->flags());
2633 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
2635 oseg
->add_output_section(*p
, seg_flags
);
2637 // Incorporate any subsequent SHT_NOTE sections, in the
2638 // hopes that the script is sensible.
2639 Layout::Section_list::const_iterator pnext
= p
+ 1;
2640 while (pnext
!= sections
->end()
2641 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
2643 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2644 oseg
->add_output_section(*pnext
, seg_flags
);
2650 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2653 gold_error(_("TLS sections are not adjacent"));
2655 elfcpp::Elf_Word seg_flags
=
2656 Layout::section_flags_to_segment((*p
)->flags());
2657 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
2659 oseg
->add_output_section(*p
, seg_flags
);
2661 Layout::Section_list::const_iterator pnext
= p
+ 1;
2662 while (pnext
!= sections
->end()
2663 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
2665 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2666 oseg
->add_output_section(*pnext
, seg_flags
);
2676 // Add a program header. The PHDRS clause is syntactically distinct
2677 // from the SECTIONS clause, but we implement it with the SECTIONS
2678 // support becauase PHDRS is useless if there is no SECTIONS clause.
2681 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
2682 bool includes_filehdr
, bool includes_phdrs
,
2683 bool is_flags_valid
, unsigned int flags
,
2684 Expression
* load_address
)
2686 if (this->phdrs_elements_
== NULL
)
2687 this->phdrs_elements_
= new Phdrs_elements();
2688 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
2691 is_flags_valid
, flags
,
2695 // Return the number of segments we expect to create based on the
2696 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2699 Script_sections::expected_segment_count(const Layout
* layout
) const
2701 if (this->saw_phdrs_clause())
2702 return this->phdrs_elements_
->size();
2704 Layout::Section_list sections
;
2705 layout
->get_allocated_sections(§ions
);
2707 // We assume that we will need two PT_LOAD segments.
2710 bool saw_note
= false;
2711 bool saw_tls
= false;
2712 for (Layout::Section_list::const_iterator p
= sections
.begin();
2713 p
!= sections
.end();
2716 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2718 // Assume that all note sections will fit into a single
2726 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2728 // There can only be one PT_TLS segment.
2740 // Create the segments from a PHDRS clause. Return the segment which
2741 // should hold the file header and program headers, if any.
2744 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
2746 this->attach_sections_using_phdrs_clause(layout
);
2747 return this->set_phdrs_clause_addresses(layout
);
2750 // Create the segments from the PHDRS clause, and put the output
2751 // sections in them.
2754 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
2756 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
2757 Name_to_segment name_to_segment
;
2758 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2759 p
!= this->phdrs_elements_
->end();
2761 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
2763 // Walk through the output sections and attach them to segments.
2764 // Output sections in the script which do not list segments are
2765 // attached to the same set of segments as the immediately preceding
2767 String_list
* phdr_names
= NULL
;
2768 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2769 p
!= this->sections_elements_
->end();
2772 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
);
2776 if (phdr_names
== NULL
)
2778 gold_error(_("allocated section not in any segment"));
2782 bool in_load_segment
= false;
2783 for (String_list::const_iterator q
= phdr_names
->begin();
2784 q
!= phdr_names
->end();
2787 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
2788 if (r
== name_to_segment
.end())
2789 gold_error(_("no segment %s"), q
->c_str());
2792 elfcpp::Elf_Word seg_flags
=
2793 Layout::section_flags_to_segment(os
->flags());
2794 r
->second
->add_output_section(os
, seg_flags
);
2796 if (r
->second
->type() == elfcpp::PT_LOAD
)
2798 if (in_load_segment
)
2799 gold_error(_("section in two PT_LOAD segments"));
2800 in_load_segment
= true;
2805 if (!in_load_segment
)
2806 gold_error(_("allocated section not in any PT_LOAD segment"));
2810 // Set the addresses for segments created from a PHDRS clause. Return
2811 // the segment which should hold the file header and program headers,
2815 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
2817 Output_segment
* load_seg
= NULL
;
2818 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2819 p
!= this->phdrs_elements_
->end();
2822 // Note that we have to set the flags after adding the output
2823 // sections to the segment, as adding an output segment can
2824 // change the flags.
2825 (*p
)->set_flags_if_valid();
2827 Output_segment
* oseg
= (*p
)->segment();
2829 if (oseg
->type() != elfcpp::PT_LOAD
)
2831 // The addresses of non-PT_LOAD segments are set from the
2832 // PT_LOAD segments.
2833 if ((*p
)->has_load_address())
2834 gold_error(_("may only specify load address for PT_LOAD segment"));
2838 // The output sections should have addresses from the SECTIONS
2839 // clause. The addresses don't have to be in order, so find the
2840 // one with the lowest load address. Use that to set the
2841 // address of the segment.
2843 Output_section
* osec
= oseg
->section_with_lowest_load_address();
2846 oseg
->set_addresses(0, 0);
2850 uint64_t vma
= osec
->address();
2851 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
2853 // Override the load address of the section with the load
2854 // address specified for the segment.
2855 if ((*p
)->has_load_address())
2857 if (osec
->has_load_address())
2858 gold_warning(_("PHDRS load address overrides "
2859 "section %s load address"),
2862 lma
= (*p
)->load_address();
2865 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
2866 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
2868 // We could support this if we wanted to.
2869 gold_error(_("using only one of FILEHDR and PHDRS is "
2870 "not currently supported"));
2874 size_t sizeof_headers
= this->total_header_size(layout
);
2875 uint64_t subtract
= this->header_size_adjustment(lma
,
2877 if (lma
>= subtract
&& vma
>= subtract
)
2884 gold_error(_("sections loaded on first page without room "
2885 "for file and program headers "
2886 "are not supported"));
2889 if (load_seg
!= NULL
)
2890 gold_error(_("using FILEHDR and PHDRS on more than one "
2891 "PT_LOAD segment is not currently supported"));
2895 oseg
->set_addresses(vma
, lma
);
2901 // Add the file header and segment headers to non-load segments
2902 // specified in the PHDRS clause.
2905 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
2906 Output_data
* segment_headers
)
2908 gold_assert(this->saw_phdrs_clause());
2909 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2910 p
!= this->phdrs_elements_
->end();
2913 if ((*p
)->type() != elfcpp::PT_LOAD
)
2915 if ((*p
)->includes_phdrs())
2916 (*p
)->segment()->add_initial_output_data(segment_headers
);
2917 if ((*p
)->includes_filehdr())
2918 (*p
)->segment()->add_initial_output_data(file_header
);
2923 // Print the SECTIONS clause to F for debugging.
2926 Script_sections::print(FILE* f
) const
2928 if (!this->saw_sections_clause_
)
2931 fprintf(f
, "SECTIONS {\n");
2933 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2934 p
!= this->sections_elements_
->end();
2940 if (this->phdrs_elements_
!= NULL
)
2942 fprintf(f
, "PHDRS {\n");
2943 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2944 p
!= this->phdrs_elements_
->end();
2951 } // End namespace gold.