2008-09-29 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / gold / output.cc
CommitLineData
a2fb1b05
ILT
1// output.cc -- manage the output file for gold
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
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
25#include <cstdlib>
04bf7072 26#include <cstring>
61ba1cf9
ILT
27#include <cerrno>
28#include <fcntl.h>
29#include <unistd.h>
30#include <sys/mman.h>
4e9d8586 31#include <sys/stat.h>
75f65a3e 32#include <algorithm>
5ffcaa86 33#include "libiberty.h" // for unlink_if_ordinary()
a2fb1b05 34
7e1edb90 35#include "parameters.h"
a2fb1b05 36#include "object.h"
ead1e424
ILT
37#include "symtab.h"
38#include "reloc.h"
b8e6aad9 39#include "merge.h"
2a00e4fb 40#include "descriptors.h"
a2fb1b05
ILT
41#include "output.h"
42
c420411f
ILT
43// Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
44#ifndef MAP_ANONYMOUS
45# define MAP_ANONYMOUS MAP_ANON
46#endif
47
a2fb1b05
ILT
48namespace gold
49{
50
a3ad94ed
ILT
51// Output_data variables.
52
27bc2bce 53bool Output_data::allocated_sizes_are_fixed;
a3ad94ed 54
a2fb1b05
ILT
55// Output_data methods.
56
57Output_data::~Output_data()
58{
59}
60
730cdc88
ILT
61// Return the default alignment for the target size.
62
63uint64_t
64Output_data::default_alignment()
65{
8851ecca
ILT
66 return Output_data::default_alignment_for_size(
67 parameters->target().get_size());
730cdc88
ILT
68}
69
75f65a3e
ILT
70// Return the default alignment for a size--32 or 64.
71
72uint64_t
730cdc88 73Output_data::default_alignment_for_size(int size)
75f65a3e
ILT
74{
75 if (size == 32)
76 return 4;
77 else if (size == 64)
78 return 8;
79 else
a3ad94ed 80 gold_unreachable();
75f65a3e
ILT
81}
82
75f65a3e
ILT
83// Output_section_header methods. This currently assumes that the
84// segment and section lists are complete at construction time.
85
86Output_section_headers::Output_section_headers(
16649710
ILT
87 const Layout* layout,
88 const Layout::Segment_list* segment_list,
6a74a719 89 const Layout::Section_list* section_list,
16649710 90 const Layout::Section_list* unattached_section_list,
d491d34e
ILT
91 const Stringpool* secnamepool,
92 const Output_section* shstrtab_section)
9025d29d 93 : layout_(layout),
75f65a3e 94 segment_list_(segment_list),
6a74a719 95 section_list_(section_list),
a3ad94ed 96 unattached_section_list_(unattached_section_list),
d491d34e
ILT
97 secnamepool_(secnamepool),
98 shstrtab_section_(shstrtab_section)
75f65a3e 99{
61ba1cf9
ILT
100 // Count all the sections. Start with 1 for the null section.
101 off_t count = 1;
8851ecca 102 if (!parameters->options().relocatable())
6a74a719
ILT
103 {
104 for (Layout::Segment_list::const_iterator p = segment_list->begin();
105 p != segment_list->end();
106 ++p)
107 if ((*p)->type() == elfcpp::PT_LOAD)
108 count += (*p)->output_section_count();
109 }
110 else
111 {
112 for (Layout::Section_list::const_iterator p = section_list->begin();
113 p != section_list->end();
114 ++p)
115 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
116 ++count;
117 }
16649710 118 count += unattached_section_list->size();
75f65a3e 119
8851ecca 120 const int size = parameters->target().get_size();
75f65a3e
ILT
121 int shdr_size;
122 if (size == 32)
123 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
124 else if (size == 64)
125 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
126 else
a3ad94ed 127 gold_unreachable();
75f65a3e
ILT
128
129 this->set_data_size(count * shdr_size);
130}
131
61ba1cf9
ILT
132// Write out the section headers.
133
75f65a3e 134void
61ba1cf9 135Output_section_headers::do_write(Output_file* of)
a2fb1b05 136{
8851ecca 137 switch (parameters->size_and_endianness())
61ba1cf9 138 {
9025d29d 139#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
140 case Parameters::TARGET_32_LITTLE:
141 this->do_sized_write<32, false>(of);
142 break;
9025d29d 143#endif
8851ecca
ILT
144#ifdef HAVE_TARGET_32_BIG
145 case Parameters::TARGET_32_BIG:
146 this->do_sized_write<32, true>(of);
147 break;
9025d29d 148#endif
9025d29d 149#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
150 case Parameters::TARGET_64_LITTLE:
151 this->do_sized_write<64, false>(of);
152 break;
9025d29d 153#endif
8851ecca
ILT
154#ifdef HAVE_TARGET_64_BIG
155 case Parameters::TARGET_64_BIG:
156 this->do_sized_write<64, true>(of);
157 break;
158#endif
159 default:
160 gold_unreachable();
61ba1cf9 161 }
61ba1cf9
ILT
162}
163
164template<int size, bool big_endian>
165void
166Output_section_headers::do_sized_write(Output_file* of)
167{
168 off_t all_shdrs_size = this->data_size();
169 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
170
171 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
172 unsigned char* v = view;
173
174 {
175 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
176 oshdr.put_sh_name(0);
177 oshdr.put_sh_type(elfcpp::SHT_NULL);
178 oshdr.put_sh_flags(0);
179 oshdr.put_sh_addr(0);
180 oshdr.put_sh_offset(0);
d491d34e
ILT
181
182 size_t section_count = (this->data_size()
183 / elfcpp::Elf_sizes<size>::shdr_size);
184 if (section_count < elfcpp::SHN_LORESERVE)
185 oshdr.put_sh_size(0);
186 else
187 oshdr.put_sh_size(section_count);
188
189 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
190 if (shstrndx < elfcpp::SHN_LORESERVE)
191 oshdr.put_sh_link(0);
192 else
193 oshdr.put_sh_link(shstrndx);
194
61ba1cf9
ILT
195 oshdr.put_sh_info(0);
196 oshdr.put_sh_addralign(0);
197 oshdr.put_sh_entsize(0);
198 }
199
200 v += shdr_size;
201
6a74a719 202 unsigned int shndx = 1;
8851ecca 203 if (!parameters->options().relocatable())
6a74a719
ILT
204 {
205 for (Layout::Segment_list::const_iterator p =
206 this->segment_list_->begin();
207 p != this->segment_list_->end();
208 ++p)
209 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
210 this->secnamepool_,
211 v,
212 &shndx);
213 }
214 else
215 {
216 for (Layout::Section_list::const_iterator p =
217 this->section_list_->begin();
218 p != this->section_list_->end();
219 ++p)
220 {
221 // We do unallocated sections below, except that group
222 // sections have to come first.
223 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
224 && (*p)->type() != elfcpp::SHT_GROUP)
225 continue;
226 gold_assert(shndx == (*p)->out_shndx());
227 elfcpp::Shdr_write<size, big_endian> oshdr(v);
228 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
229 v += shdr_size;
230 ++shndx;
231 }
232 }
233
a3ad94ed 234 for (Layout::Section_list::const_iterator p =
16649710
ILT
235 this->unattached_section_list_->begin();
236 p != this->unattached_section_list_->end();
61ba1cf9
ILT
237 ++p)
238 {
6a74a719
ILT
239 // For a relocatable link, we did unallocated group sections
240 // above, since they have to come first.
241 if ((*p)->type() == elfcpp::SHT_GROUP
8851ecca 242 && parameters->options().relocatable())
6a74a719 243 continue;
a3ad94ed 244 gold_assert(shndx == (*p)->out_shndx());
61ba1cf9 245 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 246 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
61ba1cf9 247 v += shdr_size;
ead1e424 248 ++shndx;
61ba1cf9
ILT
249 }
250
251 of->write_output_view(this->offset(), all_shdrs_size, view);
a2fb1b05
ILT
252}
253
54dc6425
ILT
254// Output_segment_header methods.
255
61ba1cf9 256Output_segment_headers::Output_segment_headers(
61ba1cf9 257 const Layout::Segment_list& segment_list)
9025d29d 258 : segment_list_(segment_list)
61ba1cf9 259{
8851ecca 260 const int size = parameters->target().get_size();
61ba1cf9
ILT
261 int phdr_size;
262 if (size == 32)
263 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
264 else if (size == 64)
265 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
266 else
a3ad94ed 267 gold_unreachable();
61ba1cf9
ILT
268
269 this->set_data_size(segment_list.size() * phdr_size);
270}
271
54dc6425 272void
61ba1cf9 273Output_segment_headers::do_write(Output_file* of)
75f65a3e 274{
8851ecca 275 switch (parameters->size_and_endianness())
61ba1cf9 276 {
9025d29d 277#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
278 case Parameters::TARGET_32_LITTLE:
279 this->do_sized_write<32, false>(of);
280 break;
9025d29d 281#endif
8851ecca
ILT
282#ifdef HAVE_TARGET_32_BIG
283 case Parameters::TARGET_32_BIG:
284 this->do_sized_write<32, true>(of);
285 break;
9025d29d 286#endif
9025d29d 287#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
288 case Parameters::TARGET_64_LITTLE:
289 this->do_sized_write<64, false>(of);
290 break;
9025d29d 291#endif
8851ecca
ILT
292#ifdef HAVE_TARGET_64_BIG
293 case Parameters::TARGET_64_BIG:
294 this->do_sized_write<64, true>(of);
295 break;
296#endif
297 default:
298 gold_unreachable();
61ba1cf9 299 }
61ba1cf9
ILT
300}
301
302template<int size, bool big_endian>
303void
304Output_segment_headers::do_sized_write(Output_file* of)
305{
306 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
307 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
a445fddf 308 gold_assert(all_phdrs_size == this->data_size());
61ba1cf9
ILT
309 unsigned char* view = of->get_output_view(this->offset(),
310 all_phdrs_size);
311 unsigned char* v = view;
312 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
313 p != this->segment_list_.end();
314 ++p)
315 {
316 elfcpp::Phdr_write<size, big_endian> ophdr(v);
317 (*p)->write_header(&ophdr);
318 v += phdr_size;
319 }
320
a445fddf
ILT
321 gold_assert(v - view == all_phdrs_size);
322
61ba1cf9 323 of->write_output_view(this->offset(), all_phdrs_size, view);
75f65a3e
ILT
324}
325
326// Output_file_header methods.
327
9025d29d 328Output_file_header::Output_file_header(const Target* target,
75f65a3e 329 const Symbol_table* symtab,
d391083d
ILT
330 const Output_segment_headers* osh,
331 const char* entry)
9025d29d 332 : target_(target),
75f65a3e 333 symtab_(symtab),
61ba1cf9 334 segment_header_(osh),
75f65a3e 335 section_header_(NULL),
d391083d
ILT
336 shstrtab_(NULL),
337 entry_(entry)
75f65a3e 338{
8851ecca 339 const int size = parameters->target().get_size();
61ba1cf9
ILT
340 int ehdr_size;
341 if (size == 32)
342 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
343 else if (size == 64)
344 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
345 else
a3ad94ed 346 gold_unreachable();
61ba1cf9
ILT
347
348 this->set_data_size(ehdr_size);
75f65a3e
ILT
349}
350
351// Set the section table information for a file header.
352
353void
354Output_file_header::set_section_info(const Output_section_headers* shdrs,
355 const Output_section* shstrtab)
356{
357 this->section_header_ = shdrs;
358 this->shstrtab_ = shstrtab;
359}
360
361// Write out the file header.
362
363void
61ba1cf9 364Output_file_header::do_write(Output_file* of)
54dc6425 365{
27bc2bce
ILT
366 gold_assert(this->offset() == 0);
367
8851ecca 368 switch (parameters->size_and_endianness())
61ba1cf9 369 {
9025d29d 370#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
371 case Parameters::TARGET_32_LITTLE:
372 this->do_sized_write<32, false>(of);
373 break;
9025d29d 374#endif
8851ecca
ILT
375#ifdef HAVE_TARGET_32_BIG
376 case Parameters::TARGET_32_BIG:
377 this->do_sized_write<32, true>(of);
378 break;
9025d29d 379#endif
9025d29d 380#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
381 case Parameters::TARGET_64_LITTLE:
382 this->do_sized_write<64, false>(of);
383 break;
9025d29d 384#endif
8851ecca
ILT
385#ifdef HAVE_TARGET_64_BIG
386 case Parameters::TARGET_64_BIG:
387 this->do_sized_write<64, true>(of);
388 break;
389#endif
390 default:
391 gold_unreachable();
61ba1cf9 392 }
61ba1cf9
ILT
393}
394
395// Write out the file header with appropriate size and endianess.
396
397template<int size, bool big_endian>
398void
399Output_file_header::do_sized_write(Output_file* of)
400{
a3ad94ed 401 gold_assert(this->offset() == 0);
61ba1cf9
ILT
402
403 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
404 unsigned char* view = of->get_output_view(0, ehdr_size);
405 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
406
407 unsigned char e_ident[elfcpp::EI_NIDENT];
408 memset(e_ident, 0, elfcpp::EI_NIDENT);
409 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
410 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
411 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
412 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
413 if (size == 32)
414 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
415 else if (size == 64)
416 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
417 else
a3ad94ed 418 gold_unreachable();
61ba1cf9
ILT
419 e_ident[elfcpp::EI_DATA] = (big_endian
420 ? elfcpp::ELFDATA2MSB
421 : elfcpp::ELFDATA2LSB);
422 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
423 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
424 oehdr.put_e_ident(e_ident);
425
426 elfcpp::ET e_type;
8851ecca 427 if (parameters->options().relocatable())
61ba1cf9 428 e_type = elfcpp::ET_REL;
8851ecca 429 else if (parameters->options().shared())
436ca963 430 e_type = elfcpp::ET_DYN;
61ba1cf9
ILT
431 else
432 e_type = elfcpp::ET_EXEC;
433 oehdr.put_e_type(e_type);
434
435 oehdr.put_e_machine(this->target_->machine_code());
436 oehdr.put_e_version(elfcpp::EV_CURRENT);
437
d391083d 438 oehdr.put_e_entry(this->entry<size>());
61ba1cf9 439
6a74a719
ILT
440 if (this->segment_header_ == NULL)
441 oehdr.put_e_phoff(0);
442 else
443 oehdr.put_e_phoff(this->segment_header_->offset());
444
61ba1cf9
ILT
445 oehdr.put_e_shoff(this->section_header_->offset());
446
447 // FIXME: The target needs to set the flags.
448 oehdr.put_e_flags(0);
449
450 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
6a74a719
ILT
451
452 if (this->segment_header_ == NULL)
453 {
454 oehdr.put_e_phentsize(0);
455 oehdr.put_e_phnum(0);
456 }
457 else
458 {
459 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
460 oehdr.put_e_phnum(this->segment_header_->data_size()
461 / elfcpp::Elf_sizes<size>::phdr_size);
462 }
463
61ba1cf9 464 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
d491d34e
ILT
465 size_t section_count = (this->section_header_->data_size()
466 / elfcpp::Elf_sizes<size>::shdr_size);
467
468 if (section_count < elfcpp::SHN_LORESERVE)
469 oehdr.put_e_shnum(this->section_header_->data_size()
470 / elfcpp::Elf_sizes<size>::shdr_size);
471 else
472 oehdr.put_e_shnum(0);
473
474 unsigned int shstrndx = this->shstrtab_->out_shndx();
475 if (shstrndx < elfcpp::SHN_LORESERVE)
476 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
477 else
478 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
61ba1cf9
ILT
479
480 of->write_output_view(0, ehdr_size, view);
54dc6425
ILT
481}
482
d391083d
ILT
483// Return the value to use for the entry address. THIS->ENTRY_ is the
484// symbol specified on the command line, if any.
485
486template<int size>
487typename elfcpp::Elf_types<size>::Elf_Addr
488Output_file_header::entry()
489{
490 const bool should_issue_warning = (this->entry_ != NULL
8851ecca
ILT
491 && !parameters->options().relocatable()
492 && !parameters->options().shared());
d391083d
ILT
493
494 // FIXME: Need to support target specific entry symbol.
495 const char* entry = this->entry_;
496 if (entry == NULL)
497 entry = "_start";
498
499 Symbol* sym = this->symtab_->lookup(entry);
500
501 typename Sized_symbol<size>::Value_type v;
502 if (sym != NULL)
503 {
504 Sized_symbol<size>* ssym;
505 ssym = this->symtab_->get_sized_symbol<size>(sym);
506 if (!ssym->is_defined() && should_issue_warning)
507 gold_warning("entry symbol '%s' exists but is not defined", entry);
508 v = ssym->value();
509 }
510 else
511 {
512 // We couldn't find the entry symbol. See if we can parse it as
513 // a number. This supports, e.g., -e 0x1000.
514 char* endptr;
515 v = strtoull(entry, &endptr, 0);
516 if (*endptr != '\0')
517 {
518 if (should_issue_warning)
519 gold_warning("cannot find entry symbol '%s'", entry);
520 v = 0;
521 }
522 }
523
524 return v;
525}
526
dbe717ef
ILT
527// Output_data_const methods.
528
529void
a3ad94ed 530Output_data_const::do_write(Output_file* of)
dbe717ef 531{
a3ad94ed
ILT
532 of->write(this->offset(), this->data_.data(), this->data_.size());
533}
534
535// Output_data_const_buffer methods.
536
537void
538Output_data_const_buffer::do_write(Output_file* of)
539{
540 of->write(this->offset(), this->p_, this->data_size());
dbe717ef
ILT
541}
542
543// Output_section_data methods.
544
16649710
ILT
545// Record the output section, and set the entry size and such.
546
547void
548Output_section_data::set_output_section(Output_section* os)
549{
550 gold_assert(this->output_section_ == NULL);
551 this->output_section_ = os;
552 this->do_adjust_output_section(os);
553}
554
555// Return the section index of the output section.
556
dbe717ef
ILT
557unsigned int
558Output_section_data::do_out_shndx() const
559{
a3ad94ed 560 gold_assert(this->output_section_ != NULL);
dbe717ef
ILT
561 return this->output_section_->out_shndx();
562}
563
759b1a24
ILT
564// Set the alignment, which means we may need to update the alignment
565// of the output section.
566
567void
568Output_section_data::set_addralign(uint64_t addralign)
569{
570 this->addralign_ = addralign;
571 if (this->output_section_ != NULL
572 && this->output_section_->addralign() < addralign)
573 this->output_section_->set_addralign(addralign);
574}
575
a3ad94ed
ILT
576// Output_data_strtab methods.
577
27bc2bce 578// Set the final data size.
a3ad94ed
ILT
579
580void
27bc2bce 581Output_data_strtab::set_final_data_size()
a3ad94ed
ILT
582{
583 this->strtab_->set_string_offsets();
584 this->set_data_size(this->strtab_->get_strtab_size());
585}
586
587// Write out a string table.
588
589void
590Output_data_strtab::do_write(Output_file* of)
591{
592 this->strtab_->write(of, this->offset());
593}
594
c06b7b0b
ILT
595// Output_reloc methods.
596
7bf1f802
ILT
597// A reloc against a global symbol.
598
599template<bool dynamic, int size, bool big_endian>
600Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
601 Symbol* gsym,
602 unsigned int type,
603 Output_data* od,
e8c846c3
ILT
604 Address address,
605 bool is_relative)
7bf1f802 606 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
dceae3c1 607 is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
7bf1f802 608{
dceae3c1
ILT
609 // this->type_ is a bitfield; make sure TYPE fits.
610 gold_assert(this->type_ == type);
7bf1f802
ILT
611 this->u1_.gsym = gsym;
612 this->u2_.od = od;
dceae3c1
ILT
613 if (dynamic)
614 this->set_needs_dynsym_index();
7bf1f802
ILT
615}
616
617template<bool dynamic, int size, bool big_endian>
618Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
619 Symbol* gsym,
620 unsigned int type,
ef9beddf 621 Sized_relobj<size, big_endian>* relobj,
7bf1f802 622 unsigned int shndx,
e8c846c3
ILT
623 Address address,
624 bool is_relative)
7bf1f802 625 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
dceae3c1 626 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
7bf1f802
ILT
627{
628 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
629 // this->type_ is a bitfield; make sure TYPE fits.
630 gold_assert(this->type_ == type);
7bf1f802
ILT
631 this->u1_.gsym = gsym;
632 this->u2_.relobj = relobj;
dceae3c1
ILT
633 if (dynamic)
634 this->set_needs_dynsym_index();
7bf1f802
ILT
635}
636
637// A reloc against a local symbol.
638
639template<bool dynamic, int size, bool big_endian>
640Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
641 Sized_relobj<size, big_endian>* relobj,
642 unsigned int local_sym_index,
643 unsigned int type,
644 Output_data* od,
e8c846c3 645 Address address,
dceae3c1
ILT
646 bool is_relative,
647 bool is_section_symbol)
7bf1f802 648 : address_(address), local_sym_index_(local_sym_index), type_(type),
dceae3c1
ILT
649 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
650 shndx_(INVALID_CODE)
7bf1f802
ILT
651{
652 gold_assert(local_sym_index != GSYM_CODE
653 && local_sym_index != INVALID_CODE);
dceae3c1
ILT
654 // this->type_ is a bitfield; make sure TYPE fits.
655 gold_assert(this->type_ == type);
7bf1f802
ILT
656 this->u1_.relobj = relobj;
657 this->u2_.od = od;
dceae3c1
ILT
658 if (dynamic)
659 this->set_needs_dynsym_index();
7bf1f802
ILT
660}
661
662template<bool dynamic, int size, bool big_endian>
663Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
664 Sized_relobj<size, big_endian>* relobj,
665 unsigned int local_sym_index,
666 unsigned int type,
667 unsigned int shndx,
e8c846c3 668 Address address,
dceae3c1
ILT
669 bool is_relative,
670 bool is_section_symbol)
7bf1f802 671 : address_(address), local_sym_index_(local_sym_index), type_(type),
dceae3c1
ILT
672 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
673 shndx_(shndx)
7bf1f802
ILT
674{
675 gold_assert(local_sym_index != GSYM_CODE
676 && local_sym_index != INVALID_CODE);
677 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
678 // this->type_ is a bitfield; make sure TYPE fits.
679 gold_assert(this->type_ == type);
7bf1f802
ILT
680 this->u1_.relobj = relobj;
681 this->u2_.relobj = relobj;
dceae3c1
ILT
682 if (dynamic)
683 this->set_needs_dynsym_index();
7bf1f802
ILT
684}
685
686// A reloc against the STT_SECTION symbol of an output section.
687
688template<bool dynamic, int size, bool big_endian>
689Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
690 Output_section* os,
691 unsigned int type,
692 Output_data* od,
693 Address address)
694 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
dceae3c1 695 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
7bf1f802 696{
dceae3c1
ILT
697 // this->type_ is a bitfield; make sure TYPE fits.
698 gold_assert(this->type_ == type);
7bf1f802
ILT
699 this->u1_.os = os;
700 this->u2_.od = od;
701 if (dynamic)
dceae3c1
ILT
702 this->set_needs_dynsym_index();
703 else
704 os->set_needs_symtab_index();
7bf1f802
ILT
705}
706
707template<bool dynamic, int size, bool big_endian>
708Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
709 Output_section* os,
710 unsigned int type,
ef9beddf 711 Sized_relobj<size, big_endian>* relobj,
7bf1f802
ILT
712 unsigned int shndx,
713 Address address)
714 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
dceae3c1 715 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
7bf1f802
ILT
716{
717 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
718 // this->type_ is a bitfield; make sure TYPE fits.
719 gold_assert(this->type_ == type);
7bf1f802
ILT
720 this->u1_.os = os;
721 this->u2_.relobj = relobj;
722 if (dynamic)
dceae3c1
ILT
723 this->set_needs_dynsym_index();
724 else
725 os->set_needs_symtab_index();
726}
727
728// Record that we need a dynamic symbol index for this relocation.
729
730template<bool dynamic, int size, bool big_endian>
731void
732Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
733set_needs_dynsym_index()
734{
735 if (this->is_relative_)
736 return;
737 switch (this->local_sym_index_)
738 {
739 case INVALID_CODE:
740 gold_unreachable();
741
742 case GSYM_CODE:
743 this->u1_.gsym->set_needs_dynsym_entry();
744 break;
745
746 case SECTION_CODE:
747 this->u1_.os->set_needs_dynsym_index();
748 break;
749
750 case 0:
751 break;
752
753 default:
754 {
755 const unsigned int lsi = this->local_sym_index_;
756 if (!this->is_section_symbol_)
757 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
758 else
ef9beddf 759 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
dceae3c1
ILT
760 }
761 break;
762 }
7bf1f802
ILT
763}
764
c06b7b0b
ILT
765// Get the symbol index of a relocation.
766
767template<bool dynamic, int size, bool big_endian>
768unsigned int
769Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
770 const
771{
772 unsigned int index;
773 switch (this->local_sym_index_)
774 {
775 case INVALID_CODE:
a3ad94ed 776 gold_unreachable();
c06b7b0b
ILT
777
778 case GSYM_CODE:
5a6f7e2d 779 if (this->u1_.gsym == NULL)
c06b7b0b
ILT
780 index = 0;
781 else if (dynamic)
5a6f7e2d 782 index = this->u1_.gsym->dynsym_index();
c06b7b0b 783 else
5a6f7e2d 784 index = this->u1_.gsym->symtab_index();
c06b7b0b
ILT
785 break;
786
787 case SECTION_CODE:
788 if (dynamic)
5a6f7e2d 789 index = this->u1_.os->dynsym_index();
c06b7b0b 790 else
5a6f7e2d 791 index = this->u1_.os->symtab_index();
c06b7b0b
ILT
792 break;
793
436ca963
ILT
794 case 0:
795 // Relocations without symbols use a symbol index of 0.
796 index = 0;
797 break;
798
c06b7b0b 799 default:
dceae3c1
ILT
800 {
801 const unsigned int lsi = this->local_sym_index_;
802 if (!this->is_section_symbol_)
803 {
804 if (dynamic)
805 index = this->u1_.relobj->dynsym_index(lsi);
806 else
807 index = this->u1_.relobj->symtab_index(lsi);
808 }
809 else
810 {
ef9beddf 811 Output_section* os = this->u1_.relobj->output_section(lsi);
dceae3c1
ILT
812 gold_assert(os != NULL);
813 if (dynamic)
814 index = os->dynsym_index();
815 else
816 index = os->symtab_index();
817 }
818 }
c06b7b0b
ILT
819 break;
820 }
a3ad94ed 821 gold_assert(index != -1U);
c06b7b0b
ILT
822 return index;
823}
824
624f8810
ILT
825// For a local section symbol, get the address of the offset ADDEND
826// within the input section.
dceae3c1
ILT
827
828template<bool dynamic, int size, bool big_endian>
ef9beddf 829typename elfcpp::Elf_types<size>::Elf_Addr
dceae3c1 830Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
624f8810 831 local_section_offset(Addend addend) const
dceae3c1 832{
624f8810
ILT
833 gold_assert(this->local_sym_index_ != GSYM_CODE
834 && this->local_sym_index_ != SECTION_CODE
835 && this->local_sym_index_ != INVALID_CODE
836 && this->is_section_symbol_);
dceae3c1 837 const unsigned int lsi = this->local_sym_index_;
ef9beddf 838 Output_section* os = this->u1_.relobj->output_section(lsi);
624f8810 839 gold_assert(os != NULL);
ef9beddf
ILT
840 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
841 if (offset != -1U)
624f8810
ILT
842 return offset + addend;
843 // This is a merge section.
844 offset = os->output_address(this->u1_.relobj, lsi, addend);
ef9beddf 845 gold_assert(offset != -1U);
dceae3c1
ILT
846 return offset;
847}
848
d98bc257 849// Get the output address of a relocation.
c06b7b0b
ILT
850
851template<bool dynamic, int size, bool big_endian>
a984ee1d 852typename elfcpp::Elf_types<size>::Elf_Addr
d98bc257 853Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
c06b7b0b 854{
a3ad94ed 855 Address address = this->address_;
5a6f7e2d
ILT
856 if (this->shndx_ != INVALID_CODE)
857 {
ef9beddf 858 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
5a6f7e2d 859 gold_assert(os != NULL);
ef9beddf
ILT
860 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
861 if (off != -1U)
730cdc88
ILT
862 address += os->address() + off;
863 else
864 {
865 address = os->output_address(this->u2_.relobj, this->shndx_,
866 address);
867 gold_assert(address != -1U);
868 }
5a6f7e2d
ILT
869 }
870 else if (this->u2_.od != NULL)
871 address += this->u2_.od->address();
d98bc257
ILT
872 return address;
873}
874
875// Write out the offset and info fields of a Rel or Rela relocation
876// entry.
877
878template<bool dynamic, int size, bool big_endian>
879template<typename Write_rel>
880void
881Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
882 Write_rel* wr) const
883{
884 wr->put_r_offset(this->get_address());
e8c846c3
ILT
885 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
886 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
c06b7b0b
ILT
887}
888
889// Write out a Rel relocation.
890
891template<bool dynamic, int size, bool big_endian>
892void
893Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
894 unsigned char* pov) const
895{
896 elfcpp::Rel_write<size, big_endian> orel(pov);
897 this->write_rel(&orel);
898}
899
e8c846c3
ILT
900// Get the value of the symbol referred to by a Rel relocation.
901
902template<bool dynamic, int size, bool big_endian>
903typename elfcpp::Elf_types<size>::Elf_Addr
d1f003c6 904Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
624f8810 905 Addend addend) const
e8c846c3
ILT
906{
907 if (this->local_sym_index_ == GSYM_CODE)
908 {
909 const Sized_symbol<size>* sym;
910 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
d1f003c6 911 return sym->value() + addend;
e8c846c3
ILT
912 }
913 gold_assert(this->local_sym_index_ != SECTION_CODE
d1f003c6
ILT
914 && this->local_sym_index_ != INVALID_CODE
915 && !this->is_section_symbol_);
916 const unsigned int lsi = this->local_sym_index_;
917 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
918 return symval->value(this->u1_.relobj, addend);
e8c846c3
ILT
919}
920
d98bc257
ILT
921// Reloc comparison. This function sorts the dynamic relocs for the
922// benefit of the dynamic linker. First we sort all relative relocs
923// to the front. Among relative relocs, we sort by output address.
924// Among non-relative relocs, we sort by symbol index, then by output
925// address.
926
927template<bool dynamic, int size, bool big_endian>
928int
929Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
930 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
931 const
932{
933 if (this->is_relative_)
934 {
935 if (!r2.is_relative_)
936 return -1;
937 // Otherwise sort by reloc address below.
938 }
939 else if (r2.is_relative_)
940 return 1;
941 else
942 {
943 unsigned int sym1 = this->get_symbol_index();
944 unsigned int sym2 = r2.get_symbol_index();
945 if (sym1 < sym2)
946 return -1;
947 else if (sym1 > sym2)
948 return 1;
949 // Otherwise sort by reloc address.
950 }
951
952 section_offset_type addr1 = this->get_address();
953 section_offset_type addr2 = r2.get_address();
954 if (addr1 < addr2)
955 return -1;
956 else if (addr1 > addr2)
957 return 1;
958
959 // Final tie breaker, in order to generate the same output on any
960 // host: reloc type.
961 unsigned int type1 = this->type_;
962 unsigned int type2 = r2.type_;
963 if (type1 < type2)
964 return -1;
965 else if (type1 > type2)
966 return 1;
967
968 // These relocs appear to be exactly the same.
969 return 0;
970}
971
c06b7b0b
ILT
972// Write out a Rela relocation.
973
974template<bool dynamic, int size, bool big_endian>
975void
976Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
977 unsigned char* pov) const
978{
979 elfcpp::Rela_write<size, big_endian> orel(pov);
980 this->rel_.write_rel(&orel);
e8c846c3 981 Addend addend = this->addend_;
dceae3c1 982 if (this->rel_.is_relative())
d1f003c6
ILT
983 addend = this->rel_.symbol_value(addend);
984 else if (this->rel_.is_local_section_symbol())
624f8810 985 addend = this->rel_.local_section_offset(addend);
e8c846c3 986 orel.put_r_addend(addend);
c06b7b0b
ILT
987}
988
989// Output_data_reloc_base methods.
990
16649710
ILT
991// Adjust the output section.
992
993template<int sh_type, bool dynamic, int size, bool big_endian>
994void
995Output_data_reloc_base<sh_type, dynamic, size, big_endian>
996 ::do_adjust_output_section(Output_section* os)
997{
998 if (sh_type == elfcpp::SHT_REL)
999 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1000 else if (sh_type == elfcpp::SHT_RELA)
1001 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1002 else
1003 gold_unreachable();
1004 if (dynamic)
1005 os->set_should_link_to_dynsym();
1006 else
1007 os->set_should_link_to_symtab();
1008}
1009
c06b7b0b
ILT
1010// Write out relocation data.
1011
1012template<int sh_type, bool dynamic, int size, bool big_endian>
1013void
1014Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1015 Output_file* of)
1016{
1017 const off_t off = this->offset();
1018 const off_t oview_size = this->data_size();
1019 unsigned char* const oview = of->get_output_view(off, oview_size);
1020
d98bc257
ILT
1021 if (this->sort_relocs_)
1022 {
1023 gold_assert(dynamic);
1024 std::sort(this->relocs_.begin(), this->relocs_.end(),
1025 Sort_relocs_comparison());
1026 }
1027
c06b7b0b
ILT
1028 unsigned char* pov = oview;
1029 for (typename Relocs::const_iterator p = this->relocs_.begin();
1030 p != this->relocs_.end();
1031 ++p)
1032 {
1033 p->write(pov);
1034 pov += reloc_size;
1035 }
1036
a3ad94ed 1037 gold_assert(pov - oview == oview_size);
c06b7b0b
ILT
1038
1039 of->write_output_view(off, oview_size, oview);
1040
1041 // We no longer need the relocation entries.
1042 this->relocs_.clear();
1043}
1044
6a74a719
ILT
1045// Class Output_relocatable_relocs.
1046
1047template<int sh_type, int size, bool big_endian>
1048void
1049Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1050{
1051 this->set_data_size(this->rr_->output_reloc_count()
1052 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1053}
1054
1055// class Output_data_group.
1056
1057template<int size, bool big_endian>
1058Output_data_group<size, big_endian>::Output_data_group(
1059 Sized_relobj<size, big_endian>* relobj,
1060 section_size_type entry_count,
8825ac63
ILT
1061 elfcpp::Elf_Word flags,
1062 std::vector<unsigned int>* input_shndxes)
6a74a719 1063 : Output_section_data(entry_count * 4, 4),
8825ac63
ILT
1064 relobj_(relobj),
1065 flags_(flags)
6a74a719 1066{
8825ac63 1067 this->input_shndxes_.swap(*input_shndxes);
6a74a719
ILT
1068}
1069
1070// Write out the section group, which means translating the section
1071// indexes to apply to the output file.
1072
1073template<int size, bool big_endian>
1074void
1075Output_data_group<size, big_endian>::do_write(Output_file* of)
1076{
1077 const off_t off = this->offset();
1078 const section_size_type oview_size =
1079 convert_to_section_size_type(this->data_size());
1080 unsigned char* const oview = of->get_output_view(off, oview_size);
1081
1082 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1083 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1084 ++contents;
1085
1086 for (std::vector<unsigned int>::const_iterator p =
8825ac63
ILT
1087 this->input_shndxes_.begin();
1088 p != this->input_shndxes_.end();
6a74a719
ILT
1089 ++p, ++contents)
1090 {
ef9beddf 1091 Output_section* os = this->relobj_->output_section(*p);
6a74a719
ILT
1092
1093 unsigned int output_shndx;
1094 if (os != NULL)
1095 output_shndx = os->out_shndx();
1096 else
1097 {
1098 this->relobj_->error(_("section group retained but "
1099 "group element discarded"));
1100 output_shndx = 0;
1101 }
1102
1103 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1104 }
1105
1106 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1107 gold_assert(wrote == oview_size);
1108
1109 of->write_output_view(off, oview_size, oview);
1110
1111 // We no longer need this information.
8825ac63 1112 this->input_shndxes_.clear();
6a74a719
ILT
1113}
1114
dbe717ef 1115// Output_data_got::Got_entry methods.
ead1e424
ILT
1116
1117// Write out the entry.
1118
1119template<int size, bool big_endian>
1120void
7e1edb90 1121Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
ead1e424
ILT
1122{
1123 Valtype val = 0;
1124
1125 switch (this->local_sym_index_)
1126 {
1127 case GSYM_CODE:
1128 {
e8c846c3
ILT
1129 // If the symbol is resolved locally, we need to write out the
1130 // link-time value, which will be relocated dynamically by a
1131 // RELATIVE relocation.
ead1e424 1132 Symbol* gsym = this->u_.gsym;
e8c846c3
ILT
1133 Sized_symbol<size>* sgsym;
1134 // This cast is a bit ugly. We don't want to put a
1135 // virtual method in Symbol, because we want Symbol to be
1136 // as small as possible.
1137 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1138 val = sgsym->value();
ead1e424
ILT
1139 }
1140 break;
1141
1142 case CONSTANT_CODE:
1143 val = this->u_.constant;
1144 break;
1145
1146 default:
d1f003c6
ILT
1147 {
1148 const unsigned int lsi = this->local_sym_index_;
1149 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1150 val = symval->value(this->u_.object, 0);
1151 }
e727fa71 1152 break;
ead1e424
ILT
1153 }
1154
a3ad94ed 1155 elfcpp::Swap<size, big_endian>::writeval(pov, val);
ead1e424
ILT
1156}
1157
dbe717ef 1158// Output_data_got methods.
ead1e424 1159
dbe717ef
ILT
1160// Add an entry for a global symbol to the GOT. This returns true if
1161// this is a new GOT entry, false if the symbol already had a GOT
1162// entry.
1163
1164template<int size, bool big_endian>
1165bool
0a65a3a7
CC
1166Output_data_got<size, big_endian>::add_global(
1167 Symbol* gsym,
1168 unsigned int got_type)
ead1e424 1169{
0a65a3a7 1170 if (gsym->has_got_offset(got_type))
dbe717ef 1171 return false;
ead1e424 1172
dbe717ef
ILT
1173 this->entries_.push_back(Got_entry(gsym));
1174 this->set_got_size();
0a65a3a7 1175 gsym->set_got_offset(got_type, this->last_got_offset());
dbe717ef
ILT
1176 return true;
1177}
ead1e424 1178
7bf1f802
ILT
1179// Add an entry for a global symbol to the GOT, and add a dynamic
1180// relocation of type R_TYPE for the GOT entry.
1181template<int size, bool big_endian>
1182void
1183Output_data_got<size, big_endian>::add_global_with_rel(
1184 Symbol* gsym,
0a65a3a7 1185 unsigned int got_type,
7bf1f802
ILT
1186 Rel_dyn* rel_dyn,
1187 unsigned int r_type)
1188{
0a65a3a7 1189 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1190 return;
1191
1192 this->entries_.push_back(Got_entry());
1193 this->set_got_size();
1194 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1195 gsym->set_got_offset(got_type, got_offset);
7bf1f802
ILT
1196 rel_dyn->add_global(gsym, r_type, this, got_offset);
1197}
1198
1199template<int size, bool big_endian>
1200void
1201Output_data_got<size, big_endian>::add_global_with_rela(
1202 Symbol* gsym,
0a65a3a7 1203 unsigned int got_type,
7bf1f802
ILT
1204 Rela_dyn* rela_dyn,
1205 unsigned int r_type)
1206{
0a65a3a7 1207 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1208 return;
1209
1210 this->entries_.push_back(Got_entry());
1211 this->set_got_size();
1212 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1213 gsym->set_got_offset(got_type, got_offset);
7bf1f802
ILT
1214 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1215}
1216
0a65a3a7
CC
1217// Add a pair of entries for a global symbol to the GOT, and add
1218// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1219// If R_TYPE_2 == 0, add the second entry with no relocation.
7bf1f802
ILT
1220template<int size, bool big_endian>
1221void
0a65a3a7
CC
1222Output_data_got<size, big_endian>::add_global_pair_with_rel(
1223 Symbol* gsym,
1224 unsigned int got_type,
7bf1f802 1225 Rel_dyn* rel_dyn,
0a65a3a7
CC
1226 unsigned int r_type_1,
1227 unsigned int r_type_2)
7bf1f802 1228{
0a65a3a7 1229 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1230 return;
1231
1232 this->entries_.push_back(Got_entry());
7bf1f802 1233 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1234 gsym->set_got_offset(got_type, got_offset);
1235 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1236
1237 this->entries_.push_back(Got_entry());
1238 if (r_type_2 != 0)
1239 {
1240 got_offset = this->last_got_offset();
1241 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1242 }
1243
1244 this->set_got_size();
7bf1f802
ILT
1245}
1246
1247template<int size, bool big_endian>
1248void
0a65a3a7
CC
1249Output_data_got<size, big_endian>::add_global_pair_with_rela(
1250 Symbol* gsym,
1251 unsigned int got_type,
7bf1f802 1252 Rela_dyn* rela_dyn,
0a65a3a7
CC
1253 unsigned int r_type_1,
1254 unsigned int r_type_2)
7bf1f802 1255{
0a65a3a7 1256 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1257 return;
1258
1259 this->entries_.push_back(Got_entry());
7bf1f802 1260 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1261 gsym->set_got_offset(got_type, got_offset);
1262 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1263
1264 this->entries_.push_back(Got_entry());
1265 if (r_type_2 != 0)
1266 {
1267 got_offset = this->last_got_offset();
1268 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1269 }
1270
1271 this->set_got_size();
7bf1f802
ILT
1272}
1273
0a65a3a7
CC
1274// Add an entry for a local symbol to the GOT. This returns true if
1275// this is a new GOT entry, false if the symbol already has a GOT
1276// entry.
07f397ab
ILT
1277
1278template<int size, bool big_endian>
1279bool
0a65a3a7
CC
1280Output_data_got<size, big_endian>::add_local(
1281 Sized_relobj<size, big_endian>* object,
1282 unsigned int symndx,
1283 unsigned int got_type)
07f397ab 1284{
0a65a3a7 1285 if (object->local_has_got_offset(symndx, got_type))
07f397ab
ILT
1286 return false;
1287
0a65a3a7 1288 this->entries_.push_back(Got_entry(object, symndx));
07f397ab 1289 this->set_got_size();
0a65a3a7 1290 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
07f397ab
ILT
1291 return true;
1292}
1293
0a65a3a7
CC
1294// Add an entry for a local symbol to the GOT, and add a dynamic
1295// relocation of type R_TYPE for the GOT entry.
7bf1f802
ILT
1296template<int size, bool big_endian>
1297void
0a65a3a7
CC
1298Output_data_got<size, big_endian>::add_local_with_rel(
1299 Sized_relobj<size, big_endian>* object,
1300 unsigned int symndx,
1301 unsigned int got_type,
7bf1f802
ILT
1302 Rel_dyn* rel_dyn,
1303 unsigned int r_type)
1304{
0a65a3a7 1305 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1306 return;
1307
1308 this->entries_.push_back(Got_entry());
1309 this->set_got_size();
1310 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1311 object->set_local_got_offset(symndx, got_type, got_offset);
1312 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
7bf1f802
ILT
1313}
1314
1315template<int size, bool big_endian>
1316void
0a65a3a7
CC
1317Output_data_got<size, big_endian>::add_local_with_rela(
1318 Sized_relobj<size, big_endian>* object,
1319 unsigned int symndx,
1320 unsigned int got_type,
7bf1f802
ILT
1321 Rela_dyn* rela_dyn,
1322 unsigned int r_type)
1323{
0a65a3a7 1324 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1325 return;
1326
1327 this->entries_.push_back(Got_entry());
1328 this->set_got_size();
1329 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1330 object->set_local_got_offset(symndx, got_type, got_offset);
1331 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
07f397ab
ILT
1332}
1333
0a65a3a7
CC
1334// Add a pair of entries for a local symbol to the GOT, and add
1335// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1336// If R_TYPE_2 == 0, add the second entry with no relocation.
7bf1f802
ILT
1337template<int size, bool big_endian>
1338void
0a65a3a7 1339Output_data_got<size, big_endian>::add_local_pair_with_rel(
7bf1f802
ILT
1340 Sized_relobj<size, big_endian>* object,
1341 unsigned int symndx,
1342 unsigned int shndx,
0a65a3a7 1343 unsigned int got_type,
7bf1f802 1344 Rel_dyn* rel_dyn,
0a65a3a7
CC
1345 unsigned int r_type_1,
1346 unsigned int r_type_2)
7bf1f802 1347{
0a65a3a7 1348 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1349 return;
1350
1351 this->entries_.push_back(Got_entry());
1352 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1353 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1354 Output_section* os = object->output_section(shndx);
0a65a3a7 1355 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
7bf1f802 1356
0a65a3a7
CC
1357 this->entries_.push_back(Got_entry(object, symndx));
1358 if (r_type_2 != 0)
1359 {
1360 got_offset = this->last_got_offset();
1361 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1362 }
7bf1f802
ILT
1363
1364 this->set_got_size();
1365}
1366
1367template<int size, bool big_endian>
1368void
0a65a3a7 1369Output_data_got<size, big_endian>::add_local_pair_with_rela(
7bf1f802
ILT
1370 Sized_relobj<size, big_endian>* object,
1371 unsigned int symndx,
1372 unsigned int shndx,
0a65a3a7 1373 unsigned int got_type,
7bf1f802 1374 Rela_dyn* rela_dyn,
0a65a3a7
CC
1375 unsigned int r_type_1,
1376 unsigned int r_type_2)
7bf1f802 1377{
0a65a3a7 1378 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1379 return;
1380
1381 this->entries_.push_back(Got_entry());
1382 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1383 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1384 Output_section* os = object->output_section(shndx);
0a65a3a7 1385 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
7bf1f802 1386
0a65a3a7
CC
1387 this->entries_.push_back(Got_entry(object, symndx));
1388 if (r_type_2 != 0)
1389 {
1390 got_offset = this->last_got_offset();
1391 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1392 }
7bf1f802
ILT
1393
1394 this->set_got_size();
1395}
1396
ead1e424
ILT
1397// Write out the GOT.
1398
1399template<int size, bool big_endian>
1400void
dbe717ef 1401Output_data_got<size, big_endian>::do_write(Output_file* of)
ead1e424
ILT
1402{
1403 const int add = size / 8;
1404
1405 const off_t off = this->offset();
c06b7b0b 1406 const off_t oview_size = this->data_size();
ead1e424
ILT
1407 unsigned char* const oview = of->get_output_view(off, oview_size);
1408
1409 unsigned char* pov = oview;
1410 for (typename Got_entries::const_iterator p = this->entries_.begin();
1411 p != this->entries_.end();
1412 ++p)
1413 {
7e1edb90 1414 p->write(pov);
ead1e424
ILT
1415 pov += add;
1416 }
1417
a3ad94ed 1418 gold_assert(pov - oview == oview_size);
c06b7b0b 1419
ead1e424
ILT
1420 of->write_output_view(off, oview_size, oview);
1421
1422 // We no longer need the GOT entries.
1423 this->entries_.clear();
1424}
1425
a3ad94ed
ILT
1426// Output_data_dynamic::Dynamic_entry methods.
1427
1428// Write out the entry.
1429
1430template<int size, bool big_endian>
1431void
1432Output_data_dynamic::Dynamic_entry::write(
1433 unsigned char* pov,
7d1a9ebb 1434 const Stringpool* pool) const
a3ad94ed
ILT
1435{
1436 typename elfcpp::Elf_types<size>::Elf_WXword val;
c2b45e22 1437 switch (this->offset_)
a3ad94ed
ILT
1438 {
1439 case DYNAMIC_NUMBER:
1440 val = this->u_.val;
1441 break;
1442
a3ad94ed 1443 case DYNAMIC_SECTION_SIZE:
16649710 1444 val = this->u_.od->data_size();
a3ad94ed
ILT
1445 break;
1446
1447 case DYNAMIC_SYMBOL:
1448 {
16649710
ILT
1449 const Sized_symbol<size>* s =
1450 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
1451 val = s->value();
1452 }
1453 break;
1454
1455 case DYNAMIC_STRING:
1456 val = pool->get_offset(this->u_.str);
1457 break;
1458
1459 default:
c2b45e22
CC
1460 val = this->u_.od->address() + this->offset_;
1461 break;
a3ad94ed
ILT
1462 }
1463
1464 elfcpp::Dyn_write<size, big_endian> dw(pov);
1465 dw.put_d_tag(this->tag_);
1466 dw.put_d_val(val);
1467}
1468
1469// Output_data_dynamic methods.
1470
16649710
ILT
1471// Adjust the output section to set the entry size.
1472
1473void
1474Output_data_dynamic::do_adjust_output_section(Output_section* os)
1475{
8851ecca 1476 if (parameters->target().get_size() == 32)
16649710 1477 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
8851ecca 1478 else if (parameters->target().get_size() == 64)
16649710
ILT
1479 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1480 else
1481 gold_unreachable();
1482}
1483
a3ad94ed
ILT
1484// Set the final data size.
1485
1486void
27bc2bce 1487Output_data_dynamic::set_final_data_size()
a3ad94ed
ILT
1488{
1489 // Add the terminating entry.
1490 this->add_constant(elfcpp::DT_NULL, 0);
1491
1492 int dyn_size;
8851ecca 1493 if (parameters->target().get_size() == 32)
a3ad94ed 1494 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
8851ecca 1495 else if (parameters->target().get_size() == 64)
a3ad94ed
ILT
1496 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1497 else
1498 gold_unreachable();
1499 this->set_data_size(this->entries_.size() * dyn_size);
1500}
1501
1502// Write out the dynamic entries.
1503
1504void
1505Output_data_dynamic::do_write(Output_file* of)
1506{
8851ecca 1507 switch (parameters->size_and_endianness())
a3ad94ed 1508 {
9025d29d 1509#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
1510 case Parameters::TARGET_32_LITTLE:
1511 this->sized_write<32, false>(of);
1512 break;
9025d29d 1513#endif
8851ecca
ILT
1514#ifdef HAVE_TARGET_32_BIG
1515 case Parameters::TARGET_32_BIG:
1516 this->sized_write<32, true>(of);
1517 break;
9025d29d 1518#endif
9025d29d 1519#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
1520 case Parameters::TARGET_64_LITTLE:
1521 this->sized_write<64, false>(of);
1522 break;
9025d29d 1523#endif
8851ecca
ILT
1524#ifdef HAVE_TARGET_64_BIG
1525 case Parameters::TARGET_64_BIG:
1526 this->sized_write<64, true>(of);
1527 break;
1528#endif
1529 default:
1530 gold_unreachable();
a3ad94ed 1531 }
a3ad94ed
ILT
1532}
1533
1534template<int size, bool big_endian>
1535void
1536Output_data_dynamic::sized_write(Output_file* of)
1537{
1538 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1539
1540 const off_t offset = this->offset();
1541 const off_t oview_size = this->data_size();
1542 unsigned char* const oview = of->get_output_view(offset, oview_size);
1543
1544 unsigned char* pov = oview;
1545 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1546 p != this->entries_.end();
1547 ++p)
1548 {
7d1a9ebb 1549 p->write<size, big_endian>(pov, this->pool_);
a3ad94ed
ILT
1550 pov += dyn_size;
1551 }
1552
1553 gold_assert(pov - oview == oview_size);
1554
1555 of->write_output_view(offset, oview_size, oview);
1556
1557 // We no longer need the dynamic entries.
1558 this->entries_.clear();
1559}
1560
d491d34e
ILT
1561// Class Output_symtab_xindex.
1562
1563void
1564Output_symtab_xindex::do_write(Output_file* of)
1565{
1566 const off_t offset = this->offset();
1567 const off_t oview_size = this->data_size();
1568 unsigned char* const oview = of->get_output_view(offset, oview_size);
1569
1570 memset(oview, 0, oview_size);
1571
1572 if (parameters->target().is_big_endian())
1573 this->endian_do_write<true>(oview);
1574 else
1575 this->endian_do_write<false>(oview);
1576
1577 of->write_output_view(offset, oview_size, oview);
1578
1579 // We no longer need the data.
1580 this->entries_.clear();
1581}
1582
1583template<bool big_endian>
1584void
1585Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1586{
1587 for (Xindex_entries::const_iterator p = this->entries_.begin();
1588 p != this->entries_.end();
1589 ++p)
1590 elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
1591}
1592
ead1e424
ILT
1593// Output_section::Input_section methods.
1594
1595// Return the data size. For an input section we store the size here.
1596// For an Output_section_data, we have to ask it for the size.
1597
1598off_t
1599Output_section::Input_section::data_size() const
1600{
1601 if (this->is_input_section())
b8e6aad9 1602 return this->u1_.data_size;
ead1e424 1603 else
b8e6aad9 1604 return this->u2_.posd->data_size();
ead1e424
ILT
1605}
1606
1607// Set the address and file offset.
1608
1609void
96803768
ILT
1610Output_section::Input_section::set_address_and_file_offset(
1611 uint64_t address,
1612 off_t file_offset,
1613 off_t section_file_offset)
ead1e424
ILT
1614{
1615 if (this->is_input_section())
96803768
ILT
1616 this->u2_.object->set_section_offset(this->shndx_,
1617 file_offset - section_file_offset);
ead1e424 1618 else
96803768
ILT
1619 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1620}
1621
a445fddf
ILT
1622// Reset the address and file offset.
1623
1624void
1625Output_section::Input_section::reset_address_and_file_offset()
1626{
1627 if (!this->is_input_section())
1628 this->u2_.posd->reset_address_and_file_offset();
1629}
1630
96803768
ILT
1631// Finalize the data size.
1632
1633void
1634Output_section::Input_section::finalize_data_size()
1635{
1636 if (!this->is_input_section())
1637 this->u2_.posd->finalize_data_size();
b8e6aad9
ILT
1638}
1639
1e983657
ILT
1640// Try to turn an input offset into an output offset. We want to
1641// return the output offset relative to the start of this
1642// Input_section in the output section.
b8e6aad9 1643
8f00aeb8 1644inline bool
8383303e
ILT
1645Output_section::Input_section::output_offset(
1646 const Relobj* object,
1647 unsigned int shndx,
1648 section_offset_type offset,
1649 section_offset_type *poutput) const
b8e6aad9
ILT
1650{
1651 if (!this->is_input_section())
730cdc88 1652 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
b8e6aad9
ILT
1653 else
1654 {
730cdc88 1655 if (this->shndx_ != shndx || this->u2_.object != object)
b8e6aad9 1656 return false;
1e983657 1657 *poutput = offset;
b8e6aad9
ILT
1658 return true;
1659 }
ead1e424
ILT
1660}
1661
a9a60db6
ILT
1662// Return whether this is the merge section for the input section
1663// SHNDX in OBJECT.
1664
1665inline bool
1666Output_section::Input_section::is_merge_section_for(const Relobj* object,
1667 unsigned int shndx) const
1668{
1669 if (this->is_input_section())
1670 return false;
1671 return this->u2_.posd->is_merge_section_for(object, shndx);
1672}
1673
ead1e424
ILT
1674// Write out the data. We don't have to do anything for an input
1675// section--they are handled via Object::relocate--but this is where
1676// we write out the data for an Output_section_data.
1677
1678void
1679Output_section::Input_section::write(Output_file* of)
1680{
1681 if (!this->is_input_section())
b8e6aad9 1682 this->u2_.posd->write(of);
ead1e424
ILT
1683}
1684
96803768
ILT
1685// Write the data to a buffer. As for write(), we don't have to do
1686// anything for an input section.
1687
1688void
1689Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1690{
1691 if (!this->is_input_section())
1692 this->u2_.posd->write_to_buffer(buffer);
1693}
1694
7d9e3d98
ILT
1695// Print to a map file.
1696
1697void
1698Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1699{
1700 switch (this->shndx_)
1701 {
1702 case OUTPUT_SECTION_CODE:
1703 case MERGE_DATA_SECTION_CODE:
1704 case MERGE_STRING_SECTION_CODE:
1705 this->u2_.posd->print_to_mapfile(mapfile);
1706 break;
1707
1708 default:
1709 mapfile->print_input_section(this->u2_.object, this->shndx_);
1710 break;
1711 }
1712}
1713
a2fb1b05
ILT
1714// Output_section methods.
1715
1716// Construct an Output_section. NAME will point into a Stringpool.
1717
96803768 1718Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
b8e6aad9 1719 elfcpp::Elf_Xword flags)
96803768 1720 : name_(name),
a2fb1b05
ILT
1721 addralign_(0),
1722 entsize_(0),
a445fddf 1723 load_address_(0),
16649710 1724 link_section_(NULL),
a2fb1b05 1725 link_(0),
16649710 1726 info_section_(NULL),
6a74a719 1727 info_symndx_(NULL),
a2fb1b05
ILT
1728 info_(0),
1729 type_(type),
61ba1cf9 1730 flags_(flags),
91ea499d 1731 out_shndx_(-1U),
c06b7b0b
ILT
1732 symtab_index_(0),
1733 dynsym_index_(0),
ead1e424
ILT
1734 input_sections_(),
1735 first_input_offset_(0),
c51e6221 1736 fills_(),
96803768 1737 postprocessing_buffer_(NULL),
a3ad94ed 1738 needs_symtab_index_(false),
16649710
ILT
1739 needs_dynsym_index_(false),
1740 should_link_to_symtab_(false),
730cdc88 1741 should_link_to_dynsym_(false),
27bc2bce 1742 after_input_sections_(false),
7bf1f802 1743 requires_postprocessing_(false),
a445fddf
ILT
1744 found_in_sections_clause_(false),
1745 has_load_address_(false),
755ab8af 1746 info_uses_section_index_(false),
2fd32231
ILT
1747 may_sort_attached_input_sections_(false),
1748 must_sort_attached_input_sections_(false),
1749 attached_input_sections_are_sorted_(false),
9f1d377b
ILT
1750 is_relro_(false),
1751 is_relro_local_(false),
7bf1f802 1752 tls_offset_(0)
a2fb1b05 1753{
27bc2bce
ILT
1754 // An unallocated section has no address. Forcing this means that
1755 // we don't need special treatment for symbols defined in debug
1756 // sections.
1757 if ((flags & elfcpp::SHF_ALLOC) == 0)
1758 this->set_address(0);
a2fb1b05
ILT
1759}
1760
54dc6425
ILT
1761Output_section::~Output_section()
1762{
1763}
1764
16649710
ILT
1765// Set the entry size.
1766
1767void
1768Output_section::set_entsize(uint64_t v)
1769{
1770 if (this->entsize_ == 0)
1771 this->entsize_ = v;
1772 else
1773 gold_assert(this->entsize_ == v);
1774}
1775
ead1e424 1776// Add the input section SHNDX, with header SHDR, named SECNAME, in
730cdc88
ILT
1777// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1778// relocation section which applies to this section, or 0 if none, or
1779// -1U if more than one. Return the offset of the input section
1780// within the output section. Return -1 if the input section will
1781// receive special handling. In the normal case we don't always keep
1782// track of input sections for an Output_section. Instead, each
1783// Object keeps track of the Output_section for each of its input
a445fddf
ILT
1784// sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1785// track of input sections here; this is used when SECTIONS appears in
1786// a linker script.
a2fb1b05
ILT
1787
1788template<int size, bool big_endian>
1789off_t
730cdc88
ILT
1790Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1791 unsigned int shndx,
ead1e424 1792 const char* secname,
730cdc88 1793 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf
ILT
1794 unsigned int reloc_shndx,
1795 bool have_sections_script)
a2fb1b05
ILT
1796{
1797 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1798 if ((addralign & (addralign - 1)) != 0)
1799 {
75f2446e
ILT
1800 object->error(_("invalid alignment %lu for section \"%s\""),
1801 static_cast<unsigned long>(addralign), secname);
1802 addralign = 1;
a2fb1b05 1803 }
a2fb1b05
ILT
1804
1805 if (addralign > this->addralign_)
1806 this->addralign_ = addralign;
1807
44a43cf9 1808 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
154e0e9a 1809 this->update_flags_for_input_section(sh_flags);
a445fddf 1810
4f833eee 1811 uint64_t entsize = shdr.get_sh_entsize();
44a43cf9
ILT
1812
1813 // .debug_str is a mergeable string section, but is not always so
1814 // marked by compilers. Mark manually here so we can optimize.
1815 if (strcmp(secname, ".debug_str") == 0)
4f833eee
ILT
1816 {
1817 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1818 entsize = 1;
1819 }
44a43cf9 1820
b8e6aad9 1821 // If this is a SHF_MERGE section, we pass all the input sections to
730cdc88 1822 // a Output_data_merge. We don't try to handle relocations for such
e0b64032
ILT
1823 // a section. We don't try to handle empty merge sections--they
1824 // mess up the mappings, and are useless anyhow.
44a43cf9 1825 if ((sh_flags & elfcpp::SHF_MERGE) != 0
e0b64032
ILT
1826 && reloc_shndx == 0
1827 && shdr.get_sh_size() > 0)
b8e6aad9 1828 {
44a43cf9 1829 if (this->add_merge_input_section(object, shndx, sh_flags,
96803768 1830 entsize, addralign))
b8e6aad9
ILT
1831 {
1832 // Tell the relocation routines that they need to call the
730cdc88 1833 // output_offset method to determine the final address.
b8e6aad9
ILT
1834 return -1;
1835 }
1836 }
1837
27bc2bce 1838 off_t offset_in_section = this->current_data_size_for_child();
c51e6221
ILT
1839 off_t aligned_offset_in_section = align_address(offset_in_section,
1840 addralign);
1841
1842 if (aligned_offset_in_section > offset_in_section
a445fddf 1843 && !have_sections_script
44a43cf9 1844 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
c51e6221
ILT
1845 && object->target()->has_code_fill())
1846 {
1847 // We need to add some fill data. Using fill_list_ when
1848 // possible is an optimization, since we will often have fill
1849 // sections without input sections.
1850 off_t fill_len = aligned_offset_in_section - offset_in_section;
1851 if (this->input_sections_.empty())
1852 this->fills_.push_back(Fill(offset_in_section, fill_len));
1853 else
1854 {
1855 // FIXME: When relaxing, the size needs to adjust to
1856 // maintain a constant alignment.
1857 std::string fill_data(object->target()->code_fill(fill_len));
1858 Output_data_const* odc = new Output_data_const(fill_data, 1);
1859 this->input_sections_.push_back(Input_section(odc));
1860 }
1861 }
1862
27bc2bce
ILT
1863 this->set_current_data_size_for_child(aligned_offset_in_section
1864 + shdr.get_sh_size());
a2fb1b05 1865
ead1e424 1866 // We need to keep track of this section if we are already keeping
2fd32231
ILT
1867 // track of sections, or if we are relaxing. Also, if this is a
1868 // section which requires sorting, or which may require sorting in
1869 // the future, we keep track of the sections. FIXME: Add test for
ead1e424 1870 // relaxing.
2fd32231
ILT
1871 if (have_sections_script
1872 || !this->input_sections_.empty()
1873 || this->may_sort_attached_input_sections()
7d9e3d98
ILT
1874 || this->must_sort_attached_input_sections()
1875 || parameters->options().user_set_Map())
ead1e424
ILT
1876 this->input_sections_.push_back(Input_section(object, shndx,
1877 shdr.get_sh_size(),
1878 addralign));
54dc6425 1879
c51e6221 1880 return aligned_offset_in_section;
61ba1cf9
ILT
1881}
1882
ead1e424
ILT
1883// Add arbitrary data to an output section.
1884
1885void
1886Output_section::add_output_section_data(Output_section_data* posd)
1887{
b8e6aad9
ILT
1888 Input_section inp(posd);
1889 this->add_output_section_data(&inp);
a445fddf
ILT
1890
1891 if (posd->is_data_size_valid())
1892 {
1893 off_t offset_in_section = this->current_data_size_for_child();
1894 off_t aligned_offset_in_section = align_address(offset_in_section,
1895 posd->addralign());
1896 this->set_current_data_size_for_child(aligned_offset_in_section
1897 + posd->data_size());
1898 }
b8e6aad9
ILT
1899}
1900
1901// Add arbitrary data to an output section by Input_section.
c06b7b0b 1902
b8e6aad9
ILT
1903void
1904Output_section::add_output_section_data(Input_section* inp)
1905{
ead1e424 1906 if (this->input_sections_.empty())
27bc2bce 1907 this->first_input_offset_ = this->current_data_size_for_child();
c06b7b0b 1908
b8e6aad9 1909 this->input_sections_.push_back(*inp);
c06b7b0b 1910
b8e6aad9 1911 uint64_t addralign = inp->addralign();
ead1e424
ILT
1912 if (addralign > this->addralign_)
1913 this->addralign_ = addralign;
c06b7b0b 1914
b8e6aad9
ILT
1915 inp->set_output_section(this);
1916}
1917
1918// Add a merge section to an output section.
1919
1920void
1921Output_section::add_output_merge_section(Output_section_data* posd,
1922 bool is_string, uint64_t entsize)
1923{
1924 Input_section inp(posd, is_string, entsize);
1925 this->add_output_section_data(&inp);
1926}
1927
1928// Add an input section to a SHF_MERGE section.
1929
1930bool
1931Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1932 uint64_t flags, uint64_t entsize,
96803768 1933 uint64_t addralign)
b8e6aad9 1934{
87f95776
ILT
1935 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1936
1937 // We only merge strings if the alignment is not more than the
1938 // character size. This could be handled, but it's unusual.
1939 if (is_string && addralign > entsize)
b8e6aad9
ILT
1940 return false;
1941
b8e6aad9
ILT
1942 Input_section_list::iterator p;
1943 for (p = this->input_sections_.begin();
1944 p != this->input_sections_.end();
1945 ++p)
87f95776 1946 if (p->is_merge_section(is_string, entsize, addralign))
9a0910c3
ILT
1947 {
1948 p->add_input_section(object, shndx);
1949 return true;
1950 }
b8e6aad9
ILT
1951
1952 // We handle the actual constant merging in Output_merge_data or
1953 // Output_merge_string_data.
9a0910c3
ILT
1954 Output_section_data* posd;
1955 if (!is_string)
1956 posd = new Output_merge_data(entsize, addralign);
b8e6aad9
ILT
1957 else
1958 {
9a0910c3
ILT
1959 switch (entsize)
1960 {
1961 case 1:
1962 posd = new Output_merge_string<char>(addralign);
1963 break;
1964 case 2:
1965 posd = new Output_merge_string<uint16_t>(addralign);
1966 break;
1967 case 4:
1968 posd = new Output_merge_string<uint32_t>(addralign);
1969 break;
1970 default:
1971 return false;
1972 }
b8e6aad9
ILT
1973 }
1974
9a0910c3
ILT
1975 this->add_output_merge_section(posd, is_string, entsize);
1976 posd->add_input_section(object, shndx);
1977
b8e6aad9
ILT
1978 return true;
1979}
1980
730cdc88
ILT
1981// Given an address OFFSET relative to the start of input section
1982// SHNDX in OBJECT, return whether this address is being included in
1983// the final link. This should only be called if SHNDX in OBJECT has
1984// a special mapping.
1985
1986bool
1987Output_section::is_input_address_mapped(const Relobj* object,
1988 unsigned int shndx,
1989 off_t offset) const
1990{
730cdc88
ILT
1991 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1992 p != this->input_sections_.end();
1993 ++p)
1994 {
8383303e 1995 section_offset_type output_offset;
730cdc88
ILT
1996 if (p->output_offset(object, shndx, offset, &output_offset))
1997 return output_offset != -1;
1998 }
1999
2000 // By default we assume that the address is mapped. This should
2001 // only be called after we have passed all sections to Layout. At
2002 // that point we should know what we are discarding.
2003 return true;
2004}
2005
2006// Given an address OFFSET relative to the start of input section
2007// SHNDX in object OBJECT, return the output offset relative to the
1e983657
ILT
2008// start of the input section in the output section. This should only
2009// be called if SHNDX in OBJECT has a special mapping.
730cdc88 2010
8383303e 2011section_offset_type
730cdc88 2012Output_section::output_offset(const Relobj* object, unsigned int shndx,
8383303e 2013 section_offset_type offset) const
730cdc88 2014{
730cdc88
ILT
2015 // This can only be called meaningfully when layout is complete.
2016 gold_assert(Output_data::is_layout_complete());
2017
2018 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2019 p != this->input_sections_.end();
2020 ++p)
2021 {
8383303e 2022 section_offset_type output_offset;
730cdc88
ILT
2023 if (p->output_offset(object, shndx, offset, &output_offset))
2024 return output_offset;
2025 }
2026 gold_unreachable();
2027}
2028
b8e6aad9
ILT
2029// Return the output virtual address of OFFSET relative to the start
2030// of input section SHNDX in object OBJECT.
2031
2032uint64_t
2033Output_section::output_address(const Relobj* object, unsigned int shndx,
2034 off_t offset) const
2035{
2036 uint64_t addr = this->address() + this->first_input_offset_;
2037 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2038 p != this->input_sections_.end();
2039 ++p)
2040 {
2041 addr = align_address(addr, p->addralign());
8383303e 2042 section_offset_type output_offset;
730cdc88
ILT
2043 if (p->output_offset(object, shndx, offset, &output_offset))
2044 {
2045 if (output_offset == -1)
2046 return -1U;
2047 return addr + output_offset;
2048 }
b8e6aad9
ILT
2049 addr += p->data_size();
2050 }
2051
2052 // If we get here, it means that we don't know the mapping for this
2053 // input section. This might happen in principle if
2054 // add_input_section were called before add_output_section_data.
2055 // But it should never actually happen.
2056
2057 gold_unreachable();
ead1e424
ILT
2058}
2059
a9a60db6
ILT
2060// Return the output address of the start of the merged section for
2061// input section SHNDX in object OBJECT.
2062
2063uint64_t
2064Output_section::starting_output_address(const Relobj* object,
2065 unsigned int shndx) const
2066{
a9a60db6
ILT
2067 uint64_t addr = this->address() + this->first_input_offset_;
2068 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2069 p != this->input_sections_.end();
2070 ++p)
2071 {
2072 addr = align_address(addr, p->addralign());
2073
2074 // It would be nice if we could use the existing output_offset
2075 // method to get the output offset of input offset 0.
2076 // Unfortunately we don't know for sure that input offset 0 is
2077 // mapped at all.
2078 if (p->is_merge_section_for(object, shndx))
2079 return addr;
2080
2081 addr += p->data_size();
2082 }
2083 gold_unreachable();
2084}
2085
27bc2bce 2086// Set the data size of an Output_section. This is where we handle
ead1e424
ILT
2087// setting the addresses of any Output_section_data objects.
2088
2089void
27bc2bce 2090Output_section::set_final_data_size()
ead1e424
ILT
2091{
2092 if (this->input_sections_.empty())
27bc2bce
ILT
2093 {
2094 this->set_data_size(this->current_data_size_for_child());
2095 return;
2096 }
ead1e424 2097
2fd32231
ILT
2098 if (this->must_sort_attached_input_sections())
2099 this->sort_attached_input_sections();
2100
27bc2bce
ILT
2101 uint64_t address = this->address();
2102 off_t startoff = this->offset();
ead1e424
ILT
2103 off_t off = startoff + this->first_input_offset_;
2104 for (Input_section_list::iterator p = this->input_sections_.begin();
2105 p != this->input_sections_.end();
2106 ++p)
2107 {
2108 off = align_address(off, p->addralign());
96803768
ILT
2109 p->set_address_and_file_offset(address + (off - startoff), off,
2110 startoff);
ead1e424
ILT
2111 off += p->data_size();
2112 }
2113
2114 this->set_data_size(off - startoff);
2115}
9a0910c3 2116
a445fddf
ILT
2117// Reset the address and file offset.
2118
2119void
2120Output_section::do_reset_address_and_file_offset()
2121{
2122 for (Input_section_list::iterator p = this->input_sections_.begin();
2123 p != this->input_sections_.end();
2124 ++p)
2125 p->reset_address_and_file_offset();
2126}
2127
7bf1f802
ILT
2128// Set the TLS offset. Called only for SHT_TLS sections.
2129
2130void
2131Output_section::do_set_tls_offset(uint64_t tls_base)
2132{
2133 this->tls_offset_ = this->address() - tls_base;
2134}
2135
2fd32231
ILT
2136// In a few cases we need to sort the input sections attached to an
2137// output section. This is used to implement the type of constructor
2138// priority ordering implemented by the GNU linker, in which the
2139// priority becomes part of the section name and the sections are
2140// sorted by name. We only do this for an output section if we see an
2141// attached input section matching ".ctor.*", ".dtor.*",
2142// ".init_array.*" or ".fini_array.*".
2143
2144class Output_section::Input_section_sort_entry
2145{
2146 public:
2147 Input_section_sort_entry()
2148 : input_section_(), index_(-1U), section_has_name_(false),
2149 section_name_()
2150 { }
2151
2152 Input_section_sort_entry(const Input_section& input_section,
2153 unsigned int index)
2154 : input_section_(input_section), index_(index),
2155 section_has_name_(input_section.is_input_section())
2156 {
2157 if (this->section_has_name_)
2158 {
2159 // This is only called single-threaded from Layout::finalize,
2160 // so it is OK to lock. Unfortunately we have no way to pass
2161 // in a Task token.
2162 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2163 Object* obj = input_section.relobj();
2164 Task_lock_obj<Object> tl(dummy_task, obj);
2165
2166 // This is a slow operation, which should be cached in
2167 // Layout::layout if this becomes a speed problem.
2168 this->section_name_ = obj->section_name(input_section.shndx());
2169 }
2170 }
2171
2172 // Return the Input_section.
2173 const Input_section&
2174 input_section() const
2175 {
2176 gold_assert(this->index_ != -1U);
2177 return this->input_section_;
2178 }
2179
2180 // The index of this entry in the original list. This is used to
2181 // make the sort stable.
2182 unsigned int
2183 index() const
2184 {
2185 gold_assert(this->index_ != -1U);
2186 return this->index_;
2187 }
2188
2189 // Whether there is a section name.
2190 bool
2191 section_has_name() const
2192 { return this->section_has_name_; }
2193
2194 // The section name.
2195 const std::string&
2196 section_name() const
2197 {
2198 gold_assert(this->section_has_name_);
2199 return this->section_name_;
2200 }
2201
ab794b6b
ILT
2202 // Return true if the section name has a priority. This is assumed
2203 // to be true if it has a dot after the initial dot.
2fd32231 2204 bool
ab794b6b 2205 has_priority() const
2fd32231
ILT
2206 {
2207 gold_assert(this->section_has_name_);
ab794b6b 2208 return this->section_name_.find('.', 1);
2fd32231
ILT
2209 }
2210
ab794b6b
ILT
2211 // Return true if this an input file whose base name matches
2212 // FILE_NAME. The base name must have an extension of ".o", and
2213 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2214 // This is to match crtbegin.o as well as crtbeginS.o without
2215 // getting confused by other possibilities. Overall matching the
2216 // file name this way is a dreadful hack, but the GNU linker does it
2217 // in order to better support gcc, and we need to be compatible.
2fd32231 2218 bool
ab794b6b 2219 match_file_name(const char* match_file_name) const
2fd32231 2220 {
2fd32231
ILT
2221 const std::string& file_name(this->input_section_.relobj()->name());
2222 const char* base_name = lbasename(file_name.c_str());
2223 size_t match_len = strlen(match_file_name);
2224 if (strncmp(base_name, match_file_name, match_len) != 0)
2225 return false;
2226 size_t base_len = strlen(base_name);
2227 if (base_len != match_len + 2 && base_len != match_len + 3)
2228 return false;
2229 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2230 }
2231
2232 private:
2233 // The Input_section we are sorting.
2234 Input_section input_section_;
2235 // The index of this Input_section in the original list.
2236 unsigned int index_;
2237 // Whether this Input_section has a section name--it won't if this
2238 // is some random Output_section_data.
2239 bool section_has_name_;
2240 // The section name if there is one.
2241 std::string section_name_;
2242};
2243
2244// Return true if S1 should come before S2 in the output section.
2245
2246bool
2247Output_section::Input_section_sort_compare::operator()(
2248 const Output_section::Input_section_sort_entry& s1,
2249 const Output_section::Input_section_sort_entry& s2) const
2250{
ab794b6b
ILT
2251 // crtbegin.o must come first.
2252 bool s1_begin = s1.match_file_name("crtbegin");
2253 bool s2_begin = s2.match_file_name("crtbegin");
2fd32231
ILT
2254 if (s1_begin || s2_begin)
2255 {
2256 if (!s1_begin)
2257 return false;
2258 if (!s2_begin)
2259 return true;
2260 return s1.index() < s2.index();
2261 }
2262
ab794b6b
ILT
2263 // crtend.o must come last.
2264 bool s1_end = s1.match_file_name("crtend");
2265 bool s2_end = s2.match_file_name("crtend");
2fd32231
ILT
2266 if (s1_end || s2_end)
2267 {
2268 if (!s1_end)
2269 return true;
2270 if (!s2_end)
2271 return false;
2272 return s1.index() < s2.index();
2273 }
2274
ab794b6b
ILT
2275 // We sort all the sections with no names to the end.
2276 if (!s1.section_has_name() || !s2.section_has_name())
2277 {
2278 if (s1.section_has_name())
2279 return true;
2280 if (s2.section_has_name())
2281 return false;
2282 return s1.index() < s2.index();
2283 }
2fd32231 2284
ab794b6b
ILT
2285 // A section with a priority follows a section without a priority.
2286 // The GNU linker does this for all but .init_array sections; until
2287 // further notice we'll assume that that is an mistake.
2288 bool s1_has_priority = s1.has_priority();
2289 bool s2_has_priority = s2.has_priority();
2290 if (s1_has_priority && !s2_has_priority)
2fd32231 2291 return false;
ab794b6b 2292 if (!s1_has_priority && s2_has_priority)
2fd32231
ILT
2293 return true;
2294
2295 // Otherwise we sort by name.
2296 int compare = s1.section_name().compare(s2.section_name());
2297 if (compare != 0)
2298 return compare < 0;
2299
2300 // Otherwise we keep the input order.
2301 return s1.index() < s2.index();
2302}
2303
2304// Sort the input sections attached to an output section.
2305
2306void
2307Output_section::sort_attached_input_sections()
2308{
2309 if (this->attached_input_sections_are_sorted_)
2310 return;
2311
2312 // The only thing we know about an input section is the object and
2313 // the section index. We need the section name. Recomputing this
2314 // is slow but this is an unusual case. If this becomes a speed
2315 // problem we can cache the names as required in Layout::layout.
2316
2317 // We start by building a larger vector holding a copy of each
2318 // Input_section, plus its current index in the list and its name.
2319 std::vector<Input_section_sort_entry> sort_list;
2320
2321 unsigned int i = 0;
2322 for (Input_section_list::iterator p = this->input_sections_.begin();
2323 p != this->input_sections_.end();
2324 ++p, ++i)
2325 sort_list.push_back(Input_section_sort_entry(*p, i));
2326
2327 // Sort the input sections.
2328 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2329
2330 // Copy the sorted input sections back to our list.
2331 this->input_sections_.clear();
2332 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2333 p != sort_list.end();
2334 ++p)
2335 this->input_sections_.push_back(p->input_section());
2336
2337 // Remember that we sorted the input sections, since we might get
2338 // called again.
2339 this->attached_input_sections_are_sorted_ = true;
2340}
2341
61ba1cf9
ILT
2342// Write the section header to *OSHDR.
2343
2344template<int size, bool big_endian>
2345void
16649710
ILT
2346Output_section::write_header(const Layout* layout,
2347 const Stringpool* secnamepool,
61ba1cf9
ILT
2348 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2349{
2350 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2351 oshdr->put_sh_type(this->type_);
6a74a719
ILT
2352
2353 elfcpp::Elf_Xword flags = this->flags_;
755ab8af 2354 if (this->info_section_ != NULL && this->info_uses_section_index_)
6a74a719
ILT
2355 flags |= elfcpp::SHF_INFO_LINK;
2356 oshdr->put_sh_flags(flags);
2357
61ba1cf9
ILT
2358 oshdr->put_sh_addr(this->address());
2359 oshdr->put_sh_offset(this->offset());
2360 oshdr->put_sh_size(this->data_size());
16649710
ILT
2361 if (this->link_section_ != NULL)
2362 oshdr->put_sh_link(this->link_section_->out_shndx());
2363 else if (this->should_link_to_symtab_)
2364 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2365 else if (this->should_link_to_dynsym_)
2366 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2367 else
2368 oshdr->put_sh_link(this->link_);
755ab8af
ILT
2369
2370 elfcpp::Elf_Word info;
16649710 2371 if (this->info_section_ != NULL)
755ab8af
ILT
2372 {
2373 if (this->info_uses_section_index_)
2374 info = this->info_section_->out_shndx();
2375 else
2376 info = this->info_section_->symtab_index();
2377 }
6a74a719 2378 else if (this->info_symndx_ != NULL)
755ab8af 2379 info = this->info_symndx_->symtab_index();
16649710 2380 else
755ab8af
ILT
2381 info = this->info_;
2382 oshdr->put_sh_info(info);
2383
61ba1cf9
ILT
2384 oshdr->put_sh_addralign(this->addralign_);
2385 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
2386}
2387
ead1e424
ILT
2388// Write out the data. For input sections the data is written out by
2389// Object::relocate, but we have to handle Output_section_data objects
2390// here.
2391
2392void
2393Output_section::do_write(Output_file* of)
2394{
96803768
ILT
2395 gold_assert(!this->requires_postprocessing());
2396
c51e6221
ILT
2397 off_t output_section_file_offset = this->offset();
2398 for (Fill_list::iterator p = this->fills_.begin();
2399 p != this->fills_.end();
2400 ++p)
2401 {
8851ecca 2402 std::string fill_data(parameters->target().code_fill(p->length()));
c51e6221 2403 of->write(output_section_file_offset + p->section_offset(),
a445fddf 2404 fill_data.data(), fill_data.size());
c51e6221
ILT
2405 }
2406
ead1e424
ILT
2407 for (Input_section_list::iterator p = this->input_sections_.begin();
2408 p != this->input_sections_.end();
2409 ++p)
2410 p->write(of);
2411}
2412
96803768
ILT
2413// If a section requires postprocessing, create the buffer to use.
2414
2415void
2416Output_section::create_postprocessing_buffer()
2417{
2418 gold_assert(this->requires_postprocessing());
1bedcac5
ILT
2419
2420 if (this->postprocessing_buffer_ != NULL)
2421 return;
96803768
ILT
2422
2423 if (!this->input_sections_.empty())
2424 {
2425 off_t off = this->first_input_offset_;
2426 for (Input_section_list::iterator p = this->input_sections_.begin();
2427 p != this->input_sections_.end();
2428 ++p)
2429 {
2430 off = align_address(off, p->addralign());
2431 p->finalize_data_size();
2432 off += p->data_size();
2433 }
2434 this->set_current_data_size_for_child(off);
2435 }
2436
2437 off_t buffer_size = this->current_data_size_for_child();
2438 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2439}
2440
2441// Write all the data of an Output_section into the postprocessing
2442// buffer. This is used for sections which require postprocessing,
2443// such as compression. Input sections are handled by
2444// Object::Relocate.
2445
2446void
2447Output_section::write_to_postprocessing_buffer()
2448{
2449 gold_assert(this->requires_postprocessing());
2450
96803768
ILT
2451 unsigned char* buffer = this->postprocessing_buffer();
2452 for (Fill_list::iterator p = this->fills_.begin();
2453 p != this->fills_.end();
2454 ++p)
2455 {
8851ecca 2456 std::string fill_data(parameters->target().code_fill(p->length()));
a445fddf
ILT
2457 memcpy(buffer + p->section_offset(), fill_data.data(),
2458 fill_data.size());
96803768
ILT
2459 }
2460
2461 off_t off = this->first_input_offset_;
2462 for (Input_section_list::iterator p = this->input_sections_.begin();
2463 p != this->input_sections_.end();
2464 ++p)
2465 {
2466 off = align_address(off, p->addralign());
2467 p->write_to_buffer(buffer + off);
2468 off += p->data_size();
2469 }
2470}
2471
a445fddf
ILT
2472// Get the input sections for linker script processing. We leave
2473// behind the Output_section_data entries. Note that this may be
2474// slightly incorrect for merge sections. We will leave them behind,
2475// but it is possible that the script says that they should follow
2476// some other input sections, as in:
2477// .rodata { *(.rodata) *(.rodata.cst*) }
2478// For that matter, we don't handle this correctly:
2479// .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2480// With luck this will never matter.
2481
2482uint64_t
2483Output_section::get_input_sections(
2484 uint64_t address,
2485 const std::string& fill,
2486 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2487{
2488 uint64_t orig_address = address;
2489
2490 address = align_address(address, this->addralign());
2491
2492 Input_section_list remaining;
2493 for (Input_section_list::iterator p = this->input_sections_.begin();
2494 p != this->input_sections_.end();
2495 ++p)
2496 {
2497 if (p->is_input_section())
2498 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2499 else
2500 {
2501 uint64_t aligned_address = align_address(address, p->addralign());
2502 if (aligned_address != address && !fill.empty())
2503 {
2504 section_size_type length =
2505 convert_to_section_size_type(aligned_address - address);
2506 std::string this_fill;
2507 this_fill.reserve(length);
2508 while (this_fill.length() + fill.length() <= length)
2509 this_fill += fill;
2510 if (this_fill.length() < length)
2511 this_fill.append(fill, 0, length - this_fill.length());
2512
2513 Output_section_data* posd = new Output_data_const(this_fill, 0);
2514 remaining.push_back(Input_section(posd));
2515 }
2516 address = aligned_address;
2517
2518 remaining.push_back(*p);
2519
2520 p->finalize_data_size();
2521 address += p->data_size();
2522 }
2523 }
2524
2525 this->input_sections_.swap(remaining);
2526 this->first_input_offset_ = 0;
2527
2528 uint64_t data_size = address - orig_address;
2529 this->set_current_data_size_for_child(data_size);
2530 return data_size;
2531}
2532
2533// Add an input section from a script.
2534
2535void
2536Output_section::add_input_section_for_script(Relobj* object,
2537 unsigned int shndx,
2538 off_t data_size,
2539 uint64_t addralign)
2540{
2541 if (addralign > this->addralign_)
2542 this->addralign_ = addralign;
2543
2544 off_t offset_in_section = this->current_data_size_for_child();
2545 off_t aligned_offset_in_section = align_address(offset_in_section,
2546 addralign);
2547
2548 this->set_current_data_size_for_child(aligned_offset_in_section
2549 + data_size);
2550
2551 this->input_sections_.push_back(Input_section(object, shndx,
2552 data_size, addralign));
2553}
2554
7d9e3d98
ILT
2555// Print to the map file.
2556
2557void
2558Output_section::do_print_to_mapfile(Mapfile* mapfile) const
2559{
2560 mapfile->print_output_section(this);
2561
2562 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2563 p != this->input_sections_.end();
2564 ++p)
2565 p->print_to_mapfile(mapfile);
2566}
2567
38c5e8b4
ILT
2568// Print stats for merge sections to stderr.
2569
2570void
2571Output_section::print_merge_stats()
2572{
2573 Input_section_list::iterator p;
2574 for (p = this->input_sections_.begin();
2575 p != this->input_sections_.end();
2576 ++p)
2577 p->print_merge_stats(this->name_);
2578}
2579
a2fb1b05
ILT
2580// Output segment methods.
2581
2582Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
54dc6425 2583 : output_data_(),
75f65a3e 2584 output_bss_(),
a2fb1b05
ILT
2585 vaddr_(0),
2586 paddr_(0),
2587 memsz_(0),
a445fddf
ILT
2588 max_align_(0),
2589 min_p_align_(0),
a2fb1b05
ILT
2590 offset_(0),
2591 filesz_(0),
2592 type_(type),
ead1e424 2593 flags_(flags),
a445fddf
ILT
2594 is_max_align_known_(false),
2595 are_addresses_set_(false)
a2fb1b05
ILT
2596{
2597}
2598
2599// Add an Output_section to an Output_segment.
2600
2601void
75f65a3e 2602Output_segment::add_output_section(Output_section* os,
01676dcd 2603 elfcpp::Elf_Word seg_flags)
a2fb1b05 2604{
a3ad94ed 2605 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
a445fddf 2606 gold_assert(!this->is_max_align_known_);
75f65a3e 2607
ead1e424 2608 // Update the segment flags.
75f65a3e 2609 this->flags_ |= seg_flags;
75f65a3e
ILT
2610
2611 Output_segment::Output_data_list* pdl;
2612 if (os->type() == elfcpp::SHT_NOBITS)
2613 pdl = &this->output_bss_;
2614 else
2615 pdl = &this->output_data_;
54dc6425 2616
a2fb1b05
ILT
2617 // So that PT_NOTE segments will work correctly, we need to ensure
2618 // that all SHT_NOTE sections are adjacent. This will normally
2619 // happen automatically, because all the SHT_NOTE input sections
2620 // will wind up in the same output section. However, it is possible
2621 // for multiple SHT_NOTE input sections to have different section
2622 // flags, and thus be in different output sections, but for the
2623 // different section flags to map into the same segment flags and
2624 // thus the same output segment.
54dc6425
ILT
2625
2626 // Note that while there may be many input sections in an output
2627 // section, there are normally only a few output sections in an
2628 // output segment. This loop is expected to be fast.
2629
61ba1cf9 2630 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
a2fb1b05 2631 {
a3ad94ed 2632 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 2633 do
54dc6425 2634 {
75f65a3e 2635 --p;
54dc6425
ILT
2636 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2637 {
2638 ++p;
75f65a3e 2639 pdl->insert(p, os);
54dc6425
ILT
2640 return;
2641 }
2642 }
75f65a3e 2643 while (p != pdl->begin());
54dc6425
ILT
2644 }
2645
2646 // Similarly, so that PT_TLS segments will work, we need to group
75f65a3e
ILT
2647 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2648 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2649 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
07f397ab
ILT
2650 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2651 // and the PT_TLS segment -- we do this grouping only for the
2652 // PT_LOAD segment.
2653 if (this->type_ != elfcpp::PT_TLS
2d924fd9 2654 && (os->flags() & elfcpp::SHF_TLS) != 0)
54dc6425 2655 {
75f65a3e
ILT
2656 pdl = &this->output_data_;
2657 bool nobits = os->type() == elfcpp::SHT_NOBITS;
ead1e424 2658 bool sawtls = false;
a3ad94ed 2659 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 2660 do
a2fb1b05 2661 {
75f65a3e 2662 --p;
ead1e424
ILT
2663 bool insert;
2664 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2665 {
2666 sawtls = true;
2667 // Put a NOBITS section after the first TLS section.
9f1d377b 2668 // Put a PROGBITS section after the first TLS/PROGBITS
ead1e424
ILT
2669 // section.
2670 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2671 }
2672 else
2673 {
2674 // If we've gone past the TLS sections, but we've seen a
2675 // TLS section, then we need to insert this section now.
2676 insert = sawtls;
2677 }
2678
2679 if (insert)
a2fb1b05
ILT
2680 {
2681 ++p;
75f65a3e 2682 pdl->insert(p, os);
a2fb1b05
ILT
2683 return;
2684 }
2685 }
75f65a3e 2686 while (p != pdl->begin());
ead1e424 2687
dbe717ef
ILT
2688 // There are no TLS sections yet; put this one at the requested
2689 // location in the section list.
a2fb1b05
ILT
2690 }
2691
9f1d377b
ILT
2692 // For the PT_GNU_RELRO segment, we need to group relro sections,
2693 // and we need to put them before any non-relro sections. Also,
2694 // relro local sections go before relro non-local sections.
2695 if (parameters->options().relro() && os->is_relro())
2696 {
2697 gold_assert(pdl == &this->output_data_);
2698 Output_segment::Output_data_list::iterator p;
2699 for (p = pdl->begin(); p != pdl->end(); ++p)
2700 {
2701 if (!(*p)->is_section())
2702 break;
2703
2704 Output_section* pos = (*p)->output_section();
2705 if (!pos->is_relro()
2706 || (os->is_relro_local() && !pos->is_relro_local()))
2707 break;
2708 }
2709
2710 pdl->insert(p, os);
2711 return;
2712 }
2713
01676dcd 2714 pdl->push_back(os);
75f65a3e
ILT
2715}
2716
1650c4ff
ILT
2717// Remove an Output_section from this segment. It is an error if it
2718// is not present.
2719
2720void
2721Output_segment::remove_output_section(Output_section* os)
2722{
2723 // We only need this for SHT_PROGBITS.
2724 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2725 for (Output_data_list::iterator p = this->output_data_.begin();
2726 p != this->output_data_.end();
2727 ++p)
2728 {
2729 if (*p == os)
2730 {
2731 this->output_data_.erase(p);
2732 return;
2733 }
2734 }
2735 gold_unreachable();
2736}
2737
75f65a3e
ILT
2738// Add an Output_data (which is not an Output_section) to the start of
2739// a segment.
2740
2741void
2742Output_segment::add_initial_output_data(Output_data* od)
2743{
a445fddf 2744 gold_assert(!this->is_max_align_known_);
75f65a3e
ILT
2745 this->output_data_.push_front(od);
2746}
2747
9f1d377b
ILT
2748// Return whether the first data section is a relro section.
2749
2750bool
2751Output_segment::is_first_section_relro() const
2752{
2753 return (!this->output_data_.empty()
2754 && this->output_data_.front()->is_section()
2755 && this->output_data_.front()->output_section()->is_relro());
2756}
2757
75f65a3e 2758// Return the maximum alignment of the Output_data in Output_segment.
75f65a3e
ILT
2759
2760uint64_t
a445fddf 2761Output_segment::maximum_alignment()
75f65a3e 2762{
a445fddf 2763 if (!this->is_max_align_known_)
ead1e424
ILT
2764 {
2765 uint64_t addralign;
2766
a445fddf
ILT
2767 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2768 if (addralign > this->max_align_)
2769 this->max_align_ = addralign;
ead1e424 2770
a445fddf
ILT
2771 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2772 if (addralign > this->max_align_)
2773 this->max_align_ = addralign;
ead1e424 2774
9f1d377b
ILT
2775 // If -z relro is in effect, and the first section in this
2776 // segment is a relro section, then the segment must be aligned
2777 // to at least the common page size. This ensures that the
2778 // PT_GNU_RELRO segment will start at a page boundary.
2d924fd9
ILT
2779 if (this->type_ == elfcpp::PT_LOAD
2780 && parameters->options().relro()
2781 && this->is_first_section_relro())
9f1d377b
ILT
2782 {
2783 addralign = parameters->target().common_pagesize();
2784 if (addralign > this->max_align_)
2785 this->max_align_ = addralign;
2786 }
2787
a445fddf 2788 this->is_max_align_known_ = true;
ead1e424
ILT
2789 }
2790
a445fddf 2791 return this->max_align_;
75f65a3e
ILT
2792}
2793
ead1e424
ILT
2794// Return the maximum alignment of a list of Output_data.
2795
2796uint64_t
a445fddf 2797Output_segment::maximum_alignment_list(const Output_data_list* pdl)
ead1e424
ILT
2798{
2799 uint64_t ret = 0;
2800 for (Output_data_list::const_iterator p = pdl->begin();
2801 p != pdl->end();
2802 ++p)
2803 {
2804 uint64_t addralign = (*p)->addralign();
2805 if (addralign > ret)
2806 ret = addralign;
2807 }
2808 return ret;
2809}
2810
4f4c5f80
ILT
2811// Return the number of dynamic relocs applied to this segment.
2812
2813unsigned int
2814Output_segment::dynamic_reloc_count() const
2815{
2816 return (this->dynamic_reloc_count_list(&this->output_data_)
2817 + this->dynamic_reloc_count_list(&this->output_bss_));
2818}
2819
2820// Return the number of dynamic relocs applied to an Output_data_list.
2821
2822unsigned int
2823Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2824{
2825 unsigned int count = 0;
2826 for (Output_data_list::const_iterator p = pdl->begin();
2827 p != pdl->end();
2828 ++p)
2829 count += (*p)->dynamic_reloc_count();
2830 return count;
2831}
2832
a445fddf
ILT
2833// Set the section addresses for an Output_segment. If RESET is true,
2834// reset the addresses first. ADDR is the address and *POFF is the
2835// file offset. Set the section indexes starting with *PSHNDX.
2836// Return the address of the immediately following segment. Update
2837// *POFF and *PSHNDX.
75f65a3e
ILT
2838
2839uint64_t
96a2b4e4
ILT
2840Output_segment::set_section_addresses(const Layout* layout, bool reset,
2841 uint64_t addr, off_t* poff,
ead1e424 2842 unsigned int* pshndx)
75f65a3e 2843{
a3ad94ed 2844 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e 2845
a445fddf
ILT
2846 if (!reset && this->are_addresses_set_)
2847 {
2848 gold_assert(this->paddr_ == addr);
2849 addr = this->vaddr_;
2850 }
2851 else
2852 {
2853 this->vaddr_ = addr;
2854 this->paddr_ = addr;
2855 this->are_addresses_set_ = true;
2856 }
75f65a3e 2857
96a2b4e4
ILT
2858 bool in_tls = false;
2859
9f1d377b
ILT
2860 bool in_relro = (parameters->options().relro()
2861 && this->is_first_section_relro());
2862
75f65a3e
ILT
2863 off_t orig_off = *poff;
2864 this->offset_ = orig_off;
2865
96a2b4e4 2866 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
9f1d377b
ILT
2867 addr, poff, pshndx, &in_tls,
2868 &in_relro);
75f65a3e
ILT
2869 this->filesz_ = *poff - orig_off;
2870
2871 off_t off = *poff;
2872
96a2b4e4
ILT
2873 uint64_t ret = this->set_section_list_addresses(layout, reset,
2874 &this->output_bss_,
2875 addr, poff, pshndx,
9f1d377b 2876 &in_tls, &in_relro);
96a2b4e4
ILT
2877
2878 // If the last section was a TLS section, align upward to the
2879 // alignment of the TLS segment, so that the overall size of the TLS
2880 // segment is aligned.
2881 if (in_tls)
2882 {
2883 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2884 *poff = align_address(*poff, segment_align);
2885 }
2886
9f1d377b
ILT
2887 // If all the sections were relro sections, align upward to the
2888 // common page size.
2889 if (in_relro)
2890 {
2891 uint64_t page_align = parameters->target().common_pagesize();
2892 *poff = align_address(*poff, page_align);
2893 }
2894
75f65a3e
ILT
2895 this->memsz_ = *poff - orig_off;
2896
2897 // Ignore the file offset adjustments made by the BSS Output_data
2898 // objects.
2899 *poff = off;
61ba1cf9
ILT
2900
2901 return ret;
75f65a3e
ILT
2902}
2903
b8e6aad9
ILT
2904// Set the addresses and file offsets in a list of Output_data
2905// structures.
75f65a3e
ILT
2906
2907uint64_t
96a2b4e4
ILT
2908Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
2909 Output_data_list* pdl,
ead1e424 2910 uint64_t addr, off_t* poff,
96a2b4e4 2911 unsigned int* pshndx,
9f1d377b 2912 bool* in_tls, bool* in_relro)
75f65a3e 2913{
ead1e424 2914 off_t startoff = *poff;
75f65a3e 2915
ead1e424 2916 off_t off = startoff;
75f65a3e
ILT
2917 for (Output_data_list::iterator p = pdl->begin();
2918 p != pdl->end();
2919 ++p)
2920 {
a445fddf
ILT
2921 if (reset)
2922 (*p)->reset_address_and_file_offset();
2923
2924 // When using a linker script the section will most likely
2925 // already have an address.
2926 if (!(*p)->is_address_valid())
3802b2dd 2927 {
96a2b4e4
ILT
2928 uint64_t align = (*p)->addralign();
2929
2930 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2931 {
2932 // Give the first TLS section the alignment of the
2933 // entire TLS segment. Otherwise the TLS segment as a
2934 // whole may be misaligned.
2935 if (!*in_tls)
2936 {
2937 Output_segment* tls_segment = layout->tls_segment();
2938 gold_assert(tls_segment != NULL);
2939 uint64_t segment_align = tls_segment->maximum_alignment();
2940 gold_assert(segment_align >= align);
2941 align = segment_align;
2942
2943 *in_tls = true;
2944 }
2945 }
2946 else
2947 {
2948 // If this is the first section after the TLS segment,
2949 // align it to at least the alignment of the TLS
2950 // segment, so that the size of the overall TLS segment
2951 // is aligned.
2952 if (*in_tls)
2953 {
2954 uint64_t segment_align =
2955 layout->tls_segment()->maximum_alignment();
2956 if (segment_align > align)
2957 align = segment_align;
2958
2959 *in_tls = false;
2960 }
2961 }
2962
9f1d377b
ILT
2963 // If this is a non-relro section after a relro section,
2964 // align it to a common page boundary so that the dynamic
2965 // linker has a page to mark as read-only.
2966 if (*in_relro
2967 && (!(*p)->is_section()
2968 || !(*p)->output_section()->is_relro()))
2969 {
2970 uint64_t page_align = parameters->target().common_pagesize();
2971 if (page_align > align)
2972 align = page_align;
2973 *in_relro = false;
2974 }
2975
96a2b4e4 2976 off = align_address(off, align);
3802b2dd
ILT
2977 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2978 }
a445fddf
ILT
2979 else
2980 {
2981 // The script may have inserted a skip forward, but it
2982 // better not have moved backward.
3802b2dd
ILT
2983 gold_assert((*p)->address() >= addr + (off - startoff));
2984 off += (*p)->address() - (addr + (off - startoff));
a445fddf
ILT
2985 (*p)->set_file_offset(off);
2986 (*p)->finalize_data_size();
2987 }
ead1e424 2988
96a2b4e4
ILT
2989 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2990 // section. Such a section does not affect the size of a
2991 // PT_LOAD segment.
2992 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
ead1e424
ILT
2993 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2994 off += (*p)->data_size();
75f65a3e 2995
ead1e424
ILT
2996 if ((*p)->is_section())
2997 {
2998 (*p)->set_out_shndx(*pshndx);
2999 ++*pshndx;
3000 }
75f65a3e
ILT
3001 }
3002
3003 *poff = off;
ead1e424 3004 return addr + (off - startoff);
75f65a3e
ILT
3005}
3006
3007// For a non-PT_LOAD segment, set the offset from the sections, if
3008// any.
3009
3010void
3011Output_segment::set_offset()
3012{
a3ad94ed 3013 gold_assert(this->type_ != elfcpp::PT_LOAD);
75f65a3e 3014
a445fddf
ILT
3015 gold_assert(!this->are_addresses_set_);
3016
75f65a3e
ILT
3017 if (this->output_data_.empty() && this->output_bss_.empty())
3018 {
3019 this->vaddr_ = 0;
3020 this->paddr_ = 0;
a445fddf 3021 this->are_addresses_set_ = true;
75f65a3e 3022 this->memsz_ = 0;
a445fddf 3023 this->min_p_align_ = 0;
75f65a3e
ILT
3024 this->offset_ = 0;
3025 this->filesz_ = 0;
3026 return;
3027 }
3028
3029 const Output_data* first;
3030 if (this->output_data_.empty())
3031 first = this->output_bss_.front();
3032 else
3033 first = this->output_data_.front();
3034 this->vaddr_ = first->address();
a445fddf
ILT
3035 this->paddr_ = (first->has_load_address()
3036 ? first->load_address()
3037 : this->vaddr_);
3038 this->are_addresses_set_ = true;
75f65a3e
ILT
3039 this->offset_ = first->offset();
3040
3041 if (this->output_data_.empty())
3042 this->filesz_ = 0;
3043 else
3044 {
3045 const Output_data* last_data = this->output_data_.back();
3046 this->filesz_ = (last_data->address()
3047 + last_data->data_size()
3048 - this->vaddr_);
3049 }
3050
3051 const Output_data* last;
3052 if (this->output_bss_.empty())
3053 last = this->output_data_.back();
3054 else
3055 last = this->output_bss_.back();
3056 this->memsz_ = (last->address()
3057 + last->data_size()
3058 - this->vaddr_);
96a2b4e4
ILT
3059
3060 // If this is a TLS segment, align the memory size. The code in
3061 // set_section_list ensures that the section after the TLS segment
3062 // is aligned to give us room.
3063 if (this->type_ == elfcpp::PT_TLS)
3064 {
3065 uint64_t segment_align = this->maximum_alignment();
3066 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3067 this->memsz_ = align_address(this->memsz_, segment_align);
3068 }
9f1d377b
ILT
3069
3070 // If this is a RELRO segment, align the memory size. The code in
3071 // set_section_list ensures that the section after the RELRO segment
3072 // is aligned to give us room.
3073 if (this->type_ == elfcpp::PT_GNU_RELRO)
3074 {
3075 uint64_t page_align = parameters->target().common_pagesize();
3076 gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3077 this->memsz_ = align_address(this->memsz_, page_align);
3078 }
75f65a3e
ILT
3079}
3080
7bf1f802
ILT
3081// Set the TLS offsets of the sections in the PT_TLS segment.
3082
3083void
3084Output_segment::set_tls_offsets()
3085{
3086 gold_assert(this->type_ == elfcpp::PT_TLS);
3087
3088 for (Output_data_list::iterator p = this->output_data_.begin();
3089 p != this->output_data_.end();
3090 ++p)
3091 (*p)->set_tls_offset(this->vaddr_);
3092
3093 for (Output_data_list::iterator p = this->output_bss_.begin();
3094 p != this->output_bss_.end();
3095 ++p)
3096 (*p)->set_tls_offset(this->vaddr_);
3097}
3098
a445fddf
ILT
3099// Return the address of the first section.
3100
3101uint64_t
3102Output_segment::first_section_load_address() const
3103{
3104 for (Output_data_list::const_iterator p = this->output_data_.begin();
3105 p != this->output_data_.end();
3106 ++p)
3107 if ((*p)->is_section())
3108 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3109
3110 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3111 p != this->output_bss_.end();
3112 ++p)
3113 if ((*p)->is_section())
3114 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3115
3116 gold_unreachable();
3117}
3118
75f65a3e
ILT
3119// Return the number of Output_sections in an Output_segment.
3120
3121unsigned int
3122Output_segment::output_section_count() const
3123{
3124 return (this->output_section_count_list(&this->output_data_)
3125 + this->output_section_count_list(&this->output_bss_));
3126}
3127
3128// Return the number of Output_sections in an Output_data_list.
3129
3130unsigned int
3131Output_segment::output_section_count_list(const Output_data_list* pdl) const
3132{
3133 unsigned int count = 0;
3134 for (Output_data_list::const_iterator p = pdl->begin();
3135 p != pdl->end();
3136 ++p)
3137 {
3138 if ((*p)->is_section())
3139 ++count;
3140 }
3141 return count;
a2fb1b05
ILT
3142}
3143
1c4f3631
ILT
3144// Return the section attached to the list segment with the lowest
3145// load address. This is used when handling a PHDRS clause in a
3146// linker script.
3147
3148Output_section*
3149Output_segment::section_with_lowest_load_address() const
3150{
3151 Output_section* found = NULL;
3152 uint64_t found_lma = 0;
3153 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3154
3155 Output_section* found_data = found;
3156 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3157 if (found != found_data && found_data != NULL)
3158 {
3159 gold_error(_("nobits section %s may not precede progbits section %s "
3160 "in same segment"),
3161 found->name(), found_data->name());
3162 return NULL;
3163 }
3164
3165 return found;
3166}
3167
3168// Look through a list for a section with a lower load address.
3169
3170void
3171Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3172 Output_section** found,
3173 uint64_t* found_lma) const
3174{
3175 for (Output_data_list::const_iterator p = pdl->begin();
3176 p != pdl->end();
3177 ++p)
3178 {
3179 if (!(*p)->is_section())
3180 continue;
3181 Output_section* os = static_cast<Output_section*>(*p);
3182 uint64_t lma = (os->has_load_address()
3183 ? os->load_address()
3184 : os->address());
3185 if (*found == NULL || lma < *found_lma)
3186 {
3187 *found = os;
3188 *found_lma = lma;
3189 }
3190 }
3191}
3192
61ba1cf9
ILT
3193// Write the segment data into *OPHDR.
3194
3195template<int size, bool big_endian>
3196void
ead1e424 3197Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
61ba1cf9
ILT
3198{
3199 ophdr->put_p_type(this->type_);
3200 ophdr->put_p_offset(this->offset_);
3201 ophdr->put_p_vaddr(this->vaddr_);
3202 ophdr->put_p_paddr(this->paddr_);
3203 ophdr->put_p_filesz(this->filesz_);
3204 ophdr->put_p_memsz(this->memsz_);
3205 ophdr->put_p_flags(this->flags_);
a445fddf 3206 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
61ba1cf9
ILT
3207}
3208
3209// Write the section headers into V.
3210
3211template<int size, bool big_endian>
3212unsigned char*
16649710
ILT
3213Output_segment::write_section_headers(const Layout* layout,
3214 const Stringpool* secnamepool,
ead1e424 3215 unsigned char* v,
7d1a9ebb 3216 unsigned int *pshndx) const
5482377d 3217{
ead1e424
ILT
3218 // Every section that is attached to a segment must be attached to a
3219 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3220 // segments.
3221 if (this->type_ != elfcpp::PT_LOAD)
3222 return v;
3223
7d1a9ebb
ILT
3224 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3225 &this->output_data_,
3226 v, pshndx);
3227 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3228 &this->output_bss_,
3229 v, pshndx);
61ba1cf9
ILT
3230 return v;
3231}
3232
3233template<int size, bool big_endian>
3234unsigned char*
16649710
ILT
3235Output_segment::write_section_headers_list(const Layout* layout,
3236 const Stringpool* secnamepool,
61ba1cf9 3237 const Output_data_list* pdl,
ead1e424 3238 unsigned char* v,
7d1a9ebb 3239 unsigned int* pshndx) const
61ba1cf9
ILT
3240{
3241 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3242 for (Output_data_list::const_iterator p = pdl->begin();
3243 p != pdl->end();
3244 ++p)
3245 {
3246 if ((*p)->is_section())
3247 {
5482377d 3248 const Output_section* ps = static_cast<const Output_section*>(*p);
a3ad94ed 3249 gold_assert(*pshndx == ps->out_shndx());
61ba1cf9 3250 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 3251 ps->write_header(layout, secnamepool, &oshdr);
61ba1cf9 3252 v += shdr_size;
ead1e424 3253 ++*pshndx;
61ba1cf9
ILT
3254 }
3255 }
3256 return v;
3257}
3258
7d9e3d98
ILT
3259// Print the output sections to the map file.
3260
3261void
3262Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3263{
3264 if (this->type() != elfcpp::PT_LOAD)
3265 return;
3266 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3267 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3268}
3269
3270// Print an output section list to the map file.
3271
3272void
3273Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3274 const Output_data_list* pdl) const
3275{
3276 for (Output_data_list::const_iterator p = pdl->begin();
3277 p != pdl->end();
3278 ++p)
3279 (*p)->print_to_mapfile(mapfile);
3280}
3281
a2fb1b05
ILT
3282// Output_file methods.
3283
14144f39
ILT
3284Output_file::Output_file(const char* name)
3285 : name_(name),
61ba1cf9
ILT
3286 o_(-1),
3287 file_size_(0),
c420411f 3288 base_(NULL),
516cb3d0
ILT
3289 map_is_anonymous_(false),
3290 is_temporary_(false)
61ba1cf9
ILT
3291{
3292}
3293
3294// Open the output file.
3295
a2fb1b05 3296void
61ba1cf9 3297Output_file::open(off_t file_size)
a2fb1b05 3298{
61ba1cf9
ILT
3299 this->file_size_ = file_size;
3300
4e9d8586
ILT
3301 // Unlink the file first; otherwise the open() may fail if the file
3302 // is busy (e.g. it's an executable that's currently being executed).
3303 //
3304 // However, the linker may be part of a system where a zero-length
3305 // file is created for it to write to, with tight permissions (gcc
3306 // 2.95 did something like this). Unlinking the file would work
3307 // around those permission controls, so we only unlink if the file
3308 // has a non-zero size. We also unlink only regular files to avoid
3309 // trouble with directories/etc.
3310 //
3311 // If we fail, continue; this command is merely a best-effort attempt
3312 // to improve the odds for open().
3313
42a1b686 3314 // We let the name "-" mean "stdout"
516cb3d0 3315 if (!this->is_temporary_)
42a1b686 3316 {
516cb3d0
ILT
3317 if (strcmp(this->name_, "-") == 0)
3318 this->o_ = STDOUT_FILENO;
3319 else
3320 {
3321 struct stat s;
3322 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
3323 unlink_if_ordinary(this->name_);
3324
8851ecca 3325 int mode = parameters->options().relocatable() ? 0666 : 0777;
2a00e4fb
ILT
3326 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
3327 mode);
516cb3d0
ILT
3328 if (o < 0)
3329 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3330 this->o_ = o;
3331 }
42a1b686 3332 }
61ba1cf9 3333
27bc2bce
ILT
3334 this->map();
3335}
3336
3337// Resize the output file.
3338
3339void
3340Output_file::resize(off_t file_size)
3341{
c420411f
ILT
3342 // If the mmap is mapping an anonymous memory buffer, this is easy:
3343 // just mremap to the new size. If it's mapping to a file, we want
3344 // to unmap to flush to the file, then remap after growing the file.
3345 if (this->map_is_anonymous_)
3346 {
3347 void* base = ::mremap(this->base_, this->file_size_, file_size,
3348 MREMAP_MAYMOVE);
3349 if (base == MAP_FAILED)
3350 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3351 this->base_ = static_cast<unsigned char*>(base);
3352 this->file_size_ = file_size;
3353 }
3354 else
3355 {
3356 this->unmap();
3357 this->file_size_ = file_size;
3358 this->map();
3359 }
27bc2bce
ILT
3360}
3361
3362// Map the file into memory.
3363
3364void
3365Output_file::map()
3366{
c420411f 3367 const int o = this->o_;
61ba1cf9 3368
c420411f
ILT
3369 // If the output file is not a regular file, don't try to mmap it;
3370 // instead, we'll mmap a block of memory (an anonymous buffer), and
3371 // then later write the buffer to the file.
3372 void* base;
3373 struct stat statbuf;
42a1b686
ILT
3374 if (o == STDOUT_FILENO || o == STDERR_FILENO
3375 || ::fstat(o, &statbuf) != 0
516cb3d0
ILT
3376 || !S_ISREG(statbuf.st_mode)
3377 || this->is_temporary_)
c420411f
ILT
3378 {
3379 this->map_is_anonymous_ = true;
3380 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3381 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3382 }
3383 else
3384 {
3385 // Write out one byte to make the file the right size.
3386 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
3387 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
3388 char b = 0;
3389 if (::write(o, &b, 1) != 1)
3390 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
3391
3392 // Map the file into memory.
3393 this->map_is_anonymous_ = false;
3394 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3395 MAP_SHARED, o, 0);
3396 }
61ba1cf9 3397 if (base == MAP_FAILED)
75f2446e 3398 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
3399 this->base_ = static_cast<unsigned char*>(base);
3400}
3401
c420411f 3402// Unmap the file from memory.
61ba1cf9
ILT
3403
3404void
c420411f 3405Output_file::unmap()
61ba1cf9
ILT
3406{
3407 if (::munmap(this->base_, this->file_size_) < 0)
a0c4fb0a 3408 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
61ba1cf9 3409 this->base_ = NULL;
c420411f
ILT
3410}
3411
3412// Close the output file.
3413
3414void
3415Output_file::close()
3416{
3417 // If the map isn't file-backed, we need to write it now.
516cb3d0 3418 if (this->map_is_anonymous_ && !this->is_temporary_)
c420411f
ILT
3419 {
3420 size_t bytes_to_write = this->file_size_;
3421 while (bytes_to_write > 0)
3422 {
3423 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
3424 if (bytes_written == 0)
3425 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
3426 else if (bytes_written < 0)
3427 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
3428 else
3429 bytes_to_write -= bytes_written;
3430 }
3431 }
3432 this->unmap();
61ba1cf9 3433
42a1b686 3434 // We don't close stdout or stderr
516cb3d0
ILT
3435 if (this->o_ != STDOUT_FILENO
3436 && this->o_ != STDERR_FILENO
3437 && !this->is_temporary_)
42a1b686
ILT
3438 if (::close(this->o_) < 0)
3439 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
61ba1cf9 3440 this->o_ = -1;
a2fb1b05
ILT
3441}
3442
3443// Instantiate the templates we need. We could use the configure
3444// script to restrict this to only the ones for implemented targets.
3445
193a53d9 3446#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
3447template
3448off_t
3449Output_section::add_input_section<32, false>(
730cdc88 3450 Sized_relobj<32, false>* object,
ead1e424 3451 unsigned int shndx,
a2fb1b05 3452 const char* secname,
730cdc88 3453 const elfcpp::Shdr<32, false>& shdr,
a445fddf
ILT
3454 unsigned int reloc_shndx,
3455 bool have_sections_script);
193a53d9 3456#endif
a2fb1b05 3457
193a53d9 3458#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
3459template
3460off_t
3461Output_section::add_input_section<32, true>(
730cdc88 3462 Sized_relobj<32, true>* object,
ead1e424 3463 unsigned int shndx,
a2fb1b05 3464 const char* secname,
730cdc88 3465 const elfcpp::Shdr<32, true>& shdr,
a445fddf
ILT
3466 unsigned int reloc_shndx,
3467 bool have_sections_script);
193a53d9 3468#endif
a2fb1b05 3469
193a53d9 3470#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
3471template
3472off_t
3473Output_section::add_input_section<64, false>(
730cdc88 3474 Sized_relobj<64, false>* object,
ead1e424 3475 unsigned int shndx,
a2fb1b05 3476 const char* secname,
730cdc88 3477 const elfcpp::Shdr<64, false>& shdr,
a445fddf
ILT
3478 unsigned int reloc_shndx,
3479 bool have_sections_script);
193a53d9 3480#endif
a2fb1b05 3481
193a53d9 3482#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
3483template
3484off_t
3485Output_section::add_input_section<64, true>(
730cdc88 3486 Sized_relobj<64, true>* object,
ead1e424 3487 unsigned int shndx,
a2fb1b05 3488 const char* secname,
730cdc88 3489 const elfcpp::Shdr<64, true>& shdr,
a445fddf
ILT
3490 unsigned int reloc_shndx,
3491 bool have_sections_script);
193a53d9 3492#endif
a2fb1b05 3493
193a53d9 3494#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3495template
3496class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
193a53d9 3497#endif
c06b7b0b 3498
193a53d9 3499#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3500template
3501class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
193a53d9 3502#endif
c06b7b0b 3503
193a53d9 3504#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3505template
3506class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
193a53d9 3507#endif
c06b7b0b 3508
193a53d9 3509#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3510template
3511class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
193a53d9 3512#endif
c06b7b0b 3513
193a53d9 3514#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3515template
3516class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
193a53d9 3517#endif
c06b7b0b 3518
193a53d9 3519#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3520template
3521class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
193a53d9 3522#endif
c06b7b0b 3523
193a53d9 3524#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3525template
3526class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
193a53d9 3527#endif
c06b7b0b 3528
193a53d9 3529#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3530template
3531class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
193a53d9 3532#endif
c06b7b0b 3533
193a53d9 3534#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3535template
3536class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
193a53d9 3537#endif
c06b7b0b 3538
193a53d9 3539#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3540template
3541class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
193a53d9 3542#endif
c06b7b0b 3543
193a53d9 3544#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3545template
3546class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
193a53d9 3547#endif
c06b7b0b 3548
193a53d9 3549#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3550template
3551class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
193a53d9 3552#endif
c06b7b0b 3553
193a53d9 3554#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3555template
3556class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
193a53d9 3557#endif
c06b7b0b 3558
193a53d9 3559#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3560template
3561class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
193a53d9 3562#endif
c06b7b0b 3563
193a53d9 3564#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3565template
3566class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
193a53d9 3567#endif
c06b7b0b 3568
193a53d9 3569#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3570template
3571class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
193a53d9 3572#endif
c06b7b0b 3573
6a74a719
ILT
3574#ifdef HAVE_TARGET_32_LITTLE
3575template
3576class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3577#endif
3578
3579#ifdef HAVE_TARGET_32_BIG
3580template
3581class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3582#endif
3583
3584#ifdef HAVE_TARGET_64_LITTLE
3585template
3586class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3587#endif
3588
3589#ifdef HAVE_TARGET_64_BIG
3590template
3591class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3592#endif
3593
3594#ifdef HAVE_TARGET_32_LITTLE
3595template
3596class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3597#endif
3598
3599#ifdef HAVE_TARGET_32_BIG
3600template
3601class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3602#endif
3603
3604#ifdef HAVE_TARGET_64_LITTLE
3605template
3606class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3607#endif
3608
3609#ifdef HAVE_TARGET_64_BIG
3610template
3611class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3612#endif
3613
3614#ifdef HAVE_TARGET_32_LITTLE
3615template
3616class Output_data_group<32, false>;
3617#endif
3618
3619#ifdef HAVE_TARGET_32_BIG
3620template
3621class Output_data_group<32, true>;
3622#endif
3623
3624#ifdef HAVE_TARGET_64_LITTLE
3625template
3626class Output_data_group<64, false>;
3627#endif
3628
3629#ifdef HAVE_TARGET_64_BIG
3630template
3631class Output_data_group<64, true>;
3632#endif
3633
193a53d9 3634#ifdef HAVE_TARGET_32_LITTLE
ead1e424 3635template
dbe717ef 3636class Output_data_got<32, false>;
193a53d9 3637#endif
ead1e424 3638
193a53d9 3639#ifdef HAVE_TARGET_32_BIG
ead1e424 3640template
dbe717ef 3641class Output_data_got<32, true>;
193a53d9 3642#endif
ead1e424 3643
193a53d9 3644#ifdef HAVE_TARGET_64_LITTLE
ead1e424 3645template
dbe717ef 3646class Output_data_got<64, false>;
193a53d9 3647#endif
ead1e424 3648
193a53d9 3649#ifdef HAVE_TARGET_64_BIG
ead1e424 3650template
dbe717ef 3651class Output_data_got<64, true>;
193a53d9 3652#endif
ead1e424 3653
a2fb1b05 3654} // End namespace gold.
This page took 0.349668 seconds and 4 git commands to generate.