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