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 (the first parameter) to
109 // the list of PHDRS to which it should be attached. If the PHDRS
110 // were not specified, don't change *PHDRS_LIST. When not returning
111 // NULL, set *ORPHAN (the second parameter) according to whether
112 // this is an orphan section--one that is not mentioned in the
114 virtual Output_section
*
115 allocate_to_segment(String_list
**, bool*)
118 // Look for an output section by name and return the address, the
119 // load address, the alignment, and the size. This is used when an
120 // expression refers to an output section which was not actually
121 // created. This returns true if the section was found, false
122 // otherwise. The only real definition is for
123 // Output_section_definition.
125 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
129 // Print the element for debugging purposes.
131 print(FILE* f
) const = 0;
134 // An assignment in a SECTIONS clause outside of an output section.
136 class Sections_element_assignment
: public Sections_element
139 Sections_element_assignment(const char* name
, size_t namelen
,
140 Expression
* val
, bool provide
, bool hidden
)
141 : assignment_(name
, namelen
, val
, provide
, hidden
)
144 // Add the symbol to the symbol table.
146 add_symbols_to_table(Symbol_table
* symtab
)
147 { this->assignment_
.add_to_table(symtab
); }
149 // Finalize the symbol.
151 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
154 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
157 // Set the section address. There is no section here, but if the
158 // value is absolute, we set the symbol. This permits us to use
159 // absolute symbols when setting dot.
161 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
162 uint64_t* dot_value
, uint64_t*)
164 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
167 // Print for debugging.
172 this->assignment_
.print(f
);
176 Symbol_assignment assignment_
;
179 // An assignment to the dot symbol in a SECTIONS clause outside of an
182 class Sections_element_dot_assignment
: public Sections_element
185 Sections_element_dot_assignment(Expression
* val
)
189 // Finalize the symbol.
191 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
194 // We ignore the section of the result because outside of an
195 // output section definition the dot symbol is always considered
197 Output_section
* dummy
;
198 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
202 // Update the dot symbol while setting section addresses.
204 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
205 uint64_t* dot_value
, uint64_t* load_address
)
207 Output_section
* dummy
;
208 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
210 *load_address
= *dot_value
;
213 // Print for debugging.
218 this->val_
->print(f
);
226 // An assertion in a SECTIONS clause outside of an output section.
228 class Sections_element_assertion
: public Sections_element
231 Sections_element_assertion(Expression
* check
, const char* message
,
233 : assertion_(check
, message
, messagelen
)
236 // Check the assertion.
238 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
239 { this->assertion_
.check(symtab
, layout
); }
241 // Print for debugging.
246 this->assertion_
.print(f
);
250 Script_assertion assertion_
;
253 // An element in an output section in a SECTIONS clause.
255 class Output_section_element
258 // A list of input sections.
259 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
261 Output_section_element()
264 virtual ~Output_section_element()
267 // Return whether this element requires an output section to exist.
269 needs_output_section() const
272 // Add any symbol being defined to the symbol table.
274 add_symbols_to_table(Symbol_table
*)
277 // Finalize symbols and check assertions.
279 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
282 // Return whether this element matches FILE_NAME and SECTION_NAME.
283 // The only real implementation is in Output_section_element_input.
285 match_name(const char*, const char*) const
288 // Set section addresses. This includes applying assignments if the
289 // the expression is an absolute value.
291 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
292 uint64_t*, Output_section
**, std::string
*,
296 // Print the element for debugging purposes.
298 print(FILE* f
) const = 0;
301 // Return a fill string that is LENGTH bytes long, filling it with
304 get_fill_string(const std::string
* fill
, section_size_type length
) const;
308 Output_section_element::get_fill_string(const std::string
* fill
,
309 section_size_type length
) const
311 std::string this_fill
;
312 this_fill
.reserve(length
);
313 while (this_fill
.length() + fill
->length() <= length
)
315 if (this_fill
.length() < length
)
316 this_fill
.append(*fill
, 0, length
- this_fill
.length());
320 // A symbol assignment in an output section.
322 class Output_section_element_assignment
: public Output_section_element
325 Output_section_element_assignment(const char* name
, size_t namelen
,
326 Expression
* val
, bool provide
,
328 : assignment_(name
, namelen
, val
, provide
, hidden
)
331 // Add the symbol to the symbol table.
333 add_symbols_to_table(Symbol_table
* symtab
)
334 { this->assignment_
.add_to_table(symtab
); }
336 // Finalize the symbol.
338 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
339 uint64_t* dot_value
, Output_section
** dot_section
)
341 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
345 // Set the section address. There is no section here, but if the
346 // value is absolute, we set the symbol. This permits us to use
347 // absolute symbols when setting dot.
349 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
350 uint64_t, uint64_t* dot_value
, Output_section
**,
351 std::string
*, Input_section_list
*)
353 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
356 // Print for debugging.
361 this->assignment_
.print(f
);
365 Symbol_assignment assignment_
;
368 // An assignment to the dot symbol in an output section.
370 class Output_section_element_dot_assignment
: public Output_section_element
373 Output_section_element_dot_assignment(Expression
* val
)
377 // Finalize the symbol.
379 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
380 uint64_t* dot_value
, Output_section
** dot_section
)
382 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
383 *dot_section
, dot_section
);
386 // Update the dot symbol while setting section addresses.
388 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
389 uint64_t, uint64_t* dot_value
, Output_section
**,
390 std::string
*, Input_section_list
*);
392 // Print for debugging.
397 this->val_
->print(f
);
405 // Update the dot symbol while setting section addresses.
408 Output_section_element_dot_assignment::set_section_addresses(
409 Symbol_table
* symtab
,
411 Output_section
* output_section
,
414 Output_section
** dot_section
,
418 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
419 *dot_value
, *dot_section
,
421 if (next_dot
< *dot_value
)
422 gold_error(_("dot may not move backward"));
423 if (next_dot
> *dot_value
&& output_section
!= NULL
)
425 section_size_type length
= convert_to_section_size_type(next_dot
427 Output_section_data
* posd
;
429 posd
= new Output_data_fixed_space(length
, 0);
432 std::string this_fill
= this->get_fill_string(fill
, length
);
433 posd
= new Output_data_const(this_fill
, 0);
435 output_section
->add_output_section_data(posd
);
437 *dot_value
= next_dot
;
440 // An assertion in an output section.
442 class Output_section_element_assertion
: public Output_section_element
445 Output_section_element_assertion(Expression
* check
, const char* message
,
447 : assertion_(check
, message
, messagelen
)
454 this->assertion_
.print(f
);
458 Script_assertion assertion_
;
461 // We use a special instance of Output_section_data to handle BYTE,
462 // SHORT, etc. This permits forward references to symbols in the
465 class Output_data_expression
: public Output_section_data
468 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
469 const Symbol_table
* symtab
, const Layout
* layout
,
470 uint64_t dot_value
, Output_section
* dot_section
)
471 : Output_section_data(size
, 0),
472 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
473 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
477 // Write the data to the output file.
479 do_write(Output_file
*);
481 // Write the data to a buffer.
483 do_write_to_buffer(unsigned char*);
486 template<bool big_endian
>
488 endian_write_to_buffer(uint64_t, unsigned char*);
492 const Symbol_table
* symtab_
;
493 const Layout
* layout_
;
495 Output_section
* dot_section_
;
498 // Write the data element to the output file.
501 Output_data_expression::do_write(Output_file
* of
)
503 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
504 this->write_to_buffer(view
);
505 of
->write_output_view(this->offset(), this->data_size(), view
);
508 // Write the data element to a buffer.
511 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
513 Output_section
* dummy
;
514 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
515 true, this->dot_value_
,
516 this->dot_section_
, &dummy
);
518 if (parameters
->target().is_big_endian())
519 this->endian_write_to_buffer
<true>(val
, buf
);
521 this->endian_write_to_buffer
<false>(val
, buf
);
524 template<bool big_endian
>
526 Output_data_expression::endian_write_to_buffer(uint64_t val
,
529 switch (this->data_size())
532 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
535 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
538 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
541 if (parameters
->target().get_size() == 32)
544 if (this->is_signed_
&& (val
& 0x80000000) != 0)
545 val
|= 0xffffffff00000000LL
;
547 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
554 // A data item in an output section.
556 class Output_section_element_data
: public Output_section_element
559 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
560 : size_(size
), is_signed_(is_signed
), val_(val
)
563 // If there is a data item, then we must create an output section.
565 needs_output_section() const
568 // Finalize symbols--we just need to update dot.
570 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
572 { *dot_value
+= this->size_
; }
574 // Store the value in the section.
576 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
577 uint64_t* dot_value
, Output_section
**, std::string
*,
578 Input_section_list
*);
580 // Print for debugging.
585 // The size in bytes.
587 // Whether the value is signed.
593 // Store the value in the section.
596 Output_section_element_data::set_section_addresses(
597 Symbol_table
* symtab
,
602 Output_section
** dot_section
,
606 gold_assert(os
!= NULL
);
607 os
->add_output_section_data(new Output_data_expression(this->size_
,
614 *dot_value
+= this->size_
;
617 // Print for debugging.
620 Output_section_element_data::print(FILE* f
) const
635 if (this->is_signed_
)
643 fprintf(f
, " %s(", s
);
644 this->val_
->print(f
);
648 // A fill value setting in an output section.
650 class Output_section_element_fill
: public Output_section_element
653 Output_section_element_fill(Expression
* val
)
657 // Update the fill value while setting section addresses.
659 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
660 uint64_t, uint64_t* dot_value
,
661 Output_section
** dot_section
,
662 std::string
* fill
, Input_section_list
*)
664 Output_section
* fill_section
;
665 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
666 *dot_value
, *dot_section
,
668 if (fill_section
!= NULL
)
669 gold_warning(_("fill value is not absolute"));
670 // FIXME: The GNU linker supports fill values of arbitrary length.
671 unsigned char fill_buff
[4];
672 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
673 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
676 // Print for debugging.
680 fprintf(f
, " FILL(");
681 this->val_
->print(f
);
686 // The new fill value.
690 // Return whether STRING contains a wildcard character. This is used
691 // to speed up matching.
694 is_wildcard_string(const std::string
& s
)
696 return strpbrk(s
.c_str(), "?*[") != NULL
;
699 // An input section specification in an output section
701 class Output_section_element_input
: public Output_section_element
704 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
706 // Finalize symbols--just update the value of the dot symbol.
708 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
709 Output_section
** dot_section
)
711 *dot_value
= this->final_dot_value_
;
712 *dot_section
= this->final_dot_section_
;
715 // See whether we match FILE_NAME and SECTION_NAME as an input
718 match_name(const char* file_name
, const char* section_name
) const;
720 // Set the section address.
722 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
723 uint64_t subalign
, uint64_t* dot_value
,
724 Output_section
**, std::string
* fill
,
725 Input_section_list
*);
727 // Print for debugging.
729 print(FILE* f
) const;
732 // An input section pattern.
733 struct Input_section_pattern
736 bool pattern_is_wildcard
;
739 Input_section_pattern(const char* patterna
, size_t patternlena
,
741 : pattern(patterna
, patternlena
),
742 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
747 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
749 // Filename_exclusions is a pair of filename pattern and a bool
750 // indicating whether the filename is a wildcard.
751 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
753 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
754 // indicates whether this is a wildcard pattern.
756 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
758 return (is_wildcard_pattern
759 ? fnmatch(pattern
, string
, 0) == 0
760 : strcmp(string
, pattern
) == 0);
763 // See if we match a file name.
765 match_file_name(const char* file_name
) const;
767 // The file name pattern. If this is the empty string, we match all
769 std::string filename_pattern_
;
770 // Whether the file name pattern is a wildcard.
771 bool filename_is_wildcard_
;
772 // How the file names should be sorted. This may only be
773 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
774 Sort_wildcard filename_sort_
;
775 // The list of file names to exclude.
776 Filename_exclusions filename_exclusions_
;
777 // The list of input section patterns.
778 Input_section_patterns input_section_patterns_
;
779 // Whether to keep this section when garbage collecting.
781 // The value of dot after including all matching sections.
782 uint64_t final_dot_value_
;
783 // The section where dot is defined after including all matching
785 Output_section
* final_dot_section_
;
788 // Construct Output_section_element_input. The parser records strings
789 // as pointers into a copy of the script file, which will go away when
790 // parsing is complete. We make sure they are in std::string objects.
792 Output_section_element_input::Output_section_element_input(
793 const Input_section_spec
* spec
,
795 : filename_pattern_(),
796 filename_is_wildcard_(false),
797 filename_sort_(spec
->file
.sort
),
798 filename_exclusions_(),
799 input_section_patterns_(),
802 final_dot_section_(NULL
)
804 // The filename pattern "*" is common, and matches all files. Turn
805 // it into the empty string.
806 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
807 this->filename_pattern_
.assign(spec
->file
.name
.value
,
808 spec
->file
.name
.length
);
809 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
811 if (spec
->input_sections
.exclude
!= NULL
)
813 for (String_list::const_iterator p
=
814 spec
->input_sections
.exclude
->begin();
815 p
!= spec
->input_sections
.exclude
->end();
818 bool is_wildcard
= is_wildcard_string(*p
);
819 this->filename_exclusions_
.push_back(std::make_pair(*p
,
824 if (spec
->input_sections
.sections
!= NULL
)
826 Input_section_patterns
& isp(this->input_section_patterns_
);
827 for (String_sort_list::const_iterator p
=
828 spec
->input_sections
.sections
->begin();
829 p
!= spec
->input_sections
.sections
->end();
831 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
836 // See whether we match FILE_NAME.
839 Output_section_element_input::match_file_name(const char* file_name
) const
841 if (!this->filename_pattern_
.empty())
843 // If we were called with no filename, we refuse to match a
844 // pattern which requires a file name.
845 if (file_name
== NULL
)
848 if (!match(file_name
, this->filename_pattern_
.c_str(),
849 this->filename_is_wildcard_
))
853 if (file_name
!= NULL
)
855 // Now we have to see whether FILE_NAME matches one of the
856 // exclusion patterns, if any.
857 for (Filename_exclusions::const_iterator p
=
858 this->filename_exclusions_
.begin();
859 p
!= this->filename_exclusions_
.end();
862 if (match(file_name
, p
->first
.c_str(), p
->second
))
870 // See whether we match FILE_NAME and SECTION_NAME.
873 Output_section_element_input::match_name(const char* file_name
,
874 const char* section_name
) const
876 if (!this->match_file_name(file_name
))
879 // If there are no section name patterns, then we match.
880 if (this->input_section_patterns_
.empty())
883 // See whether we match the section name patterns.
884 for (Input_section_patterns::const_iterator p
=
885 this->input_section_patterns_
.begin();
886 p
!= this->input_section_patterns_
.end();
889 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
893 // We didn't match any section names, so we didn't match.
897 // Information we use to sort the input sections.
899 struct Input_section_info
903 std::string section_name
;
908 // A class to sort the input sections.
910 class Input_section_sorter
913 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
914 : filename_sort_(filename_sort
), section_sort_(section_sort
)
918 operator()(const Input_section_info
&, const Input_section_info
&) const;
921 Sort_wildcard filename_sort_
;
922 Sort_wildcard section_sort_
;
926 Input_section_sorter::operator()(const Input_section_info
& isi1
,
927 const Input_section_info
& isi2
) const
929 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
930 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
931 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
932 && isi1
.addralign
== isi2
.addralign
))
934 if (isi1
.section_name
!= isi2
.section_name
)
935 return isi1
.section_name
< isi2
.section_name
;
937 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
938 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
939 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
941 if (isi1
.addralign
!= isi2
.addralign
)
942 return isi1
.addralign
< isi2
.addralign
;
944 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
946 if (isi1
.relobj
->name() != isi2
.relobj
->name())
947 return isi1
.relobj
->name() < isi2
.relobj
->name();
950 // Otherwise we leave them in the same order.
954 // Set the section address. Look in INPUT_SECTIONS for sections which
955 // match this spec, sort them as specified, and add them to the output
959 Output_section_element_input::set_section_addresses(
962 Output_section
* output_section
,
965 Output_section
** dot_section
,
967 Input_section_list
* input_sections
)
969 // We build a list of sections which match each
970 // Input_section_pattern.
972 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
973 size_t input_pattern_count
= this->input_section_patterns_
.size();
974 if (input_pattern_count
== 0)
975 input_pattern_count
= 1;
976 Matching_sections
matching_sections(input_pattern_count
);
978 // Look through the list of sections for this output section. Add
979 // each one which matches to one of the elements of
980 // MATCHING_SECTIONS.
982 Input_section_list::iterator p
= input_sections
->begin();
983 while (p
!= input_sections
->end())
985 // Calling section_name and section_addralign is not very
987 Input_section_info isi
;
988 isi
.relobj
= p
->first
;
989 isi
.shndx
= p
->second
;
991 // Lock the object so that we can get information about the
992 // section. This is OK since we know we are single-threaded
995 const Task
* task
= reinterpret_cast<const Task
*>(-1);
996 Task_lock_obj
<Object
> tl(task
, p
->first
);
998 isi
.section_name
= p
->first
->section_name(p
->second
);
999 isi
.size
= p
->first
->section_size(p
->second
);
1000 isi
.addralign
= p
->first
->section_addralign(p
->second
);
1003 if (!this->match_file_name(isi
.relobj
->name().c_str()))
1005 else if (this->input_section_patterns_
.empty())
1007 matching_sections
[0].push_back(isi
);
1008 p
= input_sections
->erase(p
);
1013 for (i
= 0; i
< input_pattern_count
; ++i
)
1015 const Input_section_pattern
&
1016 isp(this->input_section_patterns_
[i
]);
1017 if (match(isi
.section_name
.c_str(), isp
.pattern
.c_str(),
1018 isp
.pattern_is_wildcard
))
1022 if (i
>= this->input_section_patterns_
.size())
1026 matching_sections
[i
].push_back(isi
);
1027 p
= input_sections
->erase(p
);
1032 // Look through MATCHING_SECTIONS. Sort each one as specified,
1033 // using a stable sort so that we get the default order when
1034 // sections are otherwise equal. Add each input section to the
1037 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1039 if (matching_sections
[i
].empty())
1042 gold_assert(output_section
!= NULL
);
1044 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1045 if (isp
.sort
!= SORT_WILDCARD_NONE
1046 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1047 std::stable_sort(matching_sections
[i
].begin(),
1048 matching_sections
[i
].end(),
1049 Input_section_sorter(this->filename_sort_
,
1052 for (std::vector
<Input_section_info
>::const_iterator p
=
1053 matching_sections
[i
].begin();
1054 p
!= matching_sections
[i
].end();
1057 uint64_t this_subalign
= p
->addralign
;
1058 if (this_subalign
< subalign
)
1059 this_subalign
= subalign
;
1061 uint64_t address
= align_address(*dot_value
, this_subalign
);
1063 if (address
> *dot_value
&& !fill
->empty())
1065 section_size_type length
=
1066 convert_to_section_size_type(address
- *dot_value
);
1067 std::string this_fill
= this->get_fill_string(fill
, length
);
1068 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1069 output_section
->add_output_section_data(posd
);
1072 output_section
->add_input_section_for_script(p
->relobj
,
1077 *dot_value
= address
+ p
->size
;
1081 this->final_dot_value_
= *dot_value
;
1082 this->final_dot_section_
= *dot_section
;
1085 // Print for debugging.
1088 Output_section_element_input::print(FILE* f
) const
1093 fprintf(f
, "KEEP(");
1095 if (!this->filename_pattern_
.empty())
1097 bool need_close_paren
= false;
1098 switch (this->filename_sort_
)
1100 case SORT_WILDCARD_NONE
:
1102 case SORT_WILDCARD_BY_NAME
:
1103 fprintf(f
, "SORT_BY_NAME(");
1104 need_close_paren
= true;
1110 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1112 if (need_close_paren
)
1116 if (!this->input_section_patterns_
.empty()
1117 || !this->filename_exclusions_
.empty())
1121 bool need_space
= false;
1122 if (!this->filename_exclusions_
.empty())
1124 fprintf(f
, "EXCLUDE_FILE(");
1125 bool need_comma
= false;
1126 for (Filename_exclusions::const_iterator p
=
1127 this->filename_exclusions_
.begin();
1128 p
!= this->filename_exclusions_
.end();
1133 fprintf(f
, "%s", p
->first
.c_str());
1140 for (Input_section_patterns::const_iterator p
=
1141 this->input_section_patterns_
.begin();
1142 p
!= this->input_section_patterns_
.end();
1148 int close_parens
= 0;
1151 case SORT_WILDCARD_NONE
:
1153 case SORT_WILDCARD_BY_NAME
:
1154 fprintf(f
, "SORT_BY_NAME(");
1157 case SORT_WILDCARD_BY_ALIGNMENT
:
1158 fprintf(f
, "SORT_BY_ALIGNMENT(");
1161 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1162 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1165 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1166 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1173 fprintf(f
, "%s", p
->pattern
.c_str());
1175 for (int i
= 0; i
< close_parens
; ++i
)
1190 // An output section.
1192 class Output_section_definition
: public Sections_element
1195 typedef Output_section_element::Input_section_list Input_section_list
;
1197 Output_section_definition(const char* name
, size_t namelen
,
1198 const Parser_output_section_header
* header
);
1200 // Finish the output section with the information in the trailer.
1202 finish(const Parser_output_section_trailer
* trailer
);
1204 // Add a symbol to be defined.
1206 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1207 bool provide
, bool hidden
);
1209 // Add an assignment to the special dot symbol.
1211 add_dot_assignment(Expression
* value
);
1213 // Add an assertion.
1215 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1217 // Add a data item to the current output section.
1219 add_data(int size
, bool is_signed
, Expression
* val
);
1221 // Add a setting for the fill value.
1223 add_fill(Expression
* val
);
1225 // Add an input section specification.
1227 add_input_section(const Input_section_spec
* spec
, bool keep
);
1229 // Create any required output sections.
1231 create_sections(Layout
*);
1233 // Add any symbols being defined to the symbol table.
1235 add_symbols_to_table(Symbol_table
* symtab
);
1237 // Finalize symbols and check assertions.
1239 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1241 // Return the output section name to use for an input file name and
1244 output_section_name(const char* file_name
, const char* section_name
,
1247 // Return whether to place an orphan section after this one.
1249 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1251 // Set the section address.
1253 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1254 uint64_t* dot_value
, uint64_t* load_address
);
1256 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1257 // this section is constrained, and the input sections do not match,
1258 // return the constraint, and set *POSD.
1260 check_constraint(Output_section_definition
** posd
);
1262 // See if this is the alternate output section for a constrained
1263 // output section. If it is, transfer the Output_section and return
1264 // true. Otherwise return false.
1266 alternate_constraint(Output_section_definition
*, Section_constraint
);
1268 // Get the list of segments to use for an allocated section when
1269 // using a PHDRS clause.
1271 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
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
1840 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
1843 if (this->output_section_
== NULL
)
1845 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
1848 if (this->phdrs_
!= NULL
)
1849 *phdrs_list
= this->phdrs_
;
1850 return this->output_section_
;
1853 // Look for an output section by name and return the address, the load
1854 // address, the alignment, and the size. This is used when an
1855 // expression refers to an output section which was not actually
1856 // created. This returns true if the section was found, false
1860 Output_section_definition::get_output_section_info(const char* name
,
1862 uint64_t* load_address
,
1863 uint64_t* addralign
,
1864 uint64_t* size
) const
1866 if (this->name_
!= name
)
1869 if (this->output_section_
!= NULL
)
1871 *address
= this->output_section_
->address();
1872 if (this->output_section_
->has_load_address())
1873 *load_address
= this->output_section_
->load_address();
1875 *load_address
= *address
;
1876 *addralign
= this->output_section_
->addralign();
1877 *size
= this->output_section_
->current_data_size();
1881 *address
= this->evaluated_address_
;
1882 *load_address
= this->evaluated_load_address_
;
1883 *addralign
= this->evaluated_addralign_
;
1890 // Print for debugging.
1893 Output_section_definition::print(FILE* f
) const
1895 fprintf(f
, " %s ", this->name_
.c_str());
1897 if (this->address_
!= NULL
)
1899 this->address_
->print(f
);
1905 if (this->load_address_
!= NULL
)
1908 this->load_address_
->print(f
);
1912 if (this->align_
!= NULL
)
1914 fprintf(f
, "ALIGN(");
1915 this->align_
->print(f
);
1919 if (this->subalign_
!= NULL
)
1921 fprintf(f
, "SUBALIGN(");
1922 this->subalign_
->print(f
);
1928 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1929 p
!= this->elements_
.end();
1935 if (this->fill_
!= NULL
)
1938 this->fill_
->print(f
);
1941 if (this->phdrs_
!= NULL
)
1943 for (String_list::const_iterator p
= this->phdrs_
->begin();
1944 p
!= this->phdrs_
->end();
1946 fprintf(f
, " :%s", p
->c_str());
1952 // An output section created to hold orphaned input sections. These
1953 // do not actually appear in linker scripts. However, for convenience
1954 // when setting the output section addresses, we put a marker to these
1955 // sections in the appropriate place in the list of SECTIONS elements.
1957 class Orphan_output_section
: public Sections_element
1960 Orphan_output_section(Output_section
* os
)
1964 // Return whether to place an orphan section after this one.
1966 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1968 // Set section addresses.
1970 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
1972 // Get the list of segments to use for an allocated section when
1973 // using a PHDRS clause.
1975 allocate_to_segment(String_list
**, bool*);
1977 // Print for debugging.
1979 print(FILE* f
) const
1981 fprintf(f
, " marker for orphaned output section %s\n",
1986 Output_section
* os_
;
1989 // Whether to place another orphan section after this one.
1992 Orphan_output_section::place_orphan_here(const Output_section
* os
,
1995 if (this->os_
->type() == os
->type()
1996 && this->os_
->flags() == os
->flags())
2004 // Set section addresses.
2007 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2008 uint64_t* dot_value
,
2009 uint64_t* load_address
)
2011 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
2013 bool have_load_address
= *load_address
!= *dot_value
;
2015 uint64_t address
= *dot_value
;
2016 address
= align_address(address
, this->os_
->addralign());
2018 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2020 this->os_
->set_address(address
);
2021 if (have_load_address
)
2022 this->os_
->set_load_address(align_address(*load_address
,
2023 this->os_
->addralign()));
2026 Input_section_list input_sections
;
2027 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2029 for (Input_section_list::iterator p
= input_sections
.begin();
2030 p
!= input_sections
.end();
2036 // We know what are single-threaded, so it is OK to lock the
2039 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2040 Task_lock_obj
<Object
> tl(task
, p
->first
);
2041 addralign
= p
->first
->section_addralign(p
->second
);
2042 size
= p
->first
->section_size(p
->second
);
2045 address
= align_address(address
, addralign
);
2046 this->os_
->add_input_section_for_script(p
->first
, p
->second
, size
,
2051 if (!have_load_address
)
2052 *load_address
= address
;
2054 *load_address
+= address
- *dot_value
;
2056 *dot_value
= address
;
2059 // Get the list of segments to use for an allocated section when using
2060 // a PHDRS clause. If this is an allocated section, return the
2061 // Output_section. We don't change the list of segments.
2064 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2066 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2072 // Class Phdrs_element. A program header from a PHDRS clause.
2077 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2078 bool includes_filehdr
, bool includes_phdrs
,
2079 bool is_flags_valid
, unsigned int flags
,
2080 Expression
* load_address
)
2081 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2082 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2083 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2087 // Return the name of this segment.
2090 { return this->name_
; }
2092 // Return the type of the segment.
2095 { return this->type_
; }
2097 // Whether to include the file header.
2099 includes_filehdr() const
2100 { return this->includes_filehdr_
; }
2102 // Whether to include the program headers.
2104 includes_phdrs() const
2105 { return this->includes_phdrs_
; }
2107 // Return whether there is a load address.
2109 has_load_address() const
2110 { return this->load_address_
!= NULL
; }
2112 // Evaluate the load address expression if there is one.
2114 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2116 if (this->load_address_
!= NULL
)
2117 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2121 // Return the load address.
2123 load_address() const
2125 gold_assert(this->load_address_
!= NULL
);
2126 return this->load_address_value_
;
2129 // Create the segment.
2131 create_segment(Layout
* layout
)
2133 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2134 return this->segment_
;
2137 // Return the segment.
2140 { return this->segment_
; }
2142 // Set the segment flags if appropriate.
2144 set_flags_if_valid()
2146 if (this->is_flags_valid_
)
2147 this->segment_
->set_flags(this->flags_
);
2150 // Print for debugging.
2155 // The name used in the script.
2157 // The type of the segment (PT_LOAD, etc.).
2159 // Whether this segment includes the file header.
2160 bool includes_filehdr_
;
2161 // Whether this segment includes the section headers.
2162 bool includes_phdrs_
;
2163 // Whether the flags were explicitly specified.
2164 bool is_flags_valid_
;
2165 // The flags for this segment (PF_R, etc.) if specified.
2166 unsigned int flags_
;
2167 // The expression for the load address for this segment. This may
2169 Expression
* load_address_
;
2170 // The actual load address from evaluating the expression.
2171 uint64_t load_address_value_
;
2172 // The segment itself.
2173 Output_segment
* segment_
;
2176 // Print for debugging.
2179 Phdrs_element::print(FILE* f
) const
2181 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2182 if (this->includes_filehdr_
)
2183 fprintf(f
, " FILEHDR");
2184 if (this->includes_phdrs_
)
2185 fprintf(f
, " PHDRS");
2186 if (this->is_flags_valid_
)
2187 fprintf(f
, " FLAGS(%u)", this->flags_
);
2188 if (this->load_address_
!= NULL
)
2191 this->load_address_
->print(f
);
2197 // Class Script_sections.
2199 Script_sections::Script_sections()
2200 : saw_sections_clause_(false),
2201 in_sections_clause_(false),
2202 sections_elements_(NULL
),
2203 output_section_(NULL
),
2204 phdrs_elements_(NULL
)
2208 // Start a SECTIONS clause.
2211 Script_sections::start_sections()
2213 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2214 this->saw_sections_clause_
= true;
2215 this->in_sections_clause_
= true;
2216 if (this->sections_elements_
== NULL
)
2217 this->sections_elements_
= new Sections_elements
;
2220 // Finish a SECTIONS clause.
2223 Script_sections::finish_sections()
2225 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2226 this->in_sections_clause_
= false;
2229 // Add a symbol to be defined.
2232 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2233 Expression
* val
, bool provide
,
2236 if (this->output_section_
!= NULL
)
2237 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2241 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2244 this->sections_elements_
->push_back(p
);
2248 // Add an assignment to the special dot symbol.
2251 Script_sections::add_dot_assignment(Expression
* val
)
2253 if (this->output_section_
!= NULL
)
2254 this->output_section_
->add_dot_assignment(val
);
2257 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2258 this->sections_elements_
->push_back(p
);
2262 // Add an assertion.
2265 Script_sections::add_assertion(Expression
* check
, const char* message
,
2268 if (this->output_section_
!= NULL
)
2269 this->output_section_
->add_assertion(check
, message
, messagelen
);
2272 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2274 this->sections_elements_
->push_back(p
);
2278 // Start processing entries for an output section.
2281 Script_sections::start_output_section(
2284 const Parser_output_section_header
*header
)
2286 Output_section_definition
* posd
= new Output_section_definition(name
,
2289 this->sections_elements_
->push_back(posd
);
2290 gold_assert(this->output_section_
== NULL
);
2291 this->output_section_
= posd
;
2294 // Stop processing entries for an output section.
2297 Script_sections::finish_output_section(
2298 const Parser_output_section_trailer
* trailer
)
2300 gold_assert(this->output_section_
!= NULL
);
2301 this->output_section_
->finish(trailer
);
2302 this->output_section_
= NULL
;
2305 // Add a data item to the current output section.
2308 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2310 gold_assert(this->output_section_
!= NULL
);
2311 this->output_section_
->add_data(size
, is_signed
, val
);
2314 // Add a fill value setting to the current output section.
2317 Script_sections::add_fill(Expression
* val
)
2319 gold_assert(this->output_section_
!= NULL
);
2320 this->output_section_
->add_fill(val
);
2323 // Add an input section specification to the current output section.
2326 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2328 gold_assert(this->output_section_
!= NULL
);
2329 this->output_section_
->add_input_section(spec
, keep
);
2332 // Create any required sections.
2335 Script_sections::create_sections(Layout
* layout
)
2337 if (!this->saw_sections_clause_
)
2339 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2340 p
!= this->sections_elements_
->end();
2342 (*p
)->create_sections(layout
);
2345 // Add any symbols we are defining to the symbol table.
2348 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2350 if (!this->saw_sections_clause_
)
2352 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2353 p
!= this->sections_elements_
->end();
2355 (*p
)->add_symbols_to_table(symtab
);
2358 // Finalize symbols and check assertions.
2361 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2363 if (!this->saw_sections_clause_
)
2365 uint64_t dot_value
= 0;
2366 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2367 p
!= this->sections_elements_
->end();
2369 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2372 // Return the name of the output section to use for an input file name
2373 // and section name.
2376 Script_sections::output_section_name(const char* file_name
,
2377 const char* section_name
,
2378 Output_section
*** output_section_slot
)
2380 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2381 p
!= this->sections_elements_
->end();
2384 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2385 output_section_slot
);
2389 // The special name /DISCARD/ means that the input section
2390 // should be discarded.
2391 if (strcmp(ret
, "/DISCARD/") == 0)
2393 *output_section_slot
= NULL
;
2400 // If we couldn't find a mapping for the name, the output section
2401 // gets the name of the input section.
2403 *output_section_slot
= NULL
;
2405 return section_name
;
2408 // Place a marker for an orphan output section into the SECTIONS
2412 Script_sections::place_orphan(Output_section
* os
)
2414 // Look for an output section definition which matches the output
2415 // section. Put a marker after that section.
2416 Sections_elements::iterator place
= this->sections_elements_
->end();
2417 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2418 p
!= this->sections_elements_
->end();
2422 if ((*p
)->place_orphan_here(os
, &exact
))
2430 // The insert function puts the new element before the iterator.
2431 if (place
!= this->sections_elements_
->end())
2434 this->sections_elements_
->insert(place
, new Orphan_output_section(os
));
2437 // Set the addresses of all the output sections. Walk through all the
2438 // elements, tracking the dot symbol. Apply assignments which set
2439 // absolute symbol values, in case they are used when setting dot.
2440 // Fill in data statement values. As we find output sections, set the
2441 // address, set the address of all associated input sections, and
2442 // update dot. Return the segment which should hold the file header
2443 // and segment headers, if any.
2446 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2448 gold_assert(this->saw_sections_clause_
);
2450 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2451 // for our representation.
2452 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2453 p
!= this->sections_elements_
->end();
2456 Output_section_definition
* posd
;
2457 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2458 if (failed_constraint
!= CONSTRAINT_NONE
)
2460 Sections_elements::iterator q
;
2461 for (q
= this->sections_elements_
->begin();
2462 q
!= this->sections_elements_
->end();
2467 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2472 if (q
== this->sections_elements_
->end())
2473 gold_error(_("no matching section constraint"));
2477 // For a relocatable link, we implicitly set dot to zero.
2478 uint64_t dot_value
= 0;
2479 uint64_t load_address
= 0;
2480 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2481 p
!= this->sections_elements_
->end();
2483 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
2485 if (this->phdrs_elements_
!= NULL
)
2487 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2488 p
!= this->phdrs_elements_
->end();
2490 (*p
)->eval_load_address(symtab
, layout
);
2493 return this->create_segments(layout
);
2496 // Sort the sections in order to put them into segments.
2498 class Sort_output_sections
2502 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2506 Sort_output_sections::operator()(const Output_section
* os1
,
2507 const Output_section
* os2
) const
2509 // Sort first by the load address.
2510 uint64_t lma1
= (os1
->has_load_address()
2511 ? os1
->load_address()
2513 uint64_t lma2
= (os2
->has_load_address()
2514 ? os2
->load_address()
2519 // Then sort by the virtual address.
2520 if (os1
->address() != os2
->address())
2521 return os1
->address() < os2
->address();
2523 // Sort TLS sections to the end.
2524 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2525 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2529 // Sort PROGBITS before NOBITS.
2530 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2532 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2535 // Otherwise we don't care.
2539 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2540 // We treat a section with the SHF_TLS flag set as taking up space
2541 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2542 // space for them in the file.
2545 Script_sections::is_bss_section(const Output_section
* os
)
2547 return (os
->type() == elfcpp::SHT_NOBITS
2548 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2551 // Return the size taken by the file header and the program headers.
2554 Script_sections::total_header_size(Layout
* layout
) const
2556 size_t segment_count
= layout
->segment_count();
2557 size_t file_header_size
;
2558 size_t segment_headers_size
;
2559 if (parameters
->target().get_size() == 32)
2561 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2562 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2564 else if (parameters
->target().get_size() == 64)
2566 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2567 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2572 return file_header_size
+ segment_headers_size
;
2575 // Return the amount we have to subtract from the LMA to accomodate
2576 // headers of the given size. The complication is that the file
2577 // header have to be at the start of a page, as otherwise it will not
2578 // be at the start of the file.
2581 Script_sections::header_size_adjustment(uint64_t lma
,
2582 size_t sizeof_headers
) const
2584 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2585 uint64_t hdr_lma
= lma
- sizeof_headers
;
2586 hdr_lma
&= ~(abi_pagesize
- 1);
2587 return lma
- hdr_lma
;
2590 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2591 // the segment which should hold the file header and segment headers,
2595 Script_sections::create_segments(Layout
* layout
)
2597 gold_assert(this->saw_sections_clause_
);
2599 if (parameters
->options().relocatable())
2602 if (this->saw_phdrs_clause())
2603 return create_segments_from_phdrs_clause(layout
);
2605 Layout::Section_list sections
;
2606 layout
->get_allocated_sections(§ions
);
2608 // Sort the sections by address.
2609 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2611 this->create_note_and_tls_segments(layout
, §ions
);
2613 // Walk through the sections adding them to PT_LOAD segments.
2614 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2615 Output_segment
* first_seg
= NULL
;
2616 Output_segment
* current_seg
= NULL
;
2617 bool is_current_seg_readonly
= true;
2618 Layout::Section_list::iterator plast
= sections
.end();
2619 uint64_t last_vma
= 0;
2620 uint64_t last_lma
= 0;
2621 uint64_t last_size
= 0;
2622 for (Layout::Section_list::iterator p
= sections
.begin();
2623 p
!= sections
.end();
2626 const uint64_t vma
= (*p
)->address();
2627 const uint64_t lma
= ((*p
)->has_load_address()
2628 ? (*p
)->load_address()
2630 const uint64_t size
= (*p
)->current_data_size();
2632 bool need_new_segment
;
2633 if (current_seg
== NULL
)
2634 need_new_segment
= true;
2635 else if (lma
- vma
!= last_lma
- last_vma
)
2637 // This section has a different LMA relationship than the
2638 // last one; we need a new segment.
2639 need_new_segment
= true;
2641 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2642 < align_address(lma
, abi_pagesize
))
2644 // Putting this section in the segment would require
2646 need_new_segment
= true;
2648 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2650 // A non-BSS section can not follow a BSS section in the
2652 need_new_segment
= true;
2654 else if (is_current_seg_readonly
2655 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2657 // Don't put a writable section in the same segment as a
2658 // non-writable section.
2659 need_new_segment
= true;
2663 // Otherwise, reuse the existing segment.
2664 need_new_segment
= false;
2667 elfcpp::Elf_Word seg_flags
=
2668 Layout::section_flags_to_segment((*p
)->flags());
2670 if (need_new_segment
)
2672 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2674 current_seg
->set_addresses(vma
, lma
);
2675 if (first_seg
== NULL
)
2676 first_seg
= current_seg
;
2677 is_current_seg_readonly
= true;
2680 current_seg
->add_output_section(*p
, seg_flags
);
2682 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2683 is_current_seg_readonly
= false;
2691 // An ELF program should work even if the program headers are not in
2692 // a PT_LOAD segment. However, it appears that the Linux kernel
2693 // does not set the AT_PHDR auxiliary entry in that case. It sets
2694 // the load address to p_vaddr - p_offset of the first PT_LOAD
2695 // segment. It then sets AT_PHDR to the load address plus the
2696 // offset to the program headers, e_phoff in the file header. This
2697 // fails when the program headers appear in the file before the
2698 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2699 // segment to hold the file header and the program headers. This is
2700 // effectively what the GNU linker does, and it is slightly more
2701 // efficient in any case. We try to use the first PT_LOAD segment
2702 // if we can, otherwise we make a new one.
2704 if (first_seg
== NULL
)
2707 size_t sizeof_headers
= this->total_header_size(layout
);
2709 if ((first_seg
->paddr() & (abi_pagesize
- 1)) >= sizeof_headers
)
2711 first_seg
->set_addresses(first_seg
->vaddr() - sizeof_headers
,
2712 first_seg
->paddr() - sizeof_headers
);
2716 uint64_t vma
= first_seg
->vaddr();
2717 uint64_t lma
= first_seg
->paddr();
2719 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
2721 // If there is no room to squeeze in the headers, then punt. The
2722 // resulting executable probably won't run on GNU/Linux, but we
2723 // trust that the user knows what they are doing.
2724 if (lma
< subtract
|| vma
< subtract
)
2727 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2729 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
2734 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2735 // segment if there are any SHT_TLS sections.
2738 Script_sections::create_note_and_tls_segments(
2740 const Layout::Section_list
* sections
)
2742 gold_assert(!this->saw_phdrs_clause());
2744 bool saw_tls
= false;
2745 for (Layout::Section_list::const_iterator p
= sections
->begin();
2746 p
!= sections
->end();
2749 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2751 elfcpp::Elf_Word seg_flags
=
2752 Layout::section_flags_to_segment((*p
)->flags());
2753 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
2755 oseg
->add_output_section(*p
, seg_flags
);
2757 // Incorporate any subsequent SHT_NOTE sections, in the
2758 // hopes that the script is sensible.
2759 Layout::Section_list::const_iterator pnext
= p
+ 1;
2760 while (pnext
!= sections
->end()
2761 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
2763 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2764 oseg
->add_output_section(*pnext
, seg_flags
);
2770 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2773 gold_error(_("TLS sections are not adjacent"));
2775 elfcpp::Elf_Word seg_flags
=
2776 Layout::section_flags_to_segment((*p
)->flags());
2777 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
2779 oseg
->add_output_section(*p
, seg_flags
);
2781 Layout::Section_list::const_iterator pnext
= p
+ 1;
2782 while (pnext
!= sections
->end()
2783 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
2785 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2786 oseg
->add_output_section(*pnext
, seg_flags
);
2796 // Add a program header. The PHDRS clause is syntactically distinct
2797 // from the SECTIONS clause, but we implement it with the SECTIONS
2798 // support becauase PHDRS is useless if there is no SECTIONS clause.
2801 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
2802 bool includes_filehdr
, bool includes_phdrs
,
2803 bool is_flags_valid
, unsigned int flags
,
2804 Expression
* load_address
)
2806 if (this->phdrs_elements_
== NULL
)
2807 this->phdrs_elements_
= new Phdrs_elements();
2808 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
2811 is_flags_valid
, flags
,
2815 // Return the number of segments we expect to create based on the
2816 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2819 Script_sections::expected_segment_count(const Layout
* layout
) const
2821 if (this->saw_phdrs_clause())
2822 return this->phdrs_elements_
->size();
2824 Layout::Section_list sections
;
2825 layout
->get_allocated_sections(§ions
);
2827 // We assume that we will need two PT_LOAD segments.
2830 bool saw_note
= false;
2831 bool saw_tls
= false;
2832 for (Layout::Section_list::const_iterator p
= sections
.begin();
2833 p
!= sections
.end();
2836 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2838 // Assume that all note sections will fit into a single
2846 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2848 // There can only be one PT_TLS segment.
2860 // Create the segments from a PHDRS clause. Return the segment which
2861 // should hold the file header and program headers, if any.
2864 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
2866 this->attach_sections_using_phdrs_clause(layout
);
2867 return this->set_phdrs_clause_addresses(layout
);
2870 // Create the segments from the PHDRS clause, and put the output
2871 // sections in them.
2874 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
2876 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
2877 Name_to_segment name_to_segment
;
2878 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2879 p
!= this->phdrs_elements_
->end();
2881 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
2883 // Walk through the output sections and attach them to segments.
2884 // Output sections in the script which do not list segments are
2885 // attached to the same set of segments as the immediately preceding
2887 String_list
* phdr_names
= NULL
;
2888 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2889 p
!= this->sections_elements_
->end();
2893 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
2897 if (phdr_names
== NULL
)
2899 gold_error(_("allocated section not in any segment"));
2903 // If this is an orphan section--one that was not explicitly
2904 // mentioned in the linker script--then it should not inherit
2905 // any segment type other than PT_LOAD. Otherwise, e.g., the
2906 // PT_INTERP segment will pick up following orphan sections,
2907 // which does not make sense. If this is not an orphan section,
2908 // we trust the linker script.
2911 String_list::iterator q
= phdr_names
->begin();
2912 while (q
!= phdr_names
->end())
2914 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
2915 // We give errors about unknown segments below.
2916 if (r
== name_to_segment
.end()
2917 || r
->second
->type() == elfcpp::PT_LOAD
)
2920 q
= phdr_names
->erase(q
);
2924 bool in_load_segment
= false;
2925 for (String_list::const_iterator q
= phdr_names
->begin();
2926 q
!= phdr_names
->end();
2929 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
2930 if (r
== name_to_segment
.end())
2931 gold_error(_("no segment %s"), q
->c_str());
2934 elfcpp::Elf_Word seg_flags
=
2935 Layout::section_flags_to_segment(os
->flags());
2936 r
->second
->add_output_section(os
, seg_flags
);
2938 if (r
->second
->type() == elfcpp::PT_LOAD
)
2940 if (in_load_segment
)
2941 gold_error(_("section in two PT_LOAD segments"));
2942 in_load_segment
= true;
2947 if (!in_load_segment
)
2948 gold_error(_("allocated section not in any PT_LOAD segment"));
2952 // Set the addresses for segments created from a PHDRS clause. Return
2953 // the segment which should hold the file header and program headers,
2957 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
2959 Output_segment
* load_seg
= NULL
;
2960 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2961 p
!= this->phdrs_elements_
->end();
2964 // Note that we have to set the flags after adding the output
2965 // sections to the segment, as adding an output segment can
2966 // change the flags.
2967 (*p
)->set_flags_if_valid();
2969 Output_segment
* oseg
= (*p
)->segment();
2971 if (oseg
->type() != elfcpp::PT_LOAD
)
2973 // The addresses of non-PT_LOAD segments are set from the
2974 // PT_LOAD segments.
2975 if ((*p
)->has_load_address())
2976 gold_error(_("may only specify load address for PT_LOAD segment"));
2980 // The output sections should have addresses from the SECTIONS
2981 // clause. The addresses don't have to be in order, so find the
2982 // one with the lowest load address. Use that to set the
2983 // address of the segment.
2985 Output_section
* osec
= oseg
->section_with_lowest_load_address();
2988 oseg
->set_addresses(0, 0);
2992 uint64_t vma
= osec
->address();
2993 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
2995 // Override the load address of the section with the load
2996 // address specified for the segment.
2997 if ((*p
)->has_load_address())
2999 if (osec
->has_load_address())
3000 gold_warning(_("PHDRS load address overrides "
3001 "section %s load address"),
3004 lma
= (*p
)->load_address();
3007 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3008 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3010 // We could support this if we wanted to.
3011 gold_error(_("using only one of FILEHDR and PHDRS is "
3012 "not currently supported"));
3016 size_t sizeof_headers
= this->total_header_size(layout
);
3017 uint64_t subtract
= this->header_size_adjustment(lma
,
3019 if (lma
>= subtract
&& vma
>= subtract
)
3026 gold_error(_("sections loaded on first page without room "
3027 "for file and program headers "
3028 "are not supported"));
3031 if (load_seg
!= NULL
)
3032 gold_error(_("using FILEHDR and PHDRS on more than one "
3033 "PT_LOAD segment is not currently supported"));
3037 oseg
->set_addresses(vma
, lma
);
3043 // Add the file header and segment headers to non-load segments
3044 // specified in the PHDRS clause.
3047 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3048 Output_data
* segment_headers
)
3050 gold_assert(this->saw_phdrs_clause());
3051 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3052 p
!= this->phdrs_elements_
->end();
3055 if ((*p
)->type() != elfcpp::PT_LOAD
)
3057 if ((*p
)->includes_phdrs())
3058 (*p
)->segment()->add_initial_output_data(segment_headers
);
3059 if ((*p
)->includes_filehdr())
3060 (*p
)->segment()->add_initial_output_data(file_header
);
3065 // Look for an output section by name and return the address, the load
3066 // address, the alignment, and the size. This is used when an
3067 // expression refers to an output section which was not actually
3068 // created. This returns true if the section was found, false
3072 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3073 uint64_t* load_address
,
3074 uint64_t* addralign
,
3075 uint64_t* size
) const
3077 if (!this->saw_sections_clause_
)
3079 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3080 p
!= this->sections_elements_
->end();
3082 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3088 // Print the SECTIONS clause to F for debugging.
3091 Script_sections::print(FILE* f
) const
3093 if (!this->saw_sections_clause_
)
3096 fprintf(f
, "SECTIONS {\n");
3098 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3099 p
!= this->sections_elements_
->end();
3105 if (this->phdrs_elements_
!= NULL
)
3107 fprintf(f
, "PHDRS {\n");
3108 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3109 p
!= this->phdrs_elements_
->end();
3116 } // End namespace gold.