1*fae548d3Szrj // layout.cc -- lay out output file sections for gold
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 #include "gold.h"
24*fae548d3Szrj 
25*fae548d3Szrj #include <cerrno>
26*fae548d3Szrj #include <cstring>
27*fae548d3Szrj #include <algorithm>
28*fae548d3Szrj #include <iostream>
29*fae548d3Szrj #include <fstream>
30*fae548d3Szrj #include <utility>
31*fae548d3Szrj #include <fcntl.h>
32*fae548d3Szrj #include <fnmatch.h>
33*fae548d3Szrj #include <unistd.h>
34*fae548d3Szrj #include "libiberty.h"
35*fae548d3Szrj #include "md5.h"
36*fae548d3Szrj #include "sha1.h"
37*fae548d3Szrj #ifdef __MINGW32__
38*fae548d3Szrj #include <windows.h>
39*fae548d3Szrj #include <rpcdce.h>
40*fae548d3Szrj #endif
41*fae548d3Szrj 
42*fae548d3Szrj #include "parameters.h"
43*fae548d3Szrj #include "options.h"
44*fae548d3Szrj #include "mapfile.h"
45*fae548d3Szrj #include "script.h"
46*fae548d3Szrj #include "script-sections.h"
47*fae548d3Szrj #include "output.h"
48*fae548d3Szrj #include "symtab.h"
49*fae548d3Szrj #include "dynobj.h"
50*fae548d3Szrj #include "ehframe.h"
51*fae548d3Szrj #include "gdb-index.h"
52*fae548d3Szrj #include "compressed_output.h"
53*fae548d3Szrj #include "reduced_debug_output.h"
54*fae548d3Szrj #include "object.h"
55*fae548d3Szrj #include "reloc.h"
56*fae548d3Szrj #include "descriptors.h"
57*fae548d3Szrj #include "plugin.h"
58*fae548d3Szrj #include "incremental.h"
59*fae548d3Szrj #include "layout.h"
60*fae548d3Szrj 
61*fae548d3Szrj namespace gold
62*fae548d3Szrj {
63*fae548d3Szrj 
64*fae548d3Szrj // Class Free_list.
65*fae548d3Szrj 
66*fae548d3Szrj // The total number of free lists used.
67*fae548d3Szrj unsigned int Free_list::num_lists = 0;
68*fae548d3Szrj // The total number of free list nodes used.
69*fae548d3Szrj unsigned int Free_list::num_nodes = 0;
70*fae548d3Szrj // The total number of calls to Free_list::remove.
71*fae548d3Szrj unsigned int Free_list::num_removes = 0;
72*fae548d3Szrj // The total number of nodes visited during calls to Free_list::remove.
73*fae548d3Szrj unsigned int Free_list::num_remove_visits = 0;
74*fae548d3Szrj // The total number of calls to Free_list::allocate.
75*fae548d3Szrj unsigned int Free_list::num_allocates = 0;
76*fae548d3Szrj // The total number of nodes visited during calls to Free_list::allocate.
77*fae548d3Szrj unsigned int Free_list::num_allocate_visits = 0;
78*fae548d3Szrj 
79*fae548d3Szrj // Initialize the free list.  Creates a single free list node that
80*fae548d3Szrj // describes the entire region of length LEN.  If EXTEND is true,
81*fae548d3Szrj // allocate() is allowed to extend the region beyond its initial
82*fae548d3Szrj // length.
83*fae548d3Szrj 
84*fae548d3Szrj void
init(off_t len,bool extend)85*fae548d3Szrj Free_list::init(off_t len, bool extend)
86*fae548d3Szrj {
87*fae548d3Szrj   this->list_.push_front(Free_list_node(0, len));
88*fae548d3Szrj   this->last_remove_ = this->list_.begin();
89*fae548d3Szrj   this->extend_ = extend;
90*fae548d3Szrj   this->length_ = len;
91*fae548d3Szrj   ++Free_list::num_lists;
92*fae548d3Szrj   ++Free_list::num_nodes;
93*fae548d3Szrj }
94*fae548d3Szrj 
95*fae548d3Szrj // Remove a chunk from the free list.  Because we start with a single
96*fae548d3Szrj // node that covers the entire section, and remove chunks from it one
97*fae548d3Szrj // at a time, we do not need to coalesce chunks or handle cases that
98*fae548d3Szrj // span more than one free node.  We expect to remove chunks from the
99*fae548d3Szrj // free list in order, and we expect to have only a few chunks of free
100*fae548d3Szrj // space left (corresponding to files that have changed since the last
101*fae548d3Szrj // incremental link), so a simple linear list should provide sufficient
102*fae548d3Szrj // performance.
103*fae548d3Szrj 
104*fae548d3Szrj void
remove(off_t start,off_t end)105*fae548d3Szrj Free_list::remove(off_t start, off_t end)
106*fae548d3Szrj {
107*fae548d3Szrj   if (start == end)
108*fae548d3Szrj     return;
109*fae548d3Szrj   gold_assert(start < end);
110*fae548d3Szrj 
111*fae548d3Szrj   ++Free_list::num_removes;
112*fae548d3Szrj 
113*fae548d3Szrj   Iterator p = this->last_remove_;
114*fae548d3Szrj   if (p->start_ > start)
115*fae548d3Szrj     p = this->list_.begin();
116*fae548d3Szrj 
117*fae548d3Szrj   for (; p != this->list_.end(); ++p)
118*fae548d3Szrj     {
119*fae548d3Szrj       ++Free_list::num_remove_visits;
120*fae548d3Szrj       // Find a node that wholly contains the indicated region.
121*fae548d3Szrj       if (p->start_ <= start && p->end_ >= end)
122*fae548d3Szrj 	{
123*fae548d3Szrj 	  // Case 1: the indicated region spans the whole node.
124*fae548d3Szrj 	  // Add some fuzz to avoid creating tiny free chunks.
125*fae548d3Szrj 	  if (p->start_ + 3 >= start && p->end_ <= end + 3)
126*fae548d3Szrj 	    p = this->list_.erase(p);
127*fae548d3Szrj 	  // Case 2: remove a chunk from the start of the node.
128*fae548d3Szrj 	  else if (p->start_ + 3 >= start)
129*fae548d3Szrj 	    p->start_ = end;
130*fae548d3Szrj 	  // Case 3: remove a chunk from the end of the node.
131*fae548d3Szrj 	  else if (p->end_ <= end + 3)
132*fae548d3Szrj 	    p->end_ = start;
133*fae548d3Szrj 	  // Case 4: remove a chunk from the middle, and split
134*fae548d3Szrj 	  // the node into two.
135*fae548d3Szrj 	  else
136*fae548d3Szrj 	    {
137*fae548d3Szrj 	      Free_list_node newnode(p->start_, start);
138*fae548d3Szrj 	      p->start_ = end;
139*fae548d3Szrj 	      this->list_.insert(p, newnode);
140*fae548d3Szrj 	      ++Free_list::num_nodes;
141*fae548d3Szrj 	    }
142*fae548d3Szrj 	  this->last_remove_ = p;
143*fae548d3Szrj 	  return;
144*fae548d3Szrj 	}
145*fae548d3Szrj     }
146*fae548d3Szrj 
147*fae548d3Szrj   // Did not find a node containing the given chunk.  This could happen
148*fae548d3Szrj   // because a small chunk was already removed due to the fuzz.
149*fae548d3Szrj   gold_debug(DEBUG_INCREMENTAL,
150*fae548d3Szrj 	     "Free_list::remove(%d,%d) not found",
151*fae548d3Szrj 	     static_cast<int>(start), static_cast<int>(end));
152*fae548d3Szrj }
153*fae548d3Szrj 
154*fae548d3Szrj // Allocate a chunk of size LEN from the free list.  Returns -1ULL
155*fae548d3Szrj // if a sufficiently large chunk of free space is not found.
156*fae548d3Szrj // We use a simple first-fit algorithm.
157*fae548d3Szrj 
158*fae548d3Szrj off_t
allocate(off_t len,uint64_t align,off_t minoff)159*fae548d3Szrj Free_list::allocate(off_t len, uint64_t align, off_t minoff)
160*fae548d3Szrj {
161*fae548d3Szrj   gold_debug(DEBUG_INCREMENTAL,
162*fae548d3Szrj 	     "Free_list::allocate(%08lx, %d, %08lx)",
163*fae548d3Szrj 	     static_cast<long>(len), static_cast<int>(align),
164*fae548d3Szrj 	     static_cast<long>(minoff));
165*fae548d3Szrj   if (len == 0)
166*fae548d3Szrj     return align_address(minoff, align);
167*fae548d3Szrj 
168*fae548d3Szrj   ++Free_list::num_allocates;
169*fae548d3Szrj 
170*fae548d3Szrj   // We usually want to drop free chunks smaller than 4 bytes.
171*fae548d3Szrj   // If we need to guarantee a minimum hole size, though, we need
172*fae548d3Szrj   // to keep track of all free chunks.
173*fae548d3Szrj   const int fuzz = this->min_hole_ > 0 ? 0 : 3;
174*fae548d3Szrj 
175*fae548d3Szrj   for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
176*fae548d3Szrj     {
177*fae548d3Szrj       ++Free_list::num_allocate_visits;
178*fae548d3Szrj       off_t start = p->start_ > minoff ? p->start_ : minoff;
179*fae548d3Szrj       start = align_address(start, align);
180*fae548d3Szrj       off_t end = start + len;
181*fae548d3Szrj       if (end > p->end_ && p->end_ == this->length_ && this->extend_)
182*fae548d3Szrj 	{
183*fae548d3Szrj 	  this->length_ = end;
184*fae548d3Szrj 	  p->end_ = end;
185*fae548d3Szrj 	}
186*fae548d3Szrj       if (end == p->end_ || (end <= p->end_ - this->min_hole_))
187*fae548d3Szrj 	{
188*fae548d3Szrj 	  if (p->start_ + fuzz >= start && p->end_ <= end + fuzz)
189*fae548d3Szrj 	    this->list_.erase(p);
190*fae548d3Szrj 	  else if (p->start_ + fuzz >= start)
191*fae548d3Szrj 	    p->start_ = end;
192*fae548d3Szrj 	  else if (p->end_ <= end + fuzz)
193*fae548d3Szrj 	    p->end_ = start;
194*fae548d3Szrj 	  else
195*fae548d3Szrj 	    {
196*fae548d3Szrj 	      Free_list_node newnode(p->start_, start);
197*fae548d3Szrj 	      p->start_ = end;
198*fae548d3Szrj 	      this->list_.insert(p, newnode);
199*fae548d3Szrj 	      ++Free_list::num_nodes;
200*fae548d3Szrj 	    }
201*fae548d3Szrj 	  return start;
202*fae548d3Szrj 	}
203*fae548d3Szrj     }
204*fae548d3Szrj   if (this->extend_)
205*fae548d3Szrj     {
206*fae548d3Szrj       off_t start = align_address(this->length_, align);
207*fae548d3Szrj       this->length_ = start + len;
208*fae548d3Szrj       return start;
209*fae548d3Szrj     }
210*fae548d3Szrj   return -1;
211*fae548d3Szrj }
212*fae548d3Szrj 
213*fae548d3Szrj // Dump the free list (for debugging).
214*fae548d3Szrj void
dump()215*fae548d3Szrj Free_list::dump()
216*fae548d3Szrj {
217*fae548d3Szrj   gold_info("Free list:\n     start      end   length\n");
218*fae548d3Szrj   for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
219*fae548d3Szrj     gold_info("  %08lx %08lx %08lx", static_cast<long>(p->start_),
220*fae548d3Szrj 	      static_cast<long>(p->end_),
221*fae548d3Szrj 	      static_cast<long>(p->end_ - p->start_));
222*fae548d3Szrj }
223*fae548d3Szrj 
224*fae548d3Szrj // Print the statistics for the free lists.
225*fae548d3Szrj void
print_stats()226*fae548d3Szrj Free_list::print_stats()
227*fae548d3Szrj {
228*fae548d3Szrj   fprintf(stderr, _("%s: total free lists: %u\n"),
229*fae548d3Szrj 	  program_name, Free_list::num_lists);
230*fae548d3Szrj   fprintf(stderr, _("%s: total free list nodes: %u\n"),
231*fae548d3Szrj 	  program_name, Free_list::num_nodes);
232*fae548d3Szrj   fprintf(stderr, _("%s: calls to Free_list::remove: %u\n"),
233*fae548d3Szrj 	  program_name, Free_list::num_removes);
234*fae548d3Szrj   fprintf(stderr, _("%s: nodes visited: %u\n"),
235*fae548d3Szrj 	  program_name, Free_list::num_remove_visits);
236*fae548d3Szrj   fprintf(stderr, _("%s: calls to Free_list::allocate: %u\n"),
237*fae548d3Szrj 	  program_name, Free_list::num_allocates);
238*fae548d3Szrj   fprintf(stderr, _("%s: nodes visited: %u\n"),
239*fae548d3Szrj 	  program_name, Free_list::num_allocate_visits);
240*fae548d3Szrj }
241*fae548d3Szrj 
242*fae548d3Szrj // A Hash_task computes the MD5 checksum of an array of char.
243*fae548d3Szrj 
244*fae548d3Szrj class Hash_task : public Task
245*fae548d3Szrj {
246*fae548d3Szrj  public:
Hash_task(Output_file * of,size_t offset,size_t size,unsigned char * dst,Task_token * final_blocker)247*fae548d3Szrj   Hash_task(Output_file* of,
248*fae548d3Szrj 	    size_t offset,
249*fae548d3Szrj 	    size_t size,
250*fae548d3Szrj 	    unsigned char* dst,
251*fae548d3Szrj 	    Task_token* final_blocker)
252*fae548d3Szrj     : of_(of), offset_(offset), size_(size), dst_(dst),
253*fae548d3Szrj       final_blocker_(final_blocker)
254*fae548d3Szrj   { }
255*fae548d3Szrj 
256*fae548d3Szrj   void
run(Workqueue *)257*fae548d3Szrj   run(Workqueue*)
258*fae548d3Szrj   {
259*fae548d3Szrj     const unsigned char* iv =
260*fae548d3Szrj 	this->of_->get_input_view(this->offset_, this->size_);
261*fae548d3Szrj     md5_buffer(reinterpret_cast<const char*>(iv), this->size_, this->dst_);
262*fae548d3Szrj     this->of_->free_input_view(this->offset_, this->size_, iv);
263*fae548d3Szrj   }
264*fae548d3Szrj 
265*fae548d3Szrj   Task_token*
is_runnable()266*fae548d3Szrj   is_runnable()
267*fae548d3Szrj   { return NULL; }
268*fae548d3Szrj 
269*fae548d3Szrj   // Unblock FINAL_BLOCKER_ when done.
270*fae548d3Szrj   void
locks(Task_locker * tl)271*fae548d3Szrj   locks(Task_locker* tl)
272*fae548d3Szrj   { tl->add(this, this->final_blocker_); }
273*fae548d3Szrj 
274*fae548d3Szrj   std::string
get_name() const275*fae548d3Szrj   get_name() const
276*fae548d3Szrj   { return "Hash_task"; }
277*fae548d3Szrj 
278*fae548d3Szrj  private:
279*fae548d3Szrj   Output_file* of_;
280*fae548d3Szrj   const size_t offset_;
281*fae548d3Szrj   const size_t size_;
282*fae548d3Szrj   unsigned char* const dst_;
283*fae548d3Szrj   Task_token* const final_blocker_;
284*fae548d3Szrj };
285*fae548d3Szrj 
286*fae548d3Szrj // Layout::Relaxation_debug_check methods.
287*fae548d3Szrj 
288*fae548d3Szrj // Check that sections and special data are in reset states.
289*fae548d3Szrj // We do not save states for Output_sections and special Output_data.
290*fae548d3Szrj // So we check that they have not assigned any addresses or offsets.
291*fae548d3Szrj // clean_up_after_relaxation simply resets their addresses and offsets.
292*fae548d3Szrj void
check_output_data_for_reset_values(const Layout::Section_list & sections,const Layout::Data_list & special_outputs,const Layout::Data_list & relax_outputs)293*fae548d3Szrj Layout::Relaxation_debug_check::check_output_data_for_reset_values(
294*fae548d3Szrj     const Layout::Section_list& sections,
295*fae548d3Szrj     const Layout::Data_list& special_outputs,
296*fae548d3Szrj     const Layout::Data_list& relax_outputs)
297*fae548d3Szrj {
298*fae548d3Szrj   for(Layout::Section_list::const_iterator p = sections.begin();
299*fae548d3Szrj       p != sections.end();
300*fae548d3Szrj       ++p)
301*fae548d3Szrj     gold_assert((*p)->address_and_file_offset_have_reset_values());
302*fae548d3Szrj 
303*fae548d3Szrj   for(Layout::Data_list::const_iterator p = special_outputs.begin();
304*fae548d3Szrj       p != special_outputs.end();
305*fae548d3Szrj       ++p)
306*fae548d3Szrj     gold_assert((*p)->address_and_file_offset_have_reset_values());
307*fae548d3Szrj 
308*fae548d3Szrj   gold_assert(relax_outputs.empty());
309*fae548d3Szrj }
310*fae548d3Szrj 
311*fae548d3Szrj // Save information of SECTIONS for checking later.
312*fae548d3Szrj 
313*fae548d3Szrj void
read_sections(const Layout::Section_list & sections)314*fae548d3Szrj Layout::Relaxation_debug_check::read_sections(
315*fae548d3Szrj     const Layout::Section_list& sections)
316*fae548d3Szrj {
317*fae548d3Szrj   for(Layout::Section_list::const_iterator p = sections.begin();
318*fae548d3Szrj       p != sections.end();
319*fae548d3Szrj       ++p)
320*fae548d3Szrj     {
321*fae548d3Szrj       Output_section* os = *p;
322*fae548d3Szrj       Section_info info;
323*fae548d3Szrj       info.output_section = os;
324*fae548d3Szrj       info.address = os->is_address_valid() ? os->address() : 0;
325*fae548d3Szrj       info.data_size = os->is_data_size_valid() ? os->data_size() : -1;
326*fae548d3Szrj       info.offset = os->is_offset_valid()? os->offset() : -1 ;
327*fae548d3Szrj       this->section_infos_.push_back(info);
328*fae548d3Szrj     }
329*fae548d3Szrj }
330*fae548d3Szrj 
331*fae548d3Szrj // Verify SECTIONS using previously recorded information.
332*fae548d3Szrj 
333*fae548d3Szrj void
verify_sections(const Layout::Section_list & sections)334*fae548d3Szrj Layout::Relaxation_debug_check::verify_sections(
335*fae548d3Szrj     const Layout::Section_list& sections)
336*fae548d3Szrj {
337*fae548d3Szrj   size_t i = 0;
338*fae548d3Szrj   for(Layout::Section_list::const_iterator p = sections.begin();
339*fae548d3Szrj       p != sections.end();
340*fae548d3Szrj       ++p, ++i)
341*fae548d3Szrj     {
342*fae548d3Szrj       Output_section* os = *p;
343*fae548d3Szrj       uint64_t address = os->is_address_valid() ? os->address() : 0;
344*fae548d3Szrj       off_t data_size = os->is_data_size_valid() ? os->data_size() : -1;
345*fae548d3Szrj       off_t offset = os->is_offset_valid()? os->offset() : -1 ;
346*fae548d3Szrj 
347*fae548d3Szrj       if (i >= this->section_infos_.size())
348*fae548d3Szrj 	{
349*fae548d3Szrj 	  gold_fatal("Section_info of %s missing.\n", os->name());
350*fae548d3Szrj 	}
351*fae548d3Szrj       const Section_info& info = this->section_infos_[i];
352*fae548d3Szrj       if (os != info.output_section)
353*fae548d3Szrj 	gold_fatal("Section order changed.  Expecting %s but see %s\n",
354*fae548d3Szrj 		   info.output_section->name(), os->name());
355*fae548d3Szrj       if (address != info.address
356*fae548d3Szrj 	  || data_size != info.data_size
357*fae548d3Szrj 	  || offset != info.offset)
358*fae548d3Szrj 	gold_fatal("Section %s changed.\n", os->name());
359*fae548d3Szrj     }
360*fae548d3Szrj }
361*fae548d3Szrj 
362*fae548d3Szrj // Layout_task_runner methods.
363*fae548d3Szrj 
364*fae548d3Szrj // Lay out the sections.  This is called after all the input objects
365*fae548d3Szrj // have been read.
366*fae548d3Szrj 
367*fae548d3Szrj void
run(Workqueue * workqueue,const Task * task)368*fae548d3Szrj Layout_task_runner::run(Workqueue* workqueue, const Task* task)
369*fae548d3Szrj {
370*fae548d3Szrj   // See if any of the input definitions violate the One Definition Rule.
371*fae548d3Szrj   // TODO: if this is too slow, do this as a task, rather than inline.
372*fae548d3Szrj   this->symtab_->detect_odr_violations(task, this->options_.output_file_name());
373*fae548d3Szrj 
374*fae548d3Szrj   Layout* layout = this->layout_;
375*fae548d3Szrj   off_t file_size = layout->finalize(this->input_objects_,
376*fae548d3Szrj 				     this->symtab_,
377*fae548d3Szrj 				     this->target_,
378*fae548d3Szrj 				     task);
379*fae548d3Szrj 
380*fae548d3Szrj   // Now we know the final size of the output file and we know where
381*fae548d3Szrj   // each piece of information goes.
382*fae548d3Szrj 
383*fae548d3Szrj   if (this->mapfile_ != NULL)
384*fae548d3Szrj     {
385*fae548d3Szrj       this->mapfile_->print_discarded_sections(this->input_objects_);
386*fae548d3Szrj       layout->print_to_mapfile(this->mapfile_);
387*fae548d3Szrj     }
388*fae548d3Szrj 
389*fae548d3Szrj   Output_file* of;
390*fae548d3Szrj   if (layout->incremental_base() == NULL)
391*fae548d3Szrj     {
392*fae548d3Szrj       of = new Output_file(parameters->options().output_file_name());
393*fae548d3Szrj       if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
394*fae548d3Szrj 	of->set_is_temporary();
395*fae548d3Szrj       of->open(file_size);
396*fae548d3Szrj     }
397*fae548d3Szrj   else
398*fae548d3Szrj     {
399*fae548d3Szrj       of = layout->incremental_base()->output_file();
400*fae548d3Szrj 
401*fae548d3Szrj       // Apply the incremental relocations for symbols whose values
402*fae548d3Szrj       // have changed.  We do this before we resize the file and start
403*fae548d3Szrj       // writing anything else to it, so that we can read the old
404*fae548d3Szrj       // incremental information from the file before (possibly)
405*fae548d3Szrj       // overwriting it.
406*fae548d3Szrj       if (parameters->incremental_update())
407*fae548d3Szrj 	layout->incremental_base()->apply_incremental_relocs(this->symtab_,
408*fae548d3Szrj 							     this->layout_,
409*fae548d3Szrj 							     of);
410*fae548d3Szrj 
411*fae548d3Szrj       of->resize(file_size);
412*fae548d3Szrj     }
413*fae548d3Szrj 
414*fae548d3Szrj   // Queue up the final set of tasks.
415*fae548d3Szrj   gold::queue_final_tasks(this->options_, this->input_objects_,
416*fae548d3Szrj 			  this->symtab_, layout, workqueue, of);
417*fae548d3Szrj }
418*fae548d3Szrj 
419*fae548d3Szrj // Layout methods.
420*fae548d3Szrj 
Layout(int number_of_input_files,Script_options * script_options)421*fae548d3Szrj Layout::Layout(int number_of_input_files, Script_options* script_options)
422*fae548d3Szrj   : number_of_input_files_(number_of_input_files),
423*fae548d3Szrj     script_options_(script_options),
424*fae548d3Szrj     namepool_(),
425*fae548d3Szrj     sympool_(),
426*fae548d3Szrj     dynpool_(),
427*fae548d3Szrj     signatures_(),
428*fae548d3Szrj     section_name_map_(),
429*fae548d3Szrj     segment_list_(),
430*fae548d3Szrj     section_list_(),
431*fae548d3Szrj     unattached_section_list_(),
432*fae548d3Szrj     special_output_list_(),
433*fae548d3Szrj     relax_output_list_(),
434*fae548d3Szrj     section_headers_(NULL),
435*fae548d3Szrj     tls_segment_(NULL),
436*fae548d3Szrj     relro_segment_(NULL),
437*fae548d3Szrj     interp_segment_(NULL),
438*fae548d3Szrj     increase_relro_(0),
439*fae548d3Szrj     symtab_section_(NULL),
440*fae548d3Szrj     symtab_xindex_(NULL),
441*fae548d3Szrj     dynsym_section_(NULL),
442*fae548d3Szrj     dynsym_xindex_(NULL),
443*fae548d3Szrj     dynamic_section_(NULL),
444*fae548d3Szrj     dynamic_symbol_(NULL),
445*fae548d3Szrj     dynamic_data_(NULL),
446*fae548d3Szrj     eh_frame_section_(NULL),
447*fae548d3Szrj     eh_frame_data_(NULL),
448*fae548d3Szrj     added_eh_frame_data_(false),
449*fae548d3Szrj     eh_frame_hdr_section_(NULL),
450*fae548d3Szrj     gdb_index_data_(NULL),
451*fae548d3Szrj     build_id_note_(NULL),
452*fae548d3Szrj     debug_abbrev_(NULL),
453*fae548d3Szrj     debug_info_(NULL),
454*fae548d3Szrj     group_signatures_(),
455*fae548d3Szrj     output_file_size_(-1),
456*fae548d3Szrj     have_added_input_section_(false),
457*fae548d3Szrj     sections_are_attached_(false),
458*fae548d3Szrj     input_requires_executable_stack_(false),
459*fae548d3Szrj     input_with_gnu_stack_note_(false),
460*fae548d3Szrj     input_without_gnu_stack_note_(false),
461*fae548d3Szrj     has_static_tls_(false),
462*fae548d3Szrj     any_postprocessing_sections_(false),
463*fae548d3Szrj     resized_signatures_(false),
464*fae548d3Szrj     have_stabstr_section_(false),
465*fae548d3Szrj     section_ordering_specified_(false),
466*fae548d3Szrj     unique_segment_for_sections_specified_(false),
467*fae548d3Szrj     incremental_inputs_(NULL),
468*fae548d3Szrj     record_output_section_data_from_script_(false),
469*fae548d3Szrj     lto_slim_object_(false),
470*fae548d3Szrj     script_output_section_data_list_(),
471*fae548d3Szrj     segment_states_(NULL),
472*fae548d3Szrj     relaxation_debug_check_(NULL),
473*fae548d3Szrj     section_order_map_(),
474*fae548d3Szrj     section_segment_map_(),
475*fae548d3Szrj     input_section_position_(),
476*fae548d3Szrj     input_section_glob_(),
477*fae548d3Szrj     incremental_base_(NULL),
478*fae548d3Szrj     free_list_(),
479*fae548d3Szrj     gnu_properties_()
480*fae548d3Szrj {
481*fae548d3Szrj   // Make space for more than enough segments for a typical file.
482*fae548d3Szrj   // This is just for efficiency--it's OK if we wind up needing more.
483*fae548d3Szrj   this->segment_list_.reserve(12);
484*fae548d3Szrj 
485*fae548d3Szrj   // We expect two unattached Output_data objects: the file header and
486*fae548d3Szrj   // the segment headers.
487*fae548d3Szrj   this->special_output_list_.reserve(2);
488*fae548d3Szrj 
489*fae548d3Szrj   // Initialize structure needed for an incremental build.
490*fae548d3Szrj   if (parameters->incremental())
491*fae548d3Szrj     this->incremental_inputs_ = new Incremental_inputs;
492*fae548d3Szrj 
493*fae548d3Szrj   // The section name pool is worth optimizing in all cases, because
494*fae548d3Szrj   // it is small, but there are often overlaps due to .rel sections.
495*fae548d3Szrj   this->namepool_.set_optimize();
496*fae548d3Szrj }
497*fae548d3Szrj 
498*fae548d3Szrj // For incremental links, record the base file to be modified.
499*fae548d3Szrj 
500*fae548d3Szrj void
set_incremental_base(Incremental_binary * base)501*fae548d3Szrj Layout::set_incremental_base(Incremental_binary* base)
502*fae548d3Szrj {
503*fae548d3Szrj   this->incremental_base_ = base;
504*fae548d3Szrj   this->free_list_.init(base->output_file()->filesize(), true);
505*fae548d3Szrj }
506*fae548d3Szrj 
507*fae548d3Szrj // Hash a key we use to look up an output section mapping.
508*fae548d3Szrj 
509*fae548d3Szrj size_t
operator ()(const Layout::Key & k) const510*fae548d3Szrj Layout::Hash_key::operator()(const Layout::Key& k) const
511*fae548d3Szrj {
512*fae548d3Szrj  return k.first + k.second.first + k.second.second;
513*fae548d3Szrj }
514*fae548d3Szrj 
515*fae548d3Szrj // These are the debug sections that are actually used by gdb.
516*fae548d3Szrj // Currently, we've checked versions of gdb up to and including 7.4.
517*fae548d3Szrj // We only check the part of the name that follows ".debug_" or
518*fae548d3Szrj // ".zdebug_".
519*fae548d3Szrj 
520*fae548d3Szrj static const char* gdb_sections[] =
521*fae548d3Szrj {
522*fae548d3Szrj   "abbrev",
523*fae548d3Szrj   "addr",         // Fission extension
524*fae548d3Szrj   // "aranges",   // not used by gdb as of 7.4
525*fae548d3Szrj   "frame",
526*fae548d3Szrj   "gdb_scripts",
527*fae548d3Szrj   "info",
528*fae548d3Szrj   "types",
529*fae548d3Szrj   "line",
530*fae548d3Szrj   "loc",
531*fae548d3Szrj   "macinfo",
532*fae548d3Szrj   "macro",
533*fae548d3Szrj   // "pubnames",  // not used by gdb as of 7.4
534*fae548d3Szrj   // "pubtypes",  // not used by gdb as of 7.4
535*fae548d3Szrj   // "gnu_pubnames",  // Fission extension
536*fae548d3Szrj   // "gnu_pubtypes",  // Fission extension
537*fae548d3Szrj   "ranges",
538*fae548d3Szrj   "str",
539*fae548d3Szrj   "str_offsets",
540*fae548d3Szrj };
541*fae548d3Szrj 
542*fae548d3Szrj // This is the minimum set of sections needed for line numbers.
543*fae548d3Szrj 
544*fae548d3Szrj static const char* lines_only_debug_sections[] =
545*fae548d3Szrj {
546*fae548d3Szrj   "abbrev",
547*fae548d3Szrj   // "addr",      // Fission extension
548*fae548d3Szrj   // "aranges",   // not used by gdb as of 7.4
549*fae548d3Szrj   // "frame",
550*fae548d3Szrj   // "gdb_scripts",
551*fae548d3Szrj   "info",
552*fae548d3Szrj   // "types",
553*fae548d3Szrj   "line",
554*fae548d3Szrj   // "loc",
555*fae548d3Szrj   // "macinfo",
556*fae548d3Szrj   // "macro",
557*fae548d3Szrj   // "pubnames",  // not used by gdb as of 7.4
558*fae548d3Szrj   // "pubtypes",  // not used by gdb as of 7.4
559*fae548d3Szrj   // "gnu_pubnames",  // Fission extension
560*fae548d3Szrj   // "gnu_pubtypes",  // Fission extension
561*fae548d3Szrj   // "ranges",
562*fae548d3Szrj   "str",
563*fae548d3Szrj   "str_offsets",  // Fission extension
564*fae548d3Szrj };
565*fae548d3Szrj 
566*fae548d3Szrj // These sections are the DWARF fast-lookup tables, and are not needed
567*fae548d3Szrj // when building a .gdb_index section.
568*fae548d3Szrj 
569*fae548d3Szrj static const char* gdb_fast_lookup_sections[] =
570*fae548d3Szrj {
571*fae548d3Szrj   "aranges",
572*fae548d3Szrj   "pubnames",
573*fae548d3Szrj   "gnu_pubnames",
574*fae548d3Szrj   "pubtypes",
575*fae548d3Szrj   "gnu_pubtypes",
576*fae548d3Szrj };
577*fae548d3Szrj 
578*fae548d3Szrj // Returns whether the given debug section is in the list of
579*fae548d3Szrj // debug-sections-used-by-some-version-of-gdb.  SUFFIX is the
580*fae548d3Szrj // portion of the name following ".debug_" or ".zdebug_".
581*fae548d3Szrj 
582*fae548d3Szrj static inline bool
is_gdb_debug_section(const char * suffix)583*fae548d3Szrj is_gdb_debug_section(const char* suffix)
584*fae548d3Szrj {
585*fae548d3Szrj   // We can do this faster: binary search or a hashtable.  But why bother?
586*fae548d3Szrj   for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
587*fae548d3Szrj     if (strcmp(suffix, gdb_sections[i]) == 0)
588*fae548d3Szrj       return true;
589*fae548d3Szrj   return false;
590*fae548d3Szrj }
591*fae548d3Szrj 
592*fae548d3Szrj // Returns whether the given section is needed for lines-only debugging.
593*fae548d3Szrj 
594*fae548d3Szrj static inline bool
is_lines_only_debug_section(const char * suffix)595*fae548d3Szrj is_lines_only_debug_section(const char* suffix)
596*fae548d3Szrj {
597*fae548d3Szrj   // We can do this faster: binary search or a hashtable.  But why bother?
598*fae548d3Szrj   for (size_t i = 0;
599*fae548d3Szrj        i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections);
600*fae548d3Szrj        ++i)
601*fae548d3Szrj     if (strcmp(suffix, lines_only_debug_sections[i]) == 0)
602*fae548d3Szrj       return true;
603*fae548d3Szrj   return false;
604*fae548d3Szrj }
605*fae548d3Szrj 
606*fae548d3Szrj // Returns whether the given section is a fast-lookup section that
607*fae548d3Szrj // will not be needed when building a .gdb_index section.
608*fae548d3Szrj 
609*fae548d3Szrj static inline bool
is_gdb_fast_lookup_section(const char * suffix)610*fae548d3Szrj is_gdb_fast_lookup_section(const char* suffix)
611*fae548d3Szrj {
612*fae548d3Szrj   // We can do this faster: binary search or a hashtable.  But why bother?
613*fae548d3Szrj   for (size_t i = 0;
614*fae548d3Szrj        i < sizeof(gdb_fast_lookup_sections)/sizeof(*gdb_fast_lookup_sections);
615*fae548d3Szrj        ++i)
616*fae548d3Szrj     if (strcmp(suffix, gdb_fast_lookup_sections[i]) == 0)
617*fae548d3Szrj       return true;
618*fae548d3Szrj   return false;
619*fae548d3Szrj }
620*fae548d3Szrj 
621*fae548d3Szrj // Sometimes we compress sections.  This is typically done for
622*fae548d3Szrj // sections that are not part of normal program execution (such as
623*fae548d3Szrj // .debug_* sections), and where the readers of these sections know
624*fae548d3Szrj // how to deal with compressed sections.  This routine doesn't say for
625*fae548d3Szrj // certain whether we'll compress -- it depends on commandline options
626*fae548d3Szrj // as well -- just whether this section is a candidate for compression.
627*fae548d3Szrj // (The Output_compressed_section class decides whether to compress
628*fae548d3Szrj // a given section, and picks the name of the compressed section.)
629*fae548d3Szrj 
630*fae548d3Szrj static bool
is_compressible_debug_section(const char * secname)631*fae548d3Szrj is_compressible_debug_section(const char* secname)
632*fae548d3Szrj {
633*fae548d3Szrj   return (is_prefix_of(".debug", secname));
634*fae548d3Szrj }
635*fae548d3Szrj 
636*fae548d3Szrj // We may see compressed debug sections in input files.  Return TRUE
637*fae548d3Szrj // if this is the name of a compressed debug section.
638*fae548d3Szrj 
639*fae548d3Szrj bool
is_compressed_debug_section(const char * secname)640*fae548d3Szrj is_compressed_debug_section(const char* secname)
641*fae548d3Szrj {
642*fae548d3Szrj   return (is_prefix_of(".zdebug", secname));
643*fae548d3Szrj }
644*fae548d3Szrj 
645*fae548d3Szrj std::string
corresponding_uncompressed_section_name(std::string secname)646*fae548d3Szrj corresponding_uncompressed_section_name(std::string secname)
647*fae548d3Szrj {
648*fae548d3Szrj   gold_assert(secname[0] == '.' && secname[1] == 'z');
649*fae548d3Szrj   std::string ret(".");
650*fae548d3Szrj   ret.append(secname, 2, std::string::npos);
651*fae548d3Szrj   return ret;
652*fae548d3Szrj }
653*fae548d3Szrj 
654*fae548d3Szrj // Whether to include this section in the link.
655*fae548d3Szrj 
656*fae548d3Szrj template<int size, bool big_endian>
657*fae548d3Szrj bool
include_section(Sized_relobj_file<size,big_endian> *,const char * name,const elfcpp::Shdr<size,big_endian> & shdr)658*fae548d3Szrj Layout::include_section(Sized_relobj_file<size, big_endian>*, const char* name,
659*fae548d3Szrj 			const elfcpp::Shdr<size, big_endian>& shdr)
660*fae548d3Szrj {
661*fae548d3Szrj   if (!parameters->options().relocatable()
662*fae548d3Szrj       && (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE))
663*fae548d3Szrj     return false;
664*fae548d3Szrj 
665*fae548d3Szrj   elfcpp::Elf_Word sh_type = shdr.get_sh_type();
666*fae548d3Szrj 
667*fae548d3Szrj   if ((sh_type >= elfcpp::SHT_LOOS && sh_type <= elfcpp::SHT_HIOS)
668*fae548d3Szrj       || (sh_type >= elfcpp::SHT_LOPROC && sh_type <= elfcpp::SHT_HIPROC))
669*fae548d3Szrj     return parameters->target().should_include_section(sh_type);
670*fae548d3Szrj 
671*fae548d3Szrj   switch (sh_type)
672*fae548d3Szrj     {
673*fae548d3Szrj     case elfcpp::SHT_NULL:
674*fae548d3Szrj     case elfcpp::SHT_SYMTAB:
675*fae548d3Szrj     case elfcpp::SHT_DYNSYM:
676*fae548d3Szrj     case elfcpp::SHT_HASH:
677*fae548d3Szrj     case elfcpp::SHT_DYNAMIC:
678*fae548d3Szrj     case elfcpp::SHT_SYMTAB_SHNDX:
679*fae548d3Szrj       return false;
680*fae548d3Szrj 
681*fae548d3Szrj     case elfcpp::SHT_STRTAB:
682*fae548d3Szrj       // Discard the sections which have special meanings in the ELF
683*fae548d3Szrj       // ABI.  Keep others (e.g., .stabstr).  We could also do this by
684*fae548d3Szrj       // checking the sh_link fields of the appropriate sections.
685*fae548d3Szrj       return (strcmp(name, ".dynstr") != 0
686*fae548d3Szrj 	      && strcmp(name, ".strtab") != 0
687*fae548d3Szrj 	      && strcmp(name, ".shstrtab") != 0);
688*fae548d3Szrj 
689*fae548d3Szrj     case elfcpp::SHT_RELA:
690*fae548d3Szrj     case elfcpp::SHT_REL:
691*fae548d3Szrj     case elfcpp::SHT_GROUP:
692*fae548d3Szrj       // If we are emitting relocations these should be handled
693*fae548d3Szrj       // elsewhere.
694*fae548d3Szrj       gold_assert(!parameters->options().relocatable());
695*fae548d3Szrj       return false;
696*fae548d3Szrj 
697*fae548d3Szrj     case elfcpp::SHT_PROGBITS:
698*fae548d3Szrj       if (parameters->options().strip_debug()
699*fae548d3Szrj 	  && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
700*fae548d3Szrj 	{
701*fae548d3Szrj 	  if (is_debug_info_section(name))
702*fae548d3Szrj 	    return false;
703*fae548d3Szrj 	}
704*fae548d3Szrj       if (parameters->options().strip_debug_non_line()
705*fae548d3Szrj 	  && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
706*fae548d3Szrj 	{
707*fae548d3Szrj 	  // Debugging sections can only be recognized by name.
708*fae548d3Szrj 	  if (is_prefix_of(".debug_", name)
709*fae548d3Szrj 	      && !is_lines_only_debug_section(name + 7))
710*fae548d3Szrj 	    return false;
711*fae548d3Szrj 	  if (is_prefix_of(".zdebug_", name)
712*fae548d3Szrj 	      && !is_lines_only_debug_section(name + 8))
713*fae548d3Szrj 	    return false;
714*fae548d3Szrj 	}
715*fae548d3Szrj       if (parameters->options().strip_debug_gdb()
716*fae548d3Szrj 	  && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
717*fae548d3Szrj 	{
718*fae548d3Szrj 	  // Debugging sections can only be recognized by name.
719*fae548d3Szrj 	  if (is_prefix_of(".debug_", name)
720*fae548d3Szrj 	      && !is_gdb_debug_section(name + 7))
721*fae548d3Szrj 	    return false;
722*fae548d3Szrj 	  if (is_prefix_of(".zdebug_", name)
723*fae548d3Szrj 	      && !is_gdb_debug_section(name + 8))
724*fae548d3Szrj 	    return false;
725*fae548d3Szrj 	}
726*fae548d3Szrj       if (parameters->options().gdb_index()
727*fae548d3Szrj 	  && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
728*fae548d3Szrj 	{
729*fae548d3Szrj 	  // When building .gdb_index, we can strip .debug_pubnames,
730*fae548d3Szrj 	  // .debug_pubtypes, and .debug_aranges sections.
731*fae548d3Szrj 	  if (is_prefix_of(".debug_", name)
732*fae548d3Szrj 	      && is_gdb_fast_lookup_section(name + 7))
733*fae548d3Szrj 	    return false;
734*fae548d3Szrj 	  if (is_prefix_of(".zdebug_", name)
735*fae548d3Szrj 	      && is_gdb_fast_lookup_section(name + 8))
736*fae548d3Szrj 	    return false;
737*fae548d3Szrj 	}
738*fae548d3Szrj       if (parameters->options().strip_lto_sections()
739*fae548d3Szrj 	  && !parameters->options().relocatable()
740*fae548d3Szrj 	  && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
741*fae548d3Szrj 	{
742*fae548d3Szrj 	  // Ignore LTO sections containing intermediate code.
743*fae548d3Szrj 	  if (is_prefix_of(".gnu.lto_", name))
744*fae548d3Szrj 	    return false;
745*fae548d3Szrj 	}
746*fae548d3Szrj       // The GNU linker strips .gnu_debuglink sections, so we do too.
747*fae548d3Szrj       // This is a feature used to keep debugging information in
748*fae548d3Szrj       // separate files.
749*fae548d3Szrj       if (strcmp(name, ".gnu_debuglink") == 0)
750*fae548d3Szrj 	return false;
751*fae548d3Szrj       return true;
752*fae548d3Szrj 
753*fae548d3Szrj     default:
754*fae548d3Szrj       return true;
755*fae548d3Szrj     }
756*fae548d3Szrj }
757*fae548d3Szrj 
758*fae548d3Szrj // Return an output section named NAME, or NULL if there is none.
759*fae548d3Szrj 
760*fae548d3Szrj Output_section*
find_output_section(const char * name) const761*fae548d3Szrj Layout::find_output_section(const char* name) const
762*fae548d3Szrj {
763*fae548d3Szrj   for (Section_list::const_iterator p = this->section_list_.begin();
764*fae548d3Szrj        p != this->section_list_.end();
765*fae548d3Szrj        ++p)
766*fae548d3Szrj     if (strcmp((*p)->name(), name) == 0)
767*fae548d3Szrj       return *p;
768*fae548d3Szrj   return NULL;
769*fae548d3Szrj }
770*fae548d3Szrj 
771*fae548d3Szrj // Return an output segment of type TYPE, with segment flags SET set
772*fae548d3Szrj // and segment flags CLEAR clear.  Return NULL if there is none.
773*fae548d3Szrj 
774*fae548d3Szrj Output_segment*
find_output_segment(elfcpp::PT type,elfcpp::Elf_Word set,elfcpp::Elf_Word clear) const775*fae548d3Szrj Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
776*fae548d3Szrj 			    elfcpp::Elf_Word clear) const
777*fae548d3Szrj {
778*fae548d3Szrj   for (Segment_list::const_iterator p = this->segment_list_.begin();
779*fae548d3Szrj        p != this->segment_list_.end();
780*fae548d3Szrj        ++p)
781*fae548d3Szrj     if (static_cast<elfcpp::PT>((*p)->type()) == type
782*fae548d3Szrj 	&& ((*p)->flags() & set) == set
783*fae548d3Szrj 	&& ((*p)->flags() & clear) == 0)
784*fae548d3Szrj       return *p;
785*fae548d3Szrj   return NULL;
786*fae548d3Szrj }
787*fae548d3Szrj 
788*fae548d3Szrj // When we put a .ctors or .dtors section with more than one word into
789*fae548d3Szrj // a .init_array or .fini_array section, we need to reverse the words
790*fae548d3Szrj // in the .ctors/.dtors section.  This is because .init_array executes
791*fae548d3Szrj // constructors front to back, where .ctors executes them back to
792*fae548d3Szrj // front, and vice-versa for .fini_array/.dtors.  Although we do want
793*fae548d3Szrj // to remap .ctors/.dtors into .init_array/.fini_array because it can
794*fae548d3Szrj // be more efficient, we don't want to change the order in which
795*fae548d3Szrj // constructors/destructors are run.  This set just keeps track of
796*fae548d3Szrj // these sections which need to be reversed.  It is only changed by
797*fae548d3Szrj // Layout::layout.  It should be a private member of Layout, but that
798*fae548d3Szrj // would require layout.h to #include object.h to get the definition
799*fae548d3Szrj // of Section_id.
800*fae548d3Szrj static Unordered_set<Section_id, Section_id_hash> ctors_sections_in_init_array;
801*fae548d3Szrj 
802*fae548d3Szrj // Return whether OBJECT/SHNDX is a .ctors/.dtors section mapped to a
803*fae548d3Szrj // .init_array/.fini_array section.
804*fae548d3Szrj 
805*fae548d3Szrj bool
is_ctors_in_init_array(Relobj * relobj,unsigned int shndx) const806*fae548d3Szrj Layout::is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const
807*fae548d3Szrj {
808*fae548d3Szrj   return (ctors_sections_in_init_array.find(Section_id(relobj, shndx))
809*fae548d3Szrj 	  != ctors_sections_in_init_array.end());
810*fae548d3Szrj }
811*fae548d3Szrj 
812*fae548d3Szrj // Return the output section to use for section NAME with type TYPE
813*fae548d3Szrj // and section flags FLAGS.  NAME must be canonicalized in the string
814*fae548d3Szrj // pool, and NAME_KEY is the key.  ORDER is where this should appear
815*fae548d3Szrj // in the output sections.  IS_RELRO is true for a relro section.
816*fae548d3Szrj 
817*fae548d3Szrj Output_section*
get_output_section(const char * name,Stringpool::Key name_key,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags,Output_section_order order,bool is_relro)818*fae548d3Szrj Layout::get_output_section(const char* name, Stringpool::Key name_key,
819*fae548d3Szrj 			   elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
820*fae548d3Szrj 			   Output_section_order order, bool is_relro)
821*fae548d3Szrj {
822*fae548d3Szrj   elfcpp::Elf_Word lookup_type = type;
823*fae548d3Szrj 
824*fae548d3Szrj   // For lookup purposes, treat INIT_ARRAY, FINI_ARRAY, and
825*fae548d3Szrj   // PREINIT_ARRAY like PROGBITS.  This ensures that we combine
826*fae548d3Szrj   // .init_array, .fini_array, and .preinit_array sections by name
827*fae548d3Szrj   // whatever their type in the input file.  We do this because the
828*fae548d3Szrj   // types are not always right in the input files.
829*fae548d3Szrj   if (lookup_type == elfcpp::SHT_INIT_ARRAY
830*fae548d3Szrj       || lookup_type == elfcpp::SHT_FINI_ARRAY
831*fae548d3Szrj       || lookup_type == elfcpp::SHT_PREINIT_ARRAY)
832*fae548d3Szrj     lookup_type = elfcpp::SHT_PROGBITS;
833*fae548d3Szrj 
834*fae548d3Szrj   elfcpp::Elf_Xword lookup_flags = flags;
835*fae548d3Szrj 
836*fae548d3Szrj   // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
837*fae548d3Szrj   // read-write with read-only sections.  Some other ELF linkers do
838*fae548d3Szrj   // not do this.  FIXME: Perhaps there should be an option
839*fae548d3Szrj   // controlling this.
840*fae548d3Szrj   lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
841*fae548d3Szrj 
842*fae548d3Szrj   const Key key(name_key, std::make_pair(lookup_type, lookup_flags));
843*fae548d3Szrj   const std::pair<Key, Output_section*> v(key, NULL);
844*fae548d3Szrj   std::pair<Section_name_map::iterator, bool> ins(
845*fae548d3Szrj     this->section_name_map_.insert(v));
846*fae548d3Szrj 
847*fae548d3Szrj   if (!ins.second)
848*fae548d3Szrj     return ins.first->second;
849*fae548d3Szrj   else
850*fae548d3Szrj     {
851*fae548d3Szrj       // This is the first time we've seen this name/type/flags
852*fae548d3Szrj       // combination.  For compatibility with the GNU linker, we
853*fae548d3Szrj       // combine sections with contents and zero flags with sections
854*fae548d3Szrj       // with non-zero flags.  This is a workaround for cases where
855*fae548d3Szrj       // assembler code forgets to set section flags.  FIXME: Perhaps
856*fae548d3Szrj       // there should be an option to control this.
857*fae548d3Szrj       Output_section* os = NULL;
858*fae548d3Szrj 
859*fae548d3Szrj       if (lookup_type == elfcpp::SHT_PROGBITS)
860*fae548d3Szrj 	{
861*fae548d3Szrj 	  if (flags == 0)
862*fae548d3Szrj 	    {
863*fae548d3Szrj 	      Output_section* same_name = this->find_output_section(name);
864*fae548d3Szrj 	      if (same_name != NULL
865*fae548d3Szrj 		  && (same_name->type() == elfcpp::SHT_PROGBITS
866*fae548d3Szrj 		      || same_name->type() == elfcpp::SHT_INIT_ARRAY
867*fae548d3Szrj 		      || same_name->type() == elfcpp::SHT_FINI_ARRAY
868*fae548d3Szrj 		      || same_name->type() == elfcpp::SHT_PREINIT_ARRAY)
869*fae548d3Szrj 		  && (same_name->flags() & elfcpp::SHF_TLS) == 0)
870*fae548d3Szrj 		os = same_name;
871*fae548d3Szrj 	    }
872*fae548d3Szrj 	  else if ((flags & elfcpp::SHF_TLS) == 0)
873*fae548d3Szrj 	    {
874*fae548d3Szrj 	      elfcpp::Elf_Xword zero_flags = 0;
875*fae548d3Szrj 	      const Key zero_key(name_key, std::make_pair(lookup_type,
876*fae548d3Szrj 							  zero_flags));
877*fae548d3Szrj 	      Section_name_map::iterator p =
878*fae548d3Szrj 		  this->section_name_map_.find(zero_key);
879*fae548d3Szrj 	      if (p != this->section_name_map_.end())
880*fae548d3Szrj 		os = p->second;
881*fae548d3Szrj 	    }
882*fae548d3Szrj 	}
883*fae548d3Szrj 
884*fae548d3Szrj       if (os == NULL)
885*fae548d3Szrj 	os = this->make_output_section(name, type, flags, order, is_relro);
886*fae548d3Szrj 
887*fae548d3Szrj       ins.first->second = os;
888*fae548d3Szrj       return os;
889*fae548d3Szrj     }
890*fae548d3Szrj }
891*fae548d3Szrj 
892*fae548d3Szrj // Returns TRUE iff NAME (an input section from RELOBJ) will
893*fae548d3Szrj // be mapped to an output section that should be KEPT.
894*fae548d3Szrj 
895*fae548d3Szrj bool
keep_input_section(const Relobj * relobj,const char * name)896*fae548d3Szrj Layout::keep_input_section(const Relobj* relobj, const char* name)
897*fae548d3Szrj {
898*fae548d3Szrj   if (! this->script_options_->saw_sections_clause())
899*fae548d3Szrj     return false;
900*fae548d3Szrj 
901*fae548d3Szrj   Script_sections* ss = this->script_options_->script_sections();
902*fae548d3Szrj   const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
903*fae548d3Szrj   Output_section** output_section_slot;
904*fae548d3Szrj   Script_sections::Section_type script_section_type;
905*fae548d3Szrj   bool keep;
906*fae548d3Szrj 
907*fae548d3Szrj   name = ss->output_section_name(file_name, name, &output_section_slot,
908*fae548d3Szrj 				 &script_section_type, &keep, true);
909*fae548d3Szrj   return name != NULL && keep;
910*fae548d3Szrj }
911*fae548d3Szrj 
912*fae548d3Szrj // Clear the input section flags that should not be copied to the
913*fae548d3Szrj // output section.
914*fae548d3Szrj 
915*fae548d3Szrj elfcpp::Elf_Xword
get_output_section_flags(elfcpp::Elf_Xword input_section_flags)916*fae548d3Szrj Layout::get_output_section_flags(elfcpp::Elf_Xword input_section_flags)
917*fae548d3Szrj {
918*fae548d3Szrj   // Some flags in the input section should not be automatically
919*fae548d3Szrj   // copied to the output section.
920*fae548d3Szrj   input_section_flags &= ~ (elfcpp::SHF_INFO_LINK
921*fae548d3Szrj 			    | elfcpp::SHF_GROUP
922*fae548d3Szrj 			    | elfcpp::SHF_COMPRESSED
923*fae548d3Szrj 			    | elfcpp::SHF_MERGE
924*fae548d3Szrj 			    | elfcpp::SHF_STRINGS);
925*fae548d3Szrj 
926*fae548d3Szrj   // We only clear the SHF_LINK_ORDER flag in for
927*fae548d3Szrj   // a non-relocatable link.
928*fae548d3Szrj   if (!parameters->options().relocatable())
929*fae548d3Szrj     input_section_flags &= ~elfcpp::SHF_LINK_ORDER;
930*fae548d3Szrj 
931*fae548d3Szrj   return input_section_flags;
932*fae548d3Szrj }
933*fae548d3Szrj 
934*fae548d3Szrj // Pick the output section to use for section NAME, in input file
935*fae548d3Szrj // RELOBJ, with type TYPE and flags FLAGS.  RELOBJ may be NULL for a
936*fae548d3Szrj // linker created section.  IS_INPUT_SECTION is true if we are
937*fae548d3Szrj // choosing an output section for an input section found in a input
938*fae548d3Szrj // file.  ORDER is where this section should appear in the output
939*fae548d3Szrj // sections.  IS_RELRO is true for a relro section.  This will return
940*fae548d3Szrj // NULL if the input section should be discarded.  MATCH_INPUT_SPEC
941*fae548d3Szrj // is true if the section name should be matched against input specs
942*fae548d3Szrj // in a linker script.
943*fae548d3Szrj 
944*fae548d3Szrj Output_section*
choose_output_section(const Relobj * relobj,const char * name,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags,bool is_input_section,Output_section_order order,bool is_relro,bool is_reloc,bool match_input_spec)945*fae548d3Szrj Layout::choose_output_section(const Relobj* relobj, const char* name,
946*fae548d3Szrj 			      elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
947*fae548d3Szrj 			      bool is_input_section, Output_section_order order,
948*fae548d3Szrj 			      bool is_relro, bool is_reloc,
949*fae548d3Szrj 			      bool match_input_spec)
950*fae548d3Szrj {
951*fae548d3Szrj   // We should not see any input sections after we have attached
952*fae548d3Szrj   // sections to segments.
953*fae548d3Szrj   gold_assert(!is_input_section || !this->sections_are_attached_);
954*fae548d3Szrj 
955*fae548d3Szrj   flags = this->get_output_section_flags(flags);
956*fae548d3Szrj 
957*fae548d3Szrj   if (this->script_options_->saw_sections_clause() && !is_reloc)
958*fae548d3Szrj     {
959*fae548d3Szrj       // We are using a SECTIONS clause, so the output section is
960*fae548d3Szrj       // chosen based only on the name.
961*fae548d3Szrj 
962*fae548d3Szrj       Script_sections* ss = this->script_options_->script_sections();
963*fae548d3Szrj       const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
964*fae548d3Szrj       Output_section** output_section_slot;
965*fae548d3Szrj       Script_sections::Section_type script_section_type;
966*fae548d3Szrj       const char* orig_name = name;
967*fae548d3Szrj       bool keep;
968*fae548d3Szrj       name = ss->output_section_name(file_name, name, &output_section_slot,
969*fae548d3Szrj 				     &script_section_type, &keep,
970*fae548d3Szrj 				     match_input_spec);
971*fae548d3Szrj 
972*fae548d3Szrj       if (name == NULL)
973*fae548d3Szrj 	{
974*fae548d3Szrj 	  gold_debug(DEBUG_SCRIPT, _("Unable to create output section '%s' "
975*fae548d3Szrj 				     "because it is not allowed by the "
976*fae548d3Szrj 				     "SECTIONS clause of the linker script"),
977*fae548d3Szrj 		     orig_name);
978*fae548d3Szrj 	  // The SECTIONS clause says to discard this input section.
979*fae548d3Szrj 	  return NULL;
980*fae548d3Szrj 	}
981*fae548d3Szrj 
982*fae548d3Szrj       // We can only handle script section types ST_NONE and ST_NOLOAD.
983*fae548d3Szrj       switch (script_section_type)
984*fae548d3Szrj 	{
985*fae548d3Szrj 	case Script_sections::ST_NONE:
986*fae548d3Szrj 	  break;
987*fae548d3Szrj 	case Script_sections::ST_NOLOAD:
988*fae548d3Szrj 	  flags &= elfcpp::SHF_ALLOC;
989*fae548d3Szrj 	  break;
990*fae548d3Szrj 	default:
991*fae548d3Szrj 	  gold_unreachable();
992*fae548d3Szrj 	}
993*fae548d3Szrj 
994*fae548d3Szrj       // If this is an orphan section--one not mentioned in the linker
995*fae548d3Szrj       // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
996*fae548d3Szrj       // default processing below.
997*fae548d3Szrj 
998*fae548d3Szrj       if (output_section_slot != NULL)
999*fae548d3Szrj 	{
1000*fae548d3Szrj 	  if (*output_section_slot != NULL)
1001*fae548d3Szrj 	    {
1002*fae548d3Szrj 	      (*output_section_slot)->update_flags_for_input_section(flags);
1003*fae548d3Szrj 	      return *output_section_slot;
1004*fae548d3Szrj 	    }
1005*fae548d3Szrj 
1006*fae548d3Szrj 	  // We don't put sections found in the linker script into
1007*fae548d3Szrj 	  // SECTION_NAME_MAP_.  That keeps us from getting confused
1008*fae548d3Szrj 	  // if an orphan section is mapped to a section with the same
1009*fae548d3Szrj 	  // name as one in the linker script.
1010*fae548d3Szrj 
1011*fae548d3Szrj 	  name = this->namepool_.add(name, false, NULL);
1012*fae548d3Szrj 
1013*fae548d3Szrj 	  Output_section* os = this->make_output_section(name, type, flags,
1014*fae548d3Szrj 							 order, is_relro);
1015*fae548d3Szrj 
1016*fae548d3Szrj 	  os->set_found_in_sections_clause();
1017*fae548d3Szrj 
1018*fae548d3Szrj 	  // Special handling for NOLOAD sections.
1019*fae548d3Szrj 	  if (script_section_type == Script_sections::ST_NOLOAD)
1020*fae548d3Szrj 	    {
1021*fae548d3Szrj 	      os->set_is_noload();
1022*fae548d3Szrj 
1023*fae548d3Szrj 	      // The constructor of Output_section sets addresses of non-ALLOC
1024*fae548d3Szrj 	      // sections to 0 by default.  We don't want that for NOLOAD
1025*fae548d3Szrj 	      // sections even if they have no SHF_ALLOC flag.
1026*fae548d3Szrj 	      if ((os->flags() & elfcpp::SHF_ALLOC) == 0
1027*fae548d3Szrj 		  && os->is_address_valid())
1028*fae548d3Szrj 		{
1029*fae548d3Szrj 		  gold_assert(os->address() == 0
1030*fae548d3Szrj 			      && !os->is_offset_valid()
1031*fae548d3Szrj 			      && !os->is_data_size_valid());
1032*fae548d3Szrj 		  os->reset_address_and_file_offset();
1033*fae548d3Szrj 		}
1034*fae548d3Szrj 	    }
1035*fae548d3Szrj 
1036*fae548d3Szrj 	  *output_section_slot = os;
1037*fae548d3Szrj 	  return os;
1038*fae548d3Szrj 	}
1039*fae548d3Szrj     }
1040*fae548d3Szrj 
1041*fae548d3Szrj   // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
1042*fae548d3Szrj 
1043*fae548d3Szrj   size_t len = strlen(name);
1044*fae548d3Szrj   std::string uncompressed_name;
1045*fae548d3Szrj 
1046*fae548d3Szrj   // Compressed debug sections should be mapped to the corresponding
1047*fae548d3Szrj   // uncompressed section.
1048*fae548d3Szrj   if (is_compressed_debug_section(name))
1049*fae548d3Szrj     {
1050*fae548d3Szrj       uncompressed_name =
1051*fae548d3Szrj 	  corresponding_uncompressed_section_name(std::string(name, len));
1052*fae548d3Szrj       name = uncompressed_name.c_str();
1053*fae548d3Szrj       len = uncompressed_name.length();
1054*fae548d3Szrj     }
1055*fae548d3Szrj 
1056*fae548d3Szrj   // Turn NAME from the name of the input section into the name of the
1057*fae548d3Szrj   // output section.
1058*fae548d3Szrj   if (is_input_section
1059*fae548d3Szrj       && !this->script_options_->saw_sections_clause()
1060*fae548d3Szrj       && !parameters->options().relocatable())
1061*fae548d3Szrj     {
1062*fae548d3Szrj       const char *orig_name = name;
1063*fae548d3Szrj       name = parameters->target().output_section_name(relobj, name, &len);
1064*fae548d3Szrj       if (name == NULL)
1065*fae548d3Szrj 	name = Layout::output_section_name(relobj, orig_name, &len);
1066*fae548d3Szrj     }
1067*fae548d3Szrj 
1068*fae548d3Szrj   Stringpool::Key name_key;
1069*fae548d3Szrj   name = this->namepool_.add_with_length(name, len, true, &name_key);
1070*fae548d3Szrj 
1071*fae548d3Szrj   // Find or make the output section.  The output section is selected
1072*fae548d3Szrj   // based on the section name, type, and flags.
1073*fae548d3Szrj   return this->get_output_section(name, name_key, type, flags, order, is_relro);
1074*fae548d3Szrj }
1075*fae548d3Szrj 
1076*fae548d3Szrj // For incremental links, record the initial fixed layout of a section
1077*fae548d3Szrj // from the base file, and return a pointer to the Output_section.
1078*fae548d3Szrj 
1079*fae548d3Szrj template<int size, bool big_endian>
1080*fae548d3Szrj Output_section*
init_fixed_output_section(const char * name,elfcpp::Shdr<size,big_endian> & shdr)1081*fae548d3Szrj Layout::init_fixed_output_section(const char* name,
1082*fae548d3Szrj 				  elfcpp::Shdr<size, big_endian>& shdr)
1083*fae548d3Szrj {
1084*fae548d3Szrj   unsigned int sh_type = shdr.get_sh_type();
1085*fae548d3Szrj 
1086*fae548d3Szrj   // We preserve the layout of PROGBITS, NOBITS, INIT_ARRAY, FINI_ARRAY,
1087*fae548d3Szrj   // PRE_INIT_ARRAY, and NOTE sections.
1088*fae548d3Szrj   // All others will be created from scratch and reallocated.
1089*fae548d3Szrj   if (!can_incremental_update(sh_type))
1090*fae548d3Szrj     return NULL;
1091*fae548d3Szrj 
1092*fae548d3Szrj   // If we're generating a .gdb_index section, we need to regenerate
1093*fae548d3Szrj   // it from scratch.
1094*fae548d3Szrj   if (parameters->options().gdb_index()
1095*fae548d3Szrj       && sh_type == elfcpp::SHT_PROGBITS
1096*fae548d3Szrj       && strcmp(name, ".gdb_index") == 0)
1097*fae548d3Szrj     return NULL;
1098*fae548d3Szrj 
1099*fae548d3Szrj   typename elfcpp::Elf_types<size>::Elf_Addr sh_addr = shdr.get_sh_addr();
1100*fae548d3Szrj   typename elfcpp::Elf_types<size>::Elf_Off sh_offset = shdr.get_sh_offset();
1101*fae548d3Szrj   typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
1102*fae548d3Szrj   typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
1103*fae548d3Szrj   typename elfcpp::Elf_types<size>::Elf_WXword sh_addralign =
1104*fae548d3Szrj       shdr.get_sh_addralign();
1105*fae548d3Szrj 
1106*fae548d3Szrj   // Make the output section.
1107*fae548d3Szrj   Stringpool::Key name_key;
1108*fae548d3Szrj   name = this->namepool_.add(name, true, &name_key);
1109*fae548d3Szrj   Output_section* os = this->get_output_section(name, name_key, sh_type,
1110*fae548d3Szrj 						sh_flags, ORDER_INVALID, false);
1111*fae548d3Szrj   os->set_fixed_layout(sh_addr, sh_offset, sh_size, sh_addralign);
1112*fae548d3Szrj   if (sh_type != elfcpp::SHT_NOBITS)
1113*fae548d3Szrj     this->free_list_.remove(sh_offset, sh_offset + sh_size);
1114*fae548d3Szrj   return os;
1115*fae548d3Szrj }
1116*fae548d3Szrj 
1117*fae548d3Szrj // Return the index by which an input section should be ordered.  This
1118*fae548d3Szrj // is used to sort some .text sections, for compatibility with GNU ld.
1119*fae548d3Szrj 
1120*fae548d3Szrj int
special_ordering_of_input_section(const char * name)1121*fae548d3Szrj Layout::special_ordering_of_input_section(const char* name)
1122*fae548d3Szrj {
1123*fae548d3Szrj   // The GNU linker has some special handling for some sections that
1124*fae548d3Szrj   // wind up in the .text section.  Sections that start with these
1125*fae548d3Szrj   // prefixes must appear first, and must appear in the order listed
1126*fae548d3Szrj   // here.
1127*fae548d3Szrj   static const char* const text_section_sort[] =
1128*fae548d3Szrj   {
1129*fae548d3Szrj     ".text.unlikely",
1130*fae548d3Szrj     ".text.exit",
1131*fae548d3Szrj     ".text.startup",
1132*fae548d3Szrj     ".text.hot",
1133*fae548d3Szrj     ".text.sorted"
1134*fae548d3Szrj   };
1135*fae548d3Szrj 
1136*fae548d3Szrj   for (size_t i = 0;
1137*fae548d3Szrj        i < sizeof(text_section_sort) / sizeof(text_section_sort[0]);
1138*fae548d3Szrj        i++)
1139*fae548d3Szrj     if (is_prefix_of(text_section_sort[i], name))
1140*fae548d3Szrj       return i;
1141*fae548d3Szrj 
1142*fae548d3Szrj   return -1;
1143*fae548d3Szrj }
1144*fae548d3Szrj 
1145*fae548d3Szrj // Return the output section to use for input section SHNDX, with name
1146*fae548d3Szrj // NAME, with header HEADER, from object OBJECT.  RELOC_SHNDX is the
1147*fae548d3Szrj // index of a relocation section which applies to this section, or 0
1148*fae548d3Szrj // if none, or -1U if more than one.  RELOC_TYPE is the type of the
1149*fae548d3Szrj // relocation section if there is one.  Set *OFF to the offset of this
1150*fae548d3Szrj // input section without the output section.  Return NULL if the
1151*fae548d3Szrj // section should be discarded.  Set *OFF to -1 if the section
1152*fae548d3Szrj // contents should not be written directly to the output file, but
1153*fae548d3Szrj // will instead receive special handling.
1154*fae548d3Szrj 
1155*fae548d3Szrj template<int size, bool big_endian>
1156*fae548d3Szrj Output_section*
layout(Sized_relobj_file<size,big_endian> * object,unsigned int shndx,const char * name,const elfcpp::Shdr<size,big_endian> & shdr,unsigned int sh_type,unsigned int reloc_shndx,unsigned int,off_t * off)1157*fae548d3Szrj Layout::layout(Sized_relobj_file<size, big_endian>* object, unsigned int shndx,
1158*fae548d3Szrj 	       const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
1159*fae548d3Szrj 	       unsigned int sh_type, unsigned int reloc_shndx,
1160*fae548d3Szrj 	       unsigned int, off_t* off)
1161*fae548d3Szrj {
1162*fae548d3Szrj   *off = 0;
1163*fae548d3Szrj 
1164*fae548d3Szrj   if (!this->include_section(object, name, shdr))
1165*fae548d3Szrj     return NULL;
1166*fae548d3Szrj 
1167*fae548d3Szrj   // In a relocatable link a grouped section must not be combined with
1168*fae548d3Szrj   // any other sections.
1169*fae548d3Szrj   Output_section* os;
1170*fae548d3Szrj   if (parameters->options().relocatable()
1171*fae548d3Szrj       && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
1172*fae548d3Szrj     {
1173*fae548d3Szrj       // Some flags in the input section should not be automatically
1174*fae548d3Szrj       // copied to the output section.
1175*fae548d3Szrj       elfcpp::Elf_Xword flags = (shdr.get_sh_flags()
1176*fae548d3Szrj 				 & ~ elfcpp::SHF_COMPRESSED);
1177*fae548d3Szrj       name = this->namepool_.add(name, true, NULL);
1178*fae548d3Szrj       os = this->make_output_section(name, sh_type, flags,
1179*fae548d3Szrj 				     ORDER_INVALID, false);
1180*fae548d3Szrj     }
1181*fae548d3Szrj   else
1182*fae548d3Szrj     {
1183*fae548d3Szrj       // All ".text.unlikely.*" sections can be moved to a unique
1184*fae548d3Szrj       // segment with --text-unlikely-segment option.
1185*fae548d3Szrj       bool text_unlikely_segment
1186*fae548d3Szrj           = (parameters->options().text_unlikely_segment()
1187*fae548d3Szrj              && is_prefix_of(".text.unlikely",
1188*fae548d3Szrj                              object->section_name(shndx).c_str()));
1189*fae548d3Szrj       if (text_unlikely_segment)
1190*fae548d3Szrj         {
1191*fae548d3Szrj 	  elfcpp::Elf_Xword flags
1192*fae548d3Szrj 	    = this->get_output_section_flags(shdr.get_sh_flags());
1193*fae548d3Szrj 
1194*fae548d3Szrj 	  Stringpool::Key name_key;
1195*fae548d3Szrj 	  const char* os_name = this->namepool_.add(".text.unlikely", true,
1196*fae548d3Szrj 						    &name_key);
1197*fae548d3Szrj 	  os = this->get_output_section(os_name, name_key, sh_type, flags,
1198*fae548d3Szrj 					ORDER_INVALID, false);
1199*fae548d3Szrj           // Map this output section to a unique segment.  This is done to
1200*fae548d3Szrj           // separate "text" that is not likely to be executed from "text"
1201*fae548d3Szrj           // that is likely executed.
1202*fae548d3Szrj 	  os->set_is_unique_segment();
1203*fae548d3Szrj         }
1204*fae548d3Szrj       else
1205*fae548d3Szrj 	{
1206*fae548d3Szrj 	  // Plugins can choose to place one or more subsets of sections in
1207*fae548d3Szrj 	  // unique segments and this is done by mapping these section subsets
1208*fae548d3Szrj 	  // to unique output sections.  Check if this section needs to be
1209*fae548d3Szrj 	  // remapped to a unique output section.
1210*fae548d3Szrj 	  Section_segment_map::iterator it
1211*fae548d3Szrj 	    = this->section_segment_map_.find(Const_section_id(object, shndx));
1212*fae548d3Szrj 	  if (it == this->section_segment_map_.end())
1213*fae548d3Szrj 	    {
1214*fae548d3Szrj 	      os = this->choose_output_section(object, name, sh_type,
1215*fae548d3Szrj 					       shdr.get_sh_flags(), true,
1216*fae548d3Szrj 					       ORDER_INVALID, false, false,
1217*fae548d3Szrj 					       true);
1218*fae548d3Szrj 	    }
1219*fae548d3Szrj 	  else
1220*fae548d3Szrj 	    {
1221*fae548d3Szrj 	      // We know the name of the output section, directly call
1222*fae548d3Szrj 	      // get_output_section here by-passing choose_output_section.
1223*fae548d3Szrj 	      elfcpp::Elf_Xword flags
1224*fae548d3Szrj 	        = this->get_output_section_flags(shdr.get_sh_flags());
1225*fae548d3Szrj 
1226*fae548d3Szrj 	      const char* os_name = it->second->name;
1227*fae548d3Szrj 	      Stringpool::Key name_key;
1228*fae548d3Szrj 	      os_name = this->namepool_.add(os_name, true, &name_key);
1229*fae548d3Szrj 	      os = this->get_output_section(os_name, name_key, sh_type, flags,
1230*fae548d3Szrj 					ORDER_INVALID, false);
1231*fae548d3Szrj 	      if (!os->is_unique_segment())
1232*fae548d3Szrj 	        {
1233*fae548d3Szrj 	          os->set_is_unique_segment();
1234*fae548d3Szrj 	          os->set_extra_segment_flags(it->second->flags);
1235*fae548d3Szrj 	          os->set_segment_alignment(it->second->align);
1236*fae548d3Szrj 	        }
1237*fae548d3Szrj 	    }
1238*fae548d3Szrj 	  }
1239*fae548d3Szrj       if (os == NULL)
1240*fae548d3Szrj 	return NULL;
1241*fae548d3Szrj     }
1242*fae548d3Szrj 
1243*fae548d3Szrj   // By default the GNU linker sorts input sections whose names match
1244*fae548d3Szrj   // .ctors.*, .dtors.*, .init_array.*, or .fini_array.*.  The
1245*fae548d3Szrj   // sections are sorted by name.  This is used to implement
1246*fae548d3Szrj   // constructor priority ordering.  We are compatible.  When we put
1247*fae548d3Szrj   // .ctor sections in .init_array and .dtor sections in .fini_array,
1248*fae548d3Szrj   // we must also sort plain .ctor and .dtor sections.
1249*fae548d3Szrj   if (!this->script_options_->saw_sections_clause()
1250*fae548d3Szrj       && !parameters->options().relocatable()
1251*fae548d3Szrj       && (is_prefix_of(".ctors.", name)
1252*fae548d3Szrj 	  || is_prefix_of(".dtors.", name)
1253*fae548d3Szrj 	  || is_prefix_of(".init_array.", name)
1254*fae548d3Szrj 	  || is_prefix_of(".fini_array.", name)
1255*fae548d3Szrj 	  || (parameters->options().ctors_in_init_array()
1256*fae548d3Szrj 	      && (strcmp(name, ".ctors") == 0
1257*fae548d3Szrj 		  || strcmp(name, ".dtors") == 0))))
1258*fae548d3Szrj     os->set_must_sort_attached_input_sections();
1259*fae548d3Szrj 
1260*fae548d3Szrj   // By default the GNU linker sorts some special text sections ahead
1261*fae548d3Szrj   // of others.  We are compatible.
1262*fae548d3Szrj   if (parameters->options().text_reorder()
1263*fae548d3Szrj       && !this->script_options_->saw_sections_clause()
1264*fae548d3Szrj       && !this->is_section_ordering_specified()
1265*fae548d3Szrj       && !parameters->options().relocatable()
1266*fae548d3Szrj       && Layout::special_ordering_of_input_section(name) >= 0)
1267*fae548d3Szrj     os->set_must_sort_attached_input_sections();
1268*fae548d3Szrj 
1269*fae548d3Szrj   // If this is a .ctors or .ctors.* section being mapped to a
1270*fae548d3Szrj   // .init_array section, or a .dtors or .dtors.* section being mapped
1271*fae548d3Szrj   // to a .fini_array section, we will need to reverse the words if
1272*fae548d3Szrj   // there is more than one.  Record this section for later.  See
1273*fae548d3Szrj   // ctors_sections_in_init_array above.
1274*fae548d3Szrj   if (!this->script_options_->saw_sections_clause()
1275*fae548d3Szrj       && !parameters->options().relocatable()
1276*fae548d3Szrj       && shdr.get_sh_size() > size / 8
1277*fae548d3Szrj       && (((strcmp(name, ".ctors") == 0
1278*fae548d3Szrj 	    || is_prefix_of(".ctors.", name))
1279*fae548d3Szrj 	   && strcmp(os->name(), ".init_array") == 0)
1280*fae548d3Szrj 	  || ((strcmp(name, ".dtors") == 0
1281*fae548d3Szrj 	       || is_prefix_of(".dtors.", name))
1282*fae548d3Szrj 	      && strcmp(os->name(), ".fini_array") == 0)))
1283*fae548d3Szrj     ctors_sections_in_init_array.insert(Section_id(object, shndx));
1284*fae548d3Szrj 
1285*fae548d3Szrj   // FIXME: Handle SHF_LINK_ORDER somewhere.
1286*fae548d3Szrj 
1287*fae548d3Szrj   elfcpp::Elf_Xword orig_flags = os->flags();
1288*fae548d3Szrj 
1289*fae548d3Szrj   *off = os->add_input_section(this, object, shndx, name, shdr, reloc_shndx,
1290*fae548d3Szrj 			       this->script_options_->saw_sections_clause());
1291*fae548d3Szrj 
1292*fae548d3Szrj   // If the flags changed, we may have to change the order.
1293*fae548d3Szrj   if ((orig_flags & elfcpp::SHF_ALLOC) != 0)
1294*fae548d3Szrj     {
1295*fae548d3Szrj       orig_flags &= (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
1296*fae548d3Szrj       elfcpp::Elf_Xword new_flags =
1297*fae548d3Szrj 	os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
1298*fae548d3Szrj       if (orig_flags != new_flags)
1299*fae548d3Szrj 	os->set_order(this->default_section_order(os, false));
1300*fae548d3Szrj     }
1301*fae548d3Szrj 
1302*fae548d3Szrj   this->have_added_input_section_ = true;
1303*fae548d3Szrj 
1304*fae548d3Szrj   return os;
1305*fae548d3Szrj }
1306*fae548d3Szrj 
1307*fae548d3Szrj // Maps section SECN to SEGMENT s.
1308*fae548d3Szrj void
insert_section_segment_map(Const_section_id secn,Unique_segment_info * s)1309*fae548d3Szrj Layout::insert_section_segment_map(Const_section_id secn,
1310*fae548d3Szrj 				   Unique_segment_info *s)
1311*fae548d3Szrj {
1312*fae548d3Szrj   gold_assert(this->unique_segment_for_sections_specified_);
1313*fae548d3Szrj   this->section_segment_map_[secn] = s;
1314*fae548d3Szrj }
1315*fae548d3Szrj 
1316*fae548d3Szrj // Handle a relocation section when doing a relocatable link.
1317*fae548d3Szrj 
1318*fae548d3Szrj template<int size, bool big_endian>
1319*fae548d3Szrj Output_section*
layout_reloc(Sized_relobj_file<size,big_endian> *,unsigned int,const elfcpp::Shdr<size,big_endian> & shdr,Output_section * data_section,Relocatable_relocs * rr)1320*fae548d3Szrj Layout::layout_reloc(Sized_relobj_file<size, big_endian>*,
1321*fae548d3Szrj 		     unsigned int,
1322*fae548d3Szrj 		     const elfcpp::Shdr<size, big_endian>& shdr,
1323*fae548d3Szrj 		     Output_section* data_section,
1324*fae548d3Szrj 		     Relocatable_relocs* rr)
1325*fae548d3Szrj {
1326*fae548d3Szrj   gold_assert(parameters->options().relocatable()
1327*fae548d3Szrj 	      || parameters->options().emit_relocs());
1328*fae548d3Szrj 
1329*fae548d3Szrj   int sh_type = shdr.get_sh_type();
1330*fae548d3Szrj 
1331*fae548d3Szrj   std::string name;
1332*fae548d3Szrj   if (sh_type == elfcpp::SHT_REL)
1333*fae548d3Szrj     name = ".rel";
1334*fae548d3Szrj   else if (sh_type == elfcpp::SHT_RELA)
1335*fae548d3Szrj     name = ".rela";
1336*fae548d3Szrj   else
1337*fae548d3Szrj     gold_unreachable();
1338*fae548d3Szrj   name += data_section->name();
1339*fae548d3Szrj 
1340*fae548d3Szrj   // If the output data section already has a reloc section, use that;
1341*fae548d3Szrj   // otherwise, make a new one.
1342*fae548d3Szrj   Output_section* os = data_section->reloc_section();
1343*fae548d3Szrj   if (os == NULL)
1344*fae548d3Szrj     {
1345*fae548d3Szrj       const char* n = this->namepool_.add(name.c_str(), true, NULL);
1346*fae548d3Szrj       os = this->make_output_section(n, sh_type, shdr.get_sh_flags(),
1347*fae548d3Szrj 				     ORDER_INVALID, false);
1348*fae548d3Szrj       os->set_should_link_to_symtab();
1349*fae548d3Szrj       os->set_info_section(data_section);
1350*fae548d3Szrj       data_section->set_reloc_section(os);
1351*fae548d3Szrj     }
1352*fae548d3Szrj 
1353*fae548d3Szrj   Output_section_data* posd;
1354*fae548d3Szrj   if (sh_type == elfcpp::SHT_REL)
1355*fae548d3Szrj     {
1356*fae548d3Szrj       os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1357*fae548d3Szrj       posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
1358*fae548d3Szrj 					   size,
1359*fae548d3Szrj 					   big_endian>(rr);
1360*fae548d3Szrj     }
1361*fae548d3Szrj   else if (sh_type == elfcpp::SHT_RELA)
1362*fae548d3Szrj     {
1363*fae548d3Szrj       os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1364*fae548d3Szrj       posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
1365*fae548d3Szrj 					   size,
1366*fae548d3Szrj 					   big_endian>(rr);
1367*fae548d3Szrj     }
1368*fae548d3Szrj   else
1369*fae548d3Szrj     gold_unreachable();
1370*fae548d3Szrj 
1371*fae548d3Szrj   os->add_output_section_data(posd);
1372*fae548d3Szrj   rr->set_output_data(posd);
1373*fae548d3Szrj 
1374*fae548d3Szrj   return os;
1375*fae548d3Szrj }
1376*fae548d3Szrj 
1377*fae548d3Szrj // Handle a group section when doing a relocatable link.
1378*fae548d3Szrj 
1379*fae548d3Szrj template<int size, bool big_endian>
1380*fae548d3Szrj void
layout_group(Symbol_table * symtab,Sized_relobj_file<size,big_endian> * object,unsigned int,const char * group_section_name,const char * signature,const elfcpp::Shdr<size,big_endian> & shdr,elfcpp::Elf_Word flags,std::vector<unsigned int> * shndxes)1381*fae548d3Szrj Layout::layout_group(Symbol_table* symtab,
1382*fae548d3Szrj 		     Sized_relobj_file<size, big_endian>* object,
1383*fae548d3Szrj 		     unsigned int,
1384*fae548d3Szrj 		     const char* group_section_name,
1385*fae548d3Szrj 		     const char* signature,
1386*fae548d3Szrj 		     const elfcpp::Shdr<size, big_endian>& shdr,
1387*fae548d3Szrj 		     elfcpp::Elf_Word flags,
1388*fae548d3Szrj 		     std::vector<unsigned int>* shndxes)
1389*fae548d3Szrj {
1390*fae548d3Szrj   gold_assert(parameters->options().relocatable());
1391*fae548d3Szrj   gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
1392*fae548d3Szrj   group_section_name = this->namepool_.add(group_section_name, true, NULL);
1393*fae548d3Szrj   Output_section* os = this->make_output_section(group_section_name,
1394*fae548d3Szrj 						 elfcpp::SHT_GROUP,
1395*fae548d3Szrj 						 shdr.get_sh_flags(),
1396*fae548d3Szrj 						 ORDER_INVALID, false);
1397*fae548d3Szrj 
1398*fae548d3Szrj   // We need to find a symbol with the signature in the symbol table.
1399*fae548d3Szrj   // If we don't find one now, we need to look again later.
1400*fae548d3Szrj   Symbol* sym = symtab->lookup(signature, NULL);
1401*fae548d3Szrj   if (sym != NULL)
1402*fae548d3Szrj     os->set_info_symndx(sym);
1403*fae548d3Szrj   else
1404*fae548d3Szrj     {
1405*fae548d3Szrj       // Reserve some space to minimize reallocations.
1406*fae548d3Szrj       if (this->group_signatures_.empty())
1407*fae548d3Szrj 	this->group_signatures_.reserve(this->number_of_input_files_ * 16);
1408*fae548d3Szrj 
1409*fae548d3Szrj       // We will wind up using a symbol whose name is the signature.
1410*fae548d3Szrj       // So just put the signature in the symbol name pool to save it.
1411*fae548d3Szrj       signature = symtab->canonicalize_name(signature);
1412*fae548d3Szrj       this->group_signatures_.push_back(Group_signature(os, signature));
1413*fae548d3Szrj     }
1414*fae548d3Szrj 
1415*fae548d3Szrj   os->set_should_link_to_symtab();
1416*fae548d3Szrj   os->set_entsize(4);
1417*fae548d3Szrj 
1418*fae548d3Szrj   section_size_type entry_count =
1419*fae548d3Szrj     convert_to_section_size_type(shdr.get_sh_size() / 4);
1420*fae548d3Szrj   Output_section_data* posd =
1421*fae548d3Szrj     new Output_data_group<size, big_endian>(object, entry_count, flags,
1422*fae548d3Szrj 					    shndxes);
1423*fae548d3Szrj   os->add_output_section_data(posd);
1424*fae548d3Szrj }
1425*fae548d3Szrj 
1426*fae548d3Szrj // Special GNU handling of sections name .eh_frame.  They will
1427*fae548d3Szrj // normally hold exception frame data as defined by the C++ ABI
1428*fae548d3Szrj // (http://codesourcery.com/cxx-abi/).
1429*fae548d3Szrj 
1430*fae548d3Szrj template<int size, bool big_endian>
1431*fae548d3Szrj Output_section*
layout_eh_frame(Sized_relobj_file<size,big_endian> * object,const unsigned char * symbols,off_t symbols_size,const unsigned char * symbol_names,off_t symbol_names_size,unsigned int shndx,const elfcpp::Shdr<size,big_endian> & shdr,unsigned int reloc_shndx,unsigned int reloc_type,off_t * off)1432*fae548d3Szrj Layout::layout_eh_frame(Sized_relobj_file<size, big_endian>* object,
1433*fae548d3Szrj 			const unsigned char* symbols,
1434*fae548d3Szrj 			off_t symbols_size,
1435*fae548d3Szrj 			const unsigned char* symbol_names,
1436*fae548d3Szrj 			off_t symbol_names_size,
1437*fae548d3Szrj 			unsigned int shndx,
1438*fae548d3Szrj 			const elfcpp::Shdr<size, big_endian>& shdr,
1439*fae548d3Szrj 			unsigned int reloc_shndx, unsigned int reloc_type,
1440*fae548d3Szrj 			off_t* off)
1441*fae548d3Szrj {
1442*fae548d3Szrj   const unsigned int unwind_section_type =
1443*fae548d3Szrj       parameters->target().unwind_section_type();
1444*fae548d3Szrj 
1445*fae548d3Szrj   gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1446*fae548d3Szrj 	      || shdr.get_sh_type() == unwind_section_type);
1447*fae548d3Szrj   gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
1448*fae548d3Szrj 
1449*fae548d3Szrj   Output_section* os = this->make_eh_frame_section(object);
1450*fae548d3Szrj   if (os == NULL)
1451*fae548d3Szrj     return NULL;
1452*fae548d3Szrj 
1453*fae548d3Szrj   gold_assert(this->eh_frame_section_ == os);
1454*fae548d3Szrj 
1455*fae548d3Szrj   elfcpp::Elf_Xword orig_flags = os->flags();
1456*fae548d3Szrj 
1457*fae548d3Szrj   Eh_frame::Eh_frame_section_disposition disp =
1458*fae548d3Szrj       Eh_frame::EH_UNRECOGNIZED_SECTION;
1459*fae548d3Szrj   if (!parameters->incremental())
1460*fae548d3Szrj     {
1461*fae548d3Szrj       disp = this->eh_frame_data_->add_ehframe_input_section(object,
1462*fae548d3Szrj 							     symbols,
1463*fae548d3Szrj 							     symbols_size,
1464*fae548d3Szrj 							     symbol_names,
1465*fae548d3Szrj 							     symbol_names_size,
1466*fae548d3Szrj 							     shndx,
1467*fae548d3Szrj 							     reloc_shndx,
1468*fae548d3Szrj 							     reloc_type);
1469*fae548d3Szrj     }
1470*fae548d3Szrj 
1471*fae548d3Szrj   if (disp == Eh_frame::EH_OPTIMIZABLE_SECTION)
1472*fae548d3Szrj     {
1473*fae548d3Szrj       os->update_flags_for_input_section(shdr.get_sh_flags());
1474*fae548d3Szrj 
1475*fae548d3Szrj       // A writable .eh_frame section is a RELRO section.
1476*fae548d3Szrj       if ((orig_flags & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR))
1477*fae548d3Szrj 	  != (os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR)))
1478*fae548d3Szrj 	{
1479*fae548d3Szrj 	  os->set_is_relro();
1480*fae548d3Szrj 	  os->set_order(ORDER_RELRO);
1481*fae548d3Szrj 	}
1482*fae548d3Szrj 
1483*fae548d3Szrj       *off = -1;
1484*fae548d3Szrj       return os;
1485*fae548d3Szrj     }
1486*fae548d3Szrj 
1487*fae548d3Szrj   if (disp == Eh_frame::EH_END_MARKER_SECTION && !this->added_eh_frame_data_)
1488*fae548d3Szrj     {
1489*fae548d3Szrj       // We found the end marker section, so now we can add the set of
1490*fae548d3Szrj       // optimized sections to the output section.  We need to postpone
1491*fae548d3Szrj       // adding this until we've found a section we can optimize so that
1492*fae548d3Szrj       // the .eh_frame section in crtbeginT.o winds up at the start of
1493*fae548d3Szrj       // the output section.
1494*fae548d3Szrj       os->add_output_section_data(this->eh_frame_data_);
1495*fae548d3Szrj       this->added_eh_frame_data_ = true;
1496*fae548d3Szrj      }
1497*fae548d3Szrj 
1498*fae548d3Szrj   // We couldn't handle this .eh_frame section for some reason.
1499*fae548d3Szrj   // Add it as a normal section.
1500*fae548d3Szrj   bool saw_sections_clause = this->script_options_->saw_sections_clause();
1501*fae548d3Szrj   *off = os->add_input_section(this, object, shndx, ".eh_frame", shdr,
1502*fae548d3Szrj 			       reloc_shndx, saw_sections_clause);
1503*fae548d3Szrj   this->have_added_input_section_ = true;
1504*fae548d3Szrj 
1505*fae548d3Szrj   if ((orig_flags & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR))
1506*fae548d3Szrj       != (os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR)))
1507*fae548d3Szrj     os->set_order(this->default_section_order(os, false));
1508*fae548d3Szrj 
1509*fae548d3Szrj   return os;
1510*fae548d3Szrj }
1511*fae548d3Szrj 
1512*fae548d3Szrj void
finalize_eh_frame_section()1513*fae548d3Szrj Layout::finalize_eh_frame_section()
1514*fae548d3Szrj {
1515*fae548d3Szrj   // If we never found an end marker section, we need to add the
1516*fae548d3Szrj   // optimized eh sections to the output section now.
1517*fae548d3Szrj   if (!parameters->incremental()
1518*fae548d3Szrj       && this->eh_frame_section_ != NULL
1519*fae548d3Szrj       && !this->added_eh_frame_data_)
1520*fae548d3Szrj     {
1521*fae548d3Szrj       this->eh_frame_section_->add_output_section_data(this->eh_frame_data_);
1522*fae548d3Szrj       this->added_eh_frame_data_ = true;
1523*fae548d3Szrj     }
1524*fae548d3Szrj }
1525*fae548d3Szrj 
1526*fae548d3Szrj // Create and return the magic .eh_frame section.  Create
1527*fae548d3Szrj // .eh_frame_hdr also if appropriate.  OBJECT is the object with the
1528*fae548d3Szrj // input .eh_frame section; it may be NULL.
1529*fae548d3Szrj 
1530*fae548d3Szrj Output_section*
make_eh_frame_section(const Relobj * object)1531*fae548d3Szrj Layout::make_eh_frame_section(const Relobj* object)
1532*fae548d3Szrj {
1533*fae548d3Szrj   const unsigned int unwind_section_type =
1534*fae548d3Szrj       parameters->target().unwind_section_type();
1535*fae548d3Szrj 
1536*fae548d3Szrj   Output_section* os = this->choose_output_section(object, ".eh_frame",
1537*fae548d3Szrj 						   unwind_section_type,
1538*fae548d3Szrj 						   elfcpp::SHF_ALLOC, false,
1539*fae548d3Szrj 						   ORDER_EHFRAME, false, false,
1540*fae548d3Szrj 						   false);
1541*fae548d3Szrj   if (os == NULL)
1542*fae548d3Szrj     return NULL;
1543*fae548d3Szrj 
1544*fae548d3Szrj   if (this->eh_frame_section_ == NULL)
1545*fae548d3Szrj     {
1546*fae548d3Szrj       this->eh_frame_section_ = os;
1547*fae548d3Szrj       this->eh_frame_data_ = new Eh_frame();
1548*fae548d3Szrj 
1549*fae548d3Szrj       // For incremental linking, we do not optimize .eh_frame sections
1550*fae548d3Szrj       // or create a .eh_frame_hdr section.
1551*fae548d3Szrj       if (parameters->options().eh_frame_hdr() && !parameters->incremental())
1552*fae548d3Szrj 	{
1553*fae548d3Szrj 	  Output_section* hdr_os =
1554*fae548d3Szrj 	    this->choose_output_section(NULL, ".eh_frame_hdr",
1555*fae548d3Szrj 					unwind_section_type,
1556*fae548d3Szrj 					elfcpp::SHF_ALLOC, false,
1557*fae548d3Szrj 					ORDER_EHFRAME, false, false,
1558*fae548d3Szrj 					false);
1559*fae548d3Szrj 
1560*fae548d3Szrj 	  if (hdr_os != NULL)
1561*fae548d3Szrj 	    {
1562*fae548d3Szrj 	      Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
1563*fae548d3Szrj 							this->eh_frame_data_);
1564*fae548d3Szrj 	      hdr_os->add_output_section_data(hdr_posd);
1565*fae548d3Szrj 
1566*fae548d3Szrj 	      hdr_os->set_after_input_sections();
1567*fae548d3Szrj 
1568*fae548d3Szrj 	      if (!this->script_options_->saw_phdrs_clause())
1569*fae548d3Szrj 		{
1570*fae548d3Szrj 		  Output_segment* hdr_oseg;
1571*fae548d3Szrj 		  hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
1572*fae548d3Szrj 						       elfcpp::PF_R);
1573*fae548d3Szrj 		  hdr_oseg->add_output_section_to_nonload(hdr_os,
1574*fae548d3Szrj 							  elfcpp::PF_R);
1575*fae548d3Szrj 		}
1576*fae548d3Szrj 
1577*fae548d3Szrj 	      this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
1578*fae548d3Szrj 	    }
1579*fae548d3Szrj 	}
1580*fae548d3Szrj     }
1581*fae548d3Szrj 
1582*fae548d3Szrj   return os;
1583*fae548d3Szrj }
1584*fae548d3Szrj 
1585*fae548d3Szrj // Add an exception frame for a PLT.  This is called from target code.
1586*fae548d3Szrj 
1587*fae548d3Szrj void
add_eh_frame_for_plt(Output_data * plt,const unsigned char * cie_data,size_t cie_length,const unsigned char * fde_data,size_t fde_length)1588*fae548d3Szrj Layout::add_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
1589*fae548d3Szrj 			     size_t cie_length, const unsigned char* fde_data,
1590*fae548d3Szrj 			     size_t fde_length)
1591*fae548d3Szrj {
1592*fae548d3Szrj   if (parameters->incremental())
1593*fae548d3Szrj     {
1594*fae548d3Szrj       // FIXME: Maybe this could work some day....
1595*fae548d3Szrj       return;
1596*fae548d3Szrj     }
1597*fae548d3Szrj   Output_section* os = this->make_eh_frame_section(NULL);
1598*fae548d3Szrj   if (os == NULL)
1599*fae548d3Szrj     return;
1600*fae548d3Szrj   this->eh_frame_data_->add_ehframe_for_plt(plt, cie_data, cie_length,
1601*fae548d3Szrj 					    fde_data, fde_length);
1602*fae548d3Szrj   if (!this->added_eh_frame_data_)
1603*fae548d3Szrj     {
1604*fae548d3Szrj       os->add_output_section_data(this->eh_frame_data_);
1605*fae548d3Szrj       this->added_eh_frame_data_ = true;
1606*fae548d3Szrj     }
1607*fae548d3Szrj }
1608*fae548d3Szrj 
1609*fae548d3Szrj // Remove all post-map .eh_frame information for a PLT.
1610*fae548d3Szrj 
1611*fae548d3Szrj void
remove_eh_frame_for_plt(Output_data * plt,const unsigned char * cie_data,size_t cie_length)1612*fae548d3Szrj Layout::remove_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
1613*fae548d3Szrj 				size_t cie_length)
1614*fae548d3Szrj {
1615*fae548d3Szrj   if (parameters->incremental())
1616*fae548d3Szrj     {
1617*fae548d3Szrj       // FIXME: Maybe this could work some day....
1618*fae548d3Szrj       return;
1619*fae548d3Szrj     }
1620*fae548d3Szrj   this->eh_frame_data_->remove_ehframe_for_plt(plt, cie_data, cie_length);
1621*fae548d3Szrj }
1622*fae548d3Szrj 
1623*fae548d3Szrj // Scan a .debug_info or .debug_types section, and add summary
1624*fae548d3Szrj // information to the .gdb_index section.
1625*fae548d3Szrj 
1626*fae548d3Szrj template<int size, bool big_endian>
1627*fae548d3Szrj void
add_to_gdb_index(bool is_type_unit,Sized_relobj<size,big_endian> * object,const unsigned char * symbols,off_t symbols_size,unsigned int shndx,unsigned int reloc_shndx,unsigned int reloc_type)1628*fae548d3Szrj Layout::add_to_gdb_index(bool is_type_unit,
1629*fae548d3Szrj 			 Sized_relobj<size, big_endian>* object,
1630*fae548d3Szrj 			 const unsigned char* symbols,
1631*fae548d3Szrj 			 off_t symbols_size,
1632*fae548d3Szrj 			 unsigned int shndx,
1633*fae548d3Szrj 			 unsigned int reloc_shndx,
1634*fae548d3Szrj 			 unsigned int reloc_type)
1635*fae548d3Szrj {
1636*fae548d3Szrj   if (this->gdb_index_data_ == NULL)
1637*fae548d3Szrj     {
1638*fae548d3Szrj       Output_section* os = this->choose_output_section(NULL, ".gdb_index",
1639*fae548d3Szrj 						       elfcpp::SHT_PROGBITS, 0,
1640*fae548d3Szrj 						       false, ORDER_INVALID,
1641*fae548d3Szrj 						       false, false, false);
1642*fae548d3Szrj       if (os == NULL)
1643*fae548d3Szrj 	return;
1644*fae548d3Szrj 
1645*fae548d3Szrj       this->gdb_index_data_ = new Gdb_index(os);
1646*fae548d3Szrj       os->add_output_section_data(this->gdb_index_data_);
1647*fae548d3Szrj       os->set_after_input_sections();
1648*fae548d3Szrj     }
1649*fae548d3Szrj 
1650*fae548d3Szrj   this->gdb_index_data_->scan_debug_info(is_type_unit, object, symbols,
1651*fae548d3Szrj 					 symbols_size, shndx, reloc_shndx,
1652*fae548d3Szrj 					 reloc_type);
1653*fae548d3Szrj }
1654*fae548d3Szrj 
1655*fae548d3Szrj // Add POSD to an output section using NAME, TYPE, and FLAGS.  Return
1656*fae548d3Szrj // the output section.
1657*fae548d3Szrj 
1658*fae548d3Szrj Output_section*
add_output_section_data(const char * name,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags,Output_section_data * posd,Output_section_order order,bool is_relro)1659*fae548d3Szrj Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
1660*fae548d3Szrj 				elfcpp::Elf_Xword flags,
1661*fae548d3Szrj 				Output_section_data* posd,
1662*fae548d3Szrj 				Output_section_order order, bool is_relro)
1663*fae548d3Szrj {
1664*fae548d3Szrj   Output_section* os = this->choose_output_section(NULL, name, type, flags,
1665*fae548d3Szrj 						   false, order, is_relro,
1666*fae548d3Szrj 						   false, false);
1667*fae548d3Szrj   if (os != NULL)
1668*fae548d3Szrj     os->add_output_section_data(posd);
1669*fae548d3Szrj   return os;
1670*fae548d3Szrj }
1671*fae548d3Szrj 
1672*fae548d3Szrj // Map section flags to segment flags.
1673*fae548d3Szrj 
1674*fae548d3Szrj elfcpp::Elf_Word
section_flags_to_segment(elfcpp::Elf_Xword flags)1675*fae548d3Szrj Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
1676*fae548d3Szrj {
1677*fae548d3Szrj   elfcpp::Elf_Word ret = elfcpp::PF_R;
1678*fae548d3Szrj   if ((flags & elfcpp::SHF_WRITE) != 0)
1679*fae548d3Szrj     ret |= elfcpp::PF_W;
1680*fae548d3Szrj   if ((flags & elfcpp::SHF_EXECINSTR) != 0)
1681*fae548d3Szrj     ret |= elfcpp::PF_X;
1682*fae548d3Szrj   return ret;
1683*fae548d3Szrj }
1684*fae548d3Szrj 
1685*fae548d3Szrj // Make a new Output_section, and attach it to segments as
1686*fae548d3Szrj // appropriate.  ORDER is the order in which this section should
1687*fae548d3Szrj // appear in the output segment.  IS_RELRO is true if this is a relro
1688*fae548d3Szrj // (read-only after relocations) section.
1689*fae548d3Szrj 
1690*fae548d3Szrj Output_section*
make_output_section(const char * name,elfcpp::Elf_Word type,elfcpp::Elf_Xword flags,Output_section_order order,bool is_relro)1691*fae548d3Szrj Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
1692*fae548d3Szrj 			    elfcpp::Elf_Xword flags,
1693*fae548d3Szrj 			    Output_section_order order, bool is_relro)
1694*fae548d3Szrj {
1695*fae548d3Szrj   Output_section* os;
1696*fae548d3Szrj   if ((flags & elfcpp::SHF_ALLOC) == 0
1697*fae548d3Szrj       && strcmp(parameters->options().compress_debug_sections(), "none") != 0
1698*fae548d3Szrj       && is_compressible_debug_section(name))
1699*fae548d3Szrj     os = new Output_compressed_section(&parameters->options(), name, type,
1700*fae548d3Szrj 				       flags);
1701*fae548d3Szrj   else if ((flags & elfcpp::SHF_ALLOC) == 0
1702*fae548d3Szrj 	   && parameters->options().strip_debug_non_line()
1703*fae548d3Szrj 	   && strcmp(".debug_abbrev", name) == 0)
1704*fae548d3Szrj     {
1705*fae548d3Szrj       os = this->debug_abbrev_ = new Output_reduced_debug_abbrev_section(
1706*fae548d3Szrj 	  name, type, flags);
1707*fae548d3Szrj       if (this->debug_info_)
1708*fae548d3Szrj 	this->debug_info_->set_abbreviations(this->debug_abbrev_);
1709*fae548d3Szrj     }
1710*fae548d3Szrj   else if ((flags & elfcpp::SHF_ALLOC) == 0
1711*fae548d3Szrj 	   && parameters->options().strip_debug_non_line()
1712*fae548d3Szrj 	   && strcmp(".debug_info", name) == 0)
1713*fae548d3Szrj     {
1714*fae548d3Szrj       os = this->debug_info_ = new Output_reduced_debug_info_section(
1715*fae548d3Szrj 	  name, type, flags);
1716*fae548d3Szrj       if (this->debug_abbrev_)
1717*fae548d3Szrj 	this->debug_info_->set_abbreviations(this->debug_abbrev_);
1718*fae548d3Szrj     }
1719*fae548d3Szrj   else
1720*fae548d3Szrj     {
1721*fae548d3Szrj       // Sometimes .init_array*, .preinit_array* and .fini_array* do
1722*fae548d3Szrj       // not have correct section types.  Force them here.
1723*fae548d3Szrj       if (type == elfcpp::SHT_PROGBITS)
1724*fae548d3Szrj 	{
1725*fae548d3Szrj 	  if (is_prefix_of(".init_array", name))
1726*fae548d3Szrj 	    type = elfcpp::SHT_INIT_ARRAY;
1727*fae548d3Szrj 	  else if (is_prefix_of(".preinit_array", name))
1728*fae548d3Szrj 	    type = elfcpp::SHT_PREINIT_ARRAY;
1729*fae548d3Szrj 	  else if (is_prefix_of(".fini_array", name))
1730*fae548d3Szrj 	    type = elfcpp::SHT_FINI_ARRAY;
1731*fae548d3Szrj 	}
1732*fae548d3Szrj 
1733*fae548d3Szrj       // FIXME: const_cast is ugly.
1734*fae548d3Szrj       Target* target = const_cast<Target*>(&parameters->target());
1735*fae548d3Szrj       os = target->make_output_section(name, type, flags);
1736*fae548d3Szrj     }
1737*fae548d3Szrj 
1738*fae548d3Szrj   // With -z relro, we have to recognize the special sections by name.
1739*fae548d3Szrj   // There is no other way.
1740*fae548d3Szrj   bool is_relro_local = false;
1741*fae548d3Szrj   if (!this->script_options_->saw_sections_clause()
1742*fae548d3Szrj       && parameters->options().relro()
1743*fae548d3Szrj       && (flags & elfcpp::SHF_ALLOC) != 0
1744*fae548d3Szrj       && (flags & elfcpp::SHF_WRITE) != 0)
1745*fae548d3Szrj     {
1746*fae548d3Szrj       if (type == elfcpp::SHT_PROGBITS)
1747*fae548d3Szrj 	{
1748*fae548d3Szrj 	  if ((flags & elfcpp::SHF_TLS) != 0)
1749*fae548d3Szrj 	    is_relro = true;
1750*fae548d3Szrj 	  else if (strcmp(name, ".data.rel.ro") == 0)
1751*fae548d3Szrj 	    is_relro = true;
1752*fae548d3Szrj 	  else if (strcmp(name, ".data.rel.ro.local") == 0)
1753*fae548d3Szrj 	    {
1754*fae548d3Szrj 	      is_relro = true;
1755*fae548d3Szrj 	      is_relro_local = true;
1756*fae548d3Szrj 	    }
1757*fae548d3Szrj 	  else if (strcmp(name, ".ctors") == 0
1758*fae548d3Szrj 		   || strcmp(name, ".dtors") == 0
1759*fae548d3Szrj 		   || strcmp(name, ".jcr") == 0)
1760*fae548d3Szrj 	    is_relro = true;
1761*fae548d3Szrj 	}
1762*fae548d3Szrj       else if (type == elfcpp::SHT_INIT_ARRAY
1763*fae548d3Szrj 	       || type == elfcpp::SHT_FINI_ARRAY
1764*fae548d3Szrj 	       || type == elfcpp::SHT_PREINIT_ARRAY)
1765*fae548d3Szrj 	is_relro = true;
1766*fae548d3Szrj     }
1767*fae548d3Szrj 
1768*fae548d3Szrj   if (is_relro)
1769*fae548d3Szrj     os->set_is_relro();
1770*fae548d3Szrj 
1771*fae548d3Szrj   if (order == ORDER_INVALID && (flags & elfcpp::SHF_ALLOC) != 0)
1772*fae548d3Szrj     order = this->default_section_order(os, is_relro_local);
1773*fae548d3Szrj 
1774*fae548d3Szrj   os->set_order(order);
1775*fae548d3Szrj 
1776*fae548d3Szrj   parameters->target().new_output_section(os);
1777*fae548d3Szrj 
1778*fae548d3Szrj   this->section_list_.push_back(os);
1779*fae548d3Szrj 
1780*fae548d3Szrj   // The GNU linker by default sorts some sections by priority, so we
1781*fae548d3Szrj   // do the same.  We need to know that this might happen before we
1782*fae548d3Szrj   // attach any input sections.
1783*fae548d3Szrj   if (!this->script_options_->saw_sections_clause()
1784*fae548d3Szrj       && !parameters->options().relocatable()
1785*fae548d3Szrj       && (strcmp(name, ".init_array") == 0
1786*fae548d3Szrj 	  || strcmp(name, ".fini_array") == 0
1787*fae548d3Szrj 	  || (!parameters->options().ctors_in_init_array()
1788*fae548d3Szrj 	      && (strcmp(name, ".ctors") == 0
1789*fae548d3Szrj 		  || strcmp(name, ".dtors") == 0))))
1790*fae548d3Szrj     os->set_may_sort_attached_input_sections();
1791*fae548d3Szrj 
1792*fae548d3Szrj   // The GNU linker by default sorts .text.{unlikely,exit,startup,hot}
1793*fae548d3Szrj   // sections before other .text sections.  We are compatible.  We
1794*fae548d3Szrj   // need to know that this might happen before we attach any input
1795*fae548d3Szrj   // sections.
1796*fae548d3Szrj   if (parameters->options().text_reorder()
1797*fae548d3Szrj       && !this->script_options_->saw_sections_clause()
1798*fae548d3Szrj       && !this->is_section_ordering_specified()
1799*fae548d3Szrj       && !parameters->options().relocatable()
1800*fae548d3Szrj       && strcmp(name, ".text") == 0)
1801*fae548d3Szrj     os->set_may_sort_attached_input_sections();
1802*fae548d3Szrj 
1803*fae548d3Szrj   // GNU linker sorts section by name with --sort-section=name.
1804*fae548d3Szrj   if (strcmp(parameters->options().sort_section(), "name") == 0)
1805*fae548d3Szrj       os->set_must_sort_attached_input_sections();
1806*fae548d3Szrj 
1807*fae548d3Szrj   // Check for .stab*str sections, as .stab* sections need to link to
1808*fae548d3Szrj   // them.
1809*fae548d3Szrj   if (type == elfcpp::SHT_STRTAB
1810*fae548d3Szrj       && !this->have_stabstr_section_
1811*fae548d3Szrj       && strncmp(name, ".stab", 5) == 0
1812*fae548d3Szrj       && strcmp(name + strlen(name) - 3, "str") == 0)
1813*fae548d3Szrj     this->have_stabstr_section_ = true;
1814*fae548d3Szrj 
1815*fae548d3Szrj   // During a full incremental link, we add patch space to most
1816*fae548d3Szrj   // PROGBITS and NOBITS sections.  Flag those that may be
1817*fae548d3Szrj   // arbitrarily padded.
1818*fae548d3Szrj   if ((type == elfcpp::SHT_PROGBITS || type == elfcpp::SHT_NOBITS)
1819*fae548d3Szrj       && order != ORDER_INTERP
1820*fae548d3Szrj       && order != ORDER_INIT
1821*fae548d3Szrj       && order != ORDER_PLT
1822*fae548d3Szrj       && order != ORDER_FINI
1823*fae548d3Szrj       && order != ORDER_RELRO_LAST
1824*fae548d3Szrj       && order != ORDER_NON_RELRO_FIRST
1825*fae548d3Szrj       && strcmp(name, ".eh_frame") != 0
1826*fae548d3Szrj       && strcmp(name, ".ctors") != 0
1827*fae548d3Szrj       && strcmp(name, ".dtors") != 0
1828*fae548d3Szrj       && strcmp(name, ".jcr") != 0)
1829*fae548d3Szrj     {
1830*fae548d3Szrj       os->set_is_patch_space_allowed();
1831*fae548d3Szrj 
1832*fae548d3Szrj       // Certain sections require "holes" to be filled with
1833*fae548d3Szrj       // specific fill patterns.  These fill patterns may have
1834*fae548d3Szrj       // a minimum size, so we must prevent allocations from the
1835*fae548d3Szrj       // free list that leave a hole smaller than the minimum.
1836*fae548d3Szrj       if (strcmp(name, ".debug_info") == 0)
1837*fae548d3Szrj 	os->set_free_space_fill(new Output_fill_debug_info(false));
1838*fae548d3Szrj       else if (strcmp(name, ".debug_types") == 0)
1839*fae548d3Szrj 	os->set_free_space_fill(new Output_fill_debug_info(true));
1840*fae548d3Szrj       else if (strcmp(name, ".debug_line") == 0)
1841*fae548d3Szrj 	os->set_free_space_fill(new Output_fill_debug_line());
1842*fae548d3Szrj     }
1843*fae548d3Szrj 
1844*fae548d3Szrj   // If we have already attached the sections to segments, then we
1845*fae548d3Szrj   // need to attach this one now.  This happens for sections created
1846*fae548d3Szrj   // directly by the linker.
1847*fae548d3Szrj   if (this->sections_are_attached_)
1848*fae548d3Szrj     this->attach_section_to_segment(&parameters->target(), os);
1849*fae548d3Szrj 
1850*fae548d3Szrj   return os;
1851*fae548d3Szrj }
1852*fae548d3Szrj 
1853*fae548d3Szrj // Return the default order in which a section should be placed in an
1854*fae548d3Szrj // output segment.  This function captures a lot of the ideas in
1855*fae548d3Szrj // ld/scripttempl/elf.sc in the GNU linker.  Note that the order of a
1856*fae548d3Szrj // linker created section is normally set when the section is created;
1857*fae548d3Szrj // this function is used for input sections.
1858*fae548d3Szrj 
1859*fae548d3Szrj Output_section_order
default_section_order(Output_section * os,bool is_relro_local)1860*fae548d3Szrj Layout::default_section_order(Output_section* os, bool is_relro_local)
1861*fae548d3Szrj {
1862*fae548d3Szrj   gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1863*fae548d3Szrj   bool is_write = (os->flags() & elfcpp::SHF_WRITE) != 0;
1864*fae548d3Szrj   bool is_execinstr = (os->flags() & elfcpp::SHF_EXECINSTR) != 0;
1865*fae548d3Szrj   bool is_bss = false;
1866*fae548d3Szrj 
1867*fae548d3Szrj   switch (os->type())
1868*fae548d3Szrj     {
1869*fae548d3Szrj     default:
1870*fae548d3Szrj     case elfcpp::SHT_PROGBITS:
1871*fae548d3Szrj       break;
1872*fae548d3Szrj     case elfcpp::SHT_NOBITS:
1873*fae548d3Szrj       is_bss = true;
1874*fae548d3Szrj       break;
1875*fae548d3Szrj     case elfcpp::SHT_RELA:
1876*fae548d3Szrj     case elfcpp::SHT_REL:
1877*fae548d3Szrj       if (!is_write)
1878*fae548d3Szrj 	return ORDER_DYNAMIC_RELOCS;
1879*fae548d3Szrj       break;
1880*fae548d3Szrj     case elfcpp::SHT_HASH:
1881*fae548d3Szrj     case elfcpp::SHT_DYNAMIC:
1882*fae548d3Szrj     case elfcpp::SHT_SHLIB:
1883*fae548d3Szrj     case elfcpp::SHT_DYNSYM:
1884*fae548d3Szrj     case elfcpp::SHT_GNU_HASH:
1885*fae548d3Szrj     case elfcpp::SHT_GNU_verdef:
1886*fae548d3Szrj     case elfcpp::SHT_GNU_verneed:
1887*fae548d3Szrj     case elfcpp::SHT_GNU_versym:
1888*fae548d3Szrj       if (!is_write)
1889*fae548d3Szrj 	return ORDER_DYNAMIC_LINKER;
1890*fae548d3Szrj       break;
1891*fae548d3Szrj     case elfcpp::SHT_NOTE:
1892*fae548d3Szrj       return is_write ? ORDER_RW_NOTE : ORDER_RO_NOTE;
1893*fae548d3Szrj     }
1894*fae548d3Szrj 
1895*fae548d3Szrj   if ((os->flags() & elfcpp::SHF_TLS) != 0)
1896*fae548d3Szrj     return is_bss ? ORDER_TLS_BSS : ORDER_TLS_DATA;
1897*fae548d3Szrj 
1898*fae548d3Szrj   if (!is_bss && !is_write)
1899*fae548d3Szrj     {
1900*fae548d3Szrj       if (is_execinstr)
1901*fae548d3Szrj 	{
1902*fae548d3Szrj 	  if (strcmp(os->name(), ".init") == 0)
1903*fae548d3Szrj 	    return ORDER_INIT;
1904*fae548d3Szrj 	  else if (strcmp(os->name(), ".fini") == 0)
1905*fae548d3Szrj 	    return ORDER_FINI;
1906*fae548d3Szrj 	  else if (parameters->options().keep_text_section_prefix())
1907*fae548d3Szrj 	    {
1908*fae548d3Szrj 	      // -z,keep-text-section-prefix introduces additional
1909*fae548d3Szrj 	      // output sections.
1910*fae548d3Szrj 	      if (strcmp(os->name(), ".text.hot") == 0)
1911*fae548d3Szrj 		return ORDER_TEXT_HOT;
1912*fae548d3Szrj 	      else if (strcmp(os->name(), ".text.startup") == 0)
1913*fae548d3Szrj 		return ORDER_TEXT_STARTUP;
1914*fae548d3Szrj 	      else if (strcmp(os->name(), ".text.exit") == 0)
1915*fae548d3Szrj 		return ORDER_TEXT_EXIT;
1916*fae548d3Szrj 	      else if (strcmp(os->name(), ".text.unlikely") == 0)
1917*fae548d3Szrj 		return ORDER_TEXT_UNLIKELY;
1918*fae548d3Szrj 	    }
1919*fae548d3Szrj 	}
1920*fae548d3Szrj       return is_execinstr ? ORDER_TEXT : ORDER_READONLY;
1921*fae548d3Szrj     }
1922*fae548d3Szrj 
1923*fae548d3Szrj   if (os->is_relro())
1924*fae548d3Szrj     return is_relro_local ? ORDER_RELRO_LOCAL : ORDER_RELRO;
1925*fae548d3Szrj 
1926*fae548d3Szrj   if (os->is_small_section())
1927*fae548d3Szrj     return is_bss ? ORDER_SMALL_BSS : ORDER_SMALL_DATA;
1928*fae548d3Szrj   if (os->is_large_section())
1929*fae548d3Szrj     return is_bss ? ORDER_LARGE_BSS : ORDER_LARGE_DATA;
1930*fae548d3Szrj 
1931*fae548d3Szrj   return is_bss ? ORDER_BSS : ORDER_DATA;
1932*fae548d3Szrj }
1933*fae548d3Szrj 
1934*fae548d3Szrj // Attach output sections to segments.  This is called after we have
1935*fae548d3Szrj // seen all the input sections.
1936*fae548d3Szrj 
1937*fae548d3Szrj void
attach_sections_to_segments(const Target * target)1938*fae548d3Szrj Layout::attach_sections_to_segments(const Target* target)
1939*fae548d3Szrj {
1940*fae548d3Szrj   for (Section_list::iterator p = this->section_list_.begin();
1941*fae548d3Szrj        p != this->section_list_.end();
1942*fae548d3Szrj        ++p)
1943*fae548d3Szrj     this->attach_section_to_segment(target, *p);
1944*fae548d3Szrj 
1945*fae548d3Szrj   this->sections_are_attached_ = true;
1946*fae548d3Szrj }
1947*fae548d3Szrj 
1948*fae548d3Szrj // Attach an output section to a segment.
1949*fae548d3Szrj 
1950*fae548d3Szrj void
attach_section_to_segment(const Target * target,Output_section * os)1951*fae548d3Szrj Layout::attach_section_to_segment(const Target* target, Output_section* os)
1952*fae548d3Szrj {
1953*fae548d3Szrj   if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
1954*fae548d3Szrj     this->unattached_section_list_.push_back(os);
1955*fae548d3Szrj   else
1956*fae548d3Szrj     this->attach_allocated_section_to_segment(target, os);
1957*fae548d3Szrj }
1958*fae548d3Szrj 
1959*fae548d3Szrj // Attach an allocated output section to a segment.
1960*fae548d3Szrj 
1961*fae548d3Szrj void
attach_allocated_section_to_segment(const Target * target,Output_section * os)1962*fae548d3Szrj Layout::attach_allocated_section_to_segment(const Target* target,
1963*fae548d3Szrj 					    Output_section* os)
1964*fae548d3Szrj {
1965*fae548d3Szrj   elfcpp::Elf_Xword flags = os->flags();
1966*fae548d3Szrj   gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
1967*fae548d3Szrj 
1968*fae548d3Szrj   if (parameters->options().relocatable())
1969*fae548d3Szrj     return;
1970*fae548d3Szrj 
1971*fae548d3Szrj   // If we have a SECTIONS clause, we can't handle the attachment to
1972*fae548d3Szrj   // segments until after we've seen all the sections.
1973*fae548d3Szrj   if (this->script_options_->saw_sections_clause())
1974*fae548d3Szrj     return;
1975*fae548d3Szrj 
1976*fae548d3Szrj   gold_assert(!this->script_options_->saw_phdrs_clause());
1977*fae548d3Szrj 
1978*fae548d3Szrj   // This output section goes into a PT_LOAD segment.
1979*fae548d3Szrj 
1980*fae548d3Szrj   elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
1981*fae548d3Szrj 
1982*fae548d3Szrj   // If this output section's segment has extra flags that need to be set,
1983*fae548d3Szrj   // coming from a linker plugin, do that.
1984*fae548d3Szrj   seg_flags |= os->extra_segment_flags();
1985*fae548d3Szrj 
1986*fae548d3Szrj   // Check for --section-start.
1987*fae548d3Szrj   uint64_t addr;
1988*fae548d3Szrj   bool is_address_set = parameters->options().section_start(os->name(), &addr);
1989*fae548d3Szrj 
1990*fae548d3Szrj   // In general the only thing we really care about for PT_LOAD
1991*fae548d3Szrj   // segments is whether or not they are writable or executable,
1992*fae548d3Szrj   // so that is how we search for them.
1993*fae548d3Szrj   // Large data sections also go into their own PT_LOAD segment.
1994*fae548d3Szrj   // People who need segments sorted on some other basis will
1995*fae548d3Szrj   // have to use a linker script.
1996*fae548d3Szrj 
1997*fae548d3Szrj   Segment_list::const_iterator p;
1998*fae548d3Szrj   if (!os->is_unique_segment())
1999*fae548d3Szrj     {
2000*fae548d3Szrj       for (p = this->segment_list_.begin();
2001*fae548d3Szrj 	   p != this->segment_list_.end();
2002*fae548d3Szrj 	   ++p)
2003*fae548d3Szrj 	{
2004*fae548d3Szrj 	  if ((*p)->type() != elfcpp::PT_LOAD)
2005*fae548d3Szrj 	    continue;
2006*fae548d3Szrj 	  if ((*p)->is_unique_segment())
2007*fae548d3Szrj 	    continue;
2008*fae548d3Szrj 	  if (!parameters->options().omagic()
2009*fae548d3Szrj 	      && ((*p)->flags() & elfcpp::PF_W) != (seg_flags & elfcpp::PF_W))
2010*fae548d3Szrj 	    continue;
2011*fae548d3Szrj 	  if ((target->isolate_execinstr() || parameters->options().rosegment())
2012*fae548d3Szrj 	      && ((*p)->flags() & elfcpp::PF_X) != (seg_flags & elfcpp::PF_X))
2013*fae548d3Szrj 	    continue;
2014*fae548d3Szrj 	  // If -Tbss was specified, we need to separate the data and BSS
2015*fae548d3Szrj 	  // segments.
2016*fae548d3Szrj 	  if (parameters->options().user_set_Tbss())
2017*fae548d3Szrj 	    {
2018*fae548d3Szrj 	      if ((os->type() == elfcpp::SHT_NOBITS)
2019*fae548d3Szrj 		  == (*p)->has_any_data_sections())
2020*fae548d3Szrj 		continue;
2021*fae548d3Szrj 	    }
2022*fae548d3Szrj 	  if (os->is_large_data_section() && !(*p)->is_large_data_segment())
2023*fae548d3Szrj 	    continue;
2024*fae548d3Szrj 
2025*fae548d3Szrj 	  if (is_address_set)
2026*fae548d3Szrj 	    {
2027*fae548d3Szrj 	      if ((*p)->are_addresses_set())
2028*fae548d3Szrj 		continue;
2029*fae548d3Szrj 
2030*fae548d3Szrj 	      (*p)->add_initial_output_data(os);
2031*fae548d3Szrj 	      (*p)->update_flags_for_output_section(seg_flags);
2032*fae548d3Szrj 	      (*p)->set_addresses(addr, addr);
2033*fae548d3Szrj 	      break;
2034*fae548d3Szrj 	    }
2035*fae548d3Szrj 
2036*fae548d3Szrj 	  (*p)->add_output_section_to_load(this, os, seg_flags);
2037*fae548d3Szrj 	  break;
2038*fae548d3Szrj 	}
2039*fae548d3Szrj     }
2040*fae548d3Szrj 
2041*fae548d3Szrj   if (p == this->segment_list_.end()
2042*fae548d3Szrj       || os->is_unique_segment())
2043*fae548d3Szrj     {
2044*fae548d3Szrj       Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
2045*fae548d3Szrj 						       seg_flags);
2046*fae548d3Szrj       if (os->is_large_data_section())
2047*fae548d3Szrj 	oseg->set_is_large_data_segment();
2048*fae548d3Szrj       oseg->add_output_section_to_load(this, os, seg_flags);
2049*fae548d3Szrj       if (is_address_set)
2050*fae548d3Szrj 	oseg->set_addresses(addr, addr);
2051*fae548d3Szrj       // Check if segment should be marked unique.  For segments marked
2052*fae548d3Szrj       // unique by linker plugins, set the new alignment if specified.
2053*fae548d3Szrj       if (os->is_unique_segment())
2054*fae548d3Szrj 	{
2055*fae548d3Szrj 	  oseg->set_is_unique_segment();
2056*fae548d3Szrj 	  if (os->segment_alignment() != 0)
2057*fae548d3Szrj 	    oseg->set_minimum_p_align(os->segment_alignment());
2058*fae548d3Szrj 	}
2059*fae548d3Szrj     }
2060*fae548d3Szrj 
2061*fae548d3Szrj   // If we see a loadable SHT_NOTE section, we create a PT_NOTE
2062*fae548d3Szrj   // segment.
2063*fae548d3Szrj   if (os->type() == elfcpp::SHT_NOTE)
2064*fae548d3Szrj     {
2065*fae548d3Szrj       // See if we already have an equivalent PT_NOTE segment.
2066*fae548d3Szrj       for (p = this->segment_list_.begin();
2067*fae548d3Szrj 	   p != segment_list_.end();
2068*fae548d3Szrj 	   ++p)
2069*fae548d3Szrj 	{
2070*fae548d3Szrj 	  if ((*p)->type() == elfcpp::PT_NOTE
2071*fae548d3Szrj 	      && (((*p)->flags() & elfcpp::PF_W)
2072*fae548d3Szrj 		  == (seg_flags & elfcpp::PF_W)))
2073*fae548d3Szrj 	    {
2074*fae548d3Szrj 	      (*p)->add_output_section_to_nonload(os, seg_flags);
2075*fae548d3Szrj 	      break;
2076*fae548d3Szrj 	    }
2077*fae548d3Szrj 	}
2078*fae548d3Szrj 
2079*fae548d3Szrj       if (p == this->segment_list_.end())
2080*fae548d3Szrj 	{
2081*fae548d3Szrj 	  Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
2082*fae548d3Szrj 							   seg_flags);
2083*fae548d3Szrj 	  oseg->add_output_section_to_nonload(os, seg_flags);
2084*fae548d3Szrj 	}
2085*fae548d3Szrj     }
2086*fae548d3Szrj 
2087*fae548d3Szrj   // If we see a loadable SHF_TLS section, we create a PT_TLS
2088*fae548d3Szrj   // segment.  There can only be one such segment.
2089*fae548d3Szrj   if ((flags & elfcpp::SHF_TLS) != 0)
2090*fae548d3Szrj     {
2091*fae548d3Szrj       if (this->tls_segment_ == NULL)
2092*fae548d3Szrj 	this->make_output_segment(elfcpp::PT_TLS, seg_flags);
2093*fae548d3Szrj       this->tls_segment_->add_output_section_to_nonload(os, seg_flags);
2094*fae548d3Szrj     }
2095*fae548d3Szrj 
2096*fae548d3Szrj   // If -z relro is in effect, and we see a relro section, we create a
2097*fae548d3Szrj   // PT_GNU_RELRO segment.  There can only be one such segment.
2098*fae548d3Szrj   if (os->is_relro() && parameters->options().relro())
2099*fae548d3Szrj     {
2100*fae548d3Szrj       gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W));
2101*fae548d3Szrj       if (this->relro_segment_ == NULL)
2102*fae548d3Szrj 	this->make_output_segment(elfcpp::PT_GNU_RELRO, seg_flags);
2103*fae548d3Szrj       this->relro_segment_->add_output_section_to_nonload(os, seg_flags);
2104*fae548d3Szrj     }
2105*fae548d3Szrj 
2106*fae548d3Szrj   // If we see a section named .interp, put it into a PT_INTERP
2107*fae548d3Szrj   // segment.  This seems broken to me, but this is what GNU ld does,
2108*fae548d3Szrj   // and glibc expects it.
2109*fae548d3Szrj   if (strcmp(os->name(), ".interp") == 0
2110*fae548d3Szrj       && !this->script_options_->saw_phdrs_clause())
2111*fae548d3Szrj     {
2112*fae548d3Szrj       if (this->interp_segment_ == NULL)
2113*fae548d3Szrj 	this->make_output_segment(elfcpp::PT_INTERP, seg_flags);
2114*fae548d3Szrj       else
2115*fae548d3Szrj 	gold_warning(_("multiple '.interp' sections in input files "
2116*fae548d3Szrj 		       "may cause confusing PT_INTERP segment"));
2117*fae548d3Szrj       this->interp_segment_->add_output_section_to_nonload(os, seg_flags);
2118*fae548d3Szrj     }
2119*fae548d3Szrj }
2120*fae548d3Szrj 
2121*fae548d3Szrj // Make an output section for a script.
2122*fae548d3Szrj 
2123*fae548d3Szrj Output_section*
make_output_section_for_script(const char * name,Script_sections::Section_type section_type)2124*fae548d3Szrj Layout::make_output_section_for_script(
2125*fae548d3Szrj     const char* name,
2126*fae548d3Szrj     Script_sections::Section_type section_type)
2127*fae548d3Szrj {
2128*fae548d3Szrj   name = this->namepool_.add(name, false, NULL);
2129*fae548d3Szrj   elfcpp::Elf_Xword sh_flags = elfcpp::SHF_ALLOC;
2130*fae548d3Szrj   if (section_type == Script_sections::ST_NOLOAD)
2131*fae548d3Szrj     sh_flags = 0;
2132*fae548d3Szrj   Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
2133*fae548d3Szrj 						 sh_flags, ORDER_INVALID,
2134*fae548d3Szrj 						 false);
2135*fae548d3Szrj   os->set_found_in_sections_clause();
2136*fae548d3Szrj   if (section_type == Script_sections::ST_NOLOAD)
2137*fae548d3Szrj     os->set_is_noload();
2138*fae548d3Szrj   return os;
2139*fae548d3Szrj }
2140*fae548d3Szrj 
2141*fae548d3Szrj // Return the number of segments we expect to see.
2142*fae548d3Szrj 
2143*fae548d3Szrj size_t
expected_segment_count() const2144*fae548d3Szrj Layout::expected_segment_count() const
2145*fae548d3Szrj {
2146*fae548d3Szrj   size_t ret = this->segment_list_.size();
2147*fae548d3Szrj 
2148*fae548d3Szrj   // If we didn't see a SECTIONS clause in a linker script, we should
2149*fae548d3Szrj   // already have the complete list of segments.  Otherwise we ask the
2150*fae548d3Szrj   // SECTIONS clause how many segments it expects, and add in the ones
2151*fae548d3Szrj   // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
2152*fae548d3Szrj 
2153*fae548d3Szrj   if (!this->script_options_->saw_sections_clause())
2154*fae548d3Szrj     return ret;
2155*fae548d3Szrj   else
2156*fae548d3Szrj     {
2157*fae548d3Szrj       const Script_sections* ss = this->script_options_->script_sections();
2158*fae548d3Szrj       return ret + ss->expected_segment_count(this);
2159*fae548d3Szrj     }
2160*fae548d3Szrj }
2161*fae548d3Szrj 
2162*fae548d3Szrj // Handle the .note.GNU-stack section at layout time.  SEEN_GNU_STACK
2163*fae548d3Szrj // is whether we saw a .note.GNU-stack section in the object file.
2164*fae548d3Szrj // GNU_STACK_FLAGS is the section flags.  The flags give the
2165*fae548d3Szrj // protection required for stack memory.  We record this in an
2166*fae548d3Szrj // executable as a PT_GNU_STACK segment.  If an object file does not
2167*fae548d3Szrj // have a .note.GNU-stack segment, we must assume that it is an old
2168*fae548d3Szrj // object.  On some targets that will force an executable stack.
2169*fae548d3Szrj 
2170*fae548d3Szrj void
layout_gnu_stack(bool seen_gnu_stack,uint64_t gnu_stack_flags,const Object * obj)2171*fae548d3Szrj Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
2172*fae548d3Szrj 			 const Object* obj)
2173*fae548d3Szrj {
2174*fae548d3Szrj   if (!seen_gnu_stack)
2175*fae548d3Szrj     {
2176*fae548d3Szrj       this->input_without_gnu_stack_note_ = true;
2177*fae548d3Szrj       if (parameters->options().warn_execstack()
2178*fae548d3Szrj 	  && parameters->target().is_default_stack_executable())
2179*fae548d3Szrj 	gold_warning(_("%s: missing .note.GNU-stack section"
2180*fae548d3Szrj 		       " implies executable stack"),
2181*fae548d3Szrj 		     obj->name().c_str());
2182*fae548d3Szrj     }
2183*fae548d3Szrj   else
2184*fae548d3Szrj     {
2185*fae548d3Szrj       this->input_with_gnu_stack_note_ = true;
2186*fae548d3Szrj       if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
2187*fae548d3Szrj 	{
2188*fae548d3Szrj 	  this->input_requires_executable_stack_ = true;
2189*fae548d3Szrj 	  if (parameters->options().warn_execstack())
2190*fae548d3Szrj 	    gold_warning(_("%s: requires executable stack"),
2191*fae548d3Szrj 			 obj->name().c_str());
2192*fae548d3Szrj 	}
2193*fae548d3Szrj     }
2194*fae548d3Szrj }
2195*fae548d3Szrj 
2196*fae548d3Szrj // Read a value with given size and endianness.
2197*fae548d3Szrj 
2198*fae548d3Szrj static inline uint64_t
read_sized_value(size_t size,const unsigned char * buf,bool is_big_endian,const Object * object)2199*fae548d3Szrj read_sized_value(size_t size, const unsigned char* buf, bool is_big_endian,
2200*fae548d3Szrj 		 const Object* object)
2201*fae548d3Szrj {
2202*fae548d3Szrj   uint64_t val = 0;
2203*fae548d3Szrj   if (size == 4)
2204*fae548d3Szrj     {
2205*fae548d3Szrj       if (is_big_endian)
2206*fae548d3Szrj 	val = elfcpp::Swap<32, true>::readval(buf);
2207*fae548d3Szrj       else
2208*fae548d3Szrj 	val = elfcpp::Swap<32, false>::readval(buf);
2209*fae548d3Szrj     }
2210*fae548d3Szrj   else if (size == 8)
2211*fae548d3Szrj     {
2212*fae548d3Szrj       if (is_big_endian)
2213*fae548d3Szrj 	val = elfcpp::Swap<64, true>::readval(buf);
2214*fae548d3Szrj       else
2215*fae548d3Szrj 	val = elfcpp::Swap<64, false>::readval(buf);
2216*fae548d3Szrj     }
2217*fae548d3Szrj   else
2218*fae548d3Szrj     {
2219*fae548d3Szrj       gold_warning(_("%s: in .note.gnu.property section, "
2220*fae548d3Szrj 		     "pr_datasz must be 4 or 8"),
2221*fae548d3Szrj 		   object->name().c_str());
2222*fae548d3Szrj     }
2223*fae548d3Szrj   return val;
2224*fae548d3Szrj }
2225*fae548d3Szrj 
2226*fae548d3Szrj // Write a value with given size and endianness.
2227*fae548d3Szrj 
2228*fae548d3Szrj static inline void
write_sized_value(uint64_t value,size_t size,unsigned char * buf,bool is_big_endian)2229*fae548d3Szrj write_sized_value(uint64_t value, size_t size, unsigned char* buf,
2230*fae548d3Szrj 		  bool is_big_endian)
2231*fae548d3Szrj {
2232*fae548d3Szrj   if (size == 4)
2233*fae548d3Szrj     {
2234*fae548d3Szrj       if (is_big_endian)
2235*fae548d3Szrj 	elfcpp::Swap<32, true>::writeval(buf, static_cast<uint32_t>(value));
2236*fae548d3Szrj       else
2237*fae548d3Szrj 	elfcpp::Swap<32, false>::writeval(buf, static_cast<uint32_t>(value));
2238*fae548d3Szrj     }
2239*fae548d3Szrj   else if (size == 8)
2240*fae548d3Szrj     {
2241*fae548d3Szrj       if (is_big_endian)
2242*fae548d3Szrj 	elfcpp::Swap<64, true>::writeval(buf, value);
2243*fae548d3Szrj       else
2244*fae548d3Szrj 	elfcpp::Swap<64, false>::writeval(buf, value);
2245*fae548d3Szrj     }
2246*fae548d3Szrj   else
2247*fae548d3Szrj     {
2248*fae548d3Szrj       // We will have already complained about this.
2249*fae548d3Szrj     }
2250*fae548d3Szrj }
2251*fae548d3Szrj 
2252*fae548d3Szrj // Handle the .note.gnu.property section at layout time.
2253*fae548d3Szrj 
2254*fae548d3Szrj void
layout_gnu_property(unsigned int note_type,unsigned int pr_type,size_t pr_datasz,const unsigned char * pr_data,const Object * object)2255*fae548d3Szrj Layout::layout_gnu_property(unsigned int note_type,
2256*fae548d3Szrj 			    unsigned int pr_type,
2257*fae548d3Szrj 			    size_t pr_datasz,
2258*fae548d3Szrj 			    const unsigned char* pr_data,
2259*fae548d3Szrj 			    const Object* object)
2260*fae548d3Szrj {
2261*fae548d3Szrj   // We currently support only the one note type.
2262*fae548d3Szrj   gold_assert(note_type == elfcpp::NT_GNU_PROPERTY_TYPE_0);
2263*fae548d3Szrj 
2264*fae548d3Szrj   if (pr_type >= elfcpp::GNU_PROPERTY_LOPROC
2265*fae548d3Szrj       && pr_type < elfcpp::GNU_PROPERTY_HIPROC)
2266*fae548d3Szrj     {
2267*fae548d3Szrj       // Target-dependent property value; call the target to record.
2268*fae548d3Szrj       const int size = parameters->target().get_size();
2269*fae548d3Szrj       const bool is_big_endian = parameters->target().is_big_endian();
2270*fae548d3Szrj       if (size == 32)
2271*fae548d3Szrj         {
2272*fae548d3Szrj           if (is_big_endian)
2273*fae548d3Szrj             {
2274*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
2275*fae548d3Szrj 	      parameters->sized_target<32, true>()->
2276*fae548d3Szrj 		  record_gnu_property(note_type, pr_type, pr_datasz, pr_data,
2277*fae548d3Szrj 				      object);
2278*fae548d3Szrj #else
2279*fae548d3Szrj 	      gold_unreachable();
2280*fae548d3Szrj #endif
2281*fae548d3Szrj             }
2282*fae548d3Szrj           else
2283*fae548d3Szrj             {
2284*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
2285*fae548d3Szrj 	      parameters->sized_target<32, false>()->
2286*fae548d3Szrj 		  record_gnu_property(note_type, pr_type, pr_datasz, pr_data,
2287*fae548d3Szrj 				      object);
2288*fae548d3Szrj #else
2289*fae548d3Szrj 	      gold_unreachable();
2290*fae548d3Szrj #endif
2291*fae548d3Szrj             }
2292*fae548d3Szrj         }
2293*fae548d3Szrj       else if (size == 64)
2294*fae548d3Szrj         {
2295*fae548d3Szrj           if (is_big_endian)
2296*fae548d3Szrj             {
2297*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
2298*fae548d3Szrj 	      parameters->sized_target<64, true>()->
2299*fae548d3Szrj 		  record_gnu_property(note_type, pr_type, pr_datasz, pr_data,
2300*fae548d3Szrj 				      object);
2301*fae548d3Szrj #else
2302*fae548d3Szrj 	      gold_unreachable();
2303*fae548d3Szrj #endif
2304*fae548d3Szrj             }
2305*fae548d3Szrj           else
2306*fae548d3Szrj             {
2307*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
2308*fae548d3Szrj 	      parameters->sized_target<64, false>()->
2309*fae548d3Szrj 		  record_gnu_property(note_type, pr_type, pr_datasz, pr_data,
2310*fae548d3Szrj 				      object);
2311*fae548d3Szrj #else
2312*fae548d3Szrj 	      gold_unreachable();
2313*fae548d3Szrj #endif
2314*fae548d3Szrj             }
2315*fae548d3Szrj         }
2316*fae548d3Szrj       else
2317*fae548d3Szrj         gold_unreachable();
2318*fae548d3Szrj       return;
2319*fae548d3Szrj     }
2320*fae548d3Szrj 
2321*fae548d3Szrj   Gnu_properties::iterator pprop = this->gnu_properties_.find(pr_type);
2322*fae548d3Szrj   if (pprop == this->gnu_properties_.end())
2323*fae548d3Szrj     {
2324*fae548d3Szrj       Gnu_property prop;
2325*fae548d3Szrj       prop.pr_datasz = pr_datasz;
2326*fae548d3Szrj       prop.pr_data = new unsigned char[pr_datasz];
2327*fae548d3Szrj       memcpy(prop.pr_data, pr_data, pr_datasz);
2328*fae548d3Szrj       this->gnu_properties_[pr_type] = prop;
2329*fae548d3Szrj     }
2330*fae548d3Szrj   else
2331*fae548d3Szrj     {
2332*fae548d3Szrj       const bool is_big_endian = parameters->target().is_big_endian();
2333*fae548d3Szrj       switch (pr_type)
2334*fae548d3Szrj 	{
2335*fae548d3Szrj 	case elfcpp::GNU_PROPERTY_STACK_SIZE:
2336*fae548d3Szrj 	  // Record the maximum value seen.
2337*fae548d3Szrj 	  {
2338*fae548d3Szrj 	    uint64_t val1 = read_sized_value(pprop->second.pr_datasz,
2339*fae548d3Szrj 					     pprop->second.pr_data,
2340*fae548d3Szrj 					     is_big_endian, object);
2341*fae548d3Szrj 	    uint64_t val2 = read_sized_value(pr_datasz, pr_data,
2342*fae548d3Szrj 					     is_big_endian, object);
2343*fae548d3Szrj 	    if (val2 > val1)
2344*fae548d3Szrj 	      write_sized_value(val2, pprop->second.pr_datasz,
2345*fae548d3Szrj 				pprop->second.pr_data, is_big_endian);
2346*fae548d3Szrj 	  }
2347*fae548d3Szrj 	  break;
2348*fae548d3Szrj 	case elfcpp::GNU_PROPERTY_NO_COPY_ON_PROTECTED:
2349*fae548d3Szrj 	  // No data to merge.
2350*fae548d3Szrj 	  break;
2351*fae548d3Szrj 	default:
2352*fae548d3Szrj 	  gold_warning(_("%s: unknown program property type %d "
2353*fae548d3Szrj 			 "in .note.gnu.property section"),
2354*fae548d3Szrj 		       object->name().c_str(), pr_type);
2355*fae548d3Szrj 	}
2356*fae548d3Szrj     }
2357*fae548d3Szrj }
2358*fae548d3Szrj 
2359*fae548d3Szrj // Merge per-object properties with program properties.
2360*fae548d3Szrj // This lets the target identify objects that are missing certain
2361*fae548d3Szrj // properties, in cases where properties must be ANDed together.
2362*fae548d3Szrj 
2363*fae548d3Szrj void
merge_gnu_properties(const Object * object)2364*fae548d3Szrj Layout::merge_gnu_properties(const Object* object)
2365*fae548d3Szrj {
2366*fae548d3Szrj   const int size = parameters->target().get_size();
2367*fae548d3Szrj   const bool is_big_endian = parameters->target().is_big_endian();
2368*fae548d3Szrj   if (size == 32)
2369*fae548d3Szrj     {
2370*fae548d3Szrj       if (is_big_endian)
2371*fae548d3Szrj 	{
2372*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
2373*fae548d3Szrj 	  parameters->sized_target<32, true>()->merge_gnu_properties(object);
2374*fae548d3Szrj #else
2375*fae548d3Szrj 	  gold_unreachable();
2376*fae548d3Szrj #endif
2377*fae548d3Szrj 	}
2378*fae548d3Szrj       else
2379*fae548d3Szrj 	{
2380*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
2381*fae548d3Szrj 	  parameters->sized_target<32, false>()->merge_gnu_properties(object);
2382*fae548d3Szrj #else
2383*fae548d3Szrj 	  gold_unreachable();
2384*fae548d3Szrj #endif
2385*fae548d3Szrj 	}
2386*fae548d3Szrj     }
2387*fae548d3Szrj   else if (size == 64)
2388*fae548d3Szrj     {
2389*fae548d3Szrj       if (is_big_endian)
2390*fae548d3Szrj 	{
2391*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
2392*fae548d3Szrj 	  parameters->sized_target<64, true>()->merge_gnu_properties(object);
2393*fae548d3Szrj #else
2394*fae548d3Szrj 	  gold_unreachable();
2395*fae548d3Szrj #endif
2396*fae548d3Szrj 	}
2397*fae548d3Szrj       else
2398*fae548d3Szrj 	{
2399*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
2400*fae548d3Szrj 	  parameters->sized_target<64, false>()->merge_gnu_properties(object);
2401*fae548d3Szrj #else
2402*fae548d3Szrj 	  gold_unreachable();
2403*fae548d3Szrj #endif
2404*fae548d3Szrj 	}
2405*fae548d3Szrj     }
2406*fae548d3Szrj   else
2407*fae548d3Szrj     gold_unreachable();
2408*fae548d3Szrj }
2409*fae548d3Szrj 
2410*fae548d3Szrj // Add a target-specific property for the output .note.gnu.property section.
2411*fae548d3Szrj 
2412*fae548d3Szrj void
add_gnu_property(unsigned int note_type,unsigned int pr_type,size_t pr_datasz,const unsigned char * pr_data)2413*fae548d3Szrj Layout::add_gnu_property(unsigned int note_type,
2414*fae548d3Szrj 			 unsigned int pr_type,
2415*fae548d3Szrj 			 size_t pr_datasz,
2416*fae548d3Szrj 			 const unsigned char* pr_data)
2417*fae548d3Szrj {
2418*fae548d3Szrj   gold_assert(note_type == elfcpp::NT_GNU_PROPERTY_TYPE_0);
2419*fae548d3Szrj 
2420*fae548d3Szrj   Gnu_property prop;
2421*fae548d3Szrj   prop.pr_datasz = pr_datasz;
2422*fae548d3Szrj   prop.pr_data = new unsigned char[pr_datasz];
2423*fae548d3Szrj   memcpy(prop.pr_data, pr_data, pr_datasz);
2424*fae548d3Szrj   this->gnu_properties_[pr_type] = prop;
2425*fae548d3Szrj }
2426*fae548d3Szrj 
2427*fae548d3Szrj // Create automatic note sections.
2428*fae548d3Szrj 
2429*fae548d3Szrj void
create_notes()2430*fae548d3Szrj Layout::create_notes()
2431*fae548d3Szrj {
2432*fae548d3Szrj   this->create_gnu_properties_note();
2433*fae548d3Szrj   this->create_gold_note();
2434*fae548d3Szrj   this->create_stack_segment();
2435*fae548d3Szrj   this->create_build_id();
2436*fae548d3Szrj }
2437*fae548d3Szrj 
2438*fae548d3Szrj // Create the dynamic sections which are needed before we read the
2439*fae548d3Szrj // relocs.
2440*fae548d3Szrj 
2441*fae548d3Szrj void
create_initial_dynamic_sections(Symbol_table * symtab)2442*fae548d3Szrj Layout::create_initial_dynamic_sections(Symbol_table* symtab)
2443*fae548d3Szrj {
2444*fae548d3Szrj   if (parameters->doing_static_link())
2445*fae548d3Szrj     return;
2446*fae548d3Szrj 
2447*fae548d3Szrj   this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
2448*fae548d3Szrj 						       elfcpp::SHT_DYNAMIC,
2449*fae548d3Szrj 						       (elfcpp::SHF_ALLOC
2450*fae548d3Szrj 							| elfcpp::SHF_WRITE),
2451*fae548d3Szrj 						       false, ORDER_RELRO,
2452*fae548d3Szrj 						       true, false, false);
2453*fae548d3Szrj 
2454*fae548d3Szrj   // A linker script may discard .dynamic, so check for NULL.
2455*fae548d3Szrj   if (this->dynamic_section_ != NULL)
2456*fae548d3Szrj     {
2457*fae548d3Szrj       this->dynamic_symbol_ =
2458*fae548d3Szrj 	symtab->define_in_output_data("_DYNAMIC", NULL,
2459*fae548d3Szrj 				      Symbol_table::PREDEFINED,
2460*fae548d3Szrj 				      this->dynamic_section_, 0, 0,
2461*fae548d3Szrj 				      elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
2462*fae548d3Szrj 				      elfcpp::STV_HIDDEN, 0, false, false);
2463*fae548d3Szrj 
2464*fae548d3Szrj       this->dynamic_data_ =  new Output_data_dynamic(&this->dynpool_);
2465*fae548d3Szrj 
2466*fae548d3Szrj       this->dynamic_section_->add_output_section_data(this->dynamic_data_);
2467*fae548d3Szrj     }
2468*fae548d3Szrj }
2469*fae548d3Szrj 
2470*fae548d3Szrj // For each output section whose name can be represented as C symbol,
2471*fae548d3Szrj // define __start and __stop symbols for the section.  This is a GNU
2472*fae548d3Szrj // extension.
2473*fae548d3Szrj 
2474*fae548d3Szrj void
define_section_symbols(Symbol_table * symtab)2475*fae548d3Szrj Layout::define_section_symbols(Symbol_table* symtab)
2476*fae548d3Szrj {
2477*fae548d3Szrj   for (Section_list::const_iterator p = this->section_list_.begin();
2478*fae548d3Szrj        p != this->section_list_.end();
2479*fae548d3Szrj        ++p)
2480*fae548d3Szrj     {
2481*fae548d3Szrj       const char* const name = (*p)->name();
2482*fae548d3Szrj       if (is_cident(name))
2483*fae548d3Szrj 	{
2484*fae548d3Szrj 	  const std::string name_string(name);
2485*fae548d3Szrj 	  const std::string start_name(cident_section_start_prefix
2486*fae548d3Szrj 				       + name_string);
2487*fae548d3Szrj 	  const std::string stop_name(cident_section_stop_prefix
2488*fae548d3Szrj 				      + name_string);
2489*fae548d3Szrj 
2490*fae548d3Szrj 	  symtab->define_in_output_data(start_name.c_str(),
2491*fae548d3Szrj 					NULL, // version
2492*fae548d3Szrj 					Symbol_table::PREDEFINED,
2493*fae548d3Szrj 					*p,
2494*fae548d3Szrj 					0, // value
2495*fae548d3Szrj 					0, // symsize
2496*fae548d3Szrj 					elfcpp::STT_NOTYPE,
2497*fae548d3Szrj 					elfcpp::STB_GLOBAL,
2498*fae548d3Szrj 					elfcpp::STV_PROTECTED,
2499*fae548d3Szrj 					0, // nonvis
2500*fae548d3Szrj 					false, // offset_is_from_end
2501*fae548d3Szrj 					true); // only_if_ref
2502*fae548d3Szrj 
2503*fae548d3Szrj 	  symtab->define_in_output_data(stop_name.c_str(),
2504*fae548d3Szrj 					NULL, // version
2505*fae548d3Szrj 					Symbol_table::PREDEFINED,
2506*fae548d3Szrj 					*p,
2507*fae548d3Szrj 					0, // value
2508*fae548d3Szrj 					0, // symsize
2509*fae548d3Szrj 					elfcpp::STT_NOTYPE,
2510*fae548d3Szrj 					elfcpp::STB_GLOBAL,
2511*fae548d3Szrj 					elfcpp::STV_PROTECTED,
2512*fae548d3Szrj 					0, // nonvis
2513*fae548d3Szrj 					true, // offset_is_from_end
2514*fae548d3Szrj 					true); // only_if_ref
2515*fae548d3Szrj 	}
2516*fae548d3Szrj     }
2517*fae548d3Szrj }
2518*fae548d3Szrj 
2519*fae548d3Szrj // Define symbols for group signatures.
2520*fae548d3Szrj 
2521*fae548d3Szrj void
define_group_signatures(Symbol_table * symtab)2522*fae548d3Szrj Layout::define_group_signatures(Symbol_table* symtab)
2523*fae548d3Szrj {
2524*fae548d3Szrj   for (Group_signatures::iterator p = this->group_signatures_.begin();
2525*fae548d3Szrj        p != this->group_signatures_.end();
2526*fae548d3Szrj        ++p)
2527*fae548d3Szrj     {
2528*fae548d3Szrj       Symbol* sym = symtab->lookup(p->signature, NULL);
2529*fae548d3Szrj       if (sym != NULL)
2530*fae548d3Szrj 	p->section->set_info_symndx(sym);
2531*fae548d3Szrj       else
2532*fae548d3Szrj 	{
2533*fae548d3Szrj 	  // Force the name of the group section to the group
2534*fae548d3Szrj 	  // signature, and use the group's section symbol as the
2535*fae548d3Szrj 	  // signature symbol.
2536*fae548d3Szrj 	  if (strcmp(p->section->name(), p->signature) != 0)
2537*fae548d3Szrj 	    {
2538*fae548d3Szrj 	      const char* name = this->namepool_.add(p->signature,
2539*fae548d3Szrj 						     true, NULL);
2540*fae548d3Szrj 	      p->section->set_name(name);
2541*fae548d3Szrj 	    }
2542*fae548d3Szrj 	  p->section->set_needs_symtab_index();
2543*fae548d3Szrj 	  p->section->set_info_section_symndx(p->section);
2544*fae548d3Szrj 	}
2545*fae548d3Szrj     }
2546*fae548d3Szrj 
2547*fae548d3Szrj   this->group_signatures_.clear();
2548*fae548d3Szrj }
2549*fae548d3Szrj 
2550*fae548d3Szrj // Find the first read-only PT_LOAD segment, creating one if
2551*fae548d3Szrj // necessary.
2552*fae548d3Szrj 
2553*fae548d3Szrj Output_segment*
find_first_load_seg(const Target * target)2554*fae548d3Szrj Layout::find_first_load_seg(const Target* target)
2555*fae548d3Szrj {
2556*fae548d3Szrj   Output_segment* best = NULL;
2557*fae548d3Szrj   for (Segment_list::const_iterator p = this->segment_list_.begin();
2558*fae548d3Szrj        p != this->segment_list_.end();
2559*fae548d3Szrj        ++p)
2560*fae548d3Szrj     {
2561*fae548d3Szrj       if ((*p)->type() == elfcpp::PT_LOAD
2562*fae548d3Szrj 	  && ((*p)->flags() & elfcpp::PF_R) != 0
2563*fae548d3Szrj 	  && (parameters->options().omagic()
2564*fae548d3Szrj 	      || ((*p)->flags() & elfcpp::PF_W) == 0)
2565*fae548d3Szrj 	  && (!target->isolate_execinstr()
2566*fae548d3Szrj 	      || ((*p)->flags() & elfcpp::PF_X) == 0))
2567*fae548d3Szrj 	{
2568*fae548d3Szrj 	  if (best == NULL || this->segment_precedes(*p, best))
2569*fae548d3Szrj 	    best = *p;
2570*fae548d3Szrj 	}
2571*fae548d3Szrj     }
2572*fae548d3Szrj   if (best != NULL)
2573*fae548d3Szrj     return best;
2574*fae548d3Szrj 
2575*fae548d3Szrj   gold_assert(!this->script_options_->saw_phdrs_clause());
2576*fae548d3Szrj 
2577*fae548d3Szrj   Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
2578*fae548d3Szrj 						       elfcpp::PF_R);
2579*fae548d3Szrj   return load_seg;
2580*fae548d3Szrj }
2581*fae548d3Szrj 
2582*fae548d3Szrj // Save states of all current output segments.  Store saved states
2583*fae548d3Szrj // in SEGMENT_STATES.
2584*fae548d3Szrj 
2585*fae548d3Szrj void
save_segments(Segment_states * segment_states)2586*fae548d3Szrj Layout::save_segments(Segment_states* segment_states)
2587*fae548d3Szrj {
2588*fae548d3Szrj   for (Segment_list::const_iterator p = this->segment_list_.begin();
2589*fae548d3Szrj        p != this->segment_list_.end();
2590*fae548d3Szrj        ++p)
2591*fae548d3Szrj     {
2592*fae548d3Szrj       Output_segment* segment = *p;
2593*fae548d3Szrj       // Shallow copy.
2594*fae548d3Szrj       Output_segment* copy = new Output_segment(*segment);
2595*fae548d3Szrj       (*segment_states)[segment] = copy;
2596*fae548d3Szrj     }
2597*fae548d3Szrj }
2598*fae548d3Szrj 
2599*fae548d3Szrj // Restore states of output segments and delete any segment not found in
2600*fae548d3Szrj // SEGMENT_STATES.
2601*fae548d3Szrj 
2602*fae548d3Szrj void
restore_segments(const Segment_states * segment_states)2603*fae548d3Szrj Layout::restore_segments(const Segment_states* segment_states)
2604*fae548d3Szrj {
2605*fae548d3Szrj   // Go through the segment list and remove any segment added in the
2606*fae548d3Szrj   // relaxation loop.
2607*fae548d3Szrj   this->tls_segment_ = NULL;
2608*fae548d3Szrj   this->relro_segment_ = NULL;
2609*fae548d3Szrj   Segment_list::iterator list_iter = this->segment_list_.begin();
2610*fae548d3Szrj   while (list_iter != this->segment_list_.end())
2611*fae548d3Szrj     {
2612*fae548d3Szrj       Output_segment* segment = *list_iter;
2613*fae548d3Szrj       Segment_states::const_iterator states_iter =
2614*fae548d3Szrj 	  segment_states->find(segment);
2615*fae548d3Szrj       if (states_iter != segment_states->end())
2616*fae548d3Szrj 	{
2617*fae548d3Szrj 	  const Output_segment* copy = states_iter->second;
2618*fae548d3Szrj 	  // Shallow copy to restore states.
2619*fae548d3Szrj 	  *segment = *copy;
2620*fae548d3Szrj 
2621*fae548d3Szrj 	  // Also fix up TLS and RELRO segment pointers as appropriate.
2622*fae548d3Szrj 	  if (segment->type() == elfcpp::PT_TLS)
2623*fae548d3Szrj 	    this->tls_segment_ = segment;
2624*fae548d3Szrj 	  else if (segment->type() == elfcpp::PT_GNU_RELRO)
2625*fae548d3Szrj 	    this->relro_segment_ = segment;
2626*fae548d3Szrj 
2627*fae548d3Szrj 	  ++list_iter;
2628*fae548d3Szrj 	}
2629*fae548d3Szrj       else
2630*fae548d3Szrj 	{
2631*fae548d3Szrj 	  list_iter = this->segment_list_.erase(list_iter);
2632*fae548d3Szrj 	  // This is a segment created during section layout.  It should be
2633*fae548d3Szrj 	  // safe to remove it since we should have removed all pointers to it.
2634*fae548d3Szrj 	  delete segment;
2635*fae548d3Szrj 	}
2636*fae548d3Szrj     }
2637*fae548d3Szrj }
2638*fae548d3Szrj 
2639*fae548d3Szrj // Clean up after relaxation so that sections can be laid out again.
2640*fae548d3Szrj 
2641*fae548d3Szrj void
clean_up_after_relaxation()2642*fae548d3Szrj Layout::clean_up_after_relaxation()
2643*fae548d3Szrj {
2644*fae548d3Szrj   // Restore the segments to point state just prior to the relaxation loop.
2645*fae548d3Szrj   Script_sections* script_section = this->script_options_->script_sections();
2646*fae548d3Szrj   script_section->release_segments();
2647*fae548d3Szrj   this->restore_segments(this->segment_states_);
2648*fae548d3Szrj 
2649*fae548d3Szrj   // Reset section addresses and file offsets
2650*fae548d3Szrj   for (Section_list::iterator p = this->section_list_.begin();
2651*fae548d3Szrj        p != this->section_list_.end();
2652*fae548d3Szrj        ++p)
2653*fae548d3Szrj     {
2654*fae548d3Szrj       (*p)->restore_states();
2655*fae548d3Szrj 
2656*fae548d3Szrj       // If an input section changes size because of relaxation,
2657*fae548d3Szrj       // we need to adjust the section offsets of all input sections.
2658*fae548d3Szrj       // after such a section.
2659*fae548d3Szrj       if ((*p)->section_offsets_need_adjustment())
2660*fae548d3Szrj 	(*p)->adjust_section_offsets();
2661*fae548d3Szrj 
2662*fae548d3Szrj       (*p)->reset_address_and_file_offset();
2663*fae548d3Szrj     }
2664*fae548d3Szrj 
2665*fae548d3Szrj   // Reset special output object address and file offsets.
2666*fae548d3Szrj   for (Data_list::iterator p = this->special_output_list_.begin();
2667*fae548d3Szrj        p != this->special_output_list_.end();
2668*fae548d3Szrj        ++p)
2669*fae548d3Szrj     (*p)->reset_address_and_file_offset();
2670*fae548d3Szrj 
2671*fae548d3Szrj   // A linker script may have created some output section data objects.
2672*fae548d3Szrj   // They are useless now.
2673*fae548d3Szrj   for (Output_section_data_list::const_iterator p =
2674*fae548d3Szrj 	 this->script_output_section_data_list_.begin();
2675*fae548d3Szrj        p != this->script_output_section_data_list_.end();
2676*fae548d3Szrj        ++p)
2677*fae548d3Szrj     delete *p;
2678*fae548d3Szrj   this->script_output_section_data_list_.clear();
2679*fae548d3Szrj 
2680*fae548d3Szrj   // Special-case fill output objects are recreated each time through
2681*fae548d3Szrj   // the relaxation loop.
2682*fae548d3Szrj   this->reset_relax_output();
2683*fae548d3Szrj }
2684*fae548d3Szrj 
2685*fae548d3Szrj void
reset_relax_output()2686*fae548d3Szrj Layout::reset_relax_output()
2687*fae548d3Szrj {
2688*fae548d3Szrj   for (Data_list::const_iterator p = this->relax_output_list_.begin();
2689*fae548d3Szrj        p != this->relax_output_list_.end();
2690*fae548d3Szrj        ++p)
2691*fae548d3Szrj     delete *p;
2692*fae548d3Szrj   this->relax_output_list_.clear();
2693*fae548d3Szrj }
2694*fae548d3Szrj 
2695*fae548d3Szrj // Prepare for relaxation.
2696*fae548d3Szrj 
2697*fae548d3Szrj void
prepare_for_relaxation()2698*fae548d3Szrj Layout::prepare_for_relaxation()
2699*fae548d3Szrj {
2700*fae548d3Szrj   // Create an relaxation debug check if in debugging mode.
2701*fae548d3Szrj   if (is_debugging_enabled(DEBUG_RELAXATION))
2702*fae548d3Szrj     this->relaxation_debug_check_ = new Relaxation_debug_check();
2703*fae548d3Szrj 
2704*fae548d3Szrj   // Save segment states.
2705*fae548d3Szrj   this->segment_states_ = new Segment_states();
2706*fae548d3Szrj   this->save_segments(this->segment_states_);
2707*fae548d3Szrj 
2708*fae548d3Szrj   for(Section_list::const_iterator p = this->section_list_.begin();
2709*fae548d3Szrj       p != this->section_list_.end();
2710*fae548d3Szrj       ++p)
2711*fae548d3Szrj     (*p)->save_states();
2712*fae548d3Szrj 
2713*fae548d3Szrj   if (is_debugging_enabled(DEBUG_RELAXATION))
2714*fae548d3Szrj     this->relaxation_debug_check_->check_output_data_for_reset_values(
2715*fae548d3Szrj 	this->section_list_, this->special_output_list_,
2716*fae548d3Szrj 	this->relax_output_list_);
2717*fae548d3Szrj 
2718*fae548d3Szrj   // Also enable recording of output section data from scripts.
2719*fae548d3Szrj   this->record_output_section_data_from_script_ = true;
2720*fae548d3Szrj }
2721*fae548d3Szrj 
2722*fae548d3Szrj // If the user set the address of the text segment, that may not be
2723*fae548d3Szrj // compatible with putting the segment headers and file headers into
2724*fae548d3Szrj // that segment.  For isolate_execinstr() targets, it's the rodata
2725*fae548d3Szrj // segment rather than text where we might put the headers.
2726*fae548d3Szrj static inline bool
load_seg_unusable_for_headers(const Target * target)2727*fae548d3Szrj load_seg_unusable_for_headers(const Target* target)
2728*fae548d3Szrj {
2729*fae548d3Szrj   const General_options& options = parameters->options();
2730*fae548d3Szrj   if (target->isolate_execinstr())
2731*fae548d3Szrj     return (options.user_set_Trodata_segment()
2732*fae548d3Szrj 	    && options.Trodata_segment() % target->abi_pagesize() != 0);
2733*fae548d3Szrj   else
2734*fae548d3Szrj     return (options.user_set_Ttext()
2735*fae548d3Szrj 	    && options.Ttext() % target->abi_pagesize() != 0);
2736*fae548d3Szrj }
2737*fae548d3Szrj 
2738*fae548d3Szrj // Relaxation loop body:  If target has no relaxation, this runs only once
2739*fae548d3Szrj // Otherwise, the target relaxation hook is called at the end of
2740*fae548d3Szrj // each iteration.  If the hook returns true, it means re-layout of
2741*fae548d3Szrj // section is required.
2742*fae548d3Szrj //
2743*fae548d3Szrj // The number of segments created by a linking script without a PHDRS
2744*fae548d3Szrj // clause may be affected by section sizes and alignments.  There is
2745*fae548d3Szrj // a remote chance that relaxation causes different number of PT_LOAD
2746*fae548d3Szrj // segments are created and sections are attached to different segments.
2747*fae548d3Szrj // Therefore, we always throw away all segments created during section
2748*fae548d3Szrj // layout.  In order to be able to restart the section layout, we keep
2749*fae548d3Szrj // a copy of the segment list right before the relaxation loop and use
2750*fae548d3Szrj // that to restore the segments.
2751*fae548d3Szrj //
2752*fae548d3Szrj // PASS is the current relaxation pass number.
2753*fae548d3Szrj // SYMTAB is a symbol table.
2754*fae548d3Szrj // PLOAD_SEG is the address of a pointer for the load segment.
2755*fae548d3Szrj // PHDR_SEG is a pointer to the PHDR segment.
2756*fae548d3Szrj // SEGMENT_HEADERS points to the output segment header.
2757*fae548d3Szrj // FILE_HEADER points to the output file header.
2758*fae548d3Szrj // PSHNDX is the address to store the output section index.
2759*fae548d3Szrj 
2760*fae548d3Szrj off_t inline
relaxation_loop_body(int pass,Target * target,Symbol_table * symtab,Output_segment ** pload_seg,Output_segment * phdr_seg,Output_segment_headers * segment_headers,Output_file_header * file_header,unsigned int * pshndx)2761*fae548d3Szrj Layout::relaxation_loop_body(
2762*fae548d3Szrj     int pass,
2763*fae548d3Szrj     Target* target,
2764*fae548d3Szrj     Symbol_table* symtab,
2765*fae548d3Szrj     Output_segment** pload_seg,
2766*fae548d3Szrj     Output_segment* phdr_seg,
2767*fae548d3Szrj     Output_segment_headers* segment_headers,
2768*fae548d3Szrj     Output_file_header* file_header,
2769*fae548d3Szrj     unsigned int* pshndx)
2770*fae548d3Szrj {
2771*fae548d3Szrj   // If this is not the first iteration, we need to clean up after
2772*fae548d3Szrj   // relaxation so that we can lay out the sections again.
2773*fae548d3Szrj   if (pass != 0)
2774*fae548d3Szrj     this->clean_up_after_relaxation();
2775*fae548d3Szrj 
2776*fae548d3Szrj   // If there is a SECTIONS clause, put all the input sections into
2777*fae548d3Szrj   // the required order.
2778*fae548d3Szrj   Output_segment* load_seg;
2779*fae548d3Szrj   if (this->script_options_->saw_sections_clause())
2780*fae548d3Szrj     load_seg = this->set_section_addresses_from_script(symtab);
2781*fae548d3Szrj   else if (parameters->options().relocatable())
2782*fae548d3Szrj     load_seg = NULL;
2783*fae548d3Szrj   else
2784*fae548d3Szrj     load_seg = this->find_first_load_seg(target);
2785*fae548d3Szrj 
2786*fae548d3Szrj   if (parameters->options().oformat_enum()
2787*fae548d3Szrj       != General_options::OBJECT_FORMAT_ELF)
2788*fae548d3Szrj     load_seg = NULL;
2789*fae548d3Szrj 
2790*fae548d3Szrj   if (load_seg_unusable_for_headers(target))
2791*fae548d3Szrj     {
2792*fae548d3Szrj       load_seg = NULL;
2793*fae548d3Szrj       phdr_seg = NULL;
2794*fae548d3Szrj     }
2795*fae548d3Szrj 
2796*fae548d3Szrj   gold_assert(phdr_seg == NULL
2797*fae548d3Szrj 	      || load_seg != NULL
2798*fae548d3Szrj 	      || this->script_options_->saw_sections_clause());
2799*fae548d3Szrj 
2800*fae548d3Szrj   // If the address of the load segment we found has been set by
2801*fae548d3Szrj   // --section-start rather than by a script, then adjust the VMA and
2802*fae548d3Szrj   // LMA downward if possible to include the file and section headers.
2803*fae548d3Szrj   uint64_t header_gap = 0;
2804*fae548d3Szrj   if (load_seg != NULL
2805*fae548d3Szrj       && load_seg->are_addresses_set()
2806*fae548d3Szrj       && !this->script_options_->saw_sections_clause()
2807*fae548d3Szrj       && !parameters->options().relocatable())
2808*fae548d3Szrj     {
2809*fae548d3Szrj       file_header->finalize_data_size();
2810*fae548d3Szrj       segment_headers->finalize_data_size();
2811*fae548d3Szrj       size_t sizeof_headers = (file_header->data_size()
2812*fae548d3Szrj 			       + segment_headers->data_size());
2813*fae548d3Szrj       const uint64_t abi_pagesize = target->abi_pagesize();
2814*fae548d3Szrj       uint64_t hdr_paddr = load_seg->paddr() - sizeof_headers;
2815*fae548d3Szrj       hdr_paddr &= ~(abi_pagesize - 1);
2816*fae548d3Szrj       uint64_t subtract = load_seg->paddr() - hdr_paddr;
2817*fae548d3Szrj       if (load_seg->paddr() < subtract || load_seg->vaddr() < subtract)
2818*fae548d3Szrj 	load_seg = NULL;
2819*fae548d3Szrj       else
2820*fae548d3Szrj 	{
2821*fae548d3Szrj 	  load_seg->set_addresses(load_seg->vaddr() - subtract,
2822*fae548d3Szrj 				  load_seg->paddr() - subtract);
2823*fae548d3Szrj 	  header_gap = subtract - sizeof_headers;
2824*fae548d3Szrj 	}
2825*fae548d3Szrj     }
2826*fae548d3Szrj 
2827*fae548d3Szrj   // Lay out the segment headers.
2828*fae548d3Szrj   if (!parameters->options().relocatable())
2829*fae548d3Szrj     {
2830*fae548d3Szrj       gold_assert(segment_headers != NULL);
2831*fae548d3Szrj       if (header_gap != 0 && load_seg != NULL)
2832*fae548d3Szrj 	{
2833*fae548d3Szrj 	  Output_data_zero_fill* z = new Output_data_zero_fill(header_gap, 1);
2834*fae548d3Szrj 	  load_seg->add_initial_output_data(z);
2835*fae548d3Szrj 	}
2836*fae548d3Szrj       if (load_seg != NULL)
2837*fae548d3Szrj 	load_seg->add_initial_output_data(segment_headers);
2838*fae548d3Szrj       if (phdr_seg != NULL)
2839*fae548d3Szrj 	phdr_seg->add_initial_output_data(segment_headers);
2840*fae548d3Szrj     }
2841*fae548d3Szrj 
2842*fae548d3Szrj   // Lay out the file header.
2843*fae548d3Szrj   if (load_seg != NULL)
2844*fae548d3Szrj     load_seg->add_initial_output_data(file_header);
2845*fae548d3Szrj 
2846*fae548d3Szrj   if (this->script_options_->saw_phdrs_clause()
2847*fae548d3Szrj       && !parameters->options().relocatable())
2848*fae548d3Szrj     {
2849*fae548d3Szrj       // Support use of FILEHDRS and PHDRS attachments in a PHDRS
2850*fae548d3Szrj       // clause in a linker script.
2851*fae548d3Szrj       Script_sections* ss = this->script_options_->script_sections();
2852*fae548d3Szrj       ss->put_headers_in_phdrs(file_header, segment_headers);
2853*fae548d3Szrj     }
2854*fae548d3Szrj 
2855*fae548d3Szrj   // We set the output section indexes in set_segment_offsets and
2856*fae548d3Szrj   // set_section_indexes.
2857*fae548d3Szrj   *pshndx = 1;
2858*fae548d3Szrj 
2859*fae548d3Szrj   // Set the file offsets of all the segments, and all the sections
2860*fae548d3Szrj   // they contain.
2861*fae548d3Szrj   off_t off;
2862*fae548d3Szrj   if (!parameters->options().relocatable())
2863*fae548d3Szrj     off = this->set_segment_offsets(target, load_seg, pshndx);
2864*fae548d3Szrj   else
2865*fae548d3Szrj     off = this->set_relocatable_section_offsets(file_header, pshndx);
2866*fae548d3Szrj 
2867*fae548d3Szrj    // Verify that the dummy relaxation does not change anything.
2868*fae548d3Szrj   if (is_debugging_enabled(DEBUG_RELAXATION))
2869*fae548d3Szrj     {
2870*fae548d3Szrj       if (pass == 0)
2871*fae548d3Szrj 	this->relaxation_debug_check_->read_sections(this->section_list_);
2872*fae548d3Szrj       else
2873*fae548d3Szrj 	this->relaxation_debug_check_->verify_sections(this->section_list_);
2874*fae548d3Szrj     }
2875*fae548d3Szrj 
2876*fae548d3Szrj   *pload_seg = load_seg;
2877*fae548d3Szrj   return off;
2878*fae548d3Szrj }
2879*fae548d3Szrj 
2880*fae548d3Szrj // Search the list of patterns and find the position of the given section
2881*fae548d3Szrj // name in the output section.  If the section name matches a glob
2882*fae548d3Szrj // pattern and a non-glob name, then the non-glob position takes
2883*fae548d3Szrj // precedence.  Return 0 if no match is found.
2884*fae548d3Szrj 
2885*fae548d3Szrj unsigned int
find_section_order_index(const std::string & section_name)2886*fae548d3Szrj Layout::find_section_order_index(const std::string& section_name)
2887*fae548d3Szrj {
2888*fae548d3Szrj   Unordered_map<std::string, unsigned int>::iterator map_it;
2889*fae548d3Szrj   map_it = this->input_section_position_.find(section_name);
2890*fae548d3Szrj   if (map_it != this->input_section_position_.end())
2891*fae548d3Szrj     return map_it->second;
2892*fae548d3Szrj 
2893*fae548d3Szrj   // Absolute match failed.  Linear search the glob patterns.
2894*fae548d3Szrj   std::vector<std::string>::iterator it;
2895*fae548d3Szrj   for (it = this->input_section_glob_.begin();
2896*fae548d3Szrj        it != this->input_section_glob_.end();
2897*fae548d3Szrj        ++it)
2898*fae548d3Szrj     {
2899*fae548d3Szrj        if (fnmatch((*it).c_str(), section_name.c_str(), FNM_NOESCAPE) == 0)
2900*fae548d3Szrj 	 {
2901*fae548d3Szrj 	   map_it = this->input_section_position_.find(*it);
2902*fae548d3Szrj 	   gold_assert(map_it != this->input_section_position_.end());
2903*fae548d3Szrj 	   return map_it->second;
2904*fae548d3Szrj 	 }
2905*fae548d3Szrj     }
2906*fae548d3Szrj   return 0;
2907*fae548d3Szrj }
2908*fae548d3Szrj 
2909*fae548d3Szrj // Read the sequence of input sections from the file specified with
2910*fae548d3Szrj // option --section-ordering-file.
2911*fae548d3Szrj 
2912*fae548d3Szrj void
read_layout_from_file()2913*fae548d3Szrj Layout::read_layout_from_file()
2914*fae548d3Szrj {
2915*fae548d3Szrj   const char* filename = parameters->options().section_ordering_file();
2916*fae548d3Szrj   std::ifstream in;
2917*fae548d3Szrj   std::string line;
2918*fae548d3Szrj 
2919*fae548d3Szrj   in.open(filename);
2920*fae548d3Szrj   if (!in)
2921*fae548d3Szrj     gold_fatal(_("unable to open --section-ordering-file file %s: %s"),
2922*fae548d3Szrj 	       filename, strerror(errno));
2923*fae548d3Szrj 
2924*fae548d3Szrj   std::getline(in, line);   // this chops off the trailing \n, if any
2925*fae548d3Szrj   unsigned int position = 1;
2926*fae548d3Szrj   this->set_section_ordering_specified();
2927*fae548d3Szrj 
2928*fae548d3Szrj   while (in)
2929*fae548d3Szrj     {
2930*fae548d3Szrj       if (!line.empty() && line[line.length() - 1] == '\r')   // Windows
2931*fae548d3Szrj 	line.resize(line.length() - 1);
2932*fae548d3Szrj       // Ignore comments, beginning with '#'
2933*fae548d3Szrj       if (line[0] == '#')
2934*fae548d3Szrj 	{
2935*fae548d3Szrj 	  std::getline(in, line);
2936*fae548d3Szrj 	  continue;
2937*fae548d3Szrj 	}
2938*fae548d3Szrj       this->input_section_position_[line] = position;
2939*fae548d3Szrj       // Store all glob patterns in a vector.
2940*fae548d3Szrj       if (is_wildcard_string(line.c_str()))
2941*fae548d3Szrj 	this->input_section_glob_.push_back(line);
2942*fae548d3Szrj       position++;
2943*fae548d3Szrj       std::getline(in, line);
2944*fae548d3Szrj     }
2945*fae548d3Szrj }
2946*fae548d3Szrj 
2947*fae548d3Szrj // Finalize the layout.  When this is called, we have created all the
2948*fae548d3Szrj // output sections and all the output segments which are based on
2949*fae548d3Szrj // input sections.  We have several things to do, and we have to do
2950*fae548d3Szrj // them in the right order, so that we get the right results correctly
2951*fae548d3Szrj // and efficiently.
2952*fae548d3Szrj 
2953*fae548d3Szrj // 1) Finalize the list of output segments and create the segment
2954*fae548d3Szrj // table header.
2955*fae548d3Szrj 
2956*fae548d3Szrj // 2) Finalize the dynamic symbol table and associated sections.
2957*fae548d3Szrj 
2958*fae548d3Szrj // 3) Determine the final file offset of all the output segments.
2959*fae548d3Szrj 
2960*fae548d3Szrj // 4) Determine the final file offset of all the SHF_ALLOC output
2961*fae548d3Szrj // sections.
2962*fae548d3Szrj 
2963*fae548d3Szrj // 5) Create the symbol table sections and the section name table
2964*fae548d3Szrj // section.
2965*fae548d3Szrj 
2966*fae548d3Szrj // 6) Finalize the symbol table: set symbol values to their final
2967*fae548d3Szrj // value and make a final determination of which symbols are going
2968*fae548d3Szrj // into the output symbol table.
2969*fae548d3Szrj 
2970*fae548d3Szrj // 7) Create the section table header.
2971*fae548d3Szrj 
2972*fae548d3Szrj // 8) Determine the final file offset of all the output sections which
2973*fae548d3Szrj // are not SHF_ALLOC, including the section table header.
2974*fae548d3Szrj 
2975*fae548d3Szrj // 9) Finalize the ELF file header.
2976*fae548d3Szrj 
2977*fae548d3Szrj // This function returns the size of the output file.
2978*fae548d3Szrj 
2979*fae548d3Szrj off_t
finalize(const Input_objects * input_objects,Symbol_table * symtab,Target * target,const Task * task)2980*fae548d3Szrj Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
2981*fae548d3Szrj 		 Target* target, const Task* task)
2982*fae548d3Szrj {
2983*fae548d3Szrj   unsigned int local_dynamic_count = 0;
2984*fae548d3Szrj   unsigned int forced_local_dynamic_count = 0;
2985*fae548d3Szrj 
2986*fae548d3Szrj   target->finalize_sections(this, input_objects, symtab);
2987*fae548d3Szrj 
2988*fae548d3Szrj   this->count_local_symbols(task, input_objects);
2989*fae548d3Szrj 
2990*fae548d3Szrj   this->link_stabs_sections();
2991*fae548d3Szrj 
2992*fae548d3Szrj   Output_segment* phdr_seg = NULL;
2993*fae548d3Szrj   if (!parameters->options().relocatable() && !parameters->doing_static_link())
2994*fae548d3Szrj     {
2995*fae548d3Szrj       // There was a dynamic object in the link.  We need to create
2996*fae548d3Szrj       // some information for the dynamic linker.
2997*fae548d3Szrj 
2998*fae548d3Szrj       // Create the PT_PHDR segment which will hold the program
2999*fae548d3Szrj       // headers.
3000*fae548d3Szrj       if (!this->script_options_->saw_phdrs_clause())
3001*fae548d3Szrj 	phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
3002*fae548d3Szrj 
3003*fae548d3Szrj       // Create the dynamic symbol table, including the hash table.
3004*fae548d3Szrj       Output_section* dynstr;
3005*fae548d3Szrj       std::vector<Symbol*> dynamic_symbols;
3006*fae548d3Szrj       Versions versions(*this->script_options()->version_script_info(),
3007*fae548d3Szrj 			&this->dynpool_);
3008*fae548d3Szrj       this->create_dynamic_symtab(input_objects, symtab, &dynstr,
3009*fae548d3Szrj 				  &local_dynamic_count,
3010*fae548d3Szrj 				  &forced_local_dynamic_count,
3011*fae548d3Szrj 				  &dynamic_symbols,
3012*fae548d3Szrj 				  &versions);
3013*fae548d3Szrj 
3014*fae548d3Szrj       // Create the .interp section to hold the name of the
3015*fae548d3Szrj       // interpreter, and put it in a PT_INTERP segment.  Don't do it
3016*fae548d3Szrj       // if we saw a .interp section in an input file.
3017*fae548d3Szrj       if ((!parameters->options().shared()
3018*fae548d3Szrj 	   || parameters->options().dynamic_linker() != NULL)
3019*fae548d3Szrj 	  && this->interp_segment_ == NULL)
3020*fae548d3Szrj 	this->create_interp(target);
3021*fae548d3Szrj 
3022*fae548d3Szrj       // Finish the .dynamic section to hold the dynamic data, and put
3023*fae548d3Szrj       // it in a PT_DYNAMIC segment.
3024*fae548d3Szrj       this->finish_dynamic_section(input_objects, symtab);
3025*fae548d3Szrj 
3026*fae548d3Szrj       // We should have added everything we need to the dynamic string
3027*fae548d3Szrj       // table.
3028*fae548d3Szrj       this->dynpool_.set_string_offsets();
3029*fae548d3Szrj 
3030*fae548d3Szrj       // Create the version sections.  We can't do this until the
3031*fae548d3Szrj       // dynamic string table is complete.
3032*fae548d3Szrj       this->create_version_sections(&versions, symtab,
3033*fae548d3Szrj 				    (local_dynamic_count
3034*fae548d3Szrj 				     + forced_local_dynamic_count),
3035*fae548d3Szrj 				    dynamic_symbols, dynstr);
3036*fae548d3Szrj 
3037*fae548d3Szrj       // Set the size of the _DYNAMIC symbol.  We can't do this until
3038*fae548d3Szrj       // after we call create_version_sections.
3039*fae548d3Szrj       this->set_dynamic_symbol_size(symtab);
3040*fae548d3Szrj     }
3041*fae548d3Szrj 
3042*fae548d3Szrj   // Create segment headers.
3043*fae548d3Szrj   Output_segment_headers* segment_headers =
3044*fae548d3Szrj     (parameters->options().relocatable()
3045*fae548d3Szrj      ? NULL
3046*fae548d3Szrj      : new Output_segment_headers(this->segment_list_));
3047*fae548d3Szrj 
3048*fae548d3Szrj   // Lay out the file header.
3049*fae548d3Szrj   Output_file_header* file_header = new Output_file_header(target, symtab,
3050*fae548d3Szrj 							   segment_headers);
3051*fae548d3Szrj 
3052*fae548d3Szrj   this->special_output_list_.push_back(file_header);
3053*fae548d3Szrj   if (segment_headers != NULL)
3054*fae548d3Szrj     this->special_output_list_.push_back(segment_headers);
3055*fae548d3Szrj 
3056*fae548d3Szrj   // Find approriate places for orphan output sections if we are using
3057*fae548d3Szrj   // a linker script.
3058*fae548d3Szrj   if (this->script_options_->saw_sections_clause())
3059*fae548d3Szrj     this->place_orphan_sections_in_script();
3060*fae548d3Szrj 
3061*fae548d3Szrj   Output_segment* load_seg;
3062*fae548d3Szrj   off_t off;
3063*fae548d3Szrj   unsigned int shndx;
3064*fae548d3Szrj   int pass = 0;
3065*fae548d3Szrj 
3066*fae548d3Szrj   // Take a snapshot of the section layout as needed.
3067*fae548d3Szrj   if (target->may_relax())
3068*fae548d3Szrj     this->prepare_for_relaxation();
3069*fae548d3Szrj 
3070*fae548d3Szrj   // Run the relaxation loop to lay out sections.
3071*fae548d3Szrj   do
3072*fae548d3Szrj     {
3073*fae548d3Szrj       off = this->relaxation_loop_body(pass, target, symtab, &load_seg,
3074*fae548d3Szrj 				       phdr_seg, segment_headers, file_header,
3075*fae548d3Szrj 				       &shndx);
3076*fae548d3Szrj       pass++;
3077*fae548d3Szrj     }
3078*fae548d3Szrj   while (target->may_relax()
3079*fae548d3Szrj 	 && target->relax(pass, input_objects, symtab, this, task));
3080*fae548d3Szrj 
3081*fae548d3Szrj   // If there is a load segment that contains the file and program headers,
3082*fae548d3Szrj   // provide a symbol __ehdr_start pointing there.
3083*fae548d3Szrj   // A program can use this to examine itself robustly.
3084*fae548d3Szrj   Symbol *ehdr_start = symtab->lookup("__ehdr_start");
3085*fae548d3Szrj   if (ehdr_start != NULL && ehdr_start->is_predefined())
3086*fae548d3Szrj     {
3087*fae548d3Szrj       if (load_seg != NULL)
3088*fae548d3Szrj 	ehdr_start->set_output_segment(load_seg, Symbol::SEGMENT_START);
3089*fae548d3Szrj       else
3090*fae548d3Szrj 	ehdr_start->set_undefined();
3091*fae548d3Szrj     }
3092*fae548d3Szrj 
3093*fae548d3Szrj   // Set the file offsets of all the non-data sections we've seen so
3094*fae548d3Szrj   // far which don't have to wait for the input sections.  We need
3095*fae548d3Szrj   // this in order to finalize local symbols in non-allocated
3096*fae548d3Szrj   // sections.
3097*fae548d3Szrj   off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
3098*fae548d3Szrj 
3099*fae548d3Szrj   // Set the section indexes of all unallocated sections seen so far,
3100*fae548d3Szrj   // in case any of them are somehow referenced by a symbol.
3101*fae548d3Szrj   shndx = this->set_section_indexes(shndx);
3102*fae548d3Szrj 
3103*fae548d3Szrj   // Create the symbol table sections.
3104*fae548d3Szrj   this->create_symtab_sections(input_objects, symtab, shndx, &off,
3105*fae548d3Szrj 			       local_dynamic_count);
3106*fae548d3Szrj   if (!parameters->doing_static_link())
3107*fae548d3Szrj     this->assign_local_dynsym_offsets(input_objects);
3108*fae548d3Szrj 
3109*fae548d3Szrj   // Process any symbol assignments from a linker script.  This must
3110*fae548d3Szrj   // be called after the symbol table has been finalized.
3111*fae548d3Szrj   this->script_options_->finalize_symbols(symtab, this);
3112*fae548d3Szrj 
3113*fae548d3Szrj   // Create the incremental inputs sections.
3114*fae548d3Szrj   if (this->incremental_inputs_)
3115*fae548d3Szrj     {
3116*fae548d3Szrj       this->incremental_inputs_->finalize();
3117*fae548d3Szrj       this->create_incremental_info_sections(symtab);
3118*fae548d3Szrj     }
3119*fae548d3Szrj 
3120*fae548d3Szrj   // Create the .shstrtab section.
3121*fae548d3Szrj   Output_section* shstrtab_section = this->create_shstrtab();
3122*fae548d3Szrj 
3123*fae548d3Szrj   // Set the file offsets of the rest of the non-data sections which
3124*fae548d3Szrj   // don't have to wait for the input sections.
3125*fae548d3Szrj   off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
3126*fae548d3Szrj 
3127*fae548d3Szrj   // Now that all sections have been created, set the section indexes
3128*fae548d3Szrj   // for any sections which haven't been done yet.
3129*fae548d3Szrj   shndx = this->set_section_indexes(shndx);
3130*fae548d3Szrj 
3131*fae548d3Szrj   // Create the section table header.
3132*fae548d3Szrj   this->create_shdrs(shstrtab_section, &off);
3133*fae548d3Szrj 
3134*fae548d3Szrj   // If there are no sections which require postprocessing, we can
3135*fae548d3Szrj   // handle the section names now, and avoid a resize later.
3136*fae548d3Szrj   if (!this->any_postprocessing_sections_)
3137*fae548d3Szrj     {
3138*fae548d3Szrj       off = this->set_section_offsets(off,
3139*fae548d3Szrj 				      POSTPROCESSING_SECTIONS_PASS);
3140*fae548d3Szrj       off =
3141*fae548d3Szrj 	  this->set_section_offsets(off,
3142*fae548d3Szrj 				    STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
3143*fae548d3Szrj     }
3144*fae548d3Szrj 
3145*fae548d3Szrj   file_header->set_section_info(this->section_headers_, shstrtab_section);
3146*fae548d3Szrj 
3147*fae548d3Szrj   // Now we know exactly where everything goes in the output file
3148*fae548d3Szrj   // (except for non-allocated sections which require postprocessing).
3149*fae548d3Szrj   Output_data::layout_complete();
3150*fae548d3Szrj 
3151*fae548d3Szrj   this->output_file_size_ = off;
3152*fae548d3Szrj 
3153*fae548d3Szrj   return off;
3154*fae548d3Szrj }
3155*fae548d3Szrj 
3156*fae548d3Szrj // Create a note header following the format defined in the ELF ABI.
3157*fae548d3Szrj // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
3158*fae548d3Szrj // of the section to create, DESCSZ is the size of the descriptor.
3159*fae548d3Szrj // ALLOCATE is true if the section should be allocated in memory.
3160*fae548d3Szrj // This returns the new note section.  It sets *TRAILING_PADDING to
3161*fae548d3Szrj // the number of trailing zero bytes required.
3162*fae548d3Szrj 
3163*fae548d3Szrj Output_section*
create_note(const char * name,int note_type,const char * section_name,size_t descsz,bool allocate,size_t * trailing_padding)3164*fae548d3Szrj Layout::create_note(const char* name, int note_type,
3165*fae548d3Szrj 		    const char* section_name, size_t descsz,
3166*fae548d3Szrj 		    bool allocate, size_t* trailing_padding)
3167*fae548d3Szrj {
3168*fae548d3Szrj   // Authorities all agree that the values in a .note field should
3169*fae548d3Szrj   // be aligned on 4-byte boundaries for 32-bit binaries.  However,
3170*fae548d3Szrj   // they differ on what the alignment is for 64-bit binaries.
3171*fae548d3Szrj   // The GABI says unambiguously they take 8-byte alignment:
3172*fae548d3Szrj   //    http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
3173*fae548d3Szrj   // Other documentation says alignment should always be 4 bytes:
3174*fae548d3Szrj   //    http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
3175*fae548d3Szrj   // GNU ld and GNU readelf both support the latter (at least as of
3176*fae548d3Szrj   // version 2.16.91), and glibc always generates the latter for
3177*fae548d3Szrj   // .note.ABI-tag (as of version 1.6), so that's the one we go with
3178*fae548d3Szrj   // here.
3179*fae548d3Szrj #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION   // This is not defined by default.
3180*fae548d3Szrj   const int size = parameters->target().get_size();
3181*fae548d3Szrj #else
3182*fae548d3Szrj   const int size = 32;
3183*fae548d3Szrj #endif
3184*fae548d3Szrj 
3185*fae548d3Szrj   // The contents of the .note section.
3186*fae548d3Szrj   size_t namesz = strlen(name) + 1;
3187*fae548d3Szrj   size_t aligned_namesz = align_address(namesz, size / 8);
3188*fae548d3Szrj   size_t aligned_descsz = align_address(descsz, size / 8);
3189*fae548d3Szrj 
3190*fae548d3Szrj   size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
3191*fae548d3Szrj 
3192*fae548d3Szrj   unsigned char* buffer = new unsigned char[notehdrsz];
3193*fae548d3Szrj   memset(buffer, 0, notehdrsz);
3194*fae548d3Szrj 
3195*fae548d3Szrj   bool is_big_endian = parameters->target().is_big_endian();
3196*fae548d3Szrj 
3197*fae548d3Szrj   if (size == 32)
3198*fae548d3Szrj     {
3199*fae548d3Szrj       if (!is_big_endian)
3200*fae548d3Szrj 	{
3201*fae548d3Szrj 	  elfcpp::Swap<32, false>::writeval(buffer, namesz);
3202*fae548d3Szrj 	  elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
3203*fae548d3Szrj 	  elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
3204*fae548d3Szrj 	}
3205*fae548d3Szrj       else
3206*fae548d3Szrj 	{
3207*fae548d3Szrj 	  elfcpp::Swap<32, true>::writeval(buffer, namesz);
3208*fae548d3Szrj 	  elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
3209*fae548d3Szrj 	  elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
3210*fae548d3Szrj 	}
3211*fae548d3Szrj     }
3212*fae548d3Szrj   else if (size == 64)
3213*fae548d3Szrj     {
3214*fae548d3Szrj       if (!is_big_endian)
3215*fae548d3Szrj 	{
3216*fae548d3Szrj 	  elfcpp::Swap<64, false>::writeval(buffer, namesz);
3217*fae548d3Szrj 	  elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
3218*fae548d3Szrj 	  elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
3219*fae548d3Szrj 	}
3220*fae548d3Szrj       else
3221*fae548d3Szrj 	{
3222*fae548d3Szrj 	  elfcpp::Swap<64, true>::writeval(buffer, namesz);
3223*fae548d3Szrj 	  elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
3224*fae548d3Szrj 	  elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
3225*fae548d3Szrj 	}
3226*fae548d3Szrj     }
3227*fae548d3Szrj   else
3228*fae548d3Szrj     gold_unreachable();
3229*fae548d3Szrj 
3230*fae548d3Szrj   memcpy(buffer + 3 * (size / 8), name, namesz);
3231*fae548d3Szrj 
3232*fae548d3Szrj   elfcpp::Elf_Xword flags = 0;
3233*fae548d3Szrj   Output_section_order order = ORDER_INVALID;
3234*fae548d3Szrj   if (allocate)
3235*fae548d3Szrj     {
3236*fae548d3Szrj       flags = elfcpp::SHF_ALLOC;
3237*fae548d3Szrj       order = ORDER_RO_NOTE;
3238*fae548d3Szrj     }
3239*fae548d3Szrj   Output_section* os = this->choose_output_section(NULL, section_name,
3240*fae548d3Szrj 						   elfcpp::SHT_NOTE,
3241*fae548d3Szrj 						   flags, false, order, false,
3242*fae548d3Szrj 						   false, true);
3243*fae548d3Szrj   if (os == NULL)
3244*fae548d3Szrj     return NULL;
3245*fae548d3Szrj 
3246*fae548d3Szrj   Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
3247*fae548d3Szrj 							   size / 8,
3248*fae548d3Szrj 							   "** note header");
3249*fae548d3Szrj   os->add_output_section_data(posd);
3250*fae548d3Szrj 
3251*fae548d3Szrj   *trailing_padding = aligned_descsz - descsz;
3252*fae548d3Szrj 
3253*fae548d3Szrj   return os;
3254*fae548d3Szrj }
3255*fae548d3Szrj 
3256*fae548d3Szrj // Create a .note.gnu.property section to record program properties
3257*fae548d3Szrj // accumulated from the input files.
3258*fae548d3Szrj 
3259*fae548d3Szrj void
create_gnu_properties_note()3260*fae548d3Szrj Layout::create_gnu_properties_note()
3261*fae548d3Szrj {
3262*fae548d3Szrj   parameters->target().finalize_gnu_properties(this);
3263*fae548d3Szrj 
3264*fae548d3Szrj   if (this->gnu_properties_.empty())
3265*fae548d3Szrj     return;
3266*fae548d3Szrj 
3267*fae548d3Szrj   const unsigned int size = parameters->target().get_size();
3268*fae548d3Szrj   const bool is_big_endian = parameters->target().is_big_endian();
3269*fae548d3Szrj 
3270*fae548d3Szrj   // Compute the total size of the properties array.
3271*fae548d3Szrj   size_t descsz = 0;
3272*fae548d3Szrj   for (Gnu_properties::const_iterator prop = this->gnu_properties_.begin();
3273*fae548d3Szrj        prop != this->gnu_properties_.end();
3274*fae548d3Szrj        ++prop)
3275*fae548d3Szrj     {
3276*fae548d3Szrj       descsz = align_address(descsz + 8 + prop->second.pr_datasz, size / 8);
3277*fae548d3Szrj     }
3278*fae548d3Szrj 
3279*fae548d3Szrj   // Create the note section.
3280*fae548d3Szrj   size_t trailing_padding;
3281*fae548d3Szrj   Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_PROPERTY_TYPE_0,
3282*fae548d3Szrj 					 ".note.gnu.property", descsz,
3283*fae548d3Szrj 					 true, &trailing_padding);
3284*fae548d3Szrj   if (os == NULL)
3285*fae548d3Szrj     return;
3286*fae548d3Szrj   gold_assert(trailing_padding == 0);
3287*fae548d3Szrj 
3288*fae548d3Szrj   // Allocate and fill the properties array.
3289*fae548d3Szrj   unsigned char* desc = new unsigned char[descsz];
3290*fae548d3Szrj   unsigned char* p = desc;
3291*fae548d3Szrj   for (Gnu_properties::const_iterator prop = this->gnu_properties_.begin();
3292*fae548d3Szrj        prop != this->gnu_properties_.end();
3293*fae548d3Szrj        ++prop)
3294*fae548d3Szrj     {
3295*fae548d3Szrj       size_t datasz = prop->second.pr_datasz;
3296*fae548d3Szrj       size_t aligned_datasz = align_address(prop->second.pr_datasz, size / 8);
3297*fae548d3Szrj       write_sized_value(prop->first, 4, p, is_big_endian);
3298*fae548d3Szrj       write_sized_value(datasz, 4, p + 4, is_big_endian);
3299*fae548d3Szrj       memcpy(p + 8, prop->second.pr_data, datasz);
3300*fae548d3Szrj       if (aligned_datasz > datasz)
3301*fae548d3Szrj         memset(p + 8 + datasz, 0, aligned_datasz - datasz);
3302*fae548d3Szrj       p += 8 + aligned_datasz;
3303*fae548d3Szrj     }
3304*fae548d3Szrj   Output_section_data* posd = new Output_data_const(desc, descsz, 4);
3305*fae548d3Szrj   os->add_output_section_data(posd);
3306*fae548d3Szrj }
3307*fae548d3Szrj 
3308*fae548d3Szrj // For an executable or shared library, create a note to record the
3309*fae548d3Szrj // version of gold used to create the binary.
3310*fae548d3Szrj 
3311*fae548d3Szrj void
create_gold_note()3312*fae548d3Szrj Layout::create_gold_note()
3313*fae548d3Szrj {
3314*fae548d3Szrj   if (parameters->options().relocatable()
3315*fae548d3Szrj       || parameters->incremental_update())
3316*fae548d3Szrj     return;
3317*fae548d3Szrj 
3318*fae548d3Szrj   std::string desc = std::string("gold ") + gold::get_version_string();
3319*fae548d3Szrj 
3320*fae548d3Szrj   size_t trailing_padding;
3321*fae548d3Szrj   Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
3322*fae548d3Szrj 					 ".note.gnu.gold-version", desc.size(),
3323*fae548d3Szrj 					 false, &trailing_padding);
3324*fae548d3Szrj   if (os == NULL)
3325*fae548d3Szrj     return;
3326*fae548d3Szrj 
3327*fae548d3Szrj   Output_section_data* posd = new Output_data_const(desc, 4);
3328*fae548d3Szrj   os->add_output_section_data(posd);
3329*fae548d3Szrj 
3330*fae548d3Szrj   if (trailing_padding > 0)
3331*fae548d3Szrj     {
3332*fae548d3Szrj       posd = new Output_data_zero_fill(trailing_padding, 0);
3333*fae548d3Szrj       os->add_output_section_data(posd);
3334*fae548d3Szrj     }
3335*fae548d3Szrj }
3336*fae548d3Szrj 
3337*fae548d3Szrj // Record whether the stack should be executable.  This can be set
3338*fae548d3Szrj // from the command line using the -z execstack or -z noexecstack
3339*fae548d3Szrj // options.  Otherwise, if any input file has a .note.GNU-stack
3340*fae548d3Szrj // section with the SHF_EXECINSTR flag set, the stack should be
3341*fae548d3Szrj // executable.  Otherwise, if at least one input file a
3342*fae548d3Szrj // .note.GNU-stack section, and some input file has no .note.GNU-stack
3343*fae548d3Szrj // section, we use the target default for whether the stack should be
3344*fae548d3Szrj // executable.  If -z stack-size was used to set a p_memsz value for
3345*fae548d3Szrj // PT_GNU_STACK, we generate the segment regardless.  Otherwise, we
3346*fae548d3Szrj // don't generate a stack note.  When generating a object file, we
3347*fae548d3Szrj // create a .note.GNU-stack section with the appropriate marking.
3348*fae548d3Szrj // When generating an executable or shared library, we create a
3349*fae548d3Szrj // PT_GNU_STACK segment.
3350*fae548d3Szrj 
3351*fae548d3Szrj void
create_stack_segment()3352*fae548d3Szrj Layout::create_stack_segment()
3353*fae548d3Szrj {
3354*fae548d3Szrj   bool is_stack_executable;
3355*fae548d3Szrj   if (parameters->options().is_execstack_set())
3356*fae548d3Szrj     {
3357*fae548d3Szrj       is_stack_executable = parameters->options().is_stack_executable();
3358*fae548d3Szrj       if (!is_stack_executable
3359*fae548d3Szrj 	  && this->input_requires_executable_stack_
3360*fae548d3Szrj 	  && parameters->options().warn_execstack())
3361*fae548d3Szrj 	gold_warning(_("one or more inputs require executable stack, "
3362*fae548d3Szrj 		       "but -z noexecstack was given"));
3363*fae548d3Szrj     }
3364*fae548d3Szrj   else if (!this->input_with_gnu_stack_note_
3365*fae548d3Szrj 	   && (!parameters->options().user_set_stack_size()
3366*fae548d3Szrj 	       || parameters->options().relocatable()))
3367*fae548d3Szrj     return;
3368*fae548d3Szrj   else
3369*fae548d3Szrj     {
3370*fae548d3Szrj       if (this->input_requires_executable_stack_)
3371*fae548d3Szrj 	is_stack_executable = true;
3372*fae548d3Szrj       else if (this->input_without_gnu_stack_note_)
3373*fae548d3Szrj 	is_stack_executable =
3374*fae548d3Szrj 	  parameters->target().is_default_stack_executable();
3375*fae548d3Szrj       else
3376*fae548d3Szrj 	is_stack_executable = false;
3377*fae548d3Szrj     }
3378*fae548d3Szrj 
3379*fae548d3Szrj   if (parameters->options().relocatable())
3380*fae548d3Szrj     {
3381*fae548d3Szrj       const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
3382*fae548d3Szrj       elfcpp::Elf_Xword flags = 0;
3383*fae548d3Szrj       if (is_stack_executable)
3384*fae548d3Szrj 	flags |= elfcpp::SHF_EXECINSTR;
3385*fae548d3Szrj       this->make_output_section(name, elfcpp::SHT_PROGBITS, flags,
3386*fae548d3Szrj 				ORDER_INVALID, false);
3387*fae548d3Szrj     }
3388*fae548d3Szrj   else
3389*fae548d3Szrj     {
3390*fae548d3Szrj       if (this->script_options_->saw_phdrs_clause())
3391*fae548d3Szrj 	return;
3392*fae548d3Szrj       int flags = elfcpp::PF_R | elfcpp::PF_W;
3393*fae548d3Szrj       if (is_stack_executable)
3394*fae548d3Szrj 	flags |= elfcpp::PF_X;
3395*fae548d3Szrj       Output_segment* seg =
3396*fae548d3Szrj 	this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
3397*fae548d3Szrj       seg->set_size(parameters->options().stack_size());
3398*fae548d3Szrj       // BFD lets targets override this default alignment, but the only
3399*fae548d3Szrj       // targets that do so are ones that Gold does not support so far.
3400*fae548d3Szrj       seg->set_minimum_p_align(16);
3401*fae548d3Szrj     }
3402*fae548d3Szrj }
3403*fae548d3Szrj 
3404*fae548d3Szrj // If --build-id was used, set up the build ID note.
3405*fae548d3Szrj 
3406*fae548d3Szrj void
create_build_id()3407*fae548d3Szrj Layout::create_build_id()
3408*fae548d3Szrj {
3409*fae548d3Szrj   if (!parameters->options().user_set_build_id())
3410*fae548d3Szrj     return;
3411*fae548d3Szrj 
3412*fae548d3Szrj   const char* style = parameters->options().build_id();
3413*fae548d3Szrj   if (strcmp(style, "none") == 0)
3414*fae548d3Szrj     return;
3415*fae548d3Szrj 
3416*fae548d3Szrj   // Set DESCSZ to the size of the note descriptor.  When possible,
3417*fae548d3Szrj   // set DESC to the note descriptor contents.
3418*fae548d3Szrj   size_t descsz;
3419*fae548d3Szrj   std::string desc;
3420*fae548d3Szrj   if (strcmp(style, "md5") == 0)
3421*fae548d3Szrj     descsz = 128 / 8;
3422*fae548d3Szrj   else if ((strcmp(style, "sha1") == 0) || (strcmp(style, "tree") == 0))
3423*fae548d3Szrj     descsz = 160 / 8;
3424*fae548d3Szrj   else if (strcmp(style, "uuid") == 0)
3425*fae548d3Szrj     {
3426*fae548d3Szrj #ifndef __MINGW32__
3427*fae548d3Szrj       const size_t uuidsz = 128 / 8;
3428*fae548d3Szrj 
3429*fae548d3Szrj       char buffer[uuidsz];
3430*fae548d3Szrj       memset(buffer, 0, uuidsz);
3431*fae548d3Szrj 
3432*fae548d3Szrj       int descriptor = open_descriptor(-1, "/dev/urandom", O_RDONLY);
3433*fae548d3Szrj       if (descriptor < 0)
3434*fae548d3Szrj 	gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
3435*fae548d3Szrj 		   strerror(errno));
3436*fae548d3Szrj       else
3437*fae548d3Szrj 	{
3438*fae548d3Szrj 	  ssize_t got = ::read(descriptor, buffer, uuidsz);
3439*fae548d3Szrj 	  release_descriptor(descriptor, true);
3440*fae548d3Szrj 	  if (got < 0)
3441*fae548d3Szrj 	    gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
3442*fae548d3Szrj 	  else if (static_cast<size_t>(got) != uuidsz)
3443*fae548d3Szrj 	    gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
3444*fae548d3Szrj 		       uuidsz, got);
3445*fae548d3Szrj 	}
3446*fae548d3Szrj 
3447*fae548d3Szrj       desc.assign(buffer, uuidsz);
3448*fae548d3Szrj       descsz = uuidsz;
3449*fae548d3Szrj #else // __MINGW32__
3450*fae548d3Szrj       UUID uuid;
3451*fae548d3Szrj       typedef RPC_STATUS (RPC_ENTRY *UuidCreateFn)(UUID *Uuid);
3452*fae548d3Szrj 
3453*fae548d3Szrj       HMODULE rpc_library = LoadLibrary("rpcrt4.dll");
3454*fae548d3Szrj       if (!rpc_library)
3455*fae548d3Szrj 	gold_error(_("--build-id=uuid failed: could not load rpcrt4.dll"));
3456*fae548d3Szrj       else
3457*fae548d3Szrj 	{
3458*fae548d3Szrj 	  UuidCreateFn uuid_create = reinterpret_cast<UuidCreateFn>(
3459*fae548d3Szrj 	      GetProcAddress(rpc_library, "UuidCreate"));
3460*fae548d3Szrj 	  if (!uuid_create)
3461*fae548d3Szrj 	    gold_error(_("--build-id=uuid failed: could not find UuidCreate"));
3462*fae548d3Szrj 	  else if (uuid_create(&uuid) != RPC_S_OK)
3463*fae548d3Szrj 	    gold_error(_("__build_id=uuid failed: call UuidCreate() failed"));
3464*fae548d3Szrj 	  FreeLibrary(rpc_library);
3465*fae548d3Szrj 	}
3466*fae548d3Szrj       desc.assign(reinterpret_cast<const char *>(&uuid), sizeof(UUID));
3467*fae548d3Szrj       descsz = sizeof(UUID);
3468*fae548d3Szrj #endif // __MINGW32__
3469*fae548d3Szrj     }
3470*fae548d3Szrj   else if (strncmp(style, "0x", 2) == 0)
3471*fae548d3Szrj     {
3472*fae548d3Szrj       hex_init();
3473*fae548d3Szrj       const char* p = style + 2;
3474*fae548d3Szrj       while (*p != '\0')
3475*fae548d3Szrj 	{
3476*fae548d3Szrj 	  if (hex_p(p[0]) && hex_p(p[1]))
3477*fae548d3Szrj 	    {
3478*fae548d3Szrj 	      char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
3479*fae548d3Szrj 	      desc += c;
3480*fae548d3Szrj 	      p += 2;
3481*fae548d3Szrj 	    }
3482*fae548d3Szrj 	  else if (*p == '-' || *p == ':')
3483*fae548d3Szrj 	    ++p;
3484*fae548d3Szrj 	  else
3485*fae548d3Szrj 	    gold_fatal(_("--build-id argument '%s' not a valid hex number"),
3486*fae548d3Szrj 		       style);
3487*fae548d3Szrj 	}
3488*fae548d3Szrj       descsz = desc.size();
3489*fae548d3Szrj     }
3490*fae548d3Szrj   else
3491*fae548d3Szrj     gold_fatal(_("unrecognized --build-id argument '%s'"), style);
3492*fae548d3Szrj 
3493*fae548d3Szrj   // Create the note.
3494*fae548d3Szrj   size_t trailing_padding;
3495*fae548d3Szrj   Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
3496*fae548d3Szrj 					 ".note.gnu.build-id", descsz, true,
3497*fae548d3Szrj 					 &trailing_padding);
3498*fae548d3Szrj   if (os == NULL)
3499*fae548d3Szrj     return;
3500*fae548d3Szrj 
3501*fae548d3Szrj   if (!desc.empty())
3502*fae548d3Szrj     {
3503*fae548d3Szrj       // We know the value already, so we fill it in now.
3504*fae548d3Szrj       gold_assert(desc.size() == descsz);
3505*fae548d3Szrj 
3506*fae548d3Szrj       Output_section_data* posd = new Output_data_const(desc, 4);
3507*fae548d3Szrj       os->add_output_section_data(posd);
3508*fae548d3Szrj 
3509*fae548d3Szrj       if (trailing_padding != 0)
3510*fae548d3Szrj 	{
3511*fae548d3Szrj 	  posd = new Output_data_zero_fill(trailing_padding, 0);
3512*fae548d3Szrj 	  os->add_output_section_data(posd);
3513*fae548d3Szrj 	}
3514*fae548d3Szrj     }
3515*fae548d3Szrj   else
3516*fae548d3Szrj     {
3517*fae548d3Szrj       // We need to compute a checksum after we have completed the
3518*fae548d3Szrj       // link.
3519*fae548d3Szrj       gold_assert(trailing_padding == 0);
3520*fae548d3Szrj       this->build_id_note_ = new Output_data_zero_fill(descsz, 4);
3521*fae548d3Szrj       os->add_output_section_data(this->build_id_note_);
3522*fae548d3Szrj     }
3523*fae548d3Szrj }
3524*fae548d3Szrj 
3525*fae548d3Szrj // If we have both .stabXX and .stabXXstr sections, then the sh_link
3526*fae548d3Szrj // field of the former should point to the latter.  I'm not sure who
3527*fae548d3Szrj // started this, but the GNU linker does it, and some tools depend
3528*fae548d3Szrj // upon it.
3529*fae548d3Szrj 
3530*fae548d3Szrj void
link_stabs_sections()3531*fae548d3Szrj Layout::link_stabs_sections()
3532*fae548d3Szrj {
3533*fae548d3Szrj   if (!this->have_stabstr_section_)
3534*fae548d3Szrj     return;
3535*fae548d3Szrj 
3536*fae548d3Szrj   for (Section_list::iterator p = this->section_list_.begin();
3537*fae548d3Szrj        p != this->section_list_.end();
3538*fae548d3Szrj        ++p)
3539*fae548d3Szrj     {
3540*fae548d3Szrj       if ((*p)->type() != elfcpp::SHT_STRTAB)
3541*fae548d3Szrj 	continue;
3542*fae548d3Szrj 
3543*fae548d3Szrj       const char* name = (*p)->name();
3544*fae548d3Szrj       if (strncmp(name, ".stab", 5) != 0)
3545*fae548d3Szrj 	continue;
3546*fae548d3Szrj 
3547*fae548d3Szrj       size_t len = strlen(name);
3548*fae548d3Szrj       if (strcmp(name + len - 3, "str") != 0)
3549*fae548d3Szrj 	continue;
3550*fae548d3Szrj 
3551*fae548d3Szrj       std::string stab_name(name, len - 3);
3552*fae548d3Szrj       Output_section* stab_sec;
3553*fae548d3Szrj       stab_sec = this->find_output_section(stab_name.c_str());
3554*fae548d3Szrj       if (stab_sec != NULL)
3555*fae548d3Szrj 	stab_sec->set_link_section(*p);
3556*fae548d3Szrj     }
3557*fae548d3Szrj }
3558*fae548d3Szrj 
3559*fae548d3Szrj // Create .gnu_incremental_inputs and related sections needed
3560*fae548d3Szrj // for the next run of incremental linking to check what has changed.
3561*fae548d3Szrj 
3562*fae548d3Szrj void
create_incremental_info_sections(Symbol_table * symtab)3563*fae548d3Szrj Layout::create_incremental_info_sections(Symbol_table* symtab)
3564*fae548d3Szrj {
3565*fae548d3Szrj   Incremental_inputs* incr = this->incremental_inputs_;
3566*fae548d3Szrj 
3567*fae548d3Szrj   gold_assert(incr != NULL);
3568*fae548d3Szrj 
3569*fae548d3Szrj   // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
3570*fae548d3Szrj   incr->create_data_sections(symtab);
3571*fae548d3Szrj 
3572*fae548d3Szrj   // Add the .gnu_incremental_inputs section.
3573*fae548d3Szrj   const char* incremental_inputs_name =
3574*fae548d3Szrj     this->namepool_.add(".gnu_incremental_inputs", false, NULL);
3575*fae548d3Szrj   Output_section* incremental_inputs_os =
3576*fae548d3Szrj     this->make_output_section(incremental_inputs_name,
3577*fae548d3Szrj 			      elfcpp::SHT_GNU_INCREMENTAL_INPUTS, 0,
3578*fae548d3Szrj 			      ORDER_INVALID, false);
3579*fae548d3Szrj   incremental_inputs_os->add_output_section_data(incr->inputs_section());
3580*fae548d3Szrj 
3581*fae548d3Szrj   // Add the .gnu_incremental_symtab section.
3582*fae548d3Szrj   const char* incremental_symtab_name =
3583*fae548d3Szrj     this->namepool_.add(".gnu_incremental_symtab", false, NULL);
3584*fae548d3Szrj   Output_section* incremental_symtab_os =
3585*fae548d3Szrj     this->make_output_section(incremental_symtab_name,
3586*fae548d3Szrj 			      elfcpp::SHT_GNU_INCREMENTAL_SYMTAB, 0,
3587*fae548d3Szrj 			      ORDER_INVALID, false);
3588*fae548d3Szrj   incremental_symtab_os->add_output_section_data(incr->symtab_section());
3589*fae548d3Szrj   incremental_symtab_os->set_entsize(4);
3590*fae548d3Szrj 
3591*fae548d3Szrj   // Add the .gnu_incremental_relocs section.
3592*fae548d3Szrj   const char* incremental_relocs_name =
3593*fae548d3Szrj     this->namepool_.add(".gnu_incremental_relocs", false, NULL);
3594*fae548d3Szrj   Output_section* incremental_relocs_os =
3595*fae548d3Szrj     this->make_output_section(incremental_relocs_name,
3596*fae548d3Szrj 			      elfcpp::SHT_GNU_INCREMENTAL_RELOCS, 0,
3597*fae548d3Szrj 			      ORDER_INVALID, false);
3598*fae548d3Szrj   incremental_relocs_os->add_output_section_data(incr->relocs_section());
3599*fae548d3Szrj   incremental_relocs_os->set_entsize(incr->relocs_entsize());
3600*fae548d3Szrj 
3601*fae548d3Szrj   // Add the .gnu_incremental_got_plt section.
3602*fae548d3Szrj   const char* incremental_got_plt_name =
3603*fae548d3Szrj     this->namepool_.add(".gnu_incremental_got_plt", false, NULL);
3604*fae548d3Szrj   Output_section* incremental_got_plt_os =
3605*fae548d3Szrj     this->make_output_section(incremental_got_plt_name,
3606*fae548d3Szrj 			      elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT, 0,
3607*fae548d3Szrj 			      ORDER_INVALID, false);
3608*fae548d3Szrj   incremental_got_plt_os->add_output_section_data(incr->got_plt_section());
3609*fae548d3Szrj 
3610*fae548d3Szrj   // Add the .gnu_incremental_strtab section.
3611*fae548d3Szrj   const char* incremental_strtab_name =
3612*fae548d3Szrj     this->namepool_.add(".gnu_incremental_strtab", false, NULL);
3613*fae548d3Szrj   Output_section* incremental_strtab_os = this->make_output_section(incremental_strtab_name,
3614*fae548d3Szrj 							elfcpp::SHT_STRTAB, 0,
3615*fae548d3Szrj 							ORDER_INVALID, false);
3616*fae548d3Szrj   Output_data_strtab* strtab_data =
3617*fae548d3Szrj       new Output_data_strtab(incr->get_stringpool());
3618*fae548d3Szrj   incremental_strtab_os->add_output_section_data(strtab_data);
3619*fae548d3Szrj 
3620*fae548d3Szrj   incremental_inputs_os->set_after_input_sections();
3621*fae548d3Szrj   incremental_symtab_os->set_after_input_sections();
3622*fae548d3Szrj   incremental_relocs_os->set_after_input_sections();
3623*fae548d3Szrj   incremental_got_plt_os->set_after_input_sections();
3624*fae548d3Szrj 
3625*fae548d3Szrj   incremental_inputs_os->set_link_section(incremental_strtab_os);
3626*fae548d3Szrj   incremental_symtab_os->set_link_section(incremental_inputs_os);
3627*fae548d3Szrj   incremental_relocs_os->set_link_section(incremental_inputs_os);
3628*fae548d3Szrj   incremental_got_plt_os->set_link_section(incremental_inputs_os);
3629*fae548d3Szrj }
3630*fae548d3Szrj 
3631*fae548d3Szrj // Return whether SEG1 should be before SEG2 in the output file.  This
3632*fae548d3Szrj // is based entirely on the segment type and flags.  When this is
3633*fae548d3Szrj // called the segment addresses have normally not yet been set.
3634*fae548d3Szrj 
3635*fae548d3Szrj bool
segment_precedes(const Output_segment * seg1,const Output_segment * seg2)3636*fae548d3Szrj Layout::segment_precedes(const Output_segment* seg1,
3637*fae548d3Szrj 			 const Output_segment* seg2)
3638*fae548d3Szrj {
3639*fae548d3Szrj   // In order to produce a stable ordering if we're called with the same pointer
3640*fae548d3Szrj   // return false.
3641*fae548d3Szrj   if (seg1 == seg2)
3642*fae548d3Szrj     return false;
3643*fae548d3Szrj 
3644*fae548d3Szrj   elfcpp::Elf_Word type1 = seg1->type();
3645*fae548d3Szrj   elfcpp::Elf_Word type2 = seg2->type();
3646*fae548d3Szrj 
3647*fae548d3Szrj   // The single PT_PHDR segment is required to precede any loadable
3648*fae548d3Szrj   // segment.  We simply make it always first.
3649*fae548d3Szrj   if (type1 == elfcpp::PT_PHDR)
3650*fae548d3Szrj     {
3651*fae548d3Szrj       gold_assert(type2 != elfcpp::PT_PHDR);
3652*fae548d3Szrj       return true;
3653*fae548d3Szrj     }
3654*fae548d3Szrj   if (type2 == elfcpp::PT_PHDR)
3655*fae548d3Szrj     return false;
3656*fae548d3Szrj 
3657*fae548d3Szrj   // The single PT_INTERP segment is required to precede any loadable
3658*fae548d3Szrj   // segment.  We simply make it always second.
3659*fae548d3Szrj   if (type1 == elfcpp::PT_INTERP)
3660*fae548d3Szrj     {
3661*fae548d3Szrj       gold_assert(type2 != elfcpp::PT_INTERP);
3662*fae548d3Szrj       return true;
3663*fae548d3Szrj     }
3664*fae548d3Szrj   if (type2 == elfcpp::PT_INTERP)
3665*fae548d3Szrj     return false;
3666*fae548d3Szrj 
3667*fae548d3Szrj   // We then put PT_LOAD segments before any other segments.
3668*fae548d3Szrj   if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
3669*fae548d3Szrj     return true;
3670*fae548d3Szrj   if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
3671*fae548d3Szrj     return false;
3672*fae548d3Szrj 
3673*fae548d3Szrj   // We put the PT_TLS segment last except for the PT_GNU_RELRO
3674*fae548d3Szrj   // segment, because that is where the dynamic linker expects to find
3675*fae548d3Szrj   // it (this is just for efficiency; other positions would also work
3676*fae548d3Szrj   // correctly).
3677*fae548d3Szrj   if (type1 == elfcpp::PT_TLS
3678*fae548d3Szrj       && type2 != elfcpp::PT_TLS
3679*fae548d3Szrj       && type2 != elfcpp::PT_GNU_RELRO)
3680*fae548d3Szrj     return false;
3681*fae548d3Szrj   if (type2 == elfcpp::PT_TLS
3682*fae548d3Szrj       && type1 != elfcpp::PT_TLS
3683*fae548d3Szrj       && type1 != elfcpp::PT_GNU_RELRO)
3684*fae548d3Szrj     return true;
3685*fae548d3Szrj 
3686*fae548d3Szrj   // We put the PT_GNU_RELRO segment last, because that is where the
3687*fae548d3Szrj   // dynamic linker expects to find it (as with PT_TLS, this is just
3688*fae548d3Szrj   // for efficiency).
3689*fae548d3Szrj   if (type1 == elfcpp::PT_GNU_RELRO && type2 != elfcpp::PT_GNU_RELRO)
3690*fae548d3Szrj     return false;
3691*fae548d3Szrj   if (type2 == elfcpp::PT_GNU_RELRO && type1 != elfcpp::PT_GNU_RELRO)
3692*fae548d3Szrj     return true;
3693*fae548d3Szrj 
3694*fae548d3Szrj   const elfcpp::Elf_Word flags1 = seg1->flags();
3695*fae548d3Szrj   const elfcpp::Elf_Word flags2 = seg2->flags();
3696*fae548d3Szrj 
3697*fae548d3Szrj   // The order of non-PT_LOAD segments is unimportant.  We simply sort
3698*fae548d3Szrj   // by the numeric segment type and flags values.  There should not
3699*fae548d3Szrj   // be more than one segment with the same type and flags, except
3700*fae548d3Szrj   // when a linker script specifies such.
3701*fae548d3Szrj   if (type1 != elfcpp::PT_LOAD)
3702*fae548d3Szrj     {
3703*fae548d3Szrj       if (type1 != type2)
3704*fae548d3Szrj 	return type1 < type2;
3705*fae548d3Szrj       gold_assert(flags1 != flags2
3706*fae548d3Szrj 		  || this->script_options_->saw_phdrs_clause());
3707*fae548d3Szrj       return flags1 < flags2;
3708*fae548d3Szrj     }
3709*fae548d3Szrj 
3710*fae548d3Szrj   // If the addresses are set already, sort by load address.
3711*fae548d3Szrj   if (seg1->are_addresses_set())
3712*fae548d3Szrj     {
3713*fae548d3Szrj       if (!seg2->are_addresses_set())
3714*fae548d3Szrj 	return true;
3715*fae548d3Szrj 
3716*fae548d3Szrj       unsigned int section_count1 = seg1->output_section_count();
3717*fae548d3Szrj       unsigned int section_count2 = seg2->output_section_count();
3718*fae548d3Szrj       if (section_count1 == 0 && section_count2 > 0)
3719*fae548d3Szrj 	return true;
3720*fae548d3Szrj       if (section_count1 > 0 && section_count2 == 0)
3721*fae548d3Szrj 	return false;
3722*fae548d3Szrj 
3723*fae548d3Szrj       uint64_t paddr1 =	(seg1->are_addresses_set()
3724*fae548d3Szrj 			 ? seg1->paddr()
3725*fae548d3Szrj 			 : seg1->first_section_load_address());
3726*fae548d3Szrj       uint64_t paddr2 =	(seg2->are_addresses_set()
3727*fae548d3Szrj 			 ? seg2->paddr()
3728*fae548d3Szrj 			 : seg2->first_section_load_address());
3729*fae548d3Szrj 
3730*fae548d3Szrj       if (paddr1 != paddr2)
3731*fae548d3Szrj 	return paddr1 < paddr2;
3732*fae548d3Szrj     }
3733*fae548d3Szrj   else if (seg2->are_addresses_set())
3734*fae548d3Szrj     return false;
3735*fae548d3Szrj 
3736*fae548d3Szrj   // A segment which holds large data comes after a segment which does
3737*fae548d3Szrj   // not hold large data.
3738*fae548d3Szrj   if (seg1->is_large_data_segment())
3739*fae548d3Szrj     {
3740*fae548d3Szrj       if (!seg2->is_large_data_segment())
3741*fae548d3Szrj 	return false;
3742*fae548d3Szrj     }
3743*fae548d3Szrj   else if (seg2->is_large_data_segment())
3744*fae548d3Szrj     return true;
3745*fae548d3Szrj 
3746*fae548d3Szrj   // Otherwise, we sort PT_LOAD segments based on the flags.  Readonly
3747*fae548d3Szrj   // segments come before writable segments.  Then writable segments
3748*fae548d3Szrj   // with data come before writable segments without data.  Then
3749*fae548d3Szrj   // executable segments come before non-executable segments.  Then
3750*fae548d3Szrj   // the unlikely case of a non-readable segment comes before the
3751*fae548d3Szrj   // normal case of a readable segment.  If there are multiple
3752*fae548d3Szrj   // segments with the same type and flags, we require that the
3753*fae548d3Szrj   // address be set, and we sort by virtual address and then physical
3754*fae548d3Szrj   // address.
3755*fae548d3Szrj   if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
3756*fae548d3Szrj     return (flags1 & elfcpp::PF_W) == 0;
3757*fae548d3Szrj   if ((flags1 & elfcpp::PF_W) != 0
3758*fae548d3Szrj       && seg1->has_any_data_sections() != seg2->has_any_data_sections())
3759*fae548d3Szrj     return seg1->has_any_data_sections();
3760*fae548d3Szrj   if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
3761*fae548d3Szrj     return (flags1 & elfcpp::PF_X) != 0;
3762*fae548d3Szrj   if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
3763*fae548d3Szrj     return (flags1 & elfcpp::PF_R) == 0;
3764*fae548d3Szrj 
3765*fae548d3Szrj   // We shouldn't get here--we shouldn't create segments which we
3766*fae548d3Szrj   // can't distinguish.  Unless of course we are using a weird linker
3767*fae548d3Szrj   // script or overlapping --section-start options.  We could also get
3768*fae548d3Szrj   // here if plugins want unique segments for subsets of sections.
3769*fae548d3Szrj   gold_assert(this->script_options_->saw_phdrs_clause()
3770*fae548d3Szrj 	      || parameters->options().any_section_start()
3771*fae548d3Szrj 	      || this->is_unique_segment_for_sections_specified()
3772*fae548d3Szrj 	      || parameters->options().text_unlikely_segment());
3773*fae548d3Szrj   return false;
3774*fae548d3Szrj }
3775*fae548d3Szrj 
3776*fae548d3Szrj // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
3777*fae548d3Szrj 
3778*fae548d3Szrj static off_t
align_file_offset(off_t off,uint64_t addr,uint64_t abi_pagesize)3779*fae548d3Szrj align_file_offset(off_t off, uint64_t addr, uint64_t abi_pagesize)
3780*fae548d3Szrj {
3781*fae548d3Szrj   uint64_t unsigned_off = off;
3782*fae548d3Szrj   uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
3783*fae548d3Szrj 			  | (addr & (abi_pagesize - 1)));
3784*fae548d3Szrj   if (aligned_off < unsigned_off)
3785*fae548d3Szrj     aligned_off += abi_pagesize;
3786*fae548d3Szrj   return aligned_off;
3787*fae548d3Szrj }
3788*fae548d3Szrj 
3789*fae548d3Szrj // On targets where the text segment contains only executable code,
3790*fae548d3Szrj // a non-executable segment is never the text segment.
3791*fae548d3Szrj 
3792*fae548d3Szrj static inline bool
is_text_segment(const Target * target,const Output_segment * seg)3793*fae548d3Szrj is_text_segment(const Target* target, const Output_segment* seg)
3794*fae548d3Szrj {
3795*fae548d3Szrj   elfcpp::Elf_Xword flags = seg->flags();
3796*fae548d3Szrj   if ((flags & elfcpp::PF_W) != 0)
3797*fae548d3Szrj     return false;
3798*fae548d3Szrj   if ((flags & elfcpp::PF_X) == 0)
3799*fae548d3Szrj     return !target->isolate_execinstr();
3800*fae548d3Szrj   return true;
3801*fae548d3Szrj }
3802*fae548d3Szrj 
3803*fae548d3Szrj // Set the file offsets of all the segments, and all the sections they
3804*fae548d3Szrj // contain.  They have all been created.  LOAD_SEG must be laid out
3805*fae548d3Szrj // first.  Return the offset of the data to follow.
3806*fae548d3Szrj 
3807*fae548d3Szrj off_t
set_segment_offsets(const Target * target,Output_segment * load_seg,unsigned int * pshndx)3808*fae548d3Szrj Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
3809*fae548d3Szrj 			    unsigned int* pshndx)
3810*fae548d3Szrj {
3811*fae548d3Szrj   // Sort them into the final order.  We use a stable sort so that we
3812*fae548d3Szrj   // don't randomize the order of indistinguishable segments created
3813*fae548d3Szrj   // by linker scripts.
3814*fae548d3Szrj   std::stable_sort(this->segment_list_.begin(), this->segment_list_.end(),
3815*fae548d3Szrj 		   Layout::Compare_segments(this));
3816*fae548d3Szrj 
3817*fae548d3Szrj   // Find the PT_LOAD segments, and set their addresses and offsets
3818*fae548d3Szrj   // and their section's addresses and offsets.
3819*fae548d3Szrj   uint64_t start_addr;
3820*fae548d3Szrj   if (parameters->options().user_set_Ttext())
3821*fae548d3Szrj     start_addr = parameters->options().Ttext();
3822*fae548d3Szrj   else if (parameters->options().output_is_position_independent())
3823*fae548d3Szrj     start_addr = 0;
3824*fae548d3Szrj   else
3825*fae548d3Szrj     start_addr = target->default_text_segment_address();
3826*fae548d3Szrj 
3827*fae548d3Szrj   uint64_t addr = start_addr;
3828*fae548d3Szrj   off_t off = 0;
3829*fae548d3Szrj 
3830*fae548d3Szrj   // If LOAD_SEG is NULL, then the file header and segment headers
3831*fae548d3Szrj   // will not be loadable.  But they still need to be at offset 0 in
3832*fae548d3Szrj   // the file.  Set their offsets now.
3833*fae548d3Szrj   if (load_seg == NULL)
3834*fae548d3Szrj     {
3835*fae548d3Szrj       for (Data_list::iterator p = this->special_output_list_.begin();
3836*fae548d3Szrj 	   p != this->special_output_list_.end();
3837*fae548d3Szrj 	   ++p)
3838*fae548d3Szrj 	{
3839*fae548d3Szrj 	  off = align_address(off, (*p)->addralign());
3840*fae548d3Szrj 	  (*p)->set_address_and_file_offset(0, off);
3841*fae548d3Szrj 	  off += (*p)->data_size();
3842*fae548d3Szrj 	}
3843*fae548d3Szrj     }
3844*fae548d3Szrj 
3845*fae548d3Szrj   unsigned int increase_relro = this->increase_relro_;
3846*fae548d3Szrj   if (this->script_options_->saw_sections_clause())
3847*fae548d3Szrj     increase_relro = 0;
3848*fae548d3Szrj 
3849*fae548d3Szrj   const bool check_sections = parameters->options().check_sections();
3850*fae548d3Szrj   Output_segment* last_load_segment = NULL;
3851*fae548d3Szrj 
3852*fae548d3Szrj   unsigned int shndx_begin = *pshndx;
3853*fae548d3Szrj   unsigned int shndx_load_seg = *pshndx;
3854*fae548d3Szrj 
3855*fae548d3Szrj   for (Segment_list::iterator p = this->segment_list_.begin();
3856*fae548d3Szrj        p != this->segment_list_.end();
3857*fae548d3Szrj        ++p)
3858*fae548d3Szrj     {
3859*fae548d3Szrj       if ((*p)->type() == elfcpp::PT_LOAD)
3860*fae548d3Szrj 	{
3861*fae548d3Szrj 	  if (target->isolate_execinstr())
3862*fae548d3Szrj 	    {
3863*fae548d3Szrj 	      // When we hit the segment that should contain the
3864*fae548d3Szrj 	      // file headers, reset the file offset so we place
3865*fae548d3Szrj 	      // it and subsequent segments appropriately.
3866*fae548d3Szrj 	      // We'll fix up the preceding segments below.
3867*fae548d3Szrj 	      if (load_seg == *p)
3868*fae548d3Szrj 		{
3869*fae548d3Szrj 		  if (off == 0)
3870*fae548d3Szrj 		    load_seg = NULL;
3871*fae548d3Szrj 		  else
3872*fae548d3Szrj 		    {
3873*fae548d3Szrj 		      off = 0;
3874*fae548d3Szrj 		      shndx_load_seg = *pshndx;
3875*fae548d3Szrj 		    }
3876*fae548d3Szrj 		}
3877*fae548d3Szrj 	    }
3878*fae548d3Szrj 	  else
3879*fae548d3Szrj 	    {
3880*fae548d3Szrj 	      // Verify that the file headers fall into the first segment.
3881*fae548d3Szrj 	      if (load_seg != NULL && load_seg != *p)
3882*fae548d3Szrj 		gold_unreachable();
3883*fae548d3Szrj 	      load_seg = NULL;
3884*fae548d3Szrj 	    }
3885*fae548d3Szrj 
3886*fae548d3Szrj 	  bool are_addresses_set = (*p)->are_addresses_set();
3887*fae548d3Szrj 	  if (are_addresses_set)
3888*fae548d3Szrj 	    {
3889*fae548d3Szrj 	      // When it comes to setting file offsets, we care about
3890*fae548d3Szrj 	      // the physical address.
3891*fae548d3Szrj 	      addr = (*p)->paddr();
3892*fae548d3Szrj 	    }
3893*fae548d3Szrj 	  else if (parameters->options().user_set_Ttext()
3894*fae548d3Szrj 		   && (parameters->options().omagic()
3895*fae548d3Szrj 		       || is_text_segment(target, *p)))
3896*fae548d3Szrj 	    {
3897*fae548d3Szrj 	      are_addresses_set = true;
3898*fae548d3Szrj 	    }
3899*fae548d3Szrj 	  else if (parameters->options().user_set_Trodata_segment()
3900*fae548d3Szrj 		   && ((*p)->flags() & (elfcpp::PF_W | elfcpp::PF_X)) == 0)
3901*fae548d3Szrj 	    {
3902*fae548d3Szrj 	      addr = parameters->options().Trodata_segment();
3903*fae548d3Szrj 	      are_addresses_set = true;
3904*fae548d3Szrj 	    }
3905*fae548d3Szrj 	  else if (parameters->options().user_set_Tdata()
3906*fae548d3Szrj 		   && ((*p)->flags() & elfcpp::PF_W) != 0
3907*fae548d3Szrj 		   && (!parameters->options().user_set_Tbss()
3908*fae548d3Szrj 		       || (*p)->has_any_data_sections()))
3909*fae548d3Szrj 	    {
3910*fae548d3Szrj 	      addr = parameters->options().Tdata();
3911*fae548d3Szrj 	      are_addresses_set = true;
3912*fae548d3Szrj 	    }
3913*fae548d3Szrj 	  else if (parameters->options().user_set_Tbss()
3914*fae548d3Szrj 		   && ((*p)->flags() & elfcpp::PF_W) != 0
3915*fae548d3Szrj 		   && !(*p)->has_any_data_sections())
3916*fae548d3Szrj 	    {
3917*fae548d3Szrj 	      addr = parameters->options().Tbss();
3918*fae548d3Szrj 	      are_addresses_set = true;
3919*fae548d3Szrj 	    }
3920*fae548d3Szrj 
3921*fae548d3Szrj 	  uint64_t orig_addr = addr;
3922*fae548d3Szrj 	  uint64_t orig_off = off;
3923*fae548d3Szrj 
3924*fae548d3Szrj 	  uint64_t aligned_addr = 0;
3925*fae548d3Szrj 	  uint64_t abi_pagesize = target->abi_pagesize();
3926*fae548d3Szrj 	  uint64_t common_pagesize = target->common_pagesize();
3927*fae548d3Szrj 
3928*fae548d3Szrj 	  if (!parameters->options().nmagic()
3929*fae548d3Szrj 	      && !parameters->options().omagic())
3930*fae548d3Szrj 	    (*p)->set_minimum_p_align(abi_pagesize);
3931*fae548d3Szrj 
3932*fae548d3Szrj 	  if (!are_addresses_set)
3933*fae548d3Szrj 	    {
3934*fae548d3Szrj 	      // Skip the address forward one page, maintaining the same
3935*fae548d3Szrj 	      // position within the page.  This lets us store both segments
3936*fae548d3Szrj 	      // overlapping on a single page in the file, but the loader will
3937*fae548d3Szrj 	      // put them on different pages in memory. We will revisit this
3938*fae548d3Szrj 	      // decision once we know the size of the segment.
3939*fae548d3Szrj 
3940*fae548d3Szrj 	      uint64_t max_align = (*p)->maximum_alignment();
3941*fae548d3Szrj 	      if (max_align > abi_pagesize)
3942*fae548d3Szrj 		addr = align_address(addr, max_align);
3943*fae548d3Szrj 	      aligned_addr = addr;
3944*fae548d3Szrj 
3945*fae548d3Szrj 	      if (load_seg == *p)
3946*fae548d3Szrj 		{
3947*fae548d3Szrj 		  // This is the segment that will contain the file
3948*fae548d3Szrj 		  // headers, so its offset will have to be exactly zero.
3949*fae548d3Szrj 		  gold_assert(orig_off == 0);
3950*fae548d3Szrj 
3951*fae548d3Szrj 		  // If the target wants a fixed minimum distance from the
3952*fae548d3Szrj 		  // text segment to the read-only segment, move up now.
3953*fae548d3Szrj 		  uint64_t min_addr =
3954*fae548d3Szrj 		    start_addr + (parameters->options().user_set_rosegment_gap()
3955*fae548d3Szrj 				  ? parameters->options().rosegment_gap()
3956*fae548d3Szrj 				  : target->rosegment_gap());
3957*fae548d3Szrj 		  if (addr < min_addr)
3958*fae548d3Szrj 		    addr = min_addr;
3959*fae548d3Szrj 
3960*fae548d3Szrj 		  // But this is not the first segment!  To make its
3961*fae548d3Szrj 		  // address congruent with its offset, that address better
3962*fae548d3Szrj 		  // be aligned to the ABI-mandated page size.
3963*fae548d3Szrj 		  addr = align_address(addr, abi_pagesize);
3964*fae548d3Szrj 		  aligned_addr = addr;
3965*fae548d3Szrj 		}
3966*fae548d3Szrj 	      else
3967*fae548d3Szrj 		{
3968*fae548d3Szrj 		  if ((addr & (abi_pagesize - 1)) != 0)
3969*fae548d3Szrj 		    addr = addr + abi_pagesize;
3970*fae548d3Szrj 
3971*fae548d3Szrj 		  off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
3972*fae548d3Szrj 		}
3973*fae548d3Szrj 	    }
3974*fae548d3Szrj 
3975*fae548d3Szrj 	  if (!parameters->options().nmagic()
3976*fae548d3Szrj 	      && !parameters->options().omagic())
3977*fae548d3Szrj 	    {
3978*fae548d3Szrj 	      // Here we are also taking care of the case when
3979*fae548d3Szrj 	      // the maximum segment alignment is larger than the page size.
3980*fae548d3Szrj 	      off = align_file_offset(off, addr,
3981*fae548d3Szrj 				      std::max(abi_pagesize,
3982*fae548d3Szrj 					       (*p)->maximum_alignment()));
3983*fae548d3Szrj 	    }
3984*fae548d3Szrj 	  else
3985*fae548d3Szrj 	    {
3986*fae548d3Szrj 	      // This is -N or -n with a section script which prevents
3987*fae548d3Szrj 	      // us from using a load segment.  We need to ensure that
3988*fae548d3Szrj 	      // the file offset is aligned to the alignment of the
3989*fae548d3Szrj 	      // segment.  This is because the linker script
3990*fae548d3Szrj 	      // implicitly assumed a zero offset.  If we don't align
3991*fae548d3Szrj 	      // here, then the alignment of the sections in the
3992*fae548d3Szrj 	      // linker script may not match the alignment of the
3993*fae548d3Szrj 	      // sections in the set_section_addresses call below,
3994*fae548d3Szrj 	      // causing an error about dot moving backward.
3995*fae548d3Szrj 	      off = align_address(off, (*p)->maximum_alignment());
3996*fae548d3Szrj 	    }
3997*fae548d3Szrj 
3998*fae548d3Szrj 	  unsigned int shndx_hold = *pshndx;
3999*fae548d3Szrj 	  bool has_relro = false;
4000*fae548d3Szrj 	  uint64_t new_addr = (*p)->set_section_addresses(target, this,
4001*fae548d3Szrj 							  false, addr,
4002*fae548d3Szrj 							  &increase_relro,
4003*fae548d3Szrj 							  &has_relro,
4004*fae548d3Szrj 							  &off, pshndx);
4005*fae548d3Szrj 
4006*fae548d3Szrj 	  // Now that we know the size of this segment, we may be able
4007*fae548d3Szrj 	  // to save a page in memory, at the cost of wasting some
4008*fae548d3Szrj 	  // file space, by instead aligning to the start of a new
4009*fae548d3Szrj 	  // page.  Here we use the real machine page size rather than
4010*fae548d3Szrj 	  // the ABI mandated page size.  If the segment has been
4011*fae548d3Szrj 	  // aligned so that the relro data ends at a page boundary,
4012*fae548d3Szrj 	  // we do not try to realign it.
4013*fae548d3Szrj 
4014*fae548d3Szrj 	  if (!are_addresses_set
4015*fae548d3Szrj 	      && !has_relro
4016*fae548d3Szrj 	      && aligned_addr != addr
4017*fae548d3Szrj 	      && !parameters->incremental())
4018*fae548d3Szrj 	    {
4019*fae548d3Szrj 	      uint64_t first_off = (common_pagesize
4020*fae548d3Szrj 				    - (aligned_addr
4021*fae548d3Szrj 				       & (common_pagesize - 1)));
4022*fae548d3Szrj 	      uint64_t last_off = new_addr & (common_pagesize - 1);
4023*fae548d3Szrj 	      if (first_off > 0
4024*fae548d3Szrj 		  && last_off > 0
4025*fae548d3Szrj 		  && ((aligned_addr & ~ (common_pagesize - 1))
4026*fae548d3Szrj 		      != (new_addr & ~ (common_pagesize - 1)))
4027*fae548d3Szrj 		  && first_off + last_off <= common_pagesize)
4028*fae548d3Szrj 		{
4029*fae548d3Szrj 		  *pshndx = shndx_hold;
4030*fae548d3Szrj 		  addr = align_address(aligned_addr, common_pagesize);
4031*fae548d3Szrj 		  addr = align_address(addr, (*p)->maximum_alignment());
4032*fae548d3Szrj 		  if ((addr & (abi_pagesize - 1)) != 0)
4033*fae548d3Szrj 		    addr = addr + abi_pagesize;
4034*fae548d3Szrj 		  off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
4035*fae548d3Szrj 		  off = align_file_offset(off, addr, abi_pagesize);
4036*fae548d3Szrj 
4037*fae548d3Szrj 		  increase_relro = this->increase_relro_;
4038*fae548d3Szrj 		  if (this->script_options_->saw_sections_clause())
4039*fae548d3Szrj 		    increase_relro = 0;
4040*fae548d3Szrj 		  has_relro = false;
4041*fae548d3Szrj 
4042*fae548d3Szrj 		  new_addr = (*p)->set_section_addresses(target, this,
4043*fae548d3Szrj 							 true, addr,
4044*fae548d3Szrj 							 &increase_relro,
4045*fae548d3Szrj 							 &has_relro,
4046*fae548d3Szrj 							 &off, pshndx);
4047*fae548d3Szrj 		}
4048*fae548d3Szrj 	    }
4049*fae548d3Szrj 
4050*fae548d3Szrj 	  addr = new_addr;
4051*fae548d3Szrj 
4052*fae548d3Szrj 	  // Implement --check-sections.  We know that the segments
4053*fae548d3Szrj 	  // are sorted by LMA.
4054*fae548d3Szrj 	  if (check_sections && last_load_segment != NULL)
4055*fae548d3Szrj 	    {
4056*fae548d3Szrj 	      gold_assert(last_load_segment->paddr() <= (*p)->paddr());
4057*fae548d3Szrj 	      if (last_load_segment->paddr() + last_load_segment->memsz()
4058*fae548d3Szrj 		  > (*p)->paddr())
4059*fae548d3Szrj 		{
4060*fae548d3Szrj 		  unsigned long long lb1 = last_load_segment->paddr();
4061*fae548d3Szrj 		  unsigned long long le1 = lb1 + last_load_segment->memsz();
4062*fae548d3Szrj 		  unsigned long long lb2 = (*p)->paddr();
4063*fae548d3Szrj 		  unsigned long long le2 = lb2 + (*p)->memsz();
4064*fae548d3Szrj 		  gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
4065*fae548d3Szrj 			       "[0x%llx -> 0x%llx]"),
4066*fae548d3Szrj 			     lb1, le1, lb2, le2);
4067*fae548d3Szrj 		}
4068*fae548d3Szrj 	    }
4069*fae548d3Szrj 	  last_load_segment = *p;
4070*fae548d3Szrj 	}
4071*fae548d3Szrj     }
4072*fae548d3Szrj 
4073*fae548d3Szrj   if (load_seg != NULL && target->isolate_execinstr())
4074*fae548d3Szrj     {
4075*fae548d3Szrj       // Process the early segments again, setting their file offsets
4076*fae548d3Szrj       // so they land after the segments starting at LOAD_SEG.
4077*fae548d3Szrj       off = align_file_offset(off, 0, target->abi_pagesize());
4078*fae548d3Szrj 
4079*fae548d3Szrj       this->reset_relax_output();
4080*fae548d3Szrj 
4081*fae548d3Szrj       for (Segment_list::iterator p = this->segment_list_.begin();
4082*fae548d3Szrj 	   *p != load_seg;
4083*fae548d3Szrj 	   ++p)
4084*fae548d3Szrj 	{
4085*fae548d3Szrj 	  if ((*p)->type() == elfcpp::PT_LOAD)
4086*fae548d3Szrj 	    {
4087*fae548d3Szrj 	      // We repeat the whole job of assigning addresses and
4088*fae548d3Szrj 	      // offsets, but we really only want to change the offsets and
4089*fae548d3Szrj 	      // must ensure that the addresses all come out the same as
4090*fae548d3Szrj 	      // they did the first time through.
4091*fae548d3Szrj 	      bool has_relro = false;
4092*fae548d3Szrj 	      const uint64_t old_addr = (*p)->vaddr();
4093*fae548d3Szrj 	      const uint64_t old_end = old_addr + (*p)->memsz();
4094*fae548d3Szrj 	      uint64_t new_addr = (*p)->set_section_addresses(target, this,
4095*fae548d3Szrj 							      true, old_addr,
4096*fae548d3Szrj 							      &increase_relro,
4097*fae548d3Szrj 							      &has_relro,
4098*fae548d3Szrj 							      &off,
4099*fae548d3Szrj 							      &shndx_begin);
4100*fae548d3Szrj 	      gold_assert(new_addr == old_end);
4101*fae548d3Szrj 	    }
4102*fae548d3Szrj 	}
4103*fae548d3Szrj 
4104*fae548d3Szrj       gold_assert(shndx_begin == shndx_load_seg);
4105*fae548d3Szrj     }
4106*fae548d3Szrj 
4107*fae548d3Szrj   // Handle the non-PT_LOAD segments, setting their offsets from their
4108*fae548d3Szrj   // section's offsets.
4109*fae548d3Szrj   for (Segment_list::iterator p = this->segment_list_.begin();
4110*fae548d3Szrj        p != this->segment_list_.end();
4111*fae548d3Szrj        ++p)
4112*fae548d3Szrj     {
4113*fae548d3Szrj       // PT_GNU_STACK was set up correctly when it was created.
4114*fae548d3Szrj       if ((*p)->type() != elfcpp::PT_LOAD
4115*fae548d3Szrj 	  && (*p)->type() != elfcpp::PT_GNU_STACK)
4116*fae548d3Szrj 	(*p)->set_offset((*p)->type() == elfcpp::PT_GNU_RELRO
4117*fae548d3Szrj 			 ? increase_relro
4118*fae548d3Szrj 			 : 0);
4119*fae548d3Szrj     }
4120*fae548d3Szrj 
4121*fae548d3Szrj   // Set the TLS offsets for each section in the PT_TLS segment.
4122*fae548d3Szrj   if (this->tls_segment_ != NULL)
4123*fae548d3Szrj     this->tls_segment_->set_tls_offsets();
4124*fae548d3Szrj 
4125*fae548d3Szrj   return off;
4126*fae548d3Szrj }
4127*fae548d3Szrj 
4128*fae548d3Szrj // Set the offsets of all the allocated sections when doing a
4129*fae548d3Szrj // relocatable link.  This does the same jobs as set_segment_offsets,
4130*fae548d3Szrj // only for a relocatable link.
4131*fae548d3Szrj 
4132*fae548d3Szrj off_t
set_relocatable_section_offsets(Output_data * file_header,unsigned int * pshndx)4133*fae548d3Szrj Layout::set_relocatable_section_offsets(Output_data* file_header,
4134*fae548d3Szrj 					unsigned int* pshndx)
4135*fae548d3Szrj {
4136*fae548d3Szrj   off_t off = 0;
4137*fae548d3Szrj 
4138*fae548d3Szrj   file_header->set_address_and_file_offset(0, 0);
4139*fae548d3Szrj   off += file_header->data_size();
4140*fae548d3Szrj 
4141*fae548d3Szrj   for (Section_list::iterator p = this->section_list_.begin();
4142*fae548d3Szrj        p != this->section_list_.end();
4143*fae548d3Szrj        ++p)
4144*fae548d3Szrj     {
4145*fae548d3Szrj       // We skip unallocated sections here, except that group sections
4146*fae548d3Szrj       // have to come first.
4147*fae548d3Szrj       if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
4148*fae548d3Szrj 	  && (*p)->type() != elfcpp::SHT_GROUP)
4149*fae548d3Szrj 	continue;
4150*fae548d3Szrj 
4151*fae548d3Szrj       off = align_address(off, (*p)->addralign());
4152*fae548d3Szrj 
4153*fae548d3Szrj       // The linker script might have set the address.
4154*fae548d3Szrj       if (!(*p)->is_address_valid())
4155*fae548d3Szrj 	(*p)->set_address(0);
4156*fae548d3Szrj       (*p)->set_file_offset(off);
4157*fae548d3Szrj       (*p)->finalize_data_size();
4158*fae548d3Szrj       if ((*p)->type() != elfcpp::SHT_NOBITS)
4159*fae548d3Szrj 	off += (*p)->data_size();
4160*fae548d3Szrj 
4161*fae548d3Szrj       (*p)->set_out_shndx(*pshndx);
4162*fae548d3Szrj       ++*pshndx;
4163*fae548d3Szrj     }
4164*fae548d3Szrj 
4165*fae548d3Szrj   return off;
4166*fae548d3Szrj }
4167*fae548d3Szrj 
4168*fae548d3Szrj // Set the file offset of all the sections not associated with a
4169*fae548d3Szrj // segment.
4170*fae548d3Szrj 
4171*fae548d3Szrj off_t
set_section_offsets(off_t off,Layout::Section_offset_pass pass)4172*fae548d3Szrj Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
4173*fae548d3Szrj {
4174*fae548d3Szrj   off_t startoff = off;
4175*fae548d3Szrj   off_t maxoff = off;
4176*fae548d3Szrj 
4177*fae548d3Szrj   for (Section_list::iterator p = this->unattached_section_list_.begin();
4178*fae548d3Szrj        p != this->unattached_section_list_.end();
4179*fae548d3Szrj        ++p)
4180*fae548d3Szrj     {
4181*fae548d3Szrj       // The symtab section is handled in create_symtab_sections.
4182*fae548d3Szrj       if (*p == this->symtab_section_)
4183*fae548d3Szrj 	continue;
4184*fae548d3Szrj 
4185*fae548d3Szrj       // If we've already set the data size, don't set it again.
4186*fae548d3Szrj       if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
4187*fae548d3Szrj 	continue;
4188*fae548d3Szrj 
4189*fae548d3Szrj       if (pass == BEFORE_INPUT_SECTIONS_PASS
4190*fae548d3Szrj 	  && (*p)->requires_postprocessing())
4191*fae548d3Szrj 	{
4192*fae548d3Szrj 	  (*p)->create_postprocessing_buffer();
4193*fae548d3Szrj 	  this->any_postprocessing_sections_ = true;
4194*fae548d3Szrj 	}
4195*fae548d3Szrj 
4196*fae548d3Szrj       if (pass == BEFORE_INPUT_SECTIONS_PASS
4197*fae548d3Szrj 	  && (*p)->after_input_sections())
4198*fae548d3Szrj 	continue;
4199*fae548d3Szrj       else if (pass == POSTPROCESSING_SECTIONS_PASS
4200*fae548d3Szrj 	       && (!(*p)->after_input_sections()
4201*fae548d3Szrj 		   || (*p)->type() == elfcpp::SHT_STRTAB))
4202*fae548d3Szrj 	continue;
4203*fae548d3Szrj       else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
4204*fae548d3Szrj 	       && (!(*p)->after_input_sections()
4205*fae548d3Szrj 		   || (*p)->type() != elfcpp::SHT_STRTAB))
4206*fae548d3Szrj 	continue;
4207*fae548d3Szrj 
4208*fae548d3Szrj       if (!parameters->incremental_update())
4209*fae548d3Szrj 	{
4210*fae548d3Szrj 	  off = align_address(off, (*p)->addralign());
4211*fae548d3Szrj 	  (*p)->set_file_offset(off);
4212*fae548d3Szrj 	  (*p)->finalize_data_size();
4213*fae548d3Szrj 	}
4214*fae548d3Szrj       else
4215*fae548d3Szrj 	{
4216*fae548d3Szrj 	  // Incremental update: allocate file space from free list.
4217*fae548d3Szrj 	  (*p)->pre_finalize_data_size();
4218*fae548d3Szrj 	  off_t current_size = (*p)->current_data_size();
4219*fae548d3Szrj 	  off = this->allocate(current_size, (*p)->addralign(), startoff);
4220*fae548d3Szrj 	  if (off == -1)
4221*fae548d3Szrj 	    {
4222*fae548d3Szrj 	      if (is_debugging_enabled(DEBUG_INCREMENTAL))
4223*fae548d3Szrj 		this->free_list_.dump();
4224*fae548d3Szrj 	      gold_assert((*p)->output_section() != NULL);
4225*fae548d3Szrj 	      gold_fallback(_("out of patch space for section %s; "
4226*fae548d3Szrj 			      "relink with --incremental-full"),
4227*fae548d3Szrj 			    (*p)->output_section()->name());
4228*fae548d3Szrj 	    }
4229*fae548d3Szrj 	  (*p)->set_file_offset(off);
4230*fae548d3Szrj 	  (*p)->finalize_data_size();
4231*fae548d3Szrj 	  if ((*p)->data_size() > current_size)
4232*fae548d3Szrj 	    {
4233*fae548d3Szrj 	      gold_assert((*p)->output_section() != NULL);
4234*fae548d3Szrj 	      gold_fallback(_("%s: section changed size; "
4235*fae548d3Szrj 			      "relink with --incremental-full"),
4236*fae548d3Szrj 			    (*p)->output_section()->name());
4237*fae548d3Szrj 	    }
4238*fae548d3Szrj 	  gold_debug(DEBUG_INCREMENTAL,
4239*fae548d3Szrj 		     "set_section_offsets: %08lx %08lx %s",
4240*fae548d3Szrj 		     static_cast<long>(off),
4241*fae548d3Szrj 		     static_cast<long>((*p)->data_size()),
4242*fae548d3Szrj 		     ((*p)->output_section() != NULL
4243*fae548d3Szrj 		      ? (*p)->output_section()->name() : "(special)"));
4244*fae548d3Szrj 	}
4245*fae548d3Szrj 
4246*fae548d3Szrj       off += (*p)->data_size();
4247*fae548d3Szrj       if (off > maxoff)
4248*fae548d3Szrj 	maxoff = off;
4249*fae548d3Szrj 
4250*fae548d3Szrj       // At this point the name must be set.
4251*fae548d3Szrj       if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
4252*fae548d3Szrj 	this->namepool_.add((*p)->name(), false, NULL);
4253*fae548d3Szrj     }
4254*fae548d3Szrj   return maxoff;
4255*fae548d3Szrj }
4256*fae548d3Szrj 
4257*fae548d3Szrj // Set the section indexes of all the sections not associated with a
4258*fae548d3Szrj // segment.
4259*fae548d3Szrj 
4260*fae548d3Szrj unsigned int
set_section_indexes(unsigned int shndx)4261*fae548d3Szrj Layout::set_section_indexes(unsigned int shndx)
4262*fae548d3Szrj {
4263*fae548d3Szrj   for (Section_list::iterator p = this->unattached_section_list_.begin();
4264*fae548d3Szrj        p != this->unattached_section_list_.end();
4265*fae548d3Szrj        ++p)
4266*fae548d3Szrj     {
4267*fae548d3Szrj       if (!(*p)->has_out_shndx())
4268*fae548d3Szrj 	{
4269*fae548d3Szrj 	  (*p)->set_out_shndx(shndx);
4270*fae548d3Szrj 	  ++shndx;
4271*fae548d3Szrj 	}
4272*fae548d3Szrj     }
4273*fae548d3Szrj   return shndx;
4274*fae548d3Szrj }
4275*fae548d3Szrj 
4276*fae548d3Szrj // Set the section addresses according to the linker script.  This is
4277*fae548d3Szrj // only called when we see a SECTIONS clause.  This returns the
4278*fae548d3Szrj // program segment which should hold the file header and segment
4279*fae548d3Szrj // headers, if any.  It will return NULL if they should not be in a
4280*fae548d3Szrj // segment.
4281*fae548d3Szrj 
4282*fae548d3Szrj Output_segment*
set_section_addresses_from_script(Symbol_table * symtab)4283*fae548d3Szrj Layout::set_section_addresses_from_script(Symbol_table* symtab)
4284*fae548d3Szrj {
4285*fae548d3Szrj   Script_sections* ss = this->script_options_->script_sections();
4286*fae548d3Szrj   gold_assert(ss->saw_sections_clause());
4287*fae548d3Szrj   return this->script_options_->set_section_addresses(symtab, this);
4288*fae548d3Szrj }
4289*fae548d3Szrj 
4290*fae548d3Szrj // Place the orphan sections in the linker script.
4291*fae548d3Szrj 
4292*fae548d3Szrj void
place_orphan_sections_in_script()4293*fae548d3Szrj Layout::place_orphan_sections_in_script()
4294*fae548d3Szrj {
4295*fae548d3Szrj   Script_sections* ss = this->script_options_->script_sections();
4296*fae548d3Szrj   gold_assert(ss->saw_sections_clause());
4297*fae548d3Szrj 
4298*fae548d3Szrj   // Place each orphaned output section in the script.
4299*fae548d3Szrj   for (Section_list::iterator p = this->section_list_.begin();
4300*fae548d3Szrj        p != this->section_list_.end();
4301*fae548d3Szrj        ++p)
4302*fae548d3Szrj     {
4303*fae548d3Szrj       if (!(*p)->found_in_sections_clause())
4304*fae548d3Szrj 	ss->place_orphan(*p);
4305*fae548d3Szrj     }
4306*fae548d3Szrj }
4307*fae548d3Szrj 
4308*fae548d3Szrj // Count the local symbols in the regular symbol table and the dynamic
4309*fae548d3Szrj // symbol table, and build the respective string pools.
4310*fae548d3Szrj 
4311*fae548d3Szrj void
count_local_symbols(const Task * task,const Input_objects * input_objects)4312*fae548d3Szrj Layout::count_local_symbols(const Task* task,
4313*fae548d3Szrj 			    const Input_objects* input_objects)
4314*fae548d3Szrj {
4315*fae548d3Szrj   // First, figure out an upper bound on the number of symbols we'll
4316*fae548d3Szrj   // be inserting into each pool.  This helps us create the pools with
4317*fae548d3Szrj   // the right size, to avoid unnecessary hashtable resizing.
4318*fae548d3Szrj   unsigned int symbol_count = 0;
4319*fae548d3Szrj   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4320*fae548d3Szrj        p != input_objects->relobj_end();
4321*fae548d3Szrj        ++p)
4322*fae548d3Szrj     symbol_count += (*p)->local_symbol_count();
4323*fae548d3Szrj 
4324*fae548d3Szrj   // Go from "upper bound" to "estimate."  We overcount for two
4325*fae548d3Szrj   // reasons: we double-count symbols that occur in more than one
4326*fae548d3Szrj   // object file, and we count symbols that are dropped from the
4327*fae548d3Szrj   // output.  Add it all together and assume we overcount by 100%.
4328*fae548d3Szrj   symbol_count /= 2;
4329*fae548d3Szrj 
4330*fae548d3Szrj   // We assume all symbols will go into both the sympool and dynpool.
4331*fae548d3Szrj   this->sympool_.reserve(symbol_count);
4332*fae548d3Szrj   this->dynpool_.reserve(symbol_count);
4333*fae548d3Szrj 
4334*fae548d3Szrj   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4335*fae548d3Szrj        p != input_objects->relobj_end();
4336*fae548d3Szrj        ++p)
4337*fae548d3Szrj     {
4338*fae548d3Szrj       Task_lock_obj<Object> tlo(task, *p);
4339*fae548d3Szrj       (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
4340*fae548d3Szrj     }
4341*fae548d3Szrj }
4342*fae548d3Szrj 
4343*fae548d3Szrj // Create the symbol table sections.  Here we also set the final
4344*fae548d3Szrj // values of the symbols.  At this point all the loadable sections are
4345*fae548d3Szrj // fully laid out.  SHNUM is the number of sections so far.
4346*fae548d3Szrj 
4347*fae548d3Szrj void
create_symtab_sections(const Input_objects * input_objects,Symbol_table * symtab,unsigned int shnum,off_t * poff,unsigned int local_dynamic_count)4348*fae548d3Szrj Layout::create_symtab_sections(const Input_objects* input_objects,
4349*fae548d3Szrj 			       Symbol_table* symtab,
4350*fae548d3Szrj 			       unsigned int shnum,
4351*fae548d3Szrj 			       off_t* poff,
4352*fae548d3Szrj 			       unsigned int local_dynamic_count)
4353*fae548d3Szrj {
4354*fae548d3Szrj   int symsize;
4355*fae548d3Szrj   unsigned int align;
4356*fae548d3Szrj   if (parameters->target().get_size() == 32)
4357*fae548d3Szrj     {
4358*fae548d3Szrj       symsize = elfcpp::Elf_sizes<32>::sym_size;
4359*fae548d3Szrj       align = 4;
4360*fae548d3Szrj     }
4361*fae548d3Szrj   else if (parameters->target().get_size() == 64)
4362*fae548d3Szrj     {
4363*fae548d3Szrj       symsize = elfcpp::Elf_sizes<64>::sym_size;
4364*fae548d3Szrj       align = 8;
4365*fae548d3Szrj     }
4366*fae548d3Szrj   else
4367*fae548d3Szrj     gold_unreachable();
4368*fae548d3Szrj 
4369*fae548d3Szrj   // Compute file offsets relative to the start of the symtab section.
4370*fae548d3Szrj   off_t off = 0;
4371*fae548d3Szrj 
4372*fae548d3Szrj   // Save space for the dummy symbol at the start of the section.  We
4373*fae548d3Szrj   // never bother to write this out--it will just be left as zero.
4374*fae548d3Szrj   off += symsize;
4375*fae548d3Szrj   unsigned int local_symbol_index = 1;
4376*fae548d3Szrj 
4377*fae548d3Szrj   // Add STT_SECTION symbols for each Output section which needs one.
4378*fae548d3Szrj   for (Section_list::iterator p = this->section_list_.begin();
4379*fae548d3Szrj        p != this->section_list_.end();
4380*fae548d3Szrj        ++p)
4381*fae548d3Szrj     {
4382*fae548d3Szrj       if (!(*p)->needs_symtab_index())
4383*fae548d3Szrj 	(*p)->set_symtab_index(-1U);
4384*fae548d3Szrj       else
4385*fae548d3Szrj 	{
4386*fae548d3Szrj 	  (*p)->set_symtab_index(local_symbol_index);
4387*fae548d3Szrj 	  ++local_symbol_index;
4388*fae548d3Szrj 	  off += symsize;
4389*fae548d3Szrj 	}
4390*fae548d3Szrj     }
4391*fae548d3Szrj 
4392*fae548d3Szrj   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4393*fae548d3Szrj        p != input_objects->relobj_end();
4394*fae548d3Szrj        ++p)
4395*fae548d3Szrj     {
4396*fae548d3Szrj       unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
4397*fae548d3Szrj 							off, symtab);
4398*fae548d3Szrj       off += (index - local_symbol_index) * symsize;
4399*fae548d3Szrj       local_symbol_index = index;
4400*fae548d3Szrj     }
4401*fae548d3Szrj 
4402*fae548d3Szrj   unsigned int local_symcount = local_symbol_index;
4403*fae548d3Szrj   gold_assert(static_cast<off_t>(local_symcount * symsize) == off);
4404*fae548d3Szrj 
4405*fae548d3Szrj   off_t dynoff;
4406*fae548d3Szrj   size_t dyncount;
4407*fae548d3Szrj   if (this->dynsym_section_ == NULL)
4408*fae548d3Szrj     {
4409*fae548d3Szrj       dynoff = 0;
4410*fae548d3Szrj       dyncount = 0;
4411*fae548d3Szrj     }
4412*fae548d3Szrj   else
4413*fae548d3Szrj     {
4414*fae548d3Szrj       off_t locsize = local_dynamic_count * this->dynsym_section_->entsize();
4415*fae548d3Szrj       dynoff = this->dynsym_section_->offset() + locsize;
4416*fae548d3Szrj       dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
4417*fae548d3Szrj       gold_assert(static_cast<off_t>(dyncount * symsize)
4418*fae548d3Szrj 		  == this->dynsym_section_->data_size() - locsize);
4419*fae548d3Szrj     }
4420*fae548d3Szrj 
4421*fae548d3Szrj   off_t global_off = off;
4422*fae548d3Szrj   off = symtab->finalize(off, dynoff, local_dynamic_count, dyncount,
4423*fae548d3Szrj 			 &this->sympool_, &local_symcount);
4424*fae548d3Szrj 
4425*fae548d3Szrj   if (!parameters->options().strip_all())
4426*fae548d3Szrj     {
4427*fae548d3Szrj       this->sympool_.set_string_offsets();
4428*fae548d3Szrj 
4429*fae548d3Szrj       const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
4430*fae548d3Szrj       Output_section* osymtab = this->make_output_section(symtab_name,
4431*fae548d3Szrj 							  elfcpp::SHT_SYMTAB,
4432*fae548d3Szrj 							  0, ORDER_INVALID,
4433*fae548d3Szrj 							  false);
4434*fae548d3Szrj       this->symtab_section_ = osymtab;
4435*fae548d3Szrj 
4436*fae548d3Szrj       Output_section_data* pos = new Output_data_fixed_space(off, align,
4437*fae548d3Szrj 							     "** symtab");
4438*fae548d3Szrj       osymtab->add_output_section_data(pos);
4439*fae548d3Szrj 
4440*fae548d3Szrj       // We generate a .symtab_shndx section if we have more than
4441*fae548d3Szrj       // SHN_LORESERVE sections.  Technically it is possible that we
4442*fae548d3Szrj       // don't need one, because it is possible that there are no
4443*fae548d3Szrj       // symbols in any of sections with indexes larger than
4444*fae548d3Szrj       // SHN_LORESERVE.  That is probably unusual, though, and it is
4445*fae548d3Szrj       // easier to always create one than to compute section indexes
4446*fae548d3Szrj       // twice (once here, once when writing out the symbols).
4447*fae548d3Szrj       if (shnum >= elfcpp::SHN_LORESERVE)
4448*fae548d3Szrj 	{
4449*fae548d3Szrj 	  const char* symtab_xindex_name = this->namepool_.add(".symtab_shndx",
4450*fae548d3Szrj 							       false, NULL);
4451*fae548d3Szrj 	  Output_section* osymtab_xindex =
4452*fae548d3Szrj 	    this->make_output_section(symtab_xindex_name,
4453*fae548d3Szrj 				      elfcpp::SHT_SYMTAB_SHNDX, 0,
4454*fae548d3Szrj 				      ORDER_INVALID, false);
4455*fae548d3Szrj 
4456*fae548d3Szrj 	  size_t symcount = off / symsize;
4457*fae548d3Szrj 	  this->symtab_xindex_ = new Output_symtab_xindex(symcount);
4458*fae548d3Szrj 
4459*fae548d3Szrj 	  osymtab_xindex->add_output_section_data(this->symtab_xindex_);
4460*fae548d3Szrj 
4461*fae548d3Szrj 	  osymtab_xindex->set_link_section(osymtab);
4462*fae548d3Szrj 	  osymtab_xindex->set_addralign(4);
4463*fae548d3Szrj 	  osymtab_xindex->set_entsize(4);
4464*fae548d3Szrj 
4465*fae548d3Szrj 	  osymtab_xindex->set_after_input_sections();
4466*fae548d3Szrj 
4467*fae548d3Szrj 	  // This tells the driver code to wait until the symbol table
4468*fae548d3Szrj 	  // has written out before writing out the postprocessing
4469*fae548d3Szrj 	  // sections, including the .symtab_shndx section.
4470*fae548d3Szrj 	  this->any_postprocessing_sections_ = true;
4471*fae548d3Szrj 	}
4472*fae548d3Szrj 
4473*fae548d3Szrj       const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
4474*fae548d3Szrj       Output_section* ostrtab = this->make_output_section(strtab_name,
4475*fae548d3Szrj 							  elfcpp::SHT_STRTAB,
4476*fae548d3Szrj 							  0, ORDER_INVALID,
4477*fae548d3Szrj 							  false);
4478*fae548d3Szrj 
4479*fae548d3Szrj       Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
4480*fae548d3Szrj       ostrtab->add_output_section_data(pstr);
4481*fae548d3Szrj 
4482*fae548d3Szrj       off_t symtab_off;
4483*fae548d3Szrj       if (!parameters->incremental_update())
4484*fae548d3Szrj 	symtab_off = align_address(*poff, align);
4485*fae548d3Szrj       else
4486*fae548d3Szrj 	{
4487*fae548d3Szrj 	  symtab_off = this->allocate(off, align, *poff);
4488*fae548d3Szrj 	  if (off == -1)
4489*fae548d3Szrj 	    gold_fallback(_("out of patch space for symbol table; "
4490*fae548d3Szrj 			    "relink with --incremental-full"));
4491*fae548d3Szrj 	  gold_debug(DEBUG_INCREMENTAL,
4492*fae548d3Szrj 		     "create_symtab_sections: %08lx %08lx .symtab",
4493*fae548d3Szrj 		     static_cast<long>(symtab_off),
4494*fae548d3Szrj 		     static_cast<long>(off));
4495*fae548d3Szrj 	}
4496*fae548d3Szrj 
4497*fae548d3Szrj       symtab->set_file_offset(symtab_off + global_off);
4498*fae548d3Szrj       osymtab->set_file_offset(symtab_off);
4499*fae548d3Szrj       osymtab->finalize_data_size();
4500*fae548d3Szrj       osymtab->set_link_section(ostrtab);
4501*fae548d3Szrj       osymtab->set_info(local_symcount);
4502*fae548d3Szrj       osymtab->set_entsize(symsize);
4503*fae548d3Szrj 
4504*fae548d3Szrj       if (symtab_off + off > *poff)
4505*fae548d3Szrj 	*poff = symtab_off + off;
4506*fae548d3Szrj     }
4507*fae548d3Szrj }
4508*fae548d3Szrj 
4509*fae548d3Szrj // Create the .shstrtab section, which holds the names of the
4510*fae548d3Szrj // sections.  At the time this is called, we have created all the
4511*fae548d3Szrj // output sections except .shstrtab itself.
4512*fae548d3Szrj 
4513*fae548d3Szrj Output_section*
create_shstrtab()4514*fae548d3Szrj Layout::create_shstrtab()
4515*fae548d3Szrj {
4516*fae548d3Szrj   // FIXME: We don't need to create a .shstrtab section if we are
4517*fae548d3Szrj   // stripping everything.
4518*fae548d3Szrj 
4519*fae548d3Szrj   const char* name = this->namepool_.add(".shstrtab", false, NULL);
4520*fae548d3Szrj 
4521*fae548d3Szrj   Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0,
4522*fae548d3Szrj 						 ORDER_INVALID, false);
4523*fae548d3Szrj 
4524*fae548d3Szrj   if (strcmp(parameters->options().compress_debug_sections(), "none") != 0)
4525*fae548d3Szrj     {
4526*fae548d3Szrj       // We can't write out this section until we've set all the
4527*fae548d3Szrj       // section names, and we don't set the names of compressed
4528*fae548d3Szrj       // output sections until relocations are complete.  FIXME: With
4529*fae548d3Szrj       // the current names we use, this is unnecessary.
4530*fae548d3Szrj       os->set_after_input_sections();
4531*fae548d3Szrj     }
4532*fae548d3Szrj 
4533*fae548d3Szrj   Output_section_data* posd = new Output_data_strtab(&this->namepool_);
4534*fae548d3Szrj   os->add_output_section_data(posd);
4535*fae548d3Szrj 
4536*fae548d3Szrj   return os;
4537*fae548d3Szrj }
4538*fae548d3Szrj 
4539*fae548d3Szrj // Create the section headers.  SIZE is 32 or 64.  OFF is the file
4540*fae548d3Szrj // offset.
4541*fae548d3Szrj 
4542*fae548d3Szrj void
create_shdrs(const Output_section * shstrtab_section,off_t * poff)4543*fae548d3Szrj Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff)
4544*fae548d3Szrj {
4545*fae548d3Szrj   Output_section_headers* oshdrs;
4546*fae548d3Szrj   oshdrs = new Output_section_headers(this,
4547*fae548d3Szrj 				      &this->segment_list_,
4548*fae548d3Szrj 				      &this->section_list_,
4549*fae548d3Szrj 				      &this->unattached_section_list_,
4550*fae548d3Szrj 				      &this->namepool_,
4551*fae548d3Szrj 				      shstrtab_section);
4552*fae548d3Szrj   off_t off;
4553*fae548d3Szrj   if (!parameters->incremental_update())
4554*fae548d3Szrj     off = align_address(*poff, oshdrs->addralign());
4555*fae548d3Szrj   else
4556*fae548d3Szrj     {
4557*fae548d3Szrj       oshdrs->pre_finalize_data_size();
4558*fae548d3Szrj       off = this->allocate(oshdrs->data_size(), oshdrs->addralign(), *poff);
4559*fae548d3Szrj       if (off == -1)
4560*fae548d3Szrj 	  gold_fallback(_("out of patch space for section header table; "
4561*fae548d3Szrj 			  "relink with --incremental-full"));
4562*fae548d3Szrj       gold_debug(DEBUG_INCREMENTAL,
4563*fae548d3Szrj 		 "create_shdrs: %08lx %08lx (section header table)",
4564*fae548d3Szrj 		 static_cast<long>(off),
4565*fae548d3Szrj 		 static_cast<long>(off + oshdrs->data_size()));
4566*fae548d3Szrj     }
4567*fae548d3Szrj   oshdrs->set_address_and_file_offset(0, off);
4568*fae548d3Szrj   off += oshdrs->data_size();
4569*fae548d3Szrj   if (off > *poff)
4570*fae548d3Szrj     *poff = off;
4571*fae548d3Szrj   this->section_headers_ = oshdrs;
4572*fae548d3Szrj }
4573*fae548d3Szrj 
4574*fae548d3Szrj // Count the allocated sections.
4575*fae548d3Szrj 
4576*fae548d3Szrj size_t
allocated_output_section_count() const4577*fae548d3Szrj Layout::allocated_output_section_count() const
4578*fae548d3Szrj {
4579*fae548d3Szrj   size_t section_count = 0;
4580*fae548d3Szrj   for (Segment_list::const_iterator p = this->segment_list_.begin();
4581*fae548d3Szrj        p != this->segment_list_.end();
4582*fae548d3Szrj        ++p)
4583*fae548d3Szrj     section_count += (*p)->output_section_count();
4584*fae548d3Szrj   return section_count;
4585*fae548d3Szrj }
4586*fae548d3Szrj 
4587*fae548d3Szrj // Create the dynamic symbol table.
4588*fae548d3Szrj // *PLOCAL_DYNAMIC_COUNT will be set to the number of local symbols
4589*fae548d3Szrj // from input objects, and *PFORCED_LOCAL_DYNAMIC_COUNT will be set
4590*fae548d3Szrj // to the number of global symbols that have been forced local.
4591*fae548d3Szrj // We need to remember the former because the forced-local symbols are
4592*fae548d3Szrj // written along with the global symbols in Symtab::write_globals().
4593*fae548d3Szrj 
4594*fae548d3Szrj void
create_dynamic_symtab(const Input_objects * input_objects,Symbol_table * symtab,Output_section ** pdynstr,unsigned int * plocal_dynamic_count,unsigned int * pforced_local_dynamic_count,std::vector<Symbol * > * pdynamic_symbols,Versions * pversions)4595*fae548d3Szrj Layout::create_dynamic_symtab(const Input_objects* input_objects,
4596*fae548d3Szrj 			      Symbol_table* symtab,
4597*fae548d3Szrj 			      Output_section** pdynstr,
4598*fae548d3Szrj 			      unsigned int* plocal_dynamic_count,
4599*fae548d3Szrj 			      unsigned int* pforced_local_dynamic_count,
4600*fae548d3Szrj 			      std::vector<Symbol*>* pdynamic_symbols,
4601*fae548d3Szrj 			      Versions* pversions)
4602*fae548d3Szrj {
4603*fae548d3Szrj   // Count all the symbols in the dynamic symbol table, and set the
4604*fae548d3Szrj   // dynamic symbol indexes.
4605*fae548d3Szrj 
4606*fae548d3Szrj   // Skip symbol 0, which is always all zeroes.
4607*fae548d3Szrj   unsigned int index = 1;
4608*fae548d3Szrj 
4609*fae548d3Szrj   // Add STT_SECTION symbols for each Output section which needs one.
4610*fae548d3Szrj   for (Section_list::iterator p = this->section_list_.begin();
4611*fae548d3Szrj        p != this->section_list_.end();
4612*fae548d3Szrj        ++p)
4613*fae548d3Szrj     {
4614*fae548d3Szrj       if (!(*p)->needs_dynsym_index())
4615*fae548d3Szrj 	(*p)->set_dynsym_index(-1U);
4616*fae548d3Szrj       else
4617*fae548d3Szrj 	{
4618*fae548d3Szrj 	  (*p)->set_dynsym_index(index);
4619*fae548d3Szrj 	  ++index;
4620*fae548d3Szrj 	}
4621*fae548d3Szrj     }
4622*fae548d3Szrj 
4623*fae548d3Szrj   // Count the local symbols that need to go in the dynamic symbol table,
4624*fae548d3Szrj   // and set the dynamic symbol indexes.
4625*fae548d3Szrj   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4626*fae548d3Szrj        p != input_objects->relobj_end();
4627*fae548d3Szrj        ++p)
4628*fae548d3Szrj     {
4629*fae548d3Szrj       unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
4630*fae548d3Szrj       index = new_index;
4631*fae548d3Szrj     }
4632*fae548d3Szrj 
4633*fae548d3Szrj   unsigned int local_symcount = index;
4634*fae548d3Szrj   unsigned int forced_local_count = 0;
4635*fae548d3Szrj 
4636*fae548d3Szrj   index = symtab->set_dynsym_indexes(index, &forced_local_count,
4637*fae548d3Szrj 				     pdynamic_symbols, &this->dynpool_,
4638*fae548d3Szrj 				     pversions);
4639*fae548d3Szrj 
4640*fae548d3Szrj   *plocal_dynamic_count = local_symcount;
4641*fae548d3Szrj   *pforced_local_dynamic_count = forced_local_count;
4642*fae548d3Szrj 
4643*fae548d3Szrj   int symsize;
4644*fae548d3Szrj   unsigned int align;
4645*fae548d3Szrj   const int size = parameters->target().get_size();
4646*fae548d3Szrj   if (size == 32)
4647*fae548d3Szrj     {
4648*fae548d3Szrj       symsize = elfcpp::Elf_sizes<32>::sym_size;
4649*fae548d3Szrj       align = 4;
4650*fae548d3Szrj     }
4651*fae548d3Szrj   else if (size == 64)
4652*fae548d3Szrj     {
4653*fae548d3Szrj       symsize = elfcpp::Elf_sizes<64>::sym_size;
4654*fae548d3Szrj       align = 8;
4655*fae548d3Szrj     }
4656*fae548d3Szrj   else
4657*fae548d3Szrj     gold_unreachable();
4658*fae548d3Szrj 
4659*fae548d3Szrj   // Create the dynamic symbol table section.
4660*fae548d3Szrj 
4661*fae548d3Szrj   Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
4662*fae548d3Szrj 						       elfcpp::SHT_DYNSYM,
4663*fae548d3Szrj 						       elfcpp::SHF_ALLOC,
4664*fae548d3Szrj 						       false,
4665*fae548d3Szrj 						       ORDER_DYNAMIC_LINKER,
4666*fae548d3Szrj 						       false, false, false);
4667*fae548d3Szrj 
4668*fae548d3Szrj   // Check for NULL as a linker script may discard .dynsym.
4669*fae548d3Szrj   if (dynsym != NULL)
4670*fae548d3Szrj     {
4671*fae548d3Szrj       Output_section_data* odata = new Output_data_fixed_space(index * symsize,
4672*fae548d3Szrj 							       align,
4673*fae548d3Szrj 							       "** dynsym");
4674*fae548d3Szrj       dynsym->add_output_section_data(odata);
4675*fae548d3Szrj 
4676*fae548d3Szrj       dynsym->set_info(local_symcount + forced_local_count);
4677*fae548d3Szrj       dynsym->set_entsize(symsize);
4678*fae548d3Szrj       dynsym->set_addralign(align);
4679*fae548d3Szrj 
4680*fae548d3Szrj       this->dynsym_section_ = dynsym;
4681*fae548d3Szrj     }
4682*fae548d3Szrj 
4683*fae548d3Szrj   Output_data_dynamic* const odyn = this->dynamic_data_;
4684*fae548d3Szrj   if (odyn != NULL)
4685*fae548d3Szrj     {
4686*fae548d3Szrj       odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
4687*fae548d3Szrj       odyn->add_constant(elfcpp::DT_SYMENT, symsize);
4688*fae548d3Szrj     }
4689*fae548d3Szrj 
4690*fae548d3Szrj   // If there are more than SHN_LORESERVE allocated sections, we
4691*fae548d3Szrj   // create a .dynsym_shndx section.  It is possible that we don't
4692*fae548d3Szrj   // need one, because it is possible that there are no dynamic
4693*fae548d3Szrj   // symbols in any of the sections with indexes larger than
4694*fae548d3Szrj   // SHN_LORESERVE.  This is probably unusual, though, and at this
4695*fae548d3Szrj   // time we don't know the actual section indexes so it is
4696*fae548d3Szrj   // inconvenient to check.
4697*fae548d3Szrj   if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE)
4698*fae548d3Szrj     {
4699*fae548d3Szrj       Output_section* dynsym_xindex =
4700*fae548d3Szrj 	this->choose_output_section(NULL, ".dynsym_shndx",
4701*fae548d3Szrj 				    elfcpp::SHT_SYMTAB_SHNDX,
4702*fae548d3Szrj 				    elfcpp::SHF_ALLOC,
4703*fae548d3Szrj 				    false, ORDER_DYNAMIC_LINKER, false, false,
4704*fae548d3Szrj 				    false);
4705*fae548d3Szrj 
4706*fae548d3Szrj       if (dynsym_xindex != NULL)
4707*fae548d3Szrj 	{
4708*fae548d3Szrj 	  this->dynsym_xindex_ = new Output_symtab_xindex(index);
4709*fae548d3Szrj 
4710*fae548d3Szrj 	  dynsym_xindex->add_output_section_data(this->dynsym_xindex_);
4711*fae548d3Szrj 
4712*fae548d3Szrj 	  dynsym_xindex->set_link_section(dynsym);
4713*fae548d3Szrj 	  dynsym_xindex->set_addralign(4);
4714*fae548d3Szrj 	  dynsym_xindex->set_entsize(4);
4715*fae548d3Szrj 
4716*fae548d3Szrj 	  dynsym_xindex->set_after_input_sections();
4717*fae548d3Szrj 
4718*fae548d3Szrj 	  // This tells the driver code to wait until the symbol table
4719*fae548d3Szrj 	  // has written out before writing out the postprocessing
4720*fae548d3Szrj 	  // sections, including the .dynsym_shndx section.
4721*fae548d3Szrj 	  this->any_postprocessing_sections_ = true;
4722*fae548d3Szrj 	}
4723*fae548d3Szrj     }
4724*fae548d3Szrj 
4725*fae548d3Szrj   // Create the dynamic string table section.
4726*fae548d3Szrj 
4727*fae548d3Szrj   Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
4728*fae548d3Szrj 						       elfcpp::SHT_STRTAB,
4729*fae548d3Szrj 						       elfcpp::SHF_ALLOC,
4730*fae548d3Szrj 						       false,
4731*fae548d3Szrj 						       ORDER_DYNAMIC_LINKER,
4732*fae548d3Szrj 						       false, false, false);
4733*fae548d3Szrj   *pdynstr = dynstr;
4734*fae548d3Szrj   if (dynstr != NULL)
4735*fae548d3Szrj     {
4736*fae548d3Szrj       Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
4737*fae548d3Szrj       dynstr->add_output_section_data(strdata);
4738*fae548d3Szrj 
4739*fae548d3Szrj       if (dynsym != NULL)
4740*fae548d3Szrj 	dynsym->set_link_section(dynstr);
4741*fae548d3Szrj       if (this->dynamic_section_ != NULL)
4742*fae548d3Szrj 	this->dynamic_section_->set_link_section(dynstr);
4743*fae548d3Szrj 
4744*fae548d3Szrj       if (odyn != NULL)
4745*fae548d3Szrj 	{
4746*fae548d3Szrj 	  odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
4747*fae548d3Szrj 	  odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
4748*fae548d3Szrj 	}
4749*fae548d3Szrj     }
4750*fae548d3Szrj 
4751*fae548d3Szrj   // Create the hash tables.  The Gnu-style hash table must be
4752*fae548d3Szrj   // built first, because it changes the order of the symbols
4753*fae548d3Szrj   // in the dynamic symbol table.
4754*fae548d3Szrj 
4755*fae548d3Szrj   if (strcmp(parameters->options().hash_style(), "gnu") == 0
4756*fae548d3Szrj       || strcmp(parameters->options().hash_style(), "both") == 0)
4757*fae548d3Szrj     {
4758*fae548d3Szrj       unsigned char* phash;
4759*fae548d3Szrj       unsigned int hashlen;
4760*fae548d3Szrj       Dynobj::create_gnu_hash_table(*pdynamic_symbols,
4761*fae548d3Szrj 				    local_symcount + forced_local_count,
4762*fae548d3Szrj 				    &phash, &hashlen);
4763*fae548d3Szrj 
4764*fae548d3Szrj       Output_section* hashsec =
4765*fae548d3Szrj 	this->choose_output_section(NULL, ".gnu.hash", elfcpp::SHT_GNU_HASH,
4766*fae548d3Szrj 				    elfcpp::SHF_ALLOC, false,
4767*fae548d3Szrj 				    ORDER_DYNAMIC_LINKER, false, false,
4768*fae548d3Szrj 				    false);
4769*fae548d3Szrj 
4770*fae548d3Szrj       Output_section_data* hashdata = new Output_data_const_buffer(phash,
4771*fae548d3Szrj 								   hashlen,
4772*fae548d3Szrj 								   align,
4773*fae548d3Szrj 								   "** hash");
4774*fae548d3Szrj       if (hashsec != NULL && hashdata != NULL)
4775*fae548d3Szrj 	hashsec->add_output_section_data(hashdata);
4776*fae548d3Szrj 
4777*fae548d3Szrj       if (hashsec != NULL)
4778*fae548d3Szrj 	{
4779*fae548d3Szrj 	  if (dynsym != NULL)
4780*fae548d3Szrj 	    hashsec->set_link_section(dynsym);
4781*fae548d3Szrj 
4782*fae548d3Szrj 	  // For a 64-bit target, the entries in .gnu.hash do not have
4783*fae548d3Szrj 	  // a uniform size, so we only set the entry size for a
4784*fae548d3Szrj 	  // 32-bit target.
4785*fae548d3Szrj 	  if (parameters->target().get_size() == 32)
4786*fae548d3Szrj 	    hashsec->set_entsize(4);
4787*fae548d3Szrj 
4788*fae548d3Szrj 	  if (odyn != NULL)
4789*fae548d3Szrj 	    odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
4790*fae548d3Szrj 	}
4791*fae548d3Szrj     }
4792*fae548d3Szrj 
4793*fae548d3Szrj   if (strcmp(parameters->options().hash_style(), "sysv") == 0
4794*fae548d3Szrj       || strcmp(parameters->options().hash_style(), "both") == 0)
4795*fae548d3Szrj     {
4796*fae548d3Szrj       unsigned char* phash;
4797*fae548d3Szrj       unsigned int hashlen;
4798*fae548d3Szrj       Dynobj::create_elf_hash_table(*pdynamic_symbols,
4799*fae548d3Szrj 				    local_symcount + forced_local_count,
4800*fae548d3Szrj 				    &phash, &hashlen);
4801*fae548d3Szrj 
4802*fae548d3Szrj       Output_section* hashsec =
4803*fae548d3Szrj 	this->choose_output_section(NULL, ".hash", elfcpp::SHT_HASH,
4804*fae548d3Szrj 				    elfcpp::SHF_ALLOC, false,
4805*fae548d3Szrj 				    ORDER_DYNAMIC_LINKER, false, false,
4806*fae548d3Szrj 				    false);
4807*fae548d3Szrj 
4808*fae548d3Szrj       Output_section_data* hashdata = new Output_data_const_buffer(phash,
4809*fae548d3Szrj 								   hashlen,
4810*fae548d3Szrj 								   align,
4811*fae548d3Szrj 								   "** hash");
4812*fae548d3Szrj       if (hashsec != NULL && hashdata != NULL)
4813*fae548d3Szrj 	hashsec->add_output_section_data(hashdata);
4814*fae548d3Szrj 
4815*fae548d3Szrj       if (hashsec != NULL)
4816*fae548d3Szrj 	{
4817*fae548d3Szrj 	  if (dynsym != NULL)
4818*fae548d3Szrj 	    hashsec->set_link_section(dynsym);
4819*fae548d3Szrj 	  hashsec->set_entsize(parameters->target().hash_entry_size() / 8);
4820*fae548d3Szrj 	}
4821*fae548d3Szrj 
4822*fae548d3Szrj       if (odyn != NULL)
4823*fae548d3Szrj 	odyn->add_section_address(elfcpp::DT_HASH, hashsec);
4824*fae548d3Szrj     }
4825*fae548d3Szrj }
4826*fae548d3Szrj 
4827*fae548d3Szrj // Assign offsets to each local portion of the dynamic symbol table.
4828*fae548d3Szrj 
4829*fae548d3Szrj void
assign_local_dynsym_offsets(const Input_objects * input_objects)4830*fae548d3Szrj Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
4831*fae548d3Szrj {
4832*fae548d3Szrj   Output_section* dynsym = this->dynsym_section_;
4833*fae548d3Szrj   if (dynsym == NULL)
4834*fae548d3Szrj     return;
4835*fae548d3Szrj 
4836*fae548d3Szrj   off_t off = dynsym->offset();
4837*fae548d3Szrj 
4838*fae548d3Szrj   // Skip the dummy symbol at the start of the section.
4839*fae548d3Szrj   off += dynsym->entsize();
4840*fae548d3Szrj 
4841*fae548d3Szrj   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
4842*fae548d3Szrj        p != input_objects->relobj_end();
4843*fae548d3Szrj        ++p)
4844*fae548d3Szrj     {
4845*fae548d3Szrj       unsigned int count = (*p)->set_local_dynsym_offset(off);
4846*fae548d3Szrj       off += count * dynsym->entsize();
4847*fae548d3Szrj     }
4848*fae548d3Szrj }
4849*fae548d3Szrj 
4850*fae548d3Szrj // Create the version sections.
4851*fae548d3Szrj 
4852*fae548d3Szrj void
create_version_sections(const Versions * versions,const Symbol_table * symtab,unsigned int local_symcount,const std::vector<Symbol * > & dynamic_symbols,const Output_section * dynstr)4853*fae548d3Szrj Layout::create_version_sections(const Versions* versions,
4854*fae548d3Szrj 				const Symbol_table* symtab,
4855*fae548d3Szrj 				unsigned int local_symcount,
4856*fae548d3Szrj 				const std::vector<Symbol*>& dynamic_symbols,
4857*fae548d3Szrj 				const Output_section* dynstr)
4858*fae548d3Szrj {
4859*fae548d3Szrj   if (!versions->any_defs() && !versions->any_needs())
4860*fae548d3Szrj     return;
4861*fae548d3Szrj 
4862*fae548d3Szrj   switch (parameters->size_and_endianness())
4863*fae548d3Szrj     {
4864*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
4865*fae548d3Szrj     case Parameters::TARGET_32_LITTLE:
4866*fae548d3Szrj       this->sized_create_version_sections<32, false>(versions, symtab,
4867*fae548d3Szrj 						     local_symcount,
4868*fae548d3Szrj 						     dynamic_symbols, dynstr);
4869*fae548d3Szrj       break;
4870*fae548d3Szrj #endif
4871*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
4872*fae548d3Szrj     case Parameters::TARGET_32_BIG:
4873*fae548d3Szrj       this->sized_create_version_sections<32, true>(versions, symtab,
4874*fae548d3Szrj 						    local_symcount,
4875*fae548d3Szrj 						    dynamic_symbols, dynstr);
4876*fae548d3Szrj       break;
4877*fae548d3Szrj #endif
4878*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
4879*fae548d3Szrj     case Parameters::TARGET_64_LITTLE:
4880*fae548d3Szrj       this->sized_create_version_sections<64, false>(versions, symtab,
4881*fae548d3Szrj 						     local_symcount,
4882*fae548d3Szrj 						     dynamic_symbols, dynstr);
4883*fae548d3Szrj       break;
4884*fae548d3Szrj #endif
4885*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
4886*fae548d3Szrj     case Parameters::TARGET_64_BIG:
4887*fae548d3Szrj       this->sized_create_version_sections<64, true>(versions, symtab,
4888*fae548d3Szrj 						    local_symcount,
4889*fae548d3Szrj 						    dynamic_symbols, dynstr);
4890*fae548d3Szrj       break;
4891*fae548d3Szrj #endif
4892*fae548d3Szrj     default:
4893*fae548d3Szrj       gold_unreachable();
4894*fae548d3Szrj     }
4895*fae548d3Szrj }
4896*fae548d3Szrj 
4897*fae548d3Szrj // Create the version sections, sized version.
4898*fae548d3Szrj 
4899*fae548d3Szrj template<int size, bool big_endian>
4900*fae548d3Szrj void
sized_create_version_sections(const Versions * versions,const Symbol_table * symtab,unsigned int local_symcount,const std::vector<Symbol * > & dynamic_symbols,const Output_section * dynstr)4901*fae548d3Szrj Layout::sized_create_version_sections(
4902*fae548d3Szrj     const Versions* versions,
4903*fae548d3Szrj     const Symbol_table* symtab,
4904*fae548d3Szrj     unsigned int local_symcount,
4905*fae548d3Szrj     const std::vector<Symbol*>& dynamic_symbols,
4906*fae548d3Szrj     const Output_section* dynstr)
4907*fae548d3Szrj {
4908*fae548d3Szrj   Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
4909*fae548d3Szrj 						     elfcpp::SHT_GNU_versym,
4910*fae548d3Szrj 						     elfcpp::SHF_ALLOC,
4911*fae548d3Szrj 						     false,
4912*fae548d3Szrj 						     ORDER_DYNAMIC_LINKER,
4913*fae548d3Szrj 						     false, false, false);
4914*fae548d3Szrj 
4915*fae548d3Szrj   // Check for NULL since a linker script may discard this section.
4916*fae548d3Szrj   if (vsec != NULL)
4917*fae548d3Szrj     {
4918*fae548d3Szrj       unsigned char* vbuf;
4919*fae548d3Szrj       unsigned int vsize;
4920*fae548d3Szrj       versions->symbol_section_contents<size, big_endian>(symtab,
4921*fae548d3Szrj 							  &this->dynpool_,
4922*fae548d3Szrj 							  local_symcount,
4923*fae548d3Szrj 							  dynamic_symbols,
4924*fae548d3Szrj 							  &vbuf, &vsize);
4925*fae548d3Szrj 
4926*fae548d3Szrj       Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2,
4927*fae548d3Szrj 								"** versions");
4928*fae548d3Szrj 
4929*fae548d3Szrj       vsec->add_output_section_data(vdata);
4930*fae548d3Szrj       vsec->set_entsize(2);
4931*fae548d3Szrj       vsec->set_link_section(this->dynsym_section_);
4932*fae548d3Szrj     }
4933*fae548d3Szrj 
4934*fae548d3Szrj   Output_data_dynamic* const odyn = this->dynamic_data_;
4935*fae548d3Szrj   if (odyn != NULL && vsec != NULL)
4936*fae548d3Szrj     odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
4937*fae548d3Szrj 
4938*fae548d3Szrj   if (versions->any_defs())
4939*fae548d3Szrj     {
4940*fae548d3Szrj       Output_section* vdsec;
4941*fae548d3Szrj       vdsec = this->choose_output_section(NULL, ".gnu.version_d",
4942*fae548d3Szrj 					  elfcpp::SHT_GNU_verdef,
4943*fae548d3Szrj 					  elfcpp::SHF_ALLOC,
4944*fae548d3Szrj 					  false, ORDER_DYNAMIC_LINKER, false,
4945*fae548d3Szrj 					  false, false);
4946*fae548d3Szrj 
4947*fae548d3Szrj       if (vdsec != NULL)
4948*fae548d3Szrj 	{
4949*fae548d3Szrj 	  unsigned char* vdbuf;
4950*fae548d3Szrj 	  unsigned int vdsize;
4951*fae548d3Szrj 	  unsigned int vdentries;
4952*fae548d3Szrj 	  versions->def_section_contents<size, big_endian>(&this->dynpool_,
4953*fae548d3Szrj 							   &vdbuf, &vdsize,
4954*fae548d3Szrj 							   &vdentries);
4955*fae548d3Szrj 
4956*fae548d3Szrj 	  Output_section_data* vddata =
4957*fae548d3Szrj 	    new Output_data_const_buffer(vdbuf, vdsize, 4, "** version defs");
4958*fae548d3Szrj 
4959*fae548d3Szrj 	  vdsec->add_output_section_data(vddata);
4960*fae548d3Szrj 	  vdsec->set_link_section(dynstr);
4961*fae548d3Szrj 	  vdsec->set_info(vdentries);
4962*fae548d3Szrj 
4963*fae548d3Szrj 	  if (odyn != NULL)
4964*fae548d3Szrj 	    {
4965*fae548d3Szrj 	      odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
4966*fae548d3Szrj 	      odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
4967*fae548d3Szrj 	    }
4968*fae548d3Szrj 	}
4969*fae548d3Szrj     }
4970*fae548d3Szrj 
4971*fae548d3Szrj   if (versions->any_needs())
4972*fae548d3Szrj     {
4973*fae548d3Szrj       Output_section* vnsec;
4974*fae548d3Szrj       vnsec = this->choose_output_section(NULL, ".gnu.version_r",
4975*fae548d3Szrj 					  elfcpp::SHT_GNU_verneed,
4976*fae548d3Szrj 					  elfcpp::SHF_ALLOC,
4977*fae548d3Szrj 					  false, ORDER_DYNAMIC_LINKER, false,
4978*fae548d3Szrj 					  false, false);
4979*fae548d3Szrj 
4980*fae548d3Szrj       if (vnsec != NULL)
4981*fae548d3Szrj 	{
4982*fae548d3Szrj 	  unsigned char* vnbuf;
4983*fae548d3Szrj 	  unsigned int vnsize;
4984*fae548d3Szrj 	  unsigned int vnentries;
4985*fae548d3Szrj 	  versions->need_section_contents<size, big_endian>(&this->dynpool_,
4986*fae548d3Szrj 							    &vnbuf, &vnsize,
4987*fae548d3Szrj 							    &vnentries);
4988*fae548d3Szrj 
4989*fae548d3Szrj 	  Output_section_data* vndata =
4990*fae548d3Szrj 	    new Output_data_const_buffer(vnbuf, vnsize, 4, "** version refs");
4991*fae548d3Szrj 
4992*fae548d3Szrj 	  vnsec->add_output_section_data(vndata);
4993*fae548d3Szrj 	  vnsec->set_link_section(dynstr);
4994*fae548d3Szrj 	  vnsec->set_info(vnentries);
4995*fae548d3Szrj 
4996*fae548d3Szrj 	  if (odyn != NULL)
4997*fae548d3Szrj 	    {
4998*fae548d3Szrj 	      odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
4999*fae548d3Szrj 	      odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
5000*fae548d3Szrj 	    }
5001*fae548d3Szrj 	}
5002*fae548d3Szrj     }
5003*fae548d3Szrj }
5004*fae548d3Szrj 
5005*fae548d3Szrj // Create the .interp section and PT_INTERP segment.
5006*fae548d3Szrj 
5007*fae548d3Szrj void
create_interp(const Target * target)5008*fae548d3Szrj Layout::create_interp(const Target* target)
5009*fae548d3Szrj {
5010*fae548d3Szrj   gold_assert(this->interp_segment_ == NULL);
5011*fae548d3Szrj 
5012*fae548d3Szrj   const char* interp = parameters->options().dynamic_linker();
5013*fae548d3Szrj   if (interp == NULL)
5014*fae548d3Szrj     {
5015*fae548d3Szrj       interp = target->dynamic_linker();
5016*fae548d3Szrj       gold_assert(interp != NULL);
5017*fae548d3Szrj     }
5018*fae548d3Szrj 
5019*fae548d3Szrj   size_t len = strlen(interp) + 1;
5020*fae548d3Szrj 
5021*fae548d3Szrj   Output_section_data* odata = new Output_data_const(interp, len, 1);
5022*fae548d3Szrj 
5023*fae548d3Szrj   Output_section* osec = this->choose_output_section(NULL, ".interp",
5024*fae548d3Szrj 						     elfcpp::SHT_PROGBITS,
5025*fae548d3Szrj 						     elfcpp::SHF_ALLOC,
5026*fae548d3Szrj 						     false, ORDER_INTERP,
5027*fae548d3Szrj 						     false, false, false);
5028*fae548d3Szrj   if (osec != NULL)
5029*fae548d3Szrj     osec->add_output_section_data(odata);
5030*fae548d3Szrj }
5031*fae548d3Szrj 
5032*fae548d3Szrj // Add dynamic tags for the PLT and the dynamic relocs.  This is
5033*fae548d3Szrj // called by the target-specific code.  This does nothing if not doing
5034*fae548d3Szrj // a dynamic link.
5035*fae548d3Szrj 
5036*fae548d3Szrj // USE_REL is true for REL relocs rather than RELA relocs.
5037*fae548d3Szrj 
5038*fae548d3Szrj // If PLT_GOT is not NULL, then DT_PLTGOT points to it.
5039*fae548d3Szrj 
5040*fae548d3Szrj // If PLT_REL is not NULL, it is used for DT_PLTRELSZ, and DT_JMPREL,
5041*fae548d3Szrj // and we also set DT_PLTREL.  We use PLT_REL's output section, since
5042*fae548d3Szrj // some targets have multiple reloc sections in PLT_REL.
5043*fae548d3Szrj 
5044*fae548d3Szrj // If DYN_REL is not NULL, it is used for DT_REL/DT_RELA,
5045*fae548d3Szrj // DT_RELSZ/DT_RELASZ, DT_RELENT/DT_RELAENT.  Again we use the output
5046*fae548d3Szrj // section.
5047*fae548d3Szrj 
5048*fae548d3Szrj // If ADD_DEBUG is true, we add a DT_DEBUG entry when generating an
5049*fae548d3Szrj // executable.
5050*fae548d3Szrj 
5051*fae548d3Szrj void
add_target_dynamic_tags(bool use_rel,const Output_data * plt_got,const Output_data * plt_rel,const Output_data_reloc_generic * dyn_rel,bool add_debug,bool dynrel_includes_plt)5052*fae548d3Szrj Layout::add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
5053*fae548d3Szrj 				const Output_data* plt_rel,
5054*fae548d3Szrj 				const Output_data_reloc_generic* dyn_rel,
5055*fae548d3Szrj 				bool add_debug, bool dynrel_includes_plt)
5056*fae548d3Szrj {
5057*fae548d3Szrj   Output_data_dynamic* odyn = this->dynamic_data_;
5058*fae548d3Szrj   if (odyn == NULL)
5059*fae548d3Szrj     return;
5060*fae548d3Szrj 
5061*fae548d3Szrj   if (plt_got != NULL && plt_got->output_section() != NULL)
5062*fae548d3Szrj     odyn->add_section_address(elfcpp::DT_PLTGOT, plt_got);
5063*fae548d3Szrj 
5064*fae548d3Szrj   if (plt_rel != NULL && plt_rel->output_section() != NULL)
5065*fae548d3Szrj     {
5066*fae548d3Szrj       odyn->add_section_size(elfcpp::DT_PLTRELSZ, plt_rel->output_section());
5067*fae548d3Szrj       odyn->add_section_address(elfcpp::DT_JMPREL, plt_rel->output_section());
5068*fae548d3Szrj       odyn->add_constant(elfcpp::DT_PLTREL,
5069*fae548d3Szrj 			 use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA);
5070*fae548d3Szrj     }
5071*fae548d3Szrj 
5072*fae548d3Szrj   if ((dyn_rel != NULL && dyn_rel->output_section() != NULL)
5073*fae548d3Szrj       || (dynrel_includes_plt
5074*fae548d3Szrj 	  && plt_rel != NULL
5075*fae548d3Szrj 	  && plt_rel->output_section() != NULL))
5076*fae548d3Szrj     {
5077*fae548d3Szrj       bool have_dyn_rel = dyn_rel != NULL && dyn_rel->output_section() != NULL;
5078*fae548d3Szrj       bool have_plt_rel = plt_rel != NULL && plt_rel->output_section() != NULL;
5079*fae548d3Szrj       odyn->add_section_address(use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA,
5080*fae548d3Szrj 				(have_dyn_rel
5081*fae548d3Szrj 				 ? dyn_rel->output_section()
5082*fae548d3Szrj 				 : plt_rel->output_section()));
5083*fae548d3Szrj       elfcpp::DT size_tag = use_rel ? elfcpp::DT_RELSZ : elfcpp::DT_RELASZ;
5084*fae548d3Szrj       if (have_dyn_rel && have_plt_rel && dynrel_includes_plt)
5085*fae548d3Szrj 	odyn->add_section_size(size_tag,
5086*fae548d3Szrj 			       dyn_rel->output_section(),
5087*fae548d3Szrj 			       plt_rel->output_section());
5088*fae548d3Szrj       else if (have_dyn_rel)
5089*fae548d3Szrj 	odyn->add_section_size(size_tag, dyn_rel->output_section());
5090*fae548d3Szrj       else
5091*fae548d3Szrj 	odyn->add_section_size(size_tag, plt_rel->output_section());
5092*fae548d3Szrj       const int size = parameters->target().get_size();
5093*fae548d3Szrj       elfcpp::DT rel_tag;
5094*fae548d3Szrj       int rel_size;
5095*fae548d3Szrj       if (use_rel)
5096*fae548d3Szrj 	{
5097*fae548d3Szrj 	  rel_tag = elfcpp::DT_RELENT;
5098*fae548d3Szrj 	  if (size == 32)
5099*fae548d3Szrj 	    rel_size = Reloc_types<elfcpp::SHT_REL, 32, false>::reloc_size;
5100*fae548d3Szrj 	  else if (size == 64)
5101*fae548d3Szrj 	    rel_size = Reloc_types<elfcpp::SHT_REL, 64, false>::reloc_size;
5102*fae548d3Szrj 	  else
5103*fae548d3Szrj 	    gold_unreachable();
5104*fae548d3Szrj 	}
5105*fae548d3Szrj       else
5106*fae548d3Szrj 	{
5107*fae548d3Szrj 	  rel_tag = elfcpp::DT_RELAENT;
5108*fae548d3Szrj 	  if (size == 32)
5109*fae548d3Szrj 	    rel_size = Reloc_types<elfcpp::SHT_RELA, 32, false>::reloc_size;
5110*fae548d3Szrj 	  else if (size == 64)
5111*fae548d3Szrj 	    rel_size = Reloc_types<elfcpp::SHT_RELA, 64, false>::reloc_size;
5112*fae548d3Szrj 	  else
5113*fae548d3Szrj 	    gold_unreachable();
5114*fae548d3Szrj 	}
5115*fae548d3Szrj       odyn->add_constant(rel_tag, rel_size);
5116*fae548d3Szrj 
5117*fae548d3Szrj       if (parameters->options().combreloc() && have_dyn_rel)
5118*fae548d3Szrj 	{
5119*fae548d3Szrj 	  size_t c = dyn_rel->relative_reloc_count();
5120*fae548d3Szrj 	  if (c > 0)
5121*fae548d3Szrj 	    odyn->add_constant((use_rel
5122*fae548d3Szrj 				? elfcpp::DT_RELCOUNT
5123*fae548d3Szrj 				: elfcpp::DT_RELACOUNT),
5124*fae548d3Szrj 			       c);
5125*fae548d3Szrj 	}
5126*fae548d3Szrj     }
5127*fae548d3Szrj 
5128*fae548d3Szrj   if (add_debug && !parameters->options().shared())
5129*fae548d3Szrj     {
5130*fae548d3Szrj       // The value of the DT_DEBUG tag is filled in by the dynamic
5131*fae548d3Szrj       // linker at run time, and used by the debugger.
5132*fae548d3Szrj       odyn->add_constant(elfcpp::DT_DEBUG, 0);
5133*fae548d3Szrj     }
5134*fae548d3Szrj }
5135*fae548d3Szrj 
5136*fae548d3Szrj void
add_target_specific_dynamic_tag(elfcpp::DT tag,unsigned int val)5137*fae548d3Szrj Layout::add_target_specific_dynamic_tag(elfcpp::DT tag, unsigned int val)
5138*fae548d3Szrj {
5139*fae548d3Szrj   Output_data_dynamic* odyn = this->dynamic_data_;
5140*fae548d3Szrj   if (odyn == NULL)
5141*fae548d3Szrj     return;
5142*fae548d3Szrj   odyn->add_constant(tag, val);
5143*fae548d3Szrj }
5144*fae548d3Szrj 
5145*fae548d3Szrj // Finish the .dynamic section and PT_DYNAMIC segment.
5146*fae548d3Szrj 
5147*fae548d3Szrj void
finish_dynamic_section(const Input_objects * input_objects,const Symbol_table * symtab)5148*fae548d3Szrj Layout::finish_dynamic_section(const Input_objects* input_objects,
5149*fae548d3Szrj 			       const Symbol_table* symtab)
5150*fae548d3Szrj {
5151*fae548d3Szrj   if (!this->script_options_->saw_phdrs_clause()
5152*fae548d3Szrj       && this->dynamic_section_ != NULL)
5153*fae548d3Szrj     {
5154*fae548d3Szrj       Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
5155*fae548d3Szrj 						       (elfcpp::PF_R
5156*fae548d3Szrj 							| elfcpp::PF_W));
5157*fae548d3Szrj       oseg->add_output_section_to_nonload(this->dynamic_section_,
5158*fae548d3Szrj 					  elfcpp::PF_R | elfcpp::PF_W);
5159*fae548d3Szrj     }
5160*fae548d3Szrj 
5161*fae548d3Szrj   Output_data_dynamic* const odyn = this->dynamic_data_;
5162*fae548d3Szrj   if (odyn == NULL)
5163*fae548d3Szrj     return;
5164*fae548d3Szrj 
5165*fae548d3Szrj   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
5166*fae548d3Szrj        p != input_objects->dynobj_end();
5167*fae548d3Szrj        ++p)
5168*fae548d3Szrj     {
5169*fae548d3Szrj       if (!(*p)->is_needed() && (*p)->as_needed())
5170*fae548d3Szrj 	{
5171*fae548d3Szrj 	  // This dynamic object was linked with --as-needed, but it
5172*fae548d3Szrj 	  // is not needed.
5173*fae548d3Szrj 	  continue;
5174*fae548d3Szrj 	}
5175*fae548d3Szrj 
5176*fae548d3Szrj       odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
5177*fae548d3Szrj     }
5178*fae548d3Szrj 
5179*fae548d3Szrj   if (parameters->options().shared())
5180*fae548d3Szrj     {
5181*fae548d3Szrj       const char* soname = parameters->options().soname();
5182*fae548d3Szrj       if (soname != NULL)
5183*fae548d3Szrj 	odyn->add_string(elfcpp::DT_SONAME, soname);
5184*fae548d3Szrj     }
5185*fae548d3Szrj 
5186*fae548d3Szrj   Symbol* sym = symtab->lookup(parameters->options().init());
5187*fae548d3Szrj   if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
5188*fae548d3Szrj     odyn->add_symbol(elfcpp::DT_INIT, sym);
5189*fae548d3Szrj 
5190*fae548d3Szrj   sym = symtab->lookup(parameters->options().fini());
5191*fae548d3Szrj   if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
5192*fae548d3Szrj     odyn->add_symbol(elfcpp::DT_FINI, sym);
5193*fae548d3Szrj 
5194*fae548d3Szrj   // Look for .init_array, .preinit_array and .fini_array by checking
5195*fae548d3Szrj   // section types.
5196*fae548d3Szrj   for(Layout::Section_list::const_iterator p = this->section_list_.begin();
5197*fae548d3Szrj       p != this->section_list_.end();
5198*fae548d3Szrj       ++p)
5199*fae548d3Szrj     switch((*p)->type())
5200*fae548d3Szrj       {
5201*fae548d3Szrj       case elfcpp::SHT_FINI_ARRAY:
5202*fae548d3Szrj 	odyn->add_section_address(elfcpp::DT_FINI_ARRAY, *p);
5203*fae548d3Szrj 	odyn->add_section_size(elfcpp::DT_FINI_ARRAYSZ, *p);
5204*fae548d3Szrj 	break;
5205*fae548d3Szrj       case elfcpp::SHT_INIT_ARRAY:
5206*fae548d3Szrj 	odyn->add_section_address(elfcpp::DT_INIT_ARRAY, *p);
5207*fae548d3Szrj 	odyn->add_section_size(elfcpp::DT_INIT_ARRAYSZ, *p);
5208*fae548d3Szrj 	break;
5209*fae548d3Szrj       case elfcpp::SHT_PREINIT_ARRAY:
5210*fae548d3Szrj 	odyn->add_section_address(elfcpp::DT_PREINIT_ARRAY, *p);
5211*fae548d3Szrj 	odyn->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ, *p);
5212*fae548d3Szrj 	break;
5213*fae548d3Szrj       default:
5214*fae548d3Szrj 	break;
5215*fae548d3Szrj       }
5216*fae548d3Szrj 
5217*fae548d3Szrj   // Add a DT_RPATH entry if needed.
5218*fae548d3Szrj   const General_options::Dir_list& rpath(parameters->options().rpath());
5219*fae548d3Szrj   if (!rpath.empty())
5220*fae548d3Szrj     {
5221*fae548d3Szrj       std::string rpath_val;
5222*fae548d3Szrj       for (General_options::Dir_list::const_iterator p = rpath.begin();
5223*fae548d3Szrj 	   p != rpath.end();
5224*fae548d3Szrj 	   ++p)
5225*fae548d3Szrj 	{
5226*fae548d3Szrj 	  if (rpath_val.empty())
5227*fae548d3Szrj 	    rpath_val = p->name();
5228*fae548d3Szrj 	  else
5229*fae548d3Szrj 	    {
5230*fae548d3Szrj 	      // Eliminate duplicates.
5231*fae548d3Szrj 	      General_options::Dir_list::const_iterator q;
5232*fae548d3Szrj 	      for (q = rpath.begin(); q != p; ++q)
5233*fae548d3Szrj 		if (q->name() == p->name())
5234*fae548d3Szrj 		  break;
5235*fae548d3Szrj 	      if (q == p)
5236*fae548d3Szrj 		{
5237*fae548d3Szrj 		  rpath_val += ':';
5238*fae548d3Szrj 		  rpath_val += p->name();
5239*fae548d3Szrj 		}
5240*fae548d3Szrj 	    }
5241*fae548d3Szrj 	}
5242*fae548d3Szrj 
5243*fae548d3Szrj       if (!parameters->options().enable_new_dtags())
5244*fae548d3Szrj 	odyn->add_string(elfcpp::DT_RPATH, rpath_val);
5245*fae548d3Szrj       else
5246*fae548d3Szrj 	odyn->add_string(elfcpp::DT_RUNPATH, rpath_val);
5247*fae548d3Szrj     }
5248*fae548d3Szrj 
5249*fae548d3Szrj   // Look for text segments that have dynamic relocations.
5250*fae548d3Szrj   bool have_textrel = false;
5251*fae548d3Szrj   if (!this->script_options_->saw_sections_clause())
5252*fae548d3Szrj     {
5253*fae548d3Szrj       for (Segment_list::const_iterator p = this->segment_list_.begin();
5254*fae548d3Szrj 	   p != this->segment_list_.end();
5255*fae548d3Szrj 	   ++p)
5256*fae548d3Szrj 	{
5257*fae548d3Szrj 	  if ((*p)->type() == elfcpp::PT_LOAD
5258*fae548d3Szrj 	      && ((*p)->flags() & elfcpp::PF_W) == 0
5259*fae548d3Szrj 	      && (*p)->has_dynamic_reloc())
5260*fae548d3Szrj 	    {
5261*fae548d3Szrj 	      have_textrel = true;
5262*fae548d3Szrj 	      break;
5263*fae548d3Szrj 	    }
5264*fae548d3Szrj 	}
5265*fae548d3Szrj     }
5266*fae548d3Szrj   else
5267*fae548d3Szrj     {
5268*fae548d3Szrj       // We don't know the section -> segment mapping, so we are
5269*fae548d3Szrj       // conservative and just look for readonly sections with
5270*fae548d3Szrj       // relocations.  If those sections wind up in writable segments,
5271*fae548d3Szrj       // then we have created an unnecessary DT_TEXTREL entry.
5272*fae548d3Szrj       for (Section_list::const_iterator p = this->section_list_.begin();
5273*fae548d3Szrj 	   p != this->section_list_.end();
5274*fae548d3Szrj 	   ++p)
5275*fae548d3Szrj 	{
5276*fae548d3Szrj 	  if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
5277*fae548d3Szrj 	      && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
5278*fae548d3Szrj 	      && (*p)->has_dynamic_reloc())
5279*fae548d3Szrj 	    {
5280*fae548d3Szrj 	      have_textrel = true;
5281*fae548d3Szrj 	      break;
5282*fae548d3Szrj 	    }
5283*fae548d3Szrj 	}
5284*fae548d3Szrj     }
5285*fae548d3Szrj 
5286*fae548d3Szrj   if (parameters->options().filter() != NULL)
5287*fae548d3Szrj     odyn->add_string(elfcpp::DT_FILTER, parameters->options().filter());
5288*fae548d3Szrj   if (parameters->options().any_auxiliary())
5289*fae548d3Szrj     {
5290*fae548d3Szrj       for (options::String_set::const_iterator p =
5291*fae548d3Szrj 	     parameters->options().auxiliary_begin();
5292*fae548d3Szrj 	   p != parameters->options().auxiliary_end();
5293*fae548d3Szrj 	   ++p)
5294*fae548d3Szrj 	odyn->add_string(elfcpp::DT_AUXILIARY, *p);
5295*fae548d3Szrj     }
5296*fae548d3Szrj 
5297*fae548d3Szrj   // Add a DT_FLAGS entry if necessary.
5298*fae548d3Szrj   unsigned int flags = 0;
5299*fae548d3Szrj   if (have_textrel)
5300*fae548d3Szrj     {
5301*fae548d3Szrj       // Add a DT_TEXTREL for compatibility with older loaders.
5302*fae548d3Szrj       odyn->add_constant(elfcpp::DT_TEXTREL, 0);
5303*fae548d3Szrj       flags |= elfcpp::DF_TEXTREL;
5304*fae548d3Szrj 
5305*fae548d3Szrj       if (parameters->options().text())
5306*fae548d3Szrj 	gold_error(_("read-only segment has dynamic relocations"));
5307*fae548d3Szrj       else if (parameters->options().warn_shared_textrel()
5308*fae548d3Szrj 	       && parameters->options().shared())
5309*fae548d3Szrj 	gold_warning(_("shared library text segment is not shareable"));
5310*fae548d3Szrj     }
5311*fae548d3Szrj   if (parameters->options().shared() && this->has_static_tls())
5312*fae548d3Szrj     flags |= elfcpp::DF_STATIC_TLS;
5313*fae548d3Szrj   if (parameters->options().origin())
5314*fae548d3Szrj     flags |= elfcpp::DF_ORIGIN;
5315*fae548d3Szrj   if (parameters->options().Bsymbolic()
5316*fae548d3Szrj       && !parameters->options().have_dynamic_list())
5317*fae548d3Szrj     {
5318*fae548d3Szrj       flags |= elfcpp::DF_SYMBOLIC;
5319*fae548d3Szrj       // Add DT_SYMBOLIC for compatibility with older loaders.
5320*fae548d3Szrj       odyn->add_constant(elfcpp::DT_SYMBOLIC, 0);
5321*fae548d3Szrj     }
5322*fae548d3Szrj   if (parameters->options().now())
5323*fae548d3Szrj     flags |= elfcpp::DF_BIND_NOW;
5324*fae548d3Szrj   if (flags != 0)
5325*fae548d3Szrj     odyn->add_constant(elfcpp::DT_FLAGS, flags);
5326*fae548d3Szrj 
5327*fae548d3Szrj   flags = 0;
5328*fae548d3Szrj   if (parameters->options().global())
5329*fae548d3Szrj     flags |= elfcpp::DF_1_GLOBAL;
5330*fae548d3Szrj   if (parameters->options().initfirst())
5331*fae548d3Szrj     flags |= elfcpp::DF_1_INITFIRST;
5332*fae548d3Szrj   if (parameters->options().interpose())
5333*fae548d3Szrj     flags |= elfcpp::DF_1_INTERPOSE;
5334*fae548d3Szrj   if (parameters->options().loadfltr())
5335*fae548d3Szrj     flags |= elfcpp::DF_1_LOADFLTR;
5336*fae548d3Szrj   if (parameters->options().nodefaultlib())
5337*fae548d3Szrj     flags |= elfcpp::DF_1_NODEFLIB;
5338*fae548d3Szrj   if (parameters->options().nodelete())
5339*fae548d3Szrj     flags |= elfcpp::DF_1_NODELETE;
5340*fae548d3Szrj   if (parameters->options().nodlopen())
5341*fae548d3Szrj     flags |= elfcpp::DF_1_NOOPEN;
5342*fae548d3Szrj   if (parameters->options().nodump())
5343*fae548d3Szrj     flags |= elfcpp::DF_1_NODUMP;
5344*fae548d3Szrj   if (!parameters->options().shared())
5345*fae548d3Szrj     flags &= ~(elfcpp::DF_1_INITFIRST
5346*fae548d3Szrj 	       | elfcpp::DF_1_NODELETE
5347*fae548d3Szrj 	       | elfcpp::DF_1_NOOPEN);
5348*fae548d3Szrj   if (parameters->options().origin())
5349*fae548d3Szrj     flags |= elfcpp::DF_1_ORIGIN;
5350*fae548d3Szrj   if (parameters->options().now())
5351*fae548d3Szrj     flags |= elfcpp::DF_1_NOW;
5352*fae548d3Szrj   if (parameters->options().Bgroup())
5353*fae548d3Szrj     flags |= elfcpp::DF_1_GROUP;
5354*fae548d3Szrj   if (flags != 0)
5355*fae548d3Szrj     odyn->add_constant(elfcpp::DT_FLAGS_1, flags);
5356*fae548d3Szrj }
5357*fae548d3Szrj 
5358*fae548d3Szrj // Set the size of the _DYNAMIC symbol table to be the size of the
5359*fae548d3Szrj // dynamic data.
5360*fae548d3Szrj 
5361*fae548d3Szrj void
set_dynamic_symbol_size(const Symbol_table * symtab)5362*fae548d3Szrj Layout::set_dynamic_symbol_size(const Symbol_table* symtab)
5363*fae548d3Szrj {
5364*fae548d3Szrj   Output_data_dynamic* const odyn = this->dynamic_data_;
5365*fae548d3Szrj   if (odyn == NULL)
5366*fae548d3Szrj     return;
5367*fae548d3Szrj   odyn->finalize_data_size();
5368*fae548d3Szrj   if (this->dynamic_symbol_ == NULL)
5369*fae548d3Szrj     return;
5370*fae548d3Szrj   off_t data_size = odyn->data_size();
5371*fae548d3Szrj   const int size = parameters->target().get_size();
5372*fae548d3Szrj   if (size == 32)
5373*fae548d3Szrj     symtab->get_sized_symbol<32>(this->dynamic_symbol_)->set_symsize(data_size);
5374*fae548d3Szrj   else if (size == 64)
5375*fae548d3Szrj     symtab->get_sized_symbol<64>(this->dynamic_symbol_)->set_symsize(data_size);
5376*fae548d3Szrj   else
5377*fae548d3Szrj     gold_unreachable();
5378*fae548d3Szrj }
5379*fae548d3Szrj 
5380*fae548d3Szrj // The mapping of input section name prefixes to output section names.
5381*fae548d3Szrj // In some cases one prefix is itself a prefix of another prefix; in
5382*fae548d3Szrj // such a case the longer prefix must come first.  These prefixes are
5383*fae548d3Szrj // based on the GNU linker default ELF linker script.
5384*fae548d3Szrj 
5385*fae548d3Szrj #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
5386*fae548d3Szrj #define MAPPING_INIT_EXACT(f, t) { f, 0, t, sizeof(t) - 1 }
5387*fae548d3Szrj const Layout::Section_name_mapping Layout::section_name_mapping[] =
5388*fae548d3Szrj {
5389*fae548d3Szrj   MAPPING_INIT(".text.", ".text"),
5390*fae548d3Szrj   MAPPING_INIT(".rodata.", ".rodata"),
5391*fae548d3Szrj   MAPPING_INIT(".data.rel.ro.local.", ".data.rel.ro.local"),
5392*fae548d3Szrj   MAPPING_INIT_EXACT(".data.rel.ro.local", ".data.rel.ro.local"),
5393*fae548d3Szrj   MAPPING_INIT(".data.rel.ro.", ".data.rel.ro"),
5394*fae548d3Szrj   MAPPING_INIT_EXACT(".data.rel.ro", ".data.rel.ro"),
5395*fae548d3Szrj   MAPPING_INIT(".data.", ".data"),
5396*fae548d3Szrj   MAPPING_INIT(".bss.", ".bss"),
5397*fae548d3Szrj   MAPPING_INIT(".tdata.", ".tdata"),
5398*fae548d3Szrj   MAPPING_INIT(".tbss.", ".tbss"),
5399*fae548d3Szrj   MAPPING_INIT(".init_array.", ".init_array"),
5400*fae548d3Szrj   MAPPING_INIT(".fini_array.", ".fini_array"),
5401*fae548d3Szrj   MAPPING_INIT(".sdata.", ".sdata"),
5402*fae548d3Szrj   MAPPING_INIT(".sbss.", ".sbss"),
5403*fae548d3Szrj   // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
5404*fae548d3Szrj   // differently depending on whether it is creating a shared library.
5405*fae548d3Szrj   MAPPING_INIT(".sdata2.", ".sdata"),
5406*fae548d3Szrj   MAPPING_INIT(".sbss2.", ".sbss"),
5407*fae548d3Szrj   MAPPING_INIT(".lrodata.", ".lrodata"),
5408*fae548d3Szrj   MAPPING_INIT(".ldata.", ".ldata"),
5409*fae548d3Szrj   MAPPING_INIT(".lbss.", ".lbss"),
5410*fae548d3Szrj   MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
5411*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
5412*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
5413*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.t.", ".text"),
5414*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
5415*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.d.", ".data"),
5416*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
5417*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
5418*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
5419*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
5420*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
5421*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
5422*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
5423*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
5424*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
5425*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
5426*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
5427*fae548d3Szrj   MAPPING_INIT(".ARM.extab", ".ARM.extab"),
5428*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
5429*fae548d3Szrj   MAPPING_INIT(".ARM.exidx", ".ARM.exidx"),
5430*fae548d3Szrj   MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
5431*fae548d3Szrj   MAPPING_INIT(".gnu.build.attributes.", ".gnu.build.attributes"),
5432*fae548d3Szrj };
5433*fae548d3Szrj 
5434*fae548d3Szrj // Mapping for ".text" section prefixes with -z,keep-text-section-prefix.
5435*fae548d3Szrj const Layout::Section_name_mapping Layout::text_section_name_mapping[] =
5436*fae548d3Szrj {
5437*fae548d3Szrj   MAPPING_INIT(".text.hot.", ".text.hot"),
5438*fae548d3Szrj   MAPPING_INIT_EXACT(".text.hot", ".text.hot"),
5439*fae548d3Szrj   MAPPING_INIT(".text.unlikely.", ".text.unlikely"),
5440*fae548d3Szrj   MAPPING_INIT_EXACT(".text.unlikely", ".text.unlikely"),
5441*fae548d3Szrj   MAPPING_INIT(".text.startup.", ".text.startup"),
5442*fae548d3Szrj   MAPPING_INIT_EXACT(".text.startup", ".text.startup"),
5443*fae548d3Szrj   MAPPING_INIT(".text.exit.", ".text.exit"),
5444*fae548d3Szrj   MAPPING_INIT_EXACT(".text.exit", ".text.exit"),
5445*fae548d3Szrj   MAPPING_INIT(".text.", ".text"),
5446*fae548d3Szrj };
5447*fae548d3Szrj #undef MAPPING_INIT
5448*fae548d3Szrj #undef MAPPING_INIT_EXACT
5449*fae548d3Szrj 
5450*fae548d3Szrj const int Layout::section_name_mapping_count =
5451*fae548d3Szrj   (sizeof(Layout::section_name_mapping)
5452*fae548d3Szrj    / sizeof(Layout::section_name_mapping[0]));
5453*fae548d3Szrj 
5454*fae548d3Szrj const int Layout::text_section_name_mapping_count =
5455*fae548d3Szrj   (sizeof(Layout::text_section_name_mapping)
5456*fae548d3Szrj    / sizeof(Layout::text_section_name_mapping[0]));
5457*fae548d3Szrj 
5458*fae548d3Szrj // Find section name NAME in PSNM and return the mapped name if found
5459*fae548d3Szrj // with the length set in PLEN.
5460*fae548d3Szrj const char *
match_section_name(const Layout::Section_name_mapping * psnm,const int count,const char * name,size_t * plen)5461*fae548d3Szrj Layout::match_section_name(const Layout::Section_name_mapping* psnm,
5462*fae548d3Szrj 			   const int count,
5463*fae548d3Szrj 			   const char* name, size_t* plen)
5464*fae548d3Szrj {
5465*fae548d3Szrj   for (int i = 0; i < count; ++i, ++psnm)
5466*fae548d3Szrj     {
5467*fae548d3Szrj       if (psnm->fromlen > 0)
5468*fae548d3Szrj 	{
5469*fae548d3Szrj 	  if (strncmp(name, psnm->from, psnm->fromlen) == 0)
5470*fae548d3Szrj 	    {
5471*fae548d3Szrj 	      *plen = psnm->tolen;
5472*fae548d3Szrj 	      return psnm->to;
5473*fae548d3Szrj 	    }
5474*fae548d3Szrj 	}
5475*fae548d3Szrj       else
5476*fae548d3Szrj 	{
5477*fae548d3Szrj 	  if (strcmp(name, psnm->from) == 0)
5478*fae548d3Szrj 	    {
5479*fae548d3Szrj 	      *plen = psnm->tolen;
5480*fae548d3Szrj 	      return psnm->to;
5481*fae548d3Szrj 	    }
5482*fae548d3Szrj 	}
5483*fae548d3Szrj     }
5484*fae548d3Szrj   return NULL;
5485*fae548d3Szrj }
5486*fae548d3Szrj 
5487*fae548d3Szrj // Choose the output section name to use given an input section name.
5488*fae548d3Szrj // Set *PLEN to the length of the name.  *PLEN is initialized to the
5489*fae548d3Szrj // length of NAME.
5490*fae548d3Szrj 
5491*fae548d3Szrj const char*
output_section_name(const Relobj * relobj,const char * name,size_t * plen)5492*fae548d3Szrj Layout::output_section_name(const Relobj* relobj, const char* name,
5493*fae548d3Szrj 			    size_t* plen)
5494*fae548d3Szrj {
5495*fae548d3Szrj   // gcc 4.3 generates the following sorts of section names when it
5496*fae548d3Szrj   // needs a section name specific to a function:
5497*fae548d3Szrj   //   .text.FN
5498*fae548d3Szrj   //   .rodata.FN
5499*fae548d3Szrj   //   .sdata2.FN
5500*fae548d3Szrj   //   .data.FN
5501*fae548d3Szrj   //   .data.rel.FN
5502*fae548d3Szrj   //   .data.rel.local.FN
5503*fae548d3Szrj   //   .data.rel.ro.FN
5504*fae548d3Szrj   //   .data.rel.ro.local.FN
5505*fae548d3Szrj   //   .sdata.FN
5506*fae548d3Szrj   //   .bss.FN
5507*fae548d3Szrj   //   .sbss.FN
5508*fae548d3Szrj   //   .tdata.FN
5509*fae548d3Szrj   //   .tbss.FN
5510*fae548d3Szrj 
5511*fae548d3Szrj   // The GNU linker maps all of those to the part before the .FN,
5512*fae548d3Szrj   // except that .data.rel.local.FN is mapped to .data, and
5513*fae548d3Szrj   // .data.rel.ro.local.FN is mapped to .data.rel.ro.  The sections
5514*fae548d3Szrj   // beginning with .data.rel.ro.local are grouped together.
5515*fae548d3Szrj 
5516*fae548d3Szrj   // For an anonymous namespace, the string FN can contain a '.'.
5517*fae548d3Szrj 
5518*fae548d3Szrj   // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
5519*fae548d3Szrj   // GNU linker maps to .rodata.
5520*fae548d3Szrj 
5521*fae548d3Szrj   // The .data.rel.ro sections are used with -z relro.  The sections
5522*fae548d3Szrj   // are recognized by name.  We use the same names that the GNU
5523*fae548d3Szrj   // linker does for these sections.
5524*fae548d3Szrj 
5525*fae548d3Szrj   // It is hard to handle this in a principled way, so we don't even
5526*fae548d3Szrj   // try.  We use a table of mappings.  If the input section name is
5527*fae548d3Szrj   // not found in the table, we simply use it as the output section
5528*fae548d3Szrj   // name.
5529*fae548d3Szrj 
5530*fae548d3Szrj   if (parameters->options().keep_text_section_prefix()
5531*fae548d3Szrj       && is_prefix_of(".text", name))
5532*fae548d3Szrj     {
5533*fae548d3Szrj       const char* match = match_section_name(text_section_name_mapping,
5534*fae548d3Szrj 					     text_section_name_mapping_count,
5535*fae548d3Szrj 					     name, plen);
5536*fae548d3Szrj       if (match != NULL)
5537*fae548d3Szrj 	return match;
5538*fae548d3Szrj     }
5539*fae548d3Szrj 
5540*fae548d3Szrj   const char* match = match_section_name(section_name_mapping,
5541*fae548d3Szrj 					 section_name_mapping_count, name, plen);
5542*fae548d3Szrj   if (match != NULL)
5543*fae548d3Szrj     return match;
5544*fae548d3Szrj 
5545*fae548d3Szrj   // As an additional complication, .ctors sections are output in
5546*fae548d3Szrj   // either .ctors or .init_array sections, and .dtors sections are
5547*fae548d3Szrj   // output in either .dtors or .fini_array sections.
5548*fae548d3Szrj   if (is_prefix_of(".ctors.", name) || is_prefix_of(".dtors.", name))
5549*fae548d3Szrj     {
5550*fae548d3Szrj       if (parameters->options().ctors_in_init_array())
5551*fae548d3Szrj 	{
5552*fae548d3Szrj 	  *plen = 11;
5553*fae548d3Szrj 	  return name[1] == 'c' ? ".init_array" : ".fini_array";
5554*fae548d3Szrj 	}
5555*fae548d3Szrj       else
5556*fae548d3Szrj 	{
5557*fae548d3Szrj 	  *plen = 6;
5558*fae548d3Szrj 	  return name[1] == 'c' ? ".ctors" : ".dtors";
5559*fae548d3Szrj 	}
5560*fae548d3Szrj     }
5561*fae548d3Szrj   if (parameters->options().ctors_in_init_array()
5562*fae548d3Szrj       && (strcmp(name, ".ctors") == 0 || strcmp(name, ".dtors") == 0))
5563*fae548d3Szrj     {
5564*fae548d3Szrj       // To make .init_array/.fini_array work with gcc we must exclude
5565*fae548d3Szrj       // .ctors and .dtors sections from the crtbegin and crtend
5566*fae548d3Szrj       // files.
5567*fae548d3Szrj       if (relobj == NULL
5568*fae548d3Szrj 	  || (!Layout::match_file_name(relobj, "crtbegin")
5569*fae548d3Szrj 	      && !Layout::match_file_name(relobj, "crtend")))
5570*fae548d3Szrj 	{
5571*fae548d3Szrj 	  *plen = 11;
5572*fae548d3Szrj 	  return name[1] == 'c' ? ".init_array" : ".fini_array";
5573*fae548d3Szrj 	}
5574*fae548d3Szrj     }
5575*fae548d3Szrj 
5576*fae548d3Szrj   return name;
5577*fae548d3Szrj }
5578*fae548d3Szrj 
5579*fae548d3Szrj // Return true if RELOBJ is an input file whose base name matches
5580*fae548d3Szrj // FILE_NAME.  The base name must have an extension of ".o", and must
5581*fae548d3Szrj // be exactly FILE_NAME.o or FILE_NAME, one character, ".o".  This is
5582*fae548d3Szrj // to match crtbegin.o as well as crtbeginS.o without getting confused
5583*fae548d3Szrj // by other possibilities.  Overall matching the file name this way is
5584*fae548d3Szrj // a dreadful hack, but the GNU linker does it in order to better
5585*fae548d3Szrj // support gcc, and we need to be compatible.
5586*fae548d3Szrj 
5587*fae548d3Szrj bool
match_file_name(const Relobj * relobj,const char * match)5588*fae548d3Szrj Layout::match_file_name(const Relobj* relobj, const char* match)
5589*fae548d3Szrj {
5590*fae548d3Szrj   const std::string& file_name(relobj->name());
5591*fae548d3Szrj   const char* base_name = lbasename(file_name.c_str());
5592*fae548d3Szrj   size_t match_len = strlen(match);
5593*fae548d3Szrj   if (strncmp(base_name, match, match_len) != 0)
5594*fae548d3Szrj     return false;
5595*fae548d3Szrj   size_t base_len = strlen(base_name);
5596*fae548d3Szrj   if (base_len != match_len + 2 && base_len != match_len + 3)
5597*fae548d3Szrj     return false;
5598*fae548d3Szrj   return memcmp(base_name + base_len - 2, ".o", 2) == 0;
5599*fae548d3Szrj }
5600*fae548d3Szrj 
5601*fae548d3Szrj // Check if a comdat group or .gnu.linkonce section with the given
5602*fae548d3Szrj // NAME is selected for the link.  If there is already a section,
5603*fae548d3Szrj // *KEPT_SECTION is set to point to the existing section and the
5604*fae548d3Szrj // function returns false.  Otherwise, OBJECT, SHNDX, IS_COMDAT, and
5605*fae548d3Szrj // IS_GROUP_NAME are recorded for this NAME in the layout object,
5606*fae548d3Szrj // *KEPT_SECTION is set to the internal copy and the function returns
5607*fae548d3Szrj // true.
5608*fae548d3Szrj 
5609*fae548d3Szrj bool
find_or_add_kept_section(const std::string & name,Relobj * object,unsigned int shndx,bool is_comdat,bool is_group_name,Kept_section ** kept_section)5610*fae548d3Szrj Layout::find_or_add_kept_section(const std::string& name,
5611*fae548d3Szrj 				 Relobj* object,
5612*fae548d3Szrj 				 unsigned int shndx,
5613*fae548d3Szrj 				 bool is_comdat,
5614*fae548d3Szrj 				 bool is_group_name,
5615*fae548d3Szrj 				 Kept_section** kept_section)
5616*fae548d3Szrj {
5617*fae548d3Szrj   // It's normal to see a couple of entries here, for the x86 thunk
5618*fae548d3Szrj   // sections.  If we see more than a few, we're linking a C++
5619*fae548d3Szrj   // program, and we resize to get more space to minimize rehashing.
5620*fae548d3Szrj   if (this->signatures_.size() > 4
5621*fae548d3Szrj       && !this->resized_signatures_)
5622*fae548d3Szrj     {
5623*fae548d3Szrj       reserve_unordered_map(&this->signatures_,
5624*fae548d3Szrj 			    this->number_of_input_files_ * 64);
5625*fae548d3Szrj       this->resized_signatures_ = true;
5626*fae548d3Szrj     }
5627*fae548d3Szrj 
5628*fae548d3Szrj   Kept_section candidate;
5629*fae548d3Szrj   std::pair<Signatures::iterator, bool> ins =
5630*fae548d3Szrj     this->signatures_.insert(std::make_pair(name, candidate));
5631*fae548d3Szrj 
5632*fae548d3Szrj   if (kept_section != NULL)
5633*fae548d3Szrj     *kept_section = &ins.first->second;
5634*fae548d3Szrj   if (ins.second)
5635*fae548d3Szrj     {
5636*fae548d3Szrj       // This is the first time we've seen this signature.
5637*fae548d3Szrj       ins.first->second.set_object(object);
5638*fae548d3Szrj       ins.first->second.set_shndx(shndx);
5639*fae548d3Szrj       if (is_comdat)
5640*fae548d3Szrj 	ins.first->second.set_is_comdat();
5641*fae548d3Szrj       if (is_group_name)
5642*fae548d3Szrj 	ins.first->second.set_is_group_name();
5643*fae548d3Szrj       return true;
5644*fae548d3Szrj     }
5645*fae548d3Szrj 
5646*fae548d3Szrj   // We have already seen this signature.
5647*fae548d3Szrj 
5648*fae548d3Szrj   if (ins.first->second.is_group_name())
5649*fae548d3Szrj     {
5650*fae548d3Szrj       // We've already seen a real section group with this signature.
5651*fae548d3Szrj       // If the kept group is from a plugin object, and we're in the
5652*fae548d3Szrj       // replacement phase, accept the new one as a replacement.
5653*fae548d3Szrj       if (ins.first->second.object() == NULL
5654*fae548d3Szrj 	  && parameters->options().plugins()->in_replacement_phase())
5655*fae548d3Szrj 	{
5656*fae548d3Szrj 	  ins.first->second.set_object(object);
5657*fae548d3Szrj 	  ins.first->second.set_shndx(shndx);
5658*fae548d3Szrj 	  return true;
5659*fae548d3Szrj 	}
5660*fae548d3Szrj       return false;
5661*fae548d3Szrj     }
5662*fae548d3Szrj   else if (is_group_name)
5663*fae548d3Szrj     {
5664*fae548d3Szrj       // This is a real section group, and we've already seen a
5665*fae548d3Szrj       // linkonce section with this signature.  Record that we've seen
5666*fae548d3Szrj       // a section group, and don't include this section group.
5667*fae548d3Szrj       ins.first->second.set_is_group_name();
5668*fae548d3Szrj       return false;
5669*fae548d3Szrj     }
5670*fae548d3Szrj   else
5671*fae548d3Szrj     {
5672*fae548d3Szrj       // We've already seen a linkonce section and this is a linkonce
5673*fae548d3Szrj       // section.  These don't block each other--this may be the same
5674*fae548d3Szrj       // symbol name with different section types.
5675*fae548d3Szrj       return true;
5676*fae548d3Szrj     }
5677*fae548d3Szrj }
5678*fae548d3Szrj 
5679*fae548d3Szrj // Store the allocated sections into the section list.
5680*fae548d3Szrj 
5681*fae548d3Szrj void
get_allocated_sections(Section_list * section_list) const5682*fae548d3Szrj Layout::get_allocated_sections(Section_list* section_list) const
5683*fae548d3Szrj {
5684*fae548d3Szrj   for (Section_list::const_iterator p = this->section_list_.begin();
5685*fae548d3Szrj        p != this->section_list_.end();
5686*fae548d3Szrj        ++p)
5687*fae548d3Szrj     if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
5688*fae548d3Szrj       section_list->push_back(*p);
5689*fae548d3Szrj }
5690*fae548d3Szrj 
5691*fae548d3Szrj // Store the executable sections into the section list.
5692*fae548d3Szrj 
5693*fae548d3Szrj void
get_executable_sections(Section_list * section_list) const5694*fae548d3Szrj Layout::get_executable_sections(Section_list* section_list) const
5695*fae548d3Szrj {
5696*fae548d3Szrj   for (Section_list::const_iterator p = this->section_list_.begin();
5697*fae548d3Szrj        p != this->section_list_.end();
5698*fae548d3Szrj        ++p)
5699*fae548d3Szrj     if (((*p)->flags() & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
5700*fae548d3Szrj 	== (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
5701*fae548d3Szrj       section_list->push_back(*p);
5702*fae548d3Szrj }
5703*fae548d3Szrj 
5704*fae548d3Szrj // Create an output segment.
5705*fae548d3Szrj 
5706*fae548d3Szrj Output_segment*
make_output_segment(elfcpp::Elf_Word type,elfcpp::Elf_Word flags)5707*fae548d3Szrj Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
5708*fae548d3Szrj {
5709*fae548d3Szrj   gold_assert(!parameters->options().relocatable());
5710*fae548d3Szrj   Output_segment* oseg = new Output_segment(type, flags);
5711*fae548d3Szrj   this->segment_list_.push_back(oseg);
5712*fae548d3Szrj 
5713*fae548d3Szrj   if (type == elfcpp::PT_TLS)
5714*fae548d3Szrj     this->tls_segment_ = oseg;
5715*fae548d3Szrj   else if (type == elfcpp::PT_GNU_RELRO)
5716*fae548d3Szrj     this->relro_segment_ = oseg;
5717*fae548d3Szrj   else if (type == elfcpp::PT_INTERP)
5718*fae548d3Szrj     this->interp_segment_ = oseg;
5719*fae548d3Szrj 
5720*fae548d3Szrj   return oseg;
5721*fae548d3Szrj }
5722*fae548d3Szrj 
5723*fae548d3Szrj // Return the file offset of the normal symbol table.
5724*fae548d3Szrj 
5725*fae548d3Szrj off_t
symtab_section_offset() const5726*fae548d3Szrj Layout::symtab_section_offset() const
5727*fae548d3Szrj {
5728*fae548d3Szrj   if (this->symtab_section_ != NULL)
5729*fae548d3Szrj     return this->symtab_section_->offset();
5730*fae548d3Szrj   return 0;
5731*fae548d3Szrj }
5732*fae548d3Szrj 
5733*fae548d3Szrj // Return the section index of the normal symbol table.  It may have
5734*fae548d3Szrj // been stripped by the -s/--strip-all option.
5735*fae548d3Szrj 
5736*fae548d3Szrj unsigned int
symtab_section_shndx() const5737*fae548d3Szrj Layout::symtab_section_shndx() const
5738*fae548d3Szrj {
5739*fae548d3Szrj   if (this->symtab_section_ != NULL)
5740*fae548d3Szrj     return this->symtab_section_->out_shndx();
5741*fae548d3Szrj   return 0;
5742*fae548d3Szrj }
5743*fae548d3Szrj 
5744*fae548d3Szrj // Write out the Output_sections.  Most won't have anything to write,
5745*fae548d3Szrj // since most of the data will come from input sections which are
5746*fae548d3Szrj // handled elsewhere.  But some Output_sections do have Output_data.
5747*fae548d3Szrj 
5748*fae548d3Szrj void
write_output_sections(Output_file * of) const5749*fae548d3Szrj Layout::write_output_sections(Output_file* of) const
5750*fae548d3Szrj {
5751*fae548d3Szrj   for (Section_list::const_iterator p = this->section_list_.begin();
5752*fae548d3Szrj        p != this->section_list_.end();
5753*fae548d3Szrj        ++p)
5754*fae548d3Szrj     {
5755*fae548d3Szrj       if (!(*p)->after_input_sections())
5756*fae548d3Szrj 	(*p)->write(of);
5757*fae548d3Szrj     }
5758*fae548d3Szrj }
5759*fae548d3Szrj 
5760*fae548d3Szrj // Write out data not associated with a section or the symbol table.
5761*fae548d3Szrj 
5762*fae548d3Szrj void
write_data(const Symbol_table * symtab,Output_file * of) const5763*fae548d3Szrj Layout::write_data(const Symbol_table* symtab, Output_file* of) const
5764*fae548d3Szrj {
5765*fae548d3Szrj   if (!parameters->options().strip_all())
5766*fae548d3Szrj     {
5767*fae548d3Szrj       const Output_section* symtab_section = this->symtab_section_;
5768*fae548d3Szrj       for (Section_list::const_iterator p = this->section_list_.begin();
5769*fae548d3Szrj 	   p != this->section_list_.end();
5770*fae548d3Szrj 	   ++p)
5771*fae548d3Szrj 	{
5772*fae548d3Szrj 	  if ((*p)->needs_symtab_index())
5773*fae548d3Szrj 	    {
5774*fae548d3Szrj 	      gold_assert(symtab_section != NULL);
5775*fae548d3Szrj 	      unsigned int index = (*p)->symtab_index();
5776*fae548d3Szrj 	      gold_assert(index > 0 && index != -1U);
5777*fae548d3Szrj 	      off_t off = (symtab_section->offset()
5778*fae548d3Szrj 			   + index * symtab_section->entsize());
5779*fae548d3Szrj 	      symtab->write_section_symbol(*p, this->symtab_xindex_, of, off);
5780*fae548d3Szrj 	    }
5781*fae548d3Szrj 	}
5782*fae548d3Szrj     }
5783*fae548d3Szrj 
5784*fae548d3Szrj   const Output_section* dynsym_section = this->dynsym_section_;
5785*fae548d3Szrj   for (Section_list::const_iterator p = this->section_list_.begin();
5786*fae548d3Szrj        p != this->section_list_.end();
5787*fae548d3Szrj        ++p)
5788*fae548d3Szrj     {
5789*fae548d3Szrj       if ((*p)->needs_dynsym_index())
5790*fae548d3Szrj 	{
5791*fae548d3Szrj 	  gold_assert(dynsym_section != NULL);
5792*fae548d3Szrj 	  unsigned int index = (*p)->dynsym_index();
5793*fae548d3Szrj 	  gold_assert(index > 0 && index != -1U);
5794*fae548d3Szrj 	  off_t off = (dynsym_section->offset()
5795*fae548d3Szrj 		       + index * dynsym_section->entsize());
5796*fae548d3Szrj 	  symtab->write_section_symbol(*p, this->dynsym_xindex_, of, off);
5797*fae548d3Szrj 	}
5798*fae548d3Szrj     }
5799*fae548d3Szrj 
5800*fae548d3Szrj   // Write out the Output_data which are not in an Output_section.
5801*fae548d3Szrj   for (Data_list::const_iterator p = this->special_output_list_.begin();
5802*fae548d3Szrj        p != this->special_output_list_.end();
5803*fae548d3Szrj        ++p)
5804*fae548d3Szrj     (*p)->write(of);
5805*fae548d3Szrj 
5806*fae548d3Szrj   // Write out the Output_data which are not in an Output_section
5807*fae548d3Szrj   // and are regenerated in each iteration of relaxation.
5808*fae548d3Szrj   for (Data_list::const_iterator p = this->relax_output_list_.begin();
5809*fae548d3Szrj        p != this->relax_output_list_.end();
5810*fae548d3Szrj        ++p)
5811*fae548d3Szrj     (*p)->write(of);
5812*fae548d3Szrj }
5813*fae548d3Szrj 
5814*fae548d3Szrj // Write out the Output_sections which can only be written after the
5815*fae548d3Szrj // input sections are complete.
5816*fae548d3Szrj 
5817*fae548d3Szrj void
write_sections_after_input_sections(Output_file * of)5818*fae548d3Szrj Layout::write_sections_after_input_sections(Output_file* of)
5819*fae548d3Szrj {
5820*fae548d3Szrj   // Determine the final section offsets, and thus the final output
5821*fae548d3Szrj   // file size.  Note we finalize the .shstrab last, to allow the
5822*fae548d3Szrj   // after_input_section sections to modify their section-names before
5823*fae548d3Szrj   // writing.
5824*fae548d3Szrj   if (this->any_postprocessing_sections_)
5825*fae548d3Szrj     {
5826*fae548d3Szrj       off_t off = this->output_file_size_;
5827*fae548d3Szrj       off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
5828*fae548d3Szrj 
5829*fae548d3Szrj       // Now that we've finalized the names, we can finalize the shstrab.
5830*fae548d3Szrj       off =
5831*fae548d3Szrj 	this->set_section_offsets(off,
5832*fae548d3Szrj 				  STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
5833*fae548d3Szrj 
5834*fae548d3Szrj       if (off > this->output_file_size_)
5835*fae548d3Szrj 	{
5836*fae548d3Szrj 	  of->resize(off);
5837*fae548d3Szrj 	  this->output_file_size_ = off;
5838*fae548d3Szrj 	}
5839*fae548d3Szrj     }
5840*fae548d3Szrj 
5841*fae548d3Szrj   for (Section_list::const_iterator p = this->section_list_.begin();
5842*fae548d3Szrj        p != this->section_list_.end();
5843*fae548d3Szrj        ++p)
5844*fae548d3Szrj     {
5845*fae548d3Szrj       if ((*p)->after_input_sections())
5846*fae548d3Szrj 	(*p)->write(of);
5847*fae548d3Szrj     }
5848*fae548d3Szrj 
5849*fae548d3Szrj   this->section_headers_->write(of);
5850*fae548d3Szrj }
5851*fae548d3Szrj 
5852*fae548d3Szrj // If a tree-style build ID was requested, the parallel part of that computation
5853*fae548d3Szrj // is already done, and the final hash-of-hashes is computed here.  For other
5854*fae548d3Szrj // types of build IDs, all the work is done here.
5855*fae548d3Szrj 
5856*fae548d3Szrj void
write_build_id(Output_file * of,unsigned char * array_of_hashes,size_t size_of_hashes) const5857*fae548d3Szrj Layout::write_build_id(Output_file* of, unsigned char* array_of_hashes,
5858*fae548d3Szrj 		       size_t size_of_hashes) const
5859*fae548d3Szrj {
5860*fae548d3Szrj   if (this->build_id_note_ == NULL)
5861*fae548d3Szrj     return;
5862*fae548d3Szrj 
5863*fae548d3Szrj   unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
5864*fae548d3Szrj 					  this->build_id_note_->data_size());
5865*fae548d3Szrj 
5866*fae548d3Szrj   if (array_of_hashes == NULL)
5867*fae548d3Szrj     {
5868*fae548d3Szrj       const size_t output_file_size = this->output_file_size();
5869*fae548d3Szrj       const unsigned char* iv = of->get_input_view(0, output_file_size);
5870*fae548d3Szrj       const char* style = parameters->options().build_id();
5871*fae548d3Szrj 
5872*fae548d3Szrj       // If we get here with style == "tree" then the output must be
5873*fae548d3Szrj       // too small for chunking, and we use SHA-1 in that case.
5874*fae548d3Szrj       if ((strcmp(style, "sha1") == 0) || (strcmp(style, "tree") == 0))
5875*fae548d3Szrj 	sha1_buffer(reinterpret_cast<const char*>(iv), output_file_size, ov);
5876*fae548d3Szrj       else if (strcmp(style, "md5") == 0)
5877*fae548d3Szrj 	md5_buffer(reinterpret_cast<const char*>(iv), output_file_size, ov);
5878*fae548d3Szrj       else
5879*fae548d3Szrj 	gold_unreachable();
5880*fae548d3Szrj 
5881*fae548d3Szrj       of->free_input_view(0, output_file_size, iv);
5882*fae548d3Szrj     }
5883*fae548d3Szrj   else
5884*fae548d3Szrj     {
5885*fae548d3Szrj       // Non-overlapping substrings of the output file have been hashed.
5886*fae548d3Szrj       // Compute SHA-1 hash of the hashes.
5887*fae548d3Szrj       sha1_buffer(reinterpret_cast<const char*>(array_of_hashes),
5888*fae548d3Szrj 		  size_of_hashes, ov);
5889*fae548d3Szrj       delete[] array_of_hashes;
5890*fae548d3Szrj     }
5891*fae548d3Szrj 
5892*fae548d3Szrj   of->write_output_view(this->build_id_note_->offset(),
5893*fae548d3Szrj 			this->build_id_note_->data_size(),
5894*fae548d3Szrj 			ov);
5895*fae548d3Szrj }
5896*fae548d3Szrj 
5897*fae548d3Szrj // Write out a binary file.  This is called after the link is
5898*fae548d3Szrj // complete.  IN is the temporary output file we used to generate the
5899*fae548d3Szrj // ELF code.  We simply walk through the segments, read them from
5900*fae548d3Szrj // their file offset in IN, and write them to their load address in
5901*fae548d3Szrj // the output file.  FIXME: with a bit more work, we could support
5902*fae548d3Szrj // S-records and/or Intel hex format here.
5903*fae548d3Szrj 
5904*fae548d3Szrj void
write_binary(Output_file * in) const5905*fae548d3Szrj Layout::write_binary(Output_file* in) const
5906*fae548d3Szrj {
5907*fae548d3Szrj   gold_assert(parameters->options().oformat_enum()
5908*fae548d3Szrj 	      == General_options::OBJECT_FORMAT_BINARY);
5909*fae548d3Szrj 
5910*fae548d3Szrj   // Get the size of the binary file.
5911*fae548d3Szrj   uint64_t max_load_address = 0;
5912*fae548d3Szrj   for (Segment_list::const_iterator p = this->segment_list_.begin();
5913*fae548d3Szrj        p != this->segment_list_.end();
5914*fae548d3Szrj        ++p)
5915*fae548d3Szrj     {
5916*fae548d3Szrj       if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
5917*fae548d3Szrj 	{
5918*fae548d3Szrj 	  uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
5919*fae548d3Szrj 	  if (max_paddr > max_load_address)
5920*fae548d3Szrj 	    max_load_address = max_paddr;
5921*fae548d3Szrj 	}
5922*fae548d3Szrj     }
5923*fae548d3Szrj 
5924*fae548d3Szrj   Output_file out(parameters->options().output_file_name());
5925*fae548d3Szrj   out.open(max_load_address);
5926*fae548d3Szrj 
5927*fae548d3Szrj   for (Segment_list::const_iterator p = this->segment_list_.begin();
5928*fae548d3Szrj        p != this->segment_list_.end();
5929*fae548d3Szrj        ++p)
5930*fae548d3Szrj     {
5931*fae548d3Szrj       if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
5932*fae548d3Szrj 	{
5933*fae548d3Szrj 	  const unsigned char* vin = in->get_input_view((*p)->offset(),
5934*fae548d3Szrj 							(*p)->filesz());
5935*fae548d3Szrj 	  unsigned char* vout = out.get_output_view((*p)->paddr(),
5936*fae548d3Szrj 						    (*p)->filesz());
5937*fae548d3Szrj 	  memcpy(vout, vin, (*p)->filesz());
5938*fae548d3Szrj 	  out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
5939*fae548d3Szrj 	  in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
5940*fae548d3Szrj 	}
5941*fae548d3Szrj     }
5942*fae548d3Szrj 
5943*fae548d3Szrj   out.close();
5944*fae548d3Szrj }
5945*fae548d3Szrj 
5946*fae548d3Szrj // Print the output sections to the map file.
5947*fae548d3Szrj 
5948*fae548d3Szrj void
print_to_mapfile(Mapfile * mapfile) const5949*fae548d3Szrj Layout::print_to_mapfile(Mapfile* mapfile) const
5950*fae548d3Szrj {
5951*fae548d3Szrj   for (Segment_list::const_iterator p = this->segment_list_.begin();
5952*fae548d3Szrj        p != this->segment_list_.end();
5953*fae548d3Szrj        ++p)
5954*fae548d3Szrj     (*p)->print_sections_to_mapfile(mapfile);
5955*fae548d3Szrj   for (Section_list::const_iterator p = this->unattached_section_list_.begin();
5956*fae548d3Szrj        p != this->unattached_section_list_.end();
5957*fae548d3Szrj        ++p)
5958*fae548d3Szrj     (*p)->print_to_mapfile(mapfile);
5959*fae548d3Szrj }
5960*fae548d3Szrj 
5961*fae548d3Szrj // Print statistical information to stderr.  This is used for --stats.
5962*fae548d3Szrj 
5963*fae548d3Szrj void
print_stats() const5964*fae548d3Szrj Layout::print_stats() const
5965*fae548d3Szrj {
5966*fae548d3Szrj   this->namepool_.print_stats("section name pool");
5967*fae548d3Szrj   this->sympool_.print_stats("output symbol name pool");
5968*fae548d3Szrj   this->dynpool_.print_stats("dynamic name pool");
5969*fae548d3Szrj 
5970*fae548d3Szrj   for (Section_list::const_iterator p = this->section_list_.begin();
5971*fae548d3Szrj        p != this->section_list_.end();
5972*fae548d3Szrj        ++p)
5973*fae548d3Szrj     (*p)->print_merge_stats();
5974*fae548d3Szrj }
5975*fae548d3Szrj 
5976*fae548d3Szrj // Write_sections_task methods.
5977*fae548d3Szrj 
5978*fae548d3Szrj // We can always run this task.
5979*fae548d3Szrj 
5980*fae548d3Szrj Task_token*
is_runnable()5981*fae548d3Szrj Write_sections_task::is_runnable()
5982*fae548d3Szrj {
5983*fae548d3Szrj   return NULL;
5984*fae548d3Szrj }
5985*fae548d3Szrj 
5986*fae548d3Szrj // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
5987*fae548d3Szrj // when finished.
5988*fae548d3Szrj 
5989*fae548d3Szrj void
locks(Task_locker * tl)5990*fae548d3Szrj Write_sections_task::locks(Task_locker* tl)
5991*fae548d3Szrj {
5992*fae548d3Szrj   tl->add(this, this->output_sections_blocker_);
5993*fae548d3Szrj   if (this->input_sections_blocker_ != NULL)
5994*fae548d3Szrj     tl->add(this, this->input_sections_blocker_);
5995*fae548d3Szrj   tl->add(this, this->final_blocker_);
5996*fae548d3Szrj }
5997*fae548d3Szrj 
5998*fae548d3Szrj // Run the task--write out the data.
5999*fae548d3Szrj 
6000*fae548d3Szrj void
run(Workqueue *)6001*fae548d3Szrj Write_sections_task::run(Workqueue*)
6002*fae548d3Szrj {
6003*fae548d3Szrj   this->layout_->write_output_sections(this->of_);
6004*fae548d3Szrj }
6005*fae548d3Szrj 
6006*fae548d3Szrj // Write_data_task methods.
6007*fae548d3Szrj 
6008*fae548d3Szrj // We can always run this task.
6009*fae548d3Szrj 
6010*fae548d3Szrj Task_token*
is_runnable()6011*fae548d3Szrj Write_data_task::is_runnable()
6012*fae548d3Szrj {
6013*fae548d3Szrj   return NULL;
6014*fae548d3Szrj }
6015*fae548d3Szrj 
6016*fae548d3Szrj // We need to unlock FINAL_BLOCKER when finished.
6017*fae548d3Szrj 
6018*fae548d3Szrj void
locks(Task_locker * tl)6019*fae548d3Szrj Write_data_task::locks(Task_locker* tl)
6020*fae548d3Szrj {
6021*fae548d3Szrj   tl->add(this, this->final_blocker_);
6022*fae548d3Szrj }
6023*fae548d3Szrj 
6024*fae548d3Szrj // Run the task--write out the data.
6025*fae548d3Szrj 
6026*fae548d3Szrj void
run(Workqueue *)6027*fae548d3Szrj Write_data_task::run(Workqueue*)
6028*fae548d3Szrj {
6029*fae548d3Szrj   this->layout_->write_data(this->symtab_, this->of_);
6030*fae548d3Szrj }
6031*fae548d3Szrj 
6032*fae548d3Szrj // Write_symbols_task methods.
6033*fae548d3Szrj 
6034*fae548d3Szrj // We can always run this task.
6035*fae548d3Szrj 
6036*fae548d3Szrj Task_token*
is_runnable()6037*fae548d3Szrj Write_symbols_task::is_runnable()
6038*fae548d3Szrj {
6039*fae548d3Szrj   return NULL;
6040*fae548d3Szrj }
6041*fae548d3Szrj 
6042*fae548d3Szrj // We need to unlock FINAL_BLOCKER when finished.
6043*fae548d3Szrj 
6044*fae548d3Szrj void
locks(Task_locker * tl)6045*fae548d3Szrj Write_symbols_task::locks(Task_locker* tl)
6046*fae548d3Szrj {
6047*fae548d3Szrj   tl->add(this, this->final_blocker_);
6048*fae548d3Szrj }
6049*fae548d3Szrj 
6050*fae548d3Szrj // Run the task--write out the symbols.
6051*fae548d3Szrj 
6052*fae548d3Szrj void
run(Workqueue *)6053*fae548d3Szrj Write_symbols_task::run(Workqueue*)
6054*fae548d3Szrj {
6055*fae548d3Szrj   this->symtab_->write_globals(this->sympool_, this->dynpool_,
6056*fae548d3Szrj 			       this->layout_->symtab_xindex(),
6057*fae548d3Szrj 			       this->layout_->dynsym_xindex(), this->of_);
6058*fae548d3Szrj }
6059*fae548d3Szrj 
6060*fae548d3Szrj // Write_after_input_sections_task methods.
6061*fae548d3Szrj 
6062*fae548d3Szrj // We can only run this task after the input sections have completed.
6063*fae548d3Szrj 
6064*fae548d3Szrj Task_token*
is_runnable()6065*fae548d3Szrj Write_after_input_sections_task::is_runnable()
6066*fae548d3Szrj {
6067*fae548d3Szrj   if (this->input_sections_blocker_->is_blocked())
6068*fae548d3Szrj     return this->input_sections_blocker_;
6069*fae548d3Szrj   return NULL;
6070*fae548d3Szrj }
6071*fae548d3Szrj 
6072*fae548d3Szrj // We need to unlock FINAL_BLOCKER when finished.
6073*fae548d3Szrj 
6074*fae548d3Szrj void
locks(Task_locker * tl)6075*fae548d3Szrj Write_after_input_sections_task::locks(Task_locker* tl)
6076*fae548d3Szrj {
6077*fae548d3Szrj   tl->add(this, this->final_blocker_);
6078*fae548d3Szrj }
6079*fae548d3Szrj 
6080*fae548d3Szrj // Run the task.
6081*fae548d3Szrj 
6082*fae548d3Szrj void
run(Workqueue *)6083*fae548d3Szrj Write_after_input_sections_task::run(Workqueue*)
6084*fae548d3Szrj {
6085*fae548d3Szrj   this->layout_->write_sections_after_input_sections(this->of_);
6086*fae548d3Szrj }
6087*fae548d3Szrj 
6088*fae548d3Szrj // Build IDs can be computed as a "flat" sha1 or md5 of a string of bytes,
6089*fae548d3Szrj // or as a "tree" where each chunk of the string is hashed and then those
6090*fae548d3Szrj // hashes are put into a (much smaller) string which is hashed with sha1.
6091*fae548d3Szrj // We compute a checksum over the entire file because that is simplest.
6092*fae548d3Szrj 
6093*fae548d3Szrj void
run(Workqueue * workqueue,const Task *)6094*fae548d3Szrj Build_id_task_runner::run(Workqueue* workqueue, const Task*)
6095*fae548d3Szrj {
6096*fae548d3Szrj   Task_token* post_hash_tasks_blocker = new Task_token(true);
6097*fae548d3Szrj   const Layout* layout = this->layout_;
6098*fae548d3Szrj   Output_file* of = this->of_;
6099*fae548d3Szrj   const size_t filesize = (layout->output_file_size() <= 0 ? 0
6100*fae548d3Szrj 			   : static_cast<size_t>(layout->output_file_size()));
6101*fae548d3Szrj   unsigned char* array_of_hashes = NULL;
6102*fae548d3Szrj   size_t size_of_hashes = 0;
6103*fae548d3Szrj 
6104*fae548d3Szrj   if (strcmp(this->options_->build_id(), "tree") == 0
6105*fae548d3Szrj       && this->options_->build_id_chunk_size_for_treehash() > 0
6106*fae548d3Szrj       && filesize > 0
6107*fae548d3Szrj       && (filesize >= this->options_->build_id_min_file_size_for_treehash()))
6108*fae548d3Szrj     {
6109*fae548d3Szrj       static const size_t MD5_OUTPUT_SIZE_IN_BYTES = 16;
6110*fae548d3Szrj       const size_t chunk_size =
6111*fae548d3Szrj 	  this->options_->build_id_chunk_size_for_treehash();
6112*fae548d3Szrj       const size_t num_hashes = ((filesize - 1) / chunk_size) + 1;
6113*fae548d3Szrj       post_hash_tasks_blocker->add_blockers(num_hashes);
6114*fae548d3Szrj       size_of_hashes = num_hashes * MD5_OUTPUT_SIZE_IN_BYTES;
6115*fae548d3Szrj       array_of_hashes = new unsigned char[size_of_hashes];
6116*fae548d3Szrj       unsigned char *dst = array_of_hashes;
6117*fae548d3Szrj       for (size_t i = 0, src_offset = 0; i < num_hashes;
6118*fae548d3Szrj 	   i++, dst += MD5_OUTPUT_SIZE_IN_BYTES, src_offset += chunk_size)
6119*fae548d3Szrj 	{
6120*fae548d3Szrj 	  size_t size = std::min(chunk_size, filesize - src_offset);
6121*fae548d3Szrj 	  workqueue->queue(new Hash_task(of,
6122*fae548d3Szrj 					 src_offset,
6123*fae548d3Szrj 					 size,
6124*fae548d3Szrj 					 dst,
6125*fae548d3Szrj 					 post_hash_tasks_blocker));
6126*fae548d3Szrj 	}
6127*fae548d3Szrj     }
6128*fae548d3Szrj 
6129*fae548d3Szrj   // Queue the final task to write the build id and close the output file.
6130*fae548d3Szrj   workqueue->queue(new Task_function(new Close_task_runner(this->options_,
6131*fae548d3Szrj 							   layout,
6132*fae548d3Szrj 							   of,
6133*fae548d3Szrj 							   array_of_hashes,
6134*fae548d3Szrj 							   size_of_hashes),
6135*fae548d3Szrj 				     post_hash_tasks_blocker,
6136*fae548d3Szrj 				     "Task_function Close_task_runner"));
6137*fae548d3Szrj }
6138*fae548d3Szrj 
6139*fae548d3Szrj // Close_task_runner methods.
6140*fae548d3Szrj 
6141*fae548d3Szrj // Finish up the build ID computation, if necessary, and write a binary file,
6142*fae548d3Szrj // if necessary.  Then close the output file.
6143*fae548d3Szrj 
6144*fae548d3Szrj void
run(Workqueue *,const Task *)6145*fae548d3Szrj Close_task_runner::run(Workqueue*, const Task*)
6146*fae548d3Szrj {
6147*fae548d3Szrj   // At this point the multi-threaded part of the build ID computation,
6148*fae548d3Szrj   // if any, is done.  See Build_id_task_runner.
6149*fae548d3Szrj   this->layout_->write_build_id(this->of_, this->array_of_hashes_,
6150*fae548d3Szrj 				this->size_of_hashes_);
6151*fae548d3Szrj 
6152*fae548d3Szrj   // If we've been asked to create a binary file, we do so here.
6153*fae548d3Szrj   if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
6154*fae548d3Szrj     this->layout_->write_binary(this->of_);
6155*fae548d3Szrj 
6156*fae548d3Szrj   this->of_->close();
6157*fae548d3Szrj }
6158*fae548d3Szrj 
6159*fae548d3Szrj // Instantiate the templates we need.  We could use the configure
6160*fae548d3Szrj // script to restrict this to only the ones for implemented targets.
6161*fae548d3Szrj 
6162*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
6163*fae548d3Szrj template
6164*fae548d3Szrj Output_section*
6165*fae548d3Szrj Layout::init_fixed_output_section<32, false>(
6166*fae548d3Szrj     const char* name,
6167*fae548d3Szrj     elfcpp::Shdr<32, false>& shdr);
6168*fae548d3Szrj #endif
6169*fae548d3Szrj 
6170*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
6171*fae548d3Szrj template
6172*fae548d3Szrj Output_section*
6173*fae548d3Szrj Layout::init_fixed_output_section<32, true>(
6174*fae548d3Szrj     const char* name,
6175*fae548d3Szrj     elfcpp::Shdr<32, true>& shdr);
6176*fae548d3Szrj #endif
6177*fae548d3Szrj 
6178*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
6179*fae548d3Szrj template
6180*fae548d3Szrj Output_section*
6181*fae548d3Szrj Layout::init_fixed_output_section<64, false>(
6182*fae548d3Szrj     const char* name,
6183*fae548d3Szrj     elfcpp::Shdr<64, false>& shdr);
6184*fae548d3Szrj #endif
6185*fae548d3Szrj 
6186*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
6187*fae548d3Szrj template
6188*fae548d3Szrj Output_section*
6189*fae548d3Szrj Layout::init_fixed_output_section<64, true>(
6190*fae548d3Szrj     const char* name,
6191*fae548d3Szrj     elfcpp::Shdr<64, true>& shdr);
6192*fae548d3Szrj #endif
6193*fae548d3Szrj 
6194*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
6195*fae548d3Szrj template
6196*fae548d3Szrj Output_section*
6197*fae548d3Szrj Layout::layout<32, false>(Sized_relobj_file<32, false>* object,
6198*fae548d3Szrj 			  unsigned int shndx,
6199*fae548d3Szrj 			  const char* name,
6200*fae548d3Szrj 			  const elfcpp::Shdr<32, false>& shdr,
6201*fae548d3Szrj 			  unsigned int, unsigned int, unsigned int, off_t*);
6202*fae548d3Szrj #endif
6203*fae548d3Szrj 
6204*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
6205*fae548d3Szrj template
6206*fae548d3Szrj Output_section*
6207*fae548d3Szrj Layout::layout<32, true>(Sized_relobj_file<32, true>* object,
6208*fae548d3Szrj 			 unsigned int shndx,
6209*fae548d3Szrj 			 const char* name,
6210*fae548d3Szrj 			 const elfcpp::Shdr<32, true>& shdr,
6211*fae548d3Szrj 			 unsigned int, unsigned int, unsigned int, off_t*);
6212*fae548d3Szrj #endif
6213*fae548d3Szrj 
6214*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
6215*fae548d3Szrj template
6216*fae548d3Szrj Output_section*
6217*fae548d3Szrj Layout::layout<64, false>(Sized_relobj_file<64, false>* object,
6218*fae548d3Szrj 			  unsigned int shndx,
6219*fae548d3Szrj 			  const char* name,
6220*fae548d3Szrj 			  const elfcpp::Shdr<64, false>& shdr,
6221*fae548d3Szrj 			  unsigned int, unsigned int, unsigned int, off_t*);
6222*fae548d3Szrj #endif
6223*fae548d3Szrj 
6224*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
6225*fae548d3Szrj template
6226*fae548d3Szrj Output_section*
6227*fae548d3Szrj Layout::layout<64, true>(Sized_relobj_file<64, true>* object,
6228*fae548d3Szrj 			 unsigned int shndx,
6229*fae548d3Szrj 			 const char* name,
6230*fae548d3Szrj 			 const elfcpp::Shdr<64, true>& shdr,
6231*fae548d3Szrj 			 unsigned int, unsigned int, unsigned int, off_t*);
6232*fae548d3Szrj #endif
6233*fae548d3Szrj 
6234*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
6235*fae548d3Szrj template
6236*fae548d3Szrj Output_section*
6237*fae548d3Szrj Layout::layout_reloc<32, false>(Sized_relobj_file<32, false>* object,
6238*fae548d3Szrj 				unsigned int reloc_shndx,
6239*fae548d3Szrj 				const elfcpp::Shdr<32, false>& shdr,
6240*fae548d3Szrj 				Output_section* data_section,
6241*fae548d3Szrj 				Relocatable_relocs* rr);
6242*fae548d3Szrj #endif
6243*fae548d3Szrj 
6244*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
6245*fae548d3Szrj template
6246*fae548d3Szrj Output_section*
6247*fae548d3Szrj Layout::layout_reloc<32, true>(Sized_relobj_file<32, true>* object,
6248*fae548d3Szrj 			       unsigned int reloc_shndx,
6249*fae548d3Szrj 			       const elfcpp::Shdr<32, true>& shdr,
6250*fae548d3Szrj 			       Output_section* data_section,
6251*fae548d3Szrj 			       Relocatable_relocs* rr);
6252*fae548d3Szrj #endif
6253*fae548d3Szrj 
6254*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
6255*fae548d3Szrj template
6256*fae548d3Szrj Output_section*
6257*fae548d3Szrj Layout::layout_reloc<64, false>(Sized_relobj_file<64, false>* object,
6258*fae548d3Szrj 				unsigned int reloc_shndx,
6259*fae548d3Szrj 				const elfcpp::Shdr<64, false>& shdr,
6260*fae548d3Szrj 				Output_section* data_section,
6261*fae548d3Szrj 				Relocatable_relocs* rr);
6262*fae548d3Szrj #endif
6263*fae548d3Szrj 
6264*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
6265*fae548d3Szrj template
6266*fae548d3Szrj Output_section*
6267*fae548d3Szrj Layout::layout_reloc<64, true>(Sized_relobj_file<64, true>* object,
6268*fae548d3Szrj 			       unsigned int reloc_shndx,
6269*fae548d3Szrj 			       const elfcpp::Shdr<64, true>& shdr,
6270*fae548d3Szrj 			       Output_section* data_section,
6271*fae548d3Szrj 			       Relocatable_relocs* rr);
6272*fae548d3Szrj #endif
6273*fae548d3Szrj 
6274*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
6275*fae548d3Szrj template
6276*fae548d3Szrj void
6277*fae548d3Szrj Layout::layout_group<32, false>(Symbol_table* symtab,
6278*fae548d3Szrj 				Sized_relobj_file<32, false>* object,
6279*fae548d3Szrj 				unsigned int,
6280*fae548d3Szrj 				const char* group_section_name,
6281*fae548d3Szrj 				const char* signature,
6282*fae548d3Szrj 				const elfcpp::Shdr<32, false>& shdr,
6283*fae548d3Szrj 				elfcpp::Elf_Word flags,
6284*fae548d3Szrj 				std::vector<unsigned int>* shndxes);
6285*fae548d3Szrj #endif
6286*fae548d3Szrj 
6287*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
6288*fae548d3Szrj template
6289*fae548d3Szrj void
6290*fae548d3Szrj Layout::layout_group<32, true>(Symbol_table* symtab,
6291*fae548d3Szrj 			       Sized_relobj_file<32, true>* object,
6292*fae548d3Szrj 			       unsigned int,
6293*fae548d3Szrj 			       const char* group_section_name,
6294*fae548d3Szrj 			       const char* signature,
6295*fae548d3Szrj 			       const elfcpp::Shdr<32, true>& shdr,
6296*fae548d3Szrj 			       elfcpp::Elf_Word flags,
6297*fae548d3Szrj 			       std::vector<unsigned int>* shndxes);
6298*fae548d3Szrj #endif
6299*fae548d3Szrj 
6300*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
6301*fae548d3Szrj template
6302*fae548d3Szrj void
6303*fae548d3Szrj Layout::layout_group<64, false>(Symbol_table* symtab,
6304*fae548d3Szrj 				Sized_relobj_file<64, false>* object,
6305*fae548d3Szrj 				unsigned int,
6306*fae548d3Szrj 				const char* group_section_name,
6307*fae548d3Szrj 				const char* signature,
6308*fae548d3Szrj 				const elfcpp::Shdr<64, false>& shdr,
6309*fae548d3Szrj 				elfcpp::Elf_Word flags,
6310*fae548d3Szrj 				std::vector<unsigned int>* shndxes);
6311*fae548d3Szrj #endif
6312*fae548d3Szrj 
6313*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
6314*fae548d3Szrj template
6315*fae548d3Szrj void
6316*fae548d3Szrj Layout::layout_group<64, true>(Symbol_table* symtab,
6317*fae548d3Szrj 			       Sized_relobj_file<64, true>* object,
6318*fae548d3Szrj 			       unsigned int,
6319*fae548d3Szrj 			       const char* group_section_name,
6320*fae548d3Szrj 			       const char* signature,
6321*fae548d3Szrj 			       const elfcpp::Shdr<64, true>& shdr,
6322*fae548d3Szrj 			       elfcpp::Elf_Word flags,
6323*fae548d3Szrj 			       std::vector<unsigned int>* shndxes);
6324*fae548d3Szrj #endif
6325*fae548d3Szrj 
6326*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
6327*fae548d3Szrj template
6328*fae548d3Szrj Output_section*
6329*fae548d3Szrj Layout::layout_eh_frame<32, false>(Sized_relobj_file<32, false>* object,
6330*fae548d3Szrj 				   const unsigned char* symbols,
6331*fae548d3Szrj 				   off_t symbols_size,
6332*fae548d3Szrj 				   const unsigned char* symbol_names,
6333*fae548d3Szrj 				   off_t symbol_names_size,
6334*fae548d3Szrj 				   unsigned int shndx,
6335*fae548d3Szrj 				   const elfcpp::Shdr<32, false>& shdr,
6336*fae548d3Szrj 				   unsigned int reloc_shndx,
6337*fae548d3Szrj 				   unsigned int reloc_type,
6338*fae548d3Szrj 				   off_t* off);
6339*fae548d3Szrj #endif
6340*fae548d3Szrj 
6341*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
6342*fae548d3Szrj template
6343*fae548d3Szrj Output_section*
6344*fae548d3Szrj Layout::layout_eh_frame<32, true>(Sized_relobj_file<32, true>* object,
6345*fae548d3Szrj 				  const unsigned char* symbols,
6346*fae548d3Szrj 				  off_t symbols_size,
6347*fae548d3Szrj 				  const unsigned char* symbol_names,
6348*fae548d3Szrj 				  off_t symbol_names_size,
6349*fae548d3Szrj 				  unsigned int shndx,
6350*fae548d3Szrj 				  const elfcpp::Shdr<32, true>& shdr,
6351*fae548d3Szrj 				  unsigned int reloc_shndx,
6352*fae548d3Szrj 				  unsigned int reloc_type,
6353*fae548d3Szrj 				  off_t* off);
6354*fae548d3Szrj #endif
6355*fae548d3Szrj 
6356*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
6357*fae548d3Szrj template
6358*fae548d3Szrj Output_section*
6359*fae548d3Szrj Layout::layout_eh_frame<64, false>(Sized_relobj_file<64, false>* object,
6360*fae548d3Szrj 				   const unsigned char* symbols,
6361*fae548d3Szrj 				   off_t symbols_size,
6362*fae548d3Szrj 				   const unsigned char* symbol_names,
6363*fae548d3Szrj 				   off_t symbol_names_size,
6364*fae548d3Szrj 				   unsigned int shndx,
6365*fae548d3Szrj 				   const elfcpp::Shdr<64, false>& shdr,
6366*fae548d3Szrj 				   unsigned int reloc_shndx,
6367*fae548d3Szrj 				   unsigned int reloc_type,
6368*fae548d3Szrj 				   off_t* off);
6369*fae548d3Szrj #endif
6370*fae548d3Szrj 
6371*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
6372*fae548d3Szrj template
6373*fae548d3Szrj Output_section*
6374*fae548d3Szrj Layout::layout_eh_frame<64, true>(Sized_relobj_file<64, true>* object,
6375*fae548d3Szrj 				  const unsigned char* symbols,
6376*fae548d3Szrj 				  off_t symbols_size,
6377*fae548d3Szrj 				  const unsigned char* symbol_names,
6378*fae548d3Szrj 				  off_t symbol_names_size,
6379*fae548d3Szrj 				  unsigned int shndx,
6380*fae548d3Szrj 				  const elfcpp::Shdr<64, true>& shdr,
6381*fae548d3Szrj 				  unsigned int reloc_shndx,
6382*fae548d3Szrj 				  unsigned int reloc_type,
6383*fae548d3Szrj 				  off_t* off);
6384*fae548d3Szrj #endif
6385*fae548d3Szrj 
6386*fae548d3Szrj #ifdef HAVE_TARGET_32_LITTLE
6387*fae548d3Szrj template
6388*fae548d3Szrj void
6389*fae548d3Szrj Layout::add_to_gdb_index(bool is_type_unit,
6390*fae548d3Szrj 			 Sized_relobj<32, false>* object,
6391*fae548d3Szrj 			 const unsigned char* symbols,
6392*fae548d3Szrj 			 off_t symbols_size,
6393*fae548d3Szrj 			 unsigned int shndx,
6394*fae548d3Szrj 			 unsigned int reloc_shndx,
6395*fae548d3Szrj 			 unsigned int reloc_type);
6396*fae548d3Szrj #endif
6397*fae548d3Szrj 
6398*fae548d3Szrj #ifdef HAVE_TARGET_32_BIG
6399*fae548d3Szrj template
6400*fae548d3Szrj void
6401*fae548d3Szrj Layout::add_to_gdb_index(bool is_type_unit,
6402*fae548d3Szrj 			 Sized_relobj<32, true>* object,
6403*fae548d3Szrj 			 const unsigned char* symbols,
6404*fae548d3Szrj 			 off_t symbols_size,
6405*fae548d3Szrj 			 unsigned int shndx,
6406*fae548d3Szrj 			 unsigned int reloc_shndx,
6407*fae548d3Szrj 			 unsigned int reloc_type);
6408*fae548d3Szrj #endif
6409*fae548d3Szrj 
6410*fae548d3Szrj #ifdef HAVE_TARGET_64_LITTLE
6411*fae548d3Szrj template
6412*fae548d3Szrj void
6413*fae548d3Szrj Layout::add_to_gdb_index(bool is_type_unit,
6414*fae548d3Szrj 			 Sized_relobj<64, false>* object,
6415*fae548d3Szrj 			 const unsigned char* symbols,
6416*fae548d3Szrj 			 off_t symbols_size,
6417*fae548d3Szrj 			 unsigned int shndx,
6418*fae548d3Szrj 			 unsigned int reloc_shndx,
6419*fae548d3Szrj 			 unsigned int reloc_type);
6420*fae548d3Szrj #endif
6421*fae548d3Szrj 
6422*fae548d3Szrj #ifdef HAVE_TARGET_64_BIG
6423*fae548d3Szrj template
6424*fae548d3Szrj void
6425*fae548d3Szrj Layout::add_to_gdb_index(bool is_type_unit,
6426*fae548d3Szrj 			 Sized_relobj<64, true>* object,
6427*fae548d3Szrj 			 const unsigned char* symbols,
6428*fae548d3Szrj 			 off_t symbols_size,
6429*fae548d3Szrj 			 unsigned int shndx,
6430*fae548d3Szrj 			 unsigned int reloc_shndx,
6431*fae548d3Szrj 			 unsigned int reloc_type);
6432*fae548d3Szrj #endif
6433*fae548d3Szrj 
6434*fae548d3Szrj } // End namespace gold.
6435