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