Fix ChangeLog typo.
[deliverable/binutils-gdb.git] / gold / output.cc
CommitLineData
a2fb1b05
ILT
1// output.cc -- manage the output file for gold
2
e29e076a 3// Copyright 2006, 2007, 2008, 2009 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>
6a89f575 33#include "libiberty.h"
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"
2a00e4fb 40#include "descriptors.h"
a2fb1b05
ILT
41#include "output.h"
42
c420411f
ILT
43// Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS
44#ifndef MAP_ANONYMOUS
45# define MAP_ANONYMOUS MAP_ANON
46#endif
47
9201d894
ILT
48#ifndef HAVE_POSIX_FALLOCATE
49// A dummy, non general, version of posix_fallocate. Here we just set
50// the file size and hope that there is enough disk space. FIXME: We
51// could allocate disk space by walking block by block and writing a
52// zero byte into each block.
53static int
54posix_fallocate(int o, off_t offset, off_t len)
55{
56 return ftruncate(o, offset + len);
57}
58#endif // !defined(HAVE_POSIX_FALLOCATE)
59
a2fb1b05
ILT
60namespace gold
61{
62
a3ad94ed
ILT
63// Output_data variables.
64
27bc2bce 65bool Output_data::allocated_sizes_are_fixed;
a3ad94ed 66
a2fb1b05
ILT
67// Output_data methods.
68
69Output_data::~Output_data()
70{
71}
72
730cdc88
ILT
73// Return the default alignment for the target size.
74
75uint64_t
76Output_data::default_alignment()
77{
8851ecca
ILT
78 return Output_data::default_alignment_for_size(
79 parameters->target().get_size());
730cdc88
ILT
80}
81
75f65a3e
ILT
82// Return the default alignment for a size--32 or 64.
83
84uint64_t
730cdc88 85Output_data::default_alignment_for_size(int size)
75f65a3e
ILT
86{
87 if (size == 32)
88 return 4;
89 else if (size == 64)
90 return 8;
91 else
a3ad94ed 92 gold_unreachable();
75f65a3e
ILT
93}
94
75f65a3e
ILT
95// Output_section_header methods. This currently assumes that the
96// segment and section lists are complete at construction time.
97
98Output_section_headers::Output_section_headers(
16649710
ILT
99 const Layout* layout,
100 const Layout::Segment_list* segment_list,
6a74a719 101 const Layout::Section_list* section_list,
16649710 102 const Layout::Section_list* unattached_section_list,
d491d34e
ILT
103 const Stringpool* secnamepool,
104 const Output_section* shstrtab_section)
9025d29d 105 : layout_(layout),
75f65a3e 106 segment_list_(segment_list),
6a74a719 107 section_list_(section_list),
a3ad94ed 108 unattached_section_list_(unattached_section_list),
d491d34e
ILT
109 secnamepool_(secnamepool),
110 shstrtab_section_(shstrtab_section)
20e6d0d6
DK
111{
112}
113
114// Compute the current data size.
115
116off_t
117Output_section_headers::do_size() const
75f65a3e 118{
61ba1cf9
ILT
119 // Count all the sections. Start with 1 for the null section.
120 off_t count = 1;
8851ecca 121 if (!parameters->options().relocatable())
6a74a719 122 {
20e6d0d6
DK
123 for (Layout::Segment_list::const_iterator p =
124 this->segment_list_->begin();
125 p != this->segment_list_->end();
6a74a719
ILT
126 ++p)
127 if ((*p)->type() == elfcpp::PT_LOAD)
128 count += (*p)->output_section_count();
129 }
130 else
131 {
20e6d0d6
DK
132 for (Layout::Section_list::const_iterator p =
133 this->section_list_->begin();
134 p != this->section_list_->end();
6a74a719
ILT
135 ++p)
136 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
137 ++count;
138 }
20e6d0d6 139 count += this->unattached_section_list_->size();
75f65a3e 140
8851ecca 141 const int size = parameters->target().get_size();
75f65a3e
ILT
142 int shdr_size;
143 if (size == 32)
144 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
145 else if (size == 64)
146 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
147 else
a3ad94ed 148 gold_unreachable();
75f65a3e 149
20e6d0d6 150 return count * shdr_size;
75f65a3e
ILT
151}
152
61ba1cf9
ILT
153// Write out the section headers.
154
75f65a3e 155void
61ba1cf9 156Output_section_headers::do_write(Output_file* of)
a2fb1b05 157{
8851ecca 158 switch (parameters->size_and_endianness())
61ba1cf9 159 {
9025d29d 160#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
161 case Parameters::TARGET_32_LITTLE:
162 this->do_sized_write<32, false>(of);
163 break;
9025d29d 164#endif
8851ecca
ILT
165#ifdef HAVE_TARGET_32_BIG
166 case Parameters::TARGET_32_BIG:
167 this->do_sized_write<32, true>(of);
168 break;
9025d29d 169#endif
9025d29d 170#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
171 case Parameters::TARGET_64_LITTLE:
172 this->do_sized_write<64, false>(of);
173 break;
9025d29d 174#endif
8851ecca
ILT
175#ifdef HAVE_TARGET_64_BIG
176 case Parameters::TARGET_64_BIG:
177 this->do_sized_write<64, true>(of);
178 break;
179#endif
180 default:
181 gold_unreachable();
61ba1cf9 182 }
61ba1cf9
ILT
183}
184
185template<int size, bool big_endian>
186void
187Output_section_headers::do_sized_write(Output_file* of)
188{
189 off_t all_shdrs_size = this->data_size();
190 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
191
192 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
193 unsigned char* v = view;
194
195 {
196 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
197 oshdr.put_sh_name(0);
198 oshdr.put_sh_type(elfcpp::SHT_NULL);
199 oshdr.put_sh_flags(0);
200 oshdr.put_sh_addr(0);
201 oshdr.put_sh_offset(0);
d491d34e
ILT
202
203 size_t section_count = (this->data_size()
204 / elfcpp::Elf_sizes<size>::shdr_size);
205 if (section_count < elfcpp::SHN_LORESERVE)
206 oshdr.put_sh_size(0);
207 else
208 oshdr.put_sh_size(section_count);
209
210 unsigned int shstrndx = this->shstrtab_section_->out_shndx();
211 if (shstrndx < elfcpp::SHN_LORESERVE)
212 oshdr.put_sh_link(0);
213 else
214 oshdr.put_sh_link(shstrndx);
215
61ba1cf9
ILT
216 oshdr.put_sh_info(0);
217 oshdr.put_sh_addralign(0);
218 oshdr.put_sh_entsize(0);
219 }
220
221 v += shdr_size;
222
6a74a719 223 unsigned int shndx = 1;
8851ecca 224 if (!parameters->options().relocatable())
6a74a719
ILT
225 {
226 for (Layout::Segment_list::const_iterator p =
227 this->segment_list_->begin();
228 p != this->segment_list_->end();
229 ++p)
230 v = (*p)->write_section_headers<size, big_endian>(this->layout_,
231 this->secnamepool_,
232 v,
233 &shndx);
234 }
235 else
236 {
237 for (Layout::Section_list::const_iterator p =
238 this->section_list_->begin();
239 p != this->section_list_->end();
240 ++p)
241 {
242 // We do unallocated sections below, except that group
243 // sections have to come first.
244 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
245 && (*p)->type() != elfcpp::SHT_GROUP)
246 continue;
247 gold_assert(shndx == (*p)->out_shndx());
248 elfcpp::Shdr_write<size, big_endian> oshdr(v);
249 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
250 v += shdr_size;
251 ++shndx;
252 }
253 }
254
a3ad94ed 255 for (Layout::Section_list::const_iterator p =
16649710
ILT
256 this->unattached_section_list_->begin();
257 p != this->unattached_section_list_->end();
61ba1cf9
ILT
258 ++p)
259 {
6a74a719
ILT
260 // For a relocatable link, we did unallocated group sections
261 // above, since they have to come first.
262 if ((*p)->type() == elfcpp::SHT_GROUP
8851ecca 263 && parameters->options().relocatable())
6a74a719 264 continue;
a3ad94ed 265 gold_assert(shndx == (*p)->out_shndx());
61ba1cf9 266 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 267 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
61ba1cf9 268 v += shdr_size;
ead1e424 269 ++shndx;
61ba1cf9
ILT
270 }
271
272 of->write_output_view(this->offset(), all_shdrs_size, view);
a2fb1b05
ILT
273}
274
54dc6425
ILT
275// Output_segment_header methods.
276
61ba1cf9 277Output_segment_headers::Output_segment_headers(
61ba1cf9 278 const Layout::Segment_list& segment_list)
9025d29d 279 : segment_list_(segment_list)
61ba1cf9 280{
61ba1cf9
ILT
281}
282
54dc6425 283void
61ba1cf9 284Output_segment_headers::do_write(Output_file* of)
75f65a3e 285{
8851ecca 286 switch (parameters->size_and_endianness())
61ba1cf9 287 {
9025d29d 288#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
289 case Parameters::TARGET_32_LITTLE:
290 this->do_sized_write<32, false>(of);
291 break;
9025d29d 292#endif
8851ecca
ILT
293#ifdef HAVE_TARGET_32_BIG
294 case Parameters::TARGET_32_BIG:
295 this->do_sized_write<32, true>(of);
296 break;
9025d29d 297#endif
9025d29d 298#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
299 case Parameters::TARGET_64_LITTLE:
300 this->do_sized_write<64, false>(of);
301 break;
9025d29d 302#endif
8851ecca
ILT
303#ifdef HAVE_TARGET_64_BIG
304 case Parameters::TARGET_64_BIG:
305 this->do_sized_write<64, true>(of);
306 break;
307#endif
308 default:
309 gold_unreachable();
61ba1cf9 310 }
61ba1cf9
ILT
311}
312
313template<int size, bool big_endian>
314void
315Output_segment_headers::do_sized_write(Output_file* of)
316{
317 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
318 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
a445fddf 319 gold_assert(all_phdrs_size == this->data_size());
61ba1cf9
ILT
320 unsigned char* view = of->get_output_view(this->offset(),
321 all_phdrs_size);
322 unsigned char* v = view;
323 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
324 p != this->segment_list_.end();
325 ++p)
326 {
327 elfcpp::Phdr_write<size, big_endian> ophdr(v);
328 (*p)->write_header(&ophdr);
329 v += phdr_size;
330 }
331
a445fddf
ILT
332 gold_assert(v - view == all_phdrs_size);
333
61ba1cf9 334 of->write_output_view(this->offset(), all_phdrs_size, view);
75f65a3e
ILT
335}
336
20e6d0d6
DK
337off_t
338Output_segment_headers::do_size() const
339{
340 const int size = parameters->target().get_size();
341 int phdr_size;
342 if (size == 32)
343 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
344 else if (size == 64)
345 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
346 else
347 gold_unreachable();
348
349 return this->segment_list_.size() * phdr_size;
350}
351
75f65a3e
ILT
352// Output_file_header methods.
353
9025d29d 354Output_file_header::Output_file_header(const Target* target,
75f65a3e 355 const Symbol_table* symtab,
d391083d
ILT
356 const Output_segment_headers* osh,
357 const char* entry)
9025d29d 358 : target_(target),
75f65a3e 359 symtab_(symtab),
61ba1cf9 360 segment_header_(osh),
75f65a3e 361 section_header_(NULL),
d391083d
ILT
362 shstrtab_(NULL),
363 entry_(entry)
75f65a3e 364{
20e6d0d6 365 this->set_data_size(this->do_size());
75f65a3e
ILT
366}
367
368// Set the section table information for a file header.
369
370void
371Output_file_header::set_section_info(const Output_section_headers* shdrs,
372 const Output_section* shstrtab)
373{
374 this->section_header_ = shdrs;
375 this->shstrtab_ = shstrtab;
376}
377
378// Write out the file header.
379
380void
61ba1cf9 381Output_file_header::do_write(Output_file* of)
54dc6425 382{
27bc2bce
ILT
383 gold_assert(this->offset() == 0);
384
8851ecca 385 switch (parameters->size_and_endianness())
61ba1cf9 386 {
9025d29d 387#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
388 case Parameters::TARGET_32_LITTLE:
389 this->do_sized_write<32, false>(of);
390 break;
9025d29d 391#endif
8851ecca
ILT
392#ifdef HAVE_TARGET_32_BIG
393 case Parameters::TARGET_32_BIG:
394 this->do_sized_write<32, true>(of);
395 break;
9025d29d 396#endif
9025d29d 397#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
398 case Parameters::TARGET_64_LITTLE:
399 this->do_sized_write<64, false>(of);
400 break;
9025d29d 401#endif
8851ecca
ILT
402#ifdef HAVE_TARGET_64_BIG
403 case Parameters::TARGET_64_BIG:
404 this->do_sized_write<64, true>(of);
405 break;
406#endif
407 default:
408 gold_unreachable();
61ba1cf9 409 }
61ba1cf9
ILT
410}
411
412// Write out the file header with appropriate size and endianess.
413
414template<int size, bool big_endian>
415void
416Output_file_header::do_sized_write(Output_file* of)
417{
a3ad94ed 418 gold_assert(this->offset() == 0);
61ba1cf9
ILT
419
420 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
421 unsigned char* view = of->get_output_view(0, ehdr_size);
422 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
423
424 unsigned char e_ident[elfcpp::EI_NIDENT];
425 memset(e_ident, 0, elfcpp::EI_NIDENT);
426 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
427 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
428 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
429 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
430 if (size == 32)
431 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
432 else if (size == 64)
433 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
434 else
a3ad94ed 435 gold_unreachable();
61ba1cf9
ILT
436 e_ident[elfcpp::EI_DATA] = (big_endian
437 ? elfcpp::ELFDATA2MSB
438 : elfcpp::ELFDATA2LSB);
439 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
61ba1cf9
ILT
440 oehdr.put_e_ident(e_ident);
441
442 elfcpp::ET e_type;
8851ecca 443 if (parameters->options().relocatable())
61ba1cf9 444 e_type = elfcpp::ET_REL;
374ad285 445 else if (parameters->options().output_is_position_independent())
436ca963 446 e_type = elfcpp::ET_DYN;
61ba1cf9
ILT
447 else
448 e_type = elfcpp::ET_EXEC;
449 oehdr.put_e_type(e_type);
450
451 oehdr.put_e_machine(this->target_->machine_code());
452 oehdr.put_e_version(elfcpp::EV_CURRENT);
453
d391083d 454 oehdr.put_e_entry(this->entry<size>());
61ba1cf9 455
6a74a719
ILT
456 if (this->segment_header_ == NULL)
457 oehdr.put_e_phoff(0);
458 else
459 oehdr.put_e_phoff(this->segment_header_->offset());
460
61ba1cf9 461 oehdr.put_e_shoff(this->section_header_->offset());
d5b40221 462 oehdr.put_e_flags(this->target_->processor_specific_flags());
61ba1cf9 463 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
6a74a719
ILT
464
465 if (this->segment_header_ == NULL)
466 {
467 oehdr.put_e_phentsize(0);
468 oehdr.put_e_phnum(0);
469 }
470 else
471 {
472 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
473 oehdr.put_e_phnum(this->segment_header_->data_size()
474 / elfcpp::Elf_sizes<size>::phdr_size);
475 }
476
61ba1cf9 477 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
d491d34e
ILT
478 size_t section_count = (this->section_header_->data_size()
479 / elfcpp::Elf_sizes<size>::shdr_size);
480
481 if (section_count < elfcpp::SHN_LORESERVE)
482 oehdr.put_e_shnum(this->section_header_->data_size()
483 / elfcpp::Elf_sizes<size>::shdr_size);
484 else
485 oehdr.put_e_shnum(0);
486
487 unsigned int shstrndx = this->shstrtab_->out_shndx();
488 if (shstrndx < elfcpp::SHN_LORESERVE)
489 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
490 else
491 oehdr.put_e_shstrndx(elfcpp::SHN_XINDEX);
61ba1cf9 492
36959681
ILT
493 // Let the target adjust the ELF header, e.g., to set EI_OSABI in
494 // the e_ident field.
495 parameters->target().adjust_elf_header(view, ehdr_size);
496
61ba1cf9 497 of->write_output_view(0, ehdr_size, view);
54dc6425
ILT
498}
499
d391083d
ILT
500// Return the value to use for the entry address. THIS->ENTRY_ is the
501// symbol specified on the command line, if any.
502
503template<int size>
504typename elfcpp::Elf_types<size>::Elf_Addr
505Output_file_header::entry()
506{
507 const bool should_issue_warning = (this->entry_ != NULL
8851ecca
ILT
508 && !parameters->options().relocatable()
509 && !parameters->options().shared());
d391083d
ILT
510
511 // FIXME: Need to support target specific entry symbol.
512 const char* entry = this->entry_;
513 if (entry == NULL)
514 entry = "_start";
515
516 Symbol* sym = this->symtab_->lookup(entry);
517
518 typename Sized_symbol<size>::Value_type v;
519 if (sym != NULL)
520 {
521 Sized_symbol<size>* ssym;
522 ssym = this->symtab_->get_sized_symbol<size>(sym);
523 if (!ssym->is_defined() && should_issue_warning)
524 gold_warning("entry symbol '%s' exists but is not defined", entry);
525 v = ssym->value();
526 }
527 else
528 {
529 // We couldn't find the entry symbol. See if we can parse it as
530 // a number. This supports, e.g., -e 0x1000.
531 char* endptr;
532 v = strtoull(entry, &endptr, 0);
533 if (*endptr != '\0')
534 {
535 if (should_issue_warning)
536 gold_warning("cannot find entry symbol '%s'", entry);
537 v = 0;
538 }
539 }
540
541 return v;
542}
543
20e6d0d6
DK
544// Compute the current data size.
545
546off_t
547Output_file_header::do_size() const
548{
549 const int size = parameters->target().get_size();
550 if (size == 32)
551 return elfcpp::Elf_sizes<32>::ehdr_size;
552 else if (size == 64)
553 return elfcpp::Elf_sizes<64>::ehdr_size;
554 else
555 gold_unreachable();
556}
557
dbe717ef
ILT
558// Output_data_const methods.
559
560void
a3ad94ed 561Output_data_const::do_write(Output_file* of)
dbe717ef 562{
a3ad94ed
ILT
563 of->write(this->offset(), this->data_.data(), this->data_.size());
564}
565
566// Output_data_const_buffer methods.
567
568void
569Output_data_const_buffer::do_write(Output_file* of)
570{
571 of->write(this->offset(), this->p_, this->data_size());
dbe717ef
ILT
572}
573
574// Output_section_data methods.
575
16649710
ILT
576// Record the output section, and set the entry size and such.
577
578void
579Output_section_data::set_output_section(Output_section* os)
580{
581 gold_assert(this->output_section_ == NULL);
582 this->output_section_ = os;
583 this->do_adjust_output_section(os);
584}
585
586// Return the section index of the output section.
587
dbe717ef
ILT
588unsigned int
589Output_section_data::do_out_shndx() const
590{
a3ad94ed 591 gold_assert(this->output_section_ != NULL);
dbe717ef
ILT
592 return this->output_section_->out_shndx();
593}
594
759b1a24
ILT
595// Set the alignment, which means we may need to update the alignment
596// of the output section.
597
598void
599Output_section_data::set_addralign(uint64_t addralign)
600{
601 this->addralign_ = addralign;
602 if (this->output_section_ != NULL
603 && this->output_section_->addralign() < addralign)
604 this->output_section_->set_addralign(addralign);
605}
606
a3ad94ed
ILT
607// Output_data_strtab methods.
608
27bc2bce 609// Set the final data size.
a3ad94ed
ILT
610
611void
27bc2bce 612Output_data_strtab::set_final_data_size()
a3ad94ed
ILT
613{
614 this->strtab_->set_string_offsets();
615 this->set_data_size(this->strtab_->get_strtab_size());
616}
617
618// Write out a string table.
619
620void
621Output_data_strtab::do_write(Output_file* of)
622{
623 this->strtab_->write(of, this->offset());
624}
625
c06b7b0b
ILT
626// Output_reloc methods.
627
7bf1f802
ILT
628// A reloc against a global symbol.
629
630template<bool dynamic, int size, bool big_endian>
631Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
632 Symbol* gsym,
633 unsigned int type,
634 Output_data* od,
e8c846c3
ILT
635 Address address,
636 bool is_relative)
7bf1f802 637 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
dceae3c1 638 is_relative_(is_relative), is_section_symbol_(false), shndx_(INVALID_CODE)
7bf1f802 639{
dceae3c1
ILT
640 // this->type_ is a bitfield; make sure TYPE fits.
641 gold_assert(this->type_ == type);
7bf1f802
ILT
642 this->u1_.gsym = gsym;
643 this->u2_.od = od;
dceae3c1
ILT
644 if (dynamic)
645 this->set_needs_dynsym_index();
7bf1f802
ILT
646}
647
648template<bool dynamic, int size, bool big_endian>
649Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
650 Symbol* gsym,
651 unsigned int type,
ef9beddf 652 Sized_relobj<size, big_endian>* relobj,
7bf1f802 653 unsigned int shndx,
e8c846c3
ILT
654 Address address,
655 bool is_relative)
7bf1f802 656 : address_(address), local_sym_index_(GSYM_CODE), type_(type),
dceae3c1 657 is_relative_(is_relative), is_section_symbol_(false), shndx_(shndx)
7bf1f802
ILT
658{
659 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
660 // this->type_ is a bitfield; make sure TYPE fits.
661 gold_assert(this->type_ == type);
7bf1f802
ILT
662 this->u1_.gsym = gsym;
663 this->u2_.relobj = relobj;
dceae3c1
ILT
664 if (dynamic)
665 this->set_needs_dynsym_index();
7bf1f802
ILT
666}
667
668// A reloc against a local symbol.
669
670template<bool dynamic, int size, bool big_endian>
671Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
672 Sized_relobj<size, big_endian>* relobj,
673 unsigned int local_sym_index,
674 unsigned int type,
675 Output_data* od,
e8c846c3 676 Address address,
dceae3c1
ILT
677 bool is_relative,
678 bool is_section_symbol)
7bf1f802 679 : address_(address), local_sym_index_(local_sym_index), type_(type),
dceae3c1
ILT
680 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
681 shndx_(INVALID_CODE)
7bf1f802
ILT
682{
683 gold_assert(local_sym_index != GSYM_CODE
684 && local_sym_index != INVALID_CODE);
dceae3c1
ILT
685 // this->type_ is a bitfield; make sure TYPE fits.
686 gold_assert(this->type_ == type);
7bf1f802
ILT
687 this->u1_.relobj = relobj;
688 this->u2_.od = od;
dceae3c1
ILT
689 if (dynamic)
690 this->set_needs_dynsym_index();
7bf1f802
ILT
691}
692
693template<bool dynamic, int size, bool big_endian>
694Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
695 Sized_relobj<size, big_endian>* relobj,
696 unsigned int local_sym_index,
697 unsigned int type,
698 unsigned int shndx,
e8c846c3 699 Address address,
dceae3c1
ILT
700 bool is_relative,
701 bool is_section_symbol)
7bf1f802 702 : address_(address), local_sym_index_(local_sym_index), type_(type),
dceae3c1
ILT
703 is_relative_(is_relative), is_section_symbol_(is_section_symbol),
704 shndx_(shndx)
7bf1f802
ILT
705{
706 gold_assert(local_sym_index != GSYM_CODE
707 && local_sym_index != INVALID_CODE);
708 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
709 // this->type_ is a bitfield; make sure TYPE fits.
710 gold_assert(this->type_ == type);
7bf1f802
ILT
711 this->u1_.relobj = relobj;
712 this->u2_.relobj = relobj;
dceae3c1
ILT
713 if (dynamic)
714 this->set_needs_dynsym_index();
7bf1f802
ILT
715}
716
717// A reloc against the STT_SECTION symbol of an output section.
718
719template<bool dynamic, int size, bool big_endian>
720Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
721 Output_section* os,
722 unsigned int type,
723 Output_data* od,
724 Address address)
725 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
dceae3c1 726 is_relative_(false), is_section_symbol_(true), shndx_(INVALID_CODE)
7bf1f802 727{
dceae3c1
ILT
728 // this->type_ is a bitfield; make sure TYPE fits.
729 gold_assert(this->type_ == type);
7bf1f802
ILT
730 this->u1_.os = os;
731 this->u2_.od = od;
732 if (dynamic)
dceae3c1
ILT
733 this->set_needs_dynsym_index();
734 else
735 os->set_needs_symtab_index();
7bf1f802
ILT
736}
737
738template<bool dynamic, int size, bool big_endian>
739Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::Output_reloc(
740 Output_section* os,
741 unsigned int type,
ef9beddf 742 Sized_relobj<size, big_endian>* relobj,
7bf1f802
ILT
743 unsigned int shndx,
744 Address address)
745 : address_(address), local_sym_index_(SECTION_CODE), type_(type),
dceae3c1 746 is_relative_(false), is_section_symbol_(true), shndx_(shndx)
7bf1f802
ILT
747{
748 gold_assert(shndx != INVALID_CODE);
dceae3c1
ILT
749 // this->type_ is a bitfield; make sure TYPE fits.
750 gold_assert(this->type_ == type);
7bf1f802
ILT
751 this->u1_.os = os;
752 this->u2_.relobj = relobj;
753 if (dynamic)
dceae3c1
ILT
754 this->set_needs_dynsym_index();
755 else
756 os->set_needs_symtab_index();
757}
758
759// Record that we need a dynamic symbol index for this relocation.
760
761template<bool dynamic, int size, bool big_endian>
762void
763Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
764set_needs_dynsym_index()
765{
766 if (this->is_relative_)
767 return;
768 switch (this->local_sym_index_)
769 {
770 case INVALID_CODE:
771 gold_unreachable();
772
773 case GSYM_CODE:
774 this->u1_.gsym->set_needs_dynsym_entry();
775 break;
776
777 case SECTION_CODE:
778 this->u1_.os->set_needs_dynsym_index();
779 break;
780
781 case 0:
782 break;
783
784 default:
785 {
786 const unsigned int lsi = this->local_sym_index_;
787 if (!this->is_section_symbol_)
788 this->u1_.relobj->set_needs_output_dynsym_entry(lsi);
789 else
ef9beddf 790 this->u1_.relobj->output_section(lsi)->set_needs_dynsym_index();
dceae3c1
ILT
791 }
792 break;
793 }
7bf1f802
ILT
794}
795
c06b7b0b
ILT
796// Get the symbol index of a relocation.
797
798template<bool dynamic, int size, bool big_endian>
799unsigned int
800Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
801 const
802{
803 unsigned int index;
804 switch (this->local_sym_index_)
805 {
806 case INVALID_CODE:
a3ad94ed 807 gold_unreachable();
c06b7b0b
ILT
808
809 case GSYM_CODE:
5a6f7e2d 810 if (this->u1_.gsym == NULL)
c06b7b0b
ILT
811 index = 0;
812 else if (dynamic)
5a6f7e2d 813 index = this->u1_.gsym->dynsym_index();
c06b7b0b 814 else
5a6f7e2d 815 index = this->u1_.gsym->symtab_index();
c06b7b0b
ILT
816 break;
817
818 case SECTION_CODE:
819 if (dynamic)
5a6f7e2d 820 index = this->u1_.os->dynsym_index();
c06b7b0b 821 else
5a6f7e2d 822 index = this->u1_.os->symtab_index();
c06b7b0b
ILT
823 break;
824
436ca963
ILT
825 case 0:
826 // Relocations without symbols use a symbol index of 0.
827 index = 0;
828 break;
829
c06b7b0b 830 default:
dceae3c1
ILT
831 {
832 const unsigned int lsi = this->local_sym_index_;
833 if (!this->is_section_symbol_)
834 {
835 if (dynamic)
836 index = this->u1_.relobj->dynsym_index(lsi);
837 else
838 index = this->u1_.relobj->symtab_index(lsi);
839 }
840 else
841 {
ef9beddf 842 Output_section* os = this->u1_.relobj->output_section(lsi);
dceae3c1
ILT
843 gold_assert(os != NULL);
844 if (dynamic)
845 index = os->dynsym_index();
846 else
847 index = os->symtab_index();
848 }
849 }
c06b7b0b
ILT
850 break;
851 }
a3ad94ed 852 gold_assert(index != -1U);
c06b7b0b
ILT
853 return index;
854}
855
624f8810
ILT
856// For a local section symbol, get the address of the offset ADDEND
857// within the input section.
dceae3c1
ILT
858
859template<bool dynamic, int size, bool big_endian>
ef9beddf 860typename elfcpp::Elf_types<size>::Elf_Addr
dceae3c1 861Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
624f8810 862 local_section_offset(Addend addend) const
dceae3c1 863{
624f8810
ILT
864 gold_assert(this->local_sym_index_ != GSYM_CODE
865 && this->local_sym_index_ != SECTION_CODE
866 && this->local_sym_index_ != INVALID_CODE
867 && this->is_section_symbol_);
dceae3c1 868 const unsigned int lsi = this->local_sym_index_;
ef9beddf 869 Output_section* os = this->u1_.relobj->output_section(lsi);
624f8810 870 gold_assert(os != NULL);
ef9beddf 871 Address offset = this->u1_.relobj->get_output_section_offset(lsi);
eff45813 872 if (offset != invalid_address)
624f8810
ILT
873 return offset + addend;
874 // This is a merge section.
875 offset = os->output_address(this->u1_.relobj, lsi, addend);
eff45813 876 gold_assert(offset != invalid_address);
dceae3c1
ILT
877 return offset;
878}
879
d98bc257 880// Get the output address of a relocation.
c06b7b0b
ILT
881
882template<bool dynamic, int size, bool big_endian>
a984ee1d 883typename elfcpp::Elf_types<size>::Elf_Addr
d98bc257 884Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_address() const
c06b7b0b 885{
a3ad94ed 886 Address address = this->address_;
5a6f7e2d
ILT
887 if (this->shndx_ != INVALID_CODE)
888 {
ef9beddf 889 Output_section* os = this->u2_.relobj->output_section(this->shndx_);
5a6f7e2d 890 gold_assert(os != NULL);
ef9beddf 891 Address off = this->u2_.relobj->get_output_section_offset(this->shndx_);
eff45813 892 if (off != invalid_address)
730cdc88
ILT
893 address += os->address() + off;
894 else
895 {
896 address = os->output_address(this->u2_.relobj, this->shndx_,
897 address);
eff45813 898 gold_assert(address != invalid_address);
730cdc88 899 }
5a6f7e2d
ILT
900 }
901 else if (this->u2_.od != NULL)
902 address += this->u2_.od->address();
d98bc257
ILT
903 return address;
904}
905
906// Write out the offset and info fields of a Rel or Rela relocation
907// entry.
908
909template<bool dynamic, int size, bool big_endian>
910template<typename Write_rel>
911void
912Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
913 Write_rel* wr) const
914{
915 wr->put_r_offset(this->get_address());
e8c846c3
ILT
916 unsigned int sym_index = this->is_relative_ ? 0 : this->get_symbol_index();
917 wr->put_r_info(elfcpp::elf_r_info<size>(sym_index, this->type_));
c06b7b0b
ILT
918}
919
920// Write out a Rel relocation.
921
922template<bool dynamic, int size, bool big_endian>
923void
924Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
925 unsigned char* pov) const
926{
927 elfcpp::Rel_write<size, big_endian> orel(pov);
928 this->write_rel(&orel);
929}
930
e8c846c3
ILT
931// Get the value of the symbol referred to by a Rel relocation.
932
933template<bool dynamic, int size, bool big_endian>
934typename elfcpp::Elf_types<size>::Elf_Addr
d1f003c6 935Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::symbol_value(
624f8810 936 Addend addend) const
e8c846c3
ILT
937{
938 if (this->local_sym_index_ == GSYM_CODE)
939 {
940 const Sized_symbol<size>* sym;
941 sym = static_cast<const Sized_symbol<size>*>(this->u1_.gsym);
d1f003c6 942 return sym->value() + addend;
e8c846c3
ILT
943 }
944 gold_assert(this->local_sym_index_ != SECTION_CODE
d1f003c6
ILT
945 && this->local_sym_index_ != INVALID_CODE
946 && !this->is_section_symbol_);
947 const unsigned int lsi = this->local_sym_index_;
948 const Symbol_value<size>* symval = this->u1_.relobj->local_symbol(lsi);
949 return symval->value(this->u1_.relobj, addend);
e8c846c3
ILT
950}
951
d98bc257
ILT
952// Reloc comparison. This function sorts the dynamic relocs for the
953// benefit of the dynamic linker. First we sort all relative relocs
954// to the front. Among relative relocs, we sort by output address.
955// Among non-relative relocs, we sort by symbol index, then by output
956// address.
957
958template<bool dynamic, int size, bool big_endian>
959int
960Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::
961 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
962 const
963{
964 if (this->is_relative_)
965 {
966 if (!r2.is_relative_)
967 return -1;
968 // Otherwise sort by reloc address below.
969 }
970 else if (r2.is_relative_)
971 return 1;
972 else
973 {
974 unsigned int sym1 = this->get_symbol_index();
975 unsigned int sym2 = r2.get_symbol_index();
976 if (sym1 < sym2)
977 return -1;
978 else if (sym1 > sym2)
979 return 1;
980 // Otherwise sort by reloc address.
981 }
982
983 section_offset_type addr1 = this->get_address();
984 section_offset_type addr2 = r2.get_address();
985 if (addr1 < addr2)
986 return -1;
987 else if (addr1 > addr2)
988 return 1;
989
990 // Final tie breaker, in order to generate the same output on any
991 // host: reloc type.
992 unsigned int type1 = this->type_;
993 unsigned int type2 = r2.type_;
994 if (type1 < type2)
995 return -1;
996 else if (type1 > type2)
997 return 1;
998
999 // These relocs appear to be exactly the same.
1000 return 0;
1001}
1002
c06b7b0b
ILT
1003// Write out a Rela relocation.
1004
1005template<bool dynamic, int size, bool big_endian>
1006void
1007Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
1008 unsigned char* pov) const
1009{
1010 elfcpp::Rela_write<size, big_endian> orel(pov);
1011 this->rel_.write_rel(&orel);
e8c846c3 1012 Addend addend = this->addend_;
dceae3c1 1013 if (this->rel_.is_relative())
d1f003c6
ILT
1014 addend = this->rel_.symbol_value(addend);
1015 else if (this->rel_.is_local_section_symbol())
624f8810 1016 addend = this->rel_.local_section_offset(addend);
e8c846c3 1017 orel.put_r_addend(addend);
c06b7b0b
ILT
1018}
1019
1020// Output_data_reloc_base methods.
1021
16649710
ILT
1022// Adjust the output section.
1023
1024template<int sh_type, bool dynamic, int size, bool big_endian>
1025void
1026Output_data_reloc_base<sh_type, dynamic, size, big_endian>
1027 ::do_adjust_output_section(Output_section* os)
1028{
1029 if (sh_type == elfcpp::SHT_REL)
1030 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
1031 else if (sh_type == elfcpp::SHT_RELA)
1032 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
1033 else
1034 gold_unreachable();
1035 if (dynamic)
1036 os->set_should_link_to_dynsym();
1037 else
1038 os->set_should_link_to_symtab();
1039}
1040
c06b7b0b
ILT
1041// Write out relocation data.
1042
1043template<int sh_type, bool dynamic, int size, bool big_endian>
1044void
1045Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
1046 Output_file* of)
1047{
1048 const off_t off = this->offset();
1049 const off_t oview_size = this->data_size();
1050 unsigned char* const oview = of->get_output_view(off, oview_size);
1051
d98bc257
ILT
1052 if (this->sort_relocs_)
1053 {
1054 gold_assert(dynamic);
1055 std::sort(this->relocs_.begin(), this->relocs_.end(),
1056 Sort_relocs_comparison());
1057 }
1058
c06b7b0b
ILT
1059 unsigned char* pov = oview;
1060 for (typename Relocs::const_iterator p = this->relocs_.begin();
1061 p != this->relocs_.end();
1062 ++p)
1063 {
1064 p->write(pov);
1065 pov += reloc_size;
1066 }
1067
a3ad94ed 1068 gold_assert(pov - oview == oview_size);
c06b7b0b
ILT
1069
1070 of->write_output_view(off, oview_size, oview);
1071
1072 // We no longer need the relocation entries.
1073 this->relocs_.clear();
1074}
1075
6a74a719
ILT
1076// Class Output_relocatable_relocs.
1077
1078template<int sh_type, int size, bool big_endian>
1079void
1080Output_relocatable_relocs<sh_type, size, big_endian>::set_final_data_size()
1081{
1082 this->set_data_size(this->rr_->output_reloc_count()
1083 * Reloc_types<sh_type, size, big_endian>::reloc_size);
1084}
1085
1086// class Output_data_group.
1087
1088template<int size, bool big_endian>
1089Output_data_group<size, big_endian>::Output_data_group(
1090 Sized_relobj<size, big_endian>* relobj,
1091 section_size_type entry_count,
8825ac63
ILT
1092 elfcpp::Elf_Word flags,
1093 std::vector<unsigned int>* input_shndxes)
20e6d0d6 1094 : Output_section_data(entry_count * 4, 4, false),
8825ac63
ILT
1095 relobj_(relobj),
1096 flags_(flags)
6a74a719 1097{
8825ac63 1098 this->input_shndxes_.swap(*input_shndxes);
6a74a719
ILT
1099}
1100
1101// Write out the section group, which means translating the section
1102// indexes to apply to the output file.
1103
1104template<int size, bool big_endian>
1105void
1106Output_data_group<size, big_endian>::do_write(Output_file* of)
1107{
1108 const off_t off = this->offset();
1109 const section_size_type oview_size =
1110 convert_to_section_size_type(this->data_size());
1111 unsigned char* const oview = of->get_output_view(off, oview_size);
1112
1113 elfcpp::Elf_Word* contents = reinterpret_cast<elfcpp::Elf_Word*>(oview);
1114 elfcpp::Swap<32, big_endian>::writeval(contents, this->flags_);
1115 ++contents;
1116
1117 for (std::vector<unsigned int>::const_iterator p =
8825ac63
ILT
1118 this->input_shndxes_.begin();
1119 p != this->input_shndxes_.end();
6a74a719
ILT
1120 ++p, ++contents)
1121 {
ef9beddf 1122 Output_section* os = this->relobj_->output_section(*p);
6a74a719
ILT
1123
1124 unsigned int output_shndx;
1125 if (os != NULL)
1126 output_shndx = os->out_shndx();
1127 else
1128 {
1129 this->relobj_->error(_("section group retained but "
1130 "group element discarded"));
1131 output_shndx = 0;
1132 }
1133
1134 elfcpp::Swap<32, big_endian>::writeval(contents, output_shndx);
1135 }
1136
1137 size_t wrote = reinterpret_cast<unsigned char*>(contents) - oview;
1138 gold_assert(wrote == oview_size);
1139
1140 of->write_output_view(off, oview_size, oview);
1141
1142 // We no longer need this information.
8825ac63 1143 this->input_shndxes_.clear();
6a74a719
ILT
1144}
1145
dbe717ef 1146// Output_data_got::Got_entry methods.
ead1e424
ILT
1147
1148// Write out the entry.
1149
1150template<int size, bool big_endian>
1151void
7e1edb90 1152Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
ead1e424
ILT
1153{
1154 Valtype val = 0;
1155
1156 switch (this->local_sym_index_)
1157 {
1158 case GSYM_CODE:
1159 {
e8c846c3
ILT
1160 // If the symbol is resolved locally, we need to write out the
1161 // link-time value, which will be relocated dynamically by a
1162 // RELATIVE relocation.
ead1e424 1163 Symbol* gsym = this->u_.gsym;
e8c846c3
ILT
1164 Sized_symbol<size>* sgsym;
1165 // This cast is a bit ugly. We don't want to put a
1166 // virtual method in Symbol, because we want Symbol to be
1167 // as small as possible.
1168 sgsym = static_cast<Sized_symbol<size>*>(gsym);
1169 val = sgsym->value();
ead1e424
ILT
1170 }
1171 break;
1172
1173 case CONSTANT_CODE:
1174 val = this->u_.constant;
1175 break;
1176
1177 default:
d1f003c6
ILT
1178 {
1179 const unsigned int lsi = this->local_sym_index_;
1180 const Symbol_value<size>* symval = this->u_.object->local_symbol(lsi);
1181 val = symval->value(this->u_.object, 0);
1182 }
e727fa71 1183 break;
ead1e424
ILT
1184 }
1185
a3ad94ed 1186 elfcpp::Swap<size, big_endian>::writeval(pov, val);
ead1e424
ILT
1187}
1188
dbe717ef 1189// Output_data_got methods.
ead1e424 1190
dbe717ef
ILT
1191// Add an entry for a global symbol to the GOT. This returns true if
1192// this is a new GOT entry, false if the symbol already had a GOT
1193// entry.
1194
1195template<int size, bool big_endian>
1196bool
0a65a3a7
CC
1197Output_data_got<size, big_endian>::add_global(
1198 Symbol* gsym,
1199 unsigned int got_type)
ead1e424 1200{
0a65a3a7 1201 if (gsym->has_got_offset(got_type))
dbe717ef 1202 return false;
ead1e424 1203
dbe717ef
ILT
1204 this->entries_.push_back(Got_entry(gsym));
1205 this->set_got_size();
0a65a3a7 1206 gsym->set_got_offset(got_type, this->last_got_offset());
dbe717ef
ILT
1207 return true;
1208}
ead1e424 1209
7bf1f802
ILT
1210// Add an entry for a global symbol to the GOT, and add a dynamic
1211// relocation of type R_TYPE for the GOT entry.
1212template<int size, bool big_endian>
1213void
1214Output_data_got<size, big_endian>::add_global_with_rel(
1215 Symbol* gsym,
0a65a3a7 1216 unsigned int got_type,
7bf1f802
ILT
1217 Rel_dyn* rel_dyn,
1218 unsigned int r_type)
1219{
0a65a3a7 1220 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1221 return;
1222
1223 this->entries_.push_back(Got_entry());
1224 this->set_got_size();
1225 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1226 gsym->set_got_offset(got_type, got_offset);
7bf1f802
ILT
1227 rel_dyn->add_global(gsym, r_type, this, got_offset);
1228}
1229
1230template<int size, bool big_endian>
1231void
1232Output_data_got<size, big_endian>::add_global_with_rela(
1233 Symbol* gsym,
0a65a3a7 1234 unsigned int got_type,
7bf1f802
ILT
1235 Rela_dyn* rela_dyn,
1236 unsigned int r_type)
1237{
0a65a3a7 1238 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1239 return;
1240
1241 this->entries_.push_back(Got_entry());
1242 this->set_got_size();
1243 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1244 gsym->set_got_offset(got_type, got_offset);
7bf1f802
ILT
1245 rela_dyn->add_global(gsym, r_type, this, got_offset, 0);
1246}
1247
0a65a3a7
CC
1248// Add a pair of entries for a global symbol to the GOT, and add
1249// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1250// If R_TYPE_2 == 0, add the second entry with no relocation.
7bf1f802
ILT
1251template<int size, bool big_endian>
1252void
0a65a3a7
CC
1253Output_data_got<size, big_endian>::add_global_pair_with_rel(
1254 Symbol* gsym,
1255 unsigned int got_type,
7bf1f802 1256 Rel_dyn* rel_dyn,
0a65a3a7
CC
1257 unsigned int r_type_1,
1258 unsigned int r_type_2)
7bf1f802 1259{
0a65a3a7 1260 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1261 return;
1262
1263 this->entries_.push_back(Got_entry());
7bf1f802 1264 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1265 gsym->set_got_offset(got_type, got_offset);
1266 rel_dyn->add_global(gsym, r_type_1, this, got_offset);
1267
1268 this->entries_.push_back(Got_entry());
1269 if (r_type_2 != 0)
1270 {
1271 got_offset = this->last_got_offset();
1272 rel_dyn->add_global(gsym, r_type_2, this, got_offset);
1273 }
1274
1275 this->set_got_size();
7bf1f802
ILT
1276}
1277
1278template<int size, bool big_endian>
1279void
0a65a3a7
CC
1280Output_data_got<size, big_endian>::add_global_pair_with_rela(
1281 Symbol* gsym,
1282 unsigned int got_type,
7bf1f802 1283 Rela_dyn* rela_dyn,
0a65a3a7
CC
1284 unsigned int r_type_1,
1285 unsigned int r_type_2)
7bf1f802 1286{
0a65a3a7 1287 if (gsym->has_got_offset(got_type))
7bf1f802
ILT
1288 return;
1289
1290 this->entries_.push_back(Got_entry());
7bf1f802 1291 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1292 gsym->set_got_offset(got_type, got_offset);
1293 rela_dyn->add_global(gsym, r_type_1, this, got_offset, 0);
1294
1295 this->entries_.push_back(Got_entry());
1296 if (r_type_2 != 0)
1297 {
1298 got_offset = this->last_got_offset();
1299 rela_dyn->add_global(gsym, r_type_2, this, got_offset, 0);
1300 }
1301
1302 this->set_got_size();
7bf1f802
ILT
1303}
1304
0a65a3a7
CC
1305// Add an entry for a local symbol to the GOT. This returns true if
1306// this is a new GOT entry, false if the symbol already has a GOT
1307// entry.
07f397ab
ILT
1308
1309template<int size, bool big_endian>
1310bool
0a65a3a7
CC
1311Output_data_got<size, big_endian>::add_local(
1312 Sized_relobj<size, big_endian>* object,
1313 unsigned int symndx,
1314 unsigned int got_type)
07f397ab 1315{
0a65a3a7 1316 if (object->local_has_got_offset(symndx, got_type))
07f397ab
ILT
1317 return false;
1318
0a65a3a7 1319 this->entries_.push_back(Got_entry(object, symndx));
07f397ab 1320 this->set_got_size();
0a65a3a7 1321 object->set_local_got_offset(symndx, got_type, this->last_got_offset());
07f397ab
ILT
1322 return true;
1323}
1324
0a65a3a7
CC
1325// Add an entry for a local symbol to the GOT, and add a dynamic
1326// relocation of type R_TYPE for the GOT entry.
7bf1f802
ILT
1327template<int size, bool big_endian>
1328void
0a65a3a7
CC
1329Output_data_got<size, big_endian>::add_local_with_rel(
1330 Sized_relobj<size, big_endian>* object,
1331 unsigned int symndx,
1332 unsigned int got_type,
7bf1f802
ILT
1333 Rel_dyn* rel_dyn,
1334 unsigned int r_type)
1335{
0a65a3a7 1336 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1337 return;
1338
1339 this->entries_.push_back(Got_entry());
1340 this->set_got_size();
1341 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1342 object->set_local_got_offset(symndx, got_type, got_offset);
1343 rel_dyn->add_local(object, symndx, r_type, this, got_offset);
7bf1f802
ILT
1344}
1345
1346template<int size, bool big_endian>
1347void
0a65a3a7
CC
1348Output_data_got<size, big_endian>::add_local_with_rela(
1349 Sized_relobj<size, big_endian>* object,
1350 unsigned int symndx,
1351 unsigned int got_type,
7bf1f802
ILT
1352 Rela_dyn* rela_dyn,
1353 unsigned int r_type)
1354{
0a65a3a7 1355 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1356 return;
1357
1358 this->entries_.push_back(Got_entry());
1359 this->set_got_size();
1360 unsigned int got_offset = this->last_got_offset();
0a65a3a7
CC
1361 object->set_local_got_offset(symndx, got_type, got_offset);
1362 rela_dyn->add_local(object, symndx, r_type, this, got_offset, 0);
07f397ab
ILT
1363}
1364
0a65a3a7
CC
1365// Add a pair of entries for a local symbol to the GOT, and add
1366// dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1367// If R_TYPE_2 == 0, add the second entry with no relocation.
7bf1f802
ILT
1368template<int size, bool big_endian>
1369void
0a65a3a7 1370Output_data_got<size, big_endian>::add_local_pair_with_rel(
7bf1f802
ILT
1371 Sized_relobj<size, big_endian>* object,
1372 unsigned int symndx,
1373 unsigned int shndx,
0a65a3a7 1374 unsigned int got_type,
7bf1f802 1375 Rel_dyn* rel_dyn,
0a65a3a7
CC
1376 unsigned int r_type_1,
1377 unsigned int r_type_2)
7bf1f802 1378{
0a65a3a7 1379 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1380 return;
1381
1382 this->entries_.push_back(Got_entry());
1383 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1384 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1385 Output_section* os = object->output_section(shndx);
0a65a3a7 1386 rel_dyn->add_output_section(os, r_type_1, this, got_offset);
7bf1f802 1387
0a65a3a7
CC
1388 this->entries_.push_back(Got_entry(object, symndx));
1389 if (r_type_2 != 0)
1390 {
1391 got_offset = this->last_got_offset();
1392 rel_dyn->add_output_section(os, r_type_2, this, got_offset);
1393 }
7bf1f802
ILT
1394
1395 this->set_got_size();
1396}
1397
1398template<int size, bool big_endian>
1399void
0a65a3a7 1400Output_data_got<size, big_endian>::add_local_pair_with_rela(
7bf1f802
ILT
1401 Sized_relobj<size, big_endian>* object,
1402 unsigned int symndx,
1403 unsigned int shndx,
0a65a3a7 1404 unsigned int got_type,
7bf1f802 1405 Rela_dyn* rela_dyn,
0a65a3a7
CC
1406 unsigned int r_type_1,
1407 unsigned int r_type_2)
7bf1f802 1408{
0a65a3a7 1409 if (object->local_has_got_offset(symndx, got_type))
7bf1f802
ILT
1410 return;
1411
1412 this->entries_.push_back(Got_entry());
1413 unsigned int got_offset = this->last_got_offset();
0a65a3a7 1414 object->set_local_got_offset(symndx, got_type, got_offset);
ef9beddf 1415 Output_section* os = object->output_section(shndx);
0a65a3a7 1416 rela_dyn->add_output_section(os, r_type_1, this, got_offset, 0);
7bf1f802 1417
0a65a3a7
CC
1418 this->entries_.push_back(Got_entry(object, symndx));
1419 if (r_type_2 != 0)
1420 {
1421 got_offset = this->last_got_offset();
1422 rela_dyn->add_output_section(os, r_type_2, this, got_offset, 0);
1423 }
7bf1f802
ILT
1424
1425 this->set_got_size();
1426}
1427
ead1e424
ILT
1428// Write out the GOT.
1429
1430template<int size, bool big_endian>
1431void
dbe717ef 1432Output_data_got<size, big_endian>::do_write(Output_file* of)
ead1e424
ILT
1433{
1434 const int add = size / 8;
1435
1436 const off_t off = this->offset();
c06b7b0b 1437 const off_t oview_size = this->data_size();
ead1e424
ILT
1438 unsigned char* const oview = of->get_output_view(off, oview_size);
1439
1440 unsigned char* pov = oview;
1441 for (typename Got_entries::const_iterator p = this->entries_.begin();
1442 p != this->entries_.end();
1443 ++p)
1444 {
7e1edb90 1445 p->write(pov);
ead1e424
ILT
1446 pov += add;
1447 }
1448
a3ad94ed 1449 gold_assert(pov - oview == oview_size);
c06b7b0b 1450
ead1e424
ILT
1451 of->write_output_view(off, oview_size, oview);
1452
1453 // We no longer need the GOT entries.
1454 this->entries_.clear();
1455}
1456
a3ad94ed
ILT
1457// Output_data_dynamic::Dynamic_entry methods.
1458
1459// Write out the entry.
1460
1461template<int size, bool big_endian>
1462void
1463Output_data_dynamic::Dynamic_entry::write(
1464 unsigned char* pov,
7d1a9ebb 1465 const Stringpool* pool) const
a3ad94ed
ILT
1466{
1467 typename elfcpp::Elf_types<size>::Elf_WXword val;
c2b45e22 1468 switch (this->offset_)
a3ad94ed
ILT
1469 {
1470 case DYNAMIC_NUMBER:
1471 val = this->u_.val;
1472 break;
1473
a3ad94ed 1474 case DYNAMIC_SECTION_SIZE:
16649710 1475 val = this->u_.od->data_size();
a3ad94ed
ILT
1476 break;
1477
1478 case DYNAMIC_SYMBOL:
1479 {
16649710
ILT
1480 const Sized_symbol<size>* s =
1481 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
1482 val = s->value();
1483 }
1484 break;
1485
1486 case DYNAMIC_STRING:
1487 val = pool->get_offset(this->u_.str);
1488 break;
1489
1490 default:
c2b45e22
CC
1491 val = this->u_.od->address() + this->offset_;
1492 break;
a3ad94ed
ILT
1493 }
1494
1495 elfcpp::Dyn_write<size, big_endian> dw(pov);
1496 dw.put_d_tag(this->tag_);
1497 dw.put_d_val(val);
1498}
1499
1500// Output_data_dynamic methods.
1501
16649710
ILT
1502// Adjust the output section to set the entry size.
1503
1504void
1505Output_data_dynamic::do_adjust_output_section(Output_section* os)
1506{
8851ecca 1507 if (parameters->target().get_size() == 32)
16649710 1508 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
8851ecca 1509 else if (parameters->target().get_size() == 64)
16649710
ILT
1510 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
1511 else
1512 gold_unreachable();
1513}
1514
a3ad94ed
ILT
1515// Set the final data size.
1516
1517void
27bc2bce 1518Output_data_dynamic::set_final_data_size()
a3ad94ed 1519{
20e6d0d6
DK
1520 // Add the terminating entry if it hasn't been added.
1521 // Because of relaxation, we can run this multiple times.
1522 if (this->entries_.empty()
1523 || this->entries_.rbegin()->tag() != elfcpp::DT_NULL)
1524 this->add_constant(elfcpp::DT_NULL, 0);
a3ad94ed
ILT
1525
1526 int dyn_size;
8851ecca 1527 if (parameters->target().get_size() == 32)
a3ad94ed 1528 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
8851ecca 1529 else if (parameters->target().get_size() == 64)
a3ad94ed
ILT
1530 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
1531 else
1532 gold_unreachable();
1533 this->set_data_size(this->entries_.size() * dyn_size);
1534}
1535
1536// Write out the dynamic entries.
1537
1538void
1539Output_data_dynamic::do_write(Output_file* of)
1540{
8851ecca 1541 switch (parameters->size_and_endianness())
a3ad94ed 1542 {
9025d29d 1543#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
1544 case Parameters::TARGET_32_LITTLE:
1545 this->sized_write<32, false>(of);
1546 break;
9025d29d 1547#endif
8851ecca
ILT
1548#ifdef HAVE_TARGET_32_BIG
1549 case Parameters::TARGET_32_BIG:
1550 this->sized_write<32, true>(of);
1551 break;
9025d29d 1552#endif
9025d29d 1553#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
1554 case Parameters::TARGET_64_LITTLE:
1555 this->sized_write<64, false>(of);
1556 break;
9025d29d 1557#endif
8851ecca
ILT
1558#ifdef HAVE_TARGET_64_BIG
1559 case Parameters::TARGET_64_BIG:
1560 this->sized_write<64, true>(of);
1561 break;
1562#endif
1563 default:
1564 gold_unreachable();
a3ad94ed 1565 }
a3ad94ed
ILT
1566}
1567
1568template<int size, bool big_endian>
1569void
1570Output_data_dynamic::sized_write(Output_file* of)
1571{
1572 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
1573
1574 const off_t offset = this->offset();
1575 const off_t oview_size = this->data_size();
1576 unsigned char* const oview = of->get_output_view(offset, oview_size);
1577
1578 unsigned char* pov = oview;
1579 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
1580 p != this->entries_.end();
1581 ++p)
1582 {
7d1a9ebb 1583 p->write<size, big_endian>(pov, this->pool_);
a3ad94ed
ILT
1584 pov += dyn_size;
1585 }
1586
1587 gold_assert(pov - oview == oview_size);
1588
1589 of->write_output_view(offset, oview_size, oview);
1590
1591 // We no longer need the dynamic entries.
1592 this->entries_.clear();
1593}
1594
d491d34e
ILT
1595// Class Output_symtab_xindex.
1596
1597void
1598Output_symtab_xindex::do_write(Output_file* of)
1599{
1600 const off_t offset = this->offset();
1601 const off_t oview_size = this->data_size();
1602 unsigned char* const oview = of->get_output_view(offset, oview_size);
1603
1604 memset(oview, 0, oview_size);
1605
1606 if (parameters->target().is_big_endian())
1607 this->endian_do_write<true>(oview);
1608 else
1609 this->endian_do_write<false>(oview);
1610
1611 of->write_output_view(offset, oview_size, oview);
1612
1613 // We no longer need the data.
1614 this->entries_.clear();
1615}
1616
1617template<bool big_endian>
1618void
1619Output_symtab_xindex::endian_do_write(unsigned char* const oview)
1620{
1621 for (Xindex_entries::const_iterator p = this->entries_.begin();
1622 p != this->entries_.end();
1623 ++p)
20e6d0d6
DK
1624 {
1625 unsigned int symndx = p->first;
1626 gold_assert(symndx * 4 < this->data_size());
1627 elfcpp::Swap<32, big_endian>::writeval(oview + symndx * 4, p->second);
1628 }
d491d34e
ILT
1629}
1630
ead1e424
ILT
1631// Output_section::Input_section methods.
1632
1633// Return the data size. For an input section we store the size here.
1634// For an Output_section_data, we have to ask it for the size.
1635
1636off_t
1637Output_section::Input_section::data_size() const
1638{
1639 if (this->is_input_section())
b8e6aad9 1640 return this->u1_.data_size;
ead1e424 1641 else
b8e6aad9 1642 return this->u2_.posd->data_size();
ead1e424
ILT
1643}
1644
1645// Set the address and file offset.
1646
1647void
96803768
ILT
1648Output_section::Input_section::set_address_and_file_offset(
1649 uint64_t address,
1650 off_t file_offset,
1651 off_t section_file_offset)
ead1e424
ILT
1652{
1653 if (this->is_input_section())
96803768
ILT
1654 this->u2_.object->set_section_offset(this->shndx_,
1655 file_offset - section_file_offset);
ead1e424 1656 else
96803768
ILT
1657 this->u2_.posd->set_address_and_file_offset(address, file_offset);
1658}
1659
a445fddf
ILT
1660// Reset the address and file offset.
1661
1662void
1663Output_section::Input_section::reset_address_and_file_offset()
1664{
1665 if (!this->is_input_section())
1666 this->u2_.posd->reset_address_and_file_offset();
1667}
1668
96803768
ILT
1669// Finalize the data size.
1670
1671void
1672Output_section::Input_section::finalize_data_size()
1673{
1674 if (!this->is_input_section())
1675 this->u2_.posd->finalize_data_size();
b8e6aad9
ILT
1676}
1677
1e983657
ILT
1678// Try to turn an input offset into an output offset. We want to
1679// return the output offset relative to the start of this
1680// Input_section in the output section.
b8e6aad9 1681
8f00aeb8 1682inline bool
8383303e
ILT
1683Output_section::Input_section::output_offset(
1684 const Relobj* object,
1685 unsigned int shndx,
1686 section_offset_type offset,
1687 section_offset_type *poutput) const
b8e6aad9
ILT
1688{
1689 if (!this->is_input_section())
730cdc88 1690 return this->u2_.posd->output_offset(object, shndx, offset, poutput);
b8e6aad9
ILT
1691 else
1692 {
730cdc88 1693 if (this->shndx_ != shndx || this->u2_.object != object)
b8e6aad9 1694 return false;
1e983657 1695 *poutput = offset;
b8e6aad9
ILT
1696 return true;
1697 }
ead1e424
ILT
1698}
1699
a9a60db6
ILT
1700// Return whether this is the merge section for the input section
1701// SHNDX in OBJECT.
1702
1703inline bool
1704Output_section::Input_section::is_merge_section_for(const Relobj* object,
1705 unsigned int shndx) const
1706{
1707 if (this->is_input_section())
1708 return false;
1709 return this->u2_.posd->is_merge_section_for(object, shndx);
1710}
1711
ead1e424
ILT
1712// Write out the data. We don't have to do anything for an input
1713// section--they are handled via Object::relocate--but this is where
1714// we write out the data for an Output_section_data.
1715
1716void
1717Output_section::Input_section::write(Output_file* of)
1718{
1719 if (!this->is_input_section())
b8e6aad9 1720 this->u2_.posd->write(of);
ead1e424
ILT
1721}
1722
96803768
ILT
1723// Write the data to a buffer. As for write(), we don't have to do
1724// anything for an input section.
1725
1726void
1727Output_section::Input_section::write_to_buffer(unsigned char* buffer)
1728{
1729 if (!this->is_input_section())
1730 this->u2_.posd->write_to_buffer(buffer);
1731}
1732
7d9e3d98
ILT
1733// Print to a map file.
1734
1735void
1736Output_section::Input_section::print_to_mapfile(Mapfile* mapfile) const
1737{
1738 switch (this->shndx_)
1739 {
1740 case OUTPUT_SECTION_CODE:
1741 case MERGE_DATA_SECTION_CODE:
1742 case MERGE_STRING_SECTION_CODE:
1743 this->u2_.posd->print_to_mapfile(mapfile);
1744 break;
1745
20e6d0d6
DK
1746 case RELAXED_INPUT_SECTION_CODE:
1747 {
1748 Output_relaxed_input_section* relaxed_section =
1749 this->relaxed_input_section();
1750 mapfile->print_input_section(relaxed_section->relobj(),
1751 relaxed_section->shndx());
1752 }
1753 break;
7d9e3d98
ILT
1754 default:
1755 mapfile->print_input_section(this->u2_.object, this->shndx_);
1756 break;
1757 }
1758}
1759
a2fb1b05
ILT
1760// Output_section methods.
1761
1762// Construct an Output_section. NAME will point into a Stringpool.
1763
96803768 1764Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
b8e6aad9 1765 elfcpp::Elf_Xword flags)
96803768 1766 : name_(name),
a2fb1b05
ILT
1767 addralign_(0),
1768 entsize_(0),
a445fddf 1769 load_address_(0),
16649710 1770 link_section_(NULL),
a2fb1b05 1771 link_(0),
16649710 1772 info_section_(NULL),
6a74a719 1773 info_symndx_(NULL),
a2fb1b05
ILT
1774 info_(0),
1775 type_(type),
61ba1cf9 1776 flags_(flags),
91ea499d 1777 out_shndx_(-1U),
c06b7b0b
ILT
1778 symtab_index_(0),
1779 dynsym_index_(0),
ead1e424
ILT
1780 input_sections_(),
1781 first_input_offset_(0),
c51e6221 1782 fills_(),
96803768 1783 postprocessing_buffer_(NULL),
a3ad94ed 1784 needs_symtab_index_(false),
16649710
ILT
1785 needs_dynsym_index_(false),
1786 should_link_to_symtab_(false),
730cdc88 1787 should_link_to_dynsym_(false),
27bc2bce 1788 after_input_sections_(false),
7bf1f802 1789 requires_postprocessing_(false),
a445fddf
ILT
1790 found_in_sections_clause_(false),
1791 has_load_address_(false),
755ab8af 1792 info_uses_section_index_(false),
2fd32231
ILT
1793 may_sort_attached_input_sections_(false),
1794 must_sort_attached_input_sections_(false),
1795 attached_input_sections_are_sorted_(false),
9f1d377b
ILT
1796 is_relro_(false),
1797 is_relro_local_(false),
8a5e3e08
ILT
1798 is_small_section_(false),
1799 is_large_section_(false),
20e6d0d6 1800 tls_offset_(0),
c0a62865
DK
1801 checkpoint_(NULL),
1802 merge_section_map_(),
1803 merge_section_by_properties_map_(),
1804 relaxed_input_section_map_(),
1805 is_relaxed_input_section_map_valid_(true),
1806 generate_code_fills_at_write_(false)
a2fb1b05 1807{
27bc2bce
ILT
1808 // An unallocated section has no address. Forcing this means that
1809 // we don't need special treatment for symbols defined in debug
1810 // sections.
1811 if ((flags & elfcpp::SHF_ALLOC) == 0)
1812 this->set_address(0);
a2fb1b05
ILT
1813}
1814
54dc6425
ILT
1815Output_section::~Output_section()
1816{
20e6d0d6 1817 delete this->checkpoint_;
54dc6425
ILT
1818}
1819
16649710
ILT
1820// Set the entry size.
1821
1822void
1823Output_section::set_entsize(uint64_t v)
1824{
1825 if (this->entsize_ == 0)
1826 this->entsize_ = v;
1827 else
1828 gold_assert(this->entsize_ == v);
1829}
1830
ead1e424 1831// Add the input section SHNDX, with header SHDR, named SECNAME, in
730cdc88
ILT
1832// OBJECT, to the Output_section. RELOC_SHNDX is the index of a
1833// relocation section which applies to this section, or 0 if none, or
1834// -1U if more than one. Return the offset of the input section
1835// within the output section. Return -1 if the input section will
1836// receive special handling. In the normal case we don't always keep
1837// track of input sections for an Output_section. Instead, each
1838// Object keeps track of the Output_section for each of its input
a445fddf
ILT
1839// sections. However, if HAVE_SECTIONS_SCRIPT is true, we do keep
1840// track of input sections here; this is used when SECTIONS appears in
1841// a linker script.
a2fb1b05
ILT
1842
1843template<int size, bool big_endian>
1844off_t
730cdc88
ILT
1845Output_section::add_input_section(Sized_relobj<size, big_endian>* object,
1846 unsigned int shndx,
ead1e424 1847 const char* secname,
730cdc88 1848 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf
ILT
1849 unsigned int reloc_shndx,
1850 bool have_sections_script)
a2fb1b05
ILT
1851{
1852 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1853 if ((addralign & (addralign - 1)) != 0)
1854 {
75f2446e
ILT
1855 object->error(_("invalid alignment %lu for section \"%s\""),
1856 static_cast<unsigned long>(addralign), secname);
1857 addralign = 1;
a2fb1b05 1858 }
a2fb1b05
ILT
1859
1860 if (addralign > this->addralign_)
1861 this->addralign_ = addralign;
1862
44a43cf9 1863 typename elfcpp::Elf_types<size>::Elf_WXword sh_flags = shdr.get_sh_flags();
154e0e9a 1864 this->update_flags_for_input_section(sh_flags);
a445fddf 1865
4f833eee 1866 uint64_t entsize = shdr.get_sh_entsize();
44a43cf9
ILT
1867
1868 // .debug_str is a mergeable string section, but is not always so
1869 // marked by compilers. Mark manually here so we can optimize.
1870 if (strcmp(secname, ".debug_str") == 0)
4f833eee
ILT
1871 {
1872 sh_flags |= (elfcpp::SHF_MERGE | elfcpp::SHF_STRINGS);
1873 entsize = 1;
1874 }
44a43cf9 1875
b8e6aad9 1876 // If this is a SHF_MERGE section, we pass all the input sections to
730cdc88 1877 // a Output_data_merge. We don't try to handle relocations for such
e0b64032
ILT
1878 // a section. We don't try to handle empty merge sections--they
1879 // mess up the mappings, and are useless anyhow.
44a43cf9 1880 if ((sh_flags & elfcpp::SHF_MERGE) != 0
e0b64032
ILT
1881 && reloc_shndx == 0
1882 && shdr.get_sh_size() > 0)
b8e6aad9 1883 {
44a43cf9 1884 if (this->add_merge_input_section(object, shndx, sh_flags,
96803768 1885 entsize, addralign))
b8e6aad9
ILT
1886 {
1887 // Tell the relocation routines that they need to call the
730cdc88 1888 // output_offset method to determine the final address.
b8e6aad9
ILT
1889 return -1;
1890 }
1891 }
1892
27bc2bce 1893 off_t offset_in_section = this->current_data_size_for_child();
c51e6221
ILT
1894 off_t aligned_offset_in_section = align_address(offset_in_section,
1895 addralign);
1896
c0a62865
DK
1897 // Determine if we want to delay code-fill generation until the output
1898 // section is written. When the target is relaxing, we want to delay fill
1899 // generating to avoid adjusting them during relaxation.
1900 if (!this->generate_code_fills_at_write_
1901 && !have_sections_script
1902 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
1903 && parameters->target().has_code_fill()
1904 && parameters->target().may_relax())
1905 {
1906 gold_assert(this->fills_.empty());
1907 this->generate_code_fills_at_write_ = true;
1908 }
1909
c51e6221 1910 if (aligned_offset_in_section > offset_in_section
c0a62865 1911 && !this->generate_code_fills_at_write_
a445fddf 1912 && !have_sections_script
44a43cf9 1913 && (sh_flags & elfcpp::SHF_EXECINSTR) != 0
029ba973 1914 && parameters->target().has_code_fill())
c51e6221
ILT
1915 {
1916 // We need to add some fill data. Using fill_list_ when
1917 // possible is an optimization, since we will often have fill
1918 // sections without input sections.
1919 off_t fill_len = aligned_offset_in_section - offset_in_section;
1920 if (this->input_sections_.empty())
1921 this->fills_.push_back(Fill(offset_in_section, fill_len));
1922 else
1923 {
029ba973 1924 std::string fill_data(parameters->target().code_fill(fill_len));
c51e6221
ILT
1925 Output_data_const* odc = new Output_data_const(fill_data, 1);
1926 this->input_sections_.push_back(Input_section(odc));
1927 }
1928 }
1929
27bc2bce
ILT
1930 this->set_current_data_size_for_child(aligned_offset_in_section
1931 + shdr.get_sh_size());
a2fb1b05 1932
ead1e424 1933 // We need to keep track of this section if we are already keeping
2fd32231
ILT
1934 // track of sections, or if we are relaxing. Also, if this is a
1935 // section which requires sorting, or which may require sorting in
20e6d0d6 1936 // the future, we keep track of the sections.
2fd32231
ILT
1937 if (have_sections_script
1938 || !this->input_sections_.empty()
1939 || this->may_sort_attached_input_sections()
7d9e3d98 1940 || this->must_sort_attached_input_sections()
20e6d0d6 1941 || parameters->options().user_set_Map()
029ba973 1942 || parameters->target().may_relax())
ead1e424
ILT
1943 this->input_sections_.push_back(Input_section(object, shndx,
1944 shdr.get_sh_size(),
1945 addralign));
54dc6425 1946
c51e6221 1947 return aligned_offset_in_section;
61ba1cf9
ILT
1948}
1949
ead1e424
ILT
1950// Add arbitrary data to an output section.
1951
1952void
1953Output_section::add_output_section_data(Output_section_data* posd)
1954{
b8e6aad9
ILT
1955 Input_section inp(posd);
1956 this->add_output_section_data(&inp);
a445fddf
ILT
1957
1958 if (posd->is_data_size_valid())
1959 {
1960 off_t offset_in_section = this->current_data_size_for_child();
1961 off_t aligned_offset_in_section = align_address(offset_in_section,
1962 posd->addralign());
1963 this->set_current_data_size_for_child(aligned_offset_in_section
1964 + posd->data_size());
1965 }
b8e6aad9
ILT
1966}
1967
c0a62865
DK
1968// Add a relaxed input section.
1969
1970void
1971Output_section::add_relaxed_input_section(Output_relaxed_input_section* poris)
1972{
1973 Input_section inp(poris);
1974 this->add_output_section_data(&inp);
1975 if (this->is_relaxed_input_section_map_valid_)
1976 {
1977 Input_section_specifier iss(poris->relobj(), poris->shndx());
1978 this->relaxed_input_section_map_[iss] = poris;
1979 }
1980
1981 // For a relaxed section, we use the current data size. Linker scripts
1982 // get all the input sections, including relaxed one from an output
1983 // section and add them back to them same output section to compute the
1984 // output section size. If we do not account for sizes of relaxed input
1985 // sections, an output section would be incorrectly sized.
1986 off_t offset_in_section = this->current_data_size_for_child();
1987 off_t aligned_offset_in_section = align_address(offset_in_section,
1988 poris->addralign());
1989 this->set_current_data_size_for_child(aligned_offset_in_section
1990 + poris->current_data_size());
1991}
1992
b8e6aad9 1993// Add arbitrary data to an output section by Input_section.
c06b7b0b 1994
b8e6aad9
ILT
1995void
1996Output_section::add_output_section_data(Input_section* inp)
1997{
ead1e424 1998 if (this->input_sections_.empty())
27bc2bce 1999 this->first_input_offset_ = this->current_data_size_for_child();
c06b7b0b 2000
b8e6aad9 2001 this->input_sections_.push_back(*inp);
c06b7b0b 2002
b8e6aad9 2003 uint64_t addralign = inp->addralign();
ead1e424
ILT
2004 if (addralign > this->addralign_)
2005 this->addralign_ = addralign;
c06b7b0b 2006
b8e6aad9
ILT
2007 inp->set_output_section(this);
2008}
2009
2010// Add a merge section to an output section.
2011
2012void
2013Output_section::add_output_merge_section(Output_section_data* posd,
2014 bool is_string, uint64_t entsize)
2015{
2016 Input_section inp(posd, is_string, entsize);
2017 this->add_output_section_data(&inp);
2018}
2019
2020// Add an input section to a SHF_MERGE section.
2021
2022bool
2023Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
2024 uint64_t flags, uint64_t entsize,
96803768 2025 uint64_t addralign)
b8e6aad9 2026{
87f95776
ILT
2027 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
2028
2029 // We only merge strings if the alignment is not more than the
2030 // character size. This could be handled, but it's unusual.
2031 if (is_string && addralign > entsize)
b8e6aad9
ILT
2032 return false;
2033
20e6d0d6
DK
2034 // We cannot restore merged input section states.
2035 gold_assert(this->checkpoint_ == NULL);
2036
c0a62865
DK
2037 // Look up merge sections by required properties.
2038 Merge_section_properties msp(is_string, entsize, addralign);
2039 Merge_section_by_properties_map::const_iterator p =
2040 this->merge_section_by_properties_map_.find(msp);
2041 if (p != this->merge_section_by_properties_map_.end())
2042 {
2043 Output_merge_base* merge_section = p->second;
2044 merge_section->add_input_section(object, shndx);
2045 gold_assert(merge_section->is_string() == is_string
2046 && merge_section->entsize() == entsize
2047 && merge_section->addralign() == addralign);
2048
2049 // Link input section to found merge section.
2050 Input_section_specifier iss(object, shndx);
2051 this->merge_section_map_[iss] = merge_section;
2052 return true;
2053 }
b8e6aad9
ILT
2054
2055 // We handle the actual constant merging in Output_merge_data or
2056 // Output_merge_string_data.
c0a62865 2057 Output_merge_base* pomb;
9a0910c3 2058 if (!is_string)
c0a62865 2059 pomb = new Output_merge_data(entsize, addralign);
b8e6aad9
ILT
2060 else
2061 {
9a0910c3
ILT
2062 switch (entsize)
2063 {
2064 case 1:
c0a62865 2065 pomb = new Output_merge_string<char>(addralign);
9a0910c3
ILT
2066 break;
2067 case 2:
c0a62865 2068 pomb = new Output_merge_string<uint16_t>(addralign);
9a0910c3
ILT
2069 break;
2070 case 4:
c0a62865 2071 pomb = new Output_merge_string<uint32_t>(addralign);
9a0910c3
ILT
2072 break;
2073 default:
2074 return false;
2075 }
b8e6aad9
ILT
2076 }
2077
c0a62865
DK
2078 // Add new merge section to this output section and link merge section
2079 // properties to new merge section in map.
2080 this->add_output_merge_section(pomb, is_string, entsize);
2081 this->merge_section_by_properties_map_[msp] = pomb;
2082
2083 // Add input section to new merge section and link input section to new
2084 // merge section in map.
2085 pomb->add_input_section(object, shndx);
2086 Input_section_specifier iss(object, shndx);
2087 this->merge_section_map_[iss] = pomb;
9a0910c3 2088
b8e6aad9
ILT
2089 return true;
2090}
2091
c0a62865
DK
2092// Build a relaxation map to speed up relaxation of existing input sections.
2093// Look up to the first LIMIT elements in INPUT_SECTIONS.
2094
20e6d0d6 2095void
c0a62865
DK
2096Output_section::build_relaxation_map(
2097 const Input_section_list& input_sections,
2098 size_t limit,
2099 Relaxation_map* relaxation_map) const
20e6d0d6 2100{
c0a62865
DK
2101 for (size_t i = 0; i < limit; ++i)
2102 {
2103 const Input_section& is(input_sections[i]);
2104 if (is.is_input_section() || is.is_relaxed_input_section())
2105 {
2106 Input_section_specifier iss(is.relobj(), is.shndx());
2107 (*relaxation_map)[iss] = i;
2108 }
2109 }
2110}
2111
2112// Convert regular input sections in INPUT_SECTIONS into relaxed input
2113// sections in RELAXED_SECTIONS. MAP is a prebuilt map from input section
2114// specifier to indices of INPUT_SECTIONS.
20e6d0d6 2115
c0a62865
DK
2116void
2117Output_section::convert_input_sections_in_list_to_relaxed_sections(
2118 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
2119 const Relaxation_map& map,
2120 Input_section_list* input_sections)
2121{
2122 for (size_t i = 0; i < relaxed_sections.size(); ++i)
2123 {
2124 Output_relaxed_input_section* poris = relaxed_sections[i];
2125 Input_section_specifier iss(poris->relobj(), poris->shndx());
2126 Relaxation_map::const_iterator p = map.find(iss);
2127 gold_assert(p != map.end());
2128 gold_assert((*input_sections)[p->second].is_input_section());
2129 (*input_sections)[p->second] = Input_section(poris);
2130 }
2131}
2132
2133// Convert regular input sections into relaxed input sections. RELAXED_SECTIONS
2134// is a vector of pointers to Output_relaxed_input_section or its derived
2135// classes. The relaxed sections must correspond to existing input sections.
2136
2137void
2138Output_section::convert_input_sections_to_relaxed_sections(
2139 const std::vector<Output_relaxed_input_section*>& relaxed_sections)
2140{
029ba973 2141 gold_assert(parameters->target().may_relax());
20e6d0d6 2142
c0a62865
DK
2143 // We want to make sure that restore_states does not undo the effect of
2144 // this. If there is no checkpoint active, just search the current
2145 // input section list and replace the sections there. If there is
2146 // a checkpoint, also replace the sections there.
2147
2148 // By default, we look at the whole list.
2149 size_t limit = this->input_sections_.size();
2150
2151 if (this->checkpoint_ != NULL)
20e6d0d6 2152 {
c0a62865
DK
2153 // Replace input sections with relaxed input section in the saved
2154 // copy of the input section list.
2155 if (this->checkpoint_->input_sections_saved())
20e6d0d6 2156 {
c0a62865
DK
2157 Relaxation_map map;
2158 this->build_relaxation_map(
2159 *(this->checkpoint_->input_sections()),
2160 this->checkpoint_->input_sections()->size(),
2161 &map);
2162 this->convert_input_sections_in_list_to_relaxed_sections(
2163 relaxed_sections,
2164 map,
2165 this->checkpoint_->input_sections());
2166 }
2167 else
2168 {
2169 // We have not copied the input section list yet. Instead, just
2170 // look at the portion that would be saved.
2171 limit = this->checkpoint_->input_sections_size();
20e6d0d6 2172 }
20e6d0d6 2173 }
c0a62865
DK
2174
2175 // Convert input sections in input_section_list.
2176 Relaxation_map map;
2177 this->build_relaxation_map(this->input_sections_, limit, &map);
2178 this->convert_input_sections_in_list_to_relaxed_sections(
2179 relaxed_sections,
2180 map,
2181 &this->input_sections_);
20e6d0d6
DK
2182}
2183
9c547ec3
ILT
2184// Update the output section flags based on input section flags.
2185
2186void
2187Output_section::update_flags_for_input_section(elfcpp::Elf_Xword flags)
2188{
2189 // If we created the section with SHF_ALLOC clear, we set the
2190 // address. If we are now setting the SHF_ALLOC flag, we need to
2191 // undo that.
2192 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0
2193 && (flags & elfcpp::SHF_ALLOC) != 0)
2194 this->mark_address_invalid();
2195
2196 this->flags_ |= (flags
2197 & (elfcpp::SHF_WRITE
2198 | elfcpp::SHF_ALLOC
2199 | elfcpp::SHF_EXECINSTR));
2200}
2201
c0a62865
DK
2202// Find the merge section into which an input section with index SHNDX in
2203// OBJECT has been added. Return NULL if none found.
2204
2205Output_section_data*
2206Output_section::find_merge_section(const Relobj* object,
2207 unsigned int shndx) const
2208{
2209 Input_section_specifier iss(object, shndx);
2210 Output_section_data_by_input_section_map::const_iterator p =
2211 this->merge_section_map_.find(iss);
2212 if (p != this->merge_section_map_.end())
2213 {
2214 Output_section_data* posd = p->second;
2215 gold_assert(posd->is_merge_section_for(object, shndx));
2216 return posd;
2217 }
2218 else
2219 return NULL;
2220}
2221
2222// Find an relaxed input section corresponding to an input section
2223// in OBJECT with index SHNDX.
2224
2225const Output_section_data*
2226Output_section::find_relaxed_input_section(const Relobj* object,
2227 unsigned int shndx) const
2228{
2229 // Be careful that the map may not be valid due to input section export
2230 // to scripts or a check-point restore.
2231 if (!this->is_relaxed_input_section_map_valid_)
2232 {
2233 // Rebuild the map as needed.
2234 this->relaxed_input_section_map_.clear();
2235 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2236 p != this->input_sections_.end();
2237 ++p)
2238 if (p->is_relaxed_input_section())
2239 {
2240 Input_section_specifier iss(p->relobj(), p->shndx());
2241 this->relaxed_input_section_map_[iss] =
2242 p->relaxed_input_section();
2243 }
2244 this->is_relaxed_input_section_map_valid_ = true;
2245 }
2246
2247 Input_section_specifier iss(object, shndx);
2248 Output_section_data_by_input_section_map::const_iterator p =
2249 this->relaxed_input_section_map_.find(iss);
2250 if (p != this->relaxed_input_section_map_.end())
2251 return p->second;
2252 else
2253 return NULL;
2254}
2255
730cdc88
ILT
2256// Given an address OFFSET relative to the start of input section
2257// SHNDX in OBJECT, return whether this address is being included in
2258// the final link. This should only be called if SHNDX in OBJECT has
2259// a special mapping.
2260
2261bool
2262Output_section::is_input_address_mapped(const Relobj* object,
2263 unsigned int shndx,
2264 off_t offset) const
2265{
c0a62865
DK
2266 // Look at the Output_section_data_maps first.
2267 const Output_section_data* posd = this->find_merge_section(object, shndx);
2268 if (posd == NULL)
2269 posd = this->find_relaxed_input_section(object, shndx);
2270
2271 if (posd != NULL)
2272 {
2273 section_offset_type output_offset;
2274 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2275 gold_assert(found);
2276 return output_offset != -1;
2277 }
2278
2279 // Fall back to the slow look-up.
730cdc88
ILT
2280 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2281 p != this->input_sections_.end();
2282 ++p)
2283 {
8383303e 2284 section_offset_type output_offset;
730cdc88
ILT
2285 if (p->output_offset(object, shndx, offset, &output_offset))
2286 return output_offset != -1;
2287 }
2288
2289 // By default we assume that the address is mapped. This should
2290 // only be called after we have passed all sections to Layout. At
2291 // that point we should know what we are discarding.
2292 return true;
2293}
2294
2295// Given an address OFFSET relative to the start of input section
2296// SHNDX in object OBJECT, return the output offset relative to the
1e983657
ILT
2297// start of the input section in the output section. This should only
2298// be called if SHNDX in OBJECT has a special mapping.
730cdc88 2299
8383303e 2300section_offset_type
730cdc88 2301Output_section::output_offset(const Relobj* object, unsigned int shndx,
8383303e 2302 section_offset_type offset) const
730cdc88 2303{
c0a62865
DK
2304 // This can only be called meaningfully when we know the data size
2305 // of this.
2306 gold_assert(this->is_data_size_valid());
730cdc88 2307
c0a62865
DK
2308 // Look at the Output_section_data_maps first.
2309 const Output_section_data* posd = this->find_merge_section(object, shndx);
2310 if (posd == NULL)
2311 posd = this->find_relaxed_input_section(object, shndx);
2312 if (posd != NULL)
2313 {
2314 section_offset_type output_offset;
2315 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2316 gold_assert(found);
2317 return output_offset;
2318 }
2319
2320 // Fall back to the slow look-up.
730cdc88
ILT
2321 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2322 p != this->input_sections_.end();
2323 ++p)
2324 {
8383303e 2325 section_offset_type output_offset;
730cdc88
ILT
2326 if (p->output_offset(object, shndx, offset, &output_offset))
2327 return output_offset;
2328 }
2329 gold_unreachable();
2330}
2331
b8e6aad9
ILT
2332// Return the output virtual address of OFFSET relative to the start
2333// of input section SHNDX in object OBJECT.
2334
2335uint64_t
2336Output_section::output_address(const Relobj* object, unsigned int shndx,
2337 off_t offset) const
2338{
2339 uint64_t addr = this->address() + this->first_input_offset_;
c0a62865
DK
2340
2341 // Look at the Output_section_data_maps first.
2342 const Output_section_data* posd = this->find_merge_section(object, shndx);
2343 if (posd == NULL)
2344 posd = this->find_relaxed_input_section(object, shndx);
2345 if (posd != NULL && posd->is_address_valid())
2346 {
2347 section_offset_type output_offset;
2348 bool found = posd->output_offset(object, shndx, offset, &output_offset);
2349 gold_assert(found);
2350 return posd->address() + output_offset;
2351 }
2352
2353 // Fall back to the slow look-up.
b8e6aad9
ILT
2354 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2355 p != this->input_sections_.end();
2356 ++p)
2357 {
2358 addr = align_address(addr, p->addralign());
8383303e 2359 section_offset_type output_offset;
730cdc88
ILT
2360 if (p->output_offset(object, shndx, offset, &output_offset))
2361 {
2362 if (output_offset == -1)
eff45813 2363 return -1ULL;
730cdc88
ILT
2364 return addr + output_offset;
2365 }
b8e6aad9
ILT
2366 addr += p->data_size();
2367 }
2368
2369 // If we get here, it means that we don't know the mapping for this
2370 // input section. This might happen in principle if
2371 // add_input_section were called before add_output_section_data.
2372 // But it should never actually happen.
2373
2374 gold_unreachable();
ead1e424
ILT
2375}
2376
e29e076a 2377// Find the output address of the start of the merged section for
a9a60db6
ILT
2378// input section SHNDX in object OBJECT.
2379
e29e076a
ILT
2380bool
2381Output_section::find_starting_output_address(const Relobj* object,
2382 unsigned int shndx,
2383 uint64_t* paddr) const
a9a60db6 2384{
c0a62865
DK
2385 // FIXME: This becomes a bottle-neck if we have many relaxed sections.
2386 // Looking up the merge section map does not always work as we sometimes
2387 // find a merge section without its address set.
a9a60db6
ILT
2388 uint64_t addr = this->address() + this->first_input_offset_;
2389 for (Input_section_list::const_iterator p = this->input_sections_.begin();
2390 p != this->input_sections_.end();
2391 ++p)
2392 {
2393 addr = align_address(addr, p->addralign());
2394
2395 // It would be nice if we could use the existing output_offset
2396 // method to get the output offset of input offset 0.
2397 // Unfortunately we don't know for sure that input offset 0 is
2398 // mapped at all.
2399 if (p->is_merge_section_for(object, shndx))
e29e076a
ILT
2400 {
2401 *paddr = addr;
2402 return true;
2403 }
a9a60db6
ILT
2404
2405 addr += p->data_size();
2406 }
e29e076a
ILT
2407
2408 // We couldn't find a merge output section for this input section.
2409 return false;
a9a60db6
ILT
2410}
2411
27bc2bce 2412// Set the data size of an Output_section. This is where we handle
ead1e424
ILT
2413// setting the addresses of any Output_section_data objects.
2414
2415void
27bc2bce 2416Output_section::set_final_data_size()
ead1e424
ILT
2417{
2418 if (this->input_sections_.empty())
27bc2bce
ILT
2419 {
2420 this->set_data_size(this->current_data_size_for_child());
2421 return;
2422 }
ead1e424 2423
2fd32231
ILT
2424 if (this->must_sort_attached_input_sections())
2425 this->sort_attached_input_sections();
2426
27bc2bce
ILT
2427 uint64_t address = this->address();
2428 off_t startoff = this->offset();
ead1e424
ILT
2429 off_t off = startoff + this->first_input_offset_;
2430 for (Input_section_list::iterator p = this->input_sections_.begin();
2431 p != this->input_sections_.end();
2432 ++p)
2433 {
2434 off = align_address(off, p->addralign());
96803768
ILT
2435 p->set_address_and_file_offset(address + (off - startoff), off,
2436 startoff);
ead1e424
ILT
2437 off += p->data_size();
2438 }
2439
2440 this->set_data_size(off - startoff);
2441}
9a0910c3 2442
a445fddf
ILT
2443// Reset the address and file offset.
2444
2445void
2446Output_section::do_reset_address_and_file_offset()
2447{
20e6d0d6
DK
2448 // An unallocated section has no address. Forcing this means that
2449 // we don't need special treatment for symbols defined in debug
2450 // sections. We do the same in the constructor.
2451 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2452 this->set_address(0);
2453
a445fddf
ILT
2454 for (Input_section_list::iterator p = this->input_sections_.begin();
2455 p != this->input_sections_.end();
2456 ++p)
2457 p->reset_address_and_file_offset();
2458}
20e6d0d6
DK
2459
2460// Return true if address and file offset have the values after reset.
2461
2462bool
2463Output_section::do_address_and_file_offset_have_reset_values() const
2464{
2465 if (this->is_offset_valid())
2466 return false;
2467
2468 // An unallocated section has address 0 after its construction or a reset.
2469 if ((this->flags_ & elfcpp::SHF_ALLOC) == 0)
2470 return this->is_address_valid() && this->address() == 0;
2471 else
2472 return !this->is_address_valid();
2473}
a445fddf 2474
7bf1f802
ILT
2475// Set the TLS offset. Called only for SHT_TLS sections.
2476
2477void
2478Output_section::do_set_tls_offset(uint64_t tls_base)
2479{
2480 this->tls_offset_ = this->address() - tls_base;
2481}
2482
2fd32231
ILT
2483// In a few cases we need to sort the input sections attached to an
2484// output section. This is used to implement the type of constructor
2485// priority ordering implemented by the GNU linker, in which the
2486// priority becomes part of the section name and the sections are
2487// sorted by name. We only do this for an output section if we see an
2488// attached input section matching ".ctor.*", ".dtor.*",
2489// ".init_array.*" or ".fini_array.*".
2490
2491class Output_section::Input_section_sort_entry
2492{
2493 public:
2494 Input_section_sort_entry()
2495 : input_section_(), index_(-1U), section_has_name_(false),
2496 section_name_()
2497 { }
2498
2499 Input_section_sort_entry(const Input_section& input_section,
2500 unsigned int index)
2501 : input_section_(input_section), index_(index),
20e6d0d6
DK
2502 section_has_name_(input_section.is_input_section()
2503 || input_section.is_relaxed_input_section())
2fd32231
ILT
2504 {
2505 if (this->section_has_name_)
2506 {
2507 // This is only called single-threaded from Layout::finalize,
2508 // so it is OK to lock. Unfortunately we have no way to pass
2509 // in a Task token.
2510 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
20e6d0d6
DK
2511 Object* obj = (input_section.is_input_section()
2512 ? input_section.relobj()
2513 : input_section.relaxed_input_section()->relobj());
2fd32231
ILT
2514 Task_lock_obj<Object> tl(dummy_task, obj);
2515
2516 // This is a slow operation, which should be cached in
2517 // Layout::layout if this becomes a speed problem.
2518 this->section_name_ = obj->section_name(input_section.shndx());
2519 }
2520 }
2521
2522 // Return the Input_section.
2523 const Input_section&
2524 input_section() const
2525 {
2526 gold_assert(this->index_ != -1U);
2527 return this->input_section_;
2528 }
2529
2530 // The index of this entry in the original list. This is used to
2531 // make the sort stable.
2532 unsigned int
2533 index() const
2534 {
2535 gold_assert(this->index_ != -1U);
2536 return this->index_;
2537 }
2538
2539 // Whether there is a section name.
2540 bool
2541 section_has_name() const
2542 { return this->section_has_name_; }
2543
2544 // The section name.
2545 const std::string&
2546 section_name() const
2547 {
2548 gold_assert(this->section_has_name_);
2549 return this->section_name_;
2550 }
2551
ab794b6b
ILT
2552 // Return true if the section name has a priority. This is assumed
2553 // to be true if it has a dot after the initial dot.
2fd32231 2554 bool
ab794b6b 2555 has_priority() const
2fd32231
ILT
2556 {
2557 gold_assert(this->section_has_name_);
ab794b6b 2558 return this->section_name_.find('.', 1);
2fd32231
ILT
2559 }
2560
ab794b6b
ILT
2561 // Return true if this an input file whose base name matches
2562 // FILE_NAME. The base name must have an extension of ".o", and
2563 // must be exactly FILE_NAME.o or FILE_NAME, one character, ".o".
2564 // This is to match crtbegin.o as well as crtbeginS.o without
2565 // getting confused by other possibilities. Overall matching the
2566 // file name this way is a dreadful hack, but the GNU linker does it
2567 // in order to better support gcc, and we need to be compatible.
2fd32231 2568 bool
ab794b6b 2569 match_file_name(const char* match_file_name) const
2fd32231 2570 {
2fd32231
ILT
2571 const std::string& file_name(this->input_section_.relobj()->name());
2572 const char* base_name = lbasename(file_name.c_str());
2573 size_t match_len = strlen(match_file_name);
2574 if (strncmp(base_name, match_file_name, match_len) != 0)
2575 return false;
2576 size_t base_len = strlen(base_name);
2577 if (base_len != match_len + 2 && base_len != match_len + 3)
2578 return false;
2579 return memcmp(base_name + base_len - 2, ".o", 2) == 0;
2580 }
2581
2582 private:
2583 // The Input_section we are sorting.
2584 Input_section input_section_;
2585 // The index of this Input_section in the original list.
2586 unsigned int index_;
2587 // Whether this Input_section has a section name--it won't if this
2588 // is some random Output_section_data.
2589 bool section_has_name_;
2590 // The section name if there is one.
2591 std::string section_name_;
2592};
2593
2594// Return true if S1 should come before S2 in the output section.
2595
2596bool
2597Output_section::Input_section_sort_compare::operator()(
2598 const Output_section::Input_section_sort_entry& s1,
2599 const Output_section::Input_section_sort_entry& s2) const
2600{
ab794b6b
ILT
2601 // crtbegin.o must come first.
2602 bool s1_begin = s1.match_file_name("crtbegin");
2603 bool s2_begin = s2.match_file_name("crtbegin");
2fd32231
ILT
2604 if (s1_begin || s2_begin)
2605 {
2606 if (!s1_begin)
2607 return false;
2608 if (!s2_begin)
2609 return true;
2610 return s1.index() < s2.index();
2611 }
2612
ab794b6b
ILT
2613 // crtend.o must come last.
2614 bool s1_end = s1.match_file_name("crtend");
2615 bool s2_end = s2.match_file_name("crtend");
2fd32231
ILT
2616 if (s1_end || s2_end)
2617 {
2618 if (!s1_end)
2619 return true;
2620 if (!s2_end)
2621 return false;
2622 return s1.index() < s2.index();
2623 }
2624
ab794b6b
ILT
2625 // We sort all the sections with no names to the end.
2626 if (!s1.section_has_name() || !s2.section_has_name())
2627 {
2628 if (s1.section_has_name())
2629 return true;
2630 if (s2.section_has_name())
2631 return false;
2632 return s1.index() < s2.index();
2633 }
2fd32231 2634
ab794b6b
ILT
2635 // A section with a priority follows a section without a priority.
2636 // The GNU linker does this for all but .init_array sections; until
2637 // further notice we'll assume that that is an mistake.
2638 bool s1_has_priority = s1.has_priority();
2639 bool s2_has_priority = s2.has_priority();
2640 if (s1_has_priority && !s2_has_priority)
2fd32231 2641 return false;
ab794b6b 2642 if (!s1_has_priority && s2_has_priority)
2fd32231
ILT
2643 return true;
2644
2645 // Otherwise we sort by name.
2646 int compare = s1.section_name().compare(s2.section_name());
2647 if (compare != 0)
2648 return compare < 0;
2649
2650 // Otherwise we keep the input order.
2651 return s1.index() < s2.index();
2652}
2653
2654// Sort the input sections attached to an output section.
2655
2656void
2657Output_section::sort_attached_input_sections()
2658{
2659 if (this->attached_input_sections_are_sorted_)
2660 return;
2661
20e6d0d6
DK
2662 if (this->checkpoint_ != NULL
2663 && !this->checkpoint_->input_sections_saved())
2664 this->checkpoint_->save_input_sections();
2665
2fd32231
ILT
2666 // The only thing we know about an input section is the object and
2667 // the section index. We need the section name. Recomputing this
2668 // is slow but this is an unusual case. If this becomes a speed
2669 // problem we can cache the names as required in Layout::layout.
2670
2671 // We start by building a larger vector holding a copy of each
2672 // Input_section, plus its current index in the list and its name.
2673 std::vector<Input_section_sort_entry> sort_list;
2674
2675 unsigned int i = 0;
2676 for (Input_section_list::iterator p = this->input_sections_.begin();
2677 p != this->input_sections_.end();
2678 ++p, ++i)
2679 sort_list.push_back(Input_section_sort_entry(*p, i));
2680
2681 // Sort the input sections.
2682 std::sort(sort_list.begin(), sort_list.end(), Input_section_sort_compare());
2683
2684 // Copy the sorted input sections back to our list.
2685 this->input_sections_.clear();
2686 for (std::vector<Input_section_sort_entry>::iterator p = sort_list.begin();
2687 p != sort_list.end();
2688 ++p)
2689 this->input_sections_.push_back(p->input_section());
2690
2691 // Remember that we sorted the input sections, since we might get
2692 // called again.
2693 this->attached_input_sections_are_sorted_ = true;
2694}
2695
61ba1cf9
ILT
2696// Write the section header to *OSHDR.
2697
2698template<int size, bool big_endian>
2699void
16649710
ILT
2700Output_section::write_header(const Layout* layout,
2701 const Stringpool* secnamepool,
61ba1cf9
ILT
2702 elfcpp::Shdr_write<size, big_endian>* oshdr) const
2703{
2704 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
2705 oshdr->put_sh_type(this->type_);
6a74a719
ILT
2706
2707 elfcpp::Elf_Xword flags = this->flags_;
755ab8af 2708 if (this->info_section_ != NULL && this->info_uses_section_index_)
6a74a719
ILT
2709 flags |= elfcpp::SHF_INFO_LINK;
2710 oshdr->put_sh_flags(flags);
2711
61ba1cf9
ILT
2712 oshdr->put_sh_addr(this->address());
2713 oshdr->put_sh_offset(this->offset());
2714 oshdr->put_sh_size(this->data_size());
16649710
ILT
2715 if (this->link_section_ != NULL)
2716 oshdr->put_sh_link(this->link_section_->out_shndx());
2717 else if (this->should_link_to_symtab_)
2718 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
2719 else if (this->should_link_to_dynsym_)
2720 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
2721 else
2722 oshdr->put_sh_link(this->link_);
755ab8af
ILT
2723
2724 elfcpp::Elf_Word info;
16649710 2725 if (this->info_section_ != NULL)
755ab8af
ILT
2726 {
2727 if (this->info_uses_section_index_)
2728 info = this->info_section_->out_shndx();
2729 else
2730 info = this->info_section_->symtab_index();
2731 }
6a74a719 2732 else if (this->info_symndx_ != NULL)
755ab8af 2733 info = this->info_symndx_->symtab_index();
16649710 2734 else
755ab8af
ILT
2735 info = this->info_;
2736 oshdr->put_sh_info(info);
2737
61ba1cf9
ILT
2738 oshdr->put_sh_addralign(this->addralign_);
2739 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
2740}
2741
ead1e424
ILT
2742// Write out the data. For input sections the data is written out by
2743// Object::relocate, but we have to handle Output_section_data objects
2744// here.
2745
2746void
2747Output_section::do_write(Output_file* of)
2748{
96803768
ILT
2749 gold_assert(!this->requires_postprocessing());
2750
c0a62865
DK
2751 // If the target performs relaxation, we delay filler generation until now.
2752 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
2753
c51e6221
ILT
2754 off_t output_section_file_offset = this->offset();
2755 for (Fill_list::iterator p = this->fills_.begin();
2756 p != this->fills_.end();
2757 ++p)
2758 {
8851ecca 2759 std::string fill_data(parameters->target().code_fill(p->length()));
c51e6221 2760 of->write(output_section_file_offset + p->section_offset(),
a445fddf 2761 fill_data.data(), fill_data.size());
c51e6221
ILT
2762 }
2763
c0a62865 2764 off_t off = this->offset() + this->first_input_offset_;
ead1e424
ILT
2765 for (Input_section_list::iterator p = this->input_sections_.begin();
2766 p != this->input_sections_.end();
2767 ++p)
c0a62865
DK
2768 {
2769 off_t aligned_off = align_address(off, p->addralign());
2770 if (this->generate_code_fills_at_write_ && (off != aligned_off))
2771 {
2772 size_t fill_len = aligned_off - off;
2773 std::string fill_data(parameters->target().code_fill(fill_len));
2774 of->write(off, fill_data.data(), fill_data.size());
2775 }
2776
2777 p->write(of);
2778 off = aligned_off + p->data_size();
2779 }
ead1e424
ILT
2780}
2781
96803768
ILT
2782// If a section requires postprocessing, create the buffer to use.
2783
2784void
2785Output_section::create_postprocessing_buffer()
2786{
2787 gold_assert(this->requires_postprocessing());
1bedcac5
ILT
2788
2789 if (this->postprocessing_buffer_ != NULL)
2790 return;
96803768
ILT
2791
2792 if (!this->input_sections_.empty())
2793 {
2794 off_t off = this->first_input_offset_;
2795 for (Input_section_list::iterator p = this->input_sections_.begin();
2796 p != this->input_sections_.end();
2797 ++p)
2798 {
2799 off = align_address(off, p->addralign());
2800 p->finalize_data_size();
2801 off += p->data_size();
2802 }
2803 this->set_current_data_size_for_child(off);
2804 }
2805
2806 off_t buffer_size = this->current_data_size_for_child();
2807 this->postprocessing_buffer_ = new unsigned char[buffer_size];
2808}
2809
2810// Write all the data of an Output_section into the postprocessing
2811// buffer. This is used for sections which require postprocessing,
2812// such as compression. Input sections are handled by
2813// Object::Relocate.
2814
2815void
2816Output_section::write_to_postprocessing_buffer()
2817{
2818 gold_assert(this->requires_postprocessing());
2819
c0a62865
DK
2820 // If the target performs relaxation, we delay filler generation until now.
2821 gold_assert(!this->generate_code_fills_at_write_ || this->fills_.empty());
2822
96803768
ILT
2823 unsigned char* buffer = this->postprocessing_buffer();
2824 for (Fill_list::iterator p = this->fills_.begin();
2825 p != this->fills_.end();
2826 ++p)
2827 {
8851ecca 2828 std::string fill_data(parameters->target().code_fill(p->length()));
a445fddf
ILT
2829 memcpy(buffer + p->section_offset(), fill_data.data(),
2830 fill_data.size());
96803768
ILT
2831 }
2832
2833 off_t off = this->first_input_offset_;
2834 for (Input_section_list::iterator p = this->input_sections_.begin();
2835 p != this->input_sections_.end();
2836 ++p)
2837 {
c0a62865
DK
2838 off_t aligned_off = align_address(off, p->addralign());
2839 if (this->generate_code_fills_at_write_ && (off != aligned_off))
2840 {
2841 size_t fill_len = aligned_off - off;
2842 std::string fill_data(parameters->target().code_fill(fill_len));
2843 memcpy(buffer + off, fill_data.data(), fill_data.size());
2844 }
2845
2846 p->write_to_buffer(buffer + aligned_off);
2847 off = aligned_off + p->data_size();
96803768
ILT
2848 }
2849}
2850
a445fddf
ILT
2851// Get the input sections for linker script processing. We leave
2852// behind the Output_section_data entries. Note that this may be
2853// slightly incorrect for merge sections. We will leave them behind,
2854// but it is possible that the script says that they should follow
2855// some other input sections, as in:
2856// .rodata { *(.rodata) *(.rodata.cst*) }
2857// For that matter, we don't handle this correctly:
2858// .rodata { foo.o(.rodata.cst*) *(.rodata.cst*) }
2859// With luck this will never matter.
2860
2861uint64_t
2862Output_section::get_input_sections(
2863 uint64_t address,
2864 const std::string& fill,
20e6d0d6 2865 std::list<Simple_input_section>* input_sections)
a445fddf 2866{
20e6d0d6
DK
2867 if (this->checkpoint_ != NULL
2868 && !this->checkpoint_->input_sections_saved())
2869 this->checkpoint_->save_input_sections();
2870
c0a62865
DK
2871 // Invalidate the relaxed input section map.
2872 this->is_relaxed_input_section_map_valid_ = false;
2873
a445fddf
ILT
2874 uint64_t orig_address = address;
2875
2876 address = align_address(address, this->addralign());
2877
2878 Input_section_list remaining;
2879 for (Input_section_list::iterator p = this->input_sections_.begin();
2880 p != this->input_sections_.end();
2881 ++p)
2882 {
2883 if (p->is_input_section())
20e6d0d6
DK
2884 input_sections->push_back(Simple_input_section(p->relobj(),
2885 p->shndx()));
2886 else if (p->is_relaxed_input_section())
2887 input_sections->push_back(
2888 Simple_input_section(p->relaxed_input_section()));
a445fddf
ILT
2889 else
2890 {
2891 uint64_t aligned_address = align_address(address, p->addralign());
2892 if (aligned_address != address && !fill.empty())
2893 {
2894 section_size_type length =
2895 convert_to_section_size_type(aligned_address - address);
2896 std::string this_fill;
2897 this_fill.reserve(length);
2898 while (this_fill.length() + fill.length() <= length)
2899 this_fill += fill;
2900 if (this_fill.length() < length)
2901 this_fill.append(fill, 0, length - this_fill.length());
2902
2903 Output_section_data* posd = new Output_data_const(this_fill, 0);
2904 remaining.push_back(Input_section(posd));
2905 }
2906 address = aligned_address;
2907
2908 remaining.push_back(*p);
2909
2910 p->finalize_data_size();
2911 address += p->data_size();
2912 }
2913 }
2914
2915 this->input_sections_.swap(remaining);
2916 this->first_input_offset_ = 0;
2917
2918 uint64_t data_size = address - orig_address;
2919 this->set_current_data_size_for_child(data_size);
2920 return data_size;
2921}
2922
2923// Add an input section from a script.
2924
2925void
20e6d0d6 2926Output_section::add_input_section_for_script(const Simple_input_section& sis,
a445fddf
ILT
2927 off_t data_size,
2928 uint64_t addralign)
2929{
2930 if (addralign > this->addralign_)
2931 this->addralign_ = addralign;
2932
2933 off_t offset_in_section = this->current_data_size_for_child();
2934 off_t aligned_offset_in_section = align_address(offset_in_section,
2935 addralign);
2936
2937 this->set_current_data_size_for_child(aligned_offset_in_section
2938 + data_size);
2939
20e6d0d6
DK
2940 Input_section is =
2941 (sis.is_relaxed_input_section()
2942 ? Input_section(sis.relaxed_input_section())
2943 : Input_section(sis.relobj(), sis.shndx(), data_size, addralign));
2944 this->input_sections_.push_back(is);
2945}
2946
2947//
2948
2949void
2950Output_section::save_states()
2951{
2952 gold_assert(this->checkpoint_ == NULL);
2953 Checkpoint_output_section* checkpoint =
2954 new Checkpoint_output_section(this->addralign_, this->flags_,
2955 this->input_sections_,
2956 this->first_input_offset_,
2957 this->attached_input_sections_are_sorted_);
2958 this->checkpoint_ = checkpoint;
2959 gold_assert(this->fills_.empty());
2960}
2961
2962void
2963Output_section::restore_states()
2964{
2965 gold_assert(this->checkpoint_ != NULL);
2966 Checkpoint_output_section* checkpoint = this->checkpoint_;
2967
2968 this->addralign_ = checkpoint->addralign();
2969 this->flags_ = checkpoint->flags();
2970 this->first_input_offset_ = checkpoint->first_input_offset();
2971
2972 if (!checkpoint->input_sections_saved())
2973 {
2974 // If we have not copied the input sections, just resize it.
2975 size_t old_size = checkpoint->input_sections_size();
2976 gold_assert(this->input_sections_.size() >= old_size);
2977 this->input_sections_.resize(old_size);
2978 }
2979 else
2980 {
2981 // We need to copy the whole list. This is not efficient for
2982 // extremely large output with hundreads of thousands of input
2983 // objects. We may need to re-think how we should pass sections
2984 // to scripts.
c0a62865 2985 this->input_sections_ = *checkpoint->input_sections();
20e6d0d6
DK
2986 }
2987
2988 this->attached_input_sections_are_sorted_ =
2989 checkpoint->attached_input_sections_are_sorted();
c0a62865
DK
2990
2991 // Simply invalidate the relaxed input section map since we do not keep
2992 // track of it.
2993 this->is_relaxed_input_section_map_valid_ = false;
a445fddf
ILT
2994}
2995
7d9e3d98
ILT
2996// Print to the map file.
2997
2998void
2999Output_section::do_print_to_mapfile(Mapfile* mapfile) const
3000{
3001 mapfile->print_output_section(this);
3002
3003 for (Input_section_list::const_iterator p = this->input_sections_.begin();
3004 p != this->input_sections_.end();
3005 ++p)
3006 p->print_to_mapfile(mapfile);
3007}
3008
38c5e8b4
ILT
3009// Print stats for merge sections to stderr.
3010
3011void
3012Output_section::print_merge_stats()
3013{
3014 Input_section_list::iterator p;
3015 for (p = this->input_sections_.begin();
3016 p != this->input_sections_.end();
3017 ++p)
3018 p->print_merge_stats(this->name_);
3019}
3020
a2fb1b05
ILT
3021// Output segment methods.
3022
3023Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
54dc6425 3024 : output_data_(),
75f65a3e 3025 output_bss_(),
a2fb1b05
ILT
3026 vaddr_(0),
3027 paddr_(0),
3028 memsz_(0),
a445fddf
ILT
3029 max_align_(0),
3030 min_p_align_(0),
a2fb1b05
ILT
3031 offset_(0),
3032 filesz_(0),
3033 type_(type),
ead1e424 3034 flags_(flags),
a445fddf 3035 is_max_align_known_(false),
8a5e3e08
ILT
3036 are_addresses_set_(false),
3037 is_large_data_segment_(false)
a2fb1b05
ILT
3038{
3039}
3040
3041// Add an Output_section to an Output_segment.
3042
3043void
75f65a3e 3044Output_segment::add_output_section(Output_section* os,
01676dcd 3045 elfcpp::Elf_Word seg_flags)
a2fb1b05 3046{
a3ad94ed 3047 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
a445fddf 3048 gold_assert(!this->is_max_align_known_);
8a5e3e08 3049 gold_assert(os->is_large_data_section() == this->is_large_data_segment());
75f65a3e 3050
ead1e424 3051 // Update the segment flags.
75f65a3e 3052 this->flags_ |= seg_flags;
75f65a3e
ILT
3053
3054 Output_segment::Output_data_list* pdl;
3055 if (os->type() == elfcpp::SHT_NOBITS)
3056 pdl = &this->output_bss_;
3057 else
3058 pdl = &this->output_data_;
54dc6425 3059
a2fb1b05
ILT
3060 // So that PT_NOTE segments will work correctly, we need to ensure
3061 // that all SHT_NOTE sections are adjacent. This will normally
3062 // happen automatically, because all the SHT_NOTE input sections
3063 // will wind up in the same output section. However, it is possible
3064 // for multiple SHT_NOTE input sections to have different section
3065 // flags, and thus be in different output sections, but for the
3066 // different section flags to map into the same segment flags and
3067 // thus the same output segment.
54dc6425
ILT
3068
3069 // Note that while there may be many input sections in an output
3070 // section, there are normally only a few output sections in an
3071 // output segment. This loop is expected to be fast.
3072
61ba1cf9 3073 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
a2fb1b05 3074 {
a3ad94ed 3075 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 3076 do
54dc6425 3077 {
75f65a3e 3078 --p;
54dc6425
ILT
3079 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
3080 {
3081 ++p;
75f65a3e 3082 pdl->insert(p, os);
54dc6425
ILT
3083 return;
3084 }
3085 }
75f65a3e 3086 while (p != pdl->begin());
54dc6425
ILT
3087 }
3088
3089 // Similarly, so that PT_TLS segments will work, we need to group
75f65a3e
ILT
3090 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
3091 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
3092 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
07f397ab
ILT
3093 // correctly. SHF_TLS sections get added to both a PT_LOAD segment
3094 // and the PT_TLS segment -- we do this grouping only for the
3095 // PT_LOAD segment.
3096 if (this->type_ != elfcpp::PT_TLS
2d924fd9 3097 && (os->flags() & elfcpp::SHF_TLS) != 0)
54dc6425 3098 {
75f65a3e 3099 pdl = &this->output_data_;
661be1e2 3100 if (!pdl->empty())
a2fb1b05 3101 {
661be1e2
ILT
3102 bool nobits = os->type() == elfcpp::SHT_NOBITS;
3103 bool sawtls = false;
3104 Output_segment::Output_data_list::iterator p = pdl->end();
3105 gold_assert(p != pdl->begin());
3106 do
a2fb1b05 3107 {
661be1e2
ILT
3108 --p;
3109 bool insert;
3110 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3111 {
3112 sawtls = true;
3113 // Put a NOBITS section after the first TLS section.
3114 // Put a PROGBITS section after the first
3115 // TLS/PROGBITS section.
3116 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
3117 }
3118 else
3119 {
3120 // If we've gone past the TLS sections, but we've
3121 // seen a TLS section, then we need to insert this
3122 // section now.
3123 insert = sawtls;
3124 }
3125
3126 if (insert)
3127 {
3128 ++p;
3129 pdl->insert(p, os);
3130 return;
3131 }
a2fb1b05 3132 }
661be1e2 3133 while (p != pdl->begin());
a2fb1b05 3134 }
ead1e424 3135
dbe717ef
ILT
3136 // There are no TLS sections yet; put this one at the requested
3137 // location in the section list.
a2fb1b05
ILT
3138 }
3139
9f1d377b
ILT
3140 // For the PT_GNU_RELRO segment, we need to group relro sections,
3141 // and we need to put them before any non-relro sections. Also,
3142 // relro local sections go before relro non-local sections.
3143 if (parameters->options().relro() && os->is_relro())
3144 {
3145 gold_assert(pdl == &this->output_data_);
3146 Output_segment::Output_data_list::iterator p;
3147 for (p = pdl->begin(); p != pdl->end(); ++p)
3148 {
3149 if (!(*p)->is_section())
3150 break;
3151
3152 Output_section* pos = (*p)->output_section();
3153 if (!pos->is_relro()
3154 || (os->is_relro_local() && !pos->is_relro_local()))
3155 break;
3156 }
3157
3158 pdl->insert(p, os);
3159 return;
3160 }
3161
8a5e3e08
ILT
3162 // Small data sections go at the end of the list of data sections.
3163 // If OS is not small, and there are small sections, we have to
3164 // insert it before the first small section.
3165 if (os->type() != elfcpp::SHT_NOBITS
3166 && !os->is_small_section()
3167 && !pdl->empty()
3168 && pdl->back()->is_section()
3169 && pdl->back()->output_section()->is_small_section())
3170 {
3171 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3172 p != pdl->end();
3173 ++p)
3174 {
3175 if ((*p)->is_section()
3176 && (*p)->output_section()->is_small_section())
3177 {
3178 pdl->insert(p, os);
3179 return;
3180 }
3181 }
3182 gold_unreachable();
3183 }
3184
3185 // A small BSS section goes at the start of the BSS sections, after
3186 // other small BSS sections.
3187 if (os->type() == elfcpp::SHT_NOBITS && os->is_small_section())
3188 {
3189 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3190 p != pdl->end();
3191 ++p)
3192 {
3193 if (!(*p)->is_section()
3194 || !(*p)->output_section()->is_small_section())
3195 {
3196 pdl->insert(p, os);
3197 return;
3198 }
3199 }
3200 }
3201
3202 // A large BSS section goes at the end of the BSS sections, which
3203 // means that one that is not large must come before the first large
3204 // one.
3205 if (os->type() == elfcpp::SHT_NOBITS
3206 && !os->is_large_section()
3207 && !pdl->empty()
3208 && pdl->back()->is_section()
3209 && pdl->back()->output_section()->is_large_section())
3210 {
3211 for (Output_segment::Output_data_list::iterator p = pdl->begin();
3212 p != pdl->end();
3213 ++p)
3214 {
3215 if ((*p)->is_section()
3216 && (*p)->output_section()->is_large_section())
3217 {
3218 pdl->insert(p, os);
3219 return;
3220 }
3221 }
3222 gold_unreachable();
3223 }
3224
01676dcd 3225 pdl->push_back(os);
75f65a3e
ILT
3226}
3227
1650c4ff
ILT
3228// Remove an Output_section from this segment. It is an error if it
3229// is not present.
3230
3231void
3232Output_segment::remove_output_section(Output_section* os)
3233{
3234 // We only need this for SHT_PROGBITS.
3235 gold_assert(os->type() == elfcpp::SHT_PROGBITS);
3236 for (Output_data_list::iterator p = this->output_data_.begin();
3237 p != this->output_data_.end();
3238 ++p)
3239 {
3240 if (*p == os)
3241 {
3242 this->output_data_.erase(p);
3243 return;
3244 }
3245 }
3246 gold_unreachable();
3247}
3248
75f65a3e
ILT
3249// Add an Output_data (which is not an Output_section) to the start of
3250// a segment.
3251
3252void
3253Output_segment::add_initial_output_data(Output_data* od)
3254{
a445fddf 3255 gold_assert(!this->is_max_align_known_);
75f65a3e
ILT
3256 this->output_data_.push_front(od);
3257}
3258
9f1d377b
ILT
3259// Return whether the first data section is a relro section.
3260
3261bool
3262Output_segment::is_first_section_relro() const
3263{
3264 return (!this->output_data_.empty()
3265 && this->output_data_.front()->is_section()
3266 && this->output_data_.front()->output_section()->is_relro());
3267}
3268
75f65a3e 3269// Return the maximum alignment of the Output_data in Output_segment.
75f65a3e
ILT
3270
3271uint64_t
a445fddf 3272Output_segment::maximum_alignment()
75f65a3e 3273{
a445fddf 3274 if (!this->is_max_align_known_)
ead1e424
ILT
3275 {
3276 uint64_t addralign;
3277
a445fddf
ILT
3278 addralign = Output_segment::maximum_alignment_list(&this->output_data_);
3279 if (addralign > this->max_align_)
3280 this->max_align_ = addralign;
ead1e424 3281
a445fddf
ILT
3282 addralign = Output_segment::maximum_alignment_list(&this->output_bss_);
3283 if (addralign > this->max_align_)
3284 this->max_align_ = addralign;
ead1e424 3285
9f1d377b
ILT
3286 // If -z relro is in effect, and the first section in this
3287 // segment is a relro section, then the segment must be aligned
3288 // to at least the common page size. This ensures that the
3289 // PT_GNU_RELRO segment will start at a page boundary.
2d924fd9
ILT
3290 if (this->type_ == elfcpp::PT_LOAD
3291 && parameters->options().relro()
3292 && this->is_first_section_relro())
9f1d377b
ILT
3293 {
3294 addralign = parameters->target().common_pagesize();
3295 if (addralign > this->max_align_)
3296 this->max_align_ = addralign;
3297 }
3298
a445fddf 3299 this->is_max_align_known_ = true;
ead1e424
ILT
3300 }
3301
a445fddf 3302 return this->max_align_;
75f65a3e
ILT
3303}
3304
ead1e424
ILT
3305// Return the maximum alignment of a list of Output_data.
3306
3307uint64_t
a445fddf 3308Output_segment::maximum_alignment_list(const Output_data_list* pdl)
ead1e424
ILT
3309{
3310 uint64_t ret = 0;
3311 for (Output_data_list::const_iterator p = pdl->begin();
3312 p != pdl->end();
3313 ++p)
3314 {
3315 uint64_t addralign = (*p)->addralign();
3316 if (addralign > ret)
3317 ret = addralign;
3318 }
3319 return ret;
3320}
3321
4f4c5f80
ILT
3322// Return the number of dynamic relocs applied to this segment.
3323
3324unsigned int
3325Output_segment::dynamic_reloc_count() const
3326{
3327 return (this->dynamic_reloc_count_list(&this->output_data_)
3328 + this->dynamic_reloc_count_list(&this->output_bss_));
3329}
3330
3331// Return the number of dynamic relocs applied to an Output_data_list.
3332
3333unsigned int
3334Output_segment::dynamic_reloc_count_list(const Output_data_list* pdl) const
3335{
3336 unsigned int count = 0;
3337 for (Output_data_list::const_iterator p = pdl->begin();
3338 p != pdl->end();
3339 ++p)
3340 count += (*p)->dynamic_reloc_count();
3341 return count;
3342}
3343
a445fddf
ILT
3344// Set the section addresses for an Output_segment. If RESET is true,
3345// reset the addresses first. ADDR is the address and *POFF is the
3346// file offset. Set the section indexes starting with *PSHNDX.
3347// Return the address of the immediately following segment. Update
3348// *POFF and *PSHNDX.
75f65a3e
ILT
3349
3350uint64_t
96a2b4e4
ILT
3351Output_segment::set_section_addresses(const Layout* layout, bool reset,
3352 uint64_t addr, off_t* poff,
ead1e424 3353 unsigned int* pshndx)
75f65a3e 3354{
a3ad94ed 3355 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e 3356
a445fddf
ILT
3357 if (!reset && this->are_addresses_set_)
3358 {
3359 gold_assert(this->paddr_ == addr);
3360 addr = this->vaddr_;
3361 }
3362 else
3363 {
3364 this->vaddr_ = addr;
3365 this->paddr_ = addr;
3366 this->are_addresses_set_ = true;
3367 }
75f65a3e 3368
96a2b4e4
ILT
3369 bool in_tls = false;
3370
9f1d377b
ILT
3371 bool in_relro = (parameters->options().relro()
3372 && this->is_first_section_relro());
3373
75f65a3e
ILT
3374 off_t orig_off = *poff;
3375 this->offset_ = orig_off;
3376
96a2b4e4 3377 addr = this->set_section_list_addresses(layout, reset, &this->output_data_,
9f1d377b
ILT
3378 addr, poff, pshndx, &in_tls,
3379 &in_relro);
75f65a3e
ILT
3380 this->filesz_ = *poff - orig_off;
3381
3382 off_t off = *poff;
3383
96a2b4e4
ILT
3384 uint64_t ret = this->set_section_list_addresses(layout, reset,
3385 &this->output_bss_,
3386 addr, poff, pshndx,
9f1d377b 3387 &in_tls, &in_relro);
96a2b4e4
ILT
3388
3389 // If the last section was a TLS section, align upward to the
3390 // alignment of the TLS segment, so that the overall size of the TLS
3391 // segment is aligned.
3392 if (in_tls)
3393 {
3394 uint64_t segment_align = layout->tls_segment()->maximum_alignment();
3395 *poff = align_address(*poff, segment_align);
3396 }
3397
9f1d377b
ILT
3398 // If all the sections were relro sections, align upward to the
3399 // common page size.
3400 if (in_relro)
3401 {
3402 uint64_t page_align = parameters->target().common_pagesize();
3403 *poff = align_address(*poff, page_align);
3404 }
3405
75f65a3e
ILT
3406 this->memsz_ = *poff - orig_off;
3407
3408 // Ignore the file offset adjustments made by the BSS Output_data
3409 // objects.
3410 *poff = off;
61ba1cf9
ILT
3411
3412 return ret;
75f65a3e
ILT
3413}
3414
b8e6aad9
ILT
3415// Set the addresses and file offsets in a list of Output_data
3416// structures.
75f65a3e
ILT
3417
3418uint64_t
96a2b4e4
ILT
3419Output_segment::set_section_list_addresses(const Layout* layout, bool reset,
3420 Output_data_list* pdl,
ead1e424 3421 uint64_t addr, off_t* poff,
96a2b4e4 3422 unsigned int* pshndx,
9f1d377b 3423 bool* in_tls, bool* in_relro)
75f65a3e 3424{
ead1e424 3425 off_t startoff = *poff;
75f65a3e 3426
ead1e424 3427 off_t off = startoff;
75f65a3e
ILT
3428 for (Output_data_list::iterator p = pdl->begin();
3429 p != pdl->end();
3430 ++p)
3431 {
a445fddf
ILT
3432 if (reset)
3433 (*p)->reset_address_and_file_offset();
3434
3435 // When using a linker script the section will most likely
3436 // already have an address.
3437 if (!(*p)->is_address_valid())
3802b2dd 3438 {
96a2b4e4
ILT
3439 uint64_t align = (*p)->addralign();
3440
3441 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
3442 {
3443 // Give the first TLS section the alignment of the
3444 // entire TLS segment. Otherwise the TLS segment as a
3445 // whole may be misaligned.
3446 if (!*in_tls)
3447 {
3448 Output_segment* tls_segment = layout->tls_segment();
3449 gold_assert(tls_segment != NULL);
3450 uint64_t segment_align = tls_segment->maximum_alignment();
3451 gold_assert(segment_align >= align);
3452 align = segment_align;
3453
3454 *in_tls = true;
3455 }
3456 }
3457 else
3458 {
3459 // If this is the first section after the TLS segment,
3460 // align it to at least the alignment of the TLS
3461 // segment, so that the size of the overall TLS segment
3462 // is aligned.
3463 if (*in_tls)
3464 {
3465 uint64_t segment_align =
3466 layout->tls_segment()->maximum_alignment();
3467 if (segment_align > align)
3468 align = segment_align;
3469
3470 *in_tls = false;
3471 }
3472 }
3473
9f1d377b
ILT
3474 // If this is a non-relro section after a relro section,
3475 // align it to a common page boundary so that the dynamic
3476 // linker has a page to mark as read-only.
3477 if (*in_relro
3478 && (!(*p)->is_section()
3479 || !(*p)->output_section()->is_relro()))
3480 {
3481 uint64_t page_align = parameters->target().common_pagesize();
3482 if (page_align > align)
3483 align = page_align;
3484 *in_relro = false;
3485 }
3486
96a2b4e4 3487 off = align_address(off, align);
3802b2dd
ILT
3488 (*p)->set_address_and_file_offset(addr + (off - startoff), off);
3489 }
a445fddf
ILT
3490 else
3491 {
3492 // The script may have inserted a skip forward, but it
3493 // better not have moved backward.
661be1e2
ILT
3494 if ((*p)->address() >= addr + (off - startoff))
3495 off += (*p)->address() - (addr + (off - startoff));
3496 else
3497 {
3498 if (!layout->script_options()->saw_sections_clause())
3499 gold_unreachable();
3500 else
3501 {
3502 Output_section* os = (*p)->output_section();
64b1ae37
DK
3503
3504 // Cast to unsigned long long to avoid format warnings.
3505 unsigned long long previous_dot =
3506 static_cast<unsigned long long>(addr + (off - startoff));
3507 unsigned long long dot =
3508 static_cast<unsigned long long>((*p)->address());
3509
661be1e2
ILT
3510 if (os == NULL)
3511 gold_error(_("dot moves backward in linker script "
64b1ae37 3512 "from 0x%llx to 0x%llx"), previous_dot, dot);
661be1e2
ILT
3513 else
3514 gold_error(_("address of section '%s' moves backward "
3515 "from 0x%llx to 0x%llx"),
64b1ae37 3516 os->name(), previous_dot, dot);
661be1e2
ILT
3517 }
3518 }
a445fddf
ILT
3519 (*p)->set_file_offset(off);
3520 (*p)->finalize_data_size();
3521 }
ead1e424 3522
96a2b4e4
ILT
3523 // We want to ignore the size of a SHF_TLS or SHT_NOBITS
3524 // section. Such a section does not affect the size of a
3525 // PT_LOAD segment.
3526 if (!(*p)->is_section_flag_set(elfcpp::SHF_TLS)
ead1e424
ILT
3527 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
3528 off += (*p)->data_size();
75f65a3e 3529
ead1e424
ILT
3530 if ((*p)->is_section())
3531 {
3532 (*p)->set_out_shndx(*pshndx);
3533 ++*pshndx;
3534 }
75f65a3e
ILT
3535 }
3536
3537 *poff = off;
ead1e424 3538 return addr + (off - startoff);
75f65a3e
ILT
3539}
3540
3541// For a non-PT_LOAD segment, set the offset from the sections, if
3542// any.
3543
3544void
3545Output_segment::set_offset()
3546{
a3ad94ed 3547 gold_assert(this->type_ != elfcpp::PT_LOAD);
75f65a3e 3548
a445fddf
ILT
3549 gold_assert(!this->are_addresses_set_);
3550
75f65a3e
ILT
3551 if (this->output_data_.empty() && this->output_bss_.empty())
3552 {
3553 this->vaddr_ = 0;
3554 this->paddr_ = 0;
a445fddf 3555 this->are_addresses_set_ = true;
75f65a3e 3556 this->memsz_ = 0;
a445fddf 3557 this->min_p_align_ = 0;
75f65a3e
ILT
3558 this->offset_ = 0;
3559 this->filesz_ = 0;
3560 return;
3561 }
3562
3563 const Output_data* first;
3564 if (this->output_data_.empty())
3565 first = this->output_bss_.front();
3566 else
3567 first = this->output_data_.front();
3568 this->vaddr_ = first->address();
a445fddf
ILT
3569 this->paddr_ = (first->has_load_address()
3570 ? first->load_address()
3571 : this->vaddr_);
3572 this->are_addresses_set_ = true;
75f65a3e
ILT
3573 this->offset_ = first->offset();
3574
3575 if (this->output_data_.empty())
3576 this->filesz_ = 0;
3577 else
3578 {
3579 const Output_data* last_data = this->output_data_.back();
3580 this->filesz_ = (last_data->address()
3581 + last_data->data_size()
3582 - this->vaddr_);
3583 }
3584
3585 const Output_data* last;
3586 if (this->output_bss_.empty())
3587 last = this->output_data_.back();
3588 else
3589 last = this->output_bss_.back();
3590 this->memsz_ = (last->address()
3591 + last->data_size()
3592 - this->vaddr_);
96a2b4e4
ILT
3593
3594 // If this is a TLS segment, align the memory size. The code in
3595 // set_section_list ensures that the section after the TLS segment
3596 // is aligned to give us room.
3597 if (this->type_ == elfcpp::PT_TLS)
3598 {
3599 uint64_t segment_align = this->maximum_alignment();
3600 gold_assert(this->vaddr_ == align_address(this->vaddr_, segment_align));
3601 this->memsz_ = align_address(this->memsz_, segment_align);
3602 }
9f1d377b
ILT
3603
3604 // If this is a RELRO segment, align the memory size. The code in
3605 // set_section_list ensures that the section after the RELRO segment
3606 // is aligned to give us room.
3607 if (this->type_ == elfcpp::PT_GNU_RELRO)
3608 {
3609 uint64_t page_align = parameters->target().common_pagesize();
3610 gold_assert(this->vaddr_ == align_address(this->vaddr_, page_align));
3611 this->memsz_ = align_address(this->memsz_, page_align);
3612 }
75f65a3e
ILT
3613}
3614
7bf1f802
ILT
3615// Set the TLS offsets of the sections in the PT_TLS segment.
3616
3617void
3618Output_segment::set_tls_offsets()
3619{
3620 gold_assert(this->type_ == elfcpp::PT_TLS);
3621
3622 for (Output_data_list::iterator p = this->output_data_.begin();
3623 p != this->output_data_.end();
3624 ++p)
3625 (*p)->set_tls_offset(this->vaddr_);
3626
3627 for (Output_data_list::iterator p = this->output_bss_.begin();
3628 p != this->output_bss_.end();
3629 ++p)
3630 (*p)->set_tls_offset(this->vaddr_);
3631}
3632
a445fddf
ILT
3633// Return the address of the first section.
3634
3635uint64_t
3636Output_segment::first_section_load_address() const
3637{
3638 for (Output_data_list::const_iterator p = this->output_data_.begin();
3639 p != this->output_data_.end();
3640 ++p)
3641 if ((*p)->is_section())
3642 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3643
3644 for (Output_data_list::const_iterator p = this->output_bss_.begin();
3645 p != this->output_bss_.end();
3646 ++p)
3647 if ((*p)->is_section())
3648 return (*p)->has_load_address() ? (*p)->load_address() : (*p)->address();
3649
3650 gold_unreachable();
3651}
3652
75f65a3e
ILT
3653// Return the number of Output_sections in an Output_segment.
3654
3655unsigned int
3656Output_segment::output_section_count() const
3657{
3658 return (this->output_section_count_list(&this->output_data_)
3659 + this->output_section_count_list(&this->output_bss_));
3660}
3661
3662// Return the number of Output_sections in an Output_data_list.
3663
3664unsigned int
3665Output_segment::output_section_count_list(const Output_data_list* pdl) const
3666{
3667 unsigned int count = 0;
3668 for (Output_data_list::const_iterator p = pdl->begin();
3669 p != pdl->end();
3670 ++p)
3671 {
3672 if ((*p)->is_section())
3673 ++count;
3674 }
3675 return count;
a2fb1b05
ILT
3676}
3677
1c4f3631
ILT
3678// Return the section attached to the list segment with the lowest
3679// load address. This is used when handling a PHDRS clause in a
3680// linker script.
3681
3682Output_section*
3683Output_segment::section_with_lowest_load_address() const
3684{
3685 Output_section* found = NULL;
3686 uint64_t found_lma = 0;
3687 this->lowest_load_address_in_list(&this->output_data_, &found, &found_lma);
3688
3689 Output_section* found_data = found;
3690 this->lowest_load_address_in_list(&this->output_bss_, &found, &found_lma);
3691 if (found != found_data && found_data != NULL)
3692 {
3693 gold_error(_("nobits section %s may not precede progbits section %s "
3694 "in same segment"),
3695 found->name(), found_data->name());
3696 return NULL;
3697 }
3698
3699 return found;
3700}
3701
3702// Look through a list for a section with a lower load address.
3703
3704void
3705Output_segment::lowest_load_address_in_list(const Output_data_list* pdl,
3706 Output_section** found,
3707 uint64_t* found_lma) const
3708{
3709 for (Output_data_list::const_iterator p = pdl->begin();
3710 p != pdl->end();
3711 ++p)
3712 {
3713 if (!(*p)->is_section())
3714 continue;
3715 Output_section* os = static_cast<Output_section*>(*p);
3716 uint64_t lma = (os->has_load_address()
3717 ? os->load_address()
3718 : os->address());
3719 if (*found == NULL || lma < *found_lma)
3720 {
3721 *found = os;
3722 *found_lma = lma;
3723 }
3724 }
3725}
3726
61ba1cf9
ILT
3727// Write the segment data into *OPHDR.
3728
3729template<int size, bool big_endian>
3730void
ead1e424 3731Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
61ba1cf9
ILT
3732{
3733 ophdr->put_p_type(this->type_);
3734 ophdr->put_p_offset(this->offset_);
3735 ophdr->put_p_vaddr(this->vaddr_);
3736 ophdr->put_p_paddr(this->paddr_);
3737 ophdr->put_p_filesz(this->filesz_);
3738 ophdr->put_p_memsz(this->memsz_);
3739 ophdr->put_p_flags(this->flags_);
a445fddf 3740 ophdr->put_p_align(std::max(this->min_p_align_, this->maximum_alignment()));
61ba1cf9
ILT
3741}
3742
3743// Write the section headers into V.
3744
3745template<int size, bool big_endian>
3746unsigned char*
16649710
ILT
3747Output_segment::write_section_headers(const Layout* layout,
3748 const Stringpool* secnamepool,
ead1e424 3749 unsigned char* v,
7d1a9ebb 3750 unsigned int *pshndx) const
5482377d 3751{
ead1e424
ILT
3752 // Every section that is attached to a segment must be attached to a
3753 // PT_LOAD segment, so we only write out section headers for PT_LOAD
3754 // segments.
3755 if (this->type_ != elfcpp::PT_LOAD)
3756 return v;
3757
7d1a9ebb
ILT
3758 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3759 &this->output_data_,
3760 v, pshndx);
3761 v = this->write_section_headers_list<size, big_endian>(layout, secnamepool,
3762 &this->output_bss_,
3763 v, pshndx);
61ba1cf9
ILT
3764 return v;
3765}
3766
3767template<int size, bool big_endian>
3768unsigned char*
16649710
ILT
3769Output_segment::write_section_headers_list(const Layout* layout,
3770 const Stringpool* secnamepool,
61ba1cf9 3771 const Output_data_list* pdl,
ead1e424 3772 unsigned char* v,
7d1a9ebb 3773 unsigned int* pshndx) const
61ba1cf9
ILT
3774{
3775 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
3776 for (Output_data_list::const_iterator p = pdl->begin();
3777 p != pdl->end();
3778 ++p)
3779 {
3780 if ((*p)->is_section())
3781 {
5482377d 3782 const Output_section* ps = static_cast<const Output_section*>(*p);
a3ad94ed 3783 gold_assert(*pshndx == ps->out_shndx());
61ba1cf9 3784 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 3785 ps->write_header(layout, secnamepool, &oshdr);
61ba1cf9 3786 v += shdr_size;
ead1e424 3787 ++*pshndx;
61ba1cf9
ILT
3788 }
3789 }
3790 return v;
3791}
3792
7d9e3d98
ILT
3793// Print the output sections to the map file.
3794
3795void
3796Output_segment::print_sections_to_mapfile(Mapfile* mapfile) const
3797{
3798 if (this->type() != elfcpp::PT_LOAD)
3799 return;
3800 this->print_section_list_to_mapfile(mapfile, &this->output_data_);
3801 this->print_section_list_to_mapfile(mapfile, &this->output_bss_);
3802}
3803
3804// Print an output section list to the map file.
3805
3806void
3807Output_segment::print_section_list_to_mapfile(Mapfile* mapfile,
3808 const Output_data_list* pdl) const
3809{
3810 for (Output_data_list::const_iterator p = pdl->begin();
3811 p != pdl->end();
3812 ++p)
3813 (*p)->print_to_mapfile(mapfile);
3814}
3815
a2fb1b05
ILT
3816// Output_file methods.
3817
14144f39
ILT
3818Output_file::Output_file(const char* name)
3819 : name_(name),
61ba1cf9
ILT
3820 o_(-1),
3821 file_size_(0),
c420411f 3822 base_(NULL),
516cb3d0
ILT
3823 map_is_anonymous_(false),
3824 is_temporary_(false)
61ba1cf9
ILT
3825{
3826}
3827
404c2abb
ILT
3828// Try to open an existing file. Returns false if the file doesn't
3829// exist, has a size of 0 or can't be mmapped.
3830
3831bool
3832Output_file::open_for_modification()
3833{
3834 // The name "-" means "stdout".
3835 if (strcmp(this->name_, "-") == 0)
3836 return false;
3837
3838 // Don't bother opening files with a size of zero.
3839 struct stat s;
3840 if (::stat(this->name_, &s) != 0 || s.st_size == 0)
3841 return false;
3842
3843 int o = open_descriptor(-1, this->name_, O_RDWR, 0);
3844 if (o < 0)
3845 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3846 this->o_ = o;
3847 this->file_size_ = s.st_size;
3848
3849 // If the file can't be mmapped, copying the content to an anonymous
3850 // map will probably negate the performance benefits of incremental
3851 // linking. This could be helped by using views and loading only
3852 // the necessary parts, but this is not supported as of now.
3853 if (!this->map_no_anonymous())
3854 {
3855 release_descriptor(o, true);
3856 this->o_ = -1;
3857 this->file_size_ = 0;
3858 return false;
3859 }
3860
3861 return true;
3862}
3863
61ba1cf9
ILT
3864// Open the output file.
3865
a2fb1b05 3866void
61ba1cf9 3867Output_file::open(off_t file_size)
a2fb1b05 3868{
61ba1cf9
ILT
3869 this->file_size_ = file_size;
3870
4e9d8586
ILT
3871 // Unlink the file first; otherwise the open() may fail if the file
3872 // is busy (e.g. it's an executable that's currently being executed).
3873 //
3874 // However, the linker may be part of a system where a zero-length
3875 // file is created for it to write to, with tight permissions (gcc
3876 // 2.95 did something like this). Unlinking the file would work
3877 // around those permission controls, so we only unlink if the file
3878 // has a non-zero size. We also unlink only regular files to avoid
3879 // trouble with directories/etc.
3880 //
3881 // If we fail, continue; this command is merely a best-effort attempt
3882 // to improve the odds for open().
3883
42a1b686 3884 // We let the name "-" mean "stdout"
516cb3d0 3885 if (!this->is_temporary_)
42a1b686 3886 {
516cb3d0
ILT
3887 if (strcmp(this->name_, "-") == 0)
3888 this->o_ = STDOUT_FILENO;
3889 else
3890 {
3891 struct stat s;
6a89f575
CC
3892 if (::stat(this->name_, &s) == 0
3893 && (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
3894 {
3895 if (s.st_size != 0)
3896 ::unlink(this->name_);
3897 else if (!parameters->options().relocatable())
3898 {
3899 // If we don't unlink the existing file, add execute
3900 // permission where read permissions already exist
3901 // and where the umask permits.
3902 int mask = ::umask(0);
3903 ::umask(mask);
3904 s.st_mode |= (s.st_mode & 0444) >> 2;
3905 ::chmod(this->name_, s.st_mode & ~mask);
3906 }
3907 }
516cb3d0 3908
8851ecca 3909 int mode = parameters->options().relocatable() ? 0666 : 0777;
2a00e4fb
ILT
3910 int o = open_descriptor(-1, this->name_, O_RDWR | O_CREAT | O_TRUNC,
3911 mode);
516cb3d0
ILT
3912 if (o < 0)
3913 gold_fatal(_("%s: open: %s"), this->name_, strerror(errno));
3914 this->o_ = o;
3915 }
42a1b686 3916 }
61ba1cf9 3917
27bc2bce
ILT
3918 this->map();
3919}
3920
3921// Resize the output file.
3922
3923void
3924Output_file::resize(off_t file_size)
3925{
c420411f
ILT
3926 // If the mmap is mapping an anonymous memory buffer, this is easy:
3927 // just mremap to the new size. If it's mapping to a file, we want
3928 // to unmap to flush to the file, then remap after growing the file.
3929 if (this->map_is_anonymous_)
3930 {
3931 void* base = ::mremap(this->base_, this->file_size_, file_size,
3932 MREMAP_MAYMOVE);
3933 if (base == MAP_FAILED)
3934 gold_fatal(_("%s: mremap: %s"), this->name_, strerror(errno));
3935 this->base_ = static_cast<unsigned char*>(base);
3936 this->file_size_ = file_size;
3937 }
3938 else
3939 {
3940 this->unmap();
3941 this->file_size_ = file_size;
fdcac5af
ILT
3942 if (!this->map_no_anonymous())
3943 gold_fatal(_("%s: mmap: %s"), this->name_, strerror(errno));
c420411f 3944 }
27bc2bce
ILT
3945}
3946
404c2abb
ILT
3947// Map an anonymous block of memory which will later be written to the
3948// file. Return whether the map succeeded.
26736d8e 3949
404c2abb 3950bool
26736d8e
ILT
3951Output_file::map_anonymous()
3952{
404c2abb
ILT
3953 void* base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3954 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
3955 if (base != MAP_FAILED)
3956 {
3957 this->map_is_anonymous_ = true;
3958 this->base_ = static_cast<unsigned char*>(base);
3959 return true;
3960 }
3961 return false;
26736d8e
ILT
3962}
3963
404c2abb 3964// Map the file into memory. Return whether the mapping succeeded.
27bc2bce 3965
404c2abb
ILT
3966bool
3967Output_file::map_no_anonymous()
27bc2bce 3968{
c420411f 3969 const int o = this->o_;
61ba1cf9 3970
c420411f
ILT
3971 // If the output file is not a regular file, don't try to mmap it;
3972 // instead, we'll mmap a block of memory (an anonymous buffer), and
3973 // then later write the buffer to the file.
3974 void* base;
3975 struct stat statbuf;
42a1b686
ILT
3976 if (o == STDOUT_FILENO || o == STDERR_FILENO
3977 || ::fstat(o, &statbuf) != 0
516cb3d0
ILT
3978 || !S_ISREG(statbuf.st_mode)
3979 || this->is_temporary_)
404c2abb
ILT
3980 return false;
3981
3982 // Ensure that we have disk space available for the file. If we
3983 // don't do this, it is possible that we will call munmap, close,
3984 // and exit with dirty buffers still in the cache with no assigned
3985 // disk blocks. If the disk is out of space at that point, the
3986 // output file will wind up incomplete, but we will have already
3987 // exited. The alternative to fallocate would be to use fdatasync,
3988 // but that would be a more significant performance hit.
3989 if (::posix_fallocate(o, 0, this->file_size_) < 0)
3990 gold_fatal(_("%s: %s"), this->name_, strerror(errno));
3991
3992 // Map the file into memory.
3993 base = ::mmap(NULL, this->file_size_, PROT_READ | PROT_WRITE,
3994 MAP_SHARED, o, 0);
3995
3996 // The mmap call might fail because of file system issues: the file
3997 // system might not support mmap at all, or it might not support
3998 // mmap with PROT_WRITE.
61ba1cf9 3999 if (base == MAP_FAILED)
404c2abb
ILT
4000 return false;
4001
4002 this->map_is_anonymous_ = false;
61ba1cf9 4003 this->base_ = static_cast<unsigned char*>(base);
404c2abb
ILT
4004 return true;
4005}
4006
4007// Map the file into memory.
4008
4009void
4010Output_file::map()
4011{
4012 if (this->map_no_anonymous())
4013 return;
4014
4015 // The mmap call might fail because of file system issues: the file
4016 // system might not support mmap at all, or it might not support
4017 // mmap with PROT_WRITE. I'm not sure which errno values we will
4018 // see in all cases, so if the mmap fails for any reason and we
4019 // don't care about file contents, try for an anonymous map.
4020 if (this->map_anonymous())
4021 return;
4022
4023 gold_fatal(_("%s: mmap: failed to allocate %lu bytes for output file: %s"),
4024 this->name_, static_cast<unsigned long>(this->file_size_),
4025 strerror(errno));
61ba1cf9
ILT
4026}
4027
c420411f 4028// Unmap the file from memory.
61ba1cf9
ILT
4029
4030void
c420411f 4031Output_file::unmap()
61ba1cf9
ILT
4032{
4033 if (::munmap(this->base_, this->file_size_) < 0)
a0c4fb0a 4034 gold_error(_("%s: munmap: %s"), this->name_, strerror(errno));
61ba1cf9 4035 this->base_ = NULL;
c420411f
ILT
4036}
4037
4038// Close the output file.
4039
4040void
4041Output_file::close()
4042{
4043 // If the map isn't file-backed, we need to write it now.
516cb3d0 4044 if (this->map_is_anonymous_ && !this->is_temporary_)
c420411f
ILT
4045 {
4046 size_t bytes_to_write = this->file_size_;
6d1e3092 4047 size_t offset = 0;
c420411f
ILT
4048 while (bytes_to_write > 0)
4049 {
6d1e3092
CD
4050 ssize_t bytes_written = ::write(this->o_, this->base_ + offset,
4051 bytes_to_write);
c420411f
ILT
4052 if (bytes_written == 0)
4053 gold_error(_("%s: write: unexpected 0 return-value"), this->name_);
4054 else if (bytes_written < 0)
4055 gold_error(_("%s: write: %s"), this->name_, strerror(errno));
4056 else
6d1e3092
CD
4057 {
4058 bytes_to_write -= bytes_written;
4059 offset += bytes_written;
4060 }
c420411f
ILT
4061 }
4062 }
4063 this->unmap();
61ba1cf9 4064
42a1b686 4065 // We don't close stdout or stderr
516cb3d0
ILT
4066 if (this->o_ != STDOUT_FILENO
4067 && this->o_ != STDERR_FILENO
4068 && !this->is_temporary_)
42a1b686
ILT
4069 if (::close(this->o_) < 0)
4070 gold_error(_("%s: close: %s"), this->name_, strerror(errno));
61ba1cf9 4071 this->o_ = -1;
a2fb1b05
ILT
4072}
4073
4074// Instantiate the templates we need. We could use the configure
4075// script to restrict this to only the ones for implemented targets.
4076
193a53d9 4077#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
4078template
4079off_t
4080Output_section::add_input_section<32, false>(
730cdc88 4081 Sized_relobj<32, false>* object,
ead1e424 4082 unsigned int shndx,
a2fb1b05 4083 const char* secname,
730cdc88 4084 const elfcpp::Shdr<32, false>& shdr,
a445fddf
ILT
4085 unsigned int reloc_shndx,
4086 bool have_sections_script);
193a53d9 4087#endif
a2fb1b05 4088
193a53d9 4089#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
4090template
4091off_t
4092Output_section::add_input_section<32, true>(
730cdc88 4093 Sized_relobj<32, true>* object,
ead1e424 4094 unsigned int shndx,
a2fb1b05 4095 const char* secname,
730cdc88 4096 const elfcpp::Shdr<32, true>& shdr,
a445fddf
ILT
4097 unsigned int reloc_shndx,
4098 bool have_sections_script);
193a53d9 4099#endif
a2fb1b05 4100
193a53d9 4101#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
4102template
4103off_t
4104Output_section::add_input_section<64, false>(
730cdc88 4105 Sized_relobj<64, false>* object,
ead1e424 4106 unsigned int shndx,
a2fb1b05 4107 const char* secname,
730cdc88 4108 const elfcpp::Shdr<64, false>& shdr,
a445fddf
ILT
4109 unsigned int reloc_shndx,
4110 bool have_sections_script);
193a53d9 4111#endif
a2fb1b05 4112
193a53d9 4113#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
4114template
4115off_t
4116Output_section::add_input_section<64, true>(
730cdc88 4117 Sized_relobj<64, true>* object,
ead1e424 4118 unsigned int shndx,
a2fb1b05 4119 const char* secname,
730cdc88 4120 const elfcpp::Shdr<64, true>& shdr,
a445fddf
ILT
4121 unsigned int reloc_shndx,
4122 bool have_sections_script);
193a53d9 4123#endif
a2fb1b05 4124
bbbfea06
CC
4125#ifdef HAVE_TARGET_32_LITTLE
4126template
4127class Output_reloc<elfcpp::SHT_REL, false, 32, false>;
4128#endif
4129
4130#ifdef HAVE_TARGET_32_BIG
4131template
4132class Output_reloc<elfcpp::SHT_REL, false, 32, true>;
4133#endif
4134
4135#ifdef HAVE_TARGET_64_LITTLE
4136template
4137class Output_reloc<elfcpp::SHT_REL, false, 64, false>;
4138#endif
4139
4140#ifdef HAVE_TARGET_64_BIG
4141template
4142class Output_reloc<elfcpp::SHT_REL, false, 64, true>;
4143#endif
4144
4145#ifdef HAVE_TARGET_32_LITTLE
4146template
4147class Output_reloc<elfcpp::SHT_REL, true, 32, false>;
4148#endif
4149
4150#ifdef HAVE_TARGET_32_BIG
4151template
4152class Output_reloc<elfcpp::SHT_REL, true, 32, true>;
4153#endif
4154
4155#ifdef HAVE_TARGET_64_LITTLE
4156template
4157class Output_reloc<elfcpp::SHT_REL, true, 64, false>;
4158#endif
4159
4160#ifdef HAVE_TARGET_64_BIG
4161template
4162class Output_reloc<elfcpp::SHT_REL, true, 64, true>;
4163#endif
4164
4165#ifdef HAVE_TARGET_32_LITTLE
4166template
4167class Output_reloc<elfcpp::SHT_RELA, false, 32, false>;
4168#endif
4169
4170#ifdef HAVE_TARGET_32_BIG
4171template
4172class Output_reloc<elfcpp::SHT_RELA, false, 32, true>;
4173#endif
4174
4175#ifdef HAVE_TARGET_64_LITTLE
4176template
4177class Output_reloc<elfcpp::SHT_RELA, false, 64, false>;
4178#endif
4179
4180#ifdef HAVE_TARGET_64_BIG
4181template
4182class Output_reloc<elfcpp::SHT_RELA, false, 64, true>;
4183#endif
4184
4185#ifdef HAVE_TARGET_32_LITTLE
4186template
4187class Output_reloc<elfcpp::SHT_RELA, true, 32, false>;
4188#endif
4189
4190#ifdef HAVE_TARGET_32_BIG
4191template
4192class Output_reloc<elfcpp::SHT_RELA, true, 32, true>;
4193#endif
4194
4195#ifdef HAVE_TARGET_64_LITTLE
4196template
4197class Output_reloc<elfcpp::SHT_RELA, true, 64, false>;
4198#endif
4199
4200#ifdef HAVE_TARGET_64_BIG
4201template
4202class Output_reloc<elfcpp::SHT_RELA, true, 64, true>;
4203#endif
4204
193a53d9 4205#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
4206template
4207class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
193a53d9 4208#endif
c06b7b0b 4209
193a53d9 4210#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
4211template
4212class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
193a53d9 4213#endif
c06b7b0b 4214
193a53d9 4215#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
4216template
4217class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
193a53d9 4218#endif
c06b7b0b 4219
193a53d9 4220#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
4221template
4222class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
193a53d9 4223#endif
c06b7b0b 4224
193a53d9 4225#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
4226template
4227class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
193a53d9 4228#endif
c06b7b0b 4229
193a53d9 4230#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
4231template
4232class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
193a53d9 4233#endif
c06b7b0b 4234
193a53d9 4235#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
4236template
4237class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
193a53d9 4238#endif
c06b7b0b 4239
193a53d9 4240#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
4241template
4242class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
193a53d9 4243#endif
c06b7b0b 4244
193a53d9 4245#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
4246template
4247class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
193a53d9 4248#endif
c06b7b0b 4249
193a53d9 4250#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
4251template
4252class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
193a53d9 4253#endif
c06b7b0b 4254
193a53d9 4255#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
4256template
4257class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
193a53d9 4258#endif
c06b7b0b 4259
193a53d9 4260#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
4261template
4262class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
193a53d9 4263#endif
c06b7b0b 4264
193a53d9 4265#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
4266template
4267class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
193a53d9 4268#endif
c06b7b0b 4269
193a53d9 4270#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
4271template
4272class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
193a53d9 4273#endif
c06b7b0b 4274
193a53d9 4275#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
4276template
4277class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
193a53d9 4278#endif
c06b7b0b 4279
193a53d9 4280#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
4281template
4282class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
193a53d9 4283#endif
c06b7b0b 4284
6a74a719
ILT
4285#ifdef HAVE_TARGET_32_LITTLE
4286template
4287class Output_relocatable_relocs<elfcpp::SHT_REL, 32, false>;
4288#endif
4289
4290#ifdef HAVE_TARGET_32_BIG
4291template
4292class Output_relocatable_relocs<elfcpp::SHT_REL, 32, true>;
4293#endif
4294
4295#ifdef HAVE_TARGET_64_LITTLE
4296template
4297class Output_relocatable_relocs<elfcpp::SHT_REL, 64, false>;
4298#endif
4299
4300#ifdef HAVE_TARGET_64_BIG
4301template
4302class Output_relocatable_relocs<elfcpp::SHT_REL, 64, true>;
4303#endif
4304
4305#ifdef HAVE_TARGET_32_LITTLE
4306template
4307class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, false>;
4308#endif
4309
4310#ifdef HAVE_TARGET_32_BIG
4311template
4312class Output_relocatable_relocs<elfcpp::SHT_RELA, 32, true>;
4313#endif
4314
4315#ifdef HAVE_TARGET_64_LITTLE
4316template
4317class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, false>;
4318#endif
4319
4320#ifdef HAVE_TARGET_64_BIG
4321template
4322class Output_relocatable_relocs<elfcpp::SHT_RELA, 64, true>;
4323#endif
4324
4325#ifdef HAVE_TARGET_32_LITTLE
4326template
4327class Output_data_group<32, false>;
4328#endif
4329
4330#ifdef HAVE_TARGET_32_BIG
4331template
4332class Output_data_group<32, true>;
4333#endif
4334
4335#ifdef HAVE_TARGET_64_LITTLE
4336template
4337class Output_data_group<64, false>;
4338#endif
4339
4340#ifdef HAVE_TARGET_64_BIG
4341template
4342class Output_data_group<64, true>;
4343#endif
4344
193a53d9 4345#ifdef HAVE_TARGET_32_LITTLE
ead1e424 4346template
dbe717ef 4347class Output_data_got<32, false>;
193a53d9 4348#endif
ead1e424 4349
193a53d9 4350#ifdef HAVE_TARGET_32_BIG
ead1e424 4351template
dbe717ef 4352class Output_data_got<32, true>;
193a53d9 4353#endif
ead1e424 4354
193a53d9 4355#ifdef HAVE_TARGET_64_LITTLE
ead1e424 4356template
dbe717ef 4357class Output_data_got<64, false>;
193a53d9 4358#endif
ead1e424 4359
193a53d9 4360#ifdef HAVE_TARGET_64_BIG
ead1e424 4361template
dbe717ef 4362class Output_data_got<64, true>;
193a53d9 4363#endif
ead1e424 4364
a2fb1b05 4365} // End namespace gold.
This page took 0.363032 seconds and 4 git commands to generate.