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