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 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
663 // Update the dot symbol while setting section addresses.
665 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
666 uint64_t* dot_value
, uint64_t* dot_alignment
,
667 uint64_t* load_address
)
669 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
670 NULL
, NULL
, dot_alignment
);
671 *load_address
= *dot_value
;
674 // Print for debugging.
679 this->val_
->print(f
);
687 // An assertion in a SECTIONS clause outside of an output section.
689 class Sections_element_assertion
: public Sections_element
692 Sections_element_assertion(Expression
* check
, const char* message
,
694 : assertion_(check
, message
, messagelen
)
697 // Check the assertion.
699 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
700 { this->assertion_
.check(symtab
, layout
); }
702 // Print for debugging.
707 this->assertion_
.print(f
);
711 Script_assertion assertion_
;
714 // An element in an output section in a SECTIONS clause.
716 class Output_section_element
719 // A list of input sections.
720 typedef std::list
<Output_section::Input_section
> Input_section_list
;
722 Output_section_element()
725 virtual ~Output_section_element()
728 // Return whether this element requires an output section to exist.
730 needs_output_section() const
733 // Add any symbol being defined to the symbol table.
735 add_symbols_to_table(Symbol_table
*)
738 // Finalize symbols and check assertions.
740 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
743 // Return whether this element matches FILE_NAME and SECTION_NAME.
744 // The only real implementation is in Output_section_element_input.
746 match_name(const char*, const char*) const
749 // Set section addresses. This includes applying assignments if the
750 // the expression is an absolute value.
752 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
753 uint64_t*, uint64_t*, Output_section
**, std::string
*,
757 // Print the element for debugging purposes.
759 print(FILE* f
) const = 0;
762 // Return a fill string that is LENGTH bytes long, filling it with
765 get_fill_string(const std::string
* fill
, section_size_type length
) const;
769 Output_section_element::get_fill_string(const std::string
* fill
,
770 section_size_type length
) const
772 std::string this_fill
;
773 this_fill
.reserve(length
);
774 while (this_fill
.length() + fill
->length() <= length
)
776 if (this_fill
.length() < length
)
777 this_fill
.append(*fill
, 0, length
- this_fill
.length());
781 // A symbol assignment in an output section.
783 class Output_section_element_assignment
: public Output_section_element
786 Output_section_element_assignment(const char* name
, size_t namelen
,
787 Expression
* val
, bool provide
,
789 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
792 // Add the symbol to the symbol table.
794 add_symbols_to_table(Symbol_table
* symtab
)
795 { this->assignment_
.add_to_table(symtab
); }
797 // Finalize the symbol.
799 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
800 uint64_t* dot_value
, Output_section
** dot_section
)
802 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
806 // Set the section address. There is no section here, but if the
807 // value is absolute, we set the symbol. This permits us to use
808 // absolute symbols when setting dot.
810 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
811 uint64_t, uint64_t* dot_value
, uint64_t*,
812 Output_section
**, std::string
*, Input_section_list
*)
814 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
817 // Print for debugging.
822 this->assignment_
.print(f
);
826 Symbol_assignment assignment_
;
829 // An assignment to the dot symbol in an output section.
831 class Output_section_element_dot_assignment
: public Output_section_element
834 Output_section_element_dot_assignment(Expression
* val
)
838 // Finalize the symbol.
840 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
841 uint64_t* dot_value
, Output_section
** dot_section
)
843 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
844 *dot_section
, dot_section
, NULL
);
847 // Update the dot symbol while setting section addresses.
849 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
850 uint64_t, uint64_t* dot_value
, uint64_t*,
851 Output_section
**, std::string
*, Input_section_list
*);
853 // Print for debugging.
858 this->val_
->print(f
);
866 // Update the dot symbol while setting section addresses.
869 Output_section_element_dot_assignment::set_section_addresses(
870 Symbol_table
* symtab
,
872 Output_section
* output_section
,
875 uint64_t* dot_alignment
,
876 Output_section
** dot_section
,
880 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
881 *dot_value
, *dot_section
,
882 dot_section
, dot_alignment
);
883 if (next_dot
< *dot_value
)
884 gold_error(_("dot may not move backward"));
885 if (next_dot
> *dot_value
&& output_section
!= NULL
)
887 section_size_type length
= convert_to_section_size_type(next_dot
889 Output_section_data
* posd
;
891 posd
= new Output_data_zero_fill(length
, 0);
894 std::string this_fill
= this->get_fill_string(fill
, length
);
895 posd
= new Output_data_const(this_fill
, 0);
897 output_section
->add_output_section_data(posd
);
898 layout
->new_output_section_data_from_script(posd
);
900 *dot_value
= next_dot
;
903 // An assertion in an output section.
905 class Output_section_element_assertion
: public Output_section_element
908 Output_section_element_assertion(Expression
* check
, const char* message
,
910 : assertion_(check
, message
, messagelen
)
917 this->assertion_
.print(f
);
921 Script_assertion assertion_
;
924 // We use a special instance of Output_section_data to handle BYTE,
925 // SHORT, etc. This permits forward references to symbols in the
928 class Output_data_expression
: public Output_section_data
931 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
932 const Symbol_table
* symtab
, const Layout
* layout
,
933 uint64_t dot_value
, Output_section
* dot_section
)
934 : Output_section_data(size
, 0, true),
935 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
936 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
940 // Write the data to the output file.
942 do_write(Output_file
*);
944 // Write the data to a buffer.
946 do_write_to_buffer(unsigned char*);
948 // Write to a map file.
950 do_print_to_mapfile(Mapfile
* mapfile
) const
951 { mapfile
->print_output_data(this, _("** expression")); }
954 template<bool big_endian
>
956 endian_write_to_buffer(uint64_t, unsigned char*);
960 const Symbol_table
* symtab_
;
961 const Layout
* layout_
;
963 Output_section
* dot_section_
;
966 // Write the data element to the output file.
969 Output_data_expression::do_write(Output_file
* of
)
971 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
972 this->write_to_buffer(view
);
973 of
->write_output_view(this->offset(), this->data_size(), view
);
976 // Write the data element to a buffer.
979 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
981 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
982 true, this->dot_value_
,
983 this->dot_section_
, NULL
, NULL
);
985 if (parameters
->target().is_big_endian())
986 this->endian_write_to_buffer
<true>(val
, buf
);
988 this->endian_write_to_buffer
<false>(val
, buf
);
991 template<bool big_endian
>
993 Output_data_expression::endian_write_to_buffer(uint64_t val
,
996 switch (this->data_size())
999 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
1002 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
1005 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
1008 if (parameters
->target().get_size() == 32)
1011 if (this->is_signed_
&& (val
& 0x80000000) != 0)
1012 val
|= 0xffffffff00000000LL
;
1014 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
1021 // A data item in an output section.
1023 class Output_section_element_data
: public Output_section_element
1026 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
1027 : size_(size
), is_signed_(is_signed
), val_(val
)
1030 // If there is a data item, then we must create an output section.
1032 needs_output_section() const
1035 // Finalize symbols--we just need to update dot.
1037 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1039 { *dot_value
+= this->size_
; }
1041 // Store the value in the section.
1043 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
1044 uint64_t* dot_value
, uint64_t*, Output_section
**,
1045 std::string
*, Input_section_list
*);
1047 // Print for debugging.
1052 // The size in bytes.
1054 // Whether the value is signed.
1060 // Store the value in the section.
1063 Output_section_element_data::set_section_addresses(
1064 Symbol_table
* symtab
,
1068 uint64_t* dot_value
,
1070 Output_section
** dot_section
,
1072 Input_section_list
*)
1074 gold_assert(os
!= NULL
);
1075 Output_data_expression
* expression
=
1076 new Output_data_expression(this->size_
, this->is_signed_
, this->val_
,
1077 symtab
, layout
, *dot_value
, *dot_section
);
1078 os
->add_output_section_data(expression
);
1079 layout
->new_output_section_data_from_script(expression
);
1080 *dot_value
+= this->size_
;
1083 // Print for debugging.
1086 Output_section_element_data::print(FILE* f
) const
1089 switch (this->size_
)
1101 if (this->is_signed_
)
1109 fprintf(f
, " %s(", s
);
1110 this->val_
->print(f
);
1114 // A fill value setting in an output section.
1116 class Output_section_element_fill
: public Output_section_element
1119 Output_section_element_fill(Expression
* val
)
1123 // Update the fill value while setting section addresses.
1125 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1126 uint64_t, uint64_t* dot_value
, uint64_t*,
1127 Output_section
** dot_section
,
1128 std::string
* fill
, Input_section_list
*)
1130 Output_section
* fill_section
;
1131 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
1132 *dot_value
, *dot_section
,
1133 &fill_section
, NULL
);
1134 if (fill_section
!= NULL
)
1135 gold_warning(_("fill value is not absolute"));
1136 // FIXME: The GNU linker supports fill values of arbitrary length.
1137 unsigned char fill_buff
[4];
1138 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1139 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
1142 // Print for debugging.
1144 print(FILE* f
) const
1146 fprintf(f
, " FILL(");
1147 this->val_
->print(f
);
1152 // The new fill value.
1156 // An input section specification in an output section
1158 class Output_section_element_input
: public Output_section_element
1161 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
1163 // Finalize symbols--just update the value of the dot symbol.
1165 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1166 Output_section
** dot_section
)
1168 *dot_value
= this->final_dot_value_
;
1169 *dot_section
= this->final_dot_section_
;
1172 // See whether we match FILE_NAME and SECTION_NAME as an input
1175 match_name(const char* file_name
, const char* section_name
) const;
1177 // Set the section address.
1179 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1180 uint64_t subalign
, uint64_t* dot_value
, uint64_t*,
1181 Output_section
**, std::string
* fill
,
1182 Input_section_list
*);
1184 // Print for debugging.
1186 print(FILE* f
) const;
1189 // An input section pattern.
1190 struct Input_section_pattern
1192 std::string pattern
;
1193 bool pattern_is_wildcard
;
1196 Input_section_pattern(const char* patterna
, size_t patternlena
,
1197 Sort_wildcard sorta
)
1198 : pattern(patterna
, patternlena
),
1199 pattern_is_wildcard(is_wildcard_string(this->pattern
.c_str())),
1204 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
1206 // Filename_exclusions is a pair of filename pattern and a bool
1207 // indicating whether the filename is a wildcard.
1208 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
1210 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1211 // indicates whether this is a wildcard pattern.
1213 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
1215 return (is_wildcard_pattern
1216 ? fnmatch(pattern
, string
, 0) == 0
1217 : strcmp(string
, pattern
) == 0);
1220 // See if we match a file name.
1222 match_file_name(const char* file_name
) const;
1224 // The file name pattern. If this is the empty string, we match all
1226 std::string filename_pattern_
;
1227 // Whether the file name pattern is a wildcard.
1228 bool filename_is_wildcard_
;
1229 // How the file names should be sorted. This may only be
1230 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1231 Sort_wildcard filename_sort_
;
1232 // The list of file names to exclude.
1233 Filename_exclusions filename_exclusions_
;
1234 // The list of input section patterns.
1235 Input_section_patterns input_section_patterns_
;
1236 // Whether to keep this section when garbage collecting.
1238 // The value of dot after including all matching sections.
1239 uint64_t final_dot_value_
;
1240 // The section where dot is defined after including all matching
1242 Output_section
* final_dot_section_
;
1245 // Construct Output_section_element_input. The parser records strings
1246 // as pointers into a copy of the script file, which will go away when
1247 // parsing is complete. We make sure they are in std::string objects.
1249 Output_section_element_input::Output_section_element_input(
1250 const Input_section_spec
* spec
,
1252 : filename_pattern_(),
1253 filename_is_wildcard_(false),
1254 filename_sort_(spec
->file
.sort
),
1255 filename_exclusions_(),
1256 input_section_patterns_(),
1258 final_dot_value_(0),
1259 final_dot_section_(NULL
)
1261 // The filename pattern "*" is common, and matches all files. Turn
1262 // it into the empty string.
1263 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
1264 this->filename_pattern_
.assign(spec
->file
.name
.value
,
1265 spec
->file
.name
.length
);
1266 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
.c_str());
1268 if (spec
->input_sections
.exclude
!= NULL
)
1270 for (String_list::const_iterator p
=
1271 spec
->input_sections
.exclude
->begin();
1272 p
!= spec
->input_sections
.exclude
->end();
1275 bool is_wildcard
= is_wildcard_string((*p
).c_str());
1276 this->filename_exclusions_
.push_back(std::make_pair(*p
,
1281 if (spec
->input_sections
.sections
!= NULL
)
1283 Input_section_patterns
& isp(this->input_section_patterns_
);
1284 for (String_sort_list::const_iterator p
=
1285 spec
->input_sections
.sections
->begin();
1286 p
!= spec
->input_sections
.sections
->end();
1288 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
1293 // See whether we match FILE_NAME.
1296 Output_section_element_input::match_file_name(const char* file_name
) const
1298 if (!this->filename_pattern_
.empty())
1300 // If we were called with no filename, we refuse to match a
1301 // pattern which requires a file name.
1302 if (file_name
== NULL
)
1305 if (!match(file_name
, this->filename_pattern_
.c_str(),
1306 this->filename_is_wildcard_
))
1310 if (file_name
!= NULL
)
1312 // Now we have to see whether FILE_NAME matches one of the
1313 // exclusion patterns, if any.
1314 for (Filename_exclusions::const_iterator p
=
1315 this->filename_exclusions_
.begin();
1316 p
!= this->filename_exclusions_
.end();
1319 if (match(file_name
, p
->first
.c_str(), p
->second
))
1327 // See whether we match FILE_NAME and SECTION_NAME.
1330 Output_section_element_input::match_name(const char* file_name
,
1331 const char* section_name
) const
1333 if (!this->match_file_name(file_name
))
1336 // If there are no section name patterns, then we match.
1337 if (this->input_section_patterns_
.empty())
1340 // See whether we match the section name patterns.
1341 for (Input_section_patterns::const_iterator p
=
1342 this->input_section_patterns_
.begin();
1343 p
!= this->input_section_patterns_
.end();
1346 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
1350 // We didn't match any section names, so we didn't match.
1354 // Information we use to sort the input sections.
1356 class Input_section_info
1359 Input_section_info(const Output_section::Input_section
& input_section
)
1360 : input_section_(input_section
), section_name_(),
1361 size_(0), addralign_(1)
1364 // Return the simple input section.
1365 const Output_section::Input_section
&
1366 input_section() const
1367 { return this->input_section_
; }
1369 // Return the object.
1372 { return this->input_section_
.relobj(); }
1374 // Return the section index.
1377 { return this->input_section_
.shndx(); }
1379 // Return the section name.
1381 section_name() const
1382 { return this->section_name_
; }
1384 // Set the section name.
1386 set_section_name(const std::string name
)
1387 { this->section_name_
= name
; }
1389 // Return the section size.
1392 { return this->size_
; }
1394 // Set the section size.
1396 set_size(uint64_t size
)
1397 { this->size_
= size
; }
1399 // Return the address alignment.
1402 { return this->addralign_
; }
1404 // Set the address alignment.
1406 set_addralign(uint64_t addralign
)
1407 { this->addralign_
= addralign
; }
1410 // Input section, can be a relaxed section.
1411 Output_section::Input_section input_section_
;
1412 // Name of the section.
1413 std::string section_name_
;
1416 // Address alignment.
1417 uint64_t addralign_
;
1420 // A class to sort the input sections.
1422 class Input_section_sorter
1425 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
1426 : filename_sort_(filename_sort
), section_sort_(section_sort
)
1430 operator()(const Input_section_info
&, const Input_section_info
&) const;
1433 Sort_wildcard filename_sort_
;
1434 Sort_wildcard section_sort_
;
1438 Input_section_sorter::operator()(const Input_section_info
& isi1
,
1439 const Input_section_info
& isi2
) const
1441 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
1442 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1443 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1444 && isi1
.addralign() == isi2
.addralign()))
1446 if (isi1
.section_name() != isi2
.section_name())
1447 return isi1
.section_name() < isi2
.section_name();
1449 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
1450 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1451 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
1453 if (isi1
.addralign() != isi2
.addralign())
1454 return isi1
.addralign() < isi2
.addralign();
1456 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
1458 if (isi1
.relobj()->name() != isi2
.relobj()->name())
1459 return (isi1
.relobj()->name() < isi2
.relobj()->name());
1462 // Otherwise we leave them in the same order.
1466 // Set the section address. Look in INPUT_SECTIONS for sections which
1467 // match this spec, sort them as specified, and add them to the output
1471 Output_section_element_input::set_section_addresses(
1474 Output_section
* output_section
,
1476 uint64_t* dot_value
,
1478 Output_section
** dot_section
,
1480 Input_section_list
* input_sections
)
1482 // We build a list of sections which match each
1483 // Input_section_pattern.
1485 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
1486 size_t input_pattern_count
= this->input_section_patterns_
.size();
1487 if (input_pattern_count
== 0)
1488 input_pattern_count
= 1;
1489 Matching_sections
matching_sections(input_pattern_count
);
1491 // Look through the list of sections for this output section. Add
1492 // each one which matches to one of the elements of
1493 // MATCHING_SECTIONS.
1495 Input_section_list::iterator p
= input_sections
->begin();
1496 while (p
!= input_sections
->end())
1498 Relobj
* relobj
= p
->relobj();
1499 unsigned int shndx
= p
->shndx();
1500 Input_section_info
isi(*p
);
1502 // Calling section_name and section_addralign is not very
1505 // Lock the object so that we can get information about the
1506 // section. This is OK since we know we are single-threaded
1509 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1510 Task_lock_obj
<Object
> tl(task
, relobj
);
1512 isi
.set_section_name(relobj
->section_name(shndx
));
1513 if (p
->is_relaxed_input_section())
1515 // We use current data size because relxed section sizes may not
1516 // have finalized yet.
1517 isi
.set_size(p
->relaxed_input_section()->current_data_size());
1518 isi
.set_addralign(p
->relaxed_input_section()->addralign());
1522 isi
.set_size(relobj
->section_size(shndx
));
1523 isi
.set_addralign(relobj
->section_addralign(shndx
));
1527 if (!this->match_file_name(relobj
->name().c_str()))
1529 else if (this->input_section_patterns_
.empty())
1531 matching_sections
[0].push_back(isi
);
1532 p
= input_sections
->erase(p
);
1537 for (i
= 0; i
< input_pattern_count
; ++i
)
1539 const Input_section_pattern
&
1540 isp(this->input_section_patterns_
[i
]);
1541 if (match(isi
.section_name().c_str(), isp
.pattern
.c_str(),
1542 isp
.pattern_is_wildcard
))
1546 if (i
>= this->input_section_patterns_
.size())
1550 matching_sections
[i
].push_back(isi
);
1551 p
= input_sections
->erase(p
);
1556 // Look through MATCHING_SECTIONS. Sort each one as specified,
1557 // using a stable sort so that we get the default order when
1558 // sections are otherwise equal. Add each input section to the
1561 uint64_t dot
= *dot_value
;
1562 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1564 if (matching_sections
[i
].empty())
1567 gold_assert(output_section
!= NULL
);
1569 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1570 if (isp
.sort
!= SORT_WILDCARD_NONE
1571 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1572 std::stable_sort(matching_sections
[i
].begin(),
1573 matching_sections
[i
].end(),
1574 Input_section_sorter(this->filename_sort_
,
1577 for (std::vector
<Input_section_info
>::const_iterator p
=
1578 matching_sections
[i
].begin();
1579 p
!= matching_sections
[i
].end();
1582 // Override the original address alignment if SUBALIGN is specified
1583 // and is greater than the original alignment. We need to make a
1584 // copy of the input section to modify the alignment.
1585 Output_section::Input_section
sis(p
->input_section());
1587 uint64_t this_subalign
= sis
.addralign();
1588 if (!sis
.is_input_section())
1589 sis
.output_section_data()->finalize_data_size();
1590 uint64_t data_size
= sis
.data_size();
1591 if (this_subalign
< subalign
)
1593 this_subalign
= subalign
;
1594 sis
.set_addralign(subalign
);
1597 uint64_t address
= align_address(dot
, this_subalign
);
1599 if (address
> dot
&& !fill
->empty())
1601 section_size_type length
=
1602 convert_to_section_size_type(address
- dot
);
1603 std::string this_fill
= this->get_fill_string(fill
, length
);
1604 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1605 output_section
->add_output_section_data(posd
);
1606 layout
->new_output_section_data_from_script(posd
);
1609 output_section
->add_script_input_section(sis
);
1610 dot
= address
+ data_size
;
1614 // An SHF_TLS/SHT_NOBITS section does not take up any
1616 if (output_section
== NULL
1617 || (output_section
->flags() & elfcpp::SHF_TLS
) == 0
1618 || output_section
->type() != elfcpp::SHT_NOBITS
)
1621 this->final_dot_value_
= *dot_value
;
1622 this->final_dot_section_
= *dot_section
;
1625 // Print for debugging.
1628 Output_section_element_input::print(FILE* f
) const
1633 fprintf(f
, "KEEP(");
1635 if (!this->filename_pattern_
.empty())
1637 bool need_close_paren
= false;
1638 switch (this->filename_sort_
)
1640 case SORT_WILDCARD_NONE
:
1642 case SORT_WILDCARD_BY_NAME
:
1643 fprintf(f
, "SORT_BY_NAME(");
1644 need_close_paren
= true;
1650 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1652 if (need_close_paren
)
1656 if (!this->input_section_patterns_
.empty()
1657 || !this->filename_exclusions_
.empty())
1661 bool need_space
= false;
1662 if (!this->filename_exclusions_
.empty())
1664 fprintf(f
, "EXCLUDE_FILE(");
1665 bool need_comma
= false;
1666 for (Filename_exclusions::const_iterator p
=
1667 this->filename_exclusions_
.begin();
1668 p
!= this->filename_exclusions_
.end();
1673 fprintf(f
, "%s", p
->first
.c_str());
1680 for (Input_section_patterns::const_iterator p
=
1681 this->input_section_patterns_
.begin();
1682 p
!= this->input_section_patterns_
.end();
1688 int close_parens
= 0;
1691 case SORT_WILDCARD_NONE
:
1693 case SORT_WILDCARD_BY_NAME
:
1694 fprintf(f
, "SORT_BY_NAME(");
1697 case SORT_WILDCARD_BY_ALIGNMENT
:
1698 fprintf(f
, "SORT_BY_ALIGNMENT(");
1701 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1702 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1705 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1706 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1713 fprintf(f
, "%s", p
->pattern
.c_str());
1715 for (int i
= 0; i
< close_parens
; ++i
)
1730 // An output section.
1732 class Output_section_definition
: public Sections_element
1735 typedef Output_section_element::Input_section_list Input_section_list
;
1737 Output_section_definition(const char* name
, size_t namelen
,
1738 const Parser_output_section_header
* header
);
1740 // Finish the output section with the information in the trailer.
1742 finish(const Parser_output_section_trailer
* trailer
);
1744 // Add a symbol to be defined.
1746 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1747 bool provide
, bool hidden
);
1749 // Add an assignment to the special dot symbol.
1751 add_dot_assignment(Expression
* value
);
1753 // Add an assertion.
1755 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1757 // Add a data item to the current output section.
1759 add_data(int size
, bool is_signed
, Expression
* val
);
1761 // Add a setting for the fill value.
1763 add_fill(Expression
* val
);
1765 // Add an input section specification.
1767 add_input_section(const Input_section_spec
* spec
, bool keep
);
1769 // Return whether the output section is relro.
1772 { return this->is_relro_
; }
1774 // Record that the output section is relro.
1777 { this->is_relro_
= true; }
1779 // Create any required output sections.
1781 create_sections(Layout
*);
1783 // Add any symbols being defined to the symbol table.
1785 add_symbols_to_table(Symbol_table
* symtab
);
1787 // Finalize symbols and check assertions.
1789 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1791 // Return the output section name to use for an input file name and
1794 output_section_name(const char* file_name
, const char* section_name
,
1795 Output_section
***, Script_sections::Section_type
*);
1797 // Initialize OSP with an output section.
1799 orphan_section_init(Orphan_section_placement
* osp
,
1800 Script_sections::Elements_iterator p
)
1801 { osp
->output_section_init(this->name_
, this->output_section_
, p
); }
1803 // Set the section address.
1805 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1806 uint64_t* dot_value
, uint64_t*,
1807 uint64_t* load_address
);
1809 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1810 // this section is constrained, and the input sections do not match,
1811 // return the constraint, and set *POSD.
1813 check_constraint(Output_section_definition
** posd
);
1815 // See if this is the alternate output section for a constrained
1816 // output section. If it is, transfer the Output_section and return
1817 // true. Otherwise return false.
1819 alternate_constraint(Output_section_definition
*, Section_constraint
);
1821 // Get the list of segments to use for an allocated section when
1822 // using a PHDRS clause.
1824 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1826 // Look for an output section by name and return the address, the
1827 // load address, the alignment, and the size. This is used when an
1828 // expression refers to an output section which was not actually
1829 // created. This returns true if the section was found, false
1832 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1835 // Return the associated Output_section if there is one.
1837 get_output_section() const
1838 { return this->output_section_
; }
1840 // Print the contents to the FILE. This is for debugging.
1844 // Return the output section type if specified or Script_sections::ST_NONE.
1845 Script_sections::Section_type
1846 section_type() const;
1848 // Store the memory region to use.
1850 set_memory_region(Memory_region
*, bool set_vma
);
1853 set_section_vma(Expression
* address
)
1854 { this->address_
= address
; }
1857 set_section_lma(Expression
* address
)
1858 { this->load_address_
= address
; }
1861 get_section_name(void) const
1862 { return this->name_
; }
1866 script_section_type_name(Script_section_type
);
1868 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1870 // The output section name.
1872 // The address. This may be NULL.
1873 Expression
* address_
;
1874 // The load address. This may be NULL.
1875 Expression
* load_address_
;
1876 // The alignment. This may be NULL.
1878 // The input section alignment. This may be NULL.
1879 Expression
* subalign_
;
1880 // The constraint, if any.
1881 Section_constraint constraint_
;
1882 // The fill value. This may be NULL.
1884 // The list of segments this section should go into. This may be
1886 String_list
* phdrs_
;
1887 // The list of elements defining the section.
1888 Output_section_elements elements_
;
1889 // The Output_section created for this definition. This will be
1890 // NULL if none was created.
1891 Output_section
* output_section_
;
1892 // The address after it has been evaluated.
1893 uint64_t evaluated_address_
;
1894 // The load address after it has been evaluated.
1895 uint64_t evaluated_load_address_
;
1896 // The alignment after it has been evaluated.
1897 uint64_t evaluated_addralign_
;
1898 // The output section is relro.
1900 // The output section type if specified.
1901 enum Script_section_type script_section_type_
;
1906 Output_section_definition::Output_section_definition(
1909 const Parser_output_section_header
* header
)
1910 : name_(name
, namelen
),
1911 address_(header
->address
),
1912 load_address_(header
->load_address
),
1913 align_(header
->align
),
1914 subalign_(header
->subalign
),
1915 constraint_(header
->constraint
),
1919 output_section_(NULL
),
1920 evaluated_address_(0),
1921 evaluated_load_address_(0),
1922 evaluated_addralign_(0),
1924 script_section_type_(header
->section_type
)
1928 // Finish an output section.
1931 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1933 this->fill_
= trailer
->fill
;
1934 this->phdrs_
= trailer
->phdrs
;
1937 // Add a symbol to be defined.
1940 Output_section_definition::add_symbol_assignment(const char* name
,
1946 Output_section_element
* p
= new Output_section_element_assignment(name
,
1951 this->elements_
.push_back(p
);
1954 // Add an assignment to the special dot symbol.
1957 Output_section_definition::add_dot_assignment(Expression
* value
)
1959 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
1960 this->elements_
.push_back(p
);
1963 // Add an assertion.
1966 Output_section_definition::add_assertion(Expression
* check
,
1967 const char* message
,
1970 Output_section_element
* p
= new Output_section_element_assertion(check
,
1973 this->elements_
.push_back(p
);
1976 // Add a data item to the current output section.
1979 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
1981 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
1983 this->elements_
.push_back(p
);
1986 // Add a setting for the fill value.
1989 Output_section_definition::add_fill(Expression
* val
)
1991 Output_section_element
* p
= new Output_section_element_fill(val
);
1992 this->elements_
.push_back(p
);
1995 // Add an input section specification.
1998 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
2001 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
2002 this->elements_
.push_back(p
);
2005 // Create any required output sections. We need an output section if
2006 // there is a data statement here.
2009 Output_section_definition::create_sections(Layout
* layout
)
2011 if (this->output_section_
!= NULL
)
2013 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2014 p
!= this->elements_
.end();
2017 if ((*p
)->needs_output_section())
2019 const char* name
= this->name_
.c_str();
2020 this->output_section_
=
2021 layout
->make_output_section_for_script(name
, this->section_type());
2027 // Add any symbols being defined to the symbol table.
2030 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
2032 for (Output_section_elements::iterator p
= this->elements_
.begin();
2033 p
!= this->elements_
.end();
2035 (*p
)->add_symbols_to_table(symtab
);
2038 // Finalize symbols and check assertions.
2041 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
2042 const Layout
* layout
,
2043 uint64_t* dot_value
)
2045 if (this->output_section_
!= NULL
)
2046 *dot_value
= this->output_section_
->address();
2049 uint64_t address
= *dot_value
;
2050 if (this->address_
!= NULL
)
2052 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
2056 if (this->align_
!= NULL
)
2058 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
2061 address
= align_address(address
, align
);
2063 *dot_value
= address
;
2066 Output_section
* dot_section
= this->output_section_
;
2067 for (Output_section_elements::iterator p
= this->elements_
.begin();
2068 p
!= this->elements_
.end();
2070 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
2073 // Return the output section name to use for an input section name.
2076 Output_section_definition::output_section_name(
2077 const char* file_name
,
2078 const char* section_name
,
2079 Output_section
*** slot
,
2080 Script_sections::Section_type
* psection_type
)
2082 // Ask each element whether it matches NAME.
2083 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2084 p
!= this->elements_
.end();
2087 if ((*p
)->match_name(file_name
, section_name
))
2089 // We found a match for NAME, which means that it should go
2090 // into this output section.
2091 *slot
= &this->output_section_
;
2092 *psection_type
= this->section_type();
2093 return this->name_
.c_str();
2097 // We don't know about this section name.
2101 // Set the section address. Note that the OUTPUT_SECTION_ field will
2102 // be NULL if no input sections were mapped to this output section.
2103 // We still have to adjust dot and process symbol assignments.
2106 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
2108 uint64_t* dot_value
,
2109 uint64_t* dot_alignment
,
2110 uint64_t* load_address
)
2113 uint64_t old_dot_value
= *dot_value
;
2114 uint64_t old_load_address
= *load_address
;
2116 // Check for --section-start.
2117 bool is_address_set
= false;
2118 if (this->output_section_
!= NULL
)
2120 parameters
->options().section_start(this->output_section_
->name(),
2122 if (!is_address_set
)
2124 if (this->address_
== NULL
)
2125 address
= *dot_value
;
2128 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
2129 *dot_value
, NULL
, NULL
,
2135 if (this->align_
== NULL
)
2137 if (this->output_section_
== NULL
)
2140 align
= this->output_section_
->addralign();
2144 Output_section
* align_section
;
2145 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
2146 NULL
, &align_section
, NULL
);
2147 if (align_section
!= NULL
)
2148 gold_warning(_("alignment of section %s is not absolute"),
2149 this->name_
.c_str());
2150 if (this->output_section_
!= NULL
)
2151 this->output_section_
->set_addralign(align
);
2154 address
= align_address(address
, align
);
2156 uint64_t start_address
= address
;
2158 *dot_value
= address
;
2160 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2161 // forced to zero, regardless of what the linker script wants.
2162 if (this->output_section_
!= NULL
2163 && ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0
2164 || this->output_section_
->is_noload()))
2165 this->output_section_
->set_address(address
);
2167 this->evaluated_address_
= address
;
2168 this->evaluated_addralign_
= align
;
2170 if (this->load_address_
== NULL
)
2171 this->evaluated_load_address_
= address
;
2175 this->load_address_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
2176 this->output_section_
, NULL
, NULL
);
2177 if (this->output_section_
!= NULL
)
2178 this->output_section_
->set_load_address(laddr
);
2179 this->evaluated_load_address_
= laddr
;
2183 if (this->subalign_
== NULL
)
2187 Output_section
* subalign_section
;
2188 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
2190 &subalign_section
, NULL
);
2191 if (subalign_section
!= NULL
)
2192 gold_warning(_("subalign of section %s is not absolute"),
2193 this->name_
.c_str());
2197 if (this->fill_
!= NULL
)
2199 // FIXME: The GNU linker supports fill values of arbitrary
2201 Output_section
* fill_section
;
2202 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
2204 NULL
, &fill_section
,
2206 if (fill_section
!= NULL
)
2207 gold_warning(_("fill of section %s is not absolute"),
2208 this->name_
.c_str());
2209 unsigned char fill_buff
[4];
2210 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
2211 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
2214 Input_section_list input_sections
;
2215 if (this->output_section_
!= NULL
)
2217 // Get the list of input sections attached to this output
2218 // section. This will leave the output section with only
2219 // Output_section_data entries.
2220 address
+= this->output_section_
->get_input_sections(address
,
2223 *dot_value
= address
;
2226 Output_section
* dot_section
= this->output_section_
;
2227 for (Output_section_elements::iterator p
= this->elements_
.begin();
2228 p
!= this->elements_
.end();
2230 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
2231 subalign
, dot_value
, dot_alignment
,
2232 &dot_section
, &fill
, &input_sections
);
2234 gold_assert(input_sections
.empty());
2236 if (this->load_address_
== NULL
|| this->output_section_
== NULL
)
2237 *load_address
= *dot_value
;
2239 *load_address
= (this->output_section_
->load_address()
2240 + (*dot_value
- start_address
));
2242 if (this->output_section_
!= NULL
)
2244 if (this->is_relro_
)
2245 this->output_section_
->set_is_relro();
2247 this->output_section_
->clear_is_relro();
2249 // If this is a NOLOAD section, keep dot and load address unchanged.
2250 if (this->output_section_
->is_noload())
2252 *dot_value
= old_dot_value
;
2253 *load_address
= old_load_address
;
2258 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2259 // this section is constrained, and the input sections do not match,
2260 // return the constraint, and set *POSD.
2263 Output_section_definition::check_constraint(Output_section_definition
** posd
)
2265 switch (this->constraint_
)
2267 case CONSTRAINT_NONE
:
2268 return CONSTRAINT_NONE
;
2270 case CONSTRAINT_ONLY_IF_RO
:
2271 if (this->output_section_
!= NULL
2272 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
2275 return CONSTRAINT_ONLY_IF_RO
;
2277 return CONSTRAINT_NONE
;
2279 case CONSTRAINT_ONLY_IF_RW
:
2280 if (this->output_section_
!= NULL
2281 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
2284 return CONSTRAINT_ONLY_IF_RW
;
2286 return CONSTRAINT_NONE
;
2288 case CONSTRAINT_SPECIAL
:
2289 if (this->output_section_
!= NULL
)
2290 gold_error(_("SPECIAL constraints are not implemented"));
2291 return CONSTRAINT_NONE
;
2298 // See if this is the alternate output section for a constrained
2299 // output section. If it is, transfer the Output_section and return
2300 // true. Otherwise return false.
2303 Output_section_definition::alternate_constraint(
2304 Output_section_definition
* posd
,
2305 Section_constraint constraint
)
2307 if (this->name_
!= posd
->name_
)
2312 case CONSTRAINT_ONLY_IF_RO
:
2313 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
2317 case CONSTRAINT_ONLY_IF_RW
:
2318 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
2326 // We have found the alternate constraint. We just need to move
2327 // over the Output_section. When constraints are used properly,
2328 // THIS should not have an output_section pointer, as all the input
2329 // sections should have matched the other definition.
2331 if (this->output_section_
!= NULL
)
2332 gold_error(_("mismatched definition for constrained sections"));
2334 this->output_section_
= posd
->output_section_
;
2335 posd
->output_section_
= NULL
;
2337 if (this->is_relro_
)
2338 this->output_section_
->set_is_relro();
2340 this->output_section_
->clear_is_relro();
2345 // Get the list of segments to use for an allocated section when using
2349 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
2352 // Update phdrs_list even if we don't have an output section. It
2353 // might be used by the following sections.
2354 if (this->phdrs_
!= NULL
)
2355 *phdrs_list
= this->phdrs_
;
2357 if (this->output_section_
== NULL
)
2359 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2362 return this->output_section_
;
2365 // Look for an output section by name and return the address, the load
2366 // address, the alignment, and the size. This is used when an
2367 // expression refers to an output section which was not actually
2368 // created. This returns true if the section was found, false
2372 Output_section_definition::get_output_section_info(const char* name
,
2374 uint64_t* load_address
,
2375 uint64_t* addralign
,
2376 uint64_t* size
) const
2378 if (this->name_
!= name
)
2381 if (this->output_section_
!= NULL
)
2383 *address
= this->output_section_
->address();
2384 if (this->output_section_
->has_load_address())
2385 *load_address
= this->output_section_
->load_address();
2387 *load_address
= *address
;
2388 *addralign
= this->output_section_
->addralign();
2389 *size
= this->output_section_
->current_data_size();
2393 *address
= this->evaluated_address_
;
2394 *load_address
= this->evaluated_load_address_
;
2395 *addralign
= this->evaluated_addralign_
;
2402 // Print for debugging.
2405 Output_section_definition::print(FILE* f
) const
2407 fprintf(f
, " %s ", this->name_
.c_str());
2409 if (this->address_
!= NULL
)
2411 this->address_
->print(f
);
2415 if (this->script_section_type_
!= SCRIPT_SECTION_TYPE_NONE
)
2417 this->script_section_type_name(this->script_section_type_
));
2421 if (this->load_address_
!= NULL
)
2424 this->load_address_
->print(f
);
2428 if (this->align_
!= NULL
)
2430 fprintf(f
, "ALIGN(");
2431 this->align_
->print(f
);
2435 if (this->subalign_
!= NULL
)
2437 fprintf(f
, "SUBALIGN(");
2438 this->subalign_
->print(f
);
2444 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2445 p
!= this->elements_
.end();
2451 if (this->fill_
!= NULL
)
2454 this->fill_
->print(f
);
2457 if (this->phdrs_
!= NULL
)
2459 for (String_list::const_iterator p
= this->phdrs_
->begin();
2460 p
!= this->phdrs_
->end();
2462 fprintf(f
, " :%s", p
->c_str());
2468 Script_sections::Section_type
2469 Output_section_definition::section_type() const
2471 switch (this->script_section_type_
)
2473 case SCRIPT_SECTION_TYPE_NONE
:
2474 return Script_sections::ST_NONE
;
2475 case SCRIPT_SECTION_TYPE_NOLOAD
:
2476 return Script_sections::ST_NOLOAD
;
2477 case SCRIPT_SECTION_TYPE_COPY
:
2478 case SCRIPT_SECTION_TYPE_DSECT
:
2479 case SCRIPT_SECTION_TYPE_INFO
:
2480 case SCRIPT_SECTION_TYPE_OVERLAY
:
2481 // There are not really support so we treat them as ST_NONE. The
2482 // parse should have issued errors for them already.
2483 return Script_sections::ST_NONE
;
2489 // Return the name of a script section type.
2492 Output_section_definition::script_section_type_name(
2493 Script_section_type script_section_type
)
2495 switch (script_section_type
)
2497 case SCRIPT_SECTION_TYPE_NONE
:
2499 case SCRIPT_SECTION_TYPE_NOLOAD
:
2501 case SCRIPT_SECTION_TYPE_DSECT
:
2503 case SCRIPT_SECTION_TYPE_COPY
:
2505 case SCRIPT_SECTION_TYPE_INFO
:
2507 case SCRIPT_SECTION_TYPE_OVERLAY
:
2515 Output_section_definition::set_memory_region(Memory_region
* mr
, bool set_vma
)
2517 gold_assert(mr
!= NULL
);
2518 // Add the current section to the specified region's list.
2519 mr
->add_section(this, set_vma
);
2522 // An output section created to hold orphaned input sections. These
2523 // do not actually appear in linker scripts. However, for convenience
2524 // when setting the output section addresses, we put a marker to these
2525 // sections in the appropriate place in the list of SECTIONS elements.
2527 class Orphan_output_section
: public Sections_element
2530 Orphan_output_section(Output_section
* os
)
2534 // Return whether the orphan output section is relro. We can just
2535 // check the output section because we always set the flag, if
2536 // needed, just after we create the Orphan_output_section.
2539 { return this->os_
->is_relro(); }
2541 // Initialize OSP with an output section. This should have been
2544 orphan_section_init(Orphan_section_placement
*,
2545 Script_sections::Elements_iterator
)
2546 { gold_unreachable(); }
2548 // Set section addresses.
2550 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*,
2553 // Get the list of segments to use for an allocated section when
2554 // using a PHDRS clause.
2556 allocate_to_segment(String_list
**, bool*);
2558 // Return the associated Output_section.
2560 get_output_section() const
2561 { return this->os_
; }
2563 // Print for debugging.
2565 print(FILE* f
) const
2567 fprintf(f
, " marker for orphaned output section %s\n",
2572 Output_section
* os_
;
2575 // Set section addresses.
2578 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2579 uint64_t* dot_value
,
2581 uint64_t* load_address
)
2583 typedef std::list
<Output_section::Input_section
> Input_section_list
;
2585 bool have_load_address
= *load_address
!= *dot_value
;
2587 uint64_t address
= *dot_value
;
2588 address
= align_address(address
, this->os_
->addralign());
2590 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2592 this->os_
->set_address(address
);
2593 if (have_load_address
)
2594 this->os_
->set_load_address(align_address(*load_address
,
2595 this->os_
->addralign()));
2598 Input_section_list input_sections
;
2599 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2601 for (Input_section_list::iterator p
= input_sections
.begin();
2602 p
!= input_sections
.end();
2605 uint64_t addralign
= p
->addralign();
2606 if (!p
->is_input_section())
2607 p
->output_section_data()->finalize_data_size();
2608 uint64_t size
= p
->data_size();
2609 address
= align_address(address
, addralign
);
2610 this->os_
->add_script_input_section(*p
);
2614 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2615 if (this->os_
== NULL
2616 || (this->os_
->flags() & elfcpp::SHF_TLS
) == 0
2617 || this->os_
->type() != elfcpp::SHT_NOBITS
)
2619 if (!have_load_address
)
2620 *load_address
= address
;
2622 *load_address
+= address
- *dot_value
;
2624 *dot_value
= address
;
2628 // Get the list of segments to use for an allocated section when using
2629 // a PHDRS clause. If this is an allocated section, return the
2630 // Output_section. We don't change the list of segments.
2633 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2635 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2641 // Class Phdrs_element. A program header from a PHDRS clause.
2646 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2647 bool includes_filehdr
, bool includes_phdrs
,
2648 bool is_flags_valid
, unsigned int flags
,
2649 Expression
* load_address
)
2650 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2651 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2652 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2656 // Return the name of this segment.
2659 { return this->name_
; }
2661 // Return the type of the segment.
2664 { return this->type_
; }
2666 // Whether to include the file header.
2668 includes_filehdr() const
2669 { return this->includes_filehdr_
; }
2671 // Whether to include the program headers.
2673 includes_phdrs() const
2674 { return this->includes_phdrs_
; }
2676 // Return whether there is a load address.
2678 has_load_address() const
2679 { return this->load_address_
!= NULL
; }
2681 // Evaluate the load address expression if there is one.
2683 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2685 if (this->load_address_
!= NULL
)
2686 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2690 // Return the load address.
2692 load_address() const
2694 gold_assert(this->load_address_
!= NULL
);
2695 return this->load_address_value_
;
2698 // Create the segment.
2700 create_segment(Layout
* layout
)
2702 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2703 return this->segment_
;
2706 // Return the segment.
2709 { return this->segment_
; }
2711 // Release the segment.
2714 { this->segment_
= NULL
; }
2716 // Set the segment flags if appropriate.
2718 set_flags_if_valid()
2720 if (this->is_flags_valid_
)
2721 this->segment_
->set_flags(this->flags_
);
2724 // Print for debugging.
2729 // The name used in the script.
2731 // The type of the segment (PT_LOAD, etc.).
2733 // Whether this segment includes the file header.
2734 bool includes_filehdr_
;
2735 // Whether this segment includes the section headers.
2736 bool includes_phdrs_
;
2737 // Whether the flags were explicitly specified.
2738 bool is_flags_valid_
;
2739 // The flags for this segment (PF_R, etc.) if specified.
2740 unsigned int flags_
;
2741 // The expression for the load address for this segment. This may
2743 Expression
* load_address_
;
2744 // The actual load address from evaluating the expression.
2745 uint64_t load_address_value_
;
2746 // The segment itself.
2747 Output_segment
* segment_
;
2750 // Print for debugging.
2753 Phdrs_element::print(FILE* f
) const
2755 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
2756 if (this->includes_filehdr_
)
2757 fprintf(f
, " FILEHDR");
2758 if (this->includes_phdrs_
)
2759 fprintf(f
, " PHDRS");
2760 if (this->is_flags_valid_
)
2761 fprintf(f
, " FLAGS(%u)", this->flags_
);
2762 if (this->load_address_
!= NULL
)
2765 this->load_address_
->print(f
);
2771 // Add a memory region.
2774 Script_sections::add_memory_region(const char* name
, size_t namelen
,
2775 unsigned int attributes
,
2776 Expression
* start
, Expression
* length
)
2778 if (this->memory_regions_
== NULL
)
2779 this->memory_regions_
= new Memory_regions();
2780 else if (this->find_memory_region(name
, namelen
))
2782 gold_error (_("region '%.*s' already defined"), static_cast<int>(namelen
),
2784 // FIXME: Add a GOLD extension to allow multiple regions with the same
2785 // name. This would amount to a single region covering disjoint blocks
2786 // of memory, which is useful for embedded devices.
2789 // FIXME: Check the length and start values. Currently we allow
2790 // non-constant expressions for these values, whereas LD does not.
2792 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
2793 // describe a region that packs from the end address going down, rather
2794 // than the start address going up. This would be useful for embedded
2797 this->memory_regions_
->push_back(new Memory_region(name
, namelen
, attributes
,
2801 // Find a memory region.
2804 Script_sections::find_memory_region(const char* name
, size_t namelen
)
2806 if (this->memory_regions_
== NULL
)
2809 for (Memory_regions::const_iterator m
= this->memory_regions_
->begin();
2810 m
!= this->memory_regions_
->end();
2812 if ((*m
)->name_match(name
, namelen
))
2818 // Find a memory region's origin.
2821 Script_sections::find_memory_region_origin(const char* name
, size_t namelen
)
2823 Memory_region
* mr
= find_memory_region(name
, namelen
);
2827 return mr
->start_address();
2830 // Find a memory region's length.
2833 Script_sections::find_memory_region_length(const char* name
, size_t namelen
)
2835 Memory_region
* mr
= find_memory_region(name
, namelen
);
2839 return mr
->length();
2842 // Set the memory region to use for the current section.
2845 Script_sections::set_memory_region(Memory_region
* mr
, bool set_vma
)
2847 gold_assert(!this->sections_elements_
->empty());
2848 this->sections_elements_
->back()->set_memory_region(mr
, set_vma
);
2851 // Class Script_sections.
2853 Script_sections::Script_sections()
2854 : saw_sections_clause_(false),
2855 in_sections_clause_(false),
2856 sections_elements_(NULL
),
2857 output_section_(NULL
),
2858 memory_regions_(NULL
),
2859 phdrs_elements_(NULL
),
2860 orphan_section_placement_(NULL
),
2861 data_segment_align_start_(),
2862 saw_data_segment_align_(false),
2863 saw_relro_end_(false),
2864 saw_segment_start_expression_(false)
2868 // Start a SECTIONS clause.
2871 Script_sections::start_sections()
2873 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
2874 this->saw_sections_clause_
= true;
2875 this->in_sections_clause_
= true;
2876 if (this->sections_elements_
== NULL
)
2877 this->sections_elements_
= new Sections_elements
;
2880 // Finish a SECTIONS clause.
2883 Script_sections::finish_sections()
2885 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
2886 this->in_sections_clause_
= false;
2889 // Add a symbol to be defined.
2892 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
2893 Expression
* val
, bool provide
,
2896 if (this->output_section_
!= NULL
)
2897 this->output_section_
->add_symbol_assignment(name
, length
, val
,
2901 Sections_element
* p
= new Sections_element_assignment(name
, length
,
2904 this->sections_elements_
->push_back(p
);
2908 // Add an assignment to the special dot symbol.
2911 Script_sections::add_dot_assignment(Expression
* val
)
2913 if (this->output_section_
!= NULL
)
2914 this->output_section_
->add_dot_assignment(val
);
2917 // The GNU linker permits assignments to . to appears outside of
2918 // a SECTIONS clause, and treats it as appearing inside, so
2919 // sections_elements_ may be NULL here.
2920 if (this->sections_elements_
== NULL
)
2922 this->sections_elements_
= new Sections_elements
;
2923 this->saw_sections_clause_
= true;
2926 Sections_element
* p
= new Sections_element_dot_assignment(val
);
2927 this->sections_elements_
->push_back(p
);
2931 // Add an assertion.
2934 Script_sections::add_assertion(Expression
* check
, const char* message
,
2937 if (this->output_section_
!= NULL
)
2938 this->output_section_
->add_assertion(check
, message
, messagelen
);
2941 Sections_element
* p
= new Sections_element_assertion(check
, message
,
2943 this->sections_elements_
->push_back(p
);
2947 // Start processing entries for an output section.
2950 Script_sections::start_output_section(
2953 const Parser_output_section_header
* header
)
2955 Output_section_definition
* posd
= new Output_section_definition(name
,
2958 this->sections_elements_
->push_back(posd
);
2959 gold_assert(this->output_section_
== NULL
);
2960 this->output_section_
= posd
;
2963 // Stop processing entries for an output section.
2966 Script_sections::finish_output_section(
2967 const Parser_output_section_trailer
* trailer
)
2969 gold_assert(this->output_section_
!= NULL
);
2970 this->output_section_
->finish(trailer
);
2971 this->output_section_
= NULL
;
2974 // Add a data item to the current output section.
2977 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
2979 gold_assert(this->output_section_
!= NULL
);
2980 this->output_section_
->add_data(size
, is_signed
, val
);
2983 // Add a fill value setting to the current output section.
2986 Script_sections::add_fill(Expression
* val
)
2988 gold_assert(this->output_section_
!= NULL
);
2989 this->output_section_
->add_fill(val
);
2992 // Add an input section specification to the current output section.
2995 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
2997 gold_assert(this->output_section_
!= NULL
);
2998 this->output_section_
->add_input_section(spec
, keep
);
3001 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
3002 // subsequent output sections may be relro.
3005 Script_sections::data_segment_align()
3007 if (this->saw_data_segment_align_
)
3008 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3009 gold_assert(!this->sections_elements_
->empty());
3010 Sections_elements::iterator p
= this->sections_elements_
->end();
3012 this->data_segment_align_start_
= p
;
3013 this->saw_data_segment_align_
= true;
3016 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
3017 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3020 Script_sections::data_segment_relro_end()
3022 if (this->saw_relro_end_
)
3023 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3024 "in a linker script"));
3025 this->saw_relro_end_
= true;
3027 if (!this->saw_data_segment_align_
)
3028 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3031 Sections_elements::iterator p
= this->data_segment_align_start_
;
3032 for (++p
; p
!= this->sections_elements_
->end(); ++p
)
3033 (*p
)->set_is_relro();
3037 // Create any required sections.
3040 Script_sections::create_sections(Layout
* layout
)
3042 if (!this->saw_sections_clause_
)
3044 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3045 p
!= this->sections_elements_
->end();
3047 (*p
)->create_sections(layout
);
3050 // Add any symbols we are defining to the symbol table.
3053 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
3055 if (!this->saw_sections_clause_
)
3057 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3058 p
!= this->sections_elements_
->end();
3060 (*p
)->add_symbols_to_table(symtab
);
3063 // Finalize symbols and check assertions.
3066 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
3068 if (!this->saw_sections_clause_
)
3070 uint64_t dot_value
= 0;
3071 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3072 p
!= this->sections_elements_
->end();
3074 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
3077 // Return the name of the output section to use for an input file name
3078 // and section name.
3081 Script_sections::output_section_name(
3082 const char* file_name
,
3083 const char* section_name
,
3084 Output_section
*** output_section_slot
,
3085 Script_sections::Section_type
* psection_type
)
3087 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3088 p
!= this->sections_elements_
->end();
3091 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
3092 output_section_slot
,
3097 // The special name /DISCARD/ means that the input section
3098 // should be discarded.
3099 if (strcmp(ret
, "/DISCARD/") == 0)
3101 *output_section_slot
= NULL
;
3102 *psection_type
= Script_sections::ST_NONE
;
3109 // If we couldn't find a mapping for the name, the output section
3110 // gets the name of the input section.
3112 *output_section_slot
= NULL
;
3113 *psection_type
= Script_sections::ST_NONE
;
3115 return section_name
;
3118 // Place a marker for an orphan output section into the SECTIONS
3122 Script_sections::place_orphan(Output_section
* os
)
3124 Orphan_section_placement
* osp
= this->orphan_section_placement_
;
3127 // Initialize the Orphan_section_placement structure.
3128 osp
= new Orphan_section_placement();
3129 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3130 p
!= this->sections_elements_
->end();
3132 (*p
)->orphan_section_init(osp
, p
);
3133 gold_assert(!this->sections_elements_
->empty());
3134 Sections_elements::iterator last
= this->sections_elements_
->end();
3136 osp
->last_init(last
);
3137 this->orphan_section_placement_
= osp
;
3140 Orphan_output_section
* orphan
= new Orphan_output_section(os
);
3142 // Look for where to put ORPHAN.
3143 Sections_elements::iterator
* where
;
3144 if (osp
->find_place(os
, &where
))
3146 if ((**where
)->is_relro())
3149 os
->clear_is_relro();
3151 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3152 // so that the next one goes after this one.
3153 Sections_elements::iterator p
= *where
;
3154 gold_assert(p
!= this->sections_elements_
->end());
3156 *where
= this->sections_elements_
->insert(p
, orphan
);
3160 os
->clear_is_relro();
3161 // We don't have a place to put this orphan section. Put it,
3162 // and all other sections like it, at the end, but before the
3163 // sections which always come at the end.
3164 Sections_elements::iterator last
= osp
->last_place();
3165 *where
= this->sections_elements_
->insert(last
, orphan
);
3169 // Set the addresses of all the output sections. Walk through all the
3170 // elements, tracking the dot symbol. Apply assignments which set
3171 // absolute symbol values, in case they are used when setting dot.
3172 // Fill in data statement values. As we find output sections, set the
3173 // address, set the address of all associated input sections, and
3174 // update dot. Return the segment which should hold the file header
3175 // and segment headers, if any.
3178 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
3180 gold_assert(this->saw_sections_clause_
);
3182 // Walk the memory regions specified in this script, if any.
3183 if (this->memory_regions_
!= NULL
)
3185 for (Memory_regions::const_iterator mr
= this->memory_regions_
->begin();
3186 mr
!= this->memory_regions_
->end();
3189 // FIXME: What should we do with the attributes of the regions ?
3191 // For each region, set the VMA of the sections associated with it.
3192 for (Memory_region::Section_list::const_iterator s
=
3193 (*mr
)->get_vma_section_list_start();
3194 s
!= (*mr
)->get_vma_section_list_end();
3197 (*s
)->set_section_vma((*mr
)->get_current_vma_address());
3198 (*mr
)->increment_vma_offset((*s
)->get_section_name(),
3199 (*s
)->get_output_section()->current_data_size(),
3203 // Similarly, set the LMA values.
3204 for (Memory_region::Section_list::const_iterator s
=
3205 (*mr
)->get_lma_section_list_start();
3206 s
!= (*mr
)->get_lma_section_list_end();
3209 (*s
)->set_section_lma((*mr
)->get_current_lma_address());
3210 (*mr
)->increment_lma_offset((*s
)->get_section_name(),
3211 (*s
)->get_output_section()->current_data_size(),
3217 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3218 // for our representation.
3219 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3220 p
!= this->sections_elements_
->end();
3223 Output_section_definition
* posd
;
3224 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
3225 if (failed_constraint
!= CONSTRAINT_NONE
)
3227 Sections_elements::iterator q
;
3228 for (q
= this->sections_elements_
->begin();
3229 q
!= this->sections_elements_
->end();
3234 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
3239 if (q
== this->sections_elements_
->end())
3240 gold_error(_("no matching section constraint"));
3244 // Force the alignment of the first TLS section to be the maximum
3245 // alignment of all TLS sections.
3246 Output_section
* first_tls
= NULL
;
3247 uint64_t tls_align
= 0;
3248 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3249 p
!= this->sections_elements_
->end();
3252 Output_section
* os
= (*p
)->get_output_section();
3253 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
3255 if (first_tls
== NULL
)
3257 if (os
->addralign() > tls_align
)
3258 tls_align
= os
->addralign();
3261 if (first_tls
!= NULL
)
3262 first_tls
->set_addralign(tls_align
);
3264 // For a relocatable link, we implicitly set dot to zero.
3265 uint64_t dot_value
= 0;
3266 uint64_t dot_alignment
= 0;
3267 uint64_t load_address
= 0;
3269 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3270 // to set section addresses. If the script has any SEGMENT_START
3271 // expression, we do not set the section addresses.
3272 bool use_tsection_options
=
3273 (!this->saw_segment_start_expression_
3274 && (parameters
->options().user_set_Ttext()
3275 || parameters
->options().user_set_Tdata()
3276 || parameters
->options().user_set_Tbss()));
3278 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3279 p
!= this->sections_elements_
->end();
3282 Output_section
* os
= (*p
)->get_output_section();
3284 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3285 // the special sections by names and doing dot assignments.
3286 if (use_tsection_options
3288 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
3290 uint64_t new_dot_value
= dot_value
;
3292 if (parameters
->options().user_set_Ttext()
3293 && strcmp(os
->name(), ".text") == 0)
3294 new_dot_value
= parameters
->options().Ttext();
3295 else if (parameters
->options().user_set_Tdata()
3296 && strcmp(os
->name(), ".data") == 0)
3297 new_dot_value
= parameters
->options().Tdata();
3298 else if (parameters
->options().user_set_Tbss()
3299 && strcmp(os
->name(), ".bss") == 0)
3300 new_dot_value
= parameters
->options().Tbss();
3302 // Update dot and load address if necessary.
3303 if (new_dot_value
< dot_value
)
3304 gold_error(_("dot may not move backward"));
3305 else if (new_dot_value
!= dot_value
)
3307 dot_value
= new_dot_value
;
3308 load_address
= new_dot_value
;
3312 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &dot_alignment
,
3316 if (this->phdrs_elements_
!= NULL
)
3318 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3319 p
!= this->phdrs_elements_
->end();
3321 (*p
)->eval_load_address(symtab
, layout
);
3324 return this->create_segments(layout
, dot_alignment
);
3327 // Sort the sections in order to put them into segments.
3329 class Sort_output_sections
3333 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
3337 Sort_output_sections::operator()(const Output_section
* os1
,
3338 const Output_section
* os2
) const
3340 // Sort first by the load address.
3341 uint64_t lma1
= (os1
->has_load_address()
3342 ? os1
->load_address()
3344 uint64_t lma2
= (os2
->has_load_address()
3345 ? os2
->load_address()
3350 // Then sort by the virtual address.
3351 if (os1
->address() != os2
->address())
3352 return os1
->address() < os2
->address();
3354 // Sort TLS sections to the end.
3355 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
3356 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
3360 // Sort PROGBITS before NOBITS.
3361 if (os1
->type() == elfcpp::SHT_PROGBITS
&& os2
->type() == elfcpp::SHT_NOBITS
)
3363 if (os1
->type() == elfcpp::SHT_NOBITS
&& os2
->type() == elfcpp::SHT_PROGBITS
)
3366 // Sort non-NOLOAD before NOLOAD.
3367 if (os1
->is_noload() && !os2
->is_noload())
3369 if (!os1
->is_noload() && os2
->is_noload())
3372 // Otherwise we don't care.
3376 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3377 // We treat a section with the SHF_TLS flag set as taking up space
3378 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3379 // space for them in the file.
3382 Script_sections::is_bss_section(const Output_section
* os
)
3384 return (os
->type() == elfcpp::SHT_NOBITS
3385 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
3388 // Return the size taken by the file header and the program headers.
3391 Script_sections::total_header_size(Layout
* layout
) const
3393 size_t segment_count
= layout
->segment_count();
3394 size_t file_header_size
;
3395 size_t segment_headers_size
;
3396 if (parameters
->target().get_size() == 32)
3398 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
3399 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
3401 else if (parameters
->target().get_size() == 64)
3403 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
3404 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
3409 return file_header_size
+ segment_headers_size
;
3412 // Return the amount we have to subtract from the LMA to accomodate
3413 // headers of the given size. The complication is that the file
3414 // header have to be at the start of a page, as otherwise it will not
3415 // be at the start of the file.
3418 Script_sections::header_size_adjustment(uint64_t lma
,
3419 size_t sizeof_headers
) const
3421 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3422 uint64_t hdr_lma
= lma
- sizeof_headers
;
3423 hdr_lma
&= ~(abi_pagesize
- 1);
3424 return lma
- hdr_lma
;
3427 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3428 // the segment which should hold the file header and segment headers,
3432 Script_sections::create_segments(Layout
* layout
, uint64_t dot_alignment
)
3434 gold_assert(this->saw_sections_clause_
);
3436 if (parameters
->options().relocatable())
3439 if (this->saw_phdrs_clause())
3440 return create_segments_from_phdrs_clause(layout
, dot_alignment
);
3442 Layout::Section_list sections
;
3443 layout
->get_allocated_sections(§ions
);
3445 // Sort the sections by address.
3446 std::stable_sort(sections
.begin(), sections
.end(), Sort_output_sections());
3448 this->create_note_and_tls_segments(layout
, §ions
);
3450 // Walk through the sections adding them to PT_LOAD segments.
3451 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3452 Output_segment
* first_seg
= NULL
;
3453 Output_segment
* current_seg
= NULL
;
3454 bool is_current_seg_readonly
= true;
3455 Layout::Section_list::iterator plast
= sections
.end();
3456 uint64_t last_vma
= 0;
3457 uint64_t last_lma
= 0;
3458 uint64_t last_size
= 0;
3459 for (Layout::Section_list::iterator p
= sections
.begin();
3460 p
!= sections
.end();
3463 const uint64_t vma
= (*p
)->address();
3464 const uint64_t lma
= ((*p
)->has_load_address()
3465 ? (*p
)->load_address()
3467 const uint64_t size
= (*p
)->current_data_size();
3469 bool need_new_segment
;
3470 if (current_seg
== NULL
)
3471 need_new_segment
= true;
3472 else if (lma
- vma
!= last_lma
- last_vma
)
3474 // This section has a different LMA relationship than the
3475 // last one; we need a new segment.
3476 need_new_segment
= true;
3478 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
3479 < align_address(lma
, abi_pagesize
))
3481 // Putting this section in the segment would require
3483 need_new_segment
= true;
3485 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
3487 // A non-BSS section can not follow a BSS section in the
3489 need_new_segment
= true;
3491 else if (is_current_seg_readonly
3492 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
3493 && !parameters
->options().omagic())
3495 // Don't put a writable section in the same segment as a
3496 // non-writable section.
3497 need_new_segment
= true;
3501 // Otherwise, reuse the existing segment.
3502 need_new_segment
= false;
3505 elfcpp::Elf_Word seg_flags
=
3506 Layout::section_flags_to_segment((*p
)->flags());
3508 if (need_new_segment
)
3510 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3512 current_seg
->set_addresses(vma
, lma
);
3513 current_seg
->set_minimum_p_align(dot_alignment
);
3514 if (first_seg
== NULL
)
3515 first_seg
= current_seg
;
3516 is_current_seg_readonly
= true;
3519 current_seg
->add_output_section_to_load(layout
, *p
, seg_flags
);
3521 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
3522 is_current_seg_readonly
= false;
3530 // An ELF program should work even if the program headers are not in
3531 // a PT_LOAD segment. However, it appears that the Linux kernel
3532 // does not set the AT_PHDR auxiliary entry in that case. It sets
3533 // the load address to p_vaddr - p_offset of the first PT_LOAD
3534 // segment. It then sets AT_PHDR to the load address plus the
3535 // offset to the program headers, e_phoff in the file header. This
3536 // fails when the program headers appear in the file before the
3537 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3538 // segment to hold the file header and the program headers. This is
3539 // effectively what the GNU linker does, and it is slightly more
3540 // efficient in any case. We try to use the first PT_LOAD segment
3541 // if we can, otherwise we make a new one.
3543 if (first_seg
== NULL
)
3546 // -n or -N mean that the program is not demand paged and there is
3547 // no need to put the program headers in a PT_LOAD segment.
3548 if (parameters
->options().nmagic() || parameters
->options().omagic())
3551 size_t sizeof_headers
= this->total_header_size(layout
);
3553 uint64_t vma
= first_seg
->vaddr();
3554 uint64_t lma
= first_seg
->paddr();
3556 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
3558 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
3560 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3564 // If there is no room to squeeze in the headers, then punt. The
3565 // resulting executable probably won't run on GNU/Linux, but we
3566 // trust that the user knows what they are doing.
3567 if (lma
< subtract
|| vma
< subtract
)
3570 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3572 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3577 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3578 // segment if there are any SHT_TLS sections.
3581 Script_sections::create_note_and_tls_segments(
3583 const Layout::Section_list
* sections
)
3585 gold_assert(!this->saw_phdrs_clause());
3587 bool saw_tls
= false;
3588 for (Layout::Section_list::const_iterator p
= sections
->begin();
3589 p
!= sections
->end();
3592 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3594 elfcpp::Elf_Word seg_flags
=
3595 Layout::section_flags_to_segment((*p
)->flags());
3596 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
3598 oseg
->add_output_section_to_nonload(*p
, seg_flags
);
3600 // Incorporate any subsequent SHT_NOTE sections, in the
3601 // hopes that the script is sensible.
3602 Layout::Section_list::const_iterator pnext
= p
+ 1;
3603 while (pnext
!= sections
->end()
3604 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
3606 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3607 oseg
->add_output_section_to_nonload(*pnext
, seg_flags
);
3613 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3616 gold_error(_("TLS sections are not adjacent"));
3618 elfcpp::Elf_Word seg_flags
=
3619 Layout::section_flags_to_segment((*p
)->flags());
3620 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
3622 oseg
->add_output_section_to_nonload(*p
, seg_flags
);
3624 Layout::Section_list::const_iterator pnext
= p
+ 1;
3625 while (pnext
!= sections
->end()
3626 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
3628 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3629 oseg
->add_output_section_to_nonload(*pnext
, seg_flags
);
3639 // Add a program header. The PHDRS clause is syntactically distinct
3640 // from the SECTIONS clause, but we implement it with the SECTIONS
3641 // support because PHDRS is useless if there is no SECTIONS clause.
3644 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
3645 bool includes_filehdr
, bool includes_phdrs
,
3646 bool is_flags_valid
, unsigned int flags
,
3647 Expression
* load_address
)
3649 if (this->phdrs_elements_
== NULL
)
3650 this->phdrs_elements_
= new Phdrs_elements();
3651 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
3654 is_flags_valid
, flags
,
3658 // Return the number of segments we expect to create based on the
3659 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3662 Script_sections::expected_segment_count(const Layout
* layout
) const
3664 if (this->saw_phdrs_clause())
3665 return this->phdrs_elements_
->size();
3667 Layout::Section_list sections
;
3668 layout
->get_allocated_sections(§ions
);
3670 // We assume that we will need two PT_LOAD segments.
3673 bool saw_note
= false;
3674 bool saw_tls
= false;
3675 for (Layout::Section_list::const_iterator p
= sections
.begin();
3676 p
!= sections
.end();
3679 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3681 // Assume that all note sections will fit into a single
3689 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3691 // There can only be one PT_TLS segment.
3703 // Create the segments from a PHDRS clause. Return the segment which
3704 // should hold the file header and program headers, if any.
3707 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
,
3708 uint64_t dot_alignment
)
3710 this->attach_sections_using_phdrs_clause(layout
);
3711 return this->set_phdrs_clause_addresses(layout
, dot_alignment
);
3714 // Create the segments from the PHDRS clause, and put the output
3715 // sections in them.
3718 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
3720 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
3721 Name_to_segment name_to_segment
;
3722 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3723 p
!= this->phdrs_elements_
->end();
3725 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
3727 // Walk through the output sections and attach them to segments.
3728 // Output sections in the script which do not list segments are
3729 // attached to the same set of segments as the immediately preceding
3732 String_list
* phdr_names
= NULL
;
3733 bool load_segments_only
= false;
3734 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3735 p
!= this->sections_elements_
->end();
3739 String_list
* old_phdr_names
= phdr_names
;
3740 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &orphan
);
3744 if (phdr_names
== NULL
)
3746 gold_error(_("allocated section not in any segment"));
3750 // We see a list of segments names. Disable PT_LOAD segment only
3752 if (old_phdr_names
!= phdr_names
)
3753 load_segments_only
= false;
3755 // If this is an orphan section--one that was not explicitly
3756 // mentioned in the linker script--then it should not inherit
3757 // any segment type other than PT_LOAD. Otherwise, e.g., the
3758 // PT_INTERP segment will pick up following orphan sections,
3759 // which does not make sense. If this is not an orphan section,
3760 // we trust the linker script.
3763 // Enable PT_LOAD segments only filtering until we see another
3764 // list of segment names.
3765 load_segments_only
= true;
3768 bool in_load_segment
= false;
3769 for (String_list::const_iterator q
= phdr_names
->begin();
3770 q
!= phdr_names
->end();
3773 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
3774 if (r
== name_to_segment
.end())
3775 gold_error(_("no segment %s"), q
->c_str());
3778 if (load_segments_only
3779 && r
->second
->type() != elfcpp::PT_LOAD
)
3782 elfcpp::Elf_Word seg_flags
=
3783 Layout::section_flags_to_segment(os
->flags());
3785 if (r
->second
->type() != elfcpp::PT_LOAD
)
3786 r
->second
->add_output_section_to_nonload(os
, seg_flags
);
3789 r
->second
->add_output_section_to_load(layout
, os
, seg_flags
);
3790 if (in_load_segment
)
3791 gold_error(_("section in two PT_LOAD segments"));
3792 in_load_segment
= true;
3797 if (!in_load_segment
)
3798 gold_error(_("allocated section not in any PT_LOAD segment"));
3802 // Set the addresses for segments created from a PHDRS clause. Return
3803 // the segment which should hold the file header and program headers,
3807 Script_sections::set_phdrs_clause_addresses(Layout
* layout
,
3808 uint64_t dot_alignment
)
3810 Output_segment
* load_seg
= NULL
;
3811 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3812 p
!= this->phdrs_elements_
->end();
3815 // Note that we have to set the flags after adding the output
3816 // sections to the segment, as adding an output segment can
3817 // change the flags.
3818 (*p
)->set_flags_if_valid();
3820 Output_segment
* oseg
= (*p
)->segment();
3822 if (oseg
->type() != elfcpp::PT_LOAD
)
3824 // The addresses of non-PT_LOAD segments are set from the
3825 // PT_LOAD segments.
3826 if ((*p
)->has_load_address())
3827 gold_error(_("may only specify load address for PT_LOAD segment"));
3831 oseg
->set_minimum_p_align(dot_alignment
);
3833 // The output sections should have addresses from the SECTIONS
3834 // clause. The addresses don't have to be in order, so find the
3835 // one with the lowest load address. Use that to set the
3836 // address of the segment.
3838 Output_section
* osec
= oseg
->section_with_lowest_load_address();
3841 oseg
->set_addresses(0, 0);
3845 uint64_t vma
= osec
->address();
3846 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
3848 // Override the load address of the section with the load
3849 // address specified for the segment.
3850 if ((*p
)->has_load_address())
3852 if (osec
->has_load_address())
3853 gold_warning(_("PHDRS load address overrides "
3854 "section %s load address"),
3857 lma
= (*p
)->load_address();
3860 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
3861 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
3863 // We could support this if we wanted to.
3864 gold_error(_("using only one of FILEHDR and PHDRS is "
3865 "not currently supported"));
3869 size_t sizeof_headers
= this->total_header_size(layout
);
3870 uint64_t subtract
= this->header_size_adjustment(lma
,
3872 if (lma
>= subtract
&& vma
>= subtract
)
3879 gold_error(_("sections loaded on first page without room "
3880 "for file and program headers "
3881 "are not supported"));
3884 if (load_seg
!= NULL
)
3885 gold_error(_("using FILEHDR and PHDRS on more than one "
3886 "PT_LOAD segment is not currently supported"));
3890 oseg
->set_addresses(vma
, lma
);
3896 // Add the file header and segment headers to non-load segments
3897 // specified in the PHDRS clause.
3900 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
3901 Output_data
* segment_headers
)
3903 gold_assert(this->saw_phdrs_clause());
3904 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3905 p
!= this->phdrs_elements_
->end();
3908 if ((*p
)->type() != elfcpp::PT_LOAD
)
3910 if ((*p
)->includes_phdrs())
3911 (*p
)->segment()->add_initial_output_data(segment_headers
);
3912 if ((*p
)->includes_filehdr())
3913 (*p
)->segment()->add_initial_output_data(file_header
);
3918 // Look for an output section by name and return the address, the load
3919 // address, the alignment, and the size. This is used when an
3920 // expression refers to an output section which was not actually
3921 // created. This returns true if the section was found, false
3925 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
3926 uint64_t* load_address
,
3927 uint64_t* addralign
,
3928 uint64_t* size
) const
3930 if (!this->saw_sections_clause_
)
3932 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3933 p
!= this->sections_elements_
->end();
3935 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
3941 // Release all Output_segments. This remove all pointers to all
3945 Script_sections::release_segments()
3947 if (this->saw_phdrs_clause())
3949 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3950 p
!= this->phdrs_elements_
->end();
3952 (*p
)->release_segment();
3956 // Print the SECTIONS clause to F for debugging.
3959 Script_sections::print(FILE* f
) const
3961 if (this->phdrs_elements_
!= NULL
)
3963 fprintf(f
, "PHDRS {\n");
3964 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
3965 p
!= this->phdrs_elements_
->end();
3971 if (this->memory_regions_
!= NULL
)
3973 fprintf(f
, "MEMORY {\n");
3974 for (Memory_regions::const_iterator m
= this->memory_regions_
->begin();
3975 m
!= this->memory_regions_
->end();
3981 if (!this->saw_sections_clause_
)
3984 fprintf(f
, "SECTIONS {\n");
3986 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3987 p
!= this->sections_elements_
->end();
3994 } // End namespace gold.