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