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