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