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