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