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