1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008, 2009, 2010, 2011 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
),
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_address() const
93 script_exp_binary_add(this->start_
,
94 script_exp_integer(this->current_offset_
));
98 increment_offset(std::string section_name
, uint64_t amount
,
99 const Symbol_table
* symtab
, const Layout
* layout
)
101 this->current_offset_
+= amount
;
103 if (this->current_offset_
104 > this->length_
->eval(symtab
, layout
, false))
105 gold_error(_("section %s overflows end of region %s"),
106 section_name
.c_str(), this->name_
.c_str());
109 // Returns true iff there is room left in this region
110 // for AMOUNT more bytes of data.
112 has_room_for(const Symbol_table
* symtab
, const Layout
* layout
,
113 uint64_t amount
) const
115 return (this->current_offset_
+ amount
116 < this->length_
->eval(symtab
, layout
, false));
119 // Return true if the provided section flags
120 // are compatible with this region's attributes.
122 attributes_compatible(elfcpp::Elf_Xword flags
, elfcpp::Elf_Xword type
) const;
125 add_section(Output_section_definition
* sec
, bool vma
)
128 this->vma_sections_
.push_back(sec
);
130 this->lma_sections_
.push_back(sec
);
133 typedef std::vector
<Output_section_definition
*> Section_list
;
135 // Return the start of the list of sections
136 // whose VMAs are taken from this region.
137 Section_list::const_iterator
138 get_vma_section_list_start() const
139 { return this->vma_sections_
.begin(); }
141 // Return the start of the list of sections
142 // whose LMAs are taken from this region.
143 Section_list::const_iterator
144 get_lma_section_list_start() const
145 { return this->lma_sections_
.begin(); }
147 // Return the end of the list of sections
148 // whose VMAs are taken from this region.
149 Section_list::const_iterator
150 get_vma_section_list_end() const
151 { return this->vma_sections_
.end(); }
153 // Return the end of the list of sections
154 // whose LMAs are taken from this region.
155 Section_list::const_iterator
156 get_lma_section_list_end() const
157 { return this->lma_sections_
.end(); }
159 Output_section_definition
*
160 get_last_section() const
161 { return this->last_section_
; }
164 set_last_section(Output_section_definition
* sec
)
165 { this->last_section_
= sec
; }
170 unsigned int attributes_
;
173 // The offset to the next free byte in the region.
174 // Note - for compatibility with GNU LD we only maintain one offset
175 // regardless of whether the region is being used for VMA values,
176 // LMA values, or both.
177 uint64_t current_offset_
;
178 // A list of sections whose VMAs are set inside this region.
179 Section_list vma_sections_
;
180 // A list of sections whose LMAs are set inside this region.
181 Section_list lma_sections_
;
182 // The latest section to make use of this region.
183 Output_section_definition
* last_section_
;
186 // Return true if the provided section flags
187 // are compatible with this region's attributes.
190 Memory_region::attributes_compatible(elfcpp::Elf_Xword flags
,
191 elfcpp::Elf_Xword type
) const
193 unsigned int attrs
= this->attributes_
;
195 // No attributes means that this region is not compatible with anything.
202 switch (attrs
& - attrs
)
205 if ((flags
& elfcpp::SHF_EXECINSTR
) == 0)
210 if ((flags
& elfcpp::SHF_WRITE
) == 0)
215 // All sections are presumed readable.
218 case MEM_ALLOCATABLE
:
219 if ((flags
& elfcpp::SHF_ALLOC
) == 0)
223 case MEM_INITIALIZED
:
224 if ((type
& elfcpp::SHT_NOBITS
) != 0)
228 attrs
&= ~ (attrs
& - attrs
);
235 // Print a memory region.
238 Memory_region::print(FILE* f
) const
240 fprintf(f
, " %s", this->name_
.c_str());
242 unsigned int attrs
= this->attributes_
;
248 switch (attrs
& - attrs
)
250 case MEM_EXECUTABLE
: fputc('x', f
); break;
251 case MEM_WRITEABLE
: fputc('w', f
); break;
252 case MEM_READABLE
: fputc('r', f
); break;
253 case MEM_ALLOCATABLE
: fputc('a', f
); break;
254 case MEM_INITIALIZED
: fputc('i', f
); break;
258 attrs
&= ~ (attrs
& - attrs
);
264 fprintf(f
, " : origin = ");
265 this->start_
->print(f
);
266 fprintf(f
, ", length = ");
267 this->length_
->print(f
);
271 // Manage orphan sections. This is intended to be largely compatible
272 // with the GNU linker. The Linux kernel implicitly relies on
273 // something similar to the GNU linker's orphan placement. We
274 // originally used a simpler scheme here, but it caused the kernel
275 // build to fail, and was also rather inefficient.
277 class Orphan_section_placement
280 typedef Script_sections::Elements_iterator Elements_iterator
;
283 Orphan_section_placement();
285 // Handle an output section during initialization of this mapping.
287 output_section_init(const std::string
& name
, Output_section
*,
288 Elements_iterator location
);
290 // Initialize the last location.
292 last_init(Elements_iterator location
);
294 // Set *PWHERE to the address of an iterator pointing to the
295 // location to use for an orphan section. Return true if the
296 // iterator has a value, false otherwise.
298 find_place(Output_section
*, Elements_iterator
** pwhere
);
300 // Return the iterator being used for sections at the very end of
301 // the linker script.
306 // The places that we specifically recognize. This list is copied
307 // from the GNU linker.
323 // The information we keep for a specific place.
326 // The name of sections for this place.
328 // Whether we have a location for this place.
330 // The iterator for this place.
331 Elements_iterator location
;
334 // Initialize one place element.
336 initialize_place(Place_index
, const char*);
339 Place places_
[PLACE_MAX
];
340 // True if this is the first call to output_section_init.
344 // Initialize Orphan_section_placement.
346 Orphan_section_placement::Orphan_section_placement()
349 this->initialize_place(PLACE_TEXT
, ".text");
350 this->initialize_place(PLACE_RODATA
, ".rodata");
351 this->initialize_place(PLACE_DATA
, ".data");
352 this->initialize_place(PLACE_TLS
, NULL
);
353 this->initialize_place(PLACE_TLS_BSS
, NULL
);
354 this->initialize_place(PLACE_BSS
, ".bss");
355 this->initialize_place(PLACE_REL
, NULL
);
356 this->initialize_place(PLACE_INTERP
, ".interp");
357 this->initialize_place(PLACE_NONALLOC
, NULL
);
358 this->initialize_place(PLACE_LAST
, NULL
);
361 // Initialize one place element.
364 Orphan_section_placement::initialize_place(Place_index index
, const char* name
)
366 this->places_
[index
].name
= name
;
367 this->places_
[index
].have_location
= false;
370 // While initializing the Orphan_section_placement information, this
371 // is called once for each output section named in the linker script.
372 // If we found an output section during the link, it will be passed in
376 Orphan_section_placement::output_section_init(const std::string
& name
,
378 Elements_iterator location
)
380 bool first_init
= this->first_init_
;
381 this->first_init_
= false;
383 for (int i
= 0; i
< PLACE_MAX
; ++i
)
385 if (this->places_
[i
].name
!= NULL
&& this->places_
[i
].name
== name
)
387 if (this->places_
[i
].have_location
)
389 // We have already seen a section with this name.
393 this->places_
[i
].location
= location
;
394 this->places_
[i
].have_location
= true;
396 // If we just found the .bss section, restart the search for
397 // an unallocated section. This follows the GNU linker's
400 this->places_
[PLACE_NONALLOC
].have_location
= false;
406 // Relocation sections.
407 if (!this->places_
[PLACE_REL
].have_location
409 && (os
->type() == elfcpp::SHT_REL
|| os
->type() == elfcpp::SHT_RELA
)
410 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
412 this->places_
[PLACE_REL
].location
= location
;
413 this->places_
[PLACE_REL
].have_location
= true;
416 // We find the location for unallocated sections by finding the
417 // first debugging or comment section after the BSS section (if
419 if (!this->places_
[PLACE_NONALLOC
].have_location
420 && (name
== ".comment" || Layout::is_debug_info_section(name
.c_str())))
422 // We add orphan sections after the location in PLACES_. We
423 // want to store unallocated sections before LOCATION. If this
424 // is the very first section, we can't use it.
428 this->places_
[PLACE_NONALLOC
].location
= location
;
429 this->places_
[PLACE_NONALLOC
].have_location
= true;
434 // Initialize the last location.
437 Orphan_section_placement::last_init(Elements_iterator location
)
439 this->places_
[PLACE_LAST
].location
= location
;
440 this->places_
[PLACE_LAST
].have_location
= true;
443 // Set *PWHERE to the address of an iterator pointing to the location
444 // to use for an orphan section. Return true if the iterator has a
445 // value, false otherwise.
448 Orphan_section_placement::find_place(Output_section
* os
,
449 Elements_iterator
** pwhere
)
451 // Figure out where OS should go. This is based on the GNU linker
452 // code. FIXME: The GNU linker handles small data sections
453 // specially, but we don't.
454 elfcpp::Elf_Word type
= os
->type();
455 elfcpp::Elf_Xword flags
= os
->flags();
457 if ((flags
& elfcpp::SHF_ALLOC
) == 0
458 && !Layout::is_debug_info_section(os
->name()))
459 index
= PLACE_NONALLOC
;
460 else if ((flags
& elfcpp::SHF_ALLOC
) == 0)
462 else if (type
== elfcpp::SHT_NOTE
)
463 index
= PLACE_INTERP
;
464 else if ((flags
& elfcpp::SHF_TLS
) != 0)
466 if (type
== elfcpp::SHT_NOBITS
)
467 index
= PLACE_TLS_BSS
;
471 else if (type
== elfcpp::SHT_NOBITS
)
473 else if ((flags
& elfcpp::SHF_WRITE
) != 0)
475 else if (type
== elfcpp::SHT_REL
|| type
== elfcpp::SHT_RELA
)
477 else if ((flags
& elfcpp::SHF_EXECINSTR
) == 0)
478 index
= PLACE_RODATA
;
482 // If we don't have a location yet, try to find one based on a
483 // plausible ordering of sections.
484 if (!this->places_
[index
].have_location
)
509 if (!this->places_
[PLACE_TLS
].have_location
)
513 if (follow
!= PLACE_MAX
&& this->places_
[follow
].have_location
)
515 // Set the location of INDEX to the location of FOLLOW. The
516 // location of INDEX will then be incremented by the caller,
517 // so anything in INDEX will continue to be after anything
519 this->places_
[index
].location
= this->places_
[follow
].location
;
520 this->places_
[index
].have_location
= true;
524 *pwhere
= &this->places_
[index
].location
;
525 bool ret
= this->places_
[index
].have_location
;
527 // The caller will set the location.
528 this->places_
[index
].have_location
= true;
533 // Return the iterator being used for sections at the very end of the
536 Orphan_section_placement::Elements_iterator
537 Orphan_section_placement::last_place() const
539 gold_assert(this->places_
[PLACE_LAST
].have_location
);
540 return this->places_
[PLACE_LAST
].location
;
543 // An element in a SECTIONS clause.
545 class Sections_element
551 virtual ~Sections_element()
554 // Return whether an output section is relro.
559 // Record that an output section is relro.
564 // Create any required output sections. The only real
565 // implementation is in Output_section_definition.
567 create_sections(Layout
*)
570 // Add any symbol being defined to the symbol table.
572 add_symbols_to_table(Symbol_table
*)
575 // Finalize symbols and check assertions.
577 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*)
580 // Return the output section name to use for an input file name and
581 // section name. This only real implementation is in
582 // Output_section_definition.
584 output_section_name(const char*, const char*, Output_section
***,
585 Script_sections::Section_type
*)
588 // Initialize OSP with an output section.
590 orphan_section_init(Orphan_section_placement
*,
591 Script_sections::Elements_iterator
)
594 // Set section addresses. This includes applying assignments if the
595 // expression is an absolute value.
597 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*,
601 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
602 // this section is constrained, and the input sections do not match,
603 // return the constraint, and set *POSD.
604 virtual Section_constraint
605 check_constraint(Output_section_definition
**)
606 { return CONSTRAINT_NONE
; }
608 // See if this is the alternate output section for a constrained
609 // output section. If it is, transfer the Output_section and return
610 // true. Otherwise return false.
612 alternate_constraint(Output_section_definition
*, Section_constraint
)
615 // Get the list of segments to use for an allocated section when
616 // using a PHDRS clause. If this is an allocated section, return
617 // the Output_section, and set *PHDRS_LIST (the first parameter) to
618 // the list of PHDRS to which it should be attached. If the PHDRS
619 // were not specified, don't change *PHDRS_LIST. When not returning
620 // NULL, set *ORPHAN (the second parameter) according to whether
621 // this is an orphan section--one that is not mentioned in the
623 virtual Output_section
*
624 allocate_to_segment(String_list
**, bool*)
627 // Look for an output section by name and return the address, the
628 // load address, the alignment, and the size. This is used when an
629 // expression refers to an output section which was not actually
630 // created. This returns true if the section was found, false
631 // otherwise. The only real definition is for
632 // Output_section_definition.
634 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
638 // Return the associated Output_section if there is one.
639 virtual Output_section
*
640 get_output_section() const
643 // Set the section's memory regions.
645 set_memory_region(Memory_region
*, bool)
646 { gold_error(_("Attempt to set a memory region for a non-output section")); }
648 // Print the element for debugging purposes.
650 print(FILE* f
) const = 0;
653 // An assignment in a SECTIONS clause outside of an output section.
655 class Sections_element_assignment
: public Sections_element
658 Sections_element_assignment(const char* name
, size_t namelen
,
659 Expression
* val
, bool provide
, bool hidden
)
660 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
663 // Add the symbol to the symbol table.
665 add_symbols_to_table(Symbol_table
* symtab
)
666 { this->assignment_
.add_to_table(symtab
); }
668 // Finalize the symbol.
670 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
673 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
, NULL
);
676 // Set the section address. There is no section here, but if the
677 // value is absolute, we set the symbol. This permits us to use
678 // absolute symbols when setting dot.
680 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
681 uint64_t* dot_value
, uint64_t*, uint64_t*)
683 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
686 // Print for debugging.
691 this->assignment_
.print(f
);
695 Symbol_assignment assignment_
;
698 // An assignment to the dot symbol in a SECTIONS clause outside of an
701 class Sections_element_dot_assignment
: public Sections_element
704 Sections_element_dot_assignment(Expression
* val
)
708 // Finalize the symbol.
710 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
713 // We ignore the section of the result because outside of an
714 // output section definition the dot symbol is always considered
716 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
720 // Update the dot symbol while setting section addresses.
722 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
723 uint64_t* dot_value
, uint64_t* dot_alignment
,
724 uint64_t* load_address
)
726 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, false, *dot_value
,
727 NULL
, NULL
, dot_alignment
);
728 *load_address
= *dot_value
;
731 // Print for debugging.
736 this->val_
->print(f
);
744 // An assertion in a SECTIONS clause outside of an output section.
746 class Sections_element_assertion
: public Sections_element
749 Sections_element_assertion(Expression
* check
, const char* message
,
751 : assertion_(check
, message
, messagelen
)
754 // Check the assertion.
756 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
, uint64_t*)
757 { this->assertion_
.check(symtab
, layout
); }
759 // Print for debugging.
764 this->assertion_
.print(f
);
768 Script_assertion assertion_
;
771 // An element in an output section in a SECTIONS clause.
773 class Output_section_element
776 // A list of input sections.
777 typedef std::list
<Output_section::Input_section
> Input_section_list
;
779 Output_section_element()
782 virtual ~Output_section_element()
785 // Return whether this element requires an output section to exist.
787 needs_output_section() const
790 // Add any symbol being defined to the symbol table.
792 add_symbols_to_table(Symbol_table
*)
795 // Finalize symbols and check assertions.
797 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*, Output_section
**)
800 // Return whether this element matches FILE_NAME and SECTION_NAME.
801 // The only real implementation is in Output_section_element_input.
803 match_name(const char*, const char*) const
806 // Set section addresses. This includes applying assignments if the
807 // expression is an absolute value.
809 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
810 uint64_t*, uint64_t*, Output_section
**, std::string
*,
814 // Print the element for debugging purposes.
816 print(FILE* f
) const = 0;
819 // Return a fill string that is LENGTH bytes long, filling it with
822 get_fill_string(const std::string
* fill
, section_size_type length
) const;
826 Output_section_element::get_fill_string(const std::string
* fill
,
827 section_size_type length
) const
829 std::string this_fill
;
830 this_fill
.reserve(length
);
831 while (this_fill
.length() + fill
->length() <= length
)
833 if (this_fill
.length() < length
)
834 this_fill
.append(*fill
, 0, length
- this_fill
.length());
838 // A symbol assignment in an output section.
840 class Output_section_element_assignment
: public Output_section_element
843 Output_section_element_assignment(const char* name
, size_t namelen
,
844 Expression
* val
, bool provide
,
846 : assignment_(name
, namelen
, false, val
, provide
, hidden
)
849 // Add the symbol to the symbol table.
851 add_symbols_to_table(Symbol_table
* symtab
)
852 { this->assignment_
.add_to_table(symtab
); }
854 // Finalize the symbol.
856 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
857 uint64_t* dot_value
, Output_section
** dot_section
)
859 this->assignment_
.finalize_with_dot(symtab
, layout
, *dot_value
,
863 // Set the section address. There is no section here, but if the
864 // value is absolute, we set the symbol. This permits us to use
865 // absolute symbols when setting dot.
867 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
868 uint64_t, uint64_t* dot_value
, uint64_t*,
869 Output_section
**, std::string
*, Input_section_list
*)
871 this->assignment_
.set_if_absolute(symtab
, layout
, true, *dot_value
);
874 // Print for debugging.
879 this->assignment_
.print(f
);
883 Symbol_assignment assignment_
;
886 // An assignment to the dot symbol in an output section.
888 class Output_section_element_dot_assignment
: public Output_section_element
891 Output_section_element_dot_assignment(Expression
* val
)
895 // An assignment to dot within an output section is enough to force
896 // the output section to exist.
898 needs_output_section() const
901 // Finalize the symbol.
903 finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
,
904 uint64_t* dot_value
, Output_section
** dot_section
)
906 *dot_value
= this->val_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
907 *dot_section
, dot_section
, NULL
);
910 // Update the dot symbol while setting section addresses.
912 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
913 uint64_t, uint64_t* dot_value
, uint64_t*,
914 Output_section
**, std::string
*, Input_section_list
*);
916 // Print for debugging.
921 this->val_
->print(f
);
929 // Update the dot symbol while setting section addresses.
932 Output_section_element_dot_assignment::set_section_addresses(
933 Symbol_table
* symtab
,
935 Output_section
* output_section
,
938 uint64_t* dot_alignment
,
939 Output_section
** dot_section
,
943 uint64_t next_dot
= this->val_
->eval_with_dot(symtab
, layout
, false,
944 *dot_value
, *dot_section
,
945 dot_section
, dot_alignment
);
946 if (next_dot
< *dot_value
)
947 gold_error(_("dot may not move backward"));
948 if (next_dot
> *dot_value
&& output_section
!= NULL
)
950 section_size_type length
= convert_to_section_size_type(next_dot
952 Output_section_data
* posd
;
954 posd
= new Output_data_zero_fill(length
, 0);
957 std::string this_fill
= this->get_fill_string(fill
, length
);
958 posd
= new Output_data_const(this_fill
, 0);
960 output_section
->add_output_section_data(posd
);
961 layout
->new_output_section_data_from_script(posd
);
963 *dot_value
= next_dot
;
966 // An assertion in an output section.
968 class Output_section_element_assertion
: public Output_section_element
971 Output_section_element_assertion(Expression
* check
, const char* message
,
973 : assertion_(check
, message
, messagelen
)
980 this->assertion_
.print(f
);
984 Script_assertion assertion_
;
987 // We use a special instance of Output_section_data to handle BYTE,
988 // SHORT, etc. This permits forward references to symbols in the
991 class Output_data_expression
: public Output_section_data
994 Output_data_expression(int size
, bool is_signed
, Expression
* val
,
995 const Symbol_table
* symtab
, const Layout
* layout
,
996 uint64_t dot_value
, Output_section
* dot_section
)
997 : Output_section_data(size
, 0, true),
998 is_signed_(is_signed
), val_(val
), symtab_(symtab
),
999 layout_(layout
), dot_value_(dot_value
), dot_section_(dot_section
)
1003 // Write the data to the output file.
1005 do_write(Output_file
*);
1007 // Write the data to a buffer.
1009 do_write_to_buffer(unsigned char*);
1011 // Write to a map file.
1013 do_print_to_mapfile(Mapfile
* mapfile
) const
1014 { mapfile
->print_output_data(this, _("** expression")); }
1017 template<bool big_endian
>
1019 endian_write_to_buffer(uint64_t, unsigned char*);
1023 const Symbol_table
* symtab_
;
1024 const Layout
* layout_
;
1025 uint64_t dot_value_
;
1026 Output_section
* dot_section_
;
1029 // Write the data element to the output file.
1032 Output_data_expression::do_write(Output_file
* of
)
1034 unsigned char* view
= of
->get_output_view(this->offset(), this->data_size());
1035 this->write_to_buffer(view
);
1036 of
->write_output_view(this->offset(), this->data_size(), view
);
1039 // Write the data element to a buffer.
1042 Output_data_expression::do_write_to_buffer(unsigned char* buf
)
1044 uint64_t val
= this->val_
->eval_with_dot(this->symtab_
, this->layout_
,
1045 true, this->dot_value_
,
1046 this->dot_section_
, NULL
, NULL
);
1048 if (parameters
->target().is_big_endian())
1049 this->endian_write_to_buffer
<true>(val
, buf
);
1051 this->endian_write_to_buffer
<false>(val
, buf
);
1054 template<bool big_endian
>
1056 Output_data_expression::endian_write_to_buffer(uint64_t val
,
1059 switch (this->data_size())
1062 elfcpp::Swap_unaligned
<8, big_endian
>::writeval(buf
, val
);
1065 elfcpp::Swap_unaligned
<16, big_endian
>::writeval(buf
, val
);
1068 elfcpp::Swap_unaligned
<32, big_endian
>::writeval(buf
, val
);
1071 if (parameters
->target().get_size() == 32)
1074 if (this->is_signed_
&& (val
& 0x80000000) != 0)
1075 val
|= 0xffffffff00000000LL
;
1077 elfcpp::Swap_unaligned
<64, big_endian
>::writeval(buf
, val
);
1084 // A data item in an output section.
1086 class Output_section_element_data
: public Output_section_element
1089 Output_section_element_data(int size
, bool is_signed
, Expression
* val
)
1090 : size_(size
), is_signed_(is_signed
), val_(val
)
1093 // If there is a data item, then we must create an output section.
1095 needs_output_section() const
1098 // Finalize symbols--we just need to update dot.
1100 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1102 { *dot_value
+= this->size_
; }
1104 // Store the value in the section.
1106 set_section_addresses(Symbol_table
*, Layout
*, Output_section
*, uint64_t,
1107 uint64_t* dot_value
, uint64_t*, Output_section
**,
1108 std::string
*, Input_section_list
*);
1110 // Print for debugging.
1115 // The size in bytes.
1117 // Whether the value is signed.
1123 // Store the value in the section.
1126 Output_section_element_data::set_section_addresses(
1127 Symbol_table
* symtab
,
1131 uint64_t* dot_value
,
1133 Output_section
** dot_section
,
1135 Input_section_list
*)
1137 gold_assert(os
!= NULL
);
1138 Output_data_expression
* expression
=
1139 new Output_data_expression(this->size_
, this->is_signed_
, this->val_
,
1140 symtab
, layout
, *dot_value
, *dot_section
);
1141 os
->add_output_section_data(expression
);
1142 layout
->new_output_section_data_from_script(expression
);
1143 *dot_value
+= this->size_
;
1146 // Print for debugging.
1149 Output_section_element_data::print(FILE* f
) const
1152 switch (this->size_
)
1164 if (this->is_signed_
)
1172 fprintf(f
, " %s(", s
);
1173 this->val_
->print(f
);
1177 // A fill value setting in an output section.
1179 class Output_section_element_fill
: public Output_section_element
1182 Output_section_element_fill(Expression
* val
)
1186 // Update the fill value while setting section addresses.
1188 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1189 uint64_t, uint64_t* dot_value
, uint64_t*,
1190 Output_section
** dot_section
,
1191 std::string
* fill
, Input_section_list
*)
1193 Output_section
* fill_section
;
1194 uint64_t fill_val
= this->val_
->eval_with_dot(symtab
, layout
, false,
1195 *dot_value
, *dot_section
,
1196 &fill_section
, NULL
);
1197 if (fill_section
!= NULL
)
1198 gold_warning(_("fill value is not absolute"));
1199 // FIXME: The GNU linker supports fill values of arbitrary length.
1200 unsigned char fill_buff
[4];
1201 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
1202 fill
->assign(reinterpret_cast<char*>(fill_buff
), 4);
1205 // Print for debugging.
1207 print(FILE* f
) const
1209 fprintf(f
, " FILL(");
1210 this->val_
->print(f
);
1215 // The new fill value.
1219 // An input section specification in an output section
1221 class Output_section_element_input
: public Output_section_element
1224 Output_section_element_input(const Input_section_spec
* spec
, bool keep
);
1226 // Finalize symbols--just update the value of the dot symbol.
1228 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t* dot_value
,
1229 Output_section
** dot_section
)
1231 *dot_value
= this->final_dot_value_
;
1232 *dot_section
= this->final_dot_section_
;
1235 // See whether we match FILE_NAME and SECTION_NAME as an input
1238 match_name(const char* file_name
, const char* section_name
) const;
1240 // Set the section address.
1242 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
, Output_section
*,
1243 uint64_t subalign
, uint64_t* dot_value
, uint64_t*,
1244 Output_section
**, std::string
* fill
,
1245 Input_section_list
*);
1247 // Print for debugging.
1249 print(FILE* f
) const;
1252 // An input section pattern.
1253 struct Input_section_pattern
1255 std::string pattern
;
1256 bool pattern_is_wildcard
;
1259 Input_section_pattern(const char* patterna
, size_t patternlena
,
1260 Sort_wildcard sorta
)
1261 : pattern(patterna
, patternlena
),
1262 pattern_is_wildcard(is_wildcard_string(this->pattern
.c_str())),
1267 typedef std::vector
<Input_section_pattern
> Input_section_patterns
;
1269 // Filename_exclusions is a pair of filename pattern and a bool
1270 // indicating whether the filename is a wildcard.
1271 typedef std::vector
<std::pair
<std::string
, bool> > Filename_exclusions
;
1273 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1274 // indicates whether this is a wildcard pattern.
1276 match(const char* string
, const char* pattern
, bool is_wildcard_pattern
)
1278 return (is_wildcard_pattern
1279 ? fnmatch(pattern
, string
, 0) == 0
1280 : strcmp(string
, pattern
) == 0);
1283 // See if we match a file name.
1285 match_file_name(const char* file_name
) const;
1287 // The file name pattern. If this is the empty string, we match all
1289 std::string filename_pattern_
;
1290 // Whether the file name pattern is a wildcard.
1291 bool filename_is_wildcard_
;
1292 // How the file names should be sorted. This may only be
1293 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1294 Sort_wildcard filename_sort_
;
1295 // The list of file names to exclude.
1296 Filename_exclusions filename_exclusions_
;
1297 // The list of input section patterns.
1298 Input_section_patterns input_section_patterns_
;
1299 // Whether to keep this section when garbage collecting.
1301 // The value of dot after including all matching sections.
1302 uint64_t final_dot_value_
;
1303 // The section where dot is defined after including all matching
1305 Output_section
* final_dot_section_
;
1308 // Construct Output_section_element_input. The parser records strings
1309 // as pointers into a copy of the script file, which will go away when
1310 // parsing is complete. We make sure they are in std::string objects.
1312 Output_section_element_input::Output_section_element_input(
1313 const Input_section_spec
* spec
,
1315 : filename_pattern_(),
1316 filename_is_wildcard_(false),
1317 filename_sort_(spec
->file
.sort
),
1318 filename_exclusions_(),
1319 input_section_patterns_(),
1321 final_dot_value_(0),
1322 final_dot_section_(NULL
)
1324 // The filename pattern "*" is common, and matches all files. Turn
1325 // it into the empty string.
1326 if (spec
->file
.name
.length
!= 1 || spec
->file
.name
.value
[0] != '*')
1327 this->filename_pattern_
.assign(spec
->file
.name
.value
,
1328 spec
->file
.name
.length
);
1329 this->filename_is_wildcard_
= is_wildcard_string(this->filename_pattern_
.c_str());
1331 if (spec
->input_sections
.exclude
!= NULL
)
1333 for (String_list::const_iterator p
=
1334 spec
->input_sections
.exclude
->begin();
1335 p
!= spec
->input_sections
.exclude
->end();
1338 bool is_wildcard
= is_wildcard_string((*p
).c_str());
1339 this->filename_exclusions_
.push_back(std::make_pair(*p
,
1344 if (spec
->input_sections
.sections
!= NULL
)
1346 Input_section_patterns
& isp(this->input_section_patterns_
);
1347 for (String_sort_list::const_iterator p
=
1348 spec
->input_sections
.sections
->begin();
1349 p
!= spec
->input_sections
.sections
->end();
1351 isp
.push_back(Input_section_pattern(p
->name
.value
, p
->name
.length
,
1356 // See whether we match FILE_NAME.
1359 Output_section_element_input::match_file_name(const char* file_name
) const
1361 if (!this->filename_pattern_
.empty())
1363 // If we were called with no filename, we refuse to match a
1364 // pattern which requires a file name.
1365 if (file_name
== NULL
)
1368 if (!match(file_name
, this->filename_pattern_
.c_str(),
1369 this->filename_is_wildcard_
))
1373 if (file_name
!= NULL
)
1375 // Now we have to see whether FILE_NAME matches one of the
1376 // exclusion patterns, if any.
1377 for (Filename_exclusions::const_iterator p
=
1378 this->filename_exclusions_
.begin();
1379 p
!= this->filename_exclusions_
.end();
1382 if (match(file_name
, p
->first
.c_str(), p
->second
))
1390 // See whether we match FILE_NAME and SECTION_NAME.
1393 Output_section_element_input::match_name(const char* file_name
,
1394 const char* section_name
) const
1396 if (!this->match_file_name(file_name
))
1399 // If there are no section name patterns, then we match.
1400 if (this->input_section_patterns_
.empty())
1403 // See whether we match the section name patterns.
1404 for (Input_section_patterns::const_iterator p
=
1405 this->input_section_patterns_
.begin();
1406 p
!= this->input_section_patterns_
.end();
1409 if (match(section_name
, p
->pattern
.c_str(), p
->pattern_is_wildcard
))
1413 // We didn't match any section names, so we didn't match.
1417 // Information we use to sort the input sections.
1419 class Input_section_info
1422 Input_section_info(const Output_section::Input_section
& input_section
)
1423 : input_section_(input_section
), section_name_(),
1424 size_(0), addralign_(1)
1427 // Return the simple input section.
1428 const Output_section::Input_section
&
1429 input_section() const
1430 { return this->input_section_
; }
1432 // Return the object.
1435 { return this->input_section_
.relobj(); }
1437 // Return the section index.
1440 { return this->input_section_
.shndx(); }
1442 // Return the section name.
1444 section_name() const
1445 { return this->section_name_
; }
1447 // Set the section name.
1449 set_section_name(const std::string name
)
1450 { this->section_name_
= name
; }
1452 // Return the section size.
1455 { return this->size_
; }
1457 // Set the section size.
1459 set_size(uint64_t size
)
1460 { this->size_
= size
; }
1462 // Return the address alignment.
1465 { return this->addralign_
; }
1467 // Set the address alignment.
1469 set_addralign(uint64_t addralign
)
1470 { this->addralign_
= addralign
; }
1473 // Input section, can be a relaxed section.
1474 Output_section::Input_section input_section_
;
1475 // Name of the section.
1476 std::string section_name_
;
1479 // Address alignment.
1480 uint64_t addralign_
;
1483 // A class to sort the input sections.
1485 class Input_section_sorter
1488 Input_section_sorter(Sort_wildcard filename_sort
, Sort_wildcard section_sort
)
1489 : filename_sort_(filename_sort
), section_sort_(section_sort
)
1493 operator()(const Input_section_info
&, const Input_section_info
&) const;
1496 Sort_wildcard filename_sort_
;
1497 Sort_wildcard section_sort_
;
1501 Input_section_sorter::operator()(const Input_section_info
& isi1
,
1502 const Input_section_info
& isi2
) const
1504 if (this->section_sort_
== SORT_WILDCARD_BY_NAME
1505 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1506 || (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1507 && isi1
.addralign() == isi2
.addralign()))
1509 if (isi1
.section_name() != isi2
.section_name())
1510 return isi1
.section_name() < isi2
.section_name();
1512 if (this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT
1513 || this->section_sort_
== SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1514 || this->section_sort_
== SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
)
1516 if (isi1
.addralign() != isi2
.addralign())
1517 return isi1
.addralign() < isi2
.addralign();
1519 if (this->filename_sort_
== SORT_WILDCARD_BY_NAME
)
1521 if (isi1
.relobj()->name() != isi2
.relobj()->name())
1522 return (isi1
.relobj()->name() < isi2
.relobj()->name());
1525 // Otherwise we leave them in the same order.
1529 // Set the section address. Look in INPUT_SECTIONS for sections which
1530 // match this spec, sort them as specified, and add them to the output
1534 Output_section_element_input::set_section_addresses(
1537 Output_section
* output_section
,
1539 uint64_t* dot_value
,
1541 Output_section
** dot_section
,
1543 Input_section_list
* input_sections
)
1545 // We build a list of sections which match each
1546 // Input_section_pattern.
1548 typedef std::vector
<std::vector
<Input_section_info
> > Matching_sections
;
1549 size_t input_pattern_count
= this->input_section_patterns_
.size();
1550 if (input_pattern_count
== 0)
1551 input_pattern_count
= 1;
1552 Matching_sections
matching_sections(input_pattern_count
);
1554 // Look through the list of sections for this output section. Add
1555 // each one which matches to one of the elements of
1556 // MATCHING_SECTIONS.
1558 Input_section_list::iterator p
= input_sections
->begin();
1559 while (p
!= input_sections
->end())
1561 Relobj
* relobj
= p
->relobj();
1562 unsigned int shndx
= p
->shndx();
1563 Input_section_info
isi(*p
);
1565 // Calling section_name and section_addralign is not very
1568 // Lock the object so that we can get information about the
1569 // section. This is OK since we know we are single-threaded
1572 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1573 Task_lock_obj
<Object
> tl(task
, relobj
);
1575 isi
.set_section_name(relobj
->section_name(shndx
));
1576 if (p
->is_relaxed_input_section())
1578 // We use current data size because relaxed section sizes may not
1579 // have finalized yet.
1580 isi
.set_size(p
->relaxed_input_section()->current_data_size());
1581 isi
.set_addralign(p
->relaxed_input_section()->addralign());
1585 isi
.set_size(relobj
->section_size(shndx
));
1586 isi
.set_addralign(relobj
->section_addralign(shndx
));
1590 if (!this->match_file_name(relobj
->name().c_str()))
1592 else if (this->input_section_patterns_
.empty())
1594 matching_sections
[0].push_back(isi
);
1595 p
= input_sections
->erase(p
);
1600 for (i
= 0; i
< input_pattern_count
; ++i
)
1602 const Input_section_pattern
&
1603 isp(this->input_section_patterns_
[i
]);
1604 if (match(isi
.section_name().c_str(), isp
.pattern
.c_str(),
1605 isp
.pattern_is_wildcard
))
1609 if (i
>= this->input_section_patterns_
.size())
1613 matching_sections
[i
].push_back(isi
);
1614 p
= input_sections
->erase(p
);
1619 // Look through MATCHING_SECTIONS. Sort each one as specified,
1620 // using a stable sort so that we get the default order when
1621 // sections are otherwise equal. Add each input section to the
1624 uint64_t dot
= *dot_value
;
1625 for (size_t i
= 0; i
< input_pattern_count
; ++i
)
1627 if (matching_sections
[i
].empty())
1630 gold_assert(output_section
!= NULL
);
1632 const Input_section_pattern
& isp(this->input_section_patterns_
[i
]);
1633 if (isp
.sort
!= SORT_WILDCARD_NONE
1634 || this->filename_sort_
!= SORT_WILDCARD_NONE
)
1635 std::stable_sort(matching_sections
[i
].begin(),
1636 matching_sections
[i
].end(),
1637 Input_section_sorter(this->filename_sort_
,
1640 for (std::vector
<Input_section_info
>::const_iterator p
=
1641 matching_sections
[i
].begin();
1642 p
!= matching_sections
[i
].end();
1645 // Override the original address alignment if SUBALIGN is specified
1646 // and is greater than the original alignment. We need to make a
1647 // copy of the input section to modify the alignment.
1648 Output_section::Input_section
sis(p
->input_section());
1650 uint64_t this_subalign
= sis
.addralign();
1651 if (!sis
.is_input_section())
1652 sis
.output_section_data()->finalize_data_size();
1653 uint64_t data_size
= sis
.data_size();
1654 if (this_subalign
< subalign
)
1656 this_subalign
= subalign
;
1657 sis
.set_addralign(subalign
);
1660 uint64_t address
= align_address(dot
, this_subalign
);
1662 if (address
> dot
&& !fill
->empty())
1664 section_size_type length
=
1665 convert_to_section_size_type(address
- dot
);
1666 std::string this_fill
= this->get_fill_string(fill
, length
);
1667 Output_section_data
* posd
= new Output_data_const(this_fill
, 0);
1668 output_section
->add_output_section_data(posd
);
1669 layout
->new_output_section_data_from_script(posd
);
1672 output_section
->add_script_input_section(sis
);
1673 dot
= address
+ data_size
;
1677 // An SHF_TLS/SHT_NOBITS section does not take up any
1679 if (output_section
== NULL
1680 || (output_section
->flags() & elfcpp::SHF_TLS
) == 0
1681 || output_section
->type() != elfcpp::SHT_NOBITS
)
1684 this->final_dot_value_
= *dot_value
;
1685 this->final_dot_section_
= *dot_section
;
1688 // Print for debugging.
1691 Output_section_element_input::print(FILE* f
) const
1696 fprintf(f
, "KEEP(");
1698 if (!this->filename_pattern_
.empty())
1700 bool need_close_paren
= false;
1701 switch (this->filename_sort_
)
1703 case SORT_WILDCARD_NONE
:
1705 case SORT_WILDCARD_BY_NAME
:
1706 fprintf(f
, "SORT_BY_NAME(");
1707 need_close_paren
= true;
1713 fprintf(f
, "%s", this->filename_pattern_
.c_str());
1715 if (need_close_paren
)
1719 if (!this->input_section_patterns_
.empty()
1720 || !this->filename_exclusions_
.empty())
1724 bool need_space
= false;
1725 if (!this->filename_exclusions_
.empty())
1727 fprintf(f
, "EXCLUDE_FILE(");
1728 bool need_comma
= false;
1729 for (Filename_exclusions::const_iterator p
=
1730 this->filename_exclusions_
.begin();
1731 p
!= this->filename_exclusions_
.end();
1736 fprintf(f
, "%s", p
->first
.c_str());
1743 for (Input_section_patterns::const_iterator p
=
1744 this->input_section_patterns_
.begin();
1745 p
!= this->input_section_patterns_
.end();
1751 int close_parens
= 0;
1754 case SORT_WILDCARD_NONE
:
1756 case SORT_WILDCARD_BY_NAME
:
1757 fprintf(f
, "SORT_BY_NAME(");
1760 case SORT_WILDCARD_BY_ALIGNMENT
:
1761 fprintf(f
, "SORT_BY_ALIGNMENT(");
1764 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
1765 fprintf(f
, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1768 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
1769 fprintf(f
, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1776 fprintf(f
, "%s", p
->pattern
.c_str());
1778 for (int i
= 0; i
< close_parens
; ++i
)
1793 // An output section.
1795 class Output_section_definition
: public Sections_element
1798 typedef Output_section_element::Input_section_list Input_section_list
;
1800 Output_section_definition(const char* name
, size_t namelen
,
1801 const Parser_output_section_header
* header
);
1803 // Finish the output section with the information in the trailer.
1805 finish(const Parser_output_section_trailer
* trailer
);
1807 // Add a symbol to be defined.
1809 add_symbol_assignment(const char* name
, size_t length
, Expression
* value
,
1810 bool provide
, bool hidden
);
1812 // Add an assignment to the special dot symbol.
1814 add_dot_assignment(Expression
* value
);
1816 // Add an assertion.
1818 add_assertion(Expression
* check
, const char* message
, size_t messagelen
);
1820 // Add a data item to the current output section.
1822 add_data(int size
, bool is_signed
, Expression
* val
);
1824 // Add a setting for the fill value.
1826 add_fill(Expression
* val
);
1828 // Add an input section specification.
1830 add_input_section(const Input_section_spec
* spec
, bool keep
);
1832 // Return whether the output section is relro.
1835 { return this->is_relro_
; }
1837 // Record that the output section is relro.
1840 { this->is_relro_
= true; }
1842 // Create any required output sections.
1844 create_sections(Layout
*);
1846 // Add any symbols being defined to the symbol table.
1848 add_symbols_to_table(Symbol_table
* symtab
);
1850 // Finalize symbols and check assertions.
1852 finalize_symbols(Symbol_table
*, const Layout
*, uint64_t*);
1854 // Return the output section name to use for an input file name and
1857 output_section_name(const char* file_name
, const char* section_name
,
1858 Output_section
***, Script_sections::Section_type
*);
1860 // Initialize OSP with an output section.
1862 orphan_section_init(Orphan_section_placement
* osp
,
1863 Script_sections::Elements_iterator p
)
1864 { osp
->output_section_init(this->name_
, this->output_section_
, p
); }
1866 // Set the section address.
1868 set_section_addresses(Symbol_table
* symtab
, Layout
* layout
,
1869 uint64_t* dot_value
, uint64_t*,
1870 uint64_t* load_address
);
1872 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1873 // this section is constrained, and the input sections do not match,
1874 // return the constraint, and set *POSD.
1876 check_constraint(Output_section_definition
** posd
);
1878 // See if this is the alternate output section for a constrained
1879 // output section. If it is, transfer the Output_section and return
1880 // true. Otherwise return false.
1882 alternate_constraint(Output_section_definition
*, Section_constraint
);
1884 // Get the list of segments to use for an allocated section when
1885 // using a PHDRS clause.
1887 allocate_to_segment(String_list
** phdrs_list
, bool* orphan
);
1889 // Look for an output section by name and return the address, the
1890 // load address, the alignment, and the size. This is used when an
1891 // expression refers to an output section which was not actually
1892 // created. This returns true if the section was found, false
1895 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1898 // Return the associated Output_section if there is one.
1900 get_output_section() const
1901 { return this->output_section_
; }
1903 // Print the contents to the FILE. This is for debugging.
1907 // Return the output section type if specified or Script_sections::ST_NONE.
1908 Script_sections::Section_type
1909 section_type() const;
1911 // Store the memory region to use.
1913 set_memory_region(Memory_region
*, bool set_vma
);
1916 set_section_vma(Expression
* address
)
1917 { this->address_
= address
; }
1920 set_section_lma(Expression
* address
)
1921 { this->load_address_
= address
; }
1924 get_section_name() const
1925 { return this->name_
; }
1929 script_section_type_name(Script_section_type
);
1931 typedef std::vector
<Output_section_element
*> Output_section_elements
;
1933 // The output section name.
1935 // The address. This may be NULL.
1936 Expression
* address_
;
1937 // The load address. This may be NULL.
1938 Expression
* load_address_
;
1939 // The alignment. This may be NULL.
1941 // The input section alignment. This may be NULL.
1942 Expression
* subalign_
;
1943 // The constraint, if any.
1944 Section_constraint constraint_
;
1945 // The fill value. This may be NULL.
1947 // The list of segments this section should go into. This may be
1949 String_list
* phdrs_
;
1950 // The list of elements defining the section.
1951 Output_section_elements elements_
;
1952 // The Output_section created for this definition. This will be
1953 // NULL if none was created.
1954 Output_section
* output_section_
;
1955 // The address after it has been evaluated.
1956 uint64_t evaluated_address_
;
1957 // The load address after it has been evaluated.
1958 uint64_t evaluated_load_address_
;
1959 // The alignment after it has been evaluated.
1960 uint64_t evaluated_addralign_
;
1961 // The output section is relro.
1963 // The output section type if specified.
1964 enum Script_section_type script_section_type_
;
1969 Output_section_definition::Output_section_definition(
1972 const Parser_output_section_header
* header
)
1973 : name_(name
, namelen
),
1974 address_(header
->address
),
1975 load_address_(header
->load_address
),
1976 align_(header
->align
),
1977 subalign_(header
->subalign
),
1978 constraint_(header
->constraint
),
1982 output_section_(NULL
),
1983 evaluated_address_(0),
1984 evaluated_load_address_(0),
1985 evaluated_addralign_(0),
1987 script_section_type_(header
->section_type
)
1991 // Finish an output section.
1994 Output_section_definition::finish(const Parser_output_section_trailer
* trailer
)
1996 this->fill_
= trailer
->fill
;
1997 this->phdrs_
= trailer
->phdrs
;
2000 // Add a symbol to be defined.
2003 Output_section_definition::add_symbol_assignment(const char* name
,
2009 Output_section_element
* p
= new Output_section_element_assignment(name
,
2014 this->elements_
.push_back(p
);
2017 // Add an assignment to the special dot symbol.
2020 Output_section_definition::add_dot_assignment(Expression
* value
)
2022 Output_section_element
* p
= new Output_section_element_dot_assignment(value
);
2023 this->elements_
.push_back(p
);
2026 // Add an assertion.
2029 Output_section_definition::add_assertion(Expression
* check
,
2030 const char* message
,
2033 Output_section_element
* p
= new Output_section_element_assertion(check
,
2036 this->elements_
.push_back(p
);
2039 // Add a data item to the current output section.
2042 Output_section_definition::add_data(int size
, bool is_signed
, Expression
* val
)
2044 Output_section_element
* p
= new Output_section_element_data(size
, is_signed
,
2046 this->elements_
.push_back(p
);
2049 // Add a setting for the fill value.
2052 Output_section_definition::add_fill(Expression
* val
)
2054 Output_section_element
* p
= new Output_section_element_fill(val
);
2055 this->elements_
.push_back(p
);
2058 // Add an input section specification.
2061 Output_section_definition::add_input_section(const Input_section_spec
* spec
,
2064 Output_section_element
* p
= new Output_section_element_input(spec
, keep
);
2065 this->elements_
.push_back(p
);
2068 // Create any required output sections. We need an output section if
2069 // there is a data statement here.
2072 Output_section_definition::create_sections(Layout
* layout
)
2074 if (this->output_section_
!= NULL
)
2076 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2077 p
!= this->elements_
.end();
2080 if ((*p
)->needs_output_section())
2082 const char* name
= this->name_
.c_str();
2083 this->output_section_
=
2084 layout
->make_output_section_for_script(name
, this->section_type());
2090 // Add any symbols being defined to the symbol table.
2093 Output_section_definition::add_symbols_to_table(Symbol_table
* symtab
)
2095 for (Output_section_elements::iterator p
= this->elements_
.begin();
2096 p
!= this->elements_
.end();
2098 (*p
)->add_symbols_to_table(symtab
);
2101 // Finalize symbols and check assertions.
2104 Output_section_definition::finalize_symbols(Symbol_table
* symtab
,
2105 const Layout
* layout
,
2106 uint64_t* dot_value
)
2108 if (this->output_section_
!= NULL
)
2109 *dot_value
= this->output_section_
->address();
2112 uint64_t address
= *dot_value
;
2113 if (this->address_
!= NULL
)
2115 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
2119 if (this->align_
!= NULL
)
2121 uint64_t align
= this->align_
->eval_with_dot(symtab
, layout
, true,
2124 address
= align_address(address
, align
);
2126 *dot_value
= address
;
2129 Output_section
* dot_section
= this->output_section_
;
2130 for (Output_section_elements::iterator p
= this->elements_
.begin();
2131 p
!= this->elements_
.end();
2133 (*p
)->finalize_symbols(symtab
, layout
, dot_value
, &dot_section
);
2136 // Return the output section name to use for an input section name.
2139 Output_section_definition::output_section_name(
2140 const char* file_name
,
2141 const char* section_name
,
2142 Output_section
*** slot
,
2143 Script_sections::Section_type
* psection_type
)
2145 // Ask each element whether it matches NAME.
2146 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2147 p
!= this->elements_
.end();
2150 if ((*p
)->match_name(file_name
, section_name
))
2152 // We found a match for NAME, which means that it should go
2153 // into this output section.
2154 *slot
= &this->output_section_
;
2155 *psection_type
= this->section_type();
2156 return this->name_
.c_str();
2160 // We don't know about this section name.
2164 // Return true if memory from START to START + LENGTH is contained
2165 // within a memory region.
2168 Script_sections::block_in_region(Symbol_table
* symtab
, Layout
* layout
,
2169 uint64_t start
, uint64_t length
) const
2171 if (this->memory_regions_
== NULL
)
2174 for (Memory_regions::const_iterator mr
= this->memory_regions_
->begin();
2175 mr
!= this->memory_regions_
->end();
2178 uint64_t s
= (*mr
)->start_address()->eval(symtab
, layout
, false);
2179 uint64_t l
= (*mr
)->length()->eval(symtab
, layout
, false);
2182 && (s
+ l
) >= (start
+ length
))
2189 // Find a memory region that should be used by a given output SECTION.
2190 // If provided set PREVIOUS_SECTION_RETURN to point to the last section
2191 // that used the return memory region.
2194 Script_sections::find_memory_region(
2195 Output_section_definition
* section
,
2196 bool find_vma_region
,
2197 Output_section_definition
** previous_section_return
)
2199 if (previous_section_return
!= NULL
)
2200 * previous_section_return
= NULL
;
2202 // Walk the memory regions specified in this script, if any.
2203 if (this->memory_regions_
== NULL
)
2206 // The /DISCARD/ section never gets assigned to any region.
2207 if (section
->get_section_name() == "/DISCARD/")
2210 Memory_region
* first_match
= NULL
;
2212 // First check to see if a region has been assigned to this section.
2213 for (Memory_regions::const_iterator mr
= this->memory_regions_
->begin();
2214 mr
!= this->memory_regions_
->end();
2217 if (find_vma_region
)
2219 for (Memory_region::Section_list::const_iterator s
=
2220 (*mr
)->get_vma_section_list_start();
2221 s
!= (*mr
)->get_vma_section_list_end();
2223 if ((*s
) == section
)
2225 (*mr
)->set_last_section(section
);
2231 for (Memory_region::Section_list::const_iterator s
=
2232 (*mr
)->get_lma_section_list_start();
2233 s
!= (*mr
)->get_lma_section_list_end();
2235 if ((*s
) == section
)
2237 (*mr
)->set_last_section(section
);
2242 // Make a note of the first memory region whose attributes
2243 // are compatible with the section. If we do not find an
2244 // explicit region assignment, then we will return this region.
2245 Output_section
* out_sec
= section
->get_output_section();
2246 if (first_match
== NULL
2248 && (*mr
)->attributes_compatible(out_sec
->flags(),
2253 // With LMA computations, if an explicit region has not been specified then
2254 // we will want to set the difference between the VMA and the LMA of the
2255 // section were searching for to be the same as the difference between the
2256 // VMA and LMA of the last section to be added to first matched region.
2257 // Hence, if it was asked for, we return a pointer to the last section
2258 // known to be used by the first matched region.
2259 if (first_match
!= NULL
2260 && previous_section_return
!= NULL
)
2261 *previous_section_return
= first_match
->get_last_section();
2266 // Set the section address. Note that the OUTPUT_SECTION_ field will
2267 // be NULL if no input sections were mapped to this output section.
2268 // We still have to adjust dot and process symbol assignments.
2271 Output_section_definition::set_section_addresses(Symbol_table
* symtab
,
2273 uint64_t* dot_value
,
2274 uint64_t* dot_alignment
,
2275 uint64_t* load_address
)
2277 Memory_region
* vma_region
= NULL
;
2278 Memory_region
* lma_region
= NULL
;
2279 Script_sections
* script_sections
=
2280 layout
->script_options()->script_sections();
2282 uint64_t old_dot_value
= *dot_value
;
2283 uint64_t old_load_address
= *load_address
;
2285 // Decide the start address for the section. The algorithm is:
2286 // 1) If an address has been specified in a linker script, use that.
2287 // 2) Otherwise if a memory region has been specified for the section,
2288 // use the next free address in the region.
2289 // 3) Otherwise if memory regions have been specified find the first
2290 // region whose attributes are compatible with this section and
2291 // install it into that region.
2292 // 4) Otherwise use the current location counter.
2294 if (this->output_section_
!= NULL
2295 // Check for --section-start.
2296 && parameters
->options().section_start(this->output_section_
->name(),
2299 else if (this->address_
== NULL
)
2301 vma_region
= script_sections
->find_memory_region(this, true, NULL
);
2303 if (vma_region
!= NULL
)
2304 address
= vma_region
->get_current_address()->eval(symtab
, layout
,
2307 address
= *dot_value
;
2310 address
= this->address_
->eval_with_dot(symtab
, layout
, true,
2311 *dot_value
, NULL
, NULL
,
2314 if (this->align_
== NULL
)
2316 if (this->output_section_
== NULL
)
2319 align
= this->output_section_
->addralign();
2323 Output_section
* align_section
;
2324 align
= this->align_
->eval_with_dot(symtab
, layout
, true, *dot_value
,
2325 NULL
, &align_section
, NULL
);
2326 if (align_section
!= NULL
)
2327 gold_warning(_("alignment of section %s is not absolute"),
2328 this->name_
.c_str());
2329 if (this->output_section_
!= NULL
)
2330 this->output_section_
->set_addralign(align
);
2333 address
= align_address(address
, align
);
2335 uint64_t start_address
= address
;
2337 *dot_value
= address
;
2339 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2340 // forced to zero, regardless of what the linker script wants.
2341 if (this->output_section_
!= NULL
2342 && ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) != 0
2343 || this->output_section_
->is_noload()))
2344 this->output_section_
->set_address(address
);
2346 this->evaluated_address_
= address
;
2347 this->evaluated_addralign_
= align
;
2351 if (this->load_address_
== NULL
)
2353 Output_section_definition
* previous_section
;
2355 // Determine if an LMA region has been set for this section.
2356 lma_region
= script_sections
->find_memory_region(this, false,
2359 if (lma_region
!= NULL
)
2361 if (previous_section
== NULL
)
2362 // The LMA address was explicitly set to the given region.
2363 laddr
= lma_region
->get_current_address()->eval(symtab
, layout
,
2367 // We are not going to use the discovered lma_region, so
2368 // make sure that we do not update it in the code below.
2371 if (this->address_
!= NULL
|| previous_section
== this)
2373 // Either an explicit VMA address has been set, or an
2374 // explicit VMA region has been set, so set the LMA equal to
2380 // The LMA address was not explicitly or implicitly set.
2382 // We have been given the first memory region that is
2383 // compatible with the current section and a pointer to the
2384 // last section to use this region. Set the LMA of this
2385 // section so that the difference between its' VMA and LMA
2386 // is the same as the difference between the VMA and LMA of
2387 // the last section in the given region.
2388 laddr
= address
+ (previous_section
->evaluated_load_address_
2389 - previous_section
->evaluated_address_
);
2393 if (this->output_section_
!= NULL
)
2394 this->output_section_
->set_load_address(laddr
);
2398 // Do not set the load address of the output section, if one exists.
2399 // This allows future sections to determine what the load address
2400 // should be. If none is ever set, it will default to being the
2401 // same as the vma address.
2407 laddr
= this->load_address_
->eval_with_dot(symtab
, layout
, true,
2409 this->output_section_
,
2411 if (this->output_section_
!= NULL
)
2412 this->output_section_
->set_load_address(laddr
);
2415 this->evaluated_load_address_
= laddr
;
2418 if (this->subalign_
== NULL
)
2422 Output_section
* subalign_section
;
2423 subalign
= this->subalign_
->eval_with_dot(symtab
, layout
, true,
2425 &subalign_section
, NULL
);
2426 if (subalign_section
!= NULL
)
2427 gold_warning(_("subalign of section %s is not absolute"),
2428 this->name_
.c_str());
2432 if (this->fill_
!= NULL
)
2434 // FIXME: The GNU linker supports fill values of arbitrary
2436 Output_section
* fill_section
;
2437 uint64_t fill_val
= this->fill_
->eval_with_dot(symtab
, layout
, true,
2439 NULL
, &fill_section
,
2441 if (fill_section
!= NULL
)
2442 gold_warning(_("fill of section %s is not absolute"),
2443 this->name_
.c_str());
2444 unsigned char fill_buff
[4];
2445 elfcpp::Swap_unaligned
<32, true>::writeval(fill_buff
, fill_val
);
2446 fill
.assign(reinterpret_cast<char*>(fill_buff
), 4);
2449 Input_section_list input_sections
;
2450 if (this->output_section_
!= NULL
)
2452 // Get the list of input sections attached to this output
2453 // section. This will leave the output section with only
2454 // Output_section_data entries.
2455 address
+= this->output_section_
->get_input_sections(address
,
2458 *dot_value
= address
;
2461 Output_section
* dot_section
= this->output_section_
;
2462 for (Output_section_elements::iterator p
= this->elements_
.begin();
2463 p
!= this->elements_
.end();
2465 (*p
)->set_section_addresses(symtab
, layout
, this->output_section_
,
2466 subalign
, dot_value
, dot_alignment
,
2467 &dot_section
, &fill
, &input_sections
);
2469 gold_assert(input_sections
.empty());
2471 if (vma_region
!= NULL
)
2473 // Update the VMA region being used by the section now that we know how
2474 // big it is. Use the current address in the region, rather than
2475 // start_address because that might have been aligned upwards and we
2476 // need to allow for the padding.
2477 Expression
* addr
= vma_region
->get_current_address();
2478 uint64_t size
= *dot_value
- addr
->eval(symtab
, layout
, false);
2480 vma_region
->increment_offset(this->get_section_name(), size
,
2484 // If the LMA region is different from the VMA region, then increment the
2485 // offset there as well. Note that we use the same "dot_value -
2486 // start_address" formula that is used in the load_address assignment below.
2487 if (lma_region
!= NULL
&& lma_region
!= vma_region
)
2488 lma_region
->increment_offset(this->get_section_name(),
2489 *dot_value
- start_address
,
2492 // Compute the load address for the following section.
2493 if (this->output_section_
== NULL
)
2494 *load_address
= *dot_value
;
2495 else if (this->load_address_
== NULL
)
2497 if (lma_region
== NULL
)
2498 *load_address
= *dot_value
;
2501 lma_region
->get_current_address()->eval(symtab
, layout
, false);
2504 *load_address
= (this->output_section_
->load_address()
2505 + (*dot_value
- start_address
));
2507 if (this->output_section_
!= NULL
)
2509 if (this->is_relro_
)
2510 this->output_section_
->set_is_relro();
2512 this->output_section_
->clear_is_relro();
2514 // If this is a NOLOAD section, keep dot and load address unchanged.
2515 if (this->output_section_
->is_noload())
2517 *dot_value
= old_dot_value
;
2518 *load_address
= old_load_address
;
2523 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2524 // this section is constrained, and the input sections do not match,
2525 // return the constraint, and set *POSD.
2528 Output_section_definition::check_constraint(Output_section_definition
** posd
)
2530 switch (this->constraint_
)
2532 case CONSTRAINT_NONE
:
2533 return CONSTRAINT_NONE
;
2535 case CONSTRAINT_ONLY_IF_RO
:
2536 if (this->output_section_
!= NULL
2537 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) != 0)
2540 return CONSTRAINT_ONLY_IF_RO
;
2542 return CONSTRAINT_NONE
;
2544 case CONSTRAINT_ONLY_IF_RW
:
2545 if (this->output_section_
!= NULL
2546 && (this->output_section_
->flags() & elfcpp::SHF_WRITE
) == 0)
2549 return CONSTRAINT_ONLY_IF_RW
;
2551 return CONSTRAINT_NONE
;
2553 case CONSTRAINT_SPECIAL
:
2554 if (this->output_section_
!= NULL
)
2555 gold_error(_("SPECIAL constraints are not implemented"));
2556 return CONSTRAINT_NONE
;
2563 // See if this is the alternate output section for a constrained
2564 // output section. If it is, transfer the Output_section and return
2565 // true. Otherwise return false.
2568 Output_section_definition::alternate_constraint(
2569 Output_section_definition
* posd
,
2570 Section_constraint constraint
)
2572 if (this->name_
!= posd
->name_
)
2577 case CONSTRAINT_ONLY_IF_RO
:
2578 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RW
)
2582 case CONSTRAINT_ONLY_IF_RW
:
2583 if (this->constraint_
!= CONSTRAINT_ONLY_IF_RO
)
2591 // We have found the alternate constraint. We just need to move
2592 // over the Output_section. When constraints are used properly,
2593 // THIS should not have an output_section pointer, as all the input
2594 // sections should have matched the other definition.
2596 if (this->output_section_
!= NULL
)
2597 gold_error(_("mismatched definition for constrained sections"));
2599 this->output_section_
= posd
->output_section_
;
2600 posd
->output_section_
= NULL
;
2602 if (this->is_relro_
)
2603 this->output_section_
->set_is_relro();
2605 this->output_section_
->clear_is_relro();
2610 // Get the list of segments to use for an allocated section when using
2614 Output_section_definition::allocate_to_segment(String_list
** phdrs_list
,
2617 // Update phdrs_list even if we don't have an output section. It
2618 // might be used by the following sections.
2619 if (this->phdrs_
!= NULL
)
2620 *phdrs_list
= this->phdrs_
;
2622 if (this->output_section_
== NULL
)
2624 if ((this->output_section_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2627 return this->output_section_
;
2630 // Look for an output section by name and return the address, the load
2631 // address, the alignment, and the size. This is used when an
2632 // expression refers to an output section which was not actually
2633 // created. This returns true if the section was found, false
2637 Output_section_definition::get_output_section_info(const char* name
,
2639 uint64_t* load_address
,
2640 uint64_t* addralign
,
2641 uint64_t* size
) const
2643 if (this->name_
!= name
)
2646 if (this->output_section_
!= NULL
)
2648 *address
= this->output_section_
->address();
2649 if (this->output_section_
->has_load_address())
2650 *load_address
= this->output_section_
->load_address();
2652 *load_address
= *address
;
2653 *addralign
= this->output_section_
->addralign();
2654 *size
= this->output_section_
->current_data_size();
2658 *address
= this->evaluated_address_
;
2659 *load_address
= this->evaluated_load_address_
;
2660 *addralign
= this->evaluated_addralign_
;
2667 // Print for debugging.
2670 Output_section_definition::print(FILE* f
) const
2672 fprintf(f
, " %s ", this->name_
.c_str());
2674 if (this->address_
!= NULL
)
2676 this->address_
->print(f
);
2680 if (this->script_section_type_
!= SCRIPT_SECTION_TYPE_NONE
)
2682 this->script_section_type_name(this->script_section_type_
));
2686 if (this->load_address_
!= NULL
)
2689 this->load_address_
->print(f
);
2693 if (this->align_
!= NULL
)
2695 fprintf(f
, "ALIGN(");
2696 this->align_
->print(f
);
2700 if (this->subalign_
!= NULL
)
2702 fprintf(f
, "SUBALIGN(");
2703 this->subalign_
->print(f
);
2709 for (Output_section_elements::const_iterator p
= this->elements_
.begin();
2710 p
!= this->elements_
.end();
2716 if (this->fill_
!= NULL
)
2719 this->fill_
->print(f
);
2722 if (this->phdrs_
!= NULL
)
2724 for (String_list::const_iterator p
= this->phdrs_
->begin();
2725 p
!= this->phdrs_
->end();
2727 fprintf(f
, " :%s", p
->c_str());
2733 Script_sections::Section_type
2734 Output_section_definition::section_type() const
2736 switch (this->script_section_type_
)
2738 case SCRIPT_SECTION_TYPE_NONE
:
2739 return Script_sections::ST_NONE
;
2740 case SCRIPT_SECTION_TYPE_NOLOAD
:
2741 return Script_sections::ST_NOLOAD
;
2742 case SCRIPT_SECTION_TYPE_COPY
:
2743 case SCRIPT_SECTION_TYPE_DSECT
:
2744 case SCRIPT_SECTION_TYPE_INFO
:
2745 case SCRIPT_SECTION_TYPE_OVERLAY
:
2746 // There are not really support so we treat them as ST_NONE. The
2747 // parse should have issued errors for them already.
2748 return Script_sections::ST_NONE
;
2754 // Return the name of a script section type.
2757 Output_section_definition::script_section_type_name(
2758 Script_section_type script_section_type
)
2760 switch (script_section_type
)
2762 case SCRIPT_SECTION_TYPE_NONE
:
2764 case SCRIPT_SECTION_TYPE_NOLOAD
:
2766 case SCRIPT_SECTION_TYPE_DSECT
:
2768 case SCRIPT_SECTION_TYPE_COPY
:
2770 case SCRIPT_SECTION_TYPE_INFO
:
2772 case SCRIPT_SECTION_TYPE_OVERLAY
:
2780 Output_section_definition::set_memory_region(Memory_region
* mr
, bool set_vma
)
2782 gold_assert(mr
!= NULL
);
2783 // Add the current section to the specified region's list.
2784 mr
->add_section(this, set_vma
);
2787 // An output section created to hold orphaned input sections. These
2788 // do not actually appear in linker scripts. However, for convenience
2789 // when setting the output section addresses, we put a marker to these
2790 // sections in the appropriate place in the list of SECTIONS elements.
2792 class Orphan_output_section
: public Sections_element
2795 Orphan_output_section(Output_section
* os
)
2799 // Return whether the orphan output section is relro. We can just
2800 // check the output section because we always set the flag, if
2801 // needed, just after we create the Orphan_output_section.
2804 { return this->os_
->is_relro(); }
2806 // Initialize OSP with an output section. This should have been
2809 orphan_section_init(Orphan_section_placement
*,
2810 Script_sections::Elements_iterator
)
2811 { gold_unreachable(); }
2813 // Set section addresses.
2815 set_section_addresses(Symbol_table
*, Layout
*, uint64_t*, uint64_t*,
2818 // Get the list of segments to use for an allocated section when
2819 // using a PHDRS clause.
2821 allocate_to_segment(String_list
**, bool*);
2823 // Return the associated Output_section.
2825 get_output_section() const
2826 { return this->os_
; }
2828 // Print for debugging.
2830 print(FILE* f
) const
2832 fprintf(f
, " marker for orphaned output section %s\n",
2837 Output_section
* os_
;
2840 // Set section addresses.
2843 Orphan_output_section::set_section_addresses(Symbol_table
*, Layout
*,
2844 uint64_t* dot_value
,
2846 uint64_t* load_address
)
2848 typedef std::list
<Output_section::Input_section
> Input_section_list
;
2850 bool have_load_address
= *load_address
!= *dot_value
;
2852 uint64_t address
= *dot_value
;
2853 address
= align_address(address
, this->os_
->addralign());
2855 // For a relocatable link, all orphan sections are put at
2856 // address 0. In general we expect all sections to be at
2857 // address 0 for a relocatable link, but we permit the linker
2858 // script to override that for specific output sections.
2859 if (parameters
->options().relocatable())
2863 have_load_address
= false;
2866 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) != 0)
2868 this->os_
->set_address(address
);
2869 if (have_load_address
)
2870 this->os_
->set_load_address(align_address(*load_address
,
2871 this->os_
->addralign()));
2874 Input_section_list input_sections
;
2875 address
+= this->os_
->get_input_sections(address
, "", &input_sections
);
2877 for (Input_section_list::iterator p
= input_sections
.begin();
2878 p
!= input_sections
.end();
2881 uint64_t addralign
= p
->addralign();
2882 if (!p
->is_input_section())
2883 p
->output_section_data()->finalize_data_size();
2884 uint64_t size
= p
->data_size();
2885 address
= align_address(address
, addralign
);
2886 this->os_
->add_script_input_section(*p
);
2890 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2891 if (this->os_
== NULL
2892 || (this->os_
->flags() & elfcpp::SHF_TLS
) == 0
2893 || this->os_
->type() != elfcpp::SHT_NOBITS
)
2895 if (!have_load_address
)
2896 *load_address
= address
;
2898 *load_address
+= address
- *dot_value
;
2900 *dot_value
= address
;
2904 // Get the list of segments to use for an allocated section when using
2905 // a PHDRS clause. If this is an allocated section, return the
2906 // Output_section. We don't change the list of segments.
2909 Orphan_output_section::allocate_to_segment(String_list
**, bool* orphan
)
2911 if ((this->os_
->flags() & elfcpp::SHF_ALLOC
) == 0)
2917 // Class Phdrs_element. A program header from a PHDRS clause.
2922 Phdrs_element(const char* name
, size_t namelen
, unsigned int type
,
2923 bool includes_filehdr
, bool includes_phdrs
,
2924 bool is_flags_valid
, unsigned int flags
,
2925 Expression
* load_address
)
2926 : name_(name
, namelen
), type_(type
), includes_filehdr_(includes_filehdr
),
2927 includes_phdrs_(includes_phdrs
), is_flags_valid_(is_flags_valid
),
2928 flags_(flags
), load_address_(load_address
), load_address_value_(0),
2932 // Return the name of this segment.
2935 { return this->name_
; }
2937 // Return the type of the segment.
2940 { return this->type_
; }
2942 // Whether to include the file header.
2944 includes_filehdr() const
2945 { return this->includes_filehdr_
; }
2947 // Whether to include the program headers.
2949 includes_phdrs() const
2950 { return this->includes_phdrs_
; }
2952 // Return whether there is a load address.
2954 has_load_address() const
2955 { return this->load_address_
!= NULL
; }
2957 // Evaluate the load address expression if there is one.
2959 eval_load_address(Symbol_table
* symtab
, Layout
* layout
)
2961 if (this->load_address_
!= NULL
)
2962 this->load_address_value_
= this->load_address_
->eval(symtab
, layout
,
2966 // Return the load address.
2968 load_address() const
2970 gold_assert(this->load_address_
!= NULL
);
2971 return this->load_address_value_
;
2974 // Create the segment.
2976 create_segment(Layout
* layout
)
2978 this->segment_
= layout
->make_output_segment(this->type_
, this->flags_
);
2979 return this->segment_
;
2982 // Return the segment.
2985 { return this->segment_
; }
2987 // Release the segment.
2990 { this->segment_
= NULL
; }
2992 // Set the segment flags if appropriate.
2994 set_flags_if_valid()
2996 if (this->is_flags_valid_
)
2997 this->segment_
->set_flags(this->flags_
);
3000 // Print for debugging.
3005 // The name used in the script.
3007 // The type of the segment (PT_LOAD, etc.).
3009 // Whether this segment includes the file header.
3010 bool includes_filehdr_
;
3011 // Whether this segment includes the section headers.
3012 bool includes_phdrs_
;
3013 // Whether the flags were explicitly specified.
3014 bool is_flags_valid_
;
3015 // The flags for this segment (PF_R, etc.) if specified.
3016 unsigned int flags_
;
3017 // The expression for the load address for this segment. This may
3019 Expression
* load_address_
;
3020 // The actual load address from evaluating the expression.
3021 uint64_t load_address_value_
;
3022 // The segment itself.
3023 Output_segment
* segment_
;
3026 // Print for debugging.
3029 Phdrs_element::print(FILE* f
) const
3031 fprintf(f
, " %s 0x%x", this->name_
.c_str(), this->type_
);
3032 if (this->includes_filehdr_
)
3033 fprintf(f
, " FILEHDR");
3034 if (this->includes_phdrs_
)
3035 fprintf(f
, " PHDRS");
3036 if (this->is_flags_valid_
)
3037 fprintf(f
, " FLAGS(%u)", this->flags_
);
3038 if (this->load_address_
!= NULL
)
3041 this->load_address_
->print(f
);
3047 // Add a memory region.
3050 Script_sections::add_memory_region(const char* name
, size_t namelen
,
3051 unsigned int attributes
,
3052 Expression
* start
, Expression
* length
)
3054 if (this->memory_regions_
== NULL
)
3055 this->memory_regions_
= new Memory_regions();
3056 else if (this->find_memory_region(name
, namelen
))
3058 gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen
),
3060 // FIXME: Add a GOLD extension to allow multiple regions with the same
3061 // name. This would amount to a single region covering disjoint blocks
3062 // of memory, which is useful for embedded devices.
3065 // FIXME: Check the length and start values. Currently we allow
3066 // non-constant expressions for these values, whereas LD does not.
3068 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
3069 // describe a region that packs from the end address going down, rather
3070 // than the start address going up. This would be useful for embedded
3073 this->memory_regions_
->push_back(new Memory_region(name
, namelen
, attributes
,
3077 // Find a memory region.
3080 Script_sections::find_memory_region(const char* name
, size_t namelen
)
3082 if (this->memory_regions_
== NULL
)
3085 for (Memory_regions::const_iterator m
= this->memory_regions_
->begin();
3086 m
!= this->memory_regions_
->end();
3088 if ((*m
)->name_match(name
, namelen
))
3094 // Find a memory region's origin.
3097 Script_sections::find_memory_region_origin(const char* name
, size_t namelen
)
3099 Memory_region
* mr
= find_memory_region(name
, namelen
);
3103 return mr
->start_address();
3106 // Find a memory region's length.
3109 Script_sections::find_memory_region_length(const char* name
, size_t namelen
)
3111 Memory_region
* mr
= find_memory_region(name
, namelen
);
3115 return mr
->length();
3118 // Set the memory region to use for the current section.
3121 Script_sections::set_memory_region(Memory_region
* mr
, bool set_vma
)
3123 gold_assert(!this->sections_elements_
->empty());
3124 this->sections_elements_
->back()->set_memory_region(mr
, set_vma
);
3127 // Class Script_sections.
3129 Script_sections::Script_sections()
3130 : saw_sections_clause_(false),
3131 in_sections_clause_(false),
3132 sections_elements_(NULL
),
3133 output_section_(NULL
),
3134 memory_regions_(NULL
),
3135 phdrs_elements_(NULL
),
3136 orphan_section_placement_(NULL
),
3137 data_segment_align_start_(),
3138 saw_data_segment_align_(false),
3139 saw_relro_end_(false),
3140 saw_segment_start_expression_(false)
3144 // Start a SECTIONS clause.
3147 Script_sections::start_sections()
3149 gold_assert(!this->in_sections_clause_
&& this->output_section_
== NULL
);
3150 this->saw_sections_clause_
= true;
3151 this->in_sections_clause_
= true;
3152 if (this->sections_elements_
== NULL
)
3153 this->sections_elements_
= new Sections_elements
;
3156 // Finish a SECTIONS clause.
3159 Script_sections::finish_sections()
3161 gold_assert(this->in_sections_clause_
&& this->output_section_
== NULL
);
3162 this->in_sections_clause_
= false;
3165 // Add a symbol to be defined.
3168 Script_sections::add_symbol_assignment(const char* name
, size_t length
,
3169 Expression
* val
, bool provide
,
3172 if (this->output_section_
!= NULL
)
3173 this->output_section_
->add_symbol_assignment(name
, length
, val
,
3177 Sections_element
* p
= new Sections_element_assignment(name
, length
,
3180 this->sections_elements_
->push_back(p
);
3184 // Add an assignment to the special dot symbol.
3187 Script_sections::add_dot_assignment(Expression
* val
)
3189 if (this->output_section_
!= NULL
)
3190 this->output_section_
->add_dot_assignment(val
);
3193 // The GNU linker permits assignments to . to appears outside of
3194 // a SECTIONS clause, and treats it as appearing inside, so
3195 // sections_elements_ may be NULL here.
3196 if (this->sections_elements_
== NULL
)
3198 this->sections_elements_
= new Sections_elements
;
3199 this->saw_sections_clause_
= true;
3202 Sections_element
* p
= new Sections_element_dot_assignment(val
);
3203 this->sections_elements_
->push_back(p
);
3207 // Add an assertion.
3210 Script_sections::add_assertion(Expression
* check
, const char* message
,
3213 if (this->output_section_
!= NULL
)
3214 this->output_section_
->add_assertion(check
, message
, messagelen
);
3217 Sections_element
* p
= new Sections_element_assertion(check
, message
,
3219 this->sections_elements_
->push_back(p
);
3223 // Start processing entries for an output section.
3226 Script_sections::start_output_section(
3229 const Parser_output_section_header
* header
)
3231 Output_section_definition
* posd
= new Output_section_definition(name
,
3234 this->sections_elements_
->push_back(posd
);
3235 gold_assert(this->output_section_
== NULL
);
3236 this->output_section_
= posd
;
3239 // Stop processing entries for an output section.
3242 Script_sections::finish_output_section(
3243 const Parser_output_section_trailer
* trailer
)
3245 gold_assert(this->output_section_
!= NULL
);
3246 this->output_section_
->finish(trailer
);
3247 this->output_section_
= NULL
;
3250 // Add a data item to the current output section.
3253 Script_sections::add_data(int size
, bool is_signed
, Expression
* val
)
3255 gold_assert(this->output_section_
!= NULL
);
3256 this->output_section_
->add_data(size
, is_signed
, val
);
3259 // Add a fill value setting to the current output section.
3262 Script_sections::add_fill(Expression
* val
)
3264 gold_assert(this->output_section_
!= NULL
);
3265 this->output_section_
->add_fill(val
);
3268 // Add an input section specification to the current output section.
3271 Script_sections::add_input_section(const Input_section_spec
* spec
, bool keep
)
3273 gold_assert(this->output_section_
!= NULL
);
3274 this->output_section_
->add_input_section(spec
, keep
);
3277 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
3278 // subsequent output sections may be relro.
3281 Script_sections::data_segment_align()
3283 if (this->saw_data_segment_align_
)
3284 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3285 gold_assert(!this->sections_elements_
->empty());
3286 Sections_elements::iterator p
= this->sections_elements_
->end();
3288 this->data_segment_align_start_
= p
;
3289 this->saw_data_segment_align_
= true;
3292 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
3293 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3296 Script_sections::data_segment_relro_end()
3298 if (this->saw_relro_end_
)
3299 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3300 "in a linker script"));
3301 this->saw_relro_end_
= true;
3303 if (!this->saw_data_segment_align_
)
3304 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3307 Sections_elements::iterator p
= this->data_segment_align_start_
;
3308 for (++p
; p
!= this->sections_elements_
->end(); ++p
)
3309 (*p
)->set_is_relro();
3313 // Create any required sections.
3316 Script_sections::create_sections(Layout
* layout
)
3318 if (!this->saw_sections_clause_
)
3320 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3321 p
!= this->sections_elements_
->end();
3323 (*p
)->create_sections(layout
);
3326 // Add any symbols we are defining to the symbol table.
3329 Script_sections::add_symbols_to_table(Symbol_table
* symtab
)
3331 if (!this->saw_sections_clause_
)
3333 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3334 p
!= this->sections_elements_
->end();
3336 (*p
)->add_symbols_to_table(symtab
);
3339 // Finalize symbols and check assertions.
3342 Script_sections::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
3344 if (!this->saw_sections_clause_
)
3346 uint64_t dot_value
= 0;
3347 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3348 p
!= this->sections_elements_
->end();
3350 (*p
)->finalize_symbols(symtab
, layout
, &dot_value
);
3353 // Return the name of the output section to use for an input file name
3354 // and section name.
3357 Script_sections::output_section_name(
3358 const char* file_name
,
3359 const char* section_name
,
3360 Output_section
*** output_section_slot
,
3361 Script_sections::Section_type
* psection_type
)
3363 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3364 p
!= this->sections_elements_
->end();
3367 const char* ret
= (*p
)->output_section_name(file_name
, section_name
,
3368 output_section_slot
,
3373 // The special name /DISCARD/ means that the input section
3374 // should be discarded.
3375 if (strcmp(ret
, "/DISCARD/") == 0)
3377 *output_section_slot
= NULL
;
3378 *psection_type
= Script_sections::ST_NONE
;
3385 // If we couldn't find a mapping for the name, the output section
3386 // gets the name of the input section.
3388 *output_section_slot
= NULL
;
3389 *psection_type
= Script_sections::ST_NONE
;
3391 return section_name
;
3394 // Place a marker for an orphan output section into the SECTIONS
3398 Script_sections::place_orphan(Output_section
* os
)
3400 Orphan_section_placement
* osp
= this->orphan_section_placement_
;
3403 // Initialize the Orphan_section_placement structure.
3404 osp
= new Orphan_section_placement();
3405 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3406 p
!= this->sections_elements_
->end();
3408 (*p
)->orphan_section_init(osp
, p
);
3409 gold_assert(!this->sections_elements_
->empty());
3410 Sections_elements::iterator last
= this->sections_elements_
->end();
3412 osp
->last_init(last
);
3413 this->orphan_section_placement_
= osp
;
3416 Orphan_output_section
* orphan
= new Orphan_output_section(os
);
3418 // Look for where to put ORPHAN.
3419 Sections_elements::iterator
* where
;
3420 if (osp
->find_place(os
, &where
))
3422 if ((**where
)->is_relro())
3425 os
->clear_is_relro();
3427 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3428 // so that the next one goes after this one.
3429 Sections_elements::iterator p
= *where
;
3430 gold_assert(p
!= this->sections_elements_
->end());
3432 *where
= this->sections_elements_
->insert(p
, orphan
);
3436 os
->clear_is_relro();
3437 // We don't have a place to put this orphan section. Put it,
3438 // and all other sections like it, at the end, but before the
3439 // sections which always come at the end.
3440 Sections_elements::iterator last
= osp
->last_place();
3441 *where
= this->sections_elements_
->insert(last
, orphan
);
3445 // Set the addresses of all the output sections. Walk through all the
3446 // elements, tracking the dot symbol. Apply assignments which set
3447 // absolute symbol values, in case they are used when setting dot.
3448 // Fill in data statement values. As we find output sections, set the
3449 // address, set the address of all associated input sections, and
3450 // update dot. Return the segment which should hold the file header
3451 // and segment headers, if any.
3454 Script_sections::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
3456 gold_assert(this->saw_sections_clause_
);
3458 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3459 // for our representation.
3460 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3461 p
!= this->sections_elements_
->end();
3464 Output_section_definition
* posd
;
3465 Section_constraint failed_constraint
= (*p
)->check_constraint(&posd
);
3466 if (failed_constraint
!= CONSTRAINT_NONE
)
3468 Sections_elements::iterator q
;
3469 for (q
= this->sections_elements_
->begin();
3470 q
!= this->sections_elements_
->end();
3475 if ((*q
)->alternate_constraint(posd
, failed_constraint
))
3480 if (q
== this->sections_elements_
->end())
3481 gold_error(_("no matching section constraint"));
3485 // Force the alignment of the first TLS section to be the maximum
3486 // alignment of all TLS sections.
3487 Output_section
* first_tls
= NULL
;
3488 uint64_t tls_align
= 0;
3489 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
3490 p
!= this->sections_elements_
->end();
3493 Output_section
* os
= (*p
)->get_output_section();
3494 if (os
!= NULL
&& (os
->flags() & elfcpp::SHF_TLS
) != 0)
3496 if (first_tls
== NULL
)
3498 if (os
->addralign() > tls_align
)
3499 tls_align
= os
->addralign();
3502 if (first_tls
!= NULL
)
3503 first_tls
->set_addralign(tls_align
);
3505 // For a relocatable link, we implicitly set dot to zero.
3506 uint64_t dot_value
= 0;
3507 uint64_t dot_alignment
= 0;
3508 uint64_t load_address
= 0;
3510 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3511 // to set section addresses. If the script has any SEGMENT_START
3512 // expression, we do not set the section addresses.
3513 bool use_tsection_options
=
3514 (!this->saw_segment_start_expression_
3515 && (parameters
->options().user_set_Ttext()
3516 || parameters
->options().user_set_Tdata()
3517 || parameters
->options().user_set_Tbss()));
3519 for (Sections_elements::iterator p
= this->sections_elements_
->begin();
3520 p
!= this->sections_elements_
->end();
3523 Output_section
* os
= (*p
)->get_output_section();
3525 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3526 // the special sections by names and doing dot assignments.
3527 if (use_tsection_options
3529 && (os
->flags() & elfcpp::SHF_ALLOC
) != 0)
3531 uint64_t new_dot_value
= dot_value
;
3533 if (parameters
->options().user_set_Ttext()
3534 && strcmp(os
->name(), ".text") == 0)
3535 new_dot_value
= parameters
->options().Ttext();
3536 else if (parameters
->options().user_set_Tdata()
3537 && strcmp(os
->name(), ".data") == 0)
3538 new_dot_value
= parameters
->options().Tdata();
3539 else if (parameters
->options().user_set_Tbss()
3540 && strcmp(os
->name(), ".bss") == 0)
3541 new_dot_value
= parameters
->options().Tbss();
3543 // Update dot and load address if necessary.
3544 if (new_dot_value
< dot_value
)
3545 gold_error(_("dot may not move backward"));
3546 else if (new_dot_value
!= dot_value
)
3548 dot_value
= new_dot_value
;
3549 load_address
= new_dot_value
;
3553 (*p
)->set_section_addresses(symtab
, layout
, &dot_value
, &dot_alignment
,
3557 if (this->phdrs_elements_
!= NULL
)
3559 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
3560 p
!= this->phdrs_elements_
->end();
3562 (*p
)->eval_load_address(symtab
, layout
);
3565 return this->create_segments(layout
, dot_alignment
);
3568 // Sort the sections in order to put them into segments.
3570 class Sort_output_sections
3573 Sort_output_sections(const Script_sections::Sections_elements
* elements
)
3574 : elements_(elements
)
3578 operator()(const Output_section
* os1
, const Output_section
* os2
) const;
3582 script_compare(const Output_section
* os1
, const Output_section
* os2
) const;
3585 const Script_sections::Sections_elements
* elements_
;
3589 Sort_output_sections::operator()(const Output_section
* os1
,
3590 const Output_section
* os2
) const
3592 // Sort first by the load address.
3593 uint64_t lma1
= (os1
->has_load_address()
3594 ? os1
->load_address()
3596 uint64_t lma2
= (os2
->has_load_address()
3597 ? os2
->load_address()
3602 // Then sort by the virtual address.
3603 if (os1
->address() != os2
->address())
3604 return os1
->address() < os2
->address();
3606 // If the linker script says which of these sections is first, go
3607 // with what it says.
3608 int i
= this->script_compare(os1
, os2
);
3612 // Sort PROGBITS before NOBITS.
3613 bool nobits1
= os1
->type() == elfcpp::SHT_NOBITS
;
3614 bool nobits2
= os2
->type() == elfcpp::SHT_NOBITS
;
3615 if (nobits1
!= nobits2
)
3618 // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
3620 bool tls1
= (os1
->flags() & elfcpp::SHF_TLS
) != 0;
3621 bool tls2
= (os2
->flags() & elfcpp::SHF_TLS
) != 0;
3623 return nobits1
? tls1
: tls2
;
3625 // Sort non-NOLOAD before NOLOAD.
3626 if (os1
->is_noload() && !os2
->is_noload())
3628 if (!os1
->is_noload() && os2
->is_noload())
3631 // The sections seem practically identical. Sort by name to get a
3633 return os1
->name() < os2
->name();
3636 // Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
3637 // if either OS1 or OS2 is not mentioned. This ensures that we keep
3638 // empty sections in the order in which they appear in a linker
3642 Sort_output_sections::script_compare(const Output_section
* os1
,
3643 const Output_section
* os2
) const
3645 if (this->elements_
== NULL
)
3648 bool found_os1
= false;
3649 bool found_os2
= false;
3650 for (Script_sections::Sections_elements::const_iterator
3651 p
= this->elements_
->begin();
3652 p
!= this->elements_
->end();
3655 if (os2
== (*p
)->get_output_section())
3661 else if (os1
== (*p
)->get_output_section())
3672 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3673 // We treat a section with the SHF_TLS flag set as taking up space
3674 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3675 // space for them in the file.
3678 Script_sections::is_bss_section(const Output_section
* os
)
3680 return (os
->type() == elfcpp::SHT_NOBITS
3681 && (os
->flags() & elfcpp::SHF_TLS
) == 0);
3684 // Return the size taken by the file header and the program headers.
3687 Script_sections::total_header_size(Layout
* layout
) const
3689 size_t segment_count
= layout
->segment_count();
3690 size_t file_header_size
;
3691 size_t segment_headers_size
;
3692 if (parameters
->target().get_size() == 32)
3694 file_header_size
= elfcpp::Elf_sizes
<32>::ehdr_size
;
3695 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<32>::phdr_size
;
3697 else if (parameters
->target().get_size() == 64)
3699 file_header_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
3700 segment_headers_size
= segment_count
* elfcpp::Elf_sizes
<64>::phdr_size
;
3705 return file_header_size
+ segment_headers_size
;
3708 // Return the amount we have to subtract from the LMA to accommodate
3709 // headers of the given size. The complication is that the file
3710 // header have to be at the start of a page, as otherwise it will not
3711 // be at the start of the file.
3714 Script_sections::header_size_adjustment(uint64_t lma
,
3715 size_t sizeof_headers
) const
3717 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3718 uint64_t hdr_lma
= lma
- sizeof_headers
;
3719 hdr_lma
&= ~(abi_pagesize
- 1);
3720 return lma
- hdr_lma
;
3723 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3724 // the segment which should hold the file header and segment headers,
3728 Script_sections::create_segments(Layout
* layout
, uint64_t dot_alignment
)
3730 gold_assert(this->saw_sections_clause_
);
3732 if (parameters
->options().relocatable())
3735 if (this->saw_phdrs_clause())
3736 return create_segments_from_phdrs_clause(layout
, dot_alignment
);
3738 Layout::Section_list sections
;
3739 layout
->get_allocated_sections(§ions
);
3741 // Sort the sections by address.
3742 std::stable_sort(sections
.begin(), sections
.end(),
3743 Sort_output_sections(this->sections_elements_
));
3745 this->create_note_and_tls_segments(layout
, §ions
);
3747 // Walk through the sections adding them to PT_LOAD segments.
3748 const uint64_t abi_pagesize
= parameters
->target().abi_pagesize();
3749 Output_segment
* first_seg
= NULL
;
3750 Output_segment
* current_seg
= NULL
;
3751 bool is_current_seg_readonly
= true;
3752 Layout::Section_list::iterator plast
= sections
.end();
3753 uint64_t last_vma
= 0;
3754 uint64_t last_lma
= 0;
3755 uint64_t last_size
= 0;
3756 for (Layout::Section_list::iterator p
= sections
.begin();
3757 p
!= sections
.end();
3760 const uint64_t vma
= (*p
)->address();
3761 const uint64_t lma
= ((*p
)->has_load_address()
3762 ? (*p
)->load_address()
3764 const uint64_t size
= (*p
)->current_data_size();
3766 bool need_new_segment
;
3767 if (current_seg
== NULL
)
3768 need_new_segment
= true;
3769 else if (lma
- vma
!= last_lma
- last_vma
)
3771 // This section has a different LMA relationship than the
3772 // last one; we need a new segment.
3773 need_new_segment
= true;
3775 else if (align_address(last_lma
+ last_size
, abi_pagesize
)
3776 < align_address(lma
, abi_pagesize
))
3778 // Putting this section in the segment would require
3780 need_new_segment
= true;
3782 else if (is_bss_section(*plast
) && !is_bss_section(*p
))
3784 // A non-BSS section can not follow a BSS section in the
3786 need_new_segment
= true;
3788 else if (is_current_seg_readonly
3789 && ((*p
)->flags() & elfcpp::SHF_WRITE
) != 0
3790 && !parameters
->options().omagic())
3792 // Don't put a writable section in the same segment as a
3793 // non-writable section.
3794 need_new_segment
= true;
3798 // Otherwise, reuse the existing segment.
3799 need_new_segment
= false;
3802 elfcpp::Elf_Word seg_flags
=
3803 Layout::section_flags_to_segment((*p
)->flags());
3805 if (need_new_segment
)
3807 current_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3809 current_seg
->set_addresses(vma
, lma
);
3810 current_seg
->set_minimum_p_align(dot_alignment
);
3811 if (first_seg
== NULL
)
3812 first_seg
= current_seg
;
3813 is_current_seg_readonly
= true;
3816 current_seg
->add_output_section_to_load(layout
, *p
, seg_flags
);
3818 if (((*p
)->flags() & elfcpp::SHF_WRITE
) != 0)
3819 is_current_seg_readonly
= false;
3827 // An ELF program should work even if the program headers are not in
3828 // a PT_LOAD segment. However, it appears that the Linux kernel
3829 // does not set the AT_PHDR auxiliary entry in that case. It sets
3830 // the load address to p_vaddr - p_offset of the first PT_LOAD
3831 // segment. It then sets AT_PHDR to the load address plus the
3832 // offset to the program headers, e_phoff in the file header. This
3833 // fails when the program headers appear in the file before the
3834 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3835 // segment to hold the file header and the program headers. This is
3836 // effectively what the GNU linker does, and it is slightly more
3837 // efficient in any case. We try to use the first PT_LOAD segment
3838 // if we can, otherwise we make a new one.
3840 if (first_seg
== NULL
)
3843 // -n or -N mean that the program is not demand paged and there is
3844 // no need to put the program headers in a PT_LOAD segment.
3845 if (parameters
->options().nmagic() || parameters
->options().omagic())
3848 size_t sizeof_headers
= this->total_header_size(layout
);
3850 uint64_t vma
= first_seg
->vaddr();
3851 uint64_t lma
= first_seg
->paddr();
3853 uint64_t subtract
= this->header_size_adjustment(lma
, sizeof_headers
);
3855 if ((lma
& (abi_pagesize
- 1)) >= sizeof_headers
)
3857 first_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3861 // If there is no room to squeeze in the headers, then punt. The
3862 // resulting executable probably won't run on GNU/Linux, but we
3863 // trust that the user knows what they are doing.
3864 if (lma
< subtract
|| vma
< subtract
)
3867 // If memory regions have been specified and the address range
3868 // we are about to use is not contained within any region then
3869 // issue a warning message about the segment we are going to
3870 // create. It will be outside of any region and so possibly
3871 // using non-existent or protected memory. We test LMA rather
3872 // than VMA since we assume that the headers will never be
3874 if (this->memory_regions_
!= NULL
3875 && !this->block_in_region (NULL
, layout
, lma
- subtract
, subtract
))
3876 gold_warning(_("creating a segment to contain the file and program"
3877 " headers outside of any MEMORY region"));
3879 Output_segment
* load_seg
= layout
->make_output_segment(elfcpp::PT_LOAD
,
3881 load_seg
->set_addresses(vma
- subtract
, lma
- subtract
);
3886 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3887 // segment if there are any SHT_TLS sections.
3890 Script_sections::create_note_and_tls_segments(
3892 const Layout::Section_list
* sections
)
3894 gold_assert(!this->saw_phdrs_clause());
3896 bool saw_tls
= false;
3897 for (Layout::Section_list::const_iterator p
= sections
->begin();
3898 p
!= sections
->end();
3901 if ((*p
)->type() == elfcpp::SHT_NOTE
)
3903 elfcpp::Elf_Word seg_flags
=
3904 Layout::section_flags_to_segment((*p
)->flags());
3905 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_NOTE
,
3907 oseg
->add_output_section_to_nonload(*p
, seg_flags
);
3909 // Incorporate any subsequent SHT_NOTE sections, in the
3910 // hopes that the script is sensible.
3911 Layout::Section_list::const_iterator pnext
= p
+ 1;
3912 while (pnext
!= sections
->end()
3913 && (*pnext
)->type() == elfcpp::SHT_NOTE
)
3915 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3916 oseg
->add_output_section_to_nonload(*pnext
, seg_flags
);
3922 if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
3925 gold_error(_("TLS sections are not adjacent"));
3927 elfcpp::Elf_Word seg_flags
=
3928 Layout::section_flags_to_segment((*p
)->flags());
3929 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_TLS
,
3931 oseg
->add_output_section_to_nonload(*p
, seg_flags
);
3933 Layout::Section_list::const_iterator pnext
= p
+ 1;
3934 while (pnext
!= sections
->end()
3935 && ((*pnext
)->flags() & elfcpp::SHF_TLS
) != 0)
3937 seg_flags
= Layout::section_flags_to_segment((*pnext
)->flags());
3938 oseg
->add_output_section_to_nonload(*pnext
, seg_flags
);
3946 // If we are making a shared library, and we see a section named
3947 // .interp then put the .interp section in a PT_INTERP segment.
3948 // This is for GNU ld compatibility.
3949 if (strcmp((*p
)->name(), ".interp") == 0)
3951 elfcpp::Elf_Word seg_flags
=
3952 Layout::section_flags_to_segment((*p
)->flags());
3953 Output_segment
* oseg
= layout
->make_output_segment(elfcpp::PT_INTERP
,
3955 oseg
->add_output_section_to_nonload(*p
, seg_flags
);
3960 // Add a program header. The PHDRS clause is syntactically distinct
3961 // from the SECTIONS clause, but we implement it with the SECTIONS
3962 // support because PHDRS is useless if there is no SECTIONS clause.
3965 Script_sections::add_phdr(const char* name
, size_t namelen
, unsigned int type
,
3966 bool includes_filehdr
, bool includes_phdrs
,
3967 bool is_flags_valid
, unsigned int flags
,
3968 Expression
* load_address
)
3970 if (this->phdrs_elements_
== NULL
)
3971 this->phdrs_elements_
= new Phdrs_elements();
3972 this->phdrs_elements_
->push_back(new Phdrs_element(name
, namelen
, type
,
3975 is_flags_valid
, flags
,
3979 // Return the number of segments we expect to create based on the
3980 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3983 Script_sections::expected_segment_count(const Layout
* layout
) const
3985 if (this->saw_phdrs_clause())
3986 return this->phdrs_elements_
->size();
3988 Layout::Section_list sections
;
3989 layout
->get_allocated_sections(§ions
);
3991 // We assume that we will need two PT_LOAD segments.
3994 bool saw_note
= false;
3995 bool saw_tls
= false;
3996 for (Layout::Section_list::const_iterator p
= sections
.begin();
3997 p
!= sections
.end();
4000 if ((*p
)->type() == elfcpp::SHT_NOTE
)
4002 // Assume that all note sections will fit into a single
4010 else if (((*p
)->flags() & elfcpp::SHF_TLS
) != 0)
4012 // There can only be one PT_TLS segment.
4024 // Create the segments from a PHDRS clause. Return the segment which
4025 // should hold the file header and program headers, if any.
4028 Script_sections::create_segments_from_phdrs_clause(Layout
* layout
,
4029 uint64_t dot_alignment
)
4031 this->attach_sections_using_phdrs_clause(layout
);
4032 return this->set_phdrs_clause_addresses(layout
, dot_alignment
);
4035 // Create the segments from the PHDRS clause, and put the output
4036 // sections in them.
4039 Script_sections::attach_sections_using_phdrs_clause(Layout
* layout
)
4041 typedef std::map
<std::string
, Output_segment
*> Name_to_segment
;
4042 Name_to_segment name_to_segment
;
4043 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
4044 p
!= this->phdrs_elements_
->end();
4046 name_to_segment
[(*p
)->name()] = (*p
)->create_segment(layout
);
4048 // Walk through the output sections and attach them to segments.
4049 // Output sections in the script which do not list segments are
4050 // attached to the same set of segments as the immediately preceding
4053 String_list
* phdr_names
= NULL
;
4054 bool load_segments_only
= false;
4055 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
4056 p
!= this->sections_elements_
->end();
4060 String_list
* old_phdr_names
= phdr_names
;
4061 Output_section
* os
= (*p
)->allocate_to_segment(&phdr_names
, &is_orphan
);
4065 elfcpp::Elf_Word seg_flags
=
4066 Layout::section_flags_to_segment(os
->flags());
4068 if (phdr_names
== NULL
)
4070 // Don't worry about empty orphan sections.
4071 if (is_orphan
&& os
->current_data_size() > 0)
4072 gold_error(_("allocated section %s not in any segment"),
4075 // To avoid later crashes drop this section into the first
4077 for (Phdrs_elements::const_iterator ppe
=
4078 this->phdrs_elements_
->begin();
4079 ppe
!= this->phdrs_elements_
->end();
4082 Output_segment
* oseg
= (*ppe
)->segment();
4083 if (oseg
->type() == elfcpp::PT_LOAD
)
4085 oseg
->add_output_section_to_load(layout
, os
, seg_flags
);
4093 // We see a list of segments names. Disable PT_LOAD segment only
4095 if (old_phdr_names
!= phdr_names
)
4096 load_segments_only
= false;
4098 // If this is an orphan section--one that was not explicitly
4099 // mentioned in the linker script--then it should not inherit
4100 // any segment type other than PT_LOAD. Otherwise, e.g., the
4101 // PT_INTERP segment will pick up following orphan sections,
4102 // which does not make sense. If this is not an orphan section,
4103 // we trust the linker script.
4106 // Enable PT_LOAD segments only filtering until we see another
4107 // list of segment names.
4108 load_segments_only
= true;
4111 bool in_load_segment
= false;
4112 for (String_list::const_iterator q
= phdr_names
->begin();
4113 q
!= phdr_names
->end();
4116 Name_to_segment::const_iterator r
= name_to_segment
.find(*q
);
4117 if (r
== name_to_segment
.end())
4118 gold_error(_("no segment %s"), q
->c_str());
4121 if (load_segments_only
4122 && r
->second
->type() != elfcpp::PT_LOAD
)
4125 if (r
->second
->type() != elfcpp::PT_LOAD
)
4126 r
->second
->add_output_section_to_nonload(os
, seg_flags
);
4129 r
->second
->add_output_section_to_load(layout
, os
, seg_flags
);
4130 if (in_load_segment
)
4131 gold_error(_("section in two PT_LOAD segments"));
4132 in_load_segment
= true;
4137 if (!in_load_segment
)
4138 gold_error(_("allocated section not in any PT_LOAD segment"));
4142 // Set the addresses for segments created from a PHDRS clause. Return
4143 // the segment which should hold the file header and program headers,
4147 Script_sections::set_phdrs_clause_addresses(Layout
* layout
,
4148 uint64_t dot_alignment
)
4150 Output_segment
* load_seg
= NULL
;
4151 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
4152 p
!= this->phdrs_elements_
->end();
4155 // Note that we have to set the flags after adding the output
4156 // sections to the segment, as adding an output segment can
4157 // change the flags.
4158 (*p
)->set_flags_if_valid();
4160 Output_segment
* oseg
= (*p
)->segment();
4162 if (oseg
->type() != elfcpp::PT_LOAD
)
4164 // The addresses of non-PT_LOAD segments are set from the
4165 // PT_LOAD segments.
4166 if ((*p
)->has_load_address())
4167 gold_error(_("may only specify load address for PT_LOAD segment"));
4171 oseg
->set_minimum_p_align(dot_alignment
);
4173 // The output sections should have addresses from the SECTIONS
4174 // clause. The addresses don't have to be in order, so find the
4175 // one with the lowest load address. Use that to set the
4176 // address of the segment.
4178 Output_section
* osec
= oseg
->section_with_lowest_load_address();
4181 oseg
->set_addresses(0, 0);
4185 uint64_t vma
= osec
->address();
4186 uint64_t lma
= osec
->has_load_address() ? osec
->load_address() : vma
;
4188 // Override the load address of the section with the load
4189 // address specified for the segment.
4190 if ((*p
)->has_load_address())
4192 if (osec
->has_load_address())
4193 gold_warning(_("PHDRS load address overrides "
4194 "section %s load address"),
4197 lma
= (*p
)->load_address();
4200 bool headers
= (*p
)->includes_filehdr() && (*p
)->includes_phdrs();
4201 if (!headers
&& ((*p
)->includes_filehdr() || (*p
)->includes_phdrs()))
4203 // We could support this if we wanted to.
4204 gold_error(_("using only one of FILEHDR and PHDRS is "
4205 "not currently supported"));
4209 size_t sizeof_headers
= this->total_header_size(layout
);
4210 uint64_t subtract
= this->header_size_adjustment(lma
,
4212 if (lma
>= subtract
&& vma
>= subtract
)
4219 gold_error(_("sections loaded on first page without room "
4220 "for file and program headers "
4221 "are not supported"));
4224 if (load_seg
!= NULL
)
4225 gold_error(_("using FILEHDR and PHDRS on more than one "
4226 "PT_LOAD segment is not currently supported"));
4230 oseg
->set_addresses(vma
, lma
);
4236 // Add the file header and segment headers to non-load segments
4237 // specified in the PHDRS clause.
4240 Script_sections::put_headers_in_phdrs(Output_data
* file_header
,
4241 Output_data
* segment_headers
)
4243 gold_assert(this->saw_phdrs_clause());
4244 for (Phdrs_elements::iterator p
= this->phdrs_elements_
->begin();
4245 p
!= this->phdrs_elements_
->end();
4248 if ((*p
)->type() != elfcpp::PT_LOAD
)
4250 if ((*p
)->includes_phdrs())
4251 (*p
)->segment()->add_initial_output_data(segment_headers
);
4252 if ((*p
)->includes_filehdr())
4253 (*p
)->segment()->add_initial_output_data(file_header
);
4258 // Look for an output section by name and return the address, the load
4259 // address, the alignment, and the size. This is used when an
4260 // expression refers to an output section which was not actually
4261 // created. This returns true if the section was found, false
4265 Script_sections::get_output_section_info(const char* name
, uint64_t* address
,
4266 uint64_t* load_address
,
4267 uint64_t* addralign
,
4268 uint64_t* size
) const
4270 if (!this->saw_sections_clause_
)
4272 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
4273 p
!= this->sections_elements_
->end();
4275 if ((*p
)->get_output_section_info(name
, address
, load_address
, addralign
,
4281 // Release all Output_segments. This remove all pointers to all
4285 Script_sections::release_segments()
4287 if (this->saw_phdrs_clause())
4289 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
4290 p
!= this->phdrs_elements_
->end();
4292 (*p
)->release_segment();
4296 // Print the SECTIONS clause to F for debugging.
4299 Script_sections::print(FILE* f
) const
4301 if (this->phdrs_elements_
!= NULL
)
4303 fprintf(f
, "PHDRS {\n");
4304 for (Phdrs_elements::const_iterator p
= this->phdrs_elements_
->begin();
4305 p
!= this->phdrs_elements_
->end();
4311 if (this->memory_regions_
!= NULL
)
4313 fprintf(f
, "MEMORY {\n");
4314 for (Memory_regions::const_iterator m
= this->memory_regions_
->begin();
4315 m
!= this->memory_regions_
->end();
4321 if (!this->saw_sections_clause_
)
4324 fprintf(f
, "SECTIONS {\n");
4326 for (Sections_elements::const_iterator p
= this->sections_elements_
->begin();
4327 p
!= this->sections_elements_
->end();
4334 } // End namespace gold.