binutils/testsuite/
[deliverable/binutils-gdb.git] / gold / script-sections.cc
CommitLineData
494e05f4
ILT
1// script-sections.cc -- linker script SECTIONS for gold
2
3// Copyright 2008 Free Software Foundation, Inc.
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
46// An element in a SECTIONS clause.
47
48class Sections_element
49{
50 public:
51 Sections_element()
52 { }
53
54 virtual ~Sections_element()
55 { }
56
a445fddf
ILT
57 // Add any symbol being defined to the symbol table.
58 virtual void
59 add_symbols_to_table(Symbol_table*)
60 { }
61
62 // Finalize symbols and check assertions.
63 virtual void
64 finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*)
65 { }
66
67 // Return the output section name to use for an input file name and
68 // section name. This only real implementation is in
69 // Output_section_definition.
70 virtual const char*
71 output_section_name(const char*, const char*, Output_section***)
72 { return NULL; }
73
74 // Return whether to place an orphan output section after this
75 // element.
76 virtual bool
77 place_orphan_here(const Output_section *, bool*) const
78 { return false; }
79
80 // Set section addresses. This includes applying assignments if the
81 // the expression is an absolute value.
82 virtual void
83 set_section_addresses(Symbol_table*, Layout*, bool*, uint64_t*)
84 { }
85
3802b2dd
ILT
86 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
87 // this section is constrained, and the input sections do not match,
88 // return the constraint, and set *POSD.
89 virtual Section_constraint
90 check_constraint(Output_section_definition**)
91 { return CONSTRAINT_NONE; }
92
93 // See if this is the alternate output section for a constrained
94 // output section. If it is, transfer the Output_section and return
95 // true. Otherwise return false.
96 virtual bool
97 alternate_constraint(Output_section_definition*, Section_constraint)
98 { return false; }
99
1c4f3631
ILT
100 // Get the list of segments to use for an allocated section when
101 // using a PHDRS clause. If this is an allocated section, return
102 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
103 // which it should be attached. If the PHDRS were not specified,
104 // don't change *PHDRS_LIST.
105 virtual Output_section*
106 allocate_to_segment(String_list**)
107 { return NULL; }
108
a445fddf 109 // Print the element for debugging purposes.
494e05f4
ILT
110 virtual void
111 print(FILE* f) const = 0;
112};
113
114// An assignment in a SECTIONS clause outside of an output section.
115
116class Sections_element_assignment : public Sections_element
117{
118 public:
119 Sections_element_assignment(const char* name, size_t namelen,
120 Expression* val, bool provide, bool hidden)
121 : assignment_(name, namelen, val, provide, hidden)
122 { }
123
a445fddf
ILT
124 // Add the symbol to the symbol table.
125 void
126 add_symbols_to_table(Symbol_table* symtab)
127 { this->assignment_.add_to_table(symtab); }
128
129 // Finalize the symbol.
130 void
131 finalize_symbols(Symbol_table* symtab, const Layout* layout,
132 bool* dot_has_value, uint64_t* dot_value)
133 {
134 this->assignment_.finalize_with_dot(symtab, layout, *dot_has_value,
135 *dot_value);
136 }
137
138 // Set the section address. There is no section here, but if the
139 // value is absolute, we set the symbol. This permits us to use
140 // absolute symbols when setting dot.
141 void
142 set_section_addresses(Symbol_table* symtab, Layout* layout,
143 bool* dot_has_value, uint64_t* dot_value)
144 {
145 this->assignment_.set_if_absolute(symtab, layout, true, *dot_has_value,
146 *dot_value);
147 }
148
149 // Print for debugging.
494e05f4
ILT
150 void
151 print(FILE* f) const
152 {
153 fprintf(f, " ");
154 this->assignment_.print(f);
155 }
156
157 private:
158 Symbol_assignment assignment_;
159};
160
a445fddf
ILT
161// An assignment to the dot symbol in a SECTIONS clause outside of an
162// output section.
163
164class Sections_element_dot_assignment : public Sections_element
165{
166 public:
167 Sections_element_dot_assignment(Expression* val)
168 : val_(val)
169 { }
170
171 // Finalize the symbol.
172 void
173 finalize_symbols(Symbol_table* symtab, const Layout* layout,
174 bool* dot_has_value, uint64_t* dot_value)
175 {
176 bool dummy;
177 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value,
178 *dot_value, &dummy);
179 *dot_has_value = true;
180 }
181
182 // Update the dot symbol while setting section addresses.
183 void
184 set_section_addresses(Symbol_table* symtab, Layout* layout,
185 bool* dot_has_value, uint64_t* dot_value)
186 {
187 bool is_absolute;
188 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value,
189 *dot_value, &is_absolute);
190 if (!is_absolute)
191 gold_error(_("dot set to non-absolute value"));
192 *dot_has_value = true;
193 }
194
195 // Print for debugging.
196 void
197 print(FILE* f) const
198 {
199 fprintf(f, " . = ");
200 this->val_->print(f);
201 fprintf(f, "\n");
202 }
203
204 private:
205 Expression* val_;
206};
207
494e05f4
ILT
208// An assertion in a SECTIONS clause outside of an output section.
209
210class Sections_element_assertion : public Sections_element
211{
212 public:
213 Sections_element_assertion(Expression* check, const char* message,
214 size_t messagelen)
215 : assertion_(check, message, messagelen)
216 { }
217
a445fddf
ILT
218 // Check the assertion.
219 void
220 finalize_symbols(Symbol_table* symtab, const Layout* layout, bool*,
221 uint64_t*)
222 { this->assertion_.check(symtab, layout); }
223
224 // Print for debugging.
494e05f4
ILT
225 void
226 print(FILE* f) const
227 {
228 fprintf(f, " ");
229 this->assertion_.print(f);
230 }
231
232 private:
233 Script_assertion assertion_;
234};
235
236// An element in an output section in a SECTIONS clause.
237
238class Output_section_element
239{
240 public:
a445fddf
ILT
241 // A list of input sections.
242 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
243
494e05f4
ILT
244 Output_section_element()
245 { }
246
247 virtual ~Output_section_element()
248 { }
249
a445fddf
ILT
250 // Add any symbol being defined to the symbol table.
251 virtual void
252 add_symbols_to_table(Symbol_table*)
253 { }
254
255 // Finalize symbols and check assertions.
256 virtual void
257 finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*)
258 { }
259
260 // Return whether this element matches FILE_NAME and SECTION_NAME.
261 // The only real implementation is in Output_section_element_input.
262 virtual bool
263 match_name(const char*, const char*) const
264 { return false; }
265
266 // Set section addresses. This includes applying assignments if the
267 // the expression is an absolute value.
268 virtual void
269 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
270 uint64_t*, std::string*, Input_section_list*)
271 { }
272
273 // Print the element for debugging purposes.
494e05f4
ILT
274 virtual void
275 print(FILE* f) const = 0;
a445fddf
ILT
276
277 protected:
278 // Return a fill string that is LENGTH bytes long, filling it with
279 // FILL.
280 std::string
281 get_fill_string(const std::string* fill, section_size_type length) const;
494e05f4
ILT
282};
283
a445fddf
ILT
284std::string
285Output_section_element::get_fill_string(const std::string* fill,
286 section_size_type length) const
287{
288 std::string this_fill;
289 this_fill.reserve(length);
290 while (this_fill.length() + fill->length() <= length)
291 this_fill += *fill;
292 if (this_fill.length() < length)
293 this_fill.append(*fill, 0, length - this_fill.length());
294 return this_fill;
295}
296
494e05f4
ILT
297// A symbol assignment in an output section.
298
299class Output_section_element_assignment : public Output_section_element
300{
301 public:
302 Output_section_element_assignment(const char* name, size_t namelen,
303 Expression* val, bool provide,
304 bool hidden)
305 : assignment_(name, namelen, val, provide, hidden)
306 { }
307
a445fddf
ILT
308 // Add the symbol to the symbol table.
309 void
310 add_symbols_to_table(Symbol_table* symtab)
311 { this->assignment_.add_to_table(symtab); }
312
313 // Finalize the symbol.
314 void
315 finalize_symbols(Symbol_table* symtab, const Layout* layout,
316 bool* dot_has_value, uint64_t* dot_value)
317 {
318 this->assignment_.finalize_with_dot(symtab, layout, *dot_has_value,
319 *dot_value);
320 }
321
322 // Set the section address. There is no section here, but if the
323 // value is absolute, we set the symbol. This permits us to use
324 // absolute symbols when setting dot.
325 void
326 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
327 uint64_t, uint64_t* dot_value, std::string*,
328 Input_section_list*)
329 {
330 this->assignment_.set_if_absolute(symtab, layout, true, true, *dot_value);
331 }
332
333 // Print for debugging.
494e05f4
ILT
334 void
335 print(FILE* f) const
336 {
337 fprintf(f, " ");
338 this->assignment_.print(f);
339 }
340
341 private:
342 Symbol_assignment assignment_;
343};
344
a445fddf
ILT
345// An assignment to the dot symbol in an output section.
346
347class Output_section_element_dot_assignment : public Output_section_element
348{
349 public:
350 Output_section_element_dot_assignment(Expression* val)
351 : val_(val)
352 { }
353
354 // Finalize the symbol.
355 void
356 finalize_symbols(Symbol_table* symtab, const Layout* layout,
357 bool* dot_has_value, uint64_t* dot_value)
358 {
359 bool dummy;
360 *dot_value = this->val_->eval_with_dot(symtab, layout, *dot_has_value,
361 *dot_value, &dummy);
362 *dot_has_value = true;
363 }
364
365 // Update the dot symbol while setting section addresses.
366 void
367 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
368 uint64_t, uint64_t* dot_value, std::string*,
369 Input_section_list*);
370
371 // Print for debugging.
372 void
373 print(FILE* f) const
374 {
375 fprintf(f, " . = ");
376 this->val_->print(f);
377 fprintf(f, "\n");
378 }
379
380 private:
381 Expression* val_;
382};
383
384// Update the dot symbol while setting section addresses.
385
386void
387Output_section_element_dot_assignment::set_section_addresses(
388 Symbol_table* symtab,
389 Layout* layout,
390 Output_section* output_section,
391 uint64_t,
392 uint64_t* dot_value,
393 std::string* fill,
394 Input_section_list*)
395{
396 bool is_absolute;
397 uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, true,
398 *dot_value, &is_absolute);
399 if (!is_absolute)
400 gold_error(_("dot set to non-absolute value"));
401 if (next_dot < *dot_value)
402 gold_error(_("dot may not move backward"));
403 if (next_dot > *dot_value && output_section != NULL)
404 {
405 section_size_type length = convert_to_section_size_type(next_dot
406 - *dot_value);
407 Output_section_data* posd;
408 if (fill->empty())
409 posd = new Output_data_fixed_space(length, 0);
410 else
411 {
412 std::string this_fill = this->get_fill_string(fill, length);
413 posd = new Output_data_const(this_fill, 0);
414 }
415 output_section->add_output_section_data(posd);
416 }
417 *dot_value = next_dot;
418}
419
494e05f4
ILT
420// An assertion in an output section.
421
422class Output_section_element_assertion : public Output_section_element
423{
424 public:
425 Output_section_element_assertion(Expression* check, const char* message,
426 size_t messagelen)
427 : assertion_(check, message, messagelen)
428 { }
429
430 void
431 print(FILE* f) const
432 {
433 fprintf(f, " ");
434 this->assertion_.print(f);
435 }
436
437 private:
438 Script_assertion assertion_;
439};
440
441// A data item in an output section.
442
443class Output_section_element_data : public Output_section_element
444{
445 public:
446 Output_section_element_data(int size, bool is_signed, Expression* val)
447 : size_(size), is_signed_(is_signed), val_(val)
448 { }
449
a445fddf
ILT
450 // Finalize symbols--we just need to update dot.
451 void
452 finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t* dot_value)
453 { *dot_value += this->size_; }
454
455 // Store the value in the section.
456 void
457 set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
458 uint64_t* dot_value, std::string*,
459 Input_section_list*);
460
461 // Print for debugging.
494e05f4
ILT
462 void
463 print(FILE*) const;
464
465 private:
a445fddf
ILT
466 template<bool big_endian>
467 std::string
468 set_fill_string(uint64_t);
469
494e05f4
ILT
470 // The size in bytes.
471 int size_;
472 // Whether the value is signed.
473 bool is_signed_;
474 // The value.
475 Expression* val_;
476};
477
a445fddf
ILT
478// Store the value in the section.
479
480void
481Output_section_element_data::set_section_addresses(Symbol_table* symtab,
482 Layout* layout,
483 Output_section* os,
484 uint64_t,
485 uint64_t* dot_value,
486 std::string*,
487 Input_section_list*)
488{
489 gold_assert(os != NULL);
490
491 bool is_absolute;
492 uint64_t val = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
493 &is_absolute);
494 if (!is_absolute)
495 gold_error(_("data directive with non-absolute value"));
496
497 std::string fill;
498 if (parameters->is_big_endian())
499 fill = this->set_fill_string<true>(val);
500 else
501 fill = this->set_fill_string<false>(val);
502
503 os->add_output_section_data(new Output_data_const(fill, 0));
504
505 *dot_value += this->size_;
506}
507
508// Get the value to store in a std::string.
509
510template<bool big_endian>
511std::string
512 Output_section_element_data::set_fill_string(uint64_t val)
513{
514 std::string ret;
515 unsigned char buf[8];
516 switch (this->size_)
517 {
518 case 1:
519 elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
520 ret.assign(reinterpret_cast<char*>(buf), 1);
521 break;
522 case 2:
523 elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
524 ret.assign(reinterpret_cast<char*>(buf), 2);
525 break;
526 case 4:
527 elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
528 ret.assign(reinterpret_cast<char*>(buf), 4);
529 break;
530 case 8:
531 if (parameters->get_size() == 32)
532 {
533 val &= 0xffffffff;
534 if (this->is_signed_ && (val & 0x80000000) != 0)
535 val |= 0xffffffff00000000LL;
536 }
537 elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
538 ret.assign(reinterpret_cast<char*>(buf), 8);
539 break;
540 default:
541 gold_unreachable();
542 }
543 return ret;
544}
545
494e05f4
ILT
546// Print for debugging.
547
548void
549Output_section_element_data::print(FILE* f) const
550{
551 const char* s;
552 switch (this->size_)
553 {
554 case 1:
555 s = "BYTE";
556 break;
557 case 2:
558 s = "SHORT";
559 break;
560 case 4:
561 s = "LONG";
562 break;
563 case 8:
564 if (this->is_signed_)
565 s = "SQUAD";
566 else
567 s = "QUAD";
568 break;
569 default:
570 gold_unreachable();
571 }
572 fprintf(f, " %s(", s);
573 this->val_->print(f);
574 fprintf(f, ")\n");
575}
576
577// A fill value setting in an output section.
578
579class Output_section_element_fill : public Output_section_element
580{
581 public:
582 Output_section_element_fill(Expression* val)
583 : val_(val)
584 { }
585
a445fddf
ILT
586 // Update the fill value while setting section addresses.
587 void
588 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
589 uint64_t, uint64_t* dot_value, std::string* fill,
590 Input_section_list*)
591 {
592 bool is_absolute;
593 uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, true,
594 *dot_value,
595 &is_absolute);
596 if (!is_absolute)
597 gold_error(_("fill set to non-absolute value"));
598 // FIXME: The GNU linker supports fill values of arbitrary length.
599 unsigned char fill_buff[4];
600 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
601 fill->assign(reinterpret_cast<char*>(fill_buff), 4);
602 }
603
604 // Print for debugging.
494e05f4
ILT
605 void
606 print(FILE* f) const
607 {
608 fprintf(f, " FILL(");
609 this->val_->print(f);
610 fprintf(f, ")\n");
611 }
612
613 private:
614 // The new fill value.
615 Expression* val_;
616};
617
a445fddf
ILT
618// Return whether STRING contains a wildcard character. This is used
619// to speed up matching.
620
621static inline bool
622is_wildcard_string(const std::string& s)
623{
624 return strpbrk(s.c_str(), "?*[") != NULL;
625}
626
494e05f4
ILT
627// An input section specification in an output section
628
629class Output_section_element_input : public Output_section_element
630{
631 public:
494e05f4
ILT
632 Output_section_element_input(const Input_section_spec* spec, bool keep);
633
a445fddf
ILT
634 // Finalize symbols--just update the value of the dot symbol.
635 void
636 finalize_symbols(Symbol_table*, const Layout*, bool* dot_has_value,
637 uint64_t* dot_value)
638 {
639 *dot_value = this->final_dot_value_;
640 *dot_has_value = true;
641 }
642
643 // See whether we match FILE_NAME and SECTION_NAME as an input
644 // section.
645 bool
646 match_name(const char* file_name, const char* section_name) const;
647
648 // Set the section address.
649 void
650 set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
651 uint64_t subalign, uint64_t* dot_value,
652 std::string* fill, Input_section_list*);
653
654 // Print for debugging.
494e05f4
ILT
655 void
656 print(FILE* f) const;
657
658 private:
659 // An input section pattern.
660 struct Input_section_pattern
661 {
662 std::string pattern;
a445fddf 663 bool pattern_is_wildcard;
494e05f4
ILT
664 Sort_wildcard sort;
665
666 Input_section_pattern(const char* patterna, size_t patternlena,
667 Sort_wildcard sorta)
a445fddf
ILT
668 : pattern(patterna, patternlena),
669 pattern_is_wildcard(is_wildcard_string(this->pattern)),
670 sort(sorta)
494e05f4
ILT
671 { }
672 };
673
674 typedef std::vector<Input_section_pattern> Input_section_patterns;
675
a445fddf
ILT
676 // Filename_exclusions is a pair of filename pattern and a bool
677 // indicating whether the filename is a wildcard.
678 typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
679
680 // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
681 // indicates whether this is a wildcard pattern.
682 static inline bool
683 match(const char* string, const char* pattern, bool is_wildcard_pattern)
684 {
685 return (is_wildcard_pattern
686 ? fnmatch(pattern, string, 0) == 0
687 : strcmp(string, pattern) == 0);
688 }
494e05f4 689
a445fddf
ILT
690 // See if we match a file name.
691 bool
692 match_file_name(const char* file_name) const;
693
694 // The file name pattern. If this is the empty string, we match all
695 // files.
494e05f4 696 std::string filename_pattern_;
a445fddf
ILT
697 // Whether the file name pattern is a wildcard.
698 bool filename_is_wildcard_;
494e05f4
ILT
699 // How the file names should be sorted. This may only be
700 // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
701 Sort_wildcard filename_sort_;
702 // The list of file names to exclude.
703 Filename_exclusions filename_exclusions_;
704 // The list of input section patterns.
705 Input_section_patterns input_section_patterns_;
706 // Whether to keep this section when garbage collecting.
707 bool keep_;
a445fddf
ILT
708 // The value of dot after including all matching sections.
709 uint64_t final_dot_value_;
494e05f4
ILT
710};
711
712// Construct Output_section_element_input. The parser records strings
713// as pointers into a copy of the script file, which will go away when
714// parsing is complete. We make sure they are in std::string objects.
715
716Output_section_element_input::Output_section_element_input(
717 const Input_section_spec* spec,
718 bool keep)
a445fddf
ILT
719 : filename_pattern_(),
720 filename_is_wildcard_(false),
494e05f4
ILT
721 filename_sort_(spec->file.sort),
722 filename_exclusions_(),
723 input_section_patterns_(),
a445fddf
ILT
724 keep_(keep),
725 final_dot_value_(0)
494e05f4 726{
a445fddf
ILT
727 // The filename pattern "*" is common, and matches all files. Turn
728 // it into the empty string.
729 if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
730 this->filename_pattern_.assign(spec->file.name.value,
731 spec->file.name.length);
732 this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_);
733
494e05f4
ILT
734 if (spec->input_sections.exclude != NULL)
735 {
736 for (String_list::const_iterator p =
737 spec->input_sections.exclude->begin();
738 p != spec->input_sections.exclude->end();
739 ++p)
a445fddf
ILT
740 {
741 bool is_wildcard = is_wildcard_string(*p);
742 this->filename_exclusions_.push_back(std::make_pair(*p,
743 is_wildcard));
744 }
494e05f4
ILT
745 }
746
747 if (spec->input_sections.sections != NULL)
748 {
749 Input_section_patterns& isp(this->input_section_patterns_);
750 for (String_sort_list::const_iterator p =
751 spec->input_sections.sections->begin();
752 p != spec->input_sections.sections->end();
753 ++p)
754 isp.push_back(Input_section_pattern(p->name.value, p->name.length,
755 p->sort));
756 }
757}
758
a445fddf
ILT
759// See whether we match FILE_NAME.
760
761bool
762Output_section_element_input::match_file_name(const char* file_name) const
763{
764 if (!this->filename_pattern_.empty())
765 {
766 // If we were called with no filename, we refuse to match a
767 // pattern which requires a file name.
768 if (file_name == NULL)
769 return false;
770
771 if (!match(file_name, this->filename_pattern_.c_str(),
772 this->filename_is_wildcard_))
773 return false;
774 }
775
776 if (file_name != NULL)
777 {
778 // Now we have to see whether FILE_NAME matches one of the
779 // exclusion patterns, if any.
780 for (Filename_exclusions::const_iterator p =
781 this->filename_exclusions_.begin();
782 p != this->filename_exclusions_.end();
783 ++p)
784 {
785 if (match(file_name, p->first.c_str(), p->second))
786 return false;
787 }
788 }
789
790 return true;
791}
792
793// See whether we match FILE_NAME and SECTION_NAME.
794
795bool
796Output_section_element_input::match_name(const char* file_name,
797 const char* section_name) const
798{
799 if (!this->match_file_name(file_name))
800 return false;
801
802 // If there are no section name patterns, then we match.
803 if (this->input_section_patterns_.empty())
804 return true;
805
806 // See whether we match the section name patterns.
807 for (Input_section_patterns::const_iterator p =
808 this->input_section_patterns_.begin();
809 p != this->input_section_patterns_.end();
810 ++p)
811 {
812 if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
813 return true;
814 }
815
816 // We didn't match any section names, so we didn't match.
817 return false;
818}
819
820// Information we use to sort the input sections.
821
822struct Input_section_info
823{
824 Relobj* relobj;
825 unsigned int shndx;
826 std::string section_name;
827 uint64_t size;
828 uint64_t addralign;
829};
830
831// A class to sort the input sections.
832
833class Input_section_sorter
834{
835 public:
836 Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
837 : filename_sort_(filename_sort), section_sort_(section_sort)
838 { }
839
840 bool
841 operator()(const Input_section_info&, const Input_section_info&) const;
842
843 private:
844 Sort_wildcard filename_sort_;
845 Sort_wildcard section_sort_;
846};
847
848bool
849Input_section_sorter::operator()(const Input_section_info& isi1,
850 const Input_section_info& isi2) const
851{
852 if (this->section_sort_ == SORT_WILDCARD_BY_NAME
853 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
854 || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
855 && isi1.addralign == isi2.addralign))
856 {
857 if (isi1.section_name != isi2.section_name)
858 return isi1.section_name < isi2.section_name;
859 }
860 if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
861 || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
862 || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
863 {
864 if (isi1.addralign != isi2.addralign)
865 return isi1.addralign < isi2.addralign;
866 }
867 if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
868 {
869 if (isi1.relobj->name() != isi2.relobj->name())
870 return isi1.relobj->name() < isi2.relobj->name();
871 }
872
873 // Otherwise we leave them in the same order.
874 return false;
875}
876
877// Set the section address. Look in INPUT_SECTIONS for sections which
878// match this spec, sort them as specified, and add them to the output
879// section.
880
881void
882Output_section_element_input::set_section_addresses(
883 Symbol_table*,
884 Layout*,
885 Output_section* output_section,
886 uint64_t subalign,
887 uint64_t* dot_value,
888 std::string* fill,
889 Input_section_list* input_sections)
890{
891 // We build a list of sections which match each
892 // Input_section_pattern.
893
894 typedef std::vector<std::vector<Input_section_info> > Matching_sections;
895 size_t input_pattern_count = this->input_section_patterns_.size();
896 if (input_pattern_count == 0)
897 input_pattern_count = 1;
898 Matching_sections matching_sections(input_pattern_count);
899
900 // Look through the list of sections for this output section. Add
901 // each one which matches to one of the elements of
902 // MATCHING_SECTIONS.
903
904 Input_section_list::iterator p = input_sections->begin();
905 while (p != input_sections->end())
906 {
907 // Calling section_name and section_addralign is not very
908 // efficient.
909 Input_section_info isi;
910 isi.relobj = p->first;
911 isi.shndx = p->second;
912
913 // Lock the object so that we can get information about the
914 // section. This is OK since we know we are single-threaded
915 // here.
916 {
917 const Task* task = reinterpret_cast<const Task*>(-1);
918 Task_lock_obj<Object> tl(task, p->first);
919
920 isi.section_name = p->first->section_name(p->second);
921 isi.size = p->first->section_size(p->second);
922 isi.addralign = p->first->section_addralign(p->second);
923 }
924
925 if (!this->match_file_name(isi.relobj->name().c_str()))
926 ++p;
927 else if (this->input_section_patterns_.empty())
928 {
929 matching_sections[0].push_back(isi);
930 p = input_sections->erase(p);
931 }
932 else
933 {
934 size_t i;
935 for (i = 0; i < input_pattern_count; ++i)
936 {
937 const Input_section_pattern&
938 isp(this->input_section_patterns_[i]);
939 if (match(isi.section_name.c_str(), isp.pattern.c_str(),
940 isp.pattern_is_wildcard))
941 break;
942 }
943
944 if (i >= this->input_section_patterns_.size())
945 ++p;
946 else
947 {
948 matching_sections[i].push_back(isi);
949 p = input_sections->erase(p);
950 }
951 }
952 }
953
954 // Look through MATCHING_SECTIONS. Sort each one as specified,
955 // using a stable sort so that we get the default order when
956 // sections are otherwise equal. Add each input section to the
957 // output section.
958
959 for (size_t i = 0; i < input_pattern_count; ++i)
960 {
961 if (matching_sections[i].empty())
962 continue;
963
964 gold_assert(output_section != NULL);
965
966 const Input_section_pattern& isp(this->input_section_patterns_[i]);
967 if (isp.sort != SORT_WILDCARD_NONE
968 || this->filename_sort_ != SORT_WILDCARD_NONE)
969 std::stable_sort(matching_sections[i].begin(),
970 matching_sections[i].end(),
971 Input_section_sorter(this->filename_sort_,
972 isp.sort));
973
974 for (std::vector<Input_section_info>::const_iterator p =
975 matching_sections[i].begin();
976 p != matching_sections[i].end();
977 ++p)
978 {
979 uint64_t this_subalign = p->addralign;
980 if (this_subalign < subalign)
981 this_subalign = subalign;
982
983 uint64_t address = align_address(*dot_value, this_subalign);
984
985 if (address > *dot_value && !fill->empty())
986 {
987 section_size_type length =
988 convert_to_section_size_type(address - *dot_value);
989 std::string this_fill = this->get_fill_string(fill, length);
990 Output_section_data* posd = new Output_data_const(this_fill, 0);
991 output_section->add_output_section_data(posd);
992 }
993
994 output_section->add_input_section_for_script(p->relobj,
995 p->shndx,
996 p->size,
997 this_subalign);
998
999 *dot_value = address + p->size;
1000 }
1001 }
1002
1003 this->final_dot_value_ = *dot_value;
1004}
1005
494e05f4
ILT
1006// Print for debugging.
1007
1008void
1009Output_section_element_input::print(FILE* f) const
1010{
1011 fprintf(f, " ");
1012
1013 if (this->keep_)
1014 fprintf(f, "KEEP(");
1015
1016 if (!this->filename_pattern_.empty())
1017 {
1018 bool need_close_paren = false;
1019 switch (this->filename_sort_)
1020 {
1021 case SORT_WILDCARD_NONE:
1022 break;
1023 case SORT_WILDCARD_BY_NAME:
1024 fprintf(f, "SORT_BY_NAME(");
1025 need_close_paren = true;
1026 break;
1027 default:
1028 gold_unreachable();
1029 }
1030
1031 fprintf(f, "%s", this->filename_pattern_.c_str());
1032
1033 if (need_close_paren)
1034 fprintf(f, ")");
1035 }
1036
1037 if (!this->input_section_patterns_.empty()
1038 || !this->filename_exclusions_.empty())
1039 {
1040 fprintf(f, "(");
1041
1042 bool need_space = false;
1043 if (!this->filename_exclusions_.empty())
1044 {
1045 fprintf(f, "EXCLUDE_FILE(");
1046 bool need_comma = false;
1047 for (Filename_exclusions::const_iterator p =
1048 this->filename_exclusions_.begin();
1049 p != this->filename_exclusions_.end();
1050 ++p)
1051 {
1052 if (need_comma)
1053 fprintf(f, ", ");
a445fddf 1054 fprintf(f, "%s", p->first.c_str());
494e05f4
ILT
1055 need_comma = true;
1056 }
1057 fprintf(f, ")");
1058 need_space = true;
1059 }
1060
1061 for (Input_section_patterns::const_iterator p =
1062 this->input_section_patterns_.begin();
1063 p != this->input_section_patterns_.end();
1064 ++p)
1065 {
1066 if (need_space)
1067 fprintf(f, " ");
1068
1069 int close_parens = 0;
1070 switch (p->sort)
1071 {
1072 case SORT_WILDCARD_NONE:
1073 break;
1074 case SORT_WILDCARD_BY_NAME:
1075 fprintf(f, "SORT_BY_NAME(");
1076 close_parens = 1;
1077 break;
1078 case SORT_WILDCARD_BY_ALIGNMENT:
1079 fprintf(f, "SORT_BY_ALIGNMENT(");
1080 close_parens = 1;
1081 break;
1082 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1083 fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1084 close_parens = 2;
1085 break;
1086 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1087 fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1088 close_parens = 2;
1089 break;
1090 default:
1091 gold_unreachable();
1092 }
1093
1094 fprintf(f, "%s", p->pattern.c_str());
1095
1096 for (int i = 0; i < close_parens; ++i)
1097 fprintf(f, ")");
1098
1099 need_space = true;
1100 }
1101
1102 fprintf(f, ")");
1103 }
1104
1105 if (this->keep_)
1106 fprintf(f, ")");
1107
1108 fprintf(f, "\n");
1109}
1110
1111// An output section.
1112
1113class Output_section_definition : public Sections_element
1114{
1115 public:
a445fddf
ILT
1116 typedef Output_section_element::Input_section_list Input_section_list;
1117
494e05f4
ILT
1118 Output_section_definition(const char* name, size_t namelen,
1119 const Parser_output_section_header* header);
1120
1121 // Finish the output section with the information in the trailer.
1122 void
1123 finish(const Parser_output_section_trailer* trailer);
1124
1125 // Add a symbol to be defined.
1126 void
1127 add_symbol_assignment(const char* name, size_t length, Expression* value,
1128 bool provide, bool hidden);
a445fddf
ILT
1129
1130 // Add an assignment to the special dot symbol.
1131 void
1132 add_dot_assignment(Expression* value);
1133
494e05f4
ILT
1134 // Add an assertion.
1135 void
1136 add_assertion(Expression* check, const char* message, size_t messagelen);
1137
1138 // Add a data item to the current output section.
1139 void
1140 add_data(int size, bool is_signed, Expression* val);
1141
1142 // Add a setting for the fill value.
1143 void
1144 add_fill(Expression* val);
1145
1146 // Add an input section specification.
1147 void
1148 add_input_section(const Input_section_spec* spec, bool keep);
1149
a445fddf
ILT
1150 // Add any symbols being defined to the symbol table.
1151 void
1152 add_symbols_to_table(Symbol_table* symtab);
1153
1154 // Finalize symbols and check assertions.
1155 void
1156 finalize_symbols(Symbol_table*, const Layout*, bool*, uint64_t*);
1157
1158 // Return the output section name to use for an input file name and
1159 // section name.
1160 const char*
1161 output_section_name(const char* file_name, const char* section_name,
1162 Output_section***);
1163
1164 // Return whether to place an orphan section after this one.
1165 bool
1166 place_orphan_here(const Output_section *os, bool* exact) const;
1167
1168 // Set the section address.
1169 void
1170 set_section_addresses(Symbol_table* symtab, Layout* layout,
1171 bool* dot_has_value, uint64_t* dot_value);
1172
3802b2dd
ILT
1173 // Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1174 // this section is constrained, and the input sections do not match,
1175 // return the constraint, and set *POSD.
1176 Section_constraint
1177 check_constraint(Output_section_definition** posd);
1178
1179 // See if this is the alternate output section for a constrained
1180 // output section. If it is, transfer the Output_section and return
1181 // true. Otherwise return false.
1182 bool
1183 alternate_constraint(Output_section_definition*, Section_constraint);
1184
1c4f3631
ILT
1185 // Get the list of segments to use for an allocated section when
1186 // using a PHDRS clause. If this is an allocated section, return
1187 // the Output_section, and set *PHDRS_LIST to the list of PHDRS to
1188 // which it should be attached. If the PHDRS were not specified,
1189 // don't change *PHDRS_LIST.
1190 Output_section*
1191 allocate_to_segment(String_list** phdrs_list);
1192
494e05f4
ILT
1193 // Print the contents to the FILE. This is for debugging.
1194 void
1195 print(FILE*) const;
1196
1197 private:
1198 typedef std::vector<Output_section_element*> Output_section_elements;
1199
1200 // The output section name.
1201 std::string name_;
1202 // The address. This may be NULL.
1203 Expression* address_;
1204 // The load address. This may be NULL.
1205 Expression* load_address_;
1206 // The alignment. This may be NULL.
1207 Expression* align_;
1208 // The input section alignment. This may be NULL.
1209 Expression* subalign_;
3802b2dd
ILT
1210 // The constraint, if any.
1211 Section_constraint constraint_;
494e05f4
ILT
1212 // The fill value. This may be NULL.
1213 Expression* fill_;
1c4f3631
ILT
1214 // The list of segments this section should go into. This may be
1215 // NULL.
1216 String_list* phdrs_;
494e05f4
ILT
1217 // The list of elements defining the section.
1218 Output_section_elements elements_;
a445fddf
ILT
1219 // The Output_section created for this definition. This will be
1220 // NULL if none was created.
1221 Output_section* output_section_;
494e05f4
ILT
1222};
1223
1224// Constructor.
1225
1226Output_section_definition::Output_section_definition(
1227 const char* name,
1228 size_t namelen,
1229 const Parser_output_section_header* header)
1230 : name_(name, namelen),
1231 address_(header->address),
1232 load_address_(header->load_address),
1233 align_(header->align),
1234 subalign_(header->subalign),
3802b2dd 1235 constraint_(header->constraint),
494e05f4 1236 fill_(NULL),
1c4f3631 1237 phdrs_(NULL),
a445fddf
ILT
1238 elements_(),
1239 output_section_(NULL)
494e05f4
ILT
1240{
1241}
1242
1243// Finish an output section.
1244
1245void
1246Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1247{
1248 this->fill_ = trailer->fill;
1c4f3631 1249 this->phdrs_ = trailer->phdrs;
494e05f4
ILT
1250}
1251
1252// Add a symbol to be defined.
1253
1254void
1255Output_section_definition::add_symbol_assignment(const char* name,
1256 size_t length,
1257 Expression* value,
1258 bool provide,
1259 bool hidden)
1260{
1261 Output_section_element* p = new Output_section_element_assignment(name,
1262 length,
1263 value,
1264 provide,
1265 hidden);
1266 this->elements_.push_back(p);
1267}
1268
a445fddf 1269// Add an assignment to the special dot symbol.
494e05f4
ILT
1270
1271void
a445fddf
ILT
1272Output_section_definition::add_dot_assignment(Expression* value)
1273{
1274 Output_section_element* p = new Output_section_element_dot_assignment(value);
1275 this->elements_.push_back(p);
1276}
1277
1278// Add an assertion.
1279
1280void
1281Output_section_definition::add_assertion(Expression* check,
1282 const char* message,
494e05f4
ILT
1283 size_t messagelen)
1284{
1285 Output_section_element* p = new Output_section_element_assertion(check,
1286 message,
1287 messagelen);
1288 this->elements_.push_back(p);
1289}
1290
1291// Add a data item to the current output section.
1292
1293void
1294Output_section_definition::add_data(int size, bool is_signed, Expression* val)
1295{
1296 Output_section_element* p = new Output_section_element_data(size, is_signed,
1297 val);
1298 this->elements_.push_back(p);
1299}
1300
1301// Add a setting for the fill value.
1302
1303void
1304Output_section_definition::add_fill(Expression* val)
1305{
1306 Output_section_element* p = new Output_section_element_fill(val);
1307 this->elements_.push_back(p);
1308}
1309
1310// Add an input section specification.
1311
1312void
1313Output_section_definition::add_input_section(const Input_section_spec* spec,
1314 bool keep)
1315{
1316 Output_section_element* p = new Output_section_element_input(spec, keep);
1317 this->elements_.push_back(p);
1318}
1319
a445fddf
ILT
1320// Add any symbols being defined to the symbol table.
1321
1322void
1323Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
1324{
1325 for (Output_section_elements::iterator p = this->elements_.begin();
1326 p != this->elements_.end();
1327 ++p)
1328 (*p)->add_symbols_to_table(symtab);
1329}
1330
1331// Finalize symbols and check assertions.
1332
1333void
1334Output_section_definition::finalize_symbols(Symbol_table* symtab,
1335 const Layout* layout,
1336 bool* dot_has_value,
1337 uint64_t* dot_value)
1338{
1339 if (this->output_section_ != NULL)
1340 *dot_value = this->output_section_->address();
1341 else
1342 {
1343 uint64_t address = *dot_value;
1344 if (this->address_ != NULL)
1345 {
1346 bool dummy;
1347 address = this->address_->eval_with_dot(symtab, layout,
1348 *dot_has_value, *dot_value,
1349 &dummy);
1350 }
1351 if (this->align_ != NULL)
1352 {
1353 bool dummy;
1354 uint64_t align = this->align_->eval_with_dot(symtab, layout,
1355 *dot_has_value,
1356 *dot_value,
1357 &dummy);
1358 address = align_address(address, align);
1359 }
1360 *dot_value = address;
1361 }
1362 *dot_has_value = true;
1363
1364 for (Output_section_elements::iterator p = this->elements_.begin();
1365 p != this->elements_.end();
1366 ++p)
1367 (*p)->finalize_symbols(symtab, layout, dot_has_value, dot_value);
1368}
1369
1370// Return the output section name to use for an input section name.
1371
1372const char*
1373Output_section_definition::output_section_name(const char* file_name,
1374 const char* section_name,
1375 Output_section*** slot)
1376{
1377 // Ask each element whether it matches NAME.
1378 for (Output_section_elements::const_iterator p = this->elements_.begin();
1379 p != this->elements_.end();
1380 ++p)
1381 {
1382 if ((*p)->match_name(file_name, section_name))
1383 {
1384 // We found a match for NAME, which means that it should go
1385 // into this output section.
1386 *slot = &this->output_section_;
1387 return this->name_.c_str();
1388 }
1389 }
1390
1391 // We don't know about this section name.
1392 return NULL;
1393}
1394
1395// Return whether to place an orphan output section after this
1396// section.
1397
1398bool
1399Output_section_definition::place_orphan_here(const Output_section *os,
1400 bool* exact) const
1401{
1402 // Check for the simple case first.
1403 if (this->output_section_ != NULL
1404 && this->output_section_->type() == os->type()
1405 && this->output_section_->flags() == os->flags())
1406 {
1407 *exact = true;
1408 return true;
1409 }
1410
1411 // Otherwise use some heuristics.
1412
1413 if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
1414 return false;
1415
1416 if (os->type() == elfcpp::SHT_NOBITS)
1417 {
1c4f3631
ILT
1418 if (this->name_ == ".bss")
1419 {
1420 *exact = true;
1421 return true;
1422 }
a445fddf
ILT
1423 if (this->output_section_ != NULL
1424 && this->output_section_->type() == elfcpp::SHT_NOBITS)
1425 return true;
a445fddf
ILT
1426 }
1427 else if (os->type() == elfcpp::SHT_NOTE)
1428 {
1429 if (this->output_section_ != NULL
1430 && this->output_section_->type() == elfcpp::SHT_NOTE)
1c4f3631
ILT
1431 {
1432 *exact = true;
1433 return true;
1434 }
1435 if (this->name_.compare(0, 5, ".note") == 0)
1436 {
1437 *exact = true;
1438 return true;
1439 }
1440 if (this->name_ == ".interp")
a445fddf 1441 return true;
1c4f3631
ILT
1442 if (this->output_section_ != NULL
1443 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1444 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
a445fddf
ILT
1445 return true;
1446 }
1447 else if (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
1448 {
1c4f3631
ILT
1449 if (this->name_.compare(0, 4, ".rel") == 0)
1450 {
1451 *exact = true;
1452 return true;
1453 }
a445fddf
ILT
1454 if (this->output_section_ != NULL
1455 && (this->output_section_->type() == elfcpp::SHT_REL
1456 || this->output_section_->type() == elfcpp::SHT_RELA))
1c4f3631
ILT
1457 {
1458 *exact = true;
1459 return true;
1460 }
1461 if (this->output_section_ != NULL
1462 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1463 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
a445fddf
ILT
1464 return true;
1465 }
1466 else if (os->type() == elfcpp::SHT_PROGBITS
1467 && (os->flags() & elfcpp::SHF_WRITE) != 0)
1468 {
1c4f3631
ILT
1469 if (this->name_ == ".data")
1470 {
1471 *exact = true;
1472 return true;
1473 }
a445fddf
ILT
1474 if (this->output_section_ != NULL
1475 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1476 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
1477 return true;
a445fddf
ILT
1478 }
1479 else if (os->type() == elfcpp::SHT_PROGBITS
1480 && (os->flags() & elfcpp::SHF_EXECINSTR) != 0)
1481 {
1c4f3631
ILT
1482 if (this->name_ == ".text")
1483 {
1484 *exact = true;
1485 return true;
1486 }
a445fddf
ILT
1487 if (this->output_section_ != NULL
1488 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1489 && (this->output_section_->flags() & elfcpp::SHF_EXECINSTR) != 0)
1490 return true;
a445fddf 1491 }
1c4f3631
ILT
1492 else if (os->type() == elfcpp::SHT_PROGBITS
1493 || (os->type() != elfcpp::SHT_PROGBITS
1494 && (os->flags() & elfcpp::SHF_WRITE) == 0))
a445fddf 1495 {
1c4f3631
ILT
1496 if (this->name_ == ".rodata")
1497 {
1498 *exact = true;
1499 return true;
1500 }
a445fddf
ILT
1501 if (this->output_section_ != NULL
1502 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1c4f3631 1503 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
a445fddf
ILT
1504 return true;
1505 }
1506
1507 return false;
1508}
1509
1510// Set the section address. Note that the OUTPUT_SECTION_ field will
1511// be NULL if no input sections were mapped to this output section.
1512// We still have to adjust dot and process symbol assignments.
1513
1514void
1515Output_section_definition::set_section_addresses(Symbol_table* symtab,
1516 Layout* layout,
1517 bool* dot_has_value,
1518 uint64_t* dot_value)
1519{
1520 bool is_absolute;
1521 uint64_t address;
1522 if (this->address_ != NULL)
1523 {
1524 address = this->address_->eval_with_dot(symtab, layout, *dot_has_value,
1525 *dot_value, &is_absolute);
1526 if (!is_absolute)
1527 gold_error(_("address of section %s is not absolute"),
1528 this->name_.c_str());
1529 }
1530 else
1531 {
1532 if (!*dot_has_value)
1533 gold_error(_("no address given for section %s"),
1534 this->name_.c_str());
1535 address = *dot_value;
1536 }
1537
1538 uint64_t align;
1539 if (this->align_ == NULL)
1540 {
1541 if (this->output_section_ == NULL)
1542 align = 0;
1543 else
1544 align = this->output_section_->addralign();
1545 }
1546 else
1547 {
1548 align = this->align_->eval_with_dot(symtab, layout, *dot_has_value,
1549 *dot_value, &is_absolute);
1550 if (!is_absolute)
1551 gold_error(_("alignment of section %s is not absolute"),
1552 this->name_.c_str());
1553 if (this->output_section_ != NULL)
1554 this->output_section_->set_addralign(align);
1555 }
1556
1557 address = align_address(address, align);
1558
1559 *dot_value = address;
1560 *dot_has_value = true;
1561
1562 // The address of non-SHF_ALLOC sections is forced to zero,
1563 // regardless of what the linker script wants.
1564 if (this->output_section_ != NULL
1565 && (this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0)
1566 this->output_section_->set_address(address);
1567
1568 if (this->load_address_ != NULL && this->output_section_ != NULL)
1569 {
1570 uint64_t load_address =
1571 this->load_address_->eval_with_dot(symtab, layout, *dot_has_value,
1572 *dot_value, &is_absolute);
1573 if (!is_absolute)
1574 gold_error(_("load address of section %s is not absolute"),
1575 this->name_.c_str());
1576 this->output_section_->set_load_address(load_address);
1577 }
1578
1579 uint64_t subalign;
1580 if (this->subalign_ == NULL)
1581 subalign = 0;
1582 else
1583 {
1584 subalign = this->subalign_->eval_with_dot(symtab, layout, *dot_has_value,
1585 *dot_value, &is_absolute);
1586 if (!is_absolute)
1587 gold_error(_("subalign of section %s is not absolute"),
1588 this->name_.c_str());
1589 }
1590
1591 std::string fill;
1592 if (this->fill_ != NULL)
1593 {
1594 // FIXME: The GNU linker supports fill values of arbitrary
1595 // length.
1596 uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout,
1597 *dot_has_value,
1598 *dot_value,
1599 &is_absolute);
1600 if (!is_absolute)
1601 gold_error(_("fill of section %s is not absolute"),
1602 this->name_.c_str());
1603 unsigned char fill_buff[4];
1604 elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1605 fill.assign(reinterpret_cast<char*>(fill_buff), 4);
1606 }
1607
1608 Input_section_list input_sections;
1609 if (this->output_section_ != NULL)
1610 {
1611 // Get the list of input sections attached to this output
1612 // section. This will leave the output section with only
1613 // Output_section_data entries.
1614 address += this->output_section_->get_input_sections(address,
1615 fill,
1616 &input_sections);
1617 *dot_value = address;
1618 }
1619
1620 for (Output_section_elements::iterator p = this->elements_.begin();
1621 p != this->elements_.end();
1622 ++p)
1623 (*p)->set_section_addresses(symtab, layout, this->output_section_,
1624 subalign, dot_value, &fill, &input_sections);
1625
1626 gold_assert(input_sections.empty());
1627}
1628
3802b2dd
ILT
1629// Check a constraint (ONLY_IF_RO, etc.) on an output section. If
1630// this section is constrained, and the input sections do not match,
1631// return the constraint, and set *POSD.
1632
1633Section_constraint
1634Output_section_definition::check_constraint(Output_section_definition** posd)
1635{
1636 switch (this->constraint_)
1637 {
1638 case CONSTRAINT_NONE:
1639 return CONSTRAINT_NONE;
1640
1641 case CONSTRAINT_ONLY_IF_RO:
1642 if (this->output_section_ != NULL
1643 && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
1644 {
1645 *posd = this;
1646 return CONSTRAINT_ONLY_IF_RO;
1647 }
1648 return CONSTRAINT_NONE;
1649
1650 case CONSTRAINT_ONLY_IF_RW:
1651 if (this->output_section_ != NULL
1652 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1653 {
1654 *posd = this;
1655 return CONSTRAINT_ONLY_IF_RW;
1656 }
1657 return CONSTRAINT_NONE;
1658
1659 case CONSTRAINT_SPECIAL:
1660 if (this->output_section_ != NULL)
1661 gold_error(_("SPECIAL constraints are not implemented"));
1662 return CONSTRAINT_NONE;
1663
1664 default:
1665 gold_unreachable();
1666 }
1667}
1668
1669// See if this is the alternate output section for a constrained
1670// output section. If it is, transfer the Output_section and return
1671// true. Otherwise return false.
1672
1673bool
1674Output_section_definition::alternate_constraint(
1675 Output_section_definition* posd,
1676 Section_constraint constraint)
1677{
1678 if (this->name_ != posd->name_)
1679 return false;
1680
1681 switch (constraint)
1682 {
1683 case CONSTRAINT_ONLY_IF_RO:
1684 if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
1685 return false;
1686 break;
1687
1688 case CONSTRAINT_ONLY_IF_RW:
1689 if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
1690 return false;
1691 break;
1692
1693 default:
1694 gold_unreachable();
1695 }
1696
1697 // We have found the alternate constraint. We just need to move
1698 // over the Output_section. When constraints are used properly,
1699 // THIS should not have an output_section pointer, as all the input
1700 // sections should have matched the other definition.
1701
1702 if (this->output_section_ != NULL)
1703 gold_error(_("mismatched definition for constrained sections"));
1704
1705 this->output_section_ = posd->output_section_;
1706 posd->output_section_ = NULL;
1707
1708 return true;
1709}
1710
1c4f3631
ILT
1711// Get the list of segments to use for an allocated section when using
1712// a PHDRS clause. If this is an allocated section, return the
1713// Output_section, and set *PHDRS_LIST to the list of PHDRS to which
1714// it should be attached. If the PHDRS were not specified, don't
1715// change *PHDRS_LIST.
1716
1717Output_section*
1718Output_section_definition::allocate_to_segment(String_list** phdrs_list)
1719{
1720 if (this->output_section_ == NULL)
1721 return NULL;
1722 if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
1723 return NULL;
1724 if (this->phdrs_ != NULL)
1725 *phdrs_list = this->phdrs_;
1726 return this->output_section_;
1727}
1728
494e05f4
ILT
1729// Print for debugging.
1730
1731void
1732Output_section_definition::print(FILE* f) const
1733{
1734 fprintf(f, " %s ", this->name_.c_str());
1735
1736 if (this->address_ != NULL)
1737 {
1738 this->address_->print(f);
1739 fprintf(f, " ");
1740 }
1741
1742 fprintf(f, ": ");
1743
1744 if (this->load_address_ != NULL)
1745 {
1746 fprintf(f, "AT(");
1747 this->load_address_->print(f);
1748 fprintf(f, ") ");
1749 }
1750
1751 if (this->align_ != NULL)
1752 {
1753 fprintf(f, "ALIGN(");
1754 this->align_->print(f);
1755 fprintf(f, ") ");
1756 }
1757
1758 if (this->subalign_ != NULL)
1759 {
1760 fprintf(f, "SUBALIGN(");
1761 this->subalign_->print(f);
1762 fprintf(f, ") ");
1763 }
1764
1765 fprintf(f, "{\n");
1766
1767 for (Output_section_elements::const_iterator p = this->elements_.begin();
1768 p != this->elements_.end();
1769 ++p)
1770 (*p)->print(f);
1771
1772 fprintf(f, " }");
1773
1774 if (this->fill_ != NULL)
1775 {
1776 fprintf(f, " = ");
1777 this->fill_->print(f);
1778 }
1779
1780 fprintf(f, "\n");
1781}
1782
a445fddf
ILT
1783// An output section created to hold orphaned input sections. These
1784// do not actually appear in linker scripts. However, for convenience
1785// when setting the output section addresses, we put a marker to these
1786// sections in the appropriate place in the list of SECTIONS elements.
1787
1788class Orphan_output_section : public Sections_element
1789{
1790 public:
1791 Orphan_output_section(Output_section* os)
1792 : os_(os)
1793 { }
1794
1795 // Return whether to place an orphan section after this one.
1796 bool
1797 place_orphan_here(const Output_section *os, bool* exact) const;
1798
1799 // Set section addresses.
1800 void
1801 set_section_addresses(Symbol_table*, Layout*, bool*, uint64_t*);
1802
1c4f3631
ILT
1803 // Get the list of segments to use for an allocated section when
1804 // using a PHDRS clause. If this is an allocated section, return
1805 // the Output_section.
1806 Output_section*
1807 allocate_to_segment(String_list**);
1808
a445fddf
ILT
1809 // Print for debugging.
1810 void
1811 print(FILE* f) const
1812 {
1813 fprintf(f, " marker for orphaned output section %s\n",
1814 this->os_->name());
1815 }
1816
1817 private:
1818 Output_section* os_;
1819};
1820
1821// Whether to place another orphan section after this one.
1822
1823bool
1824Orphan_output_section::place_orphan_here(const Output_section* os,
1825 bool* exact) const
1826{
1827 if (this->os_->type() == os->type()
1828 && this->os_->flags() == os->flags())
1829 {
1830 *exact = true;
1831 return true;
1832 }
1833 return false;
1834}
1835
1836// Set section addresses.
1837
1838void
1839Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
1840 bool* dot_has_value,
1841 uint64_t* dot_value)
1842{
1843 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
1844
1845 if (!*dot_has_value)
1846 gold_error(_("no address for orphan section %s"), this->os_->name());
1847
1848 uint64_t address = *dot_value;
1849 address = align_address(address, this->os_->addralign());
1850
1851 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
1852 this->os_->set_address(address);
1853
1854 Input_section_list input_sections;
1855 address += this->os_->get_input_sections(address, "", &input_sections);
1856
1857 for (Input_section_list::iterator p = input_sections.begin();
1858 p != input_sections.end();
1859 ++p)
1860 {
1861 uint64_t addralign;
1862 uint64_t size;
1863
1864 // We know what are single-threaded, so it is OK to lock the
1865 // object.
1866 {
1867 const Task* task = reinterpret_cast<const Task*>(-1);
1868 Task_lock_obj<Object> tl(task, p->first);
1869 addralign = p->first->section_addralign(p->second);
1870 size = p->first->section_size(p->second);
1871 }
1872
1873 address = align_address(address, addralign);
1874 this->os_->add_input_section_for_script(p->first, p->second, size, 0);
1875 address += size;
1876 }
1877
1878 *dot_value = address;
1879}
1880
1c4f3631
ILT
1881// Get the list of segments to use for an allocated section when using
1882// a PHDRS clause. If this is an allocated section, return the
1883// Output_section. We don't change the list of segments.
1884
1885Output_section*
1886Orphan_output_section::allocate_to_segment(String_list**)
1887{
1888 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
1889 return NULL;
1890 return this->os_;
1891}
1892
1893// Class Phdrs_element. A program header from a PHDRS clause.
1894
1895class Phdrs_element
1896{
1897 public:
1898 Phdrs_element(const char* name, size_t namelen, unsigned int type,
1899 bool includes_filehdr, bool includes_phdrs,
1900 bool is_flags_valid, unsigned int flags,
1901 Expression* load_address)
1902 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
1903 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
1904 flags_(flags), load_address_(load_address), load_address_value_(0),
1905 segment_(NULL)
1906 { }
1907
1908 // Return the name of this segment.
1909 const std::string&
1910 name() const
1911 { return this->name_; }
1912
1913 // Return the type of the segment.
1914 unsigned int
1915 type() const
1916 { return this->type_; }
1917
1918 // Whether to include the file header.
1919 bool
1920 includes_filehdr() const
1921 { return this->includes_filehdr_; }
1922
1923 // Whether to include the program headers.
1924 bool
1925 includes_phdrs() const
1926 { return this->includes_phdrs_; }
1927
1928 // Return whether there is a load address.
1929 bool
1930 has_load_address() const
1931 { return this->load_address_ != NULL; }
1932
1933 // Evaluate the load address expression if there is one.
1934 void
1935 eval_load_address(Symbol_table* symtab, Layout* layout)
1936 {
1937 if (this->load_address_ != NULL)
1938 this->load_address_value_ = this->load_address_->eval(symtab, layout);
1939 }
1940
1941 // Return the load address.
1942 uint64_t
1943 load_address() const
1944 {
1945 gold_assert(this->load_address_ != NULL);
1946 return this->load_address_value_;
1947 }
1948
1949 // Create the segment.
1950 Output_segment*
1951 create_segment(Layout* layout)
1952 {
1953 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
1954 return this->segment_;
1955 }
1956
1957 // Return the segment.
1958 Output_segment*
1959 segment()
1960 { return this->segment_; }
1961
1962 // Set the segment flags if appropriate.
1963 void
1964 set_flags_if_valid()
1965 {
1966 if (this->is_flags_valid_)
1967 this->segment_->set_flags(this->flags_);
1968 }
1969
1970 private:
1971 // The name used in the script.
1972 std::string name_;
1973 // The type of the segment (PT_LOAD, etc.).
1974 unsigned int type_;
1975 // Whether this segment includes the file header.
1976 bool includes_filehdr_;
1977 // Whether this segment includes the section headers.
1978 bool includes_phdrs_;
1979 // Whether the flags were explicitly specified.
1980 bool is_flags_valid_;
1981 // The flags for this segment (PF_R, etc.) if specified.
1982 unsigned int flags_;
1983 // The expression for the load address for this segment. This may
1984 // be NULL.
1985 Expression* load_address_;
1986 // The actual load address from evaluating the expression.
1987 uint64_t load_address_value_;
1988 // The segment itself.
1989 Output_segment* segment_;
1990};
1991
494e05f4
ILT
1992// Class Script_sections.
1993
1994Script_sections::Script_sections()
1995 : saw_sections_clause_(false),
1996 in_sections_clause_(false),
1997 sections_elements_(NULL),
1c4f3631
ILT
1998 output_section_(NULL),
1999 phdrs_elements_(NULL)
494e05f4
ILT
2000{
2001}
2002
2003// Start a SECTIONS clause.
2004
2005void
2006Script_sections::start_sections()
2007{
2008 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2009 this->saw_sections_clause_ = true;
2010 this->in_sections_clause_ = true;
2011 if (this->sections_elements_ == NULL)
2012 this->sections_elements_ = new Sections_elements;
2013}
2014
2015// Finish a SECTIONS clause.
2016
2017void
2018Script_sections::finish_sections()
2019{
2020 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2021 this->in_sections_clause_ = false;
2022}
2023
2024// Add a symbol to be defined.
2025
2026void
2027Script_sections::add_symbol_assignment(const char* name, size_t length,
2028 Expression* val, bool provide,
2029 bool hidden)
2030{
2031 if (this->output_section_ != NULL)
2032 this->output_section_->add_symbol_assignment(name, length, val,
2033 provide, hidden);
2034 else
2035 {
2036 Sections_element* p = new Sections_element_assignment(name, length,
2037 val, provide,
2038 hidden);
2039 this->sections_elements_->push_back(p);
2040 }
2041}
2042
a445fddf
ILT
2043// Add an assignment to the special dot symbol.
2044
2045void
2046Script_sections::add_dot_assignment(Expression* val)
2047{
2048 if (this->output_section_ != NULL)
2049 this->output_section_->add_dot_assignment(val);
2050 else
2051 {
2052 Sections_element* p = new Sections_element_dot_assignment(val);
2053 this->sections_elements_->push_back(p);
2054 }
2055}
2056
494e05f4
ILT
2057// Add an assertion.
2058
2059void
2060Script_sections::add_assertion(Expression* check, const char* message,
2061 size_t messagelen)
2062{
2063 if (this->output_section_ != NULL)
2064 this->output_section_->add_assertion(check, message, messagelen);
2065 else
2066 {
2067 Sections_element* p = new Sections_element_assertion(check, message,
2068 messagelen);
2069 this->sections_elements_->push_back(p);
2070 }
2071}
2072
2073// Start processing entries for an output section.
2074
2075void
2076Script_sections::start_output_section(
2077 const char* name,
2078 size_t namelen,
2079 const Parser_output_section_header *header)
2080{
2081 Output_section_definition* posd = new Output_section_definition(name,
2082 namelen,
2083 header);
2084 this->sections_elements_->push_back(posd);
2085 gold_assert(this->output_section_ == NULL);
2086 this->output_section_ = posd;
2087}
2088
2089// Stop processing entries for an output section.
2090
2091void
2092Script_sections::finish_output_section(
2093 const Parser_output_section_trailer* trailer)
2094{
2095 gold_assert(this->output_section_ != NULL);
2096 this->output_section_->finish(trailer);
2097 this->output_section_ = NULL;
2098}
2099
2100// Add a data item to the current output section.
2101
2102void
2103Script_sections::add_data(int size, bool is_signed, Expression* val)
2104{
2105 gold_assert(this->output_section_ != NULL);
2106 this->output_section_->add_data(size, is_signed, val);
2107}
2108
2109// Add a fill value setting to the current output section.
2110
2111void
2112Script_sections::add_fill(Expression* val)
2113{
2114 gold_assert(this->output_section_ != NULL);
2115 this->output_section_->add_fill(val);
2116}
2117
2118// Add an input section specification to the current output section.
2119
2120void
2121Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2122{
2123 gold_assert(this->output_section_ != NULL);
2124 this->output_section_->add_input_section(spec, keep);
2125}
2126
a445fddf
ILT
2127// Add any symbols we are defining to the symbol table.
2128
2129void
2130Script_sections::add_symbols_to_table(Symbol_table* symtab)
2131{
2132 if (!this->saw_sections_clause_)
2133 return;
2134 for (Sections_elements::iterator p = this->sections_elements_->begin();
2135 p != this->sections_elements_->end();
2136 ++p)
2137 (*p)->add_symbols_to_table(symtab);
2138}
2139
2140// Finalize symbols and check assertions.
2141
2142void
2143Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2144{
2145 if (!this->saw_sections_clause_)
2146 return;
2147 bool dot_has_value = false;
2148 uint64_t dot_value = 0;
2149 for (Sections_elements::iterator p = this->sections_elements_->begin();
2150 p != this->sections_elements_->end();
2151 ++p)
2152 (*p)->finalize_symbols(symtab, layout, &dot_has_value, &dot_value);
2153}
2154
2155// Return the name of the output section to use for an input file name
2156// and section name.
2157
2158const char*
2159Script_sections::output_section_name(const char* file_name,
2160 const char* section_name,
2161 Output_section*** output_section_slot)
2162{
2163 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2164 p != this->sections_elements_->end();
2165 ++p)
2166 {
2167 const char* ret = (*p)->output_section_name(file_name, section_name,
2168 output_section_slot);
2169
2170 if (ret != NULL)
2171 {
2172 // The special name /DISCARD/ means that the input section
2173 // should be discarded.
2174 if (strcmp(ret, "/DISCARD/") == 0)
2175 {
2176 *output_section_slot = NULL;
2177 return NULL;
2178 }
2179 return ret;
2180 }
2181 }
2182
2183 // If we couldn't find a mapping for the name, the output section
2184 // gets the name of the input section.
2185
2186 *output_section_slot = NULL;
2187
2188 return section_name;
2189}
2190
2191// Place a marker for an orphan output section into the SECTIONS
2192// clause.
2193
2194void
2195Script_sections::place_orphan(Output_section* os)
2196{
2197 // Look for an output section definition which matches the output
2198 // section. Put a marker after that section.
2199 Sections_elements::iterator place = this->sections_elements_->end();
2200 for (Sections_elements::iterator p = this->sections_elements_->begin();
2201 p != this->sections_elements_->end();
2202 ++p)
2203 {
2204 bool exact;
2205 if ((*p)->place_orphan_here(os, &exact))
2206 {
2207 place = p;
2208 if (exact)
2209 break;
2210 }
2211 }
2212
2213 // The insert function puts the new element before the iterator.
2214 if (place != this->sections_elements_->end())
2215 ++place;
2216
2217 this->sections_elements_->insert(place, new Orphan_output_section(os));
2218}
2219
2220// Set the addresses of all the output sections. Walk through all the
2221// elements, tracking the dot symbol. Apply assignments which set
2222// absolute symbol values, in case they are used when setting dot.
2223// Fill in data statement values. As we find output sections, set the
2224// address, set the address of all associated input sections, and
2225// update dot. Return the segment which should hold the file header
2226// and segment headers, if any.
2227
2228Output_segment*
2229Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2230{
2231 gold_assert(this->saw_sections_clause_);
2232
3802b2dd
ILT
2233 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2234 // for our representation.
2235 for (Sections_elements::iterator p = this->sections_elements_->begin();
2236 p != this->sections_elements_->end();
2237 ++p)
2238 {
2239 Output_section_definition* posd;
2240 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2241 if (failed_constraint != CONSTRAINT_NONE)
2242 {
2243 Sections_elements::iterator q;
2244 for (q = this->sections_elements_->begin();
2245 q != this->sections_elements_->end();
2246 ++q)
2247 {
2248 if (q != p)
2249 {
2250 if ((*q)->alternate_constraint(posd, failed_constraint))
2251 break;
2252 }
2253 }
2254
2255 if (q == this->sections_elements_->end())
2256 gold_error(_("no matching section constraint"));
2257 }
2258 }
2259
a445fddf
ILT
2260 bool dot_has_value = false;
2261 uint64_t dot_value = 0;
2262 for (Sections_elements::iterator p = this->sections_elements_->begin();
2263 p != this->sections_elements_->end();
2264 ++p)
2265 (*p)->set_section_addresses(symtab, layout, &dot_has_value, &dot_value);
2266
1c4f3631
ILT
2267 if (this->phdrs_elements_ != NULL)
2268 {
2269 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2270 p != this->phdrs_elements_->end();
2271 ++p)
2272 (*p)->eval_load_address(symtab, layout);
2273 }
2274
a445fddf
ILT
2275 return this->create_segments(layout);
2276}
2277
2278// Sort the sections in order to put them into segments.
2279
2280class Sort_output_sections
2281{
2282 public:
2283 bool
2284 operator()(const Output_section* os1, const Output_section* os2) const;
2285};
2286
2287bool
2288Sort_output_sections::operator()(const Output_section* os1,
2289 const Output_section* os2) const
2290{
2291 // Sort first by the load address.
2292 uint64_t lma1 = (os1->has_load_address()
2293 ? os1->load_address()
2294 : os1->address());
2295 uint64_t lma2 = (os2->has_load_address()
2296 ? os2->load_address()
2297 : os2->address());
2298 if (lma1 != lma2)
2299 return lma1 < lma2;
2300
2301 // Then sort by the virtual address.
2302 if (os1->address() != os2->address())
2303 return os1->address() < os2->address();
2304
2305 // Sort TLS sections to the end.
2306 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
2307 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
2308 if (tls1 != tls2)
2309 return tls2;
2310
2311 // Sort PROGBITS before NOBITS.
2312 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
2313 return true;
2314 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
2315 return false;
2316
2317 // Otherwise we don't care.
2318 return false;
2319}
2320
2321// Return whether OS is a BSS section. This is a SHT_NOBITS section.
2322// We treat a section with the SHF_TLS flag set as taking up space
2323// even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2324// space for them in the file.
2325
2326bool
2327Script_sections::is_bss_section(const Output_section* os)
2328{
2329 return (os->type() == elfcpp::SHT_NOBITS
2330 && (os->flags() & elfcpp::SHF_TLS) == 0);
2331}
2332
1c4f3631
ILT
2333// Return the size taken by the file header and the program headers.
2334
2335size_t
2336Script_sections::total_header_size(Layout* layout) const
2337{
2338 size_t segment_count = layout->segment_count();
2339 size_t file_header_size;
2340 size_t segment_headers_size;
2341 if (parameters->get_size() == 32)
2342 {
2343 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
2344 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
2345 }
2346 else if (parameters->get_size() == 64)
2347 {
2348 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
2349 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
2350 }
2351 else
2352 gold_unreachable();
2353
2354 return file_header_size + segment_headers_size;
2355}
2356
2357// Return the amount we have to subtract from the LMA to accomodate
2358// headers of the given size. The complication is that the file
2359// header have to be at the start of a page, as otherwise it will not
2360// be at the start of the file.
2361
2362uint64_t
2363Script_sections::header_size_adjustment(uint64_t lma,
2364 size_t sizeof_headers) const
2365{
2366 const uint64_t abi_pagesize = parameters->target()->abi_pagesize();
2367 uint64_t hdr_lma = lma - sizeof_headers;
2368 hdr_lma &= ~(abi_pagesize - 1);
2369 return lma - hdr_lma;
2370}
2371
a445fddf
ILT
2372// Create the PT_LOAD segments when using a SECTIONS clause. Returns
2373// the segment which should hold the file header and segment headers,
2374// if any.
2375
2376Output_segment*
2377Script_sections::create_segments(Layout* layout)
2378{
2379 gold_assert(this->saw_sections_clause_);
2380
2381 if (parameters->output_is_object())
2382 return NULL;
2383
1c4f3631
ILT
2384 if (this->saw_phdrs_clause())
2385 return create_segments_from_phdrs_clause(layout);
2386
a445fddf
ILT
2387 Layout::Section_list sections;
2388 layout->get_allocated_sections(&sections);
2389
2390 // Sort the sections by address.
2391 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
2392
2393 this->create_note_and_tls_segments(layout, &sections);
2394
2395 // Walk through the sections adding them to PT_LOAD segments.
2396 const uint64_t abi_pagesize = parameters->target()->abi_pagesize();
2397 Output_segment* first_seg = NULL;
2398 Output_segment* current_seg = NULL;
2399 bool is_current_seg_readonly = true;
2400 Layout::Section_list::iterator plast = sections.end();
2401 uint64_t last_vma = 0;
2402 uint64_t last_lma = 0;
2403 uint64_t last_size = 0;
2404 for (Layout::Section_list::iterator p = sections.begin();
2405 p != sections.end();
2406 ++p)
2407 {
2408 const uint64_t vma = (*p)->address();
2409 const uint64_t lma = ((*p)->has_load_address()
2410 ? (*p)->load_address()
2411 : vma);
2412 const uint64_t size = (*p)->current_data_size();
2413
2414 bool need_new_segment;
2415 if (current_seg == NULL)
2416 need_new_segment = true;
2417 else if (lma - vma != last_lma - last_vma)
2418 {
2419 // This section has a different LMA relationship than the
2420 // last one; we need a new segment.
2421 need_new_segment = true;
2422 }
2423 else if (align_address(last_lma + last_size, abi_pagesize)
2424 < align_address(lma, abi_pagesize))
2425 {
2426 // Putting this section in the segment would require
2427 // skipping a page.
2428 need_new_segment = true;
2429 }
2430 else if (is_bss_section(*plast) && !is_bss_section(*p))
2431 {
2432 // A non-BSS section can not follow a BSS section in the
2433 // same segment.
2434 need_new_segment = true;
2435 }
2436 else if (is_current_seg_readonly
2437 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2438 {
2439 // Don't put a writable section in the same segment as a
2440 // non-writable section.
2441 need_new_segment = true;
2442 }
2443 else
2444 {
2445 // Otherwise, reuse the existing segment.
2446 need_new_segment = false;
2447 }
2448
2449 elfcpp::Elf_Word seg_flags =
2450 Layout::section_flags_to_segment((*p)->flags());
2451
2452 if (need_new_segment)
2453 {
2454 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2455 seg_flags);
2456 current_seg->set_addresses(vma, lma);
2457 if (first_seg == NULL)
2458 first_seg = current_seg;
2459 is_current_seg_readonly = true;
2460 }
2461
2462 current_seg->add_output_section(*p, seg_flags);
2463
2464 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2465 is_current_seg_readonly = false;
2466
2467 plast = p;
2468 last_vma = vma;
2469 last_lma = lma;
2470 last_size = size;
2471 }
2472
2473 // An ELF program should work even if the program headers are not in
2474 // a PT_LOAD segment. However, it appears that the Linux kernel
2475 // does not set the AT_PHDR auxiliary entry in that case. It sets
2476 // the load address to p_vaddr - p_offset of the first PT_LOAD
2477 // segment. It then sets AT_PHDR to the load address plus the
2478 // offset to the program headers, e_phoff in the file header. This
2479 // fails when the program headers appear in the file before the
2480 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2481 // segment to hold the file header and the program headers. This is
2482 // effectively what the GNU linker does, and it is slightly more
2483 // efficient in any case. We try to use the first PT_LOAD segment
2484 // if we can, otherwise we make a new one.
2485
1c4f3631 2486 size_t sizeof_headers = this->total_header_size(layout);
3802b2dd 2487
a445fddf 2488 if (first_seg != NULL
3802b2dd
ILT
2489 && (first_seg->paddr() & (abi_pagesize - 1)) >= sizeof_headers)
2490 {
2491 first_seg->set_addresses(first_seg->vaddr() - sizeof_headers,
2492 first_seg->paddr() - sizeof_headers);
2493 return first_seg;
2494 }
a445fddf
ILT
2495
2496 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2497 elfcpp::PF_R);
2498 if (first_seg == NULL)
2499 load_seg->set_addresses(0, 0);
2500 else
2501 {
2502 uint64_t vma = first_seg->vaddr();
2503 uint64_t lma = first_seg->paddr();
2504
1c4f3631
ILT
2505 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
2506 if (lma >= subtract && vma >= subtract)
2507 load_seg->set_addresses(vma - subtract, lma - subtract);
a445fddf
ILT
2508 else
2509 {
2510 // We could handle this case by create the file header
2511 // outside of any PT_LOAD segment, and creating a new
2512 // PT_LOAD segment after the others to hold the segment
2513 // headers.
2514 gold_error(_("sections loaded on first page without room for "
2515 "file and program headers are not supported"));
2516 }
2517 }
2518
2519 return load_seg;
2520}
2521
2522// Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2523// segment if there are any SHT_TLS sections.
2524
2525void
2526Script_sections::create_note_and_tls_segments(
2527 Layout* layout,
2528 const Layout::Section_list* sections)
2529{
1c4f3631
ILT
2530 gold_assert(!this->saw_phdrs_clause());
2531
a445fddf
ILT
2532 bool saw_tls = false;
2533 for (Layout::Section_list::const_iterator p = sections->begin();
2534 p != sections->end();
2535 ++p)
2536 {
2537 if ((*p)->type() == elfcpp::SHT_NOTE)
2538 {
2539 elfcpp::Elf_Word seg_flags =
2540 Layout::section_flags_to_segment((*p)->flags());
2541 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
2542 seg_flags);
2543 oseg->add_output_section(*p, seg_flags);
2544
2545 // Incorporate any subsequent SHT_NOTE sections, in the
2546 // hopes that the script is sensible.
2547 Layout::Section_list::const_iterator pnext = p + 1;
2548 while (pnext != sections->end()
2549 && (*pnext)->type() == elfcpp::SHT_NOTE)
2550 {
2551 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2552 oseg->add_output_section(*pnext, seg_flags);
2553 p = pnext;
2554 ++pnext;
2555 }
2556 }
2557
2558 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2559 {
2560 if (saw_tls)
2561 gold_error(_("TLS sections are not adjacent"));
2562
2563 elfcpp::Elf_Word seg_flags =
2564 Layout::section_flags_to_segment((*p)->flags());
2565 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
2566 seg_flags);
2567 oseg->add_output_section(*p, seg_flags);
2568
2569 Layout::Section_list::const_iterator pnext = p + 1;
2570 while (pnext != sections->end()
2571 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
2572 {
2573 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2574 oseg->add_output_section(*pnext, seg_flags);
2575 p = pnext;
2576 ++pnext;
2577 }
2578
2579 saw_tls = true;
2580 }
2581 }
2582}
2583
1c4f3631
ILT
2584// Add a program header. The PHDRS clause is syntactically distinct
2585// from the SECTIONS clause, but we implement it with the SECTIONS
2586// support becauase PHDRS is useless if there is no SECTIONS clause.
2587
2588void
2589Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
2590 bool includes_filehdr, bool includes_phdrs,
2591 bool is_flags_valid, unsigned int flags,
2592 Expression* load_address)
2593{
2594 if (this->phdrs_elements_ == NULL)
2595 this->phdrs_elements_ = new Phdrs_elements();
2596 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
2597 includes_filehdr,
2598 includes_phdrs,
2599 is_flags_valid, flags,
2600 load_address));
2601}
2602
3802b2dd
ILT
2603// Return the number of segments we expect to create based on the
2604// SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2605
2606size_t
2607Script_sections::expected_segment_count(const Layout* layout) const
2608{
1c4f3631
ILT
2609 if (this->saw_phdrs_clause())
2610 return this->phdrs_elements_->size();
2611
3802b2dd
ILT
2612 Layout::Section_list sections;
2613 layout->get_allocated_sections(&sections);
2614
2615 // We assume that we will need two PT_LOAD segments.
2616 size_t ret = 2;
2617
2618 bool saw_note = false;
2619 bool saw_tls = false;
2620 for (Layout::Section_list::const_iterator p = sections.begin();
2621 p != sections.end();
2622 ++p)
2623 {
2624 if ((*p)->type() == elfcpp::SHT_NOTE)
2625 {
2626 // Assume that all note sections will fit into a single
2627 // PT_NOTE segment.
2628 if (!saw_note)
2629 {
2630 ++ret;
2631 saw_note = true;
2632 }
2633 }
2634 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2635 {
2636 // There can only be one PT_TLS segment.
2637 if (!saw_tls)
2638 {
2639 ++ret;
2640 saw_tls = true;
2641 }
2642 }
2643 }
2644
2645 return ret;
2646}
2647
1c4f3631
ILT
2648// Create the segments from a PHDRS clause. Return the segment which
2649// should hold the file header and program headers, if any.
2650
2651Output_segment*
2652Script_sections::create_segments_from_phdrs_clause(Layout* layout)
2653{
2654 this->attach_sections_using_phdrs_clause(layout);
2655 return this->set_phdrs_clause_addresses(layout);
2656}
2657
2658// Create the segments from the PHDRS clause, and put the output
2659// sections in them.
2660
2661void
2662Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
2663{
2664 typedef std::map<std::string, Output_segment*> Name_to_segment;
2665 Name_to_segment name_to_segment;
2666 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2667 p != this->phdrs_elements_->end();
2668 ++p)
2669 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
2670
2671 // Walk through the output sections and attach them to segments.
2672 // Output sections in the script which do not list segments are
2673 // attached to the same set of segments as the immediately preceding
2674 // output section.
2675 String_list* phdr_names = NULL;
2676 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2677 p != this->sections_elements_->end();
2678 ++p)
2679 {
2680 Output_section* os = (*p)->allocate_to_segment(&phdr_names);
2681 if (os == NULL)
2682 continue;
2683
2684 if (phdr_names == NULL)
2685 {
2686 gold_error(_("allocated section not in any segment"));
2687 continue;
2688 }
2689
2690 bool in_load_segment = false;
2691 for (String_list::const_iterator q = phdr_names->begin();
2692 q != phdr_names->end();
2693 ++q)
2694 {
2695 Name_to_segment::const_iterator r = name_to_segment.find(*q);
2696 if (r == name_to_segment.end())
2697 gold_error(_("no segment %s"), q->c_str());
2698 else
2699 {
2700 elfcpp::Elf_Word seg_flags =
2701 Layout::section_flags_to_segment(os->flags());
2702 r->second->add_output_section(os, seg_flags);
2703
2704 if (r->second->type() == elfcpp::PT_LOAD)
2705 {
2706 if (in_load_segment)
2707 gold_error(_("section in two PT_LOAD segments"));
2708 in_load_segment = true;
2709 }
2710 }
2711 }
2712
2713 if (!in_load_segment)
2714 gold_error(_("allocated section not in any PT_LOAD segment"));
2715 }
2716}
2717
2718// Set the addresses for segments created from a PHDRS clause. Return
2719// the segment which should hold the file header and program headers,
2720// if any.
2721
2722Output_segment*
2723Script_sections::set_phdrs_clause_addresses(Layout* layout)
2724{
2725 Output_segment* load_seg = NULL;
2726 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2727 p != this->phdrs_elements_->end();
2728 ++p)
2729 {
2730 // Note that we have to set the flags after adding the output
2731 // sections to the segment, as adding an output segment can
2732 // change the flags.
2733 (*p)->set_flags_if_valid();
2734
2735 Output_segment* oseg = (*p)->segment();
2736
2737 if (oseg->type() != elfcpp::PT_LOAD)
2738 {
2739 // The addresses of non-PT_LOAD segments are set from the
2740 // PT_LOAD segments.
2741 if ((*p)->has_load_address())
2742 gold_error(_("may only specify load address for PT_LOAD segment"));
2743 continue;
2744 }
2745
2746 // The output sections should have addresses from the SECTIONS
2747 // clause. The addresses don't have to be in order, so find the
2748 // one with the lowest load address. Use that to set the
2749 // address of the segment.
2750
2751 Output_section* osec = oseg->section_with_lowest_load_address();
2752 if (osec == NULL)
2753 {
2754 oseg->set_addresses(0, 0);
2755 continue;
2756 }
2757
2758 uint64_t vma = osec->address();
2759 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
2760
2761 // Override the load address of the section with the load
2762 // address specified for the segment.
2763 if ((*p)->has_load_address())
2764 {
2765 if (osec->has_load_address())
2766 gold_warning(_("PHDRS load address overrides "
2767 "section %s load address"),
2768 osec->name());
2769
2770 lma = (*p)->load_address();
2771 }
2772
2773 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
2774 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
2775 {
2776 // We could support this if we wanted to.
2777 gold_error(_("using only one of FILEHDR and PHDRS is "
2778 "not currently supported"));
2779 }
2780 if (headers)
2781 {
2782 size_t sizeof_headers = this->total_header_size(layout);
2783 uint64_t subtract = this->header_size_adjustment(lma,
2784 sizeof_headers);
2785 if (lma >= subtract && vma >= subtract)
2786 {
2787 lma -= subtract;
2788 vma -= subtract;
2789 }
2790 else
2791 {
2792 gold_error(_("sections loaded on first page without room "
2793 "for file and program headers "
2794 "are not supported"));
2795 }
2796
2797 if (load_seg != NULL)
2798 gold_error(_("using FILEHDR and PHDRS on more than one "
2799 "PT_LOAD segment is not currently supported"));
2800 load_seg = oseg;
2801 }
2802
2803 oseg->set_addresses(vma, lma);
2804 }
2805
2806 return load_seg;
2807}
2808
2809// Add the file header and segment headers to non-load segments
2810// specified in the PHDRS clause.
2811
2812void
2813Script_sections::put_headers_in_phdrs(Output_data* file_header,
2814 Output_data* segment_headers)
2815{
2816 gold_assert(this->saw_phdrs_clause());
2817 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2818 p != this->phdrs_elements_->end();
2819 ++p)
2820 {
2821 if ((*p)->type() != elfcpp::PT_LOAD)
2822 {
2823 if ((*p)->includes_phdrs())
2824 (*p)->segment()->add_initial_output_data(segment_headers);
2825 if ((*p)->includes_filehdr())
2826 (*p)->segment()->add_initial_output_data(file_header);
2827 }
2828 }
2829}
2830
494e05f4
ILT
2831// Print the SECTIONS clause to F for debugging.
2832
2833void
2834Script_sections::print(FILE* f) const
2835{
2836 if (!this->saw_sections_clause_)
2837 return;
2838
2839 fprintf(f, "SECTIONS {\n");
2840
2841 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2842 p != this->sections_elements_->end();
2843 ++p)
2844 (*p)->print(f);
2845
2846 fprintf(f, "}\n");
2847}
2848
2849} // End namespace gold.
This page took 0.137708 seconds and 4 git commands to generate.