Print PHDRS clause for debugging.
[deliverable/binutils-gdb.git] / gold / script-sections.cc
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
25 #include <cstring>
26 #include <algorithm>
27 #include <list>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include <fnmatch.h>
32
33 #include "parameters.h"
34 #include "object.h"
35 #include "layout.h"
36 #include "output.h"
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
43 namespace gold
44 {
45
46 // An element in a SECTIONS clause.
47
48 class Sections_element
49 {
50 public:
51 Sections_element()
52 { }
53
54 virtual ~Sections_element()
55 { }
56
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
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
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
109 // Print the element for debugging purposes.
110 virtual void
111 print(FILE* f) const = 0;
112 };
113
114 // An assignment in a SECTIONS clause outside of an output section.
115
116 class 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
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.
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
161 // An assignment to the dot symbol in a SECTIONS clause outside of an
162 // output section.
163
164 class 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
208 // An assertion in a SECTIONS clause outside of an output section.
209
210 class 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
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.
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
238 class Output_section_element
239 {
240 public:
241 // A list of input sections.
242 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
243
244 Output_section_element()
245 { }
246
247 virtual ~Output_section_element()
248 { }
249
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.
274 virtual void
275 print(FILE* f) const = 0;
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;
282 };
283
284 std::string
285 Output_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
297 // A symbol assignment in an output section.
298
299 class 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
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.
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
345 // An assignment to the dot symbol in an output section.
346
347 class 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
386 void
387 Output_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
420 // An assertion in an output section.
421
422 class 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
443 class 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
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.
462 void
463 print(FILE*) const;
464
465 private:
466 template<bool big_endian>
467 std::string
468 set_fill_string(uint64_t);
469
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
478 // Store the value in the section.
479
480 void
481 Output_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
510 template<bool big_endian>
511 std::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
546 // Print for debugging.
547
548 void
549 Output_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
579 class Output_section_element_fill : public Output_section_element
580 {
581 public:
582 Output_section_element_fill(Expression* val)
583 : val_(val)
584 { }
585
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.
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
618 // Return whether STRING contains a wildcard character. This is used
619 // to speed up matching.
620
621 static inline bool
622 is_wildcard_string(const std::string& s)
623 {
624 return strpbrk(s.c_str(), "?*[") != NULL;
625 }
626
627 // An input section specification in an output section
628
629 class Output_section_element_input : public Output_section_element
630 {
631 public:
632 Output_section_element_input(const Input_section_spec* spec, bool keep);
633
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.
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;
663 bool pattern_is_wildcard;
664 Sort_wildcard sort;
665
666 Input_section_pattern(const char* patterna, size_t patternlena,
667 Sort_wildcard sorta)
668 : pattern(patterna, patternlena),
669 pattern_is_wildcard(is_wildcard_string(this->pattern)),
670 sort(sorta)
671 { }
672 };
673
674 typedef std::vector<Input_section_pattern> Input_section_patterns;
675
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 }
689
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.
696 std::string filename_pattern_;
697 // Whether the file name pattern is a wildcard.
698 bool filename_is_wildcard_;
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_;
708 // The value of dot after including all matching sections.
709 uint64_t final_dot_value_;
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
716 Output_section_element_input::Output_section_element_input(
717 const Input_section_spec* spec,
718 bool keep)
719 : filename_pattern_(),
720 filename_is_wildcard_(false),
721 filename_sort_(spec->file.sort),
722 filename_exclusions_(),
723 input_section_patterns_(),
724 keep_(keep),
725 final_dot_value_(0)
726 {
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
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)
740 {
741 bool is_wildcard = is_wildcard_string(*p);
742 this->filename_exclusions_.push_back(std::make_pair(*p,
743 is_wildcard));
744 }
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
759 // See whether we match FILE_NAME.
760
761 bool
762 Output_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
795 bool
796 Output_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
822 struct 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
833 class 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
848 bool
849 Input_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
881 void
882 Output_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
1006 // Print for debugging.
1007
1008 void
1009 Output_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, ", ");
1054 fprintf(f, "%s", p->first.c_str());
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
1113 class Output_section_definition : public Sections_element
1114 {
1115 public:
1116 typedef Output_section_element::Input_section_list Input_section_list;
1117
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);
1129
1130 // Add an assignment to the special dot symbol.
1131 void
1132 add_dot_assignment(Expression* value);
1133
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
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
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
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
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_;
1210 // The constraint, if any.
1211 Section_constraint constraint_;
1212 // The fill value. This may be NULL.
1213 Expression* fill_;
1214 // The list of segments this section should go into. This may be
1215 // NULL.
1216 String_list* phdrs_;
1217 // The list of elements defining the section.
1218 Output_section_elements elements_;
1219 // The Output_section created for this definition. This will be
1220 // NULL if none was created.
1221 Output_section* output_section_;
1222 };
1223
1224 // Constructor.
1225
1226 Output_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),
1235 constraint_(header->constraint),
1236 fill_(NULL),
1237 phdrs_(NULL),
1238 elements_(),
1239 output_section_(NULL)
1240 {
1241 }
1242
1243 // Finish an output section.
1244
1245 void
1246 Output_section_definition::finish(const Parser_output_section_trailer* trailer)
1247 {
1248 this->fill_ = trailer->fill;
1249 this->phdrs_ = trailer->phdrs;
1250 }
1251
1252 // Add a symbol to be defined.
1253
1254 void
1255 Output_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
1269 // Add an assignment to the special dot symbol.
1270
1271 void
1272 Output_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
1280 void
1281 Output_section_definition::add_assertion(Expression* check,
1282 const char* message,
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
1293 void
1294 Output_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
1303 void
1304 Output_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
1312 void
1313 Output_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
1320 // Add any symbols being defined to the symbol table.
1321
1322 void
1323 Output_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
1333 void
1334 Output_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
1372 const char*
1373 Output_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
1398 bool
1399 Output_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 {
1418 if (this->name_ == ".bss")
1419 {
1420 *exact = true;
1421 return true;
1422 }
1423 if (this->output_section_ != NULL
1424 && this->output_section_->type() == elfcpp::SHT_NOBITS)
1425 return true;
1426 }
1427 else if (os->type() == elfcpp::SHT_NOTE)
1428 {
1429 if (this->output_section_ != NULL
1430 && this->output_section_->type() == elfcpp::SHT_NOTE)
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")
1441 return true;
1442 if (this->output_section_ != NULL
1443 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1444 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
1445 return true;
1446 }
1447 else if (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
1448 {
1449 if (this->name_.compare(0, 4, ".rel") == 0)
1450 {
1451 *exact = true;
1452 return true;
1453 }
1454 if (this->output_section_ != NULL
1455 && (this->output_section_->type() == elfcpp::SHT_REL
1456 || this->output_section_->type() == elfcpp::SHT_RELA))
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)
1464 return true;
1465 }
1466 else if (os->type() == elfcpp::SHT_PROGBITS
1467 && (os->flags() & elfcpp::SHF_WRITE) != 0)
1468 {
1469 if (this->name_ == ".data")
1470 {
1471 *exact = true;
1472 return true;
1473 }
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;
1478 }
1479 else if (os->type() == elfcpp::SHT_PROGBITS
1480 && (os->flags() & elfcpp::SHF_EXECINSTR) != 0)
1481 {
1482 if (this->name_ == ".text")
1483 {
1484 *exact = true;
1485 return true;
1486 }
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;
1491 }
1492 else if (os->type() == elfcpp::SHT_PROGBITS
1493 || (os->type() != elfcpp::SHT_PROGBITS
1494 && (os->flags() & elfcpp::SHF_WRITE) == 0))
1495 {
1496 if (this->name_ == ".rodata")
1497 {
1498 *exact = true;
1499 return true;
1500 }
1501 if (this->output_section_ != NULL
1502 && this->output_section_->type() == elfcpp::SHT_PROGBITS
1503 && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
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
1514 void
1515 Output_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
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
1633 Section_constraint
1634 Output_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
1673 bool
1674 Output_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
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
1717 Output_section*
1718 Output_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
1729 // Print for debugging.
1730
1731 void
1732 Output_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 if (this->phdrs_ != NULL)
1781 {
1782 for (String_list::const_iterator p = this->phdrs_->begin();
1783 p != this->phdrs_->end();
1784 ++p)
1785 fprintf(f, " :%s", p->c_str());
1786 }
1787
1788 fprintf(f, "\n");
1789 }
1790
1791 // An output section created to hold orphaned input sections. These
1792 // do not actually appear in linker scripts. However, for convenience
1793 // when setting the output section addresses, we put a marker to these
1794 // sections in the appropriate place in the list of SECTIONS elements.
1795
1796 class Orphan_output_section : public Sections_element
1797 {
1798 public:
1799 Orphan_output_section(Output_section* os)
1800 : os_(os)
1801 { }
1802
1803 // Return whether to place an orphan section after this one.
1804 bool
1805 place_orphan_here(const Output_section *os, bool* exact) const;
1806
1807 // Set section addresses.
1808 void
1809 set_section_addresses(Symbol_table*, Layout*, bool*, uint64_t*);
1810
1811 // Get the list of segments to use for an allocated section when
1812 // using a PHDRS clause. If this is an allocated section, return
1813 // the Output_section.
1814 Output_section*
1815 allocate_to_segment(String_list**);
1816
1817 // Print for debugging.
1818 void
1819 print(FILE* f) const
1820 {
1821 fprintf(f, " marker for orphaned output section %s\n",
1822 this->os_->name());
1823 }
1824
1825 private:
1826 Output_section* os_;
1827 };
1828
1829 // Whether to place another orphan section after this one.
1830
1831 bool
1832 Orphan_output_section::place_orphan_here(const Output_section* os,
1833 bool* exact) const
1834 {
1835 if (this->os_->type() == os->type()
1836 && this->os_->flags() == os->flags())
1837 {
1838 *exact = true;
1839 return true;
1840 }
1841 return false;
1842 }
1843
1844 // Set section addresses.
1845
1846 void
1847 Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
1848 bool* dot_has_value,
1849 uint64_t* dot_value)
1850 {
1851 typedef std::list<std::pair<Relobj*, unsigned int> > Input_section_list;
1852
1853 if (!*dot_has_value)
1854 gold_error(_("no address for orphan section %s"), this->os_->name());
1855
1856 uint64_t address = *dot_value;
1857 address = align_address(address, this->os_->addralign());
1858
1859 if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
1860 this->os_->set_address(address);
1861
1862 Input_section_list input_sections;
1863 address += this->os_->get_input_sections(address, "", &input_sections);
1864
1865 for (Input_section_list::iterator p = input_sections.begin();
1866 p != input_sections.end();
1867 ++p)
1868 {
1869 uint64_t addralign;
1870 uint64_t size;
1871
1872 // We know what are single-threaded, so it is OK to lock the
1873 // object.
1874 {
1875 const Task* task = reinterpret_cast<const Task*>(-1);
1876 Task_lock_obj<Object> tl(task, p->first);
1877 addralign = p->first->section_addralign(p->second);
1878 size = p->first->section_size(p->second);
1879 }
1880
1881 address = align_address(address, addralign);
1882 this->os_->add_input_section_for_script(p->first, p->second, size, 0);
1883 address += size;
1884 }
1885
1886 *dot_value = address;
1887 }
1888
1889 // Get the list of segments to use for an allocated section when using
1890 // a PHDRS clause. If this is an allocated section, return the
1891 // Output_section. We don't change the list of segments.
1892
1893 Output_section*
1894 Orphan_output_section::allocate_to_segment(String_list**)
1895 {
1896 if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
1897 return NULL;
1898 return this->os_;
1899 }
1900
1901 // Class Phdrs_element. A program header from a PHDRS clause.
1902
1903 class Phdrs_element
1904 {
1905 public:
1906 Phdrs_element(const char* name, size_t namelen, unsigned int type,
1907 bool includes_filehdr, bool includes_phdrs,
1908 bool is_flags_valid, unsigned int flags,
1909 Expression* load_address)
1910 : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
1911 includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
1912 flags_(flags), load_address_(load_address), load_address_value_(0),
1913 segment_(NULL)
1914 { }
1915
1916 // Return the name of this segment.
1917 const std::string&
1918 name() const
1919 { return this->name_; }
1920
1921 // Return the type of the segment.
1922 unsigned int
1923 type() const
1924 { return this->type_; }
1925
1926 // Whether to include the file header.
1927 bool
1928 includes_filehdr() const
1929 { return this->includes_filehdr_; }
1930
1931 // Whether to include the program headers.
1932 bool
1933 includes_phdrs() const
1934 { return this->includes_phdrs_; }
1935
1936 // Return whether there is a load address.
1937 bool
1938 has_load_address() const
1939 { return this->load_address_ != NULL; }
1940
1941 // Evaluate the load address expression if there is one.
1942 void
1943 eval_load_address(Symbol_table* symtab, Layout* layout)
1944 {
1945 if (this->load_address_ != NULL)
1946 this->load_address_value_ = this->load_address_->eval(symtab, layout);
1947 }
1948
1949 // Return the load address.
1950 uint64_t
1951 load_address() const
1952 {
1953 gold_assert(this->load_address_ != NULL);
1954 return this->load_address_value_;
1955 }
1956
1957 // Create the segment.
1958 Output_segment*
1959 create_segment(Layout* layout)
1960 {
1961 this->segment_ = layout->make_output_segment(this->type_, this->flags_);
1962 return this->segment_;
1963 }
1964
1965 // Return the segment.
1966 Output_segment*
1967 segment()
1968 { return this->segment_; }
1969
1970 // Set the segment flags if appropriate.
1971 void
1972 set_flags_if_valid()
1973 {
1974 if (this->is_flags_valid_)
1975 this->segment_->set_flags(this->flags_);
1976 }
1977
1978 // Print for debugging.
1979 void
1980 print(FILE*) const;
1981
1982 private:
1983 // The name used in the script.
1984 std::string name_;
1985 // The type of the segment (PT_LOAD, etc.).
1986 unsigned int type_;
1987 // Whether this segment includes the file header.
1988 bool includes_filehdr_;
1989 // Whether this segment includes the section headers.
1990 bool includes_phdrs_;
1991 // Whether the flags were explicitly specified.
1992 bool is_flags_valid_;
1993 // The flags for this segment (PF_R, etc.) if specified.
1994 unsigned int flags_;
1995 // The expression for the load address for this segment. This may
1996 // be NULL.
1997 Expression* load_address_;
1998 // The actual load address from evaluating the expression.
1999 uint64_t load_address_value_;
2000 // The segment itself.
2001 Output_segment* segment_;
2002 };
2003
2004 // Print for debugging.
2005
2006 void
2007 Phdrs_element::print(FILE* f) const
2008 {
2009 fprintf(f, " %s 0x%x", this->name_.c_str(), this->type_);
2010 if (this->includes_filehdr_)
2011 fprintf(f, " FILEHDR");
2012 if (this->includes_phdrs_)
2013 fprintf(f, " PHDRS");
2014 if (this->is_flags_valid_)
2015 fprintf(f, " FLAGS(%u)", this->flags_);
2016 if (this->load_address_ != NULL)
2017 {
2018 fprintf(f, " AT(");
2019 this->load_address_->print(f);
2020 fprintf(f, ")");
2021 }
2022 fprintf(f, ";\n");
2023 }
2024
2025 // Class Script_sections.
2026
2027 Script_sections::Script_sections()
2028 : saw_sections_clause_(false),
2029 in_sections_clause_(false),
2030 sections_elements_(NULL),
2031 output_section_(NULL),
2032 phdrs_elements_(NULL)
2033 {
2034 }
2035
2036 // Start a SECTIONS clause.
2037
2038 void
2039 Script_sections::start_sections()
2040 {
2041 gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
2042 this->saw_sections_clause_ = true;
2043 this->in_sections_clause_ = true;
2044 if (this->sections_elements_ == NULL)
2045 this->sections_elements_ = new Sections_elements;
2046 }
2047
2048 // Finish a SECTIONS clause.
2049
2050 void
2051 Script_sections::finish_sections()
2052 {
2053 gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
2054 this->in_sections_clause_ = false;
2055 }
2056
2057 // Add a symbol to be defined.
2058
2059 void
2060 Script_sections::add_symbol_assignment(const char* name, size_t length,
2061 Expression* val, bool provide,
2062 bool hidden)
2063 {
2064 if (this->output_section_ != NULL)
2065 this->output_section_->add_symbol_assignment(name, length, val,
2066 provide, hidden);
2067 else
2068 {
2069 Sections_element* p = new Sections_element_assignment(name, length,
2070 val, provide,
2071 hidden);
2072 this->sections_elements_->push_back(p);
2073 }
2074 }
2075
2076 // Add an assignment to the special dot symbol.
2077
2078 void
2079 Script_sections::add_dot_assignment(Expression* val)
2080 {
2081 if (this->output_section_ != NULL)
2082 this->output_section_->add_dot_assignment(val);
2083 else
2084 {
2085 Sections_element* p = new Sections_element_dot_assignment(val);
2086 this->sections_elements_->push_back(p);
2087 }
2088 }
2089
2090 // Add an assertion.
2091
2092 void
2093 Script_sections::add_assertion(Expression* check, const char* message,
2094 size_t messagelen)
2095 {
2096 if (this->output_section_ != NULL)
2097 this->output_section_->add_assertion(check, message, messagelen);
2098 else
2099 {
2100 Sections_element* p = new Sections_element_assertion(check, message,
2101 messagelen);
2102 this->sections_elements_->push_back(p);
2103 }
2104 }
2105
2106 // Start processing entries for an output section.
2107
2108 void
2109 Script_sections::start_output_section(
2110 const char* name,
2111 size_t namelen,
2112 const Parser_output_section_header *header)
2113 {
2114 Output_section_definition* posd = new Output_section_definition(name,
2115 namelen,
2116 header);
2117 this->sections_elements_->push_back(posd);
2118 gold_assert(this->output_section_ == NULL);
2119 this->output_section_ = posd;
2120 }
2121
2122 // Stop processing entries for an output section.
2123
2124 void
2125 Script_sections::finish_output_section(
2126 const Parser_output_section_trailer* trailer)
2127 {
2128 gold_assert(this->output_section_ != NULL);
2129 this->output_section_->finish(trailer);
2130 this->output_section_ = NULL;
2131 }
2132
2133 // Add a data item to the current output section.
2134
2135 void
2136 Script_sections::add_data(int size, bool is_signed, Expression* val)
2137 {
2138 gold_assert(this->output_section_ != NULL);
2139 this->output_section_->add_data(size, is_signed, val);
2140 }
2141
2142 // Add a fill value setting to the current output section.
2143
2144 void
2145 Script_sections::add_fill(Expression* val)
2146 {
2147 gold_assert(this->output_section_ != NULL);
2148 this->output_section_->add_fill(val);
2149 }
2150
2151 // Add an input section specification to the current output section.
2152
2153 void
2154 Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
2155 {
2156 gold_assert(this->output_section_ != NULL);
2157 this->output_section_->add_input_section(spec, keep);
2158 }
2159
2160 // Add any symbols we are defining to the symbol table.
2161
2162 void
2163 Script_sections::add_symbols_to_table(Symbol_table* symtab)
2164 {
2165 if (!this->saw_sections_clause_)
2166 return;
2167 for (Sections_elements::iterator p = this->sections_elements_->begin();
2168 p != this->sections_elements_->end();
2169 ++p)
2170 (*p)->add_symbols_to_table(symtab);
2171 }
2172
2173 // Finalize symbols and check assertions.
2174
2175 void
2176 Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
2177 {
2178 if (!this->saw_sections_clause_)
2179 return;
2180 bool dot_has_value = false;
2181 uint64_t dot_value = 0;
2182 for (Sections_elements::iterator p = this->sections_elements_->begin();
2183 p != this->sections_elements_->end();
2184 ++p)
2185 (*p)->finalize_symbols(symtab, layout, &dot_has_value, &dot_value);
2186 }
2187
2188 // Return the name of the output section to use for an input file name
2189 // and section name.
2190
2191 const char*
2192 Script_sections::output_section_name(const char* file_name,
2193 const char* section_name,
2194 Output_section*** output_section_slot)
2195 {
2196 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2197 p != this->sections_elements_->end();
2198 ++p)
2199 {
2200 const char* ret = (*p)->output_section_name(file_name, section_name,
2201 output_section_slot);
2202
2203 if (ret != NULL)
2204 {
2205 // The special name /DISCARD/ means that the input section
2206 // should be discarded.
2207 if (strcmp(ret, "/DISCARD/") == 0)
2208 {
2209 *output_section_slot = NULL;
2210 return NULL;
2211 }
2212 return ret;
2213 }
2214 }
2215
2216 // If we couldn't find a mapping for the name, the output section
2217 // gets the name of the input section.
2218
2219 *output_section_slot = NULL;
2220
2221 return section_name;
2222 }
2223
2224 // Place a marker for an orphan output section into the SECTIONS
2225 // clause.
2226
2227 void
2228 Script_sections::place_orphan(Output_section* os)
2229 {
2230 // Look for an output section definition which matches the output
2231 // section. Put a marker after that section.
2232 Sections_elements::iterator place = this->sections_elements_->end();
2233 for (Sections_elements::iterator p = this->sections_elements_->begin();
2234 p != this->sections_elements_->end();
2235 ++p)
2236 {
2237 bool exact;
2238 if ((*p)->place_orphan_here(os, &exact))
2239 {
2240 place = p;
2241 if (exact)
2242 break;
2243 }
2244 }
2245
2246 // The insert function puts the new element before the iterator.
2247 if (place != this->sections_elements_->end())
2248 ++place;
2249
2250 this->sections_elements_->insert(place, new Orphan_output_section(os));
2251 }
2252
2253 // Set the addresses of all the output sections. Walk through all the
2254 // elements, tracking the dot symbol. Apply assignments which set
2255 // absolute symbol values, in case they are used when setting dot.
2256 // Fill in data statement values. As we find output sections, set the
2257 // address, set the address of all associated input sections, and
2258 // update dot. Return the segment which should hold the file header
2259 // and segment headers, if any.
2260
2261 Output_segment*
2262 Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
2263 {
2264 gold_assert(this->saw_sections_clause_);
2265
2266 // Implement ONLY_IF_RO/ONLY_IF_RW constraints. These are a pain
2267 // for our representation.
2268 for (Sections_elements::iterator p = this->sections_elements_->begin();
2269 p != this->sections_elements_->end();
2270 ++p)
2271 {
2272 Output_section_definition* posd;
2273 Section_constraint failed_constraint = (*p)->check_constraint(&posd);
2274 if (failed_constraint != CONSTRAINT_NONE)
2275 {
2276 Sections_elements::iterator q;
2277 for (q = this->sections_elements_->begin();
2278 q != this->sections_elements_->end();
2279 ++q)
2280 {
2281 if (q != p)
2282 {
2283 if ((*q)->alternate_constraint(posd, failed_constraint))
2284 break;
2285 }
2286 }
2287
2288 if (q == this->sections_elements_->end())
2289 gold_error(_("no matching section constraint"));
2290 }
2291 }
2292
2293 bool dot_has_value = false;
2294 uint64_t dot_value = 0;
2295 for (Sections_elements::iterator p = this->sections_elements_->begin();
2296 p != this->sections_elements_->end();
2297 ++p)
2298 (*p)->set_section_addresses(symtab, layout, &dot_has_value, &dot_value);
2299
2300 if (this->phdrs_elements_ != NULL)
2301 {
2302 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2303 p != this->phdrs_elements_->end();
2304 ++p)
2305 (*p)->eval_load_address(symtab, layout);
2306 }
2307
2308 return this->create_segments(layout);
2309 }
2310
2311 // Sort the sections in order to put them into segments.
2312
2313 class Sort_output_sections
2314 {
2315 public:
2316 bool
2317 operator()(const Output_section* os1, const Output_section* os2) const;
2318 };
2319
2320 bool
2321 Sort_output_sections::operator()(const Output_section* os1,
2322 const Output_section* os2) const
2323 {
2324 // Sort first by the load address.
2325 uint64_t lma1 = (os1->has_load_address()
2326 ? os1->load_address()
2327 : os1->address());
2328 uint64_t lma2 = (os2->has_load_address()
2329 ? os2->load_address()
2330 : os2->address());
2331 if (lma1 != lma2)
2332 return lma1 < lma2;
2333
2334 // Then sort by the virtual address.
2335 if (os1->address() != os2->address())
2336 return os1->address() < os2->address();
2337
2338 // Sort TLS sections to the end.
2339 bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
2340 bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
2341 if (tls1 != tls2)
2342 return tls2;
2343
2344 // Sort PROGBITS before NOBITS.
2345 if (os1->type() == elfcpp::SHT_PROGBITS && os2->type() == elfcpp::SHT_NOBITS)
2346 return true;
2347 if (os1->type() == elfcpp::SHT_NOBITS && os2->type() == elfcpp::SHT_PROGBITS)
2348 return false;
2349
2350 // Otherwise we don't care.
2351 return false;
2352 }
2353
2354 // Return whether OS is a BSS section. This is a SHT_NOBITS section.
2355 // We treat a section with the SHF_TLS flag set as taking up space
2356 // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
2357 // space for them in the file.
2358
2359 bool
2360 Script_sections::is_bss_section(const Output_section* os)
2361 {
2362 return (os->type() == elfcpp::SHT_NOBITS
2363 && (os->flags() & elfcpp::SHF_TLS) == 0);
2364 }
2365
2366 // Return the size taken by the file header and the program headers.
2367
2368 size_t
2369 Script_sections::total_header_size(Layout* layout) const
2370 {
2371 size_t segment_count = layout->segment_count();
2372 size_t file_header_size;
2373 size_t segment_headers_size;
2374 if (parameters->get_size() == 32)
2375 {
2376 file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
2377 segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
2378 }
2379 else if (parameters->get_size() == 64)
2380 {
2381 file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
2382 segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
2383 }
2384 else
2385 gold_unreachable();
2386
2387 return file_header_size + segment_headers_size;
2388 }
2389
2390 // Return the amount we have to subtract from the LMA to accomodate
2391 // headers of the given size. The complication is that the file
2392 // header have to be at the start of a page, as otherwise it will not
2393 // be at the start of the file.
2394
2395 uint64_t
2396 Script_sections::header_size_adjustment(uint64_t lma,
2397 size_t sizeof_headers) const
2398 {
2399 const uint64_t abi_pagesize = parameters->target()->abi_pagesize();
2400 uint64_t hdr_lma = lma - sizeof_headers;
2401 hdr_lma &= ~(abi_pagesize - 1);
2402 return lma - hdr_lma;
2403 }
2404
2405 // Create the PT_LOAD segments when using a SECTIONS clause. Returns
2406 // the segment which should hold the file header and segment headers,
2407 // if any.
2408
2409 Output_segment*
2410 Script_sections::create_segments(Layout* layout)
2411 {
2412 gold_assert(this->saw_sections_clause_);
2413
2414 if (parameters->output_is_object())
2415 return NULL;
2416
2417 if (this->saw_phdrs_clause())
2418 return create_segments_from_phdrs_clause(layout);
2419
2420 Layout::Section_list sections;
2421 layout->get_allocated_sections(&sections);
2422
2423 // Sort the sections by address.
2424 std::stable_sort(sections.begin(), sections.end(), Sort_output_sections());
2425
2426 this->create_note_and_tls_segments(layout, &sections);
2427
2428 // Walk through the sections adding them to PT_LOAD segments.
2429 const uint64_t abi_pagesize = parameters->target()->abi_pagesize();
2430 Output_segment* first_seg = NULL;
2431 Output_segment* current_seg = NULL;
2432 bool is_current_seg_readonly = true;
2433 Layout::Section_list::iterator plast = sections.end();
2434 uint64_t last_vma = 0;
2435 uint64_t last_lma = 0;
2436 uint64_t last_size = 0;
2437 for (Layout::Section_list::iterator p = sections.begin();
2438 p != sections.end();
2439 ++p)
2440 {
2441 const uint64_t vma = (*p)->address();
2442 const uint64_t lma = ((*p)->has_load_address()
2443 ? (*p)->load_address()
2444 : vma);
2445 const uint64_t size = (*p)->current_data_size();
2446
2447 bool need_new_segment;
2448 if (current_seg == NULL)
2449 need_new_segment = true;
2450 else if (lma - vma != last_lma - last_vma)
2451 {
2452 // This section has a different LMA relationship than the
2453 // last one; we need a new segment.
2454 need_new_segment = true;
2455 }
2456 else if (align_address(last_lma + last_size, abi_pagesize)
2457 < align_address(lma, abi_pagesize))
2458 {
2459 // Putting this section in the segment would require
2460 // skipping a page.
2461 need_new_segment = true;
2462 }
2463 else if (is_bss_section(*plast) && !is_bss_section(*p))
2464 {
2465 // A non-BSS section can not follow a BSS section in the
2466 // same segment.
2467 need_new_segment = true;
2468 }
2469 else if (is_current_seg_readonly
2470 && ((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2471 {
2472 // Don't put a writable section in the same segment as a
2473 // non-writable section.
2474 need_new_segment = true;
2475 }
2476 else
2477 {
2478 // Otherwise, reuse the existing segment.
2479 need_new_segment = false;
2480 }
2481
2482 elfcpp::Elf_Word seg_flags =
2483 Layout::section_flags_to_segment((*p)->flags());
2484
2485 if (need_new_segment)
2486 {
2487 current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2488 seg_flags);
2489 current_seg->set_addresses(vma, lma);
2490 if (first_seg == NULL)
2491 first_seg = current_seg;
2492 is_current_seg_readonly = true;
2493 }
2494
2495 current_seg->add_output_section(*p, seg_flags);
2496
2497 if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
2498 is_current_seg_readonly = false;
2499
2500 plast = p;
2501 last_vma = vma;
2502 last_lma = lma;
2503 last_size = size;
2504 }
2505
2506 // An ELF program should work even if the program headers are not in
2507 // a PT_LOAD segment. However, it appears that the Linux kernel
2508 // does not set the AT_PHDR auxiliary entry in that case. It sets
2509 // the load address to p_vaddr - p_offset of the first PT_LOAD
2510 // segment. It then sets AT_PHDR to the load address plus the
2511 // offset to the program headers, e_phoff in the file header. This
2512 // fails when the program headers appear in the file before the
2513 // first PT_LOAD segment. Therefore, we always create a PT_LOAD
2514 // segment to hold the file header and the program headers. This is
2515 // effectively what the GNU linker does, and it is slightly more
2516 // efficient in any case. We try to use the first PT_LOAD segment
2517 // if we can, otherwise we make a new one.
2518
2519 size_t sizeof_headers = this->total_header_size(layout);
2520
2521 if (first_seg != NULL
2522 && (first_seg->paddr() & (abi_pagesize - 1)) >= sizeof_headers)
2523 {
2524 first_seg->set_addresses(first_seg->vaddr() - sizeof_headers,
2525 first_seg->paddr() - sizeof_headers);
2526 return first_seg;
2527 }
2528
2529 Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
2530 elfcpp::PF_R);
2531 if (first_seg == NULL)
2532 load_seg->set_addresses(0, 0);
2533 else
2534 {
2535 uint64_t vma = first_seg->vaddr();
2536 uint64_t lma = first_seg->paddr();
2537
2538 uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
2539 if (lma >= subtract && vma >= subtract)
2540 load_seg->set_addresses(vma - subtract, lma - subtract);
2541 else
2542 {
2543 // We could handle this case by create the file header
2544 // outside of any PT_LOAD segment, and creating a new
2545 // PT_LOAD segment after the others to hold the segment
2546 // headers.
2547 gold_error(_("sections loaded on first page without room for "
2548 "file and program headers are not supported"));
2549 }
2550 }
2551
2552 return load_seg;
2553 }
2554
2555 // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
2556 // segment if there are any SHT_TLS sections.
2557
2558 void
2559 Script_sections::create_note_and_tls_segments(
2560 Layout* layout,
2561 const Layout::Section_list* sections)
2562 {
2563 gold_assert(!this->saw_phdrs_clause());
2564
2565 bool saw_tls = false;
2566 for (Layout::Section_list::const_iterator p = sections->begin();
2567 p != sections->end();
2568 ++p)
2569 {
2570 if ((*p)->type() == elfcpp::SHT_NOTE)
2571 {
2572 elfcpp::Elf_Word seg_flags =
2573 Layout::section_flags_to_segment((*p)->flags());
2574 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
2575 seg_flags);
2576 oseg->add_output_section(*p, seg_flags);
2577
2578 // Incorporate any subsequent SHT_NOTE sections, in the
2579 // hopes that the script is sensible.
2580 Layout::Section_list::const_iterator pnext = p + 1;
2581 while (pnext != sections->end()
2582 && (*pnext)->type() == elfcpp::SHT_NOTE)
2583 {
2584 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2585 oseg->add_output_section(*pnext, seg_flags);
2586 p = pnext;
2587 ++pnext;
2588 }
2589 }
2590
2591 if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2592 {
2593 if (saw_tls)
2594 gold_error(_("TLS sections are not adjacent"));
2595
2596 elfcpp::Elf_Word seg_flags =
2597 Layout::section_flags_to_segment((*p)->flags());
2598 Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
2599 seg_flags);
2600 oseg->add_output_section(*p, seg_flags);
2601
2602 Layout::Section_list::const_iterator pnext = p + 1;
2603 while (pnext != sections->end()
2604 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
2605 {
2606 seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
2607 oseg->add_output_section(*pnext, seg_flags);
2608 p = pnext;
2609 ++pnext;
2610 }
2611
2612 saw_tls = true;
2613 }
2614 }
2615 }
2616
2617 // Add a program header. The PHDRS clause is syntactically distinct
2618 // from the SECTIONS clause, but we implement it with the SECTIONS
2619 // support becauase PHDRS is useless if there is no SECTIONS clause.
2620
2621 void
2622 Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
2623 bool includes_filehdr, bool includes_phdrs,
2624 bool is_flags_valid, unsigned int flags,
2625 Expression* load_address)
2626 {
2627 if (this->phdrs_elements_ == NULL)
2628 this->phdrs_elements_ = new Phdrs_elements();
2629 this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
2630 includes_filehdr,
2631 includes_phdrs,
2632 is_flags_valid, flags,
2633 load_address));
2634 }
2635
2636 // Return the number of segments we expect to create based on the
2637 // SECTIONS clause. This is used to implement SIZEOF_HEADERS.
2638
2639 size_t
2640 Script_sections::expected_segment_count(const Layout* layout) const
2641 {
2642 if (this->saw_phdrs_clause())
2643 return this->phdrs_elements_->size();
2644
2645 Layout::Section_list sections;
2646 layout->get_allocated_sections(&sections);
2647
2648 // We assume that we will need two PT_LOAD segments.
2649 size_t ret = 2;
2650
2651 bool saw_note = false;
2652 bool saw_tls = false;
2653 for (Layout::Section_list::const_iterator p = sections.begin();
2654 p != sections.end();
2655 ++p)
2656 {
2657 if ((*p)->type() == elfcpp::SHT_NOTE)
2658 {
2659 // Assume that all note sections will fit into a single
2660 // PT_NOTE segment.
2661 if (!saw_note)
2662 {
2663 ++ret;
2664 saw_note = true;
2665 }
2666 }
2667 else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
2668 {
2669 // There can only be one PT_TLS segment.
2670 if (!saw_tls)
2671 {
2672 ++ret;
2673 saw_tls = true;
2674 }
2675 }
2676 }
2677
2678 return ret;
2679 }
2680
2681 // Create the segments from a PHDRS clause. Return the segment which
2682 // should hold the file header and program headers, if any.
2683
2684 Output_segment*
2685 Script_sections::create_segments_from_phdrs_clause(Layout* layout)
2686 {
2687 this->attach_sections_using_phdrs_clause(layout);
2688 return this->set_phdrs_clause_addresses(layout);
2689 }
2690
2691 // Create the segments from the PHDRS clause, and put the output
2692 // sections in them.
2693
2694 void
2695 Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
2696 {
2697 typedef std::map<std::string, Output_segment*> Name_to_segment;
2698 Name_to_segment name_to_segment;
2699 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2700 p != this->phdrs_elements_->end();
2701 ++p)
2702 name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
2703
2704 // Walk through the output sections and attach them to segments.
2705 // Output sections in the script which do not list segments are
2706 // attached to the same set of segments as the immediately preceding
2707 // output section.
2708 String_list* phdr_names = NULL;
2709 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2710 p != this->sections_elements_->end();
2711 ++p)
2712 {
2713 Output_section* os = (*p)->allocate_to_segment(&phdr_names);
2714 if (os == NULL)
2715 continue;
2716
2717 if (phdr_names == NULL)
2718 {
2719 gold_error(_("allocated section not in any segment"));
2720 continue;
2721 }
2722
2723 bool in_load_segment = false;
2724 for (String_list::const_iterator q = phdr_names->begin();
2725 q != phdr_names->end();
2726 ++q)
2727 {
2728 Name_to_segment::const_iterator r = name_to_segment.find(*q);
2729 if (r == name_to_segment.end())
2730 gold_error(_("no segment %s"), q->c_str());
2731 else
2732 {
2733 elfcpp::Elf_Word seg_flags =
2734 Layout::section_flags_to_segment(os->flags());
2735 r->second->add_output_section(os, seg_flags);
2736
2737 if (r->second->type() == elfcpp::PT_LOAD)
2738 {
2739 if (in_load_segment)
2740 gold_error(_("section in two PT_LOAD segments"));
2741 in_load_segment = true;
2742 }
2743 }
2744 }
2745
2746 if (!in_load_segment)
2747 gold_error(_("allocated section not in any PT_LOAD segment"));
2748 }
2749 }
2750
2751 // Set the addresses for segments created from a PHDRS clause. Return
2752 // the segment which should hold the file header and program headers,
2753 // if any.
2754
2755 Output_segment*
2756 Script_sections::set_phdrs_clause_addresses(Layout* layout)
2757 {
2758 Output_segment* load_seg = NULL;
2759 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2760 p != this->phdrs_elements_->end();
2761 ++p)
2762 {
2763 // Note that we have to set the flags after adding the output
2764 // sections to the segment, as adding an output segment can
2765 // change the flags.
2766 (*p)->set_flags_if_valid();
2767
2768 Output_segment* oseg = (*p)->segment();
2769
2770 if (oseg->type() != elfcpp::PT_LOAD)
2771 {
2772 // The addresses of non-PT_LOAD segments are set from the
2773 // PT_LOAD segments.
2774 if ((*p)->has_load_address())
2775 gold_error(_("may only specify load address for PT_LOAD segment"));
2776 continue;
2777 }
2778
2779 // The output sections should have addresses from the SECTIONS
2780 // clause. The addresses don't have to be in order, so find the
2781 // one with the lowest load address. Use that to set the
2782 // address of the segment.
2783
2784 Output_section* osec = oseg->section_with_lowest_load_address();
2785 if (osec == NULL)
2786 {
2787 oseg->set_addresses(0, 0);
2788 continue;
2789 }
2790
2791 uint64_t vma = osec->address();
2792 uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
2793
2794 // Override the load address of the section with the load
2795 // address specified for the segment.
2796 if ((*p)->has_load_address())
2797 {
2798 if (osec->has_load_address())
2799 gold_warning(_("PHDRS load address overrides "
2800 "section %s load address"),
2801 osec->name());
2802
2803 lma = (*p)->load_address();
2804 }
2805
2806 bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
2807 if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
2808 {
2809 // We could support this if we wanted to.
2810 gold_error(_("using only one of FILEHDR and PHDRS is "
2811 "not currently supported"));
2812 }
2813 if (headers)
2814 {
2815 size_t sizeof_headers = this->total_header_size(layout);
2816 uint64_t subtract = this->header_size_adjustment(lma,
2817 sizeof_headers);
2818 if (lma >= subtract && vma >= subtract)
2819 {
2820 lma -= subtract;
2821 vma -= subtract;
2822 }
2823 else
2824 {
2825 gold_error(_("sections loaded on first page without room "
2826 "for file and program headers "
2827 "are not supported"));
2828 }
2829
2830 if (load_seg != NULL)
2831 gold_error(_("using FILEHDR and PHDRS on more than one "
2832 "PT_LOAD segment is not currently supported"));
2833 load_seg = oseg;
2834 }
2835
2836 oseg->set_addresses(vma, lma);
2837 }
2838
2839 return load_seg;
2840 }
2841
2842 // Add the file header and segment headers to non-load segments
2843 // specified in the PHDRS clause.
2844
2845 void
2846 Script_sections::put_headers_in_phdrs(Output_data* file_header,
2847 Output_data* segment_headers)
2848 {
2849 gold_assert(this->saw_phdrs_clause());
2850 for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
2851 p != this->phdrs_elements_->end();
2852 ++p)
2853 {
2854 if ((*p)->type() != elfcpp::PT_LOAD)
2855 {
2856 if ((*p)->includes_phdrs())
2857 (*p)->segment()->add_initial_output_data(segment_headers);
2858 if ((*p)->includes_filehdr())
2859 (*p)->segment()->add_initial_output_data(file_header);
2860 }
2861 }
2862 }
2863
2864 // Print the SECTIONS clause to F for debugging.
2865
2866 void
2867 Script_sections::print(FILE* f) const
2868 {
2869 if (!this->saw_sections_clause_)
2870 return;
2871
2872 fprintf(f, "SECTIONS {\n");
2873
2874 for (Sections_elements::const_iterator p = this->sections_elements_->begin();
2875 p != this->sections_elements_->end();
2876 ++p)
2877 (*p)->print(f);
2878
2879 fprintf(f, "}\n");
2880
2881 if (this->phdrs_elements_ != NULL)
2882 {
2883 fprintf(f, "PHDRS {\n");
2884 for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
2885 p != this->phdrs_elements_->end();
2886 ++p)
2887 (*p)->print(f);
2888 fprintf(f, "}\n");
2889 }
2890 }
2891
2892 } // End namespace gold.
This page took 0.085653 seconds and 5 git commands to generate.