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