sim: glue: allow bitwise devices to only glue ints
[deliverable/binutils-gdb.git] / gold / output.h
CommitLineData
a2fb1b05
ILT
1// output.h -- manage the output file for gold -*- C++ -*-
2
88597d34 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 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"
7d9e3d98 30#include "mapfile.h"
54dc6425 31#include "layout.h"
c06b7b0b 32#include "reloc-types.h"
a2fb1b05
ILT
33
34namespace gold
35{
36
61ba1cf9 37class General_options;
a2fb1b05 38class Object;
a3ad94ed 39class Symbol;
a2fb1b05 40class Output_file;
c0a62865 41class Output_merge_base;
c06b7b0b 42class Output_section;
6a74a719 43class Relocatable_relocs;
a3ad94ed 44class Target;
54dc6425
ILT
45template<int size, bool big_endian>
46class Sized_target;
c06b7b0b
ILT
47template<int size, bool big_endian>
48class Sized_relobj;
54dc6425
ILT
49
50// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
51
52class Output_data
53{
54 public:
27bc2bce
ILT
55 explicit Output_data()
56 : address_(0), data_size_(0), offset_(-1),
57 is_address_valid_(false), is_data_size_valid_(false),
20e6d0d6 58 is_offset_valid_(false), is_data_size_fixed_(false),
22f0da72 59 has_dynamic_reloc_(false)
a2fb1b05
ILT
60 { }
61
62 virtual
63 ~Output_data();
64
27bc2bce
ILT
65 // Return the address. For allocated sections, this is only valid
66 // after Layout::finalize is finished.
75f65a3e
ILT
67 uint64_t
68 address() const
27bc2bce
ILT
69 {
70 gold_assert(this->is_address_valid_);
71 return this->address_;
72 }
75f65a3e 73
27bc2bce
ILT
74 // Return the size of the data. For allocated sections, this must
75 // be valid after Layout::finalize calls set_address, but need not
76 // be valid before then.
a2fb1b05 77 off_t
75f65a3e 78 data_size() const
27bc2bce
ILT
79 {
80 gold_assert(this->is_data_size_valid_);
81 return this->data_size_;
82 }
75f65a3e 83
cdc29364
CC
84 // Get the current data size.
85 off_t
86 current_data_size() const
87 { return this->current_data_size_for_child(); }
88
20e6d0d6
DK
89 // Return true if data size is fixed.
90 bool
91 is_data_size_fixed() const
92 { return this->is_data_size_fixed_; }
93
ead1e424 94 // Return the file offset. This is only valid after
27bc2bce
ILT
95 // Layout::finalize is finished. For some non-allocated sections,
96 // it may not be valid until near the end of the link.
75f65a3e
ILT
97 off_t
98 offset() const
27bc2bce
ILT
99 {
100 gold_assert(this->is_offset_valid_);
101 return this->offset_;
102 }
75f65a3e 103
a445fddf
ILT
104 // Reset the address and file offset. This essentially disables the
105 // sanity testing about duplicate and unknown settings.
106 void
107 reset_address_and_file_offset()
108 {
109 this->is_address_valid_ = false;
110 this->is_offset_valid_ = false;
20e6d0d6
DK
111 if (!this->is_data_size_fixed_)
112 this->is_data_size_valid_ = false;
a445fddf
ILT
113 this->do_reset_address_and_file_offset();
114 }
115
20e6d0d6
DK
116 // Return true if address and file offset already have reset values. In
117 // other words, calling reset_address_and_file_offset will not change them.
118 bool
119 address_and_file_offset_have_reset_values() const
120 { return this->do_address_and_file_offset_have_reset_values(); }
121
75f65a3e
ILT
122 // Return the required alignment.
123 uint64_t
124 addralign() const
125 { return this->do_addralign(); }
126
a445fddf
ILT
127 // Return whether this has a load address.
128 bool
129 has_load_address() const
130 { return this->do_has_load_address(); }
131
132 // Return the load address.
133 uint64_t
134 load_address() const
135 { return this->do_load_address(); }
136
75f65a3e
ILT
137 // Return whether this is an Output_section.
138 bool
139 is_section() const
140 { return this->do_is_section(); }
141
142 // Return whether this is an Output_section of the specified type.
143 bool
144 is_section_type(elfcpp::Elf_Word stt) const
145 { return this->do_is_section_type(stt); }
146
147 // Return whether this is an Output_section with the specified flag
148 // set.
149 bool
150 is_section_flag_set(elfcpp::Elf_Xword shf) const
151 { return this->do_is_section_flag_set(shf); }
152
77e65537
ILT
153 // Return the output section that this goes in, if there is one.
154 Output_section*
155 output_section()
156 { return this->do_output_section(); }
157
ea715a34
ILT
158 const Output_section*
159 output_section() const
160 { return this->do_output_section(); }
161
ead1e424
ILT
162 // Return the output section index, if there is an output section.
163 unsigned int
164 out_shndx() const
165 { return this->do_out_shndx(); }
166
167 // Set the output section index, if this is an output section.
168 void
169 set_out_shndx(unsigned int shndx)
170 { this->do_set_out_shndx(shndx); }
171
27bc2bce
ILT
172 // Set the address and file offset of this data, and finalize the
173 // size of the data. This is called during Layout::finalize for
174 // allocated sections.
75f65a3e 175 void
27bc2bce
ILT
176 set_address_and_file_offset(uint64_t addr, off_t off)
177 {
178 this->set_address(addr);
179 this->set_file_offset(off);
180 this->finalize_data_size();
181 }
182
183 // Set the address.
184 void
185 set_address(uint64_t addr)
186 {
187 gold_assert(!this->is_address_valid_);
188 this->address_ = addr;
189 this->is_address_valid_ = true;
190 }
191
192 // Set the file offset.
193 void
194 set_file_offset(off_t off)
195 {
196 gold_assert(!this->is_offset_valid_);
197 this->offset_ = off;
198 this->is_offset_valid_ = true;
199 }
200
cdc29364
CC
201 // Update the data size without finalizing it.
202 void
203 pre_finalize_data_size()
204 {
205 if (!this->is_data_size_valid_)
206 {
207 // Tell the child class to update the data size.
208 this->update_data_size();
209 }
210 }
211
27bc2bce
ILT
212 // Finalize the data size.
213 void
214 finalize_data_size()
215 {
216 if (!this->is_data_size_valid_)
217 {
218 // Tell the child class to set the data size.
219 this->set_final_data_size();
220 gold_assert(this->is_data_size_valid_);
221 }
222 }
75f65a3e 223
7bf1f802
ILT
224 // Set the TLS offset. Called only for SHT_TLS sections.
225 void
226 set_tls_offset(uint64_t tls_base)
227 { this->do_set_tls_offset(tls_base); }
228
229 // Return the TLS offset, relative to the base of the TLS segment.
230 // Valid only for SHT_TLS sections.
231 uint64_t
232 tls_offset() const
233 { return this->do_tls_offset(); }
234
ead1e424
ILT
235 // Write the data to the output file. This is called after
236 // Layout::finalize is complete.
75f65a3e
ILT
237 void
238 write(Output_file* file)
239 { this->do_write(file); }
a2fb1b05 240
27bc2bce
ILT
241 // This is called by Layout::finalize to note that the sizes of
242 // allocated sections must now be fixed.
a3ad94ed
ILT
243 static void
244 layout_complete()
27bc2bce 245 { Output_data::allocated_sizes_are_fixed = true; }
a3ad94ed 246
730cdc88
ILT
247 // Used to check that layout has been done.
248 static bool
249 is_layout_complete()
27bc2bce 250 { return Output_data::allocated_sizes_are_fixed; }
730cdc88 251
22f0da72 252 // Note that a dynamic reloc has been applied to this data.
4f4c5f80
ILT
253 void
254 add_dynamic_reloc()
22f0da72 255 { this->has_dynamic_reloc_ = true; }
4f4c5f80 256
22f0da72
ILT
257 // Return whether a dynamic reloc has been applied.
258 bool
259 has_dynamic_reloc() const
260 { return this->has_dynamic_reloc_; }
4f4c5f80 261
a9a60db6
ILT
262 // Whether the address is valid.
263 bool
264 is_address_valid() const
265 { return this->is_address_valid_; }
266
267 // Whether the file offset is valid.
268 bool
269 is_offset_valid() const
270 { return this->is_offset_valid_; }
271
272 // Whether the data size is valid.
273 bool
274 is_data_size_valid() const
275 { return this->is_data_size_valid_; }
276
7d9e3d98
ILT
277 // Print information to the map file.
278 void
279 print_to_mapfile(Mapfile* mapfile) const
280 { return this->do_print_to_mapfile(mapfile); }
281
75f65a3e
ILT
282 protected:
283 // Functions that child classes may or in some cases must implement.
284
285 // Write the data to the output file.
a2fb1b05 286 virtual void
75f65a3e
ILT
287 do_write(Output_file*) = 0;
288
289 // Return the required alignment.
290 virtual uint64_t
291 do_addralign() const = 0;
292
a445fddf
ILT
293 // Return whether this has a load address.
294 virtual bool
295 do_has_load_address() const
296 { return false; }
297
298 // Return the load address.
299 virtual uint64_t
300 do_load_address() const
301 { gold_unreachable(); }
302
75f65a3e
ILT
303 // Return whether this is an Output_section.
304 virtual bool
305 do_is_section() const
306 { return false; }
a2fb1b05 307
54dc6425 308 // Return whether this is an Output_section of the specified type.
75f65a3e 309 // This only needs to be implement by Output_section.
54dc6425 310 virtual bool
75f65a3e 311 do_is_section_type(elfcpp::Elf_Word) const
54dc6425
ILT
312 { return false; }
313
75f65a3e
ILT
314 // Return whether this is an Output_section with the specific flag
315 // set. This only needs to be implemented by Output_section.
54dc6425 316 virtual bool
75f65a3e 317 do_is_section_flag_set(elfcpp::Elf_Xword) const
54dc6425
ILT
318 { return false; }
319
77e65537
ILT
320 // Return the output section, if there is one.
321 virtual Output_section*
322 do_output_section()
323 { return NULL; }
324
ea715a34
ILT
325 virtual const Output_section*
326 do_output_section() const
327 { return NULL; }
328
ead1e424
ILT
329 // Return the output section index, if there is an output section.
330 virtual unsigned int
331 do_out_shndx() const
a3ad94ed 332 { gold_unreachable(); }
ead1e424
ILT
333
334 // Set the output section index, if this is an output section.
335 virtual void
336 do_set_out_shndx(unsigned int)
a3ad94ed 337 { gold_unreachable(); }
ead1e424 338
cdc29364
CC
339 // This is a hook for derived classes to set the preliminary data size.
340 // This is called by pre_finalize_data_size, normally called during
341 // Layout::finalize, before the section address is set, and is used
342 // during an incremental update, when we need to know the size of a
343 // section before allocating space in the output file. For classes
344 // where the current data size is up to date, this default version of
345 // the method can be inherited.
346 virtual void
347 update_data_size()
348 { }
349
27bc2bce
ILT
350 // This is a hook for derived classes to set the data size. This is
351 // called by finalize_data_size, normally called during
352 // Layout::finalize, when the section address is set.
75f65a3e 353 virtual void
27bc2bce
ILT
354 set_final_data_size()
355 { gold_unreachable(); }
75f65a3e 356
a445fddf
ILT
357 // A hook for resetting the address and file offset.
358 virtual void
359 do_reset_address_and_file_offset()
360 { }
361
20e6d0d6
DK
362 // Return true if address and file offset already have reset values. In
363 // other words, calling reset_address_and_file_offset will not change them.
364 // A child class overriding do_reset_address_and_file_offset may need to
365 // also override this.
366 virtual bool
367 do_address_and_file_offset_have_reset_values() const
368 { return !this->is_address_valid_ && !this->is_offset_valid_; }
369
7bf1f802
ILT
370 // Set the TLS offset. Called only for SHT_TLS sections.
371 virtual void
372 do_set_tls_offset(uint64_t)
373 { gold_unreachable(); }
374
375 // Return the TLS offset, relative to the base of the TLS segment.
376 // Valid only for SHT_TLS sections.
377 virtual uint64_t
378 do_tls_offset() const
379 { gold_unreachable(); }
380
7d9e3d98
ILT
381 // Print to the map file. This only needs to be implemented by
382 // classes which may appear in a PT_LOAD segment.
383 virtual void
384 do_print_to_mapfile(Mapfile*) const
385 { gold_unreachable(); }
386
75f65a3e
ILT
387 // Functions that child classes may call.
388
9c547ec3
ILT
389 // Reset the address. The Output_section class needs this when an
390 // SHF_ALLOC input section is added to an output section which was
391 // formerly not SHF_ALLOC.
392 void
393 mark_address_invalid()
394 { this->is_address_valid_ = false; }
395
a2fb1b05
ILT
396 // Set the size of the data.
397 void
2ea97941 398 set_data_size(off_t data_size)
a3ad94ed 399 {
20e6d0d6
DK
400 gold_assert(!this->is_data_size_valid_
401 && !this->is_data_size_fixed_);
2ea97941 402 this->data_size_ = data_size;
27bc2bce
ILT
403 this->is_data_size_valid_ = true;
404 }
405
20e6d0d6
DK
406 // Fix the data size. Once it is fixed, it cannot be changed
407 // and the data size remains always valid.
408 void
409 fix_data_size()
410 {
411 gold_assert(this->is_data_size_valid_);
412 this->is_data_size_fixed_ = true;
413 }
414
27bc2bce
ILT
415 // Get the current data size--this is for the convenience of
416 // sections which build up their size over time.
417 off_t
418 current_data_size_for_child() const
419 { return this->data_size_; }
420
421 // Set the current data size--this is for the convenience of
422 // sections which build up their size over time.
423 void
2ea97941 424 set_current_data_size_for_child(off_t data_size)
27bc2bce
ILT
425 {
426 gold_assert(!this->is_data_size_valid_);
2ea97941 427 this->data_size_ = data_size;
a3ad94ed 428 }
75f65a3e 429
730cdc88
ILT
430 // Return default alignment for the target size.
431 static uint64_t
432 default_alignment();
433
434 // Return default alignment for a specified size--32 or 64.
75f65a3e 435 static uint64_t
730cdc88 436 default_alignment_for_size(int size);
a2fb1b05
ILT
437
438 private:
439 Output_data(const Output_data&);
440 Output_data& operator=(const Output_data&);
441
a3ad94ed 442 // This is used for verification, to make sure that we don't try to
27bc2bce
ILT
443 // change any sizes of allocated sections after we set the section
444 // addresses.
445 static bool allocated_sizes_are_fixed;
a3ad94ed 446
27bc2bce 447 // Memory address in output file.
75f65a3e 448 uint64_t address_;
27bc2bce 449 // Size of data in output file.
75f65a3e 450 off_t data_size_;
27bc2bce 451 // File offset of contents in output file.
75f65a3e 452 off_t offset_;
27bc2bce 453 // Whether address_ is valid.
22f0da72 454 bool is_address_valid_ : 1;
27bc2bce 455 // Whether data_size_ is valid.
22f0da72 456 bool is_data_size_valid_ : 1;
27bc2bce 457 // Whether offset_ is valid.
22f0da72 458 bool is_offset_valid_ : 1;
20e6d0d6 459 // Whether data size is fixed.
22f0da72
ILT
460 bool is_data_size_fixed_ : 1;
461 // Whether any dynamic relocs have been applied to this section.
462 bool has_dynamic_reloc_ : 1;
a2fb1b05
ILT
463};
464
54dc6425
ILT
465// Output the section headers.
466
467class Output_section_headers : public Output_data
468{
469 public:
9025d29d 470 Output_section_headers(const Layout*,
16649710
ILT
471 const Layout::Segment_list*,
472 const Layout::Section_list*,
6a74a719 473 const Layout::Section_list*,
d491d34e
ILT
474 const Stringpool*,
475 const Output_section*);
54dc6425 476
27bc2bce 477 protected:
54dc6425
ILT
478 // Write the data to the file.
479 void
75f65a3e
ILT
480 do_write(Output_file*);
481
482 // Return the required alignment.
483 uint64_t
484 do_addralign() const
730cdc88 485 { return Output_data::default_alignment(); }
54dc6425 486
7d9e3d98
ILT
487 // Write to a map file.
488 void
489 do_print_to_mapfile(Mapfile* mapfile) const
490 { mapfile->print_output_data(this, _("** section headers")); }
491
cdc29364
CC
492 // Update the data size.
493 void
494 update_data_size()
495 { this->set_data_size(this->do_size()); }
496
20e6d0d6
DK
497 // Set final data size.
498 void
499 set_final_data_size()
500 { this->set_data_size(this->do_size()); }
501
54dc6425 502 private:
61ba1cf9
ILT
503 // Write the data to the file with the right size and endianness.
504 template<int size, bool big_endian>
505 void
506 do_sized_write(Output_file*);
507
20e6d0d6
DK
508 // Compute data size.
509 off_t
510 do_size() const;
511
16649710
ILT
512 const Layout* layout_;
513 const Layout::Segment_list* segment_list_;
6a74a719 514 const Layout::Section_list* section_list_;
16649710 515 const Layout::Section_list* unattached_section_list_;
61ba1cf9 516 const Stringpool* secnamepool_;
d491d34e 517 const Output_section* shstrtab_section_;
54dc6425
ILT
518};
519
520// Output the segment headers.
521
522class Output_segment_headers : public Output_data
523{
524 public:
9025d29d 525 Output_segment_headers(const Layout::Segment_list& segment_list);
54dc6425 526
27bc2bce 527 protected:
54dc6425
ILT
528 // Write the data to the file.
529 void
75f65a3e
ILT
530 do_write(Output_file*);
531
532 // Return the required alignment.
533 uint64_t
534 do_addralign() const
730cdc88 535 { return Output_data::default_alignment(); }
54dc6425 536
7d9e3d98
ILT
537 // Write to a map file.
538 void
539 do_print_to_mapfile(Mapfile* mapfile) const
540 { mapfile->print_output_data(this, _("** segment headers")); }
541
20e6d0d6
DK
542 // Set final data size.
543 void
544 set_final_data_size()
545 { this->set_data_size(this->do_size()); }
546
54dc6425 547 private:
61ba1cf9
ILT
548 // Write the data to the file with the right size and endianness.
549 template<int size, bool big_endian>
550 void
551 do_sized_write(Output_file*);
552
20e6d0d6
DK
553 // Compute the current size.
554 off_t
555 do_size() const;
556
54dc6425
ILT
557 const Layout::Segment_list& segment_list_;
558};
559
560// Output the ELF file header.
561
562class Output_file_header : public Output_data
563{
564 public:
9025d29d 565 Output_file_header(const Target*,
54dc6425 566 const Symbol_table*,
d391083d
ILT
567 const Output_segment_headers*,
568 const char* entry);
75f65a3e
ILT
569
570 // Add information about the section headers. We lay out the ELF
571 // file header before we create the section headers.
572 void set_section_info(const Output_section_headers*,
573 const Output_section* shstrtab);
54dc6425 574
27bc2bce 575 protected:
54dc6425
ILT
576 // Write the data to the file.
577 void
75f65a3e
ILT
578 do_write(Output_file*);
579
580 // Return the required alignment.
581 uint64_t
582 do_addralign() const
730cdc88 583 { return Output_data::default_alignment(); }
75f65a3e 584
7d9e3d98
ILT
585 // Write to a map file.
586 void
587 do_print_to_mapfile(Mapfile* mapfile) const
588 { mapfile->print_output_data(this, _("** file header")); }
589
20e6d0d6
DK
590 // Set final data size.
591 void
592 set_final_data_size(void)
593 { this->set_data_size(this->do_size()); }
594
54dc6425 595 private:
61ba1cf9
ILT
596 // Write the data to the file with the right size and endianness.
597 template<int size, bool big_endian>
598 void
599 do_sized_write(Output_file*);
600
d391083d
ILT
601 // Return the value to use for the entry address.
602 template<int size>
603 typename elfcpp::Elf_types<size>::Elf_Addr
604 entry();
605
20e6d0d6
DK
606 // Compute the current data size.
607 off_t
608 do_size() const;
609
54dc6425
ILT
610 const Target* target_;
611 const Symbol_table* symtab_;
61ba1cf9 612 const Output_segment_headers* segment_header_;
54dc6425
ILT
613 const Output_section_headers* section_header_;
614 const Output_section* shstrtab_;
d391083d 615 const char* entry_;
54dc6425
ILT
616};
617
ead1e424
ILT
618// Output sections are mainly comprised of input sections. However,
619// there are cases where we have data to write out which is not in an
620// input section. Output_section_data is used in such cases. This is
621// an abstract base class.
622
623class Output_section_data : public Output_data
624{
625 public:
2ea97941
ILT
626 Output_section_data(off_t data_size, uint64_t addralign,
627 bool is_data_size_fixed)
628 : Output_data(), output_section_(NULL), addralign_(addralign)
20e6d0d6 629 {
2ea97941
ILT
630 this->set_data_size(data_size);
631 if (is_data_size_fixed)
20e6d0d6
DK
632 this->fix_data_size();
633 }
ead1e424 634
2ea97941
ILT
635 Output_section_data(uint64_t addralign)
636 : Output_data(), output_section_(NULL), addralign_(addralign)
ead1e424
ILT
637 { }
638
16649710 639 // Return the output section.
7223e9ca
ILT
640 Output_section*
641 output_section()
642 { return this->output_section_; }
643
16649710
ILT
644 const Output_section*
645 output_section() const
646 { return this->output_section_; }
647
ead1e424
ILT
648 // Record the output section.
649 void
16649710 650 set_output_section(Output_section* os);
ead1e424 651
b8e6aad9
ILT
652 // Add an input section, for SHF_MERGE sections. This returns true
653 // if the section was handled.
654 bool
655 add_input_section(Relobj* object, unsigned int shndx)
656 { return this->do_add_input_section(object, shndx); }
657
658 // Given an input OBJECT, an input section index SHNDX within that
659 // object, and an OFFSET relative to the start of that input
730cdc88
ILT
660 // section, return whether or not the corresponding offset within
661 // the output section is known. If this function returns true, it
662 // sets *POUTPUT to the output offset. The value -1 indicates that
663 // this input offset is being discarded.
8f00aeb8 664 bool
8383303e 665 output_offset(const Relobj* object, unsigned int shndx,
2ea97941 666 section_offset_type offset,
ca09d69a 667 section_offset_type* poutput) const
2ea97941 668 { return this->do_output_offset(object, shndx, offset, poutput); }
b8e6aad9 669
a9a60db6
ILT
670 // Return whether this is the merge section for the input section
671 // SHNDX in OBJECT. This should return true when output_offset
672 // would return true for some values of OFFSET.
673 bool
674 is_merge_section_for(const Relobj* object, unsigned int shndx) const
675 { return this->do_is_merge_section_for(object, shndx); }
676
96803768
ILT
677 // Write the contents to a buffer. This is used for sections which
678 // require postprocessing, such as compression.
679 void
680 write_to_buffer(unsigned char* buffer)
681 { this->do_write_to_buffer(buffer); }
682
38c5e8b4
ILT
683 // Print merge stats to stderr. This should only be called for
684 // SHF_MERGE sections.
685 void
686 print_merge_stats(const char* section_name)
687 { this->do_print_merge_stats(section_name); }
688
ead1e424
ILT
689 protected:
690 // The child class must implement do_write.
691
16649710
ILT
692 // The child class may implement specific adjustments to the output
693 // section.
694 virtual void
695 do_adjust_output_section(Output_section*)
696 { }
697
b8e6aad9
ILT
698 // May be implemented by child class. Return true if the section
699 // was handled.
700 virtual bool
701 do_add_input_section(Relobj*, unsigned int)
702 { gold_unreachable(); }
703
730cdc88 704 // The child class may implement output_offset.
b8e6aad9 705 virtual bool
8383303e
ILT
706 do_output_offset(const Relobj*, unsigned int, section_offset_type,
707 section_offset_type*) const
b8e6aad9
ILT
708 { return false; }
709
a9a60db6
ILT
710 // The child class may implement is_merge_section_for.
711 virtual bool
712 do_is_merge_section_for(const Relobj*, unsigned int) const
713 { return false; }
714
96803768
ILT
715 // The child class may implement write_to_buffer. Most child
716 // classes can not appear in a compressed section, and they do not
717 // implement this.
718 virtual void
719 do_write_to_buffer(unsigned char*)
720 { gold_unreachable(); }
721
38c5e8b4
ILT
722 // Print merge statistics.
723 virtual void
724 do_print_merge_stats(const char*)
725 { gold_unreachable(); }
726
ead1e424
ILT
727 // Return the required alignment.
728 uint64_t
729 do_addralign() const
730 { return this->addralign_; }
731
77e65537
ILT
732 // Return the output section.
733 Output_section*
734 do_output_section()
735 { return this->output_section_; }
736
ea715a34
ILT
737 const Output_section*
738 do_output_section() const
739 { return this->output_section_; }
740
ead1e424
ILT
741 // Return the section index of the output section.
742 unsigned int
743 do_out_shndx() const;
744
5a6f7e2d
ILT
745 // Set the alignment.
746 void
759b1a24 747 set_addralign(uint64_t addralign);
5a6f7e2d 748
ead1e424
ILT
749 private:
750 // The output section for this section.
77e65537 751 Output_section* output_section_;
ead1e424
ILT
752 // The required alignment.
753 uint64_t addralign_;
754};
755
27bc2bce
ILT
756// Some Output_section_data classes build up their data step by step,
757// rather than all at once. This class provides an interface for
758// them.
759
760class Output_section_data_build : public Output_section_data
761{
762 public:
2ea97941
ILT
763 Output_section_data_build(uint64_t addralign)
764 : Output_section_data(addralign)
27bc2bce
ILT
765 { }
766
27bc2bce
ILT
767 // Set the current data size.
768 void
2ea97941
ILT
769 set_current_data_size(off_t data_size)
770 { this->set_current_data_size_for_child(data_size); }
27bc2bce
ILT
771
772 protected:
773 // Set the final data size.
774 virtual void
775 set_final_data_size()
776 { this->set_data_size(this->current_data_size_for_child()); }
777};
778
dbe717ef
ILT
779// A simple case of Output_data in which we have constant data to
780// output.
ead1e424 781
dbe717ef 782class Output_data_const : public Output_section_data
ead1e424
ILT
783{
784 public:
2ea97941
ILT
785 Output_data_const(const std::string& data, uint64_t addralign)
786 : Output_section_data(data.size(), addralign, true), data_(data)
dbe717ef
ILT
787 { }
788
2ea97941
ILT
789 Output_data_const(const char* p, off_t len, uint64_t addralign)
790 : Output_section_data(len, addralign, true), data_(p, len)
dbe717ef
ILT
791 { }
792
2ea97941
ILT
793 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
794 : Output_section_data(len, addralign, true),
dbe717ef
ILT
795 data_(reinterpret_cast<const char*>(p), len)
796 { }
797
27bc2bce 798 protected:
a3ad94ed 799 // Write the data to the output file.
dbe717ef 800 void
a3ad94ed 801 do_write(Output_file*);
dbe717ef 802
96803768
ILT
803 // Write the data to a buffer.
804 void
805 do_write_to_buffer(unsigned char* buffer)
806 { memcpy(buffer, this->data_.data(), this->data_.size()); }
807
7d9e3d98
ILT
808 // Write to a map file.
809 void
810 do_print_to_mapfile(Mapfile* mapfile) const
811 { mapfile->print_output_data(this, _("** fill")); }
812
dbe717ef
ILT
813 private:
814 std::string data_;
815};
816
a3ad94ed
ILT
817// Another version of Output_data with constant data, in which the
818// buffer is allocated by the caller.
dbe717ef 819
a3ad94ed 820class Output_data_const_buffer : public Output_section_data
dbe717ef
ILT
821{
822 public:
a3ad94ed 823 Output_data_const_buffer(const unsigned char* p, off_t len,
2ea97941
ILT
824 uint64_t addralign, const char* map_name)
825 : Output_section_data(len, addralign, true),
7d9e3d98 826 p_(p), map_name_(map_name)
a3ad94ed
ILT
827 { }
828
27bc2bce 829 protected:
a3ad94ed
ILT
830 // Write the data the output file.
831 void
832 do_write(Output_file*);
833
96803768
ILT
834 // Write the data to a buffer.
835 void
836 do_write_to_buffer(unsigned char* buffer)
837 { memcpy(buffer, this->p_, this->data_size()); }
838
7d9e3d98
ILT
839 // Write to a map file.
840 void
841 do_print_to_mapfile(Mapfile* mapfile) const
842 { mapfile->print_output_data(this, _(this->map_name_)); }
843
a3ad94ed 844 private:
7d9e3d98 845 // The data to output.
a3ad94ed 846 const unsigned char* p_;
7d9e3d98
ILT
847 // Name to use in a map file. Maps are a rarely used feature, but
848 // the space usage is minor as aren't very many of these objects.
849 const char* map_name_;
a3ad94ed
ILT
850};
851
27bc2bce
ILT
852// A place holder for a fixed amount of data written out via some
853// other mechanism.
a3ad94ed 854
27bc2bce 855class Output_data_fixed_space : public Output_section_data
a3ad94ed
ILT
856{
857 public:
2ea97941 858 Output_data_fixed_space(off_t data_size, uint64_t addralign,
7d9e3d98 859 const char* map_name)
2ea97941 860 : Output_section_data(data_size, addralign, true),
7d9e3d98 861 map_name_(map_name)
a3ad94ed
ILT
862 { }
863
27bc2bce
ILT
864 protected:
865 // Write out the data--the actual data must be written out
866 // elsewhere.
867 void
868 do_write(Output_file*)
ead1e424 869 { }
7d9e3d98
ILT
870
871 // Write to a map file.
872 void
873 do_print_to_mapfile(Mapfile* mapfile) const
874 { mapfile->print_output_data(this, _(this->map_name_)); }
875
876 private:
877 // Name to use in a map file. Maps are a rarely used feature, but
878 // the space usage is minor as aren't very many of these objects.
879 const char* map_name_;
27bc2bce 880};
ead1e424 881
27bc2bce
ILT
882// A place holder for variable sized data written out via some other
883// mechanism.
884
885class Output_data_space : public Output_section_data_build
886{
887 public:
2ea97941
ILT
888 explicit Output_data_space(uint64_t addralign, const char* map_name)
889 : Output_section_data_build(addralign),
7d9e3d98 890 map_name_(map_name)
27bc2bce 891 { }
ead1e424 892
5a6f7e2d
ILT
893 // Set the alignment.
894 void
895 set_space_alignment(uint64_t align)
896 { this->set_addralign(align); }
897
27bc2bce
ILT
898 protected:
899 // Write out the data--the actual data must be written out
900 // elsewhere.
ead1e424
ILT
901 void
902 do_write(Output_file*)
903 { }
7d9e3d98
ILT
904
905 // Write to a map file.
906 void
907 do_print_to_mapfile(Mapfile* mapfile) const
908 { mapfile->print_output_data(this, _(this->map_name_)); }
909
910 private:
911 // Name to use in a map file. Maps are a rarely used feature, but
912 // the space usage is minor as aren't very many of these objects.
913 const char* map_name_;
914};
915
916// Fill fixed space with zeroes. This is just like
917// Output_data_fixed_space, except that the map name is known.
918
919class Output_data_zero_fill : public Output_section_data
920{
921 public:
2ea97941
ILT
922 Output_data_zero_fill(off_t data_size, uint64_t addralign)
923 : Output_section_data(data_size, addralign, true)
7d9e3d98
ILT
924 { }
925
926 protected:
927 // There is no data to write out.
928 void
929 do_write(Output_file*)
930 { }
931
932 // Write to a map file.
933 void
934 do_print_to_mapfile(Mapfile* mapfile) const
935 { mapfile->print_output_data(this, "** zero fill"); }
ead1e424
ILT
936};
937
a3ad94ed
ILT
938// A string table which goes into an output section.
939
940class Output_data_strtab : public Output_section_data
941{
942 public:
943 Output_data_strtab(Stringpool* strtab)
944 : Output_section_data(1), strtab_(strtab)
945 { }
946
27bc2bce 947 protected:
cdc29364
CC
948 // This is called to update the section size prior to assigning
949 // the address and file offset.
950 void
951 update_data_size()
952 { this->set_final_data_size(); }
953
a3ad94ed
ILT
954 // This is called to set the address and file offset. Here we make
955 // sure that the Stringpool is finalized.
956 void
27bc2bce 957 set_final_data_size();
a3ad94ed
ILT
958
959 // Write out the data.
960 void
961 do_write(Output_file*);
962
96803768
ILT
963 // Write the data to a buffer.
964 void
965 do_write_to_buffer(unsigned char* buffer)
966 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
967
7d9e3d98
ILT
968 // Write to a map file.
969 void
970 do_print_to_mapfile(Mapfile* mapfile) const
971 { mapfile->print_output_data(this, _("** string table")); }
972
a3ad94ed
ILT
973 private:
974 Stringpool* strtab_;
975};
976
c06b7b0b
ILT
977// This POD class is used to represent a single reloc in the output
978// file. This could be a private class within Output_data_reloc, but
979// the templatization is complex enough that I broke it out into a
980// separate class. The class is templatized on either elfcpp::SHT_REL
981// or elfcpp::SHT_RELA, and also on whether this is a dynamic
982// relocation or an ordinary relocation.
983
dceae3c1
ILT
984// A relocation can be against a global symbol, a local symbol, a
985// local section symbol, an output section, or the undefined symbol at
986// index 0. We represent the latter by using a NULL global symbol.
c06b7b0b
ILT
987
988template<int sh_type, bool dynamic, int size, bool big_endian>
989class Output_reloc;
990
991template<bool dynamic, int size, bool big_endian>
992class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
993{
994 public:
995 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
624f8810 996 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
c06b7b0b 997
eff45813
CC
998 static const Address invalid_address = static_cast<Address>(0) - 1;
999
c06b7b0b
ILT
1000 // An uninitialized entry. We need this because we want to put
1001 // instances of this class into an STL container.
1002 Output_reloc()
1003 : local_sym_index_(INVALID_CODE)
1004 { }
1005
dceae3c1
ILT
1006 // We have a bunch of different constructors. They come in pairs
1007 // depending on how the address of the relocation is specified. It
1008 // can either be an offset in an Output_data or an offset in an
1009 // input section.
1010
c06b7b0b 1011 // A reloc against a global symbol.
5a6f7e2d 1012
a3ad94ed 1013 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
0da6fa6c 1014 Address address, bool is_relative, bool is_symbolless);
5a6f7e2d 1015
ef9beddf
ILT
1016 Output_reloc(Symbol* gsym, unsigned int type,
1017 Sized_relobj<size, big_endian>* relobj,
0da6fa6c
DM
1018 unsigned int shndx, Address address, bool is_relative,
1019 bool is_symbolless);
c06b7b0b 1020
dceae3c1 1021 // A reloc against a local symbol or local section symbol.
5a6f7e2d
ILT
1022
1023 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 1024 unsigned int local_sym_index, unsigned int type,
dceae3c1 1025 Output_data* od, Address address, bool is_relative,
0da6fa6c 1026 bool is_symbolless, bool is_section_symbol);
5a6f7e2d
ILT
1027
1028 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 1029 unsigned int local_sym_index, unsigned int type,
dceae3c1 1030 unsigned int shndx, Address address, bool is_relative,
0da6fa6c 1031 bool is_symbolless, bool is_section_symbol);
c06b7b0b
ILT
1032
1033 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1034
a3ad94ed 1035 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
7bf1f802 1036 Address address);
5a6f7e2d 1037
ef9beddf
ILT
1038 Output_reloc(Output_section* os, unsigned int type,
1039 Sized_relobj<size, big_endian>* relobj,
7bf1f802 1040 unsigned int shndx, Address address);
c06b7b0b 1041
e291e7b9
ILT
1042 // An absolute relocation with no symbol.
1043
1044 Output_reloc(unsigned int type, Output_data* od, Address address);
1045
1046 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1047 unsigned int shndx, Address address);
1048
1049 // A target specific relocation. The target will be called to get
1050 // the symbol index, passing ARG. The type and offset will be set
1051 // as for other relocation types.
1052
1053 Output_reloc(unsigned int type, void* arg, Output_data* od,
1054 Address address);
1055
1056 Output_reloc(unsigned int type, void* arg,
1057 Sized_relobj<size, big_endian>* relobj,
1058 unsigned int shndx, Address address);
1059
1060 // Return the reloc type.
1061 unsigned int
1062 type() const
1063 { return this->type_; }
1064
1065 // Return whether this is a RELATIVE relocation.
e8c846c3
ILT
1066 bool
1067 is_relative() const
1068 { return this->is_relative_; }
1069
0da6fa6c
DM
1070 // Return whether this is a relocation which should not use
1071 // a symbol, but which obtains its addend from a symbol.
1072 bool
1073 is_symbolless() const
1074 { return this->is_symbolless_; }
1075
dceae3c1
ILT
1076 // Return whether this is against a local section symbol.
1077 bool
1078 is_local_section_symbol() const
1079 {
1080 return (this->local_sym_index_ != GSYM_CODE
1081 && this->local_sym_index_ != SECTION_CODE
1082 && this->local_sym_index_ != INVALID_CODE
e291e7b9 1083 && this->local_sym_index_ != TARGET_CODE
dceae3c1
ILT
1084 && this->is_section_symbol_);
1085 }
1086
e291e7b9
ILT
1087 // Return whether this is a target specific relocation.
1088 bool
1089 is_target_specific() const
1090 { return this->local_sym_index_ == TARGET_CODE; }
1091
1092 // Return the argument to pass to the target for a target specific
1093 // relocation.
1094 void*
1095 target_arg() const
1096 {
1097 gold_assert(this->local_sym_index_ == TARGET_CODE);
1098 return this->u1_.arg;
1099 }
1100
dceae3c1 1101 // For a local section symbol, return the offset of the input
624f8810
ILT
1102 // section within the output section. ADDEND is the addend being
1103 // applied to the input section.
ef9beddf 1104 Address
624f8810 1105 local_section_offset(Addend addend) const;
dceae3c1 1106
d1f003c6
ILT
1107 // Get the value of the symbol referred to by a Rel relocation when
1108 // we are adding the given ADDEND.
e8c846c3 1109 Address
624f8810 1110 symbol_value(Addend addend) const;
e8c846c3 1111
c06b7b0b
ILT
1112 // Write the reloc entry to an output view.
1113 void
1114 write(unsigned char* pov) const;
1115
1116 // Write the offset and info fields to Write_rel.
1117 template<typename Write_rel>
1118 void write_rel(Write_rel*) const;
1119
d98bc257
ILT
1120 // This is used when sorting dynamic relocs. Return -1 to sort this
1121 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1122 int
1123 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1124 const;
1125
1126 // Return whether this reloc should be sorted before the argument
1127 // when sorting dynamic relocs.
1128 bool
1129 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1130 r2) const
1131 { return this->compare(r2) < 0; }
1132
c06b7b0b 1133 private:
dceae3c1
ILT
1134 // Record that we need a dynamic symbol index.
1135 void
1136 set_needs_dynsym_index();
1137
1138 // Return the symbol index.
c06b7b0b
ILT
1139 unsigned int
1140 get_symbol_index() const;
1141
d98bc257 1142 // Return the output address.
a984ee1d 1143 Address
d98bc257
ILT
1144 get_address() const;
1145
c06b7b0b
ILT
1146 // Codes for local_sym_index_.
1147 enum
1148 {
1149 // Global symbol.
1150 GSYM_CODE = -1U,
1151 // Output section.
1152 SECTION_CODE = -2U,
e291e7b9
ILT
1153 // Target specific.
1154 TARGET_CODE = -3U,
c06b7b0b 1155 // Invalid uninitialized entry.
e291e7b9 1156 INVALID_CODE = -4U
c06b7b0b
ILT
1157 };
1158
1159 union
1160 {
dceae3c1
ILT
1161 // For a local symbol or local section symbol
1162 // (this->local_sym_index_ >= 0), the object. We will never
1163 // generate a relocation against a local symbol in a dynamic
1164 // object; that doesn't make sense. And our callers will always
1165 // be templatized, so we use Sized_relobj here.
5a6f7e2d 1166 Sized_relobj<size, big_endian>* relobj;
dceae3c1
ILT
1167 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1168 // symbol. If this is NULL, it indicates a relocation against the
1169 // undefined 0 symbol.
c06b7b0b 1170 Symbol* gsym;
dceae3c1
ILT
1171 // For a relocation against an output section
1172 // (this->local_sym_index_ == SECTION_CODE), the output section.
c06b7b0b 1173 Output_section* os;
e291e7b9
ILT
1174 // For a target specific relocation, an argument to pass to the
1175 // target.
1176 void* arg;
5a6f7e2d
ILT
1177 } u1_;
1178 union
1179 {
dceae3c1
ILT
1180 // If this->shndx_ is not INVALID CODE, the object which holds the
1181 // input section being used to specify the reloc address.
ef9beddf 1182 Sized_relobj<size, big_endian>* relobj;
dceae3c1 1183 // If this->shndx_ is INVALID_CODE, the output data being used to
5a6f7e2d
ILT
1184 // specify the reloc address. This may be NULL if the reloc
1185 // address is absolute.
1186 Output_data* od;
1187 } u2_;
1188 // The address offset within the input section or the Output_data.
1189 Address address_;
dceae3c1 1190 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
e291e7b9
ILT
1191 // relocation against an output section, or TARGET_CODE for a target
1192 // specific relocation, or INVALID_CODE for an uninitialized value.
1193 // Otherwise, for a local symbol (this->is_section_symbol_ is
1194 // false), the local symbol index. For a local section symbol
1195 // (this->is_section_symbol_ is true), the section index in the
1196 // input file.
c06b7b0b 1197 unsigned int local_sym_index_;
a3ad94ed 1198 // The reloc type--a processor specific code.
0da6fa6c 1199 unsigned int type_ : 29;
e8c846c3
ILT
1200 // True if the relocation is a RELATIVE relocation.
1201 bool is_relative_ : 1;
0da6fa6c
DM
1202 // True if the relocation is one which should not use
1203 // a symbol, but which obtains its addend from a symbol.
1204 bool is_symbolless_ : 1;
dceae3c1
ILT
1205 // True if the relocation is against a section symbol.
1206 bool is_section_symbol_ : 1;
5a6f7e2d
ILT
1207 // If the reloc address is an input section in an object, the
1208 // section index. This is INVALID_CODE if the reloc address is
1209 // specified in some other way.
1210 unsigned int shndx_;
c06b7b0b
ILT
1211};
1212
1213// The SHT_RELA version of Output_reloc<>. This is just derived from
1214// the SHT_REL version of Output_reloc, but it adds an addend.
1215
1216template<bool dynamic, int size, bool big_endian>
1217class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1218{
1219 public:
1220 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1221 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1222
1223 // An uninitialized entry.
1224 Output_reloc()
1225 : rel_()
1226 { }
1227
1228 // A reloc against a global symbol.
5a6f7e2d 1229
a3ad94ed 1230 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
0da6fa6c
DM
1231 Address address, Addend addend, bool is_relative,
1232 bool is_symbolless)
1233 : rel_(gsym, type, od, address, is_relative, is_symbolless),
1234 addend_(addend)
c06b7b0b
ILT
1235 { }
1236
ef9beddf
ILT
1237 Output_reloc(Symbol* gsym, unsigned int type,
1238 Sized_relobj<size, big_endian>* relobj,
2ea97941 1239 unsigned int shndx, Address address, Addend addend,
0da6fa6c
DM
1240 bool is_relative, bool is_symbolless)
1241 : rel_(gsym, type, relobj, shndx, address, is_relative,
1242 is_symbolless), addend_(addend)
5a6f7e2d
ILT
1243 { }
1244
c06b7b0b 1245 // A reloc against a local symbol.
5a6f7e2d
ILT
1246
1247 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3 1248 unsigned int local_sym_index, unsigned int type,
2ea97941 1249 Output_data* od, Address address,
0da6fa6c
DM
1250 Addend addend, bool is_relative,
1251 bool is_symbolless, bool is_section_symbol)
2ea97941 1252 : rel_(relobj, local_sym_index, type, od, address, is_relative,
0da6fa6c 1253 is_symbolless, is_section_symbol),
e8c846c3 1254 addend_(addend)
5a6f7e2d
ILT
1255 { }
1256
1257 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3 1258 unsigned int local_sym_index, unsigned int type,
2ea97941 1259 unsigned int shndx, Address address,
0da6fa6c
DM
1260 Addend addend, bool is_relative,
1261 bool is_symbolless, bool is_section_symbol)
2ea97941 1262 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
0da6fa6c 1263 is_symbolless, is_section_symbol),
5a6f7e2d 1264 addend_(addend)
c06b7b0b
ILT
1265 { }
1266
1267 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1268
a3ad94ed 1269 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
2ea97941
ILT
1270 Address address, Addend addend)
1271 : rel_(os, type, od, address), addend_(addend)
c06b7b0b
ILT
1272 { }
1273
ef9beddf
ILT
1274 Output_reloc(Output_section* os, unsigned int type,
1275 Sized_relobj<size, big_endian>* relobj,
2ea97941
ILT
1276 unsigned int shndx, Address address, Addend addend)
1277 : rel_(os, type, relobj, shndx, address), addend_(addend)
5a6f7e2d
ILT
1278 { }
1279
e291e7b9
ILT
1280 // An absolute relocation with no symbol.
1281
1282 Output_reloc(unsigned int type, Output_data* od, Address address,
1283 Addend addend)
1284 : rel_(type, od, address), addend_(addend)
1285 { }
1286
1287 Output_reloc(unsigned int type, Sized_relobj<size, big_endian>* relobj,
1288 unsigned int shndx, Address address, Addend addend)
1289 : rel_(type, relobj, shndx, address), addend_(addend)
1290 { }
1291
1292 // A target specific relocation. The target will be called to get
1293 // the symbol index and the addend, passing ARG. The type and
1294 // offset will be set as for other relocation types.
1295
1296 Output_reloc(unsigned int type, void* arg, Output_data* od,
1297 Address address, Addend addend)
1298 : rel_(type, arg, od, address), addend_(addend)
1299 { }
1300
1301 Output_reloc(unsigned int type, void* arg,
1302 Sized_relobj<size, big_endian>* relobj,
1303 unsigned int shndx, Address address, Addend addend)
1304 : rel_(type, arg, relobj, shndx, address), addend_(addend)
1305 { }
1306
1307 // Return whether this is a RELATIVE relocation.
3a44184e
ILT
1308 bool
1309 is_relative() const
1310 { return this->rel_.is_relative(); }
1311
0da6fa6c
DM
1312 // Return whether this is a relocation which should not use
1313 // a symbol, but which obtains its addend from a symbol.
1314 bool
1315 is_symbolless() const
1316 { return this->rel_.is_symbolless(); }
1317
c06b7b0b
ILT
1318 // Write the reloc entry to an output view.
1319 void
1320 write(unsigned char* pov) const;
1321
d98bc257
ILT
1322 // Return whether this reloc should be sorted before the argument
1323 // when sorting dynamic relocs.
1324 bool
1325 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1326 r2) const
1327 {
1328 int i = this->rel_.compare(r2.rel_);
1329 if (i < 0)
d98bc257 1330 return true;
cc28ec61
ILT
1331 else if (i > 0)
1332 return false;
d98bc257
ILT
1333 else
1334 return this->addend_ < r2.addend_;
1335 }
1336
c06b7b0b
ILT
1337 private:
1338 // The basic reloc.
1339 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1340 // The addend.
1341 Addend addend_;
1342};
1343
3a44184e
ILT
1344// Output_data_reloc_generic is a non-template base class for
1345// Output_data_reloc_base. This gives the generic code a way to hold
1346// a pointer to a reloc section.
1347
1348class Output_data_reloc_generic : public Output_section_data_build
1349{
1350 public:
1351 Output_data_reloc_generic(int size, bool sort_relocs)
1352 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1353 relative_reloc_count_(0), sort_relocs_(sort_relocs)
1354 { }
1355
1356 // Return the number of relative relocs in this section.
1357 size_t
1358 relative_reloc_count() const
1359 { return this->relative_reloc_count_; }
1360
1361 // Whether we should sort the relocs.
1362 bool
1363 sort_relocs() const
1364 { return this->sort_relocs_; }
1365
1366 protected:
1367 // Note that we've added another relative reloc.
1368 void
1369 bump_relative_reloc_count()
1370 { ++this->relative_reloc_count_; }
1371
1372 private:
1373 // The number of relative relocs added to this section. This is to
1374 // support DT_RELCOUNT.
1375 size_t relative_reloc_count_;
1376 // Whether to sort the relocations when writing them out, to make
1377 // the dynamic linker more efficient.
1378 bool sort_relocs_;
1379};
1380
c06b7b0b
ILT
1381// Output_data_reloc is used to manage a section containing relocs.
1382// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1383// indicates whether this is a dynamic relocation or a normal
1384// relocation. Output_data_reloc_base is a base class.
1385// Output_data_reloc is the real class, which we specialize based on
1386// the reloc type.
1387
1388template<int sh_type, bool dynamic, int size, bool big_endian>
3a44184e 1389class Output_data_reloc_base : public Output_data_reloc_generic
c06b7b0b
ILT
1390{
1391 public:
1392 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1393 typedef typename Output_reloc_type::Address Address;
1394 static const int reloc_size =
1395 Reloc_types<sh_type, size, big_endian>::reloc_size;
1396
1397 // Construct the section.
d98bc257 1398 Output_data_reloc_base(bool sort_relocs)
3a44184e 1399 : Output_data_reloc_generic(size, sort_relocs)
c06b7b0b
ILT
1400 { }
1401
27bc2bce 1402 protected:
c06b7b0b
ILT
1403 // Write out the data.
1404 void
1405 do_write(Output_file*);
1406
16649710
ILT
1407 // Set the entry size and the link.
1408 void
ca09d69a 1409 do_adjust_output_section(Output_section* os);
16649710 1410
7d9e3d98
ILT
1411 // Write to a map file.
1412 void
1413 do_print_to_mapfile(Mapfile* mapfile) const
1414 {
1415 mapfile->print_output_data(this,
1416 (dynamic
1417 ? _("** dynamic relocs")
1418 : _("** relocs")));
1419 }
1420
c06b7b0b
ILT
1421 // Add a relocation entry.
1422 void
ca09d69a 1423 add(Output_data* od, const Output_reloc_type& reloc)
c06b7b0b
ILT
1424 {
1425 this->relocs_.push_back(reloc);
27bc2bce 1426 this->set_current_data_size(this->relocs_.size() * reloc_size);
4f4c5f80 1427 od->add_dynamic_reloc();
3a44184e
ILT
1428 if (reloc.is_relative())
1429 this->bump_relative_reloc_count();
c06b7b0b
ILT
1430 }
1431
1432 private:
1433 typedef std::vector<Output_reloc_type> Relocs;
1434
d98bc257
ILT
1435 // The class used to sort the relocations.
1436 struct Sort_relocs_comparison
1437 {
1438 bool
1439 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1440 { return r1.sort_before(r2); }
1441 };
1442
1443 // The relocations in this section.
c06b7b0b
ILT
1444 Relocs relocs_;
1445};
1446
1447// The class which callers actually create.
1448
1449template<int sh_type, bool dynamic, int size, bool big_endian>
1450class Output_data_reloc;
1451
1452// The SHT_REL version of Output_data_reloc.
1453
1454template<bool dynamic, int size, bool big_endian>
1455class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1456 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1457{
dceae3c1 1458 private:
c06b7b0b
ILT
1459 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1460 big_endian> Base;
1461
1462 public:
1463 typedef typename Base::Output_reloc_type Output_reloc_type;
1464 typedef typename Output_reloc_type::Address Address;
1465
d98bc257
ILT
1466 Output_data_reloc(bool sr)
1467 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
c06b7b0b
ILT
1468 { }
1469
1470 // Add a reloc against a global symbol.
5a6f7e2d 1471
c06b7b0b 1472 void
2ea97941 1473 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
0da6fa6c 1474 { this->add(od, Output_reloc_type(gsym, type, od, address, false, false)); }
c06b7b0b 1475
5a6f7e2d 1476 void
ef9beddf
ILT
1477 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1478 Sized_relobj<size, big_endian>* relobj,
2ea97941
ILT
1479 unsigned int shndx, Address address)
1480 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
0da6fa6c 1481 false, false)); }
e8c846c3 1482
12c0daef
ILT
1483 // These are to simplify the Copy_relocs class.
1484
1485 void
2ea97941 1486 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
12c0daef
ILT
1487 Address addend)
1488 {
1489 gold_assert(addend == 0);
2ea97941 1490 this->add_global(gsym, type, od, address);
12c0daef
ILT
1491 }
1492
1493 void
ef9beddf
ILT
1494 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1495 Sized_relobj<size, big_endian>* relobj,
2ea97941 1496 unsigned int shndx, Address address, Address addend)
12c0daef
ILT
1497 {
1498 gold_assert(addend == 0);
2ea97941 1499 this->add_global(gsym, type, od, relobj, shndx, address);
12c0daef
ILT
1500 }
1501
e8c846c3
ILT
1502 // Add a RELATIVE reloc against a global symbol. The final relocation
1503 // will not reference the symbol.
1504
1505 void
1506 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
2ea97941 1507 Address address)
0da6fa6c 1508 { this->add(od, Output_reloc_type(gsym, type, od, address, true, true)); }
e8c846c3
ILT
1509
1510 void
1511 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
ef9beddf 1512 Sized_relobj<size, big_endian>* relobj,
2ea97941 1513 unsigned int shndx, Address address)
dceae3c1 1514 {
2ea97941 1515 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
0da6fa6c
DM
1516 true, true));
1517 }
1518
1519 // Add a global relocation which does not use a symbol for the relocation,
1520 // but which gets its addend from a symbol.
1521
1522 void
1523 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1524 Output_data* od, Address address)
1525 { this->add(od, Output_reloc_type(gsym, type, od, address, false, true)); }
1526
1527 void
1528 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1529 Output_data* od,
1530 Sized_relobj<size, big_endian>* relobj,
1531 unsigned int shndx, Address address)
1532 {
1533 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1534 false, true));
dceae3c1 1535 }
5a6f7e2d 1536
c06b7b0b 1537 // Add a reloc against a local symbol.
5a6f7e2d 1538
c06b7b0b 1539 void
5a6f7e2d 1540 add_local(Sized_relobj<size, big_endian>* relobj,
a3ad94ed 1541 unsigned int local_sym_index, unsigned int type,
2ea97941 1542 Output_data* od, Address address)
dceae3c1
ILT
1543 {
1544 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
0da6fa6c 1545 address, false, false, false));
dceae3c1 1546 }
5a6f7e2d
ILT
1547
1548 void
1549 add_local(Sized_relobj<size, big_endian>* relobj,
1550 unsigned int local_sym_index, unsigned int type,
2ea97941 1551 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1552 {
1553 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
0da6fa6c 1554 address, false, false, false));
dceae3c1 1555 }
e8c846c3
ILT
1556
1557 // Add a RELATIVE reloc against a local symbol.
5a6f7e2d 1558
e8c846c3
ILT
1559 void
1560 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1561 unsigned int local_sym_index, unsigned int type,
2ea97941 1562 Output_data* od, Address address)
dceae3c1
ILT
1563 {
1564 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
0da6fa6c 1565 address, true, true, false));
dceae3c1 1566 }
e8c846c3
ILT
1567
1568 void
1569 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1570 unsigned int local_sym_index, unsigned int type,
2ea97941 1571 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1572 {
1573 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
0da6fa6c
DM
1574 address, true, true, false));
1575 }
1576
1577 // Add a local relocation which does not use a symbol for the relocation,
1578 // but which gets its addend from a symbol.
1579
1580 void
1581 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1582 unsigned int local_sym_index, unsigned int type,
1583 Output_data* od, Address address)
1584 {
1585 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1586 address, false, true, false));
1587 }
1588
1589 void
1590 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1591 unsigned int local_sym_index, unsigned int type,
1592 Output_data* od, unsigned int shndx,
1593 Address address)
1594 {
1595 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1596 address, false, true, false));
dceae3c1
ILT
1597 }
1598
1599 // Add a reloc against a local section symbol. This will be
1600 // converted into a reloc against the STT_SECTION symbol of the
1601 // output section.
1602
1603 void
1604 add_local_section(Sized_relobj<size, big_endian>* relobj,
1605 unsigned int input_shndx, unsigned int type,
2ea97941 1606 Output_data* od, Address address)
dceae3c1
ILT
1607 {
1608 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
0da6fa6c 1609 address, false, false, true));
dceae3c1
ILT
1610 }
1611
1612 void
1613 add_local_section(Sized_relobj<size, big_endian>* relobj,
1614 unsigned int input_shndx, unsigned int type,
2ea97941 1615 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1616 {
1617 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
0da6fa6c 1618 address, false, false, true));
dceae3c1 1619 }
c06b7b0b
ILT
1620
1621 // A reloc against the STT_SECTION symbol of an output section.
4f4c5f80
ILT
1622 // OS is the Output_section that the relocation refers to; OD is
1623 // the Output_data object being relocated.
5a6f7e2d 1624
c06b7b0b 1625 void
a3ad94ed 1626 add_output_section(Output_section* os, unsigned int type,
2ea97941
ILT
1627 Output_data* od, Address address)
1628 { this->add(od, Output_reloc_type(os, type, od, address)); }
5a6f7e2d
ILT
1629
1630 void
4f4c5f80 1631 add_output_section(Output_section* os, unsigned int type, Output_data* od,
ef9beddf 1632 Sized_relobj<size, big_endian>* relobj,
2ea97941
ILT
1633 unsigned int shndx, Address address)
1634 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
e291e7b9
ILT
1635
1636 // Add an absolute relocation.
1637
1638 void
1639 add_absolute(unsigned int type, Output_data* od, Address address)
1640 { this->add(od, Output_reloc_type(type, od, address)); }
1641
1642 void
1643 add_absolute(unsigned int type, Output_data* od,
1644 Sized_relobj<size, big_endian>* relobj,
1645 unsigned int shndx, Address address)
1646 { this->add(od, Output_reloc_type(type, relobj, shndx, address)); }
1647
1648 // Add a target specific relocation. A target which calls this must
1649 // define the reloc_symbol_index and reloc_addend virtual functions.
1650
1651 void
1652 add_target_specific(unsigned int type, void* arg, Output_data* od,
1653 Address address)
1654 { this->add(od, Output_reloc_type(type, arg, od, address)); }
1655
1656 void
1657 add_target_specific(unsigned int type, void* arg, Output_data* od,
1658 Sized_relobj<size, big_endian>* relobj,
1659 unsigned int shndx, Address address)
1660 { this->add(od, Output_reloc_type(type, arg, relobj, shndx, address)); }
c06b7b0b
ILT
1661};
1662
1663// The SHT_RELA version of Output_data_reloc.
1664
1665template<bool dynamic, int size, bool big_endian>
1666class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1667 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1668{
dceae3c1 1669 private:
c06b7b0b
ILT
1670 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1671 big_endian> Base;
1672
1673 public:
1674 typedef typename Base::Output_reloc_type Output_reloc_type;
1675 typedef typename Output_reloc_type::Address Address;
1676 typedef typename Output_reloc_type::Addend Addend;
1677
d98bc257
ILT
1678 Output_data_reloc(bool sr)
1679 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
c06b7b0b
ILT
1680 { }
1681
1682 // Add a reloc against a global symbol.
5a6f7e2d 1683
c06b7b0b 1684 void
a3ad94ed 1685 add_global(Symbol* gsym, unsigned int type, Output_data* od,
2ea97941
ILT
1686 Address address, Addend addend)
1687 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
0da6fa6c 1688 false, false)); }
c06b7b0b 1689
5a6f7e2d 1690 void
ef9beddf
ILT
1691 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1692 Sized_relobj<size, big_endian>* relobj,
2ea97941 1693 unsigned int shndx, Address address,
4f4c5f80 1694 Addend addend)
2ea97941 1695 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
0da6fa6c 1696 addend, false, false)); }
e8c846c3
ILT
1697
1698 // Add a RELATIVE reloc against a global symbol. The final output
1699 // relocation will not reference the symbol, but we must keep the symbol
1700 // information long enough to set the addend of the relocation correctly
1701 // when it is written.
1702
1703 void
1704 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
2ea97941 1705 Address address, Addend addend)
0da6fa6c
DM
1706 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true,
1707 true)); }
e8c846c3
ILT
1708
1709 void
1710 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
ef9beddf 1711 Sized_relobj<size, big_endian>* relobj,
2ea97941
ILT
1712 unsigned int shndx, Address address, Addend addend)
1713 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
0da6fa6c
DM
1714 addend, true, true)); }
1715
1716 // Add a global relocation which does not use a symbol for the relocation,
1717 // but which gets its addend from a symbol.
1718
1719 void
1720 add_symbolless_global_addend(Symbol* gsym, unsigned int type, Output_data* od,
1721 Address address, Addend addend)
1722 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1723 false, true)); }
1724
1725 void
1726 add_symbolless_global_addend(Symbol* gsym, unsigned int type,
1727 Output_data* od,
1728 Sized_relobj<size, big_endian>* relobj,
1729 unsigned int shndx, Address address, Addend addend)
1730 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1731 addend, false, true)); }
5a6f7e2d 1732
c06b7b0b 1733 // Add a reloc against a local symbol.
5a6f7e2d 1734
c06b7b0b 1735 void
5a6f7e2d 1736 add_local(Sized_relobj<size, big_endian>* relobj,
c06b7b0b 1737 unsigned int local_sym_index, unsigned int type,
2ea97941 1738 Output_data* od, Address address, Addend addend)
c06b7b0b 1739 {
2ea97941 1740 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
0da6fa6c 1741 addend, false, false, false));
5a6f7e2d
ILT
1742 }
1743
1744 void
1745 add_local(Sized_relobj<size, big_endian>* relobj,
1746 unsigned int local_sym_index, unsigned int type,
2ea97941 1747 Output_data* od, unsigned int shndx, Address address,
4f4c5f80 1748 Addend addend)
5a6f7e2d 1749 {
4f4c5f80 1750 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
0da6fa6c 1751 address, addend, false, false, false));
e8c846c3
ILT
1752 }
1753
1754 // Add a RELATIVE reloc against a local symbol.
1755
1756 void
1757 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1758 unsigned int local_sym_index, unsigned int type,
2ea97941 1759 Output_data* od, Address address, Addend addend)
e8c846c3 1760 {
2ea97941 1761 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
0da6fa6c 1762 addend, true, true, false));
e8c846c3
ILT
1763 }
1764
1765 void
1766 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1767 unsigned int local_sym_index, unsigned int type,
2ea97941 1768 Output_data* od, unsigned int shndx, Address address,
e8c846c3
ILT
1769 Addend addend)
1770 {
1771 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
0da6fa6c
DM
1772 address, addend, true, true, false));
1773 }
1774
1775 // Add a local relocation which does not use a symbol for the relocation,
1776 // but which gets it's addend from a symbol.
1777
1778 void
1779 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1780 unsigned int local_sym_index, unsigned int type,
1781 Output_data* od, Address address, Addend addend)
1782 {
1783 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1784 addend, false, true, false));
1785 }
1786
1787 void
1788 add_symbolless_local_addend(Sized_relobj<size, big_endian>* relobj,
1789 unsigned int local_sym_index, unsigned int type,
1790 Output_data* od, unsigned int shndx,
1791 Address address, Addend addend)
1792 {
1793 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1794 address, addend, false, true, false));
dceae3c1
ILT
1795 }
1796
1797 // Add a reloc against a local section symbol. This will be
1798 // converted into a reloc against the STT_SECTION symbol of the
1799 // output section.
1800
1801 void
1802 add_local_section(Sized_relobj<size, big_endian>* relobj,
1803 unsigned int input_shndx, unsigned int type,
2ea97941 1804 Output_data* od, Address address, Addend addend)
dceae3c1 1805 {
2ea97941 1806 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
0da6fa6c 1807 addend, false, false, true));
dceae3c1
ILT
1808 }
1809
1810 void
1811 add_local_section(Sized_relobj<size, big_endian>* relobj,
1812 unsigned int input_shndx, unsigned int type,
2ea97941 1813 Output_data* od, unsigned int shndx, Address address,
dceae3c1
ILT
1814 Addend addend)
1815 {
1816 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
0da6fa6c 1817 address, addend, false, false, true));
c06b7b0b
ILT
1818 }
1819
1820 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1821
c06b7b0b 1822 void
a3ad94ed 1823 add_output_section(Output_section* os, unsigned int type, Output_data* od,
2ea97941 1824 Address address, Addend addend)
829c9745 1825 { this->add(od, Output_reloc_type(os, type, od, address, addend)); }
5a6f7e2d
ILT
1826
1827 void
829c9745 1828 add_output_section(Output_section* os, unsigned int type, Output_data* od,
ef9beddf 1829 Sized_relobj<size, big_endian>* relobj,
2ea97941 1830 unsigned int shndx, Address address, Addend addend)
829c9745 1831 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address,
4f4c5f80 1832 addend)); }
e291e7b9
ILT
1833
1834 // Add an absolute relocation.
1835
1836 void
1837 add_absolute(unsigned int type, Output_data* od, Address address,
1838 Addend addend)
1839 { this->add(od, Output_reloc_type(type, od, address, addend)); }
1840
1841 void
1842 add_absolute(unsigned int type, Output_data* od,
1843 Sized_relobj<size, big_endian>* relobj,
1844 unsigned int shndx, Address address, Addend addend)
1845 { this->add(od, Output_reloc_type(type, relobj, shndx, address, addend)); }
1846
1847 // Add a target specific relocation. A target which calls this must
1848 // define the reloc_symbol_index and reloc_addend virtual functions.
1849
1850 void
1851 add_target_specific(unsigned int type, void* arg, Output_data* od,
1852 Address address, Addend addend)
1853 { this->add(od, Output_reloc_type(type, arg, od, address, addend)); }
1854
1855 void
1856 add_target_specific(unsigned int type, void* arg, Output_data* od,
1857 Sized_relobj<size, big_endian>* relobj,
1858 unsigned int shndx, Address address, Addend addend)
1859 {
1860 this->add(od, Output_reloc_type(type, arg, relobj, shndx, address,
1861 addend));
1862 }
c06b7b0b
ILT
1863};
1864
6a74a719
ILT
1865// Output_relocatable_relocs represents a relocation section in a
1866// relocatable link. The actual data is written out in the target
1867// hook relocate_for_relocatable. This just saves space for it.
1868
1869template<int sh_type, int size, bool big_endian>
1870class Output_relocatable_relocs : public Output_section_data
1871{
1872 public:
1873 Output_relocatable_relocs(Relocatable_relocs* rr)
1874 : Output_section_data(Output_data::default_alignment_for_size(size)),
1875 rr_(rr)
1876 { }
1877
1878 void
1879 set_final_data_size();
1880
1881 // Write out the data. There is nothing to do here.
1882 void
1883 do_write(Output_file*)
1884 { }
1885
7d9e3d98
ILT
1886 // Write to a map file.
1887 void
1888 do_print_to_mapfile(Mapfile* mapfile) const
1889 { mapfile->print_output_data(this, _("** relocs")); }
1890
6a74a719
ILT
1891 private:
1892 // The relocs associated with this input section.
1893 Relocatable_relocs* rr_;
1894};
1895
1896// Handle a GROUP section.
1897
1898template<int size, bool big_endian>
1899class Output_data_group : public Output_section_data
1900{
1901 public:
8825ac63 1902 // The constructor clears *INPUT_SHNDXES.
6a74a719
ILT
1903 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1904 section_size_type entry_count,
8825ac63
ILT
1905 elfcpp::Elf_Word flags,
1906 std::vector<unsigned int>* input_shndxes);
6a74a719
ILT
1907
1908 void
1909 do_write(Output_file*);
1910
7d9e3d98
ILT
1911 // Write to a map file.
1912 void
1913 do_print_to_mapfile(Mapfile* mapfile) const
1914 { mapfile->print_output_data(this, _("** group")); }
1915
20e6d0d6
DK
1916 // Set final data size.
1917 void
1918 set_final_data_size()
1919 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1920
6a74a719
ILT
1921 private:
1922 // The input object.
1923 Sized_relobj<size, big_endian>* relobj_;
1924 // The group flag word.
1925 elfcpp::Elf_Word flags_;
1926 // The section indexes of the input sections in this group.
8825ac63 1927 std::vector<unsigned int> input_shndxes_;
6a74a719
ILT
1928};
1929
dbe717ef
ILT
1930// Output_data_got is used to manage a GOT. Each entry in the GOT is
1931// for one symbol--either a global symbol or a local symbol in an
ead1e424 1932// object. The target specific code adds entries to the GOT as
dbe717ef 1933// needed.
ead1e424
ILT
1934
1935template<int size, bool big_endian>
27bc2bce 1936class Output_data_got : public Output_section_data_build
ead1e424
ILT
1937{
1938 public:
1939 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
7bf1f802
ILT
1940 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1941 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
ead1e424 1942
7e1edb90 1943 Output_data_got()
27bc2bce 1944 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
730cdc88 1945 entries_()
ead1e424
ILT
1946 { }
1947
dbe717ef
ILT
1948 // Add an entry for a global symbol to the GOT. Return true if this
1949 // is a new GOT entry, false if the symbol was already in the GOT.
1950 bool
0a65a3a7 1951 add_global(Symbol* gsym, unsigned int got_type);
ead1e424 1952
7223e9ca
ILT
1953 // Like add_global, but use the PLT offset of the global symbol if
1954 // it has one.
1955 bool
1956 add_global_plt(Symbol* gsym, unsigned int got_type);
1957
7bf1f802
ILT
1958 // Add an entry for a global symbol to the GOT, and add a dynamic
1959 // relocation of type R_TYPE for the GOT entry.
1960 void
0a65a3a7
CC
1961 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1962 Rel_dyn* rel_dyn, unsigned int r_type);
7bf1f802
ILT
1963
1964 void
0a65a3a7
CC
1965 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1966 Rela_dyn* rela_dyn, unsigned int r_type);
1967
1968 // Add a pair of entries for a global symbol to the GOT, and add
1969 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1970 void
1971 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1972 Rel_dyn* rel_dyn, unsigned int r_type_1,
1973 unsigned int r_type_2);
1974
1975 void
1976 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1977 Rela_dyn* rela_dyn, unsigned int r_type_1,
1978 unsigned int r_type_2);
7bf1f802 1979
e727fa71
ILT
1980 // Add an entry for a local symbol to the GOT. This returns true if
1981 // this is a new GOT entry, false if the symbol already has a GOT
1982 // entry.
1983 bool
0a65a3a7
CC
1984 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1985 unsigned int got_type);
ead1e424 1986
7223e9ca
ILT
1987 // Like add_local, but use the PLT offset of the local symbol if it
1988 // has one.
1989 bool
1990 add_local_plt(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1991 unsigned int got_type);
1992
0a65a3a7 1993 // Add an entry for a local symbol to the GOT, and add a dynamic
7bf1f802
ILT
1994 // relocation of type R_TYPE for the GOT entry.
1995 void
1996 add_local_with_rel(Sized_relobj<size, big_endian>* object,
0a65a3a7
CC
1997 unsigned int sym_index, unsigned int got_type,
1998 Rel_dyn* rel_dyn, unsigned int r_type);
7bf1f802
ILT
1999
2000 void
2001 add_local_with_rela(Sized_relobj<size, big_endian>* object,
0a65a3a7
CC
2002 unsigned int sym_index, unsigned int got_type,
2003 Rela_dyn* rela_dyn, unsigned int r_type);
07f397ab 2004
0a65a3a7
CC
2005 // Add a pair of entries for a local symbol to the GOT, and add
2006 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
7bf1f802 2007 void
0a65a3a7
CC
2008 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
2009 unsigned int sym_index, unsigned int shndx,
2010 unsigned int got_type, Rel_dyn* rel_dyn,
2011 unsigned int r_type_1, unsigned int r_type_2);
7bf1f802
ILT
2012
2013 void
0a65a3a7
CC
2014 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
2015 unsigned int sym_index, unsigned int shndx,
2016 unsigned int got_type, Rela_dyn* rela_dyn,
2017 unsigned int r_type_1, unsigned int r_type_2);
7bf1f802 2018
ead1e424
ILT
2019 // Add a constant to the GOT. This returns the offset of the new
2020 // entry from the start of the GOT.
2021 unsigned int
2022 add_constant(Valtype constant)
2023 {
2024 this->entries_.push_back(Got_entry(constant));
2025 this->set_got_size();
2026 return this->last_got_offset();
2027 }
2028
27bc2bce 2029 protected:
ead1e424
ILT
2030 // Write out the GOT table.
2031 void
2032 do_write(Output_file*);
2033
7d9e3d98
ILT
2034 // Write to a map file.
2035 void
2036 do_print_to_mapfile(Mapfile* mapfile) const
2037 { mapfile->print_output_data(this, _("** GOT")); }
2038
ead1e424
ILT
2039 private:
2040 // This POD class holds a single GOT entry.
2041 class Got_entry
2042 {
2043 public:
2044 // Create a zero entry.
2045 Got_entry()
7223e9ca 2046 : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
ead1e424
ILT
2047 { this->u_.constant = 0; }
2048
2049 // Create a global symbol entry.
7223e9ca
ILT
2050 Got_entry(Symbol* gsym, bool use_plt_offset)
2051 : local_sym_index_(GSYM_CODE), use_plt_offset_(use_plt_offset)
ead1e424
ILT
2052 { this->u_.gsym = gsym; }
2053
2054 // Create a local symbol entry.
e727fa71 2055 Got_entry(Sized_relobj<size, big_endian>* object,
7223e9ca
ILT
2056 unsigned int local_sym_index, bool use_plt_offset)
2057 : local_sym_index_(local_sym_index), use_plt_offset_(use_plt_offset)
ead1e424 2058 {
a3ad94ed 2059 gold_assert(local_sym_index != GSYM_CODE
7223e9ca
ILT
2060 && local_sym_index != CONSTANT_CODE
2061 && local_sym_index == this->local_sym_index_);
ead1e424
ILT
2062 this->u_.object = object;
2063 }
2064
2065 // Create a constant entry. The constant is a host value--it will
2066 // be swapped, if necessary, when it is written out.
a3ad94ed 2067 explicit Got_entry(Valtype constant)
7223e9ca 2068 : local_sym_index_(CONSTANT_CODE), use_plt_offset_(false)
ead1e424
ILT
2069 { this->u_.constant = constant; }
2070
2071 // Write the GOT entry to an output view.
2072 void
7e1edb90 2073 write(unsigned char* pov) const;
ead1e424
ILT
2074
2075 private:
2076 enum
2077 {
7223e9ca
ILT
2078 GSYM_CODE = 0x7fffffff,
2079 CONSTANT_CODE = 0x7ffffffe
ead1e424
ILT
2080 };
2081
2082 union
2083 {
2084 // For a local symbol, the object.
e727fa71 2085 Sized_relobj<size, big_endian>* object;
ead1e424
ILT
2086 // For a global symbol, the symbol.
2087 Symbol* gsym;
2088 // For a constant, the constant.
2089 Valtype constant;
2090 } u_;
c06b7b0b
ILT
2091 // For a local symbol, the local symbol index. This is GSYM_CODE
2092 // for a global symbol, or CONSTANT_CODE for a constant.
7223e9ca
ILT
2093 unsigned int local_sym_index_ : 31;
2094 // Whether to use the PLT offset of the symbol if it has one.
2095 bool use_plt_offset_ : 1;
ead1e424
ILT
2096 };
2097
2098 typedef std::vector<Got_entry> Got_entries;
2099
2100 // Return the offset into the GOT of GOT entry I.
2101 unsigned int
2102 got_offset(unsigned int i) const
2103 { return i * (size / 8); }
2104
2105 // Return the offset into the GOT of the last entry added.
2106 unsigned int
2107 last_got_offset() const
2108 { return this->got_offset(this->entries_.size() - 1); }
2109
2110 // Set the size of the section.
2111 void
2112 set_got_size()
27bc2bce 2113 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
ead1e424
ILT
2114
2115 // The list of GOT entries.
2116 Got_entries entries_;
2117};
2118
a3ad94ed
ILT
2119// Output_data_dynamic is used to hold the data in SHT_DYNAMIC
2120// section.
2121
2122class Output_data_dynamic : public Output_section_data
2123{
2124 public:
9025d29d 2125 Output_data_dynamic(Stringpool* pool)
730cdc88 2126 : Output_section_data(Output_data::default_alignment()),
9025d29d 2127 entries_(), pool_(pool)
a3ad94ed
ILT
2128 { }
2129
2130 // Add a new dynamic entry with a fixed numeric value.
2131 void
2132 add_constant(elfcpp::DT tag, unsigned int val)
2133 { this->add_entry(Dynamic_entry(tag, val)); }
2134
16649710 2135 // Add a new dynamic entry with the address of output data.
a3ad94ed 2136 void
16649710
ILT
2137 add_section_address(elfcpp::DT tag, const Output_data* od)
2138 { this->add_entry(Dynamic_entry(tag, od, false)); }
a3ad94ed 2139
c2b45e22
CC
2140 // Add a new dynamic entry with the address of output data
2141 // plus a constant offset.
2142 void
2143 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
2ea97941
ILT
2144 unsigned int offset)
2145 { this->add_entry(Dynamic_entry(tag, od, offset)); }
c2b45e22 2146
16649710 2147 // Add a new dynamic entry with the size of output data.
a3ad94ed 2148 void
16649710
ILT
2149 add_section_size(elfcpp::DT tag, const Output_data* od)
2150 { this->add_entry(Dynamic_entry(tag, od, true)); }
a3ad94ed 2151
612a8d3d
DM
2152 // Add a new dynamic entry with the total size of two output datas.
2153 void
2154 add_section_size(elfcpp::DT tag, const Output_data* od,
2155 const Output_data* od2)
2156 { this->add_entry(Dynamic_entry(tag, od, od2)); }
2157
a3ad94ed
ILT
2158 // Add a new dynamic entry with the address of a symbol.
2159 void
16649710 2160 add_symbol(elfcpp::DT tag, const Symbol* sym)
a3ad94ed
ILT
2161 { this->add_entry(Dynamic_entry(tag, sym)); }
2162
2163 // Add a new dynamic entry with a string.
2164 void
2165 add_string(elfcpp::DT tag, const char* str)
cfd73a4e 2166 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
a3ad94ed 2167
41f542e7
ILT
2168 void
2169 add_string(elfcpp::DT tag, const std::string& str)
2170 { this->add_string(tag, str.c_str()); }
2171
27bc2bce
ILT
2172 protected:
2173 // Adjust the output section to set the entry size.
2174 void
2175 do_adjust_output_section(Output_section*);
2176
a3ad94ed
ILT
2177 // Set the final data size.
2178 void
27bc2bce 2179 set_final_data_size();
a3ad94ed
ILT
2180
2181 // Write out the dynamic entries.
2182 void
2183 do_write(Output_file*);
2184
7d9e3d98
ILT
2185 // Write to a map file.
2186 void
2187 do_print_to_mapfile(Mapfile* mapfile) const
2188 { mapfile->print_output_data(this, _("** dynamic")); }
2189
a3ad94ed
ILT
2190 private:
2191 // This POD class holds a single dynamic entry.
2192 class Dynamic_entry
2193 {
2194 public:
2195 // Create an entry with a fixed numeric value.
2ea97941
ILT
2196 Dynamic_entry(elfcpp::DT tag, unsigned int val)
2197 : tag_(tag), offset_(DYNAMIC_NUMBER)
a3ad94ed
ILT
2198 { this->u_.val = val; }
2199
2200 // Create an entry with the size or address of a section.
2ea97941
ILT
2201 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
2202 : tag_(tag),
c2b45e22
CC
2203 offset_(section_size
2204 ? DYNAMIC_SECTION_SIZE
2205 : DYNAMIC_SECTION_ADDRESS)
612a8d3d
DM
2206 {
2207 this->u_.od = od;
2208 this->od2 = NULL;
2209 }
2210
2211 // Create an entry with the size of two sections.
2212 Dynamic_entry(elfcpp::DT tag, const Output_data* od, const Output_data* od2)
2213 : tag_(tag),
2214 offset_(DYNAMIC_SECTION_SIZE)
2215 {
2216 this->u_.od = od;
2217 this->od2 = od2;
2218 }
c2b45e22
CC
2219
2220 // Create an entry with the address of a section plus a constant offset.
2ea97941
ILT
2221 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
2222 : tag_(tag),
c2b45e22 2223 offset_(offset)
16649710 2224 { this->u_.od = od; }
a3ad94ed
ILT
2225
2226 // Create an entry with the address of a symbol.
2ea97941
ILT
2227 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
2228 : tag_(tag), offset_(DYNAMIC_SYMBOL)
a3ad94ed
ILT
2229 { this->u_.sym = sym; }
2230
2231 // Create an entry with a string.
2ea97941
ILT
2232 Dynamic_entry(elfcpp::DT tag, const char* str)
2233 : tag_(tag), offset_(DYNAMIC_STRING)
a3ad94ed
ILT
2234 { this->u_.str = str; }
2235
20e6d0d6
DK
2236 // Return the tag of this entry.
2237 elfcpp::DT
2238 tag() const
2239 { return this->tag_; }
2240
a3ad94ed
ILT
2241 // Write the dynamic entry to an output view.
2242 template<int size, bool big_endian>
2243 void
7d1a9ebb 2244 write(unsigned char* pov, const Stringpool*) const;
a3ad94ed
ILT
2245
2246 private:
c2b45e22 2247 // Classification is encoded in the OFFSET field.
a3ad94ed
ILT
2248 enum Classification
2249 {
a3ad94ed 2250 // Section address.
c2b45e22
CC
2251 DYNAMIC_SECTION_ADDRESS = 0,
2252 // Number.
2253 DYNAMIC_NUMBER = -1U,
a3ad94ed 2254 // Section size.
c2b45e22 2255 DYNAMIC_SECTION_SIZE = -2U,
a3ad94ed 2256 // Symbol adress.
c2b45e22 2257 DYNAMIC_SYMBOL = -3U,
a3ad94ed 2258 // String.
c2b45e22
CC
2259 DYNAMIC_STRING = -4U
2260 // Any other value indicates a section address plus OFFSET.
a3ad94ed
ILT
2261 };
2262
2263 union
2264 {
2265 // For DYNAMIC_NUMBER.
2266 unsigned int val;
c2b45e22 2267 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
16649710 2268 const Output_data* od;
a3ad94ed 2269 // For DYNAMIC_SYMBOL.
16649710 2270 const Symbol* sym;
a3ad94ed
ILT
2271 // For DYNAMIC_STRING.
2272 const char* str;
2273 } u_;
612a8d3d
DM
2274 // For DYNAMIC_SYMBOL with two sections.
2275 const Output_data* od2;
a3ad94ed
ILT
2276 // The dynamic tag.
2277 elfcpp::DT tag_;
c2b45e22
CC
2278 // The type of entry (Classification) or offset within a section.
2279 unsigned int offset_;
a3ad94ed
ILT
2280 };
2281
2282 // Add an entry to the list.
2283 void
2284 add_entry(const Dynamic_entry& entry)
2285 { this->entries_.push_back(entry); }
2286
2287 // Sized version of write function.
2288 template<int size, bool big_endian>
2289 void
2290 sized_write(Output_file* of);
2291
2292 // The type of the list of entries.
2293 typedef std::vector<Dynamic_entry> Dynamic_entries;
2294
a3ad94ed
ILT
2295 // The entries.
2296 Dynamic_entries entries_;
2297 // The pool used for strings.
2298 Stringpool* pool_;
2299};
2300
d491d34e
ILT
2301// Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2302// which may be required if the object file has more than
2303// SHN_LORESERVE sections.
2304
2305class Output_symtab_xindex : public Output_section_data
2306{
2307 public:
2308 Output_symtab_xindex(size_t symcount)
20e6d0d6 2309 : Output_section_data(symcount * 4, 4, true),
d491d34e
ILT
2310 entries_()
2311 { }
2312
2313 // Add an entry: symbol number SYMNDX has section SHNDX.
2314 void
2315 add(unsigned int symndx, unsigned int shndx)
2316 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2317
2318 protected:
2319 void
2320 do_write(Output_file*);
2321
7d9e3d98
ILT
2322 // Write to a map file.
2323 void
2324 do_print_to_mapfile(Mapfile* mapfile) const
2325 { mapfile->print_output_data(this, _("** symtab xindex")); }
2326
d491d34e
ILT
2327 private:
2328 template<bool big_endian>
2329 void
2330 endian_do_write(unsigned char*);
2331
2332 // It is likely that most symbols will not require entries. Rather
2333 // than keep a vector for all symbols, we keep pairs of symbol index
2334 // and section index.
2335 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2336
2337 // The entries we need.
2338 Xindex_entries entries_;
2339};
2340
20e6d0d6 2341// A relaxed input section.
c0a62865 2342class Output_relaxed_input_section : public Output_section_data_build
20e6d0d6
DK
2343{
2344 public:
2345 // We would like to call relobj->section_addralign(shndx) to get the
2346 // alignment but we do not want the constructor to fail. So callers
2347 // are repsonsible for ensuring that.
2ea97941
ILT
2348 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2349 uint64_t addralign)
2350 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
20e6d0d6
DK
2351 { }
2352
2353 // Return the Relobj of this relaxed input section.
2354 Relobj*
2355 relobj() const
2356 { return this->relobj_; }
2357
2358 // Return the section index of this relaxed input section.
2359 unsigned int
2360 shndx() const
2361 { return this->shndx_; }
2362
2363 private:
2364 Relobj* relobj_;
2365 unsigned int shndx_;
2366};
2367
0439c796
DK
2368// This class describes properties of merge data sections. It is used
2369// as a key type for maps.
2370class Merge_section_properties
2371{
2372 public:
2373 Merge_section_properties(bool is_string, uint64_t entsize,
2374 uint64_t addralign)
2375 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
2376 { }
2377
2378 // Whether this equals to another Merge_section_properties MSP.
2379 bool
2380 eq(const Merge_section_properties& msp) const
2381 {
2382 return ((this->is_string_ == msp.is_string_)
2383 && (this->entsize_ == msp.entsize_)
2384 && (this->addralign_ == msp.addralign_));
2385 }
2386
2387 // Compute a hash value for this using 64-bit FNV-1a hash.
2388 size_t
2389 hash_value() const
2390 {
2391 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
2392 uint64_t prime = 1099511628211ULL;
2393 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
2394 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
2395 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
2396 return h;
2397 }
2398
2399 // Functors for associative containers.
2400 struct equal_to
2401 {
2402 bool
2403 operator()(const Merge_section_properties& msp1,
2404 const Merge_section_properties& msp2) const
2405 { return msp1.eq(msp2); }
2406 };
2407
2408 struct hash
2409 {
2410 size_t
2411 operator()(const Merge_section_properties& msp) const
2412 { return msp.hash_value(); }
2413 };
2414
2415 private:
2416 // Whether this merge data section is for strings.
2417 bool is_string_;
2418 // Entsize of this merge data section.
2419 uint64_t entsize_;
2420 // Address alignment.
2421 uint64_t addralign_;
2422};
2423
2424// This class is used to speed up look up of special input sections in an
2425// Output_section.
2426
2427class Output_section_lookup_maps
2428{
2429 public:
2430 Output_section_lookup_maps()
2431 : is_valid_(true), merge_sections_by_properties_(),
2432 merge_sections_by_id_(), relaxed_input_sections_by_id_()
2433 { }
2434
2435 // Whether the maps are valid.
2436 bool
2437 is_valid() const
2438 { return this->is_valid_; }
2439
2440 // Invalidate the maps.
2441 void
2442 invalidate()
2443 { this->is_valid_ = false; }
2444
2445 // Clear the maps.
2446 void
2447 clear()
2448 {
2449 this->merge_sections_by_properties_.clear();
2450 this->merge_sections_by_id_.clear();
2451 this->relaxed_input_sections_by_id_.clear();
2452 // A cleared map is valid.
2453 this->is_valid_ = true;
2454 }
2455
2456 // Find a merge section by merge section properties. Return NULL if none
2457 // is found.
2458 Output_merge_base*
2459 find_merge_section(const Merge_section_properties& msp) const
2460 {
2461 gold_assert(this->is_valid_);
2462 Merge_sections_by_properties::const_iterator p =
2463 this->merge_sections_by_properties_.find(msp);
2464 return p != this->merge_sections_by_properties_.end() ? p->second : NULL;
2465 }
2466
2467 // Find a merge section by section ID of a merge input section. Return NULL
2468 // if none is found.
2469 Output_merge_base*
2470 find_merge_section(const Object* object, unsigned int shndx) const
2471 {
2472 gold_assert(this->is_valid_);
2473 Merge_sections_by_id::const_iterator p =
2474 this->merge_sections_by_id_.find(Const_section_id(object, shndx));
2475 return p != this->merge_sections_by_id_.end() ? p->second : NULL;
2476 }
2477
2478 // Add a merge section pointed by POMB with properties MSP.
2479 void
2480 add_merge_section(const Merge_section_properties& msp,
2481 Output_merge_base* pomb)
2482 {
2483 std::pair<Merge_section_properties, Output_merge_base*> value(msp, pomb);
2484 std::pair<Merge_sections_by_properties::iterator, bool> result =
2485 this->merge_sections_by_properties_.insert(value);
82742395 2486 gold_assert(result.second);
0439c796
DK
2487 }
2488
2489 // Add a mapping from a merged input section in OBJECT with index SHNDX
2490 // to a merge output section pointed by POMB.
2491 void
2492 add_merge_input_section(const Object* object, unsigned int shndx,
2493 Output_merge_base* pomb)
2494 {
2495 Const_section_id csid(object, shndx);
2496 std::pair<Const_section_id, Output_merge_base*> value(csid, pomb);
2497 std::pair<Merge_sections_by_id::iterator, bool> result =
2498 this->merge_sections_by_id_.insert(value);
82742395 2499 gold_assert(result.second);
0439c796
DK
2500 }
2501
2502 // Find a relaxed input section of OBJECT with index SHNDX.
2503 Output_relaxed_input_section*
2504 find_relaxed_input_section(const Object* object, unsigned int shndx) const
2505 {
2506 gold_assert(this->is_valid_);
2507 Relaxed_input_sections_by_id::const_iterator p =
2508 this->relaxed_input_sections_by_id_.find(Const_section_id(object, shndx));
2509 return p != this->relaxed_input_sections_by_id_.end() ? p->second : NULL;
2510 }
2511
2512 // Add a relaxed input section pointed by POMB and whose original input
2513 // section is in OBJECT with index SHNDX.
2514 void
2515 add_relaxed_input_section(const Relobj* relobj, unsigned int shndx,
2516 Output_relaxed_input_section* poris)
2517 {
2518 Const_section_id csid(relobj, shndx);
2519 std::pair<Const_section_id, Output_relaxed_input_section*>
2520 value(csid, poris);
2521 std::pair<Relaxed_input_sections_by_id::iterator, bool> result =
2522 this->relaxed_input_sections_by_id_.insert(value);
82742395 2523 gold_assert(result.second);
0439c796
DK
2524 }
2525
2526 private:
2527 typedef Unordered_map<Const_section_id, Output_merge_base*,
2528 Const_section_id_hash>
2529 Merge_sections_by_id;
2530
2531 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
2532 Merge_section_properties::hash,
2533 Merge_section_properties::equal_to>
2534 Merge_sections_by_properties;
2535
2536 typedef Unordered_map<Const_section_id, Output_relaxed_input_section*,
2537 Const_section_id_hash>
2538 Relaxed_input_sections_by_id;
2539
2540 // Whether this is valid
2541 bool is_valid_;
2542 // Merge sections by merge section properties.
2543 Merge_sections_by_properties merge_sections_by_properties_;
2544 // Merge sections by section IDs.
2545 Merge_sections_by_id merge_sections_by_id_;
2546 // Relaxed sections by section IDs.
2547 Relaxed_input_sections_by_id relaxed_input_sections_by_id_;
2548};
2549
a2fb1b05
ILT
2550// An output section. We don't expect to have too many output
2551// sections, so we don't bother to do a template on the size.
2552
54dc6425 2553class Output_section : public Output_data
a2fb1b05
ILT
2554{
2555 public:
2556 // Create an output section, giving the name, type, and flags.
96803768 2557 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
54dc6425 2558 virtual ~Output_section();
a2fb1b05 2559
ead1e424 2560 // Add a new input section SHNDX, named NAME, with header SHDR, from
730cdc88 2561 // object OBJECT. RELOC_SHNDX is the index of a relocation section
eff45813 2562 // which applies to this section, or 0 if none, or -1 if more than
a445fddf
ILT
2563 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2564 // in a linker script; in that case we need to keep track of input
2565 // sections associated with an output section. Return the offset
2566 // within the output section.
a2fb1b05
ILT
2567 template<int size, bool big_endian>
2568 off_t
6e9ba2ca 2569 add_input_section(Layout* layout, Sized_relobj<size, big_endian>* object,
ca09d69a 2570 unsigned int shndx, const char* name,
730cdc88 2571 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf 2572 unsigned int reloc_shndx, bool have_sections_script);
a2fb1b05 2573
b8e6aad9 2574 // Add generated data POSD to this output section.
c06b7b0b 2575 void
ead1e424
ILT
2576 add_output_section_data(Output_section_data* posd);
2577
d06fb4d1
DK
2578 // Add a relaxed input section PORIS called NAME to this output section
2579 // with LAYOUT.
c0a62865 2580 void
d06fb4d1
DK
2581 add_relaxed_input_section(Layout* layout,
2582 Output_relaxed_input_section* poris,
2583 const std::string& name);
c0a62865 2584
a2fb1b05
ILT
2585 // Return the section name.
2586 const char*
2587 name() const
2588 { return this->name_; }
2589
2590 // Return the section type.
2591 elfcpp::Elf_Word
2592 type() const
2593 { return this->type_; }
2594
2595 // Return the section flags.
2596 elfcpp::Elf_Xword
2597 flags() const
2598 { return this->flags_; }
2599
154e0e9a
ILT
2600 // Update the output section flags based on input section flags.
2601 void
9c547ec3 2602 update_flags_for_input_section(elfcpp::Elf_Xword flags);
154e0e9a 2603
a3ad94ed
ILT
2604 // Return the entsize field.
2605 uint64_t
2606 entsize() const
2607 { return this->entsize_; }
2608
61ba1cf9
ILT
2609 // Set the entsize field.
2610 void
16649710 2611 set_entsize(uint64_t v);
61ba1cf9 2612
a445fddf
ILT
2613 // Set the load address.
2614 void
2ea97941 2615 set_load_address(uint64_t load_address)
a445fddf 2616 {
2ea97941 2617 this->load_address_ = load_address;
a445fddf
ILT
2618 this->has_load_address_ = true;
2619 }
2620
16649710
ILT
2621 // Set the link field to the output section index of a section.
2622 void
14b31740 2623 set_link_section(const Output_data* od)
16649710
ILT
2624 {
2625 gold_assert(this->link_ == 0
2626 && !this->should_link_to_symtab_
2627 && !this->should_link_to_dynsym_);
2628 this->link_section_ = od;
2629 }
2630
2631 // Set the link field to a constant.
61ba1cf9
ILT
2632 void
2633 set_link(unsigned int v)
16649710
ILT
2634 {
2635 gold_assert(this->link_section_ == NULL
2636 && !this->should_link_to_symtab_
2637 && !this->should_link_to_dynsym_);
2638 this->link_ = v;
2639 }
61ba1cf9 2640
16649710
ILT
2641 // Record that this section should link to the normal symbol table.
2642 void
2643 set_should_link_to_symtab()
2644 {
2645 gold_assert(this->link_section_ == NULL
2646 && this->link_ == 0
2647 && !this->should_link_to_dynsym_);
2648 this->should_link_to_symtab_ = true;
2649 }
2650
2651 // Record that this section should link to the dynamic symbol table.
2652 void
2653 set_should_link_to_dynsym()
2654 {
2655 gold_assert(this->link_section_ == NULL
2656 && this->link_ == 0
2657 && !this->should_link_to_symtab_);
2658 this->should_link_to_dynsym_ = true;
2659 }
2660
2661 // Return the info field.
2662 unsigned int
2663 info() const
2664 {
755ab8af
ILT
2665 gold_assert(this->info_section_ == NULL
2666 && this->info_symndx_ == NULL);
16649710
ILT
2667 return this->info_;
2668 }
2669
2670 // Set the info field to the output section index of a section.
2671 void
755ab8af 2672 set_info_section(const Output_section* os)
16649710 2673 {
755ab8af
ILT
2674 gold_assert((this->info_section_ == NULL
2675 || (this->info_section_ == os
2676 && this->info_uses_section_index_))
2677 && this->info_symndx_ == NULL
2678 && this->info_ == 0);
2679 this->info_section_ = os;
2680 this->info_uses_section_index_= true;
16649710
ILT
2681 }
2682
6a74a719
ILT
2683 // Set the info field to the symbol table index of a symbol.
2684 void
2685 set_info_symndx(const Symbol* sym)
2686 {
755ab8af
ILT
2687 gold_assert(this->info_section_ == NULL
2688 && (this->info_symndx_ == NULL
2689 || this->info_symndx_ == sym)
2690 && this->info_ == 0);
6a74a719
ILT
2691 this->info_symndx_ = sym;
2692 }
2693
755ab8af
ILT
2694 // Set the info field to the symbol table index of a section symbol.
2695 void
2696 set_info_section_symndx(const Output_section* os)
2697 {
2698 gold_assert((this->info_section_ == NULL
2699 || (this->info_section_ == os
2700 && !this->info_uses_section_index_))
2701 && this->info_symndx_ == NULL
2702 && this->info_ == 0);
2703 this->info_section_ = os;
2704 this->info_uses_section_index_ = false;
2705 }
2706
16649710 2707 // Set the info field to a constant.
61ba1cf9
ILT
2708 void
2709 set_info(unsigned int v)
16649710 2710 {
755ab8af
ILT
2711 gold_assert(this->info_section_ == NULL
2712 && this->info_symndx_ == NULL
2713 && (this->info_ == 0
2714 || this->info_ == v));
16649710
ILT
2715 this->info_ = v;
2716 }
61ba1cf9
ILT
2717
2718 // Set the addralign field.
2719 void
2720 set_addralign(uint64_t v)
2721 { this->addralign_ = v; }
2722
d491d34e
ILT
2723 // Whether the output section index has been set.
2724 bool
2725 has_out_shndx() const
2726 { return this->out_shndx_ != -1U; }
2727
c06b7b0b
ILT
2728 // Indicate that we need a symtab index.
2729 void
2730 set_needs_symtab_index()
2731 { this->needs_symtab_index_ = true; }
2732
2733 // Return whether we need a symtab index.
2734 bool
2735 needs_symtab_index() const
2736 { return this->needs_symtab_index_; }
2737
2738 // Get the symtab index.
2739 unsigned int
2740 symtab_index() const
2741 {
a3ad94ed 2742 gold_assert(this->symtab_index_ != 0);
c06b7b0b
ILT
2743 return this->symtab_index_;
2744 }
2745
2746 // Set the symtab index.
2747 void
2748 set_symtab_index(unsigned int index)
2749 {
a3ad94ed 2750 gold_assert(index != 0);
c06b7b0b
ILT
2751 this->symtab_index_ = index;
2752 }
2753
2754 // Indicate that we need a dynsym index.
2755 void
2756 set_needs_dynsym_index()
2757 { this->needs_dynsym_index_ = true; }
2758
2759 // Return whether we need a dynsym index.
2760 bool
2761 needs_dynsym_index() const
2762 { return this->needs_dynsym_index_; }
2763
2764 // Get the dynsym index.
2765 unsigned int
2766 dynsym_index() const
2767 {
a3ad94ed 2768 gold_assert(this->dynsym_index_ != 0);
c06b7b0b
ILT
2769 return this->dynsym_index_;
2770 }
2771
2772 // Set the dynsym index.
2773 void
2774 set_dynsym_index(unsigned int index)
2775 {
a3ad94ed 2776 gold_assert(index != 0);
c06b7b0b
ILT
2777 this->dynsym_index_ = index;
2778 }
2779
2fd32231
ILT
2780 // Return whether the input sections sections attachd to this output
2781 // section may require sorting. This is used to handle constructor
2782 // priorities compatibly with GNU ld.
2783 bool
2784 may_sort_attached_input_sections() const
2785 { return this->may_sort_attached_input_sections_; }
2786
2787 // Record that the input sections attached to this output section
2788 // may require sorting.
2789 void
2790 set_may_sort_attached_input_sections()
2791 { this->may_sort_attached_input_sections_ = true; }
2792
6e9ba2ca
ST
2793 // Returns true if input sections must be sorted according to the
2794 // order in which their name appear in the --section-ordering-file.
2795 bool
2796 input_section_order_specified()
2797 { return this->input_section_order_specified_; }
2798
2799 // Record that input sections must be sorted as some of their names
2800 // match the patterns specified through --section-ordering-file.
2801 void
2802 set_input_section_order_specified()
2803 { this->input_section_order_specified_ = true; }
2804
2fd32231
ILT
2805 // Return whether the input sections attached to this output section
2806 // require sorting. This is used to handle constructor priorities
2807 // compatibly with GNU ld.
2808 bool
2809 must_sort_attached_input_sections() const
2810 { return this->must_sort_attached_input_sections_; }
2811
2812 // Record that the input sections attached to this output section
2813 // require sorting.
2814 void
2815 set_must_sort_attached_input_sections()
2816 { this->must_sort_attached_input_sections_ = true; }
2817
22f0da72
ILT
2818 // Get the order in which this section appears in the PT_LOAD output
2819 // segment.
2820 Output_section_order
2821 order() const
2822 { return this->order_; }
2823
2824 // Set the order for this section.
2825 void
2826 set_order(Output_section_order order)
2827 { this->order_ = order; }
2828
9f1d377b
ILT
2829 // Return whether this section holds relro data--data which has
2830 // dynamic relocations but which may be marked read-only after the
2831 // dynamic relocations have been completed.
2832 bool
2833 is_relro() const
2834 { return this->is_relro_; }
2835
2836 // Record that this section holds relro data.
2837 void
2838 set_is_relro()
2839 { this->is_relro_ = true; }
2840
2d924fd9
ILT
2841 // Record that this section does not hold relro data.
2842 void
2843 clear_is_relro()
2844 { this->is_relro_ = false; }
2845
8a5e3e08
ILT
2846 // True if this is a small section: a section which holds small
2847 // variables.
2848 bool
2849 is_small_section() const
2850 { return this->is_small_section_; }
2851
2852 // Record that this is a small section.
2853 void
2854 set_is_small_section()
2855 { this->is_small_section_ = true; }
2856
2857 // True if this is a large section: a section which holds large
2858 // variables.
2859 bool
2860 is_large_section() const
2861 { return this->is_large_section_; }
2862
2863 // Record that this is a large section.
2864 void
2865 set_is_large_section()
2866 { this->is_large_section_ = true; }
2867
2868 // True if this is a large data (not BSS) section.
2869 bool
2870 is_large_data_section()
2871 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2872
730cdc88
ILT
2873 // Return whether this section should be written after all the input
2874 // sections are complete.
2875 bool
2876 after_input_sections() const
2877 { return this->after_input_sections_; }
2878
2879 // Record that this section should be written after all the input
2880 // sections are complete.
2881 void
2882 set_after_input_sections()
2883 { this->after_input_sections_ = true; }
2884
27bc2bce
ILT
2885 // Return whether this section requires postprocessing after all
2886 // relocations have been applied.
2887 bool
2888 requires_postprocessing() const
2889 { return this->requires_postprocessing_; }
2890
96803768
ILT
2891 // If a section requires postprocessing, return the buffer to use.
2892 unsigned char*
2893 postprocessing_buffer() const
2894 {
2895 gold_assert(this->postprocessing_buffer_ != NULL);
2896 return this->postprocessing_buffer_;
2897 }
2898
2899 // If a section requires postprocessing, create the buffer to use.
27bc2bce 2900 void
96803768
ILT
2901 create_postprocessing_buffer();
2902
2903 // If a section requires postprocessing, this is the size of the
2904 // buffer to which relocations should be applied.
2905 off_t
2906 postprocessing_buffer_size() const
2907 { return this->current_data_size_for_child(); }
27bc2bce 2908
755ab8af
ILT
2909 // Modify the section name. This is only permitted for an
2910 // unallocated section, and only before the size has been finalized.
2911 // Otherwise the name will not get into Layout::namepool_.
2912 void
2913 set_name(const char* newname)
2914 {
2915 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2916 gold_assert(!this->is_data_size_valid());
2917 this->name_ = newname;
2918 }
2919
730cdc88
ILT
2920 // Return whether the offset OFFSET in the input section SHNDX in
2921 // object OBJECT is being included in the link.
2922 bool
2923 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2924 off_t offset) const;
2925
2926 // Return the offset within the output section of OFFSET relative to
2927 // the start of input section SHNDX in object OBJECT.
8383303e
ILT
2928 section_offset_type
2929 output_offset(const Relobj* object, unsigned int shndx,
2930 section_offset_type offset) const;
730cdc88 2931
b8e6aad9
ILT
2932 // Return the output virtual address of OFFSET relative to the start
2933 // of input section SHNDX in object OBJECT.
2934 uint64_t
2935 output_address(const Relobj* object, unsigned int shndx,
2936 off_t offset) const;
2937
e29e076a
ILT
2938 // Look for the merged section for input section SHNDX in object
2939 // OBJECT. If found, return true, and set *ADDR to the address of
2940 // the start of the merged section. This is not necessary the
2941 // output offset corresponding to input offset 0 in the section,
2942 // since the section may be mapped arbitrarily.
2943 bool
2944 find_starting_output_address(const Relobj* object, unsigned int shndx,
2945 uint64_t* addr) const;
a9a60db6 2946
a445fddf
ILT
2947 // Record that this output section was found in the SECTIONS clause
2948 // of a linker script.
2949 void
2950 set_found_in_sections_clause()
2951 { this->found_in_sections_clause_ = true; }
2952
2953 // Return whether this output section was found in the SECTIONS
2954 // clause of a linker script.
2955 bool
2956 found_in_sections_clause() const
2957 { return this->found_in_sections_clause_; }
2958
27bc2bce
ILT
2959 // Write the section header into *OPHDR.
2960 template<int size, bool big_endian>
2961 void
2962 write_header(const Layout*, const Stringpool*,
2963 elfcpp::Shdr_write<size, big_endian>*) const;
2964
a445fddf
ILT
2965 // The next few calls are for linker script support.
2966
ead1e424
ILT
2967 // In some cases we need to keep a list of the input sections
2968 // associated with this output section. We only need the list if we
2969 // might have to change the offsets of the input section within the
2970 // output section after we add the input section. The ordinary
2971 // input sections will be written out when we process the object
2972 // file, and as such we don't need to track them here. We do need
2973 // to track Output_section_data objects here. We store instances of
2974 // this structure in a std::vector, so it must be a POD. There can
2975 // be many instances of this structure, so we use a union to save
2976 // some space.
2977 class Input_section
2978 {
2979 public:
2980 Input_section()
b8e6aad9
ILT
2981 : shndx_(0), p2align_(0)
2982 {
2983 this->u1_.data_size = 0;
2984 this->u2_.object = NULL;
2985 }
ead1e424 2986
b8e6aad9 2987 // For an ordinary input section.
2ea97941
ILT
2988 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
2989 uint64_t addralign)
2990 : shndx_(shndx),
6e9ba2ca
ST
2991 p2align_(ffsll(static_cast<long long>(addralign))),
2992 section_order_index_(0)
ead1e424 2993 {
2ea97941
ILT
2994 gold_assert(shndx != OUTPUT_SECTION_CODE
2995 && shndx != MERGE_DATA_SECTION_CODE
2996 && shndx != MERGE_STRING_SECTION_CODE
2997 && shndx != RELAXED_INPUT_SECTION_CODE);
2998 this->u1_.data_size = data_size;
b8e6aad9 2999 this->u2_.object = object;
ead1e424
ILT
3000 }
3001
b8e6aad9 3002 // For a non-merge output section.
ead1e424 3003 Input_section(Output_section_data* posd)
6e9ba2ca
ST
3004 : shndx_(OUTPUT_SECTION_CODE), p2align_(0),
3005 section_order_index_(0)
b8e6aad9
ILT
3006 {
3007 this->u1_.data_size = 0;
3008 this->u2_.posd = posd;
3009 }
3010
3011 // For a merge section.
3012 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
3013 : shndx_(is_string
3014 ? MERGE_STRING_SECTION_CODE
3015 : MERGE_DATA_SECTION_CODE),
6e9ba2ca
ST
3016 p2align_(0),
3017 section_order_index_(0)
b8e6aad9
ILT
3018 {
3019 this->u1_.entsize = entsize;
3020 this->u2_.posd = posd;
3021 }
ead1e424 3022
20e6d0d6 3023 // For a relaxed input section.
ca09d69a 3024 Input_section(Output_relaxed_input_section* psection)
6e9ba2ca
ST
3025 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0),
3026 section_order_index_(0)
20e6d0d6
DK
3027 {
3028 this->u1_.data_size = 0;
3029 this->u2_.poris = psection;
3030 }
3031
6e9ba2ca
ST
3032 unsigned int
3033 section_order_index() const
3034 {
3035 return this->section_order_index_;
3036 }
3037
3038 void
3039 set_section_order_index(unsigned int number)
3040 {
3041 this->section_order_index_ = number;
3042 }
3043
ead1e424
ILT
3044 // The required alignment.
3045 uint64_t
3046 addralign() const
a3ad94ed 3047 {
6625d24e
DK
3048 if (this->p2align_ != 0)
3049 return static_cast<uint64_t>(1) << (this->p2align_ - 1);
3050 else if (!this->is_input_section())
f34787f8 3051 return this->u2_.posd->addralign();
6625d24e
DK
3052 else
3053 return 0;
a3ad94ed 3054 }
ead1e424 3055
6625d24e
DK
3056 // Set the required alignment, which must be either 0 or a power of 2.
3057 // For input sections that are sub-classes of Output_section_data, a
3058 // alignment of zero means asking the underlying object for alignment.
3059 void
3060 set_addralign(uint64_t addralign)
3061 {
3062 if (addralign == 0)
3063 this->p2align_ = 0;
3064 else
3065 {
3066 gold_assert((addralign & (addralign - 1)) == 0);
3067 this->p2align_ = ffsll(static_cast<long long>(addralign));
3068 }
3069 }
3070
cdc29364
CC
3071 // Return the current required size, without finalization.
3072 off_t
3073 current_data_size() const;
3074
ead1e424
ILT
3075 // Return the required size.
3076 off_t
3077 data_size() const;
3078
a445fddf
ILT
3079 // Whether this is an input section.
3080 bool
3081 is_input_section() const
3082 {
3083 return (this->shndx_ != OUTPUT_SECTION_CODE
3084 && this->shndx_ != MERGE_DATA_SECTION_CODE
20e6d0d6
DK
3085 && this->shndx_ != MERGE_STRING_SECTION_CODE
3086 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
a445fddf
ILT
3087 }
3088
b8e6aad9
ILT
3089 // Return whether this is a merge section which matches the
3090 // parameters.
3091 bool
87f95776 3092 is_merge_section(bool is_string, uint64_t entsize,
2ea97941 3093 uint64_t addralign) const
b8e6aad9
ILT
3094 {
3095 return (this->shndx_ == (is_string
3096 ? MERGE_STRING_SECTION_CODE
3097 : MERGE_DATA_SECTION_CODE)
87f95776 3098 && this->u1_.entsize == entsize
2ea97941 3099 && this->addralign() == addralign);
b8e6aad9
ILT
3100 }
3101
0439c796
DK
3102 // Return whether this is a merge section for some input section.
3103 bool
3104 is_merge_section() const
3105 {
3106 return (this->shndx_ == MERGE_DATA_SECTION_CODE
3107 || this->shndx_ == MERGE_STRING_SECTION_CODE);
3108 }
3109
20e6d0d6
DK
3110 // Return whether this is a relaxed input section.
3111 bool
3112 is_relaxed_input_section() const
3113 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
3114
3115 // Return whether this is a generic Output_section_data.
3116 bool
3117 is_output_section_data() const
3118 {
3119 return this->shndx_ == OUTPUT_SECTION_CODE;
3120 }
3121
a445fddf
ILT
3122 // Return the object for an input section.
3123 Relobj*
0439c796 3124 relobj() const;
a445fddf
ILT
3125
3126 // Return the input section index for an input section.
3127 unsigned int
0439c796 3128 shndx() const;
a445fddf 3129
20e6d0d6
DK
3130 // For non-input-sections, return the associated Output_section_data
3131 // object.
3132 Output_section_data*
3133 output_section_data() const
3134 {
3135 gold_assert(!this->is_input_section());
3136 return this->u2_.posd;
3137 }
3138
0439c796
DK
3139 // For a merge section, return the Output_merge_base pointer.
3140 Output_merge_base*
3141 output_merge_base() const
3142 {
3143 gold_assert(this->is_merge_section());
3144 return this->u2_.pomb;
3145 }
3146
20e6d0d6
DK
3147 // Return the Output_relaxed_input_section object.
3148 Output_relaxed_input_section*
3149 relaxed_input_section() const
3150 {
3151 gold_assert(this->is_relaxed_input_section());
3152 return this->u2_.poris;
3153 }
3154
b8e6aad9
ILT
3155 // Set the output section.
3156 void
3157 set_output_section(Output_section* os)
3158 {
3159 gold_assert(!this->is_input_section());
ca09d69a 3160 Output_section_data* posd =
20e6d0d6
DK
3161 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
3162 posd->set_output_section(os);
b8e6aad9
ILT
3163 }
3164
ead1e424 3165 // Set the address and file offset. This is called during
96803768
ILT
3166 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
3167 // the enclosing section.
ead1e424 3168 void
96803768
ILT
3169 set_address_and_file_offset(uint64_t address, off_t file_offset,
3170 off_t section_file_offset);
ead1e424 3171
a445fddf
ILT
3172 // Reset the address and file offset.
3173 void
3174 reset_address_and_file_offset();
3175
96803768
ILT
3176 // Finalize the data size.
3177 void
3178 finalize_data_size();
9a0910c3 3179
b8e6aad9
ILT
3180 // Add an input section, for SHF_MERGE sections.
3181 bool
2ea97941 3182 add_input_section(Relobj* object, unsigned int shndx)
b8e6aad9
ILT
3183 {
3184 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
3185 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2ea97941 3186 return this->u2_.posd->add_input_section(object, shndx);
b8e6aad9
ILT
3187 }
3188
3189 // Given an input OBJECT, an input section index SHNDX within that
3190 // object, and an OFFSET relative to the start of that input
730cdc88 3191 // section, return whether or not the output offset is known. If
1e983657
ILT
3192 // this function returns true, it sets *POUTPUT to the offset in
3193 // the output section, relative to the start of the input section
3194 // in the output section. *POUTPUT may be different from OFFSET
3195 // for a merged section.
b8e6aad9 3196 bool
8383303e
ILT
3197 output_offset(const Relobj* object, unsigned int shndx,
3198 section_offset_type offset,
ca09d69a 3199 section_offset_type* poutput) const;
b8e6aad9 3200
a9a60db6
ILT
3201 // Return whether this is the merge section for the input section
3202 // SHNDX in OBJECT.
3203 bool
3204 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
3205
ead1e424
ILT
3206 // Write out the data. This does nothing for an input section.
3207 void
3208 write(Output_file*);
3209
96803768
ILT
3210 // Write the data to a buffer. This does nothing for an input
3211 // section.
3212 void
3213 write_to_buffer(unsigned char*);
3214
7d9e3d98
ILT
3215 // Print to a map file.
3216 void
3217 print_to_mapfile(Mapfile*) const;
3218
38c5e8b4
ILT
3219 // Print statistics about merge sections to stderr.
3220 void
3221 print_merge_stats(const char* section_name)
3222 {
3223 if (this->shndx_ == MERGE_DATA_SECTION_CODE
3224 || this->shndx_ == MERGE_STRING_SECTION_CODE)
3225 this->u2_.posd->print_merge_stats(section_name);
3226 }
3227
ead1e424 3228 private:
b8e6aad9
ILT
3229 // Code values which appear in shndx_. If the value is not one of
3230 // these codes, it is the input section index in the object file.
3231 enum
3232 {
3233 // An Output_section_data.
3234 OUTPUT_SECTION_CODE = -1U,
3235 // An Output_section_data for an SHF_MERGE section with
3236 // SHF_STRINGS not set.
3237 MERGE_DATA_SECTION_CODE = -2U,
3238 // An Output_section_data for an SHF_MERGE section with
3239 // SHF_STRINGS set.
20e6d0d6
DK
3240 MERGE_STRING_SECTION_CODE = -3U,
3241 // An Output_section_data for a relaxed input section.
3242 RELAXED_INPUT_SECTION_CODE = -4U
b8e6aad9
ILT
3243 };
3244
b8e6aad9
ILT
3245 // For an ordinary input section, this is the section index in the
3246 // input file. For an Output_section_data, this is
3247 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3248 // MERGE_STRING_SECTION_CODE.
ead1e424
ILT
3249 unsigned int shndx_;
3250 // The required alignment, stored as a power of 2.
3251 unsigned int p2align_;
ead1e424
ILT
3252 union
3253 {
b8e6aad9
ILT
3254 // For an ordinary input section, the section size.
3255 off_t data_size;
20e6d0d6
DK
3256 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
3257 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
b8e6aad9
ILT
3258 // entity size.
3259 uint64_t entsize;
3260 } u1_;
3261 union
3262 {
3263 // For an ordinary input section, the object which holds the
ead1e424 3264 // input section.
f6ce93d6 3265 Relobj* object;
b8e6aad9
ILT
3266 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
3267 // MERGE_STRING_SECTION_CODE, the data.
ead1e424 3268 Output_section_data* posd;
0439c796 3269 Output_merge_base* pomb;
20e6d0d6
DK
3270 // For RELAXED_INPUT_SECTION_CODE, the data.
3271 Output_relaxed_input_section* poris;
b8e6aad9 3272 } u2_;
6e9ba2ca
ST
3273 // The line number of the pattern it matches in the --section-ordering-file
3274 // file. It is 0 if does not match any pattern.
3275 unsigned int section_order_index_;
ead1e424
ILT
3276 };
3277
6625d24e
DK
3278 // Store the list of input sections for this Output_section into the
3279 // list passed in. This removes the input sections, leaving only
3280 // any Output_section_data elements. This returns the size of those
3281 // Output_section_data elements. ADDRESS is the address of this
3282 // output section. FILL is the fill value to use, in case there are
3283 // any spaces between the remaining Output_section_data elements.
3284 uint64_t
3285 get_input_sections(uint64_t address, const std::string& fill,
3286 std::list<Input_section>*);
3287
3288 // Add a script input section. A script input section can either be
3289 // a plain input section or a sub-class of Output_section_data.
3290 void
3291 add_script_input_section(const Input_section& input_section);
3292
3293 // Set the current size of the output section.
3294 void
3295 set_current_data_size(off_t size)
3296 { this->set_current_data_size_for_child(size); }
3297
6625d24e
DK
3298 // End of linker script support.
3299
3300 // Save states before doing section layout.
3301 // This is used for relaxation.
3302 void
3303 save_states();
3304
3305 // Restore states prior to section layout.
3306 void
3307 restore_states();
3308
3309 // Discard states.
3310 void
3311 discard_states();
3312
3313 // Convert existing input sections to relaxed input sections.
3314 void
3315 convert_input_sections_to_relaxed_sections(
3316 const std::vector<Output_relaxed_input_section*>& sections);
3317
3318 // Find a relaxed input section to an input section in OBJECT
3319 // with index SHNDX. Return NULL if none is found.
3320 const Output_relaxed_input_section*
3321 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3322
3323 // Whether section offsets need adjustment due to relaxation.
3324 bool
3325 section_offsets_need_adjustment() const
3326 { return this->section_offsets_need_adjustment_; }
3327
3328 // Set section_offsets_need_adjustment to be true.
3329 void
3330 set_section_offsets_need_adjustment()
3331 { this->section_offsets_need_adjustment_ = true; }
3332
3333 // Adjust section offsets of input sections in this. This is
3334 // requires if relaxation caused some input sections to change sizes.
3335 void
3336 adjust_section_offsets();
3337
3338 // Whether this is a NOLOAD section.
3339 bool
3340 is_noload() const
3341 { return this->is_noload_; }
3342
3343 // Set NOLOAD flag.
3344 void
3345 set_is_noload()
3346 { this->is_noload_ = true; }
3347
3348 // Print merge statistics to stderr.
3349 void
3350 print_merge_stats();
3351
cdc29364
CC
3352 // Set a fixed layout for the section. Used for incremental update links.
3353 void
3354 set_fixed_layout(uint64_t sh_addr, off_t sh_offset, off_t sh_size,
3355 uint64_t sh_addralign);
3356
3357 // Return TRUE if the section has a fixed layout.
3358 bool
3359 has_fixed_layout() const
3360 { return this->has_fixed_layout_; }
3361
3362 // Reserve space within the fixed layout for the section. Used for
3363 // incremental update links.
3364 void
3365 reserve(uint64_t sh_offset, uint64_t sh_size);
3366
6625d24e
DK
3367 protected:
3368 // Return the output section--i.e., the object itself.
3369 Output_section*
3370 do_output_section()
3371 { return this; }
3372
3373 const Output_section*
3374 do_output_section() const
3375 { return this; }
3376
3377 // Return the section index in the output file.
3378 unsigned int
3379 do_out_shndx() const
3380 {
3381 gold_assert(this->out_shndx_ != -1U);
3382 return this->out_shndx_;
3383 }
3384
3385 // Set the output section index.
3386 void
3387 do_set_out_shndx(unsigned int shndx)
3388 {
3389 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
3390 this->out_shndx_ = shndx;
3391 }
3392
cdc29364
CC
3393 // Update the data size of the Output_section. For a typical
3394 // Output_section, there is nothing to do, but if there are any
3395 // Output_section_data objects we need to do a trial layout
3396 // here.
3397 virtual void
3398 update_data_size();
3399
6625d24e
DK
3400 // Set the final data size of the Output_section. For a typical
3401 // Output_section, there is nothing to do, but if there are any
3402 // Output_section_data objects we need to set their final addresses
3403 // here.
3404 virtual void
3405 set_final_data_size();
3406
3407 // Reset the address and file offset.
3408 void
3409 do_reset_address_and_file_offset();
3410
3411 // Return true if address and file offset already have reset values. In
3412 // other words, calling reset_address_and_file_offset will not change them.
3413 bool
3414 do_address_and_file_offset_have_reset_values() const;
3415
3416 // Write the data to the file. For a typical Output_section, this
3417 // does nothing: the data is written out by calling Object::Relocate
3418 // on each input object. But if there are any Output_section_data
3419 // objects we do need to write them out here.
3420 virtual void
3421 do_write(Output_file*);
3422
3423 // Return the address alignment--function required by parent class.
3424 uint64_t
3425 do_addralign() const
3426 { return this->addralign_; }
3427
3428 // Return whether there is a load address.
3429 bool
3430 do_has_load_address() const
3431 { return this->has_load_address_; }
3432
3433 // Return the load address.
3434 uint64_t
3435 do_load_address() const
3436 {
3437 gold_assert(this->has_load_address_);
3438 return this->load_address_;
3439 }
3440
3441 // Return whether this is an Output_section.
3442 bool
3443 do_is_section() const
3444 { return true; }
3445
3446 // Return whether this is a section of the specified type.
3447 bool
3448 do_is_section_type(elfcpp::Elf_Word type) const
3449 { return this->type_ == type; }
3450
3451 // Return whether the specified section flag is set.
3452 bool
3453 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
3454 { return (this->flags_ & flag) != 0; }
3455
3456 // Set the TLS offset. Called only for SHT_TLS sections.
3457 void
3458 do_set_tls_offset(uint64_t tls_base);
3459
3460 // Return the TLS offset, relative to the base of the TLS segment.
3461 // Valid only for SHT_TLS sections.
3462 uint64_t
3463 do_tls_offset() const
3464 { return this->tls_offset_; }
3465
3466 // This may be implemented by a child class.
3467 virtual void
3468 do_finalize_name(Layout*)
3469 { }
3470
3471 // Print to the map file.
3472 virtual void
3473 do_print_to_mapfile(Mapfile*) const;
3474
3475 // Record that this section requires postprocessing after all
3476 // relocations have been applied. This is called by a child class.
3477 void
3478 set_requires_postprocessing()
3479 {
3480 this->requires_postprocessing_ = true;
3481 this->after_input_sections_ = true;
3482 }
3483
3484 // Write all the data of an Output_section into the postprocessing
3485 // buffer.
3486 void
3487 write_to_postprocessing_buffer();
3488
ead1e424
ILT
3489 typedef std::vector<Input_section> Input_section_list;
3490
c0a62865
DK
3491 // Allow a child class to access the input sections.
3492 const Input_section_list&
3493 input_sections() const
3494 { return this->input_sections_; }
3495
131687b4
DK
3496 // Whether this always keeps an input section list
3497 bool
3498 always_keeps_input_sections() const
3499 { return this->always_keeps_input_sections_; }
3500
3501 // Always keep an input section list.
3502 void
3503 set_always_keeps_input_sections()
3504 {
3505 gold_assert(this->current_data_size_for_child() == 0);
3506 this->always_keeps_input_sections_ = true;
3507 }
3508
c0a62865 3509 private:
20e6d0d6
DK
3510 // We only save enough information to undo the effects of section layout.
3511 class Checkpoint_output_section
3512 {
3513 public:
2ea97941
ILT
3514 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
3515 const Input_section_list& input_sections,
3516 off_t first_input_offset,
3517 bool attached_input_sections_are_sorted)
3518 : addralign_(addralign), flags_(flags),
3519 input_sections_(input_sections),
20e6d0d6 3520 input_sections_size_(input_sections_.size()),
2ea97941
ILT
3521 input_sections_copy_(), first_input_offset_(first_input_offset),
3522 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
20e6d0d6
DK
3523 { }
3524
3525 virtual
3526 ~Checkpoint_output_section()
3527 { }
3528
3529 // Return the address alignment.
3530 uint64_t
3531 addralign() const
3532 { return this->addralign_; }
3533
3534 // Return the section flags.
3535 elfcpp::Elf_Xword
3536 flags() const
3537 { return this->flags_; }
3538
3539 // Return a reference to the input section list copy.
c0a62865
DK
3540 Input_section_list*
3541 input_sections()
3542 { return &this->input_sections_copy_; }
20e6d0d6
DK
3543
3544 // Return the size of input_sections at the time when checkpoint is
3545 // taken.
3546 size_t
3547 input_sections_size() const
3548 { return this->input_sections_size_; }
3549
3550 // Whether input sections are copied.
3551 bool
3552 input_sections_saved() const
3553 { return this->input_sections_copy_.size() == this->input_sections_size_; }
3554
3555 off_t
3556 first_input_offset() const
3557 { return this->first_input_offset_; }
3558
3559 bool
3560 attached_input_sections_are_sorted() const
3561 { return this->attached_input_sections_are_sorted_; }
3562
3563 // Save input sections.
3564 void
3565 save_input_sections()
3566 {
3567 this->input_sections_copy_.reserve(this->input_sections_size_);
3568 this->input_sections_copy_.clear();
3569 Input_section_list::const_iterator p = this->input_sections_.begin();
3570 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3571 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3572 this->input_sections_copy_.push_back(*p);
3573 }
3574
3575 private:
3576 // The section alignment.
3577 uint64_t addralign_;
3578 // The section flags.
3579 elfcpp::Elf_Xword flags_;
3580 // Reference to the input sections to be checkpointed.
3581 const Input_section_list& input_sections_;
3582 // Size of the checkpointed portion of input_sections_;
3583 size_t input_sections_size_;
3584 // Copy of input sections.
3585 Input_section_list input_sections_copy_;
3586 // The offset of the first entry in input_sections_.
3587 off_t first_input_offset_;
3588 // True if the input sections attached to this output section have
3589 // already been sorted.
3590 bool attached_input_sections_are_sorted_;
3591 };
3592
2fd32231
ILT
3593 // This class is used to sort the input sections.
3594 class Input_section_sort_entry;
3595
2a0ff005 3596 // This is the sort comparison function for ctors and dtors.
2fd32231
ILT
3597 struct Input_section_sort_compare
3598 {
3599 bool
3600 operator()(const Input_section_sort_entry&,
3601 const Input_section_sort_entry&) const;
3602 };
3603
2a0ff005
DK
3604 // This is the sort comparison function for .init_array and .fini_array.
3605 struct Input_section_sort_init_fini_compare
3606 {
3607 bool
3608 operator()(const Input_section_sort_entry&,
3609 const Input_section_sort_entry&) const;
3610 };
3611
6e9ba2ca
ST
3612 // This is the sort comparison function when a section order is specified
3613 // from an input file.
3614 struct Input_section_sort_section_order_index_compare
3615 {
3616 bool
3617 operator()(const Input_section_sort_entry&,
3618 const Input_section_sort_entry&) const;
3619 };
3620
c51e6221 3621 // Fill data. This is used to fill in data between input sections.
a445fddf
ILT
3622 // It is also used for data statements (BYTE, WORD, etc.) in linker
3623 // scripts. When we have to keep track of the input sections, we
3624 // can use an Output_data_const, but we don't want to have to keep
3625 // track of input sections just to implement fills.
c51e6221
ILT
3626 class Fill
3627 {
3628 public:
2ea97941
ILT
3629 Fill(off_t section_offset, off_t length)
3630 : section_offset_(section_offset),
3631 length_(convert_to_section_size_type(length))
c51e6221
ILT
3632 { }
3633
3634 // Return section offset.
3635 off_t
3636 section_offset() const
3637 { return this->section_offset_; }
3638
3639 // Return fill length.
a445fddf 3640 section_size_type
c51e6221
ILT
3641 length() const
3642 { return this->length_; }
3643
3644 private:
3645 // The offset within the output section.
3646 off_t section_offset_;
3647 // The length of the space to fill.
a445fddf 3648 section_size_type length_;
c51e6221
ILT
3649 };
3650
3651 typedef std::vector<Fill> Fill_list;
3652
c0a62865 3653 // Map used during relaxation of existing sections. This map
5ac169d4
DK
3654 // a section id an input section list index. We assume that
3655 // Input_section_list is a vector.
3656 typedef Unordered_map<Section_id, size_t, Section_id_hash> Relaxation_map;
c0a62865 3657
b8e6aad9
ILT
3658 // Add a new output section by Input_section.
3659 void
3660 add_output_section_data(Input_section*);
3661
3662 // Add an SHF_MERGE input section. Returns true if the section was
0439c796
DK
3663 // handled. If KEEPS_INPUT_SECTIONS is true, the output merge section
3664 // stores information about the merged input sections.
b8e6aad9
ILT
3665 bool
3666 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
0439c796
DK
3667 uint64_t entsize, uint64_t addralign,
3668 bool keeps_input_sections);
b8e6aad9
ILT
3669
3670 // Add an output SHF_MERGE section POSD to this output section.
3671 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3672 // ENTSIZE is the entity size. This returns the entry added to
3673 // input_sections_.
3674 void
3675 add_output_merge_section(Output_section_data* posd, bool is_string,
3676 uint64_t entsize);
3677
2fd32231
ILT
3678 // Sort the attached input sections.
3679 void
3680 sort_attached_input_sections();
3681
c0a62865
DK
3682 // Find the merge section into which an input section with index SHNDX in
3683 // OBJECT has been added. Return NULL if none found.
3684 Output_section_data*
3685 find_merge_section(const Relobj* object, unsigned int shndx) const;
3686
c0a62865
DK
3687 // Build a relaxation map.
3688 void
3689 build_relaxation_map(
3690 const Input_section_list& input_sections,
3691 size_t limit,
3692 Relaxation_map* map) const;
3693
3694 // Convert input sections in an input section list into relaxed sections.
3695 void
3696 convert_input_sections_in_list_to_relaxed_sections(
3697 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3698 const Relaxation_map& map,
3699 Input_section_list* input_sections);
3700
0439c796
DK
3701 // Build the lookup maps for merge and relaxed input sections.
3702 void
3703 build_lookup_maps() const;
3704
a2fb1b05
ILT
3705 // Most of these fields are only valid after layout.
3706
3707 // The name of the section. This will point into a Stringpool.
9a0910c3 3708 const char* name_;
75f65a3e 3709 // The section address is in the parent class.
a2fb1b05
ILT
3710 // The section alignment.
3711 uint64_t addralign_;
3712 // The section entry size.
3713 uint64_t entsize_;
a445fddf
ILT
3714 // The load address. This is only used when using a linker script
3715 // with a SECTIONS clause. The has_load_address_ field indicates
3716 // whether this field is valid.
3717 uint64_t load_address_;
75f65a3e 3718 // The file offset is in the parent class.
16649710 3719 // Set the section link field to the index of this section.
14b31740 3720 const Output_data* link_section_;
16649710 3721 // If link_section_ is NULL, this is the link field.
a2fb1b05 3722 unsigned int link_;
16649710 3723 // Set the section info field to the index of this section.
755ab8af 3724 const Output_section* info_section_;
6a74a719
ILT
3725 // If info_section_ is NULL, set the info field to the symbol table
3726 // index of this symbol.
3727 const Symbol* info_symndx_;
3728 // If info_section_ and info_symndx_ are NULL, this is the section
3729 // info field.
a2fb1b05
ILT
3730 unsigned int info_;
3731 // The section type.
27bc2bce 3732 const elfcpp::Elf_Word type_;
a2fb1b05 3733 // The section flags.
a445fddf 3734 elfcpp::Elf_Xword flags_;
22f0da72
ILT
3735 // The order of this section in the output segment.
3736 Output_section_order order_;
61ba1cf9 3737 // The section index.
ead1e424 3738 unsigned int out_shndx_;
c06b7b0b
ILT
3739 // If there is a STT_SECTION for this output section in the normal
3740 // symbol table, this is the symbol index. This starts out as zero.
3741 // It is initialized in Layout::finalize() to be the index, or -1U
3742 // if there isn't one.
3743 unsigned int symtab_index_;
3744 // If there is a STT_SECTION for this output section in the dynamic
3745 // symbol table, this is the symbol index. This starts out as zero.
3746 // It is initialized in Layout::finalize() to be the index, or -1U
3747 // if there isn't one.
3748 unsigned int dynsym_index_;
ead1e424
ILT
3749 // The input sections. This will be empty in cases where we don't
3750 // need to keep track of them.
3751 Input_section_list input_sections_;
3752 // The offset of the first entry in input_sections_.
3753 off_t first_input_offset_;
c51e6221
ILT
3754 // The fill data. This is separate from input_sections_ because we
3755 // often will need fill sections without needing to keep track of
3756 // input sections.
3757 Fill_list fills_;
96803768
ILT
3758 // If the section requires postprocessing, this buffer holds the
3759 // section contents during relocation.
3760 unsigned char* postprocessing_buffer_;
c06b7b0b
ILT
3761 // Whether this output section needs a STT_SECTION symbol in the
3762 // normal symbol table. This will be true if there is a relocation
3763 // which needs it.
3764 bool needs_symtab_index_ : 1;
3765 // Whether this output section needs a STT_SECTION symbol in the
3766 // dynamic symbol table. This will be true if there is a dynamic
3767 // relocation which needs it.
3768 bool needs_dynsym_index_ : 1;
16649710
ILT
3769 // Whether the link field of this output section should point to the
3770 // normal symbol table.
3771 bool should_link_to_symtab_ : 1;
3772 // Whether the link field of this output section should point to the
3773 // dynamic symbol table.
3774 bool should_link_to_dynsym_ : 1;
730cdc88
ILT
3775 // Whether this section should be written after all the input
3776 // sections are complete.
3777 bool after_input_sections_ : 1;
27bc2bce
ILT
3778 // Whether this section requires post processing after all
3779 // relocations have been applied.
3780 bool requires_postprocessing_ : 1;
a445fddf
ILT
3781 // Whether an input section was mapped to this output section
3782 // because of a SECTIONS clause in a linker script.
3783 bool found_in_sections_clause_ : 1;
3784 // Whether this section has an explicitly specified load address.
3785 bool has_load_address_ : 1;
755ab8af
ILT
3786 // True if the info_section_ field means the section index of the
3787 // section, false if it means the symbol index of the corresponding
3788 // section symbol.
3789 bool info_uses_section_index_ : 1;
6e9ba2ca
ST
3790 // True if input sections attached to this output section have to be
3791 // sorted according to a specified order.
3792 bool input_section_order_specified_ : 1;
2fd32231
ILT
3793 // True if the input sections attached to this output section may
3794 // need sorting.
3795 bool may_sort_attached_input_sections_ : 1;
3796 // True if the input sections attached to this output section must
3797 // be sorted.
3798 bool must_sort_attached_input_sections_ : 1;
3799 // True if the input sections attached to this output section have
3800 // already been sorted.
3801 bool attached_input_sections_are_sorted_ : 1;
9f1d377b
ILT
3802 // True if this section holds relro data.
3803 bool is_relro_ : 1;
8a5e3e08
ILT
3804 // True if this is a small section.
3805 bool is_small_section_ : 1;
3806 // True if this is a large section.
3807 bool is_large_section_ : 1;
f5c870d2
ILT
3808 // Whether code-fills are generated at write.
3809 bool generate_code_fills_at_write_ : 1;
e8cd95c7
ILT
3810 // Whether the entry size field should be zero.
3811 bool is_entsize_zero_ : 1;
8923b24c
DK
3812 // Whether section offsets need adjustment due to relaxation.
3813 bool section_offsets_need_adjustment_ : 1;
1e5d2fb1
DK
3814 // Whether this is a NOLOAD section.
3815 bool is_noload_ : 1;
131687b4
DK
3816 // Whether this always keeps input section.
3817 bool always_keeps_input_sections_ : 1;
cdc29364
CC
3818 // Whether this section has a fixed layout, for incremental update links.
3819 bool has_fixed_layout_ : 1;
7bf1f802
ILT
3820 // For SHT_TLS sections, the offset of this section relative to the base
3821 // of the TLS segment.
3822 uint64_t tls_offset_;
20e6d0d6
DK
3823 // Saved checkpoint.
3824 Checkpoint_output_section* checkpoint_;
0439c796
DK
3825 // Fast lookup maps for merged and relaxed input sections.
3826 Output_section_lookup_maps* lookup_maps_;
cdc29364
CC
3827 // List of available regions within the section, for incremental
3828 // update links.
3829 Free_list free_list_;
a2fb1b05
ILT
3830};
3831
3832// An output segment. PT_LOAD segments are built from collections of
3833// output sections. Other segments typically point within PT_LOAD
3834// segments, and are built directly as needed.
20e6d0d6
DK
3835//
3836// NOTE: We want to use the copy constructor for this class. During
3837// relaxation, we may try built the segments multiple times. We do
3838// that by copying the original segment list before lay-out, doing
3839// a trial lay-out and roll-back to the saved copied if we need to
3840// to the lay-out again.
a2fb1b05
ILT
3841
3842class Output_segment
3843{
3844 public:
3845 // Create an output segment, specifying the type and flags.
3846 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3847
3848 // Return the virtual address.
3849 uint64_t
3850 vaddr() const
3851 { return this->vaddr_; }
3852
3853 // Return the physical address.
3854 uint64_t
3855 paddr() const
3856 { return this->paddr_; }
3857
3858 // Return the segment type.
3859 elfcpp::Elf_Word
3860 type() const
3861 { return this->type_; }
3862
3863 // Return the segment flags.
3864 elfcpp::Elf_Word
3865 flags() const
3866 { return this->flags_; }
3867
92e059d8
ILT
3868 // Return the memory size.
3869 uint64_t
3870 memsz() const
3871 { return this->memsz_; }
3872
ead1e424
ILT
3873 // Return the file size.
3874 off_t
3875 filesz() const
3876 { return this->filesz_; }
3877
516cb3d0
ILT
3878 // Return the file offset.
3879 off_t
3880 offset() const
3881 { return this->offset_; }
3882
8a5e3e08
ILT
3883 // Whether this is a segment created to hold large data sections.
3884 bool
3885 is_large_data_segment() const
3886 { return this->is_large_data_segment_; }
3887
3888 // Record that this is a segment created to hold large data
3889 // sections.
3890 void
3891 set_is_large_data_segment()
3892 { this->is_large_data_segment_ = true; }
3893
75f65a3e
ILT
3894 // Return the maximum alignment of the Output_data.
3895 uint64_t
a445fddf 3896 maximum_alignment();
75f65a3e 3897
22f0da72
ILT
3898 // Add the Output_section OS to this PT_LOAD segment. SEG_FLAGS is
3899 // the segment flags to use.
3900 void
3901 add_output_section_to_load(Layout* layout, Output_section* os,
3902 elfcpp::Elf_Word seg_flags);
3903
3904 // Add the Output_section OS to this non-PT_LOAD segment. SEG_FLAGS
3905 // is the segment flags to use.
a2fb1b05 3906 void
22f0da72
ILT
3907 add_output_section_to_nonload(Output_section* os,
3908 elfcpp::Elf_Word seg_flags);
75f65a3e 3909
1650c4ff
ILT
3910 // Remove an Output_section from this segment. It is an error if it
3911 // is not present.
3912 void
3913 remove_output_section(Output_section* os);
3914
a192ba05
ILT
3915 // Add an Output_data (which need not be an Output_section) to the
3916 // start of this segment.
75f65a3e
ILT
3917 void
3918 add_initial_output_data(Output_data*);
3919
756ac4a8
ILT
3920 // Return true if this segment has any sections which hold actual
3921 // data, rather than being a BSS section.
3922 bool
22f0da72 3923 has_any_data_sections() const;
756ac4a8 3924
22f0da72
ILT
3925 // Whether this segment has a dynamic relocs.
3926 bool
3927 has_dynamic_reloc() const;
4f4c5f80 3928
a445fddf
ILT
3929 // Return the address of the first section.
3930 uint64_t
3931 first_section_load_address() const;
3932
3933 // Return whether the addresses have been set already.
3934 bool
3935 are_addresses_set() const
3936 { return this->are_addresses_set_; }
3937
3938 // Set the addresses.
3939 void
2ea97941 3940 set_addresses(uint64_t vaddr, uint64_t paddr)
a445fddf 3941 {
2ea97941
ILT
3942 this->vaddr_ = vaddr;
3943 this->paddr_ = paddr;
a445fddf
ILT
3944 this->are_addresses_set_ = true;
3945 }
3946
a192ba05
ILT
3947 // Update the flags for the flags of an output section added to this
3948 // segment.
3949 void
3950 update_flags_for_output_section(elfcpp::Elf_Xword flags)
3951 {
3952 // The ELF ABI specifies that a PT_TLS segment should always have
3953 // PF_R as the flags.
3954 if (this->type() != elfcpp::PT_TLS)
3955 this->flags_ |= flags;
3956 }
3957
1c4f3631
ILT
3958 // Set the segment flags. This is only used if we have a PHDRS
3959 // clause which explicitly specifies the flags.
3960 void
2ea97941
ILT
3961 set_flags(elfcpp::Elf_Word flags)
3962 { this->flags_ = flags; }
1c4f3631 3963
75f65a3e 3964 // Set the address of the segment to ADDR and the offset to *POFF
a445fddf
ILT
3965 // and set the addresses and offsets of all contained output
3966 // sections accordingly. Set the section indexes of all contained
3967 // output sections starting with *PSHNDX. If RESET is true, first
3968 // reset the addresses of the contained sections. Return the
3969 // address of the immediately following segment. Update *POFF and
3970 // *PSHNDX. This should only be called for a PT_LOAD segment.
75f65a3e 3971 uint64_t
cdc29364 3972 set_section_addresses(Layout*, bool reset, uint64_t addr,
fd064a5b 3973 unsigned int* increase_relro, bool* has_relro,
fc497986 3974 off_t* poff, unsigned int* pshndx);
75f65a3e 3975
0496d5e5
ILT
3976 // Set the minimum alignment of this segment. This may be adjusted
3977 // upward based on the section alignments.
3978 void
a445fddf 3979 set_minimum_p_align(uint64_t align)
f6973bdc
ILT
3980 {
3981 if (align > this->min_p_align_)
3982 this->min_p_align_ = align;
3983 }
0496d5e5 3984
75f65a3e
ILT
3985 // Set the offset of this segment based on the section. This should
3986 // only be called for a non-PT_LOAD segment.
3987 void
1a2dff53 3988 set_offset(unsigned int increase);
75f65a3e 3989
7bf1f802
ILT
3990 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3991 void
3992 set_tls_offsets();
3993
75f65a3e
ILT
3994 // Return the number of output sections.
3995 unsigned int
3996 output_section_count() const;
a2fb1b05 3997
1c4f3631
ILT
3998 // Return the section attached to the list segment with the lowest
3999 // load address. This is used when handling a PHDRS clause in a
4000 // linker script.
4001 Output_section*
4002 section_with_lowest_load_address() const;
4003
61ba1cf9
ILT
4004 // Write the segment header into *OPHDR.
4005 template<int size, bool big_endian>
4006 void
ead1e424 4007 write_header(elfcpp::Phdr_write<size, big_endian>*);
61ba1cf9
ILT
4008
4009 // Write the section headers of associated sections into V.
4010 template<int size, bool big_endian>
4011 unsigned char*
16649710 4012 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
7d1a9ebb 4013 unsigned int* pshndx) const;
61ba1cf9 4014
7d9e3d98
ILT
4015 // Print the output sections in the map file.
4016 void
4017 print_sections_to_mapfile(Mapfile*) const;
4018
a2fb1b05 4019 private:
22f0da72 4020 typedef std::vector<Output_data*> Output_data_list;
a2fb1b05 4021
ead1e424
ILT
4022 // Find the maximum alignment in an Output_data_list.
4023 static uint64_t
a445fddf 4024 maximum_alignment_list(const Output_data_list*);
ead1e424 4025
9f1d377b
ILT
4026 // Return whether the first data section is a relro section.
4027 bool
4028 is_first_section_relro() const;
4029
75f65a3e
ILT
4030 // Set the section addresses in an Output_data_list.
4031 uint64_t
cdc29364 4032 set_section_list_addresses(Layout*, bool reset, Output_data_list*,
96a2b4e4 4033 uint64_t addr, off_t* poff, unsigned int* pshndx,
1a2dff53 4034 bool* in_tls);
75f65a3e
ILT
4035
4036 // Return the number of Output_sections in an Output_data_list.
4037 unsigned int
4038 output_section_count_list(const Output_data_list*) const;
4039
22f0da72
ILT
4040 // Return whether an Output_data_list has a dynamic reloc.
4041 bool
4042 has_dynamic_reloc_list(const Output_data_list*) const;
4f4c5f80 4043
1c4f3631
ILT
4044 // Find the section with the lowest load address in an
4045 // Output_data_list.
4046 void
4047 lowest_load_address_in_list(const Output_data_list* pdl,
4048 Output_section** found,
4049 uint64_t* found_lma) const;
4050
5f1ab67a
ILT
4051 // Find the first and last entries by address.
4052 void
4053 find_first_and_last_list(const Output_data_list* pdl,
4054 const Output_data** pfirst,
4055 const Output_data** plast) const;
4056
61ba1cf9
ILT
4057 // Write the section headers in the list into V.
4058 template<int size, bool big_endian>
4059 unsigned char*
16649710
ILT
4060 write_section_headers_list(const Layout*, const Stringpool*,
4061 const Output_data_list*, unsigned char* v,
7d1a9ebb 4062 unsigned int* pshdx) const;
61ba1cf9 4063
7d9e3d98
ILT
4064 // Print a section list to the mapfile.
4065 void
4066 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
4067
20e6d0d6
DK
4068 // NOTE: We want to use the copy constructor. Currently, shallow copy
4069 // works for us so we do not need to write our own copy constructor.
4070
22f0da72
ILT
4071 // The list of output data attached to this segment.
4072 Output_data_list output_lists_[ORDER_MAX];
a2fb1b05
ILT
4073 // The segment virtual address.
4074 uint64_t vaddr_;
4075 // The segment physical address.
4076 uint64_t paddr_;
4077 // The size of the segment in memory.
4078 uint64_t memsz_;
a445fddf
ILT
4079 // The maximum section alignment. The is_max_align_known_ field
4080 // indicates whether this has been finalized.
4081 uint64_t max_align_;
4082 // The required minimum value for the p_align field. This is used
4083 // for PT_LOAD segments. Note that this does not mean that
4084 // addresses should be aligned to this value; it means the p_paddr
4085 // and p_vaddr fields must be congruent modulo this value. For
4086 // non-PT_LOAD segments, the dynamic linker works more efficiently
4087 // if the p_align field has the more conventional value, although it
4088 // can align as needed.
4089 uint64_t min_p_align_;
a2fb1b05
ILT
4090 // The offset of the segment data within the file.
4091 off_t offset_;
4092 // The size of the segment data in the file.
4093 off_t filesz_;
4094 // The segment type;
4095 elfcpp::Elf_Word type_;
4096 // The segment flags.
4097 elfcpp::Elf_Word flags_;
a445fddf
ILT
4098 // Whether we have finalized max_align_.
4099 bool is_max_align_known_ : 1;
4100 // Whether vaddr and paddr were set by a linker script.
4101 bool are_addresses_set_ : 1;
8a5e3e08
ILT
4102 // Whether this segment holds large data sections.
4103 bool is_large_data_segment_ : 1;
a2fb1b05
ILT
4104};
4105
61ba1cf9 4106// This class represents the output file.
a2fb1b05
ILT
4107
4108class Output_file
4109{
4110 public:
14144f39 4111 Output_file(const char* name);
61ba1cf9 4112
516cb3d0
ILT
4113 // Indicate that this is a temporary file which should not be
4114 // output.
4115 void
4116 set_is_temporary()
4117 { this->is_temporary_ = true; }
4118
404c2abb
ILT
4119 // Try to open an existing file. Returns false if the file doesn't
4120 // exist, has a size of 0 or can't be mmaped. This method is
4121 // thread-unsafe.
4122 bool
4123 open_for_modification();
4124
61ba1cf9 4125 // Open the output file. FILE_SIZE is the final size of the file.
404c2abb
ILT
4126 // If the file already exists, it is deleted/truncated. This method
4127 // is thread-unsafe.
61ba1cf9
ILT
4128 void
4129 open(off_t file_size);
4130
404c2abb 4131 // Resize the output file. This method is thread-unsafe.
27bc2bce
ILT
4132 void
4133 resize(off_t file_size);
4134
c420411f 4135 // Close the output file (flushing all buffered data) and make sure
404c2abb 4136 // there are no errors. This method is thread-unsafe.
61ba1cf9
ILT
4137 void
4138 close();
4139
c549a694
ILT
4140 // Return the size of this file.
4141 off_t
4142 filesize()
4143 { return this->file_size_; }
4144
3aec4f9c
RÁE
4145 // Return the name of this file.
4146 const char*
4147 filename()
4148 { return this->name_; }
4149
61ba1cf9
ILT
4150 // We currently always use mmap which makes the view handling quite
4151 // simple. In the future we may support other approaches.
a2fb1b05
ILT
4152
4153 // Write data to the output file.
4154 void
fe8718a4 4155 write(off_t offset, const void* data, size_t len)
61ba1cf9
ILT
4156 { memcpy(this->base_ + offset, data, len); }
4157
4158 // Get a buffer to use to write to the file, given the offset into
4159 // the file and the size.
4160 unsigned char*
fe8718a4 4161 get_output_view(off_t start, size_t size)
61ba1cf9 4162 {
8d32f935
ILT
4163 gold_assert(start >= 0
4164 && start + static_cast<off_t>(size) <= this->file_size_);
61ba1cf9
ILT
4165 return this->base_ + start;
4166 }
4167
4168 // VIEW must have been returned by get_output_view. Write the
4169 // buffer to the file, passing in the offset and the size.
4170 void
fe8718a4 4171 write_output_view(off_t, size_t, unsigned char*)
61ba1cf9
ILT
4172 { }
4173
730cdc88
ILT
4174 // Get a read/write buffer. This is used when we want to write part
4175 // of the file, read it in, and write it again.
4176 unsigned char*
fe8718a4 4177 get_input_output_view(off_t start, size_t size)
730cdc88
ILT
4178 { return this->get_output_view(start, size); }
4179
4180 // Write a read/write buffer back to the file.
4181 void
fe8718a4 4182 write_input_output_view(off_t, size_t, unsigned char*)
730cdc88
ILT
4183 { }
4184
4185 // Get a read buffer. This is used when we just want to read part
4186 // of the file back it in.
4187 const unsigned char*
fe8718a4 4188 get_input_view(off_t start, size_t size)
730cdc88
ILT
4189 { return this->get_output_view(start, size); }
4190
4191 // Release a read bfufer.
4192 void
fe8718a4 4193 free_input_view(off_t, size_t, const unsigned char*)
730cdc88
ILT
4194 { }
4195
61ba1cf9 4196 private:
404c2abb
ILT
4197 // Map the file into memory or, if that fails, allocate anonymous
4198 // memory.
27bc2bce
ILT
4199 void
4200 map();
4201
26736d8e 4202 // Allocate anonymous memory for the file.
404c2abb 4203 bool
26736d8e
ILT
4204 map_anonymous();
4205
404c2abb
ILT
4206 // Map the file into memory.
4207 bool
4208 map_no_anonymous();
4209
c420411f
ILT
4210 // Unmap the file from memory (and flush to disk buffers).
4211 void
4212 unmap();
4213
61ba1cf9
ILT
4214 // File name.
4215 const char* name_;
4216 // File descriptor.
4217 int o_;
4218 // File size.
4219 off_t file_size_;
4220 // Base of file mapped into memory.
4221 unsigned char* base_;
c420411f
ILT
4222 // True iff base_ points to a memory buffer rather than an output file.
4223 bool map_is_anonymous_;
88597d34
ILT
4224 // True if base_ was allocated using new rather than mmap.
4225 bool map_is_allocated_;
516cb3d0
ILT
4226 // True if this is a temporary file which should not be output.
4227 bool is_temporary_;
a2fb1b05
ILT
4228};
4229
4230} // End namespace gold.
4231
4232#endif // !defined(GOLD_OUTPUT_H)
This page took 0.45138 seconds and 4 git commands to generate.