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