From Andrew Chatham: exit on relocation error.
[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>
a2fb1b05 32
7e1edb90 33#include "parameters.h"
a2fb1b05 34#include "object.h"
ead1e424
ILT
35#include "symtab.h"
36#include "reloc.h"
b8e6aad9 37#include "merge.h"
a2fb1b05
ILT
38#include "output.h"
39
40namespace gold
41{
42
a3ad94ed
ILT
43// Output_data variables.
44
45bool Output_data::sizes_are_fixed;
46
a2fb1b05
ILT
47// Output_data methods.
48
49Output_data::~Output_data()
50{
51}
52
75f65a3e
ILT
53// Set the address and offset.
54
55void
56Output_data::set_address(uint64_t addr, off_t off)
57{
58 this->address_ = addr;
59 this->offset_ = off;
60
61 // Let the child class know.
62 this->do_set_address(addr, off);
63}
64
65// Return the default alignment for a size--32 or 64.
66
67uint64_t
68Output_data::default_alignment(int size)
69{
70 if (size == 32)
71 return 4;
72 else if (size == 64)
73 return 8;
74 else
a3ad94ed 75 gold_unreachable();
75f65a3e
ILT
76}
77
75f65a3e
ILT
78// Output_section_header methods. This currently assumes that the
79// segment and section lists are complete at construction time.
80
81Output_section_headers::Output_section_headers(
16649710
ILT
82 const Layout* layout,
83 const Layout::Segment_list* segment_list,
84 const Layout::Section_list* unattached_section_list,
61ba1cf9 85 const Stringpool* secnamepool)
9025d29d 86 : layout_(layout),
75f65a3e 87 segment_list_(segment_list),
a3ad94ed 88 unattached_section_list_(unattached_section_list),
61ba1cf9 89 secnamepool_(secnamepool)
75f65a3e 90{
61ba1cf9
ILT
91 // Count all the sections. Start with 1 for the null section.
92 off_t count = 1;
16649710
ILT
93 for (Layout::Segment_list::const_iterator p = segment_list->begin();
94 p != segment_list->end();
75f65a3e 95 ++p)
ead1e424
ILT
96 if ((*p)->type() == elfcpp::PT_LOAD)
97 count += (*p)->output_section_count();
16649710 98 count += unattached_section_list->size();
75f65a3e 99
9025d29d 100 const int size = parameters->get_size();
75f65a3e
ILT
101 int shdr_size;
102 if (size == 32)
103 shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
104 else if (size == 64)
105 shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
106 else
a3ad94ed 107 gold_unreachable();
75f65a3e
ILT
108
109 this->set_data_size(count * shdr_size);
110}
111
61ba1cf9
ILT
112// Write out the section headers.
113
75f65a3e 114void
61ba1cf9 115Output_section_headers::do_write(Output_file* of)
a2fb1b05 116{
9025d29d 117 if (parameters->get_size() == 32)
61ba1cf9 118 {
9025d29d
ILT
119 if (parameters->is_big_endian())
120 {
121#ifdef HAVE_TARGET_32_BIG
122 this->do_sized_write<32, true>(of);
123#else
124 gold_unreachable();
125#endif
126 }
61ba1cf9 127 else
9025d29d
ILT
128 {
129#ifdef HAVE_TARGET_32_LITTLE
130 this->do_sized_write<32, false>(of);
131#else
132 gold_unreachable();
133#endif
134 }
61ba1cf9 135 }
9025d29d 136 else if (parameters->get_size() == 64)
61ba1cf9 137 {
9025d29d
ILT
138 if (parameters->is_big_endian())
139 {
140#ifdef HAVE_TARGET_64_BIG
141 this->do_sized_write<64, true>(of);
142#else
143 gold_unreachable();
144#endif
145 }
61ba1cf9 146 else
9025d29d
ILT
147 {
148#ifdef HAVE_TARGET_64_LITTLE
149 this->do_sized_write<64, false>(of);
150#else
151 gold_unreachable();
152#endif
153 }
61ba1cf9
ILT
154 }
155 else
a3ad94ed 156 gold_unreachable();
61ba1cf9
ILT
157}
158
159template<int size, bool big_endian>
160void
161Output_section_headers::do_sized_write(Output_file* of)
162{
163 off_t all_shdrs_size = this->data_size();
164 unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size);
165
166 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
167 unsigned char* v = view;
168
169 {
170 typename elfcpp::Shdr_write<size, big_endian> oshdr(v);
171 oshdr.put_sh_name(0);
172 oshdr.put_sh_type(elfcpp::SHT_NULL);
173 oshdr.put_sh_flags(0);
174 oshdr.put_sh_addr(0);
175 oshdr.put_sh_offset(0);
176 oshdr.put_sh_size(0);
177 oshdr.put_sh_link(0);
178 oshdr.put_sh_info(0);
179 oshdr.put_sh_addralign(0);
180 oshdr.put_sh_entsize(0);
181 }
182
183 v += shdr_size;
184
ead1e424 185 unsigned shndx = 1;
16649710
ILT
186 for (Layout::Segment_list::const_iterator p = this->segment_list_->begin();
187 p != this->segment_list_->end();
61ba1cf9 188 ++p)
593f47df 189 v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
16649710 190 this->layout_, this->secnamepool_, v, &shndx
ead1e424 191 SELECT_SIZE_ENDIAN(size, big_endian));
a3ad94ed 192 for (Layout::Section_list::const_iterator p =
16649710
ILT
193 this->unattached_section_list_->begin();
194 p != this->unattached_section_list_->end();
61ba1cf9
ILT
195 ++p)
196 {
a3ad94ed 197 gold_assert(shndx == (*p)->out_shndx());
61ba1cf9 198 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 199 (*p)->write_header(this->layout_, this->secnamepool_, &oshdr);
61ba1cf9 200 v += shdr_size;
ead1e424 201 ++shndx;
61ba1cf9
ILT
202 }
203
204 of->write_output_view(this->offset(), all_shdrs_size, view);
a2fb1b05
ILT
205}
206
54dc6425
ILT
207// Output_segment_header methods.
208
61ba1cf9 209Output_segment_headers::Output_segment_headers(
61ba1cf9 210 const Layout::Segment_list& segment_list)
9025d29d 211 : segment_list_(segment_list)
61ba1cf9 212{
9025d29d 213 const int size = parameters->get_size();
61ba1cf9
ILT
214 int phdr_size;
215 if (size == 32)
216 phdr_size = elfcpp::Elf_sizes<32>::phdr_size;
217 else if (size == 64)
218 phdr_size = elfcpp::Elf_sizes<64>::phdr_size;
219 else
a3ad94ed 220 gold_unreachable();
61ba1cf9
ILT
221
222 this->set_data_size(segment_list.size() * phdr_size);
223}
224
54dc6425 225void
61ba1cf9 226Output_segment_headers::do_write(Output_file* of)
75f65a3e 227{
9025d29d 228 if (parameters->get_size() == 32)
61ba1cf9 229 {
9025d29d
ILT
230 if (parameters->is_big_endian())
231 {
232#ifdef HAVE_TARGET_32_BIG
233 this->do_sized_write<32, true>(of);
234#else
235 gold_unreachable();
236#endif
237 }
61ba1cf9 238 else
9025d29d
ILT
239 {
240#ifdef HAVE_TARGET_32_LITTLE
61ba1cf9 241 this->do_sized_write<32, false>(of);
9025d29d
ILT
242#else
243 gold_unreachable();
244#endif
245 }
61ba1cf9 246 }
9025d29d 247 else if (parameters->get_size() == 64)
61ba1cf9 248 {
9025d29d
ILT
249 if (parameters->is_big_endian())
250 {
251#ifdef HAVE_TARGET_64_BIG
252 this->do_sized_write<64, true>(of);
253#else
254 gold_unreachable();
255#endif
256 }
61ba1cf9 257 else
9025d29d
ILT
258 {
259#ifdef HAVE_TARGET_64_LITTLE
260 this->do_sized_write<64, false>(of);
261#else
262 gold_unreachable();
263#endif
264 }
61ba1cf9
ILT
265 }
266 else
a3ad94ed 267 gold_unreachable();
61ba1cf9
ILT
268}
269
270template<int size, bool big_endian>
271void
272Output_segment_headers::do_sized_write(Output_file* of)
273{
274 const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size;
275 off_t all_phdrs_size = this->segment_list_.size() * phdr_size;
276 unsigned char* view = of->get_output_view(this->offset(),
277 all_phdrs_size);
278 unsigned char* v = view;
279 for (Layout::Segment_list::const_iterator p = this->segment_list_.begin();
280 p != this->segment_list_.end();
281 ++p)
282 {
283 elfcpp::Phdr_write<size, big_endian> ophdr(v);
284 (*p)->write_header(&ophdr);
285 v += phdr_size;
286 }
287
288 of->write_output_view(this->offset(), all_phdrs_size, view);
75f65a3e
ILT
289}
290
291// Output_file_header methods.
292
9025d29d 293Output_file_header::Output_file_header(const Target* target,
75f65a3e
ILT
294 const Symbol_table* symtab,
295 const Output_segment_headers* osh)
9025d29d 296 : target_(target),
75f65a3e 297 symtab_(symtab),
61ba1cf9 298 segment_header_(osh),
75f65a3e
ILT
299 section_header_(NULL),
300 shstrtab_(NULL)
301{
9025d29d 302 const int size = parameters->get_size();
61ba1cf9
ILT
303 int ehdr_size;
304 if (size == 32)
305 ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size;
306 else if (size == 64)
307 ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
308 else
a3ad94ed 309 gold_unreachable();
61ba1cf9
ILT
310
311 this->set_data_size(ehdr_size);
75f65a3e
ILT
312}
313
314// Set the section table information for a file header.
315
316void
317Output_file_header::set_section_info(const Output_section_headers* shdrs,
318 const Output_section* shstrtab)
319{
320 this->section_header_ = shdrs;
321 this->shstrtab_ = shstrtab;
322}
323
324// Write out the file header.
325
326void
61ba1cf9 327Output_file_header::do_write(Output_file* of)
54dc6425 328{
9025d29d 329 if (parameters->get_size() == 32)
61ba1cf9 330 {
9025d29d
ILT
331 if (parameters->is_big_endian())
332 {
333#ifdef HAVE_TARGET_32_BIG
334 this->do_sized_write<32, true>(of);
335#else
336 gold_unreachable();
337#endif
338 }
61ba1cf9 339 else
9025d29d
ILT
340 {
341#ifdef HAVE_TARGET_32_LITTLE
342 this->do_sized_write<32, false>(of);
343#else
344 gold_unreachable();
345#endif
346 }
61ba1cf9 347 }
9025d29d 348 else if (parameters->get_size() == 64)
61ba1cf9 349 {
9025d29d
ILT
350 if (parameters->is_big_endian())
351 {
352#ifdef HAVE_TARGET_64_BIG
353 this->do_sized_write<64, true>(of);
354#else
355 gold_unreachable();
356#endif
357 }
61ba1cf9 358 else
9025d29d
ILT
359 {
360#ifdef HAVE_TARGET_64_LITTLE
361 this->do_sized_write<64, false>(of);
362#else
363 gold_unreachable();
364#endif
365 }
61ba1cf9
ILT
366 }
367 else
a3ad94ed 368 gold_unreachable();
61ba1cf9
ILT
369}
370
371// Write out the file header with appropriate size and endianess.
372
373template<int size, bool big_endian>
374void
375Output_file_header::do_sized_write(Output_file* of)
376{
a3ad94ed 377 gold_assert(this->offset() == 0);
61ba1cf9
ILT
378
379 int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
380 unsigned char* view = of->get_output_view(0, ehdr_size);
381 elfcpp::Ehdr_write<size, big_endian> oehdr(view);
382
383 unsigned char e_ident[elfcpp::EI_NIDENT];
384 memset(e_ident, 0, elfcpp::EI_NIDENT);
385 e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0;
386 e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1;
387 e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2;
388 e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3;
389 if (size == 32)
390 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32;
391 else if (size == 64)
392 e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64;
393 else
a3ad94ed 394 gold_unreachable();
61ba1cf9
ILT
395 e_ident[elfcpp::EI_DATA] = (big_endian
396 ? elfcpp::ELFDATA2MSB
397 : elfcpp::ELFDATA2LSB);
398 e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT;
399 // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION.
400 oehdr.put_e_ident(e_ident);
401
402 elfcpp::ET e_type;
403 // FIXME: ET_DYN.
7e1edb90 404 if (parameters->output_is_object())
61ba1cf9
ILT
405 e_type = elfcpp::ET_REL;
406 else
407 e_type = elfcpp::ET_EXEC;
408 oehdr.put_e_type(e_type);
409
410 oehdr.put_e_machine(this->target_->machine_code());
411 oehdr.put_e_version(elfcpp::EV_CURRENT);
412
ead1e424 413 // FIXME: Need to support -e, and target specific entry symbol.
61ba1cf9
ILT
414 Symbol* sym = this->symtab_->lookup("_start");
415 typename Sized_symbol<size>::Value_type v;
416 if (sym == NULL)
417 v = 0;
418 else
419 {
420 Sized_symbol<size>* ssym;
593f47df 421 ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) (
5482377d 422 sym SELECT_SIZE(size));
61ba1cf9
ILT
423 v = ssym->value();
424 }
425 oehdr.put_e_entry(v);
426
427 oehdr.put_e_phoff(this->segment_header_->offset());
428 oehdr.put_e_shoff(this->section_header_->offset());
429
430 // FIXME: The target needs to set the flags.
431 oehdr.put_e_flags(0);
432
433 oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size);
434 oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size);
435 oehdr.put_e_phnum(this->segment_header_->data_size()
436 / elfcpp::Elf_sizes<size>::phdr_size);
437 oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size);
438 oehdr.put_e_shnum(this->section_header_->data_size()
439 / elfcpp::Elf_sizes<size>::shdr_size);
ead1e424 440 oehdr.put_e_shstrndx(this->shstrtab_->out_shndx());
61ba1cf9
ILT
441
442 of->write_output_view(0, ehdr_size, view);
54dc6425
ILT
443}
444
dbe717ef
ILT
445// Output_data_const methods.
446
447void
a3ad94ed 448Output_data_const::do_write(Output_file* of)
dbe717ef 449{
a3ad94ed
ILT
450 of->write(this->offset(), this->data_.data(), this->data_.size());
451}
452
453// Output_data_const_buffer methods.
454
455void
456Output_data_const_buffer::do_write(Output_file* of)
457{
458 of->write(this->offset(), this->p_, this->data_size());
dbe717ef
ILT
459}
460
461// Output_section_data methods.
462
16649710
ILT
463// Record the output section, and set the entry size and such.
464
465void
466Output_section_data::set_output_section(Output_section* os)
467{
468 gold_assert(this->output_section_ == NULL);
469 this->output_section_ = os;
470 this->do_adjust_output_section(os);
471}
472
473// Return the section index of the output section.
474
dbe717ef
ILT
475unsigned int
476Output_section_data::do_out_shndx() const
477{
a3ad94ed 478 gold_assert(this->output_section_ != NULL);
dbe717ef
ILT
479 return this->output_section_->out_shndx();
480}
481
a3ad94ed
ILT
482// Output_data_strtab methods.
483
484// Set the address. We don't actually care about the address, but we
485// do set our final size.
486
487void
488Output_data_strtab::do_set_address(uint64_t, off_t)
489{
490 this->strtab_->set_string_offsets();
491 this->set_data_size(this->strtab_->get_strtab_size());
492}
493
494// Write out a string table.
495
496void
497Output_data_strtab::do_write(Output_file* of)
498{
499 this->strtab_->write(of, this->offset());
500}
501
c06b7b0b
ILT
502// Output_reloc methods.
503
504// Get the symbol index of a relocation.
505
506template<bool dynamic, int size, bool big_endian>
507unsigned int
508Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index()
509 const
510{
511 unsigned int index;
512 switch (this->local_sym_index_)
513 {
514 case INVALID_CODE:
a3ad94ed 515 gold_unreachable();
c06b7b0b
ILT
516
517 case GSYM_CODE:
5a6f7e2d 518 if (this->u1_.gsym == NULL)
c06b7b0b
ILT
519 index = 0;
520 else if (dynamic)
5a6f7e2d 521 index = this->u1_.gsym->dynsym_index();
c06b7b0b 522 else
5a6f7e2d 523 index = this->u1_.gsym->symtab_index();
c06b7b0b
ILT
524 break;
525
526 case SECTION_CODE:
527 if (dynamic)
5a6f7e2d 528 index = this->u1_.os->dynsym_index();
c06b7b0b 529 else
5a6f7e2d 530 index = this->u1_.os->symtab_index();
c06b7b0b
ILT
531 break;
532
533 default:
534 if (dynamic)
535 {
536 // FIXME: It seems that some targets may need to generate
537 // dynamic relocations against local symbols for some
538 // reasons. This will have to be addressed at some point.
a3ad94ed 539 gold_unreachable();
c06b7b0b
ILT
540 }
541 else
5a6f7e2d 542 index = this->u1_.relobj->symtab_index(this->local_sym_index_);
c06b7b0b
ILT
543 break;
544 }
a3ad94ed 545 gold_assert(index != -1U);
c06b7b0b
ILT
546 return index;
547}
548
549// Write out the offset and info fields of a Rel or Rela relocation
550// entry.
551
552template<bool dynamic, int size, bool big_endian>
553template<typename Write_rel>
554void
555Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel(
556 Write_rel* wr) const
557{
a3ad94ed 558 Address address = this->address_;
5a6f7e2d
ILT
559 if (this->shndx_ != INVALID_CODE)
560 {
561 off_t off;
562 Output_section* os = this->u2_.relobj->output_section(this->shndx_,
563 &off);
564 gold_assert(os != NULL);
565 address += os->address() + off;
566 }
567 else if (this->u2_.od != NULL)
568 address += this->u2_.od->address();
a3ad94ed 569 wr->put_r_offset(address);
c06b7b0b
ILT
570 wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(),
571 this->type_));
572}
573
574// Write out a Rel relocation.
575
576template<bool dynamic, int size, bool big_endian>
577void
578Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write(
579 unsigned char* pov) const
580{
581 elfcpp::Rel_write<size, big_endian> orel(pov);
582 this->write_rel(&orel);
583}
584
585// Write out a Rela relocation.
586
587template<bool dynamic, int size, bool big_endian>
588void
589Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write(
590 unsigned char* pov) const
591{
592 elfcpp::Rela_write<size, big_endian> orel(pov);
593 this->rel_.write_rel(&orel);
594 orel.put_r_addend(this->addend_);
595}
596
597// Output_data_reloc_base methods.
598
16649710
ILT
599// Adjust the output section.
600
601template<int sh_type, bool dynamic, int size, bool big_endian>
602void
603Output_data_reloc_base<sh_type, dynamic, size, big_endian>
604 ::do_adjust_output_section(Output_section* os)
605{
606 if (sh_type == elfcpp::SHT_REL)
607 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
608 else if (sh_type == elfcpp::SHT_RELA)
609 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
610 else
611 gold_unreachable();
612 if (dynamic)
613 os->set_should_link_to_dynsym();
614 else
615 os->set_should_link_to_symtab();
616}
617
c06b7b0b
ILT
618// Write out relocation data.
619
620template<int sh_type, bool dynamic, int size, bool big_endian>
621void
622Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write(
623 Output_file* of)
624{
625 const off_t off = this->offset();
626 const off_t oview_size = this->data_size();
627 unsigned char* const oview = of->get_output_view(off, oview_size);
628
629 unsigned char* pov = oview;
630 for (typename Relocs::const_iterator p = this->relocs_.begin();
631 p != this->relocs_.end();
632 ++p)
633 {
634 p->write(pov);
635 pov += reloc_size;
636 }
637
a3ad94ed 638 gold_assert(pov - oview == oview_size);
c06b7b0b
ILT
639
640 of->write_output_view(off, oview_size, oview);
641
642 // We no longer need the relocation entries.
643 this->relocs_.clear();
644}
645
dbe717ef 646// Output_data_got::Got_entry methods.
ead1e424
ILT
647
648// Write out the entry.
649
650template<int size, bool big_endian>
651void
7e1edb90 652Output_data_got<size, big_endian>::Got_entry::write(unsigned char* pov) const
ead1e424
ILT
653{
654 Valtype val = 0;
655
656 switch (this->local_sym_index_)
657 {
658 case GSYM_CODE:
659 {
660 Symbol* gsym = this->u_.gsym;
661
662 // If the symbol is resolved locally, we need to write out its
663 // value. Otherwise we just write zero. The target code is
664 // responsible for creating a relocation entry to fill in the
665 // value at runtime.
7e1edb90 666 if (gsym->final_value_is_known())
ead1e424
ILT
667 {
668 Sized_symbol<size>* sgsym;
669 // This cast is a bit ugly. We don't want to put a
670 // virtual method in Symbol, because we want Symbol to be
671 // as small as possible.
672 sgsym = static_cast<Sized_symbol<size>*>(gsym);
673 val = sgsym->value();
674 }
675 }
676 break;
677
678 case CONSTANT_CODE:
679 val = this->u_.constant;
680 break;
681
682 default:
e727fa71
ILT
683 val = this->u_.object->local_symbol_value(this->local_sym_index_);
684 break;
ead1e424
ILT
685 }
686
a3ad94ed 687 elfcpp::Swap<size, big_endian>::writeval(pov, val);
ead1e424
ILT
688}
689
dbe717ef 690// Output_data_got methods.
ead1e424 691
dbe717ef
ILT
692// Add an entry for a global symbol to the GOT. This returns true if
693// this is a new GOT entry, false if the symbol already had a GOT
694// entry.
695
696template<int size, bool big_endian>
697bool
698Output_data_got<size, big_endian>::add_global(Symbol* gsym)
ead1e424 699{
dbe717ef
ILT
700 if (gsym->has_got_offset())
701 return false;
ead1e424 702
dbe717ef
ILT
703 this->entries_.push_back(Got_entry(gsym));
704 this->set_got_size();
705 gsym->set_got_offset(this->last_got_offset());
706 return true;
707}
ead1e424 708
e727fa71
ILT
709// Add an entry for a local symbol to the GOT. This returns true if
710// this is a new GOT entry, false if the symbol already has a GOT
711// entry.
712
713template<int size, bool big_endian>
714bool
715Output_data_got<size, big_endian>::add_local(
716 Sized_relobj<size, big_endian>* object,
717 unsigned int symndx)
718{
719 if (object->local_has_got_offset(symndx))
720 return false;
721 this->entries_.push_back(Got_entry(object, symndx));
722 this->set_got_size();
723 object->set_local_got_offset(symndx, this->last_got_offset());
724 return true;
725}
726
ead1e424
ILT
727// Write out the GOT.
728
729template<int size, bool big_endian>
730void
dbe717ef 731Output_data_got<size, big_endian>::do_write(Output_file* of)
ead1e424
ILT
732{
733 const int add = size / 8;
734
735 const off_t off = this->offset();
c06b7b0b 736 const off_t oview_size = this->data_size();
ead1e424
ILT
737 unsigned char* const oview = of->get_output_view(off, oview_size);
738
739 unsigned char* pov = oview;
740 for (typename Got_entries::const_iterator p = this->entries_.begin();
741 p != this->entries_.end();
742 ++p)
743 {
7e1edb90 744 p->write(pov);
ead1e424
ILT
745 pov += add;
746 }
747
a3ad94ed 748 gold_assert(pov - oview == oview_size);
c06b7b0b 749
ead1e424
ILT
750 of->write_output_view(off, oview_size, oview);
751
752 // We no longer need the GOT entries.
753 this->entries_.clear();
754}
755
a3ad94ed
ILT
756// Output_data_dynamic::Dynamic_entry methods.
757
758// Write out the entry.
759
760template<int size, bool big_endian>
761void
762Output_data_dynamic::Dynamic_entry::write(
763 unsigned char* pov,
1ddbd1e6
ILT
764 const Stringpool* pool
765 ACCEPT_SIZE_ENDIAN) const
a3ad94ed
ILT
766{
767 typename elfcpp::Elf_types<size>::Elf_WXword val;
768 switch (this->classification_)
769 {
770 case DYNAMIC_NUMBER:
771 val = this->u_.val;
772 break;
773
774 case DYNAMIC_SECTION_ADDRESS:
16649710 775 val = this->u_.od->address();
a3ad94ed
ILT
776 break;
777
778 case DYNAMIC_SECTION_SIZE:
16649710 779 val = this->u_.od->data_size();
a3ad94ed
ILT
780 break;
781
782 case DYNAMIC_SYMBOL:
783 {
16649710
ILT
784 const Sized_symbol<size>* s =
785 static_cast<const Sized_symbol<size>*>(this->u_.sym);
a3ad94ed
ILT
786 val = s->value();
787 }
788 break;
789
790 case DYNAMIC_STRING:
791 val = pool->get_offset(this->u_.str);
792 break;
793
794 default:
795 gold_unreachable();
796 }
797
798 elfcpp::Dyn_write<size, big_endian> dw(pov);
799 dw.put_d_tag(this->tag_);
800 dw.put_d_val(val);
801}
802
803// Output_data_dynamic methods.
804
16649710
ILT
805// Adjust the output section to set the entry size.
806
807void
808Output_data_dynamic::do_adjust_output_section(Output_section* os)
809{
9025d29d 810 if (parameters->get_size() == 32)
16649710 811 os->set_entsize(elfcpp::Elf_sizes<32>::dyn_size);
9025d29d 812 else if (parameters->get_size() == 64)
16649710
ILT
813 os->set_entsize(elfcpp::Elf_sizes<64>::dyn_size);
814 else
815 gold_unreachable();
816}
817
a3ad94ed
ILT
818// Set the final data size.
819
820void
821Output_data_dynamic::do_set_address(uint64_t, off_t)
822{
823 // Add the terminating entry.
824 this->add_constant(elfcpp::DT_NULL, 0);
825
826 int dyn_size;
9025d29d 827 if (parameters->get_size() == 32)
a3ad94ed 828 dyn_size = elfcpp::Elf_sizes<32>::dyn_size;
9025d29d 829 else if (parameters->get_size() == 64)
a3ad94ed
ILT
830 dyn_size = elfcpp::Elf_sizes<64>::dyn_size;
831 else
832 gold_unreachable();
833 this->set_data_size(this->entries_.size() * dyn_size);
834}
835
836// Write out the dynamic entries.
837
838void
839Output_data_dynamic::do_write(Output_file* of)
840{
9025d29d 841 if (parameters->get_size() == 32)
a3ad94ed 842 {
9025d29d
ILT
843 if (parameters->is_big_endian())
844 {
845#ifdef HAVE_TARGET_32_BIG
846 this->sized_write<32, true>(of);
847#else
848 gold_unreachable();
849#endif
850 }
a3ad94ed 851 else
9025d29d
ILT
852 {
853#ifdef HAVE_TARGET_32_LITTLE
854 this->sized_write<32, false>(of);
855#else
856 gold_unreachable();
857#endif
858 }
a3ad94ed 859 }
9025d29d 860 else if (parameters->get_size() == 64)
a3ad94ed 861 {
9025d29d
ILT
862 if (parameters->is_big_endian())
863 {
864#ifdef HAVE_TARGET_64_BIG
865 this->sized_write<64, true>(of);
866#else
867 gold_unreachable();
868#endif
869 }
a3ad94ed 870 else
9025d29d
ILT
871 {
872#ifdef HAVE_TARGET_64_LITTLE
873 this->sized_write<64, false>(of);
874#else
875 gold_unreachable();
876#endif
877 }
a3ad94ed
ILT
878 }
879 else
880 gold_unreachable();
881}
882
883template<int size, bool big_endian>
884void
885Output_data_dynamic::sized_write(Output_file* of)
886{
887 const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
888
889 const off_t offset = this->offset();
890 const off_t oview_size = this->data_size();
891 unsigned char* const oview = of->get_output_view(offset, oview_size);
892
893 unsigned char* pov = oview;
894 for (typename Dynamic_entries::const_iterator p = this->entries_.begin();
895 p != this->entries_.end();
896 ++p)
897 {
1ddbd1e6
ILT
898 p->write SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
899 pov, this->pool_ SELECT_SIZE_ENDIAN(size, big_endian));
a3ad94ed
ILT
900 pov += dyn_size;
901 }
902
903 gold_assert(pov - oview == oview_size);
904
905 of->write_output_view(offset, oview_size, oview);
906
907 // We no longer need the dynamic entries.
908 this->entries_.clear();
909}
910
ead1e424
ILT
911// Output_section::Input_section methods.
912
913// Return the data size. For an input section we store the size here.
914// For an Output_section_data, we have to ask it for the size.
915
916off_t
917Output_section::Input_section::data_size() const
918{
919 if (this->is_input_section())
b8e6aad9 920 return this->u1_.data_size;
ead1e424 921 else
b8e6aad9 922 return this->u2_.posd->data_size();
ead1e424
ILT
923}
924
925// Set the address and file offset.
926
927void
928Output_section::Input_section::set_address(uint64_t addr, off_t off,
929 off_t secoff)
930{
931 if (this->is_input_section())
b8e6aad9 932 this->u2_.object->set_section_offset(this->shndx_, off - secoff);
ead1e424 933 else
b8e6aad9
ILT
934 this->u2_.posd->set_address(addr, off);
935}
936
937// Try to turn an input address into an output address.
938
939bool
940Output_section::Input_section::output_address(const Relobj* object,
941 unsigned int shndx,
942 off_t offset,
943 uint64_t output_section_address,
944 uint64_t *poutput) const
945{
946 if (!this->is_input_section())
947 return this->u2_.posd->output_address(object, shndx, offset,
948 output_section_address, poutput);
949 else
950 {
9d7094e8
ILT
951 if (this->shndx_ != shndx
952 || this->u2_.object != object)
b8e6aad9
ILT
953 return false;
954 off_t output_offset;
955 Output_section* os = object->output_section(shndx, &output_offset);
956 gold_assert(os != NULL);
957 *poutput = output_section_address + output_offset + offset;
958 return true;
959 }
ead1e424
ILT
960}
961
962// Write out the data. We don't have to do anything for an input
963// section--they are handled via Object::relocate--but this is where
964// we write out the data for an Output_section_data.
965
966void
967Output_section::Input_section::write(Output_file* of)
968{
969 if (!this->is_input_section())
b8e6aad9 970 this->u2_.posd->write(of);
ead1e424
ILT
971}
972
a2fb1b05
ILT
973// Output_section methods.
974
975// Construct an Output_section. NAME will point into a Stringpool.
976
977Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
b8e6aad9 978 elfcpp::Elf_Xword flags)
a2fb1b05 979 : name_(name),
a2fb1b05
ILT
980 addralign_(0),
981 entsize_(0),
16649710 982 link_section_(NULL),
a2fb1b05 983 link_(0),
16649710 984 info_section_(NULL),
a2fb1b05
ILT
985 info_(0),
986 type_(type),
61ba1cf9 987 flags_(flags),
ead1e424 988 out_shndx_(0),
c06b7b0b
ILT
989 symtab_index_(0),
990 dynsym_index_(0),
ead1e424
ILT
991 input_sections_(),
992 first_input_offset_(0),
c51e6221 993 fills_(),
a3ad94ed 994 needs_symtab_index_(false),
16649710
ILT
995 needs_dynsym_index_(false),
996 should_link_to_symtab_(false),
997 should_link_to_dynsym_(false)
a2fb1b05
ILT
998{
999}
1000
54dc6425
ILT
1001Output_section::~Output_section()
1002{
1003}
1004
16649710
ILT
1005// Set the entry size.
1006
1007void
1008Output_section::set_entsize(uint64_t v)
1009{
1010 if (this->entsize_ == 0)
1011 this->entsize_ = v;
1012 else
1013 gold_assert(this->entsize_ == v);
1014}
1015
ead1e424
ILT
1016// Add the input section SHNDX, with header SHDR, named SECNAME, in
1017// OBJECT, to the Output_section. Return the offset of the input
1018// section within the output section. We don't always keep track of
a2fb1b05
ILT
1019// input sections for an Output_section. Instead, each Object keeps
1020// track of the Output_section for each of its input sections.
1021
1022template<int size, bool big_endian>
1023off_t
f6ce93d6 1024Output_section::add_input_section(Relobj* object, unsigned int shndx,
ead1e424 1025 const char* secname,
a2fb1b05
ILT
1026 const elfcpp::Shdr<size, big_endian>& shdr)
1027{
1028 elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
1029 if ((addralign & (addralign - 1)) != 0)
1030 {
1031 fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
1032 program_name, object->name().c_str(),
1033 static_cast<unsigned long>(addralign), secname);
1034 gold_exit(false);
1035 }
a2fb1b05
ILT
1036
1037 if (addralign > this->addralign_)
1038 this->addralign_ = addralign;
1039
b8e6aad9
ILT
1040 // If this is a SHF_MERGE section, we pass all the input sections to
1041 // a Output_data_merge.
1042 if ((shdr.get_sh_flags() & elfcpp::SHF_MERGE) != 0)
1043 {
1044 if (this->add_merge_input_section(object, shndx, shdr.get_sh_flags(),
1045 shdr.get_sh_entsize(),
1046 addralign))
1047 {
1048 // Tell the relocation routines that they need to call the
1049 // output_address method to determine the final address.
1050 return -1;
1051 }
1052 }
1053
c51e6221
ILT
1054 off_t offset_in_section = this->data_size();
1055 off_t aligned_offset_in_section = align_address(offset_in_section,
1056 addralign);
1057
1058 if (aligned_offset_in_section > offset_in_section
1059 && (shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0
1060 && object->target()->has_code_fill())
1061 {
1062 // We need to add some fill data. Using fill_list_ when
1063 // possible is an optimization, since we will often have fill
1064 // sections without input sections.
1065 off_t fill_len = aligned_offset_in_section - offset_in_section;
1066 if (this->input_sections_.empty())
1067 this->fills_.push_back(Fill(offset_in_section, fill_len));
1068 else
1069 {
1070 // FIXME: When relaxing, the size needs to adjust to
1071 // maintain a constant alignment.
1072 std::string fill_data(object->target()->code_fill(fill_len));
1073 Output_data_const* odc = new Output_data_const(fill_data, 1);
1074 this->input_sections_.push_back(Input_section(odc));
1075 }
1076 }
1077
1078 this->set_data_size(aligned_offset_in_section + shdr.get_sh_size());
a2fb1b05 1079
ead1e424
ILT
1080 // We need to keep track of this section if we are already keeping
1081 // track of sections, or if we are relaxing. FIXME: Add test for
1082 // relaxing.
c51e6221 1083 if (!this->input_sections_.empty())
ead1e424
ILT
1084 this->input_sections_.push_back(Input_section(object, shndx,
1085 shdr.get_sh_size(),
1086 addralign));
54dc6425 1087
c51e6221 1088 return aligned_offset_in_section;
61ba1cf9
ILT
1089}
1090
ead1e424
ILT
1091// Add arbitrary data to an output section.
1092
1093void
1094Output_section::add_output_section_data(Output_section_data* posd)
1095{
b8e6aad9
ILT
1096 Input_section inp(posd);
1097 this->add_output_section_data(&inp);
1098}
1099
1100// Add arbitrary data to an output section by Input_section.
c06b7b0b 1101
b8e6aad9
ILT
1102void
1103Output_section::add_output_section_data(Input_section* inp)
1104{
ead1e424
ILT
1105 if (this->input_sections_.empty())
1106 this->first_input_offset_ = this->data_size();
c06b7b0b 1107
b8e6aad9 1108 this->input_sections_.push_back(*inp);
c06b7b0b 1109
b8e6aad9 1110 uint64_t addralign = inp->addralign();
ead1e424
ILT
1111 if (addralign > this->addralign_)
1112 this->addralign_ = addralign;
c06b7b0b 1113
b8e6aad9
ILT
1114 inp->set_output_section(this);
1115}
1116
1117// Add a merge section to an output section.
1118
1119void
1120Output_section::add_output_merge_section(Output_section_data* posd,
1121 bool is_string, uint64_t entsize)
1122{
1123 Input_section inp(posd, is_string, entsize);
1124 this->add_output_section_data(&inp);
1125}
1126
1127// Add an input section to a SHF_MERGE section.
1128
1129bool
1130Output_section::add_merge_input_section(Relobj* object, unsigned int shndx,
1131 uint64_t flags, uint64_t entsize,
1132 uint64_t addralign)
1133{
1134 // We only merge constants if the alignment is not more than the
1135 // entry size. This could be handled, but it's unusual.
1136 if (addralign > entsize)
1137 return false;
1138
1139 bool is_string = (flags & elfcpp::SHF_STRINGS) != 0;
1140 Input_section_list::iterator p;
1141 for (p = this->input_sections_.begin();
1142 p != this->input_sections_.end();
1143 ++p)
1144 if (p->is_merge_section(is_string, entsize))
1145 break;
1146
1147 // We handle the actual constant merging in Output_merge_data or
1148 // Output_merge_string_data.
1149 if (p != this->input_sections_.end())
1150 p->add_input_section(object, shndx);
1151 else
1152 {
1153 Output_section_data* posd;
1154 if (!is_string)
1155 posd = new Output_merge_data(entsize);
1156 else if (entsize == 1)
1157 posd = new Output_merge_string<char>();
1158 else if (entsize == 2)
1159 posd = new Output_merge_string<uint16_t>();
1160 else if (entsize == 4)
1161 posd = new Output_merge_string<uint32_t>();
1162 else
1163 return false;
1164
1165 this->add_output_merge_section(posd, is_string, entsize);
1166 posd->add_input_section(object, shndx);
1167 }
1168
1169 return true;
1170}
1171
1172// Return the output virtual address of OFFSET relative to the start
1173// of input section SHNDX in object OBJECT.
1174
1175uint64_t
1176Output_section::output_address(const Relobj* object, unsigned int shndx,
1177 off_t offset) const
1178{
1179 uint64_t addr = this->address() + this->first_input_offset_;
1180 for (Input_section_list::const_iterator p = this->input_sections_.begin();
1181 p != this->input_sections_.end();
1182 ++p)
1183 {
1184 addr = align_address(addr, p->addralign());
1185 uint64_t output;
1186 if (p->output_address(object, shndx, offset, addr, &output))
1187 return output;
1188 addr += p->data_size();
1189 }
1190
1191 // If we get here, it means that we don't know the mapping for this
1192 // input section. This might happen in principle if
1193 // add_input_section were called before add_output_section_data.
1194 // But it should never actually happen.
1195
1196 gold_unreachable();
ead1e424
ILT
1197}
1198
1199// Set the address of an Output_section. This is where we handle
1200// setting the addresses of any Output_section_data objects.
1201
1202void
1203Output_section::do_set_address(uint64_t address, off_t startoff)
1204{
1205 if (this->input_sections_.empty())
1206 return;
1207
1208 off_t off = startoff + this->first_input_offset_;
1209 for (Input_section_list::iterator p = this->input_sections_.begin();
1210 p != this->input_sections_.end();
1211 ++p)
1212 {
1213 off = align_address(off, p->addralign());
1214 p->set_address(address + (off - startoff), off, startoff);
1215 off += p->data_size();
1216 }
1217
1218 this->set_data_size(off - startoff);
1219}
1220
61ba1cf9
ILT
1221// Write the section header to *OSHDR.
1222
1223template<int size, bool big_endian>
1224void
16649710
ILT
1225Output_section::write_header(const Layout* layout,
1226 const Stringpool* secnamepool,
61ba1cf9
ILT
1227 elfcpp::Shdr_write<size, big_endian>* oshdr) const
1228{
1229 oshdr->put_sh_name(secnamepool->get_offset(this->name_));
1230 oshdr->put_sh_type(this->type_);
1231 oshdr->put_sh_flags(this->flags_);
1232 oshdr->put_sh_addr(this->address());
1233 oshdr->put_sh_offset(this->offset());
1234 oshdr->put_sh_size(this->data_size());
16649710
ILT
1235 if (this->link_section_ != NULL)
1236 oshdr->put_sh_link(this->link_section_->out_shndx());
1237 else if (this->should_link_to_symtab_)
1238 oshdr->put_sh_link(layout->symtab_section()->out_shndx());
1239 else if (this->should_link_to_dynsym_)
1240 oshdr->put_sh_link(layout->dynsym_section()->out_shndx());
1241 else
1242 oshdr->put_sh_link(this->link_);
1243 if (this->info_section_ != NULL)
1244 oshdr->put_sh_info(this->info_section_->out_shndx());
1245 else
1246 oshdr->put_sh_info(this->info_);
61ba1cf9
ILT
1247 oshdr->put_sh_addralign(this->addralign_);
1248 oshdr->put_sh_entsize(this->entsize_);
a2fb1b05
ILT
1249}
1250
ead1e424
ILT
1251// Write out the data. For input sections the data is written out by
1252// Object::relocate, but we have to handle Output_section_data objects
1253// here.
1254
1255void
1256Output_section::do_write(Output_file* of)
1257{
c51e6221
ILT
1258 off_t output_section_file_offset = this->offset();
1259 for (Fill_list::iterator p = this->fills_.begin();
1260 p != this->fills_.end();
1261 ++p)
1262 {
1263 std::string fill_data(of->target()->code_fill(p->length()));
1264 of->write(output_section_file_offset + p->section_offset(),
1265 fill_data.data(), fill_data.size());
1266 }
1267
ead1e424
ILT
1268 for (Input_section_list::iterator p = this->input_sections_.begin();
1269 p != this->input_sections_.end();
1270 ++p)
1271 p->write(of);
1272}
1273
a2fb1b05
ILT
1274// Output segment methods.
1275
1276Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
54dc6425 1277 : output_data_(),
75f65a3e 1278 output_bss_(),
a2fb1b05
ILT
1279 vaddr_(0),
1280 paddr_(0),
1281 memsz_(0),
1282 align_(0),
1283 offset_(0),
1284 filesz_(0),
1285 type_(type),
ead1e424
ILT
1286 flags_(flags),
1287 is_align_known_(false)
a2fb1b05
ILT
1288{
1289}
1290
1291// Add an Output_section to an Output_segment.
1292
1293void
75f65a3e 1294Output_segment::add_output_section(Output_section* os,
dbe717ef
ILT
1295 elfcpp::Elf_Word seg_flags,
1296 bool front)
a2fb1b05 1297{
a3ad94ed
ILT
1298 gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
1299 gold_assert(!this->is_align_known_);
75f65a3e 1300
ead1e424 1301 // Update the segment flags.
75f65a3e 1302 this->flags_ |= seg_flags;
75f65a3e
ILT
1303
1304 Output_segment::Output_data_list* pdl;
1305 if (os->type() == elfcpp::SHT_NOBITS)
1306 pdl = &this->output_bss_;
1307 else
1308 pdl = &this->output_data_;
54dc6425 1309
a2fb1b05
ILT
1310 // So that PT_NOTE segments will work correctly, we need to ensure
1311 // that all SHT_NOTE sections are adjacent. This will normally
1312 // happen automatically, because all the SHT_NOTE input sections
1313 // will wind up in the same output section. However, it is possible
1314 // for multiple SHT_NOTE input sections to have different section
1315 // flags, and thus be in different output sections, but for the
1316 // different section flags to map into the same segment flags and
1317 // thus the same output segment.
54dc6425
ILT
1318
1319 // Note that while there may be many input sections in an output
1320 // section, there are normally only a few output sections in an
1321 // output segment. This loop is expected to be fast.
1322
61ba1cf9 1323 if (os->type() == elfcpp::SHT_NOTE && !pdl->empty())
a2fb1b05 1324 {
a3ad94ed 1325 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 1326 do
54dc6425 1327 {
75f65a3e 1328 --p;
54dc6425
ILT
1329 if ((*p)->is_section_type(elfcpp::SHT_NOTE))
1330 {
dbe717ef 1331 // We don't worry about the FRONT parameter.
54dc6425 1332 ++p;
75f65a3e 1333 pdl->insert(p, os);
54dc6425
ILT
1334 return;
1335 }
1336 }
75f65a3e 1337 while (p != pdl->begin());
54dc6425
ILT
1338 }
1339
1340 // Similarly, so that PT_TLS segments will work, we need to group
75f65a3e
ILT
1341 // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special
1342 // case: we group the SHF_TLS/SHT_NOBITS sections right after the
1343 // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS
1344 // correctly.
61ba1cf9 1345 if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty())
54dc6425 1346 {
75f65a3e
ILT
1347 pdl = &this->output_data_;
1348 bool nobits = os->type() == elfcpp::SHT_NOBITS;
ead1e424 1349 bool sawtls = false;
a3ad94ed 1350 Output_segment::Output_data_list::iterator p = pdl->end();
75f65a3e 1351 do
a2fb1b05 1352 {
75f65a3e 1353 --p;
ead1e424
ILT
1354 bool insert;
1355 if ((*p)->is_section_flag_set(elfcpp::SHF_TLS))
1356 {
1357 sawtls = true;
1358 // Put a NOBITS section after the first TLS section.
1359 // But a PROGBITS section after the first TLS/PROGBITS
1360 // section.
1361 insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS);
1362 }
1363 else
1364 {
1365 // If we've gone past the TLS sections, but we've seen a
1366 // TLS section, then we need to insert this section now.
1367 insert = sawtls;
1368 }
1369
1370 if (insert)
a2fb1b05 1371 {
dbe717ef 1372 // We don't worry about the FRONT parameter.
a2fb1b05 1373 ++p;
75f65a3e 1374 pdl->insert(p, os);
a2fb1b05
ILT
1375 return;
1376 }
1377 }
75f65a3e 1378 while (p != pdl->begin());
ead1e424 1379
dbe717ef
ILT
1380 // There are no TLS sections yet; put this one at the requested
1381 // location in the section list.
a2fb1b05
ILT
1382 }
1383
dbe717ef
ILT
1384 if (front)
1385 pdl->push_front(os);
1386 else
1387 pdl->push_back(os);
75f65a3e
ILT
1388}
1389
1390// Add an Output_data (which is not an Output_section) to the start of
1391// a segment.
1392
1393void
1394Output_segment::add_initial_output_data(Output_data* od)
1395{
a3ad94ed 1396 gold_assert(!this->is_align_known_);
75f65a3e
ILT
1397 this->output_data_.push_front(od);
1398}
1399
1400// Return the maximum alignment of the Output_data in Output_segment.
ead1e424 1401// Once we compute this, we prohibit new sections from being added.
75f65a3e
ILT
1402
1403uint64_t
ead1e424 1404Output_segment::addralign()
75f65a3e 1405{
ead1e424
ILT
1406 if (!this->is_align_known_)
1407 {
1408 uint64_t addralign;
1409
1410 addralign = Output_segment::maximum_alignment(&this->output_data_);
1411 if (addralign > this->align_)
1412 this->align_ = addralign;
1413
1414 addralign = Output_segment::maximum_alignment(&this->output_bss_);
1415 if (addralign > this->align_)
1416 this->align_ = addralign;
1417
1418 this->is_align_known_ = true;
1419 }
1420
75f65a3e
ILT
1421 return this->align_;
1422}
1423
ead1e424
ILT
1424// Return the maximum alignment of a list of Output_data.
1425
1426uint64_t
1427Output_segment::maximum_alignment(const Output_data_list* pdl)
1428{
1429 uint64_t ret = 0;
1430 for (Output_data_list::const_iterator p = pdl->begin();
1431 p != pdl->end();
1432 ++p)
1433 {
1434 uint64_t addralign = (*p)->addralign();
1435 if (addralign > ret)
1436 ret = addralign;
1437 }
1438 return ret;
1439}
1440
75f65a3e 1441// Set the section addresses for an Output_segment. ADDR is the
ead1e424
ILT
1442// address and *POFF is the file offset. Set the section indexes
1443// starting with *PSHNDX. Return the address of the immediately
1444// following segment. Update *POFF and *PSHNDX.
75f65a3e
ILT
1445
1446uint64_t
ead1e424
ILT
1447Output_segment::set_section_addresses(uint64_t addr, off_t* poff,
1448 unsigned int* pshndx)
75f65a3e 1449{
a3ad94ed 1450 gold_assert(this->type_ == elfcpp::PT_LOAD);
75f65a3e
ILT
1451
1452 this->vaddr_ = addr;
1453 this->paddr_ = addr;
1454
1455 off_t orig_off = *poff;
1456 this->offset_ = orig_off;
1457
ead1e424
ILT
1458 *poff = align_address(*poff, this->addralign());
1459
1460 addr = this->set_section_list_addresses(&this->output_data_, addr, poff,
1461 pshndx);
75f65a3e
ILT
1462 this->filesz_ = *poff - orig_off;
1463
1464 off_t off = *poff;
1465
61ba1cf9 1466 uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr,
ead1e424 1467 poff, pshndx);
75f65a3e
ILT
1468 this->memsz_ = *poff - orig_off;
1469
1470 // Ignore the file offset adjustments made by the BSS Output_data
1471 // objects.
1472 *poff = off;
61ba1cf9
ILT
1473
1474 return ret;
75f65a3e
ILT
1475}
1476
b8e6aad9
ILT
1477// Set the addresses and file offsets in a list of Output_data
1478// structures.
75f65a3e
ILT
1479
1480uint64_t
1481Output_segment::set_section_list_addresses(Output_data_list* pdl,
ead1e424
ILT
1482 uint64_t addr, off_t* poff,
1483 unsigned int* pshndx)
75f65a3e 1484{
ead1e424 1485 off_t startoff = *poff;
75f65a3e 1486
ead1e424 1487 off_t off = startoff;
75f65a3e
ILT
1488 for (Output_data_list::iterator p = pdl->begin();
1489 p != pdl->end();
1490 ++p)
1491 {
ead1e424
ILT
1492 off = align_address(off, (*p)->addralign());
1493 (*p)->set_address(addr + (off - startoff), off);
1494
1495 // Unless this is a PT_TLS segment, we want to ignore the size
1496 // of a SHF_TLS/SHT_NOBITS section. Such a section does not
1497 // affect the size of a PT_LOAD segment.
1498 if (this->type_ == elfcpp::PT_TLS
1499 || !(*p)->is_section_flag_set(elfcpp::SHF_TLS)
1500 || !(*p)->is_section_type(elfcpp::SHT_NOBITS))
1501 off += (*p)->data_size();
75f65a3e 1502
ead1e424
ILT
1503 if ((*p)->is_section())
1504 {
1505 (*p)->set_out_shndx(*pshndx);
1506 ++*pshndx;
1507 }
75f65a3e
ILT
1508 }
1509
1510 *poff = off;
ead1e424 1511 return addr + (off - startoff);
75f65a3e
ILT
1512}
1513
1514// For a non-PT_LOAD segment, set the offset from the sections, if
1515// any.
1516
1517void
1518Output_segment::set_offset()
1519{
a3ad94ed 1520 gold_assert(this->type_ != elfcpp::PT_LOAD);
75f65a3e
ILT
1521
1522 if (this->output_data_.empty() && this->output_bss_.empty())
1523 {
1524 this->vaddr_ = 0;
1525 this->paddr_ = 0;
1526 this->memsz_ = 0;
1527 this->align_ = 0;
1528 this->offset_ = 0;
1529 this->filesz_ = 0;
1530 return;
1531 }
1532
1533 const Output_data* first;
1534 if (this->output_data_.empty())
1535 first = this->output_bss_.front();
1536 else
1537 first = this->output_data_.front();
1538 this->vaddr_ = first->address();
1539 this->paddr_ = this->vaddr_;
1540 this->offset_ = first->offset();
1541
1542 if (this->output_data_.empty())
1543 this->filesz_ = 0;
1544 else
1545 {
1546 const Output_data* last_data = this->output_data_.back();
1547 this->filesz_ = (last_data->address()
1548 + last_data->data_size()
1549 - this->vaddr_);
1550 }
1551
1552 const Output_data* last;
1553 if (this->output_bss_.empty())
1554 last = this->output_data_.back();
1555 else
1556 last = this->output_bss_.back();
1557 this->memsz_ = (last->address()
1558 + last->data_size()
1559 - this->vaddr_);
75f65a3e
ILT
1560}
1561
1562// Return the number of Output_sections in an Output_segment.
1563
1564unsigned int
1565Output_segment::output_section_count() const
1566{
1567 return (this->output_section_count_list(&this->output_data_)
1568 + this->output_section_count_list(&this->output_bss_));
1569}
1570
1571// Return the number of Output_sections in an Output_data_list.
1572
1573unsigned int
1574Output_segment::output_section_count_list(const Output_data_list* pdl) const
1575{
1576 unsigned int count = 0;
1577 for (Output_data_list::const_iterator p = pdl->begin();
1578 p != pdl->end();
1579 ++p)
1580 {
1581 if ((*p)->is_section())
1582 ++count;
1583 }
1584 return count;
a2fb1b05
ILT
1585}
1586
61ba1cf9
ILT
1587// Write the segment data into *OPHDR.
1588
1589template<int size, bool big_endian>
1590void
ead1e424 1591Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr)
61ba1cf9
ILT
1592{
1593 ophdr->put_p_type(this->type_);
1594 ophdr->put_p_offset(this->offset_);
1595 ophdr->put_p_vaddr(this->vaddr_);
1596 ophdr->put_p_paddr(this->paddr_);
1597 ophdr->put_p_filesz(this->filesz_);
1598 ophdr->put_p_memsz(this->memsz_);
1599 ophdr->put_p_flags(this->flags_);
ead1e424 1600 ophdr->put_p_align(this->addralign());
61ba1cf9
ILT
1601}
1602
1603// Write the section headers into V.
1604
1605template<int size, bool big_endian>
1606unsigned char*
16649710
ILT
1607Output_segment::write_section_headers(const Layout* layout,
1608 const Stringpool* secnamepool,
ead1e424
ILT
1609 unsigned char* v,
1610 unsigned int *pshndx
5482377d
ILT
1611 ACCEPT_SIZE_ENDIAN) const
1612{
ead1e424
ILT
1613 // Every section that is attached to a segment must be attached to a
1614 // PT_LOAD segment, so we only write out section headers for PT_LOAD
1615 // segments.
1616 if (this->type_ != elfcpp::PT_LOAD)
1617 return v;
1618
593f47df
ILT
1619 v = this->write_section_headers_list
1620 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
16649710 1621 layout, secnamepool, &this->output_data_, v, pshndx
593f47df
ILT
1622 SELECT_SIZE_ENDIAN(size, big_endian));
1623 v = this->write_section_headers_list
1624 SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
16649710 1625 layout, secnamepool, &this->output_bss_, v, pshndx
593f47df 1626 SELECT_SIZE_ENDIAN(size, big_endian));
61ba1cf9
ILT
1627 return v;
1628}
1629
1630template<int size, bool big_endian>
1631unsigned char*
16649710
ILT
1632Output_segment::write_section_headers_list(const Layout* layout,
1633 const Stringpool* secnamepool,
61ba1cf9 1634 const Output_data_list* pdl,
ead1e424
ILT
1635 unsigned char* v,
1636 unsigned int* pshndx
5482377d 1637 ACCEPT_SIZE_ENDIAN) const
61ba1cf9
ILT
1638{
1639 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1640 for (Output_data_list::const_iterator p = pdl->begin();
1641 p != pdl->end();
1642 ++p)
1643 {
1644 if ((*p)->is_section())
1645 {
5482377d 1646 const Output_section* ps = static_cast<const Output_section*>(*p);
a3ad94ed 1647 gold_assert(*pshndx == ps->out_shndx());
61ba1cf9 1648 elfcpp::Shdr_write<size, big_endian> oshdr(v);
16649710 1649 ps->write_header(layout, secnamepool, &oshdr);
61ba1cf9 1650 v += shdr_size;
ead1e424 1651 ++*pshndx;
61ba1cf9
ILT
1652 }
1653 }
1654 return v;
1655}
1656
a2fb1b05
ILT
1657// Output_file methods.
1658
c51e6221 1659Output_file::Output_file(const General_options& options, Target* target)
61ba1cf9 1660 : options_(options),
c51e6221 1661 target_(target),
61ba1cf9
ILT
1662 name_(options.output_file_name()),
1663 o_(-1),
1664 file_size_(0),
1665 base_(NULL)
1666{
1667}
1668
1669// Open the output file.
1670
a2fb1b05 1671void
61ba1cf9 1672Output_file::open(off_t file_size)
a2fb1b05 1673{
61ba1cf9
ILT
1674 this->file_size_ = file_size;
1675
4e9d8586
ILT
1676 // Unlink the file first; otherwise the open() may fail if the file
1677 // is busy (e.g. it's an executable that's currently being executed).
1678 //
1679 // However, the linker may be part of a system where a zero-length
1680 // file is created for it to write to, with tight permissions (gcc
1681 // 2.95 did something like this). Unlinking the file would work
1682 // around those permission controls, so we only unlink if the file
1683 // has a non-zero size. We also unlink only regular files to avoid
1684 // trouble with directories/etc.
1685 //
1686 // If we fail, continue; this command is merely a best-effort attempt
1687 // to improve the odds for open().
1688
1689 // FIXME: unlink the file if it's a symlink, even a symlink to a dir.
1690 // Or do we want to follow the symlink and unlink its target?
1691 struct stat s;
1692 if (::stat(this->name_, &s) == 0 && s.st_size != 0 && S_ISREG(s.st_mode))
1693 ::unlink(this->name_);
1694
7e1edb90 1695 int mode = parameters->output_is_object() ? 0666 : 0777;
61ba1cf9
ILT
1696 int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode);
1697 if (o < 0)
1698 {
1699 fprintf(stderr, _("%s: %s: open: %s\n"),
1700 program_name, this->name_, strerror(errno));
1701 gold_exit(false);
1702 }
1703 this->o_ = o;
1704
1705 // Write out one byte to make the file the right size.
1706 if (::lseek(o, file_size - 1, SEEK_SET) < 0)
1707 {
1708 fprintf(stderr, _("%s: %s: lseek: %s\n"),
1709 program_name, this->name_, strerror(errno));
1710 gold_exit(false);
1711 }
1712 char b = 0;
1713 if (::write(o, &b, 1) != 1)
1714 {
1715 fprintf(stderr, _("%s: %s: write: %s\n"),
1716 program_name, this->name_, strerror(errno));
1717 gold_exit(false);
1718 }
1719
1720 // Map the file into memory.
1721 void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE,
1722 MAP_SHARED, o, 0);
1723 if (base == MAP_FAILED)
1724 {
1725 fprintf(stderr, _("%s: %s: mmap: %s\n"),
1726 program_name, this->name_, strerror(errno));
1727 gold_exit(false);
1728 }
1729 this->base_ = static_cast<unsigned char*>(base);
1730}
1731
1732// Close the output file.
1733
1734void
1735Output_file::close()
1736{
1737 if (::munmap(this->base_, this->file_size_) < 0)
1738 {
1739 fprintf(stderr, _("%s: %s: munmap: %s\n"),
1740 program_name, this->name_, strerror(errno));
1741 gold_exit(false);
1742 }
1743 this->base_ = NULL;
1744
1745 if (::close(this->o_) < 0)
1746 {
1747 fprintf(stderr, _("%s: %s: close: %s\n"),
1748 program_name, this->name_, strerror(errno));
1749 gold_exit(false);
1750 }
1751 this->o_ = -1;
a2fb1b05
ILT
1752}
1753
1754// Instantiate the templates we need. We could use the configure
1755// script to restrict this to only the ones for implemented targets.
1756
193a53d9 1757#ifdef HAVE_TARGET_32_LITTLE
a2fb1b05
ILT
1758template
1759off_t
1760Output_section::add_input_section<32, false>(
f6ce93d6 1761 Relobj* object,
ead1e424 1762 unsigned int shndx,
a2fb1b05
ILT
1763 const char* secname,
1764 const elfcpp::Shdr<32, false>& shdr);
193a53d9 1765#endif
a2fb1b05 1766
193a53d9 1767#ifdef HAVE_TARGET_32_BIG
a2fb1b05
ILT
1768template
1769off_t
1770Output_section::add_input_section<32, true>(
f6ce93d6 1771 Relobj* object,
ead1e424 1772 unsigned int shndx,
a2fb1b05
ILT
1773 const char* secname,
1774 const elfcpp::Shdr<32, true>& shdr);
193a53d9 1775#endif
a2fb1b05 1776
193a53d9 1777#ifdef HAVE_TARGET_64_LITTLE
a2fb1b05
ILT
1778template
1779off_t
1780Output_section::add_input_section<64, false>(
f6ce93d6 1781 Relobj* object,
ead1e424 1782 unsigned int shndx,
a2fb1b05
ILT
1783 const char* secname,
1784 const elfcpp::Shdr<64, false>& shdr);
193a53d9 1785#endif
a2fb1b05 1786
193a53d9 1787#ifdef HAVE_TARGET_64_BIG
a2fb1b05
ILT
1788template
1789off_t
1790Output_section::add_input_section<64, true>(
f6ce93d6 1791 Relobj* object,
ead1e424 1792 unsigned int shndx,
a2fb1b05
ILT
1793 const char* secname,
1794 const elfcpp::Shdr<64, true>& shdr);
193a53d9 1795#endif
a2fb1b05 1796
193a53d9 1797#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
1798template
1799class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>;
193a53d9 1800#endif
c06b7b0b 1801
193a53d9 1802#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
1803template
1804class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>;
193a53d9 1805#endif
c06b7b0b 1806
193a53d9 1807#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
1808template
1809class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>;
193a53d9 1810#endif
c06b7b0b 1811
193a53d9 1812#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
1813template
1814class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>;
193a53d9 1815#endif
c06b7b0b 1816
193a53d9 1817#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
1818template
1819class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>;
193a53d9 1820#endif
c06b7b0b 1821
193a53d9 1822#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
1823template
1824class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>;
193a53d9 1825#endif
c06b7b0b 1826
193a53d9 1827#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
1828template
1829class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>;
193a53d9 1830#endif
c06b7b0b 1831
193a53d9 1832#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
1833template
1834class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>;
193a53d9 1835#endif
c06b7b0b 1836
193a53d9 1837#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
1838template
1839class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>;
193a53d9 1840#endif
c06b7b0b 1841
193a53d9 1842#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
1843template
1844class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>;
193a53d9 1845#endif
c06b7b0b 1846
193a53d9 1847#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
1848template
1849class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>;
193a53d9 1850#endif
c06b7b0b 1851
193a53d9 1852#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
1853template
1854class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>;
193a53d9 1855#endif
c06b7b0b 1856
193a53d9 1857#ifdef HAVE_TARGET_32_LITTLE
c06b7b0b
ILT
1858template
1859class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>;
193a53d9 1860#endif
c06b7b0b 1861
193a53d9 1862#ifdef HAVE_TARGET_32_BIG
c06b7b0b
ILT
1863template
1864class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>;
193a53d9 1865#endif
c06b7b0b 1866
193a53d9 1867#ifdef HAVE_TARGET_64_LITTLE
c06b7b0b
ILT
1868template
1869class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>;
193a53d9 1870#endif
c06b7b0b 1871
193a53d9 1872#ifdef HAVE_TARGET_64_BIG
c06b7b0b
ILT
1873template
1874class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>;
193a53d9 1875#endif
c06b7b0b 1876
193a53d9 1877#ifdef HAVE_TARGET_32_LITTLE
ead1e424 1878template
dbe717ef 1879class Output_data_got<32, false>;
193a53d9 1880#endif
ead1e424 1881
193a53d9 1882#ifdef HAVE_TARGET_32_BIG
ead1e424 1883template
dbe717ef 1884class Output_data_got<32, true>;
193a53d9 1885#endif
ead1e424 1886
193a53d9 1887#ifdef HAVE_TARGET_64_LITTLE
ead1e424 1888template
dbe717ef 1889class Output_data_got<64, false>;
193a53d9 1890#endif
ead1e424 1891
193a53d9 1892#ifdef HAVE_TARGET_64_BIG
ead1e424 1893template
dbe717ef 1894class Output_data_got<64, true>;
193a53d9 1895#endif
ead1e424 1896
a2fb1b05 1897} // End namespace gold.
This page took 0.141595 seconds and 4 git commands to generate.