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