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