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