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