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 // Create any required output sections. The only real
58 // implementation is in Output_section_definition.
60 create_sections(Layout
*)
63 // Add any symbol being defined to the symbol table.
65 add_symbols_to_table(Symbol_table
*)
68 // Finalize symbols and check assertions.
70 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*)
73 // Return the output section name to use for an input file name and
74 // section name. This only real implementation is in
75 // Output_section_definition.
77 output_section_name(const char*, const char*, Output_section
***)
80 // Return whether to place an orphan output section after this
83 place_orphan_here(const Output_section
*, bool*) const
86 // Set section addresses. This includes applying assignments if the
87 // the expression is an absolute value.
89 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*)
92 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
93 // this section is constrained, and the input sections do not match,
94 // return the constraint, and set *POSD.
95 virtual Section_constraint
96 check_constraint(Output_section_definition
**)
97 { return CONSTRAINT_NONE
; }
99 // See if this is the alternate output section for a constrained
100 // output section. If it is, transfer the Output_section and return
101 // true. Otherwise return false.
103 alternate_constraint(Output_section_definition
*, Section_constraint
)
106 // Get the list of segments to use for an allocated section when
107 // using a PHDRS clause. If this is an allocated section, return
108 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
109 // which it should be attached. If the PHDRS were not specified,
110 // don't change *PHDRS_LIST.
111 virtual Output_section
*
112 allocate_to_segment(String_list
**)
115 // Look for an output section by name and return the address, the
116 // load address, the alignment, and the size. This is used when an
117 // expression refers to an output section which was not actually
118 // created. This returns true if the section was found, false
119 // otherwise. The only real definition is for
120 // Output_section_definition.
122 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
126 // Print the element for debugging purposes.
128 print(FILE* f
) const = 0;
131 // An assignment in a SECTIONS clause outside of an output section.
133 class Sections_element_assignment
: public Sections_element
136 Sections_element_assignment(const char* name
, size_t namelen
,
137 Expression
* val
, bool provide
, bool hidden
)
138 : assignment_(name
, namelen
, val
, provide
, hidden
)
141 // Add the symbol to the symbol table.
143 add_symbols_to_table(Symbol_table
* symtab
)
144 { this->assignment_
.add_to_table(symtab
); }
146 // Finalize the symbol.
148 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
151 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
154 // Set the section address. There is no section here, but if the
155 // value is absolute, we set the symbol. This permits us to use
156 // absolute symbols when setting dot.
158 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
159 uint64_t* dot_value
, uint64_t*)
161 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
164 // Print for debugging.
169 this->assignment_
.print(f
);
173 Symbol_assignment assignment_
;
176 // An assignment to the dot symbol in a SECTIONS clause outside of an
179 class Sections_element_dot_assignment
: public Sections_element
182 Sections_element_dot_assignment(Expression
* val
)
186 // Finalize the symbol.
188 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
191 // We ignore the section of the result because outside of an
192 // output section definition the dot symbol is always considered
194 Output_section
* dummy
;
195 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
199 // Update the dot symbol while setting section addresses.
201 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
202 uint64_t* dot_value
, uint64_t* load_address
)
204 Output_section
* dummy
;
205 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
207 *load_address
= *dot_value
;
210 // Print for debugging.
215 this->val_
->print(f
);
223 // An assertion in a SECTIONS clause outside of an output section.
225 class Sections_element_assertion
: public Sections_element
228 Sections_element_assertion(Expression
* check
, const char* message
,
230 : assertion_(check
, message
, messagelen
)
233 // Check the assertion.
235 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
236 { this->assertion_
.check(symtab
, layout
); }
238 // Print for debugging.
243 this->assertion_
.print(f
);
247 Script_assertion assertion_
;
250 // An element in an output section in a SECTIONS clause.
252 class Output_section_element
255 // A list of input sections.
256 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
258 Output_section_element()
261 virtual ~Output_section_element()
264 // Return whether this element requires an output section to exist.
266 needs_output_section() const
269 // Add any symbol being defined to the symbol table.
271 add_symbols_to_table(Symbol_table
*)
274 // Finalize symbols and check assertions.
276 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
279 // Return whether this element matches FILE_NAME and SECTION_NAME.
280 // The only real implementation is in Output_section_element_input.
282 match_name(const char*, const char*) const
285 // Set section addresses. This includes applying assignments if the
286 // the expression is an absolute value.
288 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
289 uint64_t*, Output_section
**, std::string
*,
293 // Print the element for debugging purposes.
295 print(FILE* f
) const = 0;
298 // Return a fill string that is LENGTH bytes long, filling it with
301 get_fill_string(const std::string
* fill
, section_size_type length
) const;
305 Output_section_element::get_fill_string(const std::string
* fill
,
306 section_size_type length
) const
308 std::string this_fill
;
309 this_fill
.reserve(length
);
310 while (this_fill
.length() + fill
->length() <= length
)
312 if (this_fill
.length() < length
)
313 this_fill
.append(*fill
, 0, length
- this_fill
.length());
317 // A symbol assignment in an output section.
319 class Output_section_element_assignment
: public Output_section_element
322 Output_section_element_assignment(const char* name
, size_t namelen
,
323 Expression
* val
, bool provide
,
325 : assignment_(name
, namelen
, val
, provide
, hidden
)
328 // Add the symbol to the symbol table.
330 add_symbols_to_table(Symbol_table
* symtab
)
331 { this->assignment_
.add_to_table(symtab
); }
333 // Finalize the symbol.
335 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
336 uint64_t* dot_value
, Output_section
** dot_section
)
338 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
342 // Set the section address. There is no section here, but if the
343 // value is absolute, we set the symbol. This permits us to use
344 // absolute symbols when setting dot.
346 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
347 uint64_t, uint64_t* dot_value
, Output_section
**,
348 std::string
*, Input_section_list
*)
350 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
353 // Print for debugging.
358 this->assignment_
.print(f
);
362 Symbol_assignment assignment_
;
365 // An assignment to the dot symbol in an output section.
367 class Output_section_element_dot_assignment
: public Output_section_element
370 Output_section_element_dot_assignment(Expression
* val
)
374 // Finalize the symbol.
376 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
377 uint64_t* dot_value
, Output_section
** dot_section
)
379 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
380 *dot_section
, dot_section
);
383 // Update the dot symbol while setting section addresses.
385 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
386 uint64_t, uint64_t* dot_value
, Output_section
**,
387 std::string
*, Input_section_list
*);
389 // Print for debugging.
394 this->val_
->print(f
);
402 // Update the dot symbol while setting section addresses.
405 Output_section_element_dot_assignment::set_section_addresses(
406 Symbol_table
* symtab
,
408 Output_section
* output_section
,
411 Output_section
** dot_section
,
415 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
416 *dot_value
, *dot_section
,
418 if (next_dot
< *dot_value
)
419 gold_error(_("dot may not move backward"));
420 if (next_dot
> *dot_value
&& output_section
!= NULL
)
422 section_size_type length
= convert_to_section_size_type(next_dot
424 Output_section_data
* posd
;
426 posd
= new Output_data_fixed_space(length
, 0);
429 std::string this_fill
= this->get_fill_string(fill
, length
);
430 posd
= new Output_data_const(this_fill
, 0);
432 output_section
->add_output_section_data(posd
);
434 *dot_value
= next_dot
;
437 // An assertion in an output section.
439 class Output_section_element_assertion
: public Output_section_element
442 Output_section_element_assertion(Expression
* check
, const char* message
,
444 : assertion_(check
, message
, messagelen
)
451 this->assertion_
.print(f
);
455 Script_assertion assertion_
;
458 // We use a special instance of Output_section_data to handle BYTE,
459 // SHORT, etc. This permits forward references to symbols in the
462 class Output_data_expression
: public Output_section_data
465 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
466 const Symbol_table
* symtab
, const Layout
* layout
,
467 uint64_t dot_value
, Output_section
* dot_section
)
468 : Output_section_data(size
, 0),
469 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
470 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
474 // Write the data to the output file.
476 do_write(Output_file
*);
478 // Write the data to a buffer.
480 do_write_to_buffer(unsigned char*);
483 template<bool big_endian
>
485 endian_write_to_buffer(uint64_t, unsigned char*);
489 const Symbol_table
* symtab_
;
490 const Layout
* layout_
;
492 Output_section
* dot_section_
;
495 // Write the data element to the output file.
498 Output_data_expression::do_write(Output_file
* of
)
500 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
501 this->write_to_buffer(view
);
502 of
->write_output_view(this->offset(), this->data_size(), view
);
505 // Write the data element to a buffer.
508 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
510 Output_section
* dummy
;
511 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
512 true, this->dot_value_
,
513 this->dot_section_
, &dummy
);
515 if (parameters
->target().is_big_endian())
516 this->endian_write_to_buffer
<true>(val
, buf
);
518 this->endian_write_to_buffer
<false>(val
, buf
);
521 template<bool big_endian
>
523 Output_data_expression::endian_write_to_buffer(uint64_t val
,
526 switch (this->data_size())
529 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
532 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
535 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
538 if (parameters
->target().get_size() == 32)
541 if (this->is_signed_
&& (val
& 0x80000000) != 0)
542 val
|= 0xffffffff00000000LL
;
544 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
551 // A data item in an output section.
553 class Output_section_element_data
: public Output_section_element
556 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
557 : size_(size
), is_signed_(is_signed
), val_(val
)
560 // If there is a data item, then we must create an output section.
562 needs_output_section() const
565 // Finalize symbols--we just need to update dot.
567 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
569 { *dot_value
+= this->size_
; }
571 // Store the value in the section.
573 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
574 uint64_t* dot_value
, Output_section
**, std::string
*,
575 Input_section_list
*);
577 // Print for debugging.
582 // The size in bytes.
584 // Whether the value is signed.
590 // Store the value in the section.
593 Output_section_element_data::set_section_addresses(
594 Symbol_table
* symtab
,
599 Output_section
** dot_section
,
603 gold_assert(os
!= NULL
);
604 os
->add_output_section_data(new Output_data_expression(this->size_
,
611 *dot_value
+= this->size_
;
614 // Print for debugging.
617 Output_section_element_data::print(FILE* f
) const
632 if (this->is_signed_
)
640 fprintf(f
, " %s(", s
);
641 this->val_
->print(f
);
645 // A fill value setting in an output section.
647 class Output_section_element_fill
: public Output_section_element
650 Output_section_element_fill(Expression
* val
)
654 // Update the fill value while setting section addresses.
656 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
657 uint64_t, uint64_t* dot_value
,
658 Output_section
** dot_section
,
659 std::string
* fill
, Input_section_list
*)
661 Output_section
* fill_section
;
662 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
663 *dot_value
, *dot_section
,
665 if (fill_section
!= NULL
)
666 gold_warning(_("fill value is not absolute"));
667 // FIXME: The GNU linker supports fill values of arbitrary length.
668 unsigned char fill_buff
[4];
669 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
670 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
673 // Print for debugging.
677 fprintf(f
, " FILL(");
678 this->val_
->print(f
);
683 // The new fill value.
687 // Return whether STRING contains a wildcard character. This is used
688 // to speed up matching.
691 is_wildcard_string(const std::string
& s
)
693 return strpbrk(s
.c_str(), "?*[") != NULL
;
696 // An input section specification in an output section
698 class Output_section_element_input
: public Output_section_element
701 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
703 // Finalize symbols--just update the value of the dot symbol.
705 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
706 Output_section
** dot_section
)
708 *dot_value
= this->final_dot_value_
;
709 *dot_section
= this->final_dot_section_
;
712 // See whether we match FILE_NAME and SECTION_NAME as an input
715 match_name(const char* file_name
, const char* section_name
) const;
717 // Set the section address.
719 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
720 uint64_t subalign
, uint64_t* dot_value
,
721 Output_section
**, std::string
* fill
,
722 Input_section_list
*);
724 // Print for debugging.
726 print(FILE* f
) const;
729 // An input section pattern.
730 struct Input_section_pattern
733 bool pattern_is_wildcard
;
736 Input_section_pattern(const char* patterna
, size_t patternlena
,
738 : pattern(patterna
, patternlena
),
739 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
744 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
746 // Filename_exclusions is a pair of filename pattern and a bool
747 // indicating whether the filename is a wildcard.
748 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
750 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
751 // indicates whether this is a wildcard pattern.
753 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
755 return (is_wildcard_pattern
756 ? fnmatch(pattern
, string
, 0) == 0
757 : strcmp(string
, pattern
) == 0);
760 // See if we match a file name.
762 match_file_name(const char* file_name
) const;
764 // The file name pattern. If this is the empty string, we match all
766 std::string filename_pattern_
;
767 // Whether the file name pattern is a wildcard.
768 bool filename_is_wildcard_
;
769 // How the file names should be sorted. This may only be
770 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
771 Sort_wildcard filename_sort_
;
772 // The list of file names to exclude.
773 Filename_exclusions filename_exclusions_
;
774 // The list of input section patterns.
775 Input_section_patterns input_section_patterns_
;
776 // Whether to keep this section when garbage collecting.
778 // The value of dot after including all matching sections.
779 uint64_t final_dot_value_
;
780 // The section where dot is defined after including all matching
782 Output_section
* final_dot_section_
;
785 // Construct Output_section_element_input. The parser records strings
786 // as pointers into a copy of the script file, which will go away when
787 // parsing is complete. We make sure they are in std::string objects.
789 Output_section_element_input::Output_section_element_input(
790 const Input_section_spec
* spec
,
792 : filename_pattern_(),
793 filename_is_wildcard_(false),
794 filename_sort_(spec
->file
.sort
),
795 filename_exclusions_(),
796 input_section_patterns_(),
799 final_dot_section_(NULL
)
801 // The filename pattern "*" is common, and matches all files. Turn
802 // it into the empty string.
803 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
804 this->filename_pattern_
.assign(spec
->file
.name
.value
,
805 spec
->file
.name
.length
);
806 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
808 if (spec
->input_sections
.exclude
!= NULL
)
810 for (String_list::const_iterator p
=
811 spec
->input_sections
.exclude
->begin();
812 p
!= spec
->input_sections
.exclude
->end();
815 bool is_wildcard
= is_wildcard_string(*p
);
816 this->filename_exclusions_
.push_back(std::make_pair(*p
,
821 if (spec
->input_sections
.sections
!= NULL
)
823 Input_section_patterns
& isp(this->input_section_patterns_
);
824 for (String_sort_list::const_iterator p
=
825 spec
->input_sections
.sections
->begin();
826 p
!= spec
->input_sections
.sections
->end();
828 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
833 // See whether we match FILE_NAME.
836 Output_section_element_input::match_file_name(const char* file_name
) const
838 if (!this->filename_pattern_
.empty())
840 // If we were called with no filename, we refuse to match a
841 // pattern which requires a file name.
842 if (file_name
== NULL
)
845 if (!match(file_name
, this->filename_pattern_
.c_str(),
846 this->filename_is_wildcard_
))
850 if (file_name
!= NULL
)
852 // Now we have to see whether FILE_NAME matches one of the
853 // exclusion patterns, if any.
854 for (Filename_exclusions::const_iterator p
=
855 this->filename_exclusions_
.begin();
856 p
!= this->filename_exclusions_
.end();
859 if (match(file_name
, p
->first
.c_str(), p
->second
))
867 // See whether we match FILE_NAME and SECTION_NAME.
870 Output_section_element_input::match_name(const char* file_name
,
871 const char* section_name
) const
873 if (!this->match_file_name(file_name
))
876 // If there are no section name patterns, then we match.
877 if (this->input_section_patterns_
.empty())
880 // See whether we match the section name patterns.
881 for (Input_section_patterns::const_iterator p
=
882 this->input_section_patterns_
.begin();
883 p
!= this->input_section_patterns_
.end();
886 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
890 // We didn't match any section names, so we didn't match.
894 // Information we use to sort the input sections.
896 struct Input_section_info
900 std::string section_name
;
905 // A class to sort the input sections.
907 class Input_section_sorter
910 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
911 : filename_sort_(filename_sort
), section_sort_(section_sort
)
915 operator()(const Input_section_info
&, const Input_section_info
&) const;
918 Sort_wildcard filename_sort_
;
919 Sort_wildcard section_sort_
;
923 Input_section_sorter::operator()(const Input_section_info
& isi1
,
924 const Input_section_info
& isi2
) const
926 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
927 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
928 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
929 && isi1
.addralign
== isi2
.addralign
))
931 if (isi1
.section_name
!= isi2
.section_name
)
932 return isi1
.section_name
< isi2
.section_name
;
934 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
935 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
936 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
938 if (isi1
.addralign
!= isi2
.addralign
)
939 return isi1
.addralign
< isi2
.addralign
;
941 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
943 if (isi1
.relobj
->name() != isi2
.relobj
->name())
944 return isi1
.relobj
->name() < isi2
.relobj
->name();
947 // Otherwise we leave them in the same order.
951 // Set the section address. Look in INPUT_SECTIONS for sections which
952 // match this spec, sort them as specified, and add them to the output
956 Output_section_element_input::set_section_addresses(
959 Output_section
* output_section
,
962 Output_section
** dot_section
,
964 Input_section_list
* input_sections
)
966 // We build a list of sections which match each
967 // Input_section_pattern.
969 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
970 size_t input_pattern_count
= this->input_section_patterns_
.size();
971 if (input_pattern_count
== 0)
972 input_pattern_count
= 1;
973 Matching_sections
matching_sections(input_pattern_count
);
975 // Look through the list of sections for this output section. Add
976 // each one which matches to one of the elements of
977 // MATCHING_SECTIONS.
979 Input_section_list::iterator p
= input_sections
->begin();
980 while (p
!= input_sections
->end())
982 // Calling section_name and section_addralign is not very
984 Input_section_info isi
;
985 isi
.relobj
= p
->first
;
986 isi
.shndx
= p
->second
;
988 // Lock the object so that we can get information about the
989 // section. This is OK since we know we are single-threaded
992 const Task
* task
= reinterpret_cast<const Task
*>(-1);
993 Task_lock_obj
<Object
> tl(task
, p
->first
);
995 isi
.section_name
= p
->first
->section_name(p
->second
);
996 isi
.size
= p
->first
->section_size(p
->second
);
997 isi
.addralign
= p
->first
->section_addralign(p
->second
);
1000 if (!this->match_file_name(isi
.relobj
->name().c_str()))
1002 else if (this->input_section_patterns_
.empty())
1004 matching_sections
[0].push_back(isi
);
1005 p
= input_sections
->erase(p
);
1010 for (i
= 0; i
< input_pattern_count
; ++i
)
1012 const Input_section_pattern
&
1013 isp(this->input_section_patterns_
[i
]);
1014 if (match(isi
.section_name
.c_str(), isp
.pattern
.c_str(),
1015 isp
.pattern_is_wildcard
))
1019 if (i
>= this->input_section_patterns_
.size())
1023 matching_sections
[i
].push_back(isi
);
1024 p
= input_sections
->erase(p
);
1029 // Look through MATCHING_SECTIONS. Sort each one as specified,
1030 // using a stable sort so that we get the default order when
1031 // sections are otherwise equal. Add each input section to the
1034 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1036 if (matching_sections
[i
].empty())
1039 gold_assert(output_section
!= NULL
);
1041 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1042 if (isp
.sort
!= SORT_WILDCARD_NONE
1043 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1044 std::stable_sort(matching_sections
[i
].begin(),
1045 matching_sections
[i
].end(),
1046 Input_section_sorter(this->filename_sort_
,
1049 for (std::vector
<Input_section_info
>::const_iterator p
=
1050 matching_sections
[i
].begin();
1051 p
!= matching_sections
[i
].end();
1054 uint64_t this_subalign
= p
->addralign
;
1055 if (this_subalign
< subalign
)
1056 this_subalign
= subalign
;
1058 uint64_t address
= align_address(*dot_value
, this_subalign
);
1060 if (address
> *dot_value
&& !fill
->empty())
1062 section_size_type length
=
1063 convert_to_section_size_type(address
- *dot_value
);
1064 std::string this_fill
= this->get_fill_string(fill
, length
);
1065 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1066 output_section
->add_output_section_data(posd
);
1069 output_section
->add_input_section_for_script(p
->relobj
,
1074 *dot_value
= address
+ p
->size
;
1078 this->final_dot_value_
= *dot_value
;
1079 this->final_dot_section_
= *dot_section
;
1082 // Print for debugging.
1085 Output_section_element_input::print(FILE* f
) const
1090 fprintf(f
, "KEEP(");
1092 if (!this->filename_pattern_
.empty())
1094 bool need_close_paren
= false;
1095 switch (this->filename_sort_
)
1097 case SORT_WILDCARD_NONE
:
1099 case SORT_WILDCARD_BY_NAME
:
1100 fprintf(f
, "SORT_BY_NAME(");
1101 need_close_paren
= true;
1107 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1109 if (need_close_paren
)
1113 if (!this->input_section_patterns_
.empty()
1114 || !this->filename_exclusions_
.empty())
1118 bool need_space
= false;
1119 if (!this->filename_exclusions_
.empty())
1121 fprintf(f
, "EXCLUDE_FILE(");
1122 bool need_comma
= false;
1123 for (Filename_exclusions::const_iterator p
=
1124 this->filename_exclusions_
.begin();
1125 p
!= this->filename_exclusions_
.end();
1130 fprintf(f
, "%s", p
->first
.c_str());
1137 for (Input_section_patterns::const_iterator p
=
1138 this->input_section_patterns_
.begin();
1139 p
!= this->input_section_patterns_
.end();
1145 int close_parens
= 0;
1148 case SORT_WILDCARD_NONE
:
1150 case SORT_WILDCARD_BY_NAME
:
1151 fprintf(f
, "SORT_BY_NAME(");
1154 case SORT_WILDCARD_BY_ALIGNMENT
:
1155 fprintf(f
, "SORT_BY_ALIGNMENT(");
1158 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1159 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1162 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1163 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1170 fprintf(f
, "%s", p
->pattern
.c_str());
1172 for (int i
= 0; i
< close_parens
; ++i
)
1187 // An output section.
1189 class Output_section_definition
: public Sections_element
1192 typedef Output_section_element::Input_section_list Input_section_list
;
1194 Output_section_definition(const char* name
, size_t namelen
,
1195 const Parser_output_section_header
* header
);
1197 // Finish the output section with the information in the trailer.
1199 finish(const Parser_output_section_trailer
* trailer
);
1201 // Add a symbol to be defined.
1203 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1204 bool provide
, bool hidden
);
1206 // Add an assignment to the special dot symbol.
1208 add_dot_assignment(Expression
* value
);
1210 // Add an assertion.
1212 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1214 // Add a data item to the current output section.
1216 add_data(int size
, bool is_signed
, Expression
* val
);
1218 // Add a setting for the fill value.
1220 add_fill(Expression
* val
);
1222 // Add an input section specification.
1224 add_input_section(const Input_section_spec
* spec
, bool keep
);
1226 // Create any required output sections.
1228 create_sections(Layout
*);
1230 // Add any symbols being defined to the symbol table.
1232 add_symbols_to_table(Symbol_table
* symtab
);
1234 // Finalize symbols and check assertions.
1236 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1238 // Return the output section name to use for an input file name and
1241 output_section_name(const char* file_name
, const char* section_name
,
1244 // Return whether to place an orphan section after this one.
1246 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1248 // Set the section address.
1250 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1251 uint64_t* dot_value
, uint64_t* load_address
);
1253 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1254 // this section is constrained, and the input sections do not match,
1255 // return the constraint, and set *POSD.
1257 check_constraint(Output_section_definition
** posd
);
1259 // See if this is the alternate output section for a constrained
1260 // output section. If it is, transfer the Output_section and return
1261 // true. Otherwise return false.
1263 alternate_constraint(Output_section_definition
*, Section_constraint
);
1265 // Get the list of segments to use for an allocated section when
1266 // using a PHDRS clause. If this is an allocated section, return
1267 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
1268 // which it should be attached. If the PHDRS were not specified,
1269 // don't change *PHDRS_LIST.
1271 allocate_to_segment(String_list
** phdrs_list
);
1273 // Look for an output section by name and return the address, the
1274 // load address, the alignment, and the size. This is used when an
1275 // expression refers to an output section which was not actually
1276 // created. This returns true if the section was found, false
1279 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1282 // Print the contents to the FILE. This is for debugging.
1287 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1289 // The output section name.
1291 // The address. This may be NULL.
1292 Expression
* address_
;
1293 // The load address. This may be NULL.
1294 Expression
* load_address_
;
1295 // The alignment. This may be NULL.
1297 // The input section alignment. This may be NULL.
1298 Expression
* subalign_
;
1299 // The constraint, if any.
1300 Section_constraint constraint_
;
1301 // The fill value. This may be NULL.
1303 // The list of segments this section should go into. This may be
1305 String_list
* phdrs_
;
1306 // The list of elements defining the section.
1307 Output_section_elements elements_
;
1308 // The Output_section created for this definition. This will be
1309 // NULL if none was created.
1310 Output_section
* output_section_
;
1311 // The address after it has been evaluated.
1312 uint64_t evaluated_address_
;
1313 // The load address after it has been evaluated.
1314 uint64_t evaluated_load_address_
;
1315 // The alignment after it has been evaluated.
1316 uint64_t evaluated_addralign_
;
1321 Output_section_definition::Output_section_definition(
1324 const Parser_output_section_header
* header
)
1325 : name_(name
, namelen
),
1326 address_(header
->address
),
1327 load_address_(header
->load_address
),
1328 align_(header
->align
),
1329 subalign_(header
->subalign
),
1330 constraint_(header
->constraint
),
1334 output_section_(NULL
)
1338 // Finish an output section.
1341 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1343 this->fill_
= trailer
->fill
;
1344 this->phdrs_
= trailer
->phdrs
;
1347 // Add a symbol to be defined.
1350 Output_section_definition::add_symbol_assignment(const char* name
,
1356 Output_section_element
* p
= new Output_section_element_assignment(name
,
1361 this->elements_
.push_back(p
);
1364 // Add an assignment to the special dot symbol.
1367 Output_section_definition::add_dot_assignment(Expression
* value
)
1369 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1370 this->elements_
.push_back(p
);
1373 // Add an assertion.
1376 Output_section_definition::add_assertion(Expression
* check
,
1377 const char* message
,
1380 Output_section_element
* p
= new Output_section_element_assertion(check
,
1383 this->elements_
.push_back(p
);
1386 // Add a data item to the current output section.
1389 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1391 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1393 this->elements_
.push_back(p
);
1396 // Add a setting for the fill value.
1399 Output_section_definition::add_fill(Expression
* val
)
1401 Output_section_element
* p
= new Output_section_element_fill(val
);
1402 this->elements_
.push_back(p
);
1405 // Add an input section specification.
1408 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1411 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1412 this->elements_
.push_back(p
);
1415 // Create any required output sections. We need an output section if
1416 // there is a data statement here.
1419 Output_section_definition::create_sections(Layout
* layout
)
1421 if (this->output_section_
!= NULL
)
1423 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1424 p
!= this->elements_
.end();
1427 if ((*p
)->needs_output_section())
1429 const char* name
= this->name_
.c_str();
1430 this->output_section_
= layout
->make_output_section_for_script(name
);
1436 // Add any symbols being defined to the symbol table.
1439 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1441 for (Output_section_elements::iterator p
= this->elements_
.begin();
1442 p
!= this->elements_
.end();
1444 (*p
)->add_symbols_to_table(symtab
);
1447 // Finalize symbols and check assertions.
1450 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1451 const Layout
* layout
,
1452 uint64_t* dot_value
)
1454 if (this->output_section_
!= NULL
)
1455 *dot_value
= this->output_section_
->address();
1458 uint64_t address
= *dot_value
;
1459 if (this->address_
!= NULL
)
1461 Output_section
* dummy
;
1462 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1466 if (this->align_
!= NULL
)
1468 Output_section
* dummy
;
1469 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
1473 address
= align_address(address
, align
);
1475 *dot_value
= address
;
1478 Output_section
* dot_section
= this->output_section_
;
1479 for (Output_section_elements::iterator p
= this->elements_
.begin();
1480 p
!= this->elements_
.end();
1482 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1485 // Return the output section name to use for an input section name.
1488 Output_section_definition::output_section_name(const char* file_name
,
1489 const char* section_name
,
1490 Output_section
*** slot
)
1492 // Ask each element whether it matches NAME.
1493 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1494 p
!= this->elements_
.end();
1497 if ((*p
)->match_name(file_name
, section_name
))
1499 // We found a match for NAME, which means that it should go
1500 // into this output section.
1501 *slot
= &this->output_section_
;
1502 return this->name_
.c_str();
1506 // We don't know about this section name.
1510 // Return whether to place an orphan output section after this
1514 Output_section_definition::place_orphan_here(const Output_section
*os
,
1517 // Check for the simple case first.
1518 if (this->output_section_
!= NULL
1519 && this->output_section_
->type() == os
->type()
1520 && this->output_section_
->flags() == os
->flags())
1526 // Otherwise use some heuristics.
1528 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1531 if (os
->type() == elfcpp::SHT_NOBITS
)
1533 if (this->name_
== ".bss")
1538 if (this->output_section_
!= NULL
1539 && this->output_section_
->type() == elfcpp::SHT_NOBITS
)
1542 else if (os
->type() == elfcpp::SHT_NOTE
)
1544 if (this->output_section_
!= NULL
1545 && this->output_section_
->type() == elfcpp::SHT_NOTE
)
1550 if (this->name_
.compare(0, 5, ".note") == 0)
1555 if (this->name_
== ".interp")
1557 if (this->output_section_
!= NULL
1558 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1559 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1562 else if (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
1564 if (this->name_
.compare(0, 4, ".rel") == 0)
1569 if (this->output_section_
!= NULL
1570 && (this->output_section_
->type() == elfcpp::SHT_REL
1571 || this->output_section_
->type() == elfcpp::SHT_RELA
))
1576 if (this->output_section_
!= NULL
1577 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1578 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1581 else if (os
->type() == elfcpp::SHT_PROGBITS
1582 && (os
->flags() & elfcpp::SHF_WRITE
) != 0)
1584 if (this->name_
== ".data")
1589 if (this->output_section_
!= NULL
1590 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1591 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1594 else if (os
->type() == elfcpp::SHT_PROGBITS
1595 && (os
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1597 if (this->name_
== ".text")
1602 if (this->output_section_
!= NULL
1603 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1604 && (this->output_section_
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1607 else if (os
->type() == elfcpp::SHT_PROGBITS
1608 || (os
->type() != elfcpp::SHT_PROGBITS
1609 && (os
->flags() & elfcpp::SHF_WRITE
) == 0))
1611 if (this->name_
== ".rodata")
1616 if (this->output_section_
!= NULL
1617 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1618 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1625 // Set the section address. Note that the OUTPUT_SECTION_ field will
1626 // be NULL if no input sections were mapped to this output section.
1627 // We still have to adjust dot and process symbol assignments.
1630 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1632 uint64_t* dot_value
,
1633 uint64_t* load_address
)
1636 if (this->address_
== NULL
)
1637 address
= *dot_value
;
1640 Output_section
* dummy
;
1641 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1642 *dot_value
, NULL
, &dummy
);
1646 if (this->align_
== NULL
)
1648 if (this->output_section_
== NULL
)
1651 align
= this->output_section_
->addralign();
1655 Output_section
* align_section
;
1656 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1657 NULL
, &align_section
);
1658 if (align_section
!= NULL
)
1659 gold_warning(_("alignment of section %s is not absolute"),
1660 this->name_
.c_str());
1661 if (this->output_section_
!= NULL
)
1662 this->output_section_
->set_addralign(align
);
1665 address
= align_address(address
, align
);
1667 uint64_t start_address
= address
;
1669 *dot_value
= address
;
1671 // The address of non-SHF_ALLOC sections is forced to zero,
1672 // regardless of what the linker script wants.
1673 if (this->output_section_
!= NULL
1674 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1675 this->output_section_
->set_address(address
);
1677 this->evaluated_address_
= address
;
1678 this->evaluated_addralign_
= align
;
1680 if (this->load_address_
== NULL
)
1681 this->evaluated_load_address_
= address
;
1684 Output_section
* dummy
;
1685 uint64_t load_address
=
1686 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1687 this->output_section_
, &dummy
);
1688 if (this->output_section_
!= NULL
)
1689 this->output_section_
->set_load_address(load_address
);
1690 this->evaluated_load_address_
= load_address
;
1694 if (this->subalign_
== NULL
)
1698 Output_section
* subalign_section
;
1699 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
1702 if (subalign_section
!= NULL
)
1703 gold_warning(_("subalign of section %s is not absolute"),
1704 this->name_
.c_str());
1708 if (this->fill_
!= NULL
)
1710 // FIXME: The GNU linker supports fill values of arbitrary
1712 Output_section
* fill_section
;
1713 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
1717 if (fill_section
!= NULL
)
1718 gold_warning(_("fill of section %s is not absolute"),
1719 this->name_
.c_str());
1720 unsigned char fill_buff
[4];
1721 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1722 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1725 Input_section_list input_sections
;
1726 if (this->output_section_
!= NULL
)
1728 // Get the list of input sections attached to this output
1729 // section. This will leave the output section with only
1730 // Output_section_data entries.
1731 address
+= this->output_section_
->get_input_sections(address
,
1734 *dot_value
= address
;
1737 Output_section
* dot_section
= this->output_section_
;
1738 for (Output_section_elements::iterator p
= this->elements_
.begin();
1739 p
!= this->elements_
.end();
1741 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
1742 subalign
, dot_value
, &dot_section
, &fill
,
1745 gold_assert(input_sections
.empty());
1747 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
1748 *load_address
= *dot_value
;
1750 *load_address
= (this->output_section_
->load_address()
1751 + (*dot_value
- start_address
));
1754 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1755 // this section is constrained, and the input sections do not match,
1756 // return the constraint, and set *POSD.
1759 Output_section_definition::check_constraint(Output_section_definition
** posd
)
1761 switch (this->constraint_
)
1763 case CONSTRAINT_NONE
:
1764 return CONSTRAINT_NONE
;
1766 case CONSTRAINT_ONLY_IF_RO
:
1767 if (this->output_section_
!= NULL
1768 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1771 return CONSTRAINT_ONLY_IF_RO
;
1773 return CONSTRAINT_NONE
;
1775 case CONSTRAINT_ONLY_IF_RW
:
1776 if (this->output_section_
!= NULL
1777 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1780 return CONSTRAINT_ONLY_IF_RW
;
1782 return CONSTRAINT_NONE
;
1784 case CONSTRAINT_SPECIAL
:
1785 if (this->output_section_
!= NULL
)
1786 gold_error(_("SPECIAL constraints are not implemented"));
1787 return CONSTRAINT_NONE
;
1794 // See if this is the alternate output section for a constrained
1795 // output section. If it is, transfer the Output_section and return
1796 // true. Otherwise return false.
1799 Output_section_definition::alternate_constraint(
1800 Output_section_definition
* posd
,
1801 Section_constraint constraint
)
1803 if (this->name_
!= posd
->name_
)
1808 case CONSTRAINT_ONLY_IF_RO
:
1809 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
1813 case CONSTRAINT_ONLY_IF_RW
:
1814 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
1822 // We have found the alternate constraint. We just need to move
1823 // over the Output_section. When constraints are used properly,
1824 // THIS should not have an output_section pointer, as all the input
1825 // sections should have matched the other definition.
1827 if (this->output_section_
!= NULL
)
1828 gold_error(_("mismatched definition for constrained sections"));
1830 this->output_section_
= posd
->output_section_
;
1831 posd
->output_section_
= NULL
;
1836 // Get the list of segments to use for an allocated section when using
1837 // a PHDRS clause. If this is an allocated section, return the
1838 // Output_section, and set *PHDRS_LIST to the list of PHDRS to which
1839 // it should be attached. If the PHDRS were not specified, don't
1840 // change *PHDRS_LIST.
1843 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
)
1845 if (this->output_section_
== NULL
)
1847 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
1849 if (this->phdrs_
!= NULL
)
1850 *phdrs_list
= this->phdrs_
;
1851 return this->output_section_
;
1854 // Look for an output section by name and return the address, the load
1855 // address, the alignment, and the size. This is used when an
1856 // expression refers to an output section which was not actually
1857 // created. This returns true if the section was found, false
1861 Output_section_definition::get_output_section_info(const char* name
,
1863 uint64_t* load_address
,
1864 uint64_t* addralign
,
1865 uint64_t* size
) const
1867 if (this->name_
!= name
)
1870 if (this->output_section_
!= NULL
)
1872 *address
= this->output_section_
->address();
1873 if (this->output_section_
->has_load_address())
1874 *load_address
= this->output_section_
->load_address();
1876 *load_address
= *address
;
1877 *addralign
= this->output_section_
->addralign();
1878 *size
= this->output_section_
->current_data_size();
1882 *address
= this->evaluated_address_
;
1883 *load_address
= this->evaluated_load_address_
;
1884 *addralign
= this->evaluated_addralign_
;
1891 // Print for debugging.
1894 Output_section_definition::print(FILE* f
) const
1896 fprintf(f
, " %s ", this->name_
.c_str());
1898 if (this->address_
!= NULL
)
1900 this->address_
->print(f
);
1906 if (this->load_address_
!= NULL
)
1909 this->load_address_
->print(f
);
1913 if (this->align_
!= NULL
)
1915 fprintf(f
, "ALIGN(");
1916 this->align_
->print(f
);
1920 if (this->subalign_
!= NULL
)
1922 fprintf(f
, "SUBALIGN(");
1923 this->subalign_
->print(f
);
1929 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1930 p
!= this->elements_
.end();
1936 if (this->fill_
!= NULL
)
1939 this->fill_
->print(f
);
1942 if (this->phdrs_
!= NULL
)
1944 for (String_list::const_iterator p
= this->phdrs_
->begin();
1945 p
!= this->phdrs_
->end();
1947 fprintf(f
, " :%s", p
->c_str());
1953 // An output section created to hold orphaned input sections. These
1954 // do not actually appear in linker scripts. However, for convenience
1955 // when setting the output section addresses, we put a marker to these
1956 // sections in the appropriate place in the list of SECTIONS elements.
1958 class Orphan_output_section
: public Sections_element
1961 Orphan_output_section(Output_section
* os
)
1965 // Return whether to place an orphan section after this one.
1967 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1969 // Set section addresses.
1971 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
1973 // Get the list of segments to use for an allocated section when
1974 // using a PHDRS clause. If this is an allocated section, return
1975 // the Output_section.
1977 allocate_to_segment(String_list
**);
1979 // Print for debugging.
1981 print(FILE* f
) const
1983 fprintf(f
, " marker for orphaned output section %s\n",
1988 Output_section
* os_
;
1991 // Whether to place another orphan section after this one.
1994 Orphan_output_section::place_orphan_here(const Output_section
* os
,
1997 if (this->os_
->type() == os
->type()
1998 && this->os_
->flags() == os
->flags())
2006 // Set section addresses.
2009 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2010 uint64_t* dot_value
,
2011 uint64_t* load_address
)
2013 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
2015 bool have_load_address
= *load_address
!= *dot_value
;
2017 uint64_t address
= *dot_value
;
2018 address
= align_address(address
, this->os_
->addralign());
2020 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2022 this->os_
->set_address(address
);
2023 if (have_load_address
)
2024 this->os_
->set_load_address(align_address(*load_address
,
2025 this->os_
->addralign()));
2028 Input_section_list input_sections
;
2029 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2031 for (Input_section_list::iterator p
= input_sections
.begin();
2032 p
!= input_sections
.end();
2038 // We know what are single-threaded, so it is OK to lock the
2041 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2042 Task_lock_obj
<Object
> tl(task
, p
->first
);
2043 addralign
= p
->first
->section_addralign(p
->second
);
2044 size
= p
->first
->section_size(p
->second
);
2047 address
= align_address(address
, addralign
);
2048 this->os_
->add_input_section_for_script(p
->first
, p
->second
, size
,
2053 if (!have_load_address
)
2054 *load_address
= address
;
2056 *load_address
+= address
- *dot_value
;
2058 *dot_value
= address
;
2061 // Get the list of segments to use for an allocated section when using
2062 // a PHDRS clause. If this is an allocated section, return the
2063 // Output_section. We don't change the list of segments.
2066 Orphan_output_section::allocate_to_segment(String_list
**)
2068 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2073 // Class Phdrs_element. A program header from a PHDRS clause.
2078 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2079 bool includes_filehdr
, bool includes_phdrs
,
2080 bool is_flags_valid
, unsigned int flags
,
2081 Expression
* load_address
)
2082 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2083 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2084 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2088 // Return the name of this segment.
2091 { return this->name_
; }
2093 // Return the type of the segment.
2096 { return this->type_
; }
2098 // Whether to include the file header.
2100 includes_filehdr() const
2101 { return this->includes_filehdr_
; }
2103 // Whether to include the program headers.
2105 includes_phdrs() const
2106 { return this->includes_phdrs_
; }
2108 // Return whether there is a load address.
2110 has_load_address() const
2111 { return this->load_address_
!= NULL
; }
2113 // Evaluate the load address expression if there is one.
2115 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2117 if (this->load_address_
!= NULL
)
2118 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2122 // Return the load address.
2124 load_address() const
2126 gold_assert(this->load_address_
!= NULL
);
2127 return this->load_address_value_
;
2130 // Create the segment.
2132 create_segment(Layout
* layout
)
2134 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2135 return this->segment_
;
2138 // Return the segment.
2141 { return this->segment_
; }
2143 // Set the segment flags if appropriate.
2145 set_flags_if_valid()
2147 if (this->is_flags_valid_
)
2148 this->segment_
->set_flags(this->flags_
);
2151 // Print for debugging.
2156 // The name used in the script.
2158 // The type of the segment (PT_LOAD, etc.).
2160 // Whether this segment includes the file header.
2161 bool includes_filehdr_
;
2162 // Whether this segment includes the section headers.
2163 bool includes_phdrs_
;
2164 // Whether the flags were explicitly specified.
2165 bool is_flags_valid_
;
2166 // The flags for this segment (PF_R, etc.) if specified.
2167 unsigned int flags_
;
2168 // The expression for the load address for this segment. This may
2170 Expression
* load_address_
;
2171 // The actual load address from evaluating the expression.
2172 uint64_t load_address_value_
;
2173 // The segment itself.
2174 Output_segment
* segment_
;
2177 // Print for debugging.
2180 Phdrs_element::print(FILE* f
) const
2182 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2183 if (this->includes_filehdr_
)
2184 fprintf(f
, " FILEHDR");
2185 if (this->includes_phdrs_
)
2186 fprintf(f
, " PHDRS");
2187 if (this->is_flags_valid_
)
2188 fprintf(f
, " FLAGS(%u)", this->flags_
);
2189 if (this->load_address_
!= NULL
)
2192 this->load_address_
->print(f
);
2198 // Class Script_sections.
2200 Script_sections::Script_sections()
2201 : saw_sections_clause_(false),
2202 in_sections_clause_(false),
2203 sections_elements_(NULL
),
2204 output_section_(NULL
),
2205 phdrs_elements_(NULL
)
2209 // Start a SECTIONS clause.
2212 Script_sections::start_sections()
2214 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2215 this->saw_sections_clause_
= true;
2216 this->in_sections_clause_
= true;
2217 if (this->sections_elements_
== NULL
)
2218 this->sections_elements_
= new Sections_elements
;
2221 // Finish a SECTIONS clause.
2224 Script_sections::finish_sections()
2226 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2227 this->in_sections_clause_
= false;
2230 // Add a symbol to be defined.
2233 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2234 Expression
* val
, bool provide
,
2237 if (this->output_section_
!= NULL
)
2238 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2242 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2245 this->sections_elements_
->push_back(p
);
2249 // Add an assignment to the special dot symbol.
2252 Script_sections::add_dot_assignment(Expression
* val
)
2254 if (this->output_section_
!= NULL
)
2255 this->output_section_
->add_dot_assignment(val
);
2258 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2259 this->sections_elements_
->push_back(p
);
2263 // Add an assertion.
2266 Script_sections::add_assertion(Expression
* check
, const char* message
,
2269 if (this->output_section_
!= NULL
)
2270 this->output_section_
->add_assertion(check
, message
, messagelen
);
2273 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2275 this->sections_elements_
->push_back(p
);
2279 // Start processing entries for an output section.
2282 Script_sections::start_output_section(
2285 const Parser_output_section_header
*header
)
2287 Output_section_definition
* posd
= new Output_section_definition(name
,
2290 this->sections_elements_
->push_back(posd
);
2291 gold_assert(this->output_section_
== NULL
);
2292 this->output_section_
= posd
;
2295 // Stop processing entries for an output section.
2298 Script_sections::finish_output_section(
2299 const Parser_output_section_trailer
* trailer
)
2301 gold_assert(this->output_section_
!= NULL
);
2302 this->output_section_
->finish(trailer
);
2303 this->output_section_
= NULL
;
2306 // Add a data item to the current output section.
2309 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2311 gold_assert(this->output_section_
!= NULL
);
2312 this->output_section_
->add_data(size
, is_signed
, val
);
2315 // Add a fill value setting to the current output section.
2318 Script_sections::add_fill(Expression
* val
)
2320 gold_assert(this->output_section_
!= NULL
);
2321 this->output_section_
->add_fill(val
);
2324 // Add an input section specification to the current output section.
2327 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2329 gold_assert(this->output_section_
!= NULL
);
2330 this->output_section_
->add_input_section(spec
, keep
);
2333 // Create any required sections.
2336 Script_sections::create_sections(Layout
* layout
)
2338 if (!this->saw_sections_clause_
)
2340 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2341 p
!= this->sections_elements_
->end();
2343 (*p
)->create_sections(layout
);
2346 // Add any symbols we are defining to the symbol table.
2349 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2351 if (!this->saw_sections_clause_
)
2353 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2354 p
!= this->sections_elements_
->end();
2356 (*p
)->add_symbols_to_table(symtab
);
2359 // Finalize symbols and check assertions.
2362 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2364 if (!this->saw_sections_clause_
)
2366 uint64_t dot_value
= 0;
2367 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2368 p
!= this->sections_elements_
->end();
2370 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2373 // Return the name of the output section to use for an input file name
2374 // and section name.
2377 Script_sections::output_section_name(const char* file_name
,
2378 const char* section_name
,
2379 Output_section
*** output_section_slot
)
2381 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2382 p
!= this->sections_elements_
->end();
2385 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2386 output_section_slot
);
2390 // The special name /DISCARD/ means that the input section
2391 // should be discarded.
2392 if (strcmp(ret
, "/DISCARD/") == 0)
2394 *output_section_slot
= NULL
;
2401 // If we couldn't find a mapping for the name, the output section
2402 // gets the name of the input section.
2404 *output_section_slot
= NULL
;
2406 return section_name
;
2409 // Place a marker for an orphan output section into the SECTIONS
2413 Script_sections::place_orphan(Output_section
* os
)
2415 // Look for an output section definition which matches the output
2416 // section. Put a marker after that section.
2417 Sections_elements::iterator place
= this->sections_elements_
->end();
2418 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2419 p
!= this->sections_elements_
->end();
2423 if ((*p
)->place_orphan_here(os
, &exact
))
2431 // The insert function puts the new element before the iterator.
2432 if (place
!= this->sections_elements_
->end())
2435 this->sections_elements_
->insert(place
, new Orphan_output_section(os
));
2438 // Set the addresses of all the output sections. Walk through all the
2439 // elements, tracking the dot symbol. Apply assignments which set
2440 // absolute symbol values, in case they are used when setting dot.
2441 // Fill in data statement values. As we find output sections, set the
2442 // address, set the address of all associated input sections, and
2443 // update dot. Return the segment which should hold the file header
2444 // and segment headers, if any.
2447 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2449 gold_assert(this->saw_sections_clause_
);
2451 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2452 // for our representation.
2453 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2454 p
!= this->sections_elements_
->end();
2457 Output_section_definition
* posd
;
2458 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2459 if (failed_constraint
!= CONSTRAINT_NONE
)
2461 Sections_elements::iterator q
;
2462 for (q
= this->sections_elements_
->begin();
2463 q
!= this->sections_elements_
->end();
2468 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2473 if (q
== this->sections_elements_
->end())
2474 gold_error(_("no matching section constraint"));
2478 // For a relocatable link, we implicitly set dot to zero.
2479 uint64_t dot_value
= 0;
2480 uint64_t load_address
= 0;
2481 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2482 p
!= this->sections_elements_
->end();
2484 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
2486 if (this->phdrs_elements_
!= NULL
)
2488 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2489 p
!= this->phdrs_elements_
->end();
2491 (*p
)->eval_load_address(symtab
, layout
);
2494 return this->create_segments(layout
);
2497 // Sort the sections in order to put them into segments.
2499 class Sort_output_sections
2503 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2507 Sort_output_sections::operator()(const Output_section
* os1
,
2508 const Output_section
* os2
) const
2510 // Sort first by the load address.
2511 uint64_t lma1
= (os1
->has_load_address()
2512 ? os1
->load_address()
2514 uint64_t lma2
= (os2
->has_load_address()
2515 ? os2
->load_address()
2520 // Then sort by the virtual address.
2521 if (os1
->address() != os2
->address())
2522 return os1
->address() < os2
->address();
2524 // Sort TLS sections to the end.
2525 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2526 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2530 // Sort PROGBITS before NOBITS.
2531 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2533 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2536 // Otherwise we don't care.
2540 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2541 // We treat a section with the SHF_TLS flag set as taking up space
2542 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2543 // space for them in the file.
2546 Script_sections::is_bss_section(const Output_section
* os
)
2548 return (os
->type() == elfcpp::SHT_NOBITS
2549 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2552 // Return the size taken by the file header and the program headers.
2555 Script_sections::total_header_size(Layout
* layout
) const
2557 size_t segment_count
= layout
->segment_count();
2558 size_t file_header_size
;
2559 size_t segment_headers_size
;
2560 if (parameters
->target().get_size() == 32)
2562 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2563 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2565 else if (parameters
->target().get_size() == 64)
2567 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2568 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2573 return file_header_size
+ segment_headers_size
;
2576 // Return the amount we have to subtract from the LMA to accomodate
2577 // headers of the given size. The complication is that the file
2578 // header have to be at the start of a page, as otherwise it will not
2579 // be at the start of the file.
2582 Script_sections::header_size_adjustment(uint64_t lma
,
2583 size_t sizeof_headers
) const
2585 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2586 uint64_t hdr_lma
= lma
- sizeof_headers
;
2587 hdr_lma
&= ~(abi_pagesize
- 1);
2588 return lma
- hdr_lma
;
2591 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2592 // the segment which should hold the file header and segment headers,
2596 Script_sections::create_segments(Layout
* layout
)
2598 gold_assert(this->saw_sections_clause_
);
2600 if (parameters
->options().relocatable())
2603 if (this->saw_phdrs_clause())
2604 return create_segments_from_phdrs_clause(layout
);
2606 Layout::Section_list sections
;
2607 layout
->get_allocated_sections(§ions
);
2609 // Sort the sections by address.
2610 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2612 this->create_note_and_tls_segments(layout
, §ions
);
2614 // Walk through the sections adding them to PT_LOAD segments.
2615 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2616 Output_segment
* first_seg
= NULL
;
2617 Output_segment
* current_seg
= NULL
;
2618 bool is_current_seg_readonly
= true;
2619 Layout::Section_list::iterator plast
= sections
.end();
2620 uint64_t last_vma
= 0;
2621 uint64_t last_lma
= 0;
2622 uint64_t last_size
= 0;
2623 for (Layout::Section_list::iterator p
= sections
.begin();
2624 p
!= sections
.end();
2627 const uint64_t vma
= (*p
)->address();
2628 const uint64_t lma
= ((*p
)->has_load_address()
2629 ? (*p
)->load_address()
2631 const uint64_t size
= (*p
)->current_data_size();
2633 bool need_new_segment
;
2634 if (current_seg
== NULL
)
2635 need_new_segment
= true;
2636 else if (lma
- vma
!= last_lma
- last_vma
)
2638 // This section has a different LMA relationship than the
2639 // last one; we need a new segment.
2640 need_new_segment
= true;
2642 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2643 < align_address(lma
, abi_pagesize
))
2645 // Putting this section in the segment would require
2647 need_new_segment
= true;
2649 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2651 // A non-BSS section can not follow a BSS section in the
2653 need_new_segment
= true;
2655 else if (is_current_seg_readonly
2656 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2658 // Don't put a writable section in the same segment as a
2659 // non-writable section.
2660 need_new_segment
= true;
2664 // Otherwise, reuse the existing segment.
2665 need_new_segment
= false;
2668 elfcpp::Elf_Word seg_flags
=
2669 Layout::section_flags_to_segment((*p
)->flags());
2671 if (need_new_segment
)
2673 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2675 current_seg
->set_addresses(vma
, lma
);
2676 if (first_seg
== NULL
)
2677 first_seg
= current_seg
;
2678 is_current_seg_readonly
= true;
2681 current_seg
->add_output_section(*p
, seg_flags
);
2683 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2684 is_current_seg_readonly
= false;
2692 // An ELF program should work even if the program headers are not in
2693 // a PT_LOAD segment. However, it appears that the Linux kernel
2694 // does not set the AT_PHDR auxiliary entry in that case. It sets
2695 // the load address to p_vaddr - p_offset of the first PT_LOAD
2696 // segment. It then sets AT_PHDR to the load address plus the
2697 // offset to the program headers, e_phoff in the file header. This
2698 // fails when the program headers appear in the file before the
2699 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2700 // segment to hold the file header and the program headers. This is
2701 // effectively what the GNU linker does, and it is slightly more
2702 // efficient in any case. We try to use the first PT_LOAD segment
2703 // if we can, otherwise we make a new one.
2705 if (first_seg
== NULL
)
2708 size_t sizeof_headers
= this->total_header_size(layout
);
2710 if ((first_seg
->paddr() & (abi_pagesize
- 1)) >= sizeof_headers
)
2712 first_seg
->set_addresses(first_seg
->vaddr() - sizeof_headers
,
2713 first_seg
->paddr() - sizeof_headers
);
2717 uint64_t vma
= first_seg
->vaddr();
2718 uint64_t lma
= first_seg
->paddr();
2720 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
2722 // If there is no room to squeeze in the headers, then punt. The
2723 // resulting executable probably won't run on GNU/Linux, but we
2724 // trust that the user knows what they are doing.
2725 if (lma
< subtract
|| vma
< subtract
)
2728 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2730 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
2735 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2736 // segment if there are any SHT_TLS sections.
2739 Script_sections::create_note_and_tls_segments(
2741 const Layout::Section_list
* sections
)
2743 gold_assert(!this->saw_phdrs_clause());
2745 bool saw_tls
= false;
2746 for (Layout::Section_list::const_iterator p
= sections
->begin();
2747 p
!= sections
->end();
2750 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2752 elfcpp::Elf_Word seg_flags
=
2753 Layout::section_flags_to_segment((*p
)->flags());
2754 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
2756 oseg
->add_output_section(*p
, seg_flags
);
2758 // Incorporate any subsequent SHT_NOTE sections, in the
2759 // hopes that the script is sensible.
2760 Layout::Section_list::const_iterator pnext
= p
+ 1;
2761 while (pnext
!= sections
->end()
2762 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
2764 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2765 oseg
->add_output_section(*pnext
, seg_flags
);
2771 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2774 gold_error(_("TLS sections are not adjacent"));
2776 elfcpp::Elf_Word seg_flags
=
2777 Layout::section_flags_to_segment((*p
)->flags());
2778 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
2780 oseg
->add_output_section(*p
, seg_flags
);
2782 Layout::Section_list::const_iterator pnext
= p
+ 1;
2783 while (pnext
!= sections
->end()
2784 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
2786 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2787 oseg
->add_output_section(*pnext
, seg_flags
);
2797 // Add a program header. The PHDRS clause is syntactically distinct
2798 // from the SECTIONS clause, but we implement it with the SECTIONS
2799 // support becauase PHDRS is useless if there is no SECTIONS clause.
2802 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
2803 bool includes_filehdr
, bool includes_phdrs
,
2804 bool is_flags_valid
, unsigned int flags
,
2805 Expression
* load_address
)
2807 if (this->phdrs_elements_
== NULL
)
2808 this->phdrs_elements_
= new Phdrs_elements();
2809 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
2812 is_flags_valid
, flags
,
2816 // Return the number of segments we expect to create based on the
2817 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2820 Script_sections::expected_segment_count(const Layout
* layout
) const
2822 if (this->saw_phdrs_clause())
2823 return this->phdrs_elements_
->size();
2825 Layout::Section_list sections
;
2826 layout
->get_allocated_sections(§ions
);
2828 // We assume that we will need two PT_LOAD segments.
2831 bool saw_note
= false;
2832 bool saw_tls
= false;
2833 for (Layout::Section_list::const_iterator p
= sections
.begin();
2834 p
!= sections
.end();
2837 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2839 // Assume that all note sections will fit into a single
2847 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2849 // There can only be one PT_TLS segment.
2861 // Create the segments from a PHDRS clause. Return the segment which
2862 // should hold the file header and program headers, if any.
2865 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
2867 this->attach_sections_using_phdrs_clause(layout
);
2868 return this->set_phdrs_clause_addresses(layout
);
2871 // Create the segments from the PHDRS clause, and put the output
2872 // sections in them.
2875 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
2877 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
2878 Name_to_segment name_to_segment
;
2879 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2880 p
!= this->phdrs_elements_
->end();
2882 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
2884 // Walk through the output sections and attach them to segments.
2885 // Output sections in the script which do not list segments are
2886 // attached to the same set of segments as the immediately preceding
2888 String_list
* phdr_names
= NULL
;
2889 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2890 p
!= this->sections_elements_
->end();
2893 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
);
2897 if (phdr_names
== NULL
)
2899 gold_error(_("allocated section not in any segment"));
2903 bool in_load_segment
= false;
2904 for (String_list::const_iterator q
= phdr_names
->begin();
2905 q
!= phdr_names
->end();
2908 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
2909 if (r
== name_to_segment
.end())
2910 gold_error(_("no segment %s"), q
->c_str());
2913 elfcpp::Elf_Word seg_flags
=
2914 Layout::section_flags_to_segment(os
->flags());
2915 r
->second
->add_output_section(os
, seg_flags
);
2917 if (r
->second
->type() == elfcpp::PT_LOAD
)
2919 if (in_load_segment
)
2920 gold_error(_("section in two PT_LOAD segments"));
2921 in_load_segment
= true;
2926 if (!in_load_segment
)
2927 gold_error(_("allocated section not in any PT_LOAD segment"));
2931 // Set the addresses for segments created from a PHDRS clause. Return
2932 // the segment which should hold the file header and program headers,
2936 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
2938 Output_segment
* load_seg
= NULL
;
2939 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2940 p
!= this->phdrs_elements_
->end();
2943 // Note that we have to set the flags after adding the output
2944 // sections to the segment, as adding an output segment can
2945 // change the flags.
2946 (*p
)->set_flags_if_valid();
2948 Output_segment
* oseg
= (*p
)->segment();
2950 if (oseg
->type() != elfcpp::PT_LOAD
)
2952 // The addresses of non-PT_LOAD segments are set from the
2953 // PT_LOAD segments.
2954 if ((*p
)->has_load_address())
2955 gold_error(_("may only specify load address for PT_LOAD segment"));
2959 // The output sections should have addresses from the SECTIONS
2960 // clause. The addresses don't have to be in order, so find the
2961 // one with the lowest load address. Use that to set the
2962 // address of the segment.
2964 Output_section
* osec
= oseg
->section_with_lowest_load_address();
2967 oseg
->set_addresses(0, 0);
2971 uint64_t vma
= osec
->address();
2972 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
2974 // Override the load address of the section with the load
2975 // address specified for the segment.
2976 if ((*p
)->has_load_address())
2978 if (osec
->has_load_address())
2979 gold_warning(_("PHDRS load address overrides "
2980 "section %s load address"),
2983 lma
= (*p
)->load_address();
2986 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
2987 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
2989 // We could support this if we wanted to.
2990 gold_error(_("using only one of FILEHDR and PHDRS is "
2991 "not currently supported"));
2995 size_t sizeof_headers
= this->total_header_size(layout
);
2996 uint64_t subtract
= this->header_size_adjustment(lma
,
2998 if (lma
>= subtract
&& vma
>= subtract
)
3005 gold_error(_("sections loaded on first page without room "
3006 "for file and program headers "
3007 "are not supported"));
3010 if (load_seg
!= NULL
)
3011 gold_error(_("using FILEHDR and PHDRS on more than one "
3012 "PT_LOAD segment is not currently supported"));
3016 oseg
->set_addresses(vma
, lma
);
3022 // Add the file header and segment headers to non-load segments
3023 // specified in the PHDRS clause.
3026 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3027 Output_data
* segment_headers
)
3029 gold_assert(this->saw_phdrs_clause());
3030 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3031 p
!= this->phdrs_elements_
->end();
3034 if ((*p
)->type() != elfcpp::PT_LOAD
)
3036 if ((*p
)->includes_phdrs())
3037 (*p
)->segment()->add_initial_output_data(segment_headers
);
3038 if ((*p
)->includes_filehdr())
3039 (*p
)->segment()->add_initial_output_data(file_header
);
3044 // Look for an output section by name and return the address, the load
3045 // address, the alignment, and the size. This is used when an
3046 // expression refers to an output section which was not actually
3047 // created. This returns true if the section was found, false
3051 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3052 uint64_t* load_address
,
3053 uint64_t* addralign
,
3054 uint64_t* size
) const
3056 if (!this->saw_sections_clause_
)
3058 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3059 p
!= this->sections_elements_
->end();
3061 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3067 // Print the SECTIONS clause to F for debugging.
3070 Script_sections::print(FILE* f
) const
3072 if (!this->saw_sections_clause_
)
3075 fprintf(f
, "SECTIONS {\n");
3077 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3078 p
!= this->sections_elements_
->end();
3084 if (this->phdrs_elements_
!= NULL
)
3086 fprintf(f
, "PHDRS {\n");
3087 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3088 p
!= this->phdrs_elements_
->end();
3095 } // End namespace gold.