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