OSDN Git Service

3940f82f4027d16e9ba9e56520ad9fa262734dc6
[pf3gnuchains/sourceware.git] / gold / output.cc
1 // output.cc -- manage the output file for gold
2
3 #include "gold.h"
4
5 #include <cstdlib>
6 #include <algorithm>
7
8 #include "object.h"
9 #include "output.h"
10
11 namespace gold
12 {
13
14 // Output_data methods.
15
16 Output_data::~Output_data()
17 {
18 }
19
20 // Set the address and offset.
21
22 void
23 Output_data::set_address(uint64_t addr, off_t off)
24 {
25   this->address_ = addr;
26   this->offset_ = off;
27
28   // Let the child class know.
29   this->do_set_address(addr, off);
30 }
31
32 // Return the default alignment for a size--32 or 64.
33
34 uint64_t
35 Output_data::default_alignment(int size)
36 {
37   if (size == 32)
38     return 4;
39   else if (size == 64)
40     return 8;
41   else
42     abort();
43 }
44
45 // Output_data_const methods.
46
47 void
48 Output_data_const::do_write(Output_file* output)
49 {
50   output->write(this->offset(), data_.data(), data_.size());
51 }
52
53 // Output_section_header methods.  This currently assumes that the
54 // segment and section lists are complete at construction time.
55
56 Output_section_headers::Output_section_headers(
57     int size,
58     const Layout::Segment_list& segment_list,
59     const Layout::Section_list& section_list)
60   : size_(size),
61     segment_list_(segment_list),
62     section_list_(section_list)
63 {
64   // Count all the sections.
65   off_t count = 0;
66   for (Layout::Segment_list::const_iterator p = segment_list.begin();
67        p != segment_list.end();
68        ++p)
69     count += (*p)->output_section_count();
70   count += section_list.size();
71
72   int shdr_size;
73   if (size == 32)
74     shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
75   else if (size == 64)
76     shdr_size = elfcpp::Elf_sizes<64>::shdr_size;
77   else
78     abort();
79
80   this->set_data_size(count * shdr_size);
81 }
82
83 void
84 Output_section_headers::do_write(Output_file*)
85 {
86   // FIXME: Unimplemented.
87   abort();
88 }
89
90 // Output_segment_header methods.
91
92 void
93 Output_segment_headers::do_write(Output_file*)
94 {
95   // FIXME: Unimplemented.
96   abort();
97 }
98
99 // Output_file_header methods.
100
101 Output_file_header::Output_file_header(int size,
102                                        const General_options& options,
103                                        const Target* target,
104                                        const Symbol_table* symtab,
105                                        const Output_segment_headers* osh)
106   : size_(size),
107     options_(options),
108     target_(target),
109     symtab_(symtab),
110     program_header_(osh),
111     section_header_(NULL),
112     shstrtab_(NULL)
113 {
114 }
115
116 // Set the section table information for a file header.
117
118 void
119 Output_file_header::set_section_info(const Output_section_headers* shdrs,
120                                      const Output_section* shstrtab)
121 {
122   this->section_header_ = shdrs;
123   this->shstrtab_ = shstrtab;
124 }
125
126 // Write out the file header.
127
128 void
129 Output_file_header::do_write(Output_file*)
130 {
131   // FIXME: Unimplemented.
132   abort();
133 }
134
135 // Output_section methods.
136
137 // Construct an Output_section.  NAME will point into a Stringpool.
138
139 Output_section::Output_section(const char* name, elfcpp::Elf_Word type,
140                                elfcpp::Elf_Xword flags)
141   : name_(name),
142     addralign_(0),
143     entsize_(0),
144     link_(0),
145     info_(0),
146     type_(type),
147     flags_(flags)
148 {
149 }
150
151 Output_section::~Output_section()
152 {
153 }
154
155 // Add an input section to an Output_section.  We don't keep track of
156 // input sections for an Output_section.  Instead, each Object keeps
157 // track of the Output_section for each of its input sections.
158
159 template<int size, bool big_endian>
160 off_t
161 Output_section::add_input_section(Object* object, const char* secname,
162                                   const elfcpp::Shdr<size, big_endian>& shdr)
163 {
164   elfcpp::Elf_Xword addralign = shdr.get_sh_addralign();
165   if ((addralign & (addralign - 1)) != 0)
166     {
167       fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"),
168               program_name, object->name().c_str(),
169               static_cast<unsigned long>(addralign), secname);
170       gold_exit(false);
171     }
172
173   if (addralign > this->addralign_)
174     this->addralign_ = addralign;
175
176   off_t ssize = this->data_size();
177   ssize = (ssize + addralign - 1) &~ (addralign - 1);
178
179   // SHF_TLS/SHT_NOBITS sections are handled specially: they are
180   // treated as having no size and taking up no space.  We only use
181   // the real size when setting the pt_memsz field of the PT_TLS
182   // segment.
183   if ((this->flags_ & elfcpp::SHF_TLS) == 0
184       || this->type_ != elfcpp::SHT_NOBITS)
185     this->set_data_size(ssize + shdr.get_sh_size());
186
187   return size;
188 }
189
190 // Output_section_symtab methods.
191
192 Output_section_symtab::Output_section_symtab(const char* name, off_t size)
193   : Output_section(name, elfcpp::SHT_SYMTAB, 0)
194 {
195   this->set_data_size(size);
196 }
197
198 // Output_section_strtab methods.
199
200 Output_section_strtab::Output_section_strtab(const char* name,
201                                              Stringpool* contents)
202   : Output_section(name, elfcpp::SHT_STRTAB, 0),
203     contents_(contents)
204 {
205 }
206
207 void
208 Output_section_strtab::do_write(Output_file*)
209 {
210   // FIXME: Unimplemented.
211   abort();
212 }
213
214 // Output segment methods.
215
216 Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
217   : output_data_(),
218     output_bss_(),
219     vaddr_(0),
220     paddr_(0),
221     memsz_(0),
222     align_(0),
223     offset_(0),
224     filesz_(0),
225     type_(type),
226     flags_(flags)
227 {
228 }
229
230 // Add an Output_section to an Output_segment.
231
232 void
233 Output_segment::add_output_section(Output_section* os,
234                                    elfcpp::Elf_Word seg_flags)
235 {
236   assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
237
238   // Update the segment flags and alignment.
239   this->flags_ |= seg_flags;
240   uint64_t addralign = os->addralign();
241   if (addralign > this->align_)
242     this->align_ = addralign;
243
244   Output_segment::Output_data_list* pdl;
245   if (os->type() == elfcpp::SHT_NOBITS)
246     pdl = &this->output_bss_;
247   else
248     pdl = &this->output_data_;
249
250   // So that PT_NOTE segments will work correctly, we need to ensure
251   // that all SHT_NOTE sections are adjacent.  This will normally
252   // happen automatically, because all the SHT_NOTE input sections
253   // will wind up in the same output section.  However, it is possible
254   // for multiple SHT_NOTE input sections to have different section
255   // flags, and thus be in different output sections, but for the
256   // different section flags to map into the same segment flags and
257   // thus the same output segment.
258
259   // Note that while there may be many input sections in an output
260   // section, there are normally only a few output sections in an
261   // output segment.  This loop is expected to be fast.
262
263   if (os->type() == elfcpp::SHT_NOTE)
264     {
265       Layout::Data_list::iterator p = pdl->end();
266       do
267         {
268           --p;
269           if ((*p)->is_section_type(elfcpp::SHT_NOTE))
270             {
271               ++p;
272               pdl->insert(p, os);
273               return;
274             }
275         }
276       while (p != pdl->begin());
277     }
278
279   // Similarly, so that PT_TLS segments will work, we need to group
280   // SHF_TLS sections.  An SHF_TLS/SHT_NOBITS section is a special
281   // case: we group the SHF_TLS/SHT_NOBITS sections right after the
282   // SHF_TLS/SHT_PROGBITS sections.  This lets us set up PT_TLS
283   // correctly.
284   if ((os->flags() & elfcpp::SHF_TLS) != 0)
285     {
286       pdl = &this->output_data_;
287       bool nobits = os->type() == elfcpp::SHT_NOBITS;
288       Layout::Data_list::iterator p = pdl->end();
289       do
290         {
291           --p;
292           if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)
293               && (nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS)))
294             {
295               ++p;
296               pdl->insert(p, os);
297               return;
298             }
299         }
300       while (p != pdl->begin());
301     }
302
303   pdl->push_back(os);
304 }
305
306 // Add an Output_data (which is not an Output_section) to the start of
307 // a segment.
308
309 void
310 Output_segment::add_initial_output_data(Output_data* od)
311 {
312   uint64_t addralign = od->addralign();
313   if (addralign > this->align_)
314     this->align_ = addralign;
315
316   this->output_data_.push_front(od);
317 }
318
319 // Return the maximum alignment of the Output_data in Output_segment.
320 // We keep this up to date as we add Output_sections and Output_data.
321
322 uint64_t
323 Output_segment::max_data_align() const
324 {
325   return this->align_;
326 }
327
328 // Set the section addresses for an Output_segment.  ADDR is the
329 // address and *POFF is the file offset.  Return the address of the
330 // immediately following segment.  Update *POFF.
331
332 uint64_t
333 Output_segment::set_section_addresses(uint64_t addr, off_t* poff)
334 {
335   assert(this->type_ == elfcpp::PT_LOAD);
336
337   this->vaddr_ = addr;
338   this->paddr_ = addr;
339
340   off_t orig_off = *poff;
341   this->offset_ = orig_off;
342
343   addr = this->set_section_list_addresses(&this->output_data_, addr, poff);
344   this->filesz_ = *poff - orig_off;
345
346   off_t off = *poff;
347
348   return this->set_section_list_addresses(&this->output_bss_, addr, poff);
349   this->memsz_ = *poff - orig_off;
350
351   // Ignore the file offset adjustments made by the BSS Output_data
352   // objects.
353   *poff = off;
354 }
355
356 // Set the addresses in a list of Output_data structures.
357
358 uint64_t
359 Output_segment::set_section_list_addresses(Output_data_list* pdl,
360                                            uint64_t addr, off_t* poff)
361 {
362   off_t off = *poff;
363
364   for (Output_data_list::iterator p = pdl->begin();
365        p != pdl->end();
366        ++p)
367     {
368       uint64_t addralign = (*p)->addralign();
369       addr = (addr + addralign - 1) & ~ (addralign - 1);
370       off = (off + addralign - 1) & ~ (addralign - 1);
371       (*p)->set_address(addr, off);
372
373       uint64_t size = (*p)->data_size();
374       addr += size;
375       off += size;
376     }
377
378   *poff = off;
379   return addr;
380 }
381
382 // For a non-PT_LOAD segment, set the offset from the sections, if
383 // any.
384
385 void
386 Output_segment::set_offset()
387 {
388   assert(this->type_ != elfcpp::PT_LOAD);
389
390   if (this->output_data_.empty() && this->output_bss_.empty())
391     {
392       this->vaddr_ = 0;
393       this->paddr_ = 0;
394       this->memsz_ = 0;
395       this->align_ = 0;
396       this->offset_ = 0;
397       this->filesz_ = 0;
398       return;
399     }
400
401   const Output_data* first;
402   if (this->output_data_.empty())
403     first = this->output_bss_.front();
404   else
405     first = this->output_data_.front();
406   this->vaddr_ = first->address();
407   this->paddr_ = this->vaddr_;
408   this->offset_ = first->offset();
409
410   if (this->output_data_.empty())
411     this->filesz_ = 0;
412   else
413     {
414       const Output_data* last_data = this->output_data_.back();
415       this->filesz_ = (last_data->address()
416                        + last_data->data_size()
417                        - this->vaddr_);
418     }
419
420   const Output_data* last;
421   if (this->output_bss_.empty())
422     last = this->output_data_.back();
423   else
424     last = this->output_bss_.back();
425   this->memsz_ = (last->address()
426                   + last->data_size()
427                   - this->vaddr_);
428
429   // this->align_ was set as we added items.
430 }
431
432 // Return the number of Output_sections in an Output_segment.
433
434 unsigned int
435 Output_segment::output_section_count() const
436 {
437   return (this->output_section_count_list(&this->output_data_)
438           + this->output_section_count_list(&this->output_bss_));
439 }
440
441 // Return the number of Output_sections in an Output_data_list.
442
443 unsigned int
444 Output_segment::output_section_count_list(const Output_data_list* pdl) const
445 {
446   unsigned int count = 0;
447   for (Output_data_list::const_iterator p = pdl->begin();
448        p != pdl->end();
449        ++p)
450     {
451       if ((*p)->is_section())
452         ++count;
453     }
454   return count;
455 }
456
457 // Output_file methods.
458
459 void
460 Output_file::write(off_t, const void*, off_t)
461 {
462   abort();
463 }
464
465 // Instantiate the templates we need.  We could use the configure
466 // script to restrict this to only the ones for implemented targets.
467
468 template
469 off_t
470 Output_section::add_input_section<32, false>(
471     Object* object,
472     const char* secname,
473     const elfcpp::Shdr<32, false>& shdr);
474
475 template
476 off_t
477 Output_section::add_input_section<32, true>(
478     Object* object,
479     const char* secname,
480     const elfcpp::Shdr<32, true>& shdr);
481
482 template
483 off_t
484 Output_section::add_input_section<64, false>(
485     Object* object,
486     const char* secname,
487     const elfcpp::Shdr<64, false>& shdr);
488
489 template
490 off_t
491 Output_section::add_input_section<64, true>(
492     Object* object,
493     const char* secname,
494     const elfcpp::Shdr<64, true>& shdr);
495
496 } // End namespace gold.