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