* testsuite/Makefile.am (justsyms_2r.o): Add dependency on
[deliverable/binutils-gdb.git] / gold / output.h
CommitLineData
a2fb1b05
ILT
1// output.h -- manage the output file for gold -*- C++ -*-
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
a2fb1b05
ILT
23#ifndef GOLD_OUTPUT_H
24#define GOLD_OUTPUT_H
25
26#include <list>
ead1e424 27#include <vector>
a2fb1b05
ILT
28
29#include "elfcpp.h"
54dc6425 30#include "layout.h"
c06b7b0b 31#include "reloc-types.h"
a2fb1b05
ILT
32
33namespace gold
34{
35
61ba1cf9 36class General_options;
a2fb1b05 37class Object;
a3ad94ed 38class Symbol;
a2fb1b05 39class Output_file;
c06b7b0b 40class Output_section;
6a74a719 41class Relocatable_relocs;
a3ad94ed 42class Target;
54dc6425
ILT
43template<int size, bool big_endian>
44class Sized_target;
c06b7b0b
ILT
45template<int size, bool big_endian>
46class Sized_relobj;
54dc6425
ILT
47
48// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
49
50class Output_data
51{
52 public:
27bc2bce
ILT
53 explicit Output_data()
54 : address_(0), data_size_(0), offset_(-1),
55 is_address_valid_(false), is_data_size_valid_(false),
56 is_offset_valid_(false),
4f4c5f80 57 dynamic_reloc_count_(0)
a2fb1b05
ILT
58 { }
59
60 virtual
61 ~Output_data();
62
27bc2bce
ILT
63 // Return the address. For allocated sections, this is only valid
64 // after Layout::finalize is finished.
75f65a3e
ILT
65 uint64_t
66 address() const
27bc2bce
ILT
67 {
68 gold_assert(this->is_address_valid_);
69 return this->address_;
70 }
75f65a3e 71
27bc2bce
ILT
72 // Return the size of the data. For allocated sections, this must
73 // be valid after Layout::finalize calls set_address, but need not
74 // be valid before then.
a2fb1b05 75 off_t
75f65a3e 76 data_size() const
27bc2bce
ILT
77 {
78 gold_assert(this->is_data_size_valid_);
79 return this->data_size_;
80 }
75f65a3e 81
ead1e424 82 // Return the file offset. This is only valid after
27bc2bce
ILT
83 // Layout::finalize is finished. For some non-allocated sections,
84 // it may not be valid until near the end of the link.
75f65a3e
ILT
85 off_t
86 offset() const
27bc2bce
ILT
87 {
88 gold_assert(this->is_offset_valid_);
89 return this->offset_;
90 }
75f65a3e 91
a445fddf
ILT
92 // Reset the address and file offset. This essentially disables the
93 // sanity testing about duplicate and unknown settings.
94 void
95 reset_address_and_file_offset()
96 {
97 this->is_address_valid_ = false;
98 this->is_offset_valid_ = false;
99 this->is_data_size_valid_ = false;
100 this->do_reset_address_and_file_offset();
101 }
102
75f65a3e
ILT
103 // Return the required alignment.
104 uint64_t
105 addralign() const
106 { return this->do_addralign(); }
107
a445fddf
ILT
108 // Return whether this has a load address.
109 bool
110 has_load_address() const
111 { return this->do_has_load_address(); }
112
113 // Return the load address.
114 uint64_t
115 load_address() const
116 { return this->do_load_address(); }
117
75f65a3e
ILT
118 // Return whether this is an Output_section.
119 bool
120 is_section() const
121 { return this->do_is_section(); }
122
123 // Return whether this is an Output_section of the specified type.
124 bool
125 is_section_type(elfcpp::Elf_Word stt) const
126 { return this->do_is_section_type(stt); }
127
128 // Return whether this is an Output_section with the specified flag
129 // set.
130 bool
131 is_section_flag_set(elfcpp::Elf_Xword shf) const
132 { return this->do_is_section_flag_set(shf); }
133
77e65537
ILT
134 // Return the output section that this goes in, if there is one.
135 Output_section*
136 output_section()
137 { return this->do_output_section(); }
138
ead1e424
ILT
139 // Return the output section index, if there is an output section.
140 unsigned int
141 out_shndx() const
142 { return this->do_out_shndx(); }
143
144 // Set the output section index, if this is an output section.
145 void
146 set_out_shndx(unsigned int shndx)
147 { this->do_set_out_shndx(shndx); }
148
27bc2bce
ILT
149 // Set the address and file offset of this data, and finalize the
150 // size of the data. This is called during Layout::finalize for
151 // allocated sections.
75f65a3e 152 void
27bc2bce
ILT
153 set_address_and_file_offset(uint64_t addr, off_t off)
154 {
155 this->set_address(addr);
156 this->set_file_offset(off);
157 this->finalize_data_size();
158 }
159
160 // Set the address.
161 void
162 set_address(uint64_t addr)
163 {
164 gold_assert(!this->is_address_valid_);
165 this->address_ = addr;
166 this->is_address_valid_ = true;
167 }
168
169 // Set the file offset.
170 void
171 set_file_offset(off_t off)
172 {
173 gold_assert(!this->is_offset_valid_);
174 this->offset_ = off;
175 this->is_offset_valid_ = true;
176 }
177
178 // Finalize the data size.
179 void
180 finalize_data_size()
181 {
182 if (!this->is_data_size_valid_)
183 {
184 // Tell the child class to set the data size.
185 this->set_final_data_size();
186 gold_assert(this->is_data_size_valid_);
187 }
188 }
75f65a3e 189
7bf1f802
ILT
190 // Set the TLS offset. Called only for SHT_TLS sections.
191 void
192 set_tls_offset(uint64_t tls_base)
193 { this->do_set_tls_offset(tls_base); }
194
195 // Return the TLS offset, relative to the base of the TLS segment.
196 // Valid only for SHT_TLS sections.
197 uint64_t
198 tls_offset() const
199 { return this->do_tls_offset(); }
200
ead1e424
ILT
201 // Write the data to the output file. This is called after
202 // Layout::finalize is complete.
75f65a3e
ILT
203 void
204 write(Output_file* file)
205 { this->do_write(file); }
a2fb1b05 206
27bc2bce
ILT
207 // This is called by Layout::finalize to note that the sizes of
208 // allocated sections must now be fixed.
a3ad94ed
ILT
209 static void
210 layout_complete()
27bc2bce 211 { Output_data::allocated_sizes_are_fixed = true; }
a3ad94ed 212
730cdc88
ILT
213 // Used to check that layout has been done.
214 static bool
215 is_layout_complete()
27bc2bce 216 { return Output_data::allocated_sizes_are_fixed; }
730cdc88 217
4f4c5f80
ILT
218 // Count the number of dynamic relocations applied to this section.
219 void
220 add_dynamic_reloc()
221 { ++this->dynamic_reloc_count_; }
222
223 // Return the number of dynamic relocations applied to this section.
224 unsigned int
225 dynamic_reloc_count() const
226 { return this->dynamic_reloc_count_; }
227
a9a60db6
ILT
228 // Whether the address is valid.
229 bool
230 is_address_valid() const
231 { return this->is_address_valid_; }
232
233 // Whether the file offset is valid.
234 bool
235 is_offset_valid() const
236 { return this->is_offset_valid_; }
237
238 // Whether the data size is valid.
239 bool
240 is_data_size_valid() const
241 { return this->is_data_size_valid_; }
242
75f65a3e
ILT
243 protected:
244 // Functions that child classes may or in some cases must implement.
245
246 // Write the data to the output file.
a2fb1b05 247 virtual void
75f65a3e
ILT
248 do_write(Output_file*) = 0;
249
250 // Return the required alignment.
251 virtual uint64_t
252 do_addralign() const = 0;
253
a445fddf
ILT
254 // Return whether this has a load address.
255 virtual bool
256 do_has_load_address() const
257 { return false; }
258
259 // Return the load address.
260 virtual uint64_t
261 do_load_address() const
262 { gold_unreachable(); }
263
75f65a3e
ILT
264 // Return whether this is an Output_section.
265 virtual bool
266 do_is_section() const
267 { return false; }
a2fb1b05 268
54dc6425 269 // Return whether this is an Output_section of the specified type.
75f65a3e 270 // This only needs to be implement by Output_section.
54dc6425 271 virtual bool
75f65a3e 272 do_is_section_type(elfcpp::Elf_Word) const
54dc6425
ILT
273 { return false; }
274
75f65a3e
ILT
275 // Return whether this is an Output_section with the specific flag
276 // set. This only needs to be implemented by Output_section.
54dc6425 277 virtual bool
75f65a3e 278 do_is_section_flag_set(elfcpp::Elf_Xword) const
54dc6425
ILT
279 { return false; }
280
77e65537
ILT
281 // Return the output section, if there is one.
282 virtual Output_section*
283 do_output_section()
284 { return NULL; }
285
ead1e424
ILT
286 // Return the output section index, if there is an output section.
287 virtual unsigned int
288 do_out_shndx() const
a3ad94ed 289 { gold_unreachable(); }
ead1e424
ILT
290
291 // Set the output section index, if this is an output section.
292 virtual void
293 do_set_out_shndx(unsigned int)
a3ad94ed 294 { gold_unreachable(); }
ead1e424 295
27bc2bce
ILT
296 // This is a hook for derived classes to set the data size. This is
297 // called by finalize_data_size, normally called during
298 // Layout::finalize, when the section address is set.
75f65a3e 299 virtual void
27bc2bce
ILT
300 set_final_data_size()
301 { gold_unreachable(); }
75f65a3e 302
a445fddf
ILT
303 // A hook for resetting the address and file offset.
304 virtual void
305 do_reset_address_and_file_offset()
306 { }
307
7bf1f802
ILT
308 // Set the TLS offset. Called only for SHT_TLS sections.
309 virtual void
310 do_set_tls_offset(uint64_t)
311 { gold_unreachable(); }
312
313 // Return the TLS offset, relative to the base of the TLS segment.
314 // Valid only for SHT_TLS sections.
315 virtual uint64_t
316 do_tls_offset() const
317 { gold_unreachable(); }
318
75f65a3e
ILT
319 // Functions that child classes may call.
320
a2fb1b05
ILT
321 // Set the size of the data.
322 void
75f65a3e 323 set_data_size(off_t data_size)
a3ad94ed 324 {
27bc2bce
ILT
325 gold_assert(!this->is_data_size_valid_);
326 this->data_size_ = data_size;
327 this->is_data_size_valid_ = true;
328 }
329
330 // Get the current data size--this is for the convenience of
331 // sections which build up their size over time.
332 off_t
333 current_data_size_for_child() const
334 { return this->data_size_; }
335
336 // Set the current data size--this is for the convenience of
337 // sections which build up their size over time.
338 void
339 set_current_data_size_for_child(off_t data_size)
340 {
341 gold_assert(!this->is_data_size_valid_);
a3ad94ed
ILT
342 this->data_size_ = data_size;
343 }
75f65a3e 344
730cdc88
ILT
345 // Return default alignment for the target size.
346 static uint64_t
347 default_alignment();
348
349 // Return default alignment for a specified size--32 or 64.
75f65a3e 350 static uint64_t
730cdc88 351 default_alignment_for_size(int size);
a2fb1b05
ILT
352
353 private:
354 Output_data(const Output_data&);
355 Output_data& operator=(const Output_data&);
356
a3ad94ed 357 // This is used for verification, to make sure that we don't try to
27bc2bce
ILT
358 // change any sizes of allocated sections after we set the section
359 // addresses.
360 static bool allocated_sizes_are_fixed;
a3ad94ed 361
27bc2bce 362 // Memory address in output file.
75f65a3e 363 uint64_t address_;
27bc2bce 364 // Size of data in output file.
75f65a3e 365 off_t data_size_;
27bc2bce 366 // File offset of contents in output file.
75f65a3e 367 off_t offset_;
27bc2bce
ILT
368 // Whether address_ is valid.
369 bool is_address_valid_;
370 // Whether data_size_ is valid.
371 bool is_data_size_valid_;
372 // Whether offset_ is valid.
373 bool is_offset_valid_;
4f4c5f80
ILT
374 // Count of dynamic relocations applied to this section.
375 unsigned int dynamic_reloc_count_;
a2fb1b05
ILT
376};
377
54dc6425
ILT
378// Output the section headers.
379
380class Output_section_headers : public Output_data
381{
382 public:
9025d29d 383 Output_section_headers(const Layout*,
16649710
ILT
384 const Layout::Segment_list*,
385 const Layout::Section_list*,
6a74a719 386 const Layout::Section_list*,
61ba1cf9 387 const Stringpool*);
54dc6425 388
27bc2bce 389 protected:
54dc6425
ILT
390 // Write the data to the file.
391 void
75f65a3e
ILT
392 do_write(Output_file*);
393
394 // Return the required alignment.
395 uint64_t
396 do_addralign() const
730cdc88 397 { return Output_data::default_alignment(); }
54dc6425
ILT
398
399 private:
61ba1cf9
ILT
400 // Write the data to the file with the right size and endianness.
401 template<int size, bool big_endian>
402 void
403 do_sized_write(Output_file*);
404
16649710
ILT
405 const Layout* layout_;
406 const Layout::Segment_list* segment_list_;
6a74a719 407 const Layout::Section_list* section_list_;
16649710 408 const Layout::Section_list* unattached_section_list_;
61ba1cf9 409 const Stringpool* secnamepool_;
54dc6425
ILT
410};
411
412// Output the segment headers.
413
414class Output_segment_headers : public Output_data
415{
416 public:
9025d29d 417 Output_segment_headers(const Layout::Segment_list& segment_list);
54dc6425 418
27bc2bce 419 protected:
54dc6425
ILT
420 // Write the data to the file.
421 void
75f65a3e
ILT
422 do_write(Output_file*);
423
424 // Return the required alignment.
425 uint64_t
426 do_addralign() const
730cdc88 427 { return Output_data::default_alignment(); }
54dc6425
ILT
428
429 private:
61ba1cf9
ILT
430 // Write the data to the file with the right size and endianness.
431 template<int size, bool big_endian>
432 void
433 do_sized_write(Output_file*);
434
54dc6425
ILT
435 const Layout::Segment_list& segment_list_;
436};
437
438// Output the ELF file header.
439
440class Output_file_header : public Output_data
441{
442 public:
9025d29d 443 Output_file_header(const Target*,
54dc6425 444 const Symbol_table*,
d391083d
ILT
445 const Output_segment_headers*,
446 const char* entry);
75f65a3e
ILT
447
448 // Add information about the section headers. We lay out the ELF
449 // file header before we create the section headers.
450 void set_section_info(const Output_section_headers*,
451 const Output_section* shstrtab);
54dc6425 452
27bc2bce 453 protected:
54dc6425
ILT
454 // Write the data to the file.
455 void
75f65a3e
ILT
456 do_write(Output_file*);
457
458 // Return the required alignment.
459 uint64_t
460 do_addralign() const
730cdc88 461 { return Output_data::default_alignment(); }
75f65a3e 462
54dc6425 463 private:
61ba1cf9
ILT
464 // Write the data to the file with the right size and endianness.
465 template<int size, bool big_endian>
466 void
467 do_sized_write(Output_file*);
468
d391083d
ILT
469 // Return the value to use for the entry address.
470 template<int size>
471 typename elfcpp::Elf_types<size>::Elf_Addr
472 entry();
473
54dc6425
ILT
474 const Target* target_;
475 const Symbol_table* symtab_;
61ba1cf9 476 const Output_segment_headers* segment_header_;
54dc6425
ILT
477 const Output_section_headers* section_header_;
478 const Output_section* shstrtab_;
d391083d 479 const char* entry_;
54dc6425
ILT
480};
481
ead1e424
ILT
482// Output sections are mainly comprised of input sections. However,
483// there are cases where we have data to write out which is not in an
484// input section. Output_section_data is used in such cases. This is
485// an abstract base class.
486
487class Output_section_data : public Output_data
488{
489 public:
490 Output_section_data(off_t data_size, uint64_t addralign)
27bc2bce
ILT
491 : Output_data(), output_section_(NULL), addralign_(addralign)
492 { this->set_data_size(data_size); }
ead1e424
ILT
493
494 Output_section_data(uint64_t addralign)
27bc2bce 495 : Output_data(), output_section_(NULL), addralign_(addralign)
ead1e424
ILT
496 { }
497
16649710
ILT
498 // Return the output section.
499 const Output_section*
500 output_section() const
501 { return this->output_section_; }
502
ead1e424
ILT
503 // Record the output section.
504 void
16649710 505 set_output_section(Output_section* os);
ead1e424 506
b8e6aad9
ILT
507 // Add an input section, for SHF_MERGE sections. This returns true
508 // if the section was handled.
509 bool
510 add_input_section(Relobj* object, unsigned int shndx)
511 { return this->do_add_input_section(object, shndx); }
512
513 // Given an input OBJECT, an input section index SHNDX within that
514 // object, and an OFFSET relative to the start of that input
730cdc88
ILT
515 // section, return whether or not the corresponding offset within
516 // the output section is known. If this function returns true, it
517 // sets *POUTPUT to the output offset. The value -1 indicates that
518 // this input offset is being discarded.
8f00aeb8 519 bool
8383303e
ILT
520 output_offset(const Relobj* object, unsigned int shndx,
521 section_offset_type offset,
522 section_offset_type *poutput) const
730cdc88 523 { return this->do_output_offset(object, shndx, offset, poutput); }
b8e6aad9 524
a9a60db6
ILT
525 // Return whether this is the merge section for the input section
526 // SHNDX in OBJECT. This should return true when output_offset
527 // would return true for some values of OFFSET.
528 bool
529 is_merge_section_for(const Relobj* object, unsigned int shndx) const
530 { return this->do_is_merge_section_for(object, shndx); }
531
96803768
ILT
532 // Write the contents to a buffer. This is used for sections which
533 // require postprocessing, such as compression.
534 void
535 write_to_buffer(unsigned char* buffer)
536 { this->do_write_to_buffer(buffer); }
537
38c5e8b4
ILT
538 // Print merge stats to stderr. This should only be called for
539 // SHF_MERGE sections.
540 void
541 print_merge_stats(const char* section_name)
542 { this->do_print_merge_stats(section_name); }
543
ead1e424
ILT
544 protected:
545 // The child class must implement do_write.
546
16649710
ILT
547 // The child class may implement specific adjustments to the output
548 // section.
549 virtual void
550 do_adjust_output_section(Output_section*)
551 { }
552
b8e6aad9
ILT
553 // May be implemented by child class. Return true if the section
554 // was handled.
555 virtual bool
556 do_add_input_section(Relobj*, unsigned int)
557 { gold_unreachable(); }
558
730cdc88 559 // The child class may implement output_offset.
b8e6aad9 560 virtual bool
8383303e
ILT
561 do_output_offset(const Relobj*, unsigned int, section_offset_type,
562 section_offset_type*) const
b8e6aad9
ILT
563 { return false; }
564
a9a60db6
ILT
565 // The child class may implement is_merge_section_for.
566 virtual bool
567 do_is_merge_section_for(const Relobj*, unsigned int) const
568 { return false; }
569
96803768
ILT
570 // The child class may implement write_to_buffer. Most child
571 // classes can not appear in a compressed section, and they do not
572 // implement this.
573 virtual void
574 do_write_to_buffer(unsigned char*)
575 { gold_unreachable(); }
576
38c5e8b4
ILT
577 // Print merge statistics.
578 virtual void
579 do_print_merge_stats(const char*)
580 { gold_unreachable(); }
581
ead1e424
ILT
582 // Return the required alignment.
583 uint64_t
584 do_addralign() const
585 { return this->addralign_; }
586
77e65537
ILT
587 // Return the output section.
588 Output_section*
589 do_output_section()
590 { return this->output_section_; }
591
ead1e424
ILT
592 // Return the section index of the output section.
593 unsigned int
594 do_out_shndx() const;
595
5a6f7e2d
ILT
596 // Set the alignment.
597 void
598 set_addralign(uint64_t addralign)
599 { this->addralign_ = addralign; }
600
ead1e424
ILT
601 private:
602 // The output section for this section.
77e65537 603 Output_section* output_section_;
ead1e424
ILT
604 // The required alignment.
605 uint64_t addralign_;
606};
607
27bc2bce
ILT
608// Some Output_section_data classes build up their data step by step,
609// rather than all at once. This class provides an interface for
610// them.
611
612class Output_section_data_build : public Output_section_data
613{
614 public:
615 Output_section_data_build(uint64_t addralign)
616 : Output_section_data(addralign)
617 { }
618
619 // Get the current data size.
620 off_t
621 current_data_size() const
622 { return this->current_data_size_for_child(); }
623
624 // Set the current data size.
625 void
626 set_current_data_size(off_t data_size)
627 { this->set_current_data_size_for_child(data_size); }
628
629 protected:
630 // Set the final data size.
631 virtual void
632 set_final_data_size()
633 { this->set_data_size(this->current_data_size_for_child()); }
634};
635
dbe717ef
ILT
636// A simple case of Output_data in which we have constant data to
637// output.
ead1e424 638
dbe717ef 639class Output_data_const : public Output_section_data
ead1e424
ILT
640{
641 public:
dbe717ef
ILT
642 Output_data_const(const std::string& data, uint64_t addralign)
643 : Output_section_data(data.size(), addralign), data_(data)
644 { }
645
646 Output_data_const(const char* p, off_t len, uint64_t addralign)
647 : Output_section_data(len, addralign), data_(p, len)
648 { }
649
650 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
651 : Output_section_data(len, addralign),
652 data_(reinterpret_cast<const char*>(p), len)
653 { }
654
27bc2bce 655 protected:
a3ad94ed 656 // Write the data to the output file.
dbe717ef 657 void
a3ad94ed 658 do_write(Output_file*);
dbe717ef 659
96803768
ILT
660 // Write the data to a buffer.
661 void
662 do_write_to_buffer(unsigned char* buffer)
663 { memcpy(buffer, this->data_.data(), this->data_.size()); }
664
dbe717ef
ILT
665 private:
666 std::string data_;
667};
668
a3ad94ed
ILT
669// Another version of Output_data with constant data, in which the
670// buffer is allocated by the caller.
dbe717ef 671
a3ad94ed 672class Output_data_const_buffer : public Output_section_data
dbe717ef
ILT
673{
674 public:
a3ad94ed
ILT
675 Output_data_const_buffer(const unsigned char* p, off_t len,
676 uint64_t addralign)
677 : Output_section_data(len, addralign), p_(p)
678 { }
679
27bc2bce 680 protected:
a3ad94ed
ILT
681 // Write the data the output file.
682 void
683 do_write(Output_file*);
684
96803768
ILT
685 // Write the data to a buffer.
686 void
687 do_write_to_buffer(unsigned char* buffer)
688 { memcpy(buffer, this->p_, this->data_size()); }
689
a3ad94ed
ILT
690 private:
691 const unsigned char* p_;
692};
693
27bc2bce
ILT
694// A place holder for a fixed amount of data written out via some
695// other mechanism.
a3ad94ed 696
27bc2bce 697class Output_data_fixed_space : public Output_section_data
a3ad94ed
ILT
698{
699 public:
27bc2bce 700 Output_data_fixed_space(off_t data_size, uint64_t addralign)
a3ad94ed
ILT
701 : Output_section_data(data_size, addralign)
702 { }
703
27bc2bce
ILT
704 protected:
705 // Write out the data--the actual data must be written out
706 // elsewhere.
707 void
708 do_write(Output_file*)
ead1e424 709 { }
27bc2bce 710};
ead1e424 711
27bc2bce
ILT
712// A place holder for variable sized data written out via some other
713// mechanism.
714
715class Output_data_space : public Output_section_data_build
716{
717 public:
718 explicit Output_data_space(uint64_t addralign)
719 : Output_section_data_build(addralign)
720 { }
ead1e424 721
5a6f7e2d
ILT
722 // Set the alignment.
723 void
724 set_space_alignment(uint64_t align)
725 { this->set_addralign(align); }
726
27bc2bce
ILT
727 protected:
728 // Write out the data--the actual data must be written out
729 // elsewhere.
ead1e424
ILT
730 void
731 do_write(Output_file*)
732 { }
733};
734
a3ad94ed
ILT
735// A string table which goes into an output section.
736
737class Output_data_strtab : public Output_section_data
738{
739 public:
740 Output_data_strtab(Stringpool* strtab)
741 : Output_section_data(1), strtab_(strtab)
742 { }
743
27bc2bce 744 protected:
a3ad94ed
ILT
745 // This is called to set the address and file offset. Here we make
746 // sure that the Stringpool is finalized.
747 void
27bc2bce 748 set_final_data_size();
a3ad94ed
ILT
749
750 // Write out the data.
751 void
752 do_write(Output_file*);
753
96803768
ILT
754 // Write the data to a buffer.
755 void
756 do_write_to_buffer(unsigned char* buffer)
757 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
758
a3ad94ed
ILT
759 private:
760 Stringpool* strtab_;
761};
762
c06b7b0b
ILT
763// This POD class is used to represent a single reloc in the output
764// file. This could be a private class within Output_data_reloc, but
765// the templatization is complex enough that I broke it out into a
766// separate class. The class is templatized on either elfcpp::SHT_REL
767// or elfcpp::SHT_RELA, and also on whether this is a dynamic
768// relocation or an ordinary relocation.
769
dceae3c1
ILT
770// A relocation can be against a global symbol, a local symbol, a
771// local section symbol, an output section, or the undefined symbol at
772// index 0. We represent the latter by using a NULL global symbol.
c06b7b0b
ILT
773
774template<int sh_type, bool dynamic, int size, bool big_endian>
775class Output_reloc;
776
777template<bool dynamic, int size, bool big_endian>
778class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
779{
780 public:
781 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
624f8810 782 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
c06b7b0b
ILT
783
784 // An uninitialized entry. We need this because we want to put
785 // instances of this class into an STL container.
786 Output_reloc()
787 : local_sym_index_(INVALID_CODE)
788 { }
789
dceae3c1
ILT
790 // We have a bunch of different constructors. They come in pairs
791 // depending on how the address of the relocation is specified. It
792 // can either be an offset in an Output_data or an offset in an
793 // input section.
794
c06b7b0b 795 // A reloc against a global symbol.
5a6f7e2d 796
a3ad94ed 797 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
e8c846c3 798 Address address, bool is_relative);
5a6f7e2d
ILT
799
800 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
e8c846c3 801 unsigned int shndx, Address address, bool is_relative);
c06b7b0b 802
dceae3c1 803 // A reloc against a local symbol or local section symbol.
5a6f7e2d
ILT
804
805 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 806 unsigned int local_sym_index, unsigned int type,
dceae3c1
ILT
807 Output_data* od, Address address, bool is_relative,
808 bool is_section_symbol);
5a6f7e2d
ILT
809
810 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 811 unsigned int local_sym_index, unsigned int type,
dceae3c1
ILT
812 unsigned int shndx, Address address, bool is_relative,
813 bool is_section_symbol);
c06b7b0b
ILT
814
815 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 816
a3ad94ed 817 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
7bf1f802 818 Address address);
5a6f7e2d
ILT
819
820 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
7bf1f802 821 unsigned int shndx, Address address);
c06b7b0b 822
e8c846c3
ILT
823 // Return TRUE if this is a RELATIVE relocation.
824 bool
825 is_relative() const
826 { return this->is_relative_; }
827
dceae3c1
ILT
828 // Return whether this is against a local section symbol.
829 bool
830 is_local_section_symbol() const
831 {
832 return (this->local_sym_index_ != GSYM_CODE
833 && this->local_sym_index_ != SECTION_CODE
834 && this->local_sym_index_ != INVALID_CODE
835 && this->is_section_symbol_);
836 }
837
838 // For a local section symbol, return the offset of the input
624f8810
ILT
839 // section within the output section. ADDEND is the addend being
840 // applied to the input section.
dceae3c1 841 section_offset_type
624f8810 842 local_section_offset(Addend addend) const;
dceae3c1 843
d1f003c6
ILT
844 // Get the value of the symbol referred to by a Rel relocation when
845 // we are adding the given ADDEND.
e8c846c3 846 Address
624f8810 847 symbol_value(Addend addend) const;
e8c846c3 848
c06b7b0b
ILT
849 // Write the reloc entry to an output view.
850 void
851 write(unsigned char* pov) const;
852
853 // Write the offset and info fields to Write_rel.
854 template<typename Write_rel>
855 void write_rel(Write_rel*) const;
856
857 private:
dceae3c1
ILT
858 // Record that we need a dynamic symbol index.
859 void
860 set_needs_dynsym_index();
861
862 // Return the symbol index.
c06b7b0b
ILT
863 unsigned int
864 get_symbol_index() const;
865
866 // Codes for local_sym_index_.
867 enum
868 {
869 // Global symbol.
870 GSYM_CODE = -1U,
871 // Output section.
872 SECTION_CODE = -2U,
873 // Invalid uninitialized entry.
874 INVALID_CODE = -3U
875 };
876
877 union
878 {
dceae3c1
ILT
879 // For a local symbol or local section symbol
880 // (this->local_sym_index_ >= 0), the object. We will never
881 // generate a relocation against a local symbol in a dynamic
882 // object; that doesn't make sense. And our callers will always
883 // be templatized, so we use Sized_relobj here.
5a6f7e2d 884 Sized_relobj<size, big_endian>* relobj;
dceae3c1
ILT
885 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
886 // symbol. If this is NULL, it indicates a relocation against the
887 // undefined 0 symbol.
c06b7b0b 888 Symbol* gsym;
dceae3c1
ILT
889 // For a relocation against an output section
890 // (this->local_sym_index_ == SECTION_CODE), the output section.
c06b7b0b 891 Output_section* os;
5a6f7e2d
ILT
892 } u1_;
893 union
894 {
dceae3c1
ILT
895 // If this->shndx_ is not INVALID CODE, the object which holds the
896 // input section being used to specify the reloc address.
5a6f7e2d 897 Relobj* relobj;
dceae3c1 898 // If this->shndx_ is INVALID_CODE, the output data being used to
5a6f7e2d
ILT
899 // specify the reloc address. This may be NULL if the reloc
900 // address is absolute.
901 Output_data* od;
902 } u2_;
903 // The address offset within the input section or the Output_data.
904 Address address_;
dceae3c1
ILT
905 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
906 // relocation against an output section, or INVALID_CODE for an
907 // uninitialized value. Otherwise, for a local symbol
908 // (this->is_section_symbol_ is false), the local symbol index. For
909 // a local section symbol (this->is_section_symbol_ is true), the
910 // section index in the input file.
c06b7b0b 911 unsigned int local_sym_index_;
a3ad94ed 912 // The reloc type--a processor specific code.
dceae3c1 913 unsigned int type_ : 30;
e8c846c3
ILT
914 // True if the relocation is a RELATIVE relocation.
915 bool is_relative_ : 1;
dceae3c1
ILT
916 // True if the relocation is against a section symbol.
917 bool is_section_symbol_ : 1;
5a6f7e2d
ILT
918 // If the reloc address is an input section in an object, the
919 // section index. This is INVALID_CODE if the reloc address is
920 // specified in some other way.
921 unsigned int shndx_;
c06b7b0b
ILT
922};
923
924// The SHT_RELA version of Output_reloc<>. This is just derived from
925// the SHT_REL version of Output_reloc, but it adds an addend.
926
927template<bool dynamic, int size, bool big_endian>
928class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
929{
930 public:
931 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
932 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
933
934 // An uninitialized entry.
935 Output_reloc()
936 : rel_()
937 { }
938
939 // A reloc against a global symbol.
5a6f7e2d 940
a3ad94ed 941 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
e8c846c3
ILT
942 Address address, Addend addend, bool is_relative)
943 : rel_(gsym, type, od, address, is_relative), addend_(addend)
c06b7b0b
ILT
944 { }
945
5a6f7e2d 946 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
e8c846c3
ILT
947 unsigned int shndx, Address address, Addend addend,
948 bool is_relative)
949 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
5a6f7e2d
ILT
950 { }
951
c06b7b0b 952 // A reloc against a local symbol.
5a6f7e2d
ILT
953
954 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
955 unsigned int local_sym_index, unsigned int type,
956 Output_data* od, Address address,
dceae3c1
ILT
957 Addend addend, bool is_relative, bool is_section_symbol)
958 : rel_(relobj, local_sym_index, type, od, address, is_relative,
959 is_section_symbol),
e8c846c3 960 addend_(addend)
5a6f7e2d
ILT
961 { }
962
963 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
964 unsigned int local_sym_index, unsigned int type,
965 unsigned int shndx, Address address,
dceae3c1
ILT
966 Addend addend, bool is_relative, bool is_section_symbol)
967 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
968 is_section_symbol),
5a6f7e2d 969 addend_(addend)
c06b7b0b
ILT
970 { }
971
972 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 973
a3ad94ed
ILT
974 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
975 Address address, Addend addend)
976 : rel_(os, type, od, address), addend_(addend)
c06b7b0b
ILT
977 { }
978
5a6f7e2d
ILT
979 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
980 unsigned int shndx, Address address, Addend addend)
981 : rel_(os, type, relobj, shndx, address), addend_(addend)
982 { }
983
c06b7b0b
ILT
984 // Write the reloc entry to an output view.
985 void
986 write(unsigned char* pov) const;
987
988 private:
989 // The basic reloc.
990 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
991 // The addend.
992 Addend addend_;
993};
994
995// Output_data_reloc is used to manage a section containing relocs.
996// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
997// indicates whether this is a dynamic relocation or a normal
998// relocation. Output_data_reloc_base is a base class.
999// Output_data_reloc is the real class, which we specialize based on
1000// the reloc type.
1001
1002template<int sh_type, bool dynamic, int size, bool big_endian>
27bc2bce 1003class Output_data_reloc_base : public Output_section_data_build
c06b7b0b
ILT
1004{
1005 public:
1006 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1007 typedef typename Output_reloc_type::Address Address;
1008 static const int reloc_size =
1009 Reloc_types<sh_type, size, big_endian>::reloc_size;
1010
1011 // Construct the section.
1012 Output_data_reloc_base()
27bc2bce 1013 : Output_section_data_build(Output_data::default_alignment_for_size(size))
c06b7b0b
ILT
1014 { }
1015
27bc2bce 1016 protected:
c06b7b0b
ILT
1017 // Write out the data.
1018 void
1019 do_write(Output_file*);
1020
16649710
ILT
1021 // Set the entry size and the link.
1022 void
1023 do_adjust_output_section(Output_section *os);
1024
c06b7b0b
ILT
1025 // Add a relocation entry.
1026 void
4f4c5f80 1027 add(Output_data *od, const Output_reloc_type& reloc)
c06b7b0b
ILT
1028 {
1029 this->relocs_.push_back(reloc);
27bc2bce 1030 this->set_current_data_size(this->relocs_.size() * reloc_size);
4f4c5f80 1031 od->add_dynamic_reloc();
c06b7b0b
ILT
1032 }
1033
1034 private:
1035 typedef std::vector<Output_reloc_type> Relocs;
1036
1037 Relocs relocs_;
1038};
1039
1040// The class which callers actually create.
1041
1042template<int sh_type, bool dynamic, int size, bool big_endian>
1043class Output_data_reloc;
1044
1045// The SHT_REL version of Output_data_reloc.
1046
1047template<bool dynamic, int size, bool big_endian>
1048class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1049 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1050{
dceae3c1 1051 private:
c06b7b0b
ILT
1052 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1053 big_endian> Base;
1054
1055 public:
1056 typedef typename Base::Output_reloc_type Output_reloc_type;
1057 typedef typename Output_reloc_type::Address Address;
1058
1059 Output_data_reloc()
1060 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1061 { }
1062
1063 // Add a reloc against a global symbol.
5a6f7e2d 1064
c06b7b0b 1065 void
a3ad94ed 1066 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
e8c846c3 1067 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
c06b7b0b 1068
5a6f7e2d 1069 void
4f4c5f80 1070 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
5a6f7e2d 1071 unsigned int shndx, Address address)
e8c846c3
ILT
1072 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1073 false)); }
1074
1075 // Add a RELATIVE reloc against a global symbol. The final relocation
1076 // will not reference the symbol.
1077
1078 void
1079 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1080 Address address)
1081 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1082
1083 void
1084 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1085 Relobj* relobj, unsigned int shndx, Address address)
dceae3c1
ILT
1086 {
1087 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1088 true));
1089 }
5a6f7e2d 1090
c06b7b0b 1091 // Add a reloc against a local symbol.
5a6f7e2d 1092
c06b7b0b 1093 void
5a6f7e2d 1094 add_local(Sized_relobj<size, big_endian>* relobj,
a3ad94ed
ILT
1095 unsigned int local_sym_index, unsigned int type,
1096 Output_data* od, Address address)
dceae3c1
ILT
1097 {
1098 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1099 address, false, false));
1100 }
5a6f7e2d
ILT
1101
1102 void
1103 add_local(Sized_relobj<size, big_endian>* relobj,
1104 unsigned int local_sym_index, unsigned int type,
4f4c5f80 1105 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1106 {
1107 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1108 address, false, false));
1109 }
e8c846c3
ILT
1110
1111 // Add a RELATIVE reloc against a local symbol.
5a6f7e2d 1112
e8c846c3
ILT
1113 void
1114 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1115 unsigned int local_sym_index, unsigned int type,
1116 Output_data* od, Address address)
dceae3c1
ILT
1117 {
1118 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1119 address, true, false));
1120 }
e8c846c3
ILT
1121
1122 void
1123 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1124 unsigned int local_sym_index, unsigned int type,
1125 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1126 {
1127 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1128 address, true, false));
1129 }
1130
1131 // Add a reloc against a local section symbol. This will be
1132 // converted into a reloc against the STT_SECTION symbol of the
1133 // output section.
1134
1135 void
1136 add_local_section(Sized_relobj<size, big_endian>* relobj,
1137 unsigned int input_shndx, unsigned int type,
1138 Output_data* od, Address address)
1139 {
1140 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1141 address, false, true));
1142 }
1143
1144 void
1145 add_local_section(Sized_relobj<size, big_endian>* relobj,
1146 unsigned int input_shndx, unsigned int type,
1147 Output_data* od, unsigned int shndx, Address address)
1148 {
1149 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1150 address, false, true));
1151 }
c06b7b0b
ILT
1152
1153 // A reloc against the STT_SECTION symbol of an output section.
4f4c5f80
ILT
1154 // OS is the Output_section that the relocation refers to; OD is
1155 // the Output_data object being relocated.
5a6f7e2d 1156
c06b7b0b 1157 void
a3ad94ed
ILT
1158 add_output_section(Output_section* os, unsigned int type,
1159 Output_data* od, Address address)
4f4c5f80 1160 { this->add(od, Output_reloc_type(os, type, od, address)); }
5a6f7e2d
ILT
1161
1162 void
4f4c5f80 1163 add_output_section(Output_section* os, unsigned int type, Output_data* od,
5a6f7e2d 1164 Relobj* relobj, unsigned int shndx, Address address)
4f4c5f80 1165 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
c06b7b0b
ILT
1166};
1167
1168// The SHT_RELA version of Output_data_reloc.
1169
1170template<bool dynamic, int size, bool big_endian>
1171class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1172 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1173{
dceae3c1 1174 private:
c06b7b0b
ILT
1175 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1176 big_endian> Base;
1177
1178 public:
1179 typedef typename Base::Output_reloc_type Output_reloc_type;
1180 typedef typename Output_reloc_type::Address Address;
1181 typedef typename Output_reloc_type::Addend Addend;
1182
1183 Output_data_reloc()
1184 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1185 { }
1186
1187 // Add a reloc against a global symbol.
5a6f7e2d 1188
c06b7b0b 1189 void
a3ad94ed
ILT
1190 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1191 Address address, Addend addend)
e8c846c3
ILT
1192 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1193 false)); }
c06b7b0b 1194
5a6f7e2d 1195 void
4f4c5f80
ILT
1196 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1197 unsigned int shndx, Address address,
1198 Addend addend)
1199 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
e8c846c3
ILT
1200 addend, false)); }
1201
1202 // Add a RELATIVE reloc against a global symbol. The final output
1203 // relocation will not reference the symbol, but we must keep the symbol
1204 // information long enough to set the addend of the relocation correctly
1205 // when it is written.
1206
1207 void
1208 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1209 Address address, Addend addend)
1210 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1211
1212 void
1213 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1214 Relobj* relobj, unsigned int shndx, Address address,
1215 Addend addend)
1216 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1217 addend, true)); }
5a6f7e2d 1218
c06b7b0b 1219 // Add a reloc against a local symbol.
5a6f7e2d 1220
c06b7b0b 1221 void
5a6f7e2d 1222 add_local(Sized_relobj<size, big_endian>* relobj,
c06b7b0b 1223 unsigned int local_sym_index, unsigned int type,
a3ad94ed 1224 Output_data* od, Address address, Addend addend)
c06b7b0b 1225 {
4f4c5f80 1226 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
dceae3c1 1227 addend, false, false));
5a6f7e2d
ILT
1228 }
1229
1230 void
1231 add_local(Sized_relobj<size, big_endian>* relobj,
1232 unsigned int local_sym_index, unsigned int type,
4f4c5f80
ILT
1233 Output_data* od, unsigned int shndx, Address address,
1234 Addend addend)
5a6f7e2d 1235 {
4f4c5f80 1236 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
dceae3c1 1237 address, addend, false, false));
e8c846c3
ILT
1238 }
1239
1240 // Add a RELATIVE reloc against a local symbol.
1241
1242 void
1243 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1244 unsigned int local_sym_index, unsigned int type,
1245 Output_data* od, Address address, Addend addend)
1246 {
1247 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
dceae3c1 1248 addend, true, false));
e8c846c3
ILT
1249 }
1250
1251 void
1252 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1253 unsigned int local_sym_index, unsigned int type,
1254 Output_data* od, unsigned int shndx, Address address,
1255 Addend addend)
1256 {
1257 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
dceae3c1
ILT
1258 address, addend, true, false));
1259 }
1260
1261 // Add a reloc against a local section symbol. This will be
1262 // converted into a reloc against the STT_SECTION symbol of the
1263 // output section.
1264
1265 void
1266 add_local_section(Sized_relobj<size, big_endian>* relobj,
1267 unsigned int input_shndx, unsigned int type,
1268 Output_data* od, Address address, Addend addend)
1269 {
1270 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1271 addend, false, true));
1272 }
1273
1274 void
1275 add_local_section(Sized_relobj<size, big_endian>* relobj,
1276 unsigned int input_shndx, unsigned int type,
1277 Output_data* od, unsigned int shndx, Address address,
1278 Addend addend)
1279 {
1280 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1281 address, addend, false, true));
c06b7b0b
ILT
1282 }
1283
1284 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1285
c06b7b0b 1286 void
a3ad94ed
ILT
1287 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1288 Address address, Addend addend)
4f4c5f80 1289 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
5a6f7e2d
ILT
1290
1291 void
1292 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1293 unsigned int shndx, Address address, Addend addend)
4f4c5f80
ILT
1294 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1295 addend)); }
c06b7b0b
ILT
1296};
1297
6a74a719
ILT
1298// Output_relocatable_relocs represents a relocation section in a
1299// relocatable link. The actual data is written out in the target
1300// hook relocate_for_relocatable. This just saves space for it.
1301
1302template<int sh_type, int size, bool big_endian>
1303class Output_relocatable_relocs : public Output_section_data
1304{
1305 public:
1306 Output_relocatable_relocs(Relocatable_relocs* rr)
1307 : Output_section_data(Output_data::default_alignment_for_size(size)),
1308 rr_(rr)
1309 { }
1310
1311 void
1312 set_final_data_size();
1313
1314 // Write out the data. There is nothing to do here.
1315 void
1316 do_write(Output_file*)
1317 { }
1318
1319 private:
1320 // The relocs associated with this input section.
1321 Relocatable_relocs* rr_;
1322};
1323
1324// Handle a GROUP section.
1325
1326template<int size, bool big_endian>
1327class Output_data_group : public Output_section_data
1328{
1329 public:
1330 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1331 section_size_type entry_count,
1332 const elfcpp::Elf_Word* contents);
1333
1334 void
1335 do_write(Output_file*);
1336
1337 private:
1338 // The input object.
1339 Sized_relobj<size, big_endian>* relobj_;
1340 // The group flag word.
1341 elfcpp::Elf_Word flags_;
1342 // The section indexes of the input sections in this group.
1343 std::vector<unsigned int> input_sections_;
1344};
1345
dbe717ef
ILT
1346// Output_data_got is used to manage a GOT. Each entry in the GOT is
1347// for one symbol--either a global symbol or a local symbol in an
ead1e424 1348// object. The target specific code adds entries to the GOT as
dbe717ef 1349// needed.
ead1e424
ILT
1350
1351template<int size, bool big_endian>
27bc2bce 1352class Output_data_got : public Output_section_data_build
ead1e424
ILT
1353{
1354 public:
1355 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
7bf1f802
ILT
1356 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1357 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
ead1e424 1358
7e1edb90 1359 Output_data_got()
27bc2bce 1360 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
730cdc88 1361 entries_()
ead1e424
ILT
1362 { }
1363
dbe717ef
ILT
1364 // Add an entry for a global symbol to the GOT. Return true if this
1365 // is a new GOT entry, false if the symbol was already in the GOT.
1366 bool
0a65a3a7 1367 add_global(Symbol* gsym, unsigned int got_type);
ead1e424 1368
7bf1f802
ILT
1369 // Add an entry for a global symbol to the GOT, and add a dynamic
1370 // relocation of type R_TYPE for the GOT entry.
1371 void
0a65a3a7
CC
1372 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1373 Rel_dyn* rel_dyn, unsigned int r_type);
7bf1f802
ILT
1374
1375 void
0a65a3a7
CC
1376 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1377 Rela_dyn* rela_dyn, unsigned int r_type);
1378
1379 // Add a pair of entries for a global symbol to the GOT, and add
1380 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1381 void
1382 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1383 Rel_dyn* rel_dyn, unsigned int r_type_1,
1384 unsigned int r_type_2);
1385
1386 void
1387 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1388 Rela_dyn* rela_dyn, unsigned int r_type_1,
1389 unsigned int r_type_2);
7bf1f802 1390
e727fa71
ILT
1391 // Add an entry for a local symbol to the GOT. This returns true if
1392 // this is a new GOT entry, false if the symbol already has a GOT
1393 // entry.
1394 bool
0a65a3a7
CC
1395 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1396 unsigned int got_type);
ead1e424 1397
0a65a3a7 1398 // Add an entry for a local symbol to the GOT, and add a dynamic
7bf1f802
ILT
1399 // relocation of type R_TYPE for the GOT entry.
1400 void
1401 add_local_with_rel(Sized_relobj<size, big_endian>* object,
0a65a3a7
CC
1402 unsigned int sym_index, unsigned int got_type,
1403 Rel_dyn* rel_dyn, unsigned int r_type);
7bf1f802
ILT
1404
1405 void
1406 add_local_with_rela(Sized_relobj<size, big_endian>* object,
0a65a3a7
CC
1407 unsigned int sym_index, unsigned int got_type,
1408 Rela_dyn* rela_dyn, unsigned int r_type);
07f397ab 1409
0a65a3a7
CC
1410 // Add a pair of entries for a local symbol to the GOT, and add
1411 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
7bf1f802 1412 void
0a65a3a7
CC
1413 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1414 unsigned int sym_index, unsigned int shndx,
1415 unsigned int got_type, Rel_dyn* rel_dyn,
1416 unsigned int r_type_1, unsigned int r_type_2);
7bf1f802
ILT
1417
1418 void
0a65a3a7
CC
1419 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1420 unsigned int sym_index, unsigned int shndx,
1421 unsigned int got_type, Rela_dyn* rela_dyn,
1422 unsigned int r_type_1, unsigned int r_type_2);
7bf1f802 1423
ead1e424
ILT
1424 // Add a constant to the GOT. This returns the offset of the new
1425 // entry from the start of the GOT.
1426 unsigned int
1427 add_constant(Valtype constant)
1428 {
1429 this->entries_.push_back(Got_entry(constant));
1430 this->set_got_size();
1431 return this->last_got_offset();
1432 }
1433
27bc2bce 1434 protected:
ead1e424
ILT
1435 // Write out the GOT table.
1436 void
1437 do_write(Output_file*);
1438
1439 private:
1440 // This POD class holds a single GOT entry.
1441 class Got_entry
1442 {
1443 public:
1444 // Create a zero entry.
1445 Got_entry()
1446 : local_sym_index_(CONSTANT_CODE)
1447 { this->u_.constant = 0; }
1448
1449 // Create a global symbol entry.
a3ad94ed 1450 explicit Got_entry(Symbol* gsym)
ead1e424
ILT
1451 : local_sym_index_(GSYM_CODE)
1452 { this->u_.gsym = gsym; }
1453
1454 // Create a local symbol entry.
e727fa71
ILT
1455 Got_entry(Sized_relobj<size, big_endian>* object,
1456 unsigned int local_sym_index)
ead1e424
ILT
1457 : local_sym_index_(local_sym_index)
1458 {
a3ad94ed
ILT
1459 gold_assert(local_sym_index != GSYM_CODE
1460 && local_sym_index != CONSTANT_CODE);
ead1e424
ILT
1461 this->u_.object = object;
1462 }
1463
1464 // Create a constant entry. The constant is a host value--it will
1465 // be swapped, if necessary, when it is written out.
a3ad94ed 1466 explicit Got_entry(Valtype constant)
ead1e424
ILT
1467 : local_sym_index_(CONSTANT_CODE)
1468 { this->u_.constant = constant; }
1469
1470 // Write the GOT entry to an output view.
1471 void
7e1edb90 1472 write(unsigned char* pov) const;
ead1e424
ILT
1473
1474 private:
1475 enum
1476 {
1477 GSYM_CODE = -1U,
1478 CONSTANT_CODE = -2U
1479 };
1480
1481 union
1482 {
1483 // For a local symbol, the object.
e727fa71 1484 Sized_relobj<size, big_endian>* object;
ead1e424
ILT
1485 // For a global symbol, the symbol.
1486 Symbol* gsym;
1487 // For a constant, the constant.
1488 Valtype constant;
1489 } u_;
c06b7b0b
ILT
1490 // For a local symbol, the local symbol index. This is GSYM_CODE
1491 // for a global symbol, or CONSTANT_CODE for a constant.
ead1e424
ILT
1492 unsigned int local_sym_index_;
1493 };
1494
1495 typedef std::vector<Got_entry> Got_entries;
1496
1497 // Return the offset into the GOT of GOT entry I.
1498 unsigned int
1499 got_offset(unsigned int i) const
1500 { return i * (size / 8); }
1501
1502 // Return the offset into the GOT of the last entry added.
1503 unsigned int
1504 last_got_offset() const
1505 { return this->got_offset(this->entries_.size() - 1); }
1506
1507 // Set the size of the section.
1508 void
1509 set_got_size()
27bc2bce 1510 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
ead1e424
ILT
1511
1512 // The list of GOT entries.
1513 Got_entries entries_;
1514};
1515
a3ad94ed
ILT
1516// Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1517// section.
1518
1519class Output_data_dynamic : public Output_section_data
1520{
1521 public:
9025d29d 1522 Output_data_dynamic(Stringpool* pool)
730cdc88 1523 : Output_section_data(Output_data::default_alignment()),
9025d29d 1524 entries_(), pool_(pool)
a3ad94ed
ILT
1525 { }
1526
1527 // Add a new dynamic entry with a fixed numeric value.
1528 void
1529 add_constant(elfcpp::DT tag, unsigned int val)
1530 { this->add_entry(Dynamic_entry(tag, val)); }
1531
16649710 1532 // Add a new dynamic entry with the address of output data.
a3ad94ed 1533 void
16649710
ILT
1534 add_section_address(elfcpp::DT tag, const Output_data* od)
1535 { this->add_entry(Dynamic_entry(tag, od, false)); }
a3ad94ed 1536
16649710 1537 // Add a new dynamic entry with the size of output data.
a3ad94ed 1538 void
16649710
ILT
1539 add_section_size(elfcpp::DT tag, const Output_data* od)
1540 { this->add_entry(Dynamic_entry(tag, od, true)); }
a3ad94ed
ILT
1541
1542 // Add a new dynamic entry with the address of a symbol.
1543 void
16649710 1544 add_symbol(elfcpp::DT tag, const Symbol* sym)
a3ad94ed
ILT
1545 { this->add_entry(Dynamic_entry(tag, sym)); }
1546
1547 // Add a new dynamic entry with a string.
1548 void
1549 add_string(elfcpp::DT tag, const char* str)
cfd73a4e 1550 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
a3ad94ed 1551
41f542e7
ILT
1552 void
1553 add_string(elfcpp::DT tag, const std::string& str)
1554 { this->add_string(tag, str.c_str()); }
1555
27bc2bce
ILT
1556 protected:
1557 // Adjust the output section to set the entry size.
1558 void
1559 do_adjust_output_section(Output_section*);
1560
a3ad94ed
ILT
1561 // Set the final data size.
1562 void
27bc2bce 1563 set_final_data_size();
a3ad94ed
ILT
1564
1565 // Write out the dynamic entries.
1566 void
1567 do_write(Output_file*);
1568
1569 private:
1570 // This POD class holds a single dynamic entry.
1571 class Dynamic_entry
1572 {
1573 public:
1574 // Create an entry with a fixed numeric value.
1575 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1576 : tag_(tag), classification_(DYNAMIC_NUMBER)
1577 { this->u_.val = val; }
1578
1579 // Create an entry with the size or address of a section.
16649710 1580 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
a3ad94ed
ILT
1581 : tag_(tag),
1582 classification_(section_size
1583 ? DYNAMIC_SECTION_SIZE
1584 : DYNAMIC_SECTION_ADDRESS)
16649710 1585 { this->u_.od = od; }
a3ad94ed
ILT
1586
1587 // Create an entry with the address of a symbol.
16649710 1588 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
a3ad94ed
ILT
1589 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1590 { this->u_.sym = sym; }
1591
1592 // Create an entry with a string.
1593 Dynamic_entry(elfcpp::DT tag, const char* str)
1594 : tag_(tag), classification_(DYNAMIC_STRING)
1595 { this->u_.str = str; }
1596
1597 // Write the dynamic entry to an output view.
1598 template<int size, bool big_endian>
1599 void
7d1a9ebb 1600 write(unsigned char* pov, const Stringpool*) const;
a3ad94ed
ILT
1601
1602 private:
1603 enum Classification
1604 {
1605 // Number.
1606 DYNAMIC_NUMBER,
1607 // Section address.
1608 DYNAMIC_SECTION_ADDRESS,
1609 // Section size.
1610 DYNAMIC_SECTION_SIZE,
1611 // Symbol adress.
1612 DYNAMIC_SYMBOL,
1613 // String.
1614 DYNAMIC_STRING
1615 };
1616
1617 union
1618 {
1619 // For DYNAMIC_NUMBER.
1620 unsigned int val;
1621 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
16649710 1622 const Output_data* od;
a3ad94ed 1623 // For DYNAMIC_SYMBOL.
16649710 1624 const Symbol* sym;
a3ad94ed
ILT
1625 // For DYNAMIC_STRING.
1626 const char* str;
1627 } u_;
1628 // The dynamic tag.
1629 elfcpp::DT tag_;
1630 // The type of entry.
1631 Classification classification_;
1632 };
1633
1634 // Add an entry to the list.
1635 void
1636 add_entry(const Dynamic_entry& entry)
1637 { this->entries_.push_back(entry); }
1638
1639 // Sized version of write function.
1640 template<int size, bool big_endian>
1641 void
1642 sized_write(Output_file* of);
1643
1644 // The type of the list of entries.
1645 typedef std::vector<Dynamic_entry> Dynamic_entries;
1646
a3ad94ed
ILT
1647 // The entries.
1648 Dynamic_entries entries_;
1649 // The pool used for strings.
1650 Stringpool* pool_;
1651};
1652
a2fb1b05
ILT
1653// An output section. We don't expect to have too many output
1654// sections, so we don't bother to do a template on the size.
1655
54dc6425 1656class Output_section : public Output_data
a2fb1b05
ILT
1657{
1658 public:
1659 // Create an output section, giving the name, type, and flags.
96803768 1660 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
54dc6425 1661 virtual ~Output_section();
a2fb1b05 1662
ead1e424 1663 // Add a new input section SHNDX, named NAME, with header SHDR, from
730cdc88
ILT
1664 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1665 // which applies to this section, or 0 if none, or -1U if more than
a445fddf
ILT
1666 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1667 // in a linker script; in that case we need to keep track of input
1668 // sections associated with an output section. Return the offset
1669 // within the output section.
a2fb1b05
ILT
1670 template<int size, bool big_endian>
1671 off_t
730cdc88
ILT
1672 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1673 const char *name,
1674 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf 1675 unsigned int reloc_shndx, bool have_sections_script);
a2fb1b05 1676
b8e6aad9 1677 // Add generated data POSD to this output section.
c06b7b0b 1678 void
ead1e424
ILT
1679 add_output_section_data(Output_section_data* posd);
1680
a2fb1b05
ILT
1681 // Return the section name.
1682 const char*
1683 name() const
1684 { return this->name_; }
1685
1686 // Return the section type.
1687 elfcpp::Elf_Word
1688 type() const
1689 { return this->type_; }
1690
1691 // Return the section flags.
1692 elfcpp::Elf_Xword
1693 flags() const
1694 { return this->flags_; }
1695
1650c4ff
ILT
1696 // Set the section flags. This may only be used with the Layout
1697 // code when it is prepared to move the section to a different
1698 // segment.
1699 void
1700 set_flags(elfcpp::Elf_Xword flags)
1701 { this->flags_ = flags; }
1702
a3ad94ed
ILT
1703 // Return the entsize field.
1704 uint64_t
1705 entsize() const
1706 { return this->entsize_; }
1707
61ba1cf9
ILT
1708 // Set the entsize field.
1709 void
16649710 1710 set_entsize(uint64_t v);
61ba1cf9 1711
a445fddf
ILT
1712 // Set the load address.
1713 void
1714 set_load_address(uint64_t load_address)
1715 {
1716 this->load_address_ = load_address;
1717 this->has_load_address_ = true;
1718 }
1719
16649710
ILT
1720 // Set the link field to the output section index of a section.
1721 void
14b31740 1722 set_link_section(const Output_data* od)
16649710
ILT
1723 {
1724 gold_assert(this->link_ == 0
1725 && !this->should_link_to_symtab_
1726 && !this->should_link_to_dynsym_);
1727 this->link_section_ = od;
1728 }
1729
1730 // Set the link field to a constant.
61ba1cf9
ILT
1731 void
1732 set_link(unsigned int v)
16649710
ILT
1733 {
1734 gold_assert(this->link_section_ == NULL
1735 && !this->should_link_to_symtab_
1736 && !this->should_link_to_dynsym_);
1737 this->link_ = v;
1738 }
61ba1cf9 1739
16649710
ILT
1740 // Record that this section should link to the normal symbol table.
1741 void
1742 set_should_link_to_symtab()
1743 {
1744 gold_assert(this->link_section_ == NULL
1745 && this->link_ == 0
1746 && !this->should_link_to_dynsym_);
1747 this->should_link_to_symtab_ = true;
1748 }
1749
1750 // Record that this section should link to the dynamic symbol table.
1751 void
1752 set_should_link_to_dynsym()
1753 {
1754 gold_assert(this->link_section_ == NULL
1755 && this->link_ == 0
1756 && !this->should_link_to_symtab_);
1757 this->should_link_to_dynsym_ = true;
1758 }
1759
1760 // Return the info field.
1761 unsigned int
1762 info() const
1763 {
755ab8af
ILT
1764 gold_assert(this->info_section_ == NULL
1765 && this->info_symndx_ == NULL);
16649710
ILT
1766 return this->info_;
1767 }
1768
1769 // Set the info field to the output section index of a section.
1770 void
755ab8af 1771 set_info_section(const Output_section* os)
16649710 1772 {
755ab8af
ILT
1773 gold_assert((this->info_section_ == NULL
1774 || (this->info_section_ == os
1775 && this->info_uses_section_index_))
1776 && this->info_symndx_ == NULL
1777 && this->info_ == 0);
1778 this->info_section_ = os;
1779 this->info_uses_section_index_= true;
16649710
ILT
1780 }
1781
6a74a719
ILT
1782 // Set the info field to the symbol table index of a symbol.
1783 void
1784 set_info_symndx(const Symbol* sym)
1785 {
755ab8af
ILT
1786 gold_assert(this->info_section_ == NULL
1787 && (this->info_symndx_ == NULL
1788 || this->info_symndx_ == sym)
1789 && this->info_ == 0);
6a74a719
ILT
1790 this->info_symndx_ = sym;
1791 }
1792
755ab8af
ILT
1793 // Set the info field to the symbol table index of a section symbol.
1794 void
1795 set_info_section_symndx(const Output_section* os)
1796 {
1797 gold_assert((this->info_section_ == NULL
1798 || (this->info_section_ == os
1799 && !this->info_uses_section_index_))
1800 && this->info_symndx_ == NULL
1801 && this->info_ == 0);
1802 this->info_section_ = os;
1803 this->info_uses_section_index_ = false;
1804 }
1805
16649710 1806 // Set the info field to a constant.
61ba1cf9
ILT
1807 void
1808 set_info(unsigned int v)
16649710 1809 {
755ab8af
ILT
1810 gold_assert(this->info_section_ == NULL
1811 && this->info_symndx_ == NULL
1812 && (this->info_ == 0
1813 || this->info_ == v));
16649710
ILT
1814 this->info_ = v;
1815 }
61ba1cf9
ILT
1816
1817 // Set the addralign field.
1818 void
1819 set_addralign(uint64_t v)
1820 { this->addralign_ = v; }
1821
c06b7b0b
ILT
1822 // Indicate that we need a symtab index.
1823 void
1824 set_needs_symtab_index()
1825 { this->needs_symtab_index_ = true; }
1826
1827 // Return whether we need a symtab index.
1828 bool
1829 needs_symtab_index() const
1830 { return this->needs_symtab_index_; }
1831
1832 // Get the symtab index.
1833 unsigned int
1834 symtab_index() const
1835 {
a3ad94ed 1836 gold_assert(this->symtab_index_ != 0);
c06b7b0b
ILT
1837 return this->symtab_index_;
1838 }
1839
1840 // Set the symtab index.
1841 void
1842 set_symtab_index(unsigned int index)
1843 {
a3ad94ed 1844 gold_assert(index != 0);
c06b7b0b
ILT
1845 this->symtab_index_ = index;
1846 }
1847
1848 // Indicate that we need a dynsym index.
1849 void
1850 set_needs_dynsym_index()
1851 { this->needs_dynsym_index_ = true; }
1852
1853 // Return whether we need a dynsym index.
1854 bool
1855 needs_dynsym_index() const
1856 { return this->needs_dynsym_index_; }
1857
1858 // Get the dynsym index.
1859 unsigned int
1860 dynsym_index() const
1861 {
a3ad94ed 1862 gold_assert(this->dynsym_index_ != 0);
c06b7b0b
ILT
1863 return this->dynsym_index_;
1864 }
1865
1866 // Set the dynsym index.
1867 void
1868 set_dynsym_index(unsigned int index)
1869 {
a3ad94ed 1870 gold_assert(index != 0);
c06b7b0b
ILT
1871 this->dynsym_index_ = index;
1872 }
1873
2fd32231
ILT
1874 // Return whether the input sections sections attachd to this output
1875 // section may require sorting. This is used to handle constructor
1876 // priorities compatibly with GNU ld.
1877 bool
1878 may_sort_attached_input_sections() const
1879 { return this->may_sort_attached_input_sections_; }
1880
1881 // Record that the input sections attached to this output section
1882 // may require sorting.
1883 void
1884 set_may_sort_attached_input_sections()
1885 { this->may_sort_attached_input_sections_ = true; }
1886
1887 // Return whether the input sections attached to this output section
1888 // require sorting. This is used to handle constructor priorities
1889 // compatibly with GNU ld.
1890 bool
1891 must_sort_attached_input_sections() const
1892 { return this->must_sort_attached_input_sections_; }
1893
1894 // Record that the input sections attached to this output section
1895 // require sorting.
1896 void
1897 set_must_sort_attached_input_sections()
1898 { this->must_sort_attached_input_sections_ = true; }
1899
730cdc88
ILT
1900 // Return whether this section should be written after all the input
1901 // sections are complete.
1902 bool
1903 after_input_sections() const
1904 { return this->after_input_sections_; }
1905
1906 // Record that this section should be written after all the input
1907 // sections are complete.
1908 void
1909 set_after_input_sections()
1910 { this->after_input_sections_ = true; }
1911
27bc2bce
ILT
1912 // Return whether this section requires postprocessing after all
1913 // relocations have been applied.
1914 bool
1915 requires_postprocessing() const
1916 { return this->requires_postprocessing_; }
1917
96803768
ILT
1918 // If a section requires postprocessing, return the buffer to use.
1919 unsigned char*
1920 postprocessing_buffer() const
1921 {
1922 gold_assert(this->postprocessing_buffer_ != NULL);
1923 return this->postprocessing_buffer_;
1924 }
1925
1926 // If a section requires postprocessing, create the buffer to use.
27bc2bce 1927 void
96803768
ILT
1928 create_postprocessing_buffer();
1929
1930 // If a section requires postprocessing, this is the size of the
1931 // buffer to which relocations should be applied.
1932 off_t
1933 postprocessing_buffer_size() const
1934 { return this->current_data_size_for_child(); }
27bc2bce 1935
755ab8af
ILT
1936 // Modify the section name. This is only permitted for an
1937 // unallocated section, and only before the size has been finalized.
1938 // Otherwise the name will not get into Layout::namepool_.
1939 void
1940 set_name(const char* newname)
1941 {
1942 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1943 gold_assert(!this->is_data_size_valid());
1944 this->name_ = newname;
1945 }
1946
730cdc88
ILT
1947 // Return whether the offset OFFSET in the input section SHNDX in
1948 // object OBJECT is being included in the link.
1949 bool
1950 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1951 off_t offset) const;
1952
1953 // Return the offset within the output section of OFFSET relative to
1954 // the start of input section SHNDX in object OBJECT.
8383303e
ILT
1955 section_offset_type
1956 output_offset(const Relobj* object, unsigned int shndx,
1957 section_offset_type offset) const;
730cdc88 1958
b8e6aad9
ILT
1959 // Return the output virtual address of OFFSET relative to the start
1960 // of input section SHNDX in object OBJECT.
1961 uint64_t
1962 output_address(const Relobj* object, unsigned int shndx,
1963 off_t offset) const;
1964
a9a60db6
ILT
1965 // Return the output address of the start of the merged section for
1966 // input section SHNDX in object OBJECT. This is not necessarily
1967 // the offset corresponding to input offset 0 in the section, since
1968 // the section may be mapped arbitrarily.
1969 uint64_t
1970 starting_output_address(const Relobj* object, unsigned int shndx) const;
1971
a445fddf
ILT
1972 // Record that this output section was found in the SECTIONS clause
1973 // of a linker script.
1974 void
1975 set_found_in_sections_clause()
1976 { this->found_in_sections_clause_ = true; }
1977
1978 // Return whether this output section was found in the SECTIONS
1979 // clause of a linker script.
1980 bool
1981 found_in_sections_clause() const
1982 { return this->found_in_sections_clause_; }
1983
27bc2bce
ILT
1984 // Write the section header into *OPHDR.
1985 template<int size, bool big_endian>
1986 void
1987 write_header(const Layout*, const Stringpool*,
1988 elfcpp::Shdr_write<size, big_endian>*) const;
1989
a445fddf
ILT
1990 // The next few calls are for linker script support.
1991
1992 // Store the list of input sections for this Output_section into the
1993 // list passed in. This removes the input sections, leaving only
1994 // any Output_section_data elements. This returns the size of those
1995 // Output_section_data elements. ADDRESS is the address of this
1996 // output section. FILL is the fill value to use, in case there are
1997 // any spaces between the remaining Output_section_data elements.
1998 uint64_t
1999 get_input_sections(uint64_t address, const std::string& fill,
2000 std::list<std::pair<Relobj*, unsigned int > >*);
2001
2002 // Add an input section from a script.
2003 void
2004 add_input_section_for_script(Relobj* object, unsigned int shndx,
2005 off_t data_size, uint64_t addralign);
2006
2007 // Set the current size of the output section.
2008 void
2009 set_current_data_size(off_t size)
2010 { this->set_current_data_size_for_child(size); }
2011
2012 // Get the current size of the output section.
2013 off_t
2014 current_data_size() const
2015 { return this->current_data_size_for_child(); }
2016
2017 // End of linker script support.
2018
38c5e8b4
ILT
2019 // Print merge statistics to stderr.
2020 void
2021 print_merge_stats();
2022
27bc2bce 2023 protected:
77e65537
ILT
2024 // Return the output section--i.e., the object itself.
2025 Output_section*
2026 do_output_section()
2027 { return this; }
2028
27bc2bce
ILT
2029 // Return the section index in the output file.
2030 unsigned int
2031 do_out_shndx() const
2032 {
2033 gold_assert(this->out_shndx_ != -1U);
2034 return this->out_shndx_;
2035 }
2036
2037 // Set the output section index.
2038 void
2039 do_set_out_shndx(unsigned int shndx)
2040 {
a445fddf 2041 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
27bc2bce
ILT
2042 this->out_shndx_ = shndx;
2043 }
2044
2045 // Set the final data size of the Output_section. For a typical
ead1e424 2046 // Output_section, there is nothing to do, but if there are any
27bc2bce 2047 // Output_section_data objects we need to set their final addresses
ead1e424 2048 // here.
96803768 2049 virtual void
27bc2bce 2050 set_final_data_size();
ead1e424 2051
a445fddf
ILT
2052 // Reset the address and file offset.
2053 void
2054 do_reset_address_and_file_offset();
2055
54dc6425 2056 // Write the data to the file. For a typical Output_section, this
ead1e424
ILT
2057 // does nothing: the data is written out by calling Object::Relocate
2058 // on each input object. But if there are any Output_section_data
2059 // objects we do need to write them out here.
96803768 2060 virtual void
ead1e424 2061 do_write(Output_file*);
54dc6425 2062
75f65a3e
ILT
2063 // Return the address alignment--function required by parent class.
2064 uint64_t
2065 do_addralign() const
2066 { return this->addralign_; }
2067
a445fddf
ILT
2068 // Return whether there is a load address.
2069 bool
2070 do_has_load_address() const
2071 { return this->has_load_address_; }
2072
2073 // Return the load address.
2074 uint64_t
2075 do_load_address() const
2076 {
2077 gold_assert(this->has_load_address_);
2078 return this->load_address_;
2079 }
2080
75f65a3e
ILT
2081 // Return whether this is an Output_section.
2082 bool
2083 do_is_section() const
2084 { return true; }
2085
54dc6425
ILT
2086 // Return whether this is a section of the specified type.
2087 bool
75f65a3e 2088 do_is_section_type(elfcpp::Elf_Word type) const
54dc6425
ILT
2089 { return this->type_ == type; }
2090
2091 // Return whether the specified section flag is set.
2092 bool
75f65a3e 2093 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
54dc6425
ILT
2094 { return (this->flags_ & flag) != 0; }
2095
7bf1f802
ILT
2096 // Set the TLS offset. Called only for SHT_TLS sections.
2097 void
2098 do_set_tls_offset(uint64_t tls_base);
2099
2100 // Return the TLS offset, relative to the base of the TLS segment.
2101 // Valid only for SHT_TLS sections.
2102 uint64_t
2103 do_tls_offset() const
2104 { return this->tls_offset_; }
2105
96803768
ILT
2106 // This may be implemented by a child class.
2107 virtual void
2108 do_finalize_name(Layout*)
2109 { }
2110
2111 // Record that this section requires postprocessing after all
2112 // relocations have been applied. This is called by a child class.
2113 void
2114 set_requires_postprocessing()
2115 {
2116 this->requires_postprocessing_ = true;
2117 this->after_input_sections_ = true;
2118 }
2119
2120 // Write all the data of an Output_section into the postprocessing
2121 // buffer.
2122 void
2123 write_to_postprocessing_buffer();
2124
a2fb1b05 2125 private:
ead1e424
ILT
2126 // In some cases we need to keep a list of the input sections
2127 // associated with this output section. We only need the list if we
2128 // might have to change the offsets of the input section within the
2129 // output section after we add the input section. The ordinary
2130 // input sections will be written out when we process the object
2131 // file, and as such we don't need to track them here. We do need
2132 // to track Output_section_data objects here. We store instances of
2133 // this structure in a std::vector, so it must be a POD. There can
2134 // be many instances of this structure, so we use a union to save
2135 // some space.
2136 class Input_section
2137 {
2138 public:
2139 Input_section()
b8e6aad9
ILT
2140 : shndx_(0), p2align_(0)
2141 {
2142 this->u1_.data_size = 0;
2143 this->u2_.object = NULL;
2144 }
ead1e424 2145
b8e6aad9 2146 // For an ordinary input section.
f6ce93d6 2147 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
ead1e424
ILT
2148 uint64_t addralign)
2149 : shndx_(shndx),
b8e6aad9 2150 p2align_(ffsll(static_cast<long long>(addralign)))
ead1e424 2151 {
b8e6aad9
ILT
2152 gold_assert(shndx != OUTPUT_SECTION_CODE
2153 && shndx != MERGE_DATA_SECTION_CODE
2154 && shndx != MERGE_STRING_SECTION_CODE);
2155 this->u1_.data_size = data_size;
2156 this->u2_.object = object;
ead1e424
ILT
2157 }
2158
b8e6aad9 2159 // For a non-merge output section.
ead1e424 2160 Input_section(Output_section_data* posd)
b8e6aad9
ILT
2161 : shndx_(OUTPUT_SECTION_CODE),
2162 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2163 {
2164 this->u1_.data_size = 0;
2165 this->u2_.posd = posd;
2166 }
2167
2168 // For a merge section.
2169 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2170 : shndx_(is_string
2171 ? MERGE_STRING_SECTION_CODE
2172 : MERGE_DATA_SECTION_CODE),
2173 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2174 {
2175 this->u1_.entsize = entsize;
2176 this->u2_.posd = posd;
2177 }
ead1e424
ILT
2178
2179 // The required alignment.
2180 uint64_t
2181 addralign() const
a3ad94ed
ILT
2182 {
2183 return (this->p2align_ == 0
2184 ? 0
2185 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2186 }
ead1e424
ILT
2187
2188 // Return the required size.
2189 off_t
2190 data_size() const;
2191
a445fddf
ILT
2192 // Whether this is an input section.
2193 bool
2194 is_input_section() const
2195 {
2196 return (this->shndx_ != OUTPUT_SECTION_CODE
2197 && this->shndx_ != MERGE_DATA_SECTION_CODE
2198 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2199 }
2200
b8e6aad9
ILT
2201 // Return whether this is a merge section which matches the
2202 // parameters.
2203 bool
87f95776
ILT
2204 is_merge_section(bool is_string, uint64_t entsize,
2205 uint64_t addralign) const
b8e6aad9
ILT
2206 {
2207 return (this->shndx_ == (is_string
2208 ? MERGE_STRING_SECTION_CODE
2209 : MERGE_DATA_SECTION_CODE)
87f95776
ILT
2210 && this->u1_.entsize == entsize
2211 && this->addralign() == addralign);
b8e6aad9
ILT
2212 }
2213
a445fddf
ILT
2214 // Return the object for an input section.
2215 Relobj*
2216 relobj() const
2217 {
2218 gold_assert(this->is_input_section());
2219 return this->u2_.object;
2220 }
2221
2222 // Return the input section index for an input section.
2223 unsigned int
2224 shndx() const
2225 {
2226 gold_assert(this->is_input_section());
2227 return this->shndx_;
2228 }
2229
b8e6aad9
ILT
2230 // Set the output section.
2231 void
2232 set_output_section(Output_section* os)
2233 {
2234 gold_assert(!this->is_input_section());
2235 this->u2_.posd->set_output_section(os);
2236 }
2237
ead1e424 2238 // Set the address and file offset. This is called during
96803768
ILT
2239 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2240 // the enclosing section.
ead1e424 2241 void
96803768
ILT
2242 set_address_and_file_offset(uint64_t address, off_t file_offset,
2243 off_t section_file_offset);
ead1e424 2244
a445fddf
ILT
2245 // Reset the address and file offset.
2246 void
2247 reset_address_and_file_offset();
2248
96803768
ILT
2249 // Finalize the data size.
2250 void
2251 finalize_data_size();
9a0910c3 2252
b8e6aad9
ILT
2253 // Add an input section, for SHF_MERGE sections.
2254 bool
2255 add_input_section(Relobj* object, unsigned int shndx)
2256 {
2257 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2258 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2259 return this->u2_.posd->add_input_section(object, shndx);
2260 }
2261
2262 // Given an input OBJECT, an input section index SHNDX within that
2263 // object, and an OFFSET relative to the start of that input
730cdc88 2264 // section, return whether or not the output offset is known. If
1e983657
ILT
2265 // this function returns true, it sets *POUTPUT to the offset in
2266 // the output section, relative to the start of the input section
2267 // in the output section. *POUTPUT may be different from OFFSET
2268 // for a merged section.
b8e6aad9 2269 bool
8383303e
ILT
2270 output_offset(const Relobj* object, unsigned int shndx,
2271 section_offset_type offset,
2272 section_offset_type *poutput) const;
b8e6aad9 2273
a9a60db6
ILT
2274 // Return whether this is the merge section for the input section
2275 // SHNDX in OBJECT.
2276 bool
2277 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2278
ead1e424
ILT
2279 // Write out the data. This does nothing for an input section.
2280 void
2281 write(Output_file*);
2282
96803768
ILT
2283 // Write the data to a buffer. This does nothing for an input
2284 // section.
2285 void
2286 write_to_buffer(unsigned char*);
2287
38c5e8b4
ILT
2288 // Print statistics about merge sections to stderr.
2289 void
2290 print_merge_stats(const char* section_name)
2291 {
2292 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2293 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2294 this->u2_.posd->print_merge_stats(section_name);
2295 }
2296
ead1e424 2297 private:
b8e6aad9
ILT
2298 // Code values which appear in shndx_. If the value is not one of
2299 // these codes, it is the input section index in the object file.
2300 enum
2301 {
2302 // An Output_section_data.
2303 OUTPUT_SECTION_CODE = -1U,
2304 // An Output_section_data for an SHF_MERGE section with
2305 // SHF_STRINGS not set.
2306 MERGE_DATA_SECTION_CODE = -2U,
2307 // An Output_section_data for an SHF_MERGE section with
2308 // SHF_STRINGS set.
2309 MERGE_STRING_SECTION_CODE = -3U
2310 };
2311
b8e6aad9
ILT
2312 // For an ordinary input section, this is the section index in the
2313 // input file. For an Output_section_data, this is
2314 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2315 // MERGE_STRING_SECTION_CODE.
ead1e424
ILT
2316 unsigned int shndx_;
2317 // The required alignment, stored as a power of 2.
2318 unsigned int p2align_;
ead1e424
ILT
2319 union
2320 {
b8e6aad9
ILT
2321 // For an ordinary input section, the section size.
2322 off_t data_size;
2323 // For OUTPUT_SECTION_CODE, this is not used. For
2324 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2325 // entity size.
2326 uint64_t entsize;
2327 } u1_;
2328 union
2329 {
2330 // For an ordinary input section, the object which holds the
ead1e424 2331 // input section.
f6ce93d6 2332 Relobj* object;
b8e6aad9
ILT
2333 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2334 // MERGE_STRING_SECTION_CODE, the data.
ead1e424 2335 Output_section_data* posd;
b8e6aad9 2336 } u2_;
ead1e424
ILT
2337 };
2338
2339 typedef std::vector<Input_section> Input_section_list;
2340
2fd32231
ILT
2341 // This class is used to sort the input sections.
2342 class Input_section_sort_entry;
2343
2344 // This is the sort comparison function.
2345 struct Input_section_sort_compare
2346 {
2347 bool
2348 operator()(const Input_section_sort_entry&,
2349 const Input_section_sort_entry&) const;
2350 };
2351
c51e6221 2352 // Fill data. This is used to fill in data between input sections.
a445fddf
ILT
2353 // It is also used for data statements (BYTE, WORD, etc.) in linker
2354 // scripts. When we have to keep track of the input sections, we
2355 // can use an Output_data_const, but we don't want to have to keep
2356 // track of input sections just to implement fills.
c51e6221
ILT
2357 class Fill
2358 {
2359 public:
2360 Fill(off_t section_offset, off_t length)
a445fddf
ILT
2361 : section_offset_(section_offset),
2362 length_(convert_to_section_size_type(length))
c51e6221
ILT
2363 { }
2364
2365 // Return section offset.
2366 off_t
2367 section_offset() const
2368 { return this->section_offset_; }
2369
2370 // Return fill length.
a445fddf 2371 section_size_type
c51e6221
ILT
2372 length() const
2373 { return this->length_; }
2374
2375 private:
2376 // The offset within the output section.
2377 off_t section_offset_;
2378 // The length of the space to fill.
a445fddf 2379 section_size_type length_;
c51e6221
ILT
2380 };
2381
2382 typedef std::vector<Fill> Fill_list;
2383
b8e6aad9
ILT
2384 // Add a new output section by Input_section.
2385 void
2386 add_output_section_data(Input_section*);
2387
2388 // Add an SHF_MERGE input section. Returns true if the section was
2389 // handled.
2390 bool
2391 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
96803768 2392 uint64_t entsize, uint64_t addralign);
b8e6aad9
ILT
2393
2394 // Add an output SHF_MERGE section POSD to this output section.
2395 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2396 // ENTSIZE is the entity size. This returns the entry added to
2397 // input_sections_.
2398 void
2399 add_output_merge_section(Output_section_data* posd, bool is_string,
2400 uint64_t entsize);
2401
2fd32231
ILT
2402 // Sort the attached input sections.
2403 void
2404 sort_attached_input_sections();
2405
a2fb1b05
ILT
2406 // Most of these fields are only valid after layout.
2407
2408 // The name of the section. This will point into a Stringpool.
9a0910c3 2409 const char* name_;
75f65a3e 2410 // The section address is in the parent class.
a2fb1b05
ILT
2411 // The section alignment.
2412 uint64_t addralign_;
2413 // The section entry size.
2414 uint64_t entsize_;
a445fddf
ILT
2415 // The load address. This is only used when using a linker script
2416 // with a SECTIONS clause. The has_load_address_ field indicates
2417 // whether this field is valid.
2418 uint64_t load_address_;
75f65a3e 2419 // The file offset is in the parent class.
16649710 2420 // Set the section link field to the index of this section.
14b31740 2421 const Output_data* link_section_;
16649710 2422 // If link_section_ is NULL, this is the link field.
a2fb1b05 2423 unsigned int link_;
16649710 2424 // Set the section info field to the index of this section.
755ab8af 2425 const Output_section* info_section_;
6a74a719
ILT
2426 // If info_section_ is NULL, set the info field to the symbol table
2427 // index of this symbol.
2428 const Symbol* info_symndx_;
2429 // If info_section_ and info_symndx_ are NULL, this is the section
2430 // info field.
a2fb1b05
ILT
2431 unsigned int info_;
2432 // The section type.
27bc2bce 2433 const elfcpp::Elf_Word type_;
a2fb1b05 2434 // The section flags.
a445fddf 2435 elfcpp::Elf_Xword flags_;
61ba1cf9 2436 // The section index.
ead1e424 2437 unsigned int out_shndx_;
c06b7b0b
ILT
2438 // If there is a STT_SECTION for this output section in the normal
2439 // symbol table, this is the symbol index. This starts out as zero.
2440 // It is initialized in Layout::finalize() to be the index, or -1U
2441 // if there isn't one.
2442 unsigned int symtab_index_;
2443 // If there is a STT_SECTION for this output section in the dynamic
2444 // symbol table, this is the symbol index. This starts out as zero.
2445 // It is initialized in Layout::finalize() to be the index, or -1U
2446 // if there isn't one.
2447 unsigned int dynsym_index_;
ead1e424
ILT
2448 // The input sections. This will be empty in cases where we don't
2449 // need to keep track of them.
2450 Input_section_list input_sections_;
2451 // The offset of the first entry in input_sections_.
2452 off_t first_input_offset_;
c51e6221
ILT
2453 // The fill data. This is separate from input_sections_ because we
2454 // often will need fill sections without needing to keep track of
2455 // input sections.
2456 Fill_list fills_;
96803768
ILT
2457 // If the section requires postprocessing, this buffer holds the
2458 // section contents during relocation.
2459 unsigned char* postprocessing_buffer_;
c06b7b0b
ILT
2460 // Whether this output section needs a STT_SECTION symbol in the
2461 // normal symbol table. This will be true if there is a relocation
2462 // which needs it.
2463 bool needs_symtab_index_ : 1;
2464 // Whether this output section needs a STT_SECTION symbol in the
2465 // dynamic symbol table. This will be true if there is a dynamic
2466 // relocation which needs it.
2467 bool needs_dynsym_index_ : 1;
16649710
ILT
2468 // Whether the link field of this output section should point to the
2469 // normal symbol table.
2470 bool should_link_to_symtab_ : 1;
2471 // Whether the link field of this output section should point to the
2472 // dynamic symbol table.
2473 bool should_link_to_dynsym_ : 1;
730cdc88
ILT
2474 // Whether this section should be written after all the input
2475 // sections are complete.
2476 bool after_input_sections_ : 1;
27bc2bce
ILT
2477 // Whether this section requires post processing after all
2478 // relocations have been applied.
2479 bool requires_postprocessing_ : 1;
a445fddf
ILT
2480 // Whether an input section was mapped to this output section
2481 // because of a SECTIONS clause in a linker script.
2482 bool found_in_sections_clause_ : 1;
2483 // Whether this section has an explicitly specified load address.
2484 bool has_load_address_ : 1;
755ab8af
ILT
2485 // True if the info_section_ field means the section index of the
2486 // section, false if it means the symbol index of the corresponding
2487 // section symbol.
2488 bool info_uses_section_index_ : 1;
2fd32231
ILT
2489 // True if the input sections attached to this output section may
2490 // need sorting.
2491 bool may_sort_attached_input_sections_ : 1;
2492 // True if the input sections attached to this output section must
2493 // be sorted.
2494 bool must_sort_attached_input_sections_ : 1;
2495 // True if the input sections attached to this output section have
2496 // already been sorted.
2497 bool attached_input_sections_are_sorted_ : 1;
7bf1f802
ILT
2498 // For SHT_TLS sections, the offset of this section relative to the base
2499 // of the TLS segment.
2500 uint64_t tls_offset_;
a2fb1b05
ILT
2501};
2502
2503// An output segment. PT_LOAD segments are built from collections of
2504// output sections. Other segments typically point within PT_LOAD
2505// segments, and are built directly as needed.
2506
2507class Output_segment
2508{
2509 public:
2510 // Create an output segment, specifying the type and flags.
2511 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2512
2513 // Return the virtual address.
2514 uint64_t
2515 vaddr() const
2516 { return this->vaddr_; }
2517
2518 // Return the physical address.
2519 uint64_t
2520 paddr() const
2521 { return this->paddr_; }
2522
2523 // Return the segment type.
2524 elfcpp::Elf_Word
2525 type() const
2526 { return this->type_; }
2527
2528 // Return the segment flags.
2529 elfcpp::Elf_Word
2530 flags() const
2531 { return this->flags_; }
2532
92e059d8
ILT
2533 // Return the memory size.
2534 uint64_t
2535 memsz() const
2536 { return this->memsz_; }
2537
ead1e424
ILT
2538 // Return the file size.
2539 off_t
2540 filesz() const
2541 { return this->filesz_; }
2542
516cb3d0
ILT
2543 // Return the file offset.
2544 off_t
2545 offset() const
2546 { return this->offset_; }
2547
75f65a3e
ILT
2548 // Return the maximum alignment of the Output_data.
2549 uint64_t
a445fddf 2550 maximum_alignment();
75f65a3e 2551
a2fb1b05
ILT
2552 // Add an Output_section to this segment.
2553 void
dbe717ef
ILT
2554 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2555 { this->add_output_section(os, seg_flags, false); }
2556
2557 // Add an Output_section to the start of this segment.
2558 void
2559 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2560 { this->add_output_section(os, seg_flags, true); }
75f65a3e 2561
1650c4ff
ILT
2562 // Remove an Output_section from this segment. It is an error if it
2563 // is not present.
2564 void
2565 remove_output_section(Output_section* os);
2566
75f65a3e
ILT
2567 // Add an Output_data (which is not an Output_section) to the start
2568 // of this segment.
2569 void
2570 add_initial_output_data(Output_data*);
2571
756ac4a8
ILT
2572 // Return true if this segment has any sections which hold actual
2573 // data, rather than being a BSS section.
2574 bool
2575 has_any_data_sections() const
2576 { return !this->output_data_.empty(); }
2577
4f4c5f80
ILT
2578 // Return the number of dynamic relocations applied to this segment.
2579 unsigned int
2580 dynamic_reloc_count() const;
2581
a445fddf
ILT
2582 // Return the address of the first section.
2583 uint64_t
2584 first_section_load_address() const;
2585
2586 // Return whether the addresses have been set already.
2587 bool
2588 are_addresses_set() const
2589 { return this->are_addresses_set_; }
2590
2591 // Set the addresses.
2592 void
2593 set_addresses(uint64_t vaddr, uint64_t paddr)
2594 {
2595 this->vaddr_ = vaddr;
2596 this->paddr_ = paddr;
2597 this->are_addresses_set_ = true;
2598 }
2599
1c4f3631
ILT
2600 // Set the segment flags. This is only used if we have a PHDRS
2601 // clause which explicitly specifies the flags.
2602 void
2603 set_flags(elfcpp::Elf_Word flags)
2604 { this->flags_ = flags; }
2605
75f65a3e 2606 // Set the address of the segment to ADDR and the offset to *POFF
a445fddf
ILT
2607 // and set the addresses and offsets of all contained output
2608 // sections accordingly. Set the section indexes of all contained
2609 // output sections starting with *PSHNDX. If RESET is true, first
2610 // reset the addresses of the contained sections. Return the
2611 // address of the immediately following segment. Update *POFF and
2612 // *PSHNDX. This should only be called for a PT_LOAD segment.
75f65a3e 2613 uint64_t
96a2b4e4 2614 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
a445fddf 2615 unsigned int* pshndx);
75f65a3e 2616
0496d5e5
ILT
2617 // Set the minimum alignment of this segment. This may be adjusted
2618 // upward based on the section alignments.
2619 void
a445fddf
ILT
2620 set_minimum_p_align(uint64_t align)
2621 { this->min_p_align_ = align; }
0496d5e5 2622
75f65a3e
ILT
2623 // Set the offset of this segment based on the section. This should
2624 // only be called for a non-PT_LOAD segment.
2625 void
2626 set_offset();
2627
7bf1f802
ILT
2628 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2629 void
2630 set_tls_offsets();
2631
75f65a3e
ILT
2632 // Return the number of output sections.
2633 unsigned int
2634 output_section_count() const;
a2fb1b05 2635
1c4f3631
ILT
2636 // Return the section attached to the list segment with the lowest
2637 // load address. This is used when handling a PHDRS clause in a
2638 // linker script.
2639 Output_section*
2640 section_with_lowest_load_address() const;
2641
61ba1cf9
ILT
2642 // Write the segment header into *OPHDR.
2643 template<int size, bool big_endian>
2644 void
ead1e424 2645 write_header(elfcpp::Phdr_write<size, big_endian>*);
61ba1cf9
ILT
2646
2647 // Write the section headers of associated sections into V.
2648 template<int size, bool big_endian>
2649 unsigned char*
16649710 2650 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
7d1a9ebb 2651 unsigned int* pshndx) const;
61ba1cf9 2652
a2fb1b05
ILT
2653 private:
2654 Output_segment(const Output_segment&);
2655 Output_segment& operator=(const Output_segment&);
2656
54dc6425 2657 typedef std::list<Output_data*> Output_data_list;
a2fb1b05 2658
dbe717ef
ILT
2659 // Add an Output_section to this segment, specifying front or back.
2660 void
2661 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2662 bool front);
2663
ead1e424
ILT
2664 // Find the maximum alignment in an Output_data_list.
2665 static uint64_t
a445fddf 2666 maximum_alignment_list(const Output_data_list*);
ead1e424 2667
75f65a3e
ILT
2668 // Set the section addresses in an Output_data_list.
2669 uint64_t
96a2b4e4
ILT
2670 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
2671 uint64_t addr, off_t* poff, unsigned int* pshndx,
2672 bool* in_tls);
75f65a3e
ILT
2673
2674 // Return the number of Output_sections in an Output_data_list.
2675 unsigned int
2676 output_section_count_list(const Output_data_list*) const;
2677
4f4c5f80
ILT
2678 // Return the number of dynamic relocs in an Output_data_list.
2679 unsigned int
2680 dynamic_reloc_count_list(const Output_data_list*) const;
2681
1c4f3631
ILT
2682 // Find the section with the lowest load address in an
2683 // Output_data_list.
2684 void
2685 lowest_load_address_in_list(const Output_data_list* pdl,
2686 Output_section** found,
2687 uint64_t* found_lma) const;
2688
61ba1cf9
ILT
2689 // Write the section headers in the list into V.
2690 template<int size, bool big_endian>
2691 unsigned char*
16649710
ILT
2692 write_section_headers_list(const Layout*, const Stringpool*,
2693 const Output_data_list*, unsigned char* v,
7d1a9ebb 2694 unsigned int* pshdx) const;
61ba1cf9 2695
75f65a3e 2696 // The list of output data with contents attached to this segment.
54dc6425 2697 Output_data_list output_data_;
75f65a3e
ILT
2698 // The list of output data without contents attached to this segment.
2699 Output_data_list output_bss_;
a2fb1b05
ILT
2700 // The segment virtual address.
2701 uint64_t vaddr_;
2702 // The segment physical address.
2703 uint64_t paddr_;
2704 // The size of the segment in memory.
2705 uint64_t memsz_;
a445fddf
ILT
2706 // The maximum section alignment. The is_max_align_known_ field
2707 // indicates whether this has been finalized.
2708 uint64_t max_align_;
2709 // The required minimum value for the p_align field. This is used
2710 // for PT_LOAD segments. Note that this does not mean that
2711 // addresses should be aligned to this value; it means the p_paddr
2712 // and p_vaddr fields must be congruent modulo this value. For
2713 // non-PT_LOAD segments, the dynamic linker works more efficiently
2714 // if the p_align field has the more conventional value, although it
2715 // can align as needed.
2716 uint64_t min_p_align_;
a2fb1b05
ILT
2717 // The offset of the segment data within the file.
2718 off_t offset_;
2719 // The size of the segment data in the file.
2720 off_t filesz_;
2721 // The segment type;
2722 elfcpp::Elf_Word type_;
2723 // The segment flags.
2724 elfcpp::Elf_Word flags_;
a445fddf
ILT
2725 // Whether we have finalized max_align_.
2726 bool is_max_align_known_ : 1;
2727 // Whether vaddr and paddr were set by a linker script.
2728 bool are_addresses_set_ : 1;
a2fb1b05
ILT
2729};
2730
61ba1cf9 2731// This class represents the output file.
a2fb1b05
ILT
2732
2733class Output_file
2734{
2735 public:
14144f39 2736 Output_file(const char* name);
61ba1cf9 2737
516cb3d0
ILT
2738 // Indicate that this is a temporary file which should not be
2739 // output.
2740 void
2741 set_is_temporary()
2742 { this->is_temporary_ = true; }
2743
61ba1cf9
ILT
2744 // Open the output file. FILE_SIZE is the final size of the file.
2745 void
2746 open(off_t file_size);
2747
27bc2bce
ILT
2748 // Resize the output file.
2749 void
2750 resize(off_t file_size);
2751
c420411f
ILT
2752 // Close the output file (flushing all buffered data) and make sure
2753 // there are no errors.
61ba1cf9
ILT
2754 void
2755 close();
2756
2757 // We currently always use mmap which makes the view handling quite
2758 // simple. In the future we may support other approaches.
a2fb1b05
ILT
2759
2760 // Write data to the output file.
2761 void
fe8718a4 2762 write(off_t offset, const void* data, size_t len)
61ba1cf9
ILT
2763 { memcpy(this->base_ + offset, data, len); }
2764
2765 // Get a buffer to use to write to the file, given the offset into
2766 // the file and the size.
2767 unsigned char*
fe8718a4 2768 get_output_view(off_t start, size_t size)
61ba1cf9 2769 {
8d32f935
ILT
2770 gold_assert(start >= 0
2771 && start + static_cast<off_t>(size) <= this->file_size_);
61ba1cf9
ILT
2772 return this->base_ + start;
2773 }
2774
2775 // VIEW must have been returned by get_output_view. Write the
2776 // buffer to the file, passing in the offset and the size.
2777 void
fe8718a4 2778 write_output_view(off_t, size_t, unsigned char*)
61ba1cf9
ILT
2779 { }
2780
730cdc88
ILT
2781 // Get a read/write buffer. This is used when we want to write part
2782 // of the file, read it in, and write it again.
2783 unsigned char*
fe8718a4 2784 get_input_output_view(off_t start, size_t size)
730cdc88
ILT
2785 { return this->get_output_view(start, size); }
2786
2787 // Write a read/write buffer back to the file.
2788 void
fe8718a4 2789 write_input_output_view(off_t, size_t, unsigned char*)
730cdc88
ILT
2790 { }
2791
2792 // Get a read buffer. This is used when we just want to read part
2793 // of the file back it in.
2794 const unsigned char*
fe8718a4 2795 get_input_view(off_t start, size_t size)
730cdc88
ILT
2796 { return this->get_output_view(start, size); }
2797
2798 // Release a read bfufer.
2799 void
fe8718a4 2800 free_input_view(off_t, size_t, const unsigned char*)
730cdc88
ILT
2801 { }
2802
61ba1cf9 2803 private:
c420411f 2804 // Map the file into memory and return a pointer to the map.
27bc2bce
ILT
2805 void
2806 map();
2807
c420411f
ILT
2808 // Unmap the file from memory (and flush to disk buffers).
2809 void
2810 unmap();
2811
61ba1cf9
ILT
2812 // File name.
2813 const char* name_;
2814 // File descriptor.
2815 int o_;
2816 // File size.
2817 off_t file_size_;
2818 // Base of file mapped into memory.
2819 unsigned char* base_;
c420411f
ILT
2820 // True iff base_ points to a memory buffer rather than an output file.
2821 bool map_is_anonymous_;
516cb3d0
ILT
2822 // True if this is a temporary file which should not be output.
2823 bool is_temporary_;
a2fb1b05
ILT
2824};
2825
2826} // End namespace gold.
2827
2828#endif // !defined(GOLD_OUTPUT_H)
This page took 0.269458 seconds and 4 git commands to generate.