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