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