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