* linux-nat.c (linux_nat_attach): Add the pid we attached to, to
[deliverable/binutils-gdb.git] / gold / output.h
CommitLineData
a2fb1b05
ILT
1// output.h -- manage the output file for gold -*- C++ -*-
2
ebdbb458 3// Copyright 2006, 2007, 2008 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"
54dc6425 30#include "layout.h"
c06b7b0b 31#include "reloc-types.h"
a2fb1b05
ILT
32
33namespace gold
34{
35
61ba1cf9 36class General_options;
a2fb1b05 37class Object;
a3ad94ed 38class Symbol;
a2fb1b05 39class Output_file;
c06b7b0b 40class Output_section;
6a74a719 41class Relocatable_relocs;
a3ad94ed 42class Target;
54dc6425
ILT
43template<int size, bool big_endian>
44class Sized_target;
c06b7b0b
ILT
45template<int size, bool big_endian>
46class Sized_relobj;
54dc6425
ILT
47
48// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
49
50class Output_data
51{
52 public:
27bc2bce
ILT
53 explicit Output_data()
54 : address_(0), data_size_(0), offset_(-1),
55 is_address_valid_(false), is_data_size_valid_(false),
56 is_offset_valid_(false),
4f4c5f80 57 dynamic_reloc_count_(0)
a2fb1b05
ILT
58 { }
59
60 virtual
61 ~Output_data();
62
27bc2bce
ILT
63 // Return the address. For allocated sections, this is only valid
64 // after Layout::finalize is finished.
75f65a3e
ILT
65 uint64_t
66 address() const
27bc2bce
ILT
67 {
68 gold_assert(this->is_address_valid_);
69 return this->address_;
70 }
75f65a3e 71
27bc2bce
ILT
72 // Return the size of the data. For allocated sections, this must
73 // be valid after Layout::finalize calls set_address, but need not
74 // be valid before then.
a2fb1b05 75 off_t
75f65a3e 76 data_size() const
27bc2bce
ILT
77 {
78 gold_assert(this->is_data_size_valid_);
79 return this->data_size_;
80 }
75f65a3e 81
ead1e424 82 // Return the file offset. This is only valid after
27bc2bce
ILT
83 // Layout::finalize is finished. For some non-allocated sections,
84 // it may not be valid until near the end of the link.
75f65a3e
ILT
85 off_t
86 offset() const
27bc2bce
ILT
87 {
88 gold_assert(this->is_offset_valid_);
89 return this->offset_;
90 }
75f65a3e 91
a445fddf
ILT
92 // Reset the address and file offset. This essentially disables the
93 // sanity testing about duplicate and unknown settings.
94 void
95 reset_address_and_file_offset()
96 {
97 this->is_address_valid_ = false;
98 this->is_offset_valid_ = false;
99 this->is_data_size_valid_ = false;
100 this->do_reset_address_and_file_offset();
101 }
102
75f65a3e
ILT
103 // Return the required alignment.
104 uint64_t
105 addralign() const
106 { return this->do_addralign(); }
107
a445fddf
ILT
108 // Return whether this has a load address.
109 bool
110 has_load_address() const
111 { return this->do_has_load_address(); }
112
113 // Return the load address.
114 uint64_t
115 load_address() const
116 { return this->do_load_address(); }
117
75f65a3e
ILT
118 // Return whether this is an Output_section.
119 bool
120 is_section() const
121 { return this->do_is_section(); }
122
123 // Return whether this is an Output_section of the specified type.
124 bool
125 is_section_type(elfcpp::Elf_Word stt) const
126 { return this->do_is_section_type(stt); }
127
128 // Return whether this is an Output_section with the specified flag
129 // set.
130 bool
131 is_section_flag_set(elfcpp::Elf_Xword shf) const
132 { return this->do_is_section_flag_set(shf); }
133
77e65537
ILT
134 // Return the output section that this goes in, if there is one.
135 Output_section*
136 output_section()
137 { return this->do_output_section(); }
138
ead1e424
ILT
139 // Return the output section index, if there is an output section.
140 unsigned int
141 out_shndx() const
142 { return this->do_out_shndx(); }
143
144 // Set the output section index, if this is an output section.
145 void
146 set_out_shndx(unsigned int shndx)
147 { this->do_set_out_shndx(shndx); }
148
27bc2bce
ILT
149 // Set the address and file offset of this data, and finalize the
150 // size of the data. This is called during Layout::finalize for
151 // allocated sections.
75f65a3e 152 void
27bc2bce
ILT
153 set_address_and_file_offset(uint64_t addr, off_t off)
154 {
155 this->set_address(addr);
156 this->set_file_offset(off);
157 this->finalize_data_size();
158 }
159
160 // Set the address.
161 void
162 set_address(uint64_t addr)
163 {
164 gold_assert(!this->is_address_valid_);
165 this->address_ = addr;
166 this->is_address_valid_ = true;
167 }
168
169 // Set the file offset.
170 void
171 set_file_offset(off_t off)
172 {
173 gold_assert(!this->is_offset_valid_);
174 this->offset_ = off;
175 this->is_offset_valid_ = true;
176 }
177
178 // Finalize the data size.
179 void
180 finalize_data_size()
181 {
182 if (!this->is_data_size_valid_)
183 {
184 // Tell the child class to set the data size.
185 this->set_final_data_size();
186 gold_assert(this->is_data_size_valid_);
187 }
188 }
75f65a3e 189
7bf1f802
ILT
190 // Set the TLS offset. Called only for SHT_TLS sections.
191 void
192 set_tls_offset(uint64_t tls_base)
193 { this->do_set_tls_offset(tls_base); }
194
195 // Return the TLS offset, relative to the base of the TLS segment.
196 // Valid only for SHT_TLS sections.
197 uint64_t
198 tls_offset() const
199 { return this->do_tls_offset(); }
200
ead1e424
ILT
201 // Write the data to the output file. This is called after
202 // Layout::finalize is complete.
75f65a3e
ILT
203 void
204 write(Output_file* file)
205 { this->do_write(file); }
a2fb1b05 206
27bc2bce
ILT
207 // This is called by Layout::finalize to note that the sizes of
208 // allocated sections must now be fixed.
a3ad94ed
ILT
209 static void
210 layout_complete()
27bc2bce 211 { Output_data::allocated_sizes_are_fixed = true; }
a3ad94ed 212
730cdc88
ILT
213 // Used to check that layout has been done.
214 static bool
215 is_layout_complete()
27bc2bce 216 { return Output_data::allocated_sizes_are_fixed; }
730cdc88 217
4f4c5f80
ILT
218 // Count the number of dynamic relocations applied to this section.
219 void
220 add_dynamic_reloc()
221 { ++this->dynamic_reloc_count_; }
222
223 // Return the number of dynamic relocations applied to this section.
224 unsigned int
225 dynamic_reloc_count() const
226 { return this->dynamic_reloc_count_; }
227
a9a60db6
ILT
228 // Whether the address is valid.
229 bool
230 is_address_valid() const
231 { return this->is_address_valid_; }
232
233 // Whether the file offset is valid.
234 bool
235 is_offset_valid() const
236 { return this->is_offset_valid_; }
237
238 // Whether the data size is valid.
239 bool
240 is_data_size_valid() const
241 { return this->is_data_size_valid_; }
242
75f65a3e
ILT
243 protected:
244 // Functions that child classes may or in some cases must implement.
245
246 // Write the data to the output file.
a2fb1b05 247 virtual void
75f65a3e
ILT
248 do_write(Output_file*) = 0;
249
250 // Return the required alignment.
251 virtual uint64_t
252 do_addralign() const = 0;
253
a445fddf
ILT
254 // Return whether this has a load address.
255 virtual bool
256 do_has_load_address() const
257 { return false; }
258
259 // Return the load address.
260 virtual uint64_t
261 do_load_address() const
262 { gold_unreachable(); }
263
75f65a3e
ILT
264 // Return whether this is an Output_section.
265 virtual bool
266 do_is_section() const
267 { return false; }
a2fb1b05 268
54dc6425 269 // Return whether this is an Output_section of the specified type.
75f65a3e 270 // This only needs to be implement by Output_section.
54dc6425 271 virtual bool
75f65a3e 272 do_is_section_type(elfcpp::Elf_Word) const
54dc6425
ILT
273 { return false; }
274
75f65a3e
ILT
275 // Return whether this is an Output_section with the specific flag
276 // set. This only needs to be implemented by Output_section.
54dc6425 277 virtual bool
75f65a3e 278 do_is_section_flag_set(elfcpp::Elf_Xword) const
54dc6425
ILT
279 { return false; }
280
77e65537
ILT
281 // Return the output section, if there is one.
282 virtual Output_section*
283 do_output_section()
284 { return NULL; }
285
ead1e424
ILT
286 // Return the output section index, if there is an output section.
287 virtual unsigned int
288 do_out_shndx() const
a3ad94ed 289 { gold_unreachable(); }
ead1e424
ILT
290
291 // Set the output section index, if this is an output section.
292 virtual void
293 do_set_out_shndx(unsigned int)
a3ad94ed 294 { gold_unreachable(); }
ead1e424 295
27bc2bce
ILT
296 // This is a hook for derived classes to set the data size. This is
297 // called by finalize_data_size, normally called during
298 // Layout::finalize, when the section address is set.
75f65a3e 299 virtual void
27bc2bce
ILT
300 set_final_data_size()
301 { gold_unreachable(); }
75f65a3e 302
a445fddf
ILT
303 // A hook for resetting the address and file offset.
304 virtual void
305 do_reset_address_and_file_offset()
306 { }
307
7bf1f802
ILT
308 // Set the TLS offset. Called only for SHT_TLS sections.
309 virtual void
310 do_set_tls_offset(uint64_t)
311 { gold_unreachable(); }
312
313 // Return the TLS offset, relative to the base of the TLS segment.
314 // Valid only for SHT_TLS sections.
315 virtual uint64_t
316 do_tls_offset() const
317 { gold_unreachable(); }
318
75f65a3e
ILT
319 // Functions that child classes may call.
320
a2fb1b05
ILT
321 // Set the size of the data.
322 void
75f65a3e 323 set_data_size(off_t data_size)
a3ad94ed 324 {
27bc2bce
ILT
325 gold_assert(!this->is_data_size_valid_);
326 this->data_size_ = data_size;
327 this->is_data_size_valid_ = true;
328 }
329
330 // Get the current data size--this is for the convenience of
331 // sections which build up their size over time.
332 off_t
333 current_data_size_for_child() const
334 { return this->data_size_; }
335
336 // Set the current data size--this is for the convenience of
337 // sections which build up their size over time.
338 void
339 set_current_data_size_for_child(off_t data_size)
340 {
341 gold_assert(!this->is_data_size_valid_);
a3ad94ed
ILT
342 this->data_size_ = data_size;
343 }
75f65a3e 344
730cdc88
ILT
345 // Return default alignment for the target size.
346 static uint64_t
347 default_alignment();
348
349 // Return default alignment for a specified size--32 or 64.
75f65a3e 350 static uint64_t
730cdc88 351 default_alignment_for_size(int size);
a2fb1b05
ILT
352
353 private:
354 Output_data(const Output_data&);
355 Output_data& operator=(const Output_data&);
356
a3ad94ed 357 // This is used for verification, to make sure that we don't try to
27bc2bce
ILT
358 // change any sizes of allocated sections after we set the section
359 // addresses.
360 static bool allocated_sizes_are_fixed;
a3ad94ed 361
27bc2bce 362 // Memory address in output file.
75f65a3e 363 uint64_t address_;
27bc2bce 364 // Size of data in output file.
75f65a3e 365 off_t data_size_;
27bc2bce 366 // File offset of contents in output file.
75f65a3e 367 off_t offset_;
27bc2bce
ILT
368 // Whether address_ is valid.
369 bool is_address_valid_;
370 // Whether data_size_ is valid.
371 bool is_data_size_valid_;
372 // Whether offset_ is valid.
373 bool is_offset_valid_;
4f4c5f80
ILT
374 // Count of dynamic relocations applied to this section.
375 unsigned int dynamic_reloc_count_;
a2fb1b05
ILT
376};
377
54dc6425
ILT
378// Output the section headers.
379
380class Output_section_headers : public Output_data
381{
382 public:
9025d29d 383 Output_section_headers(const Layout*,
16649710
ILT
384 const Layout::Segment_list*,
385 const Layout::Section_list*,
6a74a719 386 const Layout::Section_list*,
61ba1cf9 387 const Stringpool*);
54dc6425 388
27bc2bce 389 protected:
54dc6425
ILT
390 // Write the data to the file.
391 void
75f65a3e
ILT
392 do_write(Output_file*);
393
394 // Return the required alignment.
395 uint64_t
396 do_addralign() const
730cdc88 397 { return Output_data::default_alignment(); }
54dc6425
ILT
398
399 private:
61ba1cf9
ILT
400 // Write the data to the file with the right size and endianness.
401 template<int size, bool big_endian>
402 void
403 do_sized_write(Output_file*);
404
16649710
ILT
405 const Layout* layout_;
406 const Layout::Segment_list* segment_list_;
6a74a719 407 const Layout::Section_list* section_list_;
16649710 408 const Layout::Section_list* unattached_section_list_;
61ba1cf9 409 const Stringpool* secnamepool_;
54dc6425
ILT
410};
411
412// Output the segment headers.
413
414class Output_segment_headers : public Output_data
415{
416 public:
9025d29d 417 Output_segment_headers(const Layout::Segment_list& segment_list);
54dc6425 418
27bc2bce 419 protected:
54dc6425
ILT
420 // Write the data to the file.
421 void
75f65a3e
ILT
422 do_write(Output_file*);
423
424 // Return the required alignment.
425 uint64_t
426 do_addralign() const
730cdc88 427 { return Output_data::default_alignment(); }
54dc6425
ILT
428
429 private:
61ba1cf9
ILT
430 // Write the data to the file with the right size and endianness.
431 template<int size, bool big_endian>
432 void
433 do_sized_write(Output_file*);
434
54dc6425
ILT
435 const Layout::Segment_list& segment_list_;
436};
437
438// Output the ELF file header.
439
440class Output_file_header : public Output_data
441{
442 public:
9025d29d 443 Output_file_header(const Target*,
54dc6425 444 const Symbol_table*,
d391083d
ILT
445 const Output_segment_headers*,
446 const char* entry);
75f65a3e
ILT
447
448 // Add information about the section headers. We lay out the ELF
449 // file header before we create the section headers.
450 void set_section_info(const Output_section_headers*,
451 const Output_section* shstrtab);
54dc6425 452
27bc2bce 453 protected:
54dc6425
ILT
454 // Write the data to the file.
455 void
75f65a3e
ILT
456 do_write(Output_file*);
457
458 // Return the required alignment.
459 uint64_t
460 do_addralign() const
730cdc88 461 { return Output_data::default_alignment(); }
75f65a3e 462
54dc6425 463 private:
61ba1cf9
ILT
464 // Write the data to the file with the right size and endianness.
465 template<int size, bool big_endian>
466 void
467 do_sized_write(Output_file*);
468
d391083d
ILT
469 // Return the value to use for the entry address.
470 template<int size>
471 typename elfcpp::Elf_types<size>::Elf_Addr
472 entry();
473
54dc6425
ILT
474 const Target* target_;
475 const Symbol_table* symtab_;
61ba1cf9 476 const Output_segment_headers* segment_header_;
54dc6425
ILT
477 const Output_section_headers* section_header_;
478 const Output_section* shstrtab_;
d391083d 479 const char* entry_;
54dc6425
ILT
480};
481
ead1e424
ILT
482// Output sections are mainly comprised of input sections. However,
483// there are cases where we have data to write out which is not in an
484// input section. Output_section_data is used in such cases. This is
485// an abstract base class.
486
487class Output_section_data : public Output_data
488{
489 public:
490 Output_section_data(off_t data_size, uint64_t addralign)
27bc2bce
ILT
491 : Output_data(), output_section_(NULL), addralign_(addralign)
492 { this->set_data_size(data_size); }
ead1e424
ILT
493
494 Output_section_data(uint64_t addralign)
27bc2bce 495 : Output_data(), output_section_(NULL), addralign_(addralign)
ead1e424
ILT
496 { }
497
16649710
ILT
498 // Return the output section.
499 const Output_section*
500 output_section() const
501 { return this->output_section_; }
502
ead1e424
ILT
503 // Record the output section.
504 void
16649710 505 set_output_section(Output_section* os);
ead1e424 506
b8e6aad9
ILT
507 // Add an input section, for SHF_MERGE sections. This returns true
508 // if the section was handled.
509 bool
510 add_input_section(Relobj* object, unsigned int shndx)
511 { return this->do_add_input_section(object, shndx); }
512
513 // Given an input OBJECT, an input section index SHNDX within that
514 // object, and an OFFSET relative to the start of that input
730cdc88
ILT
515 // section, return whether or not the corresponding offset within
516 // the output section is known. If this function returns true, it
517 // sets *POUTPUT to the output offset. The value -1 indicates that
518 // this input offset is being discarded.
8f00aeb8 519 bool
8383303e
ILT
520 output_offset(const Relobj* object, unsigned int shndx,
521 section_offset_type offset,
522 section_offset_type *poutput) const
730cdc88 523 { return this->do_output_offset(object, shndx, offset, poutput); }
b8e6aad9 524
a9a60db6
ILT
525 // Return whether this is the merge section for the input section
526 // SHNDX in OBJECT. This should return true when output_offset
527 // would return true for some values of OFFSET.
528 bool
529 is_merge_section_for(const Relobj* object, unsigned int shndx) const
530 { return this->do_is_merge_section_for(object, shndx); }
531
96803768
ILT
532 // Write the contents to a buffer. This is used for sections which
533 // require postprocessing, such as compression.
534 void
535 write_to_buffer(unsigned char* buffer)
536 { this->do_write_to_buffer(buffer); }
537
38c5e8b4
ILT
538 // Print merge stats to stderr. This should only be called for
539 // SHF_MERGE sections.
540 void
541 print_merge_stats(const char* section_name)
542 { this->do_print_merge_stats(section_name); }
543
ead1e424
ILT
544 protected:
545 // The child class must implement do_write.
546
16649710
ILT
547 // The child class may implement specific adjustments to the output
548 // section.
549 virtual void
550 do_adjust_output_section(Output_section*)
551 { }
552
b8e6aad9
ILT
553 // May be implemented by child class. Return true if the section
554 // was handled.
555 virtual bool
556 do_add_input_section(Relobj*, unsigned int)
557 { gold_unreachable(); }
558
730cdc88 559 // The child class may implement output_offset.
b8e6aad9 560 virtual bool
8383303e
ILT
561 do_output_offset(const Relobj*, unsigned int, section_offset_type,
562 section_offset_type*) const
b8e6aad9
ILT
563 { return false; }
564
a9a60db6
ILT
565 // The child class may implement is_merge_section_for.
566 virtual bool
567 do_is_merge_section_for(const Relobj*, unsigned int) const
568 { return false; }
569
96803768
ILT
570 // The child class may implement write_to_buffer. Most child
571 // classes can not appear in a compressed section, and they do not
572 // implement this.
573 virtual void
574 do_write_to_buffer(unsigned char*)
575 { gold_unreachable(); }
576
38c5e8b4
ILT
577 // Print merge statistics.
578 virtual void
579 do_print_merge_stats(const char*)
580 { gold_unreachable(); }
581
ead1e424
ILT
582 // Return the required alignment.
583 uint64_t
584 do_addralign() const
585 { return this->addralign_; }
586
77e65537
ILT
587 // Return the output section.
588 Output_section*
589 do_output_section()
590 { return this->output_section_; }
591
ead1e424
ILT
592 // Return the section index of the output section.
593 unsigned int
594 do_out_shndx() const;
595
5a6f7e2d
ILT
596 // Set the alignment.
597 void
598 set_addralign(uint64_t addralign)
599 { this->addralign_ = addralign; }
600
ead1e424
ILT
601 private:
602 // The output section for this section.
77e65537 603 Output_section* output_section_;
ead1e424
ILT
604 // The required alignment.
605 uint64_t addralign_;
606};
607
27bc2bce
ILT
608// Some Output_section_data classes build up their data step by step,
609// rather than all at once. This class provides an interface for
610// them.
611
612class Output_section_data_build : public Output_section_data
613{
614 public:
615 Output_section_data_build(uint64_t addralign)
616 : Output_section_data(addralign)
617 { }
618
619 // Get the current data size.
620 off_t
621 current_data_size() const
622 { return this->current_data_size_for_child(); }
623
624 // Set the current data size.
625 void
626 set_current_data_size(off_t data_size)
627 { this->set_current_data_size_for_child(data_size); }
628
629 protected:
630 // Set the final data size.
631 virtual void
632 set_final_data_size()
633 { this->set_data_size(this->current_data_size_for_child()); }
634};
635
dbe717ef
ILT
636// A simple case of Output_data in which we have constant data to
637// output.
ead1e424 638
dbe717ef 639class Output_data_const : public Output_section_data
ead1e424
ILT
640{
641 public:
dbe717ef
ILT
642 Output_data_const(const std::string& data, uint64_t addralign)
643 : Output_section_data(data.size(), addralign), data_(data)
644 { }
645
646 Output_data_const(const char* p, off_t len, uint64_t addralign)
647 : Output_section_data(len, addralign), data_(p, len)
648 { }
649
650 Output_data_const(const unsigned char* p, off_t len, uint64_t addralign)
651 : Output_section_data(len, addralign),
652 data_(reinterpret_cast<const char*>(p), len)
653 { }
654
27bc2bce 655 protected:
a3ad94ed 656 // Write the data to the output file.
dbe717ef 657 void
a3ad94ed 658 do_write(Output_file*);
dbe717ef 659
96803768
ILT
660 // Write the data to a buffer.
661 void
662 do_write_to_buffer(unsigned char* buffer)
663 { memcpy(buffer, this->data_.data(), this->data_.size()); }
664
dbe717ef
ILT
665 private:
666 std::string data_;
667};
668
a3ad94ed
ILT
669// Another version of Output_data with constant data, in which the
670// buffer is allocated by the caller.
dbe717ef 671
a3ad94ed 672class Output_data_const_buffer : public Output_section_data
dbe717ef
ILT
673{
674 public:
a3ad94ed
ILT
675 Output_data_const_buffer(const unsigned char* p, off_t len,
676 uint64_t addralign)
677 : Output_section_data(len, addralign), p_(p)
678 { }
679
27bc2bce 680 protected:
a3ad94ed
ILT
681 // Write the data the output file.
682 void
683 do_write(Output_file*);
684
96803768
ILT
685 // Write the data to a buffer.
686 void
687 do_write_to_buffer(unsigned char* buffer)
688 { memcpy(buffer, this->p_, this->data_size()); }
689
a3ad94ed
ILT
690 private:
691 const unsigned char* p_;
692};
693
27bc2bce
ILT
694// A place holder for a fixed amount of data written out via some
695// other mechanism.
a3ad94ed 696
27bc2bce 697class Output_data_fixed_space : public Output_section_data
a3ad94ed
ILT
698{
699 public:
27bc2bce 700 Output_data_fixed_space(off_t data_size, uint64_t addralign)
a3ad94ed
ILT
701 : Output_section_data(data_size, addralign)
702 { }
703
27bc2bce
ILT
704 protected:
705 // Write out the data--the actual data must be written out
706 // elsewhere.
707 void
708 do_write(Output_file*)
ead1e424 709 { }
27bc2bce 710};
ead1e424 711
27bc2bce
ILT
712// A place holder for variable sized data written out via some other
713// mechanism.
714
715class Output_data_space : public Output_section_data_build
716{
717 public:
718 explicit Output_data_space(uint64_t addralign)
719 : Output_section_data_build(addralign)
720 { }
ead1e424 721
5a6f7e2d
ILT
722 // Set the alignment.
723 void
724 set_space_alignment(uint64_t align)
725 { this->set_addralign(align); }
726
27bc2bce
ILT
727 protected:
728 // Write out the data--the actual data must be written out
729 // elsewhere.
ead1e424
ILT
730 void
731 do_write(Output_file*)
732 { }
733};
734
a3ad94ed
ILT
735// A string table which goes into an output section.
736
737class Output_data_strtab : public Output_section_data
738{
739 public:
740 Output_data_strtab(Stringpool* strtab)
741 : Output_section_data(1), strtab_(strtab)
742 { }
743
27bc2bce 744 protected:
a3ad94ed
ILT
745 // This is called to set the address and file offset. Here we make
746 // sure that the Stringpool is finalized.
747 void
27bc2bce 748 set_final_data_size();
a3ad94ed
ILT
749
750 // Write out the data.
751 void
752 do_write(Output_file*);
753
96803768
ILT
754 // Write the data to a buffer.
755 void
756 do_write_to_buffer(unsigned char* buffer)
757 { this->strtab_->write_to_buffer(buffer, this->data_size()); }
758
a3ad94ed
ILT
759 private:
760 Stringpool* strtab_;
761};
762
c06b7b0b
ILT
763// This POD class is used to represent a single reloc in the output
764// file. This could be a private class within Output_data_reloc, but
765// the templatization is complex enough that I broke it out into a
766// separate class. The class is templatized on either elfcpp::SHT_REL
767// or elfcpp::SHT_RELA, and also on whether this is a dynamic
768// relocation or an ordinary relocation.
769
dceae3c1
ILT
770// A relocation can be against a global symbol, a local symbol, a
771// local section symbol, an output section, or the undefined symbol at
772// index 0. We represent the latter by using a NULL global symbol.
c06b7b0b
ILT
773
774template<int sh_type, bool dynamic, int size, bool big_endian>
775class Output_reloc;
776
777template<bool dynamic, int size, bool big_endian>
778class Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
779{
780 public:
781 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
782
783 // An uninitialized entry. We need this because we want to put
784 // instances of this class into an STL container.
785 Output_reloc()
786 : local_sym_index_(INVALID_CODE)
787 { }
788
dceae3c1
ILT
789 // We have a bunch of different constructors. They come in pairs
790 // depending on how the address of the relocation is specified. It
791 // can either be an offset in an Output_data or an offset in an
792 // input section.
793
c06b7b0b 794 // A reloc against a global symbol.
5a6f7e2d 795
a3ad94ed 796 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
e8c846c3 797 Address address, bool is_relative);
5a6f7e2d
ILT
798
799 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
e8c846c3 800 unsigned int shndx, Address address, bool is_relative);
c06b7b0b 801
dceae3c1 802 // A reloc against a local symbol or local section symbol.
5a6f7e2d
ILT
803
804 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 805 unsigned int local_sym_index, unsigned int type,
dceae3c1
ILT
806 Output_data* od, Address address, bool is_relative,
807 bool is_section_symbol);
5a6f7e2d
ILT
808
809 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 810 unsigned int local_sym_index, unsigned int type,
dceae3c1
ILT
811 unsigned int shndx, Address address, bool is_relative,
812 bool is_section_symbol);
c06b7b0b
ILT
813
814 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 815
a3ad94ed 816 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
7bf1f802 817 Address address);
5a6f7e2d
ILT
818
819 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
7bf1f802 820 unsigned int shndx, Address address);
c06b7b0b 821
e8c846c3
ILT
822 // Return TRUE if this is a RELATIVE relocation.
823 bool
824 is_relative() const
825 { return this->is_relative_; }
826
dceae3c1
ILT
827 // Return whether this is against a local section symbol.
828 bool
829 is_local_section_symbol() const
830 {
831 return (this->local_sym_index_ != GSYM_CODE
832 && this->local_sym_index_ != SECTION_CODE
833 && this->local_sym_index_ != INVALID_CODE
834 && this->is_section_symbol_);
835 }
836
837 // For a local section symbol, return the offset of the input
838 // section within the output section.
839 section_offset_type
840 local_section_offset() const;
841
d1f003c6
ILT
842 // Get the value of the symbol referred to by a Rel relocation when
843 // we are adding the given ADDEND.
e8c846c3 844 Address
d1f003c6 845 symbol_value(Address addend) const;
e8c846c3 846
c06b7b0b
ILT
847 // Write the reloc entry to an output view.
848 void
849 write(unsigned char* pov) const;
850
851 // Write the offset and info fields to Write_rel.
852 template<typename Write_rel>
853 void write_rel(Write_rel*) const;
854
855 private:
dceae3c1
ILT
856 // Record that we need a dynamic symbol index.
857 void
858 set_needs_dynsym_index();
859
860 // Return the symbol index.
c06b7b0b
ILT
861 unsigned int
862 get_symbol_index() const;
863
864 // Codes for local_sym_index_.
865 enum
866 {
867 // Global symbol.
868 GSYM_CODE = -1U,
869 // Output section.
870 SECTION_CODE = -2U,
871 // Invalid uninitialized entry.
872 INVALID_CODE = -3U
873 };
874
875 union
876 {
dceae3c1
ILT
877 // For a local symbol or local section symbol
878 // (this->local_sym_index_ >= 0), the object. We will never
879 // generate a relocation against a local symbol in a dynamic
880 // object; that doesn't make sense. And our callers will always
881 // be templatized, so we use Sized_relobj here.
5a6f7e2d 882 Sized_relobj<size, big_endian>* relobj;
dceae3c1
ILT
883 // For a global symbol (this->local_sym_index_ == GSYM_CODE, the
884 // symbol. If this is NULL, it indicates a relocation against the
885 // undefined 0 symbol.
c06b7b0b 886 Symbol* gsym;
dceae3c1
ILT
887 // For a relocation against an output section
888 // (this->local_sym_index_ == SECTION_CODE), the output section.
c06b7b0b 889 Output_section* os;
5a6f7e2d
ILT
890 } u1_;
891 union
892 {
dceae3c1
ILT
893 // If this->shndx_ is not INVALID CODE, the object which holds the
894 // input section being used to specify the reloc address.
5a6f7e2d 895 Relobj* relobj;
dceae3c1 896 // If this->shndx_ is INVALID_CODE, the output data being used to
5a6f7e2d
ILT
897 // specify the reloc address. This may be NULL if the reloc
898 // address is absolute.
899 Output_data* od;
900 } u2_;
901 // The address offset within the input section or the Output_data.
902 Address address_;
dceae3c1
ILT
903 // This is GSYM_CODE for a global symbol, or SECTION_CODE for a
904 // relocation against an output section, or INVALID_CODE for an
905 // uninitialized value. Otherwise, for a local symbol
906 // (this->is_section_symbol_ is false), the local symbol index. For
907 // a local section symbol (this->is_section_symbol_ is true), the
908 // section index in the input file.
c06b7b0b 909 unsigned int local_sym_index_;
a3ad94ed 910 // The reloc type--a processor specific code.
dceae3c1 911 unsigned int type_ : 30;
e8c846c3
ILT
912 // True if the relocation is a RELATIVE relocation.
913 bool is_relative_ : 1;
dceae3c1
ILT
914 // True if the relocation is against a section symbol.
915 bool is_section_symbol_ : 1;
5a6f7e2d
ILT
916 // If the reloc address is an input section in an object, the
917 // section index. This is INVALID_CODE if the reloc address is
918 // specified in some other way.
919 unsigned int shndx_;
c06b7b0b
ILT
920};
921
922// The SHT_RELA version of Output_reloc<>. This is just derived from
923// the SHT_REL version of Output_reloc, but it adds an addend.
924
925template<bool dynamic, int size, bool big_endian>
926class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
927{
928 public:
929 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
930 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
931
932 // An uninitialized entry.
933 Output_reloc()
934 : rel_()
935 { }
936
937 // A reloc against a global symbol.
5a6f7e2d 938
a3ad94ed 939 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
e8c846c3
ILT
940 Address address, Addend addend, bool is_relative)
941 : rel_(gsym, type, od, address, is_relative), addend_(addend)
c06b7b0b
ILT
942 { }
943
5a6f7e2d 944 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
e8c846c3
ILT
945 unsigned int shndx, Address address, Addend addend,
946 bool is_relative)
947 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
5a6f7e2d
ILT
948 { }
949
c06b7b0b 950 // A reloc against a local symbol.
5a6f7e2d
ILT
951
952 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
953 unsigned int local_sym_index, unsigned int type,
954 Output_data* od, Address address,
dceae3c1
ILT
955 Addend addend, bool is_relative, bool is_section_symbol)
956 : rel_(relobj, local_sym_index, type, od, address, is_relative,
957 is_section_symbol),
e8c846c3 958 addend_(addend)
5a6f7e2d
ILT
959 { }
960
961 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
962 unsigned int local_sym_index, unsigned int type,
963 unsigned int shndx, Address address,
dceae3c1
ILT
964 Addend addend, bool is_relative, bool is_section_symbol)
965 : rel_(relobj, local_sym_index, type, shndx, address, is_relative,
966 is_section_symbol),
5a6f7e2d 967 addend_(addend)
c06b7b0b
ILT
968 { }
969
970 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 971
a3ad94ed
ILT
972 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
973 Address address, Addend addend)
974 : rel_(os, type, od, address), addend_(addend)
c06b7b0b
ILT
975 { }
976
5a6f7e2d
ILT
977 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
978 unsigned int shndx, Address address, Addend addend)
979 : rel_(os, type, relobj, shndx, address), addend_(addend)
980 { }
981
c06b7b0b
ILT
982 // Write the reloc entry to an output view.
983 void
984 write(unsigned char* pov) const;
985
986 private:
987 // The basic reloc.
988 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
989 // The addend.
990 Addend addend_;
991};
992
993// Output_data_reloc is used to manage a section containing relocs.
994// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
995// indicates whether this is a dynamic relocation or a normal
996// relocation. Output_data_reloc_base is a base class.
997// Output_data_reloc is the real class, which we specialize based on
998// the reloc type.
999
1000template<int sh_type, bool dynamic, int size, bool big_endian>
27bc2bce 1001class Output_data_reloc_base : public Output_section_data_build
c06b7b0b
ILT
1002{
1003 public:
1004 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
1005 typedef typename Output_reloc_type::Address Address;
1006 static const int reloc_size =
1007 Reloc_types<sh_type, size, big_endian>::reloc_size;
1008
1009 // Construct the section.
1010 Output_data_reloc_base()
27bc2bce 1011 : Output_section_data_build(Output_data::default_alignment_for_size(size))
c06b7b0b
ILT
1012 { }
1013
27bc2bce 1014 protected:
c06b7b0b
ILT
1015 // Write out the data.
1016 void
1017 do_write(Output_file*);
1018
16649710
ILT
1019 // Set the entry size and the link.
1020 void
1021 do_adjust_output_section(Output_section *os);
1022
c06b7b0b
ILT
1023 // Add a relocation entry.
1024 void
4f4c5f80 1025 add(Output_data *od, const Output_reloc_type& reloc)
c06b7b0b
ILT
1026 {
1027 this->relocs_.push_back(reloc);
27bc2bce 1028 this->set_current_data_size(this->relocs_.size() * reloc_size);
4f4c5f80 1029 od->add_dynamic_reloc();
c06b7b0b
ILT
1030 }
1031
1032 private:
1033 typedef std::vector<Output_reloc_type> Relocs;
1034
1035 Relocs relocs_;
1036};
1037
1038// The class which callers actually create.
1039
1040template<int sh_type, bool dynamic, int size, bool big_endian>
1041class Output_data_reloc;
1042
1043// The SHT_REL version of Output_data_reloc.
1044
1045template<bool dynamic, int size, bool big_endian>
1046class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
1047 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
1048{
dceae3c1 1049 private:
c06b7b0b
ILT
1050 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
1051 big_endian> Base;
1052
1053 public:
1054 typedef typename Base::Output_reloc_type Output_reloc_type;
1055 typedef typename Output_reloc_type::Address Address;
1056
1057 Output_data_reloc()
1058 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
1059 { }
1060
1061 // Add a reloc against a global symbol.
5a6f7e2d 1062
c06b7b0b 1063 void
a3ad94ed 1064 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
e8c846c3 1065 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
c06b7b0b 1066
5a6f7e2d 1067 void
4f4c5f80 1068 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
5a6f7e2d 1069 unsigned int shndx, Address address)
e8c846c3
ILT
1070 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1071 false)); }
1072
1073 // Add a RELATIVE reloc against a global symbol. The final relocation
1074 // will not reference the symbol.
1075
1076 void
1077 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1078 Address address)
1079 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
1080
1081 void
1082 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1083 Relobj* relobj, unsigned int shndx, Address address)
dceae3c1
ILT
1084 {
1085 this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1086 true));
1087 }
5a6f7e2d 1088
c06b7b0b 1089 // Add a reloc against a local symbol.
5a6f7e2d 1090
c06b7b0b 1091 void
5a6f7e2d 1092 add_local(Sized_relobj<size, big_endian>* relobj,
a3ad94ed
ILT
1093 unsigned int local_sym_index, unsigned int type,
1094 Output_data* od, Address address)
dceae3c1
ILT
1095 {
1096 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1097 address, false, false));
1098 }
5a6f7e2d
ILT
1099
1100 void
1101 add_local(Sized_relobj<size, big_endian>* relobj,
1102 unsigned int local_sym_index, unsigned int type,
4f4c5f80 1103 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1104 {
1105 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1106 address, false, false));
1107 }
e8c846c3
ILT
1108
1109 // Add a RELATIVE reloc against a local symbol.
5a6f7e2d 1110
e8c846c3
ILT
1111 void
1112 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1113 unsigned int local_sym_index, unsigned int type,
1114 Output_data* od, Address address)
dceae3c1
ILT
1115 {
1116 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
1117 address, true, false));
1118 }
e8c846c3
ILT
1119
1120 void
1121 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1122 unsigned int local_sym_index, unsigned int type,
1123 Output_data* od, unsigned int shndx, Address address)
dceae3c1
ILT
1124 {
1125 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1126 address, true, false));
1127 }
1128
1129 // Add a reloc against a local section symbol. This will be
1130 // converted into a reloc against the STT_SECTION symbol of the
1131 // output section.
1132
1133 void
1134 add_local_section(Sized_relobj<size, big_endian>* relobj,
1135 unsigned int input_shndx, unsigned int type,
1136 Output_data* od, Address address)
1137 {
1138 this->add(od, Output_reloc_type(relobj, input_shndx, type, od,
1139 address, false, true));
1140 }
1141
1142 void
1143 add_local_section(Sized_relobj<size, big_endian>* relobj,
1144 unsigned int input_shndx, unsigned int type,
1145 Output_data* od, unsigned int shndx, Address address)
1146 {
1147 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1148 address, false, true));
1149 }
c06b7b0b
ILT
1150
1151 // A reloc against the STT_SECTION symbol of an output section.
4f4c5f80
ILT
1152 // OS is the Output_section that the relocation refers to; OD is
1153 // the Output_data object being relocated.
5a6f7e2d 1154
c06b7b0b 1155 void
a3ad94ed
ILT
1156 add_output_section(Output_section* os, unsigned int type,
1157 Output_data* od, Address address)
4f4c5f80 1158 { this->add(od, Output_reloc_type(os, type, od, address)); }
5a6f7e2d
ILT
1159
1160 void
4f4c5f80 1161 add_output_section(Output_section* os, unsigned int type, Output_data* od,
5a6f7e2d 1162 Relobj* relobj, unsigned int shndx, Address address)
4f4c5f80 1163 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
c06b7b0b
ILT
1164};
1165
1166// The SHT_RELA version of Output_data_reloc.
1167
1168template<bool dynamic, int size, bool big_endian>
1169class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1170 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1171{
dceae3c1 1172 private:
c06b7b0b
ILT
1173 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1174 big_endian> Base;
1175
1176 public:
1177 typedef typename Base::Output_reloc_type Output_reloc_type;
1178 typedef typename Output_reloc_type::Address Address;
1179 typedef typename Output_reloc_type::Addend Addend;
1180
1181 Output_data_reloc()
1182 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1183 { }
1184
1185 // Add a reloc against a global symbol.
5a6f7e2d 1186
c06b7b0b 1187 void
a3ad94ed
ILT
1188 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1189 Address address, Addend addend)
e8c846c3
ILT
1190 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1191 false)); }
c06b7b0b 1192
5a6f7e2d 1193 void
4f4c5f80
ILT
1194 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1195 unsigned int shndx, Address address,
1196 Addend addend)
1197 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
e8c846c3
ILT
1198 addend, false)); }
1199
1200 // Add a RELATIVE reloc against a global symbol. The final output
1201 // relocation will not reference the symbol, but we must keep the symbol
1202 // information long enough to set the addend of the relocation correctly
1203 // when it is written.
1204
1205 void
1206 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1207 Address address, Addend addend)
1208 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1209
1210 void
1211 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1212 Relobj* relobj, unsigned int shndx, Address address,
1213 Addend addend)
1214 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1215 addend, true)); }
5a6f7e2d 1216
c06b7b0b 1217 // Add a reloc against a local symbol.
5a6f7e2d 1218
c06b7b0b 1219 void
5a6f7e2d 1220 add_local(Sized_relobj<size, big_endian>* relobj,
c06b7b0b 1221 unsigned int local_sym_index, unsigned int type,
a3ad94ed 1222 Output_data* od, Address address, Addend addend)
c06b7b0b 1223 {
4f4c5f80 1224 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
dceae3c1 1225 addend, false, false));
5a6f7e2d
ILT
1226 }
1227
1228 void
1229 add_local(Sized_relobj<size, big_endian>* relobj,
1230 unsigned int local_sym_index, unsigned int type,
4f4c5f80
ILT
1231 Output_data* od, unsigned int shndx, Address address,
1232 Addend addend)
5a6f7e2d 1233 {
4f4c5f80 1234 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
dceae3c1 1235 address, addend, false, false));
e8c846c3
ILT
1236 }
1237
1238 // Add a RELATIVE reloc against a local symbol.
1239
1240 void
1241 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1242 unsigned int local_sym_index, unsigned int type,
1243 Output_data* od, Address address, Addend addend)
1244 {
1245 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
dceae3c1 1246 addend, true, false));
e8c846c3
ILT
1247 }
1248
1249 void
1250 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1251 unsigned int local_sym_index, unsigned int type,
1252 Output_data* od, unsigned int shndx, Address address,
1253 Addend addend)
1254 {
1255 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
dceae3c1
ILT
1256 address, addend, true, false));
1257 }
1258
1259 // Add a reloc against a local section symbol. This will be
1260 // converted into a reloc against the STT_SECTION symbol of the
1261 // output section.
1262
1263 void
1264 add_local_section(Sized_relobj<size, big_endian>* relobj,
1265 unsigned int input_shndx, unsigned int type,
1266 Output_data* od, Address address, Addend addend)
1267 {
1268 this->add(od, Output_reloc_type(relobj, input_shndx, type, od, address,
1269 addend, false, true));
1270 }
1271
1272 void
1273 add_local_section(Sized_relobj<size, big_endian>* relobj,
1274 unsigned int input_shndx, unsigned int type,
1275 Output_data* od, unsigned int shndx, Address address,
1276 Addend addend)
1277 {
1278 this->add(od, Output_reloc_type(relobj, input_shndx, type, shndx,
1279 address, addend, false, true));
c06b7b0b
ILT
1280 }
1281
1282 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1283
c06b7b0b 1284 void
a3ad94ed
ILT
1285 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1286 Address address, Addend addend)
4f4c5f80 1287 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
5a6f7e2d
ILT
1288
1289 void
1290 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1291 unsigned int shndx, Address address, Addend addend)
4f4c5f80
ILT
1292 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1293 addend)); }
c06b7b0b
ILT
1294};
1295
6a74a719
ILT
1296// Output_relocatable_relocs represents a relocation section in a
1297// relocatable link. The actual data is written out in the target
1298// hook relocate_for_relocatable. This just saves space for it.
1299
1300template<int sh_type, int size, bool big_endian>
1301class Output_relocatable_relocs : public Output_section_data
1302{
1303 public:
1304 Output_relocatable_relocs(Relocatable_relocs* rr)
1305 : Output_section_data(Output_data::default_alignment_for_size(size)),
1306 rr_(rr)
1307 { }
1308
1309 void
1310 set_final_data_size();
1311
1312 // Write out the data. There is nothing to do here.
1313 void
1314 do_write(Output_file*)
1315 { }
1316
1317 private:
1318 // The relocs associated with this input section.
1319 Relocatable_relocs* rr_;
1320};
1321
1322// Handle a GROUP section.
1323
1324template<int size, bool big_endian>
1325class Output_data_group : public Output_section_data
1326{
1327 public:
1328 Output_data_group(Sized_relobj<size, big_endian>* relobj,
1329 section_size_type entry_count,
1330 const elfcpp::Elf_Word* contents);
1331
1332 void
1333 do_write(Output_file*);
1334
1335 private:
1336 // The input object.
1337 Sized_relobj<size, big_endian>* relobj_;
1338 // The group flag word.
1339 elfcpp::Elf_Word flags_;
1340 // The section indexes of the input sections in this group.
1341 std::vector<unsigned int> input_sections_;
1342};
1343
dbe717ef
ILT
1344// Output_data_got is used to manage a GOT. Each entry in the GOT is
1345// for one symbol--either a global symbol or a local symbol in an
ead1e424 1346// object. The target specific code adds entries to the GOT as
dbe717ef 1347// needed.
ead1e424
ILT
1348
1349template<int size, bool big_endian>
27bc2bce 1350class Output_data_got : public Output_section_data_build
ead1e424
ILT
1351{
1352 public:
1353 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
7bf1f802
ILT
1354 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1355 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
ead1e424 1356
7e1edb90 1357 Output_data_got()
27bc2bce 1358 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
730cdc88 1359 entries_()
ead1e424
ILT
1360 { }
1361
dbe717ef
ILT
1362 // Add an entry for a global symbol to the GOT. Return true if this
1363 // is a new GOT entry, false if the symbol was already in the GOT.
1364 bool
1365 add_global(Symbol* gsym);
ead1e424 1366
7bf1f802
ILT
1367 // Add an entry for a global symbol to the GOT, and add a dynamic
1368 // relocation of type R_TYPE for the GOT entry.
1369 void
1370 add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1371
1372 void
1373 add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1374
e727fa71
ILT
1375 // Add an entry for a local symbol to the GOT. This returns true if
1376 // this is a new GOT entry, false if the symbol already has a GOT
1377 // entry.
1378 bool
1379 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
ead1e424 1380
7bf1f802
ILT
1381 // Add an entry for a global symbol to the GOT, and add a dynamic
1382 // relocation of type R_TYPE for the GOT entry.
1383 void
1384 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1385 unsigned int sym_index, Rel_dyn* rel_dyn,
1386 unsigned int r_type);
1387
1388 void
1389 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1390 unsigned int sym_index, Rela_dyn* rela_dyn,
1391 unsigned int r_type);
1392
07f397ab
ILT
1393 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1394 // Return true if this is a new GOT entry, false if the symbol was
1395 // already in the GOT.
1396 bool
1397 add_global_tls(Symbol* gsym, bool need_pair);
1398
7bf1f802
ILT
1399 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1400 // relocation of type R_TYPE.
1401 void
1402 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1403 unsigned int r_type);
1404
1405 void
1406 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1407 unsigned int r_type);
1408
1409 // Add a pair of entries for a global TLS symbol to the GOT, and add
1410 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1411 void
1412 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1413 unsigned int mod_r_type,
1414 unsigned int dtv_r_type);
1415
1416 void
1417 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1418 unsigned int mod_r_type,
1419 unsigned int dtv_r_type);
1420
07f397ab
ILT
1421 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1422 // This returns true if this is a new GOT entry, false if the symbol
1423 // already has a GOT entry.
1424 bool
1425 add_local_tls(Sized_relobj<size, big_endian>* object,
1426 unsigned int sym_index, bool need_pair);
1427
7bf1f802
ILT
1428 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1429 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1430 // Because this is a local symbol, the first GOT entry can be relocated
1431 // relative to a section symbol, and the second GOT entry will have an
1432 // dtv-relative value that can be computed at link time.
1433 void
1434 add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1435 unsigned int sym_index, unsigned int shndx,
1436 bool need_pair, Rel_dyn* rel_dyn,
1437 unsigned int r_type);
1438
1439 void
1440 add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1441 unsigned int sym_index, unsigned int shndx,
1442 bool need_pair, Rela_dyn* rela_dyn,
1443 unsigned int r_type);
1444
ead1e424
ILT
1445 // Add a constant to the GOT. This returns the offset of the new
1446 // entry from the start of the GOT.
1447 unsigned int
1448 add_constant(Valtype constant)
1449 {
1450 this->entries_.push_back(Got_entry(constant));
1451 this->set_got_size();
1452 return this->last_got_offset();
1453 }
1454
27bc2bce 1455 protected:
ead1e424
ILT
1456 // Write out the GOT table.
1457 void
1458 do_write(Output_file*);
1459
1460 private:
1461 // This POD class holds a single GOT entry.
1462 class Got_entry
1463 {
1464 public:
1465 // Create a zero entry.
1466 Got_entry()
1467 : local_sym_index_(CONSTANT_CODE)
1468 { this->u_.constant = 0; }
1469
1470 // Create a global symbol entry.
a3ad94ed 1471 explicit Got_entry(Symbol* gsym)
ead1e424
ILT
1472 : local_sym_index_(GSYM_CODE)
1473 { this->u_.gsym = gsym; }
1474
1475 // Create a local symbol entry.
e727fa71
ILT
1476 Got_entry(Sized_relobj<size, big_endian>* object,
1477 unsigned int local_sym_index)
ead1e424
ILT
1478 : local_sym_index_(local_sym_index)
1479 {
a3ad94ed
ILT
1480 gold_assert(local_sym_index != GSYM_CODE
1481 && local_sym_index != CONSTANT_CODE);
ead1e424
ILT
1482 this->u_.object = object;
1483 }
1484
1485 // Create a constant entry. The constant is a host value--it will
1486 // be swapped, if necessary, when it is written out.
a3ad94ed 1487 explicit Got_entry(Valtype constant)
ead1e424
ILT
1488 : local_sym_index_(CONSTANT_CODE)
1489 { this->u_.constant = constant; }
1490
1491 // Write the GOT entry to an output view.
1492 void
7e1edb90 1493 write(unsigned char* pov) const;
ead1e424
ILT
1494
1495 private:
1496 enum
1497 {
1498 GSYM_CODE = -1U,
1499 CONSTANT_CODE = -2U
1500 };
1501
1502 union
1503 {
1504 // For a local symbol, the object.
e727fa71 1505 Sized_relobj<size, big_endian>* object;
ead1e424
ILT
1506 // For a global symbol, the symbol.
1507 Symbol* gsym;
1508 // For a constant, the constant.
1509 Valtype constant;
1510 } u_;
c06b7b0b
ILT
1511 // For a local symbol, the local symbol index. This is GSYM_CODE
1512 // for a global symbol, or CONSTANT_CODE for a constant.
ead1e424
ILT
1513 unsigned int local_sym_index_;
1514 };
1515
1516 typedef std::vector<Got_entry> Got_entries;
1517
1518 // Return the offset into the GOT of GOT entry I.
1519 unsigned int
1520 got_offset(unsigned int i) const
1521 { return i * (size / 8); }
1522
1523 // Return the offset into the GOT of the last entry added.
1524 unsigned int
1525 last_got_offset() const
1526 { return this->got_offset(this->entries_.size() - 1); }
1527
1528 // Set the size of the section.
1529 void
1530 set_got_size()
27bc2bce 1531 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
ead1e424
ILT
1532
1533 // The list of GOT entries.
1534 Got_entries entries_;
1535};
1536
a3ad94ed
ILT
1537// Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1538// section.
1539
1540class Output_data_dynamic : public Output_section_data
1541{
1542 public:
9025d29d 1543 Output_data_dynamic(Stringpool* pool)
730cdc88 1544 : Output_section_data(Output_data::default_alignment()),
9025d29d 1545 entries_(), pool_(pool)
a3ad94ed
ILT
1546 { }
1547
1548 // Add a new dynamic entry with a fixed numeric value.
1549 void
1550 add_constant(elfcpp::DT tag, unsigned int val)
1551 { this->add_entry(Dynamic_entry(tag, val)); }
1552
16649710 1553 // Add a new dynamic entry with the address of output data.
a3ad94ed 1554 void
16649710
ILT
1555 add_section_address(elfcpp::DT tag, const Output_data* od)
1556 { this->add_entry(Dynamic_entry(tag, od, false)); }
a3ad94ed 1557
16649710 1558 // Add a new dynamic entry with the size of output data.
a3ad94ed 1559 void
16649710
ILT
1560 add_section_size(elfcpp::DT tag, const Output_data* od)
1561 { this->add_entry(Dynamic_entry(tag, od, true)); }
a3ad94ed
ILT
1562
1563 // Add a new dynamic entry with the address of a symbol.
1564 void
16649710 1565 add_symbol(elfcpp::DT tag, const Symbol* sym)
a3ad94ed
ILT
1566 { this->add_entry(Dynamic_entry(tag, sym)); }
1567
1568 // Add a new dynamic entry with a string.
1569 void
1570 add_string(elfcpp::DT tag, const char* str)
cfd73a4e 1571 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
a3ad94ed 1572
41f542e7
ILT
1573 void
1574 add_string(elfcpp::DT tag, const std::string& str)
1575 { this->add_string(tag, str.c_str()); }
1576
27bc2bce
ILT
1577 protected:
1578 // Adjust the output section to set the entry size.
1579 void
1580 do_adjust_output_section(Output_section*);
1581
a3ad94ed
ILT
1582 // Set the final data size.
1583 void
27bc2bce 1584 set_final_data_size();
a3ad94ed
ILT
1585
1586 // Write out the dynamic entries.
1587 void
1588 do_write(Output_file*);
1589
1590 private:
1591 // This POD class holds a single dynamic entry.
1592 class Dynamic_entry
1593 {
1594 public:
1595 // Create an entry with a fixed numeric value.
1596 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1597 : tag_(tag), classification_(DYNAMIC_NUMBER)
1598 { this->u_.val = val; }
1599
1600 // Create an entry with the size or address of a section.
16649710 1601 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
a3ad94ed
ILT
1602 : tag_(tag),
1603 classification_(section_size
1604 ? DYNAMIC_SECTION_SIZE
1605 : DYNAMIC_SECTION_ADDRESS)
16649710 1606 { this->u_.od = od; }
a3ad94ed
ILT
1607
1608 // Create an entry with the address of a symbol.
16649710 1609 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
a3ad94ed
ILT
1610 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1611 { this->u_.sym = sym; }
1612
1613 // Create an entry with a string.
1614 Dynamic_entry(elfcpp::DT tag, const char* str)
1615 : tag_(tag), classification_(DYNAMIC_STRING)
1616 { this->u_.str = str; }
1617
1618 // Write the dynamic entry to an output view.
1619 template<int size, bool big_endian>
1620 void
7d1a9ebb 1621 write(unsigned char* pov, const Stringpool*) const;
a3ad94ed
ILT
1622
1623 private:
1624 enum Classification
1625 {
1626 // Number.
1627 DYNAMIC_NUMBER,
1628 // Section address.
1629 DYNAMIC_SECTION_ADDRESS,
1630 // Section size.
1631 DYNAMIC_SECTION_SIZE,
1632 // Symbol adress.
1633 DYNAMIC_SYMBOL,
1634 // String.
1635 DYNAMIC_STRING
1636 };
1637
1638 union
1639 {
1640 // For DYNAMIC_NUMBER.
1641 unsigned int val;
1642 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
16649710 1643 const Output_data* od;
a3ad94ed 1644 // For DYNAMIC_SYMBOL.
16649710 1645 const Symbol* sym;
a3ad94ed
ILT
1646 // For DYNAMIC_STRING.
1647 const char* str;
1648 } u_;
1649 // The dynamic tag.
1650 elfcpp::DT tag_;
1651 // The type of entry.
1652 Classification classification_;
1653 };
1654
1655 // Add an entry to the list.
1656 void
1657 add_entry(const Dynamic_entry& entry)
1658 { this->entries_.push_back(entry); }
1659
1660 // Sized version of write function.
1661 template<int size, bool big_endian>
1662 void
1663 sized_write(Output_file* of);
1664
1665 // The type of the list of entries.
1666 typedef std::vector<Dynamic_entry> Dynamic_entries;
1667
a3ad94ed
ILT
1668 // The entries.
1669 Dynamic_entries entries_;
1670 // The pool used for strings.
1671 Stringpool* pool_;
1672};
1673
a2fb1b05
ILT
1674// An output section. We don't expect to have too many output
1675// sections, so we don't bother to do a template on the size.
1676
54dc6425 1677class Output_section : public Output_data
a2fb1b05
ILT
1678{
1679 public:
1680 // Create an output section, giving the name, type, and flags.
96803768 1681 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
54dc6425 1682 virtual ~Output_section();
a2fb1b05 1683
ead1e424 1684 // Add a new input section SHNDX, named NAME, with header SHDR, from
730cdc88
ILT
1685 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1686 // which applies to this section, or 0 if none, or -1U if more than
a445fddf
ILT
1687 // one. HAVE_SECTIONS_SCRIPT is true if we have a SECTIONS clause
1688 // in a linker script; in that case we need to keep track of input
1689 // sections associated with an output section. Return the offset
1690 // within the output section.
a2fb1b05
ILT
1691 template<int size, bool big_endian>
1692 off_t
730cdc88
ILT
1693 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1694 const char *name,
1695 const elfcpp::Shdr<size, big_endian>& shdr,
a445fddf 1696 unsigned int reloc_shndx, bool have_sections_script);
a2fb1b05 1697
b8e6aad9 1698 // Add generated data POSD to this output section.
c06b7b0b 1699 void
ead1e424
ILT
1700 add_output_section_data(Output_section_data* posd);
1701
a2fb1b05
ILT
1702 // Return the section name.
1703 const char*
1704 name() const
1705 { return this->name_; }
1706
1707 // Return the section type.
1708 elfcpp::Elf_Word
1709 type() const
1710 { return this->type_; }
1711
1712 // Return the section flags.
1713 elfcpp::Elf_Xword
1714 flags() const
1715 { return this->flags_; }
1716
1650c4ff
ILT
1717 // Set the section flags. This may only be used with the Layout
1718 // code when it is prepared to move the section to a different
1719 // segment.
1720 void
1721 set_flags(elfcpp::Elf_Xword flags)
1722 { this->flags_ = flags; }
1723
a3ad94ed
ILT
1724 // Return the entsize field.
1725 uint64_t
1726 entsize() const
1727 { return this->entsize_; }
1728
61ba1cf9
ILT
1729 // Set the entsize field.
1730 void
16649710 1731 set_entsize(uint64_t v);
61ba1cf9 1732
a445fddf
ILT
1733 // Set the load address.
1734 void
1735 set_load_address(uint64_t load_address)
1736 {
1737 this->load_address_ = load_address;
1738 this->has_load_address_ = true;
1739 }
1740
16649710
ILT
1741 // Set the link field to the output section index of a section.
1742 void
14b31740 1743 set_link_section(const Output_data* od)
16649710
ILT
1744 {
1745 gold_assert(this->link_ == 0
1746 && !this->should_link_to_symtab_
1747 && !this->should_link_to_dynsym_);
1748 this->link_section_ = od;
1749 }
1750
1751 // Set the link field to a constant.
61ba1cf9
ILT
1752 void
1753 set_link(unsigned int v)
16649710
ILT
1754 {
1755 gold_assert(this->link_section_ == NULL
1756 && !this->should_link_to_symtab_
1757 && !this->should_link_to_dynsym_);
1758 this->link_ = v;
1759 }
61ba1cf9 1760
16649710
ILT
1761 // Record that this section should link to the normal symbol table.
1762 void
1763 set_should_link_to_symtab()
1764 {
1765 gold_assert(this->link_section_ == NULL
1766 && this->link_ == 0
1767 && !this->should_link_to_dynsym_);
1768 this->should_link_to_symtab_ = true;
1769 }
1770
1771 // Record that this section should link to the dynamic symbol table.
1772 void
1773 set_should_link_to_dynsym()
1774 {
1775 gold_assert(this->link_section_ == NULL
1776 && this->link_ == 0
1777 && !this->should_link_to_symtab_);
1778 this->should_link_to_dynsym_ = true;
1779 }
1780
1781 // Return the info field.
1782 unsigned int
1783 info() const
1784 {
755ab8af
ILT
1785 gold_assert(this->info_section_ == NULL
1786 && this->info_symndx_ == NULL);
16649710
ILT
1787 return this->info_;
1788 }
1789
1790 // Set the info field to the output section index of a section.
1791 void
755ab8af 1792 set_info_section(const Output_section* os)
16649710 1793 {
755ab8af
ILT
1794 gold_assert((this->info_section_ == NULL
1795 || (this->info_section_ == os
1796 && this->info_uses_section_index_))
1797 && this->info_symndx_ == NULL
1798 && this->info_ == 0);
1799 this->info_section_ = os;
1800 this->info_uses_section_index_= true;
16649710
ILT
1801 }
1802
6a74a719
ILT
1803 // Set the info field to the symbol table index of a symbol.
1804 void
1805 set_info_symndx(const Symbol* sym)
1806 {
755ab8af
ILT
1807 gold_assert(this->info_section_ == NULL
1808 && (this->info_symndx_ == NULL
1809 || this->info_symndx_ == sym)
1810 && this->info_ == 0);
6a74a719
ILT
1811 this->info_symndx_ = sym;
1812 }
1813
755ab8af
ILT
1814 // Set the info field to the symbol table index of a section symbol.
1815 void
1816 set_info_section_symndx(const Output_section* os)
1817 {
1818 gold_assert((this->info_section_ == NULL
1819 || (this->info_section_ == os
1820 && !this->info_uses_section_index_))
1821 && this->info_symndx_ == NULL
1822 && this->info_ == 0);
1823 this->info_section_ = os;
1824 this->info_uses_section_index_ = false;
1825 }
1826
16649710 1827 // Set the info field to a constant.
61ba1cf9
ILT
1828 void
1829 set_info(unsigned int v)
16649710 1830 {
755ab8af
ILT
1831 gold_assert(this->info_section_ == NULL
1832 && this->info_symndx_ == NULL
1833 && (this->info_ == 0
1834 || this->info_ == v));
16649710
ILT
1835 this->info_ = v;
1836 }
61ba1cf9
ILT
1837
1838 // Set the addralign field.
1839 void
1840 set_addralign(uint64_t v)
1841 { this->addralign_ = v; }
1842
c06b7b0b
ILT
1843 // Indicate that we need a symtab index.
1844 void
1845 set_needs_symtab_index()
1846 { this->needs_symtab_index_ = true; }
1847
1848 // Return whether we need a symtab index.
1849 bool
1850 needs_symtab_index() const
1851 { return this->needs_symtab_index_; }
1852
1853 // Get the symtab index.
1854 unsigned int
1855 symtab_index() const
1856 {
a3ad94ed 1857 gold_assert(this->symtab_index_ != 0);
c06b7b0b
ILT
1858 return this->symtab_index_;
1859 }
1860
1861 // Set the symtab index.
1862 void
1863 set_symtab_index(unsigned int index)
1864 {
a3ad94ed 1865 gold_assert(index != 0);
c06b7b0b
ILT
1866 this->symtab_index_ = index;
1867 }
1868
1869 // Indicate that we need a dynsym index.
1870 void
1871 set_needs_dynsym_index()
1872 { this->needs_dynsym_index_ = true; }
1873
1874 // Return whether we need a dynsym index.
1875 bool
1876 needs_dynsym_index() const
1877 { return this->needs_dynsym_index_; }
1878
1879 // Get the dynsym index.
1880 unsigned int
1881 dynsym_index() const
1882 {
a3ad94ed 1883 gold_assert(this->dynsym_index_ != 0);
c06b7b0b
ILT
1884 return this->dynsym_index_;
1885 }
1886
1887 // Set the dynsym index.
1888 void
1889 set_dynsym_index(unsigned int index)
1890 {
a3ad94ed 1891 gold_assert(index != 0);
c06b7b0b
ILT
1892 this->dynsym_index_ = index;
1893 }
1894
730cdc88
ILT
1895 // Return whether this section should be written after all the input
1896 // sections are complete.
1897 bool
1898 after_input_sections() const
1899 { return this->after_input_sections_; }
1900
1901 // Record that this section should be written after all the input
1902 // sections are complete.
1903 void
1904 set_after_input_sections()
1905 { this->after_input_sections_ = true; }
1906
27bc2bce
ILT
1907 // Return whether this section requires postprocessing after all
1908 // relocations have been applied.
1909 bool
1910 requires_postprocessing() const
1911 { return this->requires_postprocessing_; }
1912
96803768
ILT
1913 // If a section requires postprocessing, return the buffer to use.
1914 unsigned char*
1915 postprocessing_buffer() const
1916 {
1917 gold_assert(this->postprocessing_buffer_ != NULL);
1918 return this->postprocessing_buffer_;
1919 }
1920
1921 // If a section requires postprocessing, create the buffer to use.
27bc2bce 1922 void
96803768
ILT
1923 create_postprocessing_buffer();
1924
1925 // If a section requires postprocessing, this is the size of the
1926 // buffer to which relocations should be applied.
1927 off_t
1928 postprocessing_buffer_size() const
1929 { return this->current_data_size_for_child(); }
27bc2bce 1930
755ab8af
ILT
1931 // Modify the section name. This is only permitted for an
1932 // unallocated section, and only before the size has been finalized.
1933 // Otherwise the name will not get into Layout::namepool_.
1934 void
1935 set_name(const char* newname)
1936 {
1937 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1938 gold_assert(!this->is_data_size_valid());
1939 this->name_ = newname;
1940 }
1941
730cdc88
ILT
1942 // Return whether the offset OFFSET in the input section SHNDX in
1943 // object OBJECT is being included in the link.
1944 bool
1945 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1946 off_t offset) const;
1947
1948 // Return the offset within the output section of OFFSET relative to
1949 // the start of input section SHNDX in object OBJECT.
8383303e
ILT
1950 section_offset_type
1951 output_offset(const Relobj* object, unsigned int shndx,
1952 section_offset_type offset) const;
730cdc88 1953
b8e6aad9
ILT
1954 // Return the output virtual address of OFFSET relative to the start
1955 // of input section SHNDX in object OBJECT.
1956 uint64_t
1957 output_address(const Relobj* object, unsigned int shndx,
1958 off_t offset) const;
1959
a9a60db6
ILT
1960 // Return the output address of the start of the merged section for
1961 // input section SHNDX in object OBJECT. This is not necessarily
1962 // the offset corresponding to input offset 0 in the section, since
1963 // the section may be mapped arbitrarily.
1964 uint64_t
1965 starting_output_address(const Relobj* object, unsigned int shndx) const;
1966
a445fddf
ILT
1967 // Record that this output section was found in the SECTIONS clause
1968 // of a linker script.
1969 void
1970 set_found_in_sections_clause()
1971 { this->found_in_sections_clause_ = true; }
1972
1973 // Return whether this output section was found in the SECTIONS
1974 // clause of a linker script.
1975 bool
1976 found_in_sections_clause() const
1977 { return this->found_in_sections_clause_; }
1978
27bc2bce
ILT
1979 // Write the section header into *OPHDR.
1980 template<int size, bool big_endian>
1981 void
1982 write_header(const Layout*, const Stringpool*,
1983 elfcpp::Shdr_write<size, big_endian>*) const;
1984
a445fddf
ILT
1985 // The next few calls are for linker script support.
1986
1987 // Store the list of input sections for this Output_section into the
1988 // list passed in. This removes the input sections, leaving only
1989 // any Output_section_data elements. This returns the size of those
1990 // Output_section_data elements. ADDRESS is the address of this
1991 // output section. FILL is the fill value to use, in case there are
1992 // any spaces between the remaining Output_section_data elements.
1993 uint64_t
1994 get_input_sections(uint64_t address, const std::string& fill,
1995 std::list<std::pair<Relobj*, unsigned int > >*);
1996
1997 // Add an input section from a script.
1998 void
1999 add_input_section_for_script(Relobj* object, unsigned int shndx,
2000 off_t data_size, uint64_t addralign);
2001
2002 // Set the current size of the output section.
2003 void
2004 set_current_data_size(off_t size)
2005 { this->set_current_data_size_for_child(size); }
2006
2007 // Get the current size of the output section.
2008 off_t
2009 current_data_size() const
2010 { return this->current_data_size_for_child(); }
2011
2012 // End of linker script support.
2013
38c5e8b4
ILT
2014 // Print merge statistics to stderr.
2015 void
2016 print_merge_stats();
2017
27bc2bce 2018 protected:
77e65537
ILT
2019 // Return the output section--i.e., the object itself.
2020 Output_section*
2021 do_output_section()
2022 { return this; }
2023
27bc2bce
ILT
2024 // Return the section index in the output file.
2025 unsigned int
2026 do_out_shndx() const
2027 {
2028 gold_assert(this->out_shndx_ != -1U);
2029 return this->out_shndx_;
2030 }
2031
2032 // Set the output section index.
2033 void
2034 do_set_out_shndx(unsigned int shndx)
2035 {
a445fddf 2036 gold_assert(this->out_shndx_ == -1U || this->out_shndx_ == shndx);
27bc2bce
ILT
2037 this->out_shndx_ = shndx;
2038 }
2039
2040 // Set the final data size of the Output_section. For a typical
ead1e424 2041 // Output_section, there is nothing to do, but if there are any
27bc2bce 2042 // Output_section_data objects we need to set their final addresses
ead1e424 2043 // here.
96803768 2044 virtual void
27bc2bce 2045 set_final_data_size();
ead1e424 2046
a445fddf
ILT
2047 // Reset the address and file offset.
2048 void
2049 do_reset_address_and_file_offset();
2050
54dc6425 2051 // Write the data to the file. For a typical Output_section, this
ead1e424
ILT
2052 // does nothing: the data is written out by calling Object::Relocate
2053 // on each input object. But if there are any Output_section_data
2054 // objects we do need to write them out here.
96803768 2055 virtual void
ead1e424 2056 do_write(Output_file*);
54dc6425 2057
75f65a3e
ILT
2058 // Return the address alignment--function required by parent class.
2059 uint64_t
2060 do_addralign() const
2061 { return this->addralign_; }
2062
a445fddf
ILT
2063 // Return whether there is a load address.
2064 bool
2065 do_has_load_address() const
2066 { return this->has_load_address_; }
2067
2068 // Return the load address.
2069 uint64_t
2070 do_load_address() const
2071 {
2072 gold_assert(this->has_load_address_);
2073 return this->load_address_;
2074 }
2075
75f65a3e
ILT
2076 // Return whether this is an Output_section.
2077 bool
2078 do_is_section() const
2079 { return true; }
2080
54dc6425
ILT
2081 // Return whether this is a section of the specified type.
2082 bool
75f65a3e 2083 do_is_section_type(elfcpp::Elf_Word type) const
54dc6425
ILT
2084 { return this->type_ == type; }
2085
2086 // Return whether the specified section flag is set.
2087 bool
75f65a3e 2088 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
54dc6425
ILT
2089 { return (this->flags_ & flag) != 0; }
2090
7bf1f802
ILT
2091 // Set the TLS offset. Called only for SHT_TLS sections.
2092 void
2093 do_set_tls_offset(uint64_t tls_base);
2094
2095 // Return the TLS offset, relative to the base of the TLS segment.
2096 // Valid only for SHT_TLS sections.
2097 uint64_t
2098 do_tls_offset() const
2099 { return this->tls_offset_; }
2100
96803768
ILT
2101 // This may be implemented by a child class.
2102 virtual void
2103 do_finalize_name(Layout*)
2104 { }
2105
2106 // Record that this section requires postprocessing after all
2107 // relocations have been applied. This is called by a child class.
2108 void
2109 set_requires_postprocessing()
2110 {
2111 this->requires_postprocessing_ = true;
2112 this->after_input_sections_ = true;
2113 }
2114
2115 // Write all the data of an Output_section into the postprocessing
2116 // buffer.
2117 void
2118 write_to_postprocessing_buffer();
2119
a2fb1b05 2120 private:
ead1e424
ILT
2121 // In some cases we need to keep a list of the input sections
2122 // associated with this output section. We only need the list if we
2123 // might have to change the offsets of the input section within the
2124 // output section after we add the input section. The ordinary
2125 // input sections will be written out when we process the object
2126 // file, and as such we don't need to track them here. We do need
2127 // to track Output_section_data objects here. We store instances of
2128 // this structure in a std::vector, so it must be a POD. There can
2129 // be many instances of this structure, so we use a union to save
2130 // some space.
2131 class Input_section
2132 {
2133 public:
2134 Input_section()
b8e6aad9
ILT
2135 : shndx_(0), p2align_(0)
2136 {
2137 this->u1_.data_size = 0;
2138 this->u2_.object = NULL;
2139 }
ead1e424 2140
b8e6aad9 2141 // For an ordinary input section.
f6ce93d6 2142 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
ead1e424
ILT
2143 uint64_t addralign)
2144 : shndx_(shndx),
b8e6aad9 2145 p2align_(ffsll(static_cast<long long>(addralign)))
ead1e424 2146 {
b8e6aad9
ILT
2147 gold_assert(shndx != OUTPUT_SECTION_CODE
2148 && shndx != MERGE_DATA_SECTION_CODE
2149 && shndx != MERGE_STRING_SECTION_CODE);
2150 this->u1_.data_size = data_size;
2151 this->u2_.object = object;
ead1e424
ILT
2152 }
2153
b8e6aad9 2154 // For a non-merge output section.
ead1e424 2155 Input_section(Output_section_data* posd)
b8e6aad9
ILT
2156 : shndx_(OUTPUT_SECTION_CODE),
2157 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2158 {
2159 this->u1_.data_size = 0;
2160 this->u2_.posd = posd;
2161 }
2162
2163 // For a merge section.
2164 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
2165 : shndx_(is_string
2166 ? MERGE_STRING_SECTION_CODE
2167 : MERGE_DATA_SECTION_CODE),
2168 p2align_(ffsll(static_cast<long long>(posd->addralign())))
2169 {
2170 this->u1_.entsize = entsize;
2171 this->u2_.posd = posd;
2172 }
ead1e424
ILT
2173
2174 // The required alignment.
2175 uint64_t
2176 addralign() const
a3ad94ed
ILT
2177 {
2178 return (this->p2align_ == 0
2179 ? 0
2180 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
2181 }
ead1e424
ILT
2182
2183 // Return the required size.
2184 off_t
2185 data_size() const;
2186
a445fddf
ILT
2187 // Whether this is an input section.
2188 bool
2189 is_input_section() const
2190 {
2191 return (this->shndx_ != OUTPUT_SECTION_CODE
2192 && this->shndx_ != MERGE_DATA_SECTION_CODE
2193 && this->shndx_ != MERGE_STRING_SECTION_CODE);
2194 }
2195
b8e6aad9
ILT
2196 // Return whether this is a merge section which matches the
2197 // parameters.
2198 bool
87f95776
ILT
2199 is_merge_section(bool is_string, uint64_t entsize,
2200 uint64_t addralign) const
b8e6aad9
ILT
2201 {
2202 return (this->shndx_ == (is_string
2203 ? MERGE_STRING_SECTION_CODE
2204 : MERGE_DATA_SECTION_CODE)
87f95776
ILT
2205 && this->u1_.entsize == entsize
2206 && this->addralign() == addralign);
b8e6aad9
ILT
2207 }
2208
a445fddf
ILT
2209 // Return the object for an input section.
2210 Relobj*
2211 relobj() const
2212 {
2213 gold_assert(this->is_input_section());
2214 return this->u2_.object;
2215 }
2216
2217 // Return the input section index for an input section.
2218 unsigned int
2219 shndx() const
2220 {
2221 gold_assert(this->is_input_section());
2222 return this->shndx_;
2223 }
2224
b8e6aad9
ILT
2225 // Set the output section.
2226 void
2227 set_output_section(Output_section* os)
2228 {
2229 gold_assert(!this->is_input_section());
2230 this->u2_.posd->set_output_section(os);
2231 }
2232
ead1e424 2233 // Set the address and file offset. This is called during
96803768
ILT
2234 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
2235 // the enclosing section.
ead1e424 2236 void
96803768
ILT
2237 set_address_and_file_offset(uint64_t address, off_t file_offset,
2238 off_t section_file_offset);
ead1e424 2239
a445fddf
ILT
2240 // Reset the address and file offset.
2241 void
2242 reset_address_and_file_offset();
2243
96803768
ILT
2244 // Finalize the data size.
2245 void
2246 finalize_data_size();
9a0910c3 2247
b8e6aad9
ILT
2248 // Add an input section, for SHF_MERGE sections.
2249 bool
2250 add_input_section(Relobj* object, unsigned int shndx)
2251 {
2252 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
2253 || this->shndx_ == MERGE_STRING_SECTION_CODE);
2254 return this->u2_.posd->add_input_section(object, shndx);
2255 }
2256
2257 // Given an input OBJECT, an input section index SHNDX within that
2258 // object, and an OFFSET relative to the start of that input
730cdc88 2259 // section, return whether or not the output offset is known. If
1e983657
ILT
2260 // this function returns true, it sets *POUTPUT to the offset in
2261 // the output section, relative to the start of the input section
2262 // in the output section. *POUTPUT may be different from OFFSET
2263 // for a merged section.
b8e6aad9 2264 bool
8383303e
ILT
2265 output_offset(const Relobj* object, unsigned int shndx,
2266 section_offset_type offset,
2267 section_offset_type *poutput) const;
b8e6aad9 2268
a9a60db6
ILT
2269 // Return whether this is the merge section for the input section
2270 // SHNDX in OBJECT.
2271 bool
2272 is_merge_section_for(const Relobj* object, unsigned int shndx) const;
2273
ead1e424
ILT
2274 // Write out the data. This does nothing for an input section.
2275 void
2276 write(Output_file*);
2277
96803768
ILT
2278 // Write the data to a buffer. This does nothing for an input
2279 // section.
2280 void
2281 write_to_buffer(unsigned char*);
2282
38c5e8b4
ILT
2283 // Print statistics about merge sections to stderr.
2284 void
2285 print_merge_stats(const char* section_name)
2286 {
2287 if (this->shndx_ == MERGE_DATA_SECTION_CODE
2288 || this->shndx_ == MERGE_STRING_SECTION_CODE)
2289 this->u2_.posd->print_merge_stats(section_name);
2290 }
2291
ead1e424 2292 private:
b8e6aad9
ILT
2293 // Code values which appear in shndx_. If the value is not one of
2294 // these codes, it is the input section index in the object file.
2295 enum
2296 {
2297 // An Output_section_data.
2298 OUTPUT_SECTION_CODE = -1U,
2299 // An Output_section_data for an SHF_MERGE section with
2300 // SHF_STRINGS not set.
2301 MERGE_DATA_SECTION_CODE = -2U,
2302 // An Output_section_data for an SHF_MERGE section with
2303 // SHF_STRINGS set.
2304 MERGE_STRING_SECTION_CODE = -3U
2305 };
2306
b8e6aad9
ILT
2307 // For an ordinary input section, this is the section index in the
2308 // input file. For an Output_section_data, this is
2309 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2310 // MERGE_STRING_SECTION_CODE.
ead1e424
ILT
2311 unsigned int shndx_;
2312 // The required alignment, stored as a power of 2.
2313 unsigned int p2align_;
ead1e424
ILT
2314 union
2315 {
b8e6aad9
ILT
2316 // For an ordinary input section, the section size.
2317 off_t data_size;
2318 // For OUTPUT_SECTION_CODE, this is not used. For
2319 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
2320 // entity size.
2321 uint64_t entsize;
2322 } u1_;
2323 union
2324 {
2325 // For an ordinary input section, the object which holds the
ead1e424 2326 // input section.
f6ce93d6 2327 Relobj* object;
b8e6aad9
ILT
2328 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
2329 // MERGE_STRING_SECTION_CODE, the data.
ead1e424 2330 Output_section_data* posd;
b8e6aad9 2331 } u2_;
ead1e424
ILT
2332 };
2333
2334 typedef std::vector<Input_section> Input_section_list;
2335
c51e6221 2336 // Fill data. This is used to fill in data between input sections.
a445fddf
ILT
2337 // It is also used for data statements (BYTE, WORD, etc.) in linker
2338 // scripts. When we have to keep track of the input sections, we
2339 // can use an Output_data_const, but we don't want to have to keep
2340 // track of input sections just to implement fills.
c51e6221
ILT
2341 class Fill
2342 {
2343 public:
2344 Fill(off_t section_offset, off_t length)
a445fddf
ILT
2345 : section_offset_(section_offset),
2346 length_(convert_to_section_size_type(length))
c51e6221
ILT
2347 { }
2348
2349 // Return section offset.
2350 off_t
2351 section_offset() const
2352 { return this->section_offset_; }
2353
2354 // Return fill length.
a445fddf 2355 section_size_type
c51e6221
ILT
2356 length() const
2357 { return this->length_; }
2358
2359 private:
2360 // The offset within the output section.
2361 off_t section_offset_;
2362 // The length of the space to fill.
a445fddf 2363 section_size_type length_;
c51e6221
ILT
2364 };
2365
2366 typedef std::vector<Fill> Fill_list;
2367
b8e6aad9
ILT
2368 // Add a new output section by Input_section.
2369 void
2370 add_output_section_data(Input_section*);
2371
2372 // Add an SHF_MERGE input section. Returns true if the section was
2373 // handled.
2374 bool
2375 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
96803768 2376 uint64_t entsize, uint64_t addralign);
b8e6aad9
ILT
2377
2378 // Add an output SHF_MERGE section POSD to this output section.
2379 // IS_STRING indicates whether it is a SHF_STRINGS section, and
2380 // ENTSIZE is the entity size. This returns the entry added to
2381 // input_sections_.
2382 void
2383 add_output_merge_section(Output_section_data* posd, bool is_string,
2384 uint64_t entsize);
2385
a2fb1b05
ILT
2386 // Most of these fields are only valid after layout.
2387
2388 // The name of the section. This will point into a Stringpool.
9a0910c3 2389 const char* name_;
75f65a3e 2390 // The section address is in the parent class.
a2fb1b05
ILT
2391 // The section alignment.
2392 uint64_t addralign_;
2393 // The section entry size.
2394 uint64_t entsize_;
a445fddf
ILT
2395 // The load address. This is only used when using a linker script
2396 // with a SECTIONS clause. The has_load_address_ field indicates
2397 // whether this field is valid.
2398 uint64_t load_address_;
75f65a3e 2399 // The file offset is in the parent class.
16649710 2400 // Set the section link field to the index of this section.
14b31740 2401 const Output_data* link_section_;
16649710 2402 // If link_section_ is NULL, this is the link field.
a2fb1b05 2403 unsigned int link_;
16649710 2404 // Set the section info field to the index of this section.
755ab8af 2405 const Output_section* info_section_;
6a74a719
ILT
2406 // If info_section_ is NULL, set the info field to the symbol table
2407 // index of this symbol.
2408 const Symbol* info_symndx_;
2409 // If info_section_ and info_symndx_ are NULL, this is the section
2410 // info field.
a2fb1b05
ILT
2411 unsigned int info_;
2412 // The section type.
27bc2bce 2413 const elfcpp::Elf_Word type_;
a2fb1b05 2414 // The section flags.
a445fddf 2415 elfcpp::Elf_Xword flags_;
61ba1cf9 2416 // The section index.
ead1e424 2417 unsigned int out_shndx_;
c06b7b0b
ILT
2418 // If there is a STT_SECTION for this output section in the normal
2419 // symbol table, this is the symbol index. This starts out as zero.
2420 // It is initialized in Layout::finalize() to be the index, or -1U
2421 // if there isn't one.
2422 unsigned int symtab_index_;
2423 // If there is a STT_SECTION for this output section in the dynamic
2424 // symbol table, this is the symbol index. This starts out as zero.
2425 // It is initialized in Layout::finalize() to be the index, or -1U
2426 // if there isn't one.
2427 unsigned int dynsym_index_;
ead1e424
ILT
2428 // The input sections. This will be empty in cases where we don't
2429 // need to keep track of them.
2430 Input_section_list input_sections_;
2431 // The offset of the first entry in input_sections_.
2432 off_t first_input_offset_;
c51e6221
ILT
2433 // The fill data. This is separate from input_sections_ because we
2434 // often will need fill sections without needing to keep track of
2435 // input sections.
2436 Fill_list fills_;
96803768
ILT
2437 // If the section requires postprocessing, this buffer holds the
2438 // section contents during relocation.
2439 unsigned char* postprocessing_buffer_;
c06b7b0b
ILT
2440 // Whether this output section needs a STT_SECTION symbol in the
2441 // normal symbol table. This will be true if there is a relocation
2442 // which needs it.
2443 bool needs_symtab_index_ : 1;
2444 // Whether this output section needs a STT_SECTION symbol in the
2445 // dynamic symbol table. This will be true if there is a dynamic
2446 // relocation which needs it.
2447 bool needs_dynsym_index_ : 1;
16649710
ILT
2448 // Whether the link field of this output section should point to the
2449 // normal symbol table.
2450 bool should_link_to_symtab_ : 1;
2451 // Whether the link field of this output section should point to the
2452 // dynamic symbol table.
2453 bool should_link_to_dynsym_ : 1;
730cdc88
ILT
2454 // Whether this section should be written after all the input
2455 // sections are complete.
2456 bool after_input_sections_ : 1;
27bc2bce
ILT
2457 // Whether this section requires post processing after all
2458 // relocations have been applied.
2459 bool requires_postprocessing_ : 1;
a445fddf
ILT
2460 // Whether an input section was mapped to this output section
2461 // because of a SECTIONS clause in a linker script.
2462 bool found_in_sections_clause_ : 1;
2463 // Whether this section has an explicitly specified load address.
2464 bool has_load_address_ : 1;
755ab8af
ILT
2465 // True if the info_section_ field means the section index of the
2466 // section, false if it means the symbol index of the corresponding
2467 // section symbol.
2468 bool info_uses_section_index_ : 1;
7bf1f802
ILT
2469 // For SHT_TLS sections, the offset of this section relative to the base
2470 // of the TLS segment.
2471 uint64_t tls_offset_;
a2fb1b05
ILT
2472};
2473
2474// An output segment. PT_LOAD segments are built from collections of
2475// output sections. Other segments typically point within PT_LOAD
2476// segments, and are built directly as needed.
2477
2478class Output_segment
2479{
2480 public:
2481 // Create an output segment, specifying the type and flags.
2482 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2483
2484 // Return the virtual address.
2485 uint64_t
2486 vaddr() const
2487 { return this->vaddr_; }
2488
2489 // Return the physical address.
2490 uint64_t
2491 paddr() const
2492 { return this->paddr_; }
2493
2494 // Return the segment type.
2495 elfcpp::Elf_Word
2496 type() const
2497 { return this->type_; }
2498
2499 // Return the segment flags.
2500 elfcpp::Elf_Word
2501 flags() const
2502 { return this->flags_; }
2503
92e059d8
ILT
2504 // Return the memory size.
2505 uint64_t
2506 memsz() const
2507 { return this->memsz_; }
2508
ead1e424
ILT
2509 // Return the file size.
2510 off_t
2511 filesz() const
2512 { return this->filesz_; }
2513
516cb3d0
ILT
2514 // Return the file offset.
2515 off_t
2516 offset() const
2517 { return this->offset_; }
2518
75f65a3e
ILT
2519 // Return the maximum alignment of the Output_data.
2520 uint64_t
a445fddf 2521 maximum_alignment();
75f65a3e 2522
a2fb1b05
ILT
2523 // Add an Output_section to this segment.
2524 void
dbe717ef
ILT
2525 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2526 { this->add_output_section(os, seg_flags, false); }
2527
2528 // Add an Output_section to the start of this segment.
2529 void
2530 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2531 { this->add_output_section(os, seg_flags, true); }
75f65a3e 2532
1650c4ff
ILT
2533 // Remove an Output_section from this segment. It is an error if it
2534 // is not present.
2535 void
2536 remove_output_section(Output_section* os);
2537
75f65a3e
ILT
2538 // Add an Output_data (which is not an Output_section) to the start
2539 // of this segment.
2540 void
2541 add_initial_output_data(Output_data*);
2542
756ac4a8
ILT
2543 // Return true if this segment has any sections which hold actual
2544 // data, rather than being a BSS section.
2545 bool
2546 has_any_data_sections() const
2547 { return !this->output_data_.empty(); }
2548
4f4c5f80
ILT
2549 // Return the number of dynamic relocations applied to this segment.
2550 unsigned int
2551 dynamic_reloc_count() const;
2552
a445fddf
ILT
2553 // Return the address of the first section.
2554 uint64_t
2555 first_section_load_address() const;
2556
2557 // Return whether the addresses have been set already.
2558 bool
2559 are_addresses_set() const
2560 { return this->are_addresses_set_; }
2561
2562 // Set the addresses.
2563 void
2564 set_addresses(uint64_t vaddr, uint64_t paddr)
2565 {
2566 this->vaddr_ = vaddr;
2567 this->paddr_ = paddr;
2568 this->are_addresses_set_ = true;
2569 }
2570
1c4f3631
ILT
2571 // Set the segment flags. This is only used if we have a PHDRS
2572 // clause which explicitly specifies the flags.
2573 void
2574 set_flags(elfcpp::Elf_Word flags)
2575 { this->flags_ = flags; }
2576
75f65a3e 2577 // Set the address of the segment to ADDR and the offset to *POFF
a445fddf
ILT
2578 // and set the addresses and offsets of all contained output
2579 // sections accordingly. Set the section indexes of all contained
2580 // output sections starting with *PSHNDX. If RESET is true, first
2581 // reset the addresses of the contained sections. Return the
2582 // address of the immediately following segment. Update *POFF and
2583 // *PSHNDX. This should only be called for a PT_LOAD segment.
75f65a3e 2584 uint64_t
96a2b4e4 2585 set_section_addresses(const Layout*, bool reset, uint64_t addr, off_t* poff,
a445fddf 2586 unsigned int* pshndx);
75f65a3e 2587
0496d5e5
ILT
2588 // Set the minimum alignment of this segment. This may be adjusted
2589 // upward based on the section alignments.
2590 void
a445fddf
ILT
2591 set_minimum_p_align(uint64_t align)
2592 { this->min_p_align_ = align; }
0496d5e5 2593
75f65a3e
ILT
2594 // Set the offset of this segment based on the section. This should
2595 // only be called for a non-PT_LOAD segment.
2596 void
2597 set_offset();
2598
7bf1f802
ILT
2599 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2600 void
2601 set_tls_offsets();
2602
75f65a3e
ILT
2603 // Return the number of output sections.
2604 unsigned int
2605 output_section_count() const;
a2fb1b05 2606
1c4f3631
ILT
2607 // Return the section attached to the list segment with the lowest
2608 // load address. This is used when handling a PHDRS clause in a
2609 // linker script.
2610 Output_section*
2611 section_with_lowest_load_address() const;
2612
61ba1cf9
ILT
2613 // Write the segment header into *OPHDR.
2614 template<int size, bool big_endian>
2615 void
ead1e424 2616 write_header(elfcpp::Phdr_write<size, big_endian>*);
61ba1cf9
ILT
2617
2618 // Write the section headers of associated sections into V.
2619 template<int size, bool big_endian>
2620 unsigned char*
16649710 2621 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
7d1a9ebb 2622 unsigned int* pshndx) const;
61ba1cf9 2623
a2fb1b05
ILT
2624 private:
2625 Output_segment(const Output_segment&);
2626 Output_segment& operator=(const Output_segment&);
2627
54dc6425 2628 typedef std::list<Output_data*> Output_data_list;
a2fb1b05 2629
dbe717ef
ILT
2630 // Add an Output_section to this segment, specifying front or back.
2631 void
2632 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2633 bool front);
2634
ead1e424
ILT
2635 // Find the maximum alignment in an Output_data_list.
2636 static uint64_t
a445fddf 2637 maximum_alignment_list(const Output_data_list*);
ead1e424 2638
75f65a3e
ILT
2639 // Set the section addresses in an Output_data_list.
2640 uint64_t
96a2b4e4
ILT
2641 set_section_list_addresses(const Layout*, bool reset, Output_data_list*,
2642 uint64_t addr, off_t* poff, unsigned int* pshndx,
2643 bool* in_tls);
75f65a3e
ILT
2644
2645 // Return the number of Output_sections in an Output_data_list.
2646 unsigned int
2647 output_section_count_list(const Output_data_list*) const;
2648
4f4c5f80
ILT
2649 // Return the number of dynamic relocs in an Output_data_list.
2650 unsigned int
2651 dynamic_reloc_count_list(const Output_data_list*) const;
2652
1c4f3631
ILT
2653 // Find the section with the lowest load address in an
2654 // Output_data_list.
2655 void
2656 lowest_load_address_in_list(const Output_data_list* pdl,
2657 Output_section** found,
2658 uint64_t* found_lma) const;
2659
61ba1cf9
ILT
2660 // Write the section headers in the list into V.
2661 template<int size, bool big_endian>
2662 unsigned char*
16649710
ILT
2663 write_section_headers_list(const Layout*, const Stringpool*,
2664 const Output_data_list*, unsigned char* v,
7d1a9ebb 2665 unsigned int* pshdx) const;
61ba1cf9 2666
75f65a3e 2667 // The list of output data with contents attached to this segment.
54dc6425 2668 Output_data_list output_data_;
75f65a3e
ILT
2669 // The list of output data without contents attached to this segment.
2670 Output_data_list output_bss_;
a2fb1b05
ILT
2671 // The segment virtual address.
2672 uint64_t vaddr_;
2673 // The segment physical address.
2674 uint64_t paddr_;
2675 // The size of the segment in memory.
2676 uint64_t memsz_;
a445fddf
ILT
2677 // The maximum section alignment. The is_max_align_known_ field
2678 // indicates whether this has been finalized.
2679 uint64_t max_align_;
2680 // The required minimum value for the p_align field. This is used
2681 // for PT_LOAD segments. Note that this does not mean that
2682 // addresses should be aligned to this value; it means the p_paddr
2683 // and p_vaddr fields must be congruent modulo this value. For
2684 // non-PT_LOAD segments, the dynamic linker works more efficiently
2685 // if the p_align field has the more conventional value, although it
2686 // can align as needed.
2687 uint64_t min_p_align_;
a2fb1b05
ILT
2688 // The offset of the segment data within the file.
2689 off_t offset_;
2690 // The size of the segment data in the file.
2691 off_t filesz_;
2692 // The segment type;
2693 elfcpp::Elf_Word type_;
2694 // The segment flags.
2695 elfcpp::Elf_Word flags_;
a445fddf
ILT
2696 // Whether we have finalized max_align_.
2697 bool is_max_align_known_ : 1;
2698 // Whether vaddr and paddr were set by a linker script.
2699 bool are_addresses_set_ : 1;
a2fb1b05
ILT
2700};
2701
61ba1cf9 2702// This class represents the output file.
a2fb1b05
ILT
2703
2704class Output_file
2705{
2706 public:
14144f39 2707 Output_file(const char* name);
61ba1cf9 2708
516cb3d0
ILT
2709 // Indicate that this is a temporary file which should not be
2710 // output.
2711 void
2712 set_is_temporary()
2713 { this->is_temporary_ = true; }
2714
61ba1cf9
ILT
2715 // Open the output file. FILE_SIZE is the final size of the file.
2716 void
2717 open(off_t file_size);
2718
27bc2bce
ILT
2719 // Resize the output file.
2720 void
2721 resize(off_t file_size);
2722
c420411f
ILT
2723 // Close the output file (flushing all buffered data) and make sure
2724 // there are no errors.
61ba1cf9
ILT
2725 void
2726 close();
2727
2728 // We currently always use mmap which makes the view handling quite
2729 // simple. In the future we may support other approaches.
a2fb1b05
ILT
2730
2731 // Write data to the output file.
2732 void
fe8718a4 2733 write(off_t offset, const void* data, size_t len)
61ba1cf9
ILT
2734 { memcpy(this->base_ + offset, data, len); }
2735
2736 // Get a buffer to use to write to the file, given the offset into
2737 // the file and the size.
2738 unsigned char*
fe8718a4 2739 get_output_view(off_t start, size_t size)
61ba1cf9 2740 {
8d32f935
ILT
2741 gold_assert(start >= 0
2742 && start + static_cast<off_t>(size) <= this->file_size_);
61ba1cf9
ILT
2743 return this->base_ + start;
2744 }
2745
2746 // VIEW must have been returned by get_output_view. Write the
2747 // buffer to the file, passing in the offset and the size.
2748 void
fe8718a4 2749 write_output_view(off_t, size_t, unsigned char*)
61ba1cf9
ILT
2750 { }
2751
730cdc88
ILT
2752 // Get a read/write buffer. This is used when we want to write part
2753 // of the file, read it in, and write it again.
2754 unsigned char*
fe8718a4 2755 get_input_output_view(off_t start, size_t size)
730cdc88
ILT
2756 { return this->get_output_view(start, size); }
2757
2758 // Write a read/write buffer back to the file.
2759 void
fe8718a4 2760 write_input_output_view(off_t, size_t, unsigned char*)
730cdc88
ILT
2761 { }
2762
2763 // Get a read buffer. This is used when we just want to read part
2764 // of the file back it in.
2765 const unsigned char*
fe8718a4 2766 get_input_view(off_t start, size_t size)
730cdc88
ILT
2767 { return this->get_output_view(start, size); }
2768
2769 // Release a read bfufer.
2770 void
fe8718a4 2771 free_input_view(off_t, size_t, const unsigned char*)
730cdc88
ILT
2772 { }
2773
61ba1cf9 2774 private:
c420411f 2775 // Map the file into memory and return a pointer to the map.
27bc2bce
ILT
2776 void
2777 map();
2778
c420411f
ILT
2779 // Unmap the file from memory (and flush to disk buffers).
2780 void
2781 unmap();
2782
61ba1cf9
ILT
2783 // File name.
2784 const char* name_;
2785 // File descriptor.
2786 int o_;
2787 // File size.
2788 off_t file_size_;
2789 // Base of file mapped into memory.
2790 unsigned char* base_;
c420411f
ILT
2791 // True iff base_ points to a memory buffer rather than an output file.
2792 bool map_is_anonymous_;
516cb3d0
ILT
2793 // True if this is a temporary file which should not be output.
2794 bool is_temporary_;
a2fb1b05
ILT
2795};
2796
2797} // End namespace gold.
2798
2799#endif // !defined(GOLD_OUTPUT_H)
This page took 0.241282 seconds and 4 git commands to generate.