* archive.cc: Formatting fixes: Remove whitespace between
[binutils.git] / gold / script-sections.cc
blobf7ed68200e4d6295a4e0d90d6a3896ef34003f70
1 // script-sections.cc -- linker script SECTIONS for gold
3 // Copyright 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cstring>
26 #include <algorithm>
27 #include <list>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <fnmatch.h>
33 #include "parameters.h"
34 #include "object.h"
35 #include "layout.h"
36 #include "output.h"
37 #include "script-c.h"
38 #include "script.h"
39 #include "script-sections.h"
41 // Support for the SECTIONS clause in linker scripts.
43 namespace gold
46 // Manage orphan sections. This is intended to be largely compatible
47 // with the GNU linker. The Linux kernel implicitly relies on
48 // something similar to the GNU linker's orphan placement. We
49 // originally used a simpler scheme here, but it caused the kernel
50 // build to fail, and was also rather inefficient.
52 class Orphan_section_placement
54 private:
55 typedef Script_sections::Elements_iterator Elements_iterator;
57 public:
58 Orphan_section_placement();
60 // Handle an output section during initialization of this mapping.
61 void
62 output_section_init(const std::string& name, Output_section*,
63 Elements_iterator location);
65 // Initialize the last location.
66 void
67 last_init(Elements_iterator location);
69 // Set *PWHERE to the address of an iterator pointing to the
70 // location to use for an orphan section. Return true if the
71 // iterator has a value, false otherwise.
72 bool
73 find_place(Output_section*, Elements_iterator** pwhere);
75 // Return the iterator being used for sections at the very end of
76 // the linker script.
77 Elements_iterator
78 last_place() const;
80 private:
81 // The places that we specifically recognize. This list is copied
82 // from the GNU linker.
83 enum Place_index
85 PLACE_TEXT,
86 PLACE_RODATA,
87 PLACE_DATA,
88 PLACE_TLS,
89 PLACE_TLS_BSS,
90 PLACE_BSS,
91 PLACE_REL,
92 PLACE_INTERP,
93 PLACE_NONALLOC,
94 PLACE_LAST,
95 PLACE_MAX
98 // The information we keep for a specific place.
99 struct Place
101 // The name of sections for this place.
102 const char* name;
103 // Whether we have a location for this place.
104 bool have_location;
105 // The iterator for this place.
106 Elements_iterator location;
109 // Initialize one place element.
110 void
111 initialize_place(Place_index, const char*);
113 // The places.
114 Place places_[PLACE_MAX];
115 // True if this is the first call to output_section_init.
116 bool first_init_;
119 // Initialize Orphan_section_placement.
121 Orphan_section_placement::Orphan_section_placement()
122 : first_init_(true)
124 this->initialize_place(PLACE_TEXT, ".text");
125 this->initialize_place(PLACE_RODATA, ".rodata");
126 this->initialize_place(PLACE_DATA, ".data");
127 this->initialize_place(PLACE_TLS, NULL);
128 this->initialize_place(PLACE_TLS_BSS, NULL);
129 this->initialize_place(PLACE_BSS, ".bss");
130 this->initialize_place(PLACE_REL, NULL);
131 this->initialize_place(PLACE_INTERP, ".interp");
132 this->initialize_place(PLACE_NONALLOC, NULL);
133 this->initialize_place(PLACE_LAST, NULL);
136 // Initialize one place element.
138 void
139 Orphan_section_placement::initialize_place(Place_index index, const char* name)
141 this->places_[index].name = name;
142 this->places_[index].have_location = false;
145 // While initializing the Orphan_section_placement information, this
146 // is called once for each output section named in the linker script.
147 // If we found an output section during the link, it will be passed in
148 // OS.
150 void
151 Orphan_section_placement::output_section_init(const std::string& name,
152 Output_section* os,
153 Elements_iterator location)
155 bool first_init = this->first_init_;
156 this->first_init_ = false;
158 for (int i = 0; i < PLACE_MAX; ++i)
160 if (this->places_[i].name != NULL && this->places_[i].name == name)
162 if (this->places_[i].have_location)
164 // We have already seen a section with this name.
165 return;
168 this->places_[i].location = location;
169 this->places_[i].have_location = true;
171 // If we just found the .bss section, restart the search for
172 // an unallocated section. This follows the GNU linker's
173 // behaviour.
174 if (i == PLACE_BSS)
175 this->places_[PLACE_NONALLOC].have_location = false;
177 return;
181 // Relocation sections.
182 if (!this->places_[PLACE_REL].have_location
183 && os != NULL
184 && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
185 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
187 this->places_[PLACE_REL].location = location;
188 this->places_[PLACE_REL].have_location = true;
191 // We find the location for unallocated sections by finding the
192 // first debugging or comment section after the BSS section (if
193 // there is one).
194 if (!this->places_[PLACE_NONALLOC].have_location
195 && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
197 // We add orphan sections after the location in PLACES_. We
198 // want to store unallocated sections before LOCATION. If this
199 // is the very first section, we can't use it.
200 if (!first_init)
202 --location;
203 this->places_[PLACE_NONALLOC].location = location;
204 this->places_[PLACE_NONALLOC].have_location = true;
209 // Initialize the last location.
211 void
212 Orphan_section_placement::last_init(Elements_iterator location)
214 this->places_[PLACE_LAST].location = location;
215 this->places_[PLACE_LAST].have_location = true;
218 // Set *PWHERE to the address of an iterator pointing to the location
219 // to use for an orphan section. Return true if the iterator has a
220 // value, false otherwise.
222 bool
223 Orphan_section_placement::find_place(Output_section* os,
224 Elements_iterator** pwhere)
226 // Figure out where OS should go. This is based on the GNU linker
227 // code. FIXME: The GNU linker handles small data sections
228 // specially, but we don't.
229 elfcpp::Elf_Word type = os->type();
230 elfcpp::Elf_Xword flags = os->flags();
231 Place_index index;
232 if ((flags & elfcpp::SHF_ALLOC) == 0
233 && !Layout::is_debug_info_section(os->name()))
234 index = PLACE_NONALLOC;
235 else if ((flags & elfcpp::SHF_ALLOC) == 0)
236 index = PLACE_LAST;
237 else if (type == elfcpp::SHT_NOTE)
238 index = PLACE_INTERP;
239 else if ((flags & elfcpp::SHF_TLS) != 0)
241 if (type == elfcpp::SHT_NOBITS)
242 index = PLACE_TLS_BSS;
243 else
244 index = PLACE_TLS;
246 else if (type == elfcpp::SHT_NOBITS)
247 index = PLACE_BSS;
248 else if ((flags & elfcpp::SHF_WRITE) != 0)
249 index = PLACE_DATA;
250 else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
251 index = PLACE_REL;
252 else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
253 index = PLACE_RODATA;
254 else
255 index = PLACE_TEXT;
257 // If we don't have a location yet, try to find one based on a
258 // plausible ordering of sections.
259 if (!this->places_[index].have_location)
261 Place_index follow;
262 switch (index)
264 default:
265 follow = PLACE_MAX;
266 break;
267 case PLACE_RODATA:
268 follow = PLACE_TEXT;
269 break;
270 case PLACE_BSS:
271 follow = PLACE_DATA;
272 break;
273 case PLACE_REL:
274 follow = PLACE_TEXT;
275 break;
276 case PLACE_INTERP:
277 follow = PLACE_TEXT;
278 break;
279 case PLACE_TLS:
280 follow = PLACE_DATA;
281 break;
282 case PLACE_TLS_BSS:
283 follow = PLACE_TLS;
284 if (!this->places_[PLACE_TLS].have_location)
285 follow = PLACE_DATA;
286 break;
288 if (follow != PLACE_MAX && this->places_[follow].have_location)
290 // Set the location of INDEX to the location of FOLLOW. The
291 // location of INDEX will then be incremented by the caller,
292 // so anything in INDEX will continue to be after anything
293 // in FOLLOW.
294 this->places_[index].location = this->places_[follow].location;
295 this->places_[index].have_location = true;
299 *pwhere = &this->places_[index].location;
300 bool ret = this->places_[index].have_location;
302 // The caller will set the location.
303 this->places_[index].have_location = true;
305 return ret;
308 // Return the iterator being used for sections at the very end of the
309 // linker script.
311 Orphan_section_placement::Elements_iterator
312 Orphan_section_placement::last_place() const
314 gold_assert(this->places_[PLACE_LAST].have_location);
315 return this->places_[PLACE_LAST].location;
318 // An element in a SECTIONS clause.
320 class Sections_element
322 public:
323 Sections_element()
326 virtual ~Sections_element()
329 // Return whether an output section is relro.
330 virtual bool
331 is_relro() const
332 { return false; }
334 // Record that an output section is relro.
335 virtual void
336 set_is_relro()
339 // Create any required output sections. The only real
340 // implementation is in Output_section_definition.
341 virtual void
342 create_sections(Layout*)
345 // Add any symbol being defined to the symbol table.
346 virtual void
347 add_symbols_to_table(Symbol_table*)
350 // Finalize symbols and check assertions.
351 virtual void
352 finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
355 // Return the output section name to use for an input file name and
356 // section name. This only real implementation is in
357 // Output_section_definition.
358 virtual const char*
359 output_section_name(const char*, const char*, Output_section***,
360 Script_sections::Section_type*)
361 { return NULL; }
363 // Initialize OSP with an output section.
364 virtual void
365 orphan_section_init(Orphan_section_placement*,
366 Script_sections::Elements_iterator)
369 // Set section addresses. This includes applying assignments if the
370 // the expression is an absolute value.
371 virtual void
372 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
373 uint64_t*)
376 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
377 // this section is constrained, and the input sections do not match,
378 // return the constraint, and set *POSD.
379 virtual Section_constraint
380 check_constraint(Output_section_definition**)
381 { return CONSTRAINT_NONE; }
383 // See if this is the alternate output section for a constrained
384 // output section. If it is, transfer the Output_section and return
385 // true. Otherwise return false.
386 virtual bool
387 alternate_constraint(Output_section_definition*, Section_constraint)
388 { return false; }
390 // Get the list of segments to use for an allocated section when
391 // using a PHDRS clause. If this is an allocated section, return
392 // the Output_section, and set *PHDRS_LIST (the first parameter) to
393 // the list of PHDRS to which it should be attached. If the PHDRS
394 // were not specified, don't change *PHDRS_LIST. When not returning
395 // NULL, set *ORPHAN (the second parameter) according to whether
396 // this is an orphan section--one that is not mentioned in the
397 // linker script.
398 virtual Output_section*
399 allocate_to_segment(String_list**, bool*)
400 { return NULL; }
402 // Look for an output section by name and return the address, the
403 // load address, the alignment, and the size. This is used when an
404 // expression refers to an output section which was not actually
405 // created. This returns true if the section was found, false
406 // otherwise. The only real definition is for
407 // Output_section_definition.
408 virtual bool
409 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
410 uint64_t*) const
411 { return false; }
413 // Return the associated Output_section if there is one.
414 virtual Output_section*
415 get_output_section() const
416 { return NULL; }
418 // Print the element for debugging purposes.
419 virtual void
420 print(FILE* f) const = 0;
423 // An assignment in a SECTIONS clause outside of an output section.
425 class Sections_element_assignment : public Sections_element
427 public:
428 Sections_element_assignment(const char* name, size_t namelen,
429 Expression* val, bool provide, bool hidden)
430 : assignment_(name, namelen, false, val, provide, hidden)
433 // Add the symbol to the symbol table.
434 void
435 add_symbols_to_table(Symbol_table* symtab)
436 { this->assignment_.add_to_table(symtab); }
438 // Finalize the symbol.
439 void
440 finalize_symbols(Symbol_table* symtab, const Layout* layout,
441 uint64_t* dot_value)
443 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
446 // Set the section address. There is no section here, but if the
447 // value is absolute, we set the symbol. This permits us to use
448 // absolute symbols when setting dot.
449 void
450 set_section_addresses(Symbol_table* symtab, Layout* layout,
451 uint64_t* dot_value, uint64_t*, uint64_t*)
453 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
456 // Print for debugging.
457 void
458 print(FILE* f) const
460 fprintf(f, " ");
461 this->assignment_.print(f);
464 private:
465 Symbol_assignment assignment_;
468 // An assignment to the dot symbol in a SECTIONS clause outside of an
469 // output section.
471 class Sections_element_dot_assignment : public Sections_element
473 public:
474 Sections_element_dot_assignment(Expression* val)
475 : val_(val)
478 // Finalize the symbol.
479 void
480 finalize_symbols(Symbol_table* symtab, const Layout* layout,
481 uint64_t* dot_value)
483 // We ignore the section of the result because outside of an
484 // output section definition the dot symbol is always considered
485 // to be absolute.
486 Output_section* dummy;
487 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
488 NULL, &dummy, NULL);
491 // Update the dot symbol while setting section addresses.
492 void
493 set_section_addresses(Symbol_table* symtab, Layout* layout,
494 uint64_t* dot_value, uint64_t* dot_alignment,
495 uint64_t* load_address)
497 Output_section* dummy;
498 *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
499 NULL, &dummy, dot_alignment);
500 *load_address = *dot_value;
503 // Print for debugging.
504 void
505 print(FILE* f) const
507 fprintf(f, " . = ");
508 this->val_->print(f);
509 fprintf(f, "\n");
512 private:
513 Expression* val_;
516 // An assertion in a SECTIONS clause outside of an output section.
518 class Sections_element_assertion : public Sections_element
520 public:
521 Sections_element_assertion(Expression* check, const char* message,
522 size_t messagelen)
523 : assertion_(check, message, messagelen)
526 // Check the assertion.
527 void
528 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
529 { this->assertion_.check(symtab, layout); }
531 // Print for debugging.
532 void
533 print(FILE* f) const
535 fprintf(f, " ");
536 this->assertion_.print(f);
539 private:
540 Script_assertion assertion_;
543 // An element in an output section in a SECTIONS clause.
545 class Output_section_element
547 public:
548 // A list of input sections.
549 typedef std::list<Output_section::Input_section> Input_section_list;
551 Output_section_element()
554 virtual ~Output_section_element()
557 // Return whether this element requires an output section to exist.
558 virtual bool
559 needs_output_section() const
560 { return false; }
562 // Add any symbol being defined to the symbol table.
563 virtual void
564 add_symbols_to_table(Symbol_table*)
567 // Finalize symbols and check assertions.
568 virtual void
569 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
572 // Return whether this element matches FILE_NAME and SECTION_NAME.
573 // The only real implementation is in Output_section_element_input.
574 virtual bool
575 match_name(const char*, const char*) const
576 { return false; }
578 // Set section addresses. This includes applying assignments if the
579 // the expression is an absolute value.
580 virtual void
581 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
582 uint64_t*, uint64_t*, Output_section**, std::string*,
583 Input_section_list*)
586 // Print the element for debugging purposes.
587 virtual void
588 print(FILE* f) const = 0;
590 protected:
591 // Return a fill string that is LENGTH bytes long, filling it with
592 // FILL.
593 std::string
594 get_fill_string(const std::string* fill, section_size_type length) const;
597 std::string
598 Output_section_element::get_fill_string(const std::string* fill,
599 section_size_type length) const
601 std::string this_fill;
602 this_fill.reserve(length);
603 while (this_fill.length() + fill->length() <= length)
604 this_fill += *fill;
605 if (this_fill.length() < length)
606 this_fill.append(*fill, 0, length - this_fill.length());
607 return this_fill;
610 // A symbol assignment in an output section.
612 class Output_section_element_assignment : public Output_section_element
614 public:
615 Output_section_element_assignment(const char* name, size_t namelen,
616 Expression* val, bool provide,
617 bool hidden)
618 : assignment_(name, namelen, false, val, provide, hidden)
621 // Add the symbol to the symbol table.
622 void
623 add_symbols_to_table(Symbol_table* symtab)
624 { this->assignment_.add_to_table(symtab); }
626 // Finalize the symbol.
627 void
628 finalize_symbols(Symbol_table* symtab, const Layout* layout,
629 uint64_t* dot_value, Output_section** dot_section)
631 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
632 *dot_section);
635 // Set the section address. There is no section here, but if the
636 // value is absolute, we set the symbol. This permits us to use
637 // absolute symbols when setting dot.
638 void
639 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
640 uint64_t, uint64_t* dot_value, uint64_t*,
641 Output_section**, std::string*, Input_section_list*)
643 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
646 // Print for debugging.
647 void
648 print(FILE* f) const
650 fprintf(f, " ");
651 this->assignment_.print(f);
654 private:
655 Symbol_assignment assignment_;
658 // An assignment to the dot symbol in an output section.
660 class Output_section_element_dot_assignment : public Output_section_element
662 public:
663 Output_section_element_dot_assignment(Expression* val)
664 : val_(val)
667 // Finalize the symbol.
668 void
669 finalize_symbols(Symbol_table* symtab, const Layout* layout,
670 uint64_t* dot_value, Output_section** dot_section)
672 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
673 *dot_section, dot_section, NULL);
676 // Update the dot symbol while setting section addresses.
677 void
678 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
679 uint64_t, uint64_t* dot_value, uint64_t*,
680 Output_section**, std::string*, Input_section_list*);
682 // Print for debugging.
683 void
684 print(FILE* f) const
686 fprintf(f, " . = ");
687 this->val_->print(f);
688 fprintf(f, "\n");
691 private:
692 Expression* val_;
695 // Update the dot symbol while setting section addresses.
697 void
698 Output_section_element_dot_assignment::set_section_addresses(
699 Symbol_table* symtab,
700 Layout* layout,
701 Output_section* output_section,
702 uint64_t,
703 uint64_t* dot_value,
704 uint64_t* dot_alignment,
705 Output_section** dot_section,
706 std::string* fill,
707 Input_section_list*)
709 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
710 *dot_value, *dot_section,
711 dot_section, dot_alignment);
712 if (next_dot < *dot_value)
713 gold_error(_("dot may not move backward"));
714 if (next_dot > *dot_value && output_section != NULL)
716 section_size_type length = convert_to_section_size_type(next_dot
717 - *dot_value);
718 Output_section_data* posd;
719 if (fill->empty())
720 posd = new Output_data_zero_fill(length, 0);
721 else
723 std::string this_fill = this->get_fill_string(fill, length);
724 posd = new Output_data_const(this_fill, 0);
726 output_section->add_output_section_data(posd);
727 layout->new_output_section_data_from_script(posd);
729 *dot_value = next_dot;
732 // An assertion in an output section.
734 class Output_section_element_assertion : public Output_section_element
736 public:
737 Output_section_element_assertion(Expression* check, const char* message,
738 size_t messagelen)
739 : assertion_(check, message, messagelen)
742 void
743 print(FILE* f) const
745 fprintf(f, " ");
746 this->assertion_.print(f);
749 private:
750 Script_assertion assertion_;
753 // We use a special instance of Output_section_data to handle BYTE,
754 // SHORT, etc. This permits forward references to symbols in the
755 // expressions.
757 class Output_data_expression : public Output_section_data
759 public:
760 Output_data_expression(int size, bool is_signed, Expression* val,
761 const Symbol_table* symtab, const Layout* layout,
762 uint64_t dot_value, Output_section* dot_section)
763 : Output_section_data(size, 0, true),
764 is_signed_(is_signed), val_(val), symtab_(symtab),
765 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
768 protected:
769 // Write the data to the output file.
770 void
771 do_write(Output_file*);
773 // Write the data to a buffer.
774 void
775 do_write_to_buffer(unsigned char*);
777 // Write to a map file.
778 void
779 do_print_to_mapfile(Mapfile* mapfile) const
780 { mapfile->print_output_data(this, _("** expression")); }
782 private:
783 template<bool big_endian>
784 void
785 endian_write_to_buffer(uint64_t, unsigned char*);
787 bool is_signed_;
788 Expression* val_;
789 const Symbol_table* symtab_;
790 const Layout* layout_;
791 uint64_t dot_value_;
792 Output_section* dot_section_;
795 // Write the data element to the output file.
797 void
798 Output_data_expression::do_write(Output_file* of)
800 unsigned char* view = of->get_output_view(this->offset(), this->data_size());
801 this->write_to_buffer(view);
802 of->write_output_view(this->offset(), this->data_size(), view);
805 // Write the data element to a buffer.
807 void
808 Output_data_expression::do_write_to_buffer(unsigned char* buf)
810 Output_section* dummy;
811 uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
812 true, this->dot_value_,
813 this->dot_section_, &dummy, NULL);
815 if (parameters->target().is_big_endian())
816 this->endian_write_to_buffer<true>(val, buf);
817 else
818 this->endian_write_to_buffer<false>(val, buf);
821 template<bool big_endian>
822 void
823 Output_data_expression::endian_write_to_buffer(uint64_t val,
824 unsigned char* buf)
826 switch (this->data_size())
828 case 1:
829 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
830 break;
831 case 2:
832 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
833 break;
834 case 4:
835 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
836 break;
837 case 8:
838 if (parameters->target().get_size() == 32)
840 val &= 0xffffffff;
841 if (this->is_signed_ && (val & 0x80000000) != 0)
842 val |= 0xffffffff00000000LL;
844 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
845 break;
846 default:
847 gold_unreachable();
851 // A data item in an output section.
853 class Output_section_element_data : public Output_section_element
855 public:
856 Output_section_element_data(int size, bool is_signed, Expression* val)
857 : size_(size), is_signed_(is_signed), val_(val)
860 // If there is a data item, then we must create an output section.
861 bool
862 needs_output_section() const
863 { return true; }
865 // Finalize symbols--we just need to update dot.
866 void
867 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
868 Output_section**)
869 { *dot_value += this->size_; }
871 // Store the value in the section.
872 void
873 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
874 uint64_t* dot_value, uint64_t*, Output_section**,
875 std::string*, Input_section_list*);
877 // Print for debugging.
878 void
879 print(FILE*) const;
881 private:
882 // The size in bytes.
883 int size_;
884 // Whether the value is signed.
885 bool is_signed_;
886 // The value.
887 Expression* val_;
890 // Store the value in the section.
892 void
893 Output_section_element_data::set_section_addresses(
894 Symbol_table* symtab,
895 Layout* layout,
896 Output_section* os,
897 uint64_t,
898 uint64_t* dot_value,
899 uint64_t*,
900 Output_section** dot_section,
901 std::string*,
902 Input_section_list*)
904 gold_assert(os != NULL);
905 Output_data_expression* expression =
906 new Output_data_expression(this->size_, this->is_signed_, this->val_,
907 symtab, layout, *dot_value, *dot_section);
908 os->add_output_section_data(expression);
909 layout->new_output_section_data_from_script(expression);
910 *dot_value += this->size_;
913 // Print for debugging.
915 void
916 Output_section_element_data::print(FILE* f) const
918 const char* s;
919 switch (this->size_)
921 case 1:
922 s = "BYTE";
923 break;
924 case 2:
925 s = "SHORT";
926 break;
927 case 4:
928 s = "LONG";
929 break;
930 case 8:
931 if (this->is_signed_)
932 s = "SQUAD";
933 else
934 s = "QUAD";
935 break;
936 default:
937 gold_unreachable();
939 fprintf(f, " %s(", s);
940 this->val_->print(f);
941 fprintf(f, ")\n");
944 // A fill value setting in an output section.
946 class Output_section_element_fill : public Output_section_element
948 public:
949 Output_section_element_fill(Expression* val)
950 : val_(val)
953 // Update the fill value while setting section addresses.
954 void
955 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
956 uint64_t, uint64_t* dot_value, uint64_t*,
957 Output_section** dot_section,
958 std::string* fill, Input_section_list*)
960 Output_section* fill_section;
961 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
962 *dot_value, *dot_section,
963 &fill_section, NULL);
964 if (fill_section != NULL)
965 gold_warning(_("fill value is not absolute"));
966 // FIXME: The GNU linker supports fill values of arbitrary length.
967 unsigned char fill_buff[4];
968 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
969 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
972 // Print for debugging.
973 void
974 print(FILE* f) const
976 fprintf(f, " FILL(");
977 this->val_->print(f);
978 fprintf(f, ")\n");
981 private:
982 // The new fill value.
983 Expression* val_;
986 // An input section specification in an output section
988 class Output_section_element_input : public Output_section_element
990 public:
991 Output_section_element_input(const Input_section_spec* spec, bool keep);
993 // Finalize symbols--just update the value of the dot symbol.
994 void
995 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
996 Output_section** dot_section)
998 *dot_value = this->final_dot_value_;
999 *dot_section = this->final_dot_section_;
1002 // See whether we match FILE_NAME and SECTION_NAME as an input
1003 // section.
1004 bool
1005 match_name(const char* file_name, const char* section_name) const;
1007 // Set the section address.
1008 void
1009 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1010 uint64_t subalign, uint64_t* dot_value, uint64_t*,
1011 Output_section**, std::string* fill,
1012 Input_section_list*);
1014 // Print for debugging.
1015 void
1016 print(FILE* f) const;
1018 private:
1019 // An input section pattern.
1020 struct Input_section_pattern
1022 std::string pattern;
1023 bool pattern_is_wildcard;
1024 Sort_wildcard sort;
1026 Input_section_pattern(const char* patterna, size_t patternlena,
1027 Sort_wildcard sorta)
1028 : pattern(patterna, patternlena),
1029 pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
1030 sort(sorta)
1034 typedef std::vector<Input_section_pattern> Input_section_patterns;
1036 // Filename_exclusions is a pair of filename pattern and a bool
1037 // indicating whether the filename is a wildcard.
1038 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1040 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1041 // indicates whether this is a wildcard pattern.
1042 static inline bool
1043 match(const char* string, const char* pattern, bool is_wildcard_pattern)
1045 return (is_wildcard_pattern
1046 ? fnmatch(pattern, string, 0) == 0
1047 : strcmp(string, pattern) == 0);
1050 // See if we match a file name.
1051 bool
1052 match_file_name(const char* file_name) const;
1054 // The file name pattern. If this is the empty string, we match all
1055 // files.
1056 std::string filename_pattern_;
1057 // Whether the file name pattern is a wildcard.
1058 bool filename_is_wildcard_;
1059 // How the file names should be sorted. This may only be
1060 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1061 Sort_wildcard filename_sort_;
1062 // The list of file names to exclude.
1063 Filename_exclusions filename_exclusions_;
1064 // The list of input section patterns.
1065 Input_section_patterns input_section_patterns_;
1066 // Whether to keep this section when garbage collecting.
1067 bool keep_;
1068 // The value of dot after including all matching sections.
1069 uint64_t final_dot_value_;
1070 // The section where dot is defined after including all matching
1071 // sections.
1072 Output_section* final_dot_section_;
1075 // Construct Output_section_element_input. The parser records strings
1076 // as pointers into a copy of the script file, which will go away when
1077 // parsing is complete. We make sure they are in std::string objects.
1079 Output_section_element_input::Output_section_element_input(
1080 const Input_section_spec* spec,
1081 bool keep)
1082 : filename_pattern_(),
1083 filename_is_wildcard_(false),
1084 filename_sort_(spec->file.sort),
1085 filename_exclusions_(),
1086 input_section_patterns_(),
1087 keep_(keep),
1088 final_dot_value_(0),
1089 final_dot_section_(NULL)
1091 // The filename pattern "*" is common, and matches all files. Turn
1092 // it into the empty string.
1093 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1094 this->filename_pattern_.assign(spec->file.name.value,
1095 spec->file.name.length);
1096 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
1098 if (spec->input_sections.exclude != NULL)
1100 for (String_list::const_iterator p =
1101 spec->input_sections.exclude->begin();
1102 p != spec->input_sections.exclude->end();
1103 ++p)
1105 bool is_wildcard = is_wildcard_string((*p).c_str());
1106 this->filename_exclusions_.push_back(std::make_pair(*p,
1107 is_wildcard));
1111 if (spec->input_sections.sections != NULL)
1113 Input_section_patterns& isp(this->input_section_patterns_);
1114 for (String_sort_list::const_iterator p =
1115 spec->input_sections.sections->begin();
1116 p != spec->input_sections.sections->end();
1117 ++p)
1118 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1119 p->sort));
1123 // See whether we match FILE_NAME.
1125 bool
1126 Output_section_element_input::match_file_name(const char* file_name) const
1128 if (!this->filename_pattern_.empty())
1130 // If we were called with no filename, we refuse to match a
1131 // pattern which requires a file name.
1132 if (file_name == NULL)
1133 return false;
1135 if (!match(file_name, this->filename_pattern_.c_str(),
1136 this->filename_is_wildcard_))
1137 return false;
1140 if (file_name != NULL)
1142 // Now we have to see whether FILE_NAME matches one of the
1143 // exclusion patterns, if any.
1144 for (Filename_exclusions::const_iterator p =
1145 this->filename_exclusions_.begin();
1146 p != this->filename_exclusions_.end();
1147 ++p)
1149 if (match(file_name, p->first.c_str(), p->second))
1150 return false;
1154 return true;
1157 // See whether we match FILE_NAME and SECTION_NAME.
1159 bool
1160 Output_section_element_input::match_name(const char* file_name,
1161 const char* section_name) const
1163 if (!this->match_file_name(file_name))
1164 return false;
1166 // If there are no section name patterns, then we match.
1167 if (this->input_section_patterns_.empty())
1168 return true;
1170 // See whether we match the section name patterns.
1171 for (Input_section_patterns::const_iterator p =
1172 this->input_section_patterns_.begin();
1173 p != this->input_section_patterns_.end();
1174 ++p)
1176 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1177 return true;
1180 // We didn't match any section names, so we didn't match.
1181 return false;
1184 // Information we use to sort the input sections.
1186 class Input_section_info
1188 public:
1189 Input_section_info(const Output_section::Input_section& input_section)
1190 : input_section_(input_section), section_name_(),
1191 size_(0), addralign_(1)
1194 // Return the simple input section.
1195 const Output_section::Input_section&
1196 input_section() const
1197 { return this->input_section_; }
1199 // Return the object.
1200 Relobj*
1201 relobj() const
1202 { return this->input_section_.relobj(); }
1204 // Return the section index.
1205 unsigned int
1206 shndx()
1207 { return this->input_section_.shndx(); }
1209 // Return the section name.
1210 const std::string&
1211 section_name() const
1212 { return this->section_name_; }
1214 // Set the section name.
1215 void
1216 set_section_name(const std::string name)
1217 { this->section_name_ = name; }
1219 // Return the section size.
1220 uint64_t
1221 size() const
1222 { return this->size_; }
1224 // Set the section size.
1225 void
1226 set_size(uint64_t size)
1227 { this->size_ = size; }
1229 // Return the address alignment.
1230 uint64_t
1231 addralign() const
1232 { return this->addralign_; }
1234 // Set the address alignment.
1235 void
1236 set_addralign(uint64_t addralign)
1237 { this->addralign_ = addralign; }
1239 private:
1240 // Input section, can be a relaxed section.
1241 Output_section::Input_section input_section_;
1242 // Name of the section.
1243 std::string section_name_;
1244 // Section size.
1245 uint64_t size_;
1246 // Address alignment.
1247 uint64_t addralign_;
1250 // A class to sort the input sections.
1252 class Input_section_sorter
1254 public:
1255 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1256 : filename_sort_(filename_sort), section_sort_(section_sort)
1259 bool
1260 operator()(const Input_section_info&, const Input_section_info&) const;
1262 private:
1263 Sort_wildcard filename_sort_;
1264 Sort_wildcard section_sort_;
1267 bool
1268 Input_section_sorter::operator()(const Input_section_info& isi1,
1269 const Input_section_info& isi2) const
1271 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1272 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1273 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1274 && isi1.addralign() == isi2.addralign()))
1276 if (isi1.section_name() != isi2.section_name())
1277 return isi1.section_name() < isi2.section_name();
1279 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1280 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1281 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1283 if (isi1.addralign() != isi2.addralign())
1284 return isi1.addralign() < isi2.addralign();
1286 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1288 if (isi1.relobj()->name() != isi2.relobj()->name())
1289 return (isi1.relobj()->name() < isi2.relobj()->name());
1292 // Otherwise we leave them in the same order.
1293 return false;
1296 // Set the section address. Look in INPUT_SECTIONS for sections which
1297 // match this spec, sort them as specified, and add them to the output
1298 // section.
1300 void
1301 Output_section_element_input::set_section_addresses(
1302 Symbol_table*,
1303 Layout* layout,
1304 Output_section* output_section,
1305 uint64_t subalign,
1306 uint64_t* dot_value,
1307 uint64_t*,
1308 Output_section** dot_section,
1309 std::string* fill,
1310 Input_section_list* input_sections)
1312 // We build a list of sections which match each
1313 // Input_section_pattern.
1315 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1316 size_t input_pattern_count = this->input_section_patterns_.size();
1317 if (input_pattern_count == 0)
1318 input_pattern_count = 1;
1319 Matching_sections matching_sections(input_pattern_count);
1321 // Look through the list of sections for this output section. Add
1322 // each one which matches to one of the elements of
1323 // MATCHING_SECTIONS.
1325 Input_section_list::iterator p = input_sections->begin();
1326 while (p != input_sections->end())
1328 Relobj* relobj = p->relobj();
1329 unsigned int shndx = p->shndx();
1330 Input_section_info isi(*p);
1332 // Calling section_name and section_addralign is not very
1333 // efficient.
1335 // Lock the object so that we can get information about the
1336 // section. This is OK since we know we are single-threaded
1337 // here.
1339 const Task* task = reinterpret_cast<const Task*>(-1);
1340 Task_lock_obj<Object> tl(task, relobj);
1342 isi.set_section_name(relobj->section_name(shndx));
1343 if (p->is_relaxed_input_section())
1345 // We use current data size because relxed section sizes may not
1346 // have finalized yet.
1347 isi.set_size(p->relaxed_input_section()->current_data_size());
1348 isi.set_addralign(p->relaxed_input_section()->addralign());
1350 else
1352 isi.set_size(relobj->section_size(shndx));
1353 isi.set_addralign(relobj->section_addralign(shndx));
1357 if (!this->match_file_name(relobj->name().c_str()))
1358 ++p;
1359 else if (this->input_section_patterns_.empty())
1361 matching_sections[0].push_back(isi);
1362 p = input_sections->erase(p);
1364 else
1366 size_t i;
1367 for (i = 0; i < input_pattern_count; ++i)
1369 const Input_section_pattern&
1370 isp(this->input_section_patterns_[i]);
1371 if (match(isi.section_name().c_str(), isp.pattern.c_str(),
1372 isp.pattern_is_wildcard))
1373 break;
1376 if (i >= this->input_section_patterns_.size())
1377 ++p;
1378 else
1380 matching_sections[i].push_back(isi);
1381 p = input_sections->erase(p);
1386 // Look through MATCHING_SECTIONS. Sort each one as specified,
1387 // using a stable sort so that we get the default order when
1388 // sections are otherwise equal. Add each input section to the
1389 // output section.
1391 uint64_t dot = *dot_value;
1392 for (size_t i = 0; i < input_pattern_count; ++i)
1394 if (matching_sections[i].empty())
1395 continue;
1397 gold_assert(output_section != NULL);
1399 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1400 if (isp.sort != SORT_WILDCARD_NONE
1401 || this->filename_sort_ != SORT_WILDCARD_NONE)
1402 std::stable_sort(matching_sections[i].begin(),
1403 matching_sections[i].end(),
1404 Input_section_sorter(this->filename_sort_,
1405 isp.sort));
1407 for (std::vector<Input_section_info>::const_iterator p =
1408 matching_sections[i].begin();
1409 p != matching_sections[i].end();
1410 ++p)
1412 // Override the original address alignment if SUBALIGN is specified
1413 // and is greater than the original alignment. We need to make a
1414 // copy of the input section to modify the alignment.
1415 Output_section::Input_section sis(p->input_section());
1417 uint64_t this_subalign = sis.addralign();
1418 if (!sis.is_input_section())
1419 sis.output_section_data()->finalize_data_size();
1420 uint64_t data_size = sis.data_size();
1421 if (this_subalign < subalign)
1423 this_subalign = subalign;
1424 sis.set_addralign(subalign);
1427 uint64_t address = align_address(dot, this_subalign);
1429 if (address > dot && !fill->empty())
1431 section_size_type length =
1432 convert_to_section_size_type(address - dot);
1433 std::string this_fill = this->get_fill_string(fill, length);
1434 Output_section_data* posd = new Output_data_const(this_fill, 0);
1435 output_section->add_output_section_data(posd);
1436 layout->new_output_section_data_from_script(posd);
1439 output_section->add_script_input_section(sis);
1440 dot = address + data_size;
1444 // An SHF_TLS/SHT_NOBITS section does not take up any
1445 // address space.
1446 if (output_section == NULL
1447 || (output_section->flags() & elfcpp::SHF_TLS) == 0
1448 || output_section->type() != elfcpp::SHT_NOBITS)
1449 *dot_value = dot;
1451 this->final_dot_value_ = *dot_value;
1452 this->final_dot_section_ = *dot_section;
1455 // Print for debugging.
1457 void
1458 Output_section_element_input::print(FILE* f) const
1460 fprintf(f, " ");
1462 if (this->keep_)
1463 fprintf(f, "KEEP(");
1465 if (!this->filename_pattern_.empty())
1467 bool need_close_paren = false;
1468 switch (this->filename_sort_)
1470 case SORT_WILDCARD_NONE:
1471 break;
1472 case SORT_WILDCARD_BY_NAME:
1473 fprintf(f, "SORT_BY_NAME(");
1474 need_close_paren = true;
1475 break;
1476 default:
1477 gold_unreachable();
1480 fprintf(f, "%s", this->filename_pattern_.c_str());
1482 if (need_close_paren)
1483 fprintf(f, ")");
1486 if (!this->input_section_patterns_.empty()
1487 || !this->filename_exclusions_.empty())
1489 fprintf(f, "(");
1491 bool need_space = false;
1492 if (!this->filename_exclusions_.empty())
1494 fprintf(f, "EXCLUDE_FILE(");
1495 bool need_comma = false;
1496 for (Filename_exclusions::const_iterator p =
1497 this->filename_exclusions_.begin();
1498 p != this->filename_exclusions_.end();
1499 ++p)
1501 if (need_comma)
1502 fprintf(f, ", ");
1503 fprintf(f, "%s", p->first.c_str());
1504 need_comma = true;
1506 fprintf(f, ")");
1507 need_space = true;
1510 for (Input_section_patterns::const_iterator p =
1511 this->input_section_patterns_.begin();
1512 p != this->input_section_patterns_.end();
1513 ++p)
1515 if (need_space)
1516 fprintf(f, " ");
1518 int close_parens = 0;
1519 switch (p->sort)
1521 case SORT_WILDCARD_NONE:
1522 break;
1523 case SORT_WILDCARD_BY_NAME:
1524 fprintf(f, "SORT_BY_NAME(");
1525 close_parens = 1;
1526 break;
1527 case SORT_WILDCARD_BY_ALIGNMENT:
1528 fprintf(f, "SORT_BY_ALIGNMENT(");
1529 close_parens = 1;
1530 break;
1531 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1532 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1533 close_parens = 2;
1534 break;
1535 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1536 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1537 close_parens = 2;
1538 break;
1539 default:
1540 gold_unreachable();
1543 fprintf(f, "%s", p->pattern.c_str());
1545 for (int i = 0; i < close_parens; ++i)
1546 fprintf(f, ")");
1548 need_space = true;
1551 fprintf(f, ")");
1554 if (this->keep_)
1555 fprintf(f, ")");
1557 fprintf(f, "\n");
1560 // An output section.
1562 class Output_section_definition : public Sections_element
1564 public:
1565 typedef Output_section_element::Input_section_list Input_section_list;
1567 Output_section_definition(const char* name, size_t namelen,
1568 const Parser_output_section_header* header);
1570 // Finish the output section with the information in the trailer.
1571 void
1572 finish(const Parser_output_section_trailer* trailer);
1574 // Add a symbol to be defined.
1575 void
1576 add_symbol_assignment(const char* name, size_t length, Expression* value,
1577 bool provide, bool hidden);
1579 // Add an assignment to the special dot symbol.
1580 void
1581 add_dot_assignment(Expression* value);
1583 // Add an assertion.
1584 void
1585 add_assertion(Expression* check, const char* message, size_t messagelen);
1587 // Add a data item to the current output section.
1588 void
1589 add_data(int size, bool is_signed, Expression* val);
1591 // Add a setting for the fill value.
1592 void
1593 add_fill(Expression* val);
1595 // Add an input section specification.
1596 void
1597 add_input_section(const Input_section_spec* spec, bool keep);
1599 // Return whether the output section is relro.
1600 bool
1601 is_relro() const
1602 { return this->is_relro_; }
1604 // Record that the output section is relro.
1605 void
1606 set_is_relro()
1607 { this->is_relro_ = true; }
1609 // Create any required output sections.
1610 void
1611 create_sections(Layout*);
1613 // Add any symbols being defined to the symbol table.
1614 void
1615 add_symbols_to_table(Symbol_table* symtab);
1617 // Finalize symbols and check assertions.
1618 void
1619 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
1621 // Return the output section name to use for an input file name and
1622 // section name.
1623 const char*
1624 output_section_name(const char* file_name, const char* section_name,
1625 Output_section***, Script_sections::Section_type*);
1627 // Initialize OSP with an output section.
1628 void
1629 orphan_section_init(Orphan_section_placement* osp,
1630 Script_sections::Elements_iterator p)
1631 { osp->output_section_init(this->name_, this->output_section_, p); }
1633 // Set the section address.
1634 void
1635 set_section_addresses(Symbol_table* symtab, Layout* layout,
1636 uint64_t* dot_value, uint64_t*,
1637 uint64_t* load_address);
1639 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1640 // this section is constrained, and the input sections do not match,
1641 // return the constraint, and set *POSD.
1642 Section_constraint
1643 check_constraint(Output_section_definition** posd);
1645 // See if this is the alternate output section for a constrained
1646 // output section. If it is, transfer the Output_section and return
1647 // true. Otherwise return false.
1648 bool
1649 alternate_constraint(Output_section_definition*, Section_constraint);
1651 // Get the list of segments to use for an allocated section when
1652 // using a PHDRS clause.
1653 Output_section*
1654 allocate_to_segment(String_list** phdrs_list, bool* orphan);
1656 // Look for an output section by name and return the address, the
1657 // load address, the alignment, and the size. This is used when an
1658 // expression refers to an output section which was not actually
1659 // created. This returns true if the section was found, false
1660 // otherwise.
1661 bool
1662 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1663 uint64_t*) const;
1665 // Return the associated Output_section if there is one.
1666 Output_section*
1667 get_output_section() const
1668 { return this->output_section_; }
1670 // Print the contents to the FILE. This is for debugging.
1671 void
1672 print(FILE*) const;
1674 // Return the output section type if specified or Script_sections::ST_NONE.
1675 Script_sections::Section_type
1676 section_type() const;
1678 private:
1679 static const char*
1680 script_section_type_name(Script_section_type);
1682 typedef std::vector<Output_section_element*> Output_section_elements;
1684 // The output section name.
1685 std::string name_;
1686 // The address. This may be NULL.
1687 Expression* address_;
1688 // The load address. This may be NULL.
1689 Expression* load_address_;
1690 // The alignment. This may be NULL.
1691 Expression* align_;
1692 // The input section alignment. This may be NULL.
1693 Expression* subalign_;
1694 // The constraint, if any.
1695 Section_constraint constraint_;
1696 // The fill value. This may be NULL.
1697 Expression* fill_;
1698 // The list of segments this section should go into. This may be
1699 // NULL.
1700 String_list* phdrs_;
1701 // The list of elements defining the section.
1702 Output_section_elements elements_;
1703 // The Output_section created for this definition. This will be
1704 // NULL if none was created.
1705 Output_section* output_section_;
1706 // The address after it has been evaluated.
1707 uint64_t evaluated_address_;
1708 // The load address after it has been evaluated.
1709 uint64_t evaluated_load_address_;
1710 // The alignment after it has been evaluated.
1711 uint64_t evaluated_addralign_;
1712 // The output section is relro.
1713 bool is_relro_;
1714 // The output section type if specified.
1715 enum Script_section_type script_section_type_;
1718 // Constructor.
1720 Output_section_definition::Output_section_definition(
1721 const char* name,
1722 size_t namelen,
1723 const Parser_output_section_header* header)
1724 : name_(name, namelen),
1725 address_(header->address),
1726 load_address_(header->load_address),
1727 align_(header->align),
1728 subalign_(header->subalign),
1729 constraint_(header->constraint),
1730 fill_(NULL),
1731 phdrs_(NULL),
1732 elements_(),
1733 output_section_(NULL),
1734 evaluated_address_(0),
1735 evaluated_load_address_(0),
1736 evaluated_addralign_(0),
1737 is_relro_(false),
1738 script_section_type_(header->section_type)
1742 // Finish an output section.
1744 void
1745 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1747 this->fill_ = trailer->fill;
1748 this->phdrs_ = trailer->phdrs;
1751 // Add a symbol to be defined.
1753 void
1754 Output_section_definition::add_symbol_assignment(const char* name,
1755 size_t length,
1756 Expression* value,
1757 bool provide,
1758 bool hidden)
1760 Output_section_element* p = new Output_section_element_assignment(name,
1761 length,
1762 value,
1763 provide,
1764 hidden);
1765 this->elements_.push_back(p);
1768 // Add an assignment to the special dot symbol.
1770 void
1771 Output_section_definition::add_dot_assignment(Expression* value)
1773 Output_section_element* p = new Output_section_element_dot_assignment(value);
1774 this->elements_.push_back(p);
1777 // Add an assertion.
1779 void
1780 Output_section_definition::add_assertion(Expression* check,
1781 const char* message,
1782 size_t messagelen)
1784 Output_section_element* p = new Output_section_element_assertion(check,
1785 message,
1786 messagelen);
1787 this->elements_.push_back(p);
1790 // Add a data item to the current output section.
1792 void
1793 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1795 Output_section_element* p = new Output_section_element_data(size, is_signed,
1796 val);
1797 this->elements_.push_back(p);
1800 // Add a setting for the fill value.
1802 void
1803 Output_section_definition::add_fill(Expression* val)
1805 Output_section_element* p = new Output_section_element_fill(val);
1806 this->elements_.push_back(p);
1809 // Add an input section specification.
1811 void
1812 Output_section_definition::add_input_section(const Input_section_spec* spec,
1813 bool keep)
1815 Output_section_element* p = new Output_section_element_input(spec, keep);
1816 this->elements_.push_back(p);
1819 // Create any required output sections. We need an output section if
1820 // there is a data statement here.
1822 void
1823 Output_section_definition::create_sections(Layout* layout)
1825 if (this->output_section_ != NULL)
1826 return;
1827 for (Output_section_elements::const_iterator p = this->elements_.begin();
1828 p != this->elements_.end();
1829 ++p)
1831 if ((*p)->needs_output_section())
1833 const char* name = this->name_.c_str();
1834 this->output_section_ =
1835 layout->make_output_section_for_script(name, this->section_type());
1836 return;
1841 // Add any symbols being defined to the symbol table.
1843 void
1844 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
1846 for (Output_section_elements::iterator p = this->elements_.begin();
1847 p != this->elements_.end();
1848 ++p)
1849 (*p)->add_symbols_to_table(symtab);
1852 // Finalize symbols and check assertions.
1854 void
1855 Output_section_definition::finalize_symbols(Symbol_table* symtab,
1856 const Layout* layout,
1857 uint64_t* dot_value)
1859 if (this->output_section_ != NULL)
1860 *dot_value = this->output_section_->address();
1861 else
1863 uint64_t address = *dot_value;
1864 if (this->address_ != NULL)
1866 Output_section* dummy;
1867 address = this->address_->eval_with_dot(symtab, layout, true,
1868 *dot_value, NULL,
1869 &dummy, NULL);
1871 if (this->align_ != NULL)
1873 Output_section* dummy;
1874 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
1875 *dot_value,
1876 NULL,
1877 &dummy, NULL);
1878 address = align_address(address, align);
1880 *dot_value = address;
1883 Output_section* dot_section = this->output_section_;
1884 for (Output_section_elements::iterator p = this->elements_.begin();
1885 p != this->elements_.end();
1886 ++p)
1887 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
1890 // Return the output section name to use for an input section name.
1892 const char*
1893 Output_section_definition::output_section_name(
1894 const char* file_name,
1895 const char* section_name,
1896 Output_section*** slot,
1897 Script_sections::Section_type* psection_type)
1899 // Ask each element whether it matches NAME.
1900 for (Output_section_elements::const_iterator p = this->elements_.begin();
1901 p != this->elements_.end();
1902 ++p)
1904 if ((*p)->match_name(file_name, section_name))
1906 // We found a match for NAME, which means that it should go
1907 // into this output section.
1908 *slot = &this->output_section_;
1909 *psection_type = this->section_type();
1910 return this->name_.c_str();
1914 // We don't know about this section name.
1915 return NULL;
1918 // Set the section address. Note that the OUTPUT_SECTION_ field will
1919 // be NULL if no input sections were mapped to this output section.
1920 // We still have to adjust dot and process symbol assignments.
1922 void
1923 Output_section_definition::set_section_addresses(Symbol_table* symtab,
1924 Layout* layout,
1925 uint64_t* dot_value,
1926 uint64_t* dot_alignment,
1927 uint64_t* load_address)
1929 uint64_t address;
1930 uint64_t old_dot_value = *dot_value;
1931 uint64_t old_load_address = *load_address;
1933 // Check for --section-start.
1934 bool is_address_set = false;
1935 if (this->output_section_ != NULL)
1936 is_address_set =
1937 parameters->options().section_start(this->output_section_->name(),
1938 &address);
1939 if (!is_address_set)
1941 if (this->address_ == NULL)
1942 address = *dot_value;
1943 else
1945 Output_section* dummy;
1946 address = this->address_->eval_with_dot(symtab, layout, true,
1947 *dot_value, NULL, &dummy,
1948 dot_alignment);
1952 uint64_t align;
1953 if (this->align_ == NULL)
1955 if (this->output_section_ == NULL)
1956 align = 0;
1957 else
1958 align = this->output_section_->addralign();
1960 else
1962 Output_section* align_section;
1963 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
1964 NULL, &align_section, NULL);
1965 if (align_section != NULL)
1966 gold_warning(_("alignment of section %s is not absolute"),
1967 this->name_.c_str());
1968 if (this->output_section_ != NULL)
1969 this->output_section_->set_addralign(align);
1972 address = align_address(address, align);
1974 uint64_t start_address = address;
1976 *dot_value = address;
1978 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
1979 // forced to zero, regardless of what the linker script wants.
1980 if (this->output_section_ != NULL
1981 && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
1982 || this->output_section_->is_noload()))
1983 this->output_section_->set_address(address);
1985 this->evaluated_address_ = address;
1986 this->evaluated_addralign_ = align;
1988 if (this->load_address_ == NULL)
1989 this->evaluated_load_address_ = address;
1990 else
1992 Output_section* dummy;
1993 uint64_t laddr =
1994 this->load_address_->eval_with_dot(symtab, layout, true, *dot_value,
1995 this->output_section_, &dummy,
1996 NULL);
1997 if (this->output_section_ != NULL)
1998 this->output_section_->set_load_address(laddr);
1999 this->evaluated_load_address_ = laddr;
2002 uint64_t subalign;
2003 if (this->subalign_ == NULL)
2004 subalign = 0;
2005 else
2007 Output_section* subalign_section;
2008 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2009 *dot_value, NULL,
2010 &subalign_section, NULL);
2011 if (subalign_section != NULL)
2012 gold_warning(_("subalign of section %s is not absolute"),
2013 this->name_.c_str());
2016 std::string fill;
2017 if (this->fill_ != NULL)
2019 // FIXME: The GNU linker supports fill values of arbitrary
2020 // length.
2021 Output_section* fill_section;
2022 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
2023 *dot_value,
2024 NULL, &fill_section,
2025 NULL);
2026 if (fill_section != NULL)
2027 gold_warning(_("fill of section %s is not absolute"),
2028 this->name_.c_str());
2029 unsigned char fill_buff[4];
2030 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2031 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2034 Input_section_list input_sections;
2035 if (this->output_section_ != NULL)
2037 // Get the list of input sections attached to this output
2038 // section. This will leave the output section with only
2039 // Output_section_data entries.
2040 address += this->output_section_->get_input_sections(address,
2041 fill,
2042 &input_sections);
2043 *dot_value = address;
2046 Output_section* dot_section = this->output_section_;
2047 for (Output_section_elements::iterator p = this->elements_.begin();
2048 p != this->elements_.end();
2049 ++p)
2050 (*p)->set_section_addresses(symtab, layout, this->output_section_,
2051 subalign, dot_value, dot_alignment,
2052 &dot_section, &fill, &input_sections);
2054 gold_assert(input_sections.empty());
2056 if (this->load_address_ == NULL || this->output_section_ == NULL)
2057 *load_address = *dot_value;
2058 else
2059 *load_address = (this->output_section_->load_address()
2060 + (*dot_value - start_address));
2062 if (this->output_section_ != NULL)
2064 if (this->is_relro_)
2065 this->output_section_->set_is_relro();
2066 else
2067 this->output_section_->clear_is_relro();
2069 // If this is a NOLOAD section, keep dot and load address unchanged.
2070 if (this->output_section_->is_noload())
2072 *dot_value = old_dot_value;
2073 *load_address = old_load_address;
2078 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2079 // this section is constrained, and the input sections do not match,
2080 // return the constraint, and set *POSD.
2082 Section_constraint
2083 Output_section_definition::check_constraint(Output_section_definition** posd)
2085 switch (this->constraint_)
2087 case CONSTRAINT_NONE:
2088 return CONSTRAINT_NONE;
2090 case CONSTRAINT_ONLY_IF_RO:
2091 if (this->output_section_ != NULL
2092 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2094 *posd = this;
2095 return CONSTRAINT_ONLY_IF_RO;
2097 return CONSTRAINT_NONE;
2099 case CONSTRAINT_ONLY_IF_RW:
2100 if (this->output_section_ != NULL
2101 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2103 *posd = this;
2104 return CONSTRAINT_ONLY_IF_RW;
2106 return CONSTRAINT_NONE;
2108 case CONSTRAINT_SPECIAL:
2109 if (this->output_section_ != NULL)
2110 gold_error(_("SPECIAL constraints are not implemented"));
2111 return CONSTRAINT_NONE;
2113 default:
2114 gold_unreachable();
2118 // See if this is the alternate output section for a constrained
2119 // output section. If it is, transfer the Output_section and return
2120 // true. Otherwise return false.
2122 bool
2123 Output_section_definition::alternate_constraint(
2124 Output_section_definition* posd,
2125 Section_constraint constraint)
2127 if (this->name_ != posd->name_)
2128 return false;
2130 switch (constraint)
2132 case CONSTRAINT_ONLY_IF_RO:
2133 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2134 return false;
2135 break;
2137 case CONSTRAINT_ONLY_IF_RW:
2138 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2139 return false;
2140 break;
2142 default:
2143 gold_unreachable();
2146 // We have found the alternate constraint. We just need to move
2147 // over the Output_section. When constraints are used properly,
2148 // THIS should not have an output_section pointer, as all the input
2149 // sections should have matched the other definition.
2151 if (this->output_section_ != NULL)
2152 gold_error(_("mismatched definition for constrained sections"));
2154 this->output_section_ = posd->output_section_;
2155 posd->output_section_ = NULL;
2157 if (this->is_relro_)
2158 this->output_section_->set_is_relro();
2159 else
2160 this->output_section_->clear_is_relro();
2162 return true;
2165 // Get the list of segments to use for an allocated section when using
2166 // a PHDRS clause.
2168 Output_section*
2169 Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2170 bool* orphan)
2172 // Update phdrs_list even if we don't have an output section. It
2173 // might be used by the following sections.
2174 if (this->phdrs_ != NULL)
2175 *phdrs_list = this->phdrs_;
2177 if (this->output_section_ == NULL)
2178 return NULL;
2179 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2180 return NULL;
2181 *orphan = false;
2182 return this->output_section_;
2185 // Look for an output section by name and return the address, the load
2186 // address, the alignment, and the size. This is used when an
2187 // expression refers to an output section which was not actually
2188 // created. This returns true if the section was found, false
2189 // otherwise.
2191 bool
2192 Output_section_definition::get_output_section_info(const char* name,
2193 uint64_t* address,
2194 uint64_t* load_address,
2195 uint64_t* addralign,
2196 uint64_t* size) const
2198 if (this->name_ != name)
2199 return false;
2201 if (this->output_section_ != NULL)
2203 *address = this->output_section_->address();
2204 if (this->output_section_->has_load_address())
2205 *load_address = this->output_section_->load_address();
2206 else
2207 *load_address = *address;
2208 *addralign = this->output_section_->addralign();
2209 *size = this->output_section_->current_data_size();
2211 else
2213 *address = this->evaluated_address_;
2214 *load_address = this->evaluated_load_address_;
2215 *addralign = this->evaluated_addralign_;
2216 *size = 0;
2219 return true;
2222 // Print for debugging.
2224 void
2225 Output_section_definition::print(FILE* f) const
2227 fprintf(f, " %s ", this->name_.c_str());
2229 if (this->address_ != NULL)
2231 this->address_->print(f);
2232 fprintf(f, " ");
2235 if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2236 fprintf(f, "(%s) ",
2237 this->script_section_type_name(this->script_section_type_));
2239 fprintf(f, ": ");
2241 if (this->load_address_ != NULL)
2243 fprintf(f, "AT(");
2244 this->load_address_->print(f);
2245 fprintf(f, ") ");
2248 if (this->align_ != NULL)
2250 fprintf(f, "ALIGN(");
2251 this->align_->print(f);
2252 fprintf(f, ") ");
2255 if (this->subalign_ != NULL)
2257 fprintf(f, "SUBALIGN(");
2258 this->subalign_->print(f);
2259 fprintf(f, ") ");
2262 fprintf(f, "{\n");
2264 for (Output_section_elements::const_iterator p = this->elements_.begin();
2265 p != this->elements_.end();
2266 ++p)
2267 (*p)->print(f);
2269 fprintf(f, " }");
2271 if (this->fill_ != NULL)
2273 fprintf(f, " = ");
2274 this->fill_->print(f);
2277 if (this->phdrs_ != NULL)
2279 for (String_list::const_iterator p = this->phdrs_->begin();
2280 p != this->phdrs_->end();
2281 ++p)
2282 fprintf(f, " :%s", p->c_str());
2285 fprintf(f, "\n");
2288 Script_sections::Section_type
2289 Output_section_definition::section_type() const
2291 switch (this->script_section_type_)
2293 case SCRIPT_SECTION_TYPE_NONE:
2294 return Script_sections::ST_NONE;
2295 case SCRIPT_SECTION_TYPE_NOLOAD:
2296 return Script_sections::ST_NOLOAD;
2297 case SCRIPT_SECTION_TYPE_COPY:
2298 case SCRIPT_SECTION_TYPE_DSECT:
2299 case SCRIPT_SECTION_TYPE_INFO:
2300 case SCRIPT_SECTION_TYPE_OVERLAY:
2301 // There are not really support so we treat them as ST_NONE. The
2302 // parse should have issued errors for them already.
2303 return Script_sections::ST_NONE;
2304 default:
2305 gold_unreachable();
2309 // Return the name of a script section type.
2311 const char*
2312 Output_section_definition::script_section_type_name(
2313 Script_section_type script_section_type)
2315 switch (script_section_type)
2317 case SCRIPT_SECTION_TYPE_NONE:
2318 return "NONE";
2319 case SCRIPT_SECTION_TYPE_NOLOAD:
2320 return "NOLOAD";
2321 case SCRIPT_SECTION_TYPE_DSECT:
2322 return "DSECT";
2323 case SCRIPT_SECTION_TYPE_COPY:
2324 return "COPY";
2325 case SCRIPT_SECTION_TYPE_INFO:
2326 return "INFO";
2327 case SCRIPT_SECTION_TYPE_OVERLAY:
2328 return "OVERLAY";
2329 default:
2330 gold_unreachable();
2334 // An output section created to hold orphaned input sections. These
2335 // do not actually appear in linker scripts. However, for convenience
2336 // when setting the output section addresses, we put a marker to these
2337 // sections in the appropriate place in the list of SECTIONS elements.
2339 class Orphan_output_section : public Sections_element
2341 public:
2342 Orphan_output_section(Output_section* os)
2343 : os_(os)
2346 // Return whether the orphan output section is relro. We can just
2347 // check the output section because we always set the flag, if
2348 // needed, just after we create the Orphan_output_section.
2349 bool
2350 is_relro() const
2351 { return this->os_->is_relro(); }
2353 // Initialize OSP with an output section. This should have been
2354 // done already.
2355 void
2356 orphan_section_init(Orphan_section_placement*,
2357 Script_sections::Elements_iterator)
2358 { gold_unreachable(); }
2360 // Set section addresses.
2361 void
2362 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2363 uint64_t*);
2365 // Get the list of segments to use for an allocated section when
2366 // using a PHDRS clause.
2367 Output_section*
2368 allocate_to_segment(String_list**, bool*);
2370 // Return the associated Output_section.
2371 Output_section*
2372 get_output_section() const
2373 { return this->os_; }
2375 // Print for debugging.
2376 void
2377 print(FILE* f) const
2379 fprintf(f, " marker for orphaned output section %s\n",
2380 this->os_->name());
2383 private:
2384 Output_section* os_;
2387 // Set section addresses.
2389 void
2390 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
2391 uint64_t* dot_value,
2392 uint64_t*,
2393 uint64_t* load_address)
2395 typedef std::list<Output_section::Input_section> Input_section_list;
2397 bool have_load_address = *load_address != *dot_value;
2399 uint64_t address = *dot_value;
2400 address = align_address(address, this->os_->addralign());
2402 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
2404 this->os_->set_address(address);
2405 if (have_load_address)
2406 this->os_->set_load_address(align_address(*load_address,
2407 this->os_->addralign()));
2410 Input_section_list input_sections;
2411 address += this->os_->get_input_sections(address, "", &input_sections);
2413 for (Input_section_list::iterator p = input_sections.begin();
2414 p != input_sections.end();
2415 ++p)
2417 uint64_t addralign = p->addralign();
2418 if (!p->is_input_section())
2419 p->output_section_data()->finalize_data_size();
2420 uint64_t size = p->data_size();
2421 address = align_address(address, addralign);
2422 this->os_->add_script_input_section(*p);
2423 address += size;
2426 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2427 if (this->os_ == NULL
2428 || (this->os_->flags() & elfcpp::SHF_TLS) == 0
2429 || this->os_->type() != elfcpp::SHT_NOBITS)
2431 if (!have_load_address)
2432 *load_address = address;
2433 else
2434 *load_address += address - *dot_value;
2436 *dot_value = address;
2440 // Get the list of segments to use for an allocated section when using
2441 // a PHDRS clause. If this is an allocated section, return the
2442 // Output_section. We don't change the list of segments.
2444 Output_section*
2445 Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
2447 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2448 return NULL;
2449 *orphan = true;
2450 return this->os_;
2453 // Class Phdrs_element. A program header from a PHDRS clause.
2455 class Phdrs_element
2457 public:
2458 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2459 bool includes_filehdr, bool includes_phdrs,
2460 bool is_flags_valid, unsigned int flags,
2461 Expression* load_address)
2462 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2463 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2464 flags_(flags), load_address_(load_address), load_address_value_(0),
2465 segment_(NULL)
2468 // Return the name of this segment.
2469 const std::string&
2470 name() const
2471 { return this->name_; }
2473 // Return the type of the segment.
2474 unsigned int
2475 type() const
2476 { return this->type_; }
2478 // Whether to include the file header.
2479 bool
2480 includes_filehdr() const
2481 { return this->includes_filehdr_; }
2483 // Whether to include the program headers.
2484 bool
2485 includes_phdrs() const
2486 { return this->includes_phdrs_; }
2488 // Return whether there is a load address.
2489 bool
2490 has_load_address() const
2491 { return this->load_address_ != NULL; }
2493 // Evaluate the load address expression if there is one.
2494 void
2495 eval_load_address(Symbol_table* symtab, Layout* layout)
2497 if (this->load_address_ != NULL)
2498 this->load_address_value_ = this->load_address_->eval(symtab, layout,
2499 true);
2502 // Return the load address.
2503 uint64_t
2504 load_address() const
2506 gold_assert(this->load_address_ != NULL);
2507 return this->load_address_value_;
2510 // Create the segment.
2511 Output_segment*
2512 create_segment(Layout* layout)
2514 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2515 return this->segment_;
2518 // Return the segment.
2519 Output_segment*
2520 segment()
2521 { return this->segment_; }
2523 // Release the segment.
2524 void
2525 release_segment()
2526 { this->segment_ = NULL; }
2528 // Set the segment flags if appropriate.
2529 void
2530 set_flags_if_valid()
2532 if (this->is_flags_valid_)
2533 this->segment_->set_flags(this->flags_);
2536 // Print for debugging.
2537 void
2538 print(FILE*) const;
2540 private:
2541 // The name used in the script.
2542 std::string name_;
2543 // The type of the segment (PT_LOAD, etc.).
2544 unsigned int type_;
2545 // Whether this segment includes the file header.
2546 bool includes_filehdr_;
2547 // Whether this segment includes the section headers.
2548 bool includes_phdrs_;
2549 // Whether the flags were explicitly specified.
2550 bool is_flags_valid_;
2551 // The flags for this segment (PF_R, etc.) if specified.
2552 unsigned int flags_;
2553 // The expression for the load address for this segment. This may
2554 // be NULL.
2555 Expression* load_address_;
2556 // The actual load address from evaluating the expression.
2557 uint64_t load_address_value_;
2558 // The segment itself.
2559 Output_segment* segment_;
2562 // Print for debugging.
2564 void
2565 Phdrs_element::print(FILE* f) const
2567 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
2568 if (this->includes_filehdr_)
2569 fprintf(f, " FILEHDR");
2570 if (this->includes_phdrs_)
2571 fprintf(f, " PHDRS");
2572 if (this->is_flags_valid_)
2573 fprintf(f, " FLAGS(%u)", this->flags_);
2574 if (this->load_address_ != NULL)
2576 fprintf(f, " AT(");
2577 this->load_address_->print(f);
2578 fprintf(f, ")");
2580 fprintf(f, ";\n");
2583 // Class Script_sections.
2585 Script_sections::Script_sections()
2586 : saw_sections_clause_(false),
2587 in_sections_clause_(false),
2588 sections_elements_(NULL),
2589 output_section_(NULL),
2590 phdrs_elements_(NULL),
2591 orphan_section_placement_(NULL),
2592 data_segment_align_start_(),
2593 saw_data_segment_align_(false),
2594 saw_relro_end_(false),
2595 saw_segment_start_expression_(false)
2599 // Start a SECTIONS clause.
2601 void
2602 Script_sections::start_sections()
2604 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2605 this->saw_sections_clause_ = true;
2606 this->in_sections_clause_ = true;
2607 if (this->sections_elements_ == NULL)
2608 this->sections_elements_ = new Sections_elements;
2611 // Finish a SECTIONS clause.
2613 void
2614 Script_sections::finish_sections()
2616 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2617 this->in_sections_clause_ = false;
2620 // Add a symbol to be defined.
2622 void
2623 Script_sections::add_symbol_assignment(const char* name, size_t length,
2624 Expression* val, bool provide,
2625 bool hidden)
2627 if (this->output_section_ != NULL)
2628 this->output_section_->add_symbol_assignment(name, length, val,
2629 provide, hidden);
2630 else
2632 Sections_element* p = new Sections_element_assignment(name, length,
2633 val, provide,
2634 hidden);
2635 this->sections_elements_->push_back(p);
2639 // Add an assignment to the special dot symbol.
2641 void
2642 Script_sections::add_dot_assignment(Expression* val)
2644 if (this->output_section_ != NULL)
2645 this->output_section_->add_dot_assignment(val);
2646 else
2648 // The GNU linker permits assignments to . to appears outside of
2649 // a SECTIONS clause, and treats it as appearing inside, so
2650 // sections_elements_ may be NULL here.
2651 if (this->sections_elements_ == NULL)
2653 this->sections_elements_ = new Sections_elements;
2654 this->saw_sections_clause_ = true;
2657 Sections_element* p = new Sections_element_dot_assignment(val);
2658 this->sections_elements_->push_back(p);
2662 // Add an assertion.
2664 void
2665 Script_sections::add_assertion(Expression* check, const char* message,
2666 size_t messagelen)
2668 if (this->output_section_ != NULL)
2669 this->output_section_->add_assertion(check, message, messagelen);
2670 else
2672 Sections_element* p = new Sections_element_assertion(check, message,
2673 messagelen);
2674 this->sections_elements_->push_back(p);
2678 // Start processing entries for an output section.
2680 void
2681 Script_sections::start_output_section(
2682 const char* name,
2683 size_t namelen,
2684 const Parser_output_section_header* header)
2686 Output_section_definition* posd = new Output_section_definition(name,
2687 namelen,
2688 header);
2689 this->sections_elements_->push_back(posd);
2690 gold_assert(this->output_section_ == NULL);
2691 this->output_section_ = posd;
2694 // Stop processing entries for an output section.
2696 void
2697 Script_sections::finish_output_section(
2698 const Parser_output_section_trailer* trailer)
2700 gold_assert(this->output_section_ != NULL);
2701 this->output_section_->finish(trailer);
2702 this->output_section_ = NULL;
2705 // Add a data item to the current output section.
2707 void
2708 Script_sections::add_data(int size, bool is_signed, Expression* val)
2710 gold_assert(this->output_section_ != NULL);
2711 this->output_section_->add_data(size, is_signed, val);
2714 // Add a fill value setting to the current output section.
2716 void
2717 Script_sections::add_fill(Expression* val)
2719 gold_assert(this->output_section_ != NULL);
2720 this->output_section_->add_fill(val);
2723 // Add an input section specification to the current output section.
2725 void
2726 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2728 gold_assert(this->output_section_ != NULL);
2729 this->output_section_->add_input_section(spec, keep);
2732 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
2733 // subsequent output sections may be relro.
2735 void
2736 Script_sections::data_segment_align()
2738 if (this->saw_data_segment_align_)
2739 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2740 gold_assert(!this->sections_elements_->empty());
2741 Sections_elements::iterator p = this->sections_elements_->end();
2742 --p;
2743 this->data_segment_align_start_ = p;
2744 this->saw_data_segment_align_ = true;
2747 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
2748 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
2750 void
2751 Script_sections::data_segment_relro_end()
2753 if (this->saw_relro_end_)
2754 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2755 "in a linker script"));
2756 this->saw_relro_end_ = true;
2758 if (!this->saw_data_segment_align_)
2759 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2760 else
2762 Sections_elements::iterator p = this->data_segment_align_start_;
2763 for (++p; p != this->sections_elements_->end(); ++p)
2764 (*p)->set_is_relro();
2768 // Create any required sections.
2770 void
2771 Script_sections::create_sections(Layout* layout)
2773 if (!this->saw_sections_clause_)
2774 return;
2775 for (Sections_elements::iterator p = this->sections_elements_->begin();
2776 p != this->sections_elements_->end();
2777 ++p)
2778 (*p)->create_sections(layout);
2781 // Add any symbols we are defining to the symbol table.
2783 void
2784 Script_sections::add_symbols_to_table(Symbol_table* symtab)
2786 if (!this->saw_sections_clause_)
2787 return;
2788 for (Sections_elements::iterator p = this->sections_elements_->begin();
2789 p != this->sections_elements_->end();
2790 ++p)
2791 (*p)->add_symbols_to_table(symtab);
2794 // Finalize symbols and check assertions.
2796 void
2797 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2799 if (!this->saw_sections_clause_)
2800 return;
2801 uint64_t dot_value = 0;
2802 for (Sections_elements::iterator p = this->sections_elements_->begin();
2803 p != this->sections_elements_->end();
2804 ++p)
2805 (*p)->finalize_symbols(symtab, layout, &dot_value);
2808 // Return the name of the output section to use for an input file name
2809 // and section name.
2811 const char*
2812 Script_sections::output_section_name(
2813 const char* file_name,
2814 const char* section_name,
2815 Output_section*** output_section_slot,
2816 Script_sections::Section_type* psection_type)
2818 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2819 p != this->sections_elements_->end();
2820 ++p)
2822 const char* ret = (*p)->output_section_name(file_name, section_name,
2823 output_section_slot,
2824 psection_type);
2826 if (ret != NULL)
2828 // The special name /DISCARD/ means that the input section
2829 // should be discarded.
2830 if (strcmp(ret, "/DISCARD/") == 0)
2832 *output_section_slot = NULL;
2833 *psection_type = Script_sections::ST_NONE;
2834 return NULL;
2836 return ret;
2840 // If we couldn't find a mapping for the name, the output section
2841 // gets the name of the input section.
2843 *output_section_slot = NULL;
2844 *psection_type = Script_sections::ST_NONE;
2846 return section_name;
2849 // Place a marker for an orphan output section into the SECTIONS
2850 // clause.
2852 void
2853 Script_sections::place_orphan(Output_section* os)
2855 Orphan_section_placement* osp = this->orphan_section_placement_;
2856 if (osp == NULL)
2858 // Initialize the Orphan_section_placement structure.
2859 osp = new Orphan_section_placement();
2860 for (Sections_elements::iterator p = this->sections_elements_->begin();
2861 p != this->sections_elements_->end();
2862 ++p)
2863 (*p)->orphan_section_init(osp, p);
2864 gold_assert(!this->sections_elements_->empty());
2865 Sections_elements::iterator last = this->sections_elements_->end();
2866 --last;
2867 osp->last_init(last);
2868 this->orphan_section_placement_ = osp;
2871 Orphan_output_section* orphan = new Orphan_output_section(os);
2873 // Look for where to put ORPHAN.
2874 Sections_elements::iterator* where;
2875 if (osp->find_place(os, &where))
2877 if ((**where)->is_relro())
2878 os->set_is_relro();
2879 else
2880 os->clear_is_relro();
2882 // We want to insert ORPHAN after *WHERE, and then update *WHERE
2883 // so that the next one goes after this one.
2884 Sections_elements::iterator p = *where;
2885 gold_assert(p != this->sections_elements_->end());
2886 ++p;
2887 *where = this->sections_elements_->insert(p, orphan);
2889 else
2891 os->clear_is_relro();
2892 // We don't have a place to put this orphan section. Put it,
2893 // and all other sections like it, at the end, but before the
2894 // sections which always come at the end.
2895 Sections_elements::iterator last = osp->last_place();
2896 *where = this->sections_elements_->insert(last, orphan);
2900 // Set the addresses of all the output sections. Walk through all the
2901 // elements, tracking the dot symbol. Apply assignments which set
2902 // absolute symbol values, in case they are used when setting dot.
2903 // Fill in data statement values. As we find output sections, set the
2904 // address, set the address of all associated input sections, and
2905 // update dot. Return the segment which should hold the file header
2906 // and segment headers, if any.
2908 Output_segment*
2909 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2911 gold_assert(this->saw_sections_clause_);
2913 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2914 // for our representation.
2915 for (Sections_elements::iterator p = this->sections_elements_->begin();
2916 p != this->sections_elements_->end();
2917 ++p)
2919 Output_section_definition* posd;
2920 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2921 if (failed_constraint != CONSTRAINT_NONE)
2923 Sections_elements::iterator q;
2924 for (q = this->sections_elements_->begin();
2925 q != this->sections_elements_->end();
2926 ++q)
2928 if (q != p)
2930 if ((*q)->alternate_constraint(posd, failed_constraint))
2931 break;
2935 if (q == this->sections_elements_->end())
2936 gold_error(_("no matching section constraint"));
2940 // Force the alignment of the first TLS section to be the maximum
2941 // alignment of all TLS sections.
2942 Output_section* first_tls = NULL;
2943 uint64_t tls_align = 0;
2944 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2945 p != this->sections_elements_->end();
2946 ++p)
2948 Output_section* os = (*p)->get_output_section();
2949 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
2951 if (first_tls == NULL)
2952 first_tls = os;
2953 if (os->addralign() > tls_align)
2954 tls_align = os->addralign();
2957 if (first_tls != NULL)
2958 first_tls->set_addralign(tls_align);
2960 // For a relocatable link, we implicitly set dot to zero.
2961 uint64_t dot_value = 0;
2962 uint64_t dot_alignment = 0;
2963 uint64_t load_address = 0;
2965 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
2966 // to set section addresses. If the script has any SEGMENT_START
2967 // expression, we do not set the section addresses.
2968 bool use_tsection_options =
2969 (!this->saw_segment_start_expression_
2970 && (parameters->options().user_set_Ttext()
2971 || parameters->options().user_set_Tdata()
2972 || parameters->options().user_set_Tbss()));
2974 for (Sections_elements::iterator p = this->sections_elements_->begin();
2975 p != this->sections_elements_->end();
2976 ++p)
2978 Output_section* os = (*p)->get_output_section();
2980 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
2981 // the special sections by names and doing dot assignments.
2982 if (use_tsection_options
2983 && os != NULL
2984 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
2986 uint64_t new_dot_value = dot_value;
2988 if (parameters->options().user_set_Ttext()
2989 && strcmp(os->name(), ".text") == 0)
2990 new_dot_value = parameters->options().Ttext();
2991 else if (parameters->options().user_set_Tdata()
2992 && strcmp(os->name(), ".data") == 0)
2993 new_dot_value = parameters->options().Tdata();
2994 else if (parameters->options().user_set_Tbss()
2995 && strcmp(os->name(), ".bss") == 0)
2996 new_dot_value = parameters->options().Tbss();
2998 // Update dot and load address if necessary.
2999 if (new_dot_value < dot_value)
3000 gold_error(_("dot may not move backward"));
3001 else if (new_dot_value != dot_value)
3003 dot_value = new_dot_value;
3004 load_address = new_dot_value;
3008 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3009 &load_address);
3012 if (this->phdrs_elements_ != NULL)
3014 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3015 p != this->phdrs_elements_->end();
3016 ++p)
3017 (*p)->eval_load_address(symtab, layout);
3020 return this->create_segments(layout, dot_alignment);
3023 // Sort the sections in order to put them into segments.
3025 class Sort_output_sections
3027 public:
3028 bool
3029 operator()(const Output_section* os1, const Output_section* os2) const;
3032 bool
3033 Sort_output_sections::operator()(const Output_section* os1,
3034 const Output_section* os2) const
3036 // Sort first by the load address.
3037 uint64_t lma1 = (os1->has_load_address()
3038 ? os1->load_address()
3039 : os1->address());
3040 uint64_t lma2 = (os2->has_load_address()
3041 ? os2->load_address()
3042 : os2->address());
3043 if (lma1 != lma2)
3044 return lma1 < lma2;
3046 // Then sort by the virtual address.
3047 if (os1->address() != os2->address())
3048 return os1->address() < os2->address();
3050 // Sort TLS sections to the end.
3051 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3052 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3053 if (tls1 != tls2)
3054 return tls2;
3056 // Sort PROGBITS before NOBITS.
3057 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
3058 return true;
3059 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
3060 return false;
3062 // Sort non-NOLOAD before NOLOAD.
3063 if (os1->is_noload() && !os2->is_noload())
3064 return true;
3065 if (!os1->is_noload() && os2->is_noload())
3066 return true;
3068 // Otherwise we don't care.
3069 return false;
3072 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
3073 // We treat a section with the SHF_TLS flag set as taking up space
3074 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3075 // space for them in the file.
3077 bool
3078 Script_sections::is_bss_section(const Output_section* os)
3080 return (os->type() == elfcpp::SHT_NOBITS
3081 && (os->flags() & elfcpp::SHF_TLS) == 0);
3084 // Return the size taken by the file header and the program headers.
3086 size_t
3087 Script_sections::total_header_size(Layout* layout) const
3089 size_t segment_count = layout->segment_count();
3090 size_t file_header_size;
3091 size_t segment_headers_size;
3092 if (parameters->target().get_size() == 32)
3094 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3095 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3097 else if (parameters->target().get_size() == 64)
3099 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3100 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3102 else
3103 gold_unreachable();
3105 return file_header_size + segment_headers_size;
3108 // Return the amount we have to subtract from the LMA to accomodate
3109 // headers of the given size. The complication is that the file
3110 // header have to be at the start of a page, as otherwise it will not
3111 // be at the start of the file.
3113 uint64_t
3114 Script_sections::header_size_adjustment(uint64_t lma,
3115 size_t sizeof_headers) const
3117 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3118 uint64_t hdr_lma = lma - sizeof_headers;
3119 hdr_lma &= ~(abi_pagesize - 1);
3120 return lma - hdr_lma;
3123 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
3124 // the segment which should hold the file header and segment headers,
3125 // if any.
3127 Output_segment*
3128 Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
3130 gold_assert(this->saw_sections_clause_);
3132 if (parameters->options().relocatable())
3133 return NULL;
3135 if (this->saw_phdrs_clause())
3136 return create_segments_from_phdrs_clause(layout, dot_alignment);
3138 Layout::Section_list sections;
3139 layout->get_allocated_sections(&sections);
3141 // Sort the sections by address.
3142 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
3144 this->create_note_and_tls_segments(layout, &sections);
3146 // Walk through the sections adding them to PT_LOAD segments.
3147 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3148 Output_segment* first_seg = NULL;
3149 Output_segment* current_seg = NULL;
3150 bool is_current_seg_readonly = true;
3151 Layout::Section_list::iterator plast = sections.end();
3152 uint64_t last_vma = 0;
3153 uint64_t last_lma = 0;
3154 uint64_t last_size = 0;
3155 for (Layout::Section_list::iterator p = sections.begin();
3156 p != sections.end();
3157 ++p)
3159 const uint64_t vma = (*p)->address();
3160 const uint64_t lma = ((*p)->has_load_address()
3161 ? (*p)->load_address()
3162 : vma);
3163 const uint64_t size = (*p)->current_data_size();
3165 bool need_new_segment;
3166 if (current_seg == NULL)
3167 need_new_segment = true;
3168 else if (lma - vma != last_lma - last_vma)
3170 // This section has a different LMA relationship than the
3171 // last one; we need a new segment.
3172 need_new_segment = true;
3174 else if (align_address(last_lma + last_size, abi_pagesize)
3175 < align_address(lma, abi_pagesize))
3177 // Putting this section in the segment would require
3178 // skipping a page.
3179 need_new_segment = true;
3181 else if (is_bss_section(*plast) && !is_bss_section(*p))
3183 // A non-BSS section can not follow a BSS section in the
3184 // same segment.
3185 need_new_segment = true;
3187 else if (is_current_seg_readonly
3188 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3189 && !parameters->options().omagic())
3191 // Don't put a writable section in the same segment as a
3192 // non-writable section.
3193 need_new_segment = true;
3195 else
3197 // Otherwise, reuse the existing segment.
3198 need_new_segment = false;
3201 elfcpp::Elf_Word seg_flags =
3202 Layout::section_flags_to_segment((*p)->flags());
3204 if (need_new_segment)
3206 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3207 seg_flags);
3208 current_seg->set_addresses(vma, lma);
3209 current_seg->set_minimum_p_align(dot_alignment);
3210 if (first_seg == NULL)
3211 first_seg = current_seg;
3212 is_current_seg_readonly = true;
3215 current_seg->add_output_section_to_load(layout, *p, seg_flags);
3217 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3218 is_current_seg_readonly = false;
3220 plast = p;
3221 last_vma = vma;
3222 last_lma = lma;
3223 last_size = size;
3226 // An ELF program should work even if the program headers are not in
3227 // a PT_LOAD segment. However, it appears that the Linux kernel
3228 // does not set the AT_PHDR auxiliary entry in that case. It sets
3229 // the load address to p_vaddr - p_offset of the first PT_LOAD
3230 // segment. It then sets AT_PHDR to the load address plus the
3231 // offset to the program headers, e_phoff in the file header. This
3232 // fails when the program headers appear in the file before the
3233 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3234 // segment to hold the file header and the program headers. This is
3235 // effectively what the GNU linker does, and it is slightly more
3236 // efficient in any case. We try to use the first PT_LOAD segment
3237 // if we can, otherwise we make a new one.
3239 if (first_seg == NULL)
3240 return NULL;
3242 // -n or -N mean that the program is not demand paged and there is
3243 // no need to put the program headers in a PT_LOAD segment.
3244 if (parameters->options().nmagic() || parameters->options().omagic())
3245 return NULL;
3247 size_t sizeof_headers = this->total_header_size(layout);
3249 uint64_t vma = first_seg->vaddr();
3250 uint64_t lma = first_seg->paddr();
3252 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3254 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3256 first_seg->set_addresses(vma - subtract, lma - subtract);
3257 return first_seg;
3260 // If there is no room to squeeze in the headers, then punt. The
3261 // resulting executable probably won't run on GNU/Linux, but we
3262 // trust that the user knows what they are doing.
3263 if (lma < subtract || vma < subtract)
3264 return NULL;
3266 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3267 elfcpp::PF_R);
3268 load_seg->set_addresses(vma - subtract, lma - subtract);
3270 return load_seg;
3273 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3274 // segment if there are any SHT_TLS sections.
3276 void
3277 Script_sections::create_note_and_tls_segments(
3278 Layout* layout,
3279 const Layout::Section_list* sections)
3281 gold_assert(!this->saw_phdrs_clause());
3283 bool saw_tls = false;
3284 for (Layout::Section_list::const_iterator p = sections->begin();
3285 p != sections->end();
3286 ++p)
3288 if ((*p)->type() == elfcpp::SHT_NOTE)
3290 elfcpp::Elf_Word seg_flags =
3291 Layout::section_flags_to_segment((*p)->flags());
3292 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3293 seg_flags);
3294 oseg->add_output_section_to_nonload(*p, seg_flags);
3296 // Incorporate any subsequent SHT_NOTE sections, in the
3297 // hopes that the script is sensible.
3298 Layout::Section_list::const_iterator pnext = p + 1;
3299 while (pnext != sections->end()
3300 && (*pnext)->type() == elfcpp::SHT_NOTE)
3302 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3303 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3304 p = pnext;
3305 ++pnext;
3309 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3311 if (saw_tls)
3312 gold_error(_("TLS sections are not adjacent"));
3314 elfcpp::Elf_Word seg_flags =
3315 Layout::section_flags_to_segment((*p)->flags());
3316 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3317 seg_flags);
3318 oseg->add_output_section_to_nonload(*p, seg_flags);
3320 Layout::Section_list::const_iterator pnext = p + 1;
3321 while (pnext != sections->end()
3322 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3324 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3325 oseg->add_output_section_to_nonload(*pnext, seg_flags);
3326 p = pnext;
3327 ++pnext;
3330 saw_tls = true;
3335 // Add a program header. The PHDRS clause is syntactically distinct
3336 // from the SECTIONS clause, but we implement it with the SECTIONS
3337 // support because PHDRS is useless if there is no SECTIONS clause.
3339 void
3340 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3341 bool includes_filehdr, bool includes_phdrs,
3342 bool is_flags_valid, unsigned int flags,
3343 Expression* load_address)
3345 if (this->phdrs_elements_ == NULL)
3346 this->phdrs_elements_ = new Phdrs_elements();
3347 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3348 includes_filehdr,
3349 includes_phdrs,
3350 is_flags_valid, flags,
3351 load_address));
3354 // Return the number of segments we expect to create based on the
3355 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3357 size_t
3358 Script_sections::expected_segment_count(const Layout* layout) const
3360 if (this->saw_phdrs_clause())
3361 return this->phdrs_elements_->size();
3363 Layout::Section_list sections;
3364 layout->get_allocated_sections(&sections);
3366 // We assume that we will need two PT_LOAD segments.
3367 size_t ret = 2;
3369 bool saw_note = false;
3370 bool saw_tls = false;
3371 for (Layout::Section_list::const_iterator p = sections.begin();
3372 p != sections.end();
3373 ++p)
3375 if ((*p)->type() == elfcpp::SHT_NOTE)
3377 // Assume that all note sections will fit into a single
3378 // PT_NOTE segment.
3379 if (!saw_note)
3381 ++ret;
3382 saw_note = true;
3385 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3387 // There can only be one PT_TLS segment.
3388 if (!saw_tls)
3390 ++ret;
3391 saw_tls = true;
3396 return ret;
3399 // Create the segments from a PHDRS clause. Return the segment which
3400 // should hold the file header and program headers, if any.
3402 Output_segment*
3403 Script_sections::create_segments_from_phdrs_clause(Layout* layout,
3404 uint64_t dot_alignment)
3406 this->attach_sections_using_phdrs_clause(layout);
3407 return this->set_phdrs_clause_addresses(layout, dot_alignment);
3410 // Create the segments from the PHDRS clause, and put the output
3411 // sections in them.
3413 void
3414 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
3416 typedef std::map<std::string, Output_segment*> Name_to_segment;
3417 Name_to_segment name_to_segment;
3418 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3419 p != this->phdrs_elements_->end();
3420 ++p)
3421 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
3423 // Walk through the output sections and attach them to segments.
3424 // Output sections in the script which do not list segments are
3425 // attached to the same set of segments as the immediately preceding
3426 // output section.
3428 String_list* phdr_names = NULL;
3429 bool load_segments_only = false;
3430 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3431 p != this->sections_elements_->end();
3432 ++p)
3434 bool orphan;
3435 String_list* old_phdr_names = phdr_names;
3436 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &orphan);
3437 if (os == NULL)
3438 continue;
3440 if (phdr_names == NULL)
3442 gold_error(_("allocated section not in any segment"));
3443 continue;
3446 // We see a list of segments names. Disable PT_LOAD segment only
3447 // filtering.
3448 if (old_phdr_names != phdr_names)
3449 load_segments_only = false;
3451 // If this is an orphan section--one that was not explicitly
3452 // mentioned in the linker script--then it should not inherit
3453 // any segment type other than PT_LOAD. Otherwise, e.g., the
3454 // PT_INTERP segment will pick up following orphan sections,
3455 // which does not make sense. If this is not an orphan section,
3456 // we trust the linker script.
3457 if (orphan)
3459 // Enable PT_LOAD segments only filtering until we see another
3460 // list of segment names.
3461 load_segments_only = true;
3464 bool in_load_segment = false;
3465 for (String_list::const_iterator q = phdr_names->begin();
3466 q != phdr_names->end();
3467 ++q)
3469 Name_to_segment::const_iterator r = name_to_segment.find(*q);
3470 if (r == name_to_segment.end())
3471 gold_error(_("no segment %s"), q->c_str());
3472 else
3474 if (load_segments_only
3475 && r->second->type() != elfcpp::PT_LOAD)
3476 continue;
3478 elfcpp::Elf_Word seg_flags =
3479 Layout::section_flags_to_segment(os->flags());
3481 if (r->second->type() != elfcpp::PT_LOAD)
3482 r->second->add_output_section_to_nonload(os, seg_flags);
3483 else
3485 r->second->add_output_section_to_load(layout, os, seg_flags);
3486 if (in_load_segment)
3487 gold_error(_("section in two PT_LOAD segments"));
3488 in_load_segment = true;
3493 if (!in_load_segment)
3494 gold_error(_("allocated section not in any PT_LOAD segment"));
3498 // Set the addresses for segments created from a PHDRS clause. Return
3499 // the segment which should hold the file header and program headers,
3500 // if any.
3502 Output_segment*
3503 Script_sections::set_phdrs_clause_addresses(Layout* layout,
3504 uint64_t dot_alignment)
3506 Output_segment* load_seg = NULL;
3507 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3508 p != this->phdrs_elements_->end();
3509 ++p)
3511 // Note that we have to set the flags after adding the output
3512 // sections to the segment, as adding an output segment can
3513 // change the flags.
3514 (*p)->set_flags_if_valid();
3516 Output_segment* oseg = (*p)->segment();
3518 if (oseg->type() != elfcpp::PT_LOAD)
3520 // The addresses of non-PT_LOAD segments are set from the
3521 // PT_LOAD segments.
3522 if ((*p)->has_load_address())
3523 gold_error(_("may only specify load address for PT_LOAD segment"));
3524 continue;
3527 oseg->set_minimum_p_align(dot_alignment);
3529 // The output sections should have addresses from the SECTIONS
3530 // clause. The addresses don't have to be in order, so find the
3531 // one with the lowest load address. Use that to set the
3532 // address of the segment.
3534 Output_section* osec = oseg->section_with_lowest_load_address();
3535 if (osec == NULL)
3537 oseg->set_addresses(0, 0);
3538 continue;
3541 uint64_t vma = osec->address();
3542 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
3544 // Override the load address of the section with the load
3545 // address specified for the segment.
3546 if ((*p)->has_load_address())
3548 if (osec->has_load_address())
3549 gold_warning(_("PHDRS load address overrides "
3550 "section %s load address"),
3551 osec->name());
3553 lma = (*p)->load_address();
3556 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
3557 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
3559 // We could support this if we wanted to.
3560 gold_error(_("using only one of FILEHDR and PHDRS is "
3561 "not currently supported"));
3563 if (headers)
3565 size_t sizeof_headers = this->total_header_size(layout);
3566 uint64_t subtract = this->header_size_adjustment(lma,
3567 sizeof_headers);
3568 if (lma >= subtract && vma >= subtract)
3570 lma -= subtract;
3571 vma -= subtract;
3573 else
3575 gold_error(_("sections loaded on first page without room "
3576 "for file and program headers "
3577 "are not supported"));
3580 if (load_seg != NULL)
3581 gold_error(_("using FILEHDR and PHDRS on more than one "
3582 "PT_LOAD segment is not currently supported"));
3583 load_seg = oseg;
3586 oseg->set_addresses(vma, lma);
3589 return load_seg;
3592 // Add the file header and segment headers to non-load segments
3593 // specified in the PHDRS clause.
3595 void
3596 Script_sections::put_headers_in_phdrs(Output_data* file_header,
3597 Output_data* segment_headers)
3599 gold_assert(this->saw_phdrs_clause());
3600 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3601 p != this->phdrs_elements_->end();
3602 ++p)
3604 if ((*p)->type() != elfcpp::PT_LOAD)
3606 if ((*p)->includes_phdrs())
3607 (*p)->segment()->add_initial_output_data(segment_headers);
3608 if ((*p)->includes_filehdr())
3609 (*p)->segment()->add_initial_output_data(file_header);
3614 // Look for an output section by name and return the address, the load
3615 // address, the alignment, and the size. This is used when an
3616 // expression refers to an output section which was not actually
3617 // created. This returns true if the section was found, false
3618 // otherwise.
3620 bool
3621 Script_sections::get_output_section_info(const char* name, uint64_t* address,
3622 uint64_t* load_address,
3623 uint64_t* addralign,
3624 uint64_t* size) const
3626 if (!this->saw_sections_clause_)
3627 return false;
3628 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3629 p != this->sections_elements_->end();
3630 ++p)
3631 if ((*p)->get_output_section_info(name, address, load_address, addralign,
3632 size))
3633 return true;
3634 return false;
3637 // Release all Output_segments. This remove all pointers to all
3638 // Output_segments.
3640 void
3641 Script_sections::release_segments()
3643 if (this->saw_phdrs_clause())
3645 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3646 p != this->phdrs_elements_->end();
3647 ++p)
3648 (*p)->release_segment();
3652 // Print the SECTIONS clause to F for debugging.
3654 void
3655 Script_sections::print(FILE* f) const
3657 if (!this->saw_sections_clause_)
3658 return;
3660 fprintf(f, "SECTIONS {\n");
3662 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3663 p != this->sections_elements_->end();
3664 ++p)
3665 (*p)->print(f);
3667 fprintf(f, "}\n");
3669 if (this->phdrs_elements_ != NULL)
3671 fprintf(f, "PHDRS {\n");
3672 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3673 p != this->phdrs_elements_->end();
3674 ++p)
3675 (*p)->print(f);
3676 fprintf(f, "}\n");
3680 } // End namespace gold.