Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // output.cc -- manage the output file for gold |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include <cstdlib> | |
61ba1cf9 ILT |
6 | #include <cerrno> |
7 | #include <fcntl.h> | |
8 | #include <unistd.h> | |
9 | #include <sys/mman.h> | |
75f65a3e | 10 | #include <algorithm> |
a2fb1b05 ILT |
11 | |
12 | #include "object.h" | |
ead1e424 ILT |
13 | #include "symtab.h" |
14 | #include "reloc.h" | |
a2fb1b05 ILT |
15 | #include "output.h" |
16 | ||
17 | namespace gold | |
18 | { | |
19 | ||
a3ad94ed ILT |
20 | // Output_data variables. |
21 | ||
22 | bool Output_data::sizes_are_fixed; | |
23 | ||
a2fb1b05 ILT |
24 | // Output_data methods. |
25 | ||
26 | Output_data::~Output_data() | |
27 | { | |
28 | } | |
29 | ||
75f65a3e ILT |
30 | // Set the address and offset. |
31 | ||
32 | void | |
33 | Output_data::set_address(uint64_t addr, off_t off) | |
34 | { | |
35 | this->address_ = addr; | |
36 | this->offset_ = off; | |
37 | ||
38 | // Let the child class know. | |
39 | this->do_set_address(addr, off); | |
40 | } | |
41 | ||
42 | // Return the default alignment for a size--32 or 64. | |
43 | ||
44 | uint64_t | |
45 | Output_data::default_alignment(int size) | |
46 | { | |
47 | if (size == 32) | |
48 | return 4; | |
49 | else if (size == 64) | |
50 | return 8; | |
51 | else | |
a3ad94ed | 52 | gold_unreachable(); |
75f65a3e ILT |
53 | } |
54 | ||
75f65a3e ILT |
55 | // Output_section_header methods. This currently assumes that the |
56 | // segment and section lists are complete at construction time. | |
57 | ||
58 | Output_section_headers::Output_section_headers( | |
59 | int size, | |
61ba1cf9 | 60 | bool big_endian, |
75f65a3e | 61 | const Layout::Segment_list& segment_list, |
a3ad94ed | 62 | const Layout::Section_list& unattached_section_list, |
61ba1cf9 | 63 | const Stringpool* secnamepool) |
75f65a3e | 64 | : size_(size), |
61ba1cf9 | 65 | big_endian_(big_endian), |
75f65a3e | 66 | segment_list_(segment_list), |
a3ad94ed | 67 | unattached_section_list_(unattached_section_list), |
61ba1cf9 | 68 | secnamepool_(secnamepool) |
75f65a3e | 69 | { |
61ba1cf9 ILT |
70 | // Count all the sections. Start with 1 for the null section. |
71 | off_t count = 1; | |
75f65a3e ILT |
72 | for (Layout::Segment_list::const_iterator p = segment_list.begin(); |
73 | p != segment_list.end(); | |
74 | ++p) | |
ead1e424 ILT |
75 | if ((*p)->type() == elfcpp::PT_LOAD) |
76 | count += (*p)->output_section_count(); | |
a3ad94ed | 77 | count += unattached_section_list.size(); |
75f65a3e ILT |
78 | |
79 | int shdr_size; | |
80 | if (size == 32) | |
81 | shdr_size = elfcpp::Elf_sizes<32>::shdr_size; | |
82 | else if (size == 64) | |
83 | shdr_size = elfcpp::Elf_sizes<64>::shdr_size; | |
84 | else | |
a3ad94ed | 85 | gold_unreachable(); |
75f65a3e ILT |
86 | |
87 | this->set_data_size(count * shdr_size); | |
88 | } | |
89 | ||
61ba1cf9 ILT |
90 | // Write out the section headers. |
91 | ||
75f65a3e | 92 | void |
61ba1cf9 | 93 | Output_section_headers::do_write(Output_file* of) |
a2fb1b05 | 94 | { |
61ba1cf9 ILT |
95 | if (this->size_ == 32) |
96 | { | |
97 | if (this->big_endian_) | |
98 | this->do_sized_write<32, true>(of); | |
99 | else | |
100 | this->do_sized_write<32, false>(of); | |
101 | } | |
102 | else if (this->size_ == 64) | |
103 | { | |
104 | if (this->big_endian_) | |
105 | this->do_sized_write<64, true>(of); | |
106 | else | |
107 | this->do_sized_write<64, false>(of); | |
108 | } | |
109 | else | |
a3ad94ed | 110 | gold_unreachable(); |
61ba1cf9 ILT |
111 | } |
112 | ||
113 | template<int size, bool big_endian> | |
114 | void | |
115 | Output_section_headers::do_sized_write(Output_file* of) | |
116 | { | |
117 | off_t all_shdrs_size = this->data_size(); | |
118 | unsigned char* view = of->get_output_view(this->offset(), all_shdrs_size); | |
119 | ||
120 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
121 | unsigned char* v = view; | |
122 | ||
123 | { | |
124 | typename elfcpp::Shdr_write<size, big_endian> oshdr(v); | |
125 | oshdr.put_sh_name(0); | |
126 | oshdr.put_sh_type(elfcpp::SHT_NULL); | |
127 | oshdr.put_sh_flags(0); | |
128 | oshdr.put_sh_addr(0); | |
129 | oshdr.put_sh_offset(0); | |
130 | oshdr.put_sh_size(0); | |
131 | oshdr.put_sh_link(0); | |
132 | oshdr.put_sh_info(0); | |
133 | oshdr.put_sh_addralign(0); | |
134 | oshdr.put_sh_entsize(0); | |
135 | } | |
136 | ||
137 | v += shdr_size; | |
138 | ||
ead1e424 | 139 | unsigned shndx = 1; |
61ba1cf9 ILT |
140 | for (Layout::Segment_list::const_iterator p = this->segment_list_.begin(); |
141 | p != this->segment_list_.end(); | |
142 | ++p) | |
593f47df | 143 | v = (*p)->write_section_headers SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( |
ead1e424 ILT |
144 | this->secnamepool_, v, &shndx |
145 | SELECT_SIZE_ENDIAN(size, big_endian)); | |
a3ad94ed ILT |
146 | for (Layout::Section_list::const_iterator p = |
147 | this->unattached_section_list_.begin(); | |
148 | p != this->unattached_section_list_.end(); | |
61ba1cf9 ILT |
149 | ++p) |
150 | { | |
a3ad94ed | 151 | gold_assert(shndx == (*p)->out_shndx()); |
61ba1cf9 ILT |
152 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
153 | (*p)->write_header(this->secnamepool_, &oshdr); | |
154 | v += shdr_size; | |
ead1e424 | 155 | ++shndx; |
61ba1cf9 ILT |
156 | } |
157 | ||
158 | of->write_output_view(this->offset(), all_shdrs_size, view); | |
a2fb1b05 ILT |
159 | } |
160 | ||
54dc6425 ILT |
161 | // Output_segment_header methods. |
162 | ||
61ba1cf9 ILT |
163 | Output_segment_headers::Output_segment_headers( |
164 | int size, | |
165 | bool big_endian, | |
166 | const Layout::Segment_list& segment_list) | |
167 | : size_(size), big_endian_(big_endian), segment_list_(segment_list) | |
168 | { | |
169 | int phdr_size; | |
170 | if (size == 32) | |
171 | phdr_size = elfcpp::Elf_sizes<32>::phdr_size; | |
172 | else if (size == 64) | |
173 | phdr_size = elfcpp::Elf_sizes<64>::phdr_size; | |
174 | else | |
a3ad94ed | 175 | gold_unreachable(); |
61ba1cf9 ILT |
176 | |
177 | this->set_data_size(segment_list.size() * phdr_size); | |
178 | } | |
179 | ||
54dc6425 | 180 | void |
61ba1cf9 | 181 | Output_segment_headers::do_write(Output_file* of) |
75f65a3e | 182 | { |
61ba1cf9 ILT |
183 | if (this->size_ == 32) |
184 | { | |
185 | if (this->big_endian_) | |
186 | this->do_sized_write<32, true>(of); | |
187 | else | |
188 | this->do_sized_write<32, false>(of); | |
189 | } | |
190 | else if (this->size_ == 64) | |
191 | { | |
192 | if (this->big_endian_) | |
193 | this->do_sized_write<64, true>(of); | |
194 | else | |
195 | this->do_sized_write<64, false>(of); | |
196 | } | |
197 | else | |
a3ad94ed | 198 | gold_unreachable(); |
61ba1cf9 ILT |
199 | } |
200 | ||
201 | template<int size, bool big_endian> | |
202 | void | |
203 | Output_segment_headers::do_sized_write(Output_file* of) | |
204 | { | |
205 | const int phdr_size = elfcpp::Elf_sizes<size>::phdr_size; | |
206 | off_t all_phdrs_size = this->segment_list_.size() * phdr_size; | |
207 | unsigned char* view = of->get_output_view(this->offset(), | |
208 | all_phdrs_size); | |
209 | unsigned char* v = view; | |
210 | for (Layout::Segment_list::const_iterator p = this->segment_list_.begin(); | |
211 | p != this->segment_list_.end(); | |
212 | ++p) | |
213 | { | |
214 | elfcpp::Phdr_write<size, big_endian> ophdr(v); | |
215 | (*p)->write_header(&ophdr); | |
216 | v += phdr_size; | |
217 | } | |
218 | ||
219 | of->write_output_view(this->offset(), all_phdrs_size, view); | |
75f65a3e ILT |
220 | } |
221 | ||
222 | // Output_file_header methods. | |
223 | ||
224 | Output_file_header::Output_file_header(int size, | |
61ba1cf9 | 225 | bool big_endian, |
75f65a3e ILT |
226 | const General_options& options, |
227 | const Target* target, | |
228 | const Symbol_table* symtab, | |
229 | const Output_segment_headers* osh) | |
230 | : size_(size), | |
61ba1cf9 | 231 | big_endian_(big_endian), |
75f65a3e ILT |
232 | options_(options), |
233 | target_(target), | |
234 | symtab_(symtab), | |
61ba1cf9 | 235 | segment_header_(osh), |
75f65a3e ILT |
236 | section_header_(NULL), |
237 | shstrtab_(NULL) | |
238 | { | |
61ba1cf9 ILT |
239 | int ehdr_size; |
240 | if (size == 32) | |
241 | ehdr_size = elfcpp::Elf_sizes<32>::ehdr_size; | |
242 | else if (size == 64) | |
243 | ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
244 | else | |
a3ad94ed | 245 | gold_unreachable(); |
61ba1cf9 ILT |
246 | |
247 | this->set_data_size(ehdr_size); | |
75f65a3e ILT |
248 | } |
249 | ||
250 | // Set the section table information for a file header. | |
251 | ||
252 | void | |
253 | Output_file_header::set_section_info(const Output_section_headers* shdrs, | |
254 | const Output_section* shstrtab) | |
255 | { | |
256 | this->section_header_ = shdrs; | |
257 | this->shstrtab_ = shstrtab; | |
258 | } | |
259 | ||
260 | // Write out the file header. | |
261 | ||
262 | void | |
61ba1cf9 | 263 | Output_file_header::do_write(Output_file* of) |
54dc6425 | 264 | { |
61ba1cf9 ILT |
265 | if (this->size_ == 32) |
266 | { | |
267 | if (this->big_endian_) | |
268 | this->do_sized_write<32, true>(of); | |
269 | else | |
270 | this->do_sized_write<32, false>(of); | |
271 | } | |
272 | else if (this->size_ == 64) | |
273 | { | |
274 | if (this->big_endian_) | |
275 | this->do_sized_write<64, true>(of); | |
276 | else | |
277 | this->do_sized_write<64, false>(of); | |
278 | } | |
279 | else | |
a3ad94ed | 280 | gold_unreachable(); |
61ba1cf9 ILT |
281 | } |
282 | ||
283 | // Write out the file header with appropriate size and endianess. | |
284 | ||
285 | template<int size, bool big_endian> | |
286 | void | |
287 | Output_file_header::do_sized_write(Output_file* of) | |
288 | { | |
a3ad94ed | 289 | gold_assert(this->offset() == 0); |
61ba1cf9 ILT |
290 | |
291 | int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size; | |
292 | unsigned char* view = of->get_output_view(0, ehdr_size); | |
293 | elfcpp::Ehdr_write<size, big_endian> oehdr(view); | |
294 | ||
295 | unsigned char e_ident[elfcpp::EI_NIDENT]; | |
296 | memset(e_ident, 0, elfcpp::EI_NIDENT); | |
297 | e_ident[elfcpp::EI_MAG0] = elfcpp::ELFMAG0; | |
298 | e_ident[elfcpp::EI_MAG1] = elfcpp::ELFMAG1; | |
299 | e_ident[elfcpp::EI_MAG2] = elfcpp::ELFMAG2; | |
300 | e_ident[elfcpp::EI_MAG3] = elfcpp::ELFMAG3; | |
301 | if (size == 32) | |
302 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS32; | |
303 | else if (size == 64) | |
304 | e_ident[elfcpp::EI_CLASS] = elfcpp::ELFCLASS64; | |
305 | else | |
a3ad94ed | 306 | gold_unreachable(); |
61ba1cf9 ILT |
307 | e_ident[elfcpp::EI_DATA] = (big_endian |
308 | ? elfcpp::ELFDATA2MSB | |
309 | : elfcpp::ELFDATA2LSB); | |
310 | e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT; | |
311 | // FIXME: Some targets may need to set EI_OSABI and EI_ABIVERSION. | |
312 | oehdr.put_e_ident(e_ident); | |
313 | ||
314 | elfcpp::ET e_type; | |
315 | // FIXME: ET_DYN. | |
316 | if (this->options_.is_relocatable()) | |
317 | e_type = elfcpp::ET_REL; | |
318 | else | |
319 | e_type = elfcpp::ET_EXEC; | |
320 | oehdr.put_e_type(e_type); | |
321 | ||
322 | oehdr.put_e_machine(this->target_->machine_code()); | |
323 | oehdr.put_e_version(elfcpp::EV_CURRENT); | |
324 | ||
ead1e424 | 325 | // FIXME: Need to support -e, and target specific entry symbol. |
61ba1cf9 ILT |
326 | Symbol* sym = this->symtab_->lookup("_start"); |
327 | typename Sized_symbol<size>::Value_type v; | |
328 | if (sym == NULL) | |
329 | v = 0; | |
330 | else | |
331 | { | |
332 | Sized_symbol<size>* ssym; | |
593f47df | 333 | ssym = this->symtab_->get_sized_symbol SELECT_SIZE_NAME(size) ( |
5482377d | 334 | sym SELECT_SIZE(size)); |
61ba1cf9 ILT |
335 | v = ssym->value(); |
336 | } | |
337 | oehdr.put_e_entry(v); | |
338 | ||
339 | oehdr.put_e_phoff(this->segment_header_->offset()); | |
340 | oehdr.put_e_shoff(this->section_header_->offset()); | |
341 | ||
342 | // FIXME: The target needs to set the flags. | |
343 | oehdr.put_e_flags(0); | |
344 | ||
345 | oehdr.put_e_ehsize(elfcpp::Elf_sizes<size>::ehdr_size); | |
346 | oehdr.put_e_phentsize(elfcpp::Elf_sizes<size>::phdr_size); | |
347 | oehdr.put_e_phnum(this->segment_header_->data_size() | |
348 | / elfcpp::Elf_sizes<size>::phdr_size); | |
349 | oehdr.put_e_shentsize(elfcpp::Elf_sizes<size>::shdr_size); | |
350 | oehdr.put_e_shnum(this->section_header_->data_size() | |
351 | / elfcpp::Elf_sizes<size>::shdr_size); | |
ead1e424 | 352 | oehdr.put_e_shstrndx(this->shstrtab_->out_shndx()); |
61ba1cf9 ILT |
353 | |
354 | of->write_output_view(0, ehdr_size, view); | |
54dc6425 ILT |
355 | } |
356 | ||
dbe717ef ILT |
357 | // Output_data_const methods. |
358 | ||
359 | void | |
a3ad94ed | 360 | Output_data_const::do_write(Output_file* of) |
dbe717ef | 361 | { |
a3ad94ed ILT |
362 | of->write(this->offset(), this->data_.data(), this->data_.size()); |
363 | } | |
364 | ||
365 | // Output_data_const_buffer methods. | |
366 | ||
367 | void | |
368 | Output_data_const_buffer::do_write(Output_file* of) | |
369 | { | |
370 | of->write(this->offset(), this->p_, this->data_size()); | |
dbe717ef ILT |
371 | } |
372 | ||
373 | // Output_section_data methods. | |
374 | ||
375 | unsigned int | |
376 | Output_section_data::do_out_shndx() const | |
377 | { | |
a3ad94ed | 378 | gold_assert(this->output_section_ != NULL); |
dbe717ef ILT |
379 | return this->output_section_->out_shndx(); |
380 | } | |
381 | ||
a3ad94ed ILT |
382 | // Output_data_strtab methods. |
383 | ||
384 | // Set the address. We don't actually care about the address, but we | |
385 | // do set our final size. | |
386 | ||
387 | void | |
388 | Output_data_strtab::do_set_address(uint64_t, off_t) | |
389 | { | |
390 | this->strtab_->set_string_offsets(); | |
391 | this->set_data_size(this->strtab_->get_strtab_size()); | |
392 | } | |
393 | ||
394 | // Write out a string table. | |
395 | ||
396 | void | |
397 | Output_data_strtab::do_write(Output_file* of) | |
398 | { | |
399 | this->strtab_->write(of, this->offset()); | |
400 | } | |
401 | ||
c06b7b0b ILT |
402 | // Output_reloc methods. |
403 | ||
404 | // Get the symbol index of a relocation. | |
405 | ||
406 | template<bool dynamic, int size, bool big_endian> | |
407 | unsigned int | |
408 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::get_symbol_index() | |
409 | const | |
410 | { | |
411 | unsigned int index; | |
412 | switch (this->local_sym_index_) | |
413 | { | |
414 | case INVALID_CODE: | |
a3ad94ed | 415 | gold_unreachable(); |
c06b7b0b ILT |
416 | |
417 | case GSYM_CODE: | |
418 | if (this->u_.gsym == NULL) | |
419 | index = 0; | |
420 | else if (dynamic) | |
421 | index = this->u_.gsym->dynsym_index(); | |
422 | else | |
423 | index = this->u_.gsym->symtab_index(); | |
424 | break; | |
425 | ||
426 | case SECTION_CODE: | |
427 | if (dynamic) | |
428 | index = this->u_.os->dynsym_index(); | |
429 | else | |
430 | index = this->u_.os->symtab_index(); | |
431 | break; | |
432 | ||
433 | default: | |
434 | if (dynamic) | |
435 | { | |
436 | // FIXME: It seems that some targets may need to generate | |
437 | // dynamic relocations against local symbols for some | |
438 | // reasons. This will have to be addressed at some point. | |
a3ad94ed | 439 | gold_unreachable(); |
c06b7b0b ILT |
440 | } |
441 | else | |
442 | index = this->u_.object->symtab_index(this->local_sym_index_); | |
443 | break; | |
444 | } | |
a3ad94ed | 445 | gold_assert(index != -1U); |
c06b7b0b ILT |
446 | return index; |
447 | } | |
448 | ||
449 | // Write out the offset and info fields of a Rel or Rela relocation | |
450 | // entry. | |
451 | ||
452 | template<bool dynamic, int size, bool big_endian> | |
453 | template<typename Write_rel> | |
454 | void | |
455 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write_rel( | |
456 | Write_rel* wr) const | |
457 | { | |
a3ad94ed ILT |
458 | Address address = this->address_; |
459 | if (this->od_ != NULL) | |
460 | address += this->od_->address(); | |
461 | wr->put_r_offset(address); | |
c06b7b0b ILT |
462 | wr->put_r_info(elfcpp::elf_r_info<size>(this->get_symbol_index(), |
463 | this->type_)); | |
464 | } | |
465 | ||
466 | // Write out a Rel relocation. | |
467 | ||
468 | template<bool dynamic, int size, bool big_endian> | |
469 | void | |
470 | Output_reloc<elfcpp::SHT_REL, dynamic, size, big_endian>::write( | |
471 | unsigned char* pov) const | |
472 | { | |
473 | elfcpp::Rel_write<size, big_endian> orel(pov); | |
474 | this->write_rel(&orel); | |
475 | } | |
476 | ||
477 | // Write out a Rela relocation. | |
478 | ||
479 | template<bool dynamic, int size, bool big_endian> | |
480 | void | |
481 | Output_reloc<elfcpp::SHT_RELA, dynamic, size, big_endian>::write( | |
482 | unsigned char* pov) const | |
483 | { | |
484 | elfcpp::Rela_write<size, big_endian> orel(pov); | |
485 | this->rel_.write_rel(&orel); | |
486 | orel.put_r_addend(this->addend_); | |
487 | } | |
488 | ||
489 | // Output_data_reloc_base methods. | |
490 | ||
491 | // Write out relocation data. | |
492 | ||
493 | template<int sh_type, bool dynamic, int size, bool big_endian> | |
494 | void | |
495 | Output_data_reloc_base<sh_type, dynamic, size, big_endian>::do_write( | |
496 | Output_file* of) | |
497 | { | |
498 | const off_t off = this->offset(); | |
499 | const off_t oview_size = this->data_size(); | |
500 | unsigned char* const oview = of->get_output_view(off, oview_size); | |
501 | ||
502 | unsigned char* pov = oview; | |
503 | for (typename Relocs::const_iterator p = this->relocs_.begin(); | |
504 | p != this->relocs_.end(); | |
505 | ++p) | |
506 | { | |
507 | p->write(pov); | |
508 | pov += reloc_size; | |
509 | } | |
510 | ||
a3ad94ed | 511 | gold_assert(pov - oview == oview_size); |
c06b7b0b ILT |
512 | |
513 | of->write_output_view(off, oview_size, oview); | |
514 | ||
515 | // We no longer need the relocation entries. | |
516 | this->relocs_.clear(); | |
517 | } | |
518 | ||
dbe717ef | 519 | // Output_data_got::Got_entry methods. |
ead1e424 ILT |
520 | |
521 | // Write out the entry. | |
522 | ||
523 | template<int size, bool big_endian> | |
524 | void | |
a3ad94ed ILT |
525 | Output_data_got<size, big_endian>::Got_entry::write( |
526 | const General_options* options, | |
527 | unsigned char* pov) const | |
ead1e424 ILT |
528 | { |
529 | Valtype val = 0; | |
530 | ||
531 | switch (this->local_sym_index_) | |
532 | { | |
533 | case GSYM_CODE: | |
534 | { | |
535 | Symbol* gsym = this->u_.gsym; | |
536 | ||
537 | // If the symbol is resolved locally, we need to write out its | |
538 | // value. Otherwise we just write zero. The target code is | |
539 | // responsible for creating a relocation entry to fill in the | |
540 | // value at runtime. | |
a3ad94ed | 541 | if (gsym->final_value_is_known(options)) |
ead1e424 ILT |
542 | { |
543 | Sized_symbol<size>* sgsym; | |
544 | // This cast is a bit ugly. We don't want to put a | |
545 | // virtual method in Symbol, because we want Symbol to be | |
546 | // as small as possible. | |
547 | sgsym = static_cast<Sized_symbol<size>*>(gsym); | |
548 | val = sgsym->value(); | |
549 | } | |
550 | } | |
551 | break; | |
552 | ||
553 | case CONSTANT_CODE: | |
554 | val = this->u_.constant; | |
555 | break; | |
556 | ||
557 | default: | |
a3ad94ed | 558 | gold_unreachable(); |
ead1e424 ILT |
559 | } |
560 | ||
a3ad94ed | 561 | elfcpp::Swap<size, big_endian>::writeval(pov, val); |
ead1e424 ILT |
562 | } |
563 | ||
dbe717ef | 564 | // Output_data_got methods. |
ead1e424 | 565 | |
dbe717ef ILT |
566 | // Add an entry for a global symbol to the GOT. This returns true if |
567 | // this is a new GOT entry, false if the symbol already had a GOT | |
568 | // entry. | |
569 | ||
570 | template<int size, bool big_endian> | |
571 | bool | |
572 | Output_data_got<size, big_endian>::add_global(Symbol* gsym) | |
ead1e424 | 573 | { |
dbe717ef ILT |
574 | if (gsym->has_got_offset()) |
575 | return false; | |
ead1e424 | 576 | |
dbe717ef ILT |
577 | this->entries_.push_back(Got_entry(gsym)); |
578 | this->set_got_size(); | |
579 | gsym->set_got_offset(this->last_got_offset()); | |
580 | return true; | |
581 | } | |
ead1e424 ILT |
582 | |
583 | // Write out the GOT. | |
584 | ||
585 | template<int size, bool big_endian> | |
586 | void | |
dbe717ef | 587 | Output_data_got<size, big_endian>::do_write(Output_file* of) |
ead1e424 ILT |
588 | { |
589 | const int add = size / 8; | |
590 | ||
591 | const off_t off = this->offset(); | |
c06b7b0b | 592 | const off_t oview_size = this->data_size(); |
ead1e424 ILT |
593 | unsigned char* const oview = of->get_output_view(off, oview_size); |
594 | ||
595 | unsigned char* pov = oview; | |
596 | for (typename Got_entries::const_iterator p = this->entries_.begin(); | |
597 | p != this->entries_.end(); | |
598 | ++p) | |
599 | { | |
a3ad94ed | 600 | p->write(this->options_, pov); |
ead1e424 ILT |
601 | pov += add; |
602 | } | |
603 | ||
a3ad94ed | 604 | gold_assert(pov - oview == oview_size); |
c06b7b0b | 605 | |
ead1e424 ILT |
606 | of->write_output_view(off, oview_size, oview); |
607 | ||
608 | // We no longer need the GOT entries. | |
609 | this->entries_.clear(); | |
610 | } | |
611 | ||
a3ad94ed ILT |
612 | // Output_data_dynamic::Dynamic_entry methods. |
613 | ||
614 | // Write out the entry. | |
615 | ||
616 | template<int size, bool big_endian> | |
617 | void | |
618 | Output_data_dynamic::Dynamic_entry::write( | |
619 | unsigned char* pov, | |
620 | const Stringpool* pool) const | |
621 | { | |
622 | typename elfcpp::Elf_types<size>::Elf_WXword val; | |
623 | switch (this->classification_) | |
624 | { | |
625 | case DYNAMIC_NUMBER: | |
626 | val = this->u_.val; | |
627 | break; | |
628 | ||
629 | case DYNAMIC_SECTION_ADDRESS: | |
630 | val = this->u_.os->address(); | |
631 | break; | |
632 | ||
633 | case DYNAMIC_SECTION_SIZE: | |
634 | val = this->u_.os->data_size(); | |
635 | break; | |
636 | ||
637 | case DYNAMIC_SYMBOL: | |
638 | { | |
639 | Sized_symbol<size>* s = static_cast<Sized_symbol<size>*>(this->u_.sym); | |
640 | val = s->value(); | |
641 | } | |
642 | break; | |
643 | ||
644 | case DYNAMIC_STRING: | |
645 | val = pool->get_offset(this->u_.str); | |
646 | break; | |
647 | ||
648 | default: | |
649 | gold_unreachable(); | |
650 | } | |
651 | ||
652 | elfcpp::Dyn_write<size, big_endian> dw(pov); | |
653 | dw.put_d_tag(this->tag_); | |
654 | dw.put_d_val(val); | |
655 | } | |
656 | ||
657 | // Output_data_dynamic methods. | |
658 | ||
659 | // Set the final data size. | |
660 | ||
661 | void | |
662 | Output_data_dynamic::do_set_address(uint64_t, off_t) | |
663 | { | |
664 | // Add the terminating entry. | |
665 | this->add_constant(elfcpp::DT_NULL, 0); | |
666 | ||
667 | int dyn_size; | |
668 | if (this->target_->get_size() == 32) | |
669 | dyn_size = elfcpp::Elf_sizes<32>::dyn_size; | |
670 | else if (this->target_->get_size() == 64) | |
671 | dyn_size = elfcpp::Elf_sizes<64>::dyn_size; | |
672 | else | |
673 | gold_unreachable(); | |
674 | this->set_data_size(this->entries_.size() * dyn_size); | |
675 | } | |
676 | ||
677 | // Write out the dynamic entries. | |
678 | ||
679 | void | |
680 | Output_data_dynamic::do_write(Output_file* of) | |
681 | { | |
682 | if (this->target_->get_size() == 32) | |
683 | { | |
684 | if (this->target_->is_big_endian()) | |
685 | this->sized_write<32, true>(of); | |
686 | else | |
687 | this->sized_write<32, false>(of); | |
688 | } | |
689 | else if (this->target_->get_size() == 64) | |
690 | { | |
691 | if (this->target_->is_big_endian()) | |
692 | this->sized_write<64, true>(of); | |
693 | else | |
694 | this->sized_write<64, false>(of); | |
695 | } | |
696 | else | |
697 | gold_unreachable(); | |
698 | } | |
699 | ||
700 | template<int size, bool big_endian> | |
701 | void | |
702 | Output_data_dynamic::sized_write(Output_file* of) | |
703 | { | |
704 | const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size; | |
705 | ||
706 | const off_t offset = this->offset(); | |
707 | const off_t oview_size = this->data_size(); | |
708 | unsigned char* const oview = of->get_output_view(offset, oview_size); | |
709 | ||
710 | unsigned char* pov = oview; | |
711 | for (typename Dynamic_entries::const_iterator p = this->entries_.begin(); | |
712 | p != this->entries_.end(); | |
713 | ++p) | |
714 | { | |
715 | p->write<size, big_endian>(pov, this->pool_); | |
716 | pov += dyn_size; | |
717 | } | |
718 | ||
719 | gold_assert(pov - oview == oview_size); | |
720 | ||
721 | of->write_output_view(offset, oview_size, oview); | |
722 | ||
723 | // We no longer need the dynamic entries. | |
724 | this->entries_.clear(); | |
725 | } | |
726 | ||
ead1e424 ILT |
727 | // Output_section::Input_section methods. |
728 | ||
729 | // Return the data size. For an input section we store the size here. | |
730 | // For an Output_section_data, we have to ask it for the size. | |
731 | ||
732 | off_t | |
733 | Output_section::Input_section::data_size() const | |
734 | { | |
735 | if (this->is_input_section()) | |
736 | return this->data_size_; | |
737 | else | |
738 | return this->u_.posd->data_size(); | |
739 | } | |
740 | ||
741 | // Set the address and file offset. | |
742 | ||
743 | void | |
744 | Output_section::Input_section::set_address(uint64_t addr, off_t off, | |
745 | off_t secoff) | |
746 | { | |
747 | if (this->is_input_section()) | |
748 | this->u_.object->set_section_offset(this->shndx_, off - secoff); | |
749 | else | |
750 | this->u_.posd->set_address(addr, off); | |
751 | } | |
752 | ||
753 | // Write out the data. We don't have to do anything for an input | |
754 | // section--they are handled via Object::relocate--but this is where | |
755 | // we write out the data for an Output_section_data. | |
756 | ||
757 | void | |
758 | Output_section::Input_section::write(Output_file* of) | |
759 | { | |
760 | if (!this->is_input_section()) | |
761 | this->u_.posd->write(of); | |
762 | } | |
763 | ||
a2fb1b05 ILT |
764 | // Output_section methods. |
765 | ||
766 | // Construct an Output_section. NAME will point into a Stringpool. | |
767 | ||
768 | Output_section::Output_section(const char* name, elfcpp::Elf_Word type, | |
ead1e424 | 769 | elfcpp::Elf_Xword flags, bool may_add_data) |
a2fb1b05 | 770 | : name_(name), |
a2fb1b05 ILT |
771 | addralign_(0), |
772 | entsize_(0), | |
a2fb1b05 ILT |
773 | link_(0), |
774 | info_(0), | |
775 | type_(type), | |
61ba1cf9 | 776 | flags_(flags), |
ead1e424 | 777 | out_shndx_(0), |
c06b7b0b ILT |
778 | symtab_index_(0), |
779 | dynsym_index_(0), | |
ead1e424 ILT |
780 | input_sections_(), |
781 | first_input_offset_(0), | |
a3ad94ed ILT |
782 | may_add_data_(may_add_data), |
783 | needs_symtab_index_(false), | |
784 | needs_dynsym_index_(false) | |
a2fb1b05 ILT |
785 | { |
786 | } | |
787 | ||
54dc6425 ILT |
788 | Output_section::~Output_section() |
789 | { | |
790 | } | |
791 | ||
ead1e424 ILT |
792 | // Add the input section SHNDX, with header SHDR, named SECNAME, in |
793 | // OBJECT, to the Output_section. Return the offset of the input | |
794 | // section within the output section. We don't always keep track of | |
a2fb1b05 ILT |
795 | // input sections for an Output_section. Instead, each Object keeps |
796 | // track of the Output_section for each of its input sections. | |
797 | ||
798 | template<int size, bool big_endian> | |
799 | off_t | |
f6ce93d6 | 800 | Output_section::add_input_section(Relobj* object, unsigned int shndx, |
ead1e424 | 801 | const char* secname, |
a2fb1b05 ILT |
802 | const elfcpp::Shdr<size, big_endian>& shdr) |
803 | { | |
a3ad94ed | 804 | gold_assert(this->may_add_data_); |
ead1e424 | 805 | |
a2fb1b05 ILT |
806 | elfcpp::Elf_Xword addralign = shdr.get_sh_addralign(); |
807 | if ((addralign & (addralign - 1)) != 0) | |
808 | { | |
809 | fprintf(stderr, _("%s: %s: invalid alignment %lu for section \"%s\"\n"), | |
810 | program_name, object->name().c_str(), | |
811 | static_cast<unsigned long>(addralign), secname); | |
812 | gold_exit(false); | |
813 | } | |
a2fb1b05 ILT |
814 | |
815 | if (addralign > this->addralign_) | |
816 | this->addralign_ = addralign; | |
817 | ||
75f65a3e | 818 | off_t ssize = this->data_size(); |
ead1e424 ILT |
819 | ssize = align_address(ssize, addralign); |
820 | this->set_data_size(ssize + shdr.get_sh_size()); | |
a2fb1b05 | 821 | |
ead1e424 ILT |
822 | // We need to keep track of this section if we are already keeping |
823 | // track of sections, or if we are relaxing. FIXME: Add test for | |
824 | // relaxing. | |
825 | if (! this->input_sections_.empty()) | |
826 | this->input_sections_.push_back(Input_section(object, shndx, | |
827 | shdr.get_sh_size(), | |
828 | addralign)); | |
54dc6425 | 829 | |
61ba1cf9 ILT |
830 | return ssize; |
831 | } | |
832 | ||
ead1e424 ILT |
833 | // Add arbitrary data to an output section. |
834 | ||
835 | void | |
836 | Output_section::add_output_section_data(Output_section_data* posd) | |
837 | { | |
a3ad94ed | 838 | gold_assert(this->may_add_data_); |
c06b7b0b | 839 | |
ead1e424 ILT |
840 | if (this->input_sections_.empty()) |
841 | this->first_input_offset_ = this->data_size(); | |
c06b7b0b | 842 | |
ead1e424 | 843 | this->input_sections_.push_back(Input_section(posd)); |
c06b7b0b | 844 | |
ead1e424 ILT |
845 | uint64_t addralign = posd->addralign(); |
846 | if (addralign > this->addralign_) | |
847 | this->addralign_ = addralign; | |
c06b7b0b | 848 | |
ead1e424 ILT |
849 | posd->set_output_section(this); |
850 | } | |
851 | ||
852 | // Set the address of an Output_section. This is where we handle | |
853 | // setting the addresses of any Output_section_data objects. | |
854 | ||
855 | void | |
856 | Output_section::do_set_address(uint64_t address, off_t startoff) | |
857 | { | |
858 | if (this->input_sections_.empty()) | |
859 | return; | |
860 | ||
861 | off_t off = startoff + this->first_input_offset_; | |
862 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
863 | p != this->input_sections_.end(); | |
864 | ++p) | |
865 | { | |
866 | off = align_address(off, p->addralign()); | |
867 | p->set_address(address + (off - startoff), off, startoff); | |
868 | off += p->data_size(); | |
869 | } | |
870 | ||
871 | this->set_data_size(off - startoff); | |
872 | } | |
873 | ||
61ba1cf9 ILT |
874 | // Write the section header to *OSHDR. |
875 | ||
876 | template<int size, bool big_endian> | |
877 | void | |
878 | Output_section::write_header(const Stringpool* secnamepool, | |
879 | elfcpp::Shdr_write<size, big_endian>* oshdr) const | |
880 | { | |
881 | oshdr->put_sh_name(secnamepool->get_offset(this->name_)); | |
882 | oshdr->put_sh_type(this->type_); | |
883 | oshdr->put_sh_flags(this->flags_); | |
884 | oshdr->put_sh_addr(this->address()); | |
885 | oshdr->put_sh_offset(this->offset()); | |
886 | oshdr->put_sh_size(this->data_size()); | |
887 | oshdr->put_sh_link(this->link_); | |
888 | oshdr->put_sh_info(this->info_); | |
889 | oshdr->put_sh_addralign(this->addralign_); | |
890 | oshdr->put_sh_entsize(this->entsize_); | |
a2fb1b05 ILT |
891 | } |
892 | ||
ead1e424 ILT |
893 | // Write out the data. For input sections the data is written out by |
894 | // Object::relocate, but we have to handle Output_section_data objects | |
895 | // here. | |
896 | ||
897 | void | |
898 | Output_section::do_write(Output_file* of) | |
899 | { | |
900 | for (Input_section_list::iterator p = this->input_sections_.begin(); | |
901 | p != this->input_sections_.end(); | |
902 | ++p) | |
903 | p->write(of); | |
904 | } | |
905 | ||
a2fb1b05 ILT |
906 | // Output segment methods. |
907 | ||
908 | Output_segment::Output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags) | |
54dc6425 | 909 | : output_data_(), |
75f65a3e | 910 | output_bss_(), |
a2fb1b05 ILT |
911 | vaddr_(0), |
912 | paddr_(0), | |
913 | memsz_(0), | |
914 | align_(0), | |
915 | offset_(0), | |
916 | filesz_(0), | |
917 | type_(type), | |
ead1e424 ILT |
918 | flags_(flags), |
919 | is_align_known_(false) | |
a2fb1b05 ILT |
920 | { |
921 | } | |
922 | ||
923 | // Add an Output_section to an Output_segment. | |
924 | ||
925 | void | |
75f65a3e | 926 | Output_segment::add_output_section(Output_section* os, |
dbe717ef ILT |
927 | elfcpp::Elf_Word seg_flags, |
928 | bool front) | |
a2fb1b05 | 929 | { |
a3ad94ed ILT |
930 | gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0); |
931 | gold_assert(!this->is_align_known_); | |
75f65a3e | 932 | |
ead1e424 | 933 | // Update the segment flags. |
75f65a3e | 934 | this->flags_ |= seg_flags; |
75f65a3e ILT |
935 | |
936 | Output_segment::Output_data_list* pdl; | |
937 | if (os->type() == elfcpp::SHT_NOBITS) | |
938 | pdl = &this->output_bss_; | |
939 | else | |
940 | pdl = &this->output_data_; | |
54dc6425 | 941 | |
a2fb1b05 ILT |
942 | // So that PT_NOTE segments will work correctly, we need to ensure |
943 | // that all SHT_NOTE sections are adjacent. This will normally | |
944 | // happen automatically, because all the SHT_NOTE input sections | |
945 | // will wind up in the same output section. However, it is possible | |
946 | // for multiple SHT_NOTE input sections to have different section | |
947 | // flags, and thus be in different output sections, but for the | |
948 | // different section flags to map into the same segment flags and | |
949 | // thus the same output segment. | |
54dc6425 ILT |
950 | |
951 | // Note that while there may be many input sections in an output | |
952 | // section, there are normally only a few output sections in an | |
953 | // output segment. This loop is expected to be fast. | |
954 | ||
61ba1cf9 | 955 | if (os->type() == elfcpp::SHT_NOTE && !pdl->empty()) |
a2fb1b05 | 956 | { |
a3ad94ed | 957 | Output_segment::Output_data_list::iterator p = pdl->end(); |
75f65a3e | 958 | do |
54dc6425 | 959 | { |
75f65a3e | 960 | --p; |
54dc6425 ILT |
961 | if ((*p)->is_section_type(elfcpp::SHT_NOTE)) |
962 | { | |
dbe717ef | 963 | // We don't worry about the FRONT parameter. |
54dc6425 | 964 | ++p; |
75f65a3e | 965 | pdl->insert(p, os); |
54dc6425 ILT |
966 | return; |
967 | } | |
968 | } | |
75f65a3e | 969 | while (p != pdl->begin()); |
54dc6425 ILT |
970 | } |
971 | ||
972 | // Similarly, so that PT_TLS segments will work, we need to group | |
75f65a3e ILT |
973 | // SHF_TLS sections. An SHF_TLS/SHT_NOBITS section is a special |
974 | // case: we group the SHF_TLS/SHT_NOBITS sections right after the | |
975 | // SHF_TLS/SHT_PROGBITS sections. This lets us set up PT_TLS | |
976 | // correctly. | |
61ba1cf9 | 977 | if ((os->flags() & elfcpp::SHF_TLS) != 0 && !this->output_data_.empty()) |
54dc6425 | 978 | { |
75f65a3e ILT |
979 | pdl = &this->output_data_; |
980 | bool nobits = os->type() == elfcpp::SHT_NOBITS; | |
ead1e424 | 981 | bool sawtls = false; |
a3ad94ed | 982 | Output_segment::Output_data_list::iterator p = pdl->end(); |
75f65a3e | 983 | do |
a2fb1b05 | 984 | { |
75f65a3e | 985 | --p; |
ead1e424 ILT |
986 | bool insert; |
987 | if ((*p)->is_section_flag_set(elfcpp::SHF_TLS)) | |
988 | { | |
989 | sawtls = true; | |
990 | // Put a NOBITS section after the first TLS section. | |
991 | // But a PROGBITS section after the first TLS/PROGBITS | |
992 | // section. | |
993 | insert = nobits || !(*p)->is_section_type(elfcpp::SHT_NOBITS); | |
994 | } | |
995 | else | |
996 | { | |
997 | // If we've gone past the TLS sections, but we've seen a | |
998 | // TLS section, then we need to insert this section now. | |
999 | insert = sawtls; | |
1000 | } | |
1001 | ||
1002 | if (insert) | |
a2fb1b05 | 1003 | { |
dbe717ef | 1004 | // We don't worry about the FRONT parameter. |
a2fb1b05 | 1005 | ++p; |
75f65a3e | 1006 | pdl->insert(p, os); |
a2fb1b05 ILT |
1007 | return; |
1008 | } | |
1009 | } | |
75f65a3e | 1010 | while (p != pdl->begin()); |
ead1e424 | 1011 | |
dbe717ef ILT |
1012 | // There are no TLS sections yet; put this one at the requested |
1013 | // location in the section list. | |
a2fb1b05 ILT |
1014 | } |
1015 | ||
dbe717ef ILT |
1016 | if (front) |
1017 | pdl->push_front(os); | |
1018 | else | |
1019 | pdl->push_back(os); | |
75f65a3e ILT |
1020 | } |
1021 | ||
1022 | // Add an Output_data (which is not an Output_section) to the start of | |
1023 | // a segment. | |
1024 | ||
1025 | void | |
1026 | Output_segment::add_initial_output_data(Output_data* od) | |
1027 | { | |
a3ad94ed | 1028 | gold_assert(!this->is_align_known_); |
75f65a3e ILT |
1029 | this->output_data_.push_front(od); |
1030 | } | |
1031 | ||
1032 | // Return the maximum alignment of the Output_data in Output_segment. | |
ead1e424 | 1033 | // Once we compute this, we prohibit new sections from being added. |
75f65a3e ILT |
1034 | |
1035 | uint64_t | |
ead1e424 | 1036 | Output_segment::addralign() |
75f65a3e | 1037 | { |
ead1e424 ILT |
1038 | if (!this->is_align_known_) |
1039 | { | |
1040 | uint64_t addralign; | |
1041 | ||
1042 | addralign = Output_segment::maximum_alignment(&this->output_data_); | |
1043 | if (addralign > this->align_) | |
1044 | this->align_ = addralign; | |
1045 | ||
1046 | addralign = Output_segment::maximum_alignment(&this->output_bss_); | |
1047 | if (addralign > this->align_) | |
1048 | this->align_ = addralign; | |
1049 | ||
1050 | this->is_align_known_ = true; | |
1051 | } | |
1052 | ||
75f65a3e ILT |
1053 | return this->align_; |
1054 | } | |
1055 | ||
ead1e424 ILT |
1056 | // Return the maximum alignment of a list of Output_data. |
1057 | ||
1058 | uint64_t | |
1059 | Output_segment::maximum_alignment(const Output_data_list* pdl) | |
1060 | { | |
1061 | uint64_t ret = 0; | |
1062 | for (Output_data_list::const_iterator p = pdl->begin(); | |
1063 | p != pdl->end(); | |
1064 | ++p) | |
1065 | { | |
1066 | uint64_t addralign = (*p)->addralign(); | |
1067 | if (addralign > ret) | |
1068 | ret = addralign; | |
1069 | } | |
1070 | return ret; | |
1071 | } | |
1072 | ||
75f65a3e | 1073 | // Set the section addresses for an Output_segment. ADDR is the |
ead1e424 ILT |
1074 | // address and *POFF is the file offset. Set the section indexes |
1075 | // starting with *PSHNDX. Return the address of the immediately | |
1076 | // following segment. Update *POFF and *PSHNDX. | |
75f65a3e ILT |
1077 | |
1078 | uint64_t | |
ead1e424 ILT |
1079 | Output_segment::set_section_addresses(uint64_t addr, off_t* poff, |
1080 | unsigned int* pshndx) | |
75f65a3e | 1081 | { |
a3ad94ed | 1082 | gold_assert(this->type_ == elfcpp::PT_LOAD); |
75f65a3e ILT |
1083 | |
1084 | this->vaddr_ = addr; | |
1085 | this->paddr_ = addr; | |
1086 | ||
1087 | off_t orig_off = *poff; | |
1088 | this->offset_ = orig_off; | |
1089 | ||
ead1e424 ILT |
1090 | *poff = align_address(*poff, this->addralign()); |
1091 | ||
1092 | addr = this->set_section_list_addresses(&this->output_data_, addr, poff, | |
1093 | pshndx); | |
75f65a3e ILT |
1094 | this->filesz_ = *poff - orig_off; |
1095 | ||
1096 | off_t off = *poff; | |
1097 | ||
61ba1cf9 | 1098 | uint64_t ret = this->set_section_list_addresses(&this->output_bss_, addr, |
ead1e424 | 1099 | poff, pshndx); |
75f65a3e ILT |
1100 | this->memsz_ = *poff - orig_off; |
1101 | ||
1102 | // Ignore the file offset adjustments made by the BSS Output_data | |
1103 | // objects. | |
1104 | *poff = off; | |
61ba1cf9 ILT |
1105 | |
1106 | return ret; | |
75f65a3e ILT |
1107 | } |
1108 | ||
1109 | // Set the addresses in a list of Output_data structures. | |
1110 | ||
1111 | uint64_t | |
1112 | Output_segment::set_section_list_addresses(Output_data_list* pdl, | |
ead1e424 ILT |
1113 | uint64_t addr, off_t* poff, |
1114 | unsigned int* pshndx) | |
75f65a3e | 1115 | { |
ead1e424 | 1116 | off_t startoff = *poff; |
75f65a3e | 1117 | |
ead1e424 | 1118 | off_t off = startoff; |
75f65a3e ILT |
1119 | for (Output_data_list::iterator p = pdl->begin(); |
1120 | p != pdl->end(); | |
1121 | ++p) | |
1122 | { | |
ead1e424 ILT |
1123 | off = align_address(off, (*p)->addralign()); |
1124 | (*p)->set_address(addr + (off - startoff), off); | |
1125 | ||
1126 | // Unless this is a PT_TLS segment, we want to ignore the size | |
1127 | // of a SHF_TLS/SHT_NOBITS section. Such a section does not | |
1128 | // affect the size of a PT_LOAD segment. | |
1129 | if (this->type_ == elfcpp::PT_TLS | |
1130 | || !(*p)->is_section_flag_set(elfcpp::SHF_TLS) | |
1131 | || !(*p)->is_section_type(elfcpp::SHT_NOBITS)) | |
1132 | off += (*p)->data_size(); | |
75f65a3e | 1133 | |
ead1e424 ILT |
1134 | if ((*p)->is_section()) |
1135 | { | |
1136 | (*p)->set_out_shndx(*pshndx); | |
1137 | ++*pshndx; | |
1138 | } | |
75f65a3e ILT |
1139 | } |
1140 | ||
1141 | *poff = off; | |
ead1e424 | 1142 | return addr + (off - startoff); |
75f65a3e ILT |
1143 | } |
1144 | ||
1145 | // For a non-PT_LOAD segment, set the offset from the sections, if | |
1146 | // any. | |
1147 | ||
1148 | void | |
1149 | Output_segment::set_offset() | |
1150 | { | |
a3ad94ed | 1151 | gold_assert(this->type_ != elfcpp::PT_LOAD); |
75f65a3e ILT |
1152 | |
1153 | if (this->output_data_.empty() && this->output_bss_.empty()) | |
1154 | { | |
1155 | this->vaddr_ = 0; | |
1156 | this->paddr_ = 0; | |
1157 | this->memsz_ = 0; | |
1158 | this->align_ = 0; | |
1159 | this->offset_ = 0; | |
1160 | this->filesz_ = 0; | |
1161 | return; | |
1162 | } | |
1163 | ||
1164 | const Output_data* first; | |
1165 | if (this->output_data_.empty()) | |
1166 | first = this->output_bss_.front(); | |
1167 | else | |
1168 | first = this->output_data_.front(); | |
1169 | this->vaddr_ = first->address(); | |
1170 | this->paddr_ = this->vaddr_; | |
1171 | this->offset_ = first->offset(); | |
1172 | ||
1173 | if (this->output_data_.empty()) | |
1174 | this->filesz_ = 0; | |
1175 | else | |
1176 | { | |
1177 | const Output_data* last_data = this->output_data_.back(); | |
1178 | this->filesz_ = (last_data->address() | |
1179 | + last_data->data_size() | |
1180 | - this->vaddr_); | |
1181 | } | |
1182 | ||
1183 | const Output_data* last; | |
1184 | if (this->output_bss_.empty()) | |
1185 | last = this->output_data_.back(); | |
1186 | else | |
1187 | last = this->output_bss_.back(); | |
1188 | this->memsz_ = (last->address() | |
1189 | + last->data_size() | |
1190 | - this->vaddr_); | |
75f65a3e ILT |
1191 | } |
1192 | ||
1193 | // Return the number of Output_sections in an Output_segment. | |
1194 | ||
1195 | unsigned int | |
1196 | Output_segment::output_section_count() const | |
1197 | { | |
1198 | return (this->output_section_count_list(&this->output_data_) | |
1199 | + this->output_section_count_list(&this->output_bss_)); | |
1200 | } | |
1201 | ||
1202 | // Return the number of Output_sections in an Output_data_list. | |
1203 | ||
1204 | unsigned int | |
1205 | Output_segment::output_section_count_list(const Output_data_list* pdl) const | |
1206 | { | |
1207 | unsigned int count = 0; | |
1208 | for (Output_data_list::const_iterator p = pdl->begin(); | |
1209 | p != pdl->end(); | |
1210 | ++p) | |
1211 | { | |
1212 | if ((*p)->is_section()) | |
1213 | ++count; | |
1214 | } | |
1215 | return count; | |
a2fb1b05 ILT |
1216 | } |
1217 | ||
61ba1cf9 ILT |
1218 | // Write the segment data into *OPHDR. |
1219 | ||
1220 | template<int size, bool big_endian> | |
1221 | void | |
ead1e424 | 1222 | Output_segment::write_header(elfcpp::Phdr_write<size, big_endian>* ophdr) |
61ba1cf9 ILT |
1223 | { |
1224 | ophdr->put_p_type(this->type_); | |
1225 | ophdr->put_p_offset(this->offset_); | |
1226 | ophdr->put_p_vaddr(this->vaddr_); | |
1227 | ophdr->put_p_paddr(this->paddr_); | |
1228 | ophdr->put_p_filesz(this->filesz_); | |
1229 | ophdr->put_p_memsz(this->memsz_); | |
1230 | ophdr->put_p_flags(this->flags_); | |
ead1e424 | 1231 | ophdr->put_p_align(this->addralign()); |
61ba1cf9 ILT |
1232 | } |
1233 | ||
1234 | // Write the section headers into V. | |
1235 | ||
1236 | template<int size, bool big_endian> | |
1237 | unsigned char* | |
1238 | Output_segment::write_section_headers(const Stringpool* secnamepool, | |
ead1e424 ILT |
1239 | unsigned char* v, |
1240 | unsigned int *pshndx | |
5482377d ILT |
1241 | ACCEPT_SIZE_ENDIAN) const |
1242 | { | |
ead1e424 ILT |
1243 | // Every section that is attached to a segment must be attached to a |
1244 | // PT_LOAD segment, so we only write out section headers for PT_LOAD | |
1245 | // segments. | |
1246 | if (this->type_ != elfcpp::PT_LOAD) | |
1247 | return v; | |
1248 | ||
593f47df ILT |
1249 | v = this->write_section_headers_list |
1250 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
1251 | secnamepool, &this->output_data_, v, pshndx | |
1252 | SELECT_SIZE_ENDIAN(size, big_endian)); | |
1253 | v = this->write_section_headers_list | |
1254 | SELECT_SIZE_ENDIAN_NAME(size, big_endian) ( | |
1255 | secnamepool, &this->output_bss_, v, pshndx | |
1256 | SELECT_SIZE_ENDIAN(size, big_endian)); | |
61ba1cf9 ILT |
1257 | return v; |
1258 | } | |
1259 | ||
1260 | template<int size, bool big_endian> | |
1261 | unsigned char* | |
1262 | Output_segment::write_section_headers_list(const Stringpool* secnamepool, | |
1263 | const Output_data_list* pdl, | |
ead1e424 ILT |
1264 | unsigned char* v, |
1265 | unsigned int* pshndx | |
5482377d | 1266 | ACCEPT_SIZE_ENDIAN) const |
61ba1cf9 ILT |
1267 | { |
1268 | const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size; | |
1269 | for (Output_data_list::const_iterator p = pdl->begin(); | |
1270 | p != pdl->end(); | |
1271 | ++p) | |
1272 | { | |
1273 | if ((*p)->is_section()) | |
1274 | { | |
5482377d | 1275 | const Output_section* ps = static_cast<const Output_section*>(*p); |
a3ad94ed | 1276 | gold_assert(*pshndx == ps->out_shndx()); |
61ba1cf9 ILT |
1277 | elfcpp::Shdr_write<size, big_endian> oshdr(v); |
1278 | ps->write_header(secnamepool, &oshdr); | |
1279 | v += shdr_size; | |
ead1e424 | 1280 | ++*pshndx; |
61ba1cf9 ILT |
1281 | } |
1282 | } | |
1283 | return v; | |
1284 | } | |
1285 | ||
a2fb1b05 ILT |
1286 | // Output_file methods. |
1287 | ||
61ba1cf9 ILT |
1288 | Output_file::Output_file(const General_options& options) |
1289 | : options_(options), | |
1290 | name_(options.output_file_name()), | |
1291 | o_(-1), | |
1292 | file_size_(0), | |
1293 | base_(NULL) | |
1294 | { | |
1295 | } | |
1296 | ||
1297 | // Open the output file. | |
1298 | ||
a2fb1b05 | 1299 | void |
61ba1cf9 | 1300 | Output_file::open(off_t file_size) |
a2fb1b05 | 1301 | { |
61ba1cf9 ILT |
1302 | this->file_size_ = file_size; |
1303 | ||
1304 | int mode = this->options_.is_relocatable() ? 0666 : 0777; | |
1305 | int o = ::open(this->name_, O_RDWR | O_CREAT | O_TRUNC, mode); | |
1306 | if (o < 0) | |
1307 | { | |
1308 | fprintf(stderr, _("%s: %s: open: %s\n"), | |
1309 | program_name, this->name_, strerror(errno)); | |
1310 | gold_exit(false); | |
1311 | } | |
1312 | this->o_ = o; | |
1313 | ||
1314 | // Write out one byte to make the file the right size. | |
1315 | if (::lseek(o, file_size - 1, SEEK_SET) < 0) | |
1316 | { | |
1317 | fprintf(stderr, _("%s: %s: lseek: %s\n"), | |
1318 | program_name, this->name_, strerror(errno)); | |
1319 | gold_exit(false); | |
1320 | } | |
1321 | char b = 0; | |
1322 | if (::write(o, &b, 1) != 1) | |
1323 | { | |
1324 | fprintf(stderr, _("%s: %s: write: %s\n"), | |
1325 | program_name, this->name_, strerror(errno)); | |
1326 | gold_exit(false); | |
1327 | } | |
1328 | ||
1329 | // Map the file into memory. | |
1330 | void* base = ::mmap(NULL, file_size, PROT_READ | PROT_WRITE, | |
1331 | MAP_SHARED, o, 0); | |
1332 | if (base == MAP_FAILED) | |
1333 | { | |
1334 | fprintf(stderr, _("%s: %s: mmap: %s\n"), | |
1335 | program_name, this->name_, strerror(errno)); | |
1336 | gold_exit(false); | |
1337 | } | |
1338 | this->base_ = static_cast<unsigned char*>(base); | |
1339 | } | |
1340 | ||
1341 | // Close the output file. | |
1342 | ||
1343 | void | |
1344 | Output_file::close() | |
1345 | { | |
1346 | if (::munmap(this->base_, this->file_size_) < 0) | |
1347 | { | |
1348 | fprintf(stderr, _("%s: %s: munmap: %s\n"), | |
1349 | program_name, this->name_, strerror(errno)); | |
1350 | gold_exit(false); | |
1351 | } | |
1352 | this->base_ = NULL; | |
1353 | ||
1354 | if (::close(this->o_) < 0) | |
1355 | { | |
1356 | fprintf(stderr, _("%s: %s: close: %s\n"), | |
1357 | program_name, this->name_, strerror(errno)); | |
1358 | gold_exit(false); | |
1359 | } | |
1360 | this->o_ = -1; | |
a2fb1b05 ILT |
1361 | } |
1362 | ||
1363 | // Instantiate the templates we need. We could use the configure | |
1364 | // script to restrict this to only the ones for implemented targets. | |
1365 | ||
1366 | template | |
1367 | off_t | |
1368 | Output_section::add_input_section<32, false>( | |
f6ce93d6 | 1369 | Relobj* object, |
ead1e424 | 1370 | unsigned int shndx, |
a2fb1b05 ILT |
1371 | const char* secname, |
1372 | const elfcpp::Shdr<32, false>& shdr); | |
1373 | ||
1374 | template | |
1375 | off_t | |
1376 | Output_section::add_input_section<32, true>( | |
f6ce93d6 | 1377 | Relobj* object, |
ead1e424 | 1378 | unsigned int shndx, |
a2fb1b05 ILT |
1379 | const char* secname, |
1380 | const elfcpp::Shdr<32, true>& shdr); | |
1381 | ||
1382 | template | |
1383 | off_t | |
1384 | Output_section::add_input_section<64, false>( | |
f6ce93d6 | 1385 | Relobj* object, |
ead1e424 | 1386 | unsigned int shndx, |
a2fb1b05 ILT |
1387 | const char* secname, |
1388 | const elfcpp::Shdr<64, false>& shdr); | |
1389 | ||
1390 | template | |
1391 | off_t | |
1392 | Output_section::add_input_section<64, true>( | |
f6ce93d6 | 1393 | Relobj* object, |
ead1e424 | 1394 | unsigned int shndx, |
a2fb1b05 ILT |
1395 | const char* secname, |
1396 | const elfcpp::Shdr<64, true>& shdr); | |
1397 | ||
c06b7b0b ILT |
1398 | template |
1399 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, false>; | |
1400 | ||
1401 | template | |
1402 | class Output_data_reloc<elfcpp::SHT_REL, false, 32, true>; | |
1403 | ||
1404 | template | |
1405 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, false>; | |
1406 | ||
1407 | template | |
1408 | class Output_data_reloc<elfcpp::SHT_REL, false, 64, true>; | |
1409 | ||
1410 | template | |
1411 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, false>; | |
1412 | ||
1413 | template | |
1414 | class Output_data_reloc<elfcpp::SHT_REL, true, 32, true>; | |
1415 | ||
1416 | template | |
1417 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, false>; | |
1418 | ||
1419 | template | |
1420 | class Output_data_reloc<elfcpp::SHT_REL, true, 64, true>; | |
1421 | ||
1422 | template | |
1423 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, false>; | |
1424 | ||
1425 | template | |
1426 | class Output_data_reloc<elfcpp::SHT_RELA, false, 32, true>; | |
1427 | ||
1428 | template | |
1429 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, false>; | |
1430 | ||
1431 | template | |
1432 | class Output_data_reloc<elfcpp::SHT_RELA, false, 64, true>; | |
1433 | ||
1434 | template | |
1435 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, false>; | |
1436 | ||
1437 | template | |
1438 | class Output_data_reloc<elfcpp::SHT_RELA, true, 32, true>; | |
1439 | ||
1440 | template | |
1441 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, false>; | |
1442 | ||
1443 | template | |
1444 | class Output_data_reloc<elfcpp::SHT_RELA, true, 64, true>; | |
1445 | ||
ead1e424 | 1446 | template |
dbe717ef | 1447 | class Output_data_got<32, false>; |
ead1e424 ILT |
1448 | |
1449 | template | |
dbe717ef | 1450 | class Output_data_got<32, true>; |
ead1e424 ILT |
1451 | |
1452 | template | |
dbe717ef | 1453 | class Output_data_got<64, false>; |
ead1e424 ILT |
1454 | |
1455 | template | |
dbe717ef | 1456 | class Output_data_got<64, true>; |
ead1e424 | 1457 | |
a2fb1b05 | 1458 | } // End namespace gold. |