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
***)
362 // Initialize OSP with an output section.
364 orphan_section_init(Orphan_section_placement
*,
365 Script_sections::Elements_iterator
)
368 // Set section addresses. This includes applying assignments if the
369 // the expression is an absolute value.
371 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*)
374 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
375 // this section is constrained, and the input sections do not match,
376 // return the constraint, and set *POSD.
377 virtual Section_constraint
378 check_constraint(Output_section_definition
**)
379 { return CONSTRAINT_NONE
; }
381 // See if this is the alternate output section for a constrained
382 // output section. If it is, transfer the Output_section and return
383 // true. Otherwise return false.
385 alternate_constraint(Output_section_definition
*, Section_constraint
)
388 // Get the list of segments to use for an allocated section when
389 // using a PHDRS clause. If this is an allocated section, return
390 // the Output_section, and set *PHDRS_LIST (the first parameter) to
391 // the list of PHDRS to which it should be attached. If the PHDRS
392 // were not specified, don't change *PHDRS_LIST. When not returning
393 // NULL, set *ORPHAN (the second parameter) according to whether
394 // this is an orphan section--one that is not mentioned in the
396 virtual Output_section
*
397 allocate_to_segment(String_list
**, bool*)
400 // Look for an output section by name and return the address, the
401 // load address, the alignment, and the size. This is used when an
402 // expression refers to an output section which was not actually
403 // created. This returns true if the section was found, false
404 // otherwise. The only real definition is for
405 // Output_section_definition.
407 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
411 // Return the associated Output_section if there is one.
412 virtual Output_section
*
413 get_output_section() const
416 // Print the element for debugging purposes.
418 print(FILE* f
) const = 0;
421 // An assignment in a SECTIONS clause outside of an output section.
423 class Sections_element_assignment
: public Sections_element
426 Sections_element_assignment(const char* name
, size_t namelen
,
427 Expression
* val
, bool provide
, bool hidden
)
428 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
431 // Add the symbol to the symbol table.
433 add_symbols_to_table(Symbol_table
* symtab
)
434 { this->assignment_
.add_to_table(symtab
); }
436 // Finalize the symbol.
438 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
441 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
444 // Set the section address. There is no section here, but if the
445 // value is absolute, we set the symbol. This permits us to use
446 // absolute symbols when setting dot.
448 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
449 uint64_t* dot_value
, uint64_t*)
451 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
454 // Print for debugging.
459 this->assignment_
.print(f
);
463 Symbol_assignment assignment_
;
466 // An assignment to the dot symbol in a SECTIONS clause outside of an
469 class Sections_element_dot_assignment
: public Sections_element
472 Sections_element_dot_assignment(Expression
* val
)
476 // Finalize the symbol.
478 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
481 // We ignore the section of the result because outside of an
482 // output section definition the dot symbol is always considered
484 Output_section
* dummy
;
485 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
489 // Update the dot symbol while setting section addresses.
491 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
492 uint64_t* dot_value
, uint64_t* load_address
)
494 Output_section
* dummy
;
495 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
497 *load_address
= *dot_value
;
500 // Print for debugging.
505 this->val_
->print(f
);
513 // An assertion in a SECTIONS clause outside of an output section.
515 class Sections_element_assertion
: public Sections_element
518 Sections_element_assertion(Expression
* check
, const char* message
,
520 : assertion_(check
, message
, messagelen
)
523 // Check the assertion.
525 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
526 { this->assertion_
.check(symtab
, layout
); }
528 // Print for debugging.
533 this->assertion_
.print(f
);
537 Script_assertion assertion_
;
540 // An element in an output section in a SECTIONS clause.
542 class Output_section_element
545 // A list of input sections.
546 typedef std::list
<Output_section::Simple_input_section
> Input_section_list
;
548 Output_section_element()
551 virtual ~Output_section_element()
554 // Return whether this element requires an output section to exist.
556 needs_output_section() const
559 // Add any symbol being defined to the symbol table.
561 add_symbols_to_table(Symbol_table
*)
564 // Finalize symbols and check assertions.
566 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
569 // Return whether this element matches FILE_NAME and SECTION_NAME.
570 // The only real implementation is in Output_section_element_input.
572 match_name(const char*, const char*) const
575 // Set section addresses. This includes applying assignments if the
576 // the expression is an absolute value.
578 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
579 uint64_t*, Output_section
**, std::string
*,
583 // Print the element for debugging purposes.
585 print(FILE* f
) const = 0;
588 // Return a fill string that is LENGTH bytes long, filling it with
591 get_fill_string(const std::string
* fill
, section_size_type length
) const;
595 Output_section_element::get_fill_string(const std::string
* fill
,
596 section_size_type length
) const
598 std::string this_fill
;
599 this_fill
.reserve(length
);
600 while (this_fill
.length() + fill
->length() <= length
)
602 if (this_fill
.length() < length
)
603 this_fill
.append(*fill
, 0, length
- this_fill
.length());
607 // A symbol assignment in an output section.
609 class Output_section_element_assignment
: public Output_section_element
612 Output_section_element_assignment(const char* name
, size_t namelen
,
613 Expression
* val
, bool provide
,
615 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
618 // Add the symbol to the symbol table.
620 add_symbols_to_table(Symbol_table
* symtab
)
621 { this->assignment_
.add_to_table(symtab
); }
623 // Finalize the symbol.
625 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
626 uint64_t* dot_value
, Output_section
** dot_section
)
628 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
632 // Set the section address. There is no section here, but if the
633 // value is absolute, we set the symbol. This permits us to use
634 // absolute symbols when setting dot.
636 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
637 uint64_t, uint64_t* dot_value
, Output_section
**,
638 std::string
*, Input_section_list
*)
640 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
643 // Print for debugging.
648 this->assignment_
.print(f
);
652 Symbol_assignment assignment_
;
655 // An assignment to the dot symbol in an output section.
657 class Output_section_element_dot_assignment
: public Output_section_element
660 Output_section_element_dot_assignment(Expression
* val
)
664 // Finalize the symbol.
666 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
667 uint64_t* dot_value
, Output_section
** dot_section
)
669 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
670 *dot_section
, dot_section
);
673 // Update the dot symbol while setting section addresses.
675 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
676 uint64_t, uint64_t* dot_value
, Output_section
**,
677 std::string
*, Input_section_list
*);
679 // Print for debugging.
684 this->val_
->print(f
);
692 // Update the dot symbol while setting section addresses.
695 Output_section_element_dot_assignment::set_section_addresses(
696 Symbol_table
* symtab
,
698 Output_section
* output_section
,
701 Output_section
** dot_section
,
705 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
706 *dot_value
, *dot_section
,
708 if (next_dot
< *dot_value
)
709 gold_error(_("dot may not move backward"));
710 if (next_dot
> *dot_value
&& output_section
!= NULL
)
712 section_size_type length
= convert_to_section_size_type(next_dot
714 Output_section_data
* posd
;
716 posd
= new Output_data_zero_fill(length
, 0);
719 std::string this_fill
= this->get_fill_string(fill
, length
);
720 posd
= new Output_data_const(this_fill
, 0);
722 output_section
->add_output_section_data(posd
);
723 layout
->new_output_section_data_from_script(posd
);
725 *dot_value
= next_dot
;
728 // An assertion in an output section.
730 class Output_section_element_assertion
: public Output_section_element
733 Output_section_element_assertion(Expression
* check
, const char* message
,
735 : assertion_(check
, message
, messagelen
)
742 this->assertion_
.print(f
);
746 Script_assertion assertion_
;
749 // We use a special instance of Output_section_data to handle BYTE,
750 // SHORT, etc. This permits forward references to symbols in the
753 class Output_data_expression
: public Output_section_data
756 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
757 const Symbol_table
* symtab
, const Layout
* layout
,
758 uint64_t dot_value
, Output_section
* dot_section
)
759 : Output_section_data(size
, 0, true),
760 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
761 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
765 // Write the data to the output file.
767 do_write(Output_file
*);
769 // Write the data to a buffer.
771 do_write_to_buffer(unsigned char*);
773 // Write to a map file.
775 do_print_to_mapfile(Mapfile
* mapfile
) const
776 { mapfile
->print_output_data(this, _("** expression")); }
779 template<bool big_endian
>
781 endian_write_to_buffer(uint64_t, unsigned char*);
785 const Symbol_table
* symtab_
;
786 const Layout
* layout_
;
788 Output_section
* dot_section_
;
791 // Write the data element to the output file.
794 Output_data_expression::do_write(Output_file
* of
)
796 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
797 this->write_to_buffer(view
);
798 of
->write_output_view(this->offset(), this->data_size(), view
);
801 // Write the data element to a buffer.
804 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
806 Output_section
* dummy
;
807 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
808 true, this->dot_value_
,
809 this->dot_section_
, &dummy
);
811 if (parameters
->target().is_big_endian())
812 this->endian_write_to_buffer
<true>(val
, buf
);
814 this->endian_write_to_buffer
<false>(val
, buf
);
817 template<bool big_endian
>
819 Output_data_expression::endian_write_to_buffer(uint64_t val
,
822 switch (this->data_size())
825 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
828 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
831 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
834 if (parameters
->target().get_size() == 32)
837 if (this->is_signed_
&& (val
& 0x80000000) != 0)
838 val
|= 0xffffffff00000000LL
;
840 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
847 // A data item in an output section.
849 class Output_section_element_data
: public Output_section_element
852 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
853 : size_(size
), is_signed_(is_signed
), val_(val
)
856 // If there is a data item, then we must create an output section.
858 needs_output_section() const
861 // Finalize symbols--we just need to update dot.
863 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
865 { *dot_value
+= this->size_
; }
867 // Store the value in the section.
869 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
870 uint64_t* dot_value
, Output_section
**, std::string
*,
871 Input_section_list
*);
873 // Print for debugging.
878 // The size in bytes.
880 // Whether the value is signed.
886 // Store the value in the section.
889 Output_section_element_data::set_section_addresses(
890 Symbol_table
* symtab
,
895 Output_section
** dot_section
,
899 gold_assert(os
!= NULL
);
900 Output_data_expression
* expression
=
901 new Output_data_expression(this->size_
, this->is_signed_
, this->val_
,
902 symtab
, layout
, *dot_value
, *dot_section
);
903 os
->add_output_section_data(expression
);
904 layout
->new_output_section_data_from_script(expression
);
905 *dot_value
+= this->size_
;
908 // Print for debugging.
911 Output_section_element_data::print(FILE* f
) const
926 if (this->is_signed_
)
934 fprintf(f
, " %s(", s
);
935 this->val_
->print(f
);
939 // A fill value setting in an output section.
941 class Output_section_element_fill
: public Output_section_element
944 Output_section_element_fill(Expression
* val
)
948 // Update the fill value while setting section addresses.
950 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
951 uint64_t, uint64_t* dot_value
,
952 Output_section
** dot_section
,
953 std::string
* fill
, Input_section_list
*)
955 Output_section
* fill_section
;
956 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
957 *dot_value
, *dot_section
,
959 if (fill_section
!= NULL
)
960 gold_warning(_("fill value is not absolute"));
961 // FIXME: The GNU linker supports fill values of arbitrary length.
962 unsigned char fill_buff
[4];
963 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
964 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
967 // Print for debugging.
971 fprintf(f
, " FILL(");
972 this->val_
->print(f
);
977 // The new fill value.
981 // Return whether STRING contains a wildcard character. This is used
982 // to speed up matching.
985 is_wildcard_string(const std::string
& s
)
987 return strpbrk(s
.c_str(), "?*[") != NULL
;
990 // An input section specification in an output section
992 class Output_section_element_input
: public Output_section_element
995 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
997 // Finalize symbols--just update the value of the dot symbol.
999 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1000 Output_section
** dot_section
)
1002 *dot_value
= this->final_dot_value_
;
1003 *dot_section
= this->final_dot_section_
;
1006 // See whether we match FILE_NAME and SECTION_NAME as an input
1009 match_name(const char* file_name
, const char* section_name
) const;
1011 // Set the section address.
1013 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1014 uint64_t subalign
, uint64_t* dot_value
,
1015 Output_section
**, std::string
* fill
,
1016 Input_section_list
*);
1018 // Print for debugging.
1020 print(FILE* f
) const;
1023 // An input section pattern.
1024 struct Input_section_pattern
1026 std::string pattern
;
1027 bool pattern_is_wildcard
;
1030 Input_section_pattern(const char* patterna
, size_t patternlena
,
1031 Sort_wildcard sorta
)
1032 : pattern(patterna
, patternlena
),
1033 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
1038 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
1040 // Filename_exclusions is a pair of filename pattern and a bool
1041 // indicating whether the filename is a wildcard.
1042 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
1044 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1045 // indicates whether this is a wildcard pattern.
1047 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
1049 return (is_wildcard_pattern
1050 ? fnmatch(pattern
, string
, 0) == 0
1051 : strcmp(string
, pattern
) == 0);
1054 // See if we match a file name.
1056 match_file_name(const char* file_name
) const;
1058 // The file name pattern. If this is the empty string, we match all
1060 std::string filename_pattern_
;
1061 // Whether the file name pattern is a wildcard.
1062 bool filename_is_wildcard_
;
1063 // How the file names should be sorted. This may only be
1064 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1065 Sort_wildcard filename_sort_
;
1066 // The list of file names to exclude.
1067 Filename_exclusions filename_exclusions_
;
1068 // The list of input section patterns.
1069 Input_section_patterns input_section_patterns_
;
1070 // Whether to keep this section when garbage collecting.
1072 // The value of dot after including all matching sections.
1073 uint64_t final_dot_value_
;
1074 // The section where dot is defined after including all matching
1076 Output_section
* final_dot_section_
;
1079 // Construct Output_section_element_input. The parser records strings
1080 // as pointers into a copy of the script file, which will go away when
1081 // parsing is complete. We make sure they are in std::string objects.
1083 Output_section_element_input::Output_section_element_input(
1084 const Input_section_spec
* spec
,
1086 : filename_pattern_(),
1087 filename_is_wildcard_(false),
1088 filename_sort_(spec
->file
.sort
),
1089 filename_exclusions_(),
1090 input_section_patterns_(),
1092 final_dot_value_(0),
1093 final_dot_section_(NULL
)
1095 // The filename pattern "*" is common, and matches all files. Turn
1096 // it into the empty string.
1097 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
1098 this->filename_pattern_
.assign(spec
->file
.name
.value
,
1099 spec
->file
.name
.length
);
1100 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
1102 if (spec
->input_sections
.exclude
!= NULL
)
1104 for (String_list::const_iterator p
=
1105 spec
->input_sections
.exclude
->begin();
1106 p
!= spec
->input_sections
.exclude
->end();
1109 bool is_wildcard
= is_wildcard_string(*p
);
1110 this->filename_exclusions_
.push_back(std::make_pair(*p
,
1115 if (spec
->input_sections
.sections
!= NULL
)
1117 Input_section_patterns
& isp(this->input_section_patterns_
);
1118 for (String_sort_list::const_iterator p
=
1119 spec
->input_sections
.sections
->begin();
1120 p
!= spec
->input_sections
.sections
->end();
1122 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
1127 // See whether we match FILE_NAME.
1130 Output_section_element_input::match_file_name(const char* file_name
) const
1132 if (!this->filename_pattern_
.empty())
1134 // If we were called with no filename, we refuse to match a
1135 // pattern which requires a file name.
1136 if (file_name
== NULL
)
1139 if (!match(file_name
, this->filename_pattern_
.c_str(),
1140 this->filename_is_wildcard_
))
1144 if (file_name
!= NULL
)
1146 // Now we have to see whether FILE_NAME matches one of the
1147 // exclusion patterns, if any.
1148 for (Filename_exclusions::const_iterator p
=
1149 this->filename_exclusions_
.begin();
1150 p
!= this->filename_exclusions_
.end();
1153 if (match(file_name
, p
->first
.c_str(), p
->second
))
1161 // See whether we match FILE_NAME and SECTION_NAME.
1164 Output_section_element_input::match_name(const char* file_name
,
1165 const char* section_name
) const
1167 if (!this->match_file_name(file_name
))
1170 // If there are no section name patterns, then we match.
1171 if (this->input_section_patterns_
.empty())
1174 // See whether we match the section name patterns.
1175 for (Input_section_patterns::const_iterator p
=
1176 this->input_section_patterns_
.begin();
1177 p
!= this->input_section_patterns_
.end();
1180 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
1184 // We didn't match any section names, so we didn't match.
1188 // Information we use to sort the input sections.
1190 class Input_section_info
1193 Input_section_info(const Output_section::Simple_input_section
& input_section
)
1194 : input_section_(input_section
), section_name_(),
1195 size_(0), addralign_(1)
1198 // Return the simple input section.
1199 const Output_section::Simple_input_section
&
1200 input_section() const
1201 { return this->input_section_
; }
1203 // Return the object.
1206 { return this->input_section_
.relobj(); }
1208 // Return the section index.
1211 { return this->input_section_
.shndx(); }
1213 // Return the section name.
1215 section_name() const
1216 { return this->section_name_
; }
1218 // Set the section name.
1220 set_section_name(const std::string name
)
1221 { this->section_name_
= name
; }
1223 // Return the section size.
1226 { return this->size_
; }
1228 // Set the section size.
1230 set_size(uint64_t size
)
1231 { this->size_
= size
; }
1233 // Return the address alignment.
1236 { return this->addralign_
; }
1238 // Set the address alignment.
1240 set_addralign(uint64_t addralign
)
1241 { this->addralign_
= addralign
; }
1244 // Input section, can be a relaxed section.
1245 Output_section::Simple_input_section input_section_
;
1246 // Name of the section.
1247 std::string section_name_
;
1250 // Address alignment.
1251 uint64_t addralign_
;
1254 // A class to sort the input sections.
1256 class Input_section_sorter
1259 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
1260 : filename_sort_(filename_sort
), section_sort_(section_sort
)
1264 operator()(const Input_section_info
&, const Input_section_info
&) const;
1267 Sort_wildcard filename_sort_
;
1268 Sort_wildcard section_sort_
;
1272 Input_section_sorter::operator()(const Input_section_info
& isi1
,
1273 const Input_section_info
& isi2
) const
1275 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
1276 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1277 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1278 && isi1
.addralign() == isi2
.addralign()))
1280 if (isi1
.section_name() != isi2
.section_name())
1281 return isi1
.section_name() < isi2
.section_name();
1283 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
1284 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1285 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
1287 if (isi1
.addralign() != isi2
.addralign())
1288 return isi1
.addralign() < isi2
.addralign();
1290 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
1292 if (isi1
.relobj()->name() != isi2
.relobj()->name())
1293 return (isi1
.relobj()->name() < isi2
.relobj()->name());
1296 // Otherwise we leave them in the same order.
1300 // Set the section address. Look in INPUT_SECTIONS for sections which
1301 // match this spec, sort them as specified, and add them to the output
1305 Output_section_element_input::set_section_addresses(
1308 Output_section
* output_section
,
1310 uint64_t* dot_value
,
1311 Output_section
** dot_section
,
1313 Input_section_list
* input_sections
)
1315 // We build a list of sections which match each
1316 // Input_section_pattern.
1318 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
1319 size_t input_pattern_count
= this->input_section_patterns_
.size();
1320 if (input_pattern_count
== 0)
1321 input_pattern_count
= 1;
1322 Matching_sections
matching_sections(input_pattern_count
);
1324 // Look through the list of sections for this output section. Add
1325 // each one which matches to one of the elements of
1326 // MATCHING_SECTIONS.
1328 Input_section_list::iterator p
= input_sections
->begin();
1329 while (p
!= input_sections
->end())
1331 Relobj
* relobj
= p
->relobj();
1332 unsigned int shndx
= p
->shndx();
1333 Input_section_info
isi(*p
);
1335 // Calling section_name and section_addralign is not very
1338 // Lock the object so that we can get information about the
1339 // section. This is OK since we know we are single-threaded
1342 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1343 Task_lock_obj
<Object
> tl(task
, relobj
);
1345 isi
.set_section_name(relobj
->section_name(shndx
));
1346 if (p
->is_relaxed_input_section())
1348 // We use current data size because relxed section sizes may not
1349 // have finalized yet.
1350 isi
.set_size(p
->relaxed_input_section()->current_data_size());
1351 isi
.set_addralign(p
->relaxed_input_section()->addralign());
1355 isi
.set_size(relobj
->section_size(shndx
));
1356 isi
.set_addralign(relobj
->section_addralign(shndx
));
1360 if (!this->match_file_name(relobj
->name().c_str()))
1362 else if (this->input_section_patterns_
.empty())
1364 matching_sections
[0].push_back(isi
);
1365 p
= input_sections
->erase(p
);
1370 for (i
= 0; i
< input_pattern_count
; ++i
)
1372 const Input_section_pattern
&
1373 isp(this->input_section_patterns_
[i
]);
1374 if (match(isi
.section_name().c_str(), isp
.pattern
.c_str(),
1375 isp
.pattern_is_wildcard
))
1379 if (i
>= this->input_section_patterns_
.size())
1383 matching_sections
[i
].push_back(isi
);
1384 p
= input_sections
->erase(p
);
1389 // Look through MATCHING_SECTIONS. Sort each one as specified,
1390 // using a stable sort so that we get the default order when
1391 // sections are otherwise equal. Add each input section to the
1394 uint64_t dot
= *dot_value
;
1395 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1397 if (matching_sections
[i
].empty())
1400 gold_assert(output_section
!= NULL
);
1402 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1403 if (isp
.sort
!= SORT_WILDCARD_NONE
1404 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1405 std::stable_sort(matching_sections
[i
].begin(),
1406 matching_sections
[i
].end(),
1407 Input_section_sorter(this->filename_sort_
,
1410 for (std::vector
<Input_section_info
>::const_iterator p
=
1411 matching_sections
[i
].begin();
1412 p
!= matching_sections
[i
].end();
1415 uint64_t this_subalign
= p
->addralign();
1416 if (this_subalign
< subalign
)
1417 this_subalign
= subalign
;
1419 uint64_t address
= align_address(dot
, this_subalign
);
1421 if (address
> dot
&& !fill
->empty())
1423 section_size_type length
=
1424 convert_to_section_size_type(address
- dot
);
1425 std::string this_fill
= this->get_fill_string(fill
, length
);
1426 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1427 output_section
->add_output_section_data(posd
);
1428 layout
->new_output_section_data_from_script(posd
);
1431 output_section
->add_simple_input_section(p
->input_section(),
1435 dot
= address
+ p
->size();
1439 // An SHF_TLS/SHT_NOBITS section does not take up any
1441 if (output_section
== NULL
1442 || (output_section
->flags() & elfcpp::SHF_TLS
) == 0
1443 || output_section
->type() != elfcpp::SHT_NOBITS
)
1446 this->final_dot_value_
= *dot_value
;
1447 this->final_dot_section_
= *dot_section
;
1450 // Print for debugging.
1453 Output_section_element_input::print(FILE* f
) const
1458 fprintf(f
, "KEEP(");
1460 if (!this->filename_pattern_
.empty())
1462 bool need_close_paren
= false;
1463 switch (this->filename_sort_
)
1465 case SORT_WILDCARD_NONE
:
1467 case SORT_WILDCARD_BY_NAME
:
1468 fprintf(f
, "SORT_BY_NAME(");
1469 need_close_paren
= true;
1475 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1477 if (need_close_paren
)
1481 if (!this->input_section_patterns_
.empty()
1482 || !this->filename_exclusions_
.empty())
1486 bool need_space
= false;
1487 if (!this->filename_exclusions_
.empty())
1489 fprintf(f
, "EXCLUDE_FILE(");
1490 bool need_comma
= false;
1491 for (Filename_exclusions::const_iterator p
=
1492 this->filename_exclusions_
.begin();
1493 p
!= this->filename_exclusions_
.end();
1498 fprintf(f
, "%s", p
->first
.c_str());
1505 for (Input_section_patterns::const_iterator p
=
1506 this->input_section_patterns_
.begin();
1507 p
!= this->input_section_patterns_
.end();
1513 int close_parens
= 0;
1516 case SORT_WILDCARD_NONE
:
1518 case SORT_WILDCARD_BY_NAME
:
1519 fprintf(f
, "SORT_BY_NAME(");
1522 case SORT_WILDCARD_BY_ALIGNMENT
:
1523 fprintf(f
, "SORT_BY_ALIGNMENT(");
1526 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1527 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1530 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1531 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1538 fprintf(f
, "%s", p
->pattern
.c_str());
1540 for (int i
= 0; i
< close_parens
; ++i
)
1555 // An output section.
1557 class Output_section_definition
: public Sections_element
1560 typedef Output_section_element::Input_section_list Input_section_list
;
1562 Output_section_definition(const char* name
, size_t namelen
,
1563 const Parser_output_section_header
* header
);
1565 // Finish the output section with the information in the trailer.
1567 finish(const Parser_output_section_trailer
* trailer
);
1569 // Add a symbol to be defined.
1571 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1572 bool provide
, bool hidden
);
1574 // Add an assignment to the special dot symbol.
1576 add_dot_assignment(Expression
* value
);
1578 // Add an assertion.
1580 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1582 // Add a data item to the current output section.
1584 add_data(int size
, bool is_signed
, Expression
* val
);
1586 // Add a setting for the fill value.
1588 add_fill(Expression
* val
);
1590 // Add an input section specification.
1592 add_input_section(const Input_section_spec
* spec
, bool keep
);
1594 // Return whether the output section is relro.
1597 { return this->is_relro_
; }
1599 // Record that the output section is relro.
1602 { this->is_relro_
= true; }
1604 // Create any required output sections.
1606 create_sections(Layout
*);
1608 // Add any symbols being defined to the symbol table.
1610 add_symbols_to_table(Symbol_table
* symtab
);
1612 // Finalize symbols and check assertions.
1614 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1616 // Return the output section name to use for an input file name and
1619 output_section_name(const char* file_name
, const char* section_name
,
1622 // Initialize OSP with an output section.
1624 orphan_section_init(Orphan_section_placement
* osp
,
1625 Script_sections::Elements_iterator p
)
1626 { osp
->output_section_init(this->name_
, this->output_section_
, p
); }
1628 // Set the section address.
1630 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1631 uint64_t* dot_value
, uint64_t* load_address
);
1633 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1634 // this section is constrained, and the input sections do not match,
1635 // return the constraint, and set *POSD.
1637 check_constraint(Output_section_definition
** posd
);
1639 // See if this is the alternate output section for a constrained
1640 // output section. If it is, transfer the Output_section and return
1641 // true. Otherwise return false.
1643 alternate_constraint(Output_section_definition
*, Section_constraint
);
1645 // Get the list of segments to use for an allocated section when
1646 // using a PHDRS clause.
1648 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1650 // Look for an output section by name and return the address, the
1651 // load address, the alignment, and the size. This is used when an
1652 // expression refers to an output section which was not actually
1653 // created. This returns true if the section was found, false
1656 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1659 // Return the associated Output_section if there is one.
1661 get_output_section() const
1662 { return this->output_section_
; }
1664 // Print the contents to the FILE. This is for debugging.
1669 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1671 // The output section name.
1673 // The address. This may be NULL.
1674 Expression
* address_
;
1675 // The load address. This may be NULL.
1676 Expression
* load_address_
;
1677 // The alignment. This may be NULL.
1679 // The input section alignment. This may be NULL.
1680 Expression
* subalign_
;
1681 // The constraint, if any.
1682 Section_constraint constraint_
;
1683 // The fill value. This may be NULL.
1685 // The list of segments this section should go into. This may be
1687 String_list
* phdrs_
;
1688 // The list of elements defining the section.
1689 Output_section_elements elements_
;
1690 // The Output_section created for this definition. This will be
1691 // NULL if none was created.
1692 Output_section
* output_section_
;
1693 // The address after it has been evaluated.
1694 uint64_t evaluated_address_
;
1695 // The load address after it has been evaluated.
1696 uint64_t evaluated_load_address_
;
1697 // The alignment after it has been evaluated.
1698 uint64_t evaluated_addralign_
;
1699 // The output section is relro.
1705 Output_section_definition::Output_section_definition(
1708 const Parser_output_section_header
* header
)
1709 : name_(name
, namelen
),
1710 address_(header
->address
),
1711 load_address_(header
->load_address
),
1712 align_(header
->align
),
1713 subalign_(header
->subalign
),
1714 constraint_(header
->constraint
),
1718 output_section_(NULL
),
1719 evaluated_address_(0),
1720 evaluated_load_address_(0),
1721 evaluated_addralign_(0),
1726 // Finish an output section.
1729 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1731 this->fill_
= trailer
->fill
;
1732 this->phdrs_
= trailer
->phdrs
;
1735 // Add a symbol to be defined.
1738 Output_section_definition::add_symbol_assignment(const char* name
,
1744 Output_section_element
* p
= new Output_section_element_assignment(name
,
1749 this->elements_
.push_back(p
);
1752 // Add an assignment to the special dot symbol.
1755 Output_section_definition::add_dot_assignment(Expression
* value
)
1757 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1758 this->elements_
.push_back(p
);
1761 // Add an assertion.
1764 Output_section_definition::add_assertion(Expression
* check
,
1765 const char* message
,
1768 Output_section_element
* p
= new Output_section_element_assertion(check
,
1771 this->elements_
.push_back(p
);
1774 // Add a data item to the current output section.
1777 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1779 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1781 this->elements_
.push_back(p
);
1784 // Add a setting for the fill value.
1787 Output_section_definition::add_fill(Expression
* val
)
1789 Output_section_element
* p
= new Output_section_element_fill(val
);
1790 this->elements_
.push_back(p
);
1793 // Add an input section specification.
1796 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1799 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1800 this->elements_
.push_back(p
);
1803 // Create any required output sections. We need an output section if
1804 // there is a data statement here.
1807 Output_section_definition::create_sections(Layout
* layout
)
1809 if (this->output_section_
!= NULL
)
1811 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1812 p
!= this->elements_
.end();
1815 if ((*p
)->needs_output_section())
1817 const char* name
= this->name_
.c_str();
1818 this->output_section_
= layout
->make_output_section_for_script(name
);
1824 // Add any symbols being defined to the symbol table.
1827 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1829 for (Output_section_elements::iterator p
= this->elements_
.begin();
1830 p
!= this->elements_
.end();
1832 (*p
)->add_symbols_to_table(symtab
);
1835 // Finalize symbols and check assertions.
1838 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1839 const Layout
* layout
,
1840 uint64_t* dot_value
)
1842 if (this->output_section_
!= NULL
)
1843 *dot_value
= this->output_section_
->address();
1846 uint64_t address
= *dot_value
;
1847 if (this->address_
!= NULL
)
1849 Output_section
* dummy
;
1850 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1854 if (this->align_
!= NULL
)
1856 Output_section
* dummy
;
1857 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
1861 address
= align_address(address
, align
);
1863 *dot_value
= address
;
1866 Output_section
* dot_section
= this->output_section_
;
1867 for (Output_section_elements::iterator p
= this->elements_
.begin();
1868 p
!= this->elements_
.end();
1870 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1873 // Return the output section name to use for an input section name.
1876 Output_section_definition::output_section_name(const char* file_name
,
1877 const char* section_name
,
1878 Output_section
*** slot
)
1880 // Ask each element whether it matches NAME.
1881 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1882 p
!= this->elements_
.end();
1885 if ((*p
)->match_name(file_name
, section_name
))
1887 // We found a match for NAME, which means that it should go
1888 // into this output section.
1889 *slot
= &this->output_section_
;
1890 return this->name_
.c_str();
1894 // We don't know about this section name.
1898 // Set the section address. Note that the OUTPUT_SECTION_ field will
1899 // be NULL if no input sections were mapped to this output section.
1900 // We still have to adjust dot and process symbol assignments.
1903 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1905 uint64_t* dot_value
,
1906 uint64_t* load_address
)
1909 if (this->address_
== NULL
)
1910 address
= *dot_value
;
1913 Output_section
* dummy
;
1914 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1915 *dot_value
, NULL
, &dummy
);
1919 if (this->align_
== NULL
)
1921 if (this->output_section_
== NULL
)
1924 align
= this->output_section_
->addralign();
1928 Output_section
* align_section
;
1929 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1930 NULL
, &align_section
);
1931 if (align_section
!= NULL
)
1932 gold_warning(_("alignment of section %s is not absolute"),
1933 this->name_
.c_str());
1934 if (this->output_section_
!= NULL
)
1935 this->output_section_
->set_addralign(align
);
1938 address
= align_address(address
, align
);
1940 uint64_t start_address
= address
;
1942 *dot_value
= address
;
1944 // The address of non-SHF_ALLOC sections is forced to zero,
1945 // regardless of what the linker script wants.
1946 if (this->output_section_
!= NULL
1947 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1948 this->output_section_
->set_address(address
);
1950 this->evaluated_address_
= address
;
1951 this->evaluated_addralign_
= align
;
1953 if (this->load_address_
== NULL
)
1954 this->evaluated_load_address_
= address
;
1957 Output_section
* dummy
;
1959 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1960 this->output_section_
, &dummy
);
1961 if (this->output_section_
!= NULL
)
1962 this->output_section_
->set_load_address(laddr
);
1963 this->evaluated_load_address_
= laddr
;
1967 if (this->subalign_
== NULL
)
1971 Output_section
* subalign_section
;
1972 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
1975 if (subalign_section
!= NULL
)
1976 gold_warning(_("subalign of section %s is not absolute"),
1977 this->name_
.c_str());
1981 if (this->fill_
!= NULL
)
1983 // FIXME: The GNU linker supports fill values of arbitrary
1985 Output_section
* fill_section
;
1986 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
1990 if (fill_section
!= NULL
)
1991 gold_warning(_("fill of section %s is not absolute"),
1992 this->name_
.c_str());
1993 unsigned char fill_buff
[4];
1994 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1995 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1998 Input_section_list input_sections
;
1999 if (this->output_section_
!= NULL
)
2001 // Get the list of input sections attached to this output
2002 // section. This will leave the output section with only
2003 // Output_section_data entries.
2004 address
+= this->output_section_
->get_input_sections(address
,
2007 *dot_value
= address
;
2010 Output_section
* dot_section
= this->output_section_
;
2011 for (Output_section_elements::iterator p
= this->elements_
.begin();
2012 p
!= this->elements_
.end();
2014 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
2015 subalign
, dot_value
, &dot_section
, &fill
,
2018 gold_assert(input_sections
.empty());
2020 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
2021 *load_address
= *dot_value
;
2023 *load_address
= (this->output_section_
->load_address()
2024 + (*dot_value
- start_address
));
2026 if (this->output_section_
!= NULL
)
2028 if (this->is_relro_
)
2029 this->output_section_
->set_is_relro();
2031 this->output_section_
->clear_is_relro();
2035 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2036 // this section is constrained, and the input sections do not match,
2037 // return the constraint, and set *POSD.
2040 Output_section_definition::check_constraint(Output_section_definition
** posd
)
2042 switch (this->constraint_
)
2044 case CONSTRAINT_NONE
:
2045 return CONSTRAINT_NONE
;
2047 case CONSTRAINT_ONLY_IF_RO
:
2048 if (this->output_section_
!= NULL
2049 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
2052 return CONSTRAINT_ONLY_IF_RO
;
2054 return CONSTRAINT_NONE
;
2056 case CONSTRAINT_ONLY_IF_RW
:
2057 if (this->output_section_
!= NULL
2058 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
2061 return CONSTRAINT_ONLY_IF_RW
;
2063 return CONSTRAINT_NONE
;
2065 case CONSTRAINT_SPECIAL
:
2066 if (this->output_section_
!= NULL
)
2067 gold_error(_("SPECIAL constraints are not implemented"));
2068 return CONSTRAINT_NONE
;
2075 // See if this is the alternate output section for a constrained
2076 // output section. If it is, transfer the Output_section and return
2077 // true. Otherwise return false.
2080 Output_section_definition::alternate_constraint(
2081 Output_section_definition
* posd
,
2082 Section_constraint constraint
)
2084 if (this->name_
!= posd
->name_
)
2089 case CONSTRAINT_ONLY_IF_RO
:
2090 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
2094 case CONSTRAINT_ONLY_IF_RW
:
2095 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
2103 // We have found the alternate constraint. We just need to move
2104 // over the Output_section. When constraints are used properly,
2105 // THIS should not have an output_section pointer, as all the input
2106 // sections should have matched the other definition.
2108 if (this->output_section_
!= NULL
)
2109 gold_error(_("mismatched definition for constrained sections"));
2111 this->output_section_
= posd
->output_section_
;
2112 posd
->output_section_
= NULL
;
2114 if (this->is_relro_
)
2115 this->output_section_
->set_is_relro();
2117 this->output_section_
->clear_is_relro();
2122 // Get the list of segments to use for an allocated section when using
2126 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
2129 if (this->output_section_
== NULL
)
2131 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2134 if (this->phdrs_
!= NULL
)
2135 *phdrs_list
= this->phdrs_
;
2136 return this->output_section_
;
2139 // Look for an output section by name and return the address, the load
2140 // address, the alignment, and the size. This is used when an
2141 // expression refers to an output section which was not actually
2142 // created. This returns true if the section was found, false
2146 Output_section_definition::get_output_section_info(const char* name
,
2148 uint64_t* load_address
,
2149 uint64_t* addralign
,
2150 uint64_t* size
) const
2152 if (this->name_
!= name
)
2155 if (this->output_section_
!= NULL
)
2157 *address
= this->output_section_
->address();
2158 if (this->output_section_
->has_load_address())
2159 *load_address
= this->output_section_
->load_address();
2161 *load_address
= *address
;
2162 *addralign
= this->output_section_
->addralign();
2163 *size
= this->output_section_
->current_data_size();
2167 *address
= this->evaluated_address_
;
2168 *load_address
= this->evaluated_load_address_
;
2169 *addralign
= this->evaluated_addralign_
;
2176 // Print for debugging.
2179 Output_section_definition::print(FILE* f
) const
2181 fprintf(f
, " %s ", this->name_
.c_str());
2183 if (this->address_
!= NULL
)
2185 this->address_
->print(f
);
2191 if (this->load_address_
!= NULL
)
2194 this->load_address_
->print(f
);
2198 if (this->align_
!= NULL
)
2200 fprintf(f
, "ALIGN(");
2201 this->align_
->print(f
);
2205 if (this->subalign_
!= NULL
)
2207 fprintf(f
, "SUBALIGN(");
2208 this->subalign_
->print(f
);
2214 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2215 p
!= this->elements_
.end();
2221 if (this->fill_
!= NULL
)
2224 this->fill_
->print(f
);
2227 if (this->phdrs_
!= NULL
)
2229 for (String_list::const_iterator p
= this->phdrs_
->begin();
2230 p
!= this->phdrs_
->end();
2232 fprintf(f
, " :%s", p
->c_str());
2238 // An output section created to hold orphaned input sections. These
2239 // do not actually appear in linker scripts. However, for convenience
2240 // when setting the output section addresses, we put a marker to these
2241 // sections in the appropriate place in the list of SECTIONS elements.
2243 class Orphan_output_section
: public Sections_element
2246 Orphan_output_section(Output_section
* os
)
2250 // Return whether the orphan output section is relro. We can just
2251 // check the output section because we always set the flag, if
2252 // needed, just after we create the Orphan_output_section.
2255 { return this->os_
->is_relro(); }
2257 // Initialize OSP with an output section. This should have been
2260 orphan_section_init(Orphan_section_placement
*,
2261 Script_sections::Elements_iterator
)
2262 { gold_unreachable(); }
2264 // Set section addresses.
2266 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
2268 // Get the list of segments to use for an allocated section when
2269 // using a PHDRS clause.
2271 allocate_to_segment(String_list
**, bool*);
2273 // Return the associated Output_section.
2275 get_output_section() const
2276 { return this->os_
; }
2278 // Print for debugging.
2280 print(FILE* f
) const
2282 fprintf(f
, " marker for orphaned output section %s\n",
2287 Output_section
* os_
;
2290 // Set section addresses.
2293 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2294 uint64_t* dot_value
,
2295 uint64_t* load_address
)
2297 typedef std::list
<Output_section::Simple_input_section
> Input_section_list
;
2299 bool have_load_address
= *load_address
!= *dot_value
;
2301 uint64_t address
= *dot_value
;
2302 address
= align_address(address
, this->os_
->addralign());
2304 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2306 this->os_
->set_address(address
);
2307 if (have_load_address
)
2308 this->os_
->set_load_address(align_address(*load_address
,
2309 this->os_
->addralign()));
2312 Input_section_list input_sections
;
2313 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2315 for (Input_section_list::iterator p
= input_sections
.begin();
2316 p
!= input_sections
.end();
2322 // We know we are single-threaded, so it is OK to lock the
2325 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2326 Task_lock_obj
<Object
> tl(task
, p
->relobj());
2327 addralign
= p
->relobj()->section_addralign(p
->shndx());
2328 if (p
->is_relaxed_input_section())
2329 // We use current data size because relxed section sizes may not
2330 // have finalized yet.
2331 size
= p
->relaxed_input_section()->current_data_size();
2333 size
= p
->relobj()->section_size(p
->shndx());
2336 address
= align_address(address
, addralign
);
2337 this->os_
->add_simple_input_section(*p
, size
, addralign
);
2341 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2342 if (this->os_
== NULL
2343 || (this->os_
->flags() & elfcpp::SHF_TLS
) == 0
2344 || this->os_
->type() != elfcpp::SHT_NOBITS
)
2346 if (!have_load_address
)
2347 *load_address
= address
;
2349 *load_address
+= address
- *dot_value
;
2351 *dot_value
= address
;
2355 // Get the list of segments to use for an allocated section when using
2356 // a PHDRS clause. If this is an allocated section, return the
2357 // Output_section. We don't change the list of segments.
2360 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2362 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2368 // Class Phdrs_element. A program header from a PHDRS clause.
2373 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2374 bool includes_filehdr
, bool includes_phdrs
,
2375 bool is_flags_valid
, unsigned int flags
,
2376 Expression
* load_address
)
2377 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2378 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2379 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2383 // Return the name of this segment.
2386 { return this->name_
; }
2388 // Return the type of the segment.
2391 { return this->type_
; }
2393 // Whether to include the file header.
2395 includes_filehdr() const
2396 { return this->includes_filehdr_
; }
2398 // Whether to include the program headers.
2400 includes_phdrs() const
2401 { return this->includes_phdrs_
; }
2403 // Return whether there is a load address.
2405 has_load_address() const
2406 { return this->load_address_
!= NULL
; }
2408 // Evaluate the load address expression if there is one.
2410 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2412 if (this->load_address_
!= NULL
)
2413 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2417 // Return the load address.
2419 load_address() const
2421 gold_assert(this->load_address_
!= NULL
);
2422 return this->load_address_value_
;
2425 // Create the segment.
2427 create_segment(Layout
* layout
)
2429 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2430 return this->segment_
;
2433 // Return the segment.
2436 { return this->segment_
; }
2438 // Release the segment.
2441 { this->segment_
= NULL
; }
2443 // Set the segment flags if appropriate.
2445 set_flags_if_valid()
2447 if (this->is_flags_valid_
)
2448 this->segment_
->set_flags(this->flags_
);
2451 // Print for debugging.
2456 // The name used in the script.
2458 // The type of the segment (PT_LOAD, etc.).
2460 // Whether this segment includes the file header.
2461 bool includes_filehdr_
;
2462 // Whether this segment includes the section headers.
2463 bool includes_phdrs_
;
2464 // Whether the flags were explicitly specified.
2465 bool is_flags_valid_
;
2466 // The flags for this segment (PF_R, etc.) if specified.
2467 unsigned int flags_
;
2468 // The expression for the load address for this segment. This may
2470 Expression
* load_address_
;
2471 // The actual load address from evaluating the expression.
2472 uint64_t load_address_value_
;
2473 // The segment itself.
2474 Output_segment
* segment_
;
2477 // Print for debugging.
2480 Phdrs_element::print(FILE* f
) const
2482 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2483 if (this->includes_filehdr_
)
2484 fprintf(f
, " FILEHDR");
2485 if (this->includes_phdrs_
)
2486 fprintf(f
, " PHDRS");
2487 if (this->is_flags_valid_
)
2488 fprintf(f
, " FLAGS(%u)", this->flags_
);
2489 if (this->load_address_
!= NULL
)
2492 this->load_address_
->print(f
);
2498 // Class Script_sections.
2500 Script_sections::Script_sections()
2501 : saw_sections_clause_(false),
2502 in_sections_clause_(false),
2503 sections_elements_(NULL
),
2504 output_section_(NULL
),
2505 phdrs_elements_(NULL
),
2506 orphan_section_placement_(NULL
),
2507 data_segment_align_start_(),
2508 saw_data_segment_align_(false),
2509 saw_relro_end_(false),
2510 saw_segment_start_expression_(false)
2514 // Start a SECTIONS clause.
2517 Script_sections::start_sections()
2519 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2520 this->saw_sections_clause_
= true;
2521 this->in_sections_clause_
= true;
2522 if (this->sections_elements_
== NULL
)
2523 this->sections_elements_
= new Sections_elements
;
2526 // Finish a SECTIONS clause.
2529 Script_sections::finish_sections()
2531 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2532 this->in_sections_clause_
= false;
2535 // Add a symbol to be defined.
2538 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2539 Expression
* val
, bool provide
,
2542 if (this->output_section_
!= NULL
)
2543 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2547 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2550 this->sections_elements_
->push_back(p
);
2554 // Add an assignment to the special dot symbol.
2557 Script_sections::add_dot_assignment(Expression
* val
)
2559 if (this->output_section_
!= NULL
)
2560 this->output_section_
->add_dot_assignment(val
);
2563 // The GNU linker permits assignments to . to appears outside of
2564 // a SECTIONS clause, and treats it as appearing inside, so
2565 // sections_elements_ may be NULL here.
2566 if (this->sections_elements_
== NULL
)
2568 this->sections_elements_
= new Sections_elements
;
2569 this->saw_sections_clause_
= true;
2572 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2573 this->sections_elements_
->push_back(p
);
2577 // Add an assertion.
2580 Script_sections::add_assertion(Expression
* check
, const char* message
,
2583 if (this->output_section_
!= NULL
)
2584 this->output_section_
->add_assertion(check
, message
, messagelen
);
2587 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2589 this->sections_elements_
->push_back(p
);
2593 // Start processing entries for an output section.
2596 Script_sections::start_output_section(
2599 const Parser_output_section_header
*header
)
2601 Output_section_definition
* posd
= new Output_section_definition(name
,
2604 this->sections_elements_
->push_back(posd
);
2605 gold_assert(this->output_section_
== NULL
);
2606 this->output_section_
= posd
;
2609 // Stop processing entries for an output section.
2612 Script_sections::finish_output_section(
2613 const Parser_output_section_trailer
* trailer
)
2615 gold_assert(this->output_section_
!= NULL
);
2616 this->output_section_
->finish(trailer
);
2617 this->output_section_
= NULL
;
2620 // Add a data item to the current output section.
2623 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2625 gold_assert(this->output_section_
!= NULL
);
2626 this->output_section_
->add_data(size
, is_signed
, val
);
2629 // Add a fill value setting to the current output section.
2632 Script_sections::add_fill(Expression
* val
)
2634 gold_assert(this->output_section_
!= NULL
);
2635 this->output_section_
->add_fill(val
);
2638 // Add an input section specification to the current output section.
2641 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2643 gold_assert(this->output_section_
!= NULL
);
2644 this->output_section_
->add_input_section(spec
, keep
);
2647 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
2648 // subsequent output sections may be relro.
2651 Script_sections::data_segment_align()
2653 if (this->saw_data_segment_align_
)
2654 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2655 gold_assert(!this->sections_elements_
->empty());
2656 Sections_elements::iterator p
= this->sections_elements_
->end();
2658 this->data_segment_align_start_
= p
;
2659 this->saw_data_segment_align_
= true;
2662 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
2663 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
2666 Script_sections::data_segment_relro_end()
2668 if (this->saw_relro_end_
)
2669 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2670 "in a linker script"));
2671 this->saw_relro_end_
= true;
2673 if (!this->saw_data_segment_align_
)
2674 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2677 Sections_elements::iterator p
= this->data_segment_align_start_
;
2678 for (++p
; p
!= this->sections_elements_
->end(); ++p
)
2679 (*p
)->set_is_relro();
2683 // Create any required sections.
2686 Script_sections::create_sections(Layout
* layout
)
2688 if (!this->saw_sections_clause_
)
2690 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2691 p
!= this->sections_elements_
->end();
2693 (*p
)->create_sections(layout
);
2696 // Add any symbols we are defining to the symbol table.
2699 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2701 if (!this->saw_sections_clause_
)
2703 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2704 p
!= this->sections_elements_
->end();
2706 (*p
)->add_symbols_to_table(symtab
);
2709 // Finalize symbols and check assertions.
2712 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2714 if (!this->saw_sections_clause_
)
2716 uint64_t dot_value
= 0;
2717 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2718 p
!= this->sections_elements_
->end();
2720 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2723 // Return the name of the output section to use for an input file name
2724 // and section name.
2727 Script_sections::output_section_name(const char* file_name
,
2728 const char* section_name
,
2729 Output_section
*** output_section_slot
)
2731 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2732 p
!= this->sections_elements_
->end();
2735 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2736 output_section_slot
);
2740 // The special name /DISCARD/ means that the input section
2741 // should be discarded.
2742 if (strcmp(ret
, "/DISCARD/") == 0)
2744 *output_section_slot
= NULL
;
2751 // If we couldn't find a mapping for the name, the output section
2752 // gets the name of the input section.
2754 *output_section_slot
= NULL
;
2756 return section_name
;
2759 // Place a marker for an orphan output section into the SECTIONS
2763 Script_sections::place_orphan(Output_section
* os
)
2765 Orphan_section_placement
* osp
= this->orphan_section_placement_
;
2768 // Initialize the Orphan_section_placement structure.
2769 osp
= new Orphan_section_placement();
2770 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2771 p
!= this->sections_elements_
->end();
2773 (*p
)->orphan_section_init(osp
, p
);
2774 gold_assert(!this->sections_elements_
->empty());
2775 Sections_elements::iterator last
= this->sections_elements_
->end();
2777 osp
->last_init(last
);
2778 this->orphan_section_placement_
= osp
;
2781 Orphan_output_section
* orphan
= new Orphan_output_section(os
);
2783 // Look for where to put ORPHAN.
2784 Sections_elements::iterator
* where
;
2785 if (osp
->find_place(os
, &where
))
2787 if ((**where
)->is_relro())
2790 os
->clear_is_relro();
2792 // We want to insert ORPHAN after *WHERE, and then update *WHERE
2793 // so that the next one goes after this one.
2794 Sections_elements::iterator p
= *where
;
2795 gold_assert(p
!= this->sections_elements_
->end());
2797 *where
= this->sections_elements_
->insert(p
, orphan
);
2801 os
->clear_is_relro();
2802 // We don't have a place to put this orphan section. Put it,
2803 // and all other sections like it, at the end, but before the
2804 // sections which always come at the end.
2805 Sections_elements::iterator last
= osp
->last_place();
2806 *where
= this->sections_elements_
->insert(last
, orphan
);
2810 // Set the addresses of all the output sections. Walk through all the
2811 // elements, tracking the dot symbol. Apply assignments which set
2812 // absolute symbol values, in case they are used when setting dot.
2813 // Fill in data statement values. As we find output sections, set the
2814 // address, set the address of all associated input sections, and
2815 // update dot. Return the segment which should hold the file header
2816 // and segment headers, if any.
2819 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2821 gold_assert(this->saw_sections_clause_
);
2823 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2824 // for our representation.
2825 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2826 p
!= this->sections_elements_
->end();
2829 Output_section_definition
* posd
;
2830 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2831 if (failed_constraint
!= CONSTRAINT_NONE
)
2833 Sections_elements::iterator q
;
2834 for (q
= this->sections_elements_
->begin();
2835 q
!= this->sections_elements_
->end();
2840 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2845 if (q
== this->sections_elements_
->end())
2846 gold_error(_("no matching section constraint"));
2850 // Force the alignment of the first TLS section to be the maximum
2851 // alignment of all TLS sections.
2852 Output_section
* first_tls
= NULL
;
2853 uint64_t tls_align
= 0;
2854 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2855 p
!= this->sections_elements_
->end();
2858 Output_section
*os
= (*p
)->get_output_section();
2859 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
2861 if (first_tls
== NULL
)
2863 if (os
->addralign() > tls_align
)
2864 tls_align
= os
->addralign();
2867 if (first_tls
!= NULL
)
2868 first_tls
->set_addralign(tls_align
);
2870 // For a relocatable link, we implicitly set dot to zero.
2871 uint64_t dot_value
= 0;
2872 uint64_t load_address
= 0;
2874 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
2875 // to set section addresses. If the script has any SEGMENT_START
2876 // expression, we do not set the section addresses.
2877 bool use_tsection_options
=
2878 (!this->saw_segment_start_expression_
2879 && (parameters
->options().user_set_Ttext()
2880 || parameters
->options().user_set_Tdata()
2881 || parameters
->options().user_set_Tbss()));
2883 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2884 p
!= this->sections_elements_
->end();
2887 Output_section
* os
= (*p
)->get_output_section();
2889 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
2890 // the special sections by names and doing dot assignments.
2891 if (use_tsection_options
2893 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
2895 uint64_t new_dot_value
= dot_value
;
2897 if (parameters
->options().user_set_Ttext()
2898 && strcmp(os
->name(), ".text") == 0)
2899 new_dot_value
= parameters
->options().Ttext();
2900 else if (parameters
->options().user_set_Tdata()
2901 && strcmp(os
->name(), ".data") == 0)
2902 new_dot_value
= parameters
->options().Tdata();
2903 else if (parameters
->options().user_set_Tbss()
2904 && strcmp(os
->name(), ".bss") == 0)
2905 new_dot_value
= parameters
->options().Tbss();
2907 // Update dot and load address if necessary.
2908 if (new_dot_value
< dot_value
)
2909 gold_error(_("dot may not move backward"));
2910 else if (new_dot_value
!= dot_value
)
2912 dot_value
= new_dot_value
;
2913 load_address
= new_dot_value
;
2917 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
2920 if (this->phdrs_elements_
!= NULL
)
2922 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2923 p
!= this->phdrs_elements_
->end();
2925 (*p
)->eval_load_address(symtab
, layout
);
2928 return this->create_segments(layout
);
2931 // Sort the sections in order to put them into segments.
2933 class Sort_output_sections
2937 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2941 Sort_output_sections::operator()(const Output_section
* os1
,
2942 const Output_section
* os2
) const
2944 // Sort first by the load address.
2945 uint64_t lma1
= (os1
->has_load_address()
2946 ? os1
->load_address()
2948 uint64_t lma2
= (os2
->has_load_address()
2949 ? os2
->load_address()
2954 // Then sort by the virtual address.
2955 if (os1
->address() != os2
->address())
2956 return os1
->address() < os2
->address();
2958 // Sort TLS sections to the end.
2959 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2960 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2964 // Sort PROGBITS before NOBITS.
2965 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2967 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2970 // Otherwise we don't care.
2974 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2975 // We treat a section with the SHF_TLS flag set as taking up space
2976 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2977 // space for them in the file.
2980 Script_sections::is_bss_section(const Output_section
* os
)
2982 return (os
->type() == elfcpp::SHT_NOBITS
2983 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2986 // Return the size taken by the file header and the program headers.
2989 Script_sections::total_header_size(Layout
* layout
) const
2991 size_t segment_count
= layout
->segment_count();
2992 size_t file_header_size
;
2993 size_t segment_headers_size
;
2994 if (parameters
->target().get_size() == 32)
2996 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2997 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2999 else if (parameters
->target().get_size() == 64)
3001 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
3002 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
3007 return file_header_size
+ segment_headers_size
;
3010 // Return the amount we have to subtract from the LMA to accomodate
3011 // headers of the given size. The complication is that the file
3012 // header have to be at the start of a page, as otherwise it will not
3013 // be at the start of the file.
3016 Script_sections::header_size_adjustment(uint64_t lma
,
3017 size_t sizeof_headers
) const
3019 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3020 uint64_t hdr_lma
= lma
- sizeof_headers
;
3021 hdr_lma
&= ~(abi_pagesize
- 1);
3022 return lma
- hdr_lma
;
3025 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3026 // the segment which should hold the file header and segment headers,
3030 Script_sections::create_segments(Layout
* layout
)
3032 gold_assert(this->saw_sections_clause_
);
3034 if (parameters
->options().relocatable())
3037 if (this->saw_phdrs_clause())
3038 return create_segments_from_phdrs_clause(layout
);
3040 Layout::Section_list sections
;
3041 layout
->get_allocated_sections(§ions
);
3043 // Sort the sections by address.
3044 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
3046 this->create_note_and_tls_segments(layout
, §ions
);
3048 // Walk through the sections adding them to PT_LOAD segments.
3049 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3050 Output_segment
* first_seg
= NULL
;
3051 Output_segment
* current_seg
= NULL
;
3052 bool is_current_seg_readonly
= true;
3053 Layout::Section_list::iterator plast
= sections
.end();
3054 uint64_t last_vma
= 0;
3055 uint64_t last_lma
= 0;
3056 uint64_t last_size
= 0;
3057 for (Layout::Section_list::iterator p
= sections
.begin();
3058 p
!= sections
.end();
3061 const uint64_t vma
= (*p
)->address();
3062 const uint64_t lma
= ((*p
)->has_load_address()
3063 ? (*p
)->load_address()
3065 const uint64_t size
= (*p
)->current_data_size();
3067 bool need_new_segment
;
3068 if (current_seg
== NULL
)
3069 need_new_segment
= true;
3070 else if (lma
- vma
!= last_lma
- last_vma
)
3072 // This section has a different LMA relationship than the
3073 // last one; we need a new segment.
3074 need_new_segment
= true;
3076 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
3077 < align_address(lma
, abi_pagesize
))
3079 // Putting this section in the segment would require
3081 need_new_segment
= true;
3083 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
3085 // A non-BSS section can not follow a BSS section in the
3087 need_new_segment
= true;
3089 else if (is_current_seg_readonly
3090 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
3091 && !parameters
->options().omagic())
3093 // Don't put a writable section in the same segment as a
3094 // non-writable section.
3095 need_new_segment
= true;
3099 // Otherwise, reuse the existing segment.
3100 need_new_segment
= false;
3103 elfcpp::Elf_Word seg_flags
=
3104 Layout::section_flags_to_segment((*p
)->flags());
3106 if (need_new_segment
)
3108 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3110 current_seg
->set_addresses(vma
, lma
);
3111 if (first_seg
== NULL
)
3112 first_seg
= current_seg
;
3113 is_current_seg_readonly
= true;
3116 current_seg
->add_output_section(*p
, seg_flags
, false);
3118 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
3119 is_current_seg_readonly
= false;
3127 // An ELF program should work even if the program headers are not in
3128 // a PT_LOAD segment. However, it appears that the Linux kernel
3129 // does not set the AT_PHDR auxiliary entry in that case. It sets
3130 // the load address to p_vaddr - p_offset of the first PT_LOAD
3131 // segment. It then sets AT_PHDR to the load address plus the
3132 // offset to the program headers, e_phoff in the file header. This
3133 // fails when the program headers appear in the file before the
3134 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3135 // segment to hold the file header and the program headers. This is
3136 // effectively what the GNU linker does, and it is slightly more
3137 // efficient in any case. We try to use the first PT_LOAD segment
3138 // if we can, otherwise we make a new one.
3140 if (first_seg
== NULL
)
3143 // -n or -N mean that the program is not demand paged and there is
3144 // no need to put the program headers in a PT_LOAD segment.
3145 if (parameters
->options().nmagic() || parameters
->options().omagic())
3148 size_t sizeof_headers
= this->total_header_size(layout
);
3150 uint64_t vma
= first_seg
->vaddr();
3151 uint64_t lma
= first_seg
->paddr();
3153 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
3155 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
3157 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3161 // If there is no room to squeeze in the headers, then punt. The
3162 // resulting executable probably won't run on GNU/Linux, but we
3163 // trust that the user knows what they are doing.
3164 if (lma
< subtract
|| vma
< subtract
)
3167 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3169 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3174 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3175 // segment if there are any SHT_TLS sections.
3178 Script_sections::create_note_and_tls_segments(
3180 const Layout::Section_list
* sections
)
3182 gold_assert(!this->saw_phdrs_clause());
3184 bool saw_tls
= false;
3185 for (Layout::Section_list::const_iterator p
= sections
->begin();
3186 p
!= sections
->end();
3189 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3191 elfcpp::Elf_Word seg_flags
=
3192 Layout::section_flags_to_segment((*p
)->flags());
3193 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
3195 oseg
->add_output_section(*p
, seg_flags
, false);
3197 // Incorporate any subsequent SHT_NOTE sections, in the
3198 // hopes that the script is sensible.
3199 Layout::Section_list::const_iterator pnext
= p
+ 1;
3200 while (pnext
!= sections
->end()
3201 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
3203 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3204 oseg
->add_output_section(*pnext
, seg_flags
, false);
3210 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3213 gold_error(_("TLS sections are not adjacent"));
3215 elfcpp::Elf_Word seg_flags
=
3216 Layout::section_flags_to_segment((*p
)->flags());
3217 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
3219 oseg
->add_output_section(*p
, seg_flags
, false);
3221 Layout::Section_list::const_iterator pnext
= p
+ 1;
3222 while (pnext
!= sections
->end()
3223 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
3225 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3226 oseg
->add_output_section(*pnext
, seg_flags
, false);
3236 // Add a program header. The PHDRS clause is syntactically distinct
3237 // from the SECTIONS clause, but we implement it with the SECTIONS
3238 // support because PHDRS is useless if there is no SECTIONS clause.
3241 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
3242 bool includes_filehdr
, bool includes_phdrs
,
3243 bool is_flags_valid
, unsigned int flags
,
3244 Expression
* load_address
)
3246 if (this->phdrs_elements_
== NULL
)
3247 this->phdrs_elements_
= new Phdrs_elements();
3248 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
3251 is_flags_valid
, flags
,
3255 // Return the number of segments we expect to create based on the
3256 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3259 Script_sections::expected_segment_count(const Layout
* layout
) const
3261 if (this->saw_phdrs_clause())
3262 return this->phdrs_elements_
->size();
3264 Layout::Section_list sections
;
3265 layout
->get_allocated_sections(§ions
);
3267 // We assume that we will need two PT_LOAD segments.
3270 bool saw_note
= false;
3271 bool saw_tls
= false;
3272 for (Layout::Section_list::const_iterator p
= sections
.begin();
3273 p
!= sections
.end();
3276 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3278 // Assume that all note sections will fit into a single
3286 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3288 // There can only be one PT_TLS segment.
3300 // Create the segments from a PHDRS clause. Return the segment which
3301 // should hold the file header and program headers, if any.
3304 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
3306 this->attach_sections_using_phdrs_clause(layout
);
3307 return this->set_phdrs_clause_addresses(layout
);
3310 // Create the segments from the PHDRS clause, and put the output
3311 // sections in them.
3314 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
3316 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
3317 Name_to_segment name_to_segment
;
3318 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3319 p
!= this->phdrs_elements_
->end();
3321 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
3323 // Walk through the output sections and attach them to segments.
3324 // Output sections in the script which do not list segments are
3325 // attached to the same set of segments as the immediately preceding
3328 String_list
* phdr_names
= NULL
;
3329 bool load_segments_only
= false;
3330 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3331 p
!= this->sections_elements_
->end();
3335 String_list
* old_phdr_names
= phdr_names
;
3336 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
3340 if (phdr_names
== NULL
)
3342 gold_error(_("allocated section not in any segment"));
3346 // We see a list of segments names. Disable PT_LOAD segment only
3348 if (old_phdr_names
!= phdr_names
)
3349 load_segments_only
= false;
3351 // If this is an orphan section--one that was not explicitly
3352 // mentioned in the linker script--then it should not inherit
3353 // any segment type other than PT_LOAD. Otherwise, e.g., the
3354 // PT_INTERP segment will pick up following orphan sections,
3355 // which does not make sense. If this is not an orphan section,
3356 // we trust the linker script.
3359 // Enable PT_LOAD segments only filtering until we see another
3360 // list of segment names.
3361 load_segments_only
= true;
3364 bool in_load_segment
= false;
3365 for (String_list::const_iterator q
= phdr_names
->begin();
3366 q
!= phdr_names
->end();
3369 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3370 if (r
== name_to_segment
.end())
3371 gold_error(_("no segment %s"), q
->c_str());
3374 if (load_segments_only
3375 && r
->second
->type() != elfcpp::PT_LOAD
)
3378 elfcpp::Elf_Word seg_flags
=
3379 Layout::section_flags_to_segment(os
->flags());
3380 r
->second
->add_output_section(os
, seg_flags
, false);
3382 if (r
->second
->type() == elfcpp::PT_LOAD
)
3384 if (in_load_segment
)
3385 gold_error(_("section in two PT_LOAD segments"));
3386 in_load_segment
= true;
3391 if (!in_load_segment
)
3392 gold_error(_("allocated section not in any PT_LOAD segment"));
3396 // Set the addresses for segments created from a PHDRS clause. Return
3397 // the segment which should hold the file header and program headers,
3401 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
3403 Output_segment
* load_seg
= NULL
;
3404 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3405 p
!= this->phdrs_elements_
->end();
3408 // Note that we have to set the flags after adding the output
3409 // sections to the segment, as adding an output segment can
3410 // change the flags.
3411 (*p
)->set_flags_if_valid();
3413 Output_segment
* oseg
= (*p
)->segment();
3415 if (oseg
->type() != elfcpp::PT_LOAD
)
3417 // The addresses of non-PT_LOAD segments are set from the
3418 // PT_LOAD segments.
3419 if ((*p
)->has_load_address())
3420 gold_error(_("may only specify load address for PT_LOAD segment"));
3424 // The output sections should have addresses from the SECTIONS
3425 // clause. The addresses don't have to be in order, so find the
3426 // one with the lowest load address. Use that to set the
3427 // address of the segment.
3429 Output_section
* osec
= oseg
->section_with_lowest_load_address();
3432 oseg
->set_addresses(0, 0);
3436 uint64_t vma
= osec
->address();
3437 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3439 // Override the load address of the section with the load
3440 // address specified for the segment.
3441 if ((*p
)->has_load_address())
3443 if (osec
->has_load_address())
3444 gold_warning(_("PHDRS load address overrides "
3445 "section %s load address"),
3448 lma
= (*p
)->load_address();
3451 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3452 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3454 // We could support this if we wanted to.
3455 gold_error(_("using only one of FILEHDR and PHDRS is "
3456 "not currently supported"));
3460 size_t sizeof_headers
= this->total_header_size(layout
);
3461 uint64_t subtract
= this->header_size_adjustment(lma
,
3463 if (lma
>= subtract
&& vma
>= subtract
)
3470 gold_error(_("sections loaded on first page without room "
3471 "for file and program headers "
3472 "are not supported"));
3475 if (load_seg
!= NULL
)
3476 gold_error(_("using FILEHDR and PHDRS on more than one "
3477 "PT_LOAD segment is not currently supported"));
3481 oseg
->set_addresses(vma
, lma
);
3487 // Add the file header and segment headers to non-load segments
3488 // specified in the PHDRS clause.
3491 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3492 Output_data
* segment_headers
)
3494 gold_assert(this->saw_phdrs_clause());
3495 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3496 p
!= this->phdrs_elements_
->end();
3499 if ((*p
)->type() != elfcpp::PT_LOAD
)
3501 if ((*p
)->includes_phdrs())
3502 (*p
)->segment()->add_initial_output_data(segment_headers
);
3503 if ((*p
)->includes_filehdr())
3504 (*p
)->segment()->add_initial_output_data(file_header
);
3509 // Look for an output section by name and return the address, the load
3510 // address, the alignment, and the size. This is used when an
3511 // expression refers to an output section which was not actually
3512 // created. This returns true if the section was found, false
3516 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3517 uint64_t* load_address
,
3518 uint64_t* addralign
,
3519 uint64_t* size
) const
3521 if (!this->saw_sections_clause_
)
3523 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3524 p
!= this->sections_elements_
->end();
3526 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3532 // Release all Output_segments. This remove all pointers to all
3536 Script_sections::release_segments()
3538 if (this->saw_phdrs_clause())
3540 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3541 p
!= this->phdrs_elements_
->end();
3543 (*p
)->release_segment();
3547 // Print the SECTIONS clause to F for debugging.
3550 Script_sections::print(FILE* f
) const
3552 if (!this->saw_sections_clause_
)
3555 fprintf(f
, "SECTIONS {\n");
3557 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3558 p
!= this->sections_elements_
->end();
3564 if (this->phdrs_elements_
!= NULL
)
3566 fprintf(f
, "PHDRS {\n");
3567 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3568 p
!= this->phdrs_elements_
->end();
3575 } // End namespace gold.