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.
96 // The information we keep for a specific place.
99 // The name of sections for this place.
101 // Whether we have a location for this place.
103 // The iterator for this place.
104 Elements_iterator location
;
107 // Initialize one place element.
109 initialize_place(Place_index
, const char*);
112 Place places_
[PLACE_MAX
];
113 // True if this is the first call to output_section_init.
117 // Initialize Orphan_section_placement.
119 Orphan_section_placement::Orphan_section_placement()
122 this->initialize_place(PLACE_TEXT
, ".text");
123 this->initialize_place(PLACE_RODATA
, ".rodata");
124 this->initialize_place(PLACE_DATA
, ".data");
125 this->initialize_place(PLACE_BSS
, ".bss");
126 this->initialize_place(PLACE_REL
, NULL
);
127 this->initialize_place(PLACE_INTERP
, ".interp");
128 this->initialize_place(PLACE_NONALLOC
, NULL
);
129 this->initialize_place(PLACE_LAST
, NULL
);
132 // Initialize one place element.
135 Orphan_section_placement::initialize_place(Place_index index
, const char* name
)
137 this->places_
[index
].name
= name
;
138 this->places_
[index
].have_location
= false;
141 // While initializing the Orphan_section_placement information, this
142 // is called once for each output section named in the linker script.
143 // If we found an output section during the link, it will be passed in
147 Orphan_section_placement::output_section_init(const std::string
& name
,
149 Elements_iterator location
)
151 bool first_init
= this->first_init_
;
152 this->first_init_
= false;
154 for (int i
= 0; i
< PLACE_MAX
; ++i
)
156 if (this->places_
[i
].name
!= NULL
&& this->places_
[i
].name
== name
)
158 if (this->places_
[i
].have_location
)
160 // We have already seen a section with this name.
164 this->places_
[i
].location
= location
;
165 this->places_
[i
].have_location
= true;
167 // If we just found the .bss section, restart the search for
168 // an unallocated section. This follows the GNU linker's
171 this->places_
[PLACE_NONALLOC
].have_location
= false;
177 // Relocation sections.
178 if (!this->places_
[PLACE_REL
].have_location
180 && (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
181 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
183 this->places_
[PLACE_REL
].location
= location
;
184 this->places_
[PLACE_REL
].have_location
= true;
187 // We find the location for unallocated sections by finding the
188 // first debugging or comment section after the BSS section (if
190 if (!this->places_
[PLACE_NONALLOC
].have_location
191 && (name
== ".comment" || Layout::is_debug_info_section(name
.c_str())))
193 // We add orphan sections after the location in PLACES_. We
194 // want to store unallocated sections before LOCATION. If this
195 // is the very first section, we can't use it.
199 this->places_
[PLACE_NONALLOC
].location
= location
;
200 this->places_
[PLACE_NONALLOC
].have_location
= true;
205 // Initialize the last location.
208 Orphan_section_placement::last_init(Elements_iterator location
)
210 this->places_
[PLACE_LAST
].location
= location
;
211 this->places_
[PLACE_LAST
].have_location
= true;
214 // Set *PWHERE to the address of an iterator pointing to the location
215 // to use for an orphan section. Return true if the iterator has a
216 // value, false otherwise.
219 Orphan_section_placement::find_place(Output_section
* os
,
220 Elements_iterator
** pwhere
)
222 // Figure out where OS should go. This is based on the GNU linker
223 // code. FIXME: The GNU linker handles small data sections
224 // specially, but we don't.
225 elfcpp::Elf_Word type
= os
->type();
226 elfcpp::Elf_Xword flags
= os
->flags();
228 if ((flags
& elfcpp::SHF_ALLOC
) == 0
229 && !Layout::is_debug_info_section(os
->name()))
230 index
= PLACE_NONALLOC
;
231 else if ((flags
& elfcpp::SHF_ALLOC
) == 0)
233 else if (type
== elfcpp::SHT_NOTE
)
234 index
= PLACE_INTERP
;
235 else if (type
== elfcpp::SHT_NOBITS
)
237 else if ((flags
& elfcpp::SHF_WRITE
) != 0)
239 else if (type
== elfcpp::SHT_REL
|| type
== elfcpp::SHT_RELA
)
241 else if ((flags
& elfcpp::SHF_EXECINSTR
) == 0)
242 index
= PLACE_RODATA
;
246 // If we don't have a location yet, try to find one based on a
247 // plausible ordering of sections.
248 if (!this->places_
[index
].have_location
)
269 if (follow
!= PLACE_MAX
&& this->places_
[follow
].have_location
)
271 // Set the location of INDEX to the location of FOLLOW. The
272 // location of INDEX will then be incremented by the caller,
273 // so anything in INDEX will continue to be after anything
275 this->places_
[index
].location
= this->places_
[follow
].location
;
276 this->places_
[index
].have_location
= true;
280 *pwhere
= &this->places_
[index
].location
;
281 bool ret
= this->places_
[index
].have_location
;
283 // The caller will set the location.
284 this->places_
[index
].have_location
= true;
289 // Return the iterator being used for sections at the very end of the
292 Orphan_section_placement::Elements_iterator
293 Orphan_section_placement::last_place() const
295 gold_assert(this->places_
[PLACE_LAST
].have_location
);
296 return this->places_
[PLACE_LAST
].location
;
299 // An element in a SECTIONS clause.
301 class Sections_element
307 virtual ~Sections_element()
310 // Return whether an output section is relro.
315 // Record that an output section is relro.
320 // Create any required output sections. The only real
321 // implementation is in Output_section_definition.
323 create_sections(Layout
*)
326 // Add any symbol being defined to the symbol table.
328 add_symbols_to_table(Symbol_table
*)
331 // Finalize symbols and check assertions.
333 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*)
336 // Return the output section name to use for an input file name and
337 // section name. This only real implementation is in
338 // Output_section_definition.
340 output_section_name(const char*, const char*, Output_section
***)
343 // Initialize OSP with an output section.
345 orphan_section_init(Orphan_section_placement
*,
346 Script_sections::Elements_iterator
)
349 // Set section addresses. This includes applying assignments if the
350 // the expression is an absolute value.
352 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*)
355 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
356 // this section is constrained, and the input sections do not match,
357 // return the constraint, and set *POSD.
358 virtual Section_constraint
359 check_constraint(Output_section_definition
**)
360 { return CONSTRAINT_NONE
; }
362 // See if this is the alternate output section for a constrained
363 // output section. If it is, transfer the Output_section and return
364 // true. Otherwise return false.
366 alternate_constraint(Output_section_definition
*, Section_constraint
)
369 // Get the list of segments to use for an allocated section when
370 // using a PHDRS clause. If this is an allocated section, return
371 // the Output_section, and set *PHDRS_LIST (the first parameter) to
372 // the list of PHDRS to which it should be attached. If the PHDRS
373 // were not specified, don't change *PHDRS_LIST. When not returning
374 // NULL, set *ORPHAN (the second parameter) according to whether
375 // this is an orphan section--one that is not mentioned in the
377 virtual Output_section
*
378 allocate_to_segment(String_list
**, bool*)
381 // Look for an output section by name and return the address, the
382 // load address, the alignment, and the size. This is used when an
383 // expression refers to an output section which was not actually
384 // created. This returns true if the section was found, false
385 // otherwise. The only real definition is for
386 // Output_section_definition.
388 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
392 // Return the associated Output_section if there is one.
393 virtual Output_section
*
394 get_output_section() const
397 // Print the element for debugging purposes.
399 print(FILE* f
) const = 0;
402 // An assignment in a SECTIONS clause outside of an output section.
404 class Sections_element_assignment
: public Sections_element
407 Sections_element_assignment(const char* name
, size_t namelen
,
408 Expression
* val
, bool provide
, bool hidden
)
409 : assignment_(name
, namelen
, val
, provide
, hidden
)
412 // Add the symbol to the symbol table.
414 add_symbols_to_table(Symbol_table
* symtab
)
415 { this->assignment_
.add_to_table(symtab
); }
417 // Finalize the symbol.
419 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
422 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
425 // Set the section address. There is no section here, but if the
426 // value is absolute, we set the symbol. This permits us to use
427 // absolute symbols when setting dot.
429 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
430 uint64_t* dot_value
, uint64_t*)
432 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
435 // Print for debugging.
440 this->assignment_
.print(f
);
444 Symbol_assignment assignment_
;
447 // An assignment to the dot symbol in a SECTIONS clause outside of an
450 class Sections_element_dot_assignment
: public Sections_element
453 Sections_element_dot_assignment(Expression
* val
)
457 // Finalize the symbol.
459 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
462 // We ignore the section of the result because outside of an
463 // output section definition the dot symbol is always considered
465 Output_section
* dummy
;
466 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
470 // Update the dot symbol while setting section addresses.
472 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
473 uint64_t* dot_value
, uint64_t* load_address
)
475 Output_section
* dummy
;
476 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
478 *load_address
= *dot_value
;
481 // Print for debugging.
486 this->val_
->print(f
);
494 // An assertion in a SECTIONS clause outside of an output section.
496 class Sections_element_assertion
: public Sections_element
499 Sections_element_assertion(Expression
* check
, const char* message
,
501 : assertion_(check
, message
, messagelen
)
504 // Check the assertion.
506 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
507 { this->assertion_
.check(symtab
, layout
); }
509 // Print for debugging.
514 this->assertion_
.print(f
);
518 Script_assertion assertion_
;
521 // An element in an output section in a SECTIONS clause.
523 class Output_section_element
526 // A list of input sections.
527 typedef std::list
<Output_section::Simple_input_section
> Input_section_list
;
529 Output_section_element()
532 virtual ~Output_section_element()
535 // Return whether this element requires an output section to exist.
537 needs_output_section() const
540 // Add any symbol being defined to the symbol table.
542 add_symbols_to_table(Symbol_table
*)
545 // Finalize symbols and check assertions.
547 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
550 // Return whether this element matches FILE_NAME and SECTION_NAME.
551 // The only real implementation is in Output_section_element_input.
553 match_name(const char*, const char*) const
556 // Set section addresses. This includes applying assignments if the
557 // the expression is an absolute value.
559 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
560 uint64_t*, Output_section
**, std::string
*,
564 // Print the element for debugging purposes.
566 print(FILE* f
) const = 0;
569 // Return a fill string that is LENGTH bytes long, filling it with
572 get_fill_string(const std::string
* fill
, section_size_type length
) const;
576 Output_section_element::get_fill_string(const std::string
* fill
,
577 section_size_type length
) const
579 std::string this_fill
;
580 this_fill
.reserve(length
);
581 while (this_fill
.length() + fill
->length() <= length
)
583 if (this_fill
.length() < length
)
584 this_fill
.append(*fill
, 0, length
- this_fill
.length());
588 // A symbol assignment in an output section.
590 class Output_section_element_assignment
: public Output_section_element
593 Output_section_element_assignment(const char* name
, size_t namelen
,
594 Expression
* val
, bool provide
,
596 : assignment_(name
, namelen
, val
, provide
, hidden
)
599 // Add the symbol to the symbol table.
601 add_symbols_to_table(Symbol_table
* symtab
)
602 { this->assignment_
.add_to_table(symtab
); }
604 // Finalize the symbol.
606 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
607 uint64_t* dot_value
, Output_section
** dot_section
)
609 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
613 // Set the section address. There is no section here, but if the
614 // value is absolute, we set the symbol. This permits us to use
615 // absolute symbols when setting dot.
617 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
618 uint64_t, uint64_t* dot_value
, Output_section
**,
619 std::string
*, Input_section_list
*)
621 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
624 // Print for debugging.
629 this->assignment_
.print(f
);
633 Symbol_assignment assignment_
;
636 // An assignment to the dot symbol in an output section.
638 class Output_section_element_dot_assignment
: public Output_section_element
641 Output_section_element_dot_assignment(Expression
* val
)
645 // Finalize the symbol.
647 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
648 uint64_t* dot_value
, Output_section
** dot_section
)
650 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
651 *dot_section
, dot_section
);
654 // Update the dot symbol while setting section addresses.
656 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
657 uint64_t, uint64_t* dot_value
, Output_section
**,
658 std::string
*, Input_section_list
*);
660 // Print for debugging.
665 this->val_
->print(f
);
673 // Update the dot symbol while setting section addresses.
676 Output_section_element_dot_assignment::set_section_addresses(
677 Symbol_table
* symtab
,
679 Output_section
* output_section
,
682 Output_section
** dot_section
,
686 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
687 *dot_value
, *dot_section
,
689 if (next_dot
< *dot_value
)
690 gold_error(_("dot may not move backward"));
691 if (next_dot
> *dot_value
&& output_section
!= NULL
)
693 section_size_type length
= convert_to_section_size_type(next_dot
695 Output_section_data
* posd
;
697 posd
= new Output_data_zero_fill(length
, 0);
700 std::string this_fill
= this->get_fill_string(fill
, length
);
701 posd
= new Output_data_const(this_fill
, 0);
703 output_section
->add_output_section_data(posd
);
704 layout
->new_output_section_data_from_script(posd
);
706 *dot_value
= next_dot
;
709 // An assertion in an output section.
711 class Output_section_element_assertion
: public Output_section_element
714 Output_section_element_assertion(Expression
* check
, const char* message
,
716 : assertion_(check
, message
, messagelen
)
723 this->assertion_
.print(f
);
727 Script_assertion assertion_
;
730 // We use a special instance of Output_section_data to handle BYTE,
731 // SHORT, etc. This permits forward references to symbols in the
734 class Output_data_expression
: public Output_section_data
737 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
738 const Symbol_table
* symtab
, const Layout
* layout
,
739 uint64_t dot_value
, Output_section
* dot_section
)
740 : Output_section_data(size
, 0, true),
741 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
742 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
746 // Write the data to the output file.
748 do_write(Output_file
*);
750 // Write the data to a buffer.
752 do_write_to_buffer(unsigned char*);
754 // Write to a map file.
756 do_print_to_mapfile(Mapfile
* mapfile
) const
757 { mapfile
->print_output_data(this, _("** expression")); }
760 template<bool big_endian
>
762 endian_write_to_buffer(uint64_t, unsigned char*);
766 const Symbol_table
* symtab_
;
767 const Layout
* layout_
;
769 Output_section
* dot_section_
;
772 // Write the data element to the output file.
775 Output_data_expression::do_write(Output_file
* of
)
777 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
778 this->write_to_buffer(view
);
779 of
->write_output_view(this->offset(), this->data_size(), view
);
782 // Write the data element to a buffer.
785 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
787 Output_section
* dummy
;
788 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
789 true, this->dot_value_
,
790 this->dot_section_
, &dummy
);
792 if (parameters
->target().is_big_endian())
793 this->endian_write_to_buffer
<true>(val
, buf
);
795 this->endian_write_to_buffer
<false>(val
, buf
);
798 template<bool big_endian
>
800 Output_data_expression::endian_write_to_buffer(uint64_t val
,
803 switch (this->data_size())
806 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
809 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
812 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
815 if (parameters
->target().get_size() == 32)
818 if (this->is_signed_
&& (val
& 0x80000000) != 0)
819 val
|= 0xffffffff00000000LL
;
821 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
828 // A data item in an output section.
830 class Output_section_element_data
: public Output_section_element
833 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
834 : size_(size
), is_signed_(is_signed
), val_(val
)
837 // If there is a data item, then we must create an output section.
839 needs_output_section() const
842 // Finalize symbols--we just need to update dot.
844 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
846 { *dot_value
+= this->size_
; }
848 // Store the value in the section.
850 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
851 uint64_t* dot_value
, Output_section
**, std::string
*,
852 Input_section_list
*);
854 // Print for debugging.
859 // The size in bytes.
861 // Whether the value is signed.
867 // Store the value in the section.
870 Output_section_element_data::set_section_addresses(
871 Symbol_table
* symtab
,
876 Output_section
** dot_section
,
880 gold_assert(os
!= NULL
);
881 Output_data_expression
* expression
=
882 new Output_data_expression(this->size_
, this->is_signed_
, this->val_
,
883 symtab
, layout
, *dot_value
, *dot_section
);
884 os
->add_output_section_data(expression
);
885 layout
->new_output_section_data_from_script(expression
);
886 *dot_value
+= this->size_
;
889 // Print for debugging.
892 Output_section_element_data::print(FILE* f
) const
907 if (this->is_signed_
)
915 fprintf(f
, " %s(", s
);
916 this->val_
->print(f
);
920 // A fill value setting in an output section.
922 class Output_section_element_fill
: public Output_section_element
925 Output_section_element_fill(Expression
* val
)
929 // Update the fill value while setting section addresses.
931 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
932 uint64_t, uint64_t* dot_value
,
933 Output_section
** dot_section
,
934 std::string
* fill
, Input_section_list
*)
936 Output_section
* fill_section
;
937 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
938 *dot_value
, *dot_section
,
940 if (fill_section
!= NULL
)
941 gold_warning(_("fill value is not absolute"));
942 // FIXME: The GNU linker supports fill values of arbitrary length.
943 unsigned char fill_buff
[4];
944 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
945 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
948 // Print for debugging.
952 fprintf(f
, " FILL(");
953 this->val_
->print(f
);
958 // The new fill value.
962 // Return whether STRING contains a wildcard character. This is used
963 // to speed up matching.
966 is_wildcard_string(const std::string
& s
)
968 return strpbrk(s
.c_str(), "?*[") != NULL
;
971 // An input section specification in an output section
973 class Output_section_element_input
: public Output_section_element
976 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
978 // Finalize symbols--just update the value of the dot symbol.
980 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
981 Output_section
** dot_section
)
983 *dot_value
= this->final_dot_value_
;
984 *dot_section
= this->final_dot_section_
;
987 // See whether we match FILE_NAME and SECTION_NAME as an input
990 match_name(const char* file_name
, const char* section_name
) const;
992 // Set the section address.
994 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
995 uint64_t subalign
, uint64_t* dot_value
,
996 Output_section
**, std::string
* fill
,
997 Input_section_list
*);
999 // Print for debugging.
1001 print(FILE* f
) const;
1004 // An input section pattern.
1005 struct Input_section_pattern
1007 std::string pattern
;
1008 bool pattern_is_wildcard
;
1011 Input_section_pattern(const char* patterna
, size_t patternlena
,
1012 Sort_wildcard sorta
)
1013 : pattern(patterna
, patternlena
),
1014 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
1019 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
1021 // Filename_exclusions is a pair of filename pattern and a bool
1022 // indicating whether the filename is a wildcard.
1023 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
1025 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1026 // indicates whether this is a wildcard pattern.
1028 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
1030 return (is_wildcard_pattern
1031 ? fnmatch(pattern
, string
, 0) == 0
1032 : strcmp(string
, pattern
) == 0);
1035 // See if we match a file name.
1037 match_file_name(const char* file_name
) const;
1039 // The file name pattern. If this is the empty string, we match all
1041 std::string filename_pattern_
;
1042 // Whether the file name pattern is a wildcard.
1043 bool filename_is_wildcard_
;
1044 // How the file names should be sorted. This may only be
1045 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1046 Sort_wildcard filename_sort_
;
1047 // The list of file names to exclude.
1048 Filename_exclusions filename_exclusions_
;
1049 // The list of input section patterns.
1050 Input_section_patterns input_section_patterns_
;
1051 // Whether to keep this section when garbage collecting.
1053 // The value of dot after including all matching sections.
1054 uint64_t final_dot_value_
;
1055 // The section where dot is defined after including all matching
1057 Output_section
* final_dot_section_
;
1060 // Construct Output_section_element_input. The parser records strings
1061 // as pointers into a copy of the script file, which will go away when
1062 // parsing is complete. We make sure they are in std::string objects.
1064 Output_section_element_input::Output_section_element_input(
1065 const Input_section_spec
* spec
,
1067 : filename_pattern_(),
1068 filename_is_wildcard_(false),
1069 filename_sort_(spec
->file
.sort
),
1070 filename_exclusions_(),
1071 input_section_patterns_(),
1073 final_dot_value_(0),
1074 final_dot_section_(NULL
)
1076 // The filename pattern "*" is common, and matches all files. Turn
1077 // it into the empty string.
1078 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
1079 this->filename_pattern_
.assign(spec
->file
.name
.value
,
1080 spec
->file
.name
.length
);
1081 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
1083 if (spec
->input_sections
.exclude
!= NULL
)
1085 for (String_list::const_iterator p
=
1086 spec
->input_sections
.exclude
->begin();
1087 p
!= spec
->input_sections
.exclude
->end();
1090 bool is_wildcard
= is_wildcard_string(*p
);
1091 this->filename_exclusions_
.push_back(std::make_pair(*p
,
1096 if (spec
->input_sections
.sections
!= NULL
)
1098 Input_section_patterns
& isp(this->input_section_patterns_
);
1099 for (String_sort_list::const_iterator p
=
1100 spec
->input_sections
.sections
->begin();
1101 p
!= spec
->input_sections
.sections
->end();
1103 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
1108 // See whether we match FILE_NAME.
1111 Output_section_element_input::match_file_name(const char* file_name
) const
1113 if (!this->filename_pattern_
.empty())
1115 // If we were called with no filename, we refuse to match a
1116 // pattern which requires a file name.
1117 if (file_name
== NULL
)
1120 if (!match(file_name
, this->filename_pattern_
.c_str(),
1121 this->filename_is_wildcard_
))
1125 if (file_name
!= NULL
)
1127 // Now we have to see whether FILE_NAME matches one of the
1128 // exclusion patterns, if any.
1129 for (Filename_exclusions::const_iterator p
=
1130 this->filename_exclusions_
.begin();
1131 p
!= this->filename_exclusions_
.end();
1134 if (match(file_name
, p
->first
.c_str(), p
->second
))
1142 // See whether we match FILE_NAME and SECTION_NAME.
1145 Output_section_element_input::match_name(const char* file_name
,
1146 const char* section_name
) const
1148 if (!this->match_file_name(file_name
))
1151 // If there are no section name patterns, then we match.
1152 if (this->input_section_patterns_
.empty())
1155 // See whether we match the section name patterns.
1156 for (Input_section_patterns::const_iterator p
=
1157 this->input_section_patterns_
.begin();
1158 p
!= this->input_section_patterns_
.end();
1161 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
1165 // We didn't match any section names, so we didn't match.
1169 // Information we use to sort the input sections.
1171 class Input_section_info
1174 Input_section_info(const Output_section::Simple_input_section
& input_section
)
1175 : input_section_(input_section
), section_name_(),
1176 size_(0), addralign_(1)
1179 // Return the simple input section.
1180 const Output_section::Simple_input_section
&
1181 input_section() const
1182 { return this->input_section_
; }
1184 // Return the object.
1187 { return this->input_section_
.relobj(); }
1189 // Return the section index.
1192 { return this->input_section_
.shndx(); }
1194 // Return the section name.
1196 section_name() const
1197 { return this->section_name_
; }
1199 // Set the section name.
1201 set_section_name(const std::string name
)
1202 { this->section_name_
= name
; }
1204 // Return the section size.
1207 { return this->size_
; }
1209 // Set the section size.
1211 set_size(uint64_t size
)
1212 { this->size_
= size
; }
1214 // Return the address alignment.
1217 { return this->addralign_
; }
1219 // Set the address alignment.
1221 set_addralign(uint64_t addralign
)
1222 { this->addralign_
= addralign
; }
1225 // Input section, can be a relaxed section.
1226 Output_section::Simple_input_section input_section_
;
1227 // Name of the section.
1228 std::string section_name_
;
1231 // Address alignment.
1232 uint64_t addralign_
;
1235 // A class to sort the input sections.
1237 class Input_section_sorter
1240 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
1241 : filename_sort_(filename_sort
), section_sort_(section_sort
)
1245 operator()(const Input_section_info
&, const Input_section_info
&) const;
1248 Sort_wildcard filename_sort_
;
1249 Sort_wildcard section_sort_
;
1253 Input_section_sorter::operator()(const Input_section_info
& isi1
,
1254 const Input_section_info
& isi2
) const
1256 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
1257 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1258 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1259 && isi1
.addralign() == isi2
.addralign()))
1261 if (isi1
.section_name() != isi2
.section_name())
1262 return isi1
.section_name() < isi2
.section_name();
1264 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
1265 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1266 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
1268 if (isi1
.addralign() != isi2
.addralign())
1269 return isi1
.addralign() < isi2
.addralign();
1271 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
1273 if (isi1
.relobj()->name() != isi2
.relobj()->name())
1274 return (isi1
.relobj()->name() < isi2
.relobj()->name());
1277 // Otherwise we leave them in the same order.
1281 // Set the section address. Look in INPUT_SECTIONS for sections which
1282 // match this spec, sort them as specified, and add them to the output
1286 Output_section_element_input::set_section_addresses(
1289 Output_section
* output_section
,
1291 uint64_t* dot_value
,
1292 Output_section
** dot_section
,
1294 Input_section_list
* input_sections
)
1296 // We build a list of sections which match each
1297 // Input_section_pattern.
1299 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
1300 size_t input_pattern_count
= this->input_section_patterns_
.size();
1301 if (input_pattern_count
== 0)
1302 input_pattern_count
= 1;
1303 Matching_sections
matching_sections(input_pattern_count
);
1305 // Look through the list of sections for this output section. Add
1306 // each one which matches to one of the elements of
1307 // MATCHING_SECTIONS.
1309 Input_section_list::iterator p
= input_sections
->begin();
1310 while (p
!= input_sections
->end())
1312 Relobj
* relobj
= p
->relobj();
1313 unsigned int shndx
= p
->shndx();
1314 Input_section_info
isi(*p
);
1316 // Calling section_name and section_addralign is not very
1319 // Lock the object so that we can get information about the
1320 // section. This is OK since we know we are single-threaded
1323 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1324 Task_lock_obj
<Object
> tl(task
, relobj
);
1326 isi
.set_section_name(relobj
->section_name(shndx
));
1327 if (p
->is_relaxed_input_section())
1328 isi
.set_size(p
->relaxed_input_section()->data_size());
1330 isi
.set_size(relobj
->section_size(shndx
));
1331 isi
.set_addralign(relobj
->section_addralign(shndx
));
1334 if (!this->match_file_name(relobj
->name().c_str()))
1336 else if (this->input_section_patterns_
.empty())
1338 matching_sections
[0].push_back(isi
);
1339 p
= input_sections
->erase(p
);
1344 for (i
= 0; i
< input_pattern_count
; ++i
)
1346 const Input_section_pattern
&
1347 isp(this->input_section_patterns_
[i
]);
1348 if (match(isi
.section_name().c_str(), isp
.pattern
.c_str(),
1349 isp
.pattern_is_wildcard
))
1353 if (i
>= this->input_section_patterns_
.size())
1357 matching_sections
[i
].push_back(isi
);
1358 p
= input_sections
->erase(p
);
1363 // Look through MATCHING_SECTIONS. Sort each one as specified,
1364 // using a stable sort so that we get the default order when
1365 // sections are otherwise equal. Add each input section to the
1368 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1370 if (matching_sections
[i
].empty())
1373 gold_assert(output_section
!= NULL
);
1375 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1376 if (isp
.sort
!= SORT_WILDCARD_NONE
1377 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1378 std::stable_sort(matching_sections
[i
].begin(),
1379 matching_sections
[i
].end(),
1380 Input_section_sorter(this->filename_sort_
,
1383 for (std::vector
<Input_section_info
>::const_iterator p
=
1384 matching_sections
[i
].begin();
1385 p
!= matching_sections
[i
].end();
1388 uint64_t this_subalign
= p
->addralign();
1389 if (this_subalign
< subalign
)
1390 this_subalign
= subalign
;
1392 uint64_t address
= align_address(*dot_value
, this_subalign
);
1394 if (address
> *dot_value
&& !fill
->empty())
1396 section_size_type length
=
1397 convert_to_section_size_type(address
- *dot_value
);
1398 std::string this_fill
= this->get_fill_string(fill
, length
);
1399 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1400 output_section
->add_output_section_data(posd
);
1401 layout
->new_output_section_data_from_script(posd
);
1404 output_section
->add_input_section_for_script(p
->input_section(),
1408 *dot_value
= address
+ p
->size();
1412 this->final_dot_value_
= *dot_value
;
1413 this->final_dot_section_
= *dot_section
;
1416 // Print for debugging.
1419 Output_section_element_input::print(FILE* f
) const
1424 fprintf(f
, "KEEP(");
1426 if (!this->filename_pattern_
.empty())
1428 bool need_close_paren
= false;
1429 switch (this->filename_sort_
)
1431 case SORT_WILDCARD_NONE
:
1433 case SORT_WILDCARD_BY_NAME
:
1434 fprintf(f
, "SORT_BY_NAME(");
1435 need_close_paren
= true;
1441 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1443 if (need_close_paren
)
1447 if (!this->input_section_patterns_
.empty()
1448 || !this->filename_exclusions_
.empty())
1452 bool need_space
= false;
1453 if (!this->filename_exclusions_
.empty())
1455 fprintf(f
, "EXCLUDE_FILE(");
1456 bool need_comma
= false;
1457 for (Filename_exclusions::const_iterator p
=
1458 this->filename_exclusions_
.begin();
1459 p
!= this->filename_exclusions_
.end();
1464 fprintf(f
, "%s", p
->first
.c_str());
1471 for (Input_section_patterns::const_iterator p
=
1472 this->input_section_patterns_
.begin();
1473 p
!= this->input_section_patterns_
.end();
1479 int close_parens
= 0;
1482 case SORT_WILDCARD_NONE
:
1484 case SORT_WILDCARD_BY_NAME
:
1485 fprintf(f
, "SORT_BY_NAME(");
1488 case SORT_WILDCARD_BY_ALIGNMENT
:
1489 fprintf(f
, "SORT_BY_ALIGNMENT(");
1492 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1493 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1496 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1497 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1504 fprintf(f
, "%s", p
->pattern
.c_str());
1506 for (int i
= 0; i
< close_parens
; ++i
)
1521 // An output section.
1523 class Output_section_definition
: public Sections_element
1526 typedef Output_section_element::Input_section_list Input_section_list
;
1528 Output_section_definition(const char* name
, size_t namelen
,
1529 const Parser_output_section_header
* header
);
1531 // Finish the output section with the information in the trailer.
1533 finish(const Parser_output_section_trailer
* trailer
);
1535 // Add a symbol to be defined.
1537 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1538 bool provide
, bool hidden
);
1540 // Add an assignment to the special dot symbol.
1542 add_dot_assignment(Expression
* value
);
1544 // Add an assertion.
1546 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1548 // Add a data item to the current output section.
1550 add_data(int size
, bool is_signed
, Expression
* val
);
1552 // Add a setting for the fill value.
1554 add_fill(Expression
* val
);
1556 // Add an input section specification.
1558 add_input_section(const Input_section_spec
* spec
, bool keep
);
1560 // Return whether the output section is relro.
1563 { return this->is_relro_
; }
1565 // Record that the output section is relro.
1568 { this->is_relro_
= true; }
1570 // Create any required output sections.
1572 create_sections(Layout
*);
1574 // Add any symbols being defined to the symbol table.
1576 add_symbols_to_table(Symbol_table
* symtab
);
1578 // Finalize symbols and check assertions.
1580 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1582 // Return the output section name to use for an input file name and
1585 output_section_name(const char* file_name
, const char* section_name
,
1588 // Initialize OSP with an output section.
1590 orphan_section_init(Orphan_section_placement
* osp
,
1591 Script_sections::Elements_iterator p
)
1592 { osp
->output_section_init(this->name_
, this->output_section_
, p
); }
1594 // Set the section address.
1596 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1597 uint64_t* dot_value
, uint64_t* load_address
);
1599 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1600 // this section is constrained, and the input sections do not match,
1601 // return the constraint, and set *POSD.
1603 check_constraint(Output_section_definition
** posd
);
1605 // See if this is the alternate output section for a constrained
1606 // output section. If it is, transfer the Output_section and return
1607 // true. Otherwise return false.
1609 alternate_constraint(Output_section_definition
*, Section_constraint
);
1611 // Get the list of segments to use for an allocated section when
1612 // using a PHDRS clause.
1614 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1616 // Look for an output section by name and return the address, the
1617 // load address, the alignment, and the size. This is used when an
1618 // expression refers to an output section which was not actually
1619 // created. This returns true if the section was found, false
1622 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1625 // Return the associated Output_section if there is one.
1627 get_output_section() const
1628 { return this->output_section_
; }
1630 // Print the contents to the FILE. This is for debugging.
1635 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1637 // The output section name.
1639 // The address. This may be NULL.
1640 Expression
* address_
;
1641 // The load address. This may be NULL.
1642 Expression
* load_address_
;
1643 // The alignment. This may be NULL.
1645 // The input section alignment. This may be NULL.
1646 Expression
* subalign_
;
1647 // The constraint, if any.
1648 Section_constraint constraint_
;
1649 // The fill value. This may be NULL.
1651 // The list of segments this section should go into. This may be
1653 String_list
* phdrs_
;
1654 // The list of elements defining the section.
1655 Output_section_elements elements_
;
1656 // The Output_section created for this definition. This will be
1657 // NULL if none was created.
1658 Output_section
* output_section_
;
1659 // The address after it has been evaluated.
1660 uint64_t evaluated_address_
;
1661 // The load address after it has been evaluated.
1662 uint64_t evaluated_load_address_
;
1663 // The alignment after it has been evaluated.
1664 uint64_t evaluated_addralign_
;
1665 // The output section is relro.
1671 Output_section_definition::Output_section_definition(
1674 const Parser_output_section_header
* header
)
1675 : name_(name
, namelen
),
1676 address_(header
->address
),
1677 load_address_(header
->load_address
),
1678 align_(header
->align
),
1679 subalign_(header
->subalign
),
1680 constraint_(header
->constraint
),
1684 output_section_(NULL
),
1685 evaluated_address_(0),
1686 evaluated_load_address_(0),
1687 evaluated_addralign_(0),
1692 // Finish an output section.
1695 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1697 this->fill_
= trailer
->fill
;
1698 this->phdrs_
= trailer
->phdrs
;
1701 // Add a symbol to be defined.
1704 Output_section_definition::add_symbol_assignment(const char* name
,
1710 Output_section_element
* p
= new Output_section_element_assignment(name
,
1715 this->elements_
.push_back(p
);
1718 // Add an assignment to the special dot symbol.
1721 Output_section_definition::add_dot_assignment(Expression
* value
)
1723 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1724 this->elements_
.push_back(p
);
1727 // Add an assertion.
1730 Output_section_definition::add_assertion(Expression
* check
,
1731 const char* message
,
1734 Output_section_element
* p
= new Output_section_element_assertion(check
,
1737 this->elements_
.push_back(p
);
1740 // Add a data item to the current output section.
1743 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1745 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1747 this->elements_
.push_back(p
);
1750 // Add a setting for the fill value.
1753 Output_section_definition::add_fill(Expression
* val
)
1755 Output_section_element
* p
= new Output_section_element_fill(val
);
1756 this->elements_
.push_back(p
);
1759 // Add an input section specification.
1762 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1765 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1766 this->elements_
.push_back(p
);
1769 // Create any required output sections. We need an output section if
1770 // there is a data statement here.
1773 Output_section_definition::create_sections(Layout
* layout
)
1775 if (this->output_section_
!= NULL
)
1777 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1778 p
!= this->elements_
.end();
1781 if ((*p
)->needs_output_section())
1783 const char* name
= this->name_
.c_str();
1784 this->output_section_
= layout
->make_output_section_for_script(name
);
1790 // Add any symbols being defined to the symbol table.
1793 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1795 for (Output_section_elements::iterator p
= this->elements_
.begin();
1796 p
!= this->elements_
.end();
1798 (*p
)->add_symbols_to_table(symtab
);
1801 // Finalize symbols and check assertions.
1804 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1805 const Layout
* layout
,
1806 uint64_t* dot_value
)
1808 if (this->output_section_
!= NULL
)
1809 *dot_value
= this->output_section_
->address();
1812 uint64_t address
= *dot_value
;
1813 if (this->address_
!= NULL
)
1815 Output_section
* dummy
;
1816 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1820 if (this->align_
!= NULL
)
1822 Output_section
* dummy
;
1823 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
1827 address
= align_address(address
, align
);
1829 *dot_value
= address
;
1832 Output_section
* dot_section
= this->output_section_
;
1833 for (Output_section_elements::iterator p
= this->elements_
.begin();
1834 p
!= this->elements_
.end();
1836 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1839 // Return the output section name to use for an input section name.
1842 Output_section_definition::output_section_name(const char* file_name
,
1843 const char* section_name
,
1844 Output_section
*** slot
)
1846 // Ask each element whether it matches NAME.
1847 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1848 p
!= this->elements_
.end();
1851 if ((*p
)->match_name(file_name
, section_name
))
1853 // We found a match for NAME, which means that it should go
1854 // into this output section.
1855 *slot
= &this->output_section_
;
1856 return this->name_
.c_str();
1860 // We don't know about this section name.
1864 // Set the section address. Note that the OUTPUT_SECTION_ field will
1865 // be NULL if no input sections were mapped to this output section.
1866 // We still have to adjust dot and process symbol assignments.
1869 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1871 uint64_t* dot_value
,
1872 uint64_t* load_address
)
1875 if (this->address_
== NULL
)
1876 address
= *dot_value
;
1879 Output_section
* dummy
;
1880 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1881 *dot_value
, NULL
, &dummy
);
1885 if (this->align_
== NULL
)
1887 if (this->output_section_
== NULL
)
1890 align
= this->output_section_
->addralign();
1894 Output_section
* align_section
;
1895 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1896 NULL
, &align_section
);
1897 if (align_section
!= NULL
)
1898 gold_warning(_("alignment of section %s is not absolute"),
1899 this->name_
.c_str());
1900 if (this->output_section_
!= NULL
)
1901 this->output_section_
->set_addralign(align
);
1904 address
= align_address(address
, align
);
1906 uint64_t start_address
= address
;
1908 *dot_value
= address
;
1910 // The address of non-SHF_ALLOC sections is forced to zero,
1911 // regardless of what the linker script wants.
1912 if (this->output_section_
!= NULL
1913 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1914 this->output_section_
->set_address(address
);
1916 this->evaluated_address_
= address
;
1917 this->evaluated_addralign_
= align
;
1919 if (this->load_address_
== NULL
)
1920 this->evaluated_load_address_
= address
;
1923 Output_section
* dummy
;
1925 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1926 this->output_section_
, &dummy
);
1927 if (this->output_section_
!= NULL
)
1928 this->output_section_
->set_load_address(laddr
);
1929 this->evaluated_load_address_
= laddr
;
1933 if (this->subalign_
== NULL
)
1937 Output_section
* subalign_section
;
1938 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
1941 if (subalign_section
!= NULL
)
1942 gold_warning(_("subalign of section %s is not absolute"),
1943 this->name_
.c_str());
1947 if (this->fill_
!= NULL
)
1949 // FIXME: The GNU linker supports fill values of arbitrary
1951 Output_section
* fill_section
;
1952 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
1956 if (fill_section
!= NULL
)
1957 gold_warning(_("fill of section %s is not absolute"),
1958 this->name_
.c_str());
1959 unsigned char fill_buff
[4];
1960 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1961 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1964 Input_section_list input_sections
;
1965 if (this->output_section_
!= NULL
)
1967 // Get the list of input sections attached to this output
1968 // section. This will leave the output section with only
1969 // Output_section_data entries.
1970 address
+= this->output_section_
->get_input_sections(address
,
1973 *dot_value
= address
;
1976 Output_section
* dot_section
= this->output_section_
;
1977 for (Output_section_elements::iterator p
= this->elements_
.begin();
1978 p
!= this->elements_
.end();
1980 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
1981 subalign
, dot_value
, &dot_section
, &fill
,
1984 gold_assert(input_sections
.empty());
1986 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
1987 *load_address
= *dot_value
;
1989 *load_address
= (this->output_section_
->load_address()
1990 + (*dot_value
- start_address
));
1992 if (this->output_section_
!= NULL
)
1994 if (this->is_relro_
)
1995 this->output_section_
->set_is_relro();
1997 this->output_section_
->clear_is_relro();
2001 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2002 // this section is constrained, and the input sections do not match,
2003 // return the constraint, and set *POSD.
2006 Output_section_definition::check_constraint(Output_section_definition
** posd
)
2008 switch (this->constraint_
)
2010 case CONSTRAINT_NONE
:
2011 return CONSTRAINT_NONE
;
2013 case CONSTRAINT_ONLY_IF_RO
:
2014 if (this->output_section_
!= NULL
2015 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
2018 return CONSTRAINT_ONLY_IF_RO
;
2020 return CONSTRAINT_NONE
;
2022 case CONSTRAINT_ONLY_IF_RW
:
2023 if (this->output_section_
!= NULL
2024 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
2027 return CONSTRAINT_ONLY_IF_RW
;
2029 return CONSTRAINT_NONE
;
2031 case CONSTRAINT_SPECIAL
:
2032 if (this->output_section_
!= NULL
)
2033 gold_error(_("SPECIAL constraints are not implemented"));
2034 return CONSTRAINT_NONE
;
2041 // See if this is the alternate output section for a constrained
2042 // output section. If it is, transfer the Output_section and return
2043 // true. Otherwise return false.
2046 Output_section_definition::alternate_constraint(
2047 Output_section_definition
* posd
,
2048 Section_constraint constraint
)
2050 if (this->name_
!= posd
->name_
)
2055 case CONSTRAINT_ONLY_IF_RO
:
2056 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
2060 case CONSTRAINT_ONLY_IF_RW
:
2061 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
2069 // We have found the alternate constraint. We just need to move
2070 // over the Output_section. When constraints are used properly,
2071 // THIS should not have an output_section pointer, as all the input
2072 // sections should have matched the other definition.
2074 if (this->output_section_
!= NULL
)
2075 gold_error(_("mismatched definition for constrained sections"));
2077 this->output_section_
= posd
->output_section_
;
2078 posd
->output_section_
= NULL
;
2080 if (this->is_relro_
)
2081 this->output_section_
->set_is_relro();
2083 this->output_section_
->clear_is_relro();
2088 // Get the list of segments to use for an allocated section when using
2092 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
2095 if (this->output_section_
== NULL
)
2097 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2100 if (this->phdrs_
!= NULL
)
2101 *phdrs_list
= this->phdrs_
;
2102 return this->output_section_
;
2105 // Look for an output section by name and return the address, the load
2106 // address, the alignment, and the size. This is used when an
2107 // expression refers to an output section which was not actually
2108 // created. This returns true if the section was found, false
2112 Output_section_definition::get_output_section_info(const char* name
,
2114 uint64_t* load_address
,
2115 uint64_t* addralign
,
2116 uint64_t* size
) const
2118 if (this->name_
!= name
)
2121 if (this->output_section_
!= NULL
)
2123 *address
= this->output_section_
->address();
2124 if (this->output_section_
->has_load_address())
2125 *load_address
= this->output_section_
->load_address();
2127 *load_address
= *address
;
2128 *addralign
= this->output_section_
->addralign();
2129 *size
= this->output_section_
->current_data_size();
2133 *address
= this->evaluated_address_
;
2134 *load_address
= this->evaluated_load_address_
;
2135 *addralign
= this->evaluated_addralign_
;
2142 // Print for debugging.
2145 Output_section_definition::print(FILE* f
) const
2147 fprintf(f
, " %s ", this->name_
.c_str());
2149 if (this->address_
!= NULL
)
2151 this->address_
->print(f
);
2157 if (this->load_address_
!= NULL
)
2160 this->load_address_
->print(f
);
2164 if (this->align_
!= NULL
)
2166 fprintf(f
, "ALIGN(");
2167 this->align_
->print(f
);
2171 if (this->subalign_
!= NULL
)
2173 fprintf(f
, "SUBALIGN(");
2174 this->subalign_
->print(f
);
2180 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2181 p
!= this->elements_
.end();
2187 if (this->fill_
!= NULL
)
2190 this->fill_
->print(f
);
2193 if (this->phdrs_
!= NULL
)
2195 for (String_list::const_iterator p
= this->phdrs_
->begin();
2196 p
!= this->phdrs_
->end();
2198 fprintf(f
, " :%s", p
->c_str());
2204 // An output section created to hold orphaned input sections. These
2205 // do not actually appear in linker scripts. However, for convenience
2206 // when setting the output section addresses, we put a marker to these
2207 // sections in the appropriate place in the list of SECTIONS elements.
2209 class Orphan_output_section
: public Sections_element
2212 Orphan_output_section(Output_section
* os
)
2216 // Return whether the orphan output section is relro. We can just
2217 // check the output section because we always set the flag, if
2218 // needed, just after we create the Orphan_output_section.
2221 { return this->os_
->is_relro(); }
2223 // Initialize OSP with an output section. This should have been
2226 orphan_section_init(Orphan_section_placement
*,
2227 Script_sections::Elements_iterator
)
2228 { gold_unreachable(); }
2230 // Set section addresses.
2232 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
2234 // Get the list of segments to use for an allocated section when
2235 // using a PHDRS clause.
2237 allocate_to_segment(String_list
**, bool*);
2239 // Return the associated Output_section.
2241 get_output_section() const
2242 { return this->os_
; }
2244 // Print for debugging.
2246 print(FILE* f
) const
2248 fprintf(f
, " marker for orphaned output section %s\n",
2253 Output_section
* os_
;
2256 // Set section addresses.
2259 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2260 uint64_t* dot_value
,
2261 uint64_t* load_address
)
2263 typedef std::list
<Output_section::Simple_input_section
> Input_section_list
;
2265 bool have_load_address
= *load_address
!= *dot_value
;
2267 uint64_t address
= *dot_value
;
2268 address
= align_address(address
, this->os_
->addralign());
2270 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2272 this->os_
->set_address(address
);
2273 if (have_load_address
)
2274 this->os_
->set_load_address(align_address(*load_address
,
2275 this->os_
->addralign()));
2278 Input_section_list input_sections
;
2279 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2281 for (Input_section_list::iterator p
= input_sections
.begin();
2282 p
!= input_sections
.end();
2288 // We know what are single-threaded, so it is OK to lock the
2291 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2292 Task_lock_obj
<Object
> tl(task
, p
->relobj());
2293 addralign
= p
->relobj()->section_addralign(p
->shndx());
2294 if (p
->is_relaxed_input_section())
2295 size
= p
->relaxed_input_section()->data_size();
2297 size
= p
->relobj()->section_size(p
->shndx());
2300 address
= align_address(address
, addralign
);
2301 this->os_
->add_input_section_for_script(*p
, size
, addralign
);
2305 if (!have_load_address
)
2306 *load_address
= address
;
2308 *load_address
+= address
- *dot_value
;
2310 *dot_value
= address
;
2313 // Get the list of segments to use for an allocated section when using
2314 // a PHDRS clause. If this is an allocated section, return the
2315 // Output_section. We don't change the list of segments.
2318 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2320 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2326 // Class Phdrs_element. A program header from a PHDRS clause.
2331 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2332 bool includes_filehdr
, bool includes_phdrs
,
2333 bool is_flags_valid
, unsigned int flags
,
2334 Expression
* load_address
)
2335 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2336 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2337 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2341 // Return the name of this segment.
2344 { return this->name_
; }
2346 // Return the type of the segment.
2349 { return this->type_
; }
2351 // Whether to include the file header.
2353 includes_filehdr() const
2354 { return this->includes_filehdr_
; }
2356 // Whether to include the program headers.
2358 includes_phdrs() const
2359 { return this->includes_phdrs_
; }
2361 // Return whether there is a load address.
2363 has_load_address() const
2364 { return this->load_address_
!= NULL
; }
2366 // Evaluate the load address expression if there is one.
2368 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2370 if (this->load_address_
!= NULL
)
2371 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2375 // Return the load address.
2377 load_address() const
2379 gold_assert(this->load_address_
!= NULL
);
2380 return this->load_address_value_
;
2383 // Create the segment.
2385 create_segment(Layout
* layout
)
2387 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2388 return this->segment_
;
2391 // Return the segment.
2394 { return this->segment_
; }
2396 // Release the segment.
2399 { this->segment_
= NULL
; }
2401 // Set the segment flags if appropriate.
2403 set_flags_if_valid()
2405 if (this->is_flags_valid_
)
2406 this->segment_
->set_flags(this->flags_
);
2409 // Print for debugging.
2414 // The name used in the script.
2416 // The type of the segment (PT_LOAD, etc.).
2418 // Whether this segment includes the file header.
2419 bool includes_filehdr_
;
2420 // Whether this segment includes the section headers.
2421 bool includes_phdrs_
;
2422 // Whether the flags were explicitly specified.
2423 bool is_flags_valid_
;
2424 // The flags for this segment (PF_R, etc.) if specified.
2425 unsigned int flags_
;
2426 // The expression for the load address for this segment. This may
2428 Expression
* load_address_
;
2429 // The actual load address from evaluating the expression.
2430 uint64_t load_address_value_
;
2431 // The segment itself.
2432 Output_segment
* segment_
;
2435 // Print for debugging.
2438 Phdrs_element::print(FILE* f
) const
2440 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2441 if (this->includes_filehdr_
)
2442 fprintf(f
, " FILEHDR");
2443 if (this->includes_phdrs_
)
2444 fprintf(f
, " PHDRS");
2445 if (this->is_flags_valid_
)
2446 fprintf(f
, " FLAGS(%u)", this->flags_
);
2447 if (this->load_address_
!= NULL
)
2450 this->load_address_
->print(f
);
2456 // Class Script_sections.
2458 Script_sections::Script_sections()
2459 : saw_sections_clause_(false),
2460 in_sections_clause_(false),
2461 sections_elements_(NULL
),
2462 output_section_(NULL
),
2463 phdrs_elements_(NULL
),
2464 orphan_section_placement_(NULL
),
2465 data_segment_align_start_(),
2466 saw_data_segment_align_(false),
2467 saw_relro_end_(false)
2471 // Start a SECTIONS clause.
2474 Script_sections::start_sections()
2476 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2477 this->saw_sections_clause_
= true;
2478 this->in_sections_clause_
= true;
2479 if (this->sections_elements_
== NULL
)
2480 this->sections_elements_
= new Sections_elements
;
2483 // Finish a SECTIONS clause.
2486 Script_sections::finish_sections()
2488 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2489 this->in_sections_clause_
= false;
2492 // Add a symbol to be defined.
2495 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2496 Expression
* val
, bool provide
,
2499 if (this->output_section_
!= NULL
)
2500 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2504 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2507 this->sections_elements_
->push_back(p
);
2511 // Add an assignment to the special dot symbol.
2514 Script_sections::add_dot_assignment(Expression
* val
)
2516 if (this->output_section_
!= NULL
)
2517 this->output_section_
->add_dot_assignment(val
);
2520 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2521 this->sections_elements_
->push_back(p
);
2525 // Add an assertion.
2528 Script_sections::add_assertion(Expression
* check
, const char* message
,
2531 if (this->output_section_
!= NULL
)
2532 this->output_section_
->add_assertion(check
, message
, messagelen
);
2535 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2537 this->sections_elements_
->push_back(p
);
2541 // Start processing entries for an output section.
2544 Script_sections::start_output_section(
2547 const Parser_output_section_header
*header
)
2549 Output_section_definition
* posd
= new Output_section_definition(name
,
2552 this->sections_elements_
->push_back(posd
);
2553 gold_assert(this->output_section_
== NULL
);
2554 this->output_section_
= posd
;
2557 // Stop processing entries for an output section.
2560 Script_sections::finish_output_section(
2561 const Parser_output_section_trailer
* trailer
)
2563 gold_assert(this->output_section_
!= NULL
);
2564 this->output_section_
->finish(trailer
);
2565 this->output_section_
= NULL
;
2568 // Add a data item to the current output section.
2571 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2573 gold_assert(this->output_section_
!= NULL
);
2574 this->output_section_
->add_data(size
, is_signed
, val
);
2577 // Add a fill value setting to the current output section.
2580 Script_sections::add_fill(Expression
* val
)
2582 gold_assert(this->output_section_
!= NULL
);
2583 this->output_section_
->add_fill(val
);
2586 // Add an input section specification to the current output section.
2589 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2591 gold_assert(this->output_section_
!= NULL
);
2592 this->output_section_
->add_input_section(spec
, keep
);
2595 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
2596 // subsequent output sections may be relro.
2599 Script_sections::data_segment_align()
2601 if (this->saw_data_segment_align_
)
2602 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2603 gold_assert(!this->sections_elements_
->empty());
2604 Sections_elements::iterator p
= this->sections_elements_
->end();
2606 this->data_segment_align_start_
= p
;
2607 this->saw_data_segment_align_
= true;
2610 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
2611 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
2614 Script_sections::data_segment_relro_end()
2616 if (this->saw_relro_end_
)
2617 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2618 "in a linker script"));
2619 this->saw_relro_end_
= true;
2621 if (!this->saw_data_segment_align_
)
2622 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2625 Sections_elements::iterator p
= this->data_segment_align_start_
;
2626 for (++p
; p
!= this->sections_elements_
->end(); ++p
)
2627 (*p
)->set_is_relro();
2631 // Create any required sections.
2634 Script_sections::create_sections(Layout
* layout
)
2636 if (!this->saw_sections_clause_
)
2638 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2639 p
!= this->sections_elements_
->end();
2641 (*p
)->create_sections(layout
);
2644 // Add any symbols we are defining to the symbol table.
2647 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2649 if (!this->saw_sections_clause_
)
2651 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2652 p
!= this->sections_elements_
->end();
2654 (*p
)->add_symbols_to_table(symtab
);
2657 // Finalize symbols and check assertions.
2660 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2662 if (!this->saw_sections_clause_
)
2664 uint64_t dot_value
= 0;
2665 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2666 p
!= this->sections_elements_
->end();
2668 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2671 // Return the name of the output section to use for an input file name
2672 // and section name.
2675 Script_sections::output_section_name(const char* file_name
,
2676 const char* section_name
,
2677 Output_section
*** output_section_slot
)
2679 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2680 p
!= this->sections_elements_
->end();
2683 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2684 output_section_slot
);
2688 // The special name /DISCARD/ means that the input section
2689 // should be discarded.
2690 if (strcmp(ret
, "/DISCARD/") == 0)
2692 *output_section_slot
= NULL
;
2699 // If we couldn't find a mapping for the name, the output section
2700 // gets the name of the input section.
2702 *output_section_slot
= NULL
;
2704 return section_name
;
2707 // Place a marker for an orphan output section into the SECTIONS
2711 Script_sections::place_orphan(Output_section
* os
)
2713 Orphan_section_placement
* osp
= this->orphan_section_placement_
;
2716 // Initialize the Orphan_section_placement structure.
2717 osp
= new Orphan_section_placement();
2718 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2719 p
!= this->sections_elements_
->end();
2721 (*p
)->orphan_section_init(osp
, p
);
2722 gold_assert(!this->sections_elements_
->empty());
2723 Sections_elements::iterator last
= this->sections_elements_
->end();
2725 osp
->last_init(last
);
2726 this->orphan_section_placement_
= osp
;
2729 Orphan_output_section
* orphan
= new Orphan_output_section(os
);
2731 // Look for where to put ORPHAN.
2732 Sections_elements::iterator
* where
;
2733 if (osp
->find_place(os
, &where
))
2735 if ((**where
)->is_relro())
2738 os
->clear_is_relro();
2740 // We want to insert ORPHAN after *WHERE, and then update *WHERE
2741 // so that the next one goes after this one.
2742 Sections_elements::iterator p
= *where
;
2743 gold_assert(p
!= this->sections_elements_
->end());
2745 *where
= this->sections_elements_
->insert(p
, orphan
);
2749 os
->clear_is_relro();
2750 // We don't have a place to put this orphan section. Put it,
2751 // and all other sections like it, at the end, but before the
2752 // sections which always come at the end.
2753 Sections_elements::iterator last
= osp
->last_place();
2754 *where
= this->sections_elements_
->insert(last
, orphan
);
2758 // Set the addresses of all the output sections. Walk through all the
2759 // elements, tracking the dot symbol. Apply assignments which set
2760 // absolute symbol values, in case they are used when setting dot.
2761 // Fill in data statement values. As we find output sections, set the
2762 // address, set the address of all associated input sections, and
2763 // update dot. Return the segment which should hold the file header
2764 // and segment headers, if any.
2767 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2769 gold_assert(this->saw_sections_clause_
);
2771 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2772 // for our representation.
2773 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2774 p
!= this->sections_elements_
->end();
2777 Output_section_definition
* posd
;
2778 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2779 if (failed_constraint
!= CONSTRAINT_NONE
)
2781 Sections_elements::iterator q
;
2782 for (q
= this->sections_elements_
->begin();
2783 q
!= this->sections_elements_
->end();
2788 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2793 if (q
== this->sections_elements_
->end())
2794 gold_error(_("no matching section constraint"));
2798 // Force the alignment of the first TLS section to be the maximum
2799 // alignment of all TLS sections.
2800 Output_section
* first_tls
= NULL
;
2801 uint64_t tls_align
= 0;
2802 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2803 p
!= this->sections_elements_
->end();
2806 Output_section
*os
= (*p
)->get_output_section();
2807 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
2809 if (first_tls
== NULL
)
2811 if (os
->addralign() > tls_align
)
2812 tls_align
= os
->addralign();
2815 if (first_tls
!= NULL
)
2816 first_tls
->set_addralign(tls_align
);
2818 // For a relocatable link, we implicitly set dot to zero.
2819 uint64_t dot_value
= 0;
2820 uint64_t load_address
= 0;
2821 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2822 p
!= this->sections_elements_
->end();
2824 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
2826 if (this->phdrs_elements_
!= NULL
)
2828 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2829 p
!= this->phdrs_elements_
->end();
2831 (*p
)->eval_load_address(symtab
, layout
);
2834 return this->create_segments(layout
);
2837 // Sort the sections in order to put them into segments.
2839 class Sort_output_sections
2843 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2847 Sort_output_sections::operator()(const Output_section
* os1
,
2848 const Output_section
* os2
) const
2850 // Sort first by the load address.
2851 uint64_t lma1
= (os1
->has_load_address()
2852 ? os1
->load_address()
2854 uint64_t lma2
= (os2
->has_load_address()
2855 ? os2
->load_address()
2860 // Then sort by the virtual address.
2861 if (os1
->address() != os2
->address())
2862 return os1
->address() < os2
->address();
2864 // Sort TLS sections to the end.
2865 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2866 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2870 // Sort PROGBITS before NOBITS.
2871 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2873 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2876 // Otherwise we don't care.
2880 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2881 // We treat a section with the SHF_TLS flag set as taking up space
2882 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2883 // space for them in the file.
2886 Script_sections::is_bss_section(const Output_section
* os
)
2888 return (os
->type() == elfcpp::SHT_NOBITS
2889 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2892 // Return the size taken by the file header and the program headers.
2895 Script_sections::total_header_size(Layout
* layout
) const
2897 size_t segment_count
= layout
->segment_count();
2898 size_t file_header_size
;
2899 size_t segment_headers_size
;
2900 if (parameters
->target().get_size() == 32)
2902 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2903 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2905 else if (parameters
->target().get_size() == 64)
2907 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2908 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2913 return file_header_size
+ segment_headers_size
;
2916 // Return the amount we have to subtract from the LMA to accomodate
2917 // headers of the given size. The complication is that the file
2918 // header have to be at the start of a page, as otherwise it will not
2919 // be at the start of the file.
2922 Script_sections::header_size_adjustment(uint64_t lma
,
2923 size_t sizeof_headers
) const
2925 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2926 uint64_t hdr_lma
= lma
- sizeof_headers
;
2927 hdr_lma
&= ~(abi_pagesize
- 1);
2928 return lma
- hdr_lma
;
2931 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2932 // the segment which should hold the file header and segment headers,
2936 Script_sections::create_segments(Layout
* layout
)
2938 gold_assert(this->saw_sections_clause_
);
2940 if (parameters
->options().relocatable())
2943 if (this->saw_phdrs_clause())
2944 return create_segments_from_phdrs_clause(layout
);
2946 Layout::Section_list sections
;
2947 layout
->get_allocated_sections(§ions
);
2949 // Sort the sections by address.
2950 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2952 this->create_note_and_tls_segments(layout
, §ions
);
2954 // Walk through the sections adding them to PT_LOAD segments.
2955 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2956 Output_segment
* first_seg
= NULL
;
2957 Output_segment
* current_seg
= NULL
;
2958 bool is_current_seg_readonly
= true;
2959 Layout::Section_list::iterator plast
= sections
.end();
2960 uint64_t last_vma
= 0;
2961 uint64_t last_lma
= 0;
2962 uint64_t last_size
= 0;
2963 for (Layout::Section_list::iterator p
= sections
.begin();
2964 p
!= sections
.end();
2967 const uint64_t vma
= (*p
)->address();
2968 const uint64_t lma
= ((*p
)->has_load_address()
2969 ? (*p
)->load_address()
2971 const uint64_t size
= (*p
)->current_data_size();
2973 bool need_new_segment
;
2974 if (current_seg
== NULL
)
2975 need_new_segment
= true;
2976 else if (lma
- vma
!= last_lma
- last_vma
)
2978 // This section has a different LMA relationship than the
2979 // last one; we need a new segment.
2980 need_new_segment
= true;
2982 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2983 < align_address(lma
, abi_pagesize
))
2985 // Putting this section in the segment would require
2987 need_new_segment
= true;
2989 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2991 // A non-BSS section can not follow a BSS section in the
2993 need_new_segment
= true;
2995 else if (is_current_seg_readonly
2996 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
2997 && !parameters
->options().omagic())
2999 // Don't put a writable section in the same segment as a
3000 // non-writable section.
3001 need_new_segment
= true;
3005 // Otherwise, reuse the existing segment.
3006 need_new_segment
= false;
3009 elfcpp::Elf_Word seg_flags
=
3010 Layout::section_flags_to_segment((*p
)->flags());
3012 if (need_new_segment
)
3014 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3016 current_seg
->set_addresses(vma
, lma
);
3017 if (first_seg
== NULL
)
3018 first_seg
= current_seg
;
3019 is_current_seg_readonly
= true;
3022 current_seg
->add_output_section(*p
, seg_flags
);
3024 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
3025 is_current_seg_readonly
= false;
3033 // An ELF program should work even if the program headers are not in
3034 // a PT_LOAD segment. However, it appears that the Linux kernel
3035 // does not set the AT_PHDR auxiliary entry in that case. It sets
3036 // the load address to p_vaddr - p_offset of the first PT_LOAD
3037 // segment. It then sets AT_PHDR to the load address plus the
3038 // offset to the program headers, e_phoff in the file header. This
3039 // fails when the program headers appear in the file before the
3040 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3041 // segment to hold the file header and the program headers. This is
3042 // effectively what the GNU linker does, and it is slightly more
3043 // efficient in any case. We try to use the first PT_LOAD segment
3044 // if we can, otherwise we make a new one.
3046 if (first_seg
== NULL
)
3049 // -n or -N mean that the program is not demand paged and there is
3050 // no need to put the program headers in a PT_LOAD segment.
3051 if (parameters
->options().nmagic() || parameters
->options().omagic())
3054 size_t sizeof_headers
= this->total_header_size(layout
);
3056 uint64_t vma
= first_seg
->vaddr();
3057 uint64_t lma
= first_seg
->paddr();
3059 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
3061 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
3063 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3067 // If there is no room to squeeze in the headers, then punt. The
3068 // resulting executable probably won't run on GNU/Linux, but we
3069 // trust that the user knows what they are doing.
3070 if (lma
< subtract
|| vma
< subtract
)
3073 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3075 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3080 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3081 // segment if there are any SHT_TLS sections.
3084 Script_sections::create_note_and_tls_segments(
3086 const Layout::Section_list
* sections
)
3088 gold_assert(!this->saw_phdrs_clause());
3090 bool saw_tls
= false;
3091 for (Layout::Section_list::const_iterator p
= sections
->begin();
3092 p
!= sections
->end();
3095 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3097 elfcpp::Elf_Word seg_flags
=
3098 Layout::section_flags_to_segment((*p
)->flags());
3099 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
3101 oseg
->add_output_section(*p
, seg_flags
);
3103 // Incorporate any subsequent SHT_NOTE sections, in the
3104 // hopes that the script is sensible.
3105 Layout::Section_list::const_iterator pnext
= p
+ 1;
3106 while (pnext
!= sections
->end()
3107 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
3109 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3110 oseg
->add_output_section(*pnext
, seg_flags
);
3116 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3119 gold_error(_("TLS sections are not adjacent"));
3121 elfcpp::Elf_Word seg_flags
=
3122 Layout::section_flags_to_segment((*p
)->flags());
3123 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
3125 oseg
->add_output_section(*p
, seg_flags
);
3127 Layout::Section_list::const_iterator pnext
= p
+ 1;
3128 while (pnext
!= sections
->end()
3129 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
3131 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3132 oseg
->add_output_section(*pnext
, seg_flags
);
3142 // Add a program header. The PHDRS clause is syntactically distinct
3143 // from the SECTIONS clause, but we implement it with the SECTIONS
3144 // support because PHDRS is useless if there is no SECTIONS clause.
3147 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
3148 bool includes_filehdr
, bool includes_phdrs
,
3149 bool is_flags_valid
, unsigned int flags
,
3150 Expression
* load_address
)
3152 if (this->phdrs_elements_
== NULL
)
3153 this->phdrs_elements_
= new Phdrs_elements();
3154 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
3157 is_flags_valid
, flags
,
3161 // Return the number of segments we expect to create based on the
3162 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3165 Script_sections::expected_segment_count(const Layout
* layout
) const
3167 if (this->saw_phdrs_clause())
3168 return this->phdrs_elements_
->size();
3170 Layout::Section_list sections
;
3171 layout
->get_allocated_sections(§ions
);
3173 // We assume that we will need two PT_LOAD segments.
3176 bool saw_note
= false;
3177 bool saw_tls
= false;
3178 for (Layout::Section_list::const_iterator p
= sections
.begin();
3179 p
!= sections
.end();
3182 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3184 // Assume that all note sections will fit into a single
3192 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3194 // There can only be one PT_TLS segment.
3206 // Create the segments from a PHDRS clause. Return the segment which
3207 // should hold the file header and program headers, if any.
3210 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
3212 this->attach_sections_using_phdrs_clause(layout
);
3213 return this->set_phdrs_clause_addresses(layout
);
3216 // Create the segments from the PHDRS clause, and put the output
3217 // sections in them.
3220 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
3222 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
3223 Name_to_segment name_to_segment
;
3224 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3225 p
!= this->phdrs_elements_
->end();
3227 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
3229 // Walk through the output sections and attach them to segments.
3230 // Output sections in the script which do not list segments are
3231 // attached to the same set of segments as the immediately preceding
3234 String_list
* phdr_names
= NULL
;
3235 bool load_segments_only
= false;
3236 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3237 p
!= this->sections_elements_
->end();
3241 String_list
* old_phdr_names
= phdr_names
;
3242 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
3246 if (phdr_names
== NULL
)
3248 gold_error(_("allocated section not in any segment"));
3252 // We see a list of segments names. Disable PT_LOAD segment only
3254 if (old_phdr_names
!= phdr_names
)
3255 load_segments_only
= false;
3257 // If this is an orphan section--one that was not explicitly
3258 // mentioned in the linker script--then it should not inherit
3259 // any segment type other than PT_LOAD. Otherwise, e.g., the
3260 // PT_INTERP segment will pick up following orphan sections,
3261 // which does not make sense. If this is not an orphan section,
3262 // we trust the linker script.
3265 // Enable PT_LOAD segments only filtering until we see another
3266 // list of segment names.
3267 load_segments_only
= true;
3270 bool in_load_segment
= false;
3271 for (String_list::const_iterator q
= phdr_names
->begin();
3272 q
!= phdr_names
->end();
3275 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3276 if (r
== name_to_segment
.end())
3277 gold_error(_("no segment %s"), q
->c_str());
3280 if (load_segments_only
3281 && r
->second
->type() != elfcpp::PT_LOAD
)
3284 elfcpp::Elf_Word seg_flags
=
3285 Layout::section_flags_to_segment(os
->flags());
3286 r
->second
->add_output_section(os
, seg_flags
);
3288 if (r
->second
->type() == elfcpp::PT_LOAD
)
3290 if (in_load_segment
)
3291 gold_error(_("section in two PT_LOAD segments"));
3292 in_load_segment
= true;
3297 if (!in_load_segment
)
3298 gold_error(_("allocated section not in any PT_LOAD segment"));
3302 // Set the addresses for segments created from a PHDRS clause. Return
3303 // the segment which should hold the file header and program headers,
3307 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
3309 Output_segment
* load_seg
= NULL
;
3310 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3311 p
!= this->phdrs_elements_
->end();
3314 // Note that we have to set the flags after adding the output
3315 // sections to the segment, as adding an output segment can
3316 // change the flags.
3317 (*p
)->set_flags_if_valid();
3319 Output_segment
* oseg
= (*p
)->segment();
3321 if (oseg
->type() != elfcpp::PT_LOAD
)
3323 // The addresses of non-PT_LOAD segments are set from the
3324 // PT_LOAD segments.
3325 if ((*p
)->has_load_address())
3326 gold_error(_("may only specify load address for PT_LOAD segment"));
3330 // The output sections should have addresses from the SECTIONS
3331 // clause. The addresses don't have to be in order, so find the
3332 // one with the lowest load address. Use that to set the
3333 // address of the segment.
3335 Output_section
* osec
= oseg
->section_with_lowest_load_address();
3338 oseg
->set_addresses(0, 0);
3342 uint64_t vma
= osec
->address();
3343 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3345 // Override the load address of the section with the load
3346 // address specified for the segment.
3347 if ((*p
)->has_load_address())
3349 if (osec
->has_load_address())
3350 gold_warning(_("PHDRS load address overrides "
3351 "section %s load address"),
3354 lma
= (*p
)->load_address();
3357 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3358 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3360 // We could support this if we wanted to.
3361 gold_error(_("using only one of FILEHDR and PHDRS is "
3362 "not currently supported"));
3366 size_t sizeof_headers
= this->total_header_size(layout
);
3367 uint64_t subtract
= this->header_size_adjustment(lma
,
3369 if (lma
>= subtract
&& vma
>= subtract
)
3376 gold_error(_("sections loaded on first page without room "
3377 "for file and program headers "
3378 "are not supported"));
3381 if (load_seg
!= NULL
)
3382 gold_error(_("using FILEHDR and PHDRS on more than one "
3383 "PT_LOAD segment is not currently supported"));
3387 oseg
->set_addresses(vma
, lma
);
3393 // Add the file header and segment headers to non-load segments
3394 // specified in the PHDRS clause.
3397 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3398 Output_data
* segment_headers
)
3400 gold_assert(this->saw_phdrs_clause());
3401 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3402 p
!= this->phdrs_elements_
->end();
3405 if ((*p
)->type() != elfcpp::PT_LOAD
)
3407 if ((*p
)->includes_phdrs())
3408 (*p
)->segment()->add_initial_output_data(segment_headers
);
3409 if ((*p
)->includes_filehdr())
3410 (*p
)->segment()->add_initial_output_data(file_header
);
3415 // Look for an output section by name and return the address, the load
3416 // address, the alignment, and the size. This is used when an
3417 // expression refers to an output section which was not actually
3418 // created. This returns true if the section was found, false
3422 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3423 uint64_t* load_address
,
3424 uint64_t* addralign
,
3425 uint64_t* size
) const
3427 if (!this->saw_sections_clause_
)
3429 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3430 p
!= this->sections_elements_
->end();
3432 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3438 // Release all Output_segments. This remove all pointers to all
3442 Script_sections::release_segments()
3444 if (this->saw_phdrs_clause())
3446 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3447 p
!= this->phdrs_elements_
->end();
3449 (*p
)->release_segment();
3453 // Print the SECTIONS clause to F for debugging.
3456 Script_sections::print(FILE* f
) const
3458 if (!this->saw_sections_clause_
)
3461 fprintf(f
, "SECTIONS {\n");
3463 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3464 p
!= this->sections_elements_
->end();
3470 if (this->phdrs_elements_
!= NULL
)
3472 fprintf(f
, "PHDRS {\n");
3473 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3474 p
!= this->phdrs_elements_
->end();
3481 } // End namespace gold.