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