1*a9fa9459Szrj // layout.h -- lay out output file sections for gold -*- C++ -*-
2*a9fa9459Szrj
3*a9fa9459Szrj // Copyright (C) 2006-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 #ifndef GOLD_LAYOUT_H
24*a9fa9459Szrj #define GOLD_LAYOUT_H
25*a9fa9459Szrj
26*a9fa9459Szrj #include <cstring>
27*a9fa9459Szrj #include <list>
28*a9fa9459Szrj #include <map>
29*a9fa9459Szrj #include <string>
30*a9fa9459Szrj #include <utility>
31*a9fa9459Szrj #include <vector>
32*a9fa9459Szrj
33*a9fa9459Szrj #include "script.h"
34*a9fa9459Szrj #include "workqueue.h"
35*a9fa9459Szrj #include "object.h"
36*a9fa9459Szrj #include "dynobj.h"
37*a9fa9459Szrj #include "stringpool.h"
38*a9fa9459Szrj
39*a9fa9459Szrj namespace gold
40*a9fa9459Szrj {
41*a9fa9459Szrj
42*a9fa9459Szrj class General_options;
43*a9fa9459Szrj class Incremental_inputs;
44*a9fa9459Szrj class Incremental_binary;
45*a9fa9459Szrj class Input_objects;
46*a9fa9459Szrj class Mapfile;
47*a9fa9459Szrj class Symbol_table;
48*a9fa9459Szrj class Output_section_data;
49*a9fa9459Szrj class Output_section;
50*a9fa9459Szrj class Output_section_headers;
51*a9fa9459Szrj class Output_segment_headers;
52*a9fa9459Szrj class Output_file_header;
53*a9fa9459Szrj class Output_segment;
54*a9fa9459Szrj class Output_data;
55*a9fa9459Szrj class Output_data_reloc_generic;
56*a9fa9459Szrj class Output_data_dynamic;
57*a9fa9459Szrj class Output_symtab_xindex;
58*a9fa9459Szrj class Output_reduced_debug_abbrev_section;
59*a9fa9459Szrj class Output_reduced_debug_info_section;
60*a9fa9459Szrj class Eh_frame;
61*a9fa9459Szrj class Gdb_index;
62*a9fa9459Szrj class Target;
63*a9fa9459Szrj struct Timespec;
64*a9fa9459Szrj
65*a9fa9459Szrj // Return TRUE if SECNAME is the name of a compressed debug section.
66*a9fa9459Szrj extern bool
67*a9fa9459Szrj is_compressed_debug_section(const char* secname);
68*a9fa9459Szrj
69*a9fa9459Szrj // Return the name of the corresponding uncompressed debug section.
70*a9fa9459Szrj extern std::string
71*a9fa9459Szrj corresponding_uncompressed_section_name(std::string secname);
72*a9fa9459Szrj
73*a9fa9459Szrj // Maintain a list of free space within a section, segment, or file.
74*a9fa9459Szrj // Used for incremental update links.
75*a9fa9459Szrj
76*a9fa9459Szrj class Free_list
77*a9fa9459Szrj {
78*a9fa9459Szrj public:
79*a9fa9459Szrj struct Free_list_node
80*a9fa9459Szrj {
Free_list_nodeFree_list_node81*a9fa9459Szrj Free_list_node(off_t start, off_t end)
82*a9fa9459Szrj : start_(start), end_(end)
83*a9fa9459Szrj { }
84*a9fa9459Szrj off_t start_;
85*a9fa9459Szrj off_t end_;
86*a9fa9459Szrj };
87*a9fa9459Szrj typedef std::list<Free_list_node>::const_iterator Const_iterator;
88*a9fa9459Szrj
Free_list()89*a9fa9459Szrj Free_list()
90*a9fa9459Szrj : list_(), last_remove_(list_.begin()), extend_(false), length_(0),
91*a9fa9459Szrj min_hole_(0)
92*a9fa9459Szrj { }
93*a9fa9459Szrj
94*a9fa9459Szrj // Initialize the free list for a section of length LEN.
95*a9fa9459Szrj // If EXTEND is true, free space may be allocated past the end.
96*a9fa9459Szrj void
97*a9fa9459Szrj init(off_t len, bool extend);
98*a9fa9459Szrj
99*a9fa9459Szrj // Set the minimum hole size that is allowed when allocating
100*a9fa9459Szrj // from the free list.
101*a9fa9459Szrj void
set_min_hole_size(off_t min_hole)102*a9fa9459Szrj set_min_hole_size(off_t min_hole)
103*a9fa9459Szrj { this->min_hole_ = min_hole; }
104*a9fa9459Szrj
105*a9fa9459Szrj // Remove a chunk from the free list.
106*a9fa9459Szrj void
107*a9fa9459Szrj remove(off_t start, off_t end);
108*a9fa9459Szrj
109*a9fa9459Szrj // Allocate a chunk of space from the free list of length LEN,
110*a9fa9459Szrj // with alignment ALIGN, and minimum offset MINOFF.
111*a9fa9459Szrj off_t
112*a9fa9459Szrj allocate(off_t len, uint64_t align, off_t minoff);
113*a9fa9459Szrj
114*a9fa9459Szrj // Return an iterator for the beginning of the free list.
115*a9fa9459Szrj Const_iterator
begin()116*a9fa9459Szrj begin() const
117*a9fa9459Szrj { return this->list_.begin(); }
118*a9fa9459Szrj
119*a9fa9459Szrj // Return an iterator for the end of the free list.
120*a9fa9459Szrj Const_iterator
end()121*a9fa9459Szrj end() const
122*a9fa9459Szrj { return this->list_.end(); }
123*a9fa9459Szrj
124*a9fa9459Szrj // Dump the free list (for debugging).
125*a9fa9459Szrj void
126*a9fa9459Szrj dump();
127*a9fa9459Szrj
128*a9fa9459Szrj // Print usage statistics.
129*a9fa9459Szrj static void
130*a9fa9459Szrj print_stats();
131*a9fa9459Szrj
132*a9fa9459Szrj private:
133*a9fa9459Szrj typedef std::list<Free_list_node>::iterator Iterator;
134*a9fa9459Szrj
135*a9fa9459Szrj // The free list.
136*a9fa9459Szrj std::list<Free_list_node> list_;
137*a9fa9459Szrj
138*a9fa9459Szrj // The last node visited during a remove operation.
139*a9fa9459Szrj Iterator last_remove_;
140*a9fa9459Szrj
141*a9fa9459Szrj // Whether we can extend past the original length.
142*a9fa9459Szrj bool extend_;
143*a9fa9459Szrj
144*a9fa9459Szrj // The total length of the section, segment, or file.
145*a9fa9459Szrj off_t length_;
146*a9fa9459Szrj
147*a9fa9459Szrj // The minimum hole size allowed. When allocating from the free list,
148*a9fa9459Szrj // we must not leave a hole smaller than this.
149*a9fa9459Szrj off_t min_hole_;
150*a9fa9459Szrj
151*a9fa9459Szrj // Statistics:
152*a9fa9459Szrj // The total number of free lists used.
153*a9fa9459Szrj static unsigned int num_lists;
154*a9fa9459Szrj // The total number of free list nodes used.
155*a9fa9459Szrj static unsigned int num_nodes;
156*a9fa9459Szrj // The total number of calls to Free_list::remove.
157*a9fa9459Szrj static unsigned int num_removes;
158*a9fa9459Szrj // The total number of nodes visited during calls to Free_list::remove.
159*a9fa9459Szrj static unsigned int num_remove_visits;
160*a9fa9459Szrj // The total number of calls to Free_list::allocate.
161*a9fa9459Szrj static unsigned int num_allocates;
162*a9fa9459Szrj // The total number of nodes visited during calls to Free_list::allocate.
163*a9fa9459Szrj static unsigned int num_allocate_visits;
164*a9fa9459Szrj };
165*a9fa9459Szrj
166*a9fa9459Szrj // This task function handles mapping the input sections to output
167*a9fa9459Szrj // sections and laying them out in memory.
168*a9fa9459Szrj
169*a9fa9459Szrj class Layout_task_runner : public Task_function_runner
170*a9fa9459Szrj {
171*a9fa9459Szrj public:
172*a9fa9459Szrj // OPTIONS is the command line options, INPUT_OBJECTS is the list of
173*a9fa9459Szrj // input objects, SYMTAB is the symbol table, LAYOUT is the layout
174*a9fa9459Szrj // object.
Layout_task_runner(const General_options & options,const Input_objects * input_objects,Symbol_table * symtab,Target * target,Layout * layout,Mapfile * mapfile)175*a9fa9459Szrj Layout_task_runner(const General_options& options,
176*a9fa9459Szrj const Input_objects* input_objects,
177*a9fa9459Szrj Symbol_table* symtab,
178*a9fa9459Szrj Target* target,
179*a9fa9459Szrj Layout* layout,
180*a9fa9459Szrj Mapfile* mapfile)
181*a9fa9459Szrj : options_(options), input_objects_(input_objects), symtab_(symtab),
182*a9fa9459Szrj target_(target), layout_(layout), mapfile_(mapfile)
183*a9fa9459Szrj { }
184*a9fa9459Szrj
185*a9fa9459Szrj // Run the operation.
186*a9fa9459Szrj void
187*a9fa9459Szrj run(Workqueue*, const Task*);
188*a9fa9459Szrj
189*a9fa9459Szrj private:
190*a9fa9459Szrj Layout_task_runner(const Layout_task_runner&);
191*a9fa9459Szrj Layout_task_runner& operator=(const Layout_task_runner&);
192*a9fa9459Szrj
193*a9fa9459Szrj const General_options& options_;
194*a9fa9459Szrj const Input_objects* input_objects_;
195*a9fa9459Szrj Symbol_table* symtab_;
196*a9fa9459Szrj Target* target_;
197*a9fa9459Szrj Layout* layout_;
198*a9fa9459Szrj Mapfile* mapfile_;
199*a9fa9459Szrj };
200*a9fa9459Szrj
201*a9fa9459Szrj // This class holds information about the comdat group or
202*a9fa9459Szrj // .gnu.linkonce section that will be kept for a given signature.
203*a9fa9459Szrj
204*a9fa9459Szrj class Kept_section
205*a9fa9459Szrj {
206*a9fa9459Szrj private:
207*a9fa9459Szrj // For a comdat group, we build a mapping from the name of each
208*a9fa9459Szrj // section in the group to the section index and the size in object.
209*a9fa9459Szrj // When we discard a group in some other object file, we use this
210*a9fa9459Szrj // map to figure out which kept section the discarded section is
211*a9fa9459Szrj // associated with. We then use that mapping when processing relocs
212*a9fa9459Szrj // against discarded sections.
213*a9fa9459Szrj struct Comdat_section_info
214*a9fa9459Szrj {
215*a9fa9459Szrj // The section index.
216*a9fa9459Szrj unsigned int shndx;
217*a9fa9459Szrj // The section size.
218*a9fa9459Szrj uint64_t size;
219*a9fa9459Szrj
Comdat_section_infoComdat_section_info220*a9fa9459Szrj Comdat_section_info(unsigned int a_shndx, uint64_t a_size)
221*a9fa9459Szrj : shndx(a_shndx), size(a_size)
222*a9fa9459Szrj { }
223*a9fa9459Szrj };
224*a9fa9459Szrj
225*a9fa9459Szrj // Most comdat groups have only one or two sections, so we use a
226*a9fa9459Szrj // std::map rather than an Unordered_map to optimize for that case
227*a9fa9459Szrj // without paying too heavily for groups with more sections.
228*a9fa9459Szrj typedef std::map<std::string, Comdat_section_info> Comdat_group;
229*a9fa9459Szrj
230*a9fa9459Szrj public:
Kept_section()231*a9fa9459Szrj Kept_section()
232*a9fa9459Szrj : object_(NULL), shndx_(0), is_comdat_(false), is_group_name_(false)
233*a9fa9459Szrj { this->u_.linkonce_size = 0; }
234*a9fa9459Szrj
235*a9fa9459Szrj // We need to support copies for the signature map in the Layout
236*a9fa9459Szrj // object, but we should never copy an object after it has been
237*a9fa9459Szrj // marked as a comdat section.
Kept_section(const Kept_section & k)238*a9fa9459Szrj Kept_section(const Kept_section& k)
239*a9fa9459Szrj : object_(k.object_), shndx_(k.shndx_), is_comdat_(false),
240*a9fa9459Szrj is_group_name_(k.is_group_name_)
241*a9fa9459Szrj {
242*a9fa9459Szrj gold_assert(!k.is_comdat_);
243*a9fa9459Szrj this->u_.linkonce_size = 0;
244*a9fa9459Szrj }
245*a9fa9459Szrj
~Kept_section()246*a9fa9459Szrj ~Kept_section()
247*a9fa9459Szrj {
248*a9fa9459Szrj if (this->is_comdat_)
249*a9fa9459Szrj delete this->u_.group_sections;
250*a9fa9459Szrj }
251*a9fa9459Szrj
252*a9fa9459Szrj // The object where this section lives.
253*a9fa9459Szrj Relobj*
object()254*a9fa9459Szrj object() const
255*a9fa9459Szrj { return this->object_; }
256*a9fa9459Szrj
257*a9fa9459Szrj // Set the object.
258*a9fa9459Szrj void
set_object(Relobj * object)259*a9fa9459Szrj set_object(Relobj* object)
260*a9fa9459Szrj {
261*a9fa9459Szrj gold_assert(this->object_ == NULL);
262*a9fa9459Szrj this->object_ = object;
263*a9fa9459Szrj }
264*a9fa9459Szrj
265*a9fa9459Szrj // The section index.
266*a9fa9459Szrj unsigned int
shndx()267*a9fa9459Szrj shndx() const
268*a9fa9459Szrj { return this->shndx_; }
269*a9fa9459Szrj
270*a9fa9459Szrj // Set the section index.
271*a9fa9459Szrj void
set_shndx(unsigned int shndx)272*a9fa9459Szrj set_shndx(unsigned int shndx)
273*a9fa9459Szrj {
274*a9fa9459Szrj gold_assert(this->shndx_ == 0);
275*a9fa9459Szrj this->shndx_ = shndx;
276*a9fa9459Szrj }
277*a9fa9459Szrj
278*a9fa9459Szrj // Whether this is a comdat group.
279*a9fa9459Szrj bool
is_comdat()280*a9fa9459Szrj is_comdat() const
281*a9fa9459Szrj { return this->is_comdat_; }
282*a9fa9459Szrj
283*a9fa9459Szrj // Set that this is a comdat group.
284*a9fa9459Szrj void
set_is_comdat()285*a9fa9459Szrj set_is_comdat()
286*a9fa9459Szrj {
287*a9fa9459Szrj gold_assert(!this->is_comdat_);
288*a9fa9459Szrj this->is_comdat_ = true;
289*a9fa9459Szrj this->u_.group_sections = new Comdat_group();
290*a9fa9459Szrj }
291*a9fa9459Szrj
292*a9fa9459Szrj // Whether this is associated with the name of a group or section
293*a9fa9459Szrj // rather than the symbol name derived from a linkonce section.
294*a9fa9459Szrj bool
is_group_name()295*a9fa9459Szrj is_group_name() const
296*a9fa9459Szrj { return this->is_group_name_; }
297*a9fa9459Szrj
298*a9fa9459Szrj // Note that this represents a comdat group rather than a single
299*a9fa9459Szrj // linkonce section.
300*a9fa9459Szrj void
set_is_group_name()301*a9fa9459Szrj set_is_group_name()
302*a9fa9459Szrj { this->is_group_name_ = true; }
303*a9fa9459Szrj
304*a9fa9459Szrj // Add a section to the group list.
305*a9fa9459Szrj void
add_comdat_section(const std::string & name,unsigned int shndx,uint64_t size)306*a9fa9459Szrj add_comdat_section(const std::string& name, unsigned int shndx,
307*a9fa9459Szrj uint64_t size)
308*a9fa9459Szrj {
309*a9fa9459Szrj gold_assert(this->is_comdat_);
310*a9fa9459Szrj Comdat_section_info sinfo(shndx, size);
311*a9fa9459Szrj this->u_.group_sections->insert(std::make_pair(name, sinfo));
312*a9fa9459Szrj }
313*a9fa9459Szrj
314*a9fa9459Szrj // Look for a section name in the group list, and return whether it
315*a9fa9459Szrj // was found. If found, returns the section index and size.
316*a9fa9459Szrj bool
find_comdat_section(const std::string & name,unsigned int * pshndx,uint64_t * psize)317*a9fa9459Szrj find_comdat_section(const std::string& name, unsigned int* pshndx,
318*a9fa9459Szrj uint64_t* psize) const
319*a9fa9459Szrj {
320*a9fa9459Szrj gold_assert(this->is_comdat_);
321*a9fa9459Szrj Comdat_group::const_iterator p = this->u_.group_sections->find(name);
322*a9fa9459Szrj if (p == this->u_.group_sections->end())
323*a9fa9459Szrj return false;
324*a9fa9459Szrj *pshndx = p->second.shndx;
325*a9fa9459Szrj *psize = p->second.size;
326*a9fa9459Szrj return true;
327*a9fa9459Szrj }
328*a9fa9459Szrj
329*a9fa9459Szrj // If there is only one section in the group list, return true, and
330*a9fa9459Szrj // return the section index and size.
331*a9fa9459Szrj bool
find_single_comdat_section(unsigned int * pshndx,uint64_t * psize)332*a9fa9459Szrj find_single_comdat_section(unsigned int* pshndx, uint64_t* psize) const
333*a9fa9459Szrj {
334*a9fa9459Szrj gold_assert(this->is_comdat_);
335*a9fa9459Szrj if (this->u_.group_sections->size() != 1)
336*a9fa9459Szrj return false;
337*a9fa9459Szrj Comdat_group::const_iterator p = this->u_.group_sections->begin();
338*a9fa9459Szrj *pshndx = p->second.shndx;
339*a9fa9459Szrj *psize = p->second.size;
340*a9fa9459Szrj return true;
341*a9fa9459Szrj }
342*a9fa9459Szrj
343*a9fa9459Szrj // Return the size of a linkonce section.
344*a9fa9459Szrj uint64_t
linkonce_size()345*a9fa9459Szrj linkonce_size() const
346*a9fa9459Szrj {
347*a9fa9459Szrj gold_assert(!this->is_comdat_);
348*a9fa9459Szrj return this->u_.linkonce_size;
349*a9fa9459Szrj }
350*a9fa9459Szrj
351*a9fa9459Szrj // Set the size of a linkonce section.
352*a9fa9459Szrj void
set_linkonce_size(uint64_t size)353*a9fa9459Szrj set_linkonce_size(uint64_t size)
354*a9fa9459Szrj {
355*a9fa9459Szrj gold_assert(!this->is_comdat_);
356*a9fa9459Szrj this->u_.linkonce_size = size;
357*a9fa9459Szrj }
358*a9fa9459Szrj
359*a9fa9459Szrj private:
360*a9fa9459Szrj // No assignment.
361*a9fa9459Szrj Kept_section& operator=(const Kept_section&);
362*a9fa9459Szrj
363*a9fa9459Szrj // The object containing the comdat group or .gnu.linkonce section.
364*a9fa9459Szrj Relobj* object_;
365*a9fa9459Szrj // Index of the group section for comdats and the section itself for
366*a9fa9459Szrj // .gnu.linkonce.
367*a9fa9459Szrj unsigned int shndx_;
368*a9fa9459Szrj // True if this is for a comdat group rather than a .gnu.linkonce
369*a9fa9459Szrj // section.
370*a9fa9459Szrj bool is_comdat_;
371*a9fa9459Szrj // The Kept_sections are values of a mapping, that maps names to
372*a9fa9459Szrj // them. This field is true if this struct is associated with the
373*a9fa9459Szrj // name of a comdat or .gnu.linkonce, false if it is associated with
374*a9fa9459Szrj // the name of a symbol obtained from the .gnu.linkonce.* name
375*a9fa9459Szrj // through some heuristics.
376*a9fa9459Szrj bool is_group_name_;
377*a9fa9459Szrj union
378*a9fa9459Szrj {
379*a9fa9459Szrj // If the is_comdat_ field is true, this holds a map from names of
380*a9fa9459Szrj // the sections in the group to section indexes in object_ and to
381*a9fa9459Szrj // section sizes.
382*a9fa9459Szrj Comdat_group* group_sections;
383*a9fa9459Szrj // If the is_comdat_ field is false, this holds the size of the
384*a9fa9459Szrj // single section.
385*a9fa9459Szrj uint64_t linkonce_size;
386*a9fa9459Szrj } u_;
387*a9fa9459Szrj };
388*a9fa9459Szrj
389*a9fa9459Szrj // The ordering for output sections. This controls how output
390*a9fa9459Szrj // sections are ordered within a PT_LOAD output segment.
391*a9fa9459Szrj
392*a9fa9459Szrj enum Output_section_order
393*a9fa9459Szrj {
394*a9fa9459Szrj // Unspecified. Used for non-load segments. Also used for the file
395*a9fa9459Szrj // and segment headers.
396*a9fa9459Szrj ORDER_INVALID,
397*a9fa9459Szrj
398*a9fa9459Szrj // The PT_INTERP section should come first, so that the dynamic
399*a9fa9459Szrj // linker can pick it up quickly.
400*a9fa9459Szrj ORDER_INTERP,
401*a9fa9459Szrj
402*a9fa9459Szrj // Loadable read-only note sections come next so that the PT_NOTE
403*a9fa9459Szrj // segment is on the first page of the executable.
404*a9fa9459Szrj ORDER_RO_NOTE,
405*a9fa9459Szrj
406*a9fa9459Szrj // Put read-only sections used by the dynamic linker early in the
407*a9fa9459Szrj // executable to minimize paging.
408*a9fa9459Szrj ORDER_DYNAMIC_LINKER,
409*a9fa9459Szrj
410*a9fa9459Szrj // Put reloc sections used by the dynamic linker after other
411*a9fa9459Szrj // sections used by the dynamic linker; otherwise, objcopy and strip
412*a9fa9459Szrj // get confused.
413*a9fa9459Szrj ORDER_DYNAMIC_RELOCS,
414*a9fa9459Szrj
415*a9fa9459Szrj // Put the PLT reloc section after the other dynamic relocs;
416*a9fa9459Szrj // otherwise, prelink gets confused.
417*a9fa9459Szrj ORDER_DYNAMIC_PLT_RELOCS,
418*a9fa9459Szrj
419*a9fa9459Szrj // The .init section.
420*a9fa9459Szrj ORDER_INIT,
421*a9fa9459Szrj
422*a9fa9459Szrj // The PLT.
423*a9fa9459Szrj ORDER_PLT,
424*a9fa9459Szrj
425*a9fa9459Szrj // The regular text sections.
426*a9fa9459Szrj ORDER_TEXT,
427*a9fa9459Szrj
428*a9fa9459Szrj // The .fini section.
429*a9fa9459Szrj ORDER_FINI,
430*a9fa9459Szrj
431*a9fa9459Szrj // The read-only sections.
432*a9fa9459Szrj ORDER_READONLY,
433*a9fa9459Szrj
434*a9fa9459Szrj // The exception frame sections.
435*a9fa9459Szrj ORDER_EHFRAME,
436*a9fa9459Szrj
437*a9fa9459Szrj // The TLS sections come first in the data section.
438*a9fa9459Szrj ORDER_TLS_DATA,
439*a9fa9459Szrj ORDER_TLS_BSS,
440*a9fa9459Szrj
441*a9fa9459Szrj // Local RELRO (read-only after relocation) sections come before
442*a9fa9459Szrj // non-local RELRO sections. This data will be fully resolved by
443*a9fa9459Szrj // the prelinker.
444*a9fa9459Szrj ORDER_RELRO_LOCAL,
445*a9fa9459Szrj
446*a9fa9459Szrj // Non-local RELRO sections are grouped together after local RELRO
447*a9fa9459Szrj // sections. All RELRO sections must be adjacent so that they can
448*a9fa9459Szrj // all be put into a PT_GNU_RELRO segment.
449*a9fa9459Szrj ORDER_RELRO,
450*a9fa9459Szrj
451*a9fa9459Szrj // We permit marking exactly one output section as the last RELRO
452*a9fa9459Szrj // section. We do this so that the read-only GOT can be adjacent to
453*a9fa9459Szrj // the writable GOT.
454*a9fa9459Szrj ORDER_RELRO_LAST,
455*a9fa9459Szrj
456*a9fa9459Szrj // Similarly, we permit marking exactly one output section as the
457*a9fa9459Szrj // first non-RELRO section.
458*a9fa9459Szrj ORDER_NON_RELRO_FIRST,
459*a9fa9459Szrj
460*a9fa9459Szrj // The regular data sections come after the RELRO sections.
461*a9fa9459Szrj ORDER_DATA,
462*a9fa9459Szrj
463*a9fa9459Szrj // Large data sections normally go in large data segments.
464*a9fa9459Szrj ORDER_LARGE_DATA,
465*a9fa9459Szrj
466*a9fa9459Szrj // Group writable notes so that we can have a single PT_NOTE
467*a9fa9459Szrj // segment.
468*a9fa9459Szrj ORDER_RW_NOTE,
469*a9fa9459Szrj
470*a9fa9459Szrj // The small data sections must be at the end of the data sections,
471*a9fa9459Szrj // so that they can be adjacent to the small BSS sections.
472*a9fa9459Szrj ORDER_SMALL_DATA,
473*a9fa9459Szrj
474*a9fa9459Szrj // The BSS sections start here.
475*a9fa9459Szrj
476*a9fa9459Szrj // The small BSS sections must be at the start of the BSS sections,
477*a9fa9459Szrj // so that they can be adjacent to the small data sections.
478*a9fa9459Szrj ORDER_SMALL_BSS,
479*a9fa9459Szrj
480*a9fa9459Szrj // The regular BSS sections.
481*a9fa9459Szrj ORDER_BSS,
482*a9fa9459Szrj
483*a9fa9459Szrj // The large BSS sections come after the other BSS sections.
484*a9fa9459Szrj ORDER_LARGE_BSS,
485*a9fa9459Szrj
486*a9fa9459Szrj // Maximum value.
487*a9fa9459Szrj ORDER_MAX
488*a9fa9459Szrj };
489*a9fa9459Szrj
490*a9fa9459Szrj // This class handles the details of laying out input sections.
491*a9fa9459Szrj
492*a9fa9459Szrj class Layout
493*a9fa9459Szrj {
494*a9fa9459Szrj public:
495*a9fa9459Szrj Layout(int number_of_input_files, Script_options*);
496*a9fa9459Szrj
~Layout()497*a9fa9459Szrj ~Layout()
498*a9fa9459Szrj {
499*a9fa9459Szrj delete this->relaxation_debug_check_;
500*a9fa9459Szrj delete this->segment_states_;
501*a9fa9459Szrj }
502*a9fa9459Szrj
503*a9fa9459Szrj // For incremental links, record the base file to be modified.
504*a9fa9459Szrj void
505*a9fa9459Szrj set_incremental_base(Incremental_binary* base);
506*a9fa9459Szrj
507*a9fa9459Szrj Incremental_binary*
incremental_base()508*a9fa9459Szrj incremental_base()
509*a9fa9459Szrj { return this->incremental_base_; }
510*a9fa9459Szrj
511*a9fa9459Szrj // For incremental links, record the initial fixed layout of a section
512*a9fa9459Szrj // from the base file, and return a pointer to the Output_section.
513*a9fa9459Szrj template<int size, bool big_endian>
514*a9fa9459Szrj Output_section*
515*a9fa9459Szrj init_fixed_output_section(const char*, elfcpp::Shdr<size, big_endian>&);
516*a9fa9459Szrj
517*a9fa9459Szrj // Given an input section SHNDX, named NAME, with data in SHDR, from
518*a9fa9459Szrj // the object file OBJECT, return the output section where this
519*a9fa9459Szrj // input section should go. RELOC_SHNDX is the index of a
520*a9fa9459Szrj // relocation section which applies to this section, or 0 if none,
521*a9fa9459Szrj // or -1U if more than one. RELOC_TYPE is the type of the
522*a9fa9459Szrj // relocation section if there is one. Set *OFFSET to the offset
523*a9fa9459Szrj // within the output section.
524*a9fa9459Szrj template<int size, bool big_endian>
525*a9fa9459Szrj Output_section*
526*a9fa9459Szrj layout(Sized_relobj_file<size, big_endian> *object, unsigned int shndx,
527*a9fa9459Szrj const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
528*a9fa9459Szrj unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset);
529*a9fa9459Szrj
530*a9fa9459Szrj std::map<Section_id, unsigned int>*
get_section_order_map()531*a9fa9459Szrj get_section_order_map()
532*a9fa9459Szrj { return &this->section_order_map_; }
533*a9fa9459Szrj
534*a9fa9459Szrj // Struct to store segment info when mapping some input sections to
535*a9fa9459Szrj // unique segments using linker plugins. Mapping an input section to
536*a9fa9459Szrj // a unique segment is done by first placing such input sections in
537*a9fa9459Szrj // unique output sections and then mapping the output section to a
538*a9fa9459Szrj // unique segment. NAME is the name of the output section. FLAGS
539*a9fa9459Szrj // and ALIGN are the extra flags and alignment of the segment.
540*a9fa9459Szrj struct Unique_segment_info
541*a9fa9459Szrj {
542*a9fa9459Szrj // Identifier for the segment. ELF segments dont have names. This
543*a9fa9459Szrj // is used as the name of the output section mapped to the segment.
544*a9fa9459Szrj const char* name;
545*a9fa9459Szrj // Additional segment flags.
546*a9fa9459Szrj uint64_t flags;
547*a9fa9459Szrj // Segment alignment.
548*a9fa9459Szrj uint64_t align;
549*a9fa9459Szrj };
550*a9fa9459Szrj
551*a9fa9459Szrj // Mapping from input section to segment.
552*a9fa9459Szrj typedef std::map<Const_section_id, Unique_segment_info*>
553*a9fa9459Szrj Section_segment_map;
554*a9fa9459Szrj
555*a9fa9459Szrj // Maps section SECN to SEGMENT s.
556*a9fa9459Szrj void
557*a9fa9459Szrj insert_section_segment_map(Const_section_id secn, Unique_segment_info *s);
558*a9fa9459Szrj
559*a9fa9459Szrj // Some input sections require special ordering, for compatibility
560*a9fa9459Szrj // with GNU ld. Given the name of an input section, return -1 if it
561*a9fa9459Szrj // does not require special ordering. Otherwise, return the index
562*a9fa9459Szrj // by which it should be ordered compared to other input sections
563*a9fa9459Szrj // that require special ordering.
564*a9fa9459Szrj static int
565*a9fa9459Szrj special_ordering_of_input_section(const char* name);
566*a9fa9459Szrj
567*a9fa9459Szrj bool
is_section_ordering_specified()568*a9fa9459Szrj is_section_ordering_specified()
569*a9fa9459Szrj { return this->section_ordering_specified_; }
570*a9fa9459Szrj
571*a9fa9459Szrj void
set_section_ordering_specified()572*a9fa9459Szrj set_section_ordering_specified()
573*a9fa9459Szrj { this->section_ordering_specified_ = true; }
574*a9fa9459Szrj
575*a9fa9459Szrj bool
is_unique_segment_for_sections_specified()576*a9fa9459Szrj is_unique_segment_for_sections_specified() const
577*a9fa9459Szrj { return this->unique_segment_for_sections_specified_; }
578*a9fa9459Szrj
579*a9fa9459Szrj void
set_unique_segment_for_sections_specified()580*a9fa9459Szrj set_unique_segment_for_sections_specified()
581*a9fa9459Szrj { this->unique_segment_for_sections_specified_ = true; }
582*a9fa9459Szrj
583*a9fa9459Szrj // For incremental updates, allocate a block of memory from the
584*a9fa9459Szrj // free list. Find a block starting at or after MINOFF.
585*a9fa9459Szrj off_t
allocate(off_t len,uint64_t align,off_t minoff)586*a9fa9459Szrj allocate(off_t len, uint64_t align, off_t minoff)
587*a9fa9459Szrj { return this->free_list_.allocate(len, align, minoff); }
588*a9fa9459Szrj
589*a9fa9459Szrj unsigned int
590*a9fa9459Szrj find_section_order_index(const std::string&);
591*a9fa9459Szrj
592*a9fa9459Szrj // Read the sequence of input sections from the file specified with
593*a9fa9459Szrj // linker option --section-ordering-file.
594*a9fa9459Szrj void
595*a9fa9459Szrj read_layout_from_file();
596*a9fa9459Szrj
597*a9fa9459Szrj // Layout an input reloc section when doing a relocatable link. The
598*a9fa9459Szrj // section is RELOC_SHNDX in OBJECT, with data in SHDR.
599*a9fa9459Szrj // DATA_SECTION is the reloc section to which it refers. RR is the
600*a9fa9459Szrj // relocatable information.
601*a9fa9459Szrj template<int size, bool big_endian>
602*a9fa9459Szrj Output_section*
603*a9fa9459Szrj layout_reloc(Sized_relobj_file<size, big_endian>* object,
604*a9fa9459Szrj unsigned int reloc_shndx,
605*a9fa9459Szrj const elfcpp::Shdr<size, big_endian>& shdr,
606*a9fa9459Szrj Output_section* data_section,
607*a9fa9459Szrj Relocatable_relocs* rr);
608*a9fa9459Szrj
609*a9fa9459Szrj // Layout a group section when doing a relocatable link.
610*a9fa9459Szrj template<int size, bool big_endian>
611*a9fa9459Szrj void
612*a9fa9459Szrj layout_group(Symbol_table* symtab,
613*a9fa9459Szrj Sized_relobj_file<size, big_endian>* object,
614*a9fa9459Szrj unsigned int group_shndx,
615*a9fa9459Szrj const char* group_section_name,
616*a9fa9459Szrj const char* signature,
617*a9fa9459Szrj const elfcpp::Shdr<size, big_endian>& shdr,
618*a9fa9459Szrj elfcpp::Elf_Word flags,
619*a9fa9459Szrj std::vector<unsigned int>* shndxes);
620*a9fa9459Szrj
621*a9fa9459Szrj // Like layout, only for exception frame sections. OBJECT is an
622*a9fa9459Szrj // object file. SYMBOLS is the contents of the symbol table
623*a9fa9459Szrj // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of
624*a9fa9459Szrj // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a
625*a9fa9459Szrj // .eh_frame section in OBJECT. SHDR is the section header.
626*a9fa9459Szrj // RELOC_SHNDX is the index of a relocation section which applies to
627*a9fa9459Szrj // this section, or 0 if none, or -1U if more than one. RELOC_TYPE
628*a9fa9459Szrj // is the type of the relocation section if there is one. This
629*a9fa9459Szrj // returns the output section, and sets *OFFSET to the offset.
630*a9fa9459Szrj template<int size, bool big_endian>
631*a9fa9459Szrj Output_section*
632*a9fa9459Szrj layout_eh_frame(Sized_relobj_file<size, big_endian>* object,
633*a9fa9459Szrj const unsigned char* symbols,
634*a9fa9459Szrj off_t symbols_size,
635*a9fa9459Szrj const unsigned char* symbol_names,
636*a9fa9459Szrj off_t symbol_names_size,
637*a9fa9459Szrj unsigned int shndx,
638*a9fa9459Szrj const elfcpp::Shdr<size, big_endian>& shdr,
639*a9fa9459Szrj unsigned int reloc_shndx, unsigned int reloc_type,
640*a9fa9459Szrj off_t* offset);
641*a9fa9459Szrj
642*a9fa9459Szrj // After processing all input files, we call this to make sure that
643*a9fa9459Szrj // the optimized .eh_frame sections have been added to the output
644*a9fa9459Szrj // section.
645*a9fa9459Szrj void
646*a9fa9459Szrj finalize_eh_frame_section();
647*a9fa9459Szrj
648*a9fa9459Szrj // Add .eh_frame information for a PLT. The FDE must start with a
649*a9fa9459Szrj // 4-byte PC-relative reference to the start of the PLT, followed by
650*a9fa9459Szrj // a 4-byte size of PLT.
651*a9fa9459Szrj void
652*a9fa9459Szrj add_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
653*a9fa9459Szrj size_t cie_length, const unsigned char* fde_data,
654*a9fa9459Szrj size_t fde_length);
655*a9fa9459Szrj
656*a9fa9459Szrj // Scan a .debug_info or .debug_types section, and add summary
657*a9fa9459Szrj // information to the .gdb_index section.
658*a9fa9459Szrj template<int size, bool big_endian>
659*a9fa9459Szrj void
660*a9fa9459Szrj add_to_gdb_index(bool is_type_unit,
661*a9fa9459Szrj Sized_relobj<size, big_endian>* object,
662*a9fa9459Szrj const unsigned char* symbols,
663*a9fa9459Szrj off_t symbols_size,
664*a9fa9459Szrj unsigned int shndx,
665*a9fa9459Szrj unsigned int reloc_shndx,
666*a9fa9459Szrj unsigned int reloc_type);
667*a9fa9459Szrj
668*a9fa9459Szrj // Handle a GNU stack note. This is called once per input object
669*a9fa9459Szrj // file. SEEN_GNU_STACK is true if the object file has a
670*a9fa9459Szrj // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags
671*a9fa9459Szrj // from that section if there was one.
672*a9fa9459Szrj void
673*a9fa9459Szrj layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
674*a9fa9459Szrj const Object*);
675*a9fa9459Szrj
676*a9fa9459Szrj // Add an Output_section_data to the layout. This is used for
677*a9fa9459Szrj // special sections like the GOT section. ORDER is where the
678*a9fa9459Szrj // section should wind up in the output segment. IS_RELRO is true
679*a9fa9459Szrj // for relro sections.
680*a9fa9459Szrj Output_section*
681*a9fa9459Szrj add_output_section_data(const char* name, elfcpp::Elf_Word type,
682*a9fa9459Szrj elfcpp::Elf_Xword flags,
683*a9fa9459Szrj Output_section_data*, Output_section_order order,
684*a9fa9459Szrj bool is_relro);
685*a9fa9459Szrj
686*a9fa9459Szrj // Increase the size of the relro segment by this much.
687*a9fa9459Szrj void
increase_relro(unsigned int s)688*a9fa9459Szrj increase_relro(unsigned int s)
689*a9fa9459Szrj { this->increase_relro_ += s; }
690*a9fa9459Szrj
691*a9fa9459Szrj // Create dynamic sections if necessary.
692*a9fa9459Szrj void
693*a9fa9459Szrj create_initial_dynamic_sections(Symbol_table*);
694*a9fa9459Szrj
695*a9fa9459Szrj // Define __start and __stop symbols for output sections.
696*a9fa9459Szrj void
697*a9fa9459Szrj define_section_symbols(Symbol_table*);
698*a9fa9459Szrj
699*a9fa9459Szrj // Create automatic note sections.
700*a9fa9459Szrj void
701*a9fa9459Szrj create_notes();
702*a9fa9459Szrj
703*a9fa9459Szrj // Create sections for linker scripts.
704*a9fa9459Szrj void
create_script_sections()705*a9fa9459Szrj create_script_sections()
706*a9fa9459Szrj { this->script_options_->create_script_sections(this); }
707*a9fa9459Szrj
708*a9fa9459Szrj // Define symbols from any linker script.
709*a9fa9459Szrj void
define_script_symbols(Symbol_table * symtab)710*a9fa9459Szrj define_script_symbols(Symbol_table* symtab)
711*a9fa9459Szrj { this->script_options_->add_symbols_to_table(symtab); }
712*a9fa9459Szrj
713*a9fa9459Szrj // Define symbols for group signatures.
714*a9fa9459Szrj void
715*a9fa9459Szrj define_group_signatures(Symbol_table*);
716*a9fa9459Szrj
717*a9fa9459Szrj // Return the Stringpool used for symbol names.
718*a9fa9459Szrj const Stringpool*
sympool()719*a9fa9459Szrj sympool() const
720*a9fa9459Szrj { return &this->sympool_; }
721*a9fa9459Szrj
722*a9fa9459Szrj // Return the Stringpool used for dynamic symbol names and dynamic
723*a9fa9459Szrj // tags.
724*a9fa9459Szrj const Stringpool*
dynpool()725*a9fa9459Szrj dynpool() const
726*a9fa9459Szrj { return &this->dynpool_; }
727*a9fa9459Szrj
728*a9fa9459Szrj // Return the .dynamic output section. This is only valid after the
729*a9fa9459Szrj // layout has been finalized.
730*a9fa9459Szrj Output_section*
dynamic_section()731*a9fa9459Szrj dynamic_section() const
732*a9fa9459Szrj { return this->dynamic_section_; }
733*a9fa9459Szrj
734*a9fa9459Szrj // Return the symtab_xindex section used to hold large section
735*a9fa9459Szrj // indexes for the normal symbol table.
736*a9fa9459Szrj Output_symtab_xindex*
symtab_xindex()737*a9fa9459Szrj symtab_xindex() const
738*a9fa9459Szrj { return this->symtab_xindex_; }
739*a9fa9459Szrj
740*a9fa9459Szrj // Return the dynsym_xindex section used to hold large section
741*a9fa9459Szrj // indexes for the dynamic symbol table.
742*a9fa9459Szrj Output_symtab_xindex*
dynsym_xindex()743*a9fa9459Szrj dynsym_xindex() const
744*a9fa9459Szrj { return this->dynsym_xindex_; }
745*a9fa9459Szrj
746*a9fa9459Szrj // Return whether a section is a .gnu.linkonce section, given the
747*a9fa9459Szrj // section name.
748*a9fa9459Szrj static inline bool
is_linkonce(const char * name)749*a9fa9459Szrj is_linkonce(const char* name)
750*a9fa9459Szrj { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
751*a9fa9459Szrj
752*a9fa9459Szrj // Whether we have added an input section.
753*a9fa9459Szrj bool
have_added_input_section()754*a9fa9459Szrj have_added_input_section() const
755*a9fa9459Szrj { return this->have_added_input_section_; }
756*a9fa9459Szrj
757*a9fa9459Szrj // Return true if a section is a debugging section.
758*a9fa9459Szrj static inline bool
is_debug_info_section(const char * name)759*a9fa9459Szrj is_debug_info_section(const char* name)
760*a9fa9459Szrj {
761*a9fa9459Szrj // Debugging sections can only be recognized by name.
762*a9fa9459Szrj return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0
763*a9fa9459Szrj || strncmp(name, ".zdebug", sizeof(".zdebug") - 1) == 0
764*a9fa9459Szrj || strncmp(name, ".gnu.linkonce.wi.",
765*a9fa9459Szrj sizeof(".gnu.linkonce.wi.") - 1) == 0
766*a9fa9459Szrj || strncmp(name, ".line", sizeof(".line") - 1) == 0
767*a9fa9459Szrj || strncmp(name, ".stab", sizeof(".stab") - 1) == 0
768*a9fa9459Szrj || strncmp(name, ".pdr", sizeof(".pdr") - 1) == 0);
769*a9fa9459Szrj }
770*a9fa9459Szrj
771*a9fa9459Szrj // Return true if RELOBJ is an input file whose base name matches
772*a9fa9459Szrj // FILE_NAME. The base name must have an extension of ".o", and
773*a9fa9459Szrj // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
774*a9fa9459Szrj static bool
775*a9fa9459Szrj match_file_name(const Relobj* relobj, const char* file_name);
776*a9fa9459Szrj
777*a9fa9459Szrj // Return whether section SHNDX in RELOBJ is a .ctors/.dtors section
778*a9fa9459Szrj // with more than one word being mapped to a .init_array/.fini_array
779*a9fa9459Szrj // section.
780*a9fa9459Szrj bool
781*a9fa9459Szrj is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const;
782*a9fa9459Szrj
783*a9fa9459Szrj // Check if a comdat group or .gnu.linkonce section with the given
784*a9fa9459Szrj // NAME is selected for the link. If there is already a section,
785*a9fa9459Szrj // *KEPT_SECTION is set to point to the signature and the function
786*a9fa9459Szrj // returns false. Otherwise, OBJECT, SHNDX,IS_COMDAT, and
787*a9fa9459Szrj // IS_GROUP_NAME are recorded for this NAME in the layout object,
788*a9fa9459Szrj // *KEPT_SECTION is set to the internal copy and the function return
789*a9fa9459Szrj // false.
790*a9fa9459Szrj bool
791*a9fa9459Szrj find_or_add_kept_section(const std::string& name, Relobj* object,
792*a9fa9459Szrj unsigned int shndx, bool is_comdat,
793*a9fa9459Szrj bool is_group_name, Kept_section** kept_section);
794*a9fa9459Szrj
795*a9fa9459Szrj // Finalize the layout after all the input sections have been added.
796*a9fa9459Szrj off_t
797*a9fa9459Szrj finalize(const Input_objects*, Symbol_table*, Target*, const Task*);
798*a9fa9459Szrj
799*a9fa9459Szrj // Return whether any sections require postprocessing.
800*a9fa9459Szrj bool
any_postprocessing_sections()801*a9fa9459Szrj any_postprocessing_sections() const
802*a9fa9459Szrj { return this->any_postprocessing_sections_; }
803*a9fa9459Szrj
804*a9fa9459Szrj // Return the size of the output file.
805*a9fa9459Szrj off_t
output_file_size()806*a9fa9459Szrj output_file_size() const
807*a9fa9459Szrj { return this->output_file_size_; }
808*a9fa9459Szrj
809*a9fa9459Szrj // Return the TLS segment. This will return NULL if there isn't
810*a9fa9459Szrj // one.
811*a9fa9459Szrj Output_segment*
tls_segment()812*a9fa9459Szrj tls_segment() const
813*a9fa9459Szrj { return this->tls_segment_; }
814*a9fa9459Szrj
815*a9fa9459Szrj // Return the normal symbol table.
816*a9fa9459Szrj Output_section*
symtab_section()817*a9fa9459Szrj symtab_section() const
818*a9fa9459Szrj {
819*a9fa9459Szrj gold_assert(this->symtab_section_ != NULL);
820*a9fa9459Szrj return this->symtab_section_;
821*a9fa9459Szrj }
822*a9fa9459Szrj
823*a9fa9459Szrj // Return the file offset of the normal symbol table.
824*a9fa9459Szrj off_t
825*a9fa9459Szrj symtab_section_offset() const;
826*a9fa9459Szrj
827*a9fa9459Szrj // Return the section index of the normal symbol tabl.e
828*a9fa9459Szrj unsigned int
829*a9fa9459Szrj symtab_section_shndx() const;
830*a9fa9459Szrj
831*a9fa9459Szrj // Return the dynamic symbol table.
832*a9fa9459Szrj Output_section*
dynsym_section()833*a9fa9459Szrj dynsym_section() const
834*a9fa9459Szrj {
835*a9fa9459Szrj gold_assert(this->dynsym_section_ != NULL);
836*a9fa9459Szrj return this->dynsym_section_;
837*a9fa9459Szrj }
838*a9fa9459Szrj
839*a9fa9459Szrj // Return the dynamic tags.
840*a9fa9459Szrj Output_data_dynamic*
dynamic_data()841*a9fa9459Szrj dynamic_data() const
842*a9fa9459Szrj { return this->dynamic_data_; }
843*a9fa9459Szrj
844*a9fa9459Szrj // Write out the output sections.
845*a9fa9459Szrj void
846*a9fa9459Szrj write_output_sections(Output_file* of) const;
847*a9fa9459Szrj
848*a9fa9459Szrj // Write out data not associated with an input file or the symbol
849*a9fa9459Szrj // table.
850*a9fa9459Szrj void
851*a9fa9459Szrj write_data(const Symbol_table*, Output_file*) const;
852*a9fa9459Szrj
853*a9fa9459Szrj // Write out output sections which can not be written until all the
854*a9fa9459Szrj // input sections are complete.
855*a9fa9459Szrj void
856*a9fa9459Szrj write_sections_after_input_sections(Output_file* of);
857*a9fa9459Szrj
858*a9fa9459Szrj // Return an output section named NAME, or NULL if there is none.
859*a9fa9459Szrj Output_section*
860*a9fa9459Szrj find_output_section(const char* name) const;
861*a9fa9459Szrj
862*a9fa9459Szrj // Return an output segment of type TYPE, with segment flags SET set
863*a9fa9459Szrj // and segment flags CLEAR clear. Return NULL if there is none.
864*a9fa9459Szrj Output_segment*
865*a9fa9459Szrj find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
866*a9fa9459Szrj elfcpp::Elf_Word clear) const;
867*a9fa9459Szrj
868*a9fa9459Szrj // Return the number of segments we expect to produce.
869*a9fa9459Szrj size_t
870*a9fa9459Szrj expected_segment_count() const;
871*a9fa9459Szrj
872*a9fa9459Szrj // Set a flag to indicate that an object file uses the static TLS model.
873*a9fa9459Szrj void
set_has_static_tls()874*a9fa9459Szrj set_has_static_tls()
875*a9fa9459Szrj { this->has_static_tls_ = true; }
876*a9fa9459Szrj
877*a9fa9459Szrj // Return true if any object file uses the static TLS model.
878*a9fa9459Szrj bool
has_static_tls()879*a9fa9459Szrj has_static_tls() const
880*a9fa9459Szrj { return this->has_static_tls_; }
881*a9fa9459Szrj
882*a9fa9459Szrj // Return the options which may be set by a linker script.
883*a9fa9459Szrj Script_options*
script_options()884*a9fa9459Szrj script_options()
885*a9fa9459Szrj { return this->script_options_; }
886*a9fa9459Szrj
887*a9fa9459Szrj const Script_options*
script_options()888*a9fa9459Szrj script_options() const
889*a9fa9459Szrj { return this->script_options_; }
890*a9fa9459Szrj
891*a9fa9459Szrj // Return the object managing inputs in incremental build. NULL in
892*a9fa9459Szrj // non-incremental builds.
893*a9fa9459Szrj Incremental_inputs*
incremental_inputs()894*a9fa9459Szrj incremental_inputs() const
895*a9fa9459Szrj { return this->incremental_inputs_; }
896*a9fa9459Szrj
897*a9fa9459Szrj // For the target-specific code to add dynamic tags which are common
898*a9fa9459Szrj // to most targets.
899*a9fa9459Szrj void
900*a9fa9459Szrj add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
901*a9fa9459Szrj const Output_data* plt_rel,
902*a9fa9459Szrj const Output_data_reloc_generic* dyn_rel,
903*a9fa9459Szrj bool add_debug, bool dynrel_includes_plt);
904*a9fa9459Szrj
905*a9fa9459Szrj // Add a target-specific dynamic tag with constant value.
906*a9fa9459Szrj void
907*a9fa9459Szrj add_target_specific_dynamic_tag(elfcpp::DT tag, unsigned int val);
908*a9fa9459Szrj
909*a9fa9459Szrj // Compute and write out the build ID if needed.
910*a9fa9459Szrj void
911*a9fa9459Szrj write_build_id(Output_file*, unsigned char*, size_t) const;
912*a9fa9459Szrj
913*a9fa9459Szrj // Rewrite output file in binary format.
914*a9fa9459Szrj void
915*a9fa9459Szrj write_binary(Output_file* in) const;
916*a9fa9459Szrj
917*a9fa9459Szrj // Print output sections to the map file.
918*a9fa9459Szrj void
919*a9fa9459Szrj print_to_mapfile(Mapfile*) const;
920*a9fa9459Szrj
921*a9fa9459Szrj // Dump statistical information to stderr.
922*a9fa9459Szrj void
923*a9fa9459Szrj print_stats() const;
924*a9fa9459Szrj
925*a9fa9459Szrj // A list of segments.
926*a9fa9459Szrj
927*a9fa9459Szrj typedef std::vector<Output_segment*> Segment_list;
928*a9fa9459Szrj
929*a9fa9459Szrj // A list of sections.
930*a9fa9459Szrj
931*a9fa9459Szrj typedef std::vector<Output_section*> Section_list;
932*a9fa9459Szrj
933*a9fa9459Szrj // The list of information to write out which is not attached to
934*a9fa9459Szrj // either a section or a segment.
935*a9fa9459Szrj typedef std::vector<Output_data*> Data_list;
936*a9fa9459Szrj
937*a9fa9459Szrj // Store the allocated sections into the section list. This is used
938*a9fa9459Szrj // by the linker script code.
939*a9fa9459Szrj void
940*a9fa9459Szrj get_allocated_sections(Section_list*) const;
941*a9fa9459Szrj
942*a9fa9459Szrj // Store the executable sections into the section list.
943*a9fa9459Szrj void
944*a9fa9459Szrj get_executable_sections(Section_list*) const;
945*a9fa9459Szrj
946*a9fa9459Szrj // Make a section for a linker script to hold data.
947*a9fa9459Szrj Output_section*
948*a9fa9459Szrj make_output_section_for_script(const char* name,
949*a9fa9459Szrj Script_sections::Section_type section_type);
950*a9fa9459Szrj
951*a9fa9459Szrj // Make a segment. This is used by the linker script code.
952*a9fa9459Szrj Output_segment*
953*a9fa9459Szrj make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags);
954*a9fa9459Szrj
955*a9fa9459Szrj // Return the number of segments.
956*a9fa9459Szrj size_t
segment_count()957*a9fa9459Szrj segment_count() const
958*a9fa9459Szrj { return this->segment_list_.size(); }
959*a9fa9459Szrj
960*a9fa9459Szrj // Map from section flags to segment flags.
961*a9fa9459Szrj static elfcpp::Elf_Word
962*a9fa9459Szrj section_flags_to_segment(elfcpp::Elf_Xword flags);
963*a9fa9459Szrj
964*a9fa9459Szrj // Attach sections to segments.
965*a9fa9459Szrj void
966*a9fa9459Szrj attach_sections_to_segments(const Target*);
967*a9fa9459Szrj
968*a9fa9459Szrj // For relaxation clean up, we need to know output section data created
969*a9fa9459Szrj // from a linker script.
970*a9fa9459Szrj void
new_output_section_data_from_script(Output_section_data * posd)971*a9fa9459Szrj new_output_section_data_from_script(Output_section_data* posd)
972*a9fa9459Szrj {
973*a9fa9459Szrj if (this->record_output_section_data_from_script_)
974*a9fa9459Szrj this->script_output_section_data_list_.push_back(posd);
975*a9fa9459Szrj }
976*a9fa9459Szrj
977*a9fa9459Szrj // Return section list.
978*a9fa9459Szrj const Section_list&
section_list()979*a9fa9459Szrj section_list() const
980*a9fa9459Szrj { return this->section_list_; }
981*a9fa9459Szrj
982*a9fa9459Szrj // Returns TRUE iff NAME (an input section from RELOBJ) will
983*a9fa9459Szrj // be mapped to an output section that should be KEPT.
984*a9fa9459Szrj bool
985*a9fa9459Szrj keep_input_section(const Relobj*, const char*);
986*a9fa9459Szrj
987*a9fa9459Szrj // Add a special output object that will be recreated afresh
988*a9fa9459Szrj // if there is another relaxation iteration.
989*a9fa9459Szrj void
add_relax_output(Output_data * data)990*a9fa9459Szrj add_relax_output(Output_data* data)
991*a9fa9459Szrj { this->relax_output_list_.push_back(data); }
992*a9fa9459Szrj
993*a9fa9459Szrj // Clear out (and free) everything added by add_relax_output.
994*a9fa9459Szrj void
995*a9fa9459Szrj reset_relax_output();
996*a9fa9459Szrj
997*a9fa9459Szrj private:
998*a9fa9459Szrj Layout(const Layout&);
999*a9fa9459Szrj Layout& operator=(const Layout&);
1000*a9fa9459Szrj
1001*a9fa9459Szrj // Mapping from input section names to output section names.
1002*a9fa9459Szrj struct Section_name_mapping
1003*a9fa9459Szrj {
1004*a9fa9459Szrj const char* from;
1005*a9fa9459Szrj int fromlen;
1006*a9fa9459Szrj const char* to;
1007*a9fa9459Szrj int tolen;
1008*a9fa9459Szrj };
1009*a9fa9459Szrj static const Section_name_mapping section_name_mapping[];
1010*a9fa9459Szrj static const int section_name_mapping_count;
1011*a9fa9459Szrj
1012*a9fa9459Szrj // During a relocatable link, a list of group sections and
1013*a9fa9459Szrj // signatures.
1014*a9fa9459Szrj struct Group_signature
1015*a9fa9459Szrj {
1016*a9fa9459Szrj // The group section.
1017*a9fa9459Szrj Output_section* section;
1018*a9fa9459Szrj // The signature.
1019*a9fa9459Szrj const char* signature;
1020*a9fa9459Szrj
Group_signatureGroup_signature1021*a9fa9459Szrj Group_signature()
1022*a9fa9459Szrj : section(NULL), signature(NULL)
1023*a9fa9459Szrj { }
1024*a9fa9459Szrj
Group_signatureGroup_signature1025*a9fa9459Szrj Group_signature(Output_section* sectiona, const char* signaturea)
1026*a9fa9459Szrj : section(sectiona), signature(signaturea)
1027*a9fa9459Szrj { }
1028*a9fa9459Szrj };
1029*a9fa9459Szrj typedef std::vector<Group_signature> Group_signatures;
1030*a9fa9459Szrj
1031*a9fa9459Szrj // Create a note section, filling in the header.
1032*a9fa9459Szrj Output_section*
1033*a9fa9459Szrj create_note(const char* name, int note_type, const char* section_name,
1034*a9fa9459Szrj size_t descsz, bool allocate, size_t* trailing_padding);
1035*a9fa9459Szrj
1036*a9fa9459Szrj // Create a note section for gold version.
1037*a9fa9459Szrj void
1038*a9fa9459Szrj create_gold_note();
1039*a9fa9459Szrj
1040*a9fa9459Szrj // Record whether the stack must be executable.
1041*a9fa9459Szrj void
1042*a9fa9459Szrj create_executable_stack_info();
1043*a9fa9459Szrj
1044*a9fa9459Szrj // Create a build ID note if needed.
1045*a9fa9459Szrj void
1046*a9fa9459Szrj create_build_id();
1047*a9fa9459Szrj
1048*a9fa9459Szrj // Link .stab and .stabstr sections.
1049*a9fa9459Szrj void
1050*a9fa9459Szrj link_stabs_sections();
1051*a9fa9459Szrj
1052*a9fa9459Szrj // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
1053*a9fa9459Szrj // for the next run of incremental linking to check what has changed.
1054*a9fa9459Szrj void
1055*a9fa9459Szrj create_incremental_info_sections(Symbol_table*);
1056*a9fa9459Szrj
1057*a9fa9459Szrj // Find the first read-only PT_LOAD segment, creating one if
1058*a9fa9459Szrj // necessary.
1059*a9fa9459Szrj Output_segment*
1060*a9fa9459Szrj find_first_load_seg(const Target*);
1061*a9fa9459Szrj
1062*a9fa9459Szrj // Count the local symbols in the regular symbol table and the dynamic
1063*a9fa9459Szrj // symbol table, and build the respective string pools.
1064*a9fa9459Szrj void
1065*a9fa9459Szrj count_local_symbols(const Task*, const Input_objects*);
1066*a9fa9459Szrj
1067*a9fa9459Szrj // Create the output sections for the symbol table.
1068*a9fa9459Szrj void
1069*a9fa9459Szrj create_symtab_sections(const Input_objects*, Symbol_table*,
1070*a9fa9459Szrj unsigned int, off_t*);
1071*a9fa9459Szrj
1072*a9fa9459Szrj // Create the .shstrtab section.
1073*a9fa9459Szrj Output_section*
1074*a9fa9459Szrj create_shstrtab();
1075*a9fa9459Szrj
1076*a9fa9459Szrj // Create the section header table.
1077*a9fa9459Szrj void
1078*a9fa9459Szrj create_shdrs(const Output_section* shstrtab_section, off_t*);
1079*a9fa9459Szrj
1080*a9fa9459Szrj // Create the dynamic symbol table.
1081*a9fa9459Szrj void
1082*a9fa9459Szrj create_dynamic_symtab(const Input_objects*, Symbol_table*,
1083*a9fa9459Szrj Output_section** pdynstr,
1084*a9fa9459Szrj unsigned int* plocal_dynamic_count,
1085*a9fa9459Szrj std::vector<Symbol*>* pdynamic_symbols,
1086*a9fa9459Szrj Versions* versions);
1087*a9fa9459Szrj
1088*a9fa9459Szrj // Assign offsets to each local portion of the dynamic symbol table.
1089*a9fa9459Szrj void
1090*a9fa9459Szrj assign_local_dynsym_offsets(const Input_objects*);
1091*a9fa9459Szrj
1092*a9fa9459Szrj // Finish the .dynamic section and PT_DYNAMIC segment.
1093*a9fa9459Szrj void
1094*a9fa9459Szrj finish_dynamic_section(const Input_objects*, const Symbol_table*);
1095*a9fa9459Szrj
1096*a9fa9459Szrj // Set the size of the _DYNAMIC symbol.
1097*a9fa9459Szrj void
1098*a9fa9459Szrj set_dynamic_symbol_size(const Symbol_table*);
1099*a9fa9459Szrj
1100*a9fa9459Szrj // Create the .interp section and PT_INTERP segment.
1101*a9fa9459Szrj void
1102*a9fa9459Szrj create_interp(const Target* target);
1103*a9fa9459Szrj
1104*a9fa9459Szrj // Create the version sections.
1105*a9fa9459Szrj void
1106*a9fa9459Szrj create_version_sections(const Versions*,
1107*a9fa9459Szrj const Symbol_table*,
1108*a9fa9459Szrj unsigned int local_symcount,
1109*a9fa9459Szrj const std::vector<Symbol*>& dynamic_symbols,
1110*a9fa9459Szrj const Output_section* dynstr);
1111*a9fa9459Szrj
1112*a9fa9459Szrj template<int size, bool big_endian>
1113*a9fa9459Szrj void
1114*a9fa9459Szrj sized_create_version_sections(const Versions* versions,
1115*a9fa9459Szrj const Symbol_table*,
1116*a9fa9459Szrj unsigned int local_symcount,
1117*a9fa9459Szrj const std::vector<Symbol*>& dynamic_symbols,
1118*a9fa9459Szrj const Output_section* dynstr);
1119*a9fa9459Szrj
1120*a9fa9459Szrj // Return whether to include this section in the link.
1121*a9fa9459Szrj template<int size, bool big_endian>
1122*a9fa9459Szrj bool
1123*a9fa9459Szrj include_section(Sized_relobj_file<size, big_endian>* object, const char* name,
1124*a9fa9459Szrj const elfcpp::Shdr<size, big_endian>&);
1125*a9fa9459Szrj
1126*a9fa9459Szrj // Return the output section name to use given an input section
1127*a9fa9459Szrj // name. Set *PLEN to the length of the name. *PLEN must be
1128*a9fa9459Szrj // initialized to the length of NAME.
1129*a9fa9459Szrj static const char*
1130*a9fa9459Szrj output_section_name(const Relobj*, const char* name, size_t* plen);
1131*a9fa9459Szrj
1132*a9fa9459Szrj // Return the number of allocated output sections.
1133*a9fa9459Szrj size_t
1134*a9fa9459Szrj allocated_output_section_count() const;
1135*a9fa9459Szrj
1136*a9fa9459Szrj // Return the output section for NAME, TYPE and FLAGS.
1137*a9fa9459Szrj Output_section*
1138*a9fa9459Szrj get_output_section(const char* name, Stringpool::Key name_key,
1139*a9fa9459Szrj elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
1140*a9fa9459Szrj Output_section_order order, bool is_relro);
1141*a9fa9459Szrj
1142*a9fa9459Szrj // Clear the input section flags that should not be copied to the
1143*a9fa9459Szrj // output section.
1144*a9fa9459Szrj elfcpp::Elf_Xword
1145*a9fa9459Szrj get_output_section_flags (elfcpp::Elf_Xword input_section_flags);
1146*a9fa9459Szrj
1147*a9fa9459Szrj // Choose the output section for NAME in RELOBJ.
1148*a9fa9459Szrj Output_section*
1149*a9fa9459Szrj choose_output_section(const Relobj* relobj, const char* name,
1150*a9fa9459Szrj elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
1151*a9fa9459Szrj bool is_input_section, Output_section_order order,
1152*a9fa9459Szrj bool is_relro);
1153*a9fa9459Szrj
1154*a9fa9459Szrj // Create a new Output_section.
1155*a9fa9459Szrj Output_section*
1156*a9fa9459Szrj make_output_section(const char* name, elfcpp::Elf_Word type,
1157*a9fa9459Szrj elfcpp::Elf_Xword flags, Output_section_order order,
1158*a9fa9459Szrj bool is_relro);
1159*a9fa9459Szrj
1160*a9fa9459Szrj // Attach a section to a segment.
1161*a9fa9459Szrj void
1162*a9fa9459Szrj attach_section_to_segment(const Target*, Output_section*);
1163*a9fa9459Szrj
1164*a9fa9459Szrj // Get section order.
1165*a9fa9459Szrj Output_section_order
1166*a9fa9459Szrj default_section_order(Output_section*, bool is_relro_local);
1167*a9fa9459Szrj
1168*a9fa9459Szrj // Attach an allocated section to a segment.
1169*a9fa9459Szrj void
1170*a9fa9459Szrj attach_allocated_section_to_segment(const Target*, Output_section*);
1171*a9fa9459Szrj
1172*a9fa9459Szrj // Make the .eh_frame section.
1173*a9fa9459Szrj Output_section*
1174*a9fa9459Szrj make_eh_frame_section(const Relobj*);
1175*a9fa9459Szrj
1176*a9fa9459Szrj // Set the final file offsets of all the segments.
1177*a9fa9459Szrj off_t
1178*a9fa9459Szrj set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
1179*a9fa9459Szrj
1180*a9fa9459Szrj // Set the file offsets of the sections when doing a relocatable
1181*a9fa9459Szrj // link.
1182*a9fa9459Szrj off_t
1183*a9fa9459Szrj set_relocatable_section_offsets(Output_data*, unsigned int* pshndx);
1184*a9fa9459Szrj
1185*a9fa9459Szrj // Set the final file offsets of all the sections not associated
1186*a9fa9459Szrj // with a segment. We set section offsets in three passes: the
1187*a9fa9459Szrj // first handles all allocated sections, the second sections that
1188*a9fa9459Szrj // require postprocessing, and the last the late-bound STRTAB
1189*a9fa9459Szrj // sections (probably only shstrtab, which is the one we care about
1190*a9fa9459Szrj // because it holds section names).
1191*a9fa9459Szrj enum Section_offset_pass
1192*a9fa9459Szrj {
1193*a9fa9459Szrj BEFORE_INPUT_SECTIONS_PASS,
1194*a9fa9459Szrj POSTPROCESSING_SECTIONS_PASS,
1195*a9fa9459Szrj STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1196*a9fa9459Szrj };
1197*a9fa9459Szrj off_t
1198*a9fa9459Szrj set_section_offsets(off_t, Section_offset_pass pass);
1199*a9fa9459Szrj
1200*a9fa9459Szrj // Set the final section indexes of all the sections not associated
1201*a9fa9459Szrj // with a segment. Returns the next unused index.
1202*a9fa9459Szrj unsigned int
1203*a9fa9459Szrj set_section_indexes(unsigned int pshndx);
1204*a9fa9459Szrj
1205*a9fa9459Szrj // Set the section addresses when using a script.
1206*a9fa9459Szrj Output_segment*
1207*a9fa9459Szrj set_section_addresses_from_script(Symbol_table*);
1208*a9fa9459Szrj
1209*a9fa9459Szrj // Find appropriate places or orphan sections in a script.
1210*a9fa9459Szrj void
1211*a9fa9459Szrj place_orphan_sections_in_script();
1212*a9fa9459Szrj
1213*a9fa9459Szrj // Return whether SEG1 comes before SEG2 in the output file.
1214*a9fa9459Szrj bool
1215*a9fa9459Szrj segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
1216*a9fa9459Szrj
1217*a9fa9459Szrj // Use to save and restore segments during relaxation.
1218*a9fa9459Szrj typedef Unordered_map<const Output_segment*, const Output_segment*>
1219*a9fa9459Szrj Segment_states;
1220*a9fa9459Szrj
1221*a9fa9459Szrj // Save states of current output segments.
1222*a9fa9459Szrj void
1223*a9fa9459Szrj save_segments(Segment_states*);
1224*a9fa9459Szrj
1225*a9fa9459Szrj // Restore output segment states.
1226*a9fa9459Szrj void
1227*a9fa9459Szrj restore_segments(const Segment_states*);
1228*a9fa9459Szrj
1229*a9fa9459Szrj // Clean up after relaxation so that it is possible to lay out the
1230*a9fa9459Szrj // sections and segments again.
1231*a9fa9459Szrj void
1232*a9fa9459Szrj clean_up_after_relaxation();
1233*a9fa9459Szrj
1234*a9fa9459Szrj // Doing preparation work for relaxation. This is factored out to make
1235*a9fa9459Szrj // Layout::finalized a bit smaller and easier to read.
1236*a9fa9459Szrj void
1237*a9fa9459Szrj prepare_for_relaxation();
1238*a9fa9459Szrj
1239*a9fa9459Szrj // Main body of the relaxation loop, which lays out the section.
1240*a9fa9459Szrj off_t
1241*a9fa9459Szrj relaxation_loop_body(int, Target*, Symbol_table*, Output_segment**,
1242*a9fa9459Szrj Output_segment*, Output_segment_headers*,
1243*a9fa9459Szrj Output_file_header*, unsigned int*);
1244*a9fa9459Szrj
1245*a9fa9459Szrj // A mapping used for kept comdats/.gnu.linkonce group signatures.
1246*a9fa9459Szrj typedef Unordered_map<std::string, Kept_section> Signatures;
1247*a9fa9459Szrj
1248*a9fa9459Szrj // Mapping from input section name/type/flags to output section. We
1249*a9fa9459Szrj // use canonicalized strings here.
1250*a9fa9459Szrj
1251*a9fa9459Szrj typedef std::pair<Stringpool::Key,
1252*a9fa9459Szrj std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
1253*a9fa9459Szrj
1254*a9fa9459Szrj struct Hash_key
1255*a9fa9459Szrj {
1256*a9fa9459Szrj size_t
1257*a9fa9459Szrj operator()(const Key& k) const;
1258*a9fa9459Szrj };
1259*a9fa9459Szrj
1260*a9fa9459Szrj typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
1261*a9fa9459Szrj
1262*a9fa9459Szrj // A comparison class for segments.
1263*a9fa9459Szrj
1264*a9fa9459Szrj class Compare_segments
1265*a9fa9459Szrj {
1266*a9fa9459Szrj public:
Compare_segments(Layout * layout)1267*a9fa9459Szrj Compare_segments(Layout* layout)
1268*a9fa9459Szrj : layout_(layout)
1269*a9fa9459Szrj { }
1270*a9fa9459Szrj
1271*a9fa9459Szrj bool
operator()1272*a9fa9459Szrj operator()(const Output_segment* seg1, const Output_segment* seg2)
1273*a9fa9459Szrj { return this->layout_->segment_precedes(seg1, seg2); }
1274*a9fa9459Szrj
1275*a9fa9459Szrj private:
1276*a9fa9459Szrj Layout* layout_;
1277*a9fa9459Szrj };
1278*a9fa9459Szrj
1279*a9fa9459Szrj typedef std::vector<Output_section_data*> Output_section_data_list;
1280*a9fa9459Szrj
1281*a9fa9459Szrj // Debug checker class.
1282*a9fa9459Szrj class Relaxation_debug_check
1283*a9fa9459Szrj {
1284*a9fa9459Szrj public:
Relaxation_debug_check()1285*a9fa9459Szrj Relaxation_debug_check()
1286*a9fa9459Szrj : section_infos_()
1287*a9fa9459Szrj { }
1288*a9fa9459Szrj
1289*a9fa9459Szrj // Check that sections and special data are in reset states.
1290*a9fa9459Szrj void
1291*a9fa9459Szrj check_output_data_for_reset_values(const Layout::Section_list&,
1292*a9fa9459Szrj const Layout::Data_list& special_outputs,
1293*a9fa9459Szrj const Layout::Data_list& relax_outputs);
1294*a9fa9459Szrj
1295*a9fa9459Szrj // Record information of a section list.
1296*a9fa9459Szrj void
1297*a9fa9459Szrj read_sections(const Layout::Section_list&);
1298*a9fa9459Szrj
1299*a9fa9459Szrj // Verify a section list with recorded information.
1300*a9fa9459Szrj void
1301*a9fa9459Szrj verify_sections(const Layout::Section_list&);
1302*a9fa9459Szrj
1303*a9fa9459Szrj private:
1304*a9fa9459Szrj // Information we care about a section.
1305*a9fa9459Szrj struct Section_info
1306*a9fa9459Szrj {
1307*a9fa9459Szrj // Output section described by this.
1308*a9fa9459Szrj Output_section* output_section;
1309*a9fa9459Szrj // Load address.
1310*a9fa9459Szrj uint64_t address;
1311*a9fa9459Szrj // Data size.
1312*a9fa9459Szrj off_t data_size;
1313*a9fa9459Szrj // File offset.
1314*a9fa9459Szrj off_t offset;
1315*a9fa9459Szrj };
1316*a9fa9459Szrj
1317*a9fa9459Szrj // Section information.
1318*a9fa9459Szrj std::vector<Section_info> section_infos_;
1319*a9fa9459Szrj };
1320*a9fa9459Szrj
1321*a9fa9459Szrj // The number of input files, for sizing tables.
1322*a9fa9459Szrj int number_of_input_files_;
1323*a9fa9459Szrj // Information set by scripts or by command line options.
1324*a9fa9459Szrj Script_options* script_options_;
1325*a9fa9459Szrj // The output section names.
1326*a9fa9459Szrj Stringpool namepool_;
1327*a9fa9459Szrj // The output symbol names.
1328*a9fa9459Szrj Stringpool sympool_;
1329*a9fa9459Szrj // The dynamic strings, if needed.
1330*a9fa9459Szrj Stringpool dynpool_;
1331*a9fa9459Szrj // The list of group sections and linkonce sections which we have seen.
1332*a9fa9459Szrj Signatures signatures_;
1333*a9fa9459Szrj // The mapping from input section name/type/flags to output sections.
1334*a9fa9459Szrj Section_name_map section_name_map_;
1335*a9fa9459Szrj // The list of output segments.
1336*a9fa9459Szrj Segment_list segment_list_;
1337*a9fa9459Szrj // The list of output sections.
1338*a9fa9459Szrj Section_list section_list_;
1339*a9fa9459Szrj // The list of output sections which are not attached to any output
1340*a9fa9459Szrj // segment.
1341*a9fa9459Szrj Section_list unattached_section_list_;
1342*a9fa9459Szrj // The list of unattached Output_data objects which require special
1343*a9fa9459Szrj // handling because they are not Output_sections.
1344*a9fa9459Szrj Data_list special_output_list_;
1345*a9fa9459Szrj // Like special_output_list_, but cleared and recreated on each
1346*a9fa9459Szrj // iteration of relaxation.
1347*a9fa9459Szrj Data_list relax_output_list_;
1348*a9fa9459Szrj // The section headers.
1349*a9fa9459Szrj Output_section_headers* section_headers_;
1350*a9fa9459Szrj // A pointer to the PT_TLS segment if there is one.
1351*a9fa9459Szrj Output_segment* tls_segment_;
1352*a9fa9459Szrj // A pointer to the PT_GNU_RELRO segment if there is one.
1353*a9fa9459Szrj Output_segment* relro_segment_;
1354*a9fa9459Szrj // A pointer to the PT_INTERP segment if there is one.
1355*a9fa9459Szrj Output_segment* interp_segment_;
1356*a9fa9459Szrj // A backend may increase the size of the PT_GNU_RELRO segment if
1357*a9fa9459Szrj // there is one. This is the amount to increase it by.
1358*a9fa9459Szrj unsigned int increase_relro_;
1359*a9fa9459Szrj // The SHT_SYMTAB output section.
1360*a9fa9459Szrj Output_section* symtab_section_;
1361*a9fa9459Szrj // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one.
1362*a9fa9459Szrj Output_symtab_xindex* symtab_xindex_;
1363*a9fa9459Szrj // The SHT_DYNSYM output section if there is one.
1364*a9fa9459Szrj Output_section* dynsym_section_;
1365*a9fa9459Szrj // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one.
1366*a9fa9459Szrj Output_symtab_xindex* dynsym_xindex_;
1367*a9fa9459Szrj // The SHT_DYNAMIC output section if there is one.
1368*a9fa9459Szrj Output_section* dynamic_section_;
1369*a9fa9459Szrj // The _DYNAMIC symbol if there is one.
1370*a9fa9459Szrj Symbol* dynamic_symbol_;
1371*a9fa9459Szrj // The dynamic data which goes into dynamic_section_.
1372*a9fa9459Szrj Output_data_dynamic* dynamic_data_;
1373*a9fa9459Szrj // The exception frame output section if there is one.
1374*a9fa9459Szrj Output_section* eh_frame_section_;
1375*a9fa9459Szrj // The exception frame data for eh_frame_section_.
1376*a9fa9459Szrj Eh_frame* eh_frame_data_;
1377*a9fa9459Szrj // Whether we have added eh_frame_data_ to the .eh_frame section.
1378*a9fa9459Szrj bool added_eh_frame_data_;
1379*a9fa9459Szrj // The exception frame header output section if there is one.
1380*a9fa9459Szrj Output_section* eh_frame_hdr_section_;
1381*a9fa9459Szrj // The data for the .gdb_index section.
1382*a9fa9459Szrj Gdb_index* gdb_index_data_;
1383*a9fa9459Szrj // The space for the build ID checksum if there is one.
1384*a9fa9459Szrj Output_section_data* build_id_note_;
1385*a9fa9459Szrj // The output section containing dwarf abbreviations
1386*a9fa9459Szrj Output_reduced_debug_abbrev_section* debug_abbrev_;
1387*a9fa9459Szrj // The output section containing the dwarf debug info tree
1388*a9fa9459Szrj Output_reduced_debug_info_section* debug_info_;
1389*a9fa9459Szrj // A list of group sections and their signatures.
1390*a9fa9459Szrj Group_signatures group_signatures_;
1391*a9fa9459Szrj // The size of the output file.
1392*a9fa9459Szrj off_t output_file_size_;
1393*a9fa9459Szrj // Whether we have added an input section to an output section.
1394*a9fa9459Szrj bool have_added_input_section_;
1395*a9fa9459Szrj // Whether we have attached the sections to the segments.
1396*a9fa9459Szrj bool sections_are_attached_;
1397*a9fa9459Szrj // Whether we have seen an object file marked to require an
1398*a9fa9459Szrj // executable stack.
1399*a9fa9459Szrj bool input_requires_executable_stack_;
1400*a9fa9459Szrj // Whether we have seen at least one object file with an executable
1401*a9fa9459Szrj // stack marker.
1402*a9fa9459Szrj bool input_with_gnu_stack_note_;
1403*a9fa9459Szrj // Whether we have seen at least one object file without an
1404*a9fa9459Szrj // executable stack marker.
1405*a9fa9459Szrj bool input_without_gnu_stack_note_;
1406*a9fa9459Szrj // Whether we have seen an object file that uses the static TLS model.
1407*a9fa9459Szrj bool has_static_tls_;
1408*a9fa9459Szrj // Whether any sections require postprocessing.
1409*a9fa9459Szrj bool any_postprocessing_sections_;
1410*a9fa9459Szrj // Whether we have resized the signatures_ hash table.
1411*a9fa9459Szrj bool resized_signatures_;
1412*a9fa9459Szrj // Whether we have created a .stab*str output section.
1413*a9fa9459Szrj bool have_stabstr_section_;
1414*a9fa9459Szrj // True if the input sections in the output sections should be sorted
1415*a9fa9459Szrj // as specified in a section ordering file.
1416*a9fa9459Szrj bool section_ordering_specified_;
1417*a9fa9459Szrj // True if some input sections need to be mapped to a unique segment,
1418*a9fa9459Szrj // after being mapped to a unique Output_section.
1419*a9fa9459Szrj bool unique_segment_for_sections_specified_;
1420*a9fa9459Szrj // In incremental build, holds information check the inputs and build the
1421*a9fa9459Szrj // .gnu_incremental_inputs section.
1422*a9fa9459Szrj Incremental_inputs* incremental_inputs_;
1423*a9fa9459Szrj // Whether we record output section data created in script
1424*a9fa9459Szrj bool record_output_section_data_from_script_;
1425*a9fa9459Szrj // List of output data that needs to be removed at relaxation clean up.
1426*a9fa9459Szrj Output_section_data_list script_output_section_data_list_;
1427*a9fa9459Szrj // Structure to save segment states before entering the relaxation loop.
1428*a9fa9459Szrj Segment_states* segment_states_;
1429*a9fa9459Szrj // A relaxation debug checker. We only create one when in debugging mode.
1430*a9fa9459Szrj Relaxation_debug_check* relaxation_debug_check_;
1431*a9fa9459Szrj // Plugins specify section_ordering using this map. This is set in
1432*a9fa9459Szrj // update_section_order in plugin.cc
1433*a9fa9459Szrj std::map<Section_id, unsigned int> section_order_map_;
1434*a9fa9459Szrj // This maps an input section to a unique segment. This is done by first
1435*a9fa9459Szrj // placing such input sections in unique output sections and then mapping
1436*a9fa9459Szrj // the output section to a unique segment. Unique_segment_info stores
1437*a9fa9459Szrj // any additional flags and alignment of the new segment.
1438*a9fa9459Szrj Section_segment_map section_segment_map_;
1439*a9fa9459Szrj // Hash a pattern to its position in the section ordering file.
1440*a9fa9459Szrj Unordered_map<std::string, unsigned int> input_section_position_;
1441*a9fa9459Szrj // Vector of glob only patterns in the section_ordering file.
1442*a9fa9459Szrj std::vector<std::string> input_section_glob_;
1443*a9fa9459Szrj // For incremental links, the base file to be modified.
1444*a9fa9459Szrj Incremental_binary* incremental_base_;
1445*a9fa9459Szrj // For incremental links, a list of free space within the file.
1446*a9fa9459Szrj Free_list free_list_;
1447*a9fa9459Szrj };
1448*a9fa9459Szrj
1449*a9fa9459Szrj // This task handles writing out data in output sections which is not
1450*a9fa9459Szrj // part of an input section, or which requires special handling. When
1451*a9fa9459Szrj // this is done, it unblocks both output_sections_blocker and
1452*a9fa9459Szrj // final_blocker.
1453*a9fa9459Szrj
1454*a9fa9459Szrj class Write_sections_task : public Task
1455*a9fa9459Szrj {
1456*a9fa9459Szrj public:
Write_sections_task(const Layout * layout,Output_file * of,Task_token * output_sections_blocker,Task_token * input_sections_blocker,Task_token * final_blocker)1457*a9fa9459Szrj Write_sections_task(const Layout* layout, Output_file* of,
1458*a9fa9459Szrj Task_token* output_sections_blocker,
1459*a9fa9459Szrj Task_token* input_sections_blocker,
1460*a9fa9459Szrj Task_token* final_blocker)
1461*a9fa9459Szrj : layout_(layout), of_(of),
1462*a9fa9459Szrj output_sections_blocker_(output_sections_blocker),
1463*a9fa9459Szrj input_sections_blocker_(input_sections_blocker),
1464*a9fa9459Szrj final_blocker_(final_blocker)
1465*a9fa9459Szrj { }
1466*a9fa9459Szrj
1467*a9fa9459Szrj // The standard Task methods.
1468*a9fa9459Szrj
1469*a9fa9459Szrj Task_token*
1470*a9fa9459Szrj is_runnable();
1471*a9fa9459Szrj
1472*a9fa9459Szrj void
1473*a9fa9459Szrj locks(Task_locker*);
1474*a9fa9459Szrj
1475*a9fa9459Szrj void
1476*a9fa9459Szrj run(Workqueue*);
1477*a9fa9459Szrj
1478*a9fa9459Szrj std::string
get_name()1479*a9fa9459Szrj get_name() const
1480*a9fa9459Szrj { return "Write_sections_task"; }
1481*a9fa9459Szrj
1482*a9fa9459Szrj private:
1483*a9fa9459Szrj class Write_sections_locker;
1484*a9fa9459Szrj
1485*a9fa9459Szrj const Layout* layout_;
1486*a9fa9459Szrj Output_file* of_;
1487*a9fa9459Szrj Task_token* output_sections_blocker_;
1488*a9fa9459Szrj Task_token* input_sections_blocker_;
1489*a9fa9459Szrj Task_token* final_blocker_;
1490*a9fa9459Szrj };
1491*a9fa9459Szrj
1492*a9fa9459Szrj // This task handles writing out data which is not part of a section
1493*a9fa9459Szrj // or segment.
1494*a9fa9459Szrj
1495*a9fa9459Szrj class Write_data_task : public Task
1496*a9fa9459Szrj {
1497*a9fa9459Szrj public:
Write_data_task(const Layout * layout,const Symbol_table * symtab,Output_file * of,Task_token * final_blocker)1498*a9fa9459Szrj Write_data_task(const Layout* layout, const Symbol_table* symtab,
1499*a9fa9459Szrj Output_file* of, Task_token* final_blocker)
1500*a9fa9459Szrj : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker)
1501*a9fa9459Szrj { }
1502*a9fa9459Szrj
1503*a9fa9459Szrj // The standard Task methods.
1504*a9fa9459Szrj
1505*a9fa9459Szrj Task_token*
1506*a9fa9459Szrj is_runnable();
1507*a9fa9459Szrj
1508*a9fa9459Szrj void
1509*a9fa9459Szrj locks(Task_locker*);
1510*a9fa9459Szrj
1511*a9fa9459Szrj void
1512*a9fa9459Szrj run(Workqueue*);
1513*a9fa9459Szrj
1514*a9fa9459Szrj std::string
get_name()1515*a9fa9459Szrj get_name() const
1516*a9fa9459Szrj { return "Write_data_task"; }
1517*a9fa9459Szrj
1518*a9fa9459Szrj private:
1519*a9fa9459Szrj const Layout* layout_;
1520*a9fa9459Szrj const Symbol_table* symtab_;
1521*a9fa9459Szrj Output_file* of_;
1522*a9fa9459Szrj Task_token* final_blocker_;
1523*a9fa9459Szrj };
1524*a9fa9459Szrj
1525*a9fa9459Szrj // This task handles writing out the global symbols.
1526*a9fa9459Szrj
1527*a9fa9459Szrj class Write_symbols_task : public Task
1528*a9fa9459Szrj {
1529*a9fa9459Szrj public:
Write_symbols_task(const Layout * layout,const Symbol_table * symtab,const Input_objects *,const Stringpool * sympool,const Stringpool * dynpool,Output_file * of,Task_token * final_blocker)1530*a9fa9459Szrj Write_symbols_task(const Layout* layout, const Symbol_table* symtab,
1531*a9fa9459Szrj const Input_objects* /*input_objects*/,
1532*a9fa9459Szrj const Stringpool* sympool, const Stringpool* dynpool,
1533*a9fa9459Szrj Output_file* of, Task_token* final_blocker)
1534*a9fa9459Szrj : layout_(layout), symtab_(symtab),
1535*a9fa9459Szrj sympool_(sympool), dynpool_(dynpool), of_(of),
1536*a9fa9459Szrj final_blocker_(final_blocker)
1537*a9fa9459Szrj { }
1538*a9fa9459Szrj
1539*a9fa9459Szrj // The standard Task methods.
1540*a9fa9459Szrj
1541*a9fa9459Szrj Task_token*
1542*a9fa9459Szrj is_runnable();
1543*a9fa9459Szrj
1544*a9fa9459Szrj void
1545*a9fa9459Szrj locks(Task_locker*);
1546*a9fa9459Szrj
1547*a9fa9459Szrj void
1548*a9fa9459Szrj run(Workqueue*);
1549*a9fa9459Szrj
1550*a9fa9459Szrj std::string
get_name()1551*a9fa9459Szrj get_name() const
1552*a9fa9459Szrj { return "Write_symbols_task"; }
1553*a9fa9459Szrj
1554*a9fa9459Szrj private:
1555*a9fa9459Szrj const Layout* layout_;
1556*a9fa9459Szrj const Symbol_table* symtab_;
1557*a9fa9459Szrj const Stringpool* sympool_;
1558*a9fa9459Szrj const Stringpool* dynpool_;
1559*a9fa9459Szrj Output_file* of_;
1560*a9fa9459Szrj Task_token* final_blocker_;
1561*a9fa9459Szrj };
1562*a9fa9459Szrj
1563*a9fa9459Szrj // This task handles writing out data in output sections which can't
1564*a9fa9459Szrj // be written out until all the input sections have been handled.
1565*a9fa9459Szrj // This is for sections whose contents is based on the contents of
1566*a9fa9459Szrj // other output sections.
1567*a9fa9459Szrj
1568*a9fa9459Szrj class Write_after_input_sections_task : public Task
1569*a9fa9459Szrj {
1570*a9fa9459Szrj public:
Write_after_input_sections_task(Layout * layout,Output_file * of,Task_token * input_sections_blocker,Task_token * final_blocker)1571*a9fa9459Szrj Write_after_input_sections_task(Layout* layout, Output_file* of,
1572*a9fa9459Szrj Task_token* input_sections_blocker,
1573*a9fa9459Szrj Task_token* final_blocker)
1574*a9fa9459Szrj : layout_(layout), of_(of),
1575*a9fa9459Szrj input_sections_blocker_(input_sections_blocker),
1576*a9fa9459Szrj final_blocker_(final_blocker)
1577*a9fa9459Szrj { }
1578*a9fa9459Szrj
1579*a9fa9459Szrj // The standard Task methods.
1580*a9fa9459Szrj
1581*a9fa9459Szrj Task_token*
1582*a9fa9459Szrj is_runnable();
1583*a9fa9459Szrj
1584*a9fa9459Szrj void
1585*a9fa9459Szrj locks(Task_locker*);
1586*a9fa9459Szrj
1587*a9fa9459Szrj void
1588*a9fa9459Szrj run(Workqueue*);
1589*a9fa9459Szrj
1590*a9fa9459Szrj std::string
get_name()1591*a9fa9459Szrj get_name() const
1592*a9fa9459Szrj { return "Write_after_input_sections_task"; }
1593*a9fa9459Szrj
1594*a9fa9459Szrj private:
1595*a9fa9459Szrj Layout* layout_;
1596*a9fa9459Szrj Output_file* of_;
1597*a9fa9459Szrj Task_token* input_sections_blocker_;
1598*a9fa9459Szrj Task_token* final_blocker_;
1599*a9fa9459Szrj };
1600*a9fa9459Szrj
1601*a9fa9459Szrj // This task function handles computation of the build id.
1602*a9fa9459Szrj // When using --build-id=tree, it schedules the tasks that
1603*a9fa9459Szrj // compute the hashes for each chunk of the file. This task
1604*a9fa9459Szrj // cannot run until we have finalized the size of the output
1605*a9fa9459Szrj // file, after the completion of Write_after_input_sections_task.
1606*a9fa9459Szrj
1607*a9fa9459Szrj class Build_id_task_runner : public Task_function_runner
1608*a9fa9459Szrj {
1609*a9fa9459Szrj public:
Build_id_task_runner(const General_options * options,const Layout * layout,Output_file * of)1610*a9fa9459Szrj Build_id_task_runner(const General_options* options, const Layout* layout,
1611*a9fa9459Szrj Output_file* of)
1612*a9fa9459Szrj : options_(options), layout_(layout), of_(of)
1613*a9fa9459Szrj { }
1614*a9fa9459Szrj
1615*a9fa9459Szrj // Run the operation.
1616*a9fa9459Szrj void
1617*a9fa9459Szrj run(Workqueue*, const Task*);
1618*a9fa9459Szrj
1619*a9fa9459Szrj private:
1620*a9fa9459Szrj const General_options* options_;
1621*a9fa9459Szrj const Layout* layout_;
1622*a9fa9459Szrj Output_file* of_;
1623*a9fa9459Szrj };
1624*a9fa9459Szrj
1625*a9fa9459Szrj // This task function handles closing the file.
1626*a9fa9459Szrj
1627*a9fa9459Szrj class Close_task_runner : public Task_function_runner
1628*a9fa9459Szrj {
1629*a9fa9459Szrj public:
Close_task_runner(const General_options * options,const Layout * layout,Output_file * of,unsigned char * array_of_hashes,size_t size_of_hashes)1630*a9fa9459Szrj Close_task_runner(const General_options* options, const Layout* layout,
1631*a9fa9459Szrj Output_file* of, unsigned char* array_of_hashes,
1632*a9fa9459Szrj size_t size_of_hashes)
1633*a9fa9459Szrj : options_(options), layout_(layout), of_(of),
1634*a9fa9459Szrj array_of_hashes_(array_of_hashes), size_of_hashes_(size_of_hashes)
1635*a9fa9459Szrj { }
1636*a9fa9459Szrj
1637*a9fa9459Szrj // Run the operation.
1638*a9fa9459Szrj void
1639*a9fa9459Szrj run(Workqueue*, const Task*);
1640*a9fa9459Szrj
1641*a9fa9459Szrj private:
1642*a9fa9459Szrj const General_options* options_;
1643*a9fa9459Szrj const Layout* layout_;
1644*a9fa9459Szrj Output_file* of_;
1645*a9fa9459Szrj unsigned char* const array_of_hashes_;
1646*a9fa9459Szrj const size_t size_of_hashes_;
1647*a9fa9459Szrj };
1648*a9fa9459Szrj
1649*a9fa9459Szrj // A small helper function to align an address.
1650*a9fa9459Szrj
1651*a9fa9459Szrj inline uint64_t
align_address(uint64_t address,uint64_t addralign)1652*a9fa9459Szrj align_address(uint64_t address, uint64_t addralign)
1653*a9fa9459Szrj {
1654*a9fa9459Szrj if (addralign != 0)
1655*a9fa9459Szrj address = (address + addralign - 1) &~ (addralign - 1);
1656*a9fa9459Szrj return address;
1657*a9fa9459Szrj }
1658*a9fa9459Szrj
1659*a9fa9459Szrj } // End namespace gold.
1660*a9fa9459Szrj
1661*a9fa9459Szrj #endif // !defined(GOLD_LAYOUT_H)
1662