daily update
[deliverable/binutils-gdb.git] / gold / output.h
CommitLineData
a2fb1b05
ILT
1// output.h -- manage the output file for gold -*- C++ -*-
2
e29e076a 3// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
a2fb1b05
ILT
23#ifndef GOLD_OUTPUT_H
24#define GOLD_OUTPUT_H
25
26#include <list>
ead1e424 27#include <vector>
a2fb1b05
ILT
28
29#include "elfcpp.h"
7d9e3d98 30#include "mapfile.h"
54dc6425 31#include "layout.h"
c06b7b0b 32#include "reloc-types.h"
a2fb1b05
ILT
33
34namespace gold
35{
36
61ba1cf9 37class General_options;
a2fb1b05 38class Object;
a3ad94ed 39class Symbol;
a2fb1b05 40class Output_file;
c0a62865 41class Output_merge_base;
c06b7b0b 42class Output_section;
6a74a719 43class Relocatable_relocs;
a3ad94ed 44class Target;
54dc6425
ILT
45template<int size, bool big_endian>
46class Sized_target;
c06b7b0b
ILT
47template<int size, bool big_endian>
48class Sized_relobj;
54dc6425 49
c0a62865
DK
50// This class specifies an input section. It is used as a key type
51// for maps.
52
53class Input_section_specifier
54{
55 public:
56 Input_section_specifier(const Relobj* relobj, unsigned int shndx)
57 : relobj_(relobj), shndx_(shndx)
58 { }
59
60 // Return Relobj of this.
61 const Relobj*
62 relobj() const
63 { return this->relobj_; }
64
65 // Return section index of this.
66 unsigned int
67 shndx() const
68 { return this->shndx_; }
69
70 // Whether this equals to another specifier ISS.
71 bool
72 eq(const Input_section_specifier& iss) const
73 { return this->relobj_ == iss.relobj_ && this->shndx_ == iss.shndx_; }
74
75 // Compute a hash value of this.
76 size_t
77 hash_value() const
78 { return this->string_hash(this->relobj_->name().c_str()) ^ this->shndx_; }
79
80 // Functors for containers.
81 struct equal_to
82 {
83 bool
84 operator()(const Input_section_specifier& iss1,
85 const Input_section_specifier& iss2) const
86 { return iss1.eq(iss2); }
87 };
88
89 struct hash
90 {
91 size_t
92 operator()(const Input_section_specifier& iss) const
93 { return iss.hash_value(); }
94 };
95
96 private:
97 // For portability, we use our own string hash function instead of assuming
98 // __gnu_cxx::hash or std::tr1::hash is available. This is the same hash
99 // function used in Stringpool_template::string_hash.
100 static size_t
101 string_hash(const char* s)
102 {
103 size_t h = 5381;
104 while (*s != '\0')
105 h = h * 33 + *s++;
106 return h;
107 }
108
109 // An object.
110 const Relobj* relobj_;
111 // A section index.
112 unsigned int shndx_;
113};
114
54dc6425 115// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
116
117class Output_data
118{
119 public:
27bc2bce
ILT
120 explicit Output_data()
121 : address_(0), data_size_(0), offset_(-1),
122 is_address_valid_(false), is_data_size_valid_(false),
20e6d0d6 123 is_offset_valid_(false), is_data_size_fixed_(false),
4f4c5f80 124 dynamic_reloc_count_(0)
a2fb1b05
ILT
125 { }
126
127 virtual
128 ~Output_data();
129
27bc2bce
ILT
130 // Return the address. For allocated sections, this is only valid
131 // after Layout::finalize is finished.
75f65a3e
ILT
132 uint64_t
133 address() const
27bc2bce
ILT
134 {
135 gold_assert(this->is_address_valid_);
136 return this->address_;
137 }
75f65a3e 138
27bc2bce
ILT
139 // Return the size of the data. For allocated sections, this must
140 // be valid after Layout::finalize calls set_address, but need not
141 // be valid before then.
a2fb1b05 142 off_t
75f65a3e 143 data_size() const
27bc2bce
ILT
144 {
145 gold_assert(this->is_data_size_valid_);
146 return this->data_size_;
147 }
75f65a3e 148
20e6d0d6
DK
149 // Return true if data size is fixed.
150 bool
151 is_data_size_fixed() const
152 { return this->is_data_size_fixed_; }
153
ead1e424 154 // Return the file offset. This is only valid after
27bc2bce
ILT
155 // Layout::finalize is finished. For some non-allocated sections,
156 // it may not be valid until near the end of the link.
75f65a3e
ILT
157 off_t
158 offset() const
27bc2bce
ILT
159 {
160 gold_assert(this->is_offset_valid_);
161 return this->offset_;
162 }
75f65a3e 163
a445fddf
ILT
164 // Reset the address and file offset. This essentially disables the
165 // sanity testing about duplicate and unknown settings.
166 void
167 reset_address_and_file_offset()
168 {
169 this->is_address_valid_ = false;
170 this->is_offset_valid_ = false;
20e6d0d6
DK
171 if (!this->is_data_size_fixed_)
172 this->is_data_size_valid_ = false;
a445fddf
ILT
173 this->do_reset_address_and_file_offset();
174 }
175
20e6d0d6
DK
176 // Return true if address and file offset already have reset values. In
177 // other words, calling reset_address_and_file_offset will not change them.
178 bool
179 address_and_file_offset_have_reset_values() const
180 { return this->do_address_and_file_offset_have_reset_values(); }
181
75f65a3e
ILT
182 // Return the required alignment.
183 uint64_t
184 addralign() const
185 { return this->do_addralign(); }
186
a445fddf
ILT
187 // Return whether this has a load address.
188 bool
189 has_load_address() const
190 { return this->do_has_load_address(); }
191
192 // Return the load address.
193 uint64_t
194 load_address() const
195 { return this->do_load_address(); }
196
75f65a3e
ILT
197 // Return whether this is an Output_section.
198 bool
199 is_section() const
200 { return this->do_is_section(); }
201
202 // Return whether this is an Output_section of the specified type.
203 bool
204 is_section_type(elfcpp::Elf_Word stt) const
205 { return this->do_is_section_type(stt); }
206
207 // Return whether this is an Output_section with the specified flag
208 // set.
209 bool
210 is_section_flag_set(elfcpp::Elf_Xword shf) const
211 { return this->do_is_section_flag_set(shf); }
212
77e65537
ILT
213 // Return the output section that this goes in, if there is one.
214 Output_section*
215 output_section()
216 { return this->do_output_section(); }
217
ead1e424
ILT
218 // Return the output section index, if there is an output section.
219 unsigned int
220 out_shndx() const
221 { return this->do_out_shndx(); }
222
223 // Set the output section index, if this is an output section.
224 void
225 set_out_shndx(unsigned int shndx)
226 { this->do_set_out_shndx(shndx); }
227
27bc2bce
ILT
228 // Set the address and file offset of this data, and finalize the
229 // size of the data. This is called during Layout::finalize for
230 // allocated sections.
75f65a3e 231 void
27bc2bce
ILT
232 set_address_and_file_offset(uint64_t addr, off_t off)
233 {
234 this->set_address(addr);
235 this->set_file_offset(off);
236 this->finalize_data_size();
237 }
238
239 // Set the address.
240 void
241 set_address(uint64_t addr)
242 {
243 gold_assert(!this->is_address_valid_);
244 this->address_ = addr;
245 this->is_address_valid_ = true;
246 }
247
248 // Set the file offset.
249 void
250 set_file_offset(off_t off)
251 {
252 gold_assert(!this->is_offset_valid_);
253 this->offset_ = off;
254 this->is_offset_valid_ = true;
255 }
256
257 // Finalize the data size.
258 void
259 finalize_data_size()
260 {
261 if (!this->is_data_size_valid_)
262 {
263 // Tell the child class to set the data size.
264 this->set_final_data_size();
265 gold_assert(this->is_data_size_valid_);
266 }
267 }
75f65a3e 268
7bf1f802
ILT
269 // Set the TLS offset. Called only for SHT_TLS sections.
270 void
271 set_tls_offset(uint64_t tls_base)
272 { this->do_set_tls_offset(tls_base); }
273
274 // Return the TLS offset, relative to the base of the TLS segment.
275 // Valid only for SHT_TLS sections.
276 uint64_t
277 tls_offset() const
278 { return this->do_tls_offset(); }
279
ead1e424
ILT
280 // Write the data to the output file. This is called after
281 // Layout::finalize is complete.
75f65a3e
ILT
282 void
283 write(Output_file* file)
284 { this->do_write(file); }
a2fb1b05 285
27bc2bce
ILT
286 // This is called by Layout::finalize to note that the sizes of
287 // allocated sections must now be fixed.
a3ad94ed
ILT
288 static void
289 layout_complete()
27bc2bce 290 { Output_data::allocated_sizes_are_fixed = true; }
a3ad94ed 291
730cdc88
ILT
292 // Used to check that layout has been done.
293 static bool
294 is_layout_complete()
27bc2bce 295 { return Output_data::allocated_sizes_are_fixed; }
730cdc88 296
4f4c5f80
ILT
297 // Count the number of dynamic relocations applied to this section.
298 void
299 add_dynamic_reloc()
300 { ++this->dynamic_reloc_count_; }
301
302 // Return the number of dynamic relocations applied to this section.
303 unsigned int
304 dynamic_reloc_count() const
305 { return this->dynamic_reloc_count_; }
306
a9a60db6
ILT
307 // Whether the address is valid.
308 bool
309 is_address_valid() const
310 { return this->is_address_valid_; }
311
312 // Whether the file offset is valid.
313 bool
314 is_offset_valid() const
315 { return this->is_offset_valid_; }
316
317 // Whether the data size is valid.
318 bool
319 is_data_size_valid() const
320 { return this->is_data_size_valid_; }
321
7d9e3d98
ILT
322 // Print information to the map file.
323 void
324 print_to_mapfile(Mapfile* mapfile) const
325 { return this->do_print_to_mapfile(mapfile); }
326
75f65a3e
ILT
327 protected:
328 // Functions that child classes may or in some cases must implement.
329
330 // Write the data to the output file.
a2fb1b05 331 virtual void
75f65a3e
ILT
332 do_write(Output_file*) = 0;
333
334 // Return the required alignment.
335 virtual uint64_t
336 do_addralign() const = 0;
337
a445fddf
ILT
338 // Return whether this has a load address.
339 virtual bool
340 do_has_load_address() const
341 { return false; }
342
343 // Return the load address.
344 virtual uint64_t
345 do_load_address() const
346 { gold_unreachable(); }
347
75f65a3e
ILT
348 // Return whether this is an Output_section.
349 virtual bool
350 do_is_section() const
351 { return false; }
a2fb1b05 352
54dc6425 353 // Return whether this is an Output_section of the specified type.
75f65a3e 354 // This only needs to be implement by Output_section.
54dc6425 355 virtual bool
75f65a3e 356 do_is_section_type(elfcpp::Elf_Word) const
54dc6425
ILT
357 { return false; }
358
75f65a3e
ILT
359 // Return whether this is an Output_section with the specific flag
360 // set. This only needs to be implemented by Output_section.
54dc6425 361 virtual bool
75f65a3e 362 do_is_section_flag_set(elfcpp::Elf_Xword) const
54dc6425
ILT
363 { return false; }
364
77e65537
ILT
365 // Return the output section, if there is one.
366 virtual Output_section*
367 do_output_section()
368 { return NULL; }
369
ead1e424
ILT
370 // Return the output section index, if there is an output section.
371 virtual unsigned int
372 do_out_shndx() const
a3ad94ed 373 { gold_unreachable(); }
ead1e424
ILT
374
375 // Set the output section index, if this is an output section.
376 virtual void
377 do_set_out_shndx(unsigned int)
a3ad94ed 378 { gold_unreachable(); }
ead1e424 379
27bc2bce
ILT
380 // This is a hook for derived classes to set the data size. This is
381 // called by finalize_data_size, normally called during
382 // Layout::finalize, when the section address is set.
75f65a3e 383 virtual void
27bc2bce
ILT
384 set_final_data_size()
385 { gold_unreachable(); }
75f65a3e 386
a445fddf
ILT
387 // A hook for resetting the address and file offset.
388 virtual void
389 do_reset_address_and_file_offset()
390 { }
391
20e6d0d6
DK
392 // Return true if address and file offset already have reset values. In
393 // other words, calling reset_address_and_file_offset will not change them.
394 // A child class overriding do_reset_address_and_file_offset may need to
395 // also override this.
396 virtual bool
397 do_address_and_file_offset_have_reset_values() const
398 { return !this->is_address_valid_ && !this->is_offset_valid_; }
399
7bf1f802
ILT
400 // Set the TLS offset. Called only for SHT_TLS sections.
401 virtual void
402 do_set_tls_offset(uint64_t)
403 { gold_unreachable(); }
404
405 // Return the TLS offset, relative to the base of the TLS segment.
406 // Valid only for SHT_TLS sections.
407 virtual uint64_t
408 do_tls_offset() const
409 { gold_unreachable(); }
410
7d9e3d98
ILT
411 // Print to the map file. This only needs to be implemented by
412 // classes which may appear in a PT_LOAD segment.
413 virtual void
414 do_print_to_mapfile(Mapfile*) const
415 { gold_unreachable(); }
416
75f65a3e
ILT
417 // Functions that child classes may call.
418
9c547ec3
ILT
419 // Reset the address. The Output_section class needs this when an
420 // SHF_ALLOC input section is added to an output section which was
421 // formerly not SHF_ALLOC.
422 void
423 mark_address_invalid()
424 { this->is_address_valid_ = false; }
425
a2fb1b05
ILT
426 // Set the size of the data.
427 void
75f65a3e 428 set_data_size(off_t data_size)
a3ad94ed 429 {
20e6d0d6
DK
430 gold_assert(!this->is_data_size_valid_
431 && !this->is_data_size_fixed_);
27bc2bce
ILT
432 this->data_size_ = data_size;
433 this->is_data_size_valid_ = true;
434 }
435
20e6d0d6
DK
436 // Fix the data size. Once it is fixed, it cannot be changed
437 // and the data size remains always valid.
438 void
439 fix_data_size()
440 {
441 gold_assert(this->is_data_size_valid_);
442 this->is_data_size_fixed_ = true;
443 }
444
27bc2bce
ILT
445 // Get the current data size--this is for the convenience of
446 // sections which build up their size over time.
447 off_t
448 current_data_size_for_child() const
449 { return this->data_size_; }
450
451 // Set the current data size--this is for the convenience of
452 // sections which build up their size over time.
453 void
454 set_current_data_size_for_child(off_t data_size)
455 {
456 gold_assert(!this->is_data_size_valid_);
a3ad94ed
ILT
457 this->data_size_ = data_size;
458 }
75f65a3e 459
730cdc88
ILT
460 // Return default alignment for the target size.
461 static uint64_t
462 default_alignment();
463
464 // Return default alignment for a specified size--32 or 64.
75f65a3e 465 static uint64_t
730cdc88 466 default_alignment_for_size(int size);
a2fb1b05
ILT
467
468 private:
469 Output_data(const Output_data&);
470 Output_data& operator=(const Output_data&);
471
a3ad94ed 472 // This is used for verification, to make sure that we don't try to
27bc2bce
ILT
473 // change any sizes of allocated sections after we set the section
474 // addresses.
475 static bool allocated_sizes_are_fixed;
a3ad94ed 476
27bc2bce 477 // Memory address in output file.
75f65a3e 478 uint64_t address_;
27bc2bce 479 // Size of data in output file.
75f65a3e 480 off_t data_size_;
27bc2bce 481 // File offset of contents in output file.
75f65a3e 482 off_t offset_;
27bc2bce
ILT
483 // Whether address_ is valid.
484 bool is_address_valid_;
485 // Whether data_size_ is valid.
486 bool is_data_size_valid_;
487 // Whether offset_ is valid.
488 bool is_offset_valid_;
20e6d0d6
DK
489 // Whether data size is fixed.
490 bool is_data_size_fixed_;
4f4c5f80
ILT
491 // Count of dynamic relocations applied to this section.
492 unsigned int dynamic_reloc_count_;
a2fb1b05
ILT
493};
494
54dc6425
ILT
495// Output the section headers.
496
497class Output_section_headers : public Output_data
498{
499 public:
9025d29d 500 Output_section_headers(const Layout*,
16649710
ILT
501 const Layout::Segment_list*,
502 const Layout::Section_list*,
6a74a719 503 const Layout::Section_list*,
d491d34e
ILT
504 const Stringpool*,
505 const Output_section*);
54dc6425 506
27bc2bce 507 protected:
54dc6425
ILT
508 // Write the data to the file.
509 void
75f65a3e
ILT
510 do_write(Output_file*);
511
512 // Return the required alignment.
513 uint64_t
514 do_addralign() const
730cdc88 515 { return Output_data::default_alignment(); }
54dc6425 516
7d9e3d98
ILT
517 // Write to a map file.
518 void
519 do_print_to_mapfile(Mapfile* mapfile) const
520 { mapfile->print_output_data(this, _("** section headers")); }
521
20e6d0d6
DK
522 // Set final data size.
523 void
524 set_final_data_size()
525 { this->set_data_size(this->do_size()); }
526
54dc6425 527 private:
61ba1cf9
ILT
528 // Write the data to the file with the right size and endianness.
529 template<int size, bool big_endian>
530 void
531 do_sized_write(Output_file*);
532
20e6d0d6
DK
533 // Compute data size.
534 off_t
535 do_size() const;
536
16649710
ILT
537 const Layout* layout_;
538 const Layout::Segment_list* segment_list_;
6a74a719 539 const Layout::Section_list* section_list_;
16649710 540 const Layout::Section_list* unattached_section_list_;
61ba1cf9 541 const Stringpool* secnamepool_;
d491d34e 542 const Output_section* shstrtab_section_;
54dc6425
ILT
543};
544
545// Output the segment headers.
546
547class Output_segment_headers : public Output_data
548{
549 public:
9025d29d 550 Output_segment_headers(const Layout::Segment_list& segment_list);
54dc6425 551
27bc2bce 552 protected:
54dc6425
ILT
553 // Write the data to the file.
554 void
75f65a3e
ILT
555 do_write(Output_file*);
556
557 // Return the required alignment.
558 uint64_t
559 do_addralign() const
730cdc88 560 { return Output_data::default_alignment(); }
54dc6425 561
7d9e3d98
ILT
562 // Write to a map file.
563 void
564 do_print_to_mapfile(Mapfile* mapfile) const
565 { mapfile->print_output_data(this, _("** segment headers")); }
566
20e6d0d6
DK
567 // Set final data size.
568 void
569 set_final_data_size()
570 { this->set_data_size(this->do_size()); }
571
54dc6425 572 private:
61ba1cf9
ILT
573 // Write the data to the file with the right size and endianness.
574 template<int size, bool big_endian>
575 void
576 do_sized_write(Output_file*);
577
20e6d0d6
DK
578 // Compute the current size.
579 off_t
580 do_size() const;
581
54dc6425
ILT
582 const Layout::Segment_list& segment_list_;
583};
584
585// Output the ELF file header.
586
587class Output_file_header : public Output_data
588{
589 public:
9025d29d 590 Output_file_header(const Target*,
54dc6425 591 const Symbol_table*,
d391083d
ILT
592 const Output_segment_headers*,
593 const char* entry);
75f65a3e
ILT
594
595 // Add information about the section headers. We lay out the ELF
596 // file header before we create the section headers.
597 void set_section_info(const Output_section_headers*,
598 const Output_section* shstrtab);
54dc6425 599
27bc2bce 600 protected:
54dc6425
ILT
601 // Write the data to the file.
602 void
75f65a3e
ILT
603 do_write(Output_file*);
604
605 // Return the required alignment.
606 uint64_t
607 do_addralign() const
730cdc88 608 { return Output_data::default_alignment(); }
75f65a3e 609
7d9e3d98
ILT
610 // Write to a map file.
611 void
612 do_print_to_mapfile(Mapfile* mapfile) const
613 { mapfile->print_output_data(this, _("** file header")); }
614
20e6d0d6
DK
615 // Set final data size.
616 void
617 set_final_data_size(void)
618 { this->set_data_size(this->do_size()); }
619
54dc6425 620 private:
61ba1cf9
ILT
621 // Write the data to the file with the right size and endianness.
622 template<int size, bool big_endian>
623 void
624 do_sized_write(Output_file*);
625
d391083d
ILT
626 // Return the value to use for the entry address.
627 template<int size>
628 typename elfcpp::Elf_types<size>::Elf_Addr
629 entry();
630
20e6d0d6
DK
631 // Compute the current data size.
632 off_t
633 do_size() const;
634
54dc6425
ILT
635 const Target* target_;
636 const Symbol_table* symtab_;
61ba1cf9 637 const Output_segment_headers* segment_header_;
54dc6425
ILT
638 const Output_section_headers* section_header_;
639 const Output_section* shstrtab_;
d391083d 640 const char* entry_;
54dc6425
ILT
641};
642
ead1e424
ILT
643// Output sections are mainly comprised of input sections. However,
644// there are cases where we have data to write out which is not in an
645// input section. Output_section_data is used in such cases. This is
646// an abstract base class.
647
648class Output_section_data : public Output_data
649{
650 public:
20e6d0d6
DK
651 Output_section_data(off_t data_size, uint64_t addralign,
652 bool is_data_size_fixed)
27bc2bce 653 : Output_data(), output_section_(NULL), addralign_(addralign)
20e6d0d6
DK
654 {
655 this->set_data_size(data_size);
656 if (is_data_size_fixed)
657 this->fix_data_size();
658 }
ead1e424
ILT
659
660 Output_section_data(uint64_t addralign)
27bc2bce 661 : Output_data(), output_section_(NULL), addralign_(addralign)
ead1e424
ILT
662 { }
663
16649710
ILT
664 // Return the output section.
665 const Output_section*
666 output_section() const
667 { return this->output_section_; }
668
ead1e424
ILT
669 // Record the output section.
670 void
16649710 671 set_output_section(Output_section* os);
ead1e424 672
b8e6aad9
ILT
673 // Add an input section, for SHF_MERGE sections. This returns true
674 // if the section was handled.
675 bool
676 add_input_section(Relobj* object, unsigned int shndx)
677 { return this->do_add_input_section(object, shndx); }
678
679 // Given an input OBJECT, an input section index SHNDX within that
680 // object, and an OFFSET relative to the start of that input
730cdc88
ILT
681 // section, return whether or not the corresponding offset within
682 // the output section is known. If this function returns true, it
683 // sets *POUTPUT to the output offset. The value -1 indicates that
684 // this input offset is being discarded.
8f00aeb8 685 bool
8383303e
ILT
686 output_offset(const Relobj* object, unsigned int shndx,
687 section_offset_type offset,
688 section_offset_type *poutput) const
730cdc88 689 { return this->do_output_offset(object, shndx, offset, poutput); }
b8e6aad9 690
a9a60db6
ILT
691 // Return whether this is the merge section for the input section
692 // SHNDX in OBJECT. This should return true when output_offset
693 // would return true for some values of OFFSET.
694 bool
695 is_merge_section_for(const Relobj* object, unsigned int shndx) const
696 { return this->do_is_merge_section_for(object, shndx); }
697
96803768
ILT
698 // Write the contents to a buffer. This is used for sections which
699 // require postprocessing, such as compression.
700 void
701 write_to_buffer(unsigned char* buffer)
702 { this->do_write_to_buffer(buffer); }
703
38c5e8b4
ILT
704 // Print merge stats to stderr. This should only be called for
705 // SHF_MERGE sections.
706 void
707 print_merge_stats(const char* section_name)
708 { this->do_print_merge_stats(section_name); }
709
ead1e424
ILT
710 protected:
711 // The child class must implement do_write.
712
16649710
ILT
713 // The child class may implement specific adjustments to the output
714 // section.
715 virtual void
716 do_adjust_output_section(Output_section*)
717 { }
718
b8e6aad9
ILT
719 // May be implemented by child class. Return true if the section
720 // was handled.
721 virtual bool
722 do_add_input_section(Relobj*, unsigned int)
723 { gold_unreachable(); }
724
730cdc88 725 // The child class may implement output_offset.
b8e6aad9 726 virtual bool
8383303e
ILT
727 do_output_offset(const Relobj*, unsigned int, section_offset_type,
728 section_offset_type*) const
b8e6aad9
ILT
729 { return false; }
730
a9a60db6
ILT
731 // The child class may implement is_merge_section_for.
732 virtual bool
733 do_is_merge_section_for(const Relobj*, unsigned int) const
734 { return false; }
735
96803768
ILT
736 // The child class may implement write_to_buffer. Most child
737 // classes can not appear in a compressed section, and they do not
738 // implement this.
739 virtual void
740 do_write_to_buffer(unsigned char*)
741 { gold_unreachable(); }
742
38c5e8b4
ILT
743 // Print merge statistics.
744 virtual void
745 do_print_merge_stats(const char*)
746 { gold_unreachable(); }
747
ead1e424
ILT
748 // Return the required alignment.
749 uint64_t
750 do_addralign() const
751 { return this->addralign_; }
752
77e65537
ILT
753 // Return the output section.
754 Output_section*
755 do_output_section()
756 { return this->output_section_; }
757
ead1e424
ILT
758 // Return the section index of the output section.
759 unsigned int
760 do_out_shndx() const;
761
5a6f7e2d
ILT
762 // Set the alignment.
763 void
759b1a24 764 set_addralign(uint64_t addralign);
5a6f7e2d 765
ead1e424
ILT
766 private:
767 // The output section for this section.
77e65537 768 Output_section* output_section_;
ead1e424
ILT
769 // The required alignment.
770 uint64_t addralign_;
771};
772
27bc2bce
ILT
773// Some Output_section_data classes build up their data step by step,
774// rather than all at once. This class provides an interface for
775// them.
776
777class Output_section_data_build : public Output_section_data
778{
779 public:
780 Output_section_data_build(uint64_t addralign)
781 : Output_section_data(addralign)
782 { }
783
784 // Get the current data size.
785 off_t
786 current_data_size() const
787 { return this->current_data_size_for_child(); }
788
789 // Set the current data size.
790 void
791 set_current_data_size(off_t data_size)
792 { this->set_current_data_size_for_child(data_size); }
793
794 protected:
795 // Set the final data size.
796 virtual void
797 set_final_data_size()
798 { this->set_data_size(this->current_data_size_for_child()); }
799};
800
dbe717ef
ILT
801// A simple case of Output_data in which we have constant data to
802// output.
ead1e424 803
dbe717ef 804class Output_data_const : public Output_section_data
ead1e424
ILT
805{
806 public:
dbe717ef 807 Output_data_const(const std::string& data, uint64_t addralign)
20e6d0d6 808 : Output_section_data(data.size(), addralign, true), data_(data)
dbe717ef
ILT
809 { }
810
811 Output_data_const(const char* p, off_t len, uint64_t addralign)
20e6d0d6 812 : Output_section_data(len, addralign, true), data_(p, len)
dbe717ef
ILT
813 { }
814
815 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
20e6d0d6 816 : Output_section_data(len, addralign, true),
dbe717ef
ILT
817 data_(reinterpret_cast<const char*>(p), len)
818 { }
819
27bc2bce 820 protected:
a3ad94ed 821 // Write the data to the output file.
dbe717ef 822 void
a3ad94ed 823 do_write(Output_file*);
dbe717ef 824
96803768
ILT
825 // Write the data to a buffer.
826 void
827 do_write_to_buffer(unsigned char* buffer)
828 { memcpy(buffer, this->data_.data(), this->data_.size()); }
829
7d9e3d98
ILT
830 // Write to a map file.
831 void
832 do_print_to_mapfile(Mapfile* mapfile) const
833 { mapfile->print_output_data(this, _("** fill")); }
834
dbe717ef
ILT
835 private:
836 std::string data_;
837};
838
a3ad94ed
ILT
839// Another version of Output_data with constant data, in which the
840// buffer is allocated by the caller.
dbe717ef 841
a3ad94ed 842class Output_data_const_buffer : public Output_section_data
dbe717ef
ILT
843{
844 public:
a3ad94ed 845 Output_data_const_buffer(const unsigned char* p, off_t len,
7d9e3d98 846 uint64_t addralign, const char* map_name)
20e6d0d6 847 : Output_section_data(len, addralign, true),
7d9e3d98 848 p_(p), map_name_(map_name)
a3ad94ed
ILT
849 { }
850
27bc2bce 851 protected:
a3ad94ed
ILT
852 // Write the data the output file.
853 void
854 do_write(Output_file*);
855
96803768
ILT
856 // Write the data to a buffer.
857 void
858 do_write_to_buffer(unsigned char* buffer)
859 { memcpy(buffer, this->p_, this->data_size()); }
860
7d9e3d98
ILT
861 // Write to a map file.
862 void
863 do_print_to_mapfile(Mapfile* mapfile) const
864 { mapfile->print_output_data(this, _(this->map_name_)); }
865
a3ad94ed 866 private:
7d9e3d98 867 // The data to output.
a3ad94ed 868 const unsigned char* p_;
7d9e3d98
ILT
869 // Name to use in a map file. Maps are a rarely used feature, but
870 // the space usage is minor as aren't very many of these objects.
871 const char* map_name_;
a3ad94ed
ILT
872};
873
27bc2bce
ILT
874// A place holder for a fixed amount of data written out via some
875// other mechanism.
a3ad94ed 876
27bc2bce 877class Output_data_fixed_space : public Output_section_data
a3ad94ed
ILT
878{
879 public:
7d9e3d98
ILT
880 Output_data_fixed_space(off_t data_size, uint64_t addralign,
881 const char* map_name)
20e6d0d6 882 : Output_section_data(data_size, addralign, true),
7d9e3d98 883 map_name_(map_name)
a3ad94ed
ILT
884 { }
885
27bc2bce
ILT
886 protected:
887 // Write out the data--the actual data must be written out
888 // elsewhere.
889 void
890 do_write(Output_file*)
ead1e424 891 { }
7d9e3d98
ILT
892
893 // Write to a map file.
894 void
895 do_print_to_mapfile(Mapfile* mapfile) const
896 { mapfile->print_output_data(this, _(this->map_name_)); }
897
898 private:
899 // Name to use in a map file. Maps are a rarely used feature, but
900 // the space usage is minor as aren't very many of these objects.
901 const char* map_name_;
27bc2bce 902};
ead1e424 903
27bc2bce
ILT
904// A place holder for variable sized data written out via some other
905// mechanism.
906
907class Output_data_space : public Output_section_data_build
908{
909 public:
7d9e3d98
ILT
910 explicit Output_data_space(uint64_t addralign, const char* map_name)
911 : Output_section_data_build(addralign),
912 map_name_(map_name)
27bc2bce 913 { }
ead1e424 914
5a6f7e2d
ILT
915 // Set the alignment.
916 void
917 set_space_alignment(uint64_t align)
918 { this->set_addralign(align); }
919
27bc2bce
ILT
920 protected:
921 // Write out the data--the actual data must be written out
922 // elsewhere.
ead1e424
ILT
923 void
924 do_write(Output_file*)
925 { }
7d9e3d98
ILT
926
927 // Write to a map file.
928 void
929 do_print_to_mapfile(Mapfile* mapfile) const
930 { mapfile->print_output_data(this, _(this->map_name_)); }
931
932 private:
933 // Name to use in a map file. Maps are a rarely used feature, but
934 // the space usage is minor as aren't very many of these objects.
935 const char* map_name_;
936};
937
938// Fill fixed space with zeroes. This is just like
939// Output_data_fixed_space, except that the map name is known.
940
941class Output_data_zero_fill : public Output_section_data
942{
943 public:
944 Output_data_zero_fill(off_t data_size, uint64_t addralign)
20e6d0d6 945 : Output_section_data(data_size, addralign, true)
7d9e3d98
ILT
946 { }
947
948 protected:
949 // There is no data to write out.
950 void
951 do_write(Output_file*)
952 { }
953
954 // Write to a map file.
955 void
956 do_print_to_mapfile(Mapfile* mapfile) const
957 { mapfile->print_output_data(this, "** zero fill"); }
ead1e424
ILT
958};
959
a3ad94ed
ILT
960// A string table which goes into an output section.
961
962class Output_data_strtab : public Output_section_data
963{
964 public:
965 Output_data_strtab(Stringpool* strtab)
966 : Output_section_data(1), strtab_(strtab)
967 { }
968
27bc2bce 969 protected:
a3ad94ed
ILT
970 // This is called to set the address and file offset. Here we make
971 // sure that the Stringpool is finalized.
972 void
27bc2bce 973 set_final_data_size();
a3ad94ed
ILT
974
975 // Write out the data.
976 void
977 do_write(Output_file*);
978
96803768
ILT
979 // Write the data to a buffer.
980 void
981 do_write_to_buffer(unsigned char* buffer)
982 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
983
7d9e3d98
ILT
984 // Write to a map file.
985 void
986 do_print_to_mapfile(Mapfile* mapfile) const
987 { mapfile->print_output_data(this, _("** string table")); }
988
a3ad94ed
ILT
989 private:
990 Stringpool* strtab_;
991};
992
c06b7b0b
ILT
993// This POD class is used to represent a single reloc in the output
994// file. This could be a private class within Output_data_reloc, but
995// the templatization is complex enough that I broke it out into a
996// separate class. The class is templatized on either elfcpp::SHT_REL
997// or elfcpp::SHT_RELA, and also on whether this is a dynamic
998// relocation or an ordinary relocation.
999
dceae3c1
ILT
1000// A relocation can be against a global symbol, a local symbol, a
1001// local section symbol, an output section, or the undefined symbol at
1002// index 0. We represent the latter by using a NULL global symbol.
c06b7b0b
ILT
1003
1004template<int sh_type, bool dynamic, int size, bool big_endian>
1005class Output_reloc;
1006
1007template<bool dynamic, int size, bool big_endian>
1008class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1009{
1010 public:
1011 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
624f8810 1012 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
c06b7b0b 1013
eff45813
CC
1014 static const Address invalid_address = static_cast<Address>(0) - 1;
1015
c06b7b0b
ILT
1016 // An uninitialized entry. We need this because we want to put
1017 // instances of this class into an STL container.
1018 Output_reloc()
1019 : local_sym_index_(INVALID_CODE)
1020 { }
1021
dceae3c1
ILT
1022 // We have a bunch of different constructors. They come in pairs
1023 // depending on how the address of the relocation is specified. It
1024 // can either be an offset in an Output_data or an offset in an
1025 // input section.
1026
c06b7b0b 1027 // A reloc against a global symbol.
5a6f7e2d 1028
a3ad94ed 1029 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
e8c846c3 1030 Address address, bool is_relative);
5a6f7e2d 1031
ef9beddf
ILT
1032 Output_reloc(Symbol* gsym, unsigned int type,
1033 Sized_relobj<size, big_endian>* relobj,
e8c846c3 1034 unsigned int shndx, Address address, bool is_relative);
c06b7b0b 1035
dceae3c1 1036 // A reloc against a local symbol or local section symbol.
5a6f7e2d
ILT
1037
1038 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 1039 unsigned int local_sym_index, unsigned int type,
dceae3c1
ILT
1040 Output_data* od, Address address, bool is_relative,
1041 bool is_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
ILT
1045 unsigned int shndx, Address address, bool is_relative,
1046 bool is_section_symbol);
c06b7b0b
ILT
1047
1048 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1049
a3ad94ed 1050 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
7bf1f802 1051 Address address);
5a6f7e2d 1052
ef9beddf
ILT
1053 Output_reloc(Output_section* os, unsigned int type,
1054 Sized_relobj<size, big_endian>* relobj,
7bf1f802 1055 unsigned int shndx, Address address);
c06b7b0b 1056
e8c846c3
ILT
1057 // Return TRUE if this is a RELATIVE relocation.
1058 bool
1059 is_relative() const
1060 { return this->is_relative_; }
1061
dceae3c1
ILT
1062 // Return whether this is against a local section symbol.
1063 bool
1064 is_local_section_symbol() const
1065 {
1066 return (this->local_sym_index_ != GSYM_CODE
1067 && this->local_sym_index_ != SECTION_CODE
1068 && this->local_sym_index_ != INVALID_CODE
1069 && this->is_section_symbol_);
1070 }
1071
1072 // For a local section symbol, return the offset of the input
624f8810
ILT
1073 // section within the output section. ADDEND is the addend being
1074 // applied to the input section.
ef9beddf 1075 Address
624f8810 1076 local_section_offset(Addend addend) const;
dceae3c1 1077
d1f003c6
ILT
1078 // Get the value of the symbol referred to by a Rel relocation when
1079 // we are adding the given ADDEND.
e8c846c3 1080 Address
624f8810 1081 symbol_value(Addend addend) const;
e8c846c3 1082
c06b7b0b
ILT
1083 // Write the reloc entry to an output view.
1084 void
1085 write(unsigned char* pov) const;
1086
1087 // Write the offset and info fields to Write_rel.
1088 template<typename Write_rel>
1089 void write_rel(Write_rel*) const;
1090
d98bc257
ILT
1091 // This is used when sorting dynamic relocs. Return -1 to sort this
1092 // reloc before R2, 0 to sort the same as R2, 1 to sort after R2.
1093 int
1094 compare(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>& r2)
1095 const;
1096
1097 // Return whether this reloc should be sorted before the argument
1098 // when sorting dynamic relocs.
1099 bool
1100 sort_before(const Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>&
1101 r2) const
1102 { return this->compare(r2) < 0; }
1103
c06b7b0b 1104 private:
dceae3c1
ILT
1105 // Record that we need a dynamic symbol index.
1106 void
1107 set_needs_dynsym_index();
1108
1109 // Return the symbol index.
c06b7b0b
ILT
1110 unsigned int
1111 get_symbol_index() const;
1112
d98bc257 1113 // Return the output address.
a984ee1d 1114 Address
d98bc257
ILT
1115 get_address() const;
1116
c06b7b0b
ILT
1117 // Codes for local_sym_index_.
1118 enum
1119 {
1120 // Global symbol.
1121 GSYM_CODE = -1U,
1122 // Output section.
1123 SECTION_CODE = -2U,
1124 // Invalid uninitialized entry.
1125 INVALID_CODE = -3U
1126 };
1127
1128 union
1129 {
dceae3c1
ILT
1130 // For a local symbol or local section symbol
1131 // (this->local_sym_index_ >= 0), the object. We will never
1132 // generate a relocation against a local symbol in a dynamic
1133 // object; that doesn't make sense. And our callers will always
1134 // be templatized, so we use Sized_relobj here.
5a6f7e2d 1135 Sized_relobj<size, big_endian>* relobj;
dceae3c1
ILT
1136 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
1137 // symbol. If this is NULL, it indicates a relocation against the
1138 // undefined 0 symbol.
c06b7b0b 1139 Symbol* gsym;
dceae3c1
ILT
1140 // For a relocation against an output section
1141 // (this->local_sym_index_ == SECTION_CODE), the output section.
c06b7b0b 1142 Output_section* os;
5a6f7e2d
ILT
1143 } u1_;
1144 union
1145 {
dceae3c1
ILT
1146 // If this->shndx_ is not INVALID CODE, the object which holds the
1147 // input section being used to specify the reloc address.
ef9beddf 1148 Sized_relobj<size, big_endian>* relobj;
dceae3c1 1149 // If this->shndx_ is INVALID_CODE, the output data being used to
5a6f7e2d
ILT
1150 // specify the reloc address. This may be NULL if the reloc
1151 // address is absolute.
1152 Output_data* od;
1153 } u2_;
1154 // The address offset within the input section or the Output_data.
1155 Address address_;
dceae3c1
ILT
1156 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
1157 // relocation against an output section, or INVALID_CODE for an
1158 // uninitialized value. Otherwise, for a local symbol
1159 // (this->is_section_symbol_ is false), the local symbol index. For
1160 // a local section symbol (this->is_section_symbol_ is true), the
1161 // section index in the input file.
c06b7b0b 1162 unsigned int local_sym_index_;
a3ad94ed 1163 // The reloc type--a processor specific code.
dceae3c1 1164 unsigned int type_ : 30;
e8c846c3
ILT
1165 // True if the relocation is a RELATIVE relocation.
1166 bool is_relative_ : 1;
dceae3c1
ILT
1167 // True if the relocation is against a section symbol.
1168 bool is_section_symbol_ : 1;
5a6f7e2d
ILT
1169 // If the reloc address is an input section in an object, the
1170 // section index. This is INVALID_CODE if the reloc address is
1171 // specified in some other way.
1172 unsigned int shndx_;
c06b7b0b
ILT
1173};
1174
1175// The SHT_RELA version of Output_reloc<>. This is just derived from
1176// the SHT_REL version of Output_reloc, but it adds an addend.
1177
1178template<bool dynamic, int size, bool big_endian>
1179class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1180{
1181 public:
1182 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1183 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
1184
1185 // An uninitialized entry.
1186 Output_reloc()
1187 : rel_()
1188 { }
1189
1190 // A reloc against a global symbol.
5a6f7e2d 1191
a3ad94ed 1192 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
e8c846c3
ILT
1193 Address address, Addend addend, bool is_relative)
1194 : rel_(gsym, type, od, address, is_relative), addend_(addend)
c06b7b0b
ILT
1195 { }
1196
ef9beddf
ILT
1197 Output_reloc(Symbol* gsym, unsigned int type,
1198 Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
1199 unsigned int shndx, Address address, Addend addend,
1200 bool is_relative)
1201 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
5a6f7e2d
ILT
1202 { }
1203
c06b7b0b 1204 // A reloc against a local symbol.
5a6f7e2d
ILT
1205
1206 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
1207 unsigned int local_sym_index, unsigned int type,
1208 Output_data* od, Address address,
dceae3c1
ILT
1209 Addend addend, bool is_relative, bool is_section_symbol)
1210 : rel_(relobj, local_sym_index, type, od, address, is_relative,
1211 is_section_symbol),
e8c846c3 1212 addend_(addend)
5a6f7e2d
ILT
1213 { }
1214
1215 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
1216 unsigned int local_sym_index, unsigned int type,
1217 unsigned int shndx, Address address,
dceae3c1
ILT
1218 Addend addend, bool is_relative, bool is_section_symbol)
1219 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
1220 is_section_symbol),
5a6f7e2d 1221 addend_(addend)
c06b7b0b
ILT
1222 { }
1223
1224 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1225
a3ad94ed
ILT
1226 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
1227 Address address, Addend addend)
1228 : rel_(os, type, od, address), addend_(addend)
c06b7b0b
ILT
1229 { }
1230
ef9beddf
ILT
1231 Output_reloc(Output_section* os, unsigned int type,
1232 Sized_relobj<size, big_endian>* relobj,
5a6f7e2d
ILT
1233 unsigned int shndx, Address address, Addend addend)
1234 : rel_(os, type, relobj, shndx, address), addend_(addend)
1235 { }
1236
c06b7b0b
ILT
1237 // Write the reloc entry to an output view.
1238 void
1239 write(unsigned char* pov) const;
1240
d98bc257
ILT
1241 // Return whether this reloc should be sorted before the argument
1242 // when sorting dynamic relocs.
1243 bool
1244 sort_before(const Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>&
1245 r2) const
1246 {
1247 int i = this->rel_.compare(r2.rel_);
1248 if (i < 0)
d98bc257 1249 return true;
cc28ec61
ILT
1250 else if (i > 0)
1251 return false;
d98bc257
ILT
1252 else
1253 return this->addend_ < r2.addend_;
1254 }
1255
c06b7b0b
ILT
1256 private:
1257 // The basic reloc.
1258 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
1259 // The addend.
1260 Addend addend_;
1261};
1262
1263// Output_data_reloc is used to manage a section containing relocs.
1264// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
1265// indicates whether this is a dynamic relocation or a normal
1266// relocation. Output_data_reloc_base is a base class.
1267// Output_data_reloc is the real class, which we specialize based on
1268// the reloc type.
1269
1270template<int sh_type, bool dynamic, int size, bool big_endian>
27bc2bce 1271class Output_data_reloc_base : public Output_section_data_build
c06b7b0b
ILT
1272{
1273 public:
1274 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1275 typedef typename Output_reloc_type::Address Address;
1276 static const int reloc_size =
1277 Reloc_types<sh_type, size, big_endian>::reloc_size;
1278
1279 // Construct the section.
d98bc257
ILT
1280 Output_data_reloc_base(bool sort_relocs)
1281 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
1282 sort_relocs_(sort_relocs)
c06b7b0b
ILT
1283 { }
1284
27bc2bce 1285 protected:
c06b7b0b
ILT
1286 // Write out the data.
1287 void
1288 do_write(Output_file*);
1289
16649710
ILT
1290 // Set the entry size and the link.
1291 void
1292 do_adjust_output_section(Output_section *os);
1293
7d9e3d98
ILT
1294 // Write to a map file.
1295 void
1296 do_print_to_mapfile(Mapfile* mapfile) const
1297 {
1298 mapfile->print_output_data(this,
1299 (dynamic
1300 ? _("** dynamic relocs")
1301 : _("** relocs")));
1302 }
1303
c06b7b0b
ILT
1304 // Add a relocation entry.
1305 void
4f4c5f80 1306 add(Output_data *od, const Output_reloc_type& reloc)
c06b7b0b
ILT
1307 {
1308 this->relocs_.push_back(reloc);
27bc2bce 1309 this->set_current_data_size(this->relocs_.size() * reloc_size);
4f4c5f80 1310 od->add_dynamic_reloc();
c06b7b0b
ILT
1311 }
1312
1313 private:
1314 typedef std::vector<Output_reloc_type> Relocs;
1315
d98bc257
ILT
1316 // The class used to sort the relocations.
1317 struct Sort_relocs_comparison
1318 {
1319 bool
1320 operator()(const Output_reloc_type& r1, const Output_reloc_type& r2) const
1321 { return r1.sort_before(r2); }
1322 };
1323
1324 // The relocations in this section.
c06b7b0b 1325 Relocs relocs_;
d98bc257
ILT
1326 // Whether to sort the relocations when writing them out, to make
1327 // the dynamic linker more efficient.
1328 bool sort_relocs_;
c06b7b0b
ILT
1329};
1330
1331// The class which callers actually create.
1332
1333template<int sh_type, bool dynamic, int size, bool big_endian>
1334class Output_data_reloc;
1335
1336// The SHT_REL version of Output_data_reloc.
1337
1338template<bool dynamic, int size, bool big_endian>
1339class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1340 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1341{
dceae3c1 1342 private:
c06b7b0b
ILT
1343 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1344 big_endian> Base;
1345
1346 public:
1347 typedef typename Base::Output_reloc_type Output_reloc_type;
1348 typedef typename Output_reloc_type::Address Address;
1349
d98bc257
ILT
1350 Output_data_reloc(bool sr)
1351 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>(sr)
c06b7b0b
ILT
1352 { }
1353
1354 // Add a reloc against a global symbol.
5a6f7e2d 1355
c06b7b0b 1356 void
a3ad94ed 1357 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
e8c846c3 1358 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
c06b7b0b 1359
5a6f7e2d 1360 void
ef9beddf
ILT
1361 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1362 Sized_relobj<size, big_endian>* relobj,
5a6f7e2d 1363 unsigned int shndx, Address address)
e8c846c3
ILT
1364 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1365 false)); }
1366
12c0daef
ILT
1367 // These are to simplify the Copy_relocs class.
1368
1369 void
1370 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address,
1371 Address addend)
1372 {
1373 gold_assert(addend == 0);
1374 this->add_global(gsym, type, od, address);
1375 }
1376
1377 void
ef9beddf
ILT
1378 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1379 Sized_relobj<size, big_endian>* relobj,
12c0daef
ILT
1380 unsigned int shndx, Address address, Address addend)
1381 {
1382 gold_assert(addend == 0);
1383 this->add_global(gsym, type, od, relobj, shndx, address);
1384 }
1385
e8c846c3
ILT
1386 // Add a RELATIVE reloc against a global symbol. The final relocation
1387 // will not reference the symbol.
1388
1389 void
1390 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1391 Address address)
1392 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1393
1394 void
1395 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
ef9beddf
ILT
1396 Sized_relobj<size, big_endian>* relobj,
1397 unsigned int shndx, Address address)
dceae3c1
ILT
1398 {
1399 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1400 true));
1401 }
5a6f7e2d 1402
c06b7b0b 1403 // Add a reloc against a local symbol.
5a6f7e2d 1404
c06b7b0b 1405 void
5a6f7e2d 1406 add_local(Sized_relobj<size, big_endian>* relobj,
a3ad94ed
ILT
1407 unsigned int local_sym_index, unsigned int type,
1408 Output_data* od, Address address)
dceae3c1
ILT
1409 {
1410 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1411 address, false, false));
1412 }
5a6f7e2d
ILT
1413
1414 void
1415 add_local(Sized_relobj<size, big_endian>* relobj,
1416 unsigned int local_sym_index, unsigned int type,
4f4c5f80 1417 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1418 {
1419 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1420 address, false, false));
1421 }
e8c846c3
ILT
1422
1423 // Add a RELATIVE reloc against a local symbol.
5a6f7e2d 1424
e8c846c3
ILT
1425 void
1426 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1427 unsigned int local_sym_index, unsigned int type,
1428 Output_data* od, Address address)
dceae3c1
ILT
1429 {
1430 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1431 address, true, false));
1432 }
e8c846c3
ILT
1433
1434 void
1435 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1436 unsigned int local_sym_index, unsigned int type,
1437 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1438 {
1439 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1440 address, true, false));
1441 }
1442
1443 // Add a reloc against a local section symbol. This will be
1444 // converted into a reloc against the STT_SECTION symbol of the
1445 // output section.
1446
1447 void
1448 add_local_section(Sized_relobj<size, big_endian>* relobj,
1449 unsigned int input_shndx, unsigned int type,
1450 Output_data* od, Address address)
1451 {
1452 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1453 address, false, true));
1454 }
1455
1456 void
1457 add_local_section(Sized_relobj<size, big_endian>* relobj,
1458 unsigned int input_shndx, unsigned int type,
1459 Output_data* od, unsigned int shndx, Address address)
1460 {
1461 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1462 address, false, true));
1463 }
c06b7b0b
ILT
1464
1465 // A reloc against the STT_SECTION symbol of an output section.
4f4c5f80
ILT
1466 // OS is the Output_section that the relocation refers to; OD is
1467 // the Output_data object being relocated.
5a6f7e2d 1468
c06b7b0b 1469 void
a3ad94ed
ILT
1470 add_output_section(Output_section* os, unsigned int type,
1471 Output_data* od, Address address)
4f4c5f80 1472 { this->add(od, Output_reloc_type(os, type, od, address)); }
5a6f7e2d
ILT
1473
1474 void
4f4c5f80 1475 add_output_section(Output_section* os, unsigned int type, Output_data* od,
ef9beddf
ILT
1476 Sized_relobj<size, big_endian>* relobj,
1477 unsigned int shndx, Address address)
4f4c5f80 1478 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
c06b7b0b
ILT
1479};
1480
1481// The SHT_RELA version of Output_data_reloc.
1482
1483template<bool dynamic, int size, bool big_endian>
1484class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1485 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1486{
dceae3c1 1487 private:
c06b7b0b
ILT
1488 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1489 big_endian> Base;
1490
1491 public:
1492 typedef typename Base::Output_reloc_type Output_reloc_type;
1493 typedef typename Output_reloc_type::Address Address;
1494 typedef typename Output_reloc_type::Addend Addend;
1495
d98bc257
ILT
1496 Output_data_reloc(bool sr)
1497 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>(sr)
c06b7b0b
ILT
1498 { }
1499
1500 // Add a reloc against a global symbol.
5a6f7e2d 1501
c06b7b0b 1502 void
a3ad94ed
ILT
1503 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1504 Address address, Addend addend)
e8c846c3
ILT
1505 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1506 false)); }
c06b7b0b 1507
5a6f7e2d 1508 void
ef9beddf
ILT
1509 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1510 Sized_relobj<size, big_endian>* relobj,
4f4c5f80
ILT
1511 unsigned int shndx, Address address,
1512 Addend addend)
1513 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
e8c846c3
ILT
1514 addend, false)); }
1515
1516 // Add a RELATIVE reloc against a global symbol. The final output
1517 // relocation will not reference the symbol, but we must keep the symbol
1518 // information long enough to set the addend of the relocation correctly
1519 // when it is written.
1520
1521 void
1522 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1523 Address address, Addend addend)
1524 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1525
1526 void
1527 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
ef9beddf
ILT
1528 Sized_relobj<size, big_endian>* relobj,
1529 unsigned int shndx, Address address, Addend addend)
e8c846c3
ILT
1530 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1531 addend, true)); }
5a6f7e2d 1532
c06b7b0b 1533 // Add a reloc against a local symbol.
5a6f7e2d 1534
c06b7b0b 1535 void
5a6f7e2d 1536 add_local(Sized_relobj<size, big_endian>* relobj,
c06b7b0b 1537 unsigned int local_sym_index, unsigned int type,
a3ad94ed 1538 Output_data* od, Address address, Addend addend)
c06b7b0b 1539 {
4f4c5f80 1540 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
dceae3c1 1541 addend, false, false));
5a6f7e2d
ILT
1542 }
1543
1544 void
1545 add_local(Sized_relobj<size, big_endian>* relobj,
1546 unsigned int local_sym_index, unsigned int type,
4f4c5f80
ILT
1547 Output_data* od, unsigned int shndx, Address address,
1548 Addend addend)
5a6f7e2d 1549 {
4f4c5f80 1550 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
dceae3c1 1551 address, addend, false, false));
e8c846c3
ILT
1552 }
1553
1554 // Add a RELATIVE reloc against a local symbol.
1555
1556 void
1557 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1558 unsigned int local_sym_index, unsigned int type,
1559 Output_data* od, Address address, Addend addend)
1560 {
1561 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
dceae3c1 1562 addend, true, false));
e8c846c3
ILT
1563 }
1564
1565 void
1566 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1567 unsigned int local_sym_index, unsigned int type,
1568 Output_data* od, unsigned int shndx, Address address,
1569 Addend addend)
1570 {
1571 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
dceae3c1
ILT
1572 address, addend, true, false));
1573 }
1574
1575 // Add a reloc against a local section symbol. This will be
1576 // converted into a reloc against the STT_SECTION symbol of the
1577 // output section.
1578
1579 void
1580 add_local_section(Sized_relobj<size, big_endian>* relobj,
1581 unsigned int input_shndx, unsigned int type,
1582 Output_data* od, Address address, Addend addend)
1583 {
1584 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1585 addend, false, true));
1586 }
1587
1588 void
1589 add_local_section(Sized_relobj<size, big_endian>* relobj,
1590 unsigned int input_shndx, unsigned int type,
1591 Output_data* od, unsigned int shndx, Address address,
1592 Addend addend)
1593 {
1594 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1595 address, addend, false, true));
c06b7b0b
ILT
1596 }
1597
1598 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1599
c06b7b0b 1600 void
a3ad94ed
ILT
1601 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1602 Address address, Addend addend)
4f4c5f80 1603 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
5a6f7e2d
ILT
1604
1605 void
ef9beddf
ILT
1606 add_output_section(Output_section* os, unsigned int type,
1607 Sized_relobj<size, big_endian>* relobj,
5a6f7e2d 1608 unsigned int shndx, Address address, Addend addend)
4f4c5f80
ILT
1609 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1610 addend)); }
c06b7b0b
ILT
1611};
1612
6a74a719
ILT
1613// Output_relocatable_relocs represents a relocation section in a
1614// relocatable link. The actual data is written out in the target
1615// hook relocate_for_relocatable. This just saves space for it.
1616
1617template<int sh_type, int size, bool big_endian>
1618class Output_relocatable_relocs : public Output_section_data
1619{
1620 public:
1621 Output_relocatable_relocs(Relocatable_relocs* rr)
1622 : Output_section_data(Output_data::default_alignment_for_size(size)),
1623 rr_(rr)
1624 { }
1625
1626 void
1627 set_final_data_size();
1628
1629 // Write out the data. There is nothing to do here.
1630 void
1631 do_write(Output_file*)
1632 { }
1633
7d9e3d98
ILT
1634 // Write to a map file.
1635 void
1636 do_print_to_mapfile(Mapfile* mapfile) const
1637 { mapfile->print_output_data(this, _("** relocs")); }
1638
6a74a719
ILT
1639 private:
1640 // The relocs associated with this input section.
1641 Relocatable_relocs* rr_;
1642};
1643
1644// Handle a GROUP section.
1645
1646template<int size, bool big_endian>
1647class Output_data_group : public Output_section_data
1648{
1649 public:
8825ac63 1650 // The constructor clears *INPUT_SHNDXES.
6a74a719
ILT
1651 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1652 section_size_type entry_count,
8825ac63
ILT
1653 elfcpp::Elf_Word flags,
1654 std::vector<unsigned int>* input_shndxes);
6a74a719
ILT
1655
1656 void
1657 do_write(Output_file*);
1658
7d9e3d98
ILT
1659 // Write to a map file.
1660 void
1661 do_print_to_mapfile(Mapfile* mapfile) const
1662 { mapfile->print_output_data(this, _("** group")); }
1663
20e6d0d6
DK
1664 // Set final data size.
1665 void
1666 set_final_data_size()
1667 { this->set_data_size((this->input_shndxes_.size() + 1) * 4); }
1668
6a74a719
ILT
1669 private:
1670 // The input object.
1671 Sized_relobj<size, big_endian>* relobj_;
1672 // The group flag word.
1673 elfcpp::Elf_Word flags_;
1674 // The section indexes of the input sections in this group.
8825ac63 1675 std::vector<unsigned int> input_shndxes_;
6a74a719
ILT
1676};
1677
dbe717ef
ILT
1678// Output_data_got is used to manage a GOT. Each entry in the GOT is
1679// for one symbol--either a global symbol or a local symbol in an
ead1e424 1680// object. The target specific code adds entries to the GOT as
dbe717ef 1681// needed.
ead1e424
ILT
1682
1683template<int size, bool big_endian>
27bc2bce 1684class Output_data_got : public Output_section_data_build
ead1e424
ILT
1685{
1686 public:
1687 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
7bf1f802
ILT
1688 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1689 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
ead1e424 1690
7e1edb90 1691 Output_data_got()
27bc2bce 1692 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
730cdc88 1693 entries_()
ead1e424
ILT
1694 { }
1695
dbe717ef
ILT
1696 // Add an entry for a global symbol to the GOT. Return true if this
1697 // is a new GOT entry, false if the symbol was already in the GOT.
1698 bool
0a65a3a7 1699 add_global(Symbol* gsym, unsigned int got_type);
ead1e424 1700
7bf1f802
ILT
1701 // Add an entry for a global symbol to the GOT, and add a dynamic
1702 // relocation of type R_TYPE for the GOT entry.
1703 void
0a65a3a7
CC
1704 add_global_with_rel(Symbol* gsym, unsigned int got_type,
1705 Rel_dyn* rel_dyn, unsigned int r_type);
7bf1f802
ILT
1706
1707 void
0a65a3a7
CC
1708 add_global_with_rela(Symbol* gsym, unsigned int got_type,
1709 Rela_dyn* rela_dyn, unsigned int r_type);
1710
1711 // Add a pair of entries for a global symbol to the GOT, and add
1712 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
1713 void
1714 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
1715 Rel_dyn* rel_dyn, unsigned int r_type_1,
1716 unsigned int r_type_2);
1717
1718 void
1719 add_global_pair_with_rela(Symbol* gsym, unsigned int got_type,
1720 Rela_dyn* rela_dyn, unsigned int r_type_1,
1721 unsigned int r_type_2);
7bf1f802 1722
e727fa71
ILT
1723 // Add an entry for a local symbol to the GOT. This returns true if
1724 // this is a new GOT entry, false if the symbol already has a GOT
1725 // entry.
1726 bool
0a65a3a7
CC
1727 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index,
1728 unsigned int got_type);
ead1e424 1729
0a65a3a7 1730 // Add an entry for a local symbol to the GOT, and add a dynamic
7bf1f802
ILT
1731 // relocation of type R_TYPE for the GOT entry.
1732 void
1733 add_local_with_rel(Sized_relobj<size, big_endian>* object,
0a65a3a7
CC
1734 unsigned int sym_index, unsigned int got_type,
1735 Rel_dyn* rel_dyn, unsigned int r_type);
7bf1f802
ILT
1736
1737 void
1738 add_local_with_rela(Sized_relobj<size, big_endian>* object,
0a65a3a7
CC
1739 unsigned int sym_index, unsigned int got_type,
1740 Rela_dyn* rela_dyn, unsigned int r_type);
07f397ab 1741
0a65a3a7
CC
1742 // Add a pair of entries for a local symbol to the GOT, and add
1743 // dynamic relocations of type R_TYPE_1 and R_TYPE_2, respectively.
7bf1f802 1744 void
0a65a3a7
CC
1745 add_local_pair_with_rel(Sized_relobj<size, big_endian>* object,
1746 unsigned int sym_index, unsigned int shndx,
1747 unsigned int got_type, Rel_dyn* rel_dyn,
1748 unsigned int r_type_1, unsigned int r_type_2);
7bf1f802
ILT
1749
1750 void
0a65a3a7
CC
1751 add_local_pair_with_rela(Sized_relobj<size, big_endian>* object,
1752 unsigned int sym_index, unsigned int shndx,
1753 unsigned int got_type, Rela_dyn* rela_dyn,
1754 unsigned int r_type_1, unsigned int r_type_2);
7bf1f802 1755
ead1e424
ILT
1756 // Add a constant to the GOT. This returns the offset of the new
1757 // entry from the start of the GOT.
1758 unsigned int
1759 add_constant(Valtype constant)
1760 {
1761 this->entries_.push_back(Got_entry(constant));
1762 this->set_got_size();
1763 return this->last_got_offset();
1764 }
1765
27bc2bce 1766 protected:
ead1e424
ILT
1767 // Write out the GOT table.
1768 void
1769 do_write(Output_file*);
1770
7d9e3d98
ILT
1771 // Write to a map file.
1772 void
1773 do_print_to_mapfile(Mapfile* mapfile) const
1774 { mapfile->print_output_data(this, _("** GOT")); }
1775
ead1e424
ILT
1776 private:
1777 // This POD class holds a single GOT entry.
1778 class Got_entry
1779 {
1780 public:
1781 // Create a zero entry.
1782 Got_entry()
1783 : local_sym_index_(CONSTANT_CODE)
1784 { this->u_.constant = 0; }
1785
1786 // Create a global symbol entry.
a3ad94ed 1787 explicit Got_entry(Symbol* gsym)
ead1e424
ILT
1788 : local_sym_index_(GSYM_CODE)
1789 { this->u_.gsym = gsym; }
1790
1791 // Create a local symbol entry.
e727fa71
ILT
1792 Got_entry(Sized_relobj<size, big_endian>* object,
1793 unsigned int local_sym_index)
ead1e424
ILT
1794 : local_sym_index_(local_sym_index)
1795 {
a3ad94ed
ILT
1796 gold_assert(local_sym_index != GSYM_CODE
1797 && local_sym_index != CONSTANT_CODE);
ead1e424
ILT
1798 this->u_.object = object;
1799 }
1800
1801 // Create a constant entry. The constant is a host value--it will
1802 // be swapped, if necessary, when it is written out.
a3ad94ed 1803 explicit Got_entry(Valtype constant)
ead1e424
ILT
1804 : local_sym_index_(CONSTANT_CODE)
1805 { this->u_.constant = constant; }
1806
1807 // Write the GOT entry to an output view.
1808 void
7e1edb90 1809 write(unsigned char* pov) const;
ead1e424
ILT
1810
1811 private:
1812 enum
1813 {
1814 GSYM_CODE = -1U,
1815 CONSTANT_CODE = -2U
1816 };
1817
1818 union
1819 {
1820 // For a local symbol, the object.
e727fa71 1821 Sized_relobj<size, big_endian>* object;
ead1e424
ILT
1822 // For a global symbol, the symbol.
1823 Symbol* gsym;
1824 // For a constant, the constant.
1825 Valtype constant;
1826 } u_;
c06b7b0b
ILT
1827 // For a local symbol, the local symbol index. This is GSYM_CODE
1828 // for a global symbol, or CONSTANT_CODE for a constant.
ead1e424
ILT
1829 unsigned int local_sym_index_;
1830 };
1831
1832 typedef std::vector<Got_entry> Got_entries;
1833
1834 // Return the offset into the GOT of GOT entry I.
1835 unsigned int
1836 got_offset(unsigned int i) const
1837 { return i * (size / 8); }
1838
1839 // Return the offset into the GOT of the last entry added.
1840 unsigned int
1841 last_got_offset() const
1842 { return this->got_offset(this->entries_.size() - 1); }
1843
1844 // Set the size of the section.
1845 void
1846 set_got_size()
27bc2bce 1847 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
ead1e424
ILT
1848
1849 // The list of GOT entries.
1850 Got_entries entries_;
1851};
1852
a3ad94ed
ILT
1853// Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1854// section.
1855
1856class Output_data_dynamic : public Output_section_data
1857{
1858 public:
9025d29d 1859 Output_data_dynamic(Stringpool* pool)
730cdc88 1860 : Output_section_data(Output_data::default_alignment()),
9025d29d 1861 entries_(), pool_(pool)
a3ad94ed
ILT
1862 { }
1863
1864 // Add a new dynamic entry with a fixed numeric value.
1865 void
1866 add_constant(elfcpp::DT tag, unsigned int val)
1867 { this->add_entry(Dynamic_entry(tag, val)); }
1868
16649710 1869 // Add a new dynamic entry with the address of output data.
a3ad94ed 1870 void
16649710
ILT
1871 add_section_address(elfcpp::DT tag, const Output_data* od)
1872 { this->add_entry(Dynamic_entry(tag, od, false)); }
a3ad94ed 1873
c2b45e22
CC
1874 // Add a new dynamic entry with the address of output data
1875 // plus a constant offset.
1876 void
1877 add_section_plus_offset(elfcpp::DT tag, const Output_data* od,
1878 unsigned int offset)
1879 { this->add_entry(Dynamic_entry(tag, od, offset)); }
1880
16649710 1881 // Add a new dynamic entry with the size of output data.
a3ad94ed 1882 void
16649710
ILT
1883 add_section_size(elfcpp::DT tag, const Output_data* od)
1884 { this->add_entry(Dynamic_entry(tag, od, true)); }
a3ad94ed
ILT
1885
1886 // Add a new dynamic entry with the address of a symbol.
1887 void
16649710 1888 add_symbol(elfcpp::DT tag, const Symbol* sym)
a3ad94ed
ILT
1889 { this->add_entry(Dynamic_entry(tag, sym)); }
1890
1891 // Add a new dynamic entry with a string.
1892 void
1893 add_string(elfcpp::DT tag, const char* str)
cfd73a4e 1894 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
a3ad94ed 1895
41f542e7
ILT
1896 void
1897 add_string(elfcpp::DT tag, const std::string& str)
1898 { this->add_string(tag, str.c_str()); }
1899
27bc2bce
ILT
1900 protected:
1901 // Adjust the output section to set the entry size.
1902 void
1903 do_adjust_output_section(Output_section*);
1904
a3ad94ed
ILT
1905 // Set the final data size.
1906 void
27bc2bce 1907 set_final_data_size();
a3ad94ed
ILT
1908
1909 // Write out the dynamic entries.
1910 void
1911 do_write(Output_file*);
1912
7d9e3d98
ILT
1913 // Write to a map file.
1914 void
1915 do_print_to_mapfile(Mapfile* mapfile) const
1916 { mapfile->print_output_data(this, _("** dynamic")); }
1917
a3ad94ed
ILT
1918 private:
1919 // This POD class holds a single dynamic entry.
1920 class Dynamic_entry
1921 {
1922 public:
1923 // Create an entry with a fixed numeric value.
1924 Dynamic_entry(elfcpp::DT tag, unsigned int val)
c2b45e22 1925 : tag_(tag), offset_(DYNAMIC_NUMBER)
a3ad94ed
ILT
1926 { this->u_.val = val; }
1927
1928 // Create an entry with the size or address of a section.
16649710 1929 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
a3ad94ed 1930 : tag_(tag),
c2b45e22
CC
1931 offset_(section_size
1932 ? DYNAMIC_SECTION_SIZE
1933 : DYNAMIC_SECTION_ADDRESS)
1934 { this->u_.od = od; }
1935
1936 // Create an entry with the address of a section plus a constant offset.
1937 Dynamic_entry(elfcpp::DT tag, const Output_data* od, unsigned int offset)
1938 : tag_(tag),
1939 offset_(offset)
16649710 1940 { this->u_.od = od; }
a3ad94ed
ILT
1941
1942 // Create an entry with the address of a symbol.
16649710 1943 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
c2b45e22 1944 : tag_(tag), offset_(DYNAMIC_SYMBOL)
a3ad94ed
ILT
1945 { this->u_.sym = sym; }
1946
1947 // Create an entry with a string.
1948 Dynamic_entry(elfcpp::DT tag, const char* str)
c2b45e22 1949 : tag_(tag), offset_(DYNAMIC_STRING)
a3ad94ed
ILT
1950 { this->u_.str = str; }
1951
20e6d0d6
DK
1952 // Return the tag of this entry.
1953 elfcpp::DT
1954 tag() const
1955 { return this->tag_; }
1956
a3ad94ed
ILT
1957 // Write the dynamic entry to an output view.
1958 template<int size, bool big_endian>
1959 void
7d1a9ebb 1960 write(unsigned char* pov, const Stringpool*) const;
a3ad94ed
ILT
1961
1962 private:
c2b45e22 1963 // Classification is encoded in the OFFSET field.
a3ad94ed
ILT
1964 enum Classification
1965 {
a3ad94ed 1966 // Section address.
c2b45e22
CC
1967 DYNAMIC_SECTION_ADDRESS = 0,
1968 // Number.
1969 DYNAMIC_NUMBER = -1U,
a3ad94ed 1970 // Section size.
c2b45e22 1971 DYNAMIC_SECTION_SIZE = -2U,
a3ad94ed 1972 // Symbol adress.
c2b45e22 1973 DYNAMIC_SYMBOL = -3U,
a3ad94ed 1974 // String.
c2b45e22
CC
1975 DYNAMIC_STRING = -4U
1976 // Any other value indicates a section address plus OFFSET.
a3ad94ed
ILT
1977 };
1978
1979 union
1980 {
1981 // For DYNAMIC_NUMBER.
1982 unsigned int val;
c2b45e22 1983 // For DYNAMIC_SECTION_SIZE and section address plus OFFSET.
16649710 1984 const Output_data* od;
a3ad94ed 1985 // For DYNAMIC_SYMBOL.
16649710 1986 const Symbol* sym;
a3ad94ed
ILT
1987 // For DYNAMIC_STRING.
1988 const char* str;
1989 } u_;
1990 // The dynamic tag.
1991 elfcpp::DT tag_;
c2b45e22
CC
1992 // The type of entry (Classification) or offset within a section.
1993 unsigned int offset_;
a3ad94ed
ILT
1994 };
1995
1996 // Add an entry to the list.
1997 void
1998 add_entry(const Dynamic_entry& entry)
1999 { this->entries_.push_back(entry); }
2000
2001 // Sized version of write function.
2002 template<int size, bool big_endian>
2003 void
2004 sized_write(Output_file* of);
2005
2006 // The type of the list of entries.
2007 typedef std::vector<Dynamic_entry> Dynamic_entries;
2008
a3ad94ed
ILT
2009 // The entries.
2010 Dynamic_entries entries_;
2011 // The pool used for strings.
2012 Stringpool* pool_;
2013};
2014
d491d34e
ILT
2015// Output_symtab_xindex is used to handle SHT_SYMTAB_SHNDX sections,
2016// which may be required if the object file has more than
2017// SHN_LORESERVE sections.
2018
2019class Output_symtab_xindex : public Output_section_data
2020{
2021 public:
2022 Output_symtab_xindex(size_t symcount)
20e6d0d6 2023 : Output_section_data(symcount * 4, 4, true),
d491d34e
ILT
2024 entries_()
2025 { }
2026
2027 // Add an entry: symbol number SYMNDX has section SHNDX.
2028 void
2029 add(unsigned int symndx, unsigned int shndx)
2030 { this->entries_.push_back(std::make_pair(symndx, shndx)); }
2031
2032 protected:
2033 void
2034 do_write(Output_file*);
2035
7d9e3d98
ILT
2036 // Write to a map file.
2037 void
2038 do_print_to_mapfile(Mapfile* mapfile) const
2039 { mapfile->print_output_data(this, _("** symtab xindex")); }
2040
d491d34e
ILT
2041 private:
2042 template<bool big_endian>
2043 void
2044 endian_do_write(unsigned char*);
2045
2046 // It is likely that most symbols will not require entries. Rather
2047 // than keep a vector for all symbols, we keep pairs of symbol index
2048 // and section index.
2049 typedef std::vector<std::pair<unsigned int, unsigned int> > Xindex_entries;
2050
2051 // The entries we need.
2052 Xindex_entries entries_;
2053};
2054
20e6d0d6 2055// A relaxed input section.
c0a62865 2056class Output_relaxed_input_section : public Output_section_data_build
20e6d0d6
DK
2057{
2058 public:
2059 // We would like to call relobj->section_addralign(shndx) to get the
2060 // alignment but we do not want the constructor to fail. So callers
2061 // are repsonsible for ensuring that.
2062 Output_relaxed_input_section(Relobj* relobj, unsigned int shndx,
2063 uint64_t addralign)
c0a62865 2064 : Output_section_data_build(addralign), relobj_(relobj), shndx_(shndx)
20e6d0d6
DK
2065 { }
2066
2067 // Return the Relobj of this relaxed input section.
2068 Relobj*
2069 relobj() const
2070 { return this->relobj_; }
2071
2072 // Return the section index of this relaxed input section.
2073 unsigned int
2074 shndx() const
2075 { return this->shndx_; }
2076
2077 private:
2078 Relobj* relobj_;
2079 unsigned int shndx_;
2080};
2081
a2fb1b05
ILT
2082// An output section. We don't expect to have too many output
2083// sections, so we don't bother to do a template on the size.
2084
54dc6425 2085class Output_section : public Output_data
a2fb1b05
ILT
2086{
2087 public:
2088 // Create an output section, giving the name, type, and flags.
96803768 2089 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
54dc6425 2090 virtual ~Output_section();
a2fb1b05 2091
ead1e424 2092 // Add a new input section SHNDX, named NAME, with header SHDR, from
730cdc88 2093 // object OBJECT. RELOC_SHNDX is the index of a relocation section
eff45813 2094 // which applies to this section, or 0 if none, or -1 if more than
a445fddf
ILT
2095 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
2096 // in a linker script; in that case we need to keep track of input
2097 // sections associated with an output section. Return the offset
2098 // within the output section.
a2fb1b05
ILT
2099 template<int size, bool big_endian>
2100 off_t
730cdc88
ILT
2101 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
2102 const char *name,
2103 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf 2104 unsigned int reloc_shndx, bool have_sections_script);
a2fb1b05 2105
b8e6aad9 2106 // Add generated data POSD to this output section.
c06b7b0b 2107 void
ead1e424
ILT
2108 add_output_section_data(Output_section_data* posd);
2109
c0a62865
DK
2110 // Add a relaxed input section PORIS to this output section.
2111 void
2112 add_relaxed_input_section(Output_relaxed_input_section* poris);
2113
a2fb1b05
ILT
2114 // Return the section name.
2115 const char*
2116 name() const
2117 { return this->name_; }
2118
2119 // Return the section type.
2120 elfcpp::Elf_Word
2121 type() const
2122 { return this->type_; }
2123
2124 // Return the section flags.
2125 elfcpp::Elf_Xword
2126 flags() const
2127 { return this->flags_; }
2128
154e0e9a
ILT
2129 // Update the output section flags based on input section flags.
2130 void
9c547ec3 2131 update_flags_for_input_section(elfcpp::Elf_Xword flags);
154e0e9a 2132
a3ad94ed
ILT
2133 // Return the entsize field.
2134 uint64_t
2135 entsize() const
2136 { return this->entsize_; }
2137
61ba1cf9
ILT
2138 // Set the entsize field.
2139 void
16649710 2140 set_entsize(uint64_t v);
61ba1cf9 2141
a445fddf
ILT
2142 // Set the load address.
2143 void
2144 set_load_address(uint64_t load_address)
2145 {
2146 this->load_address_ = load_address;
2147 this->has_load_address_ = true;
2148 }
2149
16649710
ILT
2150 // Set the link field to the output section index of a section.
2151 void
14b31740 2152 set_link_section(const Output_data* od)
16649710
ILT
2153 {
2154 gold_assert(this->link_ == 0
2155 && !this->should_link_to_symtab_
2156 && !this->should_link_to_dynsym_);
2157 this->link_section_ = od;
2158 }
2159
2160 // Set the link field to a constant.
61ba1cf9
ILT
2161 void
2162 set_link(unsigned int v)
16649710
ILT
2163 {
2164 gold_assert(this->link_section_ == NULL
2165 && !this->should_link_to_symtab_
2166 && !this->should_link_to_dynsym_);
2167 this->link_ = v;
2168 }
61ba1cf9 2169
16649710
ILT
2170 // Record that this section should link to the normal symbol table.
2171 void
2172 set_should_link_to_symtab()
2173 {
2174 gold_assert(this->link_section_ == NULL
2175 && this->link_ == 0
2176 && !this->should_link_to_dynsym_);
2177 this->should_link_to_symtab_ = true;
2178 }
2179
2180 // Record that this section should link to the dynamic symbol table.
2181 void
2182 set_should_link_to_dynsym()
2183 {
2184 gold_assert(this->link_section_ == NULL
2185 && this->link_ == 0
2186 && !this->should_link_to_symtab_);
2187 this->should_link_to_dynsym_ = true;
2188 }
2189
2190 // Return the info field.
2191 unsigned int
2192 info() const
2193 {
755ab8af
ILT
2194 gold_assert(this->info_section_ == NULL
2195 && this->info_symndx_ == NULL);
16649710
ILT
2196 return this->info_;
2197 }
2198
2199 // Set the info field to the output section index of a section.
2200 void
755ab8af 2201 set_info_section(const Output_section* os)
16649710 2202 {
755ab8af
ILT
2203 gold_assert((this->info_section_ == NULL
2204 || (this->info_section_ == os
2205 && this->info_uses_section_index_))
2206 && this->info_symndx_ == NULL
2207 && this->info_ == 0);
2208 this->info_section_ = os;
2209 this->info_uses_section_index_= true;
16649710
ILT
2210 }
2211
6a74a719
ILT
2212 // Set the info field to the symbol table index of a symbol.
2213 void
2214 set_info_symndx(const Symbol* sym)
2215 {
755ab8af
ILT
2216 gold_assert(this->info_section_ == NULL
2217 && (this->info_symndx_ == NULL
2218 || this->info_symndx_ == sym)
2219 && this->info_ == 0);
6a74a719
ILT
2220 this->info_symndx_ = sym;
2221 }
2222
755ab8af
ILT
2223 // Set the info field to the symbol table index of a section symbol.
2224 void
2225 set_info_section_symndx(const Output_section* os)
2226 {
2227 gold_assert((this->info_section_ == NULL
2228 || (this->info_section_ == os
2229 && !this->info_uses_section_index_))
2230 && this->info_symndx_ == NULL
2231 && this->info_ == 0);
2232 this->info_section_ = os;
2233 this->info_uses_section_index_ = false;
2234 }
2235
16649710 2236 // Set the info field to a constant.
61ba1cf9
ILT
2237 void
2238 set_info(unsigned int v)
16649710 2239 {
755ab8af
ILT
2240 gold_assert(this->info_section_ == NULL
2241 && this->info_symndx_ == NULL
2242 && (this->info_ == 0
2243 || this->info_ == v));
16649710
ILT
2244 this->info_ = v;
2245 }
61ba1cf9
ILT
2246
2247 // Set the addralign field.
2248 void
2249 set_addralign(uint64_t v)
2250 { this->addralign_ = v; }
2251
d491d34e
ILT
2252 // Whether the output section index has been set.
2253 bool
2254 has_out_shndx() const
2255 { return this->out_shndx_ != -1U; }
2256
c06b7b0b
ILT
2257 // Indicate that we need a symtab index.
2258 void
2259 set_needs_symtab_index()
2260 { this->needs_symtab_index_ = true; }
2261
2262 // Return whether we need a symtab index.
2263 bool
2264 needs_symtab_index() const
2265 { return this->needs_symtab_index_; }
2266
2267 // Get the symtab index.
2268 unsigned int
2269 symtab_index() const
2270 {
a3ad94ed 2271 gold_assert(this->symtab_index_ != 0);
c06b7b0b
ILT
2272 return this->symtab_index_;
2273 }
2274
2275 // Set the symtab index.
2276 void
2277 set_symtab_index(unsigned int index)
2278 {
a3ad94ed 2279 gold_assert(index != 0);
c06b7b0b
ILT
2280 this->symtab_index_ = index;
2281 }
2282
2283 // Indicate that we need a dynsym index.
2284 void
2285 set_needs_dynsym_index()
2286 { this->needs_dynsym_index_ = true; }
2287
2288 // Return whether we need a dynsym index.
2289 bool
2290 needs_dynsym_index() const
2291 { return this->needs_dynsym_index_; }
2292
2293 // Get the dynsym index.
2294 unsigned int
2295 dynsym_index() const
2296 {
a3ad94ed 2297 gold_assert(this->dynsym_index_ != 0);
c06b7b0b
ILT
2298 return this->dynsym_index_;
2299 }
2300
2301 // Set the dynsym index.
2302 void
2303 set_dynsym_index(unsigned int index)
2304 {
a3ad94ed 2305 gold_assert(index != 0);
c06b7b0b
ILT
2306 this->dynsym_index_ = index;
2307 }
2308
2fd32231
ILT
2309 // Return whether the input sections sections attachd to this output
2310 // section may require sorting. This is used to handle constructor
2311 // priorities compatibly with GNU ld.
2312 bool
2313 may_sort_attached_input_sections() const
2314 { return this->may_sort_attached_input_sections_; }
2315
2316 // Record that the input sections attached to this output section
2317 // may require sorting.
2318 void
2319 set_may_sort_attached_input_sections()
2320 { this->may_sort_attached_input_sections_ = true; }
2321
2322 // Return whether the input sections attached to this output section
2323 // require sorting. This is used to handle constructor priorities
2324 // compatibly with GNU ld.
2325 bool
2326 must_sort_attached_input_sections() const
2327 { return this->must_sort_attached_input_sections_; }
2328
2329 // Record that the input sections attached to this output section
2330 // require sorting.
2331 void
2332 set_must_sort_attached_input_sections()
2333 { this->must_sort_attached_input_sections_ = true; }
2334
9f1d377b
ILT
2335 // Return whether this section holds relro data--data which has
2336 // dynamic relocations but which may be marked read-only after the
2337 // dynamic relocations have been completed.
2338 bool
2339 is_relro() const
2340 { return this->is_relro_; }
2341
2342 // Record that this section holds relro data.
2343 void
2344 set_is_relro()
2345 { this->is_relro_ = true; }
2346
2d924fd9
ILT
2347 // Record that this section does not hold relro data.
2348 void
2349 clear_is_relro()
2350 { this->is_relro_ = false; }
2351
9f1d377b
ILT
2352 // True if this section holds relro local data--relro data for which
2353 // the dynamic relocations are all RELATIVE relocations.
2354 bool
2355 is_relro_local() const
2356 { return this->is_relro_local_; }
2357
2358 // Record that this section holds relro local data.
2359 void
2360 set_is_relro_local()
2361 { this->is_relro_local_ = true; }
2362
8a5e3e08
ILT
2363 // True if this is a small section: a section which holds small
2364 // variables.
2365 bool
2366 is_small_section() const
2367 { return this->is_small_section_; }
2368
2369 // Record that this is a small section.
2370 void
2371 set_is_small_section()
2372 { this->is_small_section_ = true; }
2373
2374 // True if this is a large section: a section which holds large
2375 // variables.
2376 bool
2377 is_large_section() const
2378 { return this->is_large_section_; }
2379
2380 // Record that this is a large section.
2381 void
2382 set_is_large_section()
2383 { this->is_large_section_ = true; }
2384
2385 // True if this is a large data (not BSS) section.
2386 bool
2387 is_large_data_section()
2388 { return this->is_large_section_ && this->type_ != elfcpp::SHT_NOBITS; }
2389
730cdc88
ILT
2390 // Return whether this section should be written after all the input
2391 // sections are complete.
2392 bool
2393 after_input_sections() const
2394 { return this->after_input_sections_; }
2395
2396 // Record that this section should be written after all the input
2397 // sections are complete.
2398 void
2399 set_after_input_sections()
2400 { this->after_input_sections_ = true; }
2401
27bc2bce
ILT
2402 // Return whether this section requires postprocessing after all
2403 // relocations have been applied.
2404 bool
2405 requires_postprocessing() const
2406 { return this->requires_postprocessing_; }
2407
96803768
ILT
2408 // If a section requires postprocessing, return the buffer to use.
2409 unsigned char*
2410 postprocessing_buffer() const
2411 {
2412 gold_assert(this->postprocessing_buffer_ != NULL);
2413 return this->postprocessing_buffer_;
2414 }
2415
2416 // If a section requires postprocessing, create the buffer to use.
27bc2bce 2417 void
96803768
ILT
2418 create_postprocessing_buffer();
2419
2420 // If a section requires postprocessing, this is the size of the
2421 // buffer to which relocations should be applied.
2422 off_t
2423 postprocessing_buffer_size() const
2424 { return this->current_data_size_for_child(); }
27bc2bce 2425
755ab8af
ILT
2426 // Modify the section name. This is only permitted for an
2427 // unallocated section, and only before the size has been finalized.
2428 // Otherwise the name will not get into Layout::namepool_.
2429 void
2430 set_name(const char* newname)
2431 {
2432 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
2433 gold_assert(!this->is_data_size_valid());
2434 this->name_ = newname;
2435 }
2436
730cdc88
ILT
2437 // Return whether the offset OFFSET in the input section SHNDX in
2438 // object OBJECT is being included in the link.
2439 bool
2440 is_input_address_mapped(const Relobj* object, unsigned int shndx,
2441 off_t offset) const;
2442
2443 // Return the offset within the output section of OFFSET relative to
2444 // the start of input section SHNDX in object OBJECT.
8383303e
ILT
2445 section_offset_type
2446 output_offset(const Relobj* object, unsigned int shndx,
2447 section_offset_type offset) const;
730cdc88 2448
b8e6aad9
ILT
2449 // Return the output virtual address of OFFSET relative to the start
2450 // of input section SHNDX in object OBJECT.
2451 uint64_t
2452 output_address(const Relobj* object, unsigned int shndx,
2453 off_t offset) const;
2454
e29e076a
ILT
2455 // Look for the merged section for input section SHNDX in object
2456 // OBJECT. If found, return true, and set *ADDR to the address of
2457 // the start of the merged section. This is not necessary the
2458 // output offset corresponding to input offset 0 in the section,
2459 // since the section may be mapped arbitrarily.
2460 bool
2461 find_starting_output_address(const Relobj* object, unsigned int shndx,
2462 uint64_t* addr) const;
a9a60db6 2463
a445fddf
ILT
2464 // Record that this output section was found in the SECTIONS clause
2465 // of a linker script.
2466 void
2467 set_found_in_sections_clause()
2468 { this->found_in_sections_clause_ = true; }
2469
2470 // Return whether this output section was found in the SECTIONS
2471 // clause of a linker script.
2472 bool
2473 found_in_sections_clause() const
2474 { return this->found_in_sections_clause_; }
2475
27bc2bce
ILT
2476 // Write the section header into *OPHDR.
2477 template<int size, bool big_endian>
2478 void
2479 write_header(const Layout*, const Stringpool*,
2480 elfcpp::Shdr_write<size, big_endian>*) const;
2481
a445fddf
ILT
2482 // The next few calls are for linker script support.
2483
20e6d0d6
DK
2484 // We need to export the input sections to linker scripts. Previously
2485 // we export a pair of Relobj pointer and section index. We now need to
2486 // handle relaxed input sections as well. So we use this class.
2487 class Simple_input_section
2488 {
2489 private:
2490 static const unsigned int invalid_shndx = static_cast<unsigned int>(-1);
2491
2492 public:
2493 Simple_input_section(Relobj *relobj, unsigned int shndx)
2494 : shndx_(shndx)
2495 {
2496 gold_assert(shndx != invalid_shndx);
2497 this->u_.relobj = relobj;
2498 }
2499
2500 Simple_input_section(Output_relaxed_input_section* section)
2501 : shndx_(invalid_shndx)
2502 { this->u_.relaxed_input_section = section; }
2503
2504 // Whether this is a relaxed section.
2505 bool
2506 is_relaxed_input_section() const
2507 { return this->shndx_ == invalid_shndx; }
2508
2509 // Return object of an input section.
2510 Relobj*
2511 relobj() const
2512 {
2513 return ((this->shndx_ != invalid_shndx)
2514 ? this->u_.relobj
2515 : this->u_.relaxed_input_section->relobj());
2516 }
2517
2518 // Return index of an input section.
2519 unsigned int
2520 shndx() const
2521 {
2522 return ((this->shndx_ != invalid_shndx)
2523 ? this->shndx_
2524 : this->u_.relaxed_input_section->shndx());
2525 }
2526
2527 // Return the Output_relaxed_input_section object of a relaxed section.
2528 Output_relaxed_input_section*
2529 relaxed_input_section() const
2530 {
2531 gold_assert(this->shndx_ == invalid_shndx);
2532 return this->u_.relaxed_input_section;
2533 }
2534
2535 private:
2536 // Pointer to either an Relobj or an Output_relaxed_input_section.
2537 union
2538 {
2539 Relobj* relobj;
2540 Output_relaxed_input_section* relaxed_input_section;
2541 } u_;
2542 // Section index for an non-relaxed section or invalid_shndx for
2543 // a relaxed section.
2544 unsigned int shndx_;
2545 };
2546
a445fddf
ILT
2547 // Store the list of input sections for this Output_section into the
2548 // list passed in. This removes the input sections, leaving only
2549 // any Output_section_data elements. This returns the size of those
2550 // Output_section_data elements. ADDRESS is the address of this
2551 // output section. FILL is the fill value to use, in case there are
2552 // any spaces between the remaining Output_section_data elements.
2553 uint64_t
2554 get_input_sections(uint64_t address, const std::string& fill,
20e6d0d6 2555 std::list<Simple_input_section>*);
a445fddf
ILT
2556
2557 // Add an input section from a script.
2558 void
20e6d0d6 2559 add_input_section_for_script(const Simple_input_section& input_section,
a445fddf
ILT
2560 off_t data_size, uint64_t addralign);
2561
2562 // Set the current size of the output section.
2563 void
2564 set_current_data_size(off_t size)
2565 { this->set_current_data_size_for_child(size); }
2566
2567 // Get the current size of the output section.
2568 off_t
2569 current_data_size() const
2570 { return this->current_data_size_for_child(); }
2571
2572 // End of linker script support.
2573
20e6d0d6
DK
2574 // Save states before doing section layout.
2575 // This is used for relaxation.
2576 void
2577 save_states();
2578
2579 // Restore states prior to section layout.
2580 void
2581 restore_states();
2582
c0a62865
DK
2583 // Convert existing input sections to relaxed input sections.
2584 void
2585 convert_input_sections_to_relaxed_sections(
2586 const std::vector<Output_relaxed_input_section*>& sections);
2587
38c5e8b4
ILT
2588 // Print merge statistics to stderr.
2589 void
2590 print_merge_stats();
2591
27bc2bce 2592 protected:
77e65537
ILT
2593 // Return the output section--i.e., the object itself.
2594 Output_section*
2595 do_output_section()
2596 { return this; }
2597
27bc2bce
ILT
2598 // Return the section index in the output file.
2599 unsigned int
2600 do_out_shndx() const
2601 {
2602 gold_assert(this->out_shndx_ != -1U);
2603 return this->out_shndx_;
2604 }
2605
2606 // Set the output section index.
2607 void
2608 do_set_out_shndx(unsigned int shndx)
2609 {
a445fddf 2610 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
27bc2bce
ILT
2611 this->out_shndx_ = shndx;
2612 }
2613
2614 // Set the final data size of the Output_section. For a typical
ead1e424 2615 // Output_section, there is nothing to do, but if there are any
27bc2bce 2616 // Output_section_data objects we need to set their final addresses
ead1e424 2617 // here.
96803768 2618 virtual void
27bc2bce 2619 set_final_data_size();
ead1e424 2620
a445fddf
ILT
2621 // Reset the address and file offset.
2622 void
2623 do_reset_address_and_file_offset();
2624
20e6d0d6
DK
2625 // Return true if address and file offset already have reset values. In
2626 // other words, calling reset_address_and_file_offset will not change them.
2627 bool
2628 do_address_and_file_offset_have_reset_values() const;
2629
54dc6425 2630 // Write the data to the file. For a typical Output_section, this
ead1e424
ILT
2631 // does nothing: the data is written out by calling Object::Relocate
2632 // on each input object. But if there are any Output_section_data
2633 // objects we do need to write them out here.
96803768 2634 virtual void
ead1e424 2635 do_write(Output_file*);
54dc6425 2636
75f65a3e
ILT
2637 // Return the address alignment--function required by parent class.
2638 uint64_t
2639 do_addralign() const
2640 { return this->addralign_; }
2641
a445fddf
ILT
2642 // Return whether there is a load address.
2643 bool
2644 do_has_load_address() const
2645 { return this->has_load_address_; }
2646
2647 // Return the load address.
2648 uint64_t
2649 do_load_address() const
2650 {
2651 gold_assert(this->has_load_address_);
2652 return this->load_address_;
2653 }
2654
75f65a3e
ILT
2655 // Return whether this is an Output_section.
2656 bool
2657 do_is_section() const
2658 { return true; }
2659
54dc6425
ILT
2660 // Return whether this is a section of the specified type.
2661 bool
75f65a3e 2662 do_is_section_type(elfcpp::Elf_Word type) const
54dc6425
ILT
2663 { return this->type_ == type; }
2664
2665 // Return whether the specified section flag is set.
2666 bool
75f65a3e 2667 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
54dc6425
ILT
2668 { return (this->flags_ & flag) != 0; }
2669
7bf1f802
ILT
2670 // Set the TLS offset. Called only for SHT_TLS sections.
2671 void
2672 do_set_tls_offset(uint64_t tls_base);
2673
2674 // Return the TLS offset, relative to the base of the TLS segment.
2675 // Valid only for SHT_TLS sections.
2676 uint64_t
2677 do_tls_offset() const
2678 { return this->tls_offset_; }
2679
96803768
ILT
2680 // This may be implemented by a child class.
2681 virtual void
2682 do_finalize_name(Layout*)
2683 { }
2684
7d9e3d98
ILT
2685 // Print to the map file.
2686 virtual void
2687 do_print_to_mapfile(Mapfile*) const;
2688
96803768
ILT
2689 // Record that this section requires postprocessing after all
2690 // relocations have been applied. This is called by a child class.
2691 void
2692 set_requires_postprocessing()
2693 {
2694 this->requires_postprocessing_ = true;
2695 this->after_input_sections_ = true;
2696 }
2697
2698 // Write all the data of an Output_section into the postprocessing
2699 // buffer.
2700 void
2701 write_to_postprocessing_buffer();
2702
ead1e424
ILT
2703 // In some cases we need to keep a list of the input sections
2704 // associated with this output section. We only need the list if we
2705 // might have to change the offsets of the input section within the
2706 // output section after we add the input section. The ordinary
2707 // input sections will be written out when we process the object
2708 // file, and as such we don't need to track them here. We do need
2709 // to track Output_section_data objects here. We store instances of
2710 // this structure in a std::vector, so it must be a POD. There can
2711 // be many instances of this structure, so we use a union to save
2712 // some space.
2713 class Input_section
2714 {
2715 public:
2716 Input_section()
b8e6aad9
ILT
2717 : shndx_(0), p2align_(0)
2718 {
2719 this->u1_.data_size = 0;
2720 this->u2_.object = NULL;
2721 }
ead1e424 2722
b8e6aad9 2723 // For an ordinary input section.
f6ce93d6 2724 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
ead1e424
ILT
2725 uint64_t addralign)
2726 : shndx_(shndx),
b8e6aad9 2727 p2align_(ffsll(static_cast<long long>(addralign)))
ead1e424 2728 {
b8e6aad9
ILT
2729 gold_assert(shndx != OUTPUT_SECTION_CODE
2730 && shndx != MERGE_DATA_SECTION_CODE
20e6d0d6
DK
2731 && shndx != MERGE_STRING_SECTION_CODE
2732 && shndx != RELAXED_INPUT_SECTION_CODE);
b8e6aad9
ILT
2733 this->u1_.data_size = data_size;
2734 this->u2_.object = object;
ead1e424
ILT
2735 }
2736
b8e6aad9 2737 // For a non-merge output section.
ead1e424 2738 Input_section(Output_section_data* posd)
f34787f8 2739 : shndx_(OUTPUT_SECTION_CODE), p2align_(0)
b8e6aad9
ILT
2740 {
2741 this->u1_.data_size = 0;
2742 this->u2_.posd = posd;
2743 }
2744
2745 // For a merge section.
2746 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2747 : shndx_(is_string
2748 ? MERGE_STRING_SECTION_CODE
2749 : MERGE_DATA_SECTION_CODE),
f34787f8 2750 p2align_(0)
b8e6aad9
ILT
2751 {
2752 this->u1_.entsize = entsize;
2753 this->u2_.posd = posd;
2754 }
ead1e424 2755
20e6d0d6
DK
2756 // For a relaxed input section.
2757 Input_section(Output_relaxed_input_section *psection)
2758 : shndx_(RELAXED_INPUT_SECTION_CODE), p2align_(0)
2759 {
2760 this->u1_.data_size = 0;
2761 this->u2_.poris = psection;
2762 }
2763
ead1e424
ILT
2764 // The required alignment.
2765 uint64_t
2766 addralign() const
a3ad94ed 2767 {
f34787f8
ILT
2768 if (!this->is_input_section())
2769 return this->u2_.posd->addralign();
a3ad94ed
ILT
2770 return (this->p2align_ == 0
2771 ? 0
2772 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2773 }
ead1e424
ILT
2774
2775 // Return the required size.
2776 off_t
2777 data_size() const;
2778
a445fddf
ILT
2779 // Whether this is an input section.
2780 bool
2781 is_input_section() const
2782 {
2783 return (this->shndx_ != OUTPUT_SECTION_CODE
2784 && this->shndx_ != MERGE_DATA_SECTION_CODE
20e6d0d6
DK
2785 && this->shndx_ != MERGE_STRING_SECTION_CODE
2786 && this->shndx_ != RELAXED_INPUT_SECTION_CODE);
a445fddf
ILT
2787 }
2788
b8e6aad9
ILT
2789 // Return whether this is a merge section which matches the
2790 // parameters.
2791 bool
87f95776
ILT
2792 is_merge_section(bool is_string, uint64_t entsize,
2793 uint64_t addralign) const
b8e6aad9
ILT
2794 {
2795 return (this->shndx_ == (is_string
2796 ? MERGE_STRING_SECTION_CODE
2797 : MERGE_DATA_SECTION_CODE)
87f95776
ILT
2798 && this->u1_.entsize == entsize
2799 && this->addralign() == addralign);
b8e6aad9
ILT
2800 }
2801
20e6d0d6
DK
2802 // Return whether this is a relaxed input section.
2803 bool
2804 is_relaxed_input_section() const
2805 { return this->shndx_ == RELAXED_INPUT_SECTION_CODE; }
2806
2807 // Return whether this is a generic Output_section_data.
2808 bool
2809 is_output_section_data() const
2810 {
2811 return this->shndx_ == OUTPUT_SECTION_CODE;
2812 }
2813
a445fddf
ILT
2814 // Return the object for an input section.
2815 Relobj*
2816 relobj() const
2817 {
c0a62865
DK
2818 if (this->is_input_section())
2819 return this->u2_.object;
2820 else if (this->is_relaxed_input_section())
2821 return this->u2_.poris->relobj();
2822 else
2823 gold_unreachable();
a445fddf
ILT
2824 }
2825
2826 // Return the input section index for an input section.
2827 unsigned int
2828 shndx() const
2829 {
c0a62865
DK
2830 if (this->is_input_section())
2831 return this->shndx_;
2832 else if (this->is_relaxed_input_section())
2833 return this->u2_.poris->shndx();
2834 else
2835 gold_unreachable();
a445fddf
ILT
2836 }
2837
20e6d0d6
DK
2838 // For non-input-sections, return the associated Output_section_data
2839 // object.
2840 Output_section_data*
2841 output_section_data() const
2842 {
2843 gold_assert(!this->is_input_section());
2844 return this->u2_.posd;
2845 }
2846
2847 // Return the Output_relaxed_input_section object.
2848 Output_relaxed_input_section*
2849 relaxed_input_section() const
2850 {
2851 gold_assert(this->is_relaxed_input_section());
2852 return this->u2_.poris;
2853 }
2854
b8e6aad9
ILT
2855 // Set the output section.
2856 void
2857 set_output_section(Output_section* os)
2858 {
2859 gold_assert(!this->is_input_section());
20e6d0d6
DK
2860 Output_section_data *posd =
2861 this->is_relaxed_input_section() ? this->u2_.poris : this->u2_.posd;
2862 posd->set_output_section(os);
b8e6aad9
ILT
2863 }
2864
ead1e424 2865 // Set the address and file offset. This is called during
96803768
ILT
2866 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2867 // the enclosing section.
ead1e424 2868 void
96803768
ILT
2869 set_address_and_file_offset(uint64_t address, off_t file_offset,
2870 off_t section_file_offset);
ead1e424 2871
a445fddf
ILT
2872 // Reset the address and file offset.
2873 void
2874 reset_address_and_file_offset();
2875
96803768
ILT
2876 // Finalize the data size.
2877 void
2878 finalize_data_size();
9a0910c3 2879
b8e6aad9
ILT
2880 // Add an input section, for SHF_MERGE sections.
2881 bool
2882 add_input_section(Relobj* object, unsigned int shndx)
2883 {
2884 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2885 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2886 return this->u2_.posd->add_input_section(object, shndx);
2887 }
2888
2889 // Given an input OBJECT, an input section index SHNDX within that
2890 // object, and an OFFSET relative to the start of that input
730cdc88 2891 // section, return whether or not the output offset is known. If
1e983657
ILT
2892 // this function returns true, it sets *POUTPUT to the offset in
2893 // the output section, relative to the start of the input section
2894 // in the output section. *POUTPUT may be different from OFFSET
2895 // for a merged section.
b8e6aad9 2896 bool
8383303e
ILT
2897 output_offset(const Relobj* object, unsigned int shndx,
2898 section_offset_type offset,
2899 section_offset_type *poutput) const;
b8e6aad9 2900
a9a60db6
ILT
2901 // Return whether this is the merge section for the input section
2902 // SHNDX in OBJECT.
2903 bool
2904 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2905
ead1e424
ILT
2906 // Write out the data. This does nothing for an input section.
2907 void
2908 write(Output_file*);
2909
96803768
ILT
2910 // Write the data to a buffer. This does nothing for an input
2911 // section.
2912 void
2913 write_to_buffer(unsigned char*);
2914
7d9e3d98
ILT
2915 // Print to a map file.
2916 void
2917 print_to_mapfile(Mapfile*) const;
2918
38c5e8b4
ILT
2919 // Print statistics about merge sections to stderr.
2920 void
2921 print_merge_stats(const char* section_name)
2922 {
2923 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2924 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2925 this->u2_.posd->print_merge_stats(section_name);
2926 }
2927
ead1e424 2928 private:
b8e6aad9
ILT
2929 // Code values which appear in shndx_. If the value is not one of
2930 // these codes, it is the input section index in the object file.
2931 enum
2932 {
2933 // An Output_section_data.
2934 OUTPUT_SECTION_CODE = -1U,
2935 // An Output_section_data for an SHF_MERGE section with
2936 // SHF_STRINGS not set.
2937 MERGE_DATA_SECTION_CODE = -2U,
2938 // An Output_section_data for an SHF_MERGE section with
2939 // SHF_STRINGS set.
20e6d0d6
DK
2940 MERGE_STRING_SECTION_CODE = -3U,
2941 // An Output_section_data for a relaxed input section.
2942 RELAXED_INPUT_SECTION_CODE = -4U
b8e6aad9
ILT
2943 };
2944
b8e6aad9
ILT
2945 // For an ordinary input section, this is the section index in the
2946 // input file. For an Output_section_data, this is
2947 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2948 // MERGE_STRING_SECTION_CODE.
ead1e424
ILT
2949 unsigned int shndx_;
2950 // The required alignment, stored as a power of 2.
2951 unsigned int p2align_;
ead1e424
ILT
2952 union
2953 {
b8e6aad9
ILT
2954 // For an ordinary input section, the section size.
2955 off_t data_size;
20e6d0d6
DK
2956 // For OUTPUT_SECTION_CODE or RELAXED_INPUT_SECTION_CODE, this is not
2957 // used. For MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
b8e6aad9
ILT
2958 // entity size.
2959 uint64_t entsize;
2960 } u1_;
2961 union
2962 {
2963 // For an ordinary input section, the object which holds the
ead1e424 2964 // input section.
f6ce93d6 2965 Relobj* object;
b8e6aad9
ILT
2966 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2967 // MERGE_STRING_SECTION_CODE, the data.
ead1e424 2968 Output_section_data* posd;
20e6d0d6
DK
2969 // For RELAXED_INPUT_SECTION_CODE, the data.
2970 Output_relaxed_input_section* poris;
b8e6aad9 2971 } u2_;
ead1e424
ILT
2972 };
2973
2974 typedef std::vector<Input_section> Input_section_list;
2975
c0a62865
DK
2976 // Allow a child class to access the input sections.
2977 const Input_section_list&
2978 input_sections() const
2979 { return this->input_sections_; }
2980
2981 private:
20e6d0d6
DK
2982 // We only save enough information to undo the effects of section layout.
2983 class Checkpoint_output_section
2984 {
2985 public:
2986 Checkpoint_output_section(uint64_t addralign, elfcpp::Elf_Xword flags,
2987 const Input_section_list& input_sections,
2988 off_t first_input_offset,
2989 bool attached_input_sections_are_sorted)
2990 : addralign_(addralign), flags_(flags),
2991 input_sections_(input_sections),
2992 input_sections_size_(input_sections_.size()),
2993 input_sections_copy_(), first_input_offset_(first_input_offset),
2994 attached_input_sections_are_sorted_(attached_input_sections_are_sorted)
2995 { }
2996
2997 virtual
2998 ~Checkpoint_output_section()
2999 { }
3000
3001 // Return the address alignment.
3002 uint64_t
3003 addralign() const
3004 { return this->addralign_; }
3005
3006 // Return the section flags.
3007 elfcpp::Elf_Xword
3008 flags() const
3009 { return this->flags_; }
3010
3011 // Return a reference to the input section list copy.
c0a62865
DK
3012 Input_section_list*
3013 input_sections()
3014 { return &this->input_sections_copy_; }
20e6d0d6
DK
3015
3016 // Return the size of input_sections at the time when checkpoint is
3017 // taken.
3018 size_t
3019 input_sections_size() const
3020 { return this->input_sections_size_; }
3021
3022 // Whether input sections are copied.
3023 bool
3024 input_sections_saved() const
3025 { return this->input_sections_copy_.size() == this->input_sections_size_; }
3026
3027 off_t
3028 first_input_offset() const
3029 { return this->first_input_offset_; }
3030
3031 bool
3032 attached_input_sections_are_sorted() const
3033 { return this->attached_input_sections_are_sorted_; }
3034
3035 // Save input sections.
3036 void
3037 save_input_sections()
3038 {
3039 this->input_sections_copy_.reserve(this->input_sections_size_);
3040 this->input_sections_copy_.clear();
3041 Input_section_list::const_iterator p = this->input_sections_.begin();
3042 gold_assert(this->input_sections_size_ >= this->input_sections_.size());
3043 for(size_t i = 0; i < this->input_sections_size_ ; i++, ++p)
3044 this->input_sections_copy_.push_back(*p);
3045 }
3046
3047 private:
3048 // The section alignment.
3049 uint64_t addralign_;
3050 // The section flags.
3051 elfcpp::Elf_Xword flags_;
3052 // Reference to the input sections to be checkpointed.
3053 const Input_section_list& input_sections_;
3054 // Size of the checkpointed portion of input_sections_;
3055 size_t input_sections_size_;
3056 // Copy of input sections.
3057 Input_section_list input_sections_copy_;
3058 // The offset of the first entry in input_sections_.
3059 off_t first_input_offset_;
3060 // True if the input sections attached to this output section have
3061 // already been sorted.
3062 bool attached_input_sections_are_sorted_;
3063 };
3064
2fd32231
ILT
3065 // This class is used to sort the input sections.
3066 class Input_section_sort_entry;
3067
3068 // This is the sort comparison function.
3069 struct Input_section_sort_compare
3070 {
3071 bool
3072 operator()(const Input_section_sort_entry&,
3073 const Input_section_sort_entry&) const;
3074 };
3075
c51e6221 3076 // Fill data. This is used to fill in data between input sections.
a445fddf
ILT
3077 // It is also used for data statements (BYTE, WORD, etc.) in linker
3078 // scripts. When we have to keep track of the input sections, we
3079 // can use an Output_data_const, but we don't want to have to keep
3080 // track of input sections just to implement fills.
c51e6221
ILT
3081 class Fill
3082 {
3083 public:
3084 Fill(off_t section_offset, off_t length)
a445fddf
ILT
3085 : section_offset_(section_offset),
3086 length_(convert_to_section_size_type(length))
c51e6221
ILT
3087 { }
3088
3089 // Return section offset.
3090 off_t
3091 section_offset() const
3092 { return this->section_offset_; }
3093
3094 // Return fill length.
a445fddf 3095 section_size_type
c51e6221
ILT
3096 length() const
3097 { return this->length_; }
3098
3099 private:
3100 // The offset within the output section.
3101 off_t section_offset_;
3102 // The length of the space to fill.
a445fddf 3103 section_size_type length_;
c51e6221
ILT
3104 };
3105
3106 typedef std::vector<Fill> Fill_list;
3107
c0a62865
DK
3108 // This class describes properties of merge data sections. It is used
3109 // as a key type for maps.
3110 class Merge_section_properties
3111 {
3112 public:
3113 Merge_section_properties(bool is_string, uint64_t entsize,
3114 uint64_t addralign)
3115 : is_string_(is_string), entsize_(entsize), addralign_(addralign)
3116 { }
3117
3118 // Whether this equals to another Merge_section_properties MSP.
3119 bool
3120 eq(const Merge_section_properties& msp) const
3121 {
3122 return ((this->is_string_ == msp.is_string_)
3123 && (this->entsize_ == msp.entsize_)
3124 && (this->addralign_ == msp.addralign_));
3125 }
3126
3127 // Compute a hash value for this using 64-bit FNV-1a hash.
3128 size_t
3129 hash_value() const
3130 {
3131 uint64_t h = 14695981039346656037ULL; // FNV offset basis.
3132 uint64_t prime = 1099511628211ULL;
3133 h = (h ^ static_cast<uint64_t>(this->is_string_)) * prime;
3134 h = (h ^ static_cast<uint64_t>(this->entsize_)) * prime;
3135 h = (h ^ static_cast<uint64_t>(this->addralign_)) * prime;
3136 return h;
3137 }
3138
3139 // Functors for associative containers.
3140 struct equal_to
3141 {
3142 bool
3143 operator()(const Merge_section_properties& msp1,
3144 const Merge_section_properties& msp2) const
3145 { return msp1.eq(msp2); }
3146 };
3147
3148 struct hash
3149 {
3150 size_t
3151 operator()(const Merge_section_properties& msp) const
3152 { return msp.hash_value(); }
3153 };
3154
3155 private:
3156 // Whether this merge data section is for strings.
3157 bool is_string_;
3158 // Entsize of this merge data section.
3159 uint64_t entsize_;
3160 // Address alignment.
3161 uint64_t addralign_;
3162 };
3163
3164 // Map that link Merge_section_properties to Output_merge_base.
3165 typedef Unordered_map<Merge_section_properties, Output_merge_base*,
3166 Merge_section_properties::hash,
3167 Merge_section_properties::equal_to>
3168 Merge_section_by_properties_map;
3169
3170 // Map that link Input_section_specifier to Output_section_data.
3171 typedef Unordered_map<Input_section_specifier, Output_section_data*,
3172 Input_section_specifier::hash,
3173 Input_section_specifier::equal_to>
3174 Output_section_data_by_input_section_map;
3175
3176 // Map used during relaxation of existing sections. This map
3177 // an input section specifier to an input section list index.
3178 // We assume that Input_section_list is a vector.
3179 typedef Unordered_map<Input_section_specifier, size_t,
3180 Input_section_specifier::hash,
3181 Input_section_specifier::equal_to>
3182 Relaxation_map;
3183
b8e6aad9
ILT
3184 // Add a new output section by Input_section.
3185 void
3186 add_output_section_data(Input_section*);
3187
3188 // Add an SHF_MERGE input section. Returns true if the section was
3189 // handled.
3190 bool
3191 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
96803768 3192 uint64_t entsize, uint64_t addralign);
b8e6aad9
ILT
3193
3194 // Add an output SHF_MERGE section POSD to this output section.
3195 // IS_STRING indicates whether it is a SHF_STRINGS section, and
3196 // ENTSIZE is the entity size. This returns the entry added to
3197 // input_sections_.
3198 void
3199 add_output_merge_section(Output_section_data* posd, bool is_string,
3200 uint64_t entsize);
3201
2fd32231
ILT
3202 // Sort the attached input sections.
3203 void
3204 sort_attached_input_sections();
3205
c0a62865
DK
3206 // Find the merge section into which an input section with index SHNDX in
3207 // OBJECT has been added. Return NULL if none found.
3208 Output_section_data*
3209 find_merge_section(const Relobj* object, unsigned int shndx) const;
3210
3211 // Find a relaxed input section to an input section in OBJECT
3212 // with index SHNDX. Return NULL if none is found.
3213 const Output_section_data*
3214 find_relaxed_input_section(const Relobj* object, unsigned int shndx) const;
3215
3216 // Build a relaxation map.
3217 void
3218 build_relaxation_map(
3219 const Input_section_list& input_sections,
3220 size_t limit,
3221 Relaxation_map* map) const;
3222
3223 // Convert input sections in an input section list into relaxed sections.
3224 void
3225 convert_input_sections_in_list_to_relaxed_sections(
3226 const std::vector<Output_relaxed_input_section*>& relaxed_sections,
3227 const Relaxation_map& map,
3228 Input_section_list* input_sections);
3229
a2fb1b05
ILT
3230 // Most of these fields are only valid after layout.
3231
3232 // The name of the section. This will point into a Stringpool.
9a0910c3 3233 const char* name_;
75f65a3e 3234 // The section address is in the parent class.
a2fb1b05
ILT
3235 // The section alignment.
3236 uint64_t addralign_;
3237 // The section entry size.
3238 uint64_t entsize_;
a445fddf
ILT
3239 // The load address. This is only used when using a linker script
3240 // with a SECTIONS clause. The has_load_address_ field indicates
3241 // whether this field is valid.
3242 uint64_t load_address_;
75f65a3e 3243 // The file offset is in the parent class.
16649710 3244 // Set the section link field to the index of this section.
14b31740 3245 const Output_data* link_section_;
16649710 3246 // If link_section_ is NULL, this is the link field.
a2fb1b05 3247 unsigned int link_;
16649710 3248 // Set the section info field to the index of this section.
755ab8af 3249 const Output_section* info_section_;
6a74a719
ILT
3250 // If info_section_ is NULL, set the info field to the symbol table
3251 // index of this symbol.
3252 const Symbol* info_symndx_;
3253 // If info_section_ and info_symndx_ are NULL, this is the section
3254 // info field.
a2fb1b05
ILT
3255 unsigned int info_;
3256 // The section type.
27bc2bce 3257 const elfcpp::Elf_Word type_;
a2fb1b05 3258 // The section flags.
a445fddf 3259 elfcpp::Elf_Xword flags_;
61ba1cf9 3260 // The section index.
ead1e424 3261 unsigned int out_shndx_;
c06b7b0b
ILT
3262 // If there is a STT_SECTION for this output section in the normal
3263 // symbol table, this is the symbol index. This starts out as zero.
3264 // It is initialized in Layout::finalize() to be the index, or -1U
3265 // if there isn't one.
3266 unsigned int symtab_index_;
3267 // If there is a STT_SECTION for this output section in the dynamic
3268 // symbol table, this is the symbol index. This starts out as zero.
3269 // It is initialized in Layout::finalize() to be the index, or -1U
3270 // if there isn't one.
3271 unsigned int dynsym_index_;
ead1e424
ILT
3272 // The input sections. This will be empty in cases where we don't
3273 // need to keep track of them.
3274 Input_section_list input_sections_;
3275 // The offset of the first entry in input_sections_.
3276 off_t first_input_offset_;
c51e6221
ILT
3277 // The fill data. This is separate from input_sections_ because we
3278 // often will need fill sections without needing to keep track of
3279 // input sections.
3280 Fill_list fills_;
96803768
ILT
3281 // If the section requires postprocessing, this buffer holds the
3282 // section contents during relocation.
3283 unsigned char* postprocessing_buffer_;
c06b7b0b
ILT
3284 // Whether this output section needs a STT_SECTION symbol in the
3285 // normal symbol table. This will be true if there is a relocation
3286 // which needs it.
3287 bool needs_symtab_index_ : 1;
3288 // Whether this output section needs a STT_SECTION symbol in the
3289 // dynamic symbol table. This will be true if there is a dynamic
3290 // relocation which needs it.
3291 bool needs_dynsym_index_ : 1;
16649710
ILT
3292 // Whether the link field of this output section should point to the
3293 // normal symbol table.
3294 bool should_link_to_symtab_ : 1;
3295 // Whether the link field of this output section should point to the
3296 // dynamic symbol table.
3297 bool should_link_to_dynsym_ : 1;
730cdc88
ILT
3298 // Whether this section should be written after all the input
3299 // sections are complete.
3300 bool after_input_sections_ : 1;
27bc2bce
ILT
3301 // Whether this section requires post processing after all
3302 // relocations have been applied.
3303 bool requires_postprocessing_ : 1;
a445fddf
ILT
3304 // Whether an input section was mapped to this output section
3305 // because of a SECTIONS clause in a linker script.
3306 bool found_in_sections_clause_ : 1;
3307 // Whether this section has an explicitly specified load address.
3308 bool has_load_address_ : 1;
755ab8af
ILT
3309 // True if the info_section_ field means the section index of the
3310 // section, false if it means the symbol index of the corresponding
3311 // section symbol.
3312 bool info_uses_section_index_ : 1;
2fd32231
ILT
3313 // True if the input sections attached to this output section may
3314 // need sorting.
3315 bool may_sort_attached_input_sections_ : 1;
3316 // True if the input sections attached to this output section must
3317 // be sorted.
3318 bool must_sort_attached_input_sections_ : 1;
3319 // True if the input sections attached to this output section have
3320 // already been sorted.
3321 bool attached_input_sections_are_sorted_ : 1;
9f1d377b
ILT
3322 // True if this section holds relro data.
3323 bool is_relro_ : 1;
3324 // True if this section holds relro local data.
3325 bool is_relro_local_ : 1;
8a5e3e08
ILT
3326 // True if this is a small section.
3327 bool is_small_section_ : 1;
3328 // True if this is a large section.
3329 bool is_large_section_ : 1;
7bf1f802
ILT
3330 // For SHT_TLS sections, the offset of this section relative to the base
3331 // of the TLS segment.
3332 uint64_t tls_offset_;
20e6d0d6
DK
3333 // Saved checkpoint.
3334 Checkpoint_output_section* checkpoint_;
c0a62865
DK
3335 // Map from input sections to merge sections.
3336 Output_section_data_by_input_section_map merge_section_map_;
3337 // Map from merge section properties to merge_sections;
3338 Merge_section_by_properties_map merge_section_by_properties_map_;
3339 // Map from input sections to relaxed input sections. This is mutable
3340 // beacause it is udpated lazily. We may need to update it in a
3341 // const qualified method.
3342 mutable Output_section_data_by_input_section_map relaxed_input_section_map_;
3343 // Whether relaxed_input_section_map_ is valid.
3344 mutable bool is_relaxed_input_section_map_valid_;
3345 // Whether code-fills are generated at write.
3346 bool generate_code_fills_at_write_;
a2fb1b05
ILT
3347};
3348
3349// An output segment. PT_LOAD segments are built from collections of
3350// output sections. Other segments typically point within PT_LOAD
3351// segments, and are built directly as needed.
20e6d0d6
DK
3352//
3353// NOTE: We want to use the copy constructor for this class. During
3354// relaxation, we may try built the segments multiple times. We do
3355// that by copying the original segment list before lay-out, doing
3356// a trial lay-out and roll-back to the saved copied if we need to
3357// to the lay-out again.
a2fb1b05
ILT
3358
3359class Output_segment
3360{
3361 public:
3362 // Create an output segment, specifying the type and flags.
3363 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
3364
3365 // Return the virtual address.
3366 uint64_t
3367 vaddr() const
3368 { return this->vaddr_; }
3369
3370 // Return the physical address.
3371 uint64_t
3372 paddr() const
3373 { return this->paddr_; }
3374
3375 // Return the segment type.
3376 elfcpp::Elf_Word
3377 type() const
3378 { return this->type_; }
3379
3380 // Return the segment flags.
3381 elfcpp::Elf_Word
3382 flags() const
3383 { return this->flags_; }
3384
92e059d8
ILT
3385 // Return the memory size.
3386 uint64_t
3387 memsz() const
3388 { return this->memsz_; }
3389
ead1e424
ILT
3390 // Return the file size.
3391 off_t
3392 filesz() const
3393 { return this->filesz_; }
3394
516cb3d0
ILT
3395 // Return the file offset.
3396 off_t
3397 offset() const
3398 { return this->offset_; }
3399
8a5e3e08
ILT
3400 // Whether this is a segment created to hold large data sections.
3401 bool
3402 is_large_data_segment() const
3403 { return this->is_large_data_segment_; }
3404
3405 // Record that this is a segment created to hold large data
3406 // sections.
3407 void
3408 set_is_large_data_segment()
3409 { this->is_large_data_segment_ = true; }
3410
75f65a3e
ILT
3411 // Return the maximum alignment of the Output_data.
3412 uint64_t
a445fddf 3413 maximum_alignment();
75f65a3e 3414
a2fb1b05
ILT
3415 // Add an Output_section to this segment.
3416 void
01676dcd 3417 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags);
75f65a3e 3418
1650c4ff
ILT
3419 // Remove an Output_section from this segment. It is an error if it
3420 // is not present.
3421 void
3422 remove_output_section(Output_section* os);
3423
75f65a3e
ILT
3424 // Add an Output_data (which is not an Output_section) to the start
3425 // of this segment.
3426 void
3427 add_initial_output_data(Output_data*);
3428
756ac4a8
ILT
3429 // Return true if this segment has any sections which hold actual
3430 // data, rather than being a BSS section.
3431 bool
3432 has_any_data_sections() const
3433 { return !this->output_data_.empty(); }
3434
4f4c5f80
ILT
3435 // Return the number of dynamic relocations applied to this segment.
3436 unsigned int
3437 dynamic_reloc_count() const;
3438
a445fddf
ILT
3439 // Return the address of the first section.
3440 uint64_t
3441 first_section_load_address() const;
3442
3443 // Return whether the addresses have been set already.
3444 bool
3445 are_addresses_set() const
3446 { return this->are_addresses_set_; }
3447
3448 // Set the addresses.
3449 void
3450 set_addresses(uint64_t vaddr, uint64_t paddr)
3451 {
3452 this->vaddr_ = vaddr;
3453 this->paddr_ = paddr;
3454 this->are_addresses_set_ = true;
3455 }
3456
1c4f3631
ILT
3457 // Set the segment flags. This is only used if we have a PHDRS
3458 // clause which explicitly specifies the flags.
3459 void
3460 set_flags(elfcpp::Elf_Word flags)
3461 { this->flags_ = flags; }
3462
75f65a3e 3463 // Set the address of the segment to ADDR and the offset to *POFF
a445fddf
ILT
3464 // and set the addresses and offsets of all contained output
3465 // sections accordingly. Set the section indexes of all contained
3466 // output sections starting with *PSHNDX. If RESET is true, first
3467 // reset the addresses of the contained sections. Return the
3468 // address of the immediately following segment. Update *POFF and
3469 // *PSHNDX. This should only be called for a PT_LOAD segment.
75f65a3e 3470 uint64_t
96a2b4e4 3471 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
a445fddf 3472 unsigned int* pshndx);
75f65a3e 3473
0496d5e5
ILT
3474 // Set the minimum alignment of this segment. This may be adjusted
3475 // upward based on the section alignments.
3476 void
a445fddf
ILT
3477 set_minimum_p_align(uint64_t align)
3478 { this->min_p_align_ = align; }
0496d5e5 3479
75f65a3e
ILT
3480 // Set the offset of this segment based on the section. This should
3481 // only be called for a non-PT_LOAD segment.
3482 void
3483 set_offset();
3484
7bf1f802
ILT
3485 // Set the TLS offsets of the sections contained in the PT_TLS segment.
3486 void
3487 set_tls_offsets();
3488
75f65a3e
ILT
3489 // Return the number of output sections.
3490 unsigned int
3491 output_section_count() const;
a2fb1b05 3492
1c4f3631
ILT
3493 // Return the section attached to the list segment with the lowest
3494 // load address. This is used when handling a PHDRS clause in a
3495 // linker script.
3496 Output_section*
3497 section_with_lowest_load_address() const;
3498
61ba1cf9
ILT
3499 // Write the segment header into *OPHDR.
3500 template<int size, bool big_endian>
3501 void
ead1e424 3502 write_header(elfcpp::Phdr_write<size, big_endian>*);
61ba1cf9
ILT
3503
3504 // Write the section headers of associated sections into V.
3505 template<int size, bool big_endian>
3506 unsigned char*
16649710 3507 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
7d1a9ebb 3508 unsigned int* pshndx) const;
61ba1cf9 3509
7d9e3d98
ILT
3510 // Print the output sections in the map file.
3511 void
3512 print_sections_to_mapfile(Mapfile*) const;
3513
a2fb1b05 3514 private:
54dc6425 3515 typedef std::list<Output_data*> Output_data_list;
a2fb1b05 3516
ead1e424
ILT
3517 // Find the maximum alignment in an Output_data_list.
3518 static uint64_t
a445fddf 3519 maximum_alignment_list(const Output_data_list*);
ead1e424 3520
9f1d377b
ILT
3521 // Return whether the first data section is a relro section.
3522 bool
3523 is_first_section_relro() const;
3524
75f65a3e
ILT
3525 // Set the section addresses in an Output_data_list.
3526 uint64_t
96a2b4e4
ILT
3527 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
3528 uint64_t addr, off_t* poff, unsigned int* pshndx,
9f1d377b 3529 bool* in_tls, bool* in_relro);
75f65a3e
ILT
3530
3531 // Return the number of Output_sections in an Output_data_list.
3532 unsigned int
3533 output_section_count_list(const Output_data_list*) const;
3534
4f4c5f80
ILT
3535 // Return the number of dynamic relocs in an Output_data_list.
3536 unsigned int
3537 dynamic_reloc_count_list(const Output_data_list*) const;
3538
1c4f3631
ILT
3539 // Find the section with the lowest load address in an
3540 // Output_data_list.
3541 void
3542 lowest_load_address_in_list(const Output_data_list* pdl,
3543 Output_section** found,
3544 uint64_t* found_lma) const;
3545
61ba1cf9
ILT
3546 // Write the section headers in the list into V.
3547 template<int size, bool big_endian>
3548 unsigned char*
16649710
ILT
3549 write_section_headers_list(const Layout*, const Stringpool*,
3550 const Output_data_list*, unsigned char* v,
7d1a9ebb 3551 unsigned int* pshdx) const;
61ba1cf9 3552
7d9e3d98
ILT
3553 // Print a section list to the mapfile.
3554 void
3555 print_section_list_to_mapfile(Mapfile*, const Output_data_list*) const;
3556
20e6d0d6
DK
3557 // NOTE: We want to use the copy constructor. Currently, shallow copy
3558 // works for us so we do not need to write our own copy constructor.
3559
75f65a3e 3560 // The list of output data with contents attached to this segment.
54dc6425 3561 Output_data_list output_data_;
75f65a3e
ILT
3562 // The list of output data without contents attached to this segment.
3563 Output_data_list output_bss_;
a2fb1b05
ILT
3564 // The segment virtual address.
3565 uint64_t vaddr_;
3566 // The segment physical address.
3567 uint64_t paddr_;
3568 // The size of the segment in memory.
3569 uint64_t memsz_;
a445fddf
ILT
3570 // The maximum section alignment. The is_max_align_known_ field
3571 // indicates whether this has been finalized.
3572 uint64_t max_align_;
3573 // The required minimum value for the p_align field. This is used
3574 // for PT_LOAD segments. Note that this does not mean that
3575 // addresses should be aligned to this value; it means the p_paddr
3576 // and p_vaddr fields must be congruent modulo this value. For
3577 // non-PT_LOAD segments, the dynamic linker works more efficiently
3578 // if the p_align field has the more conventional value, although it
3579 // can align as needed.
3580 uint64_t min_p_align_;
a2fb1b05
ILT
3581 // The offset of the segment data within the file.
3582 off_t offset_;
3583 // The size of the segment data in the file.
3584 off_t filesz_;
3585 // The segment type;
3586 elfcpp::Elf_Word type_;
3587 // The segment flags.
3588 elfcpp::Elf_Word flags_;
a445fddf
ILT
3589 // Whether we have finalized max_align_.
3590 bool is_max_align_known_ : 1;
3591 // Whether vaddr and paddr were set by a linker script.
3592 bool are_addresses_set_ : 1;
8a5e3e08
ILT
3593 // Whether this segment holds large data sections.
3594 bool is_large_data_segment_ : 1;
a2fb1b05
ILT
3595};
3596
61ba1cf9 3597// This class represents the output file.
a2fb1b05
ILT
3598
3599class Output_file
3600{
3601 public:
14144f39 3602 Output_file(const char* name);
61ba1cf9 3603
516cb3d0
ILT
3604 // Indicate that this is a temporary file which should not be
3605 // output.
3606 void
3607 set_is_temporary()
3608 { this->is_temporary_ = true; }
3609
404c2abb
ILT
3610 // Try to open an existing file. Returns false if the file doesn't
3611 // exist, has a size of 0 or can't be mmaped. This method is
3612 // thread-unsafe.
3613 bool
3614 open_for_modification();
3615
61ba1cf9 3616 // Open the output file. FILE_SIZE is the final size of the file.
404c2abb
ILT
3617 // If the file already exists, it is deleted/truncated. This method
3618 // is thread-unsafe.
61ba1cf9
ILT
3619 void
3620 open(off_t file_size);
3621
404c2abb 3622 // Resize the output file. This method is thread-unsafe.
27bc2bce
ILT
3623 void
3624 resize(off_t file_size);
3625
c420411f 3626 // Close the output file (flushing all buffered data) and make sure
404c2abb 3627 // there are no errors. This method is thread-unsafe.
61ba1cf9
ILT
3628 void
3629 close();
3630
c549a694
ILT
3631 // Return the size of this file.
3632 off_t
3633 filesize()
3634 { return this->file_size_; }
3635
61ba1cf9
ILT
3636 // We currently always use mmap which makes the view handling quite
3637 // simple. In the future we may support other approaches.
a2fb1b05
ILT
3638
3639 // Write data to the output file.
3640 void
fe8718a4 3641 write(off_t offset, const void* data, size_t len)
61ba1cf9
ILT
3642 { memcpy(this->base_ + offset, data, len); }
3643
3644 // Get a buffer to use to write to the file, given the offset into
3645 // the file and the size.
3646 unsigned char*
fe8718a4 3647 get_output_view(off_t start, size_t size)
61ba1cf9 3648 {
8d32f935
ILT
3649 gold_assert(start >= 0
3650 && start + static_cast<off_t>(size) <= this->file_size_);
61ba1cf9
ILT
3651 return this->base_ + start;
3652 }
3653
3654 // VIEW must have been returned by get_output_view. Write the
3655 // buffer to the file, passing in the offset and the size.
3656 void
fe8718a4 3657 write_output_view(off_t, size_t, unsigned char*)
61ba1cf9
ILT
3658 { }
3659
730cdc88
ILT
3660 // Get a read/write buffer. This is used when we want to write part
3661 // of the file, read it in, and write it again.
3662 unsigned char*
fe8718a4 3663 get_input_output_view(off_t start, size_t size)
730cdc88
ILT
3664 { return this->get_output_view(start, size); }
3665
3666 // Write a read/write buffer back to the file.
3667 void
fe8718a4 3668 write_input_output_view(off_t, size_t, unsigned char*)
730cdc88
ILT
3669 { }
3670
3671 // Get a read buffer. This is used when we just want to read part
3672 // of the file back it in.
3673 const unsigned char*
fe8718a4 3674 get_input_view(off_t start, size_t size)
730cdc88
ILT
3675 { return this->get_output_view(start, size); }
3676
3677 // Release a read bfufer.
3678 void
fe8718a4 3679 free_input_view(off_t, size_t, const unsigned char*)
730cdc88
ILT
3680 { }
3681
61ba1cf9 3682 private:
404c2abb
ILT
3683 // Map the file into memory or, if that fails, allocate anonymous
3684 // memory.
27bc2bce
ILT
3685 void
3686 map();
3687
26736d8e 3688 // Allocate anonymous memory for the file.
404c2abb 3689 bool
26736d8e
ILT
3690 map_anonymous();
3691
404c2abb
ILT
3692 // Map the file into memory.
3693 bool
3694 map_no_anonymous();
3695
c420411f
ILT
3696 // Unmap the file from memory (and flush to disk buffers).
3697 void
3698 unmap();
3699
61ba1cf9
ILT
3700 // File name.
3701 const char* name_;
3702 // File descriptor.
3703 int o_;
3704 // File size.
3705 off_t file_size_;
3706 // Base of file mapped into memory.
3707 unsigned char* base_;
c420411f
ILT
3708 // True iff base_ points to a memory buffer rather than an output file.
3709 bool map_is_anonymous_;
516cb3d0
ILT
3710 // True if this is a temporary file which should not be output.
3711 bool is_temporary_;
a2fb1b05
ILT
3712};
3713
3714} // End namespace gold.
3715
3716#endif // !defined(GOLD_OUTPUT_H)
This page took 0.439096 seconds and 4 git commands to generate.