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