* inflow.c (terminal_ours_1): Guard access to
[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);
d1f003c6 803 gold_assert(os != NULL && offset != -1);
dceae3c1
ILT
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
d1f003c6
ILT
854Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
855 Address addend) const
e8c846c3
ILT
856{
857 if (this->local_sym_index_ == GSYM_CODE)
858 {
859 const Sized_symbol<size>* sym;
860 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
d1f003c6 861 return sym->value() + addend;
e8c846c3
ILT
862 }
863 gold_assert(this->local_sym_index_ != SECTION_CODE
d1f003c6
ILT
864 && this->local_sym_index_ != INVALID_CODE
865 && !this->is_section_symbol_);
866 const unsigned int lsi = this->local_sym_index_;
867 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
868 return symval->value(this->u1_.relobj, addend);
e8c846c3
ILT
869}
870
c06b7b0b
ILT
871// Write out a Rela relocation.
872
873template<bool dynamic, int size, bool big_endian>
874void
875Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
876 unsigned char* pov) const
877{
878 elfcpp::Rela_write<size, big_endian> orel(pov);
879 this->rel_.write_rel(&orel);
e8c846c3 880 Addend addend = this->addend_;
dceae3c1 881 if (this->rel_.is_relative())
d1f003c6
ILT
882 addend = this->rel_.symbol_value(addend);
883 else if (this->rel_.is_local_section_symbol())
dceae3c1 884 addend += this->rel_.local_section_offset();
e8c846c3 885 orel.put_r_addend(addend);
c06b7b0b
ILT
886}
887
888// Output_data_reloc_base methods.
889
16649710
ILT
890// Adjust the output section.
891
892template<int sh_type, bool dynamic, int size, bool big_endian>
893void
894Output_data_reloc_base<sh_type, dynamic, size, big_endian>
895 ::do_adjust_output_section(Output_section* os)
896{
897 if (sh_type == elfcpp::SHT_REL)
898 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
899 else if (sh_type == elfcpp::SHT_RELA)
900 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
901 else
902 gold_unreachable();
903 if (dynamic)
904 os->set_should_link_to_dynsym();
905 else
906 os->set_should_link_to_symtab();
907}
908
c06b7b0b
ILT
909// Write out relocation data.
910
911template<int sh_type, bool dynamic, int size, bool big_endian>
912void
913Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
914 Output_file* of)
915{
916 const off_t off = this->offset();
917 const off_t oview_size = this->data_size();
918 unsigned char* const oview = of->get_output_view(off, oview_size);
919
920 unsigned char* pov = oview;
921 for (typename Relocs::const_iterator p = this->relocs_.begin();
922 p != this->relocs_.end();
923 ++p)
924 {
925 p->write(pov);
926 pov += reloc_size;
927 }
928
a3ad94ed 929 gold_assert(pov - oview == oview_size);
c06b7b0b
ILT
930
931 of->write_output_view(off, oview_size, oview);
932
933 // We no longer need the relocation entries.
934 this->relocs_.clear();
935}
936
6a74a719
ILT
937// Class Output_relocatable_relocs.
938
939template<int sh_type, int size, bool big_endian>
940void
941Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
942{
943 this->set_data_size(this->rr_->output_reloc_count()
944 * Reloc_types<sh_type, size, big_endian>::reloc_size);
945}
946
947// class Output_data_group.
948
949template<int size, bool big_endian>
950Output_data_group<size, big_endian>::Output_data_group(
951 Sized_relobj<size, big_endian>* relobj,
952 section_size_type entry_count,
953 const elfcpp::Elf_Word* contents)
954 : Output_section_data(entry_count * 4, 4),
955 relobj_(relobj)
956{
957 this->flags_ = elfcpp::Swap<32, big_endian>::readval(contents);
958 for (section_size_type i = 1; i < entry_count; ++i)
959 {
960 unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
961 this->input_sections_.push_back(shndx);
962 }
963}
964
965// Write out the section group, which means translating the section
966// indexes to apply to the output file.
967
968template<int size, bool big_endian>
969void
970Output_data_group<size, big_endian>::do_write(Output_file* of)
971{
972 const off_t off = this->offset();
973 const section_size_type oview_size =
974 convert_to_section_size_type(this->data_size());
975 unsigned char* const oview = of->get_output_view(off, oview_size);
976
977 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
978 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
979 ++contents;
980
981 for (std::vector<unsigned int>::const_iterator p =
982 this->input_sections_.begin();
983 p != this->input_sections_.end();
984 ++p, ++contents)
985 {
986 section_offset_type dummy;
987 Output_section* os = this->relobj_->output_section(*p, &dummy);
988
989 unsigned int output_shndx;
990 if (os != NULL)
991 output_shndx = os->out_shndx();
992 else
993 {
994 this->relobj_->error(_("section group retained but "
995 "group element discarded"));
996 output_shndx = 0;
997 }
998
999 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1000 }
1001
1002 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1003 gold_assert(wrote == oview_size);
1004
1005 of->write_output_view(off, oview_size, oview);
1006
1007 // We no longer need this information.
1008 this->input_sections_.clear();
1009}
1010
dbe717ef 1011// Output_data_got::Got_entry methods.
ead1e424
ILT
1012
1013// Write out the entry.
1014
1015template<int size, bool big_endian>
1016void
7e1edb90 1017Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
ead1e424
ILT
1018{
1019 Valtype val = 0;
1020
1021 switch (this->local_sym_index_)
1022 {
1023 case GSYM_CODE:
1024 {
e8c846c3
ILT
1025 // If the symbol is resolved locally, we need to write out the
1026 // link-time value, which will be relocated dynamically by a
1027 // RELATIVE relocation.
ead1e424 1028 Symbol* gsym = this->u_.gsym;
e8c846c3
ILT
1029 Sized_symbol<size>* sgsym;
1030 // This cast is a bit ugly. We don't want to put a
1031 // virtual method in Symbol, because we want Symbol to be
1032 // as small as possible.
1033 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1034 val = sgsym->value();
ead1e424
ILT
1035 }
1036 break;
1037
1038 case CONSTANT_CODE:
1039 val = this->u_.constant;
1040 break;
1041
1042 default:
d1f003c6
ILT
1043 {
1044 const unsigned int lsi = this->local_sym_index_;
1045 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1046 val = symval->value(this->u_.object, 0);
1047 }
e727fa71 1048 break;
ead1e424
ILT
1049 }
1050
a3ad94ed 1051 elfcpp::Swap<size, big_endian>::writeval(pov, val);
ead1e424
ILT
1052}
1053
dbe717ef 1054// Output_data_got methods.
ead1e424 1055
dbe717ef
ILT
1056// Add an entry for a global symbol to the GOT. This returns true if
1057// this is a new GOT entry, false if the symbol already had a GOT
1058// entry.
1059
1060template<int size, bool big_endian>
1061bool
1062Output_data_got<size, big_endian>::add_global(Symbol* gsym)
ead1e424 1063{
dbe717ef
ILT
1064 if (gsym->has_got_offset())
1065 return false;
ead1e424 1066
dbe717ef
ILT
1067 this->entries_.push_back(Got_entry(gsym));
1068 this->set_got_size();
1069 gsym->set_got_offset(this->last_got_offset());
1070 return true;
1071}
ead1e424 1072
7bf1f802
ILT
1073// Add an entry for a global symbol to the GOT, and add a dynamic
1074// relocation of type R_TYPE for the GOT entry.
1075template<int size, bool big_endian>
1076void
1077Output_data_got<size, big_endian>::add_global_with_rel(
1078 Symbol* gsym,
1079 Rel_dyn* rel_dyn,
1080 unsigned int r_type)
1081{
1082 if (gsym->has_got_offset())
1083 return;
1084
1085 this->entries_.push_back(Got_entry());
1086 this->set_got_size();
1087 unsigned int got_offset = this->last_got_offset();
1088 gsym->set_got_offset(got_offset);
1089 rel_dyn->add_global(gsym, r_type, this, got_offset);
1090}
1091
1092template<int size, bool big_endian>
1093void
1094Output_data_got<size, big_endian>::add_global_with_rela(
1095 Symbol* gsym,
1096 Rela_dyn* rela_dyn,
1097 unsigned int r_type)
1098{
1099 if (gsym->has_got_offset())
1100 return;
1101
1102 this->entries_.push_back(Got_entry());
1103 this->set_got_size();
1104 unsigned int got_offset = this->last_got_offset();
1105 gsym->set_got_offset(got_offset);
1106 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1107}
1108
e727fa71
ILT
1109// Add an entry for a local symbol to the GOT. This returns true if
1110// this is a new GOT entry, false if the symbol already has a GOT
1111// entry.
1112
1113template<int size, bool big_endian>
1114bool
1115Output_data_got<size, big_endian>::add_local(
1116 Sized_relobj<size, big_endian>* object,
1117 unsigned int symndx)
1118{
1119 if (object->local_has_got_offset(symndx))
1120 return false;
07f397ab 1121
e727fa71
ILT
1122 this->entries_.push_back(Got_entry(object, symndx));
1123 this->set_got_size();
1124 object->set_local_got_offset(symndx, this->last_got_offset());
1125 return true;
1126}
1127
7bf1f802
ILT
1128// Add an entry for a local symbol to the GOT, and add a dynamic
1129// relocation of type R_TYPE for the GOT entry.
1130template<int size, bool big_endian>
1131void
1132Output_data_got<size, big_endian>::add_local_with_rel(
1133 Sized_relobj<size, big_endian>* object,
1134 unsigned int symndx,
1135 Rel_dyn* rel_dyn,
1136 unsigned int r_type)
1137{
1138 if (object->local_has_got_offset(symndx))
1139 return;
1140
1141 this->entries_.push_back(Got_entry());
1142 this->set_got_size();
1143 unsigned int got_offset = this->last_got_offset();
1144 object->set_local_got_offset(symndx, got_offset);
1145 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
1146}
1147
1148template<int size, bool big_endian>
1149void
1150Output_data_got<size, big_endian>::add_local_with_rela(
1151 Sized_relobj<size, big_endian>* object,
1152 unsigned int symndx,
1153 Rela_dyn* rela_dyn,
1154 unsigned int r_type)
1155{
1156 if (object->local_has_got_offset(symndx))
1157 return;
1158
1159 this->entries_.push_back(Got_entry());
1160 this->set_got_size();
1161 unsigned int got_offset = this->last_got_offset();
1162 object->set_local_got_offset(symndx, got_offset);
1163 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
1164}
1165
07f397ab
ILT
1166// Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
1167// In a pair of entries, the first value in the pair will be used for the
1168// module index, and the second value will be used for the dtv-relative
1169// offset. This returns true if this is a new GOT entry, false if the symbol
1170// already has a GOT entry.
1171
1172template<int size, bool big_endian>
1173bool
7bf1f802 1174Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym, bool need_pair)
07f397ab
ILT
1175{
1176 if (gsym->has_tls_got_offset(need_pair))
1177 return false;
1178
1179 this->entries_.push_back(Got_entry(gsym));
1180 gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
1181 if (need_pair)
1182 this->entries_.push_back(Got_entry(gsym));
1183 this->set_got_size();
1184 return true;
1185}
1186
7bf1f802
ILT
1187// Add an entry for a global TLS symbol to the GOT, and add a dynamic
1188// relocation of type R_TYPE.
1189template<int size, bool big_endian>
1190void
1191Output_data_got<size, big_endian>::add_global_tls_with_rel(
1192 Symbol* gsym,
1193 Rel_dyn* rel_dyn,
1194 unsigned int r_type)
1195{
1196 if (gsym->has_tls_got_offset(false))
1197 return;
1198
1199 this->entries_.push_back(Got_entry());
1200 this->set_got_size();
1201 unsigned int got_offset = this->last_got_offset();
1202 gsym->set_tls_got_offset(got_offset, false);
1203 rel_dyn->add_global(gsym, r_type, this, got_offset);
1204}
1205
1206template<int size, bool big_endian>
1207void
1208Output_data_got<size, big_endian>::add_global_tls_with_rela(
1209 Symbol* gsym,
1210 Rela_dyn* rela_dyn,
1211 unsigned int r_type)
1212{
1213 if (gsym->has_tls_got_offset(false))
1214 return;
1215
1216 this->entries_.push_back(Got_entry());
1217 this->set_got_size();
1218 unsigned int got_offset = this->last_got_offset();
1219 gsym->set_tls_got_offset(got_offset, false);
1220 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1221}
1222
1223// Add a pair of entries for a global TLS symbol to the GOT, and add
1224// dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1225template<int size, bool big_endian>
1226void
1227Output_data_got<size, big_endian>::add_global_tls_with_rel(
1228 Symbol* gsym,
1229 Rel_dyn* rel_dyn,
1230 unsigned int mod_r_type,
1231 unsigned int dtv_r_type)
1232{
1233 if (gsym->has_tls_got_offset(true))
1234 return;
1235
1236 this->entries_.push_back(Got_entry());
1237 unsigned int got_offset = this->last_got_offset();
1238 gsym->set_tls_got_offset(got_offset, true);
1239 rel_dyn->add_global(gsym, mod_r_type, this, got_offset);
1240
1241 this->entries_.push_back(Got_entry());
1242 this->set_got_size();
1243 got_offset = this->last_got_offset();
1244 rel_dyn->add_global(gsym, dtv_r_type, this, got_offset);
1245}
1246
1247template<int size, bool big_endian>
1248void
1249Output_data_got<size, big_endian>::add_global_tls_with_rela(
1250 Symbol* gsym,
1251 Rela_dyn* rela_dyn,
1252 unsigned int mod_r_type,
1253 unsigned int dtv_r_type)
1254{
1255 if (gsym->has_tls_got_offset(true))
1256 return;
1257
1258 this->entries_.push_back(Got_entry());
1259 unsigned int got_offset = this->last_got_offset();
1260 gsym->set_tls_got_offset(got_offset, true);
1261 rela_dyn->add_global(gsym, mod_r_type, this, got_offset, 0);
1262
1263 this->entries_.push_back(Got_entry());
1264 this->set_got_size();
1265 got_offset = this->last_got_offset();
1266 rela_dyn->add_global(gsym, dtv_r_type, this, got_offset, 0);
1267}
1268
07f397ab
ILT
1269// Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1270// In a pair of entries, the first value in the pair will be used for the
1271// module index, and the second value will be used for the dtv-relative
1272// offset. This returns true if this is a new GOT entry, false if the symbol
1273// already has a GOT entry.
1274
1275template<int size, bool big_endian>
1276bool
1277Output_data_got<size, big_endian>::add_local_tls(
1278 Sized_relobj<size, big_endian>* object,
1279 unsigned int symndx,
1280 bool need_pair)
1281{
1282 if (object->local_has_tls_got_offset(symndx, need_pair))
1283 return false;
1284
1285 this->entries_.push_back(Got_entry(object, symndx));
1286 object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
1287 if (need_pair)
1288 this->entries_.push_back(Got_entry(object, symndx));
1289 this->set_got_size();
1290 return true;
1291}
1292
7bf1f802
ILT
1293// Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1294// and add a dynamic relocation of type R_TYPE for the first GOT entry.
1295// Because this is a local symbol, the first GOT entry can be relocated
1296// relative to a section symbol, and the second GOT entry will have an
1297// dtv-relative value that can be computed at link time.
1298template<int size, bool big_endian>
1299void
1300Output_data_got<size, big_endian>::add_local_tls_with_rel(
1301 Sized_relobj<size, big_endian>* object,
1302 unsigned int symndx,
1303 unsigned int shndx,
1304 bool need_pair,
1305 Rel_dyn* rel_dyn,
1306 unsigned int r_type)
1307{
1308 if (object->local_has_tls_got_offset(symndx, need_pair))
1309 return;
1310
1311 this->entries_.push_back(Got_entry());
1312 unsigned int got_offset = this->last_got_offset();
1313 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
8383303e 1314 section_offset_type off;
7bf1f802
ILT
1315 Output_section* os = object->output_section(shndx, &off);
1316 rel_dyn->add_output_section(os, r_type, this, got_offset);
1317
1318 // The second entry of the pair will be statically initialized
1319 // with the TLS offset of the symbol.
1320 if (need_pair)
1321 this->entries_.push_back(Got_entry(object, symndx));
1322
1323 this->set_got_size();
1324}
1325
1326template<int size, bool big_endian>
1327void
1328Output_data_got<size, big_endian>::add_local_tls_with_rela(
1329 Sized_relobj<size, big_endian>* object,
1330 unsigned int symndx,
1331 unsigned int shndx,
1332 bool need_pair,
1333 Rela_dyn* rela_dyn,
1334 unsigned int r_type)
1335{
1336 if (object->local_has_tls_got_offset(symndx, need_pair))
1337 return;
1338
1339 this->entries_.push_back(Got_entry());
1340 unsigned int got_offset = this->last_got_offset();
1341 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
8383303e 1342 section_offset_type off;
7bf1f802
ILT
1343 Output_section* os = object->output_section(shndx, &off);
1344 rela_dyn->add_output_section(os, r_type, this, got_offset, 0);
1345
1346 // The second entry of the pair will be statically initialized
1347 // with the TLS offset of the symbol.
1348 if (need_pair)
1349 this->entries_.push_back(Got_entry(object, symndx));
1350
1351 this->set_got_size();
1352}
1353
ead1e424
ILT
1354// Write out the GOT.
1355
1356template<int size, bool big_endian>
1357void
dbe717ef 1358Output_data_got<size, big_endian>::do_write(Output_file* of)
ead1e424
ILT
1359{
1360 const int add = size / 8;
1361
1362 const off_t off = this->offset();
c06b7b0b 1363 const off_t oview_size = this->data_size();
ead1e424
ILT
1364 unsigned char* const oview = of->get_output_view(off, oview_size);
1365
1366 unsigned char* pov = oview;
1367 for (typename Got_entries::const_iterator p = this->entries_.begin();
1368 p != this->entries_.end();
1369 ++p)
1370 {
7e1edb90 1371 p->write(pov);
ead1e424
ILT
1372 pov += add;
1373 }
1374
a3ad94ed 1375 gold_assert(pov - oview == oview_size);
c06b7b0b 1376
ead1e424
ILT
1377 of->write_output_view(off, oview_size, oview);
1378
1379 // We no longer need the GOT entries.
1380 this->entries_.clear();
1381}
1382
a3ad94ed
ILT
1383// Output_data_dynamic::Dynamic_entry methods.
1384
1385// Write out the entry.
1386
1387template<int size, bool big_endian>
1388void
1389Output_data_dynamic::Dynamic_entry::write(
1390 unsigned char* pov,
7d1a9ebb 1391 const Stringpool* pool) const
a3ad94ed
ILT
1392{
1393 typename elfcpp::Elf_types<size>::Elf_WXword val;
1394 switch (this->classification_)
1395 {
1396 case DYNAMIC_NUMBER:
1397 val = this->u_.val;
1398 break;
1399
1400 case DYNAMIC_SECTION_ADDRESS:
16649710 1401 val = this->u_.od->address();
a3ad94ed
ILT
1402 break;
1403
1404 case DYNAMIC_SECTION_SIZE:
16649710 1405 val = this->u_.od->data_size();
a3ad94ed
ILT
1406 break;
1407
1408 case DYNAMIC_SYMBOL:
1409 {
16649710
ILT
1410 const Sized_symbol<size>* s =
1411 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
1412 val = s->value();
1413 }
1414 break;
1415
1416 case DYNAMIC_STRING:
1417 val = pool->get_offset(this->u_.str);
1418 break;
1419
1420 default:
1421 gold_unreachable();
1422 }
1423
1424 elfcpp::Dyn_write<size, big_endian> dw(pov);
1425 dw.put_d_tag(this->tag_);
1426 dw.put_d_val(val);
1427}
1428
1429// Output_data_dynamic methods.
1430
16649710
ILT
1431// Adjust the output section to set the entry size.
1432
1433void
1434Output_data_dynamic::do_adjust_output_section(Output_section* os)
1435{
8851ecca 1436 if (parameters->target().get_size() == 32)
16649710 1437 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
8851ecca 1438 else if (parameters->target().get_size() == 64)
16649710
ILT
1439 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1440 else
1441 gold_unreachable();
1442}
1443
a3ad94ed
ILT
1444// Set the final data size.
1445
1446void
27bc2bce 1447Output_data_dynamic::set_final_data_size()
a3ad94ed
ILT
1448{
1449 // Add the terminating entry.
1450 this->add_constant(elfcpp::DT_NULL, 0);
1451
1452 int dyn_size;
8851ecca 1453 if (parameters->target().get_size() == 32)
a3ad94ed 1454 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
8851ecca 1455 else if (parameters->target().get_size() == 64)
a3ad94ed
ILT
1456 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1457 else
1458 gold_unreachable();
1459 this->set_data_size(this->entries_.size() * dyn_size);
1460}
1461
1462// Write out the dynamic entries.
1463
1464void
1465Output_data_dynamic::do_write(Output_file* of)
1466{
8851ecca 1467 switch (parameters->size_and_endianness())
a3ad94ed 1468 {
9025d29d 1469#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
1470 case Parameters::TARGET_32_LITTLE:
1471 this->sized_write<32, false>(of);
1472 break;
9025d29d 1473#endif
8851ecca
ILT
1474#ifdef HAVE_TARGET_32_BIG
1475 case Parameters::TARGET_32_BIG:
1476 this->sized_write<32, true>(of);
1477 break;
9025d29d 1478#endif
9025d29d 1479#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
1480 case Parameters::TARGET_64_LITTLE:
1481 this->sized_write<64, false>(of);
1482 break;
9025d29d 1483#endif
8851ecca
ILT
1484#ifdef HAVE_TARGET_64_BIG
1485 case Parameters::TARGET_64_BIG:
1486 this->sized_write<64, true>(of);
1487 break;
1488#endif
1489 default:
1490 gold_unreachable();
a3ad94ed 1491 }
a3ad94ed
ILT
1492}
1493
1494template<int size, bool big_endian>
1495void
1496Output_data_dynamic::sized_write(Output_file* of)
1497{
1498 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1499
1500 const off_t offset = this->offset();
1501 const off_t oview_size = this->data_size();
1502 unsigned char* const oview = of->get_output_view(offset, oview_size);
1503
1504 unsigned char* pov = oview;
1505 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1506 p != this->entries_.end();
1507 ++p)
1508 {
7d1a9ebb 1509 p->write<size, big_endian>(pov, this->pool_);
a3ad94ed
ILT
1510 pov += dyn_size;
1511 }
1512
1513 gold_assert(pov - oview == oview_size);
1514
1515 of->write_output_view(offset, oview_size, oview);
1516
1517 // We no longer need the dynamic entries.
1518 this->entries_.clear();
1519}
1520
ead1e424
ILT
1521// Output_section::Input_section methods.
1522
1523// Return the data size. For an input section we store the size here.
1524// For an Output_section_data, we have to ask it for the size.
1525
1526off_t
1527Output_section::Input_section::data_size() const
1528{
1529 if (this->is_input_section())
b8e6aad9 1530 return this->u1_.data_size;
ead1e424 1531 else
b8e6aad9 1532 return this->u2_.posd->data_size();
ead1e424
ILT
1533}
1534
1535// Set the address and file offset.
1536
1537void
96803768
ILT
1538Output_section::Input_section::set_address_and_file_offset(
1539 uint64_t address,
1540 off_t file_offset,
1541 off_t section_file_offset)
ead1e424
ILT
1542{
1543 if (this->is_input_section())
96803768
ILT
1544 this->u2_.object->set_section_offset(this->shndx_,
1545 file_offset - section_file_offset);
ead1e424 1546 else
96803768
ILT
1547 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1548}
1549
a445fddf
ILT
1550// Reset the address and file offset.
1551
1552void
1553Output_section::Input_section::reset_address_and_file_offset()
1554{
1555 if (!this->is_input_section())
1556 this->u2_.posd->reset_address_and_file_offset();
1557}
1558
96803768
ILT
1559// Finalize the data size.
1560
1561void
1562Output_section::Input_section::finalize_data_size()
1563{
1564 if (!this->is_input_section())
1565 this->u2_.posd->finalize_data_size();
b8e6aad9
ILT
1566}
1567
1e983657
ILT
1568// Try to turn an input offset into an output offset. We want to
1569// return the output offset relative to the start of this
1570// Input_section in the output section.
b8e6aad9 1571
8f00aeb8 1572inline bool
8383303e
ILT
1573Output_section::Input_section::output_offset(
1574 const Relobj* object,
1575 unsigned int shndx,
1576 section_offset_type offset,
1577 section_offset_type *poutput) const
b8e6aad9
ILT
1578{
1579 if (!this->is_input_section())
730cdc88 1580 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
b8e6aad9
ILT
1581 else
1582 {
730cdc88 1583 if (this->shndx_ != shndx || this->u2_.object != object)
b8e6aad9 1584 return false;
1e983657 1585 *poutput = offset;
b8e6aad9
ILT
1586 return true;
1587 }
ead1e424
ILT
1588}
1589
a9a60db6
ILT
1590// Return whether this is the merge section for the input section
1591// SHNDX in OBJECT.
1592
1593inline bool
1594Output_section::Input_section::is_merge_section_for(const Relobj* object,
1595 unsigned int shndx) const
1596{
1597 if (this->is_input_section())
1598 return false;
1599 return this->u2_.posd->is_merge_section_for(object, shndx);
1600}
1601
ead1e424
ILT
1602// Write out the data. We don't have to do anything for an input
1603// section--they are handled via Object::relocate--but this is where
1604// we write out the data for an Output_section_data.
1605
1606void
1607Output_section::Input_section::write(Output_file* of)
1608{
1609 if (!this->is_input_section())
b8e6aad9 1610 this->u2_.posd->write(of);
ead1e424
ILT
1611}
1612
96803768
ILT
1613// Write the data to a buffer. As for write(), we don't have to do
1614// anything for an input section.
1615
1616void
1617Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1618{
1619 if (!this->is_input_section())
1620 this->u2_.posd->write_to_buffer(buffer);
1621}
1622
a2fb1b05
ILT
1623// Output_section methods.
1624
1625// Construct an Output_section. NAME will point into a Stringpool.
1626
96803768 1627Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
b8e6aad9 1628 elfcpp::Elf_Xword flags)
96803768 1629 : name_(name),
a2fb1b05
ILT
1630 addralign_(0),
1631 entsize_(0),
a445fddf 1632 load_address_(0),
16649710 1633 link_section_(NULL),
a2fb1b05 1634 link_(0),
16649710 1635 info_section_(NULL),
6a74a719 1636 info_symndx_(NULL),
a2fb1b05
ILT
1637 info_(0),
1638 type_(type),
61ba1cf9 1639 flags_(flags),
91ea499d 1640 out_shndx_(-1U),
c06b7b0b
ILT
1641 symtab_index_(0),
1642 dynsym_index_(0),
ead1e424
ILT
1643 input_sections_(),
1644 first_input_offset_(0),
c51e6221 1645 fills_(),
96803768 1646 postprocessing_buffer_(NULL),
a3ad94ed 1647 needs_symtab_index_(false),
16649710
ILT
1648 needs_dynsym_index_(false),
1649 should_link_to_symtab_(false),
730cdc88 1650 should_link_to_dynsym_(false),
27bc2bce 1651 after_input_sections_(false),
7bf1f802 1652 requires_postprocessing_(false),
a445fddf
ILT
1653 found_in_sections_clause_(false),
1654 has_load_address_(false),
755ab8af 1655 info_uses_section_index_(false),
7bf1f802 1656 tls_offset_(0)
a2fb1b05 1657{
27bc2bce
ILT
1658 // An unallocated section has no address. Forcing this means that
1659 // we don't need special treatment for symbols defined in debug
1660 // sections.
1661 if ((flags & elfcpp::SHF_ALLOC) == 0)
1662 this->set_address(0);
a2fb1b05
ILT
1663}
1664
54dc6425
ILT
1665Output_section::~Output_section()
1666{
1667}
1668
16649710
ILT
1669// Set the entry size.
1670
1671void
1672Output_section::set_entsize(uint64_t v)
1673{
1674 if (this->entsize_ == 0)
1675 this->entsize_ = v;
1676 else
1677 gold_assert(this->entsize_ == v);
1678}
1679
ead1e424 1680// Add the input section SHNDX, with header SHDR, named SECNAME, in
730cdc88
ILT
1681// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1682// relocation section which applies to this section, or 0 if none, or
1683// -1U if more than one. Return the offset of the input section
1684// within the output section. Return -1 if the input section will
1685// receive special handling. In the normal case we don't always keep
1686// track of input sections for an Output_section. Instead, each
1687// Object keeps track of the Output_section for each of its input
a445fddf
ILT
1688// sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1689// track of input sections here; this is used when SECTIONS appears in
1690// a linker script.
a2fb1b05
ILT
1691
1692template<int size, bool big_endian>
1693off_t
730cdc88
ILT
1694Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1695 unsigned int shndx,
ead1e424 1696 const char* secname,
730cdc88 1697 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf
ILT
1698 unsigned int reloc_shndx,
1699 bool have_sections_script)
a2fb1b05
ILT
1700{
1701 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1702 if ((addralign & (addralign - 1)) != 0)
1703 {
75f2446e
ILT
1704 object->error(_("invalid alignment %lu for section \"%s\""),
1705 static_cast<unsigned long>(addralign), secname);
1706 addralign = 1;
a2fb1b05 1707 }
a2fb1b05
ILT
1708
1709 if (addralign > this->addralign_)
1710 this->addralign_ = addralign;
1711
44a43cf9 1712 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
a445fddf
ILT
1713 this->flags_ |= (sh_flags
1714 & (elfcpp::SHF_WRITE
1715 | elfcpp::SHF_ALLOC
1716 | elfcpp::SHF_EXECINSTR));
1717
4f833eee 1718 uint64_t entsize = shdr.get_sh_entsize();
44a43cf9
ILT
1719
1720 // .debug_str is a mergeable string section, but is not always so
1721 // marked by compilers. Mark manually here so we can optimize.
1722 if (strcmp(secname, ".debug_str") == 0)
4f833eee
ILT
1723 {
1724 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1725 entsize = 1;
1726 }
44a43cf9 1727
b8e6aad9 1728 // If this is a SHF_MERGE section, we pass all the input sections to
730cdc88
ILT
1729 // a Output_data_merge. We don't try to handle relocations for such
1730 // a section.
44a43cf9 1731 if ((sh_flags & elfcpp::SHF_MERGE) != 0
730cdc88 1732 && reloc_shndx == 0)
b8e6aad9 1733 {
44a43cf9 1734 if (this->add_merge_input_section(object, shndx, sh_flags,
96803768 1735 entsize, addralign))
b8e6aad9
ILT
1736 {
1737 // Tell the relocation routines that they need to call the
730cdc88 1738 // output_offset method to determine the final address.
b8e6aad9
ILT
1739 return -1;
1740 }
1741 }
1742
27bc2bce 1743 off_t offset_in_section = this->current_data_size_for_child();
c51e6221
ILT
1744 off_t aligned_offset_in_section = align_address(offset_in_section,
1745 addralign);
1746
1747 if (aligned_offset_in_section > offset_in_section
a445fddf 1748 && !have_sections_script
44a43cf9 1749 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
c51e6221
ILT
1750 && object->target()->has_code_fill())
1751 {
1752 // We need to add some fill data. Using fill_list_ when
1753 // possible is an optimization, since we will often have fill
1754 // sections without input sections.
1755 off_t fill_len = aligned_offset_in_section - offset_in_section;
1756 if (this->input_sections_.empty())
1757 this->fills_.push_back(Fill(offset_in_section, fill_len));
1758 else
1759 {
1760 // FIXME: When relaxing, the size needs to adjust to
1761 // maintain a constant alignment.
1762 std::string fill_data(object->target()->code_fill(fill_len));
1763 Output_data_const* odc = new Output_data_const(fill_data, 1);
1764 this->input_sections_.push_back(Input_section(odc));
1765 }
1766 }
1767
27bc2bce
ILT
1768 this->set_current_data_size_for_child(aligned_offset_in_section
1769 + shdr.get_sh_size());
a2fb1b05 1770
ead1e424
ILT
1771 // We need to keep track of this section if we are already keeping
1772 // track of sections, or if we are relaxing. FIXME: Add test for
1773 // relaxing.
a445fddf 1774 if (have_sections_script || !this->input_sections_.empty())
ead1e424
ILT
1775 this->input_sections_.push_back(Input_section(object, shndx,
1776 shdr.get_sh_size(),
1777 addralign));
54dc6425 1778
c51e6221 1779 return aligned_offset_in_section;
61ba1cf9
ILT
1780}
1781
ead1e424
ILT
1782// Add arbitrary data to an output section.
1783
1784void
1785Output_section::add_output_section_data(Output_section_data* posd)
1786{
b8e6aad9
ILT
1787 Input_section inp(posd);
1788 this->add_output_section_data(&inp);
a445fddf
ILT
1789
1790 if (posd->is_data_size_valid())
1791 {
1792 off_t offset_in_section = this->current_data_size_for_child();
1793 off_t aligned_offset_in_section = align_address(offset_in_section,
1794 posd->addralign());
1795 this->set_current_data_size_for_child(aligned_offset_in_section
1796 + posd->data_size());
1797 }
b8e6aad9
ILT
1798}
1799
1800// Add arbitrary data to an output section by Input_section.
c06b7b0b 1801
b8e6aad9
ILT
1802void
1803Output_section::add_output_section_data(Input_section* inp)
1804{
ead1e424 1805 if (this->input_sections_.empty())
27bc2bce 1806 this->first_input_offset_ = this->current_data_size_for_child();
c06b7b0b 1807
b8e6aad9 1808 this->input_sections_.push_back(*inp);
c06b7b0b 1809
b8e6aad9 1810 uint64_t addralign = inp->addralign();
ead1e424
ILT
1811 if (addralign > this->addralign_)
1812 this->addralign_ = addralign;
c06b7b0b 1813
b8e6aad9
ILT
1814 inp->set_output_section(this);
1815}
1816
1817// Add a merge section to an output section.
1818
1819void
1820Output_section::add_output_merge_section(Output_section_data* posd,
1821 bool is_string, uint64_t entsize)
1822{
1823 Input_section inp(posd, is_string, entsize);
1824 this->add_output_section_data(&inp);
1825}
1826
1827// Add an input section to a SHF_MERGE section.
1828
1829bool
1830Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1831 uint64_t flags, uint64_t entsize,
96803768 1832 uint64_t addralign)
b8e6aad9 1833{
87f95776
ILT
1834 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1835
1836 // We only merge strings if the alignment is not more than the
1837 // character size. This could be handled, but it's unusual.
1838 if (is_string && addralign > entsize)
b8e6aad9
ILT
1839 return false;
1840
b8e6aad9
ILT
1841 Input_section_list::iterator p;
1842 for (p = this->input_sections_.begin();
1843 p != this->input_sections_.end();
1844 ++p)
87f95776 1845 if (p->is_merge_section(is_string, entsize, addralign))
9a0910c3
ILT
1846 {
1847 p->add_input_section(object, shndx);
1848 return true;
1849 }
b8e6aad9
ILT
1850
1851 // We handle the actual constant merging in Output_merge_data or
1852 // Output_merge_string_data.
9a0910c3
ILT
1853 Output_section_data* posd;
1854 if (!is_string)
1855 posd = new Output_merge_data(entsize, addralign);
b8e6aad9
ILT
1856 else
1857 {
9a0910c3
ILT
1858 switch (entsize)
1859 {
1860 case 1:
1861 posd = new Output_merge_string<char>(addralign);
1862 break;
1863 case 2:
1864 posd = new Output_merge_string<uint16_t>(addralign);
1865 break;
1866 case 4:
1867 posd = new Output_merge_string<uint32_t>(addralign);
1868 break;
1869 default:
1870 return false;
1871 }
b8e6aad9
ILT
1872 }
1873
9a0910c3
ILT
1874 this->add_output_merge_section(posd, is_string, entsize);
1875 posd->add_input_section(object, shndx);
1876
b8e6aad9
ILT
1877 return true;
1878}
1879
730cdc88
ILT
1880// Given an address OFFSET relative to the start of input section
1881// SHNDX in OBJECT, return whether this address is being included in
1882// the final link. This should only be called if SHNDX in OBJECT has
1883// a special mapping.
1884
1885bool
1886Output_section::is_input_address_mapped(const Relobj* object,
1887 unsigned int shndx,
1888 off_t offset) const
1889{
1890 gold_assert(object->is_section_specially_mapped(shndx));
1891
1892 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1893 p != this->input_sections_.end();
1894 ++p)
1895 {
8383303e 1896 section_offset_type output_offset;
730cdc88
ILT
1897 if (p->output_offset(object, shndx, offset, &output_offset))
1898 return output_offset != -1;
1899 }
1900
1901 // By default we assume that the address is mapped. This should
1902 // only be called after we have passed all sections to Layout. At
1903 // that point we should know what we are discarding.
1904 return true;
1905}
1906
1907// Given an address OFFSET relative to the start of input section
1908// SHNDX in object OBJECT, return the output offset relative to the
1e983657
ILT
1909// start of the input section in the output section. This should only
1910// be called if SHNDX in OBJECT has a special mapping.
730cdc88 1911
8383303e 1912section_offset_type
730cdc88 1913Output_section::output_offset(const Relobj* object, unsigned int shndx,
8383303e 1914 section_offset_type offset) const
730cdc88
ILT
1915{
1916 gold_assert(object->is_section_specially_mapped(shndx));
1917 // This can only be called meaningfully when layout is complete.
1918 gold_assert(Output_data::is_layout_complete());
1919
1920 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1921 p != this->input_sections_.end();
1922 ++p)
1923 {
8383303e 1924 section_offset_type output_offset;
730cdc88
ILT
1925 if (p->output_offset(object, shndx, offset, &output_offset))
1926 return output_offset;
1927 }
1928 gold_unreachable();
1929}
1930
b8e6aad9
ILT
1931// Return the output virtual address of OFFSET relative to the start
1932// of input section SHNDX in object OBJECT.
1933
1934uint64_t
1935Output_section::output_address(const Relobj* object, unsigned int shndx,
1936 off_t offset) const
1937{
730cdc88 1938 gold_assert(object->is_section_specially_mapped(shndx));
730cdc88 1939
b8e6aad9
ILT
1940 uint64_t addr = this->address() + this->first_input_offset_;
1941 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1942 p != this->input_sections_.end();
1943 ++p)
1944 {
1945 addr = align_address(addr, p->addralign());
8383303e 1946 section_offset_type output_offset;
730cdc88
ILT
1947 if (p->output_offset(object, shndx, offset, &output_offset))
1948 {
1949 if (output_offset == -1)
1950 return -1U;
1951 return addr + output_offset;
1952 }
b8e6aad9
ILT
1953 addr += p->data_size();
1954 }
1955
1956 // If we get here, it means that we don't know the mapping for this
1957 // input section. This might happen in principle if
1958 // add_input_section were called before add_output_section_data.
1959 // But it should never actually happen.
1960
1961 gold_unreachable();
ead1e424
ILT
1962}
1963
a9a60db6
ILT
1964// Return the output address of the start of the merged section for
1965// input section SHNDX in object OBJECT.
1966
1967uint64_t
1968Output_section::starting_output_address(const Relobj* object,
1969 unsigned int shndx) const
1970{
1971 gold_assert(object->is_section_specially_mapped(shndx));
1972
1973 uint64_t addr = this->address() + this->first_input_offset_;
1974 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1975 p != this->input_sections_.end();
1976 ++p)
1977 {
1978 addr = align_address(addr, p->addralign());
1979
1980 // It would be nice if we could use the existing output_offset
1981 // method to get the output offset of input offset 0.
1982 // Unfortunately we don't know for sure that input offset 0 is
1983 // mapped at all.
1984 if (p->is_merge_section_for(object, shndx))
1985 return addr;
1986
1987 addr += p->data_size();
1988 }
1989 gold_unreachable();
1990}
1991
27bc2bce 1992// Set the data size of an Output_section. This is where we handle
ead1e424
ILT
1993// setting the addresses of any Output_section_data objects.
1994
1995void
27bc2bce 1996Output_section::set_final_data_size()
ead1e424
ILT
1997{
1998 if (this->input_sections_.empty())
27bc2bce
ILT
1999 {
2000 this->set_data_size(this->current_data_size_for_child());
2001 return;
2002 }
ead1e424 2003
27bc2bce
ILT
2004 uint64_t address = this->address();
2005 off_t startoff = this->offset();
ead1e424
ILT
2006 off_t off = startoff + this->first_input_offset_;
2007 for (Input_section_list::iterator p = this->input_sections_.begin();
2008 p != this->input_sections_.end();
2009 ++p)
2010 {
2011 off = align_address(off, p->addralign());
96803768
ILT
2012 p->set_address_and_file_offset(address + (off - startoff), off,
2013 startoff);
ead1e424
ILT
2014 off += p->data_size();
2015 }
2016
2017 this->set_data_size(off - startoff);
2018}
9a0910c3 2019
a445fddf
ILT
2020// Reset the address and file offset.
2021
2022void
2023Output_section::do_reset_address_and_file_offset()
2024{
2025 for (Input_section_list::iterator p = this->input_sections_.begin();
2026 p != this->input_sections_.end();
2027 ++p)
2028 p->reset_address_and_file_offset();
2029}
2030
7bf1f802
ILT
2031// Set the TLS offset. Called only for SHT_TLS sections.
2032
2033void
2034Output_section::do_set_tls_offset(uint64_t tls_base)
2035{
2036 this->tls_offset_ = this->address() - tls_base;
2037}
2038
61ba1cf9
ILT
2039// Write the section header to *OSHDR.
2040
2041template<int size, bool big_endian>
2042void
16649710
ILT
2043Output_section::write_header(const Layout* layout,
2044 const Stringpool* secnamepool,
61ba1cf9
ILT
2045 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2046{
2047 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2048 oshdr->put_sh_type(this->type_);
6a74a719
ILT
2049
2050 elfcpp::Elf_Xword flags = this->flags_;
755ab8af 2051 if (this->info_section_ != NULL && this->info_uses_section_index_)
6a74a719
ILT
2052 flags |= elfcpp::SHF_INFO_LINK;
2053 oshdr->put_sh_flags(flags);
2054
61ba1cf9
ILT
2055 oshdr->put_sh_addr(this->address());
2056 oshdr->put_sh_offset(this->offset());
2057 oshdr->put_sh_size(this->data_size());
16649710
ILT
2058 if (this->link_section_ != NULL)
2059 oshdr->put_sh_link(this->link_section_->out_shndx());
2060 else if (this->should_link_to_symtab_)
2061 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2062 else if (this->should_link_to_dynsym_)
2063 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2064 else
2065 oshdr->put_sh_link(this->link_);
755ab8af
ILT
2066
2067 elfcpp::Elf_Word info;
16649710 2068 if (this->info_section_ != NULL)
755ab8af
ILT
2069 {
2070 if (this->info_uses_section_index_)
2071 info = this->info_section_->out_shndx();
2072 else
2073 info = this->info_section_->symtab_index();
2074 }
6a74a719 2075 else if (this->info_symndx_ != NULL)
755ab8af 2076 info = this->info_symndx_->symtab_index();
16649710 2077 else
755ab8af
ILT
2078 info = this->info_;
2079 oshdr->put_sh_info(info);
2080
61ba1cf9
ILT
2081 oshdr->put_sh_addralign(this->addralign_);
2082 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
2083}
2084
ead1e424
ILT
2085// Write out the data. For input sections the data is written out by
2086// Object::relocate, but we have to handle Output_section_data objects
2087// here.
2088
2089void
2090Output_section::do_write(Output_file* of)
2091{
96803768
ILT
2092 gold_assert(!this->requires_postprocessing());
2093
c51e6221
ILT
2094 off_t output_section_file_offset = this->offset();
2095 for (Fill_list::iterator p = this->fills_.begin();
2096 p != this->fills_.end();
2097 ++p)
2098 {
8851ecca 2099 std::string fill_data(parameters->target().code_fill(p->length()));
c51e6221 2100 of->write(output_section_file_offset + p->section_offset(),
a445fddf 2101 fill_data.data(), fill_data.size());
c51e6221
ILT
2102 }
2103
ead1e424
ILT
2104 for (Input_section_list::iterator p = this->input_sections_.begin();
2105 p != this->input_sections_.end();
2106 ++p)
2107 p->write(of);
2108}
2109
96803768
ILT
2110// If a section requires postprocessing, create the buffer to use.
2111
2112void
2113Output_section::create_postprocessing_buffer()
2114{
2115 gold_assert(this->requires_postprocessing());
1bedcac5
ILT
2116
2117 if (this->postprocessing_buffer_ != NULL)
2118 return;
96803768
ILT
2119
2120 if (!this->input_sections_.empty())
2121 {
2122 off_t off = this->first_input_offset_;
2123 for (Input_section_list::iterator p = this->input_sections_.begin();
2124 p != this->input_sections_.end();
2125 ++p)
2126 {
2127 off = align_address(off, p->addralign());
2128 p->finalize_data_size();
2129 off += p->data_size();
2130 }
2131 this->set_current_data_size_for_child(off);
2132 }
2133
2134 off_t buffer_size = this->current_data_size_for_child();
2135 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2136}
2137
2138// Write all the data of an Output_section into the postprocessing
2139// buffer. This is used for sections which require postprocessing,
2140// such as compression. Input sections are handled by
2141// Object::Relocate.
2142
2143void
2144Output_section::write_to_postprocessing_buffer()
2145{
2146 gold_assert(this->requires_postprocessing());
2147
96803768
ILT
2148 unsigned char* buffer = this->postprocessing_buffer();
2149 for (Fill_list::iterator p = this->fills_.begin();
2150 p != this->fills_.end();
2151 ++p)
2152 {
8851ecca 2153 std::string fill_data(parameters->target().code_fill(p->length()));
a445fddf
ILT
2154 memcpy(buffer + p->section_offset(), fill_data.data(),
2155 fill_data.size());
96803768
ILT
2156 }
2157
2158 off_t off = this->first_input_offset_;
2159 for (Input_section_list::iterator p = this->input_sections_.begin();
2160 p != this->input_sections_.end();
2161 ++p)
2162 {
2163 off = align_address(off, p->addralign());
2164 p->write_to_buffer(buffer + off);
2165 off += p->data_size();
2166 }
2167}
2168
a445fddf
ILT
2169// Get the input sections for linker script processing. We leave
2170// behind the Output_section_data entries. Note that this may be
2171// slightly incorrect for merge sections. We will leave them behind,
2172// but it is possible that the script says that they should follow
2173// some other input sections, as in:
2174// .rodata { *(.rodata) *(.rodata.cst*) }
2175// For that matter, we don't handle this correctly:
2176// .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2177// With luck this will never matter.
2178
2179uint64_t
2180Output_section::get_input_sections(
2181 uint64_t address,
2182 const std::string& fill,
2183 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2184{
2185 uint64_t orig_address = address;
2186
2187 address = align_address(address, this->addralign());
2188
2189 Input_section_list remaining;
2190 for (Input_section_list::iterator p = this->input_sections_.begin();
2191 p != this->input_sections_.end();
2192 ++p)
2193 {
2194 if (p->is_input_section())
2195 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2196 else
2197 {
2198 uint64_t aligned_address = align_address(address, p->addralign());
2199 if (aligned_address != address && !fill.empty())
2200 {
2201 section_size_type length =
2202 convert_to_section_size_type(aligned_address - address);
2203 std::string this_fill;
2204 this_fill.reserve(length);
2205 while (this_fill.length() + fill.length() <= length)
2206 this_fill += fill;
2207 if (this_fill.length() < length)
2208 this_fill.append(fill, 0, length - this_fill.length());
2209
2210 Output_section_data* posd = new Output_data_const(this_fill, 0);
2211 remaining.push_back(Input_section(posd));
2212 }
2213 address = aligned_address;
2214
2215 remaining.push_back(*p);
2216
2217 p->finalize_data_size();
2218 address += p->data_size();
2219 }
2220 }
2221
2222 this->input_sections_.swap(remaining);
2223 this->first_input_offset_ = 0;
2224
2225 uint64_t data_size = address - orig_address;
2226 this->set_current_data_size_for_child(data_size);
2227 return data_size;
2228}
2229
2230// Add an input section from a script.
2231
2232void
2233Output_section::add_input_section_for_script(Relobj* object,
2234 unsigned int shndx,
2235 off_t data_size,
2236 uint64_t addralign)
2237{
2238 if (addralign > this->addralign_)
2239 this->addralign_ = addralign;
2240
2241 off_t offset_in_section = this->current_data_size_for_child();
2242 off_t aligned_offset_in_section = align_address(offset_in_section,
2243 addralign);
2244
2245 this->set_current_data_size_for_child(aligned_offset_in_section
2246 + data_size);
2247
2248 this->input_sections_.push_back(Input_section(object, shndx,
2249 data_size, addralign));
2250}
2251
38c5e8b4
ILT
2252// Print stats for merge sections to stderr.
2253
2254void
2255Output_section::print_merge_stats()
2256{
2257 Input_section_list::iterator p;
2258 for (p = this->input_sections_.begin();
2259 p != this->input_sections_.end();
2260 ++p)
2261 p->print_merge_stats(this->name_);
2262}
2263
a2fb1b05
ILT
2264// Output segment methods.
2265
2266Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
54dc6425 2267 : output_data_(),
75f65a3e 2268 output_bss_(),
a2fb1b05
ILT
2269 vaddr_(0),
2270 paddr_(0),
2271 memsz_(0),
a445fddf
ILT
2272 max_align_(0),
2273 min_p_align_(0),
a2fb1b05
ILT
2274 offset_(0),
2275 filesz_(0),
2276 type_(type),
ead1e424 2277 flags_(flags),
a445fddf
ILT
2278 is_max_align_known_(false),
2279 are_addresses_set_(false)
a2fb1b05
ILT
2280{
2281}
2282
2283// Add an Output_section to an Output_segment.
2284
2285void
75f65a3e 2286Output_segment::add_output_section(Output_section* os,
dbe717ef
ILT
2287 elfcpp::Elf_Word seg_flags,
2288 bool front)
a2fb1b05 2289{
a3ad94ed 2290 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
a445fddf 2291 gold_assert(!this->is_max_align_known_);
75f65a3e 2292
ead1e424 2293 // Update the segment flags.
75f65a3e 2294 this->flags_ |= seg_flags;
75f65a3e
ILT
2295
2296 Output_segment::Output_data_list* pdl;
2297 if (os->type() == elfcpp::SHT_NOBITS)
2298 pdl = &this->output_bss_;
2299 else
2300 pdl = &this->output_data_;
54dc6425 2301
a2fb1b05
ILT
2302 // So that PT_NOTE segments will work correctly, we need to ensure
2303 // that all SHT_NOTE sections are adjacent. This will normally
2304 // happen automatically, because all the SHT_NOTE input sections
2305 // will wind up in the same output section. However, it is possible
2306 // for multiple SHT_NOTE input sections to have different section
2307 // flags, and thus be in different output sections, but for the
2308 // different section flags to map into the same segment flags and
2309 // thus the same output segment.
54dc6425
ILT
2310
2311 // Note that while there may be many input sections in an output
2312 // section, there are normally only a few output sections in an
2313 // output segment. This loop is expected to be fast.
2314
61ba1cf9 2315 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
a2fb1b05 2316 {
a3ad94ed 2317 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 2318 do
54dc6425 2319 {
75f65a3e 2320 --p;
54dc6425
ILT
2321 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2322 {
dbe717ef 2323 // We don't worry about the FRONT parameter.
54dc6425 2324 ++p;
75f65a3e 2325 pdl->insert(p, os);
54dc6425
ILT
2326 return;
2327 }
2328 }
75f65a3e 2329 while (p != pdl->begin());
54dc6425
ILT
2330 }
2331
2332 // Similarly, so that PT_TLS segments will work, we need to group
75f65a3e
ILT
2333 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2334 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2335 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
07f397ab
ILT
2336 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2337 // and the PT_TLS segment -- we do this grouping only for the
2338 // PT_LOAD segment.
2339 if (this->type_ != elfcpp::PT_TLS
2340 && (os->flags() & elfcpp::SHF_TLS) != 0
2341 && !this->output_data_.empty())
54dc6425 2342 {
75f65a3e
ILT
2343 pdl = &this->output_data_;
2344 bool nobits = os->type() == elfcpp::SHT_NOBITS;
ead1e424 2345 bool sawtls = false;
a3ad94ed 2346 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 2347 do
a2fb1b05 2348 {
75f65a3e 2349 --p;
ead1e424
ILT
2350 bool insert;
2351 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2352 {
2353 sawtls = true;
2354 // Put a NOBITS section after the first TLS section.
2355 // But a PROGBITS section after the first TLS/PROGBITS
2356 // section.
2357 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2358 }
2359 else
2360 {
2361 // If we've gone past the TLS sections, but we've seen a
2362 // TLS section, then we need to insert this section now.
2363 insert = sawtls;
2364 }
2365
2366 if (insert)
a2fb1b05 2367 {
dbe717ef 2368 // We don't worry about the FRONT parameter.
a2fb1b05 2369 ++p;
75f65a3e 2370 pdl->insert(p, os);
a2fb1b05
ILT
2371 return;
2372 }
2373 }
75f65a3e 2374 while (p != pdl->begin());
ead1e424 2375
dbe717ef
ILT
2376 // There are no TLS sections yet; put this one at the requested
2377 // location in the section list.
a2fb1b05
ILT
2378 }
2379
dbe717ef
ILT
2380 if (front)
2381 pdl->push_front(os);
2382 else
2383 pdl->push_back(os);
75f65a3e
ILT
2384}
2385
1650c4ff
ILT
2386// Remove an Output_section from this segment. It is an error if it
2387// is not present.
2388
2389void
2390Output_segment::remove_output_section(Output_section* os)
2391{
2392 // We only need this for SHT_PROGBITS.
2393 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2394 for (Output_data_list::iterator p = this->output_data_.begin();
2395 p != this->output_data_.end();
2396 ++p)
2397 {
2398 if (*p == os)
2399 {
2400 this->output_data_.erase(p);
2401 return;
2402 }
2403 }
2404 gold_unreachable();
2405}
2406
75f65a3e
ILT
2407// Add an Output_data (which is not an Output_section) to the start of
2408// a segment.
2409
2410void
2411Output_segment::add_initial_output_data(Output_data* od)
2412{
a445fddf 2413 gold_assert(!this->is_max_align_known_);
75f65a3e
ILT
2414 this->output_data_.push_front(od);
2415}
2416
2417// Return the maximum alignment of the Output_data in Output_segment.
75f65a3e
ILT
2418
2419uint64_t
a445fddf 2420Output_segment::maximum_alignment()
75f65a3e 2421{
a445fddf 2422 if (!this->is_max_align_known_)
ead1e424
ILT
2423 {
2424 uint64_t addralign;
2425
a445fddf
ILT
2426 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2427 if (addralign > this->max_align_)
2428 this->max_align_ = addralign;
ead1e424 2429
a445fddf
ILT
2430 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2431 if (addralign > this->max_align_)
2432 this->max_align_ = addralign;
ead1e424 2433
a445fddf 2434 this->is_max_align_known_ = true;
ead1e424
ILT
2435 }
2436
a445fddf 2437 return this->max_align_;
75f65a3e
ILT
2438}
2439
ead1e424
ILT
2440// Return the maximum alignment of a list of Output_data.
2441
2442uint64_t
a445fddf 2443Output_segment::maximum_alignment_list(const Output_data_list* pdl)
ead1e424
ILT
2444{
2445 uint64_t ret = 0;
2446 for (Output_data_list::const_iterator p = pdl->begin();
2447 p != pdl->end();
2448 ++p)
2449 {
2450 uint64_t addralign = (*p)->addralign();
2451 if (addralign > ret)
2452 ret = addralign;
2453 }
2454 return ret;
2455}
2456
4f4c5f80
ILT
2457// Return the number of dynamic relocs applied to this segment.
2458
2459unsigned int
2460Output_segment::dynamic_reloc_count() const
2461{
2462 return (this->dynamic_reloc_count_list(&this->output_data_)
2463 + this->dynamic_reloc_count_list(&this->output_bss_));
2464}
2465
2466// Return the number of dynamic relocs applied to an Output_data_list.
2467
2468unsigned int
2469Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2470{
2471 unsigned int count = 0;
2472 for (Output_data_list::const_iterator p = pdl->begin();
2473 p != pdl->end();
2474 ++p)
2475 count += (*p)->dynamic_reloc_count();
2476 return count;
2477}
2478
a445fddf
ILT
2479// Set the section addresses for an Output_segment. If RESET is true,
2480// reset the addresses first. ADDR is the address and *POFF is the
2481// file offset. Set the section indexes starting with *PSHNDX.
2482// Return the address of the immediately following segment. Update
2483// *POFF and *PSHNDX.
75f65a3e
ILT
2484
2485uint64_t
a445fddf 2486Output_segment::set_section_addresses(bool reset, uint64_t addr, off_t* poff,
ead1e424 2487 unsigned int* pshndx)
75f65a3e 2488{
a3ad94ed 2489 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e 2490
a445fddf
ILT
2491 if (!reset && this->are_addresses_set_)
2492 {
2493 gold_assert(this->paddr_ == addr);
2494 addr = this->vaddr_;
2495 }
2496 else
2497 {
2498 this->vaddr_ = addr;
2499 this->paddr_ = addr;
2500 this->are_addresses_set_ = true;
2501 }
75f65a3e
ILT
2502
2503 off_t orig_off = *poff;
2504 this->offset_ = orig_off;
2505
a445fddf
ILT
2506 addr = this->set_section_list_addresses(reset, &this->output_data_,
2507 addr, poff, pshndx);
75f65a3e
ILT
2508 this->filesz_ = *poff - orig_off;
2509
2510 off_t off = *poff;
2511
a445fddf
ILT
2512 uint64_t ret = this->set_section_list_addresses(reset, &this->output_bss_,
2513 addr, poff, pshndx);
75f65a3e
ILT
2514 this->memsz_ = *poff - orig_off;
2515
2516 // Ignore the file offset adjustments made by the BSS Output_data
2517 // objects.
2518 *poff = off;
61ba1cf9
ILT
2519
2520 return ret;
75f65a3e
ILT
2521}
2522
b8e6aad9
ILT
2523// Set the addresses and file offsets in a list of Output_data
2524// structures.
75f65a3e
ILT
2525
2526uint64_t
a445fddf 2527Output_segment::set_section_list_addresses(bool reset, Output_data_list* pdl,
ead1e424
ILT
2528 uint64_t addr, off_t* poff,
2529 unsigned int* pshndx)
75f65a3e 2530{
ead1e424 2531 off_t startoff = *poff;
75f65a3e 2532
ead1e424 2533 off_t off = startoff;
75f65a3e
ILT
2534 for (Output_data_list::iterator p = pdl->begin();
2535 p != pdl->end();
2536 ++p)
2537 {
a445fddf
ILT
2538 if (reset)
2539 (*p)->reset_address_and_file_offset();
2540
2541 // When using a linker script the section will most likely
2542 // already have an address.
2543 if (!(*p)->is_address_valid())
3802b2dd
ILT
2544 {
2545 off = align_address(off, (*p)->addralign());
2546 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2547 }
a445fddf
ILT
2548 else
2549 {
2550 // The script may have inserted a skip forward, but it
2551 // better not have moved backward.
3802b2dd
ILT
2552 gold_assert((*p)->address() >= addr + (off - startoff));
2553 off += (*p)->address() - (addr + (off - startoff));
a445fddf
ILT
2554 (*p)->set_file_offset(off);
2555 (*p)->finalize_data_size();
2556 }
ead1e424
ILT
2557
2558 // Unless this is a PT_TLS segment, we want to ignore the size
2559 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
2560 // affect the size of a PT_LOAD segment.
2561 if (this->type_ == elfcpp::PT_TLS
2562 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2563 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2564 off += (*p)->data_size();
75f65a3e 2565
ead1e424
ILT
2566 if ((*p)->is_section())
2567 {
2568 (*p)->set_out_shndx(*pshndx);
2569 ++*pshndx;
2570 }
75f65a3e
ILT
2571 }
2572
2573 *poff = off;
ead1e424 2574 return addr + (off - startoff);
75f65a3e
ILT
2575}
2576
2577// For a non-PT_LOAD segment, set the offset from the sections, if
2578// any.
2579
2580void
2581Output_segment::set_offset()
2582{
a3ad94ed 2583 gold_assert(this->type_ != elfcpp::PT_LOAD);
75f65a3e 2584
a445fddf
ILT
2585 gold_assert(!this->are_addresses_set_);
2586
75f65a3e
ILT
2587 if (this->output_data_.empty() && this->output_bss_.empty())
2588 {
2589 this->vaddr_ = 0;
2590 this->paddr_ = 0;
a445fddf 2591 this->are_addresses_set_ = true;
75f65a3e 2592 this->memsz_ = 0;
a445fddf 2593 this->min_p_align_ = 0;
75f65a3e
ILT
2594 this->offset_ = 0;
2595 this->filesz_ = 0;
2596 return;
2597 }
2598
2599 const Output_data* first;
2600 if (this->output_data_.empty())
2601 first = this->output_bss_.front();
2602 else
2603 first = this->output_data_.front();
2604 this->vaddr_ = first->address();
a445fddf
ILT
2605 this->paddr_ = (first->has_load_address()
2606 ? first->load_address()
2607 : this->vaddr_);
2608 this->are_addresses_set_ = true;
75f65a3e
ILT
2609 this->offset_ = first->offset();
2610
2611 if (this->output_data_.empty())
2612 this->filesz_ = 0;
2613 else
2614 {
2615 const Output_data* last_data = this->output_data_.back();
2616 this->filesz_ = (last_data->address()
2617 + last_data->data_size()
2618 - this->vaddr_);
2619 }
2620
2621 const Output_data* last;
2622 if (this->output_bss_.empty())
2623 last = this->output_data_.back();
2624 else
2625 last = this->output_bss_.back();
2626 this->memsz_ = (last->address()
2627 + last->data_size()
2628 - this->vaddr_);
75f65a3e
ILT
2629}
2630
7bf1f802
ILT
2631// Set the TLS offsets of the sections in the PT_TLS segment.
2632
2633void
2634Output_segment::set_tls_offsets()
2635{
2636 gold_assert(this->type_ == elfcpp::PT_TLS);
2637
2638 for (Output_data_list::iterator p = this->output_data_.begin();
2639 p != this->output_data_.end();
2640 ++p)
2641 (*p)->set_tls_offset(this->vaddr_);
2642
2643 for (Output_data_list::iterator p = this->output_bss_.begin();
2644 p != this->output_bss_.end();
2645 ++p)
2646 (*p)->set_tls_offset(this->vaddr_);
2647}
2648
a445fddf
ILT
2649// Return the address of the first section.
2650
2651uint64_t
2652Output_segment::first_section_load_address() const
2653{
2654 for (Output_data_list::const_iterator p = this->output_data_.begin();
2655 p != this->output_data_.end();
2656 ++p)
2657 if ((*p)->is_section())
2658 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2659
2660 for (Output_data_list::const_iterator p = this->output_bss_.begin();
2661 p != this->output_bss_.end();
2662 ++p)
2663 if ((*p)->is_section())
2664 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2665
2666 gold_unreachable();
2667}
2668
75f65a3e
ILT
2669// Return the number of Output_sections in an Output_segment.
2670
2671unsigned int
2672Output_segment::output_section_count() const
2673{
2674 return (this->output_section_count_list(&this->output_data_)
2675 + this->output_section_count_list(&this->output_bss_));
2676}
2677
2678// Return the number of Output_sections in an Output_data_list.
2679
2680unsigned int
2681Output_segment::output_section_count_list(const Output_data_list* pdl) const
2682{
2683 unsigned int count = 0;
2684 for (Output_data_list::const_iterator p = pdl->begin();
2685 p != pdl->end();
2686 ++p)
2687 {
2688 if ((*p)->is_section())
2689 ++count;
2690 }
2691 return count;
a2fb1b05
ILT
2692}
2693
1c4f3631
ILT
2694// Return the section attached to the list segment with the lowest
2695// load address. This is used when handling a PHDRS clause in a
2696// linker script.
2697
2698Output_section*
2699Output_segment::section_with_lowest_load_address() const
2700{
2701 Output_section* found = NULL;
2702 uint64_t found_lma = 0;
2703 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
2704
2705 Output_section* found_data = found;
2706 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
2707 if (found != found_data && found_data != NULL)
2708 {
2709 gold_error(_("nobits section %s may not precede progbits section %s "
2710 "in same segment"),
2711 found->name(), found_data->name());
2712 return NULL;
2713 }
2714
2715 return found;
2716}
2717
2718// Look through a list for a section with a lower load address.
2719
2720void
2721Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
2722 Output_section** found,
2723 uint64_t* found_lma) const
2724{
2725 for (Output_data_list::const_iterator p = pdl->begin();
2726 p != pdl->end();
2727 ++p)
2728 {
2729 if (!(*p)->is_section())
2730 continue;
2731 Output_section* os = static_cast<Output_section*>(*p);
2732 uint64_t lma = (os->has_load_address()
2733 ? os->load_address()
2734 : os->address());
2735 if (*found == NULL || lma < *found_lma)
2736 {
2737 *found = os;
2738 *found_lma = lma;
2739 }
2740 }
2741}
2742
61ba1cf9
ILT
2743// Write the segment data into *OPHDR.
2744
2745template<int size, bool big_endian>
2746void
ead1e424 2747Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
61ba1cf9
ILT
2748{
2749 ophdr->put_p_type(this->type_);
2750 ophdr->put_p_offset(this->offset_);
2751 ophdr->put_p_vaddr(this->vaddr_);
2752 ophdr->put_p_paddr(this->paddr_);
2753 ophdr->put_p_filesz(this->filesz_);
2754 ophdr->put_p_memsz(this->memsz_);
2755 ophdr->put_p_flags(this->flags_);
a445fddf 2756 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
61ba1cf9
ILT
2757}
2758
2759// Write the section headers into V.
2760
2761template<int size, bool big_endian>
2762unsigned char*
16649710
ILT
2763Output_segment::write_section_headers(const Layout* layout,
2764 const Stringpool* secnamepool,
ead1e424 2765 unsigned char* v,
7d1a9ebb 2766 unsigned int *pshndx) const
5482377d 2767{
ead1e424
ILT
2768 // Every section that is attached to a segment must be attached to a
2769 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2770 // segments.
2771 if (this->type_ != elfcpp::PT_LOAD)
2772 return v;
2773
7d1a9ebb
ILT
2774 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
2775 &this->output_data_,
2776 v, pshndx);
2777 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
2778 &this->output_bss_,
2779 v, pshndx);
61ba1cf9
ILT
2780 return v;
2781}
2782
2783template<int size, bool big_endian>
2784unsigned char*
16649710
ILT
2785Output_segment::write_section_headers_list(const Layout* layout,
2786 const Stringpool* secnamepool,
61ba1cf9 2787 const Output_data_list* pdl,
ead1e424 2788 unsigned char* v,
7d1a9ebb 2789 unsigned int* pshndx) const
61ba1cf9
ILT
2790{
2791 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2792 for (Output_data_list::const_iterator p = pdl->begin();
2793 p != pdl->end();
2794 ++p)
2795 {
2796 if ((*p)->is_section())
2797 {
5482377d 2798 const Output_section* ps = static_cast<const Output_section*>(*p);
a3ad94ed 2799 gold_assert(*pshndx == ps->out_shndx());
61ba1cf9 2800 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 2801 ps->write_header(layout, secnamepool, &oshdr);
61ba1cf9 2802 v += shdr_size;
ead1e424 2803 ++*pshndx;
61ba1cf9
ILT
2804 }
2805 }
2806 return v;
2807}
2808
a2fb1b05
ILT
2809// Output_file methods.
2810
14144f39
ILT
2811Output_file::Output_file(const char* name)
2812 : name_(name),
61ba1cf9
ILT
2813 o_(-1),
2814 file_size_(0),
c420411f 2815 base_(NULL),
516cb3d0
ILT
2816 map_is_anonymous_(false),
2817 is_temporary_(false)
61ba1cf9
ILT
2818{
2819}
2820
2821// Open the output file.
2822
a2fb1b05 2823void
61ba1cf9 2824Output_file::open(off_t file_size)
a2fb1b05 2825{
61ba1cf9
ILT
2826 this->file_size_ = file_size;
2827
4e9d8586
ILT
2828 // Unlink the file first; otherwise the open() may fail if the file
2829 // is busy (e.g. it's an executable that's currently being executed).
2830 //
2831 // However, the linker may be part of a system where a zero-length
2832 // file is created for it to write to, with tight permissions (gcc
2833 // 2.95 did something like this). Unlinking the file would work
2834 // around those permission controls, so we only unlink if the file
2835 // has a non-zero size. We also unlink only regular files to avoid
2836 // trouble with directories/etc.
2837 //
2838 // If we fail, continue; this command is merely a best-effort attempt
2839 // to improve the odds for open().
2840
42a1b686 2841 // We let the name "-" mean "stdout"
516cb3d0 2842 if (!this->is_temporary_)
42a1b686 2843 {
516cb3d0
ILT
2844 if (strcmp(this->name_, "-") == 0)
2845 this->o_ = STDOUT_FILENO;
2846 else
2847 {
2848 struct stat s;
2849 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
2850 unlink_if_ordinary(this->name_);
2851
8851ecca 2852 int mode = parameters->options().relocatable() ? 0666 : 0777;
516cb3d0
ILT
2853 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
2854 if (o < 0)
2855 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
2856 this->o_ = o;
2857 }
42a1b686 2858 }
61ba1cf9 2859
27bc2bce
ILT
2860 this->map();
2861}
2862
2863// Resize the output file.
2864
2865void
2866Output_file::resize(off_t file_size)
2867{
c420411f
ILT
2868 // If the mmap is mapping an anonymous memory buffer, this is easy:
2869 // just mremap to the new size. If it's mapping to a file, we want
2870 // to unmap to flush to the file, then remap after growing the file.
2871 if (this->map_is_anonymous_)
2872 {
2873 void* base = ::mremap(this->base_, this->file_size_, file_size,
2874 MREMAP_MAYMOVE);
2875 if (base == MAP_FAILED)
2876 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
2877 this->base_ = static_cast<unsigned char*>(base);
2878 this->file_size_ = file_size;
2879 }
2880 else
2881 {
2882 this->unmap();
2883 this->file_size_ = file_size;
2884 this->map();
2885 }
27bc2bce
ILT
2886}
2887
2888// Map the file into memory.
2889
2890void
2891Output_file::map()
2892{
c420411f 2893 const int o = this->o_;
61ba1cf9 2894
c420411f
ILT
2895 // If the output file is not a regular file, don't try to mmap it;
2896 // instead, we'll mmap a block of memory (an anonymous buffer), and
2897 // then later write the buffer to the file.
2898 void* base;
2899 struct stat statbuf;
42a1b686
ILT
2900 if (o == STDOUT_FILENO || o == STDERR_FILENO
2901 || ::fstat(o, &statbuf) != 0
516cb3d0
ILT
2902 || !S_ISREG(statbuf.st_mode)
2903 || this->is_temporary_)
c420411f
ILT
2904 {
2905 this->map_is_anonymous_ = true;
2906 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2907 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2908 }
2909 else
2910 {
2911 // Write out one byte to make the file the right size.
2912 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
2913 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
2914 char b = 0;
2915 if (::write(o, &b, 1) != 1)
2916 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
2917
2918 // Map the file into memory.
2919 this->map_is_anonymous_ = false;
2920 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2921 MAP_SHARED, o, 0);
2922 }
61ba1cf9 2923 if (base == MAP_FAILED)
75f2446e 2924 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
2925 this->base_ = static_cast<unsigned char*>(base);
2926}
2927
c420411f 2928// Unmap the file from memory.
61ba1cf9
ILT
2929
2930void
c420411f 2931Output_file::unmap()
61ba1cf9
ILT
2932{
2933 if (::munmap(this->base_, this->file_size_) < 0)
a0c4fb0a 2934 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
61ba1cf9 2935 this->base_ = NULL;
c420411f
ILT
2936}
2937
2938// Close the output file.
2939
2940void
2941Output_file::close()
2942{
2943 // If the map isn't file-backed, we need to write it now.
516cb3d0 2944 if (this->map_is_anonymous_ && !this->is_temporary_)
c420411f
ILT
2945 {
2946 size_t bytes_to_write = this->file_size_;
2947 while (bytes_to_write > 0)
2948 {
2949 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
2950 if (bytes_written == 0)
2951 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
2952 else if (bytes_written < 0)
2953 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
2954 else
2955 bytes_to_write -= bytes_written;
2956 }
2957 }
2958 this->unmap();
61ba1cf9 2959
42a1b686 2960 // We don't close stdout or stderr
516cb3d0
ILT
2961 if (this->o_ != STDOUT_FILENO
2962 && this->o_ != STDERR_FILENO
2963 && !this->is_temporary_)
42a1b686
ILT
2964 if (::close(this->o_) < 0)
2965 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
61ba1cf9 2966 this->o_ = -1;
a2fb1b05
ILT
2967}
2968
2969// Instantiate the templates we need. We could use the configure
2970// script to restrict this to only the ones for implemented targets.
2971
193a53d9 2972#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
2973template
2974off_t
2975Output_section::add_input_section<32, false>(
730cdc88 2976 Sized_relobj<32, false>* object,
ead1e424 2977 unsigned int shndx,
a2fb1b05 2978 const char* secname,
730cdc88 2979 const elfcpp::Shdr<32, false>& shdr,
a445fddf
ILT
2980 unsigned int reloc_shndx,
2981 bool have_sections_script);
193a53d9 2982#endif
a2fb1b05 2983
193a53d9 2984#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
2985template
2986off_t
2987Output_section::add_input_section<32, true>(
730cdc88 2988 Sized_relobj<32, true>* object,
ead1e424 2989 unsigned int shndx,
a2fb1b05 2990 const char* secname,
730cdc88 2991 const elfcpp::Shdr<32, true>& shdr,
a445fddf
ILT
2992 unsigned int reloc_shndx,
2993 bool have_sections_script);
193a53d9 2994#endif
a2fb1b05 2995
193a53d9 2996#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
2997template
2998off_t
2999Output_section::add_input_section<64, false>(
730cdc88 3000 Sized_relobj<64, false>* object,
ead1e424 3001 unsigned int shndx,
a2fb1b05 3002 const char* secname,
730cdc88 3003 const elfcpp::Shdr<64, false>& shdr,
a445fddf
ILT
3004 unsigned int reloc_shndx,
3005 bool have_sections_script);
193a53d9 3006#endif
a2fb1b05 3007
193a53d9 3008#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
3009template
3010off_t
3011Output_section::add_input_section<64, true>(
730cdc88 3012 Sized_relobj<64, true>* object,
ead1e424 3013 unsigned int shndx,
a2fb1b05 3014 const char* secname,
730cdc88 3015 const elfcpp::Shdr<64, true>& shdr,
a445fddf
ILT
3016 unsigned int reloc_shndx,
3017 bool have_sections_script);
193a53d9 3018#endif
a2fb1b05 3019
193a53d9 3020#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3021template
3022class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
193a53d9 3023#endif
c06b7b0b 3024
193a53d9 3025#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3026template
3027class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
193a53d9 3028#endif
c06b7b0b 3029
193a53d9 3030#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3031template
3032class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
193a53d9 3033#endif
c06b7b0b 3034
193a53d9 3035#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3036template
3037class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
193a53d9 3038#endif
c06b7b0b 3039
193a53d9 3040#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3041template
3042class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
193a53d9 3043#endif
c06b7b0b 3044
193a53d9 3045#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3046template
3047class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
193a53d9 3048#endif
c06b7b0b 3049
193a53d9 3050#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3051template
3052class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
193a53d9 3053#endif
c06b7b0b 3054
193a53d9 3055#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3056template
3057class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
193a53d9 3058#endif
c06b7b0b 3059
193a53d9 3060#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3061template
3062class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
193a53d9 3063#endif
c06b7b0b 3064
193a53d9 3065#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3066template
3067class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
193a53d9 3068#endif
c06b7b0b 3069
193a53d9 3070#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3071template
3072class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
193a53d9 3073#endif
c06b7b0b 3074
193a53d9 3075#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3076template
3077class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
193a53d9 3078#endif
c06b7b0b 3079
193a53d9 3080#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3081template
3082class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
193a53d9 3083#endif
c06b7b0b 3084
193a53d9 3085#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3086template
3087class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
193a53d9 3088#endif
c06b7b0b 3089
193a53d9 3090#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3091template
3092class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
193a53d9 3093#endif
c06b7b0b 3094
193a53d9 3095#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3096template
3097class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
193a53d9 3098#endif
c06b7b0b 3099
6a74a719
ILT
3100#ifdef HAVE_TARGET_32_LITTLE
3101template
3102class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3103#endif
3104
3105#ifdef HAVE_TARGET_32_BIG
3106template
3107class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3108#endif
3109
3110#ifdef HAVE_TARGET_64_LITTLE
3111template
3112class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3113#endif
3114
3115#ifdef HAVE_TARGET_64_BIG
3116template
3117class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3118#endif
3119
3120#ifdef HAVE_TARGET_32_LITTLE
3121template
3122class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3123#endif
3124
3125#ifdef HAVE_TARGET_32_BIG
3126template
3127class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3128#endif
3129
3130#ifdef HAVE_TARGET_64_LITTLE
3131template
3132class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3133#endif
3134
3135#ifdef HAVE_TARGET_64_BIG
3136template
3137class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3138#endif
3139
3140#ifdef HAVE_TARGET_32_LITTLE
3141template
3142class Output_data_group<32, false>;
3143#endif
3144
3145#ifdef HAVE_TARGET_32_BIG
3146template
3147class Output_data_group<32, true>;
3148#endif
3149
3150#ifdef HAVE_TARGET_64_LITTLE
3151template
3152class Output_data_group<64, false>;
3153#endif
3154
3155#ifdef HAVE_TARGET_64_BIG
3156template
3157class Output_data_group<64, true>;
3158#endif
3159
193a53d9 3160#ifdef HAVE_TARGET_32_LITTLE
ead1e424 3161template
dbe717ef 3162class Output_data_got<32, false>;
193a53d9 3163#endif
ead1e424 3164
193a53d9 3165#ifdef HAVE_TARGET_32_BIG
ead1e424 3166template
dbe717ef 3167class Output_data_got<32, true>;
193a53d9 3168#endif
ead1e424 3169
193a53d9 3170#ifdef HAVE_TARGET_64_LITTLE
ead1e424 3171template
dbe717ef 3172class Output_data_got<64, false>;
193a53d9 3173#endif
ead1e424 3174
193a53d9 3175#ifdef HAVE_TARGET_64_BIG
ead1e424 3176template
dbe717ef 3177class Output_data_got<64, true>;
193a53d9 3178#endif
ead1e424 3179
a2fb1b05 3180} // End namespace gold.
This page took 0.231073 seconds and 4 git commands to generate.