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