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