*** empty log message ***
[deliverable/binutils-gdb.git] / gold / output.h
CommitLineData
a2fb1b05
ILT
1// output.h -- manage the output file for gold -*- C++ -*-
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
a2fb1b05
ILT
23#ifndef GOLD_OUTPUT_H
24#define GOLD_OUTPUT_H
25
26#include <list>
ead1e424 27#include <vector>
a2fb1b05
ILT
28
29#include "elfcpp.h"
54dc6425 30#include "layout.h"
c06b7b0b 31#include "reloc-types.h"
a2fb1b05
ILT
32
33namespace gold
34{
35
61ba1cf9 36class General_options;
a2fb1b05 37class Object;
a3ad94ed 38class Symbol;
a2fb1b05 39class Output_file;
c06b7b0b 40class Output_section;
a3ad94ed 41class Target;
54dc6425
ILT
42template<int size, bool big_endian>
43class Sized_target;
c06b7b0b
ILT
44template<int size, bool big_endian>
45class Sized_relobj;
54dc6425
ILT
46
47// An abtract class for data which has to go into the output file.
a2fb1b05
ILT
48
49class Output_data
50{
51 public:
27bc2bce
ILT
52 explicit Output_data()
53 : address_(0), data_size_(0), offset_(-1),
54 is_address_valid_(false), is_data_size_valid_(false),
55 is_offset_valid_(false),
4f4c5f80 56 dynamic_reloc_count_(0)
a2fb1b05
ILT
57 { }
58
59 virtual
60 ~Output_data();
61
27bc2bce
ILT
62 // Return the address. For allocated sections, this is only valid
63 // after Layout::finalize is finished.
75f65a3e
ILT
64 uint64_t
65 address() const
27bc2bce
ILT
66 {
67 gold_assert(this->is_address_valid_);
68 return this->address_;
69 }
75f65a3e 70
27bc2bce
ILT
71 // Return the size of the data. For allocated sections, this must
72 // be valid after Layout::finalize calls set_address, but need not
73 // be valid before then.
a2fb1b05 74 off_t
75f65a3e 75 data_size() const
27bc2bce
ILT
76 {
77 gold_assert(this->is_data_size_valid_);
78 return this->data_size_;
79 }
75f65a3e 80
ead1e424 81 // Return the file offset. This is only valid after
27bc2bce
ILT
82 // Layout::finalize is finished. For some non-allocated sections,
83 // it may not be valid until near the end of the link.
75f65a3e
ILT
84 off_t
85 offset() const
27bc2bce
ILT
86 {
87 gold_assert(this->is_offset_valid_);
88 return this->offset_;
89 }
75f65a3e
ILT
90
91 // Return the required alignment.
92 uint64_t
93 addralign() const
94 { return this->do_addralign(); }
95
96 // Return whether this is an Output_section.
97 bool
98 is_section() const
99 { return this->do_is_section(); }
100
101 // Return whether this is an Output_section of the specified type.
102 bool
103 is_section_type(elfcpp::Elf_Word stt) const
104 { return this->do_is_section_type(stt); }
105
106 // Return whether this is an Output_section with the specified flag
107 // set.
108 bool
109 is_section_flag_set(elfcpp::Elf_Xword shf) const
110 { return this->do_is_section_flag_set(shf); }
111
ead1e424
ILT
112 // Return the output section index, if there is an output section.
113 unsigned int
114 out_shndx() const
115 { return this->do_out_shndx(); }
116
117 // Set the output section index, if this is an output section.
118 void
119 set_out_shndx(unsigned int shndx)
120 { this->do_set_out_shndx(shndx); }
121
27bc2bce
ILT
122 // Set the address and file offset of this data, and finalize the
123 // size of the data. This is called during Layout::finalize for
124 // allocated sections.
75f65a3e 125 void
27bc2bce
ILT
126 set_address_and_file_offset(uint64_t addr, off_t off)
127 {
128 this->set_address(addr);
129 this->set_file_offset(off);
130 this->finalize_data_size();
131 }
132
133 // Set the address.
134 void
135 set_address(uint64_t addr)
136 {
137 gold_assert(!this->is_address_valid_);
138 this->address_ = addr;
139 this->is_address_valid_ = true;
140 }
141
142 // Set the file offset.
143 void
144 set_file_offset(off_t off)
145 {
146 gold_assert(!this->is_offset_valid_);
147 this->offset_ = off;
148 this->is_offset_valid_ = true;
149 }
150
151 // Finalize the data size.
152 void
153 finalize_data_size()
154 {
155 if (!this->is_data_size_valid_)
156 {
157 // Tell the child class to set the data size.
158 this->set_final_data_size();
159 gold_assert(this->is_data_size_valid_);
160 }
161 }
75f65a3e 162
7bf1f802
ILT
163 // Set the TLS offset. Called only for SHT_TLS sections.
164 void
165 set_tls_offset(uint64_t tls_base)
166 { this->do_set_tls_offset(tls_base); }
167
168 // Return the TLS offset, relative to the base of the TLS segment.
169 // Valid only for SHT_TLS sections.
170 uint64_t
171 tls_offset() const
172 { return this->do_tls_offset(); }
173
ead1e424
ILT
174 // Write the data to the output file. This is called after
175 // Layout::finalize is complete.
75f65a3e
ILT
176 void
177 write(Output_file* file)
178 { this->do_write(file); }
a2fb1b05 179
27bc2bce
ILT
180 // This is called by Layout::finalize to note that the sizes of
181 // allocated sections must now be fixed.
a3ad94ed
ILT
182 static void
183 layout_complete()
27bc2bce 184 { Output_data::allocated_sizes_are_fixed = true; }
a3ad94ed 185
730cdc88
ILT
186 // Used to check that layout has been done.
187 static bool
188 is_layout_complete()
27bc2bce 189 { return Output_data::allocated_sizes_are_fixed; }
730cdc88 190
4f4c5f80
ILT
191 // Count the number of dynamic relocations applied to this section.
192 void
193 add_dynamic_reloc()
194 { ++this->dynamic_reloc_count_; }
195
196 // Return the number of dynamic relocations applied to this section.
197 unsigned int
198 dynamic_reloc_count() const
199 { return this->dynamic_reloc_count_; }
200
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,
e8c846c3 706 Address address, bool is_relative);
5a6f7e2d
ILT
707
708 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
e8c846c3 709 unsigned int shndx, Address address, bool is_relative);
c06b7b0b
ILT
710
711 // A reloc against a local symbol.
5a6f7e2d
ILT
712
713 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 714 unsigned int local_sym_index, unsigned int type,
e8c846c3 715 Output_data* od, Address address, bool is_relative);
5a6f7e2d
ILT
716
717 Output_reloc(Sized_relobj<size, big_endian>* relobj,
7bf1f802 718 unsigned int local_sym_index, unsigned int type,
e8c846c3 719 unsigned int shndx, Address address, bool is_relative);
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 728
e8c846c3
ILT
729 // Return TRUE if this is a RELATIVE relocation.
730 bool
731 is_relative() const
732 { return this->is_relative_; }
733
734 // Get the value of the symbol referred to by a Rel relocation.
735
736 Address
737 symbol_value() const;
738
c06b7b0b
ILT
739 // Write the reloc entry to an output view.
740 void
741 write(unsigned char* pov) const;
742
743 // Write the offset and info fields to Write_rel.
744 template<typename Write_rel>
745 void write_rel(Write_rel*) const;
746
747 private:
748 // Return the symbol index. We can't do a double template
749 // specialization, so we do a secondary template here.
750 unsigned int
751 get_symbol_index() const;
752
753 // Codes for local_sym_index_.
754 enum
755 {
756 // Global symbol.
757 GSYM_CODE = -1U,
758 // Output section.
759 SECTION_CODE = -2U,
760 // Invalid uninitialized entry.
761 INVALID_CODE = -3U
762 };
763
764 union
765 {
766 // For a local symbol, the object. We will never generate a
767 // relocation against a local symbol in a dynamic object; that
768 // doesn't make sense. And our callers will always be
769 // templatized, so we use Sized_relobj here.
5a6f7e2d 770 Sized_relobj<size, big_endian>* relobj;
c06b7b0b
ILT
771 // For a global symbol, the symbol. If this is NULL, it indicates
772 // a relocation against the undefined 0 symbol.
773 Symbol* gsym;
774 // For a relocation against an output section, the output section.
775 Output_section* os;
5a6f7e2d
ILT
776 } u1_;
777 union
778 {
779 // If shndx_ is not INVALID CODE, the object which holds the input
780 // section being used to specify the reloc address.
781 Relobj* relobj;
782 // If shndx_ is INVALID_CODE, the output data being used to
783 // specify the reloc address. This may be NULL if the reloc
784 // address is absolute.
785 Output_data* od;
786 } u2_;
787 // The address offset within the input section or the Output_data.
788 Address address_;
c06b7b0b
ILT
789 // For a local symbol, the local symbol index. This is GSYM_CODE
790 // for a global symbol, or INVALID_CODE for an uninitialized value.
791 unsigned int local_sym_index_;
a3ad94ed 792 // The reloc type--a processor specific code.
e8c846c3
ILT
793 unsigned int type_ : 31;
794 // True if the relocation is a RELATIVE relocation.
795 bool is_relative_ : 1;
5a6f7e2d
ILT
796 // If the reloc address is an input section in an object, the
797 // section index. This is INVALID_CODE if the reloc address is
798 // specified in some other way.
799 unsigned int shndx_;
c06b7b0b
ILT
800};
801
802// The SHT_RELA version of Output_reloc<>. This is just derived from
803// the SHT_REL version of Output_reloc, but it adds an addend.
804
805template<bool dynamic, int size, bool big_endian>
806class Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
807{
808 public:
809 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
810 typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
811
812 // An uninitialized entry.
813 Output_reloc()
814 : rel_()
815 { }
816
817 // A reloc against a global symbol.
5a6f7e2d 818
a3ad94ed 819 Output_reloc(Symbol* gsym, unsigned int type, Output_data* od,
e8c846c3
ILT
820 Address address, Addend addend, bool is_relative)
821 : rel_(gsym, type, od, address, is_relative), addend_(addend)
c06b7b0b
ILT
822 { }
823
5a6f7e2d 824 Output_reloc(Symbol* gsym, unsigned int type, Relobj* relobj,
e8c846c3
ILT
825 unsigned int shndx, Address address, Addend addend,
826 bool is_relative)
827 : rel_(gsym, type, relobj, shndx, address, is_relative), addend_(addend)
5a6f7e2d
ILT
828 { }
829
c06b7b0b 830 // A reloc against a local symbol.
5a6f7e2d
ILT
831
832 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
833 unsigned int local_sym_index, unsigned int type,
834 Output_data* od, Address address,
835 Addend addend, bool is_relative)
836 : rel_(relobj, local_sym_index, type, od, address, is_relative),
837 addend_(addend)
5a6f7e2d
ILT
838 { }
839
840 Output_reloc(Sized_relobj<size, big_endian>* relobj,
e8c846c3
ILT
841 unsigned int local_sym_index, unsigned int type,
842 unsigned int shndx, Address address,
843 Addend addend, bool is_relative)
844 : rel_(relobj, local_sym_index, type, shndx, address, is_relative),
5a6f7e2d 845 addend_(addend)
c06b7b0b
ILT
846 { }
847
848 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 849
a3ad94ed
ILT
850 Output_reloc(Output_section* os, unsigned int type, Output_data* od,
851 Address address, Addend addend)
852 : rel_(os, type, od, address), addend_(addend)
c06b7b0b
ILT
853 { }
854
5a6f7e2d
ILT
855 Output_reloc(Output_section* os, unsigned int type, Relobj* relobj,
856 unsigned int shndx, Address address, Addend addend)
857 : rel_(os, type, relobj, shndx, address), addend_(addend)
858 { }
859
c06b7b0b
ILT
860 // Write the reloc entry to an output view.
861 void
862 write(unsigned char* pov) const;
863
864 private:
865 // The basic reloc.
866 Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian> rel_;
867 // The addend.
868 Addend addend_;
869};
870
871// Output_data_reloc is used to manage a section containing relocs.
872// SH_TYPE is either elfcpp::SHT_REL or elfcpp::SHT_RELA. DYNAMIC
873// indicates whether this is a dynamic relocation or a normal
874// relocation. Output_data_reloc_base is a base class.
875// Output_data_reloc is the real class, which we specialize based on
876// the reloc type.
877
878template<int sh_type, bool dynamic, int size, bool big_endian>
27bc2bce 879class Output_data_reloc_base : public Output_section_data_build
c06b7b0b
ILT
880{
881 public:
882 typedef Output_reloc<sh_type, dynamic, size, big_endian> Output_reloc_type;
883 typedef typename Output_reloc_type::Address Address;
884 static const int reloc_size =
885 Reloc_types<sh_type, size, big_endian>::reloc_size;
886
887 // Construct the section.
888 Output_data_reloc_base()
27bc2bce 889 : Output_section_data_build(Output_data::default_alignment_for_size(size))
c06b7b0b
ILT
890 { }
891
27bc2bce 892 protected:
c06b7b0b
ILT
893 // Write out the data.
894 void
895 do_write(Output_file*);
896
16649710
ILT
897 // Set the entry size and the link.
898 void
899 do_adjust_output_section(Output_section *os);
900
c06b7b0b
ILT
901 // Add a relocation entry.
902 void
4f4c5f80 903 add(Output_data *od, const Output_reloc_type& reloc)
c06b7b0b
ILT
904 {
905 this->relocs_.push_back(reloc);
27bc2bce 906 this->set_current_data_size(this->relocs_.size() * reloc_size);
4f4c5f80 907 od->add_dynamic_reloc();
c06b7b0b
ILT
908 }
909
910 private:
911 typedef std::vector<Output_reloc_type> Relocs;
912
913 Relocs relocs_;
914};
915
916// The class which callers actually create.
917
918template<int sh_type, bool dynamic, int size, bool big_endian>
919class Output_data_reloc;
920
921// The SHT_REL version of Output_data_reloc.
922
923template<bool dynamic, int size, bool big_endian>
924class Output_data_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>
925 : public Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>
926{
927 private:
928 typedef Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size,
929 big_endian> Base;
930
931 public:
932 typedef typename Base::Output_reloc_type Output_reloc_type;
933 typedef typename Output_reloc_type::Address Address;
934
935 Output_data_reloc()
936 : Output_data_reloc_base<elfcpp::SHT_REL, dynamic, size, big_endian>()
937 { }
938
939 // Add a reloc against a global symbol.
5a6f7e2d 940
c06b7b0b 941 void
a3ad94ed 942 add_global(Symbol* gsym, unsigned int type, Output_data* od, Address address)
e8c846c3 943 { this->add(od, Output_reloc_type(gsym, type, od, address, false)); }
c06b7b0b 944
5a6f7e2d 945 void
4f4c5f80 946 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
5a6f7e2d 947 unsigned int shndx, Address address)
e8c846c3
ILT
948 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
949 false)); }
950
951 // Add a RELATIVE reloc against a global symbol. The final relocation
952 // will not reference the symbol.
953
954 void
955 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
956 Address address)
957 { this->add(od, Output_reloc_type(gsym, type, od, address, true)); }
958
959 void
960 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
961 Relobj* relobj, unsigned int shndx, Address address)
962 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
963 true)); }
5a6f7e2d 964
c06b7b0b 965 // Add a reloc against a local symbol.
5a6f7e2d 966
c06b7b0b 967 void
5a6f7e2d 968 add_local(Sized_relobj<size, big_endian>* relobj,
a3ad94ed
ILT
969 unsigned int local_sym_index, unsigned int type,
970 Output_data* od, Address address)
4f4c5f80 971 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
e8c846c3 972 address, false)); }
5a6f7e2d
ILT
973
974 void
975 add_local(Sized_relobj<size, big_endian>* relobj,
976 unsigned int local_sym_index, unsigned int type,
4f4c5f80
ILT
977 Output_data* od, unsigned int shndx, Address address)
978 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
e8c846c3
ILT
979 address, false)); }
980
981 // Add a RELATIVE reloc against a local symbol.
5a6f7e2d 982
e8c846c3
ILT
983 void
984 add_local_relative(Sized_relobj<size, big_endian>* relobj,
985 unsigned int local_sym_index, unsigned int type,
986 Output_data* od, Address address)
987 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, od,
988 address, true)); }
989
990 void
991 add_local_relative(Sized_relobj<size, big_endian>* relobj,
992 unsigned int local_sym_index, unsigned int type,
993 Output_data* od, unsigned int shndx, Address address)
994 { this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
995 address, true)); }
c06b7b0b
ILT
996
997 // A reloc against the STT_SECTION symbol of an output section.
4f4c5f80
ILT
998 // OS is the Output_section that the relocation refers to; OD is
999 // the Output_data object being relocated.
5a6f7e2d 1000
c06b7b0b 1001 void
a3ad94ed
ILT
1002 add_output_section(Output_section* os, unsigned int type,
1003 Output_data* od, Address address)
4f4c5f80 1004 { this->add(od, Output_reloc_type(os, type, od, address)); }
5a6f7e2d
ILT
1005
1006 void
4f4c5f80 1007 add_output_section(Output_section* os, unsigned int type, Output_data* od,
5a6f7e2d 1008 Relobj* relobj, unsigned int shndx, Address address)
4f4c5f80 1009 { this->add(od, Output_reloc_type(os, type, relobj, shndx, address)); }
c06b7b0b
ILT
1010};
1011
1012// The SHT_RELA version of Output_data_reloc.
1013
1014template<bool dynamic, int size, bool big_endian>
1015class Output_data_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>
1016 : public Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>
1017{
1018 private:
1019 typedef Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size,
1020 big_endian> Base;
1021
1022 public:
1023 typedef typename Base::Output_reloc_type Output_reloc_type;
1024 typedef typename Output_reloc_type::Address Address;
1025 typedef typename Output_reloc_type::Addend Addend;
1026
1027 Output_data_reloc()
1028 : Output_data_reloc_base<elfcpp::SHT_RELA, dynamic, size, big_endian>()
1029 { }
1030
1031 // Add a reloc against a global symbol.
5a6f7e2d 1032
c06b7b0b 1033 void
a3ad94ed
ILT
1034 add_global(Symbol* gsym, unsigned int type, Output_data* od,
1035 Address address, Addend addend)
e8c846c3
ILT
1036 { this->add(od, Output_reloc_type(gsym, type, od, address, addend,
1037 false)); }
c06b7b0b 1038
5a6f7e2d 1039 void
4f4c5f80
ILT
1040 add_global(Symbol* gsym, unsigned int type, Output_data* od, Relobj* relobj,
1041 unsigned int shndx, Address address,
1042 Addend addend)
1043 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
e8c846c3
ILT
1044 addend, false)); }
1045
1046 // Add a RELATIVE reloc against a global symbol. The final output
1047 // relocation will not reference the symbol, but we must keep the symbol
1048 // information long enough to set the addend of the relocation correctly
1049 // when it is written.
1050
1051 void
1052 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1053 Address address, Addend addend)
1054 { this->add(od, Output_reloc_type(gsym, type, od, address, addend, true)); }
1055
1056 void
1057 add_global_relative(Symbol* gsym, unsigned int type, Output_data* od,
1058 Relobj* relobj, unsigned int shndx, Address address,
1059 Addend addend)
1060 { this->add(od, Output_reloc_type(gsym, type, relobj, shndx, address,
1061 addend, true)); }
5a6f7e2d 1062
c06b7b0b 1063 // Add a reloc against a local symbol.
5a6f7e2d 1064
c06b7b0b 1065 void
5a6f7e2d 1066 add_local(Sized_relobj<size, big_endian>* relobj,
c06b7b0b 1067 unsigned int local_sym_index, unsigned int type,
a3ad94ed 1068 Output_data* od, Address address, Addend addend)
c06b7b0b 1069 {
4f4c5f80 1070 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
e8c846c3 1071 addend, false));
5a6f7e2d
ILT
1072 }
1073
1074 void
1075 add_local(Sized_relobj<size, big_endian>* relobj,
1076 unsigned int local_sym_index, unsigned int type,
4f4c5f80
ILT
1077 Output_data* od, unsigned int shndx, Address address,
1078 Addend addend)
5a6f7e2d 1079 {
4f4c5f80 1080 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
e8c846c3
ILT
1081 address, addend, false));
1082 }
1083
1084 // Add a RELATIVE reloc against a local symbol.
1085
1086 void
1087 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1088 unsigned int local_sym_index, unsigned int type,
1089 Output_data* od, Address address, Addend addend)
1090 {
1091 this->add(od, Output_reloc_type(relobj, local_sym_index, type, od, address,
1092 addend, true));
1093 }
1094
1095 void
1096 add_local_relative(Sized_relobj<size, big_endian>* relobj,
1097 unsigned int local_sym_index, unsigned int type,
1098 Output_data* od, unsigned int shndx, Address address,
1099 Addend addend)
1100 {
1101 this->add(od, Output_reloc_type(relobj, local_sym_index, type, shndx,
1102 address, addend, true));
c06b7b0b
ILT
1103 }
1104
1105 // A reloc against the STT_SECTION symbol of an output section.
5a6f7e2d 1106
c06b7b0b 1107 void
a3ad94ed
ILT
1108 add_output_section(Output_section* os, unsigned int type, Output_data* od,
1109 Address address, Addend addend)
4f4c5f80 1110 { this->add(os, Output_reloc_type(os, type, od, address, addend)); }
5a6f7e2d
ILT
1111
1112 void
1113 add_output_section(Output_section* os, unsigned int type, Relobj* relobj,
1114 unsigned int shndx, Address address, Addend addend)
4f4c5f80
ILT
1115 { this->add(os, Output_reloc_type(os, type, relobj, shndx, address,
1116 addend)); }
c06b7b0b
ILT
1117};
1118
dbe717ef
ILT
1119// Output_data_got is used to manage a GOT. Each entry in the GOT is
1120// for one symbol--either a global symbol or a local symbol in an
ead1e424 1121// object. The target specific code adds entries to the GOT as
dbe717ef 1122// needed.
ead1e424
ILT
1123
1124template<int size, bool big_endian>
27bc2bce 1125class Output_data_got : public Output_section_data_build
ead1e424
ILT
1126{
1127 public:
1128 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
7bf1f802
ILT
1129 typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> Rel_dyn;
1130 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
ead1e424 1131
7e1edb90 1132 Output_data_got()
27bc2bce 1133 : Output_section_data_build(Output_data::default_alignment_for_size(size)),
730cdc88 1134 entries_()
ead1e424
ILT
1135 { }
1136
dbe717ef
ILT
1137 // Add an entry for a global symbol to the GOT. Return true if this
1138 // is a new GOT entry, false if the symbol was already in the GOT.
1139 bool
1140 add_global(Symbol* gsym);
ead1e424 1141
7bf1f802
ILT
1142 // Add an entry for a global symbol to the GOT, and add a dynamic
1143 // relocation of type R_TYPE for the GOT entry.
1144 void
1145 add_global_with_rel(Symbol* gsym, Rel_dyn* rel_dyn, unsigned int r_type);
1146
1147 void
1148 add_global_with_rela(Symbol* gsym, Rela_dyn* rela_dyn, unsigned int r_type);
1149
e727fa71
ILT
1150 // Add an entry for a local symbol to the GOT. This returns true if
1151 // this is a new GOT entry, false if the symbol already has a GOT
1152 // entry.
1153 bool
1154 add_local(Sized_relobj<size, big_endian>* object, unsigned int sym_index);
ead1e424 1155
7bf1f802
ILT
1156 // Add an entry for a global symbol to the GOT, and add a dynamic
1157 // relocation of type R_TYPE for the GOT entry.
1158 void
1159 add_local_with_rel(Sized_relobj<size, big_endian>* object,
1160 unsigned int sym_index, Rel_dyn* rel_dyn,
1161 unsigned int r_type);
1162
1163 void
1164 add_local_with_rela(Sized_relobj<size, big_endian>* object,
1165 unsigned int sym_index, Rela_dyn* rela_dyn,
1166 unsigned int r_type);
1167
07f397ab
ILT
1168 // Add an entry (or pair of entries) for a global TLS symbol to the GOT.
1169 // Return true if this is a new GOT entry, false if the symbol was
1170 // already in the GOT.
1171 bool
1172 add_global_tls(Symbol* gsym, bool need_pair);
1173
7bf1f802
ILT
1174 // Add an entry for a global TLS symbol to the GOT, and add a dynamic
1175 // relocation of type R_TYPE.
1176 void
1177 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1178 unsigned int r_type);
1179
1180 void
1181 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1182 unsigned int r_type);
1183
1184 // Add a pair of entries for a global TLS symbol to the GOT, and add
1185 // dynamic relocations of type MOD_R_TYPE and DTV_R_TYPE, respectively.
1186 void
1187 add_global_tls_with_rel(Symbol* gsym, Rel_dyn* rel_dyn,
1188 unsigned int mod_r_type,
1189 unsigned int dtv_r_type);
1190
1191 void
1192 add_global_tls_with_rela(Symbol* gsym, Rela_dyn* rela_dyn,
1193 unsigned int mod_r_type,
1194 unsigned int dtv_r_type);
1195
07f397ab
ILT
1196 // Add an entry (or pair of entries) for a local TLS symbol to the GOT.
1197 // This returns true if this is a new GOT entry, false if the symbol
1198 // already has a GOT entry.
1199 bool
1200 add_local_tls(Sized_relobj<size, big_endian>* object,
1201 unsigned int sym_index, bool need_pair);
1202
7bf1f802
ILT
1203 // Add an entry (or pair of entries) for a local TLS symbol to the GOT,
1204 // and add a dynamic relocation of type R_TYPE for the first GOT entry.
1205 // Because this is a local symbol, the first GOT entry can be relocated
1206 // relative to a section symbol, and the second GOT entry will have an
1207 // dtv-relative value that can be computed at link time.
1208 void
1209 add_local_tls_with_rel(Sized_relobj<size, big_endian>* object,
1210 unsigned int sym_index, unsigned int shndx,
1211 bool need_pair, Rel_dyn* rel_dyn,
1212 unsigned int r_type);
1213
1214 void
1215 add_local_tls_with_rela(Sized_relobj<size, big_endian>* object,
1216 unsigned int sym_index, unsigned int shndx,
1217 bool need_pair, Rela_dyn* rela_dyn,
1218 unsigned int r_type);
1219
ead1e424
ILT
1220 // Add a constant to the GOT. This returns the offset of the new
1221 // entry from the start of the GOT.
1222 unsigned int
1223 add_constant(Valtype constant)
1224 {
1225 this->entries_.push_back(Got_entry(constant));
1226 this->set_got_size();
1227 return this->last_got_offset();
1228 }
1229
27bc2bce 1230 protected:
ead1e424
ILT
1231 // Write out the GOT table.
1232 void
1233 do_write(Output_file*);
1234
1235 private:
1236 // This POD class holds a single GOT entry.
1237 class Got_entry
1238 {
1239 public:
1240 // Create a zero entry.
1241 Got_entry()
1242 : local_sym_index_(CONSTANT_CODE)
1243 { this->u_.constant = 0; }
1244
1245 // Create a global symbol entry.
a3ad94ed 1246 explicit Got_entry(Symbol* gsym)
ead1e424
ILT
1247 : local_sym_index_(GSYM_CODE)
1248 { this->u_.gsym = gsym; }
1249
1250 // Create a local symbol entry.
e727fa71
ILT
1251 Got_entry(Sized_relobj<size, big_endian>* object,
1252 unsigned int local_sym_index)
ead1e424
ILT
1253 : local_sym_index_(local_sym_index)
1254 {
a3ad94ed
ILT
1255 gold_assert(local_sym_index != GSYM_CODE
1256 && local_sym_index != CONSTANT_CODE);
ead1e424
ILT
1257 this->u_.object = object;
1258 }
1259
1260 // Create a constant entry. The constant is a host value--it will
1261 // be swapped, if necessary, when it is written out.
a3ad94ed 1262 explicit Got_entry(Valtype constant)
ead1e424
ILT
1263 : local_sym_index_(CONSTANT_CODE)
1264 { this->u_.constant = constant; }
1265
1266 // Write the GOT entry to an output view.
1267 void
7e1edb90 1268 write(unsigned char* pov) const;
ead1e424
ILT
1269
1270 private:
1271 enum
1272 {
1273 GSYM_CODE = -1U,
1274 CONSTANT_CODE = -2U
1275 };
1276
1277 union
1278 {
1279 // For a local symbol, the object.
e727fa71 1280 Sized_relobj<size, big_endian>* object;
ead1e424
ILT
1281 // For a global symbol, the symbol.
1282 Symbol* gsym;
1283 // For a constant, the constant.
1284 Valtype constant;
1285 } u_;
c06b7b0b
ILT
1286 // For a local symbol, the local symbol index. This is GSYM_CODE
1287 // for a global symbol, or CONSTANT_CODE for a constant.
ead1e424
ILT
1288 unsigned int local_sym_index_;
1289 };
1290
1291 typedef std::vector<Got_entry> Got_entries;
1292
1293 // Return the offset into the GOT of GOT entry I.
1294 unsigned int
1295 got_offset(unsigned int i) const
1296 { return i * (size / 8); }
1297
1298 // Return the offset into the GOT of the last entry added.
1299 unsigned int
1300 last_got_offset() const
1301 { return this->got_offset(this->entries_.size() - 1); }
1302
1303 // Set the size of the section.
1304 void
1305 set_got_size()
27bc2bce 1306 { this->set_current_data_size(this->got_offset(this->entries_.size())); }
ead1e424
ILT
1307
1308 // The list of GOT entries.
1309 Got_entries entries_;
1310};
1311
a3ad94ed
ILT
1312// Output_data_dynamic is used to hold the data in SHT_DYNAMIC
1313// section.
1314
1315class Output_data_dynamic : public Output_section_data
1316{
1317 public:
9025d29d 1318 Output_data_dynamic(Stringpool* pool)
730cdc88 1319 : Output_section_data(Output_data::default_alignment()),
9025d29d 1320 entries_(), pool_(pool)
a3ad94ed
ILT
1321 { }
1322
1323 // Add a new dynamic entry with a fixed numeric value.
1324 void
1325 add_constant(elfcpp::DT tag, unsigned int val)
1326 { this->add_entry(Dynamic_entry(tag, val)); }
1327
16649710 1328 // Add a new dynamic entry with the address of output data.
a3ad94ed 1329 void
16649710
ILT
1330 add_section_address(elfcpp::DT tag, const Output_data* od)
1331 { this->add_entry(Dynamic_entry(tag, od, false)); }
a3ad94ed 1332
16649710 1333 // Add a new dynamic entry with the size of output data.
a3ad94ed 1334 void
16649710
ILT
1335 add_section_size(elfcpp::DT tag, const Output_data* od)
1336 { this->add_entry(Dynamic_entry(tag, od, true)); }
a3ad94ed
ILT
1337
1338 // Add a new dynamic entry with the address of a symbol.
1339 void
16649710 1340 add_symbol(elfcpp::DT tag, const Symbol* sym)
a3ad94ed
ILT
1341 { this->add_entry(Dynamic_entry(tag, sym)); }
1342
1343 // Add a new dynamic entry with a string.
1344 void
1345 add_string(elfcpp::DT tag, const char* str)
cfd73a4e 1346 { this->add_entry(Dynamic_entry(tag, this->pool_->add(str, true, NULL))); }
a3ad94ed 1347
41f542e7
ILT
1348 void
1349 add_string(elfcpp::DT tag, const std::string& str)
1350 { this->add_string(tag, str.c_str()); }
1351
27bc2bce
ILT
1352 protected:
1353 // Adjust the output section to set the entry size.
1354 void
1355 do_adjust_output_section(Output_section*);
1356
a3ad94ed
ILT
1357 // Set the final data size.
1358 void
27bc2bce 1359 set_final_data_size();
a3ad94ed
ILT
1360
1361 // Write out the dynamic entries.
1362 void
1363 do_write(Output_file*);
1364
1365 private:
1366 // This POD class holds a single dynamic entry.
1367 class Dynamic_entry
1368 {
1369 public:
1370 // Create an entry with a fixed numeric value.
1371 Dynamic_entry(elfcpp::DT tag, unsigned int val)
1372 : tag_(tag), classification_(DYNAMIC_NUMBER)
1373 { this->u_.val = val; }
1374
1375 // Create an entry with the size or address of a section.
16649710 1376 Dynamic_entry(elfcpp::DT tag, const Output_data* od, bool section_size)
a3ad94ed
ILT
1377 : tag_(tag),
1378 classification_(section_size
1379 ? DYNAMIC_SECTION_SIZE
1380 : DYNAMIC_SECTION_ADDRESS)
16649710 1381 { this->u_.od = od; }
a3ad94ed
ILT
1382
1383 // Create an entry with the address of a symbol.
16649710 1384 Dynamic_entry(elfcpp::DT tag, const Symbol* sym)
a3ad94ed
ILT
1385 : tag_(tag), classification_(DYNAMIC_SYMBOL)
1386 { this->u_.sym = sym; }
1387
1388 // Create an entry with a string.
1389 Dynamic_entry(elfcpp::DT tag, const char* str)
1390 : tag_(tag), classification_(DYNAMIC_STRING)
1391 { this->u_.str = str; }
1392
1393 // Write the dynamic entry to an output view.
1394 template<int size, bool big_endian>
1395 void
1ddbd1e6 1396 write(unsigned char* pov, const Stringpool* ACCEPT_SIZE_ENDIAN) const;
a3ad94ed
ILT
1397
1398 private:
1399 enum Classification
1400 {
1401 // Number.
1402 DYNAMIC_NUMBER,
1403 // Section address.
1404 DYNAMIC_SECTION_ADDRESS,
1405 // Section size.
1406 DYNAMIC_SECTION_SIZE,
1407 // Symbol adress.
1408 DYNAMIC_SYMBOL,
1409 // String.
1410 DYNAMIC_STRING
1411 };
1412
1413 union
1414 {
1415 // For DYNAMIC_NUMBER.
1416 unsigned int val;
1417 // For DYNAMIC_SECTION_ADDRESS and DYNAMIC_SECTION_SIZE.
16649710 1418 const Output_data* od;
a3ad94ed 1419 // For DYNAMIC_SYMBOL.
16649710 1420 const Symbol* sym;
a3ad94ed
ILT
1421 // For DYNAMIC_STRING.
1422 const char* str;
1423 } u_;
1424 // The dynamic tag.
1425 elfcpp::DT tag_;
1426 // The type of entry.
1427 Classification classification_;
1428 };
1429
1430 // Add an entry to the list.
1431 void
1432 add_entry(const Dynamic_entry& entry)
1433 { this->entries_.push_back(entry); }
1434
1435 // Sized version of write function.
1436 template<int size, bool big_endian>
1437 void
1438 sized_write(Output_file* of);
1439
1440 // The type of the list of entries.
1441 typedef std::vector<Dynamic_entry> Dynamic_entries;
1442
a3ad94ed
ILT
1443 // The entries.
1444 Dynamic_entries entries_;
1445 // The pool used for strings.
1446 Stringpool* pool_;
1447};
1448
a2fb1b05
ILT
1449// An output section. We don't expect to have too many output
1450// sections, so we don't bother to do a template on the size.
1451
54dc6425 1452class Output_section : public Output_data
a2fb1b05
ILT
1453{
1454 public:
1455 // Create an output section, giving the name, type, and flags.
96803768 1456 Output_section(const char* name, elfcpp::Elf_Word, elfcpp::Elf_Xword);
54dc6425 1457 virtual ~Output_section();
a2fb1b05 1458
ead1e424 1459 // Add a new input section SHNDX, named NAME, with header SHDR, from
730cdc88
ILT
1460 // object OBJECT. RELOC_SHNDX is the index of a relocation section
1461 // which applies to this section, or 0 if none, or -1U if more than
1462 // one. Return the offset within the output section.
a2fb1b05
ILT
1463 template<int size, bool big_endian>
1464 off_t
730cdc88
ILT
1465 add_input_section(Sized_relobj<size, big_endian>* object, unsigned int shndx,
1466 const char *name,
1467 const elfcpp::Shdr<size, big_endian>& shdr,
1468 unsigned int reloc_shndx);
a2fb1b05 1469
b8e6aad9 1470 // Add generated data POSD to this output section.
c06b7b0b 1471 void
ead1e424
ILT
1472 add_output_section_data(Output_section_data* posd);
1473
a2fb1b05
ILT
1474 // Return the section name.
1475 const char*
1476 name() const
1477 { return this->name_; }
1478
1479 // Return the section type.
1480 elfcpp::Elf_Word
1481 type() const
1482 { return this->type_; }
1483
1484 // Return the section flags.
1485 elfcpp::Elf_Xword
1486 flags() const
1487 { return this->flags_; }
1488
a3ad94ed
ILT
1489 // Return the entsize field.
1490 uint64_t
1491 entsize() const
1492 { return this->entsize_; }
1493
61ba1cf9
ILT
1494 // Set the entsize field.
1495 void
16649710 1496 set_entsize(uint64_t v);
61ba1cf9 1497
16649710
ILT
1498 // Set the link field to the output section index of a section.
1499 void
14b31740 1500 set_link_section(const Output_data* od)
16649710
ILT
1501 {
1502 gold_assert(this->link_ == 0
1503 && !this->should_link_to_symtab_
1504 && !this->should_link_to_dynsym_);
1505 this->link_section_ = od;
1506 }
1507
1508 // Set the link field to a constant.
61ba1cf9
ILT
1509 void
1510 set_link(unsigned int v)
16649710
ILT
1511 {
1512 gold_assert(this->link_section_ == NULL
1513 && !this->should_link_to_symtab_
1514 && !this->should_link_to_dynsym_);
1515 this->link_ = v;
1516 }
61ba1cf9 1517
16649710
ILT
1518 // Record that this section should link to the normal symbol table.
1519 void
1520 set_should_link_to_symtab()
1521 {
1522 gold_assert(this->link_section_ == NULL
1523 && this->link_ == 0
1524 && !this->should_link_to_dynsym_);
1525 this->should_link_to_symtab_ = true;
1526 }
1527
1528 // Record that this section should link to the dynamic symbol table.
1529 void
1530 set_should_link_to_dynsym()
1531 {
1532 gold_assert(this->link_section_ == NULL
1533 && this->link_ == 0
1534 && !this->should_link_to_symtab_);
1535 this->should_link_to_dynsym_ = true;
1536 }
1537
1538 // Return the info field.
1539 unsigned int
1540 info() const
1541 {
1542 gold_assert(this->info_section_ == NULL);
1543 return this->info_;
1544 }
1545
1546 // Set the info field to the output section index of a section.
1547 void
14b31740 1548 set_info_section(const Output_data* od)
16649710
ILT
1549 {
1550 gold_assert(this->info_ == 0);
1551 this->info_section_ = od;
1552 }
1553
1554 // Set the info field to a constant.
61ba1cf9
ILT
1555 void
1556 set_info(unsigned int v)
16649710
ILT
1557 {
1558 gold_assert(this->info_section_ == NULL);
1559 this->info_ = v;
1560 }
61ba1cf9
ILT
1561
1562 // Set the addralign field.
1563 void
1564 set_addralign(uint64_t v)
1565 { this->addralign_ = v; }
1566
c06b7b0b
ILT
1567 // Indicate that we need a symtab index.
1568 void
1569 set_needs_symtab_index()
1570 { this->needs_symtab_index_ = true; }
1571
1572 // Return whether we need a symtab index.
1573 bool
1574 needs_symtab_index() const
1575 { return this->needs_symtab_index_; }
1576
1577 // Get the symtab index.
1578 unsigned int
1579 symtab_index() const
1580 {
a3ad94ed 1581 gold_assert(this->symtab_index_ != 0);
c06b7b0b
ILT
1582 return this->symtab_index_;
1583 }
1584
1585 // Set the symtab index.
1586 void
1587 set_symtab_index(unsigned int index)
1588 {
a3ad94ed 1589 gold_assert(index != 0);
c06b7b0b
ILT
1590 this->symtab_index_ = index;
1591 }
1592
1593 // Indicate that we need a dynsym index.
1594 void
1595 set_needs_dynsym_index()
1596 { this->needs_dynsym_index_ = true; }
1597
1598 // Return whether we need a dynsym index.
1599 bool
1600 needs_dynsym_index() const
1601 { return this->needs_dynsym_index_; }
1602
1603 // Get the dynsym index.
1604 unsigned int
1605 dynsym_index() const
1606 {
a3ad94ed 1607 gold_assert(this->dynsym_index_ != 0);
c06b7b0b
ILT
1608 return this->dynsym_index_;
1609 }
1610
1611 // Set the dynsym index.
1612 void
1613 set_dynsym_index(unsigned int index)
1614 {
a3ad94ed 1615 gold_assert(index != 0);
c06b7b0b
ILT
1616 this->dynsym_index_ = index;
1617 }
1618
730cdc88
ILT
1619 // Return whether this section should be written after all the input
1620 // sections are complete.
1621 bool
1622 after_input_sections() const
1623 { return this->after_input_sections_; }
1624
1625 // Record that this section should be written after all the input
1626 // sections are complete.
1627 void
1628 set_after_input_sections()
1629 { this->after_input_sections_ = true; }
1630
27bc2bce
ILT
1631 // Return whether this section requires postprocessing after all
1632 // relocations have been applied.
1633 bool
1634 requires_postprocessing() const
1635 { return this->requires_postprocessing_; }
1636
96803768
ILT
1637 // If a section requires postprocessing, return the buffer to use.
1638 unsigned char*
1639 postprocessing_buffer() const
1640 {
1641 gold_assert(this->postprocessing_buffer_ != NULL);
1642 return this->postprocessing_buffer_;
1643 }
1644
1645 // If a section requires postprocessing, create the buffer to use.
27bc2bce 1646 void
96803768
ILT
1647 create_postprocessing_buffer();
1648
1649 // If a section requires postprocessing, this is the size of the
1650 // buffer to which relocations should be applied.
1651 off_t
1652 postprocessing_buffer_size() const
1653 { return this->current_data_size_for_child(); }
27bc2bce 1654
730cdc88
ILT
1655 // Return whether the offset OFFSET in the input section SHNDX in
1656 // object OBJECT is being included in the link.
1657 bool
1658 is_input_address_mapped(const Relobj* object, unsigned int shndx,
1659 off_t offset) const;
1660
1661 // Return the offset within the output section of OFFSET relative to
1662 // the start of input section SHNDX in object OBJECT.
1663 off_t
1664 output_offset(const Relobj* object, unsigned int shndx, off_t offset) const;
1665
b8e6aad9
ILT
1666 // Return the output virtual address of OFFSET relative to the start
1667 // of input section SHNDX in object OBJECT.
1668 uint64_t
1669 output_address(const Relobj* object, unsigned int shndx,
1670 off_t offset) const;
1671
27bc2bce
ILT
1672 // Write the section header into *OPHDR.
1673 template<int size, bool big_endian>
1674 void
1675 write_header(const Layout*, const Stringpool*,
1676 elfcpp::Shdr_write<size, big_endian>*) const;
1677
1678 protected:
1679 // Return the section index in the output file.
1680 unsigned int
1681 do_out_shndx() const
1682 {
1683 gold_assert(this->out_shndx_ != -1U);
1684 return this->out_shndx_;
1685 }
1686
1687 // Set the output section index.
1688 void
1689 do_set_out_shndx(unsigned int shndx)
1690 {
1691 gold_assert(this->out_shndx_ == -1U);
1692 this->out_shndx_ = shndx;
1693 }
1694
1695 // Set the final data size of the Output_section. For a typical
ead1e424 1696 // Output_section, there is nothing to do, but if there are any
27bc2bce 1697 // Output_section_data objects we need to set their final addresses
ead1e424 1698 // here.
96803768 1699 virtual void
27bc2bce 1700 set_final_data_size();
ead1e424 1701
54dc6425 1702 // Write the data to the file. For a typical Output_section, this
ead1e424
ILT
1703 // does nothing: the data is written out by calling Object::Relocate
1704 // on each input object. But if there are any Output_section_data
1705 // objects we do need to write them out here.
96803768 1706 virtual void
ead1e424 1707 do_write(Output_file*);
54dc6425 1708
75f65a3e
ILT
1709 // Return the address alignment--function required by parent class.
1710 uint64_t
1711 do_addralign() const
1712 { return this->addralign_; }
1713
1714 // Return whether this is an Output_section.
1715 bool
1716 do_is_section() const
1717 { return true; }
1718
54dc6425
ILT
1719 // Return whether this is a section of the specified type.
1720 bool
75f65a3e 1721 do_is_section_type(elfcpp::Elf_Word type) const
54dc6425
ILT
1722 { return this->type_ == type; }
1723
1724 // Return whether the specified section flag is set.
1725 bool
75f65a3e 1726 do_is_section_flag_set(elfcpp::Elf_Xword flag) const
54dc6425
ILT
1727 { return (this->flags_ & flag) != 0; }
1728
7bf1f802
ILT
1729 // Set the TLS offset. Called only for SHT_TLS sections.
1730 void
1731 do_set_tls_offset(uint64_t tls_base);
1732
1733 // Return the TLS offset, relative to the base of the TLS segment.
1734 // Valid only for SHT_TLS sections.
1735 uint64_t
1736 do_tls_offset() const
1737 { return this->tls_offset_; }
1738
96803768
ILT
1739 // Modify the section name. This is only permitted for an
1740 // unallocated section, and only before the size has been finalized.
1741 // Otherwise the name will not get into Layout::namepool_.
1742 void
1743 set_name(const char* newname)
1744 {
1745 gold_assert((this->flags_ & elfcpp::SHF_ALLOC) == 0);
1746 gold_assert(!this->is_data_size_valid());
1747 this->name_ = newname;
1748 }
1749
1750 // This may be implemented by a child class.
1751 virtual void
1752 do_finalize_name(Layout*)
1753 { }
1754
1755 // Record that this section requires postprocessing after all
1756 // relocations have been applied. This is called by a child class.
1757 void
1758 set_requires_postprocessing()
1759 {
1760 this->requires_postprocessing_ = true;
1761 this->after_input_sections_ = true;
1762 }
1763
1764 // Write all the data of an Output_section into the postprocessing
1765 // buffer.
1766 void
1767 write_to_postprocessing_buffer();
1768
a2fb1b05 1769 private:
ead1e424
ILT
1770 // In some cases we need to keep a list of the input sections
1771 // associated with this output section. We only need the list if we
1772 // might have to change the offsets of the input section within the
1773 // output section after we add the input section. The ordinary
1774 // input sections will be written out when we process the object
1775 // file, and as such we don't need to track them here. We do need
1776 // to track Output_section_data objects here. We store instances of
1777 // this structure in a std::vector, so it must be a POD. There can
1778 // be many instances of this structure, so we use a union to save
1779 // some space.
1780 class Input_section
1781 {
1782 public:
1783 Input_section()
b8e6aad9
ILT
1784 : shndx_(0), p2align_(0)
1785 {
1786 this->u1_.data_size = 0;
1787 this->u2_.object = NULL;
1788 }
ead1e424 1789
b8e6aad9 1790 // For an ordinary input section.
f6ce93d6 1791 Input_section(Relobj* object, unsigned int shndx, off_t data_size,
ead1e424
ILT
1792 uint64_t addralign)
1793 : shndx_(shndx),
b8e6aad9 1794 p2align_(ffsll(static_cast<long long>(addralign)))
ead1e424 1795 {
b8e6aad9
ILT
1796 gold_assert(shndx != OUTPUT_SECTION_CODE
1797 && shndx != MERGE_DATA_SECTION_CODE
1798 && shndx != MERGE_STRING_SECTION_CODE);
1799 this->u1_.data_size = data_size;
1800 this->u2_.object = object;
ead1e424
ILT
1801 }
1802
b8e6aad9 1803 // For a non-merge output section.
ead1e424 1804 Input_section(Output_section_data* posd)
b8e6aad9
ILT
1805 : shndx_(OUTPUT_SECTION_CODE),
1806 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1807 {
1808 this->u1_.data_size = 0;
1809 this->u2_.posd = posd;
1810 }
1811
1812 // For a merge section.
1813 Input_section(Output_section_data* posd, bool is_string, uint64_t entsize)
1814 : shndx_(is_string
1815 ? MERGE_STRING_SECTION_CODE
1816 : MERGE_DATA_SECTION_CODE),
1817 p2align_(ffsll(static_cast<long long>(posd->addralign())))
1818 {
1819 this->u1_.entsize = entsize;
1820 this->u2_.posd = posd;
1821 }
ead1e424
ILT
1822
1823 // The required alignment.
1824 uint64_t
1825 addralign() const
a3ad94ed
ILT
1826 {
1827 return (this->p2align_ == 0
1828 ? 0
1829 : static_cast<uint64_t>(1) << (this->p2align_ - 1));
1830 }
ead1e424
ILT
1831
1832 // Return the required size.
1833 off_t
1834 data_size() const;
1835
b8e6aad9
ILT
1836 // Return whether this is a merge section which matches the
1837 // parameters.
1838 bool
87f95776
ILT
1839 is_merge_section(bool is_string, uint64_t entsize,
1840 uint64_t addralign) const
b8e6aad9
ILT
1841 {
1842 return (this->shndx_ == (is_string
1843 ? MERGE_STRING_SECTION_CODE
1844 : MERGE_DATA_SECTION_CODE)
87f95776
ILT
1845 && this->u1_.entsize == entsize
1846 && this->addralign() == addralign);
b8e6aad9
ILT
1847 }
1848
1849 // Set the output section.
1850 void
1851 set_output_section(Output_section* os)
1852 {
1853 gold_assert(!this->is_input_section());
1854 this->u2_.posd->set_output_section(os);
1855 }
1856
ead1e424 1857 // Set the address and file offset. This is called during
96803768
ILT
1858 // Layout::finalize. SECTION_FILE_OFFSET is the file offset of
1859 // the enclosing section.
ead1e424 1860 void
96803768
ILT
1861 set_address_and_file_offset(uint64_t address, off_t file_offset,
1862 off_t section_file_offset);
ead1e424 1863
96803768
ILT
1864 // Finalize the data size.
1865 void
1866 finalize_data_size();
9a0910c3 1867
b8e6aad9
ILT
1868 // Add an input section, for SHF_MERGE sections.
1869 bool
1870 add_input_section(Relobj* object, unsigned int shndx)
1871 {
1872 gold_assert(this->shndx_ == MERGE_DATA_SECTION_CODE
1873 || this->shndx_ == MERGE_STRING_SECTION_CODE);
1874 return this->u2_.posd->add_input_section(object, shndx);
1875 }
1876
1877 // Given an input OBJECT, an input section index SHNDX within that
1878 // object, and an OFFSET relative to the start of that input
730cdc88
ILT
1879 // section, return whether or not the output offset is known. If
1880 // this function returns true, it sets *POUTPUT to the output
1881 // offset.
b8e6aad9 1882 bool
730cdc88
ILT
1883 output_offset(const Relobj* object, unsigned int shndx, off_t offset,
1884 off_t *poutput) const;
b8e6aad9 1885
ead1e424
ILT
1886 // Write out the data. This does nothing for an input section.
1887 void
1888 write(Output_file*);
1889
96803768
ILT
1890 // Write the data to a buffer. This does nothing for an input
1891 // section.
1892 void
1893 write_to_buffer(unsigned char*);
1894
ead1e424 1895 private:
b8e6aad9
ILT
1896 // Code values which appear in shndx_. If the value is not one of
1897 // these codes, it is the input section index in the object file.
1898 enum
1899 {
1900 // An Output_section_data.
1901 OUTPUT_SECTION_CODE = -1U,
1902 // An Output_section_data for an SHF_MERGE section with
1903 // SHF_STRINGS not set.
1904 MERGE_DATA_SECTION_CODE = -2U,
1905 // An Output_section_data for an SHF_MERGE section with
1906 // SHF_STRINGS set.
1907 MERGE_STRING_SECTION_CODE = -3U
1908 };
1909
ead1e424
ILT
1910 // Whether this is an input section.
1911 bool
1912 is_input_section() const
b8e6aad9
ILT
1913 {
1914 return (this->shndx_ != OUTPUT_SECTION_CODE
1915 && this->shndx_ != MERGE_DATA_SECTION_CODE
1916 && this->shndx_ != MERGE_STRING_SECTION_CODE);
1917 }
ead1e424 1918
b8e6aad9
ILT
1919 // For an ordinary input section, this is the section index in the
1920 // input file. For an Output_section_data, this is
1921 // OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1922 // MERGE_STRING_SECTION_CODE.
ead1e424
ILT
1923 unsigned int shndx_;
1924 // The required alignment, stored as a power of 2.
1925 unsigned int p2align_;
ead1e424
ILT
1926 union
1927 {
b8e6aad9
ILT
1928 // For an ordinary input section, the section size.
1929 off_t data_size;
1930 // For OUTPUT_SECTION_CODE, this is not used. For
1931 // MERGE_DATA_SECTION_CODE or MERGE_STRING_SECTION_CODE, the
1932 // entity size.
1933 uint64_t entsize;
1934 } u1_;
1935 union
1936 {
1937 // For an ordinary input section, the object which holds the
ead1e424 1938 // input section.
f6ce93d6 1939 Relobj* object;
b8e6aad9
ILT
1940 // For OUTPUT_SECTION_CODE or MERGE_DATA_SECTION_CODE or
1941 // MERGE_STRING_SECTION_CODE, the data.
ead1e424 1942 Output_section_data* posd;
b8e6aad9 1943 } u2_;
ead1e424
ILT
1944 };
1945
1946 typedef std::vector<Input_section> Input_section_list;
1947
c51e6221
ILT
1948 // Fill data. This is used to fill in data between input sections.
1949 // When we have to keep track of the input sections, we can use an
1950 // Output_data_const, but we don't want to have to keep track of
1951 // input sections just to implement fills. For a fill we record the
1952 // offset, and the actual data to be written out.
1953 class Fill
1954 {
1955 public:
1956 Fill(off_t section_offset, off_t length)
1957 : section_offset_(section_offset), length_(length)
1958 { }
1959
1960 // Return section offset.
1961 off_t
1962 section_offset() const
1963 { return this->section_offset_; }
1964
1965 // Return fill length.
1966 off_t
1967 length() const
1968 { return this->length_; }
1969
1970 private:
1971 // The offset within the output section.
1972 off_t section_offset_;
1973 // The length of the space to fill.
1974 off_t length_;
1975 };
1976
1977 typedef std::vector<Fill> Fill_list;
1978
b8e6aad9
ILT
1979 // Add a new output section by Input_section.
1980 void
1981 add_output_section_data(Input_section*);
1982
1983 // Add an SHF_MERGE input section. Returns true if the section was
1984 // handled.
1985 bool
1986 add_merge_input_section(Relobj* object, unsigned int shndx, uint64_t flags,
96803768 1987 uint64_t entsize, uint64_t addralign);
b8e6aad9
ILT
1988
1989 // Add an output SHF_MERGE section POSD to this output section.
1990 // IS_STRING indicates whether it is a SHF_STRINGS section, and
1991 // ENTSIZE is the entity size. This returns the entry added to
1992 // input_sections_.
1993 void
1994 add_output_merge_section(Output_section_data* posd, bool is_string,
1995 uint64_t entsize);
1996
a2fb1b05
ILT
1997 // Most of these fields are only valid after layout.
1998
1999 // The name of the section. This will point into a Stringpool.
9a0910c3 2000 const char* name_;
75f65a3e 2001 // The section address is in the parent class.
a2fb1b05
ILT
2002 // The section alignment.
2003 uint64_t addralign_;
2004 // The section entry size.
2005 uint64_t entsize_;
75f65a3e 2006 // The file offset is in the parent class.
16649710 2007 // Set the section link field to the index of this section.
14b31740 2008 const Output_data* link_section_;
16649710 2009 // If link_section_ is NULL, this is the link field.
a2fb1b05 2010 unsigned int link_;
16649710 2011 // Set the section info field to the index of this section.
14b31740 2012 const Output_data* info_section_;
16649710 2013 // If info_section_ is NULL, this is the section info field.
a2fb1b05
ILT
2014 unsigned int info_;
2015 // The section type.
27bc2bce 2016 const elfcpp::Elf_Word type_;
a2fb1b05 2017 // The section flags.
27bc2bce 2018 const elfcpp::Elf_Xword flags_;
61ba1cf9 2019 // The section index.
ead1e424 2020 unsigned int out_shndx_;
c06b7b0b
ILT
2021 // If there is a STT_SECTION for this output section in the normal
2022 // symbol table, this is the symbol index. This starts out as zero.
2023 // It is initialized in Layout::finalize() to be the index, or -1U
2024 // if there isn't one.
2025 unsigned int symtab_index_;
2026 // If there is a STT_SECTION for this output section in the dynamic
2027 // symbol table, this is the symbol index. This starts out as zero.
2028 // It is initialized in Layout::finalize() to be the index, or -1U
2029 // if there isn't one.
2030 unsigned int dynsym_index_;
ead1e424
ILT
2031 // The input sections. This will be empty in cases where we don't
2032 // need to keep track of them.
2033 Input_section_list input_sections_;
2034 // The offset of the first entry in input_sections_.
2035 off_t first_input_offset_;
c51e6221
ILT
2036 // The fill data. This is separate from input_sections_ because we
2037 // often will need fill sections without needing to keep track of
2038 // input sections.
2039 Fill_list fills_;
96803768
ILT
2040 // If the section requires postprocessing, this buffer holds the
2041 // section contents during relocation.
2042 unsigned char* postprocessing_buffer_;
c06b7b0b
ILT
2043 // Whether this output section needs a STT_SECTION symbol in the
2044 // normal symbol table. This will be true if there is a relocation
2045 // which needs it.
2046 bool needs_symtab_index_ : 1;
2047 // Whether this output section needs a STT_SECTION symbol in the
2048 // dynamic symbol table. This will be true if there is a dynamic
2049 // relocation which needs it.
2050 bool needs_dynsym_index_ : 1;
16649710
ILT
2051 // Whether the link field of this output section should point to the
2052 // normal symbol table.
2053 bool should_link_to_symtab_ : 1;
2054 // Whether the link field of this output section should point to the
2055 // dynamic symbol table.
2056 bool should_link_to_dynsym_ : 1;
730cdc88
ILT
2057 // Whether this section should be written after all the input
2058 // sections are complete.
2059 bool after_input_sections_ : 1;
27bc2bce
ILT
2060 // Whether this section requires post processing after all
2061 // relocations have been applied.
2062 bool requires_postprocessing_ : 1;
7bf1f802
ILT
2063 // For SHT_TLS sections, the offset of this section relative to the base
2064 // of the TLS segment.
2065 uint64_t tls_offset_;
a2fb1b05
ILT
2066};
2067
2068// An output segment. PT_LOAD segments are built from collections of
2069// output sections. Other segments typically point within PT_LOAD
2070// segments, and are built directly as needed.
2071
2072class Output_segment
2073{
2074 public:
2075 // Create an output segment, specifying the type and flags.
2076 Output_segment(elfcpp::Elf_Word, elfcpp::Elf_Word);
2077
2078 // Return the virtual address.
2079 uint64_t
2080 vaddr() const
2081 { return this->vaddr_; }
2082
2083 // Return the physical address.
2084 uint64_t
2085 paddr() const
2086 { return this->paddr_; }
2087
2088 // Return the segment type.
2089 elfcpp::Elf_Word
2090 type() const
2091 { return this->type_; }
2092
2093 // Return the segment flags.
2094 elfcpp::Elf_Word
2095 flags() const
2096 { return this->flags_; }
2097
92e059d8
ILT
2098 // Return the memory size.
2099 uint64_t
2100 memsz() const
2101 { return this->memsz_; }
2102
ead1e424
ILT
2103 // Return the file size.
2104 off_t
2105 filesz() const
2106 { return this->filesz_; }
2107
75f65a3e
ILT
2108 // Return the maximum alignment of the Output_data.
2109 uint64_t
ead1e424 2110 addralign();
75f65a3e 2111
a2fb1b05
ILT
2112 // Add an Output_section to this segment.
2113 void
dbe717ef
ILT
2114 add_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2115 { this->add_output_section(os, seg_flags, false); }
2116
2117 // Add an Output_section to the start of this segment.
2118 void
2119 add_initial_output_section(Output_section* os, elfcpp::Elf_Word seg_flags)
2120 { this->add_output_section(os, seg_flags, true); }
75f65a3e
ILT
2121
2122 // Add an Output_data (which is not an Output_section) to the start
2123 // of this segment.
2124 void
2125 add_initial_output_data(Output_data*);
2126
4f4c5f80
ILT
2127 // Return the number of dynamic relocations applied to this segment.
2128 unsigned int
2129 dynamic_reloc_count() const;
2130
75f65a3e
ILT
2131 // Set the address of the segment to ADDR and the offset to *POFF
2132 // (aligned if necessary), and set the addresses and offsets of all
ead1e424
ILT
2133 // contained output sections accordingly. Set the section indexes
2134 // of all contained output sections starting with *PSHNDX. Return
2135 // the address of the immediately following segment. Update *POFF
2136 // and *PSHNDX. This should only be called for a PT_LOAD segment.
75f65a3e 2137 uint64_t
ead1e424 2138 set_section_addresses(uint64_t addr, off_t* poff, unsigned int* pshndx);
75f65a3e 2139
0496d5e5
ILT
2140 // Set the minimum alignment of this segment. This may be adjusted
2141 // upward based on the section alignments.
2142 void
2143 set_minimum_addralign(uint64_t align)
2144 {
2145 gold_assert(!this->is_align_known_);
2146 this->align_ = align;
2147 }
2148
75f65a3e
ILT
2149 // Set the offset of this segment based on the section. This should
2150 // only be called for a non-PT_LOAD segment.
2151 void
2152 set_offset();
2153
7bf1f802
ILT
2154 // Set the TLS offsets of the sections contained in the PT_TLS segment.
2155 void
2156 set_tls_offsets();
2157
75f65a3e
ILT
2158 // Return the number of output sections.
2159 unsigned int
2160 output_section_count() const;
a2fb1b05 2161
61ba1cf9
ILT
2162 // Write the segment header into *OPHDR.
2163 template<int size, bool big_endian>
2164 void
ead1e424 2165 write_header(elfcpp::Phdr_write<size, big_endian>*);
61ba1cf9
ILT
2166
2167 // Write the section headers of associated sections into V.
2168 template<int size, bool big_endian>
2169 unsigned char*
16649710 2170 write_section_headers(const Layout*, const Stringpool*, unsigned char* v,
ead1e424 2171 unsigned int* pshndx ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 2172
a2fb1b05
ILT
2173 private:
2174 Output_segment(const Output_segment&);
2175 Output_segment& operator=(const Output_segment&);
2176
54dc6425 2177 typedef std::list<Output_data*> Output_data_list;
a2fb1b05 2178
dbe717ef
ILT
2179 // Add an Output_section to this segment, specifying front or back.
2180 void
2181 add_output_section(Output_section*, elfcpp::Elf_Word seg_flags,
2182 bool front);
2183
ead1e424
ILT
2184 // Find the maximum alignment in an Output_data_list.
2185 static uint64_t
2186 maximum_alignment(const Output_data_list*);
2187
75f65a3e
ILT
2188 // Set the section addresses in an Output_data_list.
2189 uint64_t
ead1e424
ILT
2190 set_section_list_addresses(Output_data_list*, uint64_t addr, off_t* poff,
2191 unsigned int* pshndx);
75f65a3e
ILT
2192
2193 // Return the number of Output_sections in an Output_data_list.
2194 unsigned int
2195 output_section_count_list(const Output_data_list*) const;
2196
4f4c5f80
ILT
2197 // Return the number of dynamic relocs in an Output_data_list.
2198 unsigned int
2199 dynamic_reloc_count_list(const Output_data_list*) const;
2200
61ba1cf9
ILT
2201 // Write the section headers in the list into V.
2202 template<int size, bool big_endian>
2203 unsigned char*
16649710
ILT
2204 write_section_headers_list(const Layout*, const Stringpool*,
2205 const Output_data_list*, unsigned char* v,
ead1e424 2206 unsigned int* pshdx ACCEPT_SIZE_ENDIAN) const;
61ba1cf9 2207
75f65a3e 2208 // The list of output data with contents attached to this segment.
54dc6425 2209 Output_data_list output_data_;
75f65a3e
ILT
2210 // The list of output data without contents attached to this segment.
2211 Output_data_list output_bss_;
a2fb1b05
ILT
2212 // The segment virtual address.
2213 uint64_t vaddr_;
2214 // The segment physical address.
2215 uint64_t paddr_;
2216 // The size of the segment in memory.
2217 uint64_t memsz_;
0496d5e5
ILT
2218 // The segment alignment. The is_align_known_ field indicates
2219 // whether this has been finalized. It can be set to a minimum
2220 // value before it is finalized.
a2fb1b05
ILT
2221 uint64_t align_;
2222 // The offset of the segment data within the file.
2223 off_t offset_;
2224 // The size of the segment data in the file.
2225 off_t filesz_;
2226 // The segment type;
2227 elfcpp::Elf_Word type_;
2228 // The segment flags.
2229 elfcpp::Elf_Word flags_;
0496d5e5 2230 // Whether we have finalized align_.
ead1e424 2231 bool is_align_known_;
a2fb1b05
ILT
2232};
2233
61ba1cf9 2234// This class represents the output file.
a2fb1b05
ILT
2235
2236class Output_file
2237{
2238 public:
c51e6221
ILT
2239 Output_file(const General_options& options, Target*);
2240
2241 // Get a pointer to the target.
2242 Target*
2243 target() const
2244 { return this->target_; }
61ba1cf9
ILT
2245
2246 // Open the output file. FILE_SIZE is the final size of the file.
2247 void
2248 open(off_t file_size);
2249
27bc2bce
ILT
2250 // Resize the output file.
2251 void
2252 resize(off_t file_size);
2253
c420411f
ILT
2254 // Close the output file (flushing all buffered data) and make sure
2255 // there are no errors.
61ba1cf9
ILT
2256 void
2257 close();
2258
2259 // We currently always use mmap which makes the view handling quite
2260 // simple. In the future we may support other approaches.
a2fb1b05
ILT
2261
2262 // Write data to the output file.
2263 void
61ba1cf9
ILT
2264 write(off_t offset, const void* data, off_t len)
2265 { memcpy(this->base_ + offset, data, len); }
2266
2267 // Get a buffer to use to write to the file, given the offset into
2268 // the file and the size.
2269 unsigned char*
2270 get_output_view(off_t start, off_t size)
2271 {
a3ad94ed 2272 gold_assert(start >= 0 && size >= 0 && start + size <= this->file_size_);
61ba1cf9
ILT
2273 return this->base_ + start;
2274 }
2275
2276 // VIEW must have been returned by get_output_view. Write the
2277 // buffer to the file, passing in the offset and the size.
2278 void
2279 write_output_view(off_t, off_t, unsigned char*)
2280 { }
2281
730cdc88
ILT
2282 // Get a read/write buffer. This is used when we want to write part
2283 // of the file, read it in, and write it again.
2284 unsigned char*
2285 get_input_output_view(off_t start, off_t size)
2286 { return this->get_output_view(start, size); }
2287
2288 // Write a read/write buffer back to the file.
2289 void
2290 write_input_output_view(off_t, off_t, unsigned char*)
2291 { }
2292
2293 // Get a read buffer. This is used when we just want to read part
2294 // of the file back it in.
2295 const unsigned char*
2296 get_input_view(off_t start, off_t size)
2297 { return this->get_output_view(start, size); }
2298
2299 // Release a read bfufer.
2300 void
2301 free_input_view(off_t, off_t, const unsigned char*)
2302 { }
2303
61ba1cf9 2304 private:
c420411f 2305 // Map the file into memory and return a pointer to the map.
27bc2bce
ILT
2306 void
2307 map();
2308
c420411f
ILT
2309 // Unmap the file from memory (and flush to disk buffers).
2310 void
2311 unmap();
2312
2313
61ba1cf9
ILT
2314 // General options.
2315 const General_options& options_;
c51e6221
ILT
2316 // Target.
2317 Target* target_;
61ba1cf9
ILT
2318 // File name.
2319 const char* name_;
2320 // File descriptor.
2321 int o_;
2322 // File size.
2323 off_t file_size_;
2324 // Base of file mapped into memory.
2325 unsigned char* base_;
c420411f
ILT
2326 // True iff base_ points to a memory buffer rather than an output file.
2327 bool map_is_anonymous_;
a2fb1b05
ILT
2328};
2329
2330} // End namespace gold.
2331
2332#endif // !defined(GOLD_OUTPUT_H)
This page took 0.199827 seconds and 4 git commands to generate.