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