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