]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/script-sections.cc
* options.h (class General_options): Add --[no-]gnu-unique options.
[thirdparty/binutils-gdb.git] / gold / script-sections.cc
CommitLineData
494e05f4
ILT
1// script-sections.cc -- linker script SECTIONS for gold
2
0aa45fac 3// Copyright 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
494e05f4
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
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.
12
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.
17
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.
22
23#include "gold.h"
24
a445fddf
ILT
25#include <cstring>
26#include <algorithm>
27#include <list>
1c4f3631 28#include <map>
494e05f4
ILT
29#include <string>
30#include <vector>
a445fddf 31#include <fnmatch.h>
494e05f4 32
a445fddf
ILT
33#include "parameters.h"
34#include "object.h"
35#include "layout.h"
36#include "output.h"
494e05f4
ILT
37#include "script-c.h"
38#include "script.h"
39#include "script-sections.h"
40
41// Support for the SECTIONS clause in linker scripts.
42
43namespace gold
44{
45
7f8cd844
NC
46// A region of memory.
47class Memory_region
48{
49 public:
50 Memory_region(const char* name, size_t namelen, unsigned int attributes,
51 Expression* start, Expression* length)
52 : name_(name, namelen),
53 attributes_(attributes),
54 start_(start),
55 length_(length),
ea5cae92 56 current_offset_(0),
4ef28648 57 vma_sections_(),
ea5cae92
NC
58 lma_sections_(),
59 last_section_(NULL)
7f8cd844
NC
60 { }
61
62 // Return the name of this region.
63 const std::string&
64 name() const
65 { return this->name_; }
66
67 // Return the start address of this region.
68 Expression*
69 start_address() const
70 { return this->start_; }
71
72 // Return the length of this region.
73 Expression*
74 length() const
75 { return this->length_; }
76
77 // Print the region (when debugging).
78 void
79 print(FILE*) const;
80
81 // Return true if <name,namelen> matches this region.
82 bool
83 name_match(const char* name, size_t namelen)
84 {
85 return (this->name_.length() == namelen
86 && strncmp(this->name_.c_str(), name, namelen) == 0);
87 }
88
89 Expression*
ea5cae92 90 get_current_address() const
7f8cd844
NC
91 {
92 return
93 script_exp_binary_add(this->start_,
ea5cae92 94 script_exp_integer(this->current_offset_));
7f8cd844
NC
95 }
96
97 void
ea5cae92
NC
98 increment_offset(std::string section_name, uint64_t amount,
99 const Symbol_table* symtab, const Layout* layout)
7f8cd844 100 {
ea5cae92 101 this->current_offset_ += amount;
7f8cd844 102
ea5cae92 103 if (this->current_offset_
7f8cd844 104 > this->length_->eval(symtab, layout, false))
ea5cae92
NC
105 gold_error(_("section %s overflows end of region %s"),
106 section_name.c_str(), this->name_.c_str());
7f8cd844
NC
107 }
108
ea5cae92
NC
109 // Returns true iff there is room left in this region
110 // for AMOUNT more bytes of data.
111 bool
112 has_room_for(const Symbol_table* symtab, const Layout* layout,
113 uint64_t amount) const
7f8cd844 114 {
ea5cae92
NC
115 return (this->current_offset_ + amount
116 < this->length_->eval(symtab, layout, false));
7f8cd844
NC
117 }
118
ea5cae92
NC
119 // Return true if the provided section flags
120 // are compatible with this region's attributes.
121 bool
122 attributes_compatible(elfcpp::Elf_Xword flags, elfcpp::Elf_Xword type) const;
123
7f8cd844
NC
124 void
125 add_section(Output_section_definition* sec, bool vma)
126 {
127 if (vma)
128 this->vma_sections_.push_back(sec);
129 else
130 this->lma_sections_.push_back(sec);
131 }
132
133 typedef std::vector<Output_section_definition*> Section_list;
134
135 // Return the start of the list of sections
136 // whose VMAs are taken from this region.
137 Section_list::const_iterator
ea5cae92 138 get_vma_section_list_start() const
7f8cd844
NC
139 { return this->vma_sections_.begin(); }
140
141 // Return the start of the list of sections
142 // whose LMAs are taken from this region.
143 Section_list::const_iterator
ea5cae92 144 get_lma_section_list_start() const
7f8cd844
NC
145 { return this->lma_sections_.begin(); }
146
147 // Return the end of the list of sections
148 // whose VMAs are taken from this region.
149 Section_list::const_iterator
ea5cae92 150 get_vma_section_list_end() const
7f8cd844
NC
151 { return this->vma_sections_.end(); }
152
153 // Return the end of the list of sections
154 // whose LMAs are taken from this region.
155 Section_list::const_iterator
ea5cae92 156 get_lma_section_list_end() const
7f8cd844
NC
157 { return this->lma_sections_.end(); }
158
ea5cae92
NC
159 Output_section_definition*
160 get_last_section() const
161 { return this->last_section_; }
162
163 void
164 set_last_section(Output_section_definition* sec)
165 { this->last_section_ = sec; }
166
7f8cd844
NC
167 private:
168
169 std::string name_;
170 unsigned int attributes_;
171 Expression* start_;
172 Expression* length_;
ea5cae92
NC
173 // The offset to the next free byte in the region.
174 // Note - for compatibility with GNU LD we only maintain one offset
175 // regardless of whether the region is being used for VMA values,
176 // LMA values, or both.
177 uint64_t current_offset_;
7f8cd844
NC
178 // A list of sections whose VMAs are set inside this region.
179 Section_list vma_sections_;
180 // A list of sections whose LMAs are set inside this region.
181 Section_list lma_sections_;
ea5cae92
NC
182 // The latest section to make use of this region.
183 Output_section_definition* last_section_;
7f8cd844
NC
184};
185
ea5cae92
NC
186// Return true if the provided section flags
187// are compatible with this region's attributes.
188
189bool
190Memory_region::attributes_compatible(elfcpp::Elf_Xword flags,
191 elfcpp::Elf_Xword type) const
192{
193 unsigned int attrs = this->attributes_;
194
195 // No attributes means that this region is not compatible with anything.
196 if (attrs == 0)
197 return false;
198
199 bool match = true;
200 do
201 {
202 switch (attrs & - attrs)
203 {
204 case MEM_EXECUTABLE:
205 if ((flags & elfcpp::SHF_EXECINSTR) == 0)
206 match = false;
207 break;
208
209 case MEM_WRITEABLE:
210 if ((flags & elfcpp::SHF_WRITE) == 0)
211 match = false;
212 break;
213
214 case MEM_READABLE:
215 // All sections are presumed readable.
216 break;
217
218 case MEM_ALLOCATABLE:
219 if ((flags & elfcpp::SHF_ALLOC) == 0)
220 match = false;
221 break;
222
223 case MEM_INITIALIZED:
224 if ((type & elfcpp::SHT_NOBITS) != 0)
225 match = false;
226 break;
227 }
228 attrs &= ~ (attrs & - attrs);
229 }
230 while (attrs != 0);
231
232 return match;
233}
234
7f8cd844
NC
235// Print a memory region.
236
237void
238Memory_region::print(FILE* f) const
239{
240 fprintf(f, " %s", this->name_.c_str());
241
242 unsigned int attrs = this->attributes_;
243 if (attrs != 0)
244 {
245 fprintf(f, " (");
246 do
247 {
248 switch (attrs & - attrs)
249 {
250 case MEM_EXECUTABLE: fputc('x', f); break;
251 case MEM_WRITEABLE: fputc('w', f); break;
252 case MEM_READABLE: fputc('r', f); break;
253 case MEM_ALLOCATABLE: fputc('a', f); break;
254 case MEM_INITIALIZED: fputc('i', f); break;
255 default:
256 gold_unreachable();
257 }
258 attrs &= ~ (attrs & - attrs);
259 }
260 while (attrs != 0);
261 fputc(')', f);
262 }
263
264 fprintf(f, " : origin = ");
265 this->start_->print(f);
266 fprintf(f, ", length = ");
267 this->length_->print(f);
268 fprintf(f, "\n");
269}
270
0d371ad3
ILT
271// Manage orphan sections. This is intended to be largely compatible
272// with the GNU linker. The Linux kernel implicitly relies on
273// something similar to the GNU linker's orphan placement. We
274// originally used a simpler scheme here, but it caused the kernel
275// build to fail, and was also rather inefficient.
276
277class Orphan_section_placement
278{
279 private:
280 typedef Script_sections::Elements_iterator Elements_iterator;
281
282 public:
283 Orphan_section_placement();
284
285 // Handle an output section during initialization of this mapping.
286 void
287 output_section_init(const std::string& name, Output_section*,
288 Elements_iterator location);
289
290 // Initialize the last location.
291 void
292 last_init(Elements_iterator location);
293
294 // Set *PWHERE to the address of an iterator pointing to the
295 // location to use for an orphan section. Return true if the
296 // iterator has a value, false otherwise.
297 bool
298 find_place(Output_section*, Elements_iterator** pwhere);
299
300 // Return the iterator being used for sections at the very end of
301 // the linker script.
302 Elements_iterator
303 last_place() const;
304
305 private:
306 // The places that we specifically recognize. This list is copied
307 // from the GNU linker.
308 enum Place_index
309 {
310 PLACE_TEXT,
311 PLACE_RODATA,
312 PLACE_DATA,
6c93b22c
ILT
313 PLACE_TLS,
314 PLACE_TLS_BSS,
0d371ad3
ILT
315 PLACE_BSS,
316 PLACE_REL,
317 PLACE_INTERP,
318 PLACE_NONALLOC,
319 PLACE_LAST,
320 PLACE_MAX
321 };
322
323 // The information we keep for a specific place.
324 struct Place
325 {
326 // The name of sections for this place.
327 const char* name;
328 // Whether we have a location for this place.
329 bool have_location;
330 // The iterator for this place.
331 Elements_iterator location;
332 };
333
334 // Initialize one place element.
335 void
336 initialize_place(Place_index, const char*);
337
338 // The places.
339 Place places_[PLACE_MAX];
340 // True if this is the first call to output_section_init.
341 bool first_init_;
342};
343
344// Initialize Orphan_section_placement.
345
346Orphan_section_placement::Orphan_section_placement()
347 : first_init_(true)
348{
349 this->initialize_place(PLACE_TEXT, ".text");
350 this->initialize_place(PLACE_RODATA, ".rodata");
351 this->initialize_place(PLACE_DATA, ".data");
6c93b22c
ILT
352 this->initialize_place(PLACE_TLS, NULL);
353 this->initialize_place(PLACE_TLS_BSS, NULL);
0d371ad3
ILT
354 this->initialize_place(PLACE_BSS, ".bss");
355 this->initialize_place(PLACE_REL, NULL);
356 this->initialize_place(PLACE_INTERP, ".interp");
357 this->initialize_place(PLACE_NONALLOC, NULL);
358 this->initialize_place(PLACE_LAST, NULL);
359}
360
361// Initialize one place element.
362
363void
364Orphan_section_placement::initialize_place(Place_index index, const char* name)
365{
366 this->places_[index].name = name;
367 this->places_[index].have_location = false;
368}
369
370// While initializing the Orphan_section_placement information, this
371// is called once for each output section named in the linker script.
372// If we found an output section during the link, it will be passed in
373// OS.
374
375void
376Orphan_section_placement::output_section_init(const std::string& name,
377 Output_section* os,
378 Elements_iterator location)
379{
380 bool first_init = this->first_init_;
381 this->first_init_ = false;
382
383 for (int i = 0; i < PLACE_MAX; ++i)
384 {
385 if (this->places_[i].name != NULL && this->places_[i].name == name)
386 {
387 if (this->places_[i].have_location)
388 {
389 // We have already seen a section with this name.
390 return;
391 }
392
393 this->places_[i].location = location;
394 this->places_[i].have_location = true;
395
396 // If we just found the .bss section, restart the search for
397 // an unallocated section. This follows the GNU linker's
398 // behaviour.
399 if (i == PLACE_BSS)
400 this->places_[PLACE_NONALLOC].have_location = false;
401
402 return;
403 }
404 }
405
406 // Relocation sections.
407 if (!this->places_[PLACE_REL].have_location
408 && os != NULL
409 && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
410 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
411 {
412 this->places_[PLACE_REL].location = location;
413 this->places_[PLACE_REL].have_location = true;
414 }
415
416 // We find the location for unallocated sections by finding the
417 // first debugging or comment section after the BSS section (if
418 // there is one).
419 if (!this->places_[PLACE_NONALLOC].have_location
420 && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
421 {
422 // We add orphan sections after the location in PLACES_. We
423 // want to store unallocated sections before LOCATION. If this
424 // is the very first section, we can't use it.
425 if (!first_init)
426 {
427 --location;
428 this->places_[PLACE_NONALLOC].location = location;
429 this->places_[PLACE_NONALLOC].have_location = true;
430 }
431 }
432}
433
434// Initialize the last location.
435
436void
437Orphan_section_placement::last_init(Elements_iterator location)
438{
439 this->places_[PLACE_LAST].location = location;
440 this->places_[PLACE_LAST].have_location = true;
441}
442
443// Set *PWHERE to the address of an iterator pointing to the location
444// to use for an orphan section. Return true if the iterator has a
445// value, false otherwise.
446
447bool
448Orphan_section_placement::find_place(Output_section* os,
449 Elements_iterator** pwhere)
450{
451 // Figure out where OS should go. This is based on the GNU linker
452 // code. FIXME: The GNU linker handles small data sections
453 // specially, but we don't.
454 elfcpp::Elf_Word type = os->type();
455 elfcpp::Elf_Xword flags = os->flags();
456 Place_index index;
457 if ((flags & elfcpp::SHF_ALLOC) == 0
458 && !Layout::is_debug_info_section(os->name()))
459 index = PLACE_NONALLOC;
460 else if ((flags & elfcpp::SHF_ALLOC) == 0)
461 index = PLACE_LAST;
462 else if (type == elfcpp::SHT_NOTE)
463 index = PLACE_INTERP;
6c93b22c
ILT
464 else if ((flags & elfcpp::SHF_TLS) != 0)
465 {
466 if (type == elfcpp::SHT_NOBITS)
467 index = PLACE_TLS_BSS;
468 else
469 index = PLACE_TLS;
470 }
0d371ad3
ILT
471 else if (type == elfcpp::SHT_NOBITS)
472 index = PLACE_BSS;
473 else if ((flags & elfcpp::SHF_WRITE) != 0)
474 index = PLACE_DATA;
475 else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
476 index = PLACE_REL;
477 else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
478 index = PLACE_RODATA;
479 else
480 index = PLACE_TEXT;
481
482 // If we don't have a location yet, try to find one based on a
483 // plausible ordering of sections.
484 if (!this->places_[index].have_location)
485 {
486 Place_index follow;
487 switch (index)
488 {
489 default:
490 follow = PLACE_MAX;
491 break;
492 case PLACE_RODATA:
493 follow = PLACE_TEXT;
494 break;
495 case PLACE_BSS:
496 follow = PLACE_DATA;
497 break;
498 case PLACE_REL:
499 follow = PLACE_TEXT;
500 break;
501 case PLACE_INTERP:
502 follow = PLACE_TEXT;
503 break;
6c93b22c
ILT
504 case PLACE_TLS:
505 follow = PLACE_DATA;
506 break;
507 case PLACE_TLS_BSS:
508 follow = PLACE_TLS;
509 if (!this->places_[PLACE_TLS].have_location)
510 follow = PLACE_DATA;
511 break;
0d371ad3
ILT
512 }
513 if (follow != PLACE_MAX && this->places_[follow].have_location)
514 {
515 // Set the location of INDEX to the location of FOLLOW. The
516 // location of INDEX will then be incremented by the caller,
517 // so anything in INDEX will continue to be after anything
518 // in FOLLOW.
519 this->places_[index].location = this->places_[follow].location;
520 this->places_[index].have_location = true;
521 }
522 }
523
524 *pwhere = &this->places_[index].location;
525 bool ret = this->places_[index].have_location;
526
527 // The caller will set the location.
528 this->places_[index].have_location = true;
529
530 return ret;
531}
532
533// Return the iterator being used for sections at the very end of the
534// linker script.
535
536Orphan_section_placement::Elements_iterator
537Orphan_section_placement::last_place() const
538{
539 gold_assert(this->places_[PLACE_LAST].have_location);
540 return this->places_[PLACE_LAST].location;
541}
542
494e05f4
ILT
543// An element in a SECTIONS clause.
544
545class Sections_element
546{
547 public:
548 Sections_element()
549 { }
550
551 virtual ~Sections_element()
552 { }
553
0d371ad3
ILT
554 // Return whether an output section is relro.
555 virtual bool
556 is_relro() const
557 { return false; }
558
2d924fd9
ILT
559 // Record that an output section is relro.
560 virtual void
561 set_is_relro()
562 { }
563
919ed24c
ILT
564 // Create any required output sections. The only real
565 // implementation is in Output_section_definition.
566 virtual void
567 create_sections(Layout*)
568 { }
569
a445fddf
ILT
570 // Add any symbol being defined to the symbol table.
571 virtual void
572 add_symbols_to_table(Symbol_table*)
573 { }
574
575 // Finalize symbols and check assertions.
576 virtual void
77e65537 577 finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
a445fddf
ILT
578 { }
579
580 // Return the output section name to use for an input file name and
581 // section name. This only real implementation is in
582 // Output_section_definition.
583 virtual const char*
1e5d2fb1
DK
584 output_section_name(const char*, const char*, Output_section***,
585 Script_sections::Section_type*)
a445fddf
ILT
586 { return NULL; }
587
0d371ad3
ILT
588 // Initialize OSP with an output section.
589 virtual void
590 orphan_section_init(Orphan_section_placement*,
591 Script_sections::Elements_iterator)
592 { }
a445fddf
ILT
593
594 // Set section addresses. This includes applying assignments if the
9b547ce6 595 // expression is an absolute value.
a445fddf 596 virtual void
f6973bdc
ILT
597 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
598 uint64_t*)
a445fddf
ILT
599 { }
600
3802b2dd
ILT
601 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
602 // this section is constrained, and the input sections do not match,
603 // return the constraint, and set *POSD.
604 virtual Section_constraint
605 check_constraint(Output_section_definition**)
606 { return CONSTRAINT_NONE; }
607
608 // See if this is the alternate output section for a constrained
609 // output section. If it is, transfer the Output_section and return
610 // true. Otherwise return false.
611 virtual bool
612 alternate_constraint(Output_section_definition*, Section_constraint)
613 { return false; }
614
1c4f3631
ILT
615 // Get the list of segments to use for an allocated section when
616 // using a PHDRS clause. If this is an allocated section, return
2cefc357
ILT
617 // the Output_section, and set *PHDRS_LIST (the first parameter) to
618 // the list of PHDRS to which it should be attached. If the PHDRS
619 // were not specified, don't change *PHDRS_LIST. When not returning
620 // NULL, set *ORPHAN (the second parameter) according to whether
621 // this is an orphan section--one that is not mentioned in the
622 // linker script.
1c4f3631 623 virtual Output_section*
2cefc357 624 allocate_to_segment(String_list**, bool*)
1c4f3631
ILT
625 { return NULL; }
626
8f2eb564
ILT
627 // Look for an output section by name and return the address, the
628 // load address, the alignment, and the size. This is used when an
629 // expression refers to an output section which was not actually
630 // created. This returns true if the section was found, false
631 // otherwise. The only real definition is for
632 // Output_section_definition.
633 virtual bool
634 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
635 uint64_t*) const
636 { return false; }
637
2d924fd9
ILT
638 // Return the associated Output_section if there is one.
639 virtual Output_section*
640 get_output_section() const
641 { return NULL; }
642
7f8cd844
NC
643 // Set the section's memory regions.
644 virtual void
645 set_memory_region(Memory_region*, bool)
646 { gold_error(_("Attempt to set a memory region for a non-output section")); }
647
a445fddf 648 // Print the element for debugging purposes.
494e05f4
ILT
649 virtual void
650 print(FILE* f) const = 0;
651};
652
653// An assignment in a SECTIONS clause outside of an output section.
654
655class Sections_element_assignment : public Sections_element
656{
657 public:
658 Sections_element_assignment(const char* name, size_t namelen,
659 Expression* val, bool provide, bool hidden)
99fff23b 660 : assignment_(name, namelen, false, val, provide, hidden)
494e05f4
ILT
661 { }
662
a445fddf
ILT
663 // Add the symbol to the symbol table.
664 void
665 add_symbols_to_table(Symbol_table* symtab)
666 { this->assignment_.add_to_table(symtab); }
667
668 // Finalize the symbol.
669 void
670 finalize_symbols(Symbol_table* symtab, const Layout* layout,
77e65537 671 uint64_t* dot_value)
a445fddf 672 {
77e65537 673 this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
a445fddf
ILT
674 }
675
676 // Set the section address. There is no section here, but if the
677 // value is absolute, we set the symbol. This permits us to use
678 // absolute symbols when setting dot.
679 void
680 set_section_addresses(Symbol_table* symtab, Layout* layout,
f6973bdc 681 uint64_t* dot_value, uint64_t*, uint64_t*)
a445fddf 682 {
77e65537 683 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
a445fddf
ILT
684 }
685
686 // Print for debugging.
494e05f4
ILT
687 void
688 print(FILE* f) const
689 {
690 fprintf(f, " ");
691 this->assignment_.print(f);
692 }
693
694 private:
695 Symbol_assignment assignment_;
696};
697
a445fddf
ILT
698// An assignment to the dot symbol in a SECTIONS clause outside of an
699// output section.
700
701class Sections_element_dot_assignment : public Sections_element
702{
703 public:
704 Sections_element_dot_assignment(Expression* val)
705 : val_(val)
706 { }
707
708 // Finalize the symbol.
709 void
710 finalize_symbols(Symbol_table* symtab, const Layout* layout,
77e65537 711 uint64_t* dot_value)
a445fddf 712 {
77e65537
ILT
713 // We ignore the section of the result because outside of an
714 // output section definition the dot symbol is always considered
715 // to be absolute.
919ed24c 716 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
bacff3ab 717 NULL, NULL, NULL);
a445fddf
ILT
718 }
719
720 // Update the dot symbol while setting section addresses.
721 void
722 set_section_addresses(Symbol_table* symtab, Layout* layout,
f6973bdc
ILT
723 uint64_t* dot_value, uint64_t* dot_alignment,
724 uint64_t* load_address)
a445fddf 725 {
919ed24c 726 *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
bacff3ab 727 NULL, NULL, dot_alignment);
fd247bfe 728 *load_address = *dot_value;
a445fddf
ILT
729 }
730
731 // Print for debugging.
732 void
733 print(FILE* f) const
734 {
735 fprintf(f, " . = ");
736 this->val_->print(f);
737 fprintf(f, "\n");
738 }
739
740 private:
741 Expression* val_;
742};
743
494e05f4
ILT
744// An assertion in a SECTIONS clause outside of an output section.
745
746class Sections_element_assertion : public Sections_element
747{
748 public:
749 Sections_element_assertion(Expression* check, const char* message,
750 size_t messagelen)
751 : assertion_(check, message, messagelen)
752 { }
753
a445fddf
ILT
754 // Check the assertion.
755 void
77e65537 756 finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
a445fddf
ILT
757 { this->assertion_.check(symtab, layout); }
758
759 // Print for debugging.
494e05f4
ILT
760 void
761 print(FILE* f) const
762 {
763 fprintf(f, " ");
764 this->assertion_.print(f);
765 }
766
767 private:
768 Script_assertion assertion_;
769};
770
771// An element in an output section in a SECTIONS clause.
772
773class Output_section_element
774{
775 public:
a445fddf 776 // A list of input sections.
6625d24e 777 typedef std::list<Output_section::Input_section> Input_section_list;
a445fddf 778
494e05f4
ILT
779 Output_section_element()
780 { }
781
782 virtual ~Output_section_element()
783 { }
784
919ed24c
ILT
785 // Return whether this element requires an output section to exist.
786 virtual bool
787 needs_output_section() const
788 { return false; }
789
a445fddf
ILT
790 // Add any symbol being defined to the symbol table.
791 virtual void
792 add_symbols_to_table(Symbol_table*)
793 { }
794
795 // Finalize symbols and check assertions.
796 virtual void
77e65537 797 finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
a445fddf
ILT
798 { }
799
800 // Return whether this element matches FILE_NAME and SECTION_NAME.
801 // The only real implementation is in Output_section_element_input.
802 virtual bool
803 match_name(const char*, const char*) const
804 { return false; }
805
806 // Set section addresses. This includes applying assignments if the
9b547ce6 807 // expression is an absolute value.
a445fddf
ILT
808 virtual void
809 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
f6973bdc 810 uint64_t*, uint64_t*, Output_section**, std::string*,
77e65537 811 Input_section_list*)
a445fddf
ILT
812 { }
813
814 // Print the element for debugging purposes.
494e05f4
ILT
815 virtual void
816 print(FILE* f) const = 0;
a445fddf
ILT
817
818 protected:
819 // Return a fill string that is LENGTH bytes long, filling it with
820 // FILL.
821 std::string
822 get_fill_string(const std::string* fill, section_size_type length) const;
494e05f4
ILT
823};
824
a445fddf
ILT
825std::string
826Output_section_element::get_fill_string(const std::string* fill,
827 section_size_type length) const
828{
829 std::string this_fill;
830 this_fill.reserve(length);
831 while (this_fill.length() + fill->length() <= length)
832 this_fill += *fill;
833 if (this_fill.length() < length)
834 this_fill.append(*fill, 0, length - this_fill.length());
835 return this_fill;
836}
837
494e05f4
ILT
838// A symbol assignment in an output section.
839
840class Output_section_element_assignment : public Output_section_element
841{
842 public:
843 Output_section_element_assignment(const char* name, size_t namelen,
844 Expression* val, bool provide,
845 bool hidden)
99fff23b 846 : assignment_(name, namelen, false, val, provide, hidden)
494e05f4
ILT
847 { }
848
a445fddf
ILT
849 // Add the symbol to the symbol table.
850 void
851 add_symbols_to_table(Symbol_table* symtab)
852 { this->assignment_.add_to_table(symtab); }
853
854 // Finalize the symbol.
855 void
856 finalize_symbols(Symbol_table* symtab, const Layout* layout,
77e65537 857 uint64_t* dot_value, Output_section** dot_section)
a445fddf 858 {
77e65537
ILT
859 this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
860 *dot_section);
a445fddf
ILT
861 }
862
863 // Set the section address. There is no section here, but if the
864 // value is absolute, we set the symbol. This permits us to use
865 // absolute symbols when setting dot.
866 void
867 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
f6973bdc
ILT
868 uint64_t, uint64_t* dot_value, uint64_t*,
869 Output_section**, std::string*, Input_section_list*)
a445fddf 870 {
77e65537 871 this->assignment_.set_if_absolute(symtab, layout, true, *dot_value);
a445fddf
ILT
872 }
873
874 // Print for debugging.
494e05f4
ILT
875 void
876 print(FILE* f) const
877 {
878 fprintf(f, " ");
879 this->assignment_.print(f);
880 }
881
882 private:
883 Symbol_assignment assignment_;
884};
885
a445fddf
ILT
886// An assignment to the dot symbol in an output section.
887
888class Output_section_element_dot_assignment : public Output_section_element
889{
890 public:
891 Output_section_element_dot_assignment(Expression* val)
892 : val_(val)
893 { }
894
bfc34b3f
ILT
895 // An assignment to dot within an output section is enough to force
896 // the output section to exist.
897 bool
898 needs_output_section() const
899 { return true; }
900
a445fddf
ILT
901 // Finalize the symbol.
902 void
903 finalize_symbols(Symbol_table* symtab, const Layout* layout,
77e65537 904 uint64_t* dot_value, Output_section** dot_section)
a445fddf 905 {
919ed24c 906 *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
f6973bdc 907 *dot_section, dot_section, NULL);
a445fddf
ILT
908 }
909
910 // Update the dot symbol while setting section addresses.
911 void
912 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
f6973bdc
ILT
913 uint64_t, uint64_t* dot_value, uint64_t*,
914 Output_section**, std::string*, Input_section_list*);
a445fddf
ILT
915
916 // Print for debugging.
917 void
918 print(FILE* f) const
919 {
920 fprintf(f, " . = ");
921 this->val_->print(f);
922 fprintf(f, "\n");
923 }
924
925 private:
926 Expression* val_;
927};
928
929// Update the dot symbol while setting section addresses.
930
931void
932Output_section_element_dot_assignment::set_section_addresses(
933 Symbol_table* symtab,
934 Layout* layout,
935 Output_section* output_section,
936 uint64_t,
937 uint64_t* dot_value,
f6973bdc 938 uint64_t* dot_alignment,
77e65537 939 Output_section** dot_section,
a445fddf
ILT
940 std::string* fill,
941 Input_section_list*)
942{
919ed24c
ILT
943 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
944 *dot_value, *dot_section,
f6973bdc 945 dot_section, dot_alignment);
a445fddf
ILT
946 if (next_dot < *dot_value)
947 gold_error(_("dot may not move backward"));
948 if (next_dot > *dot_value && output_section != NULL)
949 {
950 section_size_type length = convert_to_section_size_type(next_dot
951 - *dot_value);
952 Output_section_data* posd;
953 if (fill->empty())
7d9e3d98 954 posd = new Output_data_zero_fill(length, 0);
a445fddf
ILT
955 else
956 {
957 std::string this_fill = this->get_fill_string(fill, length);
958 posd = new Output_data_const(this_fill, 0);
959 }
960 output_section->add_output_section_data(posd);
20e6d0d6 961 layout->new_output_section_data_from_script(posd);
a445fddf
ILT
962 }
963 *dot_value = next_dot;
964}
965
494e05f4
ILT
966// An assertion in an output section.
967
968class Output_section_element_assertion : public Output_section_element
969{
970 public:
971 Output_section_element_assertion(Expression* check, const char* message,
972 size_t messagelen)
973 : assertion_(check, message, messagelen)
974 { }
975
976 void
977 print(FILE* f) const
978 {
979 fprintf(f, " ");
980 this->assertion_.print(f);
981 }
982
983 private:
984 Script_assertion assertion_;
985};
986
77e65537
ILT
987// We use a special instance of Output_section_data to handle BYTE,
988// SHORT, etc. This permits forward references to symbols in the
989// expressions.
494e05f4 990
77e65537 991class Output_data_expression : public Output_section_data
494e05f4
ILT
992{
993 public:
77e65537
ILT
994 Output_data_expression(int size, bool is_signed, Expression* val,
995 const Symbol_table* symtab, const Layout* layout,
996 uint64_t dot_value, Output_section* dot_section)
20e6d0d6 997 : Output_section_data(size, 0, true),
77e65537
ILT
998 is_signed_(is_signed), val_(val), symtab_(symtab),
999 layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
494e05f4
ILT
1000 { }
1001
77e65537
ILT
1002 protected:
1003 // Write the data to the output file.
a445fddf 1004 void
77e65537 1005 do_write(Output_file*);
a445fddf 1006
77e65537 1007 // Write the data to a buffer.
494e05f4 1008 void
77e65537 1009 do_write_to_buffer(unsigned char*);
494e05f4 1010
7d9e3d98
ILT
1011 // Write to a map file.
1012 void
1013 do_print_to_mapfile(Mapfile* mapfile) const
1014 { mapfile->print_output_data(this, _("** expression")); }
1015
494e05f4 1016 private:
a445fddf 1017 template<bool big_endian>
77e65537
ILT
1018 void
1019 endian_write_to_buffer(uint64_t, unsigned char*);
a445fddf 1020
494e05f4 1021 bool is_signed_;
494e05f4 1022 Expression* val_;
77e65537
ILT
1023 const Symbol_table* symtab_;
1024 const Layout* layout_;
1025 uint64_t dot_value_;
1026 Output_section* dot_section_;
494e05f4
ILT
1027};
1028
77e65537 1029// Write the data element to the output file.
a445fddf
ILT
1030
1031void
77e65537 1032Output_data_expression::do_write(Output_file* of)
a445fddf 1033{
77e65537
ILT
1034 unsigned char* view = of->get_output_view(this->offset(), this->data_size());
1035 this->write_to_buffer(view);
1036 of->write_output_view(this->offset(), this->data_size(), view);
1037}
a445fddf 1038
77e65537
ILT
1039// Write the data element to a buffer.
1040
1041void
1042Output_data_expression::do_write_to_buffer(unsigned char* buf)
1043{
77e65537 1044 uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
919ed24c 1045 true, this->dot_value_,
bacff3ab 1046 this->dot_section_, NULL, NULL);
a445fddf 1047
8851ecca 1048 if (parameters->target().is_big_endian())
77e65537 1049 this->endian_write_to_buffer<true>(val, buf);
a445fddf 1050 else
77e65537 1051 this->endian_write_to_buffer<false>(val, buf);
a445fddf
ILT
1052}
1053
a445fddf 1054template<bool big_endian>
77e65537
ILT
1055void
1056Output_data_expression::endian_write_to_buffer(uint64_t val,
1057 unsigned char* buf)
a445fddf 1058{
77e65537 1059 switch (this->data_size())
a445fddf
ILT
1060 {
1061 case 1:
1062 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
a445fddf
ILT
1063 break;
1064 case 2:
1065 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
a445fddf
ILT
1066 break;
1067 case 4:
1068 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
a445fddf
ILT
1069 break;
1070 case 8:
8851ecca 1071 if (parameters->target().get_size() == 32)
a445fddf
ILT
1072 {
1073 val &= 0xffffffff;
1074 if (this->is_signed_ && (val & 0x80000000) != 0)
1075 val |= 0xffffffff00000000LL;
1076 }
1077 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
a445fddf
ILT
1078 break;
1079 default:
1080 gold_unreachable();
1081 }
77e65537
ILT
1082}
1083
1084// A data item in an output section.
1085
1086class Output_section_element_data : public Output_section_element
1087{
1088 public:
1089 Output_section_element_data(int size, bool is_signed, Expression* val)
1090 : size_(size), is_signed_(is_signed), val_(val)
1091 { }
1092
919ed24c
ILT
1093 // If there is a data item, then we must create an output section.
1094 bool
1095 needs_output_section() const
1096 { return true; }
1097
77e65537
ILT
1098 // Finalize symbols--we just need to update dot.
1099 void
1100 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1101 Output_section**)
1102 { *dot_value += this->size_; }
1103
1104 // Store the value in the section.
1105 void
1106 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
f6973bdc
ILT
1107 uint64_t* dot_value, uint64_t*, Output_section**,
1108 std::string*, Input_section_list*);
77e65537
ILT
1109
1110 // Print for debugging.
1111 void
1112 print(FILE*) const;
1113
1114 private:
1115 // The size in bytes.
1116 int size_;
1117 // Whether the value is signed.
1118 bool is_signed_;
1119 // The value.
1120 Expression* val_;
1121};
1122
1123// Store the value in the section.
1124
1125void
1126Output_section_element_data::set_section_addresses(
1127 Symbol_table* symtab,
1128 Layout* layout,
1129 Output_section* os,
1130 uint64_t,
1131 uint64_t* dot_value,
f6973bdc 1132 uint64_t*,
77e65537
ILT
1133 Output_section** dot_section,
1134 std::string*,
1135 Input_section_list*)
1136{
1137 gold_assert(os != NULL);
20e6d0d6
DK
1138 Output_data_expression* expression =
1139 new Output_data_expression(this->size_, this->is_signed_, this->val_,
1140 symtab, layout, *dot_value, *dot_section);
1141 os->add_output_section_data(expression);
1142 layout->new_output_section_data_from_script(expression);
77e65537 1143 *dot_value += this->size_;
a445fddf
ILT
1144}
1145
494e05f4
ILT
1146// Print for debugging.
1147
1148void
1149Output_section_element_data::print(FILE* f) const
1150{
1151 const char* s;
1152 switch (this->size_)
1153 {
1154 case 1:
1155 s = "BYTE";
1156 break;
1157 case 2:
1158 s = "SHORT";
1159 break;
1160 case 4:
1161 s = "LONG";
1162 break;
1163 case 8:
1164 if (this->is_signed_)
1165 s = "SQUAD";
1166 else
1167 s = "QUAD";
1168 break;
1169 default:
1170 gold_unreachable();
1171 }
1172 fprintf(f, " %s(", s);
1173 this->val_->print(f);
1174 fprintf(f, ")\n");
1175}
1176
1177// A fill value setting in an output section.
1178
1179class Output_section_element_fill : public Output_section_element
1180{
1181 public:
1182 Output_section_element_fill(Expression* val)
1183 : val_(val)
1184 { }
1185
a445fddf
ILT
1186 // Update the fill value while setting section addresses.
1187 void
1188 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
f6973bdc 1189 uint64_t, uint64_t* dot_value, uint64_t*,
77e65537
ILT
1190 Output_section** dot_section,
1191 std::string* fill, Input_section_list*)
a445fddf 1192 {
77e65537 1193 Output_section* fill_section;
919ed24c 1194 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
77e65537 1195 *dot_value, *dot_section,
f6973bdc 1196 &fill_section, NULL);
77e65537
ILT
1197 if (fill_section != NULL)
1198 gold_warning(_("fill value is not absolute"));
a445fddf
ILT
1199 // FIXME: The GNU linker supports fill values of arbitrary length.
1200 unsigned char fill_buff[4];
1201 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1202 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
1203 }
1204
1205 // Print for debugging.
494e05f4
ILT
1206 void
1207 print(FILE* f) const
1208 {
1209 fprintf(f, " FILL(");
1210 this->val_->print(f);
1211 fprintf(f, ")\n");
1212 }
1213
1214 private:
1215 // The new fill value.
1216 Expression* val_;
1217};
1218
1219// An input section specification in an output section
1220
1221class Output_section_element_input : public Output_section_element
1222{
1223 public:
494e05f4
ILT
1224 Output_section_element_input(const Input_section_spec* spec, bool keep);
1225
a445fddf
ILT
1226 // Finalize symbols--just update the value of the dot symbol.
1227 void
77e65537
ILT
1228 finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1229 Output_section** dot_section)
a445fddf
ILT
1230 {
1231 *dot_value = this->final_dot_value_;
77e65537 1232 *dot_section = this->final_dot_section_;
a445fddf
ILT
1233 }
1234
1235 // See whether we match FILE_NAME and SECTION_NAME as an input
1236 // section.
1237 bool
1238 match_name(const char* file_name, const char* section_name) const;
1239
1240 // Set the section address.
1241 void
1242 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
f6973bdc 1243 uint64_t subalign, uint64_t* dot_value, uint64_t*,
77e65537
ILT
1244 Output_section**, std::string* fill,
1245 Input_section_list*);
a445fddf
ILT
1246
1247 // Print for debugging.
494e05f4
ILT
1248 void
1249 print(FILE* f) const;
1250
1251 private:
1252 // An input section pattern.
1253 struct Input_section_pattern
1254 {
1255 std::string pattern;
a445fddf 1256 bool pattern_is_wildcard;
494e05f4
ILT
1257 Sort_wildcard sort;
1258
1259 Input_section_pattern(const char* patterna, size_t patternlena,
1260 Sort_wildcard sorta)
a445fddf 1261 : pattern(patterna, patternlena),
6e9ba2ca 1262 pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
a445fddf 1263 sort(sorta)
494e05f4
ILT
1264 { }
1265 };
1266
1267 typedef std::vector<Input_section_pattern> Input_section_patterns;
1268
a445fddf
ILT
1269 // Filename_exclusions is a pair of filename pattern and a bool
1270 // indicating whether the filename is a wildcard.
1271 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1272
1273 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1274 // indicates whether this is a wildcard pattern.
1275 static inline bool
1276 match(const char* string, const char* pattern, bool is_wildcard_pattern)
1277 {
1278 return (is_wildcard_pattern
1279 ? fnmatch(pattern, string, 0) == 0
1280 : strcmp(string, pattern) == 0);
1281 }
494e05f4 1282
a445fddf
ILT
1283 // See if we match a file name.
1284 bool
1285 match_file_name(const char* file_name) const;
1286
1287 // The file name pattern. If this is the empty string, we match all
1288 // files.
494e05f4 1289 std::string filename_pattern_;
a445fddf
ILT
1290 // Whether the file name pattern is a wildcard.
1291 bool filename_is_wildcard_;
494e05f4
ILT
1292 // How the file names should be sorted. This may only be
1293 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1294 Sort_wildcard filename_sort_;
1295 // The list of file names to exclude.
1296 Filename_exclusions filename_exclusions_;
1297 // The list of input section patterns.
1298 Input_section_patterns input_section_patterns_;
1299 // Whether to keep this section when garbage collecting.
1300 bool keep_;
a445fddf
ILT
1301 // The value of dot after including all matching sections.
1302 uint64_t final_dot_value_;
77e65537
ILT
1303 // The section where dot is defined after including all matching
1304 // sections.
1305 Output_section* final_dot_section_;
494e05f4
ILT
1306};
1307
1308// Construct Output_section_element_input. The parser records strings
1309// as pointers into a copy of the script file, which will go away when
1310// parsing is complete. We make sure they are in std::string objects.
1311
1312Output_section_element_input::Output_section_element_input(
1313 const Input_section_spec* spec,
1314 bool keep)
a445fddf
ILT
1315 : filename_pattern_(),
1316 filename_is_wildcard_(false),
494e05f4
ILT
1317 filename_sort_(spec->file.sort),
1318 filename_exclusions_(),
1319 input_section_patterns_(),
a445fddf 1320 keep_(keep),
77e65537
ILT
1321 final_dot_value_(0),
1322 final_dot_section_(NULL)
494e05f4 1323{
a445fddf
ILT
1324 // The filename pattern "*" is common, and matches all files. Turn
1325 // it into the empty string.
1326 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1327 this->filename_pattern_.assign(spec->file.name.value,
1328 spec->file.name.length);
6e9ba2ca 1329 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
a445fddf 1330
494e05f4
ILT
1331 if (spec->input_sections.exclude != NULL)
1332 {
1333 for (String_list::const_iterator p =
1334 spec->input_sections.exclude->begin();
1335 p != spec->input_sections.exclude->end();
1336 ++p)
a445fddf 1337 {
6e9ba2ca 1338 bool is_wildcard = is_wildcard_string((*p).c_str());
a445fddf
ILT
1339 this->filename_exclusions_.push_back(std::make_pair(*p,
1340 is_wildcard));
1341 }
494e05f4
ILT
1342 }
1343
1344 if (spec->input_sections.sections != NULL)
1345 {
1346 Input_section_patterns& isp(this->input_section_patterns_);
1347 for (String_sort_list::const_iterator p =
1348 spec->input_sections.sections->begin();
1349 p != spec->input_sections.sections->end();
1350 ++p)
1351 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1352 p->sort));
1353 }
1354}
1355
a445fddf
ILT
1356// See whether we match FILE_NAME.
1357
1358bool
1359Output_section_element_input::match_file_name(const char* file_name) const
1360{
1361 if (!this->filename_pattern_.empty())
1362 {
1363 // If we were called with no filename, we refuse to match a
1364 // pattern which requires a file name.
1365 if (file_name == NULL)
1366 return false;
1367
1368 if (!match(file_name, this->filename_pattern_.c_str(),
1369 this->filename_is_wildcard_))
1370 return false;
1371 }
1372
1373 if (file_name != NULL)
1374 {
1375 // Now we have to see whether FILE_NAME matches one of the
1376 // exclusion patterns, if any.
1377 for (Filename_exclusions::const_iterator p =
1378 this->filename_exclusions_.begin();
1379 p != this->filename_exclusions_.end();
1380 ++p)
1381 {
1382 if (match(file_name, p->first.c_str(), p->second))
1383 return false;
1384 }
1385 }
1386
1387 return true;
1388}
1389
1390// See whether we match FILE_NAME and SECTION_NAME.
1391
1392bool
1393Output_section_element_input::match_name(const char* file_name,
1394 const char* section_name) const
1395{
1396 if (!this->match_file_name(file_name))
1397 return false;
1398
1399 // If there are no section name patterns, then we match.
1400 if (this->input_section_patterns_.empty())
1401 return true;
1402
1403 // See whether we match the section name patterns.
1404 for (Input_section_patterns::const_iterator p =
1405 this->input_section_patterns_.begin();
1406 p != this->input_section_patterns_.end();
1407 ++p)
1408 {
1409 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1410 return true;
1411 }
1412
1413 // We didn't match any section names, so we didn't match.
1414 return false;
1415}
1416
1417// Information we use to sort the input sections.
1418
20e6d0d6 1419class Input_section_info
a445fddf 1420{
20e6d0d6 1421 public:
6625d24e 1422 Input_section_info(const Output_section::Input_section& input_section)
2ea97941 1423 : input_section_(input_section), section_name_(),
20e6d0d6
DK
1424 size_(0), addralign_(1)
1425 { }
1426
1427 // Return the simple input section.
6625d24e 1428 const Output_section::Input_section&
20e6d0d6
DK
1429 input_section() const
1430 { return this->input_section_; }
1431
1432 // Return the object.
1433 Relobj*
1434 relobj() const
1435 { return this->input_section_.relobj(); }
1436
1437 // Return the section index.
1438 unsigned int
1439 shndx()
1440 { return this->input_section_.shndx(); }
1441
1442 // Return the section name.
1443 const std::string&
1444 section_name() const
1445 { return this->section_name_; }
1446
1447 // Set the section name.
1448 void
2ea97941
ILT
1449 set_section_name(const std::string name)
1450 { this->section_name_ = name; }
20e6d0d6
DK
1451
1452 // Return the section size.
1453 uint64_t
1454 size() const
1455 { return this->size_; }
1456
1457 // Set the section size.
1458 void
2ea97941
ILT
1459 set_size(uint64_t size)
1460 { this->size_ = size; }
20e6d0d6
DK
1461
1462 // Return the address alignment.
1463 uint64_t
1464 addralign() const
1465 { return this->addralign_; }
1466
1467 // Set the address alignment.
1468 void
2ea97941
ILT
1469 set_addralign(uint64_t addralign)
1470 { this->addralign_ = addralign; }
20e6d0d6
DK
1471
1472 private:
1473 // Input section, can be a relaxed section.
6625d24e 1474 Output_section::Input_section input_section_;
20e6d0d6
DK
1475 // Name of the section.
1476 std::string section_name_;
1477 // Section size.
1478 uint64_t size_;
1479 // Address alignment.
1480 uint64_t addralign_;
a445fddf
ILT
1481};
1482
1483// A class to sort the input sections.
1484
1485class Input_section_sorter
1486{
1487 public:
1488 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1489 : filename_sort_(filename_sort), section_sort_(section_sort)
1490 { }
1491
1492 bool
1493 operator()(const Input_section_info&, const Input_section_info&) const;
1494
1495 private:
1496 Sort_wildcard filename_sort_;
1497 Sort_wildcard section_sort_;
1498};
1499
1500bool
1501Input_section_sorter::operator()(const Input_section_info& isi1,
1502 const Input_section_info& isi2) const
1503{
1504 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1505 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1506 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
20e6d0d6 1507 && isi1.addralign() == isi2.addralign()))
a445fddf 1508 {
20e6d0d6
DK
1509 if (isi1.section_name() != isi2.section_name())
1510 return isi1.section_name() < isi2.section_name();
a445fddf
ILT
1511 }
1512 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1513 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1514 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1515 {
20e6d0d6
DK
1516 if (isi1.addralign() != isi2.addralign())
1517 return isi1.addralign() < isi2.addralign();
a445fddf
ILT
1518 }
1519 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1520 {
20e6d0d6
DK
1521 if (isi1.relobj()->name() != isi2.relobj()->name())
1522 return (isi1.relobj()->name() < isi2.relobj()->name());
a445fddf
ILT
1523 }
1524
1525 // Otherwise we leave them in the same order.
1526 return false;
1527}
1528
1529// Set the section address. Look in INPUT_SECTIONS for sections which
1530// match this spec, sort them as specified, and add them to the output
1531// section.
1532
1533void
1534Output_section_element_input::set_section_addresses(
1535 Symbol_table*,
20e6d0d6 1536 Layout* layout,
a445fddf
ILT
1537 Output_section* output_section,
1538 uint64_t subalign,
1539 uint64_t* dot_value,
f6973bdc 1540 uint64_t*,
77e65537 1541 Output_section** dot_section,
a445fddf
ILT
1542 std::string* fill,
1543 Input_section_list* input_sections)
1544{
1545 // We build a list of sections which match each
1546 // Input_section_pattern.
1547
1548 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1549 size_t input_pattern_count = this->input_section_patterns_.size();
1550 if (input_pattern_count == 0)
1551 input_pattern_count = 1;
1552 Matching_sections matching_sections(input_pattern_count);
1553
1554 // Look through the list of sections for this output section. Add
1555 // each one which matches to one of the elements of
1556 // MATCHING_SECTIONS.
1557
1558 Input_section_list::iterator p = input_sections->begin();
1559 while (p != input_sections->end())
1560 {
20e6d0d6
DK
1561 Relobj* relobj = p->relobj();
1562 unsigned int shndx = p->shndx();
1563 Input_section_info isi(*p);
1564
a445fddf
ILT
1565 // Calling section_name and section_addralign is not very
1566 // efficient.
a445fddf
ILT
1567
1568 // Lock the object so that we can get information about the
1569 // section. This is OK since we know we are single-threaded
1570 // here.
1571 {
1572 const Task* task = reinterpret_cast<const Task*>(-1);
20e6d0d6
DK
1573 Task_lock_obj<Object> tl(task, relobj);
1574
1575 isi.set_section_name(relobj->section_name(shndx));
1576 if (p->is_relaxed_input_section())
c0a62865 1577 {
ea5cae92 1578 // We use current data size because relaxed section sizes may not
c0a62865
DK
1579 // have finalized yet.
1580 isi.set_size(p->relaxed_input_section()->current_data_size());
1581 isi.set_addralign(p->relaxed_input_section()->addralign());
1582 }
20e6d0d6 1583 else
c0a62865
DK
1584 {
1585 isi.set_size(relobj->section_size(shndx));
1586 isi.set_addralign(relobj->section_addralign(shndx));
1587 }
a445fddf
ILT
1588 }
1589
20e6d0d6 1590 if (!this->match_file_name(relobj->name().c_str()))
a445fddf
ILT
1591 ++p;
1592 else if (this->input_section_patterns_.empty())
1593 {
1594 matching_sections[0].push_back(isi);
1595 p = input_sections->erase(p);
1596 }
1597 else
1598 {
1599 size_t i;
1600 for (i = 0; i < input_pattern_count; ++i)
1601 {
1602 const Input_section_pattern&
1603 isp(this->input_section_patterns_[i]);
20e6d0d6 1604 if (match(isi.section_name().c_str(), isp.pattern.c_str(),
a445fddf
ILT
1605 isp.pattern_is_wildcard))
1606 break;
1607 }
1608
1609 if (i >= this->input_section_patterns_.size())
1610 ++p;
1611 else
1612 {
1613 matching_sections[i].push_back(isi);
1614 p = input_sections->erase(p);
1615 }
1616 }
1617 }
1618
1619 // Look through MATCHING_SECTIONS. Sort each one as specified,
1620 // using a stable sort so that we get the default order when
1621 // sections are otherwise equal. Add each input section to the
1622 // output section.
1623
661be1e2 1624 uint64_t dot = *dot_value;
a445fddf
ILT
1625 for (size_t i = 0; i < input_pattern_count; ++i)
1626 {
1627 if (matching_sections[i].empty())
1628 continue;
1629
1630 gold_assert(output_section != NULL);
1631
1632 const Input_section_pattern& isp(this->input_section_patterns_[i]);
1633 if (isp.sort != SORT_WILDCARD_NONE
1634 || this->filename_sort_ != SORT_WILDCARD_NONE)
1635 std::stable_sort(matching_sections[i].begin(),
1636 matching_sections[i].end(),
1637 Input_section_sorter(this->filename_sort_,
1638 isp.sort));
1639
2ea97941 1640 for (std::vector<Input_section_info>::const_iterator p =
a445fddf 1641 matching_sections[i].begin();
2ea97941
ILT
1642 p != matching_sections[i].end();
1643 ++p)
a445fddf 1644 {
6625d24e
DK
1645 // Override the original address alignment if SUBALIGN is specified
1646 // and is greater than the original alignment. We need to make a
1647 // copy of the input section to modify the alignment.
1648 Output_section::Input_section sis(p->input_section());
1649
1650 uint64_t this_subalign = sis.addralign();
1651 if (!sis.is_input_section())
1652 sis.output_section_data()->finalize_data_size();
1653 uint64_t data_size = sis.data_size();
a445fddf 1654 if (this_subalign < subalign)
6625d24e
DK
1655 {
1656 this_subalign = subalign;
1657 sis.set_addralign(subalign);
1658 }
a445fddf 1659
661be1e2 1660 uint64_t address = align_address(dot, this_subalign);
a445fddf 1661
661be1e2 1662 if (address > dot && !fill->empty())
a445fddf
ILT
1663 {
1664 section_size_type length =
661be1e2 1665 convert_to_section_size_type(address - dot);
a445fddf
ILT
1666 std::string this_fill = this->get_fill_string(fill, length);
1667 Output_section_data* posd = new Output_data_const(this_fill, 0);
1668 output_section->add_output_section_data(posd);
20e6d0d6 1669 layout->new_output_section_data_from_script(posd);
a445fddf
ILT
1670 }
1671
6625d24e
DK
1672 output_section->add_script_input_section(sis);
1673 dot = address + data_size;
a445fddf
ILT
1674 }
1675 }
1676
661be1e2
ILT
1677 // An SHF_TLS/SHT_NOBITS section does not take up any
1678 // address space.
1679 if (output_section == NULL
1680 || (output_section->flags() & elfcpp::SHF_TLS) == 0
1681 || output_section->type() != elfcpp::SHT_NOBITS)
1682 *dot_value = dot;
1683
a445fddf 1684 this->final_dot_value_ = *dot_value;
77e65537 1685 this->final_dot_section_ = *dot_section;
a445fddf
ILT
1686}
1687
494e05f4
ILT
1688// Print for debugging.
1689
1690void
1691Output_section_element_input::print(FILE* f) const
1692{
1693 fprintf(f, " ");
1694
1695 if (this->keep_)
1696 fprintf(f, "KEEP(");
1697
1698 if (!this->filename_pattern_.empty())
1699 {
1700 bool need_close_paren = false;
1701 switch (this->filename_sort_)
1702 {
1703 case SORT_WILDCARD_NONE:
1704 break;
1705 case SORT_WILDCARD_BY_NAME:
1706 fprintf(f, "SORT_BY_NAME(");
1707 need_close_paren = true;
1708 break;
1709 default:
1710 gold_unreachable();
1711 }
1712
1713 fprintf(f, "%s", this->filename_pattern_.c_str());
1714
1715 if (need_close_paren)
1716 fprintf(f, ")");
1717 }
1718
1719 if (!this->input_section_patterns_.empty()
1720 || !this->filename_exclusions_.empty())
1721 {
1722 fprintf(f, "(");
1723
1724 bool need_space = false;
1725 if (!this->filename_exclusions_.empty())
1726 {
1727 fprintf(f, "EXCLUDE_FILE(");
1728 bool need_comma = false;
1729 for (Filename_exclusions::const_iterator p =
1730 this->filename_exclusions_.begin();
1731 p != this->filename_exclusions_.end();
1732 ++p)
1733 {
1734 if (need_comma)
1735 fprintf(f, ", ");
a445fddf 1736 fprintf(f, "%s", p->first.c_str());
494e05f4
ILT
1737 need_comma = true;
1738 }
1739 fprintf(f, ")");
1740 need_space = true;
1741 }
1742
1743 for (Input_section_patterns::const_iterator p =
1744 this->input_section_patterns_.begin();
1745 p != this->input_section_patterns_.end();
1746 ++p)
1747 {
1748 if (need_space)
1749 fprintf(f, " ");
1750
1751 int close_parens = 0;
1752 switch (p->sort)
1753 {
1754 case SORT_WILDCARD_NONE:
1755 break;
1756 case SORT_WILDCARD_BY_NAME:
1757 fprintf(f, "SORT_BY_NAME(");
1758 close_parens = 1;
1759 break;
1760 case SORT_WILDCARD_BY_ALIGNMENT:
1761 fprintf(f, "SORT_BY_ALIGNMENT(");
1762 close_parens = 1;
1763 break;
1764 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1765 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1766 close_parens = 2;
1767 break;
1768 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1769 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1770 close_parens = 2;
1771 break;
1772 default:
1773 gold_unreachable();
1774 }
1775
1776 fprintf(f, "%s", p->pattern.c_str());
1777
1778 for (int i = 0; i < close_parens; ++i)
1779 fprintf(f, ")");
1780
1781 need_space = true;
1782 }
1783
1784 fprintf(f, ")");
1785 }
1786
1787 if (this->keep_)
1788 fprintf(f, ")");
1789
1790 fprintf(f, "\n");
1791}
1792
1793// An output section.
1794
1795class Output_section_definition : public Sections_element
1796{
1797 public:
a445fddf
ILT
1798 typedef Output_section_element::Input_section_list Input_section_list;
1799
494e05f4
ILT
1800 Output_section_definition(const char* name, size_t namelen,
1801 const Parser_output_section_header* header);
1802
1803 // Finish the output section with the information in the trailer.
1804 void
1805 finish(const Parser_output_section_trailer* trailer);
1806
1807 // Add a symbol to be defined.
1808 void
1809 add_symbol_assignment(const char* name, size_t length, Expression* value,
1810 bool provide, bool hidden);
a445fddf
ILT
1811
1812 // Add an assignment to the special dot symbol.
1813 void
1814 add_dot_assignment(Expression* value);
1815
494e05f4
ILT
1816 // Add an assertion.
1817 void
1818 add_assertion(Expression* check, const char* message, size_t messagelen);
1819
1820 // Add a data item to the current output section.
1821 void
1822 add_data(int size, bool is_signed, Expression* val);
1823
1824 // Add a setting for the fill value.
1825 void
1826 add_fill(Expression* val);
1827
1828 // Add an input section specification.
1829 void
1830 add_input_section(const Input_section_spec* spec, bool keep);
1831
0d371ad3
ILT
1832 // Return whether the output section is relro.
1833 bool
1834 is_relro() const
1835 { return this->is_relro_; }
1836
2d924fd9
ILT
1837 // Record that the output section is relro.
1838 void
1839 set_is_relro()
1840 { this->is_relro_ = true; }
1841
919ed24c
ILT
1842 // Create any required output sections.
1843 void
1844 create_sections(Layout*);
1845
a445fddf
ILT
1846 // Add any symbols being defined to the symbol table.
1847 void
1848 add_symbols_to_table(Symbol_table* symtab);
1849
1850 // Finalize symbols and check assertions.
1851 void
77e65537 1852 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
a445fddf
ILT
1853
1854 // Return the output section name to use for an input file name and
1855 // section name.
1856 const char*
1857 output_section_name(const char* file_name, const char* section_name,
1e5d2fb1 1858 Output_section***, Script_sections::Section_type*);
a445fddf 1859
0d371ad3
ILT
1860 // Initialize OSP with an output section.
1861 void
1862 orphan_section_init(Orphan_section_placement* osp,
1863 Script_sections::Elements_iterator p)
1864 { osp->output_section_init(this->name_, this->output_section_, p); }
a445fddf
ILT
1865
1866 // Set the section address.
1867 void
1868 set_section_addresses(Symbol_table* symtab, Layout* layout,
f6973bdc
ILT
1869 uint64_t* dot_value, uint64_t*,
1870 uint64_t* load_address);
a445fddf 1871
3802b2dd
ILT
1872 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1873 // this section is constrained, and the input sections do not match,
1874 // return the constraint, and set *POSD.
1875 Section_constraint
1876 check_constraint(Output_section_definition** posd);
1877
1878 // See if this is the alternate output section for a constrained
1879 // output section. If it is, transfer the Output_section and return
1880 // true. Otherwise return false.
1881 bool
1882 alternate_constraint(Output_section_definition*, Section_constraint);
1883
1c4f3631 1884 // Get the list of segments to use for an allocated section when
2cefc357 1885 // using a PHDRS clause.
1c4f3631 1886 Output_section*
2cefc357 1887 allocate_to_segment(String_list** phdrs_list, bool* orphan);
1c4f3631 1888
8f2eb564
ILT
1889 // Look for an output section by name and return the address, the
1890 // load address, the alignment, and the size. This is used when an
1891 // expression refers to an output section which was not actually
1892 // created. This returns true if the section was found, false
1893 // otherwise.
1894 bool
1895 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1896 uint64_t*) const;
1897
2d924fd9
ILT
1898 // Return the associated Output_section if there is one.
1899 Output_section*
1900 get_output_section() const
1901 { return this->output_section_; }
1902
494e05f4
ILT
1903 // Print the contents to the FILE. This is for debugging.
1904 void
1905 print(FILE*) const;
1906
1e5d2fb1
DK
1907 // Return the output section type if specified or Script_sections::ST_NONE.
1908 Script_sections::Section_type
1909 section_type() const;
1910
7f8cd844
NC
1911 // Store the memory region to use.
1912 void
1913 set_memory_region(Memory_region*, bool set_vma);
1914
1915 void
1916 set_section_vma(Expression* address)
1917 { this->address_ = address; }
1918
1919 void
1920 set_section_lma(Expression* address)
1921 { this->load_address_ = address; }
1922
ea5cae92
NC
1923 const std::string&
1924 get_section_name() const
7f8cd844
NC
1925 { return this->name_; }
1926
494e05f4 1927 private:
1e5d2fb1
DK
1928 static const char*
1929 script_section_type_name(Script_section_type);
1930
494e05f4
ILT
1931 typedef std::vector<Output_section_element*> Output_section_elements;
1932
1933 // The output section name.
1934 std::string name_;
1935 // The address. This may be NULL.
1936 Expression* address_;
1937 // The load address. This may be NULL.
1938 Expression* load_address_;
1939 // The alignment. This may be NULL.
1940 Expression* align_;
1941 // The input section alignment. This may be NULL.
1942 Expression* subalign_;
3802b2dd
ILT
1943 // The constraint, if any.
1944 Section_constraint constraint_;
494e05f4
ILT
1945 // The fill value. This may be NULL.
1946 Expression* fill_;
1c4f3631
ILT
1947 // The list of segments this section should go into. This may be
1948 // NULL.
1949 String_list* phdrs_;
494e05f4
ILT
1950 // The list of elements defining the section.
1951 Output_section_elements elements_;
a445fddf
ILT
1952 // The Output_section created for this definition. This will be
1953 // NULL if none was created.
1954 Output_section* output_section_;
8f2eb564
ILT
1955 // The address after it has been evaluated.
1956 uint64_t evaluated_address_;
1957 // The load address after it has been evaluated.
1958 uint64_t evaluated_load_address_;
1959 // The alignment after it has been evaluated.
1960 uint64_t evaluated_addralign_;
2d924fd9
ILT
1961 // The output section is relro.
1962 bool is_relro_;
1e5d2fb1
DK
1963 // The output section type if specified.
1964 enum Script_section_type script_section_type_;
494e05f4
ILT
1965};
1966
1967// Constructor.
1968
1969Output_section_definition::Output_section_definition(
1970 const char* name,
1971 size_t namelen,
1972 const Parser_output_section_header* header)
1973 : name_(name, namelen),
1974 address_(header->address),
1975 load_address_(header->load_address),
1976 align_(header->align),
1977 subalign_(header->subalign),
3802b2dd 1978 constraint_(header->constraint),
494e05f4 1979 fill_(NULL),
1c4f3631 1980 phdrs_(NULL),
a445fddf 1981 elements_(),
2d924fd9
ILT
1982 output_section_(NULL),
1983 evaluated_address_(0),
1984 evaluated_load_address_(0),
1985 evaluated_addralign_(0),
1e5d2fb1
DK
1986 is_relro_(false),
1987 script_section_type_(header->section_type)
494e05f4
ILT
1988{
1989}
1990
1991// Finish an output section.
1992
1993void
1994Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1995{
1996 this->fill_ = trailer->fill;
1c4f3631 1997 this->phdrs_ = trailer->phdrs;
494e05f4
ILT
1998}
1999
2000// Add a symbol to be defined.
2001
2002void
2003Output_section_definition::add_symbol_assignment(const char* name,
2004 size_t length,
2005 Expression* value,
2006 bool provide,
2007 bool hidden)
2008{
2009 Output_section_element* p = new Output_section_element_assignment(name,
2010 length,
2011 value,
2012 provide,
2013 hidden);
2014 this->elements_.push_back(p);
2015}
2016
a445fddf 2017// Add an assignment to the special dot symbol.
494e05f4
ILT
2018
2019void
a445fddf
ILT
2020Output_section_definition::add_dot_assignment(Expression* value)
2021{
2022 Output_section_element* p = new Output_section_element_dot_assignment(value);
2023 this->elements_.push_back(p);
2024}
2025
2026// Add an assertion.
2027
2028void
2029Output_section_definition::add_assertion(Expression* check,
2030 const char* message,
494e05f4
ILT
2031 size_t messagelen)
2032{
2033 Output_section_element* p = new Output_section_element_assertion(check,
2034 message,
2035 messagelen);
2036 this->elements_.push_back(p);
2037}
2038
2039// Add a data item to the current output section.
2040
2041void
2042Output_section_definition::add_data(int size, bool is_signed, Expression* val)
2043{
2044 Output_section_element* p = new Output_section_element_data(size, is_signed,
2045 val);
2046 this->elements_.push_back(p);
2047}
2048
2049// Add a setting for the fill value.
2050
2051void
2052Output_section_definition::add_fill(Expression* val)
2053{
2054 Output_section_element* p = new Output_section_element_fill(val);
2055 this->elements_.push_back(p);
2056}
2057
2058// Add an input section specification.
2059
2060void
2061Output_section_definition::add_input_section(const Input_section_spec* spec,
2062 bool keep)
2063{
2064 Output_section_element* p = new Output_section_element_input(spec, keep);
2065 this->elements_.push_back(p);
2066}
2067
919ed24c
ILT
2068// Create any required output sections. We need an output section if
2069// there is a data statement here.
2070
2071void
2072Output_section_definition::create_sections(Layout* layout)
2073{
2074 if (this->output_section_ != NULL)
2075 return;
2076 for (Output_section_elements::const_iterator p = this->elements_.begin();
2077 p != this->elements_.end();
2078 ++p)
2079 {
2080 if ((*p)->needs_output_section())
2081 {
2082 const char* name = this->name_.c_str();
1e5d2fb1
DK
2083 this->output_section_ =
2084 layout->make_output_section_for_script(name, this->section_type());
919ed24c
ILT
2085 return;
2086 }
2087 }
2088}
2089
a445fddf
ILT
2090// Add any symbols being defined to the symbol table.
2091
2092void
2093Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
2094{
2095 for (Output_section_elements::iterator p = this->elements_.begin();
2096 p != this->elements_.end();
2097 ++p)
2098 (*p)->add_symbols_to_table(symtab);
2099}
2100
2101// Finalize symbols and check assertions.
2102
2103void
2104Output_section_definition::finalize_symbols(Symbol_table* symtab,
2105 const Layout* layout,
a445fddf
ILT
2106 uint64_t* dot_value)
2107{
2108 if (this->output_section_ != NULL)
2109 *dot_value = this->output_section_->address();
2110 else
2111 {
2112 uint64_t address = *dot_value;
2113 if (this->address_ != NULL)
2114 {
919ed24c 2115 address = this->address_->eval_with_dot(symtab, layout, true,
77e65537 2116 *dot_value, NULL,
bacff3ab 2117 NULL, NULL);
a445fddf
ILT
2118 }
2119 if (this->align_ != NULL)
2120 {
919ed24c 2121 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
bacff3ab
NC
2122 *dot_value, NULL,
2123 NULL, NULL);
a445fddf
ILT
2124 address = align_address(address, align);
2125 }
2126 *dot_value = address;
2127 }
a445fddf 2128
77e65537 2129 Output_section* dot_section = this->output_section_;
a445fddf
ILT
2130 for (Output_section_elements::iterator p = this->elements_.begin();
2131 p != this->elements_.end();
2132 ++p)
77e65537 2133 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
a445fddf
ILT
2134}
2135
2136// Return the output section name to use for an input section name.
2137
2138const char*
1e5d2fb1
DK
2139Output_section_definition::output_section_name(
2140 const char* file_name,
2141 const char* section_name,
2142 Output_section*** slot,
ca09d69a 2143 Script_sections::Section_type* psection_type)
a445fddf
ILT
2144{
2145 // Ask each element whether it matches NAME.
2146 for (Output_section_elements::const_iterator p = this->elements_.begin();
2147 p != this->elements_.end();
2148 ++p)
2149 {
2150 if ((*p)->match_name(file_name, section_name))
2151 {
2152 // We found a match for NAME, which means that it should go
2153 // into this output section.
2154 *slot = &this->output_section_;
1e5d2fb1 2155 *psection_type = this->section_type();
a445fddf
ILT
2156 return this->name_.c_str();
2157 }
2158 }
2159
2160 // We don't know about this section name.
2161 return NULL;
2162}
2163
ea5cae92
NC
2164// Return true if memory from START to START + LENGTH is contained
2165// within a memory region.
2166
2167bool
2168Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
2169 uint64_t start, uint64_t length) const
2170{
2171 if (this->memory_regions_ == NULL)
2172 return false;
2173
2174 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2175 mr != this->memory_regions_->end();
2176 ++mr)
2177 {
2178 uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
2179 uint64_t l = (*mr)->length()->eval(symtab, layout, false);
2180
2181 if (s <= start
2182 && (s + l) >= (start + length))
2183 return true;
2184 }
2185
2186 return false;
2187}
2188
2189// Find a memory region that should be used by a given output SECTION.
2190// If provided set PREVIOUS_SECTION_RETURN to point to the last section
2191// that used the return memory region.
2192
2193Memory_region*
2194Script_sections::find_memory_region(
2195 Output_section_definition* section,
2196 bool find_vma_region,
2197 Output_section_definition** previous_section_return)
2198{
2199 if (previous_section_return != NULL)
2200 * previous_section_return = NULL;
2201
2202 // Walk the memory regions specified in this script, if any.
2203 if (this->memory_regions_ == NULL)
2204 return NULL;
2205
2206 // The /DISCARD/ section never gets assigned to any region.
2207 if (section->get_section_name() == "/DISCARD/")
2208 return NULL;
2209
2210 Memory_region* first_match = NULL;
2211
2212 // First check to see if a region has been assigned to this section.
2213 for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2214 mr != this->memory_regions_->end();
2215 ++mr)
2216 {
2217 if (find_vma_region)
2218 {
2219 for (Memory_region::Section_list::const_iterator s =
2220 (*mr)->get_vma_section_list_start();
2221 s != (*mr)->get_vma_section_list_end();
2222 ++s)
2223 if ((*s) == section)
2224 {
2225 (*mr)->set_last_section(section);
2226 return *mr;
2227 }
2228 }
2229 else
2230 {
2231 for (Memory_region::Section_list::const_iterator s =
2232 (*mr)->get_lma_section_list_start();
2233 s != (*mr)->get_lma_section_list_end();
2234 ++s)
2235 if ((*s) == section)
2236 {
2237 (*mr)->set_last_section(section);
2238 return *mr;
2239 }
2240 }
2241
2242 // Make a note of the first memory region whose attributes
2243 // are compatible with the section. If we do not find an
2244 // explicit region assignment, then we will return this region.
2245 Output_section* out_sec = section->get_output_section();
2246 if (first_match == NULL
3f9a3278 2247 && out_sec != NULL
ea5cae92
NC
2248 && (*mr)->attributes_compatible(out_sec->flags(),
2249 out_sec->type()))
2250 first_match = *mr;
2251 }
2252
2253 // With LMA computations, if an explicit region has not been specified then
2254 // we will want to set the difference between the VMA and the LMA of the
2255 // section were searching for to be the same as the difference between the
2256 // VMA and LMA of the last section to be added to first matched region.
2257 // Hence, if it was asked for, we return a pointer to the last section
2258 // known to be used by the first matched region.
2259 if (first_match != NULL
2260 && previous_section_return != NULL)
2261 *previous_section_return = first_match->get_last_section();
2262
2263 return first_match;
2264}
2265
a445fddf
ILT
2266// Set the section address. Note that the OUTPUT_SECTION_ field will
2267// be NULL if no input sections were mapped to this output section.
2268// We still have to adjust dot and process symbol assignments.
2269
2270void
2271Output_section_definition::set_section_addresses(Symbol_table* symtab,
2272 Layout* layout,
fd247bfe 2273 uint64_t* dot_value,
f6973bdc 2274 uint64_t* dot_alignment,
fd247bfe 2275 uint64_t* load_address)
a445fddf 2276{
ea5cae92
NC
2277 Memory_region* vma_region = NULL;
2278 Memory_region* lma_region = NULL;
2279 Script_sections* script_sections =
2280 layout->script_options()->script_sections();
a445fddf 2281 uint64_t address;
1e5d2fb1
DK
2282 uint64_t old_dot_value = *dot_value;
2283 uint64_t old_load_address = *load_address;
2284
ea5cae92
NC
2285 // Decide the start address for the section. The algorithm is:
2286 // 1) If an address has been specified in a linker script, use that.
2287 // 2) Otherwise if a memory region has been specified for the section,
2288 // use the next free address in the region.
2289 // 3) Otherwise if memory regions have been specified find the first
2290 // region whose attributes are compatible with this section and
2291 // install it into that region.
2292 // 4) Otherwise use the current location counter.
2293
2294 if (this->output_section_ != NULL
2295 // Check for --section-start.
2296 && parameters->options().section_start(this->output_section_->name(),
2297 &address))
2298 ;
2299 else if (this->address_ == NULL)
a445fddf 2300 {
ea5cae92
NC
2301 vma_region = script_sections->find_memory_region(this, true, NULL);
2302
2303 if (vma_region != NULL)
2304 address = vma_region->get_current_address()->eval(symtab, layout,
2305 false);
f4187277 2306 else
ea5cae92 2307 address = *dot_value;
a445fddf 2308 }
ea5cae92
NC
2309 else
2310 address = this->address_->eval_with_dot(symtab, layout, true,
2311 *dot_value, NULL, NULL,
2312 dot_alignment);
a445fddf
ILT
2313 uint64_t align;
2314 if (this->align_ == NULL)
2315 {
2316 if (this->output_section_ == NULL)
2317 align = 0;
2318 else
2319 align = this->output_section_->addralign();
2320 }
2321 else
2322 {
77e65537 2323 Output_section* align_section;
919ed24c 2324 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
f6973bdc 2325 NULL, &align_section, NULL);
77e65537
ILT
2326 if (align_section != NULL)
2327 gold_warning(_("alignment of section %s is not absolute"),
2328 this->name_.c_str());
a445fddf
ILT
2329 if (this->output_section_ != NULL)
2330 this->output_section_->set_addralign(align);
2331 }
2332
2333 address = align_address(address, align);
2334
fd247bfe
ILT
2335 uint64_t start_address = address;
2336
a445fddf 2337 *dot_value = address;
a445fddf 2338
1e5d2fb1
DK
2339 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2340 // forced to zero, regardless of what the linker script wants.
a445fddf 2341 if (this->output_section_ != NULL
1e5d2fb1
DK
2342 && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
2343 || this->output_section_->is_noload()))
a445fddf
ILT
2344 this->output_section_->set_address(address);
2345
8f2eb564
ILT
2346 this->evaluated_address_ = address;
2347 this->evaluated_addralign_ = align;
2348
ea5cae92
NC
2349 uint64_t laddr;
2350
8f2eb564 2351 if (this->load_address_ == NULL)
ea5cae92
NC
2352 {
2353 Output_section_definition* previous_section;
2354
2355 // Determine if an LMA region has been set for this section.
2356 lma_region = script_sections->find_memory_region(this, false,
2357 &previous_section);
2358
2359 if (lma_region != NULL)
2360 {
2361 if (previous_section == NULL)
2362 // The LMA address was explicitly set to the given region.
2363 laddr = lma_region->get_current_address()->eval(symtab, layout,
2364 false);
2365 else
2366 {
2367 // We are not going to use the discovered lma_region, so
2368 // make sure that we do not update it in the code below.
2369 lma_region = NULL;
2370
2371 if (this->address_ != NULL || previous_section == this)
2372 {
2373 // Either an explicit VMA address has been set, or an
2374 // explicit VMA region has been set, so set the LMA equal to
2375 // the VMA.
2376 laddr = address;
2377 }
2378 else
2379 {
2380 // The LMA address was not explicitly or implicitly set.
2381 //
2382 // We have been given the first memory region that is
2383 // compatible with the current section and a pointer to the
2384 // last section to use this region. Set the LMA of this
2385 // section so that the difference between its' VMA and LMA
2386 // is the same as the difference between the VMA and LMA of
2387 // the last section in the given region.
2388 laddr = address + (previous_section->evaluated_load_address_
2389 - previous_section->evaluated_address_);
2390 }
2391 }
2392
2393 if (this->output_section_ != NULL)
2394 this->output_section_->set_load_address(laddr);
2395 }
2396 else
2397 {
2398 // Do not set the load address of the output section, if one exists.
2399 // This allows future sections to determine what the load address
2400 // should be. If none is ever set, it will default to being the
2401 // same as the vma address.
2402 laddr = address;
2403 }
2404 }
8f2eb564 2405 else
a445fddf 2406 {
ea5cae92
NC
2407 laddr = this->load_address_->eval_with_dot(symtab, layout, true,
2408 *dot_value,
2409 this->output_section_,
2410 NULL, NULL);
8f2eb564 2411 if (this->output_section_ != NULL)
55458500 2412 this->output_section_->set_load_address(laddr);
a445fddf
ILT
2413 }
2414
ea5cae92
NC
2415 this->evaluated_load_address_ = laddr;
2416
a445fddf
ILT
2417 uint64_t subalign;
2418 if (this->subalign_ == NULL)
2419 subalign = 0;
2420 else
2421 {
77e65537 2422 Output_section* subalign_section;
919ed24c
ILT
2423 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2424 *dot_value, NULL,
f6973bdc 2425 &subalign_section, NULL);
77e65537
ILT
2426 if (subalign_section != NULL)
2427 gold_warning(_("subalign of section %s is not absolute"),
2428 this->name_.c_str());
a445fddf
ILT
2429 }
2430
2431 std::string fill;
2432 if (this->fill_ != NULL)
2433 {
2434 // FIXME: The GNU linker supports fill values of arbitrary
2435 // length.
77e65537 2436 Output_section* fill_section;
919ed24c 2437 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
a445fddf 2438 *dot_value,
f6973bdc
ILT
2439 NULL, &fill_section,
2440 NULL);
77e65537
ILT
2441 if (fill_section != NULL)
2442 gold_warning(_("fill of section %s is not absolute"),
2443 this->name_.c_str());
a445fddf
ILT
2444 unsigned char fill_buff[4];
2445 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2446 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2447 }
2448
2449 Input_section_list input_sections;
2450 if (this->output_section_ != NULL)
2451 {
2452 // Get the list of input sections attached to this output
2453 // section. This will leave the output section with only
2454 // Output_section_data entries.
2455 address += this->output_section_->get_input_sections(address,
2456 fill,
2457 &input_sections);
2458 *dot_value = address;
2459 }
2460
77e65537 2461 Output_section* dot_section = this->output_section_;
a445fddf
ILT
2462 for (Output_section_elements::iterator p = this->elements_.begin();
2463 p != this->elements_.end();
2464 ++p)
2465 (*p)->set_section_addresses(symtab, layout, this->output_section_,
f6973bdc
ILT
2466 subalign, dot_value, dot_alignment,
2467 &dot_section, &fill, &input_sections);
a445fddf
ILT
2468
2469 gold_assert(input_sections.empty());
fd247bfe 2470
ea5cae92
NC
2471 if (vma_region != NULL)
2472 {
2473 // Update the VMA region being used by the section now that we know how
2474 // big it is. Use the current address in the region, rather than
2475 // start_address because that might have been aligned upwards and we
2476 // need to allow for the padding.
2477 Expression* addr = vma_region->get_current_address();
2478 uint64_t size = *dot_value - addr->eval(symtab, layout, false);
2479
2480 vma_region->increment_offset(this->get_section_name(), size,
2481 symtab, layout);
2482 }
2483
2484 // If the LMA region is different from the VMA region, then increment the
2485 // offset there as well. Note that we use the same "dot_value -
2486 // start_address" formula that is used in the load_address assignment below.
2487 if (lma_region != NULL && lma_region != vma_region)
2488 lma_region->increment_offset(this->get_section_name(),
2489 *dot_value - start_address,
2490 symtab, layout);
2491
2492 // Compute the load address for the following section.
2493 if (this->output_section_ == NULL)
fd247bfe 2494 *load_address = *dot_value;
ea5cae92
NC
2495 else if (this->load_address_ == NULL)
2496 {
2497 if (lma_region == NULL)
2498 *load_address = *dot_value;
2499 else
2500 *load_address =
2501 lma_region->get_current_address()->eval(symtab, layout, false);
2502 }
fd247bfe
ILT
2503 else
2504 *load_address = (this->output_section_->load_address()
2505 + (*dot_value - start_address));
2d924fd9
ILT
2506
2507 if (this->output_section_ != NULL)
2508 {
2509 if (this->is_relro_)
2510 this->output_section_->set_is_relro();
2511 else
2512 this->output_section_->clear_is_relro();
1e5d2fb1
DK
2513
2514 // If this is a NOLOAD section, keep dot and load address unchanged.
2515 if (this->output_section_->is_noload())
2516 {
2517 *dot_value = old_dot_value;
2518 *load_address = old_load_address;
2519 }
2d924fd9 2520 }
a445fddf
ILT
2521}
2522
3802b2dd
ILT
2523// Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2524// this section is constrained, and the input sections do not match,
2525// return the constraint, and set *POSD.
2526
2527Section_constraint
2528Output_section_definition::check_constraint(Output_section_definition** posd)
2529{
2530 switch (this->constraint_)
2531 {
2532 case CONSTRAINT_NONE:
2533 return CONSTRAINT_NONE;
2534
2535 case CONSTRAINT_ONLY_IF_RO:
2536 if (this->output_section_ != NULL
2537 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2538 {
2539 *posd = this;
2540 return CONSTRAINT_ONLY_IF_RO;
2541 }
2542 return CONSTRAINT_NONE;
2543
2544 case CONSTRAINT_ONLY_IF_RW:
2545 if (this->output_section_ != NULL
2546 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2547 {
2548 *posd = this;
2549 return CONSTRAINT_ONLY_IF_RW;
2550 }
2551 return CONSTRAINT_NONE;
2552
2553 case CONSTRAINT_SPECIAL:
2554 if (this->output_section_ != NULL)
2555 gold_error(_("SPECIAL constraints are not implemented"));
2556 return CONSTRAINT_NONE;
2557
2558 default:
2559 gold_unreachable();
2560 }
2561}
2562
2563// See if this is the alternate output section for a constrained
2564// output section. If it is, transfer the Output_section and return
2565// true. Otherwise return false.
2566
2567bool
2568Output_section_definition::alternate_constraint(
2569 Output_section_definition* posd,
2570 Section_constraint constraint)
2571{
2572 if (this->name_ != posd->name_)
2573 return false;
2574
2575 switch (constraint)
2576 {
2577 case CONSTRAINT_ONLY_IF_RO:
2578 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2579 return false;
2580 break;
2581
2582 case CONSTRAINT_ONLY_IF_RW:
2583 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2584 return false;
2585 break;
2586
2587 default:
2588 gold_unreachable();
2589 }
2590
2591 // We have found the alternate constraint. We just need to move
2592 // over the Output_section. When constraints are used properly,
2593 // THIS should not have an output_section pointer, as all the input
2594 // sections should have matched the other definition.
2595
2596 if (this->output_section_ != NULL)
2597 gold_error(_("mismatched definition for constrained sections"));
2598
2599 this->output_section_ = posd->output_section_;
2600 posd->output_section_ = NULL;
2601
2d924fd9
ILT
2602 if (this->is_relro_)
2603 this->output_section_->set_is_relro();
2604 else
2605 this->output_section_->clear_is_relro();
2606
3802b2dd
ILT
2607 return true;
2608}
2609
1c4f3631 2610// Get the list of segments to use for an allocated section when using
2cefc357 2611// a PHDRS clause.
1c4f3631
ILT
2612
2613Output_section*
2cefc357
ILT
2614Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2615 bool* orphan)
1c4f3631 2616{
d103a984
RÁE
2617 // Update phdrs_list even if we don't have an output section. It
2618 // might be used by the following sections.
2619 if (this->phdrs_ != NULL)
2620 *phdrs_list = this->phdrs_;
2621
1c4f3631
ILT
2622 if (this->output_section_ == NULL)
2623 return NULL;
2624 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2625 return NULL;
2cefc357 2626 *orphan = false;
1c4f3631
ILT
2627 return this->output_section_;
2628}
2629
8f2eb564
ILT
2630// Look for an output section by name and return the address, the load
2631// address, the alignment, and the size. This is used when an
2632// expression refers to an output section which was not actually
2633// created. This returns true if the section was found, false
2634// otherwise.
2635
2636bool
2637Output_section_definition::get_output_section_info(const char* name,
2638 uint64_t* address,
2639 uint64_t* load_address,
2ea97941 2640 uint64_t* addralign,
8f2eb564
ILT
2641 uint64_t* size) const
2642{
2643 if (this->name_ != name)
2644 return false;
2645
2646 if (this->output_section_ != NULL)
2647 {
2648 *address = this->output_section_->address();
2649 if (this->output_section_->has_load_address())
2650 *load_address = this->output_section_->load_address();
2651 else
2652 *load_address = *address;
2ea97941 2653 *addralign = this->output_section_->addralign();
8f2eb564
ILT
2654 *size = this->output_section_->current_data_size();
2655 }
2656 else
2657 {
2658 *address = this->evaluated_address_;
2659 *load_address = this->evaluated_load_address_;
2ea97941 2660 *addralign = this->evaluated_addralign_;
8f2eb564
ILT
2661 *size = 0;
2662 }
2663
2664 return true;
2665}
2666
494e05f4
ILT
2667// Print for debugging.
2668
2669void
2670Output_section_definition::print(FILE* f) const
2671{
2672 fprintf(f, " %s ", this->name_.c_str());
2673
2674 if (this->address_ != NULL)
2675 {
2676 this->address_->print(f);
2677 fprintf(f, " ");
2678 }
2679
1e5d2fb1
DK
2680 if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2681 fprintf(f, "(%s) ",
2682 this->script_section_type_name(this->script_section_type_));
2683
494e05f4
ILT
2684 fprintf(f, ": ");
2685
2686 if (this->load_address_ != NULL)
2687 {
2688 fprintf(f, "AT(");
2689 this->load_address_->print(f);
2690 fprintf(f, ") ");
2691 }
2692
2693 if (this->align_ != NULL)
2694 {
2695 fprintf(f, "ALIGN(");
2696 this->align_->print(f);
2697 fprintf(f, ") ");
2698 }
2699
2700 if (this->subalign_ != NULL)
2701 {
2702 fprintf(f, "SUBALIGN(");
2703 this->subalign_->print(f);
2704 fprintf(f, ") ");
2705 }
2706
2707 fprintf(f, "{\n");
2708
2709 for (Output_section_elements::const_iterator p = this->elements_.begin();
2710 p != this->elements_.end();
2711 ++p)
2712 (*p)->print(f);
2713
2714 fprintf(f, " }");
2715
2716 if (this->fill_ != NULL)
2717 {
2718 fprintf(f, " = ");
2719 this->fill_->print(f);
2720 }
2721
7d26c6cc
ILT
2722 if (this->phdrs_ != NULL)
2723 {
2724 for (String_list::const_iterator p = this->phdrs_->begin();
2725 p != this->phdrs_->end();
2726 ++p)
2727 fprintf(f, " :%s", p->c_str());
2728 }
2729
494e05f4
ILT
2730 fprintf(f, "\n");
2731}
2732
1e5d2fb1
DK
2733Script_sections::Section_type
2734Output_section_definition::section_type() const
2735{
2736 switch (this->script_section_type_)
2737 {
2738 case SCRIPT_SECTION_TYPE_NONE:
2739 return Script_sections::ST_NONE;
2740 case SCRIPT_SECTION_TYPE_NOLOAD:
2741 return Script_sections::ST_NOLOAD;
2742 case SCRIPT_SECTION_TYPE_COPY:
2743 case SCRIPT_SECTION_TYPE_DSECT:
2744 case SCRIPT_SECTION_TYPE_INFO:
2745 case SCRIPT_SECTION_TYPE_OVERLAY:
2746 // There are not really support so we treat them as ST_NONE. The
2747 // parse should have issued errors for them already.
2748 return Script_sections::ST_NONE;
2749 default:
2750 gold_unreachable();
2751 }
2752}
2753
2754// Return the name of a script section type.
2755
2756const char*
ca09d69a 2757Output_section_definition::script_section_type_name(
1e5d2fb1
DK
2758 Script_section_type script_section_type)
2759{
2760 switch (script_section_type)
2761 {
2762 case SCRIPT_SECTION_TYPE_NONE:
2763 return "NONE";
2764 case SCRIPT_SECTION_TYPE_NOLOAD:
2765 return "NOLOAD";
2766 case SCRIPT_SECTION_TYPE_DSECT:
2767 return "DSECT";
2768 case SCRIPT_SECTION_TYPE_COPY:
2769 return "COPY";
2770 case SCRIPT_SECTION_TYPE_INFO:
2771 return "INFO";
2772 case SCRIPT_SECTION_TYPE_OVERLAY:
2773 return "OVERLAY";
2774 default:
2775 gold_unreachable();
2776 }
2777}
2778
7f8cd844
NC
2779void
2780Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
2781{
2782 gold_assert(mr != NULL);
2783 // Add the current section to the specified region's list.
2784 mr->add_section(this, set_vma);
2785}
2786
a445fddf
ILT
2787// An output section created to hold orphaned input sections. These
2788// do not actually appear in linker scripts. However, for convenience
2789// when setting the output section addresses, we put a marker to these
2790// sections in the appropriate place in the list of SECTIONS elements.
2791
2792class Orphan_output_section : public Sections_element
2793{
2794 public:
2795 Orphan_output_section(Output_section* os)
2796 : os_(os)
2797 { }
2798
0d371ad3
ILT
2799 // Return whether the orphan output section is relro. We can just
2800 // check the output section because we always set the flag, if
2801 // needed, just after we create the Orphan_output_section.
a445fddf 2802 bool
0d371ad3
ILT
2803 is_relro() const
2804 { return this->os_->is_relro(); }
2805
2806 // Initialize OSP with an output section. This should have been
2807 // done already.
2808 void
2809 orphan_section_init(Orphan_section_placement*,
2810 Script_sections::Elements_iterator)
2811 { gold_unreachable(); }
a445fddf
ILT
2812
2813 // Set section addresses.
2814 void
f6973bdc
ILT
2815 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2816 uint64_t*);
a445fddf 2817
1c4f3631 2818 // Get the list of segments to use for an allocated section when
2cefc357 2819 // using a PHDRS clause.
1c4f3631 2820 Output_section*
2cefc357 2821 allocate_to_segment(String_list**, bool*);
1c4f3631 2822
2d924fd9
ILT
2823 // Return the associated Output_section.
2824 Output_section*
2825 get_output_section() const
2826 { return this->os_; }
2827
a445fddf
ILT
2828 // Print for debugging.
2829 void
2830 print(FILE* f) const
2831 {
2832 fprintf(f, " marker for orphaned output section %s\n",
2833 this->os_->name());
2834 }
2835
2836 private:
2837 Output_section* os_;
2838};
2839
a445fddf
ILT
2840// Set section addresses.
2841
2842void
2843Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
fd247bfe 2844 uint64_t* dot_value,
f6973bdc 2845 uint64_t*,
fd247bfe 2846 uint64_t* load_address)
a445fddf 2847{
6625d24e 2848 typedef std::list<Output_section::Input_section> Input_section_list;
a445fddf 2849
fd247bfe
ILT
2850 bool have_load_address = *load_address != *dot_value;
2851
a445fddf
ILT
2852 uint64_t address = *dot_value;
2853 address = align_address(address, this->os_->addralign());
2854
a94907d9
ILT
2855 // For a relocatable link, all orphan sections are put at
2856 // address 0. In general we expect all sections to be at
2857 // address 0 for a relocatable link, but we permit the linker
2858 // script to override that for specific output sections.
2859 if (parameters->options().relocatable())
2860 {
2861 address = 0;
2862 *load_address = 0;
2863 have_load_address = false;
2864 }
2865
a445fddf 2866 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
fd247bfe
ILT
2867 {
2868 this->os_->set_address(address);
2869 if (have_load_address)
2870 this->os_->set_load_address(align_address(*load_address,
2871 this->os_->addralign()));
2872 }
a445fddf
ILT
2873
2874 Input_section_list input_sections;
2875 address += this->os_->get_input_sections(address, "", &input_sections);
2876
2877 for (Input_section_list::iterator p = input_sections.begin();
2878 p != input_sections.end();
2879 ++p)
2880 {
6625d24e
DK
2881 uint64_t addralign = p->addralign();
2882 if (!p->is_input_section())
2883 p->output_section_data()->finalize_data_size();
2884 uint64_t size = p->data_size();
2ea97941 2885 address = align_address(address, addralign);
6625d24e 2886 this->os_->add_script_input_section(*p);
a445fddf
ILT
2887 address += size;
2888 }
2889
661be1e2
ILT
2890 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2891 if (this->os_ == NULL
2892 || (this->os_->flags() & elfcpp::SHF_TLS) == 0
2893 || this->os_->type() != elfcpp::SHT_NOBITS)
2894 {
2895 if (!have_load_address)
2896 *load_address = address;
2897 else
2898 *load_address += address - *dot_value;
fd247bfe 2899
661be1e2
ILT
2900 *dot_value = address;
2901 }
a445fddf
ILT
2902}
2903
1c4f3631
ILT
2904// Get the list of segments to use for an allocated section when using
2905// a PHDRS clause. If this is an allocated section, return the
2906// Output_section. We don't change the list of segments.
2907
2908Output_section*
2cefc357 2909Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
1c4f3631
ILT
2910{
2911 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2912 return NULL;
2cefc357 2913 *orphan = true;
1c4f3631
ILT
2914 return this->os_;
2915}
2916
2917// Class Phdrs_element. A program header from a PHDRS clause.
2918
2919class Phdrs_element
2920{
2921 public:
2ea97941
ILT
2922 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2923 bool includes_filehdr, bool includes_phdrs,
1c4f3631 2924 bool is_flags_valid, unsigned int flags,
2ea97941
ILT
2925 Expression* load_address)
2926 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2927 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2928 flags_(flags), load_address_(load_address), load_address_value_(0),
1c4f3631
ILT
2929 segment_(NULL)
2930 { }
2931
2932 // Return the name of this segment.
2933 const std::string&
2934 name() const
2935 { return this->name_; }
2936
2937 // Return the type of the segment.
2938 unsigned int
2939 type() const
2940 { return this->type_; }
2941
2942 // Whether to include the file header.
2943 bool
2944 includes_filehdr() const
2945 { return this->includes_filehdr_; }
2946
2947 // Whether to include the program headers.
2948 bool
2949 includes_phdrs() const
2950 { return this->includes_phdrs_; }
2951
2952 // Return whether there is a load address.
2953 bool
2954 has_load_address() const
2955 { return this->load_address_ != NULL; }
2956
2957 // Evaluate the load address expression if there is one.
2958 void
2ea97941 2959 eval_load_address(Symbol_table* symtab, Layout* layout)
1c4f3631
ILT
2960 {
2961 if (this->load_address_ != NULL)
2ea97941 2962 this->load_address_value_ = this->load_address_->eval(symtab, layout,
919ed24c 2963 true);
1c4f3631
ILT
2964 }
2965
2966 // Return the load address.
2967 uint64_t
2968 load_address() const
2969 {
2970 gold_assert(this->load_address_ != NULL);
2971 return this->load_address_value_;
2972 }
2973
2974 // Create the segment.
2975 Output_segment*
2976 create_segment(Layout* layout)
2977 {
2978 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2979 return this->segment_;
2980 }
2981
2982 // Return the segment.
2983 Output_segment*
2984 segment()
2985 { return this->segment_; }
2986
20e6d0d6
DK
2987 // Release the segment.
2988 void
2989 release_segment()
2990 { this->segment_ = NULL; }
2991
1c4f3631
ILT
2992 // Set the segment flags if appropriate.
2993 void
2994 set_flags_if_valid()
2995 {
2996 if (this->is_flags_valid_)
2997 this->segment_->set_flags(this->flags_);
2998 }
2999
7d26c6cc
ILT
3000 // Print for debugging.
3001 void
3002 print(FILE*) const;
3003
1c4f3631
ILT
3004 private:
3005 // The name used in the script.
3006 std::string name_;
3007 // The type of the segment (PT_LOAD, etc.).
3008 unsigned int type_;
3009 // Whether this segment includes the file header.
3010 bool includes_filehdr_;
3011 // Whether this segment includes the section headers.
3012 bool includes_phdrs_;
3013 // Whether the flags were explicitly specified.
3014 bool is_flags_valid_;
3015 // The flags for this segment (PF_R, etc.) if specified.
3016 unsigned int flags_;
3017 // The expression for the load address for this segment. This may
3018 // be NULL.
3019 Expression* load_address_;
3020 // The actual load address from evaluating the expression.
3021 uint64_t load_address_value_;
3022 // The segment itself.
3023 Output_segment* segment_;
3024};
3025
7d26c6cc
ILT
3026// Print for debugging.
3027
3028void
3029Phdrs_element::print(FILE* f) const
3030{
3031 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
3032 if (this->includes_filehdr_)
3033 fprintf(f, " FILEHDR");
3034 if (this->includes_phdrs_)
3035 fprintf(f, " PHDRS");
3036 if (this->is_flags_valid_)
3037 fprintf(f, " FLAGS(%u)", this->flags_);
3038 if (this->load_address_ != NULL)
3039 {
3040 fprintf(f, " AT(");
3041 this->load_address_->print(f);
3042 fprintf(f, ")");
3043 }
3044 fprintf(f, ";\n");
3045}
3046
7f8cd844
NC
3047// Add a memory region.
3048
3049void
3050Script_sections::add_memory_region(const char* name, size_t namelen,
3051 unsigned int attributes,
3052 Expression* start, Expression* length)
3053{
3054 if (this->memory_regions_ == NULL)
3055 this->memory_regions_ = new Memory_regions();
3056 else if (this->find_memory_region(name, namelen))
3057 {
ea5cae92 3058 gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
33dbc701 3059 name);
7f8cd844
NC
3060 // FIXME: Add a GOLD extension to allow multiple regions with the same
3061 // name. This would amount to a single region covering disjoint blocks
3062 // of memory, which is useful for embedded devices.
3063 }
3064
3065 // FIXME: Check the length and start values. Currently we allow
3066 // non-constant expressions for these values, whereas LD does not.
3067
3068 // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS. This would
3069 // describe a region that packs from the end address going down, rather
3070 // than the start address going up. This would be useful for embedded
3071 // devices.
3072
3073 this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
3074 start, length));
3075}
3076
3077// Find a memory region.
3078
3079Memory_region*
3080Script_sections::find_memory_region(const char* name, size_t namelen)
3081{
3082 if (this->memory_regions_ == NULL)
3083 return NULL;
3084
3085 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3086 m != this->memory_regions_->end();
3087 ++m)
3088 if ((*m)->name_match(name, namelen))
3089 return *m;
3090
3091 return NULL;
3092}
3093
3094// Find a memory region's origin.
3095
3096Expression*
3097Script_sections::find_memory_region_origin(const char* name, size_t namelen)
3098{
3099 Memory_region* mr = find_memory_region(name, namelen);
3100 if (mr == NULL)
3101 return NULL;
3102
3103 return mr->start_address();
3104}
3105
3106// Find a memory region's length.
3107
3108Expression*
3109Script_sections::find_memory_region_length(const char* name, size_t namelen)
3110{
3111 Memory_region* mr = find_memory_region(name, namelen);
3112 if (mr == NULL)
3113 return NULL;
3114
3115 return mr->length();
3116}
3117
3118// Set the memory region to use for the current section.
3119
3120void
3121Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
3122{
3123 gold_assert(!this->sections_elements_->empty());
3124 this->sections_elements_->back()->set_memory_region(mr, set_vma);
3125}
3126
494e05f4
ILT
3127// Class Script_sections.
3128
3129Script_sections::Script_sections()
3130 : saw_sections_clause_(false),
3131 in_sections_clause_(false),
3132 sections_elements_(NULL),
1c4f3631 3133 output_section_(NULL),
7f8cd844 3134 memory_regions_(NULL),
2d924fd9 3135 phdrs_elements_(NULL),
0d371ad3
ILT
3136 orphan_section_placement_(NULL),
3137 data_segment_align_start_(),
3138 saw_data_segment_align_(false),
3c12dcdb
DK
3139 saw_relro_end_(false),
3140 saw_segment_start_expression_(false)
494e05f4
ILT
3141{
3142}
3143
3144// Start a SECTIONS clause.
3145
3146void
3147Script_sections::start_sections()
3148{
3149 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
3150 this->saw_sections_clause_ = true;
3151 this->in_sections_clause_ = true;
3152 if (this->sections_elements_ == NULL)
3153 this->sections_elements_ = new Sections_elements;
3154}
3155
3156// Finish a SECTIONS clause.
3157
3158void
3159Script_sections::finish_sections()
3160{
3161 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
3162 this->in_sections_clause_ = false;
3163}
3164
3165// Add a symbol to be defined.
3166
3167void
3168Script_sections::add_symbol_assignment(const char* name, size_t length,
3169 Expression* val, bool provide,
3170 bool hidden)
3171{
3172 if (this->output_section_ != NULL)
3173 this->output_section_->add_symbol_assignment(name, length, val,
3174 provide, hidden);
3175 else
3176 {
3177 Sections_element* p = new Sections_element_assignment(name, length,
3178 val, provide,
3179 hidden);
3180 this->sections_elements_->push_back(p);
3181 }
3182}
3183
a445fddf
ILT
3184// Add an assignment to the special dot symbol.
3185
3186void
3187Script_sections::add_dot_assignment(Expression* val)
3188{
3189 if (this->output_section_ != NULL)
3190 this->output_section_->add_dot_assignment(val);
3191 else
3192 {
12edd763
ILT
3193 // The GNU linker permits assignments to . to appears outside of
3194 // a SECTIONS clause, and treats it as appearing inside, so
3195 // sections_elements_ may be NULL here.
3196 if (this->sections_elements_ == NULL)
3197 {
3198 this->sections_elements_ = new Sections_elements;
3199 this->saw_sections_clause_ = true;
3200 }
3201
a445fddf
ILT
3202 Sections_element* p = new Sections_element_dot_assignment(val);
3203 this->sections_elements_->push_back(p);
3204 }
3205}
3206
494e05f4
ILT
3207// Add an assertion.
3208
3209void
3210Script_sections::add_assertion(Expression* check, const char* message,
3211 size_t messagelen)
3212{
3213 if (this->output_section_ != NULL)
3214 this->output_section_->add_assertion(check, message, messagelen);
3215 else
3216 {
3217 Sections_element* p = new Sections_element_assertion(check, message,
3218 messagelen);
3219 this->sections_elements_->push_back(p);
3220 }
3221}
3222
3223// Start processing entries for an output section.
3224
3225void
3226Script_sections::start_output_section(
3227 const char* name,
3228 size_t namelen,
ca09d69a 3229 const Parser_output_section_header* header)
494e05f4
ILT
3230{
3231 Output_section_definition* posd = new Output_section_definition(name,
3232 namelen,
3233 header);
3234 this->sections_elements_->push_back(posd);
3235 gold_assert(this->output_section_ == NULL);
3236 this->output_section_ = posd;
3237}
3238
3239// Stop processing entries for an output section.
3240
3241void
3242Script_sections::finish_output_section(
3243 const Parser_output_section_trailer* trailer)
3244{
3245 gold_assert(this->output_section_ != NULL);
3246 this->output_section_->finish(trailer);
3247 this->output_section_ = NULL;
3248}
3249
3250// Add a data item to the current output section.
3251
3252void
3253Script_sections::add_data(int size, bool is_signed, Expression* val)
3254{
3255 gold_assert(this->output_section_ != NULL);
3256 this->output_section_->add_data(size, is_signed, val);
3257}
3258
3259// Add a fill value setting to the current output section.
3260
3261void
3262Script_sections::add_fill(Expression* val)
3263{
3264 gold_assert(this->output_section_ != NULL);
3265 this->output_section_->add_fill(val);
3266}
3267
3268// Add an input section specification to the current output section.
3269
3270void
3271Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3272{
3273 gold_assert(this->output_section_ != NULL);
3274 this->output_section_->add_input_section(spec, keep);
3275}
3276
2d924fd9
ILT
3277// This is called when we see DATA_SEGMENT_ALIGN. It means that any
3278// subsequent output sections may be relro.
3279
3280void
3281Script_sections::data_segment_align()
3282{
0d371ad3 3283 if (this->saw_data_segment_align_)
2d924fd9 3284 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
0d371ad3
ILT
3285 gold_assert(!this->sections_elements_->empty());
3286 Sections_elements::iterator p = this->sections_elements_->end();
3287 --p;
3288 this->data_segment_align_start_ = p;
3289 this->saw_data_segment_align_ = true;
2d924fd9
ILT
3290}
3291
3292// This is called when we see DATA_SEGMENT_RELRO_END. It means that
3293// any output sections seen since DATA_SEGMENT_ALIGN are relro.
3294
3295void
3296Script_sections::data_segment_relro_end()
3297{
3298 if (this->saw_relro_end_)
3299 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3300 "in a linker script"));
3301 this->saw_relro_end_ = true;
3302
0d371ad3 3303 if (!this->saw_data_segment_align_)
2d924fd9
ILT
3304 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3305 else
3306 {
0d371ad3
ILT
3307 Sections_elements::iterator p = this->data_segment_align_start_;
3308 for (++p; p != this->sections_elements_->end(); ++p)
3309 (*p)->set_is_relro();
2d924fd9
ILT
3310 }
3311}
3312
919ed24c
ILT
3313// Create any required sections.
3314
3315void
3316Script_sections::create_sections(Layout* layout)
3317{
3318 if (!this->saw_sections_clause_)
3319 return;
3320 for (Sections_elements::iterator p = this->sections_elements_->begin();
3321 p != this->sections_elements_->end();
3322 ++p)
3323 (*p)->create_sections(layout);
3324}
3325
a445fddf
ILT
3326// Add any symbols we are defining to the symbol table.
3327
3328void
3329Script_sections::add_symbols_to_table(Symbol_table* symtab)
3330{
3331 if (!this->saw_sections_clause_)
3332 return;
3333 for (Sections_elements::iterator p = this->sections_elements_->begin();
3334 p != this->sections_elements_->end();
3335 ++p)
3336 (*p)->add_symbols_to_table(symtab);
3337}
3338
3339// Finalize symbols and check assertions.
3340
3341void
3342Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3343{
3344 if (!this->saw_sections_clause_)
3345 return;
a445fddf
ILT
3346 uint64_t dot_value = 0;
3347 for (Sections_elements::iterator p = this->sections_elements_->begin();
3348 p != this->sections_elements_->end();
3349 ++p)
77e65537 3350 (*p)->finalize_symbols(symtab, layout, &dot_value);
a445fddf
ILT
3351}
3352
3353// Return the name of the output section to use for an input file name
3354// and section name.
3355
3356const char*
1e5d2fb1
DK
3357Script_sections::output_section_name(
3358 const char* file_name,
3359 const char* section_name,
3360 Output_section*** output_section_slot,
ca09d69a 3361 Script_sections::Section_type* psection_type)
a445fddf
ILT
3362{
3363 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3364 p != this->sections_elements_->end();
3365 ++p)
3366 {
3367 const char* ret = (*p)->output_section_name(file_name, section_name,
1e5d2fb1
DK
3368 output_section_slot,
3369 psection_type);
a445fddf
ILT
3370
3371 if (ret != NULL)
3372 {
3373 // The special name /DISCARD/ means that the input section
3374 // should be discarded.
3375 if (strcmp(ret, "/DISCARD/") == 0)
3376 {
3377 *output_section_slot = NULL;
1e5d2fb1 3378 *psection_type = Script_sections::ST_NONE;
a445fddf
ILT
3379 return NULL;
3380 }
3381 return ret;
3382 }
3383 }
3384
3385 // If we couldn't find a mapping for the name, the output section
3386 // gets the name of the input section.
3387
3388 *output_section_slot = NULL;
1e5d2fb1 3389 *psection_type = Script_sections::ST_NONE;
a445fddf
ILT
3390
3391 return section_name;
3392}
3393
3394// Place a marker for an orphan output section into the SECTIONS
3395// clause.
3396
3397void
3398Script_sections::place_orphan(Output_section* os)
3399{
0d371ad3
ILT
3400 Orphan_section_placement* osp = this->orphan_section_placement_;
3401 if (osp == NULL)
a445fddf 3402 {
0d371ad3
ILT
3403 // Initialize the Orphan_section_placement structure.
3404 osp = new Orphan_section_placement();
3405 for (Sections_elements::iterator p = this->sections_elements_->begin();
3406 p != this->sections_elements_->end();
3407 ++p)
3408 (*p)->orphan_section_init(osp, p);
3409 gold_assert(!this->sections_elements_->empty());
3410 Sections_elements::iterator last = this->sections_elements_->end();
3411 --last;
3412 osp->last_init(last);
3413 this->orphan_section_placement_ = osp;
a445fddf
ILT
3414 }
3415
0d371ad3 3416 Orphan_output_section* orphan = new Orphan_output_section(os);
2d924fd9 3417
0d371ad3
ILT
3418 // Look for where to put ORPHAN.
3419 Sections_elements::iterator* where;
3420 if (osp->find_place(os, &where))
3421 {
3422 if ((**where)->is_relro())
3423 os->set_is_relro();
3424 else
3425 os->clear_is_relro();
3426
3427 // We want to insert ORPHAN after *WHERE, and then update *WHERE
3428 // so that the next one goes after this one.
3429 Sections_elements::iterator p = *where;
3430 gold_assert(p != this->sections_elements_->end());
3431 ++p;
3432 *where = this->sections_elements_->insert(p, orphan);
3433 }
2d924fd9 3434 else
0d371ad3
ILT
3435 {
3436 os->clear_is_relro();
3437 // We don't have a place to put this orphan section. Put it,
3438 // and all other sections like it, at the end, but before the
3439 // sections which always come at the end.
3440 Sections_elements::iterator last = osp->last_place();
3441 *where = this->sections_elements_->insert(last, orphan);
3442 }
a445fddf
ILT
3443}
3444
3445// Set the addresses of all the output sections. Walk through all the
3446// elements, tracking the dot symbol. Apply assignments which set
3447// absolute symbol values, in case they are used when setting dot.
3448// Fill in data statement values. As we find output sections, set the
3449// address, set the address of all associated input sections, and
3450// update dot. Return the segment which should hold the file header
3451// and segment headers, if any.
3452
3453Output_segment*
3454Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3455{
3456 gold_assert(this->saw_sections_clause_);
7f8cd844 3457
3802b2dd
ILT
3458 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
3459 // for our representation.
3460 for (Sections_elements::iterator p = this->sections_elements_->begin();
3461 p != this->sections_elements_->end();
3462 ++p)
3463 {
3464 Output_section_definition* posd;
3465 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3466 if (failed_constraint != CONSTRAINT_NONE)
3467 {
3468 Sections_elements::iterator q;
3469 for (q = this->sections_elements_->begin();
3470 q != this->sections_elements_->end();
3471 ++q)
3472 {
3473 if (q != p)
3474 {
3475 if ((*q)->alternate_constraint(posd, failed_constraint))
3476 break;
3477 }
3478 }
3479
3480 if (q == this->sections_elements_->end())
3481 gold_error(_("no matching section constraint"));
3482 }
3483 }
3484
2d924fd9
ILT
3485 // Force the alignment of the first TLS section to be the maximum
3486 // alignment of all TLS sections.
3487 Output_section* first_tls = NULL;
3488 uint64_t tls_align = 0;
3489 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3490 p != this->sections_elements_->end();
3491 ++p)
3492 {
ca09d69a 3493 Output_section* os = (*p)->get_output_section();
2d924fd9
ILT
3494 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3495 {
3496 if (first_tls == NULL)
3497 first_tls = os;
3498 if (os->addralign() > tls_align)
3499 tls_align = os->addralign();
3500 }
3501 }
3502 if (first_tls != NULL)
3503 first_tls->set_addralign(tls_align);
3504
77e65537 3505 // For a relocatable link, we implicitly set dot to zero.
a445fddf 3506 uint64_t dot_value = 0;
f6973bdc 3507 uint64_t dot_alignment = 0;
fd247bfe 3508 uint64_t load_address = 0;
3c12dcdb
DK
3509
3510 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3511 // to set section addresses. If the script has any SEGMENT_START
3512 // expression, we do not set the section addresses.
3513 bool use_tsection_options =
3514 (!this->saw_segment_start_expression_
3515 && (parameters->options().user_set_Ttext()
3516 || parameters->options().user_set_Tdata()
3517 || parameters->options().user_set_Tbss()));
3518
a445fddf
ILT
3519 for (Sections_elements::iterator p = this->sections_elements_->begin();
3520 p != this->sections_elements_->end();
3521 ++p)
3c12dcdb
DK
3522 {
3523 Output_section* os = (*p)->get_output_section();
3524
3525 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
3526 // the special sections by names and doing dot assignments.
3527 if (use_tsection_options
3528 && os != NULL
3529 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3530 {
3531 uint64_t new_dot_value = dot_value;
3532
3533 if (parameters->options().user_set_Ttext()
3534 && strcmp(os->name(), ".text") == 0)
3535 new_dot_value = parameters->options().Ttext();
3536 else if (parameters->options().user_set_Tdata()
3537 && strcmp(os->name(), ".data") == 0)
3538 new_dot_value = parameters->options().Tdata();
3539 else if (parameters->options().user_set_Tbss()
3540 && strcmp(os->name(), ".bss") == 0)
3541 new_dot_value = parameters->options().Tbss();
3542
3543 // Update dot and load address if necessary.
3544 if (new_dot_value < dot_value)
3545 gold_error(_("dot may not move backward"));
3546 else if (new_dot_value != dot_value)
3547 {
3548 dot_value = new_dot_value;
3549 load_address = new_dot_value;
3550 }
3551 }
3552
f6973bdc
ILT
3553 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3554 &load_address);
3c12dcdb 3555 }
a445fddf 3556
1c4f3631
ILT
3557 if (this->phdrs_elements_ != NULL)
3558 {
3559 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3560 p != this->phdrs_elements_->end();
3561 ++p)
3562 (*p)->eval_load_address(symtab, layout);
3563 }
3564
f6973bdc 3565 return this->create_segments(layout, dot_alignment);
a445fddf
ILT
3566}
3567
3568// Sort the sections in order to put them into segments.
3569
3570class Sort_output_sections
3571{
3572 public:
eb373049
ILT
3573 Sort_output_sections(const Script_sections::Sections_elements* elements)
3574 : elements_(elements)
3575 { }
3576
a445fddf
ILT
3577 bool
3578 operator()(const Output_section* os1, const Output_section* os2) const;
eb373049
ILT
3579
3580 private:
fd7a005d
ILT
3581 int
3582 script_compare(const Output_section* os1, const Output_section* os2) const;
eb373049
ILT
3583
3584 private:
3585 const Script_sections::Sections_elements* elements_;
a445fddf
ILT
3586};
3587
3588bool
3589Sort_output_sections::operator()(const Output_section* os1,
3590 const Output_section* os2) const
3591{
3592 // Sort first by the load address.
3593 uint64_t lma1 = (os1->has_load_address()
3594 ? os1->load_address()
3595 : os1->address());
3596 uint64_t lma2 = (os2->has_load_address()
3597 ? os2->load_address()
3598 : os2->address());
3599 if (lma1 != lma2)
3600 return lma1 < lma2;
3601
3602 // Then sort by the virtual address.
3603 if (os1->address() != os2->address())
3604 return os1->address() < os2->address();
3605
fd7a005d
ILT
3606 // If the linker script says which of these sections is first, go
3607 // with what it says.
3608 int i = this->script_compare(os1, os2);
3609 if (i != 0)
3610 return i < 0;
3611
0aa45fac
CC
3612 // Sort PROGBITS before NOBITS.
3613 bool nobits1 = os1->type() == elfcpp::SHT_NOBITS;
3614 bool nobits2 = os2->type() == elfcpp::SHT_NOBITS;
3615 if (nobits1 != nobits2)
3616 return nobits2;
3617
3618 // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
3619 // beginning.
a445fddf
ILT
3620 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3621 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3622 if (tls1 != tls2)
0aa45fac 3623 return nobits1 ? tls1 : tls2;
a445fddf 3624
1e5d2fb1
DK
3625 // Sort non-NOLOAD before NOLOAD.
3626 if (os1->is_noload() && !os2->is_noload())
3627 return true;
3628 if (!os1->is_noload() && os2->is_noload())
3629 return true;
fd7a005d
ILT
3630
3631 // The sections seem practically identical. Sort by name to get a
3632 // stable sort.
3633 return os1->name() < os2->name();
eb373049
ILT
3634}
3635
fd7a005d
ILT
3636// Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
3637// if either OS1 or OS2 is not mentioned. This ensures that we keep
3638// empty sections in the order in which they appear in a linker
3639// script.
eb373049 3640
fd7a005d
ILT
3641int
3642Sort_output_sections::script_compare(const Output_section* os1,
3643 const Output_section* os2) const
eb373049
ILT
3644{
3645 if (this->elements_ == NULL)
fd7a005d 3646 return 0;
eb373049 3647
fd7a005d
ILT
3648 bool found_os1 = false;
3649 bool found_os2 = false;
eb373049
ILT
3650 for (Script_sections::Sections_elements::const_iterator
3651 p = this->elements_->begin();
3652 p != this->elements_->end();
3653 ++p)
3654 {
fd7a005d 3655 if (os2 == (*p)->get_output_section())
eb373049 3656 {
fd7a005d
ILT
3657 if (found_os1)
3658 return -1;
3659 found_os2 = true;
3660 }
3661 else if (os1 == (*p)->get_output_section())
3662 {
3663 if (found_os2)
3664 return 1;
3665 found_os1 = true;
eb373049
ILT
3666 }
3667 }
3668
fd7a005d 3669 return 0;
a445fddf
ILT
3670}
3671
3672// Return whether OS is a BSS section. This is a SHT_NOBITS section.
3673// We treat a section with the SHF_TLS flag set as taking up space
3674// even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3675// space for them in the file.
3676
3677bool
3678Script_sections::is_bss_section(const Output_section* os)
3679{
3680 return (os->type() == elfcpp::SHT_NOBITS
3681 && (os->flags() & elfcpp::SHF_TLS) == 0);
3682}
3683
1c4f3631
ILT
3684// Return the size taken by the file header and the program headers.
3685
3686size_t
3687Script_sections::total_header_size(Layout* layout) const
3688{
3689 size_t segment_count = layout->segment_count();
3690 size_t file_header_size;
3691 size_t segment_headers_size;
8851ecca 3692 if (parameters->target().get_size() == 32)
1c4f3631
ILT
3693 {
3694 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3695 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3696 }
8851ecca 3697 else if (parameters->target().get_size() == 64)
1c4f3631
ILT
3698 {
3699 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3700 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3701 }
3702 else
3703 gold_unreachable();
3704
3705 return file_header_size + segment_headers_size;
3706}
3707
9b547ce6 3708// Return the amount we have to subtract from the LMA to accommodate
1c4f3631
ILT
3709// headers of the given size. The complication is that the file
3710// header have to be at the start of a page, as otherwise it will not
3711// be at the start of the file.
3712
3713uint64_t
3714Script_sections::header_size_adjustment(uint64_t lma,
3715 size_t sizeof_headers) const
3716{
8851ecca 3717 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
1c4f3631
ILT
3718 uint64_t hdr_lma = lma - sizeof_headers;
3719 hdr_lma &= ~(abi_pagesize - 1);
3720 return lma - hdr_lma;
3721}
3722
a445fddf
ILT
3723// Create the PT_LOAD segments when using a SECTIONS clause. Returns
3724// the segment which should hold the file header and segment headers,
3725// if any.
3726
3727Output_segment*
f6973bdc 3728Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
a445fddf
ILT
3729{
3730 gold_assert(this->saw_sections_clause_);
3731
8851ecca 3732 if (parameters->options().relocatable())
a445fddf
ILT
3733 return NULL;
3734
1c4f3631 3735 if (this->saw_phdrs_clause())
f6973bdc 3736 return create_segments_from_phdrs_clause(layout, dot_alignment);
1c4f3631 3737
a445fddf
ILT
3738 Layout::Section_list sections;
3739 layout->get_allocated_sections(&sections);
3740
3741 // Sort the sections by address.
eb373049
ILT
3742 std::stable_sort(sections.begin(), sections.end(),
3743 Sort_output_sections(this->sections_elements_));
a445fddf
ILT
3744
3745 this->create_note_and_tls_segments(layout, &sections);
3746
3747 // Walk through the sections adding them to PT_LOAD segments.
8851ecca 3748 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
a445fddf
ILT
3749 Output_segment* first_seg = NULL;
3750 Output_segment* current_seg = NULL;
3751 bool is_current_seg_readonly = true;
3752 Layout::Section_list::iterator plast = sections.end();
3753 uint64_t last_vma = 0;
3754 uint64_t last_lma = 0;
3755 uint64_t last_size = 0;
3756 for (Layout::Section_list::iterator p = sections.begin();
3757 p != sections.end();
3758 ++p)
3759 {
3760 const uint64_t vma = (*p)->address();
3761 const uint64_t lma = ((*p)->has_load_address()
3762 ? (*p)->load_address()
3763 : vma);
3764 const uint64_t size = (*p)->current_data_size();
3765
3766 bool need_new_segment;
3767 if (current_seg == NULL)
3768 need_new_segment = true;
3769 else if (lma - vma != last_lma - last_vma)
3770 {
3771 // This section has a different LMA relationship than the
3772 // last one; we need a new segment.
3773 need_new_segment = true;
3774 }
3775 else if (align_address(last_lma + last_size, abi_pagesize)
3776 < align_address(lma, abi_pagesize))
3777 {
3778 // Putting this section in the segment would require
3779 // skipping a page.
3780 need_new_segment = true;
3781 }
3782 else if (is_bss_section(*plast) && !is_bss_section(*p))
3783 {
3784 // A non-BSS section can not follow a BSS section in the
3785 // same segment.
3786 need_new_segment = true;
3787 }
3788 else if (is_current_seg_readonly
af6156ef
ILT
3789 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3790 && !parameters->options().omagic())
a445fddf
ILT
3791 {
3792 // Don't put a writable section in the same segment as a
3793 // non-writable section.
3794 need_new_segment = true;
3795 }
3796 else
3797 {
3798 // Otherwise, reuse the existing segment.
3799 need_new_segment = false;
3800 }
3801
3802 elfcpp::Elf_Word seg_flags =
3803 Layout::section_flags_to_segment((*p)->flags());
3804
3805 if (need_new_segment)
3806 {
3807 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3808 seg_flags);
3809 current_seg->set_addresses(vma, lma);
f6973bdc 3810 current_seg->set_minimum_p_align(dot_alignment);
a445fddf
ILT
3811 if (first_seg == NULL)
3812 first_seg = current_seg;
3813 is_current_seg_readonly = true;
3814 }
3815
22f0da72 3816 current_seg->add_output_section_to_load(layout, *p, seg_flags);
a445fddf
ILT
3817
3818 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3819 is_current_seg_readonly = false;
3820
3821 plast = p;
3822 last_vma = vma;
3823 last_lma = lma;
3824 last_size = size;
3825 }
3826
3827 // An ELF program should work even if the program headers are not in
3828 // a PT_LOAD segment. However, it appears that the Linux kernel
3829 // does not set the AT_PHDR auxiliary entry in that case. It sets
3830 // the load address to p_vaddr - p_offset of the first PT_LOAD
3831 // segment. It then sets AT_PHDR to the load address plus the
3832 // offset to the program headers, e_phoff in the file header. This
3833 // fails when the program headers appear in the file before the
3834 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3835 // segment to hold the file header and the program headers. This is
3836 // effectively what the GNU linker does, and it is slightly more
3837 // efficient in any case. We try to use the first PT_LOAD segment
3838 // if we can, otherwise we make a new one.
3839
919ed24c
ILT
3840 if (first_seg == NULL)
3841 return NULL;
3842
3ee173de
ILT
3843 // -n or -N mean that the program is not demand paged and there is
3844 // no need to put the program headers in a PT_LOAD segment.
3845 if (parameters->options().nmagic() || parameters->options().omagic())
3846 return NULL;
3847
1c4f3631 3848 size_t sizeof_headers = this->total_header_size(layout);
3802b2dd 3849
919ed24c
ILT
3850 uint64_t vma = first_seg->vaddr();
3851 uint64_t lma = first_seg->paddr();
3852
3853 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3854
e6188289
ILT
3855 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3856 {
3857 first_seg->set_addresses(vma - subtract, lma - subtract);
3858 return first_seg;
3859 }
3860
919ed24c
ILT
3861 // If there is no room to squeeze in the headers, then punt. The
3862 // resulting executable probably won't run on GNU/Linux, but we
3863 // trust that the user knows what they are doing.
3864 if (lma < subtract || vma < subtract)
3865 return NULL;
3866
ea5cae92
NC
3867 // If memory regions have been specified and the address range
3868 // we are about to use is not contained within any region then
3869 // issue a warning message about the segment we are going to
3870 // create. It will be outside of any region and so possibly
3871 // using non-existent or protected memory. We test LMA rather
3872 // than VMA since we assume that the headers will never be
3873 // relocated.
3874 if (this->memory_regions_ != NULL
3875 && !this->block_in_region (NULL, layout, lma - subtract, subtract))
3876 gold_warning(_("creating a segment to contain the file and program"
3877 " headers outside of any MEMORY region"));
3878
a445fddf
ILT
3879 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3880 elfcpp::PF_R);
919ed24c 3881 load_seg->set_addresses(vma - subtract, lma - subtract);
a445fddf
ILT
3882
3883 return load_seg;
3884}
3885
3886// Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3887// segment if there are any SHT_TLS sections.
3888
3889void
3890Script_sections::create_note_and_tls_segments(
3891 Layout* layout,
3892 const Layout::Section_list* sections)
3893{
1c4f3631
ILT
3894 gold_assert(!this->saw_phdrs_clause());
3895
a445fddf
ILT
3896 bool saw_tls = false;
3897 for (Layout::Section_list::const_iterator p = sections->begin();
3898 p != sections->end();
3899 ++p)
3900 {
3901 if ((*p)->type() == elfcpp::SHT_NOTE)
3902 {
3903 elfcpp::Elf_Word seg_flags =
3904 Layout::section_flags_to_segment((*p)->flags());
3905 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3906 seg_flags);
22f0da72 3907 oseg->add_output_section_to_nonload(*p, seg_flags);
a445fddf
ILT
3908
3909 // Incorporate any subsequent SHT_NOTE sections, in the
3910 // hopes that the script is sensible.
3911 Layout::Section_list::const_iterator pnext = p + 1;
3912 while (pnext != sections->end()
3913 && (*pnext)->type() == elfcpp::SHT_NOTE)
3914 {
3915 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
22f0da72 3916 oseg->add_output_section_to_nonload(*pnext, seg_flags);
a445fddf
ILT
3917 p = pnext;
3918 ++pnext;
3919 }
3920 }
3921
3922 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3923 {
3924 if (saw_tls)
3925 gold_error(_("TLS sections are not adjacent"));
3926
3927 elfcpp::Elf_Word seg_flags =
3928 Layout::section_flags_to_segment((*p)->flags());
3929 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3930 seg_flags);
22f0da72 3931 oseg->add_output_section_to_nonload(*p, seg_flags);
a445fddf
ILT
3932
3933 Layout::Section_list::const_iterator pnext = p + 1;
3934 while (pnext != sections->end()
3935 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3936 {
3937 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
22f0da72 3938 oseg->add_output_section_to_nonload(*pnext, seg_flags);
a445fddf
ILT
3939 p = pnext;
3940 ++pnext;
3941 }
3942
3943 saw_tls = true;
3944 }
10b4f102
ILT
3945
3946 // If we are making a shared library, and we see a section named
e1f74f98
ILT
3947 // .interp then put the .interp section in a PT_INTERP segment.
3948 // This is for GNU ld compatibility.
3949 if (strcmp((*p)->name(), ".interp") == 0)
10b4f102
ILT
3950 {
3951 elfcpp::Elf_Word seg_flags =
3952 Layout::section_flags_to_segment((*p)->flags());
3953 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_INTERP,
3954 seg_flags);
3955 oseg->add_output_section_to_nonload(*p, seg_flags);
3956 }
a445fddf
ILT
3957 }
3958}
3959
1c4f3631
ILT
3960// Add a program header. The PHDRS clause is syntactically distinct
3961// from the SECTIONS clause, but we implement it with the SECTIONS
55458500 3962// support because PHDRS is useless if there is no SECTIONS clause.
1c4f3631
ILT
3963
3964void
3965Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3966 bool includes_filehdr, bool includes_phdrs,
3967 bool is_flags_valid, unsigned int flags,
3968 Expression* load_address)
3969{
3970 if (this->phdrs_elements_ == NULL)
3971 this->phdrs_elements_ = new Phdrs_elements();
3972 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3973 includes_filehdr,
3974 includes_phdrs,
3975 is_flags_valid, flags,
3976 load_address));
3977}
3978
3802b2dd
ILT
3979// Return the number of segments we expect to create based on the
3980// SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3981
3982size_t
3983Script_sections::expected_segment_count(const Layout* layout) const
3984{
1c4f3631
ILT
3985 if (this->saw_phdrs_clause())
3986 return this->phdrs_elements_->size();
3987
3802b2dd
ILT
3988 Layout::Section_list sections;
3989 layout->get_allocated_sections(&sections);
3990
3991 // We assume that we will need two PT_LOAD segments.
3992 size_t ret = 2;
3993
3994 bool saw_note = false;
3995 bool saw_tls = false;
3996 for (Layout::Section_list::const_iterator p = sections.begin();
3997 p != sections.end();
3998 ++p)
3999 {
4000 if ((*p)->type() == elfcpp::SHT_NOTE)
4001 {
4002 // Assume that all note sections will fit into a single
4003 // PT_NOTE segment.
4004 if (!saw_note)
4005 {
4006 ++ret;
4007 saw_note = true;
4008 }
4009 }
4010 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
4011 {
4012 // There can only be one PT_TLS segment.
4013 if (!saw_tls)
4014 {
4015 ++ret;
4016 saw_tls = true;
4017 }
4018 }
4019 }
4020
4021 return ret;
4022}
4023
1c4f3631
ILT
4024// Create the segments from a PHDRS clause. Return the segment which
4025// should hold the file header and program headers, if any.
4026
4027Output_segment*
f6973bdc
ILT
4028Script_sections::create_segments_from_phdrs_clause(Layout* layout,
4029 uint64_t dot_alignment)
1c4f3631
ILT
4030{
4031 this->attach_sections_using_phdrs_clause(layout);
f6973bdc 4032 return this->set_phdrs_clause_addresses(layout, dot_alignment);
1c4f3631
ILT
4033}
4034
4035// Create the segments from the PHDRS clause, and put the output
4036// sections in them.
4037
4038void
4039Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
4040{
4041 typedef std::map<std::string, Output_segment*> Name_to_segment;
4042 Name_to_segment name_to_segment;
4043 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4044 p != this->phdrs_elements_->end();
4045 ++p)
4046 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
4047
4048 // Walk through the output sections and attach them to segments.
4049 // Output sections in the script which do not list segments are
4050 // attached to the same set of segments as the immediately preceding
4051 // output section.
20e6d0d6 4052
1c4f3631 4053 String_list* phdr_names = NULL;
20e6d0d6 4054 bool load_segments_only = false;
1c4f3631
ILT
4055 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4056 p != this->sections_elements_->end();
4057 ++p)
4058 {
aecf301f 4059 bool is_orphan;
20e6d0d6 4060 String_list* old_phdr_names = phdr_names;
aecf301f 4061 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &is_orphan);
1c4f3631
ILT
4062 if (os == NULL)
4063 continue;
4064
aecf301f
ILT
4065 elfcpp::Elf_Word seg_flags =
4066 Layout::section_flags_to_segment(os->flags());
4067
1c4f3631
ILT
4068 if (phdr_names == NULL)
4069 {
aecf301f
ILT
4070 // Don't worry about empty orphan sections.
4071 if (is_orphan && os->current_data_size() > 0)
4072 gold_error(_("allocated section %s not in any segment"),
4073 os->name());
4074
4075 // To avoid later crashes drop this section into the first
4076 // PT_LOAD segment.
4077 for (Phdrs_elements::const_iterator ppe =
4078 this->phdrs_elements_->begin();
4079 ppe != this->phdrs_elements_->end();
4080 ++ppe)
4081 {
4082 Output_segment* oseg = (*ppe)->segment();
4083 if (oseg->type() == elfcpp::PT_LOAD)
4084 {
4085 oseg->add_output_section_to_load(layout, os, seg_flags);
4086 break;
4087 }
4088 }
4089
1c4f3631
ILT
4090 continue;
4091 }
4092
20e6d0d6
DK
4093 // We see a list of segments names. Disable PT_LOAD segment only
4094 // filtering.
4095 if (old_phdr_names != phdr_names)
4096 load_segments_only = false;
4097
2cefc357
ILT
4098 // If this is an orphan section--one that was not explicitly
4099 // mentioned in the linker script--then it should not inherit
4100 // any segment type other than PT_LOAD. Otherwise, e.g., the
4101 // PT_INTERP segment will pick up following orphan sections,
4102 // which does not make sense. If this is not an orphan section,
4103 // we trust the linker script.
aecf301f 4104 if (is_orphan)
2cefc357 4105 {
20e6d0d6
DK
4106 // Enable PT_LOAD segments only filtering until we see another
4107 // list of segment names.
4108 load_segments_only = true;
2cefc357
ILT
4109 }
4110
1c4f3631
ILT
4111 bool in_load_segment = false;
4112 for (String_list::const_iterator q = phdr_names->begin();
4113 q != phdr_names->end();
4114 ++q)
4115 {
4116 Name_to_segment::const_iterator r = name_to_segment.find(*q);
4117 if (r == name_to_segment.end())
4118 gold_error(_("no segment %s"), q->c_str());
4119 else
4120 {
20e6d0d6
DK
4121 if (load_segments_only
4122 && r->second->type() != elfcpp::PT_LOAD)
4123 continue;
4124
22f0da72
ILT
4125 if (r->second->type() != elfcpp::PT_LOAD)
4126 r->second->add_output_section_to_nonload(os, seg_flags);
4127 else
1c4f3631 4128 {
22f0da72 4129 r->second->add_output_section_to_load(layout, os, seg_flags);
1c4f3631
ILT
4130 if (in_load_segment)
4131 gold_error(_("section in two PT_LOAD segments"));
4132 in_load_segment = true;
4133 }
4134 }
4135 }
4136
4137 if (!in_load_segment)
4138 gold_error(_("allocated section not in any PT_LOAD segment"));
4139 }
4140}
4141
4142// Set the addresses for segments created from a PHDRS clause. Return
4143// the segment which should hold the file header and program headers,
4144// if any.
4145
4146Output_segment*
f6973bdc
ILT
4147Script_sections::set_phdrs_clause_addresses(Layout* layout,
4148 uint64_t dot_alignment)
1c4f3631
ILT
4149{
4150 Output_segment* load_seg = NULL;
4151 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4152 p != this->phdrs_elements_->end();
4153 ++p)
4154 {
4155 // Note that we have to set the flags after adding the output
4156 // sections to the segment, as adding an output segment can
4157 // change the flags.
4158 (*p)->set_flags_if_valid();
4159
4160 Output_segment* oseg = (*p)->segment();
4161
4162 if (oseg->type() != elfcpp::PT_LOAD)
4163 {
4164 // The addresses of non-PT_LOAD segments are set from the
4165 // PT_LOAD segments.
4166 if ((*p)->has_load_address())
4167 gold_error(_("may only specify load address for PT_LOAD segment"));
4168 continue;
4169 }
4170
f6973bdc
ILT
4171 oseg->set_minimum_p_align(dot_alignment);
4172
1c4f3631
ILT
4173 // The output sections should have addresses from the SECTIONS
4174 // clause. The addresses don't have to be in order, so find the
4175 // one with the lowest load address. Use that to set the
4176 // address of the segment.
4177
4178 Output_section* osec = oseg->section_with_lowest_load_address();
4179 if (osec == NULL)
4180 {
4181 oseg->set_addresses(0, 0);
4182 continue;
4183 }
4184
4185 uint64_t vma = osec->address();
4186 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
4187
4188 // Override the load address of the section with the load
4189 // address specified for the segment.
4190 if ((*p)->has_load_address())
4191 {
4192 if (osec->has_load_address())
4193 gold_warning(_("PHDRS load address overrides "
4194 "section %s load address"),
4195 osec->name());
4196
4197 lma = (*p)->load_address();
4198 }
4199
4200 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
4201 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
4202 {
4203 // We could support this if we wanted to.
4204 gold_error(_("using only one of FILEHDR and PHDRS is "
4205 "not currently supported"));
4206 }
4207 if (headers)
4208 {
4209 size_t sizeof_headers = this->total_header_size(layout);
4210 uint64_t subtract = this->header_size_adjustment(lma,
4211 sizeof_headers);
4212 if (lma >= subtract && vma >= subtract)
4213 {
4214 lma -= subtract;
4215 vma -= subtract;
4216 }
4217 else
4218 {
4219 gold_error(_("sections loaded on first page without room "
4220 "for file and program headers "
4221 "are not supported"));
4222 }
4223
4224 if (load_seg != NULL)
4225 gold_error(_("using FILEHDR and PHDRS on more than one "
4226 "PT_LOAD segment is not currently supported"));
4227 load_seg = oseg;
4228 }
4229
4230 oseg->set_addresses(vma, lma);
4231 }
4232
4233 return load_seg;
4234}
4235
4236// Add the file header and segment headers to non-load segments
4237// specified in the PHDRS clause.
4238
4239void
4240Script_sections::put_headers_in_phdrs(Output_data* file_header,
4241 Output_data* segment_headers)
4242{
4243 gold_assert(this->saw_phdrs_clause());
4244 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
4245 p != this->phdrs_elements_->end();
4246 ++p)
4247 {
4248 if ((*p)->type() != elfcpp::PT_LOAD)
4249 {
4250 if ((*p)->includes_phdrs())
4251 (*p)->segment()->add_initial_output_data(segment_headers);
4252 if ((*p)->includes_filehdr())
4253 (*p)->segment()->add_initial_output_data(file_header);
4254 }
4255 }
4256}
4257
8f2eb564
ILT
4258// Look for an output section by name and return the address, the load
4259// address, the alignment, and the size. This is used when an
4260// expression refers to an output section which was not actually
4261// created. This returns true if the section was found, false
4262// otherwise.
4263
4264bool
4265Script_sections::get_output_section_info(const char* name, uint64_t* address,
4266 uint64_t* load_address,
2ea97941 4267 uint64_t* addralign,
8f2eb564
ILT
4268 uint64_t* size) const
4269{
4270 if (!this->saw_sections_clause_)
4271 return false;
4272 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4273 p != this->sections_elements_->end();
4274 ++p)
2ea97941 4275 if ((*p)->get_output_section_info(name, address, load_address, addralign,
8f2eb564
ILT
4276 size))
4277 return true;
4278 return false;
4279}
4280
20e6d0d6
DK
4281// Release all Output_segments. This remove all pointers to all
4282// Output_segments.
4283
4284void
4285Script_sections::release_segments()
4286{
4287 if (this->saw_phdrs_clause())
4288 {
4289 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4290 p != this->phdrs_elements_->end();
4291 ++p)
4292 (*p)->release_segment();
4293 }
4294}
4295
494e05f4
ILT
4296// Print the SECTIONS clause to F for debugging.
4297
4298void
4299Script_sections::print(FILE* f) const
4300{
7f8cd844
NC
4301 if (this->phdrs_elements_ != NULL)
4302 {
4303 fprintf(f, "PHDRS {\n");
4304 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4305 p != this->phdrs_elements_->end();
4306 ++p)
4307 (*p)->print(f);
4308 fprintf(f, "}\n");
4309 }
4310
4311 if (this->memory_regions_ != NULL)
4312 {
4313 fprintf(f, "MEMORY {\n");
4314 for (Memory_regions::const_iterator m = this->memory_regions_->begin();
4315 m != this->memory_regions_->end();
4316 ++m)
4317 (*m)->print(f);
4318 fprintf(f, "}\n");
4319 }
4320
494e05f4
ILT
4321 if (!this->saw_sections_clause_)
4322 return;
4323
4324 fprintf(f, "SECTIONS {\n");
4325
4326 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4327 p != this->sections_elements_->end();
4328 ++p)
4329 (*p)->print(f);
4330
4331 fprintf(f, "}\n");
4332}
4333
4334} // End namespace gold.