bfd/
[binutils.git] / gold / script-sections.cc
blobd6e2b7279e80d6954882db7d00c863274cef116f
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_BSS,
89 PLACE_REL,
90 PLACE_INTERP,
91 PLACE_NONALLOC,
92 PLACE_LAST,
93 PLACE_MAX
96 // The information we keep for a specific place.
97 struct Place
99 // The name of sections for this place.
100 const char* name;
101 // Whether we have a location for this place.
102 bool have_location;
103 // The iterator for this place.
104 Elements_iterator location;
107 // Initialize one place element.
108 void
109 initialize_place(Place_index, const char*);
111 // The places.
112 Place places_[PLACE_MAX];
113 // True if this is the first call to output_section_init.
114 bool first_init_;
117 // Initialize Orphan_section_placement.
119 Orphan_section_placement::Orphan_section_placement()
120 : first_init_(true)
122 this->initialize_place(PLACE_TEXT, ".text");
123 this->initialize_place(PLACE_RODATA, ".rodata");
124 this->initialize_place(PLACE_DATA, ".data");
125 this->initialize_place(PLACE_BSS, ".bss");
126 this->initialize_place(PLACE_REL, NULL);
127 this->initialize_place(PLACE_INTERP, ".interp");
128 this->initialize_place(PLACE_NONALLOC, NULL);
129 this->initialize_place(PLACE_LAST, NULL);
132 // Initialize one place element.
134 void
135 Orphan_section_placement::initialize_place(Place_index index, const char* name)
137 this->places_[index].name = name;
138 this->places_[index].have_location = false;
141 // While initializing the Orphan_section_placement information, this
142 // is called once for each output section named in the linker script.
143 // If we found an output section during the link, it will be passed in
144 // OS.
146 void
147 Orphan_section_placement::output_section_init(const std::string& name,
148 Output_section* os,
149 Elements_iterator location)
151 bool first_init = this->first_init_;
152 this->first_init_ = false;
154 for (int i = 0; i < PLACE_MAX; ++i)
156 if (this->places_[i].name != NULL && this->places_[i].name == name)
158 if (this->places_[i].have_location)
160 // We have already seen a section with this name.
161 return;
164 this->places_[i].location = location;
165 this->places_[i].have_location = true;
167 // If we just found the .bss section, restart the search for
168 // an unallocated section. This follows the GNU linker's
169 // behaviour.
170 if (i == PLACE_BSS)
171 this->places_[PLACE_NONALLOC].have_location = false;
173 return;
177 // Relocation sections.
178 if (!this->places_[PLACE_REL].have_location
179 && os != NULL
180 && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
181 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
183 this->places_[PLACE_REL].location = location;
184 this->places_[PLACE_REL].have_location = true;
187 // We find the location for unallocated sections by finding the
188 // first debugging or comment section after the BSS section (if
189 // there is one).
190 if (!this->places_[PLACE_NONALLOC].have_location
191 && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
193 // We add orphan sections after the location in PLACES_. We
194 // want to store unallocated sections before LOCATION. If this
195 // is the very first section, we can't use it.
196 if (!first_init)
198 --location;
199 this->places_[PLACE_NONALLOC].location = location;
200 this->places_[PLACE_NONALLOC].have_location = true;
205 // Initialize the last location.
207 void
208 Orphan_section_placement::last_init(Elements_iterator location)
210 this->places_[PLACE_LAST].location = location;
211 this->places_[PLACE_LAST].have_location = true;
214 // Set *PWHERE to the address of an iterator pointing to the location
215 // to use for an orphan section. Return true if the iterator has a
216 // value, false otherwise.
218 bool
219 Orphan_section_placement::find_place(Output_section* os,
220 Elements_iterator** pwhere)
222 // Figure out where OS should go. This is based on the GNU linker
223 // code. FIXME: The GNU linker handles small data sections
224 // specially, but we don't.
225 elfcpp::Elf_Word type = os->type();
226 elfcpp::Elf_Xword flags = os->flags();
227 Place_index index;
228 if ((flags & elfcpp::SHF_ALLOC) == 0
229 && !Layout::is_debug_info_section(os->name()))
230 index = PLACE_NONALLOC;
231 else if ((flags & elfcpp::SHF_ALLOC) == 0)
232 index = PLACE_LAST;
233 else if (type == elfcpp::SHT_NOTE)
234 index = PLACE_INTERP;
235 else if (type == elfcpp::SHT_NOBITS)
236 index = PLACE_BSS;
237 else if ((flags & elfcpp::SHF_WRITE) != 0)
238 index = PLACE_DATA;
239 else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
240 index = PLACE_REL;
241 else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
242 index = PLACE_RODATA;
243 else
244 index = PLACE_TEXT;
246 // If we don't have a location yet, try to find one based on a
247 // plausible ordering of sections.
248 if (!this->places_[index].have_location)
250 Place_index follow;
251 switch (index)
253 default:
254 follow = PLACE_MAX;
255 break;
256 case PLACE_RODATA:
257 follow = PLACE_TEXT;
258 break;
259 case PLACE_BSS:
260 follow = PLACE_DATA;
261 break;
262 case PLACE_REL:
263 follow = PLACE_TEXT;
264 break;
265 case PLACE_INTERP:
266 follow = PLACE_TEXT;
267 break;
269 if (follow != PLACE_MAX && this->places_[follow].have_location)
271 // Set the location of INDEX to the location of FOLLOW. The
272 // location of INDEX will then be incremented by the caller,
273 // so anything in INDEX will continue to be after anything
274 // in FOLLOW.
275 this->places_[index].location = this->places_[follow].location;
276 this->places_[index].have_location = true;
280 *pwhere = &this->places_[index].location;
281 bool ret = this->places_[index].have_location;
283 // The caller will set the location.
284 this->places_[index].have_location = true;
286 return ret;
289 // Return the iterator being used for sections at the very end of the
290 // linker script.
292 Orphan_section_placement::Elements_iterator
293 Orphan_section_placement::last_place() const
295 gold_assert(this->places_[PLACE_LAST].have_location);
296 return this->places_[PLACE_LAST].location;
299 // An element in a SECTIONS clause.
301 class Sections_element
303 public:
304 Sections_element()
307 virtual ~Sections_element()
310 // Return whether an output section is relro.
311 virtual bool
312 is_relro() const
313 { return false; }
315 // Record that an output section is relro.
316 virtual void
317 set_is_relro()
320 // Create any required output sections. The only real
321 // implementation is in Output_section_definition.
322 virtual void
323 create_sections(Layout*)
326 // Add any symbol being defined to the symbol table.
327 virtual void
328 add_symbols_to_table(Symbol_table*)
331 // Finalize symbols and check assertions.
332 virtual void
333 finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
336 // Return the output section name to use for an input file name and
337 // section name. This only real implementation is in
338 // Output_section_definition.
339 virtual const char*
340 output_section_name(const char*, const char*, Output_section***)
341 { return NULL; }
343 // Initialize OSP with an output section.
344 virtual void
345 orphan_section_init(Orphan_section_placement*,
346 Script_sections::Elements_iterator)
349 // Set section addresses. This includes applying assignments if the
350 // the expression is an absolute value.
351 virtual void
352 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*)
355 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
356 // this section is constrained, and the input sections do not match,
357 // return the constraint, and set *POSD.
358 virtual Section_constraint
359 check_constraint(Output_section_definition**)
360 { return CONSTRAINT_NONE; }
362 // See if this is the alternate output section for a constrained
363 // output section. If it is, transfer the Output_section and return
364 // true. Otherwise return false.
365 virtual bool
366 alternate_constraint(Output_section_definition*, Section_constraint)
367 { return false; }
369 // Get the list of segments to use for an allocated section when
370 // using a PHDRS clause. If this is an allocated section, return
371 // the Output_section, and set *PHDRS_LIST (the first parameter) to
372 // the list of PHDRS to which it should be attached. If the PHDRS
373 // were not specified, don't change *PHDRS_LIST. When not returning
374 // NULL, set *ORPHAN (the second parameter) according to whether
375 // this is an orphan section--one that is not mentioned in the
376 // linker script.
377 virtual Output_section*
378 allocate_to_segment(String_list**, bool*)
379 { return NULL; }
381 // Look for an output section by name and return the address, the
382 // load address, the alignment, and the size. This is used when an
383 // expression refers to an output section which was not actually
384 // created. This returns true if the section was found, false
385 // otherwise. The only real definition is for
386 // Output_section_definition.
387 virtual bool
388 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
389 uint64_t*) const
390 { return false; }
392 // Return the associated Output_section if there is one.
393 virtual Output_section*
394 get_output_section() const
395 { return NULL; }
397 // Print the element for debugging purposes.
398 virtual void
399 print(FILE* f) const = 0;
402 // An assignment in a SECTIONS clause outside of an output section.
404 class Sections_element_assignment : public Sections_element
406 public:
407 Sections_element_assignment(const char* name, size_t namelen,
408 Expression* val, bool provide, bool hidden)
409 : assignment_(name, namelen, val, provide, hidden)
412 // Add the symbol to the symbol table.
413 void
414 add_symbols_to_table(Symbol_table* symtab)
415 { this->assignment_.add_to_table(symtab); }
417 // Finalize the symbol.
418 void
419 finalize_symbols(Symbol_table* symtab, const Layout* layout,
420 uint64_t* dot_value)
422 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
425 // Set the section address. There is no section here, but if the
426 // value is absolute, we set the symbol. This permits us to use
427 // absolute symbols when setting dot.
428 void
429 set_section_addresses(Symbol_table* symtab, Layout* layout,
430 uint64_t* dot_value, uint64_t*)
432 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
435 // Print for debugging.
436 void
437 print(FILE* f) const
439 fprintf(f, " ");
440 this->assignment_.print(f);
443 private:
444 Symbol_assignment assignment_;
447 // An assignment to the dot symbol in a SECTIONS clause outside of an
448 // output section.
450 class Sections_element_dot_assignment : public Sections_element
452 public:
453 Sections_element_dot_assignment(Expression* val)
454 : val_(val)
457 // Finalize the symbol.
458 void
459 finalize_symbols(Symbol_table* symtab, const Layout* layout,
460 uint64_t* dot_value)
462 // We ignore the section of the result because outside of an
463 // output section definition the dot symbol is always considered
464 // to be absolute.
465 Output_section* dummy;
466 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
467 NULL, &dummy);
470 // Update the dot symbol while setting section addresses.
471 void
472 set_section_addresses(Symbol_table* symtab, Layout* layout,
473 uint64_t* dot_value, uint64_t* load_address)
475 Output_section* dummy;
476 *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
477 NULL, &dummy);
478 *load_address = *dot_value;
481 // Print for debugging.
482 void
483 print(FILE* f) const
485 fprintf(f, " . = ");
486 this->val_->print(f);
487 fprintf(f, "\n");
490 private:
491 Expression* val_;
494 // An assertion in a SECTIONS clause outside of an output section.
496 class Sections_element_assertion : public Sections_element
498 public:
499 Sections_element_assertion(Expression* check, const char* message,
500 size_t messagelen)
501 : assertion_(check, message, messagelen)
504 // Check the assertion.
505 void
506 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
507 { this->assertion_.check(symtab, layout); }
509 // Print for debugging.
510 void
511 print(FILE* f) const
513 fprintf(f, " ");
514 this->assertion_.print(f);
517 private:
518 Script_assertion assertion_;
521 // An element in an output section in a SECTIONS clause.
523 class Output_section_element
525 public:
526 // A list of input sections.
527 typedef std::list<Output_section::Simple_input_section> Input_section_list;
529 Output_section_element()
532 virtual ~Output_section_element()
535 // Return whether this element requires an output section to exist.
536 virtual bool
537 needs_output_section() const
538 { return false; }
540 // Add any symbol being defined to the symbol table.
541 virtual void
542 add_symbols_to_table(Symbol_table*)
545 // Finalize symbols and check assertions.
546 virtual void
547 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
550 // Return whether this element matches FILE_NAME and SECTION_NAME.
551 // The only real implementation is in Output_section_element_input.
552 virtual bool
553 match_name(const char*, const char*) const
554 { return false; }
556 // Set section addresses. This includes applying assignments if the
557 // the expression is an absolute value.
558 virtual void
559 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
560 uint64_t*, Output_section**, std::string*,
561 Input_section_list*)
564 // Print the element for debugging purposes.
565 virtual void
566 print(FILE* f) const = 0;
568 protected:
569 // Return a fill string that is LENGTH bytes long, filling it with
570 // FILL.
571 std::string
572 get_fill_string(const std::string* fill, section_size_type length) const;
575 std::string
576 Output_section_element::get_fill_string(const std::string* fill,
577 section_size_type length) const
579 std::string this_fill;
580 this_fill.reserve(length);
581 while (this_fill.length() + fill->length() <= length)
582 this_fill += *fill;
583 if (this_fill.length() < length)
584 this_fill.append(*fill, 0, length - this_fill.length());
585 return this_fill;
588 // A symbol assignment in an output section.
590 class Output_section_element_assignment : public Output_section_element
592 public:
593 Output_section_element_assignment(const char* name, size_t namelen,
594 Expression* val, bool provide,
595 bool hidden)
596 : assignment_(name, namelen, val, provide, hidden)
599 // Add the symbol to the symbol table.
600 void
601 add_symbols_to_table(Symbol_table* symtab)
602 { this->assignment_.add_to_table(symtab); }
604 // Finalize the symbol.
605 void
606 finalize_symbols(Symbol_table* symtab, const Layout* layout,
607 uint64_t* dot_value, Output_section** dot_section)
609 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
610 *dot_section);
613 // Set the section address. There is no section here, but if the
614 // value is absolute, we set the symbol. This permits us to use
615 // absolute symbols when setting dot.
616 void
617 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
618 uint64_t, uint64_t* dot_value, Output_section**,
619 std::string*, Input_section_list*)
621 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
624 // Print for debugging.
625 void
626 print(FILE* f) const
628 fprintf(f, " ");
629 this->assignment_.print(f);
632 private:
633 Symbol_assignment assignment_;
636 // An assignment to the dot symbol in an output section.
638 class Output_section_element_dot_assignment : public Output_section_element
640 public:
641 Output_section_element_dot_assignment(Expression* val)
642 : val_(val)
645 // Finalize the symbol.
646 void
647 finalize_symbols(Symbol_table* symtab, const Layout* layout,
648 uint64_t* dot_value, Output_section** dot_section)
650 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
651 *dot_section, dot_section);
654 // Update the dot symbol while setting section addresses.
655 void
656 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
657 uint64_t, uint64_t* dot_value, Output_section**,
658 std::string*, Input_section_list*);
660 // Print for debugging.
661 void
662 print(FILE* f) const
664 fprintf(f, " . = ");
665 this->val_->print(f);
666 fprintf(f, "\n");
669 private:
670 Expression* val_;
673 // Update the dot symbol while setting section addresses.
675 void
676 Output_section_element_dot_assignment::set_section_addresses(
677 Symbol_table* symtab,
678 Layout* layout,
679 Output_section* output_section,
680 uint64_t,
681 uint64_t* dot_value,
682 Output_section** dot_section,
683 std::string* fill,
684 Input_section_list*)
686 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
687 *dot_value, *dot_section,
688 dot_section);
689 if (next_dot < *dot_value)
690 gold_error(_("dot may not move backward"));
691 if (next_dot > *dot_value && output_section != NULL)
693 section_size_type length = convert_to_section_size_type(next_dot
694 - *dot_value);
695 Output_section_data* posd;
696 if (fill->empty())
697 posd = new Output_data_zero_fill(length, 0);
698 else
700 std::string this_fill = this->get_fill_string(fill, length);
701 posd = new Output_data_const(this_fill, 0);
703 output_section->add_output_section_data(posd);
704 layout->new_output_section_data_from_script(posd);
706 *dot_value = next_dot;
709 // An assertion in an output section.
711 class Output_section_element_assertion : public Output_section_element
713 public:
714 Output_section_element_assertion(Expression* check, const char* message,
715 size_t messagelen)
716 : assertion_(check, message, messagelen)
719 void
720 print(FILE* f) const
722 fprintf(f, " ");
723 this->assertion_.print(f);
726 private:
727 Script_assertion assertion_;
730 // We use a special instance of Output_section_data to handle BYTE,
731 // SHORT, etc. This permits forward references to symbols in the
732 // expressions.
734 class Output_data_expression : public Output_section_data
736 public:
737 Output_data_expression(int size, bool is_signed, Expression* val,
738 const Symbol_table* symtab, const Layout* layout,
739 uint64_t dot_value, Output_section* dot_section)
740 : Output_section_data(size, 0, true),
741 is_signed_(is_signed), val_(val), symtab_(symtab),
742 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
745 protected:
746 // Write the data to the output file.
747 void
748 do_write(Output_file*);
750 // Write the data to a buffer.
751 void
752 do_write_to_buffer(unsigned char*);
754 // Write to a map file.
755 void
756 do_print_to_mapfile(Mapfile* mapfile) const
757 { mapfile->print_output_data(this, _("** expression")); }
759 private:
760 template<bool big_endian>
761 void
762 endian_write_to_buffer(uint64_t, unsigned char*);
764 bool is_signed_;
765 Expression* val_;
766 const Symbol_table* symtab_;
767 const Layout* layout_;
768 uint64_t dot_value_;
769 Output_section* dot_section_;
772 // Write the data element to the output file.
774 void
775 Output_data_expression::do_write(Output_file* of)
777 unsigned char* view = of->get_output_view(this->offset(), this->data_size());
778 this->write_to_buffer(view);
779 of->write_output_view(this->offset(), this->data_size(), view);
782 // Write the data element to a buffer.
784 void
785 Output_data_expression::do_write_to_buffer(unsigned char* buf)
787 Output_section* dummy;
788 uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
789 true, this->dot_value_,
790 this->dot_section_, &dummy);
792 if (parameters->target().is_big_endian())
793 this->endian_write_to_buffer<true>(val, buf);
794 else
795 this->endian_write_to_buffer<false>(val, buf);
798 template<bool big_endian>
799 void
800 Output_data_expression::endian_write_to_buffer(uint64_t val,
801 unsigned char* buf)
803 switch (this->data_size())
805 case 1:
806 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
807 break;
808 case 2:
809 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
810 break;
811 case 4:
812 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
813 break;
814 case 8:
815 if (parameters->target().get_size() == 32)
817 val &= 0xffffffff;
818 if (this->is_signed_ && (val & 0x80000000) != 0)
819 val |= 0xffffffff00000000LL;
821 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
822 break;
823 default:
824 gold_unreachable();
828 // A data item in an output section.
830 class Output_section_element_data : public Output_section_element
832 public:
833 Output_section_element_data(int size, bool is_signed, Expression* val)
834 : size_(size), is_signed_(is_signed), val_(val)
837 // If there is a data item, then we must create an output section.
838 bool
839 needs_output_section() const
840 { return true; }
842 // Finalize symbols--we just need to update dot.
843 void
844 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
845 Output_section**)
846 { *dot_value += this->size_; }
848 // Store the value in the section.
849 void
850 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
851 uint64_t* dot_value, Output_section**, std::string*,
852 Input_section_list*);
854 // Print for debugging.
855 void
856 print(FILE*) const;
858 private:
859 // The size in bytes.
860 int size_;
861 // Whether the value is signed.
862 bool is_signed_;
863 // The value.
864 Expression* val_;
867 // Store the value in the section.
869 void
870 Output_section_element_data::set_section_addresses(
871 Symbol_table* symtab,
872 Layout* layout,
873 Output_section* os,
874 uint64_t,
875 uint64_t* dot_value,
876 Output_section** dot_section,
877 std::string*,
878 Input_section_list*)
880 gold_assert(os != NULL);
881 Output_data_expression* expression =
882 new Output_data_expression(this->size_, this->is_signed_, this->val_,
883 symtab, layout, *dot_value, *dot_section);
884 os->add_output_section_data(expression);
885 layout->new_output_section_data_from_script(expression);
886 *dot_value += this->size_;
889 // Print for debugging.
891 void
892 Output_section_element_data::print(FILE* f) const
894 const char* s;
895 switch (this->size_)
897 case 1:
898 s = "BYTE";
899 break;
900 case 2:
901 s = "SHORT";
902 break;
903 case 4:
904 s = "LONG";
905 break;
906 case 8:
907 if (this->is_signed_)
908 s = "SQUAD";
909 else
910 s = "QUAD";
911 break;
912 default:
913 gold_unreachable();
915 fprintf(f, " %s(", s);
916 this->val_->print(f);
917 fprintf(f, ")\n");
920 // A fill value setting in an output section.
922 class Output_section_element_fill : public Output_section_element
924 public:
925 Output_section_element_fill(Expression* val)
926 : val_(val)
929 // Update the fill value while setting section addresses.
930 void
931 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
932 uint64_t, uint64_t* dot_value,
933 Output_section** dot_section,
934 std::string* fill, Input_section_list*)
936 Output_section* fill_section;
937 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
938 *dot_value, *dot_section,
939 &fill_section);
940 if (fill_section != NULL)
941 gold_warning(_("fill value is not absolute"));
942 // FIXME: The GNU linker supports fill values of arbitrary length.
943 unsigned char fill_buff[4];
944 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
945 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
948 // Print for debugging.
949 void
950 print(FILE* f) const
952 fprintf(f, " FILL(");
953 this->val_->print(f);
954 fprintf(f, ")\n");
957 private:
958 // The new fill value.
959 Expression* val_;
962 // Return whether STRING contains a wildcard character. This is used
963 // to speed up matching.
965 static inline bool
966 is_wildcard_string(const std::string& s)
968 return strpbrk(s.c_str(), "?*[") != NULL;
971 // An input section specification in an output section
973 class Output_section_element_input : public Output_section_element
975 public:
976 Output_section_element_input(const Input_section_spec* spec, bool keep);
978 // Finalize symbols--just update the value of the dot symbol.
979 void
980 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
981 Output_section** dot_section)
983 *dot_value = this->final_dot_value_;
984 *dot_section = this->final_dot_section_;
987 // See whether we match FILE_NAME and SECTION_NAME as an input
988 // section.
989 bool
990 match_name(const char* file_name, const char* section_name) const;
992 // Set the section address.
993 void
994 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
995 uint64_t subalign, uint64_t* dot_value,
996 Output_section**, std::string* fill,
997 Input_section_list*);
999 // Print for debugging.
1000 void
1001 print(FILE* f) const;
1003 private:
1004 // An input section pattern.
1005 struct Input_section_pattern
1007 std::string pattern;
1008 bool pattern_is_wildcard;
1009 Sort_wildcard sort;
1011 Input_section_pattern(const char* patterna, size_t patternlena,
1012 Sort_wildcard sorta)
1013 : pattern(patterna, patternlena),
1014 pattern_is_wildcard(is_wildcard_string(this->pattern)),
1015 sort(sorta)
1019 typedef std::vector<Input_section_pattern> Input_section_patterns;
1021 // Filename_exclusions is a pair of filename pattern and a bool
1022 // indicating whether the filename is a wildcard.
1023 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1025 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1026 // indicates whether this is a wildcard pattern.
1027 static inline bool
1028 match(const char* string, const char* pattern, bool is_wildcard_pattern)
1030 return (is_wildcard_pattern
1031 ? fnmatch(pattern, string, 0) == 0
1032 : strcmp(string, pattern) == 0);
1035 // See if we match a file name.
1036 bool
1037 match_file_name(const char* file_name) const;
1039 // The file name pattern. If this is the empty string, we match all
1040 // files.
1041 std::string filename_pattern_;
1042 // Whether the file name pattern is a wildcard.
1043 bool filename_is_wildcard_;
1044 // How the file names should be sorted. This may only be
1045 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1046 Sort_wildcard filename_sort_;
1047 // The list of file names to exclude.
1048 Filename_exclusions filename_exclusions_;
1049 // The list of input section patterns.
1050 Input_section_patterns input_section_patterns_;
1051 // Whether to keep this section when garbage collecting.
1052 bool keep_;
1053 // The value of dot after including all matching sections.
1054 uint64_t final_dot_value_;
1055 // The section where dot is defined after including all matching
1056 // sections.
1057 Output_section* final_dot_section_;
1060 // Construct Output_section_element_input. The parser records strings
1061 // as pointers into a copy of the script file, which will go away when
1062 // parsing is complete. We make sure they are in std::string objects.
1064 Output_section_element_input::Output_section_element_input(
1065 const Input_section_spec* spec,
1066 bool keep)
1067 : filename_pattern_(),
1068 filename_is_wildcard_(false),
1069 filename_sort_(spec->file.sort),
1070 filename_exclusions_(),
1071 input_section_patterns_(),
1072 keep_(keep),
1073 final_dot_value_(0),
1074 final_dot_section_(NULL)
1076 // The filename pattern "*" is common, and matches all files. Turn
1077 // it into the empty string.
1078 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1079 this->filename_pattern_.assign(spec->file.name.value,
1080 spec->file.name.length);
1081 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_);
1083 if (spec->input_sections.exclude != NULL)
1085 for (String_list::const_iterator p =
1086 spec->input_sections.exclude->begin();
1087 p != spec->input_sections.exclude->end();
1088 ++p)
1090 bool is_wildcard = is_wildcard_string(*p);
1091 this->filename_exclusions_.push_back(std::make_pair(*p,
1092 is_wildcard));
1096 if (spec->input_sections.sections != NULL)
1098 Input_section_patterns& isp(this->input_section_patterns_);
1099 for (String_sort_list::const_iterator p =
1100 spec->input_sections.sections->begin();
1101 p != spec->input_sections.sections->end();
1102 ++p)
1103 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1104 p->sort));
1108 // See whether we match FILE_NAME.
1110 bool
1111 Output_section_element_input::match_file_name(const char* file_name) const
1113 if (!this->filename_pattern_.empty())
1115 // If we were called with no filename, we refuse to match a
1116 // pattern which requires a file name.
1117 if (file_name == NULL)
1118 return false;
1120 if (!match(file_name, this->filename_pattern_.c_str(),
1121 this->filename_is_wildcard_))
1122 return false;
1125 if (file_name != NULL)
1127 // Now we have to see whether FILE_NAME matches one of the
1128 // exclusion patterns, if any.
1129 for (Filename_exclusions::const_iterator p =
1130 this->filename_exclusions_.begin();
1131 p != this->filename_exclusions_.end();
1132 ++p)
1134 if (match(file_name, p->first.c_str(), p->second))
1135 return false;
1139 return true;
1142 // See whether we match FILE_NAME and SECTION_NAME.
1144 bool
1145 Output_section_element_input::match_name(const char* file_name,
1146 const char* section_name) const
1148 if (!this->match_file_name(file_name))
1149 return false;
1151 // If there are no section name patterns, then we match.
1152 if (this->input_section_patterns_.empty())
1153 return true;
1155 // See whether we match the section name patterns.
1156 for (Input_section_patterns::const_iterator p =
1157 this->input_section_patterns_.begin();
1158 p != this->input_section_patterns_.end();
1159 ++p)
1161 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1162 return true;
1165 // We didn't match any section names, so we didn't match.
1166 return false;
1169 // Information we use to sort the input sections.
1171 class Input_section_info
1173 public:
1174 Input_section_info(const Output_section::Simple_input_section& input_section)
1175 : input_section_(input_section), section_name_(),
1176 size_(0), addralign_(1)
1179 // Return the simple input section.
1180 const Output_section::Simple_input_section&
1181 input_section() const
1182 { return this->input_section_; }
1184 // Return the object.
1185 Relobj*
1186 relobj() const
1187 { return this->input_section_.relobj(); }
1189 // Return the section index.
1190 unsigned int
1191 shndx()
1192 { return this->input_section_.shndx(); }
1194 // Return the section name.
1195 const std::string&
1196 section_name() const
1197 { return this->section_name_; }
1199 // Set the section name.
1200 void
1201 set_section_name(const std::string name)
1202 { this->section_name_ = name; }
1204 // Return the section size.
1205 uint64_t
1206 size() const
1207 { return this->size_; }
1209 // Set the section size.
1210 void
1211 set_size(uint64_t size)
1212 { this->size_ = size; }
1214 // Return the address alignment.
1215 uint64_t
1216 addralign() const
1217 { return this->addralign_; }
1219 // Set the address alignment.
1220 void
1221 set_addralign(uint64_t addralign)
1222 { this->addralign_ = addralign; }
1224 private:
1225 // Input section, can be a relaxed section.
1226 Output_section::Simple_input_section input_section_;
1227 // Name of the section.
1228 std::string section_name_;
1229 // Section size.
1230 uint64_t size_;
1231 // Address alignment.
1232 uint64_t addralign_;
1235 // A class to sort the input sections.
1237 class Input_section_sorter
1239 public:
1240 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1241 : filename_sort_(filename_sort), section_sort_(section_sort)
1244 bool
1245 operator()(const Input_section_info&, const Input_section_info&) const;
1247 private:
1248 Sort_wildcard filename_sort_;
1249 Sort_wildcard section_sort_;
1252 bool
1253 Input_section_sorter::operator()(const Input_section_info& isi1,
1254 const Input_section_info& isi2) const
1256 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1257 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1258 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1259 && isi1.addralign() == isi2.addralign()))
1261 if (isi1.section_name() != isi2.section_name())
1262 return isi1.section_name() < isi2.section_name();
1264 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1265 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1266 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1268 if (isi1.addralign() != isi2.addralign())
1269 return isi1.addralign() < isi2.addralign();
1271 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1273 if (isi1.relobj()->name() != isi2.relobj()->name())
1274 return (isi1.relobj()->name() < isi2.relobj()->name());
1277 // Otherwise we leave them in the same order.
1278 return false;
1281 // Set the section address. Look in INPUT_SECTIONS for sections which
1282 // match this spec, sort them as specified, and add them to the output
1283 // section.
1285 void
1286 Output_section_element_input::set_section_addresses(
1287 Symbol_table*,
1288 Layout* layout,
1289 Output_section* output_section,
1290 uint64_t subalign,
1291 uint64_t* dot_value,
1292 Output_section** dot_section,
1293 std::string* fill,
1294 Input_section_list* input_sections)
1296 // We build a list of sections which match each
1297 // Input_section_pattern.
1299 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1300 size_t input_pattern_count = this->input_section_patterns_.size();
1301 if (input_pattern_count == 0)
1302 input_pattern_count = 1;
1303 Matching_sections matching_sections(input_pattern_count);
1305 // Look through the list of sections for this output section. Add
1306 // each one which matches to one of the elements of
1307 // MATCHING_SECTIONS.
1309 Input_section_list::iterator p = input_sections->begin();
1310 while (p != input_sections->end())
1312 Relobj* relobj = p->relobj();
1313 unsigned int shndx = p->shndx();
1314 Input_section_info isi(*p);
1316 // Calling section_name and section_addralign is not very
1317 // efficient.
1319 // Lock the object so that we can get information about the
1320 // section. This is OK since we know we are single-threaded
1321 // here.
1323 const Task* task = reinterpret_cast<const Task*>(-1);
1324 Task_lock_obj<Object> tl(task, relobj);
1326 isi.set_section_name(relobj->section_name(shndx));
1327 if (p->is_relaxed_input_section())
1328 isi.set_size(p->relaxed_input_section()->data_size());
1329 else
1330 isi.set_size(relobj->section_size(shndx));
1331 isi.set_addralign(relobj->section_addralign(shndx));
1334 if (!this->match_file_name(relobj->name().c_str()))
1335 ++p;
1336 else if (this->input_section_patterns_.empty())
1338 matching_sections[0].push_back(isi);
1339 p = input_sections->erase(p);
1341 else
1343 size_t i;
1344 for (i = 0; i < input_pattern_count; ++i)
1346 const Input_section_pattern&
1347 isp(this->input_section_patterns_[i]);
1348 if (match(isi.section_name().c_str(), isp.pattern.c_str(),
1349 isp.pattern_is_wildcard))
1350 break;
1353 if (i >= this->input_section_patterns_.size())
1354 ++p;
1355 else
1357 matching_sections[i].push_back(isi);
1358 p = input_sections->erase(p);
1363 // Look through MATCHING_SECTIONS. Sort each one as specified,
1364 // using a stable sort so that we get the default order when
1365 // sections are otherwise equal. Add each input section to the
1366 // output section.
1368 for (size_t i = 0; i < input_pattern_count; ++i)
1370 if (matching_sections[i].empty())
1371 continue;
1373 gold_assert(output_section != NULL);
1375 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1376 if (isp.sort != SORT_WILDCARD_NONE
1377 || this->filename_sort_ != SORT_WILDCARD_NONE)
1378 std::stable_sort(matching_sections[i].begin(),
1379 matching_sections[i].end(),
1380 Input_section_sorter(this->filename_sort_,
1381 isp.sort));
1383 for (std::vector<Input_section_info>::const_iterator p =
1384 matching_sections[i].begin();
1385 p != matching_sections[i].end();
1386 ++p)
1388 uint64_t this_subalign = p->addralign();
1389 if (this_subalign < subalign)
1390 this_subalign = subalign;
1392 uint64_t address = align_address(*dot_value, this_subalign);
1394 if (address > *dot_value && !fill->empty())
1396 section_size_type length =
1397 convert_to_section_size_type(address - *dot_value);
1398 std::string this_fill = this->get_fill_string(fill, length);
1399 Output_section_data* posd = new Output_data_const(this_fill, 0);
1400 output_section->add_output_section_data(posd);
1401 layout->new_output_section_data_from_script(posd);
1404 output_section->add_input_section_for_script(p->input_section(),
1405 p->size(),
1406 this_subalign);
1408 *dot_value = address + p->size();
1412 this->final_dot_value_ = *dot_value;
1413 this->final_dot_section_ = *dot_section;
1416 // Print for debugging.
1418 void
1419 Output_section_element_input::print(FILE* f) const
1421 fprintf(f, " ");
1423 if (this->keep_)
1424 fprintf(f, "KEEP(");
1426 if (!this->filename_pattern_.empty())
1428 bool need_close_paren = false;
1429 switch (this->filename_sort_)
1431 case SORT_WILDCARD_NONE:
1432 break;
1433 case SORT_WILDCARD_BY_NAME:
1434 fprintf(f, "SORT_BY_NAME(");
1435 need_close_paren = true;
1436 break;
1437 default:
1438 gold_unreachable();
1441 fprintf(f, "%s", this->filename_pattern_.c_str());
1443 if (need_close_paren)
1444 fprintf(f, ")");
1447 if (!this->input_section_patterns_.empty()
1448 || !this->filename_exclusions_.empty())
1450 fprintf(f, "(");
1452 bool need_space = false;
1453 if (!this->filename_exclusions_.empty())
1455 fprintf(f, "EXCLUDE_FILE(");
1456 bool need_comma = false;
1457 for (Filename_exclusions::const_iterator p =
1458 this->filename_exclusions_.begin();
1459 p != this->filename_exclusions_.end();
1460 ++p)
1462 if (need_comma)
1463 fprintf(f, ", ");
1464 fprintf(f, "%s", p->first.c_str());
1465 need_comma = true;
1467 fprintf(f, ")");
1468 need_space = true;
1471 for (Input_section_patterns::const_iterator p =
1472 this->input_section_patterns_.begin();
1473 p != this->input_section_patterns_.end();
1474 ++p)
1476 if (need_space)
1477 fprintf(f, " ");
1479 int close_parens = 0;
1480 switch (p->sort)
1482 case SORT_WILDCARD_NONE:
1483 break;
1484 case SORT_WILDCARD_BY_NAME:
1485 fprintf(f, "SORT_BY_NAME(");
1486 close_parens = 1;
1487 break;
1488 case SORT_WILDCARD_BY_ALIGNMENT:
1489 fprintf(f, "SORT_BY_ALIGNMENT(");
1490 close_parens = 1;
1491 break;
1492 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1493 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1494 close_parens = 2;
1495 break;
1496 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1497 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1498 close_parens = 2;
1499 break;
1500 default:
1501 gold_unreachable();
1504 fprintf(f, "%s", p->pattern.c_str());
1506 for (int i = 0; i < close_parens; ++i)
1507 fprintf(f, ")");
1509 need_space = true;
1512 fprintf(f, ")");
1515 if (this->keep_)
1516 fprintf(f, ")");
1518 fprintf(f, "\n");
1521 // An output section.
1523 class Output_section_definition : public Sections_element
1525 public:
1526 typedef Output_section_element::Input_section_list Input_section_list;
1528 Output_section_definition(const char* name, size_t namelen,
1529 const Parser_output_section_header* header);
1531 // Finish the output section with the information in the trailer.
1532 void
1533 finish(const Parser_output_section_trailer* trailer);
1535 // Add a symbol to be defined.
1536 void
1537 add_symbol_assignment(const char* name, size_t length, Expression* value,
1538 bool provide, bool hidden);
1540 // Add an assignment to the special dot symbol.
1541 void
1542 add_dot_assignment(Expression* value);
1544 // Add an assertion.
1545 void
1546 add_assertion(Expression* check, const char* message, size_t messagelen);
1548 // Add a data item to the current output section.
1549 void
1550 add_data(int size, bool is_signed, Expression* val);
1552 // Add a setting for the fill value.
1553 void
1554 add_fill(Expression* val);
1556 // Add an input section specification.
1557 void
1558 add_input_section(const Input_section_spec* spec, bool keep);
1560 // Return whether the output section is relro.
1561 bool
1562 is_relro() const
1563 { return this->is_relro_; }
1565 // Record that the output section is relro.
1566 void
1567 set_is_relro()
1568 { this->is_relro_ = true; }
1570 // Create any required output sections.
1571 void
1572 create_sections(Layout*);
1574 // Add any symbols being defined to the symbol table.
1575 void
1576 add_symbols_to_table(Symbol_table* symtab);
1578 // Finalize symbols and check assertions.
1579 void
1580 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
1582 // Return the output section name to use for an input file name and
1583 // section name.
1584 const char*
1585 output_section_name(const char* file_name, const char* section_name,
1586 Output_section***);
1588 // Initialize OSP with an output section.
1589 void
1590 orphan_section_init(Orphan_section_placement* osp,
1591 Script_sections::Elements_iterator p)
1592 { osp->output_section_init(this->name_, this->output_section_, p); }
1594 // Set the section address.
1595 void
1596 set_section_addresses(Symbol_table* symtab, Layout* layout,
1597 uint64_t* dot_value, uint64_t* load_address);
1599 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1600 // this section is constrained, and the input sections do not match,
1601 // return the constraint, and set *POSD.
1602 Section_constraint
1603 check_constraint(Output_section_definition** posd);
1605 // See if this is the alternate output section for a constrained
1606 // output section. If it is, transfer the Output_section and return
1607 // true. Otherwise return false.
1608 bool
1609 alternate_constraint(Output_section_definition*, Section_constraint);
1611 // Get the list of segments to use for an allocated section when
1612 // using a PHDRS clause.
1613 Output_section*
1614 allocate_to_segment(String_list** phdrs_list, bool* orphan);
1616 // Look for an output section by name and return the address, the
1617 // load address, the alignment, and the size. This is used when an
1618 // expression refers to an output section which was not actually
1619 // created. This returns true if the section was found, false
1620 // otherwise.
1621 bool
1622 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1623 uint64_t*) const;
1625 // Return the associated Output_section if there is one.
1626 Output_section*
1627 get_output_section() const
1628 { return this->output_section_; }
1630 // Print the contents to the FILE. This is for debugging.
1631 void
1632 print(FILE*) const;
1634 private:
1635 typedef std::vector<Output_section_element*> Output_section_elements;
1637 // The output section name.
1638 std::string name_;
1639 // The address. This may be NULL.
1640 Expression* address_;
1641 // The load address. This may be NULL.
1642 Expression* load_address_;
1643 // The alignment. This may be NULL.
1644 Expression* align_;
1645 // The input section alignment. This may be NULL.
1646 Expression* subalign_;
1647 // The constraint, if any.
1648 Section_constraint constraint_;
1649 // The fill value. This may be NULL.
1650 Expression* fill_;
1651 // The list of segments this section should go into. This may be
1652 // NULL.
1653 String_list* phdrs_;
1654 // The list of elements defining the section.
1655 Output_section_elements elements_;
1656 // The Output_section created for this definition. This will be
1657 // NULL if none was created.
1658 Output_section* output_section_;
1659 // The address after it has been evaluated.
1660 uint64_t evaluated_address_;
1661 // The load address after it has been evaluated.
1662 uint64_t evaluated_load_address_;
1663 // The alignment after it has been evaluated.
1664 uint64_t evaluated_addralign_;
1665 // The output section is relro.
1666 bool is_relro_;
1669 // Constructor.
1671 Output_section_definition::Output_section_definition(
1672 const char* name,
1673 size_t namelen,
1674 const Parser_output_section_header* header)
1675 : name_(name, namelen),
1676 address_(header->address),
1677 load_address_(header->load_address),
1678 align_(header->align),
1679 subalign_(header->subalign),
1680 constraint_(header->constraint),
1681 fill_(NULL),
1682 phdrs_(NULL),
1683 elements_(),
1684 output_section_(NULL),
1685 evaluated_address_(0),
1686 evaluated_load_address_(0),
1687 evaluated_addralign_(0),
1688 is_relro_(false)
1692 // Finish an output section.
1694 void
1695 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1697 this->fill_ = trailer->fill;
1698 this->phdrs_ = trailer->phdrs;
1701 // Add a symbol to be defined.
1703 void
1704 Output_section_definition::add_symbol_assignment(const char* name,
1705 size_t length,
1706 Expression* value,
1707 bool provide,
1708 bool hidden)
1710 Output_section_element* p = new Output_section_element_assignment(name,
1711 length,
1712 value,
1713 provide,
1714 hidden);
1715 this->elements_.push_back(p);
1718 // Add an assignment to the special dot symbol.
1720 void
1721 Output_section_definition::add_dot_assignment(Expression* value)
1723 Output_section_element* p = new Output_section_element_dot_assignment(value);
1724 this->elements_.push_back(p);
1727 // Add an assertion.
1729 void
1730 Output_section_definition::add_assertion(Expression* check,
1731 const char* message,
1732 size_t messagelen)
1734 Output_section_element* p = new Output_section_element_assertion(check,
1735 message,
1736 messagelen);
1737 this->elements_.push_back(p);
1740 // Add a data item to the current output section.
1742 void
1743 Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1745 Output_section_element* p = new Output_section_element_data(size, is_signed,
1746 val);
1747 this->elements_.push_back(p);
1750 // Add a setting for the fill value.
1752 void
1753 Output_section_definition::add_fill(Expression* val)
1755 Output_section_element* p = new Output_section_element_fill(val);
1756 this->elements_.push_back(p);
1759 // Add an input section specification.
1761 void
1762 Output_section_definition::add_input_section(const Input_section_spec* spec,
1763 bool keep)
1765 Output_section_element* p = new Output_section_element_input(spec, keep);
1766 this->elements_.push_back(p);
1769 // Create any required output sections. We need an output section if
1770 // there is a data statement here.
1772 void
1773 Output_section_definition::create_sections(Layout* layout)
1775 if (this->output_section_ != NULL)
1776 return;
1777 for (Output_section_elements::const_iterator p = this->elements_.begin();
1778 p != this->elements_.end();
1779 ++p)
1781 if ((*p)->needs_output_section())
1783 const char* name = this->name_.c_str();
1784 this->output_section_ = layout->make_output_section_for_script(name);
1785 return;
1790 // Add any symbols being defined to the symbol table.
1792 void
1793 Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
1795 for (Output_section_elements::iterator p = this->elements_.begin();
1796 p != this->elements_.end();
1797 ++p)
1798 (*p)->add_symbols_to_table(symtab);
1801 // Finalize symbols and check assertions.
1803 void
1804 Output_section_definition::finalize_symbols(Symbol_table* symtab,
1805 const Layout* layout,
1806 uint64_t* dot_value)
1808 if (this->output_section_ != NULL)
1809 *dot_value = this->output_section_->address();
1810 else
1812 uint64_t address = *dot_value;
1813 if (this->address_ != NULL)
1815 Output_section* dummy;
1816 address = this->address_->eval_with_dot(symtab, layout, true,
1817 *dot_value, NULL,
1818 &dummy);
1820 if (this->align_ != NULL)
1822 Output_section* dummy;
1823 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
1824 *dot_value,
1825 NULL,
1826 &dummy);
1827 address = align_address(address, align);
1829 *dot_value = address;
1832 Output_section* dot_section = this->output_section_;
1833 for (Output_section_elements::iterator p = this->elements_.begin();
1834 p != this->elements_.end();
1835 ++p)
1836 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
1839 // Return the output section name to use for an input section name.
1841 const char*
1842 Output_section_definition::output_section_name(const char* file_name,
1843 const char* section_name,
1844 Output_section*** slot)
1846 // Ask each element whether it matches NAME.
1847 for (Output_section_elements::const_iterator p = this->elements_.begin();
1848 p != this->elements_.end();
1849 ++p)
1851 if ((*p)->match_name(file_name, section_name))
1853 // We found a match for NAME, which means that it should go
1854 // into this output section.
1855 *slot = &this->output_section_;
1856 return this->name_.c_str();
1860 // We don't know about this section name.
1861 return NULL;
1864 // Set the section address. Note that the OUTPUT_SECTION_ field will
1865 // be NULL if no input sections were mapped to this output section.
1866 // We still have to adjust dot and process symbol assignments.
1868 void
1869 Output_section_definition::set_section_addresses(Symbol_table* symtab,
1870 Layout* layout,
1871 uint64_t* dot_value,
1872 uint64_t* load_address)
1874 uint64_t address;
1875 if (this->address_ == NULL)
1876 address = *dot_value;
1877 else
1879 Output_section* dummy;
1880 address = this->address_->eval_with_dot(symtab, layout, true,
1881 *dot_value, NULL, &dummy);
1884 uint64_t align;
1885 if (this->align_ == NULL)
1887 if (this->output_section_ == NULL)
1888 align = 0;
1889 else
1890 align = this->output_section_->addralign();
1892 else
1894 Output_section* align_section;
1895 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
1896 NULL, &align_section);
1897 if (align_section != NULL)
1898 gold_warning(_("alignment of section %s is not absolute"),
1899 this->name_.c_str());
1900 if (this->output_section_ != NULL)
1901 this->output_section_->set_addralign(align);
1904 address = align_address(address, align);
1906 uint64_t start_address = address;
1908 *dot_value = address;
1910 // The address of non-SHF_ALLOC sections is forced to zero,
1911 // regardless of what the linker script wants.
1912 if (this->output_section_ != NULL
1913 && (this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0)
1914 this->output_section_->set_address(address);
1916 this->evaluated_address_ = address;
1917 this->evaluated_addralign_ = align;
1919 if (this->load_address_ == NULL)
1920 this->evaluated_load_address_ = address;
1921 else
1923 Output_section* dummy;
1924 uint64_t laddr =
1925 this->load_address_->eval_with_dot(symtab, layout, true, *dot_value,
1926 this->output_section_, &dummy);
1927 if (this->output_section_ != NULL)
1928 this->output_section_->set_load_address(laddr);
1929 this->evaluated_load_address_ = laddr;
1932 uint64_t subalign;
1933 if (this->subalign_ == NULL)
1934 subalign = 0;
1935 else
1937 Output_section* subalign_section;
1938 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
1939 *dot_value, NULL,
1940 &subalign_section);
1941 if (subalign_section != NULL)
1942 gold_warning(_("subalign of section %s is not absolute"),
1943 this->name_.c_str());
1946 std::string fill;
1947 if (this->fill_ != NULL)
1949 // FIXME: The GNU linker supports fill values of arbitrary
1950 // length.
1951 Output_section* fill_section;
1952 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
1953 *dot_value,
1954 NULL,
1955 &fill_section);
1956 if (fill_section != NULL)
1957 gold_warning(_("fill of section %s is not absolute"),
1958 this->name_.c_str());
1959 unsigned char fill_buff[4];
1960 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1961 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
1964 Input_section_list input_sections;
1965 if (this->output_section_ != NULL)
1967 // Get the list of input sections attached to this output
1968 // section. This will leave the output section with only
1969 // Output_section_data entries.
1970 address += this->output_section_->get_input_sections(address,
1971 fill,
1972 &input_sections);
1973 *dot_value = address;
1976 Output_section* dot_section = this->output_section_;
1977 for (Output_section_elements::iterator p = this->elements_.begin();
1978 p != this->elements_.end();
1979 ++p)
1980 (*p)->set_section_addresses(symtab, layout, this->output_section_,
1981 subalign, dot_value, &dot_section, &fill,
1982 &input_sections);
1984 gold_assert(input_sections.empty());
1986 if (this->load_address_ == NULL || this->output_section_ == NULL)
1987 *load_address = *dot_value;
1988 else
1989 *load_address = (this->output_section_->load_address()
1990 + (*dot_value - start_address));
1992 if (this->output_section_ != NULL)
1994 if (this->is_relro_)
1995 this->output_section_->set_is_relro();
1996 else
1997 this->output_section_->clear_is_relro();
2001 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2002 // this section is constrained, and the input sections do not match,
2003 // return the constraint, and set *POSD.
2005 Section_constraint
2006 Output_section_definition::check_constraint(Output_section_definition** posd)
2008 switch (this->constraint_)
2010 case CONSTRAINT_NONE:
2011 return CONSTRAINT_NONE;
2013 case CONSTRAINT_ONLY_IF_RO:
2014 if (this->output_section_ != NULL
2015 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2017 *posd = this;
2018 return CONSTRAINT_ONLY_IF_RO;
2020 return CONSTRAINT_NONE;
2022 case CONSTRAINT_ONLY_IF_RW:
2023 if (this->output_section_ != NULL
2024 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2026 *posd = this;
2027 return CONSTRAINT_ONLY_IF_RW;
2029 return CONSTRAINT_NONE;
2031 case CONSTRAINT_SPECIAL:
2032 if (this->output_section_ != NULL)
2033 gold_error(_("SPECIAL constraints are not implemented"));
2034 return CONSTRAINT_NONE;
2036 default:
2037 gold_unreachable();
2041 // See if this is the alternate output section for a constrained
2042 // output section. If it is, transfer the Output_section and return
2043 // true. Otherwise return false.
2045 bool
2046 Output_section_definition::alternate_constraint(
2047 Output_section_definition* posd,
2048 Section_constraint constraint)
2050 if (this->name_ != posd->name_)
2051 return false;
2053 switch (constraint)
2055 case CONSTRAINT_ONLY_IF_RO:
2056 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2057 return false;
2058 break;
2060 case CONSTRAINT_ONLY_IF_RW:
2061 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2062 return false;
2063 break;
2065 default:
2066 gold_unreachable();
2069 // We have found the alternate constraint. We just need to move
2070 // over the Output_section. When constraints are used properly,
2071 // THIS should not have an output_section pointer, as all the input
2072 // sections should have matched the other definition.
2074 if (this->output_section_ != NULL)
2075 gold_error(_("mismatched definition for constrained sections"));
2077 this->output_section_ = posd->output_section_;
2078 posd->output_section_ = NULL;
2080 if (this->is_relro_)
2081 this->output_section_->set_is_relro();
2082 else
2083 this->output_section_->clear_is_relro();
2085 return true;
2088 // Get the list of segments to use for an allocated section when using
2089 // a PHDRS clause.
2091 Output_section*
2092 Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2093 bool* orphan)
2095 if (this->output_section_ == NULL)
2096 return NULL;
2097 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2098 return NULL;
2099 *orphan = false;
2100 if (this->phdrs_ != NULL)
2101 *phdrs_list = this->phdrs_;
2102 return this->output_section_;
2105 // Look for an output section by name and return the address, the load
2106 // address, the alignment, and the size. This is used when an
2107 // expression refers to an output section which was not actually
2108 // created. This returns true if the section was found, false
2109 // otherwise.
2111 bool
2112 Output_section_definition::get_output_section_info(const char* name,
2113 uint64_t* address,
2114 uint64_t* load_address,
2115 uint64_t* addralign,
2116 uint64_t* size) const
2118 if (this->name_ != name)
2119 return false;
2121 if (this->output_section_ != NULL)
2123 *address = this->output_section_->address();
2124 if (this->output_section_->has_load_address())
2125 *load_address = this->output_section_->load_address();
2126 else
2127 *load_address = *address;
2128 *addralign = this->output_section_->addralign();
2129 *size = this->output_section_->current_data_size();
2131 else
2133 *address = this->evaluated_address_;
2134 *load_address = this->evaluated_load_address_;
2135 *addralign = this->evaluated_addralign_;
2136 *size = 0;
2139 return true;
2142 // Print for debugging.
2144 void
2145 Output_section_definition::print(FILE* f) const
2147 fprintf(f, " %s ", this->name_.c_str());
2149 if (this->address_ != NULL)
2151 this->address_->print(f);
2152 fprintf(f, " ");
2155 fprintf(f, ": ");
2157 if (this->load_address_ != NULL)
2159 fprintf(f, "AT(");
2160 this->load_address_->print(f);
2161 fprintf(f, ") ");
2164 if (this->align_ != NULL)
2166 fprintf(f, "ALIGN(");
2167 this->align_->print(f);
2168 fprintf(f, ") ");
2171 if (this->subalign_ != NULL)
2173 fprintf(f, "SUBALIGN(");
2174 this->subalign_->print(f);
2175 fprintf(f, ") ");
2178 fprintf(f, "{\n");
2180 for (Output_section_elements::const_iterator p = this->elements_.begin();
2181 p != this->elements_.end();
2182 ++p)
2183 (*p)->print(f);
2185 fprintf(f, " }");
2187 if (this->fill_ != NULL)
2189 fprintf(f, " = ");
2190 this->fill_->print(f);
2193 if (this->phdrs_ != NULL)
2195 for (String_list::const_iterator p = this->phdrs_->begin();
2196 p != this->phdrs_->end();
2197 ++p)
2198 fprintf(f, " :%s", p->c_str());
2201 fprintf(f, "\n");
2204 // An output section created to hold orphaned input sections. These
2205 // do not actually appear in linker scripts. However, for convenience
2206 // when setting the output section addresses, we put a marker to these
2207 // sections in the appropriate place in the list of SECTIONS elements.
2209 class Orphan_output_section : public Sections_element
2211 public:
2212 Orphan_output_section(Output_section* os)
2213 : os_(os)
2216 // Return whether the orphan output section is relro. We can just
2217 // check the output section because we always set the flag, if
2218 // needed, just after we create the Orphan_output_section.
2219 bool
2220 is_relro() const
2221 { return this->os_->is_relro(); }
2223 // Initialize OSP with an output section. This should have been
2224 // done already.
2225 void
2226 orphan_section_init(Orphan_section_placement*,
2227 Script_sections::Elements_iterator)
2228 { gold_unreachable(); }
2230 // Set section addresses.
2231 void
2232 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*);
2234 // Get the list of segments to use for an allocated section when
2235 // using a PHDRS clause.
2236 Output_section*
2237 allocate_to_segment(String_list**, bool*);
2239 // Return the associated Output_section.
2240 Output_section*
2241 get_output_section() const
2242 { return this->os_; }
2244 // Print for debugging.
2245 void
2246 print(FILE* f) const
2248 fprintf(f, " marker for orphaned output section %s\n",
2249 this->os_->name());
2252 private:
2253 Output_section* os_;
2256 // Set section addresses.
2258 void
2259 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
2260 uint64_t* dot_value,
2261 uint64_t* load_address)
2263 typedef std::list<Output_section::Simple_input_section> Input_section_list;
2265 bool have_load_address = *load_address != *dot_value;
2267 uint64_t address = *dot_value;
2268 address = align_address(address, this->os_->addralign());
2270 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
2272 this->os_->set_address(address);
2273 if (have_load_address)
2274 this->os_->set_load_address(align_address(*load_address,
2275 this->os_->addralign()));
2278 Input_section_list input_sections;
2279 address += this->os_->get_input_sections(address, "", &input_sections);
2281 for (Input_section_list::iterator p = input_sections.begin();
2282 p != input_sections.end();
2283 ++p)
2285 uint64_t addralign;
2286 uint64_t size;
2288 // We know what are single-threaded, so it is OK to lock the
2289 // object.
2291 const Task* task = reinterpret_cast<const Task*>(-1);
2292 Task_lock_obj<Object> tl(task, p->relobj());
2293 addralign = p->relobj()->section_addralign(p->shndx());
2294 if (p->is_relaxed_input_section())
2295 size = p->relaxed_input_section()->data_size();
2296 else
2297 size = p->relobj()->section_size(p->shndx());
2300 address = align_address(address, addralign);
2301 this->os_->add_input_section_for_script(*p, size, addralign);
2302 address += size;
2305 if (!have_load_address)
2306 *load_address = address;
2307 else
2308 *load_address += address - *dot_value;
2310 *dot_value = address;
2313 // Get the list of segments to use for an allocated section when using
2314 // a PHDRS clause. If this is an allocated section, return the
2315 // Output_section. We don't change the list of segments.
2317 Output_section*
2318 Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
2320 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2321 return NULL;
2322 *orphan = true;
2323 return this->os_;
2326 // Class Phdrs_element. A program header from a PHDRS clause.
2328 class Phdrs_element
2330 public:
2331 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2332 bool includes_filehdr, bool includes_phdrs,
2333 bool is_flags_valid, unsigned int flags,
2334 Expression* load_address)
2335 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2336 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2337 flags_(flags), load_address_(load_address), load_address_value_(0),
2338 segment_(NULL)
2341 // Return the name of this segment.
2342 const std::string&
2343 name() const
2344 { return this->name_; }
2346 // Return the type of the segment.
2347 unsigned int
2348 type() const
2349 { return this->type_; }
2351 // Whether to include the file header.
2352 bool
2353 includes_filehdr() const
2354 { return this->includes_filehdr_; }
2356 // Whether to include the program headers.
2357 bool
2358 includes_phdrs() const
2359 { return this->includes_phdrs_; }
2361 // Return whether there is a load address.
2362 bool
2363 has_load_address() const
2364 { return this->load_address_ != NULL; }
2366 // Evaluate the load address expression if there is one.
2367 void
2368 eval_load_address(Symbol_table* symtab, Layout* layout)
2370 if (this->load_address_ != NULL)
2371 this->load_address_value_ = this->load_address_->eval(symtab, layout,
2372 true);
2375 // Return the load address.
2376 uint64_t
2377 load_address() const
2379 gold_assert(this->load_address_ != NULL);
2380 return this->load_address_value_;
2383 // Create the segment.
2384 Output_segment*
2385 create_segment(Layout* layout)
2387 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2388 return this->segment_;
2391 // Return the segment.
2392 Output_segment*
2393 segment()
2394 { return this->segment_; }
2396 // Release the segment.
2397 void
2398 release_segment()
2399 { this->segment_ = NULL; }
2401 // Set the segment flags if appropriate.
2402 void
2403 set_flags_if_valid()
2405 if (this->is_flags_valid_)
2406 this->segment_->set_flags(this->flags_);
2409 // Print for debugging.
2410 void
2411 print(FILE*) const;
2413 private:
2414 // The name used in the script.
2415 std::string name_;
2416 // The type of the segment (PT_LOAD, etc.).
2417 unsigned int type_;
2418 // Whether this segment includes the file header.
2419 bool includes_filehdr_;
2420 // Whether this segment includes the section headers.
2421 bool includes_phdrs_;
2422 // Whether the flags were explicitly specified.
2423 bool is_flags_valid_;
2424 // The flags for this segment (PF_R, etc.) if specified.
2425 unsigned int flags_;
2426 // The expression for the load address for this segment. This may
2427 // be NULL.
2428 Expression* load_address_;
2429 // The actual load address from evaluating the expression.
2430 uint64_t load_address_value_;
2431 // The segment itself.
2432 Output_segment* segment_;
2435 // Print for debugging.
2437 void
2438 Phdrs_element::print(FILE* f) const
2440 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
2441 if (this->includes_filehdr_)
2442 fprintf(f, " FILEHDR");
2443 if (this->includes_phdrs_)
2444 fprintf(f, " PHDRS");
2445 if (this->is_flags_valid_)
2446 fprintf(f, " FLAGS(%u)", this->flags_);
2447 if (this->load_address_ != NULL)
2449 fprintf(f, " AT(");
2450 this->load_address_->print(f);
2451 fprintf(f, ")");
2453 fprintf(f, ";\n");
2456 // Class Script_sections.
2458 Script_sections::Script_sections()
2459 : saw_sections_clause_(false),
2460 in_sections_clause_(false),
2461 sections_elements_(NULL),
2462 output_section_(NULL),
2463 phdrs_elements_(NULL),
2464 orphan_section_placement_(NULL),
2465 data_segment_align_start_(),
2466 saw_data_segment_align_(false),
2467 saw_relro_end_(false)
2471 // Start a SECTIONS clause.
2473 void
2474 Script_sections::start_sections()
2476 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2477 this->saw_sections_clause_ = true;
2478 this->in_sections_clause_ = true;
2479 if (this->sections_elements_ == NULL)
2480 this->sections_elements_ = new Sections_elements;
2483 // Finish a SECTIONS clause.
2485 void
2486 Script_sections::finish_sections()
2488 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2489 this->in_sections_clause_ = false;
2492 // Add a symbol to be defined.
2494 void
2495 Script_sections::add_symbol_assignment(const char* name, size_t length,
2496 Expression* val, bool provide,
2497 bool hidden)
2499 if (this->output_section_ != NULL)
2500 this->output_section_->add_symbol_assignment(name, length, val,
2501 provide, hidden);
2502 else
2504 Sections_element* p = new Sections_element_assignment(name, length,
2505 val, provide,
2506 hidden);
2507 this->sections_elements_->push_back(p);
2511 // Add an assignment to the special dot symbol.
2513 void
2514 Script_sections::add_dot_assignment(Expression* val)
2516 if (this->output_section_ != NULL)
2517 this->output_section_->add_dot_assignment(val);
2518 else
2520 Sections_element* p = new Sections_element_dot_assignment(val);
2521 this->sections_elements_->push_back(p);
2525 // Add an assertion.
2527 void
2528 Script_sections::add_assertion(Expression* check, const char* message,
2529 size_t messagelen)
2531 if (this->output_section_ != NULL)
2532 this->output_section_->add_assertion(check, message, messagelen);
2533 else
2535 Sections_element* p = new Sections_element_assertion(check, message,
2536 messagelen);
2537 this->sections_elements_->push_back(p);
2541 // Start processing entries for an output section.
2543 void
2544 Script_sections::start_output_section(
2545 const char* name,
2546 size_t namelen,
2547 const Parser_output_section_header *header)
2549 Output_section_definition* posd = new Output_section_definition(name,
2550 namelen,
2551 header);
2552 this->sections_elements_->push_back(posd);
2553 gold_assert(this->output_section_ == NULL);
2554 this->output_section_ = posd;
2557 // Stop processing entries for an output section.
2559 void
2560 Script_sections::finish_output_section(
2561 const Parser_output_section_trailer* trailer)
2563 gold_assert(this->output_section_ != NULL);
2564 this->output_section_->finish(trailer);
2565 this->output_section_ = NULL;
2568 // Add a data item to the current output section.
2570 void
2571 Script_sections::add_data(int size, bool is_signed, Expression* val)
2573 gold_assert(this->output_section_ != NULL);
2574 this->output_section_->add_data(size, is_signed, val);
2577 // Add a fill value setting to the current output section.
2579 void
2580 Script_sections::add_fill(Expression* val)
2582 gold_assert(this->output_section_ != NULL);
2583 this->output_section_->add_fill(val);
2586 // Add an input section specification to the current output section.
2588 void
2589 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2591 gold_assert(this->output_section_ != NULL);
2592 this->output_section_->add_input_section(spec, keep);
2595 // This is called when we see DATA_SEGMENT_ALIGN. It means that any
2596 // subsequent output sections may be relro.
2598 void
2599 Script_sections::data_segment_align()
2601 if (this->saw_data_segment_align_)
2602 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
2603 gold_assert(!this->sections_elements_->empty());
2604 Sections_elements::iterator p = this->sections_elements_->end();
2605 --p;
2606 this->data_segment_align_start_ = p;
2607 this->saw_data_segment_align_ = true;
2610 // This is called when we see DATA_SEGMENT_RELRO_END. It means that
2611 // any output sections seen since DATA_SEGMENT_ALIGN are relro.
2613 void
2614 Script_sections::data_segment_relro_end()
2616 if (this->saw_relro_end_)
2617 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2618 "in a linker script"));
2619 this->saw_relro_end_ = true;
2621 if (!this->saw_data_segment_align_)
2622 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2623 else
2625 Sections_elements::iterator p = this->data_segment_align_start_;
2626 for (++p; p != this->sections_elements_->end(); ++p)
2627 (*p)->set_is_relro();
2631 // Create any required sections.
2633 void
2634 Script_sections::create_sections(Layout* layout)
2636 if (!this->saw_sections_clause_)
2637 return;
2638 for (Sections_elements::iterator p = this->sections_elements_->begin();
2639 p != this->sections_elements_->end();
2640 ++p)
2641 (*p)->create_sections(layout);
2644 // Add any symbols we are defining to the symbol table.
2646 void
2647 Script_sections::add_symbols_to_table(Symbol_table* symtab)
2649 if (!this->saw_sections_clause_)
2650 return;
2651 for (Sections_elements::iterator p = this->sections_elements_->begin();
2652 p != this->sections_elements_->end();
2653 ++p)
2654 (*p)->add_symbols_to_table(symtab);
2657 // Finalize symbols and check assertions.
2659 void
2660 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2662 if (!this->saw_sections_clause_)
2663 return;
2664 uint64_t dot_value = 0;
2665 for (Sections_elements::iterator p = this->sections_elements_->begin();
2666 p != this->sections_elements_->end();
2667 ++p)
2668 (*p)->finalize_symbols(symtab, layout, &dot_value);
2671 // Return the name of the output section to use for an input file name
2672 // and section name.
2674 const char*
2675 Script_sections::output_section_name(const char* file_name,
2676 const char* section_name,
2677 Output_section*** output_section_slot)
2679 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2680 p != this->sections_elements_->end();
2681 ++p)
2683 const char* ret = (*p)->output_section_name(file_name, section_name,
2684 output_section_slot);
2686 if (ret != NULL)
2688 // The special name /DISCARD/ means that the input section
2689 // should be discarded.
2690 if (strcmp(ret, "/DISCARD/") == 0)
2692 *output_section_slot = NULL;
2693 return NULL;
2695 return ret;
2699 // If we couldn't find a mapping for the name, the output section
2700 // gets the name of the input section.
2702 *output_section_slot = NULL;
2704 return section_name;
2707 // Place a marker for an orphan output section into the SECTIONS
2708 // clause.
2710 void
2711 Script_sections::place_orphan(Output_section* os)
2713 Orphan_section_placement* osp = this->orphan_section_placement_;
2714 if (osp == NULL)
2716 // Initialize the Orphan_section_placement structure.
2717 osp = new Orphan_section_placement();
2718 for (Sections_elements::iterator p = this->sections_elements_->begin();
2719 p != this->sections_elements_->end();
2720 ++p)
2721 (*p)->orphan_section_init(osp, p);
2722 gold_assert(!this->sections_elements_->empty());
2723 Sections_elements::iterator last = this->sections_elements_->end();
2724 --last;
2725 osp->last_init(last);
2726 this->orphan_section_placement_ = osp;
2729 Orphan_output_section* orphan = new Orphan_output_section(os);
2731 // Look for where to put ORPHAN.
2732 Sections_elements::iterator* where;
2733 if (osp->find_place(os, &where))
2735 if ((**where)->is_relro())
2736 os->set_is_relro();
2737 else
2738 os->clear_is_relro();
2740 // We want to insert ORPHAN after *WHERE, and then update *WHERE
2741 // so that the next one goes after this one.
2742 Sections_elements::iterator p = *where;
2743 gold_assert(p != this->sections_elements_->end());
2744 ++p;
2745 *where = this->sections_elements_->insert(p, orphan);
2747 else
2749 os->clear_is_relro();
2750 // We don't have a place to put this orphan section. Put it,
2751 // and all other sections like it, at the end, but before the
2752 // sections which always come at the end.
2753 Sections_elements::iterator last = osp->last_place();
2754 *where = this->sections_elements_->insert(last, orphan);
2758 // Set the addresses of all the output sections. Walk through all the
2759 // elements, tracking the dot symbol. Apply assignments which set
2760 // absolute symbol values, in case they are used when setting dot.
2761 // Fill in data statement values. As we find output sections, set the
2762 // address, set the address of all associated input sections, and
2763 // update dot. Return the segment which should hold the file header
2764 // and segment headers, if any.
2766 Output_segment*
2767 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2769 gold_assert(this->saw_sections_clause_);
2771 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2772 // for our representation.
2773 for (Sections_elements::iterator p = this->sections_elements_->begin();
2774 p != this->sections_elements_->end();
2775 ++p)
2777 Output_section_definition* posd;
2778 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2779 if (failed_constraint != CONSTRAINT_NONE)
2781 Sections_elements::iterator q;
2782 for (q = this->sections_elements_->begin();
2783 q != this->sections_elements_->end();
2784 ++q)
2786 if (q != p)
2788 if ((*q)->alternate_constraint(posd, failed_constraint))
2789 break;
2793 if (q == this->sections_elements_->end())
2794 gold_error(_("no matching section constraint"));
2798 // Force the alignment of the first TLS section to be the maximum
2799 // alignment of all TLS sections.
2800 Output_section* first_tls = NULL;
2801 uint64_t tls_align = 0;
2802 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2803 p != this->sections_elements_->end();
2804 ++p)
2806 Output_section *os = (*p)->get_output_section();
2807 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
2809 if (first_tls == NULL)
2810 first_tls = os;
2811 if (os->addralign() > tls_align)
2812 tls_align = os->addralign();
2815 if (first_tls != NULL)
2816 first_tls->set_addralign(tls_align);
2818 // For a relocatable link, we implicitly set dot to zero.
2819 uint64_t dot_value = 0;
2820 uint64_t load_address = 0;
2821 for (Sections_elements::iterator p = this->sections_elements_->begin();
2822 p != this->sections_elements_->end();
2823 ++p)
2824 (*p)->set_section_addresses(symtab, layout, &dot_value, &load_address);
2826 if (this->phdrs_elements_ != NULL)
2828 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2829 p != this->phdrs_elements_->end();
2830 ++p)
2831 (*p)->eval_load_address(symtab, layout);
2834 return this->create_segments(layout);
2837 // Sort the sections in order to put them into segments.
2839 class Sort_output_sections
2841 public:
2842 bool
2843 operator()(const Output_section* os1, const Output_section* os2) const;
2846 bool
2847 Sort_output_sections::operator()(const Output_section* os1,
2848 const Output_section* os2) const
2850 // Sort first by the load address.
2851 uint64_t lma1 = (os1->has_load_address()
2852 ? os1->load_address()
2853 : os1->address());
2854 uint64_t lma2 = (os2->has_load_address()
2855 ? os2->load_address()
2856 : os2->address());
2857 if (lma1 != lma2)
2858 return lma1 < lma2;
2860 // Then sort by the virtual address.
2861 if (os1->address() != os2->address())
2862 return os1->address() < os2->address();
2864 // Sort TLS sections to the end.
2865 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
2866 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
2867 if (tls1 != tls2)
2868 return tls2;
2870 // Sort PROGBITS before NOBITS.
2871 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
2872 return true;
2873 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
2874 return false;
2876 // Otherwise we don't care.
2877 return false;
2880 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2881 // We treat a section with the SHF_TLS flag set as taking up space
2882 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2883 // space for them in the file.
2885 bool
2886 Script_sections::is_bss_section(const Output_section* os)
2888 return (os->type() == elfcpp::SHT_NOBITS
2889 && (os->flags() & elfcpp::SHF_TLS) == 0);
2892 // Return the size taken by the file header and the program headers.
2894 size_t
2895 Script_sections::total_header_size(Layout* layout) const
2897 size_t segment_count = layout->segment_count();
2898 size_t file_header_size;
2899 size_t segment_headers_size;
2900 if (parameters->target().get_size() == 32)
2902 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
2903 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
2905 else if (parameters->target().get_size() == 64)
2907 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
2908 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
2910 else
2911 gold_unreachable();
2913 return file_header_size + segment_headers_size;
2916 // Return the amount we have to subtract from the LMA to accomodate
2917 // headers of the given size. The complication is that the file
2918 // header have to be at the start of a page, as otherwise it will not
2919 // be at the start of the file.
2921 uint64_t
2922 Script_sections::header_size_adjustment(uint64_t lma,
2923 size_t sizeof_headers) const
2925 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
2926 uint64_t hdr_lma = lma - sizeof_headers;
2927 hdr_lma &= ~(abi_pagesize - 1);
2928 return lma - hdr_lma;
2931 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2932 // the segment which should hold the file header and segment headers,
2933 // if any.
2935 Output_segment*
2936 Script_sections::create_segments(Layout* layout)
2938 gold_assert(this->saw_sections_clause_);
2940 if (parameters->options().relocatable())
2941 return NULL;
2943 if (this->saw_phdrs_clause())
2944 return create_segments_from_phdrs_clause(layout);
2946 Layout::Section_list sections;
2947 layout->get_allocated_sections(&sections);
2949 // Sort the sections by address.
2950 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
2952 this->create_note_and_tls_segments(layout, &sections);
2954 // Walk through the sections adding them to PT_LOAD segments.
2955 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
2956 Output_segment* first_seg = NULL;
2957 Output_segment* current_seg = NULL;
2958 bool is_current_seg_readonly = true;
2959 Layout::Section_list::iterator plast = sections.end();
2960 uint64_t last_vma = 0;
2961 uint64_t last_lma = 0;
2962 uint64_t last_size = 0;
2963 for (Layout::Section_list::iterator p = sections.begin();
2964 p != sections.end();
2965 ++p)
2967 const uint64_t vma = (*p)->address();
2968 const uint64_t lma = ((*p)->has_load_address()
2969 ? (*p)->load_address()
2970 : vma);
2971 const uint64_t size = (*p)->current_data_size();
2973 bool need_new_segment;
2974 if (current_seg == NULL)
2975 need_new_segment = true;
2976 else if (lma - vma != last_lma - last_vma)
2978 // This section has a different LMA relationship than the
2979 // last one; we need a new segment.
2980 need_new_segment = true;
2982 else if (align_address(last_lma + last_size, abi_pagesize)
2983 < align_address(lma, abi_pagesize))
2985 // Putting this section in the segment would require
2986 // skipping a page.
2987 need_new_segment = true;
2989 else if (is_bss_section(*plast) && !is_bss_section(*p))
2991 // A non-BSS section can not follow a BSS section in the
2992 // same segment.
2993 need_new_segment = true;
2995 else if (is_current_seg_readonly
2996 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
2997 && !parameters->options().omagic())
2999 // Don't put a writable section in the same segment as a
3000 // non-writable section.
3001 need_new_segment = true;
3003 else
3005 // Otherwise, reuse the existing segment.
3006 need_new_segment = false;
3009 elfcpp::Elf_Word seg_flags =
3010 Layout::section_flags_to_segment((*p)->flags());
3012 if (need_new_segment)
3014 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3015 seg_flags);
3016 current_seg->set_addresses(vma, lma);
3017 if (first_seg == NULL)
3018 first_seg = current_seg;
3019 is_current_seg_readonly = true;
3022 current_seg->add_output_section(*p, seg_flags);
3024 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3025 is_current_seg_readonly = false;
3027 plast = p;
3028 last_vma = vma;
3029 last_lma = lma;
3030 last_size = size;
3033 // An ELF program should work even if the program headers are not in
3034 // a PT_LOAD segment. However, it appears that the Linux kernel
3035 // does not set the AT_PHDR auxiliary entry in that case. It sets
3036 // the load address to p_vaddr - p_offset of the first PT_LOAD
3037 // segment. It then sets AT_PHDR to the load address plus the
3038 // offset to the program headers, e_phoff in the file header. This
3039 // fails when the program headers appear in the file before the
3040 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3041 // segment to hold the file header and the program headers. This is
3042 // effectively what the GNU linker does, and it is slightly more
3043 // efficient in any case. We try to use the first PT_LOAD segment
3044 // if we can, otherwise we make a new one.
3046 if (first_seg == NULL)
3047 return NULL;
3049 // -n or -N mean that the program is not demand paged and there is
3050 // no need to put the program headers in a PT_LOAD segment.
3051 if (parameters->options().nmagic() || parameters->options().omagic())
3052 return NULL;
3054 size_t sizeof_headers = this->total_header_size(layout);
3056 uint64_t vma = first_seg->vaddr();
3057 uint64_t lma = first_seg->paddr();
3059 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3061 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3063 first_seg->set_addresses(vma - subtract, lma - subtract);
3064 return first_seg;
3067 // If there is no room to squeeze in the headers, then punt. The
3068 // resulting executable probably won't run on GNU/Linux, but we
3069 // trust that the user knows what they are doing.
3070 if (lma < subtract || vma < subtract)
3071 return NULL;
3073 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3074 elfcpp::PF_R);
3075 load_seg->set_addresses(vma - subtract, lma - subtract);
3077 return load_seg;
3080 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3081 // segment if there are any SHT_TLS sections.
3083 void
3084 Script_sections::create_note_and_tls_segments(
3085 Layout* layout,
3086 const Layout::Section_list* sections)
3088 gold_assert(!this->saw_phdrs_clause());
3090 bool saw_tls = false;
3091 for (Layout::Section_list::const_iterator p = sections->begin();
3092 p != sections->end();
3093 ++p)
3095 if ((*p)->type() == elfcpp::SHT_NOTE)
3097 elfcpp::Elf_Word seg_flags =
3098 Layout::section_flags_to_segment((*p)->flags());
3099 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3100 seg_flags);
3101 oseg->add_output_section(*p, seg_flags);
3103 // Incorporate any subsequent SHT_NOTE sections, in the
3104 // hopes that the script is sensible.
3105 Layout::Section_list::const_iterator pnext = p + 1;
3106 while (pnext != sections->end()
3107 && (*pnext)->type() == elfcpp::SHT_NOTE)
3109 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3110 oseg->add_output_section(*pnext, seg_flags);
3111 p = pnext;
3112 ++pnext;
3116 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3118 if (saw_tls)
3119 gold_error(_("TLS sections are not adjacent"));
3121 elfcpp::Elf_Word seg_flags =
3122 Layout::section_flags_to_segment((*p)->flags());
3123 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3124 seg_flags);
3125 oseg->add_output_section(*p, seg_flags);
3127 Layout::Section_list::const_iterator pnext = p + 1;
3128 while (pnext != sections->end()
3129 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3131 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
3132 oseg->add_output_section(*pnext, seg_flags);
3133 p = pnext;
3134 ++pnext;
3137 saw_tls = true;
3142 // Add a program header. The PHDRS clause is syntactically distinct
3143 // from the SECTIONS clause, but we implement it with the SECTIONS
3144 // support because PHDRS is useless if there is no SECTIONS clause.
3146 void
3147 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3148 bool includes_filehdr, bool includes_phdrs,
3149 bool is_flags_valid, unsigned int flags,
3150 Expression* load_address)
3152 if (this->phdrs_elements_ == NULL)
3153 this->phdrs_elements_ = new Phdrs_elements();
3154 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3155 includes_filehdr,
3156 includes_phdrs,
3157 is_flags_valid, flags,
3158 load_address));
3161 // Return the number of segments we expect to create based on the
3162 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3164 size_t
3165 Script_sections::expected_segment_count(const Layout* layout) const
3167 if (this->saw_phdrs_clause())
3168 return this->phdrs_elements_->size();
3170 Layout::Section_list sections;
3171 layout->get_allocated_sections(&sections);
3173 // We assume that we will need two PT_LOAD segments.
3174 size_t ret = 2;
3176 bool saw_note = false;
3177 bool saw_tls = false;
3178 for (Layout::Section_list::const_iterator p = sections.begin();
3179 p != sections.end();
3180 ++p)
3182 if ((*p)->type() == elfcpp::SHT_NOTE)
3184 // Assume that all note sections will fit into a single
3185 // PT_NOTE segment.
3186 if (!saw_note)
3188 ++ret;
3189 saw_note = true;
3192 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3194 // There can only be one PT_TLS segment.
3195 if (!saw_tls)
3197 ++ret;
3198 saw_tls = true;
3203 return ret;
3206 // Create the segments from a PHDRS clause. Return the segment which
3207 // should hold the file header and program headers, if any.
3209 Output_segment*
3210 Script_sections::create_segments_from_phdrs_clause(Layout* layout)
3212 this->attach_sections_using_phdrs_clause(layout);
3213 return this->set_phdrs_clause_addresses(layout);
3216 // Create the segments from the PHDRS clause, and put the output
3217 // sections in them.
3219 void
3220 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
3222 typedef std::map<std::string, Output_segment*> Name_to_segment;
3223 Name_to_segment name_to_segment;
3224 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3225 p != this->phdrs_elements_->end();
3226 ++p)
3227 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
3229 // Walk through the output sections and attach them to segments.
3230 // Output sections in the script which do not list segments are
3231 // attached to the same set of segments as the immediately preceding
3232 // output section.
3234 String_list* phdr_names = NULL;
3235 bool load_segments_only = false;
3236 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3237 p != this->sections_elements_->end();
3238 ++p)
3240 bool orphan;
3241 String_list* old_phdr_names = phdr_names;
3242 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &orphan);
3243 if (os == NULL)
3244 continue;
3246 if (phdr_names == NULL)
3248 gold_error(_("allocated section not in any segment"));
3249 continue;
3252 // We see a list of segments names. Disable PT_LOAD segment only
3253 // filtering.
3254 if (old_phdr_names != phdr_names)
3255 load_segments_only = false;
3257 // If this is an orphan section--one that was not explicitly
3258 // mentioned in the linker script--then it should not inherit
3259 // any segment type other than PT_LOAD. Otherwise, e.g., the
3260 // PT_INTERP segment will pick up following orphan sections,
3261 // which does not make sense. If this is not an orphan section,
3262 // we trust the linker script.
3263 if (orphan)
3265 // Enable PT_LOAD segments only filtering until we see another
3266 // list of segment names.
3267 load_segments_only = true;
3270 bool in_load_segment = false;
3271 for (String_list::const_iterator q = phdr_names->begin();
3272 q != phdr_names->end();
3273 ++q)
3275 Name_to_segment::const_iterator r = name_to_segment.find(*q);
3276 if (r == name_to_segment.end())
3277 gold_error(_("no segment %s"), q->c_str());
3278 else
3280 if (load_segments_only
3281 && r->second->type() != elfcpp::PT_LOAD)
3282 continue;
3284 elfcpp::Elf_Word seg_flags =
3285 Layout::section_flags_to_segment(os->flags());
3286 r->second->add_output_section(os, seg_flags);
3288 if (r->second->type() == elfcpp::PT_LOAD)
3290 if (in_load_segment)
3291 gold_error(_("section in two PT_LOAD segments"));
3292 in_load_segment = true;
3297 if (!in_load_segment)
3298 gold_error(_("allocated section not in any PT_LOAD segment"));
3302 // Set the addresses for segments created from a PHDRS clause. Return
3303 // the segment which should hold the file header and program headers,
3304 // if any.
3306 Output_segment*
3307 Script_sections::set_phdrs_clause_addresses(Layout* layout)
3309 Output_segment* load_seg = NULL;
3310 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3311 p != this->phdrs_elements_->end();
3312 ++p)
3314 // Note that we have to set the flags after adding the output
3315 // sections to the segment, as adding an output segment can
3316 // change the flags.
3317 (*p)->set_flags_if_valid();
3319 Output_segment* oseg = (*p)->segment();
3321 if (oseg->type() != elfcpp::PT_LOAD)
3323 // The addresses of non-PT_LOAD segments are set from the
3324 // PT_LOAD segments.
3325 if ((*p)->has_load_address())
3326 gold_error(_("may only specify load address for PT_LOAD segment"));
3327 continue;
3330 // The output sections should have addresses from the SECTIONS
3331 // clause. The addresses don't have to be in order, so find the
3332 // one with the lowest load address. Use that to set the
3333 // address of the segment.
3335 Output_section* osec = oseg->section_with_lowest_load_address();
3336 if (osec == NULL)
3338 oseg->set_addresses(0, 0);
3339 continue;
3342 uint64_t vma = osec->address();
3343 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
3345 // Override the load address of the section with the load
3346 // address specified for the segment.
3347 if ((*p)->has_load_address())
3349 if (osec->has_load_address())
3350 gold_warning(_("PHDRS load address overrides "
3351 "section %s load address"),
3352 osec->name());
3354 lma = (*p)->load_address();
3357 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
3358 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
3360 // We could support this if we wanted to.
3361 gold_error(_("using only one of FILEHDR and PHDRS is "
3362 "not currently supported"));
3364 if (headers)
3366 size_t sizeof_headers = this->total_header_size(layout);
3367 uint64_t subtract = this->header_size_adjustment(lma,
3368 sizeof_headers);
3369 if (lma >= subtract && vma >= subtract)
3371 lma -= subtract;
3372 vma -= subtract;
3374 else
3376 gold_error(_("sections loaded on first page without room "
3377 "for file and program headers "
3378 "are not supported"));
3381 if (load_seg != NULL)
3382 gold_error(_("using FILEHDR and PHDRS on more than one "
3383 "PT_LOAD segment is not currently supported"));
3384 load_seg = oseg;
3387 oseg->set_addresses(vma, lma);
3390 return load_seg;
3393 // Add the file header and segment headers to non-load segments
3394 // specified in the PHDRS clause.
3396 void
3397 Script_sections::put_headers_in_phdrs(Output_data* file_header,
3398 Output_data* segment_headers)
3400 gold_assert(this->saw_phdrs_clause());
3401 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3402 p != this->phdrs_elements_->end();
3403 ++p)
3405 if ((*p)->type() != elfcpp::PT_LOAD)
3407 if ((*p)->includes_phdrs())
3408 (*p)->segment()->add_initial_output_data(segment_headers);
3409 if ((*p)->includes_filehdr())
3410 (*p)->segment()->add_initial_output_data(file_header);
3415 // Look for an output section by name and return the address, the load
3416 // address, the alignment, and the size. This is used when an
3417 // expression refers to an output section which was not actually
3418 // created. This returns true if the section was found, false
3419 // otherwise.
3421 bool
3422 Script_sections::get_output_section_info(const char* name, uint64_t* address,
3423 uint64_t* load_address,
3424 uint64_t* addralign,
3425 uint64_t* size) const
3427 if (!this->saw_sections_clause_)
3428 return false;
3429 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3430 p != this->sections_elements_->end();
3431 ++p)
3432 if ((*p)->get_output_section_info(name, address, load_address, addralign,
3433 size))
3434 return true;
3435 return false;
3438 // Release all Output_segments. This remove all pointers to all
3439 // Output_segments.
3441 void
3442 Script_sections::release_segments()
3444 if (this->saw_phdrs_clause())
3446 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3447 p != this->phdrs_elements_->end();
3448 ++p)
3449 (*p)->release_segment();
3453 // Print the SECTIONS clause to F for debugging.
3455 void
3456 Script_sections::print(FILE* f) const
3458 if (!this->saw_sections_clause_)
3459 return;
3461 fprintf(f, "SECTIONS {\n");
3463 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3464 p != this->sections_elements_->end();
3465 ++p)
3466 (*p)->print(f);
3468 fprintf(f, "}\n");
3470 if (this->phdrs_elements_ != NULL)
3472 fprintf(f, "PHDRS {\n");
3473 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3474 p != this->phdrs_elements_->end();
3475 ++p)
3476 (*p)->print(f);
3477 fprintf(f, "}\n");
3481 } // End namespace gold.