1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright (C) 2008-2019 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 set_address(uint64_t addr, const Symbol_table* symtab, const Layout* layout)
100 uint64_t start = this->start_->eval(symtab, layout, false);
101 uint64_t len = this->length_->eval(symtab, layout, false);
102 if (addr < start || addr >= start + len)
103 gold_error(_("address 0x%llx is not within region %s"),
104 static_cast<unsigned long long>(addr),
105 this->name_.c_str());
106 else if (addr < start + this->current_offset_)
107 gold_error(_("address 0x%llx moves dot backwards in region %s"),
108 static_cast<unsigned long long>(addr),
109 this->name_.c_str());
110 this->current_offset_ = addr - start;
114 increment_offset(std::string section_name, uint64_t amount,
115 const Symbol_table* symtab, const Layout* layout)
117 this->current_offset_ += amount;
119 if (this->current_offset_
120 > this->length_->eval(symtab, layout, false))
121 gold_error(_("section %s overflows end of region %s"),
122 section_name.c_str(), this->name_.c_str());
125 // Returns true iff there is room left in this region
126 // for AMOUNT more bytes of data.
128 has_room_for(const Symbol_table* symtab, const Layout* layout,
129 uint64_t amount) const
131 return (this->current_offset_ + amount
132 < this->length_->eval(symtab, layout, false));
135 // Return true if the provided section flags
136 // are compatible with this region's attributes.
138 attributes_compatible(elfcpp::Elf_Xword flags, elfcpp::Elf_Xword type) const;
141 add_section(Output_section_definition* sec, bool vma)
144 this->vma_sections_.push_back(sec);
146 this->lma_sections_.push_back(sec);
149 typedef std::vector<Output_section_definition*> Section_list;
151 // Return the start of the list of sections
152 // whose VMAs are taken from this region.
153 Section_list::const_iterator
154 get_vma_section_list_start() const
155 { return this->vma_sections_.begin(); }
157 // Return the start of the list of sections
158 // whose LMAs are taken from this region.
159 Section_list::const_iterator
160 get_lma_section_list_start() const
161 { return this->lma_sections_.begin(); }
163 // Return the end of the list of sections
164 // whose VMAs are taken from this region.
165 Section_list::const_iterator
166 get_vma_section_list_end() const
167 { return this->vma_sections_.end(); }
169 // Return the end of the list of sections
170 // whose LMAs are taken from this region.
171 Section_list::const_iterator
172 get_lma_section_list_end() const
173 { return this->lma_sections_.end(); }
175 Output_section_definition*
176 get_last_section() const
177 { return this->last_section_; }
180 set_last_section(Output_section_definition* sec)
181 { this->last_section_ = sec; }
186 unsigned int attributes_;
189 // The offset to the next free byte in the region.
190 // Note - for compatibility with GNU LD we only maintain one offset
191 // regardless of whether the region is being used for VMA values,
192 // LMA values, or both.
193 uint64_t current_offset_;
194 // A list of sections whose VMAs are set inside this region.
195 Section_list vma_sections_;
196 // A list of sections whose LMAs are set inside this region.
197 Section_list lma_sections_;
198 // The latest section to make use of this region.
199 Output_section_definition* last_section_;
202 // Return true if the provided section flags
203 // are compatible with this region's attributes.
206 Memory_region::attributes_compatible(elfcpp::Elf_Xword flags,
207 elfcpp::Elf_Xword type) const
209 unsigned int attrs = this->attributes_;
211 // No attributes means that this region is not compatible with anything.
218 switch (attrs & - attrs)
221 if ((flags & elfcpp::SHF_EXECINSTR) == 0)
226 if ((flags & elfcpp::SHF_WRITE) == 0)
231 // All sections are presumed readable.
234 case MEM_ALLOCATABLE:
235 if ((flags & elfcpp::SHF_ALLOC) == 0)
239 case MEM_INITIALIZED:
240 if ((type & elfcpp::SHT_NOBITS) != 0)
244 attrs &= ~ (attrs & - attrs);
251 // Print a memory region.
254 Memory_region::print(FILE* f) const
256 fprintf(f, " %s", this->name_.c_str());
258 unsigned int attrs = this->attributes_;
264 switch (attrs & - attrs)
266 case MEM_EXECUTABLE: fputc('x', f); break;
267 case MEM_WRITEABLE: fputc('w', f); break;
268 case MEM_READABLE: fputc('r', f); break;
269 case MEM_ALLOCATABLE: fputc('a', f); break;
270 case MEM_INITIALIZED: fputc('i', f); break;
274 attrs &= ~ (attrs & - attrs);
280 fprintf(f, " : origin = ");
281 this->start_->print(f);
282 fprintf(f, ", length = ");
283 this->length_->print(f);
287 // Manage orphan sections. This is intended to be largely compatible
288 // with the GNU linker. The Linux kernel implicitly relies on
289 // something similar to the GNU linker's orphan placement. We
290 // originally used a simpler scheme here, but it caused the kernel
291 // build to fail, and was also rather inefficient.
293 class Orphan_section_placement
296 typedef Script_sections::Elements_iterator Elements_iterator;
299 Orphan_section_placement();
301 // Handle an output section during initialization of this mapping.
303 output_section_init(const std::string& name, Output_section*,
304 Elements_iterator location);
306 // Initialize the last location.
308 last_init(Elements_iterator location);
310 // Set *PWHERE to the address of an iterator pointing to the
311 // location to use for an orphan section. Return true if the
312 // iterator has a value, false otherwise.
314 find_place(Output_section*, Elements_iterator** pwhere);
316 // Update PLACE_LAST_ALLOC.
318 update_last_alloc(Elements_iterator where);
320 // Return the iterator being used for sections at the very end of
321 // the linker script.
326 // The places that we specifically recognize. This list is copied
327 // from the GNU linker.
344 // The information we keep for a specific place.
347 // The name of sections for this place.
349 // Whether we have a location for this place.
351 // The iterator for this place.
352 Elements_iterator location;
355 // Initialize one place element.
357 initialize_place(Place_index, const char*);
360 Place places_[PLACE_MAX];
361 // True if this is the first call to output_section_init.
365 // Initialize Orphan_section_placement.
367 Orphan_section_placement::Orphan_section_placement()
370 this->initialize_place(PLACE_TEXT, ".text");
371 this->initialize_place(PLACE_RODATA, ".rodata");
372 this->initialize_place(PLACE_DATA, ".data");
373 this->initialize_place(PLACE_TLS, NULL);
374 this->initialize_place(PLACE_TLS_BSS, NULL);
375 this->initialize_place(PLACE_BSS, ".bss");
376 this->initialize_place(PLACE_LAST_ALLOC, NULL);
377 this->initialize_place(PLACE_REL, NULL);
378 this->initialize_place(PLACE_INTERP, ".interp");
379 this->initialize_place(PLACE_NONALLOC, NULL);
380 this->initialize_place(PLACE_LAST, NULL);
383 // Initialize one place element.
386 Orphan_section_placement::initialize_place(Place_index index, const char* name)
388 this->places_[index].name = name;
389 this->places_[index].have_location = false;
392 // While initializing the Orphan_section_placement information, this
393 // is called once for each output section named in the linker script.
394 // If we found an output section during the link, it will be passed in
398 Orphan_section_placement::output_section_init(const std::string& name,
400 Elements_iterator location)
402 bool first_init = this->first_init_;
403 this->first_init_ = false;
405 // Remember the last allocated section. Any orphan bss sections
406 // will be placed after it.
408 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
410 this->places_[PLACE_LAST_ALLOC].location = location;
411 this->places_[PLACE_LAST_ALLOC].have_location = true;
414 for (int i = 0; i < PLACE_MAX; ++i)
416 if (this->places_[i].name != NULL && this->places_[i].name == name)
418 if (this->places_[i].have_location)
420 // We have already seen a section with this name.
424 this->places_[i].location = location;
425 this->places_[i].have_location = true;
427 // If we just found the .bss section, restart the search for
428 // an unallocated section. This follows the GNU linker's
431 this->places_[PLACE_NONALLOC].have_location = false;
437 // Relocation sections.
438 if (!this->places_[PLACE_REL].have_location
440 && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
441 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
443 this->places_[PLACE_REL].location = location;
444 this->places_[PLACE_REL].have_location = true;
447 // We find the location for unallocated sections by finding the
448 // first debugging or comment section after the BSS section (if
450 if (!this->places_[PLACE_NONALLOC].have_location
451 && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
453 // We add orphan sections after the location in PLACES_. We
454 // want to store unallocated sections before LOCATION. If this
455 // is the very first section, we can't use it.
459 this->places_[PLACE_NONALLOC].location = location;
460 this->places_[PLACE_NONALLOC].have_location = true;
465 // Initialize the last location.
468 Orphan_section_placement::last_init(Elements_iterator location)
470 this->places_[PLACE_LAST].location = location;
471 this->places_[PLACE_LAST].have_location = true;
474 // Set *PWHERE to the address of an iterator pointing to the location
475 // to use for an orphan section. Return true if the iterator has a
476 // value, false otherwise.
479 Orphan_section_placement::find_place(Output_section* os,
480 Elements_iterator** pwhere)
482 // Figure out where OS should go. This is based on the GNU linker
483 // code. FIXME: The GNU linker handles small data sections
484 // specially, but we don't.
485 elfcpp::Elf_Word type = os->type();
486 elfcpp::Elf_Xword flags = os->flags();
488 if ((flags & elfcpp::SHF_ALLOC) == 0
489 && !Layout::is_debug_info_section(os->name()))
490 index = PLACE_NONALLOC;
491 else if ((flags & elfcpp::SHF_ALLOC) == 0)
493 else if (type == elfcpp::SHT_NOTE)
494 index = PLACE_INTERP;
495 else if ((flags & elfcpp::SHF_TLS) != 0)
497 if (type == elfcpp::SHT_NOBITS)
498 index = PLACE_TLS_BSS;
502 else if (type == elfcpp::SHT_NOBITS)
504 else if ((flags & elfcpp::SHF_WRITE) != 0)
506 else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
508 else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
509 index = PLACE_RODATA;
513 // If we don't have a location yet, try to find one based on a
514 // plausible ordering of sections.
515 if (!this->places_[index].have_location)
527 follow = PLACE_RODATA;
528 if (!this->places_[PLACE_RODATA].have_location)
532 follow = PLACE_LAST_ALLOC;
545 if (!this->places_[PLACE_TLS].have_location)
549 if (follow != PLACE_MAX && this->places_[follow].have_location)
551 // Set the location of INDEX to the location of FOLLOW. The
552 // location of INDEX will then be incremented by the caller,
553 // so anything in INDEX will continue to be after anything
555 this->places_[index].location = this->places_[follow].location;
556 this->places_[index].have_location = true;
560 *pwhere = &this->places_[index].location;
561 bool ret = this->places_[index].have_location;
563 // The caller will set the location.
564 this->places_[index].have_location = true;
569 // Update PLACE_LAST_ALLOC.
571 Orphan_section_placement::update_last_alloc(Elements_iterator elem)
573 Elements_iterator prev = elem;
575 if (this->places_[PLACE_LAST_ALLOC].have_location
576 && this->places_[PLACE_LAST_ALLOC].location == prev)
578 this->places_[PLACE_LAST_ALLOC].have_location = true;
579 this->places_[PLACE_LAST_ALLOC].location = elem;
583 // Return the iterator being used for sections at the very end of the
586 Orphan_section_placement::Elements_iterator
587 Orphan_section_placement::last_place() const
589 gold_assert(this->places_[PLACE_LAST].have_location);
590 return this->places_[PLACE_LAST].location;
593 // An element in a SECTIONS clause.
595 class Sections_element
601 virtual ~Sections_element()
604 // Return whether an output section is relro.
609 // Record that an output section is relro.
614 // Create any required output sections. The only real
615 // implementation is in Output_section_definition.
617 create_sections(Layout*)
620 // Add any symbol being defined to the symbol table.
622 add_symbols_to_table(Symbol_table*)
625 // Finalize symbols and check assertions.
627 finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
630 // Return the output section name to use for an input file name and
631 // section name. This only real implementation is in
632 // Output_section_definition.
634 output_section_name(const char*, const char*, Output_section***,
635 Script_sections::Section_type*, bool*, bool)
638 // Initialize OSP with an output section.
640 orphan_section_init(Orphan_section_placement*,
641 Script_sections::Elements_iterator)
644 // Set section addresses. This includes applying assignments if the
645 // expression is an absolute value.
647 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
651 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
652 // this section is constrained, and the input sections do not match,
653 // return the constraint, and set *POSD.
654 virtual Section_constraint
655 check_constraint(Output_section_definition**)
656 { return CONSTRAINT_NONE; }
658 // See if this is the alternate output section for a constrained
659 // output section. If it is, transfer the Output_section and return
660 // true. Otherwise return false.
662 alternate_constraint(Output_section_definition*, Section_constraint)
665 // Get the list of segments to use for an allocated section when
666 // using a PHDRS clause. If this is an allocated section, return
667 // the Output_section, and set *PHDRS_LIST (the first parameter) to
668 // the list of PHDRS to which it should be attached. If the PHDRS
669 // were not specified, don't change *PHDRS_LIST. When not returning
670 // NULL, set *ORPHAN (the second parameter) according to whether
671 // this is an orphan section--one that is not mentioned in the
673 virtual Output_section*
674 allocate_to_segment(String_list**, bool*)
677 // Look for an output section by name and return the address, the
678 // load address, the alignment, and the size. This is used when an
679 // expression refers to an output section which was not actually
680 // created. This returns true if the section was found, false
681 // otherwise. The only real definition is for
682 // Output_section_definition.
684 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
688 // Return the associated Output_section if there is one.
689 virtual Output_section*
690 get_output_section() const
693 // Set the section's memory regions.
695 set_memory_region(Memory_region*, bool)
696 { gold_error(_("Attempt to set a memory region for a non-output section")); }
698 // Print the element for debugging purposes.
700 print(FILE* f) const = 0;
703 // An assignment in a SECTIONS clause outside of an output section.
705 class Sections_element_assignment : public Sections_element
708 Sections_element_assignment(const char* name, size_t namelen,
709 Expression* val, bool provide, bool hidden)
710 : assignment_(name, namelen, false, val, provide, hidden)
713 // Add the symbol to the symbol table.
715 add_symbols_to_table(Symbol_table* symtab)
716 { this->assignment_.add_to_table(symtab); }
718 // Finalize the symbol.
720 finalize_symbols(Symbol_table* symtab, const Layout* layout,
723 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
726 // Set the section address. There is no section here, but if the
727 // value is absolute, we set the symbol. This permits us to use
728 // absolute symbols when setting dot.
730 set_section_addresses(Symbol_table* symtab, Layout* layout,
731 uint64_t* dot_value, uint64_t*, uint64_t*)
733 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value, NULL);
736 // Print for debugging.
741 this->assignment_.print(f);
745 Symbol_assignment assignment_;
748 // An assignment to the dot symbol in a SECTIONS clause outside of an
751 class Sections_element_dot_assignment : public Sections_element
754 Sections_element_dot_assignment(Expression* val)
758 // Finalize the symbol.
760 finalize_symbols(Symbol_table* symtab, const Layout* layout,
763 // We ignore the section of the result because outside of an
764 // output section definition the dot symbol is always considered
766 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
767 NULL, NULL, NULL, false);
770 // Update the dot symbol while setting section addresses.
772 set_section_addresses(Symbol_table* symtab, Layout* layout,
773 uint64_t* dot_value, uint64_t* dot_alignment,
774 uint64_t* load_address)
776 *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
777 NULL, NULL, dot_alignment, false);
778 *load_address = *dot_value;
781 // Print for debugging.
786 this->val_->print(f);
794 // An assertion in a SECTIONS clause outside of an output section.
796 class Sections_element_assertion : public Sections_element
799 Sections_element_assertion(Expression* check, const char* message,
801 : assertion_(check, message, messagelen)
804 // Check the assertion.
806 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
807 { this->assertion_.check(symtab, layout); }
809 // Print for debugging.
814 this->assertion_.print(f);
818 Script_assertion assertion_;
821 // An element in an output section in a SECTIONS clause.
823 class Output_section_element
826 // A list of input sections.
827 typedef std::list<Output_section::Input_section> Input_section_list;
829 Output_section_element()
832 virtual ~Output_section_element()
835 // Return whether this element requires an output section to exist.
837 needs_output_section() const
840 // Add any symbol being defined to the symbol table.
842 add_symbols_to_table(Symbol_table*)
845 // Finalize symbols and check assertions.
847 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
850 // Return whether this element matches FILE_NAME and SECTION_NAME.
851 // The only real implementation is in Output_section_element_input.
853 match_name(const char*, const char*, bool *) const
856 // Set section addresses. This includes applying assignments if the
857 // expression is an absolute value.
859 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
860 uint64_t*, uint64_t*, Output_section**, std::string*,
864 // Print the element for debugging purposes.
866 print(FILE* f) const = 0;
869 // Return a fill string that is LENGTH bytes long, filling it with
872 get_fill_string(const std::string* fill, section_size_type length) const;
876 Output_section_element::get_fill_string(const std::string* fill,
877 section_size_type length) const
879 std::string this_fill;
880 this_fill.reserve(length);
881 while (this_fill.length() + fill->length() <= length)
883 if (this_fill.length() < length)
884 this_fill.append(*fill, 0, length - this_fill.length());
888 // A symbol assignment in an output section.
890 class Output_section_element_assignment : public Output_section_element
893 Output_section_element_assignment(const char* name, size_t namelen,
894 Expression* val, bool provide,
896 : assignment_(name, namelen, false, val, provide, hidden)
899 // Add the symbol to the symbol table.
901 add_symbols_to_table(Symbol_table* symtab)
902 { this->assignment_.add_to_table(symtab); }
904 // Finalize the symbol.
906 finalize_symbols(Symbol_table* symtab, const Layout* layout,
907 uint64_t* dot_value, Output_section** dot_section)
909 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
913 // Set the section address. There is no section here, but if the
914 // value is absolute, we set the symbol. This permits us to use
915 // absolute symbols when setting dot.
917 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
918 uint64_t, uint64_t* dot_value, uint64_t*,
919 Output_section** dot_section, std::string*,
922 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value,
926 // Print for debugging.
931 this->assignment_.print(f);
935 Symbol_assignment assignment_;
938 // An assignment to the dot symbol in an output section.
940 class Output_section_element_dot_assignment : public Output_section_element
943 Output_section_element_dot_assignment(Expression* val)
947 // An assignment to dot within an output section is enough to force
948 // the output section to exist.
950 needs_output_section() const
953 // Finalize the symbol.
955 finalize_symbols(Symbol_table* symtab, const Layout* layout,
956 uint64_t* dot_value, Output_section** dot_section)
958 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
959 *dot_section, dot_section, NULL,
963 // Update the dot symbol while setting section addresses.
965 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
966 uint64_t, uint64_t* dot_value, uint64_t*,
967 Output_section** dot_section, std::string*,
968 Input_section_list*);
970 // Print for debugging.
975 this->val_->print(f);
983 // Update the dot symbol while setting section addresses.
986 Output_section_element_dot_assignment::set_section_addresses(
987 Symbol_table* symtab,
989 Output_section* output_section,
992 uint64_t* dot_alignment,
993 Output_section** dot_section,
997 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
998 *dot_value, *dot_section,
999 dot_section, dot_alignment,
1001 if (next_dot < *dot_value)
1002 gold_error(_("dot may not move backward"));
1003 if (next_dot > *dot_value && output_section != NULL)
1005 section_size_type length = convert_to_section_size_type(next_dot
1007 Output_section_data* posd;
1009 posd = new Output_data_zero_fill(length, 0);
1012 std::string this_fill = this->get_fill_string(fill, length);
1013 posd = new Output_data_const(this_fill, 0);
1015 output_section->add_output_section_data(posd);
1016 layout->new_output_section_data_from_script(posd);
1018 *dot_value = next_dot;
1021 // An assertion in an output section.
1023 class Output_section_element_assertion : public Output_section_element
1026 Output_section_element_assertion(Expression* check, const char* message,
1028 : assertion_(check, message, messagelen)
1032 print(FILE* f) const
1035 this->assertion_.print(f);
1039 Script_assertion assertion_;
1042 // We use a special instance of Output_section_data to handle BYTE,
1043 // SHORT, etc. This permits forward references to symbols in the
1046 class Output_data_expression : public Output_section_data
1049 Output_data_expression(int size, bool is_signed, Expression* val,
1050 const Symbol_table* symtab, const Layout* layout,
1051 uint64_t dot_value, Output_section* dot_section)
1052 : Output_section_data(size, 0, true),
1053 is_signed_(is_signed), val_(val), symtab_(symtab),
1054 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
1058 // Write the data to the output file.
1060 do_write(Output_file*);
1062 // Write the data to a buffer.
1064 do_write_to_buffer(unsigned char*);
1066 // Write to a map file.
1068 do_print_to_mapfile(Mapfile* mapfile) const
1069 { mapfile->print_output_data(this, _("** expression")); }
1072 template<bool big_endian>
1074 endian_write_to_buffer(uint64_t, unsigned char*);
1078 const Symbol_table* symtab_;
1079 const Layout* layout_;
1080 uint64_t dot_value_;
1081 Output_section* dot_section_;
1084 // Write the data element to the output file.
1087 Output_data_expression::do_write(Output_file* of)
1089 unsigned char* view = of->get_output_view(this->offset(), this->data_size());
1090 this->write_to_buffer(view);
1091 of->write_output_view(this->offset(), this->data_size(), view);
1094 // Write the data element to a buffer.
1097 Output_data_expression::do_write_to_buffer(unsigned char* buf)
1099 uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
1100 true, this->dot_value_,
1101 this->dot_section_, NULL, NULL,
1104 if (parameters->target().is_big_endian())
1105 this->endian_write_to_buffer<true>(val, buf);
1107 this->endian_write_to_buffer<false>(val, buf);
1110 template<bool big_endian>
1112 Output_data_expression::endian_write_to_buffer(uint64_t val,
1115 switch (this->data_size())
1118 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
1121 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
1124 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
1127 if (parameters->target().get_size() == 32)
1130 if (this->is_signed_ && (val & 0x80000000) != 0)
1131 val |= 0xffffffff00000000LL;
1133 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
1140 // A data item in an output section.
1142 class Output_section_element_data : public Output_section_element
1145 Output_section_element_data(int size, bool is_signed, Expression* val)
1146 : size_(size), is_signed_(is_signed), val_(val)
1149 // If there is a data item, then we must create an output section.
1151 needs_output_section() const
1154 // Finalize symbols--we just need to update dot.
1156 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1158 { *dot_value += this->size_; }
1160 // Store the value in the section.
1162 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
1163 uint64_t* dot_value, uint64_t*, Output_section**,
1164 std::string*, Input_section_list*);
1166 // Print for debugging.
1171 // The size in bytes.
1173 // Whether the value is signed.
1179 // Store the value in the section.
1182 Output_section_element_data::set_section_addresses(
1183 Symbol_table* symtab,
1187 uint64_t* dot_value,
1189 Output_section** dot_section,
1191 Input_section_list*)
1193 gold_assert(os != NULL);
1194 Output_data_expression* expression =
1195 new Output_data_expression(this->size_, this->is_signed_, this->val_,
1196 symtab, layout, *dot_value, *dot_section);
1197 os->add_output_section_data(expression);
1198 layout->new_output_section_data_from_script(expression);
1199 *dot_value += this->size_;
1202 // Print for debugging.
1205 Output_section_element_data::print(FILE* f) const
1208 switch (this->size_)
1220 if (this->is_signed_)
1228 fprintf(f, " %s(", s);
1229 this->val_->print(f);
1233 // A fill value setting in an output section.
1235 class Output_section_element_fill : public Output_section_element
1238 Output_section_element_fill(Expression* val)
1242 // Update the fill value while setting section addresses.
1244 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1245 uint64_t, uint64_t* dot_value, uint64_t*,
1246 Output_section** dot_section,
1247 std::string* fill, Input_section_list*)
1249 Output_section* fill_section;
1250 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
1251 *dot_value, *dot_section,
1252 &fill_section, NULL, false);
1253 if (fill_section != NULL)
1254 gold_warning(_("fill value is not absolute"));
1255 // FIXME: The GNU linker supports fill values of arbitrary length.
1256 unsigned char fill_buff[4];
1257 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1258 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
1261 // Print for debugging.
1263 print(FILE* f) const
1265 fprintf(f, " FILL(");
1266 this->val_->print(f);
1271 // The new fill value.
1275 // An input section specification in an output section
1277 class Output_section_element_input : public Output_section_element
1280 Output_section_element_input(const Input_section_spec* spec, bool keep);
1282 // Finalize symbols--just update the value of the dot symbol.
1284 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1285 Output_section** dot_section)
1287 *dot_value = this->final_dot_value_;
1288 *dot_section = this->final_dot_section_;
1291 // See whether we match FILE_NAME and SECTION_NAME as an input section.
1292 // If we do then also indicate whether the section should be KEPT.
1294 match_name(const char* file_name, const char* section_name, bool* keep) const;
1296 // Set the section address.
1298 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1299 uint64_t subalign, uint64_t* dot_value, uint64_t*,
1300 Output_section**, std::string* fill,
1301 Input_section_list*);
1303 // Print for debugging.
1305 print(FILE* f) const;
1308 // An input section pattern.
1309 struct Input_section_pattern
1311 std::string pattern;
1312 bool pattern_is_wildcard;
1315 Input_section_pattern(const char* patterna, size_t patternlena,
1316 Sort_wildcard sorta)
1317 : pattern(patterna, patternlena),
1318 pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
1323 typedef std::vector<Input_section_pattern> Input_section_patterns;
1325 // Filename_exclusions is a pair of filename pattern and a bool
1326 // indicating whether the filename is a wildcard.
1327 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1329 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1330 // indicates whether this is a wildcard pattern.
1332 match(const char* string, const char* pattern, bool is_wildcard_pattern)
1334 return (is_wildcard_pattern
1335 ? fnmatch(pattern, string, 0) == 0
1336 : strcmp(string, pattern) == 0);
1339 // See if we match a file name.
1341 match_file_name(const char* file_name) const;
1343 // The file name pattern. If this is the empty string, we match all
1345 std::string filename_pattern_;
1346 // Whether the file name pattern is a wildcard.
1347 bool filename_is_wildcard_;
1348 // How the file names should be sorted. This may only be
1349 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1350 Sort_wildcard filename_sort_;
1351 // The list of file names to exclude.
1352 Filename_exclusions filename_exclusions_;
1353 // The list of input section patterns.
1354 Input_section_patterns input_section_patterns_;
1355 // Whether to keep this section when garbage collecting.
1357 // The value of dot after including all matching sections.
1358 uint64_t final_dot_value_;
1359 // The section where dot is defined after including all matching
1361 Output_section* final_dot_section_;
1364 // Construct Output_section_element_input. The parser records strings
1365 // as pointers into a copy of the script file, which will go away when
1366 // parsing is complete. We make sure they are in std::string objects.
1368 Output_section_element_input::Output_section_element_input(
1369 const Input_section_spec* spec,
1371 : filename_pattern_(),
1372 filename_is_wildcard_(false),
1373 filename_sort_(spec->file.sort),
1374 filename_exclusions_(),
1375 input_section_patterns_(),
1377 final_dot_value_(0),
1378 final_dot_section_(NULL)
1380 // The filename pattern "*" is common, and matches all files. Turn
1381 // it into the empty string.
1382 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1383 this->filename_pattern_.assign(spec->file.name.value,
1384 spec->file.name.length);
1385 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
1387 if (spec->input_sections.exclude != NULL)
1389 for (String_list::const_iterator p =
1390 spec->input_sections.exclude->begin();
1391 p != spec->input_sections.exclude->end();
1394 bool is_wildcard = is_wildcard_string((*p).c_str());
1395 this->filename_exclusions_.push_back(std::make_pair(*p,
1400 if (spec->input_sections.sections != NULL)
1402 Input_section_patterns& isp(this->input_section_patterns_);
1403 for (String_sort_list::const_iterator p =
1404 spec->input_sections.sections->begin();
1405 p != spec->input_sections.sections->end();
1407 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1412 // See whether we match FILE_NAME.
1415 Output_section_element_input::match_file_name(const char* file_name) const
1417 if (!this->filename_pattern_.empty())
1419 // If we were called with no filename, we refuse to match a
1420 // pattern which requires a file name.
1421 if (file_name == NULL)
1424 if (!match(file_name, this->filename_pattern_.c_str(),
1425 this->filename_is_wildcard_))
1429 if (file_name != NULL)
1431 // Now we have to see whether FILE_NAME matches one of the
1432 // exclusion patterns, if any.
1433 for (Filename_exclusions::const_iterator p =
1434 this->filename_exclusions_.begin();
1435 p != this->filename_exclusions_.end();
1438 if (match(file_name, p->first.c_str(), p->second))
1446 // See whether we match FILE_NAME and SECTION_NAME. If we do then
1447 // KEEP indicates whether the section should survive garbage collection.
1450 Output_section_element_input::match_name(const char* file_name,
1451 const char* section_name,
1454 if (!this->match_file_name(file_name))
1457 *keep = this->keep_;
1459 // If there are no section name patterns, then we match.
1460 if (this->input_section_patterns_.empty())
1463 // See whether we match the section name patterns.
1464 for (Input_section_patterns::const_iterator p =
1465 this->input_section_patterns_.begin();
1466 p != this->input_section_patterns_.end();
1469 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1473 // We didn't match any section names, so we didn't match.
1477 // Information we use to sort the input sections.
1479 class Input_section_info
1482 Input_section_info(const Output_section::Input_section& input_section)
1483 : input_section_(input_section), section_name_(),
1484 size_(0), addralign_(1)
1487 // Return the simple input section.
1488 const Output_section::Input_section&
1489 input_section() const
1490 { return this->input_section_; }
1492 // Return the object.
1495 { return this->input_section_.relobj(); }
1497 // Return the section index.
1500 { return this->input_section_.shndx(); }
1502 // Return the section name.
1504 section_name() const
1505 { return this->section_name_; }
1507 // Set the section name.
1509 set_section_name(const std::string name)
1511 if (is_compressed_debug_section(name.c_str()))
1512 this->section_name_ = corresponding_uncompressed_section_name(name);
1514 this->section_name_ = name;
1517 // Return the section size.
1520 { return this->size_; }
1522 // Set the section size.
1524 set_size(uint64_t size)
1525 { this->size_ = size; }
1527 // Return the address alignment.
1530 { return this->addralign_; }
1532 // Set the address alignment.
1534 set_addralign(uint64_t addralign)
1535 { this->addralign_ = addralign; }
1538 // Input section, can be a relaxed section.
1539 Output_section::Input_section input_section_;
1540 // Name of the section.
1541 std::string section_name_;
1544 // Address alignment.
1545 uint64_t addralign_;
1548 // A class to sort the input sections.
1550 class Input_section_sorter
1553 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1554 : filename_sort_(filename_sort), section_sort_(section_sort)
1558 operator()(const Input_section_info&, const Input_section_info&) const;
1561 static unsigned long
1562 get_init_priority(const char*);
1564 Sort_wildcard filename_sort_;
1565 Sort_wildcard section_sort_;
1568 // Return a relative priority of the section with the specified NAME
1569 // (a lower value meand a higher priority), or 0 if it should be compared
1570 // with others as strings.
1571 // The implementation of this function is copied from ld/ldlang.c.
1574 Input_section_sorter::get_init_priority(const char* name)
1577 unsigned long init_priority;
1579 // GCC uses the following section names for the init_priority
1580 // attribute with numerical values 101 and 65535 inclusive. A
1581 // lower value means a higher priority.
1583 // 1: .init_array.NNNN/.fini_array.NNNN: Where NNNN is the
1584 // decimal numerical value of the init_priority attribute.
1585 // The order of execution in .init_array is forward and
1586 // .fini_array is backward.
1587 // 2: .ctors.NNNN/.dtors.NNNN: Where NNNN is 65535 minus the
1588 // decimal numerical value of the init_priority attribute.
1589 // The order of execution in .ctors is backward and .dtors
1592 if (strncmp(name, ".init_array.", 12) == 0
1593 || strncmp(name, ".fini_array.", 12) == 0)
1595 init_priority = strtoul(name + 12, &end, 10);
1596 return *end ? 0 : init_priority;
1598 else if (strncmp(name, ".ctors.", 7) == 0
1599 || strncmp(name, ".dtors.", 7) == 0)
1601 init_priority = strtoul(name + 7, &end, 10);
1602 return *end ? 0 : 65535 - init_priority;
1609 Input_section_sorter::operator()(const Input_section_info& isi1,
1610 const Input_section_info& isi2) const
1612 if (this->section_sort_ == SORT_WILDCARD_BY_INIT_PRIORITY)
1614 unsigned long ip1 = get_init_priority(isi1.section_name().c_str());
1615 unsigned long ip2 = get_init_priority(isi2.section_name().c_str());
1616 if (ip1 != 0 && ip2 != 0 && ip1 != ip2)
1619 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1620 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1621 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1622 && isi1.addralign() == isi2.addralign())
1623 || this->section_sort_ == SORT_WILDCARD_BY_INIT_PRIORITY)
1625 if (isi1.section_name() != isi2.section_name())
1626 return isi1.section_name() < isi2.section_name();
1628 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1629 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1630 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1632 if (isi1.addralign() != isi2.addralign())
1633 return isi1.addralign() < isi2.addralign();
1635 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1637 if (isi1.relobj()->name() != isi2.relobj()->name())
1638 return (isi1.relobj()->name() < isi2.relobj()->name());
1641 // Otherwise we leave them in the same order.
1645 // Set the section address. Look in INPUT_SECTIONS for sections which
1646 // match this spec, sort them as specified, and add them to the output
1650 Output_section_element_input::set_section_addresses(
1653 Output_section* output_section,
1655 uint64_t* dot_value,
1657 Output_section** dot_section,
1659 Input_section_list* input_sections)
1661 // We build a list of sections which match each
1662 // Input_section_pattern.
1664 // If none of the patterns specify a sort option, we throw all
1665 // matching input sections into a single bin, in the order we
1666 // find them. Otherwise, we put matching input sections into
1667 // a separate bin for each pattern, and sort each one as
1668 // specified. Thus, an input section spec like this:
1670 // will group all .foo and .bar sections in the order seen,
1673 // will group all .foo sections followed by all .bar sections.
1674 // This matches Gnu ld behavior.
1676 // Things get really weird, though, when you add a sort spec
1677 // on some, but not all, of the patterns, like this:
1678 // *(SORT_BY_NAME(.foo) .bar)
1679 // We do not attempt to match Gnu ld behavior in this case.
1681 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1682 size_t input_pattern_count = this->input_section_patterns_.size();
1683 size_t bin_count = 1;
1684 bool any_patterns_with_sort = false;
1685 for (size_t i = 0; i < input_pattern_count; ++i)
1687 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1688 if (isp.sort != SORT_WILDCARD_NONE)
1689 any_patterns_with_sort = true;
1691 if (any_patterns_with_sort)
1692 bin_count = input_pattern_count;
1693 Matching_sections matching_sections(bin_count);
1695 // Look through the list of sections for this output section. Add
1696 // each one which matches to one of the elements of
1697 // MATCHING_SECTIONS.
1699 Input_section_list::iterator p = input_sections->begin();
1700 while (p != input_sections->end())
1702 Relobj* relobj = p->relobj();
1703 unsigned int shndx = p->shndx();
1704 Input_section_info isi(*p);
1706 // Calling section_name and section_addralign is not very
1709 // Lock the object so that we can get information about the
1710 // section. This is OK since we know we are single-threaded
1713 const Task* task = reinterpret_cast<const Task*>(-1);
1714 Task_lock_obj<Object> tl(task, relobj);
1716 isi.set_section_name(relobj->section_name(shndx));
1717 if (p->is_relaxed_input_section())
1719 // We use current data size because relaxed section sizes may not
1720 // have finalized yet.
1721 isi.set_size(p->relaxed_input_section()->current_data_size());
1722 isi.set_addralign(p->relaxed_input_section()->addralign());
1726 isi.set_size(relobj->section_size(shndx));
1727 isi.set_addralign(relobj->section_addralign(shndx));
1731 if (!this->match_file_name(relobj->name().c_str()))
1733 else if (this->input_section_patterns_.empty())
1735 matching_sections[0].push_back(isi);
1736 p = input_sections->erase(p);
1741 for (i = 0; i < input_pattern_count; ++i)
1743 const Input_section_pattern&
1744 isp(this->input_section_patterns_[i]);
1745 if (match(isi.section_name().c_str(), isp.pattern.c_str(),
1746 isp.pattern_is_wildcard))
1750 if (i >= input_pattern_count)
1756 matching_sections[i].push_back(isi);
1757 p = input_sections->erase(p);
1762 // Look through MATCHING_SECTIONS. Sort each one as specified,
1763 // using a stable sort so that we get the default order when
1764 // sections are otherwise equal. Add each input section to the
1767 uint64_t dot = *dot_value;
1768 for (size_t i = 0; i < bin_count; ++i)
1770 if (matching_sections[i].empty())
1773 gold_assert(output_section != NULL);
1775 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1776 if (isp.sort != SORT_WILDCARD_NONE
1777 || this->filename_sort_ != SORT_WILDCARD_NONE)
1778 std::stable_sort(matching_sections[i].begin(),
1779 matching_sections[i].end(),
1780 Input_section_sorter(this->filename_sort_,
1783 for (std::vector<Input_section_info>::const_iterator p =
1784 matching_sections[i].begin();
1785 p != matching_sections[i].end();
1788 // Override the original address alignment if SUBALIGN is specified.
1789 // We need to make a copy of the input section to modify the
1791 Output_section::Input_section sis(p->input_section());
1793 uint64_t this_subalign = sis.addralign();
1794 if (!sis.is_input_section())
1795 sis.output_section_data()->finalize_data_size();
1796 uint64_t data_size = sis.data_size();
1799 this_subalign = subalign;
1800 sis.set_addralign(subalign);
1803 uint64_t address = align_address(dot, this_subalign);
1805 if (address > dot && !fill->empty())
1807 section_size_type length =
1808 convert_to_section_size_type(address - dot);
1809 std::string this_fill = this->get_fill_string(fill, length);
1810 Output_section_data* posd = new Output_data_const(this_fill, 0);
1811 output_section->add_output_section_data(posd);
1812 layout->new_output_section_data_from_script(posd);
1815 output_section->add_script_input_section(sis);
1816 dot = address + data_size;
1820 // An SHF_TLS/SHT_NOBITS section does not take up any
1822 if (output_section == NULL
1823 || (output_section->flags() & elfcpp::SHF_TLS) == 0
1824 || output_section->type() != elfcpp::SHT_NOBITS)
1827 this->final_dot_value_ = *dot_value;
1828 this->final_dot_section_ = *dot_section;
1831 // Print for debugging.
1834 Output_section_element_input::print(FILE* f) const
1839 fprintf(f, "KEEP(");
1841 if (!this->filename_pattern_.empty())
1843 bool need_close_paren = false;
1844 switch (this->filename_sort_)
1846 case SORT_WILDCARD_NONE:
1848 case SORT_WILDCARD_BY_NAME:
1849 fprintf(f, "SORT_BY_NAME(");
1850 need_close_paren = true;
1856 fprintf(f, "%s", this->filename_pattern_.c_str());
1858 if (need_close_paren)
1862 if (!this->input_section_patterns_.empty()
1863 || !this->filename_exclusions_.empty())
1867 bool need_space = false;
1868 if (!this->filename_exclusions_.empty())
1870 fprintf(f, "EXCLUDE_FILE(");
1871 bool need_comma = false;
1872 for (Filename_exclusions::const_iterator p =
1873 this->filename_exclusions_.begin();
1874 p != this->filename_exclusions_.end();
1879 fprintf(f, "%s", p->first.c_str());
1886 for (Input_section_patterns::const_iterator p =
1887 this->input_section_patterns_.begin();
1888 p != this->input_section_patterns_.end();
1894 int close_parens = 0;
1897 case SORT_WILDCARD_NONE:
1899 case SORT_WILDCARD_BY_NAME:
1900 fprintf(f, "SORT_BY_NAME(");
1903 case SORT_WILDCARD_BY_ALIGNMENT:
1904 fprintf(f, "SORT_BY_ALIGNMENT(");
1907 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1908 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1911 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1912 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1915 case SORT_WILDCARD_BY_INIT_PRIORITY:
1916 fprintf(f, "SORT_BY_INIT_PRIORITY(");
1923 fprintf(f, "%s", p->pattern.c_str());
1925 for (int i = 0; i < close_parens; ++i)
1940 // An output section.
1942 class Output_section_definition : public Sections_element
1945 typedef Output_section_element::Input_section_list Input_section_list;
1947 Output_section_definition(const char* name, size_t namelen,
1948 const Parser_output_section_header* header);
1950 // Finish the output section with the information in the trailer.
1952 finish(const Parser_output_section_trailer* trailer);
1954 // Add a symbol to be defined.
1956 add_symbol_assignment(const char* name, size_t length, Expression* value,
1957 bool provide, bool hidden);
1959 // Add an assignment to the special dot symbol.
1961 add_dot_assignment(Expression* value);
1963 // Add an assertion.
1965 add_assertion(Expression* check, const char* message, size_t messagelen);
1967 // Add a data item to the current output section.
1969 add_data(int size, bool is_signed, Expression* val);
1971 // Add a setting for the fill value.
1973 add_fill(Expression* val);
1975 // Add an input section specification.
1977 add_input_section(const Input_section_spec* spec, bool keep);
1979 // Return whether the output section is relro.
1982 { return this->is_relro_; }
1984 // Record that the output section is relro.
1987 { this->is_relro_ = true; }
1989 // Create any required output sections.
1991 create_sections(Layout*);
1993 // Add any symbols being defined to the symbol table.
1995 add_symbols_to_table(Symbol_table* symtab);
1997 // Finalize symbols and check assertions.
1999 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
2001 // Return the output section name to use for an input file name and
2004 output_section_name(const char* file_name, const char* section_name,
2005 Output_section***, Script_sections::Section_type*,
2008 // Initialize OSP with an output section.
2010 orphan_section_init(Orphan_section_placement* osp,
2011 Script_sections::Elements_iterator p)
2012 { osp->output_section_init(this->name_, this->output_section_, p); }
2014 // Set the section address.
2016 set_section_addresses(Symbol_table* symtab, Layout* layout,
2017 uint64_t* dot_value, uint64_t*,
2018 uint64_t* load_address);
2020 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2021 // this section is constrained, and the input sections do not match,
2022 // return the constraint, and set *POSD.
2024 check_constraint(Output_section_definition** posd);
2026 // See if this is the alternate output section for a constrained
2027 // output section. If it is, transfer the Output_section and return
2028 // true. Otherwise return false.
2030 alternate_constraint(Output_section_definition*, Section_constraint);
2032 // Get the list of segments to use for an allocated section when
2033 // using a PHDRS clause.
2035 allocate_to_segment(String_list** phdrs_list, bool* orphan);
2037 // Look for an output section by name and return the address, the
2038 // load address, the alignment, and the size. This is used when an
2039 // expression refers to an output section which was not actually
2040 // created. This returns true if the section was found, false
2043 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
2046 // Return the associated Output_section if there is one.
2048 get_output_section() const
2049 { return this->output_section_; }
2051 // Print the contents to the FILE. This is for debugging.
2055 // Return the output section type if specified or Script_sections::ST_NONE.
2056 Script_sections::Section_type
2057 section_type() const;
2059 // Store the memory region to use.
2061 set_memory_region(Memory_region*, bool set_vma);
2064 set_section_vma(Expression* address)
2065 { this->address_ = address; }
2068 set_section_lma(Expression* address)
2069 { this->load_address_ = address; }
2072 get_section_name() const
2073 { return this->name_; }
2077 script_section_type_name(Script_section_type);
2079 typedef std::vector<Output_section_element*> Output_section_elements;
2081 // The output section name.
2083 // The address. This may be NULL.
2084 Expression* address_;
2085 // The load address. This may be NULL.
2086 Expression* load_address_;
2087 // The alignment. This may be NULL.
2089 // The input section alignment. This may be NULL.
2090 Expression* subalign_;
2091 // The constraint, if any.
2092 Section_constraint constraint_;
2093 // The fill value. This may be NULL.
2095 // The list of segments this section should go into. This may be
2097 String_list* phdrs_;
2098 // The list of elements defining the section.
2099 Output_section_elements elements_;
2100 // The Output_section created for this definition. This will be
2101 // NULL if none was created.
2102 Output_section* output_section_;
2103 // The address after it has been evaluated.
2104 uint64_t evaluated_address_;
2105 // The load address after it has been evaluated.
2106 uint64_t evaluated_load_address_;
2107 // The alignment after it has been evaluated.
2108 uint64_t evaluated_addralign_;
2109 // The output section is relro.
2111 // The output section type if specified.
2112 enum Script_section_type script_section_type_;
2117 Output_section_definition::Output_section_definition(
2120 const Parser_output_section_header* header)
2121 : name_(name, namelen),
2122 address_(header->address),
2123 load_address_(header->load_address),
2124 align_(header->align),
2125 subalign_(header->subalign),
2126 constraint_(header->constraint),
2130 output_section_(NULL),
2131 evaluated_address_(0),
2132 evaluated_load_address_(0),
2133 evaluated_addralign_(0),
2135 script_section_type_(header->section_type)
2139 // Finish an output section.
2142 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
2144 this->fill_ = trailer->fill;
2145 this->phdrs_ = trailer->phdrs;
2148 // Add a symbol to be defined.
2151 Output_section_definition::add_symbol_assignment(const char* name,
2157 Output_section_element* p = new Output_section_element_assignment(name,
2162 this->elements_.push_back(p);
2165 // Add an assignment to the special dot symbol.
2168 Output_section_definition::add_dot_assignment(Expression* value)
2170 Output_section_element* p = new Output_section_element_dot_assignment(value);
2171 this->elements_.push_back(p);
2174 // Add an assertion.
2177 Output_section_definition::add_assertion(Expression* check,
2178 const char* message,
2181 Output_section_element* p = new Output_section_element_assertion(check,
2184 this->elements_.push_back(p);
2187 // Add a data item to the current output section.
2190 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
2192 Output_section_element* p = new Output_section_element_data(size, is_signed,
2194 this->elements_.push_back(p);
2197 // Add a setting for the fill value.
2200 Output_section_definition::add_fill(Expression* val)
2202 Output_section_element* p = new Output_section_element_fill(val);
2203 this->elements_.push_back(p);
2206 // Add an input section specification.
2209 Output_section_definition::add_input_section(const Input_section_spec* spec,
2212 Output_section_element* p = new Output_section_element_input(spec, keep);
2213 this->elements_.push_back(p);
2216 // Create any required output sections. We need an output section if
2217 // there is a data statement here.
2220 Output_section_definition::create_sections(Layout* layout)
2222 if (this->output_section_ != NULL)
2224 for (Output_section_elements::const_iterator p = this->elements_.begin();
2225 p != this->elements_.end();
2228 if ((*p)->needs_output_section())
2230 const char* name = this->name_.c_str();
2231 this->output_section_ =
2232 layout->make_output_section_for_script(name, this->section_type());
2238 // Add any symbols being defined to the symbol table.
2241 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
2243 for (Output_section_elements::iterator p = this->elements_.begin();
2244 p != this->elements_.end();
2246 (*p)->add_symbols_to_table(symtab);
2249 // Finalize symbols and check assertions.
2252 Output_section_definition::finalize_symbols(Symbol_table* symtab,
2253 const Layout* layout,
2254 uint64_t* dot_value)
2256 if (this->output_section_ != NULL)
2257 *dot_value = this->output_section_->address();
2260 uint64_t address = *dot_value;
2261 if (this->address_ != NULL)
2263 address = this->address_->eval_with_dot(symtab, layout, true,
2267 if (this->align_ != NULL)
2269 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
2272 address = align_address(address, align);
2274 *dot_value = address;
2277 Output_section* dot_section = this->output_section_;
2278 for (Output_section_elements::iterator p = this->elements_.begin();
2279 p != this->elements_.end();
2281 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
2284 // Return the output section name to use for an input section name.
2287 Output_section_definition::output_section_name(
2288 const char* file_name,
2289 const char* section_name,
2290 Output_section*** slot,
2291 Script_sections::Section_type* psection_type,
2293 bool match_input_spec)
2295 // If the section is a linker-created output section, just look for a match
2296 // on the output section name.
2297 if (!match_input_spec && this->name_ != "/DISCARD/")
2299 if (this->name_ != section_name)
2301 *slot = &this->output_section_;
2302 *psection_type = this->section_type();
2303 return this->name_.c_str();
2306 // Ask each element whether it matches NAME.
2307 for (Output_section_elements::const_iterator p = this->elements_.begin();
2308 p != this->elements_.end();
2311 if ((*p)->match_name(file_name, section_name, keep))
2313 // We found a match for NAME, which means that it should go
2314 // into this output section.
2315 *slot = &this->output_section_;
2316 *psection_type = this->section_type();
2317 return this->name_.c_str();
2321 // We don't know about this section name.
2325 // Return true if memory from START to START + LENGTH is contained
2326 // within a memory region.
2329 Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
2330 uint64_t start, uint64_t length) const
2332 if (this->memory_regions_ == NULL)
2335 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2336 mr != this->memory_regions_->end();
2339 uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
2340 uint64_t l = (*mr)->length()->eval(symtab, layout, false);
2343 && (s + l) >= (start + length))
2350 // Find a memory region that should be used by a given output SECTION.
2351 // If provided set PREVIOUS_SECTION_RETURN to point to the last section
2352 // that used the return memory region.
2355 Script_sections::find_memory_region(
2356 Output_section_definition* section,
2357 bool find_vma_region,
2359 Output_section_definition** previous_section_return)
2361 if (previous_section_return != NULL)
2362 * previous_section_return = NULL;
2364 // Walk the memory regions specified in this script, if any.
2365 if (this->memory_regions_ == NULL)
2368 // The /DISCARD/ section never gets assigned to any region.
2369 if (section->get_section_name() == "/DISCARD/")
2372 Memory_region* first_match = NULL;
2374 // First check to see if a region has been assigned to this section.
2375 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2376 mr != this->memory_regions_->end();
2379 if (find_vma_region)
2381 for (Memory_region::Section_list::const_iterator s =
2382 (*mr)->get_vma_section_list_start();
2383 s != (*mr)->get_vma_section_list_end();
2385 if ((*s) == section)
2387 (*mr)->set_last_section(section);
2393 for (Memory_region::Section_list::const_iterator s =
2394 (*mr)->get_lma_section_list_start();
2395 s != (*mr)->get_lma_section_list_end();
2397 if ((*s) == section)
2399 (*mr)->set_last_section(section);
2406 // Make a note of the first memory region whose attributes
2407 // are compatible with the section. If we do not find an
2408 // explicit region assignment, then we will return this region.
2409 Output_section* out_sec = section->get_output_section();
2410 if (first_match == NULL
2412 && (*mr)->attributes_compatible(out_sec->flags(),
2418 // With LMA computations, if an explicit region has not been specified then
2419 // we will want to set the difference between the VMA and the LMA of the
2420 // section were searching for to be the same as the difference between the
2421 // VMA and LMA of the last section to be added to first matched region.
2422 // Hence, if it was asked for, we return a pointer to the last section
2423 // known to be used by the first matched region.
2424 if (first_match != NULL
2425 && previous_section_return != NULL)
2426 *previous_section_return = first_match->get_last_section();
2431 // Set the section address. Note that the OUTPUT_SECTION_ field will
2432 // be NULL if no input sections were mapped to this output section.
2433 // We still have to adjust dot and process symbol assignments.
2436 Output_section_definition::set_section_addresses(Symbol_table* symtab,
2438 uint64_t* dot_value,
2439 uint64_t* dot_alignment,
2440 uint64_t* load_address)
2442 Memory_region* vma_region = NULL;
2443 Memory_region* lma_region = NULL;
2444 Script_sections* script_sections =
2445 layout->script_options()->script_sections();
2447 uint64_t old_dot_value = *dot_value;
2448 uint64_t old_load_address = *load_address;
2450 // If input section sorting is requested via --section-ordering-file or
2451 // linker plugins, then do it here. This is important because we want
2452 // any sorting specified in the linker scripts, which will be done after
2453 // this, to take precedence. The final order of input sections is then
2454 // guaranteed to be according to the linker script specification.
2455 if (this->output_section_ != NULL
2456 && this->output_section_->input_section_order_specified())
2457 this->output_section_->sort_attached_input_sections();
2459 // Decide the start address for the section. The algorithm is:
2460 // 1) If an address has been specified in a linker script, use that.
2461 // 2) Otherwise if a memory region has been specified for the section,
2462 // use the next free address in the region.
2463 // 3) Otherwise if memory regions have been specified find the first
2464 // region whose attributes are compatible with this section and
2465 // install it into that region.
2466 // 4) Otherwise use the current location counter.
2468 if (this->output_section_ != NULL
2469 // Check for --section-start.
2470 && parameters->options().section_start(this->output_section_->name(),
2473 else if (this->address_ == NULL)
2475 vma_region = script_sections->find_memory_region(this, true, false, NULL);
2476 if (vma_region != NULL)
2477 address = vma_region->get_current_address()->eval(symtab, layout,
2480 address = *dot_value;
2484 vma_region = script_sections->find_memory_region(this, true, true, NULL);
2485 address = this->address_->eval_with_dot(symtab, layout, true,
2486 *dot_value, NULL, NULL,
2487 dot_alignment, false);
2488 if (vma_region != NULL)
2489 vma_region->set_address(address, symtab, layout);
2493 if (this->align_ == NULL)
2495 if (this->output_section_ == NULL)
2498 align = this->output_section_->addralign();
2502 Output_section* align_section;
2503 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
2504 NULL, &align_section, NULL, false);
2505 if (align_section != NULL)
2506 gold_warning(_("alignment of section %s is not absolute"),
2507 this->name_.c_str());
2508 if (this->output_section_ != NULL)
2509 this->output_section_->set_addralign(align);
2513 if (this->subalign_ == NULL)
2517 Output_section* subalign_section;
2518 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2520 &subalign_section, NULL,
2522 if (subalign_section != NULL)
2523 gold_warning(_("subalign of section %s is not absolute"),
2524 this->name_.c_str());
2526 // Reserve a value of 0 to mean there is no SUBALIGN property.
2530 // The external alignment of the output section must be at least
2531 // as large as that of the input sections. If there is no
2532 // explicit ALIGN property, we set the output section alignment
2533 // to match the input section alignment.
2534 if (align < subalign || this->align_ == NULL)
2537 this->output_section_->set_addralign(align);
2541 address = align_address(address, align);
2543 uint64_t start_address = address;
2545 *dot_value = address;
2547 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2548 // forced to zero, regardless of what the linker script wants.
2549 if (this->output_section_ != NULL
2550 && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
2551 || this->output_section_->is_noload()))
2552 this->output_section_->set_address(address);
2554 this->evaluated_address_ = address;
2555 this->evaluated_addralign_ = align;
2559 if (this->load_address_ == NULL)
2561 Output_section_definition* previous_section;
2563 // Determine if an LMA region has been set for this section.
2564 lma_region = script_sections->find_memory_region(this, false, false,
2567 if (lma_region != NULL)
2569 if (previous_section == NULL)
2570 // The LMA address was explicitly set to the given region.
2571 laddr = lma_region->get_current_address()->eval(symtab, layout,
2575 // We are not going to use the discovered lma_region, so
2576 // make sure that we do not update it in the code below.
2579 if (this->address_ != NULL || previous_section == this)
2581 // Either an explicit VMA address has been set, or an
2582 // explicit VMA region has been set, so set the LMA equal to
2588 // The LMA address was not explicitly or implicitly set.
2590 // We have been given the first memory region that is
2591 // compatible with the current section and a pointer to the
2592 // last section to use this region. Set the LMA of this
2593 // section so that the difference between its' VMA and LMA
2594 // is the same as the difference between the VMA and LMA of
2595 // the last section in the given region.
2596 laddr = address + (previous_section->evaluated_load_address_
2597 - previous_section->evaluated_address_);
2601 if (this->output_section_ != NULL)
2602 this->output_section_->set_load_address(laddr);
2606 // Do not set the load address of the output section, if one exists.
2607 // This allows future sections to determine what the load address
2608 // should be. If none is ever set, it will default to being the
2609 // same as the vma address.
2615 laddr = this->load_address_->eval_with_dot(symtab, layout, true,
2617 this->output_section_,
2619 if (this->output_section_ != NULL)
2620 this->output_section_->set_load_address(laddr);
2623 this->evaluated_load_address_ = laddr;
2626 if (this->fill_ != NULL)
2628 // FIXME: The GNU linker supports fill values of arbitrary
2630 Output_section* fill_section;
2631 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
2633 NULL, &fill_section,
2635 if (fill_section != NULL)
2636 gold_warning(_("fill of section %s is not absolute"),
2637 this->name_.c_str());
2638 unsigned char fill_buff[4];
2639 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2640 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2643 Input_section_list input_sections;
2644 if (this->output_section_ != NULL)
2646 // Get the list of input sections attached to this output
2647 // section. This will leave the output section with only
2648 // Output_section_data entries.
2649 address += this->output_section_->get_input_sections(address,
2652 *dot_value = address;
2655 Output_section* dot_section = this->output_section_;
2656 for (Output_section_elements::iterator p = this->elements_.begin();
2657 p != this->elements_.end();
2659 (*p)->set_section_addresses(symtab, layout, this->output_section_,
2660 subalign, dot_value, dot_alignment,
2661 &dot_section, &fill, &input_sections);
2663 gold_assert(input_sections.empty());
2665 if (vma_region != NULL)
2667 // Update the VMA region being used by the section now that we know how
2668 // big it is. Use the current address in the region, rather than
2669 // start_address because that might have been aligned upwards and we
2670 // need to allow for the padding.
2671 Expression* addr = vma_region->get_current_address();
2672 uint64_t size = *dot_value - addr->eval(symtab, layout, false);
2674 vma_region->increment_offset(this->get_section_name(), size,
2678 // If the LMA region is different from the VMA region, then increment the
2679 // offset there as well. Note that we use the same "dot_value -
2680 // start_address" formula that is used in the load_address assignment below.
2681 if (lma_region != NULL && lma_region != vma_region)
2682 lma_region->increment_offset(this->get_section_name(),
2683 *dot_value - start_address,
2686 // Compute the load address for the following section.
2687 if (this->output_section_ == NULL)
2688 *load_address = *dot_value;
2689 else if (this->load_address_ == NULL)
2691 if (lma_region == NULL)
2692 *load_address = *dot_value;
2695 lma_region->get_current_address()->eval(symtab, layout, false);
2698 *load_address = (this->output_section_->load_address()
2699 + (*dot_value - start_address));
2701 if (this->output_section_ != NULL)
2703 if (this->is_relro_)
2704 this->output_section_->set_is_relro();
2706 this->output_section_->clear_is_relro();
2708 // If this is a NOLOAD section, keep dot and load address unchanged.
2709 if (this->output_section_->is_noload())
2711 *dot_value = old_dot_value;
2712 *load_address = old_load_address;
2717 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2718 // this section is constrained, and the input sections do not match,
2719 // return the constraint, and set *POSD.
2722 Output_section_definition::check_constraint(Output_section_definition** posd)
2724 switch (this->constraint_)
2726 case CONSTRAINT_NONE:
2727 return CONSTRAINT_NONE;
2729 case CONSTRAINT_ONLY_IF_RO:
2730 if (this->output_section_ != NULL
2731 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2734 return CONSTRAINT_ONLY_IF_RO;
2736 return CONSTRAINT_NONE;
2738 case CONSTRAINT_ONLY_IF_RW:
2739 if (this->output_section_ != NULL
2740 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2743 return CONSTRAINT_ONLY_IF_RW;
2745 return CONSTRAINT_NONE;
2747 case CONSTRAINT_SPECIAL:
2748 if (this->output_section_ != NULL)
2749 gold_error(_("SPECIAL constraints are not implemented"));
2750 return CONSTRAINT_NONE;
2757 // See if this is the alternate output section for a constrained
2758 // output section. If it is, transfer the Output_section and return
2759 // true. Otherwise return false.
2762 Output_section_definition::alternate_constraint(
2763 Output_section_definition* posd,
2764 Section_constraint constraint)
2766 if (this->name_ != posd->name_)
2771 case CONSTRAINT_ONLY_IF_RO:
2772 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2776 case CONSTRAINT_ONLY_IF_RW:
2777 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2785 // We have found the alternate constraint. We just need to move
2786 // over the Output_section. When constraints are used properly,
2787 // THIS should not have an output_section pointer, as all the input
2788 // sections should have matched the other definition.
2790 if (this->output_section_ != NULL)
2791 gold_error(_("mismatched definition for constrained sections"));
2793 this->output_section_ = posd->output_section_;
2794 posd->output_section_ = NULL;
2796 if (this->is_relro_)
2797 this->output_section_->set_is_relro();
2799 this->output_section_->clear_is_relro();
2804 // Get the list of segments to use for an allocated section when using
2808 Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2811 // Update phdrs_list even if we don't have an output section. It
2812 // might be used by the following sections.
2813 if (this->phdrs_ != NULL)
2814 *phdrs_list = this->phdrs_;
2816 if (this->output_section_ == NULL)
2818 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2821 return this->output_section_;
2824 // Look for an output section by name and return the address, the load
2825 // address, the alignment, and the size. This is used when an
2826 // expression refers to an output section which was not actually
2827 // created. This returns true if the section was found, false
2831 Output_section_definition::get_output_section_info(const char* name,
2833 uint64_t* load_address,
2834 uint64_t* addralign,
2835 uint64_t* size) const
2837 if (this->name_ != name)
2840 if (this->output_section_ != NULL)
2842 *address = this->output_section_->address();
2843 if (this->output_section_->has_load_address())
2844 *load_address = this->output_section_->load_address();
2846 *load_address = *address;
2847 *addralign = this->output_section_->addralign();
2848 *size = this->output_section_->current_data_size();
2852 *address = this->evaluated_address_;
2853 *load_address = this->evaluated_load_address_;
2854 *addralign = this->evaluated_addralign_;
2861 // Print for debugging.
2864 Output_section_definition::print(FILE* f) const
2866 fprintf(f, " %s ", this->name_.c_str());
2868 if (this->address_ != NULL)
2870 this->address_->print(f);
2874 if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2876 this->script_section_type_name(this->script_section_type_));
2880 if (this->load_address_ != NULL)
2883 this->load_address_->print(f);
2887 if (this->align_ != NULL)
2889 fprintf(f, "ALIGN(");
2890 this->align_->print(f);
2894 if (this->subalign_ != NULL)
2896 fprintf(f, "SUBALIGN(");
2897 this->subalign_->print(f);
2903 for (Output_section_elements::const_iterator p = this->elements_.begin();
2904 p != this->elements_.end();
2910 if (this->fill_ != NULL)
2913 this->fill_->print(f);
2916 if (this->phdrs_ != NULL)
2918 for (String_list::const_iterator p = this->phdrs_->begin();
2919 p != this->phdrs_->end();
2921 fprintf(f, " :%s", p->c_str());
2927 Script_sections::Section_type
2928 Output_section_definition::section_type() const
2930 switch (this->script_section_type_)
2932 case SCRIPT_SECTION_TYPE_NONE:
2933 return Script_sections::ST_NONE;
2934 case SCRIPT_SECTION_TYPE_NOLOAD:
2935 return Script_sections::ST_NOLOAD;
2936 case SCRIPT_SECTION_TYPE_COPY:
2937 case SCRIPT_SECTION_TYPE_DSECT:
2938 case SCRIPT_SECTION_TYPE_INFO:
2939 case SCRIPT_SECTION_TYPE_OVERLAY:
2940 // There are not really support so we treat them as ST_NONE. The
2941 // parse should have issued errors for them already.
2942 return Script_sections::ST_NONE;
2948 // Return the name of a script section type.
2951 Output_section_definition::script_section_type_name(
2952 Script_section_type script_section_type)
2954 switch (script_section_type)
2956 case SCRIPT_SECTION_TYPE_NONE:
2958 case SCRIPT_SECTION_TYPE_NOLOAD:
2960 case SCRIPT_SECTION_TYPE_DSECT:
2962 case SCRIPT_SECTION_TYPE_COPY:
2964 case SCRIPT_SECTION_TYPE_INFO:
2966 case SCRIPT_SECTION_TYPE_OVERLAY:
2974 Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
2976 gold_assert(mr != NULL);
2977 // Add the current section to the specified region's list.
2978 mr->add_section(this, set_vma);
2981 // An output section created to hold orphaned input sections. These
2982 // do not actually appear in linker scripts. However, for convenience
2983 // when setting the output section addresses, we put a marker to these
2984 // sections in the appropriate place in the list of SECTIONS elements.
2986 class Orphan_output_section : public Sections_element
2989 Orphan_output_section(Output_section* os)
2993 // Return whether the orphan output section is relro. We can just
2994 // check the output section because we always set the flag, if
2995 // needed, just after we create the Orphan_output_section.
2998 { return this->os_->is_relro(); }
3000 // Initialize OSP with an output section. This should have been
3003 orphan_section_init(Orphan_section_placement*,
3004 Script_sections::Elements_iterator)
3005 { gold_unreachable(); }
3007 // Set section addresses.
3009 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
3012 // Get the list of segments to use for an allocated section when
3013 // using a PHDRS clause.
3015 allocate_to_segment(String_list**, bool*);
3017 // Return the associated Output_section.
3019 get_output_section() const
3020 { return this->os_; }
3022 // Print for debugging.
3024 print(FILE* f) const
3026 fprintf(f, " marker for orphaned output section %s\n",
3031 Output_section* os_;
3034 // Set section addresses.
3037 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
3038 uint64_t* dot_value,
3040 uint64_t* load_address)
3042 typedef std::list<Output_section::Input_section> Input_section_list;
3044 bool have_load_address = *load_address != *dot_value;
3046 uint64_t address = *dot_value;
3047 address = align_address(address, this->os_->addralign());
3049 // If input section sorting is requested via --section-ordering-file or
3050 // linker plugins, then do it here. This is important because we want
3051 // any sorting specified in the linker scripts, which will be done after
3052 // this, to take precedence. The final order of input sections is then
3053 // guaranteed to be according to the linker script specification.
3054 if (this->os_ != NULL
3055 && this->os_->input_section_order_specified())
3056 this->os_->sort_attached_input_sections();
3058 // For a relocatable link, all orphan sections are put at
3059 // address 0. In general we expect all sections to be at
3060 // address 0 for a relocatable link, but we permit the linker
3061 // script to override that for specific output sections.
3062 if (parameters->options().relocatable())
3066 have_load_address = false;
3069 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
3071 this->os_->set_address(address);
3072 if (have_load_address)
3073 this->os_->set_load_address(align_address(*load_address,
3074 this->os_->addralign()));
3077 Input_section_list input_sections;
3078 address += this->os_->get_input_sections(address, "", &input_sections);
3080 for (Input_section_list::iterator p = input_sections.begin();
3081 p != input_sections.end();
3084 uint64_t addralign = p->addralign();
3085 if (!p->is_input_section())
3086 p->output_section_data()->finalize_data_size();
3087 uint64_t size = p->data_size();
3088 address = align_address(address, addralign);
3089 this->os_->add_script_input_section(*p);
3093 if (parameters->options().relocatable())
3095 // For a relocatable link, reset DOT_VALUE to 0.
3099 else if (this->os_ == NULL
3100 || (this->os_->flags() & elfcpp::SHF_TLS) == 0
3101 || this->os_->type() != elfcpp::SHT_NOBITS)
3103 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
3104 if (!have_load_address)
3105 *load_address = address;
3107 *load_address += address - *dot_value;
3109 *dot_value = address;
3113 // Get the list of segments to use for an allocated section when using
3114 // a PHDRS clause. If this is an allocated section, return the
3115 // Output_section. We don't change the list of segments.
3118 Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
3120 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
3126 // Class Phdrs_element. A program header from a PHDRS clause.
3131 Phdrs_element(const char* name, size_t namelen, unsigned int type,
3132 bool includes_filehdr, bool includes_phdrs,
3133 bool is_flags_valid, unsigned int flags,
3134 Expression* load_address)
3135 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
3136 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
3137 flags_(flags), load_address_(load_address), load_address_value_(0),
3141 // Return the name of this segment.
3144 { return this->name_; }
3146 // Return the type of the segment.
3149 { return this->type_; }
3151 // Whether to include the file header.
3153 includes_filehdr() const
3154 { return this->includes_filehdr_; }
3156 // Whether to include the program headers.
3158 includes_phdrs() const
3159 { return this->includes_phdrs_; }
3161 // Return whether there is a load address.
3163 has_load_address() const
3164 { return this->load_address_ != NULL; }
3166 // Evaluate the load address expression if there is one.
3168 eval_load_address(Symbol_table* symtab, Layout* layout)
3170 if (this->load_address_ != NULL)
3171 this->load_address_value_ = this->load_address_->eval(symtab, layout,
3175 // Return the load address.
3177 load_address() const
3179 gold_assert(this->load_address_ != NULL);
3180 return this->load_address_value_;
3183 // Create the segment.
3185 create_segment(Layout* layout)
3187 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
3188 return this->segment_;
3191 // Return the segment.
3194 { return this->segment_; }
3196 // Release the segment.
3199 { this->segment_ = NULL; }
3201 // Set the segment flags if appropriate.
3203 set_flags_if_valid()
3205 if (this->is_flags_valid_)
3206 this->segment_->set_flags(this->flags_);
3209 // Print for debugging.
3214 // The name used in the script.
3216 // The type of the segment (PT_LOAD, etc.).
3218 // Whether this segment includes the file header.
3219 bool includes_filehdr_;
3220 // Whether this segment includes the section headers.
3221 bool includes_phdrs_;
3222 // Whether the flags were explicitly specified.
3223 bool is_flags_valid_;
3224 // The flags for this segment (PF_R, etc.) if specified.
3225 unsigned int flags_;
3226 // The expression for the load address for this segment. This may
3228 Expression* load_address_;
3229 // The actual load address from evaluating the expression.
3230 uint64_t load_address_value_;
3231 // The segment itself.
3232 Output_segment* segment_;
3235 // Print for debugging.
3238 Phdrs_element::print(FILE* f) const
3240 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
3241 if (this->includes_filehdr_)
3242 fprintf(f, " FILEHDR");
3243 if (this->includes_phdrs_)
3244 fprintf(f, " PHDRS");
3245 if (this->is_flags_valid_)
3246 fprintf(f, " FLAGS(%u)", this->flags_);
3247 if (this->load_address_ != NULL)
3250 this->load_address_->print(f);
3256 // Add a memory region.
3259 Script_sections::add_memory_region(const char* name, size_t namelen,
3260 unsigned int attributes,
3261 Expression* start, Expression* length)
3263 if (this->memory_regions_ == NULL)
3264 this->memory_regions_ = new Memory_regions();
3265 else if (this->find_memory_region(name, namelen))
3267 gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
3269 // FIXME: Add a GOLD extension to allow multiple regions with the same
3270 // name. This would amount to a single region covering disjoint blocks
3271 // of memory, which is useful for embedded devices.
3274 // FIXME: Check the length and start values. Currently we allow
3275 // non-constant expressions for these values, whereas LD does not.
3277 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
3278 // describe a region that packs from the end address going down, rather
3279 // than the start address going up. This would be useful for embedded
3282 this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
3286 // Find a memory region.
3289 Script_sections::find_memory_region(const char* name, size_t namelen)
3291 if (this->memory_regions_ == NULL)
3294 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3295 m != this->memory_regions_->end();
3297 if ((*m)->name_match(name, namelen))
3303 // Find a memory region's origin.
3306 Script_sections::find_memory_region_origin(const char* name, size_t namelen)
3308 Memory_region* mr = find_memory_region(name, namelen);
3312 return mr->start_address();
3315 // Find a memory region's length.
3318 Script_sections::find_memory_region_length(const char* name, size_t namelen)
3320 Memory_region* mr = find_memory_region(name, namelen);
3324 return mr->length();
3327 // Set the memory region to use for the current section.
3330 Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
3332 gold_assert(!this->sections_elements_->empty());
3333 this->sections_elements_->back()->set_memory_region(mr, set_vma);
3336 // Class Script_sections.
3338 Script_sections::Script_sections()
3339 : saw_sections_clause_(false),
3340 in_sections_clause_(false),
3341 sections_elements_(NULL),
3342 output_section_(NULL),
3343 memory_regions_(NULL),
3344 phdrs_elements_(NULL),
3345 orphan_section_placement_(NULL),
3346 data_segment_align_start_(),
3347 saw_data_segment_align_(false),
3348 saw_relro_end_(false),
3349 saw_segment_start_expression_(false),
3350 segments_created_(false)
3354 // Start a SECTIONS clause.
3357 Script_sections::start_sections()
3359 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
3360 this->saw_sections_clause_ = true;
3361 this->in_sections_clause_ = true;
3362 if (this->sections_elements_ == NULL)
3363 this->sections_elements_ = new Sections_elements;
3366 // Finish a SECTIONS clause.
3369 Script_sections::finish_sections()
3371 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
3372 this->in_sections_clause_ = false;
3375 // Add a symbol to be defined.
3378 Script_sections::add_symbol_assignment(const char* name, size_t length,
3379 Expression* val, bool provide,
3382 if (this->output_section_ != NULL)
3383 this->output_section_->add_symbol_assignment(name, length, val,
3387 Sections_element* p = new Sections_element_assignment(name, length,
3390 this->sections_elements_->push_back(p);
3394 // Add an assignment to the special dot symbol.
3397 Script_sections::add_dot_assignment(Expression* val)
3399 if (this->output_section_ != NULL)
3400 this->output_section_->add_dot_assignment(val);
3403 // The GNU linker permits assignments to . to appears outside of
3404 // a SECTIONS clause, and treats it as appearing inside, so
3405 // sections_elements_ may be NULL here.
3406 if (this->sections_elements_ == NULL)
3408 this->sections_elements_ = new Sections_elements;
3409 this->saw_sections_clause_ = true;
3412 Sections_element* p = new Sections_element_dot_assignment(val);
3413 this->sections_elements_->push_back(p);
3417 // Add an assertion.
3420 Script_sections::add_assertion(Expression* check, const char* message,
3423 if (this->output_section_ != NULL)
3424 this->output_section_->add_assertion(check, message, messagelen);
3427 Sections_element* p = new Sections_element_assertion(check, message,
3429 this->sections_elements_->push_back(p);
3433 // Start processing entries for an output section.
3436 Script_sections::start_output_section(
3439 const Parser_output_section_header* header)
3441 Output_section_definition* posd = new Output_section_definition(name,
3444 this->sections_elements_->push_back(posd);
3445 gold_assert(this->output_section_ == NULL);
3446 this->output_section_ = posd;
3449 // Stop processing entries for an output section.
3452 Script_sections::finish_output_section(
3453 const Parser_output_section_trailer* trailer)
3455 gold_assert(this->output_section_ != NULL);
3456 this->output_section_->finish(trailer);
3457 this->output_section_ = NULL;
3460 // Add a data item to the current output section.
3463 Script_sections::add_data(int size, bool is_signed, Expression* val)
3465 gold_assert(this->output_section_ != NULL);
3466 this->output_section_->add_data(size, is_signed, val);
3469 // Add a fill value setting to the current output section.
3472 Script_sections::add_fill(Expression* val)
3474 gold_assert(this->output_section_ != NULL);
3475 this->output_section_->add_fill(val);
3478 // Add an input section specification to the current output section.
3481 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3483 gold_assert(this->output_section_ != NULL);
3484 this->output_section_->add_input_section(spec, keep);
3487 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
3488 // subsequent output sections may be relro.
3491 Script_sections::data_segment_align()
3493 if (this->saw_data_segment_align_)
3494 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3495 gold_assert(!this->sections_elements_->empty());
3496 Sections_elements::iterator p = this->sections_elements_->end();
3498 this->data_segment_align_start_ = p;
3499 this->saw_data_segment_align_ = true;
3502 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
3503 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3506 Script_sections::data_segment_relro_end()
3508 if (this->saw_relro_end_)
3509 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3510 "in a linker script"));
3511 this->saw_relro_end_ = true;
3513 if (!this->saw_data_segment_align_)
3514 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3517 Sections_elements::iterator p = this->data_segment_align_start_;
3518 for (++p; p != this->sections_elements_->end(); ++p)
3519 (*p)->set_is_relro();
3523 // Create any required sections.
3526 Script_sections::create_sections(Layout* layout)
3528 if (!this->saw_sections_clause_)
3530 for (Sections_elements::iterator p = this->sections_elements_->begin();
3531 p != this->sections_elements_->end();
3533 (*p)->create_sections(layout);
3536 // Add any symbols we are defining to the symbol table.
3539 Script_sections::add_symbols_to_table(Symbol_table* symtab)
3541 if (!this->saw_sections_clause_)
3543 for (Sections_elements::iterator p = this->sections_elements_->begin();
3544 p != this->sections_elements_->end();
3546 (*p)->add_symbols_to_table(symtab);
3549 // Finalize symbols and check assertions.
3552 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3554 if (!this->saw_sections_clause_)
3556 uint64_t dot_value = 0;
3557 for (Sections_elements::iterator p = this->sections_elements_->begin();
3558 p != this->sections_elements_->end();
3560 (*p)->finalize_symbols(symtab, layout, &dot_value);
3563 // Return the name of the output section to use for an input file name
3564 // and section name.
3567 Script_sections::output_section_name(
3568 const char* file_name,
3569 const char* section_name,
3570 Output_section*** output_section_slot,
3571 Script_sections::Section_type* psection_type,
3573 bool is_input_section)
3575 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3576 p != this->sections_elements_->end();
3579 const char* ret = (*p)->output_section_name(file_name, section_name,
3580 output_section_slot,
3581 psection_type, keep,
3586 // The special name /DISCARD/ means that the input section
3587 // should be discarded.
3588 if (strcmp(ret, "/DISCARD/") == 0)
3590 *output_section_slot = NULL;
3591 *psection_type = Script_sections::ST_NONE;
3598 // We have an orphan section.
3599 *output_section_slot = NULL;
3600 *psection_type = Script_sections::ST_NONE;
3603 General_options::Orphan_handling orphan_handling =
3604 parameters->options().orphan_handling_enum();
3605 if (orphan_handling == General_options::ORPHAN_DISCARD)
3607 if (orphan_handling == General_options::ORPHAN_ERROR)
3609 if (file_name == NULL)
3610 gold_error(_("unplaced orphan section '%s'"), section_name);
3612 gold_error(_("unplaced orphan section '%s' from '%s'"),
3613 section_name, file_name);
3616 if (orphan_handling == General_options::ORPHAN_WARN)
3618 if (file_name == NULL)
3619 gold_warning(_("orphan section '%s' is being placed in section '%s'"),
3620 section_name, section_name);
3622 gold_warning(_("orphan section '%s' from '%s' is being placed "
3624 section_name, file_name, section_name);
3627 // If we couldn't find a mapping for the name, the output section
3628 // gets the name of the input section.
3629 return section_name;
3632 // Place a marker for an orphan output section into the SECTIONS
3636 Script_sections::place_orphan(Output_section* os)
3638 Orphan_section_placement* osp = this->orphan_section_placement_;
3641 // Initialize the Orphan_section_placement structure.
3642 osp = new Orphan_section_placement();
3643 for (Sections_elements::iterator p = this->sections_elements_->begin();
3644 p != this->sections_elements_->end();
3646 (*p)->orphan_section_init(osp, p);
3647 gold_assert(!this->sections_elements_->empty());
3648 Sections_elements::iterator last = this->sections_elements_->end();
3650 osp->last_init(last);
3651 this->orphan_section_placement_ = osp;
3654 Orphan_output_section* orphan = new Orphan_output_section(os);
3656 // Look for where to put ORPHAN.
3657 Sections_elements::iterator* where;
3658 if (osp->find_place(os, &where))
3660 if ((**where)->is_relro())
3663 os->clear_is_relro();
3665 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3666 // so that the next one goes after this one.
3667 Sections_elements::iterator p = *where;
3668 gold_assert(p != this->sections_elements_->end());
3670 *where = this->sections_elements_->insert(p, orphan);
3674 os->clear_is_relro();
3675 // We don't have a place to put this orphan section. Put it,
3676 // and all other sections like it, at the end, but before the
3677 // sections which always come at the end.
3678 Sections_elements::iterator last = osp->last_place();
3679 *where = this->sections_elements_->insert(last, orphan);
3682 if ((os->flags() & elfcpp::SHF_ALLOC) != 0)
3683 osp->update_last_alloc(*where);
3686 // Set the addresses of all the output sections. Walk through all the
3687 // elements, tracking the dot symbol. Apply assignments which set
3688 // absolute symbol values, in case they are used when setting dot.
3689 // Fill in data statement values. As we find output sections, set the
3690 // address, set the address of all associated input sections, and
3691 // update dot. Return the segment which should hold the file header
3692 // and segment headers, if any.
3695 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3697 gold_assert(this->saw_sections_clause_);
3699 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3700 // for our representation.
3701 for (Sections_elements::iterator p = this->sections_elements_->begin();
3702 p != this->sections_elements_->end();
3705 Output_section_definition* posd;
3706 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3707 if (failed_constraint != CONSTRAINT_NONE)
3709 Sections_elements::iterator q;
3710 for (q = this->sections_elements_->begin();
3711 q != this->sections_elements_->end();
3716 if ((*q)->alternate_constraint(posd, failed_constraint))
3721 if (q == this->sections_elements_->end())
3722 gold_error(_("no matching section constraint"));
3726 // Force the alignment of the first TLS section to be the maximum
3727 // alignment of all TLS sections.
3728 Output_section* first_tls = NULL;
3729 uint64_t tls_align = 0;
3730 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3731 p != this->sections_elements_->end();
3734 Output_section* os = (*p)->get_output_section();
3735 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3737 if (first_tls == NULL)
3739 if (os->addralign() > tls_align)
3740 tls_align = os->addralign();
3743 if (first_tls != NULL)
3744 first_tls->set_addralign(tls_align);
3746 // For a relocatable link, we implicitly set dot to zero.
3747 uint64_t dot_value = 0;
3748 uint64_t dot_alignment = 0;
3749 uint64_t load_address = 0;
3751 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3752 // to set section addresses. If the script has any SEGMENT_START
3753 // expression, we do not set the section addresses.
3754 bool use_tsection_options =
3755 (!this->saw_segment_start_expression_
3756 && (parameters->options().user_set_Ttext()
3757 || parameters->options().user_set_Tdata()
3758 || parameters->options().user_set_Tbss()));
3760 for (Sections_elements::iterator p = this->sections_elements_->begin();
3761 p != this->sections_elements_->end();
3764 Output_section* os = (*p)->get_output_section();
3766 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3767 // the special sections by names and doing dot assignments.
3768 if (use_tsection_options
3770 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3772 uint64_t new_dot_value = dot_value;
3774 if (parameters->options().user_set_Ttext()
3775 && strcmp(os->name(), ".text") == 0)
3776 new_dot_value = parameters->options().Ttext();
3777 else if (parameters->options().user_set_Tdata()
3778 && strcmp(os->name(), ".data") == 0)
3779 new_dot_value = parameters->options().Tdata();
3780 else if (parameters->options().user_set_Tbss()
3781 && strcmp(os->name(), ".bss") == 0)
3782 new_dot_value = parameters->options().Tbss();
3784 // Update dot and load address if necessary.
3785 if (new_dot_value < dot_value)
3786 gold_error(_("dot may not move backward"));
3787 else if (new_dot_value != dot_value)
3789 dot_value = new_dot_value;
3790 load_address = new_dot_value;
3794 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3798 if (this->phdrs_elements_ != NULL)
3800 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3801 p != this->phdrs_elements_->end();
3803 (*p)->eval_load_address(symtab, layout);
3806 return this->create_segments(layout, dot_alignment);
3809 // Sort the sections in order to put them into segments.
3811 class Sort_output_sections
3814 Sort_output_sections(const Script_sections::Sections_elements* elements)
3815 : elements_(elements)
3819 operator()(const Output_section* os1, const Output_section* os2) const;
3823 script_compare(const Output_section* os1, const Output_section* os2) const;
3826 const Script_sections::Sections_elements* elements_;
3830 Sort_output_sections::operator()(const Output_section* os1,
3831 const Output_section* os2) const
3833 // Sort first by the load address.
3834 uint64_t lma1 = (os1->has_load_address()
3835 ? os1->load_address()
3837 uint64_t lma2 = (os2->has_load_address()
3838 ? os2->load_address()
3843 // Then sort by the virtual address.
3844 if (os1->address() != os2->address())
3845 return os1->address() < os2->address();
3847 // If the linker script says which of these sections is first, go
3848 // with what it says.
3849 int i = this->script_compare(os1, os2);
3853 // Sort PROGBITS before NOBITS.
3854 bool nobits1 = os1->type() == elfcpp::SHT_NOBITS;
3855 bool nobits2 = os2->type() == elfcpp::SHT_NOBITS;
3856 if (nobits1 != nobits2)
3859 // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
3861 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3862 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3864 return nobits1 ? tls1 : tls2;
3866 // Sort non-NOLOAD before NOLOAD.
3867 if (os1->is_noload() && !os2->is_noload())
3869 if (!os1->is_noload() && os2->is_noload())
3872 // The sections seem practically identical. Sort by name to get a
3874 return os1->name() < os2->name();
3877 // Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
3878 // if either OS1 or OS2 is not mentioned. This ensures that we keep
3879 // empty sections in the order in which they appear in a linker
3883 Sort_output_sections::script_compare(const Output_section* os1,
3884 const Output_section* os2) const
3886 if (this->elements_ == NULL)
3889 bool found_os1 = false;
3890 bool found_os2 = false;
3891 for (Script_sections::Sections_elements::const_iterator
3892 p = this->elements_->begin();
3893 p != this->elements_->end();
3896 if (os2 == (*p)->get_output_section())
3902 else if (os1 == (*p)->get_output_section())
3913 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3914 // We treat a section with the SHF_TLS flag set as taking up space
3915 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3916 // space for them in the file.
3919 Script_sections::is_bss_section(const Output_section* os)
3921 return (os->type() == elfcpp::SHT_NOBITS
3922 && (os->flags() & elfcpp::SHF_TLS) == 0);
3925 // Return the size taken by the file header and the program headers.
3928 Script_sections::total_header_size(Layout* layout) const
3930 size_t segment_count = layout->segment_count();
3931 size_t file_header_size;
3932 size_t segment_headers_size;
3933 if (parameters->target().get_size() == 32)
3935 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3936 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3938 else if (parameters->target().get_size() == 64)
3940 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3941 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3946 return file_header_size + segment_headers_size;
3949 // Return the amount we have to subtract from the LMA to accommodate
3950 // headers of the given size. The complication is that the file
3951 // header have to be at the start of a page, as otherwise it will not
3952 // be at the start of the file.
3955 Script_sections::header_size_adjustment(uint64_t lma,
3956 size_t sizeof_headers) const
3958 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3959 uint64_t hdr_lma = lma - sizeof_headers;
3960 hdr_lma &= ~(abi_pagesize - 1);
3961 return lma - hdr_lma;
3964 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3965 // the segment which should hold the file header and segment headers,
3969 Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
3971 gold_assert(this->saw_sections_clause_);
3973 if (parameters->options().relocatable())
3976 if (this->saw_phdrs_clause())
3977 return create_segments_from_phdrs_clause(layout, dot_alignment);
3979 Layout::Section_list sections;
3980 layout->get_allocated_sections(§ions);
3982 // Sort the sections by address.
3983 std::stable_sort(sections.begin(), sections.end(),
3984 Sort_output_sections(this->sections_elements_));
3986 this->create_note_and_tls_segments(layout, §ions);
3988 // Walk through the sections adding them to PT_LOAD segments.
3989 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3990 Output_segment* first_seg = NULL;
3991 Output_segment* current_seg = NULL;
3992 bool is_current_seg_readonly = true;
3993 uint64_t last_vma = 0;
3994 uint64_t last_lma = 0;
3995 uint64_t last_size = 0;
3996 bool in_bss = false;
3997 for (Layout::Section_list::iterator p = sections.begin();
3998 p != sections.end();
4001 const uint64_t vma = (*p)->address();
4002 const uint64_t lma = ((*p)->has_load_address()
4003 ? (*p)->load_address()
4005 const uint64_t size = (*p)->current_data_size();
4007 bool need_new_segment;
4008 if (current_seg == NULL)
4009 need_new_segment = true;
4010 else if (lma - vma != last_lma - last_vma)
4012 // This section has a different LMA relationship than the
4013 // last one; we need a new segment.
4014 need_new_segment = true;
4016 else if (align_address(last_lma + last_size, abi_pagesize)
4017 < align_address(lma, abi_pagesize))
4019 // Putting this section in the segment would require
4021 need_new_segment = true;
4023 else if (in_bss && !is_bss_section(*p))
4025 // A non-BSS section can not follow a BSS section in the
4027 need_new_segment = true;
4029 else if (is_current_seg_readonly
4030 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
4031 && !parameters->options().omagic())
4033 // Don't put a writable section in the same segment as a
4034 // non-writable section.
4035 need_new_segment = true;
4039 // Otherwise, reuse the existing segment.
4040 need_new_segment = false;
4043 elfcpp::Elf_Word seg_flags =
4044 Layout::section_flags_to_segment((*p)->flags());
4046 if (need_new_segment)
4048 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
4050 current_seg->set_addresses(vma, lma);
4051 current_seg->set_minimum_p_align(dot_alignment);
4052 if (first_seg == NULL)
4053 first_seg = current_seg;
4054 is_current_seg_readonly = true;
4058 current_seg->add_output_section_to_load(layout, *p, seg_flags);
4060 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
4061 is_current_seg_readonly = false;
4063 if (is_bss_section(*p) && size > 0)
4071 // An ELF program should work even if the program headers are not in
4072 // a PT_LOAD segment. However, it appears that the Linux kernel
4073 // does not set the AT_PHDR auxiliary entry in that case. It sets
4074 // the load address to p_vaddr - p_offset of the first PT_LOAD
4075 // segment. It then sets AT_PHDR to the load address plus the
4076 // offset to the program headers, e_phoff in the file header. This
4077 // fails when the program headers appear in the file before the
4078 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
4079 // segment to hold the file header and the program headers. This is
4080 // effectively what the GNU linker does, and it is slightly more
4081 // efficient in any case. We try to use the first PT_LOAD segment
4082 // if we can, otherwise we make a new one.
4084 if (first_seg == NULL)
4087 // -n or -N mean that the program is not demand paged and there is
4088 // no need to put the program headers in a PT_LOAD segment.
4089 if (parameters->options().nmagic() || parameters->options().omagic())
4092 size_t sizeof_headers = this->total_header_size(layout);
4094 uint64_t vma = first_seg->vaddr();
4095 uint64_t lma = first_seg->paddr();
4097 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
4099 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
4101 first_seg->set_addresses(vma - subtract, lma - subtract);
4105 // If there is no room to squeeze in the headers, then punt. The
4106 // resulting executable probably won't run on GNU/Linux, but we
4107 // trust that the user knows what they are doing.
4108 if (lma < subtract || vma < subtract)
4111 // If memory regions have been specified and the address range
4112 // we are about to use is not contained within any region then
4113 // issue a warning message about the segment we are going to
4114 // create. It will be outside of any region and so possibly
4115 // using non-existent or protected memory. We test LMA rather
4116 // than VMA since we assume that the headers will never be
4118 if (this->memory_regions_ != NULL
4119 && !this->block_in_region (NULL, layout, lma - subtract, subtract))
4120 gold_warning(_("creating a segment to contain the file and program"
4121 " headers outside of any MEMORY region"));
4123 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
4125 load_seg->set_addresses(vma - subtract, lma - subtract);
4130 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
4131 // segment if there are any SHT_TLS sections.
4134 Script_sections::create_note_and_tls_segments(
4136 const Layout::Section_list* sections)
4138 gold_assert(!this->saw_phdrs_clause());
4140 bool saw_tls = false;
4141 for (Layout::Section_list::const_iterator p = sections->begin();
4142 p != sections->end();
4145 if ((*p)->type() == elfcpp::SHT_NOTE)
4147 elfcpp::Elf_Word seg_flags =
4148 Layout::section_flags_to_segment((*p)->flags());
4149 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
4151 oseg->add_output_section_to_nonload(*p, seg_flags);
4153 // Incorporate any subsequent SHT_NOTE sections, in the
4154 // hopes that the script is sensible.
4155 Layout::Section_list::const_iterator pnext = p + 1;
4156 while (pnext != sections->end()
4157 && (*pnext)->type() == elfcpp::SHT_NOTE)
4159 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
4160 oseg->add_output_section_to_nonload(*pnext, seg_flags);
4166 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
4169 gold_error(_("TLS sections are not adjacent"));
4171 elfcpp::Elf_Word seg_flags =
4172 Layout::section_flags_to_segment((*p)->flags());
4173 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
4175 oseg->add_output_section_to_nonload(*p, seg_flags);
4177 Layout::Section_list::const_iterator pnext = p + 1;
4178 while (pnext != sections->end()
4179 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
4181 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
4182 oseg->add_output_section_to_nonload(*pnext, seg_flags);
4190 // If we see a section named .interp then put the .interp section
4191 // in a PT_INTERP segment.
4192 // This is for GNU ld compatibility.
4193 if (strcmp((*p)->name(), ".interp") == 0)
4195 elfcpp::Elf_Word seg_flags =
4196 Layout::section_flags_to_segment((*p)->flags());
4197 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_INTERP,
4199 oseg->add_output_section_to_nonload(*p, seg_flags);
4203 this->segments_created_ = true;
4206 // Add a program header. The PHDRS clause is syntactically distinct
4207 // from the SECTIONS clause, but we implement it with the SECTIONS
4208 // support because PHDRS is useless if there is no SECTIONS clause.
4211 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
4212 bool includes_filehdr, bool includes_phdrs,
4213 bool is_flags_valid, unsigned int flags,
4214 Expression* load_address)
4216 if (this->phdrs_elements_ == NULL)
4217 this->phdrs_elements_ = new Phdrs_elements();
4218 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
4221 is_flags_valid, flags,
4225 // Return the number of segments we expect to create based on the
4226 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
4229 Script_sections::expected_segment_count(const Layout* layout) const
4231 // If we've already created the segments, we won't be adding any more.
4232 if (this->segments_created_)
4235 if (this->saw_phdrs_clause())
4236 return this->phdrs_elements_->size();
4238 Layout::Section_list sections;
4239 layout->get_allocated_sections(§ions);
4241 // We assume that we will need two PT_LOAD segments.
4244 bool saw_note = false;
4245 bool saw_tls = false;
4246 bool saw_interp = false;
4247 for (Layout::Section_list::const_iterator p = sections.begin();
4248 p != sections.end();
4251 if ((*p)->type() == elfcpp::SHT_NOTE)
4253 // Assume that all note sections will fit into a single
4261 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
4263 // There can only be one PT_TLS segment.
4270 else if (strcmp((*p)->name(), ".interp") == 0)
4272 // There can only be one PT_INTERP segment.
4284 // Create the segments from a PHDRS clause. Return the segment which
4285 // should hold the file header and program headers, if any.
4288 Script_sections::create_segments_from_phdrs_clause(Layout* layout,
4289 uint64_t dot_alignment)
4291 this->attach_sections_using_phdrs_clause(layout);
4292 return this->set_phdrs_clause_addresses(layout, dot_alignment);
4295 // Create the segments from the PHDRS clause, and put the output
4296 // sections in them.
4299 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
4301 typedef std::map<std::string, Output_segment*> Name_to_segment;
4302 Name_to_segment name_to_segment;
4303 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4304 p != this->phdrs_elements_->end();
4306 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
4307 this->segments_created_ = true;
4309 // Walk through the output sections and attach them to segments.
4310 // Output sections in the script which do not list segments are
4311 // attached to the same set of segments as the immediately preceding
4314 String_list* phdr_names = NULL;
4315 bool load_segments_only = false;
4316 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4317 p != this->sections_elements_->end();
4321 String_list* old_phdr_names = phdr_names;
4322 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &is_orphan);
4326 elfcpp::Elf_Word seg_flags =
4327 Layout::section_flags_to_segment(os->flags());
4329 if (phdr_names == NULL)
4331 // Don't worry about empty orphan sections.
4332 if (is_orphan && os->current_data_size() > 0)
4333 gold_error(_("allocated section %s not in any segment"),
4336 // To avoid later crashes drop this section into the first
4338 for (Phdrs_elements::const_iterator ppe =
4339 this->phdrs_elements_->begin();
4340 ppe != this->phdrs_elements_->end();
4343 Output_segment* oseg = (*ppe)->segment();
4344 if (oseg->type() == elfcpp::PT_LOAD)
4346 oseg->add_output_section_to_load(layout, os, seg_flags);
4354 // We see a list of segments names. Disable PT_LOAD segment only
4356 if (old_phdr_names != phdr_names)
4357 load_segments_only = false;
4359 // If this is an orphan section--one that was not explicitly
4360 // mentioned in the linker script--then it should not inherit
4361 // any segment type other than PT_LOAD. Otherwise, e.g., the
4362 // PT_INTERP segment will pick up following orphan sections,
4363 // which does not make sense. If this is not an orphan section,
4364 // we trust the linker script.
4367 // Enable PT_LOAD segments only filtering until we see another
4368 // list of segment names.
4369 load_segments_only = true;
4372 bool in_load_segment = false;
4373 for (String_list::const_iterator q = phdr_names->begin();
4374 q != phdr_names->end();
4377 Name_to_segment::const_iterator r = name_to_segment.find(*q);
4378 if (r == name_to_segment.end())
4379 gold_error(_("no segment %s"), q->c_str());
4382 if (load_segments_only
4383 && r->second->type() != elfcpp::PT_LOAD)
4386 if (r->second->type() != elfcpp::PT_LOAD)
4387 r->second->add_output_section_to_nonload(os, seg_flags);
4390 r->second->add_output_section_to_load(layout, os, seg_flags);
4391 if (in_load_segment)
4392 gold_error(_("section in two PT_LOAD segments"));
4393 in_load_segment = true;
4398 if (!in_load_segment)
4399 gold_error(_("allocated section not in any PT_LOAD segment"));
4403 // Set the addresses for segments created from a PHDRS clause. Return
4404 // the segment which should hold the file header and program headers,
4408 Script_sections::set_phdrs_clause_addresses(Layout* layout,
4409 uint64_t dot_alignment)
4411 Output_segment* load_seg = NULL;
4412 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4413 p != this->phdrs_elements_->end();
4416 // Note that we have to set the flags after adding the output
4417 // sections to the segment, as adding an output segment can
4418 // change the flags.
4419 (*p)->set_flags_if_valid();
4421 Output_segment* oseg = (*p)->segment();
4423 if (oseg->type() != elfcpp::PT_LOAD)
4425 // The addresses of non-PT_LOAD segments are set from the
4426 // PT_LOAD segments.
4427 if ((*p)->has_load_address())
4428 gold_error(_("may only specify load address for PT_LOAD segment"));
4432 oseg->set_minimum_p_align(dot_alignment);
4434 // The output sections should have addresses from the SECTIONS
4435 // clause. The addresses don't have to be in order, so find the
4436 // one with the lowest load address. Use that to set the
4437 // address of the segment.
4439 Output_section* osec = oseg->section_with_lowest_load_address();
4442 oseg->set_addresses(0, 0);
4446 uint64_t vma = osec->address();
4447 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
4449 // Override the load address of the section with the load
4450 // address specified for the segment.
4451 if ((*p)->has_load_address())
4453 if (osec->has_load_address())
4454 gold_warning(_("PHDRS load address overrides "
4455 "section %s load address"),
4458 lma = (*p)->load_address();
4461 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
4462 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
4464 // We could support this if we wanted to.
4465 gold_error(_("using only one of FILEHDR and PHDRS is "
4466 "not currently supported"));
4470 size_t sizeof_headers = this->total_header_size(layout);
4471 uint64_t subtract = this->header_size_adjustment(lma,
4473 if (lma >= subtract && vma >= subtract)
4480 gold_error(_("sections loaded on first page without room "
4481 "for file and program headers "
4482 "are not supported"));
4485 if (load_seg != NULL)
4486 gold_error(_("using FILEHDR and PHDRS on more than one "
4487 "PT_LOAD segment is not currently supported"));
4491 oseg->set_addresses(vma, lma);
4497 // Add the file header and segment headers to non-load segments
4498 // specified in the PHDRS clause.
4501 Script_sections::put_headers_in_phdrs(Output_data* file_header,
4502 Output_data* segment_headers)
4504 gold_assert(this->saw_phdrs_clause());
4505 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
4506 p != this->phdrs_elements_->end();
4509 if ((*p)->type() != elfcpp::PT_LOAD)
4511 if ((*p)->includes_phdrs())
4512 (*p)->segment()->add_initial_output_data(segment_headers);
4513 if ((*p)->includes_filehdr())
4514 (*p)->segment()->add_initial_output_data(file_header);
4519 // Look for an output section by name and return the address, the load
4520 // address, the alignment, and the size. This is used when an
4521 // expression refers to an output section which was not actually
4522 // created. This returns true if the section was found, false
4526 Script_sections::get_output_section_info(const char* name, uint64_t* address,
4527 uint64_t* load_address,
4528 uint64_t* addralign,
4529 uint64_t* size) const
4531 if (!this->saw_sections_clause_)
4533 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4534 p != this->sections_elements_->end();
4536 if ((*p)->get_output_section_info(name, address, load_address, addralign,
4542 // Release all Output_segments. This remove all pointers to all
4546 Script_sections::release_segments()
4548 if (this->saw_phdrs_clause())
4550 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4551 p != this->phdrs_elements_->end();
4553 (*p)->release_segment();
4555 this->segments_created_ = false;
4558 // Print the SECTIONS clause to F for debugging.
4561 Script_sections::print(FILE* f) const
4563 if (this->phdrs_elements_ != NULL)
4565 fprintf(f, "PHDRS {\n");
4566 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4567 p != this->phdrs_elements_->end();
4573 if (this->memory_regions_ != NULL)
4575 fprintf(f, "MEMORY {\n");
4576 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
4577 m != this->memory_regions_->end();
4583 if (!this->saw_sections_clause_)
4586 fprintf(f, "SECTIONS {\n");
4588 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4589 p != this->sections_elements_->end();
4596 } // End namespace gold.