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