PR 3134
[deliverable/binutils-gdb.git] / gold / layout.cc
1 // layout.cc -- lay out output file sections for gold
2
3 // Copyright 2006, 2007, 2008 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 "options.h"
32 #include "script.h"
33 #include "script-sections.h"
34 #include "output.h"
35 #include "symtab.h"
36 #include "dynobj.h"
37 #include "ehframe.h"
38 #include "compressed_output.h"
39 #include "reloc.h"
40 #include "layout.h"
41
42 namespace gold
43 {
44
45 // Layout_task_runner methods.
46
47 // Lay out the sections. This is called after all the input objects
48 // have been read.
49
50 void
51 Layout_task_runner::run(Workqueue* workqueue, const Task* task)
52 {
53 off_t file_size = this->layout_->finalize(this->input_objects_,
54 this->symtab_,
55 task);
56
57 // Now we know the final size of the output file and we know where
58 // each piece of information goes.
59 Output_file* of = new Output_file(parameters->output_file_name());
60 if (this->options_.oformat() != General_options::OBJECT_FORMAT_ELF)
61 of->set_is_temporary();
62 of->open(file_size);
63
64 // Queue up the final set of tasks.
65 gold::queue_final_tasks(this->options_, this->input_objects_,
66 this->symtab_, this->layout_, workqueue, of);
67 }
68
69 // Layout methods.
70
71 Layout::Layout(const General_options& options, Script_options* script_options)
72 : options_(options), script_options_(script_options), namepool_(),
73 sympool_(), dynpool_(), signatures_(),
74 section_name_map_(), segment_list_(), section_list_(),
75 unattached_section_list_(), special_output_list_(),
76 section_headers_(NULL), tls_segment_(NULL), symtab_section_(NULL),
77 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL),
78 eh_frame_section_(NULL), group_signatures_(), output_file_size_(-1),
79 input_requires_executable_stack_(false),
80 input_with_gnu_stack_note_(false),
81 input_without_gnu_stack_note_(false),
82 has_static_tls_(false),
83 any_postprocessing_sections_(false)
84 {
85 // Make space for more than enough segments for a typical file.
86 // This is just for efficiency--it's OK if we wind up needing more.
87 this->segment_list_.reserve(12);
88
89 // We expect two unattached Output_data objects: the file header and
90 // the segment headers.
91 this->special_output_list_.reserve(2);
92 }
93
94 // Hash a key we use to look up an output section mapping.
95
96 size_t
97 Layout::Hash_key::operator()(const Layout::Key& k) const
98 {
99 return k.first + k.second.first + k.second.second;
100 }
101
102 // Return whether PREFIX is a prefix of STR.
103
104 static inline bool
105 is_prefix_of(const char* prefix, const char* str)
106 {
107 return strncmp(prefix, str, strlen(prefix)) == 0;
108 }
109
110 // Returns whether the given section is in the list of
111 // debug-sections-used-by-some-version-of-gdb. Currently,
112 // we've checked versions of gdb up to and including 6.7.1.
113
114 static const char* gdb_sections[] =
115 { ".debug_abbrev",
116 // ".debug_aranges", // not used by gdb as of 6.7.1
117 ".debug_frame",
118 ".debug_info",
119 ".debug_line",
120 ".debug_loc",
121 ".debug_macinfo",
122 // ".debug_pubnames", // not used by gdb as of 6.7.1
123 ".debug_ranges",
124 ".debug_str",
125 };
126
127 static inline bool
128 is_gdb_debug_section(const char* str)
129 {
130 // We can do this faster: binary search or a hashtable. But why bother?
131 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
132 if (strcmp(str, gdb_sections[i]) == 0)
133 return true;
134 return false;
135 }
136
137 // Whether to include this section in the link.
138
139 template<int size, bool big_endian>
140 bool
141 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
142 const elfcpp::Shdr<size, big_endian>& shdr)
143 {
144 switch (shdr.get_sh_type())
145 {
146 case elfcpp::SHT_NULL:
147 case elfcpp::SHT_SYMTAB:
148 case elfcpp::SHT_DYNSYM:
149 case elfcpp::SHT_STRTAB:
150 case elfcpp::SHT_HASH:
151 case elfcpp::SHT_DYNAMIC:
152 case elfcpp::SHT_SYMTAB_SHNDX:
153 return false;
154
155 case elfcpp::SHT_RELA:
156 case elfcpp::SHT_REL:
157 case elfcpp::SHT_GROUP:
158 // For a relocatable link these should be handled elsewhere.
159 gold_assert(!parameters->output_is_object());
160 return false;
161
162 case elfcpp::SHT_PROGBITS:
163 if (parameters->strip_debug()
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_prefix_of(".gnu.linkonce.wi.", name)
169 || is_prefix_of(".line", name)
170 || is_prefix_of(".stab", name))
171 return false;
172 }
173 if (parameters->strip_debug_gdb()
174 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
175 {
176 // Debugging sections can only be recognized by name.
177 if (is_prefix_of(".debug", name)
178 && !is_gdb_debug_section(name))
179 return false;
180 }
181 return true;
182
183 default:
184 return true;
185 }
186 }
187
188 // Return an output section named NAME, or NULL if there is none.
189
190 Output_section*
191 Layout::find_output_section(const char* name) const
192 {
193 for (Section_list::const_iterator p = this->section_list_.begin();
194 p != this->section_list_.end();
195 ++p)
196 if (strcmp((*p)->name(), name) == 0)
197 return *p;
198 return NULL;
199 }
200
201 // Return an output segment of type TYPE, with segment flags SET set
202 // and segment flags CLEAR clear. Return NULL if there is none.
203
204 Output_segment*
205 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
206 elfcpp::Elf_Word clear) const
207 {
208 for (Segment_list::const_iterator p = this->segment_list_.begin();
209 p != this->segment_list_.end();
210 ++p)
211 if (static_cast<elfcpp::PT>((*p)->type()) == type
212 && ((*p)->flags() & set) == set
213 && ((*p)->flags() & clear) == 0)
214 return *p;
215 return NULL;
216 }
217
218 // Return the output section to use for section NAME with type TYPE
219 // and section flags FLAGS. NAME must be canonicalized in the string
220 // pool, and NAME_KEY is the key.
221
222 Output_section*
223 Layout::get_output_section(const char* name, Stringpool::Key name_key,
224 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
225 {
226 const Key key(name_key, std::make_pair(type, flags));
227 const std::pair<Key, Output_section*> v(key, NULL);
228 std::pair<Section_name_map::iterator, bool> ins(
229 this->section_name_map_.insert(v));
230
231 if (!ins.second)
232 return ins.first->second;
233 else
234 {
235 // This is the first time we've seen this name/type/flags
236 // combination.
237 Output_section* os = this->make_output_section(name, type, flags);
238 ins.first->second = os;
239 return os;
240 }
241 }
242
243 // Pick the output section to use for section NAME, in input file
244 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
245 // linker created section. ADJUST_NAME is true if we should apply the
246 // standard name mappings in Layout::output_section_name. This will
247 // return NULL if the input section should be discarded.
248
249 Output_section*
250 Layout::choose_output_section(const Relobj* relobj, const char* name,
251 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
252 bool adjust_name)
253 {
254 // We should ignore some flags. FIXME: This will need some
255 // adjustment for ld -r.
256 flags &= ~ (elfcpp::SHF_INFO_LINK
257 | elfcpp::SHF_LINK_ORDER
258 | elfcpp::SHF_GROUP
259 | elfcpp::SHF_MERGE
260 | elfcpp::SHF_STRINGS);
261
262 if (this->script_options_->saw_sections_clause())
263 {
264 // We are using a SECTIONS clause, so the output section is
265 // chosen based only on the name.
266
267 Script_sections* ss = this->script_options_->script_sections();
268 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
269 Output_section** output_section_slot;
270 name = ss->output_section_name(file_name, name, &output_section_slot);
271 if (name == NULL)
272 {
273 // The SECTIONS clause says to discard this input section.
274 return NULL;
275 }
276
277 // If this is an orphan section--one not mentioned in the linker
278 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
279 // default processing below.
280
281 if (output_section_slot != NULL)
282 {
283 if (*output_section_slot != NULL)
284 return *output_section_slot;
285
286 // We don't put sections found in the linker script into
287 // SECTION_NAME_MAP_. That keeps us from getting confused
288 // if an orphan section is mapped to a section with the same
289 // name as one in the linker script.
290
291 name = this->namepool_.add(name, false, NULL);
292
293 Output_section* os = this->make_output_section(name, type, flags);
294 os->set_found_in_sections_clause();
295 *output_section_slot = os;
296 return os;
297 }
298 }
299
300 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
301
302 // Turn NAME from the name of the input section into the name of the
303 // output section.
304
305 size_t len = strlen(name);
306 if (adjust_name && !parameters->output_is_object())
307 name = Layout::output_section_name(name, &len);
308
309 Stringpool::Key name_key;
310 name = this->namepool_.add_with_length(name, len, true, &name_key);
311
312 // Find or make the output section. The output section is selected
313 // based on the section name, type, and flags.
314 return this->get_output_section(name, name_key, type, flags);
315 }
316
317 // Return the output section to use for input section SHNDX, with name
318 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
319 // index of a relocation section which applies to this section, or 0
320 // if none, or -1U if more than one. RELOC_TYPE is the type of the
321 // relocation section if there is one. Set *OFF to the offset of this
322 // input section without the output section. Return NULL if the
323 // section should be discarded. Set *OFF to -1 if the section
324 // contents should not be written directly to the output file, but
325 // will instead receive special handling.
326
327 template<int size, bool big_endian>
328 Output_section*
329 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
330 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
331 unsigned int reloc_shndx, unsigned int, off_t* off)
332 {
333 if (!this->include_section(object, name, shdr))
334 return NULL;
335
336 Output_section* os;
337
338 // In a relocatable link a grouped section must not be combined with
339 // any other sections.
340 if (parameters->output_is_object()
341 && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
342 {
343 name = this->namepool_.add(name, true, NULL);
344 os = this->make_output_section(name, shdr.get_sh_type(),
345 shdr.get_sh_flags());
346 }
347 else
348 {
349 os = this->choose_output_section(object, name, shdr.get_sh_type(),
350 shdr.get_sh_flags(), true);
351 if (os == NULL)
352 return NULL;
353 }
354
355 // FIXME: Handle SHF_LINK_ORDER somewhere.
356
357 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
358 this->script_options_->saw_sections_clause());
359
360 return os;
361 }
362
363 // Handle a relocation section when doing a relocatable link.
364
365 template<int size, bool big_endian>
366 Output_section*
367 Layout::layout_reloc(Sized_relobj<size, big_endian>* object,
368 unsigned int,
369 const elfcpp::Shdr<size, big_endian>& shdr,
370 Output_section* data_section,
371 Relocatable_relocs* rr)
372 {
373 gold_assert(parameters->output_is_object());
374
375 int sh_type = shdr.get_sh_type();
376
377 std::string name;
378 if (sh_type == elfcpp::SHT_REL)
379 name = ".rel";
380 else if (sh_type == elfcpp::SHT_RELA)
381 name = ".rela";
382 else
383 gold_unreachable();
384 name += data_section->name();
385
386 Output_section* os = this->choose_output_section(object, name.c_str(),
387 sh_type,
388 shdr.get_sh_flags(),
389 false);
390
391 os->set_should_link_to_symtab();
392 os->set_info_section(data_section);
393
394 Output_section_data* posd;
395 if (sh_type == elfcpp::SHT_REL)
396 {
397 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
398 posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
399 size,
400 big_endian>(rr);
401 }
402 else if (sh_type == elfcpp::SHT_RELA)
403 {
404 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
405 posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
406 size,
407 big_endian>(rr);
408 }
409 else
410 gold_unreachable();
411
412 os->add_output_section_data(posd);
413 rr->set_output_data(posd);
414
415 return os;
416 }
417
418 // Handle a group section when doing a relocatable link.
419
420 template<int size, bool big_endian>
421 void
422 Layout::layout_group(Symbol_table* symtab,
423 Sized_relobj<size, big_endian>* object,
424 unsigned int,
425 const char* group_section_name,
426 const char* signature,
427 const elfcpp::Shdr<size, big_endian>& shdr,
428 const elfcpp::Elf_Word* contents)
429 {
430 gold_assert(parameters->output_is_object());
431 gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
432 group_section_name = this->namepool_.add(group_section_name, true, NULL);
433 Output_section* os = this->make_output_section(group_section_name,
434 elfcpp::SHT_GROUP,
435 shdr.get_sh_flags());
436
437 // We need to find a symbol with the signature in the symbol table.
438 // If we don't find one now, we need to look again later.
439 Symbol* sym = symtab->lookup(signature, NULL);
440 if (sym != NULL)
441 os->set_info_symndx(sym);
442 else
443 {
444 // We will wind up using a symbol whose name is the signature.
445 // So just put the signature in the symbol name pool to save it.
446 signature = symtab->canonicalize_name(signature);
447 this->group_signatures_.push_back(Group_signature(os, signature));
448 }
449
450 os->set_should_link_to_symtab();
451 os->set_entsize(4);
452
453 section_size_type entry_count =
454 convert_to_section_size_type(shdr.get_sh_size() / 4);
455 Output_section_data* posd =
456 new Output_data_group<size, big_endian>(object, entry_count, contents);
457 os->add_output_section_data(posd);
458 }
459
460 // Special GNU handling of sections name .eh_frame. They will
461 // normally hold exception frame data as defined by the C++ ABI
462 // (http://codesourcery.com/cxx-abi/).
463
464 template<int size, bool big_endian>
465 Output_section*
466 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
467 const unsigned char* symbols,
468 off_t symbols_size,
469 const unsigned char* symbol_names,
470 off_t symbol_names_size,
471 unsigned int shndx,
472 const elfcpp::Shdr<size, big_endian>& shdr,
473 unsigned int reloc_shndx, unsigned int reloc_type,
474 off_t* off)
475 {
476 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
477 gold_assert(shdr.get_sh_flags() == elfcpp::SHF_ALLOC);
478
479 const char* const name = ".eh_frame";
480 Output_section* os = this->choose_output_section(object,
481 name,
482 elfcpp::SHT_PROGBITS,
483 elfcpp::SHF_ALLOC,
484 false);
485 if (os == NULL)
486 return NULL;
487
488 if (this->eh_frame_section_ == NULL)
489 {
490 this->eh_frame_section_ = os;
491 this->eh_frame_data_ = new Eh_frame();
492 os->add_output_section_data(this->eh_frame_data_);
493
494 if (this->options_.eh_frame_hdr())
495 {
496 Output_section* hdr_os =
497 this->choose_output_section(NULL,
498 ".eh_frame_hdr",
499 elfcpp::SHT_PROGBITS,
500 elfcpp::SHF_ALLOC,
501 false);
502
503 if (hdr_os != NULL)
504 {
505 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
506 this->eh_frame_data_);
507 hdr_os->add_output_section_data(hdr_posd);
508
509 hdr_os->set_after_input_sections();
510
511 if (!this->script_options_->saw_phdrs_clause())
512 {
513 Output_segment* hdr_oseg;
514 hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
515 elfcpp::PF_R);
516 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
517 }
518
519 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
520 }
521 }
522 }
523
524 gold_assert(this->eh_frame_section_ == os);
525
526 if (this->eh_frame_data_->add_ehframe_input_section(object,
527 symbols,
528 symbols_size,
529 symbol_names,
530 symbol_names_size,
531 shndx,
532 reloc_shndx,
533 reloc_type))
534 *off = -1;
535 else
536 {
537 // We couldn't handle this .eh_frame section for some reason.
538 // Add it as a normal section.
539 bool saw_sections_clause = this->script_options_->saw_sections_clause();
540 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
541 saw_sections_clause);
542 }
543
544 return os;
545 }
546
547 // Add POSD to an output section using NAME, TYPE, and FLAGS.
548
549 void
550 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
551 elfcpp::Elf_Xword flags,
552 Output_section_data* posd)
553 {
554 Output_section* os = this->choose_output_section(NULL, name, type, flags,
555 false);
556 if (os != NULL)
557 os->add_output_section_data(posd);
558 }
559
560 // Map section flags to segment flags.
561
562 elfcpp::Elf_Word
563 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
564 {
565 elfcpp::Elf_Word ret = elfcpp::PF_R;
566 if ((flags & elfcpp::SHF_WRITE) != 0)
567 ret |= elfcpp::PF_W;
568 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
569 ret |= elfcpp::PF_X;
570 return ret;
571 }
572
573 // Sometimes we compress sections. This is typically done for
574 // sections that are not part of normal program execution (such as
575 // .debug_* sections), and where the readers of these sections know
576 // how to deal with compressed sections. (To make it easier for them,
577 // we will rename the ouput section in such cases from .foo to
578 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
579 // doesn't say for certain whether we'll compress -- it depends on
580 // commandline options as well -- just whether this section is a
581 // candidate for compression.
582
583 static bool
584 is_compressible_debug_section(const char* secname)
585 {
586 return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
587 }
588
589 // Make a new Output_section, and attach it to segments as
590 // appropriate.
591
592 Output_section*
593 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
594 elfcpp::Elf_Xword flags)
595 {
596 Output_section* os;
597 if ((flags & elfcpp::SHF_ALLOC) == 0
598 && this->options_.compress_debug_sections()
599 && is_compressible_debug_section(name))
600 os = new Output_compressed_section(&this->options_, name, type, flags);
601 else
602 os = new Output_section(name, type, flags);
603
604 this->section_list_.push_back(os);
605
606 if ((flags & elfcpp::SHF_ALLOC) == 0)
607 this->unattached_section_list_.push_back(os);
608 else
609 {
610 if (parameters->output_is_object())
611 return os;
612
613 // If we have a SECTIONS clause, we can't handle the attachment
614 // to segments until after we've seen all the sections.
615 if (this->script_options_->saw_sections_clause())
616 return os;
617
618 gold_assert(!this->script_options_->saw_phdrs_clause());
619
620 // This output section goes into a PT_LOAD segment.
621
622 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
623
624 // In general the only thing we really care about for PT_LOAD
625 // segments is whether or not they are writable, so that is how
626 // we search for them. People who need segments sorted on some
627 // other basis will have to use a linker script.
628
629 Segment_list::const_iterator p;
630 for (p = this->segment_list_.begin();
631 p != this->segment_list_.end();
632 ++p)
633 {
634 if ((*p)->type() == elfcpp::PT_LOAD
635 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
636 {
637 // If -Tbss was specified, we need to separate the data
638 // and BSS segments.
639 if (this->options_.user_set_Tbss())
640 {
641 if ((type == elfcpp::SHT_NOBITS)
642 == (*p)->has_any_data_sections())
643 continue;
644 }
645
646 (*p)->add_output_section(os, seg_flags);
647 break;
648 }
649 }
650
651 if (p == this->segment_list_.end())
652 {
653 Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
654 seg_flags);
655 oseg->add_output_section(os, seg_flags);
656 }
657
658 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
659 // segment.
660 if (type == elfcpp::SHT_NOTE)
661 {
662 // See if we already have an equivalent PT_NOTE segment.
663 for (p = this->segment_list_.begin();
664 p != segment_list_.end();
665 ++p)
666 {
667 if ((*p)->type() == elfcpp::PT_NOTE
668 && (((*p)->flags() & elfcpp::PF_W)
669 == (seg_flags & elfcpp::PF_W)))
670 {
671 (*p)->add_output_section(os, seg_flags);
672 break;
673 }
674 }
675
676 if (p == this->segment_list_.end())
677 {
678 Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
679 seg_flags);
680 oseg->add_output_section(os, seg_flags);
681 }
682 }
683
684 // If we see a loadable SHF_TLS section, we create a PT_TLS
685 // segment. There can only be one such segment.
686 if ((flags & elfcpp::SHF_TLS) != 0)
687 {
688 if (this->tls_segment_ == NULL)
689 this->tls_segment_ = this->make_output_segment(elfcpp::PT_TLS,
690 seg_flags);
691 this->tls_segment_->add_output_section(os, seg_flags);
692 }
693 }
694
695 return os;
696 }
697
698 // Return the number of segments we expect to see.
699
700 size_t
701 Layout::expected_segment_count() const
702 {
703 size_t ret = this->segment_list_.size();
704
705 // If we didn't see a SECTIONS clause in a linker script, we should
706 // already have the complete list of segments. Otherwise we ask the
707 // SECTIONS clause how many segments it expects, and add in the ones
708 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
709
710 if (!this->script_options_->saw_sections_clause())
711 return ret;
712 else
713 {
714 const Script_sections* ss = this->script_options_->script_sections();
715 return ret + ss->expected_segment_count(this);
716 }
717 }
718
719 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
720 // is whether we saw a .note.GNU-stack section in the object file.
721 // GNU_STACK_FLAGS is the section flags. The flags give the
722 // protection required for stack memory. We record this in an
723 // executable as a PT_GNU_STACK segment. If an object file does not
724 // have a .note.GNU-stack segment, we must assume that it is an old
725 // object. On some targets that will force an executable stack.
726
727 void
728 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
729 {
730 if (!seen_gnu_stack)
731 this->input_without_gnu_stack_note_ = true;
732 else
733 {
734 this->input_with_gnu_stack_note_ = true;
735 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
736 this->input_requires_executable_stack_ = true;
737 }
738 }
739
740 // Create the dynamic sections which are needed before we read the
741 // relocs.
742
743 void
744 Layout::create_initial_dynamic_sections(Symbol_table* symtab)
745 {
746 if (parameters->doing_static_link())
747 return;
748
749 this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
750 elfcpp::SHT_DYNAMIC,
751 (elfcpp::SHF_ALLOC
752 | elfcpp::SHF_WRITE),
753 false);
754
755 symtab->define_in_output_data("_DYNAMIC", NULL, this->dynamic_section_, 0, 0,
756 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
757 elfcpp::STV_HIDDEN, 0, false, false);
758
759 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
760
761 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
762 }
763
764 // For each output section whose name can be represented as C symbol,
765 // define __start and __stop symbols for the section. This is a GNU
766 // extension.
767
768 void
769 Layout::define_section_symbols(Symbol_table* symtab)
770 {
771 for (Section_list::const_iterator p = this->section_list_.begin();
772 p != this->section_list_.end();
773 ++p)
774 {
775 const char* const name = (*p)->name();
776 if (name[strspn(name,
777 ("0123456789"
778 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
779 "abcdefghijklmnopqrstuvwxyz"
780 "_"))]
781 == '\0')
782 {
783 const std::string name_string(name);
784 const std::string start_name("__start_" + name_string);
785 const std::string stop_name("__stop_" + name_string);
786
787 symtab->define_in_output_data(start_name.c_str(),
788 NULL, // version
789 *p,
790 0, // value
791 0, // symsize
792 elfcpp::STT_NOTYPE,
793 elfcpp::STB_GLOBAL,
794 elfcpp::STV_DEFAULT,
795 0, // nonvis
796 false, // offset_is_from_end
797 true); // only_if_ref
798
799 symtab->define_in_output_data(stop_name.c_str(),
800 NULL, // version
801 *p,
802 0, // value
803 0, // symsize
804 elfcpp::STT_NOTYPE,
805 elfcpp::STB_GLOBAL,
806 elfcpp::STV_DEFAULT,
807 0, // nonvis
808 true, // offset_is_from_end
809 true); // only_if_ref
810 }
811 }
812 }
813
814 // Define symbols for group signatures.
815
816 void
817 Layout::define_group_signatures(Symbol_table* symtab)
818 {
819 for (Group_signatures::iterator p = this->group_signatures_.begin();
820 p != this->group_signatures_.end();
821 ++p)
822 {
823 Symbol* sym = symtab->lookup(p->signature, NULL);
824 if (sym != NULL)
825 p->section->set_info_symndx(sym);
826 else
827 {
828 // Force the name of the group section to the group
829 // signature, and use the group's section symbol as the
830 // signature symbol.
831 if (strcmp(p->section->name(), p->signature) != 0)
832 {
833 const char* name = this->namepool_.add(p->signature,
834 true, NULL);
835 p->section->set_name(name);
836 }
837 p->section->set_needs_symtab_index();
838 p->section->set_info_section_symndx(p->section);
839 }
840 }
841
842 this->group_signatures_.clear();
843 }
844
845 // Find the first read-only PT_LOAD segment, creating one if
846 // necessary.
847
848 Output_segment*
849 Layout::find_first_load_seg()
850 {
851 for (Segment_list::const_iterator p = this->segment_list_.begin();
852 p != this->segment_list_.end();
853 ++p)
854 {
855 if ((*p)->type() == elfcpp::PT_LOAD
856 && ((*p)->flags() & elfcpp::PF_R) != 0
857 && ((*p)->flags() & elfcpp::PF_W) == 0)
858 return *p;
859 }
860
861 gold_assert(!this->script_options_->saw_phdrs_clause());
862
863 Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
864 elfcpp::PF_R);
865 return load_seg;
866 }
867
868 // Finalize the layout. When this is called, we have created all the
869 // output sections and all the output segments which are based on
870 // input sections. We have several things to do, and we have to do
871 // them in the right order, so that we get the right results correctly
872 // and efficiently.
873
874 // 1) Finalize the list of output segments and create the segment
875 // table header.
876
877 // 2) Finalize the dynamic symbol table and associated sections.
878
879 // 3) Determine the final file offset of all the output segments.
880
881 // 4) Determine the final file offset of all the SHF_ALLOC output
882 // sections.
883
884 // 5) Create the symbol table sections and the section name table
885 // section.
886
887 // 6) Finalize the symbol table: set symbol values to their final
888 // value and make a final determination of which symbols are going
889 // into the output symbol table.
890
891 // 7) Create the section table header.
892
893 // 8) Determine the final file offset of all the output sections which
894 // are not SHF_ALLOC, including the section table header.
895
896 // 9) Finalize the ELF file header.
897
898 // This function returns the size of the output file.
899
900 off_t
901 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
902 const Task* task)
903 {
904 Target* const target = parameters->target();
905
906 target->finalize_sections(this);
907
908 this->count_local_symbols(task, input_objects);
909
910 this->create_gold_note();
911 this->create_executable_stack_info(target);
912
913 Output_segment* phdr_seg = NULL;
914 if (!parameters->output_is_object() && !parameters->doing_static_link())
915 {
916 // There was a dynamic object in the link. We need to create
917 // some information for the dynamic linker.
918
919 // Create the PT_PHDR segment which will hold the program
920 // headers.
921 if (!this->script_options_->saw_phdrs_clause())
922 phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
923
924 // Create the dynamic symbol table, including the hash table.
925 Output_section* dynstr;
926 std::vector<Symbol*> dynamic_symbols;
927 unsigned int local_dynamic_count;
928 Versions versions(*this->script_options()->version_script_info(),
929 &this->dynpool_);
930 this->create_dynamic_symtab(input_objects, symtab, &dynstr,
931 &local_dynamic_count, &dynamic_symbols,
932 &versions);
933
934 // Create the .interp section to hold the name of the
935 // interpreter, and put it in a PT_INTERP segment.
936 if (!parameters->output_is_shared())
937 this->create_interp(target);
938
939 // Finish the .dynamic section to hold the dynamic data, and put
940 // it in a PT_DYNAMIC segment.
941 this->finish_dynamic_section(input_objects, symtab);
942
943 // We should have added everything we need to the dynamic string
944 // table.
945 this->dynpool_.set_string_offsets();
946
947 // Create the version sections. We can't do this until the
948 // dynamic string table is complete.
949 this->create_version_sections(&versions, symtab, local_dynamic_count,
950 dynamic_symbols, dynstr);
951 }
952
953 // If there is a SECTIONS clause, put all the input sections into
954 // the required order.
955 Output_segment* load_seg;
956 if (this->script_options_->saw_sections_clause())
957 load_seg = this->set_section_addresses_from_script(symtab);
958 else if (parameters->output_is_object())
959 load_seg = NULL;
960 else
961 load_seg = this->find_first_load_seg();
962
963 if (this->options_.oformat() != General_options::OBJECT_FORMAT_ELF)
964 load_seg = NULL;
965
966 gold_assert(phdr_seg == NULL || load_seg != NULL);
967
968 // Lay out the segment headers.
969 Output_segment_headers* segment_headers;
970 if (parameters->output_is_object())
971 segment_headers = NULL;
972 else
973 {
974 segment_headers = new Output_segment_headers(this->segment_list_);
975 if (load_seg != NULL)
976 load_seg->add_initial_output_data(segment_headers);
977 if (phdr_seg != NULL)
978 phdr_seg->add_initial_output_data(segment_headers);
979 }
980
981 // Lay out the file header.
982 Output_file_header* file_header;
983 file_header = new Output_file_header(target, symtab, segment_headers,
984 this->options_.entry());
985 if (load_seg != NULL)
986 load_seg->add_initial_output_data(file_header);
987
988 this->special_output_list_.push_back(file_header);
989 if (segment_headers != NULL)
990 this->special_output_list_.push_back(segment_headers);
991
992 if (this->script_options_->saw_phdrs_clause()
993 && !parameters->output_is_object())
994 {
995 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
996 // clause in a linker script.
997 Script_sections* ss = this->script_options_->script_sections();
998 ss->put_headers_in_phdrs(file_header, segment_headers);
999 }
1000
1001 // We set the output section indexes in set_segment_offsets and
1002 // set_section_indexes.
1003 unsigned int shndx = 1;
1004
1005 // Set the file offsets of all the segments, and all the sections
1006 // they contain.
1007 off_t off;
1008 if (!parameters->output_is_object())
1009 off = this->set_segment_offsets(target, load_seg, &shndx);
1010 else
1011 off = this->set_relocatable_section_offsets(file_header, &shndx);
1012
1013 // Set the file offsets of all the non-data sections we've seen so
1014 // far which don't have to wait for the input sections. We need
1015 // this in order to finalize local symbols in non-allocated
1016 // sections.
1017 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1018
1019 // Create the symbol table sections.
1020 this->create_symtab_sections(input_objects, symtab, &off);
1021 if (!parameters->doing_static_link())
1022 this->assign_local_dynsym_offsets(input_objects);
1023
1024 // Process any symbol assignments from a linker script. This must
1025 // be called after the symbol table has been finalized.
1026 this->script_options_->finalize_symbols(symtab, this);
1027
1028 // Create the .shstrtab section.
1029 Output_section* shstrtab_section = this->create_shstrtab();
1030
1031 // Set the file offsets of the rest of the non-data sections which
1032 // don't have to wait for the input sections.
1033 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1034
1035 // Now that all sections have been created, set the section indexes.
1036 shndx = this->set_section_indexes(shndx);
1037
1038 // Create the section table header.
1039 this->create_shdrs(&off);
1040
1041 // If there are no sections which require postprocessing, we can
1042 // handle the section names now, and avoid a resize later.
1043 if (!this->any_postprocessing_sections_)
1044 off = this->set_section_offsets(off,
1045 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
1046
1047 file_header->set_section_info(this->section_headers_, shstrtab_section);
1048
1049 // Now we know exactly where everything goes in the output file
1050 // (except for non-allocated sections which require postprocessing).
1051 Output_data::layout_complete();
1052
1053 this->output_file_size_ = off;
1054
1055 return off;
1056 }
1057
1058 // Create a .note section for an executable or shared library. This
1059 // records the version of gold used to create the binary.
1060
1061 void
1062 Layout::create_gold_note()
1063 {
1064 if (parameters->output_is_object())
1065 return;
1066
1067 // Authorities all agree that the values in a .note field should
1068 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1069 // they differ on what the alignment is for 64-bit binaries.
1070 // The GABI says unambiguously they take 8-byte alignment:
1071 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1072 // Other documentation says alignment should always be 4 bytes:
1073 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1074 // GNU ld and GNU readelf both support the latter (at least as of
1075 // version 2.16.91), and glibc always generates the latter for
1076 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1077 // here.
1078 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1079 const int size = parameters->get_size();
1080 #else
1081 const int size = 32;
1082 #endif
1083
1084 // The contents of the .note section.
1085 const char* name = "GNU";
1086 std::string desc(std::string("gold ") + gold::get_version_string());
1087 size_t namesz = strlen(name) + 1;
1088 size_t aligned_namesz = align_address(namesz, size / 8);
1089 size_t descsz = desc.length() + 1;
1090 size_t aligned_descsz = align_address(descsz, size / 8);
1091 const int note_type = 4;
1092
1093 size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz;
1094
1095 unsigned char buffer[128];
1096 gold_assert(sizeof buffer >= notesz);
1097 memset(buffer, 0, notesz);
1098
1099 bool is_big_endian = parameters->is_big_endian();
1100
1101 if (size == 32)
1102 {
1103 if (!is_big_endian)
1104 {
1105 elfcpp::Swap<32, false>::writeval(buffer, namesz);
1106 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
1107 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
1108 }
1109 else
1110 {
1111 elfcpp::Swap<32, true>::writeval(buffer, namesz);
1112 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
1113 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
1114 }
1115 }
1116 else if (size == 64)
1117 {
1118 if (!is_big_endian)
1119 {
1120 elfcpp::Swap<64, false>::writeval(buffer, namesz);
1121 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
1122 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
1123 }
1124 else
1125 {
1126 elfcpp::Swap<64, true>::writeval(buffer, namesz);
1127 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
1128 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
1129 }
1130 }
1131 else
1132 gold_unreachable();
1133
1134 memcpy(buffer + 3 * (size / 8), name, namesz);
1135 memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz);
1136
1137 const char* note_name = this->namepool_.add(".note", false, NULL);
1138 Output_section* os = this->make_output_section(note_name,
1139 elfcpp::SHT_NOTE,
1140 0);
1141 Output_section_data* posd = new Output_data_const(buffer, notesz,
1142 size / 8);
1143 os->add_output_section_data(posd);
1144 }
1145
1146 // Record whether the stack should be executable. This can be set
1147 // from the command line using the -z execstack or -z noexecstack
1148 // options. Otherwise, if any input file has a .note.GNU-stack
1149 // section with the SHF_EXECINSTR flag set, the stack should be
1150 // executable. Otherwise, if at least one input file a
1151 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1152 // section, we use the target default for whether the stack should be
1153 // executable. Otherwise, we don't generate a stack note. When
1154 // generating a object file, we create a .note.GNU-stack section with
1155 // the appropriate marking. When generating an executable or shared
1156 // library, we create a PT_GNU_STACK segment.
1157
1158 void
1159 Layout::create_executable_stack_info(const Target* target)
1160 {
1161 bool is_stack_executable;
1162 if (this->options_.is_execstack_set())
1163 is_stack_executable = this->options_.is_stack_executable();
1164 else if (!this->input_with_gnu_stack_note_)
1165 return;
1166 else
1167 {
1168 if (this->input_requires_executable_stack_)
1169 is_stack_executable = true;
1170 else if (this->input_without_gnu_stack_note_)
1171 is_stack_executable = target->is_default_stack_executable();
1172 else
1173 is_stack_executable = false;
1174 }
1175
1176 if (parameters->output_is_object())
1177 {
1178 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
1179 elfcpp::Elf_Xword flags = 0;
1180 if (is_stack_executable)
1181 flags |= elfcpp::SHF_EXECINSTR;
1182 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
1183 }
1184 else
1185 {
1186 if (this->script_options_->saw_phdrs_clause())
1187 return;
1188 int flags = elfcpp::PF_R | elfcpp::PF_W;
1189 if (is_stack_executable)
1190 flags |= elfcpp::PF_X;
1191 this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
1192 }
1193 }
1194
1195 // Return whether SEG1 should be before SEG2 in the output file. This
1196 // is based entirely on the segment type and flags. When this is
1197 // called the segment addresses has normally not yet been set.
1198
1199 bool
1200 Layout::segment_precedes(const Output_segment* seg1,
1201 const Output_segment* seg2)
1202 {
1203 elfcpp::Elf_Word type1 = seg1->type();
1204 elfcpp::Elf_Word type2 = seg2->type();
1205
1206 // The single PT_PHDR segment is required to precede any loadable
1207 // segment. We simply make it always first.
1208 if (type1 == elfcpp::PT_PHDR)
1209 {
1210 gold_assert(type2 != elfcpp::PT_PHDR);
1211 return true;
1212 }
1213 if (type2 == elfcpp::PT_PHDR)
1214 return false;
1215
1216 // The single PT_INTERP segment is required to precede any loadable
1217 // segment. We simply make it always second.
1218 if (type1 == elfcpp::PT_INTERP)
1219 {
1220 gold_assert(type2 != elfcpp::PT_INTERP);
1221 return true;
1222 }
1223 if (type2 == elfcpp::PT_INTERP)
1224 return false;
1225
1226 // We then put PT_LOAD segments before any other segments.
1227 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
1228 return true;
1229 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
1230 return false;
1231
1232 // We put the PT_TLS segment last, because that is where the dynamic
1233 // linker expects to find it (this is just for efficiency; other
1234 // positions would also work correctly).
1235 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
1236 return false;
1237 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
1238 return true;
1239
1240 const elfcpp::Elf_Word flags1 = seg1->flags();
1241 const elfcpp::Elf_Word flags2 = seg2->flags();
1242
1243 // The order of non-PT_LOAD segments is unimportant. We simply sort
1244 // by the numeric segment type and flags values. There should not
1245 // be more than one segment with the same type and flags.
1246 if (type1 != elfcpp::PT_LOAD)
1247 {
1248 if (type1 != type2)
1249 return type1 < type2;
1250 gold_assert(flags1 != flags2);
1251 return flags1 < flags2;
1252 }
1253
1254 // If the addresses are set already, sort by load address.
1255 if (seg1->are_addresses_set())
1256 {
1257 if (!seg2->are_addresses_set())
1258 return true;
1259
1260 unsigned int section_count1 = seg1->output_section_count();
1261 unsigned int section_count2 = seg2->output_section_count();
1262 if (section_count1 == 0 && section_count2 > 0)
1263 return true;
1264 if (section_count1 > 0 && section_count2 == 0)
1265 return false;
1266
1267 uint64_t paddr1 = seg1->first_section_load_address();
1268 uint64_t paddr2 = seg2->first_section_load_address();
1269 if (paddr1 != paddr2)
1270 return paddr1 < paddr2;
1271 }
1272 else if (seg2->are_addresses_set())
1273 return false;
1274
1275 // We sort PT_LOAD segments based on the flags. Readonly segments
1276 // come before writable segments. Then writable segments with data
1277 // come before writable segments without data. Then executable
1278 // segments come before non-executable segments. Then the unlikely
1279 // case of a non-readable segment comes before the normal case of a
1280 // readable segment. If there are multiple segments with the same
1281 // type and flags, we require that the address be set, and we sort
1282 // by virtual address and then physical address.
1283 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
1284 return (flags1 & elfcpp::PF_W) == 0;
1285 if ((flags1 & elfcpp::PF_W) != 0
1286 && seg1->has_any_data_sections() != seg2->has_any_data_sections())
1287 return seg1->has_any_data_sections();
1288 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
1289 return (flags1 & elfcpp::PF_X) != 0;
1290 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
1291 return (flags1 & elfcpp::PF_R) == 0;
1292
1293 // We shouldn't get here--we shouldn't create segments which we
1294 // can't distinguish.
1295 gold_unreachable();
1296 }
1297
1298 // Set the file offsets of all the segments, and all the sections they
1299 // contain. They have all been created. LOAD_SEG must be be laid out
1300 // first. Return the offset of the data to follow.
1301
1302 off_t
1303 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
1304 unsigned int *pshndx)
1305 {
1306 // Sort them into the final order.
1307 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
1308 Layout::Compare_segments());
1309
1310 // Find the PT_LOAD segments, and set their addresses and offsets
1311 // and their section's addresses and offsets.
1312 uint64_t addr;
1313 if (this->options_.user_set_Ttext())
1314 addr = this->options_.Ttext();
1315 else if (parameters->output_is_shared())
1316 addr = 0;
1317 else
1318 addr = target->default_text_segment_address();
1319 off_t off = 0;
1320
1321 // If LOAD_SEG is NULL, then the file header and segment headers
1322 // will not be loadable. But they still need to be at offset 0 in
1323 // the file. Set their offsets now.
1324 if (load_seg == NULL)
1325 {
1326 for (Data_list::iterator p = this->special_output_list_.begin();
1327 p != this->special_output_list_.end();
1328 ++p)
1329 {
1330 off = align_address(off, (*p)->addralign());
1331 (*p)->set_address_and_file_offset(0, off);
1332 off += (*p)->data_size();
1333 }
1334 }
1335
1336 bool was_readonly = false;
1337 for (Segment_list::iterator p = this->segment_list_.begin();
1338 p != this->segment_list_.end();
1339 ++p)
1340 {
1341 if ((*p)->type() == elfcpp::PT_LOAD)
1342 {
1343 if (load_seg != NULL && load_seg != *p)
1344 gold_unreachable();
1345 load_seg = NULL;
1346
1347 bool are_addresses_set = (*p)->are_addresses_set();
1348 if (are_addresses_set)
1349 {
1350 // When it comes to setting file offsets, we care about
1351 // the physical address.
1352 addr = (*p)->paddr();
1353 }
1354 else if (this->options_.user_set_Tdata()
1355 && ((*p)->flags() & elfcpp::PF_W) != 0
1356 && (!this->options_.user_set_Tbss()
1357 || (*p)->has_any_data_sections()))
1358 {
1359 addr = this->options_.Tdata();
1360 are_addresses_set = true;
1361 }
1362 else if (this->options_.user_set_Tbss()
1363 && ((*p)->flags() & elfcpp::PF_W) != 0
1364 && !(*p)->has_any_data_sections())
1365 {
1366 addr = this->options_.Tbss();
1367 are_addresses_set = true;
1368 }
1369
1370 uint64_t orig_addr = addr;
1371 uint64_t orig_off = off;
1372
1373 uint64_t aligned_addr = 0;
1374 uint64_t abi_pagesize = target->abi_pagesize();
1375
1376 // FIXME: This should depend on the -n and -N options.
1377 (*p)->set_minimum_p_align(target->common_pagesize());
1378
1379 if (are_addresses_set)
1380 {
1381 // Adjust the file offset to the same address modulo the
1382 // page size.
1383 uint64_t unsigned_off = off;
1384 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
1385 | (addr & (abi_pagesize - 1)));
1386 if (aligned_off < unsigned_off)
1387 aligned_off += abi_pagesize;
1388 off = aligned_off;
1389 }
1390 else
1391 {
1392 // If the last segment was readonly, and this one is
1393 // not, then skip the address forward one page,
1394 // maintaining the same position within the page. This
1395 // lets us store both segments overlapping on a single
1396 // page in the file, but the loader will put them on
1397 // different pages in memory.
1398
1399 addr = align_address(addr, (*p)->maximum_alignment());
1400 aligned_addr = addr;
1401
1402 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
1403 {
1404 if ((addr & (abi_pagesize - 1)) != 0)
1405 addr = addr + abi_pagesize;
1406 }
1407
1408 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1409 }
1410
1411 unsigned int shndx_hold = *pshndx;
1412 uint64_t new_addr = (*p)->set_section_addresses(false, addr, &off,
1413 pshndx);
1414
1415 // Now that we know the size of this segment, we may be able
1416 // to save a page in memory, at the cost of wasting some
1417 // file space, by instead aligning to the start of a new
1418 // page. Here we use the real machine page size rather than
1419 // the ABI mandated page size.
1420
1421 if (!are_addresses_set && aligned_addr != addr)
1422 {
1423 uint64_t common_pagesize = target->common_pagesize();
1424 uint64_t first_off = (common_pagesize
1425 - (aligned_addr
1426 & (common_pagesize - 1)));
1427 uint64_t last_off = new_addr & (common_pagesize - 1);
1428 if (first_off > 0
1429 && last_off > 0
1430 && ((aligned_addr & ~ (common_pagesize - 1))
1431 != (new_addr & ~ (common_pagesize - 1)))
1432 && first_off + last_off <= common_pagesize)
1433 {
1434 *pshndx = shndx_hold;
1435 addr = align_address(aligned_addr, common_pagesize);
1436 addr = align_address(addr, (*p)->maximum_alignment());
1437 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1438 new_addr = (*p)->set_section_addresses(true, addr, &off,
1439 pshndx);
1440 }
1441 }
1442
1443 addr = new_addr;
1444
1445 if (((*p)->flags() & elfcpp::PF_W) == 0)
1446 was_readonly = true;
1447 }
1448 }
1449
1450 // Handle the non-PT_LOAD segments, setting their offsets from their
1451 // section's offsets.
1452 for (Segment_list::iterator p = this->segment_list_.begin();
1453 p != this->segment_list_.end();
1454 ++p)
1455 {
1456 if ((*p)->type() != elfcpp::PT_LOAD)
1457 (*p)->set_offset();
1458 }
1459
1460 // Set the TLS offsets for each section in the PT_TLS segment.
1461 if (this->tls_segment_ != NULL)
1462 this->tls_segment_->set_tls_offsets();
1463
1464 return off;
1465 }
1466
1467 // Set the offsets of all the allocated sections when doing a
1468 // relocatable link. This does the same jobs as set_segment_offsets,
1469 // only for a relocatable link.
1470
1471 off_t
1472 Layout::set_relocatable_section_offsets(Output_data* file_header,
1473 unsigned int *pshndx)
1474 {
1475 off_t off = 0;
1476
1477 file_header->set_address_and_file_offset(0, 0);
1478 off += file_header->data_size();
1479
1480 for (Section_list::iterator p = this->section_list_.begin();
1481 p != this->section_list_.end();
1482 ++p)
1483 {
1484 // We skip unallocated sections here, except that group sections
1485 // have to come first.
1486 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
1487 && (*p)->type() != elfcpp::SHT_GROUP)
1488 continue;
1489
1490 off = align_address(off, (*p)->addralign());
1491
1492 // The linker script might have set the address.
1493 if (!(*p)->is_address_valid())
1494 (*p)->set_address(0);
1495 (*p)->set_file_offset(off);
1496 (*p)->finalize_data_size();
1497 off += (*p)->data_size();
1498
1499 (*p)->set_out_shndx(*pshndx);
1500 ++*pshndx;
1501 }
1502
1503 return off;
1504 }
1505
1506 // Set the file offset of all the sections not associated with a
1507 // segment.
1508
1509 off_t
1510 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
1511 {
1512 for (Section_list::iterator p = this->unattached_section_list_.begin();
1513 p != this->unattached_section_list_.end();
1514 ++p)
1515 {
1516 // The symtab section is handled in create_symtab_sections.
1517 if (*p == this->symtab_section_)
1518 continue;
1519
1520 // If we've already set the data size, don't set it again.
1521 if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
1522 continue;
1523
1524 if (pass == BEFORE_INPUT_SECTIONS_PASS
1525 && (*p)->requires_postprocessing())
1526 {
1527 (*p)->create_postprocessing_buffer();
1528 this->any_postprocessing_sections_ = true;
1529 }
1530
1531 if (pass == BEFORE_INPUT_SECTIONS_PASS
1532 && (*p)->after_input_sections())
1533 continue;
1534 else if (pass == POSTPROCESSING_SECTIONS_PASS
1535 && (!(*p)->after_input_sections()
1536 || (*p)->type() == elfcpp::SHT_STRTAB))
1537 continue;
1538 else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1539 && (!(*p)->after_input_sections()
1540 || (*p)->type() != elfcpp::SHT_STRTAB))
1541 continue;
1542
1543 off = align_address(off, (*p)->addralign());
1544 (*p)->set_file_offset(off);
1545 (*p)->finalize_data_size();
1546 off += (*p)->data_size();
1547
1548 // At this point the name must be set.
1549 if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
1550 this->namepool_.add((*p)->name(), false, NULL);
1551 }
1552 return off;
1553 }
1554
1555 // Set the section indexes of all the sections not associated with a
1556 // segment.
1557
1558 unsigned int
1559 Layout::set_section_indexes(unsigned int shndx)
1560 {
1561 const bool output_is_object = parameters->output_is_object();
1562 for (Section_list::iterator p = this->unattached_section_list_.begin();
1563 p != this->unattached_section_list_.end();
1564 ++p)
1565 {
1566 // In a relocatable link, we already did group sections.
1567 if (output_is_object
1568 && (*p)->type() == elfcpp::SHT_GROUP)
1569 continue;
1570
1571 (*p)->set_out_shndx(shndx);
1572 ++shndx;
1573 }
1574 return shndx;
1575 }
1576
1577 // Set the section addresses according to the linker script. This is
1578 // only called when we see a SECTIONS clause. This returns the
1579 // program segment which should hold the file header and segment
1580 // headers, if any. It will return NULL if they should not be in a
1581 // segment.
1582
1583 Output_segment*
1584 Layout::set_section_addresses_from_script(Symbol_table* symtab)
1585 {
1586 Script_sections* ss = this->script_options_->script_sections();
1587 gold_assert(ss->saw_sections_clause());
1588
1589 // Place each orphaned output section in the script.
1590 for (Section_list::iterator p = this->section_list_.begin();
1591 p != this->section_list_.end();
1592 ++p)
1593 {
1594 if (!(*p)->found_in_sections_clause())
1595 ss->place_orphan(*p);
1596 }
1597
1598 return this->script_options_->set_section_addresses(symtab, this);
1599 }
1600
1601 // Count the local symbols in the regular symbol table and the dynamic
1602 // symbol table, and build the respective string pools.
1603
1604 void
1605 Layout::count_local_symbols(const Task* task,
1606 const Input_objects* input_objects)
1607 {
1608 // First, figure out an upper bound on the number of symbols we'll
1609 // be inserting into each pool. This helps us create the pools with
1610 // the right size, to avoid unnecessary hashtable resizing.
1611 unsigned int symbol_count = 0;
1612 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1613 p != input_objects->relobj_end();
1614 ++p)
1615 symbol_count += (*p)->local_symbol_count();
1616
1617 // Go from "upper bound" to "estimate." We overcount for two
1618 // reasons: we double-count symbols that occur in more than one
1619 // object file, and we count symbols that are dropped from the
1620 // output. Add it all together and assume we overcount by 100%.
1621 symbol_count /= 2;
1622
1623 // We assume all symbols will go into both the sympool and dynpool.
1624 this->sympool_.reserve(symbol_count);
1625 this->dynpool_.reserve(symbol_count);
1626
1627 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1628 p != input_objects->relobj_end();
1629 ++p)
1630 {
1631 Task_lock_obj<Object> tlo(task, *p);
1632 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
1633 }
1634 }
1635
1636 // Create the symbol table sections. Here we also set the final
1637 // values of the symbols. At this point all the loadable sections are
1638 // fully laid out.
1639
1640 void
1641 Layout::create_symtab_sections(const Input_objects* input_objects,
1642 Symbol_table* symtab,
1643 off_t* poff)
1644 {
1645 int symsize;
1646 unsigned int align;
1647 if (parameters->get_size() == 32)
1648 {
1649 symsize = elfcpp::Elf_sizes<32>::sym_size;
1650 align = 4;
1651 }
1652 else if (parameters->get_size() == 64)
1653 {
1654 symsize = elfcpp::Elf_sizes<64>::sym_size;
1655 align = 8;
1656 }
1657 else
1658 gold_unreachable();
1659
1660 off_t off = *poff;
1661 off = align_address(off, align);
1662 off_t startoff = off;
1663
1664 // Save space for the dummy symbol at the start of the section. We
1665 // never bother to write this out--it will just be left as zero.
1666 off += symsize;
1667 unsigned int local_symbol_index = 1;
1668
1669 // Add STT_SECTION symbols for each Output section which needs one.
1670 for (Section_list::iterator p = this->section_list_.begin();
1671 p != this->section_list_.end();
1672 ++p)
1673 {
1674 if (!(*p)->needs_symtab_index())
1675 (*p)->set_symtab_index(-1U);
1676 else
1677 {
1678 (*p)->set_symtab_index(local_symbol_index);
1679 ++local_symbol_index;
1680 off += symsize;
1681 }
1682 }
1683
1684 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1685 p != input_objects->relobj_end();
1686 ++p)
1687 {
1688 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
1689 off);
1690 off += (index - local_symbol_index) * symsize;
1691 local_symbol_index = index;
1692 }
1693
1694 unsigned int local_symcount = local_symbol_index;
1695 gold_assert(local_symcount * symsize == off - startoff);
1696
1697 off_t dynoff;
1698 size_t dyn_global_index;
1699 size_t dyncount;
1700 if (this->dynsym_section_ == NULL)
1701 {
1702 dynoff = 0;
1703 dyn_global_index = 0;
1704 dyncount = 0;
1705 }
1706 else
1707 {
1708 dyn_global_index = this->dynsym_section_->info();
1709 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
1710 dynoff = this->dynsym_section_->offset() + locsize;
1711 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
1712 gold_assert(static_cast<off_t>(dyncount * symsize)
1713 == this->dynsym_section_->data_size() - locsize);
1714 }
1715
1716 off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
1717 &this->sympool_, &local_symcount);
1718
1719 if (!parameters->strip_all())
1720 {
1721 this->sympool_.set_string_offsets();
1722
1723 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
1724 Output_section* osymtab = this->make_output_section(symtab_name,
1725 elfcpp::SHT_SYMTAB,
1726 0);
1727 this->symtab_section_ = osymtab;
1728
1729 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
1730 align);
1731 osymtab->add_output_section_data(pos);
1732
1733 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
1734 Output_section* ostrtab = this->make_output_section(strtab_name,
1735 elfcpp::SHT_STRTAB,
1736 0);
1737
1738 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1739 ostrtab->add_output_section_data(pstr);
1740
1741 osymtab->set_file_offset(startoff);
1742 osymtab->finalize_data_size();
1743 osymtab->set_link_section(ostrtab);
1744 osymtab->set_info(local_symcount);
1745 osymtab->set_entsize(symsize);
1746
1747 *poff = off;
1748 }
1749 }
1750
1751 // Create the .shstrtab section, which holds the names of the
1752 // sections. At the time this is called, we have created all the
1753 // output sections except .shstrtab itself.
1754
1755 Output_section*
1756 Layout::create_shstrtab()
1757 {
1758 // FIXME: We don't need to create a .shstrtab section if we are
1759 // stripping everything.
1760
1761 const char* name = this->namepool_.add(".shstrtab", false, NULL);
1762
1763 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
1764
1765 // We can't write out this section until we've set all the section
1766 // names, and we don't set the names of compressed output sections
1767 // until relocations are complete.
1768 os->set_after_input_sections();
1769
1770 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
1771 os->add_output_section_data(posd);
1772
1773 return os;
1774 }
1775
1776 // Create the section headers. SIZE is 32 or 64. OFF is the file
1777 // offset.
1778
1779 void
1780 Layout::create_shdrs(off_t* poff)
1781 {
1782 Output_section_headers* oshdrs;
1783 oshdrs = new Output_section_headers(this,
1784 &this->segment_list_,
1785 &this->section_list_,
1786 &this->unattached_section_list_,
1787 &this->namepool_);
1788 off_t off = align_address(*poff, oshdrs->addralign());
1789 oshdrs->set_address_and_file_offset(0, off);
1790 off += oshdrs->data_size();
1791 *poff = off;
1792 this->section_headers_ = oshdrs;
1793 }
1794
1795 // Create the dynamic symbol table.
1796
1797 void
1798 Layout::create_dynamic_symtab(const Input_objects* input_objects,
1799 Symbol_table* symtab,
1800 Output_section **pdynstr,
1801 unsigned int* plocal_dynamic_count,
1802 std::vector<Symbol*>* pdynamic_symbols,
1803 Versions* pversions)
1804 {
1805 // Count all the symbols in the dynamic symbol table, and set the
1806 // dynamic symbol indexes.
1807
1808 // Skip symbol 0, which is always all zeroes.
1809 unsigned int index = 1;
1810
1811 // Add STT_SECTION symbols for each Output section which needs one.
1812 for (Section_list::iterator p = this->section_list_.begin();
1813 p != this->section_list_.end();
1814 ++p)
1815 {
1816 if (!(*p)->needs_dynsym_index())
1817 (*p)->set_dynsym_index(-1U);
1818 else
1819 {
1820 (*p)->set_dynsym_index(index);
1821 ++index;
1822 }
1823 }
1824
1825 // Count the local symbols that need to go in the dynamic symbol table,
1826 // and set the dynamic symbol indexes.
1827 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1828 p != input_objects->relobj_end();
1829 ++p)
1830 {
1831 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
1832 index = new_index;
1833 }
1834
1835 unsigned int local_symcount = index;
1836 *plocal_dynamic_count = local_symcount;
1837
1838 // FIXME: We have to tell set_dynsym_indexes whether the
1839 // -E/--export-dynamic option was used.
1840 index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
1841 &this->dynpool_, pversions);
1842
1843 int symsize;
1844 unsigned int align;
1845 const int size = parameters->get_size();
1846 if (size == 32)
1847 {
1848 symsize = elfcpp::Elf_sizes<32>::sym_size;
1849 align = 4;
1850 }
1851 else if (size == 64)
1852 {
1853 symsize = elfcpp::Elf_sizes<64>::sym_size;
1854 align = 8;
1855 }
1856 else
1857 gold_unreachable();
1858
1859 // Create the dynamic symbol table section.
1860
1861 Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
1862 elfcpp::SHT_DYNSYM,
1863 elfcpp::SHF_ALLOC,
1864 false);
1865
1866 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
1867 align);
1868 dynsym->add_output_section_data(odata);
1869
1870 dynsym->set_info(local_symcount);
1871 dynsym->set_entsize(symsize);
1872 dynsym->set_addralign(align);
1873
1874 this->dynsym_section_ = dynsym;
1875
1876 Output_data_dynamic* const odyn = this->dynamic_data_;
1877 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1878 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1879
1880 // Create the dynamic string table section.
1881
1882 Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
1883 elfcpp::SHT_STRTAB,
1884 elfcpp::SHF_ALLOC,
1885 false);
1886
1887 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1888 dynstr->add_output_section_data(strdata);
1889
1890 dynsym->set_link_section(dynstr);
1891 this->dynamic_section_->set_link_section(dynstr);
1892
1893 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1894 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1895
1896 *pdynstr = dynstr;
1897
1898 // Create the hash tables.
1899
1900 // FIXME: We need an option to create a GNU hash table.
1901
1902 unsigned char* phash;
1903 unsigned int hashlen;
1904 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
1905 &phash, &hashlen);
1906
1907 Output_section* hashsec = this->choose_output_section(NULL, ".hash",
1908 elfcpp::SHT_HASH,
1909 elfcpp::SHF_ALLOC,
1910 false);
1911
1912 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1913 hashlen,
1914 align);
1915 hashsec->add_output_section_data(hashdata);
1916
1917 hashsec->set_link_section(dynsym);
1918 hashsec->set_entsize(4);
1919
1920 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1921 }
1922
1923 // Assign offsets to each local portion of the dynamic symbol table.
1924
1925 void
1926 Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
1927 {
1928 Output_section* dynsym = this->dynsym_section_;
1929 gold_assert(dynsym != NULL);
1930
1931 off_t off = dynsym->offset();
1932
1933 // Skip the dummy symbol at the start of the section.
1934 off += dynsym->entsize();
1935
1936 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1937 p != input_objects->relobj_end();
1938 ++p)
1939 {
1940 unsigned int count = (*p)->set_local_dynsym_offset(off);
1941 off += count * dynsym->entsize();
1942 }
1943 }
1944
1945 // Create the version sections.
1946
1947 void
1948 Layout::create_version_sections(const Versions* versions,
1949 const Symbol_table* symtab,
1950 unsigned int local_symcount,
1951 const std::vector<Symbol*>& dynamic_symbols,
1952 const Output_section* dynstr)
1953 {
1954 if (!versions->any_defs() && !versions->any_needs())
1955 return;
1956
1957 if (parameters->get_size() == 32)
1958 {
1959 if (parameters->is_big_endian())
1960 {
1961 #ifdef HAVE_TARGET_32_BIG
1962 this->sized_create_version_sections
1963 SELECT_SIZE_ENDIAN_NAME(32, true)(
1964 versions, symtab, local_symcount, dynamic_symbols, dynstr
1965 SELECT_SIZE_ENDIAN(32, true));
1966 #else
1967 gold_unreachable();
1968 #endif
1969 }
1970 else
1971 {
1972 #ifdef HAVE_TARGET_32_LITTLE
1973 this->sized_create_version_sections
1974 SELECT_SIZE_ENDIAN_NAME(32, false)(
1975 versions, symtab, local_symcount, dynamic_symbols, dynstr
1976 SELECT_SIZE_ENDIAN(32, false));
1977 #else
1978 gold_unreachable();
1979 #endif
1980 }
1981 }
1982 else if (parameters->get_size() == 64)
1983 {
1984 if (parameters->is_big_endian())
1985 {
1986 #ifdef HAVE_TARGET_64_BIG
1987 this->sized_create_version_sections
1988 SELECT_SIZE_ENDIAN_NAME(64, true)(
1989 versions, symtab, local_symcount, dynamic_symbols, dynstr
1990 SELECT_SIZE_ENDIAN(64, true));
1991 #else
1992 gold_unreachable();
1993 #endif
1994 }
1995 else
1996 {
1997 #ifdef HAVE_TARGET_64_LITTLE
1998 this->sized_create_version_sections
1999 SELECT_SIZE_ENDIAN_NAME(64, false)(
2000 versions, symtab, local_symcount, dynamic_symbols, dynstr
2001 SELECT_SIZE_ENDIAN(64, false));
2002 #else
2003 gold_unreachable();
2004 #endif
2005 }
2006 }
2007 else
2008 gold_unreachable();
2009 }
2010
2011 // Create the version sections, sized version.
2012
2013 template<int size, bool big_endian>
2014 void
2015 Layout::sized_create_version_sections(
2016 const Versions* versions,
2017 const Symbol_table* symtab,
2018 unsigned int local_symcount,
2019 const std::vector<Symbol*>& dynamic_symbols,
2020 const Output_section* dynstr
2021 ACCEPT_SIZE_ENDIAN)
2022 {
2023 Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
2024 elfcpp::SHT_GNU_versym,
2025 elfcpp::SHF_ALLOC,
2026 false);
2027
2028 unsigned char* vbuf;
2029 unsigned int vsize;
2030 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
2031 symtab, &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
2032 SELECT_SIZE_ENDIAN(size, big_endian));
2033
2034 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
2035
2036 vsec->add_output_section_data(vdata);
2037 vsec->set_entsize(2);
2038 vsec->set_link_section(this->dynsym_section_);
2039
2040 Output_data_dynamic* const odyn = this->dynamic_data_;
2041 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
2042
2043 if (versions->any_defs())
2044 {
2045 Output_section* vdsec;
2046 vdsec= this->choose_output_section(NULL, ".gnu.version_d",
2047 elfcpp::SHT_GNU_verdef,
2048 elfcpp::SHF_ALLOC,
2049 false);
2050
2051 unsigned char* vdbuf;
2052 unsigned int vdsize;
2053 unsigned int vdentries;
2054 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
2055 &this->dynpool_, &vdbuf, &vdsize, &vdentries
2056 SELECT_SIZE_ENDIAN(size, big_endian));
2057
2058 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
2059 vdsize,
2060 4);
2061
2062 vdsec->add_output_section_data(vddata);
2063 vdsec->set_link_section(dynstr);
2064 vdsec->set_info(vdentries);
2065
2066 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
2067 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
2068 }
2069
2070 if (versions->any_needs())
2071 {
2072 Output_section* vnsec;
2073 vnsec = this->choose_output_section(NULL, ".gnu.version_r",
2074 elfcpp::SHT_GNU_verneed,
2075 elfcpp::SHF_ALLOC,
2076 false);
2077
2078 unsigned char* vnbuf;
2079 unsigned int vnsize;
2080 unsigned int vnentries;
2081 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
2082 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
2083 SELECT_SIZE_ENDIAN(size, big_endian));
2084
2085 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
2086 vnsize,
2087 4);
2088
2089 vnsec->add_output_section_data(vndata);
2090 vnsec->set_link_section(dynstr);
2091 vnsec->set_info(vnentries);
2092
2093 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
2094 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
2095 }
2096 }
2097
2098 // Create the .interp section and PT_INTERP segment.
2099
2100 void
2101 Layout::create_interp(const Target* target)
2102 {
2103 const char* interp = this->options_.dynamic_linker();
2104 if (interp == NULL)
2105 {
2106 interp = target->dynamic_linker();
2107 gold_assert(interp != NULL);
2108 }
2109
2110 size_t len = strlen(interp) + 1;
2111
2112 Output_section_data* odata = new Output_data_const(interp, len, 1);
2113
2114 Output_section* osec = this->choose_output_section(NULL, ".interp",
2115 elfcpp::SHT_PROGBITS,
2116 elfcpp::SHF_ALLOC,
2117 false);
2118 osec->add_output_section_data(odata);
2119
2120 if (!this->script_options_->saw_phdrs_clause())
2121 {
2122 Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP,
2123 elfcpp::PF_R);
2124 oseg->add_initial_output_section(osec, elfcpp::PF_R);
2125 }
2126 }
2127
2128 // Finish the .dynamic section and PT_DYNAMIC segment.
2129
2130 void
2131 Layout::finish_dynamic_section(const Input_objects* input_objects,
2132 const Symbol_table* symtab)
2133 {
2134 if (!this->script_options_->saw_phdrs_clause())
2135 {
2136 Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
2137 (elfcpp::PF_R
2138 | elfcpp::PF_W));
2139 oseg->add_initial_output_section(this->dynamic_section_,
2140 elfcpp::PF_R | elfcpp::PF_W);
2141 }
2142
2143 Output_data_dynamic* const odyn = this->dynamic_data_;
2144
2145 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
2146 p != input_objects->dynobj_end();
2147 ++p)
2148 {
2149 // FIXME: Handle --as-needed.
2150 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
2151 }
2152
2153 if (parameters->output_is_shared())
2154 {
2155 const char* soname = this->options_.soname();
2156 if (soname != NULL)
2157 odyn->add_string(elfcpp::DT_SONAME, soname);
2158 }
2159
2160 // FIXME: Support --init and --fini.
2161 Symbol* sym = symtab->lookup("_init");
2162 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2163 odyn->add_symbol(elfcpp::DT_INIT, sym);
2164
2165 sym = symtab->lookup("_fini");
2166 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2167 odyn->add_symbol(elfcpp::DT_FINI, sym);
2168
2169 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2170
2171 // Add a DT_RPATH entry if needed.
2172 const General_options::Dir_list& rpath(this->options_.rpath());
2173 if (!rpath.empty())
2174 {
2175 std::string rpath_val;
2176 for (General_options::Dir_list::const_iterator p = rpath.begin();
2177 p != rpath.end();
2178 ++p)
2179 {
2180 if (rpath_val.empty())
2181 rpath_val = p->name();
2182 else
2183 {
2184 // Eliminate duplicates.
2185 General_options::Dir_list::const_iterator q;
2186 for (q = rpath.begin(); q != p; ++q)
2187 if (q->name() == p->name())
2188 break;
2189 if (q == p)
2190 {
2191 rpath_val += ':';
2192 rpath_val += p->name();
2193 }
2194 }
2195 }
2196
2197 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
2198 }
2199
2200 // Look for text segments that have dynamic relocations.
2201 bool have_textrel = false;
2202 if (!this->script_options_->saw_sections_clause())
2203 {
2204 for (Segment_list::const_iterator p = this->segment_list_.begin();
2205 p != this->segment_list_.end();
2206 ++p)
2207 {
2208 if (((*p)->flags() & elfcpp::PF_W) == 0
2209 && (*p)->dynamic_reloc_count() > 0)
2210 {
2211 have_textrel = true;
2212 break;
2213 }
2214 }
2215 }
2216 else
2217 {
2218 // We don't know the section -> segment mapping, so we are
2219 // conservative and just look for readonly sections with
2220 // relocations. If those sections wind up in writable segments,
2221 // then we have created an unnecessary DT_TEXTREL entry.
2222 for (Section_list::const_iterator p = this->section_list_.begin();
2223 p != this->section_list_.end();
2224 ++p)
2225 {
2226 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
2227 && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
2228 && ((*p)->dynamic_reloc_count() > 0))
2229 {
2230 have_textrel = true;
2231 break;
2232 }
2233 }
2234 }
2235
2236 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2237 // post-link tools can easily modify these flags if desired.
2238 unsigned int flags = 0;
2239 if (have_textrel)
2240 {
2241 // Add a DT_TEXTREL for compatibility with older loaders.
2242 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
2243 flags |= elfcpp::DF_TEXTREL;
2244 }
2245 if (parameters->output_is_shared() && this->has_static_tls())
2246 flags |= elfcpp::DF_STATIC_TLS;
2247 odyn->add_constant(elfcpp::DT_FLAGS, flags);
2248 }
2249
2250 // The mapping of .gnu.linkonce section names to real section names.
2251
2252 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2253 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
2254 {
2255 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
2256 MAPPING_INIT("t", ".text"),
2257 MAPPING_INIT("r", ".rodata"),
2258 MAPPING_INIT("d", ".data"),
2259 MAPPING_INIT("b", ".bss"),
2260 MAPPING_INIT("s", ".sdata"),
2261 MAPPING_INIT("sb", ".sbss"),
2262 MAPPING_INIT("s2", ".sdata2"),
2263 MAPPING_INIT("sb2", ".sbss2"),
2264 MAPPING_INIT("wi", ".debug_info"),
2265 MAPPING_INIT("td", ".tdata"),
2266 MAPPING_INIT("tb", ".tbss"),
2267 MAPPING_INIT("lr", ".lrodata"),
2268 MAPPING_INIT("l", ".ldata"),
2269 MAPPING_INIT("lb", ".lbss"),
2270 };
2271 #undef MAPPING_INIT
2272
2273 const int Layout::linkonce_mapping_count =
2274 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
2275
2276 // Return the name of the output section to use for a .gnu.linkonce
2277 // section. This is based on the default ELF linker script of the old
2278 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2279 // to ".text". Set *PLEN to the length of the name. *PLEN is
2280 // initialized to the length of NAME.
2281
2282 const char*
2283 Layout::linkonce_output_name(const char* name, size_t *plen)
2284 {
2285 const char* s = name + sizeof(".gnu.linkonce") - 1;
2286 if (*s != '.')
2287 return name;
2288 ++s;
2289 const Linkonce_mapping* plm = linkonce_mapping;
2290 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
2291 {
2292 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
2293 {
2294 *plen = plm->tolen;
2295 return plm->to;
2296 }
2297 }
2298 return name;
2299 }
2300
2301 // Choose the output section name to use given an input section name.
2302 // Set *PLEN to the length of the name. *PLEN is initialized to the
2303 // length of NAME.
2304
2305 const char*
2306 Layout::output_section_name(const char* name, size_t* plen)
2307 {
2308 if (Layout::is_linkonce(name))
2309 {
2310 // .gnu.linkonce sections are laid out as though they were named
2311 // for the sections are placed into.
2312 return Layout::linkonce_output_name(name, plen);
2313 }
2314
2315 // gcc 4.3 generates the following sorts of section names when it
2316 // needs a section name specific to a function:
2317 // .text.FN
2318 // .rodata.FN
2319 // .sdata2.FN
2320 // .data.FN
2321 // .data.rel.FN
2322 // .data.rel.local.FN
2323 // .data.rel.ro.FN
2324 // .data.rel.ro.local.FN
2325 // .sdata.FN
2326 // .bss.FN
2327 // .sbss.FN
2328 // .tdata.FN
2329 // .tbss.FN
2330
2331 // The GNU linker maps all of those to the part before the .FN,
2332 // except that .data.rel.local.FN is mapped to .data, and
2333 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2334 // beginning with .data.rel.ro.local are grouped together.
2335
2336 // For an anonymous namespace, the string FN can contain a '.'.
2337
2338 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2339 // GNU linker maps to .rodata.
2340
2341 // The .data.rel.ro sections enable a security feature triggered by
2342 // the -z relro option. Section which need to be relocated at
2343 // program startup time but which may be readonly after startup are
2344 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2345 // segment. The dynamic linker will make that segment writable,
2346 // perform relocations, and then make it read-only. FIXME: We do
2347 // not yet implement this optimization.
2348
2349 // It is hard to handle this in a principled way.
2350
2351 // These are the rules we follow:
2352
2353 // If the section name has no initial '.', or no dot other than an
2354 // initial '.', we use the name unchanged (i.e., "mysection" and
2355 // ".text" are unchanged).
2356
2357 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2358
2359 // Otherwise, we drop the second '.' and everything that comes after
2360 // it (i.e., ".text.XXX" becomes ".text").
2361
2362 const char* s = name;
2363 if (*s != '.')
2364 return name;
2365 ++s;
2366 const char* sdot = strchr(s, '.');
2367 if (sdot == NULL)
2368 return name;
2369
2370 const char* const data_rel_ro = ".data.rel.ro";
2371 if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
2372 {
2373 *plen = strlen(data_rel_ro);
2374 return data_rel_ro;
2375 }
2376
2377 *plen = sdot - name;
2378 return name;
2379 }
2380
2381 // Record the signature of a comdat section, and return whether to
2382 // include it in the link. If GROUP is true, this is a regular
2383 // section group. If GROUP is false, this is a group signature
2384 // derived from the name of a linkonce section. We want linkonce
2385 // signatures and group signatures to block each other, but we don't
2386 // want a linkonce signature to block another linkonce signature.
2387
2388 bool
2389 Layout::add_comdat(const char* signature, bool group)
2390 {
2391 std::string sig(signature);
2392 std::pair<Signatures::iterator, bool> ins(
2393 this->signatures_.insert(std::make_pair(sig, group)));
2394
2395 if (ins.second)
2396 {
2397 // This is the first time we've seen this signature.
2398 return true;
2399 }
2400
2401 if (ins.first->second)
2402 {
2403 // We've already seen a real section group with this signature.
2404 return false;
2405 }
2406 else if (group)
2407 {
2408 // This is a real section group, and we've already seen a
2409 // linkonce section with this signature. Record that we've seen
2410 // a section group, and don't include this section group.
2411 ins.first->second = true;
2412 return false;
2413 }
2414 else
2415 {
2416 // We've already seen a linkonce section and this is a linkonce
2417 // section. These don't block each other--this may be the same
2418 // symbol name with different section types.
2419 return true;
2420 }
2421 }
2422
2423 // Store the allocated sections into the section list.
2424
2425 void
2426 Layout::get_allocated_sections(Section_list* section_list) const
2427 {
2428 for (Section_list::const_iterator p = this->section_list_.begin();
2429 p != this->section_list_.end();
2430 ++p)
2431 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
2432 section_list->push_back(*p);
2433 }
2434
2435 // Create an output segment.
2436
2437 Output_segment*
2438 Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2439 {
2440 gold_assert(!parameters->output_is_object());
2441 Output_segment* oseg = new Output_segment(type, flags);
2442 this->segment_list_.push_back(oseg);
2443 return oseg;
2444 }
2445
2446 // Write out the Output_sections. Most won't have anything to write,
2447 // since most of the data will come from input sections which are
2448 // handled elsewhere. But some Output_sections do have Output_data.
2449
2450 void
2451 Layout::write_output_sections(Output_file* of) const
2452 {
2453 for (Section_list::const_iterator p = this->section_list_.begin();
2454 p != this->section_list_.end();
2455 ++p)
2456 {
2457 if (!(*p)->after_input_sections())
2458 (*p)->write(of);
2459 }
2460 }
2461
2462 // Write out data not associated with a section or the symbol table.
2463
2464 void
2465 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
2466 {
2467 if (!parameters->strip_all())
2468 {
2469 const Output_section* symtab_section = this->symtab_section_;
2470 for (Section_list::const_iterator p = this->section_list_.begin();
2471 p != this->section_list_.end();
2472 ++p)
2473 {
2474 if ((*p)->needs_symtab_index())
2475 {
2476 gold_assert(symtab_section != NULL);
2477 unsigned int index = (*p)->symtab_index();
2478 gold_assert(index > 0 && index != -1U);
2479 off_t off = (symtab_section->offset()
2480 + index * symtab_section->entsize());
2481 symtab->write_section_symbol(*p, of, off);
2482 }
2483 }
2484 }
2485
2486 const Output_section* dynsym_section = this->dynsym_section_;
2487 for (Section_list::const_iterator p = this->section_list_.begin();
2488 p != this->section_list_.end();
2489 ++p)
2490 {
2491 if ((*p)->needs_dynsym_index())
2492 {
2493 gold_assert(dynsym_section != NULL);
2494 unsigned int index = (*p)->dynsym_index();
2495 gold_assert(index > 0 && index != -1U);
2496 off_t off = (dynsym_section->offset()
2497 + index * dynsym_section->entsize());
2498 symtab->write_section_symbol(*p, of, off);
2499 }
2500 }
2501
2502 // Write out the Output_data which are not in an Output_section.
2503 for (Data_list::const_iterator p = this->special_output_list_.begin();
2504 p != this->special_output_list_.end();
2505 ++p)
2506 (*p)->write(of);
2507 }
2508
2509 // Write out the Output_sections which can only be written after the
2510 // input sections are complete.
2511
2512 void
2513 Layout::write_sections_after_input_sections(Output_file* of)
2514 {
2515 // Determine the final section offsets, and thus the final output
2516 // file size. Note we finalize the .shstrab last, to allow the
2517 // after_input_section sections to modify their section-names before
2518 // writing.
2519 if (this->any_postprocessing_sections_)
2520 {
2521 off_t off = this->output_file_size_;
2522 off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
2523
2524 // Now that we've finalized the names, we can finalize the shstrab.
2525 off =
2526 this->set_section_offsets(off,
2527 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
2528
2529 if (off > this->output_file_size_)
2530 {
2531 of->resize(off);
2532 this->output_file_size_ = off;
2533 }
2534 }
2535
2536 for (Section_list::const_iterator p = this->section_list_.begin();
2537 p != this->section_list_.end();
2538 ++p)
2539 {
2540 if ((*p)->after_input_sections())
2541 (*p)->write(of);
2542 }
2543
2544 this->section_headers_->write(of);
2545 }
2546
2547 // Write out a binary file. This is called after the link is
2548 // complete. IN is the temporary output file we used to generate the
2549 // ELF code. We simply walk through the segments, read them from
2550 // their file offset in IN, and write them to their load address in
2551 // the output file. FIXME: with a bit more work, we could support
2552 // S-records and/or Intel hex format here.
2553
2554 void
2555 Layout::write_binary(Output_file* in) const
2556 {
2557 gold_assert(this->options_.oformat()
2558 == General_options::OBJECT_FORMAT_BINARY);
2559
2560 // Get the size of the binary file.
2561 uint64_t max_load_address = 0;
2562 for (Segment_list::const_iterator p = this->segment_list_.begin();
2563 p != this->segment_list_.end();
2564 ++p)
2565 {
2566 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
2567 {
2568 uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
2569 if (max_paddr > max_load_address)
2570 max_load_address = max_paddr;
2571 }
2572 }
2573
2574 Output_file out(parameters->output_file_name());
2575 out.open(max_load_address);
2576
2577 for (Segment_list::const_iterator p = this->segment_list_.begin();
2578 p != this->segment_list_.end();
2579 ++p)
2580 {
2581 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
2582 {
2583 const unsigned char* vin = in->get_input_view((*p)->offset(),
2584 (*p)->filesz());
2585 unsigned char* vout = out.get_output_view((*p)->paddr(),
2586 (*p)->filesz());
2587 memcpy(vout, vin, (*p)->filesz());
2588 out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
2589 in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
2590 }
2591 }
2592
2593 out.close();
2594 }
2595
2596 // Print statistical information to stderr. This is used for --stats.
2597
2598 void
2599 Layout::print_stats() const
2600 {
2601 this->namepool_.print_stats("section name pool");
2602 this->sympool_.print_stats("output symbol name pool");
2603 this->dynpool_.print_stats("dynamic name pool");
2604
2605 for (Section_list::const_iterator p = this->section_list_.begin();
2606 p != this->section_list_.end();
2607 ++p)
2608 (*p)->print_merge_stats();
2609 }
2610
2611 // Write_sections_task methods.
2612
2613 // We can always run this task.
2614
2615 Task_token*
2616 Write_sections_task::is_runnable()
2617 {
2618 return NULL;
2619 }
2620
2621 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
2622 // when finished.
2623
2624 void
2625 Write_sections_task::locks(Task_locker* tl)
2626 {
2627 tl->add(this, this->output_sections_blocker_);
2628 tl->add(this, this->final_blocker_);
2629 }
2630
2631 // Run the task--write out the data.
2632
2633 void
2634 Write_sections_task::run(Workqueue*)
2635 {
2636 this->layout_->write_output_sections(this->of_);
2637 }
2638
2639 // Write_data_task methods.
2640
2641 // We can always run this task.
2642
2643 Task_token*
2644 Write_data_task::is_runnable()
2645 {
2646 return NULL;
2647 }
2648
2649 // We need to unlock FINAL_BLOCKER when finished.
2650
2651 void
2652 Write_data_task::locks(Task_locker* tl)
2653 {
2654 tl->add(this, this->final_blocker_);
2655 }
2656
2657 // Run the task--write out the data.
2658
2659 void
2660 Write_data_task::run(Workqueue*)
2661 {
2662 this->layout_->write_data(this->symtab_, this->of_);
2663 }
2664
2665 // Write_symbols_task methods.
2666
2667 // We can always run this task.
2668
2669 Task_token*
2670 Write_symbols_task::is_runnable()
2671 {
2672 return NULL;
2673 }
2674
2675 // We need to unlock FINAL_BLOCKER when finished.
2676
2677 void
2678 Write_symbols_task::locks(Task_locker* tl)
2679 {
2680 tl->add(this, this->final_blocker_);
2681 }
2682
2683 // Run the task--write out the symbols.
2684
2685 void
2686 Write_symbols_task::run(Workqueue*)
2687 {
2688 this->symtab_->write_globals(this->input_objects_, this->sympool_,
2689 this->dynpool_, this->of_);
2690 }
2691
2692 // Write_after_input_sections_task methods.
2693
2694 // We can only run this task after the input sections have completed.
2695
2696 Task_token*
2697 Write_after_input_sections_task::is_runnable()
2698 {
2699 if (this->input_sections_blocker_->is_blocked())
2700 return this->input_sections_blocker_;
2701 return NULL;
2702 }
2703
2704 // We need to unlock FINAL_BLOCKER when finished.
2705
2706 void
2707 Write_after_input_sections_task::locks(Task_locker* tl)
2708 {
2709 tl->add(this, this->final_blocker_);
2710 }
2711
2712 // Run the task.
2713
2714 void
2715 Write_after_input_sections_task::run(Workqueue*)
2716 {
2717 this->layout_->write_sections_after_input_sections(this->of_);
2718 }
2719
2720 // Close_task_runner methods.
2721
2722 // Run the task--close the file.
2723
2724 void
2725 Close_task_runner::run(Workqueue*, const Task*)
2726 {
2727 // If we've been asked to create a binary file, we do so here.
2728 if (this->options_->oformat() != General_options::OBJECT_FORMAT_ELF)
2729 this->layout_->write_binary(this->of_);
2730
2731 this->of_->close();
2732 }
2733
2734 // Instantiate the templates we need. We could use the configure
2735 // script to restrict this to only the ones for implemented targets.
2736
2737 #ifdef HAVE_TARGET_32_LITTLE
2738 template
2739 Output_section*
2740 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
2741 const char* name,
2742 const elfcpp::Shdr<32, false>& shdr,
2743 unsigned int, unsigned int, off_t*);
2744 #endif
2745
2746 #ifdef HAVE_TARGET_32_BIG
2747 template
2748 Output_section*
2749 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
2750 const char* name,
2751 const elfcpp::Shdr<32, true>& shdr,
2752 unsigned int, unsigned int, off_t*);
2753 #endif
2754
2755 #ifdef HAVE_TARGET_64_LITTLE
2756 template
2757 Output_section*
2758 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
2759 const char* name,
2760 const elfcpp::Shdr<64, false>& shdr,
2761 unsigned int, unsigned int, off_t*);
2762 #endif
2763
2764 #ifdef HAVE_TARGET_64_BIG
2765 template
2766 Output_section*
2767 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
2768 const char* name,
2769 const elfcpp::Shdr<64, true>& shdr,
2770 unsigned int, unsigned int, off_t*);
2771 #endif
2772
2773 #ifdef HAVE_TARGET_32_LITTLE
2774 template
2775 Output_section*
2776 Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object,
2777 unsigned int reloc_shndx,
2778 const elfcpp::Shdr<32, false>& shdr,
2779 Output_section* data_section,
2780 Relocatable_relocs* rr);
2781 #endif
2782
2783 #ifdef HAVE_TARGET_32_BIG
2784 template
2785 Output_section*
2786 Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object,
2787 unsigned int reloc_shndx,
2788 const elfcpp::Shdr<32, true>& shdr,
2789 Output_section* data_section,
2790 Relocatable_relocs* rr);
2791 #endif
2792
2793 #ifdef HAVE_TARGET_64_LITTLE
2794 template
2795 Output_section*
2796 Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object,
2797 unsigned int reloc_shndx,
2798 const elfcpp::Shdr<64, false>& shdr,
2799 Output_section* data_section,
2800 Relocatable_relocs* rr);
2801 #endif
2802
2803 #ifdef HAVE_TARGET_64_BIG
2804 template
2805 Output_section*
2806 Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object,
2807 unsigned int reloc_shndx,
2808 const elfcpp::Shdr<64, true>& shdr,
2809 Output_section* data_section,
2810 Relocatable_relocs* rr);
2811 #endif
2812
2813 #ifdef HAVE_TARGET_32_LITTLE
2814 template
2815 void
2816 Layout::layout_group<32, false>(Symbol_table* symtab,
2817 Sized_relobj<32, false>* object,
2818 unsigned int,
2819 const char* group_section_name,
2820 const char* signature,
2821 const elfcpp::Shdr<32, false>& shdr,
2822 const elfcpp::Elf_Word* contents);
2823 #endif
2824
2825 #ifdef HAVE_TARGET_32_BIG
2826 template
2827 void
2828 Layout::layout_group<32, true>(Symbol_table* symtab,
2829 Sized_relobj<32, true>* object,
2830 unsigned int,
2831 const char* group_section_name,
2832 const char* signature,
2833 const elfcpp::Shdr<32, true>& shdr,
2834 const elfcpp::Elf_Word* contents);
2835 #endif
2836
2837 #ifdef HAVE_TARGET_64_LITTLE
2838 template
2839 void
2840 Layout::layout_group<64, false>(Symbol_table* symtab,
2841 Sized_relobj<64, false>* object,
2842 unsigned int,
2843 const char* group_section_name,
2844 const char* signature,
2845 const elfcpp::Shdr<64, false>& shdr,
2846 const elfcpp::Elf_Word* contents);
2847 #endif
2848
2849 #ifdef HAVE_TARGET_64_BIG
2850 template
2851 void
2852 Layout::layout_group<64, true>(Symbol_table* symtab,
2853 Sized_relobj<64, true>* object,
2854 unsigned int,
2855 const char* group_section_name,
2856 const char* signature,
2857 const elfcpp::Shdr<64, true>& shdr,
2858 const elfcpp::Elf_Word* contents);
2859 #endif
2860
2861 #ifdef HAVE_TARGET_32_LITTLE
2862 template
2863 Output_section*
2864 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
2865 const unsigned char* symbols,
2866 off_t symbols_size,
2867 const unsigned char* symbol_names,
2868 off_t symbol_names_size,
2869 unsigned int shndx,
2870 const elfcpp::Shdr<32, false>& shdr,
2871 unsigned int reloc_shndx,
2872 unsigned int reloc_type,
2873 off_t* off);
2874 #endif
2875
2876 #ifdef HAVE_TARGET_32_BIG
2877 template
2878 Output_section*
2879 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
2880 const unsigned char* symbols,
2881 off_t symbols_size,
2882 const unsigned char* symbol_names,
2883 off_t symbol_names_size,
2884 unsigned int shndx,
2885 const elfcpp::Shdr<32, true>& shdr,
2886 unsigned int reloc_shndx,
2887 unsigned int reloc_type,
2888 off_t* off);
2889 #endif
2890
2891 #ifdef HAVE_TARGET_64_LITTLE
2892 template
2893 Output_section*
2894 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
2895 const unsigned char* symbols,
2896 off_t symbols_size,
2897 const unsigned char* symbol_names,
2898 off_t symbol_names_size,
2899 unsigned int shndx,
2900 const elfcpp::Shdr<64, false>& shdr,
2901 unsigned int reloc_shndx,
2902 unsigned int reloc_type,
2903 off_t* off);
2904 #endif
2905
2906 #ifdef HAVE_TARGET_64_BIG
2907 template
2908 Output_section*
2909 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
2910 const unsigned char* symbols,
2911 off_t symbols_size,
2912 const unsigned char* symbol_names,
2913 off_t symbol_names_size,
2914 unsigned int shndx,
2915 const elfcpp::Shdr<64, true>& shdr,
2916 unsigned int reloc_shndx,
2917 unsigned int reloc_type,
2918 off_t* off);
2919 #endif
2920
2921 } // End namespace gold.
This page took 0.089915 seconds and 4 git commands to generate.