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