* ppc-opc.c (powerpc_opcodes): Enable divdeu, devweu, divde,
[deliverable/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.
20e6d0d6 549 typedef std::list<Output_section::Simple_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:
2ea97941
ILT
1198 Input_section_info(const Output_section::Simple_input_section& input_section)
1199 : input_section_(input_section), section_name_(),
20e6d0d6
DK
1200 size_(0), addralign_(1)
1201 { }
1202
1203 // Return the simple input section.
1204 const Output_section::Simple_input_section&
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.
1250 Output_section::Simple_input_section input_section_;
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 {
2ea97941 1421 uint64_t this_subalign = p->addralign();
a445fddf
ILT
1422 if (this_subalign < subalign)
1423 this_subalign = subalign;
1424
661be1e2 1425 uint64_t address = align_address(dot, this_subalign);
a445fddf 1426
661be1e2 1427 if (address > dot && !fill->empty())
a445fddf
ILT
1428 {
1429 section_size_type length =
661be1e2 1430 convert_to_section_size_type(address - dot);
a445fddf
ILT
1431 std::string this_fill = this->get_fill_string(fill, length);
1432 Output_section_data* posd = new Output_data_const(this_fill, 0);
1433 output_section->add_output_section_data(posd);
20e6d0d6 1434 layout->new_output_section_data_from_script(posd);
a445fddf
ILT
1435 }
1436
8923b24c
DK
1437 output_section->add_simple_input_section(p->input_section(),
1438 p->size(),
1439 this_subalign);
a445fddf 1440
2ea97941 1441 dot = address + p->size();
a445fddf
ILT
1442 }
1443 }
1444
661be1e2
ILT
1445 // An SHF_TLS/SHT_NOBITS section does not take up any
1446 // address space.
1447 if (output_section == NULL
1448 || (output_section->flags() & elfcpp::SHF_TLS) == 0
1449 || output_section->type() != elfcpp::SHT_NOBITS)
1450 *dot_value = dot;
1451
a445fddf 1452 this->final_dot_value_ = *dot_value;
77e65537 1453 this->final_dot_section_ = *dot_section;
a445fddf
ILT
1454}
1455
494e05f4
ILT
1456// Print for debugging.
1457
1458void
1459Output_section_element_input::print(FILE* f) const
1460{
1461 fprintf(f, " ");
1462
1463 if (this->keep_)
1464 fprintf(f, "KEEP(");
1465
1466 if (!this->filename_pattern_.empty())
1467 {
1468 bool need_close_paren = false;
1469 switch (this->filename_sort_)
1470 {
1471 case SORT_WILDCARD_NONE:
1472 break;
1473 case SORT_WILDCARD_BY_NAME:
1474 fprintf(f, "SORT_BY_NAME(");
1475 need_close_paren = true;
1476 break;
1477 default:
1478 gold_unreachable();
1479 }
1480
1481 fprintf(f, "%s", this->filename_pattern_.c_str());
1482
1483 if (need_close_paren)
1484 fprintf(f, ")");
1485 }
1486
1487 if (!this->input_section_patterns_.empty()
1488 || !this->filename_exclusions_.empty())
1489 {
1490 fprintf(f, "(");
1491
1492 bool need_space = false;
1493 if (!this->filename_exclusions_.empty())
1494 {
1495 fprintf(f, "EXCLUDE_FILE(");
1496 bool need_comma = false;
1497 for (Filename_exclusions::const_iterator p =
1498 this->filename_exclusions_.begin();
1499 p != this->filename_exclusions_.end();
1500 ++p)
1501 {
1502 if (need_comma)
1503 fprintf(f, ", ");
a445fddf 1504 fprintf(f, "%s", p->first.c_str());
494e05f4
ILT
1505 need_comma = true;
1506 }
1507 fprintf(f, ")");
1508 need_space = true;
1509 }
1510
1511 for (Input_section_patterns::const_iterator p =
1512 this->input_section_patterns_.begin();
1513 p != this->input_section_patterns_.end();
1514 ++p)
1515 {
1516 if (need_space)
1517 fprintf(f, " ");
1518
1519 int close_parens = 0;
1520 switch (p->sort)
1521 {
1522 case SORT_WILDCARD_NONE:
1523 break;
1524 case SORT_WILDCARD_BY_NAME:
1525 fprintf(f, "SORT_BY_NAME(");
1526 close_parens = 1;
1527 break;
1528 case SORT_WILDCARD_BY_ALIGNMENT:
1529 fprintf(f, "SORT_BY_ALIGNMENT(");
1530 close_parens = 1;
1531 break;
1532 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1533 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1534 close_parens = 2;
1535 break;
1536 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1537 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1538 close_parens = 2;
1539 break;
1540 default:
1541 gold_unreachable();
1542 }
1543
1544 fprintf(f, "%s", p->pattern.c_str());
1545
1546 for (int i = 0; i < close_parens; ++i)
1547 fprintf(f, ")");
1548
1549 need_space = true;
1550 }
1551
1552 fprintf(f, ")");
1553 }
1554
1555 if (this->keep_)
1556 fprintf(f, ")");
1557
1558 fprintf(f, "\n");
1559}
1560
1561// An output section.
1562
1563class Output_section_definition : public Sections_element
1564{
1565 public:
a445fddf
ILT
1566 typedef Output_section_element::Input_section_list Input_section_list;
1567
494e05f4
ILT
1568 Output_section_definition(const char* name, size_t namelen,
1569 const Parser_output_section_header* header);
1570
1571 // Finish the output section with the information in the trailer.
1572 void
1573 finish(const Parser_output_section_trailer* trailer);
1574
1575 // Add a symbol to be defined.
1576 void
1577 add_symbol_assignment(const char* name, size_t length, Expression* value,
1578 bool provide, bool hidden);
a445fddf
ILT
1579
1580 // Add an assignment to the special dot symbol.
1581 void
1582 add_dot_assignment(Expression* value);
1583
494e05f4
ILT
1584 // Add an assertion.
1585 void
1586 add_assertion(Expression* check, const char* message, size_t messagelen);
1587
1588 // Add a data item to the current output section.
1589 void
1590 add_data(int size, bool is_signed, Expression* val);
1591
1592 // Add a setting for the fill value.
1593 void
1594 add_fill(Expression* val);
1595
1596 // Add an input section specification.
1597 void
1598 add_input_section(const Input_section_spec* spec, bool keep);
1599
0d371ad3
ILT
1600 // Return whether the output section is relro.
1601 bool
1602 is_relro() const
1603 { return this->is_relro_; }
1604
2d924fd9
ILT
1605 // Record that the output section is relro.
1606 void
1607 set_is_relro()
1608 { this->is_relro_ = true; }
1609
919ed24c
ILT
1610 // Create any required output sections.
1611 void
1612 create_sections(Layout*);
1613
a445fddf
ILT
1614 // Add any symbols being defined to the symbol table.
1615 void
1616 add_symbols_to_table(Symbol_table* symtab);
1617
1618 // Finalize symbols and check assertions.
1619 void
77e65537 1620 finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
a445fddf
ILT
1621
1622 // Return the output section name to use for an input file name and
1623 // section name.
1624 const char*
1625 output_section_name(const char* file_name, const char* section_name,
1e5d2fb1 1626 Output_section***, Script_sections::Section_type*);
a445fddf 1627
0d371ad3
ILT
1628 // Initialize OSP with an output section.
1629 void
1630 orphan_section_init(Orphan_section_placement* osp,
1631 Script_sections::Elements_iterator p)
1632 { osp->output_section_init(this->name_, this->output_section_, p); }
a445fddf
ILT
1633
1634 // Set the section address.
1635 void
1636 set_section_addresses(Symbol_table* symtab, Layout* layout,
f6973bdc
ILT
1637 uint64_t* dot_value, uint64_t*,
1638 uint64_t* load_address);
a445fddf 1639
3802b2dd
ILT
1640 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1641 // this section is constrained, and the input sections do not match,
1642 // return the constraint, and set *POSD.
1643 Section_constraint
1644 check_constraint(Output_section_definition** posd);
1645
1646 // See if this is the alternate output section for a constrained
1647 // output section. If it is, transfer the Output_section and return
1648 // true. Otherwise return false.
1649 bool
1650 alternate_constraint(Output_section_definition*, Section_constraint);
1651
1c4f3631 1652 // Get the list of segments to use for an allocated section when
2cefc357 1653 // using a PHDRS clause.
1c4f3631 1654 Output_section*
2cefc357 1655 allocate_to_segment(String_list** phdrs_list, bool* orphan);
1c4f3631 1656
8f2eb564
ILT
1657 // Look for an output section by name and return the address, the
1658 // load address, the alignment, and the size. This is used when an
1659 // expression refers to an output section which was not actually
1660 // created. This returns true if the section was found, false
1661 // otherwise.
1662 bool
1663 get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
1664 uint64_t*) const;
1665
2d924fd9
ILT
1666 // Return the associated Output_section if there is one.
1667 Output_section*
1668 get_output_section() const
1669 { return this->output_section_; }
1670
494e05f4
ILT
1671 // Print the contents to the FILE. This is for debugging.
1672 void
1673 print(FILE*) const;
1674
1e5d2fb1
DK
1675 // Return the output section type if specified or Script_sections::ST_NONE.
1676 Script_sections::Section_type
1677 section_type() const;
1678
494e05f4 1679 private:
1e5d2fb1
DK
1680 static const char*
1681 script_section_type_name(Script_section_type);
1682
494e05f4
ILT
1683 typedef std::vector<Output_section_element*> Output_section_elements;
1684
1685 // The output section name.
1686 std::string name_;
1687 // The address. This may be NULL.
1688 Expression* address_;
1689 // The load address. This may be NULL.
1690 Expression* load_address_;
1691 // The alignment. This may be NULL.
1692 Expression* align_;
1693 // The input section alignment. This may be NULL.
1694 Expression* subalign_;
3802b2dd
ILT
1695 // The constraint, if any.
1696 Section_constraint constraint_;
494e05f4
ILT
1697 // The fill value. This may be NULL.
1698 Expression* fill_;
1c4f3631
ILT
1699 // The list of segments this section should go into. This may be
1700 // NULL.
1701 String_list* phdrs_;
494e05f4
ILT
1702 // The list of elements defining the section.
1703 Output_section_elements elements_;
a445fddf
ILT
1704 // The Output_section created for this definition. This will be
1705 // NULL if none was created.
1706 Output_section* output_section_;
8f2eb564
ILT
1707 // The address after it has been evaluated.
1708 uint64_t evaluated_address_;
1709 // The load address after it has been evaluated.
1710 uint64_t evaluated_load_address_;
1711 // The alignment after it has been evaluated.
1712 uint64_t evaluated_addralign_;
2d924fd9
ILT
1713 // The output section is relro.
1714 bool is_relro_;
1e5d2fb1
DK
1715 // The output section type if specified.
1716 enum Script_section_type script_section_type_;
494e05f4
ILT
1717};
1718
1719// Constructor.
1720
1721Output_section_definition::Output_section_definition(
1722 const char* name,
1723 size_t namelen,
1724 const Parser_output_section_header* header)
1725 : name_(name, namelen),
1726 address_(header->address),
1727 load_address_(header->load_address),
1728 align_(header->align),
1729 subalign_(header->subalign),
3802b2dd 1730 constraint_(header->constraint),
494e05f4 1731 fill_(NULL),
1c4f3631 1732 phdrs_(NULL),
a445fddf 1733 elements_(),
2d924fd9
ILT
1734 output_section_(NULL),
1735 evaluated_address_(0),
1736 evaluated_load_address_(0),
1737 evaluated_addralign_(0),
1e5d2fb1
DK
1738 is_relro_(false),
1739 script_section_type_(header->section_type)
494e05f4
ILT
1740{
1741}
1742
1743// Finish an output section.
1744
1745void
1746Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1747{
1748 this->fill_ = trailer->fill;
1c4f3631 1749 this->phdrs_ = trailer->phdrs;
494e05f4
ILT
1750}
1751
1752// Add a symbol to be defined.
1753
1754void
1755Output_section_definition::add_symbol_assignment(const char* name,
1756 size_t length,
1757 Expression* value,
1758 bool provide,
1759 bool hidden)
1760{
1761 Output_section_element* p = new Output_section_element_assignment(name,
1762 length,
1763 value,
1764 provide,
1765 hidden);
1766 this->elements_.push_back(p);
1767}
1768
a445fddf 1769// Add an assignment to the special dot symbol.
494e05f4
ILT
1770
1771void
a445fddf
ILT
1772Output_section_definition::add_dot_assignment(Expression* value)
1773{
1774 Output_section_element* p = new Output_section_element_dot_assignment(value);
1775 this->elements_.push_back(p);
1776}
1777
1778// Add an assertion.
1779
1780void
1781Output_section_definition::add_assertion(Expression* check,
1782 const char* message,
494e05f4
ILT
1783 size_t messagelen)
1784{
1785 Output_section_element* p = new Output_section_element_assertion(check,
1786 message,
1787 messagelen);
1788 this->elements_.push_back(p);
1789}
1790
1791// Add a data item to the current output section.
1792
1793void
1794Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1795{
1796 Output_section_element* p = new Output_section_element_data(size, is_signed,
1797 val);
1798 this->elements_.push_back(p);
1799}
1800
1801// Add a setting for the fill value.
1802
1803void
1804Output_section_definition::add_fill(Expression* val)
1805{
1806 Output_section_element* p = new Output_section_element_fill(val);
1807 this->elements_.push_back(p);
1808}
1809
1810// Add an input section specification.
1811
1812void
1813Output_section_definition::add_input_section(const Input_section_spec* spec,
1814 bool keep)
1815{
1816 Output_section_element* p = new Output_section_element_input(spec, keep);
1817 this->elements_.push_back(p);
1818}
1819
919ed24c
ILT
1820// Create any required output sections. We need an output section if
1821// there is a data statement here.
1822
1823void
1824Output_section_definition::create_sections(Layout* layout)
1825{
1826 if (this->output_section_ != NULL)
1827 return;
1828 for (Output_section_elements::const_iterator p = this->elements_.begin();
1829 p != this->elements_.end();
1830 ++p)
1831 {
1832 if ((*p)->needs_output_section())
1833 {
1834 const char* name = this->name_.c_str();
1e5d2fb1
DK
1835 this->output_section_ =
1836 layout->make_output_section_for_script(name, this->section_type());
919ed24c
ILT
1837 return;
1838 }
1839 }
1840}
1841
a445fddf
ILT
1842// Add any symbols being defined to the symbol table.
1843
1844void
1845Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
1846{
1847 for (Output_section_elements::iterator p = this->elements_.begin();
1848 p != this->elements_.end();
1849 ++p)
1850 (*p)->add_symbols_to_table(symtab);
1851}
1852
1853// Finalize symbols and check assertions.
1854
1855void
1856Output_section_definition::finalize_symbols(Symbol_table* symtab,
1857 const Layout* layout,
a445fddf
ILT
1858 uint64_t* dot_value)
1859{
1860 if (this->output_section_ != NULL)
1861 *dot_value = this->output_section_->address();
1862 else
1863 {
1864 uint64_t address = *dot_value;
1865 if (this->address_ != NULL)
1866 {
77e65537 1867 Output_section* dummy;
919ed24c 1868 address = this->address_->eval_with_dot(symtab, layout, true,
77e65537 1869 *dot_value, NULL,
f6973bdc 1870 &dummy, NULL);
a445fddf
ILT
1871 }
1872 if (this->align_ != NULL)
1873 {
77e65537 1874 Output_section* dummy;
919ed24c 1875 uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
a445fddf 1876 *dot_value,
77e65537 1877 NULL,
f6973bdc 1878 &dummy, NULL);
a445fddf
ILT
1879 address = align_address(address, align);
1880 }
1881 *dot_value = address;
1882 }
a445fddf 1883
77e65537 1884 Output_section* dot_section = this->output_section_;
a445fddf
ILT
1885 for (Output_section_elements::iterator p = this->elements_.begin();
1886 p != this->elements_.end();
1887 ++p)
77e65537 1888 (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
a445fddf
ILT
1889}
1890
1891// Return the output section name to use for an input section name.
1892
1893const char*
1e5d2fb1
DK
1894Output_section_definition::output_section_name(
1895 const char* file_name,
1896 const char* section_name,
1897 Output_section*** slot,
1898 Script_sections::Section_type *psection_type)
a445fddf
ILT
1899{
1900 // Ask each element whether it matches NAME.
1901 for (Output_section_elements::const_iterator p = this->elements_.begin();
1902 p != this->elements_.end();
1903 ++p)
1904 {
1905 if ((*p)->match_name(file_name, section_name))
1906 {
1907 // We found a match for NAME, which means that it should go
1908 // into this output section.
1909 *slot = &this->output_section_;
1e5d2fb1 1910 *psection_type = this->section_type();
a445fddf
ILT
1911 return this->name_.c_str();
1912 }
1913 }
1914
1915 // We don't know about this section name.
1916 return NULL;
1917}
1918
a445fddf
ILT
1919// Set the section address. Note that the OUTPUT_SECTION_ field will
1920// be NULL if no input sections were mapped to this output section.
1921// We still have to adjust dot and process symbol assignments.
1922
1923void
1924Output_section_definition::set_section_addresses(Symbol_table* symtab,
1925 Layout* layout,
fd247bfe 1926 uint64_t* dot_value,
f6973bdc 1927 uint64_t* dot_alignment,
fd247bfe 1928 uint64_t* load_address)
a445fddf 1929{
a445fddf 1930 uint64_t address;
1e5d2fb1
DK
1931 uint64_t old_dot_value = *dot_value;
1932 uint64_t old_load_address = *load_address;
1933
77e65537
ILT
1934 if (this->address_ == NULL)
1935 address = *dot_value;
a445fddf
ILT
1936 else
1937 {
77e65537 1938 Output_section* dummy;
919ed24c 1939 address = this->address_->eval_with_dot(symtab, layout, true,
f6973bdc
ILT
1940 *dot_value, NULL, &dummy,
1941 dot_alignment);
a445fddf
ILT
1942 }
1943
1944 uint64_t align;
1945 if (this->align_ == NULL)
1946 {
1947 if (this->output_section_ == NULL)
1948 align = 0;
1949 else
1950 align = this->output_section_->addralign();
1951 }
1952 else
1953 {
77e65537 1954 Output_section* align_section;
919ed24c 1955 align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
f6973bdc 1956 NULL, &align_section, NULL);
77e65537
ILT
1957 if (align_section != NULL)
1958 gold_warning(_("alignment of section %s is not absolute"),
1959 this->name_.c_str());
a445fddf
ILT
1960 if (this->output_section_ != NULL)
1961 this->output_section_->set_addralign(align);
1962 }
1963
1964 address = align_address(address, align);
1965
fd247bfe
ILT
1966 uint64_t start_address = address;
1967
a445fddf 1968 *dot_value = address;
a445fddf 1969
1e5d2fb1
DK
1970 // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
1971 // forced to zero, regardless of what the linker script wants.
a445fddf 1972 if (this->output_section_ != NULL
1e5d2fb1
DK
1973 && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
1974 || this->output_section_->is_noload()))
a445fddf
ILT
1975 this->output_section_->set_address(address);
1976
8f2eb564
ILT
1977 this->evaluated_address_ = address;
1978 this->evaluated_addralign_ = align;
1979
1980 if (this->load_address_ == NULL)
1981 this->evaluated_load_address_ = address;
1982 else
a445fddf 1983 {
77e65537 1984 Output_section* dummy;
55458500 1985 uint64_t laddr =
919ed24c 1986 this->load_address_->eval_with_dot(symtab, layout, true, *dot_value,
f6973bdc
ILT
1987 this->output_section_, &dummy,
1988 NULL);
8f2eb564 1989 if (this->output_section_ != NULL)
55458500
ILT
1990 this->output_section_->set_load_address(laddr);
1991 this->evaluated_load_address_ = laddr;
a445fddf
ILT
1992 }
1993
1994 uint64_t subalign;
1995 if (this->subalign_ == NULL)
1996 subalign = 0;
1997 else
1998 {
77e65537 1999 Output_section* subalign_section;
919ed24c
ILT
2000 subalign = this->subalign_->eval_with_dot(symtab, layout, true,
2001 *dot_value, NULL,
f6973bdc 2002 &subalign_section, NULL);
77e65537
ILT
2003 if (subalign_section != NULL)
2004 gold_warning(_("subalign of section %s is not absolute"),
2005 this->name_.c_str());
a445fddf
ILT
2006 }
2007
2008 std::string fill;
2009 if (this->fill_ != NULL)
2010 {
2011 // FIXME: The GNU linker supports fill values of arbitrary
2012 // length.
77e65537 2013 Output_section* fill_section;
919ed24c 2014 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
a445fddf 2015 *dot_value,
f6973bdc
ILT
2016 NULL, &fill_section,
2017 NULL);
77e65537
ILT
2018 if (fill_section != NULL)
2019 gold_warning(_("fill of section %s is not absolute"),
2020 this->name_.c_str());
a445fddf
ILT
2021 unsigned char fill_buff[4];
2022 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2023 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2024 }
2025
2026 Input_section_list input_sections;
2027 if (this->output_section_ != NULL)
2028 {
2029 // Get the list of input sections attached to this output
2030 // section. This will leave the output section with only
2031 // Output_section_data entries.
2032 address += this->output_section_->get_input_sections(address,
2033 fill,
2034 &input_sections);
2035 *dot_value = address;
2036 }
2037
77e65537 2038 Output_section* dot_section = this->output_section_;
a445fddf
ILT
2039 for (Output_section_elements::iterator p = this->elements_.begin();
2040 p != this->elements_.end();
2041 ++p)
2042 (*p)->set_section_addresses(symtab, layout, this->output_section_,
f6973bdc
ILT
2043 subalign, dot_value, dot_alignment,
2044 &dot_section, &fill, &input_sections);
a445fddf
ILT
2045
2046 gold_assert(input_sections.empty());
fd247bfe
ILT
2047
2048 if (this->load_address_ == NULL || this->output_section_ == NULL)
2049 *load_address = *dot_value;
2050 else
2051 *load_address = (this->output_section_->load_address()
2052 + (*dot_value - start_address));
2d924fd9
ILT
2053
2054 if (this->output_section_ != NULL)
2055 {
2056 if (this->is_relro_)
2057 this->output_section_->set_is_relro();
2058 else
2059 this->output_section_->clear_is_relro();
1e5d2fb1
DK
2060
2061 // If this is a NOLOAD section, keep dot and load address unchanged.
2062 if (this->output_section_->is_noload())
2063 {
2064 *dot_value = old_dot_value;
2065 *load_address = old_load_address;
2066 }
2d924fd9 2067 }
a445fddf
ILT
2068}
2069
3802b2dd
ILT
2070// Check a constraint (ONLY_IF_RO, etc.) on an output section. If
2071// this section is constrained, and the input sections do not match,
2072// return the constraint, and set *POSD.
2073
2074Section_constraint
2075Output_section_definition::check_constraint(Output_section_definition** posd)
2076{
2077 switch (this->constraint_)
2078 {
2079 case CONSTRAINT_NONE:
2080 return CONSTRAINT_NONE;
2081
2082 case CONSTRAINT_ONLY_IF_RO:
2083 if (this->output_section_ != NULL
2084 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2085 {
2086 *posd = this;
2087 return CONSTRAINT_ONLY_IF_RO;
2088 }
2089 return CONSTRAINT_NONE;
2090
2091 case CONSTRAINT_ONLY_IF_RW:
2092 if (this->output_section_ != NULL
2093 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2094 {
2095 *posd = this;
2096 return CONSTRAINT_ONLY_IF_RW;
2097 }
2098 return CONSTRAINT_NONE;
2099
2100 case CONSTRAINT_SPECIAL:
2101 if (this->output_section_ != NULL)
2102 gold_error(_("SPECIAL constraints are not implemented"));
2103 return CONSTRAINT_NONE;
2104
2105 default:
2106 gold_unreachable();
2107 }
2108}
2109
2110// See if this is the alternate output section for a constrained
2111// output section. If it is, transfer the Output_section and return
2112// true. Otherwise return false.
2113
2114bool
2115Output_section_definition::alternate_constraint(
2116 Output_section_definition* posd,
2117 Section_constraint constraint)
2118{
2119 if (this->name_ != posd->name_)
2120 return false;
2121
2122 switch (constraint)
2123 {
2124 case CONSTRAINT_ONLY_IF_RO:
2125 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2126 return false;
2127 break;
2128
2129 case CONSTRAINT_ONLY_IF_RW:
2130 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2131 return false;
2132 break;
2133
2134 default:
2135 gold_unreachable();
2136 }
2137
2138 // We have found the alternate constraint. We just need to move
2139 // over the Output_section. When constraints are used properly,
2140 // THIS should not have an output_section pointer, as all the input
2141 // sections should have matched the other definition.
2142
2143 if (this->output_section_ != NULL)
2144 gold_error(_("mismatched definition for constrained sections"));
2145
2146 this->output_section_ = posd->output_section_;
2147 posd->output_section_ = NULL;
2148
2d924fd9
ILT
2149 if (this->is_relro_)
2150 this->output_section_->set_is_relro();
2151 else
2152 this->output_section_->clear_is_relro();
2153
3802b2dd
ILT
2154 return true;
2155}
2156
1c4f3631 2157// Get the list of segments to use for an allocated section when using
2cefc357 2158// a PHDRS clause.
1c4f3631
ILT
2159
2160Output_section*
2cefc357
ILT
2161Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2162 bool* orphan)
1c4f3631
ILT
2163{
2164 if (this->output_section_ == NULL)
2165 return NULL;
2166 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2167 return NULL;
2cefc357 2168 *orphan = false;
1c4f3631
ILT
2169 if (this->phdrs_ != NULL)
2170 *phdrs_list = this->phdrs_;
2171 return this->output_section_;
2172}
2173
8f2eb564
ILT
2174// Look for an output section by name and return the address, the load
2175// address, the alignment, and the size. This is used when an
2176// expression refers to an output section which was not actually
2177// created. This returns true if the section was found, false
2178// otherwise.
2179
2180bool
2181Output_section_definition::get_output_section_info(const char* name,
2182 uint64_t* address,
2183 uint64_t* load_address,
2ea97941 2184 uint64_t* addralign,
8f2eb564
ILT
2185 uint64_t* size) const
2186{
2187 if (this->name_ != name)
2188 return false;
2189
2190 if (this->output_section_ != NULL)
2191 {
2192 *address = this->output_section_->address();
2193 if (this->output_section_->has_load_address())
2194 *load_address = this->output_section_->load_address();
2195 else
2196 *load_address = *address;
2ea97941 2197 *addralign = this->output_section_->addralign();
8f2eb564
ILT
2198 *size = this->output_section_->current_data_size();
2199 }
2200 else
2201 {
2202 *address = this->evaluated_address_;
2203 *load_address = this->evaluated_load_address_;
2ea97941 2204 *addralign = this->evaluated_addralign_;
8f2eb564
ILT
2205 *size = 0;
2206 }
2207
2208 return true;
2209}
2210
494e05f4
ILT
2211// Print for debugging.
2212
2213void
2214Output_section_definition::print(FILE* f) const
2215{
2216 fprintf(f, " %s ", this->name_.c_str());
2217
2218 if (this->address_ != NULL)
2219 {
2220 this->address_->print(f);
2221 fprintf(f, " ");
2222 }
2223
1e5d2fb1
DK
2224 if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2225 fprintf(f, "(%s) ",
2226 this->script_section_type_name(this->script_section_type_));
2227
494e05f4
ILT
2228 fprintf(f, ": ");
2229
2230 if (this->load_address_ != NULL)
2231 {
2232 fprintf(f, "AT(");
2233 this->load_address_->print(f);
2234 fprintf(f, ") ");
2235 }
2236
2237 if (this->align_ != NULL)
2238 {
2239 fprintf(f, "ALIGN(");
2240 this->align_->print(f);
2241 fprintf(f, ") ");
2242 }
2243
2244 if (this->subalign_ != NULL)
2245 {
2246 fprintf(f, "SUBALIGN(");
2247 this->subalign_->print(f);
2248 fprintf(f, ") ");
2249 }
2250
2251 fprintf(f, "{\n");
2252
2253 for (Output_section_elements::const_iterator p = this->elements_.begin();
2254 p != this->elements_.end();
2255 ++p)
2256 (*p)->print(f);
2257
2258 fprintf(f, " }");
2259
2260 if (this->fill_ != NULL)
2261 {
2262 fprintf(f, " = ");
2263 this->fill_->print(f);
2264 }
2265
7d26c6cc
ILT
2266 if (this->phdrs_ != NULL)
2267 {
2268 for (String_list::const_iterator p = this->phdrs_->begin();
2269 p != this->phdrs_->end();
2270 ++p)
2271 fprintf(f, " :%s", p->c_str());
2272 }
2273
494e05f4
ILT
2274 fprintf(f, "\n");
2275}
2276
1e5d2fb1
DK
2277Script_sections::Section_type
2278Output_section_definition::section_type() const
2279{
2280 switch (this->script_section_type_)
2281 {
2282 case SCRIPT_SECTION_TYPE_NONE:
2283 return Script_sections::ST_NONE;
2284 case SCRIPT_SECTION_TYPE_NOLOAD:
2285 return Script_sections::ST_NOLOAD;
2286 case SCRIPT_SECTION_TYPE_COPY:
2287 case SCRIPT_SECTION_TYPE_DSECT:
2288 case SCRIPT_SECTION_TYPE_INFO:
2289 case SCRIPT_SECTION_TYPE_OVERLAY:
2290 // There are not really support so we treat them as ST_NONE. The
2291 // parse should have issued errors for them already.
2292 return Script_sections::ST_NONE;
2293 default:
2294 gold_unreachable();
2295 }
2296}
2297
2298// Return the name of a script section type.
2299
2300const char*
2301Output_section_definition::script_section_type_name (
2302 Script_section_type script_section_type)
2303{
2304 switch (script_section_type)
2305 {
2306 case SCRIPT_SECTION_TYPE_NONE:
2307 return "NONE";
2308 case SCRIPT_SECTION_TYPE_NOLOAD:
2309 return "NOLOAD";
2310 case SCRIPT_SECTION_TYPE_DSECT:
2311 return "DSECT";
2312 case SCRIPT_SECTION_TYPE_COPY:
2313 return "COPY";
2314 case SCRIPT_SECTION_TYPE_INFO:
2315 return "INFO";
2316 case SCRIPT_SECTION_TYPE_OVERLAY:
2317 return "OVERLAY";
2318 default:
2319 gold_unreachable();
2320 }
2321}
2322
a445fddf
ILT
2323// An output section created to hold orphaned input sections. These
2324// do not actually appear in linker scripts. However, for convenience
2325// when setting the output section addresses, we put a marker to these
2326// sections in the appropriate place in the list of SECTIONS elements.
2327
2328class Orphan_output_section : public Sections_element
2329{
2330 public:
2331 Orphan_output_section(Output_section* os)
2332 : os_(os)
2333 { }
2334
0d371ad3
ILT
2335 // Return whether the orphan output section is relro. We can just
2336 // check the output section because we always set the flag, if
2337 // needed, just after we create the Orphan_output_section.
a445fddf 2338 bool
0d371ad3
ILT
2339 is_relro() const
2340 { return this->os_->is_relro(); }
2341
2342 // Initialize OSP with an output section. This should have been
2343 // done already.
2344 void
2345 orphan_section_init(Orphan_section_placement*,
2346 Script_sections::Elements_iterator)
2347 { gold_unreachable(); }
a445fddf
ILT
2348
2349 // Set section addresses.
2350 void
f6973bdc
ILT
2351 set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
2352 uint64_t*);
a445fddf 2353
1c4f3631 2354 // Get the list of segments to use for an allocated section when
2cefc357 2355 // using a PHDRS clause.
1c4f3631 2356 Output_section*
2cefc357 2357 allocate_to_segment(String_list**, bool*);
1c4f3631 2358
2d924fd9
ILT
2359 // Return the associated Output_section.
2360 Output_section*
2361 get_output_section() const
2362 { return this->os_; }
2363
a445fddf
ILT
2364 // Print for debugging.
2365 void
2366 print(FILE* f) const
2367 {
2368 fprintf(f, " marker for orphaned output section %s\n",
2369 this->os_->name());
2370 }
2371
2372 private:
2373 Output_section* os_;
2374};
2375
a445fddf
ILT
2376// Set section addresses.
2377
2378void
2379Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
fd247bfe 2380 uint64_t* dot_value,
f6973bdc 2381 uint64_t*,
fd247bfe 2382 uint64_t* load_address)
a445fddf 2383{
20e6d0d6 2384 typedef std::list<Output_section::Simple_input_section> Input_section_list;
a445fddf 2385
fd247bfe
ILT
2386 bool have_load_address = *load_address != *dot_value;
2387
a445fddf
ILT
2388 uint64_t address = *dot_value;
2389 address = align_address(address, this->os_->addralign());
2390
2391 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
fd247bfe
ILT
2392 {
2393 this->os_->set_address(address);
2394 if (have_load_address)
2395 this->os_->set_load_address(align_address(*load_address,
2396 this->os_->addralign()));
2397 }
a445fddf
ILT
2398
2399 Input_section_list input_sections;
2400 address += this->os_->get_input_sections(address, "", &input_sections);
2401
2402 for (Input_section_list::iterator p = input_sections.begin();
2403 p != input_sections.end();
2404 ++p)
2405 {
2ea97941 2406 uint64_t addralign;
a445fddf
ILT
2407 uint64_t size;
2408
6c93b22c 2409 // We know we are single-threaded, so it is OK to lock the
a445fddf
ILT
2410 // object.
2411 {
2412 const Task* task = reinterpret_cast<const Task*>(-1);
20e6d0d6 2413 Task_lock_obj<Object> tl(task, p->relobj());
2ea97941 2414 addralign = p->relobj()->section_addralign(p->shndx());
20e6d0d6 2415 if (p->is_relaxed_input_section())
c0a62865
DK
2416 // We use current data size because relxed section sizes may not
2417 // have finalized yet.
2418 size = p->relaxed_input_section()->current_data_size();
20e6d0d6
DK
2419 else
2420 size = p->relobj()->section_size(p->shndx());
a445fddf
ILT
2421 }
2422
2ea97941 2423 address = align_address(address, addralign);
8923b24c 2424 this->os_->add_simple_input_section(*p, size, addralign);
a445fddf
ILT
2425 address += size;
2426 }
2427
661be1e2
ILT
2428 // An SHF_TLS/SHT_NOBITS section does not take up any address space.
2429 if (this->os_ == NULL
2430 || (this->os_->flags() & elfcpp::SHF_TLS) == 0
2431 || this->os_->type() != elfcpp::SHT_NOBITS)
2432 {
2433 if (!have_load_address)
2434 *load_address = address;
2435 else
2436 *load_address += address - *dot_value;
fd247bfe 2437
661be1e2
ILT
2438 *dot_value = address;
2439 }
a445fddf
ILT
2440}
2441
1c4f3631
ILT
2442// Get the list of segments to use for an allocated section when using
2443// a PHDRS clause. If this is an allocated section, return the
2444// Output_section. We don't change the list of segments.
2445
2446Output_section*
2cefc357 2447Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
1c4f3631
ILT
2448{
2449 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
2450 return NULL;
2cefc357 2451 *orphan = true;
1c4f3631
ILT
2452 return this->os_;
2453}
2454
2455// Class Phdrs_element. A program header from a PHDRS clause.
2456
2457class Phdrs_element
2458{
2459 public:
2ea97941
ILT
2460 Phdrs_element(const char* name, size_t namelen, unsigned int type,
2461 bool includes_filehdr, bool includes_phdrs,
1c4f3631 2462 bool is_flags_valid, unsigned int flags,
2ea97941
ILT
2463 Expression* load_address)
2464 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
2465 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
2466 flags_(flags), load_address_(load_address), load_address_value_(0),
1c4f3631
ILT
2467 segment_(NULL)
2468 { }
2469
2470 // Return the name of this segment.
2471 const std::string&
2472 name() const
2473 { return this->name_; }
2474
2475 // Return the type of the segment.
2476 unsigned int
2477 type() const
2478 { return this->type_; }
2479
2480 // Whether to include the file header.
2481 bool
2482 includes_filehdr() const
2483 { return this->includes_filehdr_; }
2484
2485 // Whether to include the program headers.
2486 bool
2487 includes_phdrs() const
2488 { return this->includes_phdrs_; }
2489
2490 // Return whether there is a load address.
2491 bool
2492 has_load_address() const
2493 { return this->load_address_ != NULL; }
2494
2495 // Evaluate the load address expression if there is one.
2496 void
2ea97941 2497 eval_load_address(Symbol_table* symtab, Layout* layout)
1c4f3631
ILT
2498 {
2499 if (this->load_address_ != NULL)
2ea97941 2500 this->load_address_value_ = this->load_address_->eval(symtab, layout,
919ed24c 2501 true);
1c4f3631
ILT
2502 }
2503
2504 // Return the load address.
2505 uint64_t
2506 load_address() const
2507 {
2508 gold_assert(this->load_address_ != NULL);
2509 return this->load_address_value_;
2510 }
2511
2512 // Create the segment.
2513 Output_segment*
2514 create_segment(Layout* layout)
2515 {
2516 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
2517 return this->segment_;
2518 }
2519
2520 // Return the segment.
2521 Output_segment*
2522 segment()
2523 { return this->segment_; }
2524
20e6d0d6
DK
2525 // Release the segment.
2526 void
2527 release_segment()
2528 { this->segment_ = NULL; }
2529
1c4f3631
ILT
2530 // Set the segment flags if appropriate.
2531 void
2532 set_flags_if_valid()
2533 {
2534 if (this->is_flags_valid_)
2535 this->segment_->set_flags(this->flags_);
2536 }
2537
7d26c6cc
ILT
2538 // Print for debugging.
2539 void
2540 print(FILE*) const;
2541
1c4f3631
ILT
2542 private:
2543 // The name used in the script.
2544 std::string name_;
2545 // The type of the segment (PT_LOAD, etc.).
2546 unsigned int type_;
2547 // Whether this segment includes the file header.
2548 bool includes_filehdr_;
2549 // Whether this segment includes the section headers.
2550 bool includes_phdrs_;
2551 // Whether the flags were explicitly specified.
2552 bool is_flags_valid_;
2553 // The flags for this segment (PF_R, etc.) if specified.
2554 unsigned int flags_;
2555 // The expression for the load address for this segment. This may
2556 // be NULL.
2557 Expression* load_address_;
2558 // The actual load address from evaluating the expression.
2559 uint64_t load_address_value_;
2560 // The segment itself.
2561 Output_segment* segment_;
2562};
2563
7d26c6cc
ILT
2564// Print for debugging.
2565
2566void
2567Phdrs_element::print(FILE* f) const
2568{
2569 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
2570 if (this->includes_filehdr_)
2571 fprintf(f, " FILEHDR");
2572 if (this->includes_phdrs_)
2573 fprintf(f, " PHDRS");
2574 if (this->is_flags_valid_)
2575 fprintf(f, " FLAGS(%u)", this->flags_);
2576 if (this->load_address_ != NULL)
2577 {
2578 fprintf(f, " AT(");
2579 this->load_address_->print(f);
2580 fprintf(f, ")");
2581 }
2582 fprintf(f, ";\n");
2583}
2584
494e05f4
ILT
2585// Class Script_sections.
2586
2587Script_sections::Script_sections()
2588 : saw_sections_clause_(false),
2589 in_sections_clause_(false),
2590 sections_elements_(NULL),
1c4f3631 2591 output_section_(NULL),
2d924fd9 2592 phdrs_elements_(NULL),
0d371ad3
ILT
2593 orphan_section_placement_(NULL),
2594 data_segment_align_start_(),
2595 saw_data_segment_align_(false),
3c12dcdb
DK
2596 saw_relro_end_(false),
2597 saw_segment_start_expression_(false)
494e05f4
ILT
2598{
2599}
2600
2601// Start a SECTIONS clause.
2602
2603void
2604Script_sections::start_sections()
2605{
2606 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2607 this->saw_sections_clause_ = true;
2608 this->in_sections_clause_ = true;
2609 if (this->sections_elements_ == NULL)
2610 this->sections_elements_ = new Sections_elements;
2611}
2612
2613// Finish a SECTIONS clause.
2614
2615void
2616Script_sections::finish_sections()
2617{
2618 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2619 this->in_sections_clause_ = false;
2620}
2621
2622// Add a symbol to be defined.
2623
2624void
2625Script_sections::add_symbol_assignment(const char* name, size_t length,
2626 Expression* val, bool provide,
2627 bool hidden)
2628{
2629 if (this->output_section_ != NULL)
2630 this->output_section_->add_symbol_assignment(name, length, val,
2631 provide, hidden);
2632 else
2633 {
2634 Sections_element* p = new Sections_element_assignment(name, length,
2635 val, provide,
2636 hidden);
2637 this->sections_elements_->push_back(p);
2638 }
2639}
2640
a445fddf
ILT
2641// Add an assignment to the special dot symbol.
2642
2643void
2644Script_sections::add_dot_assignment(Expression* val)
2645{
2646 if (this->output_section_ != NULL)
2647 this->output_section_->add_dot_assignment(val);
2648 else
2649 {
12edd763
ILT
2650 // The GNU linker permits assignments to . to appears outside of
2651 // a SECTIONS clause, and treats it as appearing inside, so
2652 // sections_elements_ may be NULL here.
2653 if (this->sections_elements_ == NULL)
2654 {
2655 this->sections_elements_ = new Sections_elements;
2656 this->saw_sections_clause_ = true;
2657 }
2658
a445fddf
ILT
2659 Sections_element* p = new Sections_element_dot_assignment(val);
2660 this->sections_elements_->push_back(p);
2661 }
2662}
2663
494e05f4
ILT
2664// Add an assertion.
2665
2666void
2667Script_sections::add_assertion(Expression* check, const char* message,
2668 size_t messagelen)
2669{
2670 if (this->output_section_ != NULL)
2671 this->output_section_->add_assertion(check, message, messagelen);
2672 else
2673 {
2674 Sections_element* p = new Sections_element_assertion(check, message,
2675 messagelen);
2676 this->sections_elements_->push_back(p);
2677 }
2678}
2679
2680// Start processing entries for an output section.
2681
2682void
2683Script_sections::start_output_section(
2684 const char* name,
2685 size_t namelen,
2686 const Parser_output_section_header *header)
2687{
2688 Output_section_definition* posd = new Output_section_definition(name,
2689 namelen,
2690 header);
2691 this->sections_elements_->push_back(posd);
2692 gold_assert(this->output_section_ == NULL);
2693 this->output_section_ = posd;
2694}
2695
2696// Stop processing entries for an output section.
2697
2698void
2699Script_sections::finish_output_section(
2700 const Parser_output_section_trailer* trailer)
2701{
2702 gold_assert(this->output_section_ != NULL);
2703 this->output_section_->finish(trailer);
2704 this->output_section_ = NULL;
2705}
2706
2707// Add a data item to the current output section.
2708
2709void
2710Script_sections::add_data(int size, bool is_signed, Expression* val)
2711{
2712 gold_assert(this->output_section_ != NULL);
2713 this->output_section_->add_data(size, is_signed, val);
2714}
2715
2716// Add a fill value setting to the current output section.
2717
2718void
2719Script_sections::add_fill(Expression* val)
2720{
2721 gold_assert(this->output_section_ != NULL);
2722 this->output_section_->add_fill(val);
2723}
2724
2725// Add an input section specification to the current output section.
2726
2727void
2728Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2729{
2730 gold_assert(this->output_section_ != NULL);
2731 this->output_section_->add_input_section(spec, keep);
2732}
2733
2d924fd9
ILT
2734// This is called when we see DATA_SEGMENT_ALIGN. It means that any
2735// subsequent output sections may be relro.
2736
2737void
2738Script_sections::data_segment_align()
2739{
0d371ad3 2740 if (this->saw_data_segment_align_)
2d924fd9 2741 gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
0d371ad3
ILT
2742 gold_assert(!this->sections_elements_->empty());
2743 Sections_elements::iterator p = this->sections_elements_->end();
2744 --p;
2745 this->data_segment_align_start_ = p;
2746 this->saw_data_segment_align_ = true;
2d924fd9
ILT
2747}
2748
2749// This is called when we see DATA_SEGMENT_RELRO_END. It means that
2750// any output sections seen since DATA_SEGMENT_ALIGN are relro.
2751
2752void
2753Script_sections::data_segment_relro_end()
2754{
2755 if (this->saw_relro_end_)
2756 gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
2757 "in a linker script"));
2758 this->saw_relro_end_ = true;
2759
0d371ad3 2760 if (!this->saw_data_segment_align_)
2d924fd9
ILT
2761 gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
2762 else
2763 {
0d371ad3
ILT
2764 Sections_elements::iterator p = this->data_segment_align_start_;
2765 for (++p; p != this->sections_elements_->end(); ++p)
2766 (*p)->set_is_relro();
2d924fd9
ILT
2767 }
2768}
2769
919ed24c
ILT
2770// Create any required sections.
2771
2772void
2773Script_sections::create_sections(Layout* layout)
2774{
2775 if (!this->saw_sections_clause_)
2776 return;
2777 for (Sections_elements::iterator p = this->sections_elements_->begin();
2778 p != this->sections_elements_->end();
2779 ++p)
2780 (*p)->create_sections(layout);
2781}
2782
a445fddf
ILT
2783// Add any symbols we are defining to the symbol table.
2784
2785void
2786Script_sections::add_symbols_to_table(Symbol_table* symtab)
2787{
2788 if (!this->saw_sections_clause_)
2789 return;
2790 for (Sections_elements::iterator p = this->sections_elements_->begin();
2791 p != this->sections_elements_->end();
2792 ++p)
2793 (*p)->add_symbols_to_table(symtab);
2794}
2795
2796// Finalize symbols and check assertions.
2797
2798void
2799Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2800{
2801 if (!this->saw_sections_clause_)
2802 return;
a445fddf
ILT
2803 uint64_t dot_value = 0;
2804 for (Sections_elements::iterator p = this->sections_elements_->begin();
2805 p != this->sections_elements_->end();
2806 ++p)
77e65537 2807 (*p)->finalize_symbols(symtab, layout, &dot_value);
a445fddf
ILT
2808}
2809
2810// Return the name of the output section to use for an input file name
2811// and section name.
2812
2813const char*
1e5d2fb1
DK
2814Script_sections::output_section_name(
2815 const char* file_name,
2816 const char* section_name,
2817 Output_section*** output_section_slot,
2818 Script_sections::Section_type *psection_type)
a445fddf
ILT
2819{
2820 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2821 p != this->sections_elements_->end();
2822 ++p)
2823 {
2824 const char* ret = (*p)->output_section_name(file_name, section_name,
1e5d2fb1
DK
2825 output_section_slot,
2826 psection_type);
a445fddf
ILT
2827
2828 if (ret != NULL)
2829 {
2830 // The special name /DISCARD/ means that the input section
2831 // should be discarded.
2832 if (strcmp(ret, "/DISCARD/") == 0)
2833 {
2834 *output_section_slot = NULL;
1e5d2fb1 2835 *psection_type = Script_sections::ST_NONE;
a445fddf
ILT
2836 return NULL;
2837 }
2838 return ret;
2839 }
2840 }
2841
2842 // If we couldn't find a mapping for the name, the output section
2843 // gets the name of the input section.
2844
2845 *output_section_slot = NULL;
1e5d2fb1 2846 *psection_type = Script_sections::ST_NONE;
a445fddf
ILT
2847
2848 return section_name;
2849}
2850
2851// Place a marker for an orphan output section into the SECTIONS
2852// clause.
2853
2854void
2855Script_sections::place_orphan(Output_section* os)
2856{
0d371ad3
ILT
2857 Orphan_section_placement* osp = this->orphan_section_placement_;
2858 if (osp == NULL)
a445fddf 2859 {
0d371ad3
ILT
2860 // Initialize the Orphan_section_placement structure.
2861 osp = new Orphan_section_placement();
2862 for (Sections_elements::iterator p = this->sections_elements_->begin();
2863 p != this->sections_elements_->end();
2864 ++p)
2865 (*p)->orphan_section_init(osp, p);
2866 gold_assert(!this->sections_elements_->empty());
2867 Sections_elements::iterator last = this->sections_elements_->end();
2868 --last;
2869 osp->last_init(last);
2870 this->orphan_section_placement_ = osp;
a445fddf
ILT
2871 }
2872
0d371ad3 2873 Orphan_output_section* orphan = new Orphan_output_section(os);
2d924fd9 2874
0d371ad3
ILT
2875 // Look for where to put ORPHAN.
2876 Sections_elements::iterator* where;
2877 if (osp->find_place(os, &where))
2878 {
2879 if ((**where)->is_relro())
2880 os->set_is_relro();
2881 else
2882 os->clear_is_relro();
2883
2884 // We want to insert ORPHAN after *WHERE, and then update *WHERE
2885 // so that the next one goes after this one.
2886 Sections_elements::iterator p = *where;
2887 gold_assert(p != this->sections_elements_->end());
2888 ++p;
2889 *where = this->sections_elements_->insert(p, orphan);
2890 }
2d924fd9 2891 else
0d371ad3
ILT
2892 {
2893 os->clear_is_relro();
2894 // We don't have a place to put this orphan section. Put it,
2895 // and all other sections like it, at the end, but before the
2896 // sections which always come at the end.
2897 Sections_elements::iterator last = osp->last_place();
2898 *where = this->sections_elements_->insert(last, orphan);
2899 }
a445fddf
ILT
2900}
2901
2902// Set the addresses of all the output sections. Walk through all the
2903// elements, tracking the dot symbol. Apply assignments which set
2904// absolute symbol values, in case they are used when setting dot.
2905// Fill in data statement values. As we find output sections, set the
2906// address, set the address of all associated input sections, and
2907// update dot. Return the segment which should hold the file header
2908// and segment headers, if any.
2909
2910Output_segment*
2911Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2912{
2913 gold_assert(this->saw_sections_clause_);
2914
3802b2dd
ILT
2915 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2916 // for our representation.
2917 for (Sections_elements::iterator p = this->sections_elements_->begin();
2918 p != this->sections_elements_->end();
2919 ++p)
2920 {
2921 Output_section_definition* posd;
2922 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2923 if (failed_constraint != CONSTRAINT_NONE)
2924 {
2925 Sections_elements::iterator q;
2926 for (q = this->sections_elements_->begin();
2927 q != this->sections_elements_->end();
2928 ++q)
2929 {
2930 if (q != p)
2931 {
2932 if ((*q)->alternate_constraint(posd, failed_constraint))
2933 break;
2934 }
2935 }
2936
2937 if (q == this->sections_elements_->end())
2938 gold_error(_("no matching section constraint"));
2939 }
2940 }
2941
2d924fd9
ILT
2942 // Force the alignment of the first TLS section to be the maximum
2943 // alignment of all TLS sections.
2944 Output_section* first_tls = NULL;
2945 uint64_t tls_align = 0;
2946 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2947 p != this->sections_elements_->end();
2948 ++p)
2949 {
2950 Output_section *os = (*p)->get_output_section();
2951 if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
2952 {
2953 if (first_tls == NULL)
2954 first_tls = os;
2955 if (os->addralign() > tls_align)
2956 tls_align = os->addralign();
2957 }
2958 }
2959 if (first_tls != NULL)
2960 first_tls->set_addralign(tls_align);
2961
77e65537 2962 // For a relocatable link, we implicitly set dot to zero.
a445fddf 2963 uint64_t dot_value = 0;
f6973bdc 2964 uint64_t dot_alignment = 0;
fd247bfe 2965 uint64_t load_address = 0;
3c12dcdb
DK
2966
2967 // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
2968 // to set section addresses. If the script has any SEGMENT_START
2969 // expression, we do not set the section addresses.
2970 bool use_tsection_options =
2971 (!this->saw_segment_start_expression_
2972 && (parameters->options().user_set_Ttext()
2973 || parameters->options().user_set_Tdata()
2974 || parameters->options().user_set_Tbss()));
2975
a445fddf
ILT
2976 for (Sections_elements::iterator p = this->sections_elements_->begin();
2977 p != this->sections_elements_->end();
2978 ++p)
3c12dcdb
DK
2979 {
2980 Output_section* os = (*p)->get_output_section();
2981
2982 // Handle -Ttext, -Tdata and -Tbss options. We do this by looking for
2983 // the special sections by names and doing dot assignments.
2984 if (use_tsection_options
2985 && os != NULL
2986 && (os->flags() & elfcpp::SHF_ALLOC) != 0)
2987 {
2988 uint64_t new_dot_value = dot_value;
2989
2990 if (parameters->options().user_set_Ttext()
2991 && strcmp(os->name(), ".text") == 0)
2992 new_dot_value = parameters->options().Ttext();
2993 else if (parameters->options().user_set_Tdata()
2994 && strcmp(os->name(), ".data") == 0)
2995 new_dot_value = parameters->options().Tdata();
2996 else if (parameters->options().user_set_Tbss()
2997 && strcmp(os->name(), ".bss") == 0)
2998 new_dot_value = parameters->options().Tbss();
2999
3000 // Update dot and load address if necessary.
3001 if (new_dot_value < dot_value)
3002 gold_error(_("dot may not move backward"));
3003 else if (new_dot_value != dot_value)
3004 {
3005 dot_value = new_dot_value;
3006 load_address = new_dot_value;
3007 }
3008 }
3009
f6973bdc
ILT
3010 (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3011 &load_address);
3c12dcdb 3012 }
a445fddf 3013
1c4f3631
ILT
3014 if (this->phdrs_elements_ != NULL)
3015 {
3016 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3017 p != this->phdrs_elements_->end();
3018 ++p)
3019 (*p)->eval_load_address(symtab, layout);
3020 }
3021
f6973bdc 3022 return this->create_segments(layout, dot_alignment);
a445fddf
ILT
3023}
3024
3025// Sort the sections in order to put them into segments.
3026
3027class Sort_output_sections
3028{
3029 public:
3030 bool
3031 operator()(const Output_section* os1, const Output_section* os2) const;
3032};
3033
3034bool
3035Sort_output_sections::operator()(const Output_section* os1,
3036 const Output_section* os2) const
3037{
3038 // Sort first by the load address.
3039 uint64_t lma1 = (os1->has_load_address()
3040 ? os1->load_address()
3041 : os1->address());
3042 uint64_t lma2 = (os2->has_load_address()
3043 ? os2->load_address()
3044 : os2->address());
3045 if (lma1 != lma2)
3046 return lma1 < lma2;
3047
3048 // Then sort by the virtual address.
3049 if (os1->address() != os2->address())
3050 return os1->address() < os2->address();
3051
3052 // Sort TLS sections to the end.
3053 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3054 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3055 if (tls1 != tls2)
3056 return tls2;
3057
3058 // Sort PROGBITS before NOBITS.
3059 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
3060 return true;
3061 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
3062 return false;
3063
1e5d2fb1
DK
3064 // Sort non-NOLOAD before NOLOAD.
3065 if (os1->is_noload() && !os2->is_noload())
3066 return true;
3067 if (!os1->is_noload() && os2->is_noload())
3068 return true;
3069
a445fddf
ILT
3070 // Otherwise we don't care.
3071 return false;
3072}
3073
3074// Return whether OS is a BSS section. This is a SHT_NOBITS section.
3075// We treat a section with the SHF_TLS flag set as taking up space
3076// even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3077// space for them in the file.
3078
3079bool
3080Script_sections::is_bss_section(const Output_section* os)
3081{
3082 return (os->type() == elfcpp::SHT_NOBITS
3083 && (os->flags() & elfcpp::SHF_TLS) == 0);
3084}
3085
1c4f3631
ILT
3086// Return the size taken by the file header and the program headers.
3087
3088size_t
3089Script_sections::total_header_size(Layout* layout) const
3090{
3091 size_t segment_count = layout->segment_count();
3092 size_t file_header_size;
3093 size_t segment_headers_size;
8851ecca 3094 if (parameters->target().get_size() == 32)
1c4f3631
ILT
3095 {
3096 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3097 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3098 }
8851ecca 3099 else if (parameters->target().get_size() == 64)
1c4f3631
ILT
3100 {
3101 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3102 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3103 }
3104 else
3105 gold_unreachable();
3106
3107 return file_header_size + segment_headers_size;
3108}
3109
3110// Return the amount we have to subtract from the LMA to accomodate
3111// headers of the given size. The complication is that the file
3112// header have to be at the start of a page, as otherwise it will not
3113// be at the start of the file.
3114
3115uint64_t
3116Script_sections::header_size_adjustment(uint64_t lma,
3117 size_t sizeof_headers) const
3118{
8851ecca 3119 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
1c4f3631
ILT
3120 uint64_t hdr_lma = lma - sizeof_headers;
3121 hdr_lma &= ~(abi_pagesize - 1);
3122 return lma - hdr_lma;
3123}
3124
a445fddf
ILT
3125// Create the PT_LOAD segments when using a SECTIONS clause. Returns
3126// the segment which should hold the file header and segment headers,
3127// if any.
3128
3129Output_segment*
f6973bdc 3130Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
a445fddf
ILT
3131{
3132 gold_assert(this->saw_sections_clause_);
3133
8851ecca 3134 if (parameters->options().relocatable())
a445fddf
ILT
3135 return NULL;
3136
1c4f3631 3137 if (this->saw_phdrs_clause())
f6973bdc 3138 return create_segments_from_phdrs_clause(layout, dot_alignment);
1c4f3631 3139
a445fddf
ILT
3140 Layout::Section_list sections;
3141 layout->get_allocated_sections(&sections);
3142
3143 // Sort the sections by address.
3144 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
3145
3146 this->create_note_and_tls_segments(layout, &sections);
3147
3148 // Walk through the sections adding them to PT_LOAD segments.
8851ecca 3149 const uint64_t abi_pagesize = parameters->target().abi_pagesize();
a445fddf
ILT
3150 Output_segment* first_seg = NULL;
3151 Output_segment* current_seg = NULL;
3152 bool is_current_seg_readonly = true;
3153 Layout::Section_list::iterator plast = sections.end();
3154 uint64_t last_vma = 0;
3155 uint64_t last_lma = 0;
3156 uint64_t last_size = 0;
3157 for (Layout::Section_list::iterator p = sections.begin();
3158 p != sections.end();
3159 ++p)
3160 {
3161 const uint64_t vma = (*p)->address();
3162 const uint64_t lma = ((*p)->has_load_address()
3163 ? (*p)->load_address()
3164 : vma);
3165 const uint64_t size = (*p)->current_data_size();
3166
3167 bool need_new_segment;
3168 if (current_seg == NULL)
3169 need_new_segment = true;
3170 else if (lma - vma != last_lma - last_vma)
3171 {
3172 // This section has a different LMA relationship than the
3173 // last one; we need a new segment.
3174 need_new_segment = true;
3175 }
3176 else if (align_address(last_lma + last_size, abi_pagesize)
3177 < align_address(lma, abi_pagesize))
3178 {
3179 // Putting this section in the segment would require
3180 // skipping a page.
3181 need_new_segment = true;
3182 }
3183 else if (is_bss_section(*plast) && !is_bss_section(*p))
3184 {
3185 // A non-BSS section can not follow a BSS section in the
3186 // same segment.
3187 need_new_segment = true;
3188 }
3189 else if (is_current_seg_readonly
af6156ef
ILT
3190 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
3191 && !parameters->options().omagic())
a445fddf
ILT
3192 {
3193 // Don't put a writable section in the same segment as a
3194 // non-writable section.
3195 need_new_segment = true;
3196 }
3197 else
3198 {
3199 // Otherwise, reuse the existing segment.
3200 need_new_segment = false;
3201 }
3202
3203 elfcpp::Elf_Word seg_flags =
3204 Layout::section_flags_to_segment((*p)->flags());
3205
3206 if (need_new_segment)
3207 {
3208 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3209 seg_flags);
3210 current_seg->set_addresses(vma, lma);
f6973bdc 3211 current_seg->set_minimum_p_align(dot_alignment);
a445fddf
ILT
3212 if (first_seg == NULL)
3213 first_seg = current_seg;
3214 is_current_seg_readonly = true;
3215 }
3216
f5c870d2 3217 current_seg->add_output_section(*p, seg_flags, false);
a445fddf
ILT
3218
3219 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
3220 is_current_seg_readonly = false;
3221
3222 plast = p;
3223 last_vma = vma;
3224 last_lma = lma;
3225 last_size = size;
3226 }
3227
3228 // An ELF program should work even if the program headers are not in
3229 // a PT_LOAD segment. However, it appears that the Linux kernel
3230 // does not set the AT_PHDR auxiliary entry in that case. It sets
3231 // the load address to p_vaddr - p_offset of the first PT_LOAD
3232 // segment. It then sets AT_PHDR to the load address plus the
3233 // offset to the program headers, e_phoff in the file header. This
3234 // fails when the program headers appear in the file before the
3235 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
3236 // segment to hold the file header and the program headers. This is
3237 // effectively what the GNU linker does, and it is slightly more
3238 // efficient in any case. We try to use the first PT_LOAD segment
3239 // if we can, otherwise we make a new one.
3240
919ed24c
ILT
3241 if (first_seg == NULL)
3242 return NULL;
3243
3ee173de
ILT
3244 // -n or -N mean that the program is not demand paged and there is
3245 // no need to put the program headers in a PT_LOAD segment.
3246 if (parameters->options().nmagic() || parameters->options().omagic())
3247 return NULL;
3248
1c4f3631 3249 size_t sizeof_headers = this->total_header_size(layout);
3802b2dd 3250
919ed24c
ILT
3251 uint64_t vma = first_seg->vaddr();
3252 uint64_t lma = first_seg->paddr();
3253
3254 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
3255
e6188289
ILT
3256 if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
3257 {
3258 first_seg->set_addresses(vma - subtract, lma - subtract);
3259 return first_seg;
3260 }
3261
919ed24c
ILT
3262 // If there is no room to squeeze in the headers, then punt. The
3263 // resulting executable probably won't run on GNU/Linux, but we
3264 // trust that the user knows what they are doing.
3265 if (lma < subtract || vma < subtract)
3266 return NULL;
3267
a445fddf
ILT
3268 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
3269 elfcpp::PF_R);
919ed24c 3270 load_seg->set_addresses(vma - subtract, lma - subtract);
a445fddf
ILT
3271
3272 return load_seg;
3273}
3274
3275// Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
3276// segment if there are any SHT_TLS sections.
3277
3278void
3279Script_sections::create_note_and_tls_segments(
3280 Layout* layout,
3281 const Layout::Section_list* sections)
3282{
1c4f3631
ILT
3283 gold_assert(!this->saw_phdrs_clause());
3284
a445fddf
ILT
3285 bool saw_tls = false;
3286 for (Layout::Section_list::const_iterator p = sections->begin();
3287 p != sections->end();
3288 ++p)
3289 {
3290 if ((*p)->type() == elfcpp::SHT_NOTE)
3291 {
3292 elfcpp::Elf_Word seg_flags =
3293 Layout::section_flags_to_segment((*p)->flags());
3294 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
3295 seg_flags);
f5c870d2 3296 oseg->add_output_section(*p, seg_flags, false);
a445fddf
ILT
3297
3298 // Incorporate any subsequent SHT_NOTE sections, in the
3299 // hopes that the script is sensible.
3300 Layout::Section_list::const_iterator pnext = p + 1;
3301 while (pnext != sections->end()
3302 && (*pnext)->type() == elfcpp::SHT_NOTE)
3303 {
3304 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
f5c870d2 3305 oseg->add_output_section(*pnext, seg_flags, false);
a445fddf
ILT
3306 p = pnext;
3307 ++pnext;
3308 }
3309 }
3310
3311 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3312 {
3313 if (saw_tls)
3314 gold_error(_("TLS sections are not adjacent"));
3315
3316 elfcpp::Elf_Word seg_flags =
3317 Layout::section_flags_to_segment((*p)->flags());
3318 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
3319 seg_flags);
f5c870d2 3320 oseg->add_output_section(*p, seg_flags, false);
a445fddf
ILT
3321
3322 Layout::Section_list::const_iterator pnext = p + 1;
3323 while (pnext != sections->end()
3324 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
3325 {
3326 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
f5c870d2 3327 oseg->add_output_section(*pnext, seg_flags, false);
a445fddf
ILT
3328 p = pnext;
3329 ++pnext;
3330 }
3331
3332 saw_tls = true;
3333 }
3334 }
3335}
3336
1c4f3631
ILT
3337// Add a program header. The PHDRS clause is syntactically distinct
3338// from the SECTIONS clause, but we implement it with the SECTIONS
55458500 3339// support because PHDRS is useless if there is no SECTIONS clause.
1c4f3631
ILT
3340
3341void
3342Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
3343 bool includes_filehdr, bool includes_phdrs,
3344 bool is_flags_valid, unsigned int flags,
3345 Expression* load_address)
3346{
3347 if (this->phdrs_elements_ == NULL)
3348 this->phdrs_elements_ = new Phdrs_elements();
3349 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
3350 includes_filehdr,
3351 includes_phdrs,
3352 is_flags_valid, flags,
3353 load_address));
3354}
3355
3802b2dd
ILT
3356// Return the number of segments we expect to create based on the
3357// SECTIONS clause. This is used to implement SIZEOF_HEADERS.
3358
3359size_t
3360Script_sections::expected_segment_count(const Layout* layout) const
3361{
1c4f3631
ILT
3362 if (this->saw_phdrs_clause())
3363 return this->phdrs_elements_->size();
3364
3802b2dd
ILT
3365 Layout::Section_list sections;
3366 layout->get_allocated_sections(&sections);
3367
3368 // We assume that we will need two PT_LOAD segments.
3369 size_t ret = 2;
3370
3371 bool saw_note = false;
3372 bool saw_tls = false;
3373 for (Layout::Section_list::const_iterator p = sections.begin();
3374 p != sections.end();
3375 ++p)
3376 {
3377 if ((*p)->type() == elfcpp::SHT_NOTE)
3378 {
3379 // Assume that all note sections will fit into a single
3380 // PT_NOTE segment.
3381 if (!saw_note)
3382 {
3383 ++ret;
3384 saw_note = true;
3385 }
3386 }
3387 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
3388 {
3389 // There can only be one PT_TLS segment.
3390 if (!saw_tls)
3391 {
3392 ++ret;
3393 saw_tls = true;
3394 }
3395 }
3396 }
3397
3398 return ret;
3399}
3400
1c4f3631
ILT
3401// Create the segments from a PHDRS clause. Return the segment which
3402// should hold the file header and program headers, if any.
3403
3404Output_segment*
f6973bdc
ILT
3405Script_sections::create_segments_from_phdrs_clause(Layout* layout,
3406 uint64_t dot_alignment)
1c4f3631
ILT
3407{
3408 this->attach_sections_using_phdrs_clause(layout);
f6973bdc 3409 return this->set_phdrs_clause_addresses(layout, dot_alignment);
1c4f3631
ILT
3410}
3411
3412// Create the segments from the PHDRS clause, and put the output
3413// sections in them.
3414
3415void
3416Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
3417{
3418 typedef std::map<std::string, Output_segment*> Name_to_segment;
3419 Name_to_segment name_to_segment;
3420 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3421 p != this->phdrs_elements_->end();
3422 ++p)
3423 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
3424
3425 // Walk through the output sections and attach them to segments.
3426 // Output sections in the script which do not list segments are
3427 // attached to the same set of segments as the immediately preceding
3428 // output section.
20e6d0d6 3429
1c4f3631 3430 String_list* phdr_names = NULL;
20e6d0d6 3431 bool load_segments_only = false;
1c4f3631
ILT
3432 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3433 p != this->sections_elements_->end();
3434 ++p)
3435 {
2cefc357 3436 bool orphan;
20e6d0d6 3437 String_list* old_phdr_names = phdr_names;
2cefc357 3438 Output_section* os = (*p)->allocate_to_segment(&phdr_names, &orphan);
1c4f3631
ILT
3439 if (os == NULL)
3440 continue;
3441
3442 if (phdr_names == NULL)
3443 {
3444 gold_error(_("allocated section not in any segment"));
3445 continue;
3446 }
3447
20e6d0d6
DK
3448 // We see a list of segments names. Disable PT_LOAD segment only
3449 // filtering.
3450 if (old_phdr_names != phdr_names)
3451 load_segments_only = false;
3452
2cefc357
ILT
3453 // If this is an orphan section--one that was not explicitly
3454 // mentioned in the linker script--then it should not inherit
3455 // any segment type other than PT_LOAD. Otherwise, e.g., the
3456 // PT_INTERP segment will pick up following orphan sections,
3457 // which does not make sense. If this is not an orphan section,
3458 // we trust the linker script.
3459 if (orphan)
3460 {
20e6d0d6
DK
3461 // Enable PT_LOAD segments only filtering until we see another
3462 // list of segment names.
3463 load_segments_only = true;
2cefc357
ILT
3464 }
3465
1c4f3631
ILT
3466 bool in_load_segment = false;
3467 for (String_list::const_iterator q = phdr_names->begin();
3468 q != phdr_names->end();
3469 ++q)
3470 {
3471 Name_to_segment::const_iterator r = name_to_segment.find(*q);
3472 if (r == name_to_segment.end())
3473 gold_error(_("no segment %s"), q->c_str());
3474 else
3475 {
20e6d0d6
DK
3476 if (load_segments_only
3477 && r->second->type() != elfcpp::PT_LOAD)
3478 continue;
3479
1c4f3631
ILT
3480 elfcpp::Elf_Word seg_flags =
3481 Layout::section_flags_to_segment(os->flags());
f5c870d2 3482 r->second->add_output_section(os, seg_flags, false);
1c4f3631
ILT
3483
3484 if (r->second->type() == elfcpp::PT_LOAD)
3485 {
3486 if (in_load_segment)
3487 gold_error(_("section in two PT_LOAD segments"));
3488 in_load_segment = true;
3489 }
3490 }
3491 }
3492
3493 if (!in_load_segment)
3494 gold_error(_("allocated section not in any PT_LOAD segment"));
3495 }
3496}
3497
3498// Set the addresses for segments created from a PHDRS clause. Return
3499// the segment which should hold the file header and program headers,
3500// if any.
3501
3502Output_segment*
f6973bdc
ILT
3503Script_sections::set_phdrs_clause_addresses(Layout* layout,
3504 uint64_t dot_alignment)
1c4f3631
ILT
3505{
3506 Output_segment* load_seg = NULL;
3507 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3508 p != this->phdrs_elements_->end();
3509 ++p)
3510 {
3511 // Note that we have to set the flags after adding the output
3512 // sections to the segment, as adding an output segment can
3513 // change the flags.
3514 (*p)->set_flags_if_valid();
3515
3516 Output_segment* oseg = (*p)->segment();
3517
3518 if (oseg->type() != elfcpp::PT_LOAD)
3519 {
3520 // The addresses of non-PT_LOAD segments are set from the
3521 // PT_LOAD segments.
3522 if ((*p)->has_load_address())
3523 gold_error(_("may only specify load address for PT_LOAD segment"));
3524 continue;
3525 }
3526
f6973bdc
ILT
3527 oseg->set_minimum_p_align(dot_alignment);
3528
1c4f3631
ILT
3529 // The output sections should have addresses from the SECTIONS
3530 // clause. The addresses don't have to be in order, so find the
3531 // one with the lowest load address. Use that to set the
3532 // address of the segment.
3533
3534 Output_section* osec = oseg->section_with_lowest_load_address();
3535 if (osec == NULL)
3536 {
3537 oseg->set_addresses(0, 0);
3538 continue;
3539 }
3540
3541 uint64_t vma = osec->address();
3542 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
3543
3544 // Override the load address of the section with the load
3545 // address specified for the segment.
3546 if ((*p)->has_load_address())
3547 {
3548 if (osec->has_load_address())
3549 gold_warning(_("PHDRS load address overrides "
3550 "section %s load address"),
3551 osec->name());
3552
3553 lma = (*p)->load_address();
3554 }
3555
3556 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
3557 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
3558 {
3559 // We could support this if we wanted to.
3560 gold_error(_("using only one of FILEHDR and PHDRS is "
3561 "not currently supported"));
3562 }
3563 if (headers)
3564 {
3565 size_t sizeof_headers = this->total_header_size(layout);
3566 uint64_t subtract = this->header_size_adjustment(lma,
3567 sizeof_headers);
3568 if (lma >= subtract && vma >= subtract)
3569 {
3570 lma -= subtract;
3571 vma -= subtract;
3572 }
3573 else
3574 {
3575 gold_error(_("sections loaded on first page without room "
3576 "for file and program headers "
3577 "are not supported"));
3578 }
3579
3580 if (load_seg != NULL)
3581 gold_error(_("using FILEHDR and PHDRS on more than one "
3582 "PT_LOAD segment is not currently supported"));
3583 load_seg = oseg;
3584 }
3585
3586 oseg->set_addresses(vma, lma);
3587 }
3588
3589 return load_seg;
3590}
3591
3592// Add the file header and segment headers to non-load segments
3593// specified in the PHDRS clause.
3594
3595void
3596Script_sections::put_headers_in_phdrs(Output_data* file_header,
3597 Output_data* segment_headers)
3598{
3599 gold_assert(this->saw_phdrs_clause());
3600 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3601 p != this->phdrs_elements_->end();
3602 ++p)
3603 {
3604 if ((*p)->type() != elfcpp::PT_LOAD)
3605 {
3606 if ((*p)->includes_phdrs())
3607 (*p)->segment()->add_initial_output_data(segment_headers);
3608 if ((*p)->includes_filehdr())
3609 (*p)->segment()->add_initial_output_data(file_header);
3610 }
3611 }
3612}
3613
8f2eb564
ILT
3614// Look for an output section by name and return the address, the load
3615// address, the alignment, and the size. This is used when an
3616// expression refers to an output section which was not actually
3617// created. This returns true if the section was found, false
3618// otherwise.
3619
3620bool
3621Script_sections::get_output_section_info(const char* name, uint64_t* address,
3622 uint64_t* load_address,
2ea97941 3623 uint64_t* addralign,
8f2eb564
ILT
3624 uint64_t* size) const
3625{
3626 if (!this->saw_sections_clause_)
3627 return false;
3628 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3629 p != this->sections_elements_->end();
3630 ++p)
2ea97941 3631 if ((*p)->get_output_section_info(name, address, load_address, addralign,
8f2eb564
ILT
3632 size))
3633 return true;
3634 return false;
3635}
3636
20e6d0d6
DK
3637// Release all Output_segments. This remove all pointers to all
3638// Output_segments.
3639
3640void
3641Script_sections::release_segments()
3642{
3643 if (this->saw_phdrs_clause())
3644 {
3645 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3646 p != this->phdrs_elements_->end();
3647 ++p)
3648 (*p)->release_segment();
3649 }
3650}
3651
494e05f4
ILT
3652// Print the SECTIONS clause to F for debugging.
3653
3654void
3655Script_sections::print(FILE* f) const
3656{
3657 if (!this->saw_sections_clause_)
3658 return;
3659
3660 fprintf(f, "SECTIONS {\n");
3661
3662 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3663 p != this->sections_elements_->end();
3664 ++p)
3665 (*p)->print(f);
3666
3667 fprintf(f, "}\n");
7d26c6cc
ILT
3668
3669 if (this->phdrs_elements_ != NULL)
3670 {
3671 fprintf(f, "PHDRS {\n");
3672 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
3673 p != this->phdrs_elements_->end();
3674 ++p)
3675 (*p)->print(f);
3676 fprintf(f, "}\n");
3677 }
494e05f4
ILT
3678}
3679
3680} // End namespace gold.
This page took 0.397866 seconds and 4 git commands to generate.