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