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