Compress all debug sections.
[platform/upstream/binutils.git] / gold / layout.cc
1 // layout.cc -- lay out output file sections for gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26 #include <algorithm>
27 #include <iostream>
28 #include <utility>
29
30 #include "parameters.h"
31 #include "output.h"
32 #include "symtab.h"
33 #include "dynobj.h"
34 #include "ehframe.h"
35 #include "compressed_output.h"
36 #include "layout.h"
37
38 namespace gold
39 {
40
41 // Layout_task_runner methods.
42
43 // Lay out the sections.  This is called after all the input objects
44 // have been read.
45
46 void
47 Layout_task_runner::run(Workqueue* workqueue)
48 {
49   off_t file_size = this->layout_->finalize(this->input_objects_,
50                                             this->symtab_);
51
52   // Now we know the final size of the output file and we know where
53   // each piece of information goes.
54   Output_file* of = new Output_file(this->options_,
55                                     this->input_objects_->target());
56   of->open(file_size);
57
58   // Queue up the final set of tasks.
59   gold::queue_final_tasks(this->options_, this->input_objects_,
60                           this->symtab_, this->layout_, workqueue, of);
61 }
62
63 // Layout methods.
64
65 Layout::Layout(const General_options& options)
66   : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
67     section_name_map_(), segment_list_(), section_list_(),
68     unattached_section_list_(), special_output_list_(),
69     section_headers_(NULL), tls_segment_(NULL), symtab_section_(NULL),
70     dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL),
71     eh_frame_section_(NULL), output_file_size_(-1),
72     input_requires_executable_stack_(false),
73     input_with_gnu_stack_note_(false),
74     input_without_gnu_stack_note_(false)
75 {
76   // Make space for more than enough segments for a typical file.
77   // This is just for efficiency--it's OK if we wind up needing more.
78   this->segment_list_.reserve(12);
79
80   // We expect two unattached Output_data objects: the file header and
81   // the segment headers.
82   this->special_output_list_.reserve(2);
83 }
84
85 // Hash a key we use to look up an output section mapping.
86
87 size_t
88 Layout::Hash_key::operator()(const Layout::Key& k) const
89 {
90  return k.first + k.second.first + k.second.second;
91 }
92
93 // Return whether PREFIX is a prefix of STR.
94
95 static inline bool
96 is_prefix_of(const char* prefix, const char* str)
97 {
98   return strncmp(prefix, str, strlen(prefix)) == 0;
99 }
100
101 // Returns whether the given section is in the list of
102 // debug-sections-used-by-some-version-of-gdb.  Currently,
103 // we've checked versions of gdb up to and including 6.7.1.
104
105 static const char* gdb_sections[] =
106 { ".debug_abbrev",
107   // ".debug_aranges",   // not used by gdb as of 6.7.1
108   ".debug_frame",
109   ".debug_info",
110   ".debug_line",
111   ".debug_loc",
112   ".debug_macinfo",
113   // ".debug_pubnames",  // not used by gdb as of 6.7.1
114   ".debug_ranges",
115   ".debug_str",
116 };
117
118 static inline bool
119 is_gdb_debug_section(const char* str)
120 {
121   // We can do this faster: binary search or a hashtable.  But why bother?
122   for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
123     if (strcmp(str, gdb_sections[i]) == 0)
124       return true;
125   return false;
126 }
127
128 // Whether to include this section in the link.
129
130 template<int size, bool big_endian>
131 bool
132 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
133                         const elfcpp::Shdr<size, big_endian>& shdr)
134 {
135   // Some section types are never linked.  Some are only linked when
136   // doing a relocateable link.
137   switch (shdr.get_sh_type())
138     {
139     case elfcpp::SHT_NULL:
140     case elfcpp::SHT_SYMTAB:
141     case elfcpp::SHT_DYNSYM:
142     case elfcpp::SHT_STRTAB:
143     case elfcpp::SHT_HASH:
144     case elfcpp::SHT_DYNAMIC:
145     case elfcpp::SHT_SYMTAB_SHNDX:
146       return false;
147
148     case elfcpp::SHT_RELA:
149     case elfcpp::SHT_REL:
150     case elfcpp::SHT_GROUP:
151       return parameters->output_is_object();
152
153     case elfcpp::SHT_PROGBITS:
154       if (parameters->strip_debug()
155           && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
156         {
157           // Debugging sections can only be recognized by name.
158           if (is_prefix_of(".debug", name)
159               || is_prefix_of(".gnu.linkonce.wi.", name)
160               || is_prefix_of(".line", name)
161               || is_prefix_of(".stab", name))
162             return false;
163         }
164       if (parameters->strip_debug_gdb()
165           && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
166         {
167           // Debugging sections can only be recognized by name.
168           if (is_prefix_of(".debug", name)
169               && !is_gdb_debug_section(name))
170             return false;
171         }
172       return true;
173
174     default:
175       return true;
176     }
177 }
178
179 // Return an output section named NAME, or NULL if there is none.
180
181 Output_section*
182 Layout::find_output_section(const char* name) const
183 {
184   for (Section_name_map::const_iterator p = this->section_name_map_.begin();
185        p != this->section_name_map_.end();
186        ++p)
187     if (strcmp(p->second->name(), name) == 0)
188       return p->second;
189   return NULL;
190 }
191
192 // Return an output segment of type TYPE, with segment flags SET set
193 // and segment flags CLEAR clear.  Return NULL if there is none.
194
195 Output_segment*
196 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
197                             elfcpp::Elf_Word clear) const
198 {
199   for (Segment_list::const_iterator p = this->segment_list_.begin();
200        p != this->segment_list_.end();
201        ++p)
202     if (static_cast<elfcpp::PT>((*p)->type()) == type
203         && ((*p)->flags() & set) == set
204         && ((*p)->flags() & clear) == 0)
205       return *p;
206   return NULL;
207 }
208
209 // Return the output section to use for section NAME with type TYPE
210 // and section flags FLAGS.
211
212 Output_section*
213 Layout::get_output_section(const char* name, Stringpool::Key name_key,
214                            elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
215 {
216   // We should ignore some flags.
217   flags &= ~ (elfcpp::SHF_INFO_LINK
218               | elfcpp::SHF_LINK_ORDER
219               | elfcpp::SHF_GROUP
220               | elfcpp::SHF_MERGE
221               | elfcpp::SHF_STRINGS);
222
223   const Key key(name_key, std::make_pair(type, flags));
224   const std::pair<Key, Output_section*> v(key, NULL);
225   std::pair<Section_name_map::iterator, bool> ins(
226     this->section_name_map_.insert(v));
227
228   if (!ins.second)
229     return ins.first->second;
230   else
231     {
232       // This is the first time we've seen this name/type/flags
233       // combination.
234       Output_section* os = this->make_output_section(name, type, flags);
235       ins.first->second = os;
236       return os;
237     }
238 }
239
240 // Return the output section to use for input section SHNDX, with name
241 // NAME, with header HEADER, from object OBJECT.  RELOC_SHNDX is the
242 // index of a relocation section which applies to this section, or 0
243 // if none, or -1U if more than one.  RELOC_TYPE is the type of the
244 // relocation section if there is one.  Set *OFF to the offset of this
245 // input section without the output section.  Return NULL if the
246 // section should be discarded.  Set *OFF to -1 if the section
247 // contents should not be written directly to the output file, but
248 // will instead receive special handling.
249
250 template<int size, bool big_endian>
251 Output_section*
252 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
253                const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
254                unsigned int reloc_shndx, unsigned int, off_t* off)
255 {
256   if (!this->include_section(object, name, shdr))
257     return NULL;
258
259   // If we are not doing a relocateable link, choose the name to use
260   // for the output section.
261   size_t len = strlen(name);
262   if (!parameters->output_is_object())
263     name = Layout::output_section_name(name, &len);
264
265   // FIXME: Handle SHF_OS_NONCONFORMING here.
266
267   // Canonicalize the section name.
268   Stringpool::Key name_key;
269   name = this->namepool_.add_prefix(name, len, &name_key);
270
271   // Find the output section.  The output section is selected based on
272   // the section name, type, and flags.
273   Output_section* os = this->get_output_section(name, name_key,
274                                                 shdr.get_sh_type(),
275                                                 shdr.get_sh_flags());
276
277   // FIXME: Handle SHF_LINK_ORDER somewhere.
278
279   *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx);
280
281   return os;
282 }
283
284 // Special GNU handling of sections name .eh_frame.  They will
285 // normally hold exception frame data as defined by the C++ ABI
286 // (http://codesourcery.com/cxx-abi/).
287
288 template<int size, bool big_endian>
289 Output_section*
290 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
291                         const unsigned char* symbols,
292                         off_t symbols_size,
293                         const unsigned char* symbol_names,
294                         off_t symbol_names_size,
295                         unsigned int shndx,
296                         const elfcpp::Shdr<size, big_endian>& shdr,
297                         unsigned int reloc_shndx, unsigned int reloc_type,
298                         off_t* off)
299 {
300   gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
301   gold_assert(shdr.get_sh_flags() == elfcpp::SHF_ALLOC);
302
303   Stringpool::Key name_key;
304   const char* name = this->namepool_.add(".eh_frame", false, &name_key);
305
306   Output_section* os = this->get_output_section(name, name_key,
307                                                 elfcpp::SHT_PROGBITS,
308                                                 elfcpp::SHF_ALLOC);
309
310   if (this->eh_frame_section_ == NULL)
311     {
312       this->eh_frame_section_ = os;
313       this->eh_frame_data_ = new Eh_frame();
314       os->add_output_section_data(this->eh_frame_data_);
315
316       if (this->options_.create_eh_frame_hdr())
317         {
318           Stringpool::Key hdr_name_key;
319           const char* hdr_name = this->namepool_.add(".eh_frame_hdr",
320                                                      false,
321                                                      &hdr_name_key);
322           Output_section* hdr_os =
323             this->get_output_section(hdr_name, hdr_name_key,
324                                      elfcpp::SHT_PROGBITS,
325                                      elfcpp::SHF_ALLOC);
326
327           Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os, this->eh_frame_data_);
328           hdr_os->add_output_section_data(hdr_posd);
329
330           hdr_os->set_after_input_sections();
331
332           Output_segment* hdr_oseg =
333             new Output_segment(elfcpp::PT_GNU_EH_FRAME, elfcpp::PF_R);
334           this->segment_list_.push_back(hdr_oseg);
335           hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
336
337           this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
338         }
339     }
340
341   gold_assert(this->eh_frame_section_ == os);
342
343   if (this->eh_frame_data_->add_ehframe_input_section(object,
344                                                       symbols,
345                                                       symbols_size,
346                                                       symbol_names,
347                                                       symbol_names_size,
348                                                       shndx,
349                                                       reloc_shndx,
350                                                       reloc_type))
351     *off = -1;
352   else
353     {
354       // We couldn't handle this .eh_frame section for some reason.
355       // Add it as a normal section.
356       *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx);
357     }
358
359   return os;
360 }
361
362 // Add POSD to an output section using NAME, TYPE, and FLAGS.
363
364 void
365 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
366                                 elfcpp::Elf_Xword flags,
367                                 Output_section_data* posd)
368 {
369   // Canonicalize the name.
370   Stringpool::Key name_key;
371   name = this->namepool_.add(name, true, &name_key);
372
373   Output_section* os = this->get_output_section(name, name_key, type, flags);
374   os->add_output_section_data(posd);
375 }
376
377 // Map section flags to segment flags.
378
379 elfcpp::Elf_Word
380 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
381 {
382   elfcpp::Elf_Word ret = elfcpp::PF_R;
383   if ((flags & elfcpp::SHF_WRITE) != 0)
384     ret |= elfcpp::PF_W;
385   if ((flags & elfcpp::SHF_EXECINSTR) != 0)
386     ret |= elfcpp::PF_X;
387   return ret;
388 }
389
390 // Sometimes we compress sections.  This is typically done for
391 // sections that are not part of normal program execution (such as
392 // .debug_* sections), and where the readers of these sections know
393 // how to deal with compressed sections.  (To make it easier for them,
394 // we will rename the ouput section in such cases from .foo to
395 // .foo.zlib.nnnn, where nnnn is the uncompressed size.)  This routine
396 // doesn't say for certain whether we'll compress -- it depends on
397 // commandline options as well -- just whether this section is a
398 // candidate for compression.
399
400 static bool
401 is_compressible_debug_section(const char* secname)
402 {
403   return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
404 }
405
406 // Make a new Output_section, and attach it to segments as
407 // appropriate.
408
409 Output_section*
410 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
411                             elfcpp::Elf_Xword flags)
412 {
413   Output_section* os;
414   if ((flags & elfcpp::SHF_ALLOC) == 0
415       && this->options_.compress_debug_sections()
416       && is_compressible_debug_section(name))
417     os = new Output_compressed_section(&this->options_, name, type, flags);
418   else
419     os = new Output_section(name, type, flags);
420
421   this->section_list_.push_back(os);
422
423   if ((flags & elfcpp::SHF_ALLOC) == 0)
424     this->unattached_section_list_.push_back(os);
425   else
426     {
427       // This output section goes into a PT_LOAD segment.
428
429       elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
430
431       // The only thing we really care about for PT_LOAD segments is
432       // whether or not they are writable, so that is how we search
433       // for them.  People who need segments sorted on some other
434       // basis will have to wait until we implement a mechanism for
435       // them to describe the segments they want.
436
437       Segment_list::const_iterator p;
438       for (p = this->segment_list_.begin();
439            p != this->segment_list_.end();
440            ++p)
441         {
442           if ((*p)->type() == elfcpp::PT_LOAD
443               && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
444             {
445               (*p)->add_output_section(os, seg_flags);
446               break;
447             }
448         }
449
450       if (p == this->segment_list_.end())
451         {
452           Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
453                                                     seg_flags);
454           this->segment_list_.push_back(oseg);
455           oseg->add_output_section(os, seg_flags);
456         }
457
458       // If we see a loadable SHT_NOTE section, we create a PT_NOTE
459       // segment.
460       if (type == elfcpp::SHT_NOTE)
461         {
462           // See if we already have an equivalent PT_NOTE segment.
463           for (p = this->segment_list_.begin();
464                p != segment_list_.end();
465                ++p)
466             {
467               if ((*p)->type() == elfcpp::PT_NOTE
468                   && (((*p)->flags() & elfcpp::PF_W)
469                       == (seg_flags & elfcpp::PF_W)))
470                 {
471                   (*p)->add_output_section(os, seg_flags);
472                   break;
473                 }
474             }
475
476           if (p == this->segment_list_.end())
477             {
478               Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
479                                                         seg_flags);
480               this->segment_list_.push_back(oseg);
481               oseg->add_output_section(os, seg_flags);
482             }
483         }
484
485       // If we see a loadable SHF_TLS section, we create a PT_TLS
486       // segment.  There can only be one such segment.
487       if ((flags & elfcpp::SHF_TLS) != 0)
488         {
489           if (this->tls_segment_ == NULL)
490             {
491               this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
492                                                       seg_flags);
493               this->segment_list_.push_back(this->tls_segment_);
494             }
495           this->tls_segment_->add_output_section(os, seg_flags);
496         }
497     }
498
499   return os;
500 }
501
502 // Handle the .note.GNU-stack section at layout time.  SEEN_GNU_STACK
503 // is whether we saw a .note.GNU-stack section in the object file.
504 // GNU_STACK_FLAGS is the section flags.  The flags give the
505 // protection required for stack memory.  We record this in an
506 // executable as a PT_GNU_STACK segment.  If an object file does not
507 // have a .note.GNU-stack segment, we must assume that it is an old
508 // object.  On some targets that will force an executable stack.
509
510 void
511 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
512 {
513   if (!seen_gnu_stack)
514     this->input_without_gnu_stack_note_ = true;
515   else
516     {
517       this->input_with_gnu_stack_note_ = true;
518       if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
519         this->input_requires_executable_stack_ = true;
520     }
521 }
522
523 // Create the dynamic sections which are needed before we read the
524 // relocs.
525
526 void
527 Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
528                                         Symbol_table* symtab)
529 {
530   if (parameters->doing_static_link())
531     return;
532
533   const char* dynamic_name = this->namepool_.add(".dynamic", false, NULL);
534   this->dynamic_section_ = this->make_output_section(dynamic_name,
535                                                      elfcpp::SHT_DYNAMIC,
536                                                      (elfcpp::SHF_ALLOC
537                                                       | elfcpp::SHF_WRITE));
538
539   symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
540                                 this->dynamic_section_, 0, 0,
541                                 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
542                                 elfcpp::STV_HIDDEN, 0, false, false);
543
544   this->dynamic_data_ =  new Output_data_dynamic(&this->dynpool_);
545
546   this->dynamic_section_->add_output_section_data(this->dynamic_data_);
547 }
548
549 // For each output section whose name can be represented as C symbol,
550 // define __start and __stop symbols for the section.  This is a GNU
551 // extension.
552
553 void
554 Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
555 {
556   for (Section_list::const_iterator p = this->section_list_.begin();
557        p != this->section_list_.end();
558        ++p)
559     {
560       const char* const name = (*p)->name();
561       if (name[strspn(name,
562                       ("0123456789"
563                        "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
564                        "abcdefghijklmnopqrstuvwxyz"
565                        "_"))]
566           == '\0')
567         {
568           const std::string name_string(name);
569           const std::string start_name("__start_" + name_string);
570           const std::string stop_name("__stop_" + name_string);
571
572           symtab->define_in_output_data(target,
573                                         start_name.c_str(),
574                                         NULL, // version
575                                         *p,
576                                         0, // value
577                                         0, // symsize
578                                         elfcpp::STT_NOTYPE,
579                                         elfcpp::STB_GLOBAL,
580                                         elfcpp::STV_DEFAULT,
581                                         0, // nonvis
582                                         false, // offset_is_from_end
583                                         false); // only_if_ref
584
585           symtab->define_in_output_data(target,
586                                         stop_name.c_str(),
587                                         NULL, // version
588                                         *p,
589                                         0, // value
590                                         0, // symsize
591                                         elfcpp::STT_NOTYPE,
592                                         elfcpp::STB_GLOBAL,
593                                         elfcpp::STV_DEFAULT,
594                                         0, // nonvis
595                                         true, // offset_is_from_end
596                                         false); // only_if_ref
597         }
598     }
599 }
600
601 // Find the first read-only PT_LOAD segment, creating one if
602 // necessary.
603
604 Output_segment*
605 Layout::find_first_load_seg()
606 {
607   for (Segment_list::const_iterator p = this->segment_list_.begin();
608        p != this->segment_list_.end();
609        ++p)
610     {
611       if ((*p)->type() == elfcpp::PT_LOAD
612           && ((*p)->flags() & elfcpp::PF_R) != 0
613           && ((*p)->flags() & elfcpp::PF_W) == 0)
614         return *p;
615     }
616
617   Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
618   this->segment_list_.push_back(load_seg);
619   return load_seg;
620 }
621
622 // Finalize the layout.  When this is called, we have created all the
623 // output sections and all the output segments which are based on
624 // input sections.  We have several things to do, and we have to do
625 // them in the right order, so that we get the right results correctly
626 // and efficiently.
627
628 // 1) Finalize the list of output segments and create the segment
629 // table header.
630
631 // 2) Finalize the dynamic symbol table and associated sections.
632
633 // 3) Determine the final file offset of all the output segments.
634
635 // 4) Determine the final file offset of all the SHF_ALLOC output
636 // sections.
637
638 // 5) Create the symbol table sections and the section name table
639 // section.
640
641 // 6) Finalize the symbol table: set symbol values to their final
642 // value and make a final determination of which symbols are going
643 // into the output symbol table.
644
645 // 7) Create the section table header.
646
647 // 8) Determine the final file offset of all the output sections which
648 // are not SHF_ALLOC, including the section table header.
649
650 // 9) Finalize the ELF file header.
651
652 // This function returns the size of the output file.
653
654 off_t
655 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
656 {
657   Target* const target = input_objects->target();
658
659   target->finalize_sections(this);
660
661   this->create_gold_note();
662   this->create_executable_stack_info(target);
663
664   Output_segment* phdr_seg = NULL;
665   if (!parameters->doing_static_link())
666     {
667       // There was a dynamic object in the link.  We need to create
668       // some information for the dynamic linker.
669
670       // Create the PT_PHDR segment which will hold the program
671       // headers.
672       phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
673       this->segment_list_.push_back(phdr_seg);
674
675       // Create the dynamic symbol table, including the hash table.
676       Output_section* dynstr;
677       std::vector<Symbol*> dynamic_symbols;
678       unsigned int local_dynamic_count;
679       Versions versions;
680       this->create_dynamic_symtab(target, symtab, &dynstr,
681                                   &local_dynamic_count, &dynamic_symbols,
682                                   &versions);
683
684       // Create the .interp section to hold the name of the
685       // interpreter, and put it in a PT_INTERP segment.
686       if (!parameters->output_is_shared())
687         this->create_interp(target);
688
689       // Finish the .dynamic section to hold the dynamic data, and put
690       // it in a PT_DYNAMIC segment.
691       this->finish_dynamic_section(input_objects, symtab);
692
693       // We should have added everything we need to the dynamic string
694       // table.
695       this->dynpool_.set_string_offsets();
696
697       // Create the version sections.  We can't do this until the
698       // dynamic string table is complete.
699       this->create_version_sections(&versions, symtab, local_dynamic_count,
700                                     dynamic_symbols, dynstr);
701     }
702
703   // FIXME: Handle PT_GNU_STACK.
704
705   Output_segment* load_seg = this->find_first_load_seg();
706
707   // Lay out the segment headers.
708   Output_segment_headers* segment_headers;
709   segment_headers = new Output_segment_headers(this->segment_list_);
710   load_seg->add_initial_output_data(segment_headers);
711   this->special_output_list_.push_back(segment_headers);
712   if (phdr_seg != NULL)
713     phdr_seg->add_initial_output_data(segment_headers);
714
715   // Lay out the file header.
716   Output_file_header* file_header;
717   file_header = new Output_file_header(target, symtab, segment_headers);
718   load_seg->add_initial_output_data(file_header);
719   this->special_output_list_.push_back(file_header);
720
721   // We set the output section indexes in set_segment_offsets and
722   // set_section_indexes.
723   unsigned int shndx = 1;
724
725   // Set the file offsets of all the segments, and all the sections
726   // they contain.
727   off_t off = this->set_segment_offsets(target, load_seg, &shndx);
728
729   // Create the symbol table sections.
730   this->create_symtab_sections(input_objects, symtab, &off);
731
732   // Create the .shstrtab section.
733   Output_section* shstrtab_section = this->create_shstrtab();
734
735   // Set the file offsets of all the non-data sections which don't
736   // have to wait for the input sections.
737   off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
738
739   // Now that all sections have been created, set the section indexes.
740   shndx = this->set_section_indexes(shndx);
741
742   // Create the section table header.
743   this->create_shdrs(&off);
744
745   file_header->set_section_info(this->section_headers_, shstrtab_section);
746
747   // Now we know exactly where everything goes in the output file
748   // (except for non-allocated sections which require postprocessing).
749   Output_data::layout_complete();
750
751   this->output_file_size_ = off;
752
753   return off;
754 }
755
756 // Create a .note section for an executable or shared library.  This
757 // records the version of gold used to create the binary.
758
759 void
760 Layout::create_gold_note()
761 {
762   if (parameters->output_is_object())
763     return;
764
765   // Authorities all agree that the values in a .note field should
766   // be aligned on 4-byte boundaries for 32-bit binaries.  However,
767   // they differ on what the alignment is for 64-bit binaries.
768   // The GABI says unambiguously they take 8-byte alignment:
769   //    http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
770   // Other documentation says alignment should always be 4 bytes:
771   //    http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
772   // GNU ld and GNU readelf both support the latter (at least as of
773   // version 2.16.91), and glibc always generates the latter for
774   // .note.ABI-tag (as of version 1.6), so that's the one we go with
775   // here.
776 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION   // This is not defined by default.
777   const int size = parameters->get_size();
778 #else
779   const int size = 32;
780 #endif
781
782   // The contents of the .note section.
783   const char* name = "GNU";
784   std::string desc(std::string("gold ") + gold::get_version_string());
785   size_t namesz = strlen(name) + 1;
786   size_t aligned_namesz = align_address(namesz, size / 8);
787   size_t descsz = desc.length() + 1;
788   size_t aligned_descsz = align_address(descsz, size / 8);
789   const int note_type = 4;
790
791   size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz;
792
793   unsigned char buffer[128];
794   gold_assert(sizeof buffer >= notesz);
795   memset(buffer, 0, notesz);
796
797   bool is_big_endian = parameters->is_big_endian();
798
799   if (size == 32)
800     {
801       if (!is_big_endian)
802         {
803           elfcpp::Swap<32, false>::writeval(buffer, namesz);
804           elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
805           elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
806         }
807       else
808         {
809           elfcpp::Swap<32, true>::writeval(buffer, namesz);
810           elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
811           elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
812         }
813     }
814   else if (size == 64)
815     {
816       if (!is_big_endian)
817         {
818           elfcpp::Swap<64, false>::writeval(buffer, namesz);
819           elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
820           elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
821         }
822       else
823         {
824           elfcpp::Swap<64, true>::writeval(buffer, namesz);
825           elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
826           elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
827         }
828     }
829   else
830     gold_unreachable();
831
832   memcpy(buffer + 3 * (size / 8), name, namesz);
833   memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz);
834
835   const char* note_name = this->namepool_.add(".note", false, NULL);
836   Output_section* os = this->make_output_section(note_name,
837                                                  elfcpp::SHT_NOTE,
838                                                  0);
839   Output_section_data* posd = new Output_data_const(buffer, notesz,
840                                                     size / 8);
841   os->add_output_section_data(posd);
842 }
843
844 // Record whether the stack should be executable.  This can be set
845 // from the command line using the -z execstack or -z noexecstack
846 // options.  Otherwise, if any input file has a .note.GNU-stack
847 // section with the SHF_EXECINSTR flag set, the stack should be
848 // executable.  Otherwise, if at least one input file a
849 // .note.GNU-stack section, and some input file has no .note.GNU-stack
850 // section, we use the target default for whether the stack should be
851 // executable.  Otherwise, we don't generate a stack note.  When
852 // generating a object file, we create a .note.GNU-stack section with
853 // the appropriate marking.  When generating an executable or shared
854 // library, we create a PT_GNU_STACK segment.
855
856 void
857 Layout::create_executable_stack_info(const Target* target)
858 {
859   bool is_stack_executable;
860   if (this->options_.is_execstack_set())
861     is_stack_executable = this->options_.is_stack_executable();
862   else if (!this->input_with_gnu_stack_note_)
863     return;
864   else
865     {
866       if (this->input_requires_executable_stack_)
867         is_stack_executable = true;
868       else if (this->input_without_gnu_stack_note_)
869         is_stack_executable = target->is_default_stack_executable();
870       else
871         is_stack_executable = false;
872     }
873
874   if (parameters->output_is_object())
875     {
876       const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
877       elfcpp::Elf_Xword flags = 0;
878       if (is_stack_executable)
879         flags |= elfcpp::SHF_EXECINSTR;
880       this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
881     }
882   else
883     {
884       int flags = elfcpp::PF_R | elfcpp::PF_W;
885       if (is_stack_executable)
886         flags |= elfcpp::PF_X;
887       Output_segment* oseg = new Output_segment(elfcpp::PT_GNU_STACK, flags);
888       this->segment_list_.push_back(oseg);
889     }
890 }
891
892 // Return whether SEG1 should be before SEG2 in the output file.  This
893 // is based entirely on the segment type and flags.  When this is
894 // called the segment addresses has normally not yet been set.
895
896 bool
897 Layout::segment_precedes(const Output_segment* seg1,
898                          const Output_segment* seg2)
899 {
900   elfcpp::Elf_Word type1 = seg1->type();
901   elfcpp::Elf_Word type2 = seg2->type();
902
903   // The single PT_PHDR segment is required to precede any loadable
904   // segment.  We simply make it always first.
905   if (type1 == elfcpp::PT_PHDR)
906     {
907       gold_assert(type2 != elfcpp::PT_PHDR);
908       return true;
909     }
910   if (type2 == elfcpp::PT_PHDR)
911     return false;
912
913   // The single PT_INTERP segment is required to precede any loadable
914   // segment.  We simply make it always second.
915   if (type1 == elfcpp::PT_INTERP)
916     {
917       gold_assert(type2 != elfcpp::PT_INTERP);
918       return true;
919     }
920   if (type2 == elfcpp::PT_INTERP)
921     return false;
922
923   // We then put PT_LOAD segments before any other segments.
924   if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
925     return true;
926   if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
927     return false;
928
929   // We put the PT_TLS segment last, because that is where the dynamic
930   // linker expects to find it (this is just for efficiency; other
931   // positions would also work correctly).
932   if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
933     return false;
934   if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
935     return true;
936
937   const elfcpp::Elf_Word flags1 = seg1->flags();
938   const elfcpp::Elf_Word flags2 = seg2->flags();
939
940   // The order of non-PT_LOAD segments is unimportant.  We simply sort
941   // by the numeric segment type and flags values.  There should not
942   // be more than one segment with the same type and flags.
943   if (type1 != elfcpp::PT_LOAD)
944     {
945       if (type1 != type2)
946         return type1 < type2;
947       gold_assert(flags1 != flags2);
948       return flags1 < flags2;
949     }
950
951   // We sort PT_LOAD segments based on the flags.  Readonly segments
952   // come before writable segments.  Then executable segments come
953   // before non-executable segments.  Then the unlikely case of a
954   // non-readable segment comes before the normal case of a readable
955   // segment.  If there are multiple segments with the same type and
956   // flags, we require that the address be set, and we sort by
957   // virtual address and then physical address.
958   if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
959     return (flags1 & elfcpp::PF_W) == 0;
960   if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
961     return (flags1 & elfcpp::PF_X) != 0;
962   if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
963     return (flags1 & elfcpp::PF_R) == 0;
964
965   uint64_t vaddr1 = seg1->vaddr();
966   uint64_t vaddr2 = seg2->vaddr();
967   if (vaddr1 != vaddr2)
968     return vaddr1 < vaddr2;
969
970   uint64_t paddr1 = seg1->paddr();
971   uint64_t paddr2 = seg2->paddr();
972   gold_assert(paddr1 != paddr2);
973   return paddr1 < paddr2;
974 }
975
976 // Set the file offsets of all the segments, and all the sections they
977 // contain.  They have all been created.  LOAD_SEG must be be laid out
978 // first.  Return the offset of the data to follow.
979
980 off_t
981 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
982                             unsigned int *pshndx)
983 {
984   // Sort them into the final order.
985   std::sort(this->segment_list_.begin(), this->segment_list_.end(),
986             Layout::Compare_segments());
987
988   // Find the PT_LOAD segments, and set their addresses and offsets
989   // and their section's addresses and offsets.
990   uint64_t addr;
991   if (options_.user_set_text_segment_address())
992     addr = options_.text_segment_address();
993   else
994     addr = target->default_text_segment_address();
995   off_t off = 0;
996   bool was_readonly = false;
997   for (Segment_list::iterator p = this->segment_list_.begin();
998        p != this->segment_list_.end();
999        ++p)
1000     {
1001       if ((*p)->type() == elfcpp::PT_LOAD)
1002         {
1003           if (load_seg != NULL && load_seg != *p)
1004             gold_unreachable();
1005           load_seg = NULL;
1006
1007           // If the last segment was readonly, and this one is not,
1008           // then skip the address forward one page, maintaining the
1009           // same position within the page.  This lets us store both
1010           // segments overlapping on a single page in the file, but
1011           // the loader will put them on different pages in memory.
1012
1013           uint64_t orig_addr = addr;
1014           uint64_t orig_off = off;
1015
1016           uint64_t aligned_addr = addr;
1017           uint64_t abi_pagesize = target->abi_pagesize();
1018
1019           // FIXME: This should depend on the -n and -N options.
1020           (*p)->set_minimum_addralign(target->common_pagesize());
1021
1022           if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
1023             {
1024               uint64_t align = (*p)->addralign();
1025
1026               addr = align_address(addr, align);
1027               aligned_addr = addr;
1028               if ((addr & (abi_pagesize - 1)) != 0)
1029                 addr = addr + abi_pagesize;
1030             }
1031
1032           unsigned int shndx_hold = *pshndx;
1033           off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1034           uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
1035
1036           // Now that we know the size of this segment, we may be able
1037           // to save a page in memory, at the cost of wasting some
1038           // file space, by instead aligning to the start of a new
1039           // page.  Here we use the real machine page size rather than
1040           // the ABI mandated page size.
1041
1042           if (aligned_addr != addr)
1043             {
1044               uint64_t common_pagesize = target->common_pagesize();
1045               uint64_t first_off = (common_pagesize
1046                                     - (aligned_addr
1047                                        & (common_pagesize - 1)));
1048               uint64_t last_off = new_addr & (common_pagesize - 1);
1049               if (first_off > 0
1050                   && last_off > 0
1051                   && ((aligned_addr & ~ (common_pagesize - 1))
1052                       != (new_addr & ~ (common_pagesize - 1)))
1053                   && first_off + last_off <= common_pagesize)
1054                 {
1055                   *pshndx = shndx_hold;
1056                   addr = align_address(aligned_addr, common_pagesize);
1057                   off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1058                   new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
1059                 }
1060             }
1061
1062           addr = new_addr;
1063
1064           if (((*p)->flags() & elfcpp::PF_W) == 0)
1065             was_readonly = true;
1066         }
1067     }
1068
1069   // Handle the non-PT_LOAD segments, setting their offsets from their
1070   // section's offsets.
1071   for (Segment_list::iterator p = this->segment_list_.begin();
1072        p != this->segment_list_.end();
1073        ++p)
1074     {
1075       if ((*p)->type() != elfcpp::PT_LOAD)
1076         (*p)->set_offset();
1077     }
1078
1079   return off;
1080 }
1081
1082 // Set the file offset of all the sections not associated with a
1083 // segment.
1084
1085 off_t
1086 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
1087 {
1088   for (Section_list::iterator p = this->unattached_section_list_.begin();
1089        p != this->unattached_section_list_.end();
1090        ++p)
1091     {
1092       // The symtab section is handled in create_symtab_sections.
1093       if (*p == this->symtab_section_)
1094         continue;
1095
1096       if (pass == BEFORE_INPUT_SECTIONS_PASS
1097           && (*p)->requires_postprocessing())
1098         (*p)->create_postprocessing_buffer();
1099
1100       if (pass == BEFORE_INPUT_SECTIONS_PASS
1101           && (*p)->after_input_sections())
1102         continue;
1103       else if (pass == AFTER_INPUT_SECTIONS_PASS
1104                && (!(*p)->after_input_sections()
1105                    || (*p)->type() == elfcpp::SHT_STRTAB))
1106         continue;
1107       else if (pass == STRTAB_AFTER_INPUT_SECTIONS_PASS
1108                && (!(*p)->after_input_sections()
1109                    || (*p)->type() != elfcpp::SHT_STRTAB))
1110         continue;
1111
1112       off = align_address(off, (*p)->addralign());
1113       (*p)->set_file_offset(off);
1114       (*p)->finalize_data_size();
1115       off += (*p)->data_size();
1116
1117       // At this point the name must be set.
1118       if (pass != STRTAB_AFTER_INPUT_SECTIONS_PASS)
1119         this->namepool_.add((*p)->name(), false, NULL);
1120     }
1121   return off;
1122 }
1123
1124 // Set the section indexes of all the sections not associated with a
1125 // segment.
1126
1127 unsigned int
1128 Layout::set_section_indexes(unsigned int shndx)
1129 {
1130   for (Section_list::iterator p = this->unattached_section_list_.begin();
1131        p != this->unattached_section_list_.end();
1132        ++p)
1133     {
1134       (*p)->set_out_shndx(shndx);
1135       ++shndx;
1136     }
1137   return shndx;
1138 }
1139
1140 // Create the symbol table sections.  Here we also set the final
1141 // values of the symbols.  At this point all the loadable sections are
1142 // fully laid out.
1143
1144 void
1145 Layout::create_symtab_sections(const Input_objects* input_objects,
1146                                Symbol_table* symtab,
1147                                off_t* poff)
1148 {
1149   int symsize;
1150   unsigned int align;
1151   if (parameters->get_size() == 32)
1152     {
1153       symsize = elfcpp::Elf_sizes<32>::sym_size;
1154       align = 4;
1155     }
1156   else if (parameters->get_size() == 64)
1157     {
1158       symsize = elfcpp::Elf_sizes<64>::sym_size;
1159       align = 8;
1160     }
1161   else
1162     gold_unreachable();
1163
1164   off_t off = *poff;
1165   off = align_address(off, align);
1166   off_t startoff = off;
1167
1168   // Save space for the dummy symbol at the start of the section.  We
1169   // never bother to write this out--it will just be left as zero.
1170   off += symsize;
1171   unsigned int local_symbol_index = 1;
1172
1173   // Add STT_SECTION symbols for each Output section which needs one.
1174   for (Section_list::iterator p = this->section_list_.begin();
1175        p != this->section_list_.end();
1176        ++p)
1177     {
1178       if (!(*p)->needs_symtab_index())
1179         (*p)->set_symtab_index(-1U);
1180       else
1181         {
1182           (*p)->set_symtab_index(local_symbol_index);
1183           ++local_symbol_index;
1184           off += symsize;
1185         }
1186     }
1187
1188   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1189        p != input_objects->relobj_end();
1190        ++p)
1191     {
1192       Task_lock_obj<Object> tlo(**p);
1193       unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
1194                                                         off,
1195                                                         &this->sympool_);
1196       off += (index - local_symbol_index) * symsize;
1197       local_symbol_index = index;
1198     }
1199
1200   unsigned int local_symcount = local_symbol_index;
1201   gold_assert(local_symcount * symsize == off - startoff);
1202
1203   off_t dynoff;
1204   size_t dyn_global_index;
1205   size_t dyncount;
1206   if (this->dynsym_section_ == NULL)
1207     {
1208       dynoff = 0;
1209       dyn_global_index = 0;
1210       dyncount = 0;
1211     }
1212   else
1213     {
1214       dyn_global_index = this->dynsym_section_->info();
1215       off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
1216       dynoff = this->dynsym_section_->offset() + locsize;
1217       dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
1218       gold_assert(static_cast<off_t>(dyncount * symsize)
1219                   == this->dynsym_section_->data_size() - locsize);
1220     }
1221
1222   off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
1223                          dyncount, &this->sympool_);
1224
1225   if (!parameters->strip_all())
1226     {
1227       this->sympool_.set_string_offsets();
1228
1229       const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
1230       Output_section* osymtab = this->make_output_section(symtab_name,
1231                                                           elfcpp::SHT_SYMTAB,
1232                                                           0);
1233       this->symtab_section_ = osymtab;
1234
1235       Output_section_data* pos = new Output_data_fixed_space(off - startoff,
1236                                                              align);
1237       osymtab->add_output_section_data(pos);
1238
1239       const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
1240       Output_section* ostrtab = this->make_output_section(strtab_name,
1241                                                           elfcpp::SHT_STRTAB,
1242                                                           0);
1243
1244       Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1245       ostrtab->add_output_section_data(pstr);
1246
1247       osymtab->set_file_offset(startoff);
1248       osymtab->finalize_data_size();
1249       osymtab->set_link_section(ostrtab);
1250       osymtab->set_info(local_symcount);
1251       osymtab->set_entsize(symsize);
1252
1253       *poff = off;
1254     }
1255 }
1256
1257 // Create the .shstrtab section, which holds the names of the
1258 // sections.  At the time this is called, we have created all the
1259 // output sections except .shstrtab itself.
1260
1261 Output_section*
1262 Layout::create_shstrtab()
1263 {
1264   // FIXME: We don't need to create a .shstrtab section if we are
1265   // stripping everything.
1266
1267   const char* name = this->namepool_.add(".shstrtab", false, NULL);
1268
1269   Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
1270
1271   // We can't write out this section until we've set all the section
1272   // names, and we don't set the names of compressed output sections
1273   // until relocations are complete.
1274   os->set_after_input_sections();
1275
1276   Output_section_data* posd = new Output_data_strtab(&this->namepool_);
1277   os->add_output_section_data(posd);
1278
1279   return os;
1280 }
1281
1282 // Create the section headers.  SIZE is 32 or 64.  OFF is the file
1283 // offset.
1284
1285 void
1286 Layout::create_shdrs(off_t* poff)
1287 {
1288   Output_section_headers* oshdrs;
1289   oshdrs = new Output_section_headers(this,
1290                                       &this->segment_list_,
1291                                       &this->unattached_section_list_,
1292                                       &this->namepool_);
1293   off_t off = align_address(*poff, oshdrs->addralign());
1294   oshdrs->set_address_and_file_offset(0, off);
1295   off += oshdrs->data_size();
1296   *poff = off;
1297   this->section_headers_ = oshdrs;
1298 }
1299
1300 // Create the dynamic symbol table.
1301
1302 void
1303 Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
1304                               Output_section **pdynstr,
1305                               unsigned int* plocal_dynamic_count,
1306                               std::vector<Symbol*>* pdynamic_symbols,
1307                               Versions* pversions)
1308 {
1309   // Count all the symbols in the dynamic symbol table, and set the
1310   // dynamic symbol indexes.
1311
1312   // Skip symbol 0, which is always all zeroes.
1313   unsigned int index = 1;
1314
1315   // Add STT_SECTION symbols for each Output section which needs one.
1316   for (Section_list::iterator p = this->section_list_.begin();
1317        p != this->section_list_.end();
1318        ++p)
1319     {
1320       if (!(*p)->needs_dynsym_index())
1321         (*p)->set_dynsym_index(-1U);
1322       else
1323         {
1324           (*p)->set_dynsym_index(index);
1325           ++index;
1326         }
1327     }
1328
1329   // FIXME: Some targets apparently require local symbols in the
1330   // dynamic symbol table.  Here is where we will have to count them,
1331   // and set the dynamic symbol indexes, and add the names to
1332   // this->dynpool_.
1333
1334   unsigned int local_symcount = index;
1335   *plocal_dynamic_count = local_symcount;
1336
1337   // FIXME: We have to tell set_dynsym_indexes whether the
1338   // -E/--export-dynamic option was used.
1339   index = symtab->set_dynsym_indexes(target, index, pdynamic_symbols,
1340                                      &this->dynpool_, pversions);
1341
1342   int symsize;
1343   unsigned int align;
1344   const int size = parameters->get_size();
1345   if (size == 32)
1346     {
1347       symsize = elfcpp::Elf_sizes<32>::sym_size;
1348       align = 4;
1349     }
1350   else if (size == 64)
1351     {
1352       symsize = elfcpp::Elf_sizes<64>::sym_size;
1353       align = 8;
1354     }
1355   else
1356     gold_unreachable();
1357
1358   // Create the dynamic symbol table section.
1359
1360   const char* dynsym_name = this->namepool_.add(".dynsym", false, NULL);
1361   Output_section* dynsym = this->make_output_section(dynsym_name,
1362                                                      elfcpp::SHT_DYNSYM,
1363                                                      elfcpp::SHF_ALLOC);
1364
1365   Output_section_data* odata = new Output_data_fixed_space(index * symsize,
1366                                                            align);
1367   dynsym->add_output_section_data(odata);
1368
1369   dynsym->set_info(local_symcount);
1370   dynsym->set_entsize(symsize);
1371   dynsym->set_addralign(align);
1372
1373   this->dynsym_section_ = dynsym;
1374
1375   Output_data_dynamic* const odyn = this->dynamic_data_;
1376   odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1377   odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1378
1379   // Create the dynamic string table section.
1380
1381   const char* dynstr_name = this->namepool_.add(".dynstr", false, NULL);
1382   Output_section* dynstr = this->make_output_section(dynstr_name,
1383                                                      elfcpp::SHT_STRTAB,
1384                                                      elfcpp::SHF_ALLOC);
1385
1386   Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1387   dynstr->add_output_section_data(strdata);
1388
1389   dynsym->set_link_section(dynstr);
1390   this->dynamic_section_->set_link_section(dynstr);
1391
1392   odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1393   odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1394
1395   *pdynstr = dynstr;
1396
1397   // Create the hash tables.
1398
1399   // FIXME: We need an option to create a GNU hash table.
1400
1401   unsigned char* phash;
1402   unsigned int hashlen;
1403   Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
1404                                 &phash, &hashlen);
1405
1406   const char* hash_name = this->namepool_.add(".hash", false, NULL);
1407   Output_section* hashsec = this->make_output_section(hash_name,
1408                                                       elfcpp::SHT_HASH,
1409                                                       elfcpp::SHF_ALLOC);
1410
1411   Output_section_data* hashdata = new Output_data_const_buffer(phash,
1412                                                                hashlen,
1413                                                                align);
1414   hashsec->add_output_section_data(hashdata);
1415
1416   hashsec->set_link_section(dynsym);
1417   hashsec->set_entsize(4);
1418
1419   odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1420 }
1421
1422 // Create the version sections.
1423
1424 void
1425 Layout::create_version_sections(const Versions* versions,
1426                                 const Symbol_table* symtab,
1427                                 unsigned int local_symcount,
1428                                 const std::vector<Symbol*>& dynamic_symbols,
1429                                 const Output_section* dynstr)
1430 {
1431   if (!versions->any_defs() && !versions->any_needs())
1432     return;
1433
1434   if (parameters->get_size() == 32)
1435     {
1436       if (parameters->is_big_endian())
1437         {
1438 #ifdef HAVE_TARGET_32_BIG
1439           this->sized_create_version_sections
1440               SELECT_SIZE_ENDIAN_NAME(32, true)(
1441                   versions, symtab, local_symcount, dynamic_symbols, dynstr
1442                   SELECT_SIZE_ENDIAN(32, true));
1443 #else
1444           gold_unreachable();
1445 #endif
1446         }
1447       else
1448         {
1449 #ifdef HAVE_TARGET_32_LITTLE
1450           this->sized_create_version_sections
1451               SELECT_SIZE_ENDIAN_NAME(32, false)(
1452                   versions, symtab, local_symcount, dynamic_symbols, dynstr
1453                   SELECT_SIZE_ENDIAN(32, false));
1454 #else
1455           gold_unreachable();
1456 #endif
1457         }
1458     }
1459   else if (parameters->get_size() == 64)
1460     {
1461       if (parameters->is_big_endian())
1462         {
1463 #ifdef HAVE_TARGET_64_BIG
1464           this->sized_create_version_sections
1465               SELECT_SIZE_ENDIAN_NAME(64, true)(
1466                   versions, symtab, local_symcount, dynamic_symbols, dynstr
1467                   SELECT_SIZE_ENDIAN(64, true));
1468 #else
1469           gold_unreachable();
1470 #endif
1471         }
1472       else
1473         {
1474 #ifdef HAVE_TARGET_64_LITTLE
1475           this->sized_create_version_sections
1476               SELECT_SIZE_ENDIAN_NAME(64, false)(
1477                   versions, symtab, local_symcount, dynamic_symbols, dynstr
1478                   SELECT_SIZE_ENDIAN(64, false));
1479 #else
1480           gold_unreachable();
1481 #endif
1482         }
1483     }
1484   else
1485     gold_unreachable();
1486 }
1487
1488 // Create the version sections, sized version.
1489
1490 template<int size, bool big_endian>
1491 void
1492 Layout::sized_create_version_sections(
1493     const Versions* versions,
1494     const Symbol_table* symtab,
1495     unsigned int local_symcount,
1496     const std::vector<Symbol*>& dynamic_symbols,
1497     const Output_section* dynstr
1498     ACCEPT_SIZE_ENDIAN)
1499 {
1500   const char* vname = this->namepool_.add(".gnu.version", false, NULL);
1501   Output_section* vsec = this->make_output_section(vname,
1502                                                    elfcpp::SHT_GNU_versym,
1503                                                    elfcpp::SHF_ALLOC);
1504
1505   unsigned char* vbuf;
1506   unsigned int vsize;
1507   versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1508       symtab, &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1509       SELECT_SIZE_ENDIAN(size, big_endian));
1510
1511   Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1512
1513   vsec->add_output_section_data(vdata);
1514   vsec->set_entsize(2);
1515   vsec->set_link_section(this->dynsym_section_);
1516
1517   Output_data_dynamic* const odyn = this->dynamic_data_;
1518   odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1519
1520   if (versions->any_defs())
1521     {
1522       const char* vdname = this->namepool_.add(".gnu.version_d", false, NULL);
1523       Output_section *vdsec;
1524       vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1525                                         elfcpp::SHF_ALLOC);
1526
1527       unsigned char* vdbuf;
1528       unsigned int vdsize;
1529       unsigned int vdentries;
1530       versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1531           &this->dynpool_, &vdbuf, &vdsize, &vdentries
1532           SELECT_SIZE_ENDIAN(size, big_endian));
1533
1534       Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1535                                                                  vdsize,
1536                                                                  4);
1537
1538       vdsec->add_output_section_data(vddata);
1539       vdsec->set_link_section(dynstr);
1540       vdsec->set_info(vdentries);
1541
1542       odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1543       odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1544     }
1545
1546   if (versions->any_needs())
1547     {
1548       const char* vnname = this->namepool_.add(".gnu.version_r", false, NULL);
1549       Output_section* vnsec;
1550       vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1551                                         elfcpp::SHF_ALLOC);
1552
1553       unsigned char* vnbuf;
1554       unsigned int vnsize;
1555       unsigned int vnentries;
1556       versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1557         (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1558          SELECT_SIZE_ENDIAN(size, big_endian));
1559
1560       Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1561                                                                  vnsize,
1562                                                                  4);
1563
1564       vnsec->add_output_section_data(vndata);
1565       vnsec->set_link_section(dynstr);
1566       vnsec->set_info(vnentries);
1567
1568       odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1569       odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1570     }
1571 }
1572
1573 // Create the .interp section and PT_INTERP segment.
1574
1575 void
1576 Layout::create_interp(const Target* target)
1577 {
1578   const char* interp = this->options_.dynamic_linker();
1579   if (interp == NULL)
1580     {
1581       interp = target->dynamic_linker();
1582       gold_assert(interp != NULL);
1583     }
1584
1585   size_t len = strlen(interp) + 1;
1586
1587   Output_section_data* odata = new Output_data_const(interp, len, 1);
1588
1589   const char* interp_name = this->namepool_.add(".interp", false, NULL);
1590   Output_section* osec = this->make_output_section(interp_name,
1591                                                    elfcpp::SHT_PROGBITS,
1592                                                    elfcpp::SHF_ALLOC);
1593   osec->add_output_section_data(odata);
1594
1595   Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1596   this->segment_list_.push_back(oseg);
1597   oseg->add_initial_output_section(osec, elfcpp::PF_R);
1598 }
1599
1600 // Finish the .dynamic section and PT_DYNAMIC segment.
1601
1602 void
1603 Layout::finish_dynamic_section(const Input_objects* input_objects,
1604                                const Symbol_table* symtab)
1605 {
1606   Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1607                                             elfcpp::PF_R | elfcpp::PF_W);
1608   this->segment_list_.push_back(oseg);
1609   oseg->add_initial_output_section(this->dynamic_section_,
1610                                    elfcpp::PF_R | elfcpp::PF_W);
1611
1612   Output_data_dynamic* const odyn = this->dynamic_data_;
1613
1614   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1615        p != input_objects->dynobj_end();
1616        ++p)
1617     {
1618       // FIXME: Handle --as-needed.
1619       odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1620     }
1621
1622   // FIXME: Support --init and --fini.
1623   Symbol* sym = symtab->lookup("_init");
1624   if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1625     odyn->add_symbol(elfcpp::DT_INIT, sym);
1626
1627   sym = symtab->lookup("_fini");
1628   if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1629     odyn->add_symbol(elfcpp::DT_FINI, sym);
1630
1631   // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1632
1633   // Add a DT_RPATH entry if needed.
1634   const General_options::Dir_list& rpath(this->options_.rpath());
1635   if (!rpath.empty())
1636     {
1637       std::string rpath_val;
1638       for (General_options::Dir_list::const_iterator p = rpath.begin();
1639            p != rpath.end();
1640            ++p)
1641         {
1642           if (rpath_val.empty())
1643             rpath_val = p->name();
1644           else
1645             {
1646               // Eliminate duplicates.
1647               General_options::Dir_list::const_iterator q;
1648               for (q = rpath.begin(); q != p; ++q)
1649                 if (q->name() == p->name())
1650                   break;
1651               if (q == p)
1652                 {
1653                   rpath_val += ':';
1654                   rpath_val += p->name();
1655                 }
1656             }
1657         }
1658
1659       odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1660     }
1661
1662   // Look for text segments that have dynamic relocations.
1663   bool have_textrel = false;
1664   for (Segment_list::const_iterator p = this->segment_list_.begin();
1665        p != this->segment_list_.end();
1666        ++p)
1667     {
1668       if (((*p)->flags() & elfcpp::PF_W) == 0
1669           && (*p)->dynamic_reloc_count() > 0)
1670         {
1671           have_textrel = true;
1672           break;
1673         }
1674     }
1675
1676   // Add a DT_FLAGS entry. We add it even if no flags are set so that
1677   // post-link tools can easily modify these flags if desired.
1678   unsigned int flags = 0;
1679   if (have_textrel)
1680     flags |= elfcpp::DF_TEXTREL;
1681   odyn->add_constant(elfcpp::DT_FLAGS, flags);
1682 }
1683
1684 // The mapping of .gnu.linkonce section names to real section names.
1685
1686 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1687 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1688 {
1689   MAPPING_INIT("d.rel.ro", ".data.rel.ro"),     // Must be before "d".
1690   MAPPING_INIT("t", ".text"),
1691   MAPPING_INIT("r", ".rodata"),
1692   MAPPING_INIT("d", ".data"),
1693   MAPPING_INIT("b", ".bss"),
1694   MAPPING_INIT("s", ".sdata"),
1695   MAPPING_INIT("sb", ".sbss"),
1696   MAPPING_INIT("s2", ".sdata2"),
1697   MAPPING_INIT("sb2", ".sbss2"),
1698   MAPPING_INIT("wi", ".debug_info"),
1699   MAPPING_INIT("td", ".tdata"),
1700   MAPPING_INIT("tb", ".tbss"),
1701   MAPPING_INIT("lr", ".lrodata"),
1702   MAPPING_INIT("l", ".ldata"),
1703   MAPPING_INIT("lb", ".lbss"),
1704 };
1705 #undef MAPPING_INIT
1706
1707 const int Layout::linkonce_mapping_count =
1708   sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1709
1710 // Return the name of the output section to use for a .gnu.linkonce
1711 // section.  This is based on the default ELF linker script of the old
1712 // GNU linker.  For example, we map a name like ".gnu.linkonce.t.foo"
1713 // to ".text".  Set *PLEN to the length of the name.  *PLEN is
1714 // initialized to the length of NAME.
1715
1716 const char*
1717 Layout::linkonce_output_name(const char* name, size_t *plen)
1718 {
1719   const char* s = name + sizeof(".gnu.linkonce") - 1;
1720   if (*s != '.')
1721     return name;
1722   ++s;
1723   const Linkonce_mapping* plm = linkonce_mapping;
1724   for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1725     {
1726       if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1727         {
1728           *plen = plm->tolen;
1729           return plm->to;
1730         }
1731     }
1732   return name;
1733 }
1734
1735 // Choose the output section name to use given an input section name.
1736 // Set *PLEN to the length of the name.  *PLEN is initialized to the
1737 // length of NAME.
1738
1739 const char*
1740 Layout::output_section_name(const char* name, size_t* plen)
1741 {
1742   if (Layout::is_linkonce(name))
1743     {
1744       // .gnu.linkonce sections are laid out as though they were named
1745       // for the sections are placed into.
1746       return Layout::linkonce_output_name(name, plen);
1747     }
1748
1749   // gcc 4.3 generates the following sorts of section names when it
1750   // needs a section name specific to a function:
1751   //   .text.FN
1752   //   .rodata.FN
1753   //   .sdata2.FN
1754   //   .data.FN
1755   //   .data.rel.FN
1756   //   .data.rel.local.FN
1757   //   .data.rel.ro.FN
1758   //   .data.rel.ro.local.FN
1759   //   .sdata.FN
1760   //   .bss.FN
1761   //   .sbss.FN
1762   //   .tdata.FN
1763   //   .tbss.FN
1764
1765   // The GNU linker maps all of those to the part before the .FN,
1766   // except that .data.rel.local.FN is mapped to .data, and
1767   // .data.rel.ro.local.FN is mapped to .data.rel.ro.  The sections
1768   // beginning with .data.rel.ro.local are grouped together.
1769
1770   // For an anonymous namespace, the string FN can contain a '.'.
1771
1772   // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
1773   // GNU linker maps to .rodata.
1774
1775   // The .data.rel.ro sections enable a security feature triggered by
1776   // the -z relro option.  Section which need to be relocated at
1777   // program startup time but which may be readonly after startup are
1778   // grouped into .data.rel.ro.  They are then put into a PT_GNU_RELRO
1779   // segment.  The dynamic linker will make that segment writable,
1780   // perform relocations, and then make it read-only.  FIXME: We do
1781   // not yet implement this optimization.
1782
1783   // It is hard to handle this in a principled way.
1784
1785   // These are the rules we follow:
1786
1787   // If the section name has no initial '.', or no dot other than an
1788   // initial '.', we use the name unchanged (i.e., "mysection" and
1789   // ".text" are unchanged).
1790
1791   // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
1792
1793   // Otherwise, we drop the second '.' and everything that comes after
1794   // it (i.e., ".text.XXX" becomes ".text").
1795
1796   const char* s = name;
1797   if (*s != '.')
1798     return name;
1799   ++s;
1800   const char* sdot = strchr(s, '.');
1801   if (sdot == NULL)
1802     return name;
1803
1804   const char* const data_rel_ro = ".data.rel.ro";
1805   if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
1806     {
1807       *plen = strlen(data_rel_ro);
1808       return data_rel_ro;
1809     }
1810
1811   *plen = sdot - name;
1812   return name;
1813 }
1814
1815 // Record the signature of a comdat section, and return whether to
1816 // include it in the link.  If GROUP is true, this is a regular
1817 // section group.  If GROUP is false, this is a group signature
1818 // derived from the name of a linkonce section.  We want linkonce
1819 // signatures and group signatures to block each other, but we don't
1820 // want a linkonce signature to block another linkonce signature.
1821
1822 bool
1823 Layout::add_comdat(const char* signature, bool group)
1824 {
1825   std::string sig(signature);
1826   std::pair<Signatures::iterator, bool> ins(
1827     this->signatures_.insert(std::make_pair(sig, group)));
1828
1829   if (ins.second)
1830     {
1831       // This is the first time we've seen this signature.
1832       return true;
1833     }
1834
1835   if (ins.first->second)
1836     {
1837       // We've already seen a real section group with this signature.
1838       return false;
1839     }
1840   else if (group)
1841     {
1842       // This is a real section group, and we've already seen a
1843       // linkonce section with this signature.  Record that we've seen
1844       // a section group, and don't include this section group.
1845       ins.first->second = true;
1846       return false;
1847     }
1848   else
1849     {
1850       // We've already seen a linkonce section and this is a linkonce
1851       // section.  These don't block each other--this may be the same
1852       // symbol name with different section types.
1853       return true;
1854     }
1855 }
1856
1857 // Write out the Output_sections.  Most won't have anything to write,
1858 // since most of the data will come from input sections which are
1859 // handled elsewhere.  But some Output_sections do have Output_data.
1860
1861 void
1862 Layout::write_output_sections(Output_file* of) const
1863 {
1864   for (Section_list::const_iterator p = this->section_list_.begin();
1865        p != this->section_list_.end();
1866        ++p)
1867     {
1868       if (!(*p)->after_input_sections())
1869         (*p)->write(of);
1870     }
1871 }
1872
1873 // Write out data not associated with a section or the symbol table.
1874
1875 void
1876 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
1877 {
1878   if (!parameters->strip_all())
1879     {
1880       const Output_section* symtab_section = this->symtab_section_;
1881       for (Section_list::const_iterator p = this->section_list_.begin();
1882            p != this->section_list_.end();
1883            ++p)
1884         {
1885           if ((*p)->needs_symtab_index())
1886             {
1887               gold_assert(symtab_section != NULL);
1888               unsigned int index = (*p)->symtab_index();
1889               gold_assert(index > 0 && index != -1U);
1890               off_t off = (symtab_section->offset()
1891                            + index * symtab_section->entsize());
1892               symtab->write_section_symbol(*p, of, off);
1893             }
1894         }
1895     }
1896
1897   const Output_section* dynsym_section = this->dynsym_section_;
1898   for (Section_list::const_iterator p = this->section_list_.begin();
1899        p != this->section_list_.end();
1900        ++p)
1901     {
1902       if ((*p)->needs_dynsym_index())
1903         {
1904           gold_assert(dynsym_section != NULL);
1905           unsigned int index = (*p)->dynsym_index();
1906           gold_assert(index > 0 && index != -1U);
1907           off_t off = (dynsym_section->offset()
1908                        + index * dynsym_section->entsize());
1909           symtab->write_section_symbol(*p, of, off);
1910         }
1911     }
1912
1913   // Write out the Output_data which are not in an Output_section.
1914   for (Data_list::const_iterator p = this->special_output_list_.begin();
1915        p != this->special_output_list_.end();
1916        ++p)
1917     (*p)->write(of);
1918 }
1919
1920 // Write out the Output_sections which can only be written after the
1921 // input sections are complete.
1922
1923 void
1924 Layout::write_sections_after_input_sections(Output_file* of)
1925 {
1926   // Determine the final section offsets, and thus the final output
1927   // file size.  Note we finalize the .shstrab last, to allow the
1928   // after_input_section sections to modify their section-names before
1929   // writing.
1930   off_t off = this->output_file_size_;
1931   off = this->set_section_offsets(off, AFTER_INPUT_SECTIONS_PASS);
1932
1933   // Now that we've finalized the names, we can finalize the shstrab.
1934   off = this->set_section_offsets(off, STRTAB_AFTER_INPUT_SECTIONS_PASS);
1935
1936   if (off > this->output_file_size_)
1937     {
1938       of->resize(off);
1939       this->output_file_size_ = off;
1940     }
1941
1942   for (Section_list::const_iterator p = this->section_list_.begin();
1943        p != this->section_list_.end();
1944        ++p)
1945     {
1946       if ((*p)->after_input_sections())
1947         (*p)->write(of);
1948     }
1949
1950   for (Section_list::const_iterator p = this->unattached_section_list_.begin();
1951        p != this->unattached_section_list_.end();
1952        ++p)
1953     {
1954       if ((*p)->after_input_sections())
1955         (*p)->write(of);
1956     }
1957
1958   this->section_headers_->write(of);
1959 }
1960
1961 // Write_sections_task methods.
1962
1963 // We can always run this task.
1964
1965 Task::Is_runnable_type
1966 Write_sections_task::is_runnable(Workqueue*)
1967 {
1968   return IS_RUNNABLE;
1969 }
1970
1971 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
1972 // when finished.
1973
1974 class Write_sections_task::Write_sections_locker : public Task_locker
1975 {
1976  public:
1977   Write_sections_locker(Task_token& output_sections_blocker,
1978                         Task_token& final_blocker,
1979                         Workqueue* workqueue)
1980     : output_sections_block_(output_sections_blocker, workqueue),
1981       final_block_(final_blocker, workqueue)
1982   { }
1983
1984  private:
1985   Task_block_token output_sections_block_;
1986   Task_block_token final_block_;
1987 };
1988
1989 Task_locker*
1990 Write_sections_task::locks(Workqueue* workqueue)
1991 {
1992   return new Write_sections_locker(*this->output_sections_blocker_,
1993                                    *this->final_blocker_,
1994                                    workqueue);
1995 }
1996
1997 // Run the task--write out the data.
1998
1999 void
2000 Write_sections_task::run(Workqueue*)
2001 {
2002   this->layout_->write_output_sections(this->of_);
2003 }
2004
2005 // Write_data_task methods.
2006
2007 // We can always run this task.
2008
2009 Task::Is_runnable_type
2010 Write_data_task::is_runnable(Workqueue*)
2011 {
2012   return IS_RUNNABLE;
2013 }
2014
2015 // We need to unlock FINAL_BLOCKER when finished.
2016
2017 Task_locker*
2018 Write_data_task::locks(Workqueue* workqueue)
2019 {
2020   return new Task_locker_block(*this->final_blocker_, workqueue);
2021 }
2022
2023 // Run the task--write out the data.
2024
2025 void
2026 Write_data_task::run(Workqueue*)
2027 {
2028   this->layout_->write_data(this->symtab_, this->of_);
2029 }
2030
2031 // Write_symbols_task methods.
2032
2033 // We can always run this task.
2034
2035 Task::Is_runnable_type
2036 Write_symbols_task::is_runnable(Workqueue*)
2037 {
2038   return IS_RUNNABLE;
2039 }
2040
2041 // We need to unlock FINAL_BLOCKER when finished.
2042
2043 Task_locker*
2044 Write_symbols_task::locks(Workqueue* workqueue)
2045 {
2046   return new Task_locker_block(*this->final_blocker_, workqueue);
2047 }
2048
2049 // Run the task--write out the symbols.
2050
2051 void
2052 Write_symbols_task::run(Workqueue*)
2053 {
2054   this->symtab_->write_globals(this->input_objects_, this->sympool_,
2055                                this->dynpool_, this->of_);
2056 }
2057
2058 // Write_after_input_sections_task methods.
2059
2060 // We can only run this task after the input sections have completed.
2061
2062 Task::Is_runnable_type
2063 Write_after_input_sections_task::is_runnable(Workqueue*)
2064 {
2065   if (this->input_sections_blocker_->is_blocked())
2066     return IS_BLOCKED;
2067   return IS_RUNNABLE;
2068 }
2069
2070 // We need to unlock FINAL_BLOCKER when finished.
2071
2072 Task_locker*
2073 Write_after_input_sections_task::locks(Workqueue* workqueue)
2074 {
2075   return new Task_locker_block(*this->final_blocker_, workqueue);
2076 }
2077
2078 // Run the task.
2079
2080 void
2081 Write_after_input_sections_task::run(Workqueue*)
2082 {
2083   this->layout_->write_sections_after_input_sections(this->of_);
2084 }
2085
2086 // Close_task_runner methods.
2087
2088 // Run the task--close the file.
2089
2090 void
2091 Close_task_runner::run(Workqueue*)
2092 {
2093   this->of_->close();
2094 }
2095
2096 // Instantiate the templates we need.  We could use the configure
2097 // script to restrict this to only the ones for implemented targets.
2098
2099 #ifdef HAVE_TARGET_32_LITTLE
2100 template
2101 Output_section*
2102 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
2103                           const char* name,
2104                           const elfcpp::Shdr<32, false>& shdr,
2105                           unsigned int, unsigned int, off_t*);
2106 #endif
2107
2108 #ifdef HAVE_TARGET_32_BIG
2109 template
2110 Output_section*
2111 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
2112                          const char* name,
2113                          const elfcpp::Shdr<32, true>& shdr,
2114                          unsigned int, unsigned int, off_t*);
2115 #endif
2116
2117 #ifdef HAVE_TARGET_64_LITTLE
2118 template
2119 Output_section*
2120 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
2121                           const char* name,
2122                           const elfcpp::Shdr<64, false>& shdr,
2123                           unsigned int, unsigned int, off_t*);
2124 #endif
2125
2126 #ifdef HAVE_TARGET_64_BIG
2127 template
2128 Output_section*
2129 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
2130                          const char* name,
2131                          const elfcpp::Shdr<64, true>& shdr,
2132                          unsigned int, unsigned int, off_t*);
2133 #endif
2134
2135 #ifdef HAVE_TARGET_32_LITTLE
2136 template
2137 Output_section*
2138 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
2139                                    const unsigned char* symbols,
2140                                    off_t symbols_size,
2141                                    const unsigned char* symbol_names,
2142                                    off_t symbol_names_size,
2143                                    unsigned int shndx,
2144                                    const elfcpp::Shdr<32, false>& shdr,
2145                                    unsigned int reloc_shndx,
2146                                    unsigned int reloc_type,
2147                                    off_t* off);
2148 #endif
2149
2150 #ifdef HAVE_TARGET_32_BIG
2151 template
2152 Output_section*
2153 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
2154                                    const unsigned char* symbols,
2155                                    off_t symbols_size,
2156                                   const unsigned char* symbol_names,
2157                                   off_t symbol_names_size,
2158                                   unsigned int shndx,
2159                                   const elfcpp::Shdr<32, true>& shdr,
2160                                   unsigned int reloc_shndx,
2161                                   unsigned int reloc_type,
2162                                   off_t* off);
2163 #endif
2164
2165 #ifdef HAVE_TARGET_64_LITTLE
2166 template
2167 Output_section*
2168 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
2169                                    const unsigned char* symbols,
2170                                    off_t symbols_size,
2171                                    const unsigned char* symbol_names,
2172                                    off_t symbol_names_size,
2173                                    unsigned int shndx,
2174                                    const elfcpp::Shdr<64, false>& shdr,
2175                                    unsigned int reloc_shndx,
2176                                    unsigned int reloc_type,
2177                                    off_t* off);
2178 #endif
2179
2180 #ifdef HAVE_TARGET_64_BIG
2181 template
2182 Output_section*
2183 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
2184                                    const unsigned char* symbols,
2185                                    off_t symbols_size,
2186                                   const unsigned char* symbol_names,
2187                                   off_t symbol_names_size,
2188                                   unsigned int shndx,
2189                                   const elfcpp::Shdr<64, true>& shdr,
2190                                   unsigned int reloc_shndx,
2191                                   unsigned int reloc_type,
2192                                   off_t* off);
2193 #endif
2194
2195 } // End namespace gold.