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