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