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 // A region of memory.
50 Memory_region(const char* name
, size_t namelen
, unsigned int attributes
,
51 Expression
* start
, Expression
* length
)
52 : name_(name
, namelen
),
53 attributes_(attributes
),
56 current_vma_offset_(0),
57 current_lma_offset_(0),
62 // Return the name of this region.
65 { return this->name_
; }
67 // Return the start address of this region.
70 { return this->start_
; }
72 // Return the length of this region.
75 { return this->length_
; }
77 // Print the region (when debugging).
81 // Return true if <name,namelen> matches this region.
83 name_match(const char* name
, size_t namelen
)
85 return (this->name_
.length() == namelen
86 && strncmp(this->name_
.c_str(), name
, namelen
) == 0);
90 get_current_vma_address(void) const
93 script_exp_binary_add(this->start_
,
94 script_exp_integer(this->current_vma_offset_
));
98 get_current_lma_address(void) const
101 script_exp_binary_add(this->start_
,
102 script_exp_integer(this->current_lma_offset_
));
106 increment_vma_offset(std::string section_name
, uint64_t amount
,
107 const Symbol_table
* symtab
, const Layout
* layout
)
109 this->current_vma_offset_
+= amount
;
111 if (this->current_vma_offset_
112 > this->length_
->eval(symtab
, layout
, false))
113 gold_error (_("section %s overflows end of region %s"),
114 section_name
.c_str(), this->name_
.c_str());
118 increment_lma_offset(std::string section_name
, uint64_t amount
,
119 const Symbol_table
* symtab
, const Layout
* layout
)
121 this->current_lma_offset_
+= amount
;
123 if (this->current_lma_offset_
124 > this->length_
->eval(symtab
, layout
, false))
125 gold_error (_("section %s overflows end of region %s (based on load address)"),
126 section_name
.c_str(), this->name_
.c_str());
130 add_section(Output_section_definition
* sec
, bool vma
)
133 this->vma_sections_
.push_back(sec
);
135 this->lma_sections_
.push_back(sec
);
138 typedef std::vector
<Output_section_definition
*> Section_list
;
140 // Return the start of the list of sections
141 // whose VMAs are taken from this region.
142 Section_list::const_iterator
143 get_vma_section_list_start(void) const
144 { return this->vma_sections_
.begin(); }
146 // Return the start of the list of sections
147 // whose LMAs are taken from this region.
148 Section_list::const_iterator
149 get_lma_section_list_start(void) const
150 { return this->lma_sections_
.begin(); }
152 // Return the end of the list of sections
153 // whose VMAs are taken from this region.
154 Section_list::const_iterator
155 get_vma_section_list_end(void) const
156 { return this->vma_sections_
.end(); }
158 // Return the end of the list of sections
159 // whose LMAs are taken from this region.
160 Section_list::const_iterator
161 get_lma_section_list_end(void) const
162 { return this->lma_sections_
.end(); }
167 unsigned int attributes_
;
170 uint64_t current_vma_offset_
;
171 uint64_t current_lma_offset_
;
172 // A list of sections whose VMAs are set inside this region.
173 Section_list vma_sections_
;
174 // A list of sections whose LMAs are set inside this region.
175 Section_list lma_sections_
;
178 // Print a memory region.
181 Memory_region::print(FILE* f
) const
183 fprintf(f
, " %s", this->name_
.c_str());
185 unsigned int attrs
= this->attributes_
;
191 switch (attrs
& - attrs
)
193 case MEM_EXECUTABLE
: fputc('x', f
); break;
194 case MEM_WRITEABLE
: fputc('w', f
); break;
195 case MEM_READABLE
: fputc('r', f
); break;
196 case MEM_ALLOCATABLE
: fputc('a', f
); break;
197 case MEM_INITIALIZED
: fputc('i', f
); break;
201 attrs
&= ~ (attrs
& - attrs
);
207 fprintf(f
, " : origin = ");
208 this->start_
->print(f
);
209 fprintf(f
, ", length = ");
210 this->length_
->print(f
);
214 // Manage orphan sections. This is intended to be largely compatible
215 // with the GNU linker. The Linux kernel implicitly relies on
216 // something similar to the GNU linker's orphan placement. We
217 // originally used a simpler scheme here, but it caused the kernel
218 // build to fail, and was also rather inefficient.
220 class Orphan_section_placement
223 typedef Script_sections::Elements_iterator Elements_iterator
;
226 Orphan_section_placement();
228 // Handle an output section during initialization of this mapping.
230 output_section_init(const std::string
& name
, Output_section
*,
231 Elements_iterator location
);
233 // Initialize the last location.
235 last_init(Elements_iterator location
);
237 // Set *PWHERE to the address of an iterator pointing to the
238 // location to use for an orphan section. Return true if the
239 // iterator has a value, false otherwise.
241 find_place(Output_section
*, Elements_iterator
** pwhere
);
243 // Return the iterator being used for sections at the very end of
244 // the linker script.
249 // The places that we specifically recognize. This list is copied
250 // from the GNU linker.
266 // The information we keep for a specific place.
269 // The name of sections for this place.
271 // Whether we have a location for this place.
273 // The iterator for this place.
274 Elements_iterator location
;
277 // Initialize one place element.
279 initialize_place(Place_index
, const char*);
282 Place places_
[PLACE_MAX
];
283 // True if this is the first call to output_section_init.
287 // Initialize Orphan_section_placement.
289 Orphan_section_placement::Orphan_section_placement()
292 this->initialize_place(PLACE_TEXT
, ".text");
293 this->initialize_place(PLACE_RODATA
, ".rodata");
294 this->initialize_place(PLACE_DATA
, ".data");
295 this->initialize_place(PLACE_TLS
, NULL
);
296 this->initialize_place(PLACE_TLS_BSS
, NULL
);
297 this->initialize_place(PLACE_BSS
, ".bss");
298 this->initialize_place(PLACE_REL
, NULL
);
299 this->initialize_place(PLACE_INTERP
, ".interp");
300 this->initialize_place(PLACE_NONALLOC
, NULL
);
301 this->initialize_place(PLACE_LAST
, NULL
);
304 // Initialize one place element.
307 Orphan_section_placement::initialize_place(Place_index index
, const char* name
)
309 this->places_
[index
].name
= name
;
310 this->places_
[index
].have_location
= false;
313 // While initializing the Orphan_section_placement information, this
314 // is called once for each output section named in the linker script.
315 // If we found an output section during the link, it will be passed in
319 Orphan_section_placement::output_section_init(const std::string
& name
,
321 Elements_iterator location
)
323 bool first_init
= this->first_init_
;
324 this->first_init_
= false;
326 for (int i
= 0; i
< PLACE_MAX
; ++i
)
328 if (this->places_
[i
].name
!= NULL
&& this->places_
[i
].name
== name
)
330 if (this->places_
[i
].have_location
)
332 // We have already seen a section with this name.
336 this->places_
[i
].location
= location
;
337 this->places_
[i
].have_location
= true;
339 // If we just found the .bss section, restart the search for
340 // an unallocated section. This follows the GNU linker's
343 this->places_
[PLACE_NONALLOC
].have_location
= false;
349 // Relocation sections.
350 if (!this->places_
[PLACE_REL
].have_location
352 && (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
353 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
355 this->places_
[PLACE_REL
].location
= location
;
356 this->places_
[PLACE_REL
].have_location
= true;
359 // We find the location for unallocated sections by finding the
360 // first debugging or comment section after the BSS section (if
362 if (!this->places_
[PLACE_NONALLOC
].have_location
363 && (name
== ".comment" || Layout::is_debug_info_section(name
.c_str())))
365 // We add orphan sections after the location in PLACES_. We
366 // want to store unallocated sections before LOCATION. If this
367 // is the very first section, we can't use it.
371 this->places_
[PLACE_NONALLOC
].location
= location
;
372 this->places_
[PLACE_NONALLOC
].have_location
= true;
377 // Initialize the last location.
380 Orphan_section_placement::last_init(Elements_iterator location
)
382 this->places_
[PLACE_LAST
].location
= location
;
383 this->places_
[PLACE_LAST
].have_location
= true;
386 // Set *PWHERE to the address of an iterator pointing to the location
387 // to use for an orphan section. Return true if the iterator has a
388 // value, false otherwise.
391 Orphan_section_placement::find_place(Output_section
* os
,
392 Elements_iterator
** pwhere
)
394 // Figure out where OS should go. This is based on the GNU linker
395 // code. FIXME: The GNU linker handles small data sections
396 // specially, but we don't.
397 elfcpp::Elf_Word type
= os
->type();
398 elfcpp::Elf_Xword flags
= os
->flags();
400 if ((flags
& elfcpp::SHF_ALLOC
) == 0
401 && !Layout::is_debug_info_section(os
->name()))
402 index
= PLACE_NONALLOC
;
403 else if ((flags
& elfcpp::SHF_ALLOC
) == 0)
405 else if (type
== elfcpp::SHT_NOTE
)
406 index
= PLACE_INTERP
;
407 else if ((flags
& elfcpp::SHF_TLS
) != 0)
409 if (type
== elfcpp::SHT_NOBITS
)
410 index
= PLACE_TLS_BSS
;
414 else if (type
== elfcpp::SHT_NOBITS
)
416 else if ((flags
& elfcpp::SHF_WRITE
) != 0)
418 else if (type
== elfcpp::SHT_REL
|| type
== elfcpp::SHT_RELA
)
420 else if ((flags
& elfcpp::SHF_EXECINSTR
) == 0)
421 index
= PLACE_RODATA
;
425 // If we don't have a location yet, try to find one based on a
426 // plausible ordering of sections.
427 if (!this->places_
[index
].have_location
)
452 if (!this->places_
[PLACE_TLS
].have_location
)
456 if (follow
!= PLACE_MAX
&& this->places_
[follow
].have_location
)
458 // Set the location of INDEX to the location of FOLLOW. The
459 // location of INDEX will then be incremented by the caller,
460 // so anything in INDEX will continue to be after anything
462 this->places_
[index
].location
= this->places_
[follow
].location
;
463 this->places_
[index
].have_location
= true;
467 *pwhere
= &this->places_
[index
].location
;
468 bool ret
= this->places_
[index
].have_location
;
470 // The caller will set the location.
471 this->places_
[index
].have_location
= true;
476 // Return the iterator being used for sections at the very end of the
479 Orphan_section_placement::Elements_iterator
480 Orphan_section_placement::last_place() const
482 gold_assert(this->places_
[PLACE_LAST
].have_location
);
483 return this->places_
[PLACE_LAST
].location
;
486 // An element in a SECTIONS clause.
488 class Sections_element
494 virtual ~Sections_element()
497 // Return whether an output section is relro.
502 // Record that an output section is relro.
507 // Create any required output sections. The only real
508 // implementation is in Output_section_definition.
510 create_sections(Layout
*)
513 // Add any symbol being defined to the symbol table.
515 add_symbols_to_table(Symbol_table
*)
518 // Finalize symbols and check assertions.
520 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*)
523 // Return the output section name to use for an input file name and
524 // section name. This only real implementation is in
525 // Output_section_definition.
527 output_section_name(const char*, const char*, Output_section
***,
528 Script_sections::Section_type
*)
531 // Initialize OSP with an output section.
533 orphan_section_init(Orphan_section_placement
*,
534 Script_sections::Elements_iterator
)
537 // Set section addresses. This includes applying assignments if the
538 // the expression is an absolute value.
540 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*,
544 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
545 // this section is constrained, and the input sections do not match,
546 // return the constraint, and set *POSD.
547 virtual Section_constraint
548 check_constraint(Output_section_definition
**)
549 { return CONSTRAINT_NONE
; }
551 // See if this is the alternate output section for a constrained
552 // output section. If it is, transfer the Output_section and return
553 // true. Otherwise return false.
555 alternate_constraint(Output_section_definition
*, Section_constraint
)
558 // Get the list of segments to use for an allocated section when
559 // using a PHDRS clause. If this is an allocated section, return
560 // the Output_section, and set *PHDRS_LIST (the first parameter) to
561 // the list of PHDRS to which it should be attached. If the PHDRS
562 // were not specified, don't change *PHDRS_LIST. When not returning
563 // NULL, set *ORPHAN (the second parameter) according to whether
564 // this is an orphan section--one that is not mentioned in the
566 virtual Output_section
*
567 allocate_to_segment(String_list
**, bool*)
570 // Look for an output section by name and return the address, the
571 // load address, the alignment, and the size. This is used when an
572 // expression refers to an output section which was not actually
573 // created. This returns true if the section was found, false
574 // otherwise. The only real definition is for
575 // Output_section_definition.
577 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
581 // Return the associated Output_section if there is one.
582 virtual Output_section
*
583 get_output_section() const
586 // Set the section's memory regions.
588 set_memory_region(Memory_region
*, bool)
589 { gold_error(_("Attempt to set a memory region for a non-output section")); }
591 // Print the element for debugging purposes.
593 print(FILE* f
) const = 0;
596 // An assignment in a SECTIONS clause outside of an output section.
598 class Sections_element_assignment
: public Sections_element
601 Sections_element_assignment(const char* name
, size_t namelen
,
602 Expression
* val
, bool provide
, bool hidden
)
603 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
606 // Add the symbol to the symbol table.
608 add_symbols_to_table(Symbol_table
* symtab
)
609 { this->assignment_
.add_to_table(symtab
); }
611 // Finalize the symbol.
613 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
616 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
619 // Set the section address. There is no section here, but if the
620 // value is absolute, we set the symbol. This permits us to use
621 // absolute symbols when setting dot.
623 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
624 uint64_t* dot_value
, uint64_t*, uint64_t*)
626 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
629 // Print for debugging.
634 this->assignment_
.print(f
);
638 Symbol_assignment assignment_
;
641 // An assignment to the dot symbol in a SECTIONS clause outside of an
644 class Sections_element_dot_assignment
: public Sections_element
647 Sections_element_dot_assignment(Expression
* val
)
651 // Finalize the symbol.
653 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
656 // We ignore the section of the result because outside of an
657 // output section definition the dot symbol is always considered
659 Output_section
* dummy
;
660 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
664 // Update the dot symbol while setting section addresses.
666 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
667 uint64_t* dot_value
, uint64_t* dot_alignment
,
668 uint64_t* load_address
)
670 Output_section
* dummy
;
671 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
672 NULL
, &dummy
, dot_alignment
);
673 *load_address
= *dot_value
;
676 // Print for debugging.
681 this->val_
->print(f
);
689 // An assertion in a SECTIONS clause outside of an output section.
691 class Sections_element_assertion
: public Sections_element
694 Sections_element_assertion(Expression
* check
, const char* message
,
696 : assertion_(check
, message
, messagelen
)
699 // Check the assertion.
701 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
702 { this->assertion_
.check(symtab
, layout
); }
704 // Print for debugging.
709 this->assertion_
.print(f
);
713 Script_assertion assertion_
;
716 // An element in an output section in a SECTIONS clause.
718 class Output_section_element
721 // A list of input sections.
722 typedef std::list
<Output_section::Input_section
> Input_section_list
;
724 Output_section_element()
727 virtual ~Output_section_element()
730 // Return whether this element requires an output section to exist.
732 needs_output_section() const
735 // Add any symbol being defined to the symbol table.
737 add_symbols_to_table(Symbol_table
*)
740 // Finalize symbols and check assertions.
742 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
745 // Return whether this element matches FILE_NAME and SECTION_NAME.
746 // The only real implementation is in Output_section_element_input.
748 match_name(const char*, const char*) const
751 // Set section addresses. This includes applying assignments if the
752 // the expression is an absolute value.
754 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
755 uint64_t*, uint64_t*, Output_section
**, std::string
*,
759 // Print the element for debugging purposes.
761 print(FILE* f
) const = 0;
764 // Return a fill string that is LENGTH bytes long, filling it with
767 get_fill_string(const std::string
* fill
, section_size_type length
) const;
771 Output_section_element::get_fill_string(const std::string
* fill
,
772 section_size_type length
) const
774 std::string this_fill
;
775 this_fill
.reserve(length
);
776 while (this_fill
.length() + fill
->length() <= length
)
778 if (this_fill
.length() < length
)
779 this_fill
.append(*fill
, 0, length
- this_fill
.length());
783 // A symbol assignment in an output section.
785 class Output_section_element_assignment
: public Output_section_element
788 Output_section_element_assignment(const char* name
, size_t namelen
,
789 Expression
* val
, bool provide
,
791 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
794 // Add the symbol to the symbol table.
796 add_symbols_to_table(Symbol_table
* symtab
)
797 { this->assignment_
.add_to_table(symtab
); }
799 // Finalize the symbol.
801 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
802 uint64_t* dot_value
, Output_section
** dot_section
)
804 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
808 // Set the section address. There is no section here, but if the
809 // value is absolute, we set the symbol. This permits us to use
810 // absolute symbols when setting dot.
812 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
813 uint64_t, uint64_t* dot_value
, uint64_t*,
814 Output_section
**, std::string
*, Input_section_list
*)
816 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
819 // Print for debugging.
824 this->assignment_
.print(f
);
828 Symbol_assignment assignment_
;
831 // An assignment to the dot symbol in an output section.
833 class Output_section_element_dot_assignment
: public Output_section_element
836 Output_section_element_dot_assignment(Expression
* val
)
840 // Finalize the symbol.
842 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
843 uint64_t* dot_value
, Output_section
** dot_section
)
845 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
846 *dot_section
, dot_section
, NULL
);
849 // Update the dot symbol while setting section addresses.
851 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
852 uint64_t, uint64_t* dot_value
, uint64_t*,
853 Output_section
**, std::string
*, Input_section_list
*);
855 // Print for debugging.
860 this->val_
->print(f
);
868 // Update the dot symbol while setting section addresses.
871 Output_section_element_dot_assignment::set_section_addresses(
872 Symbol_table
* symtab
,
874 Output_section
* output_section
,
877 uint64_t* dot_alignment
,
878 Output_section
** dot_section
,
882 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
883 *dot_value
, *dot_section
,
884 dot_section
, dot_alignment
);
885 if (next_dot
< *dot_value
)
886 gold_error(_("dot may not move backward"));
887 if (next_dot
> *dot_value
&& output_section
!= NULL
)
889 section_size_type length
= convert_to_section_size_type(next_dot
891 Output_section_data
* posd
;
893 posd
= new Output_data_zero_fill(length
, 0);
896 std::string this_fill
= this->get_fill_string(fill
, length
);
897 posd
= new Output_data_const(this_fill
, 0);
899 output_section
->add_output_section_data(posd
);
900 layout
->new_output_section_data_from_script(posd
);
902 *dot_value
= next_dot
;
905 // An assertion in an output section.
907 class Output_section_element_assertion
: public Output_section_element
910 Output_section_element_assertion(Expression
* check
, const char* message
,
912 : assertion_(check
, message
, messagelen
)
919 this->assertion_
.print(f
);
923 Script_assertion assertion_
;
926 // We use a special instance of Output_section_data to handle BYTE,
927 // SHORT, etc. This permits forward references to symbols in the
930 class Output_data_expression
: public Output_section_data
933 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
934 const Symbol_table
* symtab
, const Layout
* layout
,
935 uint64_t dot_value
, Output_section
* dot_section
)
936 : Output_section_data(size
, 0, true),
937 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
938 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
942 // Write the data to the output file.
944 do_write(Output_file
*);
946 // Write the data to a buffer.
948 do_write_to_buffer(unsigned char*);
950 // Write to a map file.
952 do_print_to_mapfile(Mapfile
* mapfile
) const
953 { mapfile
->print_output_data(this, _("** expression")); }
956 template<bool big_endian
>
958 endian_write_to_buffer(uint64_t, unsigned char*);
962 const Symbol_table
* symtab_
;
963 const Layout
* layout_
;
965 Output_section
* dot_section_
;
968 // Write the data element to the output file.
971 Output_data_expression::do_write(Output_file
* of
)
973 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
974 this->write_to_buffer(view
);
975 of
->write_output_view(this->offset(), this->data_size(), view
);
978 // Write the data element to a buffer.
981 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
983 Output_section
* dummy
;
984 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
985 true, this->dot_value_
,
986 this->dot_section_
, &dummy
, NULL
);
988 if (parameters
->target().is_big_endian())
989 this->endian_write_to_buffer
<true>(val
, buf
);
991 this->endian_write_to_buffer
<false>(val
, buf
);
994 template<bool big_endian
>
996 Output_data_expression::endian_write_to_buffer(uint64_t val
,
999 switch (this->data_size())
1002 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
1005 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
1008 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
1011 if (parameters
->target().get_size() == 32)
1014 if (this->is_signed_
&& (val
& 0x80000000) != 0)
1015 val
|= 0xffffffff00000000LL
;
1017 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
1024 // A data item in an output section.
1026 class Output_section_element_data
: public Output_section_element
1029 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
1030 : size_(size
), is_signed_(is_signed
), val_(val
)
1033 // If there is a data item, then we must create an output section.
1035 needs_output_section() const
1038 // Finalize symbols--we just need to update dot.
1040 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1042 { *dot_value
+= this->size_
; }
1044 // Store the value in the section.
1046 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
1047 uint64_t* dot_value
, uint64_t*, Output_section
**,
1048 std::string
*, Input_section_list
*);
1050 // Print for debugging.
1055 // The size in bytes.
1057 // Whether the value is signed.
1063 // Store the value in the section.
1066 Output_section_element_data::set_section_addresses(
1067 Symbol_table
* symtab
,
1071 uint64_t* dot_value
,
1073 Output_section
** dot_section
,
1075 Input_section_list
*)
1077 gold_assert(os
!= NULL
);
1078 Output_data_expression
* expression
=
1079 new Output_data_expression(this->size_
, this->is_signed_
, this->val_
,
1080 symtab
, layout
, *dot_value
, *dot_section
);
1081 os
->add_output_section_data(expression
);
1082 layout
->new_output_section_data_from_script(expression
);
1083 *dot_value
+= this->size_
;
1086 // Print for debugging.
1089 Output_section_element_data::print(FILE* f
) const
1092 switch (this->size_
)
1104 if (this->is_signed_
)
1112 fprintf(f
, " %s(", s
);
1113 this->val_
->print(f
);
1117 // A fill value setting in an output section.
1119 class Output_section_element_fill
: public Output_section_element
1122 Output_section_element_fill(Expression
* val
)
1126 // Update the fill value while setting section addresses.
1128 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1129 uint64_t, uint64_t* dot_value
, uint64_t*,
1130 Output_section
** dot_section
,
1131 std::string
* fill
, Input_section_list
*)
1133 Output_section
* fill_section
;
1134 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
1135 *dot_value
, *dot_section
,
1136 &fill_section
, NULL
);
1137 if (fill_section
!= NULL
)
1138 gold_warning(_("fill value is not absolute"));
1139 // FIXME: The GNU linker supports fill values of arbitrary length.
1140 unsigned char fill_buff
[4];
1141 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1142 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
1145 // Print for debugging.
1147 print(FILE* f
) const
1149 fprintf(f
, " FILL(");
1150 this->val_
->print(f
);
1155 // The new fill value.
1159 // An input section specification in an output section
1161 class Output_section_element_input
: public Output_section_element
1164 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
1166 // Finalize symbols--just update the value of the dot symbol.
1168 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1169 Output_section
** dot_section
)
1171 *dot_value
= this->final_dot_value_
;
1172 *dot_section
= this->final_dot_section_
;
1175 // See whether we match FILE_NAME and SECTION_NAME as an input
1178 match_name(const char* file_name
, const char* section_name
) const;
1180 // Set the section address.
1182 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1183 uint64_t subalign
, uint64_t* dot_value
, uint64_t*,
1184 Output_section
**, std::string
* fill
,
1185 Input_section_list
*);
1187 // Print for debugging.
1189 print(FILE* f
) const;
1192 // An input section pattern.
1193 struct Input_section_pattern
1195 std::string pattern
;
1196 bool pattern_is_wildcard
;
1199 Input_section_pattern(const char* patterna
, size_t patternlena
,
1200 Sort_wildcard sorta
)
1201 : pattern(patterna
, patternlena
),
1202 pattern_is_wildcard(is_wildcard_string(this->pattern
.c_str())),
1207 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
1209 // Filename_exclusions is a pair of filename pattern and a bool
1210 // indicating whether the filename is a wildcard.
1211 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
1213 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1214 // indicates whether this is a wildcard pattern.
1216 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
1218 return (is_wildcard_pattern
1219 ? fnmatch(pattern
, string
, 0) == 0
1220 : strcmp(string
, pattern
) == 0);
1223 // See if we match a file name.
1225 match_file_name(const char* file_name
) const;
1227 // The file name pattern. If this is the empty string, we match all
1229 std::string filename_pattern_
;
1230 // Whether the file name pattern is a wildcard.
1231 bool filename_is_wildcard_
;
1232 // How the file names should be sorted. This may only be
1233 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1234 Sort_wildcard filename_sort_
;
1235 // The list of file names to exclude.
1236 Filename_exclusions filename_exclusions_
;
1237 // The list of input section patterns.
1238 Input_section_patterns input_section_patterns_
;
1239 // Whether to keep this section when garbage collecting.
1241 // The value of dot after including all matching sections.
1242 uint64_t final_dot_value_
;
1243 // The section where dot is defined after including all matching
1245 Output_section
* final_dot_section_
;
1248 // Construct Output_section_element_input. The parser records strings
1249 // as pointers into a copy of the script file, which will go away when
1250 // parsing is complete. We make sure they are in std::string objects.
1252 Output_section_element_input::Output_section_element_input(
1253 const Input_section_spec
* spec
,
1255 : filename_pattern_(),
1256 filename_is_wildcard_(false),
1257 filename_sort_(spec
->file
.sort
),
1258 filename_exclusions_(),
1259 input_section_patterns_(),
1261 final_dot_value_(0),
1262 final_dot_section_(NULL
)
1264 // The filename pattern "*" is common, and matches all files. Turn
1265 // it into the empty string.
1266 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
1267 this->filename_pattern_
.assign(spec
->file
.name
.value
,
1268 spec
->file
.name
.length
);
1269 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
.c_str());
1271 if (spec
->input_sections
.exclude
!= NULL
)
1273 for (String_list::const_iterator p
=
1274 spec
->input_sections
.exclude
->begin();
1275 p
!= spec
->input_sections
.exclude
->end();
1278 bool is_wildcard
= is_wildcard_string((*p
).c_str());
1279 this->filename_exclusions_
.push_back(std::make_pair(*p
,
1284 if (spec
->input_sections
.sections
!= NULL
)
1286 Input_section_patterns
& isp(this->input_section_patterns_
);
1287 for (String_sort_list::const_iterator p
=
1288 spec
->input_sections
.sections
->begin();
1289 p
!= spec
->input_sections
.sections
->end();
1291 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
1296 // See whether we match FILE_NAME.
1299 Output_section_element_input::match_file_name(const char* file_name
) const
1301 if (!this->filename_pattern_
.empty())
1303 // If we were called with no filename, we refuse to match a
1304 // pattern which requires a file name.
1305 if (file_name
== NULL
)
1308 if (!match(file_name
, this->filename_pattern_
.c_str(),
1309 this->filename_is_wildcard_
))
1313 if (file_name
!= NULL
)
1315 // Now we have to see whether FILE_NAME matches one of the
1316 // exclusion patterns, if any.
1317 for (Filename_exclusions::const_iterator p
=
1318 this->filename_exclusions_
.begin();
1319 p
!= this->filename_exclusions_
.end();
1322 if (match(file_name
, p
->first
.c_str(), p
->second
))
1330 // See whether we match FILE_NAME and SECTION_NAME.
1333 Output_section_element_input::match_name(const char* file_name
,
1334 const char* section_name
) const
1336 if (!this->match_file_name(file_name
))
1339 // If there are no section name patterns, then we match.
1340 if (this->input_section_patterns_
.empty())
1343 // See whether we match the section name patterns.
1344 for (Input_section_patterns::const_iterator p
=
1345 this->input_section_patterns_
.begin();
1346 p
!= this->input_section_patterns_
.end();
1349 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
1353 // We didn't match any section names, so we didn't match.
1357 // Information we use to sort the input sections.
1359 class Input_section_info
1362 Input_section_info(const Output_section::Input_section
& input_section
)
1363 : input_section_(input_section
), section_name_(),
1364 size_(0), addralign_(1)
1367 // Return the simple input section.
1368 const Output_section::Input_section
&
1369 input_section() const
1370 { return this->input_section_
; }
1372 // Return the object.
1375 { return this->input_section_
.relobj(); }
1377 // Return the section index.
1380 { return this->input_section_
.shndx(); }
1382 // Return the section name.
1384 section_name() const
1385 { return this->section_name_
; }
1387 // Set the section name.
1389 set_section_name(const std::string name
)
1390 { this->section_name_
= name
; }
1392 // Return the section size.
1395 { return this->size_
; }
1397 // Set the section size.
1399 set_size(uint64_t size
)
1400 { this->size_
= size
; }
1402 // Return the address alignment.
1405 { return this->addralign_
; }
1407 // Set the address alignment.
1409 set_addralign(uint64_t addralign
)
1410 { this->addralign_
= addralign
; }
1413 // Input section, can be a relaxed section.
1414 Output_section::Input_section input_section_
;
1415 // Name of the section.
1416 std::string section_name_
;
1419 // Address alignment.
1420 uint64_t addralign_
;
1423 // A class to sort the input sections.
1425 class Input_section_sorter
1428 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
1429 : filename_sort_(filename_sort
), section_sort_(section_sort
)
1433 operator()(const Input_section_info
&, const Input_section_info
&) const;
1436 Sort_wildcard filename_sort_
;
1437 Sort_wildcard section_sort_
;
1441 Input_section_sorter::operator()(const Input_section_info
& isi1
,
1442 const Input_section_info
& isi2
) const
1444 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
1445 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1446 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1447 && isi1
.addralign() == isi2
.addralign()))
1449 if (isi1
.section_name() != isi2
.section_name())
1450 return isi1
.section_name() < isi2
.section_name();
1452 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
1453 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1454 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
1456 if (isi1
.addralign() != isi2
.addralign())
1457 return isi1
.addralign() < isi2
.addralign();
1459 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
1461 if (isi1
.relobj()->name() != isi2
.relobj()->name())
1462 return (isi1
.relobj()->name() < isi2
.relobj()->name());
1465 // Otherwise we leave them in the same order.
1469 // Set the section address. Look in INPUT_SECTIONS for sections which
1470 // match this spec, sort them as specified, and add them to the output
1474 Output_section_element_input::set_section_addresses(
1477 Output_section
* output_section
,
1479 uint64_t* dot_value
,
1481 Output_section
** dot_section
,
1483 Input_section_list
* input_sections
)
1485 // We build a list of sections which match each
1486 // Input_section_pattern.
1488 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
1489 size_t input_pattern_count
= this->input_section_patterns_
.size();
1490 if (input_pattern_count
== 0)
1491 input_pattern_count
= 1;
1492 Matching_sections
matching_sections(input_pattern_count
);
1494 // Look through the list of sections for this output section. Add
1495 // each one which matches to one of the elements of
1496 // MATCHING_SECTIONS.
1498 Input_section_list::iterator p
= input_sections
->begin();
1499 while (p
!= input_sections
->end())
1501 Relobj
* relobj
= p
->relobj();
1502 unsigned int shndx
= p
->shndx();
1503 Input_section_info
isi(*p
);
1505 // Calling section_name and section_addralign is not very
1508 // Lock the object so that we can get information about the
1509 // section. This is OK since we know we are single-threaded
1512 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1513 Task_lock_obj
<Object
> tl(task
, relobj
);
1515 isi
.set_section_name(relobj
->section_name(shndx
));
1516 if (p
->is_relaxed_input_section())
1518 // We use current data size because relxed section sizes may not
1519 // have finalized yet.
1520 isi
.set_size(p
->relaxed_input_section()->current_data_size());
1521 isi
.set_addralign(p
->relaxed_input_section()->addralign());
1525 isi
.set_size(relobj
->section_size(shndx
));
1526 isi
.set_addralign(relobj
->section_addralign(shndx
));
1530 if (!this->match_file_name(relobj
->name().c_str()))
1532 else if (this->input_section_patterns_
.empty())
1534 matching_sections
[0].push_back(isi
);
1535 p
= input_sections
->erase(p
);
1540 for (i
= 0; i
< input_pattern_count
; ++i
)
1542 const Input_section_pattern
&
1543 isp(this->input_section_patterns_
[i
]);
1544 if (match(isi
.section_name().c_str(), isp
.pattern
.c_str(),
1545 isp
.pattern_is_wildcard
))
1549 if (i
>= this->input_section_patterns_
.size())
1553 matching_sections
[i
].push_back(isi
);
1554 p
= input_sections
->erase(p
);
1559 // Look through MATCHING_SECTIONS. Sort each one as specified,
1560 // using a stable sort so that we get the default order when
1561 // sections are otherwise equal. Add each input section to the
1564 uint64_t dot
= *dot_value
;
1565 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1567 if (matching_sections
[i
].empty())
1570 gold_assert(output_section
!= NULL
);
1572 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1573 if (isp
.sort
!= SORT_WILDCARD_NONE
1574 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1575 std::stable_sort(matching_sections
[i
].begin(),
1576 matching_sections
[i
].end(),
1577 Input_section_sorter(this->filename_sort_
,
1580 for (std::vector
<Input_section_info
>::const_iterator p
=
1581 matching_sections
[i
].begin();
1582 p
!= matching_sections
[i
].end();
1585 // Override the original address alignment if SUBALIGN is specified
1586 // and is greater than the original alignment. We need to make a
1587 // copy of the input section to modify the alignment.
1588 Output_section::Input_section
sis(p
->input_section());
1590 uint64_t this_subalign
= sis
.addralign();
1591 if (!sis
.is_input_section())
1592 sis
.output_section_data()->finalize_data_size();
1593 uint64_t data_size
= sis
.data_size();
1594 if (this_subalign
< subalign
)
1596 this_subalign
= subalign
;
1597 sis
.set_addralign(subalign
);
1600 uint64_t address
= align_address(dot
, this_subalign
);
1602 if (address
> dot
&& !fill
->empty())
1604 section_size_type length
=
1605 convert_to_section_size_type(address
- dot
);
1606 std::string this_fill
= this->get_fill_string(fill
, length
);
1607 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1608 output_section
->add_output_section_data(posd
);
1609 layout
->new_output_section_data_from_script(posd
);
1612 output_section
->add_script_input_section(sis
);
1613 dot
= address
+ data_size
;
1617 // An SHF_TLS/SHT_NOBITS section does not take up any
1619 if (output_section
== NULL
1620 || (output_section
->flags() & elfcpp::SHF_TLS
) == 0
1621 || output_section
->type() != elfcpp::SHT_NOBITS
)
1624 this->final_dot_value_
= *dot_value
;
1625 this->final_dot_section_
= *dot_section
;
1628 // Print for debugging.
1631 Output_section_element_input::print(FILE* f
) const
1636 fprintf(f
, "KEEP(");
1638 if (!this->filename_pattern_
.empty())
1640 bool need_close_paren
= false;
1641 switch (this->filename_sort_
)
1643 case SORT_WILDCARD_NONE
:
1645 case SORT_WILDCARD_BY_NAME
:
1646 fprintf(f
, "SORT_BY_NAME(");
1647 need_close_paren
= true;
1653 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1655 if (need_close_paren
)
1659 if (!this->input_section_patterns_
.empty()
1660 || !this->filename_exclusions_
.empty())
1664 bool need_space
= false;
1665 if (!this->filename_exclusions_
.empty())
1667 fprintf(f
, "EXCLUDE_FILE(");
1668 bool need_comma
= false;
1669 for (Filename_exclusions::const_iterator p
=
1670 this->filename_exclusions_
.begin();
1671 p
!= this->filename_exclusions_
.end();
1676 fprintf(f
, "%s", p
->first
.c_str());
1683 for (Input_section_patterns::const_iterator p
=
1684 this->input_section_patterns_
.begin();
1685 p
!= this->input_section_patterns_
.end();
1691 int close_parens
= 0;
1694 case SORT_WILDCARD_NONE
:
1696 case SORT_WILDCARD_BY_NAME
:
1697 fprintf(f
, "SORT_BY_NAME(");
1700 case SORT_WILDCARD_BY_ALIGNMENT
:
1701 fprintf(f
, "SORT_BY_ALIGNMENT(");
1704 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1705 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1708 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1709 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1716 fprintf(f
, "%s", p
->pattern
.c_str());
1718 for (int i
= 0; i
< close_parens
; ++i
)
1733 // An output section.
1735 class Output_section_definition
: public Sections_element
1738 typedef Output_section_element::Input_section_list Input_section_list
;
1740 Output_section_definition(const char* name
, size_t namelen
,
1741 const Parser_output_section_header
* header
);
1743 // Finish the output section with the information in the trailer.
1745 finish(const Parser_output_section_trailer
* trailer
);
1747 // Add a symbol to be defined.
1749 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1750 bool provide
, bool hidden
);
1752 // Add an assignment to the special dot symbol.
1754 add_dot_assignment(Expression
* value
);
1756 // Add an assertion.
1758 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1760 // Add a data item to the current output section.
1762 add_data(int size
, bool is_signed
, Expression
* val
);
1764 // Add a setting for the fill value.
1766 add_fill(Expression
* val
);
1768 // Add an input section specification.
1770 add_input_section(const Input_section_spec
* spec
, bool keep
);
1772 // Return whether the output section is relro.
1775 { return this->is_relro_
; }
1777 // Record that the output section is relro.
1780 { this->is_relro_
= true; }
1782 // Create any required output sections.
1784 create_sections(Layout
*);
1786 // Add any symbols being defined to the symbol table.
1788 add_symbols_to_table(Symbol_table
* symtab
);
1790 // Finalize symbols and check assertions.
1792 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1794 // Return the output section name to use for an input file name and
1797 output_section_name(const char* file_name
, const char* section_name
,
1798 Output_section
***, Script_sections::Section_type
*);
1800 // Initialize OSP with an output section.
1802 orphan_section_init(Orphan_section_placement
* osp
,
1803 Script_sections::Elements_iterator p
)
1804 { osp
->output_section_init(this->name_
, this->output_section_
, p
); }
1806 // Set the section address.
1808 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1809 uint64_t* dot_value
, uint64_t*,
1810 uint64_t* load_address
);
1812 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1813 // this section is constrained, and the input sections do not match,
1814 // return the constraint, and set *POSD.
1816 check_constraint(Output_section_definition
** posd
);
1818 // See if this is the alternate output section for a constrained
1819 // output section. If it is, transfer the Output_section and return
1820 // true. Otherwise return false.
1822 alternate_constraint(Output_section_definition
*, Section_constraint
);
1824 // Get the list of segments to use for an allocated section when
1825 // using a PHDRS clause.
1827 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1829 // Look for an output section by name and return the address, the
1830 // load address, the alignment, and the size. This is used when an
1831 // expression refers to an output section which was not actually
1832 // created. This returns true if the section was found, false
1835 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1838 // Return the associated Output_section if there is one.
1840 get_output_section() const
1841 { return this->output_section_
; }
1843 // Print the contents to the FILE. This is for debugging.
1847 // Return the output section type if specified or Script_sections::ST_NONE.
1848 Script_sections::Section_type
1849 section_type() const;
1851 // Store the memory region to use.
1853 set_memory_region(Memory_region
*, bool set_vma
);
1856 set_section_vma(Expression
* address
)
1857 { this->address_
= address
; }
1860 set_section_lma(Expression
* address
)
1861 { this->load_address_
= address
; }
1864 get_section_name(void) const
1865 { return this->name_
; }
1869 script_section_type_name(Script_section_type
);
1871 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1873 // The output section name.
1875 // The address. This may be NULL.
1876 Expression
* address_
;
1877 // The load address. This may be NULL.
1878 Expression
* load_address_
;
1879 // The alignment. This may be NULL.
1881 // The input section alignment. This may be NULL.
1882 Expression
* subalign_
;
1883 // The constraint, if any.
1884 Section_constraint constraint_
;
1885 // The fill value. This may be NULL.
1887 // The list of segments this section should go into. This may be
1889 String_list
* phdrs_
;
1890 // The list of elements defining the section.
1891 Output_section_elements elements_
;
1892 // The Output_section created for this definition. This will be
1893 // NULL if none was created.
1894 Output_section
* output_section_
;
1895 // The address after it has been evaluated.
1896 uint64_t evaluated_address_
;
1897 // The load address after it has been evaluated.
1898 uint64_t evaluated_load_address_
;
1899 // The alignment after it has been evaluated.
1900 uint64_t evaluated_addralign_
;
1901 // The output section is relro.
1903 // The output section type if specified.
1904 enum Script_section_type script_section_type_
;
1909 Output_section_definition::Output_section_definition(
1912 const Parser_output_section_header
* header
)
1913 : name_(name
, namelen
),
1914 address_(header
->address
),
1915 load_address_(header
->load_address
),
1916 align_(header
->align
),
1917 subalign_(header
->subalign
),
1918 constraint_(header
->constraint
),
1922 output_section_(NULL
),
1923 evaluated_address_(0),
1924 evaluated_load_address_(0),
1925 evaluated_addralign_(0),
1927 script_section_type_(header
->section_type
)
1931 // Finish an output section.
1934 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1936 this->fill_
= trailer
->fill
;
1937 this->phdrs_
= trailer
->phdrs
;
1940 // Add a symbol to be defined.
1943 Output_section_definition::add_symbol_assignment(const char* name
,
1949 Output_section_element
* p
= new Output_section_element_assignment(name
,
1954 this->elements_
.push_back(p
);
1957 // Add an assignment to the special dot symbol.
1960 Output_section_definition::add_dot_assignment(Expression
* value
)
1962 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1963 this->elements_
.push_back(p
);
1966 // Add an assertion.
1969 Output_section_definition::add_assertion(Expression
* check
,
1970 const char* message
,
1973 Output_section_element
* p
= new Output_section_element_assertion(check
,
1976 this->elements_
.push_back(p
);
1979 // Add a data item to the current output section.
1982 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1984 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1986 this->elements_
.push_back(p
);
1989 // Add a setting for the fill value.
1992 Output_section_definition::add_fill(Expression
* val
)
1994 Output_section_element
* p
= new Output_section_element_fill(val
);
1995 this->elements_
.push_back(p
);
1998 // Add an input section specification.
2001 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
2004 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
2005 this->elements_
.push_back(p
);
2008 // Create any required output sections. We need an output section if
2009 // there is a data statement here.
2012 Output_section_definition::create_sections(Layout
* layout
)
2014 if (this->output_section_
!= NULL
)
2016 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2017 p
!= this->elements_
.end();
2020 if ((*p
)->needs_output_section())
2022 const char* name
= this->name_
.c_str();
2023 this->output_section_
=
2024 layout
->make_output_section_for_script(name
, this->section_type());
2030 // Add any symbols being defined to the symbol table.
2033 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
2035 for (Output_section_elements::iterator p
= this->elements_
.begin();
2036 p
!= this->elements_
.end();
2038 (*p
)->add_symbols_to_table(symtab
);
2041 // Finalize symbols and check assertions.
2044 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
2045 const Layout
* layout
,
2046 uint64_t* dot_value
)
2048 if (this->output_section_
!= NULL
)
2049 *dot_value
= this->output_section_
->address();
2052 uint64_t address
= *dot_value
;
2053 if (this->address_
!= NULL
)
2055 Output_section
* dummy
;
2056 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
2060 if (this->align_
!= NULL
)
2062 Output_section
* dummy
;
2063 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
2067 address
= align_address(address
, align
);
2069 *dot_value
= address
;
2072 Output_section
* dot_section
= this->output_section_
;
2073 for (Output_section_elements::iterator p
= this->elements_
.begin();
2074 p
!= this->elements_
.end();
2076 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
2079 // Return the output section name to use for an input section name.
2082 Output_section_definition::output_section_name(
2083 const char* file_name
,
2084 const char* section_name
,
2085 Output_section
*** slot
,
2086 Script_sections::Section_type
* psection_type
)
2088 // Ask each element whether it matches NAME.
2089 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2090 p
!= this->elements_
.end();
2093 if ((*p
)->match_name(file_name
, section_name
))
2095 // We found a match for NAME, which means that it should go
2096 // into this output section.
2097 *slot
= &this->output_section_
;
2098 *psection_type
= this->section_type();
2099 return this->name_
.c_str();
2103 // We don't know about this section name.
2107 // Set the section address. Note that the OUTPUT_SECTION_ field will
2108 // be NULL if no input sections were mapped to this output section.
2109 // We still have to adjust dot and process symbol assignments.
2112 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
2114 uint64_t* dot_value
,
2115 uint64_t* dot_alignment
,
2116 uint64_t* load_address
)
2119 uint64_t old_dot_value
= *dot_value
;
2120 uint64_t old_load_address
= *load_address
;
2122 // Check for --section-start.
2123 bool is_address_set
= false;
2124 if (this->output_section_
!= NULL
)
2126 parameters
->options().section_start(this->output_section_
->name(),
2128 if (!is_address_set
)
2130 if (this->address_
== NULL
)
2131 address
= *dot_value
;
2134 Output_section
* dummy
;
2135 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
2136 *dot_value
, NULL
, &dummy
,
2142 if (this->align_
== NULL
)
2144 if (this->output_section_
== NULL
)
2147 align
= this->output_section_
->addralign();
2151 Output_section
* align_section
;
2152 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
2153 NULL
, &align_section
, NULL
);
2154 if (align_section
!= NULL
)
2155 gold_warning(_("alignment of section %s is not absolute"),
2156 this->name_
.c_str());
2157 if (this->output_section_
!= NULL
)
2158 this->output_section_
->set_addralign(align
);
2161 address
= align_address(address
, align
);
2163 uint64_t start_address
= address
;
2165 *dot_value
= address
;
2167 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2168 // forced to zero, regardless of what the linker script wants.
2169 if (this->output_section_
!= NULL
2170 && ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0
2171 || this->output_section_
->is_noload()))
2172 this->output_section_
->set_address(address
);
2174 this->evaluated_address_
= address
;
2175 this->evaluated_addralign_
= align
;
2177 if (this->load_address_
== NULL
)
2178 this->evaluated_load_address_
= address
;
2181 Output_section
* dummy
;
2183 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
2184 this->output_section_
, &dummy
,
2186 if (this->output_section_
!= NULL
)
2187 this->output_section_
->set_load_address(laddr
);
2188 this->evaluated_load_address_
= laddr
;
2192 if (this->subalign_
== NULL
)
2196 Output_section
* subalign_section
;
2197 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
2199 &subalign_section
, NULL
);
2200 if (subalign_section
!= NULL
)
2201 gold_warning(_("subalign of section %s is not absolute"),
2202 this->name_
.c_str());
2206 if (this->fill_
!= NULL
)
2208 // FIXME: The GNU linker supports fill values of arbitrary
2210 Output_section
* fill_section
;
2211 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
2213 NULL
, &fill_section
,
2215 if (fill_section
!= NULL
)
2216 gold_warning(_("fill of section %s is not absolute"),
2217 this->name_
.c_str());
2218 unsigned char fill_buff
[4];
2219 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
2220 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
2223 Input_section_list input_sections
;
2224 if (this->output_section_
!= NULL
)
2226 // Get the list of input sections attached to this output
2227 // section. This will leave the output section with only
2228 // Output_section_data entries.
2229 address
+= this->output_section_
->get_input_sections(address
,
2232 *dot_value
= address
;
2235 Output_section
* dot_section
= this->output_section_
;
2236 for (Output_section_elements::iterator p
= this->elements_
.begin();
2237 p
!= this->elements_
.end();
2239 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
2240 subalign
, dot_value
, dot_alignment
,
2241 &dot_section
, &fill
, &input_sections
);
2243 gold_assert(input_sections
.empty());
2245 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
2246 *load_address
= *dot_value
;
2248 *load_address
= (this->output_section_
->load_address()
2249 + (*dot_value
- start_address
));
2251 if (this->output_section_
!= NULL
)
2253 if (this->is_relro_
)
2254 this->output_section_
->set_is_relro();
2256 this->output_section_
->clear_is_relro();
2258 // If this is a NOLOAD section, keep dot and load address unchanged.
2259 if (this->output_section_
->is_noload())
2261 *dot_value
= old_dot_value
;
2262 *load_address
= old_load_address
;
2267 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2268 // this section is constrained, and the input sections do not match,
2269 // return the constraint, and set *POSD.
2272 Output_section_definition::check_constraint(Output_section_definition
** posd
)
2274 switch (this->constraint_
)
2276 case CONSTRAINT_NONE
:
2277 return CONSTRAINT_NONE
;
2279 case CONSTRAINT_ONLY_IF_RO
:
2280 if (this->output_section_
!= NULL
2281 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
2284 return CONSTRAINT_ONLY_IF_RO
;
2286 return CONSTRAINT_NONE
;
2288 case CONSTRAINT_ONLY_IF_RW
:
2289 if (this->output_section_
!= NULL
2290 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
2293 return CONSTRAINT_ONLY_IF_RW
;
2295 return CONSTRAINT_NONE
;
2297 case CONSTRAINT_SPECIAL
:
2298 if (this->output_section_
!= NULL
)
2299 gold_error(_("SPECIAL constraints are not implemented"));
2300 return CONSTRAINT_NONE
;
2307 // See if this is the alternate output section for a constrained
2308 // output section. If it is, transfer the Output_section and return
2309 // true. Otherwise return false.
2312 Output_section_definition::alternate_constraint(
2313 Output_section_definition
* posd
,
2314 Section_constraint constraint
)
2316 if (this->name_
!= posd
->name_
)
2321 case CONSTRAINT_ONLY_IF_RO
:
2322 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
2326 case CONSTRAINT_ONLY_IF_RW
:
2327 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
2335 // We have found the alternate constraint. We just need to move
2336 // over the Output_section. When constraints are used properly,
2337 // THIS should not have an output_section pointer, as all the input
2338 // sections should have matched the other definition.
2340 if (this->output_section_
!= NULL
)
2341 gold_error(_("mismatched definition for constrained sections"));
2343 this->output_section_
= posd
->output_section_
;
2344 posd
->output_section_
= NULL
;
2346 if (this->is_relro_
)
2347 this->output_section_
->set_is_relro();
2349 this->output_section_
->clear_is_relro();
2354 // Get the list of segments to use for an allocated section when using
2358 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
2361 // Update phdrs_list even if we don't have an output section. It
2362 // might be used by the following sections.
2363 if (this->phdrs_
!= NULL
)
2364 *phdrs_list
= this->phdrs_
;
2366 if (this->output_section_
== NULL
)
2368 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2371 return this->output_section_
;
2374 // Look for an output section by name and return the address, the load
2375 // address, the alignment, and the size. This is used when an
2376 // expression refers to an output section which was not actually
2377 // created. This returns true if the section was found, false
2381 Output_section_definition::get_output_section_info(const char* name
,
2383 uint64_t* load_address
,
2384 uint64_t* addralign
,
2385 uint64_t* size
) const
2387 if (this->name_
!= name
)
2390 if (this->output_section_
!= NULL
)
2392 *address
= this->output_section_
->address();
2393 if (this->output_section_
->has_load_address())
2394 *load_address
= this->output_section_
->load_address();
2396 *load_address
= *address
;
2397 *addralign
= this->output_section_
->addralign();
2398 *size
= this->output_section_
->current_data_size();
2402 *address
= this->evaluated_address_
;
2403 *load_address
= this->evaluated_load_address_
;
2404 *addralign
= this->evaluated_addralign_
;
2411 // Print for debugging.
2414 Output_section_definition::print(FILE* f
) const
2416 fprintf(f
, " %s ", this->name_
.c_str());
2418 if (this->address_
!= NULL
)
2420 this->address_
->print(f
);
2424 if (this->script_section_type_
!= SCRIPT_SECTION_TYPE_NONE
)
2426 this->script_section_type_name(this->script_section_type_
));
2430 if (this->load_address_
!= NULL
)
2433 this->load_address_
->print(f
);
2437 if (this->align_
!= NULL
)
2439 fprintf(f
, "ALIGN(");
2440 this->align_
->print(f
);
2444 if (this->subalign_
!= NULL
)
2446 fprintf(f
, "SUBALIGN(");
2447 this->subalign_
->print(f
);
2453 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2454 p
!= this->elements_
.end();
2460 if (this->fill_
!= NULL
)
2463 this->fill_
->print(f
);
2466 if (this->phdrs_
!= NULL
)
2468 for (String_list::const_iterator p
= this->phdrs_
->begin();
2469 p
!= this->phdrs_
->end();
2471 fprintf(f
, " :%s", p
->c_str());
2477 Script_sections::Section_type
2478 Output_section_definition::section_type() const
2480 switch (this->script_section_type_
)
2482 case SCRIPT_SECTION_TYPE_NONE
:
2483 return Script_sections::ST_NONE
;
2484 case SCRIPT_SECTION_TYPE_NOLOAD
:
2485 return Script_sections::ST_NOLOAD
;
2486 case SCRIPT_SECTION_TYPE_COPY
:
2487 case SCRIPT_SECTION_TYPE_DSECT
:
2488 case SCRIPT_SECTION_TYPE_INFO
:
2489 case SCRIPT_SECTION_TYPE_OVERLAY
:
2490 // There are not really support so we treat them as ST_NONE. The
2491 // parse should have issued errors for them already.
2492 return Script_sections::ST_NONE
;
2498 // Return the name of a script section type.
2501 Output_section_definition::script_section_type_name(
2502 Script_section_type script_section_type
)
2504 switch (script_section_type
)
2506 case SCRIPT_SECTION_TYPE_NONE
:
2508 case SCRIPT_SECTION_TYPE_NOLOAD
:
2510 case SCRIPT_SECTION_TYPE_DSECT
:
2512 case SCRIPT_SECTION_TYPE_COPY
:
2514 case SCRIPT_SECTION_TYPE_INFO
:
2516 case SCRIPT_SECTION_TYPE_OVERLAY
:
2524 Output_section_definition::set_memory_region(Memory_region
* mr
, bool set_vma
)
2526 gold_assert(mr
!= NULL
);
2527 // Add the current section to the specified region's list.
2528 mr
->add_section(this, set_vma
);
2531 // An output section created to hold orphaned input sections. These
2532 // do not actually appear in linker scripts. However, for convenience
2533 // when setting the output section addresses, we put a marker to these
2534 // sections in the appropriate place in the list of SECTIONS elements.
2536 class Orphan_output_section
: public Sections_element
2539 Orphan_output_section(Output_section
* os
)
2543 // Return whether the orphan output section is relro. We can just
2544 // check the output section because we always set the flag, if
2545 // needed, just after we create the Orphan_output_section.
2548 { return this->os_
->is_relro(); }
2550 // Initialize OSP with an output section. This should have been
2553 orphan_section_init(Orphan_section_placement
*,
2554 Script_sections::Elements_iterator
)
2555 { gold_unreachable(); }
2557 // Set section addresses.
2559 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*,
2562 // Get the list of segments to use for an allocated section when
2563 // using a PHDRS clause.
2565 allocate_to_segment(String_list
**, bool*);
2567 // Return the associated Output_section.
2569 get_output_section() const
2570 { return this->os_
; }
2572 // Print for debugging.
2574 print(FILE* f
) const
2576 fprintf(f
, " marker for orphaned output section %s\n",
2581 Output_section
* os_
;
2584 // Set section addresses.
2587 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2588 uint64_t* dot_value
,
2590 uint64_t* load_address
)
2592 typedef std::list
<Output_section::Input_section
> Input_section_list
;
2594 bool have_load_address
= *load_address
!= *dot_value
;
2596 uint64_t address
= *dot_value
;
2597 address
= align_address(address
, this->os_
->addralign());
2599 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2601 this->os_
->set_address(address
);
2602 if (have_load_address
)
2603 this->os_
->set_load_address(align_address(*load_address
,
2604 this->os_
->addralign()));
2607 Input_section_list input_sections
;
2608 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2610 for (Input_section_list::iterator p
= input_sections
.begin();
2611 p
!= input_sections
.end();
2614 uint64_t addralign
= p
->addralign();
2615 if (!p
->is_input_section())
2616 p
->output_section_data()->finalize_data_size();
2617 uint64_t size
= p
->data_size();
2618 address
= align_address(address
, addralign
);
2619 this->os_
->add_script_input_section(*p
);
2623 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2624 if (this->os_
== NULL
2625 || (this->os_
->flags() & elfcpp::SHF_TLS
) == 0
2626 || this->os_
->type() != elfcpp::SHT_NOBITS
)
2628 if (!have_load_address
)
2629 *load_address
= address
;
2631 *load_address
+= address
- *dot_value
;
2633 *dot_value
= address
;
2637 // Get the list of segments to use for an allocated section when using
2638 // a PHDRS clause. If this is an allocated section, return the
2639 // Output_section. We don't change the list of segments.
2642 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2644 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2650 // Class Phdrs_element. A program header from a PHDRS clause.
2655 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2656 bool includes_filehdr
, bool includes_phdrs
,
2657 bool is_flags_valid
, unsigned int flags
,
2658 Expression
* load_address
)
2659 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2660 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2661 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2665 // Return the name of this segment.
2668 { return this->name_
; }
2670 // Return the type of the segment.
2673 { return this->type_
; }
2675 // Whether to include the file header.
2677 includes_filehdr() const
2678 { return this->includes_filehdr_
; }
2680 // Whether to include the program headers.
2682 includes_phdrs() const
2683 { return this->includes_phdrs_
; }
2685 // Return whether there is a load address.
2687 has_load_address() const
2688 { return this->load_address_
!= NULL
; }
2690 // Evaluate the load address expression if there is one.
2692 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2694 if (this->load_address_
!= NULL
)
2695 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2699 // Return the load address.
2701 load_address() const
2703 gold_assert(this->load_address_
!= NULL
);
2704 return this->load_address_value_
;
2707 // Create the segment.
2709 create_segment(Layout
* layout
)
2711 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2712 return this->segment_
;
2715 // Return the segment.
2718 { return this->segment_
; }
2720 // Release the segment.
2723 { this->segment_
= NULL
; }
2725 // Set the segment flags if appropriate.
2727 set_flags_if_valid()
2729 if (this->is_flags_valid_
)
2730 this->segment_
->set_flags(this->flags_
);
2733 // Print for debugging.
2738 // The name used in the script.
2740 // The type of the segment (PT_LOAD, etc.).
2742 // Whether this segment includes the file header.
2743 bool includes_filehdr_
;
2744 // Whether this segment includes the section headers.
2745 bool includes_phdrs_
;
2746 // Whether the flags were explicitly specified.
2747 bool is_flags_valid_
;
2748 // The flags for this segment (PF_R, etc.) if specified.
2749 unsigned int flags_
;
2750 // The expression for the load address for this segment. This may
2752 Expression
* load_address_
;
2753 // The actual load address from evaluating the expression.
2754 uint64_t load_address_value_
;
2755 // The segment itself.
2756 Output_segment
* segment_
;
2759 // Print for debugging.
2762 Phdrs_element::print(FILE* f
) const
2764 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2765 if (this->includes_filehdr_
)
2766 fprintf(f
, " FILEHDR");
2767 if (this->includes_phdrs_
)
2768 fprintf(f
, " PHDRS");
2769 if (this->is_flags_valid_
)
2770 fprintf(f
, " FLAGS(%u)", this->flags_
);
2771 if (this->load_address_
!= NULL
)
2774 this->load_address_
->print(f
);
2780 // Add a memory region.
2783 Script_sections::add_memory_region(const char* name
, size_t namelen
,
2784 unsigned int attributes
,
2785 Expression
* start
, Expression
* length
)
2787 if (this->memory_regions_
== NULL
)
2788 this->memory_regions_
= new Memory_regions();
2789 else if (this->find_memory_region(name
, namelen
))
2791 gold_error (_("region '%.*s' already defined"), namelen
, name
);
2792 // FIXME: Add a GOLD extension to allow multiple regions with the same
2793 // name. This would amount to a single region covering disjoint blocks
2794 // of memory, which is useful for embedded devices.
2797 // FIXME: Check the length and start values. Currently we allow
2798 // non-constant expressions for these values, whereas LD does not.
2800 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
2801 // describe a region that packs from the end address going down, rather
2802 // than the start address going up. This would be useful for embedded
2805 this->memory_regions_
->push_back(new Memory_region(name
, namelen
, attributes
,
2809 // Find a memory region.
2812 Script_sections::find_memory_region(const char* name
, size_t namelen
)
2814 if (this->memory_regions_
== NULL
)
2817 for (Memory_regions::const_iterator m
= this->memory_regions_
->begin();
2818 m
!= this->memory_regions_
->end();
2820 if ((*m
)->name_match(name
, namelen
))
2826 // Find a memory region's origin.
2829 Script_sections::find_memory_region_origin(const char* name
, size_t namelen
)
2831 Memory_region
* mr
= find_memory_region(name
, namelen
);
2835 return mr
->start_address();
2838 // Find a memory region's length.
2841 Script_sections::find_memory_region_length(const char* name
, size_t namelen
)
2843 Memory_region
* mr
= find_memory_region(name
, namelen
);
2847 return mr
->length();
2850 // Set the memory region to use for the current section.
2853 Script_sections::set_memory_region(Memory_region
* mr
, bool set_vma
)
2855 gold_assert(!this->sections_elements_
->empty());
2856 this->sections_elements_
->back()->set_memory_region(mr
, set_vma
);
2859 // Class Script_sections.
2861 Script_sections::Script_sections()
2862 : saw_sections_clause_(false),
2863 in_sections_clause_(false),
2864 sections_elements_(NULL
),
2865 output_section_(NULL
),
2866 memory_regions_(NULL
),
2867 phdrs_elements_(NULL
),
2868 orphan_section_placement_(NULL
),
2869 data_segment_align_start_(),
2870 saw_data_segment_align_(false),
2871 saw_relro_end_(false),
2872 saw_segment_start_expression_(false)
2876 // Start a SECTIONS clause.
2879 Script_sections::start_sections()
2881 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2882 this->saw_sections_clause_
= true;
2883 this->in_sections_clause_
= true;
2884 if (this->sections_elements_
== NULL
)
2885 this->sections_elements_
= new Sections_elements
;
2888 // Finish a SECTIONS clause.
2891 Script_sections::finish_sections()
2893 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2894 this->in_sections_clause_
= false;
2897 // Add a symbol to be defined.
2900 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2901 Expression
* val
, bool provide
,
2904 if (this->output_section_
!= NULL
)
2905 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2909 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2912 this->sections_elements_
->push_back(p
);
2916 // Add an assignment to the special dot symbol.
2919 Script_sections::add_dot_assignment(Expression
* val
)
2921 if (this->output_section_
!= NULL
)
2922 this->output_section_
->add_dot_assignment(val
);
2925 // The GNU linker permits assignments to . to appears outside of
2926 // a SECTIONS clause, and treats it as appearing inside, so
2927 // sections_elements_ may be NULL here.
2928 if (this->sections_elements_
== NULL
)
2930 this->sections_elements_
= new Sections_elements
;
2931 this->saw_sections_clause_
= true;
2934 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2935 this->sections_elements_
->push_back(p
);
2939 // Add an assertion.
2942 Script_sections::add_assertion(Expression
* check
, const char* message
,
2945 if (this->output_section_
!= NULL
)
2946 this->output_section_
->add_assertion(check
, message
, messagelen
);
2949 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2951 this->sections_elements_
->push_back(p
);
2955 // Start processing entries for an output section.
2958 Script_sections::start_output_section(
2961 const Parser_output_section_header
* header
)
2963 Output_section_definition
* posd
= new Output_section_definition(name
,
2966 this->sections_elements_
->push_back(posd
);
2967 gold_assert(this->output_section_
== NULL
);
2968 this->output_section_
= posd
;
2971 // Stop processing entries for an output section.
2974 Script_sections::finish_output_section(
2975 const Parser_output_section_trailer
* trailer
)
2977 gold_assert(this->output_section_
!= NULL
);
2978 this->output_section_
->finish(trailer
);
2979 this->output_section_
= NULL
;
2982 // Add a data item to the current output section.
2985 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2987 gold_assert(this->output_section_
!= NULL
);
2988 this->output_section_
->add_data(size
, is_signed
, val
);
2991 // Add a fill value setting to the current output section.
2994 Script_sections::add_fill(Expression
* val
)
2996 gold_assert(this->output_section_
!= NULL
);
2997 this->output_section_
->add_fill(val
);
3000 // Add an input section specification to the current output section.
3003 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
3005 gold_assert(this->output_section_
!= NULL
);
3006 this->output_section_
->add_input_section(spec
, keep
);
3009 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
3010 // subsequent output sections may be relro.
3013 Script_sections::data_segment_align()
3015 if (this->saw_data_segment_align_
)
3016 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3017 gold_assert(!this->sections_elements_
->empty());
3018 Sections_elements::iterator p
= this->sections_elements_
->end();
3020 this->data_segment_align_start_
= p
;
3021 this->saw_data_segment_align_
= true;
3024 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
3025 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3028 Script_sections::data_segment_relro_end()
3030 if (this->saw_relro_end_
)
3031 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3032 "in a linker script"));
3033 this->saw_relro_end_
= true;
3035 if (!this->saw_data_segment_align_
)
3036 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3039 Sections_elements::iterator p
= this->data_segment_align_start_
;
3040 for (++p
; p
!= this->sections_elements_
->end(); ++p
)
3041 (*p
)->set_is_relro();
3045 // Create any required sections.
3048 Script_sections::create_sections(Layout
* layout
)
3050 if (!this->saw_sections_clause_
)
3052 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3053 p
!= this->sections_elements_
->end();
3055 (*p
)->create_sections(layout
);
3058 // Add any symbols we are defining to the symbol table.
3061 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
3063 if (!this->saw_sections_clause_
)
3065 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3066 p
!= this->sections_elements_
->end();
3068 (*p
)->add_symbols_to_table(symtab
);
3071 // Finalize symbols and check assertions.
3074 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
3076 if (!this->saw_sections_clause_
)
3078 uint64_t dot_value
= 0;
3079 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3080 p
!= this->sections_elements_
->end();
3082 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
3085 // Return the name of the output section to use for an input file name
3086 // and section name.
3089 Script_sections::output_section_name(
3090 const char* file_name
,
3091 const char* section_name
,
3092 Output_section
*** output_section_slot
,
3093 Script_sections::Section_type
* psection_type
)
3095 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3096 p
!= this->sections_elements_
->end();
3099 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
3100 output_section_slot
,
3105 // The special name /DISCARD/ means that the input section
3106 // should be discarded.
3107 if (strcmp(ret
, "/DISCARD/") == 0)
3109 *output_section_slot
= NULL
;
3110 *psection_type
= Script_sections::ST_NONE
;
3117 // If we couldn't find a mapping for the name, the output section
3118 // gets the name of the input section.
3120 *output_section_slot
= NULL
;
3121 *psection_type
= Script_sections::ST_NONE
;
3123 return section_name
;
3126 // Place a marker for an orphan output section into the SECTIONS
3130 Script_sections::place_orphan(Output_section
* os
)
3132 Orphan_section_placement
* osp
= this->orphan_section_placement_
;
3135 // Initialize the Orphan_section_placement structure.
3136 osp
= new Orphan_section_placement();
3137 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3138 p
!= this->sections_elements_
->end();
3140 (*p
)->orphan_section_init(osp
, p
);
3141 gold_assert(!this->sections_elements_
->empty());
3142 Sections_elements::iterator last
= this->sections_elements_
->end();
3144 osp
->last_init(last
);
3145 this->orphan_section_placement_
= osp
;
3148 Orphan_output_section
* orphan
= new Orphan_output_section(os
);
3150 // Look for where to put ORPHAN.
3151 Sections_elements::iterator
* where
;
3152 if (osp
->find_place(os
, &where
))
3154 if ((**where
)->is_relro())
3157 os
->clear_is_relro();
3159 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3160 // so that the next one goes after this one.
3161 Sections_elements::iterator p
= *where
;
3162 gold_assert(p
!= this->sections_elements_
->end());
3164 *where
= this->sections_elements_
->insert(p
, orphan
);
3168 os
->clear_is_relro();
3169 // We don't have a place to put this orphan section. Put it,
3170 // and all other sections like it, at the end, but before the
3171 // sections which always come at the end.
3172 Sections_elements::iterator last
= osp
->last_place();
3173 *where
= this->sections_elements_
->insert(last
, orphan
);
3177 // Set the addresses of all the output sections. Walk through all the
3178 // elements, tracking the dot symbol. Apply assignments which set
3179 // absolute symbol values, in case they are used when setting dot.
3180 // Fill in data statement values. As we find output sections, set the
3181 // address, set the address of all associated input sections, and
3182 // update dot. Return the segment which should hold the file header
3183 // and segment headers, if any.
3186 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
3188 gold_assert(this->saw_sections_clause_
);
3190 // Walk the memory regions specified in this script, if any.
3191 if (this->memory_regions_
!= NULL
)
3193 for (Memory_regions::const_iterator mr
= this->memory_regions_
->begin();
3194 mr
!= this->memory_regions_
->end();
3197 // FIXME: What should we do with the attributes of the regions ?
3199 // For each region, set the VMA of the sections associated with it.
3200 for (Memory_region::Section_list::const_iterator s
=
3201 (*mr
)->get_vma_section_list_start();
3202 s
!= (*mr
)->get_vma_section_list_end();
3205 (*s
)->set_section_vma((*mr
)->get_current_vma_address());
3206 (*mr
)->increment_vma_offset((*s
)->get_section_name(),
3207 (*s
)->get_output_section()->current_data_size(),
3211 // Similarly, set the LMA values.
3212 for (Memory_region::Section_list::const_iterator s
=
3213 (*mr
)->get_lma_section_list_start();
3214 s
!= (*mr
)->get_lma_section_list_end();
3217 (*s
)->set_section_lma((*mr
)->get_current_lma_address());
3218 (*mr
)->increment_lma_offset((*s
)->get_section_name(),
3219 (*s
)->get_output_section()->current_data_size(),
3225 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3226 // for our representation.
3227 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3228 p
!= this->sections_elements_
->end();
3231 Output_section_definition
* posd
;
3232 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
3233 if (failed_constraint
!= CONSTRAINT_NONE
)
3235 Sections_elements::iterator q
;
3236 for (q
= this->sections_elements_
->begin();
3237 q
!= this->sections_elements_
->end();
3242 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
3247 if (q
== this->sections_elements_
->end())
3248 gold_error(_("no matching section constraint"));
3252 // Force the alignment of the first TLS section to be the maximum
3253 // alignment of all TLS sections.
3254 Output_section
* first_tls
= NULL
;
3255 uint64_t tls_align
= 0;
3256 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3257 p
!= this->sections_elements_
->end();
3260 Output_section
* os
= (*p
)->get_output_section();
3261 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
3263 if (first_tls
== NULL
)
3265 if (os
->addralign() > tls_align
)
3266 tls_align
= os
->addralign();
3269 if (first_tls
!= NULL
)
3270 first_tls
->set_addralign(tls_align
);
3272 // For a relocatable link, we implicitly set dot to zero.
3273 uint64_t dot_value
= 0;
3274 uint64_t dot_alignment
= 0;
3275 uint64_t load_address
= 0;
3277 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3278 // to set section addresses. If the script has any SEGMENT_START
3279 // expression, we do not set the section addresses.
3280 bool use_tsection_options
=
3281 (!this->saw_segment_start_expression_
3282 && (parameters
->options().user_set_Ttext()
3283 || parameters
->options().user_set_Tdata()
3284 || parameters
->options().user_set_Tbss()));
3286 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3287 p
!= this->sections_elements_
->end();
3290 Output_section
* os
= (*p
)->get_output_section();
3292 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3293 // the special sections by names and doing dot assignments.
3294 if (use_tsection_options
3296 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
3298 uint64_t new_dot_value
= dot_value
;
3300 if (parameters
->options().user_set_Ttext()
3301 && strcmp(os
->name(), ".text") == 0)
3302 new_dot_value
= parameters
->options().Ttext();
3303 else if (parameters
->options().user_set_Tdata()
3304 && strcmp(os
->name(), ".data") == 0)
3305 new_dot_value
= parameters
->options().Tdata();
3306 else if (parameters
->options().user_set_Tbss()
3307 && strcmp(os
->name(), ".bss") == 0)
3308 new_dot_value
= parameters
->options().Tbss();
3310 // Update dot and load address if necessary.
3311 if (new_dot_value
< dot_value
)
3312 gold_error(_("dot may not move backward"));
3313 else if (new_dot_value
!= dot_value
)
3315 dot_value
= new_dot_value
;
3316 load_address
= new_dot_value
;
3320 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &dot_alignment
,
3324 if (this->phdrs_elements_
!= NULL
)
3326 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3327 p
!= this->phdrs_elements_
->end();
3329 (*p
)->eval_load_address(symtab
, layout
);
3332 return this->create_segments(layout
, dot_alignment
);
3335 // Sort the sections in order to put them into segments.
3337 class Sort_output_sections
3341 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
3345 Sort_output_sections::operator()(const Output_section
* os1
,
3346 const Output_section
* os2
) const
3348 // Sort first by the load address.
3349 uint64_t lma1
= (os1
->has_load_address()
3350 ? os1
->load_address()
3352 uint64_t lma2
= (os2
->has_load_address()
3353 ? os2
->load_address()
3358 // Then sort by the virtual address.
3359 if (os1
->address() != os2
->address())
3360 return os1
->address() < os2
->address();
3362 // Sort TLS sections to the end.
3363 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
3364 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
3368 // Sort PROGBITS before NOBITS.
3369 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
3371 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
3374 // Sort non-NOLOAD before NOLOAD.
3375 if (os1
->is_noload() && !os2
->is_noload())
3377 if (!os1
->is_noload() && os2
->is_noload())
3380 // Otherwise we don't care.
3384 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3385 // We treat a section with the SHF_TLS flag set as taking up space
3386 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3387 // space for them in the file.
3390 Script_sections::is_bss_section(const Output_section
* os
)
3392 return (os
->type() == elfcpp::SHT_NOBITS
3393 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
3396 // Return the size taken by the file header and the program headers.
3399 Script_sections::total_header_size(Layout
* layout
) const
3401 size_t segment_count
= layout
->segment_count();
3402 size_t file_header_size
;
3403 size_t segment_headers_size
;
3404 if (parameters
->target().get_size() == 32)
3406 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
3407 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
3409 else if (parameters
->target().get_size() == 64)
3411 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
3412 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
3417 return file_header_size
+ segment_headers_size
;
3420 // Return the amount we have to subtract from the LMA to accomodate
3421 // headers of the given size. The complication is that the file
3422 // header have to be at the start of a page, as otherwise it will not
3423 // be at the start of the file.
3426 Script_sections::header_size_adjustment(uint64_t lma
,
3427 size_t sizeof_headers
) const
3429 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3430 uint64_t hdr_lma
= lma
- sizeof_headers
;
3431 hdr_lma
&= ~(abi_pagesize
- 1);
3432 return lma
- hdr_lma
;
3435 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3436 // the segment which should hold the file header and segment headers,
3440 Script_sections::create_segments(Layout
* layout
, uint64_t dot_alignment
)
3442 gold_assert(this->saw_sections_clause_
);
3444 if (parameters
->options().relocatable())
3447 if (this->saw_phdrs_clause())
3448 return create_segments_from_phdrs_clause(layout
, dot_alignment
);
3450 Layout::Section_list sections
;
3451 layout
->get_allocated_sections(§ions
);
3453 // Sort the sections by address.
3454 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
3456 this->create_note_and_tls_segments(layout
, §ions
);
3458 // Walk through the sections adding them to PT_LOAD segments.
3459 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3460 Output_segment
* first_seg
= NULL
;
3461 Output_segment
* current_seg
= NULL
;
3462 bool is_current_seg_readonly
= true;
3463 Layout::Section_list::iterator plast
= sections
.end();
3464 uint64_t last_vma
= 0;
3465 uint64_t last_lma
= 0;
3466 uint64_t last_size
= 0;
3467 for (Layout::Section_list::iterator p
= sections
.begin();
3468 p
!= sections
.end();
3471 const uint64_t vma
= (*p
)->address();
3472 const uint64_t lma
= ((*p
)->has_load_address()
3473 ? (*p
)->load_address()
3475 const uint64_t size
= (*p
)->current_data_size();
3477 bool need_new_segment
;
3478 if (current_seg
== NULL
)
3479 need_new_segment
= true;
3480 else if (lma
- vma
!= last_lma
- last_vma
)
3482 // This section has a different LMA relationship than the
3483 // last one; we need a new segment.
3484 need_new_segment
= true;
3486 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
3487 < align_address(lma
, abi_pagesize
))
3489 // Putting this section in the segment would require
3491 need_new_segment
= true;
3493 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
3495 // A non-BSS section can not follow a BSS section in the
3497 need_new_segment
= true;
3499 else if (is_current_seg_readonly
3500 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
3501 && !parameters
->options().omagic())
3503 // Don't put a writable section in the same segment as a
3504 // non-writable section.
3505 need_new_segment
= true;
3509 // Otherwise, reuse the existing segment.
3510 need_new_segment
= false;
3513 elfcpp::Elf_Word seg_flags
=
3514 Layout::section_flags_to_segment((*p
)->flags());
3516 if (need_new_segment
)
3518 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3520 current_seg
->set_addresses(vma
, lma
);
3521 current_seg
->set_minimum_p_align(dot_alignment
);
3522 if (first_seg
== NULL
)
3523 first_seg
= current_seg
;
3524 is_current_seg_readonly
= true;
3527 current_seg
->add_output_section_to_load(layout
, *p
, seg_flags
);
3529 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
3530 is_current_seg_readonly
= false;
3538 // An ELF program should work even if the program headers are not in
3539 // a PT_LOAD segment. However, it appears that the Linux kernel
3540 // does not set the AT_PHDR auxiliary entry in that case. It sets
3541 // the load address to p_vaddr - p_offset of the first PT_LOAD
3542 // segment. It then sets AT_PHDR to the load address plus the
3543 // offset to the program headers, e_phoff in the file header. This
3544 // fails when the program headers appear in the file before the
3545 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3546 // segment to hold the file header and the program headers. This is
3547 // effectively what the GNU linker does, and it is slightly more
3548 // efficient in any case. We try to use the first PT_LOAD segment
3549 // if we can, otherwise we make a new one.
3551 if (first_seg
== NULL
)
3554 // -n or -N mean that the program is not demand paged and there is
3555 // no need to put the program headers in a PT_LOAD segment.
3556 if (parameters
->options().nmagic() || parameters
->options().omagic())
3559 size_t sizeof_headers
= this->total_header_size(layout
);
3561 uint64_t vma
= first_seg
->vaddr();
3562 uint64_t lma
= first_seg
->paddr();
3564 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
3566 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
3568 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3572 // If there is no room to squeeze in the headers, then punt. The
3573 // resulting executable probably won't run on GNU/Linux, but we
3574 // trust that the user knows what they are doing.
3575 if (lma
< subtract
|| vma
< subtract
)
3578 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3580 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3585 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3586 // segment if there are any SHT_TLS sections.
3589 Script_sections::create_note_and_tls_segments(
3591 const Layout::Section_list
* sections
)
3593 gold_assert(!this->saw_phdrs_clause());
3595 bool saw_tls
= false;
3596 for (Layout::Section_list::const_iterator p
= sections
->begin();
3597 p
!= sections
->end();
3600 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3602 elfcpp::Elf_Word seg_flags
=
3603 Layout::section_flags_to_segment((*p
)->flags());
3604 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
3606 oseg
->add_output_section_to_nonload(*p
, seg_flags
);
3608 // Incorporate any subsequent SHT_NOTE sections, in the
3609 // hopes that the script is sensible.
3610 Layout::Section_list::const_iterator pnext
= p
+ 1;
3611 while (pnext
!= sections
->end()
3612 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
3614 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3615 oseg
->add_output_section_to_nonload(*pnext
, seg_flags
);
3621 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3624 gold_error(_("TLS sections are not adjacent"));
3626 elfcpp::Elf_Word seg_flags
=
3627 Layout::section_flags_to_segment((*p
)->flags());
3628 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
3630 oseg
->add_output_section_to_nonload(*p
, seg_flags
);
3632 Layout::Section_list::const_iterator pnext
= p
+ 1;
3633 while (pnext
!= sections
->end()
3634 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
3636 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3637 oseg
->add_output_section_to_nonload(*pnext
, seg_flags
);
3647 // Add a program header. The PHDRS clause is syntactically distinct
3648 // from the SECTIONS clause, but we implement it with the SECTIONS
3649 // support because PHDRS is useless if there is no SECTIONS clause.
3652 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
3653 bool includes_filehdr
, bool includes_phdrs
,
3654 bool is_flags_valid
, unsigned int flags
,
3655 Expression
* load_address
)
3657 if (this->phdrs_elements_
== NULL
)
3658 this->phdrs_elements_
= new Phdrs_elements();
3659 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
3662 is_flags_valid
, flags
,
3666 // Return the number of segments we expect to create based on the
3667 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3670 Script_sections::expected_segment_count(const Layout
* layout
) const
3672 if (this->saw_phdrs_clause())
3673 return this->phdrs_elements_
->size();
3675 Layout::Section_list sections
;
3676 layout
->get_allocated_sections(§ions
);
3678 // We assume that we will need two PT_LOAD segments.
3681 bool saw_note
= false;
3682 bool saw_tls
= false;
3683 for (Layout::Section_list::const_iterator p
= sections
.begin();
3684 p
!= sections
.end();
3687 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3689 // Assume that all note sections will fit into a single
3697 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3699 // There can only be one PT_TLS segment.
3711 // Create the segments from a PHDRS clause. Return the segment which
3712 // should hold the file header and program headers, if any.
3715 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
,
3716 uint64_t dot_alignment
)
3718 this->attach_sections_using_phdrs_clause(layout
);
3719 return this->set_phdrs_clause_addresses(layout
, dot_alignment
);
3722 // Create the segments from the PHDRS clause, and put the output
3723 // sections in them.
3726 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
3728 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
3729 Name_to_segment name_to_segment
;
3730 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3731 p
!= this->phdrs_elements_
->end();
3733 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
3735 // Walk through the output sections and attach them to segments.
3736 // Output sections in the script which do not list segments are
3737 // attached to the same set of segments as the immediately preceding
3740 String_list
* phdr_names
= NULL
;
3741 bool load_segments_only
= false;
3742 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3743 p
!= this->sections_elements_
->end();
3747 String_list
* old_phdr_names
= phdr_names
;
3748 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
3752 if (phdr_names
== NULL
)
3754 gold_error(_("allocated section not in any segment"));
3758 // We see a list of segments names. Disable PT_LOAD segment only
3760 if (old_phdr_names
!= phdr_names
)
3761 load_segments_only
= false;
3763 // If this is an orphan section--one that was not explicitly
3764 // mentioned in the linker script--then it should not inherit
3765 // any segment type other than PT_LOAD. Otherwise, e.g., the
3766 // PT_INTERP segment will pick up following orphan sections,
3767 // which does not make sense. If this is not an orphan section,
3768 // we trust the linker script.
3771 // Enable PT_LOAD segments only filtering until we see another
3772 // list of segment names.
3773 load_segments_only
= true;
3776 bool in_load_segment
= false;
3777 for (String_list::const_iterator q
= phdr_names
->begin();
3778 q
!= phdr_names
->end();
3781 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3782 if (r
== name_to_segment
.end())
3783 gold_error(_("no segment %s"), q
->c_str());
3786 if (load_segments_only
3787 && r
->second
->type() != elfcpp::PT_LOAD
)
3790 elfcpp::Elf_Word seg_flags
=
3791 Layout::section_flags_to_segment(os
->flags());
3793 if (r
->second
->type() != elfcpp::PT_LOAD
)
3794 r
->second
->add_output_section_to_nonload(os
, seg_flags
);
3797 r
->second
->add_output_section_to_load(layout
, os
, seg_flags
);
3798 if (in_load_segment
)
3799 gold_error(_("section in two PT_LOAD segments"));
3800 in_load_segment
= true;
3805 if (!in_load_segment
)
3806 gold_error(_("allocated section not in any PT_LOAD segment"));
3810 // Set the addresses for segments created from a PHDRS clause. Return
3811 // the segment which should hold the file header and program headers,
3815 Script_sections::set_phdrs_clause_addresses(Layout
* layout
,
3816 uint64_t dot_alignment
)
3818 Output_segment
* load_seg
= NULL
;
3819 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3820 p
!= this->phdrs_elements_
->end();
3823 // Note that we have to set the flags after adding the output
3824 // sections to the segment, as adding an output segment can
3825 // change the flags.
3826 (*p
)->set_flags_if_valid();
3828 Output_segment
* oseg
= (*p
)->segment();
3830 if (oseg
->type() != elfcpp::PT_LOAD
)
3832 // The addresses of non-PT_LOAD segments are set from the
3833 // PT_LOAD segments.
3834 if ((*p
)->has_load_address())
3835 gold_error(_("may only specify load address for PT_LOAD segment"));
3839 oseg
->set_minimum_p_align(dot_alignment
);
3841 // The output sections should have addresses from the SECTIONS
3842 // clause. The addresses don't have to be in order, so find the
3843 // one with the lowest load address. Use that to set the
3844 // address of the segment.
3846 Output_section
* osec
= oseg
->section_with_lowest_load_address();
3849 oseg
->set_addresses(0, 0);
3853 uint64_t vma
= osec
->address();
3854 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3856 // Override the load address of the section with the load
3857 // address specified for the segment.
3858 if ((*p
)->has_load_address())
3860 if (osec
->has_load_address())
3861 gold_warning(_("PHDRS load address overrides "
3862 "section %s load address"),
3865 lma
= (*p
)->load_address();
3868 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3869 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3871 // We could support this if we wanted to.
3872 gold_error(_("using only one of FILEHDR and PHDRS is "
3873 "not currently supported"));
3877 size_t sizeof_headers
= this->total_header_size(layout
);
3878 uint64_t subtract
= this->header_size_adjustment(lma
,
3880 if (lma
>= subtract
&& vma
>= subtract
)
3887 gold_error(_("sections loaded on first page without room "
3888 "for file and program headers "
3889 "are not supported"));
3892 if (load_seg
!= NULL
)
3893 gold_error(_("using FILEHDR and PHDRS on more than one "
3894 "PT_LOAD segment is not currently supported"));
3898 oseg
->set_addresses(vma
, lma
);
3904 // Add the file header and segment headers to non-load segments
3905 // specified in the PHDRS clause.
3908 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3909 Output_data
* segment_headers
)
3911 gold_assert(this->saw_phdrs_clause());
3912 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3913 p
!= this->phdrs_elements_
->end();
3916 if ((*p
)->type() != elfcpp::PT_LOAD
)
3918 if ((*p
)->includes_phdrs())
3919 (*p
)->segment()->add_initial_output_data(segment_headers
);
3920 if ((*p
)->includes_filehdr())
3921 (*p
)->segment()->add_initial_output_data(file_header
);
3926 // Look for an output section by name and return the address, the load
3927 // address, the alignment, and the size. This is used when an
3928 // expression refers to an output section which was not actually
3929 // created. This returns true if the section was found, false
3933 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3934 uint64_t* load_address
,
3935 uint64_t* addralign
,
3936 uint64_t* size
) const
3938 if (!this->saw_sections_clause_
)
3940 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3941 p
!= this->sections_elements_
->end();
3943 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3949 // Release all Output_segments. This remove all pointers to all
3953 Script_sections::release_segments()
3955 if (this->saw_phdrs_clause())
3957 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3958 p
!= this->phdrs_elements_
->end();
3960 (*p
)->release_segment();
3964 // Print the SECTIONS clause to F for debugging.
3967 Script_sections::print(FILE* f
) const
3969 if (this->phdrs_elements_
!= NULL
)
3971 fprintf(f
, "PHDRS {\n");
3972 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3973 p
!= this->phdrs_elements_
->end();
3979 if (this->memory_regions_
!= NULL
)
3981 fprintf(f
, "MEMORY {\n");
3982 for (Memory_regions::const_iterator m
= this->memory_regions_
->begin();
3983 m
!= this->memory_regions_
->end();
3989 if (!this->saw_sections_clause_
)
3992 fprintf(f
, "SECTIONS {\n");
3994 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3995 p
!= this->sections_elements_
->end();
4002 } // End namespace gold.