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