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 // Record that an output section is relro.
62 // Create any required output sections. The only real
63 // implementation is in Output_section_definition.
65 create_sections(Layout
*)
68 // Add any symbol being defined to the symbol table.
70 add_symbols_to_table(Symbol_table
*)
73 // Finalize symbols and check assertions.
75 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*)
78 // Return the output section name to use for an input file name and
79 // section name. This only real implementation is in
80 // Output_section_definition.
82 output_section_name(const char*, const char*, Output_section
***)
85 // Return whether to place an orphan output section after this
88 place_orphan_here(const Output_section
*, bool*, bool*) const
91 // Set section addresses. This includes applying assignments if the
92 // the expression is an absolute value.
94 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*)
97 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
98 // this section is constrained, and the input sections do not match,
99 // return the constraint, and set *POSD.
100 virtual Section_constraint
101 check_constraint(Output_section_definition
**)
102 { return CONSTRAINT_NONE
; }
104 // See if this is the alternate output section for a constrained
105 // output section. If it is, transfer the Output_section and return
106 // true. Otherwise return false.
108 alternate_constraint(Output_section_definition
*, Section_constraint
)
111 // Get the list of segments to use for an allocated section when
112 // using a PHDRS clause. If this is an allocated section, return
113 // the Output_section, and set *PHDRS_LIST (the first parameter) to
114 // the list of PHDRS to which it should be attached. If the PHDRS
115 // were not specified, don't change *PHDRS_LIST. When not returning
116 // NULL, set *ORPHAN (the second parameter) according to whether
117 // this is an orphan section--one that is not mentioned in the
119 virtual Output_section
*
120 allocate_to_segment(String_list
**, bool*)
123 // Look for an output section by name and return the address, the
124 // load address, the alignment, and the size. This is used when an
125 // expression refers to an output section which was not actually
126 // created. This returns true if the section was found, false
127 // otherwise. The only real definition is for
128 // Output_section_definition.
130 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
134 // Return the associated Output_section if there is one.
135 virtual Output_section
*
136 get_output_section() const
139 // Print the element for debugging purposes.
141 print(FILE* f
) const = 0;
144 // An assignment in a SECTIONS clause outside of an output section.
146 class Sections_element_assignment
: public Sections_element
149 Sections_element_assignment(const char* name
, size_t namelen
,
150 Expression
* val
, bool provide
, bool hidden
)
151 : assignment_(name
, namelen
, val
, provide
, hidden
)
154 // Add the symbol to the symbol table.
156 add_symbols_to_table(Symbol_table
* symtab
)
157 { this->assignment_
.add_to_table(symtab
); }
159 // Finalize the symbol.
161 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
164 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
167 // Set the section address. There is no section here, but if the
168 // value is absolute, we set the symbol. This permits us to use
169 // absolute symbols when setting dot.
171 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
172 uint64_t* dot_value
, uint64_t*)
174 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
177 // Print for debugging.
182 this->assignment_
.print(f
);
186 Symbol_assignment assignment_
;
189 // An assignment to the dot symbol in a SECTIONS clause outside of an
192 class Sections_element_dot_assignment
: public Sections_element
195 Sections_element_dot_assignment(Expression
* val
)
199 // Finalize the symbol.
201 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
204 // We ignore the section of the result because outside of an
205 // output section definition the dot symbol is always considered
207 Output_section
* dummy
;
208 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
212 // Update the dot symbol while setting section addresses.
214 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
215 uint64_t* dot_value
, uint64_t* load_address
)
217 Output_section
* dummy
;
218 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
220 *load_address
= *dot_value
;
223 // Print for debugging.
228 this->val_
->print(f
);
236 // An assertion in a SECTIONS clause outside of an output section.
238 class Sections_element_assertion
: public Sections_element
241 Sections_element_assertion(Expression
* check
, const char* message
,
243 : assertion_(check
, message
, messagelen
)
246 // Check the assertion.
248 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
249 { this->assertion_
.check(symtab
, layout
); }
251 // Print for debugging.
256 this->assertion_
.print(f
);
260 Script_assertion assertion_
;
263 // An element in an output section in a SECTIONS clause.
265 class Output_section_element
268 // A list of input sections.
269 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
271 Output_section_element()
274 virtual ~Output_section_element()
277 // Return whether this element requires an output section to exist.
279 needs_output_section() const
282 // Add any symbol being defined to the symbol table.
284 add_symbols_to_table(Symbol_table
*)
287 // Finalize symbols and check assertions.
289 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
292 // Return whether this element matches FILE_NAME and SECTION_NAME.
293 // The only real implementation is in Output_section_element_input.
295 match_name(const char*, const char*) const
298 // Set section addresses. This includes applying assignments if the
299 // the expression is an absolute value.
301 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
302 uint64_t*, Output_section
**, std::string
*,
306 // Print the element for debugging purposes.
308 print(FILE* f
) const = 0;
311 // Return a fill string that is LENGTH bytes long, filling it with
314 get_fill_string(const std::string
* fill
, section_size_type length
) const;
318 Output_section_element::get_fill_string(const std::string
* fill
,
319 section_size_type length
) const
321 std::string this_fill
;
322 this_fill
.reserve(length
);
323 while (this_fill
.length() + fill
->length() <= length
)
325 if (this_fill
.length() < length
)
326 this_fill
.append(*fill
, 0, length
- this_fill
.length());
330 // A symbol assignment in an output section.
332 class Output_section_element_assignment
: public Output_section_element
335 Output_section_element_assignment(const char* name
, size_t namelen
,
336 Expression
* val
, bool provide
,
338 : assignment_(name
, namelen
, val
, provide
, hidden
)
341 // Add the symbol to the symbol table.
343 add_symbols_to_table(Symbol_table
* symtab
)
344 { this->assignment_
.add_to_table(symtab
); }
346 // Finalize the symbol.
348 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
349 uint64_t* dot_value
, Output_section
** dot_section
)
351 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
355 // Set the section address. There is no section here, but if the
356 // value is absolute, we set the symbol. This permits us to use
357 // absolute symbols when setting dot.
359 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
360 uint64_t, uint64_t* dot_value
, Output_section
**,
361 std::string
*, Input_section_list
*)
363 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
366 // Print for debugging.
371 this->assignment_
.print(f
);
375 Symbol_assignment assignment_
;
378 // An assignment to the dot symbol in an output section.
380 class Output_section_element_dot_assignment
: public Output_section_element
383 Output_section_element_dot_assignment(Expression
* val
)
387 // Finalize the symbol.
389 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
390 uint64_t* dot_value
, Output_section
** dot_section
)
392 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
393 *dot_section
, dot_section
);
396 // Update the dot symbol while setting section addresses.
398 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
399 uint64_t, uint64_t* dot_value
, Output_section
**,
400 std::string
*, Input_section_list
*);
402 // Print for debugging.
407 this->val_
->print(f
);
415 // Update the dot symbol while setting section addresses.
418 Output_section_element_dot_assignment::set_section_addresses(
419 Symbol_table
* symtab
,
421 Output_section
* output_section
,
424 Output_section
** dot_section
,
428 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
429 *dot_value
, *dot_section
,
431 if (next_dot
< *dot_value
)
432 gold_error(_("dot may not move backward"));
433 if (next_dot
> *dot_value
&& output_section
!= NULL
)
435 section_size_type length
= convert_to_section_size_type(next_dot
437 Output_section_data
* posd
;
439 posd
= new Output_data_zero_fill(length
, 0);
442 std::string this_fill
= this->get_fill_string(fill
, length
);
443 posd
= new Output_data_const(this_fill
, 0);
445 output_section
->add_output_section_data(posd
);
447 *dot_value
= next_dot
;
450 // An assertion in an output section.
452 class Output_section_element_assertion
: public Output_section_element
455 Output_section_element_assertion(Expression
* check
, const char* message
,
457 : assertion_(check
, message
, messagelen
)
464 this->assertion_
.print(f
);
468 Script_assertion assertion_
;
471 // We use a special instance of Output_section_data to handle BYTE,
472 // SHORT, etc. This permits forward references to symbols in the
475 class Output_data_expression
: public Output_section_data
478 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
479 const Symbol_table
* symtab
, const Layout
* layout
,
480 uint64_t dot_value
, Output_section
* dot_section
)
481 : Output_section_data(size
, 0),
482 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
483 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
487 // Write the data to the output file.
489 do_write(Output_file
*);
491 // Write the data to a buffer.
493 do_write_to_buffer(unsigned char*);
495 // Write to a map file.
497 do_print_to_mapfile(Mapfile
* mapfile
) const
498 { mapfile
->print_output_data(this, _("** expression")); }
501 template<bool big_endian
>
503 endian_write_to_buffer(uint64_t, unsigned char*);
507 const Symbol_table
* symtab_
;
508 const Layout
* layout_
;
510 Output_section
* dot_section_
;
513 // Write the data element to the output file.
516 Output_data_expression::do_write(Output_file
* of
)
518 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
519 this->write_to_buffer(view
);
520 of
->write_output_view(this->offset(), this->data_size(), view
);
523 // Write the data element to a buffer.
526 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
528 Output_section
* dummy
;
529 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
530 true, this->dot_value_
,
531 this->dot_section_
, &dummy
);
533 if (parameters
->target().is_big_endian())
534 this->endian_write_to_buffer
<true>(val
, buf
);
536 this->endian_write_to_buffer
<false>(val
, buf
);
539 template<bool big_endian
>
541 Output_data_expression::endian_write_to_buffer(uint64_t val
,
544 switch (this->data_size())
547 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
550 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
553 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
556 if (parameters
->target().get_size() == 32)
559 if (this->is_signed_
&& (val
& 0x80000000) != 0)
560 val
|= 0xffffffff00000000LL
;
562 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
569 // A data item in an output section.
571 class Output_section_element_data
: public Output_section_element
574 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
575 : size_(size
), is_signed_(is_signed
), val_(val
)
578 // If there is a data item, then we must create an output section.
580 needs_output_section() const
583 // Finalize symbols--we just need to update dot.
585 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
587 { *dot_value
+= this->size_
; }
589 // Store the value in the section.
591 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
592 uint64_t* dot_value
, Output_section
**, std::string
*,
593 Input_section_list
*);
595 // Print for debugging.
600 // The size in bytes.
602 // Whether the value is signed.
608 // Store the value in the section.
611 Output_section_element_data::set_section_addresses(
612 Symbol_table
* symtab
,
617 Output_section
** dot_section
,
621 gold_assert(os
!= NULL
);
622 os
->add_output_section_data(new Output_data_expression(this->size_
,
629 *dot_value
+= this->size_
;
632 // Print for debugging.
635 Output_section_element_data::print(FILE* f
) const
650 if (this->is_signed_
)
658 fprintf(f
, " %s(", s
);
659 this->val_
->print(f
);
663 // A fill value setting in an output section.
665 class Output_section_element_fill
: public Output_section_element
668 Output_section_element_fill(Expression
* val
)
672 // Update the fill value while setting section addresses.
674 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
675 uint64_t, uint64_t* dot_value
,
676 Output_section
** dot_section
,
677 std::string
* fill
, Input_section_list
*)
679 Output_section
* fill_section
;
680 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
681 *dot_value
, *dot_section
,
683 if (fill_section
!= NULL
)
684 gold_warning(_("fill value is not absolute"));
685 // FIXME: The GNU linker supports fill values of arbitrary length.
686 unsigned char fill_buff
[4];
687 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
688 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
691 // Print for debugging.
695 fprintf(f
, " FILL(");
696 this->val_
->print(f
);
701 // The new fill value.
705 // Return whether STRING contains a wildcard character. This is used
706 // to speed up matching.
709 is_wildcard_string(const std::string
& s
)
711 return strpbrk(s
.c_str(), "?*[") != NULL
;
714 // An input section specification in an output section
716 class Output_section_element_input
: public Output_section_element
719 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
721 // Finalize symbols--just update the value of the dot symbol.
723 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
724 Output_section
** dot_section
)
726 *dot_value
= this->final_dot_value_
;
727 *dot_section
= this->final_dot_section_
;
730 // See whether we match FILE_NAME and SECTION_NAME as an input
733 match_name(const char* file_name
, const char* section_name
) const;
735 // Set the section address.
737 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
738 uint64_t subalign
, uint64_t* dot_value
,
739 Output_section
**, std::string
* fill
,
740 Input_section_list
*);
742 // Print for debugging.
744 print(FILE* f
) const;
747 // An input section pattern.
748 struct Input_section_pattern
751 bool pattern_is_wildcard
;
754 Input_section_pattern(const char* patterna
, size_t patternlena
,
756 : pattern(patterna
, patternlena
),
757 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
762 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
764 // Filename_exclusions is a pair of filename pattern and a bool
765 // indicating whether the filename is a wildcard.
766 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
768 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
769 // indicates whether this is a wildcard pattern.
771 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
773 return (is_wildcard_pattern
774 ? fnmatch(pattern
, string
, 0) == 0
775 : strcmp(string
, pattern
) == 0);
778 // See if we match a file name.
780 match_file_name(const char* file_name
) const;
782 // The file name pattern. If this is the empty string, we match all
784 std::string filename_pattern_
;
785 // Whether the file name pattern is a wildcard.
786 bool filename_is_wildcard_
;
787 // How the file names should be sorted. This may only be
788 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
789 Sort_wildcard filename_sort_
;
790 // The list of file names to exclude.
791 Filename_exclusions filename_exclusions_
;
792 // The list of input section patterns.
793 Input_section_patterns input_section_patterns_
;
794 // Whether to keep this section when garbage collecting.
796 // The value of dot after including all matching sections.
797 uint64_t final_dot_value_
;
798 // The section where dot is defined after including all matching
800 Output_section
* final_dot_section_
;
803 // Construct Output_section_element_input. The parser records strings
804 // as pointers into a copy of the script file, which will go away when
805 // parsing is complete. We make sure they are in std::string objects.
807 Output_section_element_input::Output_section_element_input(
808 const Input_section_spec
* spec
,
810 : filename_pattern_(),
811 filename_is_wildcard_(false),
812 filename_sort_(spec
->file
.sort
),
813 filename_exclusions_(),
814 input_section_patterns_(),
817 final_dot_section_(NULL
)
819 // The filename pattern "*" is common, and matches all files. Turn
820 // it into the empty string.
821 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
822 this->filename_pattern_
.assign(spec
->file
.name
.value
,
823 spec
->file
.name
.length
);
824 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
826 if (spec
->input_sections
.exclude
!= NULL
)
828 for (String_list::const_iterator p
=
829 spec
->input_sections
.exclude
->begin();
830 p
!= spec
->input_sections
.exclude
->end();
833 bool is_wildcard
= is_wildcard_string(*p
);
834 this->filename_exclusions_
.push_back(std::make_pair(*p
,
839 if (spec
->input_sections
.sections
!= NULL
)
841 Input_section_patterns
& isp(this->input_section_patterns_
);
842 for (String_sort_list::const_iterator p
=
843 spec
->input_sections
.sections
->begin();
844 p
!= spec
->input_sections
.sections
->end();
846 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
851 // See whether we match FILE_NAME.
854 Output_section_element_input::match_file_name(const char* file_name
) const
856 if (!this->filename_pattern_
.empty())
858 // If we were called with no filename, we refuse to match a
859 // pattern which requires a file name.
860 if (file_name
== NULL
)
863 if (!match(file_name
, this->filename_pattern_
.c_str(),
864 this->filename_is_wildcard_
))
868 if (file_name
!= NULL
)
870 // Now we have to see whether FILE_NAME matches one of the
871 // exclusion patterns, if any.
872 for (Filename_exclusions::const_iterator p
=
873 this->filename_exclusions_
.begin();
874 p
!= this->filename_exclusions_
.end();
877 if (match(file_name
, p
->first
.c_str(), p
->second
))
885 // See whether we match FILE_NAME and SECTION_NAME.
888 Output_section_element_input::match_name(const char* file_name
,
889 const char* section_name
) const
891 if (!this->match_file_name(file_name
))
894 // If there are no section name patterns, then we match.
895 if (this->input_section_patterns_
.empty())
898 // See whether we match the section name patterns.
899 for (Input_section_patterns::const_iterator p
=
900 this->input_section_patterns_
.begin();
901 p
!= this->input_section_patterns_
.end();
904 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
908 // We didn't match any section names, so we didn't match.
912 // Information we use to sort the input sections.
914 struct Input_section_info
918 std::string section_name
;
923 // A class to sort the input sections.
925 class Input_section_sorter
928 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
929 : filename_sort_(filename_sort
), section_sort_(section_sort
)
933 operator()(const Input_section_info
&, const Input_section_info
&) const;
936 Sort_wildcard filename_sort_
;
937 Sort_wildcard section_sort_
;
941 Input_section_sorter::operator()(const Input_section_info
& isi1
,
942 const Input_section_info
& isi2
) const
944 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
945 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
946 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
947 && isi1
.addralign
== isi2
.addralign
))
949 if (isi1
.section_name
!= isi2
.section_name
)
950 return isi1
.section_name
< isi2
.section_name
;
952 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
953 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
954 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
956 if (isi1
.addralign
!= isi2
.addralign
)
957 return isi1
.addralign
< isi2
.addralign
;
959 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
961 if (isi1
.relobj
->name() != isi2
.relobj
->name())
962 return isi1
.relobj
->name() < isi2
.relobj
->name();
965 // Otherwise we leave them in the same order.
969 // Set the section address. Look in INPUT_SECTIONS for sections which
970 // match this spec, sort them as specified, and add them to the output
974 Output_section_element_input::set_section_addresses(
977 Output_section
* output_section
,
980 Output_section
** dot_section
,
982 Input_section_list
* input_sections
)
984 // We build a list of sections which match each
985 // Input_section_pattern.
987 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
988 size_t input_pattern_count
= this->input_section_patterns_
.size();
989 if (input_pattern_count
== 0)
990 input_pattern_count
= 1;
991 Matching_sections
matching_sections(input_pattern_count
);
993 // Look through the list of sections for this output section. Add
994 // each one which matches to one of the elements of
995 // MATCHING_SECTIONS.
997 Input_section_list::iterator p
= input_sections
->begin();
998 while (p
!= input_sections
->end())
1000 // Calling section_name and section_addralign is not very
1002 Input_section_info isi
;
1003 isi
.relobj
= p
->first
;
1004 isi
.shndx
= p
->second
;
1006 // Lock the object so that we can get information about the
1007 // section. This is OK since we know we are single-threaded
1010 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1011 Task_lock_obj
<Object
> tl(task
, p
->first
);
1013 isi
.section_name
= p
->first
->section_name(p
->second
);
1014 isi
.size
= p
->first
->section_size(p
->second
);
1015 isi
.addralign
= p
->first
->section_addralign(p
->second
);
1018 if (!this->match_file_name(isi
.relobj
->name().c_str()))
1020 else if (this->input_section_patterns_
.empty())
1022 matching_sections
[0].push_back(isi
);
1023 p
= input_sections
->erase(p
);
1028 for (i
= 0; i
< input_pattern_count
; ++i
)
1030 const Input_section_pattern
&
1031 isp(this->input_section_patterns_
[i
]);
1032 if (match(isi
.section_name
.c_str(), isp
.pattern
.c_str(),
1033 isp
.pattern_is_wildcard
))
1037 if (i
>= this->input_section_patterns_
.size())
1041 matching_sections
[i
].push_back(isi
);
1042 p
= input_sections
->erase(p
);
1047 // Look through MATCHING_SECTIONS. Sort each one as specified,
1048 // using a stable sort so that we get the default order when
1049 // sections are otherwise equal. Add each input section to the
1052 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1054 if (matching_sections
[i
].empty())
1057 gold_assert(output_section
!= NULL
);
1059 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1060 if (isp
.sort
!= SORT_WILDCARD_NONE
1061 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1062 std::stable_sort(matching_sections
[i
].begin(),
1063 matching_sections
[i
].end(),
1064 Input_section_sorter(this->filename_sort_
,
1067 for (std::vector
<Input_section_info
>::const_iterator p
=
1068 matching_sections
[i
].begin();
1069 p
!= matching_sections
[i
].end();
1072 uint64_t this_subalign
= p
->addralign
;
1073 if (this_subalign
< subalign
)
1074 this_subalign
= subalign
;
1076 uint64_t address
= align_address(*dot_value
, this_subalign
);
1078 if (address
> *dot_value
&& !fill
->empty())
1080 section_size_type length
=
1081 convert_to_section_size_type(address
- *dot_value
);
1082 std::string this_fill
= this->get_fill_string(fill
, length
);
1083 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1084 output_section
->add_output_section_data(posd
);
1087 output_section
->add_input_section_for_script(p
->relobj
,
1092 *dot_value
= address
+ p
->size
;
1096 this->final_dot_value_
= *dot_value
;
1097 this->final_dot_section_
= *dot_section
;
1100 // Print for debugging.
1103 Output_section_element_input::print(FILE* f
) const
1108 fprintf(f
, "KEEP(");
1110 if (!this->filename_pattern_
.empty())
1112 bool need_close_paren
= false;
1113 switch (this->filename_sort_
)
1115 case SORT_WILDCARD_NONE
:
1117 case SORT_WILDCARD_BY_NAME
:
1118 fprintf(f
, "SORT_BY_NAME(");
1119 need_close_paren
= true;
1125 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1127 if (need_close_paren
)
1131 if (!this->input_section_patterns_
.empty()
1132 || !this->filename_exclusions_
.empty())
1136 bool need_space
= false;
1137 if (!this->filename_exclusions_
.empty())
1139 fprintf(f
, "EXCLUDE_FILE(");
1140 bool need_comma
= false;
1141 for (Filename_exclusions::const_iterator p
=
1142 this->filename_exclusions_
.begin();
1143 p
!= this->filename_exclusions_
.end();
1148 fprintf(f
, "%s", p
->first
.c_str());
1155 for (Input_section_patterns::const_iterator p
=
1156 this->input_section_patterns_
.begin();
1157 p
!= this->input_section_patterns_
.end();
1163 int close_parens
= 0;
1166 case SORT_WILDCARD_NONE
:
1168 case SORT_WILDCARD_BY_NAME
:
1169 fprintf(f
, "SORT_BY_NAME(");
1172 case SORT_WILDCARD_BY_ALIGNMENT
:
1173 fprintf(f
, "SORT_BY_ALIGNMENT(");
1176 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1177 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1180 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1181 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1188 fprintf(f
, "%s", p
->pattern
.c_str());
1190 for (int i
= 0; i
< close_parens
; ++i
)
1205 // An output section.
1207 class Output_section_definition
: public Sections_element
1210 typedef Output_section_element::Input_section_list Input_section_list
;
1212 Output_section_definition(const char* name
, size_t namelen
,
1213 const Parser_output_section_header
* header
);
1215 // Finish the output section with the information in the trailer.
1217 finish(const Parser_output_section_trailer
* trailer
);
1219 // Add a symbol to be defined.
1221 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1222 bool provide
, bool hidden
);
1224 // Add an assignment to the special dot symbol.
1226 add_dot_assignment(Expression
* value
);
1228 // Add an assertion.
1230 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1232 // Add a data item to the current output section.
1234 add_data(int size
, bool is_signed
, Expression
* val
);
1236 // Add a setting for the fill value.
1238 add_fill(Expression
* val
);
1240 // Add an input section specification.
1242 add_input_section(const Input_section_spec
* spec
, bool keep
);
1244 // Record that the output section is relro.
1247 { this->is_relro_
= true; }
1249 // Create any required output sections.
1251 create_sections(Layout
*);
1253 // Add any symbols being defined to the symbol table.
1255 add_symbols_to_table(Symbol_table
* symtab
);
1257 // Finalize symbols and check assertions.
1259 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1261 // Return the output section name to use for an input file name and
1264 output_section_name(const char* file_name
, const char* section_name
,
1267 // Return whether to place an orphan section after this one.
1269 place_orphan_here(const Output_section
*os
, bool* exact
, bool*) const;
1271 // Set the section address.
1273 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1274 uint64_t* dot_value
, uint64_t* load_address
);
1276 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1277 // this section is constrained, and the input sections do not match,
1278 // return the constraint, and set *POSD.
1280 check_constraint(Output_section_definition
** posd
);
1282 // See if this is the alternate output section for a constrained
1283 // output section. If it is, transfer the Output_section and return
1284 // true. Otherwise return false.
1286 alternate_constraint(Output_section_definition
*, Section_constraint
);
1288 // Get the list of segments to use for an allocated section when
1289 // using a PHDRS clause.
1291 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1293 // Look for an output section by name and return the address, the
1294 // load address, the alignment, and the size. This is used when an
1295 // expression refers to an output section which was not actually
1296 // created. This returns true if the section was found, false
1299 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1302 // Return the associated Output_section if there is one.
1304 get_output_section() const
1305 { return this->output_section_
; }
1307 // Print the contents to the FILE. This is for debugging.
1312 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1314 // The output section name.
1316 // The address. This may be NULL.
1317 Expression
* address_
;
1318 // The load address. This may be NULL.
1319 Expression
* load_address_
;
1320 // The alignment. This may be NULL.
1322 // The input section alignment. This may be NULL.
1323 Expression
* subalign_
;
1324 // The constraint, if any.
1325 Section_constraint constraint_
;
1326 // The fill value. This may be NULL.
1328 // The list of segments this section should go into. This may be
1330 String_list
* phdrs_
;
1331 // The list of elements defining the section.
1332 Output_section_elements elements_
;
1333 // The Output_section created for this definition. This will be
1334 // NULL if none was created.
1335 Output_section
* output_section_
;
1336 // The address after it has been evaluated.
1337 uint64_t evaluated_address_
;
1338 // The load address after it has been evaluated.
1339 uint64_t evaluated_load_address_
;
1340 // The alignment after it has been evaluated.
1341 uint64_t evaluated_addralign_
;
1342 // The output section is relro.
1348 Output_section_definition::Output_section_definition(
1351 const Parser_output_section_header
* header
)
1352 : name_(name
, namelen
),
1353 address_(header
->address
),
1354 load_address_(header
->load_address
),
1355 align_(header
->align
),
1356 subalign_(header
->subalign
),
1357 constraint_(header
->constraint
),
1361 output_section_(NULL
),
1362 evaluated_address_(0),
1363 evaluated_load_address_(0),
1364 evaluated_addralign_(0),
1369 // Finish an output section.
1372 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1374 this->fill_
= trailer
->fill
;
1375 this->phdrs_
= trailer
->phdrs
;
1378 // Add a symbol to be defined.
1381 Output_section_definition::add_symbol_assignment(const char* name
,
1387 Output_section_element
* p
= new Output_section_element_assignment(name
,
1392 this->elements_
.push_back(p
);
1395 // Add an assignment to the special dot symbol.
1398 Output_section_definition::add_dot_assignment(Expression
* value
)
1400 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1401 this->elements_
.push_back(p
);
1404 // Add an assertion.
1407 Output_section_definition::add_assertion(Expression
* check
,
1408 const char* message
,
1411 Output_section_element
* p
= new Output_section_element_assertion(check
,
1414 this->elements_
.push_back(p
);
1417 // Add a data item to the current output section.
1420 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1422 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1424 this->elements_
.push_back(p
);
1427 // Add a setting for the fill value.
1430 Output_section_definition::add_fill(Expression
* val
)
1432 Output_section_element
* p
= new Output_section_element_fill(val
);
1433 this->elements_
.push_back(p
);
1436 // Add an input section specification.
1439 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1442 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1443 this->elements_
.push_back(p
);
1446 // Create any required output sections. We need an output section if
1447 // there is a data statement here.
1450 Output_section_definition::create_sections(Layout
* layout
)
1452 if (this->output_section_
!= NULL
)
1454 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1455 p
!= this->elements_
.end();
1458 if ((*p
)->needs_output_section())
1460 const char* name
= this->name_
.c_str();
1461 this->output_section_
= layout
->make_output_section_for_script(name
);
1467 // Add any symbols being defined to the symbol table.
1470 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1472 for (Output_section_elements::iterator p
= this->elements_
.begin();
1473 p
!= this->elements_
.end();
1475 (*p
)->add_symbols_to_table(symtab
);
1478 // Finalize symbols and check assertions.
1481 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1482 const Layout
* layout
,
1483 uint64_t* dot_value
)
1485 if (this->output_section_
!= NULL
)
1486 *dot_value
= this->output_section_
->address();
1489 uint64_t address
= *dot_value
;
1490 if (this->address_
!= NULL
)
1492 Output_section
* dummy
;
1493 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1497 if (this->align_
!= NULL
)
1499 Output_section
* dummy
;
1500 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
1504 address
= align_address(address
, align
);
1506 *dot_value
= address
;
1509 Output_section
* dot_section
= this->output_section_
;
1510 for (Output_section_elements::iterator p
= this->elements_
.begin();
1511 p
!= this->elements_
.end();
1513 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1516 // Return the output section name to use for an input section name.
1519 Output_section_definition::output_section_name(const char* file_name
,
1520 const char* section_name
,
1521 Output_section
*** slot
)
1523 // Ask each element whether it matches NAME.
1524 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1525 p
!= this->elements_
.end();
1528 if ((*p
)->match_name(file_name
, section_name
))
1530 // We found a match for NAME, which means that it should go
1531 // into this output section.
1532 *slot
= &this->output_section_
;
1533 return this->name_
.c_str();
1537 // We don't know about this section name.
1541 // Return whether to place an orphan output section after this
1545 Output_section_definition::place_orphan_here(const Output_section
*os
,
1547 bool* is_relro
) const
1549 *is_relro
= this->is_relro_
;
1551 // Check for the simple case first.
1552 if (this->output_section_
!= NULL
1553 && this->output_section_
->type() == os
->type()
1554 && this->output_section_
->flags() == os
->flags())
1560 // Otherwise use some heuristics.
1562 if ((os
->flags() & elfcpp::SHF_ALLOC
) == 0)
1565 if (os
->type() == elfcpp::SHT_NOBITS
)
1567 if (this->name_
== ".bss")
1572 if (this->output_section_
!= NULL
1573 && this->output_section_
->type() == elfcpp::SHT_NOBITS
)
1576 else if (os
->type() == elfcpp::SHT_NOTE
)
1578 if (this->output_section_
!= NULL
1579 && this->output_section_
->type() == elfcpp::SHT_NOTE
)
1584 if (this->name_
.compare(0, 5, ".note") == 0)
1589 if (this->name_
== ".interp")
1591 if (this->output_section_
!= NULL
1592 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1593 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1596 else if (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
1598 if (this->name_
.compare(0, 4, ".rel") == 0)
1603 if (this->output_section_
!= NULL
1604 && (this->output_section_
->type() == elfcpp::SHT_REL
1605 || this->output_section_
->type() == elfcpp::SHT_RELA
))
1610 if (this->output_section_
!= NULL
1611 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1612 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1615 else if (os
->type() == elfcpp::SHT_PROGBITS
1616 && (os
->flags() & elfcpp::SHF_WRITE
) != 0)
1618 if (this->name_
== ".data")
1623 if (this->output_section_
!= NULL
1624 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1625 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1628 else if (os
->type() == elfcpp::SHT_PROGBITS
1629 && (os
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1631 if (this->name_
== ".text")
1636 if (this->output_section_
!= NULL
1637 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1638 && (this->output_section_
->flags() & elfcpp::SHF_EXECINSTR
) != 0)
1641 else if (os
->type() == elfcpp::SHT_PROGBITS
1642 || (os
->type() != elfcpp::SHT_PROGBITS
1643 && (os
->flags() & elfcpp::SHF_WRITE
) == 0))
1645 if (this->name_
== ".rodata")
1650 if (this->output_section_
!= NULL
1651 && this->output_section_
->type() == elfcpp::SHT_PROGBITS
1652 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1659 // Set the section address. Note that the OUTPUT_SECTION_ field will
1660 // be NULL if no input sections were mapped to this output section.
1661 // We still have to adjust dot and process symbol assignments.
1664 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1666 uint64_t* dot_value
,
1667 uint64_t* load_address
)
1670 if (this->address_
== NULL
)
1671 address
= *dot_value
;
1674 Output_section
* dummy
;
1675 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1676 *dot_value
, NULL
, &dummy
);
1680 if (this->align_
== NULL
)
1682 if (this->output_section_
== NULL
)
1685 align
= this->output_section_
->addralign();
1689 Output_section
* align_section
;
1690 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1691 NULL
, &align_section
);
1692 if (align_section
!= NULL
)
1693 gold_warning(_("alignment of section %s is not absolute"),
1694 this->name_
.c_str());
1695 if (this->output_section_
!= NULL
)
1696 this->output_section_
->set_addralign(align
);
1699 address
= align_address(address
, align
);
1701 uint64_t start_address
= address
;
1703 *dot_value
= address
;
1705 // The address of non-SHF_ALLOC sections is forced to zero,
1706 // regardless of what the linker script wants.
1707 if (this->output_section_
!= NULL
1708 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1709 this->output_section_
->set_address(address
);
1711 this->evaluated_address_
= address
;
1712 this->evaluated_addralign_
= align
;
1714 if (this->load_address_
== NULL
)
1715 this->evaluated_load_address_
= address
;
1718 Output_section
* dummy
;
1719 uint64_t load_address
=
1720 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1721 this->output_section_
, &dummy
);
1722 if (this->output_section_
!= NULL
)
1723 this->output_section_
->set_load_address(load_address
);
1724 this->evaluated_load_address_
= load_address
;
1728 if (this->subalign_
== NULL
)
1732 Output_section
* subalign_section
;
1733 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
1736 if (subalign_section
!= NULL
)
1737 gold_warning(_("subalign of section %s is not absolute"),
1738 this->name_
.c_str());
1742 if (this->fill_
!= NULL
)
1744 // FIXME: The GNU linker supports fill values of arbitrary
1746 Output_section
* fill_section
;
1747 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
1751 if (fill_section
!= NULL
)
1752 gold_warning(_("fill of section %s is not absolute"),
1753 this->name_
.c_str());
1754 unsigned char fill_buff
[4];
1755 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1756 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1759 Input_section_list input_sections
;
1760 if (this->output_section_
!= NULL
)
1762 // Get the list of input sections attached to this output
1763 // section. This will leave the output section with only
1764 // Output_section_data entries.
1765 address
+= this->output_section_
->get_input_sections(address
,
1768 *dot_value
= address
;
1771 Output_section
* dot_section
= this->output_section_
;
1772 for (Output_section_elements::iterator p
= this->elements_
.begin();
1773 p
!= this->elements_
.end();
1775 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
1776 subalign
, dot_value
, &dot_section
, &fill
,
1779 gold_assert(input_sections
.empty());
1781 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
1782 *load_address
= *dot_value
;
1784 *load_address
= (this->output_section_
->load_address()
1785 + (*dot_value
- start_address
));
1787 if (this->output_section_
!= NULL
)
1789 if (this->is_relro_
)
1790 this->output_section_
->set_is_relro();
1792 this->output_section_
->clear_is_relro();
1796 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1797 // this section is constrained, and the input sections do not match,
1798 // return the constraint, and set *POSD.
1801 Output_section_definition::check_constraint(Output_section_definition
** posd
)
1803 switch (this->constraint_
)
1805 case CONSTRAINT_NONE
:
1806 return CONSTRAINT_NONE
;
1808 case CONSTRAINT_ONLY_IF_RO
:
1809 if (this->output_section_
!= NULL
1810 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1813 return CONSTRAINT_ONLY_IF_RO
;
1815 return CONSTRAINT_NONE
;
1817 case CONSTRAINT_ONLY_IF_RW
:
1818 if (this->output_section_
!= NULL
1819 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1822 return CONSTRAINT_ONLY_IF_RW
;
1824 return CONSTRAINT_NONE
;
1826 case CONSTRAINT_SPECIAL
:
1827 if (this->output_section_
!= NULL
)
1828 gold_error(_("SPECIAL constraints are not implemented"));
1829 return CONSTRAINT_NONE
;
1836 // See if this is the alternate output section for a constrained
1837 // output section. If it is, transfer the Output_section and return
1838 // true. Otherwise return false.
1841 Output_section_definition::alternate_constraint(
1842 Output_section_definition
* posd
,
1843 Section_constraint constraint
)
1845 if (this->name_
!= posd
->name_
)
1850 case CONSTRAINT_ONLY_IF_RO
:
1851 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
1855 case CONSTRAINT_ONLY_IF_RW
:
1856 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
1864 // We have found the alternate constraint. We just need to move
1865 // over the Output_section. When constraints are used properly,
1866 // THIS should not have an output_section pointer, as all the input
1867 // sections should have matched the other definition.
1869 if (this->output_section_
!= NULL
)
1870 gold_error(_("mismatched definition for constrained sections"));
1872 this->output_section_
= posd
->output_section_
;
1873 posd
->output_section_
= NULL
;
1875 if (this->is_relro_
)
1876 this->output_section_
->set_is_relro();
1878 this->output_section_
->clear_is_relro();
1883 // Get the list of segments to use for an allocated section when using
1887 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
1890 if (this->output_section_
== NULL
)
1892 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
1895 if (this->phdrs_
!= NULL
)
1896 *phdrs_list
= this->phdrs_
;
1897 return this->output_section_
;
1900 // Look for an output section by name and return the address, the load
1901 // address, the alignment, and the size. This is used when an
1902 // expression refers to an output section which was not actually
1903 // created. This returns true if the section was found, false
1907 Output_section_definition::get_output_section_info(const char* name
,
1909 uint64_t* load_address
,
1910 uint64_t* addralign
,
1911 uint64_t* size
) const
1913 if (this->name_
!= name
)
1916 if (this->output_section_
!= NULL
)
1918 *address
= this->output_section_
->address();
1919 if (this->output_section_
->has_load_address())
1920 *load_address
= this->output_section_
->load_address();
1922 *load_address
= *address
;
1923 *addralign
= this->output_section_
->addralign();
1924 *size
= this->output_section_
->current_data_size();
1928 *address
= this->evaluated_address_
;
1929 *load_address
= this->evaluated_load_address_
;
1930 *addralign
= this->evaluated_addralign_
;
1937 // Print for debugging.
1940 Output_section_definition::print(FILE* f
) const
1942 fprintf(f
, " %s ", this->name_
.c_str());
1944 if (this->address_
!= NULL
)
1946 this->address_
->print(f
);
1952 if (this->load_address_
!= NULL
)
1955 this->load_address_
->print(f
);
1959 if (this->align_
!= NULL
)
1961 fprintf(f
, "ALIGN(");
1962 this->align_
->print(f
);
1966 if (this->subalign_
!= NULL
)
1968 fprintf(f
, "SUBALIGN(");
1969 this->subalign_
->print(f
);
1975 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1976 p
!= this->elements_
.end();
1982 if (this->fill_
!= NULL
)
1985 this->fill_
->print(f
);
1988 if (this->phdrs_
!= NULL
)
1990 for (String_list::const_iterator p
= this->phdrs_
->begin();
1991 p
!= this->phdrs_
->end();
1993 fprintf(f
, " :%s", p
->c_str());
1999 // An output section created to hold orphaned input sections. These
2000 // do not actually appear in linker scripts. However, for convenience
2001 // when setting the output section addresses, we put a marker to these
2002 // sections in the appropriate place in the list of SECTIONS elements.
2004 class Orphan_output_section
: public Sections_element
2007 Orphan_output_section(Output_section
* os
)
2011 // Return whether to place an orphan section after this one.
2013 place_orphan_here(const Output_section
*os
, bool* exact
, bool*) const;
2015 // Set section addresses.
2017 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
2019 // Get the list of segments to use for an allocated section when
2020 // using a PHDRS clause.
2022 allocate_to_segment(String_list
**, bool*);
2024 // Return the associated Output_section.
2026 get_output_section() const
2027 { return this->os_
; }
2029 // Print for debugging.
2031 print(FILE* f
) const
2033 fprintf(f
, " marker for orphaned output section %s\n",
2038 Output_section
* os_
;
2041 // Whether to place another orphan section after this one.
2044 Orphan_output_section::place_orphan_here(const Output_section
* os
,
2046 bool* is_relro
) const
2048 if (this->os_
->type() == os
->type()
2049 && this->os_
->flags() == os
->flags())
2052 *is_relro
= this->os_
->is_relro();
2058 // Set section addresses.
2061 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2062 uint64_t* dot_value
,
2063 uint64_t* load_address
)
2065 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
2067 bool have_load_address
= *load_address
!= *dot_value
;
2069 uint64_t address
= *dot_value
;
2070 address
= align_address(address
, this->os_
->addralign());
2072 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2074 this->os_
->set_address(address
);
2075 if (have_load_address
)
2076 this->os_
->set_load_address(align_address(*load_address
,
2077 this->os_
->addralign()));
2080 Input_section_list input_sections
;
2081 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2083 for (Input_section_list::iterator p
= input_sections
.begin();
2084 p
!= input_sections
.end();
2090 // We know what are single-threaded, so it is OK to lock the
2093 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2094 Task_lock_obj
<Object
> tl(task
, p
->first
);
2095 addralign
= p
->first
->section_addralign(p
->second
);
2096 size
= p
->first
->section_size(p
->second
);
2099 address
= align_address(address
, addralign
);
2100 this->os_
->add_input_section_for_script(p
->first
, p
->second
, size
,
2105 if (!have_load_address
)
2106 *load_address
= address
;
2108 *load_address
+= address
- *dot_value
;
2110 *dot_value
= address
;
2113 // Get the list of segments to use for an allocated section when using
2114 // a PHDRS clause. If this is an allocated section, return the
2115 // Output_section. We don't change the list of segments.
2118 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2120 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2126 // Class Phdrs_element. A program header from a PHDRS clause.
2131 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2132 bool includes_filehdr
, bool includes_phdrs
,
2133 bool is_flags_valid
, unsigned int flags
,
2134 Expression
* load_address
)
2135 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2136 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2137 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2141 // Return the name of this segment.
2144 { return this->name_
; }
2146 // Return the type of the segment.
2149 { return this->type_
; }
2151 // Whether to include the file header.
2153 includes_filehdr() const
2154 { return this->includes_filehdr_
; }
2156 // Whether to include the program headers.
2158 includes_phdrs() const
2159 { return this->includes_phdrs_
; }
2161 // Return whether there is a load address.
2163 has_load_address() const
2164 { return this->load_address_
!= NULL
; }
2166 // Evaluate the load address expression if there is one.
2168 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2170 if (this->load_address_
!= NULL
)
2171 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2175 // Return the load address.
2177 load_address() const
2179 gold_assert(this->load_address_
!= NULL
);
2180 return this->load_address_value_
;
2183 // Create the segment.
2185 create_segment(Layout
* layout
)
2187 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2188 return this->segment_
;
2191 // Return the segment.
2194 { return this->segment_
; }
2196 // Set the segment flags if appropriate.
2198 set_flags_if_valid()
2200 if (this->is_flags_valid_
)
2201 this->segment_
->set_flags(this->flags_
);
2204 // Print for debugging.
2209 // The name used in the script.
2211 // The type of the segment (PT_LOAD, etc.).
2213 // Whether this segment includes the file header.
2214 bool includes_filehdr_
;
2215 // Whether this segment includes the section headers.
2216 bool includes_phdrs_
;
2217 // Whether the flags were explicitly specified.
2218 bool is_flags_valid_
;
2219 // The flags for this segment (PF_R, etc.) if specified.
2220 unsigned int flags_
;
2221 // The expression for the load address for this segment. This may
2223 Expression
* load_address_
;
2224 // The actual load address from evaluating the expression.
2225 uint64_t load_address_value_
;
2226 // The segment itself.
2227 Output_segment
* segment_
;
2230 // Print for debugging.
2233 Phdrs_element::print(FILE* f
) const
2235 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2236 if (this->includes_filehdr_
)
2237 fprintf(f
, " FILEHDR");
2238 if (this->includes_phdrs_
)
2239 fprintf(f
, " PHDRS");
2240 if (this->is_flags_valid_
)
2241 fprintf(f
, " FLAGS(%u)", this->flags_
);
2242 if (this->load_address_
!= NULL
)
2245 this->load_address_
->print(f
);
2251 // Class Script_sections.
2253 Script_sections::Script_sections()
2254 : saw_sections_clause_(false),
2255 in_sections_clause_(false),
2256 sections_elements_(NULL
),
2257 output_section_(NULL
),
2258 phdrs_elements_(NULL
),
2259 data_segment_align_index_(-1U),
2260 saw_relro_end_(false)
2264 // Start a SECTIONS clause.
2267 Script_sections::start_sections()
2269 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2270 this->saw_sections_clause_
= true;
2271 this->in_sections_clause_
= true;
2272 if (this->sections_elements_
== NULL
)
2273 this->sections_elements_
= new Sections_elements
;
2276 // Finish a SECTIONS clause.
2279 Script_sections::finish_sections()
2281 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2282 this->in_sections_clause_
= false;
2285 // Add a symbol to be defined.
2288 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2289 Expression
* val
, bool provide
,
2292 if (this->output_section_
!= NULL
)
2293 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2297 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2300 this->sections_elements_
->push_back(p
);
2304 // Add an assignment to the special dot symbol.
2307 Script_sections::add_dot_assignment(Expression
* val
)
2309 if (this->output_section_
!= NULL
)
2310 this->output_section_
->add_dot_assignment(val
);
2313 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2314 this->sections_elements_
->push_back(p
);
2318 // Add an assertion.
2321 Script_sections::add_assertion(Expression
* check
, const char* message
,
2324 if (this->output_section_
!= NULL
)
2325 this->output_section_
->add_assertion(check
, message
, messagelen
);
2328 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2330 this->sections_elements_
->push_back(p
);
2334 // Start processing entries for an output section.
2337 Script_sections::start_output_section(
2340 const Parser_output_section_header
*header
)
2342 Output_section_definition
* posd
= new Output_section_definition(name
,
2345 this->sections_elements_
->push_back(posd
);
2346 gold_assert(this->output_section_
== NULL
);
2347 this->output_section_
= posd
;
2350 // Stop processing entries for an output section.
2353 Script_sections::finish_output_section(
2354 const Parser_output_section_trailer
* trailer
)
2356 gold_assert(this->output_section_
!= NULL
);
2357 this->output_section_
->finish(trailer
);
2358 this->output_section_
= NULL
;
2361 // Add a data item to the current output section.
2364 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2366 gold_assert(this->output_section_
!= NULL
);
2367 this->output_section_
->add_data(size
, is_signed
, val
);
2370 // Add a fill value setting to the current output section.
2373 Script_sections::add_fill(Expression
* val
)
2375 gold_assert(this->output_section_
!= NULL
);
2376 this->output_section_
->add_fill(val
);
2379 // Add an input section specification to the current output section.
2382 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2384 gold_assert(this->output_section_
!= NULL
);
2385 this->output_section_
->add_input_section(spec
, keep
);
2388 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
2389 // subsequent output sections may be relro.
2392 Script_sections::data_segment_align()
2394 if (this->data_segment_align_index_
!= -1U)
2395 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2396 this->data_segment_align_index_
= this->sections_elements_
->size();
2399 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
2400 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
2403 Script_sections::data_segment_relro_end()
2405 if (this->saw_relro_end_
)
2406 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2407 "in a linker script"));
2408 this->saw_relro_end_
= true;
2410 if (this->data_segment_align_index_
== -1U)
2411 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2414 for (size_t i
= this->data_segment_align_index_
;
2415 i
< this->sections_elements_
->size();
2417 (*this->sections_elements_
)[i
]->set_is_relro();
2421 // Create any required sections.
2424 Script_sections::create_sections(Layout
* layout
)
2426 if (!this->saw_sections_clause_
)
2428 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2429 p
!= this->sections_elements_
->end();
2431 (*p
)->create_sections(layout
);
2434 // Add any symbols we are defining to the symbol table.
2437 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2439 if (!this->saw_sections_clause_
)
2441 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2442 p
!= this->sections_elements_
->end();
2444 (*p
)->add_symbols_to_table(symtab
);
2447 // Finalize symbols and check assertions.
2450 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2452 if (!this->saw_sections_clause_
)
2454 uint64_t dot_value
= 0;
2455 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2456 p
!= this->sections_elements_
->end();
2458 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2461 // Return the name of the output section to use for an input file name
2462 // and section name.
2465 Script_sections::output_section_name(const char* file_name
,
2466 const char* section_name
,
2467 Output_section
*** output_section_slot
)
2469 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2470 p
!= this->sections_elements_
->end();
2473 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2474 output_section_slot
);
2478 // The special name /DISCARD/ means that the input section
2479 // should be discarded.
2480 if (strcmp(ret
, "/DISCARD/") == 0)
2482 *output_section_slot
= NULL
;
2489 // If we couldn't find a mapping for the name, the output section
2490 // gets the name of the input section.
2492 *output_section_slot
= NULL
;
2494 return section_name
;
2497 // Place a marker for an orphan output section into the SECTIONS
2501 Script_sections::place_orphan(Output_section
* os
)
2503 // Look for an output section definition which matches the output
2504 // section. Put a marker after that section.
2505 bool is_relro
= false;
2506 Sections_elements::iterator place
= this->sections_elements_
->end();
2507 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2508 p
!= this->sections_elements_
->end();
2513 if ((*p
)->place_orphan_here(os
, &exact
, &is_relro_here
))
2516 is_relro
= is_relro_here
;
2522 // The insert function puts the new element before the iterator.
2523 if (place
!= this->sections_elements_
->end())
2526 this->sections_elements_
->insert(place
, new Orphan_output_section(os
));
2531 os
->clear_is_relro();
2534 // Set the addresses of all the output sections. Walk through all the
2535 // elements, tracking the dot symbol. Apply assignments which set
2536 // absolute symbol values, in case they are used when setting dot.
2537 // Fill in data statement values. As we find output sections, set the
2538 // address, set the address of all associated input sections, and
2539 // update dot. Return the segment which should hold the file header
2540 // and segment headers, if any.
2543 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2545 gold_assert(this->saw_sections_clause_
);
2547 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2548 // for our representation.
2549 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2550 p
!= this->sections_elements_
->end();
2553 Output_section_definition
* posd
;
2554 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2555 if (failed_constraint
!= CONSTRAINT_NONE
)
2557 Sections_elements::iterator q
;
2558 for (q
= this->sections_elements_
->begin();
2559 q
!= this->sections_elements_
->end();
2564 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2569 if (q
== this->sections_elements_
->end())
2570 gold_error(_("no matching section constraint"));
2574 // Force the alignment of the first TLS section to be the maximum
2575 // alignment of all TLS sections.
2576 Output_section
* first_tls
= NULL
;
2577 uint64_t tls_align
= 0;
2578 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2579 p
!= this->sections_elements_
->end();
2582 Output_section
*os
= (*p
)->get_output_section();
2583 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
2585 if (first_tls
== NULL
)
2587 if (os
->addralign() > tls_align
)
2588 tls_align
= os
->addralign();
2591 if (first_tls
!= NULL
)
2592 first_tls
->set_addralign(tls_align
);
2594 // For a relocatable link, we implicitly set dot to zero.
2595 uint64_t dot_value
= 0;
2596 uint64_t load_address
= 0;
2597 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2598 p
!= this->sections_elements_
->end();
2600 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
2602 if (this->phdrs_elements_
!= NULL
)
2604 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2605 p
!= this->phdrs_elements_
->end();
2607 (*p
)->eval_load_address(symtab
, layout
);
2610 return this->create_segments(layout
);
2613 // Sort the sections in order to put them into segments.
2615 class Sort_output_sections
2619 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2623 Sort_output_sections::operator()(const Output_section
* os1
,
2624 const Output_section
* os2
) const
2626 // Sort first by the load address.
2627 uint64_t lma1
= (os1
->has_load_address()
2628 ? os1
->load_address()
2630 uint64_t lma2
= (os2
->has_load_address()
2631 ? os2
->load_address()
2636 // Then sort by the virtual address.
2637 if (os1
->address() != os2
->address())
2638 return os1
->address() < os2
->address();
2640 // Sort TLS sections to the end.
2641 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2642 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2646 // Sort PROGBITS before NOBITS.
2647 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2649 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2652 // Otherwise we don't care.
2656 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2657 // We treat a section with the SHF_TLS flag set as taking up space
2658 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2659 // space for them in the file.
2662 Script_sections::is_bss_section(const Output_section
* os
)
2664 return (os
->type() == elfcpp::SHT_NOBITS
2665 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2668 // Return the size taken by the file header and the program headers.
2671 Script_sections::total_header_size(Layout
* layout
) const
2673 size_t segment_count
= layout
->segment_count();
2674 size_t file_header_size
;
2675 size_t segment_headers_size
;
2676 if (parameters
->target().get_size() == 32)
2678 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2679 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2681 else if (parameters
->target().get_size() == 64)
2683 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2684 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2689 return file_header_size
+ segment_headers_size
;
2692 // Return the amount we have to subtract from the LMA to accomodate
2693 // headers of the given size. The complication is that the file
2694 // header have to be at the start of a page, as otherwise it will not
2695 // be at the start of the file.
2698 Script_sections::header_size_adjustment(uint64_t lma
,
2699 size_t sizeof_headers
) const
2701 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2702 uint64_t hdr_lma
= lma
- sizeof_headers
;
2703 hdr_lma
&= ~(abi_pagesize
- 1);
2704 return lma
- hdr_lma
;
2707 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2708 // the segment which should hold the file header and segment headers,
2712 Script_sections::create_segments(Layout
* layout
)
2714 gold_assert(this->saw_sections_clause_
);
2716 if (parameters
->options().relocatable())
2719 if (this->saw_phdrs_clause())
2720 return create_segments_from_phdrs_clause(layout
);
2722 Layout::Section_list sections
;
2723 layout
->get_allocated_sections(§ions
);
2725 // Sort the sections by address.
2726 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2728 this->create_note_and_tls_segments(layout
, §ions
);
2730 // Walk through the sections adding them to PT_LOAD segments.
2731 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2732 Output_segment
* first_seg
= NULL
;
2733 Output_segment
* current_seg
= NULL
;
2734 bool is_current_seg_readonly
= true;
2735 Layout::Section_list::iterator plast
= sections
.end();
2736 uint64_t last_vma
= 0;
2737 uint64_t last_lma
= 0;
2738 uint64_t last_size
= 0;
2739 for (Layout::Section_list::iterator p
= sections
.begin();
2740 p
!= sections
.end();
2743 const uint64_t vma
= (*p
)->address();
2744 const uint64_t lma
= ((*p
)->has_load_address()
2745 ? (*p
)->load_address()
2747 const uint64_t size
= (*p
)->current_data_size();
2749 bool need_new_segment
;
2750 if (current_seg
== NULL
)
2751 need_new_segment
= true;
2752 else if (lma
- vma
!= last_lma
- last_vma
)
2754 // This section has a different LMA relationship than the
2755 // last one; we need a new segment.
2756 need_new_segment
= true;
2758 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2759 < align_address(lma
, abi_pagesize
))
2761 // Putting this section in the segment would require
2763 need_new_segment
= true;
2765 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2767 // A non-BSS section can not follow a BSS section in the
2769 need_new_segment
= true;
2771 else if (is_current_seg_readonly
2772 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
2773 && !parameters
->options().omagic())
2775 // Don't put a writable section in the same segment as a
2776 // non-writable section.
2777 need_new_segment
= true;
2781 // Otherwise, reuse the existing segment.
2782 need_new_segment
= false;
2785 elfcpp::Elf_Word seg_flags
=
2786 Layout::section_flags_to_segment((*p
)->flags());
2788 if (need_new_segment
)
2790 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2792 current_seg
->set_addresses(vma
, lma
);
2793 if (first_seg
== NULL
)
2794 first_seg
= current_seg
;
2795 is_current_seg_readonly
= true;
2798 current_seg
->add_output_section(*p
, seg_flags
);
2800 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2801 is_current_seg_readonly
= false;
2809 // An ELF program should work even if the program headers are not in
2810 // a PT_LOAD segment. However, it appears that the Linux kernel
2811 // does not set the AT_PHDR auxiliary entry in that case. It sets
2812 // the load address to p_vaddr - p_offset of the first PT_LOAD
2813 // segment. It then sets AT_PHDR to the load address plus the
2814 // offset to the program headers, e_phoff in the file header. This
2815 // fails when the program headers appear in the file before the
2816 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2817 // segment to hold the file header and the program headers. This is
2818 // effectively what the GNU linker does, and it is slightly more
2819 // efficient in any case. We try to use the first PT_LOAD segment
2820 // if we can, otherwise we make a new one.
2822 if (first_seg
== NULL
)
2825 size_t sizeof_headers
= this->total_header_size(layout
);
2827 uint64_t vma
= first_seg
->vaddr();
2828 uint64_t lma
= first_seg
->paddr();
2830 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
2832 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
2834 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
2838 // If there is no room to squeeze in the headers, then punt. The
2839 // resulting executable probably won't run on GNU/Linux, but we
2840 // trust that the user knows what they are doing.
2841 if (lma
< subtract
|| vma
< subtract
)
2844 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2846 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
2851 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2852 // segment if there are any SHT_TLS sections.
2855 Script_sections::create_note_and_tls_segments(
2857 const Layout::Section_list
* sections
)
2859 gold_assert(!this->saw_phdrs_clause());
2861 bool saw_tls
= false;
2862 for (Layout::Section_list::const_iterator p
= sections
->begin();
2863 p
!= sections
->end();
2866 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2868 elfcpp::Elf_Word seg_flags
=
2869 Layout::section_flags_to_segment((*p
)->flags());
2870 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
2872 oseg
->add_output_section(*p
, seg_flags
);
2874 // Incorporate any subsequent SHT_NOTE sections, in the
2875 // hopes that the script is sensible.
2876 Layout::Section_list::const_iterator pnext
= p
+ 1;
2877 while (pnext
!= sections
->end()
2878 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
2880 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2881 oseg
->add_output_section(*pnext
, seg_flags
);
2887 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2890 gold_error(_("TLS sections are not adjacent"));
2892 elfcpp::Elf_Word seg_flags
=
2893 Layout::section_flags_to_segment((*p
)->flags());
2894 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
2896 oseg
->add_output_section(*p
, seg_flags
);
2898 Layout::Section_list::const_iterator pnext
= p
+ 1;
2899 while (pnext
!= sections
->end()
2900 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
2902 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
2903 oseg
->add_output_section(*pnext
, seg_flags
);
2913 // Add a program header. The PHDRS clause is syntactically distinct
2914 // from the SECTIONS clause, but we implement it with the SECTIONS
2915 // support becauase PHDRS is useless if there is no SECTIONS clause.
2918 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
2919 bool includes_filehdr
, bool includes_phdrs
,
2920 bool is_flags_valid
, unsigned int flags
,
2921 Expression
* load_address
)
2923 if (this->phdrs_elements_
== NULL
)
2924 this->phdrs_elements_
= new Phdrs_elements();
2925 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
2928 is_flags_valid
, flags
,
2932 // Return the number of segments we expect to create based on the
2933 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2936 Script_sections::expected_segment_count(const Layout
* layout
) const
2938 if (this->saw_phdrs_clause())
2939 return this->phdrs_elements_
->size();
2941 Layout::Section_list sections
;
2942 layout
->get_allocated_sections(§ions
);
2944 // We assume that we will need two PT_LOAD segments.
2947 bool saw_note
= false;
2948 bool saw_tls
= false;
2949 for (Layout::Section_list::const_iterator p
= sections
.begin();
2950 p
!= sections
.end();
2953 if ((*p
)->type() == elfcpp::SHT_NOTE
)
2955 // Assume that all note sections will fit into a single
2963 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
2965 // There can only be one PT_TLS segment.
2977 // Create the segments from a PHDRS clause. Return the segment which
2978 // should hold the file header and program headers, if any.
2981 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
2983 this->attach_sections_using_phdrs_clause(layout
);
2984 return this->set_phdrs_clause_addresses(layout
);
2987 // Create the segments from the PHDRS clause, and put the output
2988 // sections in them.
2991 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
2993 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
2994 Name_to_segment name_to_segment
;
2995 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
2996 p
!= this->phdrs_elements_
->end();
2998 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
3000 // Walk through the output sections and attach them to segments.
3001 // Output sections in the script which do not list segments are
3002 // attached to the same set of segments as the immediately preceding
3004 String_list
* phdr_names
= NULL
;
3005 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3006 p
!= this->sections_elements_
->end();
3010 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
3014 if (phdr_names
== NULL
)
3016 gold_error(_("allocated section not in any segment"));
3020 // If this is an orphan section--one that was not explicitly
3021 // mentioned in the linker script--then it should not inherit
3022 // any segment type other than PT_LOAD. Otherwise, e.g., the
3023 // PT_INTERP segment will pick up following orphan sections,
3024 // which does not make sense. If this is not an orphan section,
3025 // we trust the linker script.
3028 String_list::iterator q
= phdr_names
->begin();
3029 while (q
!= phdr_names
->end())
3031 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3032 // We give errors about unknown segments below.
3033 if (r
== name_to_segment
.end()
3034 || r
->second
->type() == elfcpp::PT_LOAD
)
3037 q
= phdr_names
->erase(q
);
3041 bool in_load_segment
= false;
3042 for (String_list::const_iterator q
= phdr_names
->begin();
3043 q
!= phdr_names
->end();
3046 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3047 if (r
== name_to_segment
.end())
3048 gold_error(_("no segment %s"), q
->c_str());
3051 elfcpp::Elf_Word seg_flags
=
3052 Layout::section_flags_to_segment(os
->flags());
3053 r
->second
->add_output_section(os
, seg_flags
);
3055 if (r
->second
->type() == elfcpp::PT_LOAD
)
3057 if (in_load_segment
)
3058 gold_error(_("section in two PT_LOAD segments"));
3059 in_load_segment
= true;
3064 if (!in_load_segment
)
3065 gold_error(_("allocated section not in any PT_LOAD segment"));
3069 // Set the addresses for segments created from a PHDRS clause. Return
3070 // the segment which should hold the file header and program headers,
3074 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
3076 Output_segment
* load_seg
= NULL
;
3077 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3078 p
!= this->phdrs_elements_
->end();
3081 // Note that we have to set the flags after adding the output
3082 // sections to the segment, as adding an output segment can
3083 // change the flags.
3084 (*p
)->set_flags_if_valid();
3086 Output_segment
* oseg
= (*p
)->segment();
3088 if (oseg
->type() != elfcpp::PT_LOAD
)
3090 // The addresses of non-PT_LOAD segments are set from the
3091 // PT_LOAD segments.
3092 if ((*p
)->has_load_address())
3093 gold_error(_("may only specify load address for PT_LOAD segment"));
3097 // The output sections should have addresses from the SECTIONS
3098 // clause. The addresses don't have to be in order, so find the
3099 // one with the lowest load address. Use that to set the
3100 // address of the segment.
3102 Output_section
* osec
= oseg
->section_with_lowest_load_address();
3105 oseg
->set_addresses(0, 0);
3109 uint64_t vma
= osec
->address();
3110 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3112 // Override the load address of the section with the load
3113 // address specified for the segment.
3114 if ((*p
)->has_load_address())
3116 if (osec
->has_load_address())
3117 gold_warning(_("PHDRS load address overrides "
3118 "section %s load address"),
3121 lma
= (*p
)->load_address();
3124 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3125 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3127 // We could support this if we wanted to.
3128 gold_error(_("using only one of FILEHDR and PHDRS is "
3129 "not currently supported"));
3133 size_t sizeof_headers
= this->total_header_size(layout
);
3134 uint64_t subtract
= this->header_size_adjustment(lma
,
3136 if (lma
>= subtract
&& vma
>= subtract
)
3143 gold_error(_("sections loaded on first page without room "
3144 "for file and program headers "
3145 "are not supported"));
3148 if (load_seg
!= NULL
)
3149 gold_error(_("using FILEHDR and PHDRS on more than one "
3150 "PT_LOAD segment is not currently supported"));
3154 oseg
->set_addresses(vma
, lma
);
3160 // Add the file header and segment headers to non-load segments
3161 // specified in the PHDRS clause.
3164 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3165 Output_data
* segment_headers
)
3167 gold_assert(this->saw_phdrs_clause());
3168 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3169 p
!= this->phdrs_elements_
->end();
3172 if ((*p
)->type() != elfcpp::PT_LOAD
)
3174 if ((*p
)->includes_phdrs())
3175 (*p
)->segment()->add_initial_output_data(segment_headers
);
3176 if ((*p
)->includes_filehdr())
3177 (*p
)->segment()->add_initial_output_data(file_header
);
3182 // Look for an output section by name and return the address, the load
3183 // address, the alignment, and the size. This is used when an
3184 // expression refers to an output section which was not actually
3185 // created. This returns true if the section was found, false
3189 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3190 uint64_t* load_address
,
3191 uint64_t* addralign
,
3192 uint64_t* size
) const
3194 if (!this->saw_sections_clause_
)
3196 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3197 p
!= this->sections_elements_
->end();
3199 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3205 // Print the SECTIONS clause to F for debugging.
3208 Script_sections::print(FILE* f
) const
3210 if (!this->saw_sections_clause_
)
3213 fprintf(f
, "SECTIONS {\n");
3215 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3216 p
!= this->sections_elements_
->end();
3222 if (this->phdrs_elements_
!= NULL
)
3224 fprintf(f
, "PHDRS {\n");
3225 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3226 p
!= this->phdrs_elements_
->end();
3233 } // End namespace gold.