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
<std::pair
<Relobj
*, unsigned int> > 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
);
705 *dot_value
= next_dot
;
708 // An assertion in an output section.
710 class Output_section_element_assertion
: public Output_section_element
713 Output_section_element_assertion(Expression
* check
, const char* message
,
715 : assertion_(check
, message
, messagelen
)
722 this->assertion_
.print(f
);
726 Script_assertion assertion_
;
729 // We use a special instance of Output_section_data to handle BYTE,
730 // SHORT, etc. This permits forward references to symbols in the
733 class Output_data_expression
: public Output_section_data
736 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
737 const Symbol_table
* symtab
, const Layout
* layout
,
738 uint64_t dot_value
, Output_section
* dot_section
)
739 : Output_section_data(size
, 0),
740 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
741 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
745 // Write the data to the output file.
747 do_write(Output_file
*);
749 // Write the data to a buffer.
751 do_write_to_buffer(unsigned char*);
753 // Write to a map file.
755 do_print_to_mapfile(Mapfile
* mapfile
) const
756 { mapfile
->print_output_data(this, _("** expression")); }
759 template<bool big_endian
>
761 endian_write_to_buffer(uint64_t, unsigned char*);
765 const Symbol_table
* symtab_
;
766 const Layout
* layout_
;
768 Output_section
* dot_section_
;
771 // Write the data element to the output file.
774 Output_data_expression::do_write(Output_file
* of
)
776 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
777 this->write_to_buffer(view
);
778 of
->write_output_view(this->offset(), this->data_size(), view
);
781 // Write the data element to a buffer.
784 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
786 Output_section
* dummy
;
787 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
788 true, this->dot_value_
,
789 this->dot_section_
, &dummy
);
791 if (parameters
->target().is_big_endian())
792 this->endian_write_to_buffer
<true>(val
, buf
);
794 this->endian_write_to_buffer
<false>(val
, buf
);
797 template<bool big_endian
>
799 Output_data_expression::endian_write_to_buffer(uint64_t val
,
802 switch (this->data_size())
805 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
808 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
811 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
814 if (parameters
->target().get_size() == 32)
817 if (this->is_signed_
&& (val
& 0x80000000) != 0)
818 val
|= 0xffffffff00000000LL
;
820 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
827 // A data item in an output section.
829 class Output_section_element_data
: public Output_section_element
832 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
833 : size_(size
), is_signed_(is_signed
), val_(val
)
836 // If there is a data item, then we must create an output section.
838 needs_output_section() const
841 // Finalize symbols--we just need to update dot.
843 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
845 { *dot_value
+= this->size_
; }
847 // Store the value in the section.
849 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
850 uint64_t* dot_value
, Output_section
**, std::string
*,
851 Input_section_list
*);
853 // Print for debugging.
858 // The size in bytes.
860 // Whether the value is signed.
866 // Store the value in the section.
869 Output_section_element_data::set_section_addresses(
870 Symbol_table
* symtab
,
875 Output_section
** dot_section
,
879 gold_assert(os
!= NULL
);
880 os
->add_output_section_data(new Output_data_expression(this->size_
,
887 *dot_value
+= this->size_
;
890 // Print for debugging.
893 Output_section_element_data::print(FILE* f
) const
908 if (this->is_signed_
)
916 fprintf(f
, " %s(", s
);
917 this->val_
->print(f
);
921 // A fill value setting in an output section.
923 class Output_section_element_fill
: public Output_section_element
926 Output_section_element_fill(Expression
* val
)
930 // Update the fill value while setting section addresses.
932 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
933 uint64_t, uint64_t* dot_value
,
934 Output_section
** dot_section
,
935 std::string
* fill
, Input_section_list
*)
937 Output_section
* fill_section
;
938 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
939 *dot_value
, *dot_section
,
941 if (fill_section
!= NULL
)
942 gold_warning(_("fill value is not absolute"));
943 // FIXME: The GNU linker supports fill values of arbitrary length.
944 unsigned char fill_buff
[4];
945 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
946 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
949 // Print for debugging.
953 fprintf(f
, " FILL(");
954 this->val_
->print(f
);
959 // The new fill value.
963 // Return whether STRING contains a wildcard character. This is used
964 // to speed up matching.
967 is_wildcard_string(const std::string
& s
)
969 return strpbrk(s
.c_str(), "?*[") != NULL
;
972 // An input section specification in an output section
974 class Output_section_element_input
: public Output_section_element
977 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
979 // Finalize symbols--just update the value of the dot symbol.
981 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
982 Output_section
** dot_section
)
984 *dot_value
= this->final_dot_value_
;
985 *dot_section
= this->final_dot_section_
;
988 // See whether we match FILE_NAME and SECTION_NAME as an input
991 match_name(const char* file_name
, const char* section_name
) const;
993 // Set the section address.
995 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
996 uint64_t subalign
, uint64_t* dot_value
,
997 Output_section
**, std::string
* fill
,
998 Input_section_list
*);
1000 // Print for debugging.
1002 print(FILE* f
) const;
1005 // An input section pattern.
1006 struct Input_section_pattern
1008 std::string pattern
;
1009 bool pattern_is_wildcard
;
1012 Input_section_pattern(const char* patterna
, size_t patternlena
,
1013 Sort_wildcard sorta
)
1014 : pattern(patterna
, patternlena
),
1015 pattern_is_wildcard(is_wildcard_string(this->pattern
)),
1020 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
1022 // Filename_exclusions is a pair of filename pattern and a bool
1023 // indicating whether the filename is a wildcard.
1024 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
1026 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1027 // indicates whether this is a wildcard pattern.
1029 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
1031 return (is_wildcard_pattern
1032 ? fnmatch(pattern
, string
, 0) == 0
1033 : strcmp(string
, pattern
) == 0);
1036 // See if we match a file name.
1038 match_file_name(const char* file_name
) const;
1040 // The file name pattern. If this is the empty string, we match all
1042 std::string filename_pattern_
;
1043 // Whether the file name pattern is a wildcard.
1044 bool filename_is_wildcard_
;
1045 // How the file names should be sorted. This may only be
1046 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1047 Sort_wildcard filename_sort_
;
1048 // The list of file names to exclude.
1049 Filename_exclusions filename_exclusions_
;
1050 // The list of input section patterns.
1051 Input_section_patterns input_section_patterns_
;
1052 // Whether to keep this section when garbage collecting.
1054 // The value of dot after including all matching sections.
1055 uint64_t final_dot_value_
;
1056 // The section where dot is defined after including all matching
1058 Output_section
* final_dot_section_
;
1061 // Construct Output_section_element_input. The parser records strings
1062 // as pointers into a copy of the script file, which will go away when
1063 // parsing is complete. We make sure they are in std::string objects.
1065 Output_section_element_input::Output_section_element_input(
1066 const Input_section_spec
* spec
,
1068 : filename_pattern_(),
1069 filename_is_wildcard_(false),
1070 filename_sort_(spec
->file
.sort
),
1071 filename_exclusions_(),
1072 input_section_patterns_(),
1074 final_dot_value_(0),
1075 final_dot_section_(NULL
)
1077 // The filename pattern "*" is common, and matches all files. Turn
1078 // it into the empty string.
1079 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
1080 this->filename_pattern_
.assign(spec
->file
.name
.value
,
1081 spec
->file
.name
.length
);
1082 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
);
1084 if (spec
->input_sections
.exclude
!= NULL
)
1086 for (String_list::const_iterator p
=
1087 spec
->input_sections
.exclude
->begin();
1088 p
!= spec
->input_sections
.exclude
->end();
1091 bool is_wildcard
= is_wildcard_string(*p
);
1092 this->filename_exclusions_
.push_back(std::make_pair(*p
,
1097 if (spec
->input_sections
.sections
!= NULL
)
1099 Input_section_patterns
& isp(this->input_section_patterns_
);
1100 for (String_sort_list::const_iterator p
=
1101 spec
->input_sections
.sections
->begin();
1102 p
!= spec
->input_sections
.sections
->end();
1104 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
1109 // See whether we match FILE_NAME.
1112 Output_section_element_input::match_file_name(const char* file_name
) const
1114 if (!this->filename_pattern_
.empty())
1116 // If we were called with no filename, we refuse to match a
1117 // pattern which requires a file name.
1118 if (file_name
== NULL
)
1121 if (!match(file_name
, this->filename_pattern_
.c_str(),
1122 this->filename_is_wildcard_
))
1126 if (file_name
!= NULL
)
1128 // Now we have to see whether FILE_NAME matches one of the
1129 // exclusion patterns, if any.
1130 for (Filename_exclusions::const_iterator p
=
1131 this->filename_exclusions_
.begin();
1132 p
!= this->filename_exclusions_
.end();
1135 if (match(file_name
, p
->first
.c_str(), p
->second
))
1143 // See whether we match FILE_NAME and SECTION_NAME.
1146 Output_section_element_input::match_name(const char* file_name
,
1147 const char* section_name
) const
1149 if (!this->match_file_name(file_name
))
1152 // If there are no section name patterns, then we match.
1153 if (this->input_section_patterns_
.empty())
1156 // See whether we match the section name patterns.
1157 for (Input_section_patterns::const_iterator p
=
1158 this->input_section_patterns_
.begin();
1159 p
!= this->input_section_patterns_
.end();
1162 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
1166 // We didn't match any section names, so we didn't match.
1170 // Information we use to sort the input sections.
1172 struct Input_section_info
1176 std::string section_name
;
1181 // A class to sort the input sections.
1183 class Input_section_sorter
1186 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
1187 : filename_sort_(filename_sort
), section_sort_(section_sort
)
1191 operator()(const Input_section_info
&, const Input_section_info
&) const;
1194 Sort_wildcard filename_sort_
;
1195 Sort_wildcard section_sort_
;
1199 Input_section_sorter::operator()(const Input_section_info
& isi1
,
1200 const Input_section_info
& isi2
) const
1202 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
1203 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1204 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1205 && isi1
.addralign
== isi2
.addralign
))
1207 if (isi1
.section_name
!= isi2
.section_name
)
1208 return isi1
.section_name
< isi2
.section_name
;
1210 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
1211 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1212 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
1214 if (isi1
.addralign
!= isi2
.addralign
)
1215 return isi1
.addralign
< isi2
.addralign
;
1217 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
1219 if (isi1
.relobj
->name() != isi2
.relobj
->name())
1220 return isi1
.relobj
->name() < isi2
.relobj
->name();
1223 // Otherwise we leave them in the same order.
1227 // Set the section address. Look in INPUT_SECTIONS for sections which
1228 // match this spec, sort them as specified, and add them to the output
1232 Output_section_element_input::set_section_addresses(
1235 Output_section
* output_section
,
1237 uint64_t* dot_value
,
1238 Output_section
** dot_section
,
1240 Input_section_list
* input_sections
)
1242 // We build a list of sections which match each
1243 // Input_section_pattern.
1245 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
1246 size_t input_pattern_count
= this->input_section_patterns_
.size();
1247 if (input_pattern_count
== 0)
1248 input_pattern_count
= 1;
1249 Matching_sections
matching_sections(input_pattern_count
);
1251 // Look through the list of sections for this output section. Add
1252 // each one which matches to one of the elements of
1253 // MATCHING_SECTIONS.
1255 Input_section_list::iterator p
= input_sections
->begin();
1256 while (p
!= input_sections
->end())
1258 // Calling section_name and section_addralign is not very
1260 Input_section_info isi
;
1261 isi
.relobj
= p
->first
;
1262 isi
.shndx
= p
->second
;
1264 // Lock the object so that we can get information about the
1265 // section. This is OK since we know we are single-threaded
1268 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1269 Task_lock_obj
<Object
> tl(task
, p
->first
);
1271 isi
.section_name
= p
->first
->section_name(p
->second
);
1272 isi
.size
= p
->first
->section_size(p
->second
);
1273 isi
.addralign
= p
->first
->section_addralign(p
->second
);
1276 if (!this->match_file_name(isi
.relobj
->name().c_str()))
1278 else if (this->input_section_patterns_
.empty())
1280 matching_sections
[0].push_back(isi
);
1281 p
= input_sections
->erase(p
);
1286 for (i
= 0; i
< input_pattern_count
; ++i
)
1288 const Input_section_pattern
&
1289 isp(this->input_section_patterns_
[i
]);
1290 if (match(isi
.section_name
.c_str(), isp
.pattern
.c_str(),
1291 isp
.pattern_is_wildcard
))
1295 if (i
>= this->input_section_patterns_
.size())
1299 matching_sections
[i
].push_back(isi
);
1300 p
= input_sections
->erase(p
);
1305 // Look through MATCHING_SECTIONS. Sort each one as specified,
1306 // using a stable sort so that we get the default order when
1307 // sections are otherwise equal. Add each input section to the
1310 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1312 if (matching_sections
[i
].empty())
1315 gold_assert(output_section
!= NULL
);
1317 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1318 if (isp
.sort
!= SORT_WILDCARD_NONE
1319 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1320 std::stable_sort(matching_sections
[i
].begin(),
1321 matching_sections
[i
].end(),
1322 Input_section_sorter(this->filename_sort_
,
1325 for (std::vector
<Input_section_info
>::const_iterator p
=
1326 matching_sections
[i
].begin();
1327 p
!= matching_sections
[i
].end();
1330 uint64_t this_subalign
= p
->addralign
;
1331 if (this_subalign
< subalign
)
1332 this_subalign
= subalign
;
1334 uint64_t address
= align_address(*dot_value
, this_subalign
);
1336 if (address
> *dot_value
&& !fill
->empty())
1338 section_size_type length
=
1339 convert_to_section_size_type(address
- *dot_value
);
1340 std::string this_fill
= this->get_fill_string(fill
, length
);
1341 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1342 output_section
->add_output_section_data(posd
);
1345 output_section
->add_input_section_for_script(p
->relobj
,
1350 *dot_value
= address
+ p
->size
;
1354 this->final_dot_value_
= *dot_value
;
1355 this->final_dot_section_
= *dot_section
;
1358 // Print for debugging.
1361 Output_section_element_input::print(FILE* f
) const
1366 fprintf(f
, "KEEP(");
1368 if (!this->filename_pattern_
.empty())
1370 bool need_close_paren
= false;
1371 switch (this->filename_sort_
)
1373 case SORT_WILDCARD_NONE
:
1375 case SORT_WILDCARD_BY_NAME
:
1376 fprintf(f
, "SORT_BY_NAME(");
1377 need_close_paren
= true;
1383 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1385 if (need_close_paren
)
1389 if (!this->input_section_patterns_
.empty()
1390 || !this->filename_exclusions_
.empty())
1394 bool need_space
= false;
1395 if (!this->filename_exclusions_
.empty())
1397 fprintf(f
, "EXCLUDE_FILE(");
1398 bool need_comma
= false;
1399 for (Filename_exclusions::const_iterator p
=
1400 this->filename_exclusions_
.begin();
1401 p
!= this->filename_exclusions_
.end();
1406 fprintf(f
, "%s", p
->first
.c_str());
1413 for (Input_section_patterns::const_iterator p
=
1414 this->input_section_patterns_
.begin();
1415 p
!= this->input_section_patterns_
.end();
1421 int close_parens
= 0;
1424 case SORT_WILDCARD_NONE
:
1426 case SORT_WILDCARD_BY_NAME
:
1427 fprintf(f
, "SORT_BY_NAME(");
1430 case SORT_WILDCARD_BY_ALIGNMENT
:
1431 fprintf(f
, "SORT_BY_ALIGNMENT(");
1434 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1435 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1438 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1439 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1446 fprintf(f
, "%s", p
->pattern
.c_str());
1448 for (int i
= 0; i
< close_parens
; ++i
)
1463 // An output section.
1465 class Output_section_definition
: public Sections_element
1468 typedef Output_section_element::Input_section_list Input_section_list
;
1470 Output_section_definition(const char* name
, size_t namelen
,
1471 const Parser_output_section_header
* header
);
1473 // Finish the output section with the information in the trailer.
1475 finish(const Parser_output_section_trailer
* trailer
);
1477 // Add a symbol to be defined.
1479 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1480 bool provide
, bool hidden
);
1482 // Add an assignment to the special dot symbol.
1484 add_dot_assignment(Expression
* value
);
1486 // Add an assertion.
1488 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1490 // Add a data item to the current output section.
1492 add_data(int size
, bool is_signed
, Expression
* val
);
1494 // Add a setting for the fill value.
1496 add_fill(Expression
* val
);
1498 // Add an input section specification.
1500 add_input_section(const Input_section_spec
* spec
, bool keep
);
1502 // Return whether the output section is relro.
1505 { return this->is_relro_
; }
1507 // Record that the output section is relro.
1510 { this->is_relro_
= true; }
1512 // Create any required output sections.
1514 create_sections(Layout
*);
1516 // Add any symbols being defined to the symbol table.
1518 add_symbols_to_table(Symbol_table
* symtab
);
1520 // Finalize symbols and check assertions.
1522 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1524 // Return the output section name to use for an input file name and
1527 output_section_name(const char* file_name
, const char* section_name
,
1530 // Initialize OSP with an output section.
1532 orphan_section_init(Orphan_section_placement
* osp
,
1533 Script_sections::Elements_iterator p
)
1534 { osp
->output_section_init(this->name_
, this->output_section_
, p
); }
1536 // Set the section address.
1538 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1539 uint64_t* dot_value
, uint64_t* load_address
);
1541 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1542 // this section is constrained, and the input sections do not match,
1543 // return the constraint, and set *POSD.
1545 check_constraint(Output_section_definition
** posd
);
1547 // See if this is the alternate output section for a constrained
1548 // output section. If it is, transfer the Output_section and return
1549 // true. Otherwise return false.
1551 alternate_constraint(Output_section_definition
*, Section_constraint
);
1553 // Get the list of segments to use for an allocated section when
1554 // using a PHDRS clause.
1556 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1558 // Look for an output section by name and return the address, the
1559 // load address, the alignment, and the size. This is used when an
1560 // expression refers to an output section which was not actually
1561 // created. This returns true if the section was found, false
1564 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1567 // Return the associated Output_section if there is one.
1569 get_output_section() const
1570 { return this->output_section_
; }
1572 // Print the contents to the FILE. This is for debugging.
1577 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1579 // The output section name.
1581 // The address. This may be NULL.
1582 Expression
* address_
;
1583 // The load address. This may be NULL.
1584 Expression
* load_address_
;
1585 // The alignment. This may be NULL.
1587 // The input section alignment. This may be NULL.
1588 Expression
* subalign_
;
1589 // The constraint, if any.
1590 Section_constraint constraint_
;
1591 // The fill value. This may be NULL.
1593 // The list of segments this section should go into. This may be
1595 String_list
* phdrs_
;
1596 // The list of elements defining the section.
1597 Output_section_elements elements_
;
1598 // The Output_section created for this definition. This will be
1599 // NULL if none was created.
1600 Output_section
* output_section_
;
1601 // The address after it has been evaluated.
1602 uint64_t evaluated_address_
;
1603 // The load address after it has been evaluated.
1604 uint64_t evaluated_load_address_
;
1605 // The alignment after it has been evaluated.
1606 uint64_t evaluated_addralign_
;
1607 // The output section is relro.
1613 Output_section_definition::Output_section_definition(
1616 const Parser_output_section_header
* header
)
1617 : name_(name
, namelen
),
1618 address_(header
->address
),
1619 load_address_(header
->load_address
),
1620 align_(header
->align
),
1621 subalign_(header
->subalign
),
1622 constraint_(header
->constraint
),
1626 output_section_(NULL
),
1627 evaluated_address_(0),
1628 evaluated_load_address_(0),
1629 evaluated_addralign_(0),
1634 // Finish an output section.
1637 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1639 this->fill_
= trailer
->fill
;
1640 this->phdrs_
= trailer
->phdrs
;
1643 // Add a symbol to be defined.
1646 Output_section_definition::add_symbol_assignment(const char* name
,
1652 Output_section_element
* p
= new Output_section_element_assignment(name
,
1657 this->elements_
.push_back(p
);
1660 // Add an assignment to the special dot symbol.
1663 Output_section_definition::add_dot_assignment(Expression
* value
)
1665 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1666 this->elements_
.push_back(p
);
1669 // Add an assertion.
1672 Output_section_definition::add_assertion(Expression
* check
,
1673 const char* message
,
1676 Output_section_element
* p
= new Output_section_element_assertion(check
,
1679 this->elements_
.push_back(p
);
1682 // Add a data item to the current output section.
1685 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1687 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1689 this->elements_
.push_back(p
);
1692 // Add a setting for the fill value.
1695 Output_section_definition::add_fill(Expression
* val
)
1697 Output_section_element
* p
= new Output_section_element_fill(val
);
1698 this->elements_
.push_back(p
);
1701 // Add an input section specification.
1704 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
1707 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
1708 this->elements_
.push_back(p
);
1711 // Create any required output sections. We need an output section if
1712 // there is a data statement here.
1715 Output_section_definition::create_sections(Layout
* layout
)
1717 if (this->output_section_
!= NULL
)
1719 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1720 p
!= this->elements_
.end();
1723 if ((*p
)->needs_output_section())
1725 const char* name
= this->name_
.c_str();
1726 this->output_section_
= layout
->make_output_section_for_script(name
);
1732 // Add any symbols being defined to the symbol table.
1735 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
1737 for (Output_section_elements::iterator p
= this->elements_
.begin();
1738 p
!= this->elements_
.end();
1740 (*p
)->add_symbols_to_table(symtab
);
1743 // Finalize symbols and check assertions.
1746 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
1747 const Layout
* layout
,
1748 uint64_t* dot_value
)
1750 if (this->output_section_
!= NULL
)
1751 *dot_value
= this->output_section_
->address();
1754 uint64_t address
= *dot_value
;
1755 if (this->address_
!= NULL
)
1757 Output_section
* dummy
;
1758 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1762 if (this->align_
!= NULL
)
1764 Output_section
* dummy
;
1765 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
1769 address
= align_address(address
, align
);
1771 *dot_value
= address
;
1774 Output_section
* dot_section
= this->output_section_
;
1775 for (Output_section_elements::iterator p
= this->elements_
.begin();
1776 p
!= this->elements_
.end();
1778 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
1781 // Return the output section name to use for an input section name.
1784 Output_section_definition::output_section_name(const char* file_name
,
1785 const char* section_name
,
1786 Output_section
*** slot
)
1788 // Ask each element whether it matches NAME.
1789 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
1790 p
!= this->elements_
.end();
1793 if ((*p
)->match_name(file_name
, section_name
))
1795 // We found a match for NAME, which means that it should go
1796 // into this output section.
1797 *slot
= &this->output_section_
;
1798 return this->name_
.c_str();
1802 // We don't know about this section name.
1806 // Set the section address. Note that the OUTPUT_SECTION_ field will
1807 // be NULL if no input sections were mapped to this output section.
1808 // We still have to adjust dot and process symbol assignments.
1811 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
1813 uint64_t* dot_value
,
1814 uint64_t* load_address
)
1817 if (this->address_
== NULL
)
1818 address
= *dot_value
;
1821 Output_section
* dummy
;
1822 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
1823 *dot_value
, NULL
, &dummy
);
1827 if (this->align_
== NULL
)
1829 if (this->output_section_
== NULL
)
1832 align
= this->output_section_
->addralign();
1836 Output_section
* align_section
;
1837 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1838 NULL
, &align_section
);
1839 if (align_section
!= NULL
)
1840 gold_warning(_("alignment of section %s is not absolute"),
1841 this->name_
.c_str());
1842 if (this->output_section_
!= NULL
)
1843 this->output_section_
->set_addralign(align
);
1846 address
= align_address(address
, align
);
1848 uint64_t start_address
= address
;
1850 *dot_value
= address
;
1852 // The address of non-SHF_ALLOC sections is forced to zero,
1853 // regardless of what the linker script wants.
1854 if (this->output_section_
!= NULL
1855 && (this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0)
1856 this->output_section_
->set_address(address
);
1858 this->evaluated_address_
= address
;
1859 this->evaluated_addralign_
= align
;
1861 if (this->load_address_
== NULL
)
1862 this->evaluated_load_address_
= address
;
1865 Output_section
* dummy
;
1866 uint64_t load_address
=
1867 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
1868 this->output_section_
, &dummy
);
1869 if (this->output_section_
!= NULL
)
1870 this->output_section_
->set_load_address(load_address
);
1871 this->evaluated_load_address_
= load_address
;
1875 if (this->subalign_
== NULL
)
1879 Output_section
* subalign_section
;
1880 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
1883 if (subalign_section
!= NULL
)
1884 gold_warning(_("subalign of section %s is not absolute"),
1885 this->name_
.c_str());
1889 if (this->fill_
!= NULL
)
1891 // FIXME: The GNU linker supports fill values of arbitrary
1893 Output_section
* fill_section
;
1894 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
1898 if (fill_section
!= NULL
)
1899 gold_warning(_("fill of section %s is not absolute"),
1900 this->name_
.c_str());
1901 unsigned char fill_buff
[4];
1902 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1903 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
1906 Input_section_list input_sections
;
1907 if (this->output_section_
!= NULL
)
1909 // Get the list of input sections attached to this output
1910 // section. This will leave the output section with only
1911 // Output_section_data entries.
1912 address
+= this->output_section_
->get_input_sections(address
,
1915 *dot_value
= address
;
1918 Output_section
* dot_section
= this->output_section_
;
1919 for (Output_section_elements::iterator p
= this->elements_
.begin();
1920 p
!= this->elements_
.end();
1922 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
1923 subalign
, dot_value
, &dot_section
, &fill
,
1926 gold_assert(input_sections
.empty());
1928 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
1929 *load_address
= *dot_value
;
1931 *load_address
= (this->output_section_
->load_address()
1932 + (*dot_value
- start_address
));
1934 if (this->output_section_
!= NULL
)
1936 if (this->is_relro_
)
1937 this->output_section_
->set_is_relro();
1939 this->output_section_
->clear_is_relro();
1943 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1944 // this section is constrained, and the input sections do not match,
1945 // return the constraint, and set *POSD.
1948 Output_section_definition::check_constraint(Output_section_definition
** posd
)
1950 switch (this->constraint_
)
1952 case CONSTRAINT_NONE
:
1953 return CONSTRAINT_NONE
;
1955 case CONSTRAINT_ONLY_IF_RO
:
1956 if (this->output_section_
!= NULL
1957 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
1960 return CONSTRAINT_ONLY_IF_RO
;
1962 return CONSTRAINT_NONE
;
1964 case CONSTRAINT_ONLY_IF_RW
:
1965 if (this->output_section_
!= NULL
1966 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
1969 return CONSTRAINT_ONLY_IF_RW
;
1971 return CONSTRAINT_NONE
;
1973 case CONSTRAINT_SPECIAL
:
1974 if (this->output_section_
!= NULL
)
1975 gold_error(_("SPECIAL constraints are not implemented"));
1976 return CONSTRAINT_NONE
;
1983 // See if this is the alternate output section for a constrained
1984 // output section. If it is, transfer the Output_section and return
1985 // true. Otherwise return false.
1988 Output_section_definition::alternate_constraint(
1989 Output_section_definition
* posd
,
1990 Section_constraint constraint
)
1992 if (this->name_
!= posd
->name_
)
1997 case CONSTRAINT_ONLY_IF_RO
:
1998 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
2002 case CONSTRAINT_ONLY_IF_RW
:
2003 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
2011 // We have found the alternate constraint. We just need to move
2012 // over the Output_section. When constraints are used properly,
2013 // THIS should not have an output_section pointer, as all the input
2014 // sections should have matched the other definition.
2016 if (this->output_section_
!= NULL
)
2017 gold_error(_("mismatched definition for constrained sections"));
2019 this->output_section_
= posd
->output_section_
;
2020 posd
->output_section_
= NULL
;
2022 if (this->is_relro_
)
2023 this->output_section_
->set_is_relro();
2025 this->output_section_
->clear_is_relro();
2030 // Get the list of segments to use for an allocated section when using
2034 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
2037 if (this->output_section_
== NULL
)
2039 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2042 if (this->phdrs_
!= NULL
)
2043 *phdrs_list
= this->phdrs_
;
2044 return this->output_section_
;
2047 // Look for an output section by name and return the address, the load
2048 // address, the alignment, and the size. This is used when an
2049 // expression refers to an output section which was not actually
2050 // created. This returns true if the section was found, false
2054 Output_section_definition::get_output_section_info(const char* name
,
2056 uint64_t* load_address
,
2057 uint64_t* addralign
,
2058 uint64_t* size
) const
2060 if (this->name_
!= name
)
2063 if (this->output_section_
!= NULL
)
2065 *address
= this->output_section_
->address();
2066 if (this->output_section_
->has_load_address())
2067 *load_address
= this->output_section_
->load_address();
2069 *load_address
= *address
;
2070 *addralign
= this->output_section_
->addralign();
2071 *size
= this->output_section_
->current_data_size();
2075 *address
= this->evaluated_address_
;
2076 *load_address
= this->evaluated_load_address_
;
2077 *addralign
= this->evaluated_addralign_
;
2084 // Print for debugging.
2087 Output_section_definition::print(FILE* f
) const
2089 fprintf(f
, " %s ", this->name_
.c_str());
2091 if (this->address_
!= NULL
)
2093 this->address_
->print(f
);
2099 if (this->load_address_
!= NULL
)
2102 this->load_address_
->print(f
);
2106 if (this->align_
!= NULL
)
2108 fprintf(f
, "ALIGN(");
2109 this->align_
->print(f
);
2113 if (this->subalign_
!= NULL
)
2115 fprintf(f
, "SUBALIGN(");
2116 this->subalign_
->print(f
);
2122 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2123 p
!= this->elements_
.end();
2129 if (this->fill_
!= NULL
)
2132 this->fill_
->print(f
);
2135 if (this->phdrs_
!= NULL
)
2137 for (String_list::const_iterator p
= this->phdrs_
->begin();
2138 p
!= this->phdrs_
->end();
2140 fprintf(f
, " :%s", p
->c_str());
2146 // An output section created to hold orphaned input sections. These
2147 // do not actually appear in linker scripts. However, for convenience
2148 // when setting the output section addresses, we put a marker to these
2149 // sections in the appropriate place in the list of SECTIONS elements.
2151 class Orphan_output_section
: public Sections_element
2154 Orphan_output_section(Output_section
* os
)
2158 // Return whether the orphan output section is relro. We can just
2159 // check the output section because we always set the flag, if
2160 // needed, just after we create the Orphan_output_section.
2163 { return this->os_
->is_relro(); }
2165 // Initialize OSP with an output section. This should have been
2168 orphan_section_init(Orphan_section_placement
*,
2169 Script_sections::Elements_iterator
)
2170 { gold_unreachable(); }
2172 // Set section addresses.
2174 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*);
2176 // Get the list of segments to use for an allocated section when
2177 // using a PHDRS clause.
2179 allocate_to_segment(String_list
**, bool*);
2181 // Return the associated Output_section.
2183 get_output_section() const
2184 { return this->os_
; }
2186 // Print for debugging.
2188 print(FILE* f
) const
2190 fprintf(f
, " marker for orphaned output section %s\n",
2195 Output_section
* os_
;
2198 // Set section addresses.
2201 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2202 uint64_t* dot_value
,
2203 uint64_t* load_address
)
2205 typedef std::list
<std::pair
<Relobj
*, unsigned int> > Input_section_list
;
2207 bool have_load_address
= *load_address
!= *dot_value
;
2209 uint64_t address
= *dot_value
;
2210 address
= align_address(address
, this->os_
->addralign());
2212 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2214 this->os_
->set_address(address
);
2215 if (have_load_address
)
2216 this->os_
->set_load_address(align_address(*load_address
,
2217 this->os_
->addralign()));
2220 Input_section_list input_sections
;
2221 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2223 for (Input_section_list::iterator p
= input_sections
.begin();
2224 p
!= input_sections
.end();
2230 // We know what are single-threaded, so it is OK to lock the
2233 const Task
* task
= reinterpret_cast<const Task
*>(-1);
2234 Task_lock_obj
<Object
> tl(task
, p
->first
);
2235 addralign
= p
->first
->section_addralign(p
->second
);
2236 size
= p
->first
->section_size(p
->second
);
2239 address
= align_address(address
, addralign
);
2240 this->os_
->add_input_section_for_script(p
->first
, p
->second
, size
,
2245 if (!have_load_address
)
2246 *load_address
= address
;
2248 *load_address
+= address
- *dot_value
;
2250 *dot_value
= address
;
2253 // Get the list of segments to use for an allocated section when using
2254 // a PHDRS clause. If this is an allocated section, return the
2255 // Output_section. We don't change the list of segments.
2258 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2260 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2266 // Class Phdrs_element. A program header from a PHDRS clause.
2271 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2272 bool includes_filehdr
, bool includes_phdrs
,
2273 bool is_flags_valid
, unsigned int flags
,
2274 Expression
* load_address
)
2275 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2276 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2277 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2281 // Return the name of this segment.
2284 { return this->name_
; }
2286 // Return the type of the segment.
2289 { return this->type_
; }
2291 // Whether to include the file header.
2293 includes_filehdr() const
2294 { return this->includes_filehdr_
; }
2296 // Whether to include the program headers.
2298 includes_phdrs() const
2299 { return this->includes_phdrs_
; }
2301 // Return whether there is a load address.
2303 has_load_address() const
2304 { return this->load_address_
!= NULL
; }
2306 // Evaluate the load address expression if there is one.
2308 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2310 if (this->load_address_
!= NULL
)
2311 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2315 // Return the load address.
2317 load_address() const
2319 gold_assert(this->load_address_
!= NULL
);
2320 return this->load_address_value_
;
2323 // Create the segment.
2325 create_segment(Layout
* layout
)
2327 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2328 return this->segment_
;
2331 // Return the segment.
2334 { return this->segment_
; }
2336 // Set the segment flags if appropriate.
2338 set_flags_if_valid()
2340 if (this->is_flags_valid_
)
2341 this->segment_
->set_flags(this->flags_
);
2344 // Print for debugging.
2349 // The name used in the script.
2351 // The type of the segment (PT_LOAD, etc.).
2353 // Whether this segment includes the file header.
2354 bool includes_filehdr_
;
2355 // Whether this segment includes the section headers.
2356 bool includes_phdrs_
;
2357 // Whether the flags were explicitly specified.
2358 bool is_flags_valid_
;
2359 // The flags for this segment (PF_R, etc.) if specified.
2360 unsigned int flags_
;
2361 // The expression for the load address for this segment. This may
2363 Expression
* load_address_
;
2364 // The actual load address from evaluating the expression.
2365 uint64_t load_address_value_
;
2366 // The segment itself.
2367 Output_segment
* segment_
;
2370 // Print for debugging.
2373 Phdrs_element::print(FILE* f
) const
2375 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2376 if (this->includes_filehdr_
)
2377 fprintf(f
, " FILEHDR");
2378 if (this->includes_phdrs_
)
2379 fprintf(f
, " PHDRS");
2380 if (this->is_flags_valid_
)
2381 fprintf(f
, " FLAGS(%u)", this->flags_
);
2382 if (this->load_address_
!= NULL
)
2385 this->load_address_
->print(f
);
2391 // Class Script_sections.
2393 Script_sections::Script_sections()
2394 : saw_sections_clause_(false),
2395 in_sections_clause_(false),
2396 sections_elements_(NULL
),
2397 output_section_(NULL
),
2398 phdrs_elements_(NULL
),
2399 orphan_section_placement_(NULL
),
2400 data_segment_align_start_(),
2401 saw_data_segment_align_(false),
2402 saw_relro_end_(false)
2406 // Start a SECTIONS clause.
2409 Script_sections::start_sections()
2411 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2412 this->saw_sections_clause_
= true;
2413 this->in_sections_clause_
= true;
2414 if (this->sections_elements_
== NULL
)
2415 this->sections_elements_
= new Sections_elements
;
2418 // Finish a SECTIONS clause.
2421 Script_sections::finish_sections()
2423 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2424 this->in_sections_clause_
= false;
2427 // Add a symbol to be defined.
2430 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2431 Expression
* val
, bool provide
,
2434 if (this->output_section_
!= NULL
)
2435 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2439 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2442 this->sections_elements_
->push_back(p
);
2446 // Add an assignment to the special dot symbol.
2449 Script_sections::add_dot_assignment(Expression
* val
)
2451 if (this->output_section_
!= NULL
)
2452 this->output_section_
->add_dot_assignment(val
);
2455 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2456 this->sections_elements_
->push_back(p
);
2460 // Add an assertion.
2463 Script_sections::add_assertion(Expression
* check
, const char* message
,
2466 if (this->output_section_
!= NULL
)
2467 this->output_section_
->add_assertion(check
, message
, messagelen
);
2470 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2472 this->sections_elements_
->push_back(p
);
2476 // Start processing entries for an output section.
2479 Script_sections::start_output_section(
2482 const Parser_output_section_header
*header
)
2484 Output_section_definition
* posd
= new Output_section_definition(name
,
2487 this->sections_elements_
->push_back(posd
);
2488 gold_assert(this->output_section_
== NULL
);
2489 this->output_section_
= posd
;
2492 // Stop processing entries for an output section.
2495 Script_sections::finish_output_section(
2496 const Parser_output_section_trailer
* trailer
)
2498 gold_assert(this->output_section_
!= NULL
);
2499 this->output_section_
->finish(trailer
);
2500 this->output_section_
= NULL
;
2503 // Add a data item to the current output section.
2506 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2508 gold_assert(this->output_section_
!= NULL
);
2509 this->output_section_
->add_data(size
, is_signed
, val
);
2512 // Add a fill value setting to the current output section.
2515 Script_sections::add_fill(Expression
* val
)
2517 gold_assert(this->output_section_
!= NULL
);
2518 this->output_section_
->add_fill(val
);
2521 // Add an input section specification to the current output section.
2524 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2526 gold_assert(this->output_section_
!= NULL
);
2527 this->output_section_
->add_input_section(spec
, keep
);
2530 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
2531 // subsequent output sections may be relro.
2534 Script_sections::data_segment_align()
2536 if (this->saw_data_segment_align_
)
2537 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2538 gold_assert(!this->sections_elements_
->empty());
2539 Sections_elements::iterator p
= this->sections_elements_
->end();
2541 this->data_segment_align_start_
= p
;
2542 this->saw_data_segment_align_
= true;
2545 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
2546 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
2549 Script_sections::data_segment_relro_end()
2551 if (this->saw_relro_end_
)
2552 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2553 "in a linker script"));
2554 this->saw_relro_end_
= true;
2556 if (!this->saw_data_segment_align_
)
2557 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2560 Sections_elements::iterator p
= this->data_segment_align_start_
;
2561 for (++p
; p
!= this->sections_elements_
->end(); ++p
)
2562 (*p
)->set_is_relro();
2566 // Create any required sections.
2569 Script_sections::create_sections(Layout
* layout
)
2571 if (!this->saw_sections_clause_
)
2573 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2574 p
!= this->sections_elements_
->end();
2576 (*p
)->create_sections(layout
);
2579 // Add any symbols we are defining to the symbol table.
2582 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
2584 if (!this->saw_sections_clause_
)
2586 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2587 p
!= this->sections_elements_
->end();
2589 (*p
)->add_symbols_to_table(symtab
);
2592 // Finalize symbols and check assertions.
2595 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
2597 if (!this->saw_sections_clause_
)
2599 uint64_t dot_value
= 0;
2600 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2601 p
!= this->sections_elements_
->end();
2603 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
2606 // Return the name of the output section to use for an input file name
2607 // and section name.
2610 Script_sections::output_section_name(const char* file_name
,
2611 const char* section_name
,
2612 Output_section
*** output_section_slot
)
2614 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2615 p
!= this->sections_elements_
->end();
2618 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
2619 output_section_slot
);
2623 // The special name /DISCARD/ means that the input section
2624 // should be discarded.
2625 if (strcmp(ret
, "/DISCARD/") == 0)
2627 *output_section_slot
= NULL
;
2634 // If we couldn't find a mapping for the name, the output section
2635 // gets the name of the input section.
2637 *output_section_slot
= NULL
;
2639 return section_name
;
2642 // Place a marker for an orphan output section into the SECTIONS
2646 Script_sections::place_orphan(Output_section
* os
)
2648 Orphan_section_placement
* osp
= this->orphan_section_placement_
;
2651 // Initialize the Orphan_section_placement structure.
2652 osp
= new Orphan_section_placement();
2653 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2654 p
!= this->sections_elements_
->end();
2656 (*p
)->orphan_section_init(osp
, p
);
2657 gold_assert(!this->sections_elements_
->empty());
2658 Sections_elements::iterator last
= this->sections_elements_
->end();
2660 osp
->last_init(last
);
2661 this->orphan_section_placement_
= osp
;
2664 Orphan_output_section
* orphan
= new Orphan_output_section(os
);
2666 // Look for where to put ORPHAN.
2667 Sections_elements::iterator
* where
;
2668 if (osp
->find_place(os
, &where
))
2670 if ((**where
)->is_relro())
2673 os
->clear_is_relro();
2675 // We want to insert ORPHAN after *WHERE, and then update *WHERE
2676 // so that the next one goes after this one.
2677 Sections_elements::iterator p
= *where
;
2678 gold_assert(p
!= this->sections_elements_
->end());
2680 *where
= this->sections_elements_
->insert(p
, orphan
);
2684 os
->clear_is_relro();
2685 // We don't have a place to put this orphan section. Put it,
2686 // and all other sections like it, at the end, but before the
2687 // sections which always come at the end.
2688 Sections_elements::iterator last
= osp
->last_place();
2689 *where
= this->sections_elements_
->insert(last
, orphan
);
2693 // Set the addresses of all the output sections. Walk through all the
2694 // elements, tracking the dot symbol. Apply assignments which set
2695 // absolute symbol values, in case they are used when setting dot.
2696 // Fill in data statement values. As we find output sections, set the
2697 // address, set the address of all associated input sections, and
2698 // update dot. Return the segment which should hold the file header
2699 // and segment headers, if any.
2702 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
2704 gold_assert(this->saw_sections_clause_
);
2706 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2707 // for our representation.
2708 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2709 p
!= this->sections_elements_
->end();
2712 Output_section_definition
* posd
;
2713 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
2714 if (failed_constraint
!= CONSTRAINT_NONE
)
2716 Sections_elements::iterator q
;
2717 for (q
= this->sections_elements_
->begin();
2718 q
!= this->sections_elements_
->end();
2723 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
2728 if (q
== this->sections_elements_
->end())
2729 gold_error(_("no matching section constraint"));
2733 // Force the alignment of the first TLS section to be the maximum
2734 // alignment of all TLS sections.
2735 Output_section
* first_tls
= NULL
;
2736 uint64_t tls_align
= 0;
2737 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
2738 p
!= this->sections_elements_
->end();
2741 Output_section
*os
= (*p
)->get_output_section();
2742 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
2744 if (first_tls
== NULL
)
2746 if (os
->addralign() > tls_align
)
2747 tls_align
= os
->addralign();
2750 if (first_tls
!= NULL
)
2751 first_tls
->set_addralign(tls_align
);
2753 // For a relocatable link, we implicitly set dot to zero.
2754 uint64_t dot_value
= 0;
2755 uint64_t load_address
= 0;
2756 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
2757 p
!= this->sections_elements_
->end();
2759 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &load_address
);
2761 if (this->phdrs_elements_
!= NULL
)
2763 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
2764 p
!= this->phdrs_elements_
->end();
2766 (*p
)->eval_load_address(symtab
, layout
);
2769 return this->create_segments(layout
);
2772 // Sort the sections in order to put them into segments.
2774 class Sort_output_sections
2778 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
2782 Sort_output_sections::operator()(const Output_section
* os1
,
2783 const Output_section
* os2
) const
2785 // Sort first by the load address.
2786 uint64_t lma1
= (os1
->has_load_address()
2787 ? os1
->load_address()
2789 uint64_t lma2
= (os2
->has_load_address()
2790 ? os2
->load_address()
2795 // Then sort by the virtual address.
2796 if (os1
->address() != os2
->address())
2797 return os1
->address() < os2
->address();
2799 // Sort TLS sections to the end.
2800 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
2801 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
2805 // Sort PROGBITS before NOBITS.
2806 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
2808 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
2811 // Otherwise we don't care.
2815 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2816 // We treat a section with the SHF_TLS flag set as taking up space
2817 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2818 // space for them in the file.
2821 Script_sections::is_bss_section(const Output_section
* os
)
2823 return (os
->type() == elfcpp::SHT_NOBITS
2824 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
2827 // Return the size taken by the file header and the program headers.
2830 Script_sections::total_header_size(Layout
* layout
) const
2832 size_t segment_count
= layout
->segment_count();
2833 size_t file_header_size
;
2834 size_t segment_headers_size
;
2835 if (parameters
->target().get_size() == 32)
2837 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
2838 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
2840 else if (parameters
->target().get_size() == 64)
2842 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
2843 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
2848 return file_header_size
+ segment_headers_size
;
2851 // Return the amount we have to subtract from the LMA to accomodate
2852 // headers of the given size. The complication is that the file
2853 // header have to be at the start of a page, as otherwise it will not
2854 // be at the start of the file.
2857 Script_sections::header_size_adjustment(uint64_t lma
,
2858 size_t sizeof_headers
) const
2860 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2861 uint64_t hdr_lma
= lma
- sizeof_headers
;
2862 hdr_lma
&= ~(abi_pagesize
- 1);
2863 return lma
- hdr_lma
;
2866 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2867 // the segment which should hold the file header and segment headers,
2871 Script_sections::create_segments(Layout
* layout
)
2873 gold_assert(this->saw_sections_clause_
);
2875 if (parameters
->options().relocatable())
2878 if (this->saw_phdrs_clause())
2879 return create_segments_from_phdrs_clause(layout
);
2881 Layout::Section_list sections
;
2882 layout
->get_allocated_sections(§ions
);
2884 // Sort the sections by address.
2885 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
2887 this->create_note_and_tls_segments(layout
, §ions
);
2889 // Walk through the sections adding them to PT_LOAD segments.
2890 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
2891 Output_segment
* first_seg
= NULL
;
2892 Output_segment
* current_seg
= NULL
;
2893 bool is_current_seg_readonly
= true;
2894 Layout::Section_list::iterator plast
= sections
.end();
2895 uint64_t last_vma
= 0;
2896 uint64_t last_lma
= 0;
2897 uint64_t last_size
= 0;
2898 for (Layout::Section_list::iterator p
= sections
.begin();
2899 p
!= sections
.end();
2902 const uint64_t vma
= (*p
)->address();
2903 const uint64_t lma
= ((*p
)->has_load_address()
2904 ? (*p
)->load_address()
2906 const uint64_t size
= (*p
)->current_data_size();
2908 bool need_new_segment
;
2909 if (current_seg
== NULL
)
2910 need_new_segment
= true;
2911 else if (lma
- vma
!= last_lma
- last_vma
)
2913 // This section has a different LMA relationship than the
2914 // last one; we need a new segment.
2915 need_new_segment
= true;
2917 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
2918 < align_address(lma
, abi_pagesize
))
2920 // Putting this section in the segment would require
2922 need_new_segment
= true;
2924 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
2926 // A non-BSS section can not follow a BSS section in the
2928 need_new_segment
= true;
2930 else if (is_current_seg_readonly
2931 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
2932 && !parameters
->options().omagic())
2934 // Don't put a writable section in the same segment as a
2935 // non-writable section.
2936 need_new_segment
= true;
2940 // Otherwise, reuse the existing segment.
2941 need_new_segment
= false;
2944 elfcpp::Elf_Word seg_flags
=
2945 Layout::section_flags_to_segment((*p
)->flags());
2947 if (need_new_segment
)
2949 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
2951 current_seg
->set_addresses(vma
, lma
);
2952 if (first_seg
== NULL
)
2953 first_seg
= current_seg
;
2954 is_current_seg_readonly
= true;
2957 current_seg
->add_output_section(*p
, seg_flags
);
2959 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
2960 is_current_seg_readonly
= false;
2968 // An ELF program should work even if the program headers are not in
2969 // a PT_LOAD segment. However, it appears that the Linux kernel
2970 // does not set the AT_PHDR auxiliary entry in that case. It sets
2971 // the load address to p_vaddr - p_offset of the first PT_LOAD
2972 // segment. It then sets AT_PHDR to the load address plus the
2973 // offset to the program headers, e_phoff in the file header. This
2974 // fails when the program headers appear in the file before the
2975 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2976 // segment to hold the file header and the program headers. This is
2977 // effectively what the GNU linker does, and it is slightly more
2978 // efficient in any case. We try to use the first PT_LOAD segment
2979 // if we can, otherwise we make a new one.
2981 if (first_seg
== NULL
)
2984 size_t sizeof_headers
= this->total_header_size(layout
);
2986 uint64_t vma
= first_seg
->vaddr();
2987 uint64_t lma
= first_seg
->paddr();
2989 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
2991 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
2993 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
2997 // If there is no room to squeeze in the headers, then punt. The
2998 // resulting executable probably won't run on GNU/Linux, but we
2999 // trust that the user knows what they are doing.
3000 if (lma
< subtract
|| vma
< subtract
)
3003 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3005 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3010 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3011 // segment if there are any SHT_TLS sections.
3014 Script_sections::create_note_and_tls_segments(
3016 const Layout::Section_list
* sections
)
3018 gold_assert(!this->saw_phdrs_clause());
3020 bool saw_tls
= false;
3021 for (Layout::Section_list::const_iterator p
= sections
->begin();
3022 p
!= sections
->end();
3025 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3027 elfcpp::Elf_Word seg_flags
=
3028 Layout::section_flags_to_segment((*p
)->flags());
3029 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
3031 oseg
->add_output_section(*p
, seg_flags
);
3033 // Incorporate any subsequent SHT_NOTE sections, in the
3034 // hopes that the script is sensible.
3035 Layout::Section_list::const_iterator pnext
= p
+ 1;
3036 while (pnext
!= sections
->end()
3037 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
3039 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3040 oseg
->add_output_section(*pnext
, seg_flags
);
3046 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3049 gold_error(_("TLS sections are not adjacent"));
3051 elfcpp::Elf_Word seg_flags
=
3052 Layout::section_flags_to_segment((*p
)->flags());
3053 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
3055 oseg
->add_output_section(*p
, seg_flags
);
3057 Layout::Section_list::const_iterator pnext
= p
+ 1;
3058 while (pnext
!= sections
->end()
3059 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
3061 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3062 oseg
->add_output_section(*pnext
, seg_flags
);
3072 // Add a program header. The PHDRS clause is syntactically distinct
3073 // from the SECTIONS clause, but we implement it with the SECTIONS
3074 // support becauase PHDRS is useless if there is no SECTIONS clause.
3077 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
3078 bool includes_filehdr
, bool includes_phdrs
,
3079 bool is_flags_valid
, unsigned int flags
,
3080 Expression
* load_address
)
3082 if (this->phdrs_elements_
== NULL
)
3083 this->phdrs_elements_
= new Phdrs_elements();
3084 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
3087 is_flags_valid
, flags
,
3091 // Return the number of segments we expect to create based on the
3092 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3095 Script_sections::expected_segment_count(const Layout
* layout
) const
3097 if (this->saw_phdrs_clause())
3098 return this->phdrs_elements_
->size();
3100 Layout::Section_list sections
;
3101 layout
->get_allocated_sections(§ions
);
3103 // We assume that we will need two PT_LOAD segments.
3106 bool saw_note
= false;
3107 bool saw_tls
= false;
3108 for (Layout::Section_list::const_iterator p
= sections
.begin();
3109 p
!= sections
.end();
3112 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3114 // Assume that all note sections will fit into a single
3122 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3124 // There can only be one PT_TLS segment.
3136 // Create the segments from a PHDRS clause. Return the segment which
3137 // should hold the file header and program headers, if any.
3140 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
)
3142 this->attach_sections_using_phdrs_clause(layout
);
3143 return this->set_phdrs_clause_addresses(layout
);
3146 // Create the segments from the PHDRS clause, and put the output
3147 // sections in them.
3150 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
3152 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
3153 Name_to_segment name_to_segment
;
3154 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3155 p
!= this->phdrs_elements_
->end();
3157 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
3159 // Walk through the output sections and attach them to segments.
3160 // Output sections in the script which do not list segments are
3161 // attached to the same set of segments as the immediately preceding
3163 String_list
* phdr_names
= NULL
;
3164 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3165 p
!= this->sections_elements_
->end();
3169 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
3173 if (phdr_names
== NULL
)
3175 gold_error(_("allocated section not in any segment"));
3179 // If this is an orphan section--one that was not explicitly
3180 // mentioned in the linker script--then it should not inherit
3181 // any segment type other than PT_LOAD. Otherwise, e.g., the
3182 // PT_INTERP segment will pick up following orphan sections,
3183 // which does not make sense. If this is not an orphan section,
3184 // we trust the linker script.
3187 String_list::iterator q
= phdr_names
->begin();
3188 while (q
!= phdr_names
->end())
3190 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3191 // We give errors about unknown segments below.
3192 if (r
== name_to_segment
.end()
3193 || r
->second
->type() == elfcpp::PT_LOAD
)
3196 q
= phdr_names
->erase(q
);
3200 bool in_load_segment
= false;
3201 for (String_list::const_iterator q
= phdr_names
->begin();
3202 q
!= phdr_names
->end();
3205 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3206 if (r
== name_to_segment
.end())
3207 gold_error(_("no segment %s"), q
->c_str());
3210 elfcpp::Elf_Word seg_flags
=
3211 Layout::section_flags_to_segment(os
->flags());
3212 r
->second
->add_output_section(os
, seg_flags
);
3214 if (r
->second
->type() == elfcpp::PT_LOAD
)
3216 if (in_load_segment
)
3217 gold_error(_("section in two PT_LOAD segments"));
3218 in_load_segment
= true;
3223 if (!in_load_segment
)
3224 gold_error(_("allocated section not in any PT_LOAD segment"));
3228 // Set the addresses for segments created from a PHDRS clause. Return
3229 // the segment which should hold the file header and program headers,
3233 Script_sections::set_phdrs_clause_addresses(Layout
* layout
)
3235 Output_segment
* load_seg
= NULL
;
3236 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3237 p
!= this->phdrs_elements_
->end();
3240 // Note that we have to set the flags after adding the output
3241 // sections to the segment, as adding an output segment can
3242 // change the flags.
3243 (*p
)->set_flags_if_valid();
3245 Output_segment
* oseg
= (*p
)->segment();
3247 if (oseg
->type() != elfcpp::PT_LOAD
)
3249 // The addresses of non-PT_LOAD segments are set from the
3250 // PT_LOAD segments.
3251 if ((*p
)->has_load_address())
3252 gold_error(_("may only specify load address for PT_LOAD segment"));
3256 // The output sections should have addresses from the SECTIONS
3257 // clause. The addresses don't have to be in order, so find the
3258 // one with the lowest load address. Use that to set the
3259 // address of the segment.
3261 Output_section
* osec
= oseg
->section_with_lowest_load_address();
3264 oseg
->set_addresses(0, 0);
3268 uint64_t vma
= osec
->address();
3269 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3271 // Override the load address of the section with the load
3272 // address specified for the segment.
3273 if ((*p
)->has_load_address())
3275 if (osec
->has_load_address())
3276 gold_warning(_("PHDRS load address overrides "
3277 "section %s load address"),
3280 lma
= (*p
)->load_address();
3283 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3284 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3286 // We could support this if we wanted to.
3287 gold_error(_("using only one of FILEHDR and PHDRS is "
3288 "not currently supported"));
3292 size_t sizeof_headers
= this->total_header_size(layout
);
3293 uint64_t subtract
= this->header_size_adjustment(lma
,
3295 if (lma
>= subtract
&& vma
>= subtract
)
3302 gold_error(_("sections loaded on first page without room "
3303 "for file and program headers "
3304 "are not supported"));
3307 if (load_seg
!= NULL
)
3308 gold_error(_("using FILEHDR and PHDRS on more than one "
3309 "PT_LOAD segment is not currently supported"));
3313 oseg
->set_addresses(vma
, lma
);
3319 // Add the file header and segment headers to non-load segments
3320 // specified in the PHDRS clause.
3323 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3324 Output_data
* segment_headers
)
3326 gold_assert(this->saw_phdrs_clause());
3327 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3328 p
!= this->phdrs_elements_
->end();
3331 if ((*p
)->type() != elfcpp::PT_LOAD
)
3333 if ((*p
)->includes_phdrs())
3334 (*p
)->segment()->add_initial_output_data(segment_headers
);
3335 if ((*p
)->includes_filehdr())
3336 (*p
)->segment()->add_initial_output_data(file_header
);
3341 // Look for an output section by name and return the address, the load
3342 // address, the alignment, and the size. This is used when an
3343 // expression refers to an output section which was not actually
3344 // created. This returns true if the section was found, false
3348 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3349 uint64_t* load_address
,
3350 uint64_t* addralign
,
3351 uint64_t* size
) const
3353 if (!this->saw_sections_clause_
)
3355 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3356 p
!= this->sections_elements_
->end();
3358 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3364 // Print the SECTIONS clause to F for debugging.
3367 Script_sections::print(FILE* f
) const
3369 if (!this->saw_sections_clause_
)
3372 fprintf(f
, "SECTIONS {\n");
3374 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3375 p
!= this->sections_elements_
->end();
3381 if (this->phdrs_elements_
!= NULL
)
3383 fprintf(f
, "PHDRS {\n");
3384 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3385 p
!= this->phdrs_elements_
->end();
3392 } // End namespace gold.