]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/output.cc
* gas/mips/mips.exp: Invoke the tests smartmips, mips32-dsp,
[thirdparty/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;
a445fddf 278 gold_assert(all_phdrs_size == this->data_size());
61ba1cf9
ILT
279 unsigned char* view = of->get_output_view(this->offset(),
280 all_phdrs_size);
281 unsigned char* v = view;
282 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
283 p != this->segment_list_.end();
284 ++p)
285 {
286 elfcpp::Phdr_write<size, big_endian> ophdr(v);
287 (*p)->write_header(&ophdr);
288 v += phdr_size;
289 }
290
a445fddf
ILT
291 gold_assert(v - view == all_phdrs_size);
292
61ba1cf9 293 of->write_output_view(this->offset(), all_phdrs_size, view);
75f65a3e
ILT
294}
295
296// Output_file_header methods.
297
9025d29d 298Output_file_header::Output_file_header(const Target* target,
75f65a3e 299 const Symbol_table* symtab,
d391083d
ILT
300 const Output_segment_headers* osh,
301 const char* entry)
9025d29d 302 : target_(target),
75f65a3e 303 symtab_(symtab),
61ba1cf9 304 segment_header_(osh),
75f65a3e 305 section_header_(NULL),
d391083d
ILT
306 shstrtab_(NULL),
307 entry_(entry)
75f65a3e 308{
9025d29d 309 const int size = parameters->get_size();
61ba1cf9
ILT
310 int ehdr_size;
311 if (size == 32)
312 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
313 else if (size == 64)
314 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
315 else
a3ad94ed 316 gold_unreachable();
61ba1cf9
ILT
317
318 this->set_data_size(ehdr_size);
75f65a3e
ILT
319}
320
321// Set the section table information for a file header.
322
323void
324Output_file_header::set_section_info(const Output_section_headers* shdrs,
325 const Output_section* shstrtab)
326{
327 this->section_header_ = shdrs;
328 this->shstrtab_ = shstrtab;
329}
330
331// Write out the file header.
332
333void
61ba1cf9 334Output_file_header::do_write(Output_file* of)
54dc6425 335{
27bc2bce
ILT
336 gold_assert(this->offset() == 0);
337
9025d29d 338 if (parameters->get_size() == 32)
61ba1cf9 339 {
9025d29d
ILT
340 if (parameters->is_big_endian())
341 {
342#ifdef HAVE_TARGET_32_BIG
343 this->do_sized_write<32, true>(of);
344#else
345 gold_unreachable();
346#endif
347 }
61ba1cf9 348 else
9025d29d
ILT
349 {
350#ifdef HAVE_TARGET_32_LITTLE
351 this->do_sized_write<32, false>(of);
352#else
353 gold_unreachable();
354#endif
355 }
61ba1cf9 356 }
9025d29d 357 else if (parameters->get_size() == 64)
61ba1cf9 358 {
9025d29d
ILT
359 if (parameters->is_big_endian())
360 {
361#ifdef HAVE_TARGET_64_BIG
362 this->do_sized_write<64, true>(of);
363#else
364 gold_unreachable();
365#endif
366 }
61ba1cf9 367 else
9025d29d
ILT
368 {
369#ifdef HAVE_TARGET_64_LITTLE
370 this->do_sized_write<64, false>(of);
371#else
372 gold_unreachable();
373#endif
374 }
61ba1cf9
ILT
375 }
376 else
a3ad94ed 377 gold_unreachable();
61ba1cf9
ILT
378}
379
380// Write out the file header with appropriate size and endianess.
381
382template<int size, bool big_endian>
383void
384Output_file_header::do_sized_write(Output_file* of)
385{
a3ad94ed 386 gold_assert(this->offset() == 0);
61ba1cf9
ILT
387
388 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
389 unsigned char* view = of->get_output_view(0, ehdr_size);
390 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
391
392 unsigned char e_ident[elfcpp::EI_NIDENT];
393 memset(e_ident, 0, elfcpp::EI_NIDENT);
394 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
395 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
396 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
397 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
398 if (size == 32)
399 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
400 else if (size == 64)
401 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
402 else
a3ad94ed 403 gold_unreachable();
61ba1cf9
ILT
404 e_ident[elfcpp::EI_DATA] = (big_endian
405 ? elfcpp::ELFDATA2MSB
406 : elfcpp::ELFDATA2LSB);
407 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
408 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
409 oehdr.put_e_ident(e_ident);
410
411 elfcpp::ET e_type;
7e1edb90 412 if (parameters->output_is_object())
61ba1cf9 413 e_type = elfcpp::ET_REL;
436ca963
ILT
414 else if (parameters->output_is_shared())
415 e_type = elfcpp::ET_DYN;
61ba1cf9
ILT
416 else
417 e_type = elfcpp::ET_EXEC;
418 oehdr.put_e_type(e_type);
419
420 oehdr.put_e_machine(this->target_->machine_code());
421 oehdr.put_e_version(elfcpp::EV_CURRENT);
422
d391083d 423 oehdr.put_e_entry(this->entry<size>());
61ba1cf9
ILT
424
425 oehdr.put_e_phoff(this->segment_header_->offset());
426 oehdr.put_e_shoff(this->section_header_->offset());
427
428 // FIXME: The target needs to set the flags.
429 oehdr.put_e_flags(0);
430
431 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
432 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
433 oehdr.put_e_phnum(this->segment_header_->data_size()
434 / elfcpp::Elf_sizes<size>::phdr_size);
435 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
436 oehdr.put_e_shnum(this->section_header_->data_size()
437 / elfcpp::Elf_sizes<size>::shdr_size);
ead1e424 438 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
61ba1cf9
ILT
439
440 of->write_output_view(0, ehdr_size, view);
54dc6425
ILT
441}
442
d391083d
ILT
443// Return the value to use for the entry address. THIS->ENTRY_ is the
444// symbol specified on the command line, if any.
445
446template<int size>
447typename elfcpp::Elf_types<size>::Elf_Addr
448Output_file_header::entry()
449{
450 const bool should_issue_warning = (this->entry_ != NULL
451 && parameters->output_is_executable());
452
453 // FIXME: Need to support target specific entry symbol.
454 const char* entry = this->entry_;
455 if (entry == NULL)
456 entry = "_start";
457
458 Symbol* sym = this->symtab_->lookup(entry);
459
460 typename Sized_symbol<size>::Value_type v;
461 if (sym != NULL)
462 {
463 Sized_symbol<size>* ssym;
464 ssym = this->symtab_->get_sized_symbol<size>(sym);
465 if (!ssym->is_defined() && should_issue_warning)
466 gold_warning("entry symbol '%s' exists but is not defined", entry);
467 v = ssym->value();
468 }
469 else
470 {
471 // We couldn't find the entry symbol. See if we can parse it as
472 // a number. This supports, e.g., -e 0x1000.
473 char* endptr;
474 v = strtoull(entry, &endptr, 0);
475 if (*endptr != '\0')
476 {
477 if (should_issue_warning)
478 gold_warning("cannot find entry symbol '%s'", entry);
479 v = 0;
480 }
481 }
482
483 return v;
484}
485
dbe717ef
ILT
486// Output_data_const methods.
487
488void
a3ad94ed 489Output_data_const::do_write(Output_file* of)
dbe717ef 490{
a3ad94ed
ILT
491 of->write(this->offset(), this->data_.data(), this->data_.size());
492}
493
494// Output_data_const_buffer methods.
495
496void
497Output_data_const_buffer::do_write(Output_file* of)
498{
499 of->write(this->offset(), this->p_, this->data_size());
dbe717ef
ILT
500}
501
502// Output_section_data methods.
503
16649710
ILT
504// Record the output section, and set the entry size and such.
505
506void
507Output_section_data::set_output_section(Output_section* os)
508{
509 gold_assert(this->output_section_ == NULL);
510 this->output_section_ = os;
511 this->do_adjust_output_section(os);
512}
513
514// Return the section index of the output section.
515
dbe717ef
ILT
516unsigned int
517Output_section_data::do_out_shndx() const
518{
a3ad94ed 519 gold_assert(this->output_section_ != NULL);
dbe717ef
ILT
520 return this->output_section_->out_shndx();
521}
522
a3ad94ed
ILT
523// Output_data_strtab methods.
524
27bc2bce 525// Set the final data size.
a3ad94ed
ILT
526
527void
27bc2bce 528Output_data_strtab::set_final_data_size()
a3ad94ed
ILT
529{
530 this->strtab_->set_string_offsets();
531 this->set_data_size(this->strtab_->get_strtab_size());
532}
533
534// Write out a string table.
535
536void
537Output_data_strtab::do_write(Output_file* of)
538{
539 this->strtab_->write(of, this->offset());
540}
541
c06b7b0b
ILT
542// Output_reloc methods.
543
7bf1f802
ILT
544// A reloc against a global symbol.
545
546template<bool dynamic, int size, bool big_endian>
547Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
548 Symbol* gsym,
549 unsigned int type,
550 Output_data* od,
e8c846c3
ILT
551 Address address,
552 bool is_relative)
7bf1f802 553 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
e8c846c3 554 is_relative_(is_relative), shndx_(INVALID_CODE)
7bf1f802
ILT
555{
556 this->u1_.gsym = gsym;
557 this->u2_.od = od;
e8c846c3 558 if (dynamic && !is_relative)
7bf1f802
ILT
559 gsym->set_needs_dynsym_entry();
560}
561
562template<bool dynamic, int size, bool big_endian>
563Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
564 Symbol* gsym,
565 unsigned int type,
566 Relobj* relobj,
567 unsigned int shndx,
e8c846c3
ILT
568 Address address,
569 bool is_relative)
7bf1f802 570 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
e8c846c3 571 is_relative_(is_relative), shndx_(shndx)
7bf1f802
ILT
572{
573 gold_assert(shndx != INVALID_CODE);
574 this->u1_.gsym = gsym;
575 this->u2_.relobj = relobj;
e8c846c3 576 if (dynamic && !is_relative)
7bf1f802
ILT
577 gsym->set_needs_dynsym_entry();
578}
579
580// A reloc against a local symbol.
581
582template<bool dynamic, int size, bool big_endian>
583Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
584 Sized_relobj<size, big_endian>* relobj,
585 unsigned int local_sym_index,
586 unsigned int type,
587 Output_data* od,
e8c846c3
ILT
588 Address address,
589 bool is_relative)
7bf1f802 590 : address_(address), local_sym_index_(local_sym_index), type_(type),
e8c846c3 591 is_relative_(is_relative), shndx_(INVALID_CODE)
7bf1f802
ILT
592{
593 gold_assert(local_sym_index != GSYM_CODE
594 && local_sym_index != INVALID_CODE);
595 this->u1_.relobj = relobj;
596 this->u2_.od = od;
e8c846c3 597 if (dynamic && !is_relative)
7bf1f802
ILT
598 relobj->set_needs_output_dynsym_entry(local_sym_index);
599}
600
601template<bool dynamic, int size, bool big_endian>
602Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
603 Sized_relobj<size, big_endian>* relobj,
604 unsigned int local_sym_index,
605 unsigned int type,
606 unsigned int shndx,
e8c846c3
ILT
607 Address address,
608 bool is_relative)
7bf1f802 609 : address_(address), local_sym_index_(local_sym_index), type_(type),
e8c846c3 610 is_relative_(is_relative), shndx_(shndx)
7bf1f802
ILT
611{
612 gold_assert(local_sym_index != GSYM_CODE
613 && local_sym_index != INVALID_CODE);
614 gold_assert(shndx != INVALID_CODE);
615 this->u1_.relobj = relobj;
616 this->u2_.relobj = relobj;
e8c846c3 617 if (dynamic && !is_relative)
7bf1f802
ILT
618 relobj->set_needs_output_dynsym_entry(local_sym_index);
619}
620
621// A reloc against the STT_SECTION symbol of an output section.
622
623template<bool dynamic, int size, bool big_endian>
624Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
625 Output_section* os,
626 unsigned int type,
627 Output_data* od,
628 Address address)
629 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
e8c846c3 630 is_relative_(false), shndx_(INVALID_CODE)
7bf1f802
ILT
631{
632 this->u1_.os = os;
633 this->u2_.od = od;
634 if (dynamic)
635 os->set_needs_dynsym_index();
636}
637
638template<bool dynamic, int size, bool big_endian>
639Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
640 Output_section* os,
641 unsigned int type,
642 Relobj* relobj,
643 unsigned int shndx,
644 Address address)
645 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
e8c846c3 646 is_relative_(false), shndx_(shndx)
7bf1f802
ILT
647{
648 gold_assert(shndx != INVALID_CODE);
649 this->u1_.os = os;
650 this->u2_.relobj = relobj;
651 if (dynamic)
652 os->set_needs_dynsym_index();
653}
654
c06b7b0b
ILT
655// Get the symbol index of a relocation.
656
657template<bool dynamic, int size, bool big_endian>
658unsigned int
659Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
660 const
661{
662 unsigned int index;
663 switch (this->local_sym_index_)
664 {
665 case INVALID_CODE:
a3ad94ed 666 gold_unreachable();
c06b7b0b
ILT
667
668 case GSYM_CODE:
5a6f7e2d 669 if (this->u1_.gsym == NULL)
c06b7b0b
ILT
670 index = 0;
671 else if (dynamic)
5a6f7e2d 672 index = this->u1_.gsym->dynsym_index();
c06b7b0b 673 else
5a6f7e2d 674 index = this->u1_.gsym->symtab_index();
c06b7b0b
ILT
675 break;
676
677 case SECTION_CODE:
678 if (dynamic)
5a6f7e2d 679 index = this->u1_.os->dynsym_index();
c06b7b0b 680 else
5a6f7e2d 681 index = this->u1_.os->symtab_index();
c06b7b0b
ILT
682 break;
683
436ca963
ILT
684 case 0:
685 // Relocations without symbols use a symbol index of 0.
686 index = 0;
687 break;
688
c06b7b0b
ILT
689 default:
690 if (dynamic)
7bf1f802 691 index = this->u1_.relobj->dynsym_index(this->local_sym_index_);
c06b7b0b 692 else
5a6f7e2d 693 index = this->u1_.relobj->symtab_index(this->local_sym_index_);
c06b7b0b
ILT
694 break;
695 }
a3ad94ed 696 gold_assert(index != -1U);
c06b7b0b
ILT
697 return index;
698}
699
700// Write out the offset and info fields of a Rel or Rela relocation
701// entry.
702
703template<bool dynamic, int size, bool big_endian>
704template<typename Write_rel>
705void
706Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
707 Write_rel* wr) const
708{
a3ad94ed 709 Address address = this->address_;
5a6f7e2d
ILT
710 if (this->shndx_ != INVALID_CODE)
711 {
8383303e 712 section_offset_type off;
5a6f7e2d
ILT
713 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
714 &off);
715 gold_assert(os != NULL);
730cdc88
ILT
716 if (off != -1)
717 address += os->address() + off;
718 else
719 {
720 address = os->output_address(this->u2_.relobj, this->shndx_,
721 address);
722 gold_assert(address != -1U);
723 }
5a6f7e2d
ILT
724 }
725 else if (this->u2_.od != NULL)
726 address += this->u2_.od->address();
a3ad94ed 727 wr->put_r_offset(address);
e8c846c3
ILT
728 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
729 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
c06b7b0b
ILT
730}
731
732// Write out a Rel relocation.
733
734template<bool dynamic, int size, bool big_endian>
735void
736Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
737 unsigned char* pov) const
738{
739 elfcpp::Rel_write<size, big_endian> orel(pov);
740 this->write_rel(&orel);
741}
742
e8c846c3
ILT
743// Get the value of the symbol referred to by a Rel relocation.
744
745template<bool dynamic, int size, bool big_endian>
746typename elfcpp::Elf_types<size>::Elf_Addr
747Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value() const
748{
749 if (this->local_sym_index_ == GSYM_CODE)
750 {
751 const Sized_symbol<size>* sym;
752 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
753 return sym->value();
754 }
755 gold_assert(this->local_sym_index_ != SECTION_CODE
756 && this->local_sym_index_ != INVALID_CODE);
757 const Sized_relobj<size, big_endian>* relobj = this->u1_.relobj;
758 return relobj->local_symbol_value(this->local_sym_index_);
759}
760
c06b7b0b
ILT
761// Write out a Rela relocation.
762
763template<bool dynamic, int size, bool big_endian>
764void
765Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
766 unsigned char* pov) const
767{
768 elfcpp::Rela_write<size, big_endian> orel(pov);
769 this->rel_.write_rel(&orel);
e8c846c3
ILT
770 Addend addend = this->addend_;
771 if (rel_.is_relative())
772 addend += rel_.symbol_value();
773 orel.put_r_addend(addend);
c06b7b0b
ILT
774}
775
776// Output_data_reloc_base methods.
777
16649710
ILT
778// Adjust the output section.
779
780template<int sh_type, bool dynamic, int size, bool big_endian>
781void
782Output_data_reloc_base<sh_type, dynamic, size, big_endian>
783 ::do_adjust_output_section(Output_section* os)
784{
785 if (sh_type == elfcpp::SHT_REL)
786 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
787 else if (sh_type == elfcpp::SHT_RELA)
788 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
789 else
790 gold_unreachable();
791 if (dynamic)
792 os->set_should_link_to_dynsym();
793 else
794 os->set_should_link_to_symtab();
795}
796
c06b7b0b
ILT
797// Write out relocation data.
798
799template<int sh_type, bool dynamic, int size, bool big_endian>
800void
801Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
802 Output_file* of)
803{
804 const off_t off = this->offset();
805 const off_t oview_size = this->data_size();
806 unsigned char* const oview = of->get_output_view(off, oview_size);
807
808 unsigned char* pov = oview;
809 for (typename Relocs::const_iterator p = this->relocs_.begin();
810 p != this->relocs_.end();
811 ++p)
812 {
813 p->write(pov);
814 pov += reloc_size;
815 }
816
a3ad94ed 817 gold_assert(pov - oview == oview_size);
c06b7b0b
ILT
818
819 of->write_output_view(off, oview_size, oview);
820
821 // We no longer need the relocation entries.
822 this->relocs_.clear();
823}
824
dbe717ef 825// Output_data_got::Got_entry methods.
ead1e424
ILT
826
827// Write out the entry.
828
829template<int size, bool big_endian>
830void
7e1edb90 831Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
ead1e424
ILT
832{
833 Valtype val = 0;
834
835 switch (this->local_sym_index_)
836 {
837 case GSYM_CODE:
838 {
e8c846c3
ILT
839 // If the symbol is resolved locally, we need to write out the
840 // link-time value, which will be relocated dynamically by a
841 // RELATIVE relocation.
ead1e424 842 Symbol* gsym = this->u_.gsym;
e8c846c3
ILT
843 Sized_symbol<size>* sgsym;
844 // This cast is a bit ugly. We don't want to put a
845 // virtual method in Symbol, because we want Symbol to be
846 // as small as possible.
847 sgsym = static_cast<Sized_symbol<size>*>(gsym);
848 val = sgsym->value();
ead1e424
ILT
849 }
850 break;
851
852 case CONSTANT_CODE:
853 val = this->u_.constant;
854 break;
855
856 default:
e727fa71
ILT
857 val = this->u_.object->local_symbol_value(this->local_sym_index_);
858 break;
ead1e424
ILT
859 }
860
a3ad94ed 861 elfcpp::Swap<size, big_endian>::writeval(pov, val);
ead1e424
ILT
862}
863
dbe717ef 864// Output_data_got methods.
ead1e424 865
dbe717ef
ILT
866// Add an entry for a global symbol to the GOT. This returns true if
867// this is a new GOT entry, false if the symbol already had a GOT
868// entry.
869
870template<int size, bool big_endian>
871bool
872Output_data_got<size, big_endian>::add_global(Symbol* gsym)
ead1e424 873{
dbe717ef
ILT
874 if (gsym->has_got_offset())
875 return false;
ead1e424 876
dbe717ef
ILT
877 this->entries_.push_back(Got_entry(gsym));
878 this->set_got_size();
879 gsym->set_got_offset(this->last_got_offset());
880 return true;
881}
ead1e424 882
7bf1f802
ILT
883// Add an entry for a global symbol to the GOT, and add a dynamic
884// relocation of type R_TYPE for the GOT entry.
885template<int size, bool big_endian>
886void
887Output_data_got<size, big_endian>::add_global_with_rel(
888 Symbol* gsym,
889 Rel_dyn* rel_dyn,
890 unsigned int r_type)
891{
892 if (gsym->has_got_offset())
893 return;
894
895 this->entries_.push_back(Got_entry());
896 this->set_got_size();
897 unsigned int got_offset = this->last_got_offset();
898 gsym->set_got_offset(got_offset);
899 rel_dyn->add_global(gsym, r_type, this, got_offset);
900}
901
902template<int size, bool big_endian>
903void
904Output_data_got<size, big_endian>::add_global_with_rela(
905 Symbol* gsym,
906 Rela_dyn* rela_dyn,
907 unsigned int r_type)
908{
909 if (gsym->has_got_offset())
910 return;
911
912 this->entries_.push_back(Got_entry());
913 this->set_got_size();
914 unsigned int got_offset = this->last_got_offset();
915 gsym->set_got_offset(got_offset);
916 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
917}
918
e727fa71
ILT
919// Add an entry for a local symbol to the GOT. This returns true if
920// this is a new GOT entry, false if the symbol already has a GOT
921// entry.
922
923template<int size, bool big_endian>
924bool
925Output_data_got<size, big_endian>::add_local(
926 Sized_relobj<size, big_endian>* object,
927 unsigned int symndx)
928{
929 if (object->local_has_got_offset(symndx))
930 return false;
07f397ab 931
e727fa71
ILT
932 this->entries_.push_back(Got_entry(object, symndx));
933 this->set_got_size();
934 object->set_local_got_offset(symndx, this->last_got_offset());
935 return true;
936}
937
7bf1f802
ILT
938// Add an entry for a local symbol to the GOT, and add a dynamic
939// relocation of type R_TYPE for the GOT entry.
940template<int size, bool big_endian>
941void
942Output_data_got<size, big_endian>::add_local_with_rel(
943 Sized_relobj<size, big_endian>* object,
944 unsigned int symndx,
945 Rel_dyn* rel_dyn,
946 unsigned int r_type)
947{
948 if (object->local_has_got_offset(symndx))
949 return;
950
951 this->entries_.push_back(Got_entry());
952 this->set_got_size();
953 unsigned int got_offset = this->last_got_offset();
954 object->set_local_got_offset(symndx, got_offset);
955 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
956}
957
958template<int size, bool big_endian>
959void
960Output_data_got<size, big_endian>::add_local_with_rela(
961 Sized_relobj<size, big_endian>* object,
962 unsigned int symndx,
963 Rela_dyn* rela_dyn,
964 unsigned int r_type)
965{
966 if (object->local_has_got_offset(symndx))
967 return;
968
969 this->entries_.push_back(Got_entry());
970 this->set_got_size();
971 unsigned int got_offset = this->last_got_offset();
972 object->set_local_got_offset(symndx, got_offset);
973 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
974}
975
07f397ab
ILT
976// Add an entry (or a pair of entries) for a global TLS symbol to the GOT.
977// In a pair of entries, the first value in the pair will be used for the
978// module index, and the second value will be used for the dtv-relative
979// offset. This returns true if this is a new GOT entry, false if the symbol
980// already has a GOT entry.
981
982template<int size, bool big_endian>
983bool
7bf1f802 984Output_data_got<size, big_endian>::add_global_tls(Symbol* gsym, bool need_pair)
07f397ab
ILT
985{
986 if (gsym->has_tls_got_offset(need_pair))
987 return false;
988
989 this->entries_.push_back(Got_entry(gsym));
990 gsym->set_tls_got_offset(this->last_got_offset(), need_pair);
991 if (need_pair)
992 this->entries_.push_back(Got_entry(gsym));
993 this->set_got_size();
994 return true;
995}
996
7bf1f802
ILT
997// Add an entry for a global TLS symbol to the GOT, and add a dynamic
998// relocation of type R_TYPE.
999template<int size, bool big_endian>
1000void
1001Output_data_got<size, big_endian>::add_global_tls_with_rel(
1002 Symbol* gsym,
1003 Rel_dyn* rel_dyn,
1004 unsigned int r_type)
1005{
1006 if (gsym->has_tls_got_offset(false))
1007 return;
1008
1009 this->entries_.push_back(Got_entry());
1010 this->set_got_size();
1011 unsigned int got_offset = this->last_got_offset();
1012 gsym->set_tls_got_offset(got_offset, false);
1013 rel_dyn->add_global(gsym, r_type, this, got_offset);
1014}
1015
1016template<int size, bool big_endian>
1017void
1018Output_data_got<size, big_endian>::add_global_tls_with_rela(
1019 Symbol* gsym,
1020 Rela_dyn* rela_dyn,
1021 unsigned int r_type)
1022{
1023 if (gsym->has_tls_got_offset(false))
1024 return;
1025
1026 this->entries_.push_back(Got_entry());
1027 this->set_got_size();
1028 unsigned int got_offset = this->last_got_offset();
1029 gsym->set_tls_got_offset(got_offset, false);
1030 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1031}
1032
1033// Add a pair of entries for a global TLS symbol to the GOT, and add
1034// dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1035template<int size, bool big_endian>
1036void
1037Output_data_got<size, big_endian>::add_global_tls_with_rel(
1038 Symbol* gsym,
1039 Rel_dyn* rel_dyn,
1040 unsigned int mod_r_type,
1041 unsigned int dtv_r_type)
1042{
1043 if (gsym->has_tls_got_offset(true))
1044 return;
1045
1046 this->entries_.push_back(Got_entry());
1047 unsigned int got_offset = this->last_got_offset();
1048 gsym->set_tls_got_offset(got_offset, true);
1049 rel_dyn->add_global(gsym, mod_r_type, this, got_offset);
1050
1051 this->entries_.push_back(Got_entry());
1052 this->set_got_size();
1053 got_offset = this->last_got_offset();
1054 rel_dyn->add_global(gsym, dtv_r_type, this, got_offset);
1055}
1056
1057template<int size, bool big_endian>
1058void
1059Output_data_got<size, big_endian>::add_global_tls_with_rela(
1060 Symbol* gsym,
1061 Rela_dyn* rela_dyn,
1062 unsigned int mod_r_type,
1063 unsigned int dtv_r_type)
1064{
1065 if (gsym->has_tls_got_offset(true))
1066 return;
1067
1068 this->entries_.push_back(Got_entry());
1069 unsigned int got_offset = this->last_got_offset();
1070 gsym->set_tls_got_offset(got_offset, true);
1071 rela_dyn->add_global(gsym, mod_r_type, this, got_offset, 0);
1072
1073 this->entries_.push_back(Got_entry());
1074 this->set_got_size();
1075 got_offset = this->last_got_offset();
1076 rela_dyn->add_global(gsym, dtv_r_type, this, got_offset, 0);
1077}
1078
07f397ab
ILT
1079// Add an entry (or a pair of entries) for a local TLS symbol to the GOT.
1080// In a pair of entries, the first value in the pair will be used for the
1081// module index, and the second value will be used for the dtv-relative
1082// offset. This returns true if this is a new GOT entry, false if the symbol
1083// already has a GOT entry.
1084
1085template<int size, bool big_endian>
1086bool
1087Output_data_got<size, big_endian>::add_local_tls(
1088 Sized_relobj<size, big_endian>* object,
1089 unsigned int symndx,
1090 bool need_pair)
1091{
1092 if (object->local_has_tls_got_offset(symndx, need_pair))
1093 return false;
1094
1095 this->entries_.push_back(Got_entry(object, symndx));
1096 object->set_local_tls_got_offset(symndx, this->last_got_offset(), need_pair);
1097 if (need_pair)
1098 this->entries_.push_back(Got_entry(object, symndx));
1099 this->set_got_size();
1100 return true;
1101}
1102
7bf1f802
ILT
1103// Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1104// and add a dynamic relocation of type R_TYPE for the first GOT entry.
1105// Because this is a local symbol, the first GOT entry can be relocated
1106// relative to a section symbol, and the second GOT entry will have an
1107// dtv-relative value that can be computed at link time.
1108template<int size, bool big_endian>
1109void
1110Output_data_got<size, big_endian>::add_local_tls_with_rel(
1111 Sized_relobj<size, big_endian>* object,
1112 unsigned int symndx,
1113 unsigned int shndx,
1114 bool need_pair,
1115 Rel_dyn* rel_dyn,
1116 unsigned int r_type)
1117{
1118 if (object->local_has_tls_got_offset(symndx, need_pair))
1119 return;
1120
1121 this->entries_.push_back(Got_entry());
1122 unsigned int got_offset = this->last_got_offset();
1123 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
8383303e 1124 section_offset_type off;
7bf1f802
ILT
1125 Output_section* os = object->output_section(shndx, &off);
1126 rel_dyn->add_output_section(os, r_type, this, got_offset);
1127
1128 // The second entry of the pair will be statically initialized
1129 // with the TLS offset of the symbol.
1130 if (need_pair)
1131 this->entries_.push_back(Got_entry(object, symndx));
1132
1133 this->set_got_size();
1134}
1135
1136template<int size, bool big_endian>
1137void
1138Output_data_got<size, big_endian>::add_local_tls_with_rela(
1139 Sized_relobj<size, big_endian>* object,
1140 unsigned int symndx,
1141 unsigned int shndx,
1142 bool need_pair,
1143 Rela_dyn* rela_dyn,
1144 unsigned int r_type)
1145{
1146 if (object->local_has_tls_got_offset(symndx, need_pair))
1147 return;
1148
1149 this->entries_.push_back(Got_entry());
1150 unsigned int got_offset = this->last_got_offset();
1151 object->set_local_tls_got_offset(symndx, got_offset, need_pair);
8383303e 1152 section_offset_type off;
7bf1f802
ILT
1153 Output_section* os = object->output_section(shndx, &off);
1154 rela_dyn->add_output_section(os, r_type, this, got_offset, 0);
1155
1156 // The second entry of the pair will be statically initialized
1157 // with the TLS offset of the symbol.
1158 if (need_pair)
1159 this->entries_.push_back(Got_entry(object, symndx));
1160
1161 this->set_got_size();
1162}
1163
ead1e424
ILT
1164// Write out the GOT.
1165
1166template<int size, bool big_endian>
1167void
dbe717ef 1168Output_data_got<size, big_endian>::do_write(Output_file* of)
ead1e424
ILT
1169{
1170 const int add = size / 8;
1171
1172 const off_t off = this->offset();
c06b7b0b 1173 const off_t oview_size = this->data_size();
ead1e424
ILT
1174 unsigned char* const oview = of->get_output_view(off, oview_size);
1175
1176 unsigned char* pov = oview;
1177 for (typename Got_entries::const_iterator p = this->entries_.begin();
1178 p != this->entries_.end();
1179 ++p)
1180 {
7e1edb90 1181 p->write(pov);
ead1e424
ILT
1182 pov += add;
1183 }
1184
a3ad94ed 1185 gold_assert(pov - oview == oview_size);
c06b7b0b 1186
ead1e424
ILT
1187 of->write_output_view(off, oview_size, oview);
1188
1189 // We no longer need the GOT entries.
1190 this->entries_.clear();
1191}
1192
a3ad94ed
ILT
1193// Output_data_dynamic::Dynamic_entry methods.
1194
1195// Write out the entry.
1196
1197template<int size, bool big_endian>
1198void
1199Output_data_dynamic::Dynamic_entry::write(
1200 unsigned char* pov,
1ddbd1e6
ILT
1201 const Stringpool* pool
1202 ACCEPT_SIZE_ENDIAN) const
a3ad94ed
ILT
1203{
1204 typename elfcpp::Elf_types<size>::Elf_WXword val;
1205 switch (this->classification_)
1206 {
1207 case DYNAMIC_NUMBER:
1208 val = this->u_.val;
1209 break;
1210
1211 case DYNAMIC_SECTION_ADDRESS:
16649710 1212 val = this->u_.od->address();
a3ad94ed
ILT
1213 break;
1214
1215 case DYNAMIC_SECTION_SIZE:
16649710 1216 val = this->u_.od->data_size();
a3ad94ed
ILT
1217 break;
1218
1219 case DYNAMIC_SYMBOL:
1220 {
16649710
ILT
1221 const Sized_symbol<size>* s =
1222 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
1223 val = s->value();
1224 }
1225 break;
1226
1227 case DYNAMIC_STRING:
1228 val = pool->get_offset(this->u_.str);
1229 break;
1230
1231 default:
1232 gold_unreachable();
1233 }
1234
1235 elfcpp::Dyn_write<size, big_endian> dw(pov);
1236 dw.put_d_tag(this->tag_);
1237 dw.put_d_val(val);
1238}
1239
1240// Output_data_dynamic methods.
1241
16649710
ILT
1242// Adjust the output section to set the entry size.
1243
1244void
1245Output_data_dynamic::do_adjust_output_section(Output_section* os)
1246{
9025d29d 1247 if (parameters->get_size() == 32)
16649710 1248 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
9025d29d 1249 else if (parameters->get_size() == 64)
16649710
ILT
1250 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1251 else
1252 gold_unreachable();
1253}
1254
a3ad94ed
ILT
1255// Set the final data size.
1256
1257void
27bc2bce 1258Output_data_dynamic::set_final_data_size()
a3ad94ed
ILT
1259{
1260 // Add the terminating entry.
1261 this->add_constant(elfcpp::DT_NULL, 0);
1262
1263 int dyn_size;
9025d29d 1264 if (parameters->get_size() == 32)
a3ad94ed 1265 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
9025d29d 1266 else if (parameters->get_size() == 64)
a3ad94ed
ILT
1267 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1268 else
1269 gold_unreachable();
1270 this->set_data_size(this->entries_.size() * dyn_size);
1271}
1272
1273// Write out the dynamic entries.
1274
1275void
1276Output_data_dynamic::do_write(Output_file* of)
1277{
9025d29d 1278 if (parameters->get_size() == 32)
a3ad94ed 1279 {
9025d29d
ILT
1280 if (parameters->is_big_endian())
1281 {
1282#ifdef HAVE_TARGET_32_BIG
1283 this->sized_write<32, true>(of);
1284#else
1285 gold_unreachable();
1286#endif
1287 }
a3ad94ed 1288 else
9025d29d
ILT
1289 {
1290#ifdef HAVE_TARGET_32_LITTLE
1291 this->sized_write<32, false>(of);
1292#else
1293 gold_unreachable();
1294#endif
1295 }
a3ad94ed 1296 }
9025d29d 1297 else if (parameters->get_size() == 64)
a3ad94ed 1298 {
9025d29d
ILT
1299 if (parameters->is_big_endian())
1300 {
1301#ifdef HAVE_TARGET_64_BIG
1302 this->sized_write<64, true>(of);
1303#else
1304 gold_unreachable();
1305#endif
1306 }
a3ad94ed 1307 else
9025d29d
ILT
1308 {
1309#ifdef HAVE_TARGET_64_LITTLE
1310 this->sized_write<64, false>(of);
1311#else
1312 gold_unreachable();
1313#endif
1314 }
a3ad94ed
ILT
1315 }
1316 else
1317 gold_unreachable();
1318}
1319
1320template<int size, bool big_endian>
1321void
1322Output_data_dynamic::sized_write(Output_file* of)
1323{
1324 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1325
1326 const off_t offset = this->offset();
1327 const off_t oview_size = this->data_size();
1328 unsigned char* const oview = of->get_output_view(offset, oview_size);
1329
1330 unsigned char* pov = oview;
1331 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1332 p != this->entries_.end();
1333 ++p)
1334 {
1ddbd1e6
ILT
1335 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1336 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
a3ad94ed
ILT
1337 pov += dyn_size;
1338 }
1339
1340 gold_assert(pov - oview == oview_size);
1341
1342 of->write_output_view(offset, oview_size, oview);
1343
1344 // We no longer need the dynamic entries.
1345 this->entries_.clear();
1346}
1347
ead1e424
ILT
1348// Output_section::Input_section methods.
1349
1350// Return the data size. For an input section we store the size here.
1351// For an Output_section_data, we have to ask it for the size.
1352
1353off_t
1354Output_section::Input_section::data_size() const
1355{
1356 if (this->is_input_section())
b8e6aad9 1357 return this->u1_.data_size;
ead1e424 1358 else
b8e6aad9 1359 return this->u2_.posd->data_size();
ead1e424
ILT
1360}
1361
1362// Set the address and file offset.
1363
1364void
96803768
ILT
1365Output_section::Input_section::set_address_and_file_offset(
1366 uint64_t address,
1367 off_t file_offset,
1368 off_t section_file_offset)
ead1e424
ILT
1369{
1370 if (this->is_input_section())
96803768
ILT
1371 this->u2_.object->set_section_offset(this->shndx_,
1372 file_offset - section_file_offset);
ead1e424 1373 else
96803768
ILT
1374 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1375}
1376
a445fddf
ILT
1377// Reset the address and file offset.
1378
1379void
1380Output_section::Input_section::reset_address_and_file_offset()
1381{
1382 if (!this->is_input_section())
1383 this->u2_.posd->reset_address_and_file_offset();
1384}
1385
96803768
ILT
1386// Finalize the data size.
1387
1388void
1389Output_section::Input_section::finalize_data_size()
1390{
1391 if (!this->is_input_section())
1392 this->u2_.posd->finalize_data_size();
b8e6aad9
ILT
1393}
1394
1e983657
ILT
1395// Try to turn an input offset into an output offset. We want to
1396// return the output offset relative to the start of this
1397// Input_section in the output section.
b8e6aad9 1398
8f00aeb8 1399inline bool
8383303e
ILT
1400Output_section::Input_section::output_offset(
1401 const Relobj* object,
1402 unsigned int shndx,
1403 section_offset_type offset,
1404 section_offset_type *poutput) const
b8e6aad9
ILT
1405{
1406 if (!this->is_input_section())
730cdc88 1407 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
b8e6aad9
ILT
1408 else
1409 {
730cdc88 1410 if (this->shndx_ != shndx || this->u2_.object != object)
b8e6aad9 1411 return false;
1e983657 1412 *poutput = offset;
b8e6aad9
ILT
1413 return true;
1414 }
ead1e424
ILT
1415}
1416
a9a60db6
ILT
1417// Return whether this is the merge section for the input section
1418// SHNDX in OBJECT.
1419
1420inline bool
1421Output_section::Input_section::is_merge_section_for(const Relobj* object,
1422 unsigned int shndx) const
1423{
1424 if (this->is_input_section())
1425 return false;
1426 return this->u2_.posd->is_merge_section_for(object, shndx);
1427}
1428
ead1e424
ILT
1429// Write out the data. We don't have to do anything for an input
1430// section--they are handled via Object::relocate--but this is where
1431// we write out the data for an Output_section_data.
1432
1433void
1434Output_section::Input_section::write(Output_file* of)
1435{
1436 if (!this->is_input_section())
b8e6aad9 1437 this->u2_.posd->write(of);
ead1e424
ILT
1438}
1439
96803768
ILT
1440// Write the data to a buffer. As for write(), we don't have to do
1441// anything for an input section.
1442
1443void
1444Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1445{
1446 if (!this->is_input_section())
1447 this->u2_.posd->write_to_buffer(buffer);
1448}
1449
a2fb1b05
ILT
1450// Output_section methods.
1451
1452// Construct an Output_section. NAME will point into a Stringpool.
1453
96803768 1454Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
b8e6aad9 1455 elfcpp::Elf_Xword flags)
96803768 1456 : name_(name),
a2fb1b05
ILT
1457 addralign_(0),
1458 entsize_(0),
a445fddf 1459 load_address_(0),
16649710 1460 link_section_(NULL),
a2fb1b05 1461 link_(0),
16649710 1462 info_section_(NULL),
a2fb1b05
ILT
1463 info_(0),
1464 type_(type),
61ba1cf9 1465 flags_(flags),
91ea499d 1466 out_shndx_(-1U),
c06b7b0b
ILT
1467 symtab_index_(0),
1468 dynsym_index_(0),
ead1e424
ILT
1469 input_sections_(),
1470 first_input_offset_(0),
c51e6221 1471 fills_(),
96803768 1472 postprocessing_buffer_(NULL),
a3ad94ed 1473 needs_symtab_index_(false),
16649710
ILT
1474 needs_dynsym_index_(false),
1475 should_link_to_symtab_(false),
730cdc88 1476 should_link_to_dynsym_(false),
27bc2bce 1477 after_input_sections_(false),
7bf1f802 1478 requires_postprocessing_(false),
a445fddf
ILT
1479 found_in_sections_clause_(false),
1480 has_load_address_(false),
7bf1f802 1481 tls_offset_(0)
a2fb1b05 1482{
27bc2bce
ILT
1483 // An unallocated section has no address. Forcing this means that
1484 // we don't need special treatment for symbols defined in debug
1485 // sections.
1486 if ((flags & elfcpp::SHF_ALLOC) == 0)
1487 this->set_address(0);
a2fb1b05
ILT
1488}
1489
54dc6425
ILT
1490Output_section::~Output_section()
1491{
1492}
1493
16649710
ILT
1494// Set the entry size.
1495
1496void
1497Output_section::set_entsize(uint64_t v)
1498{
1499 if (this->entsize_ == 0)
1500 this->entsize_ = v;
1501 else
1502 gold_assert(this->entsize_ == v);
1503}
1504
ead1e424 1505// Add the input section SHNDX, with header SHDR, named SECNAME, in
730cdc88
ILT
1506// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1507// relocation section which applies to this section, or 0 if none, or
1508// -1U if more than one. Return the offset of the input section
1509// within the output section. Return -1 if the input section will
1510// receive special handling. In the normal case we don't always keep
1511// track of input sections for an Output_section. Instead, each
1512// Object keeps track of the Output_section for each of its input
a445fddf
ILT
1513// sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1514// track of input sections here; this is used when SECTIONS appears in
1515// a linker script.
a2fb1b05
ILT
1516
1517template<int size, bool big_endian>
1518off_t
730cdc88
ILT
1519Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1520 unsigned int shndx,
ead1e424 1521 const char* secname,
730cdc88 1522 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf
ILT
1523 unsigned int reloc_shndx,
1524 bool have_sections_script)
a2fb1b05
ILT
1525{
1526 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1527 if ((addralign & (addralign - 1)) != 0)
1528 {
75f2446e
ILT
1529 object->error(_("invalid alignment %lu for section \"%s\""),
1530 static_cast<unsigned long>(addralign), secname);
1531 addralign = 1;
a2fb1b05 1532 }
a2fb1b05
ILT
1533
1534 if (addralign > this->addralign_)
1535 this->addralign_ = addralign;
1536
44a43cf9 1537 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
a445fddf
ILT
1538 this->flags_ |= (sh_flags
1539 & (elfcpp::SHF_WRITE
1540 | elfcpp::SHF_ALLOC
1541 | elfcpp::SHF_EXECINSTR));
1542
4f833eee 1543 uint64_t entsize = shdr.get_sh_entsize();
44a43cf9
ILT
1544
1545 // .debug_str is a mergeable string section, but is not always so
1546 // marked by compilers. Mark manually here so we can optimize.
1547 if (strcmp(secname, ".debug_str") == 0)
4f833eee
ILT
1548 {
1549 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1550 entsize = 1;
1551 }
44a43cf9 1552
b8e6aad9 1553 // If this is a SHF_MERGE section, we pass all the input sections to
730cdc88
ILT
1554 // a Output_data_merge. We don't try to handle relocations for such
1555 // a section.
44a43cf9 1556 if ((sh_flags & elfcpp::SHF_MERGE) != 0
730cdc88 1557 && reloc_shndx == 0)
b8e6aad9 1558 {
44a43cf9 1559 if (this->add_merge_input_section(object, shndx, sh_flags,
96803768 1560 entsize, addralign))
b8e6aad9
ILT
1561 {
1562 // Tell the relocation routines that they need to call the
730cdc88 1563 // output_offset method to determine the final address.
b8e6aad9
ILT
1564 return -1;
1565 }
1566 }
1567
27bc2bce 1568 off_t offset_in_section = this->current_data_size_for_child();
c51e6221
ILT
1569 off_t aligned_offset_in_section = align_address(offset_in_section,
1570 addralign);
1571
1572 if (aligned_offset_in_section > offset_in_section
a445fddf 1573 && !have_sections_script
44a43cf9 1574 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
c51e6221
ILT
1575 && object->target()->has_code_fill())
1576 {
1577 // We need to add some fill data. Using fill_list_ when
1578 // possible is an optimization, since we will often have fill
1579 // sections without input sections.
1580 off_t fill_len = aligned_offset_in_section - offset_in_section;
1581 if (this->input_sections_.empty())
1582 this->fills_.push_back(Fill(offset_in_section, fill_len));
1583 else
1584 {
1585 // FIXME: When relaxing, the size needs to adjust to
1586 // maintain a constant alignment.
1587 std::string fill_data(object->target()->code_fill(fill_len));
1588 Output_data_const* odc = new Output_data_const(fill_data, 1);
1589 this->input_sections_.push_back(Input_section(odc));
1590 }
1591 }
1592
27bc2bce
ILT
1593 this->set_current_data_size_for_child(aligned_offset_in_section
1594 + shdr.get_sh_size());
a2fb1b05 1595
ead1e424
ILT
1596 // We need to keep track of this section if we are already keeping
1597 // track of sections, or if we are relaxing. FIXME: Add test for
1598 // relaxing.
a445fddf 1599 if (have_sections_script || !this->input_sections_.empty())
ead1e424
ILT
1600 this->input_sections_.push_back(Input_section(object, shndx,
1601 shdr.get_sh_size(),
1602 addralign));
54dc6425 1603
c51e6221 1604 return aligned_offset_in_section;
61ba1cf9
ILT
1605}
1606
ead1e424
ILT
1607// Add arbitrary data to an output section.
1608
1609void
1610Output_section::add_output_section_data(Output_section_data* posd)
1611{
b8e6aad9
ILT
1612 Input_section inp(posd);
1613 this->add_output_section_data(&inp);
a445fddf
ILT
1614
1615 if (posd->is_data_size_valid())
1616 {
1617 off_t offset_in_section = this->current_data_size_for_child();
1618 off_t aligned_offset_in_section = align_address(offset_in_section,
1619 posd->addralign());
1620 this->set_current_data_size_for_child(aligned_offset_in_section
1621 + posd->data_size());
1622 }
b8e6aad9
ILT
1623}
1624
1625// Add arbitrary data to an output section by Input_section.
c06b7b0b 1626
b8e6aad9
ILT
1627void
1628Output_section::add_output_section_data(Input_section* inp)
1629{
ead1e424 1630 if (this->input_sections_.empty())
27bc2bce 1631 this->first_input_offset_ = this->current_data_size_for_child();
c06b7b0b 1632
b8e6aad9 1633 this->input_sections_.push_back(*inp);
c06b7b0b 1634
b8e6aad9 1635 uint64_t addralign = inp->addralign();
ead1e424
ILT
1636 if (addralign > this->addralign_)
1637 this->addralign_ = addralign;
c06b7b0b 1638
b8e6aad9
ILT
1639 inp->set_output_section(this);
1640}
1641
1642// Add a merge section to an output section.
1643
1644void
1645Output_section::add_output_merge_section(Output_section_data* posd,
1646 bool is_string, uint64_t entsize)
1647{
1648 Input_section inp(posd, is_string, entsize);
1649 this->add_output_section_data(&inp);
1650}
1651
1652// Add an input section to a SHF_MERGE section.
1653
1654bool
1655Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1656 uint64_t flags, uint64_t entsize,
96803768 1657 uint64_t addralign)
b8e6aad9 1658{
87f95776
ILT
1659 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1660
1661 // We only merge strings if the alignment is not more than the
1662 // character size. This could be handled, but it's unusual.
1663 if (is_string && addralign > entsize)
b8e6aad9
ILT
1664 return false;
1665
b8e6aad9
ILT
1666 Input_section_list::iterator p;
1667 for (p = this->input_sections_.begin();
1668 p != this->input_sections_.end();
1669 ++p)
87f95776 1670 if (p->is_merge_section(is_string, entsize, addralign))
9a0910c3
ILT
1671 {
1672 p->add_input_section(object, shndx);
1673 return true;
1674 }
b8e6aad9
ILT
1675
1676 // We handle the actual constant merging in Output_merge_data or
1677 // Output_merge_string_data.
9a0910c3
ILT
1678 Output_section_data* posd;
1679 if (!is_string)
1680 posd = new Output_merge_data(entsize, addralign);
b8e6aad9
ILT
1681 else
1682 {
9a0910c3
ILT
1683 switch (entsize)
1684 {
1685 case 1:
1686 posd = new Output_merge_string<char>(addralign);
1687 break;
1688 case 2:
1689 posd = new Output_merge_string<uint16_t>(addralign);
1690 break;
1691 case 4:
1692 posd = new Output_merge_string<uint32_t>(addralign);
1693 break;
1694 default:
1695 return false;
1696 }
b8e6aad9
ILT
1697 }
1698
9a0910c3
ILT
1699 this->add_output_merge_section(posd, is_string, entsize);
1700 posd->add_input_section(object, shndx);
1701
b8e6aad9
ILT
1702 return true;
1703}
1704
730cdc88
ILT
1705// Given an address OFFSET relative to the start of input section
1706// SHNDX in OBJECT, return whether this address is being included in
1707// the final link. This should only be called if SHNDX in OBJECT has
1708// a special mapping.
1709
1710bool
1711Output_section::is_input_address_mapped(const Relobj* object,
1712 unsigned int shndx,
1713 off_t offset) const
1714{
1715 gold_assert(object->is_section_specially_mapped(shndx));
1716
1717 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1718 p != this->input_sections_.end();
1719 ++p)
1720 {
8383303e 1721 section_offset_type output_offset;
730cdc88
ILT
1722 if (p->output_offset(object, shndx, offset, &output_offset))
1723 return output_offset != -1;
1724 }
1725
1726 // By default we assume that the address is mapped. This should
1727 // only be called after we have passed all sections to Layout. At
1728 // that point we should know what we are discarding.
1729 return true;
1730}
1731
1732// Given an address OFFSET relative to the start of input section
1733// SHNDX in object OBJECT, return the output offset relative to the
1e983657
ILT
1734// start of the input section in the output section. This should only
1735// be called if SHNDX in OBJECT has a special mapping.
730cdc88 1736
8383303e 1737section_offset_type
730cdc88 1738Output_section::output_offset(const Relobj* object, unsigned int shndx,
8383303e 1739 section_offset_type offset) const
730cdc88
ILT
1740{
1741 gold_assert(object->is_section_specially_mapped(shndx));
1742 // This can only be called meaningfully when layout is complete.
1743 gold_assert(Output_data::is_layout_complete());
1744
1745 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1746 p != this->input_sections_.end();
1747 ++p)
1748 {
8383303e 1749 section_offset_type output_offset;
730cdc88
ILT
1750 if (p->output_offset(object, shndx, offset, &output_offset))
1751 return output_offset;
1752 }
1753 gold_unreachable();
1754}
1755
b8e6aad9
ILT
1756// Return the output virtual address of OFFSET relative to the start
1757// of input section SHNDX in object OBJECT.
1758
1759uint64_t
1760Output_section::output_address(const Relobj* object, unsigned int shndx,
1761 off_t offset) const
1762{
730cdc88 1763 gold_assert(object->is_section_specially_mapped(shndx));
730cdc88 1764
b8e6aad9
ILT
1765 uint64_t addr = this->address() + this->first_input_offset_;
1766 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1767 p != this->input_sections_.end();
1768 ++p)
1769 {
1770 addr = align_address(addr, p->addralign());
8383303e 1771 section_offset_type output_offset;
730cdc88
ILT
1772 if (p->output_offset(object, shndx, offset, &output_offset))
1773 {
1774 if (output_offset == -1)
1775 return -1U;
1776 return addr + output_offset;
1777 }
b8e6aad9
ILT
1778 addr += p->data_size();
1779 }
1780
1781 // If we get here, it means that we don't know the mapping for this
1782 // input section. This might happen in principle if
1783 // add_input_section were called before add_output_section_data.
1784 // But it should never actually happen.
1785
1786 gold_unreachable();
ead1e424
ILT
1787}
1788
a9a60db6
ILT
1789// Return the output address of the start of the merged section for
1790// input section SHNDX in object OBJECT.
1791
1792uint64_t
1793Output_section::starting_output_address(const Relobj* object,
1794 unsigned int shndx) const
1795{
1796 gold_assert(object->is_section_specially_mapped(shndx));
1797
1798 uint64_t addr = this->address() + this->first_input_offset_;
1799 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1800 p != this->input_sections_.end();
1801 ++p)
1802 {
1803 addr = align_address(addr, p->addralign());
1804
1805 // It would be nice if we could use the existing output_offset
1806 // method to get the output offset of input offset 0.
1807 // Unfortunately we don't know for sure that input offset 0 is
1808 // mapped at all.
1809 if (p->is_merge_section_for(object, shndx))
1810 return addr;
1811
1812 addr += p->data_size();
1813 }
1814 gold_unreachable();
1815}
1816
27bc2bce 1817// Set the data size of an Output_section. This is where we handle
ead1e424
ILT
1818// setting the addresses of any Output_section_data objects.
1819
1820void
27bc2bce 1821Output_section::set_final_data_size()
ead1e424
ILT
1822{
1823 if (this->input_sections_.empty())
27bc2bce
ILT
1824 {
1825 this->set_data_size(this->current_data_size_for_child());
1826 return;
1827 }
ead1e424 1828
27bc2bce
ILT
1829 uint64_t address = this->address();
1830 off_t startoff = this->offset();
ead1e424
ILT
1831 off_t off = startoff + this->first_input_offset_;
1832 for (Input_section_list::iterator p = this->input_sections_.begin();
1833 p != this->input_sections_.end();
1834 ++p)
1835 {
1836 off = align_address(off, p->addralign());
96803768
ILT
1837 p->set_address_and_file_offset(address + (off - startoff), off,
1838 startoff);
ead1e424
ILT
1839 off += p->data_size();
1840 }
1841
1842 this->set_data_size(off - startoff);
1843}
9a0910c3 1844
a445fddf
ILT
1845// Reset the address and file offset.
1846
1847void
1848Output_section::do_reset_address_and_file_offset()
1849{
1850 for (Input_section_list::iterator p = this->input_sections_.begin();
1851 p != this->input_sections_.end();
1852 ++p)
1853 p->reset_address_and_file_offset();
1854}
1855
7bf1f802
ILT
1856// Set the TLS offset. Called only for SHT_TLS sections.
1857
1858void
1859Output_section::do_set_tls_offset(uint64_t tls_base)
1860{
1861 this->tls_offset_ = this->address() - tls_base;
1862}
1863
61ba1cf9
ILT
1864// Write the section header to *OSHDR.
1865
1866template<int size, bool big_endian>
1867void
16649710
ILT
1868Output_section::write_header(const Layout* layout,
1869 const Stringpool* secnamepool,
61ba1cf9
ILT
1870 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1871{
1872 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1873 oshdr->put_sh_type(this->type_);
1874 oshdr->put_sh_flags(this->flags_);
1875 oshdr->put_sh_addr(this->address());
1876 oshdr->put_sh_offset(this->offset());
1877 oshdr->put_sh_size(this->data_size());
16649710
ILT
1878 if (this->link_section_ != NULL)
1879 oshdr->put_sh_link(this->link_section_->out_shndx());
1880 else if (this->should_link_to_symtab_)
1881 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1882 else if (this->should_link_to_dynsym_)
1883 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1884 else
1885 oshdr->put_sh_link(this->link_);
1886 if (this->info_section_ != NULL)
1887 oshdr->put_sh_info(this->info_section_->out_shndx());
1888 else
1889 oshdr->put_sh_info(this->info_);
61ba1cf9
ILT
1890 oshdr->put_sh_addralign(this->addralign_);
1891 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
1892}
1893
ead1e424
ILT
1894// Write out the data. For input sections the data is written out by
1895// Object::relocate, but we have to handle Output_section_data objects
1896// here.
1897
1898void
1899Output_section::do_write(Output_file* of)
1900{
96803768
ILT
1901 gold_assert(!this->requires_postprocessing());
1902
c51e6221
ILT
1903 off_t output_section_file_offset = this->offset();
1904 for (Fill_list::iterator p = this->fills_.begin();
1905 p != this->fills_.end();
1906 ++p)
1907 {
14144f39 1908 std::string fill_data(parameters->target()->code_fill(p->length()));
c51e6221 1909 of->write(output_section_file_offset + p->section_offset(),
a445fddf 1910 fill_data.data(), fill_data.size());
c51e6221
ILT
1911 }
1912
ead1e424
ILT
1913 for (Input_section_list::iterator p = this->input_sections_.begin();
1914 p != this->input_sections_.end();
1915 ++p)
1916 p->write(of);
1917}
1918
96803768
ILT
1919// If a section requires postprocessing, create the buffer to use.
1920
1921void
1922Output_section::create_postprocessing_buffer()
1923{
1924 gold_assert(this->requires_postprocessing());
1bedcac5
ILT
1925
1926 if (this->postprocessing_buffer_ != NULL)
1927 return;
96803768
ILT
1928
1929 if (!this->input_sections_.empty())
1930 {
1931 off_t off = this->first_input_offset_;
1932 for (Input_section_list::iterator p = this->input_sections_.begin();
1933 p != this->input_sections_.end();
1934 ++p)
1935 {
1936 off = align_address(off, p->addralign());
1937 p->finalize_data_size();
1938 off += p->data_size();
1939 }
1940 this->set_current_data_size_for_child(off);
1941 }
1942
1943 off_t buffer_size = this->current_data_size_for_child();
1944 this->postprocessing_buffer_ = new unsigned char[buffer_size];
1945}
1946
1947// Write all the data of an Output_section into the postprocessing
1948// buffer. This is used for sections which require postprocessing,
1949// such as compression. Input sections are handled by
1950// Object::Relocate.
1951
1952void
1953Output_section::write_to_postprocessing_buffer()
1954{
1955 gold_assert(this->requires_postprocessing());
1956
1957 Target* target = parameters->target();
1958 unsigned char* buffer = this->postprocessing_buffer();
1959 for (Fill_list::iterator p = this->fills_.begin();
1960 p != this->fills_.end();
1961 ++p)
1962 {
1963 std::string fill_data(target->code_fill(p->length()));
a445fddf
ILT
1964 memcpy(buffer + p->section_offset(), fill_data.data(),
1965 fill_data.size());
96803768
ILT
1966 }
1967
1968 off_t off = this->first_input_offset_;
1969 for (Input_section_list::iterator p = this->input_sections_.begin();
1970 p != this->input_sections_.end();
1971 ++p)
1972 {
1973 off = align_address(off, p->addralign());
1974 p->write_to_buffer(buffer + off);
1975 off += p->data_size();
1976 }
1977}
1978
a445fddf
ILT
1979// Get the input sections for linker script processing. We leave
1980// behind the Output_section_data entries. Note that this may be
1981// slightly incorrect for merge sections. We will leave them behind,
1982// but it is possible that the script says that they should follow
1983// some other input sections, as in:
1984// .rodata { *(.rodata) *(.rodata.cst*) }
1985// For that matter, we don't handle this correctly:
1986// .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
1987// With luck this will never matter.
1988
1989uint64_t
1990Output_section::get_input_sections(
1991 uint64_t address,
1992 const std::string& fill,
1993 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
1994{
1995 uint64_t orig_address = address;
1996
1997 address = align_address(address, this->addralign());
1998
1999 Input_section_list remaining;
2000 for (Input_section_list::iterator p = this->input_sections_.begin();
2001 p != this->input_sections_.end();
2002 ++p)
2003 {
2004 if (p->is_input_section())
2005 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2006 else
2007 {
2008 uint64_t aligned_address = align_address(address, p->addralign());
2009 if (aligned_address != address && !fill.empty())
2010 {
2011 section_size_type length =
2012 convert_to_section_size_type(aligned_address - address);
2013 std::string this_fill;
2014 this_fill.reserve(length);
2015 while (this_fill.length() + fill.length() <= length)
2016 this_fill += fill;
2017 if (this_fill.length() < length)
2018 this_fill.append(fill, 0, length - this_fill.length());
2019
2020 Output_section_data* posd = new Output_data_const(this_fill, 0);
2021 remaining.push_back(Input_section(posd));
2022 }
2023 address = aligned_address;
2024
2025 remaining.push_back(*p);
2026
2027 p->finalize_data_size();
2028 address += p->data_size();
2029 }
2030 }
2031
2032 this->input_sections_.swap(remaining);
2033 this->first_input_offset_ = 0;
2034
2035 uint64_t data_size = address - orig_address;
2036 this->set_current_data_size_for_child(data_size);
2037 return data_size;
2038}
2039
2040// Add an input section from a script.
2041
2042void
2043Output_section::add_input_section_for_script(Relobj* object,
2044 unsigned int shndx,
2045 off_t data_size,
2046 uint64_t addralign)
2047{
2048 if (addralign > this->addralign_)
2049 this->addralign_ = addralign;
2050
2051 off_t offset_in_section = this->current_data_size_for_child();
2052 off_t aligned_offset_in_section = align_address(offset_in_section,
2053 addralign);
2054
2055 this->set_current_data_size_for_child(aligned_offset_in_section
2056 + data_size);
2057
2058 this->input_sections_.push_back(Input_section(object, shndx,
2059 data_size, addralign));
2060}
2061
38c5e8b4
ILT
2062// Print stats for merge sections to stderr.
2063
2064void
2065Output_section::print_merge_stats()
2066{
2067 Input_section_list::iterator p;
2068 for (p = this->input_sections_.begin();
2069 p != this->input_sections_.end();
2070 ++p)
2071 p->print_merge_stats(this->name_);
2072}
2073
a2fb1b05
ILT
2074// Output segment methods.
2075
2076Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
54dc6425 2077 : output_data_(),
75f65a3e 2078 output_bss_(),
a2fb1b05
ILT
2079 vaddr_(0),
2080 paddr_(0),
2081 memsz_(0),
a445fddf
ILT
2082 max_align_(0),
2083 min_p_align_(0),
a2fb1b05
ILT
2084 offset_(0),
2085 filesz_(0),
2086 type_(type),
ead1e424 2087 flags_(flags),
a445fddf
ILT
2088 is_max_align_known_(false),
2089 are_addresses_set_(false)
a2fb1b05
ILT
2090{
2091}
2092
2093// Add an Output_section to an Output_segment.
2094
2095void
75f65a3e 2096Output_segment::add_output_section(Output_section* os,
dbe717ef
ILT
2097 elfcpp::Elf_Word seg_flags,
2098 bool front)
a2fb1b05 2099{
a3ad94ed 2100 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
a445fddf 2101 gold_assert(!this->is_max_align_known_);
75f65a3e 2102
ead1e424 2103 // Update the segment flags.
75f65a3e 2104 this->flags_ |= seg_flags;
75f65a3e
ILT
2105
2106 Output_segment::Output_data_list* pdl;
2107 if (os->type() == elfcpp::SHT_NOBITS)
2108 pdl = &this->output_bss_;
2109 else
2110 pdl = &this->output_data_;
54dc6425 2111
a2fb1b05
ILT
2112 // So that PT_NOTE segments will work correctly, we need to ensure
2113 // that all SHT_NOTE sections are adjacent. This will normally
2114 // happen automatically, because all the SHT_NOTE input sections
2115 // will wind up in the same output section. However, it is possible
2116 // for multiple SHT_NOTE input sections to have different section
2117 // flags, and thus be in different output sections, but for the
2118 // different section flags to map into the same segment flags and
2119 // thus the same output segment.
54dc6425
ILT
2120
2121 // Note that while there may be many input sections in an output
2122 // section, there are normally only a few output sections in an
2123 // output segment. This loop is expected to be fast.
2124
61ba1cf9 2125 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
a2fb1b05 2126 {
a3ad94ed 2127 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 2128 do
54dc6425 2129 {
75f65a3e 2130 --p;
54dc6425
ILT
2131 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2132 {
dbe717ef 2133 // We don't worry about the FRONT parameter.
54dc6425 2134 ++p;
75f65a3e 2135 pdl->insert(p, os);
54dc6425
ILT
2136 return;
2137 }
2138 }
75f65a3e 2139 while (p != pdl->begin());
54dc6425
ILT
2140 }
2141
2142 // Similarly, so that PT_TLS segments will work, we need to group
75f65a3e
ILT
2143 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2144 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2145 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
07f397ab
ILT
2146 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2147 // and the PT_TLS segment -- we do this grouping only for the
2148 // PT_LOAD segment.
2149 if (this->type_ != elfcpp::PT_TLS
2150 && (os->flags() & elfcpp::SHF_TLS) != 0
2151 && !this->output_data_.empty())
54dc6425 2152 {
75f65a3e
ILT
2153 pdl = &this->output_data_;
2154 bool nobits = os->type() == elfcpp::SHT_NOBITS;
ead1e424 2155 bool sawtls = false;
a3ad94ed 2156 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 2157 do
a2fb1b05 2158 {
75f65a3e 2159 --p;
ead1e424
ILT
2160 bool insert;
2161 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2162 {
2163 sawtls = true;
2164 // Put a NOBITS section after the first TLS section.
2165 // But a PROGBITS section after the first TLS/PROGBITS
2166 // section.
2167 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2168 }
2169 else
2170 {
2171 // If we've gone past the TLS sections, but we've seen a
2172 // TLS section, then we need to insert this section now.
2173 insert = sawtls;
2174 }
2175
2176 if (insert)
a2fb1b05 2177 {
dbe717ef 2178 // We don't worry about the FRONT parameter.
a2fb1b05 2179 ++p;
75f65a3e 2180 pdl->insert(p, os);
a2fb1b05
ILT
2181 return;
2182 }
2183 }
75f65a3e 2184 while (p != pdl->begin());
ead1e424 2185
dbe717ef
ILT
2186 // There are no TLS sections yet; put this one at the requested
2187 // location in the section list.
a2fb1b05
ILT
2188 }
2189
dbe717ef
ILT
2190 if (front)
2191 pdl->push_front(os);
2192 else
2193 pdl->push_back(os);
75f65a3e
ILT
2194}
2195
2196// Add an Output_data (which is not an Output_section) to the start of
2197// a segment.
2198
2199void
2200Output_segment::add_initial_output_data(Output_data* od)
2201{
a445fddf 2202 gold_assert(!this->is_max_align_known_);
75f65a3e
ILT
2203 this->output_data_.push_front(od);
2204}
2205
2206// Return the maximum alignment of the Output_data in Output_segment.
75f65a3e
ILT
2207
2208uint64_t
a445fddf 2209Output_segment::maximum_alignment()
75f65a3e 2210{
a445fddf 2211 if (!this->is_max_align_known_)
ead1e424
ILT
2212 {
2213 uint64_t addralign;
2214
a445fddf
ILT
2215 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2216 if (addralign > this->max_align_)
2217 this->max_align_ = addralign;
ead1e424 2218
a445fddf
ILT
2219 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2220 if (addralign > this->max_align_)
2221 this->max_align_ = addralign;
ead1e424 2222
a445fddf 2223 this->is_max_align_known_ = true;
ead1e424
ILT
2224 }
2225
a445fddf 2226 return this->max_align_;
75f65a3e
ILT
2227}
2228
ead1e424
ILT
2229// Return the maximum alignment of a list of Output_data.
2230
2231uint64_t
a445fddf 2232Output_segment::maximum_alignment_list(const Output_data_list* pdl)
ead1e424
ILT
2233{
2234 uint64_t ret = 0;
2235 for (Output_data_list::const_iterator p = pdl->begin();
2236 p != pdl->end();
2237 ++p)
2238 {
2239 uint64_t addralign = (*p)->addralign();
2240 if (addralign > ret)
2241 ret = addralign;
2242 }
2243 return ret;
2244}
2245
4f4c5f80
ILT
2246// Return the number of dynamic relocs applied to this segment.
2247
2248unsigned int
2249Output_segment::dynamic_reloc_count() const
2250{
2251 return (this->dynamic_reloc_count_list(&this->output_data_)
2252 + this->dynamic_reloc_count_list(&this->output_bss_));
2253}
2254
2255// Return the number of dynamic relocs applied to an Output_data_list.
2256
2257unsigned int
2258Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2259{
2260 unsigned int count = 0;
2261 for (Output_data_list::const_iterator p = pdl->begin();
2262 p != pdl->end();
2263 ++p)
2264 count += (*p)->dynamic_reloc_count();
2265 return count;
2266}
2267
a445fddf
ILT
2268// Set the section addresses for an Output_segment. If RESET is true,
2269// reset the addresses first. ADDR is the address and *POFF is the
2270// file offset. Set the section indexes starting with *PSHNDX.
2271// Return the address of the immediately following segment. Update
2272// *POFF and *PSHNDX.
75f65a3e
ILT
2273
2274uint64_t
a445fddf 2275Output_segment::set_section_addresses(bool reset, uint64_t addr, off_t* poff,
ead1e424 2276 unsigned int* pshndx)
75f65a3e 2277{
a3ad94ed 2278 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e 2279
a445fddf
ILT
2280 if (!reset && this->are_addresses_set_)
2281 {
2282 gold_assert(this->paddr_ == addr);
2283 addr = this->vaddr_;
2284 }
2285 else
2286 {
2287 this->vaddr_ = addr;
2288 this->paddr_ = addr;
2289 this->are_addresses_set_ = true;
2290 }
75f65a3e
ILT
2291
2292 off_t orig_off = *poff;
2293 this->offset_ = orig_off;
2294
a445fddf
ILT
2295 addr = this->set_section_list_addresses(reset, &this->output_data_,
2296 addr, poff, pshndx);
75f65a3e
ILT
2297 this->filesz_ = *poff - orig_off;
2298
2299 off_t off = *poff;
2300
a445fddf
ILT
2301 uint64_t ret = this->set_section_list_addresses(reset, &this->output_bss_,
2302 addr, poff, pshndx);
75f65a3e
ILT
2303 this->memsz_ = *poff - orig_off;
2304
2305 // Ignore the file offset adjustments made by the BSS Output_data
2306 // objects.
2307 *poff = off;
61ba1cf9
ILT
2308
2309 return ret;
75f65a3e
ILT
2310}
2311
b8e6aad9
ILT
2312// Set the addresses and file offsets in a list of Output_data
2313// structures.
75f65a3e
ILT
2314
2315uint64_t
a445fddf 2316Output_segment::set_section_list_addresses(bool reset, Output_data_list* pdl,
ead1e424
ILT
2317 uint64_t addr, off_t* poff,
2318 unsigned int* pshndx)
75f65a3e 2319{
ead1e424 2320 off_t startoff = *poff;
75f65a3e 2321
ead1e424 2322 off_t off = startoff;
75f65a3e
ILT
2323 for (Output_data_list::iterator p = pdl->begin();
2324 p != pdl->end();
2325 ++p)
2326 {
a445fddf
ILT
2327 if (reset)
2328 (*p)->reset_address_and_file_offset();
2329
2330 // When using a linker script the section will most likely
2331 // already have an address.
2332 if (!(*p)->is_address_valid())
3802b2dd
ILT
2333 {
2334 off = align_address(off, (*p)->addralign());
2335 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2336 }
a445fddf
ILT
2337 else
2338 {
2339 // The script may have inserted a skip forward, but it
2340 // better not have moved backward.
3802b2dd
ILT
2341 gold_assert((*p)->address() >= addr + (off - startoff));
2342 off += (*p)->address() - (addr + (off - startoff));
a445fddf
ILT
2343 (*p)->set_file_offset(off);
2344 (*p)->finalize_data_size();
2345 }
ead1e424
ILT
2346
2347 // Unless this is a PT_TLS segment, we want to ignore the size
2348 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
2349 // affect the size of a PT_LOAD segment.
2350 if (this->type_ == elfcpp::PT_TLS
2351 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
2352 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2353 off += (*p)->data_size();
75f65a3e 2354
ead1e424
ILT
2355 if ((*p)->is_section())
2356 {
2357 (*p)->set_out_shndx(*pshndx);
2358 ++*pshndx;
2359 }
75f65a3e
ILT
2360 }
2361
2362 *poff = off;
ead1e424 2363 return addr + (off - startoff);
75f65a3e
ILT
2364}
2365
2366// For a non-PT_LOAD segment, set the offset from the sections, if
2367// any.
2368
2369void
2370Output_segment::set_offset()
2371{
a3ad94ed 2372 gold_assert(this->type_ != elfcpp::PT_LOAD);
75f65a3e 2373
a445fddf
ILT
2374 gold_assert(!this->are_addresses_set_);
2375
75f65a3e
ILT
2376 if (this->output_data_.empty() && this->output_bss_.empty())
2377 {
2378 this->vaddr_ = 0;
2379 this->paddr_ = 0;
a445fddf 2380 this->are_addresses_set_ = true;
75f65a3e 2381 this->memsz_ = 0;
a445fddf 2382 this->min_p_align_ = 0;
75f65a3e
ILT
2383 this->offset_ = 0;
2384 this->filesz_ = 0;
2385 return;
2386 }
2387
2388 const Output_data* first;
2389 if (this->output_data_.empty())
2390 first = this->output_bss_.front();
2391 else
2392 first = this->output_data_.front();
2393 this->vaddr_ = first->address();
a445fddf
ILT
2394 this->paddr_ = (first->has_load_address()
2395 ? first->load_address()
2396 : this->vaddr_);
2397 this->are_addresses_set_ = true;
75f65a3e
ILT
2398 this->offset_ = first->offset();
2399
2400 if (this->output_data_.empty())
2401 this->filesz_ = 0;
2402 else
2403 {
2404 const Output_data* last_data = this->output_data_.back();
2405 this->filesz_ = (last_data->address()
2406 + last_data->data_size()
2407 - this->vaddr_);
2408 }
2409
2410 const Output_data* last;
2411 if (this->output_bss_.empty())
2412 last = this->output_data_.back();
2413 else
2414 last = this->output_bss_.back();
2415 this->memsz_ = (last->address()
2416 + last->data_size()
2417 - this->vaddr_);
75f65a3e
ILT
2418}
2419
7bf1f802
ILT
2420// Set the TLS offsets of the sections in the PT_TLS segment.
2421
2422void
2423Output_segment::set_tls_offsets()
2424{
2425 gold_assert(this->type_ == elfcpp::PT_TLS);
2426
2427 for (Output_data_list::iterator p = this->output_data_.begin();
2428 p != this->output_data_.end();
2429 ++p)
2430 (*p)->set_tls_offset(this->vaddr_);
2431
2432 for (Output_data_list::iterator p = this->output_bss_.begin();
2433 p != this->output_bss_.end();
2434 ++p)
2435 (*p)->set_tls_offset(this->vaddr_);
2436}
2437
a445fddf
ILT
2438// Return the address of the first section.
2439
2440uint64_t
2441Output_segment::first_section_load_address() const
2442{
2443 for (Output_data_list::const_iterator p = this->output_data_.begin();
2444 p != this->output_data_.end();
2445 ++p)
2446 if ((*p)->is_section())
2447 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2448
2449 for (Output_data_list::const_iterator p = this->output_bss_.begin();
2450 p != this->output_bss_.end();
2451 ++p)
2452 if ((*p)->is_section())
2453 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
2454
2455 gold_unreachable();
2456}
2457
75f65a3e
ILT
2458// Return the number of Output_sections in an Output_segment.
2459
2460unsigned int
2461Output_segment::output_section_count() const
2462{
2463 return (this->output_section_count_list(&this->output_data_)
2464 + this->output_section_count_list(&this->output_bss_));
2465}
2466
2467// Return the number of Output_sections in an Output_data_list.
2468
2469unsigned int
2470Output_segment::output_section_count_list(const Output_data_list* pdl) const
2471{
2472 unsigned int count = 0;
2473 for (Output_data_list::const_iterator p = pdl->begin();
2474 p != pdl->end();
2475 ++p)
2476 {
2477 if ((*p)->is_section())
2478 ++count;
2479 }
2480 return count;
a2fb1b05
ILT
2481}
2482
1c4f3631
ILT
2483// Return the section attached to the list segment with the lowest
2484// load address. This is used when handling a PHDRS clause in a
2485// linker script.
2486
2487Output_section*
2488Output_segment::section_with_lowest_load_address() const
2489{
2490 Output_section* found = NULL;
2491 uint64_t found_lma = 0;
2492 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
2493
2494 Output_section* found_data = found;
2495 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
2496 if (found != found_data && found_data != NULL)
2497 {
2498 gold_error(_("nobits section %s may not precede progbits section %s "
2499 "in same segment"),
2500 found->name(), found_data->name());
2501 return NULL;
2502 }
2503
2504 return found;
2505}
2506
2507// Look through a list for a section with a lower load address.
2508
2509void
2510Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
2511 Output_section** found,
2512 uint64_t* found_lma) const
2513{
2514 for (Output_data_list::const_iterator p = pdl->begin();
2515 p != pdl->end();
2516 ++p)
2517 {
2518 if (!(*p)->is_section())
2519 continue;
2520 Output_section* os = static_cast<Output_section*>(*p);
2521 uint64_t lma = (os->has_load_address()
2522 ? os->load_address()
2523 : os->address());
2524 if (*found == NULL || lma < *found_lma)
2525 {
2526 *found = os;
2527 *found_lma = lma;
2528 }
2529 }
2530}
2531
61ba1cf9
ILT
2532// Write the segment data into *OPHDR.
2533
2534template<int size, bool big_endian>
2535void
ead1e424 2536Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
61ba1cf9
ILT
2537{
2538 ophdr->put_p_type(this->type_);
2539 ophdr->put_p_offset(this->offset_);
2540 ophdr->put_p_vaddr(this->vaddr_);
2541 ophdr->put_p_paddr(this->paddr_);
2542 ophdr->put_p_filesz(this->filesz_);
2543 ophdr->put_p_memsz(this->memsz_);
2544 ophdr->put_p_flags(this->flags_);
a445fddf 2545 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
61ba1cf9
ILT
2546}
2547
2548// Write the section headers into V.
2549
2550template<int size, bool big_endian>
2551unsigned char*
16649710
ILT
2552Output_segment::write_section_headers(const Layout* layout,
2553 const Stringpool* secnamepool,
ead1e424
ILT
2554 unsigned char* v,
2555 unsigned int *pshndx
5482377d
ILT
2556 ACCEPT_SIZE_ENDIAN) const
2557{
ead1e424
ILT
2558 // Every section that is attached to a segment must be attached to a
2559 // PT_LOAD segment, so we only write out section headers for PT_LOAD
2560 // segments.
2561 if (this->type_ != elfcpp::PT_LOAD)
2562 return v;
2563
593f47df
ILT
2564 v = this->write_section_headers_list
2565 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
16649710 2566 layout, secnamepool, &this->output_data_, v, pshndx
593f47df
ILT
2567 SELECT_SIZE_ENDIAN(size, big_endian));
2568 v = this->write_section_headers_list
2569 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
16649710 2570 layout, secnamepool, &this->output_bss_, v, pshndx
593f47df 2571 SELECT_SIZE_ENDIAN(size, big_endian));
61ba1cf9
ILT
2572 return v;
2573}
2574
2575template<int size, bool big_endian>
2576unsigned char*
16649710
ILT
2577Output_segment::write_section_headers_list(const Layout* layout,
2578 const Stringpool* secnamepool,
61ba1cf9 2579 const Output_data_list* pdl,
ead1e424
ILT
2580 unsigned char* v,
2581 unsigned int* pshndx
5482377d 2582 ACCEPT_SIZE_ENDIAN) const
61ba1cf9
ILT
2583{
2584 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2585 for (Output_data_list::const_iterator p = pdl->begin();
2586 p != pdl->end();
2587 ++p)
2588 {
2589 if ((*p)->is_section())
2590 {
5482377d 2591 const Output_section* ps = static_cast<const Output_section*>(*p);
a3ad94ed 2592 gold_assert(*pshndx == ps->out_shndx());
61ba1cf9 2593 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 2594 ps->write_header(layout, secnamepool, &oshdr);
61ba1cf9 2595 v += shdr_size;
ead1e424 2596 ++*pshndx;
61ba1cf9
ILT
2597 }
2598 }
2599 return v;
2600}
2601
a2fb1b05
ILT
2602// Output_file methods.
2603
14144f39
ILT
2604Output_file::Output_file(const char* name)
2605 : name_(name),
61ba1cf9
ILT
2606 o_(-1),
2607 file_size_(0),
c420411f
ILT
2608 base_(NULL),
2609 map_is_anonymous_(false)
61ba1cf9
ILT
2610{
2611}
2612
2613// Open the output file.
2614
a2fb1b05 2615void
61ba1cf9 2616Output_file::open(off_t file_size)
a2fb1b05 2617{
61ba1cf9
ILT
2618 this->file_size_ = file_size;
2619
4e9d8586
ILT
2620 // Unlink the file first; otherwise the open() may fail if the file
2621 // is busy (e.g. it's an executable that's currently being executed).
2622 //
2623 // However, the linker may be part of a system where a zero-length
2624 // file is created for it to write to, with tight permissions (gcc
2625 // 2.95 did something like this). Unlinking the file would work
2626 // around those permission controls, so we only unlink if the file
2627 // has a non-zero size. We also unlink only regular files to avoid
2628 // trouble with directories/etc.
2629 //
2630 // If we fail, continue; this command is merely a best-effort attempt
2631 // to improve the odds for open().
2632
42a1b686
ILT
2633 // We let the name "-" mean "stdout"
2634 if (strcmp(this->name_, "-") == 0)
2635 this->o_ = STDOUT_FILENO;
2636 else
2637 {
2638 struct stat s;
2639 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
2640 unlink_if_ordinary(this->name_);
2641
2642 int mode = parameters->output_is_object() ? 0666 : 0777;
2643 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
2644 if (o < 0)
2645 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
2646 this->o_ = o;
2647 }
61ba1cf9 2648
27bc2bce
ILT
2649 this->map();
2650}
2651
2652// Resize the output file.
2653
2654void
2655Output_file::resize(off_t file_size)
2656{
c420411f
ILT
2657 // If the mmap is mapping an anonymous memory buffer, this is easy:
2658 // just mremap to the new size. If it's mapping to a file, we want
2659 // to unmap to flush to the file, then remap after growing the file.
2660 if (this->map_is_anonymous_)
2661 {
2662 void* base = ::mremap(this->base_, this->file_size_, file_size,
2663 MREMAP_MAYMOVE);
2664 if (base == MAP_FAILED)
2665 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
2666 this->base_ = static_cast<unsigned char*>(base);
2667 this->file_size_ = file_size;
2668 }
2669 else
2670 {
2671 this->unmap();
2672 this->file_size_ = file_size;
2673 this->map();
2674 }
27bc2bce
ILT
2675}
2676
2677// Map the file into memory.
2678
2679void
2680Output_file::map()
2681{
c420411f 2682 const int o = this->o_;
61ba1cf9 2683
c420411f
ILT
2684 // If the output file is not a regular file, don't try to mmap it;
2685 // instead, we'll mmap a block of memory (an anonymous buffer), and
2686 // then later write the buffer to the file.
2687 void* base;
2688 struct stat statbuf;
42a1b686
ILT
2689 if (o == STDOUT_FILENO || o == STDERR_FILENO
2690 || ::fstat(o, &statbuf) != 0
c420411f
ILT
2691 || !S_ISREG(statbuf.st_mode))
2692 {
2693 this->map_is_anonymous_ = true;
2694 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2695 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
2696 }
2697 else
2698 {
2699 // Write out one byte to make the file the right size.
2700 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
2701 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
2702 char b = 0;
2703 if (::write(o, &b, 1) != 1)
2704 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
2705
2706 // Map the file into memory.
2707 this->map_is_anonymous_ = false;
2708 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
2709 MAP_SHARED, o, 0);
2710 }
61ba1cf9 2711 if (base == MAP_FAILED)
75f2446e 2712 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
2713 this->base_ = static_cast<unsigned char*>(base);
2714}
2715
c420411f 2716// Unmap the file from memory.
61ba1cf9
ILT
2717
2718void
c420411f 2719Output_file::unmap()
61ba1cf9
ILT
2720{
2721 if (::munmap(this->base_, this->file_size_) < 0)
a0c4fb0a 2722 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
61ba1cf9 2723 this->base_ = NULL;
c420411f
ILT
2724}
2725
2726// Close the output file.
2727
2728void
2729Output_file::close()
2730{
2731 // If the map isn't file-backed, we need to write it now.
2732 if (this->map_is_anonymous_)
2733 {
2734 size_t bytes_to_write = this->file_size_;
2735 while (bytes_to_write > 0)
2736 {
2737 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
2738 if (bytes_written == 0)
2739 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
2740 else if (bytes_written < 0)
2741 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
2742 else
2743 bytes_to_write -= bytes_written;
2744 }
2745 }
2746 this->unmap();
61ba1cf9 2747
42a1b686
ILT
2748 // We don't close stdout or stderr
2749 if (this->o_ != STDOUT_FILENO && this->o_ != STDERR_FILENO)
2750 if (::close(this->o_) < 0)
2751 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
61ba1cf9 2752 this->o_ = -1;
a2fb1b05
ILT
2753}
2754
2755// Instantiate the templates we need. We could use the configure
2756// script to restrict this to only the ones for implemented targets.
2757
193a53d9 2758#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
2759template
2760off_t
2761Output_section::add_input_section<32, false>(
730cdc88 2762 Sized_relobj<32, false>* object,
ead1e424 2763 unsigned int shndx,
a2fb1b05 2764 const char* secname,
730cdc88 2765 const elfcpp::Shdr<32, false>& shdr,
a445fddf
ILT
2766 unsigned int reloc_shndx,
2767 bool have_sections_script);
193a53d9 2768#endif
a2fb1b05 2769
193a53d9 2770#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
2771template
2772off_t
2773Output_section::add_input_section<32, true>(
730cdc88 2774 Sized_relobj<32, true>* object,
ead1e424 2775 unsigned int shndx,
a2fb1b05 2776 const char* secname,
730cdc88 2777 const elfcpp::Shdr<32, true>& shdr,
a445fddf
ILT
2778 unsigned int reloc_shndx,
2779 bool have_sections_script);
193a53d9 2780#endif
a2fb1b05 2781
193a53d9 2782#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
2783template
2784off_t
2785Output_section::add_input_section<64, false>(
730cdc88 2786 Sized_relobj<64, false>* object,
ead1e424 2787 unsigned int shndx,
a2fb1b05 2788 const char* secname,
730cdc88 2789 const elfcpp::Shdr<64, false>& shdr,
a445fddf
ILT
2790 unsigned int reloc_shndx,
2791 bool have_sections_script);
193a53d9 2792#endif
a2fb1b05 2793
193a53d9 2794#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
2795template
2796off_t
2797Output_section::add_input_section<64, true>(
730cdc88 2798 Sized_relobj<64, true>* object,
ead1e424 2799 unsigned int shndx,
a2fb1b05 2800 const char* secname,
730cdc88 2801 const elfcpp::Shdr<64, true>& shdr,
a445fddf
ILT
2802 unsigned int reloc_shndx,
2803 bool have_sections_script);
193a53d9 2804#endif
a2fb1b05 2805
193a53d9 2806#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
2807template
2808class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
193a53d9 2809#endif
c06b7b0b 2810
193a53d9 2811#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
2812template
2813class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
193a53d9 2814#endif
c06b7b0b 2815
193a53d9 2816#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
2817template
2818class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
193a53d9 2819#endif
c06b7b0b 2820
193a53d9 2821#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
2822template
2823class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
193a53d9 2824#endif
c06b7b0b 2825
193a53d9 2826#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
2827template
2828class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
193a53d9 2829#endif
c06b7b0b 2830
193a53d9 2831#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
2832template
2833class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
193a53d9 2834#endif
c06b7b0b 2835
193a53d9 2836#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
2837template
2838class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
193a53d9 2839#endif
c06b7b0b 2840
193a53d9 2841#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
2842template
2843class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
193a53d9 2844#endif
c06b7b0b 2845
193a53d9 2846#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
2847template
2848class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
193a53d9 2849#endif
c06b7b0b 2850
193a53d9 2851#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
2852template
2853class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
193a53d9 2854#endif
c06b7b0b 2855
193a53d9 2856#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
2857template
2858class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
193a53d9 2859#endif
c06b7b0b 2860
193a53d9 2861#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
2862template
2863class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
193a53d9 2864#endif
c06b7b0b 2865
193a53d9 2866#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
2867template
2868class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
193a53d9 2869#endif
c06b7b0b 2870
193a53d9 2871#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
2872template
2873class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
193a53d9 2874#endif
c06b7b0b 2875
193a53d9 2876#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
2877template
2878class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
193a53d9 2879#endif
c06b7b0b 2880
193a53d9 2881#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
2882template
2883class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
193a53d9 2884#endif
c06b7b0b 2885
193a53d9 2886#ifdef HAVE_TARGET_32_LITTLE
ead1e424 2887template
dbe717ef 2888class Output_data_got<32, false>;
193a53d9 2889#endif
ead1e424 2890
193a53d9 2891#ifdef HAVE_TARGET_32_BIG
ead1e424 2892template
dbe717ef 2893class Output_data_got<32, true>;
193a53d9 2894#endif
ead1e424 2895
193a53d9 2896#ifdef HAVE_TARGET_64_LITTLE
ead1e424 2897template
dbe717ef 2898class Output_data_got<64, false>;
193a53d9 2899#endif
ead1e424 2900
193a53d9 2901#ifdef HAVE_TARGET_64_BIG
ead1e424 2902template
dbe717ef 2903class Output_data_got<64, true>;
193a53d9 2904#endif
ead1e424 2905
a2fb1b05 2906} // End namespace gold.