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