1ed0d50c3Schristos // script-sections.cc -- linker script SECTIONS for gold
2ed0d50c3Schristos 
3*b88e3e88Schristos // Copyright (C) 2008-2020 Free Software Foundation, Inc.
4ed0d50c3Schristos // Written by Ian Lance Taylor <iant@google.com>.
5ed0d50c3Schristos 
6ed0d50c3Schristos // This file is part of gold.
7ed0d50c3Schristos 
8ed0d50c3Schristos // This program is free software; you can redistribute it and/or modify
9ed0d50c3Schristos // it under the terms of the GNU General Public License as published by
10ed0d50c3Schristos // the Free Software Foundation; either version 3 of the License, or
11ed0d50c3Schristos // (at your option) any later version.
12ed0d50c3Schristos 
13ed0d50c3Schristos // This program is distributed in the hope that it will be useful,
14ed0d50c3Schristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
15ed0d50c3Schristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16ed0d50c3Schristos // GNU General Public License for more details.
17ed0d50c3Schristos 
18ed0d50c3Schristos // You should have received a copy of the GNU General Public License
19ed0d50c3Schristos // along with this program; if not, write to the Free Software
20ed0d50c3Schristos // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21ed0d50c3Schristos // MA 02110-1301, USA.
22ed0d50c3Schristos 
23ed0d50c3Schristos #include "gold.h"
24ed0d50c3Schristos 
25ed0d50c3Schristos #include <cstring>
26ed0d50c3Schristos #include <algorithm>
27ed0d50c3Schristos #include <list>
28ed0d50c3Schristos #include <map>
29ed0d50c3Schristos #include <string>
30ed0d50c3Schristos #include <vector>
31ed0d50c3Schristos #include <fnmatch.h>
32ed0d50c3Schristos 
33ed0d50c3Schristos #include "parameters.h"
34ed0d50c3Schristos #include "object.h"
35ed0d50c3Schristos #include "layout.h"
36ed0d50c3Schristos #include "output.h"
37ed0d50c3Schristos #include "script-c.h"
38ed0d50c3Schristos #include "script.h"
39ed0d50c3Schristos #include "script-sections.h"
40ed0d50c3Schristos 
41ed0d50c3Schristos // Support for the SECTIONS clause in linker scripts.
42ed0d50c3Schristos 
43ed0d50c3Schristos namespace gold
44ed0d50c3Schristos {
45ed0d50c3Schristos 
46ed0d50c3Schristos // A region of memory.
47ed0d50c3Schristos class Memory_region
48ed0d50c3Schristos {
49ed0d50c3Schristos  public:
Memory_region(const char * name,size_t namelen,unsigned int attributes,Expression * start,Expression * length)50ed0d50c3Schristos   Memory_region(const char* name, size_t namelen, unsigned int attributes,
51ed0d50c3Schristos 		Expression* start, Expression* length)
52ed0d50c3Schristos     : name_(name, namelen),
53ed0d50c3Schristos       attributes_(attributes),
54ed0d50c3Schristos       start_(start),
55ed0d50c3Schristos       length_(length),
56ed0d50c3Schristos       current_offset_(0),
57ed0d50c3Schristos       vma_sections_(),
58ed0d50c3Schristos       lma_sections_(),
59ed0d50c3Schristos       last_section_(NULL)
60ed0d50c3Schristos   { }
61ed0d50c3Schristos 
62ed0d50c3Schristos   // Return the name of this region.
63ed0d50c3Schristos   const std::string&
name() const64ed0d50c3Schristos   name() const
65ed0d50c3Schristos   { return this->name_; }
66ed0d50c3Schristos 
67ed0d50c3Schristos   // Return the start address of this region.
68ed0d50c3Schristos   Expression*
start_address() const69ed0d50c3Schristos   start_address() const
70ed0d50c3Schristos   { return this->start_; }
71ed0d50c3Schristos 
72ed0d50c3Schristos   // Return the length of this region.
73ed0d50c3Schristos   Expression*
length() const74ed0d50c3Schristos   length() const
75ed0d50c3Schristos   { return this->length_; }
76ed0d50c3Schristos 
77ed0d50c3Schristos   // Print the region (when debugging).
78ed0d50c3Schristos   void
79ed0d50c3Schristos   print(FILE*) const;
80ed0d50c3Schristos 
81ed0d50c3Schristos   // Return true if <name,namelen> matches this region.
82ed0d50c3Schristos   bool
name_match(const char * name,size_t namelen)83ed0d50c3Schristos   name_match(const char* name, size_t namelen)
84ed0d50c3Schristos   {
85ed0d50c3Schristos     return (this->name_.length() == namelen
86ed0d50c3Schristos 	    && strncmp(this->name_.c_str(), name, namelen) == 0);
87ed0d50c3Schristos   }
88ed0d50c3Schristos 
89ed0d50c3Schristos   Expression*
get_current_address() const90ed0d50c3Schristos   get_current_address() const
91ed0d50c3Schristos   {
92ed0d50c3Schristos     return
93ed0d50c3Schristos       script_exp_binary_add(this->start_,
94ed0d50c3Schristos 			    script_exp_integer(this->current_offset_));
95ed0d50c3Schristos   }
96ed0d50c3Schristos 
97ed0d50c3Schristos   void
set_address(uint64_t addr,const Symbol_table * symtab,const Layout * layout)98ed0d50c3Schristos   set_address(uint64_t addr, const Symbol_table* symtab, const Layout* layout)
99ed0d50c3Schristos   {
100ed0d50c3Schristos     uint64_t start = this->start_->eval(symtab, layout, false);
101ed0d50c3Schristos     uint64_t len = this->length_->eval(symtab, layout, false);
102ed0d50c3Schristos     if (addr < start || addr >= start + len)
103ed0d50c3Schristos       gold_error(_("address 0x%llx is not within region %s"),
104ed0d50c3Schristos 		 static_cast<unsigned long long>(addr),
105ed0d50c3Schristos 		 this->name_.c_str());
106ed0d50c3Schristos     else if (addr < start + this->current_offset_)
107ed0d50c3Schristos       gold_error(_("address 0x%llx moves dot backwards in region %s"),
108ed0d50c3Schristos 		 static_cast<unsigned long long>(addr),
109ed0d50c3Schristos 		 this->name_.c_str());
110ed0d50c3Schristos     this->current_offset_ = addr - start;
111ed0d50c3Schristos   }
112ed0d50c3Schristos 
113ed0d50c3Schristos   void
increment_offset(std::string section_name,uint64_t amount,const Symbol_table * symtab,const Layout * layout)114ed0d50c3Schristos   increment_offset(std::string section_name, uint64_t amount,
115ed0d50c3Schristos 		   const Symbol_table* symtab, const Layout* layout)
116ed0d50c3Schristos   {
117ed0d50c3Schristos     this->current_offset_ += amount;
118ed0d50c3Schristos 
119ed0d50c3Schristos     if (this->current_offset_
120ed0d50c3Schristos 	> this->length_->eval(symtab, layout, false))
121ed0d50c3Schristos       gold_error(_("section %s overflows end of region %s"),
122ed0d50c3Schristos 		 section_name.c_str(), this->name_.c_str());
123ed0d50c3Schristos   }
124ed0d50c3Schristos 
125ed0d50c3Schristos   // Returns true iff there is room left in this region
126ed0d50c3Schristos   // for AMOUNT more bytes of data.
127ed0d50c3Schristos   bool
has_room_for(const Symbol_table * symtab,const Layout * layout,uint64_t amount) const128ed0d50c3Schristos   has_room_for(const Symbol_table* symtab, const Layout* layout,
129ed0d50c3Schristos 	       uint64_t amount) const
130ed0d50c3Schristos   {
131ed0d50c3Schristos     return (this->current_offset_ + amount
132ed0d50c3Schristos 	    < this->length_->eval(symtab, layout, false));
133ed0d50c3Schristos   }
134ed0d50c3Schristos 
135ed0d50c3Schristos   // Return true if the provided section flags
136ed0d50c3Schristos   // are compatible with this region's attributes.
137ed0d50c3Schristos   bool
138ed0d50c3Schristos   attributes_compatible(elfcpp::Elf_Xword flags, elfcpp::Elf_Xword type) const;
139ed0d50c3Schristos 
140ed0d50c3Schristos   void
add_section(Output_section_definition * sec,bool vma)141ed0d50c3Schristos   add_section(Output_section_definition* sec, bool vma)
142ed0d50c3Schristos   {
143ed0d50c3Schristos     if (vma)
144ed0d50c3Schristos       this->vma_sections_.push_back(sec);
145ed0d50c3Schristos     else
146ed0d50c3Schristos       this->lma_sections_.push_back(sec);
147ed0d50c3Schristos   }
148ed0d50c3Schristos 
149ed0d50c3Schristos   typedef std::vector<Output_section_definition*> Section_list;
150ed0d50c3Schristos 
151ed0d50c3Schristos   // Return the start of the list of sections
152ed0d50c3Schristos   // whose VMAs are taken from this region.
153ed0d50c3Schristos   Section_list::const_iterator
get_vma_section_list_start() const154ed0d50c3Schristos   get_vma_section_list_start() const
155ed0d50c3Schristos   { return this->vma_sections_.begin(); }
156ed0d50c3Schristos 
157ed0d50c3Schristos   // Return the start of the list of sections
158ed0d50c3Schristos   // whose LMAs are taken from this region.
159ed0d50c3Schristos   Section_list::const_iterator
get_lma_section_list_start() const160ed0d50c3Schristos   get_lma_section_list_start() const
161ed0d50c3Schristos   { return this->lma_sections_.begin(); }
162ed0d50c3Schristos 
163ed0d50c3Schristos   // Return the end of the list of sections
164ed0d50c3Schristos   // whose VMAs are taken from this region.
165ed0d50c3Schristos   Section_list::const_iterator
get_vma_section_list_end() const166ed0d50c3Schristos   get_vma_section_list_end() const
167ed0d50c3Schristos   { return this->vma_sections_.end(); }
168ed0d50c3Schristos 
169ed0d50c3Schristos   // Return the end of the list of sections
170ed0d50c3Schristos   // whose LMAs are taken from this region.
171ed0d50c3Schristos   Section_list::const_iterator
get_lma_section_list_end() const172ed0d50c3Schristos   get_lma_section_list_end() const
173ed0d50c3Schristos   { return this->lma_sections_.end(); }
174ed0d50c3Schristos 
175ed0d50c3Schristos   Output_section_definition*
get_last_section() const176ed0d50c3Schristos   get_last_section() const
177ed0d50c3Schristos   { return this->last_section_; }
178ed0d50c3Schristos 
179ed0d50c3Schristos   void
set_last_section(Output_section_definition * sec)180ed0d50c3Schristos   set_last_section(Output_section_definition* sec)
181ed0d50c3Schristos   { this->last_section_ = sec; }
182ed0d50c3Schristos 
183ed0d50c3Schristos  private:
184ed0d50c3Schristos 
185ed0d50c3Schristos   std::string name_;
186ed0d50c3Schristos   unsigned int attributes_;
187ed0d50c3Schristos   Expression* start_;
188ed0d50c3Schristos   Expression* length_;
189ed0d50c3Schristos   // The offset to the next free byte in the region.
190ed0d50c3Schristos   // Note - for compatibility with GNU LD we only maintain one offset
191ed0d50c3Schristos   // regardless of whether the region is being used for VMA values,
192ed0d50c3Schristos   // LMA values, or both.
193ed0d50c3Schristos   uint64_t current_offset_;
194ed0d50c3Schristos   // A list of sections whose VMAs are set inside this region.
195ed0d50c3Schristos   Section_list vma_sections_;
196ed0d50c3Schristos   // A list of sections whose LMAs are set inside this region.
197ed0d50c3Schristos   Section_list lma_sections_;
198ed0d50c3Schristos   // The latest section to make use of this region.
199ed0d50c3Schristos   Output_section_definition* last_section_;
200ed0d50c3Schristos };
201ed0d50c3Schristos 
202ed0d50c3Schristos // Return true if the provided section flags
203ed0d50c3Schristos // are compatible with this region's attributes.
204ed0d50c3Schristos 
205ed0d50c3Schristos bool
attributes_compatible(elfcpp::Elf_Xword flags,elfcpp::Elf_Xword type) const206ed0d50c3Schristos Memory_region::attributes_compatible(elfcpp::Elf_Xword flags,
207ed0d50c3Schristos 				     elfcpp::Elf_Xword type) const
208ed0d50c3Schristos {
209ed0d50c3Schristos   unsigned int attrs = this->attributes_;
210ed0d50c3Schristos 
211ed0d50c3Schristos   // No attributes means that this region is not compatible with anything.
212ed0d50c3Schristos   if (attrs == 0)
213ed0d50c3Schristos     return false;
214ed0d50c3Schristos 
215ed0d50c3Schristos   bool match = true;
216ed0d50c3Schristos   do
217ed0d50c3Schristos     {
218ed0d50c3Schristos       switch (attrs & - attrs)
219ed0d50c3Schristos 	{
220ed0d50c3Schristos 	case MEM_EXECUTABLE:
221ed0d50c3Schristos 	  if ((flags & elfcpp::SHF_EXECINSTR) == 0)
222ed0d50c3Schristos 	    match = false;
223ed0d50c3Schristos 	  break;
224ed0d50c3Schristos 
225ed0d50c3Schristos 	case MEM_WRITEABLE:
226ed0d50c3Schristos 	  if ((flags & elfcpp::SHF_WRITE) == 0)
227ed0d50c3Schristos 	    match = false;
228ed0d50c3Schristos 	  break;
229ed0d50c3Schristos 
230ed0d50c3Schristos 	case MEM_READABLE:
231ed0d50c3Schristos 	  // All sections are presumed readable.
232ed0d50c3Schristos 	  break;
233ed0d50c3Schristos 
234ed0d50c3Schristos 	case MEM_ALLOCATABLE:
235ed0d50c3Schristos 	  if ((flags & elfcpp::SHF_ALLOC) == 0)
236ed0d50c3Schristos 	    match = false;
237ed0d50c3Schristos 	  break;
238ed0d50c3Schristos 
239ed0d50c3Schristos 	case MEM_INITIALIZED:
240ed0d50c3Schristos 	  if ((type & elfcpp::SHT_NOBITS) != 0)
241ed0d50c3Schristos 	    match = false;
242ed0d50c3Schristos 	  break;
243ed0d50c3Schristos 	}
244ed0d50c3Schristos       attrs &= ~ (attrs & - attrs);
245ed0d50c3Schristos     }
246ed0d50c3Schristos   while (attrs != 0);
247ed0d50c3Schristos 
248ed0d50c3Schristos   return match;
249ed0d50c3Schristos }
250ed0d50c3Schristos 
251ed0d50c3Schristos // Print a memory region.
252ed0d50c3Schristos 
253ed0d50c3Schristos void
print(FILE * f) const254ed0d50c3Schristos Memory_region::print(FILE* f) const
255ed0d50c3Schristos {
256ed0d50c3Schristos   fprintf(f, "  %s", this->name_.c_str());
257ed0d50c3Schristos 
258ed0d50c3Schristos   unsigned int attrs = this->attributes_;
259ed0d50c3Schristos   if (attrs != 0)
260ed0d50c3Schristos     {
261ed0d50c3Schristos       fprintf(f, " (");
262ed0d50c3Schristos       do
263ed0d50c3Schristos 	{
264ed0d50c3Schristos 	  switch (attrs & - attrs)
265ed0d50c3Schristos 	    {
266ed0d50c3Schristos 	    case MEM_EXECUTABLE:  fputc('x', f); break;
267ed0d50c3Schristos 	    case MEM_WRITEABLE:   fputc('w', f); break;
268ed0d50c3Schristos 	    case MEM_READABLE:    fputc('r', f); break;
269ed0d50c3Schristos 	    case MEM_ALLOCATABLE: fputc('a', f); break;
270ed0d50c3Schristos 	    case MEM_INITIALIZED: fputc('i', f); break;
271ed0d50c3Schristos 	    default:
272ed0d50c3Schristos 	      gold_unreachable();
273ed0d50c3Schristos 	    }
274ed0d50c3Schristos 	  attrs &= ~ (attrs & - attrs);
275ed0d50c3Schristos 	}
276ed0d50c3Schristos       while (attrs != 0);
277ed0d50c3Schristos       fputc(')', f);
278ed0d50c3Schristos     }
279ed0d50c3Schristos 
280ed0d50c3Schristos   fprintf(f, " : origin = ");
281ed0d50c3Schristos   this->start_->print(f);
282ed0d50c3Schristos   fprintf(f, ", length = ");
283ed0d50c3Schristos   this->length_->print(f);
284ed0d50c3Schristos   fprintf(f, "\n");
285ed0d50c3Schristos }
286ed0d50c3Schristos 
287ed0d50c3Schristos // Manage orphan sections.  This is intended to be largely compatible
288ed0d50c3Schristos // with the GNU linker.  The Linux kernel implicitly relies on
289ed0d50c3Schristos // something similar to the GNU linker's orphan placement.  We
290ed0d50c3Schristos // originally used a simpler scheme here, but it caused the kernel
291ed0d50c3Schristos // build to fail, and was also rather inefficient.
292ed0d50c3Schristos 
293ed0d50c3Schristos class Orphan_section_placement
294ed0d50c3Schristos {
295ed0d50c3Schristos  private:
296ed0d50c3Schristos   typedef Script_sections::Elements_iterator Elements_iterator;
297ed0d50c3Schristos 
298ed0d50c3Schristos  public:
299ed0d50c3Schristos   Orphan_section_placement();
300ed0d50c3Schristos 
301ed0d50c3Schristos   // Handle an output section during initialization of this mapping.
302ed0d50c3Schristos   void
303ed0d50c3Schristos   output_section_init(const std::string& name, Output_section*,
304ed0d50c3Schristos 		      Elements_iterator location);
305ed0d50c3Schristos 
306ed0d50c3Schristos   // Initialize the last location.
307ed0d50c3Schristos   void
308ed0d50c3Schristos   last_init(Elements_iterator location);
309ed0d50c3Schristos 
310ed0d50c3Schristos   // Set *PWHERE to the address of an iterator pointing to the
311ed0d50c3Schristos   // location to use for an orphan section.  Return true if the
312ed0d50c3Schristos   // iterator has a value, false otherwise.
313ed0d50c3Schristos   bool
314ed0d50c3Schristos   find_place(Output_section*, Elements_iterator** pwhere);
315ed0d50c3Schristos 
31606324dcfSchristos   // Update PLACE_LAST_ALLOC.
31706324dcfSchristos   void
31806324dcfSchristos   update_last_alloc(Elements_iterator where);
31906324dcfSchristos 
320ed0d50c3Schristos   // Return the iterator being used for sections at the very end of
321ed0d50c3Schristos   // the linker script.
322ed0d50c3Schristos   Elements_iterator
323ed0d50c3Schristos   last_place() const;
324ed0d50c3Schristos 
325ed0d50c3Schristos  private:
326ed0d50c3Schristos   // The places that we specifically recognize.  This list is copied
327ed0d50c3Schristos   // from the GNU linker.
328ed0d50c3Schristos   enum Place_index
329ed0d50c3Schristos   {
330ed0d50c3Schristos     PLACE_TEXT,
331ed0d50c3Schristos     PLACE_RODATA,
332ed0d50c3Schristos     PLACE_DATA,
333ed0d50c3Schristos     PLACE_TLS,
334ed0d50c3Schristos     PLACE_TLS_BSS,
335ed0d50c3Schristos     PLACE_BSS,
33606324dcfSchristos     PLACE_LAST_ALLOC,
337ed0d50c3Schristos     PLACE_REL,
338ed0d50c3Schristos     PLACE_INTERP,
339ed0d50c3Schristos     PLACE_NONALLOC,
340ed0d50c3Schristos     PLACE_LAST,
341ed0d50c3Schristos     PLACE_MAX
342ed0d50c3Schristos   };
343ed0d50c3Schristos 
344ed0d50c3Schristos   // The information we keep for a specific place.
345ed0d50c3Schristos   struct Place
346ed0d50c3Schristos   {
347ed0d50c3Schristos     // The name of sections for this place.
348ed0d50c3Schristos     const char* name;
349ed0d50c3Schristos     // Whether we have a location for this place.
350ed0d50c3Schristos     bool have_location;
351ed0d50c3Schristos     // The iterator for this place.
352ed0d50c3Schristos     Elements_iterator location;
353ed0d50c3Schristos   };
354ed0d50c3Schristos 
355ed0d50c3Schristos   // Initialize one place element.
356ed0d50c3Schristos   void
357ed0d50c3Schristos   initialize_place(Place_index, const char*);
358ed0d50c3Schristos 
359ed0d50c3Schristos   // The places.
360ed0d50c3Schristos   Place places_[PLACE_MAX];
361ed0d50c3Schristos   // True if this is the first call to output_section_init.
362ed0d50c3Schristos   bool first_init_;
363ed0d50c3Schristos };
364ed0d50c3Schristos 
365ed0d50c3Schristos // Initialize Orphan_section_placement.
366ed0d50c3Schristos 
Orphan_section_placement()367ed0d50c3Schristos Orphan_section_placement::Orphan_section_placement()
368ed0d50c3Schristos   : first_init_(true)
369ed0d50c3Schristos {
370ed0d50c3Schristos   this->initialize_place(PLACE_TEXT, ".text");
371ed0d50c3Schristos   this->initialize_place(PLACE_RODATA, ".rodata");
372ed0d50c3Schristos   this->initialize_place(PLACE_DATA, ".data");
373ed0d50c3Schristos   this->initialize_place(PLACE_TLS, NULL);
374ed0d50c3Schristos   this->initialize_place(PLACE_TLS_BSS, NULL);
375ed0d50c3Schristos   this->initialize_place(PLACE_BSS, ".bss");
37606324dcfSchristos   this->initialize_place(PLACE_LAST_ALLOC, NULL);
377ed0d50c3Schristos   this->initialize_place(PLACE_REL, NULL);
378ed0d50c3Schristos   this->initialize_place(PLACE_INTERP, ".interp");
379ed0d50c3Schristos   this->initialize_place(PLACE_NONALLOC, NULL);
380ed0d50c3Schristos   this->initialize_place(PLACE_LAST, NULL);
381ed0d50c3Schristos }
382ed0d50c3Schristos 
383ed0d50c3Schristos // Initialize one place element.
384ed0d50c3Schristos 
385ed0d50c3Schristos void
initialize_place(Place_index index,const char * name)386ed0d50c3Schristos Orphan_section_placement::initialize_place(Place_index index, const char* name)
387ed0d50c3Schristos {
388ed0d50c3Schristos   this->places_[index].name = name;
389ed0d50c3Schristos   this->places_[index].have_location = false;
390ed0d50c3Schristos }
391ed0d50c3Schristos 
392ed0d50c3Schristos // While initializing the Orphan_section_placement information, this
393ed0d50c3Schristos // is called once for each output section named in the linker script.
394ed0d50c3Schristos // If we found an output section during the link, it will be passed in
395ed0d50c3Schristos // OS.
396ed0d50c3Schristos 
397ed0d50c3Schristos void
output_section_init(const std::string & name,Output_section * os,Elements_iterator location)398ed0d50c3Schristos Orphan_section_placement::output_section_init(const std::string& name,
399ed0d50c3Schristos 					      Output_section* os,
400ed0d50c3Schristos 					      Elements_iterator location)
401ed0d50c3Schristos {
402ed0d50c3Schristos   bool first_init = this->first_init_;
403ed0d50c3Schristos   this->first_init_ = false;
404ed0d50c3Schristos 
40506324dcfSchristos   // Remember the last allocated section. Any orphan bss sections
40606324dcfSchristos   // will be placed after it.
40706324dcfSchristos   if (os != NULL
40806324dcfSchristos       && (os->flags() & elfcpp::SHF_ALLOC) != 0)
40906324dcfSchristos     {
41006324dcfSchristos       this->places_[PLACE_LAST_ALLOC].location = location;
41106324dcfSchristos       this->places_[PLACE_LAST_ALLOC].have_location = true;
41206324dcfSchristos     }
41306324dcfSchristos 
414ed0d50c3Schristos   for (int i = 0; i < PLACE_MAX; ++i)
415ed0d50c3Schristos     {
416ed0d50c3Schristos       if (this->places_[i].name != NULL && this->places_[i].name == name)
417ed0d50c3Schristos 	{
418ed0d50c3Schristos 	  if (this->places_[i].have_location)
419ed0d50c3Schristos 	    {
420ed0d50c3Schristos 	      // We have already seen a section with this name.
421ed0d50c3Schristos 	      return;
422ed0d50c3Schristos 	    }
423ed0d50c3Schristos 
424ed0d50c3Schristos 	  this->places_[i].location = location;
425ed0d50c3Schristos 	  this->places_[i].have_location = true;
426ed0d50c3Schristos 
427ed0d50c3Schristos 	  // If we just found the .bss section, restart the search for
428ed0d50c3Schristos 	  // an unallocated section.  This follows the GNU linker's
429ed0d50c3Schristos 	  // behaviour.
430ed0d50c3Schristos 	  if (i == PLACE_BSS)
431ed0d50c3Schristos 	    this->places_[PLACE_NONALLOC].have_location = false;
432ed0d50c3Schristos 
433ed0d50c3Schristos 	  return;
434ed0d50c3Schristos 	}
435ed0d50c3Schristos     }
436ed0d50c3Schristos 
437ed0d50c3Schristos   // Relocation sections.
438ed0d50c3Schristos   if (!this->places_[PLACE_REL].have_location
439ed0d50c3Schristos       && os != NULL
440ed0d50c3Schristos       && (os->type() == elfcpp::SHT_REL || os->type() == elfcpp::SHT_RELA)
441ed0d50c3Schristos       && (os->flags() & elfcpp::SHF_ALLOC) != 0)
442ed0d50c3Schristos     {
443ed0d50c3Schristos       this->places_[PLACE_REL].location = location;
444ed0d50c3Schristos       this->places_[PLACE_REL].have_location = true;
445ed0d50c3Schristos     }
446ed0d50c3Schristos 
447ed0d50c3Schristos   // We find the location for unallocated sections by finding the
448ed0d50c3Schristos   // first debugging or comment section after the BSS section (if
449ed0d50c3Schristos   // there is one).
450ed0d50c3Schristos   if (!this->places_[PLACE_NONALLOC].have_location
451ed0d50c3Schristos       && (name == ".comment" || Layout::is_debug_info_section(name.c_str())))
452ed0d50c3Schristos     {
453ed0d50c3Schristos       // We add orphan sections after the location in PLACES_.  We
454ed0d50c3Schristos       // want to store unallocated sections before LOCATION.  If this
455ed0d50c3Schristos       // is the very first section, we can't use it.
456ed0d50c3Schristos       if (!first_init)
457ed0d50c3Schristos 	{
458ed0d50c3Schristos 	  --location;
459ed0d50c3Schristos 	  this->places_[PLACE_NONALLOC].location = location;
460ed0d50c3Schristos 	  this->places_[PLACE_NONALLOC].have_location = true;
461ed0d50c3Schristos 	}
462ed0d50c3Schristos     }
463ed0d50c3Schristos }
464ed0d50c3Schristos 
465ed0d50c3Schristos // Initialize the last location.
466ed0d50c3Schristos 
467ed0d50c3Schristos void
last_init(Elements_iterator location)468ed0d50c3Schristos Orphan_section_placement::last_init(Elements_iterator location)
469ed0d50c3Schristos {
470ed0d50c3Schristos   this->places_[PLACE_LAST].location = location;
471ed0d50c3Schristos   this->places_[PLACE_LAST].have_location = true;
472ed0d50c3Schristos }
473ed0d50c3Schristos 
474ed0d50c3Schristos // Set *PWHERE to the address of an iterator pointing to the location
475ed0d50c3Schristos // to use for an orphan section.  Return true if the iterator has a
476ed0d50c3Schristos // value, false otherwise.
477ed0d50c3Schristos 
478ed0d50c3Schristos bool
find_place(Output_section * os,Elements_iterator ** pwhere)479ed0d50c3Schristos Orphan_section_placement::find_place(Output_section* os,
480ed0d50c3Schristos 				     Elements_iterator** pwhere)
481ed0d50c3Schristos {
482ed0d50c3Schristos   // Figure out where OS should go.  This is based on the GNU linker
483ed0d50c3Schristos   // code.  FIXME: The GNU linker handles small data sections
484ed0d50c3Schristos   // specially, but we don't.
485ed0d50c3Schristos   elfcpp::Elf_Word type = os->type();
486ed0d50c3Schristos   elfcpp::Elf_Xword flags = os->flags();
487ed0d50c3Schristos   Place_index index;
488ed0d50c3Schristos   if ((flags & elfcpp::SHF_ALLOC) == 0
489ed0d50c3Schristos       && !Layout::is_debug_info_section(os->name()))
490ed0d50c3Schristos     index = PLACE_NONALLOC;
491ed0d50c3Schristos   else if ((flags & elfcpp::SHF_ALLOC) == 0)
492ed0d50c3Schristos     index = PLACE_LAST;
493ed0d50c3Schristos   else if (type == elfcpp::SHT_NOTE)
494ed0d50c3Schristos     index = PLACE_INTERP;
495ed0d50c3Schristos   else if ((flags & elfcpp::SHF_TLS) != 0)
496ed0d50c3Schristos     {
497ed0d50c3Schristos       if (type == elfcpp::SHT_NOBITS)
498ed0d50c3Schristos 	index = PLACE_TLS_BSS;
499ed0d50c3Schristos       else
500ed0d50c3Schristos 	index = PLACE_TLS;
501ed0d50c3Schristos     }
502ed0d50c3Schristos   else if (type == elfcpp::SHT_NOBITS)
503ed0d50c3Schristos     index = PLACE_BSS;
504ed0d50c3Schristos   else if ((flags & elfcpp::SHF_WRITE) != 0)
505ed0d50c3Schristos     index = PLACE_DATA;
506ed0d50c3Schristos   else if (type == elfcpp::SHT_REL || type == elfcpp::SHT_RELA)
507ed0d50c3Schristos     index = PLACE_REL;
508ed0d50c3Schristos   else if ((flags & elfcpp::SHF_EXECINSTR) == 0)
509ed0d50c3Schristos     index = PLACE_RODATA;
510ed0d50c3Schristos   else
511ed0d50c3Schristos     index = PLACE_TEXT;
512ed0d50c3Schristos 
513ed0d50c3Schristos   // If we don't have a location yet, try to find one based on a
514ed0d50c3Schristos   // plausible ordering of sections.
515ed0d50c3Schristos   if (!this->places_[index].have_location)
516ed0d50c3Schristos     {
517ed0d50c3Schristos       Place_index follow;
518ed0d50c3Schristos       switch (index)
519ed0d50c3Schristos 	{
520ed0d50c3Schristos 	default:
521ed0d50c3Schristos 	  follow = PLACE_MAX;
522ed0d50c3Schristos 	  break;
523ed0d50c3Schristos 	case PLACE_RODATA:
524ed0d50c3Schristos 	  follow = PLACE_TEXT;
525ed0d50c3Schristos 	  break;
52606324dcfSchristos 	case PLACE_DATA:
52706324dcfSchristos 	  follow = PLACE_RODATA;
52806324dcfSchristos 	  if (!this->places_[PLACE_RODATA].have_location)
52906324dcfSchristos 	    follow = PLACE_TEXT;
53006324dcfSchristos 	  break;
531ed0d50c3Schristos 	case PLACE_BSS:
53206324dcfSchristos 	  follow = PLACE_LAST_ALLOC;
533ed0d50c3Schristos 	  break;
534ed0d50c3Schristos 	case PLACE_REL:
535ed0d50c3Schristos 	  follow = PLACE_TEXT;
536ed0d50c3Schristos 	  break;
537ed0d50c3Schristos 	case PLACE_INTERP:
538ed0d50c3Schristos 	  follow = PLACE_TEXT;
539ed0d50c3Schristos 	  break;
540ed0d50c3Schristos 	case PLACE_TLS:
541ed0d50c3Schristos 	  follow = PLACE_DATA;
542ed0d50c3Schristos 	  break;
543ed0d50c3Schristos 	case PLACE_TLS_BSS:
544ed0d50c3Schristos 	  follow = PLACE_TLS;
545ed0d50c3Schristos 	  if (!this->places_[PLACE_TLS].have_location)
546ed0d50c3Schristos 	    follow = PLACE_DATA;
547ed0d50c3Schristos 	  break;
548ed0d50c3Schristos 	}
549ed0d50c3Schristos       if (follow != PLACE_MAX && this->places_[follow].have_location)
550ed0d50c3Schristos 	{
551ed0d50c3Schristos 	  // Set the location of INDEX to the location of FOLLOW.  The
552ed0d50c3Schristos 	  // location of INDEX will then be incremented by the caller,
553ed0d50c3Schristos 	  // so anything in INDEX will continue to be after anything
554ed0d50c3Schristos 	  // in FOLLOW.
555ed0d50c3Schristos 	  this->places_[index].location = this->places_[follow].location;
556ed0d50c3Schristos 	  this->places_[index].have_location = true;
557ed0d50c3Schristos 	}
558ed0d50c3Schristos     }
559ed0d50c3Schristos 
560ed0d50c3Schristos   *pwhere = &this->places_[index].location;
561ed0d50c3Schristos   bool ret = this->places_[index].have_location;
562ed0d50c3Schristos 
563ed0d50c3Schristos   // The caller will set the location.
564ed0d50c3Schristos   this->places_[index].have_location = true;
565ed0d50c3Schristos 
566ed0d50c3Schristos   return ret;
567ed0d50c3Schristos }
568ed0d50c3Schristos 
56906324dcfSchristos // Update PLACE_LAST_ALLOC.
57006324dcfSchristos void
update_last_alloc(Elements_iterator elem)57106324dcfSchristos Orphan_section_placement::update_last_alloc(Elements_iterator elem)
57206324dcfSchristos {
57306324dcfSchristos   Elements_iterator prev = elem;
57406324dcfSchristos   --prev;
57506324dcfSchristos   if (this->places_[PLACE_LAST_ALLOC].have_location
57606324dcfSchristos       && this->places_[PLACE_LAST_ALLOC].location == prev)
57706324dcfSchristos     {
57806324dcfSchristos       this->places_[PLACE_LAST_ALLOC].have_location = true;
57906324dcfSchristos       this->places_[PLACE_LAST_ALLOC].location = elem;
58006324dcfSchristos     }
58106324dcfSchristos }
58206324dcfSchristos 
583ed0d50c3Schristos // Return the iterator being used for sections at the very end of the
584ed0d50c3Schristos // linker script.
585ed0d50c3Schristos 
586ed0d50c3Schristos Orphan_section_placement::Elements_iterator
last_place() const587ed0d50c3Schristos Orphan_section_placement::last_place() const
588ed0d50c3Schristos {
589ed0d50c3Schristos   gold_assert(this->places_[PLACE_LAST].have_location);
590ed0d50c3Schristos   return this->places_[PLACE_LAST].location;
591ed0d50c3Schristos }
592ed0d50c3Schristos 
593ed0d50c3Schristos // An element in a SECTIONS clause.
594ed0d50c3Schristos 
595ed0d50c3Schristos class Sections_element
596ed0d50c3Schristos {
597ed0d50c3Schristos  public:
Sections_element()598ed0d50c3Schristos   Sections_element()
599ed0d50c3Schristos   { }
600ed0d50c3Schristos 
~Sections_element()601ed0d50c3Schristos   virtual ~Sections_element()
602ed0d50c3Schristos   { }
603ed0d50c3Schristos 
604ed0d50c3Schristos   // Return whether an output section is relro.
605ed0d50c3Schristos   virtual bool
is_relro() const606ed0d50c3Schristos   is_relro() const
607ed0d50c3Schristos   { return false; }
608ed0d50c3Schristos 
609ed0d50c3Schristos   // Record that an output section is relro.
610ed0d50c3Schristos   virtual void
set_is_relro()611ed0d50c3Schristos   set_is_relro()
612ed0d50c3Schristos   { }
613ed0d50c3Schristos 
614ed0d50c3Schristos   // Create any required output sections.  The only real
615ed0d50c3Schristos   // implementation is in Output_section_definition.
616ed0d50c3Schristos   virtual void
create_sections(Layout *)617ed0d50c3Schristos   create_sections(Layout*)
618ed0d50c3Schristos   { }
619ed0d50c3Schristos 
620ed0d50c3Schristos   // Add any symbol being defined to the symbol table.
621ed0d50c3Schristos   virtual void
add_symbols_to_table(Symbol_table *)622ed0d50c3Schristos   add_symbols_to_table(Symbol_table*)
623ed0d50c3Schristos   { }
624ed0d50c3Schristos 
625ed0d50c3Schristos   // Finalize symbols and check assertions.
626ed0d50c3Schristos   virtual void
finalize_symbols(Symbol_table *,const Layout *,uint64_t *)627ed0d50c3Schristos   finalize_symbols(Symbol_table*, const Layout*, uint64_t*)
628ed0d50c3Schristos   { }
629ed0d50c3Schristos 
630ed0d50c3Schristos   // Return the output section name to use for an input file name and
631ed0d50c3Schristos   // section name.  This only real implementation is in
632ed0d50c3Schristos   // Output_section_definition.
633ed0d50c3Schristos   virtual const char*
output_section_name(const char *,const char *,Output_section ***,Script_sections::Section_type *,bool *,bool)634ed0d50c3Schristos   output_section_name(const char*, const char*, Output_section***,
63506324dcfSchristos 		      Script_sections::Section_type*, bool*, bool)
636ed0d50c3Schristos   { return NULL; }
637ed0d50c3Schristos 
638ed0d50c3Schristos   // Initialize OSP with an output section.
639ed0d50c3Schristos   virtual void
orphan_section_init(Orphan_section_placement *,Script_sections::Elements_iterator)640ed0d50c3Schristos   orphan_section_init(Orphan_section_placement*,
641ed0d50c3Schristos 		      Script_sections::Elements_iterator)
642ed0d50c3Schristos   { }
643ed0d50c3Schristos 
644ed0d50c3Schristos   // Set section addresses.  This includes applying assignments if the
645ed0d50c3Schristos   // expression is an absolute value.
646ed0d50c3Schristos   virtual void
set_section_addresses(Symbol_table *,Layout *,uint64_t *,uint64_t *,uint64_t *)647ed0d50c3Schristos   set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
648ed0d50c3Schristos 			uint64_t*)
649ed0d50c3Schristos   { }
650ed0d50c3Schristos 
651ed0d50c3Schristos   // Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
652ed0d50c3Schristos   // this section is constrained, and the input sections do not match,
653ed0d50c3Schristos   // return the constraint, and set *POSD.
654ed0d50c3Schristos   virtual Section_constraint
check_constraint(Output_section_definition **)655ed0d50c3Schristos   check_constraint(Output_section_definition**)
656ed0d50c3Schristos   { return CONSTRAINT_NONE; }
657ed0d50c3Schristos 
658ed0d50c3Schristos   // See if this is the alternate output section for a constrained
659ed0d50c3Schristos   // output section.  If it is, transfer the Output_section and return
660ed0d50c3Schristos   // true.  Otherwise return false.
661ed0d50c3Schristos   virtual bool
alternate_constraint(Output_section_definition *,Section_constraint)662ed0d50c3Schristos   alternate_constraint(Output_section_definition*, Section_constraint)
663ed0d50c3Schristos   { return false; }
664ed0d50c3Schristos 
665ed0d50c3Schristos   // Get the list of segments to use for an allocated section when
666ed0d50c3Schristos   // using a PHDRS clause.  If this is an allocated section, return
667ed0d50c3Schristos   // the Output_section, and set *PHDRS_LIST (the first parameter) to
668ed0d50c3Schristos   // the list of PHDRS to which it should be attached.  If the PHDRS
669ed0d50c3Schristos   // were not specified, don't change *PHDRS_LIST.  When not returning
670ed0d50c3Schristos   // NULL, set *ORPHAN (the second parameter) according to whether
671ed0d50c3Schristos   // this is an orphan section--one that is not mentioned in the
672ed0d50c3Schristos   // linker script.
673ed0d50c3Schristos   virtual Output_section*
allocate_to_segment(String_list **,bool *)674ed0d50c3Schristos   allocate_to_segment(String_list**, bool*)
675ed0d50c3Schristos   { return NULL; }
676ed0d50c3Schristos 
677ed0d50c3Schristos   // Look for an output section by name and return the address, the
678ed0d50c3Schristos   // load address, the alignment, and the size.  This is used when an
679ed0d50c3Schristos   // expression refers to an output section which was not actually
680ed0d50c3Schristos   // created.  This returns true if the section was found, false
681ed0d50c3Schristos   // otherwise.  The only real definition is for
682ed0d50c3Schristos   // Output_section_definition.
683ed0d50c3Schristos   virtual bool
get_output_section_info(const char *,uint64_t *,uint64_t *,uint64_t *,uint64_t *) const684ed0d50c3Schristos   get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
685ed0d50c3Schristos                           uint64_t*) const
686ed0d50c3Schristos   { return false; }
687ed0d50c3Schristos 
688ed0d50c3Schristos   // Return the associated Output_section if there is one.
689ed0d50c3Schristos   virtual Output_section*
get_output_section() const690ed0d50c3Schristos   get_output_section() const
691ed0d50c3Schristos   { return NULL; }
692ed0d50c3Schristos 
693ed0d50c3Schristos   // Set the section's memory regions.
694ed0d50c3Schristos   virtual void
set_memory_region(Memory_region *,bool)695ed0d50c3Schristos   set_memory_region(Memory_region*, bool)
696ed0d50c3Schristos   { gold_error(_("Attempt to set a memory region for a non-output section")); }
697ed0d50c3Schristos 
698ed0d50c3Schristos   // Print the element for debugging purposes.
699ed0d50c3Schristos   virtual void
700ed0d50c3Schristos   print(FILE* f) const = 0;
701ed0d50c3Schristos };
702ed0d50c3Schristos 
703ed0d50c3Schristos // An assignment in a SECTIONS clause outside of an output section.
704ed0d50c3Schristos 
705ed0d50c3Schristos class Sections_element_assignment : public Sections_element
706ed0d50c3Schristos {
707ed0d50c3Schristos  public:
Sections_element_assignment(const char * name,size_t namelen,Expression * val,bool provide,bool hidden)708ed0d50c3Schristos   Sections_element_assignment(const char* name, size_t namelen,
709ed0d50c3Schristos 			      Expression* val, bool provide, bool hidden)
710ed0d50c3Schristos     : assignment_(name, namelen, false, val, provide, hidden)
711ed0d50c3Schristos   { }
712ed0d50c3Schristos 
713ed0d50c3Schristos   // Add the symbol to the symbol table.
714ed0d50c3Schristos   void
add_symbols_to_table(Symbol_table * symtab)715ed0d50c3Schristos   add_symbols_to_table(Symbol_table* symtab)
716ed0d50c3Schristos   { this->assignment_.add_to_table(symtab); }
717ed0d50c3Schristos 
718ed0d50c3Schristos   // Finalize the symbol.
719ed0d50c3Schristos   void
finalize_symbols(Symbol_table * symtab,const Layout * layout,uint64_t * dot_value)720ed0d50c3Schristos   finalize_symbols(Symbol_table* symtab, const Layout* layout,
721ed0d50c3Schristos 		   uint64_t* dot_value)
722ed0d50c3Schristos   {
723ed0d50c3Schristos     this->assignment_.finalize_with_dot(symtab, layout, *dot_value, NULL);
724ed0d50c3Schristos   }
725ed0d50c3Schristos 
726ed0d50c3Schristos   // Set the section address.  There is no section here, but if the
727ed0d50c3Schristos   // value is absolute, we set the symbol.  This permits us to use
728ed0d50c3Schristos   // absolute symbols when setting dot.
729ed0d50c3Schristos   void
set_section_addresses(Symbol_table * symtab,Layout * layout,uint64_t * dot_value,uint64_t *,uint64_t *)730ed0d50c3Schristos   set_section_addresses(Symbol_table* symtab, Layout* layout,
731ed0d50c3Schristos 			uint64_t* dot_value, uint64_t*, uint64_t*)
732ed0d50c3Schristos   {
733ed0d50c3Schristos     this->assignment_.set_if_absolute(symtab, layout, true, *dot_value, NULL);
734ed0d50c3Schristos   }
735ed0d50c3Schristos 
736ed0d50c3Schristos   // Print for debugging.
737ed0d50c3Schristos   void
print(FILE * f) const738ed0d50c3Schristos   print(FILE* f) const
739ed0d50c3Schristos   {
740ed0d50c3Schristos     fprintf(f, "  ");
741ed0d50c3Schristos     this->assignment_.print(f);
742ed0d50c3Schristos   }
743ed0d50c3Schristos 
744ed0d50c3Schristos  private:
745ed0d50c3Schristos   Symbol_assignment assignment_;
746ed0d50c3Schristos };
747ed0d50c3Schristos 
748ed0d50c3Schristos // An assignment to the dot symbol in a SECTIONS clause outside of an
749ed0d50c3Schristos // output section.
750ed0d50c3Schristos 
751ed0d50c3Schristos class Sections_element_dot_assignment : public Sections_element
752ed0d50c3Schristos {
753ed0d50c3Schristos  public:
Sections_element_dot_assignment(Expression * val)754ed0d50c3Schristos   Sections_element_dot_assignment(Expression* val)
755ed0d50c3Schristos     : val_(val)
756ed0d50c3Schristos   { }
757ed0d50c3Schristos 
758ed0d50c3Schristos   // Finalize the symbol.
759ed0d50c3Schristos   void
finalize_symbols(Symbol_table * symtab,const Layout * layout,uint64_t * dot_value)760ed0d50c3Schristos   finalize_symbols(Symbol_table* symtab, const Layout* layout,
761ed0d50c3Schristos 		   uint64_t* dot_value)
762ed0d50c3Schristos   {
763ed0d50c3Schristos     // We ignore the section of the result because outside of an
764ed0d50c3Schristos     // output section definition the dot symbol is always considered
765ed0d50c3Schristos     // to be absolute.
766ed0d50c3Schristos     *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
767ed0d50c3Schristos 					   NULL, NULL, NULL, false);
768ed0d50c3Schristos   }
769ed0d50c3Schristos 
770ed0d50c3Schristos   // Update the dot symbol while setting section addresses.
771ed0d50c3Schristos   void
set_section_addresses(Symbol_table * symtab,Layout * layout,uint64_t * dot_value,uint64_t * dot_alignment,uint64_t * load_address)772ed0d50c3Schristos   set_section_addresses(Symbol_table* symtab, Layout* layout,
773ed0d50c3Schristos 			uint64_t* dot_value, uint64_t* dot_alignment,
774ed0d50c3Schristos 			uint64_t* load_address)
775ed0d50c3Schristos   {
776ed0d50c3Schristos     *dot_value = this->val_->eval_with_dot(symtab, layout, false, *dot_value,
777ed0d50c3Schristos 					   NULL, NULL, dot_alignment, false);
778ed0d50c3Schristos     *load_address = *dot_value;
779ed0d50c3Schristos   }
780ed0d50c3Schristos 
781ed0d50c3Schristos   // Print for debugging.
782ed0d50c3Schristos   void
print(FILE * f) const783ed0d50c3Schristos   print(FILE* f) const
784ed0d50c3Schristos   {
785ed0d50c3Schristos     fprintf(f, "  . = ");
786ed0d50c3Schristos     this->val_->print(f);
787ed0d50c3Schristos     fprintf(f, "\n");
788ed0d50c3Schristos   }
789ed0d50c3Schristos 
790ed0d50c3Schristos  private:
791ed0d50c3Schristos   Expression* val_;
792ed0d50c3Schristos };
793ed0d50c3Schristos 
794ed0d50c3Schristos // An assertion in a SECTIONS clause outside of an output section.
795ed0d50c3Schristos 
796ed0d50c3Schristos class Sections_element_assertion : public Sections_element
797ed0d50c3Schristos {
798ed0d50c3Schristos  public:
Sections_element_assertion(Expression * check,const char * message,size_t messagelen)799ed0d50c3Schristos   Sections_element_assertion(Expression* check, const char* message,
800ed0d50c3Schristos 			     size_t messagelen)
801ed0d50c3Schristos     : assertion_(check, message, messagelen)
802ed0d50c3Schristos   { }
803ed0d50c3Schristos 
804ed0d50c3Schristos   // Check the assertion.
805ed0d50c3Schristos   void
finalize_symbols(Symbol_table * symtab,const Layout * layout,uint64_t *)806ed0d50c3Schristos   finalize_symbols(Symbol_table* symtab, const Layout* layout, uint64_t*)
807ed0d50c3Schristos   { this->assertion_.check(symtab, layout); }
808ed0d50c3Schristos 
809ed0d50c3Schristos   // Print for debugging.
810ed0d50c3Schristos   void
print(FILE * f) const811ed0d50c3Schristos   print(FILE* f) const
812ed0d50c3Schristos   {
813ed0d50c3Schristos     fprintf(f, "  ");
814ed0d50c3Schristos     this->assertion_.print(f);
815ed0d50c3Schristos   }
816ed0d50c3Schristos 
817ed0d50c3Schristos  private:
818ed0d50c3Schristos   Script_assertion assertion_;
819ed0d50c3Schristos };
820ed0d50c3Schristos 
821ed0d50c3Schristos // An element in an output section in a SECTIONS clause.
822ed0d50c3Schristos 
823ed0d50c3Schristos class Output_section_element
824ed0d50c3Schristos {
825ed0d50c3Schristos  public:
826ed0d50c3Schristos   // A list of input sections.
827ed0d50c3Schristos   typedef std::list<Output_section::Input_section> Input_section_list;
828ed0d50c3Schristos 
Output_section_element()829ed0d50c3Schristos   Output_section_element()
830ed0d50c3Schristos   { }
831ed0d50c3Schristos 
~Output_section_element()832ed0d50c3Schristos   virtual ~Output_section_element()
833ed0d50c3Schristos   { }
834ed0d50c3Schristos 
835ed0d50c3Schristos   // Return whether this element requires an output section to exist.
836ed0d50c3Schristos   virtual bool
needs_output_section() const837ed0d50c3Schristos   needs_output_section() const
838ed0d50c3Schristos   { return false; }
839ed0d50c3Schristos 
840ed0d50c3Schristos   // Add any symbol being defined to the symbol table.
841ed0d50c3Schristos   virtual void
add_symbols_to_table(Symbol_table *)842ed0d50c3Schristos   add_symbols_to_table(Symbol_table*)
843ed0d50c3Schristos   { }
844ed0d50c3Schristos 
845ed0d50c3Schristos   // Finalize symbols and check assertions.
846ed0d50c3Schristos   virtual void
finalize_symbols(Symbol_table *,const Layout *,uint64_t *,Output_section **)847ed0d50c3Schristos   finalize_symbols(Symbol_table*, const Layout*, uint64_t*, Output_section**)
848ed0d50c3Schristos   { }
849ed0d50c3Schristos 
850ed0d50c3Schristos   // Return whether this element matches FILE_NAME and SECTION_NAME.
851ed0d50c3Schristos   // The only real implementation is in Output_section_element_input.
852ed0d50c3Schristos   virtual bool
match_name(const char *,const char *,bool *) const853ed0d50c3Schristos   match_name(const char*, const char*, bool *) const
854ed0d50c3Schristos   { return false; }
855ed0d50c3Schristos 
856ed0d50c3Schristos   // Set section addresses.  This includes applying assignments if the
857ed0d50c3Schristos   // expression is an absolute value.
858ed0d50c3Schristos   virtual void
set_section_addresses(Symbol_table *,Layout *,Output_section *,uint64_t,uint64_t *,uint64_t *,Output_section **,std::string *,Input_section_list *)859ed0d50c3Schristos   set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
860ed0d50c3Schristos 			uint64_t*, uint64_t*, Output_section**, std::string*,
861ed0d50c3Schristos 			Input_section_list*)
862ed0d50c3Schristos   { }
863ed0d50c3Schristos 
864ed0d50c3Schristos   // Print the element for debugging purposes.
865ed0d50c3Schristos   virtual void
866ed0d50c3Schristos   print(FILE* f) const = 0;
867ed0d50c3Schristos 
868ed0d50c3Schristos  protected:
869ed0d50c3Schristos   // Return a fill string that is LENGTH bytes long, filling it with
870ed0d50c3Schristos   // FILL.
871ed0d50c3Schristos   std::string
872ed0d50c3Schristos   get_fill_string(const std::string* fill, section_size_type length) const;
873ed0d50c3Schristos };
874ed0d50c3Schristos 
875ed0d50c3Schristos std::string
get_fill_string(const std::string * fill,section_size_type length) const876ed0d50c3Schristos Output_section_element::get_fill_string(const std::string* fill,
877ed0d50c3Schristos 					section_size_type length) const
878ed0d50c3Schristos {
879ed0d50c3Schristos   std::string this_fill;
880ed0d50c3Schristos   this_fill.reserve(length);
881ed0d50c3Schristos   while (this_fill.length() + fill->length() <= length)
882ed0d50c3Schristos     this_fill += *fill;
883ed0d50c3Schristos   if (this_fill.length() < length)
884ed0d50c3Schristos     this_fill.append(*fill, 0, length - this_fill.length());
885ed0d50c3Schristos   return this_fill;
886ed0d50c3Schristos }
887ed0d50c3Schristos 
888ed0d50c3Schristos // A symbol assignment in an output section.
889ed0d50c3Schristos 
890ed0d50c3Schristos class Output_section_element_assignment : public Output_section_element
891ed0d50c3Schristos {
892ed0d50c3Schristos  public:
Output_section_element_assignment(const char * name,size_t namelen,Expression * val,bool provide,bool hidden)893ed0d50c3Schristos   Output_section_element_assignment(const char* name, size_t namelen,
894ed0d50c3Schristos 				    Expression* val, bool provide,
895ed0d50c3Schristos 				    bool hidden)
896ed0d50c3Schristos     : assignment_(name, namelen, false, val, provide, hidden)
897ed0d50c3Schristos   { }
898ed0d50c3Schristos 
899ed0d50c3Schristos   // Add the symbol to the symbol table.
900ed0d50c3Schristos   void
add_symbols_to_table(Symbol_table * symtab)901ed0d50c3Schristos   add_symbols_to_table(Symbol_table* symtab)
902ed0d50c3Schristos   { this->assignment_.add_to_table(symtab); }
903ed0d50c3Schristos 
904ed0d50c3Schristos   // Finalize the symbol.
905ed0d50c3Schristos   void
finalize_symbols(Symbol_table * symtab,const Layout * layout,uint64_t * dot_value,Output_section ** dot_section)906ed0d50c3Schristos   finalize_symbols(Symbol_table* symtab, const Layout* layout,
907ed0d50c3Schristos 		   uint64_t* dot_value, Output_section** dot_section)
908ed0d50c3Schristos   {
909ed0d50c3Schristos     this->assignment_.finalize_with_dot(symtab, layout, *dot_value,
910ed0d50c3Schristos 					*dot_section);
911ed0d50c3Schristos   }
912ed0d50c3Schristos 
913ed0d50c3Schristos   // Set the section address.  There is no section here, but if the
914ed0d50c3Schristos   // value is absolute, we set the symbol.  This permits us to use
915ed0d50c3Schristos   // absolute symbols when setting dot.
916ed0d50c3Schristos   void
set_section_addresses(Symbol_table * symtab,Layout * layout,Output_section *,uint64_t,uint64_t * dot_value,uint64_t *,Output_section ** dot_section,std::string *,Input_section_list *)917ed0d50c3Schristos   set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
918ed0d50c3Schristos 			uint64_t, uint64_t* dot_value, uint64_t*,
919ed0d50c3Schristos 			Output_section** dot_section, std::string*,
920ed0d50c3Schristos 			Input_section_list*)
921ed0d50c3Schristos   {
922ed0d50c3Schristos     this->assignment_.set_if_absolute(symtab, layout, true, *dot_value,
923ed0d50c3Schristos 				      *dot_section);
924ed0d50c3Schristos   }
925ed0d50c3Schristos 
926ed0d50c3Schristos   // Print for debugging.
927ed0d50c3Schristos   void
print(FILE * f) const928ed0d50c3Schristos   print(FILE* f) const
929ed0d50c3Schristos   {
930ed0d50c3Schristos     fprintf(f, "    ");
931ed0d50c3Schristos     this->assignment_.print(f);
932ed0d50c3Schristos   }
933ed0d50c3Schristos 
934ed0d50c3Schristos  private:
935ed0d50c3Schristos   Symbol_assignment assignment_;
936ed0d50c3Schristos };
937ed0d50c3Schristos 
938ed0d50c3Schristos // An assignment to the dot symbol in an output section.
939ed0d50c3Schristos 
940ed0d50c3Schristos class Output_section_element_dot_assignment : public Output_section_element
941ed0d50c3Schristos {
942ed0d50c3Schristos  public:
Output_section_element_dot_assignment(Expression * val)943ed0d50c3Schristos   Output_section_element_dot_assignment(Expression* val)
944ed0d50c3Schristos     : val_(val)
945ed0d50c3Schristos   { }
946ed0d50c3Schristos 
947ed0d50c3Schristos   // An assignment to dot within an output section is enough to force
948ed0d50c3Schristos   // the output section to exist.
949ed0d50c3Schristos   bool
needs_output_section() const950ed0d50c3Schristos   needs_output_section() const
951ed0d50c3Schristos   { return true; }
952ed0d50c3Schristos 
953ed0d50c3Schristos   // Finalize the symbol.
954ed0d50c3Schristos   void
finalize_symbols(Symbol_table * symtab,const Layout * layout,uint64_t * dot_value,Output_section ** dot_section)955ed0d50c3Schristos   finalize_symbols(Symbol_table* symtab, const Layout* layout,
956ed0d50c3Schristos 		   uint64_t* dot_value, Output_section** dot_section)
957ed0d50c3Schristos   {
958ed0d50c3Schristos     *dot_value = this->val_->eval_with_dot(symtab, layout, true, *dot_value,
959ed0d50c3Schristos 					   *dot_section, dot_section, NULL,
960ed0d50c3Schristos 					   true);
961ed0d50c3Schristos   }
962ed0d50c3Schristos 
963ed0d50c3Schristos   // Update the dot symbol while setting section addresses.
964ed0d50c3Schristos   void
965ed0d50c3Schristos   set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
966ed0d50c3Schristos 			uint64_t, uint64_t* dot_value, uint64_t*,
967ed0d50c3Schristos 			Output_section** dot_section, std::string*,
968ed0d50c3Schristos 			Input_section_list*);
969ed0d50c3Schristos 
970ed0d50c3Schristos   // Print for debugging.
971ed0d50c3Schristos   void
print(FILE * f) const972ed0d50c3Schristos   print(FILE* f) const
973ed0d50c3Schristos   {
974ed0d50c3Schristos     fprintf(f, "    . = ");
975ed0d50c3Schristos     this->val_->print(f);
976ed0d50c3Schristos     fprintf(f, "\n");
977ed0d50c3Schristos   }
978ed0d50c3Schristos 
979ed0d50c3Schristos  private:
980ed0d50c3Schristos   Expression* val_;
981ed0d50c3Schristos };
982ed0d50c3Schristos 
983ed0d50c3Schristos // Update the dot symbol while setting section addresses.
984ed0d50c3Schristos 
985ed0d50c3Schristos void
set_section_addresses(Symbol_table * symtab,Layout * layout,Output_section * output_section,uint64_t,uint64_t * dot_value,uint64_t * dot_alignment,Output_section ** dot_section,std::string * fill,Input_section_list *)986ed0d50c3Schristos Output_section_element_dot_assignment::set_section_addresses(
987ed0d50c3Schristos     Symbol_table* symtab,
988ed0d50c3Schristos     Layout* layout,
989ed0d50c3Schristos     Output_section* output_section,
990ed0d50c3Schristos     uint64_t,
991ed0d50c3Schristos     uint64_t* dot_value,
992ed0d50c3Schristos     uint64_t* dot_alignment,
993ed0d50c3Schristos     Output_section** dot_section,
994ed0d50c3Schristos     std::string* fill,
995ed0d50c3Schristos     Input_section_list*)
996ed0d50c3Schristos {
997ed0d50c3Schristos   uint64_t next_dot = this->val_->eval_with_dot(symtab, layout, false,
998ed0d50c3Schristos 						*dot_value, *dot_section,
999ed0d50c3Schristos 						dot_section, dot_alignment,
1000ed0d50c3Schristos 						true);
1001ed0d50c3Schristos   if (next_dot < *dot_value)
1002ed0d50c3Schristos     gold_error(_("dot may not move backward"));
1003ed0d50c3Schristos   if (next_dot > *dot_value && output_section != NULL)
1004ed0d50c3Schristos     {
1005ed0d50c3Schristos       section_size_type length = convert_to_section_size_type(next_dot
1006ed0d50c3Schristos 							      - *dot_value);
1007ed0d50c3Schristos       Output_section_data* posd;
1008ed0d50c3Schristos       if (fill->empty())
1009ed0d50c3Schristos 	posd = new Output_data_zero_fill(length, 0);
1010ed0d50c3Schristos       else
1011ed0d50c3Schristos 	{
1012ed0d50c3Schristos 	  std::string this_fill = this->get_fill_string(fill, length);
1013ed0d50c3Schristos 	  posd = new Output_data_const(this_fill, 0);
1014ed0d50c3Schristos 	}
1015ed0d50c3Schristos       output_section->add_output_section_data(posd);
1016ed0d50c3Schristos       layout->new_output_section_data_from_script(posd);
1017ed0d50c3Schristos     }
1018ed0d50c3Schristos   *dot_value = next_dot;
1019ed0d50c3Schristos }
1020ed0d50c3Schristos 
1021ed0d50c3Schristos // An assertion in an output section.
1022ed0d50c3Schristos 
1023ed0d50c3Schristos class Output_section_element_assertion : public Output_section_element
1024ed0d50c3Schristos {
1025ed0d50c3Schristos  public:
Output_section_element_assertion(Expression * check,const char * message,size_t messagelen)1026ed0d50c3Schristos   Output_section_element_assertion(Expression* check, const char* message,
1027ed0d50c3Schristos 				   size_t messagelen)
1028ed0d50c3Schristos     : assertion_(check, message, messagelen)
1029ed0d50c3Schristos   { }
1030ed0d50c3Schristos 
1031ed0d50c3Schristos   void
print(FILE * f) const1032ed0d50c3Schristos   print(FILE* f) const
1033ed0d50c3Schristos   {
1034ed0d50c3Schristos     fprintf(f, "    ");
1035ed0d50c3Schristos     this->assertion_.print(f);
1036ed0d50c3Schristos   }
1037ed0d50c3Schristos 
1038ed0d50c3Schristos  private:
1039ed0d50c3Schristos   Script_assertion assertion_;
1040ed0d50c3Schristos };
1041ed0d50c3Schristos 
1042ed0d50c3Schristos // We use a special instance of Output_section_data to handle BYTE,
1043ed0d50c3Schristos // SHORT, etc.  This permits forward references to symbols in the
1044ed0d50c3Schristos // expressions.
1045ed0d50c3Schristos 
1046ed0d50c3Schristos class Output_data_expression : public Output_section_data
1047ed0d50c3Schristos {
1048ed0d50c3Schristos  public:
Output_data_expression(int size,bool is_signed,Expression * val,const Symbol_table * symtab,const Layout * layout,uint64_t dot_value,Output_section * dot_section)1049ed0d50c3Schristos   Output_data_expression(int size, bool is_signed, Expression* val,
1050ed0d50c3Schristos 			 const Symbol_table* symtab, const Layout* layout,
1051ed0d50c3Schristos 			 uint64_t dot_value, Output_section* dot_section)
1052ed0d50c3Schristos     : Output_section_data(size, 0, true),
1053ed0d50c3Schristos       is_signed_(is_signed), val_(val), symtab_(symtab),
1054ed0d50c3Schristos       layout_(layout), dot_value_(dot_value), dot_section_(dot_section)
1055ed0d50c3Schristos   { }
1056ed0d50c3Schristos 
1057ed0d50c3Schristos  protected:
1058ed0d50c3Schristos   // Write the data to the output file.
1059ed0d50c3Schristos   void
1060ed0d50c3Schristos   do_write(Output_file*);
1061ed0d50c3Schristos 
1062ed0d50c3Schristos   // Write the data to a buffer.
1063ed0d50c3Schristos   void
1064ed0d50c3Schristos   do_write_to_buffer(unsigned char*);
1065ed0d50c3Schristos 
1066ed0d50c3Schristos   // Write to a map file.
1067ed0d50c3Schristos   void
do_print_to_mapfile(Mapfile * mapfile) const1068ed0d50c3Schristos   do_print_to_mapfile(Mapfile* mapfile) const
1069ed0d50c3Schristos   { mapfile->print_output_data(this, _("** expression")); }
1070ed0d50c3Schristos 
1071ed0d50c3Schristos  private:
1072ed0d50c3Schristos   template<bool big_endian>
1073ed0d50c3Schristos   void
1074ed0d50c3Schristos   endian_write_to_buffer(uint64_t, unsigned char*);
1075ed0d50c3Schristos 
1076ed0d50c3Schristos   bool is_signed_;
1077ed0d50c3Schristos   Expression* val_;
1078ed0d50c3Schristos   const Symbol_table* symtab_;
1079ed0d50c3Schristos   const Layout* layout_;
1080ed0d50c3Schristos   uint64_t dot_value_;
1081ed0d50c3Schristos   Output_section* dot_section_;
1082ed0d50c3Schristos };
1083ed0d50c3Schristos 
1084ed0d50c3Schristos // Write the data element to the output file.
1085ed0d50c3Schristos 
1086ed0d50c3Schristos void
do_write(Output_file * of)1087ed0d50c3Schristos Output_data_expression::do_write(Output_file* of)
1088ed0d50c3Schristos {
1089ed0d50c3Schristos   unsigned char* view = of->get_output_view(this->offset(), this->data_size());
1090ed0d50c3Schristos   this->write_to_buffer(view);
1091ed0d50c3Schristos   of->write_output_view(this->offset(), this->data_size(), view);
1092ed0d50c3Schristos }
1093ed0d50c3Schristos 
1094ed0d50c3Schristos // Write the data element to a buffer.
1095ed0d50c3Schristos 
1096ed0d50c3Schristos void
do_write_to_buffer(unsigned char * buf)1097ed0d50c3Schristos Output_data_expression::do_write_to_buffer(unsigned char* buf)
1098ed0d50c3Schristos {
1099ed0d50c3Schristos   uint64_t val = this->val_->eval_with_dot(this->symtab_, this->layout_,
1100ed0d50c3Schristos 					   true, this->dot_value_,
1101ed0d50c3Schristos 					   this->dot_section_, NULL, NULL,
1102ed0d50c3Schristos 					   false);
1103ed0d50c3Schristos 
1104ed0d50c3Schristos   if (parameters->target().is_big_endian())
1105ed0d50c3Schristos     this->endian_write_to_buffer<true>(val, buf);
1106ed0d50c3Schristos   else
1107ed0d50c3Schristos     this->endian_write_to_buffer<false>(val, buf);
1108ed0d50c3Schristos }
1109ed0d50c3Schristos 
1110ed0d50c3Schristos template<bool big_endian>
1111ed0d50c3Schristos void
endian_write_to_buffer(uint64_t val,unsigned char * buf)1112ed0d50c3Schristos Output_data_expression::endian_write_to_buffer(uint64_t val,
1113ed0d50c3Schristos 					       unsigned char* buf)
1114ed0d50c3Schristos {
1115ed0d50c3Schristos   switch (this->data_size())
1116ed0d50c3Schristos     {
1117ed0d50c3Schristos     case 1:
1118ed0d50c3Schristos       elfcpp::Swap_unaligned<8, big_endian>::writeval(buf, val);
1119ed0d50c3Schristos       break;
1120ed0d50c3Schristos     case 2:
1121ed0d50c3Schristos       elfcpp::Swap_unaligned<16, big_endian>::writeval(buf, val);
1122ed0d50c3Schristos       break;
1123ed0d50c3Schristos     case 4:
1124ed0d50c3Schristos       elfcpp::Swap_unaligned<32, big_endian>::writeval(buf, val);
1125ed0d50c3Schristos       break;
1126ed0d50c3Schristos     case 8:
1127ed0d50c3Schristos       if (parameters->target().get_size() == 32)
1128ed0d50c3Schristos 	{
1129ed0d50c3Schristos 	  val &= 0xffffffff;
1130ed0d50c3Schristos 	  if (this->is_signed_ && (val & 0x80000000) != 0)
1131ed0d50c3Schristos 	    val |= 0xffffffff00000000LL;
1132ed0d50c3Schristos 	}
1133ed0d50c3Schristos       elfcpp::Swap_unaligned<64, big_endian>::writeval(buf, val);
1134ed0d50c3Schristos       break;
1135ed0d50c3Schristos     default:
1136ed0d50c3Schristos       gold_unreachable();
1137ed0d50c3Schristos     }
1138ed0d50c3Schristos }
1139ed0d50c3Schristos 
1140ed0d50c3Schristos // A data item in an output section.
1141ed0d50c3Schristos 
1142ed0d50c3Schristos class Output_section_element_data : public Output_section_element
1143ed0d50c3Schristos {
1144ed0d50c3Schristos  public:
Output_section_element_data(int size,bool is_signed,Expression * val)1145ed0d50c3Schristos   Output_section_element_data(int size, bool is_signed, Expression* val)
1146ed0d50c3Schristos     : size_(size), is_signed_(is_signed), val_(val)
1147ed0d50c3Schristos   { }
1148ed0d50c3Schristos 
1149ed0d50c3Schristos   // If there is a data item, then we must create an output section.
1150ed0d50c3Schristos   bool
needs_output_section() const1151ed0d50c3Schristos   needs_output_section() const
1152ed0d50c3Schristos   { return true; }
1153ed0d50c3Schristos 
1154ed0d50c3Schristos   // Finalize symbols--we just need to update dot.
1155ed0d50c3Schristos   void
finalize_symbols(Symbol_table *,const Layout *,uint64_t * dot_value,Output_section **)1156ed0d50c3Schristos   finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1157ed0d50c3Schristos 		   Output_section**)
1158ed0d50c3Schristos   { *dot_value += this->size_; }
1159ed0d50c3Schristos 
1160ed0d50c3Schristos   // Store the value in the section.
1161ed0d50c3Schristos   void
1162ed0d50c3Schristos   set_section_addresses(Symbol_table*, Layout*, Output_section*, uint64_t,
1163ed0d50c3Schristos 			uint64_t* dot_value, uint64_t*, Output_section**,
1164ed0d50c3Schristos 			std::string*, Input_section_list*);
1165ed0d50c3Schristos 
1166ed0d50c3Schristos   // Print for debugging.
1167ed0d50c3Schristos   void
1168ed0d50c3Schristos   print(FILE*) const;
1169ed0d50c3Schristos 
1170ed0d50c3Schristos  private:
1171ed0d50c3Schristos   // The size in bytes.
1172ed0d50c3Schristos   int size_;
1173ed0d50c3Schristos   // Whether the value is signed.
1174ed0d50c3Schristos   bool is_signed_;
1175ed0d50c3Schristos   // The value.
1176ed0d50c3Schristos   Expression* val_;
1177ed0d50c3Schristos };
1178ed0d50c3Schristos 
1179ed0d50c3Schristos // Store the value in the section.
1180ed0d50c3Schristos 
1181ed0d50c3Schristos void
set_section_addresses(Symbol_table * symtab,Layout * layout,Output_section * os,uint64_t,uint64_t * dot_value,uint64_t *,Output_section ** dot_section,std::string *,Input_section_list *)1182ed0d50c3Schristos Output_section_element_data::set_section_addresses(
1183ed0d50c3Schristos     Symbol_table* symtab,
1184ed0d50c3Schristos     Layout* layout,
1185ed0d50c3Schristos     Output_section* os,
1186ed0d50c3Schristos     uint64_t,
1187ed0d50c3Schristos     uint64_t* dot_value,
1188ed0d50c3Schristos     uint64_t*,
1189ed0d50c3Schristos     Output_section** dot_section,
1190ed0d50c3Schristos     std::string*,
1191ed0d50c3Schristos     Input_section_list*)
1192ed0d50c3Schristos {
1193ed0d50c3Schristos   gold_assert(os != NULL);
1194ed0d50c3Schristos   Output_data_expression* expression =
1195ed0d50c3Schristos     new Output_data_expression(this->size_, this->is_signed_, this->val_,
1196ed0d50c3Schristos 			       symtab, layout, *dot_value, *dot_section);
1197ed0d50c3Schristos   os->add_output_section_data(expression);
1198ed0d50c3Schristos   layout->new_output_section_data_from_script(expression);
1199ed0d50c3Schristos   *dot_value += this->size_;
1200ed0d50c3Schristos }
1201ed0d50c3Schristos 
1202ed0d50c3Schristos // Print for debugging.
1203ed0d50c3Schristos 
1204ed0d50c3Schristos void
print(FILE * f) const1205ed0d50c3Schristos Output_section_element_data::print(FILE* f) const
1206ed0d50c3Schristos {
1207ed0d50c3Schristos   const char* s;
1208ed0d50c3Schristos   switch (this->size_)
1209ed0d50c3Schristos     {
1210ed0d50c3Schristos     case 1:
1211ed0d50c3Schristos       s = "BYTE";
1212ed0d50c3Schristos       break;
1213ed0d50c3Schristos     case 2:
1214ed0d50c3Schristos       s = "SHORT";
1215ed0d50c3Schristos       break;
1216ed0d50c3Schristos     case 4:
1217ed0d50c3Schristos       s = "LONG";
1218ed0d50c3Schristos       break;
1219ed0d50c3Schristos     case 8:
1220ed0d50c3Schristos       if (this->is_signed_)
1221ed0d50c3Schristos 	s = "SQUAD";
1222ed0d50c3Schristos       else
1223ed0d50c3Schristos 	s = "QUAD";
1224ed0d50c3Schristos       break;
1225ed0d50c3Schristos     default:
1226ed0d50c3Schristos       gold_unreachable();
1227ed0d50c3Schristos     }
1228ed0d50c3Schristos   fprintf(f, "    %s(", s);
1229ed0d50c3Schristos   this->val_->print(f);
1230ed0d50c3Schristos   fprintf(f, ")\n");
1231ed0d50c3Schristos }
1232ed0d50c3Schristos 
1233ed0d50c3Schristos // A fill value setting in an output section.
1234ed0d50c3Schristos 
1235ed0d50c3Schristos class Output_section_element_fill : public Output_section_element
1236ed0d50c3Schristos {
1237ed0d50c3Schristos  public:
Output_section_element_fill(Expression * val)1238ed0d50c3Schristos   Output_section_element_fill(Expression* val)
1239ed0d50c3Schristos     : val_(val)
1240ed0d50c3Schristos   { }
1241ed0d50c3Schristos 
1242ed0d50c3Schristos   // Update the fill value while setting section addresses.
1243ed0d50c3Schristos   void
set_section_addresses(Symbol_table * symtab,Layout * layout,Output_section *,uint64_t,uint64_t * dot_value,uint64_t *,Output_section ** dot_section,std::string * fill,Input_section_list *)1244ed0d50c3Schristos   set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1245ed0d50c3Schristos 			uint64_t, uint64_t* dot_value, uint64_t*,
1246ed0d50c3Schristos 			Output_section** dot_section,
1247ed0d50c3Schristos 			std::string* fill, Input_section_list*)
1248ed0d50c3Schristos   {
1249ed0d50c3Schristos     Output_section* fill_section;
1250ed0d50c3Schristos     uint64_t fill_val = this->val_->eval_with_dot(symtab, layout, false,
1251ed0d50c3Schristos 						  *dot_value, *dot_section,
1252ed0d50c3Schristos 						  &fill_section, NULL, false);
1253ed0d50c3Schristos     if (fill_section != NULL)
1254ed0d50c3Schristos       gold_warning(_("fill value is not absolute"));
1255ed0d50c3Schristos     // FIXME: The GNU linker supports fill values of arbitrary length.
1256ed0d50c3Schristos     unsigned char fill_buff[4];
1257ed0d50c3Schristos     elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
1258ed0d50c3Schristos     fill->assign(reinterpret_cast<char*>(fill_buff), 4);
1259ed0d50c3Schristos   }
1260ed0d50c3Schristos 
1261ed0d50c3Schristos   // Print for debugging.
1262ed0d50c3Schristos   void
print(FILE * f) const1263ed0d50c3Schristos   print(FILE* f) const
1264ed0d50c3Schristos   {
1265ed0d50c3Schristos     fprintf(f, "    FILL(");
1266ed0d50c3Schristos     this->val_->print(f);
1267ed0d50c3Schristos     fprintf(f, ")\n");
1268ed0d50c3Schristos   }
1269ed0d50c3Schristos 
1270ed0d50c3Schristos  private:
1271ed0d50c3Schristos   // The new fill value.
1272ed0d50c3Schristos   Expression* val_;
1273ed0d50c3Schristos };
1274ed0d50c3Schristos 
1275ed0d50c3Schristos // An input section specification in an output section
1276ed0d50c3Schristos 
1277ed0d50c3Schristos class Output_section_element_input : public Output_section_element
1278ed0d50c3Schristos {
1279ed0d50c3Schristos  public:
1280ed0d50c3Schristos   Output_section_element_input(const Input_section_spec* spec, bool keep);
1281ed0d50c3Schristos 
1282ed0d50c3Schristos   // Finalize symbols--just update the value of the dot symbol.
1283ed0d50c3Schristos   void
finalize_symbols(Symbol_table *,const Layout *,uint64_t * dot_value,Output_section ** dot_section)1284ed0d50c3Schristos   finalize_symbols(Symbol_table*, const Layout*, uint64_t* dot_value,
1285ed0d50c3Schristos 		   Output_section** dot_section)
1286ed0d50c3Schristos   {
1287ed0d50c3Schristos     *dot_value = this->final_dot_value_;
1288ed0d50c3Schristos     *dot_section = this->final_dot_section_;
1289ed0d50c3Schristos   }
1290ed0d50c3Schristos 
1291ed0d50c3Schristos   // See whether we match FILE_NAME and SECTION_NAME as an input section.
1292ed0d50c3Schristos   // If we do then also indicate whether the section should be KEPT.
1293ed0d50c3Schristos   bool
1294ed0d50c3Schristos   match_name(const char* file_name, const char* section_name, bool* keep) const;
1295ed0d50c3Schristos 
1296ed0d50c3Schristos   // Set the section address.
1297ed0d50c3Schristos   void
1298ed0d50c3Schristos   set_section_addresses(Symbol_table* symtab, Layout* layout, Output_section*,
1299ed0d50c3Schristos 			uint64_t subalign, uint64_t* dot_value, uint64_t*,
1300ed0d50c3Schristos 			Output_section**, std::string* fill,
1301ed0d50c3Schristos 			Input_section_list*);
1302ed0d50c3Schristos 
1303ed0d50c3Schristos   // Print for debugging.
1304ed0d50c3Schristos   void
1305ed0d50c3Schristos   print(FILE* f) const;
1306ed0d50c3Schristos 
1307ed0d50c3Schristos  private:
1308ed0d50c3Schristos   // An input section pattern.
1309ed0d50c3Schristos   struct Input_section_pattern
1310ed0d50c3Schristos   {
1311ed0d50c3Schristos     std::string pattern;
1312ed0d50c3Schristos     bool pattern_is_wildcard;
1313ed0d50c3Schristos     Sort_wildcard sort;
1314ed0d50c3Schristos 
Input_section_patterngold::Output_section_element_input::Input_section_pattern1315ed0d50c3Schristos     Input_section_pattern(const char* patterna, size_t patternlena,
1316ed0d50c3Schristos 			  Sort_wildcard sorta)
1317ed0d50c3Schristos       : pattern(patterna, patternlena),
1318ed0d50c3Schristos 	pattern_is_wildcard(is_wildcard_string(this->pattern.c_str())),
1319ed0d50c3Schristos 	sort(sorta)
1320ed0d50c3Schristos     { }
1321ed0d50c3Schristos   };
1322ed0d50c3Schristos 
1323ed0d50c3Schristos   typedef std::vector<Input_section_pattern> Input_section_patterns;
1324ed0d50c3Schristos 
1325ed0d50c3Schristos   // Filename_exclusions is a pair of filename pattern and a bool
1326ed0d50c3Schristos   // indicating whether the filename is a wildcard.
1327ed0d50c3Schristos   typedef std::vector<std::pair<std::string, bool> > Filename_exclusions;
1328ed0d50c3Schristos 
1329ed0d50c3Schristos   // Return whether STRING matches PATTERN, where IS_WILDCARD_PATTERN
1330ed0d50c3Schristos   // indicates whether this is a wildcard pattern.
1331ed0d50c3Schristos   static inline bool
match(const char * string,const char * pattern,bool is_wildcard_pattern)1332ed0d50c3Schristos   match(const char* string, const char* pattern, bool is_wildcard_pattern)
1333ed0d50c3Schristos   {
1334ed0d50c3Schristos     return (is_wildcard_pattern
1335ed0d50c3Schristos 	    ? fnmatch(pattern, string, 0) == 0
1336ed0d50c3Schristos 	    : strcmp(string, pattern) == 0);
1337ed0d50c3Schristos   }
1338ed0d50c3Schristos 
1339ed0d50c3Schristos   // See if we match a file name.
1340ed0d50c3Schristos   bool
1341ed0d50c3Schristos   match_file_name(const char* file_name) const;
1342ed0d50c3Schristos 
1343ed0d50c3Schristos   // The file name pattern.  If this is the empty string, we match all
1344ed0d50c3Schristos   // files.
1345ed0d50c3Schristos   std::string filename_pattern_;
1346ed0d50c3Schristos   // Whether the file name pattern is a wildcard.
1347ed0d50c3Schristos   bool filename_is_wildcard_;
1348ed0d50c3Schristos   // How the file names should be sorted.  This may only be
1349ed0d50c3Schristos   // SORT_WILDCARD_NONE or SORT_WILDCARD_BY_NAME.
1350ed0d50c3Schristos   Sort_wildcard filename_sort_;
1351ed0d50c3Schristos   // The list of file names to exclude.
1352ed0d50c3Schristos   Filename_exclusions filename_exclusions_;
1353ed0d50c3Schristos   // The list of input section patterns.
1354ed0d50c3Schristos   Input_section_patterns input_section_patterns_;
1355ed0d50c3Schristos   // Whether to keep this section when garbage collecting.
1356ed0d50c3Schristos   bool keep_;
1357ed0d50c3Schristos   // The value of dot after including all matching sections.
1358ed0d50c3Schristos   uint64_t final_dot_value_;
1359ed0d50c3Schristos   // The section where dot is defined after including all matching
1360ed0d50c3Schristos   // sections.
1361ed0d50c3Schristos   Output_section* final_dot_section_;
1362ed0d50c3Schristos };
1363ed0d50c3Schristos 
1364ed0d50c3Schristos // Construct Output_section_element_input.  The parser records strings
1365ed0d50c3Schristos // as pointers into a copy of the script file, which will go away when
1366ed0d50c3Schristos // parsing is complete.  We make sure they are in std::string objects.
1367ed0d50c3Schristos 
Output_section_element_input(const Input_section_spec * spec,bool keep)1368ed0d50c3Schristos Output_section_element_input::Output_section_element_input(
1369ed0d50c3Schristos     const Input_section_spec* spec,
1370ed0d50c3Schristos     bool keep)
1371ed0d50c3Schristos   : filename_pattern_(),
1372ed0d50c3Schristos     filename_is_wildcard_(false),
1373ed0d50c3Schristos     filename_sort_(spec->file.sort),
1374ed0d50c3Schristos     filename_exclusions_(),
1375ed0d50c3Schristos     input_section_patterns_(),
1376ed0d50c3Schristos     keep_(keep),
1377ed0d50c3Schristos     final_dot_value_(0),
1378ed0d50c3Schristos     final_dot_section_(NULL)
1379ed0d50c3Schristos {
1380ed0d50c3Schristos   // The filename pattern "*" is common, and matches all files.  Turn
1381ed0d50c3Schristos   // it into the empty string.
1382ed0d50c3Schristos   if (spec->file.name.length != 1 || spec->file.name.value[0] != '*')
1383ed0d50c3Schristos     this->filename_pattern_.assign(spec->file.name.value,
1384ed0d50c3Schristos 				   spec->file.name.length);
1385ed0d50c3Schristos   this->filename_is_wildcard_ = is_wildcard_string(this->filename_pattern_.c_str());
1386ed0d50c3Schristos 
1387ed0d50c3Schristos   if (spec->input_sections.exclude != NULL)
1388ed0d50c3Schristos     {
1389ed0d50c3Schristos       for (String_list::const_iterator p =
1390ed0d50c3Schristos 	     spec->input_sections.exclude->begin();
1391ed0d50c3Schristos 	   p != spec->input_sections.exclude->end();
1392ed0d50c3Schristos 	   ++p)
1393ed0d50c3Schristos 	{
1394ed0d50c3Schristos 	  bool is_wildcard = is_wildcard_string((*p).c_str());
1395ed0d50c3Schristos 	  this->filename_exclusions_.push_back(std::make_pair(*p,
1396ed0d50c3Schristos 							      is_wildcard));
1397ed0d50c3Schristos 	}
1398ed0d50c3Schristos     }
1399ed0d50c3Schristos 
1400ed0d50c3Schristos   if (spec->input_sections.sections != NULL)
1401ed0d50c3Schristos     {
1402ed0d50c3Schristos       Input_section_patterns& isp(this->input_section_patterns_);
1403ed0d50c3Schristos       for (String_sort_list::const_iterator p =
1404ed0d50c3Schristos 	     spec->input_sections.sections->begin();
1405ed0d50c3Schristos 	   p != spec->input_sections.sections->end();
1406ed0d50c3Schristos 	   ++p)
1407ed0d50c3Schristos 	isp.push_back(Input_section_pattern(p->name.value, p->name.length,
1408ed0d50c3Schristos 					    p->sort));
1409ed0d50c3Schristos     }
1410ed0d50c3Schristos }
1411ed0d50c3Schristos 
1412ed0d50c3Schristos // See whether we match FILE_NAME.
1413ed0d50c3Schristos 
1414ed0d50c3Schristos bool
match_file_name(const char * file_name) const1415ed0d50c3Schristos Output_section_element_input::match_file_name(const char* file_name) const
1416ed0d50c3Schristos {
1417ed0d50c3Schristos   if (!this->filename_pattern_.empty())
1418ed0d50c3Schristos     {
1419ed0d50c3Schristos       // If we were called with no filename, we refuse to match a
1420ed0d50c3Schristos       // pattern which requires a file name.
1421ed0d50c3Schristos       if (file_name == NULL)
1422ed0d50c3Schristos 	return false;
1423ed0d50c3Schristos 
1424ed0d50c3Schristos       if (!match(file_name, this->filename_pattern_.c_str(),
1425ed0d50c3Schristos 		 this->filename_is_wildcard_))
1426ed0d50c3Schristos 	return false;
1427ed0d50c3Schristos     }
1428ed0d50c3Schristos 
1429ed0d50c3Schristos   if (file_name != NULL)
1430ed0d50c3Schristos     {
1431ed0d50c3Schristos       // Now we have to see whether FILE_NAME matches one of the
1432ed0d50c3Schristos       // exclusion patterns, if any.
1433ed0d50c3Schristos       for (Filename_exclusions::const_iterator p =
1434ed0d50c3Schristos 	     this->filename_exclusions_.begin();
1435ed0d50c3Schristos 	   p != this->filename_exclusions_.end();
1436ed0d50c3Schristos 	   ++p)
1437ed0d50c3Schristos 	{
1438ed0d50c3Schristos 	  if (match(file_name, p->first.c_str(), p->second))
1439ed0d50c3Schristos 	    return false;
1440ed0d50c3Schristos 	}
1441ed0d50c3Schristos     }
1442ed0d50c3Schristos 
1443ed0d50c3Schristos   return true;
1444ed0d50c3Schristos }
1445ed0d50c3Schristos 
1446ed0d50c3Schristos // See whether we match FILE_NAME and SECTION_NAME.  If we do then
1447ed0d50c3Schristos // KEEP indicates whether the section should survive garbage collection.
1448ed0d50c3Schristos 
1449ed0d50c3Schristos bool
match_name(const char * file_name,const char * section_name,bool * keep) const1450ed0d50c3Schristos Output_section_element_input::match_name(const char* file_name,
1451ed0d50c3Schristos 					 const char* section_name,
1452ed0d50c3Schristos 					 bool *keep) const
1453ed0d50c3Schristos {
1454ed0d50c3Schristos   if (!this->match_file_name(file_name))
1455ed0d50c3Schristos     return false;
1456ed0d50c3Schristos 
1457ed0d50c3Schristos   *keep = this->keep_;
1458ed0d50c3Schristos 
1459ed0d50c3Schristos   // If there are no section name patterns, then we match.
1460ed0d50c3Schristos   if (this->input_section_patterns_.empty())
1461ed0d50c3Schristos     return true;
1462ed0d50c3Schristos 
1463ed0d50c3Schristos   // See whether we match the section name patterns.
1464ed0d50c3Schristos   for (Input_section_patterns::const_iterator p =
1465ed0d50c3Schristos 	 this->input_section_patterns_.begin();
1466ed0d50c3Schristos        p != this->input_section_patterns_.end();
1467ed0d50c3Schristos        ++p)
1468ed0d50c3Schristos     {
1469ed0d50c3Schristos       if (match(section_name, p->pattern.c_str(), p->pattern_is_wildcard))
1470ed0d50c3Schristos 	return true;
1471ed0d50c3Schristos     }
1472ed0d50c3Schristos 
1473ed0d50c3Schristos   // We didn't match any section names, so we didn't match.
1474ed0d50c3Schristos   return false;
1475ed0d50c3Schristos }
1476ed0d50c3Schristos 
1477ed0d50c3Schristos // Information we use to sort the input sections.
1478ed0d50c3Schristos 
1479ed0d50c3Schristos class Input_section_info
1480ed0d50c3Schristos {
1481ed0d50c3Schristos  public:
Input_section_info(const Output_section::Input_section & input_section)1482ed0d50c3Schristos   Input_section_info(const Output_section::Input_section& input_section)
1483ed0d50c3Schristos     : input_section_(input_section), section_name_(),
1484ed0d50c3Schristos       size_(0), addralign_(1)
1485ed0d50c3Schristos   { }
1486ed0d50c3Schristos 
1487ed0d50c3Schristos   // Return the simple input section.
1488ed0d50c3Schristos   const Output_section::Input_section&
input_section() const1489ed0d50c3Schristos   input_section() const
1490ed0d50c3Schristos   { return this->input_section_; }
1491ed0d50c3Schristos 
1492ed0d50c3Schristos   // Return the object.
1493ed0d50c3Schristos   Relobj*
relobj() const1494ed0d50c3Schristos   relobj() const
1495ed0d50c3Schristos   { return this->input_section_.relobj(); }
1496ed0d50c3Schristos 
1497ed0d50c3Schristos   // Return the section index.
1498ed0d50c3Schristos   unsigned int
shndx()1499ed0d50c3Schristos   shndx()
1500ed0d50c3Schristos   { return this->input_section_.shndx(); }
1501ed0d50c3Schristos 
1502ed0d50c3Schristos   // Return the section name.
1503ed0d50c3Schristos   const std::string&
section_name() const1504ed0d50c3Schristos   section_name() const
1505ed0d50c3Schristos   { return this->section_name_; }
1506ed0d50c3Schristos 
1507ed0d50c3Schristos   // Set the section name.
1508ed0d50c3Schristos   void
set_section_name(const std::string name)1509ed0d50c3Schristos   set_section_name(const std::string name)
1510ed0d50c3Schristos   {
1511ed0d50c3Schristos     if (is_compressed_debug_section(name.c_str()))
1512ed0d50c3Schristos       this->section_name_ = corresponding_uncompressed_section_name(name);
1513ed0d50c3Schristos     else
1514ed0d50c3Schristos       this->section_name_ = name;
1515ed0d50c3Schristos   }
1516ed0d50c3Schristos 
1517ed0d50c3Schristos   // Return the section size.
1518ed0d50c3Schristos   uint64_t
size() const1519ed0d50c3Schristos   size() const
1520ed0d50c3Schristos   { return this->size_; }
1521ed0d50c3Schristos 
1522ed0d50c3Schristos   // Set the section size.
1523ed0d50c3Schristos   void
set_size(uint64_t size)1524ed0d50c3Schristos   set_size(uint64_t size)
1525ed0d50c3Schristos   { this->size_ = size; }
1526ed0d50c3Schristos 
1527ed0d50c3Schristos   // Return the address alignment.
1528ed0d50c3Schristos   uint64_t
addralign() const1529ed0d50c3Schristos   addralign() const
1530ed0d50c3Schristos   { return this->addralign_; }
1531ed0d50c3Schristos 
1532ed0d50c3Schristos   // Set the address alignment.
1533ed0d50c3Schristos   void
set_addralign(uint64_t addralign)1534ed0d50c3Schristos   set_addralign(uint64_t addralign)
1535ed0d50c3Schristos   { this->addralign_ = addralign; }
1536ed0d50c3Schristos 
1537ed0d50c3Schristos  private:
1538ed0d50c3Schristos   // Input section, can be a relaxed section.
1539ed0d50c3Schristos   Output_section::Input_section input_section_;
1540ed0d50c3Schristos   // Name of the section.
1541ed0d50c3Schristos   std::string section_name_;
1542ed0d50c3Schristos   // Section size.
1543ed0d50c3Schristos   uint64_t size_;
1544ed0d50c3Schristos   // Address alignment.
1545ed0d50c3Schristos   uint64_t addralign_;
1546ed0d50c3Schristos };
1547ed0d50c3Schristos 
1548ed0d50c3Schristos // A class to sort the input sections.
1549ed0d50c3Schristos 
1550ed0d50c3Schristos class Input_section_sorter
1551ed0d50c3Schristos {
1552ed0d50c3Schristos  public:
Input_section_sorter(Sort_wildcard filename_sort,Sort_wildcard section_sort)1553ed0d50c3Schristos   Input_section_sorter(Sort_wildcard filename_sort, Sort_wildcard section_sort)
1554ed0d50c3Schristos     : filename_sort_(filename_sort), section_sort_(section_sort)
1555ed0d50c3Schristos   { }
1556ed0d50c3Schristos 
1557ed0d50c3Schristos   bool
1558ed0d50c3Schristos   operator()(const Input_section_info&, const Input_section_info&) const;
1559ed0d50c3Schristos 
1560ed0d50c3Schristos  private:
1561ed0d50c3Schristos   static unsigned long
1562ed0d50c3Schristos   get_init_priority(const char*);
1563ed0d50c3Schristos 
1564ed0d50c3Schristos   Sort_wildcard filename_sort_;
1565ed0d50c3Schristos   Sort_wildcard section_sort_;
1566ed0d50c3Schristos };
1567ed0d50c3Schristos 
1568ed0d50c3Schristos // Return a relative priority of the section with the specified NAME
1569ed0d50c3Schristos // (a lower value meand a higher priority), or 0 if it should be compared
1570ed0d50c3Schristos // with others as strings.
1571ed0d50c3Schristos // The implementation of this function is copied from ld/ldlang.c.
1572ed0d50c3Schristos 
1573ed0d50c3Schristos unsigned long
get_init_priority(const char * name)1574ed0d50c3Schristos Input_section_sorter::get_init_priority(const char* name)
1575ed0d50c3Schristos {
1576ed0d50c3Schristos   char* end;
1577ed0d50c3Schristos   unsigned long init_priority;
1578ed0d50c3Schristos 
1579ed0d50c3Schristos   // GCC uses the following section names for the init_priority
1580ed0d50c3Schristos   // attribute with numerical values 101 and 65535 inclusive. A
1581ed0d50c3Schristos   // lower value means a higher priority.
1582ed0d50c3Schristos   //
1583ed0d50c3Schristos   // 1: .init_array.NNNN/.fini_array.NNNN: Where NNNN is the
1584ed0d50c3Schristos   //    decimal numerical value of the init_priority attribute.
1585ed0d50c3Schristos   //    The order of execution in .init_array is forward and
1586ed0d50c3Schristos   //    .fini_array is backward.
1587ed0d50c3Schristos   // 2: .ctors.NNNN/.dtors.NNNN: Where NNNN is 65535 minus the
1588ed0d50c3Schristos   //    decimal numerical value of the init_priority attribute.
1589ed0d50c3Schristos   //    The order of execution in .ctors is backward and .dtors
1590ed0d50c3Schristos   //    is forward.
1591ed0d50c3Schristos 
1592ed0d50c3Schristos   if (strncmp(name, ".init_array.", 12) == 0
1593ed0d50c3Schristos       || strncmp(name, ".fini_array.", 12) == 0)
1594ed0d50c3Schristos     {
1595ed0d50c3Schristos       init_priority = strtoul(name + 12, &end, 10);
1596ed0d50c3Schristos       return *end ? 0 : init_priority;
1597ed0d50c3Schristos     }
1598ed0d50c3Schristos   else if (strncmp(name, ".ctors.", 7) == 0
1599ed0d50c3Schristos 	   || strncmp(name, ".dtors.", 7) == 0)
1600ed0d50c3Schristos     {
1601ed0d50c3Schristos       init_priority = strtoul(name + 7, &end, 10);
1602ed0d50c3Schristos       return *end ? 0 : 65535 - init_priority;
1603ed0d50c3Schristos     }
1604ed0d50c3Schristos 
1605ed0d50c3Schristos   return 0;
1606ed0d50c3Schristos }
1607ed0d50c3Schristos 
1608ed0d50c3Schristos bool
operator ()(const Input_section_info & isi1,const Input_section_info & isi2) const1609ed0d50c3Schristos Input_section_sorter::operator()(const Input_section_info& isi1,
1610ed0d50c3Schristos 				 const Input_section_info& isi2) const
1611ed0d50c3Schristos {
1612ed0d50c3Schristos   if (this->section_sort_ == SORT_WILDCARD_BY_INIT_PRIORITY)
1613ed0d50c3Schristos     {
1614ed0d50c3Schristos       unsigned long ip1 = get_init_priority(isi1.section_name().c_str());
1615ed0d50c3Schristos       unsigned long ip2 = get_init_priority(isi2.section_name().c_str());
1616ed0d50c3Schristos       if (ip1 != 0 && ip2 != 0 && ip1 != ip2)
1617ed0d50c3Schristos 	return ip1 < ip2;
1618ed0d50c3Schristos     }
1619ed0d50c3Schristos   if (this->section_sort_ == SORT_WILDCARD_BY_NAME
1620ed0d50c3Schristos       || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1621ed0d50c3Schristos       || (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
1622ed0d50c3Schristos 	  && isi1.addralign() == isi2.addralign())
1623ed0d50c3Schristos       || this->section_sort_ == SORT_WILDCARD_BY_INIT_PRIORITY)
1624ed0d50c3Schristos     {
1625ed0d50c3Schristos       if (isi1.section_name() != isi2.section_name())
1626ed0d50c3Schristos 	return isi1.section_name() < isi2.section_name();
1627ed0d50c3Schristos     }
1628ed0d50c3Schristos   if (this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT
1629ed0d50c3Schristos       || this->section_sort_ == SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
1630ed0d50c3Schristos       || this->section_sort_ == SORT_WILDCARD_BY_ALIGNMENT_BY_NAME)
1631ed0d50c3Schristos     {
1632ed0d50c3Schristos       if (isi1.addralign() != isi2.addralign())
1633ed0d50c3Schristos 	return isi1.addralign() < isi2.addralign();
1634ed0d50c3Schristos     }
1635ed0d50c3Schristos   if (this->filename_sort_ == SORT_WILDCARD_BY_NAME)
1636ed0d50c3Schristos     {
1637ed0d50c3Schristos       if (isi1.relobj()->name() != isi2.relobj()->name())
1638ed0d50c3Schristos 	return (isi1.relobj()->name() < isi2.relobj()->name());
1639ed0d50c3Schristos     }
1640ed0d50c3Schristos 
1641ed0d50c3Schristos   // Otherwise we leave them in the same order.
1642ed0d50c3Schristos   return false;
1643ed0d50c3Schristos }
1644ed0d50c3Schristos 
1645ed0d50c3Schristos // Set the section address.  Look in INPUT_SECTIONS for sections which
1646ed0d50c3Schristos // match this spec, sort them as specified, and add them to the output
1647ed0d50c3Schristos // section.
1648ed0d50c3Schristos 
1649ed0d50c3Schristos void
set_section_addresses(Symbol_table *,Layout * layout,Output_section * output_section,uint64_t subalign,uint64_t * dot_value,uint64_t *,Output_section ** dot_section,std::string * fill,Input_section_list * input_sections)1650ed0d50c3Schristos Output_section_element_input::set_section_addresses(
1651ed0d50c3Schristos     Symbol_table*,
1652ed0d50c3Schristos     Layout* layout,
1653ed0d50c3Schristos     Output_section* output_section,
1654ed0d50c3Schristos     uint64_t subalign,
1655ed0d50c3Schristos     uint64_t* dot_value,
1656ed0d50c3Schristos     uint64_t*,
1657ed0d50c3Schristos     Output_section** dot_section,
1658ed0d50c3Schristos     std::string* fill,
1659ed0d50c3Schristos     Input_section_list* input_sections)
1660ed0d50c3Schristos {
1661ed0d50c3Schristos   // We build a list of sections which match each
1662ed0d50c3Schristos   // Input_section_pattern.
1663ed0d50c3Schristos 
1664ed0d50c3Schristos   // If none of the patterns specify a sort option, we throw all
1665ed0d50c3Schristos   // matching input sections into a single bin, in the order we
1666ed0d50c3Schristos   // find them.  Otherwise, we put matching input sections into
1667ed0d50c3Schristos   // a separate bin for each pattern, and sort each one as
1668ed0d50c3Schristos   // specified.  Thus, an input section spec like this:
1669ed0d50c3Schristos   //   *(.foo .bar)
1670ed0d50c3Schristos   // will group all .foo and .bar sections in the order seen,
1671ed0d50c3Schristos   // whereas this:
1672ed0d50c3Schristos   //   *(.foo) *(.bar)
1673ed0d50c3Schristos   // will group all .foo sections followed by all .bar sections.
1674ed0d50c3Schristos   // This matches Gnu ld behavior.
1675ed0d50c3Schristos 
1676ed0d50c3Schristos   // Things get really weird, though, when you add a sort spec
1677ed0d50c3Schristos   // on some, but not all, of the patterns, like this:
1678ed0d50c3Schristos   //   *(SORT_BY_NAME(.foo) .bar)
1679ed0d50c3Schristos   // We do not attempt to match Gnu ld behavior in this case.
1680ed0d50c3Schristos 
1681ed0d50c3Schristos   typedef std::vector<std::vector<Input_section_info> > Matching_sections;
1682ed0d50c3Schristos   size_t input_pattern_count = this->input_section_patterns_.size();
1683ed0d50c3Schristos   size_t bin_count = 1;
1684ed0d50c3Schristos   bool any_patterns_with_sort = false;
1685ed0d50c3Schristos   for (size_t i = 0; i < input_pattern_count; ++i)
1686ed0d50c3Schristos     {
1687ed0d50c3Schristos       const Input_section_pattern& isp(this->input_section_patterns_[i]);
1688ed0d50c3Schristos       if (isp.sort != SORT_WILDCARD_NONE)
1689ed0d50c3Schristos 	any_patterns_with_sort = true;
1690ed0d50c3Schristos     }
1691ed0d50c3Schristos   if (any_patterns_with_sort)
1692ed0d50c3Schristos     bin_count = input_pattern_count;
1693ed0d50c3Schristos   Matching_sections matching_sections(bin_count);
1694ed0d50c3Schristos 
1695ed0d50c3Schristos   // Look through the list of sections for this output section.  Add
1696ed0d50c3Schristos   // each one which matches to one of the elements of
1697ed0d50c3Schristos   // MATCHING_SECTIONS.
1698ed0d50c3Schristos 
1699ed0d50c3Schristos   Input_section_list::iterator p = input_sections->begin();
1700ed0d50c3Schristos   while (p != input_sections->end())
1701ed0d50c3Schristos     {
1702ed0d50c3Schristos       Relobj* relobj = p->relobj();
1703ed0d50c3Schristos       unsigned int shndx = p->shndx();
1704ed0d50c3Schristos       Input_section_info isi(*p);
1705ed0d50c3Schristos 
1706ed0d50c3Schristos       // Calling section_name and section_addralign is not very
1707ed0d50c3Schristos       // efficient.
1708ed0d50c3Schristos 
1709ed0d50c3Schristos       // Lock the object so that we can get information about the
1710ed0d50c3Schristos       // section.  This is OK since we know we are single-threaded
1711ed0d50c3Schristos       // here.
1712ed0d50c3Schristos       {
1713ed0d50c3Schristos 	const Task* task = reinterpret_cast<const Task*>(-1);
1714ed0d50c3Schristos 	Task_lock_obj<Object> tl(task, relobj);
1715ed0d50c3Schristos 
1716ed0d50c3Schristos 	isi.set_section_name(relobj->section_name(shndx));
1717ed0d50c3Schristos 	if (p->is_relaxed_input_section())
1718ed0d50c3Schristos 	  {
1719ed0d50c3Schristos 	    // We use current data size because relaxed section sizes may not
1720ed0d50c3Schristos 	    // have finalized yet.
1721ed0d50c3Schristos 	    isi.set_size(p->relaxed_input_section()->current_data_size());
1722ed0d50c3Schristos 	    isi.set_addralign(p->relaxed_input_section()->addralign());
1723ed0d50c3Schristos 	  }
1724ed0d50c3Schristos 	else
1725ed0d50c3Schristos 	  {
1726ed0d50c3Schristos 	    isi.set_size(relobj->section_size(shndx));
1727ed0d50c3Schristos 	    isi.set_addralign(relobj->section_addralign(shndx));
1728ed0d50c3Schristos 	  }
1729ed0d50c3Schristos       }
1730ed0d50c3Schristos 
1731ed0d50c3Schristos       if (!this->match_file_name(relobj->name().c_str()))
1732ed0d50c3Schristos 	++p;
1733ed0d50c3Schristos       else if (this->input_section_patterns_.empty())
1734ed0d50c3Schristos 	{
1735ed0d50c3Schristos 	  matching_sections[0].push_back(isi);
1736ed0d50c3Schristos 	  p = input_sections->erase(p);
1737ed0d50c3Schristos 	}
1738ed0d50c3Schristos       else
1739ed0d50c3Schristos 	{
1740ed0d50c3Schristos 	  size_t i;
1741ed0d50c3Schristos 	  for (i = 0; i < input_pattern_count; ++i)
1742ed0d50c3Schristos 	    {
1743ed0d50c3Schristos 	      const Input_section_pattern&
1744ed0d50c3Schristos 		isp(this->input_section_patterns_[i]);
1745ed0d50c3Schristos 	      if (match(isi.section_name().c_str(), isp.pattern.c_str(),
1746ed0d50c3Schristos 			isp.pattern_is_wildcard))
1747ed0d50c3Schristos 		break;
1748ed0d50c3Schristos 	    }
1749ed0d50c3Schristos 
1750ed0d50c3Schristos 	  if (i >= input_pattern_count)
1751ed0d50c3Schristos 	    ++p;
1752ed0d50c3Schristos 	  else
1753ed0d50c3Schristos 	    {
1754ed0d50c3Schristos 	      if (i >= bin_count)
1755ed0d50c3Schristos 		i = 0;
1756ed0d50c3Schristos 	      matching_sections[i].push_back(isi);
1757ed0d50c3Schristos 	      p = input_sections->erase(p);
1758ed0d50c3Schristos 	    }
1759ed0d50c3Schristos 	}
1760ed0d50c3Schristos     }
1761ed0d50c3Schristos 
1762ed0d50c3Schristos   // Look through MATCHING_SECTIONS.  Sort each one as specified,
1763ed0d50c3Schristos   // using a stable sort so that we get the default order when
1764ed0d50c3Schristos   // sections are otherwise equal.  Add each input section to the
1765ed0d50c3Schristos   // output section.
1766ed0d50c3Schristos 
1767ed0d50c3Schristos   uint64_t dot = *dot_value;
1768ed0d50c3Schristos   for (size_t i = 0; i < bin_count; ++i)
1769ed0d50c3Schristos     {
1770ed0d50c3Schristos       if (matching_sections[i].empty())
1771ed0d50c3Schristos 	continue;
1772ed0d50c3Schristos 
1773ed0d50c3Schristos       gold_assert(output_section != NULL);
1774ed0d50c3Schristos 
1775ed0d50c3Schristos       const Input_section_pattern& isp(this->input_section_patterns_[i]);
1776ed0d50c3Schristos       if (isp.sort != SORT_WILDCARD_NONE
1777ed0d50c3Schristos 	  || this->filename_sort_ != SORT_WILDCARD_NONE)
1778ed0d50c3Schristos 	std::stable_sort(matching_sections[i].begin(),
1779ed0d50c3Schristos 			 matching_sections[i].end(),
1780ed0d50c3Schristos 			 Input_section_sorter(this->filename_sort_,
1781ed0d50c3Schristos 					      isp.sort));
1782ed0d50c3Schristos 
1783ed0d50c3Schristos       for (std::vector<Input_section_info>::const_iterator p =
1784ed0d50c3Schristos 	     matching_sections[i].begin();
1785ed0d50c3Schristos 	   p != matching_sections[i].end();
1786ed0d50c3Schristos 	   ++p)
1787ed0d50c3Schristos 	{
178806324dcfSchristos 	  // Override the original address alignment if SUBALIGN is specified.
178906324dcfSchristos 	  // We need to make a copy of the input section to modify the
179006324dcfSchristos 	  // alignment.
1791ed0d50c3Schristos 	  Output_section::Input_section sis(p->input_section());
1792ed0d50c3Schristos 
1793ed0d50c3Schristos 	  uint64_t this_subalign = sis.addralign();
1794ed0d50c3Schristos 	  if (!sis.is_input_section())
1795ed0d50c3Schristos 	    sis.output_section_data()->finalize_data_size();
1796ed0d50c3Schristos 	  uint64_t data_size = sis.data_size();
179706324dcfSchristos 	  if (subalign > 0)
1798ed0d50c3Schristos 	    {
1799ed0d50c3Schristos 	      this_subalign = subalign;
1800ed0d50c3Schristos 	      sis.set_addralign(subalign);
1801ed0d50c3Schristos 	    }
1802ed0d50c3Schristos 
1803ed0d50c3Schristos 	  uint64_t address = align_address(dot, this_subalign);
1804ed0d50c3Schristos 
1805ed0d50c3Schristos 	  if (address > dot && !fill->empty())
1806ed0d50c3Schristos 	    {
1807ed0d50c3Schristos 	      section_size_type length =
1808ed0d50c3Schristos 		convert_to_section_size_type(address - dot);
1809ed0d50c3Schristos 	      std::string this_fill = this->get_fill_string(fill, length);
1810ed0d50c3Schristos 	      Output_section_data* posd = new Output_data_const(this_fill, 0);
1811ed0d50c3Schristos 	      output_section->add_output_section_data(posd);
1812ed0d50c3Schristos 	      layout->new_output_section_data_from_script(posd);
1813ed0d50c3Schristos 	    }
1814ed0d50c3Schristos 
1815ed0d50c3Schristos 	  output_section->add_script_input_section(sis);
1816ed0d50c3Schristos 	  dot = address + data_size;
1817ed0d50c3Schristos 	}
1818ed0d50c3Schristos     }
1819ed0d50c3Schristos 
1820ed0d50c3Schristos   // An SHF_TLS/SHT_NOBITS section does not take up any
1821ed0d50c3Schristos   // address space.
1822ed0d50c3Schristos   if (output_section == NULL
1823ed0d50c3Schristos       || (output_section->flags() & elfcpp::SHF_TLS) == 0
1824ed0d50c3Schristos       || output_section->type() != elfcpp::SHT_NOBITS)
1825ed0d50c3Schristos     *dot_value = dot;
1826ed0d50c3Schristos 
1827ed0d50c3Schristos   this->final_dot_value_ = *dot_value;
1828ed0d50c3Schristos   this->final_dot_section_ = *dot_section;
1829ed0d50c3Schristos }
1830ed0d50c3Schristos 
1831ed0d50c3Schristos // Print for debugging.
1832ed0d50c3Schristos 
1833ed0d50c3Schristos void
print(FILE * f) const1834ed0d50c3Schristos Output_section_element_input::print(FILE* f) const
1835ed0d50c3Schristos {
1836ed0d50c3Schristos   fprintf(f, "    ");
1837ed0d50c3Schristos 
1838ed0d50c3Schristos   if (this->keep_)
1839ed0d50c3Schristos     fprintf(f, "KEEP(");
1840ed0d50c3Schristos 
1841ed0d50c3Schristos   if (!this->filename_pattern_.empty())
1842ed0d50c3Schristos     {
1843ed0d50c3Schristos       bool need_close_paren = false;
1844ed0d50c3Schristos       switch (this->filename_sort_)
1845ed0d50c3Schristos 	{
1846ed0d50c3Schristos 	case SORT_WILDCARD_NONE:
1847ed0d50c3Schristos 	  break;
1848ed0d50c3Schristos 	case SORT_WILDCARD_BY_NAME:
1849ed0d50c3Schristos 	  fprintf(f, "SORT_BY_NAME(");
1850ed0d50c3Schristos 	  need_close_paren = true;
1851ed0d50c3Schristos 	  break;
1852ed0d50c3Schristos 	default:
1853ed0d50c3Schristos 	  gold_unreachable();
1854ed0d50c3Schristos 	}
1855ed0d50c3Schristos 
1856ed0d50c3Schristos       fprintf(f, "%s", this->filename_pattern_.c_str());
1857ed0d50c3Schristos 
1858ed0d50c3Schristos       if (need_close_paren)
1859ed0d50c3Schristos 	fprintf(f, ")");
1860ed0d50c3Schristos     }
1861ed0d50c3Schristos 
1862ed0d50c3Schristos   if (!this->input_section_patterns_.empty()
1863ed0d50c3Schristos       || !this->filename_exclusions_.empty())
1864ed0d50c3Schristos     {
1865ed0d50c3Schristos       fprintf(f, "(");
1866ed0d50c3Schristos 
1867ed0d50c3Schristos       bool need_space = false;
1868ed0d50c3Schristos       if (!this->filename_exclusions_.empty())
1869ed0d50c3Schristos 	{
1870ed0d50c3Schristos 	  fprintf(f, "EXCLUDE_FILE(");
1871ed0d50c3Schristos 	  bool need_comma = false;
1872ed0d50c3Schristos 	  for (Filename_exclusions::const_iterator p =
1873ed0d50c3Schristos 		 this->filename_exclusions_.begin();
1874ed0d50c3Schristos 	       p != this->filename_exclusions_.end();
1875ed0d50c3Schristos 	       ++p)
1876ed0d50c3Schristos 	    {
1877ed0d50c3Schristos 	      if (need_comma)
1878ed0d50c3Schristos 		fprintf(f, ", ");
1879ed0d50c3Schristos 	      fprintf(f, "%s", p->first.c_str());
1880ed0d50c3Schristos 	      need_comma = true;
1881ed0d50c3Schristos 	    }
1882ed0d50c3Schristos 	  fprintf(f, ")");
1883ed0d50c3Schristos 	  need_space = true;
1884ed0d50c3Schristos 	}
1885ed0d50c3Schristos 
1886ed0d50c3Schristos       for (Input_section_patterns::const_iterator p =
1887ed0d50c3Schristos 	     this->input_section_patterns_.begin();
1888ed0d50c3Schristos 	   p != this->input_section_patterns_.end();
1889ed0d50c3Schristos 	   ++p)
1890ed0d50c3Schristos 	{
1891ed0d50c3Schristos 	  if (need_space)
1892ed0d50c3Schristos 	    fprintf(f, " ");
1893ed0d50c3Schristos 
1894ed0d50c3Schristos 	  int close_parens = 0;
1895ed0d50c3Schristos 	  switch (p->sort)
1896ed0d50c3Schristos 	    {
1897ed0d50c3Schristos 	    case SORT_WILDCARD_NONE:
1898ed0d50c3Schristos 	      break;
1899ed0d50c3Schristos 	    case SORT_WILDCARD_BY_NAME:
1900ed0d50c3Schristos 	      fprintf(f, "SORT_BY_NAME(");
1901ed0d50c3Schristos 	      close_parens = 1;
1902ed0d50c3Schristos 	      break;
1903ed0d50c3Schristos 	    case SORT_WILDCARD_BY_ALIGNMENT:
1904ed0d50c3Schristos 	      fprintf(f, "SORT_BY_ALIGNMENT(");
1905ed0d50c3Schristos 	      close_parens = 1;
1906ed0d50c3Schristos 	      break;
1907ed0d50c3Schristos 	    case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
1908ed0d50c3Schristos 	      fprintf(f, "SORT_BY_NAME(SORT_BY_ALIGNMENT(");
1909ed0d50c3Schristos 	      close_parens = 2;
1910ed0d50c3Schristos 	      break;
1911ed0d50c3Schristos 	    case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
1912ed0d50c3Schristos 	      fprintf(f, "SORT_BY_ALIGNMENT(SORT_BY_NAME(");
1913ed0d50c3Schristos 	      close_parens = 2;
1914ed0d50c3Schristos 	      break;
1915ed0d50c3Schristos 	    case SORT_WILDCARD_BY_INIT_PRIORITY:
1916ed0d50c3Schristos 	      fprintf(f, "SORT_BY_INIT_PRIORITY(");
1917ed0d50c3Schristos 	      close_parens = 1;
1918ed0d50c3Schristos 	      break;
1919ed0d50c3Schristos 	    default:
1920ed0d50c3Schristos 	      gold_unreachable();
1921ed0d50c3Schristos 	    }
1922ed0d50c3Schristos 
1923ed0d50c3Schristos 	  fprintf(f, "%s", p->pattern.c_str());
1924ed0d50c3Schristos 
1925ed0d50c3Schristos 	  for (int i = 0; i < close_parens; ++i)
1926ed0d50c3Schristos 	    fprintf(f, ")");
1927ed0d50c3Schristos 
1928ed0d50c3Schristos 	  need_space = true;
1929ed0d50c3Schristos 	}
1930ed0d50c3Schristos 
1931ed0d50c3Schristos       fprintf(f, ")");
1932ed0d50c3Schristos     }
1933ed0d50c3Schristos 
1934ed0d50c3Schristos   if (this->keep_)
1935ed0d50c3Schristos     fprintf(f, ")");
1936ed0d50c3Schristos 
1937ed0d50c3Schristos   fprintf(f, "\n");
1938ed0d50c3Schristos }
1939ed0d50c3Schristos 
1940ed0d50c3Schristos // An output section.
1941ed0d50c3Schristos 
1942ed0d50c3Schristos class Output_section_definition : public Sections_element
1943ed0d50c3Schristos {
1944ed0d50c3Schristos  public:
1945ed0d50c3Schristos   typedef Output_section_element::Input_section_list Input_section_list;
1946ed0d50c3Schristos 
1947ed0d50c3Schristos   Output_section_definition(const char* name, size_t namelen,
1948ed0d50c3Schristos 			    const Parser_output_section_header* header);
1949ed0d50c3Schristos 
1950ed0d50c3Schristos   // Finish the output section with the information in the trailer.
1951ed0d50c3Schristos   void
1952ed0d50c3Schristos   finish(const Parser_output_section_trailer* trailer);
1953ed0d50c3Schristos 
1954ed0d50c3Schristos   // Add a symbol to be defined.
1955ed0d50c3Schristos   void
1956ed0d50c3Schristos   add_symbol_assignment(const char* name, size_t length, Expression* value,
1957ed0d50c3Schristos 			bool provide, bool hidden);
1958ed0d50c3Schristos 
1959ed0d50c3Schristos   // Add an assignment to the special dot symbol.
1960ed0d50c3Schristos   void
1961ed0d50c3Schristos   add_dot_assignment(Expression* value);
1962ed0d50c3Schristos 
1963ed0d50c3Schristos   // Add an assertion.
1964ed0d50c3Schristos   void
1965ed0d50c3Schristos   add_assertion(Expression* check, const char* message, size_t messagelen);
1966ed0d50c3Schristos 
1967ed0d50c3Schristos   // Add a data item to the current output section.
1968ed0d50c3Schristos   void
1969ed0d50c3Schristos   add_data(int size, bool is_signed, Expression* val);
1970ed0d50c3Schristos 
1971ed0d50c3Schristos   // Add a setting for the fill value.
1972ed0d50c3Schristos   void
1973ed0d50c3Schristos   add_fill(Expression* val);
1974ed0d50c3Schristos 
1975ed0d50c3Schristos   // Add an input section specification.
1976ed0d50c3Schristos   void
1977ed0d50c3Schristos   add_input_section(const Input_section_spec* spec, bool keep);
1978ed0d50c3Schristos 
1979ed0d50c3Schristos   // Return whether the output section is relro.
1980ed0d50c3Schristos   bool
is_relro() const1981ed0d50c3Schristos   is_relro() const
1982ed0d50c3Schristos   { return this->is_relro_; }
1983ed0d50c3Schristos 
1984ed0d50c3Schristos   // Record that the output section is relro.
1985ed0d50c3Schristos   void
set_is_relro()1986ed0d50c3Schristos   set_is_relro()
1987ed0d50c3Schristos   { this->is_relro_ = true; }
1988ed0d50c3Schristos 
1989ed0d50c3Schristos   // Create any required output sections.
1990ed0d50c3Schristos   void
1991ed0d50c3Schristos   create_sections(Layout*);
1992ed0d50c3Schristos 
1993ed0d50c3Schristos   // Add any symbols being defined to the symbol table.
1994ed0d50c3Schristos   void
1995ed0d50c3Schristos   add_symbols_to_table(Symbol_table* symtab);
1996ed0d50c3Schristos 
1997ed0d50c3Schristos   // Finalize symbols and check assertions.
1998ed0d50c3Schristos   void
1999ed0d50c3Schristos   finalize_symbols(Symbol_table*, const Layout*, uint64_t*);
2000ed0d50c3Schristos 
2001ed0d50c3Schristos   // Return the output section name to use for an input file name and
2002ed0d50c3Schristos   // section name.
2003ed0d50c3Schristos   const char*
2004ed0d50c3Schristos   output_section_name(const char* file_name, const char* section_name,
2005ed0d50c3Schristos 		      Output_section***, Script_sections::Section_type*,
200606324dcfSchristos 		      bool*, bool);
2007ed0d50c3Schristos 
2008ed0d50c3Schristos   // Initialize OSP with an output section.
2009ed0d50c3Schristos   void
orphan_section_init(Orphan_section_placement * osp,Script_sections::Elements_iterator p)2010ed0d50c3Schristos   orphan_section_init(Orphan_section_placement* osp,
2011ed0d50c3Schristos 		      Script_sections::Elements_iterator p)
2012ed0d50c3Schristos   { osp->output_section_init(this->name_, this->output_section_, p); }
2013ed0d50c3Schristos 
2014ed0d50c3Schristos   // Set the section address.
2015ed0d50c3Schristos   void
2016ed0d50c3Schristos   set_section_addresses(Symbol_table* symtab, Layout* layout,
2017ed0d50c3Schristos 			uint64_t* dot_value, uint64_t*,
2018ed0d50c3Schristos 			uint64_t* load_address);
2019ed0d50c3Schristos 
2020ed0d50c3Schristos   // Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
2021ed0d50c3Schristos   // this section is constrained, and the input sections do not match,
2022ed0d50c3Schristos   // return the constraint, and set *POSD.
2023ed0d50c3Schristos   Section_constraint
2024ed0d50c3Schristos   check_constraint(Output_section_definition** posd);
2025ed0d50c3Schristos 
2026ed0d50c3Schristos   // See if this is the alternate output section for a constrained
2027ed0d50c3Schristos   // output section.  If it is, transfer the Output_section and return
2028ed0d50c3Schristos   // true.  Otherwise return false.
2029ed0d50c3Schristos   bool
2030ed0d50c3Schristos   alternate_constraint(Output_section_definition*, Section_constraint);
2031ed0d50c3Schristos 
2032ed0d50c3Schristos   // Get the list of segments to use for an allocated section when
2033ed0d50c3Schristos   // using a PHDRS clause.
2034ed0d50c3Schristos   Output_section*
2035ed0d50c3Schristos   allocate_to_segment(String_list** phdrs_list, bool* orphan);
2036ed0d50c3Schristos 
2037ed0d50c3Schristos   // Look for an output section by name and return the address, the
2038ed0d50c3Schristos   // load address, the alignment, and the size.  This is used when an
2039ed0d50c3Schristos   // expression refers to an output section which was not actually
2040ed0d50c3Schristos   // created.  This returns true if the section was found, false
2041ed0d50c3Schristos   // otherwise.
2042ed0d50c3Schristos   bool
2043ed0d50c3Schristos   get_output_section_info(const char*, uint64_t*, uint64_t*, uint64_t*,
2044ed0d50c3Schristos                           uint64_t*) const;
2045ed0d50c3Schristos 
2046ed0d50c3Schristos   // Return the associated Output_section if there is one.
2047ed0d50c3Schristos   Output_section*
get_output_section() const2048ed0d50c3Schristos   get_output_section() const
2049ed0d50c3Schristos   { return this->output_section_; }
2050ed0d50c3Schristos 
2051ed0d50c3Schristos   // Print the contents to the FILE.  This is for debugging.
2052ed0d50c3Schristos   void
2053ed0d50c3Schristos   print(FILE*) const;
2054ed0d50c3Schristos 
2055ed0d50c3Schristos   // Return the output section type if specified or Script_sections::ST_NONE.
2056ed0d50c3Schristos   Script_sections::Section_type
2057ed0d50c3Schristos   section_type() const;
2058ed0d50c3Schristos 
2059ed0d50c3Schristos   // Store the memory region to use.
2060ed0d50c3Schristos   void
2061ed0d50c3Schristos   set_memory_region(Memory_region*, bool set_vma);
2062ed0d50c3Schristos 
2063ed0d50c3Schristos   void
set_section_vma(Expression * address)2064ed0d50c3Schristos   set_section_vma(Expression* address)
2065ed0d50c3Schristos   { this->address_ = address; }
2066ed0d50c3Schristos 
2067ed0d50c3Schristos   void
set_section_lma(Expression * address)2068ed0d50c3Schristos   set_section_lma(Expression* address)
2069ed0d50c3Schristos   { this->load_address_ = address; }
2070ed0d50c3Schristos 
2071ed0d50c3Schristos   const std::string&
get_section_name() const2072ed0d50c3Schristos   get_section_name() const
2073ed0d50c3Schristos   { return this->name_; }
2074ed0d50c3Schristos 
2075ed0d50c3Schristos  private:
2076ed0d50c3Schristos   static const char*
2077ed0d50c3Schristos   script_section_type_name(Script_section_type);
2078ed0d50c3Schristos 
2079ed0d50c3Schristos   typedef std::vector<Output_section_element*> Output_section_elements;
2080ed0d50c3Schristos 
2081ed0d50c3Schristos   // The output section name.
2082ed0d50c3Schristos   std::string name_;
2083ed0d50c3Schristos   // The address.  This may be NULL.
2084ed0d50c3Schristos   Expression* address_;
2085ed0d50c3Schristos   // The load address.  This may be NULL.
2086ed0d50c3Schristos   Expression* load_address_;
2087ed0d50c3Schristos   // The alignment.  This may be NULL.
2088ed0d50c3Schristos   Expression* align_;
2089ed0d50c3Schristos   // The input section alignment.  This may be NULL.
2090ed0d50c3Schristos   Expression* subalign_;
2091ed0d50c3Schristos   // The constraint, if any.
2092ed0d50c3Schristos   Section_constraint constraint_;
2093ed0d50c3Schristos   // The fill value.  This may be NULL.
2094ed0d50c3Schristos   Expression* fill_;
2095ed0d50c3Schristos   // The list of segments this section should go into.  This may be
2096ed0d50c3Schristos   // NULL.
2097ed0d50c3Schristos   String_list* phdrs_;
2098ed0d50c3Schristos   // The list of elements defining the section.
2099ed0d50c3Schristos   Output_section_elements elements_;
2100ed0d50c3Schristos   // The Output_section created for this definition.  This will be
2101ed0d50c3Schristos   // NULL if none was created.
2102ed0d50c3Schristos   Output_section* output_section_;
2103ed0d50c3Schristos   // The address after it has been evaluated.
2104ed0d50c3Schristos   uint64_t evaluated_address_;
2105ed0d50c3Schristos   // The load address after it has been evaluated.
2106ed0d50c3Schristos   uint64_t evaluated_load_address_;
2107ed0d50c3Schristos   // The alignment after it has been evaluated.
2108ed0d50c3Schristos   uint64_t evaluated_addralign_;
2109ed0d50c3Schristos   // The output section is relro.
2110ed0d50c3Schristos   bool is_relro_;
2111ed0d50c3Schristos   // The output section type if specified.
2112ed0d50c3Schristos   enum Script_section_type script_section_type_;
2113ed0d50c3Schristos };
2114ed0d50c3Schristos 
2115ed0d50c3Schristos // Constructor.
2116ed0d50c3Schristos 
Output_section_definition(const char * name,size_t namelen,const Parser_output_section_header * header)2117ed0d50c3Schristos Output_section_definition::Output_section_definition(
2118ed0d50c3Schristos     const char* name,
2119ed0d50c3Schristos     size_t namelen,
2120ed0d50c3Schristos     const Parser_output_section_header* header)
2121ed0d50c3Schristos   : name_(name, namelen),
2122ed0d50c3Schristos     address_(header->address),
2123ed0d50c3Schristos     load_address_(header->load_address),
2124ed0d50c3Schristos     align_(header->align),
2125ed0d50c3Schristos     subalign_(header->subalign),
2126ed0d50c3Schristos     constraint_(header->constraint),
2127ed0d50c3Schristos     fill_(NULL),
2128ed0d50c3Schristos     phdrs_(NULL),
2129ed0d50c3Schristos     elements_(),
2130ed0d50c3Schristos     output_section_(NULL),
2131ed0d50c3Schristos     evaluated_address_(0),
2132ed0d50c3Schristos     evaluated_load_address_(0),
2133ed0d50c3Schristos     evaluated_addralign_(0),
2134ed0d50c3Schristos     is_relro_(false),
2135ed0d50c3Schristos     script_section_type_(header->section_type)
2136ed0d50c3Schristos {
2137ed0d50c3Schristos }
2138ed0d50c3Schristos 
2139ed0d50c3Schristos // Finish an output section.
2140ed0d50c3Schristos 
2141ed0d50c3Schristos void
finish(const Parser_output_section_trailer * trailer)2142ed0d50c3Schristos Output_section_definition::finish(const Parser_output_section_trailer* trailer)
2143ed0d50c3Schristos {
2144ed0d50c3Schristos   this->fill_ = trailer->fill;
2145ed0d50c3Schristos   this->phdrs_ = trailer->phdrs;
2146ed0d50c3Schristos }
2147ed0d50c3Schristos 
2148ed0d50c3Schristos // Add a symbol to be defined.
2149ed0d50c3Schristos 
2150ed0d50c3Schristos void
add_symbol_assignment(const char * name,size_t length,Expression * value,bool provide,bool hidden)2151ed0d50c3Schristos Output_section_definition::add_symbol_assignment(const char* name,
2152ed0d50c3Schristos 						 size_t length,
2153ed0d50c3Schristos 						 Expression* value,
2154ed0d50c3Schristos 						 bool provide,
2155ed0d50c3Schristos 						 bool hidden)
2156ed0d50c3Schristos {
2157ed0d50c3Schristos   Output_section_element* p = new Output_section_element_assignment(name,
2158ed0d50c3Schristos 								    length,
2159ed0d50c3Schristos 								    value,
2160ed0d50c3Schristos 								    provide,
2161ed0d50c3Schristos 								    hidden);
2162ed0d50c3Schristos   this->elements_.push_back(p);
2163ed0d50c3Schristos }
2164ed0d50c3Schristos 
2165ed0d50c3Schristos // Add an assignment to the special dot symbol.
2166ed0d50c3Schristos 
2167ed0d50c3Schristos void
add_dot_assignment(Expression * value)2168ed0d50c3Schristos Output_section_definition::add_dot_assignment(Expression* value)
2169ed0d50c3Schristos {
2170ed0d50c3Schristos   Output_section_element* p = new Output_section_element_dot_assignment(value);
2171ed0d50c3Schristos   this->elements_.push_back(p);
2172ed0d50c3Schristos }
2173ed0d50c3Schristos 
2174ed0d50c3Schristos // Add an assertion.
2175ed0d50c3Schristos 
2176ed0d50c3Schristos void
add_assertion(Expression * check,const char * message,size_t messagelen)2177ed0d50c3Schristos Output_section_definition::add_assertion(Expression* check,
2178ed0d50c3Schristos 					 const char* message,
2179ed0d50c3Schristos 					 size_t messagelen)
2180ed0d50c3Schristos {
2181ed0d50c3Schristos   Output_section_element* p = new Output_section_element_assertion(check,
2182ed0d50c3Schristos 								   message,
2183ed0d50c3Schristos 								   messagelen);
2184ed0d50c3Schristos   this->elements_.push_back(p);
2185ed0d50c3Schristos }
2186ed0d50c3Schristos 
2187ed0d50c3Schristos // Add a data item to the current output section.
2188ed0d50c3Schristos 
2189ed0d50c3Schristos void
add_data(int size,bool is_signed,Expression * val)2190ed0d50c3Schristos Output_section_definition::add_data(int size, bool is_signed, Expression* val)
2191ed0d50c3Schristos {
2192ed0d50c3Schristos   Output_section_element* p = new Output_section_element_data(size, is_signed,
2193ed0d50c3Schristos 							      val);
2194ed0d50c3Schristos   this->elements_.push_back(p);
2195ed0d50c3Schristos }
2196ed0d50c3Schristos 
2197ed0d50c3Schristos // Add a setting for the fill value.
2198ed0d50c3Schristos 
2199ed0d50c3Schristos void
add_fill(Expression * val)2200ed0d50c3Schristos Output_section_definition::add_fill(Expression* val)
2201ed0d50c3Schristos {
2202ed0d50c3Schristos   Output_section_element* p = new Output_section_element_fill(val);
2203ed0d50c3Schristos   this->elements_.push_back(p);
2204ed0d50c3Schristos }
2205ed0d50c3Schristos 
2206ed0d50c3Schristos // Add an input section specification.
2207ed0d50c3Schristos 
2208ed0d50c3Schristos void
add_input_section(const Input_section_spec * spec,bool keep)2209ed0d50c3Schristos Output_section_definition::add_input_section(const Input_section_spec* spec,
2210ed0d50c3Schristos 					     bool keep)
2211ed0d50c3Schristos {
2212ed0d50c3Schristos   Output_section_element* p = new Output_section_element_input(spec, keep);
2213ed0d50c3Schristos   this->elements_.push_back(p);
2214ed0d50c3Schristos }
2215ed0d50c3Schristos 
2216ed0d50c3Schristos // Create any required output sections.  We need an output section if
2217ed0d50c3Schristos // there is a data statement here.
2218ed0d50c3Schristos 
2219ed0d50c3Schristos void
create_sections(Layout * layout)2220ed0d50c3Schristos Output_section_definition::create_sections(Layout* layout)
2221ed0d50c3Schristos {
2222ed0d50c3Schristos   if (this->output_section_ != NULL)
2223ed0d50c3Schristos     return;
2224ed0d50c3Schristos   for (Output_section_elements::const_iterator p = this->elements_.begin();
2225ed0d50c3Schristos        p != this->elements_.end();
2226ed0d50c3Schristos        ++p)
2227ed0d50c3Schristos     {
2228ed0d50c3Schristos       if ((*p)->needs_output_section())
2229ed0d50c3Schristos 	{
2230ed0d50c3Schristos 	  const char* name = this->name_.c_str();
2231ed0d50c3Schristos 	  this->output_section_ =
2232ed0d50c3Schristos 	    layout->make_output_section_for_script(name, this->section_type());
2233ed0d50c3Schristos 	  return;
2234ed0d50c3Schristos 	}
2235ed0d50c3Schristos     }
2236ed0d50c3Schristos }
2237ed0d50c3Schristos 
2238ed0d50c3Schristos // Add any symbols being defined to the symbol table.
2239ed0d50c3Schristos 
2240ed0d50c3Schristos void
add_symbols_to_table(Symbol_table * symtab)2241ed0d50c3Schristos Output_section_definition::add_symbols_to_table(Symbol_table* symtab)
2242ed0d50c3Schristos {
2243ed0d50c3Schristos   for (Output_section_elements::iterator p = this->elements_.begin();
2244ed0d50c3Schristos        p != this->elements_.end();
2245ed0d50c3Schristos        ++p)
2246ed0d50c3Schristos     (*p)->add_symbols_to_table(symtab);
2247ed0d50c3Schristos }
2248ed0d50c3Schristos 
2249ed0d50c3Schristos // Finalize symbols and check assertions.
2250ed0d50c3Schristos 
2251ed0d50c3Schristos void
finalize_symbols(Symbol_table * symtab,const Layout * layout,uint64_t * dot_value)2252ed0d50c3Schristos Output_section_definition::finalize_symbols(Symbol_table* symtab,
2253ed0d50c3Schristos 					    const Layout* layout,
2254ed0d50c3Schristos 					    uint64_t* dot_value)
2255ed0d50c3Schristos {
2256ed0d50c3Schristos   if (this->output_section_ != NULL)
2257ed0d50c3Schristos     *dot_value = this->output_section_->address();
2258ed0d50c3Schristos   else
2259ed0d50c3Schristos     {
2260ed0d50c3Schristos       uint64_t address = *dot_value;
2261ed0d50c3Schristos       if (this->address_ != NULL)
2262ed0d50c3Schristos 	{
2263ed0d50c3Schristos 	  address = this->address_->eval_with_dot(symtab, layout, true,
2264ed0d50c3Schristos 						  *dot_value, NULL,
2265ed0d50c3Schristos 						  NULL, NULL, false);
2266ed0d50c3Schristos 	}
2267ed0d50c3Schristos       if (this->align_ != NULL)
2268ed0d50c3Schristos 	{
2269ed0d50c3Schristos 	  uint64_t align = this->align_->eval_with_dot(symtab, layout, true,
2270ed0d50c3Schristos 						       *dot_value, NULL,
2271ed0d50c3Schristos 						       NULL, NULL, false);
2272ed0d50c3Schristos 	  address = align_address(address, align);
2273ed0d50c3Schristos 	}
2274ed0d50c3Schristos       *dot_value = address;
2275ed0d50c3Schristos     }
2276ed0d50c3Schristos 
2277ed0d50c3Schristos   Output_section* dot_section = this->output_section_;
2278ed0d50c3Schristos   for (Output_section_elements::iterator p = this->elements_.begin();
2279ed0d50c3Schristos        p != this->elements_.end();
2280ed0d50c3Schristos        ++p)
2281ed0d50c3Schristos     (*p)->finalize_symbols(symtab, layout, dot_value, &dot_section);
2282ed0d50c3Schristos }
2283ed0d50c3Schristos 
2284ed0d50c3Schristos // Return the output section name to use for an input section name.
2285ed0d50c3Schristos 
2286ed0d50c3Schristos const char*
output_section_name(const char * file_name,const char * section_name,Output_section *** slot,Script_sections::Section_type * psection_type,bool * keep,bool match_input_spec)2287ed0d50c3Schristos Output_section_definition::output_section_name(
2288ed0d50c3Schristos     const char* file_name,
2289ed0d50c3Schristos     const char* section_name,
2290ed0d50c3Schristos     Output_section*** slot,
2291ed0d50c3Schristos     Script_sections::Section_type* psection_type,
229206324dcfSchristos     bool* keep,
229306324dcfSchristos     bool match_input_spec)
2294ed0d50c3Schristos {
229506324dcfSchristos   // If the section is a linker-created output section, just look for a match
229606324dcfSchristos   // on the output section name.
229706324dcfSchristos   if (!match_input_spec && this->name_ != "/DISCARD/")
229806324dcfSchristos     {
229906324dcfSchristos       if (this->name_ != section_name)
230006324dcfSchristos 	return NULL;
230106324dcfSchristos       *slot = &this->output_section_;
230206324dcfSchristos       *psection_type = this->section_type();
230306324dcfSchristos       return this->name_.c_str();
230406324dcfSchristos     }
230506324dcfSchristos 
2306ed0d50c3Schristos   // Ask each element whether it matches NAME.
2307ed0d50c3Schristos   for (Output_section_elements::const_iterator p = this->elements_.begin();
2308ed0d50c3Schristos        p != this->elements_.end();
2309ed0d50c3Schristos        ++p)
2310ed0d50c3Schristos     {
2311ed0d50c3Schristos       if ((*p)->match_name(file_name, section_name, keep))
2312ed0d50c3Schristos 	{
2313ed0d50c3Schristos 	  // We found a match for NAME, which means that it should go
2314ed0d50c3Schristos 	  // into this output section.
2315ed0d50c3Schristos 	  *slot = &this->output_section_;
2316ed0d50c3Schristos 	  *psection_type = this->section_type();
2317ed0d50c3Schristos 	  return this->name_.c_str();
2318ed0d50c3Schristos 	}
2319ed0d50c3Schristos     }
2320ed0d50c3Schristos 
2321ed0d50c3Schristos   // We don't know about this section name.
2322ed0d50c3Schristos   return NULL;
2323ed0d50c3Schristos }
2324ed0d50c3Schristos 
2325ed0d50c3Schristos // Return true if memory from START to START + LENGTH is contained
2326ed0d50c3Schristos // within a memory region.
2327ed0d50c3Schristos 
2328ed0d50c3Schristos bool
block_in_region(Symbol_table * symtab,Layout * layout,uint64_t start,uint64_t length) const2329ed0d50c3Schristos Script_sections::block_in_region(Symbol_table* symtab, Layout* layout,
2330ed0d50c3Schristos 				 uint64_t start, uint64_t length) const
2331ed0d50c3Schristos {
2332ed0d50c3Schristos   if (this->memory_regions_ == NULL)
2333ed0d50c3Schristos     return false;
2334ed0d50c3Schristos 
2335ed0d50c3Schristos   for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2336ed0d50c3Schristos        mr != this->memory_regions_->end();
2337ed0d50c3Schristos        ++mr)
2338ed0d50c3Schristos     {
2339ed0d50c3Schristos       uint64_t s = (*mr)->start_address()->eval(symtab, layout, false);
2340ed0d50c3Schristos       uint64_t l = (*mr)->length()->eval(symtab, layout, false);
2341ed0d50c3Schristos 
2342ed0d50c3Schristos       if (s <= start
2343ed0d50c3Schristos 	  && (s + l) >= (start + length))
2344ed0d50c3Schristos 	return true;
2345ed0d50c3Schristos     }
2346ed0d50c3Schristos 
2347ed0d50c3Schristos   return false;
2348ed0d50c3Schristos }
2349ed0d50c3Schristos 
2350ed0d50c3Schristos // Find a memory region that should be used by a given output SECTION.
2351ed0d50c3Schristos // If provided set PREVIOUS_SECTION_RETURN to point to the last section
2352ed0d50c3Schristos // that used the return memory region.
2353ed0d50c3Schristos 
2354ed0d50c3Schristos Memory_region*
find_memory_region(Output_section_definition * section,bool find_vma_region,bool explicit_only,Output_section_definition ** previous_section_return)2355ed0d50c3Schristos Script_sections::find_memory_region(
2356ed0d50c3Schristos     Output_section_definition* section,
2357ed0d50c3Schristos     bool find_vma_region,
2358ed0d50c3Schristos     bool explicit_only,
2359ed0d50c3Schristos     Output_section_definition** previous_section_return)
2360ed0d50c3Schristos {
2361ed0d50c3Schristos   if (previous_section_return != NULL)
2362ed0d50c3Schristos     * previous_section_return = NULL;
2363ed0d50c3Schristos 
2364ed0d50c3Schristos   // Walk the memory regions specified in this script, if any.
2365ed0d50c3Schristos   if (this->memory_regions_ == NULL)
2366ed0d50c3Schristos     return NULL;
2367ed0d50c3Schristos 
2368ed0d50c3Schristos   // The /DISCARD/ section never gets assigned to any region.
2369ed0d50c3Schristos   if (section->get_section_name() == "/DISCARD/")
2370ed0d50c3Schristos     return NULL;
2371ed0d50c3Schristos 
2372ed0d50c3Schristos   Memory_region* first_match = NULL;
2373ed0d50c3Schristos 
2374ed0d50c3Schristos   // First check to see if a region has been assigned to this section.
2375ed0d50c3Schristos   for (Memory_regions::const_iterator mr = this->memory_regions_->begin();
2376ed0d50c3Schristos        mr != this->memory_regions_->end();
2377ed0d50c3Schristos        ++mr)
2378ed0d50c3Schristos     {
2379ed0d50c3Schristos       if (find_vma_region)
2380ed0d50c3Schristos 	{
2381ed0d50c3Schristos 	  for (Memory_region::Section_list::const_iterator s =
2382ed0d50c3Schristos 		 (*mr)->get_vma_section_list_start();
2383ed0d50c3Schristos 	       s != (*mr)->get_vma_section_list_end();
2384ed0d50c3Schristos 	       ++s)
2385ed0d50c3Schristos 	    if ((*s) == section)
2386ed0d50c3Schristos 	      {
2387ed0d50c3Schristos 		(*mr)->set_last_section(section);
2388ed0d50c3Schristos 		return *mr;
2389ed0d50c3Schristos 	      }
2390ed0d50c3Schristos 	}
2391ed0d50c3Schristos       else
2392ed0d50c3Schristos 	{
2393ed0d50c3Schristos 	  for (Memory_region::Section_list::const_iterator s =
2394ed0d50c3Schristos 		 (*mr)->get_lma_section_list_start();
2395ed0d50c3Schristos 	       s != (*mr)->get_lma_section_list_end();
2396ed0d50c3Schristos 	       ++s)
2397ed0d50c3Schristos 	    if ((*s) == section)
2398ed0d50c3Schristos 	      {
2399ed0d50c3Schristos 		(*mr)->set_last_section(section);
2400ed0d50c3Schristos 		return *mr;
2401ed0d50c3Schristos 	      }
2402ed0d50c3Schristos 	}
2403ed0d50c3Schristos 
2404ed0d50c3Schristos       if (!explicit_only)
2405ed0d50c3Schristos 	{
2406ed0d50c3Schristos 	  // Make a note of the first memory region whose attributes
2407ed0d50c3Schristos 	  // are compatible with the section.  If we do not find an
2408ed0d50c3Schristos 	  // explicit region assignment, then we will return this region.
2409ed0d50c3Schristos 	  Output_section* out_sec = section->get_output_section();
2410ed0d50c3Schristos 	  if (first_match == NULL
2411ed0d50c3Schristos 	      && out_sec != NULL
2412ed0d50c3Schristos 	      && (*mr)->attributes_compatible(out_sec->flags(),
2413ed0d50c3Schristos 					      out_sec->type()))
2414ed0d50c3Schristos 	    first_match = *mr;
2415ed0d50c3Schristos 	}
2416ed0d50c3Schristos     }
2417ed0d50c3Schristos 
2418ed0d50c3Schristos   // With LMA computations, if an explicit region has not been specified then
2419ed0d50c3Schristos   // we will want to set the difference between the VMA and the LMA of the
2420ed0d50c3Schristos   // section were searching for to be the same as the difference between the
2421ed0d50c3Schristos   // VMA and LMA of the last section to be added to first matched region.
2422ed0d50c3Schristos   // Hence, if it was asked for, we return a pointer to the last section
2423ed0d50c3Schristos   // known to be used by the first matched region.
2424ed0d50c3Schristos   if (first_match != NULL
2425ed0d50c3Schristos       && previous_section_return != NULL)
2426ed0d50c3Schristos     *previous_section_return = first_match->get_last_section();
2427ed0d50c3Schristos 
2428ed0d50c3Schristos   return first_match;
2429ed0d50c3Schristos }
2430ed0d50c3Schristos 
2431ed0d50c3Schristos // Set the section address.  Note that the OUTPUT_SECTION_ field will
2432ed0d50c3Schristos // be NULL if no input sections were mapped to this output section.
2433ed0d50c3Schristos // We still have to adjust dot and process symbol assignments.
2434ed0d50c3Schristos 
2435ed0d50c3Schristos void
set_section_addresses(Symbol_table * symtab,Layout * layout,uint64_t * dot_value,uint64_t * dot_alignment,uint64_t * load_address)2436ed0d50c3Schristos Output_section_definition::set_section_addresses(Symbol_table* symtab,
2437ed0d50c3Schristos 						 Layout* layout,
2438ed0d50c3Schristos 						 uint64_t* dot_value,
2439ed0d50c3Schristos 						 uint64_t* dot_alignment,
2440ed0d50c3Schristos                                                  uint64_t* load_address)
2441ed0d50c3Schristos {
2442ed0d50c3Schristos   Memory_region* vma_region = NULL;
2443ed0d50c3Schristos   Memory_region* lma_region = NULL;
2444ed0d50c3Schristos   Script_sections* script_sections =
2445ed0d50c3Schristos     layout->script_options()->script_sections();
2446ed0d50c3Schristos   uint64_t address;
2447ed0d50c3Schristos   uint64_t old_dot_value = *dot_value;
2448ed0d50c3Schristos   uint64_t old_load_address = *load_address;
2449ed0d50c3Schristos 
2450ed0d50c3Schristos   // If input section sorting is requested via --section-ordering-file or
2451ed0d50c3Schristos   // linker plugins, then do it here.  This is important because we want
2452ed0d50c3Schristos   // any sorting specified in the linker scripts, which will be done after
2453ed0d50c3Schristos   // this, to take precedence.  The final order of input sections is then
2454ed0d50c3Schristos   // guaranteed to be according to the linker script specification.
2455ed0d50c3Schristos   if (this->output_section_ != NULL
2456ed0d50c3Schristos       && this->output_section_->input_section_order_specified())
2457ed0d50c3Schristos     this->output_section_->sort_attached_input_sections();
2458ed0d50c3Schristos 
2459ed0d50c3Schristos   // Decide the start address for the section.  The algorithm is:
2460ed0d50c3Schristos   // 1) If an address has been specified in a linker script, use that.
2461ed0d50c3Schristos   // 2) Otherwise if a memory region has been specified for the section,
2462ed0d50c3Schristos   //    use the next free address in the region.
2463ed0d50c3Schristos   // 3) Otherwise if memory regions have been specified find the first
2464ed0d50c3Schristos   //    region whose attributes are compatible with this section and
2465ed0d50c3Schristos   //    install it into that region.
2466ed0d50c3Schristos   // 4) Otherwise use the current location counter.
2467ed0d50c3Schristos 
2468ed0d50c3Schristos   if (this->output_section_ != NULL
2469ed0d50c3Schristos       // Check for --section-start.
2470ed0d50c3Schristos       && parameters->options().section_start(this->output_section_->name(),
2471ed0d50c3Schristos 					     &address))
2472ed0d50c3Schristos     ;
2473ed0d50c3Schristos   else if (this->address_ == NULL)
2474ed0d50c3Schristos     {
2475ed0d50c3Schristos       vma_region = script_sections->find_memory_region(this, true, false, NULL);
2476ed0d50c3Schristos       if (vma_region != NULL)
2477ed0d50c3Schristos 	address = vma_region->get_current_address()->eval(symtab, layout,
2478ed0d50c3Schristos 							  false);
2479ed0d50c3Schristos       else
2480ed0d50c3Schristos 	address = *dot_value;
2481ed0d50c3Schristos     }
2482ed0d50c3Schristos   else
2483ed0d50c3Schristos     {
2484ed0d50c3Schristos       vma_region = script_sections->find_memory_region(this, true, true, NULL);
2485ed0d50c3Schristos       address = this->address_->eval_with_dot(symtab, layout, true,
2486ed0d50c3Schristos 					      *dot_value, NULL, NULL,
2487ed0d50c3Schristos 					      dot_alignment, false);
2488ed0d50c3Schristos       if (vma_region != NULL)
2489ed0d50c3Schristos 	vma_region->set_address(address, symtab, layout);
2490ed0d50c3Schristos     }
2491ed0d50c3Schristos 
2492ed0d50c3Schristos   uint64_t align;
2493ed0d50c3Schristos   if (this->align_ == NULL)
2494ed0d50c3Schristos     {
2495ed0d50c3Schristos       if (this->output_section_ == NULL)
2496ed0d50c3Schristos 	align = 0;
2497ed0d50c3Schristos       else
2498ed0d50c3Schristos 	align = this->output_section_->addralign();
2499ed0d50c3Schristos     }
2500ed0d50c3Schristos   else
2501ed0d50c3Schristos     {
2502ed0d50c3Schristos       Output_section* align_section;
2503ed0d50c3Schristos       align = this->align_->eval_with_dot(symtab, layout, true, *dot_value,
2504ed0d50c3Schristos 					  NULL, &align_section, NULL, false);
2505ed0d50c3Schristos       if (align_section != NULL)
2506ed0d50c3Schristos 	gold_warning(_("alignment of section %s is not absolute"),
2507ed0d50c3Schristos 		     this->name_.c_str());
2508ed0d50c3Schristos       if (this->output_section_ != NULL)
2509ed0d50c3Schristos 	this->output_section_->set_addralign(align);
2510ed0d50c3Schristos     }
2511ed0d50c3Schristos 
251206324dcfSchristos   uint64_t subalign;
251306324dcfSchristos   if (this->subalign_ == NULL)
251406324dcfSchristos     subalign = 0;
251506324dcfSchristos   else
251606324dcfSchristos     {
251706324dcfSchristos       Output_section* subalign_section;
251806324dcfSchristos       subalign = this->subalign_->eval_with_dot(symtab, layout, true,
251906324dcfSchristos 						*dot_value, NULL,
252006324dcfSchristos 						&subalign_section, NULL,
252106324dcfSchristos 						false);
252206324dcfSchristos       if (subalign_section != NULL)
252306324dcfSchristos 	gold_warning(_("subalign of section %s is not absolute"),
252406324dcfSchristos 		     this->name_.c_str());
252506324dcfSchristos 
252606324dcfSchristos       // Reserve a value of 0 to mean there is no SUBALIGN property.
252706324dcfSchristos       if (subalign == 0)
252806324dcfSchristos 	subalign = 1;
252906324dcfSchristos 
253006324dcfSchristos       // The external alignment of the output section must be at least
253106324dcfSchristos       // as large as that of the input sections.  If there is no
253206324dcfSchristos       // explicit ALIGN property, we set the output section alignment
253306324dcfSchristos       // to match the input section alignment.
253406324dcfSchristos       if (align < subalign || this->align_ == NULL)
253506324dcfSchristos 	{
253606324dcfSchristos 	  align = subalign;
253706324dcfSchristos 	  this->output_section_->set_addralign(align);
253806324dcfSchristos 	}
253906324dcfSchristos     }
254006324dcfSchristos 
2541ed0d50c3Schristos   address = align_address(address, align);
2542ed0d50c3Schristos 
2543ed0d50c3Schristos   uint64_t start_address = address;
2544ed0d50c3Schristos 
2545ed0d50c3Schristos   *dot_value = address;
2546ed0d50c3Schristos 
2547ed0d50c3Schristos   // Except for NOLOAD sections, the address of non-SHF_ALLOC sections is
2548ed0d50c3Schristos   // forced to zero, regardless of what the linker script wants.
2549ed0d50c3Schristos   if (this->output_section_ != NULL
2550ed0d50c3Schristos       && ((this->output_section_->flags() & elfcpp::SHF_ALLOC) != 0
2551ed0d50c3Schristos 	  || this->output_section_->is_noload()))
2552ed0d50c3Schristos     this->output_section_->set_address(address);
2553ed0d50c3Schristos 
2554ed0d50c3Schristos   this->evaluated_address_ = address;
2555ed0d50c3Schristos   this->evaluated_addralign_ = align;
2556ed0d50c3Schristos 
2557ed0d50c3Schristos   uint64_t laddr;
2558ed0d50c3Schristos 
2559ed0d50c3Schristos   if (this->load_address_ == NULL)
2560ed0d50c3Schristos     {
2561ed0d50c3Schristos       Output_section_definition* previous_section;
2562ed0d50c3Schristos 
2563ed0d50c3Schristos       // Determine if an LMA region has been set for this section.
2564ed0d50c3Schristos       lma_region = script_sections->find_memory_region(this, false, false,
2565ed0d50c3Schristos 						       &previous_section);
2566ed0d50c3Schristos 
2567ed0d50c3Schristos       if (lma_region != NULL)
2568ed0d50c3Schristos 	{
2569ed0d50c3Schristos 	  if (previous_section == NULL)
2570ed0d50c3Schristos 	    // The LMA address was explicitly set to the given region.
2571ed0d50c3Schristos 	    laddr = lma_region->get_current_address()->eval(symtab, layout,
2572ed0d50c3Schristos 							    false);
2573ed0d50c3Schristos 	  else
2574ed0d50c3Schristos 	    {
2575ed0d50c3Schristos 	      // We are not going to use the discovered lma_region, so
2576ed0d50c3Schristos 	      // make sure that we do not update it in the code below.
2577ed0d50c3Schristos 	      lma_region = NULL;
2578ed0d50c3Schristos 
2579ed0d50c3Schristos 	      if (this->address_ != NULL || previous_section == this)
2580ed0d50c3Schristos 		{
2581ed0d50c3Schristos 		  // Either an explicit VMA address has been set, or an
2582ed0d50c3Schristos 		  // explicit VMA region has been set, so set the LMA equal to
2583ed0d50c3Schristos 		  // the VMA.
2584ed0d50c3Schristos 		  laddr = address;
2585ed0d50c3Schristos 		}
2586ed0d50c3Schristos 	      else
2587ed0d50c3Schristos 		{
2588ed0d50c3Schristos 		  // The LMA address was not explicitly or implicitly set.
2589ed0d50c3Schristos 		  //
2590ed0d50c3Schristos 		  // We have been given the first memory region that is
2591ed0d50c3Schristos 		  // compatible with the current section and a pointer to the
2592ed0d50c3Schristos 		  // last section to use this region.  Set the LMA of this
2593ed0d50c3Schristos 		  // section so that the difference between its' VMA and LMA
2594ed0d50c3Schristos 		  // is the same as the difference between the VMA and LMA of
2595ed0d50c3Schristos 		  // the last section in the given region.
2596ed0d50c3Schristos 		  laddr = address + (previous_section->evaluated_load_address_
2597ed0d50c3Schristos 				     - previous_section->evaluated_address_);
2598ed0d50c3Schristos 		}
2599ed0d50c3Schristos 	    }
2600ed0d50c3Schristos 
2601ed0d50c3Schristos 	  if (this->output_section_ != NULL)
2602ed0d50c3Schristos 	    this->output_section_->set_load_address(laddr);
2603ed0d50c3Schristos 	}
2604ed0d50c3Schristos       else
2605ed0d50c3Schristos 	{
2606ed0d50c3Schristos 	  // Do not set the load address of the output section, if one exists.
2607ed0d50c3Schristos 	  // This allows future sections to determine what the load address
2608ed0d50c3Schristos 	  // should be.  If none is ever set, it will default to being the
2609ed0d50c3Schristos 	  // same as the vma address.
2610ed0d50c3Schristos 	  laddr = address;
2611ed0d50c3Schristos 	}
2612ed0d50c3Schristos     }
2613ed0d50c3Schristos   else
2614ed0d50c3Schristos     {
2615ed0d50c3Schristos       laddr = this->load_address_->eval_with_dot(symtab, layout, true,
2616ed0d50c3Schristos 						 *dot_value,
2617ed0d50c3Schristos 						 this->output_section_,
2618ed0d50c3Schristos 						 NULL, NULL, false);
2619ed0d50c3Schristos       if (this->output_section_ != NULL)
2620ed0d50c3Schristos         this->output_section_->set_load_address(laddr);
2621ed0d50c3Schristos     }
2622ed0d50c3Schristos 
2623ed0d50c3Schristos   this->evaluated_load_address_ = laddr;
2624ed0d50c3Schristos 
2625ed0d50c3Schristos   std::string fill;
2626ed0d50c3Schristos   if (this->fill_ != NULL)
2627ed0d50c3Schristos     {
2628ed0d50c3Schristos       // FIXME: The GNU linker supports fill values of arbitrary
2629ed0d50c3Schristos       // length.
2630ed0d50c3Schristos       Output_section* fill_section;
2631ed0d50c3Schristos       uint64_t fill_val = this->fill_->eval_with_dot(symtab, layout, true,
2632ed0d50c3Schristos 						     *dot_value,
2633ed0d50c3Schristos 						     NULL, &fill_section,
2634ed0d50c3Schristos 						     NULL, false);
2635ed0d50c3Schristos       if (fill_section != NULL)
2636ed0d50c3Schristos 	gold_warning(_("fill of section %s is not absolute"),
2637ed0d50c3Schristos 		     this->name_.c_str());
2638ed0d50c3Schristos       unsigned char fill_buff[4];
2639ed0d50c3Schristos       elfcpp::Swap_unaligned<32, true>::writeval(fill_buff, fill_val);
2640ed0d50c3Schristos       fill.assign(reinterpret_cast<char*>(fill_buff), 4);
2641ed0d50c3Schristos     }
2642ed0d50c3Schristos 
2643ed0d50c3Schristos   Input_section_list input_sections;
2644ed0d50c3Schristos   if (this->output_section_ != NULL)
2645ed0d50c3Schristos     {
2646ed0d50c3Schristos       // Get the list of input sections attached to this output
2647ed0d50c3Schristos       // section.  This will leave the output section with only
2648ed0d50c3Schristos       // Output_section_data entries.
2649ed0d50c3Schristos       address += this->output_section_->get_input_sections(address,
2650ed0d50c3Schristos 							   fill,
2651ed0d50c3Schristos 							   &input_sections);
2652ed0d50c3Schristos       *dot_value = address;
2653ed0d50c3Schristos     }
2654ed0d50c3Schristos 
2655ed0d50c3Schristos   Output_section* dot_section = this->output_section_;
2656ed0d50c3Schristos   for (Output_section_elements::iterator p = this->elements_.begin();
2657ed0d50c3Schristos        p != this->elements_.end();
2658ed0d50c3Schristos        ++p)
2659ed0d50c3Schristos     (*p)->set_section_addresses(symtab, layout, this->output_section_,
2660ed0d50c3Schristos 				subalign, dot_value, dot_alignment,
2661ed0d50c3Schristos 				&dot_section, &fill, &input_sections);
2662ed0d50c3Schristos 
2663ed0d50c3Schristos   gold_assert(input_sections.empty());
2664ed0d50c3Schristos 
2665ed0d50c3Schristos   if (vma_region != NULL)
2666ed0d50c3Schristos     {
2667ed0d50c3Schristos       // Update the VMA region being used by the section now that we know how
2668ed0d50c3Schristos       // big it is.  Use the current address in the region, rather than
2669ed0d50c3Schristos       // start_address because that might have been aligned upwards and we
2670ed0d50c3Schristos       // need to allow for the padding.
2671ed0d50c3Schristos       Expression* addr = vma_region->get_current_address();
2672ed0d50c3Schristos       uint64_t size = *dot_value - addr->eval(symtab, layout, false);
2673ed0d50c3Schristos 
2674ed0d50c3Schristos       vma_region->increment_offset(this->get_section_name(), size,
2675ed0d50c3Schristos 				   symtab, layout);
2676ed0d50c3Schristos     }
2677ed0d50c3Schristos 
2678ed0d50c3Schristos   // If the LMA region is different from the VMA region, then increment the
2679ed0d50c3Schristos   // offset there as well.  Note that we use the same "dot_value -
2680ed0d50c3Schristos   // start_address" formula that is used in the load_address assignment below.
2681ed0d50c3Schristos   if (lma_region != NULL && lma_region != vma_region)
2682ed0d50c3Schristos     lma_region->increment_offset(this->get_section_name(),
2683ed0d50c3Schristos 				 *dot_value - start_address,
2684ed0d50c3Schristos 				 symtab, layout);
2685ed0d50c3Schristos 
2686ed0d50c3Schristos   // Compute the load address for the following section.
2687ed0d50c3Schristos   if (this->output_section_ == NULL)
2688ed0d50c3Schristos     *load_address = *dot_value;
2689ed0d50c3Schristos   else if (this->load_address_ == NULL)
2690ed0d50c3Schristos     {
2691ed0d50c3Schristos       if (lma_region == NULL)
2692ed0d50c3Schristos 	*load_address = *dot_value;
2693ed0d50c3Schristos       else
2694ed0d50c3Schristos 	*load_address =
2695ed0d50c3Schristos 	  lma_region->get_current_address()->eval(symtab, layout, false);
2696ed0d50c3Schristos     }
2697ed0d50c3Schristos   else
2698ed0d50c3Schristos     *load_address = (this->output_section_->load_address()
2699ed0d50c3Schristos                      + (*dot_value - start_address));
2700ed0d50c3Schristos 
2701ed0d50c3Schristos   if (this->output_section_ != NULL)
2702ed0d50c3Schristos     {
2703ed0d50c3Schristos       if (this->is_relro_)
2704ed0d50c3Schristos 	this->output_section_->set_is_relro();
2705ed0d50c3Schristos       else
2706ed0d50c3Schristos 	this->output_section_->clear_is_relro();
2707ed0d50c3Schristos 
2708ed0d50c3Schristos       // If this is a NOLOAD section, keep dot and load address unchanged.
2709ed0d50c3Schristos       if (this->output_section_->is_noload())
2710ed0d50c3Schristos 	{
2711ed0d50c3Schristos 	  *dot_value = old_dot_value;
2712ed0d50c3Schristos 	  *load_address = old_load_address;
2713ed0d50c3Schristos 	}
2714ed0d50c3Schristos     }
2715ed0d50c3Schristos }
2716ed0d50c3Schristos 
2717ed0d50c3Schristos // Check a constraint (ONLY_IF_RO, etc.) on an output section.  If
2718ed0d50c3Schristos // this section is constrained, and the input sections do not match,
2719ed0d50c3Schristos // return the constraint, and set *POSD.
2720ed0d50c3Schristos 
2721ed0d50c3Schristos Section_constraint
check_constraint(Output_section_definition ** posd)2722ed0d50c3Schristos Output_section_definition::check_constraint(Output_section_definition** posd)
2723ed0d50c3Schristos {
2724ed0d50c3Schristos   switch (this->constraint_)
2725ed0d50c3Schristos     {
2726ed0d50c3Schristos     case CONSTRAINT_NONE:
2727ed0d50c3Schristos       return CONSTRAINT_NONE;
2728ed0d50c3Schristos 
2729ed0d50c3Schristos     case CONSTRAINT_ONLY_IF_RO:
2730ed0d50c3Schristos       if (this->output_section_ != NULL
2731ed0d50c3Schristos 	  && (this->output_section_->flags() & elfcpp::SHF_WRITE) != 0)
2732ed0d50c3Schristos 	{
2733ed0d50c3Schristos 	  *posd = this;
2734ed0d50c3Schristos 	  return CONSTRAINT_ONLY_IF_RO;
2735ed0d50c3Schristos 	}
2736ed0d50c3Schristos       return CONSTRAINT_NONE;
2737ed0d50c3Schristos 
2738ed0d50c3Schristos     case CONSTRAINT_ONLY_IF_RW:
2739ed0d50c3Schristos       if (this->output_section_ != NULL
2740ed0d50c3Schristos 	  && (this->output_section_->flags() & elfcpp::SHF_WRITE) == 0)
2741ed0d50c3Schristos 	{
2742ed0d50c3Schristos 	  *posd = this;
2743ed0d50c3Schristos 	  return CONSTRAINT_ONLY_IF_RW;
2744ed0d50c3Schristos 	}
2745ed0d50c3Schristos       return CONSTRAINT_NONE;
2746ed0d50c3Schristos 
2747ed0d50c3Schristos     case CONSTRAINT_SPECIAL:
2748ed0d50c3Schristos       if (this->output_section_ != NULL)
2749ed0d50c3Schristos 	gold_error(_("SPECIAL constraints are not implemented"));
2750ed0d50c3Schristos       return CONSTRAINT_NONE;
2751ed0d50c3Schristos 
2752ed0d50c3Schristos     default:
2753ed0d50c3Schristos       gold_unreachable();
2754ed0d50c3Schristos     }
2755ed0d50c3Schristos }
2756ed0d50c3Schristos 
2757ed0d50c3Schristos // See if this is the alternate output section for a constrained
2758ed0d50c3Schristos // output section.  If it is, transfer the Output_section and return
2759ed0d50c3Schristos // true.  Otherwise return false.
2760ed0d50c3Schristos 
2761ed0d50c3Schristos bool
alternate_constraint(Output_section_definition * posd,Section_constraint constraint)2762ed0d50c3Schristos Output_section_definition::alternate_constraint(
2763ed0d50c3Schristos     Output_section_definition* posd,
2764ed0d50c3Schristos     Section_constraint constraint)
2765ed0d50c3Schristos {
2766ed0d50c3Schristos   if (this->name_ != posd->name_)
2767ed0d50c3Schristos     return false;
2768ed0d50c3Schristos 
2769ed0d50c3Schristos   switch (constraint)
2770ed0d50c3Schristos     {
2771ed0d50c3Schristos     case CONSTRAINT_ONLY_IF_RO:
2772ed0d50c3Schristos       if (this->constraint_ != CONSTRAINT_ONLY_IF_RW)
2773ed0d50c3Schristos 	return false;
2774ed0d50c3Schristos       break;
2775ed0d50c3Schristos 
2776ed0d50c3Schristos     case CONSTRAINT_ONLY_IF_RW:
2777ed0d50c3Schristos       if (this->constraint_ != CONSTRAINT_ONLY_IF_RO)
2778ed0d50c3Schristos 	return false;
2779ed0d50c3Schristos       break;
2780ed0d50c3Schristos 
2781ed0d50c3Schristos     default:
2782ed0d50c3Schristos       gold_unreachable();
2783ed0d50c3Schristos     }
2784ed0d50c3Schristos 
2785ed0d50c3Schristos   // We have found the alternate constraint.  We just need to move
2786ed0d50c3Schristos   // over the Output_section.  When constraints are used properly,
2787ed0d50c3Schristos   // THIS should not have an output_section pointer, as all the input
2788ed0d50c3Schristos   // sections should have matched the other definition.
2789ed0d50c3Schristos 
2790ed0d50c3Schristos   if (this->output_section_ != NULL)
2791ed0d50c3Schristos     gold_error(_("mismatched definition for constrained sections"));
2792ed0d50c3Schristos 
2793ed0d50c3Schristos   this->output_section_ = posd->output_section_;
2794ed0d50c3Schristos   posd->output_section_ = NULL;
2795ed0d50c3Schristos 
2796ed0d50c3Schristos   if (this->is_relro_)
2797ed0d50c3Schristos     this->output_section_->set_is_relro();
2798ed0d50c3Schristos   else
2799ed0d50c3Schristos     this->output_section_->clear_is_relro();
2800ed0d50c3Schristos 
2801ed0d50c3Schristos   return true;
2802ed0d50c3Schristos }
2803ed0d50c3Schristos 
2804ed0d50c3Schristos // Get the list of segments to use for an allocated section when using
2805ed0d50c3Schristos // a PHDRS clause.
2806ed0d50c3Schristos 
2807ed0d50c3Schristos Output_section*
allocate_to_segment(String_list ** phdrs_list,bool * orphan)2808ed0d50c3Schristos Output_section_definition::allocate_to_segment(String_list** phdrs_list,
2809ed0d50c3Schristos 					       bool* orphan)
2810ed0d50c3Schristos {
2811ed0d50c3Schristos   // Update phdrs_list even if we don't have an output section. It
2812ed0d50c3Schristos   // might be used by the following sections.
2813ed0d50c3Schristos   if (this->phdrs_ != NULL)
2814ed0d50c3Schristos     *phdrs_list = this->phdrs_;
2815ed0d50c3Schristos 
2816ed0d50c3Schristos   if (this->output_section_ == NULL)
2817ed0d50c3Schristos     return NULL;
2818ed0d50c3Schristos   if ((this->output_section_->flags() & elfcpp::SHF_ALLOC) == 0)
2819ed0d50c3Schristos     return NULL;
2820ed0d50c3Schristos   *orphan = false;
2821ed0d50c3Schristos   return this->output_section_;
2822ed0d50c3Schristos }
2823ed0d50c3Schristos 
2824ed0d50c3Schristos // Look for an output section by name and return the address, the load
2825ed0d50c3Schristos // address, the alignment, and the size.  This is used when an
2826ed0d50c3Schristos // expression refers to an output section which was not actually
2827ed0d50c3Schristos // created.  This returns true if the section was found, false
2828ed0d50c3Schristos // otherwise.
2829ed0d50c3Schristos 
2830ed0d50c3Schristos bool
get_output_section_info(const char * name,uint64_t * address,uint64_t * load_address,uint64_t * addralign,uint64_t * size) const2831ed0d50c3Schristos Output_section_definition::get_output_section_info(const char* name,
2832ed0d50c3Schristos                                                    uint64_t* address,
2833ed0d50c3Schristos                                                    uint64_t* load_address,
2834ed0d50c3Schristos                                                    uint64_t* addralign,
2835ed0d50c3Schristos                                                    uint64_t* size) const
2836ed0d50c3Schristos {
2837ed0d50c3Schristos   if (this->name_ != name)
2838ed0d50c3Schristos     return false;
2839ed0d50c3Schristos 
2840ed0d50c3Schristos   if (this->output_section_ != NULL)
2841ed0d50c3Schristos     {
2842ed0d50c3Schristos       *address = this->output_section_->address();
2843ed0d50c3Schristos       if (this->output_section_->has_load_address())
2844ed0d50c3Schristos         *load_address = this->output_section_->load_address();
2845ed0d50c3Schristos       else
2846ed0d50c3Schristos         *load_address = *address;
2847ed0d50c3Schristos       *addralign = this->output_section_->addralign();
2848ed0d50c3Schristos       *size = this->output_section_->current_data_size();
2849ed0d50c3Schristos     }
2850ed0d50c3Schristos   else
2851ed0d50c3Schristos     {
2852ed0d50c3Schristos       *address = this->evaluated_address_;
2853ed0d50c3Schristos       *load_address = this->evaluated_load_address_;
2854ed0d50c3Schristos       *addralign = this->evaluated_addralign_;
2855ed0d50c3Schristos       *size = 0;
2856ed0d50c3Schristos     }
2857ed0d50c3Schristos 
2858ed0d50c3Schristos   return true;
2859ed0d50c3Schristos }
2860ed0d50c3Schristos 
2861ed0d50c3Schristos // Print for debugging.
2862ed0d50c3Schristos 
2863ed0d50c3Schristos void
print(FILE * f) const2864ed0d50c3Schristos Output_section_definition::print(FILE* f) const
2865ed0d50c3Schristos {
2866ed0d50c3Schristos   fprintf(f, "  %s ", this->name_.c_str());
2867ed0d50c3Schristos 
2868ed0d50c3Schristos   if (this->address_ != NULL)
2869ed0d50c3Schristos     {
2870ed0d50c3Schristos       this->address_->print(f);
2871ed0d50c3Schristos       fprintf(f, " ");
2872ed0d50c3Schristos     }
2873ed0d50c3Schristos 
2874ed0d50c3Schristos   if (this->script_section_type_ != SCRIPT_SECTION_TYPE_NONE)
2875ed0d50c3Schristos       fprintf(f, "(%s) ",
2876ed0d50c3Schristos 	      this->script_section_type_name(this->script_section_type_));
2877ed0d50c3Schristos 
2878ed0d50c3Schristos   fprintf(f, ": ");
2879ed0d50c3Schristos 
2880ed0d50c3Schristos   if (this->load_address_ != NULL)
2881ed0d50c3Schristos     {
2882ed0d50c3Schristos       fprintf(f, "AT(");
2883ed0d50c3Schristos       this->load_address_->print(f);
2884ed0d50c3Schristos       fprintf(f, ") ");
2885ed0d50c3Schristos     }
2886ed0d50c3Schristos 
2887ed0d50c3Schristos   if (this->align_ != NULL)
2888ed0d50c3Schristos     {
2889ed0d50c3Schristos       fprintf(f, "ALIGN(");
2890ed0d50c3Schristos       this->align_->print(f);
2891ed0d50c3Schristos       fprintf(f, ") ");
2892ed0d50c3Schristos     }
2893ed0d50c3Schristos 
2894ed0d50c3Schristos   if (this->subalign_ != NULL)
2895ed0d50c3Schristos     {
2896ed0d50c3Schristos       fprintf(f, "SUBALIGN(");
2897ed0d50c3Schristos       this->subalign_->print(f);
2898ed0d50c3Schristos       fprintf(f, ") ");
2899ed0d50c3Schristos     }
2900ed0d50c3Schristos 
2901ed0d50c3Schristos   fprintf(f, "{\n");
2902ed0d50c3Schristos 
2903ed0d50c3Schristos   for (Output_section_elements::const_iterator p = this->elements_.begin();
2904ed0d50c3Schristos        p != this->elements_.end();
2905ed0d50c3Schristos        ++p)
2906ed0d50c3Schristos     (*p)->print(f);
2907ed0d50c3Schristos 
2908ed0d50c3Schristos   fprintf(f, "  }");
2909ed0d50c3Schristos 
2910ed0d50c3Schristos   if (this->fill_ != NULL)
2911ed0d50c3Schristos     {
2912ed0d50c3Schristos       fprintf(f, " = ");
2913ed0d50c3Schristos       this->fill_->print(f);
2914ed0d50c3Schristos     }
2915ed0d50c3Schristos 
2916ed0d50c3Schristos   if (this->phdrs_ != NULL)
2917ed0d50c3Schristos     {
2918ed0d50c3Schristos       for (String_list::const_iterator p = this->phdrs_->begin();
2919ed0d50c3Schristos 	   p != this->phdrs_->end();
2920ed0d50c3Schristos 	   ++p)
2921ed0d50c3Schristos 	fprintf(f, " :%s", p->c_str());
2922ed0d50c3Schristos     }
2923ed0d50c3Schristos 
2924ed0d50c3Schristos   fprintf(f, "\n");
2925ed0d50c3Schristos }
2926ed0d50c3Schristos 
2927ed0d50c3Schristos Script_sections::Section_type
section_type() const2928ed0d50c3Schristos Output_section_definition::section_type() const
2929ed0d50c3Schristos {
2930ed0d50c3Schristos   switch (this->script_section_type_)
2931ed0d50c3Schristos     {
2932ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_NONE:
2933ed0d50c3Schristos       return Script_sections::ST_NONE;
2934ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_NOLOAD:
2935ed0d50c3Schristos       return Script_sections::ST_NOLOAD;
2936ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_COPY:
2937ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_DSECT:
2938ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_INFO:
2939ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_OVERLAY:
2940ed0d50c3Schristos       // There are not really support so we treat them as ST_NONE.  The
2941ed0d50c3Schristos       // parse should have issued errors for them already.
2942ed0d50c3Schristos       return Script_sections::ST_NONE;
2943ed0d50c3Schristos     default:
2944ed0d50c3Schristos       gold_unreachable();
2945ed0d50c3Schristos     }
2946ed0d50c3Schristos }
2947ed0d50c3Schristos 
2948ed0d50c3Schristos // Return the name of a script section type.
2949ed0d50c3Schristos 
2950ed0d50c3Schristos const char*
script_section_type_name(Script_section_type script_section_type)2951ed0d50c3Schristos Output_section_definition::script_section_type_name(
2952ed0d50c3Schristos     Script_section_type script_section_type)
2953ed0d50c3Schristos {
2954ed0d50c3Schristos   switch (script_section_type)
2955ed0d50c3Schristos     {
2956ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_NONE:
2957ed0d50c3Schristos       return "NONE";
2958ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_NOLOAD:
2959ed0d50c3Schristos       return "NOLOAD";
2960ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_DSECT:
2961ed0d50c3Schristos       return "DSECT";
2962ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_COPY:
2963ed0d50c3Schristos       return "COPY";
2964ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_INFO:
2965ed0d50c3Schristos       return "INFO";
2966ed0d50c3Schristos     case SCRIPT_SECTION_TYPE_OVERLAY:
2967ed0d50c3Schristos       return "OVERLAY";
2968ed0d50c3Schristos     default:
2969ed0d50c3Schristos       gold_unreachable();
2970ed0d50c3Schristos     }
2971ed0d50c3Schristos }
2972ed0d50c3Schristos 
2973ed0d50c3Schristos void
set_memory_region(Memory_region * mr,bool set_vma)2974ed0d50c3Schristos Output_section_definition::set_memory_region(Memory_region* mr, bool set_vma)
2975ed0d50c3Schristos {
2976ed0d50c3Schristos   gold_assert(mr != NULL);
2977ed0d50c3Schristos   // Add the current section to the specified region's list.
2978ed0d50c3Schristos   mr->add_section(this, set_vma);
2979ed0d50c3Schristos }
2980ed0d50c3Schristos 
2981ed0d50c3Schristos // An output section created to hold orphaned input sections.  These
2982ed0d50c3Schristos // do not actually appear in linker scripts.  However, for convenience
2983ed0d50c3Schristos // when setting the output section addresses, we put a marker to these
2984ed0d50c3Schristos // sections in the appropriate place in the list of SECTIONS elements.
2985ed0d50c3Schristos 
2986ed0d50c3Schristos class Orphan_output_section : public Sections_element
2987ed0d50c3Schristos {
2988ed0d50c3Schristos  public:
Orphan_output_section(Output_section * os)2989ed0d50c3Schristos   Orphan_output_section(Output_section* os)
2990ed0d50c3Schristos     : os_(os)
2991ed0d50c3Schristos   { }
2992ed0d50c3Schristos 
2993ed0d50c3Schristos   // Return whether the orphan output section is relro.  We can just
2994ed0d50c3Schristos   // check the output section because we always set the flag, if
2995ed0d50c3Schristos   // needed, just after we create the Orphan_output_section.
2996ed0d50c3Schristos   bool
is_relro() const2997ed0d50c3Schristos   is_relro() const
2998ed0d50c3Schristos   { return this->os_->is_relro(); }
2999ed0d50c3Schristos 
3000ed0d50c3Schristos   // Initialize OSP with an output section.  This should have been
3001ed0d50c3Schristos   // done already.
3002ed0d50c3Schristos   void
orphan_section_init(Orphan_section_placement *,Script_sections::Elements_iterator)3003ed0d50c3Schristos   orphan_section_init(Orphan_section_placement*,
3004ed0d50c3Schristos 		      Script_sections::Elements_iterator)
3005ed0d50c3Schristos   { gold_unreachable(); }
3006ed0d50c3Schristos 
3007ed0d50c3Schristos   // Set section addresses.
3008ed0d50c3Schristos   void
3009ed0d50c3Schristos   set_section_addresses(Symbol_table*, Layout*, uint64_t*, uint64_t*,
3010ed0d50c3Schristos 			uint64_t*);
3011ed0d50c3Schristos 
3012ed0d50c3Schristos   // Get the list of segments to use for an allocated section when
3013ed0d50c3Schristos   // using a PHDRS clause.
3014ed0d50c3Schristos   Output_section*
3015ed0d50c3Schristos   allocate_to_segment(String_list**, bool*);
3016ed0d50c3Schristos 
3017ed0d50c3Schristos   // Return the associated Output_section.
3018ed0d50c3Schristos   Output_section*
get_output_section() const3019ed0d50c3Schristos   get_output_section() const
3020ed0d50c3Schristos   { return this->os_; }
3021ed0d50c3Schristos 
3022ed0d50c3Schristos   // Print for debugging.
3023ed0d50c3Schristos   void
print(FILE * f) const3024ed0d50c3Schristos   print(FILE* f) const
3025ed0d50c3Schristos   {
3026ed0d50c3Schristos     fprintf(f, "  marker for orphaned output section %s\n",
3027ed0d50c3Schristos 	    this->os_->name());
3028ed0d50c3Schristos   }
3029ed0d50c3Schristos 
3030ed0d50c3Schristos  private:
3031ed0d50c3Schristos   Output_section* os_;
3032ed0d50c3Schristos };
3033ed0d50c3Schristos 
3034ed0d50c3Schristos // Set section addresses.
3035ed0d50c3Schristos 
3036ed0d50c3Schristos void
set_section_addresses(Symbol_table *,Layout *,uint64_t * dot_value,uint64_t *,uint64_t * load_address)3037ed0d50c3Schristos Orphan_output_section::set_section_addresses(Symbol_table*, Layout*,
3038ed0d50c3Schristos 					     uint64_t* dot_value,
3039ed0d50c3Schristos 					     uint64_t*,
3040ed0d50c3Schristos                                              uint64_t* load_address)
3041ed0d50c3Schristos {
3042ed0d50c3Schristos   typedef std::list<Output_section::Input_section> Input_section_list;
3043ed0d50c3Schristos 
3044ed0d50c3Schristos   bool have_load_address = *load_address != *dot_value;
3045ed0d50c3Schristos 
3046ed0d50c3Schristos   uint64_t address = *dot_value;
3047ed0d50c3Schristos   address = align_address(address, this->os_->addralign());
3048ed0d50c3Schristos 
3049ed0d50c3Schristos   // If input section sorting is requested via --section-ordering-file or
3050ed0d50c3Schristos   // linker plugins, then do it here.  This is important because we want
3051ed0d50c3Schristos   // any sorting specified in the linker scripts, which will be done after
3052ed0d50c3Schristos   // this, to take precedence.  The final order of input sections is then
3053ed0d50c3Schristos   // guaranteed to be according to the linker script specification.
3054ed0d50c3Schristos   if (this->os_ != NULL
3055ed0d50c3Schristos       && this->os_->input_section_order_specified())
3056ed0d50c3Schristos     this->os_->sort_attached_input_sections();
3057ed0d50c3Schristos 
3058ed0d50c3Schristos   // For a relocatable link, all orphan sections are put at
3059ed0d50c3Schristos   // address 0.  In general we expect all sections to be at
3060ed0d50c3Schristos   // address 0 for a relocatable link, but we permit the linker
3061ed0d50c3Schristos   // script to override that for specific output sections.
3062ed0d50c3Schristos   if (parameters->options().relocatable())
3063ed0d50c3Schristos     {
3064ed0d50c3Schristos       address = 0;
3065ed0d50c3Schristos       *load_address = 0;
3066ed0d50c3Schristos       have_load_address = false;
3067ed0d50c3Schristos     }
3068ed0d50c3Schristos 
3069ed0d50c3Schristos   if ((this->os_->flags() & elfcpp::SHF_ALLOC) != 0)
3070ed0d50c3Schristos     {
3071ed0d50c3Schristos       this->os_->set_address(address);
3072ed0d50c3Schristos       if (have_load_address)
3073ed0d50c3Schristos         this->os_->set_load_address(align_address(*load_address,
3074ed0d50c3Schristos                                                   this->os_->addralign()));
3075ed0d50c3Schristos     }
3076ed0d50c3Schristos 
3077ed0d50c3Schristos   Input_section_list input_sections;
3078ed0d50c3Schristos   address += this->os_->get_input_sections(address, "", &input_sections);
3079ed0d50c3Schristos 
3080ed0d50c3Schristos   for (Input_section_list::iterator p = input_sections.begin();
3081ed0d50c3Schristos        p != input_sections.end();
3082ed0d50c3Schristos        ++p)
3083ed0d50c3Schristos     {
3084ed0d50c3Schristos       uint64_t addralign = p->addralign();
3085ed0d50c3Schristos       if (!p->is_input_section())
3086ed0d50c3Schristos 	p->output_section_data()->finalize_data_size();
3087ed0d50c3Schristos       uint64_t size = p->data_size();
3088ed0d50c3Schristos       address = align_address(address, addralign);
3089ed0d50c3Schristos       this->os_->add_script_input_section(*p);
3090ed0d50c3Schristos       address += size;
3091ed0d50c3Schristos     }
3092ed0d50c3Schristos 
3093ed0d50c3Schristos   if (parameters->options().relocatable())
3094ed0d50c3Schristos     {
3095ed0d50c3Schristos       // For a relocatable link, reset DOT_VALUE to 0.
3096ed0d50c3Schristos       *dot_value = 0;
3097ed0d50c3Schristos       *load_address = 0;
3098ed0d50c3Schristos     }
3099ed0d50c3Schristos   else if (this->os_ == NULL
3100ed0d50c3Schristos 	   || (this->os_->flags() & elfcpp::SHF_TLS) == 0
3101ed0d50c3Schristos 	   || this->os_->type() != elfcpp::SHT_NOBITS)
3102ed0d50c3Schristos     {
3103ed0d50c3Schristos       // An SHF_TLS/SHT_NOBITS section does not take up any address space.
3104ed0d50c3Schristos       if (!have_load_address)
3105ed0d50c3Schristos 	*load_address = address;
3106ed0d50c3Schristos       else
3107ed0d50c3Schristos 	*load_address += address - *dot_value;
3108ed0d50c3Schristos 
3109ed0d50c3Schristos       *dot_value = address;
3110ed0d50c3Schristos     }
3111ed0d50c3Schristos }
3112ed0d50c3Schristos 
3113ed0d50c3Schristos // Get the list of segments to use for an allocated section when using
3114ed0d50c3Schristos // a PHDRS clause.  If this is an allocated section, return the
3115ed0d50c3Schristos // Output_section.  We don't change the list of segments.
3116ed0d50c3Schristos 
3117ed0d50c3Schristos Output_section*
allocate_to_segment(String_list **,bool * orphan)3118ed0d50c3Schristos Orphan_output_section::allocate_to_segment(String_list**, bool* orphan)
3119ed0d50c3Schristos {
3120ed0d50c3Schristos   if ((this->os_->flags() & elfcpp::SHF_ALLOC) == 0)
3121ed0d50c3Schristos     return NULL;
3122ed0d50c3Schristos   *orphan = true;
3123ed0d50c3Schristos   return this->os_;
3124ed0d50c3Schristos }
3125ed0d50c3Schristos 
3126ed0d50c3Schristos // Class Phdrs_element.  A program header from a PHDRS clause.
3127ed0d50c3Schristos 
3128ed0d50c3Schristos class Phdrs_element
3129ed0d50c3Schristos {
3130ed0d50c3Schristos  public:
Phdrs_element(const char * name,size_t namelen,unsigned int type,bool includes_filehdr,bool includes_phdrs,bool is_flags_valid,unsigned int flags,Expression * load_address)3131ed0d50c3Schristos   Phdrs_element(const char* name, size_t namelen, unsigned int type,
3132ed0d50c3Schristos 		bool includes_filehdr, bool includes_phdrs,
3133ed0d50c3Schristos 		bool is_flags_valid, unsigned int flags,
3134ed0d50c3Schristos 		Expression* load_address)
3135ed0d50c3Schristos     : name_(name, namelen), type_(type), includes_filehdr_(includes_filehdr),
3136ed0d50c3Schristos       includes_phdrs_(includes_phdrs), is_flags_valid_(is_flags_valid),
3137ed0d50c3Schristos       flags_(flags), load_address_(load_address), load_address_value_(0),
3138ed0d50c3Schristos       segment_(NULL)
3139ed0d50c3Schristos   { }
3140ed0d50c3Schristos 
3141ed0d50c3Schristos   // Return the name of this segment.
3142ed0d50c3Schristos   const std::string&
name() const3143ed0d50c3Schristos   name() const
3144ed0d50c3Schristos   { return this->name_; }
3145ed0d50c3Schristos 
3146ed0d50c3Schristos   // Return the type of the segment.
3147ed0d50c3Schristos   unsigned int
type() const3148ed0d50c3Schristos   type() const
3149ed0d50c3Schristos   { return this->type_; }
3150ed0d50c3Schristos 
3151ed0d50c3Schristos   // Whether to include the file header.
3152ed0d50c3Schristos   bool
includes_filehdr() const3153ed0d50c3Schristos   includes_filehdr() const
3154ed0d50c3Schristos   { return this->includes_filehdr_; }
3155ed0d50c3Schristos 
3156ed0d50c3Schristos   // Whether to include the program headers.
3157ed0d50c3Schristos   bool
includes_phdrs() const3158ed0d50c3Schristos   includes_phdrs() const
3159ed0d50c3Schristos   { return this->includes_phdrs_; }
3160ed0d50c3Schristos 
3161ed0d50c3Schristos   // Return whether there is a load address.
3162ed0d50c3Schristos   bool
has_load_address() const3163ed0d50c3Schristos   has_load_address() const
3164ed0d50c3Schristos   { return this->load_address_ != NULL; }
3165ed0d50c3Schristos 
3166ed0d50c3Schristos   // Evaluate the load address expression if there is one.
3167ed0d50c3Schristos   void
eval_load_address(Symbol_table * symtab,Layout * layout)3168ed0d50c3Schristos   eval_load_address(Symbol_table* symtab, Layout* layout)
3169ed0d50c3Schristos   {
3170ed0d50c3Schristos     if (this->load_address_ != NULL)
3171ed0d50c3Schristos       this->load_address_value_ = this->load_address_->eval(symtab, layout,
3172ed0d50c3Schristos 							    true);
3173ed0d50c3Schristos   }
3174ed0d50c3Schristos 
3175ed0d50c3Schristos   // Return the load address.
3176ed0d50c3Schristos   uint64_t
load_address() const3177ed0d50c3Schristos   load_address() const
3178ed0d50c3Schristos   {
3179ed0d50c3Schristos     gold_assert(this->load_address_ != NULL);
3180ed0d50c3Schristos     return this->load_address_value_;
3181ed0d50c3Schristos   }
3182ed0d50c3Schristos 
3183ed0d50c3Schristos   // Create the segment.
3184ed0d50c3Schristos   Output_segment*
create_segment(Layout * layout)3185ed0d50c3Schristos   create_segment(Layout* layout)
3186ed0d50c3Schristos   {
3187ed0d50c3Schristos     this->segment_ = layout->make_output_segment(this->type_, this->flags_);
3188ed0d50c3Schristos     return this->segment_;
3189ed0d50c3Schristos   }
3190ed0d50c3Schristos 
3191ed0d50c3Schristos   // Return the segment.
3192ed0d50c3Schristos   Output_segment*
segment()3193ed0d50c3Schristos   segment()
3194ed0d50c3Schristos   { return this->segment_; }
3195ed0d50c3Schristos 
3196ed0d50c3Schristos   // Release the segment.
3197ed0d50c3Schristos   void
release_segment()3198ed0d50c3Schristos   release_segment()
3199ed0d50c3Schristos   { this->segment_ = NULL; }
3200ed0d50c3Schristos 
3201ed0d50c3Schristos   // Set the segment flags if appropriate.
3202ed0d50c3Schristos   void
set_flags_if_valid()3203ed0d50c3Schristos   set_flags_if_valid()
3204ed0d50c3Schristos   {
3205ed0d50c3Schristos     if (this->is_flags_valid_)
3206ed0d50c3Schristos       this->segment_->set_flags(this->flags_);
3207ed0d50c3Schristos   }
3208ed0d50c3Schristos 
3209ed0d50c3Schristos   // Print for debugging.
3210ed0d50c3Schristos   void
3211ed0d50c3Schristos   print(FILE*) const;
3212ed0d50c3Schristos 
3213ed0d50c3Schristos  private:
3214ed0d50c3Schristos   // The name used in the script.
3215ed0d50c3Schristos   std::string name_;
3216ed0d50c3Schristos   // The type of the segment (PT_LOAD, etc.).
3217ed0d50c3Schristos   unsigned int type_;
3218ed0d50c3Schristos   // Whether this segment includes the file header.
3219ed0d50c3Schristos   bool includes_filehdr_;
3220ed0d50c3Schristos   // Whether this segment includes the section headers.
3221ed0d50c3Schristos   bool includes_phdrs_;
3222ed0d50c3Schristos   // Whether the flags were explicitly specified.
3223ed0d50c3Schristos   bool is_flags_valid_;
3224ed0d50c3Schristos   // The flags for this segment (PF_R, etc.) if specified.
3225ed0d50c3Schristos   unsigned int flags_;
3226ed0d50c3Schristos   // The expression for the load address for this segment.  This may
3227ed0d50c3Schristos   // be NULL.
3228ed0d50c3Schristos   Expression* load_address_;
3229ed0d50c3Schristos   // The actual load address from evaluating the expression.
3230ed0d50c3Schristos   uint64_t load_address_value_;
3231ed0d50c3Schristos   // The segment itself.
3232ed0d50c3Schristos   Output_segment* segment_;
3233ed0d50c3Schristos };
3234ed0d50c3Schristos 
3235ed0d50c3Schristos // Print for debugging.
3236ed0d50c3Schristos 
3237ed0d50c3Schristos void
print(FILE * f) const3238ed0d50c3Schristos Phdrs_element::print(FILE* f) const
3239ed0d50c3Schristos {
3240ed0d50c3Schristos   fprintf(f, "  %s 0x%x", this->name_.c_str(), this->type_);
3241ed0d50c3Schristos   if (this->includes_filehdr_)
3242ed0d50c3Schristos     fprintf(f, " FILEHDR");
3243ed0d50c3Schristos   if (this->includes_phdrs_)
3244ed0d50c3Schristos     fprintf(f, " PHDRS");
3245ed0d50c3Schristos   if (this->is_flags_valid_)
3246ed0d50c3Schristos     fprintf(f, " FLAGS(%u)", this->flags_);
3247ed0d50c3Schristos   if (this->load_address_ != NULL)
3248ed0d50c3Schristos     {
3249ed0d50c3Schristos       fprintf(f, " AT(");
3250ed0d50c3Schristos       this->load_address_->print(f);
3251ed0d50c3Schristos       fprintf(f, ")");
3252ed0d50c3Schristos     }
3253ed0d50c3Schristos   fprintf(f, ";\n");
3254ed0d50c3Schristos }
3255ed0d50c3Schristos 
3256ed0d50c3Schristos // Add a memory region.
3257ed0d50c3Schristos 
3258ed0d50c3Schristos void
add_memory_region(const char * name,size_t namelen,unsigned int attributes,Expression * start,Expression * length)3259ed0d50c3Schristos Script_sections::add_memory_region(const char* name, size_t namelen,
3260ed0d50c3Schristos 				   unsigned int attributes,
3261ed0d50c3Schristos 				   Expression* start, Expression* length)
3262ed0d50c3Schristos {
3263ed0d50c3Schristos   if (this->memory_regions_ == NULL)
3264ed0d50c3Schristos     this->memory_regions_ = new Memory_regions();
3265ed0d50c3Schristos   else if (this->find_memory_region(name, namelen))
3266ed0d50c3Schristos     {
3267ed0d50c3Schristos       gold_error(_("region '%.*s' already defined"), static_cast<int>(namelen),
3268ed0d50c3Schristos                   name);
3269ed0d50c3Schristos       // FIXME: Add a GOLD extension to allow multiple regions with the same
3270ed0d50c3Schristos       // name.  This would amount to a single region covering disjoint blocks
3271ed0d50c3Schristos       // of memory, which is useful for embedded devices.
3272ed0d50c3Schristos     }
3273ed0d50c3Schristos 
3274ed0d50c3Schristos   // FIXME: Check the length and start values.  Currently we allow
3275ed0d50c3Schristos   // non-constant expressions for these values, whereas LD does not.
3276ed0d50c3Schristos 
3277ed0d50c3Schristos   // FIXME: Add a GOLD extension to allow NEGATIVE LENGTHS.  This would
3278ed0d50c3Schristos   // describe a region that packs from the end address going down, rather
3279ed0d50c3Schristos   // than the start address going up.  This would be useful for embedded
3280ed0d50c3Schristos   // devices.
3281ed0d50c3Schristos 
3282ed0d50c3Schristos   this->memory_regions_->push_back(new Memory_region(name, namelen, attributes,
3283ed0d50c3Schristos 						     start, length));
3284ed0d50c3Schristos }
3285ed0d50c3Schristos 
3286ed0d50c3Schristos // Find a memory region.
3287ed0d50c3Schristos 
3288ed0d50c3Schristos Memory_region*
find_memory_region(const char * name,size_t namelen)3289ed0d50c3Schristos Script_sections::find_memory_region(const char* name, size_t namelen)
3290ed0d50c3Schristos {
3291ed0d50c3Schristos   if (this->memory_regions_ == NULL)
3292ed0d50c3Schristos     return NULL;
3293ed0d50c3Schristos 
3294ed0d50c3Schristos   for (Memory_regions::const_iterator m = this->memory_regions_->begin();
3295ed0d50c3Schristos        m != this->memory_regions_->end();
3296ed0d50c3Schristos        ++m)
3297ed0d50c3Schristos     if ((*m)->name_match(name, namelen))
3298ed0d50c3Schristos       return *m;
3299ed0d50c3Schristos 
3300ed0d50c3Schristos   return NULL;
3301ed0d50c3Schristos }
3302ed0d50c3Schristos 
3303ed0d50c3Schristos // Find a memory region's origin.
3304ed0d50c3Schristos 
3305ed0d50c3Schristos Expression*
find_memory_region_origin(const char * name,size_t namelen)3306ed0d50c3Schristos Script_sections::find_memory_region_origin(const char* name, size_t namelen)
3307ed0d50c3Schristos {
3308ed0d50c3Schristos   Memory_region* mr = find_memory_region(name, namelen);
3309ed0d50c3Schristos   if (mr == NULL)
3310ed0d50c3Schristos     return NULL;
3311ed0d50c3Schristos 
3312ed0d50c3Schristos   return mr->start_address();
3313ed0d50c3Schristos }
3314ed0d50c3Schristos 
3315ed0d50c3Schristos // Find a memory region's length.
3316ed0d50c3Schristos 
3317ed0d50c3Schristos Expression*
find_memory_region_length(const char * name,size_t namelen)3318ed0d50c3Schristos Script_sections::find_memory_region_length(const char* name, size_t namelen)
3319ed0d50c3Schristos {
3320ed0d50c3Schristos   Memory_region* mr = find_memory_region(name, namelen);
3321ed0d50c3Schristos   if (mr == NULL)
3322ed0d50c3Schristos     return NULL;
3323ed0d50c3Schristos 
3324ed0d50c3Schristos   return mr->length();
3325ed0d50c3Schristos }
3326ed0d50c3Schristos 
3327ed0d50c3Schristos // Set the memory region to use for the current section.
3328ed0d50c3Schristos 
3329ed0d50c3Schristos void
set_memory_region(Memory_region * mr,bool set_vma)3330ed0d50c3Schristos Script_sections::set_memory_region(Memory_region* mr, bool set_vma)
3331ed0d50c3Schristos {
3332ed0d50c3Schristos   gold_assert(!this->sections_elements_->empty());
3333ed0d50c3Schristos   this->sections_elements_->back()->set_memory_region(mr, set_vma);
3334ed0d50c3Schristos }
3335ed0d50c3Schristos 
3336ed0d50c3Schristos // Class Script_sections.
3337ed0d50c3Schristos 
Script_sections()3338ed0d50c3Schristos Script_sections::Script_sections()
3339ed0d50c3Schristos   : saw_sections_clause_(false),
3340ed0d50c3Schristos     in_sections_clause_(false),
3341ed0d50c3Schristos     sections_elements_(NULL),
3342ed0d50c3Schristos     output_section_(NULL),
3343ed0d50c3Schristos     memory_regions_(NULL),
3344ed0d50c3Schristos     phdrs_elements_(NULL),
3345ed0d50c3Schristos     orphan_section_placement_(NULL),
3346ed0d50c3Schristos     data_segment_align_start_(),
3347ed0d50c3Schristos     saw_data_segment_align_(false),
3348ed0d50c3Schristos     saw_relro_end_(false),
3349ed0d50c3Schristos     saw_segment_start_expression_(false),
3350ed0d50c3Schristos     segments_created_(false)
3351ed0d50c3Schristos {
3352ed0d50c3Schristos }
3353ed0d50c3Schristos 
3354ed0d50c3Schristos // Start a SECTIONS clause.
3355ed0d50c3Schristos 
3356ed0d50c3Schristos void
start_sections()3357ed0d50c3Schristos Script_sections::start_sections()
3358ed0d50c3Schristos {
3359ed0d50c3Schristos   gold_assert(!this->in_sections_clause_ && this->output_section_ == NULL);
3360ed0d50c3Schristos   this->saw_sections_clause_ = true;
3361ed0d50c3Schristos   this->in_sections_clause_ = true;
3362ed0d50c3Schristos   if (this->sections_elements_ == NULL)
3363ed0d50c3Schristos     this->sections_elements_ = new Sections_elements;
3364ed0d50c3Schristos }
3365ed0d50c3Schristos 
3366ed0d50c3Schristos // Finish a SECTIONS clause.
3367ed0d50c3Schristos 
3368ed0d50c3Schristos void
finish_sections()3369ed0d50c3Schristos Script_sections::finish_sections()
3370ed0d50c3Schristos {
3371ed0d50c3Schristos   gold_assert(this->in_sections_clause_ && this->output_section_ == NULL);
3372ed0d50c3Schristos   this->in_sections_clause_ = false;
3373ed0d50c3Schristos }
3374ed0d50c3Schristos 
3375ed0d50c3Schristos // Add a symbol to be defined.
3376ed0d50c3Schristos 
3377ed0d50c3Schristos void
add_symbol_assignment(const char * name,size_t length,Expression * val,bool provide,bool hidden)3378ed0d50c3Schristos Script_sections::add_symbol_assignment(const char* name, size_t length,
3379ed0d50c3Schristos 				       Expression* val, bool provide,
3380ed0d50c3Schristos 				       bool hidden)
3381ed0d50c3Schristos {
3382ed0d50c3Schristos   if (this->output_section_ != NULL)
3383ed0d50c3Schristos     this->output_section_->add_symbol_assignment(name, length, val,
3384ed0d50c3Schristos 						 provide, hidden);
3385ed0d50c3Schristos   else
3386ed0d50c3Schristos     {
3387ed0d50c3Schristos       Sections_element* p = new Sections_element_assignment(name, length,
3388ed0d50c3Schristos 							    val, provide,
3389ed0d50c3Schristos 							    hidden);
3390ed0d50c3Schristos       this->sections_elements_->push_back(p);
3391ed0d50c3Schristos     }
3392ed0d50c3Schristos }
3393ed0d50c3Schristos 
3394ed0d50c3Schristos // Add an assignment to the special dot symbol.
3395ed0d50c3Schristos 
3396ed0d50c3Schristos void
add_dot_assignment(Expression * val)3397ed0d50c3Schristos Script_sections::add_dot_assignment(Expression* val)
3398ed0d50c3Schristos {
3399ed0d50c3Schristos   if (this->output_section_ != NULL)
3400ed0d50c3Schristos     this->output_section_->add_dot_assignment(val);
3401ed0d50c3Schristos   else
3402ed0d50c3Schristos     {
3403ed0d50c3Schristos       // The GNU linker permits assignments to . to appears outside of
3404ed0d50c3Schristos       // a SECTIONS clause, and treats it as appearing inside, so
3405ed0d50c3Schristos       // sections_elements_ may be NULL here.
3406ed0d50c3Schristos       if (this->sections_elements_ == NULL)
3407ed0d50c3Schristos 	{
3408ed0d50c3Schristos 	  this->sections_elements_ = new Sections_elements;
3409ed0d50c3Schristos 	  this->saw_sections_clause_ = true;
3410ed0d50c3Schristos 	}
3411ed0d50c3Schristos 
3412ed0d50c3Schristos       Sections_element* p = new Sections_element_dot_assignment(val);
3413ed0d50c3Schristos       this->sections_elements_->push_back(p);
3414ed0d50c3Schristos     }
3415ed0d50c3Schristos }
3416ed0d50c3Schristos 
3417ed0d50c3Schristos // Add an assertion.
3418ed0d50c3Schristos 
3419ed0d50c3Schristos void
add_assertion(Expression * check,const char * message,size_t messagelen)3420ed0d50c3Schristos Script_sections::add_assertion(Expression* check, const char* message,
3421ed0d50c3Schristos 			       size_t messagelen)
3422ed0d50c3Schristos {
3423ed0d50c3Schristos   if (this->output_section_ != NULL)
3424ed0d50c3Schristos     this->output_section_->add_assertion(check, message, messagelen);
3425ed0d50c3Schristos   else
3426ed0d50c3Schristos     {
3427ed0d50c3Schristos       Sections_element* p = new Sections_element_assertion(check, message,
3428ed0d50c3Schristos 							   messagelen);
3429ed0d50c3Schristos       this->sections_elements_->push_back(p);
3430ed0d50c3Schristos     }
3431ed0d50c3Schristos }
3432ed0d50c3Schristos 
3433ed0d50c3Schristos // Start processing entries for an output section.
3434ed0d50c3Schristos 
3435ed0d50c3Schristos void
start_output_section(const char * name,size_t namelen,const Parser_output_section_header * header)3436ed0d50c3Schristos Script_sections::start_output_section(
3437ed0d50c3Schristos     const char* name,
3438ed0d50c3Schristos     size_t namelen,
3439ed0d50c3Schristos     const Parser_output_section_header* header)
3440ed0d50c3Schristos {
3441ed0d50c3Schristos   Output_section_definition* posd = new Output_section_definition(name,
3442ed0d50c3Schristos 								  namelen,
3443ed0d50c3Schristos 								  header);
3444ed0d50c3Schristos   this->sections_elements_->push_back(posd);
3445ed0d50c3Schristos   gold_assert(this->output_section_ == NULL);
3446ed0d50c3Schristos   this->output_section_ = posd;
3447ed0d50c3Schristos }
3448ed0d50c3Schristos 
3449ed0d50c3Schristos // Stop processing entries for an output section.
3450ed0d50c3Schristos 
3451ed0d50c3Schristos void
finish_output_section(const Parser_output_section_trailer * trailer)3452ed0d50c3Schristos Script_sections::finish_output_section(
3453ed0d50c3Schristos     const Parser_output_section_trailer* trailer)
3454ed0d50c3Schristos {
3455ed0d50c3Schristos   gold_assert(this->output_section_ != NULL);
3456ed0d50c3Schristos   this->output_section_->finish(trailer);
3457ed0d50c3Schristos   this->output_section_ = NULL;
3458ed0d50c3Schristos }
3459ed0d50c3Schristos 
3460ed0d50c3Schristos // Add a data item to the current output section.
3461ed0d50c3Schristos 
3462ed0d50c3Schristos void
add_data(int size,bool is_signed,Expression * val)3463ed0d50c3Schristos Script_sections::add_data(int size, bool is_signed, Expression* val)
3464ed0d50c3Schristos {
3465ed0d50c3Schristos   gold_assert(this->output_section_ != NULL);
3466ed0d50c3Schristos   this->output_section_->add_data(size, is_signed, val);
3467ed0d50c3Schristos }
3468ed0d50c3Schristos 
3469ed0d50c3Schristos // Add a fill value setting to the current output section.
3470ed0d50c3Schristos 
3471ed0d50c3Schristos void
add_fill(Expression * val)3472ed0d50c3Schristos Script_sections::add_fill(Expression* val)
3473ed0d50c3Schristos {
3474ed0d50c3Schristos   gold_assert(this->output_section_ != NULL);
3475ed0d50c3Schristos   this->output_section_->add_fill(val);
3476ed0d50c3Schristos }
3477ed0d50c3Schristos 
3478ed0d50c3Schristos // Add an input section specification to the current output section.
3479ed0d50c3Schristos 
3480ed0d50c3Schristos void
add_input_section(const Input_section_spec * spec,bool keep)3481ed0d50c3Schristos Script_sections::add_input_section(const Input_section_spec* spec, bool keep)
3482ed0d50c3Schristos {
3483ed0d50c3Schristos   gold_assert(this->output_section_ != NULL);
3484ed0d50c3Schristos   this->output_section_->add_input_section(spec, keep);
3485ed0d50c3Schristos }
3486ed0d50c3Schristos 
3487ed0d50c3Schristos // This is called when we see DATA_SEGMENT_ALIGN.  It means that any
3488ed0d50c3Schristos // subsequent output sections may be relro.
3489ed0d50c3Schristos 
3490ed0d50c3Schristos void
data_segment_align()3491ed0d50c3Schristos Script_sections::data_segment_align()
3492ed0d50c3Schristos {
3493ed0d50c3Schristos   if (this->saw_data_segment_align_)
3494ed0d50c3Schristos     gold_error(_("DATA_SEGMENT_ALIGN may only appear once in a linker script"));
3495ed0d50c3Schristos   gold_assert(!this->sections_elements_->empty());
3496ed0d50c3Schristos   Sections_elements::iterator p = this->sections_elements_->end();
3497ed0d50c3Schristos   --p;
3498ed0d50c3Schristos   this->data_segment_align_start_ = p;
3499ed0d50c3Schristos   this->saw_data_segment_align_ = true;
3500ed0d50c3Schristos }
3501ed0d50c3Schristos 
3502ed0d50c3Schristos // This is called when we see DATA_SEGMENT_RELRO_END.  It means that
3503ed0d50c3Schristos // any output sections seen since DATA_SEGMENT_ALIGN are relro.
3504ed0d50c3Schristos 
3505ed0d50c3Schristos void
data_segment_relro_end()3506ed0d50c3Schristos Script_sections::data_segment_relro_end()
3507ed0d50c3Schristos {
3508ed0d50c3Schristos   if (this->saw_relro_end_)
3509ed0d50c3Schristos     gold_error(_("DATA_SEGMENT_RELRO_END may only appear once "
3510ed0d50c3Schristos 		 "in a linker script"));
3511ed0d50c3Schristos   this->saw_relro_end_ = true;
3512ed0d50c3Schristos 
3513ed0d50c3Schristos   if (!this->saw_data_segment_align_)
3514ed0d50c3Schristos     gold_error(_("DATA_SEGMENT_RELRO_END must follow DATA_SEGMENT_ALIGN"));
3515ed0d50c3Schristos   else
3516ed0d50c3Schristos     {
3517ed0d50c3Schristos       Sections_elements::iterator p = this->data_segment_align_start_;
3518ed0d50c3Schristos       for (++p; p != this->sections_elements_->end(); ++p)
3519ed0d50c3Schristos 	(*p)->set_is_relro();
3520ed0d50c3Schristos     }
3521ed0d50c3Schristos }
3522ed0d50c3Schristos 
3523ed0d50c3Schristos // Create any required sections.
3524ed0d50c3Schristos 
3525ed0d50c3Schristos void
create_sections(Layout * layout)3526ed0d50c3Schristos Script_sections::create_sections(Layout* layout)
3527ed0d50c3Schristos {
3528ed0d50c3Schristos   if (!this->saw_sections_clause_)
3529ed0d50c3Schristos     return;
3530ed0d50c3Schristos   for (Sections_elements::iterator p = this->sections_elements_->begin();
3531ed0d50c3Schristos        p != this->sections_elements_->end();
3532ed0d50c3Schristos        ++p)
3533ed0d50c3Schristos     (*p)->create_sections(layout);
3534ed0d50c3Schristos }
3535ed0d50c3Schristos 
3536ed0d50c3Schristos // Add any symbols we are defining to the symbol table.
3537ed0d50c3Schristos 
3538ed0d50c3Schristos void
add_symbols_to_table(Symbol_table * symtab)3539ed0d50c3Schristos Script_sections::add_symbols_to_table(Symbol_table* symtab)
3540ed0d50c3Schristos {
3541ed0d50c3Schristos   if (!this->saw_sections_clause_)
3542ed0d50c3Schristos     return;
3543ed0d50c3Schristos   for (Sections_elements::iterator p = this->sections_elements_->begin();
3544ed0d50c3Schristos        p != this->sections_elements_->end();
3545ed0d50c3Schristos        ++p)
3546ed0d50c3Schristos     (*p)->add_symbols_to_table(symtab);
3547ed0d50c3Schristos }
3548ed0d50c3Schristos 
3549ed0d50c3Schristos // Finalize symbols and check assertions.
3550ed0d50c3Schristos 
3551ed0d50c3Schristos void
finalize_symbols(Symbol_table * symtab,const Layout * layout)3552ed0d50c3Schristos Script_sections::finalize_symbols(Symbol_table* symtab, const Layout* layout)
3553ed0d50c3Schristos {
3554ed0d50c3Schristos   if (!this->saw_sections_clause_)
3555ed0d50c3Schristos     return;
3556ed0d50c3Schristos   uint64_t dot_value = 0;
3557ed0d50c3Schristos   for (Sections_elements::iterator p = this->sections_elements_->begin();
3558ed0d50c3Schristos        p != this->sections_elements_->end();
3559ed0d50c3Schristos        ++p)
3560ed0d50c3Schristos     (*p)->finalize_symbols(symtab, layout, &dot_value);
3561ed0d50c3Schristos }
3562ed0d50c3Schristos 
3563ed0d50c3Schristos // Return the name of the output section to use for an input file name
3564ed0d50c3Schristos // and section name.
3565ed0d50c3Schristos 
3566ed0d50c3Schristos const char*
output_section_name(const char * file_name,const char * section_name,Output_section *** output_section_slot,Script_sections::Section_type * psection_type,bool * keep,bool is_input_section)3567ed0d50c3Schristos Script_sections::output_section_name(
3568ed0d50c3Schristos     const char* file_name,
3569ed0d50c3Schristos     const char* section_name,
3570ed0d50c3Schristos     Output_section*** output_section_slot,
3571ed0d50c3Schristos     Script_sections::Section_type* psection_type,
357206324dcfSchristos     bool* keep,
357306324dcfSchristos     bool is_input_section)
3574ed0d50c3Schristos {
3575ed0d50c3Schristos   for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3576ed0d50c3Schristos        p != this->sections_elements_->end();
3577ed0d50c3Schristos        ++p)
3578ed0d50c3Schristos     {
3579ed0d50c3Schristos       const char* ret = (*p)->output_section_name(file_name, section_name,
3580ed0d50c3Schristos 						  output_section_slot,
358106324dcfSchristos 						  psection_type, keep,
358206324dcfSchristos 						  is_input_section);
3583ed0d50c3Schristos 
3584ed0d50c3Schristos       if (ret != NULL)
3585ed0d50c3Schristos 	{
3586ed0d50c3Schristos 	  // The special name /DISCARD/ means that the input section
3587ed0d50c3Schristos 	  // should be discarded.
3588ed0d50c3Schristos 	  if (strcmp(ret, "/DISCARD/") == 0)
3589ed0d50c3Schristos 	    {
3590ed0d50c3Schristos 	      *output_section_slot = NULL;
3591ed0d50c3Schristos 	      *psection_type = Script_sections::ST_NONE;
3592ed0d50c3Schristos 	      return NULL;
3593ed0d50c3Schristos 	    }
3594ed0d50c3Schristos 	  return ret;
3595ed0d50c3Schristos 	}
3596ed0d50c3Schristos     }
3597ed0d50c3Schristos 
359806324dcfSchristos   // We have an orphan section.
3599ed0d50c3Schristos   *output_section_slot = NULL;
3600ed0d50c3Schristos   *psection_type = Script_sections::ST_NONE;
360106324dcfSchristos   *keep = false;
3602ed0d50c3Schristos 
360306324dcfSchristos   General_options::Orphan_handling orphan_handling =
360406324dcfSchristos       parameters->options().orphan_handling_enum();
360506324dcfSchristos   if (orphan_handling == General_options::ORPHAN_DISCARD)
360606324dcfSchristos     return NULL;
360706324dcfSchristos   if (orphan_handling == General_options::ORPHAN_ERROR)
360806324dcfSchristos     {
360906324dcfSchristos       if (file_name == NULL)
361006324dcfSchristos 	gold_error(_("unplaced orphan section '%s'"), section_name);
361106324dcfSchristos       else
361206324dcfSchristos 	gold_error(_("unplaced orphan section '%s' from '%s'"),
361306324dcfSchristos 		   section_name, file_name);
361406324dcfSchristos       return NULL;
361506324dcfSchristos     }
361606324dcfSchristos   if (orphan_handling == General_options::ORPHAN_WARN)
361706324dcfSchristos     {
361806324dcfSchristos       if (file_name == NULL)
361906324dcfSchristos 	gold_warning(_("orphan section '%s' is being placed in section '%s'"),
362006324dcfSchristos 		     section_name, section_name);
362106324dcfSchristos       else
362206324dcfSchristos 	gold_warning(_("orphan section '%s' from '%s' is being placed "
362306324dcfSchristos 		       "in section '%s'"),
362406324dcfSchristos 		     section_name, file_name, section_name);
362506324dcfSchristos     }
362606324dcfSchristos 
362706324dcfSchristos   // If we couldn't find a mapping for the name, the output section
362806324dcfSchristos   // gets the name of the input section.
3629ed0d50c3Schristos   return section_name;
3630ed0d50c3Schristos }
3631ed0d50c3Schristos 
3632ed0d50c3Schristos // Place a marker for an orphan output section into the SECTIONS
3633ed0d50c3Schristos // clause.
3634ed0d50c3Schristos 
3635ed0d50c3Schristos void
place_orphan(Output_section * os)3636ed0d50c3Schristos Script_sections::place_orphan(Output_section* os)
3637ed0d50c3Schristos {
3638ed0d50c3Schristos   Orphan_section_placement* osp = this->orphan_section_placement_;
3639ed0d50c3Schristos   if (osp == NULL)
3640ed0d50c3Schristos     {
3641ed0d50c3Schristos       // Initialize the Orphan_section_placement structure.
3642ed0d50c3Schristos       osp = new Orphan_section_placement();
3643ed0d50c3Schristos       for (Sections_elements::iterator p = this->sections_elements_->begin();
3644ed0d50c3Schristos 	   p != this->sections_elements_->end();
3645ed0d50c3Schristos 	   ++p)
3646ed0d50c3Schristos 	(*p)->orphan_section_init(osp, p);
3647ed0d50c3Schristos       gold_assert(!this->sections_elements_->empty());
3648ed0d50c3Schristos       Sections_elements::iterator last = this->sections_elements_->end();
3649ed0d50c3Schristos       --last;
3650ed0d50c3Schristos       osp->last_init(last);
3651ed0d50c3Schristos       this->orphan_section_placement_ = osp;
3652ed0d50c3Schristos     }
3653ed0d50c3Schristos 
3654ed0d50c3Schristos   Orphan_output_section* orphan = new Orphan_output_section(os);
3655ed0d50c3Schristos 
3656ed0d50c3Schristos   // Look for where to put ORPHAN.
3657ed0d50c3Schristos   Sections_elements::iterator* where;
3658ed0d50c3Schristos   if (osp->find_place(os, &where))
3659ed0d50c3Schristos     {
3660ed0d50c3Schristos       if ((**where)->is_relro())
3661ed0d50c3Schristos 	os->set_is_relro();
3662ed0d50c3Schristos       else
3663ed0d50c3Schristos 	os->clear_is_relro();
3664ed0d50c3Schristos 
3665ed0d50c3Schristos       // We want to insert ORPHAN after *WHERE, and then update *WHERE
3666ed0d50c3Schristos       // so that the next one goes after this one.
3667ed0d50c3Schristos       Sections_elements::iterator p = *where;
3668ed0d50c3Schristos       gold_assert(p != this->sections_elements_->end());
3669ed0d50c3Schristos       ++p;
3670ed0d50c3Schristos       *where = this->sections_elements_->insert(p, orphan);
3671ed0d50c3Schristos     }
3672ed0d50c3Schristos   else
3673ed0d50c3Schristos     {
3674ed0d50c3Schristos       os->clear_is_relro();
3675ed0d50c3Schristos       // We don't have a place to put this orphan section.  Put it,
3676ed0d50c3Schristos       // and all other sections like it, at the end, but before the
3677ed0d50c3Schristos       // sections which always come at the end.
3678ed0d50c3Schristos       Sections_elements::iterator last = osp->last_place();
3679ed0d50c3Schristos       *where = this->sections_elements_->insert(last, orphan);
3680ed0d50c3Schristos     }
368106324dcfSchristos 
368206324dcfSchristos   if ((os->flags() & elfcpp::SHF_ALLOC) != 0)
368306324dcfSchristos     osp->update_last_alloc(*where);
3684ed0d50c3Schristos }
3685ed0d50c3Schristos 
3686ed0d50c3Schristos // Set the addresses of all the output sections.  Walk through all the
3687ed0d50c3Schristos // elements, tracking the dot symbol.  Apply assignments which set
3688ed0d50c3Schristos // absolute symbol values, in case they are used when setting dot.
3689ed0d50c3Schristos // Fill in data statement values.  As we find output sections, set the
3690ed0d50c3Schristos // address, set the address of all associated input sections, and
3691ed0d50c3Schristos // update dot.  Return the segment which should hold the file header
3692ed0d50c3Schristos // and segment headers, if any.
3693ed0d50c3Schristos 
3694ed0d50c3Schristos Output_segment*
set_section_addresses(Symbol_table * symtab,Layout * layout)3695ed0d50c3Schristos Script_sections::set_section_addresses(Symbol_table* symtab, Layout* layout)
3696ed0d50c3Schristos {
3697ed0d50c3Schristos   gold_assert(this->saw_sections_clause_);
3698ed0d50c3Schristos 
3699ed0d50c3Schristos   // Implement ONLY_IF_RO/ONLY_IF_RW constraints.  These are a pain
3700ed0d50c3Schristos   // for our representation.
3701ed0d50c3Schristos   for (Sections_elements::iterator p = this->sections_elements_->begin();
3702ed0d50c3Schristos        p != this->sections_elements_->end();
3703ed0d50c3Schristos        ++p)
3704ed0d50c3Schristos     {
3705ed0d50c3Schristos       Output_section_definition* posd;
3706ed0d50c3Schristos       Section_constraint failed_constraint = (*p)->check_constraint(&posd);
3707ed0d50c3Schristos       if (failed_constraint != CONSTRAINT_NONE)
3708ed0d50c3Schristos 	{
3709ed0d50c3Schristos 	  Sections_elements::iterator q;
3710ed0d50c3Schristos 	  for (q = this->sections_elements_->begin();
3711ed0d50c3Schristos 	       q != this->sections_elements_->end();
3712ed0d50c3Schristos 	       ++q)
3713ed0d50c3Schristos 	    {
3714ed0d50c3Schristos 	      if (q != p)
3715ed0d50c3Schristos 		{
3716ed0d50c3Schristos 		  if ((*q)->alternate_constraint(posd, failed_constraint))
3717ed0d50c3Schristos 		    break;
3718ed0d50c3Schristos 		}
3719ed0d50c3Schristos 	    }
3720ed0d50c3Schristos 
3721ed0d50c3Schristos 	  if (q == this->sections_elements_->end())
3722ed0d50c3Schristos 	    gold_error(_("no matching section constraint"));
3723ed0d50c3Schristos 	}
3724ed0d50c3Schristos     }
3725ed0d50c3Schristos 
3726ed0d50c3Schristos   // Force the alignment of the first TLS section to be the maximum
3727ed0d50c3Schristos   // alignment of all TLS sections.
3728ed0d50c3Schristos   Output_section* first_tls = NULL;
3729ed0d50c3Schristos   uint64_t tls_align = 0;
3730ed0d50c3Schristos   for (Sections_elements::const_iterator p = this->sections_elements_->begin();
3731ed0d50c3Schristos        p != this->sections_elements_->end();
3732ed0d50c3Schristos        ++p)
3733ed0d50c3Schristos     {
3734ed0d50c3Schristos       Output_section* os = (*p)->get_output_section();
3735ed0d50c3Schristos       if (os != NULL && (os->flags() & elfcpp::SHF_TLS) != 0)
3736ed0d50c3Schristos 	{
3737ed0d50c3Schristos 	  if (first_tls == NULL)
3738ed0d50c3Schristos 	    first_tls = os;
3739ed0d50c3Schristos 	  if (os->addralign() > tls_align)
3740ed0d50c3Schristos 	    tls_align = os->addralign();
3741ed0d50c3Schristos 	}
3742ed0d50c3Schristos     }
3743ed0d50c3Schristos   if (first_tls != NULL)
3744ed0d50c3Schristos     first_tls->set_addralign(tls_align);
3745ed0d50c3Schristos 
3746ed0d50c3Schristos   // For a relocatable link, we implicitly set dot to zero.
3747ed0d50c3Schristos   uint64_t dot_value = 0;
3748ed0d50c3Schristos   uint64_t dot_alignment = 0;
3749ed0d50c3Schristos   uint64_t load_address = 0;
3750ed0d50c3Schristos 
3751ed0d50c3Schristos   // Check to see if we want to use any of -Ttext, -Tdata and -Tbss options
3752ed0d50c3Schristos   // to set section addresses.  If the script has any SEGMENT_START
3753ed0d50c3Schristos   // expression, we do not set the section addresses.
3754ed0d50c3Schristos   bool use_tsection_options =
3755ed0d50c3Schristos     (!this->saw_segment_start_expression_
3756ed0d50c3Schristos      && (parameters->options().user_set_Ttext()
3757ed0d50c3Schristos 	 || parameters->options().user_set_Tdata()
3758ed0d50c3Schristos 	 || parameters->options().user_set_Tbss()));
3759ed0d50c3Schristos 
3760ed0d50c3Schristos   for (Sections_elements::iterator p = this->sections_elements_->begin();
3761ed0d50c3Schristos        p != this->sections_elements_->end();
3762ed0d50c3Schristos        ++p)
3763ed0d50c3Schristos     {
3764ed0d50c3Schristos       Output_section* os = (*p)->get_output_section();
3765ed0d50c3Schristos 
3766ed0d50c3Schristos       // Handle -Ttext, -Tdata and -Tbss options.  We do this by looking for
3767ed0d50c3Schristos       // the special sections by names and doing dot assignments.
3768ed0d50c3Schristos       if (use_tsection_options
3769ed0d50c3Schristos 	  && os != NULL
3770ed0d50c3Schristos 	  && (os->flags() & elfcpp::SHF_ALLOC) != 0)
3771ed0d50c3Schristos 	{
3772ed0d50c3Schristos 	  uint64_t new_dot_value = dot_value;
3773ed0d50c3Schristos 
3774ed0d50c3Schristos 	  if (parameters->options().user_set_Ttext()
3775ed0d50c3Schristos 	      && strcmp(os->name(), ".text") == 0)
3776ed0d50c3Schristos 	    new_dot_value = parameters->options().Ttext();
3777ed0d50c3Schristos 	  else if (parameters->options().user_set_Tdata()
3778ed0d50c3Schristos 	      && strcmp(os->name(), ".data") == 0)
3779ed0d50c3Schristos 	    new_dot_value = parameters->options().Tdata();
3780ed0d50c3Schristos 	  else if (parameters->options().user_set_Tbss()
3781ed0d50c3Schristos 	      && strcmp(os->name(), ".bss") == 0)
3782ed0d50c3Schristos 	    new_dot_value = parameters->options().Tbss();
3783ed0d50c3Schristos 
3784ed0d50c3Schristos 	  // Update dot and load address if necessary.
3785ed0d50c3Schristos 	  if (new_dot_value < dot_value)
3786ed0d50c3Schristos 	    gold_error(_("dot may not move backward"));
3787ed0d50c3Schristos 	  else if (new_dot_value != dot_value)
3788ed0d50c3Schristos 	    {
3789ed0d50c3Schristos 	      dot_value = new_dot_value;
3790ed0d50c3Schristos 	      load_address = new_dot_value;
3791ed0d50c3Schristos 	    }
3792ed0d50c3Schristos 	}
3793ed0d50c3Schristos 
3794ed0d50c3Schristos       (*p)->set_section_addresses(symtab, layout, &dot_value, &dot_alignment,
3795ed0d50c3Schristos 				  &load_address);
3796ed0d50c3Schristos     }
3797ed0d50c3Schristos 
3798ed0d50c3Schristos   if (this->phdrs_elements_ != NULL)
3799ed0d50c3Schristos     {
3800ed0d50c3Schristos       for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
3801ed0d50c3Schristos 	   p != this->phdrs_elements_->end();
3802ed0d50c3Schristos 	   ++p)
3803ed0d50c3Schristos 	(*p)->eval_load_address(symtab, layout);
3804ed0d50c3Schristos     }
3805ed0d50c3Schristos 
3806ed0d50c3Schristos   return this->create_segments(layout, dot_alignment);
3807ed0d50c3Schristos }
3808ed0d50c3Schristos 
3809ed0d50c3Schristos // Sort the sections in order to put them into segments.
3810ed0d50c3Schristos 
3811ed0d50c3Schristos class Sort_output_sections
3812ed0d50c3Schristos {
3813ed0d50c3Schristos  public:
Sort_output_sections(const Script_sections::Sections_elements * elements)3814ed0d50c3Schristos   Sort_output_sections(const Script_sections::Sections_elements* elements)
3815ed0d50c3Schristos    : elements_(elements)
3816ed0d50c3Schristos   { }
3817ed0d50c3Schristos 
3818ed0d50c3Schristos   bool
3819ed0d50c3Schristos   operator()(const Output_section* os1, const Output_section* os2) const;
3820ed0d50c3Schristos 
3821ed0d50c3Schristos  private:
3822ed0d50c3Schristos   int
3823ed0d50c3Schristos   script_compare(const Output_section* os1, const Output_section* os2) const;
3824ed0d50c3Schristos 
3825ed0d50c3Schristos  private:
3826ed0d50c3Schristos   const Script_sections::Sections_elements* elements_;
3827ed0d50c3Schristos };
3828ed0d50c3Schristos 
3829ed0d50c3Schristos bool
operator ()(const Output_section * os1,const Output_section * os2) const3830ed0d50c3Schristos Sort_output_sections::operator()(const Output_section* os1,
3831ed0d50c3Schristos 				 const Output_section* os2) const
3832ed0d50c3Schristos {
3833ed0d50c3Schristos   // Sort first by the load address.
3834ed0d50c3Schristos   uint64_t lma1 = (os1->has_load_address()
3835ed0d50c3Schristos 		   ? os1->load_address()
3836ed0d50c3Schristos 		   : os1->address());
3837ed0d50c3Schristos   uint64_t lma2 = (os2->has_load_address()
3838ed0d50c3Schristos 		   ? os2->load_address()
3839ed0d50c3Schristos 		   : os2->address());
3840ed0d50c3Schristos   if (lma1 != lma2)
3841ed0d50c3Schristos     return lma1 < lma2;
3842ed0d50c3Schristos 
3843ed0d50c3Schristos   // Then sort by the virtual address.
3844ed0d50c3Schristos   if (os1->address() != os2->address())
3845ed0d50c3Schristos     return os1->address() < os2->address();
3846ed0d50c3Schristos 
3847ed0d50c3Schristos   // If the linker script says which of these sections is first, go
3848ed0d50c3Schristos   // with what it says.
3849ed0d50c3Schristos   int i = this->script_compare(os1, os2);
3850ed0d50c3Schristos   if (i != 0)
3851ed0d50c3Schristos     return i < 0;
3852ed0d50c3Schristos 
3853ed0d50c3Schristos   // Sort PROGBITS before NOBITS.
3854ed0d50c3Schristos   bool nobits1 = os1->type() == elfcpp::SHT_NOBITS;
3855ed0d50c3Schristos   bool nobits2 = os2->type() == elfcpp::SHT_NOBITS;
3856ed0d50c3Schristos   if (nobits1 != nobits2)
3857ed0d50c3Schristos     return nobits2;
3858ed0d50c3Schristos 
3859ed0d50c3Schristos   // Sort PROGBITS TLS sections to the end, NOBITS TLS sections to the
3860ed0d50c3Schristos   // beginning.
3861ed0d50c3Schristos   bool tls1 = (os1->flags() & elfcpp::SHF_TLS) != 0;
3862ed0d50c3Schristos   bool tls2 = (os2->flags() & elfcpp::SHF_TLS) != 0;
3863ed0d50c3Schristos   if (tls1 != tls2)
3864ed0d50c3Schristos     return nobits1 ? tls1 : tls2;
3865ed0d50c3Schristos 
3866ed0d50c3Schristos   // Sort non-NOLOAD before NOLOAD.
3867ed0d50c3Schristos   if (os1->is_noload() && !os2->is_noload())
3868ed0d50c3Schristos     return true;
3869ed0d50c3Schristos   if (!os1->is_noload() && os2->is_noload())
3870ed0d50c3Schristos     return true;
3871ed0d50c3Schristos 
3872ed0d50c3Schristos   // The sections seem practically identical.  Sort by name to get a
3873ed0d50c3Schristos   // stable sort.
3874ed0d50c3Schristos   return os1->name() < os2->name();
3875ed0d50c3Schristos }
3876ed0d50c3Schristos 
3877ed0d50c3Schristos // Return -1 if OS1 comes before OS2 in ELEMENTS_, 1 if comes after, 0
3878ed0d50c3Schristos // if either OS1 or OS2 is not mentioned.  This ensures that we keep
3879ed0d50c3Schristos // empty sections in the order in which they appear in a linker
3880ed0d50c3Schristos // script.
3881ed0d50c3Schristos 
3882ed0d50c3Schristos int
script_compare(const Output_section * os1,const Output_section * os2) const3883ed0d50c3Schristos Sort_output_sections::script_compare(const Output_section* os1,
3884ed0d50c3Schristos 				     const Output_section* os2) const
3885ed0d50c3Schristos {
3886ed0d50c3Schristos   if (this->elements_ == NULL)
3887ed0d50c3Schristos     return 0;
3888ed0d50c3Schristos 
3889ed0d50c3Schristos   bool found_os1 = false;
3890ed0d50c3Schristos   bool found_os2 = false;
3891ed0d50c3Schristos   for (Script_sections::Sections_elements::const_iterator
3892ed0d50c3Schristos 	 p = this->elements_->begin();
3893ed0d50c3Schristos        p != this->elements_->end();
3894ed0d50c3Schristos        ++p)
3895ed0d50c3Schristos     {
3896ed0d50c3Schristos       if (os2 == (*p)->get_output_section())
3897ed0d50c3Schristos 	{
3898ed0d50c3Schristos 	  if (found_os1)
3899ed0d50c3Schristos 	    return -1;
3900ed0d50c3Schristos 	  found_os2 = true;
3901ed0d50c3Schristos 	}
3902ed0d50c3Schristos       else if (os1 == (*p)->get_output_section())
3903ed0d50c3Schristos 	{
3904ed0d50c3Schristos 	  if (found_os2)
3905ed0d50c3Schristos 	    return 1;
3906ed0d50c3Schristos 	  found_os1 = true;
3907ed0d50c3Schristos 	}
3908ed0d50c3Schristos     }
3909ed0d50c3Schristos 
3910ed0d50c3Schristos   return 0;
3911ed0d50c3Schristos }
3912ed0d50c3Schristos 
3913ed0d50c3Schristos // Return whether OS is a BSS section.  This is a SHT_NOBITS section.
3914ed0d50c3Schristos // We treat a section with the SHF_TLS flag set as taking up space
3915ed0d50c3Schristos // even if it is SHT_NOBITS (this is true of .tbss), as we allocate
3916ed0d50c3Schristos // space for them in the file.
3917ed0d50c3Schristos 
3918ed0d50c3Schristos bool
is_bss_section(const Output_section * os)3919ed0d50c3Schristos Script_sections::is_bss_section(const Output_section* os)
3920ed0d50c3Schristos {
3921ed0d50c3Schristos   return (os->type() == elfcpp::SHT_NOBITS
3922ed0d50c3Schristos 	  && (os->flags() & elfcpp::SHF_TLS) == 0);
3923ed0d50c3Schristos }
3924ed0d50c3Schristos 
3925ed0d50c3Schristos // Return the size taken by the file header and the program headers.
3926ed0d50c3Schristos 
3927ed0d50c3Schristos size_t
total_header_size(Layout * layout) const3928ed0d50c3Schristos Script_sections::total_header_size(Layout* layout) const
3929ed0d50c3Schristos {
3930ed0d50c3Schristos   size_t segment_count = layout->segment_count();
3931ed0d50c3Schristos   size_t file_header_size;
3932ed0d50c3Schristos   size_t segment_headers_size;
3933ed0d50c3Schristos   if (parameters->target().get_size() == 32)
3934ed0d50c3Schristos     {
3935ed0d50c3Schristos       file_header_size = elfcpp::Elf_sizes<32>::ehdr_size;
3936ed0d50c3Schristos       segment_headers_size = segment_count * elfcpp::Elf_sizes<32>::phdr_size;
3937ed0d50c3Schristos     }
3938ed0d50c3Schristos   else if (parameters->target().get_size() == 64)
3939ed0d50c3Schristos     {
3940ed0d50c3Schristos       file_header_size = elfcpp::Elf_sizes<64>::ehdr_size;
3941ed0d50c3Schristos       segment_headers_size = segment_count * elfcpp::Elf_sizes<64>::phdr_size;
3942ed0d50c3Schristos     }
3943ed0d50c3Schristos   else
3944ed0d50c3Schristos     gold_unreachable();
3945ed0d50c3Schristos 
3946ed0d50c3Schristos   return file_header_size + segment_headers_size;
3947ed0d50c3Schristos }
3948ed0d50c3Schristos 
3949ed0d50c3Schristos // Return the amount we have to subtract from the LMA to accommodate
3950ed0d50c3Schristos // headers of the given size.  The complication is that the file
3951ed0d50c3Schristos // header have to be at the start of a page, as otherwise it will not
3952ed0d50c3Schristos // be at the start of the file.
3953ed0d50c3Schristos 
3954ed0d50c3Schristos uint64_t
header_size_adjustment(uint64_t lma,size_t sizeof_headers) const3955ed0d50c3Schristos Script_sections::header_size_adjustment(uint64_t lma,
3956ed0d50c3Schristos 					size_t sizeof_headers) const
3957ed0d50c3Schristos {
3958ed0d50c3Schristos   const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3959ed0d50c3Schristos   uint64_t hdr_lma = lma - sizeof_headers;
3960ed0d50c3Schristos   hdr_lma &= ~(abi_pagesize - 1);
3961ed0d50c3Schristos   return lma - hdr_lma;
3962ed0d50c3Schristos }
3963ed0d50c3Schristos 
3964ed0d50c3Schristos // Create the PT_LOAD segments when using a SECTIONS clause.  Returns
3965ed0d50c3Schristos // the segment which should hold the file header and segment headers,
3966ed0d50c3Schristos // if any.
3967ed0d50c3Schristos 
3968ed0d50c3Schristos Output_segment*
create_segments(Layout * layout,uint64_t dot_alignment)3969ed0d50c3Schristos Script_sections::create_segments(Layout* layout, uint64_t dot_alignment)
3970ed0d50c3Schristos {
3971ed0d50c3Schristos   gold_assert(this->saw_sections_clause_);
3972ed0d50c3Schristos 
3973ed0d50c3Schristos   if (parameters->options().relocatable())
3974ed0d50c3Schristos     return NULL;
3975ed0d50c3Schristos 
3976ed0d50c3Schristos   if (this->saw_phdrs_clause())
3977ed0d50c3Schristos     return create_segments_from_phdrs_clause(layout, dot_alignment);
3978ed0d50c3Schristos 
3979ed0d50c3Schristos   Layout::Section_list sections;
3980ed0d50c3Schristos   layout->get_allocated_sections(&sections);
3981ed0d50c3Schristos 
3982ed0d50c3Schristos   // Sort the sections by address.
3983ed0d50c3Schristos   std::stable_sort(sections.begin(), sections.end(),
3984ed0d50c3Schristos 		   Sort_output_sections(this->sections_elements_));
3985ed0d50c3Schristos 
3986ed0d50c3Schristos   this->create_note_and_tls_segments(layout, &sections);
3987ed0d50c3Schristos 
3988ed0d50c3Schristos   // Walk through the sections adding them to PT_LOAD segments.
3989ed0d50c3Schristos   const uint64_t abi_pagesize = parameters->target().abi_pagesize();
3990ed0d50c3Schristos   Output_segment* first_seg = NULL;
3991ed0d50c3Schristos   Output_segment* current_seg = NULL;
3992ed0d50c3Schristos   bool is_current_seg_readonly = true;
3993ed0d50c3Schristos   uint64_t last_vma = 0;
3994ed0d50c3Schristos   uint64_t last_lma = 0;
3995ed0d50c3Schristos   uint64_t last_size = 0;
399606324dcfSchristos   bool in_bss = false;
3997ed0d50c3Schristos   for (Layout::Section_list::iterator p = sections.begin();
3998ed0d50c3Schristos        p != sections.end();
3999ed0d50c3Schristos        ++p)
4000ed0d50c3Schristos     {
4001ed0d50c3Schristos       const uint64_t vma = (*p)->address();
4002ed0d50c3Schristos       const uint64_t lma = ((*p)->has_load_address()
4003ed0d50c3Schristos 			    ? (*p)->load_address()
4004ed0d50c3Schristos 			    : vma);
4005ed0d50c3Schristos       const uint64_t size = (*p)->current_data_size();
4006ed0d50c3Schristos 
4007ed0d50c3Schristos       bool need_new_segment;
4008ed0d50c3Schristos       if (current_seg == NULL)
4009ed0d50c3Schristos 	need_new_segment = true;
4010ed0d50c3Schristos       else if (lma - vma != last_lma - last_vma)
4011ed0d50c3Schristos 	{
4012ed0d50c3Schristos 	  // This section has a different LMA relationship than the
4013ed0d50c3Schristos 	  // last one; we need a new segment.
4014ed0d50c3Schristos 	  need_new_segment = true;
4015ed0d50c3Schristos 	}
4016ed0d50c3Schristos       else if (align_address(last_lma + last_size, abi_pagesize)
4017ed0d50c3Schristos 	       < align_address(lma, abi_pagesize))
4018ed0d50c3Schristos 	{
4019ed0d50c3Schristos 	  // Putting this section in the segment would require
4020ed0d50c3Schristos 	  // skipping a page.
4021ed0d50c3Schristos 	  need_new_segment = true;
4022ed0d50c3Schristos 	}
402306324dcfSchristos       else if (in_bss && !is_bss_section(*p))
4024ed0d50c3Schristos 	{
4025ed0d50c3Schristos 	  // A non-BSS section can not follow a BSS section in the
4026ed0d50c3Schristos 	  // same segment.
4027ed0d50c3Schristos 	  need_new_segment = true;
4028ed0d50c3Schristos 	}
4029ed0d50c3Schristos       else if (is_current_seg_readonly
4030ed0d50c3Schristos 	       && ((*p)->flags() & elfcpp::SHF_WRITE) != 0
4031ed0d50c3Schristos 	       && !parameters->options().omagic())
4032ed0d50c3Schristos 	{
4033ed0d50c3Schristos 	  // Don't put a writable section in the same segment as a
4034ed0d50c3Schristos 	  // non-writable section.
4035ed0d50c3Schristos 	  need_new_segment = true;
4036ed0d50c3Schristos 	}
4037ed0d50c3Schristos       else
4038ed0d50c3Schristos 	{
4039ed0d50c3Schristos 	  // Otherwise, reuse the existing segment.
4040ed0d50c3Schristos 	  need_new_segment = false;
4041ed0d50c3Schristos 	}
4042ed0d50c3Schristos 
4043ed0d50c3Schristos       elfcpp::Elf_Word seg_flags =
4044ed0d50c3Schristos 	Layout::section_flags_to_segment((*p)->flags());
4045ed0d50c3Schristos 
4046ed0d50c3Schristos       if (need_new_segment)
4047ed0d50c3Schristos 	{
4048ed0d50c3Schristos 	  current_seg = layout->make_output_segment(elfcpp::PT_LOAD,
4049ed0d50c3Schristos 						    seg_flags);
4050ed0d50c3Schristos 	  current_seg->set_addresses(vma, lma);
4051ed0d50c3Schristos 	  current_seg->set_minimum_p_align(dot_alignment);
4052ed0d50c3Schristos 	  if (first_seg == NULL)
4053ed0d50c3Schristos 	    first_seg = current_seg;
4054ed0d50c3Schristos 	  is_current_seg_readonly = true;
405506324dcfSchristos 	  in_bss = false;
4056ed0d50c3Schristos 	}
4057ed0d50c3Schristos 
4058ed0d50c3Schristos       current_seg->add_output_section_to_load(layout, *p, seg_flags);
4059ed0d50c3Schristos 
4060ed0d50c3Schristos       if (((*p)->flags() & elfcpp::SHF_WRITE) != 0)
4061ed0d50c3Schristos 	is_current_seg_readonly = false;
4062ed0d50c3Schristos 
406306324dcfSchristos       if (is_bss_section(*p) && size > 0)
406406324dcfSchristos         in_bss = true;
406506324dcfSchristos 
4066ed0d50c3Schristos       last_vma = vma;
4067ed0d50c3Schristos       last_lma = lma;
4068ed0d50c3Schristos       last_size = size;
4069ed0d50c3Schristos     }
4070ed0d50c3Schristos 
4071ed0d50c3Schristos   // An ELF program should work even if the program headers are not in
4072ed0d50c3Schristos   // a PT_LOAD segment.  However, it appears that the Linux kernel
4073ed0d50c3Schristos   // does not set the AT_PHDR auxiliary entry in that case.  It sets
4074ed0d50c3Schristos   // the load address to p_vaddr - p_offset of the first PT_LOAD
4075ed0d50c3Schristos   // segment.  It then sets AT_PHDR to the load address plus the
4076ed0d50c3Schristos   // offset to the program headers, e_phoff in the file header.  This
4077ed0d50c3Schristos   // fails when the program headers appear in the file before the
4078ed0d50c3Schristos   // first PT_LOAD segment.  Therefore, we always create a PT_LOAD
4079ed0d50c3Schristos   // segment to hold the file header and the program headers.  This is
4080ed0d50c3Schristos   // effectively what the GNU linker does, and it is slightly more
4081ed0d50c3Schristos   // efficient in any case.  We try to use the first PT_LOAD segment
4082ed0d50c3Schristos   // if we can, otherwise we make a new one.
4083ed0d50c3Schristos 
4084ed0d50c3Schristos   if (first_seg == NULL)
4085ed0d50c3Schristos     return NULL;
4086ed0d50c3Schristos 
4087ed0d50c3Schristos   // -n or -N mean that the program is not demand paged and there is
4088ed0d50c3Schristos   // no need to put the program headers in a PT_LOAD segment.
4089ed0d50c3Schristos   if (parameters->options().nmagic() || parameters->options().omagic())
4090ed0d50c3Schristos     return NULL;
4091ed0d50c3Schristos 
4092ed0d50c3Schristos   size_t sizeof_headers = this->total_header_size(layout);
4093ed0d50c3Schristos 
4094ed0d50c3Schristos   uint64_t vma = first_seg->vaddr();
4095ed0d50c3Schristos   uint64_t lma = first_seg->paddr();
4096ed0d50c3Schristos 
4097ed0d50c3Schristos   uint64_t subtract = this->header_size_adjustment(lma, sizeof_headers);
4098ed0d50c3Schristos 
4099ed0d50c3Schristos   if ((lma & (abi_pagesize - 1)) >= sizeof_headers)
4100ed0d50c3Schristos     {
4101ed0d50c3Schristos       first_seg->set_addresses(vma - subtract, lma - subtract);
4102ed0d50c3Schristos       return first_seg;
4103ed0d50c3Schristos     }
4104ed0d50c3Schristos 
4105ed0d50c3Schristos   // If there is no room to squeeze in the headers, then punt.  The
4106ed0d50c3Schristos   // resulting executable probably won't run on GNU/Linux, but we
4107ed0d50c3Schristos   // trust that the user knows what they are doing.
4108ed0d50c3Schristos   if (lma < subtract || vma < subtract)
4109ed0d50c3Schristos     return NULL;
4110ed0d50c3Schristos 
4111ed0d50c3Schristos   // If memory regions have been specified and the address range
4112ed0d50c3Schristos   // we are about to use is not contained within any region then
4113ed0d50c3Schristos   // issue a warning message about the segment we are going to
4114ed0d50c3Schristos   // create.  It will be outside of any region and so possibly
4115ed0d50c3Schristos   // using non-existent or protected memory.  We test LMA rather
4116ed0d50c3Schristos   // than VMA since we assume that the headers will never be
4117ed0d50c3Schristos   // relocated.
4118ed0d50c3Schristos   if (this->memory_regions_ != NULL
4119ed0d50c3Schristos       && !this->block_in_region (NULL, layout, lma - subtract, subtract))
4120ed0d50c3Schristos     gold_warning(_("creating a segment to contain the file and program"
4121ed0d50c3Schristos 		   " headers outside of any MEMORY region"));
4122ed0d50c3Schristos 
4123ed0d50c3Schristos   Output_segment* load_seg = layout->make_output_segment(elfcpp::PT_LOAD,
4124ed0d50c3Schristos 							 elfcpp::PF_R);
4125ed0d50c3Schristos   load_seg->set_addresses(vma - subtract, lma - subtract);
4126ed0d50c3Schristos 
4127ed0d50c3Schristos   return load_seg;
4128ed0d50c3Schristos }
4129ed0d50c3Schristos 
4130ed0d50c3Schristos // Create a PT_NOTE segment for each SHT_NOTE section and a PT_TLS
4131ed0d50c3Schristos // segment if there are any SHT_TLS sections.
4132ed0d50c3Schristos 
4133ed0d50c3Schristos void
create_note_and_tls_segments(Layout * layout,const Layout::Section_list * sections)4134ed0d50c3Schristos Script_sections::create_note_and_tls_segments(
4135ed0d50c3Schristos     Layout* layout,
4136ed0d50c3Schristos     const Layout::Section_list* sections)
4137ed0d50c3Schristos {
4138ed0d50c3Schristos   gold_assert(!this->saw_phdrs_clause());
4139ed0d50c3Schristos 
4140ed0d50c3Schristos   bool saw_tls = false;
4141ed0d50c3Schristos   for (Layout::Section_list::const_iterator p = sections->begin();
4142ed0d50c3Schristos        p != sections->end();
4143ed0d50c3Schristos        ++p)
4144ed0d50c3Schristos     {
4145ed0d50c3Schristos       if ((*p)->type() == elfcpp::SHT_NOTE)
4146ed0d50c3Schristos 	{
4147ed0d50c3Schristos 	  elfcpp::Elf_Word seg_flags =
4148ed0d50c3Schristos 	    Layout::section_flags_to_segment((*p)->flags());
4149ed0d50c3Schristos 	  Output_segment* oseg = layout->make_output_segment(elfcpp::PT_NOTE,
4150ed0d50c3Schristos 							     seg_flags);
4151ed0d50c3Schristos 	  oseg->add_output_section_to_nonload(*p, seg_flags);
4152ed0d50c3Schristos 
4153ed0d50c3Schristos 	  // Incorporate any subsequent SHT_NOTE sections, in the
4154ed0d50c3Schristos 	  // hopes that the script is sensible.
4155ed0d50c3Schristos 	  Layout::Section_list::const_iterator pnext = p + 1;
4156ed0d50c3Schristos 	  while (pnext != sections->end()
4157ed0d50c3Schristos 		 && (*pnext)->type() == elfcpp::SHT_NOTE)
4158ed0d50c3Schristos 	    {
4159ed0d50c3Schristos 	      seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
4160ed0d50c3Schristos 	      oseg->add_output_section_to_nonload(*pnext, seg_flags);
4161ed0d50c3Schristos 	      p = pnext;
4162ed0d50c3Schristos 	      ++pnext;
4163ed0d50c3Schristos 	    }
4164ed0d50c3Schristos 	}
4165ed0d50c3Schristos 
4166ed0d50c3Schristos       if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
4167ed0d50c3Schristos 	{
4168ed0d50c3Schristos 	  if (saw_tls)
4169ed0d50c3Schristos 	    gold_error(_("TLS sections are not adjacent"));
4170ed0d50c3Schristos 
4171ed0d50c3Schristos 	  elfcpp::Elf_Word seg_flags =
4172ed0d50c3Schristos 	    Layout::section_flags_to_segment((*p)->flags());
4173ed0d50c3Schristos 	  Output_segment* oseg = layout->make_output_segment(elfcpp::PT_TLS,
4174ed0d50c3Schristos 							     seg_flags);
4175ed0d50c3Schristos 	  oseg->add_output_section_to_nonload(*p, seg_flags);
4176ed0d50c3Schristos 
4177ed0d50c3Schristos 	  Layout::Section_list::const_iterator pnext = p + 1;
4178ed0d50c3Schristos 	  while (pnext != sections->end()
4179ed0d50c3Schristos 		 && ((*pnext)->flags() & elfcpp::SHF_TLS) != 0)
4180ed0d50c3Schristos 	    {
4181ed0d50c3Schristos 	      seg_flags = Layout::section_flags_to_segment((*pnext)->flags());
4182ed0d50c3Schristos 	      oseg->add_output_section_to_nonload(*pnext, seg_flags);
4183ed0d50c3Schristos 	      p = pnext;
4184ed0d50c3Schristos 	      ++pnext;
4185ed0d50c3Schristos 	    }
4186ed0d50c3Schristos 
4187ed0d50c3Schristos 	  saw_tls = true;
4188ed0d50c3Schristos 	}
4189ed0d50c3Schristos 
4190ed0d50c3Schristos       // If we see a section named .interp then put the .interp section
4191ed0d50c3Schristos       // in a PT_INTERP segment.
4192ed0d50c3Schristos       // This is for GNU ld compatibility.
4193ed0d50c3Schristos       if (strcmp((*p)->name(), ".interp") == 0)
4194ed0d50c3Schristos 	{
4195ed0d50c3Schristos 	  elfcpp::Elf_Word seg_flags =
4196ed0d50c3Schristos 	    Layout::section_flags_to_segment((*p)->flags());
4197ed0d50c3Schristos 	  Output_segment* oseg = layout->make_output_segment(elfcpp::PT_INTERP,
4198ed0d50c3Schristos 							     seg_flags);
4199ed0d50c3Schristos 	  oseg->add_output_section_to_nonload(*p, seg_flags);
4200ed0d50c3Schristos 	}
4201ed0d50c3Schristos     }
4202ed0d50c3Schristos 
4203ed0d50c3Schristos     this->segments_created_ = true;
4204ed0d50c3Schristos }
4205ed0d50c3Schristos 
4206ed0d50c3Schristos // Add a program header.  The PHDRS clause is syntactically distinct
4207ed0d50c3Schristos // from the SECTIONS clause, but we implement it with the SECTIONS
4208ed0d50c3Schristos // support because PHDRS is useless if there is no SECTIONS clause.
4209ed0d50c3Schristos 
4210ed0d50c3Schristos void
add_phdr(const char * name,size_t namelen,unsigned int type,bool includes_filehdr,bool includes_phdrs,bool is_flags_valid,unsigned int flags,Expression * load_address)4211ed0d50c3Schristos Script_sections::add_phdr(const char* name, size_t namelen, unsigned int type,
4212ed0d50c3Schristos 			  bool includes_filehdr, bool includes_phdrs,
4213ed0d50c3Schristos 			  bool is_flags_valid, unsigned int flags,
4214ed0d50c3Schristos 			  Expression* load_address)
4215ed0d50c3Schristos {
4216ed0d50c3Schristos   if (this->phdrs_elements_ == NULL)
4217ed0d50c3Schristos     this->phdrs_elements_ = new Phdrs_elements();
4218ed0d50c3Schristos   this->phdrs_elements_->push_back(new Phdrs_element(name, namelen, type,
4219ed0d50c3Schristos 						     includes_filehdr,
4220ed0d50c3Schristos 						     includes_phdrs,
4221ed0d50c3Schristos 						     is_flags_valid, flags,
4222ed0d50c3Schristos 						     load_address));
4223ed0d50c3Schristos }
4224ed0d50c3Schristos 
4225ed0d50c3Schristos // Return the number of segments we expect to create based on the
4226ed0d50c3Schristos // SECTIONS clause.  This is used to implement SIZEOF_HEADERS.
4227ed0d50c3Schristos 
4228ed0d50c3Schristos size_t
expected_segment_count(const Layout * layout) const4229ed0d50c3Schristos Script_sections::expected_segment_count(const Layout* layout) const
4230ed0d50c3Schristos {
4231ed0d50c3Schristos   // If we've already created the segments, we won't be adding any more.
4232ed0d50c3Schristos   if (this->segments_created_)
4233ed0d50c3Schristos     return 0;
4234ed0d50c3Schristos 
4235ed0d50c3Schristos   if (this->saw_phdrs_clause())
4236ed0d50c3Schristos     return this->phdrs_elements_->size();
4237ed0d50c3Schristos 
4238ed0d50c3Schristos   Layout::Section_list sections;
4239ed0d50c3Schristos   layout->get_allocated_sections(&sections);
4240ed0d50c3Schristos 
4241ed0d50c3Schristos   // We assume that we will need two PT_LOAD segments.
4242ed0d50c3Schristos   size_t ret = 2;
4243ed0d50c3Schristos 
4244ed0d50c3Schristos   bool saw_note = false;
4245ed0d50c3Schristos   bool saw_tls = false;
4246ed0d50c3Schristos   bool saw_interp = false;
4247ed0d50c3Schristos   for (Layout::Section_list::const_iterator p = sections.begin();
4248ed0d50c3Schristos        p != sections.end();
4249ed0d50c3Schristos        ++p)
4250ed0d50c3Schristos     {
4251ed0d50c3Schristos       if ((*p)->type() == elfcpp::SHT_NOTE)
4252ed0d50c3Schristos 	{
4253ed0d50c3Schristos 	  // Assume that all note sections will fit into a single
4254ed0d50c3Schristos 	  // PT_NOTE segment.
4255ed0d50c3Schristos 	  if (!saw_note)
4256ed0d50c3Schristos 	    {
4257ed0d50c3Schristos 	      ++ret;
4258ed0d50c3Schristos 	      saw_note = true;
4259ed0d50c3Schristos 	    }
4260ed0d50c3Schristos 	}
4261ed0d50c3Schristos       else if (((*p)->flags() & elfcpp::SHF_TLS) != 0)
4262ed0d50c3Schristos 	{
4263ed0d50c3Schristos 	  // There can only be one PT_TLS segment.
4264ed0d50c3Schristos 	  if (!saw_tls)
4265ed0d50c3Schristos 	    {
4266ed0d50c3Schristos 	      ++ret;
4267ed0d50c3Schristos 	      saw_tls = true;
4268ed0d50c3Schristos 	    }
4269ed0d50c3Schristos 	}
4270ed0d50c3Schristos       else if (strcmp((*p)->name(), ".interp") == 0)
4271ed0d50c3Schristos 	{
4272ed0d50c3Schristos 	  // There can only be one PT_INTERP segment.
4273ed0d50c3Schristos 	  if (!saw_interp)
4274ed0d50c3Schristos 	    {
4275ed0d50c3Schristos 	      ++ret;
4276ed0d50c3Schristos 	      saw_interp = true;
4277ed0d50c3Schristos 	    }
4278ed0d50c3Schristos 	}
4279ed0d50c3Schristos     }
4280ed0d50c3Schristos 
4281ed0d50c3Schristos   return ret;
4282ed0d50c3Schristos }
4283ed0d50c3Schristos 
4284ed0d50c3Schristos // Create the segments from a PHDRS clause.  Return the segment which
4285ed0d50c3Schristos // should hold the file header and program headers, if any.
4286ed0d50c3Schristos 
4287ed0d50c3Schristos Output_segment*
create_segments_from_phdrs_clause(Layout * layout,uint64_t dot_alignment)4288ed0d50c3Schristos Script_sections::create_segments_from_phdrs_clause(Layout* layout,
4289ed0d50c3Schristos 						   uint64_t dot_alignment)
4290ed0d50c3Schristos {
4291ed0d50c3Schristos   this->attach_sections_using_phdrs_clause(layout);
4292ed0d50c3Schristos   return this->set_phdrs_clause_addresses(layout, dot_alignment);
4293ed0d50c3Schristos }
4294ed0d50c3Schristos 
4295ed0d50c3Schristos // Create the segments from the PHDRS clause, and put the output
4296ed0d50c3Schristos // sections in them.
4297ed0d50c3Schristos 
4298ed0d50c3Schristos void
attach_sections_using_phdrs_clause(Layout * layout)4299ed0d50c3Schristos Script_sections::attach_sections_using_phdrs_clause(Layout* layout)
4300ed0d50c3Schristos {
4301ed0d50c3Schristos   typedef std::map<std::string, Output_segment*> Name_to_segment;
4302ed0d50c3Schristos   Name_to_segment name_to_segment;
4303ed0d50c3Schristos   for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4304ed0d50c3Schristos        p != this->phdrs_elements_->end();
4305ed0d50c3Schristos        ++p)
4306ed0d50c3Schristos     name_to_segment[(*p)->name()] = (*p)->create_segment(layout);
4307ed0d50c3Schristos   this->segments_created_ = true;
4308ed0d50c3Schristos 
4309ed0d50c3Schristos   // Walk through the output sections and attach them to segments.
4310ed0d50c3Schristos   // Output sections in the script which do not list segments are
4311ed0d50c3Schristos   // attached to the same set of segments as the immediately preceding
4312ed0d50c3Schristos   // output section.
4313ed0d50c3Schristos 
4314ed0d50c3Schristos   String_list* phdr_names = NULL;
4315ed0d50c3Schristos   bool load_segments_only = false;
4316ed0d50c3Schristos   for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4317ed0d50c3Schristos        p != this->sections_elements_->end();
4318ed0d50c3Schristos        ++p)
4319ed0d50c3Schristos     {
4320ed0d50c3Schristos       bool is_orphan;
4321ed0d50c3Schristos       String_list* old_phdr_names = phdr_names;
4322ed0d50c3Schristos       Output_section* os = (*p)->allocate_to_segment(&phdr_names, &is_orphan);
4323ed0d50c3Schristos       if (os == NULL)
4324ed0d50c3Schristos 	continue;
4325ed0d50c3Schristos 
4326ed0d50c3Schristos       elfcpp::Elf_Word seg_flags =
4327ed0d50c3Schristos 	Layout::section_flags_to_segment(os->flags());
4328ed0d50c3Schristos 
4329ed0d50c3Schristos       if (phdr_names == NULL)
4330ed0d50c3Schristos 	{
4331ed0d50c3Schristos 	  // Don't worry about empty orphan sections.
4332ed0d50c3Schristos 	  if (is_orphan && os->current_data_size() > 0)
4333ed0d50c3Schristos 	    gold_error(_("allocated section %s not in any segment"),
4334ed0d50c3Schristos 		       os->name());
4335ed0d50c3Schristos 
4336ed0d50c3Schristos 	  // To avoid later crashes drop this section into the first
4337ed0d50c3Schristos 	  // PT_LOAD segment.
4338ed0d50c3Schristos 	  for (Phdrs_elements::const_iterator ppe =
4339ed0d50c3Schristos 		 this->phdrs_elements_->begin();
4340ed0d50c3Schristos 	       ppe != this->phdrs_elements_->end();
4341ed0d50c3Schristos 	       ++ppe)
4342ed0d50c3Schristos 	    {
4343ed0d50c3Schristos 	      Output_segment* oseg = (*ppe)->segment();
4344ed0d50c3Schristos 	      if (oseg->type() == elfcpp::PT_LOAD)
4345ed0d50c3Schristos 		{
4346ed0d50c3Schristos 		  oseg->add_output_section_to_load(layout, os, seg_flags);
4347ed0d50c3Schristos 		  break;
4348ed0d50c3Schristos 		}
4349ed0d50c3Schristos 	    }
4350ed0d50c3Schristos 
4351ed0d50c3Schristos 	  continue;
4352ed0d50c3Schristos 	}
4353ed0d50c3Schristos 
4354ed0d50c3Schristos       // We see a list of segments names.  Disable PT_LOAD segment only
4355ed0d50c3Schristos       // filtering.
4356ed0d50c3Schristos       if (old_phdr_names != phdr_names)
4357ed0d50c3Schristos 	load_segments_only = false;
4358ed0d50c3Schristos 
4359ed0d50c3Schristos       // If this is an orphan section--one that was not explicitly
4360ed0d50c3Schristos       // mentioned in the linker script--then it should not inherit
4361ed0d50c3Schristos       // any segment type other than PT_LOAD.  Otherwise, e.g., the
4362ed0d50c3Schristos       // PT_INTERP segment will pick up following orphan sections,
4363ed0d50c3Schristos       // which does not make sense.  If this is not an orphan section,
4364ed0d50c3Schristos       // we trust the linker script.
4365ed0d50c3Schristos       if (is_orphan)
4366ed0d50c3Schristos 	{
4367ed0d50c3Schristos 	  // Enable PT_LOAD segments only filtering until we see another
4368ed0d50c3Schristos 	  // list of segment names.
4369ed0d50c3Schristos 	  load_segments_only = true;
4370ed0d50c3Schristos 	}
4371ed0d50c3Schristos 
4372ed0d50c3Schristos       bool in_load_segment = false;
4373ed0d50c3Schristos       for (String_list::const_iterator q = phdr_names->begin();
4374ed0d50c3Schristos 	   q != phdr_names->end();
4375ed0d50c3Schristos 	   ++q)
4376ed0d50c3Schristos 	{
4377ed0d50c3Schristos 	  Name_to_segment::const_iterator r = name_to_segment.find(*q);
4378ed0d50c3Schristos 	  if (r == name_to_segment.end())
4379ed0d50c3Schristos 	    gold_error(_("no segment %s"), q->c_str());
4380ed0d50c3Schristos 	  else
4381ed0d50c3Schristos 	    {
4382ed0d50c3Schristos 	      if (load_segments_only
4383ed0d50c3Schristos 		  && r->second->type() != elfcpp::PT_LOAD)
4384ed0d50c3Schristos 		continue;
4385ed0d50c3Schristos 
4386ed0d50c3Schristos 	      if (r->second->type() != elfcpp::PT_LOAD)
4387ed0d50c3Schristos 		r->second->add_output_section_to_nonload(os, seg_flags);
4388ed0d50c3Schristos 	      else
4389ed0d50c3Schristos 		{
4390ed0d50c3Schristos 		  r->second->add_output_section_to_load(layout, os, seg_flags);
4391ed0d50c3Schristos 		  if (in_load_segment)
4392ed0d50c3Schristos 		    gold_error(_("section in two PT_LOAD segments"));
4393ed0d50c3Schristos 		  in_load_segment = true;
4394ed0d50c3Schristos 		}
4395ed0d50c3Schristos 	    }
4396ed0d50c3Schristos 	}
4397ed0d50c3Schristos 
4398ed0d50c3Schristos       if (!in_load_segment)
4399ed0d50c3Schristos 	gold_error(_("allocated section not in any PT_LOAD segment"));
4400ed0d50c3Schristos     }
4401ed0d50c3Schristos }
4402ed0d50c3Schristos 
4403ed0d50c3Schristos // Set the addresses for segments created from a PHDRS clause.  Return
4404ed0d50c3Schristos // the segment which should hold the file header and program headers,
4405ed0d50c3Schristos // if any.
4406ed0d50c3Schristos 
4407ed0d50c3Schristos Output_segment*
set_phdrs_clause_addresses(Layout * layout,uint64_t dot_alignment)4408ed0d50c3Schristos Script_sections::set_phdrs_clause_addresses(Layout* layout,
4409ed0d50c3Schristos 					    uint64_t dot_alignment)
4410ed0d50c3Schristos {
4411ed0d50c3Schristos   Output_segment* load_seg = NULL;
4412ed0d50c3Schristos   for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4413ed0d50c3Schristos        p != this->phdrs_elements_->end();
4414ed0d50c3Schristos        ++p)
4415ed0d50c3Schristos     {
4416ed0d50c3Schristos       // Note that we have to set the flags after adding the output
4417ed0d50c3Schristos       // sections to the segment, as adding an output segment can
4418ed0d50c3Schristos       // change the flags.
4419ed0d50c3Schristos       (*p)->set_flags_if_valid();
4420ed0d50c3Schristos 
4421ed0d50c3Schristos       Output_segment* oseg = (*p)->segment();
4422ed0d50c3Schristos 
4423ed0d50c3Schristos       if (oseg->type() != elfcpp::PT_LOAD)
4424ed0d50c3Schristos 	{
4425ed0d50c3Schristos 	  // The addresses of non-PT_LOAD segments are set from the
4426ed0d50c3Schristos 	  // PT_LOAD segments.
4427ed0d50c3Schristos 	  if ((*p)->has_load_address())
4428ed0d50c3Schristos 	    gold_error(_("may only specify load address for PT_LOAD segment"));
4429ed0d50c3Schristos 	  continue;
4430ed0d50c3Schristos 	}
4431ed0d50c3Schristos 
4432ed0d50c3Schristos       oseg->set_minimum_p_align(dot_alignment);
4433ed0d50c3Schristos 
4434ed0d50c3Schristos       // The output sections should have addresses from the SECTIONS
4435ed0d50c3Schristos       // clause.  The addresses don't have to be in order, so find the
4436ed0d50c3Schristos       // one with the lowest load address.  Use that to set the
4437ed0d50c3Schristos       // address of the segment.
4438ed0d50c3Schristos 
4439ed0d50c3Schristos       Output_section* osec = oseg->section_with_lowest_load_address();
4440ed0d50c3Schristos       if (osec == NULL)
4441ed0d50c3Schristos 	{
4442ed0d50c3Schristos 	  oseg->set_addresses(0, 0);
4443ed0d50c3Schristos 	  continue;
4444ed0d50c3Schristos 	}
4445ed0d50c3Schristos 
4446ed0d50c3Schristos       uint64_t vma = osec->address();
4447ed0d50c3Schristos       uint64_t lma = osec->has_load_address() ? osec->load_address() : vma;
4448ed0d50c3Schristos 
4449ed0d50c3Schristos       // Override the load address of the section with the load
4450ed0d50c3Schristos       // address specified for the segment.
4451ed0d50c3Schristos       if ((*p)->has_load_address())
4452ed0d50c3Schristos 	{
4453ed0d50c3Schristos 	  if (osec->has_load_address())
4454ed0d50c3Schristos 	    gold_warning(_("PHDRS load address overrides "
4455ed0d50c3Schristos 			   "section %s load address"),
4456ed0d50c3Schristos 			 osec->name());
4457ed0d50c3Schristos 
4458ed0d50c3Schristos 	  lma = (*p)->load_address();
4459ed0d50c3Schristos 	}
4460ed0d50c3Schristos 
4461ed0d50c3Schristos       bool headers = (*p)->includes_filehdr() && (*p)->includes_phdrs();
4462ed0d50c3Schristos       if (!headers && ((*p)->includes_filehdr() || (*p)->includes_phdrs()))
4463ed0d50c3Schristos 	{
4464ed0d50c3Schristos 	  // We could support this if we wanted to.
4465ed0d50c3Schristos 	  gold_error(_("using only one of FILEHDR and PHDRS is "
4466ed0d50c3Schristos 		       "not currently supported"));
4467ed0d50c3Schristos 	}
4468ed0d50c3Schristos       if (headers)
4469ed0d50c3Schristos 	{
4470ed0d50c3Schristos 	  size_t sizeof_headers = this->total_header_size(layout);
4471ed0d50c3Schristos 	  uint64_t subtract = this->header_size_adjustment(lma,
4472ed0d50c3Schristos 							   sizeof_headers);
4473ed0d50c3Schristos 	  if (lma >= subtract && vma >= subtract)
4474ed0d50c3Schristos 	    {
4475ed0d50c3Schristos 	      lma -= subtract;
4476ed0d50c3Schristos 	      vma -= subtract;
4477ed0d50c3Schristos 	    }
4478ed0d50c3Schristos 	  else
4479ed0d50c3Schristos 	    {
4480ed0d50c3Schristos 	      gold_error(_("sections loaded on first page without room "
4481ed0d50c3Schristos 			   "for file and program headers "
4482ed0d50c3Schristos 			   "are not supported"));
4483ed0d50c3Schristos 	    }
4484ed0d50c3Schristos 
4485ed0d50c3Schristos 	  if (load_seg != NULL)
4486ed0d50c3Schristos 	    gold_error(_("using FILEHDR and PHDRS on more than one "
4487ed0d50c3Schristos 			 "PT_LOAD segment is not currently supported"));
4488ed0d50c3Schristos 	  load_seg = oseg;
4489ed0d50c3Schristos 	}
4490ed0d50c3Schristos 
4491ed0d50c3Schristos       oseg->set_addresses(vma, lma);
4492ed0d50c3Schristos     }
4493ed0d50c3Schristos 
4494ed0d50c3Schristos   return load_seg;
4495ed0d50c3Schristos }
4496ed0d50c3Schristos 
4497ed0d50c3Schristos // Add the file header and segment headers to non-load segments
4498ed0d50c3Schristos // specified in the PHDRS clause.
4499ed0d50c3Schristos 
4500ed0d50c3Schristos void
put_headers_in_phdrs(Output_data * file_header,Output_data * segment_headers)4501ed0d50c3Schristos Script_sections::put_headers_in_phdrs(Output_data* file_header,
4502ed0d50c3Schristos 				      Output_data* segment_headers)
4503ed0d50c3Schristos {
4504ed0d50c3Schristos   gold_assert(this->saw_phdrs_clause());
4505ed0d50c3Schristos   for (Phdrs_elements::iterator p = this->phdrs_elements_->begin();
4506ed0d50c3Schristos        p != this->phdrs_elements_->end();
4507ed0d50c3Schristos        ++p)
4508ed0d50c3Schristos     {
4509ed0d50c3Schristos       if ((*p)->type() != elfcpp::PT_LOAD)
4510ed0d50c3Schristos 	{
4511ed0d50c3Schristos 	  if ((*p)->includes_phdrs())
4512ed0d50c3Schristos 	    (*p)->segment()->add_initial_output_data(segment_headers);
4513ed0d50c3Schristos 	  if ((*p)->includes_filehdr())
4514ed0d50c3Schristos 	    (*p)->segment()->add_initial_output_data(file_header);
4515ed0d50c3Schristos 	}
4516ed0d50c3Schristos     }
4517ed0d50c3Schristos }
4518ed0d50c3Schristos 
4519ed0d50c3Schristos // Look for an output section by name and return the address, the load
4520ed0d50c3Schristos // address, the alignment, and the size.  This is used when an
4521ed0d50c3Schristos // expression refers to an output section which was not actually
4522ed0d50c3Schristos // created.  This returns true if the section was found, false
4523ed0d50c3Schristos // otherwise.
4524ed0d50c3Schristos 
4525ed0d50c3Schristos bool
get_output_section_info(const char * name,uint64_t * address,uint64_t * load_address,uint64_t * addralign,uint64_t * size) const4526ed0d50c3Schristos Script_sections::get_output_section_info(const char* name, uint64_t* address,
4527ed0d50c3Schristos                                          uint64_t* load_address,
4528ed0d50c3Schristos                                          uint64_t* addralign,
4529ed0d50c3Schristos                                          uint64_t* size) const
4530ed0d50c3Schristos {
4531ed0d50c3Schristos   if (!this->saw_sections_clause_)
4532ed0d50c3Schristos     return false;
4533ed0d50c3Schristos   for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4534ed0d50c3Schristos        p != this->sections_elements_->end();
4535ed0d50c3Schristos        ++p)
4536ed0d50c3Schristos     if ((*p)->get_output_section_info(name, address, load_address, addralign,
4537ed0d50c3Schristos                                       size))
4538ed0d50c3Schristos       return true;
4539ed0d50c3Schristos   return false;
4540ed0d50c3Schristos }
4541ed0d50c3Schristos 
4542ed0d50c3Schristos // Release all Output_segments.  This remove all pointers to all
4543ed0d50c3Schristos // Output_segments.
4544ed0d50c3Schristos 
4545ed0d50c3Schristos void
release_segments()4546ed0d50c3Schristos Script_sections::release_segments()
4547ed0d50c3Schristos {
4548ed0d50c3Schristos   if (this->saw_phdrs_clause())
4549ed0d50c3Schristos     {
4550ed0d50c3Schristos       for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4551ed0d50c3Schristos 	   p != this->phdrs_elements_->end();
4552ed0d50c3Schristos 	   ++p)
4553ed0d50c3Schristos 	(*p)->release_segment();
4554ed0d50c3Schristos     }
455506324dcfSchristos   this->segments_created_ = false;
4556ed0d50c3Schristos }
4557ed0d50c3Schristos 
4558ed0d50c3Schristos // Print the SECTIONS clause to F for debugging.
4559ed0d50c3Schristos 
4560ed0d50c3Schristos void
print(FILE * f) const4561ed0d50c3Schristos Script_sections::print(FILE* f) const
4562ed0d50c3Schristos {
4563ed0d50c3Schristos   if (this->phdrs_elements_ != NULL)
4564ed0d50c3Schristos     {
4565ed0d50c3Schristos       fprintf(f, "PHDRS {\n");
4566ed0d50c3Schristos       for (Phdrs_elements::const_iterator p = this->phdrs_elements_->begin();
4567ed0d50c3Schristos 	   p != this->phdrs_elements_->end();
4568ed0d50c3Schristos 	   ++p)
4569ed0d50c3Schristos 	(*p)->print(f);
4570ed0d50c3Schristos       fprintf(f, "}\n");
4571ed0d50c3Schristos     }
4572ed0d50c3Schristos 
4573ed0d50c3Schristos   if (this->memory_regions_ != NULL)
4574ed0d50c3Schristos     {
4575ed0d50c3Schristos       fprintf(f, "MEMORY {\n");
4576ed0d50c3Schristos       for (Memory_regions::const_iterator m = this->memory_regions_->begin();
4577ed0d50c3Schristos 	   m != this->memory_regions_->end();
4578ed0d50c3Schristos 	   ++m)
4579ed0d50c3Schristos 	(*m)->print(f);
4580ed0d50c3Schristos       fprintf(f, "}\n");
4581ed0d50c3Schristos     }
4582ed0d50c3Schristos 
4583ed0d50c3Schristos   if (!this->saw_sections_clause_)
4584ed0d50c3Schristos     return;
4585ed0d50c3Schristos 
4586ed0d50c3Schristos   fprintf(f, "SECTIONS {\n");
4587ed0d50c3Schristos 
4588ed0d50c3Schristos   for (Sections_elements::const_iterator p = this->sections_elements_->begin();
4589ed0d50c3Schristos        p != this->sections_elements_->end();
4590ed0d50c3Schristos        ++p)
4591ed0d50c3Schristos     (*p)->print(f);
4592ed0d50c3Schristos 
4593ed0d50c3Schristos   fprintf(f, "}\n");
4594ed0d50c3Schristos }
4595ed0d50c3Schristos 
4596ed0d50c3Schristos } // End namespace gold.
4597