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