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