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