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