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