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