1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008, 2009 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 // Manage orphan sections. This is intended to be largely compatible
47 // with the GNU linker. The Linux kernel implicitly relies on
48 // something similar to the GNU linker's orphan placement. We
49 // originally used a simpler scheme here, but it caused the kernel
50 // build to fail, and was also rather inefficient.
52 class Orphan_section_placement
55 typedef Script_sections::Elements_iterator Elements_iterator
;
58 Orphan_section_placement();
60 // Handle an output section during initialization of this mapping.
62 output_section_init(const std::string
& name
, Output_section
*,
63 Elements_iterator location
);
65 // Initialize the last location.
67 last_init(Elements_iterator location
);
69 // Set *PWHERE to the address of an iterator pointing to the
70 // location to use for an orphan section. Return true if the
71 // iterator has a value, false otherwise.
73 find_place(Output_section
*, Elements_iterator
** pwhere
);
75 // Return the iterator being used for sections at the very end of
81 // The places that we specifically recognize. This list is copied
82 // from the GNU linker.
98 // The information we keep for a specific place.
101 // The name of sections for this place.
103 // Whether we have a location for this place.
105 // The iterator for this place.
106 Elements_iterator location
;
109 // Initialize one place element.
111 initialize_place(Place_index
, const char*);
114 Place places_
[PLACE_MAX
];
115 // True if this is the first call to output_section_init.
119 // Initialize Orphan_section_placement.
121 Orphan_section_placement::Orphan_section_placement()
124 this->initialize_place(PLACE_TEXT
, ".text");
125 this->initialize_place(PLACE_RODATA
, ".rodata");
126 this->initialize_place(PLACE_DATA
, ".data");
127 this->initialize_place(PLACE_TLS
, NULL
);
128 this->initialize_place(PLACE_TLS_BSS
, NULL
);
129 this->initialize_place(PLACE_BSS
, ".bss");
130 this->initialize_place(PLACE_REL
, NULL
);
131 this->initialize_place(PLACE_INTERP
, ".interp");
132 this->initialize_place(PLACE_NONALLOC
, NULL
);
133 this->initialize_place(PLACE_LAST
, NULL
);
136 // Initialize one place element.
139 Orphan_section_placement::initialize_place(Place_index index
, const char* name
)
141 this->places_
[index
].name
= name
;
142 this->places_
[index
].have_location
= false;
145 // While initializing the Orphan_section_placement information, this
146 // is called once for each output section named in the linker script.
147 // If we found an output section during the link, it will be passed in
151 Orphan_section_placement::output_section_init(const std::string
& name
,
153 Elements_iterator location
)
155 bool first_init
= this->first_init_
;
156 this->first_init_
= false;
158 for (int i
= 0; i
< PLACE_MAX
; ++i
)
160 if (this->places_
[i
].name
!= NULL
&& this->places_
[i
].name
== name
)
162 if (this->places_
[i
].have_location
)
164 // We have already seen a section with this name.
168 this->places_
[i
].location
= location
;
169 this->places_
[i
].have_location
= true;
171 // If we just found the .bss section, restart the search for
172 // an unallocated section. This follows the GNU linker's
175 this->places_
[PLACE_NONALLOC
].have_location
= false;
181 // Relocation sections.
182 if (!this->places_
[PLACE_REL
].have_location
184 && (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
185 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
187 this->places_
[PLACE_REL
].location
= location
;
188 this->places_
[PLACE_REL
].have_location
= true;
191 // We find the location for unallocated sections by finding the
192 // first debugging or comment section after the BSS section (if
194 if (!this->places_
[PLACE_NONALLOC
].have_location
195 && (name
== ".comment" || Layout::is_debug_info_section(name
.c_str())))
197 // We add orphan sections after the location in PLACES_. We
198 // want to store unallocated sections before LOCATION. If this
199 // is the very first section, we can't use it.
203 this->places_
[PLACE_NONALLOC
].location
= location
;
204 this->places_
[PLACE_NONALLOC
].have_location
= true;
209 // Initialize the last location.
212 Orphan_section_placement::last_init(Elements_iterator location
)
214 this->places_
[PLACE_LAST
].location
= location
;
215 this->places_
[PLACE_LAST
].have_location
= true;
218 // Set *PWHERE to the address of an iterator pointing to the location
219 // to use for an orphan section. Return true if the iterator has a
220 // value, false otherwise.
223 Orphan_section_placement::find_place(Output_section
* os
,
224 Elements_iterator
** pwhere
)
226 // Figure out where OS should go. This is based on the GNU linker
227 // code. FIXME: The GNU linker handles small data sections
228 // specially, but we don't.
229 elfcpp::Elf_Word type
= os
->type();
230 elfcpp::Elf_Xword flags
= os
->flags();
232 if ((flags
& elfcpp::SHF_ALLOC
) == 0
233 && !Layout::is_debug_info_section(os
->name()))
234 index
= PLACE_NONALLOC
;
235 else if ((flags
& elfcpp::SHF_ALLOC
) == 0)
237 else if (type
== elfcpp::SHT_NOTE
)
238 index
= PLACE_INTERP
;
239 else if ((flags
& elfcpp::SHF_TLS
) != 0)
241 if (type
== elfcpp::SHT_NOBITS
)
242 index
= PLACE_TLS_BSS
;
246 else if (type
== elfcpp::SHT_NOBITS
)
248 else if ((flags
& elfcpp::SHF_WRITE
) != 0)
250 else if (type
== elfcpp::SHT_REL
|| type
== elfcpp::SHT_RELA
)
252 else if ((flags
& elfcpp::SHF_EXECINSTR
) == 0)
253 index
= PLACE_RODATA
;
257 // If we don't have a location yet, try to find one based on a
258 // plausible ordering of sections.
259 if (!this->places_
[index
].have_location
)
284 if (!this->places_
[PLACE_TLS
].have_location
)
288 if (follow
!= PLACE_MAX
&& this->places_
[follow
].have_location
)
290 // Set the location of INDEX to the location of FOLLOW. The
291 // location of INDEX will then be incremented by the caller,
292 // so anything in INDEX will continue to be after anything
294 this->places_
[index
].location
= this->places_
[follow
].location
;
295 this->places_
[index
].have_location
= true;
299 *pwhere
= &this->places_
[index
].location
;
300 bool ret
= this->places_
[index
].have_location
;
302 // The caller will set the location.
303 this->places_
[index
].have_location
= true;
308 // Return the iterator being used for sections at the very end of the
311 Orphan_section_placement::Elements_iterator
312 Orphan_section_placement::last_place() const
314 gold_assert(this->places_
[PLACE_LAST
].have_location
);
315 return this->places_
[PLACE_LAST
].location
;
318 // An element in a SECTIONS clause.
320 class Sections_element
326 virtual ~Sections_element()
329 // Return whether an output section is relro.
334 // Record that an output section is relro.
339 // Create any required output sections. The only real
340 // implementation is in Output_section_definition.
342 create_sections(Layout
*)
345 // Add any symbol being defined to the symbol table.
347 add_symbols_to_table(Symbol_table
*)
350 // Finalize symbols and check assertions.
352 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*)
355 // Return the output section name to use for an input file name and
356 // section name. This only real implementation is in
357 // Output_section_definition.
359 output_section_name(const char*, const char*, Output_section
***,
360 Script_sections::Section_type
*)
363 // Initialize OSP with an output section.
365 orphan_section_init(Orphan_section_placement
*,
366 Script_sections::Elements_iterator
)
369 // Set section addresses. This includes applying assignments if the
370 // the expression is an absolute value.
372 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*)
375 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
376 // this section is constrained, and the input sections do not match,
377 // return the constraint, and set *POSD.
378 virtual Section_constraint
379 check_constraint(Output_section_definition
**)
380 { return CONSTRAINT_NONE
; }
382 // See if this is the alternate output section for a constrained
383 // output section. If it is, transfer the Output_section and return
384 // true. Otherwise return false.
386 alternate_constraint(Output_section_definition
*, Section_constraint
)
389 // Get the list of segments to use for an allocated section when
390 // using a PHDRS clause. If this is an allocated section, return
391 // the Output_section, and set *PHDRS_LIST (the first parameter) to
392 // the list of PHDRS to which it should be attached. If the PHDRS
393 // were not specified, don't change *PHDRS_LIST. When not returning
394 // NULL, set *ORPHAN (the second parameter) according to whether
395 // this is an orphan section--one that is not mentioned in the
397 virtual Output_section
*
398 allocate_to_segment(String_list
**, bool*)
401 // Look for an output section by name and return the address, the
402 // load address, the alignment, and the size. This is used when an
403 // expression refers to an output section which was not actually
404 // created. This returns true if the section was found, false
405 // otherwise. The only real definition is for
406 // Output_section_definition.
408 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
412 // Return the associated Output_section if there is one.
413 virtual Output_section
*
414 get_output_section() const
417 // Print the element for debugging purposes.
419 print(FILE* f
) const = 0;
422 // An assignment in a SECTIONS clause outside of an output section.
424 class Sections_element_assignment
: public Sections_element
427 Sections_element_assignment(const char* name
, size_t namelen
,
428 Expression
* val
, bool provide
, bool hidden
)
429 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
432 // Add the symbol to the symbol table.
434 add_symbols_to_table(Symbol_table
* symtab
)
435 { this->assignment_
.add_to_table(symtab
); }
437 // Finalize the symbol.
439 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
442 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
445 // Set the section address. There is no section here, but if the
446 // value is absolute, we set the symbol. This permits us to use
447 // absolute symbols when setting dot.
449 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
450 uint64_t* dot_value
, uint64_t*)
452 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
455 // Print for debugging.
460 this->assignment_
.print(f
);
464 Symbol_assignment assignment_
;
467 // An assignment to the dot symbol in a SECTIONS clause outside of an
470 class Sections_element_dot_assignment
: public Sections_element
473 Sections_element_dot_assignment(Expression
* val
)
477 // Finalize the symbol.
479 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
482 // We ignore the section of the result because outside of an
483 // output section definition the dot symbol is always considered
485 Output_section
* dummy
;
486 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
490 // Update the dot symbol while setting section addresses.
492 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
493 uint64_t* dot_value
, uint64_t* load_address
)
495 Output_section
* dummy
;
496 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
498 *load_address
= *dot_value
;
501 // Print for debugging.
506 this->val_
->print(f
);
514 // An assertion in a SECTIONS clause outside of an output section.
516 class Sections_element_assertion
: public Sections_element
519 Sections_element_assertion(Expression
* check
, const char* message
,
521 : assertion_(check
, message
, messagelen
)
524 // Check the assertion.
526 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
527 { this->assertion_
.check(symtab
, layout
); }
529 // Print for debugging.
534 this->assertion_
.print(f
);
538 Script_assertion assertion_
;
541 // An element in an output section in a SECTIONS clause.
543 class Output_section_element
546 // A list of input sections.
547 typedef std::list
<Output_section::Simple_input_section
> Input_section_list
;
549 Output_section_element()
552 virtual ~Output_section_element()
555 // Return whether this element requires an output section to exist.
557 needs_output_section() const
560 // Add any symbol being defined to the symbol table.
562 add_symbols_to_table(Symbol_table
*)
565 // Finalize symbols and check assertions.
567 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
570 // Return whether this element matches FILE_NAME and SECTION_NAME.
571 // The only real implementation is in Output_section_element_input.
573 match_name(const char*, const char*) const
576 // Set section addresses. This includes applying assignments if the
577 // the expression is an absolute value.
579 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
580 uint64_t*, Output_section
**, std::string
*,
584 // Print the element for debugging purposes.
586 print(FILE* f
) const = 0;
589 // Return a fill string that is LENGTH bytes long, filling it with
592 get_fill_string(const std::string
* fill
, section_size_type length
) const;
596 Output_section_element::get_fill_string(const std::string
* fill
,
597 section_size_type length
) const
599 std::string this_fill
;
600 this_fill
.reserve(length
);
601 while (this_fill
.length() + fill
->length() <= length
)
603 if (this_fill
.length() < length
)
604 this_fill
.append(*fill
, 0, length
- this_fill
.length());
608 // A symbol assignment in an output section.
610 class Output_section_element_assignment
: public Output_section_element
613 Output_section_element_assignment(const char* name
, size_t namelen
,
614 Expression
* val
, bool provide
,
616 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
619 // Add the symbol to the symbol table.
621 add_symbols_to_table(Symbol_table
* symtab
)
622 { this->assignment_
.add_to_table(symtab
); }
624 // Finalize the symbol.
626 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
627 uint64_t* dot_value
, Output_section
** dot_section
)
629 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
633 // Set the section address. There is no section here, but if the
634 // value is absolute, we set the symbol. This permits us to use
635 // absolute symbols when setting dot.
637 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
638 uint64_t, uint64_t* dot_value
, Output_section
**,
639 std::string
*, Input_section_list
*)
641 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
644 // Print for debugging.
649 this->assignment_
.print(f
);
653 Symbol_assignment assignment_
;
656 // An assignment to the dot symbol in an output section.
658 class Output_section_element_dot_assignment
: public Output_section_element
661 Output_section_element_dot_assignment(Expression
* val
)
665 // Finalize the symbol.
667 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
668 uint64_t* dot_value
, Output_section
** dot_section
)
670 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
671 *dot_section
, dot_section
);
674 // Update the dot symbol while setting section addresses.
676 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
677 uint64_t, uint64_t* dot_value
, Output_section
**,
678 std::string
*, Input_section_list
*);
680 // Print for debugging.
685 this->val_
->print(f
);
693 // Update the dot symbol while setting section addresses.
696 Output_section_element_dot_assignment::set_section_addresses(
697 Symbol_table
* symtab
,
699 Output_section
* output_section
,
702 Output_section
** dot_section
,
706 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
707 *dot_value
, *dot_section
,
709 if (next_dot
< *dot_value
)
710 gold_error(_("dot may not move backward"));
711 if (next_dot
> *dot_value
&& output_section
!= NULL
)
713 section_size_type length
= convert_to_section_size_type(next_dot
715 Output_section_data
* posd
;
717 posd
= new Output_data_zero_fill(length
, 0);
720 std::string this_fill
= this->get_fill_string(fill
, length
);
721 posd
= new Output_data_const(this_fill
, 0);
723 output_section
->add_output_section_data(posd
);
724 layout
->new_output_section_data_from_script(posd
);
726 *dot_value
= next_dot
;
729 // An assertion in an output section.
731 class Output_section_element_assertion
: public Output_section_element
734 Output_section_element_assertion(Expression
* check
, const char* message
,
736 : assertion_(check
, message
, messagelen
)
743 this->assertion_
.print(f
);
747 Script_assertion assertion_
;
750 // We use a special instance of Output_section_data to handle BYTE,
751 // SHORT, etc. This permits forward references to symbols in the
754 class Output_data_expression
: public Output_section_data
757 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
758 const Symbol_table
* symtab
, const Layout
* layout
,
759 uint64_t dot_value
, Output_section
* dot_section
)
760 : Output_section_data(size
, 0, true),
761 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
762 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
766 // Write the data to the output file.
768 do_write(Output_file
*);
770 // Write the data to a buffer.
772 do_write_to_buffer(unsigned char*);
774 // Write to a map file.
776 do_print_to_mapfile(Mapfile
* mapfile
) const
777 { mapfile
->print_output_data(this, _("** expression")); }
780 template<bool big_endian
>
782 endian_write_to_buffer(uint64_t, unsigned char*);
786 const Symbol_table
* symtab_
;
787 const Layout
* layout_
;
789 Output_section
* dot_section_
;
792 // Write the data element to the output file.
795 Output_data_expression::do_write(Output_file
* of
)
797 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
798 this->write_to_buffer(view
);
799 of
->write_output_view(this->offset(), this->data_size(), view
);
802 // Write the data element to a buffer.
805 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
807 Output_section
* dummy
;
808 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
809 true, this->dot_value_
,
810 this->dot_section_
, &dummy
);
812 if (parameters
->target().is_big_endian())
813 this->endian_write_to_buffer
<true>(val
, buf
);
815 this->endian_write_to_buffer
<false>(val
, buf
);
818 template<bool big_endian
>
820 Output_data_expression::endian_write_to_buffer(uint64_t val
,
823 switch (this->data_size())
826 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
829 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
832 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
835 if (parameters
->target().get_size() == 32)
838 if (this->is_signed_
&& (val
& 0x80000000) != 0)
839 val
|= 0xffffffff00000000LL
;
841 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
848 // A data item in an output section.
850 class Output_section_element_data
: public Output_section_element
853 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
854 : size_(size
), is_signed_(is_signed
), val_(val
)
857 // If there is a data item, then we must create an output section.
859 needs_output_section() const
862 // Finalize symbols--we just need to update dot.
864 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
866 { *dot_value
+= this->size_
; }
868 // Store the value in the section.
870 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
871 uint64_t* dot_value
, Output_section
**, std::string
*,
872 Input_section_list
*);
874 // Print for debugging.
879 // The size in bytes.
881 // Whether the value is signed.
887 // Store the value in the section.
890 Output_section_element_data::set_section_addresses(
891 Symbol_table
* symtab
,
896 Output_section
** dot_section
,
900 gold_assert(os
!= NULL
);
901 Output_data_expression
* expression
=
902 new Output_data_expression(this->size_
, this->is_signed_
, this->val_
,
903 symtab
, layout
, *dot_value
, *dot_section
);
904 os
->add_output_section_data(expression
);
905 layout
->new_output_section_data_from_script(expression
);
906 *dot_value
+= this->size_
;
909 // Print for debugging.
912 Output_section_element_data::print(FILE* f
) const
927 if (this->is_signed_
)
935 fprintf(f
, " %s(", s
);
936 this->val_
->print(f
);
940 // A fill value setting in an output section.
942 class Output_section_element_fill
: public Output_section_element
945 Output_section_element_fill(Expression
* val
)
949 // Update the fill value while setting section addresses.
951 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
952 uint64_t, uint64_t* dot_value
,
953 Output_section
** dot_section
,
954 std::string
* fill
, Input_section_list
*)
956 Output_section
* fill_section
;
957 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
958 *dot_value
, *dot_section
,
960 if (fill_section
!= NULL
)
961 gold_warning(_("fill value is not absolute"));
962 // FIXME: The GNU linker supports fill values of arbitrary length.
963 unsigned char fill_buff
[4];
964 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
965 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
968 // Print for debugging.
972 fprintf(f
, " FILL(");
973 this->val_
->print(f
);
978 // The new fill value.
982 // Return whether STRING contains a wildcard character. This is used
983 // to speed up matching.
986 is_wildcard_string(const std::string
& s
)
988 return strpbrk(s
.c_str(), "?*[") != NULL
;
991 // An input section specification in an output section
993 class Output_section_element_input
: public Output_section_element
996 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
998 // Finalize symbols--just update the value of the dot symbol.
1000 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1001 Output_section
** dot_section
)
1003 *dot_value
= this->final_dot_value_
;
1004 *dot_section
= this->final_dot_section_
;
1007 // See whether we match FILE_NAME and SECTION_NAME as an input
1010 match_name(const char* file_name
, const char* section_name
) const;
1012 // Set the section address.
1014 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1015 uint64_t subalign
, uint64_t* dot_value
,
1016 Output_section
**, std::string
* fill
,
1017 Input_section_list
*);
1019 // Print for debugging.
1021 print(FILE* f
) const;
1024 // An input section pattern.
1025 struct Input_section_pattern
1027 std::string pattern
;
1028 bool pattern_is_wildcard
;
1031 Input_section_pattern(const char* patterna
, size_t patternlena
,
1032 Sort_wildcard sorta
)
1033 : pattern(patterna
, patternlena
),
1034 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
1039 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
1041 // Filename_exclusions is a pair of filename pattern and a bool
1042 // indicating whether the filename is a wildcard.
1043 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
1045 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1046 // indicates whether this is a wildcard pattern.
1048 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
1050 return (is_wildcard_pattern
1051 ? fnmatch(pattern
, string
, 0) == 0
1052 : strcmp(string
, pattern
) == 0);
1055 // See if we match a file name.
1057 match_file_name(const char* file_name
) const;
1059 // The file name pattern. If this is the empty string, we match all
1061 std::string filename_pattern_
;
1062 // Whether the file name pattern is a wildcard.
1063 bool filename_is_wildcard_
;
1064 // How the file names should be sorted. This may only be
1065 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1066 Sort_wildcard filename_sort_
;
1067 // The list of file names to exclude.
1068 Filename_exclusions filename_exclusions_
;
1069 // The list of input section patterns.
1070 Input_section_patterns input_section_patterns_
;
1071 // Whether to keep this section when garbage collecting.
1073 // The value of dot after including all matching sections.
1074 uint64_t final_dot_value_
;
1075 // The section where dot is defined after including all matching
1077 Output_section
* final_dot_section_
;
1080 // Construct Output_section_element_input. The parser records strings
1081 // as pointers into a copy of the script file, which will go away when
1082 // parsing is complete. We make sure they are in std::string objects.
1084 Output_section_element_input::Output_section_element_input(
1085 const Input_section_spec
* spec
,
1087 : filename_pattern_(),
1088 filename_is_wildcard_(false),
1089 filename_sort_(spec
->file
.sort
),
1090 filename_exclusions_(),
1091 input_section_patterns_(),
1093 final_dot_value_(0),
1094 final_dot_section_(NULL
)
1096 // The filename pattern "*" is common, and matches all files. Turn
1097 // it into the empty string.
1098 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
1099 this->filename_pattern_
.assign(spec
->file
.name
.value
,
1100 spec
->file
.name
.length
);
1101 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
1103 if (spec
->input_sections
.exclude
!= NULL
)
1105 for (String_list::const_iterator p
=
1106 spec
->input_sections
.exclude
->begin();
1107 p
!= spec
->input_sections
.exclude
->end();
1110 bool is_wildcard
= is_wildcard_string(*p
);
1111 this->filename_exclusions_
.push_back(std::make_pair(*p
,
1116 if (spec
->input_sections
.sections
!= NULL
)
1118 Input_section_patterns
& isp(this->input_section_patterns_
);
1119 for (String_sort_list::const_iterator p
=
1120 spec
->input_sections
.sections
->begin();
1121 p
!= spec
->input_sections
.sections
->end();
1123 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
1128 // See whether we match FILE_NAME.
1131 Output_section_element_input::match_file_name(const char* file_name
) const
1133 if (!this->filename_pattern_
.empty())
1135 // If we were called with no filename, we refuse to match a
1136 // pattern which requires a file name.
1137 if (file_name
== NULL
)
1140 if (!match(file_name
, this->filename_pattern_
.c_str(),
1141 this->filename_is_wildcard_
))
1145 if (file_name
!= NULL
)
1147 // Now we have to see whether FILE_NAME matches one of the
1148 // exclusion patterns, if any.
1149 for (Filename_exclusions::const_iterator p
=
1150 this->filename_exclusions_
.begin();
1151 p
!= this->filename_exclusions_
.end();
1154 if (match(file_name
, p
->first
.c_str(), p
->second
))
1162 // See whether we match FILE_NAME and SECTION_NAME.
1165 Output_section_element_input::match_name(const char* file_name
,
1166 const char* section_name
) const
1168 if (!this->match_file_name(file_name
))
1171 // If there are no section name patterns, then we match.
1172 if (this->input_section_patterns_
.empty())
1175 // See whether we match the section name patterns.
1176 for (Input_section_patterns::const_iterator p
=
1177 this->input_section_patterns_
.begin();
1178 p
!= this->input_section_patterns_
.end();
1181 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
1185 // We didn't match any section names, so we didn't match.
1189 // Information we use to sort the input sections.
1191 class Input_section_info
1194 Input_section_info(const Output_section::Simple_input_section
& input_section
)
1195 : input_section_(input_section
), section_name_(),
1196 size_(0), addralign_(1)
1199 // Return the simple input section.
1200 const Output_section::Simple_input_section
&
1201 input_section() const
1202 { return this->input_section_
; }
1204 // Return the object.
1207 { return this->input_section_
.relobj(); }
1209 // Return the section index.
1212 { return this->input_section_
.shndx(); }
1214 // Return the section name.
1216 section_name() const
1217 { return this->section_name_
; }
1219 // Set the section name.
1221 set_section_name(const std::string name
)
1222 { this->section_name_
= name
; }
1224 // Return the section size.
1227 { return this->size_
; }
1229 // Set the section size.
1231 set_size(uint64_t size
)
1232 { this->size_
= size
; }
1234 // Return the address alignment.
1237 { return this->addralign_
; }
1239 // Set the address alignment.
1241 set_addralign(uint64_t addralign
)
1242 { this->addralign_
= addralign
; }
1245 // Input section, can be a relaxed section.
1246 Output_section::Simple_input_section input_section_
;
1247 // Name of the section.
1248 std::string section_name_
;
1251 // Address alignment.
1252 uint64_t addralign_
;
1255 // A class to sort the input sections.
1257 class Input_section_sorter
1260 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
1261 : filename_sort_(filename_sort
), section_sort_(section_sort
)
1265 operator()(const Input_section_info
&, const Input_section_info
&) const;
1268 Sort_wildcard filename_sort_
;
1269 Sort_wildcard section_sort_
;
1273 Input_section_sorter::operator()(const Input_section_info
& isi1
,
1274 const Input_section_info
& isi2
) const
1276 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
1277 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1278 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1279 && isi1
.addralign() == isi2
.addralign()))
1281 if (isi1
.section_name() != isi2
.section_name())
1282 return isi1
.section_name() < isi2
.section_name();
1284 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
1285 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1286 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
1288 if (isi1
.addralign() != isi2
.addralign())
1289 return isi1
.addralign() < isi2
.addralign();
1291 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
1293 if (isi1
.relobj()->name() != isi2
.relobj()->name())
1294 return (isi1
.relobj()->name() < isi2
.relobj()->name());
1297 // Otherwise we leave them in the same order.
1301 // Set the section address. Look in INPUT_SECTIONS for sections which
1302 // match this spec, sort them as specified, and add them to the output
1306 Output_section_element_input::set_section_addresses(
1309 Output_section
* output_section
,
1311 uint64_t* dot_value
,
1312 Output_section
** dot_section
,
1314 Input_section_list
* input_sections
)
1316 // We build a list of sections which match each
1317 // Input_section_pattern.
1319 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
1320 size_t input_pattern_count
= this->input_section_patterns_
.size();
1321 if (input_pattern_count
== 0)
1322 input_pattern_count
= 1;
1323 Matching_sections
matching_sections(input_pattern_count
);
1325 // Look through the list of sections for this output section. Add
1326 // each one which matches to one of the elements of
1327 // MATCHING_SECTIONS.
1329 Input_section_list::iterator p
= input_sections
->begin();
1330 while (p
!= input_sections
->end())
1332 Relobj
* relobj
= p
->relobj();
1333 unsigned int shndx
= p
->shndx();
1334 Input_section_info
isi(*p
);
1336 // Calling section_name and section_addralign is not very
1339 // Lock the object so that we can get information about the
1340 // section. This is OK since we know we are single-threaded
1343 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1344 Task_lock_obj
<Object
> tl(task
, relobj
);
1346 isi
.set_section_name(relobj
->section_name(shndx
));
1347 if (p
->is_relaxed_input_section())
1349 // We use current data size because relxed section sizes may not
1350 // have finalized yet.
1351 isi
.set_size(p
->relaxed_input_section()->current_data_size());
1352 isi
.set_addralign(p
->relaxed_input_section()->addralign());
1356 isi
.set_size(relobj
->section_size(shndx
));
1357 isi
.set_addralign(relobj
->section_addralign(shndx
));
1361 if (!this->match_file_name(relobj
->name().c_str()))
1363 else if (this->input_section_patterns_
.empty())
1365 matching_sections
[0].push_back(isi
);
1366 p
= input_sections
->erase(p
);
1371 for (i
= 0; i
< input_pattern_count
; ++i
)
1373 const Input_section_pattern
&
1374 isp(this->input_section_patterns_
[i
]);
1375 if (match(isi
.section_name().c_str(), isp
.pattern
.c_str(),
1376 isp
.pattern_is_wildcard
))
1380 if (i
>= this->input_section_patterns_
.size())
1384 matching_sections
[i
].push_back(isi
);
1385 p
= input_sections
->erase(p
);
1390 // Look through MATCHING_SECTIONS. Sort each one as specified,
1391 // using a stable sort so that we get the default order when
1392 // sections are otherwise equal. Add each input section to the
1395 uint64_t dot
= *dot_value
;
1396 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1398 if (matching_sections
[i
].empty())
1401 gold_assert(output_section
!= NULL
);
1403 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1404 if (isp
.sort
!= SORT_WILDCARD_NONE
1405 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1406 std::stable_sort(matching_sections
[i
].begin(),
1407 matching_sections
[i
].end(),
1408 Input_section_sorter(this->filename_sort_
,
1411 for (std::vector
<Input_section_info
>::const_iterator p
=
1412 matching_sections
[i
].begin();
1413 p
!= matching_sections
[i
].end();
1416 uint64_t this_subalign
= p
->addralign();
1417 if (this_subalign
< subalign
)
1418 this_subalign
= subalign
;
1420 uint64_t address
= align_address(dot
, this_subalign
);
1422 if (address
> dot
&& !fill
->empty())
1424 section_size_type length
=
1425 convert_to_section_size_type(address
- dot
);
1426 std::string this_fill
= this->get_fill_string(fill
, length
);
1427 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1428 output_section
->add_output_section_data(posd
);
1429 layout
->new_output_section_data_from_script(posd
);
1432 output_section
->add_simple_input_section(p
->input_section(),
1436 dot
= address
+ p
->size();
1440 // An SHF_TLS/SHT_NOBITS section does not take up any
1442 if (output_section
== NULL
1443 || (output_section
->flags() & elfcpp::SHF_TLS
) == 0
1444 || output_section
->type() != elfcpp::SHT_NOBITS
)
1447 this->final_dot_value_
= *dot_value
;
1448 this->final_dot_section_
= *dot_section
;
1451 // Print for debugging.
1454 Output_section_element_input::print(FILE* f
) const
1459 fprintf(f
, "KEEP(");
1461 if (!this->filename_pattern_
.empty())
1463 bool need_close_paren
= false;
1464 switch (this->filename_sort_
)
1466 case SORT_WILDCARD_NONE
:
1468 case SORT_WILDCARD_BY_NAME
:
1469 fprintf(f
, "SORT_BY_NAME(");
1470 need_close_paren
= true;
1476 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1478 if (need_close_paren
)
1482 if (!this->input_section_patterns_
.empty()
1483 || !this->filename_exclusions_
.empty())
1487 bool need_space
= false;
1488 if (!this->filename_exclusions_
.empty())
1490 fprintf(f
, "EXCLUDE_FILE(");
1491 bool need_comma
= false;
1492 for (Filename_exclusions::const_iterator p
=
1493 this->filename_exclusions_
.begin();
1494 p
!= this->filename_exclusions_
.end();
1499 fprintf(f
, "%s", p
->first
.c_str());
1506 for (Input_section_patterns::const_iterator p
=
1507 this->input_section_patterns_
.begin();
1508 p
!= this->input_section_patterns_
.end();
1514 int close_parens
= 0;
1517 case SORT_WILDCARD_NONE
:
1519 case SORT_WILDCARD_BY_NAME
:
1520 fprintf(f
, "SORT_BY_NAME(");
1523 case SORT_WILDCARD_BY_ALIGNMENT
:
1524 fprintf(f
, "SORT_BY_ALIGNMENT(");
1527 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1528 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1531 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1532 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1539 fprintf(f
, "%s", p
->pattern
.c_str());
1541 for (int i
= 0; i
< close_parens
; ++i
)
1556 // An output section.
1558 class Output_section_definition
: public Sections_element
1561 typedef Output_section_element::Input_section_list Input_section_list
;
1563 Output_section_definition(const char* name
, size_t namelen
,
1564 const Parser_output_section_header
* header
);
1566 // Finish the output section with the information in the trailer.
1568 finish(const Parser_output_section_trailer
* trailer
);
1570 // Add a symbol to be defined.
1572 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1573 bool provide
, bool hidden
);
1575 // Add an assignment to the special dot symbol.
1577 add_dot_assignment(Expression
* value
);
1579 // Add an assertion.
1581 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1583 // Add a data item to the current output section.
1585 add_data(int size
, bool is_signed
, Expression
* val
);
1587 // Add a setting for the fill value.
1589 add_fill(Expression
* val
);
1591 // Add an input section specification.
1593 add_input_section(const Input_section_spec
* spec
, bool keep
);
1595 // Return whether the output section is relro.
1598 { return this->is_relro_
; }
1600 // Record that the output section is relro.
1603 { this->is_relro_
= true; }
1605 // Create any required output sections.
1607 create_sections(Layout
*);
1609 // Add any symbols being defined to the symbol table.
1611 add_symbols_to_table(Symbol_table
* symtab
);
1613 // Finalize symbols and check assertions.
1615 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1617 // Return the output section name to use for an input file name and
1620 output_section_name(const char* file_name
, const char* section_name
,
1621 Output_section
***, Script_sections::Section_type
*);
1623 // Initialize OSP with an output section.
1625 orphan_section_init(Orphan_section_placement
* osp
,
1626 Script_sections::Elements_iterator p
)
1627 { osp
->output_section_init(this->name_
, this->output_section_
, p
); }
1629 // Set the section address.
1631 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1632 uint64_t* dot_value
, uint64_t* load_address
);
1634 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1635 // this section is constrained, and the input sections do not match,
1636 // return the constraint, and set *POSD.
1638 check_constraint(Output_section_definition
** posd
);
1640 // See if this is the alternate output section for a constrained
1641 // output section. If it is, transfer the Output_section and return
1642 // true. Otherwise return false.
1644 alternate_constraint(Output_section_definition
*, Section_constraint
);
1646 // Get the list of segments to use for an allocated section when
1647 // using a PHDRS clause.
1649 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1651 // Look for an output section by name and return the address, the
1652 // load address, the alignment, and the size. This is used when an
1653 // expression refers to an output section which was not actually
1654 // created. This returns true if the section was found, false
1657 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1660 // Return the associated Output_section if there is one.
1662 get_output_section() const
1663 { return this->output_section_
; }
1665 // Print the contents to the FILE. This is for debugging.
1669 // Return the output section type if specified or Script_sections::ST_NONE.
1670 Script_sections::Section_type
1671 section_type() const;
1675 script_section_type_name(Script_section_type
);
1677 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1679 // The output section name.
1681 // The address. This may be NULL.
1682 Expression
* address_
;
1683 // The load address. This may be NULL.
1684 Expression
* load_address_
;
1685 // The alignment. This may be NULL.
1687 // The input section alignment. This may be NULL.
1688 Expression
* subalign_
;
1689 // The constraint, if any.
1690 Section_constraint constraint_
;
1691 // The fill value. This may be NULL.
1693 // The list of segments this section should go into. This may be
1695 String_list
* phdrs_
;
1696 // The list of elements defining the section.
1697 Output_section_elements elements_
;
1698 // The Output_section created for this definition. This will be
1699 // NULL if none was created.
1700 Output_section
* output_section_
;
1701 // The address after it has been evaluated.
1702 uint64_t evaluated_address_
;
1703 // The load address after it has been evaluated.
1704 uint64_t evaluated_load_address_
;
1705 // The alignment after it has been evaluated.
1706 uint64_t evaluated_addralign_
;
1707 // The output section is relro.
1709 // The output section type if specified.
1710 enum Script_section_type script_section_type_
;
1715 Output_section_definition::Output_section_definition(
1718 const Parser_output_section_header
* header
)
1719 : name_(name
, namelen
),
1720 address_(header
->address
),
1721 load_address_(header
->load_address
),
1722 align_(header
->align
),
1723 subalign_(header
->subalign
),
1724 constraint_(header
->constraint
),
1728 output_section_(NULL
),
1729 evaluated_address_(0),
1730 evaluated_load_address_(0),
1731 evaluated_addralign_(0),
1733 script_section_type_(header
->section_type
)
1737 // Finish an output section.
1740 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1742 this->fill_
= trailer
->fill
;
1743 this->phdrs_
= trailer
->phdrs
;
1746 // Add a symbol to be defined.
1749 Output_section_definition::add_symbol_assignment(const char* name
,
1755 Output_section_element
* p
= new Output_section_element_assignment(name
,
1760 this->elements_
.push_back(p
);
1763 // Add an assignment to the special dot symbol.
1766 Output_section_definition::add_dot_assignment(Expression
* value
)
1768 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1769 this->elements_
.push_back(p
);
1772 // Add an assertion.
1775 Output_section_definition::add_assertion(Expression
* check
,
1776 const char* message
,
1779 Output_section_element
* p
= new Output_section_element_assertion(check
,
1782 this->elements_
.push_back(p
);
1785 // Add a data item to the current output section.
1788 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1790 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1792 this->elements_
.push_back(p
);
1795 // Add a setting for the fill value.
1798 Output_section_definition::add_fill(Expression
* val
)
1800 Output_section_element
* p
= new Output_section_element_fill(val
);
1801 this->elements_
.push_back(p
);
1804 // Add an input section specification.
1807 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1810 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1811 this->elements_
.push_back(p
);
1814 // Create any required output sections. We need an output section if
1815 // there is a data statement here.
1818 Output_section_definition::create_sections(Layout
* layout
)
1820 if (this->output_section_
!= NULL
)
1822 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1823 p
!= this->elements_
.end();
1826 if ((*p
)->needs_output_section())
1828 const char* name
= this->name_
.c_str();
1829 this->output_section_
=
1830 layout
->make_output_section_for_script(name
, this->section_type());
1836 // Add any symbols being defined to the symbol table.
1839 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1841 for (Output_section_elements::iterator p
= this->elements_
.begin();
1842 p
!= this->elements_
.end();
1844 (*p
)->add_symbols_to_table(symtab
);
1847 // Finalize symbols and check assertions.
1850 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1851 const Layout
* layout
,
1852 uint64_t* dot_value
)
1854 if (this->output_section_
!= NULL
)
1855 *dot_value
= this->output_section_
->address();
1858 uint64_t address
= *dot_value
;
1859 if (this->address_
!= NULL
)
1861 Output_section
* dummy
;
1862 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1866 if (this->align_
!= NULL
)
1868 Output_section
* dummy
;
1869 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
1873 address
= align_address(address
, align
);
1875 *dot_value
= address
;
1878 Output_section
* dot_section
= this->output_section_
;
1879 for (Output_section_elements::iterator p
= this->elements_
.begin();
1880 p
!= this->elements_
.end();
1882 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1885 // Return the output section name to use for an input section name.
1888 Output_section_definition::output_section_name(
1889 const char* file_name
,
1890 const char* section_name
,
1891 Output_section
*** slot
,
1892 Script_sections::Section_type
*psection_type
)
1894 // Ask each element whether it matches NAME.
1895 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1896 p
!= this->elements_
.end();
1899 if ((*p
)->match_name(file_name
, section_name
))
1901 // We found a match for NAME, which means that it should go
1902 // into this output section.
1903 *slot
= &this->output_section_
;
1904 *psection_type
= this->section_type();
1905 return this->name_
.c_str();
1909 // We don't know about this section name.
1913 // Set the section address. Note that the OUTPUT_SECTION_ field will
1914 // be NULL if no input sections were mapped to this output section.
1915 // We still have to adjust dot and process symbol assignments.
1918 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1920 uint64_t* dot_value
,
1921 uint64_t* load_address
)
1924 uint64_t old_dot_value
= *dot_value
;
1925 uint64_t old_load_address
= *load_address
;
1927 if (this->address_
== NULL
)
1928 address
= *dot_value
;
1931 Output_section
* dummy
;
1932 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1933 *dot_value
, NULL
, &dummy
);
1937 if (this->align_
== NULL
)
1939 if (this->output_section_
== NULL
)
1942 align
= this->output_section_
->addralign();
1946 Output_section
* align_section
;
1947 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1948 NULL
, &align_section
);
1949 if (align_section
!= NULL
)
1950 gold_warning(_("alignment of section %s is not absolute"),
1951 this->name_
.c_str());
1952 if (this->output_section_
!= NULL
)
1953 this->output_section_
->set_addralign(align
);
1956 address
= align_address(address
, align
);
1958 uint64_t start_address
= address
;
1960 *dot_value
= address
;
1962 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
1963 // forced to zero, regardless of what the linker script wants.
1964 if (this->output_section_
!= NULL
1965 && ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0
1966 || this->output_section_
->is_noload()))
1967 this->output_section_
->set_address(address
);
1969 this->evaluated_address_
= address
;
1970 this->evaluated_addralign_
= align
;
1972 if (this->load_address_
== NULL
)
1973 this->evaluated_load_address_
= address
;
1976 Output_section
* dummy
;
1978 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1979 this->output_section_
, &dummy
);
1980 if (this->output_section_
!= NULL
)
1981 this->output_section_
->set_load_address(laddr
);
1982 this->evaluated_load_address_
= laddr
;
1986 if (this->subalign_
== NULL
)
1990 Output_section
* subalign_section
;
1991 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
1994 if (subalign_section
!= NULL
)
1995 gold_warning(_("subalign of section %s is not absolute"),
1996 this->name_
.c_str());
2000 if (this->fill_
!= NULL
)
2002 // FIXME: The GNU linker supports fill values of arbitrary
2004 Output_section
* fill_section
;
2005 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
2009 if (fill_section
!= NULL
)
2010 gold_warning(_("fill of section %s is not absolute"),
2011 this->name_
.c_str());
2012 unsigned char fill_buff
[4];
2013 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
2014 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
2017 Input_section_list input_sections
;
2018 if (this->output_section_
!= NULL
)
2020 // Get the list of input sections attached to this output
2021 // section. This will leave the output section with only
2022 // Output_section_data entries.
2023 address
+= this->output_section_
->get_input_sections(address
,
2026 *dot_value
= address
;
2029 Output_section
* dot_section
= this->output_section_
;
2030 for (Output_section_elements::iterator p
= this->elements_
.begin();
2031 p
!= this->elements_
.end();
2033 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
2034 subalign
, dot_value
, &dot_section
, &fill
,
2037 gold_assert(input_sections
.empty());
2039 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
2040 *load_address
= *dot_value
;
2042 *load_address
= (this->output_section_
->load_address()
2043 + (*dot_value
- start_address
));
2045 if (this->output_section_
!= NULL
)
2047 if (this->is_relro_
)
2048 this->output_section_
->set_is_relro();
2050 this->output_section_
->clear_is_relro();
2052 // If this is a NOLOAD section, keep dot and load address unchanged.
2053 if (this->output_section_
->is_noload())
2055 *dot_value
= old_dot_value
;
2056 *load_address
= old_load_address
;
2061 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2062 // this section is constrained, and the input sections do not match,
2063 // return the constraint, and set *POSD.
2066 Output_section_definition::check_constraint(Output_section_definition
** posd
)
2068 switch (this->constraint_
)
2070 case CONSTRAINT_NONE
:
2071 return CONSTRAINT_NONE
;
2073 case CONSTRAINT_ONLY_IF_RO
:
2074 if (this->output_section_
!= NULL
2075 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
2078 return CONSTRAINT_ONLY_IF_RO
;
2080 return CONSTRAINT_NONE
;
2082 case CONSTRAINT_ONLY_IF_RW
:
2083 if (this->output_section_
!= NULL
2084 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
2087 return CONSTRAINT_ONLY_IF_RW
;
2089 return CONSTRAINT_NONE
;
2091 case CONSTRAINT_SPECIAL
:
2092 if (this->output_section_
!= NULL
)
2093 gold_error(_("SPECIAL constraints are not implemented"));
2094 return CONSTRAINT_NONE
;
2101 // See if this is the alternate output section for a constrained
2102 // output section. If it is, transfer the Output_section and return
2103 // true. Otherwise return false.
2106 Output_section_definition::alternate_constraint(
2107 Output_section_definition
* posd
,
2108 Section_constraint constraint
)
2110 if (this->name_
!= posd
->name_
)
2115 case CONSTRAINT_ONLY_IF_RO
:
2116 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
2120 case CONSTRAINT_ONLY_IF_RW
:
2121 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
2129 // We have found the alternate constraint. We just need to move
2130 // over the Output_section. When constraints are used properly,
2131 // THIS should not have an output_section pointer, as all the input
2132 // sections should have matched the other definition.
2134 if (this->output_section_
!= NULL
)
2135 gold_error(_("mismatched definition for constrained sections"));
2137 this->output_section_
= posd
->output_section_
;
2138 posd
->output_section_
= NULL
;
2140 if (this->is_relro_
)
2141 this->output_section_
->set_is_relro();
2143 this->output_section_
->clear_is_relro();
2148 // Get the list of segments to use for an allocated section when using
2152 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
2155 if (this->output_section_
== NULL
)
2157 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2160 if (this->phdrs_
!= NULL
)
2161 *phdrs_list
= this->phdrs_
;
2162 return this->output_section_
;
2165 // Look for an output section by name and return the address, the load
2166 // address, the alignment, and the size. This is used when an
2167 // expression refers to an output section which was not actually
2168 // created. This returns true if the section was found, false
2172 Output_section_definition::get_output_section_info(const char* name
,
2174 uint64_t* load_address
,
2175 uint64_t* addralign
,
2176 uint64_t* size
) const
2178 if (this->name_
!= name
)
2181 if (this->output_section_
!= NULL
)
2183 *address
= this->output_section_
->address();
2184 if (this->output_section_
->has_load_address())
2185 *load_address
= this->output_section_
->load_address();
2187 *load_address
= *address
;
2188 *addralign
= this->output_section_
->addralign();
2189 *size
= this->output_section_
->current_data_size();
2193 *address
= this->evaluated_address_
;
2194 *load_address
= this->evaluated_load_address_
;
2195 *addralign
= this->evaluated_addralign_
;
2202 // Print for debugging.
2205 Output_section_definition::print(FILE* f
) const
2207 fprintf(f
, " %s ", this->name_
.c_str());
2209 if (this->address_
!= NULL
)
2211 this->address_
->print(f
);
2215 if (this->script_section_type_
!= SCRIPT_SECTION_TYPE_NONE
)
2217 this->script_section_type_name(this->script_section_type_
));
2221 if (this->load_address_
!= NULL
)
2224 this->load_address_
->print(f
);
2228 if (this->align_
!= NULL
)
2230 fprintf(f
, "ALIGN(");
2231 this->align_
->print(f
);
2235 if (this->subalign_
!= NULL
)
2237 fprintf(f
, "SUBALIGN(");
2238 this->subalign_
->print(f
);
2244 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2245 p
!= this->elements_
.end();
2251 if (this->fill_
!= NULL
)
2254 this->fill_
->print(f
);
2257 if (this->phdrs_
!= NULL
)
2259 for (String_list::const_iterator p
= this->phdrs_
->begin();
2260 p
!= this->phdrs_
->end();
2262 fprintf(f
, " :%s", p
->c_str());
2268 Script_sections::Section_type
2269 Output_section_definition::section_type() const
2271 switch (this->script_section_type_
)
2273 case SCRIPT_SECTION_TYPE_NONE
:
2274 return Script_sections::ST_NONE
;
2275 case SCRIPT_SECTION_TYPE_NOLOAD
:
2276 return Script_sections::ST_NOLOAD
;
2277 case SCRIPT_SECTION_TYPE_COPY
:
2278 case SCRIPT_SECTION_TYPE_DSECT
:
2279 case SCRIPT_SECTION_TYPE_INFO
:
2280 case SCRIPT_SECTION_TYPE_OVERLAY
:
2281 // There are not really support so we treat them as ST_NONE. The
2282 // parse should have issued errors for them already.
2283 return Script_sections::ST_NONE
;
2289 // Return the name of a script section type.
2292 Output_section_definition::script_section_type_name (
2293 Script_section_type script_section_type
)
2295 switch (script_section_type
)
2297 case SCRIPT_SECTION_TYPE_NONE
:
2299 case SCRIPT_SECTION_TYPE_NOLOAD
:
2301 case SCRIPT_SECTION_TYPE_DSECT
:
2303 case SCRIPT_SECTION_TYPE_COPY
:
2305 case SCRIPT_SECTION_TYPE_INFO
:
2307 case SCRIPT_SECTION_TYPE_OVERLAY
:
2314 // An output section created to hold orphaned input sections. These
2315 // do not actually appear in linker scripts. However, for convenience
2316 // when setting the output section addresses, we put a marker to these
2317 // sections in the appropriate place in the list of SECTIONS elements.
2319 class Orphan_output_section
: public Sections_element
2322 Orphan_output_section(Output_section
* os
)
2326 // Return whether the orphan output section is relro. We can just
2327 // check the output section because we always set the flag, if
2328 // needed, just after we create the Orphan_output_section.
2331 { return this->os_
->is_relro(); }
2333 // Initialize OSP with an output section. This should have been
2336 orphan_section_init(Orphan_section_placement
*,
2337 Script_sections::Elements_iterator
)
2338 { gold_unreachable(); }
2340 // Set section addresses.
2342 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
2344 // Get the list of segments to use for an allocated section when
2345 // using a PHDRS clause.
2347 allocate_to_segment(String_list
**, bool*);
2349 // Return the associated Output_section.
2351 get_output_section() const
2352 { return this->os_
; }
2354 // Print for debugging.
2356 print(FILE* f
) const
2358 fprintf(f
, " marker for orphaned output section %s\n",
2363 Output_section
* os_
;
2366 // Set section addresses.
2369 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2370 uint64_t* dot_value
,
2371 uint64_t* load_address
)
2373 typedef std::list
<Output_section::Simple_input_section
> Input_section_list
;
2375 bool have_load_address
= *load_address
!= *dot_value
;
2377 uint64_t address
= *dot_value
;
2378 address
= align_address(address
, this->os_
->addralign());
2380 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2382 this->os_
->set_address(address
);
2383 if (have_load_address
)
2384 this->os_
->set_load_address(align_address(*load_address
,
2385 this->os_
->addralign()));
2388 Input_section_list input_sections
;
2389 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2391 for (Input_section_list::iterator p
= input_sections
.begin();
2392 p
!= input_sections
.end();
2398 // We know we are single-threaded, so it is OK to lock the
2401 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2402 Task_lock_obj
<Object
> tl(task
, p
->relobj());
2403 addralign
= p
->relobj()->section_addralign(p
->shndx());
2404 if (p
->is_relaxed_input_section())
2405 // We use current data size because relxed section sizes may not
2406 // have finalized yet.
2407 size
= p
->relaxed_input_section()->current_data_size();
2409 size
= p
->relobj()->section_size(p
->shndx());
2412 address
= align_address(address
, addralign
);
2413 this->os_
->add_simple_input_section(*p
, size
, addralign
);
2417 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2418 if (this->os_
== NULL
2419 || (this->os_
->flags() & elfcpp::SHF_TLS
) == 0
2420 || this->os_
->type() != elfcpp::SHT_NOBITS
)
2422 if (!have_load_address
)
2423 *load_address
= address
;
2425 *load_address
+= address
- *dot_value
;
2427 *dot_value
= address
;
2431 // Get the list of segments to use for an allocated section when using
2432 // a PHDRS clause. If this is an allocated section, return the
2433 // Output_section. We don't change the list of segments.
2436 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2438 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2444 // Class Phdrs_element. A program header from a PHDRS clause.
2449 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2450 bool includes_filehdr
, bool includes_phdrs
,
2451 bool is_flags_valid
, unsigned int flags
,
2452 Expression
* load_address
)
2453 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2454 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2455 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2459 // Return the name of this segment.
2462 { return this->name_
; }
2464 // Return the type of the segment.
2467 { return this->type_
; }
2469 // Whether to include the file header.
2471 includes_filehdr() const
2472 { return this->includes_filehdr_
; }
2474 // Whether to include the program headers.
2476 includes_phdrs() const
2477 { return this->includes_phdrs_
; }
2479 // Return whether there is a load address.
2481 has_load_address() const
2482 { return this->load_address_
!= NULL
; }
2484 // Evaluate the load address expression if there is one.
2486 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2488 if (this->load_address_
!= NULL
)
2489 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2493 // Return the load address.
2495 load_address() const
2497 gold_assert(this->load_address_
!= NULL
);
2498 return this->load_address_value_
;
2501 // Create the segment.
2503 create_segment(Layout
* layout
)
2505 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2506 return this->segment_
;
2509 // Return the segment.
2512 { return this->segment_
; }
2514 // Release the segment.
2517 { this->segment_
= NULL
; }
2519 // Set the segment flags if appropriate.
2521 set_flags_if_valid()
2523 if (this->is_flags_valid_
)
2524 this->segment_
->set_flags(this->flags_
);
2527 // Print for debugging.
2532 // The name used in the script.
2534 // The type of the segment (PT_LOAD, etc.).
2536 // Whether this segment includes the file header.
2537 bool includes_filehdr_
;
2538 // Whether this segment includes the section headers.
2539 bool includes_phdrs_
;
2540 // Whether the flags were explicitly specified.
2541 bool is_flags_valid_
;
2542 // The flags for this segment (PF_R, etc.) if specified.
2543 unsigned int flags_
;
2544 // The expression for the load address for this segment. This may
2546 Expression
* load_address_
;
2547 // The actual load address from evaluating the expression.
2548 uint64_t load_address_value_
;
2549 // The segment itself.
2550 Output_segment
* segment_
;
2553 // Print for debugging.
2556 Phdrs_element::print(FILE* f
) const
2558 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2559 if (this->includes_filehdr_
)
2560 fprintf(f
, " FILEHDR");
2561 if (this->includes_phdrs_
)
2562 fprintf(f
, " PHDRS");
2563 if (this->is_flags_valid_
)
2564 fprintf(f
, " FLAGS(%u)", this->flags_
);
2565 if (this->load_address_
!= NULL
)
2568 this->load_address_
->print(f
);
2574 // Class Script_sections.
2576 Script_sections::Script_sections()
2577 : saw_sections_clause_(false),
2578 in_sections_clause_(false),
2579 sections_elements_(NULL
),
2580 output_section_(NULL
),
2581 phdrs_elements_(NULL
),
2582 orphan_section_placement_(NULL
),
2583 data_segment_align_start_(),
2584 saw_data_segment_align_(false),
2585 saw_relro_end_(false),
2586 saw_segment_start_expression_(false)
2590 // Start a SECTIONS clause.
2593 Script_sections::start_sections()
2595 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2596 this->saw_sections_clause_
= true;
2597 this->in_sections_clause_
= true;
2598 if (this->sections_elements_
== NULL
)
2599 this->sections_elements_
= new Sections_elements
;
2602 // Finish a SECTIONS clause.
2605 Script_sections::finish_sections()
2607 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2608 this->in_sections_clause_
= false;
2611 // Add a symbol to be defined.
2614 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2615 Expression
* val
, bool provide
,
2618 if (this->output_section_
!= NULL
)
2619 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2623 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2626 this->sections_elements_
->push_back(p
);
2630 // Add an assignment to the special dot symbol.
2633 Script_sections::add_dot_assignment(Expression
* val
)
2635 if (this->output_section_
!= NULL
)
2636 this->output_section_
->add_dot_assignment(val
);
2639 // The GNU linker permits assignments to . to appears outside of
2640 // a SECTIONS clause, and treats it as appearing inside, so
2641 // sections_elements_ may be NULL here.
2642 if (this->sections_elements_
== NULL
)
2644 this->sections_elements_
= new Sections_elements
;
2645 this->saw_sections_clause_
= true;
2648 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2649 this->sections_elements_
->push_back(p
);
2653 // Add an assertion.
2656 Script_sections::add_assertion(Expression
* check
, const char* message
,
2659 if (this->output_section_
!= NULL
)
2660 this->output_section_
->add_assertion(check
, message
, messagelen
);
2663 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2665 this->sections_elements_
->push_back(p
);
2669 // Start processing entries for an output section.
2672 Script_sections::start_output_section(
2675 const Parser_output_section_header
*header
)
2677 Output_section_definition
* posd
= new Output_section_definition(name
,
2680 this->sections_elements_
->push_back(posd
);
2681 gold_assert(this->output_section_
== NULL
);
2682 this->output_section_
= posd
;
2685 // Stop processing entries for an output section.
2688 Script_sections::finish_output_section(
2689 const Parser_output_section_trailer
* trailer
)
2691 gold_assert(this->output_section_
!= NULL
);
2692 this->output_section_
->finish(trailer
);
2693 this->output_section_
= NULL
;
2696 // Add a data item to the current output section.
2699 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2701 gold_assert(this->output_section_
!= NULL
);
2702 this->output_section_
->add_data(size
, is_signed
, val
);
2705 // Add a fill value setting to the current output section.
2708 Script_sections::add_fill(Expression
* val
)
2710 gold_assert(this->output_section_
!= NULL
);
2711 this->output_section_
->add_fill(val
);
2714 // Add an input section specification to the current output section.
2717 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2719 gold_assert(this->output_section_
!= NULL
);
2720 this->output_section_
->add_input_section(spec
, keep
);
2723 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
2724 // subsequent output sections may be relro.
2727 Script_sections::data_segment_align()
2729 if (this->saw_data_segment_align_
)
2730 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2731 gold_assert(!this->sections_elements_
->empty());
2732 Sections_elements::iterator p
= this->sections_elements_
->end();
2734 this->data_segment_align_start_
= p
;
2735 this->saw_data_segment_align_
= true;
2738 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
2739 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
2742 Script_sections::data_segment_relro_end()
2744 if (this->saw_relro_end_
)
2745 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2746 "in a linker script"));
2747 this->saw_relro_end_
= true;
2749 if (!this->saw_data_segment_align_
)
2750 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2753 Sections_elements::iterator p
= this->data_segment_align_start_
;
2754 for (++p
; p
!= this->sections_elements_
->end(); ++p
)
2755 (*p
)->set_is_relro();
2759 // Create any required sections.
2762 Script_sections::create_sections(Layout
* layout
)
2764 if (!this->saw_sections_clause_
)
2766 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2767 p
!= this->sections_elements_
->end();
2769 (*p
)->create_sections(layout
);
2772 // Add any symbols we are defining to the symbol table.
2775 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2777 if (!this->saw_sections_clause_
)
2779 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2780 p
!= this->sections_elements_
->end();
2782 (*p
)->add_symbols_to_table(symtab
);
2785 // Finalize symbols and check assertions.
2788 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2790 if (!this->saw_sections_clause_
)
2792 uint64_t dot_value
= 0;
2793 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2794 p
!= this->sections_elements_
->end();
2796 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2799 // Return the name of the output section to use for an input file name
2800 // and section name.
2803 Script_sections::output_section_name(
2804 const char* file_name
,
2805 const char* section_name
,
2806 Output_section
*** output_section_slot
,
2807 Script_sections::Section_type
*psection_type
)
2809 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2810 p
!= this->sections_elements_
->end();
2813 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2814 output_section_slot
,
2819 // The special name /DISCARD/ means that the input section
2820 // should be discarded.
2821 if (strcmp(ret
, "/DISCARD/") == 0)
2823 *output_section_slot
= NULL
;
2824 *psection_type
= Script_sections::ST_NONE
;
2831 // If we couldn't find a mapping for the name, the output section
2832 // gets the name of the input section.
2834 *output_section_slot
= NULL
;
2835 *psection_type
= Script_sections::ST_NONE
;
2837 return section_name
;
2840 // Place a marker for an orphan output section into the SECTIONS
2844 Script_sections::place_orphan(Output_section
* os
)
2846 Orphan_section_placement
* osp
= this->orphan_section_placement_
;
2849 // Initialize the Orphan_section_placement structure.
2850 osp
= new Orphan_section_placement();
2851 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2852 p
!= this->sections_elements_
->end();
2854 (*p
)->orphan_section_init(osp
, p
);
2855 gold_assert(!this->sections_elements_
->empty());
2856 Sections_elements::iterator last
= this->sections_elements_
->end();
2858 osp
->last_init(last
);
2859 this->orphan_section_placement_
= osp
;
2862 Orphan_output_section
* orphan
= new Orphan_output_section(os
);
2864 // Look for where to put ORPHAN.
2865 Sections_elements::iterator
* where
;
2866 if (osp
->find_place(os
, &where
))
2868 if ((**where
)->is_relro())
2871 os
->clear_is_relro();
2873 // We want to insert ORPHAN after *WHERE, and then update *WHERE
2874 // so that the next one goes after this one.
2875 Sections_elements::iterator p
= *where
;
2876 gold_assert(p
!= this->sections_elements_
->end());
2878 *where
= this->sections_elements_
->insert(p
, orphan
);
2882 os
->clear_is_relro();
2883 // We don't have a place to put this orphan section. Put it,
2884 // and all other sections like it, at the end, but before the
2885 // sections which always come at the end.
2886 Sections_elements::iterator last
= osp
->last_place();
2887 *where
= this->sections_elements_
->insert(last
, orphan
);
2891 // Set the addresses of all the output sections. Walk through all the
2892 // elements, tracking the dot symbol. Apply assignments which set
2893 // absolute symbol values, in case they are used when setting dot.
2894 // Fill in data statement values. As we find output sections, set the
2895 // address, set the address of all associated input sections, and
2896 // update dot. Return the segment which should hold the file header
2897 // and segment headers, if any.
2900 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2902 gold_assert(this->saw_sections_clause_
);
2904 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2905 // for our representation.
2906 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2907 p
!= this->sections_elements_
->end();
2910 Output_section_definition
* posd
;
2911 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2912 if (failed_constraint
!= CONSTRAINT_NONE
)
2914 Sections_elements::iterator q
;
2915 for (q
= this->sections_elements_
->begin();
2916 q
!= this->sections_elements_
->end();
2921 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2926 if (q
== this->sections_elements_
->end())
2927 gold_error(_("no matching section constraint"));
2931 // Force the alignment of the first TLS section to be the maximum
2932 // alignment of all TLS sections.
2933 Output_section
* first_tls
= NULL
;
2934 uint64_t tls_align
= 0;
2935 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2936 p
!= this->sections_elements_
->end();
2939 Output_section
*os
= (*p
)->get_output_section();
2940 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
2942 if (first_tls
== NULL
)
2944 if (os
->addralign() > tls_align
)
2945 tls_align
= os
->addralign();
2948 if (first_tls
!= NULL
)
2949 first_tls
->set_addralign(tls_align
);
2951 // For a relocatable link, we implicitly set dot to zero.
2952 uint64_t dot_value
= 0;
2953 uint64_t load_address
= 0;
2955 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
2956 // to set section addresses. If the script has any SEGMENT_START
2957 // expression, we do not set the section addresses.
2958 bool use_tsection_options
=
2959 (!this->saw_segment_start_expression_
2960 && (parameters
->options().user_set_Ttext()
2961 || parameters
->options().user_set_Tdata()
2962 || parameters
->options().user_set_Tbss()));
2964 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2965 p
!= this->sections_elements_
->end();
2968 Output_section
* os
= (*p
)->get_output_section();
2970 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
2971 // the special sections by names and doing dot assignments.
2972 if (use_tsection_options
2974 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
2976 uint64_t new_dot_value
= dot_value
;
2978 if (parameters
->options().user_set_Ttext()
2979 && strcmp(os
->name(), ".text") == 0)
2980 new_dot_value
= parameters
->options().Ttext();
2981 else if (parameters
->options().user_set_Tdata()
2982 && strcmp(os
->name(), ".data") == 0)
2983 new_dot_value
= parameters
->options().Tdata();
2984 else if (parameters
->options().user_set_Tbss()
2985 && strcmp(os
->name(), ".bss") == 0)
2986 new_dot_value
= parameters
->options().Tbss();
2988 // Update dot and load address if necessary.
2989 if (new_dot_value
< dot_value
)
2990 gold_error(_("dot may not move backward"));
2991 else if (new_dot_value
!= dot_value
)
2993 dot_value
= new_dot_value
;
2994 load_address
= new_dot_value
;
2998 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
3001 if (this->phdrs_elements_
!= NULL
)
3003 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3004 p
!= this->phdrs_elements_
->end();
3006 (*p
)->eval_load_address(symtab
, layout
);
3009 return this->create_segments(layout
);
3012 // Sort the sections in order to put them into segments.
3014 class Sort_output_sections
3018 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
3022 Sort_output_sections::operator()(const Output_section
* os1
,
3023 const Output_section
* os2
) const
3025 // Sort first by the load address.
3026 uint64_t lma1
= (os1
->has_load_address()
3027 ? os1
->load_address()
3029 uint64_t lma2
= (os2
->has_load_address()
3030 ? os2
->load_address()
3035 // Then sort by the virtual address.
3036 if (os1
->address() != os2
->address())
3037 return os1
->address() < os2
->address();
3039 // Sort TLS sections to the end.
3040 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
3041 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
3045 // Sort PROGBITS before NOBITS.
3046 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
3048 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
3051 // Sort non-NOLOAD before NOLOAD.
3052 if (os1
->is_noload() && !os2
->is_noload())
3054 if (!os1
->is_noload() && os2
->is_noload())
3057 // Otherwise we don't care.
3061 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3062 // We treat a section with the SHF_TLS flag set as taking up space
3063 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3064 // space for them in the file.
3067 Script_sections::is_bss_section(const Output_section
* os
)
3069 return (os
->type() == elfcpp::SHT_NOBITS
3070 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
3073 // Return the size taken by the file header and the program headers.
3076 Script_sections::total_header_size(Layout
* layout
) const
3078 size_t segment_count
= layout
->segment_count();
3079 size_t file_header_size
;
3080 size_t segment_headers_size
;
3081 if (parameters
->target().get_size() == 32)
3083 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
3084 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
3086 else if (parameters
->target().get_size() == 64)
3088 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
3089 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
3094 return file_header_size
+ segment_headers_size
;
3097 // Return the amount we have to subtract from the LMA to accomodate
3098 // headers of the given size. The complication is that the file
3099 // header have to be at the start of a page, as otherwise it will not
3100 // be at the start of the file.
3103 Script_sections::header_size_adjustment(uint64_t lma
,
3104 size_t sizeof_headers
) const
3106 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3107 uint64_t hdr_lma
= lma
- sizeof_headers
;
3108 hdr_lma
&= ~(abi_pagesize
- 1);
3109 return lma
- hdr_lma
;
3112 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3113 // the segment which should hold the file header and segment headers,
3117 Script_sections::create_segments(Layout
* layout
)
3119 gold_assert(this->saw_sections_clause_
);
3121 if (parameters
->options().relocatable())
3124 if (this->saw_phdrs_clause())
3125 return create_segments_from_phdrs_clause(layout
);
3127 Layout::Section_list sections
;
3128 layout
->get_allocated_sections(§ions
);
3130 // Sort the sections by address.
3131 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
3133 this->create_note_and_tls_segments(layout
, §ions
);
3135 // Walk through the sections adding them to PT_LOAD segments.
3136 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3137 Output_segment
* first_seg
= NULL
;
3138 Output_segment
* current_seg
= NULL
;
3139 bool is_current_seg_readonly
= true;
3140 Layout::Section_list::iterator plast
= sections
.end();
3141 uint64_t last_vma
= 0;
3142 uint64_t last_lma
= 0;
3143 uint64_t last_size
= 0;
3144 for (Layout::Section_list::iterator p
= sections
.begin();
3145 p
!= sections
.end();
3148 const uint64_t vma
= (*p
)->address();
3149 const uint64_t lma
= ((*p
)->has_load_address()
3150 ? (*p
)->load_address()
3152 const uint64_t size
= (*p
)->current_data_size();
3154 bool need_new_segment
;
3155 if (current_seg
== NULL
)
3156 need_new_segment
= true;
3157 else if (lma
- vma
!= last_lma
- last_vma
)
3159 // This section has a different LMA relationship than the
3160 // last one; we need a new segment.
3161 need_new_segment
= true;
3163 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
3164 < align_address(lma
, abi_pagesize
))
3166 // Putting this section in the segment would require
3168 need_new_segment
= true;
3170 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
3172 // A non-BSS section can not follow a BSS section in the
3174 need_new_segment
= true;
3176 else if (is_current_seg_readonly
3177 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
3178 && !parameters
->options().omagic())
3180 // Don't put a writable section in the same segment as a
3181 // non-writable section.
3182 need_new_segment
= true;
3186 // Otherwise, reuse the existing segment.
3187 need_new_segment
= false;
3190 elfcpp::Elf_Word seg_flags
=
3191 Layout::section_flags_to_segment((*p
)->flags());
3193 if (need_new_segment
)
3195 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3197 current_seg
->set_addresses(vma
, lma
);
3198 if (first_seg
== NULL
)
3199 first_seg
= current_seg
;
3200 is_current_seg_readonly
= true;
3203 current_seg
->add_output_section(*p
, seg_flags
, false);
3205 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
3206 is_current_seg_readonly
= false;
3214 // An ELF program should work even if the program headers are not in
3215 // a PT_LOAD segment. However, it appears that the Linux kernel
3216 // does not set the AT_PHDR auxiliary entry in that case. It sets
3217 // the load address to p_vaddr - p_offset of the first PT_LOAD
3218 // segment. It then sets AT_PHDR to the load address plus the
3219 // offset to the program headers, e_phoff in the file header. This
3220 // fails when the program headers appear in the file before the
3221 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3222 // segment to hold the file header and the program headers. This is
3223 // effectively what the GNU linker does, and it is slightly more
3224 // efficient in any case. We try to use the first PT_LOAD segment
3225 // if we can, otherwise we make a new one.
3227 if (first_seg
== NULL
)
3230 // -n or -N mean that the program is not demand paged and there is
3231 // no need to put the program headers in a PT_LOAD segment.
3232 if (parameters
->options().nmagic() || parameters
->options().omagic())
3235 size_t sizeof_headers
= this->total_header_size(layout
);
3237 uint64_t vma
= first_seg
->vaddr();
3238 uint64_t lma
= first_seg
->paddr();
3240 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
3242 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
3244 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3248 // If there is no room to squeeze in the headers, then punt. The
3249 // resulting executable probably won't run on GNU/Linux, but we
3250 // trust that the user knows what they are doing.
3251 if (lma
< subtract
|| vma
< subtract
)
3254 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3256 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3261 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3262 // segment if there are any SHT_TLS sections.
3265 Script_sections::create_note_and_tls_segments(
3267 const Layout::Section_list
* sections
)
3269 gold_assert(!this->saw_phdrs_clause());
3271 bool saw_tls
= false;
3272 for (Layout::Section_list::const_iterator p
= sections
->begin();
3273 p
!= sections
->end();
3276 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3278 elfcpp::Elf_Word seg_flags
=
3279 Layout::section_flags_to_segment((*p
)->flags());
3280 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
3282 oseg
->add_output_section(*p
, seg_flags
, false);
3284 // Incorporate any subsequent SHT_NOTE sections, in the
3285 // hopes that the script is sensible.
3286 Layout::Section_list::const_iterator pnext
= p
+ 1;
3287 while (pnext
!= sections
->end()
3288 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
3290 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3291 oseg
->add_output_section(*pnext
, seg_flags
, false);
3297 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3300 gold_error(_("TLS sections are not adjacent"));
3302 elfcpp::Elf_Word seg_flags
=
3303 Layout::section_flags_to_segment((*p
)->flags());
3304 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
3306 oseg
->add_output_section(*p
, seg_flags
, false);
3308 Layout::Section_list::const_iterator pnext
= p
+ 1;
3309 while (pnext
!= sections
->end()
3310 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
3312 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3313 oseg
->add_output_section(*pnext
, seg_flags
, false);
3323 // Add a program header. The PHDRS clause is syntactically distinct
3324 // from the SECTIONS clause, but we implement it with the SECTIONS
3325 // support because PHDRS is useless if there is no SECTIONS clause.
3328 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
3329 bool includes_filehdr
, bool includes_phdrs
,
3330 bool is_flags_valid
, unsigned int flags
,
3331 Expression
* load_address
)
3333 if (this->phdrs_elements_
== NULL
)
3334 this->phdrs_elements_
= new Phdrs_elements();
3335 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
3338 is_flags_valid
, flags
,
3342 // Return the number of segments we expect to create based on the
3343 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3346 Script_sections::expected_segment_count(const Layout
* layout
) const
3348 if (this->saw_phdrs_clause())
3349 return this->phdrs_elements_
->size();
3351 Layout::Section_list sections
;
3352 layout
->get_allocated_sections(§ions
);
3354 // We assume that we will need two PT_LOAD segments.
3357 bool saw_note
= false;
3358 bool saw_tls
= false;
3359 for (Layout::Section_list::const_iterator p
= sections
.begin();
3360 p
!= sections
.end();
3363 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3365 // Assume that all note sections will fit into a single
3373 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3375 // There can only be one PT_TLS segment.
3387 // Create the segments from a PHDRS clause. Return the segment which
3388 // should hold the file header and program headers, if any.
3391 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
3393 this->attach_sections_using_phdrs_clause(layout
);
3394 return this->set_phdrs_clause_addresses(layout
);
3397 // Create the segments from the PHDRS clause, and put the output
3398 // sections in them.
3401 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
3403 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
3404 Name_to_segment name_to_segment
;
3405 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3406 p
!= this->phdrs_elements_
->end();
3408 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
3410 // Walk through the output sections and attach them to segments.
3411 // Output sections in the script which do not list segments are
3412 // attached to the same set of segments as the immediately preceding
3415 String_list
* phdr_names
= NULL
;
3416 bool load_segments_only
= false;
3417 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3418 p
!= this->sections_elements_
->end();
3422 String_list
* old_phdr_names
= phdr_names
;
3423 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
3427 if (phdr_names
== NULL
)
3429 gold_error(_("allocated section not in any segment"));
3433 // We see a list of segments names. Disable PT_LOAD segment only
3435 if (old_phdr_names
!= phdr_names
)
3436 load_segments_only
= false;
3438 // If this is an orphan section--one that was not explicitly
3439 // mentioned in the linker script--then it should not inherit
3440 // any segment type other than PT_LOAD. Otherwise, e.g., the
3441 // PT_INTERP segment will pick up following orphan sections,
3442 // which does not make sense. If this is not an orphan section,
3443 // we trust the linker script.
3446 // Enable PT_LOAD segments only filtering until we see another
3447 // list of segment names.
3448 load_segments_only
= true;
3451 bool in_load_segment
= false;
3452 for (String_list::const_iterator q
= phdr_names
->begin();
3453 q
!= phdr_names
->end();
3456 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3457 if (r
== name_to_segment
.end())
3458 gold_error(_("no segment %s"), q
->c_str());
3461 if (load_segments_only
3462 && r
->second
->type() != elfcpp::PT_LOAD
)
3465 elfcpp::Elf_Word seg_flags
=
3466 Layout::section_flags_to_segment(os
->flags());
3467 r
->second
->add_output_section(os
, seg_flags
, false);
3469 if (r
->second
->type() == elfcpp::PT_LOAD
)
3471 if (in_load_segment
)
3472 gold_error(_("section in two PT_LOAD segments"));
3473 in_load_segment
= true;
3478 if (!in_load_segment
)
3479 gold_error(_("allocated section not in any PT_LOAD segment"));
3483 // Set the addresses for segments created from a PHDRS clause. Return
3484 // the segment which should hold the file header and program headers,
3488 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
3490 Output_segment
* load_seg
= NULL
;
3491 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3492 p
!= this->phdrs_elements_
->end();
3495 // Note that we have to set the flags after adding the output
3496 // sections to the segment, as adding an output segment can
3497 // change the flags.
3498 (*p
)->set_flags_if_valid();
3500 Output_segment
* oseg
= (*p
)->segment();
3502 if (oseg
->type() != elfcpp::PT_LOAD
)
3504 // The addresses of non-PT_LOAD segments are set from the
3505 // PT_LOAD segments.
3506 if ((*p
)->has_load_address())
3507 gold_error(_("may only specify load address for PT_LOAD segment"));
3511 // The output sections should have addresses from the SECTIONS
3512 // clause. The addresses don't have to be in order, so find the
3513 // one with the lowest load address. Use that to set the
3514 // address of the segment.
3516 Output_section
* osec
= oseg
->section_with_lowest_load_address();
3519 oseg
->set_addresses(0, 0);
3523 uint64_t vma
= osec
->address();
3524 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3526 // Override the load address of the section with the load
3527 // address specified for the segment.
3528 if ((*p
)->has_load_address())
3530 if (osec
->has_load_address())
3531 gold_warning(_("PHDRS load address overrides "
3532 "section %s load address"),
3535 lma
= (*p
)->load_address();
3538 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3539 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3541 // We could support this if we wanted to.
3542 gold_error(_("using only one of FILEHDR and PHDRS is "
3543 "not currently supported"));
3547 size_t sizeof_headers
= this->total_header_size(layout
);
3548 uint64_t subtract
= this->header_size_adjustment(lma
,
3550 if (lma
>= subtract
&& vma
>= subtract
)
3557 gold_error(_("sections loaded on first page without room "
3558 "for file and program headers "
3559 "are not supported"));
3562 if (load_seg
!= NULL
)
3563 gold_error(_("using FILEHDR and PHDRS on more than one "
3564 "PT_LOAD segment is not currently supported"));
3568 oseg
->set_addresses(vma
, lma
);
3574 // Add the file header and segment headers to non-load segments
3575 // specified in the PHDRS clause.
3578 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3579 Output_data
* segment_headers
)
3581 gold_assert(this->saw_phdrs_clause());
3582 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3583 p
!= this->phdrs_elements_
->end();
3586 if ((*p
)->type() != elfcpp::PT_LOAD
)
3588 if ((*p
)->includes_phdrs())
3589 (*p
)->segment()->add_initial_output_data(segment_headers
);
3590 if ((*p
)->includes_filehdr())
3591 (*p
)->segment()->add_initial_output_data(file_header
);
3596 // Look for an output section by name and return the address, the load
3597 // address, the alignment, and the size. This is used when an
3598 // expression refers to an output section which was not actually
3599 // created. This returns true if the section was found, false
3603 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3604 uint64_t* load_address
,
3605 uint64_t* addralign
,
3606 uint64_t* size
) const
3608 if (!this->saw_sections_clause_
)
3610 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3611 p
!= this->sections_elements_
->end();
3613 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3619 // Release all Output_segments. This remove all pointers to all
3623 Script_sections::release_segments()
3625 if (this->saw_phdrs_clause())
3627 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3628 p
!= this->phdrs_elements_
->end();
3630 (*p
)->release_segment();
3634 // Print the SECTIONS clause to F for debugging.
3637 Script_sections::print(FILE* f
) const
3639 if (!this->saw_sections_clause_
)
3642 fprintf(f
, "SECTIONS {\n");
3644 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3645 p
!= this->sections_elements_
->end();
3651 if (this->phdrs_elements_
!= NULL
)
3653 fprintf(f
, "PHDRS {\n");
3654 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3655 p
!= this->phdrs_elements_
->end();
3662 } // End namespace gold.