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*,
376 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
377 // this section is constrained, and the input sections do not match,
378 // return the constraint, and set *POSD.
379 virtual Section_constraint
380 check_constraint(Output_section_definition
**)
381 { return CONSTRAINT_NONE
; }
383 // See if this is the alternate output section for a constrained
384 // output section. If it is, transfer the Output_section and return
385 // true. Otherwise return false.
387 alternate_constraint(Output_section_definition
*, Section_constraint
)
390 // Get the list of segments to use for an allocated section when
391 // using a PHDRS clause. If this is an allocated section, return
392 // the Output_section, and set *PHDRS_LIST (the first parameter) to
393 // the list of PHDRS to which it should be attached. If the PHDRS
394 // were not specified, don't change *PHDRS_LIST. When not returning
395 // NULL, set *ORPHAN (the second parameter) according to whether
396 // this is an orphan section--one that is not mentioned in the
398 virtual Output_section
*
399 allocate_to_segment(String_list
**, bool*)
402 // Look for an output section by name and return the address, the
403 // load address, the alignment, and the size. This is used when an
404 // expression refers to an output section which was not actually
405 // created. This returns true if the section was found, false
406 // otherwise. The only real definition is for
407 // Output_section_definition.
409 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
413 // Return the associated Output_section if there is one.
414 virtual Output_section
*
415 get_output_section() const
418 // Print the element for debugging purposes.
420 print(FILE* f
) const = 0;
423 // An assignment in a SECTIONS clause outside of an output section.
425 class Sections_element_assignment
: public Sections_element
428 Sections_element_assignment(const char* name
, size_t namelen
,
429 Expression
* val
, bool provide
, bool hidden
)
430 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
433 // Add the symbol to the symbol table.
435 add_symbols_to_table(Symbol_table
* symtab
)
436 { this->assignment_
.add_to_table(symtab
); }
438 // Finalize the symbol.
440 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
443 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
446 // Set the section address. There is no section here, but if the
447 // value is absolute, we set the symbol. This permits us to use
448 // absolute symbols when setting dot.
450 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
451 uint64_t* dot_value
, uint64_t*, uint64_t*)
453 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
456 // Print for debugging.
461 this->assignment_
.print(f
);
465 Symbol_assignment assignment_
;
468 // An assignment to the dot symbol in a SECTIONS clause outside of an
471 class Sections_element_dot_assignment
: public Sections_element
474 Sections_element_dot_assignment(Expression
* val
)
478 // Finalize the symbol.
480 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
483 // We ignore the section of the result because outside of an
484 // output section definition the dot symbol is always considered
486 Output_section
* dummy
;
487 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
491 // Update the dot symbol while setting section addresses.
493 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
494 uint64_t* dot_value
, uint64_t* dot_alignment
,
495 uint64_t* load_address
)
497 Output_section
* dummy
;
498 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
499 NULL
, &dummy
, dot_alignment
);
500 *load_address
= *dot_value
;
503 // Print for debugging.
508 this->val_
->print(f
);
516 // An assertion in a SECTIONS clause outside of an output section.
518 class Sections_element_assertion
: public Sections_element
521 Sections_element_assertion(Expression
* check
, const char* message
,
523 : assertion_(check
, message
, messagelen
)
526 // Check the assertion.
528 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
529 { this->assertion_
.check(symtab
, layout
); }
531 // Print for debugging.
536 this->assertion_
.print(f
);
540 Script_assertion assertion_
;
543 // An element in an output section in a SECTIONS clause.
545 class Output_section_element
548 // A list of input sections.
549 typedef std::list
<Output_section::Simple_input_section
> Input_section_list
;
551 Output_section_element()
554 virtual ~Output_section_element()
557 // Return whether this element requires an output section to exist.
559 needs_output_section() const
562 // Add any symbol being defined to the symbol table.
564 add_symbols_to_table(Symbol_table
*)
567 // Finalize symbols and check assertions.
569 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
572 // Return whether this element matches FILE_NAME and SECTION_NAME.
573 // The only real implementation is in Output_section_element_input.
575 match_name(const char*, const char*) const
578 // Set section addresses. This includes applying assignments if the
579 // the expression is an absolute value.
581 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
582 uint64_t*, uint64_t*, Output_section
**, std::string
*,
586 // Print the element for debugging purposes.
588 print(FILE* f
) const = 0;
591 // Return a fill string that is LENGTH bytes long, filling it with
594 get_fill_string(const std::string
* fill
, section_size_type length
) const;
598 Output_section_element::get_fill_string(const std::string
* fill
,
599 section_size_type length
) const
601 std::string this_fill
;
602 this_fill
.reserve(length
);
603 while (this_fill
.length() + fill
->length() <= length
)
605 if (this_fill
.length() < length
)
606 this_fill
.append(*fill
, 0, length
- this_fill
.length());
610 // A symbol assignment in an output section.
612 class Output_section_element_assignment
: public Output_section_element
615 Output_section_element_assignment(const char* name
, size_t namelen
,
616 Expression
* val
, bool provide
,
618 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
621 // Add the symbol to the symbol table.
623 add_symbols_to_table(Symbol_table
* symtab
)
624 { this->assignment_
.add_to_table(symtab
); }
626 // Finalize the symbol.
628 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
629 uint64_t* dot_value
, Output_section
** dot_section
)
631 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
635 // Set the section address. There is no section here, but if the
636 // value is absolute, we set the symbol. This permits us to use
637 // absolute symbols when setting dot.
639 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
640 uint64_t, uint64_t* dot_value
, uint64_t*,
641 Output_section
**, std::string
*, Input_section_list
*)
643 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
646 // Print for debugging.
651 this->assignment_
.print(f
);
655 Symbol_assignment assignment_
;
658 // An assignment to the dot symbol in an output section.
660 class Output_section_element_dot_assignment
: public Output_section_element
663 Output_section_element_dot_assignment(Expression
* val
)
667 // Finalize the symbol.
669 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
670 uint64_t* dot_value
, Output_section
** dot_section
)
672 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
673 *dot_section
, dot_section
, NULL
);
676 // Update the dot symbol while setting section addresses.
678 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
679 uint64_t, uint64_t* dot_value
, uint64_t*,
680 Output_section
**, std::string
*, Input_section_list
*);
682 // Print for debugging.
687 this->val_
->print(f
);
695 // Update the dot symbol while setting section addresses.
698 Output_section_element_dot_assignment::set_section_addresses(
699 Symbol_table
* symtab
,
701 Output_section
* output_section
,
704 uint64_t* dot_alignment
,
705 Output_section
** dot_section
,
709 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
710 *dot_value
, *dot_section
,
711 dot_section
, dot_alignment
);
712 if (next_dot
< *dot_value
)
713 gold_error(_("dot may not move backward"));
714 if (next_dot
> *dot_value
&& output_section
!= NULL
)
716 section_size_type length
= convert_to_section_size_type(next_dot
718 Output_section_data
* posd
;
720 posd
= new Output_data_zero_fill(length
, 0);
723 std::string this_fill
= this->get_fill_string(fill
, length
);
724 posd
= new Output_data_const(this_fill
, 0);
726 output_section
->add_output_section_data(posd
);
727 layout
->new_output_section_data_from_script(posd
);
729 *dot_value
= next_dot
;
732 // An assertion in an output section.
734 class Output_section_element_assertion
: public Output_section_element
737 Output_section_element_assertion(Expression
* check
, const char* message
,
739 : assertion_(check
, message
, messagelen
)
746 this->assertion_
.print(f
);
750 Script_assertion assertion_
;
753 // We use a special instance of Output_section_data to handle BYTE,
754 // SHORT, etc. This permits forward references to symbols in the
757 class Output_data_expression
: public Output_section_data
760 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
761 const Symbol_table
* symtab
, const Layout
* layout
,
762 uint64_t dot_value
, Output_section
* dot_section
)
763 : Output_section_data(size
, 0, true),
764 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
765 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
769 // Write the data to the output file.
771 do_write(Output_file
*);
773 // Write the data to a buffer.
775 do_write_to_buffer(unsigned char*);
777 // Write to a map file.
779 do_print_to_mapfile(Mapfile
* mapfile
) const
780 { mapfile
->print_output_data(this, _("** expression")); }
783 template<bool big_endian
>
785 endian_write_to_buffer(uint64_t, unsigned char*);
789 const Symbol_table
* symtab_
;
790 const Layout
* layout_
;
792 Output_section
* dot_section_
;
795 // Write the data element to the output file.
798 Output_data_expression::do_write(Output_file
* of
)
800 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
801 this->write_to_buffer(view
);
802 of
->write_output_view(this->offset(), this->data_size(), view
);
805 // Write the data element to a buffer.
808 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
810 Output_section
* dummy
;
811 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
812 true, this->dot_value_
,
813 this->dot_section_
, &dummy
, NULL
);
815 if (parameters
->target().is_big_endian())
816 this->endian_write_to_buffer
<true>(val
, buf
);
818 this->endian_write_to_buffer
<false>(val
, buf
);
821 template<bool big_endian
>
823 Output_data_expression::endian_write_to_buffer(uint64_t val
,
826 switch (this->data_size())
829 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
832 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
835 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
838 if (parameters
->target().get_size() == 32)
841 if (this->is_signed_
&& (val
& 0x80000000) != 0)
842 val
|= 0xffffffff00000000LL
;
844 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
851 // A data item in an output section.
853 class Output_section_element_data
: public Output_section_element
856 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
857 : size_(size
), is_signed_(is_signed
), val_(val
)
860 // If there is a data item, then we must create an output section.
862 needs_output_section() const
865 // Finalize symbols--we just need to update dot.
867 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
869 { *dot_value
+= this->size_
; }
871 // Store the value in the section.
873 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
874 uint64_t* dot_value
, uint64_t*, Output_section
**,
875 std::string
*, Input_section_list
*);
877 // Print for debugging.
882 // The size in bytes.
884 // Whether the value is signed.
890 // Store the value in the section.
893 Output_section_element_data::set_section_addresses(
894 Symbol_table
* symtab
,
900 Output_section
** dot_section
,
904 gold_assert(os
!= NULL
);
905 Output_data_expression
* expression
=
906 new Output_data_expression(this->size_
, this->is_signed_
, this->val_
,
907 symtab
, layout
, *dot_value
, *dot_section
);
908 os
->add_output_section_data(expression
);
909 layout
->new_output_section_data_from_script(expression
);
910 *dot_value
+= this->size_
;
913 // Print for debugging.
916 Output_section_element_data::print(FILE* f
) const
931 if (this->is_signed_
)
939 fprintf(f
, " %s(", s
);
940 this->val_
->print(f
);
944 // A fill value setting in an output section.
946 class Output_section_element_fill
: public Output_section_element
949 Output_section_element_fill(Expression
* val
)
953 // Update the fill value while setting section addresses.
955 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
956 uint64_t, uint64_t* dot_value
, uint64_t*,
957 Output_section
** dot_section
,
958 std::string
* fill
, Input_section_list
*)
960 Output_section
* fill_section
;
961 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
962 *dot_value
, *dot_section
,
963 &fill_section
, NULL
);
964 if (fill_section
!= NULL
)
965 gold_warning(_("fill value is not absolute"));
966 // FIXME: The GNU linker supports fill values of arbitrary length.
967 unsigned char fill_buff
[4];
968 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
969 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
972 // Print for debugging.
976 fprintf(f
, " FILL(");
977 this->val_
->print(f
);
982 // The new fill value.
986 // Return whether STRING contains a wildcard character. This is used
987 // to speed up matching.
990 is_wildcard_string(const std::string
& s
)
992 return strpbrk(s
.c_str(), "?*[") != NULL
;
995 // An input section specification in an output section
997 class Output_section_element_input
: public Output_section_element
1000 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
1002 // Finalize symbols--just update the value of the dot symbol.
1004 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1005 Output_section
** dot_section
)
1007 *dot_value
= this->final_dot_value_
;
1008 *dot_section
= this->final_dot_section_
;
1011 // See whether we match FILE_NAME and SECTION_NAME as an input
1014 match_name(const char* file_name
, const char* section_name
) const;
1016 // Set the section address.
1018 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1019 uint64_t subalign
, uint64_t* dot_value
, uint64_t*,
1020 Output_section
**, std::string
* fill
,
1021 Input_section_list
*);
1023 // Print for debugging.
1025 print(FILE* f
) const;
1028 // An input section pattern.
1029 struct Input_section_pattern
1031 std::string pattern
;
1032 bool pattern_is_wildcard
;
1035 Input_section_pattern(const char* patterna
, size_t patternlena
,
1036 Sort_wildcard sorta
)
1037 : pattern(patterna
, patternlena
),
1038 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
1043 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
1045 // Filename_exclusions is a pair of filename pattern and a bool
1046 // indicating whether the filename is a wildcard.
1047 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
1049 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1050 // indicates whether this is a wildcard pattern.
1052 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
1054 return (is_wildcard_pattern
1055 ? fnmatch(pattern
, string
, 0) == 0
1056 : strcmp(string
, pattern
) == 0);
1059 // See if we match a file name.
1061 match_file_name(const char* file_name
) const;
1063 // The file name pattern. If this is the empty string, we match all
1065 std::string filename_pattern_
;
1066 // Whether the file name pattern is a wildcard.
1067 bool filename_is_wildcard_
;
1068 // How the file names should be sorted. This may only be
1069 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1070 Sort_wildcard filename_sort_
;
1071 // The list of file names to exclude.
1072 Filename_exclusions filename_exclusions_
;
1073 // The list of input section patterns.
1074 Input_section_patterns input_section_patterns_
;
1075 // Whether to keep this section when garbage collecting.
1077 // The value of dot after including all matching sections.
1078 uint64_t final_dot_value_
;
1079 // The section where dot is defined after including all matching
1081 Output_section
* final_dot_section_
;
1084 // Construct Output_section_element_input. The parser records strings
1085 // as pointers into a copy of the script file, which will go away when
1086 // parsing is complete. We make sure they are in std::string objects.
1088 Output_section_element_input::Output_section_element_input(
1089 const Input_section_spec
* spec
,
1091 : filename_pattern_(),
1092 filename_is_wildcard_(false),
1093 filename_sort_(spec
->file
.sort
),
1094 filename_exclusions_(),
1095 input_section_patterns_(),
1097 final_dot_value_(0),
1098 final_dot_section_(NULL
)
1100 // The filename pattern "*" is common, and matches all files. Turn
1101 // it into the empty string.
1102 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
1103 this->filename_pattern_
.assign(spec
->file
.name
.value
,
1104 spec
->file
.name
.length
);
1105 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
1107 if (spec
->input_sections
.exclude
!= NULL
)
1109 for (String_list::const_iterator p
=
1110 spec
->input_sections
.exclude
->begin();
1111 p
!= spec
->input_sections
.exclude
->end();
1114 bool is_wildcard
= is_wildcard_string(*p
);
1115 this->filename_exclusions_
.push_back(std::make_pair(*p
,
1120 if (spec
->input_sections
.sections
!= NULL
)
1122 Input_section_patterns
& isp(this->input_section_patterns_
);
1123 for (String_sort_list::const_iterator p
=
1124 spec
->input_sections
.sections
->begin();
1125 p
!= spec
->input_sections
.sections
->end();
1127 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
1132 // See whether we match FILE_NAME.
1135 Output_section_element_input::match_file_name(const char* file_name
) const
1137 if (!this->filename_pattern_
.empty())
1139 // If we were called with no filename, we refuse to match a
1140 // pattern which requires a file name.
1141 if (file_name
== NULL
)
1144 if (!match(file_name
, this->filename_pattern_
.c_str(),
1145 this->filename_is_wildcard_
))
1149 if (file_name
!= NULL
)
1151 // Now we have to see whether FILE_NAME matches one of the
1152 // exclusion patterns, if any.
1153 for (Filename_exclusions::const_iterator p
=
1154 this->filename_exclusions_
.begin();
1155 p
!= this->filename_exclusions_
.end();
1158 if (match(file_name
, p
->first
.c_str(), p
->second
))
1166 // See whether we match FILE_NAME and SECTION_NAME.
1169 Output_section_element_input::match_name(const char* file_name
,
1170 const char* section_name
) const
1172 if (!this->match_file_name(file_name
))
1175 // If there are no section name patterns, then we match.
1176 if (this->input_section_patterns_
.empty())
1179 // See whether we match the section name patterns.
1180 for (Input_section_patterns::const_iterator p
=
1181 this->input_section_patterns_
.begin();
1182 p
!= this->input_section_patterns_
.end();
1185 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
1189 // We didn't match any section names, so we didn't match.
1193 // Information we use to sort the input sections.
1195 class Input_section_info
1198 Input_section_info(const Output_section::Simple_input_section
& input_section
)
1199 : input_section_(input_section
), section_name_(),
1200 size_(0), addralign_(1)
1203 // Return the simple input section.
1204 const Output_section::Simple_input_section
&
1205 input_section() const
1206 { return this->input_section_
; }
1208 // Return the object.
1211 { return this->input_section_
.relobj(); }
1213 // Return the section index.
1216 { return this->input_section_
.shndx(); }
1218 // Return the section name.
1220 section_name() const
1221 { return this->section_name_
; }
1223 // Set the section name.
1225 set_section_name(const std::string name
)
1226 { this->section_name_
= name
; }
1228 // Return the section size.
1231 { return this->size_
; }
1233 // Set the section size.
1235 set_size(uint64_t size
)
1236 { this->size_
= size
; }
1238 // Return the address alignment.
1241 { return this->addralign_
; }
1243 // Set the address alignment.
1245 set_addralign(uint64_t addralign
)
1246 { this->addralign_
= addralign
; }
1249 // Input section, can be a relaxed section.
1250 Output_section::Simple_input_section input_section_
;
1251 // Name of the section.
1252 std::string section_name_
;
1255 // Address alignment.
1256 uint64_t addralign_
;
1259 // A class to sort the input sections.
1261 class Input_section_sorter
1264 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
1265 : filename_sort_(filename_sort
), section_sort_(section_sort
)
1269 operator()(const Input_section_info
&, const Input_section_info
&) const;
1272 Sort_wildcard filename_sort_
;
1273 Sort_wildcard section_sort_
;
1277 Input_section_sorter::operator()(const Input_section_info
& isi1
,
1278 const Input_section_info
& isi2
) const
1280 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
1281 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1282 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1283 && isi1
.addralign() == isi2
.addralign()))
1285 if (isi1
.section_name() != isi2
.section_name())
1286 return isi1
.section_name() < isi2
.section_name();
1288 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
1289 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1290 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
1292 if (isi1
.addralign() != isi2
.addralign())
1293 return isi1
.addralign() < isi2
.addralign();
1295 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
1297 if (isi1
.relobj()->name() != isi2
.relobj()->name())
1298 return (isi1
.relobj()->name() < isi2
.relobj()->name());
1301 // Otherwise we leave them in the same order.
1305 // Set the section address. Look in INPUT_SECTIONS for sections which
1306 // match this spec, sort them as specified, and add them to the output
1310 Output_section_element_input::set_section_addresses(
1313 Output_section
* output_section
,
1315 uint64_t* dot_value
,
1317 Output_section
** dot_section
,
1319 Input_section_list
* input_sections
)
1321 // We build a list of sections which match each
1322 // Input_section_pattern.
1324 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
1325 size_t input_pattern_count
= this->input_section_patterns_
.size();
1326 if (input_pattern_count
== 0)
1327 input_pattern_count
= 1;
1328 Matching_sections
matching_sections(input_pattern_count
);
1330 // Look through the list of sections for this output section. Add
1331 // each one which matches to one of the elements of
1332 // MATCHING_SECTIONS.
1334 Input_section_list::iterator p
= input_sections
->begin();
1335 while (p
!= input_sections
->end())
1337 Relobj
* relobj
= p
->relobj();
1338 unsigned int shndx
= p
->shndx();
1339 Input_section_info
isi(*p
);
1341 // Calling section_name and section_addralign is not very
1344 // Lock the object so that we can get information about the
1345 // section. This is OK since we know we are single-threaded
1348 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1349 Task_lock_obj
<Object
> tl(task
, relobj
);
1351 isi
.set_section_name(relobj
->section_name(shndx
));
1352 if (p
->is_relaxed_input_section())
1354 // We use current data size because relxed section sizes may not
1355 // have finalized yet.
1356 isi
.set_size(p
->relaxed_input_section()->current_data_size());
1357 isi
.set_addralign(p
->relaxed_input_section()->addralign());
1361 isi
.set_size(relobj
->section_size(shndx
));
1362 isi
.set_addralign(relobj
->section_addralign(shndx
));
1366 if (!this->match_file_name(relobj
->name().c_str()))
1368 else if (this->input_section_patterns_
.empty())
1370 matching_sections
[0].push_back(isi
);
1371 p
= input_sections
->erase(p
);
1376 for (i
= 0; i
< input_pattern_count
; ++i
)
1378 const Input_section_pattern
&
1379 isp(this->input_section_patterns_
[i
]);
1380 if (match(isi
.section_name().c_str(), isp
.pattern
.c_str(),
1381 isp
.pattern_is_wildcard
))
1385 if (i
>= this->input_section_patterns_
.size())
1389 matching_sections
[i
].push_back(isi
);
1390 p
= input_sections
->erase(p
);
1395 // Look through MATCHING_SECTIONS. Sort each one as specified,
1396 // using a stable sort so that we get the default order when
1397 // sections are otherwise equal. Add each input section to the
1400 uint64_t dot
= *dot_value
;
1401 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1403 if (matching_sections
[i
].empty())
1406 gold_assert(output_section
!= NULL
);
1408 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1409 if (isp
.sort
!= SORT_WILDCARD_NONE
1410 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1411 std::stable_sort(matching_sections
[i
].begin(),
1412 matching_sections
[i
].end(),
1413 Input_section_sorter(this->filename_sort_
,
1416 for (std::vector
<Input_section_info
>::const_iterator p
=
1417 matching_sections
[i
].begin();
1418 p
!= matching_sections
[i
].end();
1421 uint64_t this_subalign
= p
->addralign();
1422 if (this_subalign
< subalign
)
1423 this_subalign
= subalign
;
1425 uint64_t address
= align_address(dot
, this_subalign
);
1427 if (address
> dot
&& !fill
->empty())
1429 section_size_type length
=
1430 convert_to_section_size_type(address
- dot
);
1431 std::string this_fill
= this->get_fill_string(fill
, length
);
1432 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1433 output_section
->add_output_section_data(posd
);
1434 layout
->new_output_section_data_from_script(posd
);
1437 output_section
->add_simple_input_section(p
->input_section(),
1441 dot
= address
+ p
->size();
1445 // An SHF_TLS/SHT_NOBITS section does not take up any
1447 if (output_section
== NULL
1448 || (output_section
->flags() & elfcpp::SHF_TLS
) == 0
1449 || output_section
->type() != elfcpp::SHT_NOBITS
)
1452 this->final_dot_value_
= *dot_value
;
1453 this->final_dot_section_
= *dot_section
;
1456 // Print for debugging.
1459 Output_section_element_input::print(FILE* f
) const
1464 fprintf(f
, "KEEP(");
1466 if (!this->filename_pattern_
.empty())
1468 bool need_close_paren
= false;
1469 switch (this->filename_sort_
)
1471 case SORT_WILDCARD_NONE
:
1473 case SORT_WILDCARD_BY_NAME
:
1474 fprintf(f
, "SORT_BY_NAME(");
1475 need_close_paren
= true;
1481 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1483 if (need_close_paren
)
1487 if (!this->input_section_patterns_
.empty()
1488 || !this->filename_exclusions_
.empty())
1492 bool need_space
= false;
1493 if (!this->filename_exclusions_
.empty())
1495 fprintf(f
, "EXCLUDE_FILE(");
1496 bool need_comma
= false;
1497 for (Filename_exclusions::const_iterator p
=
1498 this->filename_exclusions_
.begin();
1499 p
!= this->filename_exclusions_
.end();
1504 fprintf(f
, "%s", p
->first
.c_str());
1511 for (Input_section_patterns::const_iterator p
=
1512 this->input_section_patterns_
.begin();
1513 p
!= this->input_section_patterns_
.end();
1519 int close_parens
= 0;
1522 case SORT_WILDCARD_NONE
:
1524 case SORT_WILDCARD_BY_NAME
:
1525 fprintf(f
, "SORT_BY_NAME(");
1528 case SORT_WILDCARD_BY_ALIGNMENT
:
1529 fprintf(f
, "SORT_BY_ALIGNMENT(");
1532 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1533 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1536 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1537 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1544 fprintf(f
, "%s", p
->pattern
.c_str());
1546 for (int i
= 0; i
< close_parens
; ++i
)
1561 // An output section.
1563 class Output_section_definition
: public Sections_element
1566 typedef Output_section_element::Input_section_list Input_section_list
;
1568 Output_section_definition(const char* name
, size_t namelen
,
1569 const Parser_output_section_header
* header
);
1571 // Finish the output section with the information in the trailer.
1573 finish(const Parser_output_section_trailer
* trailer
);
1575 // Add a symbol to be defined.
1577 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1578 bool provide
, bool hidden
);
1580 // Add an assignment to the special dot symbol.
1582 add_dot_assignment(Expression
* value
);
1584 // Add an assertion.
1586 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1588 // Add a data item to the current output section.
1590 add_data(int size
, bool is_signed
, Expression
* val
);
1592 // Add a setting for the fill value.
1594 add_fill(Expression
* val
);
1596 // Add an input section specification.
1598 add_input_section(const Input_section_spec
* spec
, bool keep
);
1600 // Return whether the output section is relro.
1603 { return this->is_relro_
; }
1605 // Record that the output section is relro.
1608 { this->is_relro_
= true; }
1610 // Create any required output sections.
1612 create_sections(Layout
*);
1614 // Add any symbols being defined to the symbol table.
1616 add_symbols_to_table(Symbol_table
* symtab
);
1618 // Finalize symbols and check assertions.
1620 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1622 // Return the output section name to use for an input file name and
1625 output_section_name(const char* file_name
, const char* section_name
,
1626 Output_section
***, Script_sections::Section_type
*);
1628 // Initialize OSP with an output section.
1630 orphan_section_init(Orphan_section_placement
* osp
,
1631 Script_sections::Elements_iterator p
)
1632 { osp
->output_section_init(this->name_
, this->output_section_
, p
); }
1634 // Set the section address.
1636 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1637 uint64_t* dot_value
, uint64_t*,
1638 uint64_t* load_address
);
1640 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1641 // this section is constrained, and the input sections do not match,
1642 // return the constraint, and set *POSD.
1644 check_constraint(Output_section_definition
** posd
);
1646 // See if this is the alternate output section for a constrained
1647 // output section. If it is, transfer the Output_section and return
1648 // true. Otherwise return false.
1650 alternate_constraint(Output_section_definition
*, Section_constraint
);
1652 // Get the list of segments to use for an allocated section when
1653 // using a PHDRS clause.
1655 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1657 // Look for an output section by name and return the address, the
1658 // load address, the alignment, and the size. This is used when an
1659 // expression refers to an output section which was not actually
1660 // created. This returns true if the section was found, false
1663 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1666 // Return the associated Output_section if there is one.
1668 get_output_section() const
1669 { return this->output_section_
; }
1671 // Print the contents to the FILE. This is for debugging.
1675 // Return the output section type if specified or Script_sections::ST_NONE.
1676 Script_sections::Section_type
1677 section_type() const;
1681 script_section_type_name(Script_section_type
);
1683 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1685 // The output section name.
1687 // The address. This may be NULL.
1688 Expression
* address_
;
1689 // The load address. This may be NULL.
1690 Expression
* load_address_
;
1691 // The alignment. This may be NULL.
1693 // The input section alignment. This may be NULL.
1694 Expression
* subalign_
;
1695 // The constraint, if any.
1696 Section_constraint constraint_
;
1697 // The fill value. This may be NULL.
1699 // The list of segments this section should go into. This may be
1701 String_list
* phdrs_
;
1702 // The list of elements defining the section.
1703 Output_section_elements elements_
;
1704 // The Output_section created for this definition. This will be
1705 // NULL if none was created.
1706 Output_section
* output_section_
;
1707 // The address after it has been evaluated.
1708 uint64_t evaluated_address_
;
1709 // The load address after it has been evaluated.
1710 uint64_t evaluated_load_address_
;
1711 // The alignment after it has been evaluated.
1712 uint64_t evaluated_addralign_
;
1713 // The output section is relro.
1715 // The output section type if specified.
1716 enum Script_section_type script_section_type_
;
1721 Output_section_definition::Output_section_definition(
1724 const Parser_output_section_header
* header
)
1725 : name_(name
, namelen
),
1726 address_(header
->address
),
1727 load_address_(header
->load_address
),
1728 align_(header
->align
),
1729 subalign_(header
->subalign
),
1730 constraint_(header
->constraint
),
1734 output_section_(NULL
),
1735 evaluated_address_(0),
1736 evaluated_load_address_(0),
1737 evaluated_addralign_(0),
1739 script_section_type_(header
->section_type
)
1743 // Finish an output section.
1746 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1748 this->fill_
= trailer
->fill
;
1749 this->phdrs_
= trailer
->phdrs
;
1752 // Add a symbol to be defined.
1755 Output_section_definition::add_symbol_assignment(const char* name
,
1761 Output_section_element
* p
= new Output_section_element_assignment(name
,
1766 this->elements_
.push_back(p
);
1769 // Add an assignment to the special dot symbol.
1772 Output_section_definition::add_dot_assignment(Expression
* value
)
1774 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1775 this->elements_
.push_back(p
);
1778 // Add an assertion.
1781 Output_section_definition::add_assertion(Expression
* check
,
1782 const char* message
,
1785 Output_section_element
* p
= new Output_section_element_assertion(check
,
1788 this->elements_
.push_back(p
);
1791 // Add a data item to the current output section.
1794 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1796 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1798 this->elements_
.push_back(p
);
1801 // Add a setting for the fill value.
1804 Output_section_definition::add_fill(Expression
* val
)
1806 Output_section_element
* p
= new Output_section_element_fill(val
);
1807 this->elements_
.push_back(p
);
1810 // Add an input section specification.
1813 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1816 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1817 this->elements_
.push_back(p
);
1820 // Create any required output sections. We need an output section if
1821 // there is a data statement here.
1824 Output_section_definition::create_sections(Layout
* layout
)
1826 if (this->output_section_
!= NULL
)
1828 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1829 p
!= this->elements_
.end();
1832 if ((*p
)->needs_output_section())
1834 const char* name
= this->name_
.c_str();
1835 this->output_section_
=
1836 layout
->make_output_section_for_script(name
, this->section_type());
1842 // Add any symbols being defined to the symbol table.
1845 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1847 for (Output_section_elements::iterator p
= this->elements_
.begin();
1848 p
!= this->elements_
.end();
1850 (*p
)->add_symbols_to_table(symtab
);
1853 // Finalize symbols and check assertions.
1856 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1857 const Layout
* layout
,
1858 uint64_t* dot_value
)
1860 if (this->output_section_
!= NULL
)
1861 *dot_value
= this->output_section_
->address();
1864 uint64_t address
= *dot_value
;
1865 if (this->address_
!= NULL
)
1867 Output_section
* dummy
;
1868 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1872 if (this->align_
!= NULL
)
1874 Output_section
* dummy
;
1875 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
1879 address
= align_address(address
, align
);
1881 *dot_value
= address
;
1884 Output_section
* dot_section
= this->output_section_
;
1885 for (Output_section_elements::iterator p
= this->elements_
.begin();
1886 p
!= this->elements_
.end();
1888 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1891 // Return the output section name to use for an input section name.
1894 Output_section_definition::output_section_name(
1895 const char* file_name
,
1896 const char* section_name
,
1897 Output_section
*** slot
,
1898 Script_sections::Section_type
*psection_type
)
1900 // Ask each element whether it matches NAME.
1901 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1902 p
!= this->elements_
.end();
1905 if ((*p
)->match_name(file_name
, section_name
))
1907 // We found a match for NAME, which means that it should go
1908 // into this output section.
1909 *slot
= &this->output_section_
;
1910 *psection_type
= this->section_type();
1911 return this->name_
.c_str();
1915 // We don't know about this section name.
1919 // Set the section address. Note that the OUTPUT_SECTION_ field will
1920 // be NULL if no input sections were mapped to this output section.
1921 // We still have to adjust dot and process symbol assignments.
1924 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1926 uint64_t* dot_value
,
1927 uint64_t* dot_alignment
,
1928 uint64_t* load_address
)
1931 uint64_t old_dot_value
= *dot_value
;
1932 uint64_t old_load_address
= *load_address
;
1934 if (this->address_
== NULL
)
1935 address
= *dot_value
;
1938 Output_section
* dummy
;
1939 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1940 *dot_value
, NULL
, &dummy
,
1945 if (this->align_
== NULL
)
1947 if (this->output_section_
== NULL
)
1950 align
= this->output_section_
->addralign();
1954 Output_section
* align_section
;
1955 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1956 NULL
, &align_section
, NULL
);
1957 if (align_section
!= NULL
)
1958 gold_warning(_("alignment of section %s is not absolute"),
1959 this->name_
.c_str());
1960 if (this->output_section_
!= NULL
)
1961 this->output_section_
->set_addralign(align
);
1964 address
= align_address(address
, align
);
1966 uint64_t start_address
= address
;
1968 *dot_value
= address
;
1970 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
1971 // forced to zero, regardless of what the linker script wants.
1972 if (this->output_section_
!= NULL
1973 && ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0
1974 || this->output_section_
->is_noload()))
1975 this->output_section_
->set_address(address
);
1977 this->evaluated_address_
= address
;
1978 this->evaluated_addralign_
= align
;
1980 if (this->load_address_
== NULL
)
1981 this->evaluated_load_address_
= address
;
1984 Output_section
* dummy
;
1986 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1987 this->output_section_
, &dummy
,
1989 if (this->output_section_
!= NULL
)
1990 this->output_section_
->set_load_address(laddr
);
1991 this->evaluated_load_address_
= laddr
;
1995 if (this->subalign_
== NULL
)
1999 Output_section
* subalign_section
;
2000 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
2002 &subalign_section
, NULL
);
2003 if (subalign_section
!= NULL
)
2004 gold_warning(_("subalign of section %s is not absolute"),
2005 this->name_
.c_str());
2009 if (this->fill_
!= NULL
)
2011 // FIXME: The GNU linker supports fill values of arbitrary
2013 Output_section
* fill_section
;
2014 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
2016 NULL
, &fill_section
,
2018 if (fill_section
!= NULL
)
2019 gold_warning(_("fill of section %s is not absolute"),
2020 this->name_
.c_str());
2021 unsigned char fill_buff
[4];
2022 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
2023 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
2026 Input_section_list input_sections
;
2027 if (this->output_section_
!= NULL
)
2029 // Get the list of input sections attached to this output
2030 // section. This will leave the output section with only
2031 // Output_section_data entries.
2032 address
+= this->output_section_
->get_input_sections(address
,
2035 *dot_value
= address
;
2038 Output_section
* dot_section
= this->output_section_
;
2039 for (Output_section_elements::iterator p
= this->elements_
.begin();
2040 p
!= this->elements_
.end();
2042 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
2043 subalign
, dot_value
, dot_alignment
,
2044 &dot_section
, &fill
, &input_sections
);
2046 gold_assert(input_sections
.empty());
2048 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
2049 *load_address
= *dot_value
;
2051 *load_address
= (this->output_section_
->load_address()
2052 + (*dot_value
- start_address
));
2054 if (this->output_section_
!= NULL
)
2056 if (this->is_relro_
)
2057 this->output_section_
->set_is_relro();
2059 this->output_section_
->clear_is_relro();
2061 // If this is a NOLOAD section, keep dot and load address unchanged.
2062 if (this->output_section_
->is_noload())
2064 *dot_value
= old_dot_value
;
2065 *load_address
= old_load_address
;
2070 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2071 // this section is constrained, and the input sections do not match,
2072 // return the constraint, and set *POSD.
2075 Output_section_definition::check_constraint(Output_section_definition
** posd
)
2077 switch (this->constraint_
)
2079 case CONSTRAINT_NONE
:
2080 return CONSTRAINT_NONE
;
2082 case CONSTRAINT_ONLY_IF_RO
:
2083 if (this->output_section_
!= NULL
2084 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
2087 return CONSTRAINT_ONLY_IF_RO
;
2089 return CONSTRAINT_NONE
;
2091 case CONSTRAINT_ONLY_IF_RW
:
2092 if (this->output_section_
!= NULL
2093 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
2096 return CONSTRAINT_ONLY_IF_RW
;
2098 return CONSTRAINT_NONE
;
2100 case CONSTRAINT_SPECIAL
:
2101 if (this->output_section_
!= NULL
)
2102 gold_error(_("SPECIAL constraints are not implemented"));
2103 return CONSTRAINT_NONE
;
2110 // See if this is the alternate output section for a constrained
2111 // output section. If it is, transfer the Output_section and return
2112 // true. Otherwise return false.
2115 Output_section_definition::alternate_constraint(
2116 Output_section_definition
* posd
,
2117 Section_constraint constraint
)
2119 if (this->name_
!= posd
->name_
)
2124 case CONSTRAINT_ONLY_IF_RO
:
2125 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
2129 case CONSTRAINT_ONLY_IF_RW
:
2130 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
2138 // We have found the alternate constraint. We just need to move
2139 // over the Output_section. When constraints are used properly,
2140 // THIS should not have an output_section pointer, as all the input
2141 // sections should have matched the other definition.
2143 if (this->output_section_
!= NULL
)
2144 gold_error(_("mismatched definition for constrained sections"));
2146 this->output_section_
= posd
->output_section_
;
2147 posd
->output_section_
= NULL
;
2149 if (this->is_relro_
)
2150 this->output_section_
->set_is_relro();
2152 this->output_section_
->clear_is_relro();
2157 // Get the list of segments to use for an allocated section when using
2161 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
2164 if (this->output_section_
== NULL
)
2166 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2169 if (this->phdrs_
!= NULL
)
2170 *phdrs_list
= this->phdrs_
;
2171 return this->output_section_
;
2174 // Look for an output section by name and return the address, the load
2175 // address, the alignment, and the size. This is used when an
2176 // expression refers to an output section which was not actually
2177 // created. This returns true if the section was found, false
2181 Output_section_definition::get_output_section_info(const char* name
,
2183 uint64_t* load_address
,
2184 uint64_t* addralign
,
2185 uint64_t* size
) const
2187 if (this->name_
!= name
)
2190 if (this->output_section_
!= NULL
)
2192 *address
= this->output_section_
->address();
2193 if (this->output_section_
->has_load_address())
2194 *load_address
= this->output_section_
->load_address();
2196 *load_address
= *address
;
2197 *addralign
= this->output_section_
->addralign();
2198 *size
= this->output_section_
->current_data_size();
2202 *address
= this->evaluated_address_
;
2203 *load_address
= this->evaluated_load_address_
;
2204 *addralign
= this->evaluated_addralign_
;
2211 // Print for debugging.
2214 Output_section_definition::print(FILE* f
) const
2216 fprintf(f
, " %s ", this->name_
.c_str());
2218 if (this->address_
!= NULL
)
2220 this->address_
->print(f
);
2224 if (this->script_section_type_
!= SCRIPT_SECTION_TYPE_NONE
)
2226 this->script_section_type_name(this->script_section_type_
));
2230 if (this->load_address_
!= NULL
)
2233 this->load_address_
->print(f
);
2237 if (this->align_
!= NULL
)
2239 fprintf(f
, "ALIGN(");
2240 this->align_
->print(f
);
2244 if (this->subalign_
!= NULL
)
2246 fprintf(f
, "SUBALIGN(");
2247 this->subalign_
->print(f
);
2253 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2254 p
!= this->elements_
.end();
2260 if (this->fill_
!= NULL
)
2263 this->fill_
->print(f
);
2266 if (this->phdrs_
!= NULL
)
2268 for (String_list::const_iterator p
= this->phdrs_
->begin();
2269 p
!= this->phdrs_
->end();
2271 fprintf(f
, " :%s", p
->c_str());
2277 Script_sections::Section_type
2278 Output_section_definition::section_type() const
2280 switch (this->script_section_type_
)
2282 case SCRIPT_SECTION_TYPE_NONE
:
2283 return Script_sections::ST_NONE
;
2284 case SCRIPT_SECTION_TYPE_NOLOAD
:
2285 return Script_sections::ST_NOLOAD
;
2286 case SCRIPT_SECTION_TYPE_COPY
:
2287 case SCRIPT_SECTION_TYPE_DSECT
:
2288 case SCRIPT_SECTION_TYPE_INFO
:
2289 case SCRIPT_SECTION_TYPE_OVERLAY
:
2290 // There are not really support so we treat them as ST_NONE. The
2291 // parse should have issued errors for them already.
2292 return Script_sections::ST_NONE
;
2298 // Return the name of a script section type.
2301 Output_section_definition::script_section_type_name (
2302 Script_section_type script_section_type
)
2304 switch (script_section_type
)
2306 case SCRIPT_SECTION_TYPE_NONE
:
2308 case SCRIPT_SECTION_TYPE_NOLOAD
:
2310 case SCRIPT_SECTION_TYPE_DSECT
:
2312 case SCRIPT_SECTION_TYPE_COPY
:
2314 case SCRIPT_SECTION_TYPE_INFO
:
2316 case SCRIPT_SECTION_TYPE_OVERLAY
:
2323 // An output section created to hold orphaned input sections. These
2324 // do not actually appear in linker scripts. However, for convenience
2325 // when setting the output section addresses, we put a marker to these
2326 // sections in the appropriate place in the list of SECTIONS elements.
2328 class Orphan_output_section
: public Sections_element
2331 Orphan_output_section(Output_section
* os
)
2335 // Return whether the orphan output section is relro. We can just
2336 // check the output section because we always set the flag, if
2337 // needed, just after we create the Orphan_output_section.
2340 { return this->os_
->is_relro(); }
2342 // Initialize OSP with an output section. This should have been
2345 orphan_section_init(Orphan_section_placement
*,
2346 Script_sections::Elements_iterator
)
2347 { gold_unreachable(); }
2349 // Set section addresses.
2351 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*,
2354 // Get the list of segments to use for an allocated section when
2355 // using a PHDRS clause.
2357 allocate_to_segment(String_list
**, bool*);
2359 // Return the associated Output_section.
2361 get_output_section() const
2362 { return this->os_
; }
2364 // Print for debugging.
2366 print(FILE* f
) const
2368 fprintf(f
, " marker for orphaned output section %s\n",
2373 Output_section
* os_
;
2376 // Set section addresses.
2379 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2380 uint64_t* dot_value
,
2382 uint64_t* load_address
)
2384 typedef std::list
<Output_section::Simple_input_section
> Input_section_list
;
2386 bool have_load_address
= *load_address
!= *dot_value
;
2388 uint64_t address
= *dot_value
;
2389 address
= align_address(address
, this->os_
->addralign());
2391 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2393 this->os_
->set_address(address
);
2394 if (have_load_address
)
2395 this->os_
->set_load_address(align_address(*load_address
,
2396 this->os_
->addralign()));
2399 Input_section_list input_sections
;
2400 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2402 for (Input_section_list::iterator p
= input_sections
.begin();
2403 p
!= input_sections
.end();
2409 // We know we are single-threaded, so it is OK to lock the
2412 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2413 Task_lock_obj
<Object
> tl(task
, p
->relobj());
2414 addralign
= p
->relobj()->section_addralign(p
->shndx());
2415 if (p
->is_relaxed_input_section())
2416 // We use current data size because relxed section sizes may not
2417 // have finalized yet.
2418 size
= p
->relaxed_input_section()->current_data_size();
2420 size
= p
->relobj()->section_size(p
->shndx());
2423 address
= align_address(address
, addralign
);
2424 this->os_
->add_simple_input_section(*p
, size
, addralign
);
2428 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2429 if (this->os_
== NULL
2430 || (this->os_
->flags() & elfcpp::SHF_TLS
) == 0
2431 || this->os_
->type() != elfcpp::SHT_NOBITS
)
2433 if (!have_load_address
)
2434 *load_address
= address
;
2436 *load_address
+= address
- *dot_value
;
2438 *dot_value
= address
;
2442 // Get the list of segments to use for an allocated section when using
2443 // a PHDRS clause. If this is an allocated section, return the
2444 // Output_section. We don't change the list of segments.
2447 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2449 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2455 // Class Phdrs_element. A program header from a PHDRS clause.
2460 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2461 bool includes_filehdr
, bool includes_phdrs
,
2462 bool is_flags_valid
, unsigned int flags
,
2463 Expression
* load_address
)
2464 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2465 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2466 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2470 // Return the name of this segment.
2473 { return this->name_
; }
2475 // Return the type of the segment.
2478 { return this->type_
; }
2480 // Whether to include the file header.
2482 includes_filehdr() const
2483 { return this->includes_filehdr_
; }
2485 // Whether to include the program headers.
2487 includes_phdrs() const
2488 { return this->includes_phdrs_
; }
2490 // Return whether there is a load address.
2492 has_load_address() const
2493 { return this->load_address_
!= NULL
; }
2495 // Evaluate the load address expression if there is one.
2497 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2499 if (this->load_address_
!= NULL
)
2500 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2504 // Return the load address.
2506 load_address() const
2508 gold_assert(this->load_address_
!= NULL
);
2509 return this->load_address_value_
;
2512 // Create the segment.
2514 create_segment(Layout
* layout
)
2516 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2517 return this->segment_
;
2520 // Return the segment.
2523 { return this->segment_
; }
2525 // Release the segment.
2528 { this->segment_
= NULL
; }
2530 // Set the segment flags if appropriate.
2532 set_flags_if_valid()
2534 if (this->is_flags_valid_
)
2535 this->segment_
->set_flags(this->flags_
);
2538 // Print for debugging.
2543 // The name used in the script.
2545 // The type of the segment (PT_LOAD, etc.).
2547 // Whether this segment includes the file header.
2548 bool includes_filehdr_
;
2549 // Whether this segment includes the section headers.
2550 bool includes_phdrs_
;
2551 // Whether the flags were explicitly specified.
2552 bool is_flags_valid_
;
2553 // The flags for this segment (PF_R, etc.) if specified.
2554 unsigned int flags_
;
2555 // The expression for the load address for this segment. This may
2557 Expression
* load_address_
;
2558 // The actual load address from evaluating the expression.
2559 uint64_t load_address_value_
;
2560 // The segment itself.
2561 Output_segment
* segment_
;
2564 // Print for debugging.
2567 Phdrs_element::print(FILE* f
) const
2569 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2570 if (this->includes_filehdr_
)
2571 fprintf(f
, " FILEHDR");
2572 if (this->includes_phdrs_
)
2573 fprintf(f
, " PHDRS");
2574 if (this->is_flags_valid_
)
2575 fprintf(f
, " FLAGS(%u)", this->flags_
);
2576 if (this->load_address_
!= NULL
)
2579 this->load_address_
->print(f
);
2585 // Class Script_sections.
2587 Script_sections::Script_sections()
2588 : saw_sections_clause_(false),
2589 in_sections_clause_(false),
2590 sections_elements_(NULL
),
2591 output_section_(NULL
),
2592 phdrs_elements_(NULL
),
2593 orphan_section_placement_(NULL
),
2594 data_segment_align_start_(),
2595 saw_data_segment_align_(false),
2596 saw_relro_end_(false),
2597 saw_segment_start_expression_(false)
2601 // Start a SECTIONS clause.
2604 Script_sections::start_sections()
2606 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2607 this->saw_sections_clause_
= true;
2608 this->in_sections_clause_
= true;
2609 if (this->sections_elements_
== NULL
)
2610 this->sections_elements_
= new Sections_elements
;
2613 // Finish a SECTIONS clause.
2616 Script_sections::finish_sections()
2618 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2619 this->in_sections_clause_
= false;
2622 // Add a symbol to be defined.
2625 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2626 Expression
* val
, bool provide
,
2629 if (this->output_section_
!= NULL
)
2630 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2634 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2637 this->sections_elements_
->push_back(p
);
2641 // Add an assignment to the special dot symbol.
2644 Script_sections::add_dot_assignment(Expression
* val
)
2646 if (this->output_section_
!= NULL
)
2647 this->output_section_
->add_dot_assignment(val
);
2650 // The GNU linker permits assignments to . to appears outside of
2651 // a SECTIONS clause, and treats it as appearing inside, so
2652 // sections_elements_ may be NULL here.
2653 if (this->sections_elements_
== NULL
)
2655 this->sections_elements_
= new Sections_elements
;
2656 this->saw_sections_clause_
= true;
2659 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2660 this->sections_elements_
->push_back(p
);
2664 // Add an assertion.
2667 Script_sections::add_assertion(Expression
* check
, const char* message
,
2670 if (this->output_section_
!= NULL
)
2671 this->output_section_
->add_assertion(check
, message
, messagelen
);
2674 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2676 this->sections_elements_
->push_back(p
);
2680 // Start processing entries for an output section.
2683 Script_sections::start_output_section(
2686 const Parser_output_section_header
*header
)
2688 Output_section_definition
* posd
= new Output_section_definition(name
,
2691 this->sections_elements_
->push_back(posd
);
2692 gold_assert(this->output_section_
== NULL
);
2693 this->output_section_
= posd
;
2696 // Stop processing entries for an output section.
2699 Script_sections::finish_output_section(
2700 const Parser_output_section_trailer
* trailer
)
2702 gold_assert(this->output_section_
!= NULL
);
2703 this->output_section_
->finish(trailer
);
2704 this->output_section_
= NULL
;
2707 // Add a data item to the current output section.
2710 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2712 gold_assert(this->output_section_
!= NULL
);
2713 this->output_section_
->add_data(size
, is_signed
, val
);
2716 // Add a fill value setting to the current output section.
2719 Script_sections::add_fill(Expression
* val
)
2721 gold_assert(this->output_section_
!= NULL
);
2722 this->output_section_
->add_fill(val
);
2725 // Add an input section specification to the current output section.
2728 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2730 gold_assert(this->output_section_
!= NULL
);
2731 this->output_section_
->add_input_section(spec
, keep
);
2734 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
2735 // subsequent output sections may be relro.
2738 Script_sections::data_segment_align()
2740 if (this->saw_data_segment_align_
)
2741 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2742 gold_assert(!this->sections_elements_
->empty());
2743 Sections_elements::iterator p
= this->sections_elements_
->end();
2745 this->data_segment_align_start_
= p
;
2746 this->saw_data_segment_align_
= true;
2749 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
2750 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
2753 Script_sections::data_segment_relro_end()
2755 if (this->saw_relro_end_
)
2756 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2757 "in a linker script"));
2758 this->saw_relro_end_
= true;
2760 if (!this->saw_data_segment_align_
)
2761 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2764 Sections_elements::iterator p
= this->data_segment_align_start_
;
2765 for (++p
; p
!= this->sections_elements_
->end(); ++p
)
2766 (*p
)->set_is_relro();
2770 // Create any required sections.
2773 Script_sections::create_sections(Layout
* layout
)
2775 if (!this->saw_sections_clause_
)
2777 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2778 p
!= this->sections_elements_
->end();
2780 (*p
)->create_sections(layout
);
2783 // Add any symbols we are defining to the symbol table.
2786 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2788 if (!this->saw_sections_clause_
)
2790 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2791 p
!= this->sections_elements_
->end();
2793 (*p
)->add_symbols_to_table(symtab
);
2796 // Finalize symbols and check assertions.
2799 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2801 if (!this->saw_sections_clause_
)
2803 uint64_t dot_value
= 0;
2804 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2805 p
!= this->sections_elements_
->end();
2807 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2810 // Return the name of the output section to use for an input file name
2811 // and section name.
2814 Script_sections::output_section_name(
2815 const char* file_name
,
2816 const char* section_name
,
2817 Output_section
*** output_section_slot
,
2818 Script_sections::Section_type
*psection_type
)
2820 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2821 p
!= this->sections_elements_
->end();
2824 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2825 output_section_slot
,
2830 // The special name /DISCARD/ means that the input section
2831 // should be discarded.
2832 if (strcmp(ret
, "/DISCARD/") == 0)
2834 *output_section_slot
= NULL
;
2835 *psection_type
= Script_sections::ST_NONE
;
2842 // If we couldn't find a mapping for the name, the output section
2843 // gets the name of the input section.
2845 *output_section_slot
= NULL
;
2846 *psection_type
= Script_sections::ST_NONE
;
2848 return section_name
;
2851 // Place a marker for an orphan output section into the SECTIONS
2855 Script_sections::place_orphan(Output_section
* os
)
2857 Orphan_section_placement
* osp
= this->orphan_section_placement_
;
2860 // Initialize the Orphan_section_placement structure.
2861 osp
= new Orphan_section_placement();
2862 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2863 p
!= this->sections_elements_
->end();
2865 (*p
)->orphan_section_init(osp
, p
);
2866 gold_assert(!this->sections_elements_
->empty());
2867 Sections_elements::iterator last
= this->sections_elements_
->end();
2869 osp
->last_init(last
);
2870 this->orphan_section_placement_
= osp
;
2873 Orphan_output_section
* orphan
= new Orphan_output_section(os
);
2875 // Look for where to put ORPHAN.
2876 Sections_elements::iterator
* where
;
2877 if (osp
->find_place(os
, &where
))
2879 if ((**where
)->is_relro())
2882 os
->clear_is_relro();
2884 // We want to insert ORPHAN after *WHERE, and then update *WHERE
2885 // so that the next one goes after this one.
2886 Sections_elements::iterator p
= *where
;
2887 gold_assert(p
!= this->sections_elements_
->end());
2889 *where
= this->sections_elements_
->insert(p
, orphan
);
2893 os
->clear_is_relro();
2894 // We don't have a place to put this orphan section. Put it,
2895 // and all other sections like it, at the end, but before the
2896 // sections which always come at the end.
2897 Sections_elements::iterator last
= osp
->last_place();
2898 *where
= this->sections_elements_
->insert(last
, orphan
);
2902 // Set the addresses of all the output sections. Walk through all the
2903 // elements, tracking the dot symbol. Apply assignments which set
2904 // absolute symbol values, in case they are used when setting dot.
2905 // Fill in data statement values. As we find output sections, set the
2906 // address, set the address of all associated input sections, and
2907 // update dot. Return the segment which should hold the file header
2908 // and segment headers, if any.
2911 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2913 gold_assert(this->saw_sections_clause_
);
2915 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2916 // for our representation.
2917 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2918 p
!= this->sections_elements_
->end();
2921 Output_section_definition
* posd
;
2922 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2923 if (failed_constraint
!= CONSTRAINT_NONE
)
2925 Sections_elements::iterator q
;
2926 for (q
= this->sections_elements_
->begin();
2927 q
!= this->sections_elements_
->end();
2932 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2937 if (q
== this->sections_elements_
->end())
2938 gold_error(_("no matching section constraint"));
2942 // Force the alignment of the first TLS section to be the maximum
2943 // alignment of all TLS sections.
2944 Output_section
* first_tls
= NULL
;
2945 uint64_t tls_align
= 0;
2946 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2947 p
!= this->sections_elements_
->end();
2950 Output_section
*os
= (*p
)->get_output_section();
2951 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
2953 if (first_tls
== NULL
)
2955 if (os
->addralign() > tls_align
)
2956 tls_align
= os
->addralign();
2959 if (first_tls
!= NULL
)
2960 first_tls
->set_addralign(tls_align
);
2962 // For a relocatable link, we implicitly set dot to zero.
2963 uint64_t dot_value
= 0;
2964 uint64_t dot_alignment
= 0;
2965 uint64_t load_address
= 0;
2967 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
2968 // to set section addresses. If the script has any SEGMENT_START
2969 // expression, we do not set the section addresses.
2970 bool use_tsection_options
=
2971 (!this->saw_segment_start_expression_
2972 && (parameters
->options().user_set_Ttext()
2973 || parameters
->options().user_set_Tdata()
2974 || parameters
->options().user_set_Tbss()));
2976 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2977 p
!= this->sections_elements_
->end();
2980 Output_section
* os
= (*p
)->get_output_section();
2982 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
2983 // the special sections by names and doing dot assignments.
2984 if (use_tsection_options
2986 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
2988 uint64_t new_dot_value
= dot_value
;
2990 if (parameters
->options().user_set_Ttext()
2991 && strcmp(os
->name(), ".text") == 0)
2992 new_dot_value
= parameters
->options().Ttext();
2993 else if (parameters
->options().user_set_Tdata()
2994 && strcmp(os
->name(), ".data") == 0)
2995 new_dot_value
= parameters
->options().Tdata();
2996 else if (parameters
->options().user_set_Tbss()
2997 && strcmp(os
->name(), ".bss") == 0)
2998 new_dot_value
= parameters
->options().Tbss();
3000 // Update dot and load address if necessary.
3001 if (new_dot_value
< dot_value
)
3002 gold_error(_("dot may not move backward"));
3003 else if (new_dot_value
!= dot_value
)
3005 dot_value
= new_dot_value
;
3006 load_address
= new_dot_value
;
3010 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &dot_alignment
,
3014 if (this->phdrs_elements_
!= NULL
)
3016 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3017 p
!= this->phdrs_elements_
->end();
3019 (*p
)->eval_load_address(symtab
, layout
);
3022 return this->create_segments(layout
, dot_alignment
);
3025 // Sort the sections in order to put them into segments.
3027 class Sort_output_sections
3031 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
3035 Sort_output_sections::operator()(const Output_section
* os1
,
3036 const Output_section
* os2
) const
3038 // Sort first by the load address.
3039 uint64_t lma1
= (os1
->has_load_address()
3040 ? os1
->load_address()
3042 uint64_t lma2
= (os2
->has_load_address()
3043 ? os2
->load_address()
3048 // Then sort by the virtual address.
3049 if (os1
->address() != os2
->address())
3050 return os1
->address() < os2
->address();
3052 // Sort TLS sections to the end.
3053 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
3054 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
3058 // Sort PROGBITS before NOBITS.
3059 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
3061 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
3064 // Sort non-NOLOAD before NOLOAD.
3065 if (os1
->is_noload() && !os2
->is_noload())
3067 if (!os1
->is_noload() && os2
->is_noload())
3070 // Otherwise we don't care.
3074 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3075 // We treat a section with the SHF_TLS flag set as taking up space
3076 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3077 // space for them in the file.
3080 Script_sections::is_bss_section(const Output_section
* os
)
3082 return (os
->type() == elfcpp::SHT_NOBITS
3083 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
3086 // Return the size taken by the file header and the program headers.
3089 Script_sections::total_header_size(Layout
* layout
) const
3091 size_t segment_count
= layout
->segment_count();
3092 size_t file_header_size
;
3093 size_t segment_headers_size
;
3094 if (parameters
->target().get_size() == 32)
3096 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
3097 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
3099 else if (parameters
->target().get_size() == 64)
3101 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
3102 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
3107 return file_header_size
+ segment_headers_size
;
3110 // Return the amount we have to subtract from the LMA to accomodate
3111 // headers of the given size. The complication is that the file
3112 // header have to be at the start of a page, as otherwise it will not
3113 // be at the start of the file.
3116 Script_sections::header_size_adjustment(uint64_t lma
,
3117 size_t sizeof_headers
) const
3119 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3120 uint64_t hdr_lma
= lma
- sizeof_headers
;
3121 hdr_lma
&= ~(abi_pagesize
- 1);
3122 return lma
- hdr_lma
;
3125 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3126 // the segment which should hold the file header and segment headers,
3130 Script_sections::create_segments(Layout
* layout
, uint64_t dot_alignment
)
3132 gold_assert(this->saw_sections_clause_
);
3134 if (parameters
->options().relocatable())
3137 if (this->saw_phdrs_clause())
3138 return create_segments_from_phdrs_clause(layout
, dot_alignment
);
3140 Layout::Section_list sections
;
3141 layout
->get_allocated_sections(§ions
);
3143 // Sort the sections by address.
3144 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
3146 this->create_note_and_tls_segments(layout
, §ions
);
3148 // Walk through the sections adding them to PT_LOAD segments.
3149 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3150 Output_segment
* first_seg
= NULL
;
3151 Output_segment
* current_seg
= NULL
;
3152 bool is_current_seg_readonly
= true;
3153 Layout::Section_list::iterator plast
= sections
.end();
3154 uint64_t last_vma
= 0;
3155 uint64_t last_lma
= 0;
3156 uint64_t last_size
= 0;
3157 for (Layout::Section_list::iterator p
= sections
.begin();
3158 p
!= sections
.end();
3161 const uint64_t vma
= (*p
)->address();
3162 const uint64_t lma
= ((*p
)->has_load_address()
3163 ? (*p
)->load_address()
3165 const uint64_t size
= (*p
)->current_data_size();
3167 bool need_new_segment
;
3168 if (current_seg
== NULL
)
3169 need_new_segment
= true;
3170 else if (lma
- vma
!= last_lma
- last_vma
)
3172 // This section has a different LMA relationship than the
3173 // last one; we need a new segment.
3174 need_new_segment
= true;
3176 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
3177 < align_address(lma
, abi_pagesize
))
3179 // Putting this section in the segment would require
3181 need_new_segment
= true;
3183 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
3185 // A non-BSS section can not follow a BSS section in the
3187 need_new_segment
= true;
3189 else if (is_current_seg_readonly
3190 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
3191 && !parameters
->options().omagic())
3193 // Don't put a writable section in the same segment as a
3194 // non-writable section.
3195 need_new_segment
= true;
3199 // Otherwise, reuse the existing segment.
3200 need_new_segment
= false;
3203 elfcpp::Elf_Word seg_flags
=
3204 Layout::section_flags_to_segment((*p
)->flags());
3206 if (need_new_segment
)
3208 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3210 current_seg
->set_addresses(vma
, lma
);
3211 current_seg
->set_minimum_p_align(dot_alignment
);
3212 if (first_seg
== NULL
)
3213 first_seg
= current_seg
;
3214 is_current_seg_readonly
= true;
3217 current_seg
->add_output_section(*p
, seg_flags
, false);
3219 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
3220 is_current_seg_readonly
= false;
3228 // An ELF program should work even if the program headers are not in
3229 // a PT_LOAD segment. However, it appears that the Linux kernel
3230 // does not set the AT_PHDR auxiliary entry in that case. It sets
3231 // the load address to p_vaddr - p_offset of the first PT_LOAD
3232 // segment. It then sets AT_PHDR to the load address plus the
3233 // offset to the program headers, e_phoff in the file header. This
3234 // fails when the program headers appear in the file before the
3235 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3236 // segment to hold the file header and the program headers. This is
3237 // effectively what the GNU linker does, and it is slightly more
3238 // efficient in any case. We try to use the first PT_LOAD segment
3239 // if we can, otherwise we make a new one.
3241 if (first_seg
== NULL
)
3244 // -n or -N mean that the program is not demand paged and there is
3245 // no need to put the program headers in a PT_LOAD segment.
3246 if (parameters
->options().nmagic() || parameters
->options().omagic())
3249 size_t sizeof_headers
= this->total_header_size(layout
);
3251 uint64_t vma
= first_seg
->vaddr();
3252 uint64_t lma
= first_seg
->paddr();
3254 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
3256 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
3258 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3262 // If there is no room to squeeze in the headers, then punt. The
3263 // resulting executable probably won't run on GNU/Linux, but we
3264 // trust that the user knows what they are doing.
3265 if (lma
< subtract
|| vma
< subtract
)
3268 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3270 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3275 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3276 // segment if there are any SHT_TLS sections.
3279 Script_sections::create_note_and_tls_segments(
3281 const Layout::Section_list
* sections
)
3283 gold_assert(!this->saw_phdrs_clause());
3285 bool saw_tls
= false;
3286 for (Layout::Section_list::const_iterator p
= sections
->begin();
3287 p
!= sections
->end();
3290 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3292 elfcpp::Elf_Word seg_flags
=
3293 Layout::section_flags_to_segment((*p
)->flags());
3294 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
3296 oseg
->add_output_section(*p
, seg_flags
, false);
3298 // Incorporate any subsequent SHT_NOTE sections, in the
3299 // hopes that the script is sensible.
3300 Layout::Section_list::const_iterator pnext
= p
+ 1;
3301 while (pnext
!= sections
->end()
3302 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
3304 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3305 oseg
->add_output_section(*pnext
, seg_flags
, false);
3311 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3314 gold_error(_("TLS sections are not adjacent"));
3316 elfcpp::Elf_Word seg_flags
=
3317 Layout::section_flags_to_segment((*p
)->flags());
3318 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
3320 oseg
->add_output_section(*p
, seg_flags
, false);
3322 Layout::Section_list::const_iterator pnext
= p
+ 1;
3323 while (pnext
!= sections
->end()
3324 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
3326 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3327 oseg
->add_output_section(*pnext
, seg_flags
, false);
3337 // Add a program header. The PHDRS clause is syntactically distinct
3338 // from the SECTIONS clause, but we implement it with the SECTIONS
3339 // support because PHDRS is useless if there is no SECTIONS clause.
3342 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
3343 bool includes_filehdr
, bool includes_phdrs
,
3344 bool is_flags_valid
, unsigned int flags
,
3345 Expression
* load_address
)
3347 if (this->phdrs_elements_
== NULL
)
3348 this->phdrs_elements_
= new Phdrs_elements();
3349 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
3352 is_flags_valid
, flags
,
3356 // Return the number of segments we expect to create based on the
3357 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3360 Script_sections::expected_segment_count(const Layout
* layout
) const
3362 if (this->saw_phdrs_clause())
3363 return this->phdrs_elements_
->size();
3365 Layout::Section_list sections
;
3366 layout
->get_allocated_sections(§ions
);
3368 // We assume that we will need two PT_LOAD segments.
3371 bool saw_note
= false;
3372 bool saw_tls
= false;
3373 for (Layout::Section_list::const_iterator p
= sections
.begin();
3374 p
!= sections
.end();
3377 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3379 // Assume that all note sections will fit into a single
3387 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3389 // There can only be one PT_TLS segment.
3401 // Create the segments from a PHDRS clause. Return the segment which
3402 // should hold the file header and program headers, if any.
3405 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
,
3406 uint64_t dot_alignment
)
3408 this->attach_sections_using_phdrs_clause(layout
);
3409 return this->set_phdrs_clause_addresses(layout
, dot_alignment
);
3412 // Create the segments from the PHDRS clause, and put the output
3413 // sections in them.
3416 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
3418 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
3419 Name_to_segment name_to_segment
;
3420 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3421 p
!= this->phdrs_elements_
->end();
3423 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
3425 // Walk through the output sections and attach them to segments.
3426 // Output sections in the script which do not list segments are
3427 // attached to the same set of segments as the immediately preceding
3430 String_list
* phdr_names
= NULL
;
3431 bool load_segments_only
= false;
3432 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3433 p
!= this->sections_elements_
->end();
3437 String_list
* old_phdr_names
= phdr_names
;
3438 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
3442 if (phdr_names
== NULL
)
3444 gold_error(_("allocated section not in any segment"));
3448 // We see a list of segments names. Disable PT_LOAD segment only
3450 if (old_phdr_names
!= phdr_names
)
3451 load_segments_only
= false;
3453 // If this is an orphan section--one that was not explicitly
3454 // mentioned in the linker script--then it should not inherit
3455 // any segment type other than PT_LOAD. Otherwise, e.g., the
3456 // PT_INTERP segment will pick up following orphan sections,
3457 // which does not make sense. If this is not an orphan section,
3458 // we trust the linker script.
3461 // Enable PT_LOAD segments only filtering until we see another
3462 // list of segment names.
3463 load_segments_only
= true;
3466 bool in_load_segment
= false;
3467 for (String_list::const_iterator q
= phdr_names
->begin();
3468 q
!= phdr_names
->end();
3471 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3472 if (r
== name_to_segment
.end())
3473 gold_error(_("no segment %s"), q
->c_str());
3476 if (load_segments_only
3477 && r
->second
->type() != elfcpp::PT_LOAD
)
3480 elfcpp::Elf_Word seg_flags
=
3481 Layout::section_flags_to_segment(os
->flags());
3482 r
->second
->add_output_section(os
, seg_flags
, false);
3484 if (r
->second
->type() == elfcpp::PT_LOAD
)
3486 if (in_load_segment
)
3487 gold_error(_("section in two PT_LOAD segments"));
3488 in_load_segment
= true;
3493 if (!in_load_segment
)
3494 gold_error(_("allocated section not in any PT_LOAD segment"));
3498 // Set the addresses for segments created from a PHDRS clause. Return
3499 // the segment which should hold the file header and program headers,
3503 Script_sections::set_phdrs_clause_addresses(Layout
* layout
,
3504 uint64_t dot_alignment
)
3506 Output_segment
* load_seg
= NULL
;
3507 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3508 p
!= this->phdrs_elements_
->end();
3511 // Note that we have to set the flags after adding the output
3512 // sections to the segment, as adding an output segment can
3513 // change the flags.
3514 (*p
)->set_flags_if_valid();
3516 Output_segment
* oseg
= (*p
)->segment();
3518 if (oseg
->type() != elfcpp::PT_LOAD
)
3520 // The addresses of non-PT_LOAD segments are set from the
3521 // PT_LOAD segments.
3522 if ((*p
)->has_load_address())
3523 gold_error(_("may only specify load address for PT_LOAD segment"));
3527 oseg
->set_minimum_p_align(dot_alignment
);
3529 // The output sections should have addresses from the SECTIONS
3530 // clause. The addresses don't have to be in order, so find the
3531 // one with the lowest load address. Use that to set the
3532 // address of the segment.
3534 Output_section
* osec
= oseg
->section_with_lowest_load_address();
3537 oseg
->set_addresses(0, 0);
3541 uint64_t vma
= osec
->address();
3542 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3544 // Override the load address of the section with the load
3545 // address specified for the segment.
3546 if ((*p
)->has_load_address())
3548 if (osec
->has_load_address())
3549 gold_warning(_("PHDRS load address overrides "
3550 "section %s load address"),
3553 lma
= (*p
)->load_address();
3556 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3557 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3559 // We could support this if we wanted to.
3560 gold_error(_("using only one of FILEHDR and PHDRS is "
3561 "not currently supported"));
3565 size_t sizeof_headers
= this->total_header_size(layout
);
3566 uint64_t subtract
= this->header_size_adjustment(lma
,
3568 if (lma
>= subtract
&& vma
>= subtract
)
3575 gold_error(_("sections loaded on first page without room "
3576 "for file and program headers "
3577 "are not supported"));
3580 if (load_seg
!= NULL
)
3581 gold_error(_("using FILEHDR and PHDRS on more than one "
3582 "PT_LOAD segment is not currently supported"));
3586 oseg
->set_addresses(vma
, lma
);
3592 // Add the file header and segment headers to non-load segments
3593 // specified in the PHDRS clause.
3596 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3597 Output_data
* segment_headers
)
3599 gold_assert(this->saw_phdrs_clause());
3600 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3601 p
!= this->phdrs_elements_
->end();
3604 if ((*p
)->type() != elfcpp::PT_LOAD
)
3606 if ((*p
)->includes_phdrs())
3607 (*p
)->segment()->add_initial_output_data(segment_headers
);
3608 if ((*p
)->includes_filehdr())
3609 (*p
)->segment()->add_initial_output_data(file_header
);
3614 // Look for an output section by name and return the address, the load
3615 // address, the alignment, and the size. This is used when an
3616 // expression refers to an output section which was not actually
3617 // created. This returns true if the section was found, false
3621 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3622 uint64_t* load_address
,
3623 uint64_t* addralign
,
3624 uint64_t* size
) const
3626 if (!this->saw_sections_clause_
)
3628 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3629 p
!= this->sections_elements_
->end();
3631 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3637 // Release all Output_segments. This remove all pointers to all
3641 Script_sections::release_segments()
3643 if (this->saw_phdrs_clause())
3645 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3646 p
!= this->phdrs_elements_
->end();
3648 (*p
)->release_segment();
3652 // Print the SECTIONS clause to F for debugging.
3655 Script_sections::print(FILE* f
) const
3657 if (!this->saw_sections_clause_
)
3660 fprintf(f
, "SECTIONS {\n");
3662 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3663 p
!= this->sections_elements_
->end();
3669 if (this->phdrs_elements_
!= NULL
)
3671 fprintf(f
, "PHDRS {\n");
3672 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3673 p
!= this->phdrs_elements_
->end();
3680 } // End namespace gold.