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