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