* hostio.c: Correct copyright year.
[deliverable/binutils-gdb.git] / gold / output.cc
CommitLineData
a2fb1b05
ILT
1// output.cc -- manage the output file for gold
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
a2fb1b05
ILT
23#include "gold.h"
24
25#include <cstdlib>
61ba1cf9
ILT
26#include <cerrno>
27#include <fcntl.h>
28#include <unistd.h>
29#include <sys/mman.h>
4e9d8586 30#include <sys/stat.h>
75f65a3e 31#include <algorithm>
5ffcaa86 32#include "libiberty.h" // for unlink_if_ordinary()
a2fb1b05 33
7e1edb90 34#include "parameters.h"
9a0910c3 35#include "compressed_output.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
42namespace gold
43{
44
a3ad94ed
ILT
45// Output_data variables.
46
27bc2bce 47bool Output_data::allocated_sizes_are_fixed;
a3ad94ed 48
a2fb1b05
ILT
49// Output_data methods.
50
51Output_data::~Output_data()
52{
53}
54
730cdc88
ILT
55// Return the default alignment for the target size.
56
57uint64_t
58Output_data::default_alignment()
59{
60 return Output_data::default_alignment_for_size(parameters->get_size());
61}
62
75f65a3e
ILT
63// Return the default alignment for a size--32 or 64.
64
65uint64_t
730cdc88 66Output_data::default_alignment_for_size(int size)
75f65a3e
ILT
67{
68 if (size == 32)
69 return 4;
70 else if (size == 64)
71 return 8;
72 else
a3ad94ed 73 gold_unreachable();
75f65a3e
ILT
74}
75
75f65a3e
ILT
76// Output_section_header methods. This currently assumes that the
77// segment and section lists are complete at construction time.
78
79Output_section_headers::Output_section_headers(
16649710
ILT
80 const Layout* layout,
81 const Layout::Segment_list* segment_list,
82 const Layout::Section_list* unattached_section_list,
61ba1cf9 83 const Stringpool* secnamepool)
9025d29d 84 : layout_(layout),
75f65a3e 85 segment_list_(segment_list),
a3ad94ed 86 unattached_section_list_(unattached_section_list),
61ba1cf9 87 secnamepool_(secnamepool)
75f65a3e 88{
61ba1cf9
ILT
89 // Count all the sections. Start with 1 for the null section.
90 off_t count = 1;
16649710
ILT
91 for (Layout::Segment_list::const_iterator p = segment_list->begin();
92 p != segment_list->end();
75f65a3e 93 ++p)
ead1e424
ILT
94 if ((*p)->type() == elfcpp::PT_LOAD)
95 count += (*p)->output_section_count();
16649710 96 count += unattached_section_list->size();
75f65a3e 97
9025d29d 98 const int size = parameters->get_size();
75f65a3e
ILT
99 int shdr_size;
100 if (size == 32)
101 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
102 else if (size == 64)
103 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
104 else
a3ad94ed 105 gold_unreachable();
75f65a3e
ILT
106
107 this->set_data_size(count * shdr_size);
108}
109
61ba1cf9
ILT
110// Write out the section headers.
111
75f65a3e 112void
61ba1cf9 113Output_section_headers::do_write(Output_file* of)
a2fb1b05 114{
9025d29d 115 if (parameters->get_size() == 32)
61ba1cf9 116 {
9025d29d
ILT
117 if (parameters->is_big_endian())
118 {
119#ifdef HAVE_TARGET_32_BIG
120 this->do_sized_write<32, true>(of);
121#else
122 gold_unreachable();
123#endif
124 }
61ba1cf9 125 else
9025d29d
ILT
126 {
127#ifdef HAVE_TARGET_32_LITTLE
128 this->do_sized_write<32, false>(of);
129#else
130 gold_unreachable();
131#endif
132 }
61ba1cf9 133 }
9025d29d 134 else if (parameters->get_size() == 64)
61ba1cf9 135 {
9025d29d
ILT
136 if (parameters->is_big_endian())
137 {
138#ifdef HAVE_TARGET_64_BIG
139 this->do_sized_write<64, true>(of);
140#else
141 gold_unreachable();
142#endif
143 }
61ba1cf9 144 else
9025d29d
ILT
145 {
146#ifdef HAVE_TARGET_64_LITTLE
147 this->do_sized_write<64, false>(of);
148#else
149 gold_unreachable();
150#endif
151 }
61ba1cf9
ILT
152 }
153 else
a3ad94ed 154 gold_unreachable();
61ba1cf9
ILT
155}
156
157template<int size, bool big_endian>
158void
159Output_section_headers::do_sized_write(Output_file* of)
160{
161 off_t all_shdrs_size = this->data_size();
162 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
163
164 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
165 unsigned char* v = view;
166
167 {
168 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
169 oshdr.put_sh_name(0);
170 oshdr.put_sh_type(elfcpp::SHT_NULL);
171 oshdr.put_sh_flags(0);
172 oshdr.put_sh_addr(0);
173 oshdr.put_sh_offset(0);
174 oshdr.put_sh_size(0);
175 oshdr.put_sh_link(0);
176 oshdr.put_sh_info(0);
177 oshdr.put_sh_addralign(0);
178 oshdr.put_sh_entsize(0);
179 }
180
181 v += shdr_size;
182
ead1e424 183 unsigned shndx = 1;
16649710
ILT
184 for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
185 p != this->segment_list_->end();
61ba1cf9 186 ++p)
593f47df 187 v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
16649710 188 this->layout_, this->secnamepool_, v, &shndx
ead1e424 189 SELECT_SIZE_ENDIAN(size, big_endian));
a3ad94ed 190 for (Layout::Section_list::const_iterator p =
16649710
ILT
191 this->unattached_section_list_->begin();
192 p != this->unattached_section_list_->end();
61ba1cf9
ILT
193 ++p)
194 {
a3ad94ed 195 gold_assert(shndx == (*p)->out_shndx());
61ba1cf9 196 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 197 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
61ba1cf9 198 v += shdr_size;
ead1e424 199 ++shndx;
61ba1cf9
ILT
200 }
201
202 of->write_output_view(this->offset(), all_shdrs_size, view);
a2fb1b05
ILT
203}
204
54dc6425
ILT
205// Output_segment_header methods.
206
61ba1cf9 207Output_segment_headers::Output_segment_headers(
61ba1cf9 208 const Layout::Segment_list& segment_list)
9025d29d 209 : segment_list_(segment_list)
61ba1cf9 210{
9025d29d 211 const int size = parameters->get_size();
61ba1cf9
ILT
212 int phdr_size;
213 if (size == 32)
214 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
215 else if (size == 64)
216 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
217 else
a3ad94ed 218 gold_unreachable();
61ba1cf9
ILT
219
220 this->set_data_size(segment_list.size() * phdr_size);
221}
222
54dc6425 223void
61ba1cf9 224Output_segment_headers::do_write(Output_file* of)
75f65a3e 225{
9025d29d 226 if (parameters->get_size() == 32)
61ba1cf9 227 {
9025d29d
ILT
228 if (parameters->is_big_endian())
229 {
230#ifdef HAVE_TARGET_32_BIG
231 this->do_sized_write<32, true>(of);
232#else
233 gold_unreachable();
234#endif
235 }
61ba1cf9 236 else
9025d29d
ILT
237 {
238#ifdef HAVE_TARGET_32_LITTLE
61ba1cf9 239 this->do_sized_write<32, false>(of);
9025d29d
ILT
240#else
241 gold_unreachable();
242#endif
243 }
61ba1cf9 244 }
9025d29d 245 else if (parameters->get_size() == 64)
61ba1cf9 246 {
9025d29d
ILT
247 if (parameters->is_big_endian())
248 {
249#ifdef HAVE_TARGET_64_BIG
250 this->do_sized_write<64, true>(of);
251#else
252 gold_unreachable();
253#endif
254 }
61ba1cf9 255 else
9025d29d
ILT
256 {
257#ifdef HAVE_TARGET_64_LITTLE
258 this->do_sized_write<64, false>(of);
259#else
260 gold_unreachable();
261#endif
262 }
61ba1cf9
ILT
263 }
264 else
a3ad94ed 265 gold_unreachable();
61ba1cf9
ILT
266}
267
268template<int size, bool big_endian>
269void
270Output_segment_headers::do_sized_write(Output_file* of)
271{
272 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
273 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
274 unsigned char* view = of->get_output_view(this->offset(),
275 all_phdrs_size);
276 unsigned char* v = view;
277 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
278 p != this->segment_list_.end();
279 ++p)
280 {
281 elfcpp::Phdr_write<size, big_endian> ophdr(v);
282 (*p)->write_header(&ophdr);
283 v += phdr_size;
284 }
285
286 of->write_output_view(this->offset(), all_phdrs_size, view);
75f65a3e
ILT
287}
288
289// Output_file_header methods.
290
9025d29d 291Output_file_header::Output_file_header(const Target* target,
75f65a3e
ILT
292 const Symbol_table* symtab,
293 const Output_segment_headers* osh)
9025d29d 294 : target_(target),
75f65a3e 295 symtab_(symtab),
61ba1cf9 296 segment_header_(osh),
75f65a3e
ILT
297 section_header_(NULL),
298 shstrtab_(NULL)
299{
9025d29d 300 const int size = parameters->get_size();
61ba1cf9
ILT
301 int ehdr_size;
302 if (size == 32)
303 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
304 else if (size == 64)
305 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
306 else
a3ad94ed 307 gold_unreachable();
61ba1cf9
ILT
308
309 this->set_data_size(ehdr_size);
75f65a3e
ILT
310}
311
312// Set the section table information for a file header.
313
314void
315Output_file_header::set_section_info(const Output_section_headers* shdrs,
316 const Output_section* shstrtab)
317{
318 this->section_header_ = shdrs;
319 this->shstrtab_ = shstrtab;
320}
321
322// Write out the file header.
323
324void
61ba1cf9 325Output_file_header::do_write(Output_file* of)
54dc6425 326{
27bc2bce
ILT
327 gold_assert(this->offset() == 0);
328
9025d29d 329 if (parameters->get_size() == 32)
61ba1cf9 330 {
9025d29d
ILT
331 if (parameters->is_big_endian())
332 {
333#ifdef HAVE_TARGET_32_BIG
334 this->do_sized_write<32, true>(of);
335#else
336 gold_unreachable();
337#endif
338 }
61ba1cf9 339 else
9025d29d
ILT
340 {
341#ifdef HAVE_TARGET_32_LITTLE
342 this->do_sized_write<32, false>(of);
343#else
344 gold_unreachable();
345#endif
346 }
61ba1cf9 347 }
9025d29d 348 else if (parameters->get_size() == 64)
61ba1cf9 349 {
9025d29d
ILT
350 if (parameters->is_big_endian())
351 {
352#ifdef HAVE_TARGET_64_BIG
353 this->do_sized_write<64, true>(of);
354#else
355 gold_unreachable();
356#endif
357 }
61ba1cf9 358 else
9025d29d
ILT
359 {
360#ifdef HAVE_TARGET_64_LITTLE
361 this->do_sized_write<64, false>(of);
362#else
363 gold_unreachable();
364#endif
365 }
61ba1cf9
ILT
366 }
367 else
a3ad94ed 368 gold_unreachable();
61ba1cf9
ILT
369}
370
371// Write out the file header with appropriate size and endianess.
372
373template<int size, bool big_endian>
374void
375Output_file_header::do_sized_write(Output_file* of)
376{
a3ad94ed 377 gold_assert(this->offset() == 0);
61ba1cf9
ILT
378
379 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
380 unsigned char* view = of->get_output_view(0, ehdr_size);
381 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
382
383 unsigned char e_ident[elfcpp::EI_NIDENT];
384 memset(e_ident, 0, elfcpp::EI_NIDENT);
385 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
386 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
387 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
388 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
389 if (size == 32)
390 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
391 else if (size == 64)
392 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
393 else
a3ad94ed 394 gold_unreachable();
61ba1cf9
ILT
395 e_ident[elfcpp::EI_DATA] = (big_endian
396 ? elfcpp::ELFDATA2MSB
397 : elfcpp::ELFDATA2LSB);
398 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
399 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
400 oehdr.put_e_ident(e_ident);
401
402 elfcpp::ET e_type;
7e1edb90 403 if (parameters->output_is_object())
61ba1cf9 404 e_type = elfcpp::ET_REL;
436ca963
ILT
405 else if (parameters->output_is_shared())
406 e_type = elfcpp::ET_DYN;
61ba1cf9
ILT
407 else
408 e_type = elfcpp::ET_EXEC;
409 oehdr.put_e_type(e_type);
410
411 oehdr.put_e_machine(this->target_->machine_code());
412 oehdr.put_e_version(elfcpp::EV_CURRENT);
413
ead1e424 414 // FIXME: Need to support -e, and target specific entry symbol.
61ba1cf9
ILT
415 Symbol* sym = this->symtab_->lookup("_start");
416 typename Sized_symbol<size>::Value_type v;
417 if (sym == NULL)
418 v = 0;
419 else
420 {
421 Sized_symbol<size>* ssym;
593f47df 422 ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
5482377d 423 sym SELECT_SIZE(size));
61ba1cf9
ILT
424 v = ssym->value();
425 }
426 oehdr.put_e_entry(v);
427
428 oehdr.put_e_phoff(this->segment_header_->offset());
429 oehdr.put_e_shoff(this->section_header_->offset());
430
431 // FIXME: The target needs to set the flags.
432 oehdr.put_e_flags(0);
433
434 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
435 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
436 oehdr.put_e_phnum(this->segment_header_->data_size()
437 / elfcpp::Elf_sizes<size>::phdr_size);
438 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
439 oehdr.put_e_shnum(this->section_header_->data_size()
440 / elfcpp::Elf_sizes<size>::shdr_size);
ead1e424 441 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
61ba1cf9
ILT
442
443 of->write_output_view(0, ehdr_size, view);
54dc6425
ILT
444}
445
dbe717ef
ILT
446// Output_data_const methods.
447
448void
a3ad94ed 449Output_data_const::do_write(Output_file* of)
dbe717ef 450{
a3ad94ed
ILT
451 of->write(this->offset(), this->data_.data(), this->data_.size());
452}
453
454// Output_data_const_buffer methods.
455
456void
457Output_data_const_buffer::do_write(Output_file* of)
458{
459 of->write(this->offset(), this->p_, this->data_size());
dbe717ef
ILT
460}
461
462// Output_section_data methods.
463
16649710
ILT
464// Record the output section, and set the entry size and such.
465
466void
467Output_section_data::set_output_section(Output_section* os)
468{
469 gold_assert(this->output_section_ == NULL);
470 this->output_section_ = os;
471 this->do_adjust_output_section(os);
472}
473
474// Return the section index of the output section.
475
dbe717ef
ILT
476unsigned int
477Output_section_data::do_out_shndx() const
478{
a3ad94ed 479 gold_assert(this->output_section_ != NULL);
dbe717ef
ILT
480 return this->output_section_->out_shndx();
481}
482
a3ad94ed
ILT
483// Output_data_strtab methods.
484
27bc2bce 485// Set the final data size.
a3ad94ed
ILT
486
487void
27bc2bce 488Output_data_strtab::set_final_data_size()
a3ad94ed
ILT
489{
490 this->strtab_->set_string_offsets();
491 this->set_data_size(this->strtab_->get_strtab_size());
492}
493
494// Write out a string table.
495
496void
497Output_data_strtab::do_write(Output_file* of)
498{
499 this->strtab_->write(of, this->offset());
500}
501
c06b7b0b
ILT
502// Output_reloc methods.
503
504// Get the symbol index of a relocation.
505
506template<bool dynamic, int size, bool big_endian>
507unsigned int
508Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
509 const
510{
511 unsigned int index;
512 switch (this->local_sym_index_)
513 {
514 case INVALID_CODE:
a3ad94ed 515 gold_unreachable();
c06b7b0b
ILT
516
517 case GSYM_CODE:
5a6f7e2d 518 if (this->u1_.gsym == NULL)
c06b7b0b
ILT
519 index = 0;
520 else if (dynamic)
5a6f7e2d 521 index = this->u1_.gsym->dynsym_index();
c06b7b0b 522 else
5a6f7e2d 523 index = this->u1_.gsym->symtab_index();
c06b7b0b
ILT
524 break;
525
526 case SECTION_CODE:
527 if (dynamic)
5a6f7e2d 528 index = this->u1_.os->dynsym_index();
c06b7b0b 529 else
5a6f7e2d 530 index = this->u1_.os->symtab_index();
c06b7b0b
ILT
531 break;
532
436ca963
ILT
533 case 0:
534 // Relocations without symbols use a symbol index of 0.
535 index = 0;
536 break;
537
c06b7b0b
ILT
538 default:
539 if (dynamic)
540 {
541 // FIXME: It seems that some targets may need to generate
542 // dynamic relocations against local symbols for some
543 // reasons. This will have to be addressed at some point.
a3ad94ed 544 gold_unreachable();
c06b7b0b
ILT
545 }
546 else
5a6f7e2d 547 index = this->u1_.relobj->symtab_index(this->local_sym_index_);
c06b7b0b
ILT
548 break;
549 }
a3ad94ed 550 gold_assert(index != -1U);
c06b7b0b
ILT
551 return index;
552}
553
554// Write out the offset and info fields of a Rel or Rela relocation
555// entry.
556
557template<bool dynamic, int size, bool big_endian>
558template<typename Write_rel>
559void
560Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
561 Write_rel* wr) const
562{
a3ad94ed 563 Address address = this->address_;
5a6f7e2d
ILT
564 if (this->shndx_ != INVALID_CODE)
565 {
566 off_t off;
567 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
568 &off);
569 gold_assert(os != NULL);
730cdc88
ILT
570 if (off != -1)
571 address += os->address() + off;
572 else
573 {
574 address = os->output_address(this->u2_.relobj, this->shndx_,
575 address);
576 gold_assert(address != -1U);
577 }
5a6f7e2d
ILT
578 }
579 else if (this->u2_.od != NULL)
580 address += this->u2_.od->address();
a3ad94ed 581 wr->put_r_offset(address);
c06b7b0b
ILT
582 wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(),
583 this->type_));
584}
585
586// Write out a Rel relocation.
587
588template<bool dynamic, int size, bool big_endian>
589void
590Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
591 unsigned char* pov) const
592{
593 elfcpp::Rel_write<size, big_endian> orel(pov);
594 this->write_rel(&orel);
595}
596
597// Write out a Rela relocation.
598
599template<bool dynamic, int size, bool big_endian>
600void
601Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
602 unsigned char* pov) const
603{
604 elfcpp::Rela_write<size, big_endian> orel(pov);
605 this->rel_.write_rel(&orel);
606 orel.put_r_addend(this->addend_);
607}
608
609// Output_data_reloc_base methods.
610
16649710
ILT
611// Adjust the output section.
612
613template<int sh_type, bool dynamic, int size, bool big_endian>
614void
615Output_data_reloc_base<sh_type, dynamic, size, big_endian>
616 ::do_adjust_output_section(Output_section* os)
617{
618 if (sh_type == elfcpp::SHT_REL)
619 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
620 else if (sh_type == elfcpp::SHT_RELA)
621 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
622 else
623 gold_unreachable();
624 if (dynamic)
625 os->set_should_link_to_dynsym();
626 else
627 os->set_should_link_to_symtab();
628}
629
c06b7b0b
ILT
630// Write out relocation data.
631
632template<int sh_type, bool dynamic, int size, bool big_endian>
633void
634Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
635 Output_file* of)
636{
637 const off_t off = this->offset();
638 const off_t oview_size = this->data_size();
639 unsigned char* const oview = of->get_output_view(off, oview_size);
640
641 unsigned char* pov = oview;
642 for (typename Relocs::const_iterator p = this->relocs_.begin();
643 p != this->relocs_.end();
644 ++p)
645 {
646 p->write(pov);
647 pov += reloc_size;
648 }
649
a3ad94ed 650 gold_assert(pov - oview == oview_size);
c06b7b0b
ILT
651
652 of->write_output_view(off, oview_size, oview);
653
654 // We no longer need the relocation entries.
655 this->relocs_.clear();
656}
657
dbe717ef 658// Output_data_got::Got_entry methods.
ead1e424
ILT
659
660// Write out the entry.
661
662template<int size, bool big_endian>
663void
7e1edb90 664Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
ead1e424
ILT
665{
666 Valtype val = 0;
667
668 switch (this->local_sym_index_)
669 {
670 case GSYM_CODE:
671 {
672 Symbol* gsym = this->u_.gsym;
673
674 // If the symbol is resolved locally, we need to write out its
675 // value. Otherwise we just write zero. The target code is
676 // responsible for creating a relocation entry to fill in the
d61c6bd4
ILT
677 // value at runtime. For non-preemptible symbols in a shared
678 // library, the target will need to record whether or not the
679 // value should be written (e.g., it may use a RELATIVE
680 // relocation type).
681 if (gsym->final_value_is_known() || gsym->needs_value_in_got())
ead1e424
ILT
682 {
683 Sized_symbol<size>* sgsym;
684 // This cast is a bit ugly. We don't want to put a
685 // virtual method in Symbol, because we want Symbol to be
686 // as small as possible.
687 sgsym = static_cast<Sized_symbol<size>*>(gsym);
688 val = sgsym->value();
689 }
690 }
691 break;
692
693 case CONSTANT_CODE:
694 val = this->u_.constant;
695 break;
696
697 default:
e727fa71
ILT
698 val = this->u_.object->local_symbol_value(this->local_sym_index_);
699 break;
ead1e424
ILT
700 }
701
a3ad94ed 702 elfcpp::Swap<size, big_endian>::writeval(pov, val);
ead1e424
ILT
703}
704
dbe717ef 705// Output_data_got methods.
ead1e424 706
dbe717ef
ILT
707// Add an entry for a global symbol to the GOT. This returns true if
708// this is a new GOT entry, false if the symbol already had a GOT
709// entry.
710
711template<int size, bool big_endian>
712bool
713Output_data_got<size, big_endian>::add_global(Symbol* gsym)
ead1e424 714{
dbe717ef
ILT
715 if (gsym->has_got_offset())
716 return false;
ead1e424 717
dbe717ef
ILT
718 this->entries_.push_back(Got_entry(gsym));
719 this->set_got_size();
720 gsym->set_got_offset(this->last_got_offset());
721 return true;
722}
ead1e424 723
e727fa71
ILT
724// Add an entry for a local symbol to the GOT. This returns true if
725// this is a new GOT entry, false if the symbol already has a GOT
726// entry.
727
728template<int size, bool big_endian>
729bool
730Output_data_got<size, big_endian>::add_local(
731 Sized_relobj<size, big_endian>* object,
732 unsigned int symndx)
733{
734 if (object->local_has_got_offset(symndx))
735 return false;
07f397ab 736
e727fa71
ILT
737 this->entries_.push_back(Got_entry(object, symndx));
738 this->set_got_size();
739 object->set_local_got_offset(symndx, this->last_got_offset());
740 return true;
741}
742
07f397ab
ILT
743// Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
744// In a pair of entries, the first value in the pair will be used for the
745// module index, and the second value will be used for the dtv-relative
746// offset. This returns true if this is a new GOT entry, false if the symbol
747// already has a GOT entry.
748
749template<int size, bool big_endian>
750bool
751Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym,
752 bool need_pair)
753{
754 if (gsym->has_tls_got_offset(need_pair))
755 return false;
756
757 this->entries_.push_back(Got_entry(gsym));
758 gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
759 if (need_pair)
760 this->entries_.push_back(Got_entry(gsym));
761 this->set_got_size();
762 return true;
763}
764
765// Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
766// In a pair of entries, the first value in the pair will be used for the
767// module index, and the second value will be used for the dtv-relative
768// offset. This returns true if this is a new GOT entry, false if the symbol
769// already has a GOT entry.
770
771template<int size, bool big_endian>
772bool
773Output_data_got<size, big_endian>::add_local_tls(
774 Sized_relobj<size, big_endian>* object,
775 unsigned int symndx,
776 bool need_pair)
777{
778 if (object->local_has_tls_got_offset(symndx, need_pair))
779 return false;
780
781 this->entries_.push_back(Got_entry(object, symndx));
782 object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
783 if (need_pair)
784 this->entries_.push_back(Got_entry(object, symndx));
785 this->set_got_size();
786 return true;
787}
788
ead1e424
ILT
789// Write out the GOT.
790
791template<int size, bool big_endian>
792void
dbe717ef 793Output_data_got<size, big_endian>::do_write(Output_file* of)
ead1e424
ILT
794{
795 const int add = size / 8;
796
797 const off_t off = this->offset();
c06b7b0b 798 const off_t oview_size = this->data_size();
ead1e424
ILT
799 unsigned char* const oview = of->get_output_view(off, oview_size);
800
801 unsigned char* pov = oview;
802 for (typename Got_entries::const_iterator p = this->entries_.begin();
803 p != this->entries_.end();
804 ++p)
805 {
7e1edb90 806 p->write(pov);
ead1e424
ILT
807 pov += add;
808 }
809
a3ad94ed 810 gold_assert(pov - oview == oview_size);
c06b7b0b 811
ead1e424
ILT
812 of->write_output_view(off, oview_size, oview);
813
814 // We no longer need the GOT entries.
815 this->entries_.clear();
816}
817
a3ad94ed
ILT
818// Output_data_dynamic::Dynamic_entry methods.
819
820// Write out the entry.
821
822template<int size, bool big_endian>
823void
824Output_data_dynamic::Dynamic_entry::write(
825 unsigned char* pov,
1ddbd1e6
ILT
826 const Stringpool* pool
827 ACCEPT_SIZE_ENDIAN) const
a3ad94ed
ILT
828{
829 typename elfcpp::Elf_types<size>::Elf_WXword val;
830 switch (this->classification_)
831 {
832 case DYNAMIC_NUMBER:
833 val = this->u_.val;
834 break;
835
836 case DYNAMIC_SECTION_ADDRESS:
16649710 837 val = this->u_.od->address();
a3ad94ed
ILT
838 break;
839
840 case DYNAMIC_SECTION_SIZE:
16649710 841 val = this->u_.od->data_size();
a3ad94ed
ILT
842 break;
843
844 case DYNAMIC_SYMBOL:
845 {
16649710
ILT
846 const Sized_symbol<size>* s =
847 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
848 val = s->value();
849 }
850 break;
851
852 case DYNAMIC_STRING:
853 val = pool->get_offset(this->u_.str);
854 break;
855
856 default:
857 gold_unreachable();
858 }
859
860 elfcpp::Dyn_write<size, big_endian> dw(pov);
861 dw.put_d_tag(this->tag_);
862 dw.put_d_val(val);
863}
864
865// Output_data_dynamic methods.
866
16649710
ILT
867// Adjust the output section to set the entry size.
868
869void
870Output_data_dynamic::do_adjust_output_section(Output_section* os)
871{
9025d29d 872 if (parameters->get_size() == 32)
16649710 873 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
9025d29d 874 else if (parameters->get_size() == 64)
16649710
ILT
875 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
876 else
877 gold_unreachable();
878}
879
a3ad94ed
ILT
880// Set the final data size.
881
882void
27bc2bce 883Output_data_dynamic::set_final_data_size()
a3ad94ed
ILT
884{
885 // Add the terminating entry.
886 this->add_constant(elfcpp::DT_NULL, 0);
887
888 int dyn_size;
9025d29d 889 if (parameters->get_size() == 32)
a3ad94ed 890 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
9025d29d 891 else if (parameters->get_size() == 64)
a3ad94ed
ILT
892 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
893 else
894 gold_unreachable();
895 this->set_data_size(this->entries_.size() * dyn_size);
896}
897
898// Write out the dynamic entries.
899
900void
901Output_data_dynamic::do_write(Output_file* of)
902{
9025d29d 903 if (parameters->get_size() == 32)
a3ad94ed 904 {
9025d29d
ILT
905 if (parameters->is_big_endian())
906 {
907#ifdef HAVE_TARGET_32_BIG
908 this->sized_write<32, true>(of);
909#else
910 gold_unreachable();
911#endif
912 }
a3ad94ed 913 else
9025d29d
ILT
914 {
915#ifdef HAVE_TARGET_32_LITTLE
916 this->sized_write<32, false>(of);
917#else
918 gold_unreachable();
919#endif
920 }
a3ad94ed 921 }
9025d29d 922 else if (parameters->get_size() == 64)
a3ad94ed 923 {
9025d29d
ILT
924 if (parameters->is_big_endian())
925 {
926#ifdef HAVE_TARGET_64_BIG
927 this->sized_write<64, true>(of);
928#else
929 gold_unreachable();
930#endif
931 }
a3ad94ed 932 else
9025d29d
ILT
933 {
934#ifdef HAVE_TARGET_64_LITTLE
935 this->sized_write<64, false>(of);
936#else
937 gold_unreachable();
938#endif
939 }
a3ad94ed
ILT
940 }
941 else
942 gold_unreachable();
943}
944
945template<int size, bool big_endian>
946void
947Output_data_dynamic::sized_write(Output_file* of)
948{
949 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
950
951 const off_t offset = this->offset();
952 const off_t oview_size = this->data_size();
953 unsigned char* const oview = of->get_output_view(offset, oview_size);
954
955 unsigned char* pov = oview;
956 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
957 p != this->entries_.end();
958 ++p)
959 {
1ddbd1e6
ILT
960 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
961 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
a3ad94ed
ILT
962 pov += dyn_size;
963 }
964
965 gold_assert(pov - oview == oview_size);
966
967 of->write_output_view(offset, oview_size, oview);
968
969 // We no longer need the dynamic entries.
970 this->entries_.clear();
971}
972
ead1e424
ILT
973// Output_section::Input_section methods.
974
975// Return the data size. For an input section we store the size here.
976// For an Output_section_data, we have to ask it for the size.
977
978off_t
979Output_section::Input_section::data_size() const
980{
981 if (this->is_input_section())
b8e6aad9 982 return this->u1_.data_size;
ead1e424 983 else
b8e6aad9 984 return this->u2_.posd->data_size();
ead1e424
ILT
985}
986
987// Set the address and file offset.
988
989void
990Output_section::Input_section::set_address(uint64_t addr, off_t off,
991 off_t secoff)
992{
993 if (this->is_input_section())
b8e6aad9 994 this->u2_.object->set_section_offset(this->shndx_, off - secoff);
ead1e424 995 else
27bc2bce 996 this->u2_.posd->set_address_and_file_offset(addr, off);
b8e6aad9
ILT
997}
998
730cdc88 999// Try to turn an input offset into an output offset.
b8e6aad9
ILT
1000
1001bool
730cdc88
ILT
1002Output_section::Input_section::output_offset(const Relobj* object,
1003 unsigned int shndx,
1004 off_t offset,
1005 off_t *poutput) const
b8e6aad9
ILT
1006{
1007 if (!this->is_input_section())
730cdc88 1008 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
b8e6aad9
ILT
1009 else
1010 {
730cdc88 1011 if (this->shndx_ != shndx || this->u2_.object != object)
b8e6aad9
ILT
1012 return false;
1013 off_t output_offset;
1014 Output_section* os = object->output_section(shndx, &output_offset);
1015 gold_assert(os != NULL);
730cdc88
ILT
1016 gold_assert(output_offset != -1);
1017 *poutput = output_offset + offset;
b8e6aad9
ILT
1018 return true;
1019 }
ead1e424
ILT
1020}
1021
1022// Write out the data. We don't have to do anything for an input
1023// section--they are handled via Object::relocate--but this is where
1024// we write out the data for an Output_section_data.
1025
1026void
1027Output_section::Input_section::write(Output_file* of)
1028{
1029 if (!this->is_input_section())
b8e6aad9 1030 this->u2_.posd->write(of);
ead1e424
ILT
1031}
1032
a2fb1b05
ILT
1033// Output_section methods.
1034
1035// Construct an Output_section. NAME will point into a Stringpool.
1036
9a0910c3
ILT
1037Output_section::Output_section(const General_options& options,
1038 const char* name, elfcpp::Elf_Word type,
b8e6aad9 1039 elfcpp::Elf_Xword flags)
9a0910c3
ILT
1040 : options_(options),
1041 name_(name),
a2fb1b05
ILT
1042 addralign_(0),
1043 entsize_(0),
16649710 1044 link_section_(NULL),
a2fb1b05 1045 link_(0),
16649710 1046 info_section_(NULL),
a2fb1b05
ILT
1047 info_(0),
1048 type_(type),
61ba1cf9 1049 flags_(flags),
91ea499d 1050 out_shndx_(-1U),
c06b7b0b
ILT
1051 symtab_index_(0),
1052 dynsym_index_(0),
ead1e424
ILT
1053 input_sections_(),
1054 first_input_offset_(0),
c51e6221 1055 fills_(),
a3ad94ed 1056 needs_symtab_index_(false),
16649710
ILT
1057 needs_dynsym_index_(false),
1058 should_link_to_symtab_(false),
730cdc88 1059 should_link_to_dynsym_(false),
27bc2bce
ILT
1060 after_input_sections_(false),
1061 requires_postprocessing_(false)
a2fb1b05 1062{
27bc2bce
ILT
1063 // An unallocated section has no address. Forcing this means that
1064 // we don't need special treatment for symbols defined in debug
1065 // sections.
1066 if ((flags & elfcpp::SHF_ALLOC) == 0)
1067 this->set_address(0);
a2fb1b05
ILT
1068}
1069
54dc6425
ILT
1070Output_section::~Output_section()
1071{
1072}
1073
16649710
ILT
1074// Set the entry size.
1075
1076void
1077Output_section::set_entsize(uint64_t v)
1078{
1079 if (this->entsize_ == 0)
1080 this->entsize_ = v;
1081 else
1082 gold_assert(this->entsize_ == v);
1083}
1084
9a0910c3
ILT
1085// Sometimes we compress sections. This is typically done for
1086// sections that are not part of normal program execution (such as
1087// .debug_* sections), and where the readers of these sections know
1088// how to deal with compressed sections. (To make it easier for them,
1089// we will rename the ouput section in such cases from .foo to
1090// .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
1091// doesn't say for certain whether we'll compress -- it depends on
1092// commandline options as well -- just whether this section is a
1093// candidate for compression.
1094
1095static bool
1096is_compressible_section(const char* secname)
1097{
1098 return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
1099}
1100
ead1e424 1101// Add the input section SHNDX, with header SHDR, named SECNAME, in
730cdc88
ILT
1102// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1103// relocation section which applies to this section, or 0 if none, or
1104// -1U if more than one. Return the offset of the input section
1105// within the output section. Return -1 if the input section will
1106// receive special handling. In the normal case we don't always keep
1107// track of input sections for an Output_section. Instead, each
1108// Object keeps track of the Output_section for each of its input
1109// sections.
a2fb1b05
ILT
1110
1111template<int size, bool big_endian>
1112off_t
730cdc88
ILT
1113Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1114 unsigned int shndx,
ead1e424 1115 const char* secname,
730cdc88
ILT
1116 const elfcpp::Shdr<size, big_endian>& shdr,
1117 unsigned int reloc_shndx)
a2fb1b05
ILT
1118{
1119 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1120 if ((addralign & (addralign - 1)) != 0)
1121 {
75f2446e
ILT
1122 object->error(_("invalid alignment %lu for section \"%s\""),
1123 static_cast<unsigned long>(addralign), secname);
1124 addralign = 1;
a2fb1b05 1125 }
a2fb1b05
ILT
1126
1127 if (addralign > this->addralign_)
1128 this->addralign_ = addralign;
1129
44a43cf9 1130 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
4f833eee 1131 uint64_t entsize = shdr.get_sh_entsize();
44a43cf9
ILT
1132
1133 // .debug_str is a mergeable string section, but is not always so
1134 // marked by compilers. Mark manually here so we can optimize.
1135 if (strcmp(secname, ".debug_str") == 0)
4f833eee
ILT
1136 {
1137 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1138 entsize = 1;
1139 }
44a43cf9 1140
b8e6aad9 1141 // If this is a SHF_MERGE section, we pass all the input sections to
730cdc88
ILT
1142 // a Output_data_merge. We don't try to handle relocations for such
1143 // a section.
44a43cf9 1144 if ((sh_flags & elfcpp::SHF_MERGE) != 0
730cdc88 1145 && reloc_shndx == 0)
b8e6aad9 1146 {
44a43cf9 1147 if (this->add_merge_input_section(object, shndx, sh_flags,
9a0910c3
ILT
1148 entsize, addralign,
1149 is_compressible_section(secname)))
b8e6aad9
ILT
1150 {
1151 // Tell the relocation routines that they need to call the
730cdc88 1152 // output_offset method to determine the final address.
b8e6aad9
ILT
1153 return -1;
1154 }
1155 }
1156
27bc2bce 1157 off_t offset_in_section = this->current_data_size_for_child();
c51e6221
ILT
1158 off_t aligned_offset_in_section = align_address(offset_in_section,
1159 addralign);
1160
1161 if (aligned_offset_in_section > offset_in_section
44a43cf9 1162 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
c51e6221
ILT
1163 && object->target()->has_code_fill())
1164 {
1165 // We need to add some fill data. Using fill_list_ when
1166 // possible is an optimization, since we will often have fill
1167 // sections without input sections.
1168 off_t fill_len = aligned_offset_in_section - offset_in_section;
1169 if (this->input_sections_.empty())
1170 this->fills_.push_back(Fill(offset_in_section, fill_len));
1171 else
1172 {
1173 // FIXME: When relaxing, the size needs to adjust to
1174 // maintain a constant alignment.
1175 std::string fill_data(object->target()->code_fill(fill_len));
1176 Output_data_const* odc = new Output_data_const(fill_data, 1);
1177 this->input_sections_.push_back(Input_section(odc));
1178 }
1179 }
1180
27bc2bce
ILT
1181 this->set_current_data_size_for_child(aligned_offset_in_section
1182 + shdr.get_sh_size());
a2fb1b05 1183
ead1e424
ILT
1184 // We need to keep track of this section if we are already keeping
1185 // track of sections, or if we are relaxing. FIXME: Add test for
1186 // relaxing.
c51e6221 1187 if (!this->input_sections_.empty())
ead1e424
ILT
1188 this->input_sections_.push_back(Input_section(object, shndx,
1189 shdr.get_sh_size(),
1190 addralign));
54dc6425 1191
c51e6221 1192 return aligned_offset_in_section;
61ba1cf9
ILT
1193}
1194
ead1e424
ILT
1195// Add arbitrary data to an output section.
1196
1197void
1198Output_section::add_output_section_data(Output_section_data* posd)
1199{
b8e6aad9
ILT
1200 Input_section inp(posd);
1201 this->add_output_section_data(&inp);
1202}
1203
1204// Add arbitrary data to an output section by Input_section.
c06b7b0b 1205
b8e6aad9
ILT
1206void
1207Output_section::add_output_section_data(Input_section* inp)
1208{
ead1e424 1209 if (this->input_sections_.empty())
27bc2bce 1210 this->first_input_offset_ = this->current_data_size_for_child();
c06b7b0b 1211
b8e6aad9 1212 this->input_sections_.push_back(*inp);
c06b7b0b 1213
b8e6aad9 1214 uint64_t addralign = inp->addralign();
ead1e424
ILT
1215 if (addralign > this->addralign_)
1216 this->addralign_ = addralign;
c06b7b0b 1217
b8e6aad9
ILT
1218 inp->set_output_section(this);
1219}
1220
1221// Add a merge section to an output section.
1222
1223void
1224Output_section::add_output_merge_section(Output_section_data* posd,
1225 bool is_string, uint64_t entsize)
1226{
1227 Input_section inp(posd, is_string, entsize);
1228 this->add_output_section_data(&inp);
1229}
1230
1231// Add an input section to a SHF_MERGE section.
1232
1233bool
1234Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1235 uint64_t flags, uint64_t entsize,
9a0910c3
ILT
1236 uint64_t addralign,
1237 bool is_compressible_section)
b8e6aad9 1238{
87f95776
ILT
1239 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1240
1241 // We only merge strings if the alignment is not more than the
1242 // character size. This could be handled, but it's unusual.
1243 if (is_string && addralign > entsize)
b8e6aad9
ILT
1244 return false;
1245
b8e6aad9
ILT
1246 Input_section_list::iterator p;
1247 for (p = this->input_sections_.begin();
1248 p != this->input_sections_.end();
1249 ++p)
87f95776 1250 if (p->is_merge_section(is_string, entsize, addralign))
9a0910c3
ILT
1251 {
1252 p->add_input_section(object, shndx);
1253 return true;
1254 }
b8e6aad9
ILT
1255
1256 // We handle the actual constant merging in Output_merge_data or
1257 // Output_merge_string_data.
9a0910c3
ILT
1258 Output_section_data* posd;
1259 if (!is_string)
1260 posd = new Output_merge_data(entsize, addralign);
1261 else if (is_compressible_section && options_.compress_debug_sections())
1262 {
1263 switch (entsize)
1264 {
1265 case 1:
1266 posd = new Output_compressed_string<char>(addralign, this->options_);
1267 break;
1268 case 2:
1269 posd = new Output_compressed_string<uint16_t>(addralign,
1270 this->options_);
1271 break;
1272 case 4:
1273 posd = new Output_compressed_string<uint32_t>(addralign,
1274 this->options_);
1275 break;
1276 default:
1277 return false;
1278 }
1279 }
b8e6aad9
ILT
1280 else
1281 {
9a0910c3
ILT
1282 switch (entsize)
1283 {
1284 case 1:
1285 posd = new Output_merge_string<char>(addralign);
1286 break;
1287 case 2:
1288 posd = new Output_merge_string<uint16_t>(addralign);
1289 break;
1290 case 4:
1291 posd = new Output_merge_string<uint32_t>(addralign);
1292 break;
1293 default:
1294 return false;
1295 }
b8e6aad9
ILT
1296 }
1297
9a0910c3
ILT
1298 this->add_output_merge_section(posd, is_string, entsize);
1299 posd->add_input_section(object, shndx);
1300
b8e6aad9
ILT
1301 return true;
1302}
1303
730cdc88
ILT
1304// Given an address OFFSET relative to the start of input section
1305// SHNDX in OBJECT, return whether this address is being included in
1306// the final link. This should only be called if SHNDX in OBJECT has
1307// a special mapping.
1308
1309bool
1310Output_section::is_input_address_mapped(const Relobj* object,
1311 unsigned int shndx,
1312 off_t offset) const
1313{
1314 gold_assert(object->is_section_specially_mapped(shndx));
1315
1316 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1317 p != this->input_sections_.end();
1318 ++p)
1319 {
1320 off_t output_offset;
1321 if (p->output_offset(object, shndx, offset, &output_offset))
1322 return output_offset != -1;
1323 }
1324
1325 // By default we assume that the address is mapped. This should
1326 // only be called after we have passed all sections to Layout. At
1327 // that point we should know what we are discarding.
1328 return true;
1329}
1330
1331// Given an address OFFSET relative to the start of input section
1332// SHNDX in object OBJECT, return the output offset relative to the
1333// start of the section. This should only be called if SHNDX in
1334// OBJECT has a special mapping.
1335
1336off_t
1337Output_section::output_offset(const Relobj* object, unsigned int shndx,
1338 off_t offset) const
1339{
1340 gold_assert(object->is_section_specially_mapped(shndx));
1341 // This can only be called meaningfully when layout is complete.
1342 gold_assert(Output_data::is_layout_complete());
1343
1344 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1345 p != this->input_sections_.end();
1346 ++p)
1347 {
1348 off_t output_offset;
1349 if (p->output_offset(object, shndx, offset, &output_offset))
1350 return output_offset;
1351 }
1352 gold_unreachable();
1353}
1354
b8e6aad9
ILT
1355// Return the output virtual address of OFFSET relative to the start
1356// of input section SHNDX in object OBJECT.
1357
1358uint64_t
1359Output_section::output_address(const Relobj* object, unsigned int shndx,
1360 off_t offset) const
1361{
730cdc88
ILT
1362 gold_assert(object->is_section_specially_mapped(shndx));
1363 // This can only be called meaningfully when layout is complete.
1364 gold_assert(Output_data::is_layout_complete());
1365
b8e6aad9
ILT
1366 uint64_t addr = this->address() + this->first_input_offset_;
1367 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1368 p != this->input_sections_.end();
1369 ++p)
1370 {
1371 addr = align_address(addr, p->addralign());
730cdc88
ILT
1372 off_t output_offset;
1373 if (p->output_offset(object, shndx, offset, &output_offset))
1374 {
1375 if (output_offset == -1)
1376 return -1U;
1377 return addr + output_offset;
1378 }
b8e6aad9
ILT
1379 addr += p->data_size();
1380 }
1381
1382 // If we get here, it means that we don't know the mapping for this
1383 // input section. This might happen in principle if
1384 // add_input_section were called before add_output_section_data.
1385 // But it should never actually happen.
1386
1387 gold_unreachable();
ead1e424
ILT
1388}
1389
27bc2bce 1390// Set the data size of an Output_section. This is where we handle
ead1e424
ILT
1391// setting the addresses of any Output_section_data objects.
1392
1393void
27bc2bce 1394Output_section::set_final_data_size()
ead1e424
ILT
1395{
1396 if (this->input_sections_.empty())
27bc2bce
ILT
1397 {
1398 this->set_data_size(this->current_data_size_for_child());
1399 return;
1400 }
ead1e424 1401
27bc2bce
ILT
1402 uint64_t address = this->address();
1403 off_t startoff = this->offset();
ead1e424
ILT
1404 off_t off = startoff + this->first_input_offset_;
1405 for (Input_section_list::iterator p = this->input_sections_.begin();
1406 p != this->input_sections_.end();
1407 ++p)
1408 {
1409 off = align_address(off, p->addralign());
1410 p->set_address(address + (off - startoff), off, startoff);
1411 off += p->data_size();
1412 }
1413
1414 this->set_data_size(off - startoff);
1415}
9a0910c3
ILT
1416
1417// Ask each output_section_data member if it wants to change the name
1418// of the output section. If any of them says yes, use this to set
1419// the new name. This should be called after all processing of this
1420// output section is done, but before the name is finally committed to
1421// the output-section's header.
1422
1423bool
1424Output_section::maybe_modify_output_section_name()
1425{
1426 for (Input_section_list::const_iterator it = input_sections_.begin();
1427 it != input_sections_.end();
1428 ++it)
1429 {
1430 const char* newname = it->modified_output_section_name(this->name());
1431 if (newname != NULL)
1432 {
1433 this->set_name(newname);
1434 return true;
1435 }
1436 }
1437 return false;
1438}
ead1e424 1439
61ba1cf9
ILT
1440// Write the section header to *OSHDR.
1441
1442template<int size, bool big_endian>
1443void
16649710
ILT
1444Output_section::write_header(const Layout* layout,
1445 const Stringpool* secnamepool,
61ba1cf9
ILT
1446 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1447{
1448 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1449 oshdr->put_sh_type(this->type_);
1450 oshdr->put_sh_flags(this->flags_);
1451 oshdr->put_sh_addr(this->address());
1452 oshdr->put_sh_offset(this->offset());
1453 oshdr->put_sh_size(this->data_size());
16649710
ILT
1454 if (this->link_section_ != NULL)
1455 oshdr->put_sh_link(this->link_section_->out_shndx());
1456 else if (this->should_link_to_symtab_)
1457 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1458 else if (this->should_link_to_dynsym_)
1459 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1460 else
1461 oshdr->put_sh_link(this->link_);
1462 if (this->info_section_ != NULL)
1463 oshdr->put_sh_info(this->info_section_->out_shndx());
1464 else
1465 oshdr->put_sh_info(this->info_);
61ba1cf9
ILT
1466 oshdr->put_sh_addralign(this->addralign_);
1467 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
1468}
1469
ead1e424
ILT
1470// Write out the data. For input sections the data is written out by
1471// Object::relocate, but we have to handle Output_section_data objects
1472// here.
1473
1474void
1475Output_section::do_write(Output_file* of)
1476{
c51e6221
ILT
1477 off_t output_section_file_offset = this->offset();
1478 for (Fill_list::iterator p = this->fills_.begin();
1479 p != this->fills_.end();
1480 ++p)
1481 {
1482 std::string fill_data(of->target()->code_fill(p->length()));
1483 of->write(output_section_file_offset + p->section_offset(),
1484 fill_data.data(), fill_data.size());
1485 }
1486
ead1e424
ILT
1487 for (Input_section_list::iterator p = this->input_sections_.begin();
1488 p != this->input_sections_.end();
1489 ++p)
1490 p->write(of);
1491}
1492
a2fb1b05
ILT
1493// Output segment methods.
1494
1495Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
54dc6425 1496 : output_data_(),
75f65a3e 1497 output_bss_(),
a2fb1b05
ILT
1498 vaddr_(0),
1499 paddr_(0),
1500 memsz_(0),
1501 align_(0),
1502 offset_(0),
1503 filesz_(0),
1504 type_(type),
ead1e424
ILT
1505 flags_(flags),
1506 is_align_known_(false)
a2fb1b05
ILT
1507{
1508}
1509
1510// Add an Output_section to an Output_segment.
1511
1512void
75f65a3e 1513Output_segment::add_output_section(Output_section* os,
dbe717ef
ILT
1514 elfcpp::Elf_Word seg_flags,
1515 bool front)
a2fb1b05 1516{
a3ad94ed
ILT
1517 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1518 gold_assert(!this->is_align_known_);
75f65a3e 1519
ead1e424 1520 // Update the segment flags.
75f65a3e 1521 this->flags_ |= seg_flags;
75f65a3e
ILT
1522
1523 Output_segment::Output_data_list* pdl;
1524 if (os->type() == elfcpp::SHT_NOBITS)
1525 pdl = &this->output_bss_;
1526 else
1527 pdl = &this->output_data_;
54dc6425 1528
a2fb1b05
ILT
1529 // So that PT_NOTE segments will work correctly, we need to ensure
1530 // that all SHT_NOTE sections are adjacent. This will normally
1531 // happen automatically, because all the SHT_NOTE input sections
1532 // will wind up in the same output section. However, it is possible
1533 // for multiple SHT_NOTE input sections to have different section
1534 // flags, and thus be in different output sections, but for the
1535 // different section flags to map into the same segment flags and
1536 // thus the same output segment.
54dc6425
ILT
1537
1538 // Note that while there may be many input sections in an output
1539 // section, there are normally only a few output sections in an
1540 // output segment. This loop is expected to be fast.
1541
61ba1cf9 1542 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
a2fb1b05 1543 {
a3ad94ed 1544 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 1545 do
54dc6425 1546 {
75f65a3e 1547 --p;
54dc6425
ILT
1548 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1549 {
dbe717ef 1550 // We don't worry about the FRONT parameter.
54dc6425 1551 ++p;
75f65a3e 1552 pdl->insert(p, os);
54dc6425
ILT
1553 return;
1554 }
1555 }
75f65a3e 1556 while (p != pdl->begin());
54dc6425
ILT
1557 }
1558
1559 // Similarly, so that PT_TLS segments will work, we need to group
75f65a3e
ILT
1560 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1561 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1562 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
07f397ab
ILT
1563 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
1564 // and the PT_TLS segment -- we do this grouping only for the
1565 // PT_LOAD segment.
1566 if (this->type_ != elfcpp::PT_TLS
1567 && (os->flags() & elfcpp::SHF_TLS) != 0
1568 && !this->output_data_.empty())
54dc6425 1569 {
75f65a3e
ILT
1570 pdl = &this->output_data_;
1571 bool nobits = os->type() == elfcpp::SHT_NOBITS;
ead1e424 1572 bool sawtls = false;
a3ad94ed 1573 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 1574 do
a2fb1b05 1575 {
75f65a3e 1576 --p;
ead1e424
ILT
1577 bool insert;
1578 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1579 {
1580 sawtls = true;
1581 // Put a NOBITS section after the first TLS section.
1582 // But a PROGBITS section after the first TLS/PROGBITS
1583 // section.
1584 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1585 }
1586 else
1587 {
1588 // If we've gone past the TLS sections, but we've seen a
1589 // TLS section, then we need to insert this section now.
1590 insert = sawtls;
1591 }
1592
1593 if (insert)
a2fb1b05 1594 {
dbe717ef 1595 // We don't worry about the FRONT parameter.
a2fb1b05 1596 ++p;
75f65a3e 1597 pdl->insert(p, os);
a2fb1b05
ILT
1598 return;
1599 }
1600 }
75f65a3e 1601 while (p != pdl->begin());
ead1e424 1602
dbe717ef
ILT
1603 // There are no TLS sections yet; put this one at the requested
1604 // location in the section list.
a2fb1b05
ILT
1605 }
1606
dbe717ef
ILT
1607 if (front)
1608 pdl->push_front(os);
1609 else
1610 pdl->push_back(os);
75f65a3e
ILT
1611}
1612
1613// Add an Output_data (which is not an Output_section) to the start of
1614// a segment.
1615
1616void
1617Output_segment::add_initial_output_data(Output_data* od)
1618{
a3ad94ed 1619 gold_assert(!this->is_align_known_);
75f65a3e
ILT
1620 this->output_data_.push_front(od);
1621}
1622
1623// Return the maximum alignment of the Output_data in Output_segment.
ead1e424 1624// Once we compute this, we prohibit new sections from being added.
75f65a3e
ILT
1625
1626uint64_t
ead1e424 1627Output_segment::addralign()
75f65a3e 1628{
ead1e424
ILT
1629 if (!this->is_align_known_)
1630 {
1631 uint64_t addralign;
1632
1633 addralign = Output_segment::maximum_alignment(&this->output_data_);
1634 if (addralign > this->align_)
1635 this->align_ = addralign;
1636
1637 addralign = Output_segment::maximum_alignment(&this->output_bss_);
1638 if (addralign > this->align_)
1639 this->align_ = addralign;
1640
1641 this->is_align_known_ = true;
1642 }
1643
75f65a3e
ILT
1644 return this->align_;
1645}
1646
ead1e424
ILT
1647// Return the maximum alignment of a list of Output_data.
1648
1649uint64_t
1650Output_segment::maximum_alignment(const Output_data_list* pdl)
1651{
1652 uint64_t ret = 0;
1653 for (Output_data_list::const_iterator p = pdl->begin();
1654 p != pdl->end();
1655 ++p)
1656 {
1657 uint64_t addralign = (*p)->addralign();
1658 if (addralign > ret)
1659 ret = addralign;
1660 }
1661 return ret;
1662}
1663
4f4c5f80
ILT
1664// Return the number of dynamic relocs applied to this segment.
1665
1666unsigned int
1667Output_segment::dynamic_reloc_count() const
1668{
1669 return (this->dynamic_reloc_count_list(&this->output_data_)
1670 + this->dynamic_reloc_count_list(&this->output_bss_));
1671}
1672
1673// Return the number of dynamic relocs applied to an Output_data_list.
1674
1675unsigned int
1676Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
1677{
1678 unsigned int count = 0;
1679 for (Output_data_list::const_iterator p = pdl->begin();
1680 p != pdl->end();
1681 ++p)
1682 count += (*p)->dynamic_reloc_count();
1683 return count;
1684}
1685
75f65a3e 1686// Set the section addresses for an Output_segment. ADDR is the
ead1e424
ILT
1687// address and *POFF is the file offset. Set the section indexes
1688// starting with *PSHNDX. Return the address of the immediately
1689// following segment. Update *POFF and *PSHNDX.
75f65a3e
ILT
1690
1691uint64_t
ead1e424
ILT
1692Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
1693 unsigned int* pshndx)
75f65a3e 1694{
a3ad94ed 1695 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e
ILT
1696
1697 this->vaddr_ = addr;
1698 this->paddr_ = addr;
1699
1700 off_t orig_off = *poff;
1701 this->offset_ = orig_off;
1702
ead1e424
ILT
1703 *poff = align_address(*poff, this->addralign());
1704
1705 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
1706 pshndx);
75f65a3e
ILT
1707 this->filesz_ = *poff - orig_off;
1708
1709 off_t off = *poff;
1710
61ba1cf9 1711 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
ead1e424 1712 poff, pshndx);
75f65a3e
ILT
1713 this->memsz_ = *poff - orig_off;
1714
1715 // Ignore the file offset adjustments made by the BSS Output_data
1716 // objects.
1717 *poff = off;
61ba1cf9
ILT
1718
1719 return ret;
75f65a3e
ILT
1720}
1721
b8e6aad9
ILT
1722// Set the addresses and file offsets in a list of Output_data
1723// structures.
75f65a3e
ILT
1724
1725uint64_t
1726Output_segment::set_section_list_addresses(Output_data_list* pdl,
ead1e424
ILT
1727 uint64_t addr, off_t* poff,
1728 unsigned int* pshndx)
75f65a3e 1729{
ead1e424 1730 off_t startoff = *poff;
75f65a3e 1731
ead1e424 1732 off_t off = startoff;
75f65a3e
ILT
1733 for (Output_data_list::iterator p = pdl->begin();
1734 p != pdl->end();
1735 ++p)
1736 {
ead1e424 1737 off = align_address(off, (*p)->addralign());
27bc2bce 1738 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
ead1e424
ILT
1739
1740 // Unless this is a PT_TLS segment, we want to ignore the size
1741 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
1742 // affect the size of a PT_LOAD segment.
1743 if (this->type_ == elfcpp::PT_TLS
1744 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
1745 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
1746 off += (*p)->data_size();
75f65a3e 1747
ead1e424
ILT
1748 if ((*p)->is_section())
1749 {
1750 (*p)->set_out_shndx(*pshndx);
1751 ++*pshndx;
1752 }
75f65a3e
ILT
1753 }
1754
1755 *poff = off;
ead1e424 1756 return addr + (off - startoff);
75f65a3e
ILT
1757}
1758
1759// For a non-PT_LOAD segment, set the offset from the sections, if
1760// any.
1761
1762void
1763Output_segment::set_offset()
1764{
a3ad94ed 1765 gold_assert(this->type_ != elfcpp::PT_LOAD);
75f65a3e
ILT
1766
1767 if (this->output_data_.empty() && this->output_bss_.empty())
1768 {
1769 this->vaddr_ = 0;
1770 this->paddr_ = 0;
1771 this->memsz_ = 0;
1772 this->align_ = 0;
1773 this->offset_ = 0;
1774 this->filesz_ = 0;
1775 return;
1776 }
1777
1778 const Output_data* first;
1779 if (this->output_data_.empty())
1780 first = this->output_bss_.front();
1781 else
1782 first = this->output_data_.front();
1783 this->vaddr_ = first->address();
1784 this->paddr_ = this->vaddr_;
1785 this->offset_ = first->offset();
1786
1787 if (this->output_data_.empty())
1788 this->filesz_ = 0;
1789 else
1790 {
1791 const Output_data* last_data = this->output_data_.back();
1792 this->filesz_ = (last_data->address()
1793 + last_data->data_size()
1794 - this->vaddr_);
1795 }
1796
1797 const Output_data* last;
1798 if (this->output_bss_.empty())
1799 last = this->output_data_.back();
1800 else
1801 last = this->output_bss_.back();
1802 this->memsz_ = (last->address()
1803 + last->data_size()
1804 - this->vaddr_);
75f65a3e
ILT
1805}
1806
1807// Return the number of Output_sections in an Output_segment.
1808
1809unsigned int
1810Output_segment::output_section_count() const
1811{
1812 return (this->output_section_count_list(&this->output_data_)
1813 + this->output_section_count_list(&this->output_bss_));
1814}
1815
1816// Return the number of Output_sections in an Output_data_list.
1817
1818unsigned int
1819Output_segment::output_section_count_list(const Output_data_list* pdl) const
1820{
1821 unsigned int count = 0;
1822 for (Output_data_list::const_iterator p = pdl->begin();
1823 p != pdl->end();
1824 ++p)
1825 {
1826 if ((*p)->is_section())
1827 ++count;
1828 }
1829 return count;
a2fb1b05
ILT
1830}
1831
61ba1cf9
ILT
1832// Write the segment data into *OPHDR.
1833
1834template<int size, bool big_endian>
1835void
ead1e424 1836Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
61ba1cf9
ILT
1837{
1838 ophdr->put_p_type(this->type_);
1839 ophdr->put_p_offset(this->offset_);
1840 ophdr->put_p_vaddr(this->vaddr_);
1841 ophdr->put_p_paddr(this->paddr_);
1842 ophdr->put_p_filesz(this->filesz_);
1843 ophdr->put_p_memsz(this->memsz_);
1844 ophdr->put_p_flags(this->flags_);
ead1e424 1845 ophdr->put_p_align(this->addralign());
61ba1cf9
ILT
1846}
1847
1848// Write the section headers into V.
1849
1850template<int size, bool big_endian>
1851unsigned char*
16649710
ILT
1852Output_segment::write_section_headers(const Layout* layout,
1853 const Stringpool* secnamepool,
ead1e424
ILT
1854 unsigned char* v,
1855 unsigned int *pshndx
5482377d
ILT
1856 ACCEPT_SIZE_ENDIAN) const
1857{
ead1e424
ILT
1858 // Every section that is attached to a segment must be attached to a
1859 // PT_LOAD segment, so we only write out section headers for PT_LOAD
1860 // segments.
1861 if (this->type_ != elfcpp::PT_LOAD)
1862 return v;
1863
593f47df
ILT
1864 v = this->write_section_headers_list
1865 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
16649710 1866 layout, secnamepool, &this->output_data_, v, pshndx
593f47df
ILT
1867 SELECT_SIZE_ENDIAN(size, big_endian));
1868 v = this->write_section_headers_list
1869 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
16649710 1870 layout, secnamepool, &this->output_bss_, v, pshndx
593f47df 1871 SELECT_SIZE_ENDIAN(size, big_endian));
61ba1cf9
ILT
1872 return v;
1873}
1874
1875template<int size, bool big_endian>
1876unsigned char*
16649710
ILT
1877Output_segment::write_section_headers_list(const Layout* layout,
1878 const Stringpool* secnamepool,
61ba1cf9 1879 const Output_data_list* pdl,
ead1e424
ILT
1880 unsigned char* v,
1881 unsigned int* pshndx
5482377d 1882 ACCEPT_SIZE_ENDIAN) const
61ba1cf9
ILT
1883{
1884 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1885 for (Output_data_list::const_iterator p = pdl->begin();
1886 p != pdl->end();
1887 ++p)
1888 {
1889 if ((*p)->is_section())
1890 {
5482377d 1891 const Output_section* ps = static_cast<const Output_section*>(*p);
a3ad94ed 1892 gold_assert(*pshndx == ps->out_shndx());
61ba1cf9 1893 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 1894 ps->write_header(layout, secnamepool, &oshdr);
61ba1cf9 1895 v += shdr_size;
ead1e424 1896 ++*pshndx;
61ba1cf9
ILT
1897 }
1898 }
1899 return v;
1900}
1901
a2fb1b05
ILT
1902// Output_file methods.
1903
c51e6221 1904Output_file::Output_file(const General_options& options, Target* target)
61ba1cf9 1905 : options_(options),
c51e6221 1906 target_(target),
61ba1cf9
ILT
1907 name_(options.output_file_name()),
1908 o_(-1),
1909 file_size_(0),
1910 base_(NULL)
1911{
1912}
1913
1914// Open the output file.
1915
a2fb1b05 1916void
61ba1cf9 1917Output_file::open(off_t file_size)
a2fb1b05 1918{
61ba1cf9
ILT
1919 this->file_size_ = file_size;
1920
4e9d8586
ILT
1921 // Unlink the file first; otherwise the open() may fail if the file
1922 // is busy (e.g. it's an executable that's currently being executed).
1923 //
1924 // However, the linker may be part of a system where a zero-length
1925 // file is created for it to write to, with tight permissions (gcc
1926 // 2.95 did something like this). Unlinking the file would work
1927 // around those permission controls, so we only unlink if the file
1928 // has a non-zero size. We also unlink only regular files to avoid
1929 // trouble with directories/etc.
1930 //
1931 // If we fail, continue; this command is merely a best-effort attempt
1932 // to improve the odds for open().
1933
4e9d8586 1934 struct stat s;
5ffcaa86
ILT
1935 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
1936 unlink_if_ordinary(this->name_);
4e9d8586 1937
7e1edb90 1938 int mode = parameters->output_is_object() ? 0666 : 0777;
61ba1cf9
ILT
1939 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1940 if (o < 0)
75f2446e 1941 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
1942 this->o_ = o;
1943
27bc2bce
ILT
1944 this->map();
1945}
1946
1947// Resize the output file.
1948
1949void
1950Output_file::resize(off_t file_size)
1951{
1952 if (::munmap(this->base_, this->file_size_) < 0)
1953 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
1954 this->file_size_ = file_size;
1955 this->map();
1956}
1957
1958// Map the file into memory.
1959
1960void
1961Output_file::map()
1962{
1963 int o = this->o_;
1964
61ba1cf9 1965 // Write out one byte to make the file the right size.
27bc2bce 1966 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
75f2446e 1967 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
1968 char b = 0;
1969 if (::write(o, &b, 1) != 1)
75f2446e 1970 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
1971
1972 // Map the file into memory.
27bc2bce 1973 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
61ba1cf9
ILT
1974 MAP_SHARED, o, 0);
1975 if (base == MAP_FAILED)
75f2446e 1976 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
1977 this->base_ = static_cast<unsigned char*>(base);
1978}
1979
1980// Close the output file.
1981
1982void
1983Output_file::close()
1984{
1985 if (::munmap(this->base_, this->file_size_) < 0)
a0c4fb0a 1986 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
1987 this->base_ = NULL;
1988
1989 if (::close(this->o_) < 0)
75f2446e 1990 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
61ba1cf9 1991 this->o_ = -1;
a2fb1b05
ILT
1992}
1993
1994// Instantiate the templates we need. We could use the configure
1995// script to restrict this to only the ones for implemented targets.
1996
193a53d9 1997#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
1998template
1999off_t
2000Output_section::add_input_section<32, false>(
730cdc88 2001 Sized_relobj<32, false>* object,
ead1e424 2002 unsigned int shndx,
a2fb1b05 2003 const char* secname,
730cdc88
ILT
2004 const elfcpp::Shdr<32, false>& shdr,
2005 unsigned int reloc_shndx);
193a53d9 2006#endif
a2fb1b05 2007
193a53d9 2008#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
2009template
2010off_t
2011Output_section::add_input_section<32, true>(
730cdc88 2012 Sized_relobj<32, true>* object,
ead1e424 2013 unsigned int shndx,
a2fb1b05 2014 const char* secname,
730cdc88
ILT
2015 const elfcpp::Shdr<32, true>& shdr,
2016 unsigned int reloc_shndx);
193a53d9 2017#endif
a2fb1b05 2018
193a53d9 2019#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
2020template
2021off_t
2022Output_section::add_input_section<64, false>(
730cdc88 2023 Sized_relobj<64, false>* object,
ead1e424 2024 unsigned int shndx,
a2fb1b05 2025 const char* secname,
730cdc88
ILT
2026 const elfcpp::Shdr<64, false>& shdr,
2027 unsigned int reloc_shndx);
193a53d9 2028#endif
a2fb1b05 2029
193a53d9 2030#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
2031template
2032off_t
2033Output_section::add_input_section<64, true>(
730cdc88 2034 Sized_relobj<64, true>* object,
ead1e424 2035 unsigned int shndx,
a2fb1b05 2036 const char* secname,
730cdc88
ILT
2037 const elfcpp::Shdr<64, true>& shdr,
2038 unsigned int reloc_shndx);
193a53d9 2039#endif
a2fb1b05 2040
193a53d9 2041#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
2042template
2043class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
193a53d9 2044#endif
c06b7b0b 2045
193a53d9 2046#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
2047template
2048class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
193a53d9 2049#endif
c06b7b0b 2050
193a53d9 2051#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
2052template
2053class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
193a53d9 2054#endif
c06b7b0b 2055
193a53d9 2056#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
2057template
2058class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
193a53d9 2059#endif
c06b7b0b 2060
193a53d9 2061#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
2062template
2063class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
193a53d9 2064#endif
c06b7b0b 2065
193a53d9 2066#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
2067template
2068class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
193a53d9 2069#endif
c06b7b0b 2070
193a53d9 2071#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
2072template
2073class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
193a53d9 2074#endif
c06b7b0b 2075
193a53d9 2076#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
2077template
2078class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
193a53d9 2079#endif
c06b7b0b 2080
193a53d9 2081#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
2082template
2083class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
193a53d9 2084#endif
c06b7b0b 2085
193a53d9 2086#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
2087template
2088class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
193a53d9 2089#endif
c06b7b0b 2090
193a53d9 2091#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
2092template
2093class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
193a53d9 2094#endif
c06b7b0b 2095
193a53d9 2096#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
2097template
2098class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
193a53d9 2099#endif
c06b7b0b 2100
193a53d9 2101#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
2102template
2103class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
193a53d9 2104#endif
c06b7b0b 2105
193a53d9 2106#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
2107template
2108class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
193a53d9 2109#endif
c06b7b0b 2110
193a53d9 2111#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
2112template
2113class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
193a53d9 2114#endif
c06b7b0b 2115
193a53d9 2116#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
2117template
2118class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
193a53d9 2119#endif
c06b7b0b 2120
193a53d9 2121#ifdef HAVE_TARGET_32_LITTLE
ead1e424 2122template
dbe717ef 2123class Output_data_got<32, false>;
193a53d9 2124#endif
ead1e424 2125
193a53d9 2126#ifdef HAVE_TARGET_32_BIG
ead1e424 2127template
dbe717ef 2128class Output_data_got<32, true>;
193a53d9 2129#endif
ead1e424 2130
193a53d9 2131#ifdef HAVE_TARGET_64_LITTLE
ead1e424 2132template
dbe717ef 2133class Output_data_got<64, false>;
193a53d9 2134#endif
ead1e424 2135
193a53d9 2136#ifdef HAVE_TARGET_64_BIG
ead1e424 2137template
dbe717ef 2138class Output_data_got<64, true>;
193a53d9 2139#endif
ead1e424 2140
a2fb1b05 2141} // End namespace gold.
This page took 0.165518 seconds and 4 git commands to generate.