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