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_zero_fill(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*);
485 // Write to a map file.
487 do_print_to_mapfile(Mapfile
* mapfile
) const
488 { mapfile
->print_output_data(this, _("** expression")); }
491 template<bool big_endian
>
493 endian_write_to_buffer(uint64_t, unsigned char*);
497 const Symbol_table
* symtab_
;
498 const Layout
* layout_
;
500 Output_section
* dot_section_
;
503 // Write the data element to the output file.
506 Output_data_expression::do_write(Output_file
* of
)
508 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
509 this->write_to_buffer(view
);
510 of
->write_output_view(this->offset(), this->data_size(), view
);
513 // Write the data element to a buffer.
516 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
518 Output_section
* dummy
;
519 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
520 true, this->dot_value_
,
521 this->dot_section_
, &dummy
);
523 if (parameters
->target().is_big_endian())
524 this->endian_write_to_buffer
<true>(val
, buf
);
526 this->endian_write_to_buffer
<false>(val
, buf
);
529 template<bool big_endian
>
531 Output_data_expression::endian_write_to_buffer(uint64_t val
,
534 switch (this->data_size())
537 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
540 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
543 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
546 if (parameters
->target().get_size() == 32)
549 if (this->is_signed_
&& (val
& 0x80000000) != 0)
550 val
|= 0xffffffff00000000LL
;
552 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
559 // A data item in an output section.
561 class Output_section_element_data
: public Output_section_element
564 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
565 : size_(size
), is_signed_(is_signed
), val_(val
)
568 // If there is a data item, then we must create an output section.
570 needs_output_section() const
573 // Finalize symbols--we just need to update dot.
575 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
577 { *dot_value
+= this->size_
; }
579 // Store the value in the section.
581 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
582 uint64_t* dot_value
, Output_section
**, std::string
*,
583 Input_section_list
*);
585 // Print for debugging.
590 // The size in bytes.
592 // Whether the value is signed.
598 // Store the value in the section.
601 Output_section_element_data::set_section_addresses(
602 Symbol_table
* symtab
,
607 Output_section
** dot_section
,
611 gold_assert(os
!= NULL
);
612 os
->add_output_section_data(new Output_data_expression(this->size_
,
619 *dot_value
+= this->size_
;
622 // Print for debugging.
625 Output_section_element_data::print(FILE* f
) const
640 if (this->is_signed_
)
648 fprintf(f
, " %s(", s
);
649 this->val_
->print(f
);
653 // A fill value setting in an output section.
655 class Output_section_element_fill
: public Output_section_element
658 Output_section_element_fill(Expression
* val
)
662 // Update the fill value while setting section addresses.
664 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
665 uint64_t, uint64_t* dot_value
,
666 Output_section
** dot_section
,
667 std::string
* fill
, Input_section_list
*)
669 Output_section
* fill_section
;
670 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
671 *dot_value
, *dot_section
,
673 if (fill_section
!= NULL
)
674 gold_warning(_("fill value is not absolute"));
675 // FIXME: The GNU linker supports fill values of arbitrary length.
676 unsigned char fill_buff
[4];
677 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
678 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
681 // Print for debugging.
685 fprintf(f
, " FILL(");
686 this->val_
->print(f
);
691 // The new fill value.
695 // Return whether STRING contains a wildcard character. This is used
696 // to speed up matching.
699 is_wildcard_string(const std::string
& s
)
701 return strpbrk(s
.c_str(), "?*[") != NULL
;
704 // An input section specification in an output section
706 class Output_section_element_input
: public Output_section_element
709 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
711 // Finalize symbols--just update the value of the dot symbol.
713 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
714 Output_section
** dot_section
)
716 *dot_value
= this->final_dot_value_
;
717 *dot_section
= this->final_dot_section_
;
720 // See whether we match FILE_NAME and SECTION_NAME as an input
723 match_name(const char* file_name
, const char* section_name
) const;
725 // Set the section address.
727 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
728 uint64_t subalign
, uint64_t* dot_value
,
729 Output_section
**, std::string
* fill
,
730 Input_section_list
*);
732 // Print for debugging.
734 print(FILE* f
) const;
737 // An input section pattern.
738 struct Input_section_pattern
741 bool pattern_is_wildcard
;
744 Input_section_pattern(const char* patterna
, size_t patternlena
,
746 : pattern(patterna
, patternlena
),
747 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
752 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
754 // Filename_exclusions is a pair of filename pattern and a bool
755 // indicating whether the filename is a wildcard.
756 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
758 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
759 // indicates whether this is a wildcard pattern.
761 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
763 return (is_wildcard_pattern
764 ? fnmatch(pattern
, string
, 0) == 0
765 : strcmp(string
, pattern
) == 0);
768 // See if we match a file name.
770 match_file_name(const char* file_name
) const;
772 // The file name pattern. If this is the empty string, we match all
774 std::string filename_pattern_
;
775 // Whether the file name pattern is a wildcard.
776 bool filename_is_wildcard_
;
777 // How the file names should be sorted. This may only be
778 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
779 Sort_wildcard filename_sort_
;
780 // The list of file names to exclude.
781 Filename_exclusions filename_exclusions_
;
782 // The list of input section patterns.
783 Input_section_patterns input_section_patterns_
;
784 // Whether to keep this section when garbage collecting.
786 // The value of dot after including all matching sections.
787 uint64_t final_dot_value_
;
788 // The section where dot is defined after including all matching
790 Output_section
* final_dot_section_
;
793 // Construct Output_section_element_input. The parser records strings
794 // as pointers into a copy of the script file, which will go away when
795 // parsing is complete. We make sure they are in std::string objects.
797 Output_section_element_input::Output_section_element_input(
798 const Input_section_spec
* spec
,
800 : filename_pattern_(),
801 filename_is_wildcard_(false),
802 filename_sort_(spec
->file
.sort
),
803 filename_exclusions_(),
804 input_section_patterns_(),
807 final_dot_section_(NULL
)
809 // The filename pattern "*" is common, and matches all files. Turn
810 // it into the empty string.
811 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
812 this->filename_pattern_
.assign(spec
->file
.name
.value
,
813 spec
->file
.name
.length
);
814 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
816 if (spec
->input_sections
.exclude
!= NULL
)
818 for (String_list::const_iterator p
=
819 spec
->input_sections
.exclude
->begin();
820 p
!= spec
->input_sections
.exclude
->end();
823 bool is_wildcard
= is_wildcard_string(*p
);
824 this->filename_exclusions_
.push_back(std::make_pair(*p
,
829 if (spec
->input_sections
.sections
!= NULL
)
831 Input_section_patterns
& isp(this->input_section_patterns_
);
832 for (String_sort_list::const_iterator p
=
833 spec
->input_sections
.sections
->begin();
834 p
!= spec
->input_sections
.sections
->end();
836 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
841 // See whether we match FILE_NAME.
844 Output_section_element_input::match_file_name(const char* file_name
) const
846 if (!this->filename_pattern_
.empty())
848 // If we were called with no filename, we refuse to match a
849 // pattern which requires a file name.
850 if (file_name
== NULL
)
853 if (!match(file_name
, this->filename_pattern_
.c_str(),
854 this->filename_is_wildcard_
))
858 if (file_name
!= NULL
)
860 // Now we have to see whether FILE_NAME matches one of the
861 // exclusion patterns, if any.
862 for (Filename_exclusions::const_iterator p
=
863 this->filename_exclusions_
.begin();
864 p
!= this->filename_exclusions_
.end();
867 if (match(file_name
, p
->first
.c_str(), p
->second
))
875 // See whether we match FILE_NAME and SECTION_NAME.
878 Output_section_element_input::match_name(const char* file_name
,
879 const char* section_name
) const
881 if (!this->match_file_name(file_name
))
884 // If there are no section name patterns, then we match.
885 if (this->input_section_patterns_
.empty())
888 // See whether we match the section name patterns.
889 for (Input_section_patterns::const_iterator p
=
890 this->input_section_patterns_
.begin();
891 p
!= this->input_section_patterns_
.end();
894 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
898 // We didn't match any section names, so we didn't match.
902 // Information we use to sort the input sections.
904 struct Input_section_info
908 std::string section_name
;
913 // A class to sort the input sections.
915 class Input_section_sorter
918 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
919 : filename_sort_(filename_sort
), section_sort_(section_sort
)
923 operator()(const Input_section_info
&, const Input_section_info
&) const;
926 Sort_wildcard filename_sort_
;
927 Sort_wildcard section_sort_
;
931 Input_section_sorter::operator()(const Input_section_info
& isi1
,
932 const Input_section_info
& isi2
) const
934 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
935 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
936 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
937 && isi1
.addralign
== isi2
.addralign
))
939 if (isi1
.section_name
!= isi2
.section_name
)
940 return isi1
.section_name
< isi2
.section_name
;
942 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
943 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
944 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
946 if (isi1
.addralign
!= isi2
.addralign
)
947 return isi1
.addralign
< isi2
.addralign
;
949 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
951 if (isi1
.relobj
->name() != isi2
.relobj
->name())
952 return isi1
.relobj
->name() < isi2
.relobj
->name();
955 // Otherwise we leave them in the same order.
959 // Set the section address. Look in INPUT_SECTIONS for sections which
960 // match this spec, sort them as specified, and add them to the output
964 Output_section_element_input::set_section_addresses(
967 Output_section
* output_section
,
970 Output_section
** dot_section
,
972 Input_section_list
* input_sections
)
974 // We build a list of sections which match each
975 // Input_section_pattern.
977 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
978 size_t input_pattern_count
= this->input_section_patterns_
.size();
979 if (input_pattern_count
== 0)
980 input_pattern_count
= 1;
981 Matching_sections
matching_sections(input_pattern_count
);
983 // Look through the list of sections for this output section. Add
984 // each one which matches to one of the elements of
985 // MATCHING_SECTIONS.
987 Input_section_list::iterator p
= input_sections
->begin();
988 while (p
!= input_sections
->end())
990 // Calling section_name and section_addralign is not very
992 Input_section_info isi
;
993 isi
.relobj
= p
->first
;
994 isi
.shndx
= p
->second
;
996 // Lock the object so that we can get information about the
997 // section. This is OK since we know we are single-threaded
1000 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1001 Task_lock_obj
<Object
> tl(task
, p
->first
);
1003 isi
.section_name
= p
->first
->section_name(p
->second
);
1004 isi
.size
= p
->first
->section_size(p
->second
);
1005 isi
.addralign
= p
->first
->section_addralign(p
->second
);
1008 if (!this->match_file_name(isi
.relobj
->name().c_str()))
1010 else if (this->input_section_patterns_
.empty())
1012 matching_sections
[0].push_back(isi
);
1013 p
= input_sections
->erase(p
);
1018 for (i
= 0; i
< input_pattern_count
; ++i
)
1020 const Input_section_pattern
&
1021 isp(this->input_section_patterns_
[i
]);
1022 if (match(isi
.section_name
.c_str(), isp
.pattern
.c_str(),
1023 isp
.pattern_is_wildcard
))
1027 if (i
>= this->input_section_patterns_
.size())
1031 matching_sections
[i
].push_back(isi
);
1032 p
= input_sections
->erase(p
);
1037 // Look through MATCHING_SECTIONS. Sort each one as specified,
1038 // using a stable sort so that we get the default order when
1039 // sections are otherwise equal. Add each input section to the
1042 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1044 if (matching_sections
[i
].empty())
1047 gold_assert(output_section
!= NULL
);
1049 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1050 if (isp
.sort
!= SORT_WILDCARD_NONE
1051 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1052 std::stable_sort(matching_sections
[i
].begin(),
1053 matching_sections
[i
].end(),
1054 Input_section_sorter(this->filename_sort_
,
1057 for (std::vector
<Input_section_info
>::const_iterator p
=
1058 matching_sections
[i
].begin();
1059 p
!= matching_sections
[i
].end();
1062 uint64_t this_subalign
= p
->addralign
;
1063 if (this_subalign
< subalign
)
1064 this_subalign
= subalign
;
1066 uint64_t address
= align_address(*dot_value
, this_subalign
);
1068 if (address
> *dot_value
&& !fill
->empty())
1070 section_size_type length
=
1071 convert_to_section_size_type(address
- *dot_value
);
1072 std::string this_fill
= this->get_fill_string(fill
, length
);
1073 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1074 output_section
->add_output_section_data(posd
);
1077 output_section
->add_input_section_for_script(p
->relobj
,
1082 *dot_value
= address
+ p
->size
;
1086 this->final_dot_value_
= *dot_value
;
1087 this->final_dot_section_
= *dot_section
;
1090 // Print for debugging.
1093 Output_section_element_input::print(FILE* f
) const
1098 fprintf(f
, "KEEP(");
1100 if (!this->filename_pattern_
.empty())
1102 bool need_close_paren
= false;
1103 switch (this->filename_sort_
)
1105 case SORT_WILDCARD_NONE
:
1107 case SORT_WILDCARD_BY_NAME
:
1108 fprintf(f
, "SORT_BY_NAME(");
1109 need_close_paren
= true;
1115 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1117 if (need_close_paren
)
1121 if (!this->input_section_patterns_
.empty()
1122 || !this->filename_exclusions_
.empty())
1126 bool need_space
= false;
1127 if (!this->filename_exclusions_
.empty())
1129 fprintf(f
, "EXCLUDE_FILE(");
1130 bool need_comma
= false;
1131 for (Filename_exclusions::const_iterator p
=
1132 this->filename_exclusions_
.begin();
1133 p
!= this->filename_exclusions_
.end();
1138 fprintf(f
, "%s", p
->first
.c_str());
1145 for (Input_section_patterns::const_iterator p
=
1146 this->input_section_patterns_
.begin();
1147 p
!= this->input_section_patterns_
.end();
1153 int close_parens
= 0;
1156 case SORT_WILDCARD_NONE
:
1158 case SORT_WILDCARD_BY_NAME
:
1159 fprintf(f
, "SORT_BY_NAME(");
1162 case SORT_WILDCARD_BY_ALIGNMENT
:
1163 fprintf(f
, "SORT_BY_ALIGNMENT(");
1166 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1167 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1170 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1171 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1178 fprintf(f
, "%s", p
->pattern
.c_str());
1180 for (int i
= 0; i
< close_parens
; ++i
)
1195 // An output section.
1197 class Output_section_definition
: public Sections_element
1200 typedef Output_section_element::Input_section_list Input_section_list
;
1202 Output_section_definition(const char* name
, size_t namelen
,
1203 const Parser_output_section_header
* header
);
1205 // Finish the output section with the information in the trailer.
1207 finish(const Parser_output_section_trailer
* trailer
);
1209 // Add a symbol to be defined.
1211 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1212 bool provide
, bool hidden
);
1214 // Add an assignment to the special dot symbol.
1216 add_dot_assignment(Expression
* value
);
1218 // Add an assertion.
1220 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1222 // Add a data item to the current output section.
1224 add_data(int size
, bool is_signed
, Expression
* val
);
1226 // Add a setting for the fill value.
1228 add_fill(Expression
* val
);
1230 // Add an input section specification.
1232 add_input_section(const Input_section_spec
* spec
, bool keep
);
1234 // Create any required output sections.
1236 create_sections(Layout
*);
1238 // Add any symbols being defined to the symbol table.
1240 add_symbols_to_table(Symbol_table
* symtab
);
1242 // Finalize symbols and check assertions.
1244 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1246 // Return the output section name to use for an input file name and
1249 output_section_name(const char* file_name
, const char* section_name
,
1252 // Return whether to place an orphan section after this one.
1254 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1256 // Set the section address.
1258 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1259 uint64_t* dot_value
, uint64_t* load_address
);
1261 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1262 // this section is constrained, and the input sections do not match,
1263 // return the constraint, and set *POSD.
1265 check_constraint(Output_section_definition
** posd
);
1267 // See if this is the alternate output section for a constrained
1268 // output section. If it is, transfer the Output_section and return
1269 // true. Otherwise return false.
1271 alternate_constraint(Output_section_definition
*, Section_constraint
);
1273 // Get the list of segments to use for an allocated section when
1274 // using a PHDRS clause.
1276 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1278 // Look for an output section by name and return the address, the
1279 // load address, the alignment, and the size. This is used when an
1280 // expression refers to an output section which was not actually
1281 // created. This returns true if the section was found, false
1284 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1287 // Print the contents to the FILE. This is for debugging.
1292 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1294 // The output section name.
1296 // The address. This may be NULL.
1297 Expression
* address_
;
1298 // The load address. This may be NULL.
1299 Expression
* load_address_
;
1300 // The alignment. This may be NULL.
1302 // The input section alignment. This may be NULL.
1303 Expression
* subalign_
;
1304 // The constraint, if any.
1305 Section_constraint constraint_
;
1306 // The fill value. This may be NULL.
1308 // The list of segments this section should go into. This may be
1310 String_list
* phdrs_
;
1311 // The list of elements defining the section.
1312 Output_section_elements elements_
;
1313 // The Output_section created for this definition. This will be
1314 // NULL if none was created.
1315 Output_section
* output_section_
;
1316 // The address after it has been evaluated.
1317 uint64_t evaluated_address_
;
1318 // The load address after it has been evaluated.
1319 uint64_t evaluated_load_address_
;
1320 // The alignment after it has been evaluated.
1321 uint64_t evaluated_addralign_
;
1326 Output_section_definition::Output_section_definition(
1329 const Parser_output_section_header
* header
)
1330 : name_(name
, namelen
),
1331 address_(header
->address
),
1332 load_address_(header
->load_address
),
1333 align_(header
->align
),
1334 subalign_(header
->subalign
),
1335 constraint_(header
->constraint
),
1339 output_section_(NULL
)
1343 // Finish an output section.
1346 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1348 this->fill_
= trailer
->fill
;
1349 this->phdrs_
= trailer
->phdrs
;
1352 // Add a symbol to be defined.
1355 Output_section_definition::add_symbol_assignment(const char* name
,
1361 Output_section_element
* p
= new Output_section_element_assignment(name
,
1366 this->elements_
.push_back(p
);
1369 // Add an assignment to the special dot symbol.
1372 Output_section_definition::add_dot_assignment(Expression
* value
)
1374 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1375 this->elements_
.push_back(p
);
1378 // Add an assertion.
1381 Output_section_definition::add_assertion(Expression
* check
,
1382 const char* message
,
1385 Output_section_element
* p
= new Output_section_element_assertion(check
,
1388 this->elements_
.push_back(p
);
1391 // Add a data item to the current output section.
1394 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1396 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1398 this->elements_
.push_back(p
);
1401 // Add a setting for the fill value.
1404 Output_section_definition::add_fill(Expression
* val
)
1406 Output_section_element
* p
= new Output_section_element_fill(val
);
1407 this->elements_
.push_back(p
);
1410 // Add an input section specification.
1413 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1416 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1417 this->elements_
.push_back(p
);
1420 // Create any required output sections. We need an output section if
1421 // there is a data statement here.
1424 Output_section_definition::create_sections(Layout
* layout
)
1426 if (this->output_section_
!= NULL
)
1428 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1429 p
!= this->elements_
.end();
1432 if ((*p
)->needs_output_section())
1434 const char* name
= this->name_
.c_str();
1435 this->output_section_
= layout
->make_output_section_for_script(name
);
1441 // Add any symbols being defined to the symbol table.
1444 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1446 for (Output_section_elements::iterator p
= this->elements_
.begin();
1447 p
!= this->elements_
.end();
1449 (*p
)->add_symbols_to_table(symtab
);
1452 // Finalize symbols and check assertions.
1455 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1456 const Layout
* layout
,
1457 uint64_t* dot_value
)
1459 if (this->output_section_
!= NULL
)
1460 *dot_value
= this->output_section_
->address();
1463 uint64_t address
= *dot_value
;
1464 if (this->address_
!= NULL
)
1466 Output_section
* dummy
;
1467 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1471 if (this->align_
!= NULL
)
1473 Output_section
* dummy
;
1474 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
1478 address
= align_address(address
, align
);
1480 *dot_value
= address
;
1483 Output_section
* dot_section
= this->output_section_
;
1484 for (Output_section_elements::iterator p
= this->elements_
.begin();
1485 p
!= this->elements_
.end();
1487 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1490 // Return the output section name to use for an input section name.
1493 Output_section_definition::output_section_name(const char* file_name
,
1494 const char* section_name
,
1495 Output_section
*** slot
)
1497 // Ask each element whether it matches NAME.
1498 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1499 p
!= this->elements_
.end();
1502 if ((*p
)->match_name(file_name
, section_name
))
1504 // We found a match for NAME, which means that it should go
1505 // into this output section.
1506 *slot
= &this->output_section_
;
1507 return this->name_
.c_str();
1511 // We don't know about this section name.
1515 // Return whether to place an orphan output section after this
1519 Output_section_definition::place_orphan_here(const Output_section
*os
,
1522 // Check for the simple case first.
1523 if (this->output_section_
!= NULL
1524 && this->output_section_
->type() == os
->type()
1525 && this->output_section_
->flags() == os
->flags())
1531 // Otherwise use some heuristics.
1533 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1536 if (os
->type() == elfcpp::SHT_NOBITS
)
1538 if (this->name_
== ".bss")
1543 if (this->output_section_
!= NULL
1544 && this->output_section_
->type() == elfcpp::SHT_NOBITS
)
1547 else if (os
->type() == elfcpp::SHT_NOTE
)
1549 if (this->output_section_
!= NULL
1550 && this->output_section_
->type() == elfcpp::SHT_NOTE
)
1555 if (this->name_
.compare(0, 5, ".note") == 0)
1560 if (this->name_
== ".interp")
1562 if (this->output_section_
!= NULL
1563 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1564 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1567 else if (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
1569 if (this->name_
.compare(0, 4, ".rel") == 0)
1574 if (this->output_section_
!= NULL
1575 && (this->output_section_
->type() == elfcpp::SHT_REL
1576 || this->output_section_
->type() == elfcpp::SHT_RELA
))
1581 if (this->output_section_
!= NULL
1582 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1583 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1586 else if (os
->type() == elfcpp::SHT_PROGBITS
1587 && (os
->flags() & elfcpp::SHF_WRITE
) != 0)
1589 if (this->name_
== ".data")
1594 if (this->output_section_
!= NULL
1595 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1596 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1599 else if (os
->type() == elfcpp::SHT_PROGBITS
1600 && (os
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1602 if (this->name_
== ".text")
1607 if (this->output_section_
!= NULL
1608 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1609 && (this->output_section_
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1612 else if (os
->type() == elfcpp::SHT_PROGBITS
1613 || (os
->type() != elfcpp::SHT_PROGBITS
1614 && (os
->flags() & elfcpp::SHF_WRITE
) == 0))
1616 if (this->name_
== ".rodata")
1621 if (this->output_section_
!= NULL
1622 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1623 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1630 // Set the section address. Note that the OUTPUT_SECTION_ field will
1631 // be NULL if no input sections were mapped to this output section.
1632 // We still have to adjust dot and process symbol assignments.
1635 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1637 uint64_t* dot_value
,
1638 uint64_t* load_address
)
1641 if (this->address_
== NULL
)
1642 address
= *dot_value
;
1645 Output_section
* dummy
;
1646 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1647 *dot_value
, NULL
, &dummy
);
1651 if (this->align_
== NULL
)
1653 if (this->output_section_
== NULL
)
1656 align
= this->output_section_
->addralign();
1660 Output_section
* align_section
;
1661 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1662 NULL
, &align_section
);
1663 if (align_section
!= NULL
)
1664 gold_warning(_("alignment of section %s is not absolute"),
1665 this->name_
.c_str());
1666 if (this->output_section_
!= NULL
)
1667 this->output_section_
->set_addralign(align
);
1670 address
= align_address(address
, align
);
1672 uint64_t start_address
= address
;
1674 *dot_value
= address
;
1676 // The address of non-SHF_ALLOC sections is forced to zero,
1677 // regardless of what the linker script wants.
1678 if (this->output_section_
!= NULL
1679 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1680 this->output_section_
->set_address(address
);
1682 this->evaluated_address_
= address
;
1683 this->evaluated_addralign_
= align
;
1685 if (this->load_address_
== NULL
)
1686 this->evaluated_load_address_
= address
;
1689 Output_section
* dummy
;
1690 uint64_t load_address
=
1691 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1692 this->output_section_
, &dummy
);
1693 if (this->output_section_
!= NULL
)
1694 this->output_section_
->set_load_address(load_address
);
1695 this->evaluated_load_address_
= load_address
;
1699 if (this->subalign_
== NULL
)
1703 Output_section
* subalign_section
;
1704 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
1707 if (subalign_section
!= NULL
)
1708 gold_warning(_("subalign of section %s is not absolute"),
1709 this->name_
.c_str());
1713 if (this->fill_
!= NULL
)
1715 // FIXME: The GNU linker supports fill values of arbitrary
1717 Output_section
* fill_section
;
1718 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
1722 if (fill_section
!= NULL
)
1723 gold_warning(_("fill of section %s is not absolute"),
1724 this->name_
.c_str());
1725 unsigned char fill_buff
[4];
1726 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1727 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1730 Input_section_list input_sections
;
1731 if (this->output_section_
!= NULL
)
1733 // Get the list of input sections attached to this output
1734 // section. This will leave the output section with only
1735 // Output_section_data entries.
1736 address
+= this->output_section_
->get_input_sections(address
,
1739 *dot_value
= address
;
1742 Output_section
* dot_section
= this->output_section_
;
1743 for (Output_section_elements::iterator p
= this->elements_
.begin();
1744 p
!= this->elements_
.end();
1746 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
1747 subalign
, dot_value
, &dot_section
, &fill
,
1750 gold_assert(input_sections
.empty());
1752 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
1753 *load_address
= *dot_value
;
1755 *load_address
= (this->output_section_
->load_address()
1756 + (*dot_value
- start_address
));
1759 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1760 // this section is constrained, and the input sections do not match,
1761 // return the constraint, and set *POSD.
1764 Output_section_definition::check_constraint(Output_section_definition
** posd
)
1766 switch (this->constraint_
)
1768 case CONSTRAINT_NONE
:
1769 return CONSTRAINT_NONE
;
1771 case CONSTRAINT_ONLY_IF_RO
:
1772 if (this->output_section_
!= NULL
1773 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1776 return CONSTRAINT_ONLY_IF_RO
;
1778 return CONSTRAINT_NONE
;
1780 case CONSTRAINT_ONLY_IF_RW
:
1781 if (this->output_section_
!= NULL
1782 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1785 return CONSTRAINT_ONLY_IF_RW
;
1787 return CONSTRAINT_NONE
;
1789 case CONSTRAINT_SPECIAL
:
1790 if (this->output_section_
!= NULL
)
1791 gold_error(_("SPECIAL constraints are not implemented"));
1792 return CONSTRAINT_NONE
;
1799 // See if this is the alternate output section for a constrained
1800 // output section. If it is, transfer the Output_section and return
1801 // true. Otherwise return false.
1804 Output_section_definition::alternate_constraint(
1805 Output_section_definition
* posd
,
1806 Section_constraint constraint
)
1808 if (this->name_
!= posd
->name_
)
1813 case CONSTRAINT_ONLY_IF_RO
:
1814 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
1818 case CONSTRAINT_ONLY_IF_RW
:
1819 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
1827 // We have found the alternate constraint. We just need to move
1828 // over the Output_section. When constraints are used properly,
1829 // THIS should not have an output_section pointer, as all the input
1830 // sections should have matched the other definition.
1832 if (this->output_section_
!= NULL
)
1833 gold_error(_("mismatched definition for constrained sections"));
1835 this->output_section_
= posd
->output_section_
;
1836 posd
->output_section_
= NULL
;
1841 // Get the list of segments to use for an allocated section when using
1845 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
1848 if (this->output_section_
== NULL
)
1850 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
1853 if (this->phdrs_
!= NULL
)
1854 *phdrs_list
= this->phdrs_
;
1855 return this->output_section_
;
1858 // Look for an output section by name and return the address, the load
1859 // address, the alignment, and the size. This is used when an
1860 // expression refers to an output section which was not actually
1861 // created. This returns true if the section was found, false
1865 Output_section_definition::get_output_section_info(const char* name
,
1867 uint64_t* load_address
,
1868 uint64_t* addralign
,
1869 uint64_t* size
) const
1871 if (this->name_
!= name
)
1874 if (this->output_section_
!= NULL
)
1876 *address
= this->output_section_
->address();
1877 if (this->output_section_
->has_load_address())
1878 *load_address
= this->output_section_
->load_address();
1880 *load_address
= *address
;
1881 *addralign
= this->output_section_
->addralign();
1882 *size
= this->output_section_
->current_data_size();
1886 *address
= this->evaluated_address_
;
1887 *load_address
= this->evaluated_load_address_
;
1888 *addralign
= this->evaluated_addralign_
;
1895 // Print for debugging.
1898 Output_section_definition::print(FILE* f
) const
1900 fprintf(f
, " %s ", this->name_
.c_str());
1902 if (this->address_
!= NULL
)
1904 this->address_
->print(f
);
1910 if (this->load_address_
!= NULL
)
1913 this->load_address_
->print(f
);
1917 if (this->align_
!= NULL
)
1919 fprintf(f
, "ALIGN(");
1920 this->align_
->print(f
);
1924 if (this->subalign_
!= NULL
)
1926 fprintf(f
, "SUBALIGN(");
1927 this->subalign_
->print(f
);
1933 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1934 p
!= this->elements_
.end();
1940 if (this->fill_
!= NULL
)
1943 this->fill_
->print(f
);
1946 if (this->phdrs_
!= NULL
)
1948 for (String_list::const_iterator p
= this->phdrs_
->begin();
1949 p
!= this->phdrs_
->end();
1951 fprintf(f
, " :%s", p
->c_str());
1957 // An output section created to hold orphaned input sections. These
1958 // do not actually appear in linker scripts. However, for convenience
1959 // when setting the output section addresses, we put a marker to these
1960 // sections in the appropriate place in the list of SECTIONS elements.
1962 class Orphan_output_section
: public Sections_element
1965 Orphan_output_section(Output_section
* os
)
1969 // Return whether to place an orphan section after this one.
1971 place_orphan_here(const Output_section
*os
, bool* exact
) const;
1973 // Set section addresses.
1975 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
1977 // Get the list of segments to use for an allocated section when
1978 // using a PHDRS clause.
1980 allocate_to_segment(String_list
**, bool*);
1982 // Print for debugging.
1984 print(FILE* f
) const
1986 fprintf(f
, " marker for orphaned output section %s\n",
1991 Output_section
* os_
;
1994 // Whether to place another orphan section after this one.
1997 Orphan_output_section::place_orphan_here(const Output_section
* os
,
2000 if (this->os_
->type() == os
->type()
2001 && this->os_
->flags() == os
->flags())
2009 // Set section addresses.
2012 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2013 uint64_t* dot_value
,
2014 uint64_t* load_address
)
2016 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
2018 bool have_load_address
= *load_address
!= *dot_value
;
2020 uint64_t address
= *dot_value
;
2021 address
= align_address(address
, this->os_
->addralign());
2023 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2025 this->os_
->set_address(address
);
2026 if (have_load_address
)
2027 this->os_
->set_load_address(align_address(*load_address
,
2028 this->os_
->addralign()));
2031 Input_section_list input_sections
;
2032 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2034 for (Input_section_list::iterator p
= input_sections
.begin();
2035 p
!= input_sections
.end();
2041 // We know what are single-threaded, so it is OK to lock the
2044 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2045 Task_lock_obj
<Object
> tl(task
, p
->first
);
2046 addralign
= p
->first
->section_addralign(p
->second
);
2047 size
= p
->first
->section_size(p
->second
);
2050 address
= align_address(address
, addralign
);
2051 this->os_
->add_input_section_for_script(p
->first
, p
->second
, size
,
2056 if (!have_load_address
)
2057 *load_address
= address
;
2059 *load_address
+= address
- *dot_value
;
2061 *dot_value
= address
;
2064 // Get the list of segments to use for an allocated section when using
2065 // a PHDRS clause. If this is an allocated section, return the
2066 // Output_section. We don't change the list of segments.
2069 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2071 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2077 // Class Phdrs_element. A program header from a PHDRS clause.
2082 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2083 bool includes_filehdr
, bool includes_phdrs
,
2084 bool is_flags_valid
, unsigned int flags
,
2085 Expression
* load_address
)
2086 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2087 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2088 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2092 // Return the name of this segment.
2095 { return this->name_
; }
2097 // Return the type of the segment.
2100 { return this->type_
; }
2102 // Whether to include the file header.
2104 includes_filehdr() const
2105 { return this->includes_filehdr_
; }
2107 // Whether to include the program headers.
2109 includes_phdrs() const
2110 { return this->includes_phdrs_
; }
2112 // Return whether there is a load address.
2114 has_load_address() const
2115 { return this->load_address_
!= NULL
; }
2117 // Evaluate the load address expression if there is one.
2119 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2121 if (this->load_address_
!= NULL
)
2122 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2126 // Return the load address.
2128 load_address() const
2130 gold_assert(this->load_address_
!= NULL
);
2131 return this->load_address_value_
;
2134 // Create the segment.
2136 create_segment(Layout
* layout
)
2138 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2139 return this->segment_
;
2142 // Return the segment.
2145 { return this->segment_
; }
2147 // Set the segment flags if appropriate.
2149 set_flags_if_valid()
2151 if (this->is_flags_valid_
)
2152 this->segment_
->set_flags(this->flags_
);
2155 // Print for debugging.
2160 // The name used in the script.
2162 // The type of the segment (PT_LOAD, etc.).
2164 // Whether this segment includes the file header.
2165 bool includes_filehdr_
;
2166 // Whether this segment includes the section headers.
2167 bool includes_phdrs_
;
2168 // Whether the flags were explicitly specified.
2169 bool is_flags_valid_
;
2170 // The flags for this segment (PF_R, etc.) if specified.
2171 unsigned int flags_
;
2172 // The expression for the load address for this segment. This may
2174 Expression
* load_address_
;
2175 // The actual load address from evaluating the expression.
2176 uint64_t load_address_value_
;
2177 // The segment itself.
2178 Output_segment
* segment_
;
2181 // Print for debugging.
2184 Phdrs_element::print(FILE* f
) const
2186 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2187 if (this->includes_filehdr_
)
2188 fprintf(f
, " FILEHDR");
2189 if (this->includes_phdrs_
)
2190 fprintf(f
, " PHDRS");
2191 if (this->is_flags_valid_
)
2192 fprintf(f
, " FLAGS(%u)", this->flags_
);
2193 if (this->load_address_
!= NULL
)
2196 this->load_address_
->print(f
);
2202 // Class Script_sections.
2204 Script_sections::Script_sections()
2205 : saw_sections_clause_(false),
2206 in_sections_clause_(false),
2207 sections_elements_(NULL
),
2208 output_section_(NULL
),
2209 phdrs_elements_(NULL
)
2213 // Start a SECTIONS clause.
2216 Script_sections::start_sections()
2218 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2219 this->saw_sections_clause_
= true;
2220 this->in_sections_clause_
= true;
2221 if (this->sections_elements_
== NULL
)
2222 this->sections_elements_
= new Sections_elements
;
2225 // Finish a SECTIONS clause.
2228 Script_sections::finish_sections()
2230 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2231 this->in_sections_clause_
= false;
2234 // Add a symbol to be defined.
2237 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2238 Expression
* val
, bool provide
,
2241 if (this->output_section_
!= NULL
)
2242 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2246 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2249 this->sections_elements_
->push_back(p
);
2253 // Add an assignment to the special dot symbol.
2256 Script_sections::add_dot_assignment(Expression
* val
)
2258 if (this->output_section_
!= NULL
)
2259 this->output_section_
->add_dot_assignment(val
);
2262 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2263 this->sections_elements_
->push_back(p
);
2267 // Add an assertion.
2270 Script_sections::add_assertion(Expression
* check
, const char* message
,
2273 if (this->output_section_
!= NULL
)
2274 this->output_section_
->add_assertion(check
, message
, messagelen
);
2277 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2279 this->sections_elements_
->push_back(p
);
2283 // Start processing entries for an output section.
2286 Script_sections::start_output_section(
2289 const Parser_output_section_header
*header
)
2291 Output_section_definition
* posd
= new Output_section_definition(name
,
2294 this->sections_elements_
->push_back(posd
);
2295 gold_assert(this->output_section_
== NULL
);
2296 this->output_section_
= posd
;
2299 // Stop processing entries for an output section.
2302 Script_sections::finish_output_section(
2303 const Parser_output_section_trailer
* trailer
)
2305 gold_assert(this->output_section_
!= NULL
);
2306 this->output_section_
->finish(trailer
);
2307 this->output_section_
= NULL
;
2310 // Add a data item to the current output section.
2313 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2315 gold_assert(this->output_section_
!= NULL
);
2316 this->output_section_
->add_data(size
, is_signed
, val
);
2319 // Add a fill value setting to the current output section.
2322 Script_sections::add_fill(Expression
* val
)
2324 gold_assert(this->output_section_
!= NULL
);
2325 this->output_section_
->add_fill(val
);
2328 // Add an input section specification to the current output section.
2331 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2333 gold_assert(this->output_section_
!= NULL
);
2334 this->output_section_
->add_input_section(spec
, keep
);
2337 // Create any required sections.
2340 Script_sections::create_sections(Layout
* layout
)
2342 if (!this->saw_sections_clause_
)
2344 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2345 p
!= this->sections_elements_
->end();
2347 (*p
)->create_sections(layout
);
2350 // Add any symbols we are defining to the symbol table.
2353 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2355 if (!this->saw_sections_clause_
)
2357 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2358 p
!= this->sections_elements_
->end();
2360 (*p
)->add_symbols_to_table(symtab
);
2363 // Finalize symbols and check assertions.
2366 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2368 if (!this->saw_sections_clause_
)
2370 uint64_t dot_value
= 0;
2371 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2372 p
!= this->sections_elements_
->end();
2374 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2377 // Return the name of the output section to use for an input file name
2378 // and section name.
2381 Script_sections::output_section_name(const char* file_name
,
2382 const char* section_name
,
2383 Output_section
*** output_section_slot
)
2385 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2386 p
!= this->sections_elements_
->end();
2389 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2390 output_section_slot
);
2394 // The special name /DISCARD/ means that the input section
2395 // should be discarded.
2396 if (strcmp(ret
, "/DISCARD/") == 0)
2398 *output_section_slot
= NULL
;
2405 // If we couldn't find a mapping for the name, the output section
2406 // gets the name of the input section.
2408 *output_section_slot
= NULL
;
2410 return section_name
;
2413 // Place a marker for an orphan output section into the SECTIONS
2417 Script_sections::place_orphan(Output_section
* os
)
2419 // Look for an output section definition which matches the output
2420 // section. Put a marker after that section.
2421 Sections_elements::iterator place
= this->sections_elements_
->end();
2422 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2423 p
!= this->sections_elements_
->end();
2427 if ((*p
)->place_orphan_here(os
, &exact
))
2435 // The insert function puts the new element before the iterator.
2436 if (place
!= this->sections_elements_
->end())
2439 this->sections_elements_
->insert(place
, new Orphan_output_section(os
));
2442 // Set the addresses of all the output sections. Walk through all the
2443 // elements, tracking the dot symbol. Apply assignments which set
2444 // absolute symbol values, in case they are used when setting dot.
2445 // Fill in data statement values. As we find output sections, set the
2446 // address, set the address of all associated input sections, and
2447 // update dot. Return the segment which should hold the file header
2448 // and segment headers, if any.
2451 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2453 gold_assert(this->saw_sections_clause_
);
2455 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2456 // for our representation.
2457 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2458 p
!= this->sections_elements_
->end();
2461 Output_section_definition
* posd
;
2462 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2463 if (failed_constraint
!= CONSTRAINT_NONE
)
2465 Sections_elements::iterator q
;
2466 for (q
= this->sections_elements_
->begin();
2467 q
!= this->sections_elements_
->end();
2472 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2477 if (q
== this->sections_elements_
->end())
2478 gold_error(_("no matching section constraint"));
2482 // For a relocatable link, we implicitly set dot to zero.
2483 uint64_t dot_value
= 0;
2484 uint64_t load_address
= 0;
2485 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2486 p
!= this->sections_elements_
->end();
2488 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
2490 if (this->phdrs_elements_
!= NULL
)
2492 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2493 p
!= this->phdrs_elements_
->end();
2495 (*p
)->eval_load_address(symtab
, layout
);
2498 return this->create_segments(layout
);
2501 // Sort the sections in order to put them into segments.
2503 class Sort_output_sections
2507 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2511 Sort_output_sections::operator()(const Output_section
* os1
,
2512 const Output_section
* os2
) const
2514 // Sort first by the load address.
2515 uint64_t lma1
= (os1
->has_load_address()
2516 ? os1
->load_address()
2518 uint64_t lma2
= (os2
->has_load_address()
2519 ? os2
->load_address()
2524 // Then sort by the virtual address.
2525 if (os1
->address() != os2
->address())
2526 return os1
->address() < os2
->address();
2528 // Sort TLS sections to the end.
2529 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2530 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2534 // Sort PROGBITS before NOBITS.
2535 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2537 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2540 // Otherwise we don't care.
2544 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2545 // We treat a section with the SHF_TLS flag set as taking up space
2546 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2547 // space for them in the file.
2550 Script_sections::is_bss_section(const Output_section
* os
)
2552 return (os
->type() == elfcpp::SHT_NOBITS
2553 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2556 // Return the size taken by the file header and the program headers.
2559 Script_sections::total_header_size(Layout
* layout
) const
2561 size_t segment_count
= layout
->segment_count();
2562 size_t file_header_size
;
2563 size_t segment_headers_size
;
2564 if (parameters
->target().get_size() == 32)
2566 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2567 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2569 else if (parameters
->target().get_size() == 64)
2571 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2572 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2577 return file_header_size
+ segment_headers_size
;
2580 // Return the amount we have to subtract from the LMA to accomodate
2581 // headers of the given size. The complication is that the file
2582 // header have to be at the start of a page, as otherwise it will not
2583 // be at the start of the file.
2586 Script_sections::header_size_adjustment(uint64_t lma
,
2587 size_t sizeof_headers
) const
2589 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2590 uint64_t hdr_lma
= lma
- sizeof_headers
;
2591 hdr_lma
&= ~(abi_pagesize
- 1);
2592 return lma
- hdr_lma
;
2595 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2596 // the segment which should hold the file header and segment headers,
2600 Script_sections::create_segments(Layout
* layout
)
2602 gold_assert(this->saw_sections_clause_
);
2604 if (parameters
->options().relocatable())
2607 if (this->saw_phdrs_clause())
2608 return create_segments_from_phdrs_clause(layout
);
2610 Layout::Section_list sections
;
2611 layout
->get_allocated_sections(§ions
);
2613 // Sort the sections by address.
2614 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2616 this->create_note_and_tls_segments(layout
, §ions
);
2618 // Walk through the sections adding them to PT_LOAD segments.
2619 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2620 Output_segment
* first_seg
= NULL
;
2621 Output_segment
* current_seg
= NULL
;
2622 bool is_current_seg_readonly
= true;
2623 Layout::Section_list::iterator plast
= sections
.end();
2624 uint64_t last_vma
= 0;
2625 uint64_t last_lma
= 0;
2626 uint64_t last_size
= 0;
2627 for (Layout::Section_list::iterator p
= sections
.begin();
2628 p
!= sections
.end();
2631 const uint64_t vma
= (*p
)->address();
2632 const uint64_t lma
= ((*p
)->has_load_address()
2633 ? (*p
)->load_address()
2635 const uint64_t size
= (*p
)->current_data_size();
2637 bool need_new_segment
;
2638 if (current_seg
== NULL
)
2639 need_new_segment
= true;
2640 else if (lma
- vma
!= last_lma
- last_vma
)
2642 // This section has a different LMA relationship than the
2643 // last one; we need a new segment.
2644 need_new_segment
= true;
2646 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2647 < align_address(lma
, abi_pagesize
))
2649 // Putting this section in the segment would require
2651 need_new_segment
= true;
2653 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2655 // A non-BSS section can not follow a BSS section in the
2657 need_new_segment
= true;
2659 else if (is_current_seg_readonly
2660 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
2661 && !parameters
->options().omagic())
2663 // Don't put a writable section in the same segment as a
2664 // non-writable section.
2665 need_new_segment
= true;
2669 // Otherwise, reuse the existing segment.
2670 need_new_segment
= false;
2673 elfcpp::Elf_Word seg_flags
=
2674 Layout::section_flags_to_segment((*p
)->flags());
2676 if (need_new_segment
)
2678 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2680 current_seg
->set_addresses(vma
, lma
);
2681 if (first_seg
== NULL
)
2682 first_seg
= current_seg
;
2683 is_current_seg_readonly
= true;
2686 current_seg
->add_output_section(*p
, seg_flags
);
2688 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2689 is_current_seg_readonly
= false;
2697 // An ELF program should work even if the program headers are not in
2698 // a PT_LOAD segment. However, it appears that the Linux kernel
2699 // does not set the AT_PHDR auxiliary entry in that case. It sets
2700 // the load address to p_vaddr - p_offset of the first PT_LOAD
2701 // segment. It then sets AT_PHDR to the load address plus the
2702 // offset to the program headers, e_phoff in the file header. This
2703 // fails when the program headers appear in the file before the
2704 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2705 // segment to hold the file header and the program headers. This is
2706 // effectively what the GNU linker does, and it is slightly more
2707 // efficient in any case. We try to use the first PT_LOAD segment
2708 // if we can, otherwise we make a new one.
2710 if (first_seg
== NULL
)
2713 size_t sizeof_headers
= this->total_header_size(layout
);
2715 uint64_t vma
= first_seg
->vaddr();
2716 uint64_t lma
= first_seg
->paddr();
2718 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
2720 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
2722 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
2726 // If there is no room to squeeze in the headers, then punt. The
2727 // resulting executable probably won't run on GNU/Linux, but we
2728 // trust that the user knows what they are doing.
2729 if (lma
< subtract
|| vma
< subtract
)
2732 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2734 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
2739 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2740 // segment if there are any SHT_TLS sections.
2743 Script_sections::create_note_and_tls_segments(
2745 const Layout::Section_list
* sections
)
2747 gold_assert(!this->saw_phdrs_clause());
2749 bool saw_tls
= false;
2750 for (Layout::Section_list::const_iterator p
= sections
->begin();
2751 p
!= sections
->end();
2754 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2756 elfcpp::Elf_Word seg_flags
=
2757 Layout::section_flags_to_segment((*p
)->flags());
2758 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
2760 oseg
->add_output_section(*p
, seg_flags
);
2762 // Incorporate any subsequent SHT_NOTE sections, in the
2763 // hopes that the script is sensible.
2764 Layout::Section_list::const_iterator pnext
= p
+ 1;
2765 while (pnext
!= sections
->end()
2766 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
2768 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2769 oseg
->add_output_section(*pnext
, seg_flags
);
2775 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2778 gold_error(_("TLS sections are not adjacent"));
2780 elfcpp::Elf_Word seg_flags
=
2781 Layout::section_flags_to_segment((*p
)->flags());
2782 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
2784 oseg
->add_output_section(*p
, seg_flags
);
2786 Layout::Section_list::const_iterator pnext
= p
+ 1;
2787 while (pnext
!= sections
->end()
2788 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
2790 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2791 oseg
->add_output_section(*pnext
, seg_flags
);
2801 // Add a program header. The PHDRS clause is syntactically distinct
2802 // from the SECTIONS clause, but we implement it with the SECTIONS
2803 // support becauase PHDRS is useless if there is no SECTIONS clause.
2806 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
2807 bool includes_filehdr
, bool includes_phdrs
,
2808 bool is_flags_valid
, unsigned int flags
,
2809 Expression
* load_address
)
2811 if (this->phdrs_elements_
== NULL
)
2812 this->phdrs_elements_
= new Phdrs_elements();
2813 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
2816 is_flags_valid
, flags
,
2820 // Return the number of segments we expect to create based on the
2821 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2824 Script_sections::expected_segment_count(const Layout
* layout
) const
2826 if (this->saw_phdrs_clause())
2827 return this->phdrs_elements_
->size();
2829 Layout::Section_list sections
;
2830 layout
->get_allocated_sections(§ions
);
2832 // We assume that we will need two PT_LOAD segments.
2835 bool saw_note
= false;
2836 bool saw_tls
= false;
2837 for (Layout::Section_list::const_iterator p
= sections
.begin();
2838 p
!= sections
.end();
2841 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2843 // Assume that all note sections will fit into a single
2851 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2853 // There can only be one PT_TLS segment.
2865 // Create the segments from a PHDRS clause. Return the segment which
2866 // should hold the file header and program headers, if any.
2869 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
2871 this->attach_sections_using_phdrs_clause(layout
);
2872 return this->set_phdrs_clause_addresses(layout
);
2875 // Create the segments from the PHDRS clause, and put the output
2876 // sections in them.
2879 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
2881 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
2882 Name_to_segment name_to_segment
;
2883 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2884 p
!= this->phdrs_elements_
->end();
2886 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
2888 // Walk through the output sections and attach them to segments.
2889 // Output sections in the script which do not list segments are
2890 // attached to the same set of segments as the immediately preceding
2892 String_list
* phdr_names
= NULL
;
2893 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2894 p
!= this->sections_elements_
->end();
2898 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
2902 if (phdr_names
== NULL
)
2904 gold_error(_("allocated section not in any segment"));
2908 // If this is an orphan section--one that was not explicitly
2909 // mentioned in the linker script--then it should not inherit
2910 // any segment type other than PT_LOAD. Otherwise, e.g., the
2911 // PT_INTERP segment will pick up following orphan sections,
2912 // which does not make sense. If this is not an orphan section,
2913 // we trust the linker script.
2916 String_list::iterator q
= phdr_names
->begin();
2917 while (q
!= phdr_names
->end())
2919 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
2920 // We give errors about unknown segments below.
2921 if (r
== name_to_segment
.end()
2922 || r
->second
->type() == elfcpp::PT_LOAD
)
2925 q
= phdr_names
->erase(q
);
2929 bool in_load_segment
= false;
2930 for (String_list::const_iterator q
= phdr_names
->begin();
2931 q
!= phdr_names
->end();
2934 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
2935 if (r
== name_to_segment
.end())
2936 gold_error(_("no segment %s"), q
->c_str());
2939 elfcpp::Elf_Word seg_flags
=
2940 Layout::section_flags_to_segment(os
->flags());
2941 r
->second
->add_output_section(os
, seg_flags
);
2943 if (r
->second
->type() == elfcpp::PT_LOAD
)
2945 if (in_load_segment
)
2946 gold_error(_("section in two PT_LOAD segments"));
2947 in_load_segment
= true;
2952 if (!in_load_segment
)
2953 gold_error(_("allocated section not in any PT_LOAD segment"));
2957 // Set the addresses for segments created from a PHDRS clause. Return
2958 // the segment which should hold the file header and program headers,
2962 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
2964 Output_segment
* load_seg
= NULL
;
2965 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2966 p
!= this->phdrs_elements_
->end();
2969 // Note that we have to set the flags after adding the output
2970 // sections to the segment, as adding an output segment can
2971 // change the flags.
2972 (*p
)->set_flags_if_valid();
2974 Output_segment
* oseg
= (*p
)->segment();
2976 if (oseg
->type() != elfcpp::PT_LOAD
)
2978 // The addresses of non-PT_LOAD segments are set from the
2979 // PT_LOAD segments.
2980 if ((*p
)->has_load_address())
2981 gold_error(_("may only specify load address for PT_LOAD segment"));
2985 // The output sections should have addresses from the SECTIONS
2986 // clause. The addresses don't have to be in order, so find the
2987 // one with the lowest load address. Use that to set the
2988 // address of the segment.
2990 Output_section
* osec
= oseg
->section_with_lowest_load_address();
2993 oseg
->set_addresses(0, 0);
2997 uint64_t vma
= osec
->address();
2998 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3000 // Override the load address of the section with the load
3001 // address specified for the segment.
3002 if ((*p
)->has_load_address())
3004 if (osec
->has_load_address())
3005 gold_warning(_("PHDRS load address overrides "
3006 "section %s load address"),
3009 lma
= (*p
)->load_address();
3012 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3013 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3015 // We could support this if we wanted to.
3016 gold_error(_("using only one of FILEHDR and PHDRS is "
3017 "not currently supported"));
3021 size_t sizeof_headers
= this->total_header_size(layout
);
3022 uint64_t subtract
= this->header_size_adjustment(lma
,
3024 if (lma
>= subtract
&& vma
>= subtract
)
3031 gold_error(_("sections loaded on first page without room "
3032 "for file and program headers "
3033 "are not supported"));
3036 if (load_seg
!= NULL
)
3037 gold_error(_("using FILEHDR and PHDRS on more than one "
3038 "PT_LOAD segment is not currently supported"));
3042 oseg
->set_addresses(vma
, lma
);
3048 // Add the file header and segment headers to non-load segments
3049 // specified in the PHDRS clause.
3052 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3053 Output_data
* segment_headers
)
3055 gold_assert(this->saw_phdrs_clause());
3056 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3057 p
!= this->phdrs_elements_
->end();
3060 if ((*p
)->type() != elfcpp::PT_LOAD
)
3062 if ((*p
)->includes_phdrs())
3063 (*p
)->segment()->add_initial_output_data(segment_headers
);
3064 if ((*p
)->includes_filehdr())
3065 (*p
)->segment()->add_initial_output_data(file_header
);
3070 // Look for an output section by name and return the address, the load
3071 // address, the alignment, and the size. This is used when an
3072 // expression refers to an output section which was not actually
3073 // created. This returns true if the section was found, false
3077 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3078 uint64_t* load_address
,
3079 uint64_t* addralign
,
3080 uint64_t* size
) const
3082 if (!this->saw_sections_clause_
)
3084 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3085 p
!= this->sections_elements_
->end();
3087 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3093 // Print the SECTIONS clause to F for debugging.
3096 Script_sections::print(FILE* f
) const
3098 if (!this->saw_sections_clause_
)
3101 fprintf(f
, "SECTIONS {\n");
3103 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3104 p
!= this->sections_elements_
->end();
3110 if (this->phdrs_elements_
!= NULL
)
3112 fprintf(f
, "PHDRS {\n");
3113 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3114 p
!= this->phdrs_elements_
->end();
3121 } // End namespace gold.