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