Add licensing text to every source file.
[external/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 "layout.h"
35
36 namespace gold
37 {
38
39 // Layout_task_runner methods.
40
41 // Lay out the sections.  This is called after all the input objects
42 // have been read.
43
44 void
45 Layout_task_runner::run(Workqueue* workqueue)
46 {
47   off_t file_size = this->layout_->finalize(this->input_objects_,
48                                             this->symtab_);
49
50   // Now we know the final size of the output file and we know where
51   // each piece of information goes.
52   Output_file* of = new Output_file(this->options_,
53                                     this->input_objects_->target());
54   of->open(file_size);
55
56   // Queue up the final set of tasks.
57   gold::queue_final_tasks(this->options_, this->input_objects_,
58                           this->symtab_, this->layout_, workqueue, of);
59 }
60
61 // Layout methods.
62
63 Layout::Layout(const General_options& options)
64   : options_(options), namepool_(), sympool_(), dynpool_(), signatures_(),
65     section_name_map_(), segment_list_(), section_list_(),
66     unattached_section_list_(), special_output_list_(),
67     tls_segment_(NULL), symtab_section_(NULL),
68     dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL)
69 {
70   // Make space for more than enough segments for a typical file.
71   // This is just for efficiency--it's OK if we wind up needing more.
72   this->segment_list_.reserve(12);
73
74   // We expect three unattached Output_data objects: the file header,
75   // the segment headers, and the section headers.
76   this->special_output_list_.reserve(3);
77 }
78
79 // Hash a key we use to look up an output section mapping.
80
81 size_t
82 Layout::Hash_key::operator()(const Layout::Key& k) const
83 {
84  return k.first + k.second.first + k.second.second;
85 }
86
87 // Whether to include this section in the link.
88
89 template<int size, bool big_endian>
90 bool
91 Layout::include_section(Object*, const char*,
92                         const elfcpp::Shdr<size, big_endian>& shdr)
93 {
94   // Some section types are never linked.  Some are only linked when
95   // doing a relocateable link.
96   switch (shdr.get_sh_type())
97     {
98     case elfcpp::SHT_NULL:
99     case elfcpp::SHT_SYMTAB:
100     case elfcpp::SHT_DYNSYM:
101     case elfcpp::SHT_STRTAB:
102     case elfcpp::SHT_HASH:
103     case elfcpp::SHT_DYNAMIC:
104     case elfcpp::SHT_SYMTAB_SHNDX:
105       return false;
106
107     case elfcpp::SHT_RELA:
108     case elfcpp::SHT_REL:
109     case elfcpp::SHT_GROUP:
110       return parameters->output_is_object();
111
112     default:
113       // FIXME: Handle stripping debug sections here.
114       return true;
115     }
116 }
117
118 // Return an output section named NAME, or NULL if there is none.
119
120 Output_section*
121 Layout::find_output_section(const char* name) const
122 {
123   for (Section_name_map::const_iterator p = this->section_name_map_.begin();
124        p != this->section_name_map_.end();
125        ++p)
126     if (strcmp(p->second->name(), name) == 0)
127       return p->second;
128   return NULL;
129 }
130
131 // Return an output segment of type TYPE, with segment flags SET set
132 // and segment flags CLEAR clear.  Return NULL if there is none.
133
134 Output_segment*
135 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
136                             elfcpp::Elf_Word clear) const
137 {
138   for (Segment_list::const_iterator p = this->segment_list_.begin();
139        p != this->segment_list_.end();
140        ++p)
141     if (static_cast<elfcpp::PT>((*p)->type()) == type
142         && ((*p)->flags() & set) == set
143         && ((*p)->flags() & clear) == 0)
144       return *p;
145   return NULL;
146 }
147
148 // Return the output section to use for section NAME with type TYPE
149 // and section flags FLAGS.
150
151 Output_section*
152 Layout::get_output_section(const char* name, Stringpool::Key name_key,
153                            elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
154 {
155   // We should ignore some flags.
156   flags &= ~ (elfcpp::SHF_INFO_LINK
157               | elfcpp::SHF_LINK_ORDER
158               | elfcpp::SHF_GROUP
159               | elfcpp::SHF_MERGE
160               | elfcpp::SHF_STRINGS);
161
162   const Key key(name_key, std::make_pair(type, flags));
163   const std::pair<Key, Output_section*> v(key, NULL);
164   std::pair<Section_name_map::iterator, bool> ins(
165     this->section_name_map_.insert(v));
166
167   if (!ins.second)
168     return ins.first->second;
169   else
170     {
171       // This is the first time we've seen this name/type/flags
172       // combination.
173       Output_section* os = this->make_output_section(name, type, flags);
174       ins.first->second = os;
175       return os;
176     }
177 }
178
179 // Return the output section to use for input section SHNDX, with name
180 // NAME, with header HEADER, from object OBJECT.  Set *OFF to the
181 // offset of this input section without the output section.
182
183 template<int size, bool big_endian>
184 Output_section*
185 Layout::layout(Relobj* object, unsigned int shndx, const char* name,
186                const elfcpp::Shdr<size, big_endian>& shdr, off_t* off)
187 {
188   if (!this->include_section(object, name, shdr))
189     return NULL;
190
191   // If we are not doing a relocateable link, choose the name to use
192   // for the output section.
193   size_t len = strlen(name);
194   if (!parameters->output_is_object())
195     name = Layout::output_section_name(name, &len);
196
197   // FIXME: Handle SHF_OS_NONCONFORMING here.
198
199   // Canonicalize the section name.
200   Stringpool::Key name_key;
201   name = this->namepool_.add(name, len, &name_key);
202
203   // Find the output section.  The output section is selected based on
204   // the section name, type, and flags.
205   Output_section* os = this->get_output_section(name, name_key,
206                                                 shdr.get_sh_type(),
207                                                 shdr.get_sh_flags());
208
209   // FIXME: Handle SHF_LINK_ORDER somewhere.
210
211   *off = os->add_input_section(object, shndx, name, shdr);
212
213   return os;
214 }
215
216 // Add POSD to an output section using NAME, TYPE, and FLAGS.
217
218 void
219 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
220                                 elfcpp::Elf_Xword flags,
221                                 Output_section_data* posd)
222 {
223   // Canonicalize the name.
224   Stringpool::Key name_key;
225   name = this->namepool_.add(name, &name_key);
226
227   Output_section* os = this->get_output_section(name, name_key, type, flags);
228   os->add_output_section_data(posd);
229 }
230
231 // Map section flags to segment flags.
232
233 elfcpp::Elf_Word
234 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
235 {
236   elfcpp::Elf_Word ret = elfcpp::PF_R;
237   if ((flags & elfcpp::SHF_WRITE) != 0)
238     ret |= elfcpp::PF_W;
239   if ((flags & elfcpp::SHF_EXECINSTR) != 0)
240     ret |= elfcpp::PF_X;
241   return ret;
242 }
243
244 // Make a new Output_section, and attach it to segments as
245 // appropriate.
246
247 Output_section*
248 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
249                             elfcpp::Elf_Xword flags)
250 {
251   Output_section* os = new Output_section(name, type, flags);
252   this->section_list_.push_back(os);
253
254   if ((flags & elfcpp::SHF_ALLOC) == 0)
255     this->unattached_section_list_.push_back(os);
256   else
257     {
258       // This output section goes into a PT_LOAD segment.
259
260       elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
261
262       // The only thing we really care about for PT_LOAD segments is
263       // whether or not they are writable, so that is how we search
264       // for them.  People who need segments sorted on some other
265       // basis will have to wait until we implement a mechanism for
266       // them to describe the segments they want.
267
268       Segment_list::const_iterator p;
269       for (p = this->segment_list_.begin();
270            p != this->segment_list_.end();
271            ++p)
272         {
273           if ((*p)->type() == elfcpp::PT_LOAD
274               && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
275             {
276               (*p)->add_output_section(os, seg_flags);
277               break;
278             }
279         }
280
281       if (p == this->segment_list_.end())
282         {
283           Output_segment* oseg = new Output_segment(elfcpp::PT_LOAD,
284                                                     seg_flags);
285           this->segment_list_.push_back(oseg);
286           oseg->add_output_section(os, seg_flags);
287         }
288
289       // If we see a loadable SHT_NOTE section, we create a PT_NOTE
290       // segment.
291       if (type == elfcpp::SHT_NOTE)
292         {
293           // See if we already have an equivalent PT_NOTE segment.
294           for (p = this->segment_list_.begin();
295                p != segment_list_.end();
296                ++p)
297             {
298               if ((*p)->type() == elfcpp::PT_NOTE
299                   && (((*p)->flags() & elfcpp::PF_W)
300                       == (seg_flags & elfcpp::PF_W)))
301                 {
302                   (*p)->add_output_section(os, seg_flags);
303                   break;
304                 }
305             }
306
307           if (p == this->segment_list_.end())
308             {
309               Output_segment* oseg = new Output_segment(elfcpp::PT_NOTE,
310                                                         seg_flags);
311               this->segment_list_.push_back(oseg);
312               oseg->add_output_section(os, seg_flags);
313             }
314         }
315
316       // If we see a loadable SHF_TLS section, we create a PT_TLS
317       // segment.  There can only be one such segment.
318       if ((flags & elfcpp::SHF_TLS) != 0)
319         {
320           if (this->tls_segment_ == NULL)
321             {
322               this->tls_segment_ = new Output_segment(elfcpp::PT_TLS,
323                                                       seg_flags);
324               this->segment_list_.push_back(this->tls_segment_);
325             }
326           this->tls_segment_->add_output_section(os, seg_flags);
327         }
328     }
329
330   return os;
331 }
332
333 // Create the dynamic sections which are needed before we read the
334 // relocs.
335
336 void
337 Layout::create_initial_dynamic_sections(const Input_objects* input_objects,
338                                         Symbol_table* symtab)
339 {
340   if (!input_objects->any_dynamic())
341     return;
342
343   const char* dynamic_name = this->namepool_.add(".dynamic", NULL);
344   this->dynamic_section_ = this->make_output_section(dynamic_name,
345                                                      elfcpp::SHT_DYNAMIC,
346                                                      (elfcpp::SHF_ALLOC
347                                                       | elfcpp::SHF_WRITE));
348
349   symtab->define_in_output_data(input_objects->target(), "_DYNAMIC", NULL,
350                                 this->dynamic_section_, 0, 0,
351                                 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
352                                 elfcpp::STV_HIDDEN, 0, false, false);
353
354   this->dynamic_data_ =  new Output_data_dynamic(input_objects->target(),
355                                                  &this->dynpool_);
356
357   this->dynamic_section_->add_output_section_data(this->dynamic_data_);
358 }
359
360 // For each output section whose name can be represented as C symbol,
361 // define __start and __stop symbols for the section.  This is a GNU
362 // extension.
363
364 void
365 Layout::define_section_symbols(Symbol_table* symtab, const Target* target)
366 {
367   for (Section_list::const_iterator p = this->section_list_.begin();
368        p != this->section_list_.end();
369        ++p)
370     {
371       const char* const name = (*p)->name();
372       if (name[strspn(name,
373                       ("0123456789"
374                        "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
375                        "abcdefghijklmnopqrstuvwxyz"
376                        "_"))]
377           == '\0')
378         {
379           const std::string name_string(name);
380           const std::string start_name("__start_" + name_string);
381           const std::string stop_name("__stop_" + name_string);
382
383           symtab->define_in_output_data(target,
384                                         start_name.c_str(),
385                                         NULL, // version
386                                         *p,
387                                         0, // value
388                                         0, // symsize
389                                         elfcpp::STT_NOTYPE,
390                                         elfcpp::STB_GLOBAL,
391                                         elfcpp::STV_DEFAULT,
392                                         0, // nonvis
393                                         false, // offset_is_from_end
394                                         false); // only_if_ref
395
396           symtab->define_in_output_data(target,
397                                         stop_name.c_str(),
398                                         NULL, // version
399                                         *p,
400                                         0, // value
401                                         0, // symsize
402                                         elfcpp::STT_NOTYPE,
403                                         elfcpp::STB_GLOBAL,
404                                         elfcpp::STV_DEFAULT,
405                                         0, // nonvis
406                                         true, // offset_is_from_end
407                                         false); // only_if_ref
408         }
409     }
410 }
411
412 // Find the first read-only PT_LOAD segment, creating one if
413 // necessary.
414
415 Output_segment*
416 Layout::find_first_load_seg()
417 {
418   for (Segment_list::const_iterator p = this->segment_list_.begin();
419        p != this->segment_list_.end();
420        ++p)
421     {
422       if ((*p)->type() == elfcpp::PT_LOAD
423           && ((*p)->flags() & elfcpp::PF_R) != 0
424           && ((*p)->flags() & elfcpp::PF_W) == 0)
425         return *p;
426     }
427
428   Output_segment* load_seg = new Output_segment(elfcpp::PT_LOAD, elfcpp::PF_R);
429   this->segment_list_.push_back(load_seg);
430   return load_seg;
431 }
432
433 // Finalize the layout.  When this is called, we have created all the
434 // output sections and all the output segments which are based on
435 // input sections.  We have several things to do, and we have to do
436 // them in the right order, so that we get the right results correctly
437 // and efficiently.
438
439 // 1) Finalize the list of output segments and create the segment
440 // table header.
441
442 // 2) Finalize the dynamic symbol table and associated sections.
443
444 // 3) Determine the final file offset of all the output segments.
445
446 // 4) Determine the final file offset of all the SHF_ALLOC output
447 // sections.
448
449 // 5) Create the symbol table sections and the section name table
450 // section.
451
452 // 6) Finalize the symbol table: set symbol values to their final
453 // value and make a final determination of which symbols are going
454 // into the output symbol table.
455
456 // 7) Create the section table header.
457
458 // 8) Determine the final file offset of all the output sections which
459 // are not SHF_ALLOC, including the section table header.
460
461 // 9) Finalize the ELF file header.
462
463 // This function returns the size of the output file.
464
465 off_t
466 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab)
467 {
468   Target* const target = input_objects->target();
469   const int size = target->get_size();
470
471   target->finalize_sections(this);
472
473   Output_segment* phdr_seg = NULL;
474   if (input_objects->any_dynamic())
475     {
476       // There was a dynamic object in the link.  We need to create
477       // some information for the dynamic linker.
478
479       // Create the PT_PHDR segment which will hold the program
480       // headers.
481       phdr_seg = new Output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
482       this->segment_list_.push_back(phdr_seg);
483
484       // Create the dynamic symbol table, including the hash table.
485       Output_section* dynstr;
486       std::vector<Symbol*> dynamic_symbols;
487       unsigned int local_dynamic_count;
488       Versions versions;
489       this->create_dynamic_symtab(target, symtab, &dynstr,
490                                   &local_dynamic_count, &dynamic_symbols,
491                                   &versions);
492
493       // Create the .interp section to hold the name of the
494       // interpreter, and put it in a PT_INTERP segment.
495       this->create_interp(target);
496
497       // Finish the .dynamic section to hold the dynamic data, and put
498       // it in a PT_DYNAMIC segment.
499       this->finish_dynamic_section(input_objects, symtab);
500
501       // We should have added everything we need to the dynamic string
502       // table.
503       this->dynpool_.set_string_offsets();
504
505       // Create the version sections.  We can't do this until the
506       // dynamic string table is complete.
507       this->create_version_sections(target, &versions, local_dynamic_count,
508                                     dynamic_symbols, dynstr);
509     }
510
511   // FIXME: Handle PT_GNU_STACK.
512
513   Output_segment* load_seg = this->find_first_load_seg();
514
515   // Lay out the segment headers.
516   bool big_endian = target->is_big_endian();
517   Output_segment_headers* segment_headers;
518   segment_headers = new Output_segment_headers(size, big_endian,
519                                                this->segment_list_);
520   load_seg->add_initial_output_data(segment_headers);
521   this->special_output_list_.push_back(segment_headers);
522   if (phdr_seg != NULL)
523     phdr_seg->add_initial_output_data(segment_headers);
524
525   // Lay out the file header.
526   Output_file_header* file_header;
527   file_header = new Output_file_header(size,
528                                        big_endian,
529                                        target,
530                                        symtab,
531                                        segment_headers);
532   load_seg->add_initial_output_data(file_header);
533   this->special_output_list_.push_back(file_header);
534
535   // We set the output section indexes in set_segment_offsets and
536   // set_section_offsets.
537   unsigned int shndx = 1;
538
539   // Set the file offsets of all the segments, and all the sections
540   // they contain.
541   off_t off = this->set_segment_offsets(target, load_seg, &shndx);
542
543   // Create the symbol table sections.
544   this->create_symtab_sections(size, input_objects, symtab, &off);
545
546   // Create the .shstrtab section.
547   Output_section* shstrtab_section = this->create_shstrtab();
548
549   // Set the file offsets of all the sections not associated with
550   // segments.
551   off = this->set_section_offsets(off, &shndx);
552
553   // Create the section table header.
554   Output_section_headers* oshdrs = this->create_shdrs(size, big_endian, &off);
555
556   file_header->set_section_info(oshdrs, shstrtab_section);
557
558   // Now we know exactly where everything goes in the output file.
559   Output_data::layout_complete();
560
561   return off;
562 }
563
564 // Return whether SEG1 should be before SEG2 in the output file.  This
565 // is based entirely on the segment type and flags.  When this is
566 // called the segment addresses has normally not yet been set.
567
568 bool
569 Layout::segment_precedes(const Output_segment* seg1,
570                          const Output_segment* seg2)
571 {
572   elfcpp::Elf_Word type1 = seg1->type();
573   elfcpp::Elf_Word type2 = seg2->type();
574
575   // The single PT_PHDR segment is required to precede any loadable
576   // segment.  We simply make it always first.
577   if (type1 == elfcpp::PT_PHDR)
578     {
579       gold_assert(type2 != elfcpp::PT_PHDR);
580       return true;
581     }
582   if (type2 == elfcpp::PT_PHDR)
583     return false;
584
585   // The single PT_INTERP segment is required to precede any loadable
586   // segment.  We simply make it always second.
587   if (type1 == elfcpp::PT_INTERP)
588     {
589       gold_assert(type2 != elfcpp::PT_INTERP);
590       return true;
591     }
592   if (type2 == elfcpp::PT_INTERP)
593     return false;
594
595   // We then put PT_LOAD segments before any other segments.
596   if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
597     return true;
598   if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
599     return false;
600
601   // We put the PT_TLS segment last, because that is where the dynamic
602   // linker expects to find it (this is just for efficiency; other
603   // positions would also work correctly).
604   if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
605     return false;
606   if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
607     return true;
608
609   const elfcpp::Elf_Word flags1 = seg1->flags();
610   const elfcpp::Elf_Word flags2 = seg2->flags();
611
612   // The order of non-PT_LOAD segments is unimportant.  We simply sort
613   // by the numeric segment type and flags values.  There should not
614   // be more than one segment with the same type and flags.
615   if (type1 != elfcpp::PT_LOAD)
616     {
617       if (type1 != type2)
618         return type1 < type2;
619       gold_assert(flags1 != flags2);
620       return flags1 < flags2;
621     }
622
623   // We sort PT_LOAD segments based on the flags.  Readonly segments
624   // come before writable segments.  Then executable segments come
625   // before non-executable segments.  Then the unlikely case of a
626   // non-readable segment comes before the normal case of a readable
627   // segment.  If there are multiple segments with the same type and
628   // flags, we require that the address be set, and we sort by
629   // virtual address and then physical address.
630   if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
631     return (flags1 & elfcpp::PF_W) == 0;
632   if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
633     return (flags1 & elfcpp::PF_X) != 0;
634   if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
635     return (flags1 & elfcpp::PF_R) == 0;
636
637   uint64_t vaddr1 = seg1->vaddr();
638   uint64_t vaddr2 = seg2->vaddr();
639   if (vaddr1 != vaddr2)
640     return vaddr1 < vaddr2;
641
642   uint64_t paddr1 = seg1->paddr();
643   uint64_t paddr2 = seg2->paddr();
644   gold_assert(paddr1 != paddr2);
645   return paddr1 < paddr2;
646 }
647
648 // Set the file offsets of all the segments, and all the sections they
649 // contain.  They have all been created.  LOAD_SEG must be be laid out
650 // first.  Return the offset of the data to follow.
651
652 off_t
653 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
654                             unsigned int *pshndx)
655 {
656   // Sort them into the final order.
657   std::sort(this->segment_list_.begin(), this->segment_list_.end(),
658             Layout::Compare_segments());
659
660   // Find the PT_LOAD segments, and set their addresses and offsets
661   // and their section's addresses and offsets.
662   uint64_t addr = target->text_segment_address();
663   off_t off = 0;
664   bool was_readonly = false;
665   for (Segment_list::iterator p = this->segment_list_.begin();
666        p != this->segment_list_.end();
667        ++p)
668     {
669       if ((*p)->type() == elfcpp::PT_LOAD)
670         {
671           if (load_seg != NULL && load_seg != *p)
672             gold_unreachable();
673           load_seg = NULL;
674
675           // If the last segment was readonly, and this one is not,
676           // then skip the address forward one page, maintaining the
677           // same position within the page.  This lets us store both
678           // segments overlapping on a single page in the file, but
679           // the loader will put them on different pages in memory.
680
681           uint64_t orig_addr = addr;
682           uint64_t orig_off = off;
683
684           uint64_t aligned_addr = addr;
685           uint64_t abi_pagesize = target->abi_pagesize();
686
687           // FIXME: This should depend on the -n and -N options.
688           (*p)->set_minimum_addralign(target->common_pagesize());
689
690           if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
691             {
692               uint64_t align = (*p)->addralign();
693
694               addr = align_address(addr, align);
695               aligned_addr = addr;
696               if ((addr & (abi_pagesize - 1)) != 0)
697                 addr = addr + abi_pagesize;
698             }
699
700           unsigned int shndx_hold = *pshndx;
701           off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
702           uint64_t new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
703
704           // Now that we know the size of this segment, we may be able
705           // to save a page in memory, at the cost of wasting some
706           // file space, by instead aligning to the start of a new
707           // page.  Here we use the real machine page size rather than
708           // the ABI mandated page size.
709
710           if (aligned_addr != addr)
711             {
712               uint64_t common_pagesize = target->common_pagesize();
713               uint64_t first_off = (common_pagesize
714                                     - (aligned_addr
715                                        & (common_pagesize - 1)));
716               uint64_t last_off = new_addr & (common_pagesize - 1);
717               if (first_off > 0
718                   && last_off > 0
719                   && ((aligned_addr & ~ (common_pagesize - 1))
720                       != (new_addr & ~ (common_pagesize - 1)))
721                   && first_off + last_off <= common_pagesize)
722                 {
723                   *pshndx = shndx_hold;
724                   addr = align_address(aligned_addr, common_pagesize);
725                   off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
726                   new_addr = (*p)->set_section_addresses(addr, &off, pshndx);
727                 }
728             }
729
730           addr = new_addr;
731
732           if (((*p)->flags() & elfcpp::PF_W) == 0)
733             was_readonly = true;
734         }
735     }
736
737   // Handle the non-PT_LOAD segments, setting their offsets from their
738   // section's offsets.
739   for (Segment_list::iterator p = this->segment_list_.begin();
740        p != this->segment_list_.end();
741        ++p)
742     {
743       if ((*p)->type() != elfcpp::PT_LOAD)
744         (*p)->set_offset();
745     }
746
747   return off;
748 }
749
750 // Set the file offset of all the sections not associated with a
751 // segment.
752
753 off_t
754 Layout::set_section_offsets(off_t off, unsigned int* pshndx)
755 {
756   for (Section_list::iterator p = this->unattached_section_list_.begin();
757        p != this->unattached_section_list_.end();
758        ++p)
759     {
760       (*p)->set_out_shndx(*pshndx);
761       ++*pshndx;
762       if ((*p)->offset() != -1)
763         continue;
764       off = align_address(off, (*p)->addralign());
765       (*p)->set_address(0, off);
766       off += (*p)->data_size();
767     }
768   return off;
769 }
770
771 // Create the symbol table sections.  Here we also set the final
772 // values of the symbols.  At this point all the loadable sections are
773 // fully laid out.
774
775 void
776 Layout::create_symtab_sections(int size, const Input_objects* input_objects,
777                                Symbol_table* symtab,
778                                off_t* poff)
779 {
780   int symsize;
781   unsigned int align;
782   if (size == 32)
783     {
784       symsize = elfcpp::Elf_sizes<32>::sym_size;
785       align = 4;
786     }
787   else if (size == 64)
788     {
789       symsize = elfcpp::Elf_sizes<64>::sym_size;
790       align = 8;
791     }
792   else
793     gold_unreachable();
794
795   off_t off = *poff;
796   off = align_address(off, align);
797   off_t startoff = off;
798
799   // Save space for the dummy symbol at the start of the section.  We
800   // never bother to write this out--it will just be left as zero.
801   off += symsize;
802   unsigned int local_symbol_index = 1;
803
804   // Add STT_SECTION symbols for each Output section which needs one.
805   for (Section_list::iterator p = this->section_list_.begin();
806        p != this->section_list_.end();
807        ++p)
808     {
809       if (!(*p)->needs_symtab_index())
810         (*p)->set_symtab_index(-1U);
811       else
812         {
813           (*p)->set_symtab_index(local_symbol_index);
814           ++local_symbol_index;
815           off += symsize;
816         }
817     }
818
819   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
820        p != input_objects->relobj_end();
821        ++p)
822     {
823       Task_lock_obj<Object> tlo(**p);
824       unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
825                                                         off,
826                                                         &this->sympool_);
827       off += (index - local_symbol_index) * symsize;
828       local_symbol_index = index;
829     }
830
831   unsigned int local_symcount = local_symbol_index;
832   gold_assert(local_symcount * symsize == off - startoff);
833
834   off_t dynoff;
835   size_t dyn_global_index;
836   size_t dyncount;
837   if (this->dynsym_section_ == NULL)
838     {
839       dynoff = 0;
840       dyn_global_index = 0;
841       dyncount = 0;
842     }
843   else
844     {
845       dyn_global_index = this->dynsym_section_->info();
846       off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
847       dynoff = this->dynsym_section_->offset() + locsize;
848       dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
849       gold_assert(dyncount * symsize
850                   == this->dynsym_section_->data_size() - locsize);
851     }
852
853   off = symtab->finalize(local_symcount, off, dynoff, dyn_global_index,
854                          dyncount, &this->sympool_);
855
856   this->sympool_.set_string_offsets();
857
858   const char* symtab_name = this->namepool_.add(".symtab", NULL);
859   Output_section* osymtab = this->make_output_section(symtab_name,
860                                                       elfcpp::SHT_SYMTAB,
861                                                       0);
862   this->symtab_section_ = osymtab;
863
864   Output_section_data* pos = new Output_data_space(off - startoff,
865                                                    align);
866   osymtab->add_output_section_data(pos);
867
868   const char* strtab_name = this->namepool_.add(".strtab", NULL);
869   Output_section* ostrtab = this->make_output_section(strtab_name,
870                                                       elfcpp::SHT_STRTAB,
871                                                       0);
872
873   Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
874   ostrtab->add_output_section_data(pstr);
875
876   osymtab->set_address(0, startoff);
877   osymtab->set_link_section(ostrtab);
878   osymtab->set_info(local_symcount);
879   osymtab->set_entsize(symsize);
880
881   *poff = off;
882 }
883
884 // Create the .shstrtab section, which holds the names of the
885 // sections.  At the time this is called, we have created all the
886 // output sections except .shstrtab itself.
887
888 Output_section*
889 Layout::create_shstrtab()
890 {
891   // FIXME: We don't need to create a .shstrtab section if we are
892   // stripping everything.
893
894   const char* name = this->namepool_.add(".shstrtab", NULL);
895
896   this->namepool_.set_string_offsets();
897
898   Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
899
900   Output_section_data* posd = new Output_data_strtab(&this->namepool_);
901   os->add_output_section_data(posd);
902
903   return os;
904 }
905
906 // Create the section headers.  SIZE is 32 or 64.  OFF is the file
907 // offset.
908
909 Output_section_headers*
910 Layout::create_shdrs(int size, bool big_endian, off_t* poff)
911 {
912   Output_section_headers* oshdrs;
913   oshdrs = new Output_section_headers(size, big_endian, this,
914                                       &this->segment_list_,
915                                       &this->unattached_section_list_,
916                                       &this->namepool_);
917   off_t off = align_address(*poff, oshdrs->addralign());
918   oshdrs->set_address(0, off);
919   off += oshdrs->data_size();
920   *poff = off;
921   this->special_output_list_.push_back(oshdrs);
922   return oshdrs;
923 }
924
925 // Create the dynamic symbol table.
926
927 void
928 Layout::create_dynamic_symtab(const Target* target, Symbol_table* symtab,
929                               Output_section **pdynstr,
930                               unsigned int* plocal_dynamic_count,
931                               std::vector<Symbol*>* pdynamic_symbols,
932                               Versions* pversions)
933 {
934   // Count all the symbols in the dynamic symbol table, and set the
935   // dynamic symbol indexes.
936
937   // Skip symbol 0, which is always all zeroes.
938   unsigned int index = 1;
939
940   // Add STT_SECTION symbols for each Output section which needs one.
941   for (Section_list::iterator p = this->section_list_.begin();
942        p != this->section_list_.end();
943        ++p)
944     {
945       if (!(*p)->needs_dynsym_index())
946         (*p)->set_dynsym_index(-1U);
947       else
948         {
949           (*p)->set_dynsym_index(index);
950           ++index;
951         }
952     }
953
954   // FIXME: Some targets apparently require local symbols in the
955   // dynamic symbol table.  Here is where we will have to count them,
956   // and set the dynamic symbol indexes, and add the names to
957   // this->dynpool_.
958
959   unsigned int local_symcount = index;
960   *plocal_dynamic_count = local_symcount;
961
962   // FIXME: We have to tell set_dynsym_indexes whether the
963   // -E/--export-dynamic option was used.
964   index = symtab->set_dynsym_indexes(&this->options_, target, index,
965                                      pdynamic_symbols, &this->dynpool_,
966                                      pversions);
967
968   int symsize;
969   unsigned int align;
970   const int size = target->get_size();
971   if (size == 32)
972     {
973       symsize = elfcpp::Elf_sizes<32>::sym_size;
974       align = 4;
975     }
976   else if (size == 64)
977     {
978       symsize = elfcpp::Elf_sizes<64>::sym_size;
979       align = 8;
980     }
981   else
982     gold_unreachable();
983
984   // Create the dynamic symbol table section.
985
986   const char* dynsym_name = this->namepool_.add(".dynsym", NULL);
987   Output_section* dynsym = this->make_output_section(dynsym_name,
988                                                      elfcpp::SHT_DYNSYM,
989                                                      elfcpp::SHF_ALLOC);
990
991   Output_section_data* odata = new Output_data_space(index * symsize,
992                                                      align);
993   dynsym->add_output_section_data(odata);
994
995   dynsym->set_info(local_symcount);
996   dynsym->set_entsize(symsize);
997   dynsym->set_addralign(align);
998
999   this->dynsym_section_ = dynsym;
1000
1001   Output_data_dynamic* const odyn = this->dynamic_data_;
1002   odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1003   odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1004
1005   // Create the dynamic string table section.
1006
1007   const char* dynstr_name = this->namepool_.add(".dynstr", NULL);
1008   Output_section* dynstr = this->make_output_section(dynstr_name,
1009                                                      elfcpp::SHT_STRTAB,
1010                                                      elfcpp::SHF_ALLOC);
1011
1012   Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1013   dynstr->add_output_section_data(strdata);
1014
1015   dynsym->set_link_section(dynstr);
1016   this->dynamic_section_->set_link_section(dynstr);
1017
1018   odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1019   odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1020
1021   *pdynstr = dynstr;
1022
1023   // Create the hash tables.
1024
1025   // FIXME: We need an option to create a GNU hash table.
1026
1027   unsigned char* phash;
1028   unsigned int hashlen;
1029   Dynobj::create_elf_hash_table(target, *pdynamic_symbols, local_symcount,
1030                                 &phash, &hashlen);
1031
1032   const char* hash_name = this->namepool_.add(".hash", NULL);
1033   Output_section* hashsec = this->make_output_section(hash_name,
1034                                                       elfcpp::SHT_HASH,
1035                                                       elfcpp::SHF_ALLOC);
1036
1037   Output_section_data* hashdata = new Output_data_const_buffer(phash,
1038                                                                hashlen,
1039                                                                align);
1040   hashsec->add_output_section_data(hashdata);
1041
1042   hashsec->set_link_section(dynsym);
1043   hashsec->set_entsize(4);
1044
1045   odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1046 }
1047
1048 // Create the version sections.
1049
1050 void
1051 Layout::create_version_sections(const Target* target, const Versions* versions,
1052                                 unsigned int local_symcount,
1053                                 const std::vector<Symbol*>& dynamic_symbols,
1054                                 const Output_section* dynstr)
1055 {
1056   if (!versions->any_defs() && !versions->any_needs())
1057     return;
1058
1059   if (target->get_size() == 32)
1060     {
1061       if (target->is_big_endian())
1062         {
1063 #ifdef HAVE_TARGET_32_BIG
1064           this->sized_create_version_sections
1065               SELECT_SIZE_ENDIAN_NAME(32, true)(
1066                   versions, local_symcount, dynamic_symbols, dynstr
1067                   SELECT_SIZE_ENDIAN(32, true));
1068 #else
1069           gold_unreachable();
1070 #endif
1071         }
1072       else
1073         {
1074 #ifdef HAVE_TARGET_32_LITTLE
1075           this->sized_create_version_sections
1076               SELECT_SIZE_ENDIAN_NAME(32, false)(
1077                   versions, local_symcount, dynamic_symbols, dynstr
1078                   SELECT_SIZE_ENDIAN(32, false));
1079 #else
1080           gold_unreachable();
1081 #endif
1082         }
1083     }
1084   else if (target->get_size() == 64)
1085     {
1086       if (target->is_big_endian())
1087         {
1088 #ifdef HAVE_TARGET_64_BIG
1089           this->sized_create_version_sections
1090               SELECT_SIZE_ENDIAN_NAME(64, true)(
1091                   versions, local_symcount, dynamic_symbols, dynstr
1092                   SELECT_SIZE_ENDIAN(64, true));
1093 #else
1094           gold_unreachable();
1095 #endif
1096         }
1097       else
1098         {
1099 #ifdef HAVE_TARGET_64_LITTLE
1100           this->sized_create_version_sections
1101               SELECT_SIZE_ENDIAN_NAME(64, false)(
1102                   versions, local_symcount, dynamic_symbols, dynstr
1103                   SELECT_SIZE_ENDIAN(64, false));
1104 #else
1105           gold_unreachable();
1106 #endif
1107         }
1108     }
1109   else
1110     gold_unreachable();
1111 }
1112
1113 // Create the version sections, sized version.
1114
1115 template<int size, bool big_endian>
1116 void
1117 Layout::sized_create_version_sections(
1118     const Versions* versions,
1119     unsigned int local_symcount,
1120     const std::vector<Symbol*>& dynamic_symbols,
1121     const Output_section* dynstr
1122     ACCEPT_SIZE_ENDIAN)
1123 {
1124   const char* vname = this->namepool_.add(".gnu.version", NULL);
1125   Output_section* vsec = this->make_output_section(vname,
1126                                                    elfcpp::SHT_GNU_versym,
1127                                                    elfcpp::SHF_ALLOC);
1128
1129   unsigned char* vbuf;
1130   unsigned int vsize;
1131   versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1132       &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1133       SELECT_SIZE_ENDIAN(size, big_endian));
1134
1135   Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1136
1137   vsec->add_output_section_data(vdata);
1138   vsec->set_entsize(2);
1139   vsec->set_link_section(this->dynsym_section_);
1140
1141   Output_data_dynamic* const odyn = this->dynamic_data_;
1142   odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1143
1144   if (versions->any_defs())
1145     {
1146       const char* vdname = this->namepool_.add(".gnu.version_d", NULL);
1147       Output_section *vdsec;
1148       vdsec = this->make_output_section(vdname, elfcpp::SHT_GNU_verdef,
1149                                         elfcpp::SHF_ALLOC);
1150
1151       unsigned char* vdbuf;
1152       unsigned int vdsize;
1153       unsigned int vdentries;
1154       versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1155           &this->dynpool_, &vdbuf, &vdsize, &vdentries
1156           SELECT_SIZE_ENDIAN(size, big_endian));
1157
1158       Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1159                                                                  vdsize,
1160                                                                  4);
1161
1162       vdsec->add_output_section_data(vddata);
1163       vdsec->set_link_section(dynstr);
1164       vdsec->set_info(vdentries);
1165
1166       odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1167       odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
1168     }
1169
1170   if (versions->any_needs())
1171     {
1172       const char* vnname = this->namepool_.add(".gnu.version_r", NULL);
1173       Output_section* vnsec;
1174       vnsec = this->make_output_section(vnname, elfcpp::SHT_GNU_verneed,
1175                                         elfcpp::SHF_ALLOC);
1176
1177       unsigned char* vnbuf;
1178       unsigned int vnsize;
1179       unsigned int vnentries;
1180       versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
1181         (&this->dynpool_, &vnbuf, &vnsize, &vnentries
1182          SELECT_SIZE_ENDIAN(size, big_endian));
1183
1184       Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
1185                                                                  vnsize,
1186                                                                  4);
1187
1188       vnsec->add_output_section_data(vndata);
1189       vnsec->set_link_section(dynstr);
1190       vnsec->set_info(vnentries);
1191
1192       odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
1193       odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
1194     }
1195 }
1196
1197 // Create the .interp section and PT_INTERP segment.
1198
1199 void
1200 Layout::create_interp(const Target* target)
1201 {
1202   const char* interp = this->options_.dynamic_linker();
1203   if (interp == NULL)
1204     {
1205       interp = target->dynamic_linker();
1206       gold_assert(interp != NULL);
1207     }
1208
1209   size_t len = strlen(interp) + 1;
1210
1211   Output_section_data* odata = new Output_data_const(interp, len, 1);
1212
1213   const char* interp_name = this->namepool_.add(".interp", NULL);
1214   Output_section* osec = this->make_output_section(interp_name,
1215                                                    elfcpp::SHT_PROGBITS,
1216                                                    elfcpp::SHF_ALLOC);
1217   osec->add_output_section_data(odata);
1218
1219   Output_segment* oseg = new Output_segment(elfcpp::PT_INTERP, elfcpp::PF_R);
1220   this->segment_list_.push_back(oseg);
1221   oseg->add_initial_output_section(osec, elfcpp::PF_R);
1222 }
1223
1224 // Finish the .dynamic section and PT_DYNAMIC segment.
1225
1226 void
1227 Layout::finish_dynamic_section(const Input_objects* input_objects,
1228                                const Symbol_table* symtab)
1229 {
1230   Output_segment* oseg = new Output_segment(elfcpp::PT_DYNAMIC,
1231                                             elfcpp::PF_R | elfcpp::PF_W);
1232   this->segment_list_.push_back(oseg);
1233   oseg->add_initial_output_section(this->dynamic_section_,
1234                                    elfcpp::PF_R | elfcpp::PF_W);
1235
1236   Output_data_dynamic* const odyn = this->dynamic_data_;
1237
1238   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
1239        p != input_objects->dynobj_end();
1240        ++p)
1241     {
1242       // FIXME: Handle --as-needed.
1243       odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
1244     }
1245
1246   // FIXME: Support --init and --fini.
1247   Symbol* sym = symtab->lookup("_init");
1248   if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1249     odyn->add_symbol(elfcpp::DT_INIT, sym);
1250
1251   sym = symtab->lookup("_fini");
1252   if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
1253     odyn->add_symbol(elfcpp::DT_FINI, sym);
1254
1255   // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
1256
1257   // Add a DT_RPATH entry if needed.
1258   const General_options::Dir_list& rpath(this->options_.rpath());
1259   if (!rpath.empty())
1260     {
1261       std::string rpath_val;
1262       for (General_options::Dir_list::const_iterator p = rpath.begin();
1263            p != rpath.end();
1264            ++p)
1265         {
1266           if (rpath_val.empty())
1267             rpath_val = *p;
1268           else
1269             {
1270               // Eliminate duplicates.
1271               General_options::Dir_list::const_iterator q;
1272               for (q = rpath.begin(); q != p; ++q)
1273                 if (strcmp(*q, *p) == 0)
1274                   break;
1275               if (q == p)
1276                 {
1277                   rpath_val += ':';
1278                   rpath_val += *p;
1279                 }
1280             }
1281         }
1282
1283       odyn->add_string(elfcpp::DT_RPATH, rpath_val);
1284     }
1285 }
1286
1287 // The mapping of .gnu.linkonce section names to real section names.
1288
1289 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
1290 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
1291 {
1292   MAPPING_INIT("d.rel.ro", ".data.rel.ro"),     // Must be before "d".
1293   MAPPING_INIT("t", ".text"),
1294   MAPPING_INIT("r", ".rodata"),
1295   MAPPING_INIT("d", ".data"),
1296   MAPPING_INIT("b", ".bss"),
1297   MAPPING_INIT("s", ".sdata"),
1298   MAPPING_INIT("sb", ".sbss"),
1299   MAPPING_INIT("s2", ".sdata2"),
1300   MAPPING_INIT("sb2", ".sbss2"),
1301   MAPPING_INIT("wi", ".debug_info"),
1302   MAPPING_INIT("td", ".tdata"),
1303   MAPPING_INIT("tb", ".tbss"),
1304   MAPPING_INIT("lr", ".lrodata"),
1305   MAPPING_INIT("l", ".ldata"),
1306   MAPPING_INIT("lb", ".lbss"),
1307 };
1308 #undef MAPPING_INIT
1309
1310 const int Layout::linkonce_mapping_count =
1311   sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
1312
1313 // Return the name of the output section to use for a .gnu.linkonce
1314 // section.  This is based on the default ELF linker script of the old
1315 // GNU linker.  For example, we map a name like ".gnu.linkonce.t.foo"
1316 // to ".text".  Set *PLEN to the length of the name.  *PLEN is
1317 // initialized to the length of NAME.
1318
1319 const char*
1320 Layout::linkonce_output_name(const char* name, size_t *plen)
1321 {
1322   const char* s = name + sizeof(".gnu.linkonce") - 1;
1323   if (*s != '.')
1324     return name;
1325   ++s;
1326   const Linkonce_mapping* plm = linkonce_mapping;
1327   for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
1328     {
1329       if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
1330         {
1331           *plen = plm->tolen;
1332           return plm->to;
1333         }
1334     }
1335   return name;
1336 }
1337
1338 // Choose the output section name to use given an input section name.
1339 // Set *PLEN to the length of the name.  *PLEN is initialized to the
1340 // length of NAME.
1341
1342 const char*
1343 Layout::output_section_name(const char* name, size_t* plen)
1344 {
1345   if (Layout::is_linkonce(name))
1346     {
1347       // .gnu.linkonce sections are laid out as though they were named
1348       // for the sections are placed into.
1349       return Layout::linkonce_output_name(name, plen);
1350     }
1351
1352   // If the section name has no '.', or only an initial '.', we use
1353   // the name unchanged (i.e., ".text" is unchanged).
1354
1355   // Otherwise, if the section name does not include ".rel", we drop
1356   // the last '.'  and everything that follows (i.e., ".text.XXX"
1357   // becomes ".text").
1358
1359   // Otherwise, if the section name has zero or one '.' after the
1360   // ".rel", we use the name unchanged (i.e., ".rel.text" is
1361   // unchanged).
1362
1363   // Otherwise, we drop the last '.' and everything that follows
1364   // (i.e., ".rel.text.XXX" becomes ".rel.text").
1365
1366   const char* s = name;
1367   if (*s == '.')
1368     ++s;
1369   const char* sdot = strchr(s, '.');
1370   if (sdot == NULL)
1371     return name;
1372
1373   const char* srel = strstr(s, ".rel");
1374   if (srel == NULL)
1375     {
1376       *plen = sdot - name;
1377       return name;
1378     }
1379
1380   sdot = strchr(srel + 1, '.');
1381   if (sdot == NULL)
1382     return name;
1383   sdot = strchr(sdot + 1, '.');
1384   if (sdot == NULL)
1385     return name;
1386
1387   *plen = sdot - name;
1388   return name;
1389 }
1390
1391 // Record the signature of a comdat section, and return whether to
1392 // include it in the link.  If GROUP is true, this is a regular
1393 // section group.  If GROUP is false, this is a group signature
1394 // derived from the name of a linkonce section.  We want linkonce
1395 // signatures and group signatures to block each other, but we don't
1396 // want a linkonce signature to block another linkonce signature.
1397
1398 bool
1399 Layout::add_comdat(const char* signature, bool group)
1400 {
1401   std::string sig(signature);
1402   std::pair<Signatures::iterator, bool> ins(
1403     this->signatures_.insert(std::make_pair(sig, group)));
1404
1405   if (ins.second)
1406     {
1407       // This is the first time we've seen this signature.
1408       return true;
1409     }
1410
1411   if (ins.first->second)
1412     {
1413       // We've already seen a real section group with this signature.
1414       return false;
1415     }
1416   else if (group)
1417     {
1418       // This is a real section group, and we've already seen a
1419       // linkonce section with tihs signature.  Record that we've seen
1420       // a section group, and don't include this section group.
1421       ins.first->second = true;
1422       return false;
1423     }
1424   else
1425     {
1426       // We've already seen a linkonce section and this is a linkonce
1427       // section.  These don't block each other--this may be the same
1428       // symbol name with different section types.
1429       return true;
1430     }
1431 }
1432
1433 // Write out data not associated with a section or the symbol table.
1434
1435 void
1436 Layout::write_data(const Symbol_table* symtab, const Target* target,
1437                    Output_file* of) const
1438 {
1439   const Output_section* symtab_section = this->symtab_section_;
1440   for (Section_list::const_iterator p = this->section_list_.begin();
1441        p != this->section_list_.end();
1442        ++p)
1443     {
1444       if ((*p)->needs_symtab_index())
1445         {
1446           gold_assert(symtab_section != NULL);
1447           unsigned int index = (*p)->symtab_index();
1448           gold_assert(index > 0 && index != -1U);
1449           off_t off = (symtab_section->offset()
1450                        + index * symtab_section->entsize());
1451           symtab->write_section_symbol(target, *p, of, off);
1452         }
1453     }
1454
1455   const Output_section* dynsym_section = this->dynsym_section_;
1456   for (Section_list::const_iterator p = this->section_list_.begin();
1457        p != this->section_list_.end();
1458        ++p)
1459     {
1460       if ((*p)->needs_dynsym_index())
1461         {
1462           gold_assert(dynsym_section != NULL);
1463           unsigned int index = (*p)->dynsym_index();
1464           gold_assert(index > 0 && index != -1U);
1465           off_t off = (dynsym_section->offset()
1466                        + index * dynsym_section->entsize());
1467           symtab->write_section_symbol(target, *p, of, off);
1468         }
1469     }
1470
1471   // Write out the Output_sections.  Most won't have anything to
1472   // write, since most of the data will come from input sections which
1473   // are handled elsewhere.  But some Output_sections do have
1474   // Output_data.
1475   for (Section_list::const_iterator p = this->section_list_.begin();
1476        p != this->section_list_.end();
1477        ++p)
1478     (*p)->write(of);
1479
1480   // Write out the Output_data which are not in an Output_section.
1481   for (Data_list::const_iterator p = this->special_output_list_.begin();
1482        p != this->special_output_list_.end();
1483        ++p)
1484     (*p)->write(of);
1485 }
1486
1487 // Write_data_task methods.
1488
1489 // We can always run this task.
1490
1491 Task::Is_runnable_type
1492 Write_data_task::is_runnable(Workqueue*)
1493 {
1494   return IS_RUNNABLE;
1495 }
1496
1497 // We need to unlock FINAL_BLOCKER when finished.
1498
1499 Task_locker*
1500 Write_data_task::locks(Workqueue* workqueue)
1501 {
1502   return new Task_locker_block(*this->final_blocker_, workqueue);
1503 }
1504
1505 // Run the task--write out the data.
1506
1507 void
1508 Write_data_task::run(Workqueue*)
1509 {
1510   this->layout_->write_data(this->symtab_, this->target_, this->of_);
1511 }
1512
1513 // Write_symbols_task methods.
1514
1515 // We can always run this task.
1516
1517 Task::Is_runnable_type
1518 Write_symbols_task::is_runnable(Workqueue*)
1519 {
1520   return IS_RUNNABLE;
1521 }
1522
1523 // We need to unlock FINAL_BLOCKER when finished.
1524
1525 Task_locker*
1526 Write_symbols_task::locks(Workqueue* workqueue)
1527 {
1528   return new Task_locker_block(*this->final_blocker_, workqueue);
1529 }
1530
1531 // Run the task--write out the symbols.
1532
1533 void
1534 Write_symbols_task::run(Workqueue*)
1535 {
1536   this->symtab_->write_globals(this->target_, this->sympool_, this->dynpool_,
1537                                this->of_);
1538 }
1539
1540 // Close_task_runner methods.
1541
1542 // Run the task--close the file.
1543
1544 void
1545 Close_task_runner::run(Workqueue*)
1546 {
1547   this->of_->close();
1548 }
1549
1550 // Instantiate the templates we need.  We could use the configure
1551 // script to restrict this to only the ones for implemented targets.
1552
1553 #ifdef HAVE_TARGET_32_LITTLE
1554 template
1555 Output_section*
1556 Layout::layout<32, false>(Relobj* object, unsigned int shndx, const char* name,
1557                           const elfcpp::Shdr<32, false>& shdr, off_t*);
1558 #endif
1559
1560 #ifdef HAVE_TARGET_32_BIG
1561 template
1562 Output_section*
1563 Layout::layout<32, true>(Relobj* object, unsigned int shndx, const char* name,
1564                          const elfcpp::Shdr<32, true>& shdr, off_t*);
1565 #endif
1566
1567 #ifdef HAVE_TARGET_64_LITTLE
1568 template
1569 Output_section*
1570 Layout::layout<64, false>(Relobj* object, unsigned int shndx, const char* name,
1571                           const elfcpp::Shdr<64, false>& shdr, off_t*);
1572 #endif
1573
1574 #ifdef HAVE_TARGET_64_BIG
1575 template
1576 Output_section*
1577 Layout::layout<64, true>(Relobj* object, unsigned int shndx, const char* name,
1578                          const elfcpp::Shdr<64, true>& shdr, off_t*);
1579 #endif
1580
1581
1582 } // End namespace gold.