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