*** empty log message ***
[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,
ef9beddf 620 Sized_relobj<size, big_endian>* relobj,
7bf1f802 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,
ef9beddf 710 Sized_relobj<size, big_endian>* relobj,
7bf1f802
ILT
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
ef9beddf 758 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
dceae3c1
ILT
759 }
760 break;
761 }
7bf1f802
ILT
762}
763
c06b7b0b
ILT
764// Get the symbol index of a relocation.
765
766template<bool dynamic, int size, bool big_endian>
767unsigned int
768Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
769 const
770{
771 unsigned int index;
772 switch (this->local_sym_index_)
773 {
774 case INVALID_CODE:
a3ad94ed 775 gold_unreachable();
c06b7b0b
ILT
776
777 case GSYM_CODE:
5a6f7e2d 778 if (this->u1_.gsym == NULL)
c06b7b0b
ILT
779 index = 0;
780 else if (dynamic)
5a6f7e2d 781 index = this->u1_.gsym->dynsym_index();
c06b7b0b 782 else
5a6f7e2d 783 index = this->u1_.gsym->symtab_index();
c06b7b0b
ILT
784 break;
785
786 case SECTION_CODE:
787 if (dynamic)
5a6f7e2d 788 index = this->u1_.os->dynsym_index();
c06b7b0b 789 else
5a6f7e2d 790 index = this->u1_.os->symtab_index();
c06b7b0b
ILT
791 break;
792
436ca963
ILT
793 case 0:
794 // Relocations without symbols use a symbol index of 0.
795 index = 0;
796 break;
797
c06b7b0b 798 default:
dceae3c1
ILT
799 {
800 const unsigned int lsi = this->local_sym_index_;
801 if (!this->is_section_symbol_)
802 {
803 if (dynamic)
804 index = this->u1_.relobj->dynsym_index(lsi);
805 else
806 index = this->u1_.relobj->symtab_index(lsi);
807 }
808 else
809 {
ef9beddf 810 Output_section* os = this->u1_.relobj->output_section(lsi);
dceae3c1
ILT
811 gold_assert(os != NULL);
812 if (dynamic)
813 index = os->dynsym_index();
814 else
815 index = os->symtab_index();
816 }
817 }
c06b7b0b
ILT
818 break;
819 }
a3ad94ed 820 gold_assert(index != -1U);
c06b7b0b
ILT
821 return index;
822}
823
624f8810
ILT
824// For a local section symbol, get the address of the offset ADDEND
825// within the input section.
dceae3c1
ILT
826
827template<bool dynamic, int size, bool big_endian>
ef9beddf 828typename elfcpp::Elf_types<size>::Elf_Addr
dceae3c1 829Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
624f8810 830 local_section_offset(Addend addend) const
dceae3c1 831{
624f8810
ILT
832 gold_assert(this->local_sym_index_ != GSYM_CODE
833 && this->local_sym_index_ != SECTION_CODE
834 && this->local_sym_index_ != INVALID_CODE
835 && this->is_section_symbol_);
dceae3c1 836 const unsigned int lsi = this->local_sym_index_;
ef9beddf 837 Output_section* os = this->u1_.relobj->output_section(lsi);
624f8810 838 gold_assert(os != NULL);
ef9beddf
ILT
839 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
840 if (offset != -1U)
624f8810
ILT
841 return offset + addend;
842 // This is a merge section.
843 offset = os->output_address(this->u1_.relobj, lsi, addend);
ef9beddf 844 gold_assert(offset != -1U);
dceae3c1
ILT
845 return offset;
846}
847
d98bc257 848// Get the output address of a relocation.
c06b7b0b
ILT
849
850template<bool dynamic, int size, bool big_endian>
a984ee1d 851typename elfcpp::Elf_types<size>::Elf_Addr
d98bc257 852Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
c06b7b0b 853{
a3ad94ed 854 Address address = this->address_;
5a6f7e2d
ILT
855 if (this->shndx_ != INVALID_CODE)
856 {
ef9beddf 857 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
5a6f7e2d 858 gold_assert(os != NULL);
ef9beddf
ILT
859 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
860 if (off != -1U)
730cdc88
ILT
861 address += os->address() + off;
862 else
863 {
864 address = os->output_address(this->u2_.relobj, this->shndx_,
865 address);
866 gold_assert(address != -1U);
867 }
5a6f7e2d
ILT
868 }
869 else if (this->u2_.od != NULL)
870 address += this->u2_.od->address();
d98bc257
ILT
871 return address;
872}
873
874// Write out the offset and info fields of a Rel or Rela relocation
875// entry.
876
877template<bool dynamic, int size, bool big_endian>
878template<typename Write_rel>
879void
880Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
881 Write_rel* wr) const
882{
883 wr->put_r_offset(this->get_address());
e8c846c3
ILT
884 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
885 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
c06b7b0b
ILT
886}
887
888// Write out a Rel relocation.
889
890template<bool dynamic, int size, bool big_endian>
891void
892Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
893 unsigned char* pov) const
894{
895 elfcpp::Rel_write<size, big_endian> orel(pov);
896 this->write_rel(&orel);
897}
898
e8c846c3
ILT
899// Get the value of the symbol referred to by a Rel relocation.
900
901template<bool dynamic, int size, bool big_endian>
902typename elfcpp::Elf_types<size>::Elf_Addr
d1f003c6 903Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
624f8810 904 Addend addend) const
e8c846c3
ILT
905{
906 if (this->local_sym_index_ == GSYM_CODE)
907 {
908 const Sized_symbol<size>* sym;
909 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
d1f003c6 910 return sym->value() + addend;
e8c846c3
ILT
911 }
912 gold_assert(this->local_sym_index_ != SECTION_CODE
d1f003c6
ILT
913 && this->local_sym_index_ != INVALID_CODE
914 && !this->is_section_symbol_);
915 const unsigned int lsi = this->local_sym_index_;
916 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
917 return symval->value(this->u1_.relobj, addend);
e8c846c3
ILT
918}
919
d98bc257
ILT
920// Reloc comparison. This function sorts the dynamic relocs for the
921// benefit of the dynamic linker. First we sort all relative relocs
922// to the front. Among relative relocs, we sort by output address.
923// Among non-relative relocs, we sort by symbol index, then by output
924// address.
925
926template<bool dynamic, int size, bool big_endian>
927int
928Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
929 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
930 const
931{
932 if (this->is_relative_)
933 {
934 if (!r2.is_relative_)
935 return -1;
936 // Otherwise sort by reloc address below.
937 }
938 else if (r2.is_relative_)
939 return 1;
940 else
941 {
942 unsigned int sym1 = this->get_symbol_index();
943 unsigned int sym2 = r2.get_symbol_index();
944 if (sym1 < sym2)
945 return -1;
946 else if (sym1 > sym2)
947 return 1;
948 // Otherwise sort by reloc address.
949 }
950
951 section_offset_type addr1 = this->get_address();
952 section_offset_type addr2 = r2.get_address();
953 if (addr1 < addr2)
954 return -1;
955 else if (addr1 > addr2)
956 return 1;
957
958 // Final tie breaker, in order to generate the same output on any
959 // host: reloc type.
960 unsigned int type1 = this->type_;
961 unsigned int type2 = r2.type_;
962 if (type1 < type2)
963 return -1;
964 else if (type1 > type2)
965 return 1;
966
967 // These relocs appear to be exactly the same.
968 return 0;
969}
970
c06b7b0b
ILT
971// Write out a Rela relocation.
972
973template<bool dynamic, int size, bool big_endian>
974void
975Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
976 unsigned char* pov) const
977{
978 elfcpp::Rela_write<size, big_endian> orel(pov);
979 this->rel_.write_rel(&orel);
e8c846c3 980 Addend addend = this->addend_;
dceae3c1 981 if (this->rel_.is_relative())
d1f003c6
ILT
982 addend = this->rel_.symbol_value(addend);
983 else if (this->rel_.is_local_section_symbol())
624f8810 984 addend = this->rel_.local_section_offset(addend);
e8c846c3 985 orel.put_r_addend(addend);
c06b7b0b
ILT
986}
987
988// Output_data_reloc_base methods.
989
16649710
ILT
990// Adjust the output section.
991
992template<int sh_type, bool dynamic, int size, bool big_endian>
993void
994Output_data_reloc_base<sh_type, dynamic, size, big_endian>
995 ::do_adjust_output_section(Output_section* os)
996{
997 if (sh_type == elfcpp::SHT_REL)
998 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
999 else if (sh_type == elfcpp::SHT_RELA)
1000 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1001 else
1002 gold_unreachable();
1003 if (dynamic)
1004 os->set_should_link_to_dynsym();
1005 else
1006 os->set_should_link_to_symtab();
1007}
1008
c06b7b0b
ILT
1009// Write out relocation data.
1010
1011template<int sh_type, bool dynamic, int size, bool big_endian>
1012void
1013Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1014 Output_file* of)
1015{
1016 const off_t off = this->offset();
1017 const off_t oview_size = this->data_size();
1018 unsigned char* const oview = of->get_output_view(off, oview_size);
1019
d98bc257
ILT
1020 if (this->sort_relocs_)
1021 {
1022 gold_assert(dynamic);
1023 std::sort(this->relocs_.begin(), this->relocs_.end(),
1024 Sort_relocs_comparison());
1025 }
1026
c06b7b0b
ILT
1027 unsigned char* pov = oview;
1028 for (typename Relocs::const_iterator p = this->relocs_.begin();
1029 p != this->relocs_.end();
1030 ++p)
1031 {
1032 p->write(pov);
1033 pov += reloc_size;
1034 }
1035
a3ad94ed 1036 gold_assert(pov - oview == oview_size);
c06b7b0b
ILT
1037
1038 of->write_output_view(off, oview_size, oview);
1039
1040 // We no longer need the relocation entries.
1041 this->relocs_.clear();
1042}
1043
6a74a719
ILT
1044// Class Output_relocatable_relocs.
1045
1046template<int sh_type, int size, bool big_endian>
1047void
1048Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1049{
1050 this->set_data_size(this->rr_->output_reloc_count()
1051 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1052}
1053
1054// class Output_data_group.
1055
1056template<int size, bool big_endian>
1057Output_data_group<size, big_endian>::Output_data_group(
1058 Sized_relobj<size, big_endian>* relobj,
1059 section_size_type entry_count,
8825ac63
ILT
1060 elfcpp::Elf_Word flags,
1061 std::vector<unsigned int>* input_shndxes)
6a74a719 1062 : Output_section_data(entry_count * 4, 4),
8825ac63
ILT
1063 relobj_(relobj),
1064 flags_(flags)
6a74a719 1065{
8825ac63 1066 this->input_shndxes_.swap(*input_shndxes);
6a74a719
ILT
1067}
1068
1069// Write out the section group, which means translating the section
1070// indexes to apply to the output file.
1071
1072template<int size, bool big_endian>
1073void
1074Output_data_group<size, big_endian>::do_write(Output_file* of)
1075{
1076 const off_t off = this->offset();
1077 const section_size_type oview_size =
1078 convert_to_section_size_type(this->data_size());
1079 unsigned char* const oview = of->get_output_view(off, oview_size);
1080
1081 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1082 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1083 ++contents;
1084
1085 for (std::vector<unsigned int>::const_iterator p =
8825ac63
ILT
1086 this->input_shndxes_.begin();
1087 p != this->input_shndxes_.end();
6a74a719
ILT
1088 ++p, ++contents)
1089 {
ef9beddf 1090 Output_section* os = this->relobj_->output_section(*p);
6a74a719
ILT
1091
1092 unsigned int output_shndx;
1093 if (os != NULL)
1094 output_shndx = os->out_shndx();
1095 else
1096 {
1097 this->relobj_->error(_("section group retained but "
1098 "group element discarded"));
1099 output_shndx = 0;
1100 }
1101
1102 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1103 }
1104
1105 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1106 gold_assert(wrote == oview_size);
1107
1108 of->write_output_view(off, oview_size, oview);
1109
1110 // We no longer need this information.
8825ac63 1111 this->input_shndxes_.clear();
6a74a719
ILT
1112}
1113
dbe717ef 1114// Output_data_got::Got_entry methods.
ead1e424
ILT
1115
1116// Write out the entry.
1117
1118template<int size, bool big_endian>
1119void
7e1edb90 1120Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
ead1e424
ILT
1121{
1122 Valtype val = 0;
1123
1124 switch (this->local_sym_index_)
1125 {
1126 case GSYM_CODE:
1127 {
e8c846c3
ILT
1128 // If the symbol is resolved locally, we need to write out the
1129 // link-time value, which will be relocated dynamically by a
1130 // RELATIVE relocation.
ead1e424 1131 Symbol* gsym = this->u_.gsym;
e8c846c3
ILT
1132 Sized_symbol<size>* sgsym;
1133 // This cast is a bit ugly. We don't want to put a
1134 // virtual method in Symbol, because we want Symbol to be
1135 // as small as possible.
1136 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1137 val = sgsym->value();
ead1e424
ILT
1138 }
1139 break;
1140
1141 case CONSTANT_CODE:
1142 val = this->u_.constant;
1143 break;
1144
1145 default:
d1f003c6
ILT
1146 {
1147 const unsigned int lsi = this->local_sym_index_;
1148 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1149 val = symval->value(this->u_.object, 0);
1150 }
e727fa71 1151 break;
ead1e424
ILT
1152 }
1153
a3ad94ed 1154 elfcpp::Swap<size, big_endian>::writeval(pov, val);
ead1e424
ILT
1155}
1156
dbe717ef 1157// Output_data_got methods.
ead1e424 1158
dbe717ef
ILT
1159// Add an entry for a global symbol to the GOT. This returns true if
1160// this is a new GOT entry, false if the symbol already had a GOT
1161// entry.
1162
1163template<int size, bool big_endian>
1164bool
0a65a3a7
CC
1165Output_data_got<size, big_endian>::add_global(
1166 Symbol* gsym,
1167 unsigned int got_type)
ead1e424 1168{
0a65a3a7 1169 if (gsym->has_got_offset(got_type))
dbe717ef 1170 return false;
ead1e424 1171
dbe717ef
ILT
1172 this->entries_.push_back(Got_entry(gsym));
1173 this->set_got_size();
0a65a3a7 1174 gsym->set_got_offset(got_type, this->last_got_offset());
dbe717ef
ILT
1175 return true;
1176}
ead1e424 1177
7bf1f802
ILT
1178// Add an entry for a global symbol to the GOT, and add a dynamic
1179// relocation of type R_TYPE for the GOT entry.
1180template<int size, bool big_endian>
1181void
1182Output_data_got<size, big_endian>::add_global_with_rel(
1183 Symbol* gsym,
0a65a3a7 1184 unsigned int got_type,
7bf1f802
ILT
1185 Rel_dyn* rel_dyn,
1186 unsigned int r_type)
1187{
0a65a3a7 1188 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1189 return;
1190
1191 this->entries_.push_back(Got_entry());
1192 this->set_got_size();
1193 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1194 gsym->set_got_offset(got_type, got_offset);
7bf1f802
ILT
1195 rel_dyn->add_global(gsym, r_type, this, got_offset);
1196}
1197
1198template<int size, bool big_endian>
1199void
1200Output_data_got<size, big_endian>::add_global_with_rela(
1201 Symbol* gsym,
0a65a3a7 1202 unsigned int got_type,
7bf1f802
ILT
1203 Rela_dyn* rela_dyn,
1204 unsigned int r_type)
1205{
0a65a3a7 1206 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1207 return;
1208
1209 this->entries_.push_back(Got_entry());
1210 this->set_got_size();
1211 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1212 gsym->set_got_offset(got_type, got_offset);
7bf1f802
ILT
1213 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1214}
1215
0a65a3a7
CC
1216// Add a pair of entries for a global symbol to the GOT, and add
1217// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1218// If R_TYPE_2 == 0, add the second entry with no relocation.
7bf1f802
ILT
1219template<int size, bool big_endian>
1220void
0a65a3a7
CC
1221Output_data_got<size, big_endian>::add_global_pair_with_rel(
1222 Symbol* gsym,
1223 unsigned int got_type,
7bf1f802 1224 Rel_dyn* rel_dyn,
0a65a3a7
CC
1225 unsigned int r_type_1,
1226 unsigned int r_type_2)
7bf1f802 1227{
0a65a3a7 1228 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1229 return;
1230
1231 this->entries_.push_back(Got_entry());
7bf1f802 1232 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1233 gsym->set_got_offset(got_type, got_offset);
1234 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1235
1236 this->entries_.push_back(Got_entry());
1237 if (r_type_2 != 0)
1238 {
1239 got_offset = this->last_got_offset();
1240 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1241 }
1242
1243 this->set_got_size();
7bf1f802
ILT
1244}
1245
1246template<int size, bool big_endian>
1247void
0a65a3a7
CC
1248Output_data_got<size, big_endian>::add_global_pair_with_rela(
1249 Symbol* gsym,
1250 unsigned int got_type,
7bf1f802 1251 Rela_dyn* rela_dyn,
0a65a3a7
CC
1252 unsigned int r_type_1,
1253 unsigned int r_type_2)
7bf1f802 1254{
0a65a3a7 1255 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1256 return;
1257
1258 this->entries_.push_back(Got_entry());
7bf1f802 1259 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1260 gsym->set_got_offset(got_type, got_offset);
1261 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1262
1263 this->entries_.push_back(Got_entry());
1264 if (r_type_2 != 0)
1265 {
1266 got_offset = this->last_got_offset();
1267 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1268 }
1269
1270 this->set_got_size();
7bf1f802
ILT
1271}
1272
0a65a3a7
CC
1273// Add an entry for a local symbol to the GOT. This returns true if
1274// this is a new GOT entry, false if the symbol already has a GOT
1275// entry.
07f397ab
ILT
1276
1277template<int size, bool big_endian>
1278bool
0a65a3a7
CC
1279Output_data_got<size, big_endian>::add_local(
1280 Sized_relobj<size, big_endian>* object,
1281 unsigned int symndx,
1282 unsigned int got_type)
07f397ab 1283{
0a65a3a7 1284 if (object->local_has_got_offset(symndx, got_type))
07f397ab
ILT
1285 return false;
1286
0a65a3a7 1287 this->entries_.push_back(Got_entry(object, symndx));
07f397ab 1288 this->set_got_size();
0a65a3a7 1289 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
07f397ab
ILT
1290 return true;
1291}
1292
0a65a3a7
CC
1293// Add an entry for a local symbol to the GOT, and add a dynamic
1294// relocation of type R_TYPE for the GOT entry.
7bf1f802
ILT
1295template<int size, bool big_endian>
1296void
0a65a3a7
CC
1297Output_data_got<size, big_endian>::add_local_with_rel(
1298 Sized_relobj<size, big_endian>* object,
1299 unsigned int symndx,
1300 unsigned int got_type,
7bf1f802
ILT
1301 Rel_dyn* rel_dyn,
1302 unsigned int r_type)
1303{
0a65a3a7 1304 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1305 return;
1306
1307 this->entries_.push_back(Got_entry());
1308 this->set_got_size();
1309 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1310 object->set_local_got_offset(symndx, got_type, got_offset);
1311 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
7bf1f802
ILT
1312}
1313
1314template<int size, bool big_endian>
1315void
0a65a3a7
CC
1316Output_data_got<size, big_endian>::add_local_with_rela(
1317 Sized_relobj<size, big_endian>* object,
1318 unsigned int symndx,
1319 unsigned int got_type,
7bf1f802
ILT
1320 Rela_dyn* rela_dyn,
1321 unsigned int r_type)
1322{
0a65a3a7 1323 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1324 return;
1325
1326 this->entries_.push_back(Got_entry());
1327 this->set_got_size();
1328 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1329 object->set_local_got_offset(symndx, got_type, got_offset);
1330 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
07f397ab
ILT
1331}
1332
0a65a3a7
CC
1333// Add a pair of entries for a local symbol to the GOT, and add
1334// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1335// If R_TYPE_2 == 0, add the second entry with no relocation.
7bf1f802
ILT
1336template<int size, bool big_endian>
1337void
0a65a3a7 1338Output_data_got<size, big_endian>::add_local_pair_with_rel(
7bf1f802
ILT
1339 Sized_relobj<size, big_endian>* object,
1340 unsigned int symndx,
1341 unsigned int shndx,
0a65a3a7 1342 unsigned int got_type,
7bf1f802 1343 Rel_dyn* rel_dyn,
0a65a3a7
CC
1344 unsigned int r_type_1,
1345 unsigned int r_type_2)
7bf1f802 1346{
0a65a3a7 1347 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1348 return;
1349
1350 this->entries_.push_back(Got_entry());
1351 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1352 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1353 Output_section* os = object->output_section(shndx);
0a65a3a7 1354 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
7bf1f802 1355
0a65a3a7
CC
1356 this->entries_.push_back(Got_entry(object, symndx));
1357 if (r_type_2 != 0)
1358 {
1359 got_offset = this->last_got_offset();
1360 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1361 }
7bf1f802
ILT
1362
1363 this->set_got_size();
1364}
1365
1366template<int size, bool big_endian>
1367void
0a65a3a7 1368Output_data_got<size, big_endian>::add_local_pair_with_rela(
7bf1f802
ILT
1369 Sized_relobj<size, big_endian>* object,
1370 unsigned int symndx,
1371 unsigned int shndx,
0a65a3a7 1372 unsigned int got_type,
7bf1f802 1373 Rela_dyn* rela_dyn,
0a65a3a7
CC
1374 unsigned int r_type_1,
1375 unsigned int r_type_2)
7bf1f802 1376{
0a65a3a7 1377 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1378 return;
1379
1380 this->entries_.push_back(Got_entry());
1381 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1382 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1383 Output_section* os = object->output_section(shndx);
0a65a3a7 1384 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
7bf1f802 1385
0a65a3a7
CC
1386 this->entries_.push_back(Got_entry(object, symndx));
1387 if (r_type_2 != 0)
1388 {
1389 got_offset = this->last_got_offset();
1390 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1391 }
7bf1f802
ILT
1392
1393 this->set_got_size();
1394}
1395
ead1e424
ILT
1396// Write out the GOT.
1397
1398template<int size, bool big_endian>
1399void
dbe717ef 1400Output_data_got<size, big_endian>::do_write(Output_file* of)
ead1e424
ILT
1401{
1402 const int add = size / 8;
1403
1404 const off_t off = this->offset();
c06b7b0b 1405 const off_t oview_size = this->data_size();
ead1e424
ILT
1406 unsigned char* const oview = of->get_output_view(off, oview_size);
1407
1408 unsigned char* pov = oview;
1409 for (typename Got_entries::const_iterator p = this->entries_.begin();
1410 p != this->entries_.end();
1411 ++p)
1412 {
7e1edb90 1413 p->write(pov);
ead1e424
ILT
1414 pov += add;
1415 }
1416
a3ad94ed 1417 gold_assert(pov - oview == oview_size);
c06b7b0b 1418
ead1e424
ILT
1419 of->write_output_view(off, oview_size, oview);
1420
1421 // We no longer need the GOT entries.
1422 this->entries_.clear();
1423}
1424
a3ad94ed
ILT
1425// Output_data_dynamic::Dynamic_entry methods.
1426
1427// Write out the entry.
1428
1429template<int size, bool big_endian>
1430void
1431Output_data_dynamic::Dynamic_entry::write(
1432 unsigned char* pov,
7d1a9ebb 1433 const Stringpool* pool) const
a3ad94ed
ILT
1434{
1435 typename elfcpp::Elf_types<size>::Elf_WXword val;
c2b45e22 1436 switch (this->offset_)
a3ad94ed
ILT
1437 {
1438 case DYNAMIC_NUMBER:
1439 val = this->u_.val;
1440 break;
1441
a3ad94ed 1442 case DYNAMIC_SECTION_SIZE:
16649710 1443 val = this->u_.od->data_size();
a3ad94ed
ILT
1444 break;
1445
1446 case DYNAMIC_SYMBOL:
1447 {
16649710
ILT
1448 const Sized_symbol<size>* s =
1449 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
1450 val = s->value();
1451 }
1452 break;
1453
1454 case DYNAMIC_STRING:
1455 val = pool->get_offset(this->u_.str);
1456 break;
1457
1458 default:
c2b45e22
CC
1459 val = this->u_.od->address() + this->offset_;
1460 break;
a3ad94ed
ILT
1461 }
1462
1463 elfcpp::Dyn_write<size, big_endian> dw(pov);
1464 dw.put_d_tag(this->tag_);
1465 dw.put_d_val(val);
1466}
1467
1468// Output_data_dynamic methods.
1469
16649710
ILT
1470// Adjust the output section to set the entry size.
1471
1472void
1473Output_data_dynamic::do_adjust_output_section(Output_section* os)
1474{
8851ecca 1475 if (parameters->target().get_size() == 32)
16649710 1476 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
8851ecca 1477 else if (parameters->target().get_size() == 64)
16649710
ILT
1478 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1479 else
1480 gold_unreachable();
1481}
1482
a3ad94ed
ILT
1483// Set the final data size.
1484
1485void
27bc2bce 1486Output_data_dynamic::set_final_data_size()
a3ad94ed
ILT
1487{
1488 // Add the terminating entry.
1489 this->add_constant(elfcpp::DT_NULL, 0);
1490
1491 int dyn_size;
8851ecca 1492 if (parameters->target().get_size() == 32)
a3ad94ed 1493 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
8851ecca 1494 else if (parameters->target().get_size() == 64)
a3ad94ed
ILT
1495 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1496 else
1497 gold_unreachable();
1498 this->set_data_size(this->entries_.size() * dyn_size);
1499}
1500
1501// Write out the dynamic entries.
1502
1503void
1504Output_data_dynamic::do_write(Output_file* of)
1505{
8851ecca 1506 switch (parameters->size_and_endianness())
a3ad94ed 1507 {
9025d29d 1508#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
1509 case Parameters::TARGET_32_LITTLE:
1510 this->sized_write<32, false>(of);
1511 break;
9025d29d 1512#endif
8851ecca
ILT
1513#ifdef HAVE_TARGET_32_BIG
1514 case Parameters::TARGET_32_BIG:
1515 this->sized_write<32, true>(of);
1516 break;
9025d29d 1517#endif
9025d29d 1518#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
1519 case Parameters::TARGET_64_LITTLE:
1520 this->sized_write<64, false>(of);
1521 break;
9025d29d 1522#endif
8851ecca
ILT
1523#ifdef HAVE_TARGET_64_BIG
1524 case Parameters::TARGET_64_BIG:
1525 this->sized_write<64, true>(of);
1526 break;
1527#endif
1528 default:
1529 gold_unreachable();
a3ad94ed 1530 }
a3ad94ed
ILT
1531}
1532
1533template<int size, bool big_endian>
1534void
1535Output_data_dynamic::sized_write(Output_file* of)
1536{
1537 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1538
1539 const off_t offset = this->offset();
1540 const off_t oview_size = this->data_size();
1541 unsigned char* const oview = of->get_output_view(offset, oview_size);
1542
1543 unsigned char* pov = oview;
1544 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1545 p != this->entries_.end();
1546 ++p)
1547 {
7d1a9ebb 1548 p->write<size, big_endian>(pov, this->pool_);
a3ad94ed
ILT
1549 pov += dyn_size;
1550 }
1551
1552 gold_assert(pov - oview == oview_size);
1553
1554 of->write_output_view(offset, oview_size, oview);
1555
1556 // We no longer need the dynamic entries.
1557 this->entries_.clear();
1558}
1559
d491d34e
ILT
1560// Class Output_symtab_xindex.
1561
1562void
1563Output_symtab_xindex::do_write(Output_file* of)
1564{
1565 const off_t offset = this->offset();
1566 const off_t oview_size = this->data_size();
1567 unsigned char* const oview = of->get_output_view(offset, oview_size);
1568
1569 memset(oview, 0, oview_size);
1570
1571 if (parameters->target().is_big_endian())
1572 this->endian_do_write<true>(oview);
1573 else
1574 this->endian_do_write<false>(oview);
1575
1576 of->write_output_view(offset, oview_size, oview);
1577
1578 // We no longer need the data.
1579 this->entries_.clear();
1580}
1581
1582template<bool big_endian>
1583void
1584Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1585{
1586 for (Xindex_entries::const_iterator p = this->entries_.begin();
1587 p != this->entries_.end();
1588 ++p)
1589 elfcpp::Swap<32, big_endian>::writeval(oview + p->first * 4, p->second);
1590}
1591
ead1e424
ILT
1592// Output_section::Input_section methods.
1593
1594// Return the data size. For an input section we store the size here.
1595// For an Output_section_data, we have to ask it for the size.
1596
1597off_t
1598Output_section::Input_section::data_size() const
1599{
1600 if (this->is_input_section())
b8e6aad9 1601 return this->u1_.data_size;
ead1e424 1602 else
b8e6aad9 1603 return this->u2_.posd->data_size();
ead1e424
ILT
1604}
1605
1606// Set the address and file offset.
1607
1608void
96803768
ILT
1609Output_section::Input_section::set_address_and_file_offset(
1610 uint64_t address,
1611 off_t file_offset,
1612 off_t section_file_offset)
ead1e424
ILT
1613{
1614 if (this->is_input_section())
96803768
ILT
1615 this->u2_.object->set_section_offset(this->shndx_,
1616 file_offset - section_file_offset);
ead1e424 1617 else
96803768
ILT
1618 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1619}
1620
a445fddf
ILT
1621// Reset the address and file offset.
1622
1623void
1624Output_section::Input_section::reset_address_and_file_offset()
1625{
1626 if (!this->is_input_section())
1627 this->u2_.posd->reset_address_and_file_offset();
1628}
1629
96803768
ILT
1630// Finalize the data size.
1631
1632void
1633Output_section::Input_section::finalize_data_size()
1634{
1635 if (!this->is_input_section())
1636 this->u2_.posd->finalize_data_size();
b8e6aad9
ILT
1637}
1638
1e983657
ILT
1639// Try to turn an input offset into an output offset. We want to
1640// return the output offset relative to the start of this
1641// Input_section in the output section.
b8e6aad9 1642
8f00aeb8 1643inline bool
8383303e
ILT
1644Output_section::Input_section::output_offset(
1645 const Relobj* object,
1646 unsigned int shndx,
1647 section_offset_type offset,
1648 section_offset_type *poutput) const
b8e6aad9
ILT
1649{
1650 if (!this->is_input_section())
730cdc88 1651 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
b8e6aad9
ILT
1652 else
1653 {
730cdc88 1654 if (this->shndx_ != shndx || this->u2_.object != object)
b8e6aad9 1655 return false;
1e983657 1656 *poutput = offset;
b8e6aad9
ILT
1657 return true;
1658 }
ead1e424
ILT
1659}
1660
a9a60db6
ILT
1661// Return whether this is the merge section for the input section
1662// SHNDX in OBJECT.
1663
1664inline bool
1665Output_section::Input_section::is_merge_section_for(const Relobj* object,
1666 unsigned int shndx) const
1667{
1668 if (this->is_input_section())
1669 return false;
1670 return this->u2_.posd->is_merge_section_for(object, shndx);
1671}
1672
ead1e424
ILT
1673// Write out the data. We don't have to do anything for an input
1674// section--they are handled via Object::relocate--but this is where
1675// we write out the data for an Output_section_data.
1676
1677void
1678Output_section::Input_section::write(Output_file* of)
1679{
1680 if (!this->is_input_section())
b8e6aad9 1681 this->u2_.posd->write(of);
ead1e424
ILT
1682}
1683
96803768
ILT
1684// Write the data to a buffer. As for write(), we don't have to do
1685// anything for an input section.
1686
1687void
1688Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1689{
1690 if (!this->is_input_section())
1691 this->u2_.posd->write_to_buffer(buffer);
1692}
1693
7d9e3d98
ILT
1694// Print to a map file.
1695
1696void
1697Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1698{
1699 switch (this->shndx_)
1700 {
1701 case OUTPUT_SECTION_CODE:
1702 case MERGE_DATA_SECTION_CODE:
1703 case MERGE_STRING_SECTION_CODE:
1704 this->u2_.posd->print_to_mapfile(mapfile);
1705 break;
1706
1707 default:
1708 mapfile->print_input_section(this->u2_.object, this->shndx_);
1709 break;
1710 }
1711}
1712
a2fb1b05
ILT
1713// Output_section methods.
1714
1715// Construct an Output_section. NAME will point into a Stringpool.
1716
96803768 1717Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
b8e6aad9 1718 elfcpp::Elf_Xword flags)
96803768 1719 : name_(name),
a2fb1b05
ILT
1720 addralign_(0),
1721 entsize_(0),
a445fddf 1722 load_address_(0),
16649710 1723 link_section_(NULL),
a2fb1b05 1724 link_(0),
16649710 1725 info_section_(NULL),
6a74a719 1726 info_symndx_(NULL),
a2fb1b05
ILT
1727 info_(0),
1728 type_(type),
61ba1cf9 1729 flags_(flags),
91ea499d 1730 out_shndx_(-1U),
c06b7b0b
ILT
1731 symtab_index_(0),
1732 dynsym_index_(0),
ead1e424
ILT
1733 input_sections_(),
1734 first_input_offset_(0),
c51e6221 1735 fills_(),
96803768 1736 postprocessing_buffer_(NULL),
a3ad94ed 1737 needs_symtab_index_(false),
16649710
ILT
1738 needs_dynsym_index_(false),
1739 should_link_to_symtab_(false),
730cdc88 1740 should_link_to_dynsym_(false),
27bc2bce 1741 after_input_sections_(false),
7bf1f802 1742 requires_postprocessing_(false),
a445fddf
ILT
1743 found_in_sections_clause_(false),
1744 has_load_address_(false),
755ab8af 1745 info_uses_section_index_(false),
2fd32231
ILT
1746 may_sort_attached_input_sections_(false),
1747 must_sort_attached_input_sections_(false),
1748 attached_input_sections_are_sorted_(false),
9f1d377b
ILT
1749 is_relro_(false),
1750 is_relro_local_(false),
7bf1f802 1751 tls_offset_(0)
a2fb1b05 1752{
27bc2bce
ILT
1753 // An unallocated section has no address. Forcing this means that
1754 // we don't need special treatment for symbols defined in debug
1755 // sections.
1756 if ((flags & elfcpp::SHF_ALLOC) == 0)
1757 this->set_address(0);
a2fb1b05
ILT
1758}
1759
54dc6425
ILT
1760Output_section::~Output_section()
1761{
1762}
1763
16649710
ILT
1764// Set the entry size.
1765
1766void
1767Output_section::set_entsize(uint64_t v)
1768{
1769 if (this->entsize_ == 0)
1770 this->entsize_ = v;
1771 else
1772 gold_assert(this->entsize_ == v);
1773}
1774
ead1e424 1775// Add the input section SHNDX, with header SHDR, named SECNAME, in
730cdc88
ILT
1776// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1777// relocation section which applies to this section, or 0 if none, or
1778// -1U if more than one. Return the offset of the input section
1779// within the output section. Return -1 if the input section will
1780// receive special handling. In the normal case we don't always keep
1781// track of input sections for an Output_section. Instead, each
1782// Object keeps track of the Output_section for each of its input
a445fddf
ILT
1783// sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1784// track of input sections here; this is used when SECTIONS appears in
1785// a linker script.
a2fb1b05
ILT
1786
1787template<int size, bool big_endian>
1788off_t
730cdc88
ILT
1789Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1790 unsigned int shndx,
ead1e424 1791 const char* secname,
730cdc88 1792 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf
ILT
1793 unsigned int reloc_shndx,
1794 bool have_sections_script)
a2fb1b05
ILT
1795{
1796 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1797 if ((addralign & (addralign - 1)) != 0)
1798 {
75f2446e
ILT
1799 object->error(_("invalid alignment %lu for section \"%s\""),
1800 static_cast<unsigned long>(addralign), secname);
1801 addralign = 1;
a2fb1b05 1802 }
a2fb1b05
ILT
1803
1804 if (addralign > this->addralign_)
1805 this->addralign_ = addralign;
1806
44a43cf9 1807 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
154e0e9a 1808 this->update_flags_for_input_section(sh_flags);
a445fddf 1809
4f833eee 1810 uint64_t entsize = shdr.get_sh_entsize();
44a43cf9
ILT
1811
1812 // .debug_str is a mergeable string section, but is not always so
1813 // marked by compilers. Mark manually here so we can optimize.
1814 if (strcmp(secname, ".debug_str") == 0)
4f833eee
ILT
1815 {
1816 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1817 entsize = 1;
1818 }
44a43cf9 1819
b8e6aad9 1820 // If this is a SHF_MERGE section, we pass all the input sections to
730cdc88 1821 // a Output_data_merge. We don't try to handle relocations for such
e0b64032
ILT
1822 // a section. We don't try to handle empty merge sections--they
1823 // mess up the mappings, and are useless anyhow.
44a43cf9 1824 if ((sh_flags & elfcpp::SHF_MERGE) != 0
e0b64032
ILT
1825 && reloc_shndx == 0
1826 && shdr.get_sh_size() > 0)
b8e6aad9 1827 {
44a43cf9 1828 if (this->add_merge_input_section(object, shndx, sh_flags,
96803768 1829 entsize, addralign))
b8e6aad9
ILT
1830 {
1831 // Tell the relocation routines that they need to call the
730cdc88 1832 // output_offset method to determine the final address.
b8e6aad9
ILT
1833 return -1;
1834 }
1835 }
1836
27bc2bce 1837 off_t offset_in_section = this->current_data_size_for_child();
c51e6221
ILT
1838 off_t aligned_offset_in_section = align_address(offset_in_section,
1839 addralign);
1840
1841 if (aligned_offset_in_section > offset_in_section
a445fddf 1842 && !have_sections_script
44a43cf9 1843 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
c51e6221
ILT
1844 && object->target()->has_code_fill())
1845 {
1846 // We need to add some fill data. Using fill_list_ when
1847 // possible is an optimization, since we will often have fill
1848 // sections without input sections.
1849 off_t fill_len = aligned_offset_in_section - offset_in_section;
1850 if (this->input_sections_.empty())
1851 this->fills_.push_back(Fill(offset_in_section, fill_len));
1852 else
1853 {
1854 // FIXME: When relaxing, the size needs to adjust to
1855 // maintain a constant alignment.
1856 std::string fill_data(object->target()->code_fill(fill_len));
1857 Output_data_const* odc = new Output_data_const(fill_data, 1);
1858 this->input_sections_.push_back(Input_section(odc));
1859 }
1860 }
1861
27bc2bce
ILT
1862 this->set_current_data_size_for_child(aligned_offset_in_section
1863 + shdr.get_sh_size());
a2fb1b05 1864
ead1e424 1865 // We need to keep track of this section if we are already keeping
2fd32231
ILT
1866 // track of sections, or if we are relaxing. Also, if this is a
1867 // section which requires sorting, or which may require sorting in
1868 // the future, we keep track of the sections. FIXME: Add test for
ead1e424 1869 // relaxing.
2fd32231
ILT
1870 if (have_sections_script
1871 || !this->input_sections_.empty()
1872 || this->may_sort_attached_input_sections()
7d9e3d98
ILT
1873 || this->must_sort_attached_input_sections()
1874 || parameters->options().user_set_Map())
ead1e424
ILT
1875 this->input_sections_.push_back(Input_section(object, shndx,
1876 shdr.get_sh_size(),
1877 addralign));
54dc6425 1878
c51e6221 1879 return aligned_offset_in_section;
61ba1cf9
ILT
1880}
1881
ead1e424
ILT
1882// Add arbitrary data to an output section.
1883
1884void
1885Output_section::add_output_section_data(Output_section_data* posd)
1886{
b8e6aad9
ILT
1887 Input_section inp(posd);
1888 this->add_output_section_data(&inp);
a445fddf
ILT
1889
1890 if (posd->is_data_size_valid())
1891 {
1892 off_t offset_in_section = this->current_data_size_for_child();
1893 off_t aligned_offset_in_section = align_address(offset_in_section,
1894 posd->addralign());
1895 this->set_current_data_size_for_child(aligned_offset_in_section
1896 + posd->data_size());
1897 }
b8e6aad9
ILT
1898}
1899
1900// Add arbitrary data to an output section by Input_section.
c06b7b0b 1901
b8e6aad9
ILT
1902void
1903Output_section::add_output_section_data(Input_section* inp)
1904{
ead1e424 1905 if (this->input_sections_.empty())
27bc2bce 1906 this->first_input_offset_ = this->current_data_size_for_child();
c06b7b0b 1907
b8e6aad9 1908 this->input_sections_.push_back(*inp);
c06b7b0b 1909
b8e6aad9 1910 uint64_t addralign = inp->addralign();
ead1e424
ILT
1911 if (addralign > this->addralign_)
1912 this->addralign_ = addralign;
c06b7b0b 1913
b8e6aad9
ILT
1914 inp->set_output_section(this);
1915}
1916
1917// Add a merge section to an output section.
1918
1919void
1920Output_section::add_output_merge_section(Output_section_data* posd,
1921 bool is_string, uint64_t entsize)
1922{
1923 Input_section inp(posd, is_string, entsize);
1924 this->add_output_section_data(&inp);
1925}
1926
1927// Add an input section to a SHF_MERGE section.
1928
1929bool
1930Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1931 uint64_t flags, uint64_t entsize,
96803768 1932 uint64_t addralign)
b8e6aad9 1933{
87f95776
ILT
1934 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1935
1936 // We only merge strings if the alignment is not more than the
1937 // character size. This could be handled, but it's unusual.
1938 if (is_string && addralign > entsize)
b8e6aad9
ILT
1939 return false;
1940
b8e6aad9
ILT
1941 Input_section_list::iterator p;
1942 for (p = this->input_sections_.begin();
1943 p != this->input_sections_.end();
1944 ++p)
87f95776 1945 if (p->is_merge_section(is_string, entsize, addralign))
9a0910c3
ILT
1946 {
1947 p->add_input_section(object, shndx);
1948 return true;
1949 }
b8e6aad9
ILT
1950
1951 // We handle the actual constant merging in Output_merge_data or
1952 // Output_merge_string_data.
9a0910c3
ILT
1953 Output_section_data* posd;
1954 if (!is_string)
1955 posd = new Output_merge_data(entsize, addralign);
b8e6aad9
ILT
1956 else
1957 {
9a0910c3
ILT
1958 switch (entsize)
1959 {
1960 case 1:
1961 posd = new Output_merge_string<char>(addralign);
1962 break;
1963 case 2:
1964 posd = new Output_merge_string<uint16_t>(addralign);
1965 break;
1966 case 4:
1967 posd = new Output_merge_string<uint32_t>(addralign);
1968 break;
1969 default:
1970 return false;
1971 }
b8e6aad9
ILT
1972 }
1973
9a0910c3
ILT
1974 this->add_output_merge_section(posd, is_string, entsize);
1975 posd->add_input_section(object, shndx);
1976
b8e6aad9
ILT
1977 return true;
1978}
1979
730cdc88
ILT
1980// Given an address OFFSET relative to the start of input section
1981// SHNDX in OBJECT, return whether this address is being included in
1982// the final link. This should only be called if SHNDX in OBJECT has
1983// a special mapping.
1984
1985bool
1986Output_section::is_input_address_mapped(const Relobj* object,
1987 unsigned int shndx,
1988 off_t offset) const
1989{
730cdc88
ILT
1990 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1991 p != this->input_sections_.end();
1992 ++p)
1993 {
8383303e 1994 section_offset_type output_offset;
730cdc88
ILT
1995 if (p->output_offset(object, shndx, offset, &output_offset))
1996 return output_offset != -1;
1997 }
1998
1999 // By default we assume that the address is mapped. This should
2000 // only be called after we have passed all sections to Layout. At
2001 // that point we should know what we are discarding.
2002 return true;
2003}
2004
2005// Given an address OFFSET relative to the start of input section
2006// SHNDX in object OBJECT, return the output offset relative to the
1e983657
ILT
2007// start of the input section in the output section. This should only
2008// be called if SHNDX in OBJECT has a special mapping.
730cdc88 2009
8383303e 2010section_offset_type
730cdc88 2011Output_section::output_offset(const Relobj* object, unsigned int shndx,
8383303e 2012 section_offset_type offset) const
730cdc88 2013{
730cdc88
ILT
2014 // This can only be called meaningfully when layout is complete.
2015 gold_assert(Output_data::is_layout_complete());
2016
2017 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2018 p != this->input_sections_.end();
2019 ++p)
2020 {
8383303e 2021 section_offset_type output_offset;
730cdc88
ILT
2022 if (p->output_offset(object, shndx, offset, &output_offset))
2023 return output_offset;
2024 }
2025 gold_unreachable();
2026}
2027
b8e6aad9
ILT
2028// Return the output virtual address of OFFSET relative to the start
2029// of input section SHNDX in object OBJECT.
2030
2031uint64_t
2032Output_section::output_address(const Relobj* object, unsigned int shndx,
2033 off_t offset) const
2034{
2035 uint64_t addr = this->address() + this->first_input_offset_;
2036 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2037 p != this->input_sections_.end();
2038 ++p)
2039 {
2040 addr = align_address(addr, p->addralign());
8383303e 2041 section_offset_type output_offset;
730cdc88
ILT
2042 if (p->output_offset(object, shndx, offset, &output_offset))
2043 {
2044 if (output_offset == -1)
2045 return -1U;
2046 return addr + output_offset;
2047 }
b8e6aad9
ILT
2048 addr += p->data_size();
2049 }
2050
2051 // If we get here, it means that we don't know the mapping for this
2052 // input section. This might happen in principle if
2053 // add_input_section were called before add_output_section_data.
2054 // But it should never actually happen.
2055
2056 gold_unreachable();
ead1e424
ILT
2057}
2058
a9a60db6
ILT
2059// Return the output address of the start of the merged section for
2060// input section SHNDX in object OBJECT.
2061
2062uint64_t
2063Output_section::starting_output_address(const Relobj* object,
2064 unsigned int shndx) const
2065{
a9a60db6
ILT
2066 uint64_t addr = this->address() + this->first_input_offset_;
2067 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2068 p != this->input_sections_.end();
2069 ++p)
2070 {
2071 addr = align_address(addr, p->addralign());
2072
2073 // It would be nice if we could use the existing output_offset
2074 // method to get the output offset of input offset 0.
2075 // Unfortunately we don't know for sure that input offset 0 is
2076 // mapped at all.
2077 if (p->is_merge_section_for(object, shndx))
2078 return addr;
2079
2080 addr += p->data_size();
2081 }
2082 gold_unreachable();
2083}
2084
27bc2bce 2085// Set the data size of an Output_section. This is where we handle
ead1e424
ILT
2086// setting the addresses of any Output_section_data objects.
2087
2088void
27bc2bce 2089Output_section::set_final_data_size()
ead1e424
ILT
2090{
2091 if (this->input_sections_.empty())
27bc2bce
ILT
2092 {
2093 this->set_data_size(this->current_data_size_for_child());
2094 return;
2095 }
ead1e424 2096
2fd32231
ILT
2097 if (this->must_sort_attached_input_sections())
2098 this->sort_attached_input_sections();
2099
27bc2bce
ILT
2100 uint64_t address = this->address();
2101 off_t startoff = this->offset();
ead1e424
ILT
2102 off_t off = startoff + this->first_input_offset_;
2103 for (Input_section_list::iterator p = this->input_sections_.begin();
2104 p != this->input_sections_.end();
2105 ++p)
2106 {
2107 off = align_address(off, p->addralign());
96803768
ILT
2108 p->set_address_and_file_offset(address + (off - startoff), off,
2109 startoff);
ead1e424
ILT
2110 off += p->data_size();
2111 }
2112
2113 this->set_data_size(off - startoff);
2114}
9a0910c3 2115
a445fddf
ILT
2116// Reset the address and file offset.
2117
2118void
2119Output_section::do_reset_address_and_file_offset()
2120{
2121 for (Input_section_list::iterator p = this->input_sections_.begin();
2122 p != this->input_sections_.end();
2123 ++p)
2124 p->reset_address_and_file_offset();
2125}
2126
7bf1f802
ILT
2127// Set the TLS offset. Called only for SHT_TLS sections.
2128
2129void
2130Output_section::do_set_tls_offset(uint64_t tls_base)
2131{
2132 this->tls_offset_ = this->address() - tls_base;
2133}
2134
2fd32231
ILT
2135// In a few cases we need to sort the input sections attached to an
2136// output section. This is used to implement the type of constructor
2137// priority ordering implemented by the GNU linker, in which the
2138// priority becomes part of the section name and the sections are
2139// sorted by name. We only do this for an output section if we see an
2140// attached input section matching ".ctor.*", ".dtor.*",
2141// ".init_array.*" or ".fini_array.*".
2142
2143class Output_section::Input_section_sort_entry
2144{
2145 public:
2146 Input_section_sort_entry()
2147 : input_section_(), index_(-1U), section_has_name_(false),
2148 section_name_()
2149 { }
2150
2151 Input_section_sort_entry(const Input_section& input_section,
2152 unsigned int index)
2153 : input_section_(input_section), index_(index),
2154 section_has_name_(input_section.is_input_section())
2155 {
2156 if (this->section_has_name_)
2157 {
2158 // This is only called single-threaded from Layout::finalize,
2159 // so it is OK to lock. Unfortunately we have no way to pass
2160 // in a Task token.
2161 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
2162 Object* obj = input_section.relobj();
2163 Task_lock_obj<Object> tl(dummy_task, obj);
2164
2165 // This is a slow operation, which should be cached in
2166 // Layout::layout if this becomes a speed problem.
2167 this->section_name_ = obj->section_name(input_section.shndx());
2168 }
2169 }
2170
2171 // Return the Input_section.
2172 const Input_section&
2173 input_section() const
2174 {
2175 gold_assert(this->index_ != -1U);
2176 return this->input_section_;
2177 }
2178
2179 // The index of this entry in the original list. This is used to
2180 // make the sort stable.
2181 unsigned int
2182 index() const
2183 {
2184 gold_assert(this->index_ != -1U);
2185 return this->index_;
2186 }
2187
2188 // Whether there is a section name.
2189 bool
2190 section_has_name() const
2191 { return this->section_has_name_; }
2192
2193 // The section name.
2194 const std::string&
2195 section_name() const
2196 {
2197 gold_assert(this->section_has_name_);
2198 return this->section_name_;
2199 }
2200
ab794b6b
ILT
2201 // Return true if the section name has a priority. This is assumed
2202 // to be true if it has a dot after the initial dot.
2fd32231 2203 bool
ab794b6b 2204 has_priority() const
2fd32231
ILT
2205 {
2206 gold_assert(this->section_has_name_);
ab794b6b 2207 return this->section_name_.find('.', 1);
2fd32231
ILT
2208 }
2209
ab794b6b
ILT
2210 // Return true if this an input file whose base name matches
2211 // FILE_NAME. The base name must have an extension of ".o", and
2212 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2213 // This is to match crtbegin.o as well as crtbeginS.o without
2214 // getting confused by other possibilities. Overall matching the
2215 // file name this way is a dreadful hack, but the GNU linker does it
2216 // in order to better support gcc, and we need to be compatible.
2fd32231 2217 bool
ab794b6b 2218 match_file_name(const char* match_file_name) const
2fd32231 2219 {
2fd32231
ILT
2220 const std::string& file_name(this->input_section_.relobj()->name());
2221 const char* base_name = lbasename(file_name.c_str());
2222 size_t match_len = strlen(match_file_name);
2223 if (strncmp(base_name, match_file_name, match_len) != 0)
2224 return false;
2225 size_t base_len = strlen(base_name);
2226 if (base_len != match_len + 2 && base_len != match_len + 3)
2227 return false;
2228 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2229 }
2230
2231 private:
2232 // The Input_section we are sorting.
2233 Input_section input_section_;
2234 // The index of this Input_section in the original list.
2235 unsigned int index_;
2236 // Whether this Input_section has a section name--it won't if this
2237 // is some random Output_section_data.
2238 bool section_has_name_;
2239 // The section name if there is one.
2240 std::string section_name_;
2241};
2242
2243// Return true if S1 should come before S2 in the output section.
2244
2245bool
2246Output_section::Input_section_sort_compare::operator()(
2247 const Output_section::Input_section_sort_entry& s1,
2248 const Output_section::Input_section_sort_entry& s2) const
2249{
ab794b6b
ILT
2250 // crtbegin.o must come first.
2251 bool s1_begin = s1.match_file_name("crtbegin");
2252 bool s2_begin = s2.match_file_name("crtbegin");
2fd32231
ILT
2253 if (s1_begin || s2_begin)
2254 {
2255 if (!s1_begin)
2256 return false;
2257 if (!s2_begin)
2258 return true;
2259 return s1.index() < s2.index();
2260 }
2261
ab794b6b
ILT
2262 // crtend.o must come last.
2263 bool s1_end = s1.match_file_name("crtend");
2264 bool s2_end = s2.match_file_name("crtend");
2fd32231
ILT
2265 if (s1_end || s2_end)
2266 {
2267 if (!s1_end)
2268 return true;
2269 if (!s2_end)
2270 return false;
2271 return s1.index() < s2.index();
2272 }
2273
ab794b6b
ILT
2274 // We sort all the sections with no names to the end.
2275 if (!s1.section_has_name() || !s2.section_has_name())
2276 {
2277 if (s1.section_has_name())
2278 return true;
2279 if (s2.section_has_name())
2280 return false;
2281 return s1.index() < s2.index();
2282 }
2fd32231 2283
ab794b6b
ILT
2284 // A section with a priority follows a section without a priority.
2285 // The GNU linker does this for all but .init_array sections; until
2286 // further notice we'll assume that that is an mistake.
2287 bool s1_has_priority = s1.has_priority();
2288 bool s2_has_priority = s2.has_priority();
2289 if (s1_has_priority && !s2_has_priority)
2fd32231 2290 return false;
ab794b6b 2291 if (!s1_has_priority && s2_has_priority)
2fd32231
ILT
2292 return true;
2293
2294 // Otherwise we sort by name.
2295 int compare = s1.section_name().compare(s2.section_name());
2296 if (compare != 0)
2297 return compare < 0;
2298
2299 // Otherwise we keep the input order.
2300 return s1.index() < s2.index();
2301}
2302
2303// Sort the input sections attached to an output section.
2304
2305void
2306Output_section::sort_attached_input_sections()
2307{
2308 if (this->attached_input_sections_are_sorted_)
2309 return;
2310
2311 // The only thing we know about an input section is the object and
2312 // the section index. We need the section name. Recomputing this
2313 // is slow but this is an unusual case. If this becomes a speed
2314 // problem we can cache the names as required in Layout::layout.
2315
2316 // We start by building a larger vector holding a copy of each
2317 // Input_section, plus its current index in the list and its name.
2318 std::vector<Input_section_sort_entry> sort_list;
2319
2320 unsigned int i = 0;
2321 for (Input_section_list::iterator p = this->input_sections_.begin();
2322 p != this->input_sections_.end();
2323 ++p, ++i)
2324 sort_list.push_back(Input_section_sort_entry(*p, i));
2325
2326 // Sort the input sections.
2327 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2328
2329 // Copy the sorted input sections back to our list.
2330 this->input_sections_.clear();
2331 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2332 p != sort_list.end();
2333 ++p)
2334 this->input_sections_.push_back(p->input_section());
2335
2336 // Remember that we sorted the input sections, since we might get
2337 // called again.
2338 this->attached_input_sections_are_sorted_ = true;
2339}
2340
61ba1cf9
ILT
2341// Write the section header to *OSHDR.
2342
2343template<int size, bool big_endian>
2344void
16649710
ILT
2345Output_section::write_header(const Layout* layout,
2346 const Stringpool* secnamepool,
61ba1cf9
ILT
2347 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2348{
2349 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2350 oshdr->put_sh_type(this->type_);
6a74a719
ILT
2351
2352 elfcpp::Elf_Xword flags = this->flags_;
755ab8af 2353 if (this->info_section_ != NULL && this->info_uses_section_index_)
6a74a719
ILT
2354 flags |= elfcpp::SHF_INFO_LINK;
2355 oshdr->put_sh_flags(flags);
2356
61ba1cf9
ILT
2357 oshdr->put_sh_addr(this->address());
2358 oshdr->put_sh_offset(this->offset());
2359 oshdr->put_sh_size(this->data_size());
16649710
ILT
2360 if (this->link_section_ != NULL)
2361 oshdr->put_sh_link(this->link_section_->out_shndx());
2362 else if (this->should_link_to_symtab_)
2363 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2364 else if (this->should_link_to_dynsym_)
2365 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2366 else
2367 oshdr->put_sh_link(this->link_);
755ab8af
ILT
2368
2369 elfcpp::Elf_Word info;
16649710 2370 if (this->info_section_ != NULL)
755ab8af
ILT
2371 {
2372 if (this->info_uses_section_index_)
2373 info = this->info_section_->out_shndx();
2374 else
2375 info = this->info_section_->symtab_index();
2376 }
6a74a719 2377 else if (this->info_symndx_ != NULL)
755ab8af 2378 info = this->info_symndx_->symtab_index();
16649710 2379 else
755ab8af
ILT
2380 info = this->info_;
2381 oshdr->put_sh_info(info);
2382
61ba1cf9
ILT
2383 oshdr->put_sh_addralign(this->addralign_);
2384 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
2385}
2386
ead1e424
ILT
2387// Write out the data. For input sections the data is written out by
2388// Object::relocate, but we have to handle Output_section_data objects
2389// here.
2390
2391void
2392Output_section::do_write(Output_file* of)
2393{
96803768
ILT
2394 gold_assert(!this->requires_postprocessing());
2395
c51e6221
ILT
2396 off_t output_section_file_offset = this->offset();
2397 for (Fill_list::iterator p = this->fills_.begin();
2398 p != this->fills_.end();
2399 ++p)
2400 {
8851ecca 2401 std::string fill_data(parameters->target().code_fill(p->length()));
c51e6221 2402 of->write(output_section_file_offset + p->section_offset(),
a445fddf 2403 fill_data.data(), fill_data.size());
c51e6221
ILT
2404 }
2405
ead1e424
ILT
2406 for (Input_section_list::iterator p = this->input_sections_.begin();
2407 p != this->input_sections_.end();
2408 ++p)
2409 p->write(of);
2410}
2411
96803768
ILT
2412// If a section requires postprocessing, create the buffer to use.
2413
2414void
2415Output_section::create_postprocessing_buffer()
2416{
2417 gold_assert(this->requires_postprocessing());
1bedcac5
ILT
2418
2419 if (this->postprocessing_buffer_ != NULL)
2420 return;
96803768
ILT
2421
2422 if (!this->input_sections_.empty())
2423 {
2424 off_t off = this->first_input_offset_;
2425 for (Input_section_list::iterator p = this->input_sections_.begin();
2426 p != this->input_sections_.end();
2427 ++p)
2428 {
2429 off = align_address(off, p->addralign());
2430 p->finalize_data_size();
2431 off += p->data_size();
2432 }
2433 this->set_current_data_size_for_child(off);
2434 }
2435
2436 off_t buffer_size = this->current_data_size_for_child();
2437 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2438}
2439
2440// Write all the data of an Output_section into the postprocessing
2441// buffer. This is used for sections which require postprocessing,
2442// such as compression. Input sections are handled by
2443// Object::Relocate.
2444
2445void
2446Output_section::write_to_postprocessing_buffer()
2447{
2448 gold_assert(this->requires_postprocessing());
2449
96803768
ILT
2450 unsigned char* buffer = this->postprocessing_buffer();
2451 for (Fill_list::iterator p = this->fills_.begin();
2452 p != this->fills_.end();
2453 ++p)
2454 {
8851ecca 2455 std::string fill_data(parameters->target().code_fill(p->length()));
a445fddf
ILT
2456 memcpy(buffer + p->section_offset(), fill_data.data(),
2457 fill_data.size());
96803768
ILT
2458 }
2459
2460 off_t off = this->first_input_offset_;
2461 for (Input_section_list::iterator p = this->input_sections_.begin();
2462 p != this->input_sections_.end();
2463 ++p)
2464 {
2465 off = align_address(off, p->addralign());
2466 p->write_to_buffer(buffer + off);
2467 off += p->data_size();
2468 }
2469}
2470
a445fddf
ILT
2471// Get the input sections for linker script processing. We leave
2472// behind the Output_section_data entries. Note that this may be
2473// slightly incorrect for merge sections. We will leave them behind,
2474// but it is possible that the script says that they should follow
2475// some other input sections, as in:
2476// .rodata { *(.rodata) *(.rodata.cst*) }
2477// For that matter, we don't handle this correctly:
2478// .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2479// With luck this will never matter.
2480
2481uint64_t
2482Output_section::get_input_sections(
2483 uint64_t address,
2484 const std::string& fill,
2485 std::list<std::pair<Relobj*, unsigned int> >* input_sections)
2486{
2487 uint64_t orig_address = address;
2488
2489 address = align_address(address, this->addralign());
2490
2491 Input_section_list remaining;
2492 for (Input_section_list::iterator p = this->input_sections_.begin();
2493 p != this->input_sections_.end();
2494 ++p)
2495 {
2496 if (p->is_input_section())
2497 input_sections->push_back(std::make_pair(p->relobj(), p->shndx()));
2498 else
2499 {
2500 uint64_t aligned_address = align_address(address, p->addralign());
2501 if (aligned_address != address && !fill.empty())
2502 {
2503 section_size_type length =
2504 convert_to_section_size_type(aligned_address - address);
2505 std::string this_fill;
2506 this_fill.reserve(length);
2507 while (this_fill.length() + fill.length() <= length)
2508 this_fill += fill;
2509 if (this_fill.length() < length)
2510 this_fill.append(fill, 0, length - this_fill.length());
2511
2512 Output_section_data* posd = new Output_data_const(this_fill, 0);
2513 remaining.push_back(Input_section(posd));
2514 }
2515 address = aligned_address;
2516
2517 remaining.push_back(*p);
2518
2519 p->finalize_data_size();
2520 address += p->data_size();
2521 }
2522 }
2523
2524 this->input_sections_.swap(remaining);
2525 this->first_input_offset_ = 0;
2526
2527 uint64_t data_size = address - orig_address;
2528 this->set_current_data_size_for_child(data_size);
2529 return data_size;
2530}
2531
2532// Add an input section from a script.
2533
2534void
2535Output_section::add_input_section_for_script(Relobj* object,
2536 unsigned int shndx,
2537 off_t data_size,
2538 uint64_t addralign)
2539{
2540 if (addralign > this->addralign_)
2541 this->addralign_ = addralign;
2542
2543 off_t offset_in_section = this->current_data_size_for_child();
2544 off_t aligned_offset_in_section = align_address(offset_in_section,
2545 addralign);
2546
2547 this->set_current_data_size_for_child(aligned_offset_in_section
2548 + data_size);
2549
2550 this->input_sections_.push_back(Input_section(object, shndx,
2551 data_size, addralign));
2552}
2553
7d9e3d98
ILT
2554// Print to the map file.
2555
2556void
2557Output_section::do_print_to_mapfile(Mapfile* mapfile) const
2558{
2559 mapfile->print_output_section(this);
2560
2561 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2562 p != this->input_sections_.end();
2563 ++p)
2564 p->print_to_mapfile(mapfile);
2565}
2566
38c5e8b4
ILT
2567// Print stats for merge sections to stderr.
2568
2569void
2570Output_section::print_merge_stats()
2571{
2572 Input_section_list::iterator p;
2573 for (p = this->input_sections_.begin();
2574 p != this->input_sections_.end();
2575 ++p)
2576 p->print_merge_stats(this->name_);
2577}
2578
a2fb1b05
ILT
2579// Output segment methods.
2580
2581Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
54dc6425 2582 : output_data_(),
75f65a3e 2583 output_bss_(),
a2fb1b05
ILT
2584 vaddr_(0),
2585 paddr_(0),
2586 memsz_(0),
a445fddf
ILT
2587 max_align_(0),
2588 min_p_align_(0),
a2fb1b05
ILT
2589 offset_(0),
2590 filesz_(0),
2591 type_(type),
ead1e424 2592 flags_(flags),
a445fddf
ILT
2593 is_max_align_known_(false),
2594 are_addresses_set_(false)
a2fb1b05
ILT
2595{
2596}
2597
2598// Add an Output_section to an Output_segment.
2599
2600void
75f65a3e 2601Output_segment::add_output_section(Output_section* os,
01676dcd 2602 elfcpp::Elf_Word seg_flags)
a2fb1b05 2603{
a3ad94ed 2604 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
a445fddf 2605 gold_assert(!this->is_max_align_known_);
75f65a3e 2606
ead1e424 2607 // Update the segment flags.
75f65a3e 2608 this->flags_ |= seg_flags;
75f65a3e
ILT
2609
2610 Output_segment::Output_data_list* pdl;
2611 if (os->type() == elfcpp::SHT_NOBITS)
2612 pdl = &this->output_bss_;
2613 else
2614 pdl = &this->output_data_;
54dc6425 2615
a2fb1b05
ILT
2616 // So that PT_NOTE segments will work correctly, we need to ensure
2617 // that all SHT_NOTE sections are adjacent. This will normally
2618 // happen automatically, because all the SHT_NOTE input sections
2619 // will wind up in the same output section. However, it is possible
2620 // for multiple SHT_NOTE input sections to have different section
2621 // flags, and thus be in different output sections, but for the
2622 // different section flags to map into the same segment flags and
2623 // thus the same output segment.
54dc6425
ILT
2624
2625 // Note that while there may be many input sections in an output
2626 // section, there are normally only a few output sections in an
2627 // output segment. This loop is expected to be fast.
2628
61ba1cf9 2629 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
a2fb1b05 2630 {
a3ad94ed 2631 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 2632 do
54dc6425 2633 {
75f65a3e 2634 --p;
54dc6425
ILT
2635 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
2636 {
2637 ++p;
75f65a3e 2638 pdl->insert(p, os);
54dc6425
ILT
2639 return;
2640 }
2641 }
75f65a3e 2642 while (p != pdl->begin());
54dc6425
ILT
2643 }
2644
2645 // Similarly, so that PT_TLS segments will work, we need to group
75f65a3e
ILT
2646 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
2647 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
2648 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
07f397ab
ILT
2649 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
2650 // and the PT_TLS segment -- we do this grouping only for the
2651 // PT_LOAD segment.
2652 if (this->type_ != elfcpp::PT_TLS
2653 && (os->flags() & elfcpp::SHF_TLS) != 0
2654 && !this->output_data_.empty())
54dc6425 2655 {
75f65a3e
ILT
2656 pdl = &this->output_data_;
2657 bool nobits = os->type() == elfcpp::SHT_NOBITS;
ead1e424 2658 bool sawtls = false;
a3ad94ed 2659 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 2660 do
a2fb1b05 2661 {
75f65a3e 2662 --p;
ead1e424
ILT
2663 bool insert;
2664 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2665 {
2666 sawtls = true;
2667 // Put a NOBITS section after the first TLS section.
9f1d377b 2668 // Put a PROGBITS section after the first TLS/PROGBITS
ead1e424
ILT
2669 // section.
2670 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
2671 }
2672 else
2673 {
2674 // If we've gone past the TLS sections, but we've seen a
2675 // TLS section, then we need to insert this section now.
2676 insert = sawtls;
2677 }
2678
2679 if (insert)
a2fb1b05
ILT
2680 {
2681 ++p;
75f65a3e 2682 pdl->insert(p, os);
a2fb1b05
ILT
2683 return;
2684 }
2685 }
75f65a3e 2686 while (p != pdl->begin());
ead1e424 2687
dbe717ef
ILT
2688 // There are no TLS sections yet; put this one at the requested
2689 // location in the section list.
a2fb1b05
ILT
2690 }
2691
9f1d377b
ILT
2692 // For the PT_GNU_RELRO segment, we need to group relro sections,
2693 // and we need to put them before any non-relro sections. Also,
2694 // relro local sections go before relro non-local sections.
2695 if (parameters->options().relro() && os->is_relro())
2696 {
2697 gold_assert(pdl == &this->output_data_);
2698 Output_segment::Output_data_list::iterator p;
2699 for (p = pdl->begin(); p != pdl->end(); ++p)
2700 {
2701 if (!(*p)->is_section())
2702 break;
2703
2704 Output_section* pos = (*p)->output_section();
2705 if (!pos->is_relro()
2706 || (os->is_relro_local() && !pos->is_relro_local()))
2707 break;
2708 }
2709
2710 pdl->insert(p, os);
2711 return;
2712 }
2713
01676dcd 2714 pdl->push_back(os);
75f65a3e
ILT
2715}
2716
1650c4ff
ILT
2717// Remove an Output_section from this segment. It is an error if it
2718// is not present.
2719
2720void
2721Output_segment::remove_output_section(Output_section* os)
2722{
2723 // We only need this for SHT_PROGBITS.
2724 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
2725 for (Output_data_list::iterator p = this->output_data_.begin();
2726 p != this->output_data_.end();
2727 ++p)
2728 {
2729 if (*p == os)
2730 {
2731 this->output_data_.erase(p);
2732 return;
2733 }
2734 }
2735 gold_unreachable();
2736}
2737
75f65a3e
ILT
2738// Add an Output_data (which is not an Output_section) to the start of
2739// a segment.
2740
2741void
2742Output_segment::add_initial_output_data(Output_data* od)
2743{
a445fddf 2744 gold_assert(!this->is_max_align_known_);
75f65a3e
ILT
2745 this->output_data_.push_front(od);
2746}
2747
9f1d377b
ILT
2748// Return whether the first data section is a relro section.
2749
2750bool
2751Output_segment::is_first_section_relro() const
2752{
2753 return (!this->output_data_.empty()
2754 && this->output_data_.front()->is_section()
2755 && this->output_data_.front()->output_section()->is_relro());
2756}
2757
75f65a3e 2758// Return the maximum alignment of the Output_data in Output_segment.
75f65a3e
ILT
2759
2760uint64_t
a445fddf 2761Output_segment::maximum_alignment()
75f65a3e 2762{
a445fddf 2763 if (!this->is_max_align_known_)
ead1e424
ILT
2764 {
2765 uint64_t addralign;
2766
a445fddf
ILT
2767 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
2768 if (addralign > this->max_align_)
2769 this->max_align_ = addralign;
ead1e424 2770
a445fddf
ILT
2771 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
2772 if (addralign > this->max_align_)
2773 this->max_align_ = addralign;
ead1e424 2774
9f1d377b
ILT
2775 // If -z relro is in effect, and the first section in this
2776 // segment is a relro section, then the segment must be aligned
2777 // to at least the common page size. This ensures that the
2778 // PT_GNU_RELRO segment will start at a page boundary.
2779 if (parameters->options().relro() && this->is_first_section_relro())
2780 {
2781 addralign = parameters->target().common_pagesize();
2782 if (addralign > this->max_align_)
2783 this->max_align_ = addralign;
2784 }
2785
a445fddf 2786 this->is_max_align_known_ = true;
ead1e424
ILT
2787 }
2788
a445fddf 2789 return this->max_align_;
75f65a3e
ILT
2790}
2791
ead1e424
ILT
2792// Return the maximum alignment of a list of Output_data.
2793
2794uint64_t
a445fddf 2795Output_segment::maximum_alignment_list(const Output_data_list* pdl)
ead1e424
ILT
2796{
2797 uint64_t ret = 0;
2798 for (Output_data_list::const_iterator p = pdl->begin();
2799 p != pdl->end();
2800 ++p)
2801 {
2802 uint64_t addralign = (*p)->addralign();
2803 if (addralign > ret)
2804 ret = addralign;
2805 }
2806 return ret;
2807}
2808
4f4c5f80
ILT
2809// Return the number of dynamic relocs applied to this segment.
2810
2811unsigned int
2812Output_segment::dynamic_reloc_count() const
2813{
2814 return (this->dynamic_reloc_count_list(&this->output_data_)
2815 + this->dynamic_reloc_count_list(&this->output_bss_));
2816}
2817
2818// Return the number of dynamic relocs applied to an Output_data_list.
2819
2820unsigned int
2821Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
2822{
2823 unsigned int count = 0;
2824 for (Output_data_list::const_iterator p = pdl->begin();
2825 p != pdl->end();
2826 ++p)
2827 count += (*p)->dynamic_reloc_count();
2828 return count;
2829}
2830
a445fddf
ILT
2831// Set the section addresses for an Output_segment. If RESET is true,
2832// reset the addresses first. ADDR is the address and *POFF is the
2833// file offset. Set the section indexes starting with *PSHNDX.
2834// Return the address of the immediately following segment. Update
2835// *POFF and *PSHNDX.
75f65a3e
ILT
2836
2837uint64_t
96a2b4e4
ILT
2838Output_segment::set_section_addresses(const Layout* layout, bool reset,
2839 uint64_t addr, off_t* poff,
ead1e424 2840 unsigned int* pshndx)
75f65a3e 2841{
a3ad94ed 2842 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e 2843
a445fddf
ILT
2844 if (!reset && this->are_addresses_set_)
2845 {
2846 gold_assert(this->paddr_ == addr);
2847 addr = this->vaddr_;
2848 }
2849 else
2850 {
2851 this->vaddr_ = addr;
2852 this->paddr_ = addr;
2853 this->are_addresses_set_ = true;
2854 }
75f65a3e 2855
96a2b4e4
ILT
2856 bool in_tls = false;
2857
9f1d377b
ILT
2858 bool in_relro = (parameters->options().relro()
2859 && this->is_first_section_relro());
2860
75f65a3e
ILT
2861 off_t orig_off = *poff;
2862 this->offset_ = orig_off;
2863
96a2b4e4 2864 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
9f1d377b
ILT
2865 addr, poff, pshndx, &in_tls,
2866 &in_relro);
75f65a3e
ILT
2867 this->filesz_ = *poff - orig_off;
2868
2869 off_t off = *poff;
2870
96a2b4e4
ILT
2871 uint64_t ret = this->set_section_list_addresses(layout, reset,
2872 &this->output_bss_,
2873 addr, poff, pshndx,
9f1d377b 2874 &in_tls, &in_relro);
96a2b4e4
ILT
2875
2876 // If the last section was a TLS section, align upward to the
2877 // alignment of the TLS segment, so that the overall size of the TLS
2878 // segment is aligned.
2879 if (in_tls)
2880 {
2881 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
2882 *poff = align_address(*poff, segment_align);
2883 }
2884
9f1d377b
ILT
2885 // If all the sections were relro sections, align upward to the
2886 // common page size.
2887 if (in_relro)
2888 {
2889 uint64_t page_align = parameters->target().common_pagesize();
2890 *poff = align_address(*poff, page_align);
2891 }
2892
75f65a3e
ILT
2893 this->memsz_ = *poff - orig_off;
2894
2895 // Ignore the file offset adjustments made by the BSS Output_data
2896 // objects.
2897 *poff = off;
61ba1cf9
ILT
2898
2899 return ret;
75f65a3e
ILT
2900}
2901
b8e6aad9
ILT
2902// Set the addresses and file offsets in a list of Output_data
2903// structures.
75f65a3e
ILT
2904
2905uint64_t
96a2b4e4
ILT
2906Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
2907 Output_data_list* pdl,
ead1e424 2908 uint64_t addr, off_t* poff,
96a2b4e4 2909 unsigned int* pshndx,
9f1d377b 2910 bool* in_tls, bool* in_relro)
75f65a3e 2911{
ead1e424 2912 off_t startoff = *poff;
75f65a3e 2913
ead1e424 2914 off_t off = startoff;
75f65a3e
ILT
2915 for (Output_data_list::iterator p = pdl->begin();
2916 p != pdl->end();
2917 ++p)
2918 {
a445fddf
ILT
2919 if (reset)
2920 (*p)->reset_address_and_file_offset();
2921
2922 // When using a linker script the section will most likely
2923 // already have an address.
2924 if (!(*p)->is_address_valid())
3802b2dd 2925 {
96a2b4e4
ILT
2926 uint64_t align = (*p)->addralign();
2927
2928 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
2929 {
2930 // Give the first TLS section the alignment of the
2931 // entire TLS segment. Otherwise the TLS segment as a
2932 // whole may be misaligned.
2933 if (!*in_tls)
2934 {
2935 Output_segment* tls_segment = layout->tls_segment();
2936 gold_assert(tls_segment != NULL);
2937 uint64_t segment_align = tls_segment->maximum_alignment();
2938 gold_assert(segment_align >= align);
2939 align = segment_align;
2940
2941 *in_tls = true;
2942 }
2943 }
2944 else
2945 {
2946 // If this is the first section after the TLS segment,
2947 // align it to at least the alignment of the TLS
2948 // segment, so that the size of the overall TLS segment
2949 // is aligned.
2950 if (*in_tls)
2951 {
2952 uint64_t segment_align =
2953 layout->tls_segment()->maximum_alignment();
2954 if (segment_align > align)
2955 align = segment_align;
2956
2957 *in_tls = false;
2958 }
2959 }
2960
9f1d377b
ILT
2961 // If this is a non-relro section after a relro section,
2962 // align it to a common page boundary so that the dynamic
2963 // linker has a page to mark as read-only.
2964 if (*in_relro
2965 && (!(*p)->is_section()
2966 || !(*p)->output_section()->is_relro()))
2967 {
2968 uint64_t page_align = parameters->target().common_pagesize();
2969 if (page_align > align)
2970 align = page_align;
2971 *in_relro = false;
2972 }
2973
96a2b4e4 2974 off = align_address(off, align);
3802b2dd
ILT
2975 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
2976 }
a445fddf
ILT
2977 else
2978 {
2979 // The script may have inserted a skip forward, but it
2980 // better not have moved backward.
3802b2dd
ILT
2981 gold_assert((*p)->address() >= addr + (off - startoff));
2982 off += (*p)->address() - (addr + (off - startoff));
a445fddf
ILT
2983 (*p)->set_file_offset(off);
2984 (*p)->finalize_data_size();
2985 }
ead1e424 2986
96a2b4e4
ILT
2987 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
2988 // section. Such a section does not affect the size of a
2989 // PT_LOAD segment.
2990 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
ead1e424
ILT
2991 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
2992 off += (*p)->data_size();
75f65a3e 2993
ead1e424
ILT
2994 if ((*p)->is_section())
2995 {
2996 (*p)->set_out_shndx(*pshndx);
2997 ++*pshndx;
2998 }
75f65a3e
ILT
2999 }
3000
3001 *poff = off;
ead1e424 3002 return addr + (off - startoff);
75f65a3e
ILT
3003}
3004
3005// For a non-PT_LOAD segment, set the offset from the sections, if
3006// any.
3007
3008void
3009Output_segment::set_offset()
3010{
a3ad94ed 3011 gold_assert(this->type_ != elfcpp::PT_LOAD);
75f65a3e 3012
a445fddf
ILT
3013 gold_assert(!this->are_addresses_set_);
3014
75f65a3e
ILT
3015 if (this->output_data_.empty() && this->output_bss_.empty())
3016 {
3017 this->vaddr_ = 0;
3018 this->paddr_ = 0;
a445fddf 3019 this->are_addresses_set_ = true;
75f65a3e 3020 this->memsz_ = 0;
a445fddf 3021 this->min_p_align_ = 0;
75f65a3e
ILT
3022 this->offset_ = 0;
3023 this->filesz_ = 0;
3024 return;
3025 }
3026
3027 const Output_data* first;
3028 if (this->output_data_.empty())
3029 first = this->output_bss_.front();
3030 else
3031 first = this->output_data_.front();
3032 this->vaddr_ = first->address();
a445fddf
ILT
3033 this->paddr_ = (first->has_load_address()
3034 ? first->load_address()
3035 : this->vaddr_);
3036 this->are_addresses_set_ = true;
75f65a3e
ILT
3037 this->offset_ = first->offset();
3038
3039 if (this->output_data_.empty())
3040 this->filesz_ = 0;
3041 else
3042 {
3043 const Output_data* last_data = this->output_data_.back();
3044 this->filesz_ = (last_data->address()
3045 + last_data->data_size()
3046 - this->vaddr_);
3047 }
3048
3049 const Output_data* last;
3050 if (this->output_bss_.empty())
3051 last = this->output_data_.back();
3052 else
3053 last = this->output_bss_.back();
3054 this->memsz_ = (last->address()
3055 + last->data_size()
3056 - this->vaddr_);
96a2b4e4
ILT
3057
3058 // If this is a TLS segment, align the memory size. The code in
3059 // set_section_list ensures that the section after the TLS segment
3060 // is aligned to give us room.
3061 if (this->type_ == elfcpp::PT_TLS)
3062 {
3063 uint64_t segment_align = this->maximum_alignment();
3064 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3065 this->memsz_ = align_address(this->memsz_, segment_align);
3066 }
9f1d377b
ILT
3067
3068 // If this is a RELRO segment, align the memory size. The code in
3069 // set_section_list ensures that the section after the RELRO segment
3070 // is aligned to give us room.
3071 if (this->type_ == elfcpp::PT_GNU_RELRO)
3072 {
3073 uint64_t page_align = parameters->target().common_pagesize();
3074 gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3075 this->memsz_ = align_address(this->memsz_, page_align);
3076 }
75f65a3e
ILT
3077}
3078
7bf1f802
ILT
3079// Set the TLS offsets of the sections in the PT_TLS segment.
3080
3081void
3082Output_segment::set_tls_offsets()
3083{
3084 gold_assert(this->type_ == elfcpp::PT_TLS);
3085
3086 for (Output_data_list::iterator p = this->output_data_.begin();
3087 p != this->output_data_.end();
3088 ++p)
3089 (*p)->set_tls_offset(this->vaddr_);
3090
3091 for (Output_data_list::iterator p = this->output_bss_.begin();
3092 p != this->output_bss_.end();
3093 ++p)
3094 (*p)->set_tls_offset(this->vaddr_);
3095}
3096
a445fddf
ILT
3097// Return the address of the first section.
3098
3099uint64_t
3100Output_segment::first_section_load_address() const
3101{
3102 for (Output_data_list::const_iterator p = this->output_data_.begin();
3103 p != this->output_data_.end();
3104 ++p)
3105 if ((*p)->is_section())
3106 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3107
3108 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3109 p != this->output_bss_.end();
3110 ++p)
3111 if ((*p)->is_section())
3112 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3113
3114 gold_unreachable();
3115}
3116
75f65a3e
ILT
3117// Return the number of Output_sections in an Output_segment.
3118
3119unsigned int
3120Output_segment::output_section_count() const
3121{
3122 return (this->output_section_count_list(&this->output_data_)
3123 + this->output_section_count_list(&this->output_bss_));
3124}
3125
3126// Return the number of Output_sections in an Output_data_list.
3127
3128unsigned int
3129Output_segment::output_section_count_list(const Output_data_list* pdl) const
3130{
3131 unsigned int count = 0;
3132 for (Output_data_list::const_iterator p = pdl->begin();
3133 p != pdl->end();
3134 ++p)
3135 {
3136 if ((*p)->is_section())
3137 ++count;
3138 }
3139 return count;
a2fb1b05
ILT
3140}
3141
1c4f3631
ILT
3142// Return the section attached to the list segment with the lowest
3143// load address. This is used when handling a PHDRS clause in a
3144// linker script.
3145
3146Output_section*
3147Output_segment::section_with_lowest_load_address() const
3148{
3149 Output_section* found = NULL;
3150 uint64_t found_lma = 0;
3151 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3152
3153 Output_section* found_data = found;
3154 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3155 if (found != found_data && found_data != NULL)
3156 {
3157 gold_error(_("nobits section %s may not precede progbits section %s "
3158 "in same segment"),
3159 found->name(), found_data->name());
3160 return NULL;
3161 }
3162
3163 return found;
3164}
3165
3166// Look through a list for a section with a lower load address.
3167
3168void
3169Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3170 Output_section** found,
3171 uint64_t* found_lma) const
3172{
3173 for (Output_data_list::const_iterator p = pdl->begin();
3174 p != pdl->end();
3175 ++p)
3176 {
3177 if (!(*p)->is_section())
3178 continue;
3179 Output_section* os = static_cast<Output_section*>(*p);
3180 uint64_t lma = (os->has_load_address()
3181 ? os->load_address()
3182 : os->address());
3183 if (*found == NULL || lma < *found_lma)
3184 {
3185 *found = os;
3186 *found_lma = lma;
3187 }
3188 }
3189}
3190
61ba1cf9
ILT
3191// Write the segment data into *OPHDR.
3192
3193template<int size, bool big_endian>
3194void
ead1e424 3195Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
61ba1cf9
ILT
3196{
3197 ophdr->put_p_type(this->type_);
3198 ophdr->put_p_offset(this->offset_);
3199 ophdr->put_p_vaddr(this->vaddr_);
3200 ophdr->put_p_paddr(this->paddr_);
3201 ophdr->put_p_filesz(this->filesz_);
3202 ophdr->put_p_memsz(this->memsz_);
3203 ophdr->put_p_flags(this->flags_);
a445fddf 3204 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
61ba1cf9
ILT
3205}
3206
3207// Write the section headers into V.
3208
3209template<int size, bool big_endian>
3210unsigned char*
16649710
ILT
3211Output_segment::write_section_headers(const Layout* layout,
3212 const Stringpool* secnamepool,
ead1e424 3213 unsigned char* v,
7d1a9ebb 3214 unsigned int *pshndx) const
5482377d 3215{
ead1e424
ILT
3216 // Every section that is attached to a segment must be attached to a
3217 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3218 // segments.
3219 if (this->type_ != elfcpp::PT_LOAD)
3220 return v;
3221
7d1a9ebb
ILT
3222 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3223 &this->output_data_,
3224 v, pshndx);
3225 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3226 &this->output_bss_,
3227 v, pshndx);
61ba1cf9
ILT
3228 return v;
3229}
3230
3231template<int size, bool big_endian>
3232unsigned char*
16649710
ILT
3233Output_segment::write_section_headers_list(const Layout* layout,
3234 const Stringpool* secnamepool,
61ba1cf9 3235 const Output_data_list* pdl,
ead1e424 3236 unsigned char* v,
7d1a9ebb 3237 unsigned int* pshndx) const
61ba1cf9
ILT
3238{
3239 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3240 for (Output_data_list::const_iterator p = pdl->begin();
3241 p != pdl->end();
3242 ++p)
3243 {
3244 if ((*p)->is_section())
3245 {
5482377d 3246 const Output_section* ps = static_cast<const Output_section*>(*p);
a3ad94ed 3247 gold_assert(*pshndx == ps->out_shndx());
61ba1cf9 3248 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 3249 ps->write_header(layout, secnamepool, &oshdr);
61ba1cf9 3250 v += shdr_size;
ead1e424 3251 ++*pshndx;
61ba1cf9
ILT
3252 }
3253 }
3254 return v;
3255}
3256
7d9e3d98
ILT
3257// Print the output sections to the map file.
3258
3259void
3260Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3261{
3262 if (this->type() != elfcpp::PT_LOAD)
3263 return;
3264 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3265 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3266}
3267
3268// Print an output section list to the map file.
3269
3270void
3271Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3272 const Output_data_list* pdl) const
3273{
3274 for (Output_data_list::const_iterator p = pdl->begin();
3275 p != pdl->end();
3276 ++p)
3277 (*p)->print_to_mapfile(mapfile);
3278}
3279
a2fb1b05
ILT
3280// Output_file methods.
3281
14144f39
ILT
3282Output_file::Output_file(const char* name)
3283 : name_(name),
61ba1cf9
ILT
3284 o_(-1),
3285 file_size_(0),
c420411f 3286 base_(NULL),
516cb3d0
ILT
3287 map_is_anonymous_(false),
3288 is_temporary_(false)
61ba1cf9
ILT
3289{
3290}
3291
3292// Open the output file.
3293
a2fb1b05 3294void
61ba1cf9 3295Output_file::open(off_t file_size)
a2fb1b05 3296{
61ba1cf9
ILT
3297 this->file_size_ = file_size;
3298
4e9d8586
ILT
3299 // Unlink the file first; otherwise the open() may fail if the file
3300 // is busy (e.g. it's an executable that's currently being executed).
3301 //
3302 // However, the linker may be part of a system where a zero-length
3303 // file is created for it to write to, with tight permissions (gcc
3304 // 2.95 did something like this). Unlinking the file would work
3305 // around those permission controls, so we only unlink if the file
3306 // has a non-zero size. We also unlink only regular files to avoid
3307 // trouble with directories/etc.
3308 //
3309 // If we fail, continue; this command is merely a best-effort attempt
3310 // to improve the odds for open().
3311
42a1b686 3312 // We let the name "-" mean "stdout"
516cb3d0 3313 if (!this->is_temporary_)
42a1b686 3314 {
516cb3d0
ILT
3315 if (strcmp(this->name_, "-") == 0)
3316 this->o_ = STDOUT_FILENO;
3317 else
3318 {
3319 struct stat s;
3320 if (::stat(this->name_, &s) == 0 && s.st_size != 0)
3321 unlink_if_ordinary(this->name_);
3322
8851ecca 3323 int mode = parameters->options().relocatable() ? 0666 : 0777;
516cb3d0
ILT
3324 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
3325 if (o < 0)
3326 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3327 this->o_ = o;
3328 }
42a1b686 3329 }
61ba1cf9 3330
27bc2bce
ILT
3331 this->map();
3332}
3333
3334// Resize the output file.
3335
3336void
3337Output_file::resize(off_t file_size)
3338{
c420411f
ILT
3339 // If the mmap is mapping an anonymous memory buffer, this is easy:
3340 // just mremap to the new size. If it's mapping to a file, we want
3341 // to unmap to flush to the file, then remap after growing the file.
3342 if (this->map_is_anonymous_)
3343 {
3344 void* base = ::mremap(this->base_, this->file_size_, file_size,
3345 MREMAP_MAYMOVE);
3346 if (base == MAP_FAILED)
3347 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3348 this->base_ = static_cast<unsigned char*>(base);
3349 this->file_size_ = file_size;
3350 }
3351 else
3352 {
3353 this->unmap();
3354 this->file_size_ = file_size;
3355 this->map();
3356 }
27bc2bce
ILT
3357}
3358
3359// Map the file into memory.
3360
3361void
3362Output_file::map()
3363{
c420411f 3364 const int o = this->o_;
61ba1cf9 3365
c420411f
ILT
3366 // If the output file is not a regular file, don't try to mmap it;
3367 // instead, we'll mmap a block of memory (an anonymous buffer), and
3368 // then later write the buffer to the file.
3369 void* base;
3370 struct stat statbuf;
42a1b686
ILT
3371 if (o == STDOUT_FILENO || o == STDERR_FILENO
3372 || ::fstat(o, &statbuf) != 0
516cb3d0
ILT
3373 || !S_ISREG(statbuf.st_mode)
3374 || this->is_temporary_)
c420411f
ILT
3375 {
3376 this->map_is_anonymous_ = true;
3377 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3378 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3379 }
3380 else
3381 {
3382 // Write out one byte to make the file the right size.
3383 if (::lseek(o, this->file_size_ - 1, SEEK_SET) < 0)
3384 gold_fatal(_("%s: lseek: %s"), this->name_, strerror(errno));
3385 char b = 0;
3386 if (::write(o, &b, 1) != 1)
3387 gold_fatal(_("%s: write: %s"), this->name_, strerror(errno));
3388
3389 // Map the file into memory.
3390 this->map_is_anonymous_ = false;
3391 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3392 MAP_SHARED, o, 0);
3393 }
61ba1cf9 3394 if (base == MAP_FAILED)
75f2446e 3395 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
61ba1cf9
ILT
3396 this->base_ = static_cast<unsigned char*>(base);
3397}
3398
c420411f 3399// Unmap the file from memory.
61ba1cf9
ILT
3400
3401void
c420411f 3402Output_file::unmap()
61ba1cf9
ILT
3403{
3404 if (::munmap(this->base_, this->file_size_) < 0)
a0c4fb0a 3405 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
61ba1cf9 3406 this->base_ = NULL;
c420411f
ILT
3407}
3408
3409// Close the output file.
3410
3411void
3412Output_file::close()
3413{
3414 // If the map isn't file-backed, we need to write it now.
516cb3d0 3415 if (this->map_is_anonymous_ && !this->is_temporary_)
c420411f
ILT
3416 {
3417 size_t bytes_to_write = this->file_size_;
3418 while (bytes_to_write > 0)
3419 {
3420 ssize_t bytes_written = ::write(this->o_, this->base_, bytes_to_write);
3421 if (bytes_written == 0)
3422 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
3423 else if (bytes_written < 0)
3424 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
3425 else
3426 bytes_to_write -= bytes_written;
3427 }
3428 }
3429 this->unmap();
61ba1cf9 3430
42a1b686 3431 // We don't close stdout or stderr
516cb3d0
ILT
3432 if (this->o_ != STDOUT_FILENO
3433 && this->o_ != STDERR_FILENO
3434 && !this->is_temporary_)
42a1b686
ILT
3435 if (::close(this->o_) < 0)
3436 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
61ba1cf9 3437 this->o_ = -1;
a2fb1b05
ILT
3438}
3439
3440// Instantiate the templates we need. We could use the configure
3441// script to restrict this to only the ones for implemented targets.
3442
193a53d9 3443#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
3444template
3445off_t
3446Output_section::add_input_section<32, false>(
730cdc88 3447 Sized_relobj<32, false>* object,
ead1e424 3448 unsigned int shndx,
a2fb1b05 3449 const char* secname,
730cdc88 3450 const elfcpp::Shdr<32, false>& shdr,
a445fddf
ILT
3451 unsigned int reloc_shndx,
3452 bool have_sections_script);
193a53d9 3453#endif
a2fb1b05 3454
193a53d9 3455#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
3456template
3457off_t
3458Output_section::add_input_section<32, true>(
730cdc88 3459 Sized_relobj<32, true>* object,
ead1e424 3460 unsigned int shndx,
a2fb1b05 3461 const char* secname,
730cdc88 3462 const elfcpp::Shdr<32, true>& shdr,
a445fddf
ILT
3463 unsigned int reloc_shndx,
3464 bool have_sections_script);
193a53d9 3465#endif
a2fb1b05 3466
193a53d9 3467#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
3468template
3469off_t
3470Output_section::add_input_section<64, false>(
730cdc88 3471 Sized_relobj<64, false>* object,
ead1e424 3472 unsigned int shndx,
a2fb1b05 3473 const char* secname,
730cdc88 3474 const elfcpp::Shdr<64, false>& shdr,
a445fddf
ILT
3475 unsigned int reloc_shndx,
3476 bool have_sections_script);
193a53d9 3477#endif
a2fb1b05 3478
193a53d9 3479#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
3480template
3481off_t
3482Output_section::add_input_section<64, true>(
730cdc88 3483 Sized_relobj<64, true>* object,
ead1e424 3484 unsigned int shndx,
a2fb1b05 3485 const char* secname,
730cdc88 3486 const elfcpp::Shdr<64, true>& shdr,
a445fddf
ILT
3487 unsigned int reloc_shndx,
3488 bool have_sections_script);
193a53d9 3489#endif
a2fb1b05 3490
193a53d9 3491#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3492template
3493class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
193a53d9 3494#endif
c06b7b0b 3495
193a53d9 3496#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3497template
3498class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
193a53d9 3499#endif
c06b7b0b 3500
193a53d9 3501#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3502template
3503class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
193a53d9 3504#endif
c06b7b0b 3505
193a53d9 3506#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3507template
3508class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
193a53d9 3509#endif
c06b7b0b 3510
193a53d9 3511#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3512template
3513class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
193a53d9 3514#endif
c06b7b0b 3515
193a53d9 3516#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3517template
3518class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
193a53d9 3519#endif
c06b7b0b 3520
193a53d9 3521#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3522template
3523class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
193a53d9 3524#endif
c06b7b0b 3525
193a53d9 3526#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3527template
3528class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
193a53d9 3529#endif
c06b7b0b 3530
193a53d9 3531#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3532template
3533class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
193a53d9 3534#endif
c06b7b0b 3535
193a53d9 3536#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3537template
3538class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
193a53d9 3539#endif
c06b7b0b 3540
193a53d9 3541#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3542template
3543class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
193a53d9 3544#endif
c06b7b0b 3545
193a53d9 3546#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3547template
3548class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
193a53d9 3549#endif
c06b7b0b 3550
193a53d9 3551#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
3552template
3553class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
193a53d9 3554#endif
c06b7b0b 3555
193a53d9 3556#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
3557template
3558class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
193a53d9 3559#endif
c06b7b0b 3560
193a53d9 3561#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
3562template
3563class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
193a53d9 3564#endif
c06b7b0b 3565
193a53d9 3566#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
3567template
3568class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
193a53d9 3569#endif
c06b7b0b 3570
6a74a719
ILT
3571#ifdef HAVE_TARGET_32_LITTLE
3572template
3573class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
3574#endif
3575
3576#ifdef HAVE_TARGET_32_BIG
3577template
3578class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
3579#endif
3580
3581#ifdef HAVE_TARGET_64_LITTLE
3582template
3583class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
3584#endif
3585
3586#ifdef HAVE_TARGET_64_BIG
3587template
3588class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
3589#endif
3590
3591#ifdef HAVE_TARGET_32_LITTLE
3592template
3593class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
3594#endif
3595
3596#ifdef HAVE_TARGET_32_BIG
3597template
3598class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
3599#endif
3600
3601#ifdef HAVE_TARGET_64_LITTLE
3602template
3603class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
3604#endif
3605
3606#ifdef HAVE_TARGET_64_BIG
3607template
3608class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
3609#endif
3610
3611#ifdef HAVE_TARGET_32_LITTLE
3612template
3613class Output_data_group<32, false>;
3614#endif
3615
3616#ifdef HAVE_TARGET_32_BIG
3617template
3618class Output_data_group<32, true>;
3619#endif
3620
3621#ifdef HAVE_TARGET_64_LITTLE
3622template
3623class Output_data_group<64, false>;
3624#endif
3625
3626#ifdef HAVE_TARGET_64_BIG
3627template
3628class Output_data_group<64, true>;
3629#endif
3630
193a53d9 3631#ifdef HAVE_TARGET_32_LITTLE
ead1e424 3632template
dbe717ef 3633class Output_data_got<32, false>;
193a53d9 3634#endif
ead1e424 3635
193a53d9 3636#ifdef HAVE_TARGET_32_BIG
ead1e424 3637template
dbe717ef 3638class Output_data_got<32, true>;
193a53d9 3639#endif
ead1e424 3640
193a53d9 3641#ifdef HAVE_TARGET_64_LITTLE
ead1e424 3642template
dbe717ef 3643class Output_data_got<64, false>;
193a53d9 3644#endif
ead1e424 3645
193a53d9 3646#ifdef HAVE_TARGET_64_BIG
ead1e424 3647template
dbe717ef 3648class Output_data_got<64, true>;
193a53d9 3649#endif
ead1e424 3650
a2fb1b05 3651} // End namespace gold.
This page took 0.307199 seconds and 4 git commands to generate.