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