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