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