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